Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
KAMEZAWA Hiroyuki | 76b67ed9 | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 14 | #include <linux/cpu.h> |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 15 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | static struct sysdev_class node_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 18 | .name = "node", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 22 | static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
| 24 | struct node *node_dev = to_node(dev); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 25 | node_to_cpumask_ptr(mask, node_dev->sysdev.id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | int len; |
| 27 | |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 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)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 31 | len = type? |
| 32 | cpulist_scnprintf(buf, PAGE_SIZE-2, *mask): |
| 33 | cpumask_scnprintf(buf, PAGE_SIZE-2, *mask); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 34 | buf[len++] = '\n'; |
| 35 | buf[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | return len; |
| 37 | } |
| 38 | |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 39 | static inline ssize_t node_read_cpumask(struct sys_device *dev, char *buf) |
| 40 | { |
| 41 | return node_read_cpumap(dev, 0, buf); |
| 42 | } |
| 43 | static inline ssize_t node_read_cpulist(struct sys_device *dev, char *buf) |
| 44 | { |
| 45 | return node_read_cpumap(dev, 1, buf); |
| 46 | } |
| 47 | |
| 48 | static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL); |
| 49 | static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #define K(x) ((x) << (PAGE_SHIFT - 10)) |
| 52 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | si_meminfo_node(&i, nid); |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | 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 Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 66 | #ifdef CONFIG_HIGHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | "Node %d HighTotal: %8lu kB\n" |
| 68 | "Node %d HighFree: %8lu kB\n" |
| 69 | "Node %d LowTotal: %8lu kB\n" |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 70 | "Node %d LowFree: %8lu kB\n" |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 71 | #endif |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 72 | "Node %d Dirty: %8lu kB\n" |
| 73 | "Node %d Writeback: %8lu kB\n" |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 74 | "Node %d FilePages: %8lu kB\n" |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 75 | "Node %d Mapped: %8lu kB\n" |
Christoph Lameter | f3dbd34 | 2006-06-30 01:55:36 -0700 | [diff] [blame] | 76 | "Node %d AnonPages: %8lu kB\n" |
Christoph Lameter | df849a1 | 2006-06-30 01:55:38 -0700 | [diff] [blame] | 77 | "Node %d PageTables: %8lu kB\n" |
Andrew Morton | f5ef68d | 2006-08-27 01:23:58 -0700 | [diff] [blame] | 78 | "Node %d NFS_Unstable: %8lu kB\n" |
Christoph Lameter | d2c5e30 | 2006-06-30 01:55:41 -0700 | [diff] [blame] | 79 | "Node %d Bounce: %8lu kB\n" |
Miklos Szeredi | fc3ba69 | 2008-04-30 00:54:38 -0700 | [diff] [blame] | 80 | "Node %d WritebackTmp: %8lu kB\n" |
Christoph Lameter | 972d1a7 | 2006-09-25 23:31:51 -0700 | [diff] [blame] | 81 | "Node %d Slab: %8lu kB\n" |
| 82 | "Node %d SReclaimable: %8lu kB\n" |
| 83 | "Node %d SUnreclaim: %8lu kB\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | nid, K(i.totalram), |
| 85 | nid, K(i.freeram), |
| 86 | nid, K(i.totalram - i.freeram), |
John Blackwood | 2d5c1be | 2008-07-04 10:00:05 -0700 | [diff] [blame] | 87 | nid, K(node_page_state(nid, NR_ACTIVE)), |
| 88 | nid, K(node_page_state(nid, NR_INACTIVE)), |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 89 | #ifdef CONFIG_HIGHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | nid, K(i.totalhigh), |
| 91 | nid, K(i.freehigh), |
| 92 | nid, K(i.totalram - i.totalhigh), |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 93 | nid, K(i.freeram - i.freehigh), |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 94 | #endif |
Christoph Lameter | b1e7a8f | 2006-06-30 01:55:39 -0700 | [diff] [blame] | 95 | nid, K(node_page_state(nid, NR_FILE_DIRTY)), |
Christoph Lameter | ce866b3 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 96 | nid, K(node_page_state(nid, NR_WRITEBACK)), |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 97 | nid, K(node_page_state(nid, NR_FILE_PAGES)), |
Christoph Lameter | 65ba55f | 2006-06-30 01:55:34 -0700 | [diff] [blame] | 98 | nid, K(node_page_state(nid, NR_FILE_MAPPED)), |
Christoph Lameter | f3dbd34 | 2006-06-30 01:55:36 -0700 | [diff] [blame] | 99 | nid, K(node_page_state(nid, NR_ANON_PAGES)), |
Christoph Lameter | df849a1 | 2006-06-30 01:55:38 -0700 | [diff] [blame] | 100 | nid, K(node_page_state(nid, NR_PAGETABLE)), |
Christoph Lameter | fd39fc8 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 101 | nid, K(node_page_state(nid, NR_UNSTABLE_NFS)), |
Christoph Lameter | d2c5e30 | 2006-06-30 01:55:41 -0700 | [diff] [blame] | 102 | nid, K(node_page_state(nid, NR_BOUNCE)), |
Miklos Szeredi | fc3ba69 | 2008-04-30 00:54:38 -0700 | [diff] [blame] | 103 | nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)), |
Christoph Lameter | 972d1a7 | 2006-09-25 23:31:51 -0700 | [diff] [blame] | 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))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | n += hugetlb_report_node_meminfo(nid, buf + n); |
| 109 | return n; |
| 110 | } |
| 111 | |
| 112 | #undef K |
| 113 | static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL); |
| 114 | |
| 115 | static ssize_t node_read_numastat(struct sys_device * dev, char * buf) |
| 116 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return sprintf(buf, |
| 118 | "numa_hit %lu\n" |
| 119 | "numa_miss %lu\n" |
| 120 | "numa_foreign %lu\n" |
| 121 | "interleave_hit %lu\n" |
| 122 | "local_node %lu\n" |
| 123 | "other_node %lu\n", |
Christoph Lameter | ca889e6 | 2006-06-30 01:55:44 -0700 | [diff] [blame] | 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)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL); |
| 132 | |
| 133 | static ssize_t node_read_distance(struct sys_device * dev, char * buf) |
| 134 | { |
| 135 | int nid = dev->id; |
| 136 | int len = 0; |
| 137 | int i; |
| 138 | |
| 139 | /* buf currently PAGE_SIZE, need ~4 chars per node */ |
| 140 | BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2); |
| 141 | |
| 142 | for_each_online_node(i) |
| 143 | len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i)); |
| 144 | |
| 145 | len += sprintf(buf + len, "\n"); |
| 146 | return len; |
| 147 | } |
| 148 | static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL); |
| 149 | |
| 150 | |
| 151 | /* |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 152 | * register_node - Setup a sysfs device for a node. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * @num - Node number to use when creating the device. |
| 154 | * |
| 155 | * Initialize and register the node device. |
| 156 | */ |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 157 | int register_node(struct node *node, int num, struct node *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
| 159 | int error; |
| 160 | |
| 161 | node->sysdev.id = num; |
| 162 | node->sysdev.cls = &node_class; |
| 163 | error = sysdev_register(&node->sysdev); |
| 164 | |
| 165 | if (!error){ |
| 166 | sysdev_create_file(&node->sysdev, &attr_cpumap); |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 167 | sysdev_create_file(&node->sysdev, &attr_cpulist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | sysdev_create_file(&node->sysdev, &attr_meminfo); |
| 169 | sysdev_create_file(&node->sysdev, &attr_numastat); |
| 170 | sysdev_create_file(&node->sysdev, &attr_distance); |
| 171 | } |
| 172 | return error; |
| 173 | } |
| 174 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 175 | /** |
| 176 | * unregister_node - unregister a node device |
| 177 | * @node: node going away |
| 178 | * |
| 179 | * Unregisters a node device @node. All the devices on the node must be |
| 180 | * unregistered before calling this function. |
| 181 | */ |
| 182 | void unregister_node(struct node *node) |
| 183 | { |
| 184 | sysdev_remove_file(&node->sysdev, &attr_cpumap); |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 185 | sysdev_remove_file(&node->sysdev, &attr_cpulist); |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 186 | sysdev_remove_file(&node->sysdev, &attr_meminfo); |
| 187 | sysdev_remove_file(&node->sysdev, &attr_numastat); |
| 188 | sysdev_remove_file(&node->sysdev, &attr_distance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 190 | sysdev_unregister(&node->sysdev); |
| 191 | } |
| 192 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 193 | struct node node_devices[MAX_NUMNODES]; |
| 194 | |
KAMEZAWA Hiroyuki | 76b67ed9 | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 195 | /* |
| 196 | * register cpu under node |
| 197 | */ |
| 198 | int register_cpu_under_node(unsigned int cpu, unsigned int nid) |
| 199 | { |
| 200 | if (node_online(nid)) { |
| 201 | struct sys_device *obj = get_cpu_sysdev(cpu); |
| 202 | if (!obj) |
| 203 | return 0; |
| 204 | return sysfs_create_link(&node_devices[nid].sysdev.kobj, |
| 205 | &obj->kobj, |
| 206 | kobject_name(&obj->kobj)); |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | int unregister_cpu_under_node(unsigned int cpu, unsigned int nid) |
| 213 | { |
| 214 | if (node_online(nid)) { |
| 215 | struct sys_device *obj = get_cpu_sysdev(cpu); |
| 216 | if (obj) |
| 217 | sysfs_remove_link(&node_devices[nid].sysdev.kobj, |
| 218 | kobject_name(&obj->kobj)); |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 223 | int register_one_node(int nid) |
| 224 | { |
| 225 | int error = 0; |
KAMEZAWA Hiroyuki | 76b67ed9 | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 226 | int cpu; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 227 | |
| 228 | if (node_online(nid)) { |
| 229 | int p_node = parent_node(nid); |
| 230 | struct node *parent = NULL; |
| 231 | |
| 232 | if (p_node != nid) |
| 233 | parent = &node_devices[p_node]; |
| 234 | |
| 235 | error = register_node(&node_devices[nid], nid, parent); |
KAMEZAWA Hiroyuki | 76b67ed9 | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 236 | |
| 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); |
| 241 | } |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return error; |
| 245 | |
| 246 | } |
| 247 | |
| 248 | void unregister_one_node(int nid) |
| 249 | { |
| 250 | unregister_node(&node_devices[nid]); |
| 251 | } |
| 252 | |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 253 | /* |
| 254 | * node states attributes |
| 255 | */ |
| 256 | |
| 257 | static ssize_t print_nodes_state(enum node_states state, char *buf) |
| 258 | { |
| 259 | int n; |
| 260 | |
| 261 | n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]); |
| 262 | if (n > 0 && PAGE_SIZE > n + 1) { |
| 263 | *(buf + n++) = '\n'; |
| 264 | *(buf + n++) = '\0'; |
| 265 | } |
| 266 | return n; |
| 267 | } |
| 268 | |
| 269 | static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf) |
| 270 | { |
| 271 | return print_nodes_state(N_POSSIBLE, buf); |
| 272 | } |
| 273 | |
| 274 | static ssize_t print_nodes_online(struct sysdev_class *class, char *buf) |
| 275 | { |
| 276 | return print_nodes_state(N_ONLINE, buf); |
| 277 | } |
| 278 | |
| 279 | static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class, |
| 280 | char *buf) |
| 281 | { |
| 282 | return print_nodes_state(N_NORMAL_MEMORY, buf); |
| 283 | } |
| 284 | |
| 285 | static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf) |
| 286 | { |
| 287 | return print_nodes_state(N_CPU, buf); |
| 288 | } |
| 289 | |
| 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, |
| 293 | NULL); |
| 294 | static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL); |
| 295 | |
| 296 | #ifdef CONFIG_HIGHMEM |
| 297 | static ssize_t print_nodes_has_high_memory(struct sysdev_class *class, |
| 298 | char *buf) |
| 299 | { |
| 300 | return print_nodes_state(N_HIGH_MEMORY, buf); |
| 301 | } |
| 302 | |
| 303 | static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory, |
| 304 | NULL); |
| 305 | #endif |
| 306 | |
| 307 | struct sysdev_class_attribute *node_state_attr[] = { |
| 308 | &attr_possible, |
| 309 | &attr_online, |
| 310 | &attr_has_normal_memory, |
| 311 | #ifdef CONFIG_HIGHMEM |
| 312 | &attr_has_high_memory, |
| 313 | #endif |
| 314 | &attr_has_cpu, |
| 315 | }; |
| 316 | |
| 317 | static int node_states_init(void) |
| 318 | { |
| 319 | int i; |
| 320 | int err = 0; |
| 321 | |
| 322 | for (i = 0; i < NR_NODE_STATES; i++) { |
| 323 | int ret; |
| 324 | ret = sysdev_class_create_file(&node_class, node_state_attr[i]); |
| 325 | if (!err) |
| 326 | err = ret; |
| 327 | } |
| 328 | return err; |
| 329 | } |
| 330 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 331 | static int __init register_node_type(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 333 | int ret; |
| 334 | |
| 335 | ret = sysdev_class_register(&node_class); |
| 336 | if (!ret) |
| 337 | ret = node_states_init(); |
| 338 | |
| 339 | /* |
| 340 | * Note: we're not going to unregister the node class if we fail |
| 341 | * to register the node state class attribute files. |
| 342 | */ |
| 343 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | postcore_initcall(register_node_type); |