resources: ensure callback doesn't allocate outside available space
authorBjorn Helgaas <bjorn.helgaas@hp.com>
Tue, 26 Oct 2010 21:41:23 +0000 (15:41 -0600)
committerJesse Barnes <jbarnes@virtuousgeek.org>
Tue, 26 Oct 2010 22:33:26 +0000 (15:33 -0700)
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>
kernel/resource.c

index 26e9f25..89d5041 100644 (file)
@@ -374,6 +374,11 @@ static void resource_clip(struct resource *res, resource_size_t min,
                res->end = max;
 }
 
+static bool resource_contains(struct resource *res1, struct resource *res2)
+{
+       return res1->start <= res2->start && res1->end >= res2->end;
+}
+
 /*
  * Find empty slot in the resource tree given range and alignment.
  */
@@ -387,7 +392,7 @@ static int find_resource(struct resource *root, struct resource *new,
                         void *alignf_data)
 {
        struct resource *this = root->child;
-       struct resource tmp = *new;
+       struct resource tmp = *new, alloc;
 
        tmp.start = root->start;
        /*
@@ -407,10 +412,11 @@ static int find_resource(struct resource *root, struct resource *new,
                resource_clip(&tmp, min, max);
                tmp.start = ALIGN(tmp.start, align);
 
-               tmp.start = alignf(alignf_data, &tmp, size, align);
-               if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
-                       new->start = tmp.start;
-                       new->end = tmp.start + size - 1;
+               alloc.start = alignf(alignf_data, &tmp, size, align);
+               alloc.end = alloc.start + size - 1;
+               if (resource_contains(&tmp, &alloc)) {
+                       new->start = alloc.start;
+                       new->end = alloc.end;
                        return 0;
                }
                if (!this)