node.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * Basic Node interface support
  3. */
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/mm.h>
  7. #include <linux/memory.h>
  8. #include <linux/vmstat.h>
  9. #include <linux/notifier.h>
  10. #include <linux/node.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/compaction.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/topology.h>
  15. #include <linux/nodemask.h>
  16. #include <linux/cpu.h>
  17. #include <linux/device.h>
  18. #include <linux/swap.h>
  19. #include <linux/slab.h>
  20. static struct bus_type node_subsys = {
  21. .name = "node",
  22. .dev_name = "node",
  23. };
  24. static ssize_t node_read_cpumap(struct device *dev, int type, char *buf)
  25. {
  26. struct node *node_dev = to_node(dev);
  27. const struct cpumask *mask = cpumask_of_node(node_dev->dev.id);
  28. int len;
  29. /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
  30. BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
  31. len = type?
  32. cpulist_scnprintf(buf, PAGE_SIZE-2, mask) :
  33. cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
  34. buf[len++] = '\n';
  35. buf[len] = '\0';
  36. return len;
  37. }
  38. static inline ssize_t node_read_cpumask(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. return node_read_cpumap(dev, 0, buf);
  42. }
  43. static inline ssize_t node_read_cpulist(struct device *dev,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. return node_read_cpumap(dev, 1, buf);
  47. }
  48. static DEVICE_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
  49. static DEVICE_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
  50. #define K(x) ((x) << (PAGE_SHIFT - 10))
  51. static ssize_t node_read_meminfo(struct device *dev,
  52. struct device_attribute *attr, char *buf)
  53. {
  54. int n;
  55. int nid = dev->id;
  56. struct sysinfo i;
  57. si_meminfo_node(&i, nid);
  58. n = sprintf(buf,
  59. "Node %d MemTotal: %8lu kB\n"
  60. "Node %d MemFree: %8lu kB\n"
  61. "Node %d MemUsed: %8lu kB\n"
  62. "Node %d Active: %8lu kB\n"
  63. "Node %d Inactive: %8lu kB\n"
  64. "Node %d Active(anon): %8lu kB\n"
  65. "Node %d Inactive(anon): %8lu kB\n"
  66. "Node %d Active(file): %8lu kB\n"
  67. "Node %d Inactive(file): %8lu kB\n"
  68. "Node %d Unevictable: %8lu kB\n"
  69. "Node %d Mlocked: %8lu kB\n",
  70. nid, K(i.totalram),
  71. nid, K(i.freeram),
  72. nid, K(i.totalram - i.freeram),
  73. nid, K(node_page_state(nid, NR_ACTIVE_ANON) +
  74. node_page_state(nid, NR_ACTIVE_FILE)),
  75. nid, K(node_page_state(nid, NR_INACTIVE_ANON) +
  76. node_page_state(nid, NR_INACTIVE_FILE)),
  77. nid, K(node_page_state(nid, NR_ACTIVE_ANON)),
  78. nid, K(node_page_state(nid, NR_INACTIVE_ANON)),
  79. nid, K(node_page_state(nid, NR_ACTIVE_FILE)),
  80. nid, K(node_page_state(nid, NR_INACTIVE_FILE)),
  81. nid, K(node_page_state(nid, NR_UNEVICTABLE)),
  82. nid, K(node_page_state(nid, NR_MLOCK)));
  83. #ifdef CONFIG_HIGHMEM
  84. n += sprintf(buf + n,
  85. "Node %d HighTotal: %8lu kB\n"
  86. "Node %d HighFree: %8lu kB\n"
  87. "Node %d LowTotal: %8lu kB\n"
  88. "Node %d LowFree: %8lu kB\n",
  89. nid, K(i.totalhigh),
  90. nid, K(i.freehigh),
  91. nid, K(i.totalram - i.totalhigh),
  92. nid, K(i.freeram - i.freehigh));
  93. #endif
  94. n += sprintf(buf + n,
  95. "Node %d Dirty: %8lu kB\n"
  96. "Node %d Writeback: %8lu kB\n"
  97. "Node %d FilePages: %8lu kB\n"
  98. "Node %d Mapped: %8lu kB\n"
  99. "Node %d AnonPages: %8lu kB\n"
  100. "Node %d Shmem: %8lu kB\n"
  101. "Node %d KernelStack: %8lu kB\n"
  102. "Node %d PageTables: %8lu kB\n"
  103. "Node %d NFS_Unstable: %8lu kB\n"
  104. "Node %d Bounce: %8lu kB\n"
  105. "Node %d WritebackTmp: %8lu kB\n"
  106. "Node %d Slab: %8lu kB\n"
  107. "Node %d SReclaimable: %8lu kB\n"
  108. "Node %d SUnreclaim: %8lu kB\n"
  109. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  110. "Node %d AnonHugePages: %8lu kB\n"
  111. #endif
  112. ,
  113. nid, K(node_page_state(nid, NR_FILE_DIRTY)),
  114. nid, K(node_page_state(nid, NR_WRITEBACK)),
  115. nid, K(node_page_state(nid, NR_FILE_PAGES)),
  116. nid, K(node_page_state(nid, NR_FILE_MAPPED)),
  117. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  118. nid, K(node_page_state(nid, NR_ANON_PAGES)
  119. + node_page_state(nid, NR_ANON_TRANSPARENT_HUGEPAGES) *
  120. HPAGE_PMD_NR),
  121. #else
  122. nid, K(node_page_state(nid, NR_ANON_PAGES)),
  123. #endif
  124. nid, K(node_page_state(nid, NR_SHMEM)),
  125. nid, node_page_state(nid, NR_KERNEL_STACK) *
  126. THREAD_SIZE / 1024,
  127. nid, K(node_page_state(nid, NR_PAGETABLE)),
  128. nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
  129. nid, K(node_page_state(nid, NR_BOUNCE)),
  130. nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
  131. nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
  132. node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
  133. nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
  134. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  135. nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE))
  136. , nid,
  137. K(node_page_state(nid, NR_ANON_TRANSPARENT_HUGEPAGES) *
  138. HPAGE_PMD_NR));
  139. #else
  140. nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
  141. #endif
  142. n += hugetlb_report_node_meminfo(nid, buf + n);
  143. return n;
  144. }
  145. #undef K
  146. static DEVICE_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
  147. static ssize_t node_read_numastat(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. return sprintf(buf,
  151. "numa_hit %lu\n"
  152. "numa_miss %lu\n"
  153. "numa_foreign %lu\n"
  154. "interleave_hit %lu\n"
  155. "local_node %lu\n"
  156. "other_node %lu\n",
  157. node_page_state(dev->id, NUMA_HIT),
  158. node_page_state(dev->id, NUMA_MISS),
  159. node_page_state(dev->id, NUMA_FOREIGN),
  160. node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
  161. node_page_state(dev->id, NUMA_LOCAL),
  162. node_page_state(dev->id, NUMA_OTHER));
  163. }
  164. static DEVICE_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
  165. static ssize_t node_read_vmstat(struct device *dev,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. int nid = dev->id;
  169. int i;
  170. int n = 0;
  171. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  172. n += sprintf(buf+n, "%s %lu\n", vmstat_text[i],
  173. node_page_state(nid, i));
  174. return n;
  175. }
  176. static DEVICE_ATTR(vmstat, S_IRUGO, node_read_vmstat, NULL);
  177. static ssize_t node_read_distance(struct device *dev,
  178. struct device_attribute *attr, char * buf)
  179. {
  180. int nid = dev->id;
  181. int len = 0;
  182. int i;
  183. /*
  184. * buf is currently PAGE_SIZE in length and each node needs 4 chars
  185. * at the most (distance + space or newline).
  186. */
  187. BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
  188. for_each_online_node(i)
  189. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  190. len += sprintf(buf + len, "\n");
  191. return len;
  192. }
  193. static DEVICE_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  194. #ifdef CONFIG_HUGETLBFS
  195. /*
  196. * hugetlbfs per node attributes registration interface:
  197. * When/if hugetlb[fs] subsystem initializes [sometime after this module],
  198. * it will register its per node attributes for all online nodes with
  199. * memory. It will also call register_hugetlbfs_with_node(), below, to
  200. * register its attribute registration functions with this node driver.
  201. * Once these hooks have been initialized, the node driver will call into
  202. * the hugetlb module to [un]register attributes for hot-plugged nodes.
  203. */
  204. static node_registration_func_t __hugetlb_register_node;
  205. static node_registration_func_t __hugetlb_unregister_node;
  206. static inline bool hugetlb_register_node(struct node *node)
  207. {
  208. if (__hugetlb_register_node &&
  209. node_state(node->dev.id, N_MEMORY)) {
  210. __hugetlb_register_node(node);
  211. return true;
  212. }
  213. return false;
  214. }
  215. static inline void hugetlb_unregister_node(struct node *node)
  216. {
  217. if (__hugetlb_unregister_node)
  218. __hugetlb_unregister_node(node);
  219. }
  220. void register_hugetlbfs_with_node(node_registration_func_t doregister,
  221. node_registration_func_t unregister)
  222. {
  223. __hugetlb_register_node = doregister;
  224. __hugetlb_unregister_node = unregister;
  225. }
  226. #else
  227. static inline void hugetlb_register_node(struct node *node) {}
  228. static inline void hugetlb_unregister_node(struct node *node) {}
  229. #endif
  230. static void node_device_release(struct device *dev)
  231. {
  232. struct node *node = to_node(dev);
  233. #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HUGETLBFS)
  234. /*
  235. * We schedule the work only when a memory section is
  236. * onlined/offlined on this node. When we come here,
  237. * all the memory on this node has been offlined,
  238. * so we won't enqueue new work to this work.
  239. *
  240. * The work is using node->node_work, so we should
  241. * flush work before freeing the memory.
  242. */
  243. flush_work(&node->node_work);
  244. #endif
  245. kfree(node);
  246. }
  247. /*
  248. * register_node - Setup a sysfs device for a node.
  249. * @num - Node number to use when creating the device.
  250. *
  251. * Initialize and register the node device.
  252. */
  253. static int register_node(struct node *node, int num, struct node *parent)
  254. {
  255. int error;
  256. node->dev.id = num;
  257. node->dev.bus = &node_subsys;
  258. node->dev.release = node_device_release;
  259. error = device_register(&node->dev);
  260. if (!error){
  261. device_create_file(&node->dev, &dev_attr_cpumap);
  262. device_create_file(&node->dev, &dev_attr_cpulist);
  263. device_create_file(&node->dev, &dev_attr_meminfo);
  264. device_create_file(&node->dev, &dev_attr_numastat);
  265. device_create_file(&node->dev, &dev_attr_distance);
  266. device_create_file(&node->dev, &dev_attr_vmstat);
  267. scan_unevictable_register_node(node);
  268. hugetlb_register_node(node);
  269. compaction_register_node(node);
  270. }
  271. return error;
  272. }
  273. /**
  274. * unregister_node - unregister a node device
  275. * @node: node going away
  276. *
  277. * Unregisters a node device @node. All the devices on the node must be
  278. * unregistered before calling this function.
  279. */
  280. void unregister_node(struct node *node)
  281. {
  282. device_remove_file(&node->dev, &dev_attr_cpumap);
  283. device_remove_file(&node->dev, &dev_attr_cpulist);
  284. device_remove_file(&node->dev, &dev_attr_meminfo);
  285. device_remove_file(&node->dev, &dev_attr_numastat);
  286. device_remove_file(&node->dev, &dev_attr_distance);
  287. device_remove_file(&node->dev, &dev_attr_vmstat);
  288. scan_unevictable_unregister_node(node);
  289. hugetlb_unregister_node(node); /* no-op, if memoryless node */
  290. device_unregister(&node->dev);
  291. }
  292. struct node *node_devices[MAX_NUMNODES];
  293. /*
  294. * register cpu under node
  295. */
  296. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  297. {
  298. int ret;
  299. struct device *obj;
  300. if (!node_online(nid))
  301. return 0;
  302. obj = get_cpu_device(cpu);
  303. if (!obj)
  304. return 0;
  305. ret = sysfs_create_link(&node_devices[nid]->dev.kobj,
  306. &obj->kobj,
  307. kobject_name(&obj->kobj));
  308. if (ret)
  309. return ret;
  310. return sysfs_create_link(&obj->kobj,
  311. &node_devices[nid]->dev.kobj,
  312. kobject_name(&node_devices[nid]->dev.kobj));
  313. }
  314. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  315. {
  316. struct device *obj;
  317. if (!node_online(nid))
  318. return 0;
  319. obj = get_cpu_device(cpu);
  320. if (!obj)
  321. return 0;
  322. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  323. kobject_name(&obj->kobj));
  324. sysfs_remove_link(&obj->kobj,
  325. kobject_name(&node_devices[nid]->dev.kobj));
  326. return 0;
  327. }
  328. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  329. #define page_initialized(page) (page->lru.next)
  330. static int get_nid_for_pfn(unsigned long pfn)
  331. {
  332. struct page *page;
  333. if (!pfn_valid_within(pfn))
  334. return -1;
  335. page = pfn_to_page(pfn);
  336. if (!page_initialized(page))
  337. return -1;
  338. return pfn_to_nid(pfn);
  339. }
  340. /* register memory section under specified node if it spans that node */
  341. int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
  342. {
  343. int ret;
  344. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  345. if (!mem_blk)
  346. return -EFAULT;
  347. if (!node_online(nid))
  348. return 0;
  349. sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
  350. sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
  351. sect_end_pfn += PAGES_PER_SECTION - 1;
  352. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  353. int page_nid;
  354. page_nid = get_nid_for_pfn(pfn);
  355. if (page_nid < 0)
  356. continue;
  357. if (page_nid != nid)
  358. continue;
  359. ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
  360. &mem_blk->dev.kobj,
  361. kobject_name(&mem_blk->dev.kobj));
  362. if (ret)
  363. return ret;
  364. return sysfs_create_link_nowarn(&mem_blk->dev.kobj,
  365. &node_devices[nid]->dev.kobj,
  366. kobject_name(&node_devices[nid]->dev.kobj));
  367. }
  368. /* mem section does not span the specified node */
  369. return 0;
  370. }
  371. /* unregister memory section under all nodes that it spans */
  372. int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
  373. unsigned long phys_index)
  374. {
  375. NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
  376. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  377. if (!mem_blk) {
  378. NODEMASK_FREE(unlinked_nodes);
  379. return -EFAULT;
  380. }
  381. if (!unlinked_nodes)
  382. return -ENOMEM;
  383. nodes_clear(*unlinked_nodes);
  384. sect_start_pfn = section_nr_to_pfn(phys_index);
  385. sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
  386. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  387. int nid;
  388. nid = get_nid_for_pfn(pfn);
  389. if (nid < 0)
  390. continue;
  391. if (!node_online(nid))
  392. continue;
  393. if (node_test_and_set(nid, *unlinked_nodes))
  394. continue;
  395. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  396. kobject_name(&mem_blk->dev.kobj));
  397. sysfs_remove_link(&mem_blk->dev.kobj,
  398. kobject_name(&node_devices[nid]->dev.kobj));
  399. }
  400. NODEMASK_FREE(unlinked_nodes);
  401. return 0;
  402. }
  403. static int link_mem_sections(int nid)
  404. {
  405. unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
  406. unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
  407. unsigned long pfn;
  408. struct memory_block *mem_blk = NULL;
  409. int err = 0;
  410. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  411. unsigned long section_nr = pfn_to_section_nr(pfn);
  412. struct mem_section *mem_sect;
  413. int ret;
  414. if (!present_section_nr(section_nr))
  415. continue;
  416. mem_sect = __nr_to_section(section_nr);
  417. /* same memblock ? */
  418. if (mem_blk)
  419. if ((section_nr >= mem_blk->start_section_nr) &&
  420. (section_nr <= mem_blk->end_section_nr))
  421. continue;
  422. mem_blk = find_memory_block_hinted(mem_sect, mem_blk);
  423. ret = register_mem_sect_under_node(mem_blk, nid);
  424. if (!err)
  425. err = ret;
  426. /* discard ref obtained in find_memory_block() */
  427. }
  428. if (mem_blk)
  429. kobject_put(&mem_blk->dev.kobj);
  430. return err;
  431. }
  432. #ifdef CONFIG_HUGETLBFS
  433. /*
  434. * Handle per node hstate attribute [un]registration on transistions
  435. * to/from memoryless state.
  436. */
  437. static void node_hugetlb_work(struct work_struct *work)
  438. {
  439. struct node *node = container_of(work, struct node, node_work);
  440. /*
  441. * We only get here when a node transitions to/from memoryless state.
  442. * We can detect which transition occurred by examining whether the
  443. * node has memory now. hugetlb_register_node() already check this
  444. * so we try to register the attributes. If that fails, then the
  445. * node has transitioned to memoryless, try to unregister the
  446. * attributes.
  447. */
  448. if (!hugetlb_register_node(node))
  449. hugetlb_unregister_node(node);
  450. }
  451. static void init_node_hugetlb_work(int nid)
  452. {
  453. INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work);
  454. }
  455. static int node_memory_callback(struct notifier_block *self,
  456. unsigned long action, void *arg)
  457. {
  458. struct memory_notify *mnb = arg;
  459. int nid = mnb->status_change_nid;
  460. switch (action) {
  461. case MEM_ONLINE:
  462. case MEM_OFFLINE:
  463. /*
  464. * offload per node hstate [un]registration to a work thread
  465. * when transitioning to/from memoryless state.
  466. */
  467. if (nid != NUMA_NO_NODE)
  468. schedule_work(&node_devices[nid]->node_work);
  469. break;
  470. case MEM_GOING_ONLINE:
  471. case MEM_GOING_OFFLINE:
  472. case MEM_CANCEL_ONLINE:
  473. case MEM_CANCEL_OFFLINE:
  474. default:
  475. break;
  476. }
  477. return NOTIFY_OK;
  478. }
  479. #endif /* CONFIG_HUGETLBFS */
  480. #else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
  481. static int link_mem_sections(int nid) { return 0; }
  482. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  483. #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
  484. !defined(CONFIG_HUGETLBFS)
  485. static inline int node_memory_callback(struct notifier_block *self,
  486. unsigned long action, void *arg)
  487. {
  488. return NOTIFY_OK;
  489. }
  490. static void init_node_hugetlb_work(int nid) { }
  491. #endif
  492. int register_one_node(int nid)
  493. {
  494. int error = 0;
  495. int cpu;
  496. if (node_online(nid)) {
  497. int p_node = parent_node(nid);
  498. struct node *parent = NULL;
  499. if (p_node != nid)
  500. parent = node_devices[p_node];
  501. node_devices[nid] = kzalloc(sizeof(struct node), GFP_KERNEL);
  502. if (!node_devices[nid])
  503. return -ENOMEM;
  504. error = register_node(node_devices[nid], nid, parent);
  505. /* link cpu under this node */
  506. for_each_present_cpu(cpu) {
  507. if (cpu_to_node(cpu) == nid)
  508. register_cpu_under_node(cpu, nid);
  509. }
  510. /* link memory sections under this node */
  511. error = link_mem_sections(nid);
  512. /* initialize work queue for memory hot plug */
  513. init_node_hugetlb_work(nid);
  514. }
  515. return error;
  516. }
  517. void unregister_one_node(int nid)
  518. {
  519. unregister_node(node_devices[nid]);
  520. node_devices[nid] = NULL;
  521. }
  522. /*
  523. * node states attributes
  524. */
  525. static ssize_t print_nodes_state(enum node_states state, char *buf)
  526. {
  527. int n;
  528. n = nodelist_scnprintf(buf, PAGE_SIZE-2, node_states[state]);
  529. buf[n++] = '\n';
  530. buf[n] = '\0';
  531. return n;
  532. }
  533. struct node_attr {
  534. struct device_attribute attr;
  535. enum node_states state;
  536. };
  537. static ssize_t show_node_state(struct device *dev,
  538. struct device_attribute *attr, char *buf)
  539. {
  540. struct node_attr *na = container_of(attr, struct node_attr, attr);
  541. return print_nodes_state(na->state, buf);
  542. }
  543. #define _NODE_ATTR(name, state) \
  544. { __ATTR(name, 0444, show_node_state, NULL), state }
  545. static struct node_attr node_state_attr[] = {
  546. [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
  547. [N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
  548. [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
  549. #ifdef CONFIG_HIGHMEM
  550. [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
  551. #endif
  552. #ifdef CONFIG_MOVABLE_NODE
  553. [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
  554. #endif
  555. [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
  556. };
  557. static struct attribute *node_state_attrs[] = {
  558. &node_state_attr[N_POSSIBLE].attr.attr,
  559. &node_state_attr[N_ONLINE].attr.attr,
  560. &node_state_attr[N_NORMAL_MEMORY].attr.attr,
  561. #ifdef CONFIG_HIGHMEM
  562. &node_state_attr[N_HIGH_MEMORY].attr.attr,
  563. #endif
  564. #ifdef CONFIG_MOVABLE_NODE
  565. &node_state_attr[N_MEMORY].attr.attr,
  566. #endif
  567. &node_state_attr[N_CPU].attr.attr,
  568. NULL
  569. };
  570. static struct attribute_group memory_root_attr_group = {
  571. .attrs = node_state_attrs,
  572. };
  573. static const struct attribute_group *cpu_root_attr_groups[] = {
  574. &memory_root_attr_group,
  575. NULL,
  576. };
  577. #define NODE_CALLBACK_PRI 2 /* lower than SLAB */
  578. static int __init register_node_type(void)
  579. {
  580. int ret;
  581. BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
  582. BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
  583. ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
  584. if (!ret) {
  585. static struct notifier_block node_memory_callback_nb = {
  586. .notifier_call = node_memory_callback,
  587. .priority = NODE_CALLBACK_PRI,
  588. };
  589. register_hotmemory_notifier(&node_memory_callback_nb);
  590. }
  591. /*
  592. * Note: we're not going to unregister the node class if we fail
  593. * to register the node state class attribute files.
  594. */
  595. return ret;
  596. }
  597. postcore_initcall(register_node_type);