]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/base/memory.c
drivers/base/memory.c: get rid of find_memory_block_hinted()
[thirdparty/linux.git] / drivers / base / memory.c
CommitLineData
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
29static 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
35static int sections_per_block;
36
90ec010f 37static 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 42static 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
47static inline unsigned long phys_to_block_id(unsigned long phys)
48{
49 return pfn_to_block_id(PFN_DOWN(phys));
50}
51
4960e05e
RW
52static int memory_subsys_online(struct device *dev);
53static int memory_subsys_offline(struct device *dev);
54
10fbcf4c 55static 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 62static BLOCKING_NOTIFIER_HEAD(memory_chain);
3947be19 63
98a38ebd 64int register_memory_notifier(struct notifier_block *nb)
3947be19 65{
2aeebca2 66 return blocking_notifier_chain_register(&memory_chain, nb);
3947be19 67}
3c82c30c 68EXPORT_SYMBOL(register_memory_notifier);
3947be19 69
98a38ebd 70void unregister_memory_notifier(struct notifier_block *nb)
3947be19 71{
2aeebca2 72 blocking_notifier_chain_unregister(&memory_chain, nb);
3947be19 73}
3c82c30c 74EXPORT_SYMBOL(unregister_memory_notifier);
3947be19 75
925cc71e
RJ
76static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
77
78int register_memory_isolate_notifier(struct notifier_block *nb)
79{
80 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
81}
82EXPORT_SYMBOL(register_memory_isolate_notifier);
83
84void unregister_memory_isolate_notifier(struct notifier_block *nb)
85{
86 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
87}
88EXPORT_SYMBOL(unregister_memory_isolate_notifier);
89
fa7194eb
YI
90static 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
97unsigned long __weak memory_block_size_bytes(void)
98{
99 return MIN_MEMORY_BLOCK_SIZE;
100}
c221c0b0 101EXPORT_SYMBOL_GPL(memory_block_size_bytes);
0c2c99b1
NF
102
103static 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
123static 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
136static 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 153out:
5c755e9f
BP
154 return sprintf(buf, "%d\n", ret);
155}
156
3947be19
DH
157/*
158 * online, offline, going offline, etc.
159 */
3f8e9178
DH
160static 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 190int 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
195int 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 205static 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 */
243static int
063b8a4c
BH
244memory_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 272static 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
292static 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 316static 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
330static 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 368err:
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 388static 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
396static 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 409static 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 444out:
a371d9f1
RA
445 strcat(buf, "\n");
446
447 return strlen(buf);
ed2f2400 448}
3f8e9178 449static DEVICE_ATTR_RO(valid_zones);
ed2f2400
ZZ
450#endif
451
3f8e9178
DH
452static DEVICE_ATTR_RO(phys_index);
453static DEVICE_ATTR_RW(state);
454static DEVICE_ATTR_RO(phys_device);
455static DEVICE_ATTR_RO(removable);
3947be19 456
3947be19
DH
457/*
458 * Block size attribute stuff
459 */
3f8e9178
DH
460static 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 466static DEVICE_ATTR_RO(block_size_bytes);
3947be19 467
31bc3858
VK
468/*
469 * Memory auto online policy.
470 */
471
3f8e9178
DH
472static 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
481static 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 495static 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
504static 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;
530out:
8df1d0e4 531 unlock_device_hotplug();
9f0af69b 532 return ret;
3947be19 533}
3947be19 534
3f8e9178 535static 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
544static 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
562static 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
577static DEVICE_ATTR_WO(soft_offline_page);
578static 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
586int __weak arch_get_memory_phys_device(unsigned long start_pfn)
587{
588 return 0;
589}
3947be19 590
dd625285
DH
591/* A reference for the returned memory block device is acquired. */
592static struct memory_block *find_memory_block_by_id(unsigned long block_id)
3947be19 593{
10fbcf4c 594 struct device *dev;
3947be19 595
dd625285
DH
596 dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
597 return dev ? to_memory_block(dev) : NULL;
db051a0d
DH
598}
599
98383031
RH
600/*
601 * For now, we have a linear search to go find the appropriate
602 * memory_block corresponding to a particular phys_index. If
603 * this gets to be a real problem, we can always use a radix
604 * tree or something here.
605 *
10fbcf4c 606 * This could be made generic for all device subsystems.
98383031
RH
607 */
608struct memory_block *find_memory_block(struct mem_section *section)
609{
dd625285
DH
610 unsigned long block_id = base_memory_block_id(__section_nr(section));
611
612 return find_memory_block_by_id(block_id);
98383031
RH
613}
614
96b2c0fc
NF
615static struct attribute *memory_memblk_attrs[] = {
616 &dev_attr_phys_index.attr,
96b2c0fc
NF
617 &dev_attr_state.attr,
618 &dev_attr_phys_device.attr,
619 &dev_attr_removable.attr,
ed2f2400
ZZ
620#ifdef CONFIG_MEMORY_HOTREMOVE
621 &dev_attr_valid_zones.attr,
622#endif
96b2c0fc
NF
623 NULL
624};
625
626static struct attribute_group memory_memblk_attr_group = {
627 .attrs = memory_memblk_attrs,
628};
629
630static const struct attribute_group *memory_memblk_attr_groups[] = {
631 &memory_memblk_attr_group,
632 NULL,
633};
634
635/*
636 * register_memory - Setup a sysfs device for a memory block
637 */
638static
639int register_memory(struct memory_block *memory)
640{
085aa2de
AY
641 int ret;
642
96b2c0fc
NF
643 memory->dev.bus = &memory_subsys;
644 memory->dev.id = memory->start_section_nr / sections_per_block;
645 memory->dev.release = memory_block_release;
646 memory->dev.groups = memory_memblk_attr_groups;
f991fae5 647 memory->dev.offline = memory->state == MEM_OFFLINE;
96b2c0fc 648
085aa2de
AY
649 ret = device_register(&memory->dev);
650 if (ret)
651 put_device(&memory->dev);
652
653 return ret;
96b2c0fc
NF
654}
655
90ec010f
DH
656static int init_memory_block(struct memory_block **memory,
657 unsigned long block_id, unsigned long state)
e4619c85 658{
0c2c99b1 659 struct memory_block *mem;
e4619c85
NF
660 unsigned long start_pfn;
661 int ret = 0;
662
dd625285 663 mem = find_memory_block_by_id(block_id);
db051a0d
DH
664 if (mem) {
665 put_device(&mem->dev);
666 return -EEXIST;
667 }
0c2c99b1 668 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
e4619c85
NF
669 if (!mem)
670 return -ENOMEM;
671
18115825 672 mem->start_section_nr = block_id * sections_per_block;
d3360164 673 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
e4619c85 674 mem->state = state;
d3360164 675 start_pfn = section_nr_to_pfn(mem->start_section_nr);
e4619c85
NF
676 mem->phys_device = arch_get_memory_phys_device(start_pfn);
677
0c2c99b1 678 ret = register_memory(mem);
0c2c99b1
NF
679
680 *memory = mem;
681 return ret;
682}
683
2491f0a2 684static int add_memory_block(unsigned long base_section_nr)
0c2c99b1 685{
2491f0a2 686 int ret, section_count = 0;
cb5e39b8 687 struct memory_block *mem;
2491f0a2 688 unsigned long nr;
0c2c99b1 689
2491f0a2
DH
690 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
691 nr++)
692 if (present_section_nr(nr))
18115825 693 section_count++;
e4619c85 694
cb5e39b8
SJ
695 if (section_count == 0)
696 return 0;
18115825
DH
697 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
698 MEM_ONLINE);
cb5e39b8
SJ
699 if (ret)
700 return ret;
701 mem->section_count = section_count;
702 return 0;
e4619c85
NF
703}
704
db051a0d
DH
705static void unregister_memory(struct memory_block *memory)
706{
707 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
708 return;
709
710 /* drop the ref. we got via find_memory_block() */
711 put_device(&memory->dev);
712 device_unregister(&memory->dev);
713}
714
4edd7cef 715/*
db051a0d
DH
716 * Create memory block devices for the given memory area. Start and size
717 * have to be aligned to memory block granularity. Memory block devices
718 * will be initialized as offline.
4edd7cef 719 */
db051a0d 720int create_memory_block_devices(unsigned long start, unsigned long size)
4edd7cef 721{
90ec010f
DH
722 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
723 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
d7f80530 724 struct memory_block *mem;
db051a0d
DH
725 unsigned long block_id;
726 int ret = 0;
b1eaef3d 727
db051a0d
DH
728 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
729 !IS_ALIGNED(size, memory_block_size_bytes())))
730 return -EINVAL;
b1eaef3d 731
db051a0d
DH
732 mutex_lock(&mem_sysfs_mutex);
733 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
18115825 734 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
d7f80530 735 if (ret)
db051a0d
DH
736 break;
737 mem->section_count = sections_per_block;
738 }
739 if (ret) {
740 end_block_id = block_id;
741 for (block_id = start_block_id; block_id != end_block_id;
742 block_id++) {
dd625285 743 mem = find_memory_block_by_id(block_id);
db051a0d
DH
744 mem->section_count = 0;
745 unregister_memory(mem);
746 }
d7f80530 747 }
d7f80530 748 mutex_unlock(&mem_sysfs_mutex);
b1eaef3d 749 return ret;
4edd7cef
DR
750}
751
4c4b7f9b
DH
752/*
753 * Remove memory block devices for the given memory area. Start and size
754 * have to be aligned to memory block granularity. Memory block devices
755 * have to be offline.
756 */
757void remove_memory_block_devices(unsigned long start, unsigned long size)
3947be19 758{
90ec010f
DH
759 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
760 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
3947be19 761 struct memory_block *mem;
90ec010f 762 unsigned long block_id;
3947be19 763
4c4b7f9b
DH
764 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
765 !IS_ALIGNED(size, memory_block_size_bytes())))
cb7b3a36
DH
766 return;
767
2938ffbd 768 mutex_lock(&mem_sysfs_mutex);
4c4b7f9b 769 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
dd625285 770 mem = find_memory_block_by_id(block_id);
4c4b7f9b
DH
771 if (WARN_ON_ONCE(!mem))
772 continue;
773 mem->section_count = 0;
774 unregister_memory_block_under_nodes(mem);
0c2c99b1 775 unregister_memory(mem);
4c4b7f9b 776 }
2938ffbd 777 mutex_unlock(&mem_sysfs_mutex);
3947be19
DH
778}
779
6677e3ea
YI
780/* return true if the memory block is offlined, otherwise, return false */
781bool is_memblock_offlined(struct memory_block *mem)
782{
783 return mem->state == MEM_OFFLINE;
784}
785
96b2c0fc
NF
786static struct attribute *memory_root_attrs[] = {
787#ifdef CONFIG_ARCH_MEMORY_PROBE
788 &dev_attr_probe.attr,
789#endif
790
791#ifdef CONFIG_MEMORY_FAILURE
792 &dev_attr_soft_offline_page.attr,
793 &dev_attr_hard_offline_page.attr,
794#endif
795
796 &dev_attr_block_size_bytes.attr,
31bc3858 797 &dev_attr_auto_online_blocks.attr,
96b2c0fc
NF
798 NULL
799};
800
801static struct attribute_group memory_root_attr_group = {
802 .attrs = memory_root_attrs,
803};
804
805static const struct attribute_group *memory_root_attr_groups[] = {
806 &memory_root_attr_group,
807 NULL,
808};
809
3947be19
DH
810/*
811 * Initialize the sysfs support for memory devices...
812 */
813int __init memory_dev_init(void)
814{
3947be19 815 int ret;
28ec24e2 816 int err;
2491f0a2 817 unsigned long block_sz, nr;
3947be19 818
96b2c0fc 819 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
28ec24e2
AM
820 if (ret)
821 goto out;
3947be19 822
0c2c99b1
NF
823 block_sz = get_memory_block_size();
824 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
825
3947be19
DH
826 /*
827 * Create entries for memory sections that were found
828 * during boot and have been initialized
829 */
b1eaef3d 830 mutex_lock(&mem_sysfs_mutex);
2491f0a2
DH
831 for (nr = 0; nr <= __highest_present_section_nr;
832 nr += sections_per_block) {
833 err = add_memory_block(nr);
28ec24e2
AM
834 if (!ret)
835 ret = err;
3947be19 836 }
b1eaef3d 837 mutex_unlock(&mem_sysfs_mutex);
3947be19 838
28ec24e2
AM
839out:
840 if (ret)
2b3a302a 841 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
3947be19
DH
842 return ret;
843}
ea884641
DH
844
845/**
846 * walk_memory_blocks - walk through all present memory blocks overlapped
847 * by the range [start, start + size)
848 *
849 * @start: start address of the memory range
850 * @size: size of the memory range
851 * @arg: argument passed to func
852 * @func: callback for each memory section walked
853 *
854 * This function walks through all present memory blocks overlapped by the
855 * range [start, start + size), calling func on each memory block.
856 *
857 * In case func() returns an error, walking is aborted and the error is
858 * returned.
859 */
860int walk_memory_blocks(unsigned long start, unsigned long size,
861 void *arg, walk_memory_blocks_func_t func)
862{
863 const unsigned long start_block_id = phys_to_block_id(start);
864 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
865 struct memory_block *mem;
866 unsigned long block_id;
867 int ret = 0;
868
dd625285
DH
869 if (!size)
870 return 0;
871
ea884641 872 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
dd625285 873 mem = find_memory_block_by_id(block_id);
ea884641
DH
874 if (!mem)
875 continue;
876
877 ret = func(mem, arg);
878 put_device(&mem->dev);
879 if (ret)
880 break;
881 }
882 return ret;
883}