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