nvdimm: Introduce guard() for nvdimm_bus_lock
Dave Jiang
maintainerPATCH 1/1 nvdimm: Introduce guard() for nvdimm_bus_lock
Converting nvdimm_bus_lock/unlock to guard() to clean up usage of gotos for error handling and avoid future mistakes of missed unlock on error paths.
---
Ira, up to you if you want to take this cleanup.
--- drivers/nvdimm/badrange.c | 3 +- drivers/nvdimm/btt_devs.c | 20 +++----- drivers/nvdimm/bus.c | 17 +++---- drivers/nvdimm/claim.c | 5 +- drivers/nvdimm/core.c | 17 ++++--- drivers/nvdimm/dax_devs.c | 8 ++-- drivers/nvdimm/dimm.c | 17 ++++--- drivers/nvdimm/dimm_devs.c | 43 ++++++++--------- drivers/nvdimm/namespace_devs.c | 81 +++++++++++++++------------------ drivers/nvdimm/nd.h | 2 + drivers/nvdimm/pfn_devs.c | 29 +++++------- drivers/nvdimm/region.c | 14 +++--- drivers/nvdimm/region_devs.c | 72 ++++++++++++++--------------- drivers/nvdimm/security.c | 6 +-- 14 files changed, 149 insertions(+), 185 deletions(-)
drivers/nvdimm/badrange.c +1 -2
@@ -278,8 +278,7 @@ void nvdimm_badblocks_populate(struct nd_region *nd_region, } nvdimm_bus =walk_to_nvdimm_bus (&nd_region->dev); -nvdimm_bus_lock (&nvdimm_bus->dev); +guard (nvdimm_bus)(&nvdimm_bus->dev);badblocks_populate (&nvdimm_bus->badrange, bb, range); -nvdimm_bus_unlock (&nvdimm_bus->dev); }EXPORT_SYMBOL_GPL (nvdimm_badblocks_populate);
drivers/nvdimm/btt_devs.c +7 -13
@@ -50,14 +50,12 @@ static ssize_t sector_size_store(struct device *dev,struct nd_btt *nd_btt =to_nd_btt (dev);ssize_t rc; -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_size_select_store (dev, buf, &nd_btt->lbasize, btt_lbasize_supported);dev_dbg (dev,"result: %zd wrote: %s%s" , rc, buf, buf[len -1 ] =='\n' ?"" :"\n" ); -nvdimm_bus_unlock (dev); -device_unlock (dev);return rc ? rc : len; }
@@ -95,10 +93,9 @@ static ssize_t namespace_show(struct device *dev,struct nd_btt *nd_btt =to_nd_btt (dev);ssize_t rc; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev); rc =sprintf (buf,"%s\n" , nd_btt->ndns ?dev_name (&nd_btt->ndns->dev) :"" ); -nvdimm_bus_unlock (dev);return rc; }
@@ -108,13 +105,11 @@ static ssize_t namespace_store(struct device *dev,struct nd_btt *nd_btt =to_nd_btt (dev);ssize_t rc; -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_namespace_store (dev, &nd_btt->ndns, buf, len);dev_dbg (dev,"result: %zd wrote: %s%s" , rc, buf, buf[len -1 ] =='\n' ?"" :"\n" ); -nvdimm_bus_unlock (dev); -device_unlock (dev);return rc; }
@@ -351,9 +346,8 @@ int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)return -ENODEV ; } -nvdimm_bus_lock (&ndns->dev); - btt_dev =__nd_btt_create (nd_region,0 ,NULL , ndns); -nvdimm_bus_unlock (&ndns->dev); +scoped_guard (nvdimm_bus, &ndns->dev) + btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);if (!btt_dev)return -ENOMEM ; btt_sb =devm_kzalloc (dev,sizeof (*btt_sb),GFP_KERNEL );
drivers/nvdimm/bus.c +6 -11
@@ -64,17 +64,15 @@ static struct module *to_bus_provider(struct device *dev)static void nvdimm_bus_probe_start (struct nvdimm_bus *nvdimm_bus) { -nvdimm_bus_lock (&nvdimm_bus->dev); +guard (nvdimm_bus)(&nvdimm_bus->dev); nvdimm_bus->probe_active++; -nvdimm_bus_unlock (&nvdimm_bus->dev); }static void nvdimm_bus_probe_end (struct nvdimm_bus *nvdimm_bus) { -nvdimm_bus_lock (&nvdimm_bus->dev); +guard (nvdimm_bus)(&nvdimm_bus->dev);if (--nvdimm_bus->probe_active ==0 )wake_up (&nvdimm_bus->wait); -nvdimm_bus_unlock (&nvdimm_bus->dev); }static int nvdimm_bus_probe (struct device *dev)
@@ -1177,15 +1175,15 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, goto out; } -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_cmd_clear_to_send (nvdimm_bus, nvdimm, func, buf);if (rc) - goto out_unlock; + goto out; rc = nd_desc->ndctl (nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);if (rc <0 ) - goto out_unlock; + goto out;if (!nvdimm && cmd ==ND_CMD_CLEAR_ERROR && cmd_rc >=0 ) {struct nd_cmd_clear_error *clear_err = buf;
@@ -1197,9 +1195,6 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,if (copy_to_user (p, buf, buf_len)) rc = -EFAULT ; -out_unlock: -nvdimm_bus_unlock (dev); -device_unlock (dev); out:kfree (in_env);kfree (out_env);
drivers/nvdimm/claim.c +2 -3
@@ -35,9 +35,8 @@ void nd_detach_ndns(struct device *dev,if (!ndns)return ;get_device (&ndns->dev); -nvdimm_bus_lock (&ndns->dev); -__nd_detach_ndns (dev, _ndns); -nvdimm_bus_unlock (&ndns->dev); + scoped_guard(nvdimm_bus, &ndns->dev) +__nd_detach_ndns (dev, _ndns);put_device (&ndns->dev); }
drivers/nvdimm/core.c +8 -9
@@ -141,9 +141,8 @@ static void nvdimm_map_put(void *data)struct nvdimm_map *nvdimm_map = data;struct nvdimm_bus *nvdimm_bus = nvdimm_map->nvdimm_bus; -nvdimm_bus_lock (&nvdimm_bus->dev); +guard (nvdimm_bus)(&nvdimm_bus->dev);kref_put (&nvdimm_map->kref, nvdimm_map_release); -nvdimm_bus_unlock (&nvdimm_bus->dev); }/**
@@ -158,13 +157,13 @@ void *devm_nvdimm_memremap(struct device *dev, resource_size_t offset, {struct nvdimm_map *nvdimm_map; -nvdimm_bus_lock (dev); - nvdimm_map =find_nvdimm_map (dev, offset); -if (!nvdimm_map) - nvdimm_map =alloc_nvdimm_map (dev, offset, size, flags); -else -kref_get (&nvdimm_map->kref); -nvdimm_bus_unlock (dev); + scoped_guard(nvdimm_bus, dev) { + nvdimm_map =find_nvdimm_map (dev, offset); +if (!nvdimm_map) + nvdimm_map =alloc_nvdimm_map (dev, offset, size, flags); +else +kref_get (&nvdimm_map->kref); + }if (!nvdimm_map)return NULL ;
drivers/nvdimm/dax_devs.c +4 -4
@@ -104,10 +104,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)return -ENODEV ; } -nvdimm_bus_lock (&ndns->dev); - nd_dax =nd_dax_alloc (nd_region); - dax_dev =nd_dax_devinit (nd_dax, ndns); -nvdimm_bus_unlock (&ndns->dev); + scoped_guard(nvdimm_bus, &ndns->dev) { + nd_dax =nd_dax_alloc (nd_region); + dax_dev =nd_dax_devinit (nd_dax, ndns); + }if (!dax_dev)return -ENOMEM ; pfn_sb =devm_kmalloc (dev,sizeof (*pfn_sb),GFP_KERNEL );
drivers/nvdimm/dimm.c +8 -9
@@ -95,13 +95,13 @@ static int nvdimm_probe(struct device *dev)dev_dbg (dev,"config data size: %d\n" , ndd->nsarea.config_size); -nvdimm_bus_lock (dev); -if (ndd->ns_current >=0 ) { - rc =nd_label_reserve_dpa (ndd); -if (rc ==0 ) -nvdimm_set_labeling (dev); + scoped_guard(nvdimm_bus, dev) { +if (ndd->ns_current >=0 ) { + rc =nd_label_reserve_dpa (ndd); +if (rc ==0 ) +nvdimm_set_labeling (dev); + } } -nvdimm_bus_unlock (dev);if (rc) goto err;
@@ -117,9 +117,8 @@ static void nvdimm_remove(struct device *dev) {struct nvdimm_drvdata *ndd =dev_get_drvdata (dev); -nvdimm_bus_lock (dev); -dev_set_drvdata (dev,NULL ); -nvdimm_bus_unlock (dev); + scoped_guard(nvdimm_bus, dev) +dev_set_drvdata (dev,NULL );put_ndd (ndd); }
drivers/nvdimm/dimm_devs.c +18 -25
101 lines not on this page · raw message
drivers/nvdimm/namespace_devs.c +37 -44
171 lines not on this page · raw message
drivers/nvdimm/nd.h +2 -0
@@ -632,6 +632,8 @@ u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,u64 nd_region_interleave_set_altcookie (struct nd_region *nd_region);void nvdimm_bus_lock (struct device *dev);void nvdimm_bus_unlock (struct device *dev); +DEFINE_GUARD(nvdimm_bus, struct device *, nvdimm_bus_lock(_T), nvdimm_bus_unlock(_T)); +bool is_nvdimm_bus_locked (struct device *dev);void nvdimm_check_and_set_ro (struct gendisk *disk);void nvdimm_drvdata_release (struct kref *kref);
drivers/nvdimm/pfn_devs.c +11 -18
@@ -57,8 +57,8 @@ static ssize_t mode_store(struct device *dev,struct nd_pfn *nd_pfn =to_nd_pfn_safe (dev);ssize_t rc =0 ; -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev);if (dev->driver) rc = -EBUSY ;else {
@@ -78,8 +78,6 @@ static ssize_t mode_store(struct device *dev, }dev_dbg (dev,"result: %zd wrote: %s%s" , rc, buf, buf[len -1 ] =='\n' ?"" :"\n" ); -nvdimm_bus_unlock (dev); -device_unlock (dev);return rc ? rc : len; }
@@ -125,14 +123,12 @@ static ssize_t align_store(struct device *dev,unsigned long aligns[MAX_NVDIMM_ALIGN ] = { [0 ] =0 , };ssize_t rc; -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_size_select_store (dev, buf, &nd_pfn->align,nd_pfn_supported_alignments (aligns));dev_dbg (dev,"result: %zd wrote: %s%s" , rc, buf, buf[len -1 ] =='\n' ?"" :"\n" ); -nvdimm_bus_unlock (dev); -device_unlock (dev);return rc ? rc : len; }
@@ -170,10 +166,9 @@ static ssize_t namespace_show(struct device *dev,struct nd_pfn *nd_pfn =to_nd_pfn_safe (dev);ssize_t rc; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev); rc =sprintf (buf,"%s\n" , nd_pfn->ndns ?dev_name (&nd_pfn->ndns->dev) :"" ); -nvdimm_bus_unlock (dev);return rc; }
@@ -183,13 +178,11 @@ static ssize_t namespace_store(struct device *dev,struct nd_pfn *nd_pfn =to_nd_pfn_safe (dev);ssize_t rc; -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_namespace_store (dev, &nd_pfn->ndns, buf, len);dev_dbg (dev,"result: %zd wrote: %s%s" , rc, buf, buf[len -1 ] =='\n' ?"" :"\n" ); -nvdimm_bus_unlock (dev); -device_unlock (dev);return rc; }
@@ -639,10 +632,10 @@ int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)return -ENODEV ; } -nvdimm_bus_lock (&ndns->dev); - nd_pfn =nd_pfn_alloc (nd_region); - pfn_dev =nd_pfn_devinit (nd_pfn, ndns); -nvdimm_bus_unlock (&ndns->dev); + scoped_guard(nvdimm_bus, &ndns->dev) { + nd_pfn =nd_pfn_alloc (nd_region); + pfn_dev =nd_pfn_devinit (nd_pfn, ndns); + }if (!pfn_dev)return -ENOMEM ; pfn_sb =devm_kmalloc (dev,sizeof (*pfn_sb),GFP_KERNEL );
drivers/nvdimm/region.c +7 -7
@@ -87,13 +87,13 @@ static void nd_region_remove(struct device *dev)device_for_each_child (dev,NULL , child_unregister);/* flush attribute readers and disable */ -nvdimm_bus_lock (dev); - nd_region->ns_seed =NULL ; - nd_region->btt_seed =NULL ; - nd_region->pfn_seed =NULL ; - nd_region->dax_seed =NULL ; -dev_set_drvdata (dev,NULL ); -nvdimm_bus_unlock (dev); + scoped_guard(nvdimm_bus, dev) { + nd_region->ns_seed =NULL ; + nd_region->btt_seed =NULL ; + nd_region->pfn_seed =NULL ; + nd_region->dax_seed =NULL ; +dev_set_drvdata (dev,NULL ); + }/* * Note, this assumes device_lock() context to not race
drivers/nvdimm/region_devs.c +36 -36
190 lines not on this page · raw message
drivers/nvdimm/security.c +2 -4
@@ -221,9 +221,8 @@ int nvdimm_security_unlock(struct device *dev)struct nvdimm *nvdimm =to_nvdimm (dev);int rc; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev); rc =__nvdimm_security_unlock (nvdimm); -nvdimm_bus_unlock (dev);return rc; }
@@ -490,9 +489,8 @@ void nvdimm_security_overwrite_query(struct work_struct *work)struct nvdimm *nvdimm =container_of (work,typeof (*nvdimm), dwork.work); -nvdimm_bus_lock (&nvdimm->dev); +guard (nvdimm_bus)(&nvdimm->dev);__nvdimm_security_overwrite_query (nvdimm); -nvdimm_bus_unlock (&nvdimm->dev); }#define OPS \
base-commit: 07e27ad16399afcd693be20211b0dfae63e0615f
Signature · 1 line
2.51.0
Quoted 10 lines · Dave Jiang
Converting nvdimm_bus_lock/unlock to
guard()to clean up usage of gotos for error handling and avoid future mistakes of missed unlock on error paths.Suggested-by Jonathan Cameron <[email protected]>Signed-off-by Dave Jiang <[email protected]>---
Ira, up to you if you want to take this cleanup.
---
[..]
Quoted 24 lines
drivers/nvdimm/bus.c +4
-4@@ -1177,15 +1175,15 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, goto out; } -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_cmd_clear_to_send (nvdimm_bus, nvdimm, func, buf);if (rc) - goto out_unlock; + goto out; rc = nd_desc->ndctl (nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);if (rc <0 ) - goto out_unlock; + goto out;if (!nvdimm && cmd ==ND_CMD_CLEAR_ERROR && cmd_rc >=0 ) {struct nd_cmd_clear_error *clear_err = buf;
[..]
Quoted 14 lines
drivers/nvdimm/dimm.c +1
-5@@ -95,13 +95,13 @@ static int nvdimm_probe(struct device *dev)dev_dbg (dev,"config data size: %d\n" , ndd->nsarea.config_size); -nvdimm_bus_lock (dev); -if (ndd->ns_current >=0 ) { - rc =nd_label_reserve_dpa (ndd); -if (rc ==0 ) -nvdimm_set_labeling (dev); + scoped_guard(nvdimm_bus, dev) {
drivers/nvdimm/dimm.c static int nvdimm_probe(struct device *dev) · line 107
Quoted 10 lines
drivers/nvdimm/dimm.c +5
-1+if (ndd->ns_current >=0 ) { + rc =nd_label_reserve_dpa (ndd); +if (rc ==0 ) +nvdimm_set_labeling (dev); + } } -nvdimm_bus_unlock (dev);if (rc) goto err;
[.. trim the hunks that successfully eliminated goto ..]
My litmus test for scoped-based-cleanup conversions is whether *all* of the gotos are removed in a converted function. So either fully convert all error unwind in a function to scope-based, or none of it. Do not leave people to reason about potentially jumping through scopes with goto.
Dave Jiang
maintainerQuoted 63 lines · On 9/22/25 3:50 PM, [email protected] wrote:
Quoted 10 lines · Dave Jiang
Converting nvdimm_bus_lock/unlock to
guard()to clean up usage of gotos for error handling and avoid future mistakes of missed unlock on error paths.Suggested-by Jonathan Cameron <[email protected]>Signed-off-by Dave Jiang <[email protected]>---
Ira, up to you if you want to take this cleanup.
---[..]
Quoted 24 lines
drivers/nvdimm/bus.c +4
-4@@ -1177,15 +1175,15 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, goto out; } -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_cmd_clear_to_send (nvdimm_bus, nvdimm, func, buf);if (rc) - goto out_unlock; + goto out; rc = nd_desc->ndctl (nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);if (rc <0 ) - goto out_unlock; + goto out;if (!nvdimm && cmd ==ND_CMD_CLEAR_ERROR && cmd_rc >=0 ) {struct nd_cmd_clear_error *clear_err = buf;[..]
Quoted 14 lines
drivers/nvdimm/dimm.c +1
-5@@ -95,13 +95,13 @@ static int nvdimm_probe(struct device *dev)dev_dbg (dev,"config data size: %d\n" , ndd->nsarea.config_size); -nvdimm_bus_lock (dev); -if (ndd->ns_current >=0 ) { - rc =nd_label_reserve_dpa (ndd); -if (rc ==0 ) -nvdimm_set_labeling (dev); + scoped_guard(nvdimm_bus, dev) {drivers/nvdimm/dimm.c static int nvdimm_probe(struct device *dev) · line 107
Quoted 10 lines
drivers/nvdimm/dimm.c +5
-1+if (ndd->ns_current >=0 ) { + rc =nd_label_reserve_dpa (ndd); +if (rc ==0 ) +nvdimm_set_labeling (dev); + } } -nvdimm_bus_unlock (dev);if (rc) goto err;[.. trim the hunks that successfully eliminated goto ..]
Quoted 5 lines
My litmus test for scoped-based-cleanup conversions is whether *all* of the gotos are removed in a converted function. So either fully convert all error unwind in a function to scope-based, or none of it. Do not leave people to reason about potentially jumping through scopes with goto.
This needs a bit more changes. I was undecided if I should include those changes in this patch or if they push out of scope. But it sounds like I should include them.
Dave Jiang wrote:
[..]
Quoted 3 lines
This needs a bit more changes. I was undecided if I should include those changes in this patch or if they push out of scope. But it sounds like I should include them.
You can do them in their own patches, but I would keep those patches together in a series.
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Dave,
kernel test robot noticed the following build errors:
[auto build test ERROR on 07e27ad16399afcd693be20211b0dfae63e0615f]
url: https://github.com/intel-lab-lkp/linux/commits/Dave-Jiang/nvdimm-Introduce-guard-for-nvdimm_bus_lock/20250923-051440 base: 07e27ad16399afcd693be20211b0dfae63e0615f patch link: https://lore.kernel.org/r/20250922211330.1433044-1-dave.jiang%40intel.com patch subject: [PATCH] nvdimm: Introduce guard() for nvdimm_bus_lock config: x86_64-buildonly-randconfig-006-20250923 (https://download.01.org/0day-ci/archive/20250923/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250923/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> drivers/nvdimm/bus.c:1175:3: error: cannot jump from this goto statement to its label
1175 | goto out;
| ^
drivers/nvdimm/bus.c:1179:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
1179 | guard(nvdimm_bus)(dev);
| ^
include/linux/cleanup.h:401:15: note: expanded from macro 'guard'
401 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:166:29: note: expanded from macro '__UNIQUE_ID'
166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:93:1: note: expanded from here
93 | __UNIQUE_ID_guard458
| ^
drivers/nvdimm/bus.c:1178:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
1178 | guard(device)(dev);
| ^
include/linux/cleanup.h:401:15: note: expanded from macro 'guard'
401 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:166:29: note: expanded from macro '__UNIQUE_ID'
166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:84:1: note: expanded from here
84 | __UNIQUE_ID_guard457
| ^
drivers/nvdimm/bus.c:1170:3: error: cannot jump from this goto statement to its label
1170 | goto out;
| ^
drivers/nvdimm/bus.c:1179:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
1179 | guard(nvdimm_bus)(dev);
| ^
include/linux/cleanup.h:401:15: note: expanded from macro 'guard'
401 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:166:29: note: expanded from macro '__UNIQUE_ID'
166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:93:1: note: expanded from here
93 | __UNIQUE_ID_guard458
| ^
drivers/nvdimm/bus.c:1178:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
1178 | guard(device)(dev);
| ^
include/linux/cleanup.h:401:15: note: expanded from macro 'guard'
401 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:166:29: note: expanded from macro '__UNIQUE_ID'
166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:84:1: note: expanded from here
84 | __UNIQUE_ID_guard457
| ^
drivers/nvdimm/bus.c:1164:3: error: cannot jump from this goto statement to its label
1164 | goto out;
| ^
drivers/nvdimm/bus.c:1179:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
1179 | guard(nvdimm_bus)(dev);
| ^
include/linux/cleanup.h:401:15: note: expanded from macro 'guard'
401 | CLASS(_name, __UNIQUE_ID(guard))
| ^
include/linux/compiler.h:166:29: note: expanded from macro '__UNIQUE_ID'
166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
| ^
include/linux/compiler_types.h:84:22: note: expanded from macro '__PASTE'
84 | #define __PASTE(a,b) ___PASTE(a,b)
| ^
include/linux/compiler_types.h:83:23: note: expanded from macro '___PASTE'
83 | #define ___PASTE(a,b) a##b
| ^
<scratch space>:93:1: note: expanded from here
93 | __UNIQUE_ID_guard458
| ^
drivers/nvdimm/bus.c:1178:2: note: jump bypasses initialization of variable with __attribute__((cleanup))
1178 | guard(device)(dev);vim +1175 drivers/nvdimm/bus.c
eaf961536e1622 Dan Williams 2015-05-01 1023
62232e45f4a265 Dan Williams 2015-06-08 1024 static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,
62232e45f4a265 Dan Williams 2015-06-08 1025 int read_only, unsigned int ioctl_cmd, unsigned long arg)
62232e45f4a265 Dan Williams 2015-06-08 1026 {
62232e45f4a265 Dan Williams 2015-06-08 1027 struct nvdimm_bus_descriptor *nd_desc = nvdimm_bus->nd_desc;
62232e45f4a265 Dan Williams 2015-06-08 1028 const struct nd_cmd_desc *desc = NULL;
62232e45f4a265 Dan Williams 2015-06-08 1029 unsigned int cmd = _IOC_NR(ioctl_cmd);
62232e45f4a265 Dan Williams 2015-06-08 1030 struct device *dev = &nvdimm_bus->dev;
58738c495e15ba Dan Williams 2017-08-31 1031 void __user *p = (void __user *) arg;
6de5d06e657acd Dan Williams 2019-07-17 1032 char *out_env = NULL, *in_env = NULL;
62232e45f4a265 Dan Williams 2015-06-08 1033 const char *cmd_name, *dimm_name;
58738c495e15ba Dan Williams 2017-08-31 1034 u32 in_len = 0, out_len = 0;
58738c495e15ba Dan Williams 2017-08-31 1035 unsigned int func = cmd;
e3654eca70d637 Dan Williams 2016-04-28 1036 unsigned long cmd_mask;
58738c495e15ba Dan Williams 2017-08-31 1037 struct nd_cmd_pkg pkg;
006358b35c73ab Dave Jiang 2017-04-07 1038 int rc, i, cmd_rc;
6de5d06e657acd Dan Williams 2019-07-17 1039 void *buf = NULL;
58738c495e15ba Dan Williams 2017-08-31 1040 u64 buf_len = 0;
62232e45f4a265 Dan Williams 2015-06-08 1041
62232e45f4a265 Dan Williams 2015-06-08 1042 if (nvdimm) {
62232e45f4a265 Dan Williams 2015-06-08 1043 desc = nd_cmd_dimm_desc(cmd);
62232e45f4a265 Dan Williams 2015-06-08 1044 cmd_name = nvdimm_cmd_name(cmd);
e3654eca70d637 Dan Williams 2016-04-28 1045 cmd_mask = nvdimm->cmd_mask;
62232e45f4a265 Dan Williams 2015-06-08 1046 dimm_name = dev_name(&nvdimm->dev);
62232e45f4a265 Dan Williams 2015-06-08 1047 } else {
62232e45f4a265 Dan Williams 2015-06-08 1048 desc = nd_cmd_bus_desc(cmd);
62232e45f4a265 Dan Williams 2015-06-08 1049 cmd_name = nvdimm_bus_cmd_name(cmd);
e3654eca70d637 Dan Williams 2016-04-28 1050 cmd_mask = nd_desc->cmd_mask;
62232e45f4a265 Dan Williams 2015-06-08 1051 dimm_name = "bus";
62232e45f4a265 Dan Williams 2015-06-08 1052 }
62232e45f4a265 Dan Williams 2015-06-08 1053
92fe2aa859f52c Dan Williams 2020-07-20 1054 /* Validate command family support against bus declared support */
31eca76ba2fc98 Dan Williams 2016-04-28 1055 if (cmd == ND_CMD_CALL) {
92fe2aa859f52c Dan Williams 2020-07-20 1056 unsigned long *mask;
92fe2aa859f52c Dan Williams 2020-07-20 1057
31eca76ba2fc98 Dan Williams 2016-04-28 1058 if (copy_from_user(&pkg, p, sizeof(pkg)))
31eca76ba2fc98 Dan Williams 2016-04-28 1059 return -EFAULT;
92fe2aa859f52c Dan Williams 2020-07-20 1060
92fe2aa859f52c Dan Williams 2020-07-20 1061 if (nvdimm) {
92fe2aa859f52c Dan Williams 2020-07-20 1062 if (pkg.nd_family > NVDIMM_FAMILY_MAX)
92fe2aa859f52c Dan Williams 2020-07-20 1063 return -EINVAL;
92fe2aa859f52c Dan Williams 2020-07-20 1064 mask = &nd_desc->dimm_family_mask;
92fe2aa859f52c Dan Williams 2020-07-20 1065 } else {
92fe2aa859f52c Dan Williams 2020-07-20 1066 if (pkg.nd_family > NVDIMM_BUS_FAMILY_MAX)
92fe2aa859f52c Dan Williams 2020-07-20 1067 return -EINVAL;
92fe2aa859f52c Dan Williams 2020-07-20 1068 mask = &nd_desc->bus_family_mask;
92fe2aa859f52c Dan Williams 2020-07-20 1069 }
92fe2aa859f52c Dan Williams 2020-07-20 1070
92fe2aa859f52c Dan Williams 2020-07-20 1071 if (!test_bit(pkg.nd_family, mask))
92fe2aa859f52c Dan Williams 2020-07-20 1072 return -EINVAL;
31eca76ba2fc98 Dan Williams 2016-04-28 1073 }
31eca76ba2fc98 Dan Williams 2016-04-28 1074
f84afbdd3a9e5e Dan Carpenter 2020-02-25 1075 if (!desc ||
f84afbdd3a9e5e Dan Carpenter 2020-02-25 1076 (desc->out_num + desc->in_num == 0) ||
f84afbdd3a9e5e Dan Carpenter 2020-02-25 1077 cmd > ND_CMD_CALL ||
e3654eca70d637 Dan Williams 2016-04-28 1078 !test_bit(cmd, &cmd_mask))
62232e45f4a265 Dan Williams 2015-06-08 1079 return -ENOTTY;
62232e45f4a265 Dan Williams 2015-06-08 1080
62232e45f4a265 Dan Williams 2015-06-08 1081 /* fail write commands (when read-only) */
62232e45f4a265 Dan Williams 2015-06-08 1082 if (read_only)
07accfa9d1a8ba Jerry Hoemann 2016-01-06 1083 switch (cmd) {
07accfa9d1a8ba Jerry Hoemann 2016-01-06 1084 case ND_CMD_VENDOR:
07accfa9d1a8ba Jerry Hoemann 2016-01-06 1085 case ND_CMD_SET_CONFIG_DATA:
07accfa9d1a8ba Jerry Hoemann 2016-01-06 1086 case ND_CMD_ARS_START:
d4f323672aa637 Dan Williams 2016-03-03 1087 case ND_CMD_CLEAR_ERROR:
31eca76ba2fc98 Dan Williams 2016-04-28 1088 case ND_CMD_CALL:
ca6bf264f6d856 Dan Williams 2019-07-17 1089 dev_dbg(dev, "'%s' command while read-only.\n",
62232e45f4a265 Dan Williams 2015-06-08 1090 nvdimm ? nvdimm_cmd_name(cmd)
62232e45f4a265 Dan Williams 2015-06-08 1091 : nvdimm_bus_cmd_name(cmd));
62232e45f4a265 Dan Williams 2015-06-08 1092 return -EPERM;
62232e45f4a265 Dan Williams 2015-06-08 1093 default:
62232e45f4a265 Dan Williams 2015-06-08 1094 break;
62232e45f4a265 Dan Williams 2015-06-08 1095 }
62232e45f4a265 Dan Williams 2015-06-08 1096
62232e45f4a265 Dan Williams 2015-06-08 1097 /* process an input envelope */
6de5d06e657acd Dan Williams 2019-07-17 1098 in_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
6de5d06e657acd Dan Williams 2019-07-17 1099 if (!in_env)
6de5d06e657acd Dan Williams 2019-07-17 1100 return -ENOMEM;
62232e45f4a265 Dan Williams 2015-06-08 1101 for (i = 0; i < desc->in_num; i++) {
62232e45f4a265 Dan Williams 2015-06-08 1102 u32 in_size, copy;
62232e45f4a265 Dan Williams 2015-06-08 1103
62232e45f4a265 Dan Williams 2015-06-08 1104 in_size = nd_cmd_in_size(nvdimm, cmd, desc, i, in_env);
62232e45f4a265 Dan Williams 2015-06-08 1105 if (in_size == UINT_MAX) {
62232e45f4a265 Dan Williams 2015-06-08 1106 dev_err(dev, "%s:%s unknown input size cmd: %s field: %d\n",
62232e45f4a265 Dan Williams 2015-06-08 1107 __func__, dimm_name, cmd_name, i);
6de5d06e657acd Dan Williams 2019-07-17 1108 rc = -ENXIO;
6de5d06e657acd Dan Williams 2019-07-17 1109 goto out;
45def22c1fab85 Dan Williams 2015-04-26 1110 }
6de5d06e657acd Dan Williams 2019-07-17 1111 if (in_len < ND_CMD_MAX_ENVELOPE)
6de5d06e657acd Dan Williams 2019-07-17 1112 copy = min_t(u32, ND_CMD_MAX_ENVELOPE - in_len, in_size);
62232e45f4a265 Dan Williams 2015-06-08 1113 else
62232e45f4a265 Dan Williams 2015-06-08 1114 copy = 0;
6de5d06e657acd Dan Williams 2019-07-17 1115 if (copy && copy_from_user(&in_env[in_len], p + in_len, copy)) {
6de5d06e657acd Dan Williams 2019-07-17 1116 rc = -EFAULT;
6de5d06e657acd Dan Williams 2019-07-17 1117 goto out;
6de5d06e657acd Dan Williams 2019-07-17 1118 }
62232e45f4a265 Dan Williams 2015-06-08 1119 in_len += in_size;
62232e45f4a265 Dan Williams 2015-06-08 1120 }
62232e45f4a265 Dan Williams 2015-06-08 1121
31eca76ba2fc98 Dan Williams 2016-04-28 1122 if (cmd == ND_CMD_CALL) {
53b85a449b15e0 Jerry Hoemann 2017-06-30 1123 func = pkg.nd_command;
426824d63b77bd Dan Williams 2018-03-05 1124 dev_dbg(dev, "%s, idx: %llu, in: %u, out: %u, len %llu\n",
426824d63b77bd Dan Williams 2018-03-05 1125 dimm_name, pkg.nd_command,
31eca76ba2fc98 Dan Williams 2016-04-28 1126 in_len, out_len, buf_len);
31eca76ba2fc98 Dan Williams 2016-04-28 1127 }
31eca76ba2fc98 Dan Williams 2016-04-28 1128
62232e45f4a265 Dan Williams 2015-06-08 1129 /* process an output envelope */
6de5d06e657acd Dan Williams 2019-07-17 1130 out_env = kzalloc(ND_CMD_MAX_ENVELOPE, GFP_KERNEL);
6de5d06e657acd Dan Williams 2019-07-17 1131 if (!out_env) {
6de5d06e657acd Dan Williams 2019-07-17 1132 rc = -ENOMEM;
6de5d06e657acd Dan Williams 2019-07-17 1133 goto out;
6de5d06e657acd Dan Williams 2019-07-17 1134 }
6de5d06e657acd Dan Williams 2019-07-17 1135
62232e45f4a265 Dan Williams 2015-06-08 1136 for (i = 0; i < desc->out_num; i++) {
62232e45f4a265 Dan Williams 2015-06-08 1137 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i,
efda1b5d87cbc3 Dan Williams 2016-12-06 1138 (u32 *) in_env, (u32 *) out_env, 0);
62232e45f4a265 Dan Williams 2015-06-08 1139 u32 copy;
62232e45f4a265 Dan Williams 2015-06-08 1140
62232e45f4a265 Dan Williams 2015-06-08 1141 if (out_size == UINT_MAX) {
426824d63b77bd Dan Williams 2018-03-05 1142 dev_dbg(dev, "%s unknown output size cmd: %s field: %d\n",
426824d63b77bd Dan Williams 2018-03-05 1143 dimm_name, cmd_name, i);
6de5d06e657acd Dan Williams 2019-07-17 1144 rc = -EFAULT;
6de5d06e657acd Dan Williams 2019-07-17 1145 goto out;
62232e45f4a265 Dan Williams 2015-06-08 1146 }
6de5d06e657acd Dan Williams 2019-07-17 1147 if (out_len < ND_CMD_MAX_ENVELOPE)
6de5d06e657acd Dan Williams 2019-07-17 1148 copy = min_t(u32, ND_CMD_MAX_ENVELOPE - out_len, out_size);
62232e45f4a265 Dan Williams 2015-06-08 1149 else
62232e45f4a265 Dan Williams 2015-06-08 1150 copy = 0;
62232e45f4a265 Dan Williams 2015-06-08 1151 if (copy && copy_from_user(&out_env[out_len],
6de5d06e657acd Dan Williams 2019-07-17 1152 p + in_len + out_len, copy)) {
6de5d06e657acd Dan Williams 2019-07-17 1153 rc = -EFAULT;
6de5d06e657acd Dan Williams 2019-07-17 1154 goto out;
6de5d06e657acd Dan Williams 2019-07-17 1155 }
62232e45f4a265 Dan Williams 2015-06-08 1156 out_len += out_size;
62232e45f4a265 Dan Williams 2015-06-08 1157 }
62232e45f4a265 Dan Williams 2015-06-08 1158
58738c495e15ba Dan Williams 2017-08-31 1159 buf_len = (u64) out_len + (u64) in_len;
62232e45f4a265 Dan Williams 2015-06-08 1160 if (buf_len > ND_IOCTL_MAX_BUFLEN) {
426824d63b77bd Dan Williams 2018-03-05 1161 dev_dbg(dev, "%s cmd: %s buf_len: %llu > %d\n", dimm_name,
426824d63b77bd Dan Williams 2018-03-05 1162 cmd_name, buf_len, ND_IOCTL_MAX_BUFLEN);
6de5d06e657acd Dan Williams 2019-07-17 1163 rc = -EINVAL;
6de5d06e657acd Dan Williams 2019-07-17 1164 goto out;
62232e45f4a265 Dan Williams 2015-06-08 1165 }
62232e45f4a265 Dan Williams 2015-06-08 1166
62232e45f4a265 Dan Williams 2015-06-08 1167 buf = vmalloc(buf_len);
6de5d06e657acd Dan Williams 2019-07-17 1168 if (!buf) {
6de5d06e657acd Dan Williams 2019-07-17 1169 rc = -ENOMEM;
6de5d06e657acd Dan Williams 2019-07-17 1170 goto out;
6de5d06e657acd Dan Williams 2019-07-17 1171 }
62232e45f4a265 Dan Williams 2015-06-08 1172
62232e45f4a265 Dan Williams 2015-06-08 1173 if (copy_from_user(buf, p, buf_len)) {
62232e45f4a265 Dan Williams 2015-06-08 1174 rc = -EFAULT;
62232e45f4a265 Dan Williams 2015-06-08 @1175 goto out;
62232e45f4a265 Dan Williams 2015-06-08 1176 }
62232e45f4a265 Dan Williams 2015-06-08 1177
eb0e82bc5f69da Dave Jiang 2025-09-22 1178 guard(device)(dev);
eb0e82bc5f69da Dave Jiang 2025-09-22 1179 guard(nvdimm_bus)(dev);
53b85a449b15e0 Jerry Hoemann 2017-06-30 1180 rc = nd_cmd_clear_to_send(nvdimm_bus, nvdimm, func, buf);
eaf961536e1622 Dan Williams 2015-05-01 1181 if (rc)
eb0e82bc5f69da Dave Jiang 2025-09-22 1182 goto out;
eaf961536e1622 Dan Williams 2015-05-01 1183
006358b35c73ab Dave Jiang 2017-04-07 1184 rc = nd_desc->ndctl(nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);
62232e45f4a265 Dan Williams 2015-06-08 1185 if (rc < 0)
eb0e82bc5f69da Dave Jiang 2025-09-22 1186 goto out;
006358b35c73ab Dave Jiang 2017-04-07 1187
006358b35c73ab Dave Jiang 2017-04-07 1188 if (!nvdimm && cmd == ND_CMD_CLEAR_ERROR && cmd_rc >= 0) {
006358b35c73ab Dave Jiang 2017-04-07 1189 struct nd_cmd_clear_error *clear_err = buf;
006358b35c73ab Dave Jiang 2017-04-07 1190
23f4984483623c Dan Williams 2017-04-29 1191 nvdimm_account_cleared_poison(nvdimm_bus, clear_err->address,
006358b35c73ab Dave Jiang 2017-04-07 1192 clear_err->cleared);
006358b35c73ab Dave Jiang 2017-04-07 1193 }
0beb2012a17226 Dan Williams 2017-04-07 1194
62232e45f4a265 Dan Williams 2015-06-08 1195 if (copy_to_user(p, buf, buf_len))
62232e45f4a265 Dan Williams 2015-06-08 1196 rc = -EFAULT;
0beb2012a17226 Dan Williams 2017-04-07 1197
62232e45f4a265 Dan Williams 2015-06-08 1198 out:
6de5d06e657acd Dan Williams 2019-07-17 1199 kfree(in_env);
6de5d06e657acd Dan Williams 2019-07-17 1200 kfree(out_env);
62232e45f4a265 Dan Williams 2015-06-08 1201 vfree(buf);
62232e45f4a265 Dan Williams 2015-06-08 1202 return rc;
62232e45f4a265 Dan Williams 2015-06-08 1203 }
62232e45f4a265 Dan Williams 2015-06-08 1204 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 7 lines · On Mon, 22 Sep 2025 14:13:30 -0700 Dave Jiang <[email protected]> wrote:
Converting nvdimm_bus_lock/unlock to
guard()to clean up usage of gotos for error handling and avoid future mistakes of missed unlock on error paths.Suggested-by Jonathan Cameron <[email protected]>Signed-off-by Dave Jiang <[email protected]>
Hi Dave,
Thanks for looking at this.
Fully agree with Dan about the getting rid of all gotos by end of series.
A few other things inline. Mostly places where the use of guard() opens up low hanging fruit that improves readability (+ shortens code).
This code has a lot of dev_dbg() and some of them are so generic I'm not sure they are actually useful (cover a whole set of error paths). Perhaps it is worth splitting some of those up, or reducing the paths that trigger them as part of this refactor.
Jonathan
Quoted 5 lines
EXPORT_SYMBOL_GPL(nvdimm_badblocks_populate);drivers/nvdimm/btt_devs.c +0
-0
...
drivers/nvdimm/btt_devs.c static ssize_t namespace_show(struct device *dev, · line 98
Quoted 8 lines
drivers/nvdimm/btt_devs.c +1
-1@@ -95,10 +93,9 @@ static ssize_t namespace_show(struct device *dev,struct nd_btt *nd_btt =to_nd_btt (dev);ssize_t rc; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev); rc =sprintf (buf,"%s\n" , nd_btt->ndns ?dev_name (&nd_btt->ndns->dev) :"" );
return sprintf();
and drop the local variable.
Quoted 31 lines
drivers/nvdimm/btt_devs.c +0
-1-nvdimm_bus_unlock (dev);return rc; }drivers/nvdimm/bus.c +4
-4static int nvdimm_bus_probe (struct device *dev)@@ -1177,15 +1175,15 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, goto out; } -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_cmd_clear_to_send (nvdimm_bus, nvdimm, func, buf);if (rc) - goto out_unlock; + goto out; rc = nd_desc->ndctl (nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);if (rc <0 ) - goto out_unlock; + goto out;if (!nvdimm && cmd ==ND_CMD_CLEAR_ERROR && cmd_rc >=0 ) {struct nd_cmd_clear_error *clear_err = buf;
drivers/nvdimm/bus.c static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · line 1198
Quoted 7 lines
drivers/nvdimm/bus.c +0
-3@@ -1197,9 +1195,6 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,if (copy_to_user (p, buf, buf_len)) rc = -EFAULT ; -out_unlock: -nvdimm_bus_unlock (dev); -device_unlock (dev); out:
Hmm. I'm not a fan of gotos that rely on initializing a bunch of pointers to NULL so fewer labels are used. Will be nice to replace that as well via __free
Going to need a DEFINE_FREE for the vfree that follow these but that looks standard to me.
Quoted 7 lines
drivers/nvdimm/bus.c +0
-0kfree (in_env);kfree (out_env);drivers/nvdimm/claim.c +0
-0
drivers/nvdimm/claim.c void nd_detach_ndns(struct device *dev, · line 40
Quoted 9 lines
drivers/nvdimm/claim.c +2
-3@@ -35,9 +35,8 @@ void nd_detach_ndns(struct device *dev,if (!ndns)return ;get_device (&ndns->dev); -nvdimm_bus_lock (&ndns->dev); -__nd_detach_ndns (dev, _ndns); -nvdimm_bus_unlock (&ndns->dev); + scoped_guard(nvdimm_bus, &ndns->dev) +__nd_detach_ndns (dev, _ndns);put_device (&ndns->dev);
maybe a guard for this as well? Then you could just use guards for both rather than needing the scoped guard.
Quoted 10 lines
drivers/nvdimm/claim.c +0
-0}drivers/nvdimm/dax_devs.c +0
-0@@ -104,10 +104,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)return -ENODEV ; }
drivers/nvdimm/dax_devs.c int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns) · line 112
Quoted 10 lines
drivers/nvdimm/dax_devs.c +4
-4-nvdimm_bus_lock (&ndns->dev); - nd_dax =nd_dax_alloc (nd_region); - dax_dev =nd_dax_devinit (nd_dax, ndns); -nvdimm_bus_unlock (&ndns->dev); + scoped_guard(nvdimm_bus, &ndns->dev) { + nd_dax =nd_dax_alloc (nd_region); + dax_dev =nd_dax_devinit (nd_dax, ndns); + }if (!dax_dev)return -ENOMEM ;
Maybe move this check under the lock? For me that would be a little more obvious as right next to where it is set.
Quoted 12 lines
drivers/nvdimm/dax_devs.c +0
-0pfn_sb =devm_kmalloc (dev,sizeof (*pfn_sb),GFP_KERNEL );drivers/nvdimm/dimm.c +0
-3@@ -95,13 +95,13 @@ static int nvdimm_probe(struct device *dev)dev_dbg (dev,"config data size: %d\n" , ndd->nsarea.config_size); -nvdimm_bus_lock (dev); -if (ndd->ns_current >=0 ) { - rc =nd_label_reserve_dpa (ndd);
drivers/nvdimm/dimm.c static int nvdimm_probe(struct device *dev) · line 104
Quoted 10 lines
drivers/nvdimm/dimm.c +6
-3-if (rc ==0 ) -nvdimm_set_labeling (dev); + scoped_guard(nvdimm_bus, dev) { +if (ndd->ns_current >=0 ) { + rc =nd_label_reserve_dpa (ndd); +if (rc ==0 ) +nvdimm_set_labeling (dev); + } } -nvdimm_bus_unlock (dev);
This one looks awkward wrt to the goto and put_ndd(). Might not be worth the bother. I tend to take the view that it is fine to use guard() for a lock where it helps but not force it to be used universally if there are places where it doesn't.
Quoted 30 lines
drivers/nvdimm/dimm.c +2
-3if (rc) goto err;@@ -117,9 +117,8 @@ static void nvdimm_remove(struct device *dev) {struct nvdimm_drvdata *ndd =dev_get_drvdata (dev); -nvdimm_bus_lock (dev); -dev_set_drvdata (dev,NULL ); -nvdimm_bus_unlock (dev); + scoped_guard(nvdimm_bus, dev) +dev_set_drvdata (dev,NULL );put_ndd (ndd); }drivers/nvdimm/dimm_devs.c +1
-1@@ -326,7 +326,7 @@ static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf)return -ENXIO ; dev = ndd->dev; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev); nfree =nd_label_nfree (ndd);if (nfree -1 > nfree) {dev_WARN_ONCE (dev,1 ,"we ate our last label?\n" );
drivers/nvdimm/dimm_devs.c static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf) · line 337
Quoted 5 lines
drivers/nvdimm/dimm_devs.c +0
-1@@ -334,7 +334,6 @@ static ssize_t __available_slots_show(struct nvdimm_drvdata *ndd, char *buf) }else nfree--; rc =sprintf (buf,"%d\n" , nfree); -nvdimm_bus_unlock (dev);return rc;
return sprintf() nad drop then then unused rc.
drivers/nvdimm/dimm_devs.c static ssize_t security_store(struct device *dev, · line 400
Quoted 12 lines
drivers/nvdimm/dimm_devs.c +2
-2}@@ -395,12 +394,10 @@ static ssize_t security_store(struct device *dev,* done while probing is idle and the DIMM is not in active use * in any region. */ -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev);wait_nvdimm_bus_probe_idle (dev); rc =nvdimm_security_store (dev, buf, len);
return nvdimm_security_store()
drivers/nvdimm/dimm_devs.c static ssize_t result_show(struct device *dev, struct device_attribute *attr, ch · line 454
Quoted 13 lines
drivers/nvdimm/dimm_devs.c +1
-5-nvdimm_bus_unlock (dev); -device_unlock (dev);return rc; }@@ -454,9 +451,8 @@ static ssize_t result_show(struct device *dev, struct device_attribute *attr, chif (!nvdimm->fw_ops)return -EOPNOTSUPP ; -nvdimm_bus_lock (dev); - result = nvdimm->fw_ops->activate_result (nvdimm); -nvdimm_bus_unlock (dev); +scoped_guard (nvdimm_bus, dev)
Maybe just guard? Seems unlikely to matter if we do the prints under the lock.
drivers/nvdimm/dimm_devs.c static ssize_t activate_show(struct device *dev, struct device_attribute *attr, · line 482
Quoted 12 lines
drivers/nvdimm/dimm_devs.c +2
-3+ result = nvdimm->fw_ops->activate_result (nvdimm);switch (result) {case NVDIMM_FWA_RESULT_NONE :@@ -483,9 +479,8 @@ static ssize_t activate_show(struct device *dev, struct device_attribute *attr,if (!nvdimm->fw_ops)return -EOPNOTSUPP ; -nvdimm_bus_lock (dev); - state = nvdimm->fw_ops->activate_state (nvdimm); -nvdimm_bus_unlock (dev); +scoped_guard (nvdimm_bus, dev)
Similar to above. I'd just hold it to function exit to simplify the code a little.
Quoted 4 lines
drivers/nvdimm/dimm_devs.c +1
-0+ state = nvdimm->fw_ops->activate_state (nvdimm);switch (state) {case NVDIMM_FWA_IDLE :
..
drivers/nvdimm/dimm_devs.c void nvdimm_delete(struct nvdimm *nvdimm) · line 640
Quoted 13 lines
drivers/nvdimm/dimm_devs.c +4
-5@@ -641,11 +634,11 @@ void nvdimm_delete(struct nvdimm *nvdimm)bool dev_put = false;/* We are shutting down. Make state frozen artificially. */ -nvdimm_bus_lock (dev); -set_bit (NVDIMM_SECURITY_FROZEN , &nvdimm->sec.flags); -if (test_and_clear_bit (NDD_WORK_PENDING , &nvdimm->flags)) - dev_put = true; -nvdimm_bus_unlock (dev); + scoped_guard(nvdimm_bus, dev) { +set_bit (NVDIMM_SECURITY_FROZEN , &nvdimm->sec.flags); +if (test_and_clear_bit (NDD_WORK_PENDING , &nvdimm->flags)) + dev_put = true;
Not sure why this isn't
dev_put = test_and_clear_bit();
Maybe some earlier refactoring left this somewhat odd bit of code or
Quoted 8 lines
drivers/nvdimm/dimm_devs.c +1
-0+ }cancel_delayed_work_sync (&nvdimm->dwork);if (dev_put)put_device (dev);drivers/nvdimm/namespace_devs.c +0
-0
...
drivers/nvdimm/namespace_devs.c resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns) · line 894
Quoted 10 lines
drivers/nvdimm/namespace_devs.c +1
-2@@ -893,9 +888,8 @@ resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns) {resource_size_t size; -nvdimm_bus_lock (&ndns->dev); +guard (nvdimm_bus)(&ndns->dev); size =__nvdimm_namespace_capacity (ndns); -nvdimm_bus_unlock (&ndns->dev);return size;
return __nvdimm_namespace_capacity(ndns);
drivers/nvdimm/namespace_devs.c static ssize_t sector_size_store(struct device *dev, · line 1118
Quoted 13 lines
drivers/nvdimm/namespace_devs.c +2
-2}@@ -1119,8 +1111,8 @@ static ssize_t sector_size_store(struct device *dev, }else return -ENXIO ; -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev);if (to_ndns (dev)->claim) rc = -EBUSY ;if (rc >=0 )
There is a bit mess of if (rc >= 0)
stuff in here that could just become early exits and generally result I think in more readable code flow (enabled by the guard() usage) The side effect being the dev_dbg() might need to be replicated but it could ten say what actually happened rather than current case of one of 3 things failed.
if (to_ndns(dev)->claim) {
dev_dbg(dev...)
return -EBUSY;
}
rc = nd_size_select_store();
if (rc < 0) {
dev_dbg()
return rc;
}
rc = nd_namespace_label_update(nd_region, dev);
if (rc < 0) {
dev_dbg(); // if all these are actually useful given we know what we wrote and what failed.
return rc;
}drivers/nvdimm/namespace_devs.c static ssize_t dpa_extents_show(struct device *dev, · line 1159
Quoted 13 lines
drivers/nvdimm/namespace_devs.c +1
-1@@ -1145,7 +1135,7 @@ static ssize_t dpa_extents_show(struct device *dev,int count =0 , i;u32 flags =0 ; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev);if (is_namespace_pmem (dev)) {struct nd_namespace_pmem *nspm =to_nd_namespace_pmem (dev);@@ -1167,8 +1157,6 @@ static ssize_t dpa_extents_show(struct device *dev, count++; } out:
Can drop this and return early particularly as it's just
return sprintf(buf, "0\n");
in that one path.
drivers/nvdimm/namespace_devs.c static int init_active_labels(struct nd_region *nd_region) · line 2141
Quoted 11 lines
drivers/nvdimm/namespace_devs.c +1
-2-nvdimm_bus_unlock (dev); -return sprintf (buf,"%d\n" , count); }static DEVICE_ATTR_RO (dpa_extents);@@ -2152,31 +2138,38 @@ static int init_active_labels(struct nd_region *nd_region) nd_region); } +static int __create_namespaces(struct nd_region *nd_region, int *type,
naming is a little odd given it calls create_namespaces internally. Maybe
create_relevant_namespace() or something along those lines. Or rename the create_namespaces() to be PMEM specific and reuse that name for the wrapper.
Quoted 57 lines
drivers/nvdimm/namespace_devs.c +24
-18+ struct device ***devs) +{ +int rc; + +guard (nvdimm_bus)(&nd_region->dev); + rc =init_active_labels (nd_region); +if (rc) +return rc; + + *type =nd_region_to_nstype (nd_region); +switch (*type) { +case ND_DEVICE_NAMESPACE_IO : + *devs =create_namespace_io (nd_region); +break ; +case ND_DEVICE_NAMESPACE_PMEM : + *devs =create_namespaces (nd_region); +break ; + } + +return 0 ; +} +int nd_region_register_namespaces (struct nd_region *nd_region,int *err) {struct device **devs =NULL ;int i, rc =0 , type; *err =0 ; -nvdimm_bus_lock (&nd_region->dev); - rc =init_active_labels (nd_region); -if (rc) { -nvdimm_bus_unlock (&nd_region->dev); + rc =__create_namespaces (nd_region, &type, &devs); +if (rc)return rc; - } - - type =nd_region_to_nstype (nd_region); -switch (type) { -case ND_DEVICE_NAMESPACE_IO : - devs =create_namespace_io (nd_region); -break ; -case ND_DEVICE_NAMESPACE_PMEM : - devs =create_namespaces (nd_region); -break ; -default : -break ; - } -nvdimm_bus_unlock (&nd_region->dev);if (!devs)return -ENODEV ;drivers/nvdimm/nd.h +0
-0
drivers/nvdimm/nd.h u64 nd_region_interleave_set_cookie(struct nd_region *nd_region, · line 635
Quoted 4 lines
drivers/nvdimm/nd.h +1
-0@@ -632,6 +632,8 @@ u64 nd_region_interleave_set_cookie(struct nd_region *nd_region,u64 nd_region_interleave_set_altcookie (struct nd_region *nd_region);void nvdimm_bus_lock (struct device *dev);void nvdimm_bus_unlock (struct device *dev); +DEFINE_GUARD(nvdimm_bus, struct device *, nvdimm_bus_lock(_T), nvdimm_bus_unlock(_T));
You want that if (_T) or the IS_ERR_OR_NULL variant if appropriate. Technically not needed, but as Peter Z pointed out in a similar case, sometimes the compiler might spot that _T is always NULL in a path and optimize out the call. So he was very keen to keep that guard in the definition unless the the cleanup was also visible (As an inline in the header for instance).
Quoted 9 lines
drivers/nvdimm/nd.h +1
-0+bool is_nvdimm_bus_locked (struct device *dev);void nvdimm_check_and_set_ro (struct gendisk *disk);void nvdimm_drvdata_release (struct kref *kref);drivers/nvdimm/pfn_devs.c +0
-0
drivers/nvdimm/pfn_devs.c static ssize_t mode_store(struct device *dev, · line 63
Quoted 9 lines
drivers/nvdimm/pfn_devs.c +2
-2@@ -57,8 +57,8 @@ static ssize_t mode_store(struct device *dev,struct nd_pfn *nd_pfn =to_nd_pfn_safe (dev);ssize_t rc =0 ; -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev);if (dev->driver) rc = -EBUSY ;
Maybe just do an early return here. Is it really that useful to see anything other than -EBUSY? If so maybe have a new dev_dbg() for this path. Nice to reduce the indent on the rest.
Quoted 12 lines
drivers/nvdimm/pfn_devs.c +0
-2else {@@ -78,8 +78,6 @@ static ssize_t mode_store(struct device *dev, }dev_dbg (dev,"result: %zd wrote: %s%s" , rc, buf, buf[len -1 ] =='\n' ?"" :"\n" ); -nvdimm_bus_unlock (dev); -device_unlock (dev);return rc ? rc : len; }
drivers/nvdimm/pfn_devs.c static ssize_t namespace_show(struct device *dev, · line 172
Quoted 9 lines
drivers/nvdimm/pfn_devs.c +1
-2@@ -170,10 +166,9 @@ static ssize_t namespace_show(struct device *dev,struct nd_pfn *nd_pfn =to_nd_pfn_safe (dev);ssize_t rc; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev); rc =sprintf (buf,"%s\n" , nd_pfn->ndns ?dev_name (&nd_pfn->ndns->dev) :"" ); -nvdimm_bus_unlock (dev);return rc;
return sprintf()
Quoted 1 line
drivers/nvdimm/pfn_devs.c +0
-0}
...
Quoted 42 lines
drivers/nvdimm/region_devs.c +12
-4+int nd_region_activate (struct nd_region *nd_region) +{ +int i, j, rc, num_flush; +struct nd_region_data *ndrd; +struct device *dev = &nd_region->dev; +size_t flush_data_size; + + rc =get_flush_data (nd_region, &flush_data_size, &num_flush); +if (rc) +return rc; rc =nd_region_invalidate_memregion (nd_region);if (rc)@@ -327,8 +340,8 @@ static ssize_t set_cookie_show(struct device *dev,* the v1.1 namespace label cookie definition. To read all this * data we need to wait for probing to settle. */ -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev);wait_nvdimm_bus_probe_idle (dev);if (nd_region->ndr_mappings) {struct nd_mapping *nd_mapping = &nd_region->mapping[0 ];@@ -343,8 +356,6 @@ static ssize_t set_cookie_show(struct device *dev, nsindex)); } } -nvdimm_bus_unlock (dev); -device_unlock (dev);if (rc)return rc;@@ -401,12 +412,10 @@ static ssize_t available_size_show(struct device *dev,* memory nvdimm_bus_lock() is dropped, but that's userspace's * problem to not race itself. */
drivers/nvdimm/region_devs.c static ssize_t available_size_show(struct device *dev, · line 420
Quoted 10 lines
drivers/nvdimm/region_devs.c +2
-4-device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev);wait_nvdimm_bus_probe_idle (dev); available =nd_region_available_dpa (nd_region); -nvdimm_bus_unlock (dev); -device_unlock (dev);return sprintf (buf,"%llu\n" , available);
Could role this together without I think any real loss of readability.
return sprintf(buf, "%llu\n", nd_region_available_dpa(nd_region));
Quoted 5 lines
drivers/nvdimm/region_devs.c +0
-0}@@ -418,12 +427,10 @@ static ssize_t max_available_extent_show(struct device *dev,struct nd_region *nd_region =to_nd_region (dev);unsigned long long available =0 ;
drivers/nvdimm/region_devs.c static ssize_t max_available_extent_show(struct device *dev, · line 435
Quoted 10 lines
drivers/nvdimm/region_devs.c +2
-4-device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev);wait_nvdimm_bus_probe_idle (dev); available =nd_region_allocatable_dpa (nd_region); -nvdimm_bus_unlock (dev); -device_unlock (dev);return sprintf (buf,"%llu\n" , available);
Similarly.
Quoted 4 lines
drivers/nvdimm/region_devs.c +0
-0}@@ -435,12 +442,11 @@ static ssize_t init_namespaces_show(struct device *dev,struct nd_region_data *ndrd =dev_get_drvdata (dev);ssize_t rc;
drivers/nvdimm/region_devs.c static ssize_t init_namespaces_show(struct device *dev, · line 451
Quoted 10 lines
drivers/nvdimm/region_devs.c +1
-2-nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev);if (ndrd) rc =sprintf (buf,"%d/%d\n" , ndrd->ns_active, ndrd->ns_count);else rc = -ENXIO ; -nvdimm_bus_unlock (dev);return rc;
I'd do
if (!ndrd) return -ENXIO; return sprintf();
drivers/nvdimm/region_devs.c static ssize_t namespace_seed_show(struct device *dev, · line 466
Quoted 13 lines
drivers/nvdimm/region_devs.c +1
-2}@@ -452,12 +458,11 @@ static ssize_t namespace_seed_show(struct device *dev,struct nd_region *nd_region =to_nd_region (dev);ssize_t rc; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev);if (nd_region->ns_seed) rc =sprintf (buf,"%s\n" ,dev_name (nd_region->ns_seed));else rc =sprintf (buf,"\n" ); -nvdimm_bus_unlock (dev);return rc;
likewise, I'd just return directly in each of the legs
Quoted 5 lines
drivers/nvdimm/region_devs.c +0
-0}static DEVICE_ATTR_RO (namespace_seed);@@ -468,12 +473,11 @@ static ssize_t btt_seed_show(struct device *dev,struct nd_region *nd_region =to_nd_region (dev);ssize_t rc;
drivers/nvdimm/region_devs.c static ssize_t btt_seed_show(struct device *dev, · line 482
Quoted 10 lines
drivers/nvdimm/region_devs.c +1
-2-nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev);if (nd_region->btt_seed) rc =sprintf (buf,"%s\n" ,dev_name (nd_region->btt_seed));else rc =sprintf (buf,"\n" ); -nvdimm_bus_unlock (dev);return rc;
Here as well.
Quoted 4 lines
drivers/nvdimm/region_devs.c +0
-0}@@ -485,12 +489,11 @@ static ssize_t pfn_seed_show(struct device *dev,struct nd_region *nd_region =to_nd_region (dev);ssize_t rc;
drivers/nvdimm/region_devs.c static ssize_t pfn_seed_show(struct device *dev, · line 498
Quoted 10 lines
drivers/nvdimm/region_devs.c +1
-2-nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev);if (nd_region->pfn_seed) rc =sprintf (buf,"%s\n" ,dev_name (nd_region->pfn_seed));else rc =sprintf (buf,"\n" ); -nvdimm_bus_unlock (dev);return rc;
A common theme is emerging ;)
drivers/nvdimm/region_devs.c static ssize_t dax_seed_show(struct device *dev, · line 510
Quoted 12 lines
drivers/nvdimm/region_devs.c +1
-2}@@ -502,12 +505,11 @@ static ssize_t dax_seed_show(struct device *dev,struct nd_region *nd_region =to_nd_region (dev);ssize_t rc; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev);if (nd_region->dax_seed) rc =sprintf (buf,"%s\n" ,dev_name (nd_region->dax_seed));else rc =sprintf (buf,"\n" ); -nvdimm_bus_unlock (dev);
And again.
Quoted 10 lines
drivers/nvdimm/region_devs.c +0
-0return rc; }drivers/nvdimm/security.c +0
-0
drivers/nvdimm/security.c int nvdimm_security_unlock(struct device *dev) · line 226
Quoted 8 lines
drivers/nvdimm/security.c +1
-2@@ -221,9 +221,8 @@ int nvdimm_security_unlock(struct device *dev)struct nvdimm *nvdimm =to_nvdimm (dev);int rc; -nvdimm_bus_lock (dev); +guard (nvdimm_bus)(dev); rc =__nvdimm_security_unlock (nvdimm); -nvdimm_bus_unlock (dev);return rc;
return __nvdimm_se...
Quoted 2 lines
drivers/nvdimm/security.c +0
-0}
Dave Jiang
maintainerQuoted 7 lines · Jonathan Cameron
Quoted 4 lines · On Mon, 22 Sep 2025 14:13:30 -0700 Dave Jiang <[email protected]> wrote:
Converting nvdimm_bus_lock/unlock to
guard()to clean up usage of gotos for error handling and avoid future mistakes of missed unlock on error paths.
Quoted 8 lines
Quoted 3 lines
Suggested-by Jonathan Cameron <[email protected]>Signed-off-by Dave Jiang <[email protected]>Hi Dave,
Thanks for looking at this.
Fully agree with Dan about the getting rid of all gotos by end of series.
I'll do that in a different patch for that particular chunk.
Quoted 11 lines
A few other things inline. Mostly places where the use of
guard()opens up low hanging fruit that improves readability (+ shortens code).This code has a lot of
dev_dbg()and some of them are so generic I'm not sure they are actually useful (cover a whole set of error paths). Perhaps it is worth splitting some of those up, or reducing the paths that trigger them as part of this refactor.Quoted 1 line
Jonathan
<snip>
Quoted 30 lines
Quoted 30 lines
drivers/nvdimm/bus.c +4
-4static int nvdimm_bus_probe (struct device *dev)@@ -1177,15 +1175,15 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, goto out; } -device_lock (dev); -nvdimm_bus_lock (dev); +guard (device)(dev); +guard (nvdimm_bus)(dev); rc =nd_cmd_clear_to_send (nvdimm_bus, nvdimm, func, buf);if (rc) - goto out_unlock; + goto out; rc = nd_desc->ndctl (nd_desc, nvdimm, cmd, buf, buf_len, &cmd_rc);if (rc <0 ) - goto out_unlock; + goto out;if (!nvdimm && cmd ==ND_CMD_CLEAR_ERROR && cmd_rc >=0 ) {struct nd_cmd_clear_error *clear_err = buf;@@ -1197,9 +1195,6 @@ static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm,if (copy_to_user (p, buf, buf_len)) rc = -EFAULT ;
Quoted 8 lines
drivers/nvdimm/bus.c static int __nd_ioctl(struct nvdimm_bus *nvdimm_bus, struct nvdimm *nvdimm, · line 1198
Quoted 4 lines
drivers/nvdimm/bus.c +0
-3-out_unlock: -nvdimm_bus_unlock (dev); -device_unlock (dev); out:Hmm. I'm not a fan of gotos that rely on initializing a bunch of pointers to NULL so fewer labels are used. Will be nice to replace that as well via
__freeGoing to need a
DEFINE_FREEfor the vfree that follow these but that looks standard to me.
I ended up moving it to kvzalloc() instead. Seems reasonable and give us zeroing of the buffer as well.
DJ