Skip to contentSkip to conversation

sched_football: synchronize with kickoff flag to reduce skew

PATCH Superseded Li Wang via ltp 3 participants 6 messages Sep 4, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_football: synchronize with kickoff flag to reduce skew

Previously, thread synchronization in sched_football only relied on a thread_barrier. This ensured that all threads were created before the referee started the game, but did not fully prevent offense threads from getting a scheduling opportunity before defense threads were migrated, leading to occasional non-zero final ball positions on kvm or debug kernels.

This patch introduces an explicit kickoff_flag:

* All player threads (offense, defense, fans) wait at the barrier and then spin until the referee sets kickoff_flag. This reduces kernel scheduling skew, as threads only proceed once the referee explicitly signals the kickoff.

* The referee now:
- Waits at the barrier.
- Clears the ball position.
- Sets kickoff_flag to start the ball.

* Game termination is also slightly reordered (by Cyril):
- Final ball position is read before game_over is set, avoiding a race where the ball could still move right after defense threads stop.

* Only test on RT-kernels.

This makes startup sequencing more deterministic while still allowing some nondeterminism, which is intentional for testing scheduler behavior under load.

---
 .../func/sched_football/sched_football.c      | 27 ++++++++++++++-----
 1 file changed, 21 insertions(+), 6 deletions(-)
testcases/realtime/func/sched_football/sched_football.c +21 -6
index 0617bdb87..97ac9d413 100644
@@ -44,6 +44,7 @@
 static tst_atomic_t the_ball;
 static int players_per_team = 0;
 static int game_length = DEF_GAME_LENGTH;
+static tst_atomic_t kickoff_flag;
 static tst_atomic_t game_over;
 
 static char *str_game_length;
@@ -55,6 +56,9 @@ void *thread_fan(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	prctl(PR_SET_NAME, "crazy_fan", 0, 0, 0);
 	pthread_barrier_wait(&start_barrier);
+	while (!tst_atomic_load(&kickoff_flag))
+		;
+
 	/*occasionally wake up and run across the field */
 	while (!tst_atomic_load(&game_over)) {
 		struct timespec start, stop;
@@ -80,6 +84,9 @@ void *thread_defense(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	prctl(PR_SET_NAME, "defense", 0, 0, 0);
 	pthread_barrier_wait(&start_barrier);
+	while (!tst_atomic_load(&kickoff_flag))
+		;
+
 	/*keep the ball from being moved */
 	while (!tst_atomic_load(&game_over)) {
 	}
@@ -92,6 +99,9 @@ void *thread_offense(void *arg LTP_ATTRIBUTE_UNUSED)
 {
 	prctl(PR_SET_NAME, "offense", 0, 0, 0);
 	pthread_barrier_wait(&start_barrier);
+	while (!tst_atomic_load(&kickoff_flag))
+		;
+
 	while (!tst_atomic_load(&game_over)) {
 		tst_atomic_add_return(1, &the_ball); /* move the ball ahead one yard */
 	}
@@ -115,9 +125,10 @@ void referee(int game_length)
 	now = start;
 
 	/* Start the game! */
-	tst_atomic_store(0, &the_ball);
-	pthread_barrier_wait(&start_barrier);
 	atrace_marker_write("sched_football", "Game_started!");
+	pthread_barrier_wait(&start_barrier);
+	tst_atomic_store(0, &the_ball);
+	tst_atomic_store(1, &kickoff_flag);
 
 	/* Watch the game */
 	while ((now.tv_sec - start.tv_sec) < game_length) {
@@ -125,14 +136,14 @@ void referee(int game_length)
 		gettimeofday(&now, NULL);
 	}
 
-	/* Stop the game! */
-	tst_atomic_store(1, &game_over);
-	atrace_marker_write("sched_football", "Game_Over!");
-
 	/* Blow the whistle */
 	final_ball = tst_atomic_load(&the_ball);
 	tst_res(TINFO, "Final ball position: %d", final_ball);
 
+	/* Stop the game! */
+	tst_atomic_store(1, &game_over);
+	atrace_marker_write("sched_football", "Game_Over!");
+
 	TST_EXP_EXPR(final_ball == 0);
 }
 
@@ -154,6 +165,7 @@ static void do_test(void)
 	/* We're the ref, so set our priority right */
 	param.sched_priority = sched_get_priority_min(SCHED_FIFO) + 80;
 	sched_setscheduler(0, SCHED_FIFO, &param);
+	tst_atomic_store(0, &kickoff_flag);
 
 	/*
 	 * Start the offense
@@ -186,6 +198,9 @@ static void do_test(void)
 
 static void do_setup(void)
 {
+	if (!tst_check_preempt_rt())
+		tst_brk(TCONF, "Test requires real-time kernel");
+
 	if (tst_parse_int(str_game_length, &game_length, 1, INT_MAX))
 		tst_brk(TBROK, "Invalid game length '%s'", str_game_length);
 
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#2 lore raw
In reply to Li Wang via ltp

Hi Li,

...

testcases/realtime/func/sched_football/sched_football.c static void do_test(void) · line 202

Quoted 4 lines

static void do_setup(void)
{

testcases/realtime/func/sched_football/sched_football.c +2 -0
+	if (!tst_check_preempt_rt())
+		tst_brk(TCONF, "Test requires real-time kernel");

I guess this itself fix many false positives :). I'm ok with requesting RT kernel, but just to remind the claim originated from Cyril [1] "sched_football is useful not only for testing realtime". So was Cyril wrong?

(I'm ok to keep the test in runtest/sched even requesting RT kernel, because runtest/sched and other LTP tests are run on both RT and non-RT.

The rest LGTM, thanks for stabilising the test.

Kind regards,
Petr

[1] https://github.com/linux-test-project/ltp/commit/3a8e38fc2c46c886525bbb708dff462c6b633d32

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi!

Quoted 8 lines

testcases/realtime/func/sched_football/sched_football.c static void do_test(void) · line 202

Quoted 4 lines

static void do_setup(void)
{

testcases/realtime/func/sched_football/sched_football.c +2 -0
+	if (!tst_check_preempt_rt())
+		tst_brk(TCONF, "Test requires real-time kernel");

I guess this itself fix many false positives :). I'm ok with requesting RT kernel, but just to remind the claim originated from Cyril [1] "sched_football is useful not only for testing realtime". So was Cyril wrong?

No Cyril wasn't wrong. The realtime schedulling priorities in kernel are completely different thing from realtime kernel patchset.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 6 lines · Cyril Hrubis

Hi!

Quoted 5 lines
Quoted 4 lines

static void do_setup(void)
{

testcases/realtime/func/sched_football/sched_football.c +2 -0
+   if (!tst_check_preempt_rt())
+           tst_brk(TCONF, "Test requires real-time kernel");
Quoted 9 lines
Quoted 1 line

I guess this itself fix many false positives :). I'm ok with requesting

RT

Quoted 1 line

kernel, but just to remind the claim originated from Cyril [1]

"sched_football

Quoted 1 line

is useful not only for testing realtime". So was Cyril wrong?

No Cyril wasn't wrong. The realtime schedulling priorities in kernel are completely different thing from realtime kernel patchset.

Yes, we can set up processes with SCHED_FIFO or SCHED_RR policies on the stock kernel, but it's not the same thing as the PREEMPT_RT kernel.

I guess we don't need to do more tries on test with sched_setscheduler(pid, SCHED_FIFO) situation just to avoid false positives, because there might be no perfect solution.

So I'd prefer to keep sched_football tested only on real-time kernels.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Hi!

Quoted 6 lines
Quoted 3 lines

No Cyril wasn't wrong. The realtime schedulling priorities in kernel are completely different thing from realtime kernel patchset.

Yes, we can set up processes with SCHED_FIFO or SCHED_RR policies on the stock kernel, but it's not the same thing as the PREEMPT_RT kernel.

Honestly it is, the difference with PREEMT_RT is that the scheduller is more aggressive, which probably explains why things settle down into a steady state faster after the barrier.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp