From nobody Fri Dec 19 22:04:05 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BE4418C907; Tue, 30 Jul 2024 17:06:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722359201; cv=none; b=s2qnXjWV7tFp3EhQY66bH6MklyfeOC3eGny9ls8Xz2zbGiTDflPaIGtvdqB/iAcj2d6D96YydgWJNaGG+vzzkbNlsJKD5XIAzK5kj5TvTrkxKHjMmT975YcTLl3aMXMsIDofXEwGdtdVOvXook+yJLNplcV/z5RjFp5rQWytdBs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722359201; c=relaxed/simple; bh=VpNcHbhJMVkQzK37gufq1qJOirGgZDBUxMIfqM1WMJk=; h=Date:From:To:Subject:Cc:MIME-Version:Message-ID:Content-Type; b=SsC77sFUy5ElDRri37nS5VOH/ACUY7tFmaqOJmEkWFbFTvr+lxpM7GCpR+8rPn1Aog/JIzONVKI8222yf3HAS05lkz1/oqWoWTLbqOum2onBR7PeO/9iOzt7bht2068cBde++rLKMb4wpieG359oQ2puNU3o6/0BnskA7nSeH3I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=QoOEndaI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=b3WVuPaV; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="QoOEndaI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="b3WVuPaV" Date: Tue, 30 Jul 2024 17:06:37 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722359198; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1GSNgRge/ShNllYXBvKwjEQdJwmkvEYVSVbbURbu02k=; b=QoOEndaITTFtsNVorfpCqJV0hF1/WDFOjwJukwdq+Vrhl/zZ29uCUuslI9NnQZTE11ZM8M 2Fnrg1VM6gNW+MgI3rGdGlrcuVovKfV9RiRZSSm9yHU3Kzj1iXNCN66uqdT08Pxwa1WDfi 9ZFBHw52C/DhmQtxxjcizVBFt0H48ZBws7kmkJHTN9qozn3fehnQaw64WOJDr3QTE8tflB JpKCAvdDxGmkjAFXdScsHHLjudnH35jhCQ2ptPtBUybT7tjKU1iEkqrWR6qiiZkpbLo7AS zZVH5do9HYwdVj/eJ0c3Su3IcDt5pD8RRyXwC83RCUEIYx64iKrpAOJBSKvKdA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722359198; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1GSNgRge/ShNllYXBvKwjEQdJwmkvEYVSVbbURbu02k=; b=b3WVuPaVOAIQNADjy8/N9yk7Kawo32E+mQX3wOTtSH1hkOMP5hjrPdN93vXtyECiUWw86+ chWkJT57b8Iu7hBQ== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] selftests/timers/posix_timers: Simplify error handling Cc: Thomas Gleixner , Frederic Weisbecker , "Anna-Maria Behnsen" , x86@kernel.org, linux-kernel@vger.kernel.org Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <172235919756.2215.8366740488070916839.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the timers/core branch of tip: Commit-ID: 0af02a8e356fc6d3b1eebb32fae7d35625127835 Gitweb: https://git.kernel.org/tip/0af02a8e356fc6d3b1eebb32fae7d3562= 5127835 Author: Thomas Gleixner AuthorDate: Mon, 10 Jun 2024 18:42:06 +02:00 Committer: Frederic Weisbecker CommitterDate: Mon, 29 Jul 2024 21:57:34 +02:00 selftests/timers/posix_timers: Simplify error handling No point in returning to main() on fatal errors. Just exit right away. Signed-off-by: Thomas Gleixner Signed-off-by: Frederic Weisbecker Reviewed-by: Anna-Maria Behnsen --- tools/testing/selftests/timers/posix_timers.c | 151 +++++------------ 1 file changed, 52 insertions(+), 99 deletions(-) diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/= selftests/timers/posix_timers.c index 07c81c0..38db82c 100644 --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,20 @@ #define DELAY 2 #define USECS_PER_SEC 1000000 =20 +static void __fatal_error(const char *test, const char *name, const char *= what) +{ + char buf[64]; + + strerror_r(errno, buf, sizeof(buf)); + + if (name && strlen(name)) + ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, buf); + else + ksft_exit_fail_msg("%s %s %s\n", test, what, buf); +} + +#define fatal_error(name, what) __fatal_error(__func__, name, what) + static volatile int done; =20 /* Busy loop in userspace to elapse ITIMER_VIRTUAL */ @@ -74,24 +89,13 @@ static int check_diff(struct timeval start, struct time= val end) return 0; } =20 -static int check_itimer(int which) +static void check_itimer(int which, const char *name) { - const char *name; - int err; struct timeval start, end; struct itimerval val =3D { .it_value.tv_sec =3D DELAY, }; =20 - if (which =3D=3D ITIMER_VIRTUAL) - name =3D "ITIMER_VIRTUAL"; - else if (which =3D=3D ITIMER_PROF) - name =3D "ITIMER_PROF"; - else if (which =3D=3D ITIMER_REAL) - name =3D "ITIMER_REAL"; - else - return -1; - done =3D 0; =20 if (which =3D=3D ITIMER_VIRTUAL) @@ -101,17 +105,11 @@ static int check_itimer(int which) else if (which =3D=3D ITIMER_REAL) signal(SIGALRM, sig_handler); =20 - err =3D gettimeofday(&start, NULL); - if (err < 0) { - ksft_perror("Can't call gettimeofday()"); - return -1; - } + if (gettimeofday(&start, NULL) < 0) + fatal_error(name, "gettimeofday()"); =20 - err =3D setitimer(which, &val, NULL); - if (err < 0) { - ksft_perror("Can't set timer"); - return -1; - } + if (setitimer(which, &val, NULL) < 0) + fatal_error(name, "setitimer()"); =20 if (which =3D=3D ITIMER_VIRTUAL) user_loop(); @@ -120,68 +118,41 @@ static int check_itimer(int which) else if (which =3D=3D ITIMER_REAL) idle_loop(); =20 - err =3D gettimeofday(&end, NULL); - if (err < 0) { - ksft_perror("Can't call gettimeofday()"); - return -1; - } + if (gettimeofday(&end, NULL) < 0) + fatal_error(name, "gettimeofday()"); =20 ksft_test_result(check_diff(start, end) =3D=3D 0, "%s\n", name); - - return 0; } =20 -static int check_timer_create(int which) +static void check_timer_create(int which, const char *name) { - const char *type; - int err; - timer_t id; struct timeval start, end; struct itimerspec val =3D { .it_value.tv_sec =3D DELAY, }; - - if (which =3D=3D CLOCK_THREAD_CPUTIME_ID) { - type =3D "thread"; - } else if (which =3D=3D CLOCK_PROCESS_CPUTIME_ID) { - type =3D "process"; - } else { - ksft_print_msg("Unknown timer_create() type %d\n", which); - return -1; - } + timer_t id; =20 done =3D 0; - err =3D timer_create(which, NULL, &id); - if (err < 0) { - ksft_perror("Can't create timer"); - return -1; - } - signal(SIGALRM, sig_handler); =20 - err =3D gettimeofday(&start, NULL); - if (err < 0) { - ksft_perror("Can't call gettimeofday()"); - return -1; - } + if (timer_create(which, NULL, &id) < 0) + fatal_error(name, "timer_create()"); =20 - err =3D timer_settime(id, 0, &val, NULL); - if (err < 0) { - ksft_perror("Can't set timer"); - return -1; - } + if (signal(SIGALRM, sig_handler) =3D=3D SIG_ERR) + fatal_error(name, "signal()"); + + if (gettimeofday(&start, NULL) < 0) + fatal_error(name, "gettimeofday()"); + + if (timer_settime(id, 0, &val, NULL) < 0) + fatal_error(name, "timer_settime()"); =20 user_loop(); =20 - err =3D gettimeofday(&end, NULL); - if (err < 0) { - ksft_perror("Can't call gettimeofday()"); - return -1; - } + if (gettimeofday(&end, NULL) < 0) + fatal_error(name, "gettimeofday()"); =20 ksft_test_result(check_diff(start, end) =3D=3D 0, - "timer_create() per %s\n", type); - - return 0; + "timer_create() per %s\n", name); } =20 static pthread_t ctd_thread; @@ -209,15 +180,14 @@ static void *ctd_thread_func(void *arg) =20 ctd_count =3D 100; if (timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id)) - return "Can't create timer\n"; + fatal_error(NULL, "timer_create()"); if (timer_settime(id, 0, &val, NULL)) - return "Can't set timer\n"; - + fatal_error(NULL, "timer_settime()"); while (ctd_count > 0 && !ctd_failed) ; =20 if (timer_delete(id)) - return "Can't delete timer\n"; + fatal_error(NULL, "timer_delete()"); =20 return NULL; } @@ -225,19 +195,16 @@ static void *ctd_thread_func(void *arg) /* * Test that only the running thread receives the timer signal. */ -static int check_timer_distribution(void) +static void check_timer_distribution(void) { - const char *errmsg; - - signal(SIGALRM, ctd_sighandler); + if (signal(SIGALRM, ctd_sighandler) =3D=3D SIG_ERR) + fatal_error(NULL, "signal()"); =20 - errmsg =3D "Can't create thread\n"; if (pthread_create(&ctd_thread, NULL, ctd_thread_func, NULL)) - goto err; + fatal_error(NULL, "pthread_create()"); =20 - errmsg =3D "Can't join thread\n"; - if (pthread_join(ctd_thread, (void **)&errmsg) || errmsg) - goto err; + if (pthread_join(ctd_thread, NULL)) + fatal_error(NULL, "pthread_join()"); =20 if (!ctd_failed) ksft_test_result_pass("check signal distribution\n"); @@ -245,10 +212,6 @@ static int check_timer_distribution(void) ksft_test_result_fail("check signal distribution\n"); else ksft_test_result_skip("check signal distribution (old kernel)\n"); - return 0; -err: - ksft_print_msg("%s", errmsg); - return -1; } =20 int main(int argc, char **argv) @@ -259,17 +222,10 @@ int main(int argc, char **argv) ksft_print_msg("Testing posix timers. False negative may happen on CPU ex= ecution \n"); ksft_print_msg("based timers if other threads run on the CPU...\n"); =20 - if (check_itimer(ITIMER_VIRTUAL) < 0) - ksft_exit_fail(); - - if (check_itimer(ITIMER_PROF) < 0) - ksft_exit_fail(); - - if (check_itimer(ITIMER_REAL) < 0) - ksft_exit_fail(); - - if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0) - ksft_exit_fail(); + check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL"); + check_itimer(ITIMER_PROF, "ITIMER_PROF"); + check_itimer(ITIMER_REAL, "ITIMER_REAL"); + check_timer_create(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); =20 /* * It's unfortunately hard to reliably test a timer expiration @@ -280,11 +236,8 @@ int main(int argc, char **argv) * to ensure true parallelism. So test only one thread until we * find a better solution. */ - if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0) - ksft_exit_fail(); - - if (check_timer_distribution() < 0) - ksft_exit_fail(); + check_timer_create(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); + check_timer_distribution(); =20 ksft_finished(); }