node.c 18 KB

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