Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp460723lqe; Sat, 6 Apr 2024 08:11:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVnrRWAhmFAEE5OE0gSZGNjgk9adTYTE0/XIsLbEW461czmPeuN8JNLIjFezYQA7be2lU65QRiOvwGC644Ymgn3MfkVZ/I32mgcbc+2Vw== X-Google-Smtp-Source: AGHT+IHlPE74hK6l9mBMPxxUYGHZFohKh82R2V5ngMjajjcFrovbbuToH2jS0ds4kJ1qyVu1ZCPA X-Received: by 2002:a17:906:594e:b0:a4e:2220:f748 with SMTP id g14-20020a170906594e00b00a4e2220f748mr3136842ejr.40.1712416317369; Sat, 06 Apr 2024 08:11:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712416317; cv=pass; d=google.com; s=arc-20160816; b=eUewr3yo/P7lbB3E8xGd6Abb3eigo8+rDIlKq554rBLTJmIXgkJzglJ7wTTkLqUILZ 2KWFo5p4wNlNBjKBDvZqJ6841OsoAoduuum3B+2Wf9/lOjYw5FXk48QHBoS8PYlkYIdY OZcw6iPJMvmQhP67eBAS1MekQHZ/szJ1tYpbwt/g3QqwTw1oJXAVuHcDE4PUu07tyy4S 4lk3V/Up0BcIerRg3g3Q2Ifjtf+iEa/b6P7smZPvE5hAqSt6RvctP3jda/wPnFXGpqdt 4BFnBPe032vTq9NR2Ay+hA7CKyUlKNh6ROSVZmyG6nn1FrEaCq3kICL3V5D6RYsj/OCs czGA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date; bh=ig0EmkPspn25ineI/4jKY6oNuD+KZIpANNpTNpRld0A=; fh=QM13yX5cyx0gKVLH9qgXc292C0jk7N6nZCeRL8zZxhQ=; b=a1ovTJgvYJ1OABfAaN2LxngN9j85tKbMLDsnfAhi+e6RNEEW+cFCdZQ8r5nruvqwHW d+MjCd4tkSoEmttbTo/r+qHjq2Hlf3wuuCzadRcAaT5cdLI3wBCdm+/HDObSBjiPzGWs waKjxn3K9FtRtgsjQvS49tOs+uvQ52INEC4QZMDk/mK+wzgjCU7QOc0slmGEBsdueLDy YX/L8NkG6UmULgvMNAvnLzZ1HJrEEflH0vN+6dfmxHO/FFEpvl0w/jahQWwQrsRzhGky jg/wqPKQRrBKF+OQ2VkKP+zC2sk8MXOBev2gQpWT7FeJZFzSFxI4uAMZy1my2P2gZ1on iCMQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-133966-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-133966-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id dc19-20020a170906c7d300b00a4725e6b8aesi1742386ejb.18.2024.04.06.08.11.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 08:11:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-133966-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-133966-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-133966-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id DA35C1F21958 for ; Sat, 6 Apr 2024 15:11:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 025B139879; Sat, 6 Apr 2024 15:11:51 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 8FB152BAE3 for ; Sat, 6 Apr 2024 15:11:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712416310; cv=none; b=cVv+apHwH+aRRb8HvDH86XrHSeNiBEey+7DmDGAC/DlBldGPEZewfxgoGljQGqZmMDa0ytX5KPLfcIKEn3PBa2kC7unJxkX0ubOwNFixNhsoVrNY6y+cZRouFmX/wOrpp/rsIdKiwcjKyVScUzXUFR11qDM0EQBWaERfW9RfXsU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712416310; c=relaxed/simple; bh=m7FZIiwrd7YEBY/n2UxCR55958qoIAOrRTaYULHViHY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sl6Q0b9tv32Wl48H4r4qwPEbyUf+fuUVdkYu22zThPgxJk4uYYem2gfno6zBEIppYgYrxZGJNxIaCdDrHcMjFLO17uKr91CKXL5KD9UwD0yDBK+4tHpFvaE1yJTwdqDMLX1L0g67sSd3nG2wpApJHkxTRYl7A9gRMuHtKCFzcBQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-505-8qpYagkzMxSV1T4VL2nekg-1; Sat, 06 Apr 2024 11:11:40 -0400 X-MC-Unique: 8qpYagkzMxSV1T4VL2nekg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F348128AC1E7; Sat, 6 Apr 2024 15:11:39 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.226.136]) by smtp.corp.redhat.com (Postfix) with SMTP id 0E9593C20; Sat, 6 Apr 2024 15:11:36 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Sat, 6 Apr 2024 17:10:15 +0200 (CEST) Date: Sat, 6 Apr 2024 17:09:51 +0200 From: Oleg Nesterov To: Thomas Gleixner Cc: John Stultz , Marco Elver , Peter Zijlstra , Ingo Molnar , "Eric W. Biederman" , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Dmitry Vyukov , kasan-dev@googlegroups.com, Edward Liaw , Carlos Llamas , Greg Kroah-Hartman Subject: [PATCH] selftests/timers/posix_timers: reimplement check_timer_distribution() Message-ID: <20240406150950.GA3060@redhat.com> References: <87sf02bgez.ffs@tglx> <87r0fmbe65.ffs@tglx> <87o7aqb6uw.ffs@tglx> <87frw2axv0.ffs@tglx> <20240404145408.GD7153@redhat.com> <87le5t9f14.ffs@tglx> 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=us-ascii Content-Disposition: inline In-Reply-To: <87le5t9f14.ffs@tglx> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 Thomas says: The signal distribution test has a tendency to hang for a long time as the signal delivery is not really evenly distributed. In fact it might never be distributed across all threads ever in the way it is written. To me even the This primarily tests that the kernel does not favour any one. comment doesn't look right. The kernel does favour a thread which hits the timer interrupt when CLOCK_PROCESS_CPUTIME_ID expires. The new version simply checks that the group leader sleeping in join() never receives SIGALRM, cpu_timer_fire() should always send the signal to the thread which burns cpu. Without the commit bcb7ee79029d ("posix-timers: Prefer delivery of signals to the current thread") the test-case fails immediately, the very 1st tick wakes the leader up. Otherwise it quickly succeeds after 100 ticks. Signed-off-by: Oleg Nesterov --- tools/testing/selftests/timers/posix_timers.c | 102 ++++++++---------- 1 file changed, 46 insertions(+), 56 deletions(-) diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c index d49dd3ffd0d9..2586a6552737 100644 --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -184,80 +184,70 @@ static int check_timer_create(int which) return 0; } -int remain; -__thread int got_signal; +static pthread_t ctd_thread; +static volatile int ctd_count, ctd_failed; -static void *distribution_thread(void *arg) +static void ctd_sighandler(int sig) { - while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); - return NULL; + if (pthread_self() != ctd_thread) + ctd_failed = 1; + ctd_count--; } -static void distribution_handler(int nr) +static void *ctd_thread_func(void *arg) { - if (!__atomic_exchange_n(&got_signal, 1, __ATOMIC_RELAXED)) - __atomic_fetch_sub(&remain, 1, __ATOMIC_RELAXED); -} - -/* - * Test that all running threads _eventually_ receive CLOCK_PROCESS_CPUTIME_ID - * timer signals. This primarily tests that the kernel does not favour any one. - */ -static int check_timer_distribution(void) -{ - int err, i; - timer_t id; - const int nthreads = 10; - pthread_t threads[nthreads]; struct itimerspec val = { .it_value.tv_sec = 0, .it_value.tv_nsec = 1000 * 1000, .it_interval.tv_sec = 0, .it_interval.tv_nsec = 1000 * 1000, }; + timer_t id; - remain = nthreads + 1; /* worker threads + this thread */ - signal(SIGALRM, distribution_handler); - err = timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); - if (err < 0) { - ksft_perror("Can't create timer"); - return -1; - } - err = timer_settime(id, 0, &val, NULL); - if (err < 0) { - ksft_perror("Can't set timer"); - return -1; - } + /* 1/10 seconds to ensure the leader sleeps */ + usleep(10000); - for (i = 0; i < nthreads; i++) { - err = pthread_create(&threads[i], NULL, distribution_thread, - NULL); - if (err) { - ksft_print_msg("Can't create thread: %s (%d)\n", - strerror(errno), errno); - return -1; - } - } + ctd_count = 100; + if (timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id)) + return "Can't create timer"; + if (timer_settime(id, 0, &val, NULL)) + return "Can't set timer"; - /* Wait for all threads to receive the signal. */ - while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); + while (ctd_count > 0 && !ctd_failed) + ; - for (i = 0; i < nthreads; i++) { - err = pthread_join(threads[i], NULL); - if (err) { - ksft_print_msg("Can't join thread: %s (%d)\n", - strerror(errno), errno); - return -1; - } - } + if (timer_delete(id)) + return "Can't delete timer"; - if (timer_delete(id)) { - ksft_perror("Can't delete timer"); - return -1; - } + return NULL; +} + +/* + * Test that only the running thread receives the timer signal. + */ +static int check_timer_distribution(void) +{ + const char *errmsg; + + signal(SIGALRM, ctd_sighandler); + + errmsg = "Can't create thread"; + if (pthread_create(&ctd_thread, NULL, ctd_thread_func, NULL)) + goto err; + + errmsg = "Can't join thread"; + if (pthread_join(ctd_thread, (void **)&errmsg) || errmsg) + goto err; + + if (ctd_failed) + ksft_test_result_skip("No signal distribution. Assuming old kernel\n"); + else + ksft_test_result_pass("check signal distribution\n"); - ksft_test_result_pass("check_timer_distribution\n"); return 0; +err: + ksft_print_msg(errmsg); + return -1; } int main(int argc, char **argv) -- 2.25.1.362.g51ebf55