Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/memory.c - basic Memory class support |
| 3 | * |
| 4 | * Written by Matt Tolentino <matthew.e.tolentino@intel.com> |
| 5 | * Dave Hansen <haveblue@us.ibm.com> |
| 6 | * |
| 7 | * This file provides the necessary infrastructure to represent |
| 8 | * a SPARSEMEM-memory-model system's physical memory in /sysfs. |
| 9 | * All arch-independent code that assumes MEMORY_HOTPLUG requires |
| 10 | * SPARSEMEM should be contained here, or in mm/memory_hotplug.c. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/sysdev.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 16 | #include <linux/topology.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 17 | #include <linux/capability.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 18 | #include <linux/device.h> |
| 19 | #include <linux/memory.h> |
| 20 | #include <linux/kobject.h> |
| 21 | #include <linux/memory_hotplug.h> |
| 22 | #include <linux/mm.h> |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Shaohua Li | 9f1b16a | 2008-10-18 20:27:12 -0700 | [diff] [blame] | 24 | #include <linux/stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Shaohua Li | 9f1b16a | 2008-10-18 20:27:12 -0700 | [diff] [blame] | 26 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 27 | #include <asm/atomic.h> |
| 28 | #include <asm/uaccess.h> |
| 29 | |
| 30 | #define MEMORY_CLASS_NAME "memory" |
| 31 | |
| 32 | static struct sysdev_class memory_sysdev_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 33 | .name = MEMORY_CLASS_NAME, |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 34 | }; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 35 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 36 | static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 37 | { |
| 38 | return MEMORY_CLASS_NAME; |
| 39 | } |
| 40 | |
Al Viro | 9ec0fd4 | 2007-10-14 06:53:45 +0100 | [diff] [blame] | 41 | static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 42 | { |
| 43 | int retval = 0; |
| 44 | |
| 45 | return retval; |
| 46 | } |
| 47 | |
Emese Revfy | 9cd4361 | 2009-12-31 14:52:51 +0100 | [diff] [blame] | 48 | static const struct kset_uevent_ops memory_uevent_ops = { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 49 | .name = memory_uevent_name, |
| 50 | .uevent = memory_uevent, |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 53 | static BLOCKING_NOTIFIER_HEAD(memory_chain); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 54 | |
Andy Whitcroft | 98a38eb | 2006-01-06 00:10:35 -0800 | [diff] [blame] | 55 | int register_memory_notifier(struct notifier_block *nb) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 56 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 57 | return blocking_notifier_chain_register(&memory_chain, nb); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 58 | } |
Hannes Hering | 3c82c30 | 2008-05-07 14:43:01 +0200 | [diff] [blame] | 59 | EXPORT_SYMBOL(register_memory_notifier); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 60 | |
Andy Whitcroft | 98a38eb | 2006-01-06 00:10:35 -0800 | [diff] [blame] | 61 | void unregister_memory_notifier(struct notifier_block *nb) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 62 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 63 | blocking_notifier_chain_unregister(&memory_chain, nb); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 64 | } |
Hannes Hering | 3c82c30 | 2008-05-07 14:43:01 +0200 | [diff] [blame] | 65 | EXPORT_SYMBOL(unregister_memory_notifier); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 66 | |
Robert Jennings | 925cc71 | 2009-12-17 14:44:38 +0000 | [diff] [blame] | 67 | static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain); |
| 68 | |
| 69 | int register_memory_isolate_notifier(struct notifier_block *nb) |
| 70 | { |
| 71 | return atomic_notifier_chain_register(&memory_isolate_chain, nb); |
| 72 | } |
| 73 | EXPORT_SYMBOL(register_memory_isolate_notifier); |
| 74 | |
| 75 | void unregister_memory_isolate_notifier(struct notifier_block *nb) |
| 76 | { |
| 77 | atomic_notifier_chain_unregister(&memory_isolate_chain, nb); |
| 78 | } |
| 79 | EXPORT_SYMBOL(unregister_memory_isolate_notifier); |
| 80 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 81 | /* |
| 82 | * register_memory - Setup a sysfs device for a memory block |
| 83 | */ |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 84 | static |
| 85 | int register_memory(struct memory_block *memory, struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 86 | { |
| 87 | int error; |
| 88 | |
| 89 | memory->sysdev.cls = &memory_sysdev_class; |
| 90 | memory->sysdev.id = __section_nr(section); |
| 91 | |
| 92 | error = sysdev_register(&memory->sysdev); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 93 | return error; |
| 94 | } |
| 95 | |
| 96 | static void |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 97 | unregister_memory(struct memory_block *memory, struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 98 | { |
| 99 | BUG_ON(memory->sysdev.cls != &memory_sysdev_class); |
| 100 | BUG_ON(memory->sysdev.id != __section_nr(section)); |
| 101 | |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 102 | /* drop the ref. we got in remove_memory_block() */ |
| 103 | kobject_put(&memory->sysdev.kobj); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 104 | sysdev_unregister(&memory->sysdev); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * use this as the physical section index that this memsection |
| 109 | * uses. |
| 110 | */ |
| 111 | |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 112 | static ssize_t show_mem_phys_index(struct sys_device *dev, |
| 113 | struct sysdev_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 114 | { |
| 115 | struct memory_block *mem = |
| 116 | container_of(dev, struct memory_block, sysdev); |
| 117 | return sprintf(buf, "%08lx\n", mem->phys_index); |
| 118 | } |
| 119 | |
| 120 | /* |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 121 | * Show whether the section of memory is likely to be hot-removable |
| 122 | */ |
Stephen Rothwell | 1f07be1 | 2008-07-28 11:05:04 +1000 | [diff] [blame] | 123 | static ssize_t show_mem_removable(struct sys_device *dev, |
| 124 | struct sysdev_attribute *attr, char *buf) |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 125 | { |
| 126 | unsigned long start_pfn; |
| 127 | int ret; |
| 128 | struct memory_block *mem = |
| 129 | container_of(dev, struct memory_block, sysdev); |
| 130 | |
| 131 | start_pfn = section_nr_to_pfn(mem->phys_index); |
| 132 | ret = is_mem_section_removable(start_pfn, PAGES_PER_SECTION); |
| 133 | return sprintf(buf, "%d\n", ret); |
| 134 | } |
| 135 | |
| 136 | /* |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 137 | * online, offline, going offline, etc. |
| 138 | */ |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 139 | static ssize_t show_mem_state(struct sys_device *dev, |
| 140 | struct sysdev_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 141 | { |
| 142 | struct memory_block *mem = |
| 143 | container_of(dev, struct memory_block, sysdev); |
| 144 | ssize_t len = 0; |
| 145 | |
| 146 | /* |
| 147 | * We can probably put these states in a nice little array |
| 148 | * so that they're not open-coded |
| 149 | */ |
| 150 | switch (mem->state) { |
| 151 | case MEM_ONLINE: |
| 152 | len = sprintf(buf, "online\n"); |
| 153 | break; |
| 154 | case MEM_OFFLINE: |
| 155 | len = sprintf(buf, "offline\n"); |
| 156 | break; |
| 157 | case MEM_GOING_OFFLINE: |
| 158 | len = sprintf(buf, "going-offline\n"); |
| 159 | break; |
| 160 | default: |
| 161 | len = sprintf(buf, "ERROR-UNKNOWN-%ld\n", |
| 162 | mem->state); |
| 163 | WARN_ON(1); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | return len; |
| 168 | } |
| 169 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 170 | int memory_notify(unsigned long val, void *v) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 171 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 172 | return blocking_notifier_call_chain(&memory_chain, val, v); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Robert Jennings | 925cc71 | 2009-12-17 14:44:38 +0000 | [diff] [blame] | 175 | int memory_isolate_notify(unsigned long val, void *v) |
| 176 | { |
| 177 | return atomic_notifier_call_chain(&memory_isolate_chain, val, v); |
| 178 | } |
| 179 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 180 | /* |
| 181 | * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is |
| 182 | * OK to have direct references to sparsemem variables in here. |
| 183 | */ |
| 184 | static int |
| 185 | memory_block_action(struct memory_block *mem, unsigned long action) |
| 186 | { |
| 187 | int i; |
| 188 | unsigned long psection; |
| 189 | unsigned long start_pfn, start_paddr; |
| 190 | struct page *first_page; |
| 191 | int ret; |
| 192 | int old_state = mem->state; |
| 193 | |
| 194 | psection = mem->phys_index; |
| 195 | first_page = pfn_to_page(psection << PFN_SECTION_SHIFT); |
| 196 | |
| 197 | /* |
| 198 | * The probe routines leave the pages reserved, just |
| 199 | * as the bootmem code does. Make sure they're still |
| 200 | * that way. |
| 201 | */ |
| 202 | if (action == MEM_ONLINE) { |
| 203 | for (i = 0; i < PAGES_PER_SECTION; i++) { |
| 204 | if (PageReserved(first_page+i)) |
| 205 | continue; |
| 206 | |
| 207 | printk(KERN_WARNING "section number %ld page number %d " |
| 208 | "not reserved, was it already online? \n", |
| 209 | psection, i); |
| 210 | return -EBUSY; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | switch (action) { |
| 215 | case MEM_ONLINE: |
| 216 | start_pfn = page_to_pfn(first_page); |
| 217 | ret = online_pages(start_pfn, PAGES_PER_SECTION); |
| 218 | break; |
| 219 | case MEM_OFFLINE: |
| 220 | mem->state = MEM_GOING_OFFLINE; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 221 | start_paddr = page_to_pfn(first_page) << PAGE_SHIFT; |
| 222 | ret = remove_memory(start_paddr, |
| 223 | PAGES_PER_SECTION << PAGE_SHIFT); |
| 224 | if (ret) { |
| 225 | mem->state = old_state; |
| 226 | break; |
| 227 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 228 | break; |
| 229 | default: |
Arjan van de Ven | f810a5c | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 230 | WARN(1, KERN_WARNING "%s(%p, %ld) unknown action: %ld\n", |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 231 | __func__, mem, action, action); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 232 | ret = -EINVAL; |
| 233 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static int memory_block_change_state(struct memory_block *mem, |
| 239 | unsigned long to_state, unsigned long from_state_req) |
| 240 | { |
| 241 | int ret = 0; |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 242 | mutex_lock(&mem->state_mutex); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 243 | |
| 244 | if (mem->state != from_state_req) { |
| 245 | ret = -EINVAL; |
| 246 | goto out; |
| 247 | } |
| 248 | |
| 249 | ret = memory_block_action(mem, to_state); |
| 250 | if (!ret) |
| 251 | mem->state = to_state; |
| 252 | |
| 253 | out: |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 254 | mutex_unlock(&mem->state_mutex); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | static ssize_t |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 259 | store_mem_state(struct sys_device *dev, |
| 260 | struct sysdev_attribute *attr, const char *buf, size_t count) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 261 | { |
| 262 | struct memory_block *mem; |
| 263 | unsigned int phys_section_nr; |
| 264 | int ret = -EINVAL; |
| 265 | |
| 266 | mem = container_of(dev, struct memory_block, sysdev); |
| 267 | phys_section_nr = mem->phys_index; |
| 268 | |
Andy Whitcroft | 540557b | 2007-10-16 01:24:11 -0700 | [diff] [blame] | 269 | if (!present_section_nr(phys_section_nr)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 270 | goto out; |
| 271 | |
| 272 | if (!strncmp(buf, "online", min((int)count, 6))) |
| 273 | ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); |
| 274 | else if(!strncmp(buf, "offline", min((int)count, 7))) |
| 275 | ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE); |
| 276 | out: |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | return count; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * phys_device is a bad name for this. What I really want |
| 284 | * is a way to differentiate between memory ranges that |
| 285 | * are part of physical devices that constitute |
| 286 | * a complete removable unit or fru. |
| 287 | * i.e. do these ranges belong to the same physical device, |
| 288 | * s.t. if I offline all of these sections I can then |
| 289 | * remove the physical device? |
| 290 | */ |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 291 | static ssize_t show_phys_device(struct sys_device *dev, |
| 292 | struct sysdev_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 293 | { |
| 294 | struct memory_block *mem = |
| 295 | container_of(dev, struct memory_block, sysdev); |
| 296 | return sprintf(buf, "%d\n", mem->phys_device); |
| 297 | } |
| 298 | |
| 299 | static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL); |
| 300 | static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state); |
| 301 | static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 302 | static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 303 | |
| 304 | #define mem_create_simple_file(mem, attr_name) \ |
| 305 | sysdev_create_file(&mem->sysdev, &attr_##attr_name) |
| 306 | #define mem_remove_simple_file(mem, attr_name) \ |
| 307 | sysdev_remove_file(&mem->sysdev, &attr_##attr_name) |
| 308 | |
| 309 | /* |
| 310 | * Block size attribute stuff |
| 311 | */ |
| 312 | static ssize_t |
Andi Kleen | 8564a6c | 2010-01-05 12:48:06 +0100 | [diff] [blame] | 313 | print_block_size(struct sysdev_class *class, struct sysdev_class_attribute *attr, |
| 314 | char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 315 | { |
Linus Torvalds | 4dc86ae | 2010-04-09 10:05:33 -0700 | [diff] [blame] | 316 | return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Andi Kleen | 8564a6c | 2010-01-05 12:48:06 +0100 | [diff] [blame] | 319 | static SYSDEV_CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 320 | |
| 321 | static int block_size_init(void) |
| 322 | { |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 323 | return sysfs_create_file(&memory_sysdev_class.kset.kobj, |
Andi Kleen | 8564a6c | 2010-01-05 12:48:06 +0100 | [diff] [blame] | 324 | &attr_block_size_bytes.attr); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Some architectures will have custom drivers to do this, and |
| 329 | * will not need to do it from userspace. The fake hot-add code |
| 330 | * as well as ppc64 will do all of their discovery in userspace |
| 331 | * and will require this interface. |
| 332 | */ |
| 333 | #ifdef CONFIG_ARCH_MEMORY_PROBE |
| 334 | static ssize_t |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 335 | memory_probe_store(struct class *class, struct class_attribute *attr, |
| 336 | const char *buf, size_t count) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 337 | { |
| 338 | u64 phys_addr; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 339 | int nid; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 340 | int ret; |
| 341 | |
| 342 | phys_addr = simple_strtoull(buf, NULL, 0); |
| 343 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 344 | nid = memory_add_physaddr_to_nid(phys_addr); |
| 345 | ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 346 | |
| 347 | if (ret) |
| 348 | count = ret; |
| 349 | |
| 350 | return count; |
| 351 | } |
Greg Kroah-Hartman | bd79667 | 2010-01-19 13:08:16 -0800 | [diff] [blame] | 352 | static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 353 | |
| 354 | static int memory_probe_init(void) |
| 355 | { |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 356 | return sysfs_create_file(&memory_sysdev_class.kset.kobj, |
Greg Kroah-Hartman | bd79667 | 2010-01-19 13:08:16 -0800 | [diff] [blame] | 357 | &class_attr_probe.attr); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 358 | } |
| 359 | #else |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 360 | static inline int memory_probe_init(void) |
| 361 | { |
| 362 | return 0; |
| 363 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 364 | #endif |
| 365 | |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 366 | #ifdef CONFIG_MEMORY_FAILURE |
| 367 | /* |
| 368 | * Support for offlining pages of memory |
| 369 | */ |
| 370 | |
| 371 | /* Soft offline a page */ |
| 372 | static ssize_t |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 373 | store_soft_offline_page(struct class *class, |
| 374 | struct class_attribute *attr, |
| 375 | const char *buf, size_t count) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 376 | { |
| 377 | int ret; |
| 378 | u64 pfn; |
| 379 | if (!capable(CAP_SYS_ADMIN)) |
| 380 | return -EPERM; |
| 381 | if (strict_strtoull(buf, 0, &pfn) < 0) |
| 382 | return -EINVAL; |
| 383 | pfn >>= PAGE_SHIFT; |
| 384 | if (!pfn_valid(pfn)) |
| 385 | return -ENXIO; |
| 386 | ret = soft_offline_page(pfn_to_page(pfn), 0); |
| 387 | return ret == 0 ? count : ret; |
| 388 | } |
| 389 | |
| 390 | /* Forcibly offline a page, including killing processes. */ |
| 391 | static ssize_t |
Andi Kleen | 28812fe | 2010-01-05 12:48:07 +0100 | [diff] [blame] | 392 | store_hard_offline_page(struct class *class, |
| 393 | struct class_attribute *attr, |
| 394 | const char *buf, size_t count) |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 395 | { |
| 396 | int ret; |
| 397 | u64 pfn; |
| 398 | if (!capable(CAP_SYS_ADMIN)) |
| 399 | return -EPERM; |
| 400 | if (strict_strtoull(buf, 0, &pfn) < 0) |
| 401 | return -EINVAL; |
| 402 | pfn >>= PAGE_SHIFT; |
| 403 | ret = __memory_failure(pfn, 0, 0); |
| 404 | return ret ? ret : count; |
| 405 | } |
| 406 | |
Greg Kroah-Hartman | bd79667 | 2010-01-19 13:08:16 -0800 | [diff] [blame] | 407 | static CLASS_ATTR(soft_offline_page, 0644, NULL, store_soft_offline_page); |
| 408 | static CLASS_ATTR(hard_offline_page, 0644, NULL, store_hard_offline_page); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 409 | |
| 410 | static __init int memory_fail_init(void) |
| 411 | { |
| 412 | int err; |
| 413 | |
| 414 | err = sysfs_create_file(&memory_sysdev_class.kset.kobj, |
Greg Kroah-Hartman | bd79667 | 2010-01-19 13:08:16 -0800 | [diff] [blame] | 415 | &class_attr_soft_offline_page.attr); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 416 | if (!err) |
| 417 | err = sysfs_create_file(&memory_sysdev_class.kset.kobj, |
Greg Kroah-Hartman | bd79667 | 2010-01-19 13:08:16 -0800 | [diff] [blame] | 418 | &class_attr_hard_offline_page.attr); |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 419 | return err; |
| 420 | } |
| 421 | #else |
| 422 | static inline int memory_fail_init(void) |
| 423 | { |
| 424 | return 0; |
| 425 | } |
| 426 | #endif |
| 427 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 428 | /* |
| 429 | * Note that phys_device is optional. It is here to allow for |
| 430 | * differentiation between which *physical* devices each |
| 431 | * section belongs to... |
| 432 | */ |
Heiko Carstens | bc32df0 | 2010-03-15 00:35:03 -0400 | [diff] [blame] | 433 | int __weak arch_get_memory_phys_device(unsigned long start_pfn) |
| 434 | { |
| 435 | return 0; |
| 436 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 437 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 438 | static int add_memory_block(int nid, struct mem_section *section, |
Heiko Carstens | bc32df0 | 2010-03-15 00:35:03 -0400 | [diff] [blame] | 439 | unsigned long state, enum mem_add_context context) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 440 | { |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 441 | struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
Heiko Carstens | bc32df0 | 2010-03-15 00:35:03 -0400 | [diff] [blame] | 442 | unsigned long start_pfn; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 443 | int ret = 0; |
| 444 | |
| 445 | if (!mem) |
| 446 | return -ENOMEM; |
| 447 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 448 | mem->phys_index = __section_nr(section); |
| 449 | mem->state = state; |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 450 | mutex_init(&mem->state_mutex); |
Heiko Carstens | bc32df0 | 2010-03-15 00:35:03 -0400 | [diff] [blame] | 451 | start_pfn = section_nr_to_pfn(mem->phys_index); |
| 452 | mem->phys_device = arch_get_memory_phys_device(start_pfn); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 453 | |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 454 | ret = register_memory(mem, section); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 455 | if (!ret) |
| 456 | ret = mem_create_simple_file(mem, phys_index); |
| 457 | if (!ret) |
| 458 | ret = mem_create_simple_file(mem, state); |
| 459 | if (!ret) |
| 460 | ret = mem_create_simple_file(mem, phys_device); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 461 | if (!ret) |
| 462 | ret = mem_create_simple_file(mem, removable); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 463 | if (!ret) { |
| 464 | if (context == HOTPLUG) |
| 465 | ret = register_mem_sect_under_node(mem, nid); |
| 466 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 467 | |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * For now, we have a linear search to go find the appropriate |
| 473 | * memory_block corresponding to a particular phys_index. If |
| 474 | * this gets to be a real problem, we can always use a radix |
| 475 | * tree or something here. |
| 476 | * |
| 477 | * This could be made generic for all sysdev classes. |
| 478 | */ |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 479 | struct memory_block *find_memory_block(struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 480 | { |
| 481 | struct kobject *kobj; |
| 482 | struct sys_device *sysdev; |
| 483 | struct memory_block *mem; |
| 484 | char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1]; |
| 485 | |
| 486 | /* |
| 487 | * This only works because we know that section == sysdev->id |
| 488 | * slightly redundant with sysdev_register() |
| 489 | */ |
| 490 | sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section)); |
| 491 | |
| 492 | kobj = kset_find_obj(&memory_sysdev_class.kset, name); |
| 493 | if (!kobj) |
| 494 | return NULL; |
| 495 | |
| 496 | sysdev = container_of(kobj, struct sys_device, kobj); |
| 497 | mem = container_of(sysdev, struct memory_block, sysdev); |
| 498 | |
| 499 | return mem; |
| 500 | } |
| 501 | |
| 502 | int remove_memory_block(unsigned long node_id, struct mem_section *section, |
| 503 | int phys_device) |
| 504 | { |
| 505 | struct memory_block *mem; |
| 506 | |
| 507 | mem = find_memory_block(section); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 508 | unregister_mem_sect_under_nodes(mem); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 509 | mem_remove_simple_file(mem, phys_index); |
| 510 | mem_remove_simple_file(mem, state); |
| 511 | mem_remove_simple_file(mem, phys_device); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 512 | mem_remove_simple_file(mem, removable); |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 513 | unregister_memory(mem, section); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * need an interface for the VM to add new memory regions, |
| 520 | * but without onlining it. |
| 521 | */ |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 522 | int register_new_memory(int nid, struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 523 | { |
Heiko Carstens | bc32df0 | 2010-03-15 00:35:03 -0400 | [diff] [blame] | 524 | return add_memory_block(nid, section, MEM_OFFLINE, HOTPLUG); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | int unregister_memory_section(struct mem_section *section) |
| 528 | { |
Andy Whitcroft | 540557b | 2007-10-16 01:24:11 -0700 | [diff] [blame] | 529 | if (!present_section(section)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 530 | return -EINVAL; |
| 531 | |
| 532 | return remove_memory_block(0, section, 0); |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Initialize the sysfs support for memory devices... |
| 537 | */ |
| 538 | int __init memory_dev_init(void) |
| 539 | { |
| 540 | unsigned int i; |
| 541 | int ret; |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 542 | int err; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 543 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 544 | memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 545 | ret = sysdev_class_register(&memory_sysdev_class); |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 546 | if (ret) |
| 547 | goto out; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * Create entries for memory sections that were found |
| 551 | * during boot and have been initialized |
| 552 | */ |
| 553 | for (i = 0; i < NR_MEM_SECTIONS; i++) { |
Andy Whitcroft | 540557b | 2007-10-16 01:24:11 -0700 | [diff] [blame] | 554 | if (!present_section_nr(i)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 555 | continue; |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 556 | err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, |
Heiko Carstens | bc32df0 | 2010-03-15 00:35:03 -0400 | [diff] [blame] | 557 | BOOT); |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 558 | if (!ret) |
| 559 | ret = err; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 562 | err = memory_probe_init(); |
| 563 | if (!ret) |
| 564 | ret = err; |
Andi Kleen | facb601 | 2009-12-16 12:20:00 +0100 | [diff] [blame] | 565 | err = memory_fail_init(); |
| 566 | if (!ret) |
| 567 | ret = err; |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 568 | err = block_size_init(); |
| 569 | if (!ret) |
| 570 | ret = err; |
| 571 | out: |
| 572 | if (ret) |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 573 | printk(KERN_ERR "%s() failed: %d\n", __func__, ret); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 574 | return ret; |
| 575 | } |