Linux 3.11-rc7
[deliverable/linux.git] / drivers / base / memory.c
CommitLineData
3947be19 1/*
10fbcf4c 2 * Memory subsystem support
3947be19
DH
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
3947be19
DH
13#include <linux/module.h>
14#include <linux/init.h>
3947be19 15#include <linux/topology.h>
c59ede7b 16#include <linux/capability.h>
3947be19
DH
17#include <linux/device.h>
18#include <linux/memory.h>
19#include <linux/kobject.h>
20#include <linux/memory_hotplug.h>
21#include <linux/mm.h>
da19cbcf 22#include <linux/mutex.h>
9f1b16a5 23#include <linux/stat.h>
5a0e3ad6 24#include <linux/slab.h>
9f1b16a5 25
60063497 26#include <linux/atomic.h>
3947be19
DH
27#include <asm/uaccess.h>
28
2938ffbd
NF
29static DEFINE_MUTEX(mem_sysfs_mutex);
30
3947be19 31#define MEMORY_CLASS_NAME "memory"
0c2c99b1
NF
32
33static int sections_per_block;
34
35static inline int base_memory_block_id(int section_nr)
36{
37 return section_nr / sections_per_block;
38}
3947be19 39
4960e05e
RW
40static int memory_subsys_online(struct device *dev);
41static int memory_subsys_offline(struct device *dev);
42
10fbcf4c 43static struct bus_type memory_subsys = {
af5ca3f4 44 .name = MEMORY_CLASS_NAME,
10fbcf4c 45 .dev_name = MEMORY_CLASS_NAME,
4960e05e
RW
46 .online = memory_subsys_online,
47 .offline = memory_subsys_offline,
3947be19
DH
48};
49
e041c683 50static BLOCKING_NOTIFIER_HEAD(memory_chain);
3947be19 51
98a38ebd 52int register_memory_notifier(struct notifier_block *nb)
3947be19 53{
e041c683 54 return blocking_notifier_chain_register(&memory_chain, nb);
3947be19 55}
3c82c30c 56EXPORT_SYMBOL(register_memory_notifier);
3947be19 57
98a38ebd 58void unregister_memory_notifier(struct notifier_block *nb)
3947be19 59{
e041c683 60 blocking_notifier_chain_unregister(&memory_chain, nb);
3947be19 61}
3c82c30c 62EXPORT_SYMBOL(unregister_memory_notifier);
3947be19 63
925cc71e
RJ
64static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
65
66int register_memory_isolate_notifier(struct notifier_block *nb)
67{
68 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
69}
70EXPORT_SYMBOL(register_memory_isolate_notifier);
71
72void unregister_memory_isolate_notifier(struct notifier_block *nb)
73{
74 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
75}
76EXPORT_SYMBOL(unregister_memory_isolate_notifier);
77
fa7194eb
YI
78static void memory_block_release(struct device *dev)
79{
80 struct memory_block *mem = container_of(dev, struct memory_block, dev);
81
82 kfree(mem);
83}
84
0c2c99b1
NF
85unsigned long __weak memory_block_size_bytes(void)
86{
87 return MIN_MEMORY_BLOCK_SIZE;
88}
89
90static unsigned long get_memory_block_size(void)
91{
92 unsigned long block_sz;
93
94 block_sz = memory_block_size_bytes();
95
96 /* Validate blk_sz is a power of 2 and not less than section size */
97 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
98 WARN_ON(1);
99 block_sz = MIN_MEMORY_BLOCK_SIZE;
100 }
101
102 return block_sz;
103}
104
3947be19
DH
105/*
106 * use this as the physical section index that this memsection
107 * uses.
108 */
109
10fbcf4c
KS
110static ssize_t show_mem_start_phys_index(struct device *dev,
111 struct device_attribute *attr, char *buf)
3947be19
DH
112{
113 struct memory_block *mem =
10fbcf4c 114 container_of(dev, struct memory_block, dev);
d3360164
NF
115 unsigned long phys_index;
116
117 phys_index = mem->start_section_nr / sections_per_block;
118 return sprintf(buf, "%08lx\n", phys_index);
119}
120
10fbcf4c
KS
121static ssize_t show_mem_end_phys_index(struct device *dev,
122 struct device_attribute *attr, char *buf)
d3360164
NF
123{
124 struct memory_block *mem =
10fbcf4c 125 container_of(dev, struct memory_block, dev);
d3360164
NF
126 unsigned long phys_index;
127
128 phys_index = mem->end_section_nr / sections_per_block;
129 return sprintf(buf, "%08lx\n", phys_index);
3947be19
DH
130}
131
5c755e9f
BP
132/*
133 * Show whether the section of memory is likely to be hot-removable
134 */
10fbcf4c
KS
135static ssize_t show_mem_removable(struct device *dev,
136 struct device_attribute *attr, char *buf)
5c755e9f 137{
0c2c99b1
NF
138 unsigned long i, pfn;
139 int ret = 1;
5c755e9f 140 struct memory_block *mem =
10fbcf4c 141 container_of(dev, struct memory_block, dev);
5c755e9f 142
0c2c99b1 143 for (i = 0; i < sections_per_block; i++) {
d3360164 144 pfn = section_nr_to_pfn(mem->start_section_nr + i);
0c2c99b1
NF
145 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
146 }
147
5c755e9f
BP
148 return sprintf(buf, "%d\n", ret);
149}
150
3947be19
DH
151/*
152 * online, offline, going offline, etc.
153 */
10fbcf4c
KS
154static ssize_t show_mem_state(struct device *dev,
155 struct device_attribute *attr, char *buf)
3947be19
DH
156{
157 struct memory_block *mem =
10fbcf4c 158 container_of(dev, struct memory_block, dev);
3947be19
DH
159 ssize_t len = 0;
160
161 /*
162 * We can probably put these states in a nice little array
163 * so that they're not open-coded
164 */
165 switch (mem->state) {
166 case MEM_ONLINE:
167 len = sprintf(buf, "online\n");
168 break;
169 case MEM_OFFLINE:
170 len = sprintf(buf, "offline\n");
171 break;
172 case MEM_GOING_OFFLINE:
173 len = sprintf(buf, "going-offline\n");
174 break;
175 default:
176 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
177 mem->state);
178 WARN_ON(1);
179 break;
180 }
181
182 return len;
183}
184
7b78d335 185int memory_notify(unsigned long val, void *v)
3947be19 186{
e041c683 187 return blocking_notifier_call_chain(&memory_chain, val, v);
3947be19
DH
188}
189
925cc71e
RJ
190int memory_isolate_notify(unsigned long val, void *v)
191{
192 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
193}
194
2bbcb878
MG
195/*
196 * The probe routines leave the pages reserved, just as the bootmem code does.
197 * Make sure they're still that way.
198 */
6056d619 199static bool pages_correctly_reserved(unsigned long start_pfn)
2bbcb878
MG
200{
201 int i, j;
202 struct page *page;
203 unsigned long pfn = start_pfn;
204
205 /*
206 * memmap between sections is not contiguous except with
207 * SPARSEMEM_VMEMMAP. We lookup the page once per section
208 * and assume memmap is contiguous within each section
209 */
210 for (i = 0; i < sections_per_block; i++, pfn += PAGES_PER_SECTION) {
211 if (WARN_ON_ONCE(!pfn_valid(pfn)))
212 return false;
213 page = pfn_to_page(pfn);
214
215 for (j = 0; j < PAGES_PER_SECTION; j++) {
216 if (PageReserved(page + j))
217 continue;
218
219 printk(KERN_WARNING "section number %ld page number %d "
220 "not reserved, was it already online?\n",
221 pfn_to_section_nr(pfn), j);
222
223 return false;
224 }
225 }
226
227 return true;
228}
229
3947be19
DH
230/*
231 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
232 * OK to have direct references to sparsemem variables in here.
233 */
234static int
511c2aba 235memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
3947be19 236{
a16cee10 237 unsigned long start_pfn;
5409d2cd 238 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
de0ed36a 239 struct page *first_page;
3947be19 240 int ret;
3947be19 241
de0ed36a 242 first_page = pfn_to_page(phys_index << PFN_SECTION_SHIFT);
a16cee10 243 start_pfn = page_to_pfn(first_page);
de0ed36a 244
3947be19
DH
245 switch (action) {
246 case MEM_ONLINE:
6056d619 247 if (!pages_correctly_reserved(start_pfn))
2bbcb878
MG
248 return -EBUSY;
249
511c2aba 250 ret = online_pages(start_pfn, nr_pages, online_type);
3947be19
DH
251 break;
252 case MEM_OFFLINE:
a16cee10 253 ret = offline_pages(start_pfn, nr_pages);
3947be19
DH
254 break;
255 default:
0c2c99b1
NF
256 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
257 "%ld\n", __func__, phys_index, action, action);
3947be19
DH
258 ret = -EINVAL;
259 }
3947be19
DH
260
261 return ret;
262}
263
e90bdb7f 264static int __memory_block_change_state(struct memory_block *mem,
511c2aba
LJ
265 unsigned long to_state, unsigned long from_state_req,
266 int online_type)
3947be19 267{
de0ed36a 268 int ret = 0;
0c2c99b1 269
4960e05e
RW
270 if (mem->state != from_state_req)
271 return -EINVAL;
3947be19 272
0c2c99b1
NF
273 if (to_state == MEM_OFFLINE)
274 mem->state = MEM_GOING_OFFLINE;
275
511c2aba 276 ret = memory_block_action(mem->start_section_nr, to_state, online_type);
b2c064b2 277 mem->state = ret ? from_state_req : to_state;
4960e05e
RW
278 return ret;
279}
0c2c99b1 280
4960e05e
RW
281static int memory_subsys_online(struct device *dev)
282{
283 struct memory_block *mem = container_of(dev, struct memory_block, dev);
284 int ret;
3947be19 285
4960e05e
RW
286 mutex_lock(&mem->state_mutex);
287
288 ret = mem->state == MEM_ONLINE ? 0 :
289 __memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE,
b2c064b2 290 ONLINE_KEEP);
4960e05e
RW
291
292 mutex_unlock(&mem->state_mutex);
293 return ret;
294}
295
296static int memory_subsys_offline(struct device *dev)
297{
298 struct memory_block *mem = container_of(dev, struct memory_block, dev);
299 int ret;
300
301 mutex_lock(&mem->state_mutex);
302
303 ret = mem->state == MEM_OFFLINE ? 0 :
304 __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1);
305
306 mutex_unlock(&mem->state_mutex);
307 return ret;
308}
309
310static int __memory_block_change_state_uevent(struct memory_block *mem,
311 unsigned long to_state, unsigned long from_state_req,
312 int online_type)
313{
314 int ret = __memory_block_change_state(mem, to_state, from_state_req,
315 online_type);
316 if (!ret) {
317 switch (mem->state) {
318 case MEM_OFFLINE:
319 kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE);
320 break;
321 case MEM_ONLINE:
322 kobject_uevent(&mem->dev.kobj, KOBJ_ONLINE);
323 break;
324 default:
325 break;
326 }
f5138e42 327 }
3947be19
DH
328 return ret;
329}
330
e90bdb7f 331static int memory_block_change_state(struct memory_block *mem,
511c2aba
LJ
332 unsigned long to_state, unsigned long from_state_req,
333 int online_type)
e90bdb7f
WC
334{
335 int ret;
336
337 mutex_lock(&mem->state_mutex);
4960e05e
RW
338 ret = __memory_block_change_state_uevent(mem, to_state, from_state_req,
339 online_type);
e90bdb7f
WC
340 mutex_unlock(&mem->state_mutex);
341
342 return ret;
343}
3947be19 344static ssize_t
10fbcf4c
KS
345store_mem_state(struct device *dev,
346 struct device_attribute *attr, const char *buf, size_t count)
3947be19
DH
347{
348 struct memory_block *mem;
4960e05e 349 bool offline;
3947be19
DH
350 int ret = -EINVAL;
351
10fbcf4c 352 mem = container_of(dev, struct memory_block, dev);
3947be19 353
4960e05e
RW
354 lock_device_hotplug();
355
356 if (!strncmp(buf, "online_kernel", min_t(int, count, 13))) {
357 offline = false;
511c2aba
LJ
358 ret = memory_block_change_state(mem, MEM_ONLINE,
359 MEM_OFFLINE, ONLINE_KERNEL);
4960e05e
RW
360 } else if (!strncmp(buf, "online_movable", min_t(int, count, 14))) {
361 offline = false;
511c2aba
LJ
362 ret = memory_block_change_state(mem, MEM_ONLINE,
363 MEM_OFFLINE, ONLINE_MOVABLE);
4960e05e
RW
364 } else if (!strncmp(buf, "online", min_t(int, count, 6))) {
365 offline = false;
511c2aba
LJ
366 ret = memory_block_change_state(mem, MEM_ONLINE,
367 MEM_OFFLINE, ONLINE_KEEP);
4960e05e
RW
368 } else if(!strncmp(buf, "offline", min_t(int, count, 7))) {
369 offline = true;
511c2aba
LJ
370 ret = memory_block_change_state(mem, MEM_OFFLINE,
371 MEM_ONLINE, -1);
4960e05e
RW
372 }
373 if (!ret)
374 dev->offline = offline;
375
376 unlock_device_hotplug();
0c2c99b1 377
3947be19
DH
378 if (ret)
379 return ret;
380 return count;
381}
382
383/*
384 * phys_device is a bad name for this. What I really want
385 * is a way to differentiate between memory ranges that
386 * are part of physical devices that constitute
387 * a complete removable unit or fru.
388 * i.e. do these ranges belong to the same physical device,
389 * s.t. if I offline all of these sections I can then
390 * remove the physical device?
391 */
10fbcf4c
KS
392static ssize_t show_phys_device(struct device *dev,
393 struct device_attribute *attr, char *buf)
3947be19
DH
394{
395 struct memory_block *mem =
10fbcf4c 396 container_of(dev, struct memory_block, dev);
3947be19
DH
397 return sprintf(buf, "%d\n", mem->phys_device);
398}
399
10fbcf4c
KS
400static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
401static DEVICE_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
402static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
403static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
404static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
3947be19 405
3947be19
DH
406/*
407 * Block size attribute stuff
408 */
409static ssize_t
10fbcf4c 410print_block_size(struct device *dev, struct device_attribute *attr,
8564a6c1 411 char *buf)
3947be19 412{
0c2c99b1 413 return sprintf(buf, "%lx\n", get_memory_block_size());
3947be19
DH
414}
415
10fbcf4c 416static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
3947be19 417
3947be19
DH
418/*
419 * Some architectures will have custom drivers to do this, and
420 * will not need to do it from userspace. The fake hot-add code
421 * as well as ppc64 will do all of their discovery in userspace
422 * and will require this interface.
423 */
424#ifdef CONFIG_ARCH_MEMORY_PROBE
425static ssize_t
10fbcf4c 426memory_probe_store(struct device *dev, struct device_attribute *attr,
28812fe1 427 const char *buf, size_t count)
3947be19
DH
428{
429 u64 phys_addr;
bc02af93 430 int nid;
6add7cd6 431 int i, ret;
61b94fea 432 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
3947be19
DH
433
434 phys_addr = simple_strtoull(buf, NULL, 0);
435
61b94fea
AB
436 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
437 return -EINVAL;
438
6add7cd6
NF
439 for (i = 0; i < sections_per_block; i++) {
440 nid = memory_add_physaddr_to_nid(phys_addr);
441 ret = add_memory(nid, phys_addr,
442 PAGES_PER_SECTION << PAGE_SHIFT);
443 if (ret)
9f0af69b 444 goto out;
6add7cd6
NF
445
446 phys_addr += MIN_MEMORY_BLOCK_SIZE;
447 }
3947be19 448
9f0af69b
NK
449 ret = count;
450out:
451 return ret;
3947be19 452}
3947be19 453
96b2c0fc 454static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
3947be19
DH
455#endif
456
facb6011
AK
457#ifdef CONFIG_MEMORY_FAILURE
458/*
459 * Support for offlining pages of memory
460 */
461
462/* Soft offline a page */
463static ssize_t
10fbcf4c
KS
464store_soft_offline_page(struct device *dev,
465 struct device_attribute *attr,
28812fe1 466 const char *buf, size_t count)
facb6011
AK
467{
468 int ret;
469 u64 pfn;
470 if (!capable(CAP_SYS_ADMIN))
471 return -EPERM;
472 if (strict_strtoull(buf, 0, &pfn) < 0)
473 return -EINVAL;
474 pfn >>= PAGE_SHIFT;
475 if (!pfn_valid(pfn))
476 return -ENXIO;
477 ret = soft_offline_page(pfn_to_page(pfn), 0);
478 return ret == 0 ? count : ret;
479}
480
481/* Forcibly offline a page, including killing processes. */
482static ssize_t
10fbcf4c
KS
483store_hard_offline_page(struct device *dev,
484 struct device_attribute *attr,
28812fe1 485 const char *buf, size_t count)
facb6011
AK
486{
487 int ret;
488 u64 pfn;
489 if (!capable(CAP_SYS_ADMIN))
490 return -EPERM;
491 if (strict_strtoull(buf, 0, &pfn) < 0)
492 return -EINVAL;
493 pfn >>= PAGE_SHIFT;
cd42f4a3 494 ret = memory_failure(pfn, 0, 0);
facb6011
AK
495 return ret ? ret : count;
496}
497
74fef7a8
FB
498static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
499static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
facb6011
AK
500#endif
501
3947be19
DH
502/*
503 * Note that phys_device is optional. It is here to allow for
504 * differentiation between which *physical* devices each
505 * section belongs to...
506 */
bc32df00
HC
507int __weak arch_get_memory_phys_device(unsigned long start_pfn)
508{
509 return 0;
510}
3947be19 511
10fbcf4c
KS
512/*
513 * A reference for the returned object is held and the reference for the
514 * hinted object is released.
515 */
98383031
RH
516struct memory_block *find_memory_block_hinted(struct mem_section *section,
517 struct memory_block *hint)
3947be19 518{
0c2c99b1 519 int block_id = base_memory_block_id(__section_nr(section));
10fbcf4c
KS
520 struct device *hintdev = hint ? &hint->dev : NULL;
521 struct device *dev;
3947be19 522
10fbcf4c
KS
523 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
524 if (hint)
525 put_device(&hint->dev);
526 if (!dev)
3947be19 527 return NULL;
10fbcf4c 528 return container_of(dev, struct memory_block, dev);
3947be19
DH
529}
530
98383031
RH
531/*
532 * For now, we have a linear search to go find the appropriate
533 * memory_block corresponding to a particular phys_index. If
534 * this gets to be a real problem, we can always use a radix
535 * tree or something here.
536 *
10fbcf4c 537 * This could be made generic for all device subsystems.
98383031
RH
538 */
539struct memory_block *find_memory_block(struct mem_section *section)
540{
541 return find_memory_block_hinted(section, NULL);
542}
543
96b2c0fc
NF
544static struct attribute *memory_memblk_attrs[] = {
545 &dev_attr_phys_index.attr,
546 &dev_attr_end_phys_index.attr,
547 &dev_attr_state.attr,
548 &dev_attr_phys_device.attr,
549 &dev_attr_removable.attr,
550 NULL
551};
552
553static struct attribute_group memory_memblk_attr_group = {
554 .attrs = memory_memblk_attrs,
555};
556
557static const struct attribute_group *memory_memblk_attr_groups[] = {
558 &memory_memblk_attr_group,
559 NULL,
560};
561
562/*
563 * register_memory - Setup a sysfs device for a memory block
564 */
565static
566int register_memory(struct memory_block *memory)
567{
568 int error;
569
570 memory->dev.bus = &memory_subsys;
571 memory->dev.id = memory->start_section_nr / sections_per_block;
572 memory->dev.release = memory_block_release;
573 memory->dev.groups = memory_memblk_attr_groups;
f991fae5 574 memory->dev.offline = memory->state == MEM_OFFLINE;
96b2c0fc
NF
575
576 error = device_register(&memory->dev);
577 return error;
578}
579
0c2c99b1
NF
580static int init_memory_block(struct memory_block **memory,
581 struct mem_section *section, unsigned long state)
e4619c85 582{
0c2c99b1 583 struct memory_block *mem;
e4619c85 584 unsigned long start_pfn;
0c2c99b1 585 int scn_nr;
e4619c85
NF
586 int ret = 0;
587
0c2c99b1 588 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
e4619c85
NF
589 if (!mem)
590 return -ENOMEM;
591
0c2c99b1 592 scn_nr = __section_nr(section);
d3360164
NF
593 mem->start_section_nr =
594 base_memory_block_id(scn_nr) * sections_per_block;
595 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
e4619c85 596 mem->state = state;
07681215 597 mem->section_count++;
e4619c85 598 mutex_init(&mem->state_mutex);
d3360164 599 start_pfn = section_nr_to_pfn(mem->start_section_nr);
e4619c85
NF
600 mem->phys_device = arch_get_memory_phys_device(start_pfn);
601
0c2c99b1 602 ret = register_memory(mem);
0c2c99b1
NF
603
604 *memory = mem;
605 return ret;
606}
607
608static int add_memory_section(int nid, struct mem_section *section,
321bf4ed 609 struct memory_block **mem_p,
0c2c99b1
NF
610 unsigned long state, enum mem_add_context context)
611{
321bf4ed
YL
612 struct memory_block *mem = NULL;
613 int scn_nr = __section_nr(section);
0c2c99b1
NF
614 int ret = 0;
615
616 mutex_lock(&mem_sysfs_mutex);
617
321bf4ed
YL
618 if (context == BOOT) {
619 /* same memory block ? */
620 if (mem_p && *mem_p)
621 if (scn_nr >= (*mem_p)->start_section_nr &&
622 scn_nr <= (*mem_p)->end_section_nr) {
623 mem = *mem_p;
624 kobject_get(&mem->dev.kobj);
625 }
626 } else
627 mem = find_memory_block(section);
628
0c2c99b1
NF
629 if (mem) {
630 mem->section_count++;
10fbcf4c 631 kobject_put(&mem->dev.kobj);
321bf4ed 632 } else {
0c2c99b1 633 ret = init_memory_block(&mem, section, state);
321bf4ed
YL
634 /* store memory_block pointer for next loop */
635 if (!ret && context == BOOT)
636 if (mem_p)
637 *mem_p = mem;
638 }
0c2c99b1 639
e4619c85 640 if (!ret) {
0c2c99b1
NF
641 if (context == HOTPLUG &&
642 mem->section_count == sections_per_block)
e4619c85
NF
643 ret = register_mem_sect_under_node(mem, nid);
644 }
645
2938ffbd 646 mutex_unlock(&mem_sysfs_mutex);
e4619c85
NF
647 return ret;
648}
649
4edd7cef
DR
650/*
651 * need an interface for the VM to add new memory regions,
652 * but without onlining it.
653 */
654int register_new_memory(int nid, struct mem_section *section)
655{
656 return add_memory_section(nid, section, NULL, MEM_OFFLINE, HOTPLUG);
657}
658
659#ifdef CONFIG_MEMORY_HOTREMOVE
660static void
661unregister_memory(struct memory_block *memory)
662{
663 BUG_ON(memory->dev.bus != &memory_subsys);
664
665 /* drop the ref. we got in remove_memory_block() */
666 kobject_put(&memory->dev.kobj);
667 device_unregister(&memory->dev);
668}
669
670static int remove_memory_block(unsigned long node_id,
671 struct mem_section *section, int phys_device)
3947be19
DH
672{
673 struct memory_block *mem;
674
2938ffbd 675 mutex_lock(&mem_sysfs_mutex);
3947be19 676 mem = find_memory_block(section);
d3360164 677 unregister_mem_sect_under_nodes(mem, __section_nr(section));
07681215
NF
678
679 mem->section_count--;
96b2c0fc 680 if (mem->section_count == 0)
0c2c99b1 681 unregister_memory(mem);
96b2c0fc 682 else
10fbcf4c 683 kobject_put(&mem->dev.kobj);
3947be19 684
2938ffbd 685 mutex_unlock(&mem_sysfs_mutex);
3947be19
DH
686 return 0;
687}
688
3947be19
DH
689int unregister_memory_section(struct mem_section *section)
690{
540557b9 691 if (!present_section(section))
3947be19
DH
692 return -EINVAL;
693
694 return remove_memory_block(0, section, 0);
695}
4edd7cef 696#endif /* CONFIG_MEMORY_HOTREMOVE */
3947be19 697
6677e3ea
YI
698/* return true if the memory block is offlined, otherwise, return false */
699bool is_memblock_offlined(struct memory_block *mem)
700{
701 return mem->state == MEM_OFFLINE;
702}
703
96b2c0fc
NF
704static struct attribute *memory_root_attrs[] = {
705#ifdef CONFIG_ARCH_MEMORY_PROBE
706 &dev_attr_probe.attr,
707#endif
708
709#ifdef CONFIG_MEMORY_FAILURE
710 &dev_attr_soft_offline_page.attr,
711 &dev_attr_hard_offline_page.attr,
712#endif
713
714 &dev_attr_block_size_bytes.attr,
715 NULL
716};
717
718static struct attribute_group memory_root_attr_group = {
719 .attrs = memory_root_attrs,
720};
721
722static const struct attribute_group *memory_root_attr_groups[] = {
723 &memory_root_attr_group,
724 NULL,
725};
726
3947be19
DH
727/*
728 * Initialize the sysfs support for memory devices...
729 */
730int __init memory_dev_init(void)
731{
732 unsigned int i;
733 int ret;
28ec24e2 734 int err;
0c2c99b1 735 unsigned long block_sz;
321bf4ed 736 struct memory_block *mem = NULL;
3947be19 737
96b2c0fc 738 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
28ec24e2
AM
739 if (ret)
740 goto out;
3947be19 741
0c2c99b1
NF
742 block_sz = get_memory_block_size();
743 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
744
3947be19
DH
745 /*
746 * Create entries for memory sections that were found
747 * during boot and have been initialized
748 */
749 for (i = 0; i < NR_MEM_SECTIONS; i++) {
540557b9 750 if (!present_section_nr(i))
3947be19 751 continue;
321bf4ed
YL
752 /* don't need to reuse memory_block if only one per block */
753 err = add_memory_section(0, __nr_to_section(i),
754 (sections_per_block == 1) ? NULL : &mem,
755 MEM_ONLINE,
0c2c99b1 756 BOOT);
28ec24e2
AM
757 if (!ret)
758 ret = err;
3947be19
DH
759 }
760
28ec24e2
AM
761out:
762 if (ret)
2b3a302a 763 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
3947be19
DH
764 return ret;
765}
This page took 0.775072 seconds and 5 git commands to generate.