node.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * drivers/base/node.c - basic Node class support
  3. */
  4. #include <linux/sysdev.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/mm.h>
  8. #include <linux/memory.h>
  9. #include <linux/node.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/topology.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/cpu.h>
  15. #include <linux/device.h>
  16. #include <linux/swap.h>
  17. static struct sysdev_class node_class = {
  18. .name = "node",
  19. };
  20. static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
  21. {
  22. struct node *node_dev = to_node(dev);
  23. const struct cpumask *mask = cpumask_of_node(node_dev->sysdev.id);
  24. int len;
  25. /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
  26. BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
  27. len = type?
  28. cpulist_scnprintf(buf, PAGE_SIZE-2, mask) :
  29. cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
  30. buf[len++] = '\n';
  31. buf[len] = '\0';
  32. return len;
  33. }
  34. static inline ssize_t node_read_cpumask(struct sys_device *dev,
  35. struct sysdev_attribute *attr, char *buf)
  36. {
  37. return node_read_cpumap(dev, 0, buf);
  38. }
  39. static inline ssize_t node_read_cpulist(struct sys_device *dev,
  40. struct sysdev_attribute *attr, char *buf)
  41. {
  42. return node_read_cpumap(dev, 1, buf);
  43. }
  44. static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
  45. static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
  46. #define K(x) ((x) << (PAGE_SHIFT - 10))
  47. static ssize_t node_read_meminfo(struct sys_device * dev,
  48. struct sysdev_attribute *attr, char * buf)
  49. {
  50. int n;
  51. int nid = dev->id;
  52. struct sysinfo i;
  53. si_meminfo_node(&i, nid);
  54. n = sprintf(buf, "\n"
  55. "Node %d MemTotal: %8lu kB\n"
  56. "Node %d MemFree: %8lu kB\n"
  57. "Node %d MemUsed: %8lu kB\n"
  58. "Node %d Active: %8lu kB\n"
  59. "Node %d Inactive: %8lu kB\n"
  60. "Node %d Active(anon): %8lu kB\n"
  61. "Node %d Inactive(anon): %8lu kB\n"
  62. "Node %d Active(file): %8lu kB\n"
  63. "Node %d Inactive(file): %8lu kB\n"
  64. "Node %d Unevictable: %8lu kB\n"
  65. "Node %d Mlocked: %8lu kB\n"
  66. #ifdef CONFIG_HIGHMEM
  67. "Node %d HighTotal: %8lu kB\n"
  68. "Node %d HighFree: %8lu kB\n"
  69. "Node %d LowTotal: %8lu kB\n"
  70. "Node %d LowFree: %8lu kB\n"
  71. #endif
  72. "Node %d Dirty: %8lu kB\n"
  73. "Node %d Writeback: %8lu kB\n"
  74. "Node %d FilePages: %8lu kB\n"
  75. "Node %d Mapped: %8lu kB\n"
  76. "Node %d AnonPages: %8lu kB\n"
  77. "Node %d PageTables: %8lu kB\n"
  78. "Node %d NFS_Unstable: %8lu kB\n"
  79. "Node %d Bounce: %8lu kB\n"
  80. "Node %d WritebackTmp: %8lu kB\n"
  81. "Node %d Slab: %8lu kB\n"
  82. "Node %d SReclaimable: %8lu kB\n"
  83. "Node %d SUnreclaim: %8lu kB\n",
  84. nid, K(i.totalram),
  85. nid, K(i.freeram),
  86. nid, K(i.totalram - i.freeram),
  87. nid, K(node_page_state(nid, NR_ACTIVE_ANON) +
  88. node_page_state(nid, NR_ACTIVE_FILE)),
  89. nid, K(node_page_state(nid, NR_INACTIVE_ANON) +
  90. node_page_state(nid, NR_INACTIVE_FILE)),
  91. nid, K(node_page_state(nid, NR_ACTIVE_ANON)),
  92. nid, K(node_page_state(nid, NR_INACTIVE_ANON)),
  93. nid, K(node_page_state(nid, NR_ACTIVE_FILE)),
  94. nid, K(node_page_state(nid, NR_INACTIVE_FILE)),
  95. nid, K(node_page_state(nid, NR_UNEVICTABLE)),
  96. nid, K(node_page_state(nid, NR_MLOCK)),
  97. #ifdef CONFIG_HIGHMEM
  98. nid, K(i.totalhigh),
  99. nid, K(i.freehigh),
  100. nid, K(i.totalram - i.totalhigh),
  101. nid, K(i.freeram - i.freehigh),
  102. #endif
  103. nid, K(node_page_state(nid, NR_FILE_DIRTY)),
  104. nid, K(node_page_state(nid, NR_WRITEBACK)),
  105. nid, K(node_page_state(nid, NR_FILE_PAGES)),
  106. nid, K(node_page_state(nid, NR_FILE_MAPPED)),
  107. nid, K(node_page_state(nid, NR_ANON_PAGES)),
  108. nid, K(node_page_state(nid, NR_PAGETABLE)),
  109. nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
  110. nid, K(node_page_state(nid, NR_BOUNCE)),
  111. nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
  112. nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
  113. node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
  114. nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
  115. nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
  116. n += hugetlb_report_node_meminfo(nid, buf + n);
  117. return n;
  118. }
  119. #undef K
  120. static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
  121. static ssize_t node_read_numastat(struct sys_device * dev,
  122. struct sysdev_attribute *attr, char * buf)
  123. {
  124. return sprintf(buf,
  125. "numa_hit %lu\n"
  126. "numa_miss %lu\n"
  127. "numa_foreign %lu\n"
  128. "interleave_hit %lu\n"
  129. "local_node %lu\n"
  130. "other_node %lu\n",
  131. node_page_state(dev->id, NUMA_HIT),
  132. node_page_state(dev->id, NUMA_MISS),
  133. node_page_state(dev->id, NUMA_FOREIGN),
  134. node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
  135. node_page_state(dev->id, NUMA_LOCAL),
  136. node_page_state(dev->id, NUMA_OTHER));
  137. }
  138. static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
  139. static ssize_t node_read_distance(struct sys_device * dev,
  140. struct sysdev_attribute *attr, char * buf)
  141. {
  142. int nid = dev->id;
  143. int len = 0;
  144. int i;
  145. /* buf currently PAGE_SIZE, need ~4 chars per node */
  146. BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
  147. for_each_online_node(i)
  148. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  149. len += sprintf(buf + len, "\n");
  150. return len;
  151. }
  152. static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  153. /*
  154. * register_node - Setup a sysfs device for a node.
  155. * @num - Node number to use when creating the device.
  156. *
  157. * Initialize and register the node device.
  158. */
  159. int register_node(struct node *node, int num, struct node *parent)
  160. {
  161. int error;
  162. node->sysdev.id = num;
  163. node->sysdev.cls = &node_class;
  164. error = sysdev_register(&node->sysdev);
  165. if (!error){
  166. sysdev_create_file(&node->sysdev, &attr_cpumap);
  167. sysdev_create_file(&node->sysdev, &attr_cpulist);
  168. sysdev_create_file(&node->sysdev, &attr_meminfo);
  169. sysdev_create_file(&node->sysdev, &attr_numastat);
  170. sysdev_create_file(&node->sysdev, &attr_distance);
  171. scan_unevictable_register_node(node);
  172. }
  173. return error;
  174. }
  175. /**
  176. * unregister_node - unregister a node device
  177. * @node: node going away
  178. *
  179. * Unregisters a node device @node. All the devices on the node must be
  180. * unregistered before calling this function.
  181. */
  182. void unregister_node(struct node *node)
  183. {
  184. sysdev_remove_file(&node->sysdev, &attr_cpumap);
  185. sysdev_remove_file(&node->sysdev, &attr_cpulist);
  186. sysdev_remove_file(&node->sysdev, &attr_meminfo);
  187. sysdev_remove_file(&node->sysdev, &attr_numastat);
  188. sysdev_remove_file(&node->sysdev, &attr_distance);
  189. scan_unevictable_unregister_node(node);
  190. sysdev_unregister(&node->sysdev);
  191. }
  192. struct node node_devices[MAX_NUMNODES];
  193. /*
  194. * register cpu under node
  195. */
  196. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  197. {
  198. if (node_online(nid)) {
  199. struct sys_device *obj = get_cpu_sysdev(cpu);
  200. if (!obj)
  201. return 0;
  202. return sysfs_create_link(&node_devices[nid].sysdev.kobj,
  203. &obj->kobj,
  204. kobject_name(&obj->kobj));
  205. }
  206. return 0;
  207. }
  208. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  209. {
  210. if (node_online(nid)) {
  211. struct sys_device *obj = get_cpu_sysdev(cpu);
  212. if (obj)
  213. sysfs_remove_link(&node_devices[nid].sysdev.kobj,
  214. kobject_name(&obj->kobj));
  215. }
  216. return 0;
  217. }
  218. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  219. #define page_initialized(page) (page->lru.next)
  220. static int get_nid_for_pfn(unsigned long pfn)
  221. {
  222. struct page *page;
  223. if (!pfn_valid_within(pfn))
  224. return -1;
  225. page = pfn_to_page(pfn);
  226. if (!page_initialized(page))
  227. return -1;
  228. return pfn_to_nid(pfn);
  229. }
  230. /* register memory section under specified node if it spans that node */
  231. int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
  232. {
  233. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  234. if (!mem_blk)
  235. return -EFAULT;
  236. if (!node_online(nid))
  237. return 0;
  238. sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
  239. sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
  240. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  241. int page_nid;
  242. page_nid = get_nid_for_pfn(pfn);
  243. if (page_nid < 0)
  244. continue;
  245. if (page_nid != nid)
  246. continue;
  247. return sysfs_create_link_nowarn(&node_devices[nid].sysdev.kobj,
  248. &mem_blk->sysdev.kobj,
  249. kobject_name(&mem_blk->sysdev.kobj));
  250. }
  251. /* mem section does not span the specified node */
  252. return 0;
  253. }
  254. /* unregister memory section under all nodes that it spans */
  255. int unregister_mem_sect_under_nodes(struct memory_block *mem_blk)
  256. {
  257. nodemask_t unlinked_nodes;
  258. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  259. if (!mem_blk)
  260. return -EFAULT;
  261. nodes_clear(unlinked_nodes);
  262. sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index);
  263. sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
  264. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  265. int nid;
  266. nid = get_nid_for_pfn(pfn);
  267. if (nid < 0)
  268. continue;
  269. if (!node_online(nid))
  270. continue;
  271. if (node_test_and_set(nid, unlinked_nodes))
  272. continue;
  273. sysfs_remove_link(&node_devices[nid].sysdev.kobj,
  274. kobject_name(&mem_blk->sysdev.kobj));
  275. }
  276. return 0;
  277. }
  278. static int link_mem_sections(int nid)
  279. {
  280. unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
  281. unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
  282. unsigned long pfn;
  283. int err = 0;
  284. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  285. unsigned long section_nr = pfn_to_section_nr(pfn);
  286. struct mem_section *mem_sect;
  287. struct memory_block *mem_blk;
  288. int ret;
  289. if (!present_section_nr(section_nr))
  290. continue;
  291. mem_sect = __nr_to_section(section_nr);
  292. mem_blk = find_memory_block(mem_sect);
  293. ret = register_mem_sect_under_node(mem_blk, nid);
  294. if (!err)
  295. err = ret;
  296. /* discard ref obtained in find_memory_block() */
  297. kobject_put(&mem_blk->sysdev.kobj);
  298. }
  299. return err;
  300. }
  301. #else
  302. static int link_mem_sections(int nid) { return 0; }
  303. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  304. int register_one_node(int nid)
  305. {
  306. int error = 0;
  307. int cpu;
  308. if (node_online(nid)) {
  309. int p_node = parent_node(nid);
  310. struct node *parent = NULL;
  311. if (p_node != nid)
  312. parent = &node_devices[p_node];
  313. error = register_node(&node_devices[nid], nid, parent);
  314. /* link cpu under this node */
  315. for_each_present_cpu(cpu) {
  316. if (cpu_to_node(cpu) == nid)
  317. register_cpu_under_node(cpu, nid);
  318. }
  319. /* link memory sections under this node */
  320. error = link_mem_sections(nid);
  321. }
  322. return error;
  323. }
  324. void unregister_one_node(int nid)
  325. {
  326. unregister_node(&node_devices[nid]);
  327. }
  328. /*
  329. * node states attributes
  330. */
  331. static ssize_t print_nodes_state(enum node_states state, char *buf)
  332. {
  333. int n;
  334. n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
  335. if (n > 0 && PAGE_SIZE > n + 1) {
  336. *(buf + n++) = '\n';
  337. *(buf + n++) = '\0';
  338. }
  339. return n;
  340. }
  341. static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
  342. {
  343. return print_nodes_state(N_POSSIBLE, buf);
  344. }
  345. static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
  346. {
  347. return print_nodes_state(N_ONLINE, buf);
  348. }
  349. static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
  350. char *buf)
  351. {
  352. return print_nodes_state(N_NORMAL_MEMORY, buf);
  353. }
  354. static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
  355. {
  356. return print_nodes_state(N_CPU, buf);
  357. }
  358. static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
  359. static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
  360. static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
  361. NULL);
  362. static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
  363. #ifdef CONFIG_HIGHMEM
  364. static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
  365. char *buf)
  366. {
  367. return print_nodes_state(N_HIGH_MEMORY, buf);
  368. }
  369. static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
  370. NULL);
  371. #endif
  372. struct sysdev_class_attribute *node_state_attr[] = {
  373. &attr_possible,
  374. &attr_online,
  375. &attr_has_normal_memory,
  376. #ifdef CONFIG_HIGHMEM
  377. &attr_has_high_memory,
  378. #endif
  379. &attr_has_cpu,
  380. };
  381. static int node_states_init(void)
  382. {
  383. int i;
  384. int err = 0;
  385. for (i = 0; i < NR_NODE_STATES; i++) {
  386. int ret;
  387. ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
  388. if (!err)
  389. err = ret;
  390. }
  391. return err;
  392. }
  393. static int __init register_node_type(void)
  394. {
  395. int ret;
  396. ret = sysdev_class_register(&node_class);
  397. if (!ret)
  398. ret = node_states_init();
  399. /*
  400. * Note: we're not going to unregister the node class if we fail
  401. * to register the node state class attribute files.
  402. */
  403. return ret;
  404. }
  405. postcore_initcall(register_node_type);