node.c 18 KB

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