From: Mika Westerberg <mika.westerberg@linux.intel.com>
To: Nicholas Johnson <nicholas.johnson-opensource@outlook.com.au>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Subject: Re: [PATCH v1 1/3] nvmem: Add support for write-only instances
Date: Tue, 25 Feb 2020 14:51:41 +0200 [thread overview]
Message-ID: <20200225125141.GA2667@lahna.fi.intel.com> (raw)
In-Reply-To: <PSXP216MB043820B6E11AE4E78374C7F980EC0@PSXP216MB0438.KORP216.PROD.OUTLOOK.COM>
On Mon, Feb 24, 2020 at 05:42:33PM +0000, Nicholas Johnson wrote:
> Mika Westerberg requires write-only nvmem for the Thunderbolt driver.
> Refer to 03cd45d2e219 ("thunderbolt: Prevent crash if non-active NVMem
> file is read"). Hence, there is at least one real-world use for
> write-only nvmem instances.
Well, I don't require anything ;-) It is the thunderbolt driver that has
one nvmem device that is write-only and it may take advantage of this.
> Add support for write-only nvmem instances by changing the nvmem attrs
> to 0222 if the .reg_read callback is not populated.
>
> Add a WARN_ON in case a driver populates neither .reg_read nor
> .reg_write because this behaviour should clearly never occur.
>
> Signed-off-by: Nicholas Johnson <nicholas.johnson-opensource@outlook.com.au>
> ---
> drivers/nvmem/nvmem-sysfs.c | 77 +++++++++++++++++++++++++++++++++----
> 1 file changed, 70 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/nvmem/nvmem-sysfs.c b/drivers/nvmem/nvmem-sysfs.c
> index 9e0c429cd..be3b94f0b 100644
> --- a/drivers/nvmem/nvmem-sysfs.c
> +++ b/drivers/nvmem/nvmem-sysfs.c
> @@ -147,6 +147,30 @@ static const struct attribute_group *nvmem_ro_dev_groups[] = {
> NULL,
> };
>
> +/* write only permission */
> +static struct bin_attribute bin_attr_wo_nvmem = {
> + .attr = {
> + .name = "nvmem",
> + .mode = 0222,
I would say no sysfs attribute should ever be writable by the world.
Actually I think maybe we make this one only writeable by root, in other
words it would always require ->root_only to be set.
> + },
> + .write = bin_attr_nvmem_write,
> +};
> +
> +static struct bin_attribute *nvmem_bin_wo_attributes[] = {
> + &bin_attr_wo_nvmem,
> + NULL,
> +};
> +
> +static const struct attribute_group nvmem_bin_wo_group = {
> + .bin_attrs = nvmem_bin_wo_attributes,
> + .attrs = nvmem_attrs,
> +};
> +
> +static const struct attribute_group *nvmem_wo_dev_groups[] = {
> + &nvmem_bin_wo_group,
> + NULL,
> +};
> +
> /* default read/write permissions, root only */
> static struct bin_attribute bin_attr_rw_root_nvmem = {
> .attr = {
> @@ -196,16 +220,50 @@ static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
> NULL,
> };
>
> +/* write only permission, root only */
> +static struct bin_attribute bin_attr_wo_root_nvmem = {
> + .attr = {
> + .name = "nvmem",
> + .mode = 0200,
> + },
> + .write = bin_attr_nvmem_write,
> +};
> +
> +static struct bin_attribute *nvmem_bin_wo_root_attributes[] = {
> + &bin_attr_wo_root_nvmem,
> + NULL,
> +};
> +
> +static const struct attribute_group nvmem_bin_wo_root_group = {
> + .bin_attrs = nvmem_bin_wo_root_attributes,
> + .attrs = nvmem_attrs,
> +};
> +
> +static const struct attribute_group *nvmem_wo_root_dev_groups[] = {
> + &nvmem_bin_wo_root_group,
> + NULL,
> +};
> +
> const struct attribute_group **nvmem_sysfs_get_groups(
> struct nvmem_device *nvmem,
> const struct nvmem_config *config)
> {
> - if (config->root_only)
> - return nvmem->read_only ?
> - nvmem_ro_root_dev_groups :
> - nvmem_rw_root_dev_groups;
> -
> - return nvmem->read_only ? nvmem_ro_dev_groups : nvmem_rw_dev_groups;
> + /*
> + * If neither reg_read nor reg_write are provided, we cannot use this
> + * nvmem entry, as any operation will cause kernel mode NULL reference.
> + */
> + WARN_ON(!nvmem->reg_read && !nvmem->reg_write);
This should also be documented in kernel-doc of struct nvmem_config.
> +
> + if (nvmem->reg_read && nvmem->reg_write)
> + return config->root_only ?
> + nvmem_rw_root_dev_groups : nvmem_rw_dev_groups;
> +
> + if (nvmem->reg_read && !nvmem->reg_write)
> + return config->root_only ?
> + nvmem_ro_root_dev_groups : nvmem_ro_dev_groups;
> +
> + return config->root_only ?
> + nvmem_wo_root_dev_groups : nvmem_wo_dev_groups;
> }
>
> /*
> @@ -224,11 +282,16 @@ int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
> if (!config->base_dev)
> return -EINVAL;
>
> - if (nvmem->read_only) {
> + if (nvmem->reg_read && !nvmem->reg_write) {
> if (config->root_only)
> nvmem->eeprom = bin_attr_ro_root_nvmem;
> else
> nvmem->eeprom = bin_attr_ro_nvmem;
> + } else if (!nvmem->reg_read && nvmem->reg_write) {
> + if (config->root_only)
> + nvmem->eeprom = bin_attr_wo_root_nvmem;
> + else
> + nvmem->eeprom = bin_attr_wo_nvmem;
> } else {
> if (config->root_only)
> nvmem->eeprom = bin_attr_rw_root_nvmem;
> --
> 2.25.1
next prev parent reply other threads:[~2020-02-25 12:51 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-24 17:41 [PATCH v1 0/3] nvmem: Add support for write-only instances, and clean-up Nicholas Johnson
2020-02-24 17:42 ` [PATCH v1 1/3] nvmem: Add support for write-only instances Nicholas Johnson
2020-02-25 12:51 ` Mika Westerberg [this message]
2020-02-25 15:30 ` Nicholas Johnson
2020-02-25 15:43 ` Mika Westerberg
2020-02-27 14:46 ` Nicholas Johnson
2020-02-24 17:43 ` [PATCH v1 2/3] Revert "thunderbolt: Prevent crash if non-active NVMem file is read" Nicholas Johnson
2020-02-25 12:56 ` Mika Westerberg
2020-02-25 15:33 ` Nicholas Johnson
2020-02-25 15:44 ` Mika Westerberg
2020-02-24 17:43 ` [PATCH v1 3/3] nvmem: Remove .read_only field from nvmem_config Nicholas Johnson
2020-02-25 1:19 ` kbuild test robot
2020-02-25 10:49 ` Nicholas Johnson
2020-02-25 14:59 ` [PATCH v1 0/3] nvmem: Add support for write-only instances, and clean-up Srinivas Kandagatla
2020-02-25 15:23 ` Nicholas Johnson
2020-02-25 17:04 ` Srinivas Kandagatla
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=20200225125141.GA2667@lahna.fi.intel.com \
--to=mika.westerberg@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=nicholas.johnson-opensource@outlook.com.au \
--cc=srinivas.kandagatla@linaro.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
Powered by JetHome