From nobody Fri Dec 19 20:14:14 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 65A77C8DE for ; Wed, 10 Apr 2024 22:46:16 +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=1712789178; cv=none; b=ceZoWN0vOvmbfcsIbNpz3oi4+Jdb5r1UqGTmRlRlR5DSVlPSCskpfj+/P8zN/cmSlG7kEn68CkpH0R9ZTO2rFwJ0bMVwRF/XDdY7PP0aOd0umf4zhdaDJW691hltcQtuM82BpXA2C87gVkmEwpM6Xkh81tdV0q76lJB1c7DaQBM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789178; c=relaxed/simple; bh=mcxTls6lDnCzT+ZsVcDw2cF9I39ULaXnSI2HylG/01A=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=KkFoglTCfsjThjvprkSL19LcZ+HyoKzTHU0xKMzD4wsNbwcxnEyiaZoAnVlBou0sn4VvWpUk/drbWl8EKPs/GZr9UCPclzSnDq6VIXaYaoCDA75YkYqkzNwbSEMC4Js0t5XKSrqM1pHVp9C6w/LmodnCdx1Iqt+hBLzzs5bYgOo= 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=yazMrQE5; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nS289D/Y; 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="yazMrQE5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nS289D/Y" Message-ID: <20240410165551.001110156@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789174; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=8A7Q0gydwJDE3JFyRi4LCQ2XYlaCSue7mg8fs/zz4UA=; b=yazMrQE5/BqzU4lFK3LhH+zJ0r4h5G0u7/OZ4KC3tGv7RKtrFvAqTbCxsjKzBbLH/pMr57 EKaPgCRWdf4x4th0RvhK9ME49mU1Bajo+2u/Z1Ig8Tor0Nyl+YmZJZugwINzdRI0dX6PoD RvvnKu3bEZSO2c6z3BZouuj9mAnQUXeE7g5vqWtLMZwmbeYv1dIvD9kN6GpHc02ERjh4Mt LtF5NeZy7T+lKJAL2p2TDZgVJnF5IuBifE7iL6RURyrx7O6UGZNibCZvKjPHKAwLT1J/j5 /fDbrcXK1yKElv1HHSu6CVZl7CA7ug1hO8O9Zczt7Q4Xv0r6MV6XeWzu57VinQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789174; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=8A7Q0gydwJDE3JFyRi4LCQ2XYlaCSue7mg8fs/zz4UA=; b=nS289D/YmgRWQxo2sSCQ4mmIMTPsVNsJ6mnGKaDv5zCRJm4NEEVVgbS+c/972KGPWNdS2o m1zChOXpYCYBteAA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 01/50] selftests/timers/posix_timers: Simplify error handling References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:13 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" No point in returning to main() on fatal errors. Just exit right away. Signed-off-by: Thomas Gleixner Reviewed-by: Anna-Maria Behnsen --- tools/testing/selftests/timers/posix_timers.c | 151 ++++++++-------------= ----- 1 file changed, 52 insertions(+), 99 deletions(-) --- 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 sta 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(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) - return ksft_exit_fail(); - - if (check_itimer(ITIMER_PROF) < 0) - return ksft_exit_fail(); - - if (check_itimer(ITIMER_REAL) < 0) - return ksft_exit_fail(); - - if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0) - return 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) - return ksft_exit_fail(); - - if (check_timer_distribution() < 0) - return ksft_exit_fail(); + check_timer_create(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); + check_timer_distribution(); =20 ksft_finished(); } From nobody Fri Dec 19 20:14:14 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 87ED917BDA for ; Wed, 10 Apr 2024 22:46:17 +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=1712789179; cv=none; b=jQZ3EShm8OxHgKAbDRE0MHRyLVprZ5SkY/vUwn7W7SC9DVr3gbn3X10hfBOuy3cgthEODHd2jazU8J62JiZzEe6GLpOEBwUOnGn6t5GjQYRomdiDj6aYM01chqrTkZOw3nf6ljTU9BrtuFbMW9KIlyFx43OoZl1ung+t8w3nAbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789179; c=relaxed/simple; bh=AEwG/M/Bkml3zMl7Ux1PQtqNKrZozpL+XEzsujpu//A=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Ri9Uap6l2Kecpm/DWWYvcMYujw94Tdin72BmLxp3+euz8EjqHIjC/PCN2cyse07sV2UxCdarERGqfLKianb+ESd48OlZZi78C8/l1K6eR8NRim4PrOKcIsBPDMgnjqdhFgYneLOEVIGWgeaiPZx3r0W1vcBYQbTj6UBiaKkbcgA= 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=S7FUrk4+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cz9bDsaP; 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="S7FUrk4+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cz9bDsaP" Message-ID: <20240410165551.064545113@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789176; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=mG203+TomORLX85Hq8lXp4OYnptpdQupAxvOnDDHeaY=; b=S7FUrk4+0vWk2KjFmsDrCYHLe8TA8ncVHslxY5KvXhMwdkB7ZFyKj+euD8jqOKotXLiN6N bX3KOCHuK2WJ1XZG9np49x+3GYT0MzTShtRmQOprdmpMG7U2sWf86zEfE5w4DlafNA5u7G 9rkGXvXVHeq4JUrkGKKCjYEyqobiLpSjs+3BMxAppv6oMytzWUWHSJmLwQvXOsm40b0nNE c3BkkbbHmp9Fqow1b8XwVwi5L1otz2oB/5PcevtTjXDAqwgIR8SmskCyL0A9k/3VefmQoZ w97mTd1t86WiVmrYauqiQc6CF3Z0L6ORArXoWMVQpfiYLPDLFQTxBUgnuzXOWA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789176; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=mG203+TomORLX85Hq8lXp4OYnptpdQupAxvOnDDHeaY=; b=cz9bDsaPLYrDUcW5slz0gitAkYpYc3DqtlmBN6LfHkfHwlp+J30yJtrDNc9dCZ+p/+E5AY ssTnA6e86n18HvDg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 02/50] selftests/timers/posix_timers: Add SIG_IGN test References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:15 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a test case to validate correct behaviour vs. SIG_IGN. The posix specification states: "Setting a signal action to SIG_IGN for a signal that is pending shall cause the pending signal to be discarded, whether or not it is blocked." The kernel implements this in the signal handling code, but due to the way how posix timers are handling SIG_IGN for periodic timers, the behaviour after installing a real handler again is inconsistent and suprising. The following sequence is expected to deliver a signal: install_handler(SIG); block_signal(SIG); timer_create(...); <- Should send SIG timer_settime(value=3D100ms, interval=3D100ms); sleep(1); <- Timer expires and queues signal, timer is not rearmed as that should happen in the signal delivery path ignore_signal(SIG); <- Discards queued signal install_handler(SIG); <- Restore handler, should rearm but does not sleep(1); unblock_signal(SIG); <- Should deliver one signal with overrun count set in siginfo This fails because nothing rearms the timer when the signal handler is restored. Add a test for this case which fails until the signal and posix timer code is fixed. Signed-off-by: Thomas Gleixner --- tools/testing/selftests/timers/posix_timers.c | 127 +++++++++++++++++++++= ++++- 1 file changed, 125 insertions(+), 2 deletions(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -6,8 +6,9 @@ * * Kernel loop code stolen from Steven Rostedt */ - +#define _GNU_SOURCE #include +#include #include #include #include @@ -214,10 +215,129 @@ static void check_timer_distribution(voi ksft_test_result_skip("check signal distribution (old kernel)\n"); } =20 +struct tmrsig { + int signals; + int overruns; +}; + +static void siginfo_handler(int sig, siginfo_t *si, void *uc) +{ + struct tmrsig *tsig =3D si ? si->si_ptr : NULL; + + if (tsig) { + tsig->signals++; + tsig->overruns +=3D si->si_overrun; + } +} + +static void *ignore_thread(void *arg) +{ + unsigned int *tid =3D arg; + sigset_t set; + + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + if (sigprocmask(SIG_BLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); + + *tid =3D gettid(); + sleep(100); + + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); + return NULL; +} + +static void check_sig_ign(int thread) +{ + struct tmrsig tsig =3D { }; + struct itimerspec its; + unsigned int tid =3D 0; + struct sigaction sa; + struct sigevent sev; + pthread_t pthread; + timer_t timerid; + sigset_t set; + + if (thread) { + if (pthread_create(&pthread, NULL, ignore_thread, &tid)) + fatal_error(NULL, "pthread_create()"); + sleep(1); + } + + sa.sa_flags =3D SA_SIGINFO; + sa.sa_sigaction =3D siginfo_handler; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL)) + fatal_error(NULL, "sigaction()"); + + /* Block the signal */ + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + if (sigprocmask(SIG_BLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); + + memset(&sev, 0, sizeof(sev)); + sev.sigev_notify =3D SIGEV_SIGNAL; + sev.sigev_signo =3D SIGUSR1; + sev.sigev_value.sival_ptr =3D &tsig; + if (thread) { + sev.sigev_notify =3D SIGEV_THREAD_ID; + sev._sigev_un._tid =3D tid; + } + + if (timer_create(CLOCK_MONOTONIC, &sev, &timerid)) + fatal_error(NULL, "timer_create()"); + + /* Start the timer to expire in 100ms and 100ms intervals */ + its.it_value.tv_sec =3D 0; + its.it_value.tv_nsec =3D 100000000; + its.it_interval.tv_sec =3D 0; + its.it_interval.tv_nsec =3D 100000000; + timer_settime(timerid, 0, &its, NULL); + + sleep(1); + + /* Set the signal to be ignored */ + if (signal(SIGUSR1, SIG_IGN) =3D=3D SIG_ERR) + fatal_error(NULL, "signal(SIG_IGN)"); + + sleep(1); + + if (thread) { + /* Stop the thread first. No signal should be delivered to it */ + if (pthread_cancel(pthread)) + fatal_error(NULL, "pthread_cancel()"); + if (pthread_join(pthread, NULL)) + fatal_error(NULL, "pthread_join()"); + } + + /* Restore the handler */ + if (sigaction(SIGUSR1, &sa, NULL)) + fatal_error(NULL, "sigaction()"); + + sleep(1); + + /* Unblock it, which should deliver the signal in the !thread case*/ + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); + + if (timer_delete(timerid)) + fatal_error(NULL, "timer_delete()"); + + if (!thread) { + ksft_test_result(tsig.signals =3D=3D 1 && tsig.overruns =3D=3D 29, + "check_sig_ign SIGEV_SIGNAL\n"); + } else { + ksft_test_result(tsig.signals =3D=3D 0 && tsig.overruns =3D=3D 0, + "check_sig_ign SIGEV_THREAD_ID\n"); + } +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(6); + ksft_set_plan(8); =20 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"); @@ -239,5 +359,8 @@ int main(int argc, char **argv) check_timer_create(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); check_timer_distribution(); =20 + check_sig_ign(0); + check_sig_ign(1); + ksft_finished(); } From nobody Fri Dec 19 20:14:14 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 AA0A92EB0B for ; Wed, 10 Apr 2024 22:46:19 +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=1712789181; cv=none; b=DnqDcs2yHbqXy7/acHXjop1UZ69UgRozWfJCxHq66WJ0kcXazAYDgpPmLk2PnlhAd/hTlMGz5QE9RFJQO2xhqVgkIqsciDfWzsoFDQ2k4o+QbDlZb9/S8HRruNDQR+9eWntbw6NPdHNtUs0k31Xb0NHIXbSp62n5+7v6ylCHzig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789181; c=relaxed/simple; bh=0dOor7S/Yn3rDNsac02RvT1qNgsnOubhGwF303qU4IE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=VZW3tpm7Qsx+IUESWWyWpBrUsfigEOoQc8/67iWZAcQpP08rtNLaNXBrDjycELPcX2fzaaafsBz9bWrKDI2z7Lkot+meaJMsvkf2uLcJ7KufdsIbDUBX10g0iKIHe1p/GUveDcP+JtNi/YMiUadWeenXuomCHhfsc4pG9XJkOxA= 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=0iUKFiyd; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=caTf1ZIl; 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="0iUKFiyd"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="caTf1ZIl" Message-ID: <20240410165551.126670434@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=53aZ8Z0KAYc29a8/TyvmKDTswskAp7PFmRVpcJga0I0=; b=0iUKFiydvpLT6lDDnkTvpYZ1jRsvCtCCN6Oj9PD15+qkwNlGXlnmI2O2JVyJQ6zvBE66Lp 6Pa2eYkcVYTUwlNQ55J6TeXreWxxUFBsxetPJc3UccyQ4Yct4EDeaZo8vtsk2i+SleQQOV z/byf4uzVi9vJ+XteToDqjpMObS83/+tofht2VKAk6/J52nj3qQaXmdusdw0dbJBSp/BoE /s1i54fbWDoqxOeKTFzjgQ6urmDtkLaYPp8A7tBUtuGe0z7bKIp7InDALjY/t7M8iBHOqy Q/4/DufK2umItwcOTL4sK3YlgNV+M/3lLWHghjBjEZEdxDrxDywezgdFXwe4cg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=53aZ8Z0KAYc29a8/TyvmKDTswskAp7PFmRVpcJga0I0=; b=caTf1ZIlVoP0PVlm5Onw13uKDfieYjX3oY13CdhGRrZfWZW+jIxWwL/dUSsc7JC/4e1Gn5 CEvBmI8uW7wGOlBw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 03/50] selftests/timers/posix_timers: Validate signal rules References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:17 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a test case to validate correct behaviour vs. timer reprogramming and deletion. The handling of queued signals in case of timer reprogramming or deletion is inconsistent at best. POSIX does not really specify the behaviour for that: - "The effect of disarming or resetting a timer with pending expiration notifications is unspecified." - "The disposition of pending signals for the deleted timer is unspecified." In both cases it is reasonable to expect that pending signals are discarded. Especially in the reprogramming case it does not make sense to account for previous overruns or to deliver a signal for a timer which has been disarmed. Add tests to validate that no unexpected signals are delivered. They fail for now until the signal and posix timer code is updated. Signed-off-by: Thomas Gleixner --- tools/testing/selftests/timers/posix_timers.c | 108 +++++++++++++++++++++= ++++- 1 file changed, 107 insertions(+), 1 deletion(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -334,10 +334,114 @@ static void check_sig_ign(int thread) } } =20 +static void check_rearm(void) +{ + struct tmrsig tsig =3D { }; + struct itimerspec its; + struct sigaction sa; + struct sigevent sev; + timer_t timerid; + sigset_t set; + + sa.sa_flags =3D SA_SIGINFO; + sa.sa_sigaction =3D siginfo_handler; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL)) + fatal_error(NULL, "sigaction()"); + + /* Block the signal */ + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + if (sigprocmask(SIG_BLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); + + memset(&sev, 0, sizeof(sev)); + sev.sigev_notify =3D SIGEV_SIGNAL; + sev.sigev_signo =3D SIGUSR1; + sev.sigev_value.sival_ptr =3D &tsig; + if (timer_create(CLOCK_MONOTONIC, &sev, &timerid)) + fatal_error(NULL, "timer_create()"); + + /* Start the timer to expire in 100ms and 100ms intervals */ + its.it_value.tv_sec =3D 0; + its.it_value.tv_nsec =3D 100000000; + its.it_interval.tv_sec =3D 0; + its.it_interval.tv_nsec =3D 100000000; + if (timer_settime(timerid, 0, &its, NULL)) + fatal_error(NULL, "timer_settime()"); + + sleep(1); + + /* Reprogram the timer to single shot */ + its.it_value.tv_sec =3D 10; + its.it_value.tv_nsec =3D 0; + its.it_interval.tv_sec =3D 0; + its.it_interval.tv_nsec =3D 0; + if (timer_settime(timerid, 0, &its, NULL)) + fatal_error(NULL, "timer_settime()"); + + /* Unblock it, which should not deliver a signal */ + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); + + if (timer_delete(timerid)) + fatal_error(NULL, "timer_delete()"); + + ksft_test_result(!tsig.signals, "check_rearm\n"); +} + +static void check_delete(void) +{ + struct tmrsig tsig =3D { }; + struct itimerspec its; + struct sigaction sa; + struct sigevent sev; + timer_t timerid; + sigset_t set; + + sa.sa_flags =3D SA_SIGINFO; + sa.sa_sigaction =3D siginfo_handler; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL)) + fatal_error(NULL, "sigaction()"); + + /* Block the signal */ + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + if (sigprocmask(SIG_BLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); + + memset(&sev, 0, sizeof(sev)); + sev.sigev_notify =3D SIGEV_SIGNAL; + sev.sigev_signo =3D SIGUSR1; + sev.sigev_value.sival_ptr =3D &tsig; + if (timer_create(CLOCK_MONOTONIC, &sev, &timerid)) + fatal_error(NULL, "timer_create()"); + + /* Start the timer to expire in 100ms and 100ms intervals */ + its.it_value.tv_sec =3D 0; + its.it_value.tv_nsec =3D 100000000; + its.it_interval.tv_sec =3D 0; + its.it_interval.tv_nsec =3D 100000000; + if (timer_settime(timerid, 0, &its, NULL)) + fatal_error(NULL, "timer_settime()"); + + sleep(1); + + if (timer_delete(timerid)) + fatal_error(NULL, "timer_delete()"); + + /* Unblock it, which should not deliver a signal */ + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); + + ksft_test_result(!tsig.signals, "check_delete\n"); +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(8); + ksft_set_plan(10); =20 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"); @@ -361,6 +465,8 @@ int main(int argc, char **argv) =20 check_sig_ign(0); check_sig_ign(1); + check_rearm(); + check_delete(); =20 ksft_finished(); } From nobody Fri Dec 19 20:14:14 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 2EB1B3AC16 for ; Wed, 10 Apr 2024 22:46:21 +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=1712789182; cv=none; b=my35rGAIQ3ZFsgx363jLmidemwDNEjSSe4wXs5H4FiS+90hXNMsrnvTPe4B+6N6Rj5Ij3xiStDR7/PY1mHREuUqJ930vocpMzJqgbnjbpc0tmYXhNlZYsZmMshcaTaUE/uzxVeIiqK19d782O/L2vW5ZL8KtZPs2hS7XQ/kxG5U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789182; c=relaxed/simple; bh=dFadCdHIj/hoRcxQcT8VgvmC98onMotQm8AlW+HXtsQ=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=QfB02g9EmGTQaG7uSeH6UAsgOOwB4T4OpR5HLYFQfvRwOfU/+0u/SNgS/8Ej7qZdoTyJWzC6FlNaWwL3q2kzjQqNFaOoOF/nEOYmi/QZ7j5lLN2waMlMI8q+pE8s0Z6E1aYe5mux8B0hYPE0GF/9yHQOeBzUmowViBeSiXnd2rw= 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=wcK5YdEl; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qPGLUpsJ; 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="wcK5YdEl"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qPGLUpsJ" Message-ID: <20240410165551.188561268@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789179; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=wBuqBPrJ78lfRZ7AX91zzV87OtbmlXth39hEp1fmaS0=; b=wcK5YdElAv+Vw/2iSm0WNRt2z53gmNT2k2k/Y0YycLZbQjWnnye/CoEjQg1CBx9WCDwM6T QgCkvaiKynTeEQpvyG12ctfls5fhDuqw3hVaxXll6cVBjqp3hKLSllzshO5vWhC+Vuvby/ AUdtETnq4NZtx3uJlIi2E+uwC3NegQ3QrhPLj1cnkV7mZJEL8/7FdEFxGAoo7xvVmIMhk8 5C/LULJ/W1UR+oWYhSSXnWhHAFOIogcuLWyuckHum/RjLzxUIkfn8QOKutbb++jfqlieYd IcPkx8iW7u8SXjYUv38MY5azirtaZ1SS3QmmXRsVZGYKQ8zc4CsWrTQtPLxaMw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789179; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=wBuqBPrJ78lfRZ7AX91zzV87OtbmlXth39hEp1fmaS0=; b=qPGLUpsJ5erUqRCEre+kulajLbpbjs9RFKDAYYMfNcyI4/zxSvVyiF9Hx1ZMll+wR8bjX3 n1mlYx1JW7vGQIAw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 04/50] selftests/timers/posix-timers: Validate SIGEV_NONE References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:19 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Posix timers with a delivery mode of SIGEV_NONE deliver no signals but the remaining expiry time must be readable via timer_gettime() for both one shot and interval timers. That's implemented correctly for regular posix timers but broken for posix CPU timers. Add a self test so the fixes can be verified. Signed-off-by: Thomas Gleixner --- tools/testing/selftests/timers/posix_timers.c | 53 +++++++++++++++++++++= ++++- 1 file changed, 52 insertions(+), 1 deletion(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,7 @@ =20 #define DELAY 2 #define USECS_PER_SEC 1000000 +#define NSECS_PER_SEC 1000000000 =20 static void __fatal_error(const char *test, const char *name, const char *= what) { @@ -438,10 +440,57 @@ static void check_delete(void) ksft_test_result(!tsig.signals, "check_delete\n"); } =20 +static inline int64_t calcdiff_ns(struct timespec t1, struct timespec t2) +{ + int64_t diff; + + diff =3D NSECS_PER_SEC * (int64_t)((int) t1.tv_sec - (int) t2.tv_sec); + diff +=3D ((int) t1.tv_nsec - (int) t2.tv_nsec); + return diff; +} + +static void check_sigev_none(int which, const char *name) +{ + struct timespec start, now; + struct itimerspec its; + struct sigevent sev; + timer_t timerid; + + memset(&sev, 0, sizeof(sev)); + sev.sigev_notify =3D SIGEV_NONE; + + if (timer_create(which, &sev, &timerid)) + fatal_error(name, "timer_create()"); + + /* Start the timer to expire in 100ms and 100ms intervals */ + its.it_value.tv_sec =3D 0; + its.it_value.tv_nsec =3D 100000000; + its.it_interval.tv_sec =3D 0; + its.it_interval.tv_nsec =3D 100000000; + timer_settime(timerid, 0, &its, NULL); + + if (clock_gettime(which, &start)) + fatal_error(name, "clock_gettime()"); + + do { + if (clock_gettime(which, &now)) + fatal_error(name, "clock_gettime()"); + } while (calcdiff_ns(now, start) < NSECS_PER_SEC); + + if (timer_gettime(timerid, &its)) + fatal_error(name, "timer_gettime()"); + + if (timer_delete(timerid)) + fatal_error(name, "timer_delete()"); + + ksft_test_result(its.it_value.tv_sec || its.it_value.tv_nsec, + "check_sigev_none %s\n", name); +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(10); + ksft_set_plan(12); =20 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"); @@ -467,6 +516,8 @@ int main(int argc, char **argv) check_sig_ign(1); check_rearm(); check_delete(); + check_sigev_none(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); + check_sigev_none(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); =20 ksft_finished(); } From nobody Fri Dec 19 20:14:14 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 C6D3D10A11 for ; Wed, 10 Apr 2024 22:46:22 +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=1712789184; cv=none; b=Lzlc3meEo07bXIShgZ8ZcDLYdFCju+1GRuSb1uCDXyzHlmWKu9xhbcIC71fZub2TgZO4DtWbyAkmpq9gzBkTum8+4WqYuAXxseMDMPgkXxT1eTclQfjFhi8WgHSCGmSYbqO3RSpIhtsaSD321BIcxeV/GrL+UcY/Ab6GugxTs3I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789184; c=relaxed/simple; bh=Qw6a6vr3job2BPNhi3+9A2NeMwWriv1M9zdK8XeX/E8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=nn31LCCwNpKy+4G6bKvW4CKaFAb77iXNokyIHNDHlAVA5eFaEOG4FqAfy297iPbFVawpOqV8A6GzK8ESDXeE8rkqE6YvVYgDZ1/A5ep3An60lMXdih7koug0o+8dVXU8aP4bUuSkqHYOqp2FmxISkOa2OJ+Q6+LCPLm81urKYE0= 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=EPo+dibk; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=PZBEKA0O; 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="EPo+dibk"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="PZBEKA0O" Message-ID: <20240410165551.251325142@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789181; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=7n8Tw6w2v5Y162ZsOpKlDwSopUc0/cD/VnEumBQN9lE=; b=EPo+dibk9/cUdIVDUsVlNyI7E/L1A9apZ7sOxGP3CWY4js9xFJ783z7XU8aQ9KqYB8zs3F 0DKPK12H0F7O+zaiNoKT0GEdLX/QivokV5yA0g9xikk9n20i2uciicoBv+BTA6Qll1viV8 XFYhcT4Fx0xex5w14DBhgAzx6Qq+BSHCUdDCOpZLLqgdsxUe51yGQh6x/ci65wc7F2TmX2 kIDn1D+gezfTBsPm0Jktbbu6IYvKecKsYyVspz6UgwDr13v2R7K8WhaBNCNtLOGyH99nLM tprDsK6oJ96XXbAIJHZbiL3sTHuQ8m2V/41vgzpaToWRPssad4rirXwVVZHUrQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789181; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=7n8Tw6w2v5Y162ZsOpKlDwSopUc0/cD/VnEumBQN9lE=; b=PZBEKA0OSrRLu9UwlneLihE3TM+OM/ZMErkVtd5uasKKTECivVDmWtpRcsa/ubmm4QdaN5 aNHlb/3Oa1c7amBg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 05/50] selftests/timers/posix-timers: Validate timer_gettime() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:20 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" timer_gettime() must return the correct expiry time for interval timers even when the timer is not armed, which is the case when a signal is pending but blocked. Works correctly for regular posix timers, but not for posix CPU timers. Add a selftest to validate the fixes. Signed-off-by: Thomas Gleixner --- tools/testing/selftests/timers/posix_timers.c | 58 +++++++++++++++++++++= ++++- 1 file changed, 57 insertions(+), 1 deletion(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -487,10 +487,63 @@ static void check_sigev_none(int which, "check_sigev_none %s\n", name); } =20 +static void check_gettime(int which, const char *name) +{ + struct itimerspec its, prev; + struct timespec start, now; + struct sigevent sev; + timer_t timerid; + int wraps =3D 0; + sigset_t set; + + /* Block the signal */ + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + if (sigprocmask(SIG_BLOCK, &set, NULL)) + fatal_error(name, "sigprocmask(SIG_BLOCK)"); + + memset(&sev, 0, sizeof(sev)); + sev.sigev_notify =3D SIGEV_SIGNAL; + sev.sigev_signo =3D SIGUSR1; + + if (timer_create(which, &sev, &timerid)) + fatal_error(name, "timer_create()"); + + /* Start the timer to expire in 100ms and 100ms intervals */ + its.it_value.tv_sec =3D 0; + its.it_value.tv_nsec =3D 100000000; + its.it_interval.tv_sec =3D 0; + its.it_interval.tv_nsec =3D 100000000; + if (timer_settime(timerid, 0, &its, NULL)) + fatal_error(name, "timer_settime()"); + + if (timer_gettime(timerid, &prev)) + fatal_error(name, "timer_gettime()"); + + if (clock_gettime(which, &start)) + fatal_error(name, "clock_gettime()"); + + do { + if (clock_gettime(which, &now)) + fatal_error(name, "clock_gettime()"); + if (timer_gettime(timerid, &its)) + fatal_error(name, "timer_gettime()"); + if (its.it_value.tv_nsec > prev.it_value.tv_nsec) + wraps++; + prev =3D its; + + } while (calcdiff_ns(now, start) < NSECS_PER_SEC); + + if (timer_delete(timerid)) + fatal_error(name, "timer_delete()"); + + ksft_test_result(wraps > 1, "check_gettime %s\n", name); +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(12); + ksft_set_plan(15); =20 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"); @@ -518,6 +571,9 @@ int main(int argc, char **argv) check_delete(); check_sigev_none(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); check_sigev_none(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); + check_gettime(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); + check_gettime(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); + check_gettime(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); =20 ksft_finished(); } From nobody Fri Dec 19 20:14:14 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 C99DF17BDA for ; Wed, 10 Apr 2024 22:46:24 +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=1712789186; cv=none; b=uE467w1vz50t0Y87zdEqakc78vRe6iAz5wFZqNW9QS0sKctxMXwoniGWmwnhQI2f5WCWvDZU+dW7XBRnhkARHUDVhottVWFPLcr2Xr8epNVaMPvajSMrcOf/R+gx9+MCkwiYF2x+N26g0tnHSYql5U+NEeO3tdAhv8Uv9zKJ8/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789186; c=relaxed/simple; bh=Jz7RJXZF/wpnmhSKslp2B8lopo6bOLwGJ9GuQe5xSbw=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Y8Hx7Z5jmrOzP91AOhFHlxNvog3t4DjkJXee7Uh+g80ssdYbW1ymfq3GDCj+J/IEoShrN9VqY5BjCqbwRNKmBpT4BzVCvUAYc7K0UZE9VUZgzuxvhtuV8PMKH+/EI0a5y5rFsOuL0R6ILcYMhX1esqBJ/+vuxCQU5gxRg0DPCQg= 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=w07gFicp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=W+hc683y; 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="w07gFicp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="W+hc683y" Message-ID: <20240410165551.314429637@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=oS7SrzV2Wd9Ag3yMmGtoq7FlROh6d1Si5LvLo/AXQAY=; b=w07gFicpR/1qi8Djk6/+jTf0DdJb59vWhGl7olnPMcNLuwbA00kMjxEv5SEHStFf6NgS9K s8Di41Uu+5f/e2ehWvMDsaMKWUKOF2Zn7fja5m67bNtSrfolTQLXmx7LKALLFtTqgdHYew VL7GxkZBjQa7vgzBRTUvIoq0e5/oZj8rJnzW5fsHP9tfAY4H5e8JNumQolGunJHd3Gw01+ 8iPUH2Ss2tDss7/4JSAzWOOdsOyHaBd5fLrIOtBCstXsEp1aVRW8tpTfBnal0rmg+RlOlC KWLpw/p+lPumnM2RPSE07pabsFQG+qwDmnCia8gu+B9s1EUm3pK2vZ/3H+gZWg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=oS7SrzV2Wd9Ag3yMmGtoq7FlROh6d1Si5LvLo/AXQAY=; b=W+hc683y45YViYoAAxz/noNrfSafkVxzK32JLuNnIHY6uMGijv5KO1bND5iHfLzyr2Mc0z /Gt6w/cHE+JS0PAQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 06/50] selftests/timers/posix-timers: Validate overrun after unblock References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:22 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a timer signal is blocked and later unblocked then one signal should be delivered with the correct number of overruns since the timer was queued. Validate that behaviour. Signed-off-by: Thomas Gleixner --- tools/testing/selftests/timers/posix_timers.c | 61 +++++++++++++++++++++= ++++- 1 file changed, 60 insertions(+), 1 deletion(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -540,10 +540,66 @@ static void check_gettime(int which, con ksft_test_result(wraps > 1, "check_gettime %s\n", name); } =20 +static void check_overrun(int which, const char *name) +{ + struct timespec start, now; + struct tmrsig tsig =3D { }; + struct itimerspec its; + struct sigaction sa; + struct sigevent sev; + timer_t timerid; + sigset_t set; + + sa.sa_flags =3D SA_SIGINFO; + sa.sa_sigaction =3D siginfo_handler; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL)) + fatal_error(name, "sigaction()"); + + /* Block the signal */ + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + if (sigprocmask(SIG_BLOCK, &set, NULL)) + fatal_error(name, "sigprocmask(SIG_BLOCK)"); + + memset(&sev, 0, sizeof(sev)); + sev.sigev_notify =3D SIGEV_SIGNAL; + sev.sigev_signo =3D SIGUSR1; + sev.sigev_value.sival_ptr =3D &tsig; + if (timer_create(which, &sev, &timerid)) + fatal_error(name, "timer_create()"); + + /* Start the timer to expire in 100ms and 100ms intervals */ + its.it_value.tv_sec =3D 0; + its.it_value.tv_nsec =3D 100000000; + its.it_interval.tv_sec =3D 0; + its.it_interval.tv_nsec =3D 100000000; + if (timer_settime(timerid, 0, &its, NULL)) + fatal_error(name, "timer_settime()"); + + if (clock_gettime(which, &start)) + fatal_error(name, "clock_gettime()"); + + do { + if (clock_gettime(which, &now)) + fatal_error(name, "clock_gettime()"); + } while (calcdiff_ns(now, start) < NSECS_PER_SEC); + + /* Unblock it, which should deliver a signal */ + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) + fatal_error(name, "sigprocmask(SIG_UNBLOCK)"); + + if (timer_delete(timerid)) + fatal_error(name, "timer_delete()"); + + ksft_test_result(tsig.signals =3D=3D 1 && tsig.overruns =3D=3D 9, + "check_overrun %s\n", name); +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(15); + ksft_set_plan(18); =20 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"); @@ -574,6 +630,9 @@ int main(int argc, char **argv) check_gettime(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); check_gettime(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); check_gettime(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); + check_overrun(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); + check_overrun(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); + check_overrun(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); =20 ksft_finished(); } From nobody Fri Dec 19 20:14:14 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 CEB5040BF2 for ; Wed, 10 Apr 2024 22:46:26 +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=1712789188; cv=none; b=MQ0vBb9T+DvZ9OfZqDH/bJMEH2WLqBYg8bXKvwV+ld16pHh6eBf9TOpFewosN2+zf8sgtFnbzKG6W8gMeGC3T50ofSD5G9alUgNdXdP+SMjUR3DPjvneHrxyKVhuSM1x4hZ7GeiDGojo5oNFXnw6gjcGrTINpsuIRaocTWeNv+c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789188; c=relaxed/simple; bh=5E5SF6djGIEJs9V+wAgKePOcQC5ojpZdVLcsv/5ENUo=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=NcoWmy5uKCS2VB4vCngm1lhBjYBXh4sF8N8+cU42y58FA2Cv2WMKFIG5fOIIkCV+v4nNNDLTejuNWaLub1+FIjTaTa2fQap9MQcT5yFkb7wF7FZxuJJrafye/SNW2O/Rb9dYhV9qUwL2ToFTtYqVXTKfc4lP6QaESeShxVjvVpA= 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=SxdENd8A; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AeR/qmm9; 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="SxdENd8A"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AeR/qmm9" Message-ID: <20240410165551.376994018@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789184; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=+/WP0ANjczawtPlZe0Ru3wamTdkxabLE/EGLxYuvFcc=; b=SxdENd8AQlIuGElK3HqraY3nm3G+pcB3HvAZrvv6BHt/sPK62dkVLHU5K6EBzJFdAPdJCr fSfPqTJ3Nl1dwjAe2fEUDa+YbQwBhMOwkTN6WNkrGJ7dMZYlyLcfKk84PpmHtBUtCx1FNg fjaJeWaM+vI/xhWRJv73i1h2houoB4CktHQcHBXlYDm/y38BpOkw1Lv4PI+a+9jKPBhi6R wC3ZuavN7IVZvdp253Yls2RuA5zjsZ08qmwVmnSp80cwBRipjhNPH+02evKKxOPZJX1Nul Q9ITohwXt2E0qNLdg61MA1f92YVGx1qGR7hiBJJUWmJApbp2QSXgo9KY5Q4Reg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789184; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=+/WP0ANjczawtPlZe0Ru3wamTdkxabLE/EGLxYuvFcc=; b=AeR/qmm9tI8sitHKw4Zm4S278BreQlFM09BR+brsBiKExnnafX4U+mcQzABLWjKQ+AP6P2 L38W7hsisXBdnmAg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 07/50] posix-cpu-timers: Split up posix_cpu_timer_get() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:24 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for addressing issues in the timer_get() and timer_set() functions of posix CPU timers. No functional change. Signed-off-by: Thomas Gleixner Reviewed-by: Anna-Maria Behnsen Reviewed-by: Frederic Weisbecker --- V2: Split out into new patch to make review simpler - Frederic --- kernel/time/posix-cpu-timers.c | 51 +++++++++++++++++++-----------------= ----- 1 file changed, 24 insertions(+), 27 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -785,33 +785,9 @@ static int posix_cpu_timer_set(struct k_ return ret; } =20 -static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec6= 4 *itp) +static void __posix_cpu_timer_get(struct k_itimer *timer, struct itimerspe= c64 *itp, u64 now) { - clockid_t clkid =3D CPUCLOCK_WHICH(timer->it_clock); - struct cpu_timer *ctmr =3D &timer->it.cpu; - u64 now, expires =3D cpu_timer_getexpires(ctmr); - struct task_struct *p; - - rcu_read_lock(); - p =3D cpu_timer_task_rcu(timer); - if (!p) - goto out; - - /* - * Easy part: convert the reload time. - */ - itp->it_interval =3D ktime_to_timespec64(timer->it_interval); - - if (!expires) - goto out; - - /* - * Sample the clock to take the difference with the expiry time. - */ - if (CPUCLOCK_PERTHREAD(timer->it_clock)) - now =3D cpu_clock_sample(clkid, p); - else - now =3D cpu_clock_sample_group(clkid, p, false); + u64 expires =3D cpu_timer_getexpires(&timer->it.cpu); =20 if (now < expires) { itp->it_value =3D ns_to_timespec64(expires - now); @@ -823,7 +799,28 @@ static void posix_cpu_timer_get(struct k itp->it_value.tv_nsec =3D 1; itp->it_value.tv_sec =3D 0; } -out: +} + +static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec6= 4 *itp) +{ + clockid_t clkid =3D CPUCLOCK_WHICH(timer->it_clock); + struct task_struct *p; + u64 now; + + rcu_read_lock(); + p =3D cpu_timer_task_rcu(timer); + if (p) { + itp->it_interval =3D ktime_to_timespec64(timer->it_interval); + + if (cpu_timer_getexpires(&timer->it.cpu)) { + if (CPUCLOCK_PERTHREAD(timer->it_clock)) + now =3D cpu_clock_sample(clkid, p); + else + now =3D cpu_clock_sample_group(clkid, p, false); + + __posix_cpu_timer_get(timer, itp, now); + } + } rcu_read_unlock(); } From nobody Fri Dec 19 20:14:14 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 59F6E41C61 for ; Wed, 10 Apr 2024 22:46:28 +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=1712789189; cv=none; b=eID62gg3cEHfVt2ea1pl76okz4H1xi/uqRrDQmENkz6sRJvrhNeyVpgqq4Qs1adIpiP+9S4PzmwM0+j8/WvTGHcFx7dytLAbZrNlCtEgzZSGMNuDSSfBJ5m5M+ldXsFgBXPPUOTYWSAQ++wGxJZCpso7VtkXS2DNSXUwiFXxPP4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789189; c=relaxed/simple; bh=gUItmgDH5rsS1e6yVmaYPpvSzF4y549YIibjLNxq98E=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=NFCYRdIlaQheqmcHfz7Z/obO1G5MVP3zDChsdL9vzkShf1ijMYJR8+5jtVAQigKP7pz9xEfLipmvQWGlaoeaGtfvHqMHRpzPb77G9w3OQiHoR9vTF4qwR6c+Q/QuM0RJOEPINy7P9pWChlXInM0RBKzVM7Ufak0es7/RLKeLWJc= 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=k3J8Iis4; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lsgHz7XW; 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="k3J8Iis4"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lsgHz7XW" Message-ID: <20240410165551.442678509@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789186; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=AfHYSU/HugM7h61r392aP8dR7dmyLHuDBcUa2xZEhTQ=; b=k3J8Iis4UlCEz7ouQzMEVjnMpqbka+RyNovjeI8aMklnVdHDeXDeuJOAsK8CKGSiUaYXRu p319ipNtFrkW8B4FkNSODjp9yWBC18lHf5Etwd6DcKrJfwHNo8k0WjZXYK/kNHoO6x06Jq znwaNA51jlKRQXRtDWLg1hcBBAZ6y/AnVg3i70ZBrn0vsqBXIDAIS7Wc/drUIsTt7RwOPI DdtiRfeiMMBU801yoaLMb1n6NFZGqKYOAsobsy50Jzux3g66vK32R1rUuMpKi37o6PAn+N aYN7lcE/Rt8T+RibDHnavHbTTSN1c83/EOkJwWkAEWOtqRTma9aUbhJTo8pONQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789186; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=AfHYSU/HugM7h61r392aP8dR7dmyLHuDBcUa2xZEhTQ=; b=lsgHz7XWUuyEN3c9+lGzFckBDC+HrPIbvO/LBtiP8ihJet9FKA4TQGmyR23O7VM9XWD0LY GrevTs8eMP3acTBw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 08/50] posix-cpu-timers: Save interval only for armed timers References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:26 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There is no point to return the interval for timers which have been disarmed. Signed-off-by: Thomas Gleixner --- V2: Split out into new patch to make review simpler - Frederic --- kernel/time/posix-cpu-timers.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -809,17 +809,15 @@ static void posix_cpu_timer_get(struct k =20 rcu_read_lock(); p =3D cpu_timer_task_rcu(timer); - if (p) { + if (p && cpu_timer_getexpires(&timer->it.cpu)) { itp->it_interval =3D ktime_to_timespec64(timer->it_interval); =20 - if (cpu_timer_getexpires(&timer->it.cpu)) { - if (CPUCLOCK_PERTHREAD(timer->it_clock)) - now =3D cpu_clock_sample(clkid, p); - else - now =3D cpu_clock_sample_group(clkid, p, false); + if (CPUCLOCK_PERTHREAD(timer->it_clock)) + now =3D cpu_clock_sample(clkid, p); + else + now =3D cpu_clock_sample_group(clkid, p, false); =20 - __posix_cpu_timer_get(timer, itp, now); - } + __posix_cpu_timer_get(timer, itp, now); } rcu_read_unlock(); } From nobody Fri Dec 19 20:14:14 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 3AD3044C9C for ; Wed, 10 Apr 2024 22:46:29 +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=1712789191; cv=none; b=aQFlX81kRmqZz404qNcanW31lroif72hKAy+EkrRX1lC4HROzRdk8dGvWtsocVwft54JdDZvjlD9qagqZz1xght2yZpmy5BbH4p0nYyzHl1osPcBEVqooiRv8Mb+7R9mmyji/2eYH03wRLOFfsR3ylgIF6CicxbqPV1n+CKQ2ng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789191; c=relaxed/simple; bh=tBM7wjzipNsnWNbyoaSXjOxN31IJ8Anu9Imn8eeRuXc=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=l2Ct9sEl3OouNQMPxyc+RTyyL/dlRckxp2XR35Go6otqVqPXo1ongsjjLSuMOha8soe2mKc0maRQi2rqt3sEjfHUCyUlPBc0/ObliIZMAB85vKb7GBO/kgJMwJftwCo01bVTIK5hKYGVu0bxOIFBmQGnmnp+drS3AGTeNwJgLZU= 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=JwBgcegB; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Hac/vYtR; 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="JwBgcegB"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Hac/vYtR" Message-ID: <20240410165551.507566619@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789188; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=bZ0w/TM6AE9SMRNm3Xm9JEw2iNfzEaEbkpBswlSbBEk=; b=JwBgcegBSK9F8S1rCnEh4K9h4RRWM/Kw2wiHMj1/nDmTravpXYCT/k0L0Pm0e4ljUM/IHn G8/RW6PDW88uQQ3R2Iy9go6jmBxoShjXpcmYMtG1IM7GepGCGWNJzBk5CJRN7zZdVDKGrH mTAniro9eIsm9A4r0N/4eB9VgpaiK0zxPURA2Pp9XRsIqNjHBSiEuZ/14cuvL4f/OV5zoF vFOOZ/Lk91Fw57CCZPcQvi0WeEDvAKyRKoonTr/Q3zaEYjfTvJhwU5hq/qmt8r97zJmTAz ZtNmVeomMpz07L3NteXi1YslIGNDkV1b6gT60sl/GDr8KLikZbHjD2H5FDc4sA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789188; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=bZ0w/TM6AE9SMRNm3Xm9JEw2iNfzEaEbkpBswlSbBEk=; b=Hac/vYtRiZ/v2yf/qhXTlwR+4llBiC0FbGcSvaa+/fVJuqyZlm+bBGIdylWhZIkZrjElwO xMF2/1wiZ6JXOaCA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 09/50] posix-cpu-timers: Handle interval timers correctly in timer_get() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:27 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" timer_gettime() must return the remaining time to the next expiry of a timer or 0 if the timer is not armed and no signal pending, but posix CPU timers fail to forward a timer which is already expired. Add the required logic to address that. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- V2: Split out into new patch to make review simpler - Frederic --- kernel/time/posix-cpu-timers.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -787,8 +787,24 @@ static int posix_cpu_timer_set(struct k_ =20 static void __posix_cpu_timer_get(struct k_itimer *timer, struct itimerspe= c64 *itp, u64 now) { - u64 expires =3D cpu_timer_getexpires(&timer->it.cpu); + u64 expires, iv =3D timer->it_interval; =20 + /* + * Make sure that interval timers are moved forward for the + * following cases: + * - Timers which expired, but the signal has not yet been + * delivered + */ + if (iv && (timer->it_requeue_pending & REQUEUE_PENDING)) + expires =3D bump_cpu_timer(timer, now); + else + expires =3D cpu_timer_getexpires(&timer->it.cpu); + + /* + * Expired interval timers cannot have a remaining time <=3D 0. + * The kernel has to move them forward so that the next + * timer expiry is > @now. + */ if (now < expires) { itp->it_value =3D ns_to_timespec64(expires - now); } else { From nobody Fri Dec 19 20:14:14 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 AA8FF282F0 for ; Wed, 10 Apr 2024 22:46:31 +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=1712789193; cv=none; b=kdAtYy1AC6KeJ63qhntvf0qXsQt8sClZoJ9OteSnqbZeg3RtQuu3Bn6SpXBaTSbzVgIhv6sWd/Qp/JCsJjpr282o/2nefn97XsPN9T20U8RbD2TlMQAyc7CL/VxAZFI1MfqKahlctF1TxAeIoezPj+S5nHdSeyKZ45xtuUM4Kj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789193; c=relaxed/simple; bh=uF1VDsOZ4FIX9FT58rai3eHyWyTmEsXGceZwBt+UMwI=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=I3Q2GF5QdrpZiQ/0r4Tt4PZDGjMzViS7K33ql2c0OkHjhbJSmsBRJzgTdQmCFgDeJ5L9Hvim9dl6kXdBsk/p1F4Xp4Yc5yxluaHrS7ZcuVI73JVrrsFqStVRjckk47ETuqgQDppWM5eRsui73Vnw501CKAl3TBUY0mQjUL6CxsA= 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=Kwfe8kRA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=7PrYUi+m; 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="Kwfe8kRA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="7PrYUi+m" Message-ID: <20240410165551.571240036@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=vXPCMrPwGrJjX858eQh8BXnWzZ6Yig+J2QuB2rurbL0=; b=Kwfe8kRAdTEDXospO4AAKMJu55D/3M7fRmrEW12rngOQzXYSEKBCor+NvfzU5qHJEcs0CM R6qEIywleaHXsaaTAgpBC7UFL4twnXfK4nxFvVmPEPRKfSerLG6jlMRp0YAQr85R+r4jW7 48YiFQBrDdqypIbB/tXFGyoCQkYp/mtW53V/h1X5Q5habEs/IB+rs4TGua3McY44i/9EPM PFy7pTZKLiRXHhKFQ0Kji5haUPkBx004THcv1mERRlcyNtIL48SCoi9JAnmmhDajx/eZD0 1/f/dpfR8RAJcVs+Jv+GSJQL5Lj6lpPqKqjDfQyze5D1//TqIwCnIaoWMKqldw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=vXPCMrPwGrJjX858eQh8BXnWzZ6Yig+J2QuB2rurbL0=; b=7PrYUi+mLm5feOooZsxJFhtOBOc5kFndJxUY0Pw+3Nwu+38Paua1zKiSgt1ymz8J2UKALI XsvAAm20nnb3unAg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 10/50] posix-cpu-timers: Handle SIGEV_NONE timers correctly in timer_get() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:29 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expired SIGEV_NONE oneshot timers must return 0 nsec for the expiry time in timer_get(), but the posix CPU timer implementation returns 1 nsec. Add the missing conditional. Signed-off-by: Thomas Gleixner --- V2: Split out into new patch to make review simpler - Frederic --- kernel/time/posix-cpu-timers.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -787,15 +787,17 @@ static int posix_cpu_timer_set(struct k_ =20 static void __posix_cpu_timer_get(struct k_itimer *timer, struct itimerspe= c64 *itp, u64 now) { + bool sigev_none =3D timer->it_sigev_notify =3D=3D SIGEV_NONE; u64 expires, iv =3D timer->it_interval; =20 /* * Make sure that interval timers are moved forward for the * following cases: + * - SIGEV_NONE timers which are never armed * - Timers which expired, but the signal has not yet been * delivered */ - if (iv && (timer->it_requeue_pending & REQUEUE_PENDING)) + if (iv && ((timer->it_requeue_pending & REQUEUE_PENDING) || sigev_none)) expires =3D bump_cpu_timer(timer, now); else expires =3D cpu_timer_getexpires(&timer->it.cpu); @@ -809,11 +811,13 @@ static void __posix_cpu_timer_get(struct itp->it_value =3D ns_to_timespec64(expires - now); } else { /* - * The timer should have expired already, but the firing - * hasn't taken place yet. Say it's just about to expire. + * A single shot SIGEV_NONE timer must return 0, when it is + * expired! Timers which have a real signal delivery mode + * must return a remaining time greater than 0 because the + * signal has not yet been delivered. */ - itp->it_value.tv_nsec =3D 1; - itp->it_value.tv_sec =3D 0; + if (!sigev_none) + itp->it_value.tv_nsec =3D 1; } } From nobody Fri Dec 19 20:14:14 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 BBFB947A55 for ; Wed, 10 Apr 2024 22:46:33 +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=1712789195; cv=none; b=IvYGXJliPSfGBgNb+uiohsbhF/tBH/onmUbQM6sPYx8vykrB8UMXNZrauGummfgY27htD55Fx+YqXzqKEHiU5pF/buT7AQ0WWeBNZdPcjli4YbRVoW9Pot/tu0JcT10yvwbA8OphX9hn2c4rARBrO7dUmv0P5lTmScDtfK/czPo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789195; c=relaxed/simple; bh=ELAaPaGkJq6p3gTMdWaH/+B64HkzxUph4WcIXAbdkUg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=sW+FWeWGQBGScwdZUr3CNDEsQG9ybJVFO8lwDXIPzbih1AGEGvWILwqJ+qLENOWVPH4uir5cuNXp5gBmVyW0O1Cl0QJFuYVCnftGtOiLMkOK/b7EDdOSRw/cspg5bC+MW2F008Jqe9QJjmlGTnjLA7LBdgWC6svmwA05j1rqvSQ= 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=X1XIAc5i; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RBNNpa/c; 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="X1XIAc5i"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RBNNpa/c" Message-ID: <20240410165551.635345704@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789191; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=amj9rEI4omA2o9SPkzdGtXDAK0TjsBjy30f0aomhQz0=; b=X1XIAc5ij2T1DenkME9geSLVZ5+2chgjOLrHVCBPgXDu1pbdO6nF4K7Yd63o7BoELiiuqa I1Gtiuykz8zstGf10d0kLon2yKIQFSBmM2l2mcoma0pXmgCVi7EbGrBfawvL84DvTKIk0u 8YCPC25TYGpnSOLProlR/fEXpmC75DALdryHs+tnCTlaoVE+q6HhWLz5cnLDHkm5RcY3Z4 H4AAl3sXCzp7j711VgjxheNh35sX08s4FXFYrYRFNgkxyD6yhXvYhXlpdL2+Yh27IgX2v7 E5wtHysFQaWPD6YfdkCfDqISXa/gy+8R05c+WY+fZtqJwi6LLDbTb4zdPrv7CQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789191; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=amj9rEI4omA2o9SPkzdGtXDAK0TjsBjy30f0aomhQz0=; b=RBNNpa/c5LCQEjTTy+WVRDmuoswkLSi6v/fibcAYXPq/f/PWBE9oNctbolPglNeip7xT8Y BLhS5ggEoKVX8PCw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 11/50] posix-cpu-timers: Handle SIGEV_NONE timers correctly in timer_set() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:31 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expired SIGEV_NONE oneshot timers must return 0 nsec for the expiry time in timer_get(), but the posix CPU timer implementation returns 1 nsec. Add the missing conditional. This will be cleaned up in a follow up patch. Signed-off-by: Thomas Gleixner --- V2: Split out into new patch to make review simpler - Frederic --- kernel/time/posix-cpu-timers.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -706,7 +706,16 @@ static int posix_cpu_timer_set(struct k_ old_expires =3D exp - val; old->it_value =3D ns_to_timespec64(old_expires); } else { - old->it_value.tv_nsec =3D 1; + /* + * A single shot SIGEV_NONE timer must return 0, when it is + * expired! Timers which have a real signal delivery mode + * must return a remaining time greater than 0 because the + * signal has not yet been delivered. + */ + if (sigev_none) + old->it_value.tv_nsec =3D 0; + else + old->it_value.tv_nsec =3D 1; old->it_value.tv_sec =3D 0; } } From nobody Fri Dec 19 20:14:14 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 5AB62481A0 for ; Wed, 10 Apr 2024 22:46:35 +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=1712789196; cv=none; b=kCs8NgRLjZ1V6PKFRBRJhdgMyZGyPP9GJewrhFZDHGcatWgsg5u2U7mxMKVgj5FS1E740GYpkYPul1vywGOqU1yYfZ8VKiTPn5Iaek0vDm1efrjkNhlbEIEu9TTWmRIJZjn1EoKff6w3Pmze6pnBjJ3azg86BRAMhoswsgXFOZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789196; c=relaxed/simple; bh=UGVZy9yiNtE3R/qqXfALDVUSsmVr1T6CsA5jtA7JuEI=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=tWr8JNooFS8YV6LXoi4U36qxerJIvDUu5XxKEPwfkhKEI6rvqd14lr1g33n8tkpaKzLZc+7AqfJeNje5K4cJsNtRVj3uc6qkJ1AD3qXjgvaDpWTaQ0TX7CE9o8hPCPDAblzTX41nPZLKFc4XrJM97mUmkwnP/EwEz3fcwqM5IhU= 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=to5uQCzb; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=IKnaH5ee; 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="to5uQCzb"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="IKnaH5ee" Message-ID: <20240410165551.696591376@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789193; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=IjZXK69CXPSZdDcaR3Lrs+m7BfPVp6Q5CnwMmPhWl8E=; b=to5uQCzbmFKdomBLmv3NP7ql54Ca08WkprguSTAXcy7Z0KV6QEETqqCuobT8Xe11Ejyd1V jjo6KdBnvCjhNgBPTOFlJqS1QpahiOJIwqGn9+CmhJTekx+J0wHF3SFi12JhYmsfeVSz3l Pc0uOBSbDNIn9sbBgPLGI9luIGcJqSzB0Md0I9r8xBIzANYwTKVuHdT1WuJwAtptbmWDEC h1U0/jeAuTH5o89FRsQAUkMMd2IfQgS5xY53Hs3z/UPoE0NtnZ+XjVPSiWkhtJ4wnqLlYJ sjuk8nVRRDki3quH+z9fjvOff0cwVGoerKO9JxhjmjJznJfsJqHDQAW07o5FnA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789193; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=IjZXK69CXPSZdDcaR3Lrs+m7BfPVp6Q5CnwMmPhWl8E=; b=IKnaH5eeEkqdTNKYcQVmE8boxuVc4upmvjSu73JY/z8bqDPen2BZJQU9tx4kfNsbJQhHII UlkVvYUW/cSK2aCA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 12/50] posix-cpu-timers: Replace old expiry retrieval in posix_cpu_timer_set() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:33 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Reuse the split out __posix_cpu_timer_get() function which does already the right thing. No functional change. Signed-off-by: Thomas Gleixner Reviewed-by: Anna-Maria Behnsen --- V2: Adopted to previous changes --- kernel/time/posix-cpu-timers.c | 36 ++++++------------------------------ 1 file changed, 6 insertions(+), 30 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -614,6 +614,8 @@ static void cpu_timer_fire(struct k_itim } } =20 +static void __posix_cpu_timer_get(struct k_itimer *timer, struct itimerspe= c64 *itp, u64 now); + /* * Guts of sys_timer_settime for CPU timers. * This is called with the timer locked and interrupts disabled. @@ -688,37 +690,11 @@ static int posix_cpu_timer_set(struct k_ else val =3D cpu_clock_sample_group(clkid, p, true); =20 + /* Retrieve the previous expiry value if requested. */ if (old) { - if (old_expires =3D=3D 0) { - old->it_value.tv_sec =3D 0; - old->it_value.tv_nsec =3D 0; - } else { - /* - * Update the timer in case it has overrun already. - * If it has, we'll report it as having overrun and - * with the next reloaded timer already ticking, - * though we are swallowing that pending - * notification here to install the new setting. - */ - u64 exp =3D bump_cpu_timer(timer, val); - - if (val < exp) { - old_expires =3D exp - val; - old->it_value =3D ns_to_timespec64(old_expires); - } else { - /* - * A single shot SIGEV_NONE timer must return 0, when it is - * expired! Timers which have a real signal delivery mode - * must return a remaining time greater than 0 because the - * signal has not yet been delivered. - */ - if (sigev_none) - old->it_value.tv_nsec =3D 0; - else - old->it_value.tv_nsec =3D 1; - old->it_value.tv_sec =3D 0; - } - } + old->it_value =3D (struct timespec64){ }; + if (old_expires) + __posix_cpu_timer_get(timer, old, val); } =20 if (unlikely(ret)) { From nobody Fri Dec 19 20:14:14 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 EE14148787 for ; Wed, 10 Apr 2024 22:46:36 +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=1712789198; cv=none; b=ls9gpnSp7in0bQ2v2zP5VZiiu8WR7Dsg2pG7ZGU6H3k32pveFnxY7gTqHtj9/rFEvHQHkTQT1gw5Qno+OxM7JGMDzKDJq5p5EzkujeCNjjtUppWUrBcs3VGDPTWYutMQLOinxHGK1FjhSNleVnYorg4b21XwwWyKdDTVsIk5JaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789198; c=relaxed/simple; bh=XOVBbigKGjPLdX5P0pMrym6mQdm4eaTiAmzCqhAsQJU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Q8admTkqo4v+y2rDFAkfFT8FJENPyY83gYxjfNyoJz61sIZYVumOhmr/tKI3jO0C1wgpmuRr1trZtykLvxUamkQ3sk5KpxhfhvwrhsV0VRCdefpeokKUofOwDptmkGbdkkm6ilsyG2r3MAyGT2RpjZJ84bXmWE/+j7PTYuaFeDI= 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=KGMZ0QcS; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rh1zKcqn; 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="KGMZ0QcS"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rh1zKcqn" Message-ID: <20240410165551.757677561@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789195; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=osYubTMAcZxZdMMm1RQggfIXZUJTx+ldm1e90x9mqTY=; b=KGMZ0QcSsDrIFmz1HuXGMSEeYWKjpSHbfEvGRaRmkTuh/EH1WHIcjCMoFqnaRTWFDuOA2/ CvISAI8kzccU0Gl3fi21vDDtM0O+AfSEnqt0XOU6gBFFpcSFg6UPUGJPe2kh88wgKkntHE lgGly3PqA1cxnyGmrVXqPXm5DqxWZ2wggs94w7bP2839k18PkHym7eXrCIJJ+PHpkeIaR9 aYDKeIKt8wrs2Mfx/k3UXAqdQV+eAjnE4bGqzQIH6mdAKVS4gH5pax3IUE0rjQE2LZ32iQ i/o62jo+z3EKr0sqpEKa2Wq+88TFqWIqaKG2UpW6fBWT9dEa3mi5R53eyf1KnQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789195; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=osYubTMAcZxZdMMm1RQggfIXZUJTx+ldm1e90x9mqTY=; b=rh1zKcqnevxoG3D4vAAnbhlB4eisPLVMYLLIyfKIT9XpKcY3qHtT4egZhZLlzYu1JBAkiV IFzzTBhI18AcVBAQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 13/50] posix-cpu-timers: Do not arm SIGEV_NONE timers References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:34 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There is no point in arming SIGEV_NONE timers as they never deliver a signal. timer_gettime() is handling the expiry time correctly and that's all SIGEV_NONE timers care about. Prevent arming them and remove the expiry handler code which just disarms them. Signed-off-by: Thomas Gleixner Reviewed-by: Anna-Maria Behnsen --- V2: Split out into new patch to make review simpler - Frederic --- kernel/time/posix-cpu-timers.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -584,12 +584,7 @@ static void cpu_timer_fire(struct k_itim { struct cpu_timer *ctmr =3D &timer->it.cpu; =20 - if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) =3D=3D SIGEV_NONE) { - /* - * User don't want any signal. - */ - cpu_timer_setexpires(ctmr, 0); - } else if (unlikely(timer->sigq =3D=3D NULL)) { + if (unlikely(timer->sigq =3D=3D NULL)) { /* * This a special case for clock_nanosleep, * not a normal timer from sys_timer_create. @@ -625,6 +620,7 @@ static void __posix_cpu_timer_get(struct static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, struct itimerspec64 *new, struct itimerspec64 *old) { + bool sigev_none =3D timer->it_sigev_notify =3D=3D SIGEV_NONE; clockid_t clkid =3D CPUCLOCK_WHICH(timer->it_clock); u64 old_expires, new_expires, old_incr, val; struct cpu_timer *ctmr =3D &timer->it.cpu; @@ -688,7 +684,7 @@ static int posix_cpu_timer_set(struct k_ if (CPUCLOCK_PERTHREAD(timer->it_clock)) val =3D cpu_clock_sample(clkid, p); else - val =3D cpu_clock_sample_group(clkid, p, true); + val =3D cpu_clock_sample_group(clkid, p, !sigev_none); =20 /* Retrieve the previous expiry value if requested. */ if (old) { @@ -708,19 +704,20 @@ static int posix_cpu_timer_set(struct k_ goto out; } =20 - if (new_expires !=3D 0 && !(timer_flags & TIMER_ABSTIME)) { + /* Convert relative expiry time to absolute */ + if (new_expires && !(timer_flags & TIMER_ABSTIME)) new_expires +=3D val; - } + + /* Set the new expiry time (might be 0) */ + cpu_timer_setexpires(ctmr, new_expires); =20 /* - * Install the new expiry time (or zero). - * For a timer with no notification action, we don't actually - * arm the timer (we'll just fake it for timer_gettime). + * Arm the timer if it is not disabled, the new expiry value has + * not yet expired and the timer requires signal delivery. + * SIGEV_NONE timers are never armed. */ - cpu_timer_setexpires(ctmr, new_expires); - if (new_expires !=3D 0 && val < new_expires) { + if (!sigev_none && new_expires && val < new_expires) arm_timer(timer, p); - } =20 unlock_task_sighand(p, &flags); /* @@ -739,7 +736,7 @@ static int posix_cpu_timer_set(struct k_ timer->it_overrun_last =3D 0; timer->it_overrun =3D -1; =20 - if (val >=3D new_expires) { + if (!sigev_none && val >=3D new_expires) { if (new_expires !=3D 0) { /* * The designated time already passed, so we notify From nobody Fri Dec 19 20:14:14 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 BFB674AEF0 for ; Wed, 10 Apr 2024 22:46:38 +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=1712789200; cv=none; b=d853m5RklhWQmBuZQSUB6NpJOQACPQHZIvh540tL/ujfrqz37HjfAK1pT7+GCI50D5bH3djoq589M0A3V2dqpr7ZVwdDucaZx4CKODqOAtP7V5eZRnRgZkie7wSLz5i9RpHrH0M2Hvdf371jGoJQG3VUj8WSrW14soP+5nGgtJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789200; c=relaxed/simple; bh=dB7rimPTTgIr+XyZdIogR3o9dlxge5G3DTP9z9KIRro=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=UGsaTWrgT63cFBSW7Uzm5OI4m4OabavIhIxlihh12IBbpY+aLuL1hjOXXZ48PIoBLh2jck5tgGraSuFfZJsmBVij2owF71+pIp5rU6gl1cRQYEzaKvUi1ywBZJlh7l0V4Si5qHe1okNtVn/Q9ESXGwdxJVWd+mSbfK6kkWbGioI= 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=Zv7eZuDY; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=au0L6PkP; 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="Zv7eZuDY"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="au0L6PkP" Message-ID: <20240410165551.819762665@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789197; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=ExiTF3X05BrX2DL4sY3OO9bGQhEyAP9yIOBmxwKI0QM=; b=Zv7eZuDY6jfp+OF3wtQ59z6745cPcoDLK8WvDi3i5DVS7dGKFe9PCwYr23I594ocv4GBHB AZE83iVNQR8glt5Tq9PL7OipZam708qVuooL34IRFAAAsOa5WPJ8cZZfM3uTYtOI3cWSV7 1xhCM+Utdz+Aj63ZCXmopq2Qu3k8wB/rTInTPU8My0fjlqlyoWqMMcmnSsiswsyvtEFSl6 hmXAoJagN6N1ulJZF3Mg1bxarLAfJV3Wk6ttu1DFgfWFCM8urhKdaLudhU6dEo8/nD1ORs OH3JvzNuPXAY7Aju6ijSD81DcT6USWKVmVrNziVAOgokptwHQCfNjHDxijwIrQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789197; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=ExiTF3X05BrX2DL4sY3OO9bGQhEyAP9yIOBmxwKI0QM=; b=au0L6PkPGh5Pj8csUnTBrFeJaO+Ke1tmtONjPo9knLZS/Z5rMpHrvjCVTqua+l8RGyWn1j UfNn/slP3vzaBoDQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 14/50] posix-cpu-timers: Use @now instead of @val for clarity References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:36 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" posix_cpu_timer_set() uses @val as variable for the current time. That's confusing at best. Use @now as anywhere else and rewrite the confusing comment about clock sampling. No functional change. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- kernel/time/posix-cpu-timers.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -622,7 +622,7 @@ static int posix_cpu_timer_set(struct k_ { bool sigev_none =3D timer->it_sigev_notify =3D=3D SIGEV_NONE; clockid_t clkid =3D CPUCLOCK_WHICH(timer->it_clock); - u64 old_expires, new_expires, old_incr, val; + u64 old_expires, new_expires, old_incr, now; struct cpu_timer *ctmr =3D &timer->it.cpu; struct sighand_struct *sighand; struct task_struct *p; @@ -674,23 +674,19 @@ static int posix_cpu_timer_set(struct k_ } =20 /* - * We need to sample the current value to convert the new - * value from to relative and absolute, and to convert the - * old value from absolute to relative. To set a process - * timer, we need a sample to balance the thread expiry - * times (in arm_timer). With an absolute time, we must - * check if it's already passed. In short, we need a sample. + * Sample the current clock for saving the previous setting + * and for rearming the timer. */ if (CPUCLOCK_PERTHREAD(timer->it_clock)) - val =3D cpu_clock_sample(clkid, p); + now =3D cpu_clock_sample(clkid, p); else - val =3D cpu_clock_sample_group(clkid, p, !sigev_none); + now =3D cpu_clock_sample_group(clkid, p, !sigev_none); =20 /* Retrieve the previous expiry value if requested. */ if (old) { old->it_value =3D (struct timespec64){ }; if (old_expires) - __posix_cpu_timer_get(timer, old, val); + __posix_cpu_timer_get(timer, old, now); } =20 if (unlikely(ret)) { @@ -706,7 +702,7 @@ static int posix_cpu_timer_set(struct k_ =20 /* Convert relative expiry time to absolute */ if (new_expires && !(timer_flags & TIMER_ABSTIME)) - new_expires +=3D val; + new_expires +=3D now; =20 /* Set the new expiry time (might be 0) */ cpu_timer_setexpires(ctmr, new_expires); @@ -716,7 +712,7 @@ static int posix_cpu_timer_set(struct k_ * not yet expired and the timer requires signal delivery. * SIGEV_NONE timers are never armed. */ - if (!sigev_none && new_expires && val < new_expires) + if (!sigev_none && new_expires && now < new_expires) arm_timer(timer, p); =20 unlock_task_sighand(p, &flags); @@ -736,7 +732,7 @@ static int posix_cpu_timer_set(struct k_ timer->it_overrun_last =3D 0; timer->it_overrun =3D -1; =20 - if (!sigev_none && val >=3D new_expires) { + if (!sigev_none && now >=3D new_expires) { if (new_expires !=3D 0) { /* * The designated time already passed, so we notify From nobody Fri Dec 19 20:14:14 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 AC81A4D58E for ; Wed, 10 Apr 2024 22:46:40 +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=1712789202; cv=none; b=oNiBew9yxB97TiucGAlH8qoVspee9Ql+T9mb3ugGITUX5nwjtDSusrRYTeFlZ+hxxLl024hRe1Pf7wTPDCnoqQmqfYofr5si2Ku08nvgSZYZr39iFlqiUs9i8/Bi0LciF9HZUKqbEqSrin7Feqyxf/QlYt4xFmqSep2jsfcB3Fs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789202; c=relaxed/simple; bh=dqa4y3Sy5EE+IYLFY+hDqiZngky6H6vw41xP99gkF3Q=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=eDSUhJJVUnOdnMmHamtCazFSzLmgHosGiRBRo6bn4/zLgL75qIILKXGwk295lOQ9Guxr79QnjVWAkguAVxIA5GZCJ87w6eV8PSKtos90kFsaD/uafhHsX6FD8+Twwb6gpWo/gzur6e72OC8F2Ug15FM1vHP5QVYXC70xLhZGyYg= 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=N2STrY0D; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=X5ggaPAH; 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="N2STrY0D"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="X5ggaPAH" Message-ID: <20240410165551.881137309@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789198; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=4hHoBY9MrEXBowGP0DzQQAtC7OGDF1Jxkr/5VDlxgmk=; b=N2STrY0Dwm2Mv5xsMFM/qy1ueCHSxZo6g49pE4uzGHGWWRNPY/FOupisQimKKzAFxsLfXv yCSH8TTkyTcmV9KVpv8+3RY03uZPiHFk/HY3k00cGQkyaZkOlFrtJYUfExlkfWpA52+9Dl 9mpUjr6i2VuCWwaurhfUsRgD04jPFO+/dw4KCmff5kcVPKzfycqja5te8hLJKXLx2iQcQn JfiG/9J1cn6rTEWatb/KsImn+ZyJSb6vLbpyHaSuCRauFb1u7hqlPJ3yQOBSmRdC8BYXK1 bxesIYyvgUJ9CF83uw77EMbCo1QLvrxKN1k75F8/Hg/6jSN5VQgnz9z/e4vs2w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789198; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=4hHoBY9MrEXBowGP0DzQQAtC7OGDF1Jxkr/5VDlxgmk=; b=X5ggaPAHzS4RVSwb2VElGPi4Oa+EfCt/7GP8daM3Yeb7m67qmJrsPjntYCqWbo/LO3BPnw qBb2q/R2hTROa9Cw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 15/50] posix-cpu-timers: Remove incorrect comment in posix_cpu_timer_set() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:38 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A leftover from historical code which describes fiction. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- kernel/time/posix-cpu-timers.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -689,13 +689,8 @@ static int posix_cpu_timer_set(struct k_ __posix_cpu_timer_get(timer, old, now); } =20 + /* Retry if the timer expiry is running concurrently */ if (unlikely(ret)) { - /* - * We are colliding with the timer actually firing. - * Punt after filling in the timer's old value, and - * disable this firing since we are already reporting - * it as an overrun (thanks to bump_cpu_timer above). - */ unlock_task_sighand(p, &flags); goto out; } From nobody Fri Dec 19 20:14:14 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 5DFA64D112 for ; Wed, 10 Apr 2024 22:46:42 +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=1712789204; cv=none; b=kiowRvrsi+9rcr4c3x0UzGqhNyjgsOKpc27VF416EMNxHCrzA4slchsDqZk4BI82qjOt/l5sYAYuvd45pIpYE0V7jJQJ3JPZ1XzmXCluTVwFxQMWYSpjEXs9Yhsggz66Dpv01IrraIAVTJ50gi8x8SQcXD82ux5LXeOwKliOcXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789204; c=relaxed/simple; bh=jwLW+rNG7bMs4zQGx/O8eL+ZFKFCQ9a+FbpMQ0kFoaQ=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=km1gSvrwxgy3INsyTfGlzq+v9VQhczGYYra4Hp5pJwUOSG6vq2SGJcCB6PjKz7LaXjGG1+AQRVUrxwuHd7n4g+ONUKFpJ1IVaNrLyqicEkXDxZxbvohjNMxZOmZfYrB9H4oqJ40OAXvOReirVF7GBJF4tFmaAdzltqTRc35Vivk= 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=cjCI4sb2; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=MtBQOmX9; 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="cjCI4sb2"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="MtBQOmX9" Message-ID: <20240410165551.943625183@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=prXY1arwiBmGreObgLsf7R5UB66La837yy2ta+3sTC4=; b=cjCI4sb2+ouxHsXnpuMYh8rkL5BNqBue0YnhK4whsbmKl9PTRMA5eSNJDwFS72Z+VXMXH3 XQFD5O/sVegZGi+bc/CezdFtt+B1BHo9SE2z7ghaaoihKQySMaIzbRpzXD9mxYomOSIhKz 9Y2sW0E6OQsDSaBM7A8/p26iNUcpfY+IX1zxckTb7Y6s7mcwPpV7JSw9t6coNxZvOTLt21 3Og2ZwFNYdXraNQtNXKqKKPrG2w/WlFvnWE3PNX+zyBV2JS93EwqY+TBfBy0LkjsS2RV2n PrGIHQnxo4Ki42hxVPZmG5U5N7Fw3TSb28l9wemS2l0MlAY0X+hFFdj6d7EUvQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=prXY1arwiBmGreObgLsf7R5UB66La837yy2ta+3sTC4=; b=MtBQOmX99XXLjGA+DYB/y8fzrQ7xInDBvvCcUod2moCFArtFK0y2CmGIgtqblp+p9x0LOx 6Gnt3aArXdCX98Dg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 16/50] posix-cpu-timers: Simplify posix_cpu_timer_set() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:40 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Avoid the late sighand lock/unlock dance when a timer is not armed to enforce reevaluation of the timer base so that the process wide CPU timer sampling can be disabled. Do it right at the point where the arming decision is made which already has sighand locked. Signed-off-by: Thomas Gleixner Reviewed-by: Anna-Maria Behnsen --- kernel/time/posix-cpu-timers.c | 44 +++++++++++++++---------------------= ----- 1 file changed, 17 insertions(+), 27 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -705,10 +705,16 @@ static int posix_cpu_timer_set(struct k_ /* * Arm the timer if it is not disabled, the new expiry value has * not yet expired and the timer requires signal delivery. - * SIGEV_NONE timers are never armed. + * SIGEV_NONE timers are never armed. In case the timer is not + * armed, enforce the reevaluation of the timer base so that the + * process wide cputime counter can be disabled eventually. */ - if (!sigev_none && new_expires && now < new_expires) - arm_timer(timer, p); + if (likely(!sigev_none)) { + if (new_expires && now < new_expires) + arm_timer(timer, p); + else + trigger_base_recalc_expires(timer, p); + } =20 unlock_task_sighand(p, &flags); /* @@ -727,30 +733,14 @@ static int posix_cpu_timer_set(struct k_ timer->it_overrun_last =3D 0; timer->it_overrun =3D -1; =20 - if (!sigev_none && now >=3D new_expires) { - if (new_expires !=3D 0) { - /* - * The designated time already passed, so we notify - * immediately, even if the thread never runs to - * accumulate more time on this clock. - */ - cpu_timer_fire(timer); - } - - /* - * Make sure we don't keep around the process wide cputime - * counter or the tick dependency if they are not necessary. - */ - sighand =3D lock_task_sighand(p, &flags); - if (!sighand) - goto out; - - if (!cpu_timer_queued(ctmr)) - trigger_base_recalc_expires(timer, p); - - unlock_task_sighand(p, &flags); - } - out: + /* + * If the new expiry time was already in the past the timer was not + * queued. Fire it immediately even if the thread never runs to + * accumulate more time on this clock. + */ + if (!sigev_none && new_expires && now >=3D new_expires) + cpu_timer_fire(timer); +out: rcu_read_unlock(); if (old) old->it_interval =3D ns_to_timespec64(old_incr); From nobody Fri Dec 19 20:14:14 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 6F9504F5EA for ; Wed, 10 Apr 2024 22:46:44 +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=1712789205; cv=none; b=er5IjfrzO+Ara+F3xnu77EeUtZ79ZCaWdDv/a92ifcLEcRJel6oYLhLTHlOQZkN1RikymoXcH6Ex+HLRIJtUWuP4cK/XQSvu2HDdXHpRGvW1CxnVf8yMDzN8Fw1WVqxQpoqMpcgtMbjyicX9Hg5EAfe84y+hTOEwoDfwwFyIJtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789205; c=relaxed/simple; bh=M9Q7ax6L6HgZ2sYcMaynew0ayWNP6jxDDhPzY9nkD8E=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=UsyrI5lpWVVWaBGoK2ambXpD+x4pm2EnG3UUUIO7V0zvQgj7WkUXjniwHcyFstlFFCOEkEt1kf3vlT2NHJt94hnr2jw8qR9aKZupdiiXMgPs4XNKpU0JcptC8SSlwMnzFWae8g8fNinvtMGHViRqgknuDqvx+8JDdKG+OTd1WRE= 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=C01lQgvn; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=1S9lztVW; 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="C01lQgvn"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1S9lztVW" Message-ID: <20240410165552.006883398@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789202; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=a4kBoLxoimUoUZk3ev7GSbKQUyXJ3UwY85qwVJP4oio=; b=C01lQgvnpJfKiWn4HWNf7Sv/QhuG1SRBw6FdV/B0Agr60V6KbpT7YrMuIL/cZM25UFPClm rJTx5ssCYKQLh9HiVlL8h6Ythh6i0GHyjapo0AxxlcLNpLeGEYtWUaxfdn0UZFw4mx1ELR AOIZYzL7+IcQwDucOaGQQcUAs0LpkdR5hO1t4wG/sfvdXX+g7RK1CaK/G3c3drgojlpjWR tdQYsjV0w7Biy9Qhx1DxCdVeDuwAGDycEBZpkrDtbpDH4NbnyumfMyG1essOCZas/V0gfi Ms4ApNoawa9EFvwtNvLD6nQknODJ8nUBCG27MY0NA+ZYXfR4GXr7ZtoCL92LOg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789202; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=a4kBoLxoimUoUZk3ev7GSbKQUyXJ3UwY85qwVJP4oio=; b=1S9lztVWxLgK1OLkPLMZ26JATwvZaSWE9lLqTLHm73YPwWss7AwpGc+1+m0h5iYVXlLPUP J4ouymcsEEifd/BQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 17/50] posix-timers: Retrieve interval in common timer_settime() code References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:41 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" No point in doing this all over the place. Signed-off-by: Thomas Gleixner Reviewed-by: Anna-Maria Behnsen --- V2: Removed the timer_gettime() part - Frederic --- kernel/time/posix-cpu-timers.c | 10 ++-------- kernel/time/posix-timers.c | 5 ++++- 2 files changed, 6 insertions(+), 9 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -622,8 +622,8 @@ static int posix_cpu_timer_set(struct k_ { bool sigev_none =3D timer->it_sigev_notify =3D=3D SIGEV_NONE; clockid_t clkid =3D CPUCLOCK_WHICH(timer->it_clock); - u64 old_expires, new_expires, old_incr, now; struct cpu_timer *ctmr =3D &timer->it.cpu; + u64 old_expires, new_expires, now; struct sighand_struct *sighand; struct task_struct *p; unsigned long flags; @@ -660,10 +660,7 @@ static int posix_cpu_timer_set(struct k_ return -ESRCH; } =20 - /* - * Disarm any old timer after extracting its expiry time. - */ - old_incr =3D timer->it_interval; + /* Retrieve the current expiry time before disarming the timer */ old_expires =3D cpu_timer_getexpires(ctmr); =20 if (unlikely(timer->it.cpu.firing)) { @@ -742,9 +739,6 @@ static int posix_cpu_timer_set(struct k_ cpu_timer_fire(timer); out: rcu_read_unlock(); - if (old) - old->it_interval =3D ns_to_timespec64(old_incr); - return ret; } =20 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -904,7 +904,7 @@ static int do_timer_settime(timer_t time const struct k_clock *kc; struct k_itimer *timr; unsigned long flags; - int error =3D 0; + int error; =20 if (!timespec64_valid(&new_spec64->it_interval) || !timespec64_valid(&new_spec64->it_value)) @@ -918,6 +918,9 @@ static int do_timer_settime(timer_t time if (!timr) return -EINVAL; =20 + if (old_spec64) + old_spec64->it_interval =3D ktime_to_timespec64(timr->it_interval); + kc =3D timr->kclock; if (WARN_ON_ONCE(!kc || !kc->timer_set)) error =3D -EINVAL; From nobody Fri Dec 19 20:14:14 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 F204B50249 for ; Wed, 10 Apr 2024 22:46:45 +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=1712789207; cv=none; b=Vw2wii4I/uzYEwboTY190VVDL2VK17DfljHHqLXS3tp3Vh3fxU4e0deuSnhVfXJOYTTCFWE7peHdFyrynfocVTVz+ukS3ON2c1XR1FRVBHLWlfZcr2YLyQmtZF10aDH4TRPXE5aQidMBNgQ5AnG9Ri/OhmS1/F+FwLZsaRkOYSs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789207; c=relaxed/simple; bh=9JR/luG7t5T3jOUPOePLWiqmKbhtO9ZuNt63a7+o1js=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=fdKW8WDrDdYjLG8uS92FM2UsMRvZAxi/3Gb6wzZyNbSTPIziFo2311psF289sFMBJzpFENWxyc40aMwmyavSj4FSQoFZCw5BOYrniXHdPga8mDqU2uwERteuDRT+FRL3JygyY66UQS8/lv6oworWE8ZsEPunwqyi/5dQuSxmaSY= 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=KEfir5ut; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mRPtHIDZ; 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="KEfir5ut"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mRPtHIDZ" Message-ID: <20240410165552.069350753@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789204; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=eV59jzwlnvMqC78q7RxRP7GMuITiFT7fNLVqSGWWbAc=; b=KEfir5utURRdFHULxGLSLgNBqucoflI9ebdqhPJ2zP9A2YFAVFjRuBAtDucpHCjNfqAWeN PULK8sogl1TVIkLuhtYCYAAxIHIh1JIWZVe9McPVuy7zqKat2JDQyfwQLXcR0GlivapuVd BH+gNrWg6ZmBgeOeAqwbpyYk71rwyd0/uWeVmyOcEV/L3lYoElNcgFw+B5QeKL695qGxBD sCNxh8NfqIPz4l0gOo32pc656kaxeVqny/M+L+4J4016mRw+XSPEpNPl2h9Lod7H4+FMB6 uxVOTT6r3ldmRrzd6H75XNasqe4O7FuyzQMfkzehGFmflpD5fzk9xp2oxzuPvw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789204; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=eV59jzwlnvMqC78q7RxRP7GMuITiFT7fNLVqSGWWbAc=; b=mRPtHIDZwPxnELHsnfmJe5k2TmYBxInFypS+d40LNMIv4kSRS5YGx2Lu//iqJHaXroHNcG bHdZfBC7POS0/KCg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 18/50] posix-timers: Clear overrun in common_timer_set() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:43 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Keeping the overrun count of the previous setup around is just wrong. The new setting has nothing to do with the previous one and has to start from a clean slate. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- kernel/time/posix-timers.c | 1 + 1 file changed, 1 insertion(+) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -881,6 +881,7 @@ int common_timer_set(struct k_itimer *ti timr->it_requeue_pending =3D (timr->it_requeue_pending + 2) & ~REQUEUE_PENDING; timr->it_overrun_last =3D 0; + timr->it_overrun =3D -1LL; =20 /* Switch off the timer when it_value is zero */ if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) From nobody Fri Dec 19 20:14:14 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 96A0F50A79 for ; Wed, 10 Apr 2024 22:46:47 +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=1712789209; cv=none; b=fUCFX700RVWEDZdOd2P9gOoJxmK6nhmR1ecO4AoovN7QI80EGZqTNPuIx4A+NgLpXRXGHHhYcQdTPjc5YeCkSOW9yWwhvLdqdg5XY8a0iH3b3u4HqsHFIMJSvAxB5LJKLmxkBp2cy0AXQw39ggfLFsf+sPYWRp7xF5yvTAvV0eY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789209; c=relaxed/simple; bh=pmCbpSGE0XRK0ukKqo1U6qxPo/JyzQDl0umJpcWyXGY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Ig+F5iJOsb9ndo0oZnyV6p1k6QlH1N0tTODxwyz3Bk1jAKHKSdNoj5ENfQX3hNTnRhs94q7pZ1x6EKsPLkI6RnMQg40vkSd4kegchaTxipPT69BJCArbTqBKGGlLJt3z9+woH5rPQ2dsj1fK8sCjx4+Xstr+yxDffh/Y0dfJibY= 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=HEm37kCg; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wKIWlwuE; 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="HEm37kCg"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wKIWlwuE" Message-ID: <20240410165552.133844381@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789206; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=6GMzkiAHRvFkC+fzhDyL20WSpzWzUN7g85C6W+O5hxI=; b=HEm37kCgKIYq1xXA7Xhxbl8h4sWdXv5kXz//YqGI7k0AiHBNWfn5QH7xh2DBmS5ZoquJGq PMTySSzPlY741xYVP2ymhWjlggwQxjlmxGLEcQewa0YooznKYwuyZLoqFALSGSFsxciM1F CJ05jPO5CelgyoR9ByWYa9DoMeLmqtCh1hrnaZuOA7WVSYK+AHI+0fSWa8V9o6PlaApjyI GOou4O7ThGoRJadYOM4Zmj6Kt5/ODvp5tf9dps9lrbQeBjoUGAHRsAucLYFGmWsPxxDtqN liYOawtC7FkrWIdKaDZdFRSoOTYCKXOEPs4GoSV+W2WhCZh+zGscw0J8EhEmmw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789206; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=6GMzkiAHRvFkC+fzhDyL20WSpzWzUN7g85C6W+O5hxI=; b=wKIWlwuE0WHxWUlDrbEqKDSA7uQv9EvXU/k4BPGT34IeHBUL/ycWuYeH3SxjCx0NvXEnmi SrL/KGTagzcUCYAQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 19/50] posix-timers: Convert timer list to hlist References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:45 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" No requirement for a real list. Spare a few bytes. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- fs/proc/base.c | 6 +++--- include/linux/posix-timers.h | 2 +- include/linux/sched/signal.h | 2 +- init/init_task.c | 2 +- kernel/fork.c | 2 +- kernel/time/posix-timers.c | 19 ++++++++----------- 6 files changed, 15 insertions(+), 18 deletions(-) --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2456,13 +2456,13 @@ static void *timers_start(struct seq_fil if (!tp->sighand) return ERR_PTR(-ESRCH); =20 - return seq_list_start(&tp->task->signal->posix_timers, *pos); + return seq_hlist_start(&tp->task->signal->posix_timers, *pos); } =20 static void *timers_next(struct seq_file *m, void *v, loff_t *pos) { struct timers_private *tp =3D m->private; - return seq_list_next(v, &tp->task->signal->posix_timers, pos); + return seq_hlist_next(v, &tp->task->signal->posix_timers, pos); } =20 static void timers_stop(struct seq_file *m, void *v) @@ -2491,7 +2491,7 @@ static int show_timer(struct seq_file *m [SIGEV_THREAD] =3D "thread", }; =20 - timer =3D list_entry((struct list_head *)v, struct k_itimer, list); + timer =3D hlist_entry((struct hlist_node *)v, struct k_itimer, list); notify =3D timer->it_sigev_notify; =20 seq_printf(m, "ID: %d\n", timer->it_id); --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -158,7 +158,7 @@ static inline void posix_cputimers_init_ * @rcu: RCU head for freeing the timer. */ struct k_itimer { - struct list_head list; + struct hlist_node list; struct hlist_node t_hash; spinlock_t it_lock; const struct k_clock *kclock; --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -137,7 +137,7 @@ struct signal_struct { =20 /* POSIX.1b Interval Timers */ unsigned int next_posix_timer_id; - struct list_head posix_timers; + struct hlist_head posix_timers; =20 /* ITIMER_REAL timer for the process */ struct hrtimer real_timer; --- a/init/init_task.c +++ b/init/init_task.c @@ -29,7 +29,7 @@ static struct signal_struct init_signals .cred_guard_mutex =3D __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), .exec_update_lock =3D __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS - .posix_timers =3D LIST_HEAD_INIT(init_signals.posix_timers), + .posix_timers =3D HLIST_HEAD_INIT, .cputimer =3D { .cputime_atomic =3D INIT_CPUTIME_ATOMIC, }, --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1876,7 +1876,7 @@ static int copy_signal(unsigned long clo prev_cputime_init(&sig->prev_cputime); =20 #ifdef CONFIG_POSIX_TIMERS - INIT_LIST_HEAD(&sig->posix_timers); + INIT_HLIST_HEAD(&sig->posix_timers); hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); sig->real_timer.function =3D it_real_fn; #endif --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -515,7 +515,7 @@ static int do_timer_create(clockid_t whi spin_lock_irq(¤t->sighand->siglock); /* This makes the timer valid in the hash table */ WRITE_ONCE(new_timer->it_signal, current->signal); - list_add(&new_timer->list, ¤t->signal->posix_timers); + hlist_add_head(&new_timer->list, ¤t->signal->posix_timers); spin_unlock_irq(¤t->sighand->siglock); /* * After unlocking sighand::siglock @new_timer is subject to @@ -1025,7 +1025,7 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t } =20 spin_lock(¤t->sighand->siglock); - list_del(&timer->list); + hlist_del(&timer->list); spin_unlock(¤t->sighand->siglock); /* * A concurrent lookup could check timer::it_signal lockless. It @@ -1075,7 +1075,7 @@ static void itimer_delete(struct k_itime =20 goto retry_delete; } - list_del(&timer->list); + hlist_del(&timer->list); =20 /* * Setting timer::it_signal to NULL is technically not required @@ -1096,22 +1096,19 @@ static void itimer_delete(struct k_itime */ void exit_itimers(struct task_struct *tsk) { - struct list_head timers; - struct k_itimer *tmr; + struct hlist_head timers; =20 - if (list_empty(&tsk->signal->posix_timers)) + if (hlist_empty(&tsk->signal->posix_timers)) return; =20 /* Protect against concurrent read via /proc/$PID/timers */ spin_lock_irq(&tsk->sighand->siglock); - list_replace_init(&tsk->signal->posix_timers, &timers); + hlist_move_list(&tsk->signal->posix_timers, &timers); spin_unlock_irq(&tsk->sighand->siglock); =20 /* The timers are not longer accessible via tsk::signal */ - while (!list_empty(&timers)) { - tmr =3D list_first_entry(&timers, struct k_itimer, list); - itimer_delete(tmr); - } + while (!hlist_empty(&timers)) + itimer_delete(hlist_entry(timers.first, struct k_itimer, list)); } =20 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, From nobody Fri Dec 19 20:14:14 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 9A58E51C43 for ; Wed, 10 Apr 2024 22:46:49 +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=1712789211; cv=none; b=AKllEtvU0gRbrGAkeeRZQsiCbK50YgSARn3EEhcdaMxjjlUdTJ9q7BfqrczV/ZN9RXIi7Zx0KGTZ0phQ+bTjWEDKmDG1LA6Hha/fszTzGS21qh6z/MHjEFtFCoHcNrBiTsKgDa1Hw0CaLIdtFnLsdEtw+icRRvhZKQ16ilaLDa0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789211; c=relaxed/simple; bh=YN6n6j+MA0nG5Yheb3hxrtVPCRI7lZJF9lPKtB8/yQI=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=nZ6r6vuTlnaahmttOwwLVNoHaE4601ski+cAE8sXwvFWN1YxGjOn2B7io3ObVGFPJ68fd3ceai91jDK7rfDjvnlOzOcnI/cQ/r4tWTh/sb6oPOP/VUFfRnmuPrIAPmSFigH0b3wJ3DFAM0xV1HbAZIJBJJ8Q4YwCv50B9St+Uak= 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=fDTxmrpT; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=B/t61fbX; 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="fDTxmrpT"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="B/t61fbX" Message-ID: <20240410165552.194398633@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789207; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=ITJ/+5vbw/2+B5QmAsO44YK98eSbLVo+fKwi2ezjk6Q=; b=fDTxmrpTo2NlMooJbp4usfDzBZjBMXQhEEOxDyVm1DFm9Fyn9nHxac++HjaqdPiBUhO44w NuytRBwFIKznXcpOHb2HhjC3zzr2p6/ZSkQ7N1BaAzQW3laNZrq6SEOtDyNAA7mad3PiHF oM1Pn3iDdV4TRLlpf09OR5oZnEXWEyU6LhSVbF6AV9uRHUkbXlJ3kTusWm2MvYlXGQvPH9 nvES6MR2oRDhT8n6dgagemH8P7UUh4bJTdju5+dVXtk5mo4R6c62vbDqZ6G4s5/PhsPNhE UPuD0KFnFkGvPnbDKmELybVDsVV3BV31bYK1dtCcBgxgnKYnH9vmMa/qE4VbBQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789207; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=ITJ/+5vbw/2+B5QmAsO44YK98eSbLVo+fKwi2ezjk6Q=; b=B/t61fbX3yQbzg36U2pGGfymXC3tXs1q+dVrhPMpq9Vle37tNpUEjwWMcQPa8hyuqBk4Dd Z3DRb1tUNEPap1Bg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 20/50] posix-timers: Consolidate timer setup References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:47 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" hrtimer based and CPU timers have their own way to install the new interval and to reset overrun and signal handling related data. Create a helper function and do the same operation for all variants. This also makes the handling of the interval consistent. It's only stored when the timer is actually armed, i.e. timer->it_value !=3D 0. Before that = it was stored unconditionally for posix CPU timers and conditionally for the other posix timers. Signed-off-by: Thomas Gleixner --- kernel/time/posix-cpu-timers.c | 15 +-------------- kernel/time/posix-timers.c | 25 +++++++++++++++++++------ kernel/time/posix-timers.h | 1 + 3 files changed, 21 insertions(+), 20 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -714,21 +714,8 @@ static int posix_cpu_timer_set(struct k_ } =20 unlock_task_sighand(p, &flags); - /* - * Install the new reload setting, and - * set up the signal and overrun bookkeeping. - */ - timer->it_interval =3D timespec64_to_ktime(new->it_interval); =20 - /* - * This acts as a modification timestamp for the timer, - * so any automatic reload attempt will punt on seeing - * that we have reset the timer manually. - */ - timer->it_requeue_pending =3D (timer->it_requeue_pending + 2) & - ~REQUEUE_PENDING; - timer->it_overrun_last =3D 0; - timer->it_overrun =3D -1; + posix_timer_set_common(timer, new); =20 /* * If the new expiry time was already in the past the timer was not --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -856,6 +856,23 @@ static struct k_itimer *timer_wait_runni return lock_timer(timer_id, flags); } =20 +/* + * Set up the new interval and reset the signal delivery data + */ +void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *n= ew_setting) +{ + if (new_setting->it_value.tv_sec || new_setting->it_value.tv_nsec) + timer->it_interval =3D timespec64_to_ktime(new_setting->it_interval); + else + timer->it_interval =3D 0; + + /* Prevent reloading in case there is a signal pending */ + timer->it_requeue_pending =3D (timer->it_requeue_pending + 2) & ~REQUEUE_= PENDING; + /* Reset overrun accounting */ + timer->it_overrun_last =3D 0; + timer->it_overrun =3D -1LL; +} + /* Set a POSIX.1b interval timer. */ int common_timer_set(struct k_itimer *timr, int flags, struct itimerspec64 *new_setting, @@ -878,16 +895,12 @@ int common_timer_set(struct k_itimer *ti return TIMER_RETRY; =20 timr->it_active =3D 0; - timr->it_requeue_pending =3D (timr->it_requeue_pending + 2) & - ~REQUEUE_PENDING; - timr->it_overrun_last =3D 0; - timr->it_overrun =3D -1LL; + posix_timer_set_common(timr, new_setting); =20 - /* Switch off the timer when it_value is zero */ + /* Keep timer disarmed when it_value is zero */ if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) return 0; =20 - timr->it_interval =3D timespec64_to_ktime(new_setting->it_interval); expires =3D timespec64_to_ktime(new_setting->it_value); if (flags & TIMER_ABSTIME) expires =3D timens_ktime_to_host(timr->it_clock, expires); --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -42,4 +42,5 @@ void common_timer_get(struct k_itimer *t int common_timer_set(struct k_itimer *timr, int flags, struct itimerspec64 *new_setting, struct itimerspec64 *old_setting); +void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *n= ew_setting); int common_timer_del(struct k_itimer *timer); From nobody Fri Dec 19 20:14:14 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 6AD4C524D6 for ; Wed, 10 Apr 2024 22:46:51 +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=1712789212; cv=none; b=Ipfu1oikJaUUR7VK5iDd+pEvgWs8fR5mhPP+QmI5d9a36L5R3F7jXx5kOEX5tuV2mfVJIJwbVYf2acV4LxRNdZfR/98Dcb+sgVxlmC2GuADkCo6ZotgyigG4LPagrte7s/Lj59C9Gx3F8sGhdcg+kYF2Gyt4VCQ3mRRIpY2QmQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789212; c=relaxed/simple; bh=vhFqJQGzuzFxet5n8aXmRbKfqxdROxlKhbpES6yLgrw=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=En8uITqZj243jotODJpxZ1f9h8NacWV6Jl6IMO63BSPJ0aSk9b3lG2C/206NvH0KrLmyguAWIMn7OuFe3xr0gk6lZbOltzEOJ0wD8t3T6pQOrhBKo3B7+CKpZbx8M1yaYVZq3e25kIFYaPQar9wOZwyQkSryZXygHkzpgOZMzXk= 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=n0xnL1QA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TvDSjwNs; 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="n0xnL1QA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TvDSjwNs" Message-ID: <20240410165552.256162566@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789209; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=ftWWtT5tkPA9yVyOVqrZETMprBLvt80uYl9BSpWJ448=; b=n0xnL1QAZmGNIlBKmm5arU5JQ8EByh6Z8pzopn+PzlDB8DVN/rrHfFRVhysvd3GxxbZAOF yfK/BhFt9b8C0VR8XMhL5i6uwoMEpvIIh+Bb3tuW5jUeqpKkIjzU6JKcCUAsJ5+n6++sqZ 01KSkyzPWn8mQsUi422bDWabuYRfnQOXPQUhnWl2PvzAVUbEc+k9f0MA/1RgZkPdUkg4m1 gRzapxLU7BkbhRgPEuNlotXHQIkaa5iLw/2zrAFMBGlSkF6CMm/7SOwrCxx+yXk2m1PXVL GklRu8d5SMDPqWULVvkYK3Z/OqpBffflp4xnDVlRFqCZL5m8KMUJjcxqfY2AXg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789209; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=ftWWtT5tkPA9yVyOVqrZETMprBLvt80uYl9BSpWJ448=; b=TvDSjwNsrAIUqpDrYI/G4XS8W+WaEhBJ0yKCEifTAo82F9+E+09y1F0LfrSlpfEEwxAgrz w9ByE4gpnpXu9eDg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 21/50] posix-cpu-timers: Make k_itimer::it_active consistent References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:49 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Posix CPU timers are not updating k_itimer::it_active which makes it impossible to base decisions in the common posix timer code on it. Update it when queueing or dequeueing posix CPU timers. Signed-off-by: Thomas Gleixner Reviewed-by: Anna-Maria Behnsen --- V2: Move the clearing to cpu_timer_fire() - Frederic --- kernel/time/posix-cpu-timers.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -453,6 +453,7 @@ static void disarm_timer(struct k_itimer struct cpu_timer *ctmr =3D &timer->it.cpu; struct posix_cputimer_base *base; =20 + timer->it_active =3D 0; if (!cpu_timer_dequeue(ctmr)) return; =20 @@ -559,6 +560,7 @@ static void arm_timer(struct k_itimer *t struct cpu_timer *ctmr =3D &timer->it.cpu; u64 newexp =3D cpu_timer_getexpires(ctmr); =20 + timer->it_active =3D 1; if (!cpu_timer_enqueue(&base->tqhead, ctmr)) return; =20 @@ -584,6 +586,7 @@ static void cpu_timer_fire(struct k_itim { struct cpu_timer *ctmr =3D &timer->it.cpu; =20 + timer->it_active =3D 0; if (unlikely(timer->sigq =3D=3D NULL)) { /* * This a special case for clock_nanosleep, @@ -668,6 +671,7 @@ static int posix_cpu_timer_set(struct k_ ret =3D TIMER_RETRY; } else { cpu_timer_dequeue(ctmr); + timer->it_active =3D 0; } =20 /* From nobody Fri Dec 19 20:14:14 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 CDE5853392 for ; Wed, 10 Apr 2024 22:46:52 +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=1712789214; cv=none; b=Wo/eOJMy9K5blbJs7IjXdt8MENIMGOAd1PZqdxM3qpaNCobEEz1JqeysTvFJkVkx1u4v6tPwnx6w2yMwO9BkxAt8P4rSX5cmj6OS2AaI+1tDILjwAHb0EbIpVbuANIW8UeLKmclcS12qrgk5F3ylYqoG0igHGWyHmkkB8oQXK08= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789214; c=relaxed/simple; bh=ur+O419OtXBNtUIZuacqd+BURpp8/MYytOmiM6ylieg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=txV645223cHplSn1oJgbAZMOi5cfVVEQnMpLaAXz6cx4UP1JhLs5LFs0/ZjB1valkEVjHnk22rYjHEDnJnFHbvHr13VWRleqMGnF1vIwSQVh+JhZvjhohQsVy5C6ndoaahWRJMJnWo8FfRPiPKWM8sPXK6WmgYLo2LdUfT1gxQc= 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=C5LtzvpX; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=+bzqUESy; 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="C5LtzvpX"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="+bzqUESy" Message-ID: <20240410165552.319533154@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789211; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=uSjFneRht2ixPq2dOvLaSspY3kohFRB8UKUX6K60b2g=; b=C5LtzvpX8y2eS6SFUSY2IIVe7GEx2gr3bFqrXADZvzvpvg62rlH5uVI8u+dPmBmPiOkJgL ItrsHRvQx4AdzCjJk7os+ZEf9l01PO8RvpJCm8DJZhFBmMa3s8i9Q9U/q3A1YIhu38amYD zK9rus3KzNjwaOB35DCft4SANSTHgMyZIgDZdQ01iIzgnghFibtrMRp7TdzuoAfJLBG2ib WdTt5EY4hSF1HHsIHX6BVZz8s7aV/CmgBfAOkPY9TofZkEdxl6OExoAj/WX7gVde3Zs1ie D3A6cIsJpQPg3X9kTA9r414KD7FmrUpYatWR6phEsiJE+hyrfI6h+sVJGAOVxw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789211; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=uSjFneRht2ixPq2dOvLaSspY3kohFRB8UKUX6K60b2g=; b=+bzqUESyV5ujgVRnBoGxUqfAwarPWgcpS72c1w5t4ahP7w5a2rw0fXQbNwGXGX3gJ82r8S eeR6u2X4ipRrhmAg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 22/50] posix-timers: Consolidate signal queueing References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:50 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename posix_timer_event() to posix_timer_queue_signal() as this is what the function is about. Consolidate the requeue pending and deactivation updates into that function as there is no point in doing this in all incarnations of posix timers. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- kernel/time/alarmtimer.c | 7 +------ kernel/time/posix-cpu-timers.c | 4 ++-- kernel/time/posix-timers.c | 21 +++++++++++---------- kernel/time/posix-timers.h | 2 +- 4 files changed, 15 insertions(+), 19 deletions(-) --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -574,15 +574,10 @@ static enum alarmtimer_restart alarm_han it.alarm.alarmtimer); enum alarmtimer_restart result =3D ALARMTIMER_NORESTART; unsigned long flags; - int si_private =3D 0; =20 spin_lock_irqsave(&ptr->it_lock, flags); =20 - ptr->it_active =3D 0; - if (ptr->it_interval) - si_private =3D ++ptr->it_requeue_pending; - - if (posix_timer_event(ptr, si_private) && ptr->it_interval) { + if (posix_timer_queue_signal(ptr) && ptr->it_interval) { /* * Handle ignored signals and rearm the timer. This will go * away once we handle ignored signals proper. Ensure that --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -598,9 +598,9 @@ static void cpu_timer_fire(struct k_itim /* * One-shot timer. Clear it as soon as it's fired. */ - posix_timer_event(timer, 0); + posix_timer_queue_signal(timer); cpu_timer_setexpires(ctmr, 0); - } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { + } else if (posix_timer_queue_signal(timer)) { /* * The signal did not get queued because the signal * was ignored, so we won't get any callback to --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -277,10 +277,17 @@ void posixtimer_rearm(struct kernel_sigi unlock_timer(timr, flags); } =20 -int posix_timer_event(struct k_itimer *timr, int si_private) +int posix_timer_queue_signal(struct k_itimer *timr) { + int ret, si_private =3D 0; enum pid_type type; - int ret; + + lockdep_assert_held(&timr->it_lock); + + timr->it_active =3D 0; + if (timr->it_interval) + si_private =3D ++timr->it_requeue_pending; + /* * FIXME: if ->sigq is queued we can race with * dequeue_signal()->posixtimer_rearm(). @@ -309,19 +316,13 @@ int posix_timer_event(struct k_itimer *t */ static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) { + struct k_itimer *timr =3D container_of(timer, struct k_itimer, it.real.ti= mer); enum hrtimer_restart ret =3D HRTIMER_NORESTART; - struct k_itimer *timr; unsigned long flags; - int si_private =3D 0; =20 - timr =3D container_of(timer, struct k_itimer, it.real.timer); spin_lock_irqsave(&timr->it_lock, flags); =20 - timr->it_active =3D 0; - if (timr->it_interval !=3D 0) - si_private =3D ++timr->it_requeue_pending; - - if (posix_timer_event(timr, si_private)) { + if (posix_timer_queue_signal(timr)) { /* * The signal was not queued due to SIG_IGN. As a * consequence the timer is not going to be rearmed from --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -36,7 +36,7 @@ extern const struct k_clock clock_proces extern const struct k_clock clock_thread; extern const struct k_clock alarm_clock; =20 -int posix_timer_event(struct k_itimer *timr, int si_private); +int posix_timer_queue_signal(struct k_itimer *timr); =20 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_sett= ing); int common_timer_set(struct k_itimer *timr, int flags, From nobody Fri Dec 19 20:14:14 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 8FE1851C43 for ; Wed, 10 Apr 2024 22:46:54 +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=1712789216; cv=none; b=W+btllpD4GVwW5PTQwGERzpr73DQzeMl4TU8ULtXF4SOBGwrn0WJKxUwF/sqyQJ0n+ywh+ZJIqAUb8u6k+W5OcCFtRjQBOJ0Xupq+Y6kNhj4ZQM6DzZLCZXru8nVfDaGQGzRdRFIcMKRWRVK/s+VXFk6t5ywMJtiNKTp773PyTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789216; c=relaxed/simple; bh=+8eJhLmLaBhsdGTxPY+WrOPg4LBJu0d/eL03Sd/TxJs=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=oPvtSLQ35YFlG4oRdyULSRjMWBnWYvJSx/GFy/K5SB9tlo5jqhauhsrR0kuw9AitVlx6MNYvG+eIm8sUtq5QdmUalBWMtx3PxFf8qbpV/A7jgihnRCXWY/9uavmfvDHU6ZXjoqtULYynAwG5sLBVJ2ZuBa2l2/bDbkCf1303FCo= 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=NYJjIESs; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=icpzEGWo; 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="NYJjIESs"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="icpzEGWo" Message-ID: <20240410165552.381760138@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789213; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Uf6CyGlUDp6ZuYdKBwWfggnizxjbUvxBehy8Wo6qoQk=; b=NYJjIESs6bBU6Y64vsH3XYdBtc1g0QTb1WETOk95koqefnolJFSJSHX7DdFUB1MiibCaxq bEXy+JqSXsTzF0foDzNo685LrlaIdDNW3Rb63OXU4XhFarRg+gR/36rU/A4G5dIEN8PEDZ l7ZJ15ms9SQ4Mz9UyacYGewC0+VydKpspP7EH8uugH3Ur7B75Aawerf/SwfBVvRaQLPr8J dNujNOtI0JI9Uqd5r5F9HweDfwQOt+l9cQcCuvtXsC4OaqcdtVK00gXSJGl8UpR8xv/eD3 NULDnvEfJGEe9DkfFwALJ8Uu2xHZfuJ8ymkIvN48zu4hjGBmT2Uee1PtHiaC7A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789213; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Uf6CyGlUDp6ZuYdKBwWfggnizxjbUvxBehy8Wo6qoQk=; b=icpzEGWoPoG3Saqqyav5+hYuUTJHZj1lezVlTJRSn3lGoDK2u/+gNCBTclnXbwvkiI4sym OFEb1y+l7QtbtTBQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 23/50] signal: Remove task argument from dequeue_signal() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:52 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The task pointer which is handed to dequeue_signal() is always current. The argument along with the first comment about signalfd in that function is confusing at best. Remove it and use current internally. Update the stale comment for dequeue_signal() while at it. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker Reviewed-by: Oleg Nesterov --- fs/signalfd.c | 4 ++-- include/linux/sched/signal.h | 5 ++--- kernel/signal.c | 23 ++++++++++------------- 3 files changed, 14 insertions(+), 18 deletions(-) --- a/fs/signalfd.c +++ b/fs/signalfd.c @@ -160,7 +160,7 @@ static ssize_t signalfd_dequeue(struct s DECLARE_WAITQUEUE(wait, current); =20 spin_lock_irq(¤t->sighand->siglock); - ret =3D dequeue_signal(current, &ctx->sigmask, info, &type); + ret =3D dequeue_signal(&ctx->sigmask, info, &type); switch (ret) { case 0: if (!nonblock) @@ -175,7 +175,7 @@ static ssize_t signalfd_dequeue(struct s add_wait_queue(¤t->sighand->signalfd_wqh, &wait); for (;;) { set_current_state(TASK_INTERRUPTIBLE); - ret =3D dequeue_signal(current, &ctx->sigmask, info, &type); + ret =3D dequeue_signal(&ctx->sigmask, info, &type); if (ret !=3D 0) break; if (signal_pending(current)) { --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -276,8 +276,7 @@ static inline void signal_set_stop_flags extern void flush_signals(struct task_struct *); extern void ignore_signals(struct task_struct *); extern void flush_signal_handlers(struct task_struct *, int force_default); -extern int dequeue_signal(struct task_struct *task, sigset_t *mask, - kernel_siginfo_t *info, enum pid_type *type); +extern int dequeue_signal(sigset_t *mask, kernel_siginfo_t *info, enum pid= _type *type); =20 static inline int kernel_dequeue_signal(void) { @@ -287,7 +286,7 @@ static inline int kernel_dequeue_signal( int ret; =20 spin_lock_irq(&task->sighand->siglock); - ret =3D dequeue_signal(task, &task->blocked, &__info, &__type); + ret =3D dequeue_signal(&task->blocked, &__info, &__type); spin_unlock_irq(&task->sighand->siglock); =20 return ret; --- a/kernel/signal.c +++ b/kernel/signal.c @@ -618,20 +618,18 @@ static int __dequeue_signal(struct sigpe } =20 /* - * Dequeue a signal and return the element to the caller, which is - * expected to free it. - * - * All callers have to hold the siglock. + * Try to dequeue a signal. If a deliverable signal is found fill in the + * caller provided siginfo and return the signal number. Otherwise return + * 0. */ -int dequeue_signal(struct task_struct *tsk, sigset_t *mask, - kernel_siginfo_t *info, enum pid_type *type) +int dequeue_signal(sigset_t *mask, kernel_siginfo_t *info, enum pid_type *= type) { + struct task_struct *tsk =3D current; bool resched_timer =3D false; int signr; =20 - /* We only dequeue private signals from ourselves, we don't let - * signalfd steal them - */ + lockdep_assert_held(&tsk->sighand->siglock); + *type =3D PIDTYPE_PID; signr =3D __dequeue_signal(&tsk->pending, mask, info, &resched_timer); if (!signr) { @@ -2787,8 +2785,7 @@ bool get_signal(struct ksignal *ksig) type =3D PIDTYPE_PID; signr =3D dequeue_synchronous_signal(&ksig->info); if (!signr) - signr =3D dequeue_signal(current, ¤t->blocked, - &ksig->info, &type); + signr =3D dequeue_signal(¤t->blocked, &ksig->info, &type); =20 if (!signr) break; /* will return 0 */ @@ -3642,7 +3639,7 @@ static int do_sigtimedwait(const sigset_ signotset(&mask); =20 spin_lock_irq(&tsk->sighand->siglock); - sig =3D dequeue_signal(tsk, &mask, info, &type); + sig =3D dequeue_signal(&mask, info, &type); if (!sig && timeout) { /* * None ready, temporarily unblock those we're interested @@ -3661,7 +3658,7 @@ static int do_sigtimedwait(const sigset_ spin_lock_irq(&tsk->sighand->siglock); __set_task_blocked(tsk, &tsk->real_blocked); sigemptyset(&tsk->real_blocked); - sig =3D dequeue_signal(tsk, &mask, info, &type); + sig =3D dequeue_signal(&mask, info, &type); } spin_unlock_irq(&tsk->sighand->siglock); From nobody Fri Dec 19 20:14:14 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 A51F653E3B for ; Wed, 10 Apr 2024 22:46:56 +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=1712789218; cv=none; b=fCyXz7aWeMnK0fSo+ife9P2gn6g9iL3PNaKXGwUR192Tr02/LiKKCM2JmhxHxEiIMfNvQTpwy9Dy3BW4OUODKQNG4AWyI3mWw5ZPvHlLajiqyoWzsPEQleuB1Uno8zwR4mA22ikCgSBGw5hzmLnDTDko1JIvBn+J9/5sYWcSphE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789218; c=relaxed/simple; bh=d1BJ7Kja+UEwMgVwc6Tt6tmvcnKhbXaS3H8LE3K80r4=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=mw0B/8+l1XXjTjbVXgwxJrrtjx2qk6UgN6NnhDklVELySkWQivDKNGeDPkKuW9J8mS8h4qEFY1uWX6De7LeTQX1xYZ7UL3tES2zGpn9egoQfCwANrVZF5Wujw7OV+5uZofZhXr3PG+vUnFRHcrWkCUzFNVVBZJEpzjxbuv5SZuA= 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=BywFEK8E; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0BzM0uy0; 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="BywFEK8E"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0BzM0uy0" Message-ID: <20240410165552.447880671@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789214; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Qgv0rOS1X6FuYj5LENG49jFoHaTII1ldHQt4poVGsnc=; b=BywFEK8EhBKq+GTAmPwdrzCsML4KpjodV0Aa5UV7k+IvmE75liV0APZQ5B8086XCqnu9qy w3oBNIHUbUba2WaMMkZPf/anVzdlQiwNE6Vtbq3+mWTE6ClBoV39rgA0oVMJbUaklfx8oW D3ZY4PyG4L9qqBRviqpFAUnoC/w8bFqXPKn7FqhV2GKFVUCGrKafdYiwBSDnsWnt5DzU/K CjmYcgDJS87JtiTGHjlSzGnDs65BSPuIj90SBPhuMqRCVOD7xzz8i0f3TNiFhwsTqeRwNc rqGIbvWr+k3SL6YKd9eF7rZt4zr2+aqJblrzVMcwzEMA55nJtFt3+dQgwMH4mQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789214; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Qgv0rOS1X6FuYj5LENG49jFoHaTII1ldHQt4poVGsnc=; b=0BzM0uy0YvtHTATQTAAu4NX8a7YREsxX/iXHgXRS+pI3tynfPokyIJNUco+FwxHWiil85d dNLfUXrv1l5TG0AA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 24/50] signal: Replace BUG_ON()s References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:54 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" These really can be handled gracefully without killing the machine. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker Reviewed-by: Oleg Nesterov --- kernel/signal.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1940,10 +1940,11 @@ struct sigqueue *sigqueue_alloc(void) =20 void sigqueue_free(struct sigqueue *q) { - unsigned long flags; spinlock_t *lock =3D ¤t->sighand->siglock; + unsigned long flags; =20 - BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); + if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC))) + return; /* * We must hold ->siglock while testing q->list * to serialize with collect_signal() or with @@ -1971,7 +1972,10 @@ int send_sigqueue(struct sigqueue *q, st unsigned long flags; int ret, result; =20 - BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); + if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC))) + return 0; + if (WARN_ON_ONCE(q->info.si_code !=3D SI_TIMER)) + return 0; =20 ret =3D -1; rcu_read_lock(); @@ -2006,7 +2010,6 @@ int send_sigqueue(struct sigqueue *q, st * If an SI_TIMER entry is already queue just increment * the overrun count. */ - BUG_ON(q->info.si_code !=3D SI_TIMER); q->info.si_overrun++; result =3D TRACE_SIGNAL_ALREADY_PENDING; goto out; From nobody Fri Dec 19 20:14:14 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 7E15654772 for ; Wed, 10 Apr 2024 22:46:58 +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=1712789220; cv=none; b=ZbdeWnSvhEW7QO6ZjtjQ3rwDxbHDIBDWlEV+viqgZs8WUf0lMj6WZzVPvNVr4hUAweTldlY66WffRykIRoao+H5N1xECOld3QnB19VrhbOFJEPKlwpxROHe01TZv6+CbXG1c7xdvi0p1Nzvex/K9CQ9/s8Sfr3+Ie2T67P5G7bA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789220; c=relaxed/simple; bh=TKJn1dWiLSzYuFWlYalQBj85JnAn82gG7J9kjYz+D/0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=iQiPlG7sM7r5yQlvcl+AdQJr2PBrga+ah0pZ/wn2PdZwBeeSgAEKYxAPTKO1ecyYwaioZbvO3sb++AeaZYJVy9sIl58YJJqOcCn9ammynHs6Pu5mTtzXW6aeZ7P4OW0T/1jFlxUzPOw3MbUo+aRJ2OGYZo94MMGnFv8e5/EpIEY= 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=cSHu0Muz; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=6/bhmbRJ; 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="cSHu0Muz"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="6/bhmbRJ" Message-ID: <20240410165552.509700441@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789216; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Zqea4ZCKHXjl0dJ4sa3tZPhdgHz3TXIy35qUKT56zVA=; b=cSHu0MuzZO+1skf/vLAKbmPMJ3mrTqt2Hges/1yKRdZ/RUG0jv5nbJpwWnU8M+lHxQnBvJ MiocgRQItWCYoTRBlnAgBrT8Y5mPPK14LuwmpRQi5G8Pplt+mAN/HG9hkzNcFxDkMgSg4r TjlH/wGSYLPzWL6gQM+tVff8EUK3PMd7my1XdcFaV78EW7GUNslPgIAR8r+VY5OpaXtbdo ExBhUnGN8hNa8LVaGlPJ5EQquL3tPG3WBuhHLzi6ewlYSwznCJFA/FNoJa5StrSPEnOIjb l+kw02VuwwHPLoEM0Y8dGmDqq/kbh4zDQpM/dbWuur/jMfR0gu91HUA3GWjS1Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789216; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Zqea4ZCKHXjl0dJ4sa3tZPhdgHz3TXIy35qUKT56zVA=; b=6/bhmbRJyclGwlsKMNeILN5t6+UAO3qt/n4F/aQWdxoRooyIiEuo//ir+nKWJrLkoOZROp yW+PM1gp5esmmBBQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 25/50] signal: Confine POSIX_TIMERS properly References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:56 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the itimer rearming out of the signal code and consolidate all posix timer related functions in the signal code under one ifdef. Signed-off-by: Thomas Gleixner Reviewed-by: Anna-Maria Behnsen Reviewed-by: Oleg Nesterov --- include/linux/posix-timers.h | 5 + kernel/signal.c | 125 +++++++++++++++-----------------------= ----- kernel/time/itimer.c | 22 +++++++ kernel/time/posix-timers.c | 15 ++++- 4 files changed, 82 insertions(+), 85 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -100,6 +100,8 @@ static inline void posix_cputimers_rt_wa { pct->bases[CPUCLOCK_SCHED].nextevt =3D runtime; } +void posixtimer_rearm_itimer(struct task_struct *p); +void posixtimer_rearm(struct kernel_siginfo *info); =20 /* Init task static initializer */ #define INIT_CPU_TIMERBASE(b) { \ @@ -122,6 +124,8 @@ struct cpu_timer { }; static inline void posix_cputimers_init(struct posix_cputimers *pct) { } static inline void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit) { } +static inline void posixtimer_rearm_itimer(struct task_struct *p) { } +static inline void posixtimer_rearm(struct kernel_siginfo *info) { } #endif =20 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK @@ -196,5 +200,4 @@ void set_process_cpu_timer(struct task_s =20 int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new); =20 -void posixtimer_rearm(struct kernel_siginfo *info); #endif --- a/kernel/signal.c +++ b/kernel/signal.c @@ -478,42 +478,6 @@ void flush_signals(struct task_struct *t } EXPORT_SYMBOL(flush_signals); =20 -#ifdef CONFIG_POSIX_TIMERS -static void __flush_itimer_signals(struct sigpending *pending) -{ - sigset_t signal, retain; - struct sigqueue *q, *n; - - signal =3D pending->signal; - sigemptyset(&retain); - - list_for_each_entry_safe(q, n, &pending->list, list) { - int sig =3D q->info.si_signo; - - if (likely(q->info.si_code !=3D SI_TIMER)) { - sigaddset(&retain, sig); - } else { - sigdelset(&signal, sig); - list_del_init(&q->list); - __sigqueue_free(q); - } - } - - sigorsets(&pending->signal, &signal, &retain); -} - -void flush_itimer_signals(void) -{ - struct task_struct *tsk =3D current; - unsigned long flags; - - spin_lock_irqsave(&tsk->sighand->siglock, flags); - __flush_itimer_signals(&tsk->pending); - __flush_itimer_signals(&tsk->signal->shared_pending); - spin_unlock_irqrestore(&tsk->sighand->siglock, flags); -} -#endif - void ignore_signals(struct task_struct *t) { int i; @@ -636,31 +600,9 @@ int dequeue_signal(sigset_t *mask, kerne *type =3D PIDTYPE_TGID; signr =3D __dequeue_signal(&tsk->signal->shared_pending, mask, info, &resched_timer); -#ifdef CONFIG_POSIX_TIMERS - /* - * itimer signal ? - * - * itimers are process shared and we restart periodic - * itimers in the signal delivery path to prevent DoS - * attacks in the high resolution timer case. This is - * compliant with the old way of self-restarting - * itimers, as the SIGALRM is a legacy signal and only - * queued once. Changing the restart behaviour to - * restart the timer in the signal dequeue path is - * reducing the timer noise on heavy loaded !highres - * systems too. - */ - if (unlikely(signr =3D=3D SIGALRM)) { - struct hrtimer *tmr =3D &tsk->signal->real_timer; =20 - if (!hrtimer_is_queued(tmr) && - tsk->signal->it_real_incr !=3D 0) { - hrtimer_forward(tmr, tmr->base->get_time(), - tsk->signal->it_real_incr); - hrtimer_restart(tmr); - } - } -#endif + if (unlikely(signr =3D=3D SIGALRM)) + posixtimer_rearm_itimer(tsk); } =20 recalc_sigpending(); @@ -682,22 +624,12 @@ int dequeue_signal(sigset_t *mask, kerne */ current->jobctl |=3D JOBCTL_STOP_DEQUEUED; } -#ifdef CONFIG_POSIX_TIMERS - if (resched_timer) { - /* - * Release the siglock to ensure proper locking order - * of timer locks outside of siglocks. Note, we leave - * irqs disabled here, since the posix-timers code is - * about to disable them again anyway. - */ - spin_unlock(&tsk->sighand->siglock); - posixtimer_rearm(info); - spin_lock(&tsk->sighand->siglock); =20 - /* Don't expose the si_sys_private value to userspace */ - info->si_sys_private =3D 0; + if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { + if (unlikely(resched_timer)) + posixtimer_rearm(info); } -#endif + return signr; } EXPORT_SYMBOL_GPL(dequeue_signal); @@ -1924,15 +1856,45 @@ int kill_pid(struct pid *pid, int sig, i } EXPORT_SYMBOL(kill_pid); =20 +#ifdef CONFIG_POSIX_TIMERS /* - * These functions support sending signals using preallocated sigqueue - * structures. This is needed "because realtime applications cannot - * afford to lose notifications of asynchronous events, like timer - * expirations or I/O completions". In the case of POSIX Timers - * we allocate the sigqueue structure from the timer_create. If this - * allocation fails we are able to report the failure to the application - * with an EAGAIN error. + * These functions handle POSIX timer signals. POSIX timers use + * preallocated sigqueue structs for sending signals. */ +static void __flush_itimer_signals(struct sigpending *pending) +{ + sigset_t signal, retain; + struct sigqueue *q, *n; + + signal =3D pending->signal; + sigemptyset(&retain); + + list_for_each_entry_safe(q, n, &pending->list, list) { + int sig =3D q->info.si_signo; + + if (likely(q->info.si_code !=3D SI_TIMER)) { + sigaddset(&retain, sig); + } else { + sigdelset(&signal, sig); + list_del_init(&q->list); + __sigqueue_free(q); + } + } + + sigorsets(&pending->signal, &signal, &retain); +} + +void flush_itimer_signals(void) +{ + struct task_struct *tsk =3D current; + unsigned long flags; + + spin_lock_irqsave(&tsk->sighand->siglock, flags); + __flush_itimer_signals(&tsk->pending); + __flush_itimer_signals(&tsk->signal->shared_pending); + spin_unlock_irqrestore(&tsk->sighand->siglock, flags); +} + struct sigqueue *sigqueue_alloc(void) { return __sigqueue_alloc(-1, current, GFP_KERNEL, 0, SIGQUEUE_PREALLOC); @@ -2029,6 +1991,7 @@ int send_sigqueue(struct sigqueue *q, st rcu_read_unlock(); return ret; } +#endif /* CONFIG_POSIX_TIMERS */ =20 void do_notify_pidfd(struct task_struct *task) { --- a/kernel/time/itimer.c +++ b/kernel/time/itimer.c @@ -151,7 +151,27 @@ COMPAT_SYSCALL_DEFINE2(getitimer, int, w #endif =20 /* - * The timer is automagically restarted, when interval !=3D 0 + * Invoked from dequeue_signal() when SIG_ALRM is delivered. + * + * Restart the ITIMER_REAL timer if it is armed as periodic timer. Doing + * this in the signal delivery path instead of self rearming prevents a DoS + * with small increments in the high reolution timer case and reduces timer + * noise in general. + */ +void posixtimer_rearm_itimer(struct task_struct *tsk) +{ + struct hrtimer *tmr =3D &tsk->signal->real_timer; + + if (!hrtimer_is_queued(tmr) && tsk->signal->it_real_incr !=3D 0) { + hrtimer_forward(tmr, tmr->base->get_time(), + tsk->signal->it_real_incr); + hrtimer_restart(tmr); + } +} + +/* + * Interval timers are restarted in the signal delivery path. See + * posixtimer_rearm_itimer(). */ enum hrtimer_restart it_real_fn(struct hrtimer *timer) { --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -251,7 +251,7 @@ static void common_hrtimer_rearm(struct =20 /* * This function is called from the signal delivery code if - * info->si_sys_private is not zero, which indicates that the timer has to + * info::si_sys_private is not zero, which indicates that the timer has to * be rearmed. Restart the timer and update info::si_overrun. */ void posixtimer_rearm(struct kernel_siginfo *info) @@ -259,9 +259,15 @@ void posixtimer_rearm(struct kernel_sigi struct k_itimer *timr; unsigned long flags; =20 + /* + * Release siglock to ensure proper locking order versus + * timr::it_lock. Keep interrupts disabled. + */ + spin_unlock(¤t->sighand->siglock); + timr =3D lock_timer(info->si_tid, &flags); if (!timr) - return; + goto out; =20 if (timr->it_interval && timr->it_requeue_pending =3D=3D info->si_sys_pri= vate) { timr->kclock->timer_rearm(timr); @@ -275,6 +281,11 @@ void posixtimer_rearm(struct kernel_sigi } =20 unlock_timer(timr, flags); +out: + spin_lock(¤t->sighand->siglock); + + /* Don't expose the si_sys_private value to userspace */ + info->si_sys_private =3D 0; } =20 int posix_timer_queue_signal(struct k_itimer *timr) From nobody Fri Dec 19 20:14:14 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 1A87654F91 for ; Wed, 10 Apr 2024 22:46:59 +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=1712789221; cv=none; b=sX/m7inHdMmGH3K6bceYpIL5yXf0/nO7Bcc1KI961gfkSyLMc18rGxgG4o5y23lyMLxMp8e1PYdVmyPieY3N1e+pXEl1qiuRm4HrW5ZxLYaOaYq31vS8IFLLOyo8VM3AJVs2bnz8F2VWp0/KCTqMSnXQd9NozNnb/sV2tfthZVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789221; c=relaxed/simple; bh=ipfz+AaYDg2jRk2QzxjlJP24YxnDWUAysNYhb2QjuYU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Ictxthlcx/mIf9UnRdAVhABgsC59MgCIVKrxBu1nlqXEhIl5WcpXd5okF7tXlWyifEPx1/K/Pojk2ar+UAlwAqiH13Q7qq0vlGC+ooiYY9ONNeFUf+L/ZNAbNRszGxpd2AJfnCPisC7/BS69ZtQFiydoB1YD7lEIZcW7yt//GrI= 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=mPDwHGzM; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cbDIKNug; 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="mPDwHGzM"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cbDIKNug" Message-ID: <20240410165552.572304080@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789218; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=t7pjsSQKBc6tEHZWynKUhlTqjE2Ct45jVqlBq7pgRI0=; b=mPDwHGzMsYi9guyziH1nqXc1/wh9AIulGLHFee23j+SNPuBzYg17wR2FD3n2ytABEFRe5V 7zzTKUgFXVZwjBnMm5VYbofTEf8mrJ3mW1x1hndkMX3QhRAE6QUXoIfz9zH1obhBN21VCT +tXZGzOJ9G2lNMsgp3uOIk/jxysM/vsKR7IZoydMfjzaY7FAJ0/biRpjLTcP1id51HhBzn g20pxjgMUnTce3UhlaBWSmsVTQ+OkbqmJ0JK7j1vTBNjte8uOPOD1PtBHcxkm2pJykX/nc yk1LJlOQvrIaVYpRBc5xW2ymV3/YQcD5Bz7VDa7LcJM+oJFOpal8hCbta/zz5Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789218; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=t7pjsSQKBc6tEHZWynKUhlTqjE2Ct45jVqlBq7pgRI0=; b=cbDIKNugg358Psea1JVSW1YSq4zWt2pclLbdfx6o180qJhFcPTu1xY5dzI6MFHhxiyLJ9i 8SqzYdSfVCSzSLCg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 26/50] signal: Get rid of resched_timer logic References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:57 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There is no reason for handing the *resched pointer argument through several functions just to check whether the signal is related to a self rearming posix timer. SI_TIMER is only used by the posix timer code and cannot be queued from user space. The only extra check in collect_signal() to verify whether the queued signal is preallocated is not really useful. Some other places already check purely the SI_TIMER type. Signed-off-by: Thomas Gleixner --- kernel/signal.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -526,8 +526,7 @@ bool unhandled_signal(struct task_struct return !tsk->ptrace; } =20 -static void collect_signal(int sig, struct sigpending *list, kernel_siginf= o_t *info, - bool *resched_timer) +static void collect_signal(int sig, struct sigpending *list, kernel_siginf= o_t *info) { struct sigqueue *q, *first =3D NULL; =20 @@ -549,12 +548,6 @@ static void collect_signal(int sig, stru still_pending: list_del_init(&first->list); copy_siginfo(info, &first->info); - - *resched_timer =3D - (first->flags & SIGQUEUE_PREALLOC) && - (info->si_code =3D=3D SI_TIMER) && - (info->si_sys_private); - __sigqueue_free(first); } else { /* @@ -571,13 +564,12 @@ static void collect_signal(int sig, stru } } =20 -static int __dequeue_signal(struct sigpending *pending, sigset_t *mask, - kernel_siginfo_t *info, bool *resched_timer) +static int __dequeue_signal(struct sigpending *pending, sigset_t *mask, ke= rnel_siginfo_t *info) { int sig =3D next_signal(pending, mask); =20 if (sig) - collect_signal(sig, pending, info, resched_timer); + collect_signal(sig, pending, info); return sig; } =20 @@ -589,17 +581,15 @@ static int __dequeue_signal(struct sigpe int dequeue_signal(sigset_t *mask, kernel_siginfo_t *info, enum pid_type *= type) { struct task_struct *tsk =3D current; - bool resched_timer =3D false; int signr; =20 lockdep_assert_held(&tsk->sighand->siglock); =20 *type =3D PIDTYPE_PID; - signr =3D __dequeue_signal(&tsk->pending, mask, info, &resched_timer); + signr =3D __dequeue_signal(&tsk->pending, mask, info); if (!signr) { *type =3D PIDTYPE_TGID; - signr =3D __dequeue_signal(&tsk->signal->shared_pending, - mask, info, &resched_timer); + signr =3D __dequeue_signal(&tsk->signal->shared_pending, mask, info); =20 if (unlikely(signr =3D=3D SIGALRM)) posixtimer_rearm_itimer(tsk); @@ -626,7 +616,7 @@ int dequeue_signal(sigset_t *mask, kerne } =20 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { - if (unlikely(resched_timer)) + if (unlikely(info->si_code =3D=3D SI_TIMER && info->si_sys_private)) posixtimer_rearm(info); } =20 @@ -1011,6 +1001,9 @@ static int __send_signal_locked(int sig, =20 lockdep_assert_held(&t->sighand->siglock); =20 + if (WARN_ON_ONCE(!is_si_special(info) && info->si_code =3D=3D SI_TIMER)) + return 0; + result =3D TRACE_SIGNAL_IGNORED; if (!prepare_signal(sig, t, force)) goto ret; From nobody Fri Dec 19 20:14:14 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 DC50555C1A for ; Wed, 10 Apr 2024 22:47:01 +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=1712789223; cv=none; b=WDc74spEFD+5cEn1d+o/f3XriMlLzwljxHBfPCTcQjHloH90q0n+i4hcSV6FB8lA9SdJXEOhbzWfyVdFT2R0GIS+z4htfkUciRGg6tqCRQy+ehU0MKPzQNIgpb+cm6/1HElBqo9RDgT5I+GSkfYU5RVlJfxzt1gPuo74Ut1MGik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789223; c=relaxed/simple; bh=YUYXbvNLeAAZW3gzVGbt6Jlqa6UfoUuXxN1tUXO46bM=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=htu4aKcr3AQd8/CW57twRaTvHX1MVFNIpmtTMA+sC6IJNZ+vke8+/aQL3CaHzYNENa5GzFDfzAQH65wN3rckmRLO18dmroUmIhMR8wurQ3mPc67c9v68aRhHbAlRlAiq2maQORXttdS56cDHxpwORKw6/ZtJhUa+U4ZLU4gIgsY= 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=MxY/71Ws; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=KpccPDeE; 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="MxY/71Ws"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="KpccPDeE" Message-ID: <20240410165552.633773866@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789220; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=WkRZaZxYlvrm5s7eSkUnSudnYssu5hu4hAXagPKaAJw=; b=MxY/71Wsh0Jh4+Amnr/H/hy2OvhG7xkSPUB0bVzu1aVYcn6upCmiE4YjQ8GJ3+yDZDhjyu dx5Tv+q+tz0ErIQjnK1IqFSMPQ1WA7m8r35jBvSk6dNvqUpOApLAO1gI9v4zL8mahWhbxE T+64wN7uHlEaaTNqPGTzvye6+stvuE1v7N0nKlw2sPHOr1cu3sHHZH4yambIT3ePvUJsqn Lc6/9h/JmmLm8nfLGbciW/EmubO3QII+hWlBLl83BIHchq9l2grFjg6dETnR89JknnrIPl F2BDSucxddGq6+KR4wYmiNG/9uQ7bUwPqSZokhRiGoT7p+5NkZzSC/r/gm20aw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789220; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=WkRZaZxYlvrm5s7eSkUnSudnYssu5hu4hAXagPKaAJw=; b=KpccPDeE+wwk9dDhrW1VJb86aaO0ndsNOVDcVIM6jyKFODiG7T1EpQirrgWBW5Q4a2M9d7 +Q73MJkSbMtlQSCw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 27/50] posix-timers: Cure si_sys_private race References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:46:59 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The si_sys_private member of the siginfo which is embedded in the preallocated sigqueue is used by the posix timer code to decide whether a timer must be reprogrammed on signal delivery. The handling of this is racy as a long standing comment in that code documents. It is modified with the timer lock held, but without sighand lock being held. The actual signal delivery code checks for it under sighand lock without holding the timer lock. Hand the new value to send_sigqueue() as argument and store it with sighand lock held. Signed-off-by: Thomas Gleixner --- include/linux/sched/signal.h | 2 +- kernel/signal.c | 10 +++++++++- kernel/time/posix-timers.c | 15 +-------------- 3 files changed, 11 insertions(+), 16 deletions(-) --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -339,7 +339,7 @@ extern int send_sig(int, struct task_str extern int zap_other_threads(struct task_struct *p); extern struct sigqueue *sigqueue_alloc(void); extern void sigqueue_free(struct sigqueue *); -extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type); +extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type, i= nt si_private); extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *); =20 static inline void clear_notify_signal(void) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1919,7 +1919,7 @@ void sigqueue_free(struct sigqueue *q) __sigqueue_free(q); } =20 -int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type) +int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type,= int si_private) { int sig =3D q->info.si_signo; struct sigpending *pending; @@ -1954,6 +1954,14 @@ int send_sigqueue(struct sigqueue *q, st if (!likely(lock_task_sighand(t, &flags))) goto ret; =20 + /* + * Update @q::info::si_sys_private for posix timer signals with + * sighand locked to prevent a race against dequeue_signal() which + * decides based on si_sys_private whether to invoke + * posixtimer_rearm() or not. + */ + q->info.si_sys_private =3D si_private; + ret =3D 1; /* the signal is ignored */ result =3D TRACE_SIGNAL_IGNORED; if (!prepare_signal(sig, t, false)) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -299,21 +299,8 @@ int posix_timer_queue_signal(struct k_it if (timr->it_interval) si_private =3D ++timr->it_requeue_pending; =20 - /* - * FIXME: if ->sigq is queued we can race with - * dequeue_signal()->posixtimer_rearm(). - * - * If dequeue_signal() sees the "right" value of - * si_sys_private it calls posixtimer_rearm(). - * We re-queue ->sigq and drop ->it_lock(). - * posixtimer_rearm() locks the timer - * and re-schedules it while ->sigq is pending. - * Not really bad, but not that we want. - */ - timr->sigq->info.si_sys_private =3D si_private; - type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; - ret =3D send_sigqueue(timr->sigq, timr->it_pid, type); + ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, si_private); /* If we failed to send the signal the timer stops. */ return ret > 0; } From nobody Fri Dec 19 20:14:14 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 3C68C56448 for ; Wed, 10 Apr 2024 22:47:03 +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=1712789224; cv=none; b=tNhEg/1weEzjeb7z0HKNfyks28QkKrNQ6H+/FnU90cEeooFpld1j1TTF34hAibBfZRoTb5AJXpsP+IsOBwfjNLIUzqgazdtau2v1L7Fo8cnbgr7TvzPBuRnNTP92IFVqytz6W4pnE1A+OR9+nrOwNet8PtQ6bzvVVo1MQ13pZL0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789224; c=relaxed/simple; bh=YH2GNJxCNBMbmtwU0fr39UpQz8wyT6gB/kPu6Cst2a0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=tS7ndSpf9N0ztTc+ZXzX5azUoDFqU2gNzjOwOMRwog9d5RlNCWqlLhIWWsJSiMXv1uZhGSV1TrsO8sZOEWcrs7S+Q7m4A0XaaCwzKyfkV9XdML7UE+HPaBLv/64aY8QuSTbduMPkiJwZ9yNaTYZZX6EK6VDWYhSBg84+tS/3LS8= 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=CfjuLqyF; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=UWn/b8x5; 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="CfjuLqyF"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="UWn/b8x5" Message-ID: <20240410165552.697213341@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789221; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=7CK+FAEXH0tvW3CxjLUKVaxfftLEf1lOjkxUKErNSnE=; b=CfjuLqyFZC+9G/0y3VW1+Tuaz6Hw3HwQNTnj/doY2nvvH5gIQ7JT9KhnOqGsZJ11kxkN42 h8762CtIf2YnysV/4+EUlJcahi77wpGCMHC70BglasJ+pcDkRD/JjD7yKFrD2iHZ5bkHeV Kch01mVb4qoqtbpipRc1b7NZpCYL/W3U/DJDkDgfGBSa5kFXaB4LWsxyy/m34Ojd9d8+7t ViF6P08BA2KXFZAopUk/Ch8uKP1dyAhX4NeBPQsHS1ECp59NdMpaBdAFaAKthGAsYo66dG DpaVoeM/RyU7a4zGym42u/W9pVOr1b/fewgh2R6GxKLRlOA/+4DSVRevp2fxvw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789221; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=7CK+FAEXH0tvW3CxjLUKVaxfftLEf1lOjkxUKErNSnE=; b=UWn/b8x5KNguxyhReCU0noKBkvAay0Y9758nFeM5K38fgXYetzIHkj2siO4kAZzg3lFlfJ Uf5lpG5kk3BMukBQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 28/50] signal: Allow POSIX timer signals to be dropped References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:01 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In case that a timer was reprogrammed or deleted an already pending signal is obsolete. Right now such signals are kept around and eventually delivered. While POSIX is blury about this: - "The effect of disarming or resetting a timer with pending expiration notifications is unspecified." - "The disposition of pending signals for the deleted timer is unspecified." it is reasonable in both cases to expect that pending signals are discarded as they have no meaning anymore. Prepare the signal code to allow dropping posix timer signals. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 5 +++-- kernel/signal.c | 7 +++++-- kernel/time/posix-timers.c | 3 ++- 3 files changed, 10 insertions(+), 5 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -100,8 +100,9 @@ static inline void posix_cputimers_rt_wa { pct->bases[CPUCLOCK_SCHED].nextevt =3D runtime; } + void posixtimer_rearm_itimer(struct task_struct *p); -void posixtimer_rearm(struct kernel_siginfo *info); +bool posixtimer_deliver_signal(struct kernel_siginfo *info); =20 /* Init task static initializer */ #define INIT_CPU_TIMERBASE(b) { \ @@ -125,7 +126,7 @@ static inline void posix_cputimers_init( static inline void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit) { } static inline void posixtimer_rearm_itimer(struct task_struct *p) { } -static inline void posixtimer_rearm(struct kernel_siginfo *info) { } +static inline bool posixtimer_deliver_signal(struct kernel_siginfo *info) = { return false; } #endif =20 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK --- a/kernel/signal.c +++ b/kernel/signal.c @@ -585,6 +585,7 @@ int dequeue_signal(sigset_t *mask, kerne =20 lockdep_assert_held(&tsk->sighand->siglock); =20 +again: *type =3D PIDTYPE_PID; signr =3D __dequeue_signal(&tsk->pending, mask, info); if (!signr) { @@ -616,8 +617,10 @@ int dequeue_signal(sigset_t *mask, kerne } =20 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { - if (unlikely(info->si_code =3D=3D SI_TIMER && info->si_sys_private)) - posixtimer_rearm(info); + if (unlikely(info->si_code =3D=3D SI_TIMER && info->si_sys_private)) { + if (!posixtimer_deliver_signal(info)) + goto again; + } } =20 return signr; --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -254,7 +254,7 @@ static void common_hrtimer_rearm(struct * info::si_sys_private is not zero, which indicates that the timer has to * be rearmed. Restart the timer and update info::si_overrun. */ -void posixtimer_rearm(struct kernel_siginfo *info) +bool posixtimer_deliver_signal(struct kernel_siginfo *info) { struct k_itimer *timr; unsigned long flags; @@ -286,6 +286,7 @@ void posixtimer_rearm(struct kernel_sigi =20 /* Don't expose the si_sys_private value to userspace */ info->si_sys_private =3D 0; + return true; } =20 int posix_timer_queue_signal(struct k_itimer *timr) From nobody Fri Dec 19 20:14:14 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 8EF6057301 for ; Wed, 10 Apr 2024 22:47:05 +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=1712789227; cv=none; b=i5zttYu9p3a6bdfsSckoHFnwXCIxUT9Jg+ZWofv6bD1GkMqGb+MPgib54oe/yimrBa2nLDF99thJgUSokNqu/BSnI4JQGjhZkTLPFcxSPpcbkkqU+miIG+4QxlXMyN90Zh5+7hgPiWU1KIiIMQymhr6diP4DgF1GgRnSEv5TXpk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789227; c=relaxed/simple; bh=5k+o9FSXZnXGzFOAjEfjafxS6KdlxmAI2+L7+gmWoPY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=WyUHX1PnXfCMjtO5C7szpB9i9zTwGeZ4CViIRkg9gDftcg7gdFRcS5JXKx4mPCDWTmM5knFnYhbLLeCp7i/cOA7w1lsv/HaKPUp8lm5LLFnnwy4pYUWUXInilGkY443lo+H8QjC8D5TzslCT5ZuzULRGbwAkXDwhtbe6c38BuNk= 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=EPSlGxp8; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=zb94DRPm; 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="EPSlGxp8"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="zb94DRPm" Message-ID: <20240410165552.772386179@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789223; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=PVNa4KLijTQ7CcOYu3CDLgD0Q8pt6GmBRvk3JFfaO1Y=; b=EPSlGxp8JXih8Om6h4+9QdMXVPd8lx7psYKe5X7Gu8B0B3ImRetobqK89Hommw1hGBIczx wWJYhjwCG+BJSP+LNlODvhYswd4fzEsPb6HJUPE1yOtlTpKjx80w3p7ubFUyE/abtH2Xpw 4x4xZbpLdAY4rJPvjrZur/7dM0PB3Kvl5SJatuJ2YStmHVO8mE61HVOhdmm8b7MD7SHj+x +GQFtD+QREoUvpH+5xKbKbdOjz2pTwOwpKt6fISUtyM1PM8h425XkK46GzEI5jUxxRiDbG dIqIumyUmkndjun0XA7OxJzxfgK8vh9/sQ4oJINf22CvuEe/GD+f9Qa+GJw0Hg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789223; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=PVNa4KLijTQ7CcOYu3CDLgD0Q8pt6GmBRvk3JFfaO1Y=; b=zb94DRPmudpBTbZH4Dd+LKY50DUrgeyku8OyhgZ7LMUeHbT7W5RzyggyT/keTQ/LEjTG1R sXuecsE6cnDPfSDw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 29/50] posix-timers: Drop signal if timer has been deleted or reprogrammed References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:03 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" No point in delivering a signal from the past. POSIX does not specify the behaviour here: - "The effect of disarming or resetting a timer with pending expiration notifications is unspecified." - "The disposition of pending signals for the deleted timer is unspecified= ." In both cases it is reasonable to expect that pending signals are discarded. Especially in the reprogramming case it does not make sense to account for previous overruns or to deliver a signal for a timer which has been disarmed. Drop the signal as that is conistent and understandable behaviour. Signed-off-by: Thomas Gleixner --- kernel/time/posix-timers.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -250,14 +250,14 @@ static void common_hrtimer_rearm(struct } =20 /* - * This function is called from the signal delivery code if - * info::si_sys_private is not zero, which indicates that the timer has to - * be rearmed. Restart the timer and update info::si_overrun. + * This function is called from the signal delivery code. It decides + * whether the signal should be dropped and rearms interval timers. */ bool posixtimer_deliver_signal(struct kernel_siginfo *info) { struct k_itimer *timr; unsigned long flags; + bool ret =3D false; =20 /* * Release siglock to ensure proper locking order versus @@ -279,6 +279,7 @@ bool posixtimer_deliver_signal(struct ke =20 info->si_overrun =3D timer_overrun_to_int(timr, info->si_overrun); } + ret =3D true; =20 unlock_timer(timr, flags); out: @@ -286,7 +287,7 @@ bool posixtimer_deliver_signal(struct ke =20 /* Don't expose the si_sys_private value to userspace */ info->si_sys_private =3D 0; - return true; + return ret; } =20 int posix_timer_queue_signal(struct k_itimer *timr) From nobody Fri Dec 19 20:14:14 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 2F14558AC8 for ; Wed, 10 Apr 2024 22:47:06 +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=1712789228; cv=none; b=daR3GCfmxzb8aV5gYdqgwwMrtGkBIm/ZBWBINy7cFKiwpZWH8/bMNnIQsZxfTQDXy4MOUYnzSbSbr244+hUCPpcu9xVobGLeBNgLfizjS8V3kov8MFPgCjD96zDXY7SWNJkubI+2swYOmwRFeUqpaPV8vimsbrdJ0P3q8PCaqfA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789228; c=relaxed/simple; bh=QRAMzsqKtG3Z4cwj9EcqM7O3sI1tPeIJIx+RIdoHDh8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=rIJmemYv+7pLTbq/OkxLWXwpeoVY1VPHJ+TPY6m0njVR0EHcc49IPVhsHF3kf8txsTS0T8tTNhIFhijYvRsgGSjLi9J2TW0BYWKvuRvZ/ORzkhUwyfw1dHMuQZip7cksQWs7nZP57MzHUaqYAcuS9X0lEbLtKybGGSEqHWZLpJU= 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=BTAWkgCF; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=aTtKDutb; 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="BTAWkgCF"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="aTtKDutb" Message-ID: <20240410165552.833418453@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789225; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=4ZXhSygM4HMfUyJQFe7w4Q0s7qUUsNDQCaN6Cvt7YM4=; b=BTAWkgCFj9d/ovgcd4dXuneCf6Ow9rdolX3dRYjuix16CWMvnq841yxqMEb8OfNCBx3u/L k3n4fWFzCUqiwAl7Pg1uTjiD3Ea1DovSsxRR58H2zuFNfePNcxS/fDUfZzL03hLjfs9jy+ ZUz4Bciain+7+VZvJWdB7es9cMrHUxZ6u8eapMNk1/cq99g0D2Uw/1ytwI/wvfCd4/8VVL zHwgtxGOmZmMhNxBhVV7z4DJQBpepqnBgTtATKvOEyFsBIAnw0EY01GNw+GlNikLraUkgp gGfAc9bml1C0n7VbXkVyfTm8T9A4sluFDAJl/h/RIqG/lW2+1O7PWDYMaUeXWA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789225; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=4ZXhSygM4HMfUyJQFe7w4Q0s7qUUsNDQCaN6Cvt7YM4=; b=aTtKDutbuGIxyE6hfQiTfKUzgDog/q/KWcVArYbSA9knIlhAxXA+5h9E4+e01g1A52mh/6 8VTdWc4k1+QJW4DA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 30/50] posix-timers: Rename k_itimer::it_requeue_pending References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:04 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Prepare for using this struct member to do a proper reprogramming and deletion accounting so that stale signals can be dropped. No functional change. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 5 ++--- kernel/time/alarmtimer.c | 2 +- kernel/time/posix-cpu-timers.c | 4 ++-- kernel/time/posix-timers.c | 12 ++++++------ 4 files changed, 11 insertions(+), 12 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -150,8 +150,7 @@ static inline void posix_cputimers_init_ * @it_active: Marker that timer is active * @it_overrun: The overrun counter for pending signals * @it_overrun_last: The overrun at the time of the last delivered signal - * @it_requeue_pending: Indicator that timer waits for being requeued on - * signal delivery + * @it_signal_seq: Sequence count to control signal delivery * @it_sigev_notify: The notify word of sigevent struct for signal delivery * @it_interval: The interval for periodic timers * @it_signal: Pointer to the creators signal struct @@ -172,7 +171,7 @@ struct k_itimer { int it_active; s64 it_overrun; s64 it_overrun_last; - int it_requeue_pending; + unsigned int it_signal_seq; int it_sigev_notify; ktime_t it_interval; struct signal_struct *it_signal; --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -584,7 +584,7 @@ static enum alarmtimer_restart alarm_han * small intervals cannot starve the system. */ ptr->it_overrun +=3D __alarm_forward_now(alarm, ptr->it_interval, true); - ++ptr->it_requeue_pending; + ++ptr->it_signal_seq; ptr->it_active =3D 1; result =3D ALARMTIMER_RESTART; } --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -608,7 +608,7 @@ static void cpu_timer_fire(struct k_itim * ticking in case the signal is deliverable next time. */ posix_cpu_timer_rearm(timer); - ++timer->it_requeue_pending; + ++timer->it_signal_seq; } } =20 @@ -745,7 +745,7 @@ static void __posix_cpu_timer_get(struct * - Timers which expired, but the signal has not yet been * delivered */ - if (iv && ((timer->it_requeue_pending & REQUEUE_PENDING) || sigev_none)) + if (iv && ((timer->it_signal_seq & REQUEUE_PENDING) || sigev_none)) expires =3D bump_cpu_timer(timer, now); else expires =3D cpu_timer_getexpires(&timer->it.cpu); --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -269,13 +269,13 @@ bool posixtimer_deliver_signal(struct ke if (!timr) goto out; =20 - if (timr->it_interval && timr->it_requeue_pending =3D=3D info->si_sys_pri= vate) { + if (timr->it_interval && timr->it_signal_seq =3D=3D info->si_sys_private)= { timr->kclock->timer_rearm(timr); =20 timr->it_active =3D 1; timr->it_overrun_last =3D timr->it_overrun; timr->it_overrun =3D -1LL; - ++timr->it_requeue_pending; + ++timr->it_signal_seq; =20 info->si_overrun =3D timer_overrun_to_int(timr, info->si_overrun); } @@ -299,7 +299,7 @@ int posix_timer_queue_signal(struct k_it =20 timr->it_active =3D 0; if (timr->it_interval) - si_private =3D ++timr->it_requeue_pending; + si_private =3D ++timr->it_signal_seq; =20 type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, si_private); @@ -366,7 +366,7 @@ static enum hrtimer_restart posix_timer_ =20 timr->it_overrun +=3D hrtimer_forward(timer, now, timr->it_interval); ret =3D HRTIMER_RESTART; - ++timr->it_requeue_pending; + ++timr->it_signal_seq; timr->it_active =3D 1; } } @@ -667,7 +667,7 @@ void common_timer_get(struct k_itimer *t * is a SIGEV_NONE timer move the expiry time forward by intervals, * so expiry is > now. */ - if (iv && (timr->it_requeue_pending & REQUEUE_PENDING || sig_none)) + if (iv && (timr->it_signal_seq & REQUEUE_PENDING || sig_none)) timr->it_overrun +=3D kc->timer_forward(timr, now); =20 remaining =3D kc->timer_remaining(timr, now); @@ -868,7 +868,7 @@ void posix_timer_set_common(struct k_iti timer->it_interval =3D 0; =20 /* Prevent reloading in case there is a signal pending */ - timer->it_requeue_pending =3D (timer->it_requeue_pending + 2) & ~REQUEUE_= PENDING; + timer->it_signal_seq =3D (timer->it_signal_seq + 2) & ~REQUEUE_PENDING; /* Reset overrun accounting */ timer->it_overrun_last =3D 0; timer->it_overrun =3D -1LL; From nobody Fri Dec 19 20:14:14 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 C5E913F9D8 for ; Wed, 10 Apr 2024 22:47:08 +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=1712789230; cv=none; b=EgTld/xCqfSEdJeYH7LjdZxNYkW5qDj8e9uH9hl/MhA0yYX5Ry6qHfS+2DZCPPU4nBF1m3B9TbvdI32haig2pHXV+e6lHV+4J4dN4z84zNMvq+u8/Z4WzMOPGQ5LhurxUOmli59HkbbiPfJzxWcAQaa7fbAg4OILYgn6beDHanM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789230; c=relaxed/simple; bh=FlJJagnR9HUnZRLrJ3Canm8+IlguEM9dtcq4WQlXOec=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=pCUyZHqym/U6ACTX2zlM3ggTWKv7UMTU2UPGw4XBWl8ENwRSkFL9Qswr6BWvjRLpxmSpL/B0qg2e8GQcF3lxtHSwag2S/vUlCRLf6sMZWAZxOTxxxX7FMb8Qx/+/bw1Kc8hSJQtIy1tmYwPbCDRFnEtH5ezdEKAgjwpJZsJ2Rwc= 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=e6M7Aftp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jw13IChK; 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="e6M7Aftp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jw13IChK" Message-ID: <20240410165552.895516106@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789227; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=pDW2UUZDnP/mksmKSp58tEw1w9QqpWE3E22mpO9GuFY=; b=e6M7AftpQ0urAko2VwlWDIAiusTSFI6tvLhkXGkOoLUkQrNj4EL1CGF3lUs5sOPKbARhZQ bNtad1Z7zS10vLJRsU7kkDvqPnh3QnhUy5WpZWIjwk6fHMJWai2v82s1IlJJL4jRGf24Fg TsjFlIuJy7infhHu+QNc2N6GIbktKBK13JgUYDccROxNSHOKpKaVXScqT19xk5dWM8FdBR s8lWyLHy1MwiNhRB2iM5xmu9sJSOzFJGERtQHQLlCIZ14FSAXfNkljIvN7rZ9TZ+WLWVe0 Jh3YUbyQrdoCNDGt81byOxSpbYyQnG1YHkSHl7/47vAyAgIMQNfiUNG1PtsJQA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789227; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=pDW2UUZDnP/mksmKSp58tEw1w9QqpWE3E22mpO9GuFY=; b=jw13IChKRaZMqJY/aLk4WKPRXMFxstNkqAdNLG4bn7sC+M4CwkKHTj6bNiaKdUukyRL/B3 pGUtxRk6XpR2+SAQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 31/50] posix-timers: Add proper state tracking References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:06 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Right now the state tracking is done by two struct members: - it_active: A boolean which tracks armed/disarmed state - it_signal_seq: A sequence counter which is used to invalidate settings and prevent rearming Replace it_active with it_status and keep properly track about the states in one place. This allows to reuse it_signal_seq to track reprogramming, disarm and delete operations in order to drop signals which are related to the state previous of those operations. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 4 ++-- kernel/time/alarmtimer.c | 2 +- kernel/time/posix-cpu-timers.c | 15 ++++++++------- kernel/time/posix-timers.c | 22 +++++++++++++--------- kernel/time/posix-timers.h | 6 ++++++ 5 files changed, 30 insertions(+), 19 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -147,7 +147,7 @@ static inline void posix_cputimers_init_ * @kclock: Pointer to the k_clock struct handling this timer * @it_clock: The posix timer clock id * @it_id: The posix timer id for identifying the timer - * @it_active: Marker that timer is active + * @it_status: The status of the timer * @it_overrun: The overrun counter for pending signals * @it_overrun_last: The overrun at the time of the last delivered signal * @it_signal_seq: Sequence count to control signal delivery @@ -168,7 +168,7 @@ struct k_itimer { const struct k_clock *kclock; clockid_t it_clock; timer_t it_id; - int it_active; + int it_status; s64 it_overrun; s64 it_overrun_last; unsigned int it_signal_seq; --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -585,7 +585,7 @@ static enum alarmtimer_restart alarm_han */ ptr->it_overrun +=3D __alarm_forward_now(alarm, ptr->it_interval, true); ++ptr->it_signal_seq; - ptr->it_active =3D 1; + ptr->it_status =3D POSIX_TIMER_ARMED; result =3D ALARMTIMER_RESTART; } spin_unlock_irqrestore(&ptr->it_lock, flags); --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -453,7 +453,6 @@ static void disarm_timer(struct k_itimer struct cpu_timer *ctmr =3D &timer->it.cpu; struct posix_cputimer_base *base; =20 - timer->it_active =3D 0; if (!cpu_timer_dequeue(ctmr)) return; =20 @@ -494,11 +493,12 @@ static int posix_cpu_timer_del(struct k_ */ WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node)); } else { - if (timer->it.cpu.firing) + if (timer->it.cpu.firing) { ret =3D TIMER_RETRY; - else + } else { disarm_timer(timer, p); - + timer->it_status =3D POSIX_TIMER_DISARMED; + } unlock_task_sighand(p, &flags); } =20 @@ -560,7 +560,7 @@ static void arm_timer(struct k_itimer *t struct cpu_timer *ctmr =3D &timer->it.cpu; u64 newexp =3D cpu_timer_getexpires(ctmr); =20 - timer->it_active =3D 1; + timer->it_status =3D POSIX_TIMER_ARMED; if (!cpu_timer_enqueue(&base->tqhead, ctmr)) return; =20 @@ -586,7 +586,8 @@ static void cpu_timer_fire(struct k_itim { struct cpu_timer *ctmr =3D &timer->it.cpu; =20 - timer->it_active =3D 0; + timer->it_status =3D POSIX_TIMER_DISARMED; + if (unlikely(timer->sigq =3D=3D NULL)) { /* * This a special case for clock_nanosleep, @@ -671,7 +672,7 @@ static int posix_cpu_timer_set(struct k_ ret =3D TIMER_RETRY; } else { cpu_timer_dequeue(ctmr); - timer->it_active =3D 0; + timer->it_status =3D POSIX_TIMER_DISARMED; } =20 /* --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -272,7 +272,7 @@ bool posixtimer_deliver_signal(struct ke if (timr->it_interval && timr->it_signal_seq =3D=3D info->si_sys_private)= { timr->kclock->timer_rearm(timr); =20 - timr->it_active =3D 1; + timr->it_status =3D POSIX_TIMER_ARMED; timr->it_overrun_last =3D timr->it_overrun; timr->it_overrun =3D -1LL; ++timr->it_signal_seq; @@ -292,14 +292,17 @@ bool posixtimer_deliver_signal(struct ke =20 int posix_timer_queue_signal(struct k_itimer *timr) { + enum posix_timer_state state =3D POSIX_TIMER_DISARMED; int ret, si_private =3D 0; enum pid_type type; =20 lockdep_assert_held(&timr->it_lock); =20 - timr->it_active =3D 0; - if (timr->it_interval) + if (timr->it_interval) { + state =3D POSIX_TIMER_REQUEUE_PENDING; si_private =3D ++timr->it_signal_seq; + } + timr->it_status =3D state; =20 type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, si_private); @@ -367,7 +370,7 @@ static enum hrtimer_restart posix_timer_ timr->it_overrun +=3D hrtimer_forward(timer, now, timr->it_interval); ret =3D HRTIMER_RESTART; ++timr->it_signal_seq; - timr->it_active =3D 1; + timr->it_status =3D POSIX_TIMER_ARMED; } } =20 @@ -647,10 +650,10 @@ void common_timer_get(struct k_itimer *t /* interval timer ? */ if (iv) { cur_setting->it_interval =3D ktime_to_timespec64(iv); - } else if (!timr->it_active) { + } else if (timr->it_status =3D=3D POSIX_TIMER_DISARMED) { /* * SIGEV_NONE oneshot timers are never queued and therefore - * timr->it_active is always false. The check below + * timr->it_status is always DISARMED. The check below * vs. remaining time will handle this case. * * For all other timers there is nothing to update here, so @@ -895,7 +898,7 @@ int common_timer_set(struct k_itimer *ti if (kc->timer_try_to_cancel(timr) < 0) return TIMER_RETRY; =20 - timr->it_active =3D 0; + timr->it_status =3D POSIX_TIMER_DISARMED; posix_timer_set_common(timr, new_setting); =20 /* Keep timer disarmed when it_value is zero */ @@ -908,7 +911,8 @@ int common_timer_set(struct k_itimer *ti sigev_none =3D timr->it_sigev_notify =3D=3D SIGEV_NONE; =20 kc->timer_arm(timr, expires, flags & TIMER_ABSTIME, sigev_none); - timr->it_active =3D !sigev_none; + if (!sigev_none) + timr->it_status =3D POSIX_TIMER_ARMED; return 0; } =20 @@ -1007,7 +1011,7 @@ int common_timer_del(struct k_itimer *ti timer->it_interval =3D 0; if (kc->timer_try_to_cancel(timer) < 0) return TIMER_RETRY; - timer->it_active =3D 0; + timer->it_status =3D POSIX_TIMER_DISARMED; return 0; } =20 --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -1,6 +1,12 @@ /* SPDX-License-Identifier: GPL-2.0 */ #define TIMER_RETRY 1 =20 +enum posix_timer_state { + POSIX_TIMER_DISARMED, + POSIX_TIMER_ARMED, + POSIX_TIMER_REQUEUE_PENDING, +}; + struct k_clock { int (*clock_getres)(const clockid_t which_clock, struct timespec64 *tp); From nobody Fri Dec 19 20:14:14 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 043557E119 for ; Wed, 10 Apr 2024 22:47:11 +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=1712789232; cv=none; b=dcFqDBPkRQ3safr91eDlJEId/RWaXRWFAeG+dKpfehNJj6ssEhMUdWlaS6RTTHkD2K/C69zjjKCvWu7eiXRk9Jn/hkPUU5HIXRnMSB81yla2nc66sdqzTyIHvvSW59QYoEvcvDRZJF54jdT2GjuHEa3Tb2jBCufYcIXhe82aWJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789232; c=relaxed/simple; bh=AEiar334sfqpOjIgW/6Bp180YytMBQoPwW2maP7uYRs=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=fdtwTgBkEGCPW+LFJT93qy6MLgG4HpXTzm6h97PSzjZ0QkM/QJwHpoHoCYRiIV6nNQm9O+pvLg+ymN7lTM0+JamlJ7Ao+4FRHqlOvdVozX1iYz2Di3NQTuyXbqS2yrKtu8Ab8H5BodQPNOqOrgDxOIqVmr98r/X7DWk2xEEKnqs= 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=Ne5hvYKc; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=l2HdACHW; 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="Ne5hvYKc"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="l2HdACHW" Message-ID: <20240410165552.957338323@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789229; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Un9A+m4IV/Z9ANmuwoM32FYX2xiyOcqxqSOJRbG47kY=; b=Ne5hvYKcHYDkD4XQhNb8JajbQk8E1Hd/SmBICv8LOvK870MDpHGIiLppg4C2qLwyHninvQ PVXqB4voPpJBuDpo9FPNebEq8D+35mZ8JXBt+nH1smQY1MKtkiq81V1v03tME+Ds17sqzY +A6enh/6cyTdRwhzbqoSC2TcH4G3ZRu/bxSFgvAogRQ/EoSM2Bh79qybJZKv6+R1F4Msoi 6OGxElx1cai5idZW50kD0EuCck8ORijDH5AQMojRVlUMGAIMthWB2D/8NFLDjJDg3GlGk4 opN4625pub6qu06YmtFeEl30wcjHn3LhS1vCEx8R+m65rteLqFsQH2tck6Fmkw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789229; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Un9A+m4IV/Z9ANmuwoM32FYX2xiyOcqxqSOJRbG47kY=; b=l2HdACHWpTJTJKBANyJF/FP8N/MTcOVHSK1HWC9rnuRzunOPVcX7o65ChLmiUGEZzz9FO0 TYDQ1eAVKkRwvMAg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 32/50] posix-timers: Make signal delivery consistent References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:08 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Signals of timers which are reprogammed, disarmed or deleted can deliver signals related to the past. The POSIX spec is blury about this: - "The effect of disarming or resetting a timer with pending expiration notifications is unspecified." - "The disposition of pending signals for the deleted timer is unspecified." In both cases it is reasonable to expect that pending signals are discarded. Especially in the reprogramming case it does not make sense to account for previous overruns or to deliver a signal for a timer which has been disarmed. This makes the behaviour consistent and understandable. Remove the si_sys_private check from the signal delivery code and invoke posix_timer_deliver_signal() unconditionally. Change that function so it controls the actual signal delivery via the return value. It now instructs the signal code to drop the signal when: 1) The timer does not longer exist in the hash table 2) The timer signal_seq value is not the same as the si_sys_private value which was set when the signal was queued. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 2 -- kernel/signal.c | 2 +- kernel/time/posix-cpu-timers.c | 2 +- kernel/time/posix-timers.c | 25 +++++++++++++++---------- 4 files changed, 17 insertions(+), 14 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -137,8 +137,6 @@ static inline void clear_posix_cputimers static inline void posix_cputimers_init_work(void) { } #endif =20 -#define REQUEUE_PENDING 1 - /** * struct k_itimer - POSIX.1b interval timer structure. * @list: List head for binding the timer to signals->posix_timers --- a/kernel/signal.c +++ b/kernel/signal.c @@ -617,7 +617,7 @@ int dequeue_signal(sigset_t *mask, kerne } =20 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) { - if (unlikely(info->si_code =3D=3D SI_TIMER && info->si_sys_private)) { + if (unlikely(info->si_code =3D=3D SI_TIMER)) { if (!posixtimer_deliver_signal(info)) goto again; } --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -746,7 +746,7 @@ static void __posix_cpu_timer_get(struct * - Timers which expired, but the signal has not yet been * delivered */ - if (iv && ((timer->it_signal_seq & REQUEUE_PENDING) || sigev_none)) + if (iv && timer->it_status !=3D POSIX_TIMER_ARMED) expires =3D bump_cpu_timer(timer, now); else expires =3D cpu_timer_getexpires(&timer->it.cpu); --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -269,7 +269,10 @@ bool posixtimer_deliver_signal(struct ke if (!timr) goto out; =20 - if (timr->it_interval && timr->it_signal_seq =3D=3D info->si_sys_private)= { + if (timr->it_signal_seq !=3D info->si_sys_private) + goto out_unlock; + + if (timr->it_interval && timr->it_status =3D=3D POSIX_TIMER_REQUEUE_PENDI= NG) { timr->kclock->timer_rearm(timr); =20 timr->it_status =3D POSIX_TIMER_ARMED; @@ -281,6 +284,7 @@ bool posixtimer_deliver_signal(struct ke } ret =3D true; =20 +out_unlock: unlock_timer(timr, flags); out: spin_lock(¤t->sighand->siglock); @@ -293,19 +297,19 @@ bool posixtimer_deliver_signal(struct ke int posix_timer_queue_signal(struct k_itimer *timr) { enum posix_timer_state state =3D POSIX_TIMER_DISARMED; - int ret, si_private =3D 0; enum pid_type type; + int ret; =20 lockdep_assert_held(&timr->it_lock); =20 if (timr->it_interval) { + timr->it_signal_seq++; state =3D POSIX_TIMER_REQUEUE_PENDING; - si_private =3D ++timr->it_signal_seq; } timr->it_status =3D state; =20 type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; - ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, si_private); + ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, timr->it_signal_seq= ); /* If we failed to send the signal the timer stops. */ return ret > 0; } @@ -670,7 +674,7 @@ void common_timer_get(struct k_itimer *t * is a SIGEV_NONE timer move the expiry time forward by intervals, * so expiry is > now. */ - if (iv && (timr->it_signal_seq & REQUEUE_PENDING || sig_none)) + if (iv && timr->it_status !=3D POSIX_TIMER_ARMED) timr->it_overrun +=3D kc->timer_forward(timr, now); =20 remaining =3D kc->timer_remaining(timr, now); @@ -870,8 +874,6 @@ void posix_timer_set_common(struct k_iti else timer->it_interval =3D 0; =20 - /* Prevent reloading in case there is a signal pending */ - timer->it_signal_seq =3D (timer->it_signal_seq + 2) & ~REQUEUE_PENDING; /* Reset overrun accounting */ timer->it_overrun_last =3D 0; timer->it_overrun =3D -1LL; @@ -889,8 +891,6 @@ int common_timer_set(struct k_itimer *ti if (old_setting) common_timer_get(timr, old_setting); =20 - /* Prevent rearming by clearing the interval */ - timr->it_interval =3D 0; /* * Careful here. On SMP systems the timer expiry function could be * active and spinning on timr->it_lock. @@ -940,6 +940,9 @@ static int do_timer_settime(timer_t time if (old_spec64) old_spec64->it_interval =3D ktime_to_timespec64(timr->it_interval); =20 + /* Prevent signal delivery and rearming. */ + timr->it_signal_seq++; + kc =3D timr->kclock; if (WARN_ON_ONCE(!kc || !kc->timer_set)) error =3D -EINVAL; @@ -1008,7 +1011,6 @@ int common_timer_del(struct k_itimer *ti { const struct k_clock *kc =3D timer->kclock; =20 - timer->it_interval =3D 0; if (kc->timer_try_to_cancel(timer) < 0) return TIMER_RETRY; timer->it_status =3D POSIX_TIMER_DISARMED; @@ -1036,6 +1038,9 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t if (!timer) return -EINVAL; =20 + /* Prevent signal delivery and rearming. */ + timer->it_signal_seq++; + if (unlikely(timer_delete_hook(timer) =3D=3D TIMER_RETRY)) { /* Unlocks and relocks the timer if it still exists */ timer =3D timer_wait_running(timer, &flags); From nobody Fri Dec 19 20:14:14 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 8AADB7FBB0 for ; Wed, 10 Apr 2024 22:47:12 +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=1712789234; cv=none; b=Tkbdz8m9XD4ptJhNnmuJnt95FiSuz6TkOFi6I4OLgr1HMaCY9V5uFnd9md4W0C6kC+BKA6N2I4KzHVezAM6cyLHjSgOaDr656aG2ROOf6hHzc+TTn/W/4ZGWz3hxRJxhanPJQx34YpNRt4SgmJSrammCStaVLUtHPmZ5MkkKYsY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789234; c=relaxed/simple; bh=hYNVuSguWX5kL+feRx+7A+qZ1CJIoDAjlsCoswgjWkw=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=LVUwH5KnDUYkUf+fRt7IFK2+tFyFMskLqDvWu41hPHqTXjtQrYKLrjlB8AdhioV0JHtXp5zLTL+OkIhVdzWRGuAM+ZSDW1qBZ1SPr6WKdW/p0uLJmjAihqXDRZq6wCEvUP/9K27XFlYwPwF3gXOGUl6tGHY4hSlcV5SsxKRGatw= 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=vuz+Dh71; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=OVgsM3sC; 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="vuz+Dh71"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="OVgsM3sC" Message-ID: <20240410165553.018701571@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789230; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=aNS2WvbMpSrKZ+gyfUquRBBrzhJQZEx4EROddFmkK80=; b=vuz+Dh71piLtX10dIzChDEHBEph9k5zEn+2i8jRwosi2JDR4XcBxLkOzJhLYTPymHCCVq2 cLtAw75XODqsnYr6vlXR4i8qxzthjXvHW28NWk9DLDtxD9xmUbSm9qLKzfQXj1z6SAjf7L AOcYFH/xT2oh2Eohv5RtU2swQM5tgGF9GWEbJfuDR+fijQ2VHnZxeIAczE1IjIK+wvpKny J35IfQN6vvtk3GvPoNu2GhWON1UB1Wl98kIddR6PLCxlIN6YUEbYJN0kd0FIhpQTJHv+1j NVhfDyQXDOoaYrBJr9s5IxxSBEj1opynJ1IykEg/BtCnIFjDWcRNOhB0y+cdyA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789230; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=aNS2WvbMpSrKZ+gyfUquRBBrzhJQZEx4EROddFmkK80=; b=OVgsM3sCWMqwALpb2w3q3xUU6iTpbkkM2F+dFQD9XiJRohkgoM2p/QGqmslB9GIqS+9fTn h9+TlGcemjYefeCg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 33/50] posix-timers: Make signal overrun accounting sensible References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:10 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The handling of the timer overrun in the signal code is inconsistent as it takes previous overruns into account. This is just wrong as after the reprogramming of a timer the overrun count starts over from a clean state, i.e. 0. Make the accounting in send_sigqueue() consistent with that. Signed-off-by: Thomas Gleixner --- kernel/signal.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1965,6 +1965,34 @@ int send_sigqueue(struct sigqueue *q, st */ q->info.si_sys_private =3D si_private; =20 + /* + * Set the overrun count to zero unconditionally. The posix timer + * code does not self rearm periodic timers. They are rearmed from + * dequeue_signal(). + * + * But there is a situation where @q is already enqueued: + * + * 1) timer_settime() + * arm_timer() + * 2) timer_expires() + * send_sigqueue(@q) + * enqueue(@q) + * 3) timer_settime() + * arm_timer() + * 4) timer_expires() + * send_sigqueue(@q) <- Observes @q already queued + * + * In this case incrementing si_overrun does not make sense because + * there is no relationship between timer_settime() #1 and #2. + * + * The POSIX specification is useful as always: "The effect of + * disarming or resetting a timer with pending expiration + * notifications is unspecified." + * + * Just do the sensible thing and reset the overrun. + */ + q->info.si_overrun =3D 0; + ret =3D 1; /* the signal is ignored */ result =3D TRACE_SIGNAL_IGNORED; if (!prepare_signal(sig, t, false)) @@ -1972,15 +2000,9 @@ int send_sigqueue(struct sigqueue *q, st =20 ret =3D 0; if (unlikely(!list_empty(&q->list))) { - /* - * If an SI_TIMER entry is already queue just increment - * the overrun count. - */ - q->info.si_overrun++; result =3D TRACE_SIGNAL_ALREADY_PENDING; goto out; } - q->info.si_overrun =3D 0; =20 signalfd_notify(t, sig); pending =3D (type !=3D PIDTYPE_PID) ? &t->signal->shared_pending : &t->pe= nding; From nobody Fri Dec 19 20:14:14 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 E088B8175E for ; Wed, 10 Apr 2024 22:47:13 +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=1712789235; cv=none; b=Uo7qT/60Ujd9L67JKzv3NZoCK8BHo31QAWFWVNKQmZ9KiaAogPVDlBsob9w3+o4JZOmQxDDzzlexRm/hq41fGSO2wYymr+4j7ln1lGkiSCYL55EKAHPJEKHbn6d+EvLZmLp3qa8XCt95G1AdrczMMOeqOi7sdQZXP5JnBrUHDgo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789235; c=relaxed/simple; bh=XsbgZtCOc5ETrVPrJ/FtKkFplUEZ2QIE7aWQKb1cCHc=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=NJIdtOJ1sLLAyBCkWtSAv3G+L6dcJxcmTtkuX7rcikNG2qhvqkjfksvIl147a9liIVUMlVUUidPQ/7jo3AP3xOqMvzK3LZvEoO/f9GiVgd4Ee22b+B6xW5UcDyT+0wcrEyRHed19Y4ZybClGWMqcGVa3hFaXuBxAJhCHsKEbuQ8= 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=VnUu5UoH; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CFtICFy1; 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="VnUu5UoH"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CFtICFy1" Message-ID: <20240410165553.080403121@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789232; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=MQO2EYZPqj5U7pySt49QYzOaZrWaUy6rIJlwsdY8EF8=; b=VnUu5UoHDvK8n4kJSBRl6PdAIf/aC8599XcwM8idmqAUlEszj4tpOzu11AJToVKW+28kfu k64SBsgVoa4CzLclyrWPNDGgtrUl0EU2Lx4quBCLRJzPvi3YPcleI0y6CK7I9YYR0eKLnB ePeXHcKn+9fwfpYk3ZpyKOrV7gEV55M8Vfs8jyVuupCSHHBQy08ZaZYTb+84w8hvWInQkh MYZM6gySeAbSiI0+rueL8lXF+C8I+6Xi3dbaOjeygl5aEtLi0/fWE6grEnHnx4QxfjIf8t wISMl3sHPWMlHilU9n/R+vSSan2rKvw3QOS904d4aNKka7bowAoqNrG0jPQ0EQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789232; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=MQO2EYZPqj5U7pySt49QYzOaZrWaUy6rIJlwsdY8EF8=; b=CFtICFy16+rLTegzsdwBNe7ea/fcn7FE2j/lwCDicVWSMDD214U9/+KkV0B2cyqVu3HiB2 o/YghTJnGQllkABQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 34/50] posix-cpu-timers: Use dedicated flag for CPU timer nanosleep References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:12 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" POSIX CPU timer nanosleep creates a k_itimer on stack and uses the sigq pointer to detect the nanosleep case in the expiry function. Prepare for embedding sigqueue into struct k_itimer by using a dedicated flag for nanosleep. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 4 +++- kernel/time/posix-cpu-timers.c | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -42,6 +42,7 @@ static inline int clockid_to_fd(const cl * @pid: Pointer to target task PID * @elist: List head for the expiry list * @firing: Timer is currently firing + * @nanosleep: Timer is used for nanosleep and is not a regular posix-timer * @handling: Pointer to the task which handles expiry */ struct cpu_timer { @@ -49,7 +50,8 @@ struct cpu_timer { struct timerqueue_head *head; struct pid *pid; struct list_head elist; - int firing; + bool firing; + bool nanosleep; struct task_struct __rcu *handling; }; =20 --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -588,7 +588,7 @@ static void cpu_timer_fire(struct k_itim =20 timer->it_status =3D POSIX_TIMER_DISARMED; =20 - if (unlikely(timer->sigq =3D=3D NULL)) { + if (unlikely(ctmr->nanosleep)) { /* * This a special case for clock_nanosleep, * not a normal timer from sys_timer_create. @@ -1479,6 +1479,7 @@ static int do_cpu_nanosleep(const clocki timer.it_overrun =3D -1; error =3D posix_cpu_timer_create(&timer); timer.it_process =3D current; + timer.it.cpu.nanosleep =3D true; =20 if (!error) { static struct itimerspec64 zero_it; From nobody Fri Dec 19 20:14:14 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 B02A985928 for ; Wed, 10 Apr 2024 22:47:15 +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=1712789237; cv=none; b=b7h3UGMtp2NCxNuJ59nk/Qnc3yf5VZfot4+OkXtc4Zz3VreOdxQu2KW7b9e99yVc78cpKfGG62JkRWh6tBmeOawQEOORX4fr3oSl0GeEpkLauFFmIFVfSi2r2CI4RLlwsnGCYi4J5ZYKm9gykbKs4o5v7pTUXKVUfX0RbwgaQAY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789237; c=relaxed/simple; bh=SeQ2OVxjlLINqynDEK9vT3MFjf860yuU+9xoy6p/o24=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=pPKYgwxZPWxwIRiVPqUTRMSpCwA5R+UcCrDAySGUWeJRd+pGl2y35GpBJ8DyBEybAk8p11Z49JeGv/37ZUcyjHD59x2vAZv9vtQWVf/Y1izPnKs6lOfMECgzl5GBNEuOim6s0f9bzQjHkUvzuRSpmeDIGZKve4xc9y5fR+ZQzKM= 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=da5FNt0f; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=o/yUsh2y; 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="da5FNt0f"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="o/yUsh2y" Message-ID: <20240410165553.141005565@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789234; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=hZthBClJqDYnocBuqiMS4yLZv+QB2bQbndBZ4lDgqqQ=; b=da5FNt0f+6gCM2oMb3FB/ZNQNsbuKurn7MGmZK5KiVJcmraK5ghlj9ln3bvPOljHUhhTxa t/8D3Bx590UZQyMWTPHPtaoXEMC+ulp2l1U5RlFbIsUvy96lw4BnoRWyldaYwgpP4iJ2Hc gNU42f7lnGYuHWqoOFDdy38OnOtNaUF/K+Z8Z1rh7wI3pboqxtkrvOtd7TPeAIl9lL5p7f RvHp0zesNYkplNVWCN2TXuNkymvY1RVCWy71slE6NNWvgDGIEeElrcnipwla6YgXknDwZQ jARjN4eiHAKI5T2K3WxIuixrxCPVx5Shekwbuav3CKPKZWXVps/TfRTG3t3vwA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789234; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=hZthBClJqDYnocBuqiMS4yLZv+QB2bQbndBZ4lDgqqQ=; b=o/yUsh2y3J7rZsQfE1irH6DnTnNXFms0FT97Ok/4Ti4uS9VaBGWRxFK73P5shf12exmEvN wuzgyCgSk+iBk2DA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 35/50] posix-timers: Add a refcount to struct k_itimer References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:13 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. To make this work correctly this needs reference counting so that timer deletion does not free the timer prematuraly when there is a signal queued or delivered concurrently. Add a rcuref to the posix timer part. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 14 ++++++++++++++ kernel/time/posix-timers.c | 7 ++++--- 2 files changed, 18 insertions(+), 3 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -6,11 +6,13 @@ #include #include #include +#include #include #include =20 struct kernel_siginfo; struct task_struct; +struct k_itimer; =20 static inline clockid_t make_process_cpuclock(const unsigned int pid, const clockid_t clock) @@ -105,6 +107,7 @@ static inline void posix_cputimers_rt_wa =20 void posixtimer_rearm_itimer(struct task_struct *p); bool posixtimer_deliver_signal(struct kernel_siginfo *info); +void posixtimer_free_timer(struct k_itimer *timer); =20 /* Init task static initializer */ #define INIT_CPU_TIMERBASE(b) { \ @@ -129,6 +132,7 @@ static inline void posix_cputimers_group u64 cpu_limit) { } static inline void posixtimer_rearm_itimer(struct task_struct *p) { } static inline bool posixtimer_deliver_signal(struct kernel_siginfo *info) = { return false; } +static inline void posixtimer_free_timer(struct k_itimer *timer) { } #endif =20 #ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK @@ -156,6 +160,7 @@ static inline void posix_cputimers_init_ * @it_signal: Pointer to the creators signal struct * @it_pid: The pid of the process/task targeted by the signal * @it_process: The task to wakeup on clock_nanosleep (CPU timers) + * @rcuref: Reference count for life time management * @sigq: Pointer to preallocated sigqueue * @it: Union representing the various posix timer type * internals. @@ -180,6 +185,7 @@ struct k_itimer { struct task_struct *it_process; }; struct sigqueue *sigq; + rcuref_t rcuref; union { struct { struct hrtimer timer; @@ -200,4 +206,12 @@ void set_process_cpu_timer(struct task_s =20 int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new); =20 +#ifdef CONFIG_POSIX_TIMERS +static inline void posixtimer_putref(struct k_itimer *tmr) +{ + if (rcuref_put(&tmr->rcuref)) + posixtimer_free_timer(tmr); +} +#endif /* !CONFIG_POSIX_TIMERS */ + #endif --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -417,6 +417,7 @@ static struct k_itimer * alloc_posix_tim return NULL; } clear_siginfo(&tmr->sigq->info); + rcuref_init(&tmr->rcuref, 1); return tmr; } =20 @@ -427,7 +428,7 @@ static void k_itimer_rcu_free(struct rcu kmem_cache_free(posix_timers_cache, tmr); } =20 -static void posix_timer_free(struct k_itimer *tmr) +void posixtimer_free_timer(struct k_itimer *tmr) { put_pid(tmr->it_pid); sigqueue_free(tmr->sigq); @@ -439,7 +440,7 @@ static void posix_timer_unhash_and_free( spin_lock(&hash_lock); hlist_del_rcu(&tmr->t_hash); spin_unlock(&hash_lock); - posix_timer_free(tmr); + posixtimer_putref(tmr); } =20 static int common_timer_create(struct k_itimer *new_timer) @@ -474,7 +475,7 @@ static int do_timer_create(clockid_t whi */ new_timer_id =3D posix_timer_add(new_timer); if (new_timer_id < 0) { - posix_timer_free(new_timer); + posixtimer_free_timer(new_timer); return new_timer_id; } From nobody Fri Dec 19 20:14:14 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 7B56812D20C for ; Wed, 10 Apr 2024 22:47:17 +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=1712789238; cv=none; b=sYs59iZwtsiWJS40UmB7ksFVM3tewpK76chez0GG1dNmNVQ+Fv33311QCAY8VVcXG+9hjwjMjj2YFrjcX9MRh6tWrrDqrZlTmnr0YbMwxzT5ayRt/sZgCeAv3aQ5nM1c0AO/GfGRKL1rsMFKIiTiamqnQ54Bber3On/rpLaMLOA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789238; c=relaxed/simple; bh=189exjZEUItQ6zpVS/Cs7Gleaem5nfeA9wEfKXDkURg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=lg6Off/1q6Z5QXcFjE9gSNgrbNsgMbIWMElBdOIT500sZgbh6ubBtShzxSBfgvFOhVkMQrjIkOkN34E4SnIVKPKfhgqJVBQUCnSSh9Z2PeojRHe0rlK7m5zZriuyvkM75kKVdnNUaGc2eKCfYkl0/G1s/Cx6sCpBrEl1SLjcQq8= 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=Q8l4ehUr; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=oJFJvzUl; 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="Q8l4ehUr"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="oJFJvzUl" Message-ID: <20240410165553.202164808@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789236; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=RIInviEgVz+BvUxJyZo2yuM0Ys8NWIcWoXCI4X06Uos=; b=Q8l4ehUrSY8KNwdv2J4h50yWeOBPg/t5S95UirgpyEtbLiQf70zxze+YPRs7z0vBr7gofS SwAg2uqbiWoyACVKT+Y6iB6sX6baAIo+5ldlpiH26k9u7VUjhii13BO8oSL9+bjr/s3NRT I1v3eVHw28IiO4HUgEGPrVgrGa0VnB0O+7n552T6r+D7Kkud7gp4HkK6VymTb1gz4agUGQ ks7dlxv52LLS/BCf9sC7gZV5aojZJ4e91YYuVtFR8yBtldM4VbAjCQ4796pfQmNtxwUqGI aR11GnO8/W8RedtPmK/6boDOJTGfDGI9fDfSsk6wah/WWfksem+t0vS1oI3ZeA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789236; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=RIInviEgVz+BvUxJyZo2yuM0Ys8NWIcWoXCI4X06Uos=; b=oJFJvzUlOldq1VWiahteWWiYoFSg7iBQZkGSUPqTzvJH74Ccx8EKZoGUxdv7Yun9zcOpek +a1FihEDfDBziDAA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 36/50] signal: Split up __sigqueue_alloc() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:15 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. Reorganize __sigqueue_alloc() so the ucounts retrieval and the initialization can be used independently. No functional change. Signed-off-by: Thomas Gleixner --- kernel/signal.c | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -396,16 +396,9 @@ void task_join_group_stop(struct task_st task_set_jobctl_pending(task, mask | JOBCTL_STOP_PENDING); } =20 -/* - * allocate a new signal queue record - * - this may be called without locks if and only if t =3D=3D current, oth= erwise an - * appropriate lock must be held to stop the target task from exiting - */ -static struct sigqueue * -__sigqueue_alloc(int sig, struct task_struct *t, gfp_t gfp_flags, - int override_rlimit, const unsigned int sigqueue_flags) +static struct ucounts *sig_get_ucounts(struct task_struct *t, int sig, + int override_rlimit) { - struct sigqueue *q =3D NULL; struct ucounts *ucounts; long sigpending; =20 @@ -424,19 +417,44 @@ static struct sigqueue * if (!sigpending) return NULL; =20 - if (override_rlimit || likely(sigpending <=3D task_rlimit(t, RLIMIT_SIGPE= NDING))) { - q =3D kmem_cache_alloc(sigqueue_cachep, gfp_flags); - } else { + if (unlikely(!override_rlimit && sigpending > task_rlimit(t, RLIMIT_SIGPE= NDING))) { + dec_rlimit_put_ucounts(ucounts, UCOUNT_RLIMIT_SIGPENDING); print_dropped_signal(sig); + return NULL; } =20 - if (unlikely(q =3D=3D NULL)) { + return ucounts; +} + +static void __sigqueue_init(struct sigqueue *q, struct ucounts *ucounts, + const unsigned int sigqueue_flags) +{ + INIT_LIST_HEAD(&q->list); + q->flags =3D sigqueue_flags; + q->ucounts =3D ucounts; +} + +/* + * allocate a new signal queue record + * - this may be called without locks if and only if t =3D=3D current, oth= erwise an + * appropriate lock must be held to stop the target task from exiting + */ +static struct sigqueue *__sigqueue_alloc(int sig, struct task_struct *t, g= fp_t gfp_flags, + int override_rlimit, const unsigned int sigqueue_flags) +{ + struct ucounts *ucounts =3D sig_get_ucounts(t, sig, override_rlimit); + struct sigqueue *q; + + if (!ucounts) + return NULL; + + q =3D kmem_cache_alloc(sigqueue_cachep, gfp_flags); + if (!q) { dec_rlimit_put_ucounts(ucounts, UCOUNT_RLIMIT_SIGPENDING); - } else { - INIT_LIST_HEAD(&q->list); - q->flags =3D sigqueue_flags; - q->ucounts =3D ucounts; + return NULL; } + + __sigqueue_init(q, ucounts, sigqueue_flags); return q; } From nobody Fri Dec 19 20:14:14 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 A775712F598 for ; Wed, 10 Apr 2024 22:47:19 +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=1712789241; cv=none; b=qMx/EMi6c0UNl5CaBGPSHR5JR1AhsyGq1BlVlC8E3/QSqobQn2NUaoh5VRmhFYBxkTSbW2xBYY8PRWyLHVIAOlybGRAXGBAlIFekh4JTraiiMw9VIobxHwGGGCKfuzVbKTp9VcfTMfGlFK6IKJW67kqGNJIiURbqbfNXa3h5rlg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789241; c=relaxed/simple; bh=JBVsg51a1C6emguYqjQ6r5sm9uw8SoWsLBDNYeJITWk=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=EGjzKCOi0iMAWGkl5sNiguju6ZMxOy5M9Ui4Ayl3y+nksYc4bAkxMJFyfg5pfSwB06XYIgbzesVJel/qxiMHgdUYNXrYZ35GUVUWPEWvX+zbSIqwLIy8w4delArE4CWCICgvsxJVmleZpnoFZ/fNU5o14helTCNoWJNI1Lwg1LA= 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=RCBCziDD; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uHNfD1Ve; 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="RCBCziDD"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uHNfD1Ve" Message-ID: <20240410165553.263875002@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Uih5CqJpJPUslYvMN+crWXfC4uyqUetkrCUQg27T5sU=; b=RCBCziDDYk8F0k08nosKA+Ykwj99TGVPTAO8C1s4EAvyRezArbTUnTxcqm9pLdoFCzNCl/ DjV5+/1UTy6p3rp7we9OH1j38D90ObzcZ/xhq9MakdCmGDGkNxXo0jIp1AJNnKWNNWfRI8 q+UcU/i0XjzRWTYOw4UDeKf1cu3E1vSqUo0NwJrMQ4dX+LeZwt1RuUeM+/nUCIVlaFIWDL LSgVfj8L5I4SCtMqqB2ioLs/nttUZg77sErDBvEVQZ8V18FRHBeLtZTNcqsXjT81lq9QPD xpp4I4jbkS2YkLg0e7+jjkwAtKWYyWRFaGVNbElTe+tcqEs0GXliPCh4f7CIlg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Uih5CqJpJPUslYvMN+crWXfC4uyqUetkrCUQg27T5sU=; b=uHNfD1VesuL0ML1QFJEDCYSbAmdJvPtbk96Erb0B7GRhFHI3b9yz2W0CXTpNpfu7WBdSmZ mi5dEovzu4lAA4Aw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 37/50] signal: Provide posixtimer_sigqueue_init() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:17 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. Provide a new function to initialize the embedded sigqueue to prepare for that. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 2 ++ kernel/signal.c | 11 +++++++++++ 2 files changed, 13 insertions(+) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -12,6 +12,7 @@ =20 struct kernel_siginfo; struct task_struct; +struct sigqueue; struct k_itimer; =20 static inline clockid_t make_process_cpuclock(const unsigned int pid, @@ -106,6 +107,7 @@ static inline void posix_cputimers_rt_wa } =20 void posixtimer_rearm_itimer(struct task_struct *p); +bool posixtimer_init_sigqueue(struct sigqueue *q); bool posixtimer_deliver_signal(struct kernel_siginfo *info); void posixtimer_free_timer(struct k_itimer *timer); =20 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1909,6 +1909,17 @@ void flush_itimer_signals(void) spin_unlock_irqrestore(&tsk->sighand->siglock, flags); } =20 +bool posixtimer_init_sigqueue(struct sigqueue *q) +{ + struct ucounts *ucounts =3D sig_get_ucounts(current, -1, 0); + + if (!ucounts) + return false; + clear_siginfo(&q->info); + __sigqueue_init(q, ucounts, SIGQUEUE_PREALLOC); + return true; +} + struct sigqueue *sigqueue_alloc(void) { return __sigqueue_alloc(-1, current, GFP_KERNEL, 0, SIGQUEUE_PREALLOC); From nobody Fri Dec 19 20:14:14 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 DF39F85928 for ; Wed, 10 Apr 2024 22:47:20 +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=1712789242; cv=none; b=rhIAs3fyM3ys/bHPdnyuiePGxtrQmxsBcYpa4Ojv3iu0PvPeEAABp42IHDe5QpkAFeL0CJFKHoO0Ec+P9LI2VU1RtLipx5OcK4gx2k/TBN4FH+b+CIEVtGlego250nfMDyrHhmlYB3MiyESG7Vjpx4mao59AGuybRCCxgmfGm7c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789242; c=relaxed/simple; bh=eFn5GMxZi9L4ZeXpUtDZMmDhnFXqtlmDu0N+Bo14zl0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=L6iZUCkhRKKz8QL89I8RS95YW4OMrYRmIMz0mCPOHOVZX81nQ1JOTNdZ4c8tz8LAKX51nCYWFAOX8qmLEhK+180ufQYrByZgMtcMR6TOkSczZpKpEc1lAWVVFzYbN/rPiPHpchRxsjNcQ5Lgc3dS3F7c0g/f8Evznd15HYmmilk= 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=mytQT9Mw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=H4yS4X44; 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="mytQT9Mw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="H4yS4X44" Message-ID: <20240410165553.325347784@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789239; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=dkh9zDfRIgoUx8unNZEZZL+uvuDybK3sMar20TESOQE=; b=mytQT9MwPFn91zP6paQCMFi/+0uOfGzj/80NEpk5KW92J2mRcA7kMfXvulNYTU/CkFBOg6 HlR4eCkVKdHFAtPcK6QYuNFlReSM9bQgJ+rS16VL32D445kMTeNyCSC0nv067k+HjKUSq3 iZrFkVuPonqw/iBlXlu8+hUVu3b+6c/tlg7A25N+fXCZAP4D7HlgqPez9OCLpN++RARqFS 8h7uE3DfPF0GN9Lm+X4FnuEwJOgNYLDJmdoLP/SqEubrpk8ieeVM8UAtNbFbQBoJJPjfQn K9RQbsVtQHIbvOgOw2lcbE9eu2Ro181C0XEOQ2kqAdzYfAnPK3lz87qy/EHM2w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789239; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=dkh9zDfRIgoUx8unNZEZZL+uvuDybK3sMar20TESOQE=; b=H4yS4X44nEM523JTZp3U+DwkDzCYodCU+f0KFOphTWWYn1N9ARtPSOFoQIwbA8YIKCVp7R oWa8BdiDsCNAEkCw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 38/50] signal: Add sys_private_ptr to siginfo::_sifields::_timer References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:19 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" On signal delivery collect_signal() copies the queued siginfo into a caller provided siginfo struct. The posix timer signal delivery code then uses siginfo::si_tid to lookup the timer in the hash table. That's required today as the timer and the sigqueue are separate entities and have different life time rules. The sigqueue will be embedded into struct k_itimer to address a few issues in the posix timer signal handling, which means the life time rules are not longer separate, which can spare the lookup. Due to locking rules posixtimer_deliver_signal() cannot be invoked from collect_signal(). The timer pointer could be handed down from collect_signal() to dequeue_signal(), but that's just overhead for the non-posixtimer case. There is room in the _sifields union for an extra pointer which will be used later for storing the timer pointer. This field is copied with siginfo and cleared before the info is delivered to userspace like the existing si_sys_private field. Signed-off-by: Thomas Gleixner --- arch/x86/kernel/signal_32.c | 2 +- arch/x86/kernel/signal_64.c | 2 +- include/uapi/asm-generic/siginfo.h | 2 ++ kernel/time/posix-timers.c | 4 +++- 4 files changed, 7 insertions(+), 3 deletions(-) --- a/arch/x86/kernel/signal_32.c +++ b/arch/x86/kernel/signal_32.c @@ -456,7 +456,7 @@ CHECK_SI_OFFSET(_timer); /* compat_siginfo_t doesn't have si_sys_private */ CHECK_SI_SIZE (_timer, 3*sizeof(int)); #else -CHECK_SI_SIZE (_timer, 4*sizeof(int)); +CHECK_SI_SIZE (_timer, 5*sizeof(int)); #endif static_assert(offsetof(siginfo32_t, si_tid) =3D=3D 0x0C); static_assert(offsetof(siginfo32_t, si_overrun) =3D=3D 0x10); --- a/arch/x86/kernel/signal_64.c +++ b/arch/x86/kernel/signal_64.c @@ -456,7 +456,7 @@ static_assert(offsetof(siginfo_t, si_pid static_assert(offsetof(siginfo_t, si_uid) =3D=3D 0x14); =20 CHECK_SI_OFFSET(_timer); -CHECK_SI_SIZE (_timer, 6*sizeof(int)); +CHECK_SI_SIZE (_timer, 8*sizeof(int)); static_assert(offsetof(siginfo_t, si_tid) =3D=3D 0x10); static_assert(offsetof(siginfo_t, si_overrun) =3D=3D 0x14); static_assert(offsetof(siginfo_t, si_value) =3D=3D 0x18); --- a/include/uapi/asm-generic/siginfo.h +++ b/include/uapi/asm-generic/siginfo.h @@ -47,6 +47,7 @@ union __sifields { int _overrun; /* overrun count */ sigval_t _sigval; /* same as below */ int _sys_private; /* not to be passed to user */ + void *_sys_privptr; /* not to be passed to user */ } _timer; =20 /* POSIX.1b signals */ @@ -146,6 +147,7 @@ typedef struct siginfo { #define si_tid _sifields._timer._tid #define si_overrun _sifields._timer._overrun #define si_sys_private _sifields._timer._sys_private +#define si_sys_privptr _sifields._timer._sys_privptr #define si_status _sifields._sigchld._status #define si_utime _sifields._sigchld._utime #define si_stime _sifields._sigchld._stime --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -289,8 +289,9 @@ bool posixtimer_deliver_signal(struct ke out: spin_lock(¤t->sighand->siglock); =20 - /* Don't expose the si_sys_private value to userspace */ + /* Don't expose the si_sys_priv* values to userspace */ info->si_sys_private =3D 0; + info->si_sys_privptr =3D NULL; return ret; } =20 @@ -505,6 +506,7 @@ static int do_timer_create(clockid_t whi =20 new_timer->sigq->info.si_tid =3D new_timer->it_id; new_timer->sigq->info.si_code =3D SI_TIMER; + new_timer->sigq->info.si_sys_privptr =3D new_timer; =20 if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id)))= { error =3D -EFAULT; From nobody Fri Dec 19 20:14:14 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 CC532130AF5 for ; Wed, 10 Apr 2024 22:47:22 +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=1712789244; cv=none; b=LsuJ0btTvAHI20F0IDLLNT9ahq38nX4LNqra6w+pb+5A4ENHAxXpak8cAcAco4v8xL3DtQiV6YPwrHCQ9OeSPEI4DJYek0ACdyZ41Ao1gB6Sb4R630edznefOPqP7Xw0ZnGD6Cx0PQ4vvwr5iMjLfkvsStd9aSrbBIVhgoQEF9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789244; c=relaxed/simple; bh=Bp/mJsKy0K7y+W6reqzXaf54OlcjiFTLH7F90/BxRvU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=PM3AgPrRRqNS7i88h8XdvonOYvyfQzbPXenPDk2fz/UI5QFbkLX5Dtfn96edCEdmjzl4fSxJYZAVCzFBpuR8+hKtPP1fOTgSzDmIF7+c4knOjPs502MvfVf7S+QqN6BAOCWxFD4+bmZ2iQ4u5CsUYBI8MOFqkqk4+qgaCEcnBYI= 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=IQJwS85w; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=VwaX23gM; 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="IQJwS85w"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="VwaX23gM" Message-ID: <20240410165553.387066593@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789241; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=lbd4hY6DO5yS4CWfhpTcHrpvo9CKLyiSwH3SuNY1ueE=; b=IQJwS85wFDXsrKe0C6XWzpveznQFewwTyDCb6S/ynb+fO+Ap9ITuNiIhpcYWswPXOLW/Ov Cr1WJEL3aPShg3IazMwt1/vQygY1OmwgxbCt0O0ubNuwG7cCTCAPx1SINo6Jupgbx6kt0N YaKRmjz6u6QO5IIHNLXuZ8WKKlCZvveBhb0Up3CrI21o+RXS8auZdZQp2XFaIYk3ne8zY3 xmzIimj3nXjOUFzul8p+785gel/hSyDdFh9zppUwvyKaAniRphH/F6Y2VXB/VYkDKnFhdZ /79LvgkydAvGJlCnoYZ1VGEJsesmMBO+8F6LE1C2Sz3XRoRVdIxbhTl49/OBkQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789241; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=lbd4hY6DO5yS4CWfhpTcHrpvo9CKLyiSwH3SuNY1ueE=; b=VwaX23gMKC01DU1IKqG7texaZGLY/Rg1ekcZAmiKs1W3FG27Latp+P3mvI3Kc+OUWf9vQF EB17bB3MjtD5XaCQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 39/50] posix-timers: Store PID type in the timer References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:20 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" instead of re-evaluating the signal delivery mode everywhere. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 2 ++ kernel/time/posix-timers.c | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -180,6 +181,7 @@ struct k_itimer { s64 it_overrun_last; unsigned int it_signal_seq; int it_sigev_notify; + enum pid_type it_pid_type; ktime_t it_interval; struct signal_struct *it_signal; union { --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -298,7 +298,6 @@ bool posixtimer_deliver_signal(struct ke int posix_timer_queue_signal(struct k_itimer *timr) { enum posix_timer_state state =3D POSIX_TIMER_DISARMED; - enum pid_type type; int ret; =20 lockdep_assert_held(&timr->it_lock); @@ -309,8 +308,7 @@ int posix_timer_queue_signal(struct k_it } timr->it_status =3D state; =20 - type =3D !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDT= YPE_PID; - ret =3D send_sigqueue(timr->sigq, timr->it_pid, type, timr->it_signal_seq= ); + ret =3D send_sigqueue(timr->sigq, timr->it_pid, timr->it_pid_type, timr->= it_signal_seq); /* If we failed to send the signal the timer stops. */ return ret > 0; } @@ -504,6 +502,11 @@ static int do_timer_create(clockid_t whi new_timer->it_pid =3D get_pid(task_tgid(current)); } =20 + if (new_timer->it_sigev_notify & SIGEV_THREAD_ID) + new_timer->it_pid_type =3D PIDTYPE_PID; + else + new_timer->it_pid_type =3D PIDTYPE_TGID; + new_timer->sigq->info.si_tid =3D new_timer->it_id; new_timer->sigq->info.si_code =3D SI_TIMER; new_timer->sigq->info.si_sys_privptr =3D new_timer; From nobody Fri Dec 19 20:14:14 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 310A9131730 for ; Wed, 10 Apr 2024 22:47:25 +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=1712789246; cv=none; b=l0HVSoJiZa5xjzAeFhb3z16as1omFgGDUz+Spt5J1mHS+SzF8K/xWU7mCvl+saM+9sAYkncVbT5CjRoVYZsDxx2wYv+cHZ9VpaD/9NqPjDw/GSgMTu+5mdgALeanyGNVpGD6lnexuBNuSxvxs2P8mDL47zL5FPtiXMtrpFm5Kfs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789246; c=relaxed/simple; bh=EqhPVQMzADG4QMzVNntP2fSlusFXVBtkMeermLs0koQ=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Pud7atY/2wKb3jqk1czwP+ePV1NXOY/Pi3TV6l9ywVotOLjct/hKvm/fuz7n4/ca4jUo9DPAMRYbX9aVaSC5G/UjP0AlDi2igrXLLbkzVc5BXDvki5+jo161C7H84RTnJUqdygQp9k1bJXUcBp6Uif6eA7yQDMAp5sprvoAJB0w= 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=ruyeMb6K; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=5W0MZRt9; 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="ruyeMb6K"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="5W0MZRt9" Message-ID: <20240410165553.452316924@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789243; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=bEYSYyylkeIDlYjAvzDVPfVWeutTKCJkm14TlxmYRZI=; b=ruyeMb6KSimPAYvkN1VUcoRsqDEHU+mQZWf6AWitEbZozL2GmZzg3m4CdB9nXMxC0Vvc1G UdnzoqSpaD+xoZAKHTKa+xpcRi8kXQZIs3dtKyK9SIqh1eehUXPrUthBVyXaUOFbUEXETQ chLgVHGDf0uhEQ+NAnuPn4cJocV0bjVx5wbWYbgzEzRFCbmaGSShFNvfjMR3WeYm7MsuSI Get9snDnWsJP6HB05ljSQHNfko4RMbDSnA/uI2rmWMTPoicExVX4K4A8B3vGLdwQ1dOtAD iQzoKNVHiU8DILNG0g6Xie5JNTlPRueH4c69pWG2gOfNv+noo+8wCDhmtEfqIw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789243; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=bEYSYyylkeIDlYjAvzDVPfVWeutTKCJkm14TlxmYRZI=; b=5W0MZRt941HhUZ+PUSGzsEiZ9o0tz8sOEmS1ofNmfFaZTj0DIg8c/WMlvlVIZ68MPBoKOl lSFKaRI9i8I1baAQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 40/50] signal: Refactor send_sigqueue() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:22 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To handle posix timers which have their signal ignored via SIG_IGN properly its required to requeue a ignored signal for delivery when SIG_IGN is lifted so the timer gets rearmed. Split the required code out of send_sigqueue() so it can be reused in context of sigaction(). While at it rename send_sigqueue() to posixtimer_send_sigqueue() so its clear what this is about. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 1=20 include/linux/sched/signal.h | 1=20 kernel/signal.c | 73 ++++++++++++++++++++++++--------------= ----- kernel/time/posix-timers.c | 2 - 4 files changed, 44 insertions(+), 33 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -109,6 +109,7 @@ static inline void posix_cputimers_rt_wa =20 void posixtimer_rearm_itimer(struct task_struct *p); bool posixtimer_init_sigqueue(struct sigqueue *q); +int posixtimer_send_sigqueue(struct k_itimer *tmr); bool posixtimer_deliver_signal(struct kernel_siginfo *info); void posixtimer_free_timer(struct k_itimer *timer); =20 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -339,7 +339,6 @@ extern int send_sig(int, struct task_str extern int zap_other_threads(struct task_struct *p); extern struct sigqueue *sigqueue_alloc(void); extern void sigqueue_free(struct sigqueue *); -extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type, i= nt si_private); extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *); =20 static inline void clear_notify_signal(void) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1951,38 +1951,53 @@ void sigqueue_free(struct sigqueue *q) __sigqueue_free(q); } =20 -int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type,= int si_private) +static void posixtimer_queue_sigqueue(struct sigqueue *q, struct task_stru= ct *t, enum pid_type type) { - int sig =3D q->info.si_signo; struct sigpending *pending; + int sig =3D q->info.si_signo; + + signalfd_notify(t, sig); + pending =3D (type !=3D PIDTYPE_PID) ? &t->signal->shared_pending : &t->pe= nding; + list_add_tail(&q->list, &pending->list); + sigaddset(&pending->signal, sig); + complete_signal(sig, t, type); +} + +/* + * This function is used by POSIX timers to deliver a timer signal. + * Where type is PIDTYPE_PID (such as for timers with SIGEV_THREAD_ID + * set), the signal must be delivered to the specific thread (queues + * into t->pending). + * + * Where type is not PIDTYPE_PID, signals must be delivered to the + * process. In this case, prefer to deliver to current if it is in + * the same thread group as the target process, which avoids + * unnecessarily waking up a potentially idle task. + */ +static inline struct task_struct *posixtimer_get_target(struct k_itimer *t= mr) +{ + struct task_struct *t =3D pid_task(tmr->it_pid, tmr->it_pid_type); + + if (t && tmr->it_pid_type !=3D PIDTYPE_PID && same_thread_group(t, curren= t)) + t =3D current; + return t; +} + +int posixtimer_send_sigqueue(struct k_itimer *tmr) +{ + struct sigqueue *q =3D tmr->sigq; + int sig =3D q->info.si_signo; struct task_struct *t; unsigned long flags; int ret, result; =20 - if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC))) - return 0; - if (WARN_ON_ONCE(q->info.si_code !=3D SI_TIMER)) - return 0; - ret =3D -1; rcu_read_lock(); =20 - /* - * This function is used by POSIX timers to deliver a timer signal. - * Where type is PIDTYPE_PID (such as for timers with SIGEV_THREAD_ID - * set), the signal must be delivered to the specific thread (queues - * into t->pending). - * - * Where type is not PIDTYPE_PID, signals must be delivered to the - * process. In this case, prefer to deliver to current if it is in - * the same thread group as the target process, which avoids - * unnecessarily waking up a potentially idle task. - */ - t =3D pid_task(pid, type); + t =3D posixtimer_get_target(tmr); if (!t) goto ret; - if (type !=3D PIDTYPE_PID && same_thread_group(t, current)) - t =3D current; + if (!likely(lock_task_sighand(t, &flags))) goto ret; =20 @@ -1992,7 +2007,7 @@ int send_sigqueue(struct sigqueue *q, st * decides based on si_sys_private whether to invoke * posixtimer_rearm() or not. */ - q->info.si_sys_private =3D si_private; + q->info.si_sys_private =3D tmr->it_signal_seq; =20 /* * Set the overrun count to zero unconditionally. The posix timer @@ -2023,24 +2038,20 @@ int send_sigqueue(struct sigqueue *q, st q->info.si_overrun =3D 0; =20 ret =3D 1; /* the signal is ignored */ - result =3D TRACE_SIGNAL_IGNORED; - if (!prepare_signal(sig, t, false)) + if (!prepare_signal(sig, t, false)) { + result =3D TRACE_SIGNAL_IGNORED; goto out; + } =20 ret =3D 0; if (unlikely(!list_empty(&q->list))) { result =3D TRACE_SIGNAL_ALREADY_PENDING; goto out; } - - signalfd_notify(t, sig); - pending =3D (type !=3D PIDTYPE_PID) ? &t->signal->shared_pending : &t->pe= nding; - list_add_tail(&q->list, &pending->list); - sigaddset(&pending->signal, sig); - complete_signal(sig, t, type); + posixtimer_queue_sigqueue(q, t, tmr->it_pid_type); result =3D TRACE_SIGNAL_DELIVERED; out: - trace_signal_generate(sig, &q->info, t, type !=3D PIDTYPE_PID, result); + trace_signal_generate(sig, &q->info, t, tmr->it_pid_type !=3D PIDTYPE_PID= , result); unlock_task_sighand(t, &flags); ret: rcu_read_unlock(); --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -308,7 +308,7 @@ int posix_timer_queue_signal(struct k_it } timr->it_status =3D state; =20 - ret =3D send_sigqueue(timr->sigq, timr->it_pid, timr->it_pid_type, timr->= it_signal_seq); + ret =3D posixtimer_send_sigqueue(timr); /* If we failed to send the signal the timer stops. */ return ret > 0; } From nobody Fri Dec 19 20:14:14 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 A5C0713175F for ; Wed, 10 Apr 2024 22:47:26 +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=1712789248; cv=none; b=bNVpeR9RueThE8ZQAbkPPPl+ShyhAnxGMHv76UkkcFWpjsptRdSztIDYUi2beQmpj+VhKDDjHU/TD2Met+MoGJ/4aW/DBmoyfBekjuSjHsTUmwUNQS1eSubKiTWGlYAtkWxzHS3AIOetxGaV7pbot0iK5ZC0D8sPGzRN45TSNqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789248; c=relaxed/simple; bh=Av97VYgeAlc/bafwWL1PU9mzhCMFWi3Lv4kAdQb33FE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=gUgnfpSs6rlmYfUTYziSNIhDjply2LyGj9t/gQAFslqaGodaYcjszooDd4EiLegWT7u/P4/gogElkSSjP7600XKSQAhJlld8+wxBj1zUbkS+Zf6Cun8kv4Tg7TPS7lg+dj54ucsWUZl3Nmfma6UDRMd2FUMIFC8B9chekCR2pVY= 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=3kbH20h3; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=iJEnKXMJ; 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="3kbH20h3"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="iJEnKXMJ" Message-ID: <20240410165553.514023899@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789244; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=QZvmW79aSWaOCLeBfAMHsnSFDDjvmnR1BtBxPWfUF0o=; b=3kbH20h3GZBDT7shxup5NTYfeMLvf3Fo/aRo/kt4GJ8Vq4mO6y27gUh04toPQDEjGFFvVu lJfJ9xZ3nl7sgSJU1EKmrR01OkZdJneyb3sDEmjlvGydr6YpNgEJzJrEd+AjHtnv4J+q/h f0NVVhnAEUkdBMUp1wMEfstsXKsXRlWbwMzv/k3DaO1AhyZ8zmoYWCSqJgSNIsZNMEuwCk X09iiFqb7cU+aFkAsLtY/eBl4GzMwayCpv3xpdEiS6n79Knu3fPBQhTxozj+wz/4M/IeMB m6IScmcwNGBP/+4qnkRt6Z1mlwfYCz+vyZ4t98hfMdzp8PILs6xQ1DBPEMFasA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789244; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=QZvmW79aSWaOCLeBfAMHsnSFDDjvmnR1BtBxPWfUF0o=; b=iJEnKXMJm8xIPDPMgynOSaA0BIlaEPHaDWzLBvhifC8kErPdix45lr1aVBuogeAQSSSR3r q5yggw1Or2DBViAQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 41/50] posix-timers: Embed sigqueue in struct k_itimer References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:24 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To cure the SIG_IGN handling for posix interval timers, the preallocated sigqueue needs to be embedded into struct k_itimer to prevent life time races of all sorts. Now that the prerequisites are in place, embed the sigqueue into struct k_itimer and fixup the relevant usage sites. Aside of preparing for proper SIG_IGN handling, this spares an extra allocation. Signed-off-by: Thomas Gleixner --- fs/proc/base.c | 4 +- include/linux/posix-timers.h | 23 +++++++++++++++- kernel/signal.c | 12 +++++++- kernel/time/posix-timers.c | 59 ++++++++++++++++++++++++++------------= ----- 4 files changed, 69 insertions(+), 29 deletions(-) --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2496,8 +2496,8 @@ static int show_timer(struct seq_file *m =20 seq_printf(m, "ID: %d\n", timer->it_id); seq_printf(m, "signal: %d/%px\n", - timer->sigq->info.si_signo, - timer->sigq->info.si_value.sival_ptr); + timer->sigq.info.si_signo, + timer->sigq.info.si_value.sival_ptr); seq_printf(m, "notify: %s/%s.%d\n", nstr[notify & ~SIGEV_THREAD_ID], (notify & SIGEV_THREAD_ID) ? "tid" : "pid", --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -39,6 +39,8 @@ static inline int clockid_to_fd(const cl =20 #ifdef CONFIG_POSIX_TIMERS =20 +#include + /** * cpu_timer - Posix CPU timer representation for k_itimer * @node: timerqueue node to queue in the task/sig @@ -165,7 +167,7 @@ static inline void posix_cputimers_init_ * @it_pid: The pid of the process/task targeted by the signal * @it_process: The task to wakeup on clock_nanosleep (CPU timers) * @rcuref: Reference count for life time management - * @sigq: Pointer to preallocated sigqueue + * @sigq: Embedded sigqueue * @it: Union representing the various posix timer type * internals. * @rcu: RCU head for freeing the timer. @@ -189,7 +191,7 @@ struct k_itimer { struct pid *it_pid; struct task_struct *it_process; }; - struct sigqueue *sigq; + struct sigqueue sigq; rcuref_t rcuref; union { struct { @@ -217,6 +219,23 @@ static inline void posixtimer_putref(str if (rcuref_put(&tmr->rcuref)) posixtimer_free_timer(tmr); } + +static inline void posixtimer_sigqueue_getref(struct sigqueue *q) +{ + struct k_itimer *tmr =3D container_of(q, struct k_itimer, sigq); + + WARN_ON_ONCE(!rcuref_get(&tmr->rcuref)); +} + +static inline void posixtimer_sigqueue_putref(struct sigqueue *q) +{ + struct k_itimer *tmr =3D container_of(q, struct k_itimer, sigq); + + posixtimer_putref(tmr); +} +#else /* CONFIG_POSIX_TIMERS */ +static inline void posixtimer_sigqueue_getref(struct sigqueue *q) { } +static inline void posixtimer_sigqueue_putref(struct sigqueue *q) { } #endif /* !CONFIG_POSIX_TIMERS */ =20 #endif --- a/kernel/signal.c +++ b/kernel/signal.c @@ -566,7 +566,12 @@ static void collect_signal(int sig, stru still_pending: list_del_init(&first->list); copy_siginfo(info, &first->info); - __sigqueue_free(first); + /* + * Do not drop the reference count for posix timer + * signals. That's done in posix_timer_deliver_signal(). + */ + if (info->si_code !=3D SI_TIMER) + __sigqueue_free(first); } else { /* * Ok, it wasn't in the queue. This must be @@ -1985,7 +1990,7 @@ static inline struct task_struct *posixt =20 int posixtimer_send_sigqueue(struct k_itimer *tmr) { - struct sigqueue *q =3D tmr->sigq; + struct sigqueue *q =3D &tmr->sigq; int sig =3D q->info.si_signo; struct task_struct *t; unsigned long flags; @@ -2045,9 +2050,12 @@ int posixtimer_send_sigqueue(struct k_it =20 ret =3D 0; if (unlikely(!list_empty(&q->list))) { + /* This holds a reference count already */ result =3D TRACE_SIGNAL_ALREADY_PENDING; goto out; } + + posixtimer_sigqueue_getref(q); posixtimer_queue_sigqueue(q, t, tmr->it_pid_type); result =3D TRACE_SIGNAL_DELIVERED; out: --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -251,12 +251,13 @@ static void common_hrtimer_rearm(struct =20 /* * This function is called from the signal delivery code. It decides - * whether the signal should be dropped and rearms interval timers. + * whether the signal should be dropped and rearms interval timers. The + * timer can be unconditionally accessed as there is a reference held on + * it. */ bool posixtimer_deliver_signal(struct kernel_siginfo *info) { - struct k_itimer *timr; - unsigned long flags; + struct k_itimer *timr =3D info->si_sys_privptr; bool ret =3D false; =20 /* @@ -264,12 +265,14 @@ bool posixtimer_deliver_signal(struct ke * timr::it_lock. Keep interrupts disabled. */ spin_unlock(¤t->sighand->siglock); + spin_lock(&timr->it_lock); =20 - timr =3D lock_timer(info->si_tid, &flags); - if (!timr) - goto out; - - if (timr->it_signal_seq !=3D info->si_sys_private) + /* + * Check if the timer is still alive or whether it got modified + * since the signal was queued. In either case, don't rearm and + * drop the signal. + */ + if (!timr->it_signal || timr->it_signal_seq !=3D info->si_sys_private) goto out_unlock; =20 if (timr->it_interval && timr->it_status =3D=3D POSIX_TIMER_REQUEUE_PENDI= NG) { @@ -285,8 +288,10 @@ bool posixtimer_deliver_signal(struct ke ret =3D true; =20 out_unlock: - unlock_timer(timr, flags); -out: + spin_unlock(&timr->it_lock); + /* Drop the reference which was acquired when the signal was queued */ + posixtimer_putref(timr); + spin_lock(¤t->sighand->siglock); =20 /* Don't expose the si_sys_priv* values to userspace */ @@ -405,17 +410,17 @@ static struct pid *good_sigevent(sigeven } } =20 -static struct k_itimer * alloc_posix_timer(void) +static struct k_itimer *alloc_posix_timer(void) { struct k_itimer *tmr =3D kmem_cache_zalloc(posix_timers_cache, GFP_KERNEL= ); =20 if (!tmr) return tmr; - if (unlikely(!(tmr->sigq =3D sigqueue_alloc()))) { + + if (unlikely(!posixtimer_init_sigqueue(&tmr->sigq))) { kmem_cache_free(posix_timers_cache, tmr); return NULL; } - clear_siginfo(&tmr->sigq->info); rcuref_init(&tmr->rcuref, 1); return tmr; } @@ -430,7 +435,8 @@ static void k_itimer_rcu_free(struct rcu void posixtimer_free_timer(struct k_itimer *tmr) { put_pid(tmr->it_pid); - sigqueue_free(tmr->sigq); + if (tmr->sigq.ucounts) + dec_rlimit_put_ucounts(tmr->sigq.ucounts, UCOUNT_RLIMIT_SIGPENDING); call_rcu(&tmr->rcu, k_itimer_rcu_free); } =20 @@ -492,13 +498,13 @@ static int do_timer_create(clockid_t whi goto out; } new_timer->it_sigev_notify =3D event->sigev_notify; - new_timer->sigq->info.si_signo =3D event->sigev_signo; - new_timer->sigq->info.si_value =3D event->sigev_value; + new_timer->sigq.info.si_signo =3D event->sigev_signo; + new_timer->sigq.info.si_value =3D event->sigev_value; } else { new_timer->it_sigev_notify =3D SIGEV_SIGNAL; - new_timer->sigq->info.si_signo =3D SIGALRM; - memset(&new_timer->sigq->info.si_value, 0, sizeof(sigval_t)); - new_timer->sigq->info.si_value.sival_int =3D new_timer->it_id; + new_timer->sigq.info.si_signo =3D SIGALRM; + memset(&new_timer->sigq.info.si_value, 0, sizeof(sigval_t)); + new_timer->sigq.info.si_value.sival_int =3D new_timer->it_id; new_timer->it_pid =3D get_pid(task_tgid(current)); } =20 @@ -507,9 +513,9 @@ static int do_timer_create(clockid_t whi else new_timer->it_pid_type =3D PIDTYPE_TGID; =20 - new_timer->sigq->info.si_tid =3D new_timer->it_id; - new_timer->sigq->info.si_code =3D SI_TIMER; - new_timer->sigq->info.si_sys_privptr =3D new_timer; + new_timer->sigq.info.si_tid =3D new_timer->it_id; + new_timer->sigq.info.si_code =3D SI_TIMER; + new_timer->sigq.info.si_sys_privptr =3D new_timer; =20 if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id)))= { error =3D -EFAULT; @@ -593,7 +599,14 @@ static struct k_itimer *__lock_timer(tim * 1) Set timr::it_signal to NULL with timr::it_lock held * 2) Release timr::it_lock * 3) Remove from the hash under hash_lock - * 4) Call RCU for removal after the grace period + * 4) Put the reference count. + * + * The reference count might not drop to zero if timr::sigq is + * queued. In that case the signal delivery or flush will put the + * last reference count. + * + * When the reference count reaches zero, the timer is scheduled + * for RCU removal after the grace period. * * Holding rcu_read_lock() accross the lookup ensures that * the timer cannot be freed. From nobody Fri Dec 19 20:14:14 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 1A40E40843 for ; Wed, 10 Apr 2024 22:47:28 +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=1712789249; cv=none; b=N1t2TwbMQtzbvDRycRx1VCgKPVacS8T2G8Z5qKpzMcOdMuX8x8XKVLIDqW5vjCAvX/8LV8PPOmwlml21hVATbtG8dV0UO4PZ0OG9Pu9IDN2d13KPZKLqHiNFeb9jlwXnkUUcykQVzlwODIUkef+Y1BeMs0ewfuHSIdaFpwOLJSs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789249; c=relaxed/simple; bh=NFKqTJ9ShtyiYAgy+AdG3Zbk7Do+0qFmxaGilWT4BiI=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=UKstWOUTEeGccqdFQTRyi0ME8knCmelYlGrWD0+x01+UI7sr7Nl1RerWulbG0O1nqfGf64+r9HSbVRm6qRbuI1kwrWCyblcEmV7jalIDWLvjHMAMDiD1pEFlO/eF+9lIQSAe8EPtYX2X97tP5bfgZr3ekKnYUA6oulvDdnszeF0= 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=rJvDVrTE; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qylp4H7x; 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="rJvDVrTE"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qylp4H7x" Message-ID: <20240410165553.575951796@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789246; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=DxPffv+7izSrJC7n0/k0QCEO3R4CfssBq8UJE3DdsrY=; b=rJvDVrTE+1ky0Yq0jWNVPhG+ArTg8A5L41Kqlyxk0d0qD6f9k6O8EsKOm+VYp+1XtOS1Ch WQx2Iudy9ve1/LrgseDKPxvsCYXRoLXdoJnBIav+XhFSCL2uB8cIm7Zs+uGnDj++hsvmK0 kpOz0WgEjpoWn5nTxUDlc3DTddxw6nBWUpUULlmPLS2+VYtJi8aFpm6Y4aEMHKDcDPqXQc 6s7lTpveHoJhj5EiTJs5sAgkl1WUZtetzQ5gvS+7JtWgtbv4rI5At3PszZ/Z+sWTwVCOjO sKAyj9yrowz5uA5mOH2OhdJKnT4iqbAS9ealmdx0S4A9I/Gdh/hsKZ6OmbqaOw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789246; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=DxPffv+7izSrJC7n0/k0QCEO3R4CfssBq8UJE3DdsrY=; b=qylp4H7xxjolIyCzjsq4MKgXTom4k1nfJrOwjGKjSI5UBPMIfyP1du3PNwwl6GwrGnr2EG Rqq9wBTx+UE8bVBg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 42/50] signal: Cleanup unused posix-timer leftovers References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:26 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Remove the leftovers of sigqueue preallocation as it's not longer used. Signed-off-by: Thomas Gleixner --- include/linux/sched/signal.h | 2 -- kernel/signal.c | 43 +++++++-------------------------------= ----- 2 files changed, 7 insertions(+), 38 deletions(-) --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -337,8 +337,6 @@ extern void force_fatal_sig(int); extern void force_exit_sig(int); extern int send_sig(int, struct task_struct *, int); extern int zap_other_threads(struct task_struct *p); -extern struct sigqueue *sigqueue_alloc(void); -extern void sigqueue_free(struct sigqueue *); extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *); =20 static inline void clear_notify_signal(void) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -439,8 +439,8 @@ static void __sigqueue_init(struct sigqu * - this may be called without locks if and only if t =3D=3D current, oth= erwise an * appropriate lock must be held to stop the target task from exiting */ -static struct sigqueue *__sigqueue_alloc(int sig, struct task_struct *t, g= fp_t gfp_flags, - int override_rlimit, const unsigned int sigqueue_flags) +static struct sigqueue *sigqueue_alloc(int sig, struct task_struct *t, gfp= _t gfp_flags, + int override_rlimit) { struct ucounts *ucounts =3D sig_get_ucounts(t, sig, override_rlimit); struct sigqueue *q; @@ -454,14 +454,16 @@ static struct sigqueue *__sigqueue_alloc return NULL; } =20 - __sigqueue_init(q, ucounts, sigqueue_flags); + __sigqueue_init(q, ucounts, 0); return q; } =20 static void __sigqueue_free(struct sigqueue *q) { - if (q->flags & SIGQUEUE_PREALLOC) + if (q->flags & SIGQUEUE_PREALLOC) { + posixtimer_sigqueue_putref(q); return; + } if (q->ucounts) { dec_rlimit_put_ucounts(q->ucounts, UCOUNT_RLIMIT_SIGPENDING); q->ucounts =3D NULL; @@ -1065,7 +1067,7 @@ static int __send_signal_locked(int sig, else override_rlimit =3D 0; =20 - q =3D __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit, 0); + q =3D sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit); =20 if (q) { list_add_tail(&q->list, &pending->list); @@ -1925,37 +1927,6 @@ bool posixtimer_init_sigqueue(struct sig return true; } =20 -struct sigqueue *sigqueue_alloc(void) -{ - return __sigqueue_alloc(-1, current, GFP_KERNEL, 0, SIGQUEUE_PREALLOC); -} - -void sigqueue_free(struct sigqueue *q) -{ - spinlock_t *lock =3D ¤t->sighand->siglock; - unsigned long flags; - - if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC))) - return; - /* - * We must hold ->siglock while testing q->list - * to serialize with collect_signal() or with - * __exit_signal()->flush_sigqueue(). - */ - spin_lock_irqsave(lock, flags); - q->flags &=3D ~SIGQUEUE_PREALLOC; - /* - * If it is queued it will be freed when dequeued, - * like the "regular" sigqueue. - */ - if (!list_empty(&q->list)) - q =3D NULL; - spin_unlock_irqrestore(lock, flags); - - if (q) - __sigqueue_free(q); -} - static void posixtimer_queue_sigqueue(struct sigqueue *q, struct task_stru= ct *t, enum pid_type type) { struct sigpending *pending; From nobody Fri Dec 19 20:14:14 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 E850A13248B for ; Wed, 10 Apr 2024 22:47:29 +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=1712789251; cv=none; b=BFZkKHGkgG48gpNev/rwJTF3/L4jymuaKNPtGxiRjpuR1bRahcUejMqMbb0BMnIPag7ybpFGSk6FVjQeujlanOpcNq1OGCuLI2YteJU4UnSo3S+PBc5sZP1zYgdRyBZVgqfp1YNbKbo4QOeDlMdqHqWtwwPFrLEDegoNKJUGCJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789251; c=relaxed/simple; bh=H8C4ae3qB9pJTAhYrEUW6UIkzelbDNrIyM/SxoxcYVc=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Ms2m+6btisAVZ5j2KUp1HijTjKVJRMdBV0SvHIu4mIl4K8YZo9vyxvJ/SJSJXfKilz79NcP7kVAy2e+98N0ukcCYTQBfB1lpcO1SjVl6KTnHjmmQYBJSkWqBsqNRmghYTSsaUU1B+dpBjrAie7F4lf5lbhNbgvZwPQ9gIUHcO2U= 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=SIHbGenp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lUXOrPoD; 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="SIHbGenp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lUXOrPoD" Message-ID: <20240410165553.636753630@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789248; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=gS0GunzjJF0mIhSZ9sJXDkEdhMOznUExWpmLyjVDd8s=; b=SIHbGenp76SOLVBs2dAVyy4fAMu+LZLTM/FfoCydWnvcOyeVcs0e2Ctnjy6Y3/oIYFsoHp mrVNV4oTcW8ky86GK0kmy7K3qFC9zlG46RxpYdZW8NlJGpp0ubRHiocrJXiC3Xn/y+gcFA OcBNNZhUfevnKD2S33f0OhFo3NQGgy7gRXVQJ+PF9CoTGszsQNp6eycRPcHTLDWik39P4g NRJoEPEguuLlfD5qgVkRTnhfqcUjJpyVvJ6RM/DOET2BEyFBEzBqOdvhP0oeEbF5ZqiOl8 eIalQiEhI3gvxVJspxs4rq+Vl9lEiJ77YGLjK+wpv98S5Hdnnzai8XsoCwcY7A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789248; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=gS0GunzjJF0mIhSZ9sJXDkEdhMOznUExWpmLyjVDd8s=; b=lUXOrPoD+iJthx03iSV1MRAA+nF/RxjZnoJ9yWD7k+5odTGsMjLbHg9bA3O3ehohvd+mkR 38HzHIdZlHcR+uDQ== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 43/50] signal: Add task argument to flush_sigqueue_mask() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:27 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To prepare for handling posix timer signals on sigaction(SIG_IGN) properly, add a task argument to flush_sigqueue_mask() and fixup all call sites. This argument will be used in a later step to enqueue posix timers on an ignored list, so their signal can be requeued when SIG_IGN is lifted later on. Signed-off-by: Thomas Gleixner --- kernel/signal.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -724,11 +724,10 @@ void signal_wake_up_state(struct task_st =20 /* * Remove signals in mask from the pending set and queue. - * Returns 1 if any signals were found. * * All callers must be holding the siglock. */ -static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s) +static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s, stru= ct task_struct *ptmr_tsk) { struct sigqueue *q, *n; sigset_t m; @@ -866,18 +865,18 @@ static bool prepare_signal(int sig, stru * This is a stop signal. Remove SIGCONT from all queues. */ siginitset(&flush, sigmask(SIGCONT)); - flush_sigqueue_mask(&flush, &signal->shared_pending); + flush_sigqueue_mask(&flush, &signal->shared_pending, NULL); for_each_thread(p, t) - flush_sigqueue_mask(&flush, &t->pending); + flush_sigqueue_mask(&flush, &t->pending, NULL); } else if (sig =3D=3D SIGCONT) { unsigned int why; /* * Remove all stop signals from all queues, wake all threads. */ siginitset(&flush, SIG_KERNEL_STOP_MASK); - flush_sigqueue_mask(&flush, &signal->shared_pending); + flush_sigqueue_mask(&flush, &signal->shared_pending, NULL); for_each_thread(p, t) { - flush_sigqueue_mask(&flush, &t->pending); + flush_sigqueue_mask(&flush, &t->pending, NULL); task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING); if (likely(!(t->ptrace & PT_SEIZED))) { t->jobctl &=3D ~JOBCTL_STOPPED; @@ -4155,8 +4154,8 @@ void kernel_sigaction(int sig, __sighand sigemptyset(&mask); sigaddset(&mask, sig); =20 - flush_sigqueue_mask(&mask, ¤t->signal->shared_pending); - flush_sigqueue_mask(&mask, ¤t->pending); + flush_sigqueue_mask(&mask, ¤t->signal->shared_pending, NULL); + flush_sigqueue_mask(&mask, ¤t->pending, NULL); recalc_sigpending(); } spin_unlock_irq(¤t->sighand->siglock); @@ -4223,9 +4222,9 @@ int do_sigaction(int sig, struct k_sigac if (sig_handler_ignored(sig_handler(p, sig), sig)) { sigemptyset(&mask); sigaddset(&mask, sig); - flush_sigqueue_mask(&mask, &p->signal->shared_pending); + flush_sigqueue_mask(&mask, &p->signal->shared_pending, NULL); for_each_thread(p, t) - flush_sigqueue_mask(&mask, &t->pending); + flush_sigqueue_mask(&mask, &t->pending, NULL); } } From nobody Fri Dec 19 20:14:14 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 C4C5A134723 for ; Wed, 10 Apr 2024 22:47:31 +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=1712789253; cv=none; b=NYGTWEzhInV97UzmW2cUqSizMmcwOm7Ftxn8Ay8raRoQHFRrAhs6jpRdANTQl9KJ+joXLYSnlFVekRJma42KkrU0r7UQdK5fkNq9vF3hID1qQRuaaynic78z6d7gM+4Qoa1EbxEyJTi8xl5zOkblTkfWhyQZhJxMWnIe36GPn3I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789253; c=relaxed/simple; bh=cp19dVUb7xvWUsokdlmPF5WkDY+X0uWwwuOAQ7Ty6Vc=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=rqdFF+64BWxz8sZKTJL5EU7XE9EcwPpgKYbJs09oq5DlMalq1mUBkUvU5CSkoM7rgFy5or7Z49Ub6HP5H6zWKyZp/f/qEJ+zQmP/QY6pGXkTerIa/1zOLQrDOKCmiDGY8nyS75pAg3W0whVl2MQZWJpsXN4Qqu59OVuGfBLzNnE= 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=jUaOE1Vx; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=65/gtTev; 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="jUaOE1Vx"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="65/gtTev" Message-ID: <20240410165553.699821482@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789250; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=pv6YrQN2ETIiH5UgbcE48m3AAv3y/nIIur52dGrER6A=; b=jUaOE1VxK0rkmAl5Q50vmOUaJIP24WYKpeBT9ld8uKfYKCb23U5uzyQHzO7obq6pBZIF8E HCe7glmNlBbc/ciYExpG/z4mY+mSsIW50zkbuloJTh05+/tBqqv1/x1lWnFdNbk/0WSGPn uesy6o6gv78OwT3V3QEML+cRKgxGyn8O+4IbBvMVVMpJXV+v8x3FBhYv05lXYqgXrvQ7bP neuztj+7bFE9QywjjIQqVgN5gAqS/fh2wCzQm7UT/MAoQUNv08UueRkiA4kWzpd6UeKicp l+AX/y8OODO+V/ayugdTQ54lX40+T6hoW0ap875xTgQWv73N1UK9267pEbsSnQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789250; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=pv6YrQN2ETIiH5UgbcE48m3AAv3y/nIIur52dGrER6A=; b=65/gtTev2nEhnOx3GTX9mzsFaXErSxtYWTmSWv4oAE7m3TOOcc+WKKiULC7QIuRY+KACcs gP9CJMyfsQ4zHBDw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 44/50] signal: Provide ignored_posix_timers list References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:29 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To prepare for handling posix timer signals on sigaction(SIG_IGN) properly, add a list to task::signal. This list will be used to queue posix timers so their signal can be requeued when SIG_IGN is lifted later. Signed-off-by: Thomas Gleixner --- include/linux/sched/signal.h | 1 + init/init_task.c | 5 +++-- kernel/fork.c | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -138,6 +138,7 @@ struct signal_struct { /* POSIX.1b Interval Timers */ unsigned int next_posix_timer_id; struct hlist_head posix_timers; + struct hlist_head ignored_posix_timers; =20 /* ITIMER_REAL timer for the process */ struct hrtimer real_timer; --- a/init/init_task.c +++ b/init/init_task.c @@ -29,8 +29,9 @@ static struct signal_struct init_signals .cred_guard_mutex =3D __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), .exec_update_lock =3D __RWSEM_INITIALIZER(init_signals.exec_update_lock), #ifdef CONFIG_POSIX_TIMERS - .posix_timers =3D HLIST_HEAD_INIT, - .cputimer =3D { + .posix_timers =3D HLIST_HEAD_INIT, + .ignored_posix_timers =3D HLIST_HEAD_INIT, + .cputimer =3D { .cputime_atomic =3D INIT_CPUTIME_ATOMIC, }, #endif --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1877,6 +1877,7 @@ static int copy_signal(unsigned long clo =20 #ifdef CONFIG_POSIX_TIMERS INIT_HLIST_HEAD(&sig->posix_timers); + INIT_HLIST_HEAD(&sig->ignored_posix_timers); hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); sig->real_timer.function =3D it_real_fn; #endif From nobody Fri Dec 19 20:14:14 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 A862E13664C for ; Wed, 10 Apr 2024 22:47:33 +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=1712789255; cv=none; b=k7QWF4dx9nrgHPp/RrkiFAY+CnvpbxXnhy/VQpWmXaRft3Zeykc6Ss+tPEFnLMkmNMODsXKBJLHD6UBqK2s4gyVI1yCghew1jtMuKdO6ZGrhvoZl2Nu7q515Xaafg9tSx9Y5jXdmhRinHneSfuvSrPKWYT+eJAsQpPsxsyTD7BE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789255; c=relaxed/simple; bh=S6JUDkyUPN6u68oQn5OUPewj7arp5+lzXuIlS/xwEHU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=KRm5m8Pa6hX02+cjQM6neVxa9vKSQoTdKKOSoPDIK5+AA3Foa6m5bvwNSRJYLUcOhRBGsSpEbnRfSCYaFM1Vt0+UtJI9DbG7emy7j+iygn89WOxD1AV5r8aIVgLeXKHASz1ezjmfiybnpIAy01JcQkOGO+18pqpnp1R3gdiULuI= 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=V9lct4Gz; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=N/o6o6fS; 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="V9lct4Gz"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="N/o6o6fS" Message-ID: <20240410165553.760259947@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789251; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=h6hDrXB0chXNrUH4sCnb+tErkyrDv3iJrXtwT3bPMl8=; b=V9lct4GzHZn49a1TrhvBCi/6RZvlkGjiUqQfEuE5lNaZvghRTrpIzvenCiD7ig0kKM3WfC k1dZcsPXInjzXGnhKR7qe5JYm8xcT0jvFX4Llt0HMLzjqT4LreIP+T7ERDAOvoO4OhwW7R YeivbXxQcO480iQNRn72IfVtPtzTT2Qi86jPyH77Zc8wI4Cc2wGqtctekvsryjC765djG9 13jd2kkk/L5MAosfOiLJt+v9SuimmAdqPfrhmDZtrMLjPCM1CTr5jroYGMfck9IBG3PmO2 xvKxaWeDu903OKKoW3UlnfoFHwpxJ3GAIIUVySK27cIzuiXAPK+2qzL0yWt23A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789251; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=h6hDrXB0chXNrUH4sCnb+tErkyrDv3iJrXtwT3bPMl8=; b=N/o6o6fSPXwuZzN6JKYJYD+vNEglvin24m4COI1zmV4PdYMYn2m1NhCYkmQSy781u4qMbZ pJrEW2hqqEqs0CDA== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 45/50] posix-timers: Handle ignored list on delete and exit References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:31 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To handle posix timer signals on sigaction(SIG_IGN) properly, the timers will be queued on a separate ignored list. Add the necessary cleanup code for timer_delete() and exit_itimers(). Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 4 +++- kernel/time/posix-timers.c | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -151,7 +151,8 @@ static inline void posix_cputimers_init_ =20 /** * struct k_itimer - POSIX.1b interval timer structure. - * @list: List head for binding the timer to signals->posix_timers + * @list: List node for binding the timer to tsk::signal::posix_timers + * @ignored_list: List node for tracking ignored timers in tsk::signal::ig= nored_posix_timers * @t_hash: Entry in the posix timer hash table * @it_lock: Lock protecting the timer * @kclock: Pointer to the k_clock struct handling this timer @@ -174,6 +175,7 @@ static inline void posix_cputimers_init_ */ struct k_itimer { struct hlist_node list; + struct hlist_node ignored_list; struct hlist_node t_hash; spinlock_t it_lock; const struct k_clock *kclock; --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1036,6 +1036,18 @@ int common_timer_del(struct k_itimer *ti return 0; } =20 +/* + * If the deleted timer is on the ignored list, remove it and + * drop the associated reference. + */ +static inline void posix_timer_cleanup_ignored(struct k_itimer *tmr) +{ + if (!hlist_unhashed(&tmr->ignored_list)) { + hlist_del_init(&tmr->ignored_list); + posixtimer_putref(tmr); + } +} + static inline int timer_delete_hook(struct k_itimer *timer) { const struct k_clock *kc =3D timer->kclock; @@ -1068,6 +1080,7 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t =20 spin_lock(¤t->sighand->siglock); hlist_del(&timer->list); + posix_timer_cleanup_ignored(timer); spin_unlock(¤t->sighand->siglock); /* * A concurrent lookup could check timer::it_signal lockless. It @@ -1119,6 +1132,8 @@ static void itimer_delete(struct k_itime } hlist_del(&timer->list); =20 + posix_timer_cleanup_ignored(timer); + /* * Setting timer::it_signal to NULL is technically not required * here as nothing can access the timer anymore legitimately via @@ -1151,6 +1166,11 @@ void exit_itimers(struct task_struct *ts /* The timers are not longer accessible via tsk::signal */ while (!hlist_empty(&timers)) itimer_delete(hlist_entry(timers.first, struct k_itimer, list)); + + /* Mop up timers which are on the ignored list */ + hlist_move_list(&tsk->signal->ignored_posix_timers, &timers); + while (!hlist_empty(&timers)) + posix_timer_cleanup_ignored(hlist_entry(timers.first, struct k_itimer, l= ist)); } =20 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, From nobody Fri Dec 19 20:14:14 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 2FE8E136E00 for ; Wed, 10 Apr 2024 22:47:35 +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=1712789256; cv=none; b=PINF3qYgDRnhJR0DL5QSj7zx+NFB53OBFKwwcE+xrDE8wUhg+2MaVJEBjnCX1iyzOS5QoPBToXAN+H4ZYv8aR6HQv/jF1gyMLaYcSh9K1SxPgaZFJk3JPfGMAJ/J6LCCg344Oeu7mb2AJCsJYt7i7ztMF5+x9OVEnx1WHDGPEoA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789256; c=relaxed/simple; bh=Kaitif/719xp2xVCsTipKGEhafvxHZw3eYIDLxrxhD4=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=tVbk0+cuU6+c0+jBQHC9ya4haCIh0LA/hSrzYbpTjV+Rv66ElnkvDNYWq7CHH+Q7pXu3VgctROKE/IVLZurr66Q16Z5XenbZaFqs64wPr6XsfJmWPoHoqCp7Ga353sixD0UlZm/epxstUk92JYE1+VdjfOhbjAlb53EGNjNTuR4= 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=wPQqvvBO; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=YhNl7uJX; 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="wPQqvvBO"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="YhNl7uJX" Message-ID: <20240410165553.820640735@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789253; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=TLf+HH0NPG+O40MqHfVvMTLhniRUMJJ6i8JJ174+iCU=; b=wPQqvvBOu/geE0SSLMNT8rSv5WiV+XmXZL+SewO9aQzpikSOothjioLtJqbS/WBSWwwF1b rUf/UzvgrzQg8B6KsjHCTQ9csFJ/DjxUGtvPCxI4Hy8BV/s61msp7GF0oJPK6OPXJa1f4f 4q2SVh6d4BCaBJBtvZVm0bEAy5tDmvA5z9zg3NXYBZhu/h2hRK7bBBqV61fmmrhuKugV5Z eVNQ/YY4mgHcn3Ls8tcd5FUZRGvwMB4F0P6uFYdKjO2KwKgszfLK3RacYR3+5i2Enn7V2E oXqln6OpykbL+HIX+iYERgTRQRzpuqLFkxNNnWnJx1LymRm/0TP2VBZqOqNPtg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789253; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=TLf+HH0NPG+O40MqHfVvMTLhniRUMJJ6i8JJ174+iCU=; b=YhNl7uJXnHg0KUvq8XAAfyDPEd8bbrLHrifvknMG2s3N+AoTMHhELDkkswXGxb95u8rQkX z2Nsj9uKn8d7HLCw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 46/50] signal: Handle ignored signals in do_sigaction(action != SIG_IGN) References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:33 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a real handler (including SIG_DFL) is installed for a signal, which had previously SIG_IGN set, then the list of ignored posix timers has to be checked for timers which are affected by this change. Add a list walk function which checks for the matching signal number and if found requeues the timers signal, so the timer is rearmed on signal delivery. Rearming the timer right away is not possible because that requires to drop sighand lock. No functional change as the counter part which queues the timers on the ignored list is still missing. Signed-off-by: Thomas Gleixner --- kernel/signal.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++= ++- 1 file changed, 53 insertions(+), 1 deletion(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -2035,7 +2035,55 @@ int posixtimer_send_sigqueue(struct k_it rcu_read_unlock(); return ret; } -#endif /* CONFIG_POSIX_TIMERS */ + +static void posixtimer_sig_unignore(struct task_struct *tsk, int sig) +{ + struct hlist_head *head =3D &tsk->signal->ignored_posix_timers; + struct hlist_node *tmp; + struct k_itimer *tmr; + + if (likely(hlist_empty(head))) + return; + + /* + * Rearming a timer with sighand lock held is not possible due to + * lock ordering vs. tmr::it_lock. Just stick the sigqueue back and + * let the signal delivery path deal with it whether it needs to be + * rearmed or not. This cannot be decided here w/o dropping sighand + * lock and creating a loop retry horror show. + */ + hlist_for_each_entry_safe(tmr, tmp , head, ignored_list) { + struct task_struct *target; + + /* + * tmr::sigq.info.si_signo is immutable, so accessing it + * without holding tmr::it_lock is safe. + */ + if (tmr->sigq.info.si_signo !=3D sig) + continue; + + hlist_del_init(&tmr->ignored_list); + + /* This should never happen and leaks a reference count */ + if (WARN_ON_ONCE(!list_empty(&tmr->sigq.list))) + continue; + + /* + * Get the target for the signal. If target is a thread and + * has exited by now, drop the reference count. + */ + rcu_read_lock(); + target =3D posixtimer_get_target(tmr); + if (target) + posixtimer_queue_sigqueue(&tmr->sigq, target, tmr->it_pid_type); + else + posixtimer_putref(tmr); + rcu_read_unlock(); + } +} +#else /* CONFIG_POSIX_TIMERS */ +static inline void posixtimer_sig_unignore(struct task_struct *tsk, int si= g) { } +#endif /* !CONFIG_POSIX_TIMERS */ =20 void do_notify_pidfd(struct task_struct *task) { @@ -4205,6 +4253,8 @@ int do_sigaction(int sig, struct k_sigac sigaction_compat_abi(act, oact); =20 if (act) { + bool was_ignored =3D k->sa.sa_handler =3D=3D SIG_IGN; + sigdelsetmask(&act->sa.sa_mask, sigmask(SIGKILL) | sigmask(SIGSTOP)); *k =3D *act; @@ -4225,6 +4275,8 @@ int do_sigaction(int sig, struct k_sigac flush_sigqueue_mask(&mask, &p->signal->shared_pending, NULL); for_each_thread(p, t) flush_sigqueue_mask(&mask, &t->pending, NULL); + } else if (was_ignored) { + posixtimer_sig_unignore(p, sig); } } From nobody Fri Dec 19 20:14:14 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 C927E137746 for ; Wed, 10 Apr 2024 22:47:36 +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=1712789258; cv=none; b=tW59qm3Q1H7c9qJvBMs1qfNShjPFy3NZWwlrHDwzlN55CyNfzMPMyLuLGJD/eGjlsnJibP3Na4s2YYFdIkb/YjZLf0zxob3yvC34vZjVYqxKWL76z4ynOyaE8Ldhu2tVz8pvcVrnsv2RHA6lSuGcqVAvYdpGGAwvOv8pSYqt7Iw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789258; c=relaxed/simple; bh=ojSHAmzDSUCtV20tG0Eic6qt8OMUSGLWdNQCp73/DO0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=CFYeSx/Y0L/tuiAkMmPx4tmFtgFMzc5XV63U9xWqI0FEyk5ysu413aEbeAPAclJDfJFvfo/kfGHvcH2fmZ3UUFZ9K03Q/ezue+fOEsJv3qq7pD+2yrY28koXVv0tJKjPX7aXDIz7suTdAQi9hjm9gcbEsezE+YftPWoYMDJaozs= 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=ZULKCBP7; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=A+aDrg01; 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="ZULKCBP7"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="A+aDrg01" Message-ID: <20240410165553.882049383@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789255; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=bzEboz8AUjwt0LmBupDNjtajHSCekoKcw08hOKXcNgs=; b=ZULKCBP76zpp9/bl0KZoilv8DFZ/NLPLD4CS6xtmVCfo5zqztBAhbPUwmCNzkKCA4xc8/m wyIHhJQPtDu+O5+eM+r64YmfrKgXlk38qIVp2dqTTtQVC83g/RtNo8GlIq6nvRrW05KKTe 5oqbnZmdGqqBmdOvhqMCsxd4HUH8IjgRehupAvb+ZbIu+emUdznD7vK9ZlvglgTs4E9ZFe xCV0rORN7i/lVC6yIohKT+1N7TV+/CgNxbIq44K4Ad65sNP/Tus6ETKYWI0eVe1X9Hfc79 JC+80h1LZJVEr9/2mH6e1dW+iVVyvI9tmYOlD55IE5xpCuP2VNlLPTLnpydpAw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789255; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=bzEboz8AUjwt0LmBupDNjtajHSCekoKcw08hOKXcNgs=; b=A+aDrg01PivhTTdDSGJ0HCSDxhdjuor8i5KU9ixzsV0uwoO8VO/Zr4oM6XTWohXKMPl/PL bWV/A8Rp5J2tZZDw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 47/50] signal: Queue ignored posixtimers on ignore list References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:34 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Queue posixtimers which have their signal ignored on the ignored list: 1) When the timer fires and the signal has SIG_IGN set 2) When SIG_IGN is installed via sigaction() and a timer signal is queued This completes the SIG_IGN handling and such timers are not longer self rearmed which avoids pointless wakeups. Signed-off-by: Thomas Gleixner --- kernel/signal.c | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -722,6 +722,16 @@ void signal_wake_up_state(struct task_st kick_process(t); } =20 +static inline void posixtimer_sig_ignore(struct task_struct *tsk, struct s= igqueue *q); + +static void sigqueue_free_ignored(struct task_struct *ptmr_tsk, struct sig= queue *q) +{ + if (likely(!ptmr_tsk || q->info.si_code !=3D SI_TIMER)) + __sigqueue_free(q); + else + posixtimer_sig_ignore(ptmr_tsk, q); +} + /* * Remove signals in mask from the pending set and queue. * @@ -740,7 +750,7 @@ static void flush_sigqueue_mask(sigset_t list_for_each_entry_safe(q, n, &s->list, list) { if (sigismember(mask, q->info.si_signo)) { list_del_init(&q->list); - __sigqueue_free(q); + sigqueue_free_ignored(ptmr_tsk, q); } } } @@ -1964,9 +1974,8 @@ int posixtimer_send_sigqueue(struct k_it int sig =3D q->info.si_signo; struct task_struct *t; unsigned long flags; - int ret, result; + int result; =20 - ret =3D -1; rcu_read_lock(); =20 t =3D posixtimer_get_target(tmr); @@ -2012,13 +2021,24 @@ int posixtimer_send_sigqueue(struct k_it */ q->info.si_overrun =3D 0; =20 - ret =3D 1; /* the signal is ignored */ if (!prepare_signal(sig, t, false)) { result =3D TRACE_SIGNAL_IGNORED; + + /* Paranoia check. Try to survive. */ + if (WARN_ON_ONCE(!list_empty(&q->list))) + goto out; + + if (hlist_unhashed(&tmr->ignored_list)) { + hlist_add_head(&tmr->ignored_list, &t->signal->ignored_posix_timers); + posixtimer_sigqueue_getref(q); + } goto out; } =20 - ret =3D 0; + /* This should never happen and leaks a reference count */ + if (WARN_ON_ONCE(!hlist_unhashed(&tmr->ignored_list))) + hlist_del_init(&tmr->ignored_list); + if (unlikely(!list_empty(&q->list))) { /* This holds a reference count already */ result =3D TRACE_SIGNAL_ALREADY_PENDING; @@ -2033,7 +2053,14 @@ int posixtimer_send_sigqueue(struct k_it unlock_task_sighand(t, &flags); ret: rcu_read_unlock(); - return ret; + return 0; +} + +static inline void posixtimer_sig_ignore(struct task_struct *tsk, struct s= igqueue *q) +{ + struct k_itimer *tmr =3D container_of(q, struct k_itimer, sigq); + + hlist_add_head(&tmr->ignored_list, &tsk->signal->ignored_posix_timers); } =20 static void posixtimer_sig_unignore(struct task_struct *tsk, int sig) @@ -2082,6 +2109,7 @@ static void posixtimer_sig_unignore(stru } } #else /* CONFIG_POSIX_TIMERS */ +static inline void posixtimer_sig_ignore(struct task_struct *tsk, struct s= igqueue *q) { } static inline void posixtimer_sig_unignore(struct task_struct *tsk, int si= g) { } #endif /* !CONFIG_POSIX_TIMERS */ =20 @@ -4272,9 +4300,9 @@ int do_sigaction(int sig, struct k_sigac if (sig_handler_ignored(sig_handler(p, sig), sig)) { sigemptyset(&mask); sigaddset(&mask, sig); - flush_sigqueue_mask(&mask, &p->signal->shared_pending, NULL); + flush_sigqueue_mask(&mask, &p->signal->shared_pending, p); for_each_thread(p, t) - flush_sigqueue_mask(&mask, &t->pending, NULL); + flush_sigqueue_mask(&mask, &t->pending, p); } else if (was_ignored) { posixtimer_sig_unignore(p, sig); } From nobody Fri Dec 19 20:14:14 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 06875137777 for ; Wed, 10 Apr 2024 22:47: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=1712789260; cv=none; b=CuQkwl85ENoE8/Er8ULSm//ij4Qvid3TKH9os8NJlk+4gFVXq7z013F7pbhTbur7KGaML+judeEN8KiKDN3HXxw8K3pbdTCPzkrbISo+2JIafWkj9vP01jtuhw+oEVGTi1jhJtLCdG3D43nOndVyWxkTMekm2tVy0GB1aSWRu+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789260; c=relaxed/simple; bh=6+oMP4yhaB9snN9L9lq4a4BLEyD2V/UbAwBhcb8bH04=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=g/v8Oufdg0r8FTqxRA66vF36U0odAoOdDO1qEqWvGW3uAu/LgTpYTsmozCRm+nz9EV4Eydwpxh5sJNRLbfjiBpcQ6AufJIk7n8vVyGYfd5wMijjh19wwy/3Wr6C9VmtXZ55S53/DY6PedSDck8FHzpR+NllJ2UW7YQ4RidMwkS0= 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=WtaJMplz; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=MPTfS2Uj; 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="WtaJMplz"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="MPTfS2Uj" Message-ID: <20240410165553.944340294@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789257; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=kAkqE4xY1tktojAkL7ohXROJF3Nq4zDNsVY2SgjGpGc=; b=WtaJMplzdx7bJXkeFHMbPAf76h1SeUpmsI11TmRfyPLfEYUY7faWJh3xSbrGMjxPix6Mqt U6b3oRqKFVWkd7umtHxX8fX1baV5H0/LJnZXdhYG8Aa5mCQTCDx9VFowzA5vUQXK1RfHCC sJ1gJMYsARzinQpHzcP3vCUl/lmrgEcxn7UPHgfCBJiDsxdQ+G7TP8Y0mQouAzsNzezkvk ScawoVFH3qpV2F54GoxG8UC867hg5thGhnWfceNroM/avu1S7StLo/gIjNh2bPtoa1HlkB bDNn859O+5lTxWkV1h9ygj9f4wcB2QmZZ2SAIbKzxkEGXjVKg+hPu3jdF59Gcg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789257; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=kAkqE4xY1tktojAkL7ohXROJF3Nq4zDNsVY2SgjGpGc=; b=MPTfS2UjRZCPYADveZKuYYz6s3QqCzNS3eD1D/mgQV8kzwUFYDmbCP1gZxj6frL35rJfId BSvYoSDIz4JjpFDw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 48/50] posix-timers: Cleanup SIG_IGN workaround leftovers References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:36 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that ignored posix timer signals are requeued and the timers are rearmed on signal delivery the workaround to keep such timers alive and self rearm them is not longer required. Remove the relevant hacks and the not longer required return values from the related functions. The alarm timer workarounds will be cleaned up in a separate step. Signed-off-by: Thomas Gleixner --- include/linux/posix-timers.h | 2 - kernel/signal.c | 3 - kernel/time/alarmtimer.c | 37 +++--------------------- kernel/time/posix-cpu-timers.c | 18 ++--------- kernel/time/posix-timers.c | 63 +++---------------------------------= ----- kernel/time/posix-timers.h | 2 - 6 files changed, 18 insertions(+), 107 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -111,7 +111,7 @@ static inline void posix_cputimers_rt_wa =20 void posixtimer_rearm_itimer(struct task_struct *p); bool posixtimer_init_sigqueue(struct sigqueue *q); -int posixtimer_send_sigqueue(struct k_itimer *tmr); +void posixtimer_send_sigqueue(struct k_itimer *tmr); bool posixtimer_deliver_signal(struct kernel_siginfo *info); void posixtimer_free_timer(struct k_itimer *timer); =20 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1968,7 +1968,7 @@ static inline struct task_struct *posixt return t; } =20 -int posixtimer_send_sigqueue(struct k_itimer *tmr) +void posixtimer_send_sigqueue(struct k_itimer *tmr) { struct sigqueue *q =3D &tmr->sigq; int sig =3D q->info.si_signo; @@ -2053,7 +2053,6 @@ int posixtimer_send_sigqueue(struct k_it unlock_task_sighand(t, &flags); ret: rcu_read_unlock(); - return 0; } =20 static inline void posixtimer_sig_ignore(struct task_struct *tsk, struct s= igqueue *q) --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -198,27 +198,16 @@ static enum hrtimer_restart alarmtimer_f struct alarm *alarm =3D container_of(timer, struct alarm, timer); struct alarm_base *base =3D &alarm_bases[alarm->type]; unsigned long flags; - int ret =3D HRTIMER_NORESTART; - int restart =3D ALARMTIMER_NORESTART; =20 spin_lock_irqsave(&base->lock, flags); alarmtimer_dequeue(base, alarm); spin_unlock_irqrestore(&base->lock, flags); =20 if (alarm->function) - restart =3D alarm->function(alarm, base->get_ktime()); - - spin_lock_irqsave(&base->lock, flags); - if (restart !=3D ALARMTIMER_NORESTART) { - hrtimer_set_expires(&alarm->timer, alarm->node.expires); - alarmtimer_enqueue(base, alarm); - ret =3D HRTIMER_RESTART; - } - spin_unlock_irqrestore(&base->lock, flags); + alarm->function(alarm, base->get_ktime()); =20 trace_alarmtimer_fired(alarm, base->get_ktime()); - return ret; - + return HRTIMER_NORESTART; } =20 ktime_t alarm_expires_remaining(const struct alarm *alarm) @@ -567,30 +556,16 @@ static enum alarmtimer_type clock2alarm( * * Return: whether the timer is to be restarted */ -static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, - ktime_t now) +static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, kti= me_t now) { - struct k_itimer *ptr =3D container_of(alarm, struct k_itimer, - it.alarm.alarmtimer); - enum alarmtimer_restart result =3D ALARMTIMER_NORESTART; + struct k_itimer *ptr =3D container_of(alarm, struct k_itimer, it.alarm.al= armtimer); unsigned long flags; =20 spin_lock_irqsave(&ptr->it_lock, flags); - - if (posix_timer_queue_signal(ptr) && ptr->it_interval) { - /* - * Handle ignored signals and rearm the timer. This will go - * away once we handle ignored signals proper. Ensure that - * small intervals cannot starve the system. - */ - ptr->it_overrun +=3D __alarm_forward_now(alarm, ptr->it_interval, true); - ++ptr->it_signal_seq; - ptr->it_status =3D POSIX_TIMER_ARMED; - result =3D ALARMTIMER_RESTART; - } + posix_timer_queue_signal(ptr); spin_unlock_irqrestore(&ptr->it_lock, flags); =20 - return result; + return ALARMTIMER_NORESTART; } =20 /** --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -595,21 +595,11 @@ static void cpu_timer_fire(struct k_itim */ wake_up_process(timer->it_process); cpu_timer_setexpires(ctmr, 0); - } else if (!timer->it_interval) { - /* - * One-shot timer. Clear it as soon as it's fired. - */ + } else { posix_timer_queue_signal(timer); - cpu_timer_setexpires(ctmr, 0); - } else if (posix_timer_queue_signal(timer)) { - /* - * The signal did not get queued because the signal - * was ignored, so we won't get any callback to - * reload the timer. But we need to keep it - * ticking in case the signal is deliverable next time. - */ - posix_cpu_timer_rearm(timer); - ++timer->it_signal_seq; + /* Disable oneshot timers */ + if (!timer->it_interval) + cpu_timer_setexpires(ctmr, 0); } } =20 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -300,10 +300,9 @@ bool posixtimer_deliver_signal(struct ke return ret; } =20 -int posix_timer_queue_signal(struct k_itimer *timr) +void posix_timer_queue_signal(struct k_itimer *timr) { enum posix_timer_state state =3D POSIX_TIMER_DISARMED; - int ret; =20 lockdep_assert_held(&timr->it_lock); =20 @@ -313,9 +312,7 @@ int posix_timer_queue_signal(struct k_it } timr->it_status =3D state; =20 - ret =3D posixtimer_send_sigqueue(timr); - /* If we failed to send the signal the timer stops. */ - return ret > 0; + posixtimer_send_sigqueue(timr); } =20 /* @@ -328,62 +325,12 @@ int posix_timer_queue_signal(struct k_it static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) { struct k_itimer *timr =3D container_of(timer, struct k_itimer, it.real.ti= mer); - enum hrtimer_restart ret =3D HRTIMER_NORESTART; unsigned long flags; =20 spin_lock_irqsave(&timr->it_lock, flags); - - if (posix_timer_queue_signal(timr)) { - /* - * The signal was not queued due to SIG_IGN. As a - * consequence the timer is not going to be rearmed from - * the signal delivery path. But as a real signal handler - * can be installed later the timer must be rearmed here. - */ - if (timr->it_interval !=3D 0) { - ktime_t now =3D hrtimer_cb_get_time(timer); - - /* - * FIXME: What we really want, is to stop this - * timer completely and restart it in case the - * SIG_IGN is removed. This is a non trivial - * change to the signal handling code. - * - * For now let timers with an interval less than a - * jiffie expire every jiffie and recheck for a - * valid signal handler. - * - * This avoids interrupt starvation in case of a - * very small interval, which would expire the - * timer immediately again. - * - * Moving now ahead of time by one jiffie tricks - * hrtimer_forward() to expire the timer later, - * while it still maintains the overrun accuracy - * for the price of a slight inconsistency in the - * timer_gettime() case. This is at least better - * than a timer storm. - * - * Only required when high resolution timers are - * enabled as the periodic tick based timers are - * automatically aligned to the next tick. - */ - if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS)) { - ktime_t kj =3D TICK_NSEC; - - if (timr->it_interval < kj) - now =3D ktime_add(now, kj); - } - - timr->it_overrun +=3D hrtimer_forward(timer, now, timr->it_interval); - ret =3D HRTIMER_RESTART; - ++timr->it_signal_seq; - timr->it_status =3D POSIX_TIMER_ARMED; - } - } - - unlock_timer(timr, flags); - return ret; + posix_timer_queue_signal(timr); + spin_unlock_irqrestore(&timr->it_lock, flags); + return HRTIMER_NORESTART; } =20 static struct pid *good_sigevent(sigevent_t * event) --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -42,7 +42,7 @@ extern const struct k_clock clock_proces extern const struct k_clock clock_thread; extern const struct k_clock alarm_clock; =20 -int posix_timer_queue_signal(struct k_itimer *timr); +void posix_timer_queue_signal(struct k_itimer *timr); =20 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_sett= ing); int common_timer_set(struct k_itimer *timr, int flags, From nobody Fri Dec 19 20:14:14 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 6ACA2137920 for ; Wed, 10 Apr 2024 22:47:41 +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=1712789262; cv=none; b=EmpG5TwvvTBPak7G7nli3OfP0HWI8a+RKX/stGbnew4I6d3btBRKbmrE340hsTIIon/AkYDj48r6z1/X6lVZOUbkvlw/R/vp7P5eR9eLXJfnI9Lo+kCtUfH61JiH2pEdM0WoGv/eWetga50J8FI+R5pj3oMZ+BjarJoUr+iwyks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789262; c=relaxed/simple; bh=dVOMH7eiDEHVePUyZa4h1ExK1Rjvcqco8G9d9SW/qIU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=gmAVOXhulHbyYCUQbTCkNwihjluPXmnCeD0//7dGIDBlVKSsRIiDwtNv9Y++JDVWp/Brj4uPZt3YPKKe+6Z8ao9ZUIHjfur86AMLReqivygsEZdbMKAchZLJiSHAvOCkPnEfrxYvHMY91VOhLGoj8A94fvp2M1XaRCqf2a1n/IY= 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=YZJxQh1d; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=U0MpMRws; 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="YZJxQh1d"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="U0MpMRws" Message-ID: <20240410165554.006264248@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789259; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=HdIXDmhsPUZ2RPLujxzrsxlJ5xE23RHzUivxcwc9L5U=; b=YZJxQh1d7Qif5pxOkRNipZyUppU6m7sWLta6DG+n/0pZ/d3DDBme6w9JKblkxesr2xKRAe NDaUMFmXYwQ9k99YKrz5ayXJsIr9vM4nVNU+Lv3v9p61poxL0/miinJE1MJQfRblI5ZyqN wcjBBnQYDjmBwfv2r6tUVY5XSmGXsrolH68Uj6Jz7l4ZquTNpim7Syda1dmz3LZ9KNni8a hGX+OomHVlNmRnTnMixD78g8r6fkWXWyD1AUGmCHQIHqWjMxOImsSYyjnrgPUQA6On6lD+ 9Py09DDHUzPQMnf1p+2VUU+b9TtySj8Rw+QN2Rr8u753N1WQHpr8KLL7zFIu2A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789259; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=HdIXDmhsPUZ2RPLujxzrsxlJ5xE23RHzUivxcwc9L5U=; b=U0MpMRwsQqLODerCJMW3g9iewUoFzUAL4LK7s/hdDaIgFg7aH5IFT4KJfnF94R40zLByef hPYWncPcq/ud5NCw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 49/50] alarmtimers: Remove the throttle mechanism from alarm_forward_now() References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:38 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that ignored posix timer signals are requeued and the timers are rearmed on signal delivery the workaround to keep such timers alive and self rearm them is not longer required. Remove the unused alarm timer parts. Signed-off-by: Thomas Gleixner --- kernel/time/alarmtimer.c | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -469,35 +469,11 @@ u64 alarm_forward(struct alarm *alarm, k } EXPORT_SYMBOL_GPL(alarm_forward); =20 -static u64 __alarm_forward_now(struct alarm *alarm, ktime_t interval, bool= throttle) +u64 alarm_forward_now(struct alarm *alarm, ktime_t interval) { struct alarm_base *base =3D &alarm_bases[alarm->type]; - ktime_t now =3D base->get_ktime(); - - if (IS_ENABLED(CONFIG_HIGH_RES_TIMERS) && throttle) { - /* - * Same issue as with posix_timer_fn(). Timers which are - * periodic but the signal is ignored can starve the system - * with a very small interval. The real fix which was - * promised in the context of posix_timer_fn() never - * materialized, but someone should really work on it. - * - * To prevent DOS fake @now to be 1 jiffie out which keeps - * the overrun accounting correct but creates an - * inconsistency vs. timer_gettime(2). - */ - ktime_t kj =3D NSEC_PER_SEC / HZ; =20 - if (interval < kj) - now =3D ktime_add(now, kj); - } - - return alarm_forward(alarm, now, interval); -} - -u64 alarm_forward_now(struct alarm *alarm, ktime_t interval) -{ - return __alarm_forward_now(alarm, interval, false); + return alarm_forward(alarm, base->get_ktime(), interval); } EXPORT_SYMBOL_GPL(alarm_forward_now); From nobody Fri Dec 19 20:14:14 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 E4BA4137929 for ; Wed, 10 Apr 2024 22:47:42 +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=1712789264; cv=none; b=pyIagEYED+KK9wgrcbFlI3gwON0ApV66hfncY+7t9d7/VCpu5CzFKa5hf1aAhO/yXo8pbm5+awkhV+iXkhfisyBlKWKibGMBXsdQ9I4BFYo6qfEP9BBpoZsxCJ3YBVk/7rrlXAICFCqdhf9IidOJK+Py5dSZWjlACb3DEAVT10Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789264; c=relaxed/simple; bh=VFiMQKyOoL+mDJlBq/h+g4Nftt1Jj5QgaCKxsBJg4zs=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=TTrZSsoZxjf60oACRgqfTw/fGRwzVg6+ckJiDQ0QTthau2KyISehLDdyMeLLWWExwuiZ5ZUe+xzb50y4/hlj7xeYBpok6CjsLTIDEwF75JCYNR3UnNOYmfldPdWQ697WARNw/8J/yvFXQafGT+nZ5cpugQ6MSXdETOJvCBls97s= 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=QFULevm3; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=aziXDweo; 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="QFULevm3"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="aziXDweo" Message-ID: <20240410165554.066922956@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789260; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=a/VQUBOvvL1gBabTFPPMD8zLuxnFJjH7wVLI37j0UKo=; b=QFULevm3pNPVq4sPFoNtlQtIfeIT5kELxrpiIbqmHIBtAuuNDoguX9N/kW1E/j7+QpmJ5I 3xbeEQqy2Is44x8XoJFCOsfd4vIib4NL8oc3lSM1wl80p8F/hELozUhtzZoFAa7cek4oJ9 GihByQS7S83cZy6S6J9sQ9KKFFcQSA4FWmOeAugr1RUTY1m2K57Z91US1gXhxCs3cjryzZ xAqhgh8S5x1jiDmbuXwdNq9URVmiUWMahkoKA15PQuuQydwE1Ej6wrwTv4GrSdH2suyqAX 6YZGbJ6p+gRQ5r2a1d+vHtCQzggsbxCoCCUiokSUorZw9vDtNQz054Qe8vRMKQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789260; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=a/VQUBOvvL1gBabTFPPMD8zLuxnFJjH7wVLI37j0UKo=; b=aziXDweoIOwMqaw0tWIwt++4bi5PYaXdweiclWXHrTRfFb483DbfoOltmPOKNjmlaq+msJ m2+eZ5dSBlWyvMAg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 50/50] alarmtimers: Remove return value from alarm functions References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Thu, 11 Apr 2024 00:47:40 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that the SIG_IGN problem is solved in the core code, the alarmtimer callbacks do not require a return value anymore. Signed-off-by: Thomas Gleixner --- drivers/power/supply/charger-manager.c | 3 +-- fs/timerfd.c | 4 +--- include/linux/alarmtimer.h | 10 ++-------- kernel/time/alarmtimer.c | 16 +++++----------- net/netfilter/xt_IDLETIMER.c | 4 +--- 5 files changed, 10 insertions(+), 27 deletions(-) --- a/drivers/power/supply/charger-manager.c +++ b/drivers/power/supply/charger-manager.c @@ -1412,10 +1412,9 @@ static inline struct charger_desc *cm_ge return dev_get_platdata(&pdev->dev); } =20 -static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t = now) +static void cm_timer_func(struct alarm *alarm, ktime_t now) { cm_timer_set =3D false; - return ALARMTIMER_NORESTART; } =20 static int charger_manager_probe(struct platform_device *pdev) --- a/fs/timerfd.c +++ b/fs/timerfd.c @@ -79,13 +79,11 @@ static enum hrtimer_restart timerfd_tmrp return HRTIMER_NORESTART; } =20 -static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm, - ktime_t now) +static void timerfd_alarmproc(struct alarm *alarm, ktime_t now) { struct timerfd_ctx *ctx =3D container_of(alarm, struct timerfd_ctx, t.alarm); timerfd_triggered(ctx); - return ALARMTIMER_NORESTART; } =20 /* --- a/include/linux/alarmtimer.h +++ b/include/linux/alarmtimer.h @@ -20,12 +20,6 @@ enum alarmtimer_type { ALARM_BOOTTIME_FREEZER, }; =20 -enum alarmtimer_restart { - ALARMTIMER_NORESTART, - ALARMTIMER_RESTART, -}; - - #define ALARMTIMER_STATE_INACTIVE 0x00 #define ALARMTIMER_STATE_ENQUEUED 0x01 =20 @@ -42,14 +36,14 @@ enum alarmtimer_restart { struct alarm { struct timerqueue_node node; struct hrtimer timer; - enum alarmtimer_restart (*function)(struct alarm *, ktime_t now); + void (*function)(struct alarm *, ktime_t now); enum alarmtimer_type type; int state; void *data; }; =20 void alarm_init(struct alarm *alarm, enum alarmtimer_type type, - enum alarmtimer_restart (*function)(struct alarm *, ktime_t)); + void (*function)(struct alarm *, ktime_t)); void alarm_start(struct alarm *alarm, ktime_t start); void alarm_start_relative(struct alarm *alarm, ktime_t start); void alarm_restart(struct alarm *alarm); --- a/kernel/time/alarmtimer.c +++ b/kernel/time/alarmtimer.c @@ -323,7 +323,7 @@ static int alarmtimer_resume(struct devi =20 static void __alarm_init(struct alarm *alarm, enum alarmtimer_type type, - enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) + void (*function)(struct alarm *, ktime_t)) { timerqueue_init(&alarm->node); alarm->timer.function =3D alarmtimer_fired; @@ -339,7 +339,7 @@ static void * @function: callback that is run when the alarm fires */ void alarm_init(struct alarm *alarm, enum alarmtimer_type type, - enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) + void (*function)(struct alarm *, ktime_t)) { hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid, HRTIMER_MODE_ABS); @@ -532,7 +532,7 @@ static enum alarmtimer_type clock2alarm( * * Return: whether the timer is to be restarted */ -static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm, kti= me_t now) +static void alarm_handle_timer(struct alarm *alarm, ktime_t now) { struct k_itimer *ptr =3D container_of(alarm, struct k_itimer, it.alarm.al= armtimer); unsigned long flags; @@ -540,8 +540,6 @@ static enum alarmtimer_restart alarm_han spin_lock_irqsave(&ptr->it_lock, flags); posix_timer_queue_signal(ptr); spin_unlock_irqrestore(&ptr->it_lock, flags); - - return ALARMTIMER_NORESTART; } =20 /** @@ -702,18 +700,14 @@ static int alarm_timer_create(struct k_i * @now: time at the timer expiration * * Wakes up the task that set the alarmtimer - * - * Return: ALARMTIMER_NORESTART */ -static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alar= m, - ktime_t now) +static void alarmtimer_nsleep_wakeup(struct alarm *alarm, ktime_t now) { struct task_struct *task =3D alarm->data; =20 alarm->data =3D NULL; if (task) wake_up_process(task); - return ALARMTIMER_NORESTART; } =20 /** @@ -765,7 +759,7 @@ static int alarmtimer_do_nsleep(struct a =20 static void alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type, - enum alarmtimer_restart (*function)(struct alarm *, ktime_t)) + void (*function)(struct alarm *, ktime_t)) { hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid, HRTIMER_MODE_ABS); --- a/net/netfilter/xt_IDLETIMER.c +++ b/net/netfilter/xt_IDLETIMER.c @@ -107,14 +107,12 @@ static void idletimer_tg_expired(struct schedule_work(&timer->work); } =20 -static enum alarmtimer_restart idletimer_tg_alarmproc(struct alarm *alarm, - ktime_t now) +static void idletimer_tg_alarmproc(struct alarm *alarm, ktime_t now) { struct idletimer_tg *timer =3D alarm->data; =20 pr_debug("alarm %s expired\n", timer->attr.attr.name); schedule_work(&timer->work); - return ALARMTIMER_NORESTART; } =20 static int idletimer_check_sysfs_name(const char *name, unsigned int size) From nobody Fri Dec 19 20:14:14 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 4D9081FA1 for ; Mon, 15 Apr 2024 12:30:54 +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=1713184256; cv=none; b=Gyf1Rst6XJFxK1RQl+eBXGMgb//L1juXY+Ab7cAIQoywQwk7VMqpkHB8h/MY7Yy5cud6t6OePP0gLgYAXDtJipFi0f5jvAcWXwo1wJ+d87PVFw3KvXGkIGSwn2h7mFR6+HwOlP45fRjvsLD5f+AqRZ/D9e3yHb5KZjww7Ewz/Zs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713184256; c=relaxed/simple; bh=16igbDBxYxvU1x661QKxvfTSCMQD0ch6OYA44oQy3vM=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=CaiiWqwgwhsuXLmw7m2nxoNJIy3SygCUY/Tgg6KAMJROMobZ6OKFl9uM9ALPaXP1pBguJSeIW+TvSgVcVuL1olZ+CfW4PQVu44TysYxYEOT0Ou1FLq/ruXo0mcYIIF17BDWBvMi0+6sORxNAEUOtj9pd44TBtRl/iuG7LjLEM3M= 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=IA2k3c0V; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TUgzW5rl; 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="IA2k3c0V"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TUgzW5rl" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1713184246; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=31UKcuC29fhH2gQ7AFdErWb9xDpB3f6MYdu5CdzUIIQ=; b=IA2k3c0VmYV/3qlHKG/q0IL16+qVecLW1kmkRnzkF8ZJQyMSpEQHQ03AhC+Qrf1qersnMn OjmfGotp7DvX/pNtpnTxEqe0380WqVjMynm/cZit7Arlx3x4TlJP/BxcJxtKp8qr36bBgc fPV/88gmDf3HBPqCmi0KZEYUYVvV0FicAHPTpDUyqsTDGXUCSZVB2G6bqekpvOmZKT0FII 0Q8mHybMXof9/JkcsB5ULGYMPreJwUDblsZebPW4V+2kRZGdDh1VqNgblUb0d6WagCLrSu hpoFiiCXUn5dbOgiU8jDGWqlpCofehUb/Jm2h7cYY5aUe2McPU6PZ25WSa4dHA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1713184246; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=31UKcuC29fhH2gQ7AFdErWb9xDpB3f6MYdu5CdzUIIQ=; b=TUgzW5rlPcFwUnHGWaqJx1OzgGk/iRrOD4lh6AyUxW9TXvku79JXSZ64ChEDnt8EhUMXW3 4RYcrrpU6vkFVZAQ== To: Thomas Gleixner , LKML Cc: Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [PATCH] posix-timers: Handle returned errors poperly in [i]timer_delete() In-Reply-To: <20240410164558.316665885@linutronix.de> References: <20240410164558.316665885@linutronix.de> Date: Mon, 15 Apr 2024 14:30:46 +0200 Message-ID: <8734rm95ah.fsf@somnus> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" timer_delete_hook() returns -EINVAL when the clock or the timer_del callback of the clock does not exist. This return value is not handled by the callsites timer_delete() and itimer_delete(). Therefore add proper error handling. Signed-off-by: Anna-Maria Behnsen --- When having a look at the posix timer code during reviewing the queue, I stumbled over this inconsitency. Maybe you want to have it in your cleanup queue. Patch applies on top of your queue. kernel/time/posix-timers.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1009,6 +1009,7 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t { struct k_itimer *timer; unsigned long flags; + int ret; =20 timer =3D lock_timer(timer_id, &flags); =20 @@ -1019,7 +1020,11 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t /* Prevent signal delivery and rearming. */ timer->it_signal_seq++; =20 - if (unlikely(timer_delete_hook(timer) =3D=3D TIMER_RETRY)) { + ret =3D timer_delete_hook(timer); + if (ret < 0) + return ret; + + if (unlikely(ret =3D=3D TIMER_RETRY)) { /* Unlocks and relocks the timer if it still exists */ timer =3D timer_wait_running(timer, &flags); goto retry_delete; @@ -1047,6 +1052,7 @@ SYSCALL_DEFINE1(timer_delete, timer_t, t static void itimer_delete(struct k_itimer *timer) { unsigned long flags; + int ret; =20 /* * irqsave is required to make timer_wait_running() work. @@ -1054,13 +1060,17 @@ static void itimer_delete(struct k_itime spin_lock_irqsave(&timer->it_lock, flags); =20 retry_delete: + ret =3D timer_delete_hook(timer); + if (WARN_ON_ONCE(ret < 0)) + return; + /* * Even if the timer is not longer accessible from other tasks * it still might be armed and queued in the underlying timer * mechanism. Worse, that timer mechanism might run the expiry * function concurrently. */ - if (timer_delete_hook(timer) =3D=3D TIMER_RETRY) { + if (ret =3D=3D TIMER_RETRY) { /* * Timer is expired concurrently, prevent livelocks * and pointless spinning on RT.