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/node.h>
10 #include <linux/hugetlb.h>
11 #include <linux/cpumask.h>
12 #include <linux/topology.h>
13 #include <linux/nodemask.h>
14 #include <linux/cpu.h>
15 #include <linux/device.h>
17 static struct sysdev_class node_class
= {
22 static ssize_t
node_read_cpumap(struct sys_device
*dev
, int type
, char *buf
)
24 struct node
*node_dev
= to_node(dev
);
25 node_to_cpumask_ptr(mask
, node_dev
->sysdev
.id
);
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));
32 cpulist_scnprintf(buf
, PAGE_SIZE
-2, *mask
):
33 cpumask_scnprintf(buf
, PAGE_SIZE
-2, *mask
);
39 static inline ssize_t
node_read_cpumask(struct sys_device
*dev
, char *buf
)
41 return node_read_cpumap(dev
, 0, buf
);
43 static inline ssize_t
node_read_cpulist(struct sys_device
*dev
, char *buf
)
45 return node_read_cpumap(dev
, 1, buf
);
48 static SYSDEV_ATTR(cpumap
, S_IRUGO
, node_read_cpumask
, NULL
);
49 static SYSDEV_ATTR(cpulist
, S_IRUGO
, node_read_cpulist
, NULL
);
51 #define K(x) ((x) << (PAGE_SHIFT - 10))
52 static ssize_t
node_read_meminfo(struct sys_device
* dev
, char * buf
)
58 si_meminfo_node(&i
, nid
);
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"
67 "Node %d HighTotal: %8lu kB\n"
68 "Node %d HighFree: %8lu kB\n"
69 "Node %d LowTotal: %8lu kB\n"
70 "Node %d LowFree: %8lu kB\n"
72 "Node %d Dirty: %8lu kB\n"
73 "Node %d Writeback: %8lu kB\n"
74 "Node %d FilePages: %8lu kB\n"
75 "Node %d Mapped: %8lu kB\n"
76 "Node %d AnonPages: %8lu kB\n"
77 "Node %d PageTables: %8lu kB\n"
78 "Node %d NFS_Unstable: %8lu kB\n"
79 "Node %d Bounce: %8lu kB\n"
80 "Node %d WritebackTmp: %8lu kB\n"
81 "Node %d Slab: %8lu kB\n"
82 "Node %d SReclaimable: %8lu kB\n"
83 "Node %d SUnreclaim: %8lu kB\n",
86 nid
, K(i
.totalram
- i
.freeram
),
87 nid
, node_page_state(nid
, NR_ACTIVE
),
88 nid
, node_page_state(nid
, NR_INACTIVE
),
92 nid
, K(i
.totalram
- i
.totalhigh
),
93 nid
, K(i
.freeram
- i
.freehigh
),
95 nid
, K(node_page_state(nid
, NR_FILE_DIRTY
)),
96 nid
, K(node_page_state(nid
, NR_WRITEBACK
)),
97 nid
, K(node_page_state(nid
, NR_FILE_PAGES
)),
98 nid
, K(node_page_state(nid
, NR_FILE_MAPPED
)),
99 nid
, K(node_page_state(nid
, NR_ANON_PAGES
)),
100 nid
, K(node_page_state(nid
, NR_PAGETABLE
)),
101 nid
, K(node_page_state(nid
, NR_UNSTABLE_NFS
)),
102 nid
, K(node_page_state(nid
, NR_BOUNCE
)),
103 nid
, K(node_page_state(nid
, NR_WRITEBACK_TEMP
)),
104 nid
, K(node_page_state(nid
, NR_SLAB_RECLAIMABLE
) +
105 node_page_state(nid
, NR_SLAB_UNRECLAIMABLE
)),
106 nid
, K(node_page_state(nid
, NR_SLAB_RECLAIMABLE
)),
107 nid
, K(node_page_state(nid
, NR_SLAB_UNRECLAIMABLE
)));
108 n
+= hugetlb_report_node_meminfo(nid
, buf
+ n
);
113 static SYSDEV_ATTR(meminfo
, S_IRUGO
, node_read_meminfo
, NULL
);
115 static ssize_t
node_read_numastat(struct sys_device
* dev
, char * buf
)
121 "interleave_hit %lu\n"
124 node_page_state(dev
->id
, NUMA_HIT
),
125 node_page_state(dev
->id
, NUMA_MISS
),
126 node_page_state(dev
->id
, NUMA_FOREIGN
),
127 node_page_state(dev
->id
, NUMA_INTERLEAVE_HIT
),
128 node_page_state(dev
->id
, NUMA_LOCAL
),
129 node_page_state(dev
->id
, NUMA_OTHER
));
131 static SYSDEV_ATTR(numastat
, S_IRUGO
, node_read_numastat
, NULL
);
133 static ssize_t
node_read_distance(struct sys_device
* dev
, char * buf
)
139 /* buf currently PAGE_SIZE, need ~4 chars per node */
140 BUILD_BUG_ON(MAX_NUMNODES
*4 > PAGE_SIZE
/2);
142 for_each_online_node(i
)
143 len
+= sprintf(buf
+ len
, "%s%d", i
? " " : "", node_distance(nid
, i
));
145 len
+= sprintf(buf
+ len
, "\n");
148 static SYSDEV_ATTR(distance
, S_IRUGO
, node_read_distance
, NULL
);
152 * register_node - Setup a sysfs device for a node.
153 * @num - Node number to use when creating the device.
155 * Initialize and register the node device.
157 int register_node(struct node
*node
, int num
, struct node
*parent
)
161 node
->sysdev
.id
= num
;
162 node
->sysdev
.cls
= &node_class
;
163 error
= sysdev_register(&node
->sysdev
);
166 sysdev_create_file(&node
->sysdev
, &attr_cpumap
);
167 sysdev_create_file(&node
->sysdev
, &attr_cpulist
);
168 sysdev_create_file(&node
->sysdev
, &attr_meminfo
);
169 sysdev_create_file(&node
->sysdev
, &attr_numastat
);
170 sysdev_create_file(&node
->sysdev
, &attr_distance
);
176 * unregister_node - unregister a node device
177 * @node: node going away
179 * Unregisters a node device @node. All the devices on the node must be
180 * unregistered before calling this function.
182 void unregister_node(struct node
*node
)
184 sysdev_remove_file(&node
->sysdev
, &attr_cpumap
);
185 sysdev_remove_file(&node
->sysdev
, &attr_cpulist
);
186 sysdev_remove_file(&node
->sysdev
, &attr_meminfo
);
187 sysdev_remove_file(&node
->sysdev
, &attr_numastat
);
188 sysdev_remove_file(&node
->sysdev
, &attr_distance
);
190 sysdev_unregister(&node
->sysdev
);
193 struct node node_devices
[MAX_NUMNODES
];
196 * register cpu under node
198 int register_cpu_under_node(unsigned int cpu
, unsigned int nid
)
200 if (node_online(nid
)) {
201 struct sys_device
*obj
= get_cpu_sysdev(cpu
);
204 return sysfs_create_link(&node_devices
[nid
].sysdev
.kobj
,
206 kobject_name(&obj
->kobj
));
212 int unregister_cpu_under_node(unsigned int cpu
, unsigned int nid
)
214 if (node_online(nid
)) {
215 struct sys_device
*obj
= get_cpu_sysdev(cpu
);
217 sysfs_remove_link(&node_devices
[nid
].sysdev
.kobj
,
218 kobject_name(&obj
->kobj
));
223 int register_one_node(int nid
)
228 if (node_online(nid
)) {
229 int p_node
= parent_node(nid
);
230 struct node
*parent
= NULL
;
233 parent
= &node_devices
[p_node
];
235 error
= register_node(&node_devices
[nid
], nid
, parent
);
237 /* link cpu under this node */
238 for_each_present_cpu(cpu
) {
239 if (cpu_to_node(cpu
) == nid
)
240 register_cpu_under_node(cpu
, nid
);
248 void unregister_one_node(int nid
)
250 unregister_node(&node_devices
[nid
]);
254 * node states attributes
257 static ssize_t
print_nodes_state(enum node_states state
, char *buf
)
261 n
= nodelist_scnprintf(buf
, PAGE_SIZE
, node_states
[state
]);
262 if (n
> 0 && PAGE_SIZE
> n
+ 1) {
269 static ssize_t
print_nodes_possible(struct sysdev_class
*class, char *buf
)
271 return print_nodes_state(N_POSSIBLE
, buf
);
274 static ssize_t
print_nodes_online(struct sysdev_class
*class, char *buf
)
276 return print_nodes_state(N_ONLINE
, buf
);
279 static ssize_t
print_nodes_has_normal_memory(struct sysdev_class
*class,
282 return print_nodes_state(N_NORMAL_MEMORY
, buf
);
285 static ssize_t
print_nodes_has_cpu(struct sysdev_class
*class, char *buf
)
287 return print_nodes_state(N_CPU
, buf
);
290 static SYSDEV_CLASS_ATTR(possible
, 0444, print_nodes_possible
, NULL
);
291 static SYSDEV_CLASS_ATTR(online
, 0444, print_nodes_online
, NULL
);
292 static SYSDEV_CLASS_ATTR(has_normal_memory
, 0444, print_nodes_has_normal_memory
,
294 static SYSDEV_CLASS_ATTR(has_cpu
, 0444, print_nodes_has_cpu
, NULL
);
296 #ifdef CONFIG_HIGHMEM
297 static ssize_t
print_nodes_has_high_memory(struct sysdev_class
*class,
300 return print_nodes_state(N_HIGH_MEMORY
, buf
);
303 static SYSDEV_CLASS_ATTR(has_high_memory
, 0444, print_nodes_has_high_memory
,
307 struct sysdev_class_attribute
*node_state_attr
[] = {
310 &attr_has_normal_memory
,
311 #ifdef CONFIG_HIGHMEM
312 &attr_has_high_memory
,
317 static int node_states_init(void)
322 for (i
= 0; i
< NR_NODE_STATES
; i
++) {
324 ret
= sysdev_class_create_file(&node_class
, node_state_attr
[i
]);
331 static int __init
register_node_type(void)
335 ret
= sysdev_class_register(&node_class
);
337 ret
= node_states_init();
340 * Note: we're not going to unregister the node class if we fail
341 * to register the node state class attribute files.
345 postcore_initcall(register_node_type
);