node.c 19 KB

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