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