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