* [PATCH v3 1/9] nvmem: remove unused field from struct nvmem_device
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 2/9] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks Bartosz Golaszewski
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
The node list_head in struct nvmem_device was added accidentally by
commit ec9c08a1cb8d ("nvmem: Create a header for internal sharing") and
is unused so remove it.
Fixes: ec9c08a1cb8d ("nvmem: Create a header for internal sharing")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/internals.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 18fed57270e5e3391cf24d5e49836121a53a8cd6..7cbc55f40259fc4315c41979ad8bf75c36bcb056 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -10,7 +10,6 @@
struct nvmem_device {
struct module *owner;
struct device dev;
- struct list_head node;
int stride;
int word_size;
int id;
--
2.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v3 2/9] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 1/9] nvmem: remove unused field from struct nvmem_device Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 3/9] nvmem: check the return value of gpiod_set_value_cansleep() Bartosz Golaszewski
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
__nvmem_reg_read/write() currently return -EINVAL if the relevant
callback is not present. User-space helpers again check the presence
of the callbacks to see if they should return -EPERM.
Ahead of adding SRCU synchronization: change the error code returned to
in-kernel users to -EOPNOTSUPP which is more indicative of the actual
reason for the failure. Move these checks into the higher-level
nvmem_reg_read/write() functions to avoid calling into
nvmem_access_with_keepouts() unnecessarily.
Remove the checks from the sysfs attribute callbacks as these are not
visible without the required callbacks.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 27 +++++++++++----------------
1 file changed, 11 insertions(+), 16 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 311cb2e5a5c02d2c6979d7c9bbb7f94abdfbdad1..c18c7bb94a13b20ad27ec452a2b9fe80c2a4f843 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -55,10 +55,10 @@ static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- if (nvmem->reg_read)
- return nvmem->reg_read(nvmem->priv, offset, val, bytes);
+ if (!nvmem->reg_read)
+ return -EOPNOTSUPP;
- return -EINVAL;
+ return nvmem->reg_read(nvmem->priv, offset, val, bytes);
}
static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
@@ -66,14 +66,14 @@ static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
{
int ret;
- if (nvmem->reg_write) {
- gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
- ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
- gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
- return ret;
- }
+ if (!nvmem->reg_write)
+ return -EOPNOTSUPP;
+
+ gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
+ ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
+ gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
- return -EINVAL;
+ return ret;
}
static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
@@ -231,11 +231,7 @@ static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
count = round_down(count, nvmem->word_size);
- if (!nvmem->reg_read)
- return -EPERM;
-
rc = nvmem_reg_read(nvmem, pos, buf, count);
-
if (rc)
return rc;
@@ -264,11 +260,10 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
count = round_down(count, nvmem->word_size);
- if (!nvmem->reg_write || nvmem->read_only)
+ if (nvmem->read_only)
return -EPERM;
rc = nvmem_reg_write(nvmem, pos, buf, count);
-
if (rc)
return rc;
--
2.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v3 3/9] nvmem: check the return value of gpiod_set_value_cansleep()
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 1/9] nvmem: remove unused field from struct nvmem_device Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 2/9] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 4/9] nvmem: simplify locking with guard() Bartosz Golaszewski
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
GPIO setters now return integer values and can indicate failures in
lower abstraction layers. Check the return values of
gpiod_set_value_cansleep() calls in nvmem core.
Reviewed-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index c18c7bb94a13b20ad27ec452a2b9fe80c2a4f843..61f7484f96f3a89450686745c1efad8fc24c667e 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -64,16 +64,22 @@ static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- int ret;
+ int ret, written;
if (!nvmem->reg_write)
return -EOPNOTSUPP;
- gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
- ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
- gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
+ ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
+ if (ret)
+ return ret;
- return ret;
+ written = nvmem->reg_write(nvmem->priv, offset, val, bytes);
+
+ ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
+ if (ret)
+ return ret;
+
+ return written;
}
static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
--
2.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v3 4/9] nvmem: simplify locking with guard()
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
` (2 preceding siblings ...)
2026-04-29 15:43 ` [PATCH v3 3/9] nvmem: check the return value of gpiod_set_value_cansleep() Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 5/9] nvmem: remove unneeded __nvmem_device_put() Bartosz Golaszewski
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
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>
---
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 61f7484f96f3a89450686745c1efad8fc24c667e..d6c62b3a527d3412a990ca143bee3209ff28d18d 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>
@@ -462,27 +464,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) {
@@ -495,10 +493,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++;
@@ -508,13 +504,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;
}
@@ -552,9 +545,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);
@@ -570,9 +562,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);
}
@@ -722,14 +713,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;
}
@@ -1115,11 +1106,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);
@@ -1329,7 +1320,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) &&
@@ -1337,11 +1328,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);
@@ -1357,7 +1346,6 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
}
}
- mutex_unlock(&nvmem_lookup_mutex);
return cell;
}
@@ -1371,18 +1359,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)
@@ -2117,10 +2103,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);
@@ -2135,10 +2121,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.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v3 5/9] nvmem: remove unneeded __nvmem_device_put()
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
` (3 preceding siblings ...)
2026-04-29 15:43 ` [PATCH v3 4/9] nvmem: simplify locking with guard() Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 6/9] nvmem: split out the reg_read/write() callbacks out of struct nvmem_device Bartosz Golaszewski
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
__nvmem_device_put() is wrapped by nvmem_device_put() but there's no
extra functionality offered by the latter so just fold one into the
other. There's still the corresponding __nvmem_device_get() so in order
to keep things symmetrical: rename it to nvmem_device_match() to better
reflect its functionality and not confuse users by its presence in the
absence of the similarly prefixed put() counterpart.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 35 +++++++++++++++--------------------
1 file changed, 15 insertions(+), 20 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index d6c62b3a527d3412a990ca143bee3209ff28d18d..bca2cce793451bb46ede9e672dec6ec20f369fd8 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1100,7 +1100,7 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
}
EXPORT_SYMBOL_GPL(devm_nvmem_register);
-static struct nvmem_device *__nvmem_device_get(void *data,
+static struct nvmem_device *nvmem_device_match(void *data,
int (*match)(struct device *dev, const void *data))
{
struct nvmem_device *nvmem = NULL;
@@ -1128,13 +1128,6 @@ static struct nvmem_device *__nvmem_device_get(void *data,
return nvmem;
}
-static void __nvmem_device_put(struct nvmem_device *nvmem)
-{
- put_device(&nvmem->dev);
- module_put(nvmem->owner);
- kref_put(&nvmem->refcnt, nvmem_device_release);
-}
-
#if IS_ENABLED(CONFIG_OF)
/**
* of_nvmem_device_get() - Get nvmem device from a given id
@@ -1159,7 +1152,7 @@ struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
if (!nvmem_np)
return ERR_PTR(-ENOENT);
- nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
+ nvmem = nvmem_device_match(nvmem_np, device_match_of_node);
of_node_put(nvmem_np);
return nvmem;
}
@@ -1187,7 +1180,7 @@ struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
}
- return __nvmem_device_get((void *)dev_name, device_match_name);
+ return nvmem_device_match((void *)dev_name, device_match_name);
}
EXPORT_SYMBOL_GPL(nvmem_device_get);
@@ -1203,7 +1196,7 @@ EXPORT_SYMBOL_GPL(nvmem_device_get);
struct nvmem_device *nvmem_device_find(void *data,
int (*match)(struct device *dev, const void *data))
{
- return __nvmem_device_get(data, match);
+ return nvmem_device_match(data, match);
}
EXPORT_SYMBOL_GPL(nvmem_device_find);
@@ -1247,7 +1240,9 @@ EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
*/
void nvmem_device_put(struct nvmem_device *nvmem)
{
- __nvmem_device_put(nvmem);
+ put_device(&nvmem->dev);
+ module_put(nvmem->owner);
+ kref_put(&nvmem->refcnt, nvmem_device_release);
}
EXPORT_SYMBOL_GPL(nvmem_device_put);
@@ -1326,7 +1321,7 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
if ((strcmp(lookup->dev_id, dev_id) == 0) &&
(strcmp(lookup->con_id, con_id) == 0)) {
/* This is the right entry. */
- nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
+ nvmem = nvmem_device_match((void *)lookup->nvmem_name,
device_match_name);
if (IS_ERR(nvmem))
/* Provider may not be registered yet. */
@@ -1335,12 +1330,12 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
cell_entry = nvmem_find_cell_entry_by_name(nvmem,
lookup->cell_name);
if (!cell_entry) {
- __nvmem_device_put(nvmem);
+ nvmem_device_put(nvmem);
cell = ERR_PTR(-ENOENT);
} else {
cell = nvmem_create_cell(cell_entry, con_id, 0);
if (IS_ERR(cell))
- __nvmem_device_put(nvmem);
+ nvmem_device_put(nvmem);
}
break;
}
@@ -1438,7 +1433,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
}
}
- nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
+ nvmem = nvmem_device_match(nvmem_np, device_match_of_node);
of_node_put(nvmem_np);
if (IS_ERR(nvmem)) {
of_node_put(cell_np);
@@ -1448,14 +1443,14 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
ret = nvmem_layout_module_get_optional(nvmem);
if (ret) {
of_node_put(cell_np);
- __nvmem_device_put(nvmem);
+ nvmem_device_put(nvmem);
return ERR_PTR(ret);
}
cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
of_node_put(cell_np);
if (!cell_entry) {
- __nvmem_device_put(nvmem);
+ nvmem_device_put(nvmem);
nvmem_layout_module_put(nvmem);
if (nvmem->layout)
return ERR_PTR(-EPROBE_DEFER);
@@ -1465,7 +1460,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
cell = nvmem_create_cell(cell_entry, id, cell_index);
if (IS_ERR(cell)) {
- __nvmem_device_put(nvmem);
+ nvmem_device_put(nvmem);
nvmem_layout_module_put(nvmem);
}
@@ -1580,7 +1575,7 @@ void nvmem_cell_put(struct nvmem_cell *cell)
kfree_const(cell->id);
kfree(cell);
- __nvmem_device_put(nvmem);
+ nvmem_device_put(nvmem);
nvmem_layout_module_put(nvmem);
}
EXPORT_SYMBOL_GPL(nvmem_cell_put);
--
2.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v3 6/9] nvmem: split out the reg_read/write() callbacks out of struct nvmem_device
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
` (4 preceding siblings ...)
2026-04-29 15:43 ` [PATCH v3 5/9] nvmem: remove unneeded __nvmem_device_put() Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 7/9] nvmem: simplify nvmem_sysfs_remove_compat() Bartosz Golaszewski
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
The reg_read/write() fields of struct nvmem_device point to memory owned
by the nvmem provider. They must not be dereferenced after the provider
is unregistered. Ahead of protecting against accesses to invalid memory
with SRCU, move the callbacks into a separate structure the address of
which is stored in nvmem_device.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 37 +++++++++++++++++++++++++++----------
drivers/nvmem/internals.h | 9 +++++++--
2 files changed, 34 insertions(+), 12 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index bca2cce793451bb46ede9e672dec6ec20f369fd8..83ef4858939c471daabd4641f9746d92b43daf5d 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -57,25 +57,28 @@ static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- if (!nvmem->reg_read)
+ struct nvmem_operations *ops = nvmem->ops;
+
+ if (!ops->reg_read)
return -EOPNOTSUPP;
- return nvmem->reg_read(nvmem->priv, offset, val, bytes);
+ return ops->reg_read(nvmem->priv, offset, val, bytes);
}
static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
+ struct nvmem_operations *ops = nvmem->ops;
int ret, written;
- if (!nvmem->reg_write)
+ if (!ops->reg_write)
return -EOPNOTSUPP;
ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
if (ret)
return ret;
- written = nvmem->reg_write(nvmem->priv, offset, val, bytes);
+ written = ops->reg_write(nvmem->priv, offset, val, bytes);
ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
if (ret)
@@ -280,6 +283,8 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
{
+ struct nvmem_operations *ops = nvmem->ops;
+
umode_t mode = 0400;
if (!nvmem->root_only)
@@ -288,10 +293,10 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
if (!nvmem->read_only)
mode |= 0200;
- if (!nvmem->reg_write)
+ if (!ops->reg_write)
mode &= ~0200;
- if (!nvmem->reg_read)
+ if (!ops->reg_read)
mode &= ~0444;
return mode;
@@ -322,6 +327,7 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj,
{
struct device *dev = kobj_to_dev(kobj);
struct nvmem_device *nvmem = to_nvmem_device(dev);
+ struct nvmem_operations *ops = nvmem->ops;
/*
* If the device has no .reg_write operation, do not allow
@@ -330,7 +336,7 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj,
* can be forced into read-write mode using the 'force_ro'
* attribute.
*/
- if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
+ if (attr == &dev_attr_force_ro.attr && !ops->reg_write)
return 0; /* Attribute not visible */
return attr->mode;
@@ -531,6 +537,7 @@ static void nvmem_release(struct device *dev)
ida_free(&nvmem_ida, nvmem->id);
gpiod_put(nvmem->wp_gpio);
+ kfree(nvmem->ops);
kfree(nvmem);
}
@@ -891,6 +898,7 @@ EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
struct nvmem_device *nvmem_register(const struct nvmem_config *config)
{
+ struct nvmem_operations *ops;
struct nvmem_device *nvmem;
int rval;
@@ -904,8 +912,15 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
if (!nvmem)
return ERR_PTR(-ENOMEM);
+ ops = kzalloc_obj(*ops);
+ if (!ops) {
+ kfree(nvmem);
+ return ERR_PTR(-ENOMEM);
+ }
+
rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
if (rval < 0) {
+ kfree(ops);
kfree(nvmem);
return ERR_PTR(rval);
}
@@ -931,6 +946,10 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
INIT_LIST_HEAD(&nvmem->cells);
nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
+ ops->reg_read = config->reg_read;
+ ops->reg_write = config->reg_write;
+
+ nvmem->ops = ops;
nvmem->owner = config->owner;
if (!nvmem->owner && config->dev->driver)
nvmem->owner = config->dev->driver->owner;
@@ -940,8 +959,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->root_only = config->root_only;
nvmem->priv = config->priv;
nvmem->type = config->type;
- nvmem->reg_read = config->reg_read;
- nvmem->reg_write = config->reg_write;
nvmem->keepout = config->keepout;
nvmem->nkeepout = config->nkeepout;
if (config->of_node)
@@ -967,7 +984,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
goto err_put_device;
nvmem->read_only = device_property_present(config->dev, "read-only") ||
- config->read_only || !nvmem->reg_write;
+ config->read_only || !ops->reg_write;
#ifdef CONFIG_NVMEM_SYSFS
nvmem->dev.groups = nvmem_dev_groups;
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 7cbc55f40259fc4315c41979ad8bf75c36bcb056..070eef89ffca78fe033aaaada0f91c257fe2f166 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -7,6 +7,12 @@
#include <linux/nvmem-consumer.h>
#include <linux/nvmem-provider.h>
+/* Hold pointers to callbacks owned by the nvmem provider module. */
+struct nvmem_operations {
+ nvmem_reg_read_t reg_read;
+ nvmem_reg_write_t reg_write;
+};
+
struct nvmem_device {
struct module *owner;
struct device dev;
@@ -26,10 +32,9 @@ struct nvmem_device {
struct nvmem_cell_info *cell);
const struct nvmem_keepout *keepout;
unsigned int nkeepout;
- nvmem_reg_read_t reg_read;
- nvmem_reg_write_t reg_write;
struct gpio_desc *wp_gpio;
struct nvmem_layout *layout;
+ struct nvmem_operations *ops;
void *priv;
bool sysfs_cells_populated;
};
--
2.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v3 7/9] nvmem: simplify nvmem_sysfs_remove_compat()
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
` (5 preceding siblings ...)
2026-04-29 15:43 ` [PATCH v3 6/9] nvmem: split out the reg_read/write() callbacks out of struct nvmem_device Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 8/9] nvmem: remove duplicated reference counting Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 9/9] nvmem: protect nvmem_device::ops with SRCU Bartosz Golaszewski
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
There's no need for the config argument in nvmem_sysfs_remove_compat().
Once the compat attribute is registered, that information is carried in
nvmem_device::flags. Rework the code to always query that field and drop
the second argument.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 83ef4858939c471daabd4641f9746d92b43daf5d..32fe048a4a9152b7a5ec465ae5bbf04e30319554 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -454,10 +454,9 @@ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
return 0;
}
-static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
- const struct nvmem_config *config)
+static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
{
- if (config->compat)
+ if (nvmem->flags & FLAG_COMPAT)
device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
}
@@ -524,8 +523,7 @@ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
{
return -ENOSYS;
}
-static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
- const struct nvmem_config *config)
+static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
{
}
@@ -1046,8 +1044,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
device_del(&nvmem->dev);
err_remove_cells:
nvmem_device_remove_all_cells(nvmem);
- if (config->compat)
- nvmem_sysfs_remove_compat(nvmem, config);
+ nvmem_sysfs_remove_compat(nvmem);
err_put_device:
put_device(&nvmem->dev);
@@ -1063,8 +1060,7 @@ static void nvmem_device_release(struct kref *kref)
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
- if (nvmem->flags & FLAG_COMPAT)
- device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
+ nvmem_sysfs_remove_compat(nvmem);
nvmem_device_remove_all_cells(nvmem);
nvmem_destroy_layout(nvmem);
--
2.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v3 8/9] nvmem: remove duplicated reference counting
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
` (6 preceding siblings ...)
2026-04-29 15:43 ` [PATCH v3 7/9] nvmem: simplify nvmem_sysfs_remove_compat() Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 9/9] nvmem: protect nvmem_device::ops with SRCU Bartosz Golaszewski
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
Commit c1de7f43bd84 ("nvmem: use kref") introduced reference counting
with kref to an already reference counted nvmem_device structure. We
only need one refcount so use the one provded by device's kobject and
drop the kref field from struct nvmem_device.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 77 +++++++++++++++++++----------------------------
drivers/nvmem/internals.h | 1 -
2 files changed, 31 insertions(+), 47 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 32fe048a4a9152b7a5ec465ae5bbf04e30319554..590d880ede944670c09dd62b394626a959e5cdd1 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -529,24 +529,6 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
#endif /* CONFIG_NVMEM_SYSFS */
-static void nvmem_release(struct device *dev)
-{
- struct nvmem_device *nvmem = to_nvmem_device(dev);
-
- ida_free(&nvmem_ida, nvmem->id);
- gpiod_put(nvmem->wp_gpio);
- kfree(nvmem->ops);
- kfree(nvmem);
-}
-
-static const struct device_type nvmem_provider_type = {
- .release = nvmem_release,
-};
-
-static const struct bus_type nvmem_bus_type = {
- .name = "nvmem",
-};
-
static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
{
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
@@ -565,6 +547,25 @@ static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
nvmem_cell_entry_drop(cell);
}
+static void nvmem_release(struct device *dev)
+{
+ struct nvmem_device *nvmem = to_nvmem_device(dev);
+
+ gpiod_put(nvmem->wp_gpio);
+ nvmem_device_remove_all_cells(nvmem);
+ ida_free(&nvmem_ida, nvmem->id);
+ kfree(nvmem->ops);
+ kfree(nvmem);
+}
+
+static const struct device_type nvmem_provider_type = {
+ .release = nvmem_release,
+};
+
+static const struct bus_type nvmem_bus_type = {
+ .name = "nvmem",
+};
+
static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
{
scoped_guard(mutex, &nvmem_mutex)
@@ -940,7 +941,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
goto err_put_device;
}
- kref_init(&nvmem->refcnt);
INIT_LIST_HEAD(&nvmem->cells);
nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
@@ -1003,24 +1003,24 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
if (config->cells) {
rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
if (rval)
- goto err_remove_cells;
+ goto err_remove_compat;
}
if (config->add_legacy_fixed_of_cells) {
rval = nvmem_add_cells_from_legacy_of(nvmem);
if (rval)
- goto err_remove_cells;
+ goto err_remove_compat;
}
rval = nvmem_add_cells_from_fixed_layout(nvmem);
if (rval)
- goto err_remove_cells;
+ goto err_remove_compat;
dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
rval = device_add(&nvmem->dev);
if (rval)
- goto err_remove_cells;
+ goto err_remove_compat;
rval = nvmem_populate_layout(nvmem);
if (rval)
@@ -1042,8 +1042,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
#endif
err_remove_dev:
device_del(&nvmem->dev);
-err_remove_cells:
- nvmem_device_remove_all_cells(nvmem);
+err_remove_compat:
nvmem_sysfs_remove_compat(nvmem);
err_put_device:
put_device(&nvmem->dev);
@@ -1052,21 +1051,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
}
EXPORT_SYMBOL_GPL(nvmem_register);
-static void nvmem_device_release(struct kref *kref)
-{
- struct nvmem_device *nvmem;
-
- nvmem = container_of(kref, struct nvmem_device, refcnt);
-
- blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
-
- nvmem_sysfs_remove_compat(nvmem);
-
- nvmem_device_remove_all_cells(nvmem);
- nvmem_destroy_layout(nvmem);
- device_unregister(&nvmem->dev);
-}
-
/**
* nvmem_unregister() - Unregister previously registered nvmem device
*
@@ -1074,8 +1058,12 @@ static void nvmem_device_release(struct kref *kref)
*/
void nvmem_unregister(struct nvmem_device *nvmem)
{
- if (nvmem)
- kref_put(&nvmem->refcnt, nvmem_device_release);
+ blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
+
+ nvmem_sysfs_remove_compat(nvmem);
+ nvmem_destroy_layout(nvmem);
+
+ device_unregister(&nvmem->dev);
}
EXPORT_SYMBOL_GPL(nvmem_unregister);
@@ -1136,8 +1124,6 @@ static struct nvmem_device *nvmem_device_match(void *data,
return ERR_PTR(-EINVAL);
}
- kref_get(&nvmem->refcnt);
-
return nvmem;
}
@@ -1253,9 +1239,8 @@ EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
*/
void nvmem_device_put(struct nvmem_device *nvmem)
{
- put_device(&nvmem->dev);
module_put(nvmem->owner);
- kref_put(&nvmem->refcnt, nvmem_device_release);
+ put_device(&nvmem->dev);
}
EXPORT_SYMBOL_GPL(nvmem_device_put);
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 070eef89ffca78fe033aaaada0f91c257fe2f166..60daa79c404dc915872481bd3b02de2258d33406 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -19,7 +19,6 @@ struct nvmem_device {
int stride;
int word_size;
int id;
- struct kref refcnt;
size_t size;
bool read_only;
bool root_only;
--
2.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH v3 9/9] nvmem: protect nvmem_device::ops with SRCU
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
` (7 preceding siblings ...)
2026-04-29 15:43 ` [PATCH v3 8/9] nvmem: remove duplicated reference counting Bartosz Golaszewski
@ 2026-04-29 15:43 ` Bartosz Golaszewski
8 siblings, 0 replies; 10+ messages in thread
From: Bartosz Golaszewski @ 2026-04-29 15:43 UTC (permalink / raw)
To: Srinivas Kandagatla, Bartosz Golaszewski, Johan Hovold
Cc: linux-kernel, brgl, Bartosz Golaszewski
With the provider-owned data split out into a separate 'ops' structure,
we can now protect it with SRCU.
Protect all dereferences of nvmem->ops with an SRCU read lock.
Synchronize SRCU in nvmem_unregister() after setting the implementation
pointer to NULL. This has the effect of numbing down the device after
nvmem_unregister() returns - it will no longer accept any consumer calls
and return -ENODEV. The actual device will live on for as long as there
are references to it but we will no longer reach into the consumer's
memory which may be gone by this time.
Nvmem cell entries are destroyed in .release() now as they may be still
dereferenced via the nvmem_cell handles after nvmem_release(). The
actual calls will still go through SRCU and fail with -ENODEV if the
provider is gone.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 42 ++++++++++++++++++++++++++++++++++++------
drivers/nvmem/internals.h | 4 +++-
2 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 590d880ede944670c09dd62b394626a959e5cdd1..9ec2d831ece6e51f918b5a815e45db346f30be7f 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -57,7 +57,12 @@ static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- struct nvmem_operations *ops = nvmem->ops;
+ struct nvmem_operations *ops;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = rcu_dereference(nvmem->ops);
+ if (!ops)
+ return -ENODEV;
if (!ops->reg_read)
return -EOPNOTSUPP;
@@ -68,9 +73,14 @@ static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- struct nvmem_operations *ops = nvmem->ops;
+ struct nvmem_operations *ops;
int ret, written;
+ guard(srcu)(&nvmem->srcu);
+ ops = rcu_dereference(nvmem->ops);
+ if (!ops)
+ return -ENODEV;
+
if (!ops->reg_write)
return -EOPNOTSUPP;
@@ -283,7 +293,7 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
{
- struct nvmem_operations *ops = nvmem->ops;
+ struct nvmem_operations *ops = rcu_dereference_raw(nvmem->ops);
umode_t mode = 0400;
@@ -327,7 +337,7 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj,
{
struct device *dev = kobj_to_dev(kobj);
struct nvmem_device *nvmem = to_nvmem_device(dev);
- struct nvmem_operations *ops = nvmem->ops;
+ struct nvmem_operations *ops = rcu_dereference_raw(nvmem->ops);
/*
* If the device has no .reg_write operation, do not allow
@@ -554,7 +564,7 @@ static void nvmem_release(struct device *dev)
gpiod_put(nvmem->wp_gpio);
nvmem_device_remove_all_cells(nvmem);
ida_free(&nvmem_ida, nvmem->id);
- kfree(nvmem->ops);
+ cleanup_srcu_struct(&nvmem->srcu);
kfree(nvmem);
}
@@ -930,6 +940,18 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->dev.bus = &nvmem_bus_type;
nvmem->dev.parent = config->dev;
+ /*
+ * Must happen before we assign the release() callback in
+ * device_initialize().
+ */
+ rval = init_srcu_struct(&nvmem->srcu);
+ if (rval) {
+ ida_free(&nvmem_ida, nvmem->id);
+ kfree(ops);
+ kfree(nvmem);
+ return ERR_PTR(rval);
+ }
+
device_initialize(&nvmem->dev);
if (!config->ignore_wp)
@@ -947,7 +969,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
ops->reg_read = config->reg_read;
ops->reg_write = config->reg_write;
- nvmem->ops = ops;
+ rcu_assign_pointer(nvmem->ops, ops);
+
nvmem->owner = config->owner;
if (!nvmem->owner && config->dev->driver)
nvmem->owner = config->dev->driver->owner;
@@ -1046,6 +1069,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem_sysfs_remove_compat(nvmem);
err_put_device:
put_device(&nvmem->dev);
+ kfree(ops);
return ERR_PTR(rval);
}
@@ -1058,10 +1082,16 @@ EXPORT_SYMBOL_GPL(nvmem_register);
*/
void nvmem_unregister(struct nvmem_device *nvmem)
{
+ struct nvmem_operations *ops;
+
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
+ ops = rcu_replace_pointer(nvmem->ops, NULL, true);
+ synchronize_srcu(&nvmem->srcu);
+
nvmem_sysfs_remove_compat(nvmem);
nvmem_destroy_layout(nvmem);
+ kfree(ops);
device_unregister(&nvmem->dev);
}
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 60daa79c404dc915872481bd3b02de2258d33406..17418fd0dcc92b101b56082fc15d74042386107d 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -6,6 +6,7 @@
#include <linux/device.h>
#include <linux/nvmem-consumer.h>
#include <linux/nvmem-provider.h>
+#include <linux/srcu.h>
/* Hold pointers to callbacks owned by the nvmem provider module. */
struct nvmem_operations {
@@ -16,6 +17,7 @@ struct nvmem_operations {
struct nvmem_device {
struct module *owner;
struct device dev;
+ struct srcu_struct srcu;
int stride;
int word_size;
int id;
@@ -33,7 +35,7 @@ struct nvmem_device {
unsigned int nkeepout;
struct gpio_desc *wp_gpio;
struct nvmem_layout *layout;
- struct nvmem_operations *ops;
+ struct nvmem_operations __rcu *ops;
void *priv;
bool sysfs_cells_populated;
};
--
2.47.3
^ permalink raw reply [flat|nested] 10+ messages in thread