xref: /linux/drivers/base/memory.c (revision ae99fb8baafc881b35aa0b79d7ac0178a7c40c89)
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  * 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.
104  */
105 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
106 			      char *buf)
107 {
108 	struct memory_block *mem = to_memory_block(dev);
109 	unsigned long pfn;
110 	int ret = 1, i;
111 
112 	if (mem->state != MEM_ONLINE)
113 		goto out;
114 
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);
120 	}
121 
122 out:
123 	return sprintf(buf, "%d\n", ret);
124 }
125 
126 /*
127  * online, offline, going offline, etc.
128  */
129 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
130 			  char *buf)
131 {
132 	struct memory_block *mem = to_memory_block(dev);
133 	ssize_t len = 0;
134 
135 	/*
136 	 * We can probably put these states in a nice little array
137 	 * so that they're not open-coded
138 	 */
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;
154 	}
155 
156 	return len;
157 }
158 
159 int memory_notify(unsigned long val, void *v)
160 {
161 	return blocking_notifier_call_chain(&memory_chain, val, v);
162 }
163 
164 /*
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.
168  */
169 static bool pages_correctly_probed(unsigned long start_pfn)
170 {
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;
174 
175 	/*
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
179 	 */
180 	for (; section_nr < section_nr_end; section_nr++) {
181 		if (WARN_ON_ONCE(!pfn_valid(pfn)))
182 			return false;
183 
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;
196 		}
197 		pfn += PAGES_PER_SECTION;
198 	}
199 
200 	return true;
201 }
202 
203 /*
204  * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
205  * OK to have direct references to sparsemem variables in here.
206  */
207 static int
208 memory_block_action(unsigned long start_section_nr, unsigned long action,
209 		    int online_type, int nid)
210 {
211 	unsigned long start_pfn;
212 	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
213 	int ret;
214 
215 	start_pfn = section_nr_to_pfn(start_section_nr);
216 
217 	switch (action) {
218 	case MEM_ONLINE:
219 		if (!pages_correctly_probed(start_pfn))
220 			return -EBUSY;
221 
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;
231 	}
232 
233 	return ret;
234 }
235 
236 static int memory_block_change_state(struct memory_block *mem,
237 		unsigned long to_state, unsigned long from_state_req)
238 {
239 	int ret = 0;
240 
241 	if (mem->state != from_state_req)
242 		return -EINVAL;
243 
244 	if (to_state == MEM_OFFLINE)
245 		mem->state = MEM_GOING_OFFLINE;
246 
247 	ret = memory_block_action(mem->start_section_nr, to_state,
248 				  mem->online_type, mem->nid);
249 
250 	mem->state = ret ? from_state_req : to_state;
251 
252 	return ret;
253 }
254 
255 /* The device lock serializes operations on memory_subsys_[online|offline] */
256 static int memory_subsys_online(struct device *dev)
257 {
258 	struct memory_block *mem = to_memory_block(dev);
259 	int ret;
260 
261 	if (mem->state == MEM_ONLINE)
262 		return 0;
263 
264 	/*
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.
268 	 */
269 	if (mem->online_type < 0)
270 		mem->online_type = MMOP_ONLINE_KEEP;
271 
272 	ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
273 
274 	/* clear online_type */
275 	mem->online_type = -1;
276 
277 	return ret;
278 }
279 
280 static int memory_subsys_offline(struct device *dev)
281 {
282 	struct memory_block *mem = to_memory_block(dev);
283 
284 	if (mem->state == MEM_OFFLINE)
285 		return 0;
286 
287 	/* Can't offline block with non-present sections */
288 	if (mem->section_count != sections_per_block)
289 		return -EINVAL;
290 
291 	return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
292 }
293 
294 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
295 			   const char *buf, size_t count)
296 {
297 	struct memory_block *mem = to_memory_block(dev);
298 	int ret, online_type;
299 
300 	ret = lock_device_hotplug_sysfs();
301 	if (ret)
302 		return ret;
303 
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;
315 	}
316 
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 */
330 	}
331 
332 err:
333 	unlock_device_hotplug();
334 
335 	if (ret < 0)
336 		return ret;
337 	if (ret)
338 		return -EINVAL;
339 
340 	return count;
341 }
342 
343 /*
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?
351  */
352 static ssize_t phys_device_show(struct device *dev,
353 				struct device_attribute *attr, char *buf)
354 {
355 	struct memory_block *mem = to_memory_block(dev);
356 	return sprintf(buf, "%d\n", mem->phys_device);
357 }
358 
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)
363 {
364 	struct zone *zone;
365 
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);
370 	}
371 }
372 
373 static ssize_t valid_zones_show(struct device *dev,
374 				struct device_attribute *attr, char *buf)
375 {
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;
381 
382 	/*
383 	 * Check the existing zone. Make sure that we do that only on the
384 	 * online nodes otherwise the page_zone is not reliable
385 	 */
386 	if (mem->state == MEM_ONLINE) {
387 		/*
388 		 * The block contains more than one zone can not be offlined.
389 		 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
390 		 */
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;
397 	}
398 
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);
402 
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");
409 
410 	return strlen(buf);
411 }
412 static DEVICE_ATTR_RO(valid_zones);
413 #endif
414 
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);
419 
420 /*
421  * Show the memory block size (shared by all memory blocks).
422  */
423 static ssize_t block_size_bytes_show(struct device *dev,
424 				     struct device_attribute *attr, char *buf)
425 {
426 	return sprintf(buf, "%lx\n", memory_block_size_bytes());
427 }
428 
429 static DEVICE_ATTR_RO(block_size_bytes);
430 
431 /*
432  * Memory auto online policy.
433  */
434 
435 static ssize_t auto_online_blocks_show(struct device *dev,
436 				       struct device_attribute *attr, char *buf)
437 {
438 	if (memhp_auto_online)
439 		return sprintf(buf, "online\n");
440 	else
441 		return sprintf(buf, "offline\n");
442 }
443 
444 static ssize_t auto_online_blocks_store(struct device *dev,
445 					struct device_attribute *attr,
446 					const char *buf, size_t count)
447 {
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;
454 
455 	return count;
456 }
457 
458 static DEVICE_ATTR_RW(auto_online_blocks);
459 
460 /*
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.
465  */
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)
469 {
470 	u64 phys_addr;
471 	int nid, ret;
472 	unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
473 
474 	ret = kstrtoull(buf, 0, &phys_addr);
475 	if (ret)
476 		return ret;
477 
478 	if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
479 		return -EINVAL;
480 
481 	ret = lock_device_hotplug_sysfs();
482 	if (ret)
483 		return ret;
484 
485 	nid = memory_add_physaddr_to_nid(phys_addr);
486 	ret = __add_memory(nid, phys_addr,
487 			   MIN_MEMORY_BLOCK_SIZE * sections_per_block);
488 
489 	if (ret)
490 		goto out;
491 
492 	ret = count;
493 out:
494 	unlock_device_hotplug();
495 	return ret;
496 }
497 
498 static DEVICE_ATTR_WO(probe);
499 #endif
500 
501 #ifdef CONFIG_MEMORY_FAILURE
502 /*
503  * Support for offlining pages of memory
504  */
505 
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)
510 {
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;
520 }
521 
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)
526 {
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;
536 }
537 
538 static DEVICE_ATTR_WO(soft_offline_page);
539 static DEVICE_ATTR_WO(hard_offline_page);
540 #endif
541 
542 /*
543  * Note that phys_device is optional.  It is here to allow for
544  * differentiation between which *physical* devices each
545  * section belongs to...
546  */
547 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
548 {
549 	return 0;
550 }
551 
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)
554 {
555 	struct device *dev;
556 
557 	dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
558 	return dev ? to_memory_block(dev) : NULL;
559 }
560 
561 /*
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.
566  *
567  * This could be made generic for all device subsystems.
568  */
569 struct memory_block *find_memory_block(struct mem_section *section)
570 {
571 	unsigned long block_id = base_memory_block_id(__section_nr(section));
572 
573 	return find_memory_block_by_id(block_id);
574 }
575 
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
585 };
586 
587 static struct attribute_group memory_memblk_attr_group = {
588 	.attrs = memory_memblk_attrs,
589 };
590 
591 static const struct attribute_group *memory_memblk_attr_groups[] = {
592 	&memory_memblk_attr_group,
593 	NULL,
594 };
595 
596 /*
597  * register_memory - Setup a sysfs device for a memory block
598  */
599 static
600 int register_memory(struct memory_block *memory)
601 {
602 	int ret;
603 
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;
609 
610 	ret = device_register(&memory->dev);
611 	if (ret)
612 		put_device(&memory->dev);
613 
614 	return ret;
615 }
616 
617 static int init_memory_block(struct memory_block **memory,
618 			     unsigned long block_id, unsigned long state)
619 {
620 	struct memory_block *mem;
621 	unsigned long start_pfn;
622 	int ret = 0;
623 
624 	mem = find_memory_block_by_id(block_id);
625 	if (mem) {
626 		put_device(&mem->dev);
627 		return -EEXIST;
628 	}
629 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
630 	if (!mem)
631 		return -ENOMEM;
632 
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;
638 
639 	ret = register_memory(mem);
640 
641 	*memory = mem;
642 	return ret;
643 }
644 
645 static int add_memory_block(unsigned long base_section_nr)
646 {
647 	int ret, section_count = 0;
648 	struct memory_block *mem;
649 	unsigned long nr;
650 
651 	for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
652 	     nr++)
653 		if (present_section_nr(nr))
654 			section_count++;
655 
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;
664 }
665 
666 static void unregister_memory(struct memory_block *memory)
667 {
668 	if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
669 		return;
670 
671 	/* drop the ref. we got via find_memory_block() */
672 	put_device(&memory->dev);
673 	device_unregister(&memory->dev);
674 }
675 
676 /*
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.
680  *
681  * Called under device_hotplug_lock.
682  */
683 int create_memory_block_devices(unsigned long start, unsigned long size)
684 {
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;
690 
691 	if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
692 			 !IS_ALIGNED(size, memory_block_size_bytes())))
693 		return -EINVAL;
694 
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;
700 	}
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);
710 		}
711 	}
712 	return ret;
713 }
714 
715 /*
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.
719  *
720  * Called under device_hotplug_lock.
721  */
722 void remove_memory_block_devices(unsigned long start, unsigned long size)
723 {
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;
728 
729 	if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
730 			 !IS_ALIGNED(size, memory_block_size_bytes())))
731 		return;
732 
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);
740 	}
741 }
742 
743 /* return true if the memory block is offlined, otherwise, return false */
744 bool is_memblock_offlined(struct memory_block *mem)
745 {
746 	return mem->state == MEM_OFFLINE;
747 }
748 
749 static struct attribute *memory_root_attrs[] = {
750 #ifdef CONFIG_ARCH_MEMORY_PROBE
751 	&dev_attr_probe.attr,
752 #endif
753 
754 #ifdef CONFIG_MEMORY_FAILURE
755 	&dev_attr_soft_offline_page.attr,
756 	&dev_attr_hard_offline_page.attr,
757 #endif
758 
759 	&dev_attr_block_size_bytes.attr,
760 	&dev_attr_auto_online_blocks.attr,
761 	NULL
762 };
763 
764 static struct attribute_group memory_root_attr_group = {
765 	.attrs = memory_root_attrs,
766 };
767 
768 static const struct attribute_group *memory_root_attr_groups[] = {
769 	&memory_root_attr_group,
770 	NULL,
771 };
772 
773 /*
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.
777  */
778 void __init memory_dev_init(void)
779 {
780 	int ret;
781 	unsigned long block_sz, nr;
782 
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;
788 
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);
792 
793 	/*
794 	 * Create entries for memory sections that were found
795 	 * during boot and have been initialized
796 	 */
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);
803 	}
804 }
805 
806 /**
807  * walk_memory_blocks - walk through all present memory blocks overlapped
808  *			by the range [start, start + size)
809  *
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
814  *
815  * This function walks through all present memory blocks overlapped by the
816  * range [start, start + size), calling func on each memory block.
817  *
818  * In case func() returns an error, walking is aborted and the error is
819  * returned.
820  */
821 int walk_memory_blocks(unsigned long start, unsigned long size,
822 		       void *arg, walk_memory_blocks_func_t func)
823 {
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;
829 
830 	if (!size)
831 		return 0;
832 
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;
837 
838 		ret = func(mem, arg);
839 		put_device(&mem->dev);
840 		if (ret)
841 			break;
842 	}
843 	return ret;
844 }
845 
846 struct for_each_memory_block_cb_data {
847 	walk_memory_blocks_func_t func;
848 	void *arg;
849 };
850 
851 static int for_each_memory_block_cb(struct device *dev, void *data)
852 {
853 	struct memory_block *mem = to_memory_block(dev);
854 	struct for_each_memory_block_cb_data *cb_data = data;
855 
856 	return cb_data->func(mem, cb_data->arg);
857 }
858 
859 /**
860  * for_each_memory_block - walk through all present memory blocks
861  *
862  * @arg: argument passed to func
863  * @func: callback for each memory block walked
864  *
865  * This function walks through all present memory blocks, calling func on
866  * each memory block.
867  *
868  * In case func() returns an error, walking is aborted and the error is
869  * returned.
870  */
871 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
872 {
873 	struct for_each_memory_block_cb_data cb_data = {
874 		.func = func,
875 		.arg = arg,
876 	};
877 
878 	return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
879 				for_each_memory_block_cb);
880 }
881