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