From patchwork Thu Mar 24 01:26:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 8655651 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 3D688C0553 for ; Thu, 24 Mar 2016 01:26:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 49D7120380 for ; Thu, 24 Mar 2016 01:26:49 +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 43A3020390 for ; Thu, 24 Mar 2016 01:26:48 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 1F5691A1ED0; Wed, 23 Mar 2016 18:27:13 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by ml01.01.org (Postfix) with ESMTP id 9B8091A1EE2 for ; Wed, 23 Mar 2016 18:27:11 -0700 (PDT) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP; 23 Mar 2016 18:26:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,383,1455004800"; d="scan'208";a="917588266" Received: from dwillia2-desk3.jf.intel.com ([10.54.39.14]) by orsmga001.jf.intel.com with ESMTP; 23 Mar 2016 18:26:46 -0700 Subject: [PATCH 09/13] libnvdimm, pmem: use devm_add_action to release bdev resources From: Dan Williams To: linux-nvdimm@lists.01.org Date: Wed, 23 Mar 2016 18:26:08 -0700 Message-ID: <20160324012608.21436.8940.stgit@dwillia2-desk3.jf.intel.com> In-Reply-To: <20160324012520.21436.22505.stgit@dwillia2-desk3.jf.intel.com> References: <20160324012520.21436.22505.stgit@dwillia2-desk3.jf.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-kernel@vger.kernel.org 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, T_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 Register a callback to clean up the request_queue and put the gendisk at driver disable time. Cc: Ross Zwisler Signed-off-by: Dan Williams Reviewed-by: Johannes Thumshirn --- drivers/nvdimm/pmem.c | 88 ++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 49 deletions(-) diff --git a/drivers/nvdimm/pmem.c b/drivers/nvdimm/pmem.c index ff336dd26064..97bc91b944b7 100644 --- a/drivers/nvdimm/pmem.c +++ b/drivers/nvdimm/pmem.c @@ -184,6 +184,17 @@ static const struct block_device_operations pmem_fops = { .revalidate_disk = nvdimm_revalidate_disk, }; +static void pmem_release_queue(void *q) +{ + blk_cleanup_queue(q); +} + +void pmem_release_disk(void *disk) +{ + del_gendisk(disk); + put_disk(disk); +} + static struct pmem_device *pmem_alloc(struct device *dev, struct resource *res, int id) { @@ -220,25 +231,22 @@ static struct pmem_device *pmem_alloc(struct device *dev, pmem->phys_addr, pmem->size, ARCH_MEMREMAP_PMEM); - if (IS_ERR(pmem->virt_addr)) { + /* + * At release time the queue must be dead before + * devm_memremap_pages is unwound + */ + if (devm_add_action(dev, pmem_release_queue, q)) { blk_cleanup_queue(q); - return (void __force *) pmem->virt_addr; + return ERR_PTR(-ENOMEM); } + if (IS_ERR(pmem->virt_addr)) + return (void __force *) pmem->virt_addr; + pmem->pmem_queue = q; return pmem; } -static void pmem_detach_disk(struct pmem_device *pmem) -{ - if (!pmem->pmem_disk) - return; - - del_gendisk(pmem->pmem_disk); - put_disk(pmem->pmem_disk); - blk_cleanup_queue(pmem->pmem_queue); -} - static int pmem_attach_disk(struct device *dev, struct nd_namespace_common *ndns, struct pmem_device *pmem) { @@ -255,8 +263,10 @@ static int pmem_attach_disk(struct device *dev, pmem->pmem_queue->queuedata = pmem; disk = alloc_disk_node(0, nid); - if (!disk) { - blk_cleanup_queue(pmem->pmem_queue); + if (!disk) + return -ENOMEM; + if (devm_add_action(dev, pmem_release_disk, disk)) { + put_disk(disk); return -ENOMEM; } @@ -413,15 +423,6 @@ static int nd_pfn_init(struct nd_pfn *nd_pfn) return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)); } -static void nvdimm_namespace_detach_pfn(struct nd_pfn *nd_pfn) -{ - struct pmem_device *pmem; - - /* free pmem disk */ - pmem = dev_get_drvdata(&nd_pfn->dev); - pmem_detach_disk(pmem); -} - /* * We hotplug memory at section granularity, pad the reserved area from * the previous section base to the namespace base address. @@ -444,7 +445,6 @@ static unsigned long init_altmap_reserve(resource_size_t base) static int __nvdimm_namespace_attach_pfn(struct nd_pfn *nd_pfn) { - int rc; struct resource res; struct request_queue *q; struct pmem_device *pmem; @@ -481,35 +481,33 @@ static int __nvdimm_namespace_attach_pfn(struct nd_pfn *nd_pfn) altmap = & __altmap; altmap->free = PHYS_PFN(pmem->data_offset - SZ_8K); altmap->alloc = 0; - } else { - rc = -ENXIO; - goto err; - } + } else + return -ENXIO; /* establish pfn range for lookup, and switch to direct map */ q = pmem->pmem_queue; memcpy(&res, &nsio->res, sizeof(res)); res.start += start_pad; res.end -= end_trunc; + devm_remove_action(dev, pmem_release_queue, q); devm_memunmap(dev, (void __force *) pmem->virt_addr); pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, &res, &q->q_usage_counter, altmap); pmem->pfn_flags |= PFN_MAP; - if (IS_ERR(pmem->virt_addr)) { - rc = PTR_ERR(pmem->virt_addr); - goto err; + + /* + * At release time the queue must be dead before + * devm_memremap_pages is unwound + */ + if (devm_add_action(dev, pmem_release_queue, q)) { + blk_cleanup_queue(q); + return -ENOMEM; } + if (IS_ERR(pmem->virt_addr)) + return PTR_ERR(pmem->virt_addr); /* attach pmem disk in "pfn-mode" */ - rc = pmem_attach_disk(dev, ndns, pmem); - if (rc) - goto err; - - return rc; - err: - nvdimm_namespace_detach_pfn(nd_pfn); - return rc; - + return pmem_attach_disk(dev, ndns, pmem); } static int nvdimm_namespace_attach_pfn(struct nd_namespace_common *ndns) @@ -551,8 +549,8 @@ static int nd_pmem_probe(struct device *dev) if (is_nd_btt(dev)) { /* btt allocates its own request_queue */ + devm_remove_action(dev, pmem_release_queue, pmem->pmem_queue); blk_cleanup_queue(pmem->pmem_queue); - pmem->pmem_queue = NULL; return nvdimm_namespace_attach_btt(ndns); } @@ -565,7 +563,6 @@ static int nd_pmem_probe(struct device *dev) * We'll come back as either btt-pmem, or pfn-pmem, so * drop the queue allocation for now. */ - blk_cleanup_queue(pmem->pmem_queue); return -ENXIO; } @@ -574,15 +571,8 @@ static int nd_pmem_probe(struct device *dev) static int nd_pmem_remove(struct device *dev) { - struct pmem_device *pmem = dev_get_drvdata(dev); - if (is_nd_btt(dev)) nvdimm_namespace_detach_btt(to_nd_btt(dev)); - else if (is_nd_pfn(dev)) - nvdimm_namespace_detach_pfn(to_nd_pfn(dev)); - else - pmem_detach_disk(pmem); - return 0; }