node.c 17 KB

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