node.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. hugetlb_register_node(node);
  285. compaction_register_node(node);
  286. }
  287. return error;
  288. }
  289. /**
  290. * unregister_node - unregister a node device
  291. * @node: node going away
  292. *
  293. * Unregisters a node device @node. All the devices on the node must be
  294. * unregistered before calling this function.
  295. */
  296. void unregister_node(struct node *node)
  297. {
  298. hugetlb_unregister_node(node); /* no-op, if memoryless node */
  299. device_unregister(&node->dev);
  300. }
  301. struct node *node_devices[MAX_NUMNODES];
  302. /*
  303. * register cpu under node
  304. */
  305. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  306. {
  307. int ret;
  308. struct device *obj;
  309. if (!node_online(nid))
  310. return 0;
  311. obj = get_cpu_device(cpu);
  312. if (!obj)
  313. return 0;
  314. ret = sysfs_create_link(&node_devices[nid]->dev.kobj,
  315. &obj->kobj,
  316. kobject_name(&obj->kobj));
  317. if (ret)
  318. return ret;
  319. return sysfs_create_link(&obj->kobj,
  320. &node_devices[nid]->dev.kobj,
  321. kobject_name(&node_devices[nid]->dev.kobj));
  322. }
  323. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  324. {
  325. struct device *obj;
  326. if (!node_online(nid))
  327. return 0;
  328. obj = get_cpu_device(cpu);
  329. if (!obj)
  330. return 0;
  331. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  332. kobject_name(&obj->kobj));
  333. sysfs_remove_link(&obj->kobj,
  334. kobject_name(&node_devices[nid]->dev.kobj));
  335. return 0;
  336. }
  337. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  338. static int __ref get_nid_for_pfn(unsigned long pfn)
  339. {
  340. if (!pfn_valid_within(pfn))
  341. return -1;
  342. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  343. if (system_state < SYSTEM_RUNNING)
  344. return early_pfn_to_nid(pfn);
  345. #endif
  346. return pfn_to_nid(pfn);
  347. }
  348. /* register memory section under specified node if it spans that node */
  349. int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
  350. {
  351. int ret;
  352. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  353. if (!mem_blk)
  354. return -EFAULT;
  355. if (!node_online(nid))
  356. return 0;
  357. sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
  358. sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
  359. sect_end_pfn += PAGES_PER_SECTION - 1;
  360. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  361. int page_nid;
  362. /*
  363. * memory block could have several absent sections from start.
  364. * skip pfn range from absent section
  365. */
  366. if (!pfn_present(pfn)) {
  367. pfn = round_down(pfn + PAGES_PER_SECTION,
  368. PAGES_PER_SECTION) - 1;
  369. continue;
  370. }
  371. page_nid = get_nid_for_pfn(pfn);
  372. if (page_nid < 0)
  373. continue;
  374. if (page_nid != nid)
  375. continue;
  376. ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
  377. &mem_blk->dev.kobj,
  378. kobject_name(&mem_blk->dev.kobj));
  379. if (ret)
  380. return ret;
  381. return sysfs_create_link_nowarn(&mem_blk->dev.kobj,
  382. &node_devices[nid]->dev.kobj,
  383. kobject_name(&node_devices[nid]->dev.kobj));
  384. }
  385. /* mem section does not span the specified node */
  386. return 0;
  387. }
  388. /* unregister memory section under all nodes that it spans */
  389. int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
  390. unsigned long phys_index)
  391. {
  392. NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
  393. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  394. if (!mem_blk) {
  395. NODEMASK_FREE(unlinked_nodes);
  396. return -EFAULT;
  397. }
  398. if (!unlinked_nodes)
  399. return -ENOMEM;
  400. nodes_clear(*unlinked_nodes);
  401. sect_start_pfn = section_nr_to_pfn(phys_index);
  402. sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
  403. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  404. int nid;
  405. nid = get_nid_for_pfn(pfn);
  406. if (nid < 0)
  407. continue;
  408. if (!node_online(nid))
  409. continue;
  410. if (node_test_and_set(nid, *unlinked_nodes))
  411. continue;
  412. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  413. kobject_name(&mem_blk->dev.kobj));
  414. sysfs_remove_link(&mem_blk->dev.kobj,
  415. kobject_name(&node_devices[nid]->dev.kobj));
  416. }
  417. NODEMASK_FREE(unlinked_nodes);
  418. return 0;
  419. }
  420. int link_mem_sections(int nid, unsigned long start_pfn, unsigned long nr_pages)
  421. {
  422. unsigned long end_pfn = start_pfn + nr_pages;
  423. unsigned long pfn;
  424. struct memory_block *mem_blk = NULL;
  425. int err = 0;
  426. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  427. unsigned long section_nr = pfn_to_section_nr(pfn);
  428. struct mem_section *mem_sect;
  429. int ret;
  430. if (!present_section_nr(section_nr))
  431. continue;
  432. mem_sect = __nr_to_section(section_nr);
  433. /* same memblock ? */
  434. if (mem_blk)
  435. if ((section_nr >= mem_blk->start_section_nr) &&
  436. (section_nr <= mem_blk->end_section_nr))
  437. continue;
  438. mem_blk = find_memory_block_hinted(mem_sect, mem_blk);
  439. ret = register_mem_sect_under_node(mem_blk, nid);
  440. if (!err)
  441. err = ret;
  442. /* discard ref obtained in find_memory_block() */
  443. }
  444. if (mem_blk)
  445. kobject_put(&mem_blk->dev.kobj);
  446. return err;
  447. }
  448. #ifdef CONFIG_HUGETLBFS
  449. /*
  450. * Handle per node hstate attribute [un]registration on transistions
  451. * to/from memoryless state.
  452. */
  453. static void node_hugetlb_work(struct work_struct *work)
  454. {
  455. struct node *node = container_of(work, struct node, node_work);
  456. /*
  457. * We only get here when a node transitions to/from memoryless state.
  458. * We can detect which transition occurred by examining whether the
  459. * node has memory now. hugetlb_register_node() already check this
  460. * so we try to register the attributes. If that fails, then the
  461. * node has transitioned to memoryless, try to unregister the
  462. * attributes.
  463. */
  464. if (!hugetlb_register_node(node))
  465. hugetlb_unregister_node(node);
  466. }
  467. static void init_node_hugetlb_work(int nid)
  468. {
  469. INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work);
  470. }
  471. static int node_memory_callback(struct notifier_block *self,
  472. unsigned long action, void *arg)
  473. {
  474. struct memory_notify *mnb = arg;
  475. int nid = mnb->status_change_nid;
  476. switch (action) {
  477. case MEM_ONLINE:
  478. case MEM_OFFLINE:
  479. /*
  480. * offload per node hstate [un]registration to a work thread
  481. * when transitioning to/from memoryless state.
  482. */
  483. if (nid != NUMA_NO_NODE)
  484. schedule_work(&node_devices[nid]->node_work);
  485. break;
  486. case MEM_GOING_ONLINE:
  487. case MEM_GOING_OFFLINE:
  488. case MEM_CANCEL_ONLINE:
  489. case MEM_CANCEL_OFFLINE:
  490. default:
  491. break;
  492. }
  493. return NOTIFY_OK;
  494. }
  495. #endif /* CONFIG_HUGETLBFS */
  496. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  497. #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
  498. !defined(CONFIG_HUGETLBFS)
  499. static inline int node_memory_callback(struct notifier_block *self,
  500. unsigned long action, void *arg)
  501. {
  502. return NOTIFY_OK;
  503. }
  504. static void init_node_hugetlb_work(int nid) { }
  505. #endif
  506. int __register_one_node(int nid)
  507. {
  508. int error;
  509. int cpu;
  510. node_devices[nid] = kzalloc(sizeof(struct node), GFP_KERNEL);
  511. if (!node_devices[nid])
  512. return -ENOMEM;
  513. error = register_node(node_devices[nid], nid);
  514. /* link cpu under this node */
  515. for_each_present_cpu(cpu) {
  516. if (cpu_to_node(cpu) == nid)
  517. register_cpu_under_node(cpu, nid);
  518. }
  519. /* initialize work queue for memory hot plug */
  520. init_node_hugetlb_work(nid);
  521. return error;
  522. }
  523. void unregister_one_node(int nid)
  524. {
  525. if (!node_devices[nid])
  526. return;
  527. unregister_node(node_devices[nid]);
  528. node_devices[nid] = NULL;
  529. }
  530. /*
  531. * node states attributes
  532. */
  533. static ssize_t print_nodes_state(enum node_states state, char *buf)
  534. {
  535. int n;
  536. n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  537. nodemask_pr_args(&node_states[state]));
  538. buf[n++] = '\n';
  539. buf[n] = '\0';
  540. return n;
  541. }
  542. struct node_attr {
  543. struct device_attribute attr;
  544. enum node_states state;
  545. };
  546. static ssize_t show_node_state(struct device *dev,
  547. struct device_attribute *attr, char *buf)
  548. {
  549. struct node_attr *na = container_of(attr, struct node_attr, attr);
  550. return print_nodes_state(na->state, buf);
  551. }
  552. #define _NODE_ATTR(name, state) \
  553. { __ATTR(name, 0444, show_node_state, NULL), state }
  554. static struct node_attr node_state_attr[] = {
  555. [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
  556. [N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
  557. [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
  558. #ifdef CONFIG_HIGHMEM
  559. [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
  560. #endif
  561. [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
  562. [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
  563. };
  564. static struct attribute *node_state_attrs[] = {
  565. &node_state_attr[N_POSSIBLE].attr.attr,
  566. &node_state_attr[N_ONLINE].attr.attr,
  567. &node_state_attr[N_NORMAL_MEMORY].attr.attr,
  568. #ifdef CONFIG_HIGHMEM
  569. &node_state_attr[N_HIGH_MEMORY].attr.attr,
  570. #endif
  571. &node_state_attr[N_MEMORY].attr.attr,
  572. &node_state_attr[N_CPU].attr.attr,
  573. NULL
  574. };
  575. static struct attribute_group memory_root_attr_group = {
  576. .attrs = node_state_attrs,
  577. };
  578. static const struct attribute_group *cpu_root_attr_groups[] = {
  579. &memory_root_attr_group,
  580. NULL,
  581. };
  582. #define NODE_CALLBACK_PRI 2 /* lower than SLAB */
  583. static int __init register_node_type(void)
  584. {
  585. int ret;
  586. BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
  587. BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
  588. ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
  589. if (!ret) {
  590. static struct notifier_block node_memory_callback_nb = {
  591. .notifier_call = node_memory_callback,
  592. .priority = NODE_CALLBACK_PRI,
  593. };
  594. register_hotmemory_notifier(&node_memory_callback_nb);
  595. }
  596. /*
  597. * Note: we're not going to unregister the node class if we fail
  598. * to register the node state class attribute files.
  599. */
  600. return ret;
  601. }
  602. postcore_initcall(register_node_type);