node.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. #ifdef CONFIG_SHADOW_CALL_STACK
  98. "Node %d ShadowCallStack:%8lu kB\n"
  99. #endif
  100. "Node %d PageTables: %8lu kB\n"
  101. "Node %d NFS_Unstable: %8lu kB\n"
  102. "Node %d Bounce: %8lu kB\n"
  103. "Node %d WritebackTmp: %8lu kB\n"
  104. "Node %d Slab: %8lu kB\n"
  105. "Node %d SReclaimable: %8lu kB\n"
  106. "Node %d SUnreclaim: %8lu kB\n"
  107. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  108. "Node %d AnonHugePages: %8lu kB\n"
  109. "Node %d ShmemHugePages: %8lu kB\n"
  110. "Node %d ShmemPmdMapped: %8lu kB\n"
  111. #endif
  112. ,
  113. nid, K(node_page_state(pgdat, NR_FILE_DIRTY)),
  114. nid, K(node_page_state(pgdat, NR_WRITEBACK)),
  115. nid, K(node_page_state(pgdat, NR_FILE_PAGES)),
  116. nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
  117. nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
  118. nid, K(i.sharedram),
  119. nid, sum_zone_node_page_state(nid, NR_KERNEL_STACK_KB),
  120. #ifdef CONFIG_SHADOW_CALL_STACK
  121. nid, sum_zone_node_page_state(nid, NR_KERNEL_SCS_BYTES) / 1024,
  122. #endif
  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(sum_zone_node_page_state(nid, NR_SLAB_RECLAIMABLE) +
  128. sum_zone_node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
  129. nid, K(sum_zone_node_page_state(nid, NR_SLAB_RECLAIMABLE)),
  130. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  131. nid, K(sum_zone_node_page_state(nid, 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(sum_zone_node_page_state(nid, 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_node_page_state(dev->id, NUMA_HIT),
  157. sum_zone_node_page_state(dev->id, NUMA_MISS),
  158. sum_zone_node_page_state(dev->id, NUMA_FOREIGN),
  159. sum_zone_node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
  160. sum_zone_node_page_state(dev->id, NUMA_LOCAL),
  161. sum_zone_node_page_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. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
  175. n += sprintf(buf+n, "%s %lu\n",
  176. vmstat_text[i + NR_VM_ZONE_STAT_ITEMS],
  177. node_page_state(pgdat, i));
  178. return n;
  179. }
  180. static DEVICE_ATTR(vmstat, S_IRUGO, node_read_vmstat, NULL);
  181. static ssize_t node_read_distance(struct device *dev,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. int nid = dev->id;
  185. int len = 0;
  186. int i;
  187. /*
  188. * buf is currently PAGE_SIZE in length and each node needs 4 chars
  189. * at the most (distance + space or newline).
  190. */
  191. BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
  192. for_each_online_node(i)
  193. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  194. len += sprintf(buf + len, "\n");
  195. return len;
  196. }
  197. static DEVICE_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  198. static struct attribute *node_dev_attrs[] = {
  199. &dev_attr_cpumap.attr,
  200. &dev_attr_cpulist.attr,
  201. &dev_attr_meminfo.attr,
  202. &dev_attr_numastat.attr,
  203. &dev_attr_distance.attr,
  204. &dev_attr_vmstat.attr,
  205. NULL
  206. };
  207. ATTRIBUTE_GROUPS(node_dev);
  208. #ifdef CONFIG_HUGETLBFS
  209. /*
  210. * hugetlbfs per node attributes registration interface:
  211. * When/if hugetlb[fs] subsystem initializes [sometime after this module],
  212. * it will register its per node attributes for all online nodes with
  213. * memory. It will also call register_hugetlbfs_with_node(), below, to
  214. * register its attribute registration functions with this node driver.
  215. * Once these hooks have been initialized, the node driver will call into
  216. * the hugetlb module to [un]register attributes for hot-plugged nodes.
  217. */
  218. static node_registration_func_t __hugetlb_register_node;
  219. static node_registration_func_t __hugetlb_unregister_node;
  220. static inline bool hugetlb_register_node(struct node *node)
  221. {
  222. if (__hugetlb_register_node &&
  223. node_state(node->dev.id, N_MEMORY)) {
  224. __hugetlb_register_node(node);
  225. return true;
  226. }
  227. return false;
  228. }
  229. static inline void hugetlb_unregister_node(struct node *node)
  230. {
  231. if (__hugetlb_unregister_node)
  232. __hugetlb_unregister_node(node);
  233. }
  234. void register_hugetlbfs_with_node(node_registration_func_t doregister,
  235. node_registration_func_t unregister)
  236. {
  237. __hugetlb_register_node = doregister;
  238. __hugetlb_unregister_node = unregister;
  239. }
  240. #else
  241. static inline void hugetlb_register_node(struct node *node) {}
  242. static inline void hugetlb_unregister_node(struct node *node) {}
  243. #endif
  244. static void node_device_release(struct device *dev)
  245. {
  246. struct node *node = to_node(dev);
  247. #if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HUGETLBFS)
  248. /*
  249. * We schedule the work only when a memory section is
  250. * onlined/offlined on this node. When we come here,
  251. * all the memory on this node has been offlined,
  252. * so we won't enqueue new work to this work.
  253. *
  254. * The work is using node->node_work, so we should
  255. * flush work before freeing the memory.
  256. */
  257. flush_work(&node->node_work);
  258. #endif
  259. kfree(node);
  260. }
  261. /*
  262. * register_node - Setup a sysfs device for a node.
  263. * @num - Node number to use when creating the device.
  264. *
  265. * Initialize and register the node device.
  266. */
  267. static int register_node(struct node *node, int num, struct node *parent)
  268. {
  269. int error;
  270. node->dev.id = num;
  271. node->dev.bus = &node_subsys;
  272. node->dev.release = node_device_release;
  273. node->dev.groups = node_dev_groups;
  274. error = device_register(&node->dev);
  275. if (!error){
  276. hugetlb_register_node(node);
  277. compaction_register_node(node);
  278. }
  279. return error;
  280. }
  281. /**
  282. * unregister_node - unregister a node device
  283. * @node: node going away
  284. *
  285. * Unregisters a node device @node. All the devices on the node must be
  286. * unregistered before calling this function.
  287. */
  288. void unregister_node(struct node *node)
  289. {
  290. hugetlb_unregister_node(node); /* no-op, if memoryless node */
  291. device_unregister(&node->dev);
  292. }
  293. struct node *node_devices[MAX_NUMNODES];
  294. /*
  295. * register cpu under node
  296. */
  297. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  298. {
  299. int ret;
  300. struct device *obj;
  301. if (!node_online(nid))
  302. return 0;
  303. obj = get_cpu_device(cpu);
  304. if (!obj)
  305. return 0;
  306. ret = sysfs_create_link(&node_devices[nid]->dev.kobj,
  307. &obj->kobj,
  308. kobject_name(&obj->kobj));
  309. if (ret)
  310. return ret;
  311. return sysfs_create_link(&obj->kobj,
  312. &node_devices[nid]->dev.kobj,
  313. kobject_name(&node_devices[nid]->dev.kobj));
  314. }
  315. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  316. {
  317. struct device *obj;
  318. if (!node_online(nid))
  319. return 0;
  320. obj = get_cpu_device(cpu);
  321. if (!obj)
  322. return 0;
  323. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  324. kobject_name(&obj->kobj));
  325. sysfs_remove_link(&obj->kobj,
  326. kobject_name(&node_devices[nid]->dev.kobj));
  327. return 0;
  328. }
  329. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  330. #define page_initialized(page) (page->lru.next)
  331. static int __ref get_nid_for_pfn(unsigned long pfn)
  332. {
  333. struct page *page;
  334. if (!pfn_valid_within(pfn))
  335. return -1;
  336. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  337. if (system_state == SYSTEM_BOOTING)
  338. return early_pfn_to_nid(pfn);
  339. #endif
  340. page = pfn_to_page(pfn);
  341. if (!page_initialized(page))
  342. return -1;
  343. return pfn_to_nid(pfn);
  344. }
  345. /* register memory section under specified node if it spans that node */
  346. int register_mem_sect_under_node(struct memory_block *mem_blk, int nid)
  347. {
  348. int ret;
  349. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  350. if (!mem_blk)
  351. return -EFAULT;
  352. if (!node_online(nid))
  353. return 0;
  354. sect_start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
  355. sect_end_pfn = section_nr_to_pfn(mem_blk->end_section_nr);
  356. sect_end_pfn += PAGES_PER_SECTION - 1;
  357. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  358. int page_nid;
  359. /*
  360. * memory block could have several absent sections from start.
  361. * skip pfn range from absent section
  362. */
  363. if (!pfn_present(pfn)) {
  364. pfn = round_down(pfn + PAGES_PER_SECTION,
  365. PAGES_PER_SECTION) - 1;
  366. continue;
  367. }
  368. page_nid = get_nid_for_pfn(pfn);
  369. if (page_nid < 0)
  370. continue;
  371. if (page_nid != nid)
  372. continue;
  373. ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
  374. &mem_blk->dev.kobj,
  375. kobject_name(&mem_blk->dev.kobj));
  376. if (ret)
  377. return ret;
  378. return sysfs_create_link_nowarn(&mem_blk->dev.kobj,
  379. &node_devices[nid]->dev.kobj,
  380. kobject_name(&node_devices[nid]->dev.kobj));
  381. }
  382. /* mem section does not span the specified node */
  383. return 0;
  384. }
  385. /* unregister memory section under all nodes that it spans */
  386. int unregister_mem_sect_under_nodes(struct memory_block *mem_blk,
  387. unsigned long phys_index)
  388. {
  389. NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL);
  390. unsigned long pfn, sect_start_pfn, sect_end_pfn;
  391. if (!mem_blk) {
  392. NODEMASK_FREE(unlinked_nodes);
  393. return -EFAULT;
  394. }
  395. if (!unlinked_nodes)
  396. return -ENOMEM;
  397. nodes_clear(*unlinked_nodes);
  398. sect_start_pfn = section_nr_to_pfn(phys_index);
  399. sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1;
  400. for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) {
  401. int nid;
  402. nid = get_nid_for_pfn(pfn);
  403. if (nid < 0)
  404. continue;
  405. if (!node_online(nid))
  406. continue;
  407. if (node_test_and_set(nid, *unlinked_nodes))
  408. continue;
  409. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  410. kobject_name(&mem_blk->dev.kobj));
  411. sysfs_remove_link(&mem_blk->dev.kobj,
  412. kobject_name(&node_devices[nid]->dev.kobj));
  413. }
  414. NODEMASK_FREE(unlinked_nodes);
  415. return 0;
  416. }
  417. static int link_mem_sections(int nid)
  418. {
  419. unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
  420. unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
  421. unsigned long pfn;
  422. struct memory_block *mem_blk = NULL;
  423. int err = 0;
  424. for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
  425. unsigned long section_nr = pfn_to_section_nr(pfn);
  426. struct mem_section *mem_sect;
  427. int ret;
  428. if (!present_section_nr(section_nr))
  429. continue;
  430. mem_sect = __nr_to_section(section_nr);
  431. /* same memblock ? */
  432. if (mem_blk)
  433. if ((section_nr >= mem_blk->start_section_nr) &&
  434. (section_nr <= mem_blk->end_section_nr))
  435. continue;
  436. mem_blk = find_memory_block_hinted(mem_sect, mem_blk);
  437. ret = register_mem_sect_under_node(mem_blk, nid);
  438. if (!err)
  439. err = ret;
  440. /* discard ref obtained in find_memory_block() */
  441. }
  442. if (mem_blk)
  443. kobject_put(&mem_blk->dev.kobj);
  444. return err;
  445. }
  446. #ifdef CONFIG_HUGETLBFS
  447. /*
  448. * Handle per node hstate attribute [un]registration on transistions
  449. * to/from memoryless state.
  450. */
  451. static void node_hugetlb_work(struct work_struct *work)
  452. {
  453. struct node *node = container_of(work, struct node, node_work);
  454. /*
  455. * We only get here when a node transitions to/from memoryless state.
  456. * We can detect which transition occurred by examining whether the
  457. * node has memory now. hugetlb_register_node() already check this
  458. * so we try to register the attributes. If that fails, then the
  459. * node has transitioned to memoryless, try to unregister the
  460. * attributes.
  461. */
  462. if (!hugetlb_register_node(node))
  463. hugetlb_unregister_node(node);
  464. }
  465. static void init_node_hugetlb_work(int nid)
  466. {
  467. INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work);
  468. }
  469. static int node_memory_callback(struct notifier_block *self,
  470. unsigned long action, void *arg)
  471. {
  472. struct memory_notify *mnb = arg;
  473. int nid = mnb->status_change_nid;
  474. switch (action) {
  475. case MEM_ONLINE:
  476. case MEM_OFFLINE:
  477. /*
  478. * offload per node hstate [un]registration to a work thread
  479. * when transitioning to/from memoryless state.
  480. */
  481. if (nid != NUMA_NO_NODE)
  482. schedule_work(&node_devices[nid]->node_work);
  483. break;
  484. case MEM_GOING_ONLINE:
  485. case MEM_GOING_OFFLINE:
  486. case MEM_CANCEL_ONLINE:
  487. case MEM_CANCEL_OFFLINE:
  488. default:
  489. break;
  490. }
  491. return NOTIFY_OK;
  492. }
  493. #endif /* CONFIG_HUGETLBFS */
  494. #else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
  495. static int link_mem_sections(int nid) { return 0; }
  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 = 0;
  509. int cpu;
  510. if (node_online(nid)) {
  511. int p_node = parent_node(nid);
  512. struct node *parent = NULL;
  513. if (p_node != nid)
  514. parent = node_devices[p_node];
  515. node_devices[nid] = kzalloc(sizeof(struct node), GFP_KERNEL);
  516. if (!node_devices[nid])
  517. return -ENOMEM;
  518. error = register_node(node_devices[nid], nid, parent);
  519. /* link cpu under this node */
  520. for_each_present_cpu(cpu) {
  521. if (cpu_to_node(cpu) == nid)
  522. register_cpu_under_node(cpu, nid);
  523. }
  524. /* link memory sections under this node */
  525. error = link_mem_sections(nid);
  526. /* initialize work queue for memory hot plug */
  527. init_node_hugetlb_work(nid);
  528. }
  529. return error;
  530. }
  531. void unregister_one_node(int nid)
  532. {
  533. if (!node_devices[nid])
  534. return;
  535. unregister_node(node_devices[nid]);
  536. node_devices[nid] = NULL;
  537. }
  538. /*
  539. * node states attributes
  540. */
  541. static ssize_t print_nodes_state(enum node_states state, char *buf)
  542. {
  543. int n;
  544. n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
  545. nodemask_pr_args(&node_states[state]));
  546. buf[n++] = '\n';
  547. buf[n] = '\0';
  548. return n;
  549. }
  550. struct node_attr {
  551. struct device_attribute attr;
  552. enum node_states state;
  553. };
  554. static ssize_t show_node_state(struct device *dev,
  555. struct device_attribute *attr, char *buf)
  556. {
  557. struct node_attr *na = container_of(attr, struct node_attr, attr);
  558. return print_nodes_state(na->state, buf);
  559. }
  560. #define _NODE_ATTR(name, state) \
  561. { __ATTR(name, 0444, show_node_state, NULL), state }
  562. static struct node_attr node_state_attr[] = {
  563. [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
  564. [N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
  565. [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
  566. #ifdef CONFIG_HIGHMEM
  567. [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
  568. #endif
  569. #ifdef CONFIG_MOVABLE_NODE
  570. [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
  571. #endif
  572. [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
  573. };
  574. static struct attribute *node_state_attrs[] = {
  575. &node_state_attr[N_POSSIBLE].attr.attr,
  576. &node_state_attr[N_ONLINE].attr.attr,
  577. &node_state_attr[N_NORMAL_MEMORY].attr.attr,
  578. #ifdef CONFIG_HIGHMEM
  579. &node_state_attr[N_HIGH_MEMORY].attr.attr,
  580. #endif
  581. #ifdef CONFIG_MOVABLE_NODE
  582. &node_state_attr[N_MEMORY].attr.attr,
  583. #endif
  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);