Skip to content

Commit

Permalink
Merge pull request torvalds#47 from sched-ext/scx-fix-flags-corruption
Browse files Browse the repository at this point in the history
scx: Fix p->scx.flags corruption due to unsynchronized writes of SCX_TASK_ON_DSQ_PRIQ
  • Loading branch information
Byte-Lab authored Sep 20, 2023
2 parents 2c5e6d3 + 21f4c19 commit ee9077a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion include/linux/sched/ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,6 @@ enum scx_ent_flags {
SCX_TASK_QUEUED = 1 << 0, /* on ext runqueue */
SCX_TASK_BAL_KEEP = 1 << 1, /* balance decided to keep current */
SCX_TASK_ENQ_LOCAL = 1 << 2, /* used by scx_select_cpu_dfl() to set SCX_ENQ_LOCAL */
SCX_TASK_ON_DSQ_PRIQ = 1 << 3, /* task is queued on the priority queue of a dsq */

SCX_TASK_OPS_PREPPED = 1 << 8, /* prepared for BPF scheduler enable */
SCX_TASK_OPS_ENABLED = 1 << 9, /* task has BPF scheduler enabled */
Expand All @@ -609,6 +608,11 @@ enum scx_ent_flags {
SCX_TASK_CURSOR = 1 << 31, /* iteration cursor, not a task */
};

/* scx_entity.dsq_flags */
enum scx_ent_dsq_flags {
SCX_TASK_DSQ_ON_PRIQ = 1 << 0, /* task is queued on the priority queue of a dsq */
};

/*
* Mask bits for scx_entity.kf_mask. Not all kfuncs can be called from
* everywhere and the following bits track which kfunc sets are currently
Expand Down Expand Up @@ -646,6 +650,7 @@ struct sched_ext_entity {
} dsq_node;
struct list_head watchdog_node;
u32 flags; /* protected by rq lock */
u32 dsq_flags; /* protected by dsq lock */
u32 weight;
s32 sticky_cpu;
s32 holding_cpu;
Expand Down
8 changes: 4 additions & 4 deletions kernel/sched/ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ static void dispatch_enqueue(struct scx_dispatch_q *dsq, struct task_struct *p,
bool is_local = dsq->id == SCX_DSQ_LOCAL;

WARN_ON_ONCE(p->scx.dsq || !list_empty(&p->scx.dsq_node.fifo));
WARN_ON_ONCE((p->scx.flags & SCX_TASK_ON_DSQ_PRIQ) ||
WARN_ON_ONCE((p->scx.dsq_flags & SCX_TASK_DSQ_ON_PRIQ) ||
!RB_EMPTY_NODE(&p->scx.dsq_node.priq));

if (!is_local) {
Expand All @@ -635,7 +635,7 @@ static void dispatch_enqueue(struct scx_dispatch_q *dsq, struct task_struct *p,
}

if (enq_flags & SCX_ENQ_DSQ_PRIQ) {
p->scx.flags |= SCX_TASK_ON_DSQ_PRIQ;
p->scx.dsq_flags |= SCX_TASK_DSQ_ON_PRIQ;
rb_add_cached(&p->scx.dsq_node.priq, &dsq->priq,
scx_dsq_priq_less);
} else {
Expand Down Expand Up @@ -675,10 +675,10 @@ static void dispatch_enqueue(struct scx_dispatch_q *dsq, struct task_struct *p,
static void task_unlink_from_dsq(struct task_struct *p,
struct scx_dispatch_q *dsq)
{
if (p->scx.flags & SCX_TASK_ON_DSQ_PRIQ) {
if (p->scx.dsq_flags & SCX_TASK_DSQ_ON_PRIQ) {
rb_erase_cached(&p->scx.dsq_node.priq, &dsq->priq);
RB_CLEAR_NODE(&p->scx.dsq_node.priq);
p->scx.flags &= ~SCX_TASK_ON_DSQ_PRIQ;
p->scx.dsq_flags &= ~SCX_TASK_DSQ_ON_PRIQ;
} else {
list_del_init(&p->scx.dsq_node.fifo);
}
Expand Down

0 comments on commit ee9077a

Please sign in to comment.