node.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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, int nid,
  352. bool check_nid)
  353. {
  354. int ret;
  355. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  356. if (!mem_blk)
  357. return -EFAULT;
  358. mem_blk->nid = nid;
  359. if (!node_online(nid))
  360. return 0;
  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 (check_nid) {
  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 nr_pages,
  432. bool check_nid)
  433. {
  434. unsigned long end_pfn = start_pfn + nr_pages;
  435. unsigned long pfn;
  436. struct memory_block *mem_blk = NULL;
  437. int err = 0;
  438. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  439. unsigned long section_nr = pfn_to_section_nr(pfn);
  440. struct mem_section *mem_sect;
  441. int ret;
  442. if (!present_section_nr(section_nr))
  443. continue;
  444. mem_sect = __nr_to_section(section_nr);
  445. /* same memblock ? */
  446. if (mem_blk)
  447. if ((section_nr >= mem_blk->start_section_nr) &&
  448. (section_nr <= mem_blk->end_section_nr))
  449. continue;
  450. mem_blk = find_memory_block_hinted(mem_sect, mem_blk);
  451. ret = register_mem_sect_under_node(mem_blk, nid, check_nid);
  452. if (!err)
  453. err = ret;
  454. /* discard ref obtained in find_memory_block() */
  455. }
  456. if (mem_blk)
  457. kobject_put(&mem_blk->dev.kobj);
  458. return err;
  459. }
  460. #ifdef CONFIG_HUGETLBFS
  461. /*
  462. * Handle per node hstate attribute [un]registration on transistions
  463. * to/from memoryless state.
  464. */
  465. static void node_hugetlb_work(struct work_struct *work)
  466. {
  467. struct node *node = container_of(work, struct node, node_work);
  468. /*
  469. * We only get here when a node transitions to/from memoryless state.
  470. * We can detect which transition occurred by examining whether the
  471. * node has memory now. hugetlb_register_node() already check this
  472. * so we try to register the attributes. If that fails, then the
  473. * node has transitioned to memoryless, try to unregister the
  474. * attributes.
  475. */
  476. if (!hugetlb_register_node(node))
  477. hugetlb_unregister_node(node);
  478. }
  479. static void init_node_hugetlb_work(int nid)
  480. {
  481. INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work);
  482. }
  483. static int node_memory_callback(struct notifier_block *self,
  484. unsigned long action, void *arg)
  485. {
  486. struct memory_notify *mnb = arg;
  487. int nid = mnb->status_change_nid;
  488. switch (action) {
  489. case MEM_ONLINE:
  490. case MEM_OFFLINE:
  491. /*
  492. * offload per node hstate [un]registration to a work thread
  493. * when transitioning to/from memoryless state.
  494. */
  495. if (nid != NUMA_NO_NODE)
  496. schedule_work(&node_devices[nid]->node_work);
  497. break;
  498. case MEM_GOING_ONLINE:
  499. case MEM_GOING_OFFLINE:
  500. case MEM_CANCEL_ONLINE:
  501. case MEM_CANCEL_OFFLINE:
  502. default:
  503. break;
  504. }
  505. return NOTIFY_OK;
  506. }
  507. #endif /* CONFIG_HUGETLBFS */
  508. #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
  509. #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
  510. !defined(CONFIG_HUGETLBFS)
  511. static inline int node_memory_callback(struct notifier_block *self,
  512. unsigned long action, void *arg)
  513. {
  514. return NOTIFY_OK;
  515. }
  516. static void init_node_hugetlb_work(int nid) { }
  517. #endif
  518. int __register_one_node(int nid)
  519. {
  520. int error;
  521. int cpu;
  522. node_devices[nid] = kzalloc(sizeof(struct node), GFP_KERNEL);
  523. if (!node_devices[nid])
  524. return -ENOMEM;
  525. error = register_node(node_devices[nid], nid);
  526. /* link cpu under this node */
  527. for_each_present_cpu(cpu) {
  528. if (cpu_to_node(cpu) == nid)
  529. register_cpu_under_node(cpu, nid);
  530. }
  531. /* initialize work queue for memory hot plug */
  532. init_node_hugetlb_work(nid);
  533. return error;
  534. }
  535. void unregister_one_node(int nid)
  536. {
  537. if (!node_devices[nid])
  538. return;
  539. unregister_node(node_devices[nid]);
  540. node_devices[nid] = NULL;
  541. }
  542. /*
  543. * node states attributes
  544. */
  545. static ssize_t print_nodes_state(enum node_states state, char *buf)
  546. {
  547. int n;
  548. n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  549. nodemask_pr_args(&node_states[state]));
  550. buf[n++] = '\n';
  551. buf[n] = '\0';
  552. return n;
  553. }
  554. struct node_attr {
  555. struct device_attribute attr;
  556. enum node_states state;
  557. };
  558. static ssize_t show_node_state(struct device *dev,
  559. struct device_attribute *attr, char *buf)
  560. {
  561. struct node_attr *na = container_of(attr, struct node_attr, attr);
  562. return print_nodes_state(na->state, buf);
  563. }
  564. #define _NODE_ATTR(name, state) \
  565. { __ATTR(name, 0444, show_node_state, NULL), state }
  566. static struct node_attr node_state_attr[] = {
  567. [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
  568. [N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
  569. [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
  570. #ifdef CONFIG_HIGHMEM
  571. [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
  572. #endif
  573. [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
  574. [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
  575. };
  576. static struct attribute *node_state_attrs[] = {
  577. &node_state_attr[N_POSSIBLE].attr.attr,
  578. &node_state_attr[N_ONLINE].attr.attr,
  579. &node_state_attr[N_NORMAL_MEMORY].attr.attr,
  580. #ifdef CONFIG_HIGHMEM
  581. &node_state_attr[N_HIGH_MEMORY].attr.attr,
  582. #endif
  583. &node_state_attr[N_MEMORY].attr.attr,
  584. &node_state_attr[N_CPU].attr.attr,
  585. NULL
  586. };
  587. static struct attribute_group memory_root_attr_group = {
  588. .attrs = node_state_attrs,
  589. };
  590. static const struct attribute_group *cpu_root_attr_groups[] = {
  591. &memory_root_attr_group,
  592. NULL,
  593. };
  594. #define NODE_CALLBACK_PRI 2 /* lower than SLAB */
  595. static int __init register_node_type(void)
  596. {
  597. int ret;
  598. BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
  599. BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
  600. ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
  601. if (!ret) {
  602. static struct notifier_block node_memory_callback_nb = {
  603. .notifier_call = node_memory_callback,
  604. .priority = NODE_CALLBACK_PRI,
  605. };
  606. register_hotmemory_notifier(&node_memory_callback_nb);
  607. }
  608. /*
  609. * Note: we're not going to unregister the node class if we fail
  610. * to register the node state class attribute files.
  611. */
  612. return ret;
  613. }
  614. postcore_initcall(register_node_type);