Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew
@ 2025-09-04  6:41 Li Wang via ltp
  2025-09-04  7:20 ` Andrea Cervesato via ltp
  2025-09-04  9:48 ` Petr Vorel
  0 siblings, 2 replies; 6+ messages in thread
From: Li Wang via ltp @ 2025-09-04  6:41 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Li Wang <liwang@redhat.com>
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
Cc: Andrea Cervesato <andrea.cervesato@suse.com>
---
 .../func/sched_football/sched_football.c      | 27 ++++++++++++++-----
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/testcases/realtime/func/sched_football/sched_football.c b/testcases/realtime/func/sched_football/sched_football.c
index 0617bdb87..97ac9d413 100644
--- a/testcases/realtime/func/sched_football/sched_football.c
+++ b/testcases/realtime/func/sched_football/sched_football.c
@@ -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);
 
-- 
2.51.0


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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew
  2025-09-04  6:41 [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew Li Wang via ltp
@ 2025-09-04  7:20 ` Andrea Cervesato via ltp
  2025-09-04  9:48 ` Petr Vorel
  1 sibling, 0 replies; 6+ messages in thread
From: Andrea Cervesato via ltp @ 2025-09-04  7:20 UTC (permalink / raw)
  To: Li Wang, ltp

Hi!

Tested-by: Andrea Cervesato <andrea.cervesato@suse.com>

- Andrea


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew
  2025-09-04  6:41 [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew Li Wang via ltp
  2025-09-04  7:20 ` Andrea Cervesato via ltp
@ 2025-09-04  9:48 ` Petr Vorel
  2025-09-04  9:54   ` Cyril Hrubis
  1 sibling, 1 reply; 6+ messages in thread
From: Petr Vorel @ 2025-09-04  9:48 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp

Hi Li,

...
>  static void do_setup(void)
>  {
> +	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.

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew
  2025-09-04  9:48 ` Petr Vorel
@ 2025-09-04  9:54   ` Cyril Hrubis
  2025-09-04 10:04     ` Li Wang via ltp
  0 siblings, 1 reply; 6+ messages in thread
From: Cyril Hrubis @ 2025-09-04  9:54 UTC (permalink / raw)
  To: Petr Vorel; +Cc: ltp

Hi!
> >  static void do_setup(void)
> >  {
> > +	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.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew
  2025-09-04  9:54   ` Cyril Hrubis
@ 2025-09-04 10:04     ` Li Wang via ltp
  2025-09-04 10:08       ` Cyril Hrubis
  0 siblings, 1 reply; 6+ messages in thread
From: Li Wang via ltp @ 2025-09-04 10:04 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: ltp

On Thu, Sep 4, 2025 at 5:53 PM Cyril Hrubis <chrubis@suse.cz> wrote:

> Hi!
> > >  static void do_setup(void)
> > >  {
> > > +   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.
>

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.


-- 
Regards,
Li Wang

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew
  2025-09-04 10:04     ` Li Wang via ltp
@ 2025-09-04 10:08       ` Cyril Hrubis
  0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2025-09-04 10:08 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp

Hi!
> > 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.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-09-04 10:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-04  6:41 [LTP] [PATCH] sched_football: synchronize with kickoff flag to reduce skew Li Wang via ltp
2025-09-04  7:20 ` Andrea Cervesato via ltp
2025-09-04  9:48 ` Petr Vorel
2025-09-04  9:54   ` Cyril Hrubis
2025-09-04 10:04     ` Li Wang via ltp
2025-09-04 10:08       ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox