blob: 12fde2d03d695a112f02debd25c4fb897a4b937c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070014#include <linux/cpu.h>
Lee Schermerhornbde631a2007-10-16 01:26:27 -070015#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17static struct sysdev_class node_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010018 .name = "node",
Linus Torvalds1da177e2005-04-16 15:20:36 -070019};
20
21
Mike Travis39106dc2008-04-08 11:43:03 -070022static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 struct node *node_dev = to_node(dev);
Mike Travisc5f59f02008-04-04 18:11:10 -070025 node_to_cpumask_ptr(mask, node_dev->sysdev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int len;
27
Mike Travis39106dc2008-04-08 11:43:03 -070028 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
29 BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Mike Travis39106dc2008-04-08 11:43:03 -070031 len = type?
32 cpulist_scnprintf(buf, PAGE_SIZE-2, *mask):
33 cpumask_scnprintf(buf, PAGE_SIZE-2, *mask);
Mike Travisc5f59f02008-04-04 18:11:10 -070034 buf[len++] = '\n';
35 buf[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return len;
37}
38
Mike Travis39106dc2008-04-08 11:43:03 -070039static inline ssize_t node_read_cpumask(struct sys_device *dev, char *buf)
40{
41 return node_read_cpumap(dev, 0, buf);
42}
43static inline ssize_t node_read_cpulist(struct sys_device *dev, char *buf)
44{
45 return node_read_cpumap(dev, 1, buf);
46}
47
48static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
49static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define K(x) ((x) << (PAGE_SHIFT - 10))
52static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
53{
54 int n;
55 int nid = dev->id;
56 struct sysinfo i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 si_meminfo_node(&i, nid);
Martin Hicksc07e02d2005-09-03 15:55:11 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 n = sprintf(buf, "\n"
61 "Node %d MemTotal: %8lu kB\n"
62 "Node %d MemFree: %8lu kB\n"
63 "Node %d MemUsed: %8lu kB\n"
64 "Node %d Active: %8lu kB\n"
65 "Node %d Inactive: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070066#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 "Node %d HighTotal: %8lu kB\n"
68 "Node %d HighFree: %8lu kB\n"
69 "Node %d LowTotal: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070070 "Node %d LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070071#endif
Martin Hicksc07e02d2005-09-03 15:55:11 -070072 "Node %d Dirty: %8lu kB\n"
73 "Node %d Writeback: %8lu kB\n"
Christoph Lameter347ce432006-06-30 01:55:35 -070074 "Node %d FilePages: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070075 "Node %d Mapped: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -070076 "Node %d AnonPages: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -070077 "Node %d PageTables: %8lu kB\n"
Andrew Mortonf5ef68d2006-08-27 01:23:58 -070078 "Node %d NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -070079 "Node %d Bounce: %8lu kB\n"
Christoph Lameter972d1a72006-09-25 23:31:51 -070080 "Node %d Slab: %8lu kB\n"
81 "Node %d SReclaimable: %8lu kB\n"
82 "Node %d SUnreclaim: %8lu kB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 nid, K(i.totalram),
84 nid, K(i.freeram),
85 nid, K(i.totalram - i.freeram),
Christoph Lameter05a04162007-02-10 01:43:05 -080086 nid, node_page_state(nid, NR_ACTIVE),
87 nid, node_page_state(nid, NR_INACTIVE),
Christoph Lameter182e8e22006-09-25 23:31:10 -070088#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 nid, K(i.totalhigh),
90 nid, K(i.freehigh),
91 nid, K(i.totalram - i.totalhigh),
Martin Hicksc07e02d2005-09-03 15:55:11 -070092 nid, K(i.freeram - i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -070093#endif
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -070094 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -070095 nid, K(node_page_state(nid, NR_WRITEBACK)),
Christoph Lameter347ce432006-06-30 01:55:35 -070096 nid, K(node_page_state(nid, NR_FILE_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -070097 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -070098 nid, K(node_page_state(nid, NR_ANON_PAGES)),
Christoph Lameterdf849a12006-06-30 01:55:38 -070099 nid, K(node_page_state(nid, NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -0700100 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700101 nid, K(node_page_state(nid, NR_BOUNCE)),
Christoph Lameter972d1a72006-09-25 23:31:51 -0700102 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
103 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
104 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
105 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 n += hugetlb_report_node_meminfo(nid, buf + n);
107 return n;
108}
109
110#undef K
111static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
112
113static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
114{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return sprintf(buf,
116 "numa_hit %lu\n"
117 "numa_miss %lu\n"
118 "numa_foreign %lu\n"
119 "interleave_hit %lu\n"
120 "local_node %lu\n"
121 "other_node %lu\n",
Christoph Lameterca889e62006-06-30 01:55:44 -0700122 node_page_state(dev->id, NUMA_HIT),
123 node_page_state(dev->id, NUMA_MISS),
124 node_page_state(dev->id, NUMA_FOREIGN),
125 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
126 node_page_state(dev->id, NUMA_LOCAL),
127 node_page_state(dev->id, NUMA_OTHER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
130
131static ssize_t node_read_distance(struct sys_device * dev, char * buf)
132{
133 int nid = dev->id;
134 int len = 0;
135 int i;
136
137 /* buf currently PAGE_SIZE, need ~4 chars per node */
138 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
139
140 for_each_online_node(i)
141 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
142
143 len += sprintf(buf + len, "\n");
144 return len;
145}
146static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
147
148
149/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100150 * register_node - Setup a sysfs device for a node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * @num - Node number to use when creating the device.
152 *
153 * Initialize and register the node device.
154 */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900155int register_node(struct node *node, int num, struct node *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int error;
158
159 node->sysdev.id = num;
160 node->sysdev.cls = &node_class;
161 error = sysdev_register(&node->sysdev);
162
163 if (!error){
164 sysdev_create_file(&node->sysdev, &attr_cpumap);
Mike Travis39106dc2008-04-08 11:43:03 -0700165 sysdev_create_file(&node->sysdev, &attr_cpulist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 sysdev_create_file(&node->sysdev, &attr_meminfo);
167 sysdev_create_file(&node->sysdev, &attr_numastat);
168 sysdev_create_file(&node->sysdev, &attr_distance);
169 }
170 return error;
171}
172
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900173/**
174 * unregister_node - unregister a node device
175 * @node: node going away
176 *
177 * Unregisters a node device @node. All the devices on the node must be
178 * unregistered before calling this function.
179 */
180void unregister_node(struct node *node)
181{
182 sysdev_remove_file(&node->sysdev, &attr_cpumap);
Mike Travis39106dc2008-04-08 11:43:03 -0700183 sysdev_remove_file(&node->sysdev, &attr_cpulist);
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900184 sysdev_remove_file(&node->sysdev, &attr_meminfo);
185 sysdev_remove_file(&node->sysdev, &attr_numastat);
186 sysdev_remove_file(&node->sysdev, &attr_distance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900188 sysdev_unregister(&node->sysdev);
189}
190
Yasunori Goto0fc44152006-06-27 02:53:38 -0700191struct node node_devices[MAX_NUMNODES];
192
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700193/*
194 * register cpu under node
195 */
196int register_cpu_under_node(unsigned int cpu, unsigned int nid)
197{
198 if (node_online(nid)) {
199 struct sys_device *obj = get_cpu_sysdev(cpu);
200 if (!obj)
201 return 0;
202 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
203 &obj->kobj,
204 kobject_name(&obj->kobj));
205 }
206
207 return 0;
208}
209
210int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
211{
212 if (node_online(nid)) {
213 struct sys_device *obj = get_cpu_sysdev(cpu);
214 if (obj)
215 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
216 kobject_name(&obj->kobj));
217 }
218 return 0;
219}
220
Yasunori Goto0fc44152006-06-27 02:53:38 -0700221int register_one_node(int nid)
222{
223 int error = 0;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700224 int cpu;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700225
226 if (node_online(nid)) {
227 int p_node = parent_node(nid);
228 struct node *parent = NULL;
229
230 if (p_node != nid)
231 parent = &node_devices[p_node];
232
233 error = register_node(&node_devices[nid], nid, parent);
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700234
235 /* link cpu under this node */
236 for_each_present_cpu(cpu) {
237 if (cpu_to_node(cpu) == nid)
238 register_cpu_under_node(cpu, nid);
239 }
Yasunori Goto0fc44152006-06-27 02:53:38 -0700240 }
241
242 return error;
243
244}
245
246void unregister_one_node(int nid)
247{
248 unregister_node(&node_devices[nid]);
249}
250
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700251/*
252 * node states attributes
253 */
254
255static ssize_t print_nodes_state(enum node_states state, char *buf)
256{
257 int n;
258
259 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
260 if (n > 0 && PAGE_SIZE > n + 1) {
261 *(buf + n++) = '\n';
262 *(buf + n++) = '\0';
263 }
264 return n;
265}
266
267static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
268{
269 return print_nodes_state(N_POSSIBLE, buf);
270}
271
272static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
273{
274 return print_nodes_state(N_ONLINE, buf);
275}
276
277static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
278 char *buf)
279{
280 return print_nodes_state(N_NORMAL_MEMORY, buf);
281}
282
283static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
284{
285 return print_nodes_state(N_CPU, buf);
286}
287
288static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
289static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
290static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
291 NULL);
292static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
293
294#ifdef CONFIG_HIGHMEM
295static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
296 char *buf)
297{
298 return print_nodes_state(N_HIGH_MEMORY, buf);
299}
300
301static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
302 NULL);
303#endif
304
305struct sysdev_class_attribute *node_state_attr[] = {
306 &attr_possible,
307 &attr_online,
308 &attr_has_normal_memory,
309#ifdef CONFIG_HIGHMEM
310 &attr_has_high_memory,
311#endif
312 &attr_has_cpu,
313};
314
315static int node_states_init(void)
316{
317 int i;
318 int err = 0;
319
320 for (i = 0; i < NR_NODE_STATES; i++) {
321 int ret;
322 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
323 if (!err)
324 err = ret;
325 }
326 return err;
327}
328
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900329static int __init register_node_type(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700331 int ret;
332
333 ret = sysdev_class_register(&node_class);
334 if (!ret)
335 ret = node_states_init();
336
337 /*
338 * Note: we're not going to unregister the node class if we fail
339 * to register the node state class attribute files.
340 */
341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343postcore_initcall(register_node_type);