Commit 485bcc29 authored by Bjorn Helgaas's avatar Bjorn Helgaas Committed by Greg Kroah-Hartman

resource: Fix find_next_iomem_res() iteration issue

[ Upstream commit 010a93bf ]

Previously find_next_iomem_res() used "*res" as both an input parameter for
the range to search and the type of resource to search for, and an output
parameter for the resource we found, which makes the interface confusing.

The current callers use find_next_iomem_res() incorrectly because they
allocate a single struct resource and use it for repeated calls to
find_next_iomem_res().  When find_next_iomem_res() returns a resource, it
overwrites the start, end, flags, and desc members of the struct.  If we
call find_next_iomem_res() again, we must update or restore these fields.
The previous code restored res.start and res.end, but not res.flags or
res.desc.

Since the callers did not restore res.flags, if they searched for flags
IORESOURCE_MEM | IORESOURCE_BUSY and found a resource with flags
IORESOURCE_MEM | IORESOURCE_BUSY | IORESOURCE_SYSRAM, the next search would
incorrectly skip resources unless they were also marked as
IORESOURCE_SYSRAM.

Fix this by restructuring the interface so it takes explicit "start, end,
flags" parameters and uses "*res" only as an output parameter.

Based on a patch by Lianbo Jiang <lijiang@redhat.com>.

 [ bp: While at it:
   - make comments kernel-doc style.
   -

Originally-by: http://lore.kernel.org/lkml/20180921073211.20097-2-lijiang@redhat.comSigned-off-by: default avatarBjorn Helgaas <bhelgaas@google.com>
Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: Brijesh Singh <brijesh.singh@amd.com>
CC: Dan Williams <dan.j.williams@intel.com>
CC: H. Peter Anvin <hpa@zytor.com>
CC: Lianbo Jiang <lijiang@redhat.com>
CC: Takashi Iwai <tiwai@suse.de>
CC: Thomas Gleixner <tglx@linutronix.de>
CC: Tom Lendacky <thomas.lendacky@amd.com>
CC: Vivek Goyal <vgoyal@redhat.com>
CC: Yaowei Bai <baiyaowei@cmss.chinamobile.com>
CC: bhe@redhat.com
CC: dan.j.williams@intel.com
CC: dyoung@redhat.com
CC: kexec@lists.infradead.org
CC: mingo@redhat.com
CC: x86-ml <x86@kernel.org>
Link: http://lkml.kernel.org/r/153805812916.1157.177580438135143788.stgit@bhelgaas-glaptop.roam.corp.google.comSigned-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 9a80dfcc
...@@ -318,24 +318,27 @@ int release_resource(struct resource *old) ...@@ -318,24 +318,27 @@ int release_resource(struct resource *old)
EXPORT_SYMBOL(release_resource); EXPORT_SYMBOL(release_resource);
/* /**
* Finds the lowest iomem resource existing within [res->start..res->end]. * Finds the lowest iomem resource that covers part of [start..end]. The
* The caller must specify res->start, res->end, res->flags, and optionally * caller must specify start, end, flags, and desc (which may be
* desc. If found, returns 0, res is overwritten, if not found, returns -1. * IORES_DESC_NONE).
* This function walks the whole tree and not just first level children until *
* and unless first_level_children_only is true. * If a resource is found, returns 0 and *res is overwritten with the part
* of the resource that's within [start..end]; if none is found, returns
* -1.
*
* This function walks the whole tree and not just first level children
* unless @first_level_children_only is true.
*/ */
static int find_next_iomem_res(struct resource *res, unsigned long desc, static int find_next_iomem_res(resource_size_t start, resource_size_t end,
bool first_level_children_only) unsigned long flags, unsigned long desc,
bool first_level_children_only,
struct resource *res)
{ {
resource_size_t start, end;
struct resource *p; struct resource *p;
bool sibling_only = false; bool sibling_only = false;
BUG_ON(!res); BUG_ON(!res);
start = res->start;
end = res->end;
BUG_ON(start >= end); BUG_ON(start >= end);
if (first_level_children_only) if (first_level_children_only)
...@@ -344,7 +347,7 @@ static int find_next_iomem_res(struct resource *res, unsigned long desc, ...@@ -344,7 +347,7 @@ static int find_next_iomem_res(struct resource *res, unsigned long desc,
read_lock(&resource_lock); read_lock(&resource_lock);
for (p = iomem_resource.child; p; p = next_resource(p, sibling_only)) { for (p = iomem_resource.child; p; p = next_resource(p, sibling_only)) {
if ((p->flags & res->flags) != res->flags) if ((p->flags & flags) != flags)
continue; continue;
if ((desc != IORES_DESC_NONE) && (desc != p->desc)) if ((desc != IORES_DESC_NONE) && (desc != p->desc))
continue; continue;
...@@ -359,32 +362,31 @@ static int find_next_iomem_res(struct resource *res, unsigned long desc, ...@@ -359,32 +362,31 @@ static int find_next_iomem_res(struct resource *res, unsigned long desc,
read_unlock(&resource_lock); read_unlock(&resource_lock);
if (!p) if (!p)
return -1; return -1;
/* copy data */ /* copy data */
if (res->start < p->start) res->start = max(start, p->start);
res->start = p->start; res->end = min(end, p->end);
if (res->end > p->end)
res->end = p->end;
res->flags = p->flags; res->flags = p->flags;
res->desc = p->desc; res->desc = p->desc;
return 0; return 0;
} }
static int __walk_iomem_res_desc(struct resource *res, unsigned long desc, static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
bool first_level_children_only, unsigned long flags, unsigned long desc,
void *arg, bool first_level_children_only, void *arg,
int (*func)(struct resource *, void *)) int (*func)(struct resource *, void *))
{ {
u64 orig_end = res->end; struct resource res;
int ret = -1; int ret = -1;
while ((res->start < res->end) && while (start < end &&
!find_next_iomem_res(res, desc, first_level_children_only)) { !find_next_iomem_res(start, end, flags, desc,
ret = (*func)(res, arg); first_level_children_only, &res)) {
ret = (*func)(&res, arg);
if (ret) if (ret)
break; break;
res->start = res->end + 1; start = res.end + 1;
res->end = orig_end;
} }
return ret; return ret;
...@@ -407,13 +409,7 @@ static int __walk_iomem_res_desc(struct resource *res, unsigned long desc, ...@@ -407,13 +409,7 @@ static int __walk_iomem_res_desc(struct resource *res, unsigned long desc,
int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
u64 end, void *arg, int (*func)(struct resource *, void *)) u64 end, void *arg, int (*func)(struct resource *, void *))
{ {
struct resource res; return __walk_iomem_res_desc(start, end, flags, desc, false, arg, func);
res.start = start;
res.end = end;
res.flags = flags;
return __walk_iomem_res_desc(&res, desc, false, arg, func);
} }
EXPORT_SYMBOL_GPL(walk_iomem_res_desc); EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
...@@ -427,13 +423,9 @@ EXPORT_SYMBOL_GPL(walk_iomem_res_desc); ...@@ -427,13 +423,9 @@ EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
int walk_system_ram_res(u64 start, u64 end, void *arg, int walk_system_ram_res(u64 start, u64 end, void *arg,
int (*func)(struct resource *, void *)) int (*func)(struct resource *, void *))
{ {
struct resource res; unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
res.start = start; return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, true,
res.end = end;
res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
return __walk_iomem_res_desc(&res, IORES_DESC_NONE, true,
arg, func); arg, func);
} }
...@@ -444,13 +436,9 @@ int walk_system_ram_res(u64 start, u64 end, void *arg, ...@@ -444,13 +436,9 @@ int walk_system_ram_res(u64 start, u64 end, void *arg,
int walk_mem_res(u64 start, u64 end, void *arg, int walk_mem_res(u64 start, u64 end, void *arg,
int (*func)(struct resource *, void *)) int (*func)(struct resource *, void *))
{ {
struct resource res; unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
res.start = start; return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, true,
res.end = end;
res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
return __walk_iomem_res_desc(&res, IORES_DESC_NONE, true,
arg, func); arg, func);
} }
...@@ -464,25 +452,25 @@ int walk_mem_res(u64 start, u64 end, void *arg, ...@@ -464,25 +452,25 @@ int walk_mem_res(u64 start, u64 end, void *arg,
int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages, int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
void *arg, int (*func)(unsigned long, unsigned long, void *)) void *arg, int (*func)(unsigned long, unsigned long, void *))
{ {
resource_size_t start, end;
unsigned long flags;
struct resource res; struct resource res;
unsigned long pfn, end_pfn; unsigned long pfn, end_pfn;
u64 orig_end;
int ret = -1; int ret = -1;
res.start = (u64) start_pfn << PAGE_SHIFT; start = (u64) start_pfn << PAGE_SHIFT;
res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1; end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
orig_end = res.end; while (start < end &&
while ((res.start < res.end) && !find_next_iomem_res(start, end, flags, IORES_DESC_NONE,
(find_next_iomem_res(&res, IORES_DESC_NONE, true) >= 0)) { true, &res)) {
pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT; pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
end_pfn = (res.end + 1) >> PAGE_SHIFT; end_pfn = (res.end + 1) >> PAGE_SHIFT;
if (end_pfn > pfn) if (end_pfn > pfn)
ret = (*func)(pfn, end_pfn - pfn, arg); ret = (*func)(pfn, end_pfn - pfn, arg);
if (ret) if (ret)
break; break;
res.start = res.end + 1; start = res.end + 1;
res.end = orig_end;
} }
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment