node.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Basic Node interface support
  4. */
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/mm.h>
  8. #include <linux/memory.h>
  9. #include <linux/mempolicy.h>
  10. #include <linux/vmstat.h>
  11. #include <linux/notifier.h>
  12. #include <linux/node.h>
  13. #include <linux/hugetlb.h>
  14. #include <linux/compaction.h>
  15. #include <linux/cpumask.h>
  16. #include <linux/topology.h>
  17. #include <linux/nodemask.h>
  18. #include <linux/cpu.h>
  19. #include <linux/device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/swap.h>
  22. #include <linux/slab.h>
  23. #include <linux/memblock.h>
  24. static const struct bus_type node_subsys = {
  25. .name = "node",
  26. .dev_name = "node",
  27. };
  28. static inline ssize_t cpumap_read(struct file *file, struct kobject *kobj,
  29. const struct bin_attribute *attr, char *buf,
  30. loff_t off, size_t count)
  31. {
  32. struct device *dev = kobj_to_dev(kobj);
  33. struct node *node_dev = to_node(dev);
  34. cpumask_var_t mask;
  35. ssize_t n;
  36. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  37. return 0;
  38. cpumask_and(mask, cpumask_of_node(node_dev->dev.id), cpu_online_mask);
  39. n = cpumap_print_bitmask_to_buf(buf, mask, off, count);
  40. free_cpumask_var(mask);
  41. return n;
  42. }
  43. static const BIN_ATTR_RO(cpumap, CPUMAP_FILE_MAX_BYTES);
  44. static inline ssize_t cpulist_read(struct file *file, struct kobject *kobj,
  45. const struct bin_attribute *attr, char *buf,
  46. loff_t off, size_t count)
  47. {
  48. struct device *dev = kobj_to_dev(kobj);
  49. struct node *node_dev = to_node(dev);
  50. cpumask_var_t mask;
  51. ssize_t n;
  52. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  53. return 0;
  54. cpumask_and(mask, cpumask_of_node(node_dev->dev.id), cpu_online_mask);
  55. n = cpumap_print_list_to_buf(buf, mask, off, count);
  56. free_cpumask_var(mask);
  57. return n;
  58. }
  59. static const BIN_ATTR_RO(cpulist, CPULIST_FILE_MAX_BYTES);
  60. /**
  61. * struct node_access_nodes - Access class device to hold user visible
  62. * relationships to other nodes.
  63. * @dev: Device for this memory access class
  64. * @list_node: List element in the node's access list
  65. * @access: The access class rank
  66. * @coord: Heterogeneous memory performance coordinates
  67. */
  68. struct node_access_nodes {
  69. struct device dev;
  70. struct list_head list_node;
  71. unsigned int access;
  72. #ifdef CONFIG_HMEM_REPORTING
  73. struct access_coordinate coord;
  74. #endif
  75. };
  76. #define to_access_nodes(dev) container_of(dev, struct node_access_nodes, dev)
  77. static struct attribute *node_init_access_node_attrs[] = {
  78. NULL,
  79. };
  80. static struct attribute *node_targ_access_node_attrs[] = {
  81. NULL,
  82. };
  83. static const struct attribute_group initiators = {
  84. .name = "initiators",
  85. .attrs = node_init_access_node_attrs,
  86. };
  87. static const struct attribute_group targets = {
  88. .name = "targets",
  89. .attrs = node_targ_access_node_attrs,
  90. };
  91. static const struct attribute_group *node_access_node_groups[] = {
  92. &initiators,
  93. &targets,
  94. NULL,
  95. };
  96. #ifdef CONFIG_MEMORY_HOTPLUG
  97. static BLOCKING_NOTIFIER_HEAD(node_chain);
  98. int register_node_notifier(struct notifier_block *nb)
  99. {
  100. return blocking_notifier_chain_register(&node_chain, nb);
  101. }
  102. EXPORT_SYMBOL(register_node_notifier);
  103. void unregister_node_notifier(struct notifier_block *nb)
  104. {
  105. blocking_notifier_chain_unregister(&node_chain, nb);
  106. }
  107. EXPORT_SYMBOL(unregister_node_notifier);
  108. int node_notify(unsigned long val, void *v)
  109. {
  110. return blocking_notifier_call_chain(&node_chain, val, v);
  111. }
  112. #endif
  113. static void node_remove_accesses(struct node *node)
  114. {
  115. struct node_access_nodes *c, *cnext;
  116. list_for_each_entry_safe(c, cnext, &node->access_list, list_node) {
  117. list_del(&c->list_node);
  118. device_unregister(&c->dev);
  119. }
  120. }
  121. static void node_access_release(struct device *dev)
  122. {
  123. kfree(to_access_nodes(dev));
  124. }
  125. static struct node_access_nodes *node_init_node_access(struct node *node,
  126. enum access_coordinate_class access)
  127. {
  128. struct node_access_nodes *access_node;
  129. struct device *dev;
  130. list_for_each_entry(access_node, &node->access_list, list_node)
  131. if (access_node->access == access)
  132. return access_node;
  133. access_node = kzalloc_obj(*access_node);
  134. if (!access_node)
  135. return NULL;
  136. access_node->access = access;
  137. dev = &access_node->dev;
  138. dev->parent = &node->dev;
  139. dev->release = node_access_release;
  140. dev->groups = node_access_node_groups;
  141. if (dev_set_name(dev, "access%u", access))
  142. goto free;
  143. if (device_register(dev))
  144. goto free_name;
  145. pm_runtime_no_callbacks(dev);
  146. list_add_tail(&access_node->list_node, &node->access_list);
  147. return access_node;
  148. free_name:
  149. kfree_const(dev->kobj.name);
  150. free:
  151. kfree(access_node);
  152. return NULL;
  153. }
  154. #ifdef CONFIG_HMEM_REPORTING
  155. #define ACCESS_ATTR(property) \
  156. static ssize_t property##_show(struct device *dev, \
  157. struct device_attribute *attr, \
  158. char *buf) \
  159. { \
  160. return sysfs_emit(buf, "%u\n", \
  161. to_access_nodes(dev)->coord.property); \
  162. } \
  163. static DEVICE_ATTR_RO(property)
  164. ACCESS_ATTR(read_bandwidth);
  165. ACCESS_ATTR(read_latency);
  166. ACCESS_ATTR(write_bandwidth);
  167. ACCESS_ATTR(write_latency);
  168. static struct attribute *access_attrs[] = {
  169. &dev_attr_read_bandwidth.attr,
  170. &dev_attr_read_latency.attr,
  171. &dev_attr_write_bandwidth.attr,
  172. &dev_attr_write_latency.attr,
  173. NULL,
  174. };
  175. /**
  176. * node_set_perf_attrs - Set the performance values for given access class
  177. * @nid: Node identifier to be set
  178. * @coord: Heterogeneous memory performance coordinates
  179. * @access: The access class the for the given attributes
  180. */
  181. void node_set_perf_attrs(unsigned int nid, struct access_coordinate *coord,
  182. enum access_coordinate_class access)
  183. {
  184. struct node_access_nodes *c;
  185. struct node *node;
  186. int i;
  187. if (WARN_ON_ONCE(!node_online(nid)))
  188. return;
  189. node = node_devices[nid];
  190. c = node_init_node_access(node, access);
  191. if (!c)
  192. return;
  193. c->coord = *coord;
  194. for (i = 0; access_attrs[i] != NULL; i++) {
  195. if (sysfs_add_file_to_group(&c->dev.kobj, access_attrs[i],
  196. "initiators")) {
  197. pr_info("failed to add performance attribute to node %d\n",
  198. nid);
  199. break;
  200. }
  201. }
  202. /* When setting CPU access coordinates, update mempolicy */
  203. if (access == ACCESS_COORDINATE_CPU) {
  204. if (mempolicy_set_node_perf(nid, coord)) {
  205. pr_info("failed to set mempolicy attrs for node %d\n",
  206. nid);
  207. }
  208. }
  209. }
  210. EXPORT_SYMBOL_GPL(node_set_perf_attrs);
  211. /**
  212. * node_update_perf_attrs - Update the performance values for given access class
  213. * @nid: Node identifier to be updated
  214. * @coord: Heterogeneous memory performance coordinates
  215. * @access: The access class for the given attributes
  216. */
  217. void node_update_perf_attrs(unsigned int nid, struct access_coordinate *coord,
  218. enum access_coordinate_class access)
  219. {
  220. struct node_access_nodes *access_node;
  221. struct node *node;
  222. int i;
  223. if (WARN_ON_ONCE(!node_online(nid)))
  224. return;
  225. node = node_devices[nid];
  226. list_for_each_entry(access_node, &node->access_list, list_node) {
  227. if (access_node->access != access)
  228. continue;
  229. access_node->coord = *coord;
  230. for (i = 0; access_attrs[i]; i++) {
  231. sysfs_notify(&access_node->dev.kobj,
  232. NULL, access_attrs[i]->name);
  233. }
  234. break;
  235. }
  236. /* When setting CPU access coordinates, update mempolicy */
  237. if (access != ACCESS_COORDINATE_CPU)
  238. return;
  239. if (mempolicy_set_node_perf(nid, coord))
  240. pr_info("failed to set mempolicy attrs for node %d\n", nid);
  241. }
  242. EXPORT_SYMBOL_GPL(node_update_perf_attrs);
  243. /**
  244. * struct node_cache_info - Internal tracking for memory node caches
  245. * @dev: Device represeting the cache level
  246. * @node: List element for tracking in the node
  247. * @cache_attrs:Attributes for this cache level
  248. */
  249. struct node_cache_info {
  250. struct device dev;
  251. struct list_head node;
  252. struct node_cache_attrs cache_attrs;
  253. };
  254. #define to_cache_info(device) container_of(device, struct node_cache_info, dev)
  255. #define CACHE_ATTR(name, fmt) \
  256. static ssize_t name##_show(struct device *dev, \
  257. struct device_attribute *attr, \
  258. char *buf) \
  259. { \
  260. return sysfs_emit(buf, fmt "\n", \
  261. to_cache_info(dev)->cache_attrs.name); \
  262. } \
  263. static DEVICE_ATTR_RO(name);
  264. CACHE_ATTR(size, "%llu")
  265. CACHE_ATTR(line_size, "%u")
  266. CACHE_ATTR(indexing, "%u")
  267. CACHE_ATTR(write_policy, "%u")
  268. CACHE_ATTR(address_mode, "%#x")
  269. static struct attribute *cache_attrs[] = {
  270. &dev_attr_indexing.attr,
  271. &dev_attr_size.attr,
  272. &dev_attr_line_size.attr,
  273. &dev_attr_write_policy.attr,
  274. &dev_attr_address_mode.attr,
  275. NULL,
  276. };
  277. ATTRIBUTE_GROUPS(cache);
  278. static void node_cache_release(struct device *dev)
  279. {
  280. kfree(dev);
  281. }
  282. static void node_cacheinfo_release(struct device *dev)
  283. {
  284. struct node_cache_info *info = to_cache_info(dev);
  285. kfree(info);
  286. }
  287. static void node_init_cache_dev(struct node *node)
  288. {
  289. struct device *dev;
  290. dev = kzalloc_obj(*dev);
  291. if (!dev)
  292. return;
  293. device_initialize(dev);
  294. dev->parent = &node->dev;
  295. dev->release = node_cache_release;
  296. if (dev_set_name(dev, "memory_side_cache"))
  297. goto put_device;
  298. if (device_add(dev))
  299. goto put_device;
  300. pm_runtime_no_callbacks(dev);
  301. node->cache_dev = dev;
  302. return;
  303. put_device:
  304. put_device(dev);
  305. }
  306. /**
  307. * node_add_cache() - add cache attribute to a memory node
  308. * @nid: Node identifier that has new cache attributes
  309. * @cache_attrs: Attributes for the cache being added
  310. */
  311. void node_add_cache(unsigned int nid, struct node_cache_attrs *cache_attrs)
  312. {
  313. struct node_cache_info *info;
  314. struct device *dev;
  315. struct node *node;
  316. if (!node_online(nid) || !node_devices[nid])
  317. return;
  318. node = node_devices[nid];
  319. list_for_each_entry(info, &node->cache_attrs, node) {
  320. if (info->cache_attrs.level == cache_attrs->level) {
  321. dev_warn(&node->dev,
  322. "attempt to add duplicate cache level:%d\n",
  323. cache_attrs->level);
  324. return;
  325. }
  326. }
  327. if (!node->cache_dev)
  328. node_init_cache_dev(node);
  329. if (!node->cache_dev)
  330. return;
  331. info = kzalloc_obj(*info);
  332. if (!info)
  333. return;
  334. dev = &info->dev;
  335. device_initialize(dev);
  336. dev->parent = node->cache_dev;
  337. dev->release = node_cacheinfo_release;
  338. dev->groups = cache_groups;
  339. if (dev_set_name(dev, "index%d", cache_attrs->level))
  340. goto put_device;
  341. info->cache_attrs = *cache_attrs;
  342. if (device_add(dev)) {
  343. dev_warn(&node->dev, "failed to add cache level:%d\n",
  344. cache_attrs->level);
  345. goto put_device;
  346. }
  347. pm_runtime_no_callbacks(dev);
  348. list_add_tail(&info->node, &node->cache_attrs);
  349. return;
  350. put_device:
  351. put_device(dev);
  352. }
  353. static void node_remove_caches(struct node *node)
  354. {
  355. struct node_cache_info *info, *next;
  356. if (!node->cache_dev)
  357. return;
  358. list_for_each_entry_safe(info, next, &node->cache_attrs, node) {
  359. list_del(&info->node);
  360. device_unregister(&info->dev);
  361. }
  362. device_unregister(node->cache_dev);
  363. }
  364. static void node_init_caches(unsigned int nid)
  365. {
  366. INIT_LIST_HEAD(&node_devices[nid]->cache_attrs);
  367. }
  368. #else
  369. static void node_init_caches(unsigned int nid) { }
  370. static void node_remove_caches(struct node *node) { }
  371. #endif
  372. #define K(x) ((x) << (PAGE_SHIFT - 10))
  373. static ssize_t node_read_meminfo(struct device *dev,
  374. struct device_attribute *attr, char *buf)
  375. {
  376. int len = 0;
  377. int nid = dev->id;
  378. struct pglist_data *pgdat = NODE_DATA(nid);
  379. struct sysinfo i;
  380. unsigned long sreclaimable, sunreclaimable;
  381. unsigned long swapcached = 0;
  382. si_meminfo_node(&i, nid);
  383. sreclaimable = node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B);
  384. sunreclaimable = node_page_state_pages(pgdat, NR_SLAB_UNRECLAIMABLE_B);
  385. #ifdef CONFIG_SWAP
  386. swapcached = node_page_state_pages(pgdat, NR_SWAPCACHE);
  387. #endif
  388. len = sysfs_emit_at(buf, len,
  389. "Node %d MemTotal: %8lu kB\n"
  390. "Node %d MemFree: %8lu kB\n"
  391. "Node %d MemUsed: %8lu kB\n"
  392. "Node %d SwapCached: %8lu kB\n"
  393. "Node %d Active: %8lu kB\n"
  394. "Node %d Inactive: %8lu kB\n"
  395. "Node %d Active(anon): %8lu kB\n"
  396. "Node %d Inactive(anon): %8lu kB\n"
  397. "Node %d Active(file): %8lu kB\n"
  398. "Node %d Inactive(file): %8lu kB\n"
  399. "Node %d Unevictable: %8lu kB\n"
  400. "Node %d Mlocked: %8lu kB\n",
  401. nid, K(i.totalram),
  402. nid, K(i.freeram),
  403. nid, K(i.totalram - i.freeram),
  404. nid, K(swapcached),
  405. nid, K(node_page_state(pgdat, NR_ACTIVE_ANON) +
  406. node_page_state(pgdat, NR_ACTIVE_FILE)),
  407. nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) +
  408. node_page_state(pgdat, NR_INACTIVE_FILE)),
  409. nid, K(node_page_state(pgdat, NR_ACTIVE_ANON)),
  410. nid, K(node_page_state(pgdat, NR_INACTIVE_ANON)),
  411. nid, K(node_page_state(pgdat, NR_ACTIVE_FILE)),
  412. nid, K(node_page_state(pgdat, NR_INACTIVE_FILE)),
  413. nid, K(node_page_state(pgdat, NR_UNEVICTABLE)),
  414. nid, K(sum_zone_node_page_state(nid, NR_MLOCK)));
  415. #ifdef CONFIG_HIGHMEM
  416. len += sysfs_emit_at(buf, len,
  417. "Node %d HighTotal: %8lu kB\n"
  418. "Node %d HighFree: %8lu kB\n"
  419. "Node %d LowTotal: %8lu kB\n"
  420. "Node %d LowFree: %8lu kB\n",
  421. nid, K(i.totalhigh),
  422. nid, K(i.freehigh),
  423. nid, K(i.totalram - i.totalhigh),
  424. nid, K(i.freeram - i.freehigh));
  425. #endif
  426. len += sysfs_emit_at(buf, len,
  427. "Node %d Dirty: %8lu kB\n"
  428. "Node %d Writeback: %8lu kB\n"
  429. "Node %d FilePages: %8lu kB\n"
  430. "Node %d Mapped: %8lu kB\n"
  431. "Node %d AnonPages: %8lu kB\n"
  432. "Node %d Shmem: %8lu kB\n"
  433. "Node %d KernelStack: %8lu kB\n"
  434. #ifdef CONFIG_SHADOW_CALL_STACK
  435. "Node %d ShadowCallStack:%8lu kB\n"
  436. #endif
  437. "Node %d PageTables: %8lu kB\n"
  438. "Node %d SecPageTables: %8lu kB\n"
  439. "Node %d NFS_Unstable: %8lu kB\n"
  440. "Node %d Bounce: %8lu kB\n"
  441. "Node %d WritebackTmp: %8lu kB\n"
  442. "Node %d KReclaimable: %8lu kB\n"
  443. "Node %d Slab: %8lu kB\n"
  444. "Node %d SReclaimable: %8lu kB\n"
  445. "Node %d SUnreclaim: %8lu kB\n"
  446. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  447. "Node %d AnonHugePages: %8lu kB\n"
  448. "Node %d ShmemHugePages: %8lu kB\n"
  449. "Node %d ShmemPmdMapped: %8lu kB\n"
  450. "Node %d FileHugePages: %8lu kB\n"
  451. "Node %d FilePmdMapped: %8lu kB\n"
  452. #endif
  453. #ifdef CONFIG_UNACCEPTED_MEMORY
  454. "Node %d Unaccepted: %8lu kB\n"
  455. #endif
  456. ,
  457. nid, K(node_page_state(pgdat, NR_FILE_DIRTY)),
  458. nid, K(node_page_state(pgdat, NR_WRITEBACK)),
  459. nid, K(node_page_state(pgdat, NR_FILE_PAGES)),
  460. nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
  461. nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
  462. nid, K(i.sharedram),
  463. nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
  464. #ifdef CONFIG_SHADOW_CALL_STACK
  465. nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
  466. #endif
  467. nid, K(node_page_state(pgdat, NR_PAGETABLE)),
  468. nid, K(node_page_state(pgdat, NR_SECONDARY_PAGETABLE)),
  469. nid, 0UL,
  470. nid, 0UL,
  471. nid, 0UL,
  472. nid, K(sreclaimable +
  473. node_page_state(pgdat, NR_KERNEL_MISC_RECLAIMABLE)),
  474. nid, K(sreclaimable + sunreclaimable),
  475. nid, K(sreclaimable),
  476. nid, K(sunreclaimable)
  477. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  478. ,
  479. nid, K(node_page_state(pgdat, NR_ANON_THPS)),
  480. nid, K(node_page_state(pgdat, NR_SHMEM_THPS)),
  481. nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
  482. nid, K(node_page_state(pgdat, NR_FILE_THPS)),
  483. nid, K(node_page_state(pgdat, NR_FILE_PMDMAPPED))
  484. #endif
  485. #ifdef CONFIG_UNACCEPTED_MEMORY
  486. ,
  487. nid, K(sum_zone_node_page_state(nid, NR_UNACCEPTED))
  488. #endif
  489. );
  490. len += hugetlb_report_node_meminfo(buf, len, nid);
  491. return len;
  492. }
  493. #undef K
  494. static DEVICE_ATTR(meminfo, 0444, node_read_meminfo, NULL);
  495. static ssize_t node_read_numastat(struct device *dev,
  496. struct device_attribute *attr, char *buf)
  497. {
  498. fold_vm_numa_events();
  499. return sysfs_emit(buf,
  500. "numa_hit %lu\n"
  501. "numa_miss %lu\n"
  502. "numa_foreign %lu\n"
  503. "interleave_hit %lu\n"
  504. "local_node %lu\n"
  505. "other_node %lu\n",
  506. sum_zone_numa_event_state(dev->id, NUMA_HIT),
  507. sum_zone_numa_event_state(dev->id, NUMA_MISS),
  508. sum_zone_numa_event_state(dev->id, NUMA_FOREIGN),
  509. sum_zone_numa_event_state(dev->id, NUMA_INTERLEAVE_HIT),
  510. sum_zone_numa_event_state(dev->id, NUMA_LOCAL),
  511. sum_zone_numa_event_state(dev->id, NUMA_OTHER));
  512. }
  513. static DEVICE_ATTR(numastat, 0444, node_read_numastat, NULL);
  514. static ssize_t node_read_vmstat(struct device *dev,
  515. struct device_attribute *attr, char *buf)
  516. {
  517. int nid = dev->id;
  518. struct pglist_data *pgdat = NODE_DATA(nid);
  519. int i;
  520. int len = 0;
  521. for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
  522. len += sysfs_emit_at(buf, len, "%s %lu\n",
  523. zone_stat_name(i),
  524. sum_zone_node_page_state(nid, i));
  525. #ifdef CONFIG_NUMA
  526. fold_vm_numa_events();
  527. for (i = 0; i < NR_VM_NUMA_EVENT_ITEMS; i++)
  528. len += sysfs_emit_at(buf, len, "%s %lu\n",
  529. numa_stat_name(i),
  530. sum_zone_numa_event_state(nid, i));
  531. #endif
  532. for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++) {
  533. unsigned long pages = node_page_state_pages(pgdat, i);
  534. if (vmstat_item_print_in_thp(i))
  535. pages /= HPAGE_PMD_NR;
  536. len += sysfs_emit_at(buf, len, "%s %lu\n", node_stat_name(i),
  537. pages);
  538. }
  539. return len;
  540. }
  541. static DEVICE_ATTR(vmstat, 0444, node_read_vmstat, NULL);
  542. static ssize_t node_read_distance(struct device *dev,
  543. struct device_attribute *attr, char *buf)
  544. {
  545. int nid = dev->id;
  546. int len = 0;
  547. int i;
  548. /*
  549. * buf is currently PAGE_SIZE in length and each node needs 4 chars
  550. * at the most (distance + space or newline).
  551. */
  552. BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
  553. for_each_online_node(i) {
  554. len += sysfs_emit_at(buf, len, "%s%d",
  555. i ? " " : "", node_distance(nid, i));
  556. }
  557. len += sysfs_emit_at(buf, len, "\n");
  558. return len;
  559. }
  560. static DEVICE_ATTR(distance, 0444, node_read_distance, NULL);
  561. static struct attribute *node_dev_attrs[] = {
  562. &dev_attr_meminfo.attr,
  563. &dev_attr_numastat.attr,
  564. &dev_attr_distance.attr,
  565. &dev_attr_vmstat.attr,
  566. NULL
  567. };
  568. static const struct bin_attribute *node_dev_bin_attrs[] = {
  569. &bin_attr_cpumap,
  570. &bin_attr_cpulist,
  571. NULL
  572. };
  573. static const struct attribute_group node_dev_group = {
  574. .attrs = node_dev_attrs,
  575. .bin_attrs = node_dev_bin_attrs,
  576. };
  577. static const struct attribute_group *node_dev_groups[] = {
  578. &node_dev_group,
  579. #ifdef CONFIG_HAVE_ARCH_NODE_DEV_GROUP
  580. &arch_node_dev_group,
  581. #endif
  582. #ifdef CONFIG_MEMORY_FAILURE
  583. &memory_failure_attr_group,
  584. #endif
  585. NULL
  586. };
  587. static void node_device_release(struct device *dev)
  588. {
  589. kfree(to_node(dev));
  590. }
  591. struct node *node_devices[MAX_NUMNODES];
  592. /*
  593. * register cpu under node
  594. */
  595. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  596. {
  597. int ret;
  598. struct device *obj;
  599. if (!node_online(nid))
  600. return 0;
  601. obj = get_cpu_device(cpu);
  602. if (!obj)
  603. return 0;
  604. ret = sysfs_create_link(&node_devices[nid]->dev.kobj,
  605. &obj->kobj,
  606. kobject_name(&obj->kobj));
  607. if (ret)
  608. return ret;
  609. return sysfs_create_link(&obj->kobj,
  610. &node_devices[nid]->dev.kobj,
  611. kobject_name(&node_devices[nid]->dev.kobj));
  612. }
  613. /**
  614. * register_memory_node_under_compute_node - link memory node to its compute
  615. * node for a given access class.
  616. * @mem_nid: Memory node number
  617. * @cpu_nid: Cpu node number
  618. * @access: Access class to register
  619. *
  620. * Description:
  621. * For use with platforms that may have separate memory and compute nodes.
  622. * This function will export node relationships linking which memory
  623. * initiator nodes can access memory targets at a given ranked access
  624. * class.
  625. */
  626. int register_memory_node_under_compute_node(unsigned int mem_nid,
  627. unsigned int cpu_nid,
  628. enum access_coordinate_class access)
  629. {
  630. struct node *init_node, *targ_node;
  631. struct node_access_nodes *initiator, *target;
  632. int ret;
  633. if (!node_online(cpu_nid) || !node_online(mem_nid))
  634. return -ENODEV;
  635. init_node = node_devices[cpu_nid];
  636. targ_node = node_devices[mem_nid];
  637. initiator = node_init_node_access(init_node, access);
  638. target = node_init_node_access(targ_node, access);
  639. if (!initiator || !target)
  640. return -ENOMEM;
  641. ret = sysfs_add_link_to_group(&initiator->dev.kobj, "targets",
  642. &targ_node->dev.kobj,
  643. dev_name(&targ_node->dev));
  644. if (ret)
  645. return ret;
  646. ret = sysfs_add_link_to_group(&target->dev.kobj, "initiators",
  647. &init_node->dev.kobj,
  648. dev_name(&init_node->dev));
  649. if (ret)
  650. goto err;
  651. return 0;
  652. err:
  653. sysfs_remove_link_from_group(&initiator->dev.kobj, "targets",
  654. dev_name(&targ_node->dev));
  655. return ret;
  656. }
  657. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  658. {
  659. struct device *obj;
  660. if (!node_online(nid))
  661. return 0;
  662. obj = get_cpu_device(cpu);
  663. if (!obj)
  664. return 0;
  665. sysfs_remove_link(&node_devices[nid]->dev.kobj,
  666. kobject_name(&obj->kobj));
  667. sysfs_remove_link(&obj->kobj,
  668. kobject_name(&node_devices[nid]->dev.kobj));
  669. return 0;
  670. }
  671. #ifdef CONFIG_MEMORY_HOTPLUG
  672. static void do_register_memory_block_under_node(int nid,
  673. struct memory_block *mem_blk)
  674. {
  675. int ret;
  676. ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
  677. &mem_blk->dev.kobj,
  678. kobject_name(&mem_blk->dev.kobj));
  679. if (ret && ret != -EEXIST)
  680. dev_err_ratelimited(&node_devices[nid]->dev,
  681. "can't create link to %s in sysfs (%d)\n",
  682. kobject_name(&mem_blk->dev.kobj), ret);
  683. ret = sysfs_create_link_nowarn(&mem_blk->dev.kobj,
  684. &node_devices[nid]->dev.kobj,
  685. kobject_name(&node_devices[nid]->dev.kobj));
  686. if (ret && ret != -EEXIST)
  687. dev_err_ratelimited(&mem_blk->dev,
  688. "can't create link to %s in sysfs (%d)\n",
  689. kobject_name(&node_devices[nid]->dev.kobj),
  690. ret);
  691. }
  692. /*
  693. * During hotplug we know that all pages in the memory block belong to the same
  694. * node.
  695. */
  696. static int register_mem_block_under_node_hotplug(struct memory_block *mem_blk,
  697. void *arg)
  698. {
  699. int nid = *(int *)arg;
  700. do_register_memory_block_under_node(nid, mem_blk);
  701. return 0;
  702. }
  703. /*
  704. * Unregister a memory block device under the node it spans. Memory blocks
  705. * with multiple nodes cannot be offlined and therefore also never be removed.
  706. */
  707. void unregister_memory_block_under_nodes(struct memory_block *mem_blk)
  708. {
  709. if (mem_blk->nid == NUMA_NO_NODE)
  710. return;
  711. sysfs_remove_link(&node_devices[mem_blk->nid]->dev.kobj,
  712. kobject_name(&mem_blk->dev.kobj));
  713. sysfs_remove_link(&mem_blk->dev.kobj,
  714. kobject_name(&node_devices[mem_blk->nid]->dev.kobj));
  715. }
  716. /* register all memory blocks under the corresponding nodes */
  717. static void register_memory_blocks_under_nodes(void)
  718. {
  719. struct memblock_region *r;
  720. for_each_mem_region(r) {
  721. const unsigned long start_block_id = phys_to_block_id(r->base);
  722. const unsigned long end_block_id = phys_to_block_id(r->base + r->size - 1);
  723. const int nid = memblock_get_region_node(r);
  724. unsigned long block_id;
  725. if (!node_online(nid))
  726. continue;
  727. for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
  728. struct memory_block *mem;
  729. mem = find_memory_block_by_id(block_id);
  730. if (!mem)
  731. continue;
  732. memory_block_add_nid_early(mem, nid);
  733. do_register_memory_block_under_node(nid, mem);
  734. put_device(&mem->dev);
  735. }
  736. }
  737. }
  738. void register_memory_blocks_under_node_hotplug(int nid, unsigned long start_pfn,
  739. unsigned long end_pfn)
  740. {
  741. walk_memory_blocks(PFN_PHYS(start_pfn), PFN_PHYS(end_pfn - start_pfn),
  742. (void *)&nid, register_mem_block_under_node_hotplug);
  743. return;
  744. }
  745. #endif /* CONFIG_MEMORY_HOTPLUG */
  746. /**
  747. * register_node - Initialize and register the node device.
  748. * @nid: Node number to use when creating the device.
  749. *
  750. * Return: 0 on success, -errno otherwise
  751. */
  752. int register_node(int nid)
  753. {
  754. int error;
  755. int cpu;
  756. struct node *node;
  757. node = kzalloc_obj(struct node);
  758. if (!node)
  759. return -ENOMEM;
  760. INIT_LIST_HEAD(&node->access_list);
  761. node->dev.id = nid;
  762. node->dev.bus = &node_subsys;
  763. node->dev.release = node_device_release;
  764. node->dev.groups = node_dev_groups;
  765. error = device_register(&node->dev);
  766. if (error) {
  767. put_device(&node->dev);
  768. return error;
  769. }
  770. node_devices[nid] = node;
  771. hugetlb_register_node(node);
  772. compaction_register_node(node);
  773. reclaim_register_node(node);
  774. /* link cpu under this node */
  775. for_each_present_cpu(cpu) {
  776. if (cpu_to_node(cpu) == nid)
  777. register_cpu_under_node(cpu, nid);
  778. }
  779. node_init_caches(nid);
  780. return error;
  781. }
  782. /**
  783. * unregister_node - unregister a node device
  784. * @nid: nid of the node going away
  785. *
  786. * Unregisters the node device at node id @nid. All the devices on the
  787. * node must be unregistered before calling this function.
  788. */
  789. void unregister_node(int nid)
  790. {
  791. struct node *node = node_devices[nid];
  792. if (!node)
  793. return;
  794. hugetlb_unregister_node(node);
  795. compaction_unregister_node(node);
  796. reclaim_unregister_node(node);
  797. node_remove_accesses(node);
  798. node_remove_caches(node);
  799. device_unregister(&node->dev);
  800. node_devices[nid] = NULL;
  801. }
  802. /*
  803. * node states attributes
  804. */
  805. struct node_attr {
  806. struct device_attribute attr;
  807. enum node_states state;
  808. };
  809. static ssize_t show_node_state(struct device *dev,
  810. struct device_attribute *attr, char *buf)
  811. {
  812. struct node_attr *na = container_of(attr, struct node_attr, attr);
  813. return sysfs_emit(buf, "%*pbl\n",
  814. nodemask_pr_args(&node_states[na->state]));
  815. }
  816. #define _NODE_ATTR(name, state) \
  817. { __ATTR(name, 0444, show_node_state, NULL), state }
  818. static struct node_attr node_state_attr[] = {
  819. [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
  820. [N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
  821. [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
  822. #ifdef CONFIG_HIGHMEM
  823. [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
  824. #endif
  825. [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
  826. [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
  827. [N_GENERIC_INITIATOR] = _NODE_ATTR(has_generic_initiator,
  828. N_GENERIC_INITIATOR),
  829. };
  830. static struct attribute *node_state_attrs[] = {
  831. &node_state_attr[N_POSSIBLE].attr.attr,
  832. &node_state_attr[N_ONLINE].attr.attr,
  833. &node_state_attr[N_NORMAL_MEMORY].attr.attr,
  834. #ifdef CONFIG_HIGHMEM
  835. &node_state_attr[N_HIGH_MEMORY].attr.attr,
  836. #endif
  837. &node_state_attr[N_MEMORY].attr.attr,
  838. &node_state_attr[N_CPU].attr.attr,
  839. &node_state_attr[N_GENERIC_INITIATOR].attr.attr,
  840. NULL
  841. };
  842. static const struct attribute_group memory_root_attr_group = {
  843. .attrs = node_state_attrs,
  844. };
  845. static const struct attribute_group *cpu_root_attr_groups[] = {
  846. &memory_root_attr_group,
  847. NULL,
  848. };
  849. void __init node_dev_init(void)
  850. {
  851. int ret, i;
  852. BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
  853. BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
  854. ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
  855. if (ret)
  856. panic("%s() failed to register subsystem: %d\n", __func__, ret);
  857. /*
  858. * Create all node devices, which will properly link the node
  859. * to already created cpu devices.
  860. */
  861. for_each_online_node(i) {
  862. ret = register_node(i);
  863. if (ret)
  864. panic("%s() failed to add node: %d\n", __func__, ret);
  865. }
  866. register_memory_blocks_under_nodes();
  867. }