From patchwork Fri Dec 15 05:25:27 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: 13493999 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 A27A0C4332F for ; Fri, 15 Dec 2023 05:25:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5EC0D8D0114; Fri, 15 Dec 2023 00:25:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 59C218D0103; Fri, 15 Dec 2023 00:25:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43CEA8D0114; Fri, 15 Dec 2023 00:25:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 321638D0103 for ; Fri, 15 Dec 2023 00:25:44 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 00DE740BAA for ; Fri, 15 Dec 2023 05:25:43 +0000 (UTC) X-FDA: 81567915366.17.5042E0E Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by imf04.hostedemail.com (Postfix) with ESMTP id EBB6D40004 for ; Fri, 15 Dec 2023 05:25:41 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JG4N7r1Y; spf=pass (imf04.hostedemail.com: domain of vishal.l.verma@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702617942; 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=SFE81RVIBefb8ndeDPk21+d5GyAt9g3Z1geS6hDUsqctkZeMQH9YJAAR7m8pnKh729rZG+ lRqEOieojkKi+u8dl0XXjaVHGWz+Pe/DBqEMVRpWP+DUVeGGmMh2qhmk8mrzms+h0LRxTW G2c2fkGrYl5rt9mpswTkw/f1QrF5QGo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702617942; a=rsa-sha256; cv=none; b=dIsTUFYRZhDk+72pbLjEgg7tFOCAjU3E6O78HCk/S6OnV04KBTieuVCGp8OdGS5KUe+PhZ mfkalJ9qO/bDSQgxrTJhxo0rUIXhQ6G//Wy2W6pp1vaoTJvpl+7P9mq9X02pSbhLBPINUK vPhGlSouiZyA+dOab5z+905gT844NnU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=JG4N7r1Y; spf=pass (imf04.hostedemail.com: domain of vishal.l.verma@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=vishal.l.verma@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702617941; x=1734153941; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=g9l8RslOMp5ad/gXCRHw+j360xuk1nb3+acdwFrlL50=; b=JG4N7r1Y0DjNUSwWXgGVWBPpHMKJr/HBIoHFBE4cKWjbrKo7SnzYxNnk gzagxomHBmrgFXkKLGUUKlE5K1IGgJYmTOKk1nvSlTNlToRSKrbwRMe+d tdxxHQFnyPO/4xsM83G/EIOiN5ryI4RtbHxz3dfOA7X5onokiquMvup+E GQ/MIX2ic65AEqnWsEg3Kkibj7QA1zXQB83s22VmGB2Xkr5Ty5eruSCnW DYvNJHiujRS0MPPXmgAUqr75Bo5lfa/96Sc35L3tnI0R64aZrlAZibAce ncyTW/WHthOF8A7kDNDBKO7FxTiQzHmEnKmwJrYEA1sDkQHgx0KDGCiSY Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="461695028" X-IronPort-AV: E=Sophos;i="6.04,277,1695711600"; d="scan'208";a="461695028" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Dec 2023 21:25:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10924"; a="808847937" X-IronPort-AV: E=Sophos;i="6.04,277,1695711600"; d="scan'208";a="808847937" Received: from mmtakalk-mobl.amr.corp.intel.com (HELO [192.168.1.200]) ([10.212.109.101]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Dec 2023 21:25:38 -0800 From: Vishal Verma Date: Thu, 14 Dec 2023 22:25:27 -0700 Subject: [PATCH v6 2/4] dax/bus: Use guard(device) in sysfs attribute helpers MIME-Version: 1.0 Message-Id: <20231214-vv-dax_abi-v6-2-ad900d698438@intel.com> References: <20231214-vv-dax_abi-v6-0-ad900d698438@intel.com> In-Reply-To: <20231214-vv-dax_abi-v6-0-ad900d698438@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=owGbwMvMwCXGf25diOft7jLG02pJDKnVj/23PV4Tsi/68R5Hl9unM3Y0djytWdHAbcGkJ6wik Xx6GsP5jlIWBjEuBlkxRZa/ez4yHpPbns8TmOAIM4eVCWQIAxenAEykr57hf0DG/93heXPCGta+ VdH15phpYq+zb1lQoDBvzzV7Xmm3bEaG70EflVMniO1OexLerz/H53mywgcvmXW6Pzq2vT/JGvK bGQA= X-Developer-Key: i=vishal.l.verma@intel.com; a=openpgp; fpr=F8682BE134C67A12332A2ED07AFA61BEA3B84DFF X-Rspamd-Queue-Id: EBB6D40004 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: a4zmncfm6fhhrqq85fj7x1rwxypm3fcx X-HE-Tag: 1702617941-208846 X-HE-Meta: U2FsdGVkX1+kH54ILAJeJsmSXMITOLpy0RjkyAVDKZ0yObsMHyQkSnOVLYFtjbwFRYH06H+sa3sN+LaNo4sQrspErv6aKFfSBqkuBCv3C+vG9mdtoFpmvEUePfWe4XkxWbyvMJpeVTtRIb/oRiPev2g3lW9iCOiw5gboNd/qalaN6Px8NRLvazKqFegb0bPi8OHOFdj12XPcYgXMpomG28rLEpnpln3w2VN1cxp6jXgGJmtkkk0DmKYWoTP1GdUTkXHQ1beYYoqry6mI2YYi7oAOmB+iJDePE2b42GiSpegEefrNcnMEt2LCzhVVhniL3OPiqV7eJojoi7w5T2BH1yoH8UxQDYdtOMqBI/S8qTeC+tkSfckYcheej6UxZUrCrpq6WJxx0P0ZJrdJkr3UZj/icKe0U0H7XKgLYmdHk/Q+3mQ5acJgUjk1rKg5yE4ZNneD5uwEt4m8i4VW8G6px9J9t9vfN8kyPLfg+skDjV73eHrmwWdly7qpudheEaBztgLEHQ2w4AHwYiyBOQaa1jARu3vGQbhG0T9z/97zK6P+Ty2+Po2evKX9cVpHbEAwKiGuWEawxvS04H/qv3WXp4EuRDFCcZe6s7qmAA61OqicrCJ6qQcYN2WcCALcGCLspof+6YotfQndYSHWFvkNiUY18qy29UuOz0dsZKKS5yq+kC60FI5czgihbBKqj1aov/JuNf8JCBW4jbu5bH+hBXYfQLC/EZnZufUDcx+2vcwdChCxt4zqmy+xCQxTiVNURqAqzWQv8JNAVIYBz/XlUQhJyT/ifwOTvnWUynNJe4VzbJsFMlwe9OOjwQf/q4WQtmzfxL+b1oNVRZJGl0dfZwbBUOva9/X4OF2+mnAer+0Nia1S31+oDGhgddKIxP4M+77AzG4htM/UjzhkafsqOmU5KORjsfiyk37vnoNPiErmCG2yc3QO7v3kZzPtSlbSpkdvKCKWpKGYy8IsAlF cXMF7ABu 4/DAqLUryO+TiOve1ynM6Vk+pfzY1Ica4C9Q6HBfl1b4SSnYCiukoFBs4DcOa8IWfz6UrZvYUsu88FmXt6yn+sqHbfowSwVlHkQJGyNPYaMk9UkkXehEVrP3SxmOEVq9i7lWdoX6jAJLQUplLRdaeZe3BLZSFmlO+tsqBzxH9aDOYwH3NEASJNC2FSDkabToK8IVN 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 Reviewed-by: Jonathan Cameron --- 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);