[PATCH v3 2/9] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks

From: Bartosz Golaszewski

Date: Wed Apr 29 2026 - 11:54:40 EST


__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@xxxxxxxxxxxxxxxx>
---
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