node.c 19 KB

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