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