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