From patchwork Fri Mar 24 22:30:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13187431 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D79EC77B60 for ; Fri, 24 Mar 2023 22:30:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 142896B007B; Fri, 24 Mar 2023 18:30:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A37C900005; Fri, 24 Mar 2023 18:30:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB1D96B0081; Fri, 24 Mar 2023 18:30:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9A11A900003 for ; Fri, 24 Mar 2023 18:30:43 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6735940B66 for ; Fri, 24 Mar 2023 22:30:43 +0000 (UTC) X-FDA: 80605237566.19.9F5BF85 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf29.hostedemail.com (Postfix) with ESMTP id 9BA42120006 for ; Fri, 24 Mar 2023 22:30:41 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=ptna8i4O; spf=pass (imf29.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679697041; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yLsV99bpcauClh1MlrmzrNiAoYsutXdpDe7QcYfID8o=; b=bd3E0prlyBa5s4BAbQ7ExNLmmQElfghQ4eFlIfZNZKpEPad2xF5pgYJrpZ4qyOsWqXGhJt X3Um2WE++Qoh7ZnNi3sf/17v6eOHCaoKdqk74QRPvnOcrcQrz1GU79aqEUnTBi5D69CW7C GJeIL8f5APRNfOirMiGdK104X6J+fLw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=ptna8i4O; spf=pass (imf29.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679697041; a=rsa-sha256; cv=none; b=j4m7vG4MZhegKCSJCebS9T/YfMGXCeEnGNBX7M9afG6Kw9Drg5NA/KIOVqXiNGO3IdiyGG D1OPC+KZNjMJSV+6qF4PmlTw7mwWWrVb+E2WbLiVqwk6cXG7853do/dFv3abfXOeWGnTg/ Dz3EejdFP4umm6Rn7S2BlZ1TyErYEiM= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 1D8ED20FC4E8; Fri, 24 Mar 2023 15:30:40 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 1D8ED20FC4E8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697040; bh=yLsV99bpcauClh1MlrmzrNiAoYsutXdpDe7QcYfID8o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ptna8i4OIyE99pNNWGB/XMEXh1wQAyC00ZvdH3TLa4dKs+4ZSA+x6Qd9s+5CjTffW MwplxVLcw06wb9j36lHhM7N5ZJRnl86aDXipIQPyrqrjmZ0lceEr+RfX7BMayR84th DpiYNQOCkCiMsd2Bv3sAkjFHzVUXbRWx5uCAk8kE= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v9 07/11] tracing/user_events: Add ABI self-test Date: Fri, 24 Mar 2023 15:30:24 -0700 Message-Id: <20230324223028.172-8-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230324223028.172-1-beaub@linux.microsoft.com> References: <20230324223028.172-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: tm6oe1oa6zrjwnk4e6ukdu5qh1ry97xr X-Rspam-User: X-Rspamd-Queue-Id: 9BA42120006 X-Rspamd-Server: rspam06 X-HE-Tag: 1679697041-925512 X-HE-Meta: U2FsdGVkX1+lVvm97WcFglmMXu6KeNaTUKrWApww2X+cXwWw+w/riJCadudxefzSvt1ESo3s4fDg39qmaXQpEpNTC8YfxLypzI/TcrxCK1D0pb6hS7sgfKke28wDeHmymiM496vy5YYUIfF8b6UQQxFMozU5VFwuzRKTdzoJZulqlsxmaZwYl04EYs7a1TrAhBO8XBQmHAoVrlRQU9QmaUqO61sN8W4AubobwLunmTqU6tE/T9cgAQR6T/nqcSIreQUb2WUEP5joDRHsemvV1N2WSHnyy2dhByRumGsklyEcwhTdahwPpgjzEgia4YGNi/xjW3wX5YtEvx51AT8PEscbHW9wHSJ99we5LxHsZm4fzxdxvAhUVuJU64l1dqju2nDy27/SN8KDTdTm0wVx20E9lvjlgO88Vavj0fE/U5d8NxIplXJXxJ8slQKhok0u3UlPV7ohgXIKQwuUjIrK4wnL8/v8NarbJqRWe5vHkn6ozLKmp9C6JdVQ8uTiIQ8BjAmMAUJV3QsS/Qqam59idTt2GTo0tkz09rN780NQGV6mqlTZ6Q4emvBf5PJT27AUUQamAcLBe533qG2fKKi9dle0mJ6ptyMf7yCFDDNWzgZSRU+0fHDSVjycJDKBo4qIzNHPunyK5sqoq9ANo1GZFz2Imxjrq9UYXnixbFi9D5b08w1cLNIIPv5jQDkzrEe9DqugtFjpYfJ5YBjnGIOwPOuiImawYcQooCICprEQOoAFxabtS4+qOna0YEnpVakm3q7nKXE0POdg1nHMtLmp/UIb8m8+lcSEdbacb0+D1HHkdBRTEAPfjApE1iN8hJIC0UxM0UxXCRtsO8/UxKX+k6QTheomvu1f4I1aDhlfEccPRkkz40cXQcZVAJ0VuuPLEqnEEgN2zXh1627PrhcokOmqPdQ3uNg9b+MuSnb2QgfP4NOpdvBFnFF2BUC9rwTGhv6CYqNuubfScWsvlp9 1F7Z2F4U 2PwLVBRi5fLtJAtHngv+EHjYywFvN1WXc6Rg3zdaF+S7sxU2nU9z/dMfOladBMLOoTtdXgVCHjB6cE/9GY3roCm2dINA2NZny9LHuqQTu/g/XQJNXhM14ubmf+6ygdXcw1CQlSiwbnTH7PPcG1zSH22bx/kbjaYiAohdhjoxzouGha1l+8N0o842ZNAGcd/g5nR88tVEiqbrtnhV9pkHAjV5Dtn1QWstVk3kOUUaoFHjHJjGY6STQbjEhmRg5HuSLBV36GXutrQ2DBAmgrlP0zWX7UiabkkHwEpLnl1rMikzju3M= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add ABI specific self-test to ensure enablements work in various scenarios such as fork, VM_CLONE, and basic event enable/disable. Ensure ABI contracts/limits are also being upheld, such as bit limits and data size limits. Signed-off-by: Beau Belgrave --- tools/testing/selftests/user_events/Makefile | 2 +- .../testing/selftests/user_events/abi_test.c | 226 ++++++++++++++++++ 2 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/user_events/abi_test.c diff --git a/tools/testing/selftests/user_events/Makefile b/tools/testing/selftests/user_events/Makefile index c765d8635d9a..d5f64ef93197 100644 --- a/tools/testing/selftests/user_events/Makefile +++ b/tools/testing/selftests/user_events/Makefile @@ -2,7 +2,7 @@ CFLAGS += -Wl,-no-as-needed -Wall -I../../../../usr/include LDLIBS += -lrt -lpthread -lm -TEST_GEN_PROGS = ftrace_test dyn_test perf_test +TEST_GEN_PROGS = ftrace_test dyn_test perf_test abi_test TEST_FILES := settings diff --git a/tools/testing/selftests/user_events/abi_test.c b/tools/testing/selftests/user_events/abi_test.c new file mode 100644 index 000000000000..e0323d3777a7 --- /dev/null +++ b/tools/testing/selftests/user_events/abi_test.c @@ -0,0 +1,226 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * User Events ABI Test Program + * + * Copyright (c) 2022 Beau Belgrave + */ + +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +const char *data_file = "/sys/kernel/tracing/user_events_data"; +const char *enable_file = "/sys/kernel/tracing/events/user_events/__abi_event/enable"; + +static int change_event(bool enable) +{ + int fd = open(enable_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + if (enable) + ret = write(fd, "1", 1); + else + ret = write(fd, "0", 1); + + close(fd); + + if (ret == 1) + ret = 0; + else + ret = -1; + + return ret; +} + +static int reg_enable(long *enable, int size, int bit) +{ + struct user_reg reg = {0}; + int fd = open(data_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + reg.size = sizeof(reg); + reg.name_args = (__u64)"__abi_event"; + reg.enable_bit = bit; + reg.enable_addr = (__u64)enable; + reg.enable_size = size; + + ret = ioctl(fd, DIAG_IOCSREG, ®); + + close(fd); + + return ret; +} + +static int reg_disable(long *enable, int bit) +{ + struct user_unreg reg = {0}; + int fd = open(data_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + reg.size = sizeof(reg); + reg.disable_bit = bit; + reg.disable_addr = (__u64)enable; + + ret = ioctl(fd, DIAG_IOCSUNREG, ®); + + close(fd); + + return ret; +} + +FIXTURE(user) { + long check; +}; + +FIXTURE_SETUP(user) { + change_event(false); + self->check = 0; +} + +FIXTURE_TEARDOWN(user) { +} + +TEST_F(user, enablement) { + /* Changes should be reflected immediately */ + ASSERT_EQ(0, self->check); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, change_event(true)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(0, self->check); + + /* Should not change after disable */ + ASSERT_EQ(0, change_event(true)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, reg_disable(&self->check, 0)); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(1, self->check); + self->check = 0; +} + +TEST_F(user, bit_sizes) { + /* Allow 0-31 bits for 32-bit */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 31)); + ASSERT_NE(0, reg_enable(&self->check, sizeof(int), 32)); + ASSERT_EQ(0, reg_disable(&self->check, 0)); + ASSERT_EQ(0, reg_disable(&self->check, 31)); + +#if BITS_PER_LONG == 8 + /* Allow 0-64 bits for 64-bit */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(long), 63)); + ASSERT_NE(0, reg_enable(&self->check, sizeof(long), 64)); + ASSERT_EQ(0, reg_disable(&self->check, 63)); +#endif + + /* Disallowed sizes (everything beside 4 and 8) */ + ASSERT_NE(0, reg_enable(&self->check, 1, 0)); + ASSERT_NE(0, reg_enable(&self->check, 2, 0)); + ASSERT_NE(0, reg_enable(&self->check, 3, 0)); + ASSERT_NE(0, reg_enable(&self->check, 5, 0)); + ASSERT_NE(0, reg_enable(&self->check, 6, 0)); + ASSERT_NE(0, reg_enable(&self->check, 7, 0)); + ASSERT_NE(0, reg_enable(&self->check, 9, 0)); + ASSERT_NE(0, reg_enable(&self->check, 128, 0)); +} + +TEST_F(user, forks) { + int i; + + /* Ensure COW pages get updated after fork */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, self->check); + + if (fork() == 0) { + /* Force COW */ + self->check = 0; + + /* Up to 1 sec for enablement */ + for (i = 0; i < 10; ++i) { + usleep(100000); + + if (self->check) + exit(0); + } + + exit(1); + } + + /* Allow generous time for COW, then enable */ + usleep(100000); + ASSERT_EQ(0, change_event(true)); + + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + + /* Ensure child doesn't disable parent */ + if (fork() == 0) + exit(reg_disable(&self->check, 0)); + + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(0, self->check); +} + +/* Waits up to 1 sec for enablement */ +static int clone_check(void *check) +{ + int i; + + for (i = 0; i < 10; ++i) { + usleep(100000); + + if (*(long *)check) + return 0; + } + + return 1; +} + +TEST_F(user, clones) { + int i, stack_size = 4096; + void *stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, + -1, 0); + + ASSERT_NE(MAP_FAILED, stack); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, self->check); + + /* Shared VM should see enablements */ + ASSERT_NE(-1, clone(&clone_check, stack + stack_size, + CLONE_VM | SIGCHLD, &self->check)); + + ASSERT_EQ(0, change_event(true)); + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + munmap(stack, stack_size); + ASSERT_EQ(0, change_event(false)); +} + +int main(int argc, char **argv) +{ + return test_harness_run(argc, argv); +}