]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/base/node.c
unevictable lru: add event counting with statistics
[thirdparty/kernel/stable.git] / drivers / base / node.c
CommitLineData
1da177e4
LT
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/node.h>
10#include <linux/hugetlb.h>
11#include <linux/cpumask.h>
12#include <linux/topology.h>
13#include <linux/nodemask.h>
76b67ed9 14#include <linux/cpu.h>
bde631a5 15#include <linux/device.h>
1da177e4
LT
16
17static struct sysdev_class node_class = {
af5ca3f4 18 .name = "node",
1da177e4
LT
19};
20
21
39106dcf 22static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
1da177e4
LT
23{
24 struct node *node_dev = to_node(dev);
c5f59f08 25 node_to_cpumask_ptr(mask, node_dev->sysdev.id);
1da177e4
LT
26 int len;
27
39106dcf
MT
28 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
29 BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
1da177e4 30
39106dcf
MT
31 len = type?
32 cpulist_scnprintf(buf, PAGE_SIZE-2, *mask):
33 cpumask_scnprintf(buf, PAGE_SIZE-2, *mask);
c5f59f08
MT
34 buf[len++] = '\n';
35 buf[len] = '\0';
1da177e4
LT
36 return len;
37}
38
4a0b2b4d
AK
39static inline ssize_t node_read_cpumask(struct sys_device *dev,
40 struct sysdev_attribute *attr, char *buf)
39106dcf
MT
41{
42 return node_read_cpumap(dev, 0, buf);
43}
4a0b2b4d
AK
44static inline ssize_t node_read_cpulist(struct sys_device *dev,
45 struct sysdev_attribute *attr, char *buf)
39106dcf
MT
46{
47 return node_read_cpumap(dev, 1, buf);
48}
49
50static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
51static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
1da177e4
LT
52
53#define K(x) ((x) << (PAGE_SHIFT - 10))
4a0b2b4d
AK
54static ssize_t node_read_meminfo(struct sys_device * dev,
55 struct sysdev_attribute *attr, char * buf)
1da177e4
LT
56{
57 int n;
58 int nid = dev->id;
59 struct sysinfo i;
1da177e4
LT
60
61 si_meminfo_node(&i, nid);
c07e02db 62
1da177e4 63 n = sprintf(buf, "\n"
4f98a2fe
RR
64 "Node %d MemTotal: %8lu kB\n"
65 "Node %d MemFree: %8lu kB\n"
66 "Node %d MemUsed: %8lu kB\n"
67 "Node %d Active: %8lu kB\n"
68 "Node %d Inactive: %8lu kB\n"
69 "Node %d Active(anon): %8lu kB\n"
70 "Node %d Inactive(anon): %8lu kB\n"
71 "Node %d Active(file): %8lu kB\n"
72 "Node %d Inactive(file): %8lu kB\n"
182e8e23 73#ifdef CONFIG_HIGHMEM
4f98a2fe
RR
74 "Node %d HighTotal: %8lu kB\n"
75 "Node %d HighFree: %8lu kB\n"
76 "Node %d LowTotal: %8lu kB\n"
77 "Node %d LowFree: %8lu kB\n"
182e8e23 78#endif
4f98a2fe
RR
79 "Node %d Dirty: %8lu kB\n"
80 "Node %d Writeback: %8lu kB\n"
81 "Node %d FilePages: %8lu kB\n"
82 "Node %d Mapped: %8lu kB\n"
83 "Node %d AnonPages: %8lu kB\n"
84 "Node %d PageTables: %8lu kB\n"
85 "Node %d NFS_Unstable: %8lu kB\n"
86 "Node %d Bounce: %8lu kB\n"
87 "Node %d WritebackTmp: %8lu kB\n"
88 "Node %d Slab: %8lu kB\n"
89 "Node %d SReclaimable: %8lu kB\n"
90 "Node %d SUnreclaim: %8lu kB\n",
1da177e4
LT
91 nid, K(i.totalram),
92 nid, K(i.freeram),
93 nid, K(i.totalram - i.freeram),
4f98a2fe
RR
94 nid, K(node_page_state(nid, NR_ACTIVE_ANON) +
95 node_page_state(nid, NR_ACTIVE_FILE)),
96 nid, K(node_page_state(nid, NR_INACTIVE_ANON) +
97 node_page_state(nid, NR_INACTIVE_FILE)),
98 nid, K(node_page_state(nid, NR_ACTIVE_ANON)),
99 nid, K(node_page_state(nid, NR_INACTIVE_ANON)),
100 nid, K(node_page_state(nid, NR_ACTIVE_FILE)),
101 nid, K(node_page_state(nid, NR_INACTIVE_FILE)),
182e8e23 102#ifdef CONFIG_HIGHMEM
1da177e4
LT
103 nid, K(i.totalhigh),
104 nid, K(i.freehigh),
105 nid, K(i.totalram - i.totalhigh),
c07e02db 106 nid, K(i.freeram - i.freehigh),
182e8e23 107#endif
b1e7a8fd 108 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
ce866b34 109 nid, K(node_page_state(nid, NR_WRITEBACK)),
347ce434 110 nid, K(node_page_state(nid, NR_FILE_PAGES)),
65ba55f5 111 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
f3dbd344 112 nid, K(node_page_state(nid, NR_ANON_PAGES)),
df849a15 113 nid, K(node_page_state(nid, NR_PAGETABLE)),
fd39fc85 114 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
d2c5e30c 115 nid, K(node_page_state(nid, NR_BOUNCE)),
fc3ba692 116 nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
972d1a7b
CL
117 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
118 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
119 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
120 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
1da177e4
LT
121 n += hugetlb_report_node_meminfo(nid, buf + n);
122 return n;
123}
124
125#undef K
126static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
127
4a0b2b4d
AK
128static ssize_t node_read_numastat(struct sys_device * dev,
129 struct sysdev_attribute *attr, char * buf)
1da177e4 130{
1da177e4
LT
131 return sprintf(buf,
132 "numa_hit %lu\n"
133 "numa_miss %lu\n"
134 "numa_foreign %lu\n"
135 "interleave_hit %lu\n"
136 "local_node %lu\n"
137 "other_node %lu\n",
ca889e6c
CL
138 node_page_state(dev->id, NUMA_HIT),
139 node_page_state(dev->id, NUMA_MISS),
140 node_page_state(dev->id, NUMA_FOREIGN),
141 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
142 node_page_state(dev->id, NUMA_LOCAL),
143 node_page_state(dev->id, NUMA_OTHER));
1da177e4
LT
144}
145static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
146
4a0b2b4d
AK
147static ssize_t node_read_distance(struct sys_device * dev,
148 struct sysdev_attribute *attr, char * buf)
1da177e4
LT
149{
150 int nid = dev->id;
151 int len = 0;
152 int i;
153
154 /* buf currently PAGE_SIZE, need ~4 chars per node */
155 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
156
157 for_each_online_node(i)
158 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
159
160 len += sprintf(buf + len, "\n");
161 return len;
162}
163static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
164
165
166/*
405ae7d3 167 * register_node - Setup a sysfs device for a node.
1da177e4
LT
168 * @num - Node number to use when creating the device.
169 *
170 * Initialize and register the node device.
171 */
4b45099b 172int register_node(struct node *node, int num, struct node *parent)
1da177e4
LT
173{
174 int error;
175
176 node->sysdev.id = num;
177 node->sysdev.cls = &node_class;
178 error = sysdev_register(&node->sysdev);
179
180 if (!error){
181 sysdev_create_file(&node->sysdev, &attr_cpumap);
39106dcf 182 sysdev_create_file(&node->sysdev, &attr_cpulist);
1da177e4
LT
183 sysdev_create_file(&node->sysdev, &attr_meminfo);
184 sysdev_create_file(&node->sysdev, &attr_numastat);
185 sysdev_create_file(&node->sysdev, &attr_distance);
186 }
187 return error;
188}
189
4b45099b
KT
190/**
191 * unregister_node - unregister a node device
192 * @node: node going away
193 *
194 * Unregisters a node device @node. All the devices on the node must be
195 * unregistered before calling this function.
196 */
197void unregister_node(struct node *node)
198{
199 sysdev_remove_file(&node->sysdev, &attr_cpumap);
39106dcf 200 sysdev_remove_file(&node->sysdev, &attr_cpulist);
4b45099b
KT
201 sysdev_remove_file(&node->sysdev, &attr_meminfo);
202 sysdev_remove_file(&node->sysdev, &attr_numastat);
203 sysdev_remove_file(&node->sysdev, &attr_distance);
204
205 sysdev_unregister(&node->sysdev);
206}
1da177e4 207
0fc44159
YG
208struct node node_devices[MAX_NUMNODES];
209
76b67ed9
KH
210/*
211 * register cpu under node
212 */
213int register_cpu_under_node(unsigned int cpu, unsigned int nid)
214{
215 if (node_online(nid)) {
216 struct sys_device *obj = get_cpu_sysdev(cpu);
217 if (!obj)
218 return 0;
219 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
220 &obj->kobj,
221 kobject_name(&obj->kobj));
222 }
223
224 return 0;
225}
226
227int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
228{
229 if (node_online(nid)) {
230 struct sys_device *obj = get_cpu_sysdev(cpu);
231 if (obj)
232 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
233 kobject_name(&obj->kobj));
234 }
235 return 0;
236}
237
0fc44159
YG
238int register_one_node(int nid)
239{
240 int error = 0;
76b67ed9 241 int cpu;
0fc44159
YG
242
243 if (node_online(nid)) {
244 int p_node = parent_node(nid);
245 struct node *parent = NULL;
246
247 if (p_node != nid)
248 parent = &node_devices[p_node];
249
250 error = register_node(&node_devices[nid], nid, parent);
76b67ed9
KH
251
252 /* link cpu under this node */
253 for_each_present_cpu(cpu) {
254 if (cpu_to_node(cpu) == nid)
255 register_cpu_under_node(cpu, nid);
256 }
0fc44159
YG
257 }
258
259 return error;
260
261}
262
263void unregister_one_node(int nid)
264{
265 unregister_node(&node_devices[nid]);
266}
267
bde631a5
LS
268/*
269 * node states attributes
270 */
271
272static ssize_t print_nodes_state(enum node_states state, char *buf)
273{
274 int n;
275
276 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
277 if (n > 0 && PAGE_SIZE > n + 1) {
278 *(buf + n++) = '\n';
279 *(buf + n++) = '\0';
280 }
281 return n;
282}
283
284static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
285{
286 return print_nodes_state(N_POSSIBLE, buf);
287}
288
289static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
290{
291 return print_nodes_state(N_ONLINE, buf);
292}
293
294static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
295 char *buf)
296{
297 return print_nodes_state(N_NORMAL_MEMORY, buf);
298}
299
300static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
301{
302 return print_nodes_state(N_CPU, buf);
303}
304
305static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
306static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
307static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
308 NULL);
309static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
310
311#ifdef CONFIG_HIGHMEM
312static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
313 char *buf)
314{
315 return print_nodes_state(N_HIGH_MEMORY, buf);
316}
317
318static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
319 NULL);
320#endif
321
322struct sysdev_class_attribute *node_state_attr[] = {
323 &attr_possible,
324 &attr_online,
325 &attr_has_normal_memory,
326#ifdef CONFIG_HIGHMEM
327 &attr_has_high_memory,
328#endif
329 &attr_has_cpu,
330};
331
332static int node_states_init(void)
333{
334 int i;
335 int err = 0;
336
337 for (i = 0; i < NR_NODE_STATES; i++) {
338 int ret;
339 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
340 if (!err)
341 err = ret;
342 }
343 return err;
344}
345
4b45099b 346static int __init register_node_type(void)
1da177e4 347{
bde631a5
LS
348 int ret;
349
350 ret = sysdev_class_register(&node_class);
351 if (!ret)
352 ret = node_states_init();
353
354 /*
355 * Note: we're not going to unregister the node class if we fail
356 * to register the node state class attribute files.
357 */
358 return ret;
1da177e4
LT
359}
360postcore_initcall(register_node_type);