node.c 17 KB

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