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> |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 9 | #include <linux/memory.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/node.h> |
| 11 | #include <linux/hugetlb.h> |
| 12 | #include <linux/cpumask.h> |
| 13 | #include <linux/topology.h> |
| 14 | #include <linux/nodemask.h> |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 15 | #include <linux/cpu.h> |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 16 | #include <linux/device.h> |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 17 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Andi Kleen | 3701cde | 2010-01-05 12:48:04 +0100 | [diff] [blame^] | 19 | static struct sysdev_class_attribute *node_state_attrs[]; |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | static struct sysdev_class node_class = { |
Kay Sievers | af5ca3f4 | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 22 | .name = "node", |
Andi Kleen | 3701cde | 2010-01-05 12:48:04 +0100 | [diff] [blame^] | 23 | .attrs = node_state_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 27 | 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] | 28 | { |
| 29 | struct node *node_dev = to_node(dev); |
Rusty Russell | a70f730 | 2009-03-13 14:49:46 +1030 | [diff] [blame] | 30 | const struct cpumask *mask = cpumask_of_node(node_dev->sysdev.id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | int len; |
| 32 | |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 33 | /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */ |
| 34 | BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 36 | len = type? |
Rusty Russell | 29c0177 | 2008-12-13 21:20:25 +1030 | [diff] [blame] | 37 | cpulist_scnprintf(buf, PAGE_SIZE-2, mask) : |
| 38 | cpumask_scnprintf(buf, PAGE_SIZE-2, mask); |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 39 | buf[len++] = '\n'; |
| 40 | buf[len] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | return len; |
| 42 | } |
| 43 | |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 44 | static inline ssize_t node_read_cpumask(struct sys_device *dev, |
| 45 | struct sysdev_attribute *attr, char *buf) |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 46 | { |
| 47 | return node_read_cpumap(dev, 0, buf); |
| 48 | } |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 49 | static inline ssize_t node_read_cpulist(struct sys_device *dev, |
| 50 | struct sysdev_attribute *attr, char *buf) |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 51 | { |
| 52 | return node_read_cpumap(dev, 1, buf); |
| 53 | } |
| 54 | |
| 55 | static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL); |
| 56 | static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | #define K(x) ((x) << (PAGE_SHIFT - 10)) |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 59 | static ssize_t node_read_meminfo(struct sys_device * dev, |
| 60 | struct sysdev_attribute *attr, char * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | int n; |
| 63 | int nid = dev->id; |
| 64 | struct sysinfo i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | si_meminfo_node(&i, nid); |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | n = sprintf(buf, "\n" |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 69 | "Node %d MemTotal: %8lu kB\n" |
| 70 | "Node %d MemFree: %8lu kB\n" |
| 71 | "Node %d MemUsed: %8lu kB\n" |
| 72 | "Node %d Active: %8lu kB\n" |
| 73 | "Node %d Inactive: %8lu kB\n" |
| 74 | "Node %d Active(anon): %8lu kB\n" |
| 75 | "Node %d Inactive(anon): %8lu kB\n" |
| 76 | "Node %d Active(file): %8lu kB\n" |
| 77 | "Node %d Inactive(file): %8lu kB\n" |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 78 | "Node %d Unevictable: %8lu kB\n" |
| 79 | "Node %d Mlocked: %8lu kB\n" |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 80 | #ifdef CONFIG_HIGHMEM |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 81 | "Node %d HighTotal: %8lu kB\n" |
| 82 | "Node %d HighFree: %8lu kB\n" |
| 83 | "Node %d LowTotal: %8lu kB\n" |
| 84 | "Node %d LowFree: %8lu kB\n" |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 85 | #endif |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 86 | "Node %d Dirty: %8lu kB\n" |
| 87 | "Node %d Writeback: %8lu kB\n" |
| 88 | "Node %d FilePages: %8lu kB\n" |
| 89 | "Node %d Mapped: %8lu kB\n" |
| 90 | "Node %d AnonPages: %8lu kB\n" |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 91 | "Node %d Shmem: %8lu kB\n" |
KOSAKI Motohiro | c6a7f57 | 2009-09-21 17:01:32 -0700 | [diff] [blame] | 92 | "Node %d KernelStack: %8lu kB\n" |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 93 | "Node %d PageTables: %8lu kB\n" |
| 94 | "Node %d NFS_Unstable: %8lu kB\n" |
| 95 | "Node %d Bounce: %8lu kB\n" |
| 96 | "Node %d WritebackTmp: %8lu kB\n" |
| 97 | "Node %d Slab: %8lu kB\n" |
| 98 | "Node %d SReclaimable: %8lu kB\n" |
| 99 | "Node %d SUnreclaim: %8lu kB\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | nid, K(i.totalram), |
| 101 | nid, K(i.freeram), |
| 102 | nid, K(i.totalram - i.freeram), |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 103 | nid, K(node_page_state(nid, NR_ACTIVE_ANON) + |
| 104 | node_page_state(nid, NR_ACTIVE_FILE)), |
| 105 | nid, K(node_page_state(nid, NR_INACTIVE_ANON) + |
| 106 | node_page_state(nid, NR_INACTIVE_FILE)), |
| 107 | nid, K(node_page_state(nid, NR_ACTIVE_ANON)), |
| 108 | nid, K(node_page_state(nid, NR_INACTIVE_ANON)), |
| 109 | nid, K(node_page_state(nid, NR_ACTIVE_FILE)), |
| 110 | nid, K(node_page_state(nid, NR_INACTIVE_FILE)), |
Lee Schermerhorn | 7b85412 | 2008-10-18 20:26:40 -0700 | [diff] [blame] | 111 | nid, K(node_page_state(nid, NR_UNEVICTABLE)), |
Nick Piggin | 5344b7e | 2008-10-18 20:26:51 -0700 | [diff] [blame] | 112 | nid, K(node_page_state(nid, NR_MLOCK)), |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 113 | #ifdef CONFIG_HIGHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | nid, K(i.totalhigh), |
| 115 | nid, K(i.freehigh), |
| 116 | nid, K(i.totalram - i.totalhigh), |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 117 | nid, K(i.freeram - i.freehigh), |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 118 | #endif |
Christoph Lameter | b1e7a8f | 2006-06-30 01:55:39 -0700 | [diff] [blame] | 119 | nid, K(node_page_state(nid, NR_FILE_DIRTY)), |
Christoph Lameter | ce866b3 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 120 | nid, K(node_page_state(nid, NR_WRITEBACK)), |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 121 | nid, K(node_page_state(nid, NR_FILE_PAGES)), |
Christoph Lameter | 65ba55f | 2006-06-30 01:55:34 -0700 | [diff] [blame] | 122 | nid, K(node_page_state(nid, NR_FILE_MAPPED)), |
Christoph Lameter | f3dbd34 | 2006-06-30 01:55:36 -0700 | [diff] [blame] | 123 | nid, K(node_page_state(nid, NR_ANON_PAGES)), |
KOSAKI Motohiro | 4b02108 | 2009-09-21 17:01:33 -0700 | [diff] [blame] | 124 | nid, K(node_page_state(nid, NR_SHMEM)), |
KOSAKI Motohiro | c6a7f57 | 2009-09-21 17:01:32 -0700 | [diff] [blame] | 125 | nid, node_page_state(nid, NR_KERNEL_STACK) * |
| 126 | THREAD_SIZE / 1024, |
Christoph Lameter | df849a1 | 2006-06-30 01:55:38 -0700 | [diff] [blame] | 127 | nid, K(node_page_state(nid, NR_PAGETABLE)), |
Christoph Lameter | fd39fc85 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 128 | nid, K(node_page_state(nid, NR_UNSTABLE_NFS)), |
Christoph Lameter | d2c5e30 | 2006-06-30 01:55:41 -0700 | [diff] [blame] | 129 | nid, K(node_page_state(nid, NR_BOUNCE)), |
Miklos Szeredi | fc3ba69 | 2008-04-30 00:54:38 -0700 | [diff] [blame] | 130 | nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)), |
Christoph Lameter | 972d1a7 | 2006-09-25 23:31:51 -0700 | [diff] [blame] | 131 | nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) + |
| 132 | node_page_state(nid, NR_SLAB_UNRECLAIMABLE)), |
| 133 | nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)), |
| 134 | nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | n += hugetlb_report_node_meminfo(nid, buf + n); |
| 136 | return n; |
| 137 | } |
| 138 | |
| 139 | #undef K |
| 140 | static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL); |
| 141 | |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 142 | static ssize_t node_read_numastat(struct sys_device * dev, |
| 143 | struct sysdev_attribute *attr, char * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return sprintf(buf, |
| 146 | "numa_hit %lu\n" |
| 147 | "numa_miss %lu\n" |
| 148 | "numa_foreign %lu\n" |
| 149 | "interleave_hit %lu\n" |
| 150 | "local_node %lu\n" |
| 151 | "other_node %lu\n", |
Christoph Lameter | ca889e6 | 2006-06-30 01:55:44 -0700 | [diff] [blame] | 152 | node_page_state(dev->id, NUMA_HIT), |
| 153 | node_page_state(dev->id, NUMA_MISS), |
| 154 | node_page_state(dev->id, NUMA_FOREIGN), |
| 155 | node_page_state(dev->id, NUMA_INTERLEAVE_HIT), |
| 156 | node_page_state(dev->id, NUMA_LOCAL), |
| 157 | node_page_state(dev->id, NUMA_OTHER)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL); |
| 160 | |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 161 | static ssize_t node_read_distance(struct sys_device * dev, |
| 162 | struct sysdev_attribute *attr, char * buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | int nid = dev->id; |
| 165 | int len = 0; |
| 166 | int i; |
| 167 | |
| 168 | /* buf currently PAGE_SIZE, need ~4 chars per node */ |
| 169 | BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2); |
| 170 | |
| 171 | for_each_online_node(i) |
| 172 | len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i)); |
| 173 | |
| 174 | len += sprintf(buf + len, "\n"); |
| 175 | return len; |
| 176 | } |
| 177 | static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL); |
| 178 | |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 179 | #ifdef CONFIG_HUGETLBFS |
| 180 | /* |
| 181 | * hugetlbfs per node attributes registration interface: |
| 182 | * When/if hugetlb[fs] subsystem initializes [sometime after this module], |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 183 | * it will register its per node attributes for all online nodes with |
| 184 | * memory. It will also call register_hugetlbfs_with_node(), below, to |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 185 | * register its attribute registration functions with this node driver. |
| 186 | * Once these hooks have been initialized, the node driver will call into |
| 187 | * the hugetlb module to [un]register attributes for hot-plugged nodes. |
| 188 | */ |
| 189 | static node_registration_func_t __hugetlb_register_node; |
| 190 | static node_registration_func_t __hugetlb_unregister_node; |
| 191 | |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 192 | static inline bool hugetlb_register_node(struct node *node) |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 193 | { |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 194 | if (__hugetlb_register_node && |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 195 | node_state(node->sysdev.id, N_HIGH_MEMORY)) { |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 196 | __hugetlb_register_node(node); |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | return false; |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static inline void hugetlb_unregister_node(struct node *node) |
| 203 | { |
| 204 | if (__hugetlb_unregister_node) |
| 205 | __hugetlb_unregister_node(node); |
| 206 | } |
| 207 | |
| 208 | void register_hugetlbfs_with_node(node_registration_func_t doregister, |
| 209 | node_registration_func_t unregister) |
| 210 | { |
| 211 | __hugetlb_register_node = doregister; |
| 212 | __hugetlb_unregister_node = unregister; |
| 213 | } |
| 214 | #else |
| 215 | static inline void hugetlb_register_node(struct node *node) {} |
| 216 | |
| 217 | static inline void hugetlb_unregister_node(struct node *node) {} |
| 218 | #endif |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | /* |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 222 | * register_node - Setup a sysfs device for a node. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | * @num - Node number to use when creating the device. |
| 224 | * |
| 225 | * Initialize and register the node device. |
| 226 | */ |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 227 | int register_node(struct node *node, int num, struct node *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 229 | int error; |
| 230 | |
| 231 | node->sysdev.id = num; |
| 232 | node->sysdev.cls = &node_class; |
| 233 | error = sysdev_register(&node->sysdev); |
| 234 | |
| 235 | if (!error){ |
| 236 | sysdev_create_file(&node->sysdev, &attr_cpumap); |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 237 | sysdev_create_file(&node->sysdev, &attr_cpulist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | sysdev_create_file(&node->sysdev, &attr_meminfo); |
| 239 | sysdev_create_file(&node->sysdev, &attr_numastat); |
| 240 | sysdev_create_file(&node->sysdev, &attr_distance); |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 241 | |
| 242 | scan_unevictable_register_node(node); |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 243 | |
Lee Schermerhorn | 9a305230 | 2009-12-14 17:58:25 -0800 | [diff] [blame] | 244 | hugetlb_register_node(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | return error; |
| 247 | } |
| 248 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 249 | /** |
| 250 | * unregister_node - unregister a node device |
| 251 | * @node: node going away |
| 252 | * |
| 253 | * Unregisters a node device @node. All the devices on the node must be |
| 254 | * unregistered before calling this function. |
| 255 | */ |
| 256 | void unregister_node(struct node *node) |
| 257 | { |
| 258 | sysdev_remove_file(&node->sysdev, &attr_cpumap); |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 259 | sysdev_remove_file(&node->sysdev, &attr_cpulist); |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 260 | sysdev_remove_file(&node->sysdev, &attr_meminfo); |
| 261 | sysdev_remove_file(&node->sysdev, &attr_numastat); |
| 262 | sysdev_remove_file(&node->sysdev, &attr_distance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 264 | scan_unevictable_unregister_node(node); |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 265 | hugetlb_unregister_node(node); /* no-op, if memoryless node */ |
Lee Schermerhorn | af936a1 | 2008-10-18 20:26:53 -0700 | [diff] [blame] | 266 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 267 | sysdev_unregister(&node->sysdev); |
| 268 | } |
| 269 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 270 | struct node node_devices[MAX_NUMNODES]; |
| 271 | |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 272 | /* |
| 273 | * register cpu under node |
| 274 | */ |
| 275 | int register_cpu_under_node(unsigned int cpu, unsigned int nid) |
| 276 | { |
Alex Chiang | 1830794 | 2009-12-14 17:59:08 -0800 | [diff] [blame] | 277 | int ret; |
Alex Chiang | f8246f3 | 2009-12-14 17:59:06 -0800 | [diff] [blame] | 278 | struct sys_device *obj; |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 279 | |
Alex Chiang | f8246f3 | 2009-12-14 17:59:06 -0800 | [diff] [blame] | 280 | if (!node_online(nid)) |
| 281 | return 0; |
| 282 | |
| 283 | obj = get_cpu_sysdev(cpu); |
| 284 | if (!obj) |
| 285 | return 0; |
| 286 | |
Alex Chiang | 1830794 | 2009-12-14 17:59:08 -0800 | [diff] [blame] | 287 | ret = sysfs_create_link(&node_devices[nid].sysdev.kobj, |
Alex Chiang | f8246f3 | 2009-12-14 17:59:06 -0800 | [diff] [blame] | 288 | &obj->kobj, |
| 289 | kobject_name(&obj->kobj)); |
Alex Chiang | 1830794 | 2009-12-14 17:59:08 -0800 | [diff] [blame] | 290 | if (ret) |
| 291 | return ret; |
| 292 | |
| 293 | return sysfs_create_link(&obj->kobj, |
| 294 | &node_devices[nid].sysdev.kobj, |
| 295 | kobject_name(&node_devices[nid].sysdev.kobj)); |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | int unregister_cpu_under_node(unsigned int cpu, unsigned int nid) |
| 299 | { |
Alex Chiang | b9d52da | 2009-12-14 17:59:07 -0800 | [diff] [blame] | 300 | struct sys_device *obj; |
| 301 | |
| 302 | if (!node_online(nid)) |
| 303 | return 0; |
| 304 | |
| 305 | obj = get_cpu_sysdev(cpu); |
| 306 | if (!obj) |
| 307 | return 0; |
| 308 | |
| 309 | sysfs_remove_link(&node_devices[nid].sysdev.kobj, |
| 310 | kobject_name(&obj->kobj)); |
Alex Chiang | 1830794 | 2009-12-14 17:59:08 -0800 | [diff] [blame] | 311 | sysfs_remove_link(&obj->kobj, |
| 312 | kobject_name(&node_devices[nid].sysdev.kobj)); |
Alex Chiang | b9d52da | 2009-12-14 17:59:07 -0800 | [diff] [blame] | 313 | |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 317 | #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE |
| 318 | #define page_initialized(page) (page->lru.next) |
| 319 | |
| 320 | static int get_nid_for_pfn(unsigned long pfn) |
| 321 | { |
| 322 | struct page *page; |
| 323 | |
| 324 | if (!pfn_valid_within(pfn)) |
| 325 | return -1; |
| 326 | page = pfn_to_page(pfn); |
| 327 | if (!page_initialized(page)) |
| 328 | return -1; |
| 329 | return pfn_to_nid(pfn); |
| 330 | } |
| 331 | |
| 332 | /* register memory section under specified node if it spans that node */ |
| 333 | int register_mem_sect_under_node(struct memory_block *mem_blk, int nid) |
| 334 | { |
Alex Chiang | dee5d0d | 2009-12-14 17:59:05 -0800 | [diff] [blame] | 335 | int ret; |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 336 | unsigned long pfn, sect_start_pfn, sect_end_pfn; |
| 337 | |
| 338 | if (!mem_blk) |
| 339 | return -EFAULT; |
| 340 | if (!node_online(nid)) |
| 341 | return 0; |
| 342 | sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index); |
| 343 | sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1; |
| 344 | for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { |
| 345 | int page_nid; |
| 346 | |
| 347 | page_nid = get_nid_for_pfn(pfn); |
| 348 | if (page_nid < 0) |
| 349 | continue; |
| 350 | if (page_nid != nid) |
| 351 | continue; |
Alex Chiang | dee5d0d | 2009-12-14 17:59:05 -0800 | [diff] [blame] | 352 | ret = sysfs_create_link_nowarn(&node_devices[nid].sysdev.kobj, |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 353 | &mem_blk->sysdev.kobj, |
| 354 | kobject_name(&mem_blk->sysdev.kobj)); |
Alex Chiang | dee5d0d | 2009-12-14 17:59:05 -0800 | [diff] [blame] | 355 | if (ret) |
| 356 | return ret; |
| 357 | |
| 358 | return sysfs_create_link_nowarn(&mem_blk->sysdev.kobj, |
| 359 | &node_devices[nid].sysdev.kobj, |
| 360 | kobject_name(&node_devices[nid].sysdev.kobj)); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 361 | } |
| 362 | /* mem section does not span the specified node */ |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /* unregister memory section under all nodes that it spans */ |
| 367 | int unregister_mem_sect_under_nodes(struct memory_block *mem_blk) |
| 368 | { |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 369 | NODEMASK_ALLOC(nodemask_t, unlinked_nodes, GFP_KERNEL); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 370 | unsigned long pfn, sect_start_pfn, sect_end_pfn; |
| 371 | |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 372 | if (!mem_blk) { |
| 373 | NODEMASK_FREE(unlinked_nodes); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 374 | return -EFAULT; |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 375 | } |
| 376 | if (!unlinked_nodes) |
| 377 | return -ENOMEM; |
| 378 | nodes_clear(*unlinked_nodes); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 379 | sect_start_pfn = section_nr_to_pfn(mem_blk->phys_index); |
| 380 | sect_end_pfn = sect_start_pfn + PAGES_PER_SECTION - 1; |
| 381 | for (pfn = sect_start_pfn; pfn <= sect_end_pfn; pfn++) { |
Roel Kluin | 4750498 | 2009-03-10 12:55:45 -0700 | [diff] [blame] | 382 | int nid; |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 383 | |
| 384 | nid = get_nid_for_pfn(pfn); |
| 385 | if (nid < 0) |
| 386 | continue; |
| 387 | if (!node_online(nid)) |
| 388 | continue; |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 389 | if (node_test_and_set(nid, *unlinked_nodes)) |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 390 | continue; |
| 391 | sysfs_remove_link(&node_devices[nid].sysdev.kobj, |
| 392 | kobject_name(&mem_blk->sysdev.kobj)); |
Alex Chiang | dee5d0d | 2009-12-14 17:59:05 -0800 | [diff] [blame] | 393 | sysfs_remove_link(&mem_blk->sysdev.kobj, |
| 394 | kobject_name(&node_devices[nid].sysdev.kobj)); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 395 | } |
David Rientjes | 9ae49fa | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 396 | NODEMASK_FREE(unlinked_nodes); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static int link_mem_sections(int nid) |
| 401 | { |
| 402 | unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn; |
| 403 | unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages; |
| 404 | unsigned long pfn; |
| 405 | int err = 0; |
| 406 | |
| 407 | for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { |
| 408 | unsigned long section_nr = pfn_to_section_nr(pfn); |
| 409 | struct mem_section *mem_sect; |
| 410 | struct memory_block *mem_blk; |
| 411 | int ret; |
| 412 | |
| 413 | if (!present_section_nr(section_nr)) |
| 414 | continue; |
| 415 | mem_sect = __nr_to_section(section_nr); |
| 416 | mem_blk = find_memory_block(mem_sect); |
| 417 | ret = register_mem_sect_under_node(mem_blk, nid); |
| 418 | if (!err) |
| 419 | err = ret; |
| 420 | |
| 421 | /* discard ref obtained in find_memory_block() */ |
| 422 | kobject_put(&mem_blk->sysdev.kobj); |
| 423 | } |
| 424 | return err; |
| 425 | } |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 426 | |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 427 | #ifdef CONFIG_HUGETLBFS |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 428 | /* |
| 429 | * Handle per node hstate attribute [un]registration on transistions |
| 430 | * to/from memoryless state. |
| 431 | */ |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 432 | static void node_hugetlb_work(struct work_struct *work) |
| 433 | { |
| 434 | struct node *node = container_of(work, struct node, node_work); |
| 435 | |
| 436 | /* |
| 437 | * We only get here when a node transitions to/from memoryless state. |
| 438 | * We can detect which transition occurred by examining whether the |
| 439 | * node has memory now. hugetlb_register_node() already check this |
| 440 | * so we try to register the attributes. If that fails, then the |
| 441 | * node has transitioned to memoryless, try to unregister the |
| 442 | * attributes. |
| 443 | */ |
| 444 | if (!hugetlb_register_node(node)) |
| 445 | hugetlb_unregister_node(node); |
| 446 | } |
| 447 | |
| 448 | static void init_node_hugetlb_work(int nid) |
| 449 | { |
| 450 | INIT_WORK(&node_devices[nid].node_work, node_hugetlb_work); |
| 451 | } |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 452 | |
| 453 | static int node_memory_callback(struct notifier_block *self, |
| 454 | unsigned long action, void *arg) |
| 455 | { |
| 456 | struct memory_notify *mnb = arg; |
| 457 | int nid = mnb->status_change_nid; |
| 458 | |
| 459 | switch (action) { |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 460 | case MEM_ONLINE: |
| 461 | case MEM_OFFLINE: |
| 462 | /* |
| 463 | * offload per node hstate [un]registration to a work thread |
| 464 | * when transitioning to/from memoryless state. |
| 465 | */ |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 466 | if (nid != NUMA_NO_NODE) |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 467 | schedule_work(&node_devices[nid].node_work); |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 468 | break; |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 469 | |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 470 | case MEM_GOING_ONLINE: |
| 471 | case MEM_GOING_OFFLINE: |
| 472 | case MEM_CANCEL_ONLINE: |
| 473 | case MEM_CANCEL_OFFLINE: |
| 474 | default: |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | return NOTIFY_OK; |
| 479 | } |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 480 | #endif /* CONFIG_HUGETLBFS */ |
| 481 | #else /* !CONFIG_MEMORY_HOTPLUG_SPARSE */ |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 482 | |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 483 | static int link_mem_sections(int nid) { return 0; } |
| 484 | #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ |
| 485 | |
| 486 | #if !defined(CONFIG_MEMORY_HOTPLUG_SPARSE) || \ |
| 487 | !defined(CONFIG_HUGETLBFS) |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 488 | static inline int node_memory_callback(struct notifier_block *self, |
| 489 | unsigned long action, void *arg) |
| 490 | { |
| 491 | return NOTIFY_OK; |
| 492 | } |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 493 | |
| 494 | static void init_node_hugetlb_work(int nid) { } |
| 495 | |
| 496 | #endif |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 497 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 498 | int register_one_node(int nid) |
| 499 | { |
| 500 | int error = 0; |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 501 | int cpu; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 502 | |
| 503 | if (node_online(nid)) { |
| 504 | int p_node = parent_node(nid); |
| 505 | struct node *parent = NULL; |
| 506 | |
| 507 | if (p_node != nid) |
| 508 | parent = &node_devices[p_node]; |
| 509 | |
| 510 | error = register_node(&node_devices[nid], nid, parent); |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 511 | |
| 512 | /* link cpu under this node */ |
| 513 | for_each_present_cpu(cpu) { |
| 514 | if (cpu_to_node(cpu) == nid) |
| 515 | register_cpu_under_node(cpu, nid); |
| 516 | } |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 517 | |
| 518 | /* link memory sections under this node */ |
| 519 | error = link_mem_sections(nid); |
Lee Schermerhorn | 39da08c | 2009-12-14 17:58:36 -0800 | [diff] [blame] | 520 | |
| 521 | /* initialize work queue for memory hot plug */ |
| 522 | init_node_hugetlb_work(nid); |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return error; |
| 526 | |
| 527 | } |
| 528 | |
| 529 | void unregister_one_node(int nid) |
| 530 | { |
| 531 | unregister_node(&node_devices[nid]); |
| 532 | } |
| 533 | |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 534 | /* |
| 535 | * node states attributes |
| 536 | */ |
| 537 | |
| 538 | static ssize_t print_nodes_state(enum node_states state, char *buf) |
| 539 | { |
| 540 | int n; |
| 541 | |
| 542 | n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]); |
| 543 | if (n > 0 && PAGE_SIZE > n + 1) { |
| 544 | *(buf + n++) = '\n'; |
| 545 | *(buf + n++) = '\0'; |
| 546 | } |
| 547 | return n; |
| 548 | } |
| 549 | |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 550 | struct node_attr { |
| 551 | struct sysdev_class_attribute attr; |
| 552 | enum node_states state; |
| 553 | }; |
| 554 | |
| 555 | static ssize_t show_node_state(struct sysdev_class *class, |
| 556 | struct sysdev_class_attribute *attr, char *buf) |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 557 | { |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 558 | struct node_attr *na = container_of(attr, struct node_attr, attr); |
| 559 | return print_nodes_state(na->state, buf); |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 562 | #define _NODE_ATTR(name, state) \ |
| 563 | { _SYSDEV_CLASS_ATTR(name, 0444, show_node_state, NULL), state } |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 564 | |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 565 | static struct node_attr node_state_attr[] = { |
| 566 | _NODE_ATTR(possible, N_POSSIBLE), |
| 567 | _NODE_ATTR(online, N_ONLINE), |
| 568 | _NODE_ATTR(has_normal_memory, N_NORMAL_MEMORY), |
| 569 | _NODE_ATTR(has_cpu, N_CPU), |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 570 | #ifdef CONFIG_HIGHMEM |
Andi Kleen | b15f562 | 2010-01-05 12:47:59 +0100 | [diff] [blame] | 571 | _NODE_ATTR(has_high_memory, N_HIGH_MEMORY), |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 572 | #endif |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 573 | }; |
| 574 | |
Andi Kleen | 3701cde | 2010-01-05 12:48:04 +0100 | [diff] [blame^] | 575 | static struct sysdev_class_attribute *node_state_attrs[] = { |
| 576 | &node_state_attr[0].attr, |
| 577 | &node_state_attr[1].attr, |
| 578 | &node_state_attr[2].attr, |
| 579 | &node_state_attr[3].attr, |
| 580 | #ifdef CONFIG_HIGHMEM |
| 581 | &node_state_attr[4].attr, |
| 582 | #endif |
| 583 | NULL |
| 584 | }; |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 585 | |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 586 | #define NODE_CALLBACK_PRI 2 /* lower than SLAB */ |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 587 | static int __init register_node_type(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | { |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 589 | int ret; |
| 590 | |
Andi Kleen | 3701cde | 2010-01-05 12:48:04 +0100 | [diff] [blame^] | 591 | BUILD_BUG_ON(ARRAY_SIZE(node_state_attr) != NR_NODE_STATES); |
| 592 | BUILD_BUG_ON(ARRAY_SIZE(node_state_attrs)-1 != NR_NODE_STATES); |
| 593 | |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 594 | ret = sysdev_class_register(&node_class); |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 595 | if (!ret) { |
Lee Schermerhorn | 4faf8d9 | 2009-12-14 17:58:35 -0800 | [diff] [blame] | 596 | hotplug_memory_notifier(node_memory_callback, |
| 597 | NODE_CALLBACK_PRI); |
| 598 | } |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | * Note: we're not going to unregister the node class if we fail |
| 602 | * to register the node state class attribute files. |
| 603 | */ |
| 604 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | postcore_initcall(register_node_type); |