Skip to content

Commit 6909ba1

Browse files
Bjorn Helgaasjbarnes993
authored andcommitted
resources: ensure callback doesn't allocate outside available space
The alignment callback returns a proposed location, which may have been adjusted to avoid ISA aliases or for other architecture-specific reasons. We already had a check ("tmp.start < tmp.end") to make sure the callback doesn't return an area that extends past the available area. This patch reworks the check to make sure it doesn't return an area that extends either below or above the available area. Signed-off-by: Bjorn Helgaas <bjorn.helgaas@hp.com> Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
1 parent 5d6b1fa commit 6909ba1

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

‎kernel/resource.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ static void resource_clip(struct resource *res, resource_size_t min,
374374
res->end = max;
375375
}
376376

377+
static bool resource_contains(struct resource *res1, struct resource *res2)
378+
{
379+
return res1->start <= res2->start && res1->end >= res2->end;
380+
}
381+
377382
/*
378383
* Find empty slot in the resource tree given range and alignment.
379384
*/
@@ -387,7 +392,7 @@ static int find_resource(struct resource *root, struct resource *new,
387392
void *alignf_data)
388393
{
389394
struct resource *this = root->child;
390-
struct resource tmp = *new;
395+
struct resource tmp = *new, alloc;
391396

392397
tmp.start = root->start;
393398
/*
@@ -407,10 +412,11 @@ static int find_resource(struct resource *root, struct resource *new,
407412
resource_clip(&tmp, min, max);
408413
tmp.start = ALIGN(tmp.start, align);
409414

410-
tmp.start = alignf(alignf_data, &tmp, size, align);
411-
if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
412-
new->start = tmp.start;
413-
new->end = tmp.start + size - 1;
415+
alloc.start = alignf(alignf_data, &tmp, size, align);
416+
alloc.end = alloc.start + size - 1;
417+
if (resource_contains(&tmp, &alloc)) {
418+
new->start = alloc.start;
419+
new->end = alloc.end;
414420
return 0;
415421
}
416422
if (!this)

0 commit comments

Comments
 (0)