Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4509250pxf; Tue, 23 Mar 2021 12:20:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx4X8CAegCqsjZG3TO0bcTQ5yZkj1LL1TfZST/XVumtJcZBzCrA6ytxDZjAgks/jdiOV8C2 X-Received: by 2002:a17:907:9709:: with SMTP id jg9mr6257547ejc.276.1616527232719; Tue, 23 Mar 2021 12:20:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616527232; cv=none; d=google.com; s=arc-20160816; b=sHHFztVeCDzAdy0t1WoWIebjJWfkR8q3BqvJGAJhcp9tzsCIrjR8wJo9Vih6wlNWg9 r7EaNPJsi6NFNZu3hIV7PtCNqOnYJvZRqYoJr22m5n/p4eGWT0waIJ5sbngdbPWYl2x/ Nh6gijxXE0de2oO3RzA75QWDrcJ8D39s4bfR3w3edz8BlXnzcDoEpZUlbSeasYXVdjoI Le9u44J+anrqPfTmdYQPDoj7uEinnS2icXT+vavBphHabz8i2fTMSQaBGBBHY5C/7r09 wvJK1AfkzIXIk2IZcc9tz1L3wWZP64CQaD7NKHp6j8ydPcDrOtvEPdVU6ph6ZYwHwbbu kgVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=tNS+z+6QvyvEx8iMwg0Sa9sQJaEBMryjtauXMfUQXsg=; b=l1vRAqrr8Brv19Zolb1bKBduTU9wB2z93OHJj3c/Z7cH4YI1ZuwsN+7gK4C7YYL3Jl YCoUWOmOJAWI6q4OKSrcR/+4fkoCNYtlij89ZIbi2aCmma+pUP/QOmUfcKD/YPRzeZ2o zaYkI9sHp6G6d02rUaIP3PWUZwVNIErOVwfszr3jbdOuxTicgPfQXLtnOOexeVtnn/0M KOrlw9QDvxmYZsumLkMqFtAJ6E52je4SV4n4yyr4dxX8l1+/vi59vVwRakr2bvG7QOVj KPDLEuA3qVgqtXDIRKtgZqMDUyUq7Sxnvjq0zMpVGbyMbz9j/fkVFFER1UNJYEhRNUxU FlKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=mI5F3aFO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v10si14079873edc.465.2021.03.23.12.20.08; Tue, 23 Mar 2021 12:20:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=mI5F3aFO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233330AbhCWTRG (ORCPT + 99 others); Tue, 23 Mar 2021 15:17:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233072AbhCWTQi (ORCPT ); Tue, 23 Mar 2021 15:16:38 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB99FC061763 for ; Tue, 23 Mar 2021 12:16:37 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id f22-20020a7bc8d60000b029010c024a1407so13647516wml.2 for ; Tue, 23 Mar 2021 12:16:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=tNS+z+6QvyvEx8iMwg0Sa9sQJaEBMryjtauXMfUQXsg=; b=mI5F3aFOIe/mAhDiuSQnPZIA9HNNew1MolHTGbNOmJqKkXu6a0VIPwJUcmWee297OD wkr9SloIeaER3jqaqc3SzUZvkm15n28g9c360eLRWpOdIqQ/1Tvntr/b9UInFVQoniUc DQH4dtuFQPNKXNlHRLkf47W5VKv2eIr72mhasMsOFlsispiZu4nbcSuPBG3+ZLNvCqTG qlGsewcIFEqo0aTzKIws6wiO5iOpW39351Fe5ePVOot+X9/sOQpGTS4c/s9tyuGVvJJV OtstntnD/pgERiv6qawFok8+BMX2A8WGuydE8L1nHR6go76dj8HG8ypIZujMqAI7gZtB iD7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=tNS+z+6QvyvEx8iMwg0Sa9sQJaEBMryjtauXMfUQXsg=; b=PE5sP4AZoor1ra0IQAEmBI63Hz5WiOoAUqlaPNx83rqeWdo008uj9eLendPrRMo49p XfW40xJ/SR4gS0PLzi1vwKWyo6l3FhZEBfZqx696dpF92+Z6Kew7LqxU30bNl67ZHwkX /00YK02J2rqQ99JAf7cJu4nJyy8cGy9+g3dHG5iFPFvjTe0ixXlK1bekaLdsmQqyRpx8 smmEpTCTkOznS4nFMcQqd0r0D7DLulTLoCk4jqfJdprJlfwQaRS0UPFp/06If/4uAw5y WP0eivGbDlbYt1CAuo65zkx51GJushWAef/uNtGrUFYniWmn+l8qu98CZlXjt2ZjD+3O 3YDQ== X-Gm-Message-State: AOAM532iwbwG/+8cqfc61p+64FSO3eccPcESPTj8ih6GqUqiCSgBkFEW JFPA17CmNWtHPjczJUtbh7CWng== X-Received: by 2002:a05:600c:3515:: with SMTP id h21mr4870837wmq.9.1616526996154; Tue, 23 Mar 2021 12:16:36 -0700 (PDT) Received: from elver.google.com ([2a00:79e0:15:13:4cfd:1405:ab5d:85f8]) by smtp.gmail.com with ESMTPSA id m10sm3547153wmh.13.2021.03.23.12.16.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 12:16:35 -0700 (PDT) Date: Tue, 23 Mar 2021 20:16:29 +0100 From: Marco Elver To: Ian Rogers Cc: Peter Zijlstra , Alexander Shishkin , Arnaldo Carvalho de Melo , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Thomas Gleixner , Alexander Potapenko , Al Viro , Arnd Bergmann , Christian Brauner , Dmitry Vyukov , Jann Horn , Jens Axboe , Matt Morehouse , Peter Collingbourne , kasan-dev , linux-arch , linux-fsdevel , LKML , x86 , "open list:KERNEL SELFTEST FRAMEWORK" , Vince Weaver Subject: Re: [PATCH RFC v2 8/8] selftests/perf: Add kselftest for remove_on_exec Message-ID: References: <20210310104139.679618-1-elver@google.com> <20210310104139.679618-9-elver@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.0.5 (2021-01-21) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 23, 2021 at 10:47AM +0100, Marco Elver wrote: > On Tue, 23 Mar 2021 at 04:10, Ian Rogers wrote: > > On Mon, Mar 22, 2021 at 6:24 AM Marco Elver wrote: > > > On Wed, Mar 10, 2021 at 11:41AM +0100, Marco Elver wrote: > > > > Add kselftest to test that remove_on_exec removes inherited events from > > > > child tasks. > > > > > > > > Signed-off-by: Marco Elver > > > > > > To make compatible with more recent libc, we'll need to fixup the tests > > > with the below. > > > > > > Also, I've seen that tools/perf/tests exists, however it seems to be > > > primarily about perf-tool related tests. Is this correct? > > > > > > I'd propose to keep these purely kernel ABI related tests separate, and > > > that way we can also make use of the kselftests framework which will > > > also integrate into various CI systems such as kernelci.org. > > > > Perhaps there is a way to have both? Having the perf tool spot an > > errant kernel feels like a feature. There are also > > tools/lib/perf/tests and Vince Weaver's tests [1]. It is possible to > > run standalone tests from within perf test by having them be executed > > by a shell test. > > Thanks for the pointers. Sure, I'd support more additional tests. > > But I had another look and it seems the tests in > tools/{perf,lib/perf}/tests do focus on perf-tool or the library > respectively, so adding kernel ABI tests there feels wrong. (If > perf-tool somehow finds use for sigtrap, or remove_on_exec, then > having a perf-tool specific test for those would make sense again.) Ok, I checked once more, and I did find a few pure kernel ABI tests e.g. in "wp.c". [...] > Because I'd much prefer in-tree tests with little boilerplate, that > are structured with parsable output; in the kernel we have the > kselftest framework for tests with a user space component, and KUnit > for pure in-kernel tests. So let's try to have both... but from what I could tell, the remove_on_exec test just can't be turned into a perf tool built-in test, at least not easily. In perf tool I also can't use the new "si_perf" field yet. I'll add the patch below at the end of the series, so that we can have both. Too many tests probably don't hurt... Thanks, -- Marco ------ >8 ------ commit 6a98611ace59c867aa135f780b1879990180548e Author: Marco Elver Date: Tue Mar 23 19:51:12 2021 +0100 perf test: Add basic stress test for sigtrap handling Ports the stress test from tools/testing/selftests/sigtrap_threads.c, and add as a perf tool built-in test. This allows checking the basic sigtrap functionality from within the perf tool. Signed-off-by: Marco Elver diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 650aec19d490..a429c7a02b37 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build @@ -64,6 +64,7 @@ perf-y += parse-metric.o perf-y += pe-file-parsing.o perf-y += expand-cgroup.o perf-y += perf-time-to-tsc.o +perf-y += sigtrap.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index c4b888f18e9c..28a1cb5eaa77 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -359,6 +359,11 @@ static struct test generic_tests[] = { .func = test__perf_time_to_tsc, .is_supported = test__tsc_is_supported, }, + { + .desc = "Sigtrap support", + .func = test__sigtrap, + .is_supported = test__wp_is_supported, /* uses wp for test */ + }, { .func = NULL, }, diff --git a/tools/perf/tests/sigtrap.c b/tools/perf/tests/sigtrap.c new file mode 100644 index 000000000000..0888a4e02222 --- /dev/null +++ b/tools/perf/tests/sigtrap.c @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Basic stress-test for sigtrap support. + * + * Copyright (C) 2021, Google LLC. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tests.h" +#include "debug.h" +#include "event.h" +#include "cloexec.h" +#include "../perf-sys.h" + +#define NUM_THREADS 5 + +/* Data shared between test body, threads, and signal handler. */ +static struct { + int tids_want_signal; /* Which threads still want a signal. */ + int signal_count; /* Sanity check number of signals received. */ + volatile int iterate_on; /* Variable to set breakpoint on. */ + siginfo_t first_siginfo; /* First observed siginfo_t. */ +} ctx; + +static struct perf_event_attr make_event_attr(void) +{ + struct perf_event_attr attr = { + .type = PERF_TYPE_BREAKPOINT, + .size = sizeof(attr), + .sample_period = 1, + .disabled = 1, + .bp_addr = (long)&ctx.iterate_on, + .bp_type = HW_BREAKPOINT_RW, + .bp_len = HW_BREAKPOINT_LEN_1, + .inherit = 1, /* Children inherit events ... */ + .inherit_thread = 1, /* ... but only cloned with CLONE_THREAD. */ + .remove_on_exec = 1, /* Required by sigtrap. */ + .sigtrap = 1, /* Request synchronous SIGTRAP on event. */ + }; + return attr; +} + +static void +sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused) +{ + if (!__atomic_fetch_add(&ctx.signal_count, 1, __ATOMIC_RELAXED)) + ctx.first_siginfo = *info; + __atomic_fetch_sub(&ctx.tids_want_signal, syscall(SYS_gettid), __ATOMIC_RELAXED); +} + +static void *test_thread(void *arg) +{ + pthread_barrier_t *barrier = (pthread_barrier_t *)arg; + pid_t tid = syscall(SYS_gettid); + int i; + + pthread_barrier_wait(barrier); + + __atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED); + for (i = 0; i < ctx.iterate_on - 1; i++) + __atomic_fetch_add(&ctx.tids_want_signal, tid, __ATOMIC_RELAXED); + + return NULL; +} + +static int run_test_threads(pthread_t *threads, pthread_barrier_t *barrier) +{ + int i; + + pthread_barrier_wait(barrier); + for (i = 0; i < NUM_THREADS; i++) + TEST_ASSERT_EQUAL("pthread_join() failed", pthread_join(threads[i], NULL), 0); + + return 0; +} + +static int run_stress_test(int fd, pthread_t *threads, pthread_barrier_t *barrier) +{ + ctx.iterate_on = 3000; + + TEST_ASSERT_EQUAL("misfired signal?", ctx.signal_count, 0); + TEST_ASSERT_EQUAL("enable failed", ioctl(fd, PERF_EVENT_IOC_ENABLE, 0), 0); + if (run_test_threads(threads, barrier)) + return -1; + TEST_ASSERT_EQUAL("disable failed", ioctl(fd, PERF_EVENT_IOC_DISABLE, 0), 0); + + TEST_ASSERT_EQUAL("unexpected sigtraps", ctx.signal_count, NUM_THREADS * ctx.iterate_on); + TEST_ASSERT_EQUAL("missing signals or incorrectly delivered", ctx.tids_want_signal, 0); + TEST_ASSERT_VAL("unexpected si_addr", ctx.first_siginfo.si_addr == &ctx.iterate_on); + TEST_ASSERT_EQUAL("unexpected si_errno", ctx.first_siginfo.si_errno, PERF_TYPE_BREAKPOINT); +#if 0 /* FIXME: test build and enable when libc's signal.h has si_perf. */ + TEST_ASSERT_VAL("unexpected si_perf", ctx.first_siginfo.si_perf == + ((HW_BREAKPOINT_LEN_1 << 16) | HW_BREAKPOINT_RW)); +#endif + + return 0; +} + +int test__sigtrap(struct test *test __maybe_unused, int subtest __maybe_unused) +{ + struct perf_event_attr attr = make_event_attr(); + struct sigaction action = {}; + struct sigaction oldact; + pthread_t threads[NUM_THREADS]; + pthread_barrier_t barrier; + int i, fd, ret = 0; + + pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1); + + action.sa_flags = SA_SIGINFO | SA_NODEFER; + action.sa_sigaction = sigtrap_handler; + sigemptyset(&action.sa_mask); + if (sigaction(SIGTRAP, &action, &oldact)) { + pr_debug("FAILED sigaction()\n"); + ret = -1; + goto out_sigaction; + } + + + fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag()); + if (fd < 0) { + pr_debug("FAILED sys_perf_event_open()\n"); + ret = -1; + goto out_sigaction; + } + + /* Spawn threads inheriting perf event. */ + for (i = 0; i < NUM_THREADS; i++) { + if (pthread_create(&threads[i], NULL, test_thread, &barrier)) { + pr_debug("FAILED pthread_create()"); + ret = -1; + goto out_perf_event; + } + } + + ret |= run_stress_test(fd, threads, &barrier); + +out_perf_event: + close(fd); +out_sigaction: + sigaction(SIGTRAP, &oldact, NULL); + pthread_barrier_destroy(&barrier); + return ret; +} diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index b85f005308a3..c3f2e2ecbfd6 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -127,6 +127,7 @@ int test__parse_metric(struct test *test, int subtest); int test__pe_file_parsing(struct test *test, int subtest); int test__expand_cgroup_events(struct test *test, int subtest); int test__perf_time_to_tsc(struct test *test, int subtest); +int test__sigtrap(struct test *test, int subtest); bool test__bp_signal_is_supported(void); bool test__bp_account_is_supported(void);