Linux 3.2.52
[pandora-kernel.git] / drivers / base / memory.c
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>
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>
22 #include <linux/mm.h>
23 #include <linux/mutex.h>
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26
27 #include <linux/atomic.h>
28 #include <asm/uaccess.h>
29
30 static DEFINE_MUTEX(mem_sysfs_mutex);
31
32 #define MEMORY_CLASS_NAME       "memory"
33
34 static int sections_per_block;
35
36 static inline int base_memory_block_id(int section_nr)
37 {
38         return section_nr / sections_per_block;
39 }
40
41 static struct sysdev_class memory_sysdev_class = {
42         .name = MEMORY_CLASS_NAME,
43 };
44
45 static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
46 {
47         return MEMORY_CLASS_NAME;
48 }
49
50 static int memory_uevent(struct kset *kset, struct kobject *obj,
51                         struct kobj_uevent_env *env)
52 {
53         int retval = 0;
54
55         return retval;
56 }
57
58 static const struct kset_uevent_ops memory_uevent_ops = {
59         .name           = memory_uevent_name,
60         .uevent         = memory_uevent,
61 };
62
63 static BLOCKING_NOTIFIER_HEAD(memory_chain);
64
65 int register_memory_notifier(struct notifier_block *nb)
66 {
67         return blocking_notifier_chain_register(&memory_chain, nb);
68 }
69 EXPORT_SYMBOL(register_memory_notifier);
70
71 void unregister_memory_notifier(struct notifier_block *nb)
72 {
73         blocking_notifier_chain_unregister(&memory_chain, nb);
74 }
75 EXPORT_SYMBOL(unregister_memory_notifier);
76
77 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
78
79 int register_memory_isolate_notifier(struct notifier_block *nb)
80 {
81         return atomic_notifier_chain_register(&memory_isolate_chain, nb);
82 }
83 EXPORT_SYMBOL(register_memory_isolate_notifier);
84
85 void unregister_memory_isolate_notifier(struct notifier_block *nb)
86 {
87         atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
88 }
89 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
90
91 /*
92  * register_memory - Setup a sysfs device for a memory block
93  */
94 static
95 int register_memory(struct memory_block *memory)
96 {
97         int error;
98
99         memory->sysdev.cls = &memory_sysdev_class;
100         memory->sysdev.id = memory->start_section_nr / sections_per_block;
101
102         error = sysdev_register(&memory->sysdev);
103         return error;
104 }
105
106 static void
107 unregister_memory(struct memory_block *memory)
108 {
109         BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
110
111         /* drop the ref. we got in remove_memory_block() */
112         kobject_put(&memory->sysdev.kobj);
113         sysdev_unregister(&memory->sysdev);
114 }
115
116 unsigned long __weak memory_block_size_bytes(void)
117 {
118         return MIN_MEMORY_BLOCK_SIZE;
119 }
120
121 static unsigned long get_memory_block_size(void)
122 {
123         unsigned long block_sz;
124
125         block_sz = memory_block_size_bytes();
126
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)) {
129                 WARN_ON(1);
130                 block_sz = MIN_MEMORY_BLOCK_SIZE;
131         }
132
133         return block_sz;
134 }
135
136 /*
137  * use this as the physical section index that this memsection
138  * uses.
139  */
140
141 static ssize_t show_mem_start_phys_index(struct sys_device *dev,
142                         struct sysdev_attribute *attr, char *buf)
143 {
144         struct memory_block *mem =
145                 container_of(dev, struct memory_block, sysdev);
146         unsigned long phys_index;
147
148         phys_index = mem->start_section_nr / sections_per_block;
149         return sprintf(buf, "%08lx\n", phys_index);
150 }
151
152 static ssize_t show_mem_end_phys_index(struct sys_device *dev,
153                         struct sysdev_attribute *attr, char *buf)
154 {
155         struct memory_block *mem =
156                 container_of(dev, struct memory_block, sysdev);
157         unsigned long phys_index;
158
159         phys_index = mem->end_section_nr / sections_per_block;
160         return sprintf(buf, "%08lx\n", phys_index);
161 }
162
163 /*
164  * Show whether the section of memory is likely to be hot-removable
165  */
166 static ssize_t show_mem_removable(struct sys_device *dev,
167                         struct sysdev_attribute *attr, char *buf)
168 {
169         unsigned long i, pfn;
170         int ret = 1;
171         struct memory_block *mem =
172                 container_of(dev, struct memory_block, sysdev);
173
174         for (i = 0; i < sections_per_block; i++) {
175                 if (!present_section_nr(mem->start_section_nr + i))
176                         continue;
177                 pfn = section_nr_to_pfn(mem->start_section_nr + i);
178                 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
179         }
180
181         return sprintf(buf, "%d\n", ret);
182 }
183
184 /*
185  * online, offline, going offline, etc.
186  */
187 static ssize_t show_mem_state(struct sys_device *dev,
188                         struct sysdev_attribute *attr, char *buf)
189 {
190         struct memory_block *mem =
191                 container_of(dev, struct memory_block, sysdev);
192         ssize_t len = 0;
193
194         /*
195          * We can probably put these states in a nice little array
196          * so that they're not open-coded
197          */
198         switch (mem->state) {
199                 case MEM_ONLINE:
200                         len = sprintf(buf, "online\n");
201                         break;
202                 case MEM_OFFLINE:
203                         len = sprintf(buf, "offline\n");
204                         break;
205                 case MEM_GOING_OFFLINE:
206                         len = sprintf(buf, "going-offline\n");
207                         break;
208                 default:
209                         len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
210                                         mem->state);
211                         WARN_ON(1);
212                         break;
213         }
214
215         return len;
216 }
217
218 int memory_notify(unsigned long val, void *v)
219 {
220         return blocking_notifier_call_chain(&memory_chain, val, v);
221 }
222
223 int memory_isolate_notify(unsigned long val, void *v)
224 {
225         return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
226 }
227
228 /*
229  * The probe routines leave the pages reserved, just as the bootmem code does.
230  * Make sure they're still that way.
231  */
232 static bool pages_correctly_reserved(unsigned long start_pfn,
233                                         unsigned long nr_pages)
234 {
235         int i, j;
236         struct page *page;
237         unsigned long pfn = start_pfn;
238
239         /*
240          * memmap between sections is not contiguous except with
241          * SPARSEMEM_VMEMMAP. We lookup the page once per section
242          * and assume memmap is contiguous within each section
243          */
244         for (i = 0; i < sections_per_block; i++, pfn += PAGES_PER_SECTION) {
245                 if (WARN_ON_ONCE(!pfn_valid(pfn)))
246                         return false;
247                 page = pfn_to_page(pfn);
248
249                 for (j = 0; j < PAGES_PER_SECTION; j++) {
250                         if (PageReserved(page + j))
251                                 continue;
252
253                         printk(KERN_WARNING "section number %ld page number %d "
254                                 "not reserved, was it already online?\n",
255                                 pfn_to_section_nr(pfn), j);
256
257                         return false;
258                 }
259         }
260
261         return true;
262 }
263
264 /*
265  * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
266  * OK to have direct references to sparsemem variables in here.
267  */
268 static int
269 memory_block_action(unsigned long phys_index, unsigned long action)
270 {
271         unsigned long start_pfn, start_paddr;
272         unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
273         struct page *first_page;
274         int ret;
275
276         first_page = pfn_to_page(phys_index << PFN_SECTION_SHIFT);
277
278         switch (action) {
279                 case MEM_ONLINE:
280                         start_pfn = page_to_pfn(first_page);
281
282                         if (!pages_correctly_reserved(start_pfn, nr_pages))
283                                 return -EBUSY;
284
285                         ret = online_pages(start_pfn, nr_pages);
286                         break;
287                 case MEM_OFFLINE:
288                         start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
289                         ret = remove_memory(start_paddr,
290                                             nr_pages << PAGE_SHIFT);
291                         break;
292                 default:
293                         WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
294                              "%ld\n", __func__, phys_index, action, action);
295                         ret = -EINVAL;
296         }
297
298         return ret;
299 }
300
301 static int memory_block_change_state(struct memory_block *mem,
302                 unsigned long to_state, unsigned long from_state_req)
303 {
304         int ret = 0;
305
306         mutex_lock(&mem->state_mutex);
307
308         if (mem->state != from_state_req) {
309                 ret = -EINVAL;
310                 goto out;
311         }
312
313         if (to_state == MEM_OFFLINE)
314                 mem->state = MEM_GOING_OFFLINE;
315
316         ret = memory_block_action(mem->start_section_nr, to_state);
317
318         if (ret)
319                 mem->state = from_state_req;
320         else
321                 mem->state = to_state;
322
323 out:
324         mutex_unlock(&mem->state_mutex);
325         return ret;
326 }
327
328 static ssize_t
329 store_mem_state(struct sys_device *dev,
330                 struct sysdev_attribute *attr, const char *buf, size_t count)
331 {
332         struct memory_block *mem;
333         int ret = -EINVAL;
334
335         mem = container_of(dev, struct memory_block, sysdev);
336
337         if (!strncmp(buf, "online", min((int)count, 6)))
338                 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
339         else if(!strncmp(buf, "offline", min((int)count, 7)))
340                 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
341
342         if (ret)
343                 return ret;
344         return count;
345 }
346
347 /*
348  * phys_device is a bad name for this.  What I really want
349  * is a way to differentiate between memory ranges that
350  * are part of physical devices that constitute
351  * a complete removable unit or fru.
352  * i.e. do these ranges belong to the same physical device,
353  * s.t. if I offline all of these sections I can then
354  * remove the physical device?
355  */
356 static ssize_t show_phys_device(struct sys_device *dev,
357                                 struct sysdev_attribute *attr, char *buf)
358 {
359         struct memory_block *mem =
360                 container_of(dev, struct memory_block, sysdev);
361         return sprintf(buf, "%d\n", mem->phys_device);
362 }
363
364 static SYSDEV_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
365 static SYSDEV_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
366 static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
367 static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
368 static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL);
369
370 #define mem_create_simple_file(mem, attr_name)  \
371         sysdev_create_file(&mem->sysdev, &attr_##attr_name)
372 #define mem_remove_simple_file(mem, attr_name)  \
373         sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
374
375 /*
376  * Block size attribute stuff
377  */
378 static ssize_t
379 print_block_size(struct sysdev_class *class, struct sysdev_class_attribute *attr,
380                  char *buf)
381 {
382         return sprintf(buf, "%lx\n", get_memory_block_size());
383 }
384
385 static SYSDEV_CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
386
387 static int block_size_init(void)
388 {
389         return sysfs_create_file(&memory_sysdev_class.kset.kobj,
390                                 &attr_block_size_bytes.attr);
391 }
392
393 /*
394  * Some architectures will have custom drivers to do this, and
395  * will not need to do it from userspace.  The fake hot-add code
396  * as well as ppc64 will do all of their discovery in userspace
397  * and will require this interface.
398  */
399 #ifdef CONFIG_ARCH_MEMORY_PROBE
400 static ssize_t
401 memory_probe_store(struct class *class, struct class_attribute *attr,
402                    const char *buf, size_t count)
403 {
404         u64 phys_addr;
405         int nid;
406         int i, ret;
407         unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
408
409         phys_addr = simple_strtoull(buf, NULL, 0);
410
411         if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
412                 return -EINVAL;
413
414         for (i = 0; i < sections_per_block; i++) {
415                 nid = memory_add_physaddr_to_nid(phys_addr);
416                 ret = add_memory(nid, phys_addr,
417                                  PAGES_PER_SECTION << PAGE_SHIFT);
418                 if (ret)
419                         goto out;
420
421                 phys_addr += MIN_MEMORY_BLOCK_SIZE;
422         }
423
424         ret = count;
425 out:
426         return ret;
427 }
428 static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
429
430 static int memory_probe_init(void)
431 {
432         return sysfs_create_file(&memory_sysdev_class.kset.kobj,
433                                 &class_attr_probe.attr);
434 }
435 #else
436 static inline int memory_probe_init(void)
437 {
438         return 0;
439 }
440 #endif
441
442 #ifdef CONFIG_MEMORY_FAILURE
443 /*
444  * Support for offlining pages of memory
445  */
446
447 /* Soft offline a page */
448 static ssize_t
449 store_soft_offline_page(struct class *class,
450                         struct class_attribute *attr,
451                         const char *buf, size_t count)
452 {
453         int ret;
454         u64 pfn;
455         if (!capable(CAP_SYS_ADMIN))
456                 return -EPERM;
457         if (strict_strtoull(buf, 0, &pfn) < 0)
458                 return -EINVAL;
459         pfn >>= PAGE_SHIFT;
460         if (!pfn_valid(pfn))
461                 return -ENXIO;
462         ret = soft_offline_page(pfn_to_page(pfn), 0);
463         return ret == 0 ? count : ret;
464 }
465
466 /* Forcibly offline a page, including killing processes. */
467 static ssize_t
468 store_hard_offline_page(struct class *class,
469                         struct class_attribute *attr,
470                         const char *buf, size_t count)
471 {
472         int ret;
473         u64 pfn;
474         if (!capable(CAP_SYS_ADMIN))
475                 return -EPERM;
476         if (strict_strtoull(buf, 0, &pfn) < 0)
477                 return -EINVAL;
478         pfn >>= PAGE_SHIFT;
479         ret = __memory_failure(pfn, 0, 0);
480         return ret ? ret : count;
481 }
482
483 static CLASS_ATTR(soft_offline_page, 0644, NULL, store_soft_offline_page);
484 static CLASS_ATTR(hard_offline_page, 0644, NULL, store_hard_offline_page);
485
486 static __init int memory_fail_init(void)
487 {
488         int err;
489
490         err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
491                                 &class_attr_soft_offline_page.attr);
492         if (!err)
493                 err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
494                                 &class_attr_hard_offline_page.attr);
495         return err;
496 }
497 #else
498 static inline int memory_fail_init(void)
499 {
500         return 0;
501 }
502 #endif
503
504 /*
505  * Note that phys_device is optional.  It is here to allow for
506  * differentiation between which *physical* devices each
507  * section belongs to...
508  */
509 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
510 {
511         return 0;
512 }
513
514 struct memory_block *find_memory_block_hinted(struct mem_section *section,
515                                               struct memory_block *hint)
516 {
517         struct kobject *kobj;
518         struct sys_device *sysdev;
519         struct memory_block *mem;
520         char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
521         int block_id = base_memory_block_id(__section_nr(section));
522
523         kobj = hint ? &hint->sysdev.kobj : NULL;
524
525         /*
526          * This only works because we know that section == sysdev->id
527          * slightly redundant with sysdev_register()
528          */
529         sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, block_id);
530
531         kobj = kset_find_obj_hinted(&memory_sysdev_class.kset, name, kobj);
532         if (!kobj)
533                 return NULL;
534
535         sysdev = container_of(kobj, struct sys_device, kobj);
536         mem = container_of(sysdev, struct memory_block, sysdev);
537
538         return mem;
539 }
540
541 /*
542  * For now, we have a linear search to go find the appropriate
543  * memory_block corresponding to a particular phys_index. If
544  * this gets to be a real problem, we can always use a radix
545  * tree or something here.
546  *
547  * This could be made generic for all sysdev classes.
548  */
549 struct memory_block *find_memory_block(struct mem_section *section)
550 {
551         return find_memory_block_hinted(section, NULL);
552 }
553
554 static int init_memory_block(struct memory_block **memory,
555                              struct mem_section *section, unsigned long state)
556 {
557         struct memory_block *mem;
558         unsigned long start_pfn;
559         int scn_nr;
560         int ret = 0;
561
562         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
563         if (!mem)
564                 return -ENOMEM;
565
566         scn_nr = __section_nr(section);
567         mem->start_section_nr =
568                         base_memory_block_id(scn_nr) * sections_per_block;
569         mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
570         mem->state = state;
571         mem->section_count++;
572         mutex_init(&mem->state_mutex);
573         start_pfn = section_nr_to_pfn(mem->start_section_nr);
574         mem->phys_device = arch_get_memory_phys_device(start_pfn);
575
576         ret = register_memory(mem);
577         if (!ret)
578                 ret = mem_create_simple_file(mem, phys_index);
579         if (!ret)
580                 ret = mem_create_simple_file(mem, end_phys_index);
581         if (!ret)
582                 ret = mem_create_simple_file(mem, state);
583         if (!ret)
584                 ret = mem_create_simple_file(mem, phys_device);
585         if (!ret)
586                 ret = mem_create_simple_file(mem, removable);
587
588         *memory = mem;
589         return ret;
590 }
591
592 static int add_memory_section(int nid, struct mem_section *section,
593                         unsigned long state, enum mem_add_context context)
594 {
595         struct memory_block *mem;
596         int ret = 0;
597
598         mutex_lock(&mem_sysfs_mutex);
599
600         mem = find_memory_block(section);
601         if (mem) {
602                 mem->section_count++;
603                 kobject_put(&mem->sysdev.kobj);
604         } else
605                 ret = init_memory_block(&mem, section, state);
606
607         if (!ret) {
608                 if (context == HOTPLUG &&
609                     mem->section_count == sections_per_block)
610                         ret = register_mem_sect_under_node(mem, nid);
611         }
612
613         mutex_unlock(&mem_sysfs_mutex);
614         return ret;
615 }
616
617 int remove_memory_block(unsigned long node_id, struct mem_section *section,
618                 int phys_device)
619 {
620         struct memory_block *mem;
621
622         mutex_lock(&mem_sysfs_mutex);
623         mem = find_memory_block(section);
624         unregister_mem_sect_under_nodes(mem, __section_nr(section));
625
626         mem->section_count--;
627         if (mem->section_count == 0) {
628                 mem_remove_simple_file(mem, phys_index);
629                 mem_remove_simple_file(mem, end_phys_index);
630                 mem_remove_simple_file(mem, state);
631                 mem_remove_simple_file(mem, phys_device);
632                 mem_remove_simple_file(mem, removable);
633                 unregister_memory(mem);
634                 kfree(mem);
635         } else
636                 kobject_put(&mem->sysdev.kobj);
637
638         mutex_unlock(&mem_sysfs_mutex);
639         return 0;
640 }
641
642 /*
643  * need an interface for the VM to add new memory regions,
644  * but without onlining it.
645  */
646 int register_new_memory(int nid, struct mem_section *section)
647 {
648         return add_memory_section(nid, section, MEM_OFFLINE, HOTPLUG);
649 }
650
651 int unregister_memory_section(struct mem_section *section)
652 {
653         if (!present_section(section))
654                 return -EINVAL;
655
656         return remove_memory_block(0, section, 0);
657 }
658
659 /*
660  * Initialize the sysfs support for memory devices...
661  */
662 int __init memory_dev_init(void)
663 {
664         unsigned int i;
665         int ret;
666         int err;
667         unsigned long block_sz;
668
669         memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
670         ret = sysdev_class_register(&memory_sysdev_class);
671         if (ret)
672                 goto out;
673
674         block_sz = get_memory_block_size();
675         sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
676
677         /*
678          * Create entries for memory sections that were found
679          * during boot and have been initialized
680          */
681         for (i = 0; i < NR_MEM_SECTIONS; i++) {
682                 if (!present_section_nr(i))
683                         continue;
684                 err = add_memory_section(0, __nr_to_section(i), MEM_ONLINE,
685                                          BOOT);
686                 if (!ret)
687                         ret = err;
688         }
689
690         err = memory_probe_init();
691         if (!ret)
692                 ret = err;
693         err = memory_fail_init();
694         if (!ret)
695                 ret = err;
696         err = block_size_init();
697         if (!ret)
698                 ret = err;
699 out:
700         if (ret)
701                 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
702         return ret;
703 }