From patchwork Tue Mar 8 18:08:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kani, Toshi" X-Patchwork-Id: 8536331 Return-Path: X-Original-To: patchwork-linux-nvdimm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 04DE2C0553 for ; Tue, 8 Mar 2016 17:16:21 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 85A95201FE for ; Tue, 8 Mar 2016 17:16:18 +0000 (UTC) Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7AB7F201FA for ; Tue, 8 Mar 2016 17:16:17 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id AE7301A1EBF; Tue, 8 Mar 2016 09:16:30 -0800 (PST) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from g4t3426.houston.hp.com (g4t3426.houston.hp.com [15.201.208.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 73A0D1A1EBF for ; Tue, 8 Mar 2016 09:16:29 -0800 (PST) Received: from g9t2301.houston.hp.com (g9t2301.houston.hp.com [16.216.185.78]) by g4t3426.houston.hp.com (Postfix) with ESMTP id DDF6759; Tue, 8 Mar 2016 17:16:14 +0000 (UTC) Received: from misato.fc.hp.com (misato.fc.hp.com [16.78.168.61]) by g9t2301.houston.hp.com (Postfix) with ESMTP id 1B6F657; Tue, 8 Mar 2016 17:16:13 +0000 (UTC) From: Toshi Kani To: mingo@kernel.org, bp@suse.de, dan.j.williams@intel.com, rjw@rjwysocki.net, akpm@linux-foundation.org Subject: [PATCH v2-UPDATE2 3/4] resource: Add device-managed insert/remove_resource() Date: Tue, 8 Mar 2016 11:08:50 -0700 Message-Id: <1457460530-17959-1-git-send-email-toshi.kani@hpe.com> X-Mailer: git-send-email 2.5.0 Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nvdimm@lists.01.org X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, 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 insert_resource() and remove_resouce() are called by producers of resources, such as FW modules and bus drivers. These modules may be implemented as loadable modules. Add device-managed implementaions of insert_resource() and remove_resouce() functions. Signed-off-by: Toshi Kani Cc: Ingo Molnar Cc: Borislav Petkov Cc: Andrew Morton Cc: Dan Williams Acked-by: Ingo Molnar --- v2-UPDATE2: - Update per Ingo's review comments; use ret, use WARN_ON_ONCE(). v2-UPDATE: - Rename a helper remove func to __devm_remove_resource(). (Dan Williams) --- include/linux/ioport.h | 5 +++ kernel/resource.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/include/linux/ioport.h b/include/linux/ioport.h index 8017b8b..3580038 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -259,6 +259,11 @@ extern struct resource * __devm_request_region(struct device *dev, extern void __devm_release_region(struct device *dev, struct resource *parent, resource_size_t start, resource_size_t n); + +extern int devm_insert_resource(struct device *dev, struct resource *root, + struct resource *new); +extern void devm_remove_resource(struct device *dev, struct resource *old); + extern int iomem_map_sanity_check(resource_size_t addr, unsigned long size); extern int iomem_is_exclusive(u64 addr); diff --git a/kernel/resource.c b/kernel/resource.c index effb6ee..86fdd28 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1449,6 +1449,78 @@ void __devm_release_region(struct device *dev, struct resource *parent, EXPORT_SYMBOL(__devm_release_region); /* + * Helper remove function for devm_insert_resource() and devm_remove_resource() + */ +static void __devm_remove_resource(struct device *dev, void *ptr) +{ + struct resource **r = ptr; + + remove_resource(*r); +} + +/** + * devm_insert_resource() - insert an I/O or memory resource + * @dev: device for which to produce the resource + * @root: root of the resource tree + * @new: descriptor of the new resource + * + * This is a device-managed version of insert_resource(). There is usually + * no need to release resources requested by this function explicitly, since + * that will be taken care of when the device is unbound from its bus driver. + * If for some reason the resource needs to be released explicitly, because + * of ordering issues for example, bus drivers must call devm_remove_resource() + * rather than the regular remove_resource(). + * + * devm_insert_resource() is intended for producers of resources, such as + * FW modules and bus drivers. + * + * Returns 0 on success or a negative error code on failure. + */ +int devm_insert_resource(struct device *dev, struct resource *root, + struct resource *new) +{ + struct resource **ptr; + int ret; + + ptr = devres_alloc(__devm_remove_resource, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + *ptr = new; + + ret = insert_resource(root, new); + if (ret) { + dev_err(dev, "unable to insert resource: %pR (%d)\n", new, ret); + devres_free(ptr); + return ret; + } + + devres_add(dev, ptr); + return 0; +} +EXPORT_SYMBOL_GPL(devm_insert_resource); + +/** + * devm_remove_resource() - remove a previously inserted resource + * @dev: device for which to remove the resource + * @old: descriptor of the resource + * + * Remove a resource previously inserted using devm_insert_resource(). + * + * devm_remove_resource() is intended for producers of resources, such as + * FW modules and bus drivers. + */ +void devm_remove_resource(struct device *dev, struct resource *old) +{ + int ret; + + ret = devres_release(dev, __devm_remove_resource, + devm_resource_match, old); + WARN_ON_ONCE(ret); +} +EXPORT_SYMBOL_GPL(devm_remove_resource); + +/* * Called from init/main.c to reserve IO ports. */ #define MAXRESERVE 4