node.c 13 KB

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