node.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. unsigned long sreclaimable, sunreclaimable;
  60. si_meminfo_node(&i, nid);
  61. sreclaimable = node_page_state(pgdat, NR_SLAB_RECLAIMABLE);
  62. sunreclaimable = node_page_state(pgdat, NR_SLAB_UNRECLAIMABLE);
  63. n = sprintf(buf,
  64. "Node %d MemTotal: %8lu kB\n"
  65. "Node %d MemFree: %8lu kB\n"
  66. "Node %d MemUsed: %8lu kB\n"
  67. "Node %d Active: %8lu kB\n"
  68. "Node %d Inactive: %8lu kB\n"
  69. "Node %d Active(anon): %8lu kB\n"
  70. "Node %d Inactive(anon): %8lu kB\n"
  71. "Node %d Active(file): %8lu kB\n"
  72. "Node %d Inactive(file): %8lu kB\n"
  73. "Node %d Unevictable: %8lu kB\n"
  74. "Node %d Mlocked: %8lu kB\n",
  75. nid, K(i.totalram),
  76. nid, K(i.freeram),
  77. nid, K(i.totalram - i.freeram),
  78. nid, K(node_page_state(pgdat, NR_ACTIVE_ANON) +
  79. node_page_state(pgdat, NR_ACTIVE_FILE)),
  80. nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) +
  81. node_page_state(pgdat, NR_INACTIVE_FILE)),
  82. nid, K(node_page_state(pgdat, NR_ACTIVE_ANON)),
  83. nid, K(node_page_state(pgdat, NR_INACTIVE_ANON)),
  84. nid, K(node_page_state(pgdat, NR_ACTIVE_FILE)),
  85. nid, K(node_page_state(pgdat, NR_INACTIVE_FILE)),
  86. nid, K(node_page_state(pgdat, NR_UNEVICTABLE)),
  87. nid, K(sum_zone_node_page_state(nid, NR_MLOCK)));
  88. #ifdef CONFIG_HIGHMEM
  89. n += sprintf(buf + n,
  90. "Node %d HighTotal: %8lu kB\n"
  91. "Node %d HighFree: %8lu kB\n"
  92. "Node %d LowTotal: %8lu kB\n"
  93. "Node %d LowFree: %8lu kB\n",
  94. nid, K(i.totalhigh),
  95. nid, K(i.freehigh),
  96. nid, K(i.totalram - i.totalhigh),
  97. nid, K(i.freeram - i.freehigh));
  98. #endif
  99. n += sprintf(buf + n,
  100. "Node %d Dirty: %8lu kB\n"
  101. "Node %d Writeback: %8lu kB\n"
  102. "Node %d FilePages: %8lu kB\n"
  103. "Node %d Mapped: %8lu kB\n"
  104. "Node %d AnonPages: %8lu kB\n"
  105. "Node %d Shmem: %8lu kB\n"
  106. "Node %d KernelStack: %8lu kB\n"
  107. "Node %d PageTables: %8lu kB\n"
  108. "Node %d NFS_Unstable: %8lu kB\n"
  109. "Node %d Bounce: %8lu kB\n"
  110. "Node %d WritebackTmp: %8lu kB\n"
  111. "Node %d KReclaimable: %8lu kB\n"
  112. "Node %d Slab: %8lu kB\n"
  113. "Node %d SReclaimable: %8lu kB\n"
  114. "Node %d SUnreclaim: %8lu kB\n"
  115. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  116. "Node %d AnonHugePages: %8lu kB\n"
  117. "Node %d ShmemHugePages: %8lu kB\n"
  118. "Node %d ShmemPmdMapped: %8lu kB\n"
  119. #endif
  120. ,
  121. nid, K(node_page_state(pgdat, NR_FILE_DIRTY)),
  122. nid, K(node_page_state(pgdat, NR_WRITEBACK)),
  123. nid, K(node_page_state(pgdat, NR_FILE_PAGES)),
  124. nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
  125. nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
  126. nid, K(i.sharedram),
  127. nid, sum_zone_node_page_state(nid, NR_KERNEL_STACK_KB),
  128. nid, K(sum_zone_node_page_state(nid, NR_PAGETABLE)),
  129. nid, K(node_page_state(pgdat, NR_UNSTABLE_NFS)),
  130. nid, K(sum_zone_node_page_state(nid, NR_BOUNCE)),
  131. nid, K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
  132. nid, K(sreclaimable +
  133. node_page_state(pgdat, NR_KERNEL_MISC_RECLAIMABLE)),
  134. nid, K(sreclaimable + sunreclaimable),
  135. nid, K(sreclaimable),
  136. nid, K(sunreclaimable)
  137. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  138. ,
  139. nid, K(node_page_state(pgdat, NR_ANON_THPS) *
  140. HPAGE_PMD_NR),
  141. nid, K(node_page_state(pgdat, NR_SHMEM_THPS) *
  142. HPAGE_PMD_NR),
  143. nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED) *
  144. HPAGE_PMD_NR)
  145. #endif
  146. );
  147. n += hugetlb_report_node_meminfo(nid, buf + n);
  148. return n;
  149. }
  150. #undef K
  151. static DEVICE_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
  152. static ssize_t node_read_numastat(struct device *dev,
  153. struct device_attribute *attr, char *buf)
  154. {
  155. return sprintf(buf,
  156. "numa_hit %lu\n"
  157. "numa_miss %lu\n"
  158. "numa_foreign %lu\n"
  159. "interleave_hit %lu\n"
  160. "local_node %lu\n"
  161. "other_node %lu\n",
  162. sum_zone_numa_state(dev->id, NUMA_HIT),
  163. sum_zone_numa_state(dev->id, NUMA_MISS),
  164. sum_zone_numa_state(dev->id, NUMA_FOREIGN),
  165. sum_zone_numa_state(dev->id, NUMA_INTERLEAVE_HIT),
  166. sum_zone_numa_state(dev->id, NUMA_LOCAL),
  167. sum_zone_numa_state(dev->id, NUMA_OTHER));
  168. }
  169. static DEVICE_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
  170. static ssize_t node_read_vmstat(struct device *dev,
  171. struct device_attribute *attr, char *buf)
  172. {
  173. int nid = dev->id;
  174. struct pglist_data *pgdat = NODE_DATA(nid);
  175. int i;
  176. int n = 0;
  177. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  178. n += sprintf(buf+n, "%s %lu\n", vmstat_text[i],
  179. sum_zone_node_page_state(nid, i));
  180. #ifdef CONFIG_NUMA
  181. for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
  182. n += sprintf(buf+n, "%s %lu\n",
  183. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
  184. sum_zone_numa_state(nid, i));
  185. #endif
  186. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  187. n += sprintf(buf+n, "%s %lu\n",
  188. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS +
  189. NR_VM_NUMA_STAT_ITEMS],
  190. node_page_state(pgdat, i));
  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. /* register memory section under specified node if it spans that node */
  356. int register_mem_sect_under_node(struct memory_block *mem_blk, void *arg)
  357. {
  358. int ret, nid = *(int *)arg;
  359. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  360. mem_blk->nid = nid;
  361. sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
  362. sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
  363. sect_end_pfn += PAGES_PER_SECTION - 1;
  364. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  365. int page_nid;
  366. /*
  367. * memory block could have several absent sections from start.
  368. * skip pfn range from absent section
  369. */
  370. if (!pfn_present(pfn)) {
  371. pfn = round_down(pfn + PAGES_PER_SECTION,
  372. PAGES_PER_SECTION) - 1;
  373. continue;
  374. }
  375. /*
  376. * We need to check if page belongs to nid only for the boot
  377. * case, during hotplug we know that all pages in the memory
  378. * block belong to the same node.
  379. */
  380. if (system_state == SYSTEM_BOOTING) {
  381. page_nid = get_nid_for_pfn(pfn);
  382. if (page_nid < 0)
  383. continue;
  384. if (page_nid != nid)
  385. continue;
  386. }
  387. ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
  388. &mem_blk->dev.kobj,
  389. kobject_name(&mem_blk->dev.kobj));
  390. if (ret)
  391. return ret;
  392. return sysfs_create_link_nowarn(&mem_blk->dev.kobj,
  393. &node_devices[nid]->dev.kobj,
  394. kobject_name(&node_devices[nid]->dev.kobj));
  395. }
  396. /* mem section does not span the specified node */
  397. return 0;
  398. }
  399. /* unregister memory section under all nodes that it spans */
  400. int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
  401. unsigned long phys_index)
  402. {
  403. NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
  404. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  405. if (!mem_blk) {
  406. NODEMASK_FREE(unlinked_nodes);
  407. return -EFAULT;
  408. }
  409. if (!unlinked_nodes)
  410. return -ENOMEM;
  411. nodes_clear(*unlinked_nodes);
  412. sect_start_pfn = section_nr_to_pfn(phys_index);
  413. sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
  414. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  415. int nid;
  416. nid = get_nid_for_pfn(pfn);
  417. if (nid < 0)
  418. continue;
  419. if (!node_online(nid))
  420. continue;
  421. if (node_test_and_set(nid, *unlinked_nodes))
  422. continue;
  423. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  424. kobject_name(&mem_blk->dev.kobj));
  425. sysfs_remove_link(&mem_blk->dev.kobj,
  426. kobject_name(&node_devices[nid]->dev.kobj));
  427. }
  428. NODEMASK_FREE(unlinked_nodes);
  429. return 0;
  430. }
  431. int link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn)
  432. {
  433. return walk_memory_range(start_pfn, end_pfn, (void *)&nid,
  434. register_mem_sect_under_node);
  435. }
  436. #ifdef CONFIG_HUGETLBFS
  437. /*
  438. * Handle per node hstate attribute [un]registration on transistions
  439. * to/from memoryless state.
  440. */
  441. static void node_hugetlb_work(struct work_struct *work)
  442. {
  443. struct node *node = container_of(work, struct node, node_work);
  444. /*
  445. * We only get here when a node transitions to/from memoryless state.
  446. * We can detect which transition occurred by examining whether the
  447. * node has memory now. hugetlb_register_node() already check this
  448. * so we try to register the attributes. If that fails, then the
  449. * node has transitioned to memoryless, try to unregister the
  450. * attributes.
  451. */
  452. if (!hugetlb_register_node(node))
  453. hugetlb_unregister_node(node);
  454. }
  455. static void init_node_hugetlb_work(int nid)
  456. {
  457. INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work);
  458. }
  459. static int node_memory_callback(struct notifier_block *self,
  460. unsigned long action, void *arg)
  461. {
  462. struct memory_notify *mnb = arg;
  463. int nid = mnb->status_change_nid;
  464. switch (action) {
  465. case MEM_ONLINE:
  466. case MEM_OFFLINE:
  467. /*
  468. * offload per node hstate [un]registration to a work thread
  469. * when transitioning to/from memoryless state.
  470. */
  471. if (nid != NUMA_NO_NODE)
  472. schedule_work(&node_devices[nid]->node_work);
  473. break;
  474. case MEM_GOING_ONLINE:
  475. case MEM_GOING_OFFLINE:
  476. case MEM_CANCEL_ONLINE:
  477. case MEM_CANCEL_OFFLINE:
  478. default:
  479. break;
  480. }
  481. return NOTIFY_OK;
  482. }
  483. #endif /* CONFIG_HUGETLBFS */
  484. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  485. #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
  486. !defined(CONFIG_HUGETLBFS)
  487. static inline int node_memory_callback(struct notifier_block *self,
  488. unsigned long action, void *arg)
  489. {
  490. return NOTIFY_OK;
  491. }
  492. static void init_node_hugetlb_work(int nid) { }
  493. #endif
  494. int __register_one_node(int nid)
  495. {
  496. int error;
  497. int cpu;
  498. node_devices[nid] = kzalloc(sizeof(struct node), GFP_KERNEL);
  499. if (!node_devices[nid])
  500. return -ENOMEM;
  501. error = register_node(node_devices[nid], nid);
  502. /* link cpu under this node */
  503. for_each_present_cpu(cpu) {
  504. if (cpu_to_node(cpu) == nid)
  505. register_cpu_under_node(cpu, nid);
  506. }
  507. /* initialize work queue for memory hot plug */
  508. init_node_hugetlb_work(nid);
  509. return error;
  510. }
  511. void unregister_one_node(int nid)
  512. {
  513. if (!node_devices[nid])
  514. return;
  515. unregister_node(node_devices[nid]);
  516. node_devices[nid] = NULL;
  517. }
  518. /*
  519. * node states attributes
  520. */
  521. static ssize_t print_nodes_state(enum node_states state, char *buf)
  522. {
  523. int n;
  524. n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  525. nodemask_pr_args(&node_states[state]));
  526. buf[n++] = '\n';
  527. buf[n] = '\0';
  528. return n;
  529. }
  530. struct node_attr {
  531. struct device_attribute attr;
  532. enum node_states state;
  533. };
  534. static ssize_t show_node_state(struct device *dev,
  535. struct device_attribute *attr, char *buf)
  536. {
  537. struct node_attr *na = container_of(attr, struct node_attr, attr);
  538. return print_nodes_state(na->state, buf);
  539. }
  540. #define _NODE_ATTR(name, state) \
  541. { __ATTR(name, 0444, show_node_state, NULL), state }
  542. static struct node_attr node_state_attr[] = {
  543. [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
  544. [N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
  545. [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
  546. #ifdef CONFIG_HIGHMEM
  547. [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
  548. #endif
  549. [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
  550. [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
  551. };
  552. static struct attribute *node_state_attrs[] = {
  553. &node_state_attr[N_POSSIBLE].attr.attr,
  554. &node_state_attr[N_ONLINE].attr.attr,
  555. &node_state_attr[N_NORMAL_MEMORY].attr.attr,
  556. #ifdef CONFIG_HIGHMEM
  557. &node_state_attr[N_HIGH_MEMORY].attr.attr,
  558. #endif
  559. &node_state_attr[N_MEMORY].attr.attr,
  560. &node_state_attr[N_CPU].attr.attr,
  561. NULL
  562. };
  563. static struct attribute_group memory_root_attr_group = {
  564. .attrs = node_state_attrs,
  565. };
  566. static const struct attribute_group *cpu_root_attr_groups[] = {
  567. &memory_root_attr_group,
  568. NULL,
  569. };
  570. #define NODE_CALLBACK_PRI 2 /* lower than SLAB */
  571. static int __init register_node_type(void)
  572. {
  573. int ret;
  574. BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
  575. BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
  576. ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
  577. if (!ret) {
  578. static struct notifier_block node_memory_callback_nb = {
  579. .notifier_call = node_memory_callback,
  580. .priority = NODE_CALLBACK_PRI,
  581. };
  582. register_hotmemory_notifier(&node_memory_callback_nb);
  583. }
  584. /*
  585. * Note: we're not going to unregister the node class if we fail
  586. * to register the node state class attribute files.
  587. */
  588. return ret;
  589. }
  590. postcore_initcall(register_node_type);