From: srini@kernel.org
To: gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
Loic Poulain <loic.poulain@oss.qualcomm.com>,
Srinivas Kandagatla <srini@kernel.org>
Subject: [PATCH 04/14] nvmem: simplify locking with guard()
Date: Wed, 29 Jul 2026 10:46:37 +0100 [thread overview]
Message-ID: <20260729094647.111468-5-srini@kernel.org> (raw)
In-Reply-To: <20260729094647.111468-1-srini@kernel.org>
From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Use lock guards from cleanup.h to simplify locking. While at it: add the
missing mutex.h include.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Tested-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
Signed-off-by: Srinivas Kandagatla <srini@kernel.org>
---
drivers/nvmem/core.c | 88 +++++++++++++++++++-------------------------
1 file changed, 37 insertions(+), 51 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index a7e58bceb7ab..0a011b6b5837 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -6,6 +6,7 @@
* Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
*/
+#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/export.h>
#include <linux/fs.h>
@@ -13,6 +14,7 @@
#include <linux/init.h>
#include <linux/kref.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/nvmem-consumer.h>
#include <linux/nvmem-provider.h>
#include <linux/gpio/consumer.h>
@@ -468,27 +470,23 @@ static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
const struct bin_attribute **pattrs;
struct bin_attribute *attrs;
unsigned int ncells = 0, i = 0;
- int ret = 0;
+ int ret;
- mutex_lock(&nvmem_mutex);
+ guard(mutex)(&nvmem_mutex);
if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
- goto unlock_mutex;
+ return 0;
/* Allocate an array of attributes with a sentinel */
ncells = list_count_nodes(&nvmem->cells);
pattrs = devm_kcalloc(&nvmem->dev, ncells + 1,
sizeof(struct bin_attribute *), GFP_KERNEL);
- if (!pattrs) {
- ret = -ENOMEM;
- goto unlock_mutex;
- }
+ if (!pattrs)
+ return -ENOMEM;
attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
- if (!attrs) {
- ret = -ENOMEM;
- goto unlock_mutex;
- }
+ if (!attrs)
+ return -ENOMEM;
/* Initialize each attribute to take the name and size of the cell */
list_for_each_entry(entry, &nvmem->cells, node) {
@@ -501,10 +499,8 @@ static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
attrs[i].size = entry->bytes;
attrs[i].read = &nvmem_cell_attr_read;
attrs[i].private = entry;
- if (!attrs[i].attr.name) {
- ret = -ENOMEM;
- goto unlock_mutex;
- }
+ if (!attrs[i].attr.name)
+ return -ENOMEM;
pattrs[i] = &attrs[i];
i++;
@@ -514,13 +510,10 @@ static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
ret = device_add_group(&nvmem->dev, &group);
if (ret)
- goto unlock_mutex;
+ return ret;
nvmem->sysfs_cells_populated = true;
-unlock_mutex:
- mutex_unlock(&nvmem_mutex);
-
return ret;
}
@@ -558,9 +551,8 @@ static const struct bus_type nvmem_bus_type = {
static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
{
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
- mutex_lock(&nvmem_mutex);
- list_del(&cell->node);
- mutex_unlock(&nvmem_mutex);
+ scoped_guard(mutex, &nvmem_mutex)
+ list_del(&cell->node);
of_node_put(cell->np);
kfree_const(cell->name);
kfree(cell);
@@ -576,9 +568,8 @@ static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
{
- mutex_lock(&nvmem_mutex);
- list_add_tail(&cell->node, &cell->nvmem->cells);
- mutex_unlock(&nvmem_mutex);
+ scoped_guard(mutex, &nvmem_mutex)
+ list_add_tail(&cell->node, &cell->nvmem->cells);
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
}
@@ -728,14 +719,14 @@ nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
{
struct nvmem_cell_entry *iter, *cell = NULL;
- mutex_lock(&nvmem_mutex);
+ guard(mutex)(&nvmem_mutex);
+
list_for_each_entry(iter, &nvmem->cells, node) {
if (strcmp(cell_id, iter->name) == 0) {
cell = iter;
break;
}
}
- mutex_unlock(&nvmem_mutex);
return cell;
}
@@ -1105,11 +1096,11 @@ static struct nvmem_device *__nvmem_device_get(void *data,
struct nvmem_device *nvmem = NULL;
struct device *dev;
- mutex_lock(&nvmem_mutex);
- dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
- if (dev)
- nvmem = to_nvmem_device(dev);
- mutex_unlock(&nvmem_mutex);
+ scoped_guard(mutex, &nvmem_mutex) {
+ dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
+ if (dev)
+ nvmem = to_nvmem_device(dev);
+ }
if (!nvmem)
return ERR_PTR(-EPROBE_DEFER);
@@ -1319,7 +1310,7 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
dev_id = dev_name(dev);
- mutex_lock(&nvmem_lookup_mutex);
+ guard(mutex)(&nvmem_lookup_mutex);
list_for_each_entry(lookup, &nvmem_lookup_list, node) {
if ((strcmp(lookup->dev_id, dev_id) == 0) &&
@@ -1327,11 +1318,9 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
/* This is the right entry. */
nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
device_match_name);
- if (IS_ERR(nvmem)) {
+ if (IS_ERR(nvmem))
/* Provider may not be registered yet. */
- cell = ERR_CAST(nvmem);
- break;
- }
+ return ERR_CAST(nvmem);
cell_entry = nvmem_find_cell_entry_by_name(nvmem,
lookup->cell_name);
@@ -1347,7 +1336,6 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
}
}
- mutex_unlock(&nvmem_lookup_mutex);
return cell;
}
@@ -1361,18 +1349,16 @@ static void nvmem_layout_module_put(struct nvmem_device *nvmem)
static struct nvmem_cell_entry *
nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
{
- struct nvmem_cell_entry *iter, *cell = NULL;
+ struct nvmem_cell_entry *cell;
- mutex_lock(&nvmem_mutex);
- list_for_each_entry(iter, &nvmem->cells, node) {
- if (np == iter->np) {
- cell = iter;
- break;
- }
+ guard(mutex)(&nvmem_mutex);
+
+ list_for_each_entry(cell, &nvmem->cells, node) {
+ if (np == cell->np)
+ return cell;
}
- mutex_unlock(&nvmem_mutex);
- return cell;
+ return NULL;
}
static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
@@ -2105,10 +2091,10 @@ void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
{
int i;
- mutex_lock(&nvmem_lookup_mutex);
+ guard(mutex)(&nvmem_lookup_mutex);
+
for (i = 0; i < nentries; i++)
list_add_tail(&entries[i].node, &nvmem_lookup_list);
- mutex_unlock(&nvmem_lookup_mutex);
}
EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
@@ -2123,10 +2109,10 @@ void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
{
int i;
- mutex_lock(&nvmem_lookup_mutex);
+ guard(mutex)(&nvmem_lookup_mutex);
+
for (i = 0; i < nentries; i++)
list_del(&entries[i].node);
- mutex_unlock(&nvmem_lookup_mutex);
}
EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
--
2.53.0
next prev parent reply other threads:[~2026-07-29 9:46 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-29 9:46 [PATCH 00/14] nvmem: updates for 7.3 srini
2026-07-29 9:46 ` [PATCH 01/14] nvmem: remove unused field from struct nvmem_device srini
2026-07-29 9:46 ` [PATCH 02/14] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks srini
2026-07-29 9:46 ` [PATCH 03/14] nvmem: check the return value of gpiod_set_value_cansleep() srini
2026-07-29 9:46 ` srini [this message]
2026-07-29 9:46 ` [PATCH 05/14] nvmem: remove unneeded __nvmem_device_put() srini
2026-07-29 9:46 ` [PATCH 06/14] nvmem: split out the reg_read/write() callbacks out of struct nvmem_device srini
2026-07-29 9:46 ` [PATCH 07/14] nvmem: simplify nvmem_sysfs_remove_compat() srini
2026-07-29 9:46 ` [PATCH 08/14] nvmem: remove duplicated reference counting srini
2026-07-29 9:46 ` [PATCH 09/14] nvmem: protect nvmem_device::ops with SRCU srini
2026-07-29 9:46 ` [PATCH 10/14] eeprom: move nvmem EEPROM drivers to drivers/nvmem/ srini
2026-07-29 9:46 ` [PATCH 11/14] nvmem: airoha: add ARM64 dependency srini
2026-07-29 9:46 ` [PATCH 12/14] nvmem: brcm_nvram: reject empty NVRAM partition srini
2026-07-29 9:46 ` [PATCH 13/14] nvmem: brcm_nvram: fix out-of-bounds access on malformed flash data srini
2026-07-29 9:46 ` [PATCH 14/14] dt-bindings: nvmem: qfprom: Add ipq5210 & ipq9650 compatible srini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260729094647.111468-5-srini@kernel.org \
--to=srini@kernel.org \
--cc=bartosz.golaszewski@oss.qualcomm.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=loic.poulain@oss.qualcomm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.