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