xref: /openbmc/linux/drivers/base/memory.c (revision a3b91d8b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Memory subsystem support
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 
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/topology.h>
17 #include <linux/capability.h>
18 #include <linux/device.h>
19 #include <linux/memory.h>
20 #include <linux/memory_hotplug.h>
21 #include <linux/mm.h>
22 #include <linux/stat.h>
23 #include <linux/slab.h>
24 
25 #include <linux/atomic.h>
26 #include <linux/uaccess.h>
27 
28 #define MEMORY_CLASS_NAME	"memory"
29 
30 static const char *const online_type_to_str[] = {
31 	[MMOP_OFFLINE] = "offline",
32 	[MMOP_ONLINE] = "online",
33 	[MMOP_ONLINE_KERNEL] = "online_kernel",
34 	[MMOP_ONLINE_MOVABLE] = "online_movable",
35 };
36 
37 int memhp_online_type_from_str(const char *str)
38 {
39 	int i;
40 
41 	for (i = 0; i < ARRAY_SIZE(online_type_to_str); i++) {
42 		if (sysfs_streq(str, online_type_to_str[i]))
43 			return i;
44 	}
45 	return -EINVAL;
46 }
47 
48 #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
49 
50 static int sections_per_block;
51 
52 static inline unsigned long base_memory_block_id(unsigned long section_nr)
53 {
54 	return section_nr / sections_per_block;
55 }
56 
57 static inline unsigned long pfn_to_block_id(unsigned long pfn)
58 {
59 	return base_memory_block_id(pfn_to_section_nr(pfn));
60 }
61 
62 static inline unsigned long phys_to_block_id(unsigned long phys)
63 {
64 	return pfn_to_block_id(PFN_DOWN(phys));
65 }
66 
67 static int memory_subsys_online(struct device *dev);
68 static int memory_subsys_offline(struct device *dev);
69 
70 static struct bus_type memory_subsys = {
71 	.name = MEMORY_CLASS_NAME,
72 	.dev_name = MEMORY_CLASS_NAME,
73 	.online = memory_subsys_online,
74 	.offline = memory_subsys_offline,
75 };
76 
77 static BLOCKING_NOTIFIER_HEAD(memory_chain);
78 
79 int register_memory_notifier(struct notifier_block *nb)
80 {
81 	return blocking_notifier_chain_register(&memory_chain, nb);
82 }
83 EXPORT_SYMBOL(register_memory_notifier);
84 
85 void unregister_memory_notifier(struct notifier_block *nb)
86 {
87 	blocking_notifier_chain_unregister(&memory_chain, nb);
88 }
89 EXPORT_SYMBOL(unregister_memory_notifier);
90 
91 static void memory_block_release(struct device *dev)
92 {
93 	struct memory_block *mem = to_memory_block(dev);
94 
95 	kfree(mem);
96 }
97 
98 unsigned long __weak memory_block_size_bytes(void)
99 {
100 	return MIN_MEMORY_BLOCK_SIZE;
101 }
102 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
103 
104 /*
105  * Show the first physical section index (number) of this memory block.
106  */
107 static ssize_t phys_index_show(struct device *dev,
108 			       struct device_attribute *attr, char *buf)
109 {
110 	struct memory_block *mem = to_memory_block(dev);
111 	unsigned long phys_index;
112 
113 	phys_index = mem->start_section_nr / sections_per_block;
114 	return sprintf(buf, "%08lx\n", phys_index);
115 }
116 
117 /*
118  * Legacy interface that we cannot remove. Always indicate "removable"
119  * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
120  */
121 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
122 			      char *buf)
123 {
124 	return sprintf(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
125 }
126 
127 /*
128  * online, offline, going offline, etc.
129  */
130 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
131 			  char *buf)
132 {
133 	struct memory_block *mem = to_memory_block(dev);
134 	ssize_t len = 0;
135 
136 	/*
137 	 * We can probably put these states in a nice little array
138 	 * so that they're not open-coded
139 	 */
140 	switch (mem->state) {
141 	case MEM_ONLINE:
142 		len = sprintf(buf, "online\n");
143 		break;
144 	case MEM_OFFLINE:
145 		len = sprintf(buf, "offline\n");
146 		break;
147 	case MEM_GOING_OFFLINE:
148 		len = sprintf(buf, "going-offline\n");
149 		break;
150 	default:
151 		len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
152 				mem->state);
153 		WARN_ON(1);
154 		break;
155 	}
156 
157 	return len;
158 }
159 
160 int memory_notify(unsigned long val, void *v)
161 {
162 	return blocking_notifier_call_chain(&memory_chain, val, v);
163 }
164 
165 /*
166  * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
167  * OK to have direct references to sparsemem variables in here.
168  */
169 static int
170 memory_block_action(unsigned long start_section_nr, unsigned long action,
171 		    int online_type, int nid)
172 {
173 	unsigned long start_pfn;
174 	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
175 	int ret;
176 
177 	start_pfn = section_nr_to_pfn(start_section_nr);
178 
179 	switch (action) {
180 	case MEM_ONLINE:
181 		ret = online_pages(start_pfn, nr_pages, online_type, nid);
182 		break;
183 	case MEM_OFFLINE:
184 		ret = offline_pages(start_pfn, nr_pages);
185 		break;
186 	default:
187 		WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
188 		     "%ld\n", __func__, start_section_nr, action, action);
189 		ret = -EINVAL;
190 	}
191 
192 	return ret;
193 }
194 
195 static int memory_block_change_state(struct memory_block *mem,
196 		unsigned long to_state, unsigned long from_state_req)
197 {
198 	int ret = 0;
199 
200 	if (mem->state != from_state_req)
201 		return -EINVAL;
202 
203 	if (to_state == MEM_OFFLINE)
204 		mem->state = MEM_GOING_OFFLINE;
205 
206 	ret = memory_block_action(mem->start_section_nr, to_state,
207 				  mem->online_type, mem->nid);
208 
209 	mem->state = ret ? from_state_req : to_state;
210 
211 	return ret;
212 }
213 
214 /* The device lock serializes operations on memory_subsys_[online|offline] */
215 static int memory_subsys_online(struct device *dev)
216 {
217 	struct memory_block *mem = to_memory_block(dev);
218 	int ret;
219 
220 	if (mem->state == MEM_ONLINE)
221 		return 0;
222 
223 	/*
224 	 * When called via device_online() without configuring the online_type,
225 	 * we want to default to MMOP_ONLINE.
226 	 */
227 	if (mem->online_type == MMOP_OFFLINE)
228 		mem->online_type = MMOP_ONLINE;
229 
230 	ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
231 	mem->online_type = MMOP_OFFLINE;
232 
233 	return ret;
234 }
235 
236 static int memory_subsys_offline(struct device *dev)
237 {
238 	struct memory_block *mem = to_memory_block(dev);
239 
240 	if (mem->state == MEM_OFFLINE)
241 		return 0;
242 
243 	return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
244 }
245 
246 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
247 			   const char *buf, size_t count)
248 {
249 	const int online_type = memhp_online_type_from_str(buf);
250 	struct memory_block *mem = to_memory_block(dev);
251 	int ret;
252 
253 	if (online_type < 0)
254 		return -EINVAL;
255 
256 	ret = lock_device_hotplug_sysfs();
257 	if (ret)
258 		return ret;
259 
260 	switch (online_type) {
261 	case MMOP_ONLINE_KERNEL:
262 	case MMOP_ONLINE_MOVABLE:
263 	case MMOP_ONLINE:
264 		/* mem->online_type is protected by device_hotplug_lock */
265 		mem->online_type = online_type;
266 		ret = device_online(&mem->dev);
267 		break;
268 	case MMOP_OFFLINE:
269 		ret = device_offline(&mem->dev);
270 		break;
271 	default:
272 		ret = -EINVAL; /* should never happen */
273 	}
274 
275 	unlock_device_hotplug();
276 
277 	if (ret < 0)
278 		return ret;
279 	if (ret)
280 		return -EINVAL;
281 
282 	return count;
283 }
284 
285 /*
286  * phys_device is a bad name for this.  What I really want
287  * is a way to differentiate between memory ranges that
288  * are part of physical devices that constitute
289  * a complete removable unit or fru.
290  * i.e. do these ranges belong to the same physical device,
291  * s.t. if I offline all of these sections I can then
292  * remove the physical device?
293  */
294 static ssize_t phys_device_show(struct device *dev,
295 				struct device_attribute *attr, char *buf)
296 {
297 	struct memory_block *mem = to_memory_block(dev);
298 	return sprintf(buf, "%d\n", mem->phys_device);
299 }
300 
301 #ifdef CONFIG_MEMORY_HOTREMOVE
302 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
303 		unsigned long nr_pages, int online_type,
304 		struct zone *default_zone)
305 {
306 	struct zone *zone;
307 
308 	zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
309 	if (zone != default_zone) {
310 		strcat(buf, " ");
311 		strcat(buf, zone->name);
312 	}
313 }
314 
315 static ssize_t valid_zones_show(struct device *dev,
316 				struct device_attribute *attr, char *buf)
317 {
318 	struct memory_block *mem = to_memory_block(dev);
319 	unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
320 	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
321 	struct zone *default_zone;
322 	int nid;
323 
324 	/*
325 	 * Check the existing zone. Make sure that we do that only on the
326 	 * online nodes otherwise the page_zone is not reliable
327 	 */
328 	if (mem->state == MEM_ONLINE) {
329 		/*
330 		 * The block contains more than one zone can not be offlined.
331 		 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
332 		 */
333 		default_zone = test_pages_in_a_zone(start_pfn,
334 						    start_pfn + nr_pages);
335 		if (!default_zone)
336 			return sprintf(buf, "none\n");
337 		strcat(buf, default_zone->name);
338 		goto out;
339 	}
340 
341 	nid = mem->nid;
342 	default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, start_pfn,
343 					  nr_pages);
344 	strcat(buf, default_zone->name);
345 
346 	print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
347 			default_zone);
348 	print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
349 			default_zone);
350 out:
351 	strcat(buf, "\n");
352 
353 	return strlen(buf);
354 }
355 static DEVICE_ATTR_RO(valid_zones);
356 #endif
357 
358 static DEVICE_ATTR_RO(phys_index);
359 static DEVICE_ATTR_RW(state);
360 static DEVICE_ATTR_RO(phys_device);
361 static DEVICE_ATTR_RO(removable);
362 
363 /*
364  * Show the memory block size (shared by all memory blocks).
365  */
366 static ssize_t block_size_bytes_show(struct device *dev,
367 				     struct device_attribute *attr, char *buf)
368 {
369 	return sprintf(buf, "%lx\n", memory_block_size_bytes());
370 }
371 
372 static DEVICE_ATTR_RO(block_size_bytes);
373 
374 /*
375  * Memory auto online policy.
376  */
377 
378 static ssize_t auto_online_blocks_show(struct device *dev,
379 				       struct device_attribute *attr, char *buf)
380 {
381 	return sprintf(buf, "%s\n",
382 		       online_type_to_str[memhp_default_online_type]);
383 }
384 
385 static ssize_t auto_online_blocks_store(struct device *dev,
386 					struct device_attribute *attr,
387 					const char *buf, size_t count)
388 {
389 	const int online_type = memhp_online_type_from_str(buf);
390 
391 	if (online_type < 0)
392 		return -EINVAL;
393 
394 	memhp_default_online_type = online_type;
395 	return count;
396 }
397 
398 static DEVICE_ATTR_RW(auto_online_blocks);
399 
400 /*
401  * Some architectures will have custom drivers to do this, and
402  * will not need to do it from userspace.  The fake hot-add code
403  * as well as ppc64 will do all of their discovery in userspace
404  * and will require this interface.
405  */
406 #ifdef CONFIG_ARCH_MEMORY_PROBE
407 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
408 			   const char *buf, size_t count)
409 {
410 	u64 phys_addr;
411 	int nid, ret;
412 	unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
413 
414 	ret = kstrtoull(buf, 0, &phys_addr);
415 	if (ret)
416 		return ret;
417 
418 	if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
419 		return -EINVAL;
420 
421 	ret = lock_device_hotplug_sysfs();
422 	if (ret)
423 		return ret;
424 
425 	nid = memory_add_physaddr_to_nid(phys_addr);
426 	ret = __add_memory(nid, phys_addr,
427 			   MIN_MEMORY_BLOCK_SIZE * sections_per_block);
428 
429 	if (ret)
430 		goto out;
431 
432 	ret = count;
433 out:
434 	unlock_device_hotplug();
435 	return ret;
436 }
437 
438 static DEVICE_ATTR_WO(probe);
439 #endif
440 
441 #ifdef CONFIG_MEMORY_FAILURE
442 /*
443  * Support for offlining pages of memory
444  */
445 
446 /* Soft offline a page */
447 static ssize_t soft_offline_page_store(struct device *dev,
448 				       struct device_attribute *attr,
449 				       const char *buf, size_t count)
450 {
451 	int ret;
452 	u64 pfn;
453 	if (!capable(CAP_SYS_ADMIN))
454 		return -EPERM;
455 	if (kstrtoull(buf, 0, &pfn) < 0)
456 		return -EINVAL;
457 	pfn >>= PAGE_SHIFT;
458 	ret = soft_offline_page(pfn, 0);
459 	return ret == 0 ? count : ret;
460 }
461 
462 /* Forcibly offline a page, including killing processes. */
463 static ssize_t hard_offline_page_store(struct device *dev,
464 				       struct device_attribute *attr,
465 				       const char *buf, size_t count)
466 {
467 	int ret;
468 	u64 pfn;
469 	if (!capable(CAP_SYS_ADMIN))
470 		return -EPERM;
471 	if (kstrtoull(buf, 0, &pfn) < 0)
472 		return -EINVAL;
473 	pfn >>= PAGE_SHIFT;
474 	ret = memory_failure(pfn, 0);
475 	return ret ? ret : count;
476 }
477 
478 static DEVICE_ATTR_WO(soft_offline_page);
479 static DEVICE_ATTR_WO(hard_offline_page);
480 #endif
481 
482 /*
483  * Note that phys_device is optional.  It is here to allow for
484  * differentiation between which *physical* devices each
485  * section belongs to...
486  */
487 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
488 {
489 	return 0;
490 }
491 
492 /* A reference for the returned memory block device is acquired. */
493 static struct memory_block *find_memory_block_by_id(unsigned long block_id)
494 {
495 	struct device *dev;
496 
497 	dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
498 	return dev ? to_memory_block(dev) : NULL;
499 }
500 
501 /*
502  * For now, we have a linear search to go find the appropriate
503  * memory_block corresponding to a particular phys_index. If
504  * this gets to be a real problem, we can always use a radix
505  * tree or something here.
506  *
507  * This could be made generic for all device subsystems.
508  */
509 struct memory_block *find_memory_block(struct mem_section *section)
510 {
511 	unsigned long block_id = base_memory_block_id(__section_nr(section));
512 
513 	return find_memory_block_by_id(block_id);
514 }
515 
516 static struct attribute *memory_memblk_attrs[] = {
517 	&dev_attr_phys_index.attr,
518 	&dev_attr_state.attr,
519 	&dev_attr_phys_device.attr,
520 	&dev_attr_removable.attr,
521 #ifdef CONFIG_MEMORY_HOTREMOVE
522 	&dev_attr_valid_zones.attr,
523 #endif
524 	NULL
525 };
526 
527 static struct attribute_group memory_memblk_attr_group = {
528 	.attrs = memory_memblk_attrs,
529 };
530 
531 static const struct attribute_group *memory_memblk_attr_groups[] = {
532 	&memory_memblk_attr_group,
533 	NULL,
534 };
535 
536 /*
537  * register_memory - Setup a sysfs device for a memory block
538  */
539 static
540 int register_memory(struct memory_block *memory)
541 {
542 	int ret;
543 
544 	memory->dev.bus = &memory_subsys;
545 	memory->dev.id = memory->start_section_nr / sections_per_block;
546 	memory->dev.release = memory_block_release;
547 	memory->dev.groups = memory_memblk_attr_groups;
548 	memory->dev.offline = memory->state == MEM_OFFLINE;
549 
550 	ret = device_register(&memory->dev);
551 	if (ret)
552 		put_device(&memory->dev);
553 
554 	return ret;
555 }
556 
557 static int init_memory_block(struct memory_block **memory,
558 			     unsigned long block_id, unsigned long state)
559 {
560 	struct memory_block *mem;
561 	unsigned long start_pfn;
562 	int ret = 0;
563 
564 	mem = find_memory_block_by_id(block_id);
565 	if (mem) {
566 		put_device(&mem->dev);
567 		return -EEXIST;
568 	}
569 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
570 	if (!mem)
571 		return -ENOMEM;
572 
573 	mem->start_section_nr = block_id * sections_per_block;
574 	mem->state = state;
575 	start_pfn = section_nr_to_pfn(mem->start_section_nr);
576 	mem->phys_device = arch_get_memory_phys_device(start_pfn);
577 	mem->nid = NUMA_NO_NODE;
578 
579 	ret = register_memory(mem);
580 
581 	*memory = mem;
582 	return ret;
583 }
584 
585 static int add_memory_block(unsigned long base_section_nr)
586 {
587 	int section_count = 0;
588 	struct memory_block *mem;
589 	unsigned long nr;
590 
591 	for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
592 	     nr++)
593 		if (present_section_nr(nr))
594 			section_count++;
595 
596 	if (section_count == 0)
597 		return 0;
598 	return init_memory_block(&mem, base_memory_block_id(base_section_nr),
599 				 MEM_ONLINE);
600 }
601 
602 static void unregister_memory(struct memory_block *memory)
603 {
604 	if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
605 		return;
606 
607 	/* drop the ref. we got via find_memory_block() */
608 	put_device(&memory->dev);
609 	device_unregister(&memory->dev);
610 }
611 
612 /*
613  * Create memory block devices for the given memory area. Start and size
614  * have to be aligned to memory block granularity. Memory block devices
615  * will be initialized as offline.
616  *
617  * Called under device_hotplug_lock.
618  */
619 int create_memory_block_devices(unsigned long start, unsigned long size)
620 {
621 	const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
622 	unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
623 	struct memory_block *mem;
624 	unsigned long block_id;
625 	int ret = 0;
626 
627 	if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
628 			 !IS_ALIGNED(size, memory_block_size_bytes())))
629 		return -EINVAL;
630 
631 	for (block_id = start_block_id; block_id != end_block_id; block_id++) {
632 		ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
633 		if (ret)
634 			break;
635 	}
636 	if (ret) {
637 		end_block_id = block_id;
638 		for (block_id = start_block_id; block_id != end_block_id;
639 		     block_id++) {
640 			mem = find_memory_block_by_id(block_id);
641 			if (WARN_ON_ONCE(!mem))
642 				continue;
643 			unregister_memory(mem);
644 		}
645 	}
646 	return ret;
647 }
648 
649 /*
650  * Remove memory block devices for the given memory area. Start and size
651  * have to be aligned to memory block granularity. Memory block devices
652  * have to be offline.
653  *
654  * Called under device_hotplug_lock.
655  */
656 void remove_memory_block_devices(unsigned long start, unsigned long size)
657 {
658 	const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
659 	const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
660 	struct memory_block *mem;
661 	unsigned long block_id;
662 
663 	if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
664 			 !IS_ALIGNED(size, memory_block_size_bytes())))
665 		return;
666 
667 	for (block_id = start_block_id; block_id != end_block_id; block_id++) {
668 		mem = find_memory_block_by_id(block_id);
669 		if (WARN_ON_ONCE(!mem))
670 			continue;
671 		unregister_memory_block_under_nodes(mem);
672 		unregister_memory(mem);
673 	}
674 }
675 
676 /* return true if the memory block is offlined, otherwise, return false */
677 bool is_memblock_offlined(struct memory_block *mem)
678 {
679 	return mem->state == MEM_OFFLINE;
680 }
681 
682 static struct attribute *memory_root_attrs[] = {
683 #ifdef CONFIG_ARCH_MEMORY_PROBE
684 	&dev_attr_probe.attr,
685 #endif
686 
687 #ifdef CONFIG_MEMORY_FAILURE
688 	&dev_attr_soft_offline_page.attr,
689 	&dev_attr_hard_offline_page.attr,
690 #endif
691 
692 	&dev_attr_block_size_bytes.attr,
693 	&dev_attr_auto_online_blocks.attr,
694 	NULL
695 };
696 
697 static struct attribute_group memory_root_attr_group = {
698 	.attrs = memory_root_attrs,
699 };
700 
701 static const struct attribute_group *memory_root_attr_groups[] = {
702 	&memory_root_attr_group,
703 	NULL,
704 };
705 
706 /*
707  * Initialize the sysfs support for memory devices. At the time this function
708  * is called, we cannot have concurrent creation/deletion of memory block
709  * devices, the device_hotplug_lock is not needed.
710  */
711 void __init memory_dev_init(void)
712 {
713 	int ret;
714 	unsigned long block_sz, nr;
715 
716 	/* Validate the configured memory block size */
717 	block_sz = memory_block_size_bytes();
718 	if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
719 		panic("Memory block size not suitable: 0x%lx\n", block_sz);
720 	sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
721 
722 	ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
723 	if (ret)
724 		panic("%s() failed to register subsystem: %d\n", __func__, ret);
725 
726 	/*
727 	 * Create entries for memory sections that were found
728 	 * during boot and have been initialized
729 	 */
730 	for (nr = 0; nr <= __highest_present_section_nr;
731 	     nr += sections_per_block) {
732 		ret = add_memory_block(nr);
733 		if (ret)
734 			panic("%s() failed to add memory block: %d\n", __func__,
735 			      ret);
736 	}
737 }
738 
739 /**
740  * walk_memory_blocks - walk through all present memory blocks overlapped
741  *			by the range [start, start + size)
742  *
743  * @start: start address of the memory range
744  * @size: size of the memory range
745  * @arg: argument passed to func
746  * @func: callback for each memory section walked
747  *
748  * This function walks through all present memory blocks overlapped by the
749  * range [start, start + size), calling func on each memory block.
750  *
751  * In case func() returns an error, walking is aborted and the error is
752  * returned.
753  */
754 int walk_memory_blocks(unsigned long start, unsigned long size,
755 		       void *arg, walk_memory_blocks_func_t func)
756 {
757 	const unsigned long start_block_id = phys_to_block_id(start);
758 	const unsigned long end_block_id = phys_to_block_id(start + size - 1);
759 	struct memory_block *mem;
760 	unsigned long block_id;
761 	int ret = 0;
762 
763 	if (!size)
764 		return 0;
765 
766 	for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
767 		mem = find_memory_block_by_id(block_id);
768 		if (!mem)
769 			continue;
770 
771 		ret = func(mem, arg);
772 		put_device(&mem->dev);
773 		if (ret)
774 			break;
775 	}
776 	return ret;
777 }
778 
779 struct for_each_memory_block_cb_data {
780 	walk_memory_blocks_func_t func;
781 	void *arg;
782 };
783 
784 static int for_each_memory_block_cb(struct device *dev, void *data)
785 {
786 	struct memory_block *mem = to_memory_block(dev);
787 	struct for_each_memory_block_cb_data *cb_data = data;
788 
789 	return cb_data->func(mem, cb_data->arg);
790 }
791 
792 /**
793  * for_each_memory_block - walk through all present memory blocks
794  *
795  * @arg: argument passed to func
796  * @func: callback for each memory block walked
797  *
798  * This function walks through all present memory blocks, calling func on
799  * each memory block.
800  *
801  * In case func() returns an error, walking is aborted and the error is
802  * returned.
803  */
804 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
805 {
806 	struct for_each_memory_block_cb_data cb_data = {
807 		.func = func,
808 		.arg = arg,
809 	};
810 
811 	return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
812 				for_each_memory_block_cb);
813 }
814