From: Johan Hovold <johan@kernel.org>
To: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Cc: Srinivas Kandagatla <srini@kernel.org>,
Bartosz Golaszewski <brgl@kernel.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/7] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks
Date: Mon, 23 Mar 2026 17:21:51 +0100 [thread overview]
Message-ID: <acFon03BPVdEcTZr@hovoldconsulting.com> (raw)
In-Reply-To: <20260223-nvmem-unbind-v2-2-0df33a933dca@oss.qualcomm.com>
On Mon, Feb 23, 2026 at 11:57:03AM +0100, Bartosz Golaszewski wrote:
> __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 as well as allows us to translate it easily to
> -EPERM in sysfs callbacks without having to dereference the callback
> pointers. This will allow us to limit the number of SRCU critical
> sections in the follow-up commits.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
> ---
> drivers/nvmem/core.c | 37 +++++++++++++++++++------------------
> 1 file changed, 19 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 311cb2e5a5c02d2c6979d7c9bbb7f94abdfbdad1..14f583e466c8d95690539bd886fd0c2fdd440998 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;
> }
The above looks like good clean ups in their own right.
But shouldn't the check be moved to to nvmem_reg_read/write() to avoid
calling into nvmem_access_with_keepouts()?
> static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
> @@ -231,13 +231,12 @@ 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)
> + if (rc) {
> + if (rc == -EOPNOTSUPP)
> + rc = -EPERM;
> return rc;
> + }
>
> return count;
> }
> @@ -264,13 +263,15 @@ 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)
> + if (rc) {
> + if (rc == -EOPNOTSUPP)
> + rc = -EPERM;
> return rc;
> + }
>
> return count;
> }
And these attributes are not even visible when the device does not
support reading or writing so checking later is not an issue.
Did you make sure that there are no in-kernel callers that will get
confused by the -EINVAL => -EOPNOTSUPP change?
Johan
next prev parent reply other threads:[~2026-03-23 16:21 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-23 10:57 [PATCH v2 0/7] nvmem: survive unbind with active consumers Bartosz Golaszewski
2026-02-23 10:57 ` [PATCH v2 1/7] nvmem: remove unused field from struct nvmem_device Bartosz Golaszewski
2026-03-23 16:00 ` Johan Hovold
2026-02-23 10:57 ` [PATCH v2 2/7] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks Bartosz Golaszewski
2026-03-23 16:21 ` Johan Hovold [this message]
2026-02-23 10:57 ` [PATCH v2 3/7] nvmem: check the return value of gpiod_set_value_cansleep() Bartosz Golaszewski
2026-03-23 16:28 ` Johan Hovold
2026-02-23 10:57 ` [PATCH v2 4/7] nvmem: simplify locking with guard() Bartosz Golaszewski
2026-03-23 16:35 ` Johan Hovold
2026-02-23 10:57 ` [PATCH v2 5/7] nvmem: remove unneeded __nvmem_device_put() Bartosz Golaszewski
2026-03-23 16:44 ` Johan Hovold
2026-02-23 10:57 ` [PATCH v2 6/7] nvmem: split struct nvmem_device into refcounted and provider-owned data Bartosz Golaszewski
2026-03-17 10:44 ` Johan Hovold
2026-03-17 12:52 ` Bartosz Golaszewski
2026-03-23 16:45 ` Johan Hovold
2026-02-23 10:57 ` [PATCH v2 7/7] nvmem: synchronize nvmem device unregistering with SRCU Bartosz Golaszewski
2026-03-17 11:31 ` Johan Hovold
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=acFon03BPVdEcTZr@hovoldconsulting.com \
--to=johan@kernel.org \
--cc=bartosz.golaszewski@oss.qualcomm.com \
--cc=brgl@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=srini@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox