From patchwork Tue Oct 27 20:55:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yinghai Lu X-Patchwork-Id: 7502101 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 185AD9F36A for ; Tue, 27 Oct 2015 20:59:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1352E2096B for ; Tue, 27 Oct 2015 20:59:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 00B1B20961 for ; Tue, 27 Oct 2015 20:59:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965452AbbJ0U7N (ORCPT ); Tue, 27 Oct 2015 16:59:13 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:37374 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965695AbbJ0U5Q (ORCPT ); Tue, 27 Oct 2015 16:57:16 -0400 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t9RKv41M003492 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 27 Oct 2015 20:57:04 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0022.oracle.com (8.13.8/8.13.8) with ESMTP id t9RKv3Sr010581 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Tue, 27 Oct 2015 20:57:04 GMT Received: from abhmp0017.oracle.com (abhmp0017.oracle.com [141.146.116.23]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id t9RKv2JT027335; Tue, 27 Oct 2015 20:57:02 GMT Received: from aserv0021.oracle.com (/10.132.126.176) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 27 Oct 2015 13:57:01 -0700 From: Yinghai Lu To: Bjorn Helgaas , David Miller , Benjamin Herrenschmidt , Wei Yang , TJ , Yijing Wang , Khalid Aziz Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Yinghai Lu Subject: [PATCH v8 54/61] resources: Split out __allocate_resource() Date: Tue, 27 Oct 2015 13:55:46 -0700 Message-Id: <1445979353-1728-55-git-send-email-yinghai@kernel.org> X-Mailer: git-send-email 1.8.4.5 In-Reply-To: <1445979353-1728-1-git-send-email-yinghai@kernel.org> References: <1445979353-1728-1-git-send-email-yinghai@kernel.org> X-Source-IP: aserv0022.oracle.com [141.146.126.234] Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This one is separated from next patch for best fit allocation. Split out __allocate_resource(), and it will not hold lock, so we could use it in other functions that hold the resource lock already. -v2: according to Linus, using "bool lock" as parameter aka "conditionally take lock" is *wrong*. Signed-off-by: Yinghai Lu Acked-by: Linus Torvalds --- kernel/resource.c | 70 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index 6927298..62321b0 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -654,7 +654,7 @@ static int find_resource(struct resource *root, struct resource *new, } /** - * reallocate_resource - allocate a slot in the resource tree given range & alignment. + * __reallocate_resource - allocate a slot in the resource tree given range & alignment. * The resource will be relocated if the new size cannot be reallocated in the * current location. * @@ -663,7 +663,7 @@ static int find_resource(struct resource *root, struct resource *new, * @newsize: new size of the resource descriptor * @constraint: the size and alignment constraints to be met. */ -static int reallocate_resource(struct resource *root, struct resource *old, +static int __reallocate_resource(struct resource *root, struct resource *old, resource_size_t newsize, struct resource_constraint *constraint) { @@ -671,8 +671,6 @@ static int reallocate_resource(struct resource *root, struct resource *old, struct resource new = *old; struct resource *conflict; - write_lock(&resource_lock); - if ((err = __find_resource(root, old, &new, newsize, constraint))) goto out; @@ -697,14 +695,13 @@ static int reallocate_resource(struct resource *root, struct resource *old, BUG_ON(conflict); } out: - write_unlock(&resource_lock); return err; } - /** - * allocate_resource - allocate empty slot in the resource tree given range & alignment. - * The resource will be reallocated with a new size if it was already allocated + * __allocate_resource - allocate empty slot in the resource tree given range & alignment. + * The resource will be reallocated with a new size if it was already + * allocated * @root: root resource descriptor * @new: resource descriptor desired by caller * @size: requested resource region size @@ -713,15 +710,17 @@ out: * @align: alignment requested, in bytes * @alignf: alignment function, optional, called if not NULL * @alignf_data: arbitrary data to pass to the @alignf function + * + * Caller need to hold resource_lock if needed. */ -int allocate_resource(struct resource *root, struct resource *new, - resource_size_t size, resource_size_t min, - resource_size_t max, resource_size_t align, - resource_size_t (*alignf)(void *, - const struct resource *, - resource_size_t, - resource_size_t), - void *alignf_data) +static int __allocate_resource(struct resource *root, struct resource *new, + resource_size_t size, resource_size_t min, + resource_size_t max, resource_size_t align, + resource_size_t (*alignf)(void *, + const struct resource *, + resource_size_t, + resource_size_t), + void *alignf_data) { int err; struct resource_constraint constraint; @@ -735,20 +734,51 @@ int allocate_resource(struct resource *root, struct resource *new, constraint.alignf = alignf; constraint.alignf_data = alignf_data; - if ( new->parent ) { + if (new->parent) { /* resource is already allocated, try reallocating with the new constraints */ - return reallocate_resource(root, new, size, &constraint); + return __reallocate_resource(root, new, size, &constraint); } - write_lock(&resource_lock); err = find_resource(root, new, size, &constraint); if (err >= 0 && __request_resource(root, new)) err = -EBUSY; - write_unlock(&resource_lock); + return err; } +/** + * allocate_resource - allocate empty slot in the resource tree given range & alignment. + * The resource will be reallocated with a new size if it was already + * allocated + * @root: root resource descriptor + * @new: resource descriptor desired by caller + * @size: requested resource region size + * @min: minimum boundary to allocate + * @max: maximum boundary to allocate + * @align: alignment requested, in bytes + * @alignf: alignment function, optional, called if not NULL + * @alignf_data: arbitrary data to pass to the @alignf function + */ +int allocate_resource(struct resource *root, struct resource *new, + resource_size_t size, resource_size_t min, + resource_size_t max, resource_size_t align, + resource_size_t (*alignf)(void *, + const struct resource *, + resource_size_t, + resource_size_t), + void *alignf_data) +{ + int ret; + + write_lock(&resource_lock); + ret = __allocate_resource(root, new, size, min, max, align, + alignf, alignf_data); + write_unlock(&resource_lock); + + return ret; +} + EXPORT_SYMBOL(allocate_resource); /**