2 * drivers/base/memory.c - basic Memory class support
4 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
5 * Dave Hansen <haveblue@us.ibm.com>
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.
13 #include <linux/sysdev.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/topology.h>
17 #include <linux/capability.h>
18 #include <linux/device.h>
19 #include <linux/memory.h>
20 #include <linux/kobject.h>
21 #include <linux/memory_hotplug.h>
23 #include <linux/mutex.h>
24 #include <linux/stat.h>
25 #include <linux/slab.h>
27 #include <asm/atomic.h>
28 #include <asm/uaccess.h>
30 #define MEMORY_CLASS_NAME "memory"
32 static struct sysdev_class memory_sysdev_class = {
33 .name = MEMORY_CLASS_NAME,
36 static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
38 return MEMORY_CLASS_NAME;
41 static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
48 static const struct kset_uevent_ops memory_uevent_ops = {
49 .name = memory_uevent_name,
50 .uevent = memory_uevent,
53 static BLOCKING_NOTIFIER_HEAD(memory_chain);
55 int register_memory_notifier(struct notifier_block *nb)
57 return blocking_notifier_chain_register(&memory_chain, nb);
59 EXPORT_SYMBOL(register_memory_notifier);
61 void unregister_memory_notifier(struct notifier_block *nb)
63 blocking_notifier_chain_unregister(&memory_chain, nb);
65 EXPORT_SYMBOL(unregister_memory_notifier);
67 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
69 int register_memory_isolate_notifier(struct notifier_block *nb)
71 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
73 EXPORT_SYMBOL(register_memory_isolate_notifier);
75 void unregister_memory_isolate_notifier(struct notifier_block *nb)
77 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
79 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
82 * register_memory - Setup a sysfs device for a memory block
85 int register_memory(struct memory_block *memory, struct mem_section *section)
89 memory->sysdev.cls = &memory_sysdev_class;
90 memory->sysdev.id = __section_nr(section);
92 error = sysdev_register(&memory->sysdev);
97 unregister_memory(struct memory_block *memory, struct mem_section *section)
99 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
100 BUG_ON(memory->sysdev.id != __section_nr(section));
102 /* drop the ref. we got in remove_memory_block() */
103 kobject_put(&memory->sysdev.kobj);
104 sysdev_unregister(&memory->sysdev);
108 * use this as the physical section index that this memsection
112 static ssize_t show_mem_phys_index(struct sys_device *dev,
113 struct sysdev_attribute *attr, char *buf)
115 struct memory_block *mem =
116 container_of(dev, struct memory_block, sysdev);
117 return sprintf(buf, "%08lx\n", mem->phys_index);
121 * Show whether the section of memory is likely to be hot-removable
123 static ssize_t show_mem_removable(struct sys_device *dev,
124 struct sysdev_attribute *attr, char *buf)
126 unsigned long start_pfn;
128 struct memory_block *mem =
129 container_of(dev, struct memory_block, sysdev);
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);
137 * online, offline, going offline, etc.
139 static ssize_t show_mem_state(struct sys_device *dev,
140 struct sysdev_attribute *attr, char *buf)
142 struct memory_block *mem =
143 container_of(dev, struct memory_block, sysdev);
147 * We can probably put these states in a nice little array
148 * so that they're not open-coded
150 switch (mem->state) {
152 len = sprintf(buf, "online\n");
155 len = sprintf(buf, "offline\n");
157 case MEM_GOING_OFFLINE:
158 len = sprintf(buf, "going-offline\n");
161 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
170 int memory_notify(unsigned long val, void *v)
172 return blocking_notifier_call_chain(&memory_chain, val, v);
175 int memory_isolate_notify(unsigned long val, void *v)
177 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
181 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
182 * OK to have direct references to sparsemem variables in here.
185 memory_block_action(struct memory_block *mem, unsigned long action)
188 unsigned long psection;
189 unsigned long start_pfn, start_paddr;
190 struct page *first_page;
192 int old_state = mem->state;
194 psection = mem->phys_index;
195 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
198 * The probe routines leave the pages reserved, just
199 * as the bootmem code does. Make sure they're still
202 if (action == MEM_ONLINE) {
203 for (i = 0; i < PAGES_PER_SECTION; i++) {
204 if (PageReserved(first_page+i))
207 printk(KERN_WARNING "section number %ld page number %d "
208 "not reserved, was it already online? \n",
216 start_pfn = page_to_pfn(first_page);
217 ret = online_pages(start_pfn, PAGES_PER_SECTION);
220 mem->state = MEM_GOING_OFFLINE;
221 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
222 ret = remove_memory(start_paddr,
223 PAGES_PER_SECTION << PAGE_SHIFT);
225 mem->state = old_state;
230 WARN(1, KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
231 __func__, mem, action, action);
238 static int memory_block_change_state(struct memory_block *mem,
239 unsigned long to_state, unsigned long from_state_req)
242 mutex_lock(&mem->state_mutex);
244 if (mem->state != from_state_req) {
249 ret = memory_block_action(mem, to_state);
251 mem->state = to_state;
254 mutex_unlock(&mem->state_mutex);
259 store_mem_state(struct sys_device *dev,
260 struct sysdev_attribute *attr, const char *buf, size_t count)
262 struct memory_block *mem;
263 unsigned int phys_section_nr;
266 mem = container_of(dev, struct memory_block, sysdev);
267 phys_section_nr = mem->phys_index;
269 if (!present_section_nr(phys_section_nr))
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);
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?
291 static ssize_t show_phys_device(struct sys_device *dev,
292 struct sysdev_attribute *attr, char *buf)
294 struct memory_block *mem =
295 container_of(dev, struct memory_block, sysdev);
296 return sprintf(buf, "%d\n", mem->phys_device);
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);
302 static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL);
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)
310 * Block size attribute stuff
313 print_block_size(struct sysdev_class *class, struct sysdev_class_attribute *attr,
316 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
319 static SYSDEV_CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
321 static int block_size_init(void)
323 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
324 &attr_block_size_bytes.attr);
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.
333 #ifdef CONFIG_ARCH_MEMORY_PROBE
335 memory_probe_store(struct class *class, struct class_attribute *attr,
336 const char *buf, size_t count)
342 phys_addr = simple_strtoull(buf, NULL, 0);
344 nid = memory_add_physaddr_to_nid(phys_addr);
345 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
352 static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
354 static int memory_probe_init(void)
356 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
357 &class_attr_probe.attr);
360 static inline int memory_probe_init(void)
366 #ifdef CONFIG_MEMORY_FAILURE
368 * Support for offlining pages of memory
371 /* Soft offline a page */
373 store_soft_offline_page(struct class *class,
374 struct class_attribute *attr,
375 const char *buf, size_t count)
379 if (!capable(CAP_SYS_ADMIN))
381 if (strict_strtoull(buf, 0, &pfn) < 0)
386 ret = soft_offline_page(pfn_to_page(pfn), 0);
387 return ret == 0 ? count : ret;
390 /* Forcibly offline a page, including killing processes. */
392 store_hard_offline_page(struct class *class,
393 struct class_attribute *attr,
394 const char *buf, size_t count)
398 if (!capable(CAP_SYS_ADMIN))
400 if (strict_strtoull(buf, 0, &pfn) < 0)
403 ret = __memory_failure(pfn, 0, 0);
404 return ret ? ret : count;
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);
410 static __init int memory_fail_init(void)
414 err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
415 &class_attr_soft_offline_page.attr);
417 err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
418 &class_attr_hard_offline_page.attr);
422 static inline int memory_fail_init(void)
429 * Note that phys_device is optional. It is here to allow for
430 * differentiation between which *physical* devices each
431 * section belongs to...
433 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
438 static int add_memory_block(int nid, struct mem_section *section,
439 unsigned long state, enum mem_add_context context)
441 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
442 unsigned long start_pfn;
448 mem->phys_index = __section_nr(section);
450 mutex_init(&mem->state_mutex);
451 start_pfn = section_nr_to_pfn(mem->phys_index);
452 mem->phys_device = arch_get_memory_phys_device(start_pfn);
454 ret = register_memory(mem, section);
456 ret = mem_create_simple_file(mem, phys_index);
458 ret = mem_create_simple_file(mem, state);
460 ret = mem_create_simple_file(mem, phys_device);
462 ret = mem_create_simple_file(mem, removable);
464 if (context == HOTPLUG)
465 ret = register_mem_sect_under_node(mem, nid);
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.
477 * This could be made generic for all sysdev classes.
479 struct memory_block *find_memory_block(struct mem_section *section)
481 struct kobject *kobj;
482 struct sys_device *sysdev;
483 struct memory_block *mem;
484 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
487 * This only works because we know that section == sysdev->id
488 * slightly redundant with sysdev_register()
490 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
492 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
496 sysdev = container_of(kobj, struct sys_device, kobj);
497 mem = container_of(sysdev, struct memory_block, sysdev);
502 int remove_memory_block(unsigned long node_id, struct mem_section *section,
505 struct memory_block *mem;
507 mem = find_memory_block(section);
508 unregister_mem_sect_under_nodes(mem);
509 mem_remove_simple_file(mem, phys_index);
510 mem_remove_simple_file(mem, state);
511 mem_remove_simple_file(mem, phys_device);
512 mem_remove_simple_file(mem, removable);
513 unregister_memory(mem, section);
519 * need an interface for the VM to add new memory regions,
520 * but without onlining it.
522 int register_new_memory(int nid, struct mem_section *section)
524 return add_memory_block(nid, section, MEM_OFFLINE, HOTPLUG);
527 int unregister_memory_section(struct mem_section *section)
529 if (!present_section(section))
532 return remove_memory_block(0, section, 0);
536 * Initialize the sysfs support for memory devices...
538 int __init memory_dev_init(void)
544 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
545 ret = sysdev_class_register(&memory_sysdev_class);
550 * Create entries for memory sections that were found
551 * during boot and have been initialized
553 for (i = 0; i < NR_MEM_SECTIONS; i++) {
554 if (!present_section_nr(i))
556 err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE,
562 err = memory_probe_init();
565 err = memory_fail_init();
568 err = block_size_init();
573 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);