[PATCH] EDAC/sysfs: Fix error handling in edac_device_create_instance()

Guangshuo Li posted 1 patch 1 month, 2 weeks ago
drivers/edac/edac_device_sysfs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] EDAC/sysfs: Fix error handling in edac_device_create_instance()
Posted by Guangshuo Li 1 month, 2 weeks ago
Once kobject_init_and_add() failed, we should call kobject_put() to
decrement the reference count of the initialized kobject. Or it could
cause a reference count leak.

The release callback of the instance kobject drops the reference of the
main kobject, so replace kobject_put(main_kobj) with
kobject_put(&instance->kobj).

As comment of kobject_init_and_add() says, if this function returns an
error, kobject_put() must be called to properly clean up the memory
associated with the object.

This issue was found by a static analysis tool I am developing.

Fixes: b2ed215a3338 ("Kobject: change drivers/edac to use kobject_init_and_add")
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/edac/edac_device_sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/edac/edac_device_sysfs.c b/drivers/edac/edac_device_sysfs.c
index 32460c6dfb7c..0f5bc2f1fa5e 100644
--- a/drivers/edac/edac_device_sysfs.c
+++ b/drivers/edac/edac_device_sysfs.c
@@ -626,7 +626,7 @@ static int edac_device_create_instance(struct edac_device_ctl_info *edac_dev,
 	if (err != 0) {
 		edac_dbg(2, "Failed to register instance '%s'\n",
 			 instance->name);
-		kobject_put(main_kobj);
+		kobject_put(&instance->kobj);
 		goto err_out;
 	}
 
-- 
2.43.0
RE: [PATCH] EDAC/sysfs: Fix error handling in edac_device_create_instance()
Posted by Zhuo, Qiuxu 1 month, 2 weeks ago
Hi Guangshuo,

> From: Guangshuo Li <lgs201920130244@gmail.com>
> Sent: Thursday, April 30, 2026 8:00 PM
> To: Borislav Petkov <bp@alien8.de>; Luck, Tony <tony.luck@intel.com>; Doug
> Thompson <dougthompson@xmission.com>; Greg Kroah-Hartman
> <gregkh@suse.de>; linux-edac@vger.kernel.org; linux-kernel@vger.kernel.org
> Cc: Guangshuo Li <lgs201920130244@gmail.com>
> Subject: [PATCH] EDAC/sysfs: Fix error handling in
> edac_device_create_instance()
> 
> Once kobject_init_and_add() failed, we should call kobject_put() to
> decrement the reference count of the initialized kobject. Or it could cause a
> reference count leak.
> 
> The release callback of the instance kobject drops the reference of the main
> kobject, so replace kobject_put(main_kobj) with kobject_put(&instance-
> >kobj).
> 
> As comment of kobject_init_and_add() says, if this function returns an error,
> kobject_put() must be called to properly clean up the memory associated
> with the object.
> 
> This issue was found by a static analysis tool I am developing.
> 

You may run: 

   grep -rn "kobject_init_and_add" drivers/edac/ 

and see there are several other places that need the same fix, like [1], [2] etc.
 
Could you please address all of them in your previous patch [3] instead of fixing only part of them?

[1] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/edac/edac_device_sysfs.c#n519
[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/edac/edac_pci_sysfs.c#n175
...
[3] https://lore.kernel.org/all/20260428160828.377129-1-lgs201920130244@gmail.com/

Thanks!
-Qiuxu
Re: [PATCH] EDAC/sysfs: Fix error handling in edac_device_create_instance()
Posted by Guangshuo Li 1 month, 2 weeks ago
Hi Qiuxu,

Thanks for your review and suggestion.

On Thu, 30 Apr 2026 at 22:59, Zhuo, Qiuxu <qiuxu.zhuo@intel.com> wrote:
>
> You may run:
>
>    grep -rn "kobject_init_and_add" drivers/edac/
>
> and see there are several other places that need the same fix, like [1], [2] etc.
>
> Could you please address all of them in your previous patch [3] instead of fixing only part of them?
>
> [1] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/edac/edac_device_sysfs.c#n519
> [2] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/edac/edac_pci_sysfs.c#n175
> ...
> [3] https://lore.kernel.org/all/20260428160828.377129-1-lgs201920130244@gmail.com/
>
> Thanks!
> -Qiuxu

I will check all kobject_init_and_add() error paths under
drivers/edac/ and address the similar issues together in a v2 patch.

Thanks,
Guangshuo
RE: [PATCH] EDAC/sysfs: Fix error handling in edac_device_create_instance()
Posted by Zhuo, Qiuxu 1 month, 2 weeks ago
> From: Guangshuo Li <lgs201920130244@gmail.com>
> [...]
> Subject: Re: [PATCH] EDAC/sysfs: Fix error handling in
> edac_device_create_instance()
> [...]
> 
> I will check all kobject_init_and_add() error paths under drivers/edac/ and
> address the similar issues together in a v2 patch.
> 

Thanks for your updated v3 patch.

-Qiuxu