blob: 21965de8538bea60ba1a32818647f9d4b516c804 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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/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>
David Brazdil0f672f62019-12-10 10:32:29 +000020#include <linux/pm_runtime.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021#include <linux/swap.h>
22#include <linux/slab.h>
23
24static struct bus_type node_subsys = {
25 .name = "node",
26 .dev_name = "node",
27};
28
29
30static ssize_t node_read_cpumap(struct device *dev, bool list, char *buf)
31{
32 ssize_t n;
33 cpumask_var_t mask;
34 struct node *node_dev = to_node(dev);
35
36 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
37 BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
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_to_pagebuf(list, buf, mask);
44 free_cpumask_var(mask);
45
46 return n;
47}
48
Olivier Deprez157378f2022-04-04 15:47:50 +020049static inline ssize_t cpumap_show(struct device *dev,
50 struct device_attribute *attr,
51 char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052{
53 return node_read_cpumap(dev, false, buf);
54}
Olivier Deprez157378f2022-04-04 15:47:50 +020055
56static DEVICE_ATTR_RO(cpumap);
57
58static inline ssize_t cpulist_show(struct device *dev,
59 struct device_attribute *attr,
60 char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061{
62 return node_read_cpumap(dev, true, buf);
63}
64
Olivier Deprez157378f2022-04-04 15:47:50 +020065static DEVICE_ATTR_RO(cpulist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066
David Brazdil0f672f62019-12-10 10:32:29 +000067/**
68 * struct node_access_nodes - Access class device to hold user visible
69 * relationships to other nodes.
70 * @dev: Device for this memory access class
71 * @list_node: List element in the node's access list
72 * @access: The access class rank
73 * @hmem_attrs: Heterogeneous memory performance attributes
74 */
75struct node_access_nodes {
76 struct device dev;
77 struct list_head list_node;
78 unsigned access;
79#ifdef CONFIG_HMEM_REPORTING
80 struct node_hmem_attrs hmem_attrs;
81#endif
82};
83#define to_access_nodes(dev) container_of(dev, struct node_access_nodes, dev)
84
85static struct attribute *node_init_access_node_attrs[] = {
86 NULL,
87};
88
89static struct attribute *node_targ_access_node_attrs[] = {
90 NULL,
91};
92
93static const struct attribute_group initiators = {
94 .name = "initiators",
95 .attrs = node_init_access_node_attrs,
96};
97
98static const struct attribute_group targets = {
99 .name = "targets",
100 .attrs = node_targ_access_node_attrs,
101};
102
103static const struct attribute_group *node_access_node_groups[] = {
104 &initiators,
105 &targets,
106 NULL,
107};
108
109static void node_remove_accesses(struct node *node)
110{
111 struct node_access_nodes *c, *cnext;
112
113 list_for_each_entry_safe(c, cnext, &node->access_list, list_node) {
114 list_del(&c->list_node);
115 device_unregister(&c->dev);
116 }
117}
118
119static void node_access_release(struct device *dev)
120{
121 kfree(to_access_nodes(dev));
122}
123
124static struct node_access_nodes *node_init_node_access(struct node *node,
125 unsigned access)
126{
127 struct node_access_nodes *access_node;
128 struct device *dev;
129
130 list_for_each_entry(access_node, &node->access_list, list_node)
131 if (access_node->access == access)
132 return access_node;
133
134 access_node = kzalloc(sizeof(*access_node), GFP_KERNEL);
135 if (!access_node)
136 return NULL;
137
138 access_node->access = access;
139 dev = &access_node->dev;
140 dev->parent = &node->dev;
141 dev->release = node_access_release;
142 dev->groups = node_access_node_groups;
143 if (dev_set_name(dev, "access%u", access))
144 goto free;
145
146 if (device_register(dev))
147 goto free_name;
148
149 pm_runtime_no_callbacks(dev);
150 list_add_tail(&access_node->list_node, &node->access_list);
151 return access_node;
152free_name:
153 kfree_const(dev->kobj.name);
154free:
155 kfree(access_node);
156 return NULL;
157}
158
159#ifdef CONFIG_HMEM_REPORTING
Olivier Deprez157378f2022-04-04 15:47:50 +0200160#define ACCESS_ATTR(name) \
161static ssize_t name##_show(struct device *dev, \
162 struct device_attribute *attr, \
163 char *buf) \
164{ \
165 return sysfs_emit(buf, "%u\n", \
166 to_access_nodes(dev)->hmem_attrs.name); \
167} \
168static DEVICE_ATTR_RO(name)
David Brazdil0f672f62019-12-10 10:32:29 +0000169
Olivier Deprez157378f2022-04-04 15:47:50 +0200170ACCESS_ATTR(read_bandwidth);
171ACCESS_ATTR(read_latency);
172ACCESS_ATTR(write_bandwidth);
173ACCESS_ATTR(write_latency);
David Brazdil0f672f62019-12-10 10:32:29 +0000174
175static struct attribute *access_attrs[] = {
176 &dev_attr_read_bandwidth.attr,
177 &dev_attr_read_latency.attr,
178 &dev_attr_write_bandwidth.attr,
179 &dev_attr_write_latency.attr,
180 NULL,
181};
182
183/**
184 * node_set_perf_attrs - Set the performance values for given access class
185 * @nid: Node identifier to be set
186 * @hmem_attrs: Heterogeneous memory performance attributes
187 * @access: The access class the for the given attributes
188 */
189void node_set_perf_attrs(unsigned int nid, struct node_hmem_attrs *hmem_attrs,
190 unsigned access)
191{
192 struct node_access_nodes *c;
193 struct node *node;
194 int i;
195
196 if (WARN_ON_ONCE(!node_online(nid)))
197 return;
198
199 node = node_devices[nid];
200 c = node_init_node_access(node, access);
201 if (!c)
202 return;
203
204 c->hmem_attrs = *hmem_attrs;
205 for (i = 0; access_attrs[i] != NULL; i++) {
206 if (sysfs_add_file_to_group(&c->dev.kobj, access_attrs[i],
207 "initiators")) {
208 pr_info("failed to add performance attribute to node %d\n",
209 nid);
210 break;
211 }
212 }
213}
214
215/**
216 * struct node_cache_info - Internal tracking for memory node caches
217 * @dev: Device represeting the cache level
218 * @node: List element for tracking in the node
219 * @cache_attrs:Attributes for this cache level
220 */
221struct node_cache_info {
222 struct device dev;
223 struct list_head node;
224 struct node_cache_attrs cache_attrs;
225};
226#define to_cache_info(device) container_of(device, struct node_cache_info, dev)
227
228#define CACHE_ATTR(name, fmt) \
229static ssize_t name##_show(struct device *dev, \
230 struct device_attribute *attr, \
231 char *buf) \
232{ \
Olivier Deprez157378f2022-04-04 15:47:50 +0200233 return sysfs_emit(buf, fmt "\n", \
234 to_cache_info(dev)->cache_attrs.name); \
David Brazdil0f672f62019-12-10 10:32:29 +0000235} \
236DEVICE_ATTR_RO(name);
237
238CACHE_ATTR(size, "%llu")
239CACHE_ATTR(line_size, "%u")
240CACHE_ATTR(indexing, "%u")
241CACHE_ATTR(write_policy, "%u")
242
243static struct attribute *cache_attrs[] = {
244 &dev_attr_indexing.attr,
245 &dev_attr_size.attr,
246 &dev_attr_line_size.attr,
247 &dev_attr_write_policy.attr,
248 NULL,
249};
250ATTRIBUTE_GROUPS(cache);
251
252static void node_cache_release(struct device *dev)
253{
254 kfree(dev);
255}
256
257static void node_cacheinfo_release(struct device *dev)
258{
259 struct node_cache_info *info = to_cache_info(dev);
260 kfree(info);
261}
262
263static void node_init_cache_dev(struct node *node)
264{
265 struct device *dev;
266
267 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
268 if (!dev)
269 return;
270
Olivier Deprez0e641232021-09-23 10:07:05 +0200271 device_initialize(dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000272 dev->parent = &node->dev;
273 dev->release = node_cache_release;
274 if (dev_set_name(dev, "memory_side_cache"))
Olivier Deprez0e641232021-09-23 10:07:05 +0200275 goto put_device;
David Brazdil0f672f62019-12-10 10:32:29 +0000276
Olivier Deprez0e641232021-09-23 10:07:05 +0200277 if (device_add(dev))
278 goto put_device;
David Brazdil0f672f62019-12-10 10:32:29 +0000279
280 pm_runtime_no_callbacks(dev);
281 node->cache_dev = dev;
282 return;
Olivier Deprez0e641232021-09-23 10:07:05 +0200283put_device:
284 put_device(dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000285}
286
287/**
288 * node_add_cache() - add cache attribute to a memory node
289 * @nid: Node identifier that has new cache attributes
290 * @cache_attrs: Attributes for the cache being added
291 */
292void node_add_cache(unsigned int nid, struct node_cache_attrs *cache_attrs)
293{
294 struct node_cache_info *info;
295 struct device *dev;
296 struct node *node;
297
298 if (!node_online(nid) || !node_devices[nid])
299 return;
300
301 node = node_devices[nid];
302 list_for_each_entry(info, &node->cache_attrs, node) {
303 if (info->cache_attrs.level == cache_attrs->level) {
304 dev_warn(&node->dev,
305 "attempt to add duplicate cache level:%d\n",
306 cache_attrs->level);
307 return;
308 }
309 }
310
311 if (!node->cache_dev)
312 node_init_cache_dev(node);
313 if (!node->cache_dev)
314 return;
315
316 info = kzalloc(sizeof(*info), GFP_KERNEL);
317 if (!info)
318 return;
319
320 dev = &info->dev;
Olivier Deprez0e641232021-09-23 10:07:05 +0200321 device_initialize(dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000322 dev->parent = node->cache_dev;
323 dev->release = node_cacheinfo_release;
324 dev->groups = cache_groups;
325 if (dev_set_name(dev, "index%d", cache_attrs->level))
Olivier Deprez0e641232021-09-23 10:07:05 +0200326 goto put_device;
David Brazdil0f672f62019-12-10 10:32:29 +0000327
328 info->cache_attrs = *cache_attrs;
Olivier Deprez0e641232021-09-23 10:07:05 +0200329 if (device_add(dev)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000330 dev_warn(&node->dev, "failed to add cache level:%d\n",
331 cache_attrs->level);
Olivier Deprez0e641232021-09-23 10:07:05 +0200332 goto put_device;
David Brazdil0f672f62019-12-10 10:32:29 +0000333 }
334 pm_runtime_no_callbacks(dev);
335 list_add_tail(&info->node, &node->cache_attrs);
336 return;
Olivier Deprez0e641232021-09-23 10:07:05 +0200337put_device:
338 put_device(dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000339}
340
341static void node_remove_caches(struct node *node)
342{
343 struct node_cache_info *info, *next;
344
345 if (!node->cache_dev)
346 return;
347
348 list_for_each_entry_safe(info, next, &node->cache_attrs, node) {
349 list_del(&info->node);
350 device_unregister(&info->dev);
351 }
352 device_unregister(node->cache_dev);
353}
354
355static void node_init_caches(unsigned int nid)
356{
357 INIT_LIST_HEAD(&node_devices[nid]->cache_attrs);
358}
359#else
360static void node_init_caches(unsigned int nid) { }
361static void node_remove_caches(struct node *node) { }
362#endif
363
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364#define K(x) ((x) << (PAGE_SHIFT - 10))
365static ssize_t node_read_meminfo(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
Olivier Deprez157378f2022-04-04 15:47:50 +0200368 int len = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000369 int nid = dev->id;
370 struct pglist_data *pgdat = NODE_DATA(nid);
371 struct sysinfo i;
David Brazdil0f672f62019-12-10 10:32:29 +0000372 unsigned long sreclaimable, sunreclaimable;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000373
374 si_meminfo_node(&i, nid);
Olivier Deprez157378f2022-04-04 15:47:50 +0200375 sreclaimable = node_page_state_pages(pgdat, NR_SLAB_RECLAIMABLE_B);
376 sunreclaimable = node_page_state_pages(pgdat, NR_SLAB_UNRECLAIMABLE_B);
377 len = sysfs_emit_at(buf, len,
378 "Node %d MemTotal: %8lu kB\n"
379 "Node %d MemFree: %8lu kB\n"
380 "Node %d MemUsed: %8lu kB\n"
381 "Node %d Active: %8lu kB\n"
382 "Node %d Inactive: %8lu kB\n"
383 "Node %d Active(anon): %8lu kB\n"
384 "Node %d Inactive(anon): %8lu kB\n"
385 "Node %d Active(file): %8lu kB\n"
386 "Node %d Inactive(file): %8lu kB\n"
387 "Node %d Unevictable: %8lu kB\n"
388 "Node %d Mlocked: %8lu kB\n",
389 nid, K(i.totalram),
390 nid, K(i.freeram),
391 nid, K(i.totalram - i.freeram),
392 nid, K(node_page_state(pgdat, NR_ACTIVE_ANON) +
393 node_page_state(pgdat, NR_ACTIVE_FILE)),
394 nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) +
395 node_page_state(pgdat, NR_INACTIVE_FILE)),
396 nid, K(node_page_state(pgdat, NR_ACTIVE_ANON)),
397 nid, K(node_page_state(pgdat, NR_INACTIVE_ANON)),
398 nid, K(node_page_state(pgdat, NR_ACTIVE_FILE)),
399 nid, K(node_page_state(pgdat, NR_INACTIVE_FILE)),
400 nid, K(node_page_state(pgdat, NR_UNEVICTABLE)),
401 nid, K(sum_zone_node_page_state(nid, NR_MLOCK)));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000402
403#ifdef CONFIG_HIGHMEM
Olivier Deprez157378f2022-04-04 15:47:50 +0200404 len += sysfs_emit_at(buf, len,
405 "Node %d HighTotal: %8lu kB\n"
406 "Node %d HighFree: %8lu kB\n"
407 "Node %d LowTotal: %8lu kB\n"
408 "Node %d LowFree: %8lu kB\n",
409 nid, K(i.totalhigh),
410 nid, K(i.freehigh),
411 nid, K(i.totalram - i.totalhigh),
412 nid, K(i.freeram - i.freehigh));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000413#endif
Olivier Deprez157378f2022-04-04 15:47:50 +0200414 len += sysfs_emit_at(buf, len,
415 "Node %d Dirty: %8lu kB\n"
416 "Node %d Writeback: %8lu kB\n"
417 "Node %d FilePages: %8lu kB\n"
418 "Node %d Mapped: %8lu kB\n"
419 "Node %d AnonPages: %8lu kB\n"
420 "Node %d Shmem: %8lu kB\n"
421 "Node %d KernelStack: %8lu kB\n"
422#ifdef CONFIG_SHADOW_CALL_STACK
423 "Node %d ShadowCallStack:%8lu kB\n"
424#endif
425 "Node %d PageTables: %8lu kB\n"
426 "Node %d NFS_Unstable: %8lu kB\n"
427 "Node %d Bounce: %8lu kB\n"
428 "Node %d WritebackTmp: %8lu kB\n"
429 "Node %d KReclaimable: %8lu kB\n"
430 "Node %d Slab: %8lu kB\n"
431 "Node %d SReclaimable: %8lu kB\n"
432 "Node %d SUnreclaim: %8lu kB\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000433#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Olivier Deprez157378f2022-04-04 15:47:50 +0200434 "Node %d AnonHugePages: %8lu kB\n"
435 "Node %d ShmemHugePages: %8lu kB\n"
436 "Node %d ShmemPmdMapped: %8lu kB\n"
437 "Node %d FileHugePages: %8lu kB\n"
438 "Node %d FilePmdMapped: %8lu kB\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439#endif
Olivier Deprez157378f2022-04-04 15:47:50 +0200440 ,
441 nid, K(node_page_state(pgdat, NR_FILE_DIRTY)),
442 nid, K(node_page_state(pgdat, NR_WRITEBACK)),
443 nid, K(node_page_state(pgdat, NR_FILE_PAGES)),
444 nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
445 nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
446 nid, K(i.sharedram),
447 nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
448#ifdef CONFIG_SHADOW_CALL_STACK
449 nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
450#endif
451 nid, K(sum_zone_node_page_state(nid, NR_PAGETABLE)),
452 nid, 0UL,
453 nid, K(sum_zone_node_page_state(nid, NR_BOUNCE)),
454 nid, K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
455 nid, K(sreclaimable +
456 node_page_state(pgdat, NR_KERNEL_MISC_RECLAIMABLE)),
457 nid, K(sreclaimable + sunreclaimable),
458 nid, K(sreclaimable),
459 nid, K(sunreclaimable)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000460#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Olivier Deprez157378f2022-04-04 15:47:50 +0200461 ,
462 nid, K(node_page_state(pgdat, NR_ANON_THPS) *
463 HPAGE_PMD_NR),
464 nid, K(node_page_state(pgdat, NR_SHMEM_THPS) *
465 HPAGE_PMD_NR),
466 nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED) *
467 HPAGE_PMD_NR),
468 nid, K(node_page_state(pgdat, NR_FILE_THPS) *
469 HPAGE_PMD_NR),
470 nid, K(node_page_state(pgdat, NR_FILE_PMDMAPPED) *
471 HPAGE_PMD_NR)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000472#endif
Olivier Deprez157378f2022-04-04 15:47:50 +0200473 );
474 len += hugetlb_report_node_meminfo(buf, len, nid);
475 return len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000476}
477
478#undef K
Olivier Deprez157378f2022-04-04 15:47:50 +0200479static DEVICE_ATTR(meminfo, 0444, node_read_meminfo, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000480
481static ssize_t node_read_numastat(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200482 struct device_attribute *attr, char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000483{
Olivier Deprez157378f2022-04-04 15:47:50 +0200484 return sysfs_emit(buf,
485 "numa_hit %lu\n"
486 "numa_miss %lu\n"
487 "numa_foreign %lu\n"
488 "interleave_hit %lu\n"
489 "local_node %lu\n"
490 "other_node %lu\n",
491 sum_zone_numa_state(dev->id, NUMA_HIT),
492 sum_zone_numa_state(dev->id, NUMA_MISS),
493 sum_zone_numa_state(dev->id, NUMA_FOREIGN),
494 sum_zone_numa_state(dev->id, NUMA_INTERLEAVE_HIT),
495 sum_zone_numa_state(dev->id, NUMA_LOCAL),
496 sum_zone_numa_state(dev->id, NUMA_OTHER));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497}
Olivier Deprez157378f2022-04-04 15:47:50 +0200498static DEVICE_ATTR(numastat, 0444, node_read_numastat, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000499
500static ssize_t node_read_vmstat(struct device *dev,
501 struct device_attribute *attr, char *buf)
502{
503 int nid = dev->id;
504 struct pglist_data *pgdat = NODE_DATA(nid);
505 int i;
Olivier Deprez157378f2022-04-04 15:47:50 +0200506 int len = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000507
508 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Olivier Deprez157378f2022-04-04 15:47:50 +0200509 len += sysfs_emit_at(buf, len, "%s %lu\n",
510 zone_stat_name(i),
511 sum_zone_node_page_state(nid, i));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000512
513#ifdef CONFIG_NUMA
514 for (i = 0; i < NR_VM_NUMA_STAT_ITEMS; i++)
Olivier Deprez157378f2022-04-04 15:47:50 +0200515 len += sysfs_emit_at(buf, len, "%s %lu\n",
516 numa_stat_name(i),
517 sum_zone_numa_state(nid, i));
518
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000519#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520 for (i = 0; i < NR_VM_NODE_STAT_ITEMS; i++)
Olivier Deprez157378f2022-04-04 15:47:50 +0200521 len += sysfs_emit_at(buf, len, "%s %lu\n",
522 node_stat_name(i),
523 node_page_state_pages(pgdat, i));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524
Olivier Deprez157378f2022-04-04 15:47:50 +0200525 return len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000526}
Olivier Deprez157378f2022-04-04 15:47:50 +0200527static DEVICE_ATTR(vmstat, 0444, node_read_vmstat, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528
529static ssize_t node_read_distance(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200530 struct device_attribute *attr, char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000531{
532 int nid = dev->id;
533 int len = 0;
534 int i;
535
536 /*
537 * buf is currently PAGE_SIZE in length and each node needs 4 chars
538 * at the most (distance + space or newline).
539 */
540 BUILD_BUG_ON(MAX_NUMNODES * 4 > PAGE_SIZE);
541
Olivier Deprez157378f2022-04-04 15:47:50 +0200542 for_each_online_node(i) {
543 len += sysfs_emit_at(buf, len, "%s%d",
544 i ? " " : "", node_distance(nid, i));
545 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000546
Olivier Deprez157378f2022-04-04 15:47:50 +0200547 len += sysfs_emit_at(buf, len, "\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000548 return len;
549}
Olivier Deprez157378f2022-04-04 15:47:50 +0200550static DEVICE_ATTR(distance, 0444, node_read_distance, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551
552static struct attribute *node_dev_attrs[] = {
553 &dev_attr_cpumap.attr,
554 &dev_attr_cpulist.attr,
555 &dev_attr_meminfo.attr,
556 &dev_attr_numastat.attr,
557 &dev_attr_distance.attr,
558 &dev_attr_vmstat.attr,
559 NULL
560};
561ATTRIBUTE_GROUPS(node_dev);
562
563#ifdef CONFIG_HUGETLBFS
564/*
565 * hugetlbfs per node attributes registration interface:
566 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
567 * it will register its per node attributes for all online nodes with
568 * memory. It will also call register_hugetlbfs_with_node(), below, to
569 * register its attribute registration functions with this node driver.
570 * Once these hooks have been initialized, the node driver will call into
571 * the hugetlb module to [un]register attributes for hot-plugged nodes.
572 */
573static node_registration_func_t __hugetlb_register_node;
574static node_registration_func_t __hugetlb_unregister_node;
575
576static inline bool hugetlb_register_node(struct node *node)
577{
578 if (__hugetlb_register_node &&
579 node_state(node->dev.id, N_MEMORY)) {
580 __hugetlb_register_node(node);
581 return true;
582 }
583 return false;
584}
585
586static inline void hugetlb_unregister_node(struct node *node)
587{
588 if (__hugetlb_unregister_node)
589 __hugetlb_unregister_node(node);
590}
591
592void register_hugetlbfs_with_node(node_registration_func_t doregister,
593 node_registration_func_t unregister)
594{
595 __hugetlb_register_node = doregister;
596 __hugetlb_unregister_node = unregister;
597}
598#else
599static inline void hugetlb_register_node(struct node *node) {}
600
601static inline void hugetlb_unregister_node(struct node *node) {}
602#endif
603
604static void node_device_release(struct device *dev)
605{
606 struct node *node = to_node(dev);
607
608#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HUGETLBFS)
609 /*
610 * We schedule the work only when a memory section is
611 * onlined/offlined on this node. When we come here,
612 * all the memory on this node has been offlined,
613 * so we won't enqueue new work to this work.
614 *
615 * The work is using node->node_work, so we should
616 * flush work before freeing the memory.
617 */
618 flush_work(&node->node_work);
619#endif
620 kfree(node);
621}
622
623/*
624 * register_node - Setup a sysfs device for a node.
625 * @num - Node number to use when creating the device.
626 *
627 * Initialize and register the node device.
628 */
629static int register_node(struct node *node, int num)
630{
631 int error;
632
633 node->dev.id = num;
634 node->dev.bus = &node_subsys;
635 node->dev.release = node_device_release;
636 node->dev.groups = node_dev_groups;
637 error = device_register(&node->dev);
638
639 if (error)
640 put_device(&node->dev);
641 else {
642 hugetlb_register_node(node);
643
644 compaction_register_node(node);
645 }
646 return error;
647}
648
649/**
650 * unregister_node - unregister a node device
651 * @node: node going away
652 *
653 * Unregisters a node device @node. All the devices on the node must be
654 * unregistered before calling this function.
655 */
656void unregister_node(struct node *node)
657{
658 hugetlb_unregister_node(node); /* no-op, if memoryless node */
David Brazdil0f672f62019-12-10 10:32:29 +0000659 node_remove_accesses(node);
660 node_remove_caches(node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000661 device_unregister(&node->dev);
662}
663
664struct node *node_devices[MAX_NUMNODES];
665
666/*
667 * register cpu under node
668 */
669int register_cpu_under_node(unsigned int cpu, unsigned int nid)
670{
671 int ret;
672 struct device *obj;
673
674 if (!node_online(nid))
675 return 0;
676
677 obj = get_cpu_device(cpu);
678 if (!obj)
679 return 0;
680
681 ret = sysfs_create_link(&node_devices[nid]->dev.kobj,
682 &obj->kobj,
683 kobject_name(&obj->kobj));
684 if (ret)
685 return ret;
686
687 return sysfs_create_link(&obj->kobj,
688 &node_devices[nid]->dev.kobj,
689 kobject_name(&node_devices[nid]->dev.kobj));
690}
691
David Brazdil0f672f62019-12-10 10:32:29 +0000692/**
693 * register_memory_node_under_compute_node - link memory node to its compute
694 * node for a given access class.
695 * @mem_nid: Memory node number
696 * @cpu_nid: Cpu node number
697 * @access: Access class to register
698 *
699 * Description:
700 * For use with platforms that may have separate memory and compute nodes.
701 * This function will export node relationships linking which memory
702 * initiator nodes can access memory targets at a given ranked access
703 * class.
704 */
705int register_memory_node_under_compute_node(unsigned int mem_nid,
706 unsigned int cpu_nid,
707 unsigned access)
708{
709 struct node *init_node, *targ_node;
710 struct node_access_nodes *initiator, *target;
711 int ret;
712
713 if (!node_online(cpu_nid) || !node_online(mem_nid))
714 return -ENODEV;
715
716 init_node = node_devices[cpu_nid];
717 targ_node = node_devices[mem_nid];
718 initiator = node_init_node_access(init_node, access);
719 target = node_init_node_access(targ_node, access);
720 if (!initiator || !target)
721 return -ENOMEM;
722
723 ret = sysfs_add_link_to_group(&initiator->dev.kobj, "targets",
724 &targ_node->dev.kobj,
725 dev_name(&targ_node->dev));
726 if (ret)
727 return ret;
728
729 ret = sysfs_add_link_to_group(&target->dev.kobj, "initiators",
730 &init_node->dev.kobj,
731 dev_name(&init_node->dev));
732 if (ret)
733 goto err;
734
735 return 0;
736 err:
737 sysfs_remove_link_from_group(&initiator->dev.kobj, "targets",
738 dev_name(&targ_node->dev));
739 return ret;
740}
741
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000742int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
743{
744 struct device *obj;
745
746 if (!node_online(nid))
747 return 0;
748
749 obj = get_cpu_device(cpu);
750 if (!obj)
751 return 0;
752
753 sysfs_remove_link(&node_devices[nid]->dev.kobj,
754 kobject_name(&obj->kobj));
755 sysfs_remove_link(&obj->kobj,
756 kobject_name(&node_devices[nid]->dev.kobj));
757
758 return 0;
759}
760
761#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
762static int __ref get_nid_for_pfn(unsigned long pfn)
763{
764 if (!pfn_valid_within(pfn))
765 return -1;
766#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
767 if (system_state < SYSTEM_RUNNING)
768 return early_pfn_to_nid(pfn);
769#endif
770 return pfn_to_nid(pfn);
771}
772
Olivier Deprez157378f2022-04-04 15:47:50 +0200773static void do_register_memory_block_under_node(int nid,
774 struct memory_block *mem_blk)
Olivier Deprez0e641232021-09-23 10:07:05 +0200775{
776 int ret;
777
778 /*
779 * If this memory block spans multiple nodes, we only indicate
780 * the last processed node.
781 */
782 mem_blk->nid = nid;
783
784 ret = sysfs_create_link_nowarn(&node_devices[nid]->dev.kobj,
785 &mem_blk->dev.kobj,
786 kobject_name(&mem_blk->dev.kobj));
Olivier Deprez157378f2022-04-04 15:47:50 +0200787 if (ret && ret != -EEXIST)
788 dev_err_ratelimited(&node_devices[nid]->dev,
789 "can't create link to %s in sysfs (%d)\n",
790 kobject_name(&mem_blk->dev.kobj), ret);
Olivier Deprez0e641232021-09-23 10:07:05 +0200791
Olivier Deprez157378f2022-04-04 15:47:50 +0200792 ret = sysfs_create_link_nowarn(&mem_blk->dev.kobj,
Olivier Deprez0e641232021-09-23 10:07:05 +0200793 &node_devices[nid]->dev.kobj,
794 kobject_name(&node_devices[nid]->dev.kobj));
Olivier Deprez157378f2022-04-04 15:47:50 +0200795 if (ret && ret != -EEXIST)
796 dev_err_ratelimited(&mem_blk->dev,
797 "can't create link to %s in sysfs (%d)\n",
798 kobject_name(&node_devices[nid]->dev.kobj),
799 ret);
Olivier Deprez0e641232021-09-23 10:07:05 +0200800}
801
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000802/* register memory section under specified node if it spans that node */
Olivier Deprez0e641232021-09-23 10:07:05 +0200803static int register_mem_block_under_node_early(struct memory_block *mem_blk,
804 void *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000805{
David Brazdil0f672f62019-12-10 10:32:29 +0000806 unsigned long memory_block_pfns = memory_block_size_bytes() / PAGE_SIZE;
807 unsigned long start_pfn = section_nr_to_pfn(mem_blk->start_section_nr);
808 unsigned long end_pfn = start_pfn + memory_block_pfns - 1;
Olivier Deprez0e641232021-09-23 10:07:05 +0200809 int nid = *(int *)arg;
David Brazdil0f672f62019-12-10 10:32:29 +0000810 unsigned long pfn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000811
David Brazdil0f672f62019-12-10 10:32:29 +0000812 for (pfn = start_pfn; pfn <= end_pfn; pfn++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000813 int page_nid;
814
815 /*
816 * memory block could have several absent sections from start.
817 * skip pfn range from absent section
818 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200819 if (!pfn_in_present_section(pfn)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000820 pfn = round_down(pfn + PAGES_PER_SECTION,
821 PAGES_PER_SECTION) - 1;
822 continue;
823 }
824
825 /*
Olivier Deprez0e641232021-09-23 10:07:05 +0200826 * We need to check if page belongs to nid only at the boot
827 * case because node's ranges can be interleaved.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000828 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200829 page_nid = get_nid_for_pfn(pfn);
830 if (page_nid < 0)
831 continue;
832 if (page_nid != nid)
833 continue;
David Brazdil0f672f62019-12-10 10:32:29 +0000834
Olivier Deprez157378f2022-04-04 15:47:50 +0200835 do_register_memory_block_under_node(nid, mem_blk);
836 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000837 }
838 /* mem section does not span the specified node */
839 return 0;
840}
841
David Brazdil0f672f62019-12-10 10:32:29 +0000842/*
Olivier Deprez0e641232021-09-23 10:07:05 +0200843 * During hotplug we know that all pages in the memory block belong to the same
844 * node.
845 */
846static int register_mem_block_under_node_hotplug(struct memory_block *mem_blk,
847 void *arg)
848{
849 int nid = *(int *)arg;
850
Olivier Deprez157378f2022-04-04 15:47:50 +0200851 do_register_memory_block_under_node(nid, mem_blk);
852 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +0200853}
854
855/*
David Brazdil0f672f62019-12-10 10:32:29 +0000856 * Unregister a memory block device under the node it spans. Memory blocks
857 * with multiple nodes cannot be offlined and therefore also never be removed.
858 */
859void unregister_memory_block_under_nodes(struct memory_block *mem_blk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000860{
David Brazdil0f672f62019-12-10 10:32:29 +0000861 if (mem_blk->nid == NUMA_NO_NODE)
862 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000863
David Brazdil0f672f62019-12-10 10:32:29 +0000864 sysfs_remove_link(&node_devices[mem_blk->nid]->dev.kobj,
865 kobject_name(&mem_blk->dev.kobj));
866 sysfs_remove_link(&mem_blk->dev.kobj,
867 kobject_name(&node_devices[mem_blk->nid]->dev.kobj));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000868}
869
Olivier Deprez157378f2022-04-04 15:47:50 +0200870void link_mem_sections(int nid, unsigned long start_pfn, unsigned long end_pfn,
871 enum meminit_context context)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000872{
Olivier Deprez0e641232021-09-23 10:07:05 +0200873 walk_memory_blocks_func_t func;
874
875 if (context == MEMINIT_HOTPLUG)
876 func = register_mem_block_under_node_hotplug;
877 else
878 func = register_mem_block_under_node_early;
879
Olivier Deprez157378f2022-04-04 15:47:50 +0200880 walk_memory_blocks(PFN_PHYS(start_pfn), PFN_PHYS(end_pfn - start_pfn),
881 (void *)&nid, func);
882 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000883}
884
885#ifdef CONFIG_HUGETLBFS
886/*
887 * Handle per node hstate attribute [un]registration on transistions
888 * to/from memoryless state.
889 */
890static void node_hugetlb_work(struct work_struct *work)
891{
892 struct node *node = container_of(work, struct node, node_work);
893
894 /*
895 * We only get here when a node transitions to/from memoryless state.
896 * We can detect which transition occurred by examining whether the
897 * node has memory now. hugetlb_register_node() already check this
898 * so we try to register the attributes. If that fails, then the
899 * node has transitioned to memoryless, try to unregister the
900 * attributes.
901 */
902 if (!hugetlb_register_node(node))
903 hugetlb_unregister_node(node);
904}
905
906static void init_node_hugetlb_work(int nid)
907{
908 INIT_WORK(&node_devices[nid]->node_work, node_hugetlb_work);
909}
910
911static int node_memory_callback(struct notifier_block *self,
912 unsigned long action, void *arg)
913{
914 struct memory_notify *mnb = arg;
915 int nid = mnb->status_change_nid;
916
917 switch (action) {
918 case MEM_ONLINE:
919 case MEM_OFFLINE:
920 /*
921 * offload per node hstate [un]registration to a work thread
922 * when transitioning to/from memoryless state.
923 */
924 if (nid != NUMA_NO_NODE)
925 schedule_work(&node_devices[nid]->node_work);
926 break;
927
928 case MEM_GOING_ONLINE:
929 case MEM_GOING_OFFLINE:
930 case MEM_CANCEL_ONLINE:
931 case MEM_CANCEL_OFFLINE:
932 default:
933 break;
934 }
935
936 return NOTIFY_OK;
937}
938#endif /* CONFIG_HUGETLBFS */
939#endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
940
941#if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
942 !defined(CONFIG_HUGETLBFS)
943static inline int node_memory_callback(struct notifier_block *self,
944 unsigned long action, void *arg)
945{
946 return NOTIFY_OK;
947}
948
949static void init_node_hugetlb_work(int nid) { }
950
951#endif
952
953int __register_one_node(int nid)
954{
955 int error;
956 int cpu;
957
958 node_devices[nid] = kzalloc(sizeof(struct node), GFP_KERNEL);
959 if (!node_devices[nid])
960 return -ENOMEM;
961
962 error = register_node(node_devices[nid], nid);
963
964 /* link cpu under this node */
965 for_each_present_cpu(cpu) {
966 if (cpu_to_node(cpu) == nid)
967 register_cpu_under_node(cpu, nid);
968 }
969
David Brazdil0f672f62019-12-10 10:32:29 +0000970 INIT_LIST_HEAD(&node_devices[nid]->access_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000971 /* initialize work queue for memory hot plug */
972 init_node_hugetlb_work(nid);
David Brazdil0f672f62019-12-10 10:32:29 +0000973 node_init_caches(nid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000974
975 return error;
976}
977
978void unregister_one_node(int nid)
979{
980 if (!node_devices[nid])
981 return;
982
983 unregister_node(node_devices[nid]);
984 node_devices[nid] = NULL;
985}
986
987/*
988 * node states attributes
989 */
990
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000991struct node_attr {
992 struct device_attribute attr;
993 enum node_states state;
994};
995
996static ssize_t show_node_state(struct device *dev,
997 struct device_attribute *attr, char *buf)
998{
999 struct node_attr *na = container_of(attr, struct node_attr, attr);
Olivier Deprez157378f2022-04-04 15:47:50 +02001000
1001 return sysfs_emit(buf, "%*pbl\n",
1002 nodemask_pr_args(&node_states[na->state]));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001003}
1004
1005#define _NODE_ATTR(name, state) \
1006 { __ATTR(name, 0444, show_node_state, NULL), state }
1007
1008static struct node_attr node_state_attr[] = {
1009 [N_POSSIBLE] = _NODE_ATTR(possible, N_POSSIBLE),
1010 [N_ONLINE] = _NODE_ATTR(online, N_ONLINE),
1011 [N_NORMAL_MEMORY] = _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY),
1012#ifdef CONFIG_HIGHMEM
1013 [N_HIGH_MEMORY] = _NODE_ATTR(has_high_memory, N_HIGH_MEMORY),
1014#endif
1015 [N_MEMORY] = _NODE_ATTR(has_memory, N_MEMORY),
1016 [N_CPU] = _NODE_ATTR(has_cpu, N_CPU),
Olivier Deprez157378f2022-04-04 15:47:50 +02001017 [N_GENERIC_INITIATOR] = _NODE_ATTR(has_generic_initiator,
1018 N_GENERIC_INITIATOR),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019};
1020
1021static struct attribute *node_state_attrs[] = {
1022 &node_state_attr[N_POSSIBLE].attr.attr,
1023 &node_state_attr[N_ONLINE].attr.attr,
1024 &node_state_attr[N_NORMAL_MEMORY].attr.attr,
1025#ifdef CONFIG_HIGHMEM
1026 &node_state_attr[N_HIGH_MEMORY].attr.attr,
1027#endif
1028 &node_state_attr[N_MEMORY].attr.attr,
1029 &node_state_attr[N_CPU].attr.attr,
Olivier Deprez157378f2022-04-04 15:47:50 +02001030 &node_state_attr[N_GENERIC_INITIATOR].attr.attr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001031 NULL
1032};
1033
1034static struct attribute_group memory_root_attr_group = {
1035 .attrs = node_state_attrs,
1036};
1037
1038static const struct attribute_group *cpu_root_attr_groups[] = {
1039 &memory_root_attr_group,
1040 NULL,
1041};
1042
1043#define NODE_CALLBACK_PRI 2 /* lower than SLAB */
1044static int __init register_node_type(void)
1045{
1046 int ret;
1047
1048 BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES);
1049 BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES);
1050
1051 ret = subsys_system_register(&node_subsys, cpu_root_attr_groups);
1052 if (!ret) {
1053 static struct notifier_block node_memory_callback_nb = {
1054 .notifier_call = node_memory_callback,
1055 .priority = NODE_CALLBACK_PRI,
1056 };
1057 register_hotmemory_notifier(&node_memory_callback_nb);
1058 }
1059
1060 /*
1061 * Note: we're not going to unregister the node class if we fail
1062 * to register the node state class attribute files.
1063 */
1064 return ret;
1065}
1066postcore_initcall(register_node_type);