xref: /linux/kernel/resource.c (revision 40286d6379aacfcc053253ef78dc78b09addffda)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *	linux/kernel/resource.c
4  *
5  * Copyright (C) 1999	Linus Torvalds
6  * Copyright (C) 1999	Martin Mares <mj@ucw.cz>
7  *
8  * Arbitrary resource management.
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/export.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/fs.h>
20 #include <linux/proc_fs.h>
21 #include <linux/pseudo_fs.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/device.h>
25 #include <linux/pfn.h>
26 #include <linux/mm.h>
27 #include <linux/mount.h>
28 #include <linux/resource_ext.h>
29 #include <uapi/linux/magic.h>
30 #include <linux/string.h>
31 #include <linux/vmalloc.h>
32 #include <asm/io.h>
33 
34 
35 struct resource ioport_resource = {
36 	.name	= "PCI IO",
37 	.start	= 0,
38 	.end	= IO_SPACE_LIMIT,
39 	.flags	= IORESOURCE_IO,
40 };
41 EXPORT_SYMBOL(ioport_resource);
42 
43 struct resource iomem_resource = {
44 	.name	= "PCI mem",
45 	.start	= 0,
46 	.end	= -1,
47 	.flags	= IORESOURCE_MEM,
48 };
49 EXPORT_SYMBOL(iomem_resource);
50 
51 struct resource soft_reserve_resource = {
52 	.name	= "Soft Reserved",
53 	.start	= 0,
54 	.end	= -1,
55 	.desc	= IORES_DESC_SOFT_RESERVED,
56 	.flags	= IORESOURCE_MEM,
57 };
58 
59 static DEFINE_RWLOCK(resource_lock);
60 
61 /*
62  * Return the next node of @p in pre-order tree traversal.  If
63  * @skip_children is true, skip the descendant nodes of @p in
64  * traversal.  If @p is a descendant of @subtree_root, only traverse
65  * the subtree under @subtree_root.
66  */
67 static struct resource *next_resource(struct resource *p, bool skip_children,
68 				      struct resource *subtree_root)
69 {
70 	if (!skip_children && p->child)
71 		return p->child;
72 	while (!p->sibling && p->parent) {
73 		p = p->parent;
74 		if (p == subtree_root)
75 			return NULL;
76 	}
77 	return p->sibling;
78 }
79 
80 /*
81  * Traverse the resource subtree under @_root in pre-order, excluding
82  * @_root itself.
83  *
84  * NOTE: '__p' is introduced to avoid shadowing '_p' outside of loop.
85  * And it is referenced to avoid unused variable warning.
86  */
87 #define for_each_resource(_root, _p, _skip_children) \
88 	for (typeof(_root) __root = (_root), __p = _p = __root->child;	\
89 	     __p && _p; _p = next_resource(_p, _skip_children, __root))
90 
91 #ifdef CONFIG_PROC_FS
92 
93 enum { MAX_IORES_LEVEL = 8 };
94 
95 static void *r_start(struct seq_file *m, loff_t *pos)
96 	__acquires(resource_lock)
97 {
98 	struct resource *root = pde_data(file_inode(m->file));
99 	struct resource *p;
100 	loff_t l = *pos;
101 
102 	read_lock(&resource_lock);
103 	for_each_resource(root, p, false) {
104 		if (l-- == 0)
105 			break;
106 	}
107 
108 	return p;
109 }
110 
111 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
112 {
113 	struct resource *p = v;
114 
115 	(*pos)++;
116 
117 	return (void *)next_resource(p, false, NULL);
118 }
119 
120 static void r_stop(struct seq_file *m, void *v)
121 	__releases(resource_lock)
122 {
123 	read_unlock(&resource_lock);
124 }
125 
126 static int r_show(struct seq_file *m, void *v)
127 {
128 	struct resource *root = pde_data(file_inode(m->file));
129 	struct resource *r = v, *p;
130 	unsigned long long start, end;
131 	int width = root->end < 0x10000 ? 4 : 8;
132 	int depth;
133 
134 	for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
135 		if (p->parent == root)
136 			break;
137 
138 	if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
139 		start = r->start;
140 		end = r->end;
141 	} else {
142 		start = end = 0;
143 	}
144 
145 	seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
146 			depth * 2, "",
147 			width, start,
148 			width, end,
149 			r->name ? r->name : "<BAD>");
150 	return 0;
151 }
152 
153 static const struct seq_operations resource_op = {
154 	.start	= r_start,
155 	.next	= r_next,
156 	.stop	= r_stop,
157 	.show	= r_show,
158 };
159 
160 static int __init ioresources_init(void)
161 {
162 	proc_create_seq_data("ioports", 0, NULL, &resource_op,
163 			&ioport_resource);
164 	proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
165 	return 0;
166 }
167 __initcall(ioresources_init);
168 
169 #endif /* CONFIG_PROC_FS */
170 
171 static void free_resource(struct resource *res)
172 {
173 	/**
174 	 * If the resource was allocated using memblock early during boot
175 	 * we'll leak it here: we can only return full pages back to the
176 	 * buddy and trying to be smart and reusing them eventually in
177 	 * alloc_resource() overcomplicates resource handling.
178 	 */
179 	if (res && PageSlab(virt_to_head_page(res)))
180 		kfree(res);
181 }
182 
183 static struct resource *alloc_resource(gfp_t flags)
184 {
185 	return kzalloc_obj(struct resource, flags);
186 }
187 
188 /* Return the conflict entry if you can't request it */
189 static struct resource * __request_resource(struct resource *root, struct resource *new)
190 {
191 	resource_size_t start = new->start;
192 	resource_size_t end = new->end;
193 	struct resource *tmp, **p;
194 
195 	if (end < start)
196 		return root;
197 	if (start < root->start)
198 		return root;
199 	if (end > root->end)
200 		return root;
201 	p = &root->child;
202 	for (;;) {
203 		tmp = *p;
204 		if (!tmp || tmp->start > end) {
205 			new->sibling = tmp;
206 			*p = new;
207 			new->parent = root;
208 			return NULL;
209 		}
210 		p = &tmp->sibling;
211 		if (tmp->end < start)
212 			continue;
213 		return tmp;
214 	}
215 }
216 
217 static int __release_resource(struct resource *old, bool release_child)
218 {
219 	struct resource *tmp, **p, *chd;
220 
221 	p = &old->parent->child;
222 	for (;;) {
223 		tmp = *p;
224 		if (!tmp)
225 			break;
226 		if (tmp == old) {
227 			if (release_child || !(tmp->child)) {
228 				*p = tmp->sibling;
229 			} else {
230 				for (chd = tmp->child;; chd = chd->sibling) {
231 					chd->parent = tmp->parent;
232 					if (!(chd->sibling))
233 						break;
234 				}
235 				*p = tmp->child;
236 				chd->sibling = tmp->sibling;
237 			}
238 			old->parent = NULL;
239 			return 0;
240 		}
241 		p = &tmp->sibling;
242 	}
243 	return -EINVAL;
244 }
245 
246 static void __release_child_resources(struct resource *r)
247 {
248 	struct resource *tmp, *p;
249 	resource_size_t size;
250 
251 	p = r->child;
252 	r->child = NULL;
253 	while (p) {
254 		tmp = p;
255 		p = p->sibling;
256 
257 		tmp->parent = NULL;
258 		tmp->sibling = NULL;
259 		__release_child_resources(tmp);
260 
261 		printk(KERN_DEBUG "release child resource %pR\n", tmp);
262 		/* need to restore size, and keep flags */
263 		size = resource_size(tmp);
264 		tmp->start = 0;
265 		tmp->end = size - 1;
266 	}
267 }
268 
269 void release_child_resources(struct resource *r)
270 {
271 	write_lock(&resource_lock);
272 	__release_child_resources(r);
273 	write_unlock(&resource_lock);
274 }
275 
276 /**
277  * request_resource_conflict - request and reserve an I/O or memory resource
278  * @root: root resource descriptor
279  * @new: resource descriptor desired by caller
280  *
281  * Returns 0 for success, conflict resource on error.
282  */
283 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
284 {
285 	struct resource *conflict;
286 
287 	write_lock(&resource_lock);
288 	conflict = __request_resource(root, new);
289 	write_unlock(&resource_lock);
290 	return conflict;
291 }
292 
293 /**
294  * request_resource - request and reserve an I/O or memory resource
295  * @root: root resource descriptor
296  * @new: resource descriptor desired by caller
297  *
298  * Returns 0 for success, negative error code on error.
299  */
300 int request_resource(struct resource *root, struct resource *new)
301 {
302 	struct resource *conflict;
303 
304 	conflict = request_resource_conflict(root, new);
305 	return conflict ? -EBUSY : 0;
306 }
307 
308 EXPORT_SYMBOL(request_resource);
309 
310 /**
311  * release_resource - release a previously reserved resource
312  * @old: resource pointer
313  */
314 int release_resource(struct resource *old)
315 {
316 	int retval;
317 
318 	write_lock(&resource_lock);
319 	retval = __release_resource(old, true);
320 	write_unlock(&resource_lock);
321 	return retval;
322 }
323 
324 EXPORT_SYMBOL(release_resource);
325 
326 static bool is_type_match(struct resource *p, unsigned long flags, unsigned long desc)
327 {
328 	return (p->flags & flags) == flags && (desc == IORES_DESC_NONE || desc == p->desc);
329 }
330 
331 /**
332  * find_next_res - Finds the lowest resource that covers part of
333  *		   [@start..@end].
334  *
335  * If a resource is found, returns 0 and @*res is overwritten with the part
336  * of the resource that's within [@start..@end]; if none is found, returns
337  * -ENODEV.  Returns -EINVAL for invalid parameters.
338  *
339  * @parent:	resource tree root to search
340  * @start:	start address of the resource searched for
341  * @end:	end address of same resource
342  * @flags:	flags which the resource must have
343  * @desc:	descriptor the resource must have
344  * @res:	return ptr, if resource found
345  *
346  * The caller must specify @start, @end, @flags, and @desc
347  * (which may be IORES_DESC_NONE).
348  */
349 static int find_next_res(struct resource *parent, resource_size_t start,
350 			 resource_size_t end, unsigned long flags,
351 			 unsigned long desc, struct resource *res)
352 {
353 	/* Skip children until we find a top level range that matches */
354 	bool skip_children = true;
355 	struct resource *p;
356 
357 	if (!res)
358 		return -EINVAL;
359 
360 	if (start >= end)
361 		return -EINVAL;
362 
363 	read_lock(&resource_lock);
364 
365 	for_each_resource(parent, p, skip_children) {
366 		/* If we passed the resource we are looking for, stop */
367 		if (p->start > end) {
368 			p = NULL;
369 			break;
370 		}
371 
372 		/* Skip until we find a range that matches what we look for */
373 		if (p->end < start)
374 			continue;
375 
376 		/*
377 		 * We found a top level range that matches what we are looking
378 		 * for. Time to start checking children too.
379 		 */
380 		skip_children = false;
381 
382 		/* Found a match, break */
383 		if (is_type_match(p, flags, desc))
384 			break;
385 	}
386 
387 	if (p) {
388 		/* copy data */
389 		*res = (struct resource) {
390 			.start = max(start, p->start),
391 			.end = min(end, p->end),
392 			.flags = p->flags,
393 			.desc = p->desc,
394 			.parent = p->parent,
395 		};
396 	}
397 
398 	read_unlock(&resource_lock);
399 	return p ? 0 : -ENODEV;
400 }
401 
402 static int find_next_iomem_res(resource_size_t start, resource_size_t end,
403 			       unsigned long flags, unsigned long desc,
404 			       struct resource *res)
405 {
406 	return find_next_res(&iomem_resource, start, end, flags, desc, res);
407 }
408 
409 static int walk_res_desc(struct resource *parent, resource_size_t start,
410 			 resource_size_t end, unsigned long flags,
411 			 unsigned long desc, void *arg,
412 			 int (*func)(struct resource *, void *))
413 {
414 	struct resource res;
415 	int ret = -EINVAL;
416 
417 	while (start < end &&
418 	       !find_next_res(parent, start, end, flags, desc, &res)) {
419 		ret = (*func)(&res, arg);
420 		if (ret)
421 			break;
422 
423 		start = res.end + 1;
424 	}
425 
426 	return ret;
427 }
428 
429 static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
430 				 unsigned long flags, unsigned long desc,
431 				 void *arg,
432 				 int (*func)(struct resource *, void *))
433 {
434 	return walk_res_desc(&iomem_resource, start, end, flags, desc, arg, func);
435 }
436 
437 
438 /**
439  * walk_iomem_res_desc - Walks through iomem resources and calls func()
440  *			 with matching resource ranges.
441  * *
442  * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
443  * @flags: I/O resource flags
444  * @start: start addr
445  * @end: end addr
446  * @arg: function argument for the callback @func
447  * @func: callback function that is called for each qualifying resource area
448  *
449  * All the memory ranges which overlap start,end and also match flags and
450  * desc are valid candidates.
451  *
452  * NOTE: For a new descriptor search, define a new IORES_DESC in
453  * <linux/ioport.h> and set it in 'desc' of a target resource entry.
454  */
455 int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
456 		u64 end, void *arg, int (*func)(struct resource *, void *))
457 {
458 	return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
459 }
460 EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
461 
462 /*
463  * In support of device drivers claiming Soft Reserved resources, walk the Soft
464  * Reserved resource deferral tree.
465  */
466 int walk_soft_reserve_res(u64 start, u64 end, void *arg,
467 			  int (*func)(struct resource *, void *))
468 {
469 	return walk_res_desc(&soft_reserve_resource, start, end, IORESOURCE_MEM,
470 			     IORES_DESC_SOFT_RESERVED, arg, func);
471 }
472 EXPORT_SYMBOL_GPL(walk_soft_reserve_res);
473 
474 /*
475  * This function calls the @func callback against all memory ranges of type
476  * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
477  * Now, this function is only for System RAM, it deals with full ranges and
478  * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
479  * ranges.
480  */
481 int walk_system_ram_res(u64 start, u64 end, void *arg,
482 			int (*func)(struct resource *, void *))
483 {
484 	unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
485 
486 	return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
487 				     func);
488 }
489 
490 /*
491  * This function, being a variant of walk_system_ram_res(), calls the @func
492  * callback against all memory ranges of type System RAM which are marked as
493  * IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY in reversed order, i.e., from
494  * higher to lower.
495  */
496 int walk_system_ram_res_rev(u64 start, u64 end, void *arg,
497 				int (*func)(struct resource *, void *))
498 {
499 	struct resource res, *rams;
500 	int rams_size = 16, i;
501 	unsigned long flags;
502 	int ret = -1;
503 
504 	/* create a list */
505 	rams = kvzalloc_objs(struct resource, rams_size);
506 	if (!rams)
507 		return ret;
508 
509 	flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
510 	i = 0;
511 	while ((start < end) &&
512 		(!find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res))) {
513 		if (i >= rams_size) {
514 			/* re-alloc */
515 			struct resource *rams_new;
516 
517 			rams_new = kvrealloc(rams, (rams_size + 16) * sizeof(struct resource),
518 					     GFP_KERNEL);
519 			if (!rams_new)
520 				goto out;
521 
522 			rams = rams_new;
523 			rams_size += 16;
524 		}
525 
526 		rams[i++] = res;
527 		start = res.end + 1;
528 	}
529 
530 	/* go reverse */
531 	for (i--; i >= 0; i--) {
532 		ret = (*func)(&rams[i], arg);
533 		if (ret)
534 			break;
535 	}
536 
537 out:
538 	kvfree(rams);
539 	return ret;
540 }
541 
542 /*
543  * This function calls the @func callback against all memory ranges, which
544  * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
545  */
546 int walk_mem_res(u64 start, u64 end, void *arg,
547 		 int (*func)(struct resource *, void *))
548 {
549 	unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
550 
551 	return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
552 				     func);
553 }
554 
555 /*
556  * This function calls the @func callback against all memory ranges of type
557  * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
558  * It is to be used only for System RAM.
559  */
560 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
561 			  void *arg, int (*func)(unsigned long, unsigned long, void *))
562 {
563 	resource_size_t start, end;
564 	unsigned long flags;
565 	struct resource res;
566 	unsigned long pfn, end_pfn;
567 	int ret = -EINVAL;
568 
569 	start = (u64) start_pfn << PAGE_SHIFT;
570 	end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
571 	flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
572 	while (start < end &&
573 	       !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
574 		pfn = PFN_UP(res.start);
575 		end_pfn = PFN_DOWN(res.end + 1);
576 		if (end_pfn > pfn)
577 			ret = (*func)(pfn, end_pfn - pfn, arg);
578 		if (ret)
579 			break;
580 		start = res.end + 1;
581 	}
582 	return ret;
583 }
584 
585 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
586 {
587 	return 1;
588 }
589 
590 /*
591  * This generic page_is_ram() returns true if specified address is
592  * registered as System RAM in iomem_resource list.
593  */
594 int __weak page_is_ram(unsigned long pfn)
595 {
596 	return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
597 }
598 EXPORT_SYMBOL_GPL(page_is_ram);
599 
600 static int __region_intersects(struct resource *parent, resource_size_t start,
601 			       size_t size, unsigned long flags,
602 			       unsigned long desc)
603 {
604 	int type = 0; int other = 0;
605 	struct resource *p, *dp;
606 	struct resource res, o;
607 	bool covered;
608 
609 	res = DEFINE_RES(start, size, 0);
610 
611 	for (p = parent->child; p ; p = p->sibling) {
612 		if (!resource_intersection(p, &res, &o))
613 			continue;
614 		if (is_type_match(p, flags, desc)) {
615 			type++;
616 			continue;
617 		}
618 		/*
619 		 * Continue to search in descendant resources as if the
620 		 * matched descendant resources cover some ranges of 'p'.
621 		 *
622 		 * |------------- "CXL Window 0" ------------|
623 		 * |-- "System RAM" --|
624 		 *
625 		 * will behave similar as the following fake resource
626 		 * tree when searching "System RAM".
627 		 *
628 		 * |-- "System RAM" --||-- "CXL Window 0a" --|
629 		 */
630 		covered = false;
631 		for_each_resource(p, dp, false) {
632 			if (!resource_overlaps(dp, &res))
633 				continue;
634 			if (is_type_match(dp, flags, desc)) {
635 				type++;
636 				/*
637 				 * Range from 'o.start' to 'dp->start'
638 				 * isn't covered by matched resource.
639 				 */
640 				if (dp->start > o.start)
641 					break;
642 				if (dp->end >= o.end) {
643 					covered = true;
644 					break;
645 				}
646 				/* Remove covered range */
647 				o.start = max(o.start, dp->end + 1);
648 			}
649 		}
650 		if (!covered)
651 			other++;
652 	}
653 
654 	if (type == 0)
655 		return REGION_DISJOINT;
656 
657 	if (other == 0)
658 		return REGION_INTERSECTS;
659 
660 	return REGION_MIXED;
661 }
662 
663 /**
664  * region_intersects() - determine intersection of region with known resources
665  * @start: region start address
666  * @size: size of region
667  * @flags: flags of resource (in iomem_resource)
668  * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
669  *
670  * Check if the specified region partially overlaps or fully eclipses a
671  * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
672  * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
673  * return REGION_MIXED if the region overlaps @flags/@desc and another
674  * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
675  * and no other defined resource. Note that REGION_INTERSECTS is also
676  * returned in the case when the specified region overlaps RAM and undefined
677  * memory holes.
678  *
679  * region_intersect() is used by memory remapping functions to ensure
680  * the user is not remapping RAM and is a vast speed up over walking
681  * through the resource table page by page.
682  */
683 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
684 		      unsigned long desc)
685 {
686 	int ret;
687 
688 	read_lock(&resource_lock);
689 	ret = __region_intersects(&iomem_resource, start, size, flags, desc);
690 	read_unlock(&resource_lock);
691 
692 	return ret;
693 }
694 EXPORT_SYMBOL_GPL(region_intersects);
695 
696 /*
697  * Check if the provided range is registered in the Soft Reserved resource
698  * deferral tree for driver consideration.
699  */
700 int region_intersects_soft_reserve(resource_size_t start, size_t size)
701 {
702 	guard(read_lock)(&resource_lock);
703 	return __region_intersects(&soft_reserve_resource, start, size,
704 				   IORESOURCE_MEM, IORES_DESC_SOFT_RESERVED);
705 }
706 EXPORT_SYMBOL_GPL(region_intersects_soft_reserve);
707 
708 void __weak arch_remove_reservations(struct resource *avail)
709 {
710 }
711 
712 static void resource_clip(struct resource *res, resource_size_t min,
713 			  resource_size_t max)
714 {
715 	if (res->start < min)
716 		res->start = min;
717 	if (res->end > max)
718 		res->end = max;
719 }
720 
721 /*
722  * Find empty space in the resource tree with the given range and
723  * alignment constraints
724  */
725 static int __find_resource_space(struct resource *root, struct resource *old,
726 				 struct resource *new, resource_size_t size,
727 				 struct resource_constraint *constraint)
728 {
729 	struct resource *this = root->child;
730 	struct resource full_avail = *new, avail, alloc;
731 	resource_alignf alignf = constraint->alignf;
732 
733 	full_avail.start = root->start;
734 	/*
735 	 * Skip past an allocated resource that starts at 0, since the assignment
736 	 * of this->start - 1 to full_avail->end below would cause an underflow.
737 	 */
738 	if (this && this->start == root->start) {
739 		full_avail.start = (this == old) ? old->start : this->end + 1;
740 		this = this->sibling;
741 	}
742 	for(;;) {
743 		if (this)
744 			full_avail.end = (this == old) ?  this->end : this->start - 1;
745 		else
746 			full_avail.end = root->end;
747 
748 		if (full_avail.end < full_avail.start)
749 			goto next;
750 
751 		resource_clip(&full_avail, constraint->min, constraint->max);
752 		arch_remove_reservations(&full_avail);
753 
754 		/* Check for overflow after ALIGN() */
755 		avail.start = ALIGN(full_avail.start, constraint->align);
756 		avail.end = full_avail.end;
757 		avail.flags = new->flags;
758 		if (avail.start >= full_avail.start) {
759 			alloc.flags = avail.flags;
760 			if (alignf) {
761 				alloc.start = alignf(constraint->alignf_data,
762 						     &avail, &full_avail,
763 						     size, constraint->align);
764 			} else {
765 				alloc.start = avail.start;
766 			}
767 			alloc.end = alloc.start + size - 1;
768 			if (alloc.start <= alloc.end &&
769 			    __resource_contains_unbound(&full_avail, &alloc)) {
770 				new->start = alloc.start;
771 				new->end = alloc.end;
772 				return 0;
773 			}
774 		}
775 
776 next:		if (!this || this->end == root->end)
777 			break;
778 
779 		if (this != old)
780 			full_avail.start = this->end + 1;
781 		this = this->sibling;
782 	}
783 	return -EBUSY;
784 }
785 
786 /**
787  * find_resource_space - Find empty space in the resource tree
788  * @root:	Root resource descriptor
789  * @new:	Resource descriptor awaiting an empty resource space
790  * @size:	The minimum size of the empty space
791  * @constraint:	The range and alignment constraints to be met
792  *
793  * Finds an empty space under @root in the resource tree satisfying range and
794  * alignment @constraints.
795  *
796  * Return:
797  * * %0		- if successful, @new members start, end, and flags are altered.
798  * * %-EBUSY	- if no empty space was found.
799  */
800 int find_resource_space(struct resource *root, struct resource *new,
801 			resource_size_t size,
802 			struct resource_constraint *constraint)
803 {
804 	return  __find_resource_space(root, NULL, new, size, constraint);
805 }
806 EXPORT_SYMBOL_GPL(find_resource_space);
807 
808 /**
809  * reallocate_resource - allocate a slot in the resource tree given range & alignment.
810  *	The resource will be relocated if the new size cannot be reallocated in the
811  *	current location.
812  *
813  * @root: root resource descriptor
814  * @old:  resource descriptor desired by caller
815  * @newsize: new size of the resource descriptor
816  * @constraint: the memory range and alignment constraints to be met.
817  */
818 static int reallocate_resource(struct resource *root, struct resource *old,
819 			       resource_size_t newsize,
820 			       struct resource_constraint *constraint)
821 {
822 	int err=0;
823 	struct resource new = *old;
824 	struct resource *conflict;
825 
826 	write_lock(&resource_lock);
827 
828 	if ((err = __find_resource_space(root, old, &new, newsize, constraint)))
829 		goto out;
830 
831 	if (resource_contains(&new, old)) {
832 		old->start = new.start;
833 		old->end = new.end;
834 		goto out;
835 	}
836 
837 	if (old->child) {
838 		err = -EBUSY;
839 		goto out;
840 	}
841 
842 	if (resource_contains(old, &new)) {
843 		old->start = new.start;
844 		old->end = new.end;
845 	} else {
846 		__release_resource(old, true);
847 		*old = new;
848 		conflict = __request_resource(root, old);
849 		BUG_ON(conflict);
850 	}
851 out:
852 	write_unlock(&resource_lock);
853 	return err;
854 }
855 
856 
857 /**
858  * allocate_resource - allocate empty slot in the resource tree given range & alignment.
859  * 	The resource will be reallocated with a new size if it was already allocated
860  * @root: root resource descriptor
861  * @new: resource descriptor desired by caller
862  * @size: requested resource region size
863  * @min: minimum boundary to allocate
864  * @max: maximum boundary to allocate
865  * @align: alignment requested, in bytes
866  * @alignf: alignment function, optional, called if not NULL
867  * @alignf_data: arbitrary data to pass to the @alignf function
868  */
869 int allocate_resource(struct resource *root, struct resource *new,
870 		      resource_size_t size, resource_size_t min,
871 		      resource_size_t max, resource_size_t align,
872 		      resource_alignf alignf,
873 		      void *alignf_data)
874 {
875 	int err;
876 	struct resource_constraint constraint;
877 
878 	constraint.min = min;
879 	constraint.max = max;
880 	constraint.align = align;
881 	constraint.alignf = alignf;
882 	constraint.alignf_data = alignf_data;
883 
884 	if ( new->parent ) {
885 		/* resource is already allocated, try reallocating with
886 		   the new constraints */
887 		return reallocate_resource(root, new, size, &constraint);
888 	}
889 
890 	write_lock(&resource_lock);
891 	err = find_resource_space(root, new, size, &constraint);
892 	if (err >= 0 && __request_resource(root, new))
893 		err = -EBUSY;
894 	write_unlock(&resource_lock);
895 	return err;
896 }
897 
898 EXPORT_SYMBOL(allocate_resource);
899 
900 /**
901  * lookup_resource - find an existing resource by a resource start address
902  * @root: root resource descriptor
903  * @start: resource start address
904  *
905  * Returns a pointer to the resource if found, NULL otherwise
906  */
907 struct resource *lookup_resource(struct resource *root, resource_size_t start)
908 {
909 	struct resource *res;
910 
911 	read_lock(&resource_lock);
912 	for (res = root->child; res; res = res->sibling) {
913 		if (res->start == start)
914 			break;
915 	}
916 	read_unlock(&resource_lock);
917 
918 	return res;
919 }
920 
921 /*
922  * Insert a resource into the resource tree. If successful, return NULL,
923  * otherwise return the conflicting resource (compare to __request_resource())
924  */
925 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
926 {
927 	struct resource *first, *next;
928 
929 	for (;; parent = first) {
930 		first = __request_resource(parent, new);
931 		if (!first)
932 			return first;
933 
934 		if (first == parent)
935 			return first;
936 		if (WARN_ON(first == new))	/* duplicated insertion */
937 			return first;
938 
939 		if ((first->start > new->start) || (first->end < new->end))
940 			break;
941 		if ((first->start == new->start) && (first->end == new->end))
942 			break;
943 	}
944 
945 	for (next = first; ; next = next->sibling) {
946 		/* Partial overlap? Bad, and unfixable */
947 		if (next->start < new->start || next->end > new->end)
948 			return next;
949 		if (!next->sibling)
950 			break;
951 		if (next->sibling->start > new->end)
952 			break;
953 	}
954 
955 	new->parent = parent;
956 	new->sibling = next->sibling;
957 	new->child = first;
958 
959 	next->sibling = NULL;
960 	for (next = first; next; next = next->sibling)
961 		next->parent = new;
962 
963 	if (parent->child == first) {
964 		parent->child = new;
965 	} else {
966 		next = parent->child;
967 		while (next->sibling != first)
968 			next = next->sibling;
969 		next->sibling = new;
970 	}
971 	return NULL;
972 }
973 
974 /**
975  * insert_resource_conflict - Inserts resource in the resource tree
976  * @parent: parent of the new resource
977  * @new: new resource to insert
978  *
979  * Returns 0 on success, conflict resource if the resource can't be inserted.
980  *
981  * This function is equivalent to request_resource_conflict when no conflict
982  * happens. If a conflict happens, and the conflicting resources
983  * entirely fit within the range of the new resource, then the new
984  * resource is inserted and the conflicting resources become children of
985  * the new resource.
986  *
987  * This function is intended for producers of resources, such as FW modules
988  * and bus drivers.
989  */
990 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
991 {
992 	struct resource *conflict;
993 
994 	write_lock(&resource_lock);
995 	conflict = __insert_resource(parent, new);
996 	write_unlock(&resource_lock);
997 	return conflict;
998 }
999 
1000 /**
1001  * insert_resource - Inserts a resource in the resource tree
1002  * @parent: parent of the new resource
1003  * @new: new resource to insert
1004  *
1005  * Returns 0 on success, -EBUSY if the resource can't be inserted.
1006  *
1007  * This function is intended for producers of resources, such as FW modules
1008  * and bus drivers.
1009  */
1010 int insert_resource(struct resource *parent, struct resource *new)
1011 {
1012 	struct resource *conflict;
1013 
1014 	conflict = insert_resource_conflict(parent, new);
1015 	return conflict ? -EBUSY : 0;
1016 }
1017 EXPORT_SYMBOL_GPL(insert_resource);
1018 
1019 /**
1020  * insert_resource_expand_to_fit - Insert a resource into the resource tree
1021  * @root: root resource descriptor
1022  * @new: new resource to insert
1023  *
1024  * Insert a resource into the resource tree, possibly expanding it in order
1025  * to make it encompass any conflicting resources.
1026  */
1027 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
1028 {
1029 	if (new->parent)
1030 		return;
1031 
1032 	write_lock(&resource_lock);
1033 	for (;;) {
1034 		struct resource *conflict;
1035 
1036 		conflict = __insert_resource(root, new);
1037 		if (!conflict)
1038 			break;
1039 		if (conflict == root)
1040 			break;
1041 
1042 		/* Ok, expand resource to cover the conflict, then try again .. */
1043 		if (conflict->start < new->start)
1044 			new->start = conflict->start;
1045 		if (conflict->end > new->end)
1046 			new->end = conflict->end;
1047 
1048 		pr_info("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
1049 	}
1050 	write_unlock(&resource_lock);
1051 }
1052 /*
1053  * Not for general consumption, only early boot memory map parsing, PCI
1054  * resource discovery, and late discovery of CXL resources are expected
1055  * to use this interface. The former are built-in and only the latter,
1056  * CXL, is a module.
1057  */
1058 EXPORT_SYMBOL_NS_GPL(insert_resource_expand_to_fit, "CXL");
1059 
1060 /**
1061  * remove_resource - Remove a resource in the resource tree
1062  * @old: resource to remove
1063  *
1064  * Returns 0 on success, -EINVAL if the resource is not valid.
1065  *
1066  * This function removes a resource previously inserted by insert_resource()
1067  * or insert_resource_conflict(), and moves the children (if any) up to
1068  * where they were before.  insert_resource() and insert_resource_conflict()
1069  * insert a new resource, and move any conflicting resources down to the
1070  * children of the new resource.
1071  *
1072  * insert_resource(), insert_resource_conflict() and remove_resource() are
1073  * intended for producers of resources, such as FW modules and bus drivers.
1074  */
1075 int remove_resource(struct resource *old)
1076 {
1077 	int retval;
1078 
1079 	write_lock(&resource_lock);
1080 	retval = __release_resource(old, false);
1081 	write_unlock(&resource_lock);
1082 	return retval;
1083 }
1084 EXPORT_SYMBOL_GPL(remove_resource);
1085 
1086 static int __adjust_resource(struct resource *res, resource_size_t start,
1087 				resource_size_t size)
1088 {
1089 	struct resource *tmp, *parent = res->parent;
1090 	resource_size_t end = start + size - 1;
1091 	int result = -EBUSY;
1092 
1093 	if (!parent)
1094 		goto skip;
1095 
1096 	if ((start < parent->start) || (end > parent->end))
1097 		goto out;
1098 
1099 	if (res->sibling && (res->sibling->start <= end))
1100 		goto out;
1101 
1102 	tmp = parent->child;
1103 	if (tmp != res) {
1104 		while (tmp->sibling != res)
1105 			tmp = tmp->sibling;
1106 		if (start <= tmp->end)
1107 			goto out;
1108 	}
1109 
1110 skip:
1111 	for (tmp = res->child; tmp; tmp = tmp->sibling)
1112 		if ((tmp->start < start) || (tmp->end > end))
1113 			goto out;
1114 
1115 	res->start = start;
1116 	res->end = end;
1117 	result = 0;
1118 
1119  out:
1120 	return result;
1121 }
1122 
1123 /**
1124  * adjust_resource - modify a resource's start and size
1125  * @res: resource to modify
1126  * @start: new start value
1127  * @size: new size
1128  *
1129  * Given an existing resource, change its start and size to match the
1130  * arguments.  Returns 0 on success, -EBUSY if it can't fit.
1131  * Existing children of the resource are assumed to be immutable.
1132  */
1133 int adjust_resource(struct resource *res, resource_size_t start,
1134 		    resource_size_t size)
1135 {
1136 	int result;
1137 
1138 	write_lock(&resource_lock);
1139 	result = __adjust_resource(res, start, size);
1140 	write_unlock(&resource_lock);
1141 	return result;
1142 }
1143 EXPORT_SYMBOL(adjust_resource);
1144 
1145 static void __init
1146 __reserve_region_with_split(struct resource *root, resource_size_t start,
1147 			    resource_size_t end, const char *name)
1148 {
1149 	struct resource *parent = root;
1150 	struct resource *conflict;
1151 	struct resource *res = alloc_resource(GFP_ATOMIC);
1152 	struct resource *next_res = NULL;
1153 	int type = resource_type(root);
1154 
1155 	if (!res)
1156 		return;
1157 
1158 	res->name = name;
1159 	res->start = start;
1160 	res->end = end;
1161 	res->flags = type | IORESOURCE_BUSY;
1162 	res->desc = IORES_DESC_NONE;
1163 
1164 	while (1) {
1165 
1166 		conflict = __request_resource(parent, res);
1167 		if (!conflict) {
1168 			if (!next_res)
1169 				break;
1170 			res = next_res;
1171 			next_res = NULL;
1172 			continue;
1173 		}
1174 
1175 		/* conflict covered whole area */
1176 		if (conflict->start <= res->start &&
1177 				conflict->end >= res->end) {
1178 			free_resource(res);
1179 			WARN_ON(next_res);
1180 			break;
1181 		}
1182 
1183 		/* failed, split and try again */
1184 		if (conflict->start > res->start) {
1185 			end = res->end;
1186 			res->end = conflict->start - 1;
1187 			if (conflict->end < end) {
1188 				next_res = alloc_resource(GFP_ATOMIC);
1189 				if (!next_res) {
1190 					free_resource(res);
1191 					break;
1192 				}
1193 				next_res->name = name;
1194 				next_res->start = conflict->end + 1;
1195 				next_res->end = end;
1196 				next_res->flags = type | IORESOURCE_BUSY;
1197 				next_res->desc = IORES_DESC_NONE;
1198 			}
1199 		} else {
1200 			res->start = conflict->end + 1;
1201 		}
1202 	}
1203 
1204 }
1205 
1206 void __init
1207 reserve_region_with_split(struct resource *root, resource_size_t start,
1208 			  resource_size_t end, const char *name)
1209 {
1210 	int abort = 0;
1211 
1212 	write_lock(&resource_lock);
1213 	if (root->start > start || root->end < end) {
1214 		pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1215 		       (unsigned long long)start, (unsigned long long)end,
1216 		       root);
1217 		if (start > root->end || end < root->start)
1218 			abort = 1;
1219 		else {
1220 			if (end > root->end)
1221 				end = root->end;
1222 			if (start < root->start)
1223 				start = root->start;
1224 			pr_err("fixing request to [0x%llx-0x%llx]\n",
1225 			       (unsigned long long)start,
1226 			       (unsigned long long)end);
1227 		}
1228 		dump_stack();
1229 	}
1230 	if (!abort)
1231 		__reserve_region_with_split(root, start, end, name);
1232 	write_unlock(&resource_lock);
1233 }
1234 
1235 /**
1236  * resource_alignment - calculate resource's alignment
1237  * @res: resource pointer
1238  *
1239  * Returns alignment on success, 0 (invalid alignment) on failure.
1240  */
1241 resource_size_t resource_alignment(struct resource *res)
1242 {
1243 	switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1244 	case IORESOURCE_SIZEALIGN:
1245 		return resource_size(res);
1246 	case IORESOURCE_STARTALIGN:
1247 		return res->start;
1248 	default:
1249 		return 0;
1250 	}
1251 }
1252 
1253 /*
1254  * This is compatibility stuff for IO resources.
1255  *
1256  * Note how this, unlike the above, knows about
1257  * the IO flag meanings (busy etc).
1258  *
1259  * request_region creates a new busy region.
1260  *
1261  * release_region releases a matching busy region.
1262  */
1263 
1264 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1265 
1266 static struct inode *iomem_inode;
1267 
1268 #ifdef CONFIG_IO_STRICT_DEVMEM
1269 static void revoke_iomem(struct resource *res)
1270 {
1271 	/* pairs with smp_store_release() in iomem_init_inode() */
1272 	struct inode *inode = smp_load_acquire(&iomem_inode);
1273 
1274 	/*
1275 	 * Check that the initialization has completed. Losing the race
1276 	 * is ok because it means drivers are claiming resources before
1277 	 * the fs_initcall level of init and prevent iomem_get_mapping users
1278 	 * from establishing mappings.
1279 	 */
1280 	if (!inode)
1281 		return;
1282 
1283 	/*
1284 	 * The expectation is that the driver has successfully marked
1285 	 * the resource busy by this point, so devmem_is_allowed()
1286 	 * should start returning false, however for performance this
1287 	 * does not iterate the entire resource range.
1288 	 */
1289 	if (devmem_is_allowed(PHYS_PFN(res->start)) &&
1290 	    devmem_is_allowed(PHYS_PFN(res->end))) {
1291 		/*
1292 		 * *cringe* iomem=relaxed says "go ahead, what's the
1293 		 * worst that can happen?"
1294 		 */
1295 		return;
1296 	}
1297 
1298 	unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
1299 }
1300 #else
1301 static void revoke_iomem(struct resource *res) {}
1302 #endif
1303 
1304 struct address_space *iomem_get_mapping(void)
1305 {
1306 	/*
1307 	 * This function is only called from file open paths, hence guaranteed
1308 	 * that fs_initcalls have completed and no need to check for NULL. But
1309 	 * since revoke_iomem can be called before the initcall we still need
1310 	 * the barrier to appease checkers.
1311 	 */
1312 	return smp_load_acquire(&iomem_inode)->i_mapping;
1313 }
1314 
1315 static int __request_region_locked(struct resource *res, struct resource *parent,
1316 				   resource_size_t start, resource_size_t n,
1317 				   const char *name, int flags)
1318 {
1319 	DECLARE_WAITQUEUE(wait, current);
1320 
1321 	res->name = name;
1322 	res->start = start;
1323 	res->end = start + n - 1;
1324 
1325 	for (;;) {
1326 		struct resource *conflict;
1327 
1328 		res->flags = resource_type(parent) | resource_ext_type(parent);
1329 		res->flags |= IORESOURCE_BUSY | flags;
1330 		res->desc = parent->desc;
1331 
1332 		conflict = __request_resource(parent, res);
1333 		if (!conflict)
1334 			break;
1335 		/*
1336 		 * mm/hmm.c reserves physical addresses which then
1337 		 * become unavailable to other users.  Conflicts are
1338 		 * not expected.  Warn to aid debugging if encountered.
1339 		 */
1340 		if (parent == &iomem_resource &&
1341 		    conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1342 			pr_warn("Unaddressable device %s %pR conflicts with %pR\n",
1343 				conflict->name, conflict, res);
1344 		}
1345 		if (conflict != parent) {
1346 			if (!(conflict->flags & IORESOURCE_BUSY)) {
1347 				parent = conflict;
1348 				continue;
1349 			}
1350 		}
1351 		if (conflict->flags & flags & IORESOURCE_MUXED) {
1352 			add_wait_queue(&muxed_resource_wait, &wait);
1353 			write_unlock(&resource_lock);
1354 			set_current_state(TASK_UNINTERRUPTIBLE);
1355 			schedule();
1356 			remove_wait_queue(&muxed_resource_wait, &wait);
1357 			write_lock(&resource_lock);
1358 			continue;
1359 		}
1360 		/* Uhhuh, that didn't work out.. */
1361 		return -EBUSY;
1362 	}
1363 
1364 	return 0;
1365 }
1366 
1367 /**
1368  * __request_region - create a new busy resource region
1369  * @parent: parent resource descriptor
1370  * @start: resource start address
1371  * @n: resource region size
1372  * @name: reserving caller's ID string
1373  * @flags: IO resource flags
1374  */
1375 struct resource *__request_region(struct resource *parent,
1376 				  resource_size_t start, resource_size_t n,
1377 				  const char *name, int flags)
1378 {
1379 	struct resource *res = alloc_resource(GFP_KERNEL);
1380 	int ret;
1381 
1382 	if (!res)
1383 		return NULL;
1384 
1385 	write_lock(&resource_lock);
1386 	ret = __request_region_locked(res, parent, start, n, name, flags);
1387 	write_unlock(&resource_lock);
1388 
1389 	if (ret) {
1390 		free_resource(res);
1391 		return NULL;
1392 	}
1393 
1394 	if (parent == &iomem_resource)
1395 		revoke_iomem(res);
1396 
1397 	return res;
1398 }
1399 EXPORT_SYMBOL(__request_region);
1400 
1401 /**
1402  * __release_region - release a previously reserved resource region
1403  * @parent: parent resource descriptor
1404  * @start: resource start address
1405  * @n: resource region size
1406  *
1407  * The described resource region must match a currently busy region.
1408  */
1409 void __release_region(struct resource *parent, resource_size_t start,
1410 		      resource_size_t n)
1411 {
1412 	struct resource **p;
1413 	resource_size_t end;
1414 
1415 	p = &parent->child;
1416 	end = start + n - 1;
1417 
1418 	write_lock(&resource_lock);
1419 
1420 	for (;;) {
1421 		struct resource *res = *p;
1422 
1423 		if (!res)
1424 			break;
1425 		if (res->start <= start && res->end >= end) {
1426 			if (!(res->flags & IORESOURCE_BUSY)) {
1427 				p = &res->child;
1428 				continue;
1429 			}
1430 			if (res->start != start || res->end != end)
1431 				break;
1432 			*p = res->sibling;
1433 			write_unlock(&resource_lock);
1434 			if (res->flags & IORESOURCE_MUXED)
1435 				wake_up(&muxed_resource_wait);
1436 			free_resource(res);
1437 			return;
1438 		}
1439 		p = &res->sibling;
1440 	}
1441 
1442 	write_unlock(&resource_lock);
1443 
1444 	pr_warn("Trying to free nonexistent resource <%pa-%pa>\n", &start, &end);
1445 }
1446 EXPORT_SYMBOL(__release_region);
1447 
1448 #ifdef CONFIG_MEMORY_HOTREMOVE
1449 static void append_child_to_parent(struct resource *new_parent, struct resource *new_child)
1450 {
1451 	struct resource *child;
1452 
1453 	child = new_parent->child;
1454 	if (child) {
1455 		while (child->sibling)
1456 			child = child->sibling;
1457 		child->sibling = new_child;
1458 	} else {
1459 		new_parent->child = new_child;
1460 	}
1461 	new_child->parent = new_parent;
1462 	new_child->sibling = NULL;
1463 }
1464 
1465 /*
1466  * Reparent all child resources that no longer belong to "low" after a split to
1467  * "high". Note that "high" does not have any children, because "low" is the
1468  * original resource and "high" is a new resource. Treat "low" as the original
1469  * resource being split and defer its range adjustment to __adjust_resource().
1470  */
1471 static void reparent_children_after_split(struct resource *low,
1472 					  struct resource *high,
1473 					  resource_size_t split_addr)
1474 {
1475 	struct resource *child, *next, **p;
1476 
1477 	p = &low->child;
1478 	while ((child = *p)) {
1479 		next = child->sibling;
1480 		if (child->start > split_addr) {
1481 			/* unlink child */
1482 			*p = next;
1483 			append_child_to_parent(high, child);
1484 		} else {
1485 			p = &child->sibling;
1486 		}
1487 	}
1488 }
1489 
1490 /**
1491  * release_mem_region_adjustable - release a previously reserved memory region
1492  * @start: resource start address
1493  * @size: resource region size
1494  *
1495  * This interface is intended for memory hot-delete.  The requested region
1496  * is released from a currently busy memory resource.  The requested region
1497  * must either match exactly or fit into a single busy resource entry.  In
1498  * the latter case, the remaining resource is adjusted accordingly.
1499  *
1500  * Note:
1501  * - Additional release conditions, such as overlapping region, can be
1502  *   supported after they are confirmed as valid cases.
1503  * - When a busy memory resource gets split into two entries, its children are
1504  *   reassigned to the correct parent based on their range. If a child memory
1505  *   resource overlaps with more than one parent, enhance the logic as needed.
1506  */
1507 void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
1508 {
1509 	struct resource *parent = &iomem_resource;
1510 	struct resource *new_res = NULL;
1511 	bool alloc_nofail = false;
1512 	struct resource **p;
1513 	struct resource *res;
1514 	resource_size_t end;
1515 
1516 	end = start + size - 1;
1517 	if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1518 		return;
1519 
1520 	/*
1521 	 * We free up quite a lot of memory on memory hotunplug (esp., memap),
1522 	 * just before releasing the region. This is highly unlikely to
1523 	 * fail - let's play save and make it never fail as the caller cannot
1524 	 * perform any error handling (e.g., trying to re-add memory will fail
1525 	 * similarly).
1526 	 */
1527 retry:
1528 	new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
1529 
1530 	p = &parent->child;
1531 	write_lock(&resource_lock);
1532 
1533 	while ((res = *p)) {
1534 		if (res->start >= end)
1535 			break;
1536 
1537 		/* look for the next resource if it does not fit into */
1538 		if (res->start > start || res->end < end) {
1539 			p = &res->sibling;
1540 			continue;
1541 		}
1542 
1543 		if (!(res->flags & IORESOURCE_MEM))
1544 			break;
1545 
1546 		if (!(res->flags & IORESOURCE_BUSY)) {
1547 			p = &res->child;
1548 			continue;
1549 		}
1550 
1551 		/* found the target resource; let's adjust accordingly */
1552 		if (res->start == start && res->end == end) {
1553 			/* free the whole entry */
1554 			*p = res->sibling;
1555 			free_resource(res);
1556 		} else if (res->start == start && res->end != end) {
1557 			/* adjust the start */
1558 			WARN_ON_ONCE(__adjust_resource(res, end + 1,
1559 						       res->end - end));
1560 		} else if (res->start != start && res->end == end) {
1561 			/* adjust the end */
1562 			WARN_ON_ONCE(__adjust_resource(res, res->start,
1563 						       start - res->start));
1564 		} else {
1565 			/* split into two entries - we need a new resource */
1566 			if (!new_res) {
1567 				new_res = alloc_resource(GFP_ATOMIC);
1568 				if (!new_res) {
1569 					alloc_nofail = true;
1570 					write_unlock(&resource_lock);
1571 					goto retry;
1572 				}
1573 			}
1574 			new_res->name = res->name;
1575 			new_res->start = end + 1;
1576 			new_res->end = res->end;
1577 			new_res->flags = res->flags;
1578 			new_res->desc = res->desc;
1579 			new_res->parent = res->parent;
1580 			new_res->sibling = res->sibling;
1581 			new_res->child = NULL;
1582 			reparent_children_after_split(res, new_res, end);
1583 
1584 			if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1585 							   start - res->start)))
1586 				break;
1587 			res->sibling = new_res;
1588 			new_res = NULL;
1589 		}
1590 
1591 		break;
1592 	}
1593 
1594 	write_unlock(&resource_lock);
1595 	free_resource(new_res);
1596 }
1597 #endif	/* CONFIG_MEMORY_HOTREMOVE */
1598 
1599 #ifdef CONFIG_MEMORY_HOTPLUG
1600 static bool system_ram_resources_mergeable(struct resource *r1,
1601 					   struct resource *r2)
1602 {
1603 	/* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
1604 	return r1->flags == r2->flags && r1->end + 1 == r2->start &&
1605 	       r1->name == r2->name && r1->desc == r2->desc &&
1606 	       !r1->child && !r2->child;
1607 }
1608 
1609 /**
1610  * merge_system_ram_resource - mark the System RAM resource mergeable and try to
1611  *	merge it with adjacent, mergeable resources
1612  * @res: resource descriptor
1613  *
1614  * This interface is intended for memory hotplug, whereby lots of contiguous
1615  * system ram resources are added (e.g., via add_memory*()) by a driver, and
1616  * the actual resource boundaries are not of interest (e.g., it might be
1617  * relevant for DIMMs). Only resources that are marked mergeable, that have the
1618  * same parent, and that don't have any children are considered. All mergeable
1619  * resources must be immutable during the request.
1620  *
1621  * Note:
1622  * - The caller has to make sure that no pointers to resources that are
1623  *   marked mergeable are used anymore after this call - the resource might
1624  *   be freed and the pointer might be stale!
1625  * - release_mem_region_adjustable() will split on demand on memory hotunplug
1626  */
1627 void merge_system_ram_resource(struct resource *res)
1628 {
1629 	const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1630 	struct resource *cur;
1631 
1632 	if (WARN_ON_ONCE((res->flags & flags) != flags))
1633 		return;
1634 
1635 	write_lock(&resource_lock);
1636 	res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
1637 
1638 	/* Try to merge with next item in the list. */
1639 	cur = res->sibling;
1640 	if (cur && system_ram_resources_mergeable(res, cur)) {
1641 		res->end = cur->end;
1642 		res->sibling = cur->sibling;
1643 		free_resource(cur);
1644 	}
1645 
1646 	/* Try to merge with previous item in the list. */
1647 	cur = res->parent->child;
1648 	while (cur && cur->sibling != res)
1649 		cur = cur->sibling;
1650 	if (cur && system_ram_resources_mergeable(cur, res)) {
1651 		cur->end = res->end;
1652 		cur->sibling = res->sibling;
1653 		free_resource(res);
1654 	}
1655 	write_unlock(&resource_lock);
1656 }
1657 #endif	/* CONFIG_MEMORY_HOTPLUG */
1658 
1659 /*
1660  * Managed region resource
1661  */
1662 static void devm_resource_release(struct device *dev, void *ptr)
1663 {
1664 	struct resource **r = ptr;
1665 
1666 	release_resource(*r);
1667 }
1668 
1669 /**
1670  * devm_request_resource() - request and reserve an I/O or memory resource
1671  * @dev: device for which to request the resource
1672  * @root: root of the resource tree from which to request the resource
1673  * @new: descriptor of the resource to request
1674  *
1675  * This is a device-managed version of request_resource(). There is usually
1676  * no need to release resources requested by this function explicitly since
1677  * that will be taken care of when the device is unbound from its driver.
1678  * If for some reason the resource needs to be released explicitly, because
1679  * of ordering issues for example, drivers must call devm_release_resource()
1680  * rather than the regular release_resource().
1681  *
1682  * When a conflict is detected between any existing resources and the newly
1683  * requested resource, an error message will be printed.
1684  *
1685  * Returns 0 on success or a negative error code on failure.
1686  */
1687 int devm_request_resource(struct device *dev, struct resource *root,
1688 			  struct resource *new)
1689 {
1690 	struct resource *conflict, **ptr;
1691 
1692 	ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1693 	if (!ptr)
1694 		return -ENOMEM;
1695 
1696 	*ptr = new;
1697 
1698 	conflict = request_resource_conflict(root, new);
1699 	if (conflict) {
1700 		dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1701 			new, conflict->name, conflict);
1702 		devres_free(ptr);
1703 		return -EBUSY;
1704 	}
1705 
1706 	devres_add(dev, ptr);
1707 	return 0;
1708 }
1709 EXPORT_SYMBOL(devm_request_resource);
1710 
1711 static int devm_resource_match(struct device *dev, void *res, void *data)
1712 {
1713 	struct resource **ptr = res;
1714 
1715 	return *ptr == data;
1716 }
1717 
1718 /**
1719  * devm_release_resource() - release a previously requested resource
1720  * @dev: device for which to release the resource
1721  * @new: descriptor of the resource to release
1722  *
1723  * Releases a resource previously requested using devm_request_resource().
1724  */
1725 void devm_release_resource(struct device *dev, struct resource *new)
1726 {
1727 	WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1728 			       new));
1729 }
1730 EXPORT_SYMBOL(devm_release_resource);
1731 
1732 struct region_devres {
1733 	struct resource *parent;
1734 	resource_size_t start;
1735 	resource_size_t n;
1736 };
1737 
1738 static void devm_region_release(struct device *dev, void *res)
1739 {
1740 	struct region_devres *this = res;
1741 
1742 	__release_region(this->parent, this->start, this->n);
1743 }
1744 
1745 static int devm_region_match(struct device *dev, void *res, void *match_data)
1746 {
1747 	struct region_devres *this = res, *match = match_data;
1748 
1749 	return this->parent == match->parent &&
1750 		this->start == match->start && this->n == match->n;
1751 }
1752 
1753 struct resource *
1754 __devm_request_region(struct device *dev, struct resource *parent,
1755 		      resource_size_t start, resource_size_t n, const char *name)
1756 {
1757 	struct region_devres *dr = NULL;
1758 	struct resource *res;
1759 
1760 	dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1761 			  GFP_KERNEL);
1762 	if (!dr)
1763 		return NULL;
1764 
1765 	dr->parent = parent;
1766 	dr->start = start;
1767 	dr->n = n;
1768 
1769 	res = __request_region(parent, start, n, name, 0);
1770 	if (res)
1771 		devres_add(dev, dr);
1772 	else
1773 		devres_free(dr);
1774 
1775 	return res;
1776 }
1777 EXPORT_SYMBOL(__devm_request_region);
1778 
1779 void __devm_release_region(struct device *dev, struct resource *parent,
1780 			   resource_size_t start, resource_size_t n)
1781 {
1782 	struct region_devres match_data = { parent, start, n };
1783 
1784 	WARN_ON(devres_release(dev, devm_region_release, devm_region_match,
1785 			       &match_data));
1786 }
1787 EXPORT_SYMBOL(__devm_release_region);
1788 
1789 /*
1790  * Reserve I/O ports or memory based on "reserve=" kernel parameter.
1791  */
1792 #define MAXRESERVE 4
1793 static int __init reserve_setup(char *str)
1794 {
1795 	static int reserved;
1796 	static struct resource reserve[MAXRESERVE];
1797 
1798 	for (;;) {
1799 		unsigned int io_start, io_num;
1800 		int x = reserved;
1801 		struct resource *parent;
1802 
1803 		if (get_option(&str, &io_start) != 2)
1804 			break;
1805 		if (get_option(&str, &io_num) == 0)
1806 			break;
1807 		if (x < MAXRESERVE) {
1808 			struct resource *res = reserve + x;
1809 
1810 			/*
1811 			 * If the region starts below 0x10000, we assume it's
1812 			 * I/O port space; otherwise assume it's memory.
1813 			 */
1814 			if (io_start < 0x10000) {
1815 				*res = DEFINE_RES_IO_NAMED(io_start, io_num, "reserved");
1816 				parent = &ioport_resource;
1817 			} else {
1818 				*res = DEFINE_RES_MEM_NAMED(io_start, io_num, "reserved");
1819 				parent = &iomem_resource;
1820 			}
1821 			res->flags |= IORESOURCE_BUSY;
1822 			if (request_resource(parent, res) == 0)
1823 				reserved = x+1;
1824 		}
1825 	}
1826 	return 1;
1827 }
1828 __setup("reserve=", reserve_setup);
1829 
1830 /*
1831  * Check if the requested addr and size spans more than any slot in the
1832  * iomem resource tree.
1833  */
1834 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1835 {
1836 	resource_size_t end = addr + size - 1;
1837 	struct resource *p;
1838 	int err = 0;
1839 
1840 	read_lock(&resource_lock);
1841 	for_each_resource(&iomem_resource, p, false) {
1842 		/*
1843 		 * We can probably skip the resources without
1844 		 * IORESOURCE_IO attribute?
1845 		 */
1846 		if (p->start > end)
1847 			continue;
1848 		if (p->end < addr)
1849 			continue;
1850 		if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1851 		    PFN_DOWN(p->end) >= PFN_DOWN(end))
1852 			continue;
1853 		/*
1854 		 * if a resource is "BUSY", it's not a hardware resource
1855 		 * but a driver mapping of such a resource; we don't want
1856 		 * to warn for those; some drivers legitimately map only
1857 		 * partial hardware resources. (example: vesafb)
1858 		 */
1859 		if (p->flags & IORESOURCE_BUSY)
1860 			continue;
1861 
1862 		pr_warn("resource sanity check: requesting [mem %pa-%pa], which spans more than %s %pR\n",
1863 			&addr, &end, p->name, p);
1864 		err = -1;
1865 		break;
1866 	}
1867 	read_unlock(&resource_lock);
1868 
1869 	return err;
1870 }
1871 
1872 #ifdef CONFIG_STRICT_DEVMEM
1873 static int strict_iomem_checks = 1;
1874 #else
1875 static int strict_iomem_checks;
1876 #endif
1877 
1878 /*
1879  * Check if an address is exclusive to the kernel and must not be mapped to
1880  * user space, for example, via /dev/mem.
1881  *
1882  * Returns true if exclusive to the kernel, otherwise returns false.
1883  */
1884 bool resource_is_exclusive(struct resource *root, u64 addr, resource_size_t size)
1885 {
1886 	const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
1887 						  IORESOURCE_EXCLUSIVE;
1888 	bool skip_children = false, err = false;
1889 	struct resource *p;
1890 
1891 	read_lock(&resource_lock);
1892 	for_each_resource(root, p, skip_children) {
1893 		if (p->start >= addr + size)
1894 			break;
1895 		if (p->end < addr) {
1896 			skip_children = true;
1897 			continue;
1898 		}
1899 		skip_children = false;
1900 
1901 		/*
1902 		 * IORESOURCE_SYSTEM_RAM resources are exclusive if
1903 		 * IORESOURCE_EXCLUSIVE is set, even if they
1904 		 * are not busy and even if "iomem=relaxed" is set. The
1905 		 * responsible driver dynamically adds/removes system RAM within
1906 		 * such an area and uncontrolled access is dangerous.
1907 		 */
1908 		if ((p->flags & exclusive_system_ram) == exclusive_system_ram) {
1909 			err = true;
1910 			break;
1911 		}
1912 
1913 		/*
1914 		 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1915 		 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1916 		 * resource is busy.
1917 		 */
1918 		if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
1919 			continue;
1920 		if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1921 				|| p->flags & IORESOURCE_EXCLUSIVE) {
1922 			err = true;
1923 			break;
1924 		}
1925 	}
1926 	read_unlock(&resource_lock);
1927 
1928 	return err;
1929 }
1930 
1931 bool iomem_is_exclusive(u64 addr)
1932 {
1933 	return resource_is_exclusive(&iomem_resource, addr & PAGE_MASK,
1934 				     PAGE_SIZE);
1935 }
1936 
1937 struct resource_entry *resource_list_create_entry(struct resource *res,
1938 						  size_t extra_size)
1939 {
1940 	struct resource_entry *entry;
1941 
1942 	entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1943 	if (entry) {
1944 		INIT_LIST_HEAD(&entry->node);
1945 		entry->res = res ? res : &entry->__res;
1946 	}
1947 
1948 	return entry;
1949 }
1950 EXPORT_SYMBOL(resource_list_create_entry);
1951 
1952 void resource_list_free(struct list_head *head)
1953 {
1954 	struct resource_entry *entry, *tmp;
1955 
1956 	list_for_each_entry_safe(entry, tmp, head, node)
1957 		resource_list_destroy_entry(entry);
1958 }
1959 EXPORT_SYMBOL(resource_list_free);
1960 
1961 #ifdef CONFIG_GET_FREE_REGION
1962 #define GFR_DESCENDING		(1UL << 0)
1963 #define GFR_REQUEST_REGION	(1UL << 1)
1964 #ifdef PA_SECTION_SHIFT
1965 #define GFR_DEFAULT_ALIGN	(1UL << PA_SECTION_SHIFT)
1966 #else
1967 #define GFR_DEFAULT_ALIGN	PAGE_SIZE
1968 #endif
1969 
1970 static resource_size_t gfr_start(struct resource *base, resource_size_t size,
1971 				 resource_size_t align, unsigned long flags)
1972 {
1973 	if (flags & GFR_DESCENDING) {
1974 		resource_size_t end;
1975 
1976 		end = min_t(resource_size_t, base->end, DIRECT_MAP_PHYSMEM_END);
1977 		return end - size + 1;
1978 	}
1979 
1980 	return ALIGN(max(base->start, align), align);
1981 }
1982 
1983 static bool gfr_continue(struct resource *base, resource_size_t addr,
1984 			 resource_size_t size, unsigned long flags)
1985 {
1986 	if (flags & GFR_DESCENDING)
1987 		return addr > size && addr >= base->start;
1988 	/*
1989 	 * In the ascend case be careful that the last increment by
1990 	 * @size did not wrap 0.
1991 	 */
1992 	return addr > addr - size &&
1993 	       addr <= min_t(resource_size_t, base->end, DIRECT_MAP_PHYSMEM_END);
1994 }
1995 
1996 static resource_size_t gfr_next(resource_size_t addr, resource_size_t size,
1997 				unsigned long flags)
1998 {
1999 	if (flags & GFR_DESCENDING)
2000 		return addr - size;
2001 	return addr + size;
2002 }
2003 
2004 static void remove_free_mem_region(void *_res)
2005 {
2006 	struct resource *res = _res;
2007 
2008 	if (res->parent)
2009 		remove_resource(res);
2010 	free_resource(res);
2011 }
2012 
2013 static struct resource *
2014 get_free_mem_region(struct device *dev, struct resource *base,
2015 		    resource_size_t size, const unsigned long align,
2016 		    const char *name, const unsigned long desc,
2017 		    const unsigned long flags)
2018 {
2019 	resource_size_t addr;
2020 	struct resource *res;
2021 	struct region_devres *dr = NULL;
2022 
2023 	size = ALIGN(size, align);
2024 
2025 	res = alloc_resource(GFP_KERNEL);
2026 	if (!res)
2027 		return ERR_PTR(-ENOMEM);
2028 
2029 	if (dev && (flags & GFR_REQUEST_REGION)) {
2030 		dr = devres_alloc(devm_region_release,
2031 				sizeof(struct region_devres), GFP_KERNEL);
2032 		if (!dr) {
2033 			free_resource(res);
2034 			return ERR_PTR(-ENOMEM);
2035 		}
2036 	} else if (dev) {
2037 		if (devm_add_action_or_reset(dev, remove_free_mem_region, res))
2038 			return ERR_PTR(-ENOMEM);
2039 	}
2040 
2041 	write_lock(&resource_lock);
2042 	for (addr = gfr_start(base, size, align, flags);
2043 	     gfr_continue(base, addr, align, flags);
2044 	     addr = gfr_next(addr, align, flags)) {
2045 		if (__region_intersects(base, addr, size, 0, IORES_DESC_NONE) !=
2046 		    REGION_DISJOINT)
2047 			continue;
2048 
2049 		if (flags & GFR_REQUEST_REGION) {
2050 			if (__request_region_locked(res, &iomem_resource, addr,
2051 						    size, name, 0))
2052 				break;
2053 
2054 			if (dev) {
2055 				dr->parent = &iomem_resource;
2056 				dr->start = addr;
2057 				dr->n = size;
2058 				devres_add(dev, dr);
2059 			}
2060 
2061 			res->desc = desc;
2062 			write_unlock(&resource_lock);
2063 
2064 
2065 			/*
2066 			 * A driver is claiming this region so revoke any
2067 			 * mappings.
2068 			 */
2069 			revoke_iomem(res);
2070 		} else {
2071 			*res = DEFINE_RES_NAMED_DESC(addr, size, name, IORESOURCE_MEM, desc);
2072 
2073 			/*
2074 			 * Only succeed if the resource hosts an exclusive
2075 			 * range after the insert
2076 			 */
2077 			if (__insert_resource(base, res) || res->child)
2078 				break;
2079 
2080 			write_unlock(&resource_lock);
2081 		}
2082 
2083 		return res;
2084 	}
2085 	write_unlock(&resource_lock);
2086 
2087 	if (flags & GFR_REQUEST_REGION) {
2088 		free_resource(res);
2089 		devres_free(dr);
2090 	} else if (dev)
2091 		devm_release_action(dev, remove_free_mem_region, res);
2092 
2093 	return ERR_PTR(-ERANGE);
2094 }
2095 
2096 /**
2097  * devm_request_free_mem_region - find free region for device private memory
2098  *
2099  * @dev: device struct to bind the resource to
2100  * @size: size in bytes of the device memory to add
2101  * @base: resource tree to look in
2102  *
2103  * This function tries to find an empty range of physical address big enough to
2104  * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
2105  * memory, which in turn allocates struct pages.
2106  */
2107 struct resource *devm_request_free_mem_region(struct device *dev,
2108 		struct resource *base, unsigned long size)
2109 {
2110 	unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
2111 
2112 	return get_free_mem_region(dev, base, size, GFR_DEFAULT_ALIGN,
2113 				   dev_name(dev),
2114 				   IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
2115 }
2116 EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
2117 
2118 struct resource *request_free_mem_region(struct resource *base,
2119 		unsigned long size, const char *name)
2120 {
2121 	unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
2122 
2123 	return get_free_mem_region(NULL, base, size, GFR_DEFAULT_ALIGN, name,
2124 				   IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
2125 }
2126 EXPORT_SYMBOL_GPL(request_free_mem_region);
2127 
2128 /**
2129  * alloc_free_mem_region - find a free region relative to @base
2130  * @base: resource that will parent the new resource
2131  * @size: size in bytes of memory to allocate from @base
2132  * @align: alignment requirements for the allocation
2133  * @name: resource name
2134  *
2135  * Buses like CXL, that can dynamically instantiate new memory regions,
2136  * need a method to allocate physical address space for those regions.
2137  * Allocate and insert a new resource to cover a free, unclaimed by a
2138  * descendant of @base, range in the span of @base.
2139  */
2140 struct resource *alloc_free_mem_region(struct resource *base,
2141 				       unsigned long size, unsigned long align,
2142 				       const char *name)
2143 {
2144 	/* Default of ascending direction and insert resource */
2145 	unsigned long flags = 0;
2146 
2147 	return get_free_mem_region(NULL, base, size, align, name,
2148 				   IORES_DESC_NONE, flags);
2149 }
2150 EXPORT_SYMBOL_GPL(alloc_free_mem_region);
2151 #endif /* CONFIG_GET_FREE_REGION */
2152 
2153 static int __init strict_iomem(char *str)
2154 {
2155 	if (strstr(str, "relaxed"))
2156 		strict_iomem_checks = 0;
2157 	if (strstr(str, "strict"))
2158 		strict_iomem_checks = 1;
2159 	return 1;
2160 }
2161 
2162 static int iomem_fs_init_fs_context(struct fs_context *fc)
2163 {
2164 	return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
2165 }
2166 
2167 static struct file_system_type iomem_fs_type = {
2168 	.name		= "iomem",
2169 	.owner		= THIS_MODULE,
2170 	.init_fs_context = iomem_fs_init_fs_context,
2171 	.kill_sb	= kill_anon_super,
2172 };
2173 
2174 static int __init iomem_init_inode(void)
2175 {
2176 	static struct vfsmount *iomem_vfs_mount;
2177 	static int iomem_fs_cnt;
2178 	struct inode *inode;
2179 	int rc;
2180 
2181 	rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
2182 	if (rc < 0) {
2183 		pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
2184 		return rc;
2185 	}
2186 
2187 	inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
2188 	if (IS_ERR(inode)) {
2189 		rc = PTR_ERR(inode);
2190 		pr_err("Cannot allocate inode for iomem: %d\n", rc);
2191 		simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
2192 		return rc;
2193 	}
2194 
2195 	/*
2196 	 * Publish iomem revocation inode initialized.
2197 	 * Pairs with smp_load_acquire() in revoke_iomem().
2198 	 */
2199 	smp_store_release(&iomem_inode, inode);
2200 
2201 	return 0;
2202 }
2203 
2204 fs_initcall(iomem_init_inode);
2205 
2206 __setup("iomem=", strict_iomem);
2207