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 nid
, K(node_page_state(nid
, NR_FILE_DIRTY
)),
122 nid
, K(node_page_state(nid
, NR_WRITEBACK
)),
123 nid
, K(node_page_state(nid
, NR_FILE_PAGES
)),
124 nid
, K(node_page_state(nid
, NR_FILE_MAPPED
)),
125 nid
, K(node_page_state(nid
, NR_ANON_PAGES
)),
126 nid
, K(node_page_state(nid
, NR_SHMEM
)),
127 nid
, node_page_state(nid
, NR_KERNEL_STACK
) *
129 nid
, K(node_page_state(nid
, NR_PAGETABLE
)),
130 nid
, K(node_page_state(nid
, NR_UNSTABLE_NFS
)),
131 nid
, K(node_page_state(nid
, NR_BOUNCE
)),
132 nid
, K(node_page_state(nid
, NR_WRITEBACK_TEMP
)),
133 nid
, K(node_page_state(nid
, NR_SLAB_RECLAIMABLE
) +
134 node_page_state(nid
, NR_SLAB_UNRECLAIMABLE
)),
135 nid
, K(node_page_state(nid
, NR_SLAB_RECLAIMABLE
)),
136 nid
, K(node_page_state(nid
, NR_SLAB_UNRECLAIMABLE
)));
137 n
+= hugetlb_report_node_meminfo(nid
, buf
+ n
);
142 static SYSDEV_ATTR(meminfo
, S_IRUGO
, node_read_meminfo
, NULL
);
144 static ssize_t
node_read_numastat(struct sys_device
* dev
,
145 struct sysdev_attribute
*attr
, char * buf
)
151 "interleave_hit %lu\n"
154 node_page_state(dev
->id
, NUMA_HIT
),
155 node_page_state(dev
->id
, NUMA_MISS
),
156 node_page_state(dev
->id
, NUMA_FOREIGN
),
157 node_page_state(dev
->id
, NUMA_INTERLEAVE_HIT
),
158 node_page_state(dev
->id
, NUMA_LOCAL
),
159 node_page_state(dev
->id
, NUMA_OTHER
));
161 static SYSDEV_ATTR(numastat
, S_IRUGO
, node_read_numastat
, NULL
);
163 static ssize_t
node_read_vmstat(struct sys_device
*dev
,
164 struct sysdev_attribute
*attr
, char *buf
)
170 node_page_state(nid
, NR_WRITTEN
),
171 node_page_state(nid
, NR_DIRTIED
));
173 static SYSDEV_ATTR(vmstat
, S_IRUGO
, node_read_vmstat
, NULL
);
175 static ssize_t
node_read_distance(struct sys_device
* dev
,
176 struct sysdev_attribute
*attr
, char * buf
)
183 * buf is currently PAGE_SIZE in length and each node needs 4 chars
184 * at the most (distance + space or newline).
186 BUILD_BUG_ON(MAX_NUMNODES
* 4 > PAGE_SIZE
);
188 for_each_online_node(i
)
189 len
+= sprintf(buf
+ len
, "%s%d", i
? " " : "", node_distance(nid
, i
));
191 len
+= sprintf(buf
+ len
, "\n");
194 static SYSDEV_ATTR(distance
, S_IRUGO
, node_read_distance
, NULL
);
196 #ifdef CONFIG_HUGETLBFS
198 * hugetlbfs per node attributes registration interface:
199 * When/if hugetlb[fs] subsystem initializes [sometime after this module],
200 * it will register its per node attributes for all online nodes with
201 * memory. It will also call register_hugetlbfs_with_node(), below, to
202 * register its attribute registration functions with this node driver.
203 * Once these hooks have been initialized, the node driver will call into
204 * the hugetlb module to [un]register attributes for hot-plugged nodes.
206 static node_registration_func_t __hugetlb_register_node
;
207 static node_registration_func_t __hugetlb_unregister_node
;
209 static inline bool hugetlb_register_node(struct node
*node
)
211 if (__hugetlb_register_node
&&
212 node_state(node
->sysdev
.id
, N_HIGH_MEMORY
)) {
213 __hugetlb_register_node(node
);
219 static inline void hugetlb_unregister_node(struct node
*node
)
221 if (__hugetlb_unregister_node
)
222 __hugetlb_unregister_node(node
);
225 void register_hugetlbfs_with_node(node_registration_func_t doregister
,
226 node_registration_func_t unregister
)
228 __hugetlb_register_node
= doregister
;
229 __hugetlb_unregister_node
= unregister
;
232 static inline void hugetlb_register_node(struct node
*node
) {}
234 static inline void hugetlb_unregister_node(struct node
*node
) {}
239 * register_node - Setup a sysfs device for a node.
240 * @num - Node number to use when creating the device.
242 * Initialize and register the node device.
244 int register_node(struct node
*node
, int num
, struct node
*parent
)
248 node
->sysdev
.id
= num
;
249 node
->sysdev
.cls
= &node_class
;
250 error
= sysdev_register(&node
->sysdev
);
253 sysdev_create_file(&node
->sysdev
, &attr_cpumap
);
254 sysdev_create_file(&node
->sysdev
, &attr_cpulist
);
255 sysdev_create_file(&node
->sysdev
, &attr_meminfo
);
256 sysdev_create_file(&node
->sysdev
, &attr_numastat
);
257 sysdev_create_file(&node
->sysdev
, &attr_distance
);
258 sysdev_create_file(&node
->sysdev
, &attr_vmstat
);
260 scan_unevictable_register_node(node
);
262 hugetlb_register_node(node
);
264 compaction_register_node(node
);
270 * unregister_node - unregister a node device
271 * @node: node going away
273 * Unregisters a node device @node. All the devices on the node must be
274 * unregistered before calling this function.
276 void unregister_node(struct node
*node
)
278 sysdev_remove_file(&node
->sysdev
, &attr_cpumap
);
279 sysdev_remove_file(&node
->sysdev
, &attr_cpulist
);
280 sysdev_remove_file(&node
->sysdev
, &attr_meminfo
);
281 sysdev_remove_file(&node
->sysdev
, &attr_numastat
);
282 sysdev_remove_file(&node
->sysdev
, &attr_distance
);
283 sysdev_remove_file(&node
->sysdev
, &attr_vmstat
);
285 scan_unevictable_unregister_node(node
);
286 hugetlb_unregister_node(node
); /* no-op, if memoryless node */
288 sysdev_unregister(&node
->sysdev
);
291 struct node node_devices
[MAX_NUMNODES
];
294 * register cpu under node
296 int register_cpu_under_node(unsigned int cpu
, unsigned int nid
)
299 struct sys_device
*obj
;
301 if (!node_online(nid
))
304 obj
= get_cpu_sysdev(cpu
);
308 ret
= sysfs_create_link(&node_devices
[nid
].sysdev
.kobj
,
310 kobject_name(&obj
->kobj
));
314 return sysfs_create_link(&obj
->kobj
,
315 &node_devices
[nid
].sysdev
.kobj
,
316 kobject_name(&node_devices
[nid
].sysdev
.kobj
));
319 int unregister_cpu_under_node(unsigned int cpu
, unsigned int nid
)
321 struct sys_device
*obj
;
323 if (!node_online(nid
))
326 obj
= get_cpu_sysdev(cpu
);
330 sysfs_remove_link(&node_devices
[nid
].sysdev
.kobj
,
331 kobject_name(&obj
->kobj
));
332 sysfs_remove_link(&obj
->kobj
,
333 kobject_name(&node_devices
[nid
].sysdev
.kobj
));
338 #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
339 #define page_initialized(page) (page->lru.next)
341 static int get_nid_for_pfn(unsigned long pfn
)
345 if (!pfn_valid_within(pfn
))
347 page
= pfn_to_page(pfn
);
348 if (!page_initialized(page
))
350 return pfn_to_nid(pfn
);
353 /* register memory section under specified node if it spans that node */
354 int register_mem_sect_under_node(struct memory_block
*mem_blk
, int nid
)
357 unsigned long pfn
, sect_start_pfn
, sect_end_pfn
;
361 if (!node_online(nid
))
363 sect_start_pfn
= section_nr_to_pfn(mem_blk
->phys_index
);
364 sect_end_pfn
= sect_start_pfn
+ PAGES_PER_SECTION
- 1;
365 for (pfn
= sect_start_pfn
; pfn
<= sect_end_pfn
; pfn
++) {
368 page_nid
= get_nid_for_pfn(pfn
);
373 ret
= sysfs_create_link_nowarn(&node_devices
[nid
].sysdev
.kobj
,
374 &mem_blk
->sysdev
.kobj
,
375 kobject_name(&mem_blk
->sysdev
.kobj
));
379 return sysfs_create_link_nowarn(&mem_blk
->sysdev
.kobj
,
380 &node_devices
[nid
].sysdev
.kobj
,
381 kobject_name(&node_devices
[nid
].sysdev
.kobj
));
383 /* mem section does not span the specified node */
387 /* unregister memory section under all nodes that it spans */
388 int unregister_mem_sect_under_nodes(struct memory_block
*mem_blk
)
390 NODEMASK_ALLOC(nodemask_t
, unlinked_nodes
, GFP_KERNEL
);
391 unsigned long pfn
, sect_start_pfn
, sect_end_pfn
;
394 NODEMASK_FREE(unlinked_nodes
);
399 nodes_clear(*unlinked_nodes
);
400 sect_start_pfn
= section_nr_to_pfn(mem_blk
->phys_index
);
401 sect_end_pfn
= sect_start_pfn
+ PAGES_PER_SECTION
- 1;
402 for (pfn
= sect_start_pfn
; pfn
<= sect_end_pfn
; pfn
++) {
405 nid
= get_nid_for_pfn(pfn
);
408 if (!node_online(nid
))
410 if (node_test_and_set(nid
, *unlinked_nodes
))
412 sysfs_remove_link(&node_devices
[nid
].sysdev
.kobj
,
413 kobject_name(&mem_blk
->sysdev
.kobj
));
414 sysfs_remove_link(&mem_blk
->sysdev
.kobj
,
415 kobject_name(&node_devices
[nid
].sysdev
.kobj
));
417 NODEMASK_FREE(unlinked_nodes
);
421 static int link_mem_sections(int nid
)
423 unsigned long start_pfn
= NODE_DATA(nid
)->node_start_pfn
;
424 unsigned long end_pfn
= start_pfn
+ NODE_DATA(nid
)->node_spanned_pages
;
426 struct memory_block
*mem_blk
= NULL
;
429 for (pfn
= start_pfn
; pfn
< end_pfn
; pfn
+= PAGES_PER_SECTION
) {
430 unsigned long section_nr
= pfn_to_section_nr(pfn
);
431 struct mem_section
*mem_sect
;
434 if (!present_section_nr(section_nr
))
436 mem_sect
= __nr_to_section(section_nr
);
437 mem_blk
= find_memory_block_hinted(mem_sect
, mem_blk
);
438 ret
= register_mem_sect_under_node(mem_blk
, nid
);
442 /* discard ref obtained in find_memory_block() */
446 kobject_put(&mem_blk
->sysdev
.kobj
);
450 #ifdef CONFIG_HUGETLBFS
452 * Handle per node hstate attribute [un]registration on transistions
453 * to/from memoryless state.
455 static void node_hugetlb_work(struct work_struct
*work
)
457 struct node
*node
= container_of(work
, struct node
, node_work
);
460 * We only get here when a node transitions to/from memoryless state.
461 * We can detect which transition occurred by examining whether the
462 * node has memory now. hugetlb_register_node() already check this
463 * so we try to register the attributes. If that fails, then the
464 * node has transitioned to memoryless, try to unregister the
467 if (!hugetlb_register_node(node
))
468 hugetlb_unregister_node(node
);
471 static void init_node_hugetlb_work(int nid
)
473 INIT_WORK(&node_devices
[nid
].node_work
, node_hugetlb_work
);
476 static int node_memory_callback(struct notifier_block
*self
,
477 unsigned long action
, void *arg
)
479 struct memory_notify
*mnb
= arg
;
480 int nid
= mnb
->status_change_nid
;
486 * offload per node hstate [un]registration to a work thread
487 * when transitioning to/from memoryless state.
489 if (nid
!= NUMA_NO_NODE
)
490 schedule_work(&node_devices
[nid
].node_work
);
493 case MEM_GOING_ONLINE
:
494 case MEM_GOING_OFFLINE
:
495 case MEM_CANCEL_ONLINE
:
496 case MEM_CANCEL_OFFLINE
:
503 #endif /* CONFIG_HUGETLBFS */
504 #else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */
506 static int link_mem_sections(int nid
) { return 0; }
507 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
509 #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \
510 !defined(CONFIG_HUGETLBFS)
511 static inline int node_memory_callback(struct notifier_block
*self
,
512 unsigned long action
, void *arg
)
517 static void init_node_hugetlb_work(int nid
) { }
521 int register_one_node(int nid
)
526 if (node_online(nid
)) {
527 int p_node
= parent_node(nid
);
528 struct node
*parent
= NULL
;
531 parent
= &node_devices
[p_node
];
533 error
= register_node(&node_devices
[nid
], nid
, parent
);
535 /* link cpu under this node */
536 for_each_present_cpu(cpu
) {
537 if (cpu_to_node(cpu
) == nid
)
538 register_cpu_under_node(cpu
, nid
);
541 /* link memory sections under this node */
542 error
= link_mem_sections(nid
);
544 /* initialize work queue for memory hot plug */
545 init_node_hugetlb_work(nid
);
552 void unregister_one_node(int nid
)
554 unregister_node(&node_devices
[nid
]);
558 * node states attributes
561 static ssize_t
print_nodes_state(enum node_states state
, char *buf
)
565 n
= nodelist_scnprintf(buf
, PAGE_SIZE
, node_states
[state
]);
566 if (n
> 0 && PAGE_SIZE
> n
+ 1) {
574 struct sysdev_class_attribute attr
;
575 enum node_states state
;
578 static ssize_t
show_node_state(struct sysdev_class
*class,
579 struct sysdev_class_attribute
*attr
, char *buf
)
581 struct node_attr
*na
= container_of(attr
, struct node_attr
, attr
);
582 return print_nodes_state(na
->state
, buf
);
585 #define _NODE_ATTR(name, state) \
586 { _SYSDEV_CLASS_ATTR(name, 0444, show_node_state, NULL), state }
588 static struct node_attr node_state_attr
[] = {
589 _NODE_ATTR(possible
, N_POSSIBLE
),
590 _NODE_ATTR(online
, N_ONLINE
),
591 _NODE_ATTR(has_normal_memory
, N_NORMAL_MEMORY
),
592 _NODE_ATTR(has_cpu
, N_CPU
),
593 #ifdef CONFIG_HIGHMEM
594 _NODE_ATTR(has_high_memory
, N_HIGH_MEMORY
),
598 static struct sysdev_class_attribute
*node_state_attrs
[] = {
599 &node_state_attr
[0].attr
,
600 &node_state_attr
[1].attr
,
601 &node_state_attr
[2].attr
,
602 &node_state_attr
[3].attr
,
603 #ifdef CONFIG_HIGHMEM
604 &node_state_attr
[4].attr
,
609 #define NODE_CALLBACK_PRI 2 /* lower than SLAB */
610 static int __init
register_node_type(void)
614 BUILD_BUG_ON(ARRAY_SIZE(node_state_attr
) != NR_NODE_STATES
);
615 BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs
)-1 != NR_NODE_STATES
);
617 ret
= sysdev_class_register(&node_class
);
619 hotplug_memory_notifier(node_memory_callback
,
624 * Note: we're not going to unregister the node class if we fail
625 * to register the node state class attribute files.
629 postcore_initcall(register_node_type
);