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