From patchwork Tue Mar 28 23:52:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191674 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 5E7C1C761A6 for ; Tue, 28 Mar 2023 23:52:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 46F2B6B0080; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 447BE6B0081; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 051D56B0082; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D2F0F6B0080 for ; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 99BE81A047B for ; Tue, 28 Mar 2023 23:52:30 +0000 (UTC) X-FDA: 80619958860.03.664C50F Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf24.hostedemail.com (Postfix) with ESMTP id CA3C6180004 for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=i6MpK4pf; spf=pass (imf24.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=1680047549; 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=ybG5+eUOhrRp012Obsz3AX1F0Qf1gQeUFXkawLV+7wsw40Qjz+J+uAHzvy8MpnDNKf8Jle krqO1UVS2JSkwBb9/VBPm8aKWiIWzFx8uIt1zST/NSU9tYzwrC/XCHmrCmZEg8+wp+kUdg tCzQrVU5bBA3st9LO51LVpYG+MT3YzE= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=i6MpK4pf; spf=pass (imf24.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=1680047549; a=rsa-sha256; cv=none; b=OSbABf1h4Y+wpM5ywS+KOwrXs6enz3Wi2jORFVPqO7U6VyAWhGjgc2zgA9O2VxtY9Ntw/Z X1AWVd6zmUNNxO5BZDwYG0tY+AmgcdjQ9UWz4XGPOOc7l32ah0vJuojAX75gYVBD8x6x7d 3+JYF0a7VBWpQp1lw/txx4txpfAaswU= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 2C38520FDC21; Tue, 28 Mar 2023 16:52:27 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2C38520FDC21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047547; bh=yLsV99bpcauClh1MlrmzrNiAoYsutXdpDe7QcYfID8o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i6MpK4pfOWHADyzKWlDD34ASp6Fi+iHOshXZoGrS+ecaPzj5oVvyxVaVAxAgPBNzy v6K43iulDXumEgDf7BCcG6XDam3aUbptyOWXWt+Fs909i4qa4Cc38ftt+STQ+1pFUT a5qU8X+av+WTqdTcsVSxVNbV29vHr4AJrpyjxpaM= 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 v10 07/12] tracing/user_events: Add ABI self-test Date: Tue, 28 Mar 2023 16:52:14 -0700 Message-Id: <20230328235219.203-8-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: ofzgdhmyg1dkkkfuanmt1hweneu5y3kz X-Rspam-User: X-Rspamd-Queue-Id: CA3C6180004 X-Rspamd-Server: rspam06 X-HE-Tag: 1680047548-647598 X-HE-Meta: U2FsdGVkX19kY1Frh30h/SGLTwS4uHXZjeKEU22CPr0fa+7ROUQYx/RZ6GmuVYzVg/HSD6CNaalJwKBDdabg98tg6J6U8gO6zZul/By83mLVXW0hkzwGBoK42x70Pqq7KR63cUg85fjnFPK17dgRlVhTUWOvtRhs15LuRFocoT56GnmgDeFZWMGnBjHEjyakpyTgtvEE2qCPS2sE9uLkHeu0X1Dma/M9T92Ch1Xa5ahd2os5TkJKuhrHIs4rsUiCYI8o+w3gtMSt/CdiAMNYQIciKeUo2jocoIFdi06CxIAYqRmOcY5kJpyRgzNED3aCU+VpCwH6HrC1GejmRYeRoYHmzDNcXpzKVDWR1yr+hfeoJaAeyq4UjrmvyjCEYsyG1261VF1Yu1eyl/35Wjde8SJ+QtMQXZRxjo7D/V6gycyEvlj4647scYMjwSTqwGnlDFdO/64oRfaJR0Trt65vd3Es0cuwRS5b6injhl8k2QAb9bKze1RNmyaOhLA1w9FERuNDj/eQlvjEY5bJJEn2NBLtrCyY6EChjy0DDlpJndSY7Qe4QHe7b3W3lKKnxvXexRuYWXs+iNV5j6S4wVF7d5KR9aR5NsHioUTqaomcY5kEpM1Z7hPn7K2f0BjZOq75ezCo8OvZ1kGy9VMisKOhsw9TnAIW6XcaARuOJVCXlJuKtGbY8g86cqxttlw96Fm0ly9QZlv6IEyuks84drLqbId0Mn8EN3nAJCRfCVNVD25HbM0AEM2vqmUucInf8FuZ4fj1jVI+vaXWC0CvxnQuWU2PnoDHJbWU/XlhhBPJAKpN+JvDEris+2aD0ctdGF9yRqk74qwQpquaKQFWkKqTEOLZ2QLRR8Srn9QUzor6c0pIEwjTT1bBbVAvDhjvtxqjjJegDyLhGiOoJ4UjHBQqbzWdjeEvrsfGArPsWr29y+/t6GM5SACLqrejtQnwWZizCM55T5s2azWL+PpjWeS /TsyFrfP suLenjTNi2rYjEI5wD9p+Xckl5KoyKxbuyTHSkUX02Xl5sRT5p71dLo28G4efGr9YMR72nDI0MynctRdHpztu/jT0nINFmqJPAJljZDMiDFCpcKtOIsO0aJZD2xZvMbh9LiCiJZxXJH3IBqslQm5TqJpoTbtitL3jaIHOzYyy4zP+UHHnVNgaax8IvaIQjANq+T6nMaH60Pq2x+Yo9v5uGP1bY57kGMZFly4QpfTfw1GE5ldUEEdBqRq91UrRqAhoLxOaAXEQJoylq9whaddQIb7GMB/OjzIoHSkGmYZewjGuVM5GV88w+pdUhqfvGf5KRfLLDJE22W7x2Tp2W+QD8GWhe36f9YsQ/RpvjipF5JhxF2XTL1A4eVIrteUhzGxFmM/X 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); +}