Skip to contentSkip to conversation

sched_ext: Use kobject_put() for kobject_init_and_add() failure in scx_alloc_and_add_sched()

PATCH In review David Carlier 2 participants 3 messages Mar 14 – Mar 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_ext: Use kobject_put() for kobject_init_and_add() failure in scx_alloc_and_add_sched()

When kobject_init_and_add() fails, the error path jumps to err_stop_helper which eventually calls kfree(sch) directly. However, kobject_init_and_add() internally calls kobject_init() which initializes the refcount and may allocate the name string. As documented in lib/kobject.c:

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

Use kobject_put() which triggers scx_kobj_release() and scx_sched_free_rcu_work(), handling cleanup of all previously allocated resources.

Fixes 17108735b47d ("sched_ext: Use dynamic allocation for scx_sched")
---
 kernel/sched/ext.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
kernel/sched/ext.c +14 -4
index 9202c6d7a771..c35c13da5a8f 100644
@@ -6468,8 +6468,12 @@ static struct scx_sched *scx_alloc_and_add_sched(struct sched_ext_ops *ops,
 		ret = kobject_init_and_add(&sch->kobj, &scx_ktype, NULL, "root");
 
 	if (ret < 0) {
-		kfree(sch->cgrp_path);
-		goto err_stop_helper;
+		/*
+		 * kobject was initialized, kobject_put() needed for cleanup,
+		 * see Documentation/core-api/kobject.rst
+		 */
+		kobject_put(&sch->kobj);
+		return ERR_PTR(ret);
 	}
 
 	if (ops->sub_attach) {
@@ -6482,8 +6486,14 @@ static struct scx_sched *scx_alloc_and_add_sched(struct sched_ext_ops *ops,
 
 #else	/* CONFIG_EXT_SUB_SCHED */
 	ret = kobject_init_and_add(&sch->kobj, &scx_ktype, NULL, "root");
-	if (ret < 0)
-		goto err_stop_helper;
+	if (ret < 0) {
+		/*
+		 * kobject was initialized, kobject_put() needed for cleanup,
+		 * see Documentation/core-api/kobject.rst
+		 */
+		kobject_put(&sch->kobj);
+		return ERR_PTR(ret);
+	}
 #endif	/* CONFIG_EXT_SUB_SCHED */
 	return sch;
 
Signature · 1 line
2.51.0
reviewer
#1 lore raw

Hello,

Thanks for pointing this out. Using kobject_put() is the right approach but the release callback (scx_sched_free_rcu_work) also calls cgroup_put() on sch->cgrp, which was already set at that point, leading to a double-put with the caller.

I posted a patchset fixing this along with a pre-existing cgroup double-put on the abort path:

https://lore.kernel.org/r/[email protected]

Thanks.
--
tejun

#2 lore raw

ah yes I ve seen your patchset, it is better indeed.

Cheers.

Quoted 16 lines · Tejun Heo

Hello,

Thanks for pointing this out. Using kobject_put() is the right approach but the release callback (scx_sched_free_rcu_work) also calls cgroup_put() on sch->cgrp, which was already set at that point, leading to a double-put with the caller.

I posted a patchset fixing this along with a pre-existing cgroup double-put on the abort path:

https://lore.kernel.org/r/[email protected]

Thanks.
--
tejun