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 static DEFINE_MUTEX(mem_sysfs_mutex);
32 #define MEMORY_CLASS_NAME "memory"
33 #define MIN_MEMORY_BLOCK_SIZE (1 << SECTION_SIZE_BITS)
35 static int sections_per_block;
37 static inline int base_memory_block_id(int section_nr)
39 return section_nr / sections_per_block;
42 static struct sysdev_class memory_sysdev_class = {
43 .name = MEMORY_CLASS_NAME,
46 static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
48 return MEMORY_CLASS_NAME;
51 static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
58 static const struct kset_uevent_ops memory_uevent_ops = {
59 .name = memory_uevent_name,
60 .uevent = memory_uevent,
63 static BLOCKING_NOTIFIER_HEAD(memory_chain);
65 int register_memory_notifier(struct notifier_block *nb)
67 return blocking_notifier_chain_register(&memory_chain, nb);
69 EXPORT_SYMBOL(register_memory_notifier);
71 void unregister_memory_notifier(struct notifier_block *nb)
73 blocking_notifier_chain_unregister(&memory_chain, nb);
75 EXPORT_SYMBOL(unregister_memory_notifier);
77 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
79 int register_memory_isolate_notifier(struct notifier_block *nb)
81 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
83 EXPORT_SYMBOL(register_memory_isolate_notifier);
85 void unregister_memory_isolate_notifier(struct notifier_block *nb)
87 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
89 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
92 * register_memory - Setup a sysfs device for a memory block
95 int register_memory(struct memory_block *memory)
99 memory->sysdev.cls = &memory_sysdev_class;
100 memory->sysdev.id = memory->phys_index / sections_per_block;
102 error = sysdev_register(&memory->sysdev);
107 unregister_memory(struct memory_block *memory)
109 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
111 /* drop the ref. we got in remove_memory_block() */
112 kobject_put(&memory->sysdev.kobj);
113 sysdev_unregister(&memory->sysdev);
116 unsigned long __weak memory_block_size_bytes(void)
118 return MIN_MEMORY_BLOCK_SIZE;
121 static unsigned long get_memory_block_size(void)
123 unsigned long block_sz;
125 block_sz = memory_block_size_bytes();
127 /* Validate blk_sz is a power of 2 and not less than section size */
128 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
130 block_sz = MIN_MEMORY_BLOCK_SIZE;
137 * use this as the physical section index that this memsection
141 static ssize_t show_mem_phys_index(struct sys_device *dev,
142 struct sysdev_attribute *attr, char *buf)
144 struct memory_block *mem =
145 container_of(dev, struct memory_block, sysdev);
146 return sprintf(buf, "%08lx\n", mem->phys_index / sections_per_block);
150 * Show whether the section of memory is likely to be hot-removable
152 static ssize_t show_mem_removable(struct sys_device *dev,
153 struct sysdev_attribute *attr, char *buf)
155 unsigned long i, pfn;
157 struct memory_block *mem =
158 container_of(dev, struct memory_block, sysdev);
160 for (i = 0; i < sections_per_block; i++) {
161 pfn = section_nr_to_pfn(mem->phys_index + i);
162 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
165 return sprintf(buf, "%d\n", ret);
169 * online, offline, going offline, etc.
171 static ssize_t show_mem_state(struct sys_device *dev,
172 struct sysdev_attribute *attr, char *buf)
174 struct memory_block *mem =
175 container_of(dev, struct memory_block, sysdev);
179 * We can probably put these states in a nice little array
180 * so that they're not open-coded
182 switch (mem->state) {
184 len = sprintf(buf, "online\n");
187 len = sprintf(buf, "offline\n");
189 case MEM_GOING_OFFLINE:
190 len = sprintf(buf, "going-offline\n");
193 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
202 int memory_notify(unsigned long val, void *v)
204 return blocking_notifier_call_chain(&memory_chain, val, v);
207 int memory_isolate_notify(unsigned long val, void *v)
209 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
213 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
214 * OK to have direct references to sparsemem variables in here.
217 memory_section_action(unsigned long phys_index, unsigned long action)
220 unsigned long start_pfn, start_paddr;
221 struct page *first_page;
224 first_page = pfn_to_page(phys_index << PFN_SECTION_SHIFT);
227 * The probe routines leave the pages reserved, just
228 * as the bootmem code does. Make sure they're still
231 if (action == MEM_ONLINE) {
232 for (i = 0; i < PAGES_PER_SECTION; i++) {
233 if (PageReserved(first_page+i))
236 printk(KERN_WARNING "section number %ld page number %d "
237 "not reserved, was it already online?\n",
245 start_pfn = page_to_pfn(first_page);
246 ret = online_pages(start_pfn, PAGES_PER_SECTION);
249 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
250 ret = remove_memory(start_paddr,
251 PAGES_PER_SECTION << PAGE_SHIFT);
254 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
255 "%ld\n", __func__, phys_index, action, action);
262 static int memory_block_change_state(struct memory_block *mem,
263 unsigned long to_state, unsigned long from_state_req)
267 mutex_lock(&mem->state_mutex);
269 if (mem->state != from_state_req) {
274 if (to_state == MEM_OFFLINE)
275 mem->state = MEM_GOING_OFFLINE;
277 for (i = 0; i < sections_per_block; i++) {
278 ret = memory_section_action(mem->phys_index + i, to_state);
284 for (i = 0; i < sections_per_block; i++)
285 memory_section_action(mem->phys_index + i,
288 mem->state = from_state_req;
290 mem->state = to_state;
293 mutex_unlock(&mem->state_mutex);
298 store_mem_state(struct sys_device *dev,
299 struct sysdev_attribute *attr, const char *buf, size_t count)
301 struct memory_block *mem;
304 mem = container_of(dev, struct memory_block, sysdev);
306 if (!strncmp(buf, "online", min((int)count, 6)))
307 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
308 else if(!strncmp(buf, "offline", min((int)count, 7)))
309 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
317 * phys_device is a bad name for this. What I really want
318 * is a way to differentiate between memory ranges that
319 * are part of physical devices that constitute
320 * a complete removable unit or fru.
321 * i.e. do these ranges belong to the same physical device,
322 * s.t. if I offline all of these sections I can then
323 * remove the physical device?
325 static ssize_t show_phys_device(struct sys_device *dev,
326 struct sysdev_attribute *attr, char *buf)
328 struct memory_block *mem =
329 container_of(dev, struct memory_block, sysdev);
330 return sprintf(buf, "%d\n", mem->phys_device);
333 static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
334 static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
335 static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
336 static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL);
338 #define mem_create_simple_file(mem, attr_name) \
339 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
340 #define mem_remove_simple_file(mem, attr_name) \
341 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
344 * Block size attribute stuff
347 print_block_size(struct sysdev_class *class, struct sysdev_class_attribute *attr,
350 return sprintf(buf, "%lx\n", get_memory_block_size());
353 static SYSDEV_CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
355 static int block_size_init(void)
357 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
358 &attr_block_size_bytes.attr);
362 * Some architectures will have custom drivers to do this, and
363 * will not need to do it from userspace. The fake hot-add code
364 * as well as ppc64 will do all of their discovery in userspace
365 * and will require this interface.
367 #ifdef CONFIG_ARCH_MEMORY_PROBE
369 memory_probe_store(struct class *class, struct class_attribute *attr,
370 const char *buf, size_t count)
376 phys_addr = simple_strtoull(buf, NULL, 0);
378 nid = memory_add_physaddr_to_nid(phys_addr);
379 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
386 static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
388 static int memory_probe_init(void)
390 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
391 &class_attr_probe.attr);
394 static inline int memory_probe_init(void)
400 #ifdef CONFIG_MEMORY_FAILURE
402 * Support for offlining pages of memory
405 /* Soft offline a page */
407 store_soft_offline_page(struct class *class,
408 struct class_attribute *attr,
409 const char *buf, size_t count)
413 if (!capable(CAP_SYS_ADMIN))
415 if (strict_strtoull(buf, 0, &pfn) < 0)
420 ret = soft_offline_page(pfn_to_page(pfn), 0);
421 return ret == 0 ? count : ret;
424 /* Forcibly offline a page, including killing processes. */
426 store_hard_offline_page(struct class *class,
427 struct class_attribute *attr,
428 const char *buf, size_t count)
432 if (!capable(CAP_SYS_ADMIN))
434 if (strict_strtoull(buf, 0, &pfn) < 0)
437 ret = __memory_failure(pfn, 0, 0);
438 return ret ? ret : count;
441 static CLASS_ATTR(soft_offline_page, 0644, NULL, store_soft_offline_page);
442 static CLASS_ATTR(hard_offline_page, 0644, NULL, store_hard_offline_page);
444 static __init int memory_fail_init(void)
448 err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
449 &class_attr_soft_offline_page.attr);
451 err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
452 &class_attr_hard_offline_page.attr);
456 static inline int memory_fail_init(void)
463 * Note that phys_device is optional. It is here to allow for
464 * differentiation between which *physical* devices each
465 * section belongs to...
467 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
472 struct memory_block *find_memory_block_hinted(struct mem_section *section,
473 struct memory_block *hint)
475 struct kobject *kobj;
476 struct sys_device *sysdev;
477 struct memory_block *mem;
478 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
479 int block_id = base_memory_block_id(__section_nr(section));
481 kobj = hint ? &hint->sysdev.kobj : NULL;
484 * This only works because we know that section == sysdev->id
485 * slightly redundant with sysdev_register()
487 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, block_id);
489 kobj = kset_find_obj_hinted(&memory_sysdev_class.kset, name, kobj);
493 sysdev = container_of(kobj, struct sys_device, kobj);
494 mem = container_of(sysdev, struct memory_block, sysdev);
500 * For now, we have a linear search to go find the appropriate
501 * memory_block corresponding to a particular phys_index. If
502 * this gets to be a real problem, we can always use a radix
503 * tree or something here.
505 * This could be made generic for all sysdev classes.
507 struct memory_block *find_memory_block(struct mem_section *section)
509 return find_memory_block_hinted(section, NULL);
512 static int init_memory_block(struct memory_block **memory,
513 struct mem_section *section, unsigned long state)
515 struct memory_block *mem;
516 unsigned long start_pfn;
520 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
524 scn_nr = __section_nr(section);
525 mem->phys_index = base_memory_block_id(scn_nr) * sections_per_block;
527 mem->section_count++;
528 mutex_init(&mem->state_mutex);
529 start_pfn = section_nr_to_pfn(mem->phys_index);
530 mem->phys_device = arch_get_memory_phys_device(start_pfn);
532 ret = register_memory(mem);
534 ret = mem_create_simple_file(mem, phys_index);
536 ret = mem_create_simple_file(mem, state);
538 ret = mem_create_simple_file(mem, phys_device);
540 ret = mem_create_simple_file(mem, removable);
546 static int add_memory_section(int nid, struct mem_section *section,
547 unsigned long state, enum mem_add_context context)
549 struct memory_block *mem;
552 mutex_lock(&mem_sysfs_mutex);
554 mem = find_memory_block(section);
556 mem->section_count++;
557 kobject_put(&mem->sysdev.kobj);
559 ret = init_memory_block(&mem, section, state);
562 if (context == HOTPLUG &&
563 mem->section_count == sections_per_block)
564 ret = register_mem_sect_under_node(mem, nid);
567 mutex_unlock(&mem_sysfs_mutex);
571 int remove_memory_block(unsigned long node_id, struct mem_section *section,
574 struct memory_block *mem;
576 mutex_lock(&mem_sysfs_mutex);
577 mem = find_memory_block(section);
579 mem->section_count--;
580 if (mem->section_count == 0) {
581 unregister_mem_sect_under_nodes(mem);
582 mem_remove_simple_file(mem, phys_index);
583 mem_remove_simple_file(mem, state);
584 mem_remove_simple_file(mem, phys_device);
585 mem_remove_simple_file(mem, removable);
586 unregister_memory(mem);
589 kobject_put(&mem->sysdev.kobj);
591 mutex_unlock(&mem_sysfs_mutex);
596 * need an interface for the VM to add new memory regions,
597 * but without onlining it.
599 int register_new_memory(int nid, struct mem_section *section)
601 return add_memory_section(nid, section, MEM_OFFLINE, HOTPLUG);
604 int unregister_memory_section(struct mem_section *section)
606 if (!present_section(section))
609 return remove_memory_block(0, section, 0);
613 * Initialize the sysfs support for memory devices...
615 int __init memory_dev_init(void)
620 unsigned long block_sz;
622 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
623 ret = sysdev_class_register(&memory_sysdev_class);
627 block_sz = get_memory_block_size();
628 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
631 * Create entries for memory sections that were found
632 * during boot and have been initialized
634 for (i = 0; i < NR_MEM_SECTIONS; i++) {
635 if (!present_section_nr(i))
637 err = add_memory_section(0, __nr_to_section(i), MEM_ONLINE,
643 err = memory_probe_init();
646 err = memory_fail_init();
649 err = block_size_init();
654 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);