node.c 17 KB

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