From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB0F7C7EE24 for ; Tue, 6 Jun 2023 14:37:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237988AbjFFOhg (ORCPT ); Tue, 6 Jun 2023 10:37:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236520AbjFFOh0 (ORCPT ); Tue, 6 Jun 2023 10:37:26 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDB7010C3 for ; Tue, 6 Jun 2023 07:37:21 -0700 (PDT) Message-ID: <20230606142031.071059989@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062240; 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=iBZbrM8aXg0lrMpkqk/B9ueGbYMxSh3wWbEDiu0Wh1o=; b=zAvCHooVXCehbvvBRL4bEjYh9UjlT1Dg5/wlaOn2FjT9/Q6BaQ0j7jdRCyE+KtygLsHVZx olQCqmJVAu4L20tAv2dh63LCsSboF8YM+3vOsTbdbS+KvI7ZXf83UVCgJuum6THOr4jdjm NHbW8bWPt8dzatd4cDAUNw0RYnKZeNEXal/Zmoz0h1fuVzFFiHeaBO6XD/QCIKBI5VrrQl 0DipQL9zXTv/BCAKUZnQVWrWXJFtGfOWBMqWeR8zSgnF6jQfjTDuyHY+1cGAcJE62SUrj0 JHE6gdfaFFsLE5cjvBxFkPT7lK1xbaE9MxUp8l5Xqtyp3GzHC3xKpO64kBb10g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062240; 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=iBZbrM8aXg0lrMpkqk/B9ueGbYMxSh3wWbEDiu0Wh1o=; b=9VqH++YV0s3KDheDw0FwKjW9k1vXOoHmjwK4BwJVHg7Foj9ibE8CzUrGhhrJerU4EL5OZ3 NqBkukSrCqmHTQBw== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 01/45] selftests/timers/posix_timers: Make signal distribution test less fragile References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:19 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The signal distribution test has a tendency to hang for a long time as the signal delivery is not really evenly distributed. Increasing the timer interval to 10ms makes this less likely. Add a timeout to catch the case where it hangs and terminate the test gracefully. While at it get rid of the pointless atomic operation on a the thread local variable in the signal handler. Signed-off-by: Thomas Gleixner --- tools/testing/selftests/timers/posix_timers.c | 42 ++++++++++++++++-----= ----- 1 file changed, 27 insertions(+), 15 deletions(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -188,18 +188,19 @@ static int check_timer_create(int which) return 0; } =20 -int remain; -__thread int got_signal; +static int remain; +static __thread int got_signal; =20 static void *distribution_thread(void *arg) { - while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); + while (__atomic_load_n(&remain, __ATOMIC_RELAXED) && !done); + return NULL; } =20 static void distribution_handler(int nr) { - if (!__atomic_exchange_n(&got_signal, 1, __ATOMIC_RELAXED)) + if (++got_signal =3D=3D 1) __atomic_fetch_sub(&remain, 1, __ATOMIC_RELAXED); } =20 @@ -209,20 +210,22 @@ static void distribution_handler(int nr) */ static int check_timer_distribution(void) { - int err, i; - timer_t id; const int nthreads =3D 10; pthread_t threads[nthreads]; struct itimerspec val =3D { .it_value.tv_sec =3D 0, - .it_value.tv_nsec =3D 1000 * 1000, + .it_value.tv_nsec =3D 20 * 1000 * 1000, .it_interval.tv_sec =3D 0, - .it_interval.tv_nsec =3D 1000 * 1000, + .it_interval.tv_nsec =3D 20 * 1000 * 1000, }; + time_t start, now; + int err, i; + timer_t id; =20 printf("Check timer_create() per process signal distribution... "); fflush(stdout); =20 + done =3D 0; remain =3D nthreads + 1; /* worker threads + this thread */ signal(SIGALRM, distribution_handler); err =3D timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); @@ -244,7 +247,18 @@ static int check_timer_distribution(void } =20 /* Wait for all threads to receive the signal. */ - while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); + now =3D start =3D time(NULL); + while (__atomic_load_n(&remain, __ATOMIC_RELAXED)) { + now =3D time(NULL); + if (now - start > 5) + break; + } + done =3D 1; + + if (timer_delete(id)) { + perror("Can't delete timer\n"); + return -1; + } =20 for (i =3D 0; i < nthreads; i++) { if (pthread_join(threads[i], NULL)) { @@ -253,12 +267,10 @@ static int check_timer_distribution(void } } =20 - if (timer_delete(id)) { - perror("Can't delete timer\n"); - return -1; - } - - printf("[OK]\n"); + if (now - start <=3D 5) + printf("[OK]\n"); + else + printf("%d [FAIL]\n", got_signal); return 0; } From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B605FC7EE24 for ; Tue, 6 Jun 2023 14:37:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237997AbjFFOhj (ORCPT ); Tue, 6 Jun 2023 10:37:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237716AbjFFOh1 (ORCPT ); Tue, 6 Jun 2023 10:37:27 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3ACB210D9 for ; Tue, 6 Jun 2023 07:37:23 -0700 (PDT) Message-ID: <20230606142031.130432648@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062241; 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=MIIp9euOFQMWAHPLIaiH4BFUPFpK2SH94uXT++JmpqU=; b=OhJCSbA791K9lpNAXmM+45wOMTWX9MdTdYKmX1TmFSBi9VUGrV9iTeqZ2IdzbFlKCJ1x1X O6dxV1AF4Pw/BS8wgB9/C30jgSrae/3Lf535gEqFmy3PEdUQO7ETmQ5TV/P+mEvTTaFHMe vWOb8VDiJjEvZIyNSNIehpnd4xDxsoNvM5fvujIIsEvR8+uZ6r2/OP6Ggyv9JGnXDBHVY1 rhDlJs8Jsmfmdb627MbKyFqyVhpoZNdI5UAYv6CGEZ1gnt7OOcQppYKOFQ/MfiWK0kX4AV Fi5jlCmSTdZLsoVsAuZ3m/eoixDXpJXyxR+aql2CA/QqzB9T7O8343Q95Npl2A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062241; 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=MIIp9euOFQMWAHPLIaiH4BFUPFpK2SH94uXT++JmpqU=; b=VhykV8UV6fU3kmkb4uU6Ex5lS64pWPlCpFWkDAaJWZ/CmJYkJVKdWONv1VtKZME8w+rNcu uervyfdtn2KW4DAQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 02/45] selftests/timers/posix_timers: Use TAP reporting format References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:21 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" No functional change. Signed-off-by: Thomas Gleixner --- tools/testing/selftests/timers/posix_timers.c | 173 +++++++++------------= ----- 1 file changed, 65 insertions(+), 108 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 */ @@ -66,15 +81,13 @@ static int check_diff(struct timeval sta diff =3D end.tv_usec - start.tv_usec; diff +=3D (end.tv_sec - start.tv_sec) * USECS_PER_SEC; =20 - if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) { - printf("Diff too high: %lld..", diff); + if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) return -1; - } =20 return 0; } =20 -static int check_itimer(int which) +static void check_itimer(int which, const char *name) { int err; struct timeval start, end; @@ -82,17 +95,6 @@ static int check_itimer(int which) .it_value.tv_sec =3D DELAY, }; =20 - printf("Check itimer "); - - if (which =3D=3D ITIMER_VIRTUAL) - printf("virtual... "); - else if (which =3D=3D ITIMER_PROF) - printf("prof... "); - else if (which =3D=3D ITIMER_REAL) - printf("real... "); - - fflush(stdout); - done =3D 0; =20 if (which =3D=3D ITIMER_VIRTUAL) @@ -103,16 +105,12 @@ static int check_itimer(int which) signal(SIGALRM, sig_handler); =20 err =3D gettimeofday(&start, NULL); - if (err < 0) { - perror("Can't call gettimeofday()\n"); - return -1; - } + if (err < 0) + fatal_error(name, "gettimeofday()"); =20 err =3D setitimer(which, &val, NULL); - if (err < 0) { - perror("Can't set timer\n"); - return -1; - } + if (err < 0) + fatal_error(name, "setitimer()"); =20 if (which =3D=3D ITIMER_VIRTUAL) user_loop(); @@ -122,20 +120,16 @@ static int check_itimer(int which) idle_loop(); =20 err =3D gettimeofday(&end, NULL); - if (err < 0) { - perror("Can't call gettimeofday()\n"); - return -1; - } + if (err < 0) + fatal_error(name, "gettimeofday()"); =20 if (!check_diff(start, end)) - printf("[OK]\n"); + ksft_test_result_pass("check_itimer %s\n", name); else - printf("[FAIL]\n"); - - return 0; + ksft_test_result_fail("check_itimer %s\n", name); } =20 -static int check_timer_create(int which) +static void check_timer_create(int which, const char *name) { int err; timer_t id; @@ -144,48 +138,32 @@ static int check_timer_create(int which) .it_value.tv_sec =3D DELAY, }; =20 - printf("Check timer_create() "); - if (which =3D=3D CLOCK_THREAD_CPUTIME_ID) { - printf("per thread... "); - } else if (which =3D=3D CLOCK_PROCESS_CPUTIME_ID) { - printf("per process... "); - } - fflush(stdout); - done =3D 0; err =3D timer_create(which, NULL, &id); - if (err < 0) { - perror("Can't create timer\n"); - return -1; - } - signal(SIGALRM, sig_handler); + if (err < 0) + fatal_error(name, "timer_create()"); + + if (signal(SIGALRM, sig_handler) =3D=3D SIG_ERR) + fatal_error(name, "signal()"); =20 err =3D gettimeofday(&start, NULL); - if (err < 0) { - perror("Can't call gettimeofday()\n"); - return -1; - } + if (err < 0) + fatal_error(name, "gettimeofday()"); =20 err =3D timer_settime(id, 0, &val, NULL); - if (err < 0) { - perror("Can't set timer\n"); - return -1; - } + if (err < 0) + fatal_error(name, "timer_settime()"); =20 user_loop(); =20 err =3D gettimeofday(&end, NULL); - if (err < 0) { - perror("Can't call gettimeofday()\n"); - return -1; - } + if (err < 0) + fatal_error(name, "gettimeofday()"); =20 if (!check_diff(start, end)) - printf("[OK]\n"); + ksft_test_result_pass("check_timer_create %s\n", name); else - printf("[FAIL]\n"); - - return 0; + ksft_test_result_fail("check_timer_create %s\n", name); } =20 static int remain; @@ -208,7 +186,7 @@ static void distribution_handler(int nr) * Test that all running threads _eventually_ receive CLOCK_PROCESS_CPUTIM= E_ID * timer signals. This primarily tests that the kernel does not favour any= one. */ -static int check_timer_distribution(void) +static void check_timer_distribution(void) { const int nthreads =3D 10; pthread_t threads[nthreads]; @@ -222,28 +200,22 @@ static int check_timer_distribution(void int err, i; timer_t id; =20 - printf("Check timer_create() per process signal distribution... "); - fflush(stdout); - done =3D 0; remain =3D nthreads + 1; /* worker threads + this thread */ - signal(SIGALRM, distribution_handler); + if (signal(SIGALRM, distribution_handler) =3D=3D SIG_ERR) + fatal_error(NULL, "signal()"); + err =3D timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); - if (err < 0) { - perror("Can't create timer\n"); - return -1; - } + if (err < 0) + fatal_error(NULL, "timer_create()"); + err =3D timer_settime(id, 0, &val, NULL); - if (err < 0) { - perror("Can't set timer\n"); - return -1; - } + if (err < 0) + fatal_error(NULL, "timer_settime()"); =20 for (i =3D 0; i < nthreads; i++) { - if (pthread_create(&threads[i], NULL, distribution_thread, NULL)) { - perror("Can't create thread\n"); - return -1; - } + if (pthread_create(&threads[i], NULL, distribution_thread, NULL)) + fatal_error(NULL, "pthread_create()"); } =20 /* Wait for all threads to receive the signal. */ @@ -255,41 +227,29 @@ static int check_timer_distribution(void } done =3D 1; =20 - if (timer_delete(id)) { - perror("Can't delete timer\n"); - return -1; - } + if (timer_delete(id)) + fatal_error(NULL, "timer_delete()"); =20 for (i =3D 0; i < nthreads; i++) { - if (pthread_join(threads[i], NULL)) { - perror("Can't join thread\n"); - return -1; - } + if (pthread_join(threads[i], NULL)) + fatal_error(NULL, "pthread_join()"); } =20 if (now - start <=3D 5) - printf("[OK]\n"); + ksft_test_result_pass("check_signal_distribution\n"); else - printf("%d [FAIL]\n", got_signal); - return 0; + ksft_test_result_fail("check_signal_distribution\n"); } =20 int main(int argc, char **argv) { - printf("Testing posix timers. False negative may happen on CPU execution = \n"); - printf("based timers if other threads run on the CPU...\n"); - - if (check_itimer(ITIMER_VIRTUAL) < 0) - return ksft_exit_fail(); + ksft_print_header(); + ksft_set_plan(6); =20 - 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 @@ -300,11 +260,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 - return ksft_exit_pass(); + ksft_finished(); } From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9306DC77B73 for ; Tue, 6 Jun 2023 14:37:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237714AbjFFOhn (ORCPT ); Tue, 6 Jun 2023 10:37:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237902AbjFFOh2 (ORCPT ); Tue, 6 Jun 2023 10:37:28 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9039E42 for ; Tue, 6 Jun 2023 07:37:24 -0700 (PDT) Message-ID: <20230606142031.189748136@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062243; 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=yYUjgtlS1VYYLW2ktjjDityNYKx8aW+Dqr0QO8tGRWs=; b=dqlh7RYzlcA2jThLOloAPoArtDoxWdPYT7GQmlD3Zr7Iqj9FgxzLWj7BQt+DdU4HZmnHEV aeFB9SOdnYOPJm5JyY6uKvthAqUS/aZUNI6bQNbF27xSO4SpFBd/MSvN/EWF4Yx2XIiPTN uMMqDJa4CMmySUsPpx9UP/kyHYAsAkYWfME+fkM36x4s336v5ZQZonXRVPjgnNhBq05Vd7 SngygYdiDVOBhMJC/Q144+gbjs8FsXRDh7DlqhgeCEjbMI5B3EGo2V6MNdSNffMGmbcZgh 9MvSp7VXqBU76uQtWAteqRi/xhehkMsF2yzwLpHnTbfG/waGatznAK2N5KHGXA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062243; 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=yYUjgtlS1VYYLW2ktjjDityNYKx8aW+Dqr0QO8tGRWs=; b=lREuHplGAsm0oXNanGsGUW9sxxtwmwYdDXj/iv6dfw8WFGJ+gjV/scneX3N4i4Vy4HFCCI mbr1yfTldUxzHHBQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 03/45] selftests/timers/posix_timers: Add SIG_IGN test References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:22 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 128 +++++++++++++++++++++= ++++- 1 file changed, 126 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 @@ -241,10 +242,131 @@ static void check_timer_distribution(voi ksft_test_result_fail("check_signal_distribution\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 */ + pthread_cancel(pthread); + pthread_join(pthread, NULL); + } + + /* 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) { + if (tsig.signals =3D=3D 1 && tsig.overruns =3D=3D 29) + ksft_test_result_pass("check_sig_ign SIGEV_SIGNAL\n"); + else + ksft_test_result_fail("check_sig_ign SIGEV_SIGNAL\n"); + } else { + if (tsig.signals =3D=3D 0 && tsig.overruns =3D=3D 0) + ksft_test_result_pass("check_sig_ign SIGEV_THREAD_ID\n"); + else + ksft_test_result_fail("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 check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL"); check_itimer(ITIMER_PROF, "ITIMER_PROF"); @@ -262,6 +384,8 @@ int main(int argc, char **argv) */ check_timer_create(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); check_timer_distribution(); + check_sig_ign(0); + check_sig_ign(1); =20 ksft_finished(); } From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9521CC7EE29 for ; Tue, 6 Jun 2023 14:37:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238038AbjFFOhp (ORCPT ); Tue, 6 Jun 2023 10:37:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237497AbjFFOhb (ORCPT ); Tue, 6 Jun 2023 10:37:31 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D465710E0 for ; Tue, 6 Jun 2023 07:37:26 -0700 (PDT) Message-ID: <20230606142031.246358079@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062245; 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=dCkKva35ZSsZYYHSJnYJ6h+ag01NecWv1M8cjMvd9xA=; b=QU/albRfnjNxtVaW4ks6OeYpS1Ry45BwaI5jS+HjFYb2u9gZDXAtypmNiWbcV65qkRyqjL HkO3FnDqbvQNrM2SS2t0J90ZBR0/kwI5aVuDtjPm0vp5kifLJwelmPtGbeekDoXQdVQVNg 6gzmA7A68rL0e05W+bxbYJexIfYLw7n+FqSlykA/BLTUIxqYLyKs9izoFlPR/TiP21srSF oOIObNNB7TCW8ZY2MfwOO7aNETXuW6va+/WD30jSYAPTqvxjxkGIFlXmdYNV3FpLVsUSlV pukYPDRopj0hHXsVKgTk1qbdJsRoXds9OHOlfTT5kO+QzmetKchG3vX569dxGQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062245; 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=dCkKva35ZSsZYYHSJnYJ6h+ag01NecWv1M8cjMvd9xA=; b=sBlw+8T1RUYJw2yek8bJMo4AxLnoh2lL8sCOe8xz95+a/RPwM6Jv3KHDRF4SYEMEOouBcZ Z5jUQsDF7OSaohBg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 04/45] selftests/timers/posix_timers: Validate signal rules References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:25 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 113 +++++++++++++++++++++= ++++- 1 file changed, 112 insertions(+), 1 deletion(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -363,10 +363,119 @@ 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; + timer_settime(timerid, 0, &its, NULL); + + sleep(1); + + /* Reprogram the timer to single shot */ + its.it_value.tv_sec =3D 1; + its.it_value.tv_nsec =3D 0; + its.it_interval.tv_sec =3D 0; + its.it_interval.tv_nsec =3D 0; + timer_settime(timerid, 0, &its, NULL); + + /* 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()"); + + if (!tsig.signals) + ksft_test_result_pass("check_rearm\n"); + else + ksft_test_result_fail("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; + timer_settime(timerid, 0, &its, NULL); + + 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)"); + + if (!tsig.signals) + ksft_test_result_pass("check_delete\n"); + else + ksft_test_result_fail("check_delete\n"); +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(8); + ksft_set_plan(10); =20 check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL"); check_itimer(ITIMER_PROF, "ITIMER_PROF"); @@ -386,6 +495,8 @@ int main(int argc, char **argv) check_timer_distribution(); check_sig_ign(0); check_sig_ign(1); + check_rearm(); + check_delete(); =20 ksft_finished(); } From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77EA0C7EE24 for ; Tue, 6 Jun 2023 14:37:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238042AbjFFOhs (ORCPT ); Tue, 6 Jun 2023 10:37:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237960AbjFFOhc (ORCPT ); Tue, 6 Jun 2023 10:37:32 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8630E10D9 for ; Tue, 6 Jun 2023 07:37:28 -0700 (PDT) Message-ID: <20230606142031.303328672@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062247; 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=kUcWS9fKA9MhrLnlJFYDQVKQCJlVl0X8B00s0xylFSg=; b=mFOehDrpWendqw/cAVSYhGChRkb3iYtPm4Xp7CX2H+d1SxJ9qVRb1vkP+aBwyehWPL44Nc 4pGtpZuU7woPf1OS7IEHZDzox0gOr3B8BcPEzx5AaJixiefUMsREZviaiDzO1L9TlNYfTm 3DuOAjwU+CIn9UAno4nNi+5UEgaCZ2lb8c3Axqwe0CGBq5MiR+quuTs7EpBw/JfGcRpLtA VYi3dlDyqaK1ouZzY5414NW/cuT9Oz0FZ0smuICrFzunBUPIy7Zr/IfXPu2IIu7Shp9RTK 23O5GioCN4650KgduvEkZinYfWMfTSjDOf9OFC7wb4tSy5IvlriSp8DzYIjvFA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062247; 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=kUcWS9fKA9MhrLnlJFYDQVKQCJlVl0X8B00s0xylFSg=; b=gjFNHKwfsFOq8w0NS15qbmk1cLOOF5v0OtUF2zBdEg7PyKe02PSpVNHQzFxGWp798k9GVC A/7Gz6v1UK1CDcDA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 05/45] selftests/timers/posix-timers: Validate SIGEV_NONE References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:26 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 55 +++++++++++++++++++++= ++++- 1 file changed, 54 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) { @@ -472,10 +474,59 @@ static void check_delete(void) ksft_test_result_fail("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()"); + + if (its.it_value.tv_sec || its.it_value.tv_nsec) + ksft_test_result_pass("check_sigev_none %s\n", name); + else + ksft_test_result_fail("check_sigev_none %s\n", name); +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(10); + ksft_set_plan(12); =20 check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL"); check_itimer(ITIMER_PROF, "ITIMER_PROF"); @@ -497,6 +548,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F0A4C77B73 for ; Tue, 6 Jun 2023 14:37:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238047AbjFFOhv (ORCPT ); Tue, 6 Jun 2023 10:37:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237969AbjFFOhe (ORCPT ); Tue, 6 Jun 2023 10:37:34 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 113FCE6E for ; Tue, 6 Jun 2023 07:37:30 -0700 (PDT) Message-ID: <20230606142031.361364275@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062248; 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=vEpeXsIrZe+OvjERrC+2BpJZspHhIovUmZBUn14588o=; b=MFxYngCMvgFGbZ+qeTOM2WVe2omVI9vCB0KU5kPN8qjGKnGt0ttOHj9DjYtH8UPEe6vSe/ SgYgJrhjyWbHDxrwa8vngQ7l/MqwIYGAHyHvS4w1wS4W8A8QYC4L9MlPRHVypqGCfwjp7s FAT7Wy+ZQghzVPaNqnZMN43gDUXrDxKtjPbFcebAmmad7ohKC4Jgqo6UV6bmcoAwyHL0AX 7ExdTVqndCXYfLUHSJlpQ+uHfZY/s9cNtDHkU/mhxpgPzzABT1ACoAdHzWdG7Kzgo5pzvd 0pPI5wJ01zomBYaoD3u4BxHpsBQAA0CHWvLRb9FmcIKTGWgS6u44Fup8Tcqb4A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062248; 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=vEpeXsIrZe+OvjERrC+2BpJZspHhIovUmZBUn14588o=; b=0KZ505Aw8WKTp7bXANs+TTFcUIQxHVvXogT58oey98b195Yyc7+kJIcfaqn6Aw3wycatnY /Df4udSlqHwuMeDw== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 06/45] selftests/timers/posix-timers: Validate timer_gettime() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:28 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 60 +++++++++++++++++++++= ++++- 1 file changed, 59 insertions(+), 1 deletion(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -523,10 +523,65 @@ static void check_sigev_none(int which, ksft_test_result_fail("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; + timer_settime(timerid, 0, &its, NULL); + + 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()"); + + if (wraps > 1) + ksft_test_result_pass("check_gettime %s\n", name); + else + ksft_test_result_fail("check_gettime %s\n", name); +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(12); + ksft_set_plan(15); =20 check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL"); check_itimer(ITIMER_PROF, "ITIMER_PROF"); @@ -550,6 +605,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29BE3C77B73 for ; Tue, 6 Jun 2023 14:37:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238082AbjFFOh5 (ORCPT ); Tue, 6 Jun 2023 10:37:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238012AbjFFOhl (ORCPT ); Tue, 6 Jun 2023 10:37:41 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B29010F0 for ; Tue, 6 Jun 2023 07:37:31 -0700 (PDT) Message-ID: <20230606142031.418148389@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062250; 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=BSLAZiaPu4OlRt7MJLr8iteahUnDIy2W/NiT+df9pkg=; b=NGSYWuzHITJVHI6VTsAf1RP0yNKLn79a3HYPloWgCUX5l4IL/Y3fEGAy5jJdnxpqLdeDOu BJEU49bHoqnbPP7zSmGBLf2qDwc7R8IkUMt++9Nm10DLjKwhwEGOL1s2w62k/FYrneQ8hq GLGJH5NU02wxZ5XXnXWHnfIk9i8+nCDuMdTgZ8UbZSmviMi+9BrdNVWYMr1GDvX2oTCiWe XiMRa9wCPZg4gkYCfXjL3GGSbpfnIkymBPHGLs01wQLKlnV62KgQk4u62mxfS2WEOSaRvh jZaEA4GGm40/gTq3MCj1QDqIHJpmUPBZon847bpwaRNcZwShycOsh8rfoDRhfQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062250; 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=BSLAZiaPu4OlRt7MJLr8iteahUnDIy2W/NiT+df9pkg=; b=SZ+WwAc0hpLj84ylncyoVTYGGxBxKyzgeyk1hVyKYq34jKgtd5/JJtvSKU/azX3SrSJ+3V bPmlI+45gUg4iQCg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 07/45] selftests/timers/posix-timers: Validate overrun after unblock References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:29 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 63 +++++++++++++++++++++= ++++- 1 file changed, 62 insertions(+), 1 deletion(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -578,10 +578,68 @@ static void check_gettime(int which, con ksft_test_result_fail("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; + 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); + + /* 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()"); + + if (tsig.signals =3D=3D 1 && tsig.overruns =3D=3D 9) + ksft_test_result_pass("check_overrun %s\n", name); + else + ksft_test_result_fail("check_overrun %s\n", name); +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(15); + ksft_set_plan(18); =20 check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL"); check_itimer(ITIMER_PROF, "ITIMER_PROF"); @@ -608,6 +666,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2BC9C7EE29 for ; Tue, 6 Jun 2023 14:38:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237716AbjFFOiA (ORCPT ); Tue, 6 Jun 2023 10:38:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238014AbjFFOhl (ORCPT ); Tue, 6 Jun 2023 10:37:41 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DB3710FB for ; Tue, 6 Jun 2023 07:37:33 -0700 (PDT) Message-ID: <20230606142031.475141256@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062251; 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=feQqbMJR8LNStx4hiDUEIHYcUpveV5I+mK7qWj22Q+I=; b=K+0PvOJHghqEuSL/XjZ+ievKQ/BpS5nPc98SPu4u7Piv031DQHuSw9oGajptxP1/O3ogEw Fl8h62mYfWJzhBl5vt1SfnvfDv6IZsSKv2SkCdnWniZbRcMcW24rreNAvg5IFmYuJPH25V 8/A8aPCo2v2h6c+XDhMrLrBrRY9PaCT0+uUpc4KREfVFN7t97aDXY5hWuqtYp6/LUg/gdU FD5gLMYyWUjwLccJ3W1YoL64EQ0KSi+4O9ewaCKY/+VUesGZWbzBVVhb0bN/KHpZ17NiK1 19EOg+RuOZcA4sR2BL32JucxrtRvxhIoNTYZ2saEXqn0+S6yjn3mqpv1K1J98w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062251; 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=feQqbMJR8LNStx4hiDUEIHYcUpveV5I+mK7qWj22Q+I=; b=t67079sbUXtqL232iy/n95/NpVWYpidkqzaBa5qDnHjwomga0pshXT7Z0jzaT88DiOzQz+ gBhkOKIZPUOtoeAw== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 08/45] posix-timers: Convert timer list to hlist References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:31 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 16 ++++++++-------- 6 files changed, 15 insertions(+), 15 deletions(-) --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2461,13 +2461,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) @@ -2496,7 +2496,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 @@ -221,7 +221,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 @@ -136,7 +136,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 @@ -28,7 +28,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 @@ -1880,7 +1880,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 @@ -1021,7 +1021,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 @@ -1071,7 +1071,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 @@ -1092,20 +1092,20 @@ static void itimer_delete(struct k_itime */ void exit_itimers(struct task_struct *tsk) { - struct list_head timers; + struct hlist_head timers; struct k_itimer *tmr; =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); + while (!hlist_empty(&timers)) { + tmr =3D hlist_entry(timers.first, struct k_itimer, list); itimer_delete(tmr); } } From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31FF4C77B73 for ; Tue, 6 Jun 2023 14:38:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238143AbjFFOiF (ORCPT ); Tue, 6 Jun 2023 10:38:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238034AbjFFOhp (ORCPT ); Tue, 6 Jun 2023 10:37:45 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E11861706 for ; Tue, 6 Jun 2023 07:37:34 -0700 (PDT) Message-ID: <20230606142031.532247561@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062253; 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=2mlrLNBzpK9wpMz36KzO779P+Hz9+q0V8boLAVqB0tk=; b=CJ8WvJz92LOksqzWChyCdQgegHuA0iI3APF+UbZhF4QTEWI16R6Zi1n2EtGAEhAxFgoWpJ 2IhuE+b5XAhpZg7ZVSz8T8FGoUAYr4iIoPtW3zaHAWtOc6KHuETgfoIDai0N8rWfxqjbhU OFL1WyXZZuX0SiBvAy/0daSoqx7caiDZLTGQOwhOJebvQkfHQDkpukEZnHVAo+QfJCof8f lDnuydDPUcN+YHK6UNRLZ3zf4iff7iraJyNVf+Jhnj4mJWYCNaE1lCBjn+SqVpZFrdRodF 5ewoilq02tbaXMHgi/2snlkBwR6zKEHT1chfANfqXoGu3PI1R2Z77g2CXBMK2Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062253; 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=2mlrLNBzpK9wpMz36KzO779P+Hz9+q0V8boLAVqB0tk=; b=+c+DRtc+oNbJkpTeMlItVZc5M7N1nViAElD/CWYyRFb4rkkQD6yPHkUaB2xaaSn3fF6hR6 Rm3g9gbP3GROP+Bg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 09/45] posix-cpu-timers: Fix posix_cpu_timer_get() behaviour References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:33 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. This has to be correct also for interval timers even if the signal is pending or the timer has been created with SIGEV_NONE. The posix CPU timer implementation fails for both cases as it does not forward the timer in posix_cpu_timer_get() before calculating the expiry time. It neither clears the expiry value when a oneshot SIGEV_NONE timer expired and returns 1nsec instead, which is only correct for timers with signals when the signal delivery did not happen yet. Aside of that posix_cpu_timer_set() pointlessly arms SIGEV_NONE timers which are later disarmed when the initial expiry happens. That's bogus and just keeping the process wide timer active for nothing. Cure this by: 1) Avoiding to arm SIGEV_NONE timers 2) Forwarding interval timers in posix_cpu_timer_get() 3) Taking SIGEV_NONE into account when a oneshot timer has expired Make the update logic a separate function so it can be reused to simplify posix_cpu_timer_set(). Fixes: ae1a78eecc45 ("cpu-timers: Return correct previous timer reload valu= e") Signed-off-by: Thomas Gleixner --- kernel/time/posix-cpu-timers.c | 96 +++++++++++++++++++++++-------------= ----- 1 file changed, 54 insertions(+), 42 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. @@ -623,6 +618,7 @@ static void cpu_timer_fire(struct k_itim 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; @@ -686,7 +682,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 if (old) { if (old_expires =3D=3D 0) { @@ -723,19 +719,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); /* @@ -754,7 +751,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 @@ -785,45 +782,60 @@ 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; + bool sigev_none =3D timer->it_sigev_notify =3D=3D SIGEV_NONE; + u64 expires; =20 /* - * Easy part: convert the reload time. + * 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 */ - itp->it_interval =3D ktime_to_timespec64(timer->it_interval); - - if (!expires) - goto out; + expires =3D bump_cpu_timer(timer, now); =20 /* - * Sample the clock to take the difference with the expiry time. + * Interval timers cannot have a remaining time <=3D 0 because the + * forwarding guarantees to move them forward so that the next + * timer expiry is > @now. */ - if (CPUCLOCK_PERTHREAD(timer->it_clock)) - now =3D cpu_clock_sample(clkid, p); - else - now =3D cpu_clock_sample_group(clkid, p, false); - if (now < expires) { 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; + } +} + +static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec6= 4 *itp) +{ + clockid_t clkid =3D CPUCLOCK_WHICH(timer->it_clock); + struct cpu_timer *ctmr =3D &timer->it.cpu; + 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(ctmr)) { + 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); + } } -out: rcu_read_unlock(); } From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B714C77B73 for ; Tue, 6 Jun 2023 14:38:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238148AbjFFOiI (ORCPT ); Tue, 6 Jun 2023 10:38:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238053AbjFFOhw (ORCPT ); Tue, 6 Jun 2023 10:37:52 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 815E7170B for ; Tue, 6 Jun 2023 07:37:36 -0700 (PDT) Message-ID: <20230606142031.591764431@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062255; 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=HZelD7evOzJCYoBqQ1DtkrmmziVEl2j+krjTDCi41Fw=; b=eKWFjcJLrzMr3pZrkCaPux8kc0/F5ATRMLRIkAmIXE6vAW2dLhmRu1FlnipAK8ZqAY87yN OKdVizAKcJwW+gDt6j3meD7VOb5suNceXo19xEpUev4iofYTJRl26w0Xy4alTZM/CxVXfe u5Jn1bIM2B7BbNYM9CmWdtcD7q+H7+Eh+kVl3u+Rl0NR55gR6TKNZ+Wfa38jIBGrSAX+qT SUFbn8l+kwkxvZMmCk1gb4P/lmmbv8D/ltbIfN/lrH0YwzcTdZsbdw/QYws73GyNWn9gJ0 2jXicaTUDGO28/nNj+QYy82r/Znje7VcTuELo1Q0ucGg/iEcnlgY+4yPrAurdw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062255; 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=HZelD7evOzJCYoBqQ1DtkrmmziVEl2j+krjTDCi41Fw=; b=DZ+CbV6pqD6kTZqg47gK9orPuBvbFpmsqiJfvD93glKabXigqESgkTneY7J5E6nbMULLEW 9J1HkuJTyR6CM1BQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 10/45] posix-cpu-timers: Use @now instead of @val for clarity References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:34 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -620,7 +620,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; @@ -672,17 +672,13 @@ 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 if (old) { if (old_expires =3D=3D 0) { @@ -696,10 +692,10 @@ static int posix_cpu_timer_set(struct k_ * though we are swallowing that pending * notification here to install the new setting. */ - u64 exp =3D bump_cpu_timer(timer, val); + u64 exp =3D bump_cpu_timer(timer, now); =20 - if (val < exp) { - old_expires =3D exp - val; + if (now < exp) { + old_expires =3D exp - now; old->it_value =3D ns_to_timespec64(old_expires); } else { old->it_value.tv_nsec =3D 1; @@ -721,7 +717,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); @@ -731,7 +727,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); @@ -751,7 +747,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 490F3C77B73 for ; Tue, 6 Jun 2023 14:38:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238160AbjFFOiM (ORCPT ); Tue, 6 Jun 2023 10:38:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238061AbjFFOhx (ORCPT ); Tue, 6 Jun 2023 10:37:53 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8952A171A for ; Tue, 6 Jun 2023 07:37:38 -0700 (PDT) Message-ID: <20230606142031.648340279@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062256; 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=8wmGhqPNf6kLMrAqxn6joNLQMcupbwgXa/J0H7C+wQY=; b=GwMdq1JohYipn8STMGUW7ZcBa0yY0aE3Ne8q2Y9kWG6zabuktu+VI67AjX4eDD+0a+nfDQ Y5y91IEr8HN2mxJVrGL5+RPQvuivR8M9KTlm9W21IBPkuEff8Ga4g1o5Bt0mYvUcmII07A 3rahALeLN57kl6Xfo9m1eVR3o9mND58LTV+fiR/lwgnq89D337yFIktgK83Wh/Lq++f+bW k6SXurPQDJPLB+GXX4Zi7UhydCCH35CP3tSuwcEOZTrkHs4vKmHbI2hPwi6bsbaWpNZy4W y+NZUY2n1XOUpcTQ2vxyZGs5aEkTjT2CiCdAR1FOy2Syh6s9Z464xbdAXQiUaw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062256; 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=8wmGhqPNf6kLMrAqxn6joNLQMcupbwgXa/J0H7C+wQY=; b=AFQgyOFQtWxkhRmhQZ8dSxldXlmaT+Lhpj3c5Vt0pTIv0bC1H3JDQa2jlaj3E8vb34iPS+ p88qJF0IKGlbLFBg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 11/45] posix-cpu-timers: Remove incorrect comment in posix_cpu_timer_set() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:36 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -704,13 +704,8 @@ static int posix_cpu_timer_set(struct k_ } } =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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDE15C77B7A for ; Tue, 6 Jun 2023 14:38:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238120AbjFFOiW (ORCPT ); Tue, 6 Jun 2023 10:38:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237955AbjFFOh6 (ORCPT ); Tue, 6 Jun 2023 10:37:58 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3070172E for ; Tue, 6 Jun 2023 07:37:39 -0700 (PDT) Message-ID: <20230606142031.705286109@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062258; 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=dI0D1Wo/Od6DcD7Ru+BANLzwyGF+XR8DfEoK3hFuF2Q=; b=Yk/DXFdlqLfDQyfx0BKzMlN/bE3cA9fTYL2SSewfi6g0ICus4972urAWOD18WUFrh8QElC J2cZ+8OHKTjsths3W8x50BG9w+ENc2CAv0z9OyxhrYV9U2Khyusw5ZL++NT1iWc3Iw/NoW U7GHdOSNqa54uenn6q2mW4QdYxaUIH3QWvztjseXzYm93xO3le3AajEnSDX02FtCwZzB5H 7ta7T0VkQEUzZ4AVGTQ5cTQG5WGT2B3OV/cOVbp7uyRGicCMiZfpp+wBEnp2KxryAAA8if PJvMUwjuhbMdyuEf4D+zN9MMG1vngzz37BKBmzPQrDgwUdGEgP7QnKYvj/Z12w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062258; 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=dI0D1Wo/Od6DcD7Ru+BANLzwyGF+XR8DfEoK3hFuF2Q=; b=SNm/mWkMVZs8Rfv0eVLM0rOkYdYVZZ281k1zAaoHe4TG53Ioth7F4d1QqGFxgdGx53QrZA udmiyD8CV7sID9CQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 12/45] posix-cpu-timers: Simplify posix_cpu_timer_set() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:37 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- kernel/time/posix-cpu-timers.c | 38 +++++++++++++-----------------------= -- 1 file changed, 13 insertions(+), 25 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -720,10 +720,14 @@ 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); + else + trigger_base_recalc_expires(timer, p); =20 unlock_task_sighand(p, &flags); /* @@ -742,30 +746,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18BCDC77B7A for ; Tue, 6 Jun 2023 14:38:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238023AbjFFOiT (ORCPT ); Tue, 6 Jun 2023 10:38:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238012AbjFFOh6 (ORCPT ); Tue, 6 Jun 2023 10:37:58 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EE6F10E0 for ; Tue, 6 Jun 2023 07:37:41 -0700 (PDT) Message-ID: <20230606142031.761271959@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062259; 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=ob/W6MhzxScgyFPIMc+gvNg8o5xjqBQZWBN/IorB7bE=; b=SCvoCkSp2EBNPlg9q940g+JrZP6pzFOgz7FFTWpU46jD5cHmKxKnhMZjnbZ9xoPZCpn3ae FVI578SMgJamC3rLpuuE0d9oufWTkLhmu/h29m2mbczYj9XIo0jczpnqU4XebflDkC2Fdl xI7TfXR/VSOru+FfjXi95UcYj6t8ucuEJeBkHeGzzET9MmOCCDx9JdYA7BDNvIE1ufK3gL hZU043m7L2kxkfuozb1a5r3cYXeS0+IorFh837i9kxsGH85T+cx/k6J/lQxkF5kkaE8McZ T36BXvfzeE6X7FZpYYioGrJE1f7S/Ffhxf5c98hWVriw05GhCegVxf2GeNks9w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062259; 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=ob/W6MhzxScgyFPIMc+gvNg8o5xjqBQZWBN/IorB7bE=; b=nQFBQ5ePLWCt0uAyyP09UUBBGlfIp/c+ObKLOPQeUWXRW/gOnELQ77dt6uEmoAQmjmauIu NfATje6SDz2RZGDQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 13/45] posix-cpu-timers: Replace old expiry retrieval in posix_cpu_timer_set() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:39 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- kernel/time/posix-cpu-timers.c | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -609,6 +609,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. @@ -680,29 +682,9 @@ static int posix_cpu_timer_set(struct k_ else now =3D cpu_clock_sample_group(clkid, p, !sigev_none); =20 - 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, now); - - if (now < exp) { - old_expires =3D exp - now; - old->it_value =3D ns_to_timespec64(old_expires); - } else { - old->it_value.tv_nsec =3D 1; - old->it_value.tv_sec =3D 0; - } - } - } + /* Retrieve the previous expiry value if requested. */ + if (old && old_expires) + __posix_cpu_timer_get(timer, old, now); =20 /* Retry if the timer expiry is running concurrently */ if (unlikely(ret)) { From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70C93C7EE24 for ; Tue, 6 Jun 2023 14:38:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238220AbjFFOiY (ORCPT ); Tue, 6 Jun 2023 10:38:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238117AbjFFOh7 (ORCPT ); Tue, 6 Jun 2023 10:37:59 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDD981980 for ; Tue, 6 Jun 2023 07:37:42 -0700 (PDT) Message-ID: <20230606142031.816970056@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062261; 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=BUuJOcWJlcdLruwzTzGc8KK7BJlrslnwV3YCPjzAVYM=; b=I5569+xRiO8L9SxBdKWLlInNoyMp15B5f1fsUhs1CMRrvo5cp44kG2L/Vcn4zOOOwD3ItK P0YOx8Jr1BJcWmL6/6Bre25ByppqEqyTZGXau7n2XKgzVwo1iKT+NygySaNgaVIaZgKgDm FBGuAblTC/3KUREjAh0G5g+N332bg8jawAaS4FdNzKcaqvn4/9dys+TWVzQH1Dig/P+eJ3 M6jDAzJ009c4ETi/evZbe7DzEEPSKUMTFfWtKzSjHvkYNnRQWMpIVDTaNx7mek0YPtQwZu IWNGPjoYqTN6VZJWe/scJykc/7NCj9CRvp36kpt0gfRsLPx5zwOwoBsmbMnUhw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062261; 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=BUuJOcWJlcdLruwzTzGc8KK7BJlrslnwV3YCPjzAVYM=; b=/2HWb7Dxf5+XWRHkdmmsirK76BRh2t1DBD0zPzjUamOK6oidZTOXdFM19EyVGUwK69a7Pm sCh2xituWWCaJOBA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 14/45] posix-timers: Consolidate interval retrieval References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:40 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There is no point to collect the current interval in the posix clock specific settime() and gettime() callbacks. Just do it right in the common code. Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker --- kernel/time/posix-cpu-timers.c | 26 ++++++++------------------ kernel/time/posix-timers.c | 18 +++++++++--------- 2 files changed, 17 insertions(+), 27 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)) { @@ -737,9 +734,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 @@ -785,17 +779,13 @@ static void posix_cpu_timer_get(struct k =20 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(ctmr)) { - if (CPUCLOCK_PERTHREAD(timer->it_clock)) - now =3D cpu_clock_sample(clkid, p); - else - now =3D cpu_clock_sample_group(clkid, p, false); + if (p && cpu_timer_getexpires(ctmr)) { + 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(); } --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -636,17 +636,12 @@ static s64 common_hrtimer_forward(struct */ void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_sett= ing) { + bool sig_none =3D timr->it_sigev_notify =3D=3D SIGEV_NONE; const struct k_clock *kc =3D timr->kclock; - ktime_t now, remaining, iv; - bool sig_none; + bool iv =3D !!timr->it_interval; + ktime_t now, remaining; =20 - sig_none =3D timr->it_sigev_notify =3D=3D SIGEV_NONE; - iv =3D timr->it_interval; - - /* interval timer ? */ - if (iv) { - cur_setting->it_interval =3D ktime_to_timespec64(iv); - } else if (!timr->it_active) { + if (!iv && !timr->it_active) { /* * SIGEV_NONE oneshot timers are never queued and therefore * timr->it_active is always false. The check below @@ -705,6 +700,8 @@ static int do_timer_gettime(timer_t time return -EINVAL; =20 memset(setting, 0, sizeof(*setting)); + setting->it_interval =3D ktime_to_timespec64(timr->it_interval); + kc =3D timr->kclock; if (WARN_ON_ONCE(!kc || !kc->timer_get)) ret =3D -EINVAL; @@ -918,6 +915,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 276E5C77B7A for ; Tue, 6 Jun 2023 14:38:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238165AbjFFOic (ORCPT ); Tue, 6 Jun 2023 10:38:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238139AbjFFOiA (ORCPT ); Tue, 6 Jun 2023 10:38:00 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAEB9198C for ; Tue, 6 Jun 2023 07:37:44 -0700 (PDT) Message-ID: <20230606142031.872478114@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062263; 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=rQhQI9gfYBa5P/AVMCL9wQCPxah+3H97GdCXzoiFlTY=; b=xj06h7G2AYuL8XA++I9UO3Tv1HthEl4dLE7oPU7JbFZ/+TZxwMAc8uTehkZNcyX/ZP4rer o1y5qyIdGw2Mu7QAHEEWRZ2lBuZZWBDOEuXPAnIwOVcgYNi801OeIE/mW5WpQmU1vFhAf/ B+Xa9iasYCAVeffcCeSe+dzyLc4tRHmCFup6o5W6Bl8OHAD/F0cKE7m9SVXE8bvCwWKgOl zOII3BcKHF0ZP0frhI4/nZIRHH21D0ArWVsUAQbHTZHYzCihbcxJThjmTUsmFx05htv7U7 cZo6rr+MrdUFepmjJMXA4nVC5KAPPzUitd0dpuAF2eozJdC8PXOPBC2iU3MQuQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062263; 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=rQhQI9gfYBa5P/AVMCL9wQCPxah+3H97GdCXzoiFlTY=; b=H6NJu23pM67W+4Wwdro+qTF2fnserfMFQXQZIBAcn6l/+MFh0aGf5bjOGA8hsJVHoR2MQd gLnBPwseG2ePG/DA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 15/45] posix-timers: Clear overrun in common_timer_set() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:42 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -867,6 +867,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9DD9AC7EE29 for ; Tue, 6 Jun 2023 14:38:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238076AbjFFOif (ORCPT ); Tue, 6 Jun 2023 10:38:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238155AbjFFOiL (ORCPT ); Tue, 6 Jun 2023 10:38:11 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1EC29E42 for ; Tue, 6 Jun 2023 07:37:46 -0700 (PDT) Message-ID: <20230606142031.928021762@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062264; 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=BVz57Fiq4y3HANqcZc1xKtZ2T7kpNoRDOZcZ2I4Dywg=; b=OXNv1AoeCwdVG8cgbF4uXunKSC+maSLNg/mynVZgvO9IFMNYyEspVWkSY9eokdBKGA2yGz 5umVpPLu1o8Bq9v0QJkLGjLcjDDCKdvEmgZ/XMNLUzFrv8jzQIDpRaHwvDZMhqsBnmk2bZ zHTmOjzDpL0RvXHDykanfAW2dB2WxRuLnuwMDtItFPiSEjSA9AmrupfA5qHyeYnRo4BwjL hzDqIwZBcItlKFxAG4iTMSdyG4REMta+QimfGeEU44roRuzuZPotlRCGHwlOZnJ31m1VEP y1L+mopDD8Ye3mrWDMgrd0UyfYYipTqW73ZQVDRVQJQtZocHnSt4Qp0wdrghpA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062264; 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=BVz57Fiq4y3HANqcZc1xKtZ2T7kpNoRDOZcZ2I4Dywg=; b=0S4FTbLs/WLg9eK1op5nChLJ5PMVrhUanQz0OQODSwoxoI2fZjXNqo94T/KbL+0WAIn+TM N8pa5f1PXIjBtGDA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 16/45] posix-timers: Consolidate timer setup References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:44 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Thomas Gleixner --- kernel/time/posix-cpu-timers.c | 15 +-------------- kernel/time/posix-timers.c | 23 +++++++++++++++++------ kernel/time/posix-timers.h | 1 + 3 files changed, 19 insertions(+), 20 deletions(-) --- a/kernel/time/posix-cpu-timers.c +++ b/kernel/time/posix-cpu-timers.c @@ -709,21 +709,8 @@ static int posix_cpu_timer_set(struct k_ trigger_base_recalc_expires(timer, p); =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 @@ -853,6 +853,21 @@ 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); + + /* 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, @@ -875,16 +890,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8653C7EE24 for ; Tue, 6 Jun 2023 14:38:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238091AbjFFOih (ORCPT ); Tue, 6 Jun 2023 10:38:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238164AbjFFOiM (ORCPT ); Tue, 6 Jun 2023 10:38:12 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC9421993 for ; Tue, 6 Jun 2023 07:37:47 -0700 (PDT) Message-ID: <20230606142031.983662966@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062266; 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=lbMmZ14JuZ/krQR6RrGQ1HvOBRw0gNG6TW4Rb4ueY4Q=; b=fr07iI8eiGqzm9c4spmDHsJoDzM4I3AshKTKn0B4qfJFd1pupaIcWrQv+EkERqd+s1WZXs wiyFUStOfDusf4fsevgOi6yqYiPcD5rIPfMqwufQ+O7nln7PpgKlnpIsyYAcPND2lKlfxA nqTeBws/rzNYwbtQMmrjRpBIwSy9mfkZP3jls4azpkFz+v692Oj9/0ucQAW5A1hsEuN5EF 77uSwrbqgHYNvjBVYwOLZiuiSyeD2bDxvYe2gtARLMvpw5pOtrBLYk66YAeNkHilPxXN4m wMa16b0ivC9t/ywD/tYI0O1OYDxFMyZvUBXy4WPxPwgFAiCo2OzMTsscmUMgsg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062266; 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=lbMmZ14JuZ/krQR6RrGQ1HvOBRw0gNG6TW4Rb4ueY4Q=; b=C/BEPwo7ibfW/Qaq98/UdgpbTp1XbZou+bEA3mK0iDko1X3WaoZgGPVPm/FyOape4HgzCU wfAtQ770fIo9KsDg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 17/45] posix-cpu-timers: Make k_itimer::it_active consistent References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:45 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- kernel/time/posix-cpu-timers.c | 6 ++++++ 1 file changed, 6 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 @@ -668,6 +670,7 @@ static int posix_cpu_timer_set(struct k_ ret =3D TIMER_RETRY; } else { cpu_timer_dequeue(ctmr); + timer->it_active =3D 0; } =20 /* @@ -787,6 +790,7 @@ static u64 collect_timerqueue(struct tim =20 while ((next =3D timerqueue_getnext(head))) { struct cpu_timer *ctmr; + struct k_itimer *ktmr; u64 expires; =20 ctmr =3D container_of(next, struct cpu_timer, node); @@ -799,6 +803,8 @@ static u64 collect_timerqueue(struct tim /* See posix_cpu_timer_wait_running() */ rcu_assign_pointer(ctmr->handling, current); cpu_timer_dequeue(ctmr); + ktmr =3D container_of(ctmr, struct k_itimer, it.cpu); + ktmr->it_active =3D 0; list_add_tail(&ctmr->elist, firing); } From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA350C77B7A for ; Tue, 6 Jun 2023 14:38:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238245AbjFFOij (ORCPT ); Tue, 6 Jun 2023 10:38:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237908AbjFFOiN (ORCPT ); Tue, 6 Jun 2023 10:38:13 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 160EA19A3 for ; Tue, 6 Jun 2023 07:37:50 -0700 (PDT) Message-ID: <20230606142032.040938859@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062267; 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=psF57WLKdFqu9d22GEwITnNmBUegjhn1nfEG4ZPBrXA=; b=mSPDSqrv1NDyZxfVdpBVPROOQPPJPJbiZLlJlHdy2N3yQzUi5VtXwvWZ0d2alC7elOSJ4Y fbMuF1NEfVrLI2MYC4WrsVzJNyBgERX+kUQItZu0/8d6GlKbYIde4rA7J60J+3PskfQUFI UxPW6Ba+qLTR3IzvxNe8tjVVE+AYd6fUTT5gGATzpVuB1BTCJVCXszMJWWsLfoK7b4dcMU H4tDOXT2dBL7K1Mn0IpeOGSCvmZpTQnhuB9EDdbNA04jbipBuRDAZZYg4otlrEwamP/GHX d2+7Fd3tr81dkp37P3b0pWv5y6cW3USN6pXGwQIoixH17ZtuV6DkURXv46IQWA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062267; 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=psF57WLKdFqu9d22GEwITnNmBUegjhn1nfEG4ZPBrXA=; b=yoLu84z6rrJTIWXMD3aLk4dbgwAS/EFLZIa05jJ8SYQPQhUanOTFGf6+DF2mdXsdkWozuA SEmVuUjkwhLu4bAA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 18/45] posix-timers: Consolidate signal queueing References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:47 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -563,15 +563,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 @@ -597,9 +597,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D38BCC7EE24 for ; Tue, 6 Jun 2023 14:38:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238252AbjFFOim (ORCPT ); Tue, 6 Jun 2023 10:38:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238096AbjFFOiS (ORCPT ); Tue, 6 Jun 2023 10:38:18 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B284F19AD for ; Tue, 6 Jun 2023 07:37:50 -0700 (PDT) Message-ID: <20230606142032.095893220@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062269; 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=NkUrDTbG2kUXm4FwAX4frsR2bQohb+OmRpFyHE6t2mA=; b=3vJpsNCd5Cty9AxlHVIAAC4r6I8hNXiaYpKzMmQUemVg9DoLwOy5WiGazhw+1WqznMIonr cgmpaFC/pWVkbwMhxfiKohvbxtNUHKzO8h9Ykar5+D3cVgyPtCtkrq2xfVG5WrnGwVnrXg z4aULVl0zsL31Js+l0Rx+1lquD6nsZ7aSazjFG+8bHwI/WXTAs+6P89jKbOiNyvxhkGRYU 2gE4nWX0rTtcvxsbJQryz6Tkmw2zobEwcocwp17CV8kVkhJjZ0HF2gj6FP1kVArZhWIilO N8HJjFNqli/nezp9omLTO59XWsqIRqeMfIIheNVfJFz846u2MYZZ4azVZ0FxEg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062269; 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=NkUrDTbG2kUXm4FwAX4frsR2bQohb+OmRpFyHE6t2mA=; b=FOhRJm+RNuhNW+Fzm6R39QCjgVamcm3e4KM3EJudB2HxdLzadgjA+Nkpt2PHOYjQ6frDo5 U1fiiTT0AP6Pu+Cw== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 19/45] signal: Remove task argument from dequeue_signal() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:48 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 @@ -275,8 +275,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) { @@ -286,7 +285,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 @@ -621,20 +621,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) { @@ -2751,8 +2749,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 */ @@ -3606,7 +3603,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 @@ -3625,7 +3622,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6108C7EE24 for ; Tue, 6 Jun 2023 14:39:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238070AbjFFOjF (ORCPT ); Tue, 6 Jun 2023 10:39:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238195AbjFFOiT (ORCPT ); Tue, 6 Jun 2023 10:38:19 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61A2919BA for ; Tue, 6 Jun 2023 07:37:52 -0700 (PDT) Message-ID: <20230606142032.151323086@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062270; 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=Oszm2ZXBXWLXUTPr0v8LyoBnFHycifs2Z0aI1dsyju0=; b=D0ntuw5CvYsxl7SSpv78NSEO1OXove23skoVb9/culdydz2S3W/9AodOxMk18vwf/Sda8X PLJ+JN27N/9vZ/ZbfB4sfPzgijMO/Czl53luFkcn4sIF4hJJcmNPGHjzTsXxzCTuQXM/8N dVHffqboWgZ/JDXz5LZmHKgSfX30JvUl6sWrLznm6Gq8WUW9vvZvUIF7Ubzq7vYct7mHgY +oow8XxaFw958QPoqnCvgEGlpcHVj22vj1yHcS7Yg4+2vkOHWiq3s4w8X+RGdZSVeFy45v 5lYPgme3D0nI8L6FjetOVpodb/vsthI08TeP63e4ezMc159nfQ853OizEUzDEw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062270; 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=Oszm2ZXBXWLXUTPr0v8LyoBnFHycifs2Z0aI1dsyju0=; b=5htpoKmcrRXlS0Nl4wNjcwhNX8aRSMwzaKf44yg10U6SoWgH0XxX8TaMn1da7IsgGlX4ih 8rmcZVlqOixyvoDQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 20/45] signal: Replace BUG_ON()s References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:50 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- kernel/signal.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1932,10 +1932,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 @@ -1963,7 +1964,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(); @@ -1998,7 +2002,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F82CC7EE24 for ; Tue, 6 Jun 2023 14:39:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238060AbjFFOjK (ORCPT ); Tue, 6 Jun 2023 10:39:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238012AbjFFOiU (ORCPT ); Tue, 6 Jun 2023 10:38:20 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D64A110F0 for ; Tue, 6 Jun 2023 07:37:53 -0700 (PDT) Message-ID: <20230606142032.209201867@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062272; 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=y+RhxJJzE8uzstkBWfAPFhTwZDjjv6jfhmmgOqvJSb0=; b=gwgYgVQAUT+3KKpdi3wnvtWIKTUWUU1RrFh4glOGKxb8ztFzovqeMm1EggBrlL8G8jRujj 9sWrK/aqDnCG2lntLhiY9APg0B8wQBz0O6Id7xuWxfQGFCWHFvpUtMlNP1mF1qNlhpFp8s EWGimSiG+2yY5aeJbSqv5ewDg3tDYOxfCKTNH+Z1pGC4juRBP5mFPGdBWqsSgilptkrNwW H7bsyawQ20vNZRJNJy0x3MoAIArY6AbBVsv8M/P4/YjCXLOt/C0PW9PwHVca16uYqq9iW3 8A9sHCbP4Uhv85Gp68cCl25nO1tduw2Kq47p5cekTq//wf24iFKkeDeSDixRWA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062272; 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=y+RhxJJzE8uzstkBWfAPFhTwZDjjv6jfhmmgOqvJSb0=; b=ljl4qT+fsd3lzxkk2vZtMdZ2om0ov9TE6cQQfxnx07U26+XlAHdYyV98BPjTx6oEslQGJS VdQh1J6J657GerBw== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 21/45] signal: Confine POSIX_TIMERS properly References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:52 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 @@ -162,6 +162,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) { \ @@ -185,6 +187,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 @@ -259,5 +263,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 @@ -485,42 +485,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; @@ -639,31 +603,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(); @@ -685,22 +627,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); @@ -1916,15 +1848,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); @@ -2021,6 +1983,7 @@ int send_sigqueue(struct sigqueue *q, st rcu_read_unlock(); return ret; } +#endif /* CONFIG_POSIX_TIMERS */ =20 static 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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF030C7EE24 for ; Tue, 6 Jun 2023 14:39:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238156AbjFFOjO (ORCPT ); Tue, 6 Jun 2023 10:39:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238130AbjFFOi3 (ORCPT ); Tue, 6 Jun 2023 10:38:29 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C07791BCC for ; Tue, 6 Jun 2023 07:37:55 -0700 (PDT) Message-ID: <20230606142032.265274690@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062274; 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=pTwgQriJEXYz/BGSH2oQaAJz9q8TYA27uj1w2y/zcJc=; b=0IRnAHwIhJIqglZvpXDrkZu/M/QwBgEWV0yLKYPAXoPN4kfedaIOLiuO2Z5c5hn3FtPUuu QGE7N7o7rl5iQg6iHrhjKFaYtTqIYjNsJ6DOC5S4bM6SAHBATUCGLkAfbT6FK3BzBe5nFL Toi2QxVGSgU74Ak5v8wR34tC742JhdqyFhqjxrji1CFbDYfrtvN7/O7kEy7xIlJgu+BgAm mXFgL6K7p6gPkpg9uQ87lk9akugxVrSUwIAa8xb2ye54ygC3fodE8dLn+4A76CIHznpTrr sEcWOjihR7swEfGfh9xix9cOzwsBw/E7KojqlxpE3vxK3kVlvU5nDusbj6TCTw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062274; 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=pTwgQriJEXYz/BGSH2oQaAJz9q8TYA27uj1w2y/zcJc=; b=JRFbBcCjP4x4iocN+ozTdkGi1p1nb01JhhQ6f5PJp6rE0q/eY9U36wAeEw+i+XMHN5fnK4 KpBY5EooAmYsNODQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 22/45] signal: Get rid of resched_timer logic References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:53 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -529,8 +529,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 @@ -552,12 +551,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 { /* @@ -574,13 +567,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 @@ -592,17 +584,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); @@ -629,7 +619,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 @@ -1015,6 +1005,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E72EAC77B7A for ; Tue, 6 Jun 2023 14:39:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238247AbjFFOjY (ORCPT ); Tue, 6 Jun 2023 10:39:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238167AbjFFOic (ORCPT ); Tue, 6 Jun 2023 10:38:32 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 691C61BDC for ; Tue, 6 Jun 2023 07:37:57 -0700 (PDT) Message-ID: <20230606142032.320851262@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062275; 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=H0SHtIHKvGNjccihe5bl5KJFGvU0rvv1sAUDjIUVmZA=; b=xu+LjC6FcJ1D7kEM2up0mLuCoSwJNM/DGQub2BCOd4pms81P6znJhC3mipA9MCHfWSSfQ6 nLLV7s3Qnj6R4CZ3oicRu3KRRzvgAD6Qek4KAp/h9o7GT29H4vlAsUN30CDYNKJjazs146 PSOpfDH9bxwGV/aA+3z8XnuWk9jjeV7bUCvEbjhOvQ3wv/0PCPub4A94lNdvXYMJoD3jD2 qg1OGNKqWrNbCjdZLii6CwZAy0bsShKFN1GAqqZqxhMFZokduyrpGnWFOsHD/voFJ45rxI OL2NZ9Afkyn+6Wpaotd6bYKIUfmrEQoiG0Npd5J0uutFjBvWEUNuXLUyKia6uQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062275; 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=H0SHtIHKvGNjccihe5bl5KJFGvU0rvv1sAUDjIUVmZA=; b=29FxhteEWsRBzyVrLTeHNbrXTrB5L2OGD6EO2aYPwHO8ZDj9MiYQjhFnv9Nf9lkU5MCT3A emcm6vEe2z8gzICg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 23/45] posix-timers: Cure si_sys_private race References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:55 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -348,7 +348,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 @@ -1911,7 +1911,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; @@ -1946,6 +1946,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 553F0C77B7A for ; Tue, 6 Jun 2023 14:39:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238259AbjFFOj1 (ORCPT ); Tue, 6 Jun 2023 10:39:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238232AbjFFOie (ORCPT ); Tue, 6 Jun 2023 10:38:34 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AFBF10F2 for ; Tue, 6 Jun 2023 07:37:59 -0700 (PDT) Message-ID: <20230606142032.377649534@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062277; 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=FsQ2RLjA0ULPK1LuVh79qQG566mUAal9FpthIwhTUsc=; b=Tmh6xJJN3AmL9nJl7HXapdxc/ZScy95W8+cLDx1L6xE+OP1B2vQPjMLL+5svUX3zoTFAdT gncpwf7lXSp9Ji272ARmG3jMJ2POAqx47PEe+O6pbC62uP1i8RHhb3YpokscJqRUswAWCc VYBDoVqX94siQQ1l/gGSWYOAfMfaSvlt28l9sS0Tp+127j8OnAPpi7pzMsLuA3dgcz4KU8 EPKlkqh/1D1C8Qw5P8QSTShe/ri42DmDm2gA0TUY1FtM02qrEI5yHNZfL3e623giI1omF6 fpnxrcQ1X3iKH2jsmleBx0ROWU8cnw0c++oOho0RNttnba5Ou8bKsRcoYT61SQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062277; 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=FsQ2RLjA0ULPK1LuVh79qQG566mUAal9FpthIwhTUsc=; b=PL0Wz+ZPwNKudLjbFeGDBMcUIJtGwB9cEhmWlmGb5tmx9eStFY3LSEeHX2P7CH6jsbif4L fG2953MQDdo3QkDg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 24/45] signal: Allow POSIX timer signals to be dropped References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:56 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -162,8 +162,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) { \ @@ -188,7 +189,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 @@ -588,6 +588,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) { @@ -619,8 +620,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1265C7EE24 for ; Tue, 6 Jun 2023 14:39:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238209AbjFFOja (ORCPT ); Tue, 6 Jun 2023 10:39:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238183AbjFFOit (ORCPT ); Tue, 6 Jun 2023 10:38:49 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58E231BE4 for ; Tue, 6 Jun 2023 07:38:01 -0700 (PDT) Message-ID: <20230606142032.433429880@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062278; 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=3tnlpwZR7AOO7Yh1fmxAmC8HLkEI6QD76zGVnsBJknf8UMe5AeHLON600517dH6ZfYHN1t EsBV1RT/DYKL87zBhaT5vrA6XEstqCzGt65cKoBch1fUhqGgkU0qJFAAFHmZG+cuw8mxxM vTIynumi25/W/Ey+lLIyrp8rJAj3Otsm1xnxe/uPFsybfThD3/Jl3yakh/8aC+J1+oQ21b sCxb0iDMYure57654loi6WY4SlZSuiJ//iQ7wp0Xz94G/FiHDrYQq8FwolQVk48G2kPCjd JuVT+hfISI+Am4uRRb4esCZptkkEH5WdA2cnn263pWvRwpz9Pkk51VmspWig5w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062278; 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=zuo3CLae5BOp6gIZnvW7WN6SahTzqoMNx2aNczVT7msm1Y6lFSCoT8iMQb3JZiQXA9Wm/F ZFr6Z0bcwUN/2uBg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 25/45] posix-timers: Drop signal if timer has been deleted or reprogrammed References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:37:58 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 259CFC77B7A for ; Tue, 6 Jun 2023 14:39:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238303AbjFFOjf (ORCPT ); Tue, 6 Jun 2023 10:39:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238107AbjFFOjE (ORCPT ); Tue, 6 Jun 2023 10:39:04 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8DEA1BF2 for ; Tue, 6 Jun 2023 07:38:05 -0700 (PDT) Message-ID: <20230606142032.489307214@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062280; 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=9SYjUCAe0AgsqCGuTWf7t+WUqVVywcl4/ZGVc9cUSlA=; b=qxu1lZ9377m1VdXrFDK7hm0MCDbgZuf1oa7cKHpXv9jMupkRKDli7MoIf4AeWKjuPEuWPd 9HjzVBhABZ+JqAiOrNP7c4ViUqE7MgiHSt4LSAEqRJoyVpyjYcXTiMlqLzYMoxsMl6iVVz WSyKN2iWFFR1oGgYONZoIe1/lzhxvYf5mut4FHNSrJVnq9nzukQjRELWq3P3wXp7rK6fNr 9a+0J0Dkm87qeVVlj2N5iTSDvUKu3SmZ1s+ao3oU3A14aOQ/ZWwHyUa6yNsb3u4kzqHvV7 FrGeZZVjmyaHIGITJnuuI/KCbN/iwsBRzA0NEksuMBlXWXrcwhxvBthG1AERGQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062280; 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=9SYjUCAe0AgsqCGuTWf7t+WUqVVywcl4/ZGVc9cUSlA=; b=x/Zj3IWC//rixGZDrq+hF1gwDYu/SR5Sf3JVr45VCeoe7YU3bpROw7BBg8TGKJa7FweLbz PO9GHG/GXN45WWAQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 26/45] posix-timers: Rename k_itimer::it_requeue_pending References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:00 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 2 +- kernel/time/posix-timers.c | 12 ++++++------ 4 files changed, 10 insertions(+), 11 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -213,8 +213,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 @@ -235,7 +234,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 @@ -573,7 +573,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 @@ -607,7 +607,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 --- 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; } } @@ -662,7 +662,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); @@ -863,7 +863,7 @@ void posix_timer_set_common(struct k_iti timer->it_interval =3D timespec64_to_ktime(new_setting->it_interval); =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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4995C7EE24 for ; Tue, 6 Jun 2023 14:40:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238285AbjFFOka (ORCPT ); Tue, 6 Jun 2023 10:40:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238279AbjFFOjU (ORCPT ); Tue, 6 Jun 2023 10:39:20 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 576E31FCE for ; Tue, 6 Jun 2023 07:38:17 -0700 (PDT) Message-ID: <20230606142032.544511403@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062282; 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=5ed1VkOh2UBEVrURDClugUqs9wtwWx9MqD/9qEBm3z4=; b=SuKcaX/uHCB7zYDh0doxbGCGrsnwgsbNatkptPv4YN63eB3SI+fQBRMq82+9X4EFM3OX+d Q2aLgkzG3HLJ7+D5/bvQlaB3bL11ESuo19w8/hYUiZwt3mSp9TWAVxafl60oWkz7+H91+E srSXZRI5FUVOyQZuK7uC1Dh6dvQv2dVY+hCX4zLHmot+wvuHplX074nrPq1EBsUUzloub4 5EoleiXrscDNFk0Zw5vecrIt8UcSs1Xmb97R/1dbfj7p5XD9tF2Wh+oD0K55ogMLQFKhVX 18GxPs8IylsMWpmnDC16vZ9T6wOB5amVLNUae6Xz3YALjUdzr+pxOezVnRgyFw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062282; 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=5ed1VkOh2UBEVrURDClugUqs9wtwWx9MqD/9qEBm3z4=; b=pva/uGMhqiSbwG4R61C+hTFA66LNiX0FDV8a7t7kF753w3k3Uv6gPo7UxGDBcSDwoBDWN0 SFtTkR5NoQ/IgiCQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 27/45] posix-timers: Add proper state tracking References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:01 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 14 +++++++------- kernel/time/posix-timers.c | 22 +++++++++++++--------- kernel/time/posix-timers.h | 6 ++++++ 5 files changed, 29 insertions(+), 19 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -210,7 +210,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 @@ -231,7 +231,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 @@ -574,7 +574,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 @@ -670,7 +670,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 /* @@ -804,7 +804,7 @@ static u64 collect_timerqueue(struct tim rcu_assign_pointer(ctmr->handling, current); cpu_timer_dequeue(ctmr); ktmr =3D container_of(ctmr, struct k_itimer, it.cpu); - ktmr->it_active =3D 0; + ktmr->it_status =3D POSIX_TIMER_DISARMED; list_add_tail(&ctmr->elist, firing); } =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 @@ -642,10 +645,10 @@ void common_timer_get(struct k_itimer *t bool iv =3D !!timr->it_interval; ktime_t now, remaining; =20 - if (!iv && !timr->it_active) { + if (!iv && 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 @@ -890,7 +893,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 */ @@ -903,7 +906,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 @@ -1002,7 +1006,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5725C77B7A for ; Tue, 6 Jun 2023 14:40:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238344AbjFFOkm (ORCPT ); Tue, 6 Jun 2023 10:40:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238281AbjFFOjY (ORCPT ); Tue, 6 Jun 2023 10:39:24 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6CBB173E for ; Tue, 6 Jun 2023 07:38:23 -0700 (PDT) Message-ID: <20230606142032.599847383@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062283; 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=IE+/o+H4G+/DJ08h5MOIwSs6Di0NMd0uAJ8BN6taE4E=; b=zIJxWHdUqM4n7bDb1/OCIIoG2UlagFbiwmxrqg8dvkJ5ygXMH7gVqE4XowoOn9QGXa+/pH /DauAZgropTMZ552OMqoZZGes+EvSCLW65weFBD3kRg/96tWgQIxK3cRedD/qX4FSekFFV GXLj+zaD6CXsijyqayQizYSYclMKkIVfz5MGTKRxjDZj7z7NJ6as/lImmj/7v9VIlNmYOm BPnIGg3bJVs6eC8ImSwVEj3hG1408+yjUAaJXaqVovuOsP1v/zTrWBf4HyywVaqwDd11SJ MqHKGzV/DQaTyjdcAuIpvS2BPUCIXAE+SZ8081xRyuOupldHJUqj+PKXH6t/Wg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062283; 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=IE+/o+H4G+/DJ08h5MOIwSs6Di0NMd0uAJ8BN6taE4E=; b=wKlW6c9/tmJd3EDbm0eBxWX51b7a+W86UZZgl1eeqOI4mOlHm5BIph2/T5pfwQWr0nISIY Kfgex+wV+4YEniCA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 28/45] posix-timers: Make signal delivery consistent References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:03 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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-timers.c | 25 +++++++++++++++---------- 3 files changed, 16 insertions(+), 13 deletions(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -200,8 +200,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 @@ -620,7 +620,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-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; } @@ -665,7 +669,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); @@ -865,8 +869,6 @@ void posix_timer_set_common(struct k_iti if (new_setting->it_value.tv_sec || new_setting->it_value.tv_nsec) timer->it_interval =3D timespec64_to_ktime(new_setting->it_interval); =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; @@ -884,8 +886,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. @@ -935,6 +935,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; @@ -1003,7 +1006,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; @@ -1031,6 +1033,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BCE3C7EE24 for ; Tue, 6 Jun 2023 14:40:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238191AbjFFOkX (ORCPT ); Tue, 6 Jun 2023 10:40:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238266AbjFFOjH (ORCPT ); Tue, 6 Jun 2023 10:39:07 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FCB1171D for ; Tue, 6 Jun 2023 07:38:14 -0700 (PDT) Message-ID: <20230606142032.655376396@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062285; 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=CLYMwgJmr5FlvwjCFRZh30bCjVfx+q+Z6l1NHlqA5Kg=; b=SBEkWjjKOPc5r+5qv1tF0CLdempgLMDgk3cqd3AUuEo9hq8jZKn6O3sLLj0nysbfM68AYn VikIQ8Zln3DJFEQyrO8rAU45wcbGPtUUHnJDPSTrsLR/EHe/Q+FByVe3EfMAO4ntt05wBz IMPaxCElQF7f3rpKXvZLfYJZwvTcIjsl3nIGGxvbPOnexQCQu+XuRrreGtQYgQ8iDSMOFJ XkB4GmB6/R4Qj3LdbTQYkT557pSh2o3wqC1padUFRnRJsbGMdq2ZR7RbVGwWdm0IyyfB0Y SJfuSEVhr7SUTgvVfJbOTwNF8qzx3PDPf6chFeCTao3G1pVQpnn/gVH7QN0jyg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062285; 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=CLYMwgJmr5FlvwjCFRZh30bCjVfx+q+Z6l1NHlqA5Kg=; b=kCwRxsDUwnRH9F3ouDI9aLA4jIZh6Raet5hysFRmvvgVfj9nsm6Vn789ZsSIWMFM5RAHaA P1eLYe5cCVClT/Cg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 29/45] posix-timers: Make signal overrun accounting sensible References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:04 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -1957,6 +1957,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)) @@ -1964,15 +1992,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77698C7EE24 for ; Tue, 6 Jun 2023 14:41:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238377AbjFFOlO (ORCPT ); Tue, 6 Jun 2023 10:41:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238384AbjFFOj7 (ORCPT ); Tue, 6 Jun 2023 10:39:59 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BF811FFC for ; Tue, 6 Jun 2023 07:38:44 -0700 (PDT) Message-ID: <20230606142032.710699683@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062287; 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=tGjZhkMVQyRr4XnKiVSbNfTqylPySohR9BvGVWy+/yU=; b=Js0F1AHEaIizBcWa7NPJK4AlSsrHSTctCJtwd7EXPcaUDu6Iw0MFE4A2cVWrts6eEMwuLg F0D+hYNNMgqX6YW61nxInrcWTjNchojQ0rjcHf/BWCmOdcvOiQv3hN763kS6Win12saETu v4bxvCeUQkEe0talgjedYqgUHMqzQvVorHTMwki7YpcRa2qXLZyI7d/qkfQ89IbnjWs3Og FGXLTAUUbYTYdE42MXg5P0RzT1CekezHUrsHZmXWPgcI6ALGDtnVzhrL9fUMzrjPi+gCcf /slyhGEhuzLxSk33oQ9jbmqf+Kfmfdwv4plghTsCMlyw0VtxgEjEAguNo90zRg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062287; 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=tGjZhkMVQyRr4XnKiVSbNfTqylPySohR9BvGVWy+/yU=; b=4JlCTJy514LpWzmZGXVL9Qe2KOlRSWplLy8k9tNqv1tE/OCU7Bc0O3PvAhCBFshCffOYrT MeXFQfA9/r/LRqBA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 30/45] posix-cpu-timers: Use dedicated flag for CPU timer nanosleep References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:06 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -66,6 +66,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 { @@ -73,7 +74,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 @@ -586,7 +586,7 @@ static void cpu_timer_fire(struct k_itim { struct cpu_timer *ctmr =3D &timer->it.cpu; =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. @@ -1471,6 +1471,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99AE0C7EE24 for ; Tue, 6 Jun 2023 14:40:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238278AbjFFOk1 (ORCPT ); Tue, 6 Jun 2023 10:40:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238139AbjFFOjV (ORCPT ); Tue, 6 Jun 2023 10:39:21 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC4AA10E0 for ; Tue, 6 Jun 2023 07:38:18 -0700 (PDT) Message-ID: <20230606142032.768353204@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062288; 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=WBARrxgJ0Fo3O3pXIjgjYSXb/czDjqPuA7NhGPJIs00=; b=oEOxb0sUym1Pwy7Z9NDCrzBqaCSSmmAuNYE8cNhD9uSA2Muk9RagSHTmhGzOUEUlWgY6mh Gh6EqnielU4dcz0Ho51Yng5JazPPiIWqHbA/EiQvyPzKjh+c+NaWZjzfA6XffeZwNJZRvn cGsHnK+TGhN1FzHt4cyF62HJCIHlokHGgqa07A8imVK6kY5bdtdEErPjSUxk0lTiyMvZwg kHR1DIJXAabMZVX4mksvN86FGlGiRfQi4f1EqtWH7evD0FwPlOz31EX4syi6IKLj0WUxQ/ +3xkOPBlEEj+bgzm18F0jdJCAMUn+dg+CUL9qpsSbxljvgRr28/ITciir3PY7g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062288; 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=WBARrxgJ0Fo3O3pXIjgjYSXb/czDjqPuA7NhGPJIs00=; b=EiH4wIJOOX9gXUPVuG+4+zy6hy9FQTz/fYU92XeTosFRTHiKSnOBst+MAfEaLsjuTBN8dH sh5zPjLfVn7Wc8CA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 31/45] posix-timers: Add a refcount to struct k_itimer References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:08 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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,10 +6,12 @@ #include #include #include +#include #include =20 struct kernel_siginfo; struct task_struct; +struct k_itimer; =20 /* * Bit fields within a clockid: @@ -167,6 +169,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) { \ @@ -192,6 +195,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 @@ -219,6 +223,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. @@ -243,6 +248,7 @@ struct k_itimer { struct task_struct *it_process; }; struct sigqueue *sigq; + rcuref_t rcuref; union { struct { struct hrtimer timer; @@ -263,4 +269,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD6BAC77B7A for ; Tue, 6 Jun 2023 14:41:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238384AbjFFOlR (ORCPT ); Tue, 6 Jun 2023 10:41:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238454AbjFFOkL (ORCPT ); Tue, 6 Jun 2023 10:40:11 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4FBA2127 for ; Tue, 6 Jun 2023 07:38:54 -0700 (PDT) Message-ID: <20230606142032.826604145@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062290; 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=wHuT7Hr0NdOKLI1EunOgn59lsnWIJRlvleS26aoiaoA=; b=tl52fd+obPeHQXAjdOtL/sLgOTRfX+FOkkGpvpvRPKOjZH6KoaVfGmKYfD/ixurZHJq2aO p9b15raE24WnbUgpqOWt49PpPr4S/Xj91olySxUAnLLT19vCBUZTrzFOy1JsVMM3C0gMCV Kj78hiY2NhIhJNP+6kxOiRArkqZt1Q71wFNmm5TmA/vCbaG9vstMsUjbu9/tNxUa1ULaLJ OPHNxDm+w9CZgFwXjrPDFEpzQ4QoqCZIKzYHfQKCwXaQj2mYwbGIZkhKdgyJ95SWWbFxCI 7mSNxPw0qqixkzC26R8FNCjKgDwECT/6KhlSIuXysweXwKUEKeckVCddnfKK7w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062290; 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=wHuT7Hr0NdOKLI1EunOgn59lsnWIJRlvleS26aoiaoA=; b=tKLxhZYkjMhy1U7P7k4zoE3Aq3+VKrKcHWCwEdcwwhgUinWe88yTvqhy/x4nyfdjb1Q9GJ E1Zgn7CDIDpelQCg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 32/45] signal: Split up __sigqueue_alloc() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:09 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -403,16 +403,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 =3D NULL; long sigpending; =20 @@ -431,19 +424,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E8FBC77B7A for ; Tue, 6 Jun 2023 14:40:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238293AbjFFOkd (ORCPT ); Tue, 6 Jun 2023 10:40:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238231AbjFFOjV (ORCPT ); Tue, 6 Jun 2023 10:39:21 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04A421730 for ; Tue, 6 Jun 2023 07:38:22 -0700 (PDT) Message-ID: <20230606142032.881951490@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062291; 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=F1AIORQBsb8Q0wbN0ersqX3Q8WiAIjYPdK55avtTgbc=; b=zYwP+c1d0TKBY7umwUqJyZjwLRc75LhgtytLBn0aq2G7R+AEjzhXBYIIc0fMDCIezfOwju EfDzX3nUqEs7bEu4nFl8TNqFvnUb3gEV9jdmdsOKAiiVVWG+sjpYC0yoI5AT2EpQrAubF4 8U40jBTqyal18gawuojiioVpDPLa7G1irpHhsrUayCyedCUUitLHCjcz8nO1WV+pKkotKP oUbnNHJNKUMgrt16vS9OHNRm5n67pBjDPk1x9q6KEqWwqdCa8jIK7mMjvtPJDVxYygNgTs hVTBSjN1kruivBXxTnTLJTS7oHHj5Dy3Koz/4kh+tjAUjTPsJKTtE+HlZd2GYw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062291; 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=F1AIORQBsb8Q0wbN0ersqX3Q8WiAIjYPdK55avtTgbc=; b=PXGS/1RDSbPqccGdfDQYlb/tXnCiLRy+k6oowj4ENqJgwDUvgVlmb55JC5BmOLQiKgKG7W PU5cWxeT8xHU+vCQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 33/45] signal: Provide posixtimer_sigqueue_init() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:11 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 1 + kernel/signal.c | 11 +++++++++++ 2 files changed, 12 insertions(+) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -168,6 +168,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 @@ -1901,6 +1901,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9EC5FC7EE24 for ; Tue, 6 Jun 2023 14:40:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238320AbjFFOki (ORCPT ); Tue, 6 Jun 2023 10:40:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238072AbjFFOjX (ORCPT ); Tue, 6 Jun 2023 10:39:23 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22FF91733 for ; Tue, 6 Jun 2023 07:38:22 -0700 (PDT) Message-ID: <20230606142032.937376270@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062293; 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=X5/mGdJCXgY2x3hbkFl+DcjtQwggjg9WtZyekAGVZm8=; b=ccXf2YoXP6bmaxL+24Dyu26Jc5EOOnCAU1rwP+7LXMT7NQbtNp7ZqfMgccuylPEE0zuJQA TNAeh3IEOxnWArQhCcIkCwzOqXAupwepk/pBSqxzynyZnwyXdq8wUOI8dGHcBkmw/WGtaX 31Zjsh/Ky9/zJpIxwq/2+9AxNR0ZhRMgUwrRGNxBzv+3OtPc6q/vooEleyMWh2QzGNhHWo 1ABi8DAkkAwxPqSo8kS8mUWPcoM1BGVzcn2SFn2eyV2YdqZsYdPjjptknUHkSKEXQAsAAc FbO9C2bl+unpCOF5qvDuxGbl3TG0/3Fdd3eqrwkK8/FkbQqJsswN1HyfD8VWFw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062293; 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=X5/mGdJCXgY2x3hbkFl+DcjtQwggjg9WtZyekAGVZm8=; b=X788qT6ZGfh/f6eNpLqqkZA8SRx6637+/DkoOQMnZ6oUQzeYu5Y1kc3+Q2QeVwqyIknaG3 mBo/xp4ickMo10AA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 34/45] signal: Add sys_private_ptr to siginfo::_sifields::_timer References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:12 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -450,7 +450,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 */ @@ -151,6 +152,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D356AC77B7A for ; Tue, 6 Jun 2023 14:40:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238339AbjFFOkk (ORCPT ); Tue, 6 Jun 2023 10:40:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236184AbjFFOjZ (ORCPT ); Tue, 6 Jun 2023 10:39:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 582421984 for ; Tue, 6 Jun 2023 07:38:24 -0700 (PDT) Message-ID: <20230606142032.993368223@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062295; 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=g7l/2zlLif3HG/2XRk0djJlztUlSNJVVihTa+AYF0Ng=; b=DY64q94Gr7wrbcshB8A2DNDl4W2LY5jV7sNZq7ieAT3C9PxCNyFWm+ECQQovXgpGL0ZOGN wNL0y4ypWKpchpDU4O2ePECiJuF4LdD2J4K4sez9VlJGS2zf5+6+pttuejY5WMn0wNRpOy UiFsqOR4a7HuThqyK4RtfeBEx2WEefOZrAVJSlpYze2HK1P2e19fp3SIRImFHRA4nklXjk Dy1ppYjZ6z0QuCftVICJiHoPDBfdGJsPxTM9YFIQ9xn8ITHVyKhrOlZ6XPzang/4143qtQ cz9Qc6jsQhzZ0gGfkhvO0ioNRACcoaGXsguk2ArKi3U4Y71s7jCSs38XW+hdNA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062295; 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=g7l/2zlLif3HG/2XRk0djJlztUlSNJVVihTa+AYF0Ng=; b=kMg9UwkfddSxmVr4V0gVW7txrjdRA9kem0+WWFZ5TPnH/Qv3jMUURv5SkYZZ/a0mg6cnA3 vaYPPloZYtgAMgCA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 35/45] signal: Refactor send_sigqueue() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:14 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 4 ++ include/linux/sched/signal.h | 1=20 kernel/signal.c | 73 ++++++++++++++++++++++++--------------= ----- kernel/time/posix-timers.c | 10 ++++- 4 files changed, 53 insertions(+), 35 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 sigqueue; struct k_itimer; =20 /* @@ -169,6 +171,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 @@ -242,6 +245,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/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -348,7 +348,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 @@ -1943,38 +1943,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 @@ -1984,7 +1999,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 @@ -2015,24 +2030,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 @@ -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 posixtimer_send_sigqueue(timr); /* If we failed to send the signal the timer stops. */ return ret > 0; } @@ -504,8 +502,14 @@ 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; =20 if (copy_to_user(created_timer_id, &new_timer_id, sizeof (new_timer_id)))= { From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C90DC7EE2A for ; Tue, 6 Jun 2023 14:46:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238163AbjFFOqI (ORCPT ); Tue, 6 Jun 2023 10:46:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238848AbjFFOpk (ORCPT ); Tue, 6 Jun 2023 10:45:40 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2E131995 for ; Tue, 6 Jun 2023 07:44:26 -0700 (PDT) Message-ID: <20230606142033.048775731@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062296; 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=FbfX95bonQ6QuMNiHBaPdEVKsEDfcrvq6gge8bQAWbs=; b=qlPPi2zJ1gOLSHTvVnbN6QCmF6iqPsLJVhZeEwc5YDBdAyQLMp416H3yJCEezDSZnvKfTX hyooItR3CpENkV5UDazbBwUjcGIya+Az8psFl3glrZ1jrtJtIxB9Fu+6syIbjOr7zpA7Cq nCyxdYVLopIwnxhvUDeaj1RDPM2pYklFAo7yIvc99hkCPLsrNhVrSd7NtieS7Ok1u+CjZu CSp7DrEzYcJVUuFnkWoAZI9Y8pUqfa0gBA/eetCjEa1pmGG3BNUTm9+j7bvY1/at6D2P0G 19k7eLR9TDaZUbhlLcr8uldK+n7Orh+g+8UgByCgFkiVpD1viADjgwwReqXltw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062296; 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=FbfX95bonQ6QuMNiHBaPdEVKsEDfcrvq6gge8bQAWbs=; b=DNdn4sddi0bwxcS7c+BucGdlXd/d1WavXafOwXOJF33YNAMK6P0VbLjtzLhCI5le5LfMMs lzNAd6E0WvZ4bEAg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 36/45] posix-timers: Embed sigqueue in struct k_itimer References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:16 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -2501,8 +2501,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 @@ -63,6 +63,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 @@ -228,7 +230,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. @@ -252,7 +254,7 @@ struct k_itimer { struct pid *it_pid; struct task_struct *it_process; }; - struct sigqueue *sigq; + struct sigqueue sigq; rcuref_t rcuref; union { struct { @@ -280,6 +282,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 @@ -569,7 +569,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 @@ -1977,7 +1982,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; @@ -2037,9 +2042,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,10 +513,10 @@ 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_tid =3D new_timer->it_id; + new_timer->sigq.info.si_code =3D SI_TIMER; =20 - new_timer->sigq->info.si_sys_privptr =3D new_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; @@ -594,7 +600,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C377C77B7A for ; Tue, 6 Jun 2023 14:41:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238326AbjFFOli (ORCPT ); Tue, 6 Jun 2023 10:41:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238348AbjFFOkq (ORCPT ); Tue, 6 Jun 2023 10:40:46 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C27301BCB for ; Tue, 6 Jun 2023 07:39:10 -0700 (PDT) Message-ID: <20230606142033.104268005@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062298; 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=SKWTs7mKb9Ib3ce5RFjSWFvKB4MXfu0NjoxHJuv9oio=; b=IATYFsnS0gO/sbdPVKjHqcBUnjyvM8CNZDC17HGay7W1U6Amt/e/X2eEeS/IpQqcqi5FQd fSTsFDp3mMmGe2BSfdTfWrotDoOZrOFzIwWh49VAPLpdJcJnHnYqoWwURvhBVa8hMDCCyu nTaKbRO5vGAX9SwyZRvJSL6UrcJuc5lJCd4jpUspxAqrm9oIxPjsoNUIZd0Lo6cCyT8Cfl 1Ydw0gw83gEeb/zO0h2Vr400N8OpOd01pjzlFLLEcHqZmJauCOddS6Im/3192QkO6N/FYW ngi/62P2zi6/091XQa5xV94bhrG4Dtap26uQmuixdKwlUfh85CpHdiz2lDaMzA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062298; 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=SKWTs7mKb9Ib3ce5RFjSWFvKB4MXfu0NjoxHJuv9oio=; b=mu8ZLkR72aeYjlFHJHr7VBzPy3g6BhNtw2qEPd8eUo696YIlNoe6HtSw1TOW4NS10lyqg9 s1Sc1HgVcuCPAOBg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 37/45] signal: Cleanup unused posix-timer leftovers References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:17 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -346,8 +346,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 @@ -446,8 +446,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; @@ -461,14 +461,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; @@ -1069,7 +1071,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); @@ -1917,37 +1919,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF1A0C77B7A for ; Tue, 6 Jun 2023 14:41:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238386AbjFFOll (ORCPT ); Tue, 6 Jun 2023 10:41:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238354AbjFFOk4 (ORCPT ); Tue, 6 Jun 2023 10:40:56 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A80911BD5 for ; Tue, 6 Jun 2023 07:39:11 -0700 (PDT) Message-ID: <20230606142033.172901153@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062299; 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=jyhJ/3GMU7sDY6xXeRfAzgG/VNBQy0NUgflCvHfEUKI=; b=XJilMgHM4dy9cG9v0F6jyIRHdq6ACnil9FvydviHNHu7inyWZ/fx45hSDqAuNz94cKj0vE enpsGWuvD3DGfZjgYaBI9R5lV3TBjwSQBJSg3GiXnc0Vx2kerYmC2qYG9PtBsJpcYQr6Fk Cps1voMigj6U4UKBV49hXW8apAh3Rif3C51aPFhWeWN3QL6VHUFsRqJSDO35WHmrAs/T1B NHOAQPuPSYEhLjGCN5STeqRPp/dMAVHUq4nIFZstODIQzMNo1Tk1UE/XThfM1krMGuMf4h VNCJcelb2WbxdzWdxlwPI0NN+W0g/+bmtFU2GoqPO17U5T50yBUNo+xNRCN58g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062299; 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=jyhJ/3GMU7sDY6xXeRfAzgG/VNBQy0NUgflCvHfEUKI=; b=RLxZMsOTpnVTE7P8NlmT+H5U2JMs2DASwAD18xvfifkr75/k6ysGl+21VdvBYKC+2+U4/e 8OuAtFyJ3UcNJkCw== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 38/45] signal: Add task argument to flush_sigqueue_mask() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:19 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -727,11 +727,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; @@ -869,18 +868,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; @@ -4097,8 +4096,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); @@ -4165,9 +4164,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB410C77B7A for ; Tue, 6 Jun 2023 14:40:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238347AbjFFOkq (ORCPT ); Tue, 6 Jun 2023 10:40:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238254AbjFFOj0 (ORCPT ); Tue, 6 Jun 2023 10:39:26 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D35C91FD8 for ; Tue, 6 Jun 2023 07:38:27 -0700 (PDT) Message-ID: <20230606142033.225765113@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062301; 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=FIVxxbxY5nm7gn9Zivm/IXjYdCBMNgvVCHW/aP5AbxI=; b=G6i/iDvAKRjpnjskrO4KtqHQuYJKJRrXwL2YweYtiuegs0NCLAy6Pb25JWXlFoQwMKyfpR 1nRwTmeynWyQ4FMaNIv5tDh+79wZYFWD31QE57op231XuvU37+E/JZs8aMd/eV4nTWMtrC cSoO1FqNmROB5HyQI6MP98mhXwmjO8W4CM+1W6BrKA3UYPJMrtLf98GHN9i/CxU9IxWhq7 NpTmwR769D1GgHACm2qoDWQrWjBQsOmN7RyHGBe1iTcHqS6nDcVrMAE/mTD+KJ0aspBt5Y 2XRZAzo/z2Wx6OIMcm/j+qk/PEFXZLct23pJPTZac0jmRFxVqXZrd2IHQX5hqA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062301; 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=FIVxxbxY5nm7gn9Zivm/IXjYdCBMNgvVCHW/aP5AbxI=; b=Vge8TkXGkw3jGg0X9UkQfMUAPmd57LOptsE0K6Wi3GYHsXdCwD/sfyqwSZmzU1CooGTGkg R6GpuyzvnFWsO8CA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 39/45] signal: Provide ignored_posix_timers list References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:20 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -137,6 +137,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 @@ -28,8 +28,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 @@ -1881,6 +1881,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDC67C7EE2A for ; Tue, 6 Jun 2023 14:40:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238308AbjFFOkt (ORCPT ); Tue, 6 Jun 2023 10:40:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238096AbjFFOj2 (ORCPT ); Tue, 6 Jun 2023 10:39:28 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5535B1FDE for ; Tue, 6 Jun 2023 07:38:29 -0700 (PDT) Message-ID: <20230606142033.278111872@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062302; 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=Uv3+NRg/Kf1yCAL/nMP1t/I/1XH205FZqRDMC4g6F7E=; b=Nt7Cp8cks3GA6mTuqXJmwXQsjXCEORcSPqUYS7EZkdTdEUAPwg6tMSXVbab6r4bownb+1I xOvqJUeBBA+eRMtdMWauNtYTmPrcXPFSOBSD5WCeWES6yZyG6gS8CyGS6zISG0nZaKPl5a VboyMwT5HJHmvVIJQ0FvVydOxhLx1akpwG+Vt0RHyFl3TrI/N4AJx1nsu+jNQ+wYXHhKwd TYlqOpe4ijaMUFlgM3oY+xOBcFX0slYk5iwAT/G7o68wiD4/QS2M7qv+VQ9roAvlx06a5H oe34ahABp5FcXz76n8VG0Ba2Z2F4Bc94vfi4351U9FyyuGPO1FcjxOno9r+XOw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062302; 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=Uv3+NRg/Kf1yCAL/nMP1t/I/1XH205FZqRDMC4g6F7E=; b=rSKcKGs6zOPvDRlV0uIw8qAWPWHuL2cEJvKN1zMRHu51x0sQPhlDha8JpkYs/qCMdMAoLs PjOUop+JBZVpT+DQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 40/45] posix-timers: Handle ignored list on delete and exit References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:22 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) --- a/include/linux/posix-timers.h +++ b/include/linux/posix-timers.h @@ -214,7 +214,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 @@ -237,6 +238,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 @@ -1032,6 +1032,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; @@ -1064,6 +1076,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 @@ -1115,6 +1128,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 @@ -1150,6 +1165,13 @@ void exit_itimers(struct task_struct *ts tmr =3D hlist_entry(timers.first, struct k_itimer, list); itimer_delete(tmr); } + + /* Mop up timers which are on the ignored list */ + hlist_move_list(&tsk->signal->ignored_posix_timers, &timers); + while (!hlist_empty(&timers)) { + tmr =3D hlist_entry(timers.first, struct k_itimer, list); + posix_timer_cleanup_ignored(tmr); + } } =20 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock, From nobody Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6722C77B7A for ; Tue, 6 Jun 2023 14:40:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238353AbjFFOkz (ORCPT ); Tue, 6 Jun 2023 10:40:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238295AbjFFOj2 (ORCPT ); Tue, 6 Jun 2023 10:39:28 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 289E9173C for ; Tue, 6 Jun 2023 07:38:30 -0700 (PDT) Message-ID: <20230606142033.330496913@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062304; 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=fTIz5o5hBoARANmS3u0/FKhZKFc8aKqTiazS/c29bx4=; b=OdcnS/g7TLtMK5Roc3vTrxXoq8H3HZqVOPPQe3bOqccGbVUkYJB42PX7X5hlIf783ui1oO JccgdrWqou7YIRWHS6jaj+Soza9KAIEKCVOXbv5iPcjjXuLMrv5RRkFtA6BdCQZWp/WOEE BN0qyQ9T8rtFc/IqQLe/bYDDZA+L5F0HlKgkHnbdhWYKaptYIV8YPYZ7CkfN+GHHhldxhX a3fyCXaTjGPKB/9bX1Dm+Qu1FmJrWuWWyyXMq1lWtM8NtCSvqAaLwv7ZXhwiADF4SMJHEY LKk0XUphu1XFKyePl6h4gqQTocvR8kxW7WRvEgbkAKEbEz2kcwcHW/eL4jVS1A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062304; 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=fTIz5o5hBoARANmS3u0/FKhZKFc8aKqTiazS/c29bx4=; b=s07WC/Jk01SKH8mTDohiDizZp7YjucvjsApqlFfGCLQVuTivaxZ/DQYPyoZW6cg7EVClwN pjnYijYFiVYxaGBQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 41/45] signal: Handle ignored signals in do_sigaction(action != SIG_IGN) References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:23 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -2027,7 +2027,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 static void do_notify_pidfd(struct task_struct *task) { @@ -4147,6 +4195,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; @@ -4167,6 +4217,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBBFBC7EE24 for ; Tue, 6 Jun 2023 14:41:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238358AbjFFOlB (ORCPT ); Tue, 6 Jun 2023 10:41:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238049AbjFFOj3 (ORCPT ); Tue, 6 Jun 2023 10:39:29 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63DF2173B for ; Tue, 6 Jun 2023 07:38:31 -0700 (PDT) Message-ID: <20230606142033.384181292@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062306; 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=LYF80Bk8/VFt3/Zfq3hqFvDVFoPNEcj4ORUMtha4AAY=; b=cZacQ8wYAB7Fb9DsCdI9e5upBjaTR3/GonogUx1w32o7gnoJL8+oGIBsGZRjWoLL+9KRvV ZF8AjijMZ6myMI0e5MjuVzggO9U6nzZvgkZ4NM5NmQBDDbozZgEm/bzfeQ9yFWJR7n2kp9 DoYdIPD8R/+9CdHtmfpldGwPzv8AdJudlXDytEpOKs1/HM/QvBGZusnYeGZvZGZ92pYiag IyM7pJ/3zaWS/wNloxDpFu1X3UENzr9+5RgPiZ9iJcC7TN4UgfeNEr+j5xFWK8U9QJbpCF X0TEOM1VbVURlL97TkNbqOjr32eMih58SBLQy7kpnwzA4iNKDLcf01PslOTFwg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062306; 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=LYF80Bk8/VFt3/Zfq3hqFvDVFoPNEcj4ORUMtha4AAY=; b=BqlduYM4tBY34xpYtNtBJa2wlNU+kRrBw7+Hr5IfTnIWZ4dF+Q1zbvXE49vkvTgr9KSJwU m4OySW2lELrVxJCQ== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 42/45] signal: Queue ignored posixtimers on ignore list References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:25 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -725,6 +725,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. * @@ -743,7 +753,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); } } } @@ -1956,9 +1966,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); @@ -2004,13 +2013,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; @@ -2025,7 +2045,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) @@ -2074,6 +2101,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 @@ -4214,9 +4242,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 180BCC77B7A for ; Tue, 6 Jun 2023 14:41:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238365AbjFFOlF (ORCPT ); Tue, 6 Jun 2023 10:41:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237988AbjFFOjs (ORCPT ); Tue, 6 Jun 2023 10:39:48 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0158B1989 for ; Tue, 6 Jun 2023 07:38:32 -0700 (PDT) Message-ID: <20230606142033.441203108@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062307; 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=KPCEPHtczWIeEtOKLehO2QvyWfrjwM+++WUquCbpcRw=; b=1zWf1jQqodWBHesc5jNRNFwVphF/JwvuwQna14t8+u4vPvJ3+YuUGqenFpAHOkE110KJZb +c0Jk7dbTnIOEzrtwyWkasQcqR6DhR/TBGvNOZbxKFCr/NVJ9/FPsK9BBXZFIeGY9Vhwou Q9Xfhw+EXIaTMUk+qe0H6JVGBXJN9gA07uPqzWV0Pb59lzilizTGX1eJ7W/XhZ9e3EmqFk +rfDwWAXcw1CeUq17l5hvN6r4kuWk3crCYvLrLHNzuFYfwYTJ9AexjfRmV2oihEGIi9BKr H6xyK3u3D9/YbFLg4ACsEp6DGRR6Dnp8Vrx6q2Kn/4+m13dB9ZITLz4zX5wepw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062307; 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=KPCEPHtczWIeEtOKLehO2QvyWfrjwM+++WUquCbpcRw=; b=O/NQe4Bm1NUZ1jjSQh1sYdCGkranWpGj655TOKUl5yu08KaH+ZGR9ainJvhDQyS9uTH0m0 MHosmz8BK5lJQEAg== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 43/45] posix-timers: Cleanup SIG_IGN workaround leftovers References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:27 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -173,7 +173,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 @@ -1960,7 +1960,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; @@ -2045,7 +2045,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) @@ -556,30 +545,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 @@ -593,21 +593,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_HIGHRES_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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB264C77B7A for ; Tue, 6 Jun 2023 14:41:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238371AbjFFOlJ (ORCPT ); Tue, 6 Jun 2023 10:41:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238322AbjFFOjw (ORCPT ); Tue, 6 Jun 2023 10:39:52 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE90C1996 for ; Tue, 6 Jun 2023 07:38:35 -0700 (PDT) Message-ID: <20230606142033.497103352@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062309; 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=QX5B1K8+RZfszOFPTG8OurkUDJXHJzL25+Bw2kwFOks=; b=vOQ16cEyHNDbLUGFYruJwYjN4q7pIkx1YIMSNjLezUFntAY4NfLKrNSAzGYShK+1bxxWsj JdTJO0LI62p7/tTNapErdK+exa61vb0ceeby4gZNozm6CemlpY0qk0sD2sT7M+FiQU2Mgm NbHtaH8AZaVsAQkk0f2bWrwKKme4v0bmelpc59kik23/kxjpHC1p+NgFHzWTUiH6xCoKRj sTr9suQXRMCs1SfNb1Bc6nL3DFVt2lxu4oA/+8uGYxnOxiSrHpvnGEGVvgEpb/QB8QW0kb fOTF+6Avu2+90TgxQBnR0zlbNgBF2TU00pkceF9D6xOU5bS7og20jyHSdMQKVQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062309; 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=QX5B1K8+RZfszOFPTG8OurkUDJXHJzL25+Bw2kwFOks=; b=20weg5fGBIvVECqLe85GEUt4JCXeb060IkO4yfxADrXdfn7vxTPULxdLKN53UFqd5AC16g BcxX0Gy8w4gmKuCA== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 44/45] alarmtimers: Remove the throttle mechanism from alarm_forward_now() References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:28 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -458,35 +458,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 Mon Feb 9 00:20:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1733BC7EE24 for ; Tue, 6 Jun 2023 14:42:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238319AbjFFOl7 (ORCPT ); Tue, 6 Jun 2023 10:41:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238264AbjFFOlb (ORCPT ); Tue, 6 Jun 2023 10:41:31 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82261170F for ; Tue, 6 Jun 2023 07:39:25 -0700 (PDT) Message-ID: <20230606142033.552409135@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686062310; 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=nJRKWItHXWzejPb3W+5S3exZgj7E4NSZUyd430XhqCY=; b=mrU8sqCrn+7K5DAa7KyiaclT2cu1WFJxS9ck94DJpJt1eUDVdDcA1fYPWAnyQFamwfS/HC Gpak+zdw939IwtgcWduZb6bqN7FHZhnYejDlNgyWtsFeguHp8ZPl6aaFGD6Y1INMfFRySI ffwbT360ct3HKA2j2EUHzhwadvU1GksVv6JEDhDCN0kcdC4ZhPC63FtKz/R+9sBpwFIv8i hEYOD8SnRFPkDZWM9Yv5WF+SYKbeajRMAVIORQvm13OOe9/qgQN+us2/FhVvNT20N2W8g9 XuMBrwUKbljR5tmWey+T8pNtnzBZg/ythb1eNvAIjc/3gNdjTiahhlt0ll9aHA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686062310; 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=nJRKWItHXWzejPb3W+5S3exZgj7E4NSZUyd430XhqCY=; b=67v7msoLNJB90fR2k7RuTb3wOWWZ8Z5Ef9sRYGHJay27sXrmhHV299B+LzdYRMEZ+/2eYW RByXcYSwhRAMa6Aw== From: Thomas Gleixner To: LKML Cc: Frederic Weisbecker , Anna-Maria Behnsen , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch 45/45] alarmtimers: Remove return value from alarm functions References: <20230606132949.068951363@linutronix.de> MIME-Version: 1.0 Date: Tue, 6 Jun 2023 16:38:30 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 @@ -312,7 +312,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; @@ -328,7 +328,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); @@ -521,7 +521,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; @@ -529,8 +529,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 /** @@ -691,18 +689,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 (struct task_struct *)alarm->data; =20 alarm->data =3D NULL; if (task) wake_up_process(task); - return ALARMTIMER_NORESTART; } =20 /** @@ -754,7 +748,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)