All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marius Kittler <mkittler@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v1 1/2] Ensure prio is within valid range in `rt-migrate.c`
Date: Tue, 12 Sep 2023 16:43:50 +0200	[thread overview]
Message-ID: <20230912144351.13494-2-mkittler@suse.de> (raw)
In-Reply-To: <20230912144351.13494-1-mkittler@suse.de>

* According to the documentation the value param->sched_priority
  must lie within the range given by sched_get_priority_min(2) and
  sched_get_priority_max(2). This change ensures that this is the
  case without completely restructuring the test yet.
* See https://github.com/linux-test-project/ltp/issues/812

Signed-off-by: Marius Kittler <mkittler@suse.de>
---
 .../realtime/func/rt-migrate/rt-migrate.c     | 21 ++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/testcases/realtime/func/rt-migrate/rt-migrate.c b/testcases/realtime/func/rt-migrate/rt-migrate.c
index 97ab604c7..2554f63e2 100644
--- a/testcases/realtime/func/rt-migrate/rt-migrate.c
+++ b/testcases/realtime/func/rt-migrate/rt-migrate.c
@@ -74,6 +74,9 @@
 
 #define VERSION_STRING "V 0.4LTP"
 
+#define CLAMP(x, lower, upper) (MIN(upper, MAX(x, lower)))
+#define CLAMP_PRIO(prio) CLAMP(prio, prio_min, prio_max)
+
 int nr_tasks;
 int lfd;
 
@@ -137,7 +140,7 @@ static unsigned long long interval = INTERVAL;
 static unsigned long long run_interval = RUN_INTERVAL;
 static unsigned long long max_err = MAX_ERR;
 static int nr_runs = NR_RUNS;
-static int prio_start = PRIO_START;
+static int prio_start = PRIO_START, prio_min, prio_max;
 static int check = 1;
 static int stop;
 
@@ -284,8 +287,8 @@ static void print_results(void)
 	printf("Parent pid: %d\n", getpid());
 
 	for (t = 0; t < nr_tasks; t++) {
-		printf(" Task %d (prio %d) (pid %ld):\n", t, t + prio_start,
-		       thread_pids[t]);
+		printf(" Task %d (prio %d) (pid %ld):\n", t,
+			   CLAMP_PRIO(t + prio_start), thread_pids[t]);
 		printf("   Max: %lld us\n", tasks_max[t]);
 		printf("   Min: %lld us\n", tasks_min[t]);
 		printf("   Tot: %lld us\n", tasks_avg[t] * nr_runs);
@@ -394,6 +397,13 @@ static void stop_log(int sig)
 
 int main(int argc, char **argv)
 {
+	/*
+	 * Determine the valid priority range; subtracting one from the
+	 * maximum to reserve the highest prio for main thread.
+	 */
+	prio_min = sched_get_priority_min(SCHED_FIFO);
+	prio_max = sched_get_priority_max(SCHED_FIFO) - 1;
+
 	int *threads;
 	long i;
 	int ret;
@@ -448,7 +458,7 @@ int main(int argc, char **argv)
 
 	for (i = 0; i < nr_tasks; i++) {
 		threads[i] = create_fifo_thread(start_task, (void *)i,
-						prio_start + i);
+						CLAMP_PRIO(prio_start + i));
 	}
 
 	/*
@@ -460,7 +470,8 @@ int main(int argc, char **argv)
 
 	/* up our prio above all tasks */
 	memset(&param, 0, sizeof(param));
-	param.sched_priority = nr_tasks + prio_start;
+	param.sched_priority = CLAMP(nr_tasks + prio_start, prio_min,
+								 prio_max + 1);
 	if (sched_setscheduler(0, SCHED_FIFO, &param))
 		debug(DBG_WARN, "Warning, can't set priority of main thread!\n");
 	intv.tv_sec = INTERVAL / NS_PER_SEC;
-- 
2.42.0


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

  reply	other threads:[~2023-09-12 14:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-12 14:43 [LTP] [PATCH v1 0/2] Fix most severe problems with `rt-migrate.c` Marius Kittler
2023-09-12 14:43 ` Marius Kittler [this message]
2023-09-13  9:11   ` [LTP] [PATCH v1 1/2] Ensure prio is within valid range in `rt-migrate.c` Andrea Cervesato via ltp
2023-09-13  9:27     ` Marius Kittler
2023-11-13  9:31       ` Richard Palethorpe
2023-09-12 14:43 ` [LTP] [PATCH v1 2/2] Prevent segmentation fault when negative task count specified Marius Kittler
2023-09-13  8:54   ` Andrea Cervesato via ltp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230912144351.13494-2-mkittler@suse.de \
    --to=mkittler@suse.de \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.