Skip to content

Commit f892f9f

Browse files
devnexenhtejun
authored andcommitted
tools/sched_ext: scx_userland: fix data races on shared counters
The stats thread reads nr_vruntime_enqueues, nr_vruntime_dispatches, nr_vruntime_failed, and nr_curr_enqueued concurrently with the main thread writing them, with no synchronization. Use __atomic builtins with relaxed ordering for all accesses to these counters to eliminate the data races. Only display accuracy is affected, not scheduling correctness. Signed-off-by: David Carlier <devnexen@gmail.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 625be34 commit f892f9f

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

tools/sched_ext/scx_userland.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ static int dispatch_task(__s32 pid)
157157

158158
err = bpf_map_update_elem(dispatched_fd, NULL, &pid, 0);
159159
if (err) {
160-
nr_vruntime_failed++;
160+
__atomic_add_fetch(&nr_vruntime_failed, 1, __ATOMIC_RELAXED);
161161
} else {
162-
nr_vruntime_dispatches++;
162+
__atomic_add_fetch(&nr_vruntime_dispatches, 1, __ATOMIC_RELAXED);
163163
}
164164

165165
return err;
@@ -202,8 +202,8 @@ static int vruntime_enqueue(const struct scx_userland_enqueued_task *bpf_task)
202202
return ENOENT;
203203

204204
update_enqueued(curr, bpf_task);
205-
nr_vruntime_enqueues++;
206-
nr_curr_enqueued++;
205+
__atomic_add_fetch(&nr_vruntime_enqueues, 1, __ATOMIC_RELAXED);
206+
__atomic_add_fetch(&nr_curr_enqueued, 1, __ATOMIC_RELAXED);
207207

208208
/*
209209
* Enqueue the task in a vruntime-sorted list. A more optimal data
@@ -279,9 +279,9 @@ static void dispatch_batch(void)
279279
LIST_INSERT_HEAD(&vruntime_head, task, entries);
280280
break;
281281
}
282-
nr_curr_enqueued--;
282+
__atomic_sub_fetch(&nr_curr_enqueued, 1, __ATOMIC_RELAXED);
283283
}
284-
skel->bss->nr_scheduled = nr_curr_enqueued;
284+
skel->bss->nr_scheduled = __atomic_load_n(&nr_curr_enqueued, __ATOMIC_RELAXED);
285285
}
286286

287287
static void *run_stats_printer(void *arg)
@@ -306,9 +306,9 @@ static void *run_stats_printer(void *arg)
306306
printf("|-----------------------|\n");
307307
printf("| VRUNTIME / USER |\n");
308308
printf("|-----------------------|\n");
309-
printf("| enq: %10llu |\n", nr_vruntime_enqueues);
310-
printf("| disp: %10llu |\n", nr_vruntime_dispatches);
311-
printf("| failed: %10llu |\n", nr_vruntime_failed);
309+
printf("| enq: %10llu |\n", __atomic_load_n(&nr_vruntime_enqueues, __ATOMIC_RELAXED));
310+
printf("| disp: %10llu |\n", __atomic_load_n(&nr_vruntime_dispatches, __ATOMIC_RELAXED));
311+
printf("| failed: %10llu |\n", __atomic_load_n(&nr_vruntime_failed, __ATOMIC_RELAXED));
312312
printf("o-----------------------o\n");
313313
printf("\n\n");
314314
fflush(stdout);
@@ -376,10 +376,10 @@ static void bootstrap(char *comm)
376376
{
377377
exit_req = 0;
378378
min_vruntime = 0.0;
379-
nr_vruntime_enqueues = 0;
380-
nr_vruntime_dispatches = 0;
381-
nr_vruntime_failed = 0;
382-
nr_curr_enqueued = 0;
379+
__atomic_store_n(&nr_vruntime_enqueues, 0, __ATOMIC_RELAXED);
380+
__atomic_store_n(&nr_vruntime_dispatches, 0, __ATOMIC_RELAXED);
381+
__atomic_store_n(&nr_vruntime_failed, 0, __ATOMIC_RELAXED);
382+
__atomic_store_n(&nr_curr_enqueued, 0, __ATOMIC_RELAXED);
383383
memset(tasks, 0, pid_max * sizeof(*tasks));
384384
LIST_INIT(&vruntime_head);
385385

0 commit comments

Comments
 (0)