Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1965747rdb; Tue, 20 Feb 2024 12:25:56 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVbbKUA55A3NZr6HpPzizEYk6DRy8vmkBLJhB7MMWb7F8hmpTIaEBfBvYiHW+pDdg6I0SZx2ACFvrO0fBJfkHQt35mIlMBnMreJpSSxig== X-Google-Smtp-Source: AGHT+IHiGmD5LIvTVoJHOMz/D2DZY520MwNm0Vkz3hZNzx1cHE9OVLJAV6J3oUqbNlfUa0shZ1yq X-Received: by 2002:a17:902:fc8f:b0:1dc:b8f:cd0f with SMTP id mf15-20020a170902fc8f00b001dc0b8fcd0fmr5500753plb.54.1708460756671; Tue, 20 Feb 2024 12:25:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708460756; cv=pass; d=google.com; s=arc-20160816; b=YxtGU/Fw0tRPiMJXyEkYINDr6i+c0SmUucUIt0P3cHkDXXelnhKGa9iWRrI7/+oBrY 12tjWjVEongCx6dzDR04QJH5cwvzLHHpo5oqX48hILIsUiB+Bd/uz8I1pMzEI8fozcuD yPPXp12tzQDhTTANELmKDw1bbWRdmtviYL1uYlpaO1TXflNgAZEUMv2aXE+XxPpRmdeM 2SYVSnLV+3Lwf3JVBmanua4aqIFbgGU4d1mp9CfWv9iZZ26mHtDH80Y7Z/cpCRH9qdux na5VYifRmvaiRnHSwliauikDD4UbhhmtF8ytM3WBUk1n9A2p2qf5/g6ZBfxyp1Oyxq8D eqOA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=iZjxidu+JFmCthSlYk9Bx3D9eTqyNowu6l339iQEnac=; fh=T7N/iEEh01bWr1hOYjauur4pFOej0W24cse96RT3mfQ=; b=Bbs+F4FOIoMY1knyC9+bIa8xC4XfdWyjhEMEFas7MUAtBOK37uCa2ECO2moiBeRe2a FJdsV63WJDOgjE0SkIrxFNZOGVWj7qbRJ5P7ksfBOnaP5YL7QpJc+GQpSEAWi5s9mLxG RNYzAv1fL0jMlTVSCHbAYIiQ0Ta1570l4PzMr8gNvTeboiCX6/DcFh3wIsojCFoRpito ssc2i1kLSkML1XeWVGq/cRs+b3qPSRGg19GEb6AM+9c7E2KqcNOHWdxB5T9X2s+CObbM c+SxrwqPt/q8n/1gGqKSZYdqhg6EvmOgVE78PyINtwze9wu5Lqcn2qxJy5e4Q2ICuqbB ewVA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZH8migIR; arc=pass (i=1 spf=pass spfdomain=flex--vdonnefort.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-73674-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73674-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h18-20020a170902f55200b001d95cfd2bdesi6654611plf.150.2024.02.20.12.25.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 12:25:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73674-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZH8migIR; arc=pass (i=1 spf=pass spfdomain=flex--vdonnefort.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-73674-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73674-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 33D6E282F39 for ; Tue, 20 Feb 2024 20:25:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E7308150989; Tue, 20 Feb 2024 20:23:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZH8migIR" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD92214F9C9 for ; Tue, 20 Feb 2024 20:23:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708460611; cv=none; b=siGS1/2ADtFvNljUfXQMnFn1YT4WjUV53UGTNr9Wb1BdezscUjx8cvAB9NIKmD8pMI6VmqT99x0T+l2Lx296AkF+FPX+DFq2qz+oUI1IlVcYBdQjskA2rE2hhaaQHgzb3Sejc5grVMo3Yb+G79cI0XoWN4ccjKqhaNdfbYQhObM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708460611; c=relaxed/simple; bh=ccTAR8GzSCDWXW0/WIZvXFB3XJOO/H5kh/tczds8Vvw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=quy0GqIL6Ehhtrh6+pWf30Ka9dvszMy3UD1pSvyVoC4xdIl2vdovtaWGvE+Kbu8v/WG0aB6ArNzG0NkN36/Ud0q3S4GZwgjoJKw0Lvq3oTqRrvrIq3fU7VJhZaSJpg6avKXFfhLhF2QuRnM6aRp2GPbYC2mcQFklxqfbNs4bA2s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZH8migIR; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60810a74e2eso63376587b3.1 for ; Tue, 20 Feb 2024 12:23:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708460609; x=1709065409; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iZjxidu+JFmCthSlYk9Bx3D9eTqyNowu6l339iQEnac=; b=ZH8migIR+tJ8xmkaw830LOHR1KpmVbQibIbzK3lsK/hOWWCQoCSUt5+0MLMBWQBqpZ P/3Q4n+IKOD6qYp/1SgVLRbrvkjtyvUKplPKglmG3aEOj8Wcu8kMO9z3fyEq3Hb1clKE 4SVnK3g5ZtODx5QBWFQdJIs6uP934XMaPVMkSS8XF8poZ7dyoUyE7+GaUgulT7l1AYwG a5Yr15FtZ3js1yqKEmQcbnuisriGyjRNtce9UR1TdLGk+hLsp4vn4UA3EtxztwGxBTue u0J5k5AKdncq2Cz0HF9iECrvCz7YZFUcjvKhJuPtWLLNNyGPRk1FLxcQj6CPPcJIE7rJ 1CBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708460609; x=1709065409; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iZjxidu+JFmCthSlYk9Bx3D9eTqyNowu6l339iQEnac=; b=iR1tWfyR7KBEI6j7z5veqCKvA9JTXDFef8K+5yzt913zUL9XCOVfm1P1gHTJNmDc67 ryvC/+A7wk3iqTCmm6+N8kvxr9KAih0GPU82o24BAz9KyIVXN8sTJT4+unsZ0ioO575m 8kmANwzfWbI5jvdVs3X/ghggJW2uIsyrghuH5LHIVmR9NAkeU9YrJ+g/5ASheUg3N6ch +JypNWQTq60pdGehIsIFL6cz1HlMOqK2XgXkNB8mbaNyzKomuEQVGIIiGYoM9cKmmMEc ErVvpYDx0vSywZUH+adMOvvjwlhkJXnwndj0uHKGVg9/QcSo9tGcuDw7EZ9Qel1HbWV0 dVOQ== X-Forwarded-Encrypted: i=1; AJvYcCUo/HvF7zuwQd0WKVWXSTH5aLTzJ7SW1A19jtdE+MYOr3gRfa65tLJcpPaMkCH4Vt+HpuHHbc7VOUzxtLQq7Lg4gsAADM96GPqy70US X-Gm-Message-State: AOJu0YwobM3GQHalJSIcSPLE3lh1e50/vpbzBFd9Isk1BaYrpYxsAnRG 5pYr3I3pLRL7Q0ELTihPJxYiAkJKf2XWSc17QR3fJMWotUHX7IkoCyoSsP95A7jU8nWuU7PorVs ZNGPlBcfpjvwxQRvwtQ== X-Received: from vdonnefort.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:2eea]) (user=vdonnefort job=sendgmr) by 2002:a05:690c:fd0:b0:608:dae:a0c2 with SMTP id dg16-20020a05690c0fd000b006080daea0c2mr2896583ywb.3.1708460608985; Tue, 20 Feb 2024 12:23:28 -0800 (PST) Date: Tue, 20 Feb 2024 20:23:10 +0000 In-Reply-To: <20240220202310.2489614-1-vdonnefort@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240220202310.2489614-1-vdonnefort@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240220202310.2489614-7-vdonnefort@google.com> Subject: [PATCH v18 6/6] ring-buffer/selftest: Add ring-buffer mapping test From: Vincent Donnefort To: rostedt@goodmis.org, mhiramat@kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: mathieu.desnoyers@efficios.com, kernel-team@android.com, Vincent Donnefort , Shuah Khan , Shuah Khan , linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" This test maps a ring-buffer and validate the meta-page after reset and after emitting few events. Cc: Shuah Khan Cc: Shuah Khan Cc: linux-kselftest@vger.kernel.org Signed-off-by: Vincent Donnefort diff --git a/tools/testing/selftests/ring-buffer/Makefile b/tools/testing/selftests/ring-buffer/Makefile new file mode 100644 index 000000000000..627c5fa6d1ab --- /dev/null +++ b/tools/testing/selftests/ring-buffer/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS += -Wl,-no-as-needed -Wall +CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -D_GNU_SOURCE + +TEST_GEN_PROGS = map_test + +include ../lib.mk diff --git a/tools/testing/selftests/ring-buffer/config b/tools/testing/selftests/ring-buffer/config new file mode 100644 index 000000000000..d936f8f00e78 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/config @@ -0,0 +1,2 @@ +CONFIG_FTRACE=y +CONFIG_TRACER_SNAPSHOT=y diff --git a/tools/testing/selftests/ring-buffer/map_test.c b/tools/testing/selftests/ring-buffer/map_test.c new file mode 100644 index 000000000000..56c44b29d998 --- /dev/null +++ b/tools/testing/selftests/ring-buffer/map_test.c @@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Ring-buffer memory mapping tests + * + * Copyright (c) 2024 Vincent Donnefort + */ +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include "../user_events/user_events_selftests.h" /* share tracefs setup */ +#include "../kselftest_harness.h" + +#define TRACEFS_ROOT "/sys/kernel/tracing" + +static int __tracefs_write(const char *path, const char *value) +{ + int fd, ret; + + fd = open(path, O_WRONLY | O_TRUNC); + if (fd < 0) + return fd; + + ret = write(fd, value, strlen(value)); + + close(fd); + + return ret == -1 ? -errno : 0; +} + +static int __tracefs_write_int(const char *path, int value) +{ + char *str; + int ret; + + if (asprintf(&str, "%d", value) < 0) + return -1; + + ret = __tracefs_write(path, str); + + free(str); + + return ret; +} + +#define tracefs_write_int(path, value) \ + ASSERT_EQ(__tracefs_write_int((path), (value)), 0) + +#define tracefs_write(path, value) \ + ASSERT_EQ(__tracefs_write((path), (value)), 0) + +static int tracefs_reset(void) +{ + if (__tracefs_write_int(TRACEFS_ROOT"/tracing_on", 0)) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/trace", "")) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/set_event", "")) + return -1; + if (__tracefs_write(TRACEFS_ROOT"/current_tracer", "nop")) + return -1; + + return 0; +} + +struct tracefs_cpu_map_desc { + struct trace_buffer_meta *meta; + void *data; + int cpu_fd; +}; + +int tracefs_cpu_map(struct tracefs_cpu_map_desc *desc, int cpu) +{ + unsigned long meta_len, data_len; + int page_size = getpagesize(); + char *cpu_path; + void *map; + + if (asprintf(&cpu_path, + TRACEFS_ROOT"/per_cpu/cpu%d/trace_pipe_raw", + cpu) < 0) + return -ENOMEM; + + desc->cpu_fd = open(cpu_path, O_RDONLY | O_NONBLOCK); + free(cpu_path); + if (desc->cpu_fd < 0) + return -ENODEV; + + map = mmap(NULL, page_size, PROT_READ, MAP_SHARED, desc->cpu_fd, 0); + if (map == MAP_FAILED) + return -errno; + + desc->meta = (struct trace_buffer_meta *)map; + + meta_len = desc->meta->meta_page_size; + data_len = desc->meta->subbuf_size * desc->meta->nr_subbufs; + + map = mmap(NULL, data_len, PROT_READ, MAP_SHARED, desc->cpu_fd, meta_len); + if (map == MAP_FAILED) { + munmap(desc->meta, desc->meta->meta_page_size); + return -EINVAL; + } + + desc->data = map; + + return 0; +} + +void tracefs_cpu_unmap(struct tracefs_cpu_map_desc *desc) +{ + munmap(desc->data, desc->meta->subbuf_size * desc->meta->nr_subbufs); + munmap(desc->meta, desc->meta->meta_page_size); + close(desc->cpu_fd); +} + +FIXTURE(map) { + struct tracefs_cpu_map_desc map_desc; + bool umount; +}; + +FIXTURE_VARIANT(map) { + int subbuf_size; +}; + +FIXTURE_VARIANT_ADD(map, subbuf_size_4k) { + .subbuf_size = 4, +}; + +FIXTURE_VARIANT_ADD(map, subbuf_size_8k) { + .subbuf_size = 8, +}; + +FIXTURE_SETUP(map) +{ + int cpu = sched_getcpu(); + cpu_set_t cpu_mask; + bool fail, umount; + char *message; + + if (!tracefs_enabled(&message, &fail, &umount)) { + if (fail) { + TH_LOG("Tracefs setup failed: %s", message); + ASSERT_FALSE(fail); + } + SKIP(return, "Skipping: %s", message); + } + + self->umount = umount; + + ASSERT_GE(cpu, 0); + + ASSERT_EQ(tracefs_reset(), 0); + + tracefs_write_int(TRACEFS_ROOT"/buffer_subbuf_size_kb", variant->subbuf_size); + + ASSERT_EQ(tracefs_cpu_map(&self->map_desc, cpu), 0); + + /* + * Ensure generated events will be found on this very same ring-buffer. + */ + CPU_ZERO(&cpu_mask); + CPU_SET(cpu, &cpu_mask); + ASSERT_EQ(sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask), 0); +} + +FIXTURE_TEARDOWN(map) +{ + tracefs_reset(); + + if (self->umount) + tracefs_unmount(); + + tracefs_cpu_unmap(&self->map_desc); +} + +TEST_F(map, meta_page_check) +{ + struct tracefs_cpu_map_desc *desc = &self->map_desc; + int cnt = 0; + + ASSERT_EQ(desc->meta->entries, 0); + ASSERT_EQ(desc->meta->overrun, 0); + ASSERT_EQ(desc->meta->read, 0); + + ASSERT_EQ(desc->meta->reader.id, 0); + ASSERT_EQ(desc->meta->reader.read, 0); + + ASSERT_EQ(ioctl(desc->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + ASSERT_EQ(desc->meta->reader.id, 0); + + tracefs_write_int(TRACEFS_ROOT"/tracing_on", 1); + for (int i = 0; i < 16; i++) + tracefs_write_int(TRACEFS_ROOT"/trace_marker", i); +again: + ASSERT_EQ(ioctl(desc->cpu_fd, TRACE_MMAP_IOCTL_GET_READER), 0); + + ASSERT_EQ(desc->meta->entries, 16); + ASSERT_EQ(desc->meta->overrun, 0); + ASSERT_EQ(desc->meta->read, 16); + + ASSERT_EQ(desc->meta->reader.id, 1); + + if (!(cnt++)) + goto again; +} + +FIXTURE(snapshot) { + bool umount; +}; + +FIXTURE_SETUP(snapshot) +{ + bool fail, umount; + struct stat sb; + char *message; + + if (stat(TRACEFS_ROOT"/snapshot", &sb)) + SKIP(return, "Skipping: %s", "snapshot not available"); + + if (!tracefs_enabled(&message, &fail, &umount)) { + if (fail) { + TH_LOG("Tracefs setup failed: %s", message); + ASSERT_FALSE(fail); + } + SKIP(return, "Skipping: %s", message); + } + + self->umount = umount; +} + +FIXTURE_TEARDOWN(snapshot) +{ + __tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger", + "!snapshot"); + tracefs_reset(); + + if (self->umount) + tracefs_unmount(); +} + +TEST_F(snapshot, excludes_map) +{ + struct tracefs_cpu_map_desc map_desc; + int cpu = sched_getcpu(); + + ASSERT_GE(cpu, 0); + tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger", + "snapshot"); + ASSERT_EQ(tracefs_cpu_map(&map_desc, cpu), -EBUSY); +} + +TEST_F(snapshot, excluded_by_map) +{ + struct tracefs_cpu_map_desc map_desc; + int cpu = sched_getcpu(); + + ASSERT_EQ(tracefs_cpu_map(&map_desc, cpu), 0); + + ASSERT_EQ(__tracefs_write(TRACEFS_ROOT"/events/sched/sched_switch/trigger", + "snapshot"), -EBUSY); + ASSERT_EQ(__tracefs_write(TRACEFS_ROOT"/snapshot", + "1"), -EBUSY); +} + +TEST_HARNESS_MAIN -- 2.44.0.rc0.258.g7320e95886-goog