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