Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "James Hilliard" <james.hilliard1@gmail.com>,
	"Srinivas Kandagatla" <srini@kernel.org>,
	"Mathieu Dubois-Briand" <mathieu.dubois-briand@bootlin.com>,
	"Michael Walle" <mwalle@kernel.org>,
	"Rafał Miłecki" <rafal@milecki.pl>
Cc: "Bartosz Golaszewski" <bartosz.golaszewski@oss.qualcomm.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Miquel Raynal" <miquel.raynal@bootlin.com>,
	"Paul Cercueil" <paul@crapouillou.net>,
	linux-mtd@lists.infradead.org
Subject: Re: [PATCH 06/11] nvmem: manage cell attributes independently of device resources
Date: Tue, 17 Nov 2026 19:59:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-219539@kernel.org> (raw)
In-Reply-To: <20261117-submit-nvmem-provider-lifetime-v1-v1-6-3cfbd48812b5@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] nvmem: mix of cleanup helpers and goto unwinding in
  nvmem_populate_sysfs_cells()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Medium] nvmem: resource leak in nvmem_layout_register() [1]
--

commit 3af327034f410c1d0e7599f729d78e0fec4f2dfa
Author: James Hilliard <james.hilliard1@gmail.com>

nvmem: manage cell attributes independently of device resources

This commit manages NVMEM sysfs cell attributes explicitly with reference
counting and a dedicated mutex so the attribute group can be rebuilt or
removed independently of device lifetime.

> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 70b6126886d9f..14cc70e8ce785 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
[ ... ]
> @@ -496,59 +497,102 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
[ ... ]
>  static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
>  {
> -	struct attribute_group group = {
> -		.name	= "cells",
> -	};
>  	struct nvmem_cell_entry *entry;
> -	const struct bin_attribute **pattrs;
> -	struct bin_attribute *attrs;
> -	unsigned int ncells = 0, i = 0;
> -	int ret;
> +	const struct bin_attribute **attrs = NULL;
> +	unsigned int i = 0;
> +	int ret = 0;
>  
> -	guard(mutex)(&nvmem_mutex);
> +	guard(mutex)(&nvmem->sysfs_mutex);
>  
> -	if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
> -		return 0;
> +	__nvmem_sysfs_remove_cells(nvmem);
>  
> -	/* Allocate an array of attributes with a sentinel */
> -	ncells = list_count_nodes(&nvmem->cells);
> -	pattrs = devm_kcalloc(&nvmem->dev, ncells + 1,
> -			      sizeof(*pattrs), GFP_KERNEL);
> -	if (!pattrs)
> -		return -ENOMEM;
> -
> -	attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
> -	if (!attrs)
> -		return -ENOMEM;
> +	scoped_guard(mutex, &nvmem_mutex) {
> +		if (!rcu_access_pointer(nvmem->ops))
> +			return -ENODEV;
> +		if (list_empty(&nvmem->cells))
> +			return 0;
>  
> -	/* Initialize each attribute to take the name and size of the cell */
> -	list_for_each_entry(entry, &nvmem->cells, node) {
> -		sysfs_bin_attr_init(&attrs[i]);
> -		attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
> -						    "%s@%x,%x", entry->name,
> -						    entry->offset,
> -						    entry->bit_offset);
> -		attrs[i].attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem);
> -		attrs[i].size = entry->bytes;
> -		attrs[i].read = &nvmem_cell_attr_read;
> -		attrs[i].private = entry;
> -		if (!attrs[i].attr.name)
> +		attrs = kcalloc(list_count_nodes(&nvmem->cells) + 1,
> +				sizeof(*attrs), GFP_KERNEL);
> +		if (!attrs)
>  			return -ENOMEM;
>  
> -		pattrs[i] = &attrs[i];
> -		i++;
> +		list_for_each_entry(entry, &nvmem->cells, node) {
> +			struct bin_attribute *attr;
> +
> +			attr = kzalloc_obj(*attr);
> +			if (!attr) {
> +				ret = -ENOMEM;
> +				break;
> +			}
> +			sysfs_bin_attr_init(attr);
> +			attr->attr.name = kasprintf(GFP_KERNEL, "%s@%x,%x",
> +						   entry->name, entry->offset,
> +						   entry->bit_offset);
> +			if (!attr->attr.name) {
> +				kfree(attr);
> +				ret = -ENOMEM;
> +				break;
> +			}
> +			attr->attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem);
> +			attr->size = entry->bytes;
> +			attr->read = nvmem_cell_attr_read;
> +			attr->private = entry;
> +			kref_get(&entry->refcnt);
> +			attrs[i++] = attr;
> +		}
>  	}
> +	if (ret)
> +		goto err_free;
>  
> -	group.bin_attrs = pattrs;
> +	nvmem->cells_group.name = "cells";
> +	nvmem->cells_group.bin_attrs = attrs;
>  
> -	ret = device_add_group(&nvmem->dev, &group);
> -	if (ret)
> -		return ret;
> +	ret = device_add_group(&nvmem->dev, &nvmem->cells_group);
> +	if (ret) {
> +		nvmem->cells_group.bin_attrs = NULL;
> +		goto err_free;
> +	}
>  
>  	nvmem->sysfs_cells_populated = true;
> +	return 0;
>  
> +err_free:
> +	nvmem_sysfs_free_cell_attrs(attrs);
>  	return ret;
>  }

[Severity: Low]
This isn't a bug, but nvmem_populate_sysfs_cells() mixes guard() and
scoped_guard() with goto err_free cleanup in the same function, whereas
cleanup.h guidelines prefer either converting all resources in a function
to scope-based cleanup or keeping traditional goto unwinding.

[Severity: Medium]
This problem wasn't introduced by this patch, but in the caller
nvmem_layout_register(), can a partial failure in layout->add_cells() leak
nvmem_cell_entry structures [1]?

drivers/nvmem/core.c:nvmem_layout_register() {
	...
	scoped_guard(srcu, &layout->nvmem->srcu) {
		if (!srcu_dereference(layout->nvmem->ops, &layout->nvmem->srcu))
			return -ENODEV;
		ret = layout->add_cells(layout);
	}
	if (ret)
		return ret;

#ifdef CONFIG_NVMEM_SYSFS
	ret = nvmem_populate_sysfs_cells(layout->nvmem);
	if (ret) {
		nvmem_device_remove_all_cells(layout->nvmem);
		return ret;
	}
#endif
	...
}

Unlike the nvmem_populate_sysfs_cells() error path right below it, if
layout->add_cells() fails after adding one or more cells, it returns
without calling nvmem_device_remove_all_cells(layout->nvmem), leaving those
entries attached to nvmem->cells and duplicating them if the layout is
later retried or rebound.

[1] https://sashiko.dev/#/bug/linux-014a0624-e8da-49aa-95a8-67a7c19dfa24

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com?part=6

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2026-11-17 19:59 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
2026-11-17 19:38 ` [PATCH 01/11] nvmem: retain the provider node until device release James Hilliard
2026-11-17 19:38 ` [PATCH 02/11] nvmem: drain keepout and cell post-processing accesses James Hilliard
2026-11-17 19:50   ` sashiko-bot
2026-11-17 19:38 ` [PATCH 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
2026-11-17 19:38 ` [PATCH 04/11] nvmem: release write protection when the provider disconnects James Hilliard
2026-11-17 19:38 ` [PATCH 05/11] nvmem: reference cell entries independently of their provider list James Hilliard
2026-11-17 19:48   ` sashiko-bot
2026-11-17 19:38 ` [PATCH 06/11] nvmem: manage cell attributes independently of device resources James Hilliard
2026-11-17 19:59   ` sashiko-bot [this message]
2026-11-17 19:38 ` [PATCH 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
2026-11-17 19:51   ` sashiko-bot
2026-11-17 19:38 ` [PATCH 08/11] nvmem: disconnect cells before removing their layout James Hilliard
2026-11-17 19:38 ` [PATCH 09/11] nvmem: disconnect published devices before registration rollback James Hilliard
2026-11-17 19:38 ` [PATCH 10/11] nvmem: u-boot-env: propagate cell registration errors James Hilliard
2026-11-17 19:38 ` [PATCH 11/11] nvmem: allow providers to specify an explicit NULL owner James Hilliard

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=sashiko-outbox-219539@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=james.hilliard1@gmail.com \
    --cc=linux-mtd@lists.infradead.org \
    --cc=mathieu.dubois-briand@bootlin.com \
    --cc=miquel.raynal@bootlin.com \
    --cc=mwalle@kernel.org \
    --cc=paul@crapouillou.net \
    --cc=rafal@milecki.pl \
    --cc=sashiko-reviews@lists.linux.dev \
    --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