Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1632719lqe; Mon, 8 Apr 2024 15:17:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUOfGh2QdT3ETZewQ1i2f4dqDPZ7GL+64ASLqfIrW4n/Krt8GAm3lSikbV3o89rAo4cADUNThPyRPU4HEzUxy1tPRylUdCOEknJAWO8jg== X-Google-Smtp-Source: AGHT+IFMWvvqBr5pL8+hUjFk7rSB8Ltt0eF3MbTod+fKZBcLA/mWX43W3KaIZWNSGI4qK/F4UhVi X-Received: by 2002:a05:6358:7b97:b0:181:601f:d8f with SMTP id n23-20020a0563587b9700b00181601f0d8fmr11791739rwg.2.1712614647121; Mon, 08 Apr 2024 15:17:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712614647; cv=pass; d=google.com; s=arc-20160816; b=qQeteuNGT8Io5CKBvZomZ/7aQa9uxgkNps9QWOvxMK1jp6NZrramEYj54ziTGw3kra QlofNl2CRWBJWS98gle6dqq0W8x41+5uJBhCgvZ2xIDsjVCYzQAH2//rHW1QEA5eG+Xx OAtijFj2icrVD2ZdFWtOtyAjmlMBO0EjOVYjHIui101pS+0jWyReLjpiFd2tZvOcSOBJ mgbHfTmdK9kmyvOjPs9YcnenSDn8J3HOM+9xi0epLO7pTKBKy4an1gcxMuPdLo9UogNg Aquk5546Z7GFL6sSxZf97noRCWdPSMQ/JJIyJO2FDTyUX8QSzxmLiKa39mB/S8pchOht 2ahQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :message-id:date:references:in-reply-to:subject:cc:to:dkim-signature :dkim-signature:from; bh=Mmbn5DsA9ubK+0jZ4PQnylXNHi12qhjspWzNa9zKp8Q=; fh=4Nv/nEfyFs8DOpffvrTsm+4TdL3Ck8nKKOoUBFJ9uL8=; b=eBuHwJLFoipytjQ7kKg0HnyHM+m8Irl9yYtRHod5CT0UIfsTVOcpzdjN1ElsT73FHy Roi57SXGoiar5zuuEqLVYV+bkBJJBzvmKfBHCQvBxVi9V1DpH1IHG6cvuPCu2IPUJX2/ 6ZTySJEAKm+U3SHBovsyUFshvujX60XWURc5fKdqvUcWP5Jvgf5lYAWQzEEjaGdT4xjc wK8WxxMesTPapwu+lGeqTVv+QlUc7E+fckhpEzGmzjJZRj9Cgda/GyblHHT3dWeYroay KgLckt+Nc602oBNHlvkKXGIckMRZI42gkBwVDEWzJrLQ12cvPSmILrK7f7N8GdYbezgF oQ2Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=HQn0DlXC; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=2tsU0bRX; 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-135971-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135971-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 g131-20020a636b89000000b005e51394040esi7086936pgc.105.2024.04.08.15.17.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 15:17:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135971-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=HQn0DlXC; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=2tsU0bRX; 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-135971-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135971-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 90BAD282F76 for ; Mon, 8 Apr 2024 22:17:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AC0ED149C71; Mon, 8 Apr 2024 22:17:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HQn0DlXC"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="2tsU0bRX" 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 041C0148FE3; Mon, 8 Apr 2024 22:17:18 +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=1712614640; cv=none; b=Dgq3iD0b8qGzpG4z16HPnkhttgeUHEnwfqJ8B0wGFeSdg3Oes77Zn6x0LR3IK7aYbMCDaHeyzDXCUmsiLhKLyIb+1Prdbhg2VyCyh+EW2NsyHQm3LQAr3SWCiayk/Soa93NiVFi4J61jGO3Rg67BW4PSjCimn7i1ipapwd0QiCA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712614640; c=relaxed/simple; bh=whQdxiq0EcWkxnnWUTp+6cxyZsM42KKWCB84lKtU8d0=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=rkEUb+8Ky+hkONSJRcWrbYbAI13xziCnozYZs/WPxmWBiDzDpFmf8mD83XIPZ7lia+lC7IrDf6XnvBCxNPF7JwccVRtwBnUm5LJfX4EfdIgXhzMz3eR4j4jC64lzrObgBVhzqiYHoCI1qD6mTmkj+9aY8jJRNYm3xvnOiDEICCE= 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=HQn0DlXC; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=2tsU0bRX; 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 From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712614636; 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: in-reply-to:in-reply-to:references:references; bh=Mmbn5DsA9ubK+0jZ4PQnylXNHi12qhjspWzNa9zKp8Q=; b=HQn0DlXCU8VSOIC30XSbmso5sXg0l225Uki7Vl+M20aIEvF9KkWjo6In6VrbRrfdmijDUz OscXcKvbdP24s7cumZJt+yanfArOKRDxAjB7TRxVu7GAyyen9SgfpgpwMjdPrKInsAOiaV 350rax9nHGT+Dq8zbJkXQMIjzLQ+FLpNUJCQBenPLrGGnFCceoZSNteKDlmD+kJO2c+2YI vRnE8+YX7KHtuhg79ZQpc0aW7I1UcwlSSGdAkudwVnX/Smp5ZLTYWr5Q25dvIbnzel5K5H nWvcigoxriS6yD0jTWzzUBQBMcuJWZvtJ/qs6yZKOfrBh2xSmXovPwg0YGExew== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712614636; 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: in-reply-to:in-reply-to:references:references; bh=Mmbn5DsA9ubK+0jZ4PQnylXNHi12qhjspWzNa9zKp8Q=; b=2tsU0bRXVb9M89ctOJ/7UxL8bF9gx83mqL04zlDseQBw8ToliZXoI1z0RZwNkNhBVIDhuZ cybOcN/doI2DqRDg== To: Oleg Nesterov , Dmitry Vyukov Cc: John Stultz , Marco Elver , Peter Zijlstra , Ingo Molnar , "Eric W. Biederman" , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kasan-dev@googlegroups.com, Edward Liaw , Carlos Llamas , Greg Kroah-Hartman Subject: Re: [PATCH] selftests/timers/posix_timers: reimplement check_timer_distribution() In-Reply-To: <20240408184957.GD25058@redhat.com> References: <87o7aqb6uw.ffs@tglx> <87frw2axv0.ffs@tglx> <20240404145408.GD7153@redhat.com> <87le5t9f14.ffs@tglx> <20240406150950.GA3060@redhat.com> <20240406151057.GB3060@redhat.com> <20240408102639.GA25058@redhat.com> <20240408184957.GD25058@redhat.com> Date: Tue, 09 Apr 2024 00:17:15 +0200 Message-ID: <87il0r7b4k.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 On Mon, Apr 08 2024 at 20:49, Oleg Nesterov wrote: > To me this test should simply do > > ksft_test_result(!ctd_failed, "check signal distribution\n"); > return 0; Right. > but I am not familiar with tools/testing/selftests/ and I am not sure > I understand the last email from Thomas. The discussion started about running new tests on older kernels. As this is a feature and not a bug fix that obviously fails on older kernels. So something like the uncompiled below should work. Thanks, tglx --- --- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -184,80 +184,83 @@ 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; -} - -static void distribution_handler(int nr) -{ - if (!__atomic_exchange_n(&got_signal, 1, __ATOMIC_RELAXED)) - __atomic_fetch_sub(&remain, 1, __ATOMIC_RELAXED); + if (pthread_self() != ctd_thread) + ctd_failed = 1; + ctd_count--; } -/* - * 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) +static void *ctd_thread_func(void *arg) { - 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 NULL; +} + +static bool check_kernel_version(unsigned int min_major, unsigned int min_minor) +{ + unsigned int major, minor; + struct utsname info; + + uname(&info); + if (sscanf(info.release, "%u.%u.", &major, &minor) != 2) + ksft_exit_fail(); + return major > min_major || (major == min_major && minor >= min_minor); +} + +/* + * Test that only the running thread receives the timer signal. + */ +static int check_timer_distribution(void) +{ + const char *errmsg; + + if (!check_kernel_version(6, 3)) { + ksft_test_result_skip("check signal distribution (old kernel)\n"); return 0; } - ksft_test_result_pass("check_timer_distribution\n"); + 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; + + ksft_test_result(!ctd_failed, "check signal distribution\n"); return 0; + +err: + ksft_print_msg(errmsg); + return -1; } int main(int argc, char **argv)