Re: [PATCH v5 02/17] nvmem: add DEFINE_FREE for nvmem_cell_put cleanup

Jonathan Cameron <[email protected]> Wed, 5 Aug 2026 01:38:39 +0100
Newsgroups gmane.linux.drivers.devicetree,gmane.linux.kernel.iio,gmane.linux.ports.arm.kernel,gmane.linux.kernel
Message-ID <20260805013839.1c4d10f9@jic23-huawei>
On Tue, 4 Aug 2026 17:03:23 +0530
Varshini Rajendran <[email protected]> wrote:

> Add cleanup.h helper for nvmem_cell_put() to enable automatic resource
> cleanup using __free(nvmem_cell_put) annotation.
> 
> Use IS_ERR_OR_NULL() since nvmem_cell_get() can return error pointers,
> and passing an ERR_PTR to nvmem_cell_put() would cause issues.
> 
> Signed-off-by: Varshini Rajendran <[email protected]>
https://sashiko.dev/#/patchset/20260804113338.261782-1-varshini.rajendran%40microchip.com
Has a question about whether this should be available when
!CONFIG_NVMEM

Seems like a reasonable suggestion to me given nvmem_cell_put() is stubbed
out.

Jonathan

> ---
>  include/linux/nvmem-consumer.h | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
> index 34c0e58dfa26..35a2ea8b8957 100644
> --- a/include/linux/nvmem-consumer.h
> +++ b/include/linux/nvmem-consumer.h
> @@ -9,6 +9,7 @@
>  #ifndef _LINUX_NVMEM_CONSUMER_H
>  #define _LINUX_NVMEM_CONSUMER_H
>  
> +#include <linux/cleanup.h>
>  #include <linux/err.h>
>  #include <linux/errno.h>
>  #include <linux/notifier.h>
> @@ -54,6 +55,9 @@ struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id);
>  struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id);
>  void nvmem_cell_put(struct nvmem_cell *cell);
>  void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
> +
> +DEFINE_FREE(nvmem_cell_put, struct nvmem_cell *, if (!IS_ERR_OR_NULL(_T)) nvmem_cell_put(_T))
> +
>  void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len);
>  int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len);
>  int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val);