2020-03-18 14:51:47

by Nicholas Johnson

[permalink] [raw]
Subject: [PATCH v5 0/1] nvmem: Add support for write-only instances, and clean-up

Hello all,

Previous version: https://lkml.org/lkml/2020/3/16/1051

Changed since previous version:

- No longer leaking objects such as idr and gpio.

- Got rid of accidental line of diff removing blank line.

- Rebased on top of [0] as requested in [1] - no conflicts, anyway.

[0]
https://git.kernel.org/pub/scm/linux/kernel/git/srini/nvmem.git/tree/drivers/nvmem/core.c?h=for-next

[1]
https://lkml.org/lkml/2020/3/18/171

Nicholas Johnson (1):
nvmem: Add support for write-only instances

drivers/nvmem/core.c | 10 +++++--
drivers/nvmem/nvmem-sysfs.c | 56 +++++++++++++++++++++++++++++++------
2 files changed, 56 insertions(+), 10 deletions(-)

--
2.25.1


2020-03-18 15:21:12

by Nicholas Johnson

[permalink] [raw]
Subject: [PATCH v5 1/1] nvmem: Add support for write-only instances

There is at least one real-world use-case for write-only nvmem
instances. Refer to 03cd45d2e219 ("thunderbolt: Prevent crash if
non-active NVMem file is read").

Add support for write-only nvmem instances by adding attrs for 0200.

Change nvmem_register() to abort if NULL group is returned from
nvmem_sysfs_get_groups().

Return NULL from nvmem_sysfs_get_groups() in invalid cases.

Signed-off-by: Nicholas Johnson <[email protected]>
---
drivers/nvmem/core.c | 10 +++++--
drivers/nvmem/nvmem-sysfs.c | 56 +++++++++++++++++++++++++++++++------
2 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 77d890d36..ddc7be514 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -381,6 +381,14 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->type = config->type;
nvmem->reg_read = config->reg_read;
nvmem->reg_write = config->reg_write;
+ nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
+ if (!nvmem->dev.groups) {
+ ida_simple_remove(&nvmem_ida, nvmem->id);
+ gpiod_put(nvmem->wp_gpio);
+ kfree(nvmem);
+ return ERR_PTR(-EINVAL);
+ }
+
if (!config->no_of_node)
nvmem->dev.of_node = config->dev->of_node;

@@ -395,8 +403,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->read_only = device_property_present(config->dev, "read-only") ||
config->read_only || !nvmem->reg_write;

- nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
-
device_initialize(&nvmem->dev);

dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
diff --git a/drivers/nvmem/nvmem-sysfs.c b/drivers/nvmem/nvmem-sysfs.c
index 9e0c429cd..4ceac8680 100644
--- a/drivers/nvmem/nvmem-sysfs.c
+++ b/drivers/nvmem/nvmem-sysfs.c
@@ -196,16 +196,49 @@ 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;
+ /* Read-only */
+ if (nvmem->reg_read && (!nvmem->reg_write || nvmem->read_only))
+ return config->root_only ?
+ nvmem_ro_root_dev_groups : nvmem_ro_dev_groups;
+
+ /* Read-write */
+ if (nvmem->reg_read && nvmem->reg_write && !nvmem->read_only)
+ return config->root_only ?
+ nvmem_rw_root_dev_groups : nvmem_rw_dev_groups;
+
+ /* Write-only, do not honour request for global writable entry */
+ if (!nvmem->reg_read && nvmem->reg_write && !nvmem->read_only)
+ return config->root_only ? nvmem_wo_root_dev_groups : NULL;

- return nvmem->read_only ? nvmem_ro_dev_groups : nvmem_rw_dev_groups;
+ return NULL;
}

/*
@@ -224,17 +257,24 @@ 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 || nvmem->read_only)) {
if (config->root_only)
nvmem->eeprom = bin_attr_ro_root_nvmem;
else
nvmem->eeprom = bin_attr_ro_nvmem;
- } else {
+ } else if (!nvmem->reg_read && nvmem->reg_write && !nvmem->read_only) {
+ if (config->root_only)
+ nvmem->eeprom = bin_attr_wo_root_nvmem;
+ else
+ return -EINVAL;
+ } else if (nvmem->reg_read && nvmem->reg_write && !nvmem->read_only) {
if (config->root_only)
nvmem->eeprom = bin_attr_rw_root_nvmem;
else
nvmem->eeprom = bin_attr_rw_nvmem;
- }
+ } else
+ return -EINVAL;
+
nvmem->eeprom.attr.name = "eeprom";
nvmem->eeprom.size = nvmem->size;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
--
2.25.1

2020-03-18 16:10:21

by Srinivas Kandagatla

[permalink] [raw]
Subject: Re: [PATCH v5 1/1] nvmem: Add support for write-only instances



On 18/03/2020 15:20, Nicholas Johnson wrote:
> There is at least one real-world use-case for write-only nvmem
> instances. Refer to 03cd45d2e219 ("thunderbolt: Prevent crash if
> non-active NVMem file is read").
>
> Add support for write-only nvmem instances by adding attrs for 0200.
>
> Change nvmem_register() to abort if NULL group is returned from
> nvmem_sysfs_get_groups().
>
> Return NULL from nvmem_sysfs_get_groups() in invalid cases.
>
> Signed-off-by: Nicholas Johnson<[email protected]>
> ---
> drivers/nvmem/core.c | 10 +++++--
> drivers/nvmem/nvmem-sysfs.c | 56 +++++++++++++++++++++++++++++++------
> 2 files changed, 56 insertions(+), 10 deletions(-)

Applied thanks,

--srini

2020-03-24 12:23:59

by Srinivas Kandagatla

[permalink] [raw]
Subject: Re: [PATCH v5 1/1] nvmem: Add support for write-only instances

Sorry for such late review, but I did endup finding two new issues with
this patch.

On 18/03/2020 15:20, Nicholas Johnson wrote:
> There is at least one real-world use-case for write-only nvmem
> instances. Refer to 03cd45d2e219 ("thunderbolt: Prevent crash if
> non-active NVMem file is read").
>
> Add support for write-only nvmem instances by adding attrs for 0200.
>
> Change nvmem_register() to abort if NULL group is returned from
> nvmem_sysfs_get_groups().
>
> Return NULL from nvmem_sysfs_get_groups() in invalid cases.
>
> Signed-off-by: Nicholas Johnson <[email protected]>
> ---
> drivers/nvmem/core.c | 10 +++++--
> drivers/nvmem/nvmem-sysfs.c | 56 +++++++++++++++++++++++++++++++------
> 2 files changed, 56 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 77d890d36..ddc7be514 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -381,6 +381,14 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
> nvmem->type = config->type;
> nvmem->reg_read = config->reg_read;
> nvmem->reg_write = config->reg_write;
> + nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
> + if (!nvmem->dev.groups) {
> + ida_simple_remove(&nvmem_ida, nvmem->id);
> + gpiod_put(nvmem->wp_gpio);
> + kfree(nvmem);
> + return ERR_PTR(-EINVAL);
> + }

returning NULL is valid usecase here, consider it like not having
CONFIG_NVMEM_SYSFS so no need to check the return value.

But a warning/error message from nvmem_sysfs_get_groups would help debug
in case of invalid configurations.

> +
> if (!config->no_of_node)
> nvmem->dev.of_node = config->dev->of_node;
>
> @@ -395,8 +403,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
> nvmem->read_only = device_property_present(config->dev, "read-only") ||
> config->read_only || !nvmem->reg_write;
>
> - nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
> -
Moving this to before read_only flag is set would not work! as
nvmem_sysfs_get_groups() is going to use read_only flag.

Ideally you do not need to change anything in core.c file and just
update nvmem-sysfs.c should be enough.


> device_initialize(&nvmem->dev);
>
> dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
> diff --git a/drivers/nvmem/nvmem-sysfs.c b/drivers/nvmem/nvmem-sysfs.c
> index 9e0c429cd..4ceac8680 100644
> --- a/drivers/nvmem/nvmem-sysfs.c
> +++ b/drivers/nvmem/nvmem-sysfs.c
> @@ -196,16 +196,49 @@ 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;
> + /* Read-only */
> + if (nvmem->reg_read && (!nvmem->reg_write || nvmem->read_only))
> + return config->root_only ?
> + nvmem_ro_root_dev_groups : nvmem_ro_dev_groups;
> +
> + /* Read-write */
> + if (nvmem->reg_read && nvmem->reg_write && !nvmem->read_only)
> + return config->root_only ?
> + nvmem_rw_root_dev_groups : nvmem_rw_dev_groups;
> +
> + /* Write-only, do not honour request for global writable entry */
> + if (!nvmem->reg_read && nvmem->reg_write && !nvmem->read_only)
> + return config->root_only ? nvmem_wo_root_dev_groups : NULL;
>
> - return nvmem->read_only ? nvmem_ro_dev_groups : nvmem_rw_dev_groups;
> + return NULL;
> }
>
> /*
> @@ -224,17 +257,24 @@ 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 || nvmem->read_only)) {
> if (config->root_only)
> nvmem->eeprom = bin_attr_ro_root_nvmem;
> else
> nvmem->eeprom = bin_attr_ro_nvmem;
> - } else {
> + } else if (!nvmem->reg_read && nvmem->reg_write && !nvmem->read_only) {
> + if (config->root_only)
> + nvmem->eeprom = bin_attr_wo_root_nvmem;
> + else
> + return -EINVAL;
> + } else if (nvmem->reg_read && nvmem->reg_write && !nvmem->read_only) {
> if (config->root_only)
> nvmem->eeprom = bin_attr_rw_root_nvmem;
> else
> nvmem->eeprom = bin_attr_rw_nvmem;
> - }
> + } else
> + return -EINVAL;
> +
> nvmem->eeprom.attr.name = "eeprom";
> nvmem->eeprom.size = nvmem->size;
> #ifdef CONFIG_DEBUG_LOCK_ALLOC
>