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