From patchwork Tue Oct 26 21:41:23 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bjorn Helgaas X-Patchwork-Id: 284052 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id o9QLguR4010690 for ; Tue, 26 Oct 2010 21:42:56 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760526Ab0JZVl0 (ORCPT ); Tue, 26 Oct 2010 17:41:26 -0400 Received: from g5t0006.atlanta.hp.com ([15.192.0.43]:16347 "EHLO g5t0006.atlanta.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758504Ab0JZVlZ (ORCPT ); Tue, 26 Oct 2010 17:41:25 -0400 Received: from g5t0029.atlanta.hp.com (g5t0029.atlanta.hp.com [16.228.8.141]) by g5t0006.atlanta.hp.com (Postfix) with ESMTP id 30ADCC265; Tue, 26 Oct 2010 21:41:24 +0000 (UTC) Received: from ldl (ldl.usa.hp.com [16.125.112.222]) by g5t0029.atlanta.hp.com (Postfix) with ESMTP id EF8532004A; Tue, 26 Oct 2010 21:41:23 +0000 (UTC) Received: from localhost (ldl.fc.hp.com [127.0.0.1]) by ldl (Postfix) with ESMTP id C85DACF0013; Tue, 26 Oct 2010 15:41:23 -0600 (MDT) Received: from ldl ([127.0.0.1]) by localhost (ldl.fc.hp.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id IihCU1bccNa4; Tue, 26 Oct 2010 15:41:23 -0600 (MDT) Received: from eh.fc.hp.com (bob.lnx.usa.hp.com [16.125.112.218]) by ldl (Postfix) with ESMTP id ABDAFCF0012; Tue, 26 Oct 2010 15:41:23 -0600 (MDT) Received: from bob.kio (localhost [127.0.0.1]) by eh.fc.hp.com (Postfix) with ESMTP id 94AE9260EB; Tue, 26 Oct 2010 15:41:23 -0600 (MDT) Subject: [PATCH v5 3/9] resources: ensure callback doesn't allocate outside available space To: Jesse Barnes From: Bjorn Helgaas Cc: Bob Picco , Brian Bloniarz , Charles Butterfield , Denys Vlasenko , Ingo Molnar , linux-pci@vger.kernel.org, "Horst H. von Brand" , "H. Peter Anvin" , linux-kernel@vger.kernel.org, Stefan Becker , Chuck Ebbert , Fabrice Bellet , Yinghai Lu , Leann Ogasawara , Linus Torvalds , Thomas Gleixner Date: Tue, 26 Oct 2010 15:41:23 -0600 Message-ID: <20101026214123.29808.24645.stgit@bob.kio> In-Reply-To: <20101026214033.29808.15272.stgit@bob.kio> References: <20101026214033.29808.15272.stgit@bob.kio> User-Agent: StGit/0.15 MIME-Version: 1.0 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Tue, 26 Oct 2010 21:42:56 +0000 (UTC) diff --git a/kernel/resource.c b/kernel/resource.c index 26e9f25..89d5041 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -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)