Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp2567053rwe; Sun, 16 Apr 2023 00:15:27 -0700 (PDT) X-Google-Smtp-Source: AKy350YaM8CjUjQWwCN49RjHGPHJdiQ/fPpgZPSuLWp12ygBS8XKmVGGG5ra9gtNmLMD8r+P/pL3 X-Received: by 2002:a05:6a20:6917:b0:ef:44da:940d with SMTP id q23-20020a056a20691700b000ef44da940dmr2415423pzj.20.1681629327528; Sun, 16 Apr 2023 00:15:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681629327; cv=none; d=google.com; s=arc-20160816; b=Rmkw9y/57OGzW3ZuqFSc6Qr739Q3oFo/dCXTT+kMM9JS7zinJTlbw8ajRJCEij8tdk 2HCo1h0MMvGFG2o4NP5aoTzcE8/i4aH2HYRwNsqf6hZhSwoPmGk+jZBB/2Ibi9PI8AyL IFO5s1eYmef/CFEoiEUI9mSQreMlFLNDotMgw0GFfHqhiL7EEcjeFXIJPOsJnruTKGwX TTEG4RkSmpWtyCdj2ykcypmJtFLhQYGX+E1+VGFLOKTr2L58w/wU+g9dkWog5lnkL7pG 6rcs3ZLv3R0Cfz3HuM2L/UTqtsnLHUDNHGp3UiYgsEbm8QcmYJNV3C8mvtZCd9p8Awy7 iM9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=S+f7GdkF3JQ90ONxa0bAHxG1+bv8XBIUSxJ2AMx2Jcs=; b=zzUwgT8x4x/D3hXIIxLSZufa1L69DnlVEVMHC9EwA6b70Zj6085mnsSgpreX2AQ1DS NLb6F7ydAR9Za0noVxIGhTW5TUAbQvT5mzsQnM2Veic74PKcYqvB+aI+jpz36TzDxNR6 jMasku49H54v7JW6Myu175AblBvLV0PQnqHuKKsm6pVk7PIc/KIV7WjSbNp3sH1bb0lE K3s/BaiUVa5eXaQTdI74STGU1iybeVbQpCK72VetjCuJMf1Zk58Mi00gHcpa0OdyoVpK puXAIBytQVYvqXifIDfs+yweIOsn25K1t5gUa+L67ViUS8YcHjhmrGJvfrwviJZdF0RP Hexw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=RjO8dFSO; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=W90zU+QV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z19-20020a63e113000000b0051bb415165dsi19990pgh.194.2023.04.16.00.15.14; Sun, 16 Apr 2023 00:15:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=RjO8dFSO; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=W90zU+QV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229975AbjDPHEO (ORCPT + 99 others); Sun, 16 Apr 2023 03:04:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229582AbjDPHEN (ORCPT ); Sun, 16 Apr 2023 03:04:13 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90B851FDA; Sun, 16 Apr 2023 00:04:10 -0700 (PDT) Date: Sun, 16 Apr 2023 07:04:07 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1681628648; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S+f7GdkF3JQ90ONxa0bAHxG1+bv8XBIUSxJ2AMx2Jcs=; b=RjO8dFSOFAYWrePhcpxPCF/kqL4KX/eq3g2PbL0f9i2S+PK2tJ8DZnN6etwcDBO/f4HUyh TwfAaryS0nS0xuBO/F7jQYCo2Z6NL3sbubFJWKCz/jt6BjJdaX0lJYNlF5x8UG8DX1Qr6Y brvP/VfSninVYjCJLotLWjRcahfQIU1JnTc2TYNHjtQq1+Y1arbFbZf+0df6mX3VrFnlrC AU4xKmsl82STSqGK/YaunsO6B+Y16Dc5Wkk6DMEYSGFCdOcJjjrhcCzGefuhAf/l3ZrRAK j/hdZ6DszFl5f1VI2qE9DMZWViqWMs1GSuXzmPjTiERu8FfhwYD+QeALODfynw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1681628648; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S+f7GdkF3JQ90ONxa0bAHxG1+bv8XBIUSxJ2AMx2Jcs=; b=W90zU+QVXka4kilzFI+qrSHVmps1ETX55nVUKbkCmRYFZzfb6nDQ9UytiAlTaJoAtthBBG W+52q3eXSEAsdjAA== From: "tip-bot2 for Dmitry Vyukov" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] selftests/timers/posix_timers: Test delivery of signals across threads Cc: Dmitry Vyukov , Marco Elver , Thomas Gleixner , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230316123028.2890338-2-elver@google.com> References: <20230316123028.2890338-2-elver@google.com> MIME-Version: 1.0 Message-ID: <168162864799.404.9782456748146333366.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the timers/core branch of tip: Commit-ID: e797203fb3ba8c0ed2f4a8800d626c9d54fedfbf Gitweb: https://git.kernel.org/tip/e797203fb3ba8c0ed2f4a8800d626c9d54fedfbf Author: Dmitry Vyukov AuthorDate: Thu, 16 Mar 2023 13:30:28 +01:00 Committer: Thomas Gleixner CommitterDate: Sun, 16 Apr 2023 09:00:18 +02:00 selftests/timers/posix_timers: Test delivery of signals across threads Test that POSIX timers using CLOCK_PROCESS_CPUTIME_ID eventually deliver a signal to all running threads. This effectively tests that the kernel doesn't prefer any one thread (or subset of threads) for signal delivery. Signed-off-by: Dmitry Vyukov Signed-off-by: Marco Elver Signed-off-by: Thomas Gleixner Link: https://lore.kernel.org/r/20230316123028.2890338-2-elver@google.com --- tools/testing/selftests/timers/posix_timers.c | 77 ++++++++++++++++++- 1 file changed, 77 insertions(+) diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c index 0ba5000..8a17c0e 100644 --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -188,6 +188,80 @@ static int check_timer_create(int which) return 0; } +int remain; +__thread int got_signal; + +static void *distribution_thread(void *arg) +{ + while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); + return NULL; +} + +static void distribution_handler(int nr) +{ + 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, + }; + + printf("Check timer_create() per process signal distribution... "); + fflush(stdout); + + remain = nthreads + 1; /* worker threads + this thread */ + signal(SIGALRM, distribution_handler); + err = timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); + if (err < 0) { + perror("Can't create timer\n"); + return -1; + } + err = timer_settime(id, 0, &val, NULL); + if (err < 0) { + perror("Can't set timer\n"); + return -1; + } + + for (i = 0; i < nthreads; i++) { + if (pthread_create(&threads[i], NULL, distribution_thread, NULL)) { + perror("Can't create thread\n"); + return -1; + } + } + + /* Wait for all threads to receive the signal. */ + while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); + + for (i = 0; i < nthreads; i++) { + if (pthread_join(threads[i], NULL)) { + perror("Can't join thread\n"); + return -1; + } + } + + if (timer_delete(id)) { + perror("Can't delete timer\n"); + return -1; + } + + printf("[OK]\n"); + return 0; +} + int main(int argc, char **argv) { printf("Testing posix timers. False negative may happen on CPU execution \n"); @@ -217,5 +291,8 @@ int main(int argc, char **argv) if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0) return ksft_exit_fail(); + if (check_timer_distribution() < 0) + return ksft_exit_fail(); + return ksft_exit_pass(); }