From patchwork Thu Mar 23 19:06:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186005 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 88331C6FD1C for ; Thu, 23 Mar 2023 19:06:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D8F66B007E; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 450A56B0082; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E1C56B007E; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 173AD6B0082 for ; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C7E2116058F for ; Thu, 23 Mar 2023 19:06:20 +0000 (UTC) X-FDA: 80601093720.03.94C8F53 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf15.hostedemail.com (Postfix) with ESMTP id 1215FA0022 for ; Thu, 23 Mar 2023 19:06:18 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=b8bFnfII; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf15.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679598379; 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=GWgDDu2j0Zbhv8Ed5hJ9RpUDQ1SIrtpnFVXm0JO2cXQBW2fQhQfGU767xuNBTPTtbwMmLM P3VlR1aN6E4yjtx5itR4u1RfgNcSiMVw512nckuzOj0C9wjtjQ+1XmpBb2XRc7dnNc2iEX bEYjwB45OE3Gkg5IewFoOaPq+ibR/lM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=b8bFnfII; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf15.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679598379; a=rsa-sha256; cv=none; b=k4/y7wIsk0ps8rdMqhEj/gDDcYwaw23wWFOWeRJZ8+LpaoG+P4wcdqVDtjZ4N78jCS2DfY Zn4EXVz8lTlivfgXCBLbWNqLlcEafBs9bTjBcAb383amtwwF3qB0UVLOatHaxnvontl5MC OywzxCH6AwUBq5K3CWXAVrOfK7IesQA= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 2813020FC3CA; Thu, 23 Mar 2023 12:06:17 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2813020FC3CA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598377; bh=yLsV99bpcauClh1MlrmzrNiAoYsutXdpDe7QcYfID8o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b8bFnfIIBtYwXJQLdnYf0l+f/B38p//PJ27erGrmpVPj4MHh5+0YGKRxqFdLjrx4U vkv9MUNzZgu/TXVlqAL1W1cxfXE5WaCm+rjzv5h6PKkPquJ5CPeQkqbe+awLmpHGxY xdfikiGk9OoIu6EDLr+5BXYr84l4QPY948QRAl8U= 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: [RESEND PATCH v8 07/11] tracing/user_events: Add ABI self-test Date: Thu, 23 Mar 2023 12:06:06 -0700 Message-Id: <20230323190610.251-8-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1215FA0022 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: q5u757boj6mwy6gd3yrukc7xqwww184i X-HE-Tag: 1679598378-345718 X-HE-Meta: U2FsdGVkX1+EqPEcPsP2EIoY1EM6HEUT1POkouH2w+OlEg0ndRgouBiR5TGIiUypGgWOVETnh81w44HAyGNF17iTKwvrWqZUlj78IkBGyomejwnSDIXb9E6GI7KS0S8vbltP+v9aUPcdG0yUAb/NhhSCLsrVG5pransTm2HJw5J/K46OK9CfrbCl3X+LX8qD0BeJjz4gpf06n0X3su5/Rk+D908qj1n4XtZfMUd96NL/QktgstixtLtClXNmU4wdHagq6XUY0+MTZtaHAZM9Jfk1n9M3tK3pcWedruMic56vyzrroijH3b+cUU6JowJz1EYL50V77CPuT9Hnr0Usf7oimBH/3qNqmAtPeFbK1zYLhpDuVq7DQXLTqu9JonjhtHoyxDwkl7Clx5AnygWSSMLLCv25efV8Yl3XyLFkn6KzmDdTZ4TTT70jzcSNCN+WPU43tKB0LcTLJv0lcPEsSHdGcWrUKXMdz2P3yCBONbjCliRCfSg3c8AM1gRkN1A31ePjSgJ1WEt6lA9sTmZ7hYfPA/TEifFDsE6ALTpdBsjyRZT3NNvA6V6s+HM1TQAXgc+x1al4bsObL48CKhLIObvic0jCOdl5mjneR2xTrBN82FaXrSU+DezYF3vniQOmDZRgKcV6IAFj/oVgQ8FKTe8sAfkaaqiY8EBWhEd+6fzTsmBxr+EXqEQuA1w2L3o7mXiYHQn4piup21Qr76brd6pHBclKVtUJXyC2vbQLt2ZgL7tPtFoD3r92rb+hq/UnWtJXiX/9wEGyjRwFWBwKy09PhxjXeLDQBwhoqGU4SXoVmlPFEj8iM3owsavcyUoI9ILW4353Ce4AXnqAIGxZG1zpdD4K04lGAziVgaQFWfHRBtPfDyTvzvXbMuJYvPMTxcPpTf6EW/mOOpQsQCUVGkyZMSBau0gESHoqgqMlGOvFq7rMnuuBNrKWxYSDp5AaXidgX18GmjkTWOLECnY ZiHvNbta +CG4VPFH8zfFDCRhewdeZDMFs2AQD7IW0czocdrAMLv44fNpxdtFdmLBvc3Ryt2YkBfVE7FMUBmQatExoEkjXSGXHPqUdDFML1G9xeQs5bHSY0xmlJCWLvCSfQWX3Q3+5jZeAxtses1X72ouLzG/+Q7tEDCva3LvcbjStegNaa6o3ZkOgk6aQTzIklOfMSWbkrr1k3UYwjFG/5JFY9mNvkruYPEsJYv4zJBzkHTK53uGwa83FP/x30amFEmiD2ZenPkAjEFiMs/XnzzhEibom9bkjiNaNCOjE0fw4FcGiMqD7k0E= 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); +}