]>
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 | |
511c2aba | 229 | memory_block_action(unsigned long phys_index, unsigned long action, int online_type) |
3947be19 | 230 | { |
a16cee10 | 231 | unsigned long start_pfn; |
5409d2cd | 232 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; |
3947be19 | 233 | int ret; |
3947be19 | 234 | |
19c07d5e | 235 | start_pfn = section_nr_to_pfn(phys_index); |
de0ed36a | 236 | |
3947be19 | 237 | switch (action) { |
3d3af6af IC |
238 | case MEM_ONLINE: |
239 | if (!pages_correctly_reserved(start_pfn)) | |
240 | return -EBUSY; | |
241 | ||
242 | ret = online_pages(start_pfn, nr_pages, online_type); | |
243 | break; | |
244 | case MEM_OFFLINE: | |
245 | ret = offline_pages(start_pfn, nr_pages); | |
246 | break; | |
247 | default: | |
248 | WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: " | |
249 | "%ld\n", __func__, phys_index, action, action); | |
250 | ret = -EINVAL; | |
3947be19 | 251 | } |
3947be19 DH |
252 | |
253 | return ret; | |
254 | } | |
255 | ||
dc18d706 | 256 | static int memory_block_change_state(struct memory_block *mem, |
fa2be40f | 257 | unsigned long to_state, unsigned long from_state_req) |
3947be19 | 258 | { |
de0ed36a | 259 | int ret = 0; |
0c2c99b1 | 260 | |
4960e05e RW |
261 | if (mem->state != from_state_req) |
262 | return -EINVAL; | |
3947be19 | 263 | |
0c2c99b1 NF |
264 | if (to_state == MEM_OFFLINE) |
265 | mem->state = MEM_GOING_OFFLINE; | |
266 | ||
fa2be40f SJ |
267 | ret = memory_block_action(mem->start_section_nr, to_state, |
268 | mem->online_type); | |
269 | ||
b2c064b2 | 270 | mem->state = ret ? from_state_req : to_state; |
fa2be40f | 271 | |
4960e05e RW |
272 | return ret; |
273 | } | |
0c2c99b1 | 274 | |
fa2be40f | 275 | /* The device lock serializes operations on memory_subsys_[online|offline] */ |
4960e05e RW |
276 | static int memory_subsys_online(struct device *dev) |
277 | { | |
7315f0cc | 278 | struct memory_block *mem = to_memory_block(dev); |
4960e05e | 279 | int ret; |
3947be19 | 280 | |
fa2be40f SJ |
281 | if (mem->state == MEM_ONLINE) |
282 | return 0; | |
4960e05e | 283 | |
fa2be40f SJ |
284 | /* |
285 | * If we are called from store_mem_state(), online_type will be | |
286 | * set >= 0 Otherwise we were called from the device online | |
287 | * attribute and need to set the online_type. | |
288 | */ | |
289 | if (mem->online_type < 0) | |
4f7c6b49 | 290 | mem->online_type = MMOP_ONLINE_KEEP; |
4960e05e | 291 | |
fa2be40f | 292 | ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); |
4960e05e | 293 | |
fa2be40f SJ |
294 | /* clear online_type */ |
295 | mem->online_type = -1; | |
4960e05e | 296 | |
4960e05e RW |
297 | return ret; |
298 | } | |
299 | ||
4960e05e | 300 | static int memory_subsys_offline(struct device *dev) |
e90bdb7f | 301 | { |
7315f0cc | 302 | struct memory_block *mem = to_memory_block(dev); |
e90bdb7f | 303 | |
fa2be40f SJ |
304 | if (mem->state == MEM_OFFLINE) |
305 | return 0; | |
e90bdb7f | 306 | |
26bbe7ef SJ |
307 | /* Can't offline block with non-present sections */ |
308 | if (mem->section_count != sections_per_block) | |
309 | return -EINVAL; | |
310 | ||
fa2be40f | 311 | return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE); |
e90bdb7f | 312 | } |
4960e05e | 313 | |
3947be19 | 314 | static ssize_t |
10fbcf4c KS |
315 | store_mem_state(struct device *dev, |
316 | struct device_attribute *attr, const char *buf, size_t count) | |
3947be19 | 317 | { |
7315f0cc | 318 | struct memory_block *mem = to_memory_block(dev); |
fa2be40f | 319 | int ret, online_type; |
3947be19 | 320 | |
5e33bc41 RW |
321 | ret = lock_device_hotplug_sysfs(); |
322 | if (ret) | |
323 | return ret; | |
4960e05e | 324 | |
1f6a6cc8 | 325 | if (sysfs_streq(buf, "online_kernel")) |
4f7c6b49 | 326 | online_type = MMOP_ONLINE_KERNEL; |
1f6a6cc8 | 327 | else if (sysfs_streq(buf, "online_movable")) |
4f7c6b49 | 328 | online_type = MMOP_ONLINE_MOVABLE; |
1f6a6cc8 | 329 | else if (sysfs_streq(buf, "online")) |
4f7c6b49 | 330 | online_type = MMOP_ONLINE_KEEP; |
1f6a6cc8 | 331 | else if (sysfs_streq(buf, "offline")) |
4f7c6b49 | 332 | online_type = MMOP_OFFLINE; |
a37f8630 YI |
333 | else { |
334 | ret = -EINVAL; | |
335 | goto err; | |
336 | } | |
fa2be40f SJ |
337 | |
338 | switch (online_type) { | |
4f7c6b49 TC |
339 | case MMOP_ONLINE_KERNEL: |
340 | case MMOP_ONLINE_MOVABLE: | |
341 | case MMOP_ONLINE_KEEP: | |
48373544 | 342 | /* mem->online_type is protected by device_hotplug_lock */ |
fa2be40f SJ |
343 | mem->online_type = online_type; |
344 | ret = device_online(&mem->dev); | |
345 | break; | |
4f7c6b49 | 346 | case MMOP_OFFLINE: |
fa2be40f SJ |
347 | ret = device_offline(&mem->dev); |
348 | break; | |
349 | default: | |
350 | ret = -EINVAL; /* should never happen */ | |
4960e05e | 351 | } |
4960e05e | 352 | |
a37f8630 | 353 | err: |
4960e05e | 354 | unlock_device_hotplug(); |
0c2c99b1 | 355 | |
d66ba15b | 356 | if (ret < 0) |
3947be19 | 357 | return ret; |
d66ba15b RA |
358 | if (ret) |
359 | return -EINVAL; | |
360 | ||
3947be19 DH |
361 | return count; |
362 | } | |
363 | ||
364 | /* | |
365 | * phys_device is a bad name for this. What I really want | |
366 | * is a way to differentiate between memory ranges that | |
367 | * are part of physical devices that constitute | |
368 | * a complete removable unit or fru. | |
369 | * i.e. do these ranges belong to the same physical device, | |
370 | * s.t. if I offline all of these sections I can then | |
371 | * remove the physical device? | |
372 | */ | |
10fbcf4c KS |
373 | static ssize_t show_phys_device(struct device *dev, |
374 | struct device_attribute *attr, char *buf) | |
3947be19 | 375 | { |
7315f0cc | 376 | struct memory_block *mem = to_memory_block(dev); |
3947be19 DH |
377 | return sprintf(buf, "%d\n", mem->phys_device); |
378 | } | |
379 | ||
ed2f2400 | 380 | #ifdef CONFIG_MEMORY_HOTREMOVE |
e5e68930 MH |
381 | static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn, |
382 | unsigned long nr_pages, int online_type, | |
383 | struct zone *default_zone) | |
384 | { | |
385 | struct zone *zone; | |
386 | ||
e5e68930 MH |
387 | zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages); |
388 | if (zone != default_zone) { | |
389 | strcat(buf, " "); | |
390 | strcat(buf, zone->name); | |
391 | } | |
392 | } | |
393 | ||
ed2f2400 ZZ |
394 | static ssize_t show_valid_zones(struct device *dev, |
395 | struct device_attribute *attr, char *buf) | |
396 | { | |
397 | struct memory_block *mem = to_memory_block(dev); | |
f1dd2cd1 | 398 | unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr); |
ed2f2400 | 399 | unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; |
f1dd2cd1 | 400 | unsigned long valid_start_pfn, valid_end_pfn; |
e5e68930 | 401 | struct zone *default_zone; |
f1dd2cd1 | 402 | int nid; |
ed2f2400 | 403 | |
f1dd2cd1 MH |
404 | /* |
405 | * The block contains more than one zone can not be offlined. | |
406 | * This can happen e.g. for ZONE_DMA and ZONE_DMA32 | |
407 | */ | |
408 | if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages, &valid_start_pfn, &valid_end_pfn)) | |
ed2f2400 ZZ |
409 | return sprintf(buf, "none\n"); |
410 | ||
f1dd2cd1 MH |
411 | start_pfn = valid_start_pfn; |
412 | nr_pages = valid_end_pfn - start_pfn; | |
ed2f2400 | 413 | |
f1dd2cd1 MH |
414 | /* |
415 | * Check the existing zone. Make sure that we do that only on the | |
416 | * online nodes otherwise the page_zone is not reliable | |
417 | */ | |
418 | if (mem->state == MEM_ONLINE) { | |
419 | strcat(buf, page_zone(pfn_to_page(start_pfn))->name); | |
420 | goto out; | |
ed2f2400 ZZ |
421 | } |
422 | ||
f1dd2cd1 | 423 | nid = pfn_to_nid(start_pfn); |
e5e68930 MH |
424 | default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages); |
425 | strcat(buf, default_zone->name); | |
ed2f2400 | 426 | |
e5e68930 MH |
427 | print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL, |
428 | default_zone); | |
429 | print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE, | |
430 | default_zone); | |
f1dd2cd1 | 431 | out: |
a371d9f1 RA |
432 | strcat(buf, "\n"); |
433 | ||
434 | return strlen(buf); | |
ed2f2400 ZZ |
435 | } |
436 | static DEVICE_ATTR(valid_zones, 0444, show_valid_zones, NULL); | |
437 | #endif | |
438 | ||
10fbcf4c | 439 | static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL); |
10fbcf4c KS |
440 | static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state); |
441 | static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL); | |
442 | static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL); | |
3947be19 | 443 | |
3947be19 DH |
444 | /* |
445 | * Block size attribute stuff | |
446 | */ | |
447 | static ssize_t | |
10fbcf4c | 448 | print_block_size(struct device *dev, struct device_attribute *attr, |
8564a6c1 | 449 | char *buf) |
3947be19 | 450 | { |
0c2c99b1 | 451 | return sprintf(buf, "%lx\n", get_memory_block_size()); |
3947be19 DH |
452 | } |
453 | ||
10fbcf4c | 454 | static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL); |
3947be19 | 455 | |
31bc3858 VK |
456 | /* |
457 | * Memory auto online policy. | |
458 | */ | |
459 | ||
460 | static ssize_t | |
461 | show_auto_online_blocks(struct device *dev, struct device_attribute *attr, | |
462 | char *buf) | |
463 | { | |
464 | if (memhp_auto_online) | |
465 | return sprintf(buf, "online\n"); | |
466 | else | |
467 | return sprintf(buf, "offline\n"); | |
468 | } | |
469 | ||
470 | static ssize_t | |
471 | store_auto_online_blocks(struct device *dev, struct device_attribute *attr, | |
472 | const char *buf, size_t count) | |
473 | { | |
474 | if (sysfs_streq(buf, "online")) | |
475 | memhp_auto_online = true; | |
476 | else if (sysfs_streq(buf, "offline")) | |
477 | memhp_auto_online = false; | |
478 | else | |
479 | return -EINVAL; | |
480 | ||
481 | return count; | |
482 | } | |
483 | ||
484 | static DEVICE_ATTR(auto_online_blocks, 0644, show_auto_online_blocks, | |
485 | store_auto_online_blocks); | |
486 | ||
3947be19 DH |
487 | /* |
488 | * Some architectures will have custom drivers to do this, and | |
489 | * will not need to do it from userspace. The fake hot-add code | |
490 | * as well as ppc64 will do all of their discovery in userspace | |
491 | * and will require this interface. | |
492 | */ | |
493 | #ifdef CONFIG_ARCH_MEMORY_PROBE | |
494 | static ssize_t | |
10fbcf4c | 495 | memory_probe_store(struct device *dev, struct device_attribute *attr, |
28812fe1 | 496 | const char *buf, size_t count) |
3947be19 DH |
497 | { |
498 | u64 phys_addr; | |
cb5490a5 | 499 | int nid, ret; |
61b94fea | 500 | unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block; |
3947be19 | 501 | |
b69deb2b ZZ |
502 | ret = kstrtoull(buf, 0, &phys_addr); |
503 | if (ret) | |
504 | return ret; | |
3947be19 | 505 | |
61b94fea AB |
506 | if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1)) |
507 | return -EINVAL; | |
508 | ||
de29654b DH |
509 | ret = lock_device_hotplug_sysfs(); |
510 | if (ret) | |
b69edb14 | 511 | return ret; |
de29654b | 512 | |
cb5490a5 | 513 | nid = memory_add_physaddr_to_nid(phys_addr); |
de29654b DH |
514 | ret = __add_memory(nid, phys_addr, |
515 | MIN_MEMORY_BLOCK_SIZE * sections_per_block); | |
6add7cd6 | 516 | |
cb5490a5 JA |
517 | if (ret) |
518 | goto out; | |
3947be19 | 519 | |
9f0af69b NK |
520 | ret = count; |
521 | out: | |
de29654b | 522 | unlock_device_hotplug(); |
9f0af69b | 523 | return ret; |
3947be19 | 524 | } |
3947be19 | 525 | |
96b2c0fc | 526 | static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store); |
3947be19 DH |
527 | #endif |
528 | ||
facb6011 AK |
529 | #ifdef CONFIG_MEMORY_FAILURE |
530 | /* | |
531 | * Support for offlining pages of memory | |
532 | */ | |
533 | ||
534 | /* Soft offline a page */ | |
535 | static ssize_t | |
10fbcf4c KS |
536 | store_soft_offline_page(struct device *dev, |
537 | struct device_attribute *attr, | |
28812fe1 | 538 | const char *buf, size_t count) |
facb6011 AK |
539 | { |
540 | int ret; | |
541 | u64 pfn; | |
542 | if (!capable(CAP_SYS_ADMIN)) | |
543 | return -EPERM; | |
34da5e67 | 544 | if (kstrtoull(buf, 0, &pfn) < 0) |
facb6011 AK |
545 | return -EINVAL; |
546 | pfn >>= PAGE_SHIFT; | |
547 | if (!pfn_valid(pfn)) | |
548 | return -ENXIO; | |
2cba4b08 DH |
549 | /* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */ |
550 | if (!pfn_to_online_page(pfn)) | |
551 | return -EIO; | |
facb6011 AK |
552 | ret = soft_offline_page(pfn_to_page(pfn), 0); |
553 | return ret == 0 ? count : ret; | |
554 | } | |
555 | ||
556 | /* Forcibly offline a page, including killing processes. */ | |
557 | static ssize_t | |
10fbcf4c KS |
558 | store_hard_offline_page(struct device *dev, |
559 | struct device_attribute *attr, | |
28812fe1 | 560 | const char *buf, size_t count) |
facb6011 AK |
561 | { |
562 | int ret; | |
563 | u64 pfn; | |
564 | if (!capable(CAP_SYS_ADMIN)) | |
565 | return -EPERM; | |
34da5e67 | 566 | if (kstrtoull(buf, 0, &pfn) < 0) |
facb6011 AK |
567 | return -EINVAL; |
568 | pfn >>= PAGE_SHIFT; | |
cd42f4a3 | 569 | ret = memory_failure(pfn, 0, 0); |
facb6011 AK |
570 | return ret ? ret : count; |
571 | } | |
572 | ||
74fef7a8 FB |
573 | static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page); |
574 | static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page); | |
facb6011 AK |
575 | #endif |
576 | ||
3947be19 DH |
577 | /* |
578 | * Note that phys_device is optional. It is here to allow for | |
579 | * differentiation between which *physical* devices each | |
580 | * section belongs to... | |
581 | */ | |
bc32df00 HC |
582 | int __weak arch_get_memory_phys_device(unsigned long start_pfn) |
583 | { | |
584 | return 0; | |
585 | } | |
3947be19 | 586 | |
10fbcf4c KS |
587 | /* |
588 | * A reference for the returned object is held and the reference for the | |
589 | * hinted object is released. | |
590 | */ | |
98383031 RH |
591 | struct memory_block *find_memory_block_hinted(struct mem_section *section, |
592 | struct memory_block *hint) | |
3947be19 | 593 | { |
0c2c99b1 | 594 | int block_id = base_memory_block_id(__section_nr(section)); |
10fbcf4c KS |
595 | struct device *hintdev = hint ? &hint->dev : NULL; |
596 | struct device *dev; | |
3947be19 | 597 | |
10fbcf4c KS |
598 | dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev); |
599 | if (hint) | |
600 | put_device(&hint->dev); | |
601 | if (!dev) | |
3947be19 | 602 | return NULL; |
7315f0cc | 603 | return to_memory_block(dev); |
3947be19 DH |
604 | } |
605 | ||
98383031 RH |
606 | /* |
607 | * For now, we have a linear search to go find the appropriate | |
608 | * memory_block corresponding to a particular phys_index. If | |
609 | * this gets to be a real problem, we can always use a radix | |
610 | * tree or something here. | |
611 | * | |
10fbcf4c | 612 | * This could be made generic for all device subsystems. |
98383031 RH |
613 | */ |
614 | struct memory_block *find_memory_block(struct mem_section *section) | |
615 | { | |
616 | return find_memory_block_hinted(section, NULL); | |
617 | } | |
618 | ||
96b2c0fc NF |
619 | static struct attribute *memory_memblk_attrs[] = { |
620 | &dev_attr_phys_index.attr, | |
96b2c0fc NF |
621 | &dev_attr_state.attr, |
622 | &dev_attr_phys_device.attr, | |
623 | &dev_attr_removable.attr, | |
ed2f2400 ZZ |
624 | #ifdef CONFIG_MEMORY_HOTREMOVE |
625 | &dev_attr_valid_zones.attr, | |
626 | #endif | |
96b2c0fc NF |
627 | NULL |
628 | }; | |
629 | ||
630 | static struct attribute_group memory_memblk_attr_group = { | |
631 | .attrs = memory_memblk_attrs, | |
632 | }; | |
633 | ||
634 | static const struct attribute_group *memory_memblk_attr_groups[] = { | |
635 | &memory_memblk_attr_group, | |
636 | NULL, | |
637 | }; | |
638 | ||
639 | /* | |
640 | * register_memory - Setup a sysfs device for a memory block | |
641 | */ | |
642 | static | |
643 | int register_memory(struct memory_block *memory) | |
644 | { | |
96b2c0fc NF |
645 | memory->dev.bus = &memory_subsys; |
646 | memory->dev.id = memory->start_section_nr / sections_per_block; | |
647 | memory->dev.release = memory_block_release; | |
648 | memory->dev.groups = memory_memblk_attr_groups; | |
f991fae5 | 649 | memory->dev.offline = memory->state == MEM_OFFLINE; |
96b2c0fc | 650 | |
879f1bec | 651 | return device_register(&memory->dev); |
96b2c0fc NF |
652 | } |
653 | ||
0c2c99b1 NF |
654 | static int init_memory_block(struct memory_block **memory, |
655 | struct mem_section *section, unsigned long state) | |
e4619c85 | 656 | { |
0c2c99b1 | 657 | struct memory_block *mem; |
e4619c85 | 658 | unsigned long start_pfn; |
0c2c99b1 | 659 | int scn_nr; |
e4619c85 NF |
660 | int ret = 0; |
661 | ||
0c2c99b1 | 662 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
e4619c85 NF |
663 | if (!mem) |
664 | return -ENOMEM; | |
665 | ||
0c2c99b1 | 666 | scn_nr = __section_nr(section); |
d3360164 NF |
667 | mem->start_section_nr = |
668 | base_memory_block_id(scn_nr) * sections_per_block; | |
669 | mem->end_section_nr = mem->start_section_nr + sections_per_block - 1; | |
e4619c85 | 670 | mem->state = state; |
d3360164 | 671 | start_pfn = section_nr_to_pfn(mem->start_section_nr); |
e4619c85 NF |
672 | mem->phys_device = arch_get_memory_phys_device(start_pfn); |
673 | ||
0c2c99b1 | 674 | ret = register_memory(mem); |
0c2c99b1 NF |
675 | |
676 | *memory = mem; | |
677 | return ret; | |
678 | } | |
679 | ||
cb5e39b8 | 680 | static int add_memory_block(int base_section_nr) |
0c2c99b1 | 681 | { |
cb5e39b8 SJ |
682 | struct memory_block *mem; |
683 | int i, ret, section_count = 0, section_nr; | |
0c2c99b1 | 684 | |
cb5e39b8 SJ |
685 | for (i = base_section_nr; |
686 | (i < base_section_nr + sections_per_block) && i < NR_MEM_SECTIONS; | |
687 | i++) { | |
688 | if (!present_section_nr(i)) | |
689 | continue; | |
690 | if (section_count == 0) | |
691 | section_nr = i; | |
692 | section_count++; | |
e4619c85 NF |
693 | } |
694 | ||
cb5e39b8 SJ |
695 | if (section_count == 0) |
696 | return 0; | |
697 | ret = init_memory_block(&mem, __nr_to_section(section_nr), MEM_ONLINE); | |
698 | if (ret) | |
699 | return ret; | |
700 | mem->section_count = section_count; | |
701 | return 0; | |
e4619c85 NF |
702 | } |
703 | ||
4edd7cef DR |
704 | /* |
705 | * need an interface for the VM to add new memory regions, | |
706 | * but without onlining it. | |
707 | */ | |
708 | int register_new_memory(int nid, struct mem_section *section) | |
709 | { | |
d7f80530 SJ |
710 | int ret = 0; |
711 | struct memory_block *mem; | |
b1eaef3d SJ |
712 | |
713 | mutex_lock(&mem_sysfs_mutex); | |
b1eaef3d | 714 | |
d7f80530 SJ |
715 | mem = find_memory_block(section); |
716 | if (mem) { | |
717 | mem->section_count++; | |
718 | put_device(&mem->dev); | |
719 | } else { | |
720 | ret = init_memory_block(&mem, section, MEM_OFFLINE); | |
721 | if (ret) | |
722 | goto out; | |
56c6b5d3 | 723 | mem->section_count++; |
d7f80530 SJ |
724 | } |
725 | ||
726 | if (mem->section_count == sections_per_block) | |
727 | ret = register_mem_sect_under_node(mem, nid); | |
728 | out: | |
729 | mutex_unlock(&mem_sysfs_mutex); | |
b1eaef3d | 730 | return ret; |
4edd7cef DR |
731 | } |
732 | ||
733 | #ifdef CONFIG_MEMORY_HOTREMOVE | |
734 | static void | |
735 | unregister_memory(struct memory_block *memory) | |
736 | { | |
737 | BUG_ON(memory->dev.bus != &memory_subsys); | |
738 | ||
739 | /* drop the ref. we got in remove_memory_block() */ | |
df2b717c | 740 | put_device(&memory->dev); |
4edd7cef DR |
741 | device_unregister(&memory->dev); |
742 | } | |
743 | ||
cc292b0b | 744 | static int remove_memory_section(unsigned long node_id, |
4edd7cef | 745 | struct mem_section *section, int phys_device) |
3947be19 DH |
746 | { |
747 | struct memory_block *mem; | |
748 | ||
2938ffbd | 749 | mutex_lock(&mem_sysfs_mutex); |
1b862aec MH |
750 | |
751 | /* | |
752 | * Some users of the memory hotplug do not want/need memblock to | |
753 | * track all sections. Skip over those. | |
754 | */ | |
3947be19 | 755 | mem = find_memory_block(section); |
1b862aec MH |
756 | if (!mem) |
757 | goto out_unlock; | |
758 | ||
d3360164 | 759 | unregister_mem_sect_under_nodes(mem, __section_nr(section)); |
07681215 NF |
760 | |
761 | mem->section_count--; | |
96b2c0fc | 762 | if (mem->section_count == 0) |
0c2c99b1 | 763 | unregister_memory(mem); |
96b2c0fc | 764 | else |
df2b717c | 765 | put_device(&mem->dev); |
3947be19 | 766 | |
1b862aec | 767 | out_unlock: |
2938ffbd | 768 | mutex_unlock(&mem_sysfs_mutex); |
3947be19 DH |
769 | return 0; |
770 | } | |
771 | ||
3947be19 DH |
772 | int unregister_memory_section(struct mem_section *section) |
773 | { | |
540557b9 | 774 | if (!present_section(section)) |
3947be19 DH |
775 | return -EINVAL; |
776 | ||
cc292b0b | 777 | return remove_memory_section(0, section, 0); |
3947be19 | 778 | } |
4edd7cef | 779 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
3947be19 | 780 | |
6677e3ea YI |
781 | /* return true if the memory block is offlined, otherwise, return false */ |
782 | bool is_memblock_offlined(struct memory_block *mem) | |
783 | { | |
784 | return mem->state == MEM_OFFLINE; | |
785 | } | |
786 | ||
96b2c0fc NF |
787 | static struct attribute *memory_root_attrs[] = { |
788 | #ifdef CONFIG_ARCH_MEMORY_PROBE | |
789 | &dev_attr_probe.attr, | |
790 | #endif | |
791 | ||
792 | #ifdef CONFIG_MEMORY_FAILURE | |
793 | &dev_attr_soft_offline_page.attr, | |
794 | &dev_attr_hard_offline_page.attr, | |
795 | #endif | |
796 | ||
797 | &dev_attr_block_size_bytes.attr, | |
31bc3858 | 798 | &dev_attr_auto_online_blocks.attr, |
96b2c0fc NF |
799 | NULL |
800 | }; | |
801 | ||
802 | static struct attribute_group memory_root_attr_group = { | |
803 | .attrs = memory_root_attrs, | |
804 | }; | |
805 | ||
806 | static const struct attribute_group *memory_root_attr_groups[] = { | |
807 | &memory_root_attr_group, | |
808 | NULL, | |
809 | }; | |
810 | ||
3947be19 DH |
811 | /* |
812 | * Initialize the sysfs support for memory devices... | |
813 | */ | |
814 | int __init memory_dev_init(void) | |
815 | { | |
816 | unsigned int i; | |
817 | int ret; | |
28ec24e2 | 818 | int err; |
0c2c99b1 | 819 | unsigned long block_sz; |
3947be19 | 820 | |
96b2c0fc | 821 | ret = subsys_system_register(&memory_subsys, memory_root_attr_groups); |
28ec24e2 AM |
822 | if (ret) |
823 | goto out; | |
3947be19 | 824 | |
0c2c99b1 NF |
825 | block_sz = get_memory_block_size(); |
826 | sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; | |
827 | ||
3947be19 DH |
828 | /* |
829 | * Create entries for memory sections that were found | |
830 | * during boot and have been initialized | |
831 | */ | |
b1eaef3d | 832 | mutex_lock(&mem_sysfs_mutex); |
cb5e39b8 | 833 | for (i = 0; i < NR_MEM_SECTIONS; i += sections_per_block) { |
c4e1be9e DH |
834 | /* Don't iterate over sections we know are !present: */ |
835 | if (i > __highest_present_section_nr) | |
836 | break; | |
837 | ||
cb5e39b8 | 838 | err = add_memory_block(i); |
28ec24e2 AM |
839 | if (!ret) |
840 | ret = err; | |
3947be19 | 841 | } |
b1eaef3d | 842 | mutex_unlock(&mem_sysfs_mutex); |
3947be19 | 843 | |
28ec24e2 AM |
844 | out: |
845 | if (ret) | |
2b3a302a | 846 | printk(KERN_ERR "%s() failed: %d\n", __func__, ret); |
3947be19 DH |
847 | return ret; |
848 | } |