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