[PATCH] EDAC/sysfs: Fix error handling in edac_device_create_instance()
From: Guangshuo Li
Date: Thu Apr 30 2026 - 08:02:13 EST
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@xxxxxxxxx>
---
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