History

sched_ext: Defer scx_sched kobj sysfs add into the enable workfns

Split kobject_init_and_add() in scx_alloc_and_add_sched(): only kobject_init() runs there. A new scx_sched_sysfs_add() helper does kobject_add() (and creates sub_kset when the scheduler implements ops.sub_attach), called by both enable workfns once @sch is linked and its sysfs-visible state is initialized. Prep so a future caps attribute can rely on @sch being fully built by the time it's sysfs-visible. Add early enough that a stall later in enable still leaves sysfs inspectable.

Tejun Heo committed 4 months ago

Commit 80e6ada1 parent 30067643 files · +53 −29Browse files

Unified · Split

3 files
  1. kernel/sched/ext/ext.c +45 −28
  2. kernel/sched/ext/internal.h +1 −0
  3. kernel/sched/ext/sub.c +7 −1
kernel/sched/ext/ext.c+45−28
@@ -5735,7 +5735,9 @@ static void scx_root_disable(struct scx_sched *sch) · 5734 unmodified lines
57355735 if (sch->sub_kset)
57365736 kobject_del(&sch->sub_kset->kobj);
57375737 #endif
5738− kobject_del(&sch->kobj);
5738+ /* not added if enable failed before scx_sched_sysfs_add() */
5739+ if (sch->kobj.state_in_sysfs)
5740+ kobject_del(&sch->kobj);
57395741
57405742 free_kick_syncs();
57415743
@@ -6454,36 +6456,15 @@ struct scx_sched *scx_alloc_and_add_sched(struct scx_enable_cmd *cmd, · 712 unmodified lines
64546456 * disable. Released in scx_sched_free_rcu_work().
64556457 */
64566458 kobject_get(&parent->kobj);
6457− ret = kobject_init_and_add(&sch->kobj, &scx_ktype,
6458− &parent->sub_kset->kobj,
6459− "sub-%llu", cgroup_id(cgrp));
6460− } else {
6461− ret = kobject_init_and_add(&sch->kobj, &scx_ktype, NULL, "root");
6462− }
6463−
6464− if (ret < 0) {
6465− RCU_INIT_POINTER(ops->priv, NULL);
6466− kobject_put(&sch->kobj);
6467− return ERR_PTR(ret);
6468− }
6469−
6470− if (ops->sub_attach) {
6471− sch->sub_kset = kset_create_and_add("sub", NULL, &sch->kobj);
6472− if (!sch->sub_kset) {
6473− RCU_INIT_POINTER(ops->priv, NULL);
6474− kobject_put(&sch->kobj);
6475− return ERR_PTR(-ENOMEM);
6476− }
6477− }
6478−#else /* CONFIG_EXT_SUB_SCHED */
6479− ret = kobject_init_and_add(&sch->kobj, &scx_ktype, NULL, "root");
6480− if (ret < 0) {
6481− RCU_INIT_POINTER(ops->priv, NULL);
6482− kobject_put(&sch->kobj);
6483− return ERR_PTR(ret);
64846459 }
64856460 #endif /* CONFIG_EXT_SUB_SCHED */
64866461
6462+ /*
6463+ * Init the kobj but don't add to sysfs yet. The enable path calls
6464+ * scx_sched_sysfs_add() once @sch's sysfs-visible state is initialized.
6465+ */
6466+ kobject_init(&sch->kobj, &scx_ktype);
6467+
64876468 /*
64886469 * Consume the arena_map ref bpf_scx_reg_cid() took. Defer to here so
64896470 * earlier failure paths leave cmd->arena_map set and bpf_scx_reg_cid
@@ -6541,6 +6522,36 @@ err_put_cgrp: · 51 unmodified lines
65416522 return ERR_PTR(ret);
65426523 }
65436524
6525+/*
6526+ * Add @sch's kobject to sysfs, and create its sub_kset if the scheduler
6527+ * implements ops.sub_attach. Called by the enable workfns once @sch's
6528+ * sysfs-visible state is initialized.
6529+ */
6530+int scx_sched_sysfs_add(struct scx_sched *sch)
6531+{
6532+#ifdef CONFIG_EXT_SUB_SCHED
6533+ struct scx_sched *parent = scx_parent(sch);
6534+ int ret;
6535+
6536+ if (parent)
6537+ ret = kobject_add(&sch->kobj, &parent->sub_kset->kobj,
6538+ "sub-%llu", cgroup_id(sch_cgroup(sch)));
6539+ else
6540+ ret = kobject_add(&sch->kobj, NULL, "root");
6541+ if (ret < 0)
6542+ return ret;
6543+
6544+ if (sch->ops.sub_attach) {
6545+ sch->sub_kset = kset_create_and_add("sub", NULL, &sch->kobj);
6546+ if (!sch->sub_kset)
6547+ return -ENOMEM;
6548+ }
6549+ return 0;
6550+#else
6551+ return kobject_add(&sch->kobj, NULL, "root");
6552+#endif
6553+}
6554+
65446555 static int check_hotplug_seq(struct scx_sched *sch,
65456556 const struct sched_ext_ops *ops)
65466557 {
@@ -6763,6 +6774,12 @@ static void scx_root_enable_workfn(struct kthread_work *work) · 216 unmodified lines
67636774 sch->exit_info->flags |= SCX_EFLAG_INITIALIZED;
67646775 }
67656776
6777+ ret = scx_sched_sysfs_add(sch);
6778+ if (ret) {
6779+ cpus_read_unlock();
6780+ goto err_disable;
6781+ }
6782+
67666783 for (i = SCX_OPI_CPU_HOTPLUG_BEGIN; i < SCX_OPI_CPU_HOTPLUG_END; i++)
67676784 if (((void (**)(void))ops)[i])
67686785 set_bit(i, sch->has_op);
kernel/sched/ext/internal.h+1−0
@@ -1690,6 +1690,7 @@ struct scx_sched *scx_alloc_and_add_sched(struct scx_enable_cmd *cmd, · 1689 unmodified lines
16901690 struct cgroup *cgrp,
16911691 struct scx_sched *parent);
16921692 int scx_validate_ops(struct scx_sched *sch, const struct sched_ext_ops *ops);
1693+int scx_sched_sysfs_add(struct scx_sched *sch);
16931694
16941695 extern raw_spinlock_t scx_sched_lock;
16951696 extern struct mutex scx_enable_mutex;
kernel/sched/ext/sub.c+7−1
@@ -270,7 +270,9 @@ void scx_sub_disable(struct scx_sched *sch) · 269 unmodified lines
270270 SCX_CALL_OP(sch, exit, NULL, sch->exit_info);
271271 if (sch->sub_kset)
272272 kobject_del(&sch->sub_kset->kobj);
273− kobject_del(&sch->kobj);
273+ /* not added if enable failed before scx_sched_sysfs_add() */
274+ if (sch->kobj.state_in_sysfs)
275+ kobject_del(&sch->kobj);
274276 }
275277
276278 /* verify that a scheduler can be attached to @cgrp and return the parent */
@@ -363,6 +365,10 @@ void scx_sub_enable_workfn(struct kthread_work *work) · 86 unmodified lines
363365 if (ret)
364366 goto err_disable;
365367
368+ ret = scx_sched_sysfs_add(sch);
369+ if (ret)
370+ goto err_disable;
371+
366372 if (sch->level >= SCX_SUB_MAX_DEPTH) {
367373 scx_error(sch, "max nesting depth %d violated",
368374 SCX_SUB_MAX_DEPTH);