diff mbox series

[v4,3/4] nvmem: increase the reference count of a gpio passed over config

Message ID 20200220100141.5905-4-brgl@bgdev.pl
State New
Headers show
Series nvmem/gpio: fix resource management | expand

Commit Message

Bartosz Golaszewski Feb. 20, 2020, 10:01 a.m. UTC
From: Bartosz Golaszewski <bgolaszewski@baylibre.com>

We can obtain the write-protect GPIO in nvmem_register() by requesting
it ourselves or by storing the gpio_desc passed in nvmem_config. In the
latter case we need to increase the reference count so that it gets
freed correctly.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

kernel test robot Feb. 22, 2020, 11:54 a.m. UTC | #1
Hi Bartosz,

I love your patch! Yet something to improve:

[auto build test ERROR on next-20200221]
[also build test ERROR on v5.6-rc2]
[cannot apply to gpio/for-next linus/master v5.6-rc2 v5.6-rc1 v5.5]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Bartosz-Golaszewski/nvmem-gpio-fix-resource-management/20200222-054341
base:    bee46b309a13ca158c99c325d0408fb2f0db207f
config: sparc-defconfig (attached as .config)
compiler: sparc-linux-gcc (GCC) 7.5.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        GCC_VERSION=7.5.0 make.cross ARCH=sparc 

If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   drivers/nvmem/core.c: In function 'nvmem_register':
>> drivers/nvmem/core.c:352:20: error: implicit declaration of function 'gpiod_ref'; did you mean 'gpiod_get'? [-Werror=implicit-function-declaration]
      nvmem->wp_gpio = gpiod_ref(config->wp_gpio);
                       ^~~~~~~~~
                       gpiod_get
   drivers/nvmem/core.c:352:18: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
      nvmem->wp_gpio = gpiod_ref(config->wp_gpio);
                     ^
   cc1: some warnings being treated as errors

vim +352 drivers/nvmem/core.c

   322	
   323	/**
   324	 * nvmem_register() - Register a nvmem device for given nvmem_config.
   325	 * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
   326	 *
   327	 * @config: nvmem device configuration with which nvmem device is created.
   328	 *
   329	 * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
   330	 * on success.
   331	 */
   332	
   333	struct nvmem_device *nvmem_register(const struct nvmem_config *config)
   334	{
   335		struct nvmem_device *nvmem;
   336		int rval;
   337	
   338		if (!config->dev)
   339			return ERR_PTR(-EINVAL);
   340	
   341		nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
   342		if (!nvmem)
   343			return ERR_PTR(-ENOMEM);
   344	
   345		rval  = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
   346		if (rval < 0) {
   347			kfree(nvmem);
   348			return ERR_PTR(rval);
   349		}
   350	
   351		if (config->wp_gpio)
 > 352			nvmem->wp_gpio = gpiod_ref(config->wp_gpio);
   353		else
   354			nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
   355							    GPIOD_OUT_HIGH);
   356		if (IS_ERR(nvmem->wp_gpio)) {
   357			ida_simple_remove(&nvmem_ida, nvmem->id);
   358			kfree(nvmem);
   359			return ERR_CAST(nvmem->wp_gpio);
   360		}
   361	
   362		kref_init(&nvmem->refcnt);
   363		INIT_LIST_HEAD(&nvmem->cells);
   364	
   365		nvmem->id = rval;
   366		nvmem->owner = config->owner;
   367		if (!nvmem->owner && config->dev->driver)
   368			nvmem->owner = config->dev->driver->owner;
   369		nvmem->stride = config->stride ?: 1;
   370		nvmem->word_size = config->word_size ?: 1;
   371		nvmem->size = config->size;
   372		nvmem->dev.type = &nvmem_provider_type;
   373		nvmem->dev.bus = &nvmem_bus_type;
   374		nvmem->dev.parent = config->dev;
   375		nvmem->priv = config->priv;
   376		nvmem->type = config->type;
   377		nvmem->reg_read = config->reg_read;
   378		nvmem->reg_write = config->reg_write;
   379		if (!config->no_of_node)
   380			nvmem->dev.of_node = config->dev->of_node;
   381	
   382		if (config->id == -1 && config->name) {
   383			dev_set_name(&nvmem->dev, "%s", config->name);
   384		} else {
   385			dev_set_name(&nvmem->dev, "%s%d",
   386				     config->name ? : "nvmem",
   387				     config->name ? config->id : nvmem->id);
   388		}
   389	
   390		nvmem->read_only = device_property_present(config->dev, "read-only") ||
   391				   config->read_only || !nvmem->reg_write;
   392	
   393		nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config);
   394	
   395		device_initialize(&nvmem->dev);
   396	
   397		dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
   398	
   399		rval = device_add(&nvmem->dev);
   400		if (rval)
   401			goto err_put_device;
   402	
   403		if (config->compat) {
   404			rval = nvmem_sysfs_setup_compat(nvmem, config);
   405			if (rval)
   406				goto err_device_del;
   407		}
   408	
   409		if (config->cells) {
   410			rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
   411			if (rval)
   412				goto err_teardown_compat;
   413		}
   414	
   415		rval = nvmem_add_cells_from_table(nvmem);
   416		if (rval)
   417			goto err_remove_cells;
   418	
   419		rval = nvmem_add_cells_from_of(nvmem);
   420		if (rval)
   421			goto err_remove_cells;
   422	
   423		blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
   424	
   425		return nvmem;
   426	
   427	err_remove_cells:
   428		nvmem_device_remove_all_cells(nvmem);
   429	err_teardown_compat:
   430		if (config->compat)
   431			nvmem_sysfs_remove_compat(nvmem, config);
   432	err_device_del:
   433		device_del(&nvmem->dev);
   434	err_put_device:
   435		put_device(&nvmem->dev);
   436	
   437		return ERR_PTR(rval);
   438	}
   439	EXPORT_SYMBOL_GPL(nvmem_register);
   440	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
Bartosz Golaszewski Feb. 23, 2020, 8:22 p.m. UTC | #2
sob., 22 lut 2020 o 12:54 kbuild test robot <lkp@intel.com> napisaƂ(a):
>
> Hi Bartosz,
>
> I love your patch! Yet something to improve:
>
> [auto build test ERROR on next-20200221]
> [also build test ERROR on v5.6-rc2]
> [cannot apply to gpio/for-next linus/master v5.6-rc2 v5.6-rc1 v5.5]
> [if your patch is applied to the wrong git tree, please drop us a note to help
> improve the system. BTW, we also suggest to use '--base' option to specify the
> base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
>
> url:    https://github.com/0day-ci/linux/commits/Bartosz-Golaszewski/nvmem-gpio-fix-resource-management/20200222-054341
> base:    bee46b309a13ca158c99c325d0408fb2f0db207f
> config: sparc-defconfig (attached as .config)
> compiler: sparc-linux-gcc (GCC) 7.5.0
> reproduce:
>         wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
>         chmod +x ~/bin/make.cross
>         # save the attached .config to linux build tree
>         GCC_VERSION=7.5.0 make.cross ARCH=sparc
>
> If you fix the issue, kindly add following tag
> Reported-by: kbuild test robot <lkp@intel.com>
>
> All errors (new ones prefixed by >>):
>
>    drivers/nvmem/core.c: In function 'nvmem_register':
> >> drivers/nvmem/core.c:352:20: error: implicit declaration of function 'gpiod_ref'; did you mean 'gpiod_get'? [-Werror=implicit-function-declaration]
>       nvmem->wp_gpio = gpiod_ref(config->wp_gpio);
>                        ^~~~~~~~~
>                        gpiod_get
>    drivers/nvmem/core.c:352:18: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
>       nvmem->wp_gpio = gpiod_ref(config->wp_gpio);
>                      ^
>    cc1: some warnings being treated as errors
>
> vim +352 drivers/nvmem/core.c
>

Of course I forgot to add the stub...

Will fix in next iteration.

Bart
diff mbox series

Patch

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 948c7ebce340..071fb897394c 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -349,7 +349,7 @@  struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	}
 
 	if (config->wp_gpio)
-		nvmem->wp_gpio = config->wp_gpio;
+		nvmem->wp_gpio = gpiod_ref(config->wp_gpio);
 	else
 		nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
 						    GPIOD_OUT_HIGH);