node.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. /* Skip hidden vmstat items. */
  183. if (*vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
  184. NR_VM_NUMA_STAT_ITEMS] == '\0')
  185. continue;
  186. n += sprintf(buf+n, "%s %lu\n",
  187. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
  188. NR_VM_NUMA_STAT_ITEMS],
  189. node_page_state(pgdat, i));
  190. }
  191. return n;
  192. }
  193. static DEVICE_ATTR(vmstat, S_IRUGO, node_read_vmstat, NULL);
  194. static ssize_t node_read_distance(struct device *dev,
  195. struct device_attribute *attr, char *buf)
  196. {
  197. int nid = dev->id;
  198. int len = 0;
  199. int i;
  200. /*
  201. * buf is currently PAGE_SIZE in length and each node needs 4 chars
  202. * at the most (distance + space or newline).
  203. */
  204. BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
  205. for_each_online_node(i)
  206. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  207. len += sprintf(buf + len, "\n");
  208. return len;
  209. }
  210. static DEVICE_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  211. static struct attribute *node_dev_attrs[] = {
  212. &dev_attr_cpumap.attr,
  213. &dev_attr_cpulist.attr,
  214. &dev_attr_meminfo.attr,
  215. &dev_attr_numastat.attr,
  216. &dev_attr_distance.attr,
  217. &dev_attr_vmstat.attr,
  218. NULL
  219. };
  220. ATTRIBUTE_GROUPS(node_dev);
  221. #ifdef CONFIG_HUGETLBFS
  222. /*
  223. * hugetlbfs per node attributes registration interface:
  224. * When/if hugetlb[fs] subsystem initializes [sometime after this module],
  225. * it will register its per node attributes for all online nodes with
  226. * memory. It will also call register_hugetlbfs_with_node(), below, to
  227. * register its attribute registration functions with this node driver.
  228. * Once these hooks have been initialized, the node driver will call into
  229. * the hugetlb module to [un]register attributes for hot-plugged nodes.
  230. */
  231. static node_registration_func_t __hugetlb_register_node;
  232. static node_registration_func_t __hugetlb_unregister_node;
  233. static inline bool hugetlb_register_node(struct node *node)
  234. {
  235. if (__hugetlb_register_node &&
  236. node_state(node->dev.id, N_MEMORY)) {
  237. __hugetlb_register_node(node);
  238. return true;
  239. }
  240. return false;
  241. }
  242. static inline void hugetlb_unregister_node(struct node *node)
  243. {
  244. if (__hugetlb_unregister_node)
  245. __hugetlb_unregister_node(node);
  246. }
  247. void register_hugetlbfs_with_node(node_registration_func_t doregister,
  248. node_registration_func_t unregister)
  249. {
  250. __hugetlb_register_node = doregister;
  251. __hugetlb_unregister_node = unregister;
  252. }
  253. #else
  254. static inline void hugetlb_register_node(struct node *node) {}
  255. static inline void hugetlb_unregister_node(struct node *node) {}
  256. #endif
  257. static void node_device_release(struct device *dev)
  258. {
  259. struct node *node = to_node(dev);
  260. #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HUGETLBFS)
  261. /*
  262. * We schedule the work only when a memory section is
  263. * onlined/offlined on this node. When we come here,
  264. * all the memory on this node has been offlined,
  265. * so we won't enqueue new work to this work.
  266. *
  267. * The work is using node->node_work, so we should
  268. * flush work before freeing the memory.
  269. */
  270. flush_work(&node->node_work);
  271. #endif
  272. kfree(node);
  273. }
  274. /*
  275. * register_node - Setup a sysfs device for a node.
  276. * @num - Node number to use when creating the device.
  277. *
  278. * Initialize and register the node device.
  279. */
  280. static int register_node(struct node *node, int num)
  281. {
  282. int error;
  283. node->dev.id = num;
  284. node->dev.bus = &node_subsys;
  285. node->dev.release = node_device_release;
  286. node->dev.groups = node_dev_groups;
  287. error = device_register(&node->dev);
  288. if (error)
  289. put_device(&node->dev);
  290. else {
  291. hugetlb_register_node(node);
  292. compaction_register_node(node);
  293. }
  294. return error;
  295. }
  296. /**
  297. * unregister_node - unregister a node device
  298. * @node: node going away
  299. *
  300. * Unregisters a node device @node. All the devices on the node must be
  301. * unregistered before calling this function.
  302. */
  303. void unregister_node(struct node *node)
  304. {
  305. hugetlb_unregister_node(node); /* no-op, if memoryless node */
  306. device_unregister(&node->dev);
  307. }
  308. struct node *node_devices[MAX_NUMNODES];
  309. /*
  310. * register cpu under node
  311. */
  312. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  313. {
  314. int ret;
  315. struct device *obj;
  316. if (!node_online(nid))
  317. return 0;
  318. obj = get_cpu_device(cpu);
  319. if (!obj)
  320. return 0;
  321. ret = sysfs_create_link(&node_devices[nid]->dev.kobj,
  322. &obj->kobj,
  323. kobject_name(&obj->kobj));
  324. if (ret)
  325. return ret;
  326. return sysfs_create_link(&obj->kobj,
  327. &node_devices[nid]->dev.kobj,
  328. kobject_name(&node_devices[nid]->dev.kobj));
  329. }
  330. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  331. {
  332. struct device *obj;
  333. if (!node_online(nid))
  334. return 0;
  335. obj = get_cpu_device(cpu);
  336. if (!obj)
  337. return 0;
  338. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  339. kobject_name(&obj->kobj));
  340. sysfs_remove_link(&obj->kobj,
  341. kobject_name(&node_devices[nid]->dev.kobj));
  342. return 0;
  343. }
  344. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  345. static int __ref get_nid_for_pfn(unsigned long pfn)
  346. {
  347. if (!pfn_valid_within(pfn))
  348. return -1;
  349. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  350. if (system_state < SYSTEM_RUNNING)
  351. return early_pfn_to_nid(pfn);
  352. #endif
  353. return pfn_to_nid(pfn);
  354. }
  355. static int do_register_memory_block_under_node(int nid,
  356. struct memory_block *mem_blk)
  357. {
  358. int ret;
  359. /*
  360. * If this memory block spans multiple nodes, we only indicate
  361. * the last processed node.
  362. */
  363. mem_blk->nid = nid;
  364. ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
  365. &mem_blk->dev.kobj,
  366. kobject_name(&mem_blk->dev.kobj));
  367. if (ret)
  368. return ret;
  369. return sysfs_create_link_nowarn(&mem_blk->dev.kobj,
  370. &node_devices[nid]->dev.kobj,
  371. kobject_name(&node_devices[nid]->dev.kobj));
  372. }
  373. /* register memory section under specified node if it spans that node */
  374. int register_mem_block_under_node_early(struct memory_block *mem_blk, void *arg)
  375. {
  376. int nid = *(int *)arg;
  377. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  378. sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
  379. sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
  380. sect_end_pfn += PAGES_PER_SECTION - 1;
  381. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  382. int page_nid;
  383. /*
  384. * memory block could have several absent sections from start.
  385. * skip pfn range from absent section
  386. */
  387. if (!pfn_present(pfn)) {
  388. pfn = round_down(pfn + PAGES_PER_SECTION,
  389. PAGES_PER_SECTION) - 1;
  390. continue;
  391. }
  392. /*
  393. * We need to check if page belongs to nid only at the boot
  394. * case because node's ranges can be interleaved.
  395. */
  396. page_nid = get_nid_for_pfn(pfn);
  397. if (page_nid < 0)
  398. continue;
  399. if (page_nid != nid)
  400. continue;
  401. return do_register_memory_block_under_node(nid, mem_blk);
  402. }
  403. /* mem section does not span the specified node */
  404. return 0;
  405. }
  406. /*
  407. * During hotplug we know that all pages in the memory block belong to the same
  408. * node.
  409. */
  410. static int register_mem_block_under_node_hotplug(struct memory_block *mem_blk,
  411. void *arg)
  412. {
  413. int nid = *(int *)arg;
  414. return do_register_memory_block_under_node(nid, mem_blk);
  415. }
  416. /*
  417. * Unregister a memory block device under the node it spans. Memory blocks
  418. * with multiple nodes cannot be offlined and therefore also never be removed.
  419. */
  420. void unregister_memory_block_under_nodes(struct memory_block *mem_blk)
  421. {
  422. if (mem_blk->nid == NUMA_NO_NODE)
  423. return;
  424. sysfs_remove_link(&node_devices[mem_blk->nid]->dev.kobj,
  425. kobject_name(&mem_blk->dev.kobj));
  426. sysfs_remove_link(&mem_blk->dev.kobj,
  427. kobject_name(&node_devices[mem_blk->nid]->dev.kobj));
  428. }
  429. int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn,
  430. enum meminit_context context)
  431. {
  432. walk_memory_blocks_func_t func;
  433. if (context == MEMINIT_HOTPLUG)
  434. func = register_mem_block_under_node_hotplug;
  435. else
  436. func = register_mem_block_under_node_early;
  437. return walk_memory_range(start_pfn, end_pfn, (void *)&nid, func);
  438. }
  439. #ifdef CONFIG_HUGETLBFS
  440. /*
  441. * Handle per node hstate attribute [un]registration on transistions
  442. * to/from memoryless state.
  443. */
  444. static void node_hugetlb_work(struct work_struct *work)
  445. {
  446. struct node *node = container_of(work, struct node, node_work);
  447. /*
  448. * We only get here when a node transitions to/from memoryless state.
  449. * We can detect which transition occurred by examining whether the
  450. * node has memory now. hugetlb_register_node() already check this
  451. * so we try to register the attributes. If that fails, then the
  452. * node has transitioned to memoryless, try to unregister the
  453. * attributes.
  454. */
  455. if (!hugetlb_register_node(node))
  456. hugetlb_unregister_node(node);
  457. }
  458. static void init_node_hugetlb_work(int nid)
  459. {
  460. INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work);
  461. }
  462. static int node_memory_callback(struct notifier_block *self,
  463. unsigned long action, void *arg)
  464. {
  465. struct memory_notify *mnb = arg;
  466. int nid = mnb->status_change_nid;
  467. switch (action) {
  468. case MEM_ONLINE:
  469. case MEM_OFFLINE:
  470. /*
  471. * offload per node hstate [un]registration to a work thread
  472. * when transitioning to/from memoryless state.
  473. */
  474. if (nid != NUMA_NO_NODE)
  475. schedule_work(&node_devices[nid]->node_work);
  476. break;
  477. case MEM_GOING_ONLINE:
  478. case MEM_GOING_OFFLINE:
  479. case MEM_CANCEL_ONLINE:
  480. case MEM_CANCEL_OFFLINE:
  481. default:
  482. break;
  483. }
  484. return NOTIFY_OK;
  485. }
  486. #endif /* CONFIG_HUGETLBFS */
  487. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  488. #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
  489. !defined(CONFIG_HUGETLBFS)
  490. static inline int node_memory_callback(struct notifier_block *self,
  491. unsigned long action, void *arg)
  492. {
  493. return NOTIFY_OK;
  494. }
  495. static void init_node_hugetlb_work(int nid) { }
  496. #endif
  497. int __register_one_node(int nid)
  498. {
  499. int error;
  500. int cpu;
  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);
  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. /* initialize work queue for memory hot plug */
  511. init_node_hugetlb_work(nid);
  512. return error;
  513. }
  514. void unregister_one_node(int nid)
  515. {
  516. if (!node_devices[nid])
  517. return;
  518. unregister_node(node_devices[nid]);
  519. node_devices[nid] = NULL;
  520. }
  521. /*
  522. * node states attributes
  523. */
  524. static ssize_t print_nodes_state(enum node_states state, char *buf)
  525. {
  526. int n;
  527. n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  528. nodemask_pr_args(&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. [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
  553. [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
  554. };
  555. static struct attribute *node_state_attrs[] = {
  556. &node_state_attr[N_POSSIBLE].attr.attr,
  557. &node_state_attr[N_ONLINE].attr.attr,
  558. &node_state_attr[N_NORMAL_MEMORY].attr.attr,
  559. #ifdef CONFIG_HIGHMEM
  560. &node_state_attr[N_HIGH_MEMORY].attr.attr,
  561. #endif
  562. &node_state_attr[N_MEMORY].attr.attr,
  563. &node_state_attr[N_CPU].attr.attr,
  564. NULL
  565. };
  566. static struct attribute_group memory_root_attr_group = {
  567. .attrs = node_state_attrs,
  568. };
  569. static const struct attribute_group *cpu_root_attr_groups[] = {
  570. &memory_root_attr_group,
  571. NULL,
  572. };
  573. #define NODE_CALLBACK_PRI 2 /* lower than SLAB */
  574. static int __init register_node_type(void)
  575. {
  576. int ret;
  577. BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
  578. BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
  579. ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
  580. if (!ret) {
  581. static struct notifier_block node_memory_callback_nb = {
  582. .notifier_call = node_memory_callback,
  583. .priority = NODE_CALLBACK_PRI,
  584. };
  585. register_hotmemory_notifier(&node_memory_callback_nb);
  586. }
  587. /*
  588. * Note: we're not going to unregister the node class if we fail
  589. * to register the node state class attribute files.
  590. */
  591. return ret;
  592. }
  593. postcore_initcall(register_node_type);