treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / base / memory.c
blobb9f474c1139302116c9639612ea7746e7427b4f1
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Memory subsystem support
5 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
6 * Dave Hansen <haveblue@us.ibm.com>
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.
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>
25 #include <linux/atomic.h>
26 #include <linux/uaccess.h>
28 #define MEMORY_CLASS_NAME "memory"
30 #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
32 static int sections_per_block;
34 static inline unsigned long base_memory_block_id(unsigned long section_nr)
36 return section_nr / sections_per_block;
39 static inline unsigned long pfn_to_block_id(unsigned long pfn)
41 return base_memory_block_id(pfn_to_section_nr(pfn));
44 static inline unsigned long phys_to_block_id(unsigned long phys)
46 return pfn_to_block_id(PFN_DOWN(phys));
49 static int memory_subsys_online(struct device *dev);
50 static int memory_subsys_offline(struct device *dev);
52 static struct bus_type memory_subsys = {
53 .name = MEMORY_CLASS_NAME,
54 .dev_name = MEMORY_CLASS_NAME,
55 .online = memory_subsys_online,
56 .offline = memory_subsys_offline,
59 static BLOCKING_NOTIFIER_HEAD(memory_chain);
61 int register_memory_notifier(struct notifier_block *nb)
63 return blocking_notifier_chain_register(&memory_chain, nb);
65 EXPORT_SYMBOL(register_memory_notifier);
67 void unregister_memory_notifier(struct notifier_block *nb)
69 blocking_notifier_chain_unregister(&memory_chain, nb);
71 EXPORT_SYMBOL(unregister_memory_notifier);
73 static void memory_block_release(struct device *dev)
75 struct memory_block *mem = to_memory_block(dev);
77 kfree(mem);
80 unsigned long __weak memory_block_size_bytes(void)
82 return MIN_MEMORY_BLOCK_SIZE;
84 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
87 * Show the first physical section index (number) of this memory block.
89 static ssize_t phys_index_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
92 struct memory_block *mem = to_memory_block(dev);
93 unsigned long phys_index;
95 phys_index = mem->start_section_nr / sections_per_block;
96 return sprintf(buf, "%08lx\n", phys_index);
100 * Show whether the memory block is likely to be offlineable (or is already
101 * offline). Once offline, the memory block could be removed. The return
102 * value does, however, not indicate that there is a way to remove the
103 * memory block.
105 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
106 char *buf)
108 struct memory_block *mem = to_memory_block(dev);
109 unsigned long pfn;
110 int ret = 1, i;
112 if (mem->state != MEM_ONLINE)
113 goto out;
115 for (i = 0; i < sections_per_block; i++) {
116 if (!present_section_nr(mem->start_section_nr + i))
117 continue;
118 pfn = section_nr_to_pfn(mem->start_section_nr + i);
119 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
122 out:
123 return sprintf(buf, "%d\n", ret);
127 * online, offline, going offline, etc.
129 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
130 char *buf)
132 struct memory_block *mem = to_memory_block(dev);
133 ssize_t len = 0;
136 * We can probably put these states in a nice little array
137 * so that they're not open-coded
139 switch (mem->state) {
140 case MEM_ONLINE:
141 len = sprintf(buf, "online\n");
142 break;
143 case MEM_OFFLINE:
144 len = sprintf(buf, "offline\n");
145 break;
146 case MEM_GOING_OFFLINE:
147 len = sprintf(buf, "going-offline\n");
148 break;
149 default:
150 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
151 mem->state);
152 WARN_ON(1);
153 break;
156 return len;
159 int memory_notify(unsigned long val, void *v)
161 return blocking_notifier_call_chain(&memory_chain, val, v);
165 * The probe routines leave the pages uninitialized, just as the bootmem code
166 * does. Make sure we do not access them, but instead use only information from
167 * within sections.
169 static bool pages_correctly_probed(unsigned long start_pfn)
171 unsigned long section_nr = pfn_to_section_nr(start_pfn);
172 unsigned long section_nr_end = section_nr + sections_per_block;
173 unsigned long pfn = start_pfn;
176 * memmap between sections is not contiguous except with
177 * SPARSEMEM_VMEMMAP. We lookup the page once per section
178 * and assume memmap is contiguous within each section
180 for (; section_nr < section_nr_end; section_nr++) {
181 if (WARN_ON_ONCE(!pfn_valid(pfn)))
182 return false;
184 if (!present_section_nr(section_nr)) {
185 pr_warn("section %ld pfn[%lx, %lx) not present\n",
186 section_nr, pfn, pfn + PAGES_PER_SECTION);
187 return false;
188 } else if (!valid_section_nr(section_nr)) {
189 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
190 section_nr, pfn, pfn + PAGES_PER_SECTION);
191 return false;
192 } else if (online_section_nr(section_nr)) {
193 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
194 section_nr, pfn, pfn + PAGES_PER_SECTION);
195 return false;
197 pfn += PAGES_PER_SECTION;
200 return true;
204 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
205 * OK to have direct references to sparsemem variables in here.
207 static int
208 memory_block_action(unsigned long start_section_nr, unsigned long action,
209 int online_type, int nid)
211 unsigned long start_pfn;
212 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
213 int ret;
215 start_pfn = section_nr_to_pfn(start_section_nr);
217 switch (action) {
218 case MEM_ONLINE:
219 if (!pages_correctly_probed(start_pfn))
220 return -EBUSY;
222 ret = online_pages(start_pfn, nr_pages, online_type, nid);
223 break;
224 case MEM_OFFLINE:
225 ret = offline_pages(start_pfn, nr_pages);
226 break;
227 default:
228 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
229 "%ld\n", __func__, start_section_nr, action, action);
230 ret = -EINVAL;
233 return ret;
236 static int memory_block_change_state(struct memory_block *mem,
237 unsigned long to_state, unsigned long from_state_req)
239 int ret = 0;
241 if (mem->state != from_state_req)
242 return -EINVAL;
244 if (to_state == MEM_OFFLINE)
245 mem->state = MEM_GOING_OFFLINE;
247 ret = memory_block_action(mem->start_section_nr, to_state,
248 mem->online_type, mem->nid);
250 mem->state = ret ? from_state_req : to_state;
252 return ret;
255 /* The device lock serializes operations on memory_subsys_[online|offline] */
256 static int memory_subsys_online(struct device *dev)
258 struct memory_block *mem = to_memory_block(dev);
259 int ret;
261 if (mem->state == MEM_ONLINE)
262 return 0;
265 * If we are called from state_store(), online_type will be
266 * set >= 0 Otherwise we were called from the device online
267 * attribute and need to set the online_type.
269 if (mem->online_type < 0)
270 mem->online_type = MMOP_ONLINE_KEEP;
272 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
274 /* clear online_type */
275 mem->online_type = -1;
277 return ret;
280 static int memory_subsys_offline(struct device *dev)
282 struct memory_block *mem = to_memory_block(dev);
284 if (mem->state == MEM_OFFLINE)
285 return 0;
287 /* Can't offline block with non-present sections */
288 if (mem->section_count != sections_per_block)
289 return -EINVAL;
291 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
294 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
297 struct memory_block *mem = to_memory_block(dev);
298 int ret, online_type;
300 ret = lock_device_hotplug_sysfs();
301 if (ret)
302 return ret;
304 if (sysfs_streq(buf, "online_kernel"))
305 online_type = MMOP_ONLINE_KERNEL;
306 else if (sysfs_streq(buf, "online_movable"))
307 online_type = MMOP_ONLINE_MOVABLE;
308 else if (sysfs_streq(buf, "online"))
309 online_type = MMOP_ONLINE_KEEP;
310 else if (sysfs_streq(buf, "offline"))
311 online_type = MMOP_OFFLINE;
312 else {
313 ret = -EINVAL;
314 goto err;
317 switch (online_type) {
318 case MMOP_ONLINE_KERNEL:
319 case MMOP_ONLINE_MOVABLE:
320 case MMOP_ONLINE_KEEP:
321 /* mem->online_type is protected by device_hotplug_lock */
322 mem->online_type = online_type;
323 ret = device_online(&mem->dev);
324 break;
325 case MMOP_OFFLINE:
326 ret = device_offline(&mem->dev);
327 break;
328 default:
329 ret = -EINVAL; /* should never happen */
332 err:
333 unlock_device_hotplug();
335 if (ret < 0)
336 return ret;
337 if (ret)
338 return -EINVAL;
340 return count;
344 * phys_device is a bad name for this. What I really want
345 * is a way to differentiate between memory ranges that
346 * are part of physical devices that constitute
347 * a complete removable unit or fru.
348 * i.e. do these ranges belong to the same physical device,
349 * s.t. if I offline all of these sections I can then
350 * remove the physical device?
352 static ssize_t phys_device_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
355 struct memory_block *mem = to_memory_block(dev);
356 return sprintf(buf, "%d\n", mem->phys_device);
359 #ifdef CONFIG_MEMORY_HOTREMOVE
360 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
361 unsigned long nr_pages, int online_type,
362 struct zone *default_zone)
364 struct zone *zone;
366 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
367 if (zone != default_zone) {
368 strcat(buf, " ");
369 strcat(buf, zone->name);
373 static ssize_t valid_zones_show(struct device *dev,
374 struct device_attribute *attr, char *buf)
376 struct memory_block *mem = to_memory_block(dev);
377 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
378 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
379 struct zone *default_zone;
380 int nid;
383 * Check the existing zone. Make sure that we do that only on the
384 * online nodes otherwise the page_zone is not reliable
386 if (mem->state == MEM_ONLINE) {
388 * The block contains more than one zone can not be offlined.
389 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
391 default_zone = test_pages_in_a_zone(start_pfn,
392 start_pfn + nr_pages);
393 if (!default_zone)
394 return sprintf(buf, "none\n");
395 strcat(buf, default_zone->name);
396 goto out;
399 nid = mem->nid;
400 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
401 strcat(buf, default_zone->name);
403 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
404 default_zone);
405 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
406 default_zone);
407 out:
408 strcat(buf, "\n");
410 return strlen(buf);
412 static DEVICE_ATTR_RO(valid_zones);
413 #endif
415 static DEVICE_ATTR_RO(phys_index);
416 static DEVICE_ATTR_RW(state);
417 static DEVICE_ATTR_RO(phys_device);
418 static DEVICE_ATTR_RO(removable);
421 * Show the memory block size (shared by all memory blocks).
423 static ssize_t block_size_bytes_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
426 return sprintf(buf, "%lx\n", memory_block_size_bytes());
429 static DEVICE_ATTR_RO(block_size_bytes);
432 * Memory auto online policy.
435 static ssize_t auto_online_blocks_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
438 if (memhp_auto_online)
439 return sprintf(buf, "online\n");
440 else
441 return sprintf(buf, "offline\n");
444 static ssize_t auto_online_blocks_store(struct device *dev,
445 struct device_attribute *attr,
446 const char *buf, size_t count)
448 if (sysfs_streq(buf, "online"))
449 memhp_auto_online = true;
450 else if (sysfs_streq(buf, "offline"))
451 memhp_auto_online = false;
452 else
453 return -EINVAL;
455 return count;
458 static DEVICE_ATTR_RW(auto_online_blocks);
461 * Some architectures will have custom drivers to do this, and
462 * will not need to do it from userspace. The fake hot-add code
463 * as well as ppc64 will do all of their discovery in userspace
464 * and will require this interface.
466 #ifdef CONFIG_ARCH_MEMORY_PROBE
467 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
468 const char *buf, size_t count)
470 u64 phys_addr;
471 int nid, ret;
472 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
474 ret = kstrtoull(buf, 0, &phys_addr);
475 if (ret)
476 return ret;
478 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
479 return -EINVAL;
481 ret = lock_device_hotplug_sysfs();
482 if (ret)
483 return ret;
485 nid = memory_add_physaddr_to_nid(phys_addr);
486 ret = __add_memory(nid, phys_addr,
487 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
489 if (ret)
490 goto out;
492 ret = count;
493 out:
494 unlock_device_hotplug();
495 return ret;
498 static DEVICE_ATTR_WO(probe);
499 #endif
501 #ifdef CONFIG_MEMORY_FAILURE
503 * Support for offlining pages of memory
506 /* Soft offline a page */
507 static ssize_t soft_offline_page_store(struct device *dev,
508 struct device_attribute *attr,
509 const char *buf, size_t count)
511 int ret;
512 u64 pfn;
513 if (!capable(CAP_SYS_ADMIN))
514 return -EPERM;
515 if (kstrtoull(buf, 0, &pfn) < 0)
516 return -EINVAL;
517 pfn >>= PAGE_SHIFT;
518 ret = soft_offline_page(pfn, 0);
519 return ret == 0 ? count : ret;
522 /* Forcibly offline a page, including killing processes. */
523 static ssize_t hard_offline_page_store(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf, size_t count)
527 int ret;
528 u64 pfn;
529 if (!capable(CAP_SYS_ADMIN))
530 return -EPERM;
531 if (kstrtoull(buf, 0, &pfn) < 0)
532 return -EINVAL;
533 pfn >>= PAGE_SHIFT;
534 ret = memory_failure(pfn, 0);
535 return ret ? ret : count;
538 static DEVICE_ATTR_WO(soft_offline_page);
539 static DEVICE_ATTR_WO(hard_offline_page);
540 #endif
543 * Note that phys_device is optional. It is here to allow for
544 * differentiation between which *physical* devices each
545 * section belongs to...
547 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
549 return 0;
552 /* A reference for the returned memory block device is acquired. */
553 static struct memory_block *find_memory_block_by_id(unsigned long block_id)
555 struct device *dev;
557 dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
558 return dev ? to_memory_block(dev) : NULL;
562 * For now, we have a linear search to go find the appropriate
563 * memory_block corresponding to a particular phys_index. If
564 * this gets to be a real problem, we can always use a radix
565 * tree or something here.
567 * This could be made generic for all device subsystems.
569 struct memory_block *find_memory_block(struct mem_section *section)
571 unsigned long block_id = base_memory_block_id(__section_nr(section));
573 return find_memory_block_by_id(block_id);
576 static struct attribute *memory_memblk_attrs[] = {
577 &dev_attr_phys_index.attr,
578 &dev_attr_state.attr,
579 &dev_attr_phys_device.attr,
580 &dev_attr_removable.attr,
581 #ifdef CONFIG_MEMORY_HOTREMOVE
582 &dev_attr_valid_zones.attr,
583 #endif
584 NULL
587 static struct attribute_group memory_memblk_attr_group = {
588 .attrs = memory_memblk_attrs,
591 static const struct attribute_group *memory_memblk_attr_groups[] = {
592 &memory_memblk_attr_group,
593 NULL,
597 * register_memory - Setup a sysfs device for a memory block
599 static
600 int register_memory(struct memory_block *memory)
602 int ret;
604 memory->dev.bus = &memory_subsys;
605 memory->dev.id = memory->start_section_nr / sections_per_block;
606 memory->dev.release = memory_block_release;
607 memory->dev.groups = memory_memblk_attr_groups;
608 memory->dev.offline = memory->state == MEM_OFFLINE;
610 ret = device_register(&memory->dev);
611 if (ret)
612 put_device(&memory->dev);
614 return ret;
617 static int init_memory_block(struct memory_block **memory,
618 unsigned long block_id, unsigned long state)
620 struct memory_block *mem;
621 unsigned long start_pfn;
622 int ret = 0;
624 mem = find_memory_block_by_id(block_id);
625 if (mem) {
626 put_device(&mem->dev);
627 return -EEXIST;
629 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
630 if (!mem)
631 return -ENOMEM;
633 mem->start_section_nr = block_id * sections_per_block;
634 mem->state = state;
635 start_pfn = section_nr_to_pfn(mem->start_section_nr);
636 mem->phys_device = arch_get_memory_phys_device(start_pfn);
637 mem->nid = NUMA_NO_NODE;
639 ret = register_memory(mem);
641 *memory = mem;
642 return ret;
645 static int add_memory_block(unsigned long base_section_nr)
647 int ret, section_count = 0;
648 struct memory_block *mem;
649 unsigned long nr;
651 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
652 nr++)
653 if (present_section_nr(nr))
654 section_count++;
656 if (section_count == 0)
657 return 0;
658 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
659 MEM_ONLINE);
660 if (ret)
661 return ret;
662 mem->section_count = section_count;
663 return 0;
666 static void unregister_memory(struct memory_block *memory)
668 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
669 return;
671 /* drop the ref. we got via find_memory_block() */
672 put_device(&memory->dev);
673 device_unregister(&memory->dev);
677 * Create memory block devices for the given memory area. Start and size
678 * have to be aligned to memory block granularity. Memory block devices
679 * will be initialized as offline.
681 * Called under device_hotplug_lock.
683 int create_memory_block_devices(unsigned long start, unsigned long size)
685 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
686 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
687 struct memory_block *mem;
688 unsigned long block_id;
689 int ret = 0;
691 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
692 !IS_ALIGNED(size, memory_block_size_bytes())))
693 return -EINVAL;
695 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
696 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
697 if (ret)
698 break;
699 mem->section_count = sections_per_block;
701 if (ret) {
702 end_block_id = block_id;
703 for (block_id = start_block_id; block_id != end_block_id;
704 block_id++) {
705 mem = find_memory_block_by_id(block_id);
706 if (WARN_ON_ONCE(!mem))
707 continue;
708 mem->section_count = 0;
709 unregister_memory(mem);
712 return ret;
716 * Remove memory block devices for the given memory area. Start and size
717 * have to be aligned to memory block granularity. Memory block devices
718 * have to be offline.
720 * Called under device_hotplug_lock.
722 void remove_memory_block_devices(unsigned long start, unsigned long size)
724 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
725 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
726 struct memory_block *mem;
727 unsigned long block_id;
729 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
730 !IS_ALIGNED(size, memory_block_size_bytes())))
731 return;
733 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
734 mem = find_memory_block_by_id(block_id);
735 if (WARN_ON_ONCE(!mem))
736 continue;
737 mem->section_count = 0;
738 unregister_memory_block_under_nodes(mem);
739 unregister_memory(mem);
743 /* return true if the memory block is offlined, otherwise, return false */
744 bool is_memblock_offlined(struct memory_block *mem)
746 return mem->state == MEM_OFFLINE;
749 static struct attribute *memory_root_attrs[] = {
750 #ifdef CONFIG_ARCH_MEMORY_PROBE
751 &dev_attr_probe.attr,
752 #endif
754 #ifdef CONFIG_MEMORY_FAILURE
755 &dev_attr_soft_offline_page.attr,
756 &dev_attr_hard_offline_page.attr,
757 #endif
759 &dev_attr_block_size_bytes.attr,
760 &dev_attr_auto_online_blocks.attr,
761 NULL
764 static struct attribute_group memory_root_attr_group = {
765 .attrs = memory_root_attrs,
768 static const struct attribute_group *memory_root_attr_groups[] = {
769 &memory_root_attr_group,
770 NULL,
774 * Initialize the sysfs support for memory devices. At the time this function
775 * is called, we cannot have concurrent creation/deletion of memory block
776 * devices, the device_hotplug_lock is not needed.
778 void __init memory_dev_init(void)
780 int ret;
781 unsigned long block_sz, nr;
783 /* Validate the configured memory block size */
784 block_sz = memory_block_size_bytes();
785 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
786 panic("Memory block size not suitable: 0x%lx\n", block_sz);
787 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
789 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
790 if (ret)
791 panic("%s() failed to register subsystem: %d\n", __func__, ret);
794 * Create entries for memory sections that were found
795 * during boot and have been initialized
797 for (nr = 0; nr <= __highest_present_section_nr;
798 nr += sections_per_block) {
799 ret = add_memory_block(nr);
800 if (ret)
801 panic("%s() failed to add memory block: %d\n", __func__,
802 ret);
807 * walk_memory_blocks - walk through all present memory blocks overlapped
808 * by the range [start, start + size)
810 * @start: start address of the memory range
811 * @size: size of the memory range
812 * @arg: argument passed to func
813 * @func: callback for each memory section walked
815 * This function walks through all present memory blocks overlapped by the
816 * range [start, start + size), calling func on each memory block.
818 * In case func() returns an error, walking is aborted and the error is
819 * returned.
821 int walk_memory_blocks(unsigned long start, unsigned long size,
822 void *arg, walk_memory_blocks_func_t func)
824 const unsigned long start_block_id = phys_to_block_id(start);
825 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
826 struct memory_block *mem;
827 unsigned long block_id;
828 int ret = 0;
830 if (!size)
831 return 0;
833 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
834 mem = find_memory_block_by_id(block_id);
835 if (!mem)
836 continue;
838 ret = func(mem, arg);
839 put_device(&mem->dev);
840 if (ret)
841 break;
843 return ret;
846 struct for_each_memory_block_cb_data {
847 walk_memory_blocks_func_t func;
848 void *arg;
851 static int for_each_memory_block_cb(struct device *dev, void *data)
853 struct memory_block *mem = to_memory_block(dev);
854 struct for_each_memory_block_cb_data *cb_data = data;
856 return cb_data->func(mem, cb_data->arg);
860 * for_each_memory_block - walk through all present memory blocks
862 * @arg: argument passed to func
863 * @func: callback for each memory block walked
865 * This function walks through all present memory blocks, calling func on
866 * each memory block.
868 * In case func() returns an error, walking is aborted and the error is
869 * returned.
871 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
873 struct for_each_memory_block_cb_data cb_data = {
874 .func = func,
875 .arg = arg,
878 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
879 for_each_memory_block_cb);