Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp482676lqp; Mon, 10 Jun 2024 09:43:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXxgxdXYwmOd1pBGgC0wVL87Twhf2V9Y++nJ4QqnfKCpjKznGuDoZlWZtM06UOtbYU1YB4xsyWXVSXZVfWHJbiho+jselfoT8xcoU6Flg== X-Google-Smtp-Source: AGHT+IEB77yBF3ALLqx1DVBWmC1QCSxuSS48UhFpJIPoSTqQ1Po+SQ1HlidYyh6kegC35kUk/kIB X-Received: by 2002:a05:6358:4327:b0:19f:4d5a:c688 with SMTP id e5c5f4694b2df-19f4d5ac9a6mr341796655d.15.1718037809170; Mon, 10 Jun 2024 09:43:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718037809; cv=pass; d=google.com; s=arc-20160816; b=WqZqfDGJq805B4Md6uwPpCkqiEeaLzkLgzoJ6XtGGCs3ctDzM/CnXUEDj7F8sqVyDm DUSup3zqLskXpb2W7sHbQA76pV6Re41RS6bf4TBO9TRQfgV7vBk6R+Gff9yUqIzyZ9IM GXz+Pw5bHUITgPM0lQ4LMLtsNxLFrkevc8/mr21/9czJrjzw3UbQxvLDPZJM1m0tCkg7 n9+UfXGu6qDHG0kPW/i2FjIOYjdpeTGs5ae4PMm6CT3k6uNGPSSO8mchruGYCbWg9hBE z256mOlh3oGr47wINFuEjSv/E8xa1kkf9jkx0TO398IPq4QvBoygv99GkhC8OkUJNf2f 2hgA== 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=0UI7HzXPt8XkY17pzDcEdt1KqKS+XuBzMwPx04vGKBoYJATRkJCT+eFKPMXAl4KaVC /Zon3sXm80BJFNH7u48yHyN0PfiJ5wuWcNwi8LZcAFvADxuFSW/xX5Zz1zr/8MJ0dWAL bObsxha36jkKlvPwT3440XCyRSggGOAx7luv8qv5oYIhmoxSg51Q2Be9bOSVrmnePmSZ aea7Kstspmefp/+NrCjTYlPiMiuUMzHODuS7OjKiDKVxziZFHA2Eu8wgUp4Q+2Jnbw7H MLK/P7zm7Deaq8KrWzcuUVSMSvCBsJVPIpmEjspGKd9UUsx2uACevicpmcD2qXZ8L5Ro Yvaw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ToG5mItK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=g5ancSxf; 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-208567-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208567-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de261cac13si8001758a12.417.2024.06.10.09.43.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 09:43:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208567-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ToG5mItK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=g5ancSxf; 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-208567-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208567-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8FBB02853E2 for ; Mon, 10 Jun 2024 16:42:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 613C214A615; Mon, 10 Jun 2024 16:42:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ToG5mItK"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="g5ancSxf" 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 59EE914535C for ; Mon, 10 Jun 2024 16:42: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=1718037739; cv=none; b=dIYUcsHigIBkY/ZLP2ZTGIKOPXptzFD90ktYD+jJ940FRQyhz3fo00RRr7UyM1pN80Zcg5cg+RpYVo4Tkpxo2bEnCKKQhSTPhzpoLhKXkzSar/AzCqA4bK7x1P++CGAjXgyM7MCxv7u90fDBWoljVdY5irLc7lkyi1cTmjphWU4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718037739; c=relaxed/simple; bh=AEwG/M/Bkml3zMl7Ux1PQtqNKrZozpL+XEzsujpu//A=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Bkp2vXP3o80NooaD6Fi6zN8Alrh2XhrJonl6vx1lqoI9KPVsj6HiK18Tt2K3tPx+HeaZNR+gzBSfA6Ucrh3iVCKfLzsQ1CLHpjMmZqjpKrGOyVvzaeHQ7tXWGlv11s+uqkeRo386+Ptni+FqrdL3XwRM2jh15OSbSXvXQbInl1A= 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=ToG5mItK; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=g5ancSxf; 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: <20240610164025.579598461@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718037728; 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=ToG5mItK89iYz6hAh0GoU0xmIr7Y3aPWUCKYRssfAeXF6ZUpczedK+qpNs8UdNhh1Wpe+0 KopUfdbmrDThGkaS1Qrwhd0r1KMFcCecrvSseL7MmnYVkEnitVitrhu585D34+zyQgYCBB jtE2Vxk/Aa5eyS3FQtkZV4ofNpBWoWRYZrs/JJ70wHZKUs+8kB6chub7EnOUTGURLHojZx 1RCgsbx255s2a5GW8e6d7JQ9oYwOtYc9t3nYg0bneAzFRaU4i8Fs+4XVplzJPIIT0xkEuS tbUZoUdlRCRRaKSQ7uKCFccgpOg+r2rjXeZ1XPCx3JgCNp1pyVUnZqUK4OUzEA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718037728; 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=g5ancSxf+71eTa+aP+/NK5gmccML/vAveYoCLixEaZpN0QuKyUf0CnFN3prJF1tlQP5i92 NVn+VjX0nLFDFACA== 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 V3 02/51] selftests/timers/posix_timers: Add SIG_IGN test References: <20240610163452.591699700@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: Mon, 10 Jun 2024 18:42:07 +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(); }