diff mbox series

EDAC: Fix some refcount leaks

Message ID 20220511081402.19784-1-linmq006@gmail.com (mailing list archive)
State New, archived
Headers show
Series EDAC: Fix some refcount leaks | expand

Commit Message

Miaoqian Lin May 11, 2022, 8:14 a.m. UTC
kobject_init_and_add() takes reference even when it fails.
According to the doc of kobject_init_and_add()

   If this function returns an error, kobject_put() must be called to
   properly clean up the memory associated with the object.

Fix this by calling kobject_put() when kobject_init_and_add() fails.

Fixes: b2ed215a3338 ("Kobject: change drivers/edac to use kobject_init_and_add")
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
 drivers/edac/edac_device_sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

kernel test robot May 11, 2022, 2:33 p.m. UTC | #1
Hi Miaoqian,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on ras/edac-for-next]
[also build test ERROR on linux/master linus/master v5.18-rc6 next-20220511]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Miaoqian-Lin/EDAC-Fix-some-refcount-leaks/20220511-161440
base:   https://git.kernel.org/pub/scm/linux/kernel/git/ras/ras.git edac-for-next
config: arm64-randconfig-r032-20220509 (https://download.01.org/0day-ci/archive/20220511/202205112212.TdxIloxH-lkp@intel.com/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 18dd123c56754edf62c7042dcf23185c3727610f)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm64 cross compiling tool for clang build
        # apt-get install binutils-aarch64-linux-gnu
        # https://github.com/intel-lab-lkp/linux/commit/e5e3d8b94764dd1abe3c99881483c3f6dee8030a
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Miaoqian-Lin/EDAC-Fix-some-refcount-leaks/20220511-161440
        git checkout e5e3d8b94764dd1abe3c99881483c3f6dee8030a
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash drivers/edac/

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

All errors (new ones prefixed by >>):

>> drivers/edac/edac_device_sysfs.c:640:8: error: use of undeclared label 'edac_device_create_instance'
                   goto edac_device_create_instance;
                        ^
   1 error generated.


vim +/edac_device_create_instance +640 drivers/edac/edac_device_sysfs.c

   604	
   605	/*
   606	 * edac_device_create_instance
   607	 *	create just one instance of an edac_device 'instance'
   608	 */
   609	static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
   610					int idx)
   611	{
   612		int i, j;
   613		int err;
   614		struct edac_device_instance *instance;
   615		struct kobject *main_kobj;
   616	
   617		instance = &edac_dev->instances[idx];
   618	
   619		/* Init the instance's kobject */
   620		memset(&instance->kobj, 0, sizeof(struct kobject));
   621	
   622		instance->ctl = edac_dev;
   623	
   624		/* bump the main kobject's reference count for this controller
   625		 * and this instance is dependent on the main
   626		 */
   627		main_kobj = kobject_get(&edac_dev->kobj);
   628		if (!main_kobj) {
   629			err = -ENODEV;
   630			goto err_out;
   631		}
   632	
   633		/* Formally register this instance's kobject under the edac_device */
   634		err = kobject_init_and_add(&instance->kobj, &ktype_instance_ctrl,
   635					   &edac_dev->kobj, "%s", instance->name);
   636		if (err != 0) {
   637			edac_dbg(2, "Failed to register instance '%s'\n",
   638				 instance->name);
   639			kobject_put(main_kobj);
 > 640			goto edac_device_create_instance;
   641		}
   642	
   643		edac_dbg(4, "now register '%d' blocks for instance %d\n",
   644			 instance->nr_blocks, idx);
   645	
   646		/* register all blocks of this instance */
   647		for (i = 0; i < instance->nr_blocks; i++) {
   648			err = edac_device_create_block(edac_dev, instance,
   649							&instance->blocks[i]);
   650			if (err) {
   651				/* If any fail, remove all previous ones */
   652				for (j = 0; j < i; j++)
   653					edac_device_delete_block(edac_dev,
   654								&instance->blocks[j]);
   655				goto err_release_instance_kobj;
   656			}
   657		}
   658		kobject_uevent(&instance->kobj, KOBJ_ADD);
   659	
   660		edac_dbg(4, "Registered instance %d '%s' kobject\n",
   661			 idx, instance->name);
   662	
   663		return 0;
   664	
   665		/* error unwind stack */
   666	err_release_instance_kobj:
   667		kobject_put(&instance->kobj);
   668	
   669	err_out:
   670		return err;
   671	}
   672
kernel test robot May 11, 2022, 5:59 p.m. UTC | #2
Hi Miaoqian,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on ras/edac-for-next]
[also build test ERROR on linux/master linus/master v5.18-rc6 next-20220511]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Miaoqian-Lin/EDAC-Fix-some-refcount-leaks/20220511-161440
base:   https://git.kernel.org/pub/scm/linux/kernel/git/ras/ras.git edac-for-next
config: powerpc-allyesconfig (https://download.01.org/0day-ci/archive/20220512/202205120119.sArUU4SY-lkp@intel.com/config)
compiler: powerpc-linux-gcc (GCC) 11.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/e5e3d8b94764dd1abe3c99881483c3f6dee8030a
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Miaoqian-Lin/EDAC-Fix-some-refcount-leaks/20220511-161440
        git checkout e5e3d8b94764dd1abe3c99881483c3f6dee8030a
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=powerpc SHELL=/bin/bash drivers/

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

All errors (new ones prefixed by >>):

   drivers/edac/edac_device_sysfs.c: In function 'edac_device_create_instance':
>> drivers/edac/edac_device_sysfs.c:640:17: error: label 'edac_device_create_instance' used but not defined
     640 |                 goto edac_device_create_instance;
         |                 ^~~~


vim +/edac_device_create_instance +640 drivers/edac/edac_device_sysfs.c

   604	
   605	/*
   606	 * edac_device_create_instance
   607	 *	create just one instance of an edac_device 'instance'
   608	 */
   609	static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
   610					int idx)
   611	{
   612		int i, j;
   613		int err;
   614		struct edac_device_instance *instance;
   615		struct kobject *main_kobj;
   616	
   617		instance = &edac_dev->instances[idx];
   618	
   619		/* Init the instance's kobject */
   620		memset(&instance->kobj, 0, sizeof(struct kobject));
   621	
   622		instance->ctl = edac_dev;
   623	
   624		/* bump the main kobject's reference count for this controller
   625		 * and this instance is dependent on the main
   626		 */
   627		main_kobj = kobject_get(&edac_dev->kobj);
   628		if (!main_kobj) {
   629			err = -ENODEV;
   630			goto err_out;
   631		}
   632	
   633		/* Formally register this instance's kobject under the edac_device */
   634		err = kobject_init_and_add(&instance->kobj, &ktype_instance_ctrl,
   635					   &edac_dev->kobj, "%s", instance->name);
   636		if (err != 0) {
   637			edac_dbg(2, "Failed to register instance '%s'\n",
   638				 instance->name);
   639			kobject_put(main_kobj);
 > 640			goto edac_device_create_instance;
   641		}
   642	
   643		edac_dbg(4, "now register '%d' blocks for instance %d\n",
   644			 instance->nr_blocks, idx);
   645	
   646		/* register all blocks of this instance */
   647		for (i = 0; i < instance->nr_blocks; i++) {
   648			err = edac_device_create_block(edac_dev, instance,
   649							&instance->blocks[i]);
   650			if (err) {
   651				/* If any fail, remove all previous ones */
   652				for (j = 0; j < i; j++)
   653					edac_device_delete_block(edac_dev,
   654								&instance->blocks[j]);
   655				goto err_release_instance_kobj;
   656			}
   657		}
   658		kobject_uevent(&instance->kobj, KOBJ_ADD);
   659	
   660		edac_dbg(4, "Registered instance %d '%s' kobject\n",
   661			 idx, instance->name);
   662	
   663		return 0;
   664	
   665		/* error unwind stack */
   666	err_release_instance_kobj:
   667		kobject_put(&instance->kobj);
   668	
   669	err_out:
   670		return err;
   671	}
   672
diff mbox series

Patch

diff --git a/drivers/edac/edac_device_sysfs.c b/drivers/edac/edac_device_sysfs.c
index 9a61d92bdf42..08cba532ad48 100644
--- a/drivers/edac/edac_device_sysfs.c
+++ b/drivers/edac/edac_device_sysfs.c
@@ -542,7 +542,7 @@  static int edac_device_create_block(struct edac_device_ctl_info *edac_dev,
 		edac_dbg(1, "Failed to register instance '%s'\n", block->name);
 		kobject_put(main_kobj);
 		err = -ENODEV;
-		goto err_out;
+		goto err_on_attrib;
 	}
 
 	/* If there are driver level block attributes, then added them
@@ -640,7 +640,7 @@  static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
 		edac_dbg(2, "Failed to register instance '%s'\n",
 			 instance->name);
 		kobject_put(main_kobj);
-		goto err_out;
+		goto edac_device_create_instance;
 	}
 
 	edac_dbg(4, "now register '%d' blocks for instance %d\n",