2 * drivers/base/node.c - basic Node class support
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <linux/memory.h>
10 #include <linux/node.h>
11 #include <linux/hugetlb.h>
12 #include <linux/compaction.h>
13 #include <linux/cpumask.h>
14 #include <linux/topology.h>
15 #include <linux/nodemask.h>
16 #include <linux/cpu.h>
17 #include <linux/device.h>
18 #include <linux/swap.h>
19 #include <linux/slab.h>
21 static struct sysdev_class_attribute
*node_state_attrs
[];
23 static struct sysdev_class node_class
= {
25 .attrs
= node_state_attrs
,
29 static ssize_t
node_read_cpumap(struct sys_device
*dev
, int type
, char *buf
)
31 struct node
*node_dev
= to_node(dev
);
32 const struct cpumask
*mask
= cpumask_of_node(node_dev
->sysdev
.id
);
35 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
36 BUILD_BUG_ON((NR_CPUS
/32 * 9) > (PAGE_SIZE
-1));
39 cpulist_scnprintf(buf
, PAGE_SIZE
-2, mask
) :
40 cpumask_scnprintf(buf
, PAGE_SIZE
-2, mask
);
46 static inline ssize_t
node_read_cpumask(struct sys_device
*dev
,
47 struct sysdev_attribute
*attr
, char *buf
)
49 return node_read_cpumap(dev
, 0, buf
);
51 static inline ssize_t
node_read_cpulist(struct sys_device
*dev
,
52 struct sysdev_attribute
*attr
, char *buf
)
54 return node_read_cpumap(dev
, 1, buf
);
57 static SYSDEV_ATTR(cpumap
, S_IRUGO
, node_read_cpumask
, NULL
);
58 static SYSDEV_ATTR(cpulist
, S_IRUGO
, node_read_cpulist
, NULL
);
60 #define K(x) ((x) << (PAGE_SHIFT - 10))
61 static ssize_t
node_read_meminfo(struct sys_device
* dev
,
62 struct sysdev_attribute
*attr
, char * buf
)
68 si_meminfo_node(&i
, nid
);
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",
83 nid
, K(i
.totalram
- i
.freeram
),
84 nid
, K(node_page_state(nid
, NR_ACTIVE_ANON
) +
85 node_page_state(nid
, NR_ACTIVE_FILE
)),
86 nid
, K(node_page_state(nid
, NR_INACTIVE_ANON
) +
87 node_page_state(nid
, NR_INACTIVE_FILE
)),
88 nid
, K(node_page_state(nid
, NR_ACTIVE_ANON
)),
89 nid
, K(node_page_state(nid
, NR_INACTIVE_ANON
)),
90 nid
, K(node_page_state(nid
, NR_ACTIVE_FILE
)),
91 nid
, K(node_page_state(nid
, NR_INACTIVE_FILE
)),
92 nid
, K(node_page_state(nid
, NR_UNEVICTABLE
)),
93 nid
, K(node_page_state(nid
, NR_MLOCK
)));
97 "Node %d HighTotal: %8lu kB\n"
98 "Node %d HighFree: %8lu kB\n"
99 "Node %d LowTotal: %8lu kB\n"
100 "Node %d LowFree: %8lu kB\n",
103 nid
, K(i
.totalram
- i
.totalhigh
),
104 nid
, K(i
.freeram
- i
.freehigh
));
106 n
+= sprintf(buf
+ n
,
107 "Node %d Dirty: %8lu kB\n"
108 "Node %d Writeback: %8lu kB\n"
109 "Node %d FilePages: %8lu kB\n"
110 "Node %d Mapped: %8lu kB\n"
111 "Node %d AnonPages: %8lu kB\n"
112 "Node %d Shmem: %8lu kB\n"
113 "Node %d KernelStack: %8lu kB\n"
114 "Node %d PageTables: %8lu kB\n"
115 "Node %d NFS_Unstable: %8lu kB\n"
116 "Node %d Bounce: %8lu kB\n"
117 "Node %d WritebackTmp: %8lu kB\n"
118 "Node %d Slab: %8lu kB\n"
119 "Node %d SReclaimable: %8lu kB\n"
120 "Node %d SUnreclaim: %8lu kB\n"
121 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
122 "Node %d AnonHugePages: %8lu kB\n"
125 nid
, K(node_page_state(nid
, NR_FILE_DIRTY
)),
126 nid
, K(node_page_state(nid
, NR_WRITEBACK
)),
127 nid
, K(node_page_state(nid
, NR_FILE_PAGES
)),
128 nid
, K(node_page_state(nid
, NR_FILE_MAPPED
)),
129 nid
, K(node_page_state(nid
, NR_ANON_PAGES
)
130 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
131 + node_page_state(nid
, NR_ANON_TRANSPARENT_HUGEPAGES
) *
135 nid
, K(node_page_state(nid
, NR_SHMEM
)),
136 nid
, node_page_state(nid
, NR_KERNEL_STACK
) *
138 nid
, K(node_page_state(nid
, NR_PAGETABLE
)),
139 nid
, K(node_page_state(nid
, NR_UNSTABLE_NFS
)),
140 nid
, K(node_page_state(nid
, NR_BOUNCE
)),
141 nid
, K(node_page_state(nid
, NR_WRITEBACK_TEMP
)),
142 nid
, K(node_page_state(nid
, NR_SLAB_RECLAIMABLE
) +
143 node_page_state(nid
, NR_SLAB_UNRECLAIMABLE
)),
144 nid
, K(node_page_state(nid
, NR_SLAB_RECLAIMABLE
)),
145 nid
, K(node_page_state(nid
, NR_SLAB_UNRECLAIMABLE
))
146 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
148 K(node_page_state(nid
, NR_ANON_TRANSPARENT_HUGEPAGES
) *
152 n
+= hugetlb_report_node_meminfo(nid
, buf
+ n
);
157 static SYSDEV_ATTR(meminfo
, S_IRUGO
, node_read_meminfo
, NULL
);
159 static ssize_t
node_read_numastat(struct sys_device
* dev
,
160 struct sysdev_attribute
*attr
, char * buf
)
166 "interleave_hit %lu\n"
169 node_page_state(dev
->id
, NUMA_HIT
),
170 node_page_state(dev
->id
, NUMA_MISS
),
171 node_page_state(dev
->id
, NUMA_FOREIGN
),
172 node_page_state(dev
->id
, NUMA_INTERLEAVE_HIT
),
173 node_page_state(dev
->id
, NUMA_LOCAL
),
174 node_page_state(dev
->id
, NUMA_OTHER
));
176 static SYSDEV_ATTR(numastat
, S_IRUGO
, node_read_numastat
, NULL
);
178 static ssize_t
node_read_vmstat(struct sys_device
*dev
,
179 struct sysdev_attribute
*attr
, char *buf
)
185 node_page_state(nid
, NR_WRITTEN
),
186 node_page_state(nid
, NR_DIRTIED
));
188 static SYSDEV_ATTR(vmstat
, S_IRUGO
, node_read_vmstat
, NULL
);
190 static ssize_t
node_read_distance(struct sys_device
* dev
,
191 struct sysdev_attribute
*attr
, char * buf
)
198 * buf is currently PAGE_SIZE in length and each node needs 4 chars
199 * at the most (distance + space or newline).
201 BUILD_BUG_ON(MAX_NUMNODES
* 4 > PAGE_SIZE
);
203 for_each_online_node(i
)
204 len
+= sprintf(buf
+ len
, "%s%d", i
? " " : "", node_distance(nid
, i
));
206 len
+= sprintf(buf
+ len
, "\n");
209 static SYSDEV_ATTR(distance
, S_IRUGO
, node_read_distance
, NULL
);
211 #ifdef CONFIG_HUGETLBFS
213 * hugetlbfs per node attributes registration interface:
214 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
215 * it will register its per node attributes for all online nodes with
216 * memory. It will also call register_hugetlbfs_with_node(), below, to
217 * register its attribute registration functions with this node driver.
218 * Once these hooks have been initialized, the node driver will call into
219 * the hugetlb module to [un]register attributes for hot-plugged nodes.
221 static node_registration_func_t __hugetlb_register_node
;
222 static node_registration_func_t __hugetlb_unregister_node
;
224 static inline bool hugetlb_register_node(struct node
*node
)
226 if (__hugetlb_register_node
&&
227 node_state(node
->sysdev
.id
, N_HIGH_MEMORY
)) {
228 __hugetlb_register_node(node
);
234 static inline void hugetlb_unregister_node(struct node
*node
)
236 if (__hugetlb_unregister_node
)
237 __hugetlb_unregister_node(node
);
240 void register_hugetlbfs_with_node(node_registration_func_t doregister
,
241 node_registration_func_t unregister
)
243 __hugetlb_register_node
= doregister
;
244 __hugetlb_unregister_node
= unregister
;
247 static inline void hugetlb_register_node(struct node
*node
) {}
249 static inline void hugetlb_unregister_node(struct node
*node
) {}
254 * register_node - Setup a sysfs device for a node.
255 * @num - Node number to use when creating the device.
257 * Initialize and register the node device.
259 int register_node(struct node
*node
, int num
, struct node
*parent
)
263 node
->sysdev
.id
= num
;
264 node
->sysdev
.cls
= &node_class
;
265 error
= sysdev_register(&node
->sysdev
);
268 sysdev_create_file(&node
->sysdev
, &attr_cpumap
);
269 sysdev_create_file(&node
->sysdev
, &attr_cpulist
);
270 sysdev_create_file(&node
->sysdev
, &attr_meminfo
);
271 sysdev_create_file(&node
->sysdev
, &attr_numastat
);
272 sysdev_create_file(&node
->sysdev
, &attr_distance
);
273 sysdev_create_file(&node
->sysdev
, &attr_vmstat
);
275 scan_unevictable_register_node(node
);
277 hugetlb_register_node(node
);
279 compaction_register_node(node
);
285 * unregister_node - unregister a node device
286 * @node: node going away
288 * Unregisters a node device @node. All the devices on the node must be
289 * unregistered before calling this function.
291 void unregister_node(struct node
*node
)
293 sysdev_remove_file(&node
->sysdev
, &attr_cpumap
);
294 sysdev_remove_file(&node
->sysdev
, &attr_cpulist
);
295 sysdev_remove_file(&node
->sysdev
, &attr_meminfo
);
296 sysdev_remove_file(&node
->sysdev
, &attr_numastat
);
297 sysdev_remove_file(&node
->sysdev
, &attr_distance
);
298 sysdev_remove_file(&node
->sysdev
, &attr_vmstat
);
300 scan_unevictable_unregister_node(node
);
301 hugetlb_unregister_node(node
); /* no-op, if memoryless node */
303 sysdev_unregister(&node
->sysdev
);
306 struct node node_devices
[MAX_NUMNODES
];
309 * register cpu under node
311 int register_cpu_under_node(unsigned int cpu
, unsigned int nid
)
314 struct sys_device
*obj
;
316 if (!node_online(nid
))
319 obj
= get_cpu_sysdev(cpu
);
323 ret
= sysfs_create_link(&node_devices
[nid
].sysdev
.kobj
,
325 kobject_name(&obj
->kobj
));
329 return sysfs_create_link(&obj
->kobj
,
330 &node_devices
[nid
].sysdev
.kobj
,
331 kobject_name(&node_devices
[nid
].sysdev
.kobj
));
334 int unregister_cpu_under_node(unsigned int cpu
, unsigned int nid
)
336 struct sys_device
*obj
;
338 if (!node_online(nid
))
341 obj
= get_cpu_sysdev(cpu
);
345 sysfs_remove_link(&node_devices
[nid
].sysdev
.kobj
,
346 kobject_name(&obj
->kobj
));
347 sysfs_remove_link(&obj
->kobj
,
348 kobject_name(&node_devices
[nid
].sysdev
.kobj
));
353 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
354 #define page_initialized(page) (page->lru.next)
356 static int get_nid_for_pfn(unsigned long pfn
)
360 if (!pfn_valid_within(pfn
))
362 page
= pfn_to_page(pfn
);
363 if (!page_initialized(page
))
365 return pfn_to_nid(pfn
);
368 /* register memory section under specified node if it spans that node */
369 int register_mem_sect_under_node(struct memory_block
*mem_blk
, int nid
)
372 unsigned long pfn
, sect_start_pfn
, sect_end_pfn
;
376 if (!node_online(nid
))
379 sect_start_pfn
= section_nr_to_pfn(mem_blk
->start_section_nr
);
380 sect_end_pfn
= section_nr_to_pfn(mem_blk
->end_section_nr
);
381 sect_end_pfn
+= PAGES_PER_SECTION
- 1;
382 for (pfn
= sect_start_pfn
; pfn
<= sect_end_pfn
; pfn
++) {
385 page_nid
= get_nid_for_pfn(pfn
);
390 ret
= sysfs_create_link_nowarn(&node_devices
[nid
].sysdev
.kobj
,
391 &mem_blk
->sysdev
.kobj
,
392 kobject_name(&mem_blk
->sysdev
.kobj
));
396 return sysfs_create_link_nowarn(&mem_blk
->sysdev
.kobj
,
397 &node_devices
[nid
].sysdev
.kobj
,
398 kobject_name(&node_devices
[nid
].sysdev
.kobj
));
400 /* mem section does not span the specified node */
404 /* unregister memory section under all nodes that it spans */
405 int unregister_mem_sect_under_nodes(struct memory_block
*mem_blk
,
406 unsigned long phys_index
)
408 NODEMASK_ALLOC(nodemask_t
, unlinked_nodes
, GFP_KERNEL
);
409 unsigned long pfn
, sect_start_pfn
, sect_end_pfn
;
412 NODEMASK_FREE(unlinked_nodes
);
417 nodes_clear(*unlinked_nodes
);
419 sect_start_pfn
= section_nr_to_pfn(phys_index
);
420 sect_end_pfn
= sect_start_pfn
+ PAGES_PER_SECTION
- 1;
421 for (pfn
= sect_start_pfn
; pfn
<= sect_end_pfn
; pfn
++) {
424 nid
= get_nid_for_pfn(pfn
);
427 if (!node_online(nid
))
429 if (node_test_and_set(nid
, *unlinked_nodes
))
431 sysfs_remove_link(&node_devices
[nid
].sysdev
.kobj
,
432 kobject_name(&mem_blk
->sysdev
.kobj
));
433 sysfs_remove_link(&mem_blk
->sysdev
.kobj
,
434 kobject_name(&node_devices
[nid
].sysdev
.kobj
));
436 NODEMASK_FREE(unlinked_nodes
);
440 static int link_mem_sections(int nid
)
442 unsigned long start_pfn
= NODE_DATA(nid
)->node_start_pfn
;
443 unsigned long end_pfn
= start_pfn
+ NODE_DATA(nid
)->node_spanned_pages
;
445 struct memory_block
*mem_blk
= NULL
;
448 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
449 unsigned long section_nr
= pfn_to_section_nr(pfn
);
450 struct mem_section
*mem_sect
;
453 if (!present_section_nr(section_nr
))
455 mem_sect
= __nr_to_section(section_nr
);
456 mem_blk
= find_memory_block_hinted(mem_sect
, mem_blk
);
457 ret
= register_mem_sect_under_node(mem_blk
, nid
);
461 /* discard ref obtained in find_memory_block() */
465 kobject_put(&mem_blk
->sysdev
.kobj
);
469 #ifdef CONFIG_HUGETLBFS
471 * Handle per node hstate attribute [un]registration on transistions
472 * to/from memoryless state.
474 static void node_hugetlb_work(struct work_struct
*work
)
476 struct node
*node
= container_of(work
, struct node
, node_work
);
479 * We only get here when a node transitions to/from memoryless state.
480 * We can detect which transition occurred by examining whether the
481 * node has memory now. hugetlb_register_node() already check this
482 * so we try to register the attributes. If that fails, then the
483 * node has transitioned to memoryless, try to unregister the
486 if (!hugetlb_register_node(node
))
487 hugetlb_unregister_node(node
);
490 static void init_node_hugetlb_work(int nid
)
492 INIT_WORK(&node_devices
[nid
].node_work
, node_hugetlb_work
);
495 static int node_memory_callback(struct notifier_block
*self
,
496 unsigned long action
, void *arg
)
498 struct memory_notify
*mnb
= arg
;
499 int nid
= mnb
->status_change_nid
;
505 * offload per node hstate [un]registration to a work thread
506 * when transitioning to/from memoryless state.
508 if (nid
!= NUMA_NO_NODE
)
509 schedule_work(&node_devices
[nid
].node_work
);
512 case MEM_GOING_ONLINE
:
513 case MEM_GOING_OFFLINE
:
514 case MEM_CANCEL_ONLINE
:
515 case MEM_CANCEL_OFFLINE
:
522 #endif /* CONFIG_HUGETLBFS */
523 #else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
525 static int link_mem_sections(int nid
) { return 0; }
526 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
528 #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
529 !defined(CONFIG_HUGETLBFS)
530 static inline int node_memory_callback(struct notifier_block
*self
,
531 unsigned long action
, void *arg
)
536 static void init_node_hugetlb_work(int nid
) { }
540 int register_one_node(int nid
)
545 if (node_online(nid
)) {
546 int p_node
= parent_node(nid
);
547 struct node
*parent
= NULL
;
550 parent
= &node_devices
[p_node
];
552 error
= register_node(&node_devices
[nid
], nid
, parent
);
554 /* link cpu under this node */
555 for_each_present_cpu(cpu
) {
556 if (cpu_to_node(cpu
) == nid
)
557 register_cpu_under_node(cpu
, nid
);
560 /* link memory sections under this node */
561 error
= link_mem_sections(nid
);
563 /* initialize work queue for memory hot plug */
564 init_node_hugetlb_work(nid
);
571 void unregister_one_node(int nid
)
573 unregister_node(&node_devices
[nid
]);
577 * node states attributes
580 static ssize_t
print_nodes_state(enum node_states state
, char *buf
)
584 n
= nodelist_scnprintf(buf
, PAGE_SIZE
, node_states
[state
]);
585 if (n
> 0 && PAGE_SIZE
> n
+ 1) {
593 struct sysdev_class_attribute attr
;
594 enum node_states state
;
597 static ssize_t
show_node_state(struct sysdev_class
*class,
598 struct sysdev_class_attribute
*attr
, char *buf
)
600 struct node_attr
*na
= container_of(attr
, struct node_attr
, attr
);
601 return print_nodes_state(na
->state
, buf
);
604 #define _NODE_ATTR(name, state) \
605 { _SYSDEV_CLASS_ATTR(name, 0444, show_node_state, NULL), state }
607 static struct node_attr node_state_attr
[] = {
608 _NODE_ATTR(possible
, N_POSSIBLE
),
609 _NODE_ATTR(online
, N_ONLINE
),
610 _NODE_ATTR(has_normal_memory
, N_NORMAL_MEMORY
),
611 _NODE_ATTR(has_cpu
, N_CPU
),
612 #ifdef CONFIG_HIGHMEM
613 _NODE_ATTR(has_high_memory
, N_HIGH_MEMORY
),
617 static struct sysdev_class_attribute
*node_state_attrs
[] = {
618 &node_state_attr
[0].attr
,
619 &node_state_attr
[1].attr
,
620 &node_state_attr
[2].attr
,
621 &node_state_attr
[3].attr
,
622 #ifdef CONFIG_HIGHMEM
623 &node_state_attr
[4].attr
,
628 #define NODE_CALLBACK_PRI 2 /* lower than SLAB */
629 static int __init
register_node_type(void)
633 BUILD_BUG_ON(ARRAY_SIZE(node_state_attr
) != NR_NODE_STATES
);
634 BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs
)-1 != NR_NODE_STATES
);
636 ret
= sysdev_class_register(&node_class
);
638 hotplug_memory_notifier(node_memory_callback
,
643 * Note: we're not going to unregister the node class if we fail
644 * to register the node state class attribute files.
648 postcore_initcall(register_node_type
);