Received: by 2002:ab2:1347:0:b0:1f4:ac9d:b246 with SMTP id g7csp64397lqg; Wed, 10 Apr 2024 15:46:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUDQF6Pwy+WpsnNCHAaxwFwpbycYCqbFhF8wAXnE1Ch5u0FglhQUgQHtOpVXBGBVGTaoSX+7RJckPUP/g1tmYdhLiTqgYWUWeh9s6mrlg== X-Google-Smtp-Source: AGHT+IGsMfGkSFPPbGjM3x7QX0rFpJKohHfMHwwvxBu+w8TRDtTO2O7/VAl/kHXeVAFFINiwB00w X-Received: by 2002:a05:6808:202a:b0:3c5:f1fa:ced0 with SMTP id q42-20020a056808202a00b003c5f1faced0mr5125363oiw.3.1712789201271; Wed, 10 Apr 2024 15:46:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712789201; cv=pass; d=google.com; s=arc-20160816; b=O38xGoHXU462cv8Pswizmazd4iRWA71OfmUhl+bq/rW+SmVC8oQ4Q/6v/cPdpoJatk sLNCPQWnpLZPSQ0XtPZrlO3lFNGBHlXxSVjFCun7ta6SOqf5wWU0pSncu6KoK23ipvZi fJS9XnQ6mRfbRpvGuz+FF2FqAftuLD5dCwDHyFIVJYFLOT5h3fHGQ3Ysvu5yzg1Vaa41 wKDiPs92Gbi1mUXR/S1mp835tLzy9Nv0/UUZmpe6jaMIQyJSiYN1v4KYNpK17EvHywfV OPxvu88IezzZ6VW/K1uszcah7I6Bw0FZMrBJ1j6rtwwWFlF7YPUCdQtEexgTYkMMHc60 1zjA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:subject:cc:to:from:dkim-signature :dkim-signature:message-id; bh=mG203+TomORLX85Hq8lXp4OYnptpdQupAxvOnDDHeaY=; fh=joBs/8L+orz77ylyCy6wqNwwi35f7sYhLR+/TQjQjhI=; b=bVKjPLae4X4ymEG62AOBGeI3JHxY8HyHKPbN9nHS6fOQHENdcbB2cZoG9Q7Alg3Fs2 hky5m8aUlSD2kfDn/WepD6/tdb5cKC/PL1lJcpLytTT0urs9RHu6j6ylU5pRw1a7ICXw v8L6mQCtU3/QhqR0kpcFmWjQ/KcOJTtVO7rEUaOy0Pxw6usVRlQmaqrLgOsAumzQVSKi 9+3OZJwC6GMW/aumJUJsi6sqDCsQEm8TxzLpQ/AOVkpUehtbI5Kf9D1oYr6X08flhsyG kXIfc6XuzpUcnnx8dK9n1gL1U/KfW/cxpNSsWF6MsFECZHweW/ONP6qx4bx8JioRZGgh j6zg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=S7FUrk4+; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-139454-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139454-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id l20-20020a05622a051400b004347adedf05si209496qtx.749.2024.04.10.15.46.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 15:46:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-139454-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=S7FUrk4+; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-139454-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139454-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B95E81C221BA for ; Wed, 10 Apr 2024 22:46:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 364BE383B5; Wed, 10 Apr 2024 22:46:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="S7FUrk4+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cz9bDsaP" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87ED917BDA for ; Wed, 10 Apr 2024 22:46:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789179; cv=none; b=jQZ3EShm8OxHgKAbDRE0MHRyLVprZ5SkY/vUwn7W7SC9DVr3gbn3X10hfBOuy3cgthEODHd2jazU8J62JiZzEe6GLpOEBwUOnGn6t5GjQYRomdiDj6aYM01chqrTkZOw3nf6ljTU9BrtuFbMW9KIlyFx43OoZl1ung+t8w3nAbU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712789179; c=relaxed/simple; bh=AEwG/M/Bkml3zMl7Ux1PQtqNKrZozpL+XEzsujpu//A=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Ri9Uap6l2Kecpm/DWWYvcMYujw94Tdin72BmLxp3+euz8EjqHIjC/PCN2cyse07sV2UxCdarERGqfLKianb+ESd48OlZZi78C8/l1K6eR8NRim4PrOKcIsBPDMgnjqdhFgYneLOEVIGWgeaiPZx3r0W1vcBYQbTj6UBiaKkbcgA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=S7FUrk4+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cz9bDsaP; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Message-ID: <20240410165551.064545113@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712789176; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=mG203+TomORLX85Hq8lXp4OYnptpdQupAxvOnDDHeaY=; b=S7FUrk4+0vWk2KjFmsDrCYHLe8TA8ncVHslxY5KvXhMwdkB7ZFyKj+euD8jqOKotXLiN6N bX3KOCHuK2WJ1XZG9np49x+3GYT0MzTShtRmQOprdmpMG7U2sWf86zEfE5w4DlafNA5u7G 9rkGXvXVHeq4JUrkGKKCjYEyqobiLpSjs+3BMxAppv6oMytzWUWHSJmLwQvXOsm40b0nNE c3BkkbbHmp9Fqow1b8XwVwi5L1otz2oB/5PcevtTjXDAqwgIR8SmskCyL0A9k/3VefmQoZ w97mTd1t86WiVmrYauqiQc6CF3Z0L6ORArXoWMVQpfiYLPDLFQTxBUgnuzXOWA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712789176; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=mG203+TomORLX85Hq8lXp4OYnptpdQupAxvOnDDHeaY=; b=cz9bDsaPLYrDUcW5slz0gitAkYpYc3DqtlmBN6LfHkfHwlp+J30yJtrDNc9dCZ+p/+E5AY ssTnA6e86n18HvDg== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V2 02/50] selftests/timers/posix_timers: Add SIG_IGN test References: <20240410164558.316665885@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Thu, 11 Apr 2024 00:46:15 +0200 (CEST) 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=100ms, interval=100ms); sleep(1); <- Timer expires and queues signal, timer is not rearmed as that should happen in the signal delivery path ignore_signal(SIG); <- Discards queued signal install_handler(SIG); <- Restore handler, should rearm but does not sleep(1); unblock_signal(SIG); <- Should deliver one signal with overrun count set in siginfo This fails because nothing rearms the timer when the signal handler is restored. Add a test for this case which fails until the signal and posix timer code is fixed. Signed-off-by: Thomas Gleixner --- tools/testing/selftests/timers/posix_timers.c | 127 +++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 2 deletions(-) --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -6,8 +6,9 @@ * * Kernel loop code stolen from Steven Rostedt */ - +#define _GNU_SOURCE #include +#include #include #include #include @@ -214,10 +215,129 @@ static void check_timer_distribution(voi ksft_test_result_skip("check signal distribution (old kernel)\n"); } +struct tmrsig { + int signals; + int overruns; +}; + +static void siginfo_handler(int sig, siginfo_t *si, void *uc) +{ + struct tmrsig *tsig = si ? si->si_ptr : NULL; + + if (tsig) { + tsig->signals++; + tsig->overruns += si->si_overrun; + } +} + +static void *ignore_thread(void *arg) +{ + unsigned int *tid = arg; + sigset_t set; + + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + if (sigprocmask(SIG_BLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); + + *tid = 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 = { }; + struct itimerspec its; + unsigned int tid = 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 = SA_SIGINFO; + sa.sa_sigaction = 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 = SIGEV_SIGNAL; + sev.sigev_signo = SIGUSR1; + sev.sigev_value.sival_ptr = &tsig; + if (thread) { + sev.sigev_notify = SIGEV_THREAD_ID; + sev._sigev_un._tid = 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 = 0; + its.it_value.tv_nsec = 100000000; + its.it_interval.tv_sec = 0; + its.it_interval.tv_nsec = 100000000; + timer_settime(timerid, 0, &its, NULL); + + sleep(1); + + /* Set the signal to be ignored */ + if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) + fatal_error(NULL, "signal(SIG_IGN)"); + + sleep(1); + + if (thread) { + /* Stop the thread first. No signal should be delivered to it */ + if (pthread_cancel(pthread)) + fatal_error(NULL, "pthread_cancel()"); + if (pthread_join(pthread, NULL)) + fatal_error(NULL, "pthread_join()"); + } + + /* Restore the handler */ + if (sigaction(SIGUSR1, &sa, NULL)) + fatal_error(NULL, "sigaction()"); + + sleep(1); + + /* Unblock it, which should deliver the signal in the !thread case*/ + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); + + if (timer_delete(timerid)) + fatal_error(NULL, "timer_delete()"); + + if (!thread) { + ksft_test_result(tsig.signals == 1 && tsig.overruns == 29, + "check_sig_ign SIGEV_SIGNAL\n"); + } else { + ksft_test_result(tsig.signals == 0 && tsig.overruns == 0, + "check_sig_ign SIGEV_THREAD_ID\n"); + } +} + int main(int argc, char **argv) { ksft_print_header(); - ksft_set_plan(6); + ksft_set_plan(8); ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n"); ksft_print_msg("based timers if other threads run on the CPU...\n"); @@ -239,5 +359,8 @@ int main(int argc, char **argv) check_timer_create(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); check_timer_distribution(); + check_sig_ign(0); + check_sig_ign(1); + ksft_finished(); }