Skip to content

Commit 010a93b

Browse files
bjorn-helgaassuryasaimadhu
authored andcommitted
resource: Fix find_next_iomem_res() iteration issue
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.com Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Signed-off-by: Borislav 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.com
1 parent a98959f commit 010a93b

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

kernel/resource.c

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,27 @@ int release_resource(struct resource *old)
318318

319319
EXPORT_SYMBOL(release_resource);
320320

321-
/*
322-
* Finds the lowest iomem resource existing within [res->start..res->end].
323-
* The caller must specify res->start, res->end, res->flags, and optionally
324-
* desc. If found, returns 0, res is overwritten, if not found, returns -1.
325-
* This function walks the whole tree and not just first level children until
326-
* and unless first_level_children_only is true.
321+
/**
322+
* Finds the lowest iomem resource that covers part of [start..end]. The
323+
* caller must specify start, end, flags, and desc (which may be
324+
* IORES_DESC_NONE).
325+
*
326+
* If a resource is found, returns 0 and *res is overwritten with the part
327+
* of the resource that's within [start..end]; if none is found, returns
328+
* -1.
329+
*
330+
* This function walks the whole tree and not just first level children
331+
* unless @first_level_children_only is true.
327332
*/
328-
static int find_next_iomem_res(struct resource *res, unsigned long desc,
329-
bool first_level_children_only)
333+
static int find_next_iomem_res(resource_size_t start, resource_size_t end,
334+
unsigned long flags, unsigned long desc,
335+
bool first_level_children_only,
336+
struct resource *res)
330337
{
331-
resource_size_t start, end;
332338
struct resource *p;
333339
bool sibling_only = false;
334340

335341
BUG_ON(!res);
336-
337-
start = res->start;
338-
end = res->end;
339342
BUG_ON(start >= end);
340343

341344
if (first_level_children_only)
@@ -344,7 +347,7 @@ static int find_next_iomem_res(struct resource *res, unsigned long desc,
344347
read_lock(&resource_lock);
345348

346349
for (p = iomem_resource.child; p; p = next_resource(p, sibling_only)) {
347-
if ((p->flags & res->flags) != res->flags)
350+
if ((p->flags & flags) != flags)
348351
continue;
349352
if ((desc != IORES_DESC_NONE) && (desc != p->desc))
350353
continue;
@@ -359,32 +362,31 @@ static int find_next_iomem_res(struct resource *res, unsigned long desc,
359362
read_unlock(&resource_lock);
360363
if (!p)
361364
return -1;
365+
362366
/* copy data */
363-
if (res->start < p->start)
364-
res->start = p->start;
365-
if (res->end > p->end)
366-
res->end = p->end;
367+
res->start = max(start, p->start);
368+
res->end = min(end, p->end);
367369
res->flags = p->flags;
368370
res->desc = p->desc;
369371
return 0;
370372
}
371373

372-
static int __walk_iomem_res_desc(struct resource *res, unsigned long desc,
373-
bool first_level_children_only,
374-
void *arg,
374+
static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
375+
unsigned long flags, unsigned long desc,
376+
bool first_level_children_only, void *arg,
375377
int (*func)(struct resource *, void *))
376378
{
377-
u64 orig_end = res->end;
379+
struct resource res;
378380
int ret = -1;
379381

380-
while ((res->start < res->end) &&
381-
!find_next_iomem_res(res, desc, first_level_children_only)) {
382-
ret = (*func)(res, arg);
382+
while (start < end &&
383+
!find_next_iomem_res(start, end, flags, desc,
384+
first_level_children_only, &res)) {
385+
ret = (*func)(&res, arg);
383386
if (ret)
384387
break;
385388

386-
res->start = res->end + 1;
387-
res->end = orig_end;
389+
start = res.end + 1;
388390
}
389391

390392
return ret;
@@ -407,13 +409,7 @@ static int __walk_iomem_res_desc(struct resource *res, unsigned long desc,
407409
int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
408410
u64 end, void *arg, int (*func)(struct resource *, void *))
409411
{
410-
struct resource res;
411-
412-
res.start = start;
413-
res.end = end;
414-
res.flags = flags;
415-
416-
return __walk_iomem_res_desc(&res, desc, false, arg, func);
412+
return __walk_iomem_res_desc(start, end, flags, desc, false, arg, func);
417413
}
418414
EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
419415

@@ -427,13 +423,9 @@ EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
427423
int walk_system_ram_res(u64 start, u64 end, void *arg,
428424
int (*func)(struct resource *, void *))
429425
{
430-
struct resource res;
426+
unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
431427

432-
res.start = start;
433-
res.end = end;
434-
res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
435-
436-
return __walk_iomem_res_desc(&res, IORES_DESC_NONE, true,
428+
return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, true,
437429
arg, func);
438430
}
439431

@@ -444,13 +436,9 @@ int walk_system_ram_res(u64 start, u64 end, void *arg,
444436
int walk_mem_res(u64 start, u64 end, void *arg,
445437
int (*func)(struct resource *, void *))
446438
{
447-
struct resource res;
439+
unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
448440

449-
res.start = start;
450-
res.end = end;
451-
res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
452-
453-
return __walk_iomem_res_desc(&res, IORES_DESC_NONE, true,
441+
return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, true,
454442
arg, func);
455443
}
456444

@@ -464,25 +452,25 @@ int walk_mem_res(u64 start, u64 end, void *arg,
464452
int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
465453
void *arg, int (*func)(unsigned long, unsigned long, void *))
466454
{
455+
resource_size_t start, end;
456+
unsigned long flags;
467457
struct resource res;
468458
unsigned long pfn, end_pfn;
469-
u64 orig_end;
470459
int ret = -1;
471460

472-
res.start = (u64) start_pfn << PAGE_SHIFT;
473-
res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
474-
res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
475-
orig_end = res.end;
476-
while ((res.start < res.end) &&
477-
(find_next_iomem_res(&res, IORES_DESC_NONE, true) >= 0)) {
461+
start = (u64) start_pfn << PAGE_SHIFT;
462+
end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
463+
flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
464+
while (start < end &&
465+
!find_next_iomem_res(start, end, flags, IORES_DESC_NONE,
466+
true, &res)) {
478467
pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
479468
end_pfn = (res.end + 1) >> PAGE_SHIFT;
480469
if (end_pfn > pfn)
481470
ret = (*func)(pfn, end_pfn - pfn, arg);
482471
if (ret)
483472
break;
484-
res.start = res.end + 1;
485-
res.end = orig_end;
473+
start = res.end + 1;
486474
}
487475
return ret;
488476
}

0 commit comments

Comments
 (0)