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