From patchwork Thu Dec 14 07:37:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 13492509 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6451FC4167B for ; Thu, 14 Dec 2023 07:38:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7C178D00A0; Thu, 14 Dec 2023 02:38:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C2B7E8D009C; Thu, 14 Dec 2023 02:38:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA5808D00A0; Thu, 14 Dec 2023 02:38:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 986D48D009C for ; Thu, 14 Dec 2023 02:38:19 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 65F9F404CF for ; Thu, 14 Dec 2023 07:38:19 +0000 (UTC) X-FDA: 81564620718.29.7767878 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by imf28.hostedemail.com (Postfix) with ESMTP id 41014C0015 for ; Thu, 14 Dec 2023 07:38:17 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QPcGYiaA; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf28.hostedemail.com: domain of vishal.l.verma@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702539497; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5J+gIfvXGnhCDLBK5rZw2WLoOAKtg347Jw8tbqyeHBk=; b=z9mIUZljZOntghzSqdBJFEnWJfQqlfI3T2wdLpBzyBW2Tm7XsJEOZ2YQ2IXjj6IIN+xr1T WnJGIsgJesXvA9sypowKZtn0pKpDJcoRKGhaSOwPhVJ+57cGzPDtrL7fLjkT1a/DPbW7oF vv6EeREBhqC1GMGDzFaIoWWFxNY1oX4= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QPcGYiaA; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf28.hostedemail.com: domain of vishal.l.verma@intel.com designates 192.55.52.43 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702539497; a=rsa-sha256; cv=none; b=rw+VUIIi0NGpCGhMyEc9W6XTuHGpFnBMp+SFLppx+Ui8DvZ0le1yaE2D7FRFz9h0EAFMD9 HNZuaufYVMxflMLj3E7fjCBlvrRwUJ4FLo6qW8Yw395ypKPwI97BAGmTKalAy9SReqnLMV FrMVJVBNL4A+2UfG42heka+mAaplWb4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702539497; x=1734075497; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=g9l8RslOMp5ad/gXCRHw+j360xuk1nb3+acdwFrlL50=; b=QPcGYiaANOEb+k3o+l0av37H90eT1ABCPApchs1dnLeC/HCfcim+IZn7 LiostB7pyxnfCnhbSojujYTv1GghTCv3JJQ/Vws+gUyiSvtATHT14gm51 6yNWS6OFmr5Ble6RFvMTKL2jfpKGpWmdWi1aYPvR49HnU+iO+Yaj4cTpt w7v8VO2hZ5PO6WhKTntf31jS2ae2jHZmnxNYrwt4NSkDB0xx57wN0RXjP khVhuFnPvn2iU1HPm+hXYa/JKpgkMTqmdM5LW+nVOrApAqjWOAZ4aV8gU FkCfSbr1ZSQlTYoSRisxzvp1ITzJb3pipfiyFWinrvbZd0DyUlSFxpPdg Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10923"; a="481275524" X-IronPort-AV: E=Sophos;i="6.04,274,1695711600"; d="scan'208";a="481275524" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 23:38:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10923"; a="723972064" X-IronPort-AV: E=Sophos;i="6.04,274,1695711600"; d="scan'208";a="723972064" Received: from llblake-mobl1.amr.corp.intel.com (HELO [192.168.1.200]) ([10.213.191.124]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 23:38:12 -0800 From: Vishal Verma Date: Thu, 14 Dec 2023 00:37:55 -0700 Subject: [PATCH v5 2/4] dax/bus: Use guard(device) in sysfs attribute helpers MIME-Version: 1.0 Message-Id: <20231214-vv-dax_abi-v5-2-3f7b006960b4@intel.com> References: <20231214-vv-dax_abi-v5-0-3f7b006960b4@intel.com> In-Reply-To: <20231214-vv-dax_abi-v5-0-3f7b006960b4@intel.com> To: Dan Williams , Vishal Verma , Dave Jiang , Andrew Morton , Oscar Salvador Cc: linux-kernel@vger.kernel.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, David Hildenbrand , Dave Hansen , Huang Ying , Greg Kroah-Hartman , linux-mm@kvack.org, Joao Martins X-Mailer: b4 0.13-dev-433a8 X-Developer-Signature: v=1; a=openpgp-sha256; l=7001; i=vishal.l.verma@intel.com; h=from:subject:message-id; bh=g9l8RslOMp5ad/gXCRHw+j360xuk1nb3+acdwFrlL50=; b=owGbwMvMwCXGf25diOft7jLG02pJDKlVGx4ZxJv18x/TfPRo6pLQrU05Fp3PElasO7C0qM1kr 5tHY3xlRykLgxgXg6yYIsvfPR8Zj8ltz+cJTHCEmcPKBDKEgYtTACZySoGRoZ9jz78FO0WOfBHN z7hkJdKzWZrxycoHdT8NjQSDuFZGHmZk+Kn/VmUD14viH/ymS1fZFjwx1PJr9J8nFCC9Yf3cLpa H7AA= X-Developer-Key: i=vishal.l.verma@intel.com; a=openpgp; fpr=F8682BE134C67A12332A2ED07AFA61BEA3B84DFF X-Rspam-User: X-Stat-Signature: x1zqdfxt3qm4r8pdw7opar9rzhtkrsos X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 41014C0015 X-HE-Tag: 1702539497-930383 X-HE-Meta: U2FsdGVkX18uM2pGorDFAjOxRVfLzpqQBdprLFZ+gx0DCQ1XS/9P1ESix6mH7NDPmNJwZ7zTkP8wSwMWO1Vl+EHiWPWgdohLWBTpHegugWIqOi3jKwVRLh/iP6k9ntKqIfdxCPTxm1+eZbhEPKp+7MtC18c0XI2jpdHimwbPoX0AynHi90E/dW1ABzLFFPBEdlKMHTPSHjEZBU1EOZshDfBRVhq+4hI63hDI9gjPxmUM41yXESbpGKM3uakKGoNqaU7gXNYJKN3LKiU6gvho9ZzeUy0Ug+X5JwKIzsyuVjL/x4Su4yVbDpQuwEZ35+SjLUBZxyiIHxHvHriNF5B2sp7vXzIERCwkDoDMwOoC9YJYbcBKFNxkvvlRAzQdd2ObQnzlyTcdV9zAVv3r1pyw+H4rvrGGcsKJX4yszopK1sO2ML/LfHHxJT0ufKS95OY7mAbLcA53UPu7OdXafYhDfnUvdtHWQ0I4hJh0CskIdWUC1d1RfrYtdduvVGo43GtGl4C5sVg9ysAiUJbb11vPnd7NFBAIfYtGJ/RBv0gUlos3OsvzgRMukQoUS0ys/+fLG5/UL6xIiPcFq1WuFvpRyv2Ja+r6+W4thiCpkq9Da5JogXni0U/Y1orqbi7OS1vBetBE05OV2XOBjdsQFM5+zirH7PJLzYtYqL0QYtp7m4XC54L3bN7FL27i4mMICzQ+nH+KdoPrvbHDAEQuPthVYuK0zxBIFTkwD6JeEon5PLkCID/ZlFhcUM+nwsAgs6rfyENa1D44o5vsPTcZQwbLjgHLUMjcBqUS1lB+E7WA6t1pPSVjf/Sp6r/rvI64nP3ZtI3GfBFu/J+RkQEA+Nm9IBfSLHVIhusYhpm8GoIrlrYXGPkzx9cIJ2ahw3s5uU0moKV9rB6bAQoizjRrqPjg1prrbUvX3puYTjkucwZNFasiEtgPgJ1B5lqgZNsf59i2GJWyPLP9qf0fOvVCtQ2 275f//bO MbEIS3WUhkJCP3qWnl2mbx+iWYDPZTlX0YwrpYAAiiTpQwtSM/9mmjeg/o5xK8IpYXTnu/UiPMAZ3/hlIU03G6nd/yNJ4dS1bueMDRvIkenKNJx/vYqZ9twO4+D5yUBIHTNk3xd5E1tRiC221cccYd6wy31uJU60pu86gE72obi6Z/jxphTftLBPjDeLFPlWBEYY4 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Use the guard(device) macro to lock a 'struct device', and unlock it automatically when going out of scope using Scope Based Resource Management semantics. A lot of the sysfs attribute writes in drivers/dax/bus.c benefit from a cleanup using these, so change these where applicable. Cc: Joao Martins Cc: Dan Williams Signed-off-by: Vishal Verma --- drivers/dax/bus.c | 143 ++++++++++++++++++++++-------------------------------- 1 file changed, 59 insertions(+), 84 deletions(-) diff --git a/drivers/dax/bus.c b/drivers/dax/bus.c index 1ff1ab5fa105..6226de131d17 100644 --- a/drivers/dax/bus.c +++ b/drivers/dax/bus.c @@ -294,13 +294,10 @@ static ssize_t available_size_show(struct device *dev, struct device_attribute *attr, char *buf) { struct dax_region *dax_region = dev_get_drvdata(dev); - unsigned long long size; - device_lock(dev); - size = dax_region_avail_size(dax_region); - device_unlock(dev); + guard(device)(dev); - return sprintf(buf, "%llu\n", size); + return sprintf(buf, "%llu\n", dax_region_avail_size(dax_region)); } static DEVICE_ATTR_RO(available_size); @@ -309,17 +306,14 @@ static ssize_t seed_show(struct device *dev, { struct dax_region *dax_region = dev_get_drvdata(dev); struct device *seed; - ssize_t rc; if (is_static(dax_region)) return -EINVAL; - device_lock(dev); + guard(device)(dev); seed = dax_region->seed; - rc = sprintf(buf, "%s\n", seed ? dev_name(seed) : ""); - device_unlock(dev); - return rc; + return sprintf(buf, "%s\n", seed ? dev_name(seed) : ""); } static DEVICE_ATTR_RO(seed); @@ -328,24 +322,28 @@ static ssize_t create_show(struct device *dev, { struct dax_region *dax_region = dev_get_drvdata(dev); struct device *youngest; - ssize_t rc; if (is_static(dax_region)) return -EINVAL; - device_lock(dev); + guard(device)(dev); youngest = dax_region->youngest; - rc = sprintf(buf, "%s\n", youngest ? dev_name(youngest) : ""); - device_unlock(dev); - return rc; + return sprintf(buf, "%s\n", youngest ? dev_name(youngest) : ""); } static ssize_t create_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { struct dax_region *dax_region = dev_get_drvdata(dev); + struct dev_dax_data data = { + .dax_region = dax_region, + .size = 0, + .id = -1, + .memmap_on_memory = false, + }; unsigned long long avail; + struct dev_dax *dev_dax; ssize_t rc; int val; @@ -358,38 +356,25 @@ static ssize_t create_store(struct device *dev, struct device_attribute *attr, if (val != 1) return -EINVAL; - device_lock(dev); + guard(device)(dev); avail = dax_region_avail_size(dax_region); if (avail == 0) - rc = -ENOSPC; - else { - struct dev_dax_data data = { - .dax_region = dax_region, - .size = 0, - .id = -1, - .memmap_on_memory = false, - }; - struct dev_dax *dev_dax = devm_create_dev_dax(&data); + return -ENOSPC; - if (IS_ERR(dev_dax)) - rc = PTR_ERR(dev_dax); - else { - /* - * In support of crafting multiple new devices - * simultaneously multiple seeds can be created, - * but only the first one that has not been - * successfully bound is tracked as the region - * seed. - */ - if (!dax_region->seed) - dax_region->seed = &dev_dax->dev; - dax_region->youngest = &dev_dax->dev; - rc = len; - } - } - device_unlock(dev); + dev_dax = devm_create_dev_dax(&data); + if (IS_ERR(dev_dax)) + return PTR_ERR(dev_dax); - return rc; + /* + * In support of crafting multiple new devices simultaneously multiple + * seeds can be created, but only the first one that has not been + * successfully bound is tracked as the region seed. + */ + if (!dax_region->seed) + dax_region->seed = &dev_dax->dev; + dax_region->youngest = &dev_dax->dev; + + return len; } static DEVICE_ATTR_RW(create); @@ -481,12 +466,9 @@ static int __free_dev_dax_id(struct dev_dax *dev_dax) static int free_dev_dax_id(struct dev_dax *dev_dax) { struct device *dev = &dev_dax->dev; - int rc; - device_lock(dev); - rc = __free_dev_dax_id(dev_dax); - device_unlock(dev); - return rc; + guard(device)(dev); + return __free_dev_dax_id(dev_dax); } static int alloc_dev_dax_id(struct dev_dax *dev_dax) @@ -908,9 +890,8 @@ static ssize_t size_show(struct device *dev, struct dev_dax *dev_dax = to_dev_dax(dev); unsigned long long size; - device_lock(dev); + guard(device)(dev); size = dev_dax_size(dev_dax); - device_unlock(dev); return sprintf(buf, "%llu\n", size); } @@ -1080,17 +1061,16 @@ static ssize_t size_store(struct device *dev, struct device_attribute *attr, return -EINVAL; } - device_lock(dax_region->dev); - if (!dax_region->dev->driver) { - device_unlock(dax_region->dev); + guard(device)(dax_region->dev); + if (!dax_region->dev->driver) return -ENXIO; - } - device_lock(dev); + + guard(device)(dev); rc = dev_dax_resize(dax_region, dev_dax, val); - device_unlock(dev); - device_unlock(dax_region->dev); + if (rc) + return rc; - return rc == 0 ? len : rc; + return len; } static DEVICE_ATTR_RW(size); @@ -1137,21 +1117,20 @@ static ssize_t mapping_store(struct device *dev, struct device_attribute *attr, if (rc) return rc; - rc = -ENXIO; - device_lock(dax_region->dev); - if (!dax_region->dev->driver) { - device_unlock(dax_region->dev); - return rc; - } - device_lock(dev); + guard(device)(dax_region->dev); + if (!dax_region->dev->driver) + return -ENXIO; + guard(device)(dev); to_alloc = range_len(&r); - if (alloc_is_aligned(dev_dax, to_alloc)) - rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc); - device_unlock(dev); - device_unlock(dax_region->dev); + if (!alloc_is_aligned(dev_dax, to_alloc)) + return -ENXIO; - return rc == 0 ? len : rc; + rc = alloc_dev_dax_range(dev_dax, r.start, to_alloc); + if (rc) + return rc; + + return len; } static DEVICE_ATTR_WO(mapping); @@ -1196,27 +1175,23 @@ static ssize_t align_store(struct device *dev, struct device_attribute *attr, if (!dax_align_valid(val)) return -EINVAL; - device_lock(dax_region->dev); - if (!dax_region->dev->driver) { - device_unlock(dax_region->dev); + guard(device)(dax_region->dev); + if (!dax_region->dev->driver) return -ENXIO; - } - device_lock(dev); - if (dev->driver) { - rc = -EBUSY; - goto out_unlock; - } + guard(device)(dev); + if (dev->driver) + return -EBUSY; align_save = dev_dax->align; dev_dax->align = val; rc = dev_dax_validate_align(dev_dax); - if (rc) + if (rc) { dev_dax->align = align_save; -out_unlock: - device_unlock(dev); - device_unlock(dax_region->dev); - return rc == 0 ? len : rc; + return rc; + } + + return len; } static DEVICE_ATTR_RW(align);