From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 67495C6FD1C for ; Fri, 24 Mar 2023 22:30:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232138AbjCXWao (ORCPT ); Fri, 24 Mar 2023 18:30:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231903AbjCXWaj (ORCPT ); Fri, 24 Mar 2023 18:30:39 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6DE0516AEE; Fri, 24 Mar 2023 15:30:38 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 9C8DA20FC45C; Fri, 24 Mar 2023 15:30:37 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 9C8DA20FC45C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697037; bh=OSmDP26J8YzoMhJc3UKK7slznwSGVE3KNwggNF1IdJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PC3fLGkFs4XtaJsoRdT5RfKgAC1GMmcW+2cgDHWwoFbnAwoO0UUq7EFue+kHRzR2H zSqIR9gkMpBxBWgGg1k6v2ZNOwx4uRxzH6Kdqb77Aq8Mt0oo970po/nf9+/yO0gHXd 0Gb2yLD3OieWzydLn1P3MlKXlJyvi8gXOEGeQo94= 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 01/11] tracing/user_events: Split header into uapi and kernel Date: Fri, 24 Mar 2023 15:30:18 -0700 Message-Id: <20230324223028.172-2-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The UAPI parts need to be split out from the kernel parts of user_events now that other parts of the kernel will reference it. Do so by moving the existing include/linux/user_events.h into include/uapi/linux/user_events.h. Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- include/linux/user_events.h | 52 ++++---------------------------- include/uapi/linux/user_events.h | 48 +++++++++++++++++++++++++++++ kernel/trace/trace_events_user.c | 5 --- 3 files changed, 54 insertions(+), 51 deletions(-) create mode 100644 include/uapi/linux/user_events.h diff --git a/include/linux/user_events.h b/include/linux/user_events.h index 592a3fbed98e..13689589d36e 100644 --- a/include/linux/user_events.h +++ b/include/linux/user_events.h @@ -1,54 +1,14 @@ -/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* SPDX-License-Identifier: GPL-2.0-only */ /* - * Copyright (c) 2021, Microsoft Corporation. + * Copyright (c) 2022, Microsoft Corporation. * * Authors: * Beau Belgrave */ -#ifndef _UAPI_LINUX_USER_EVENTS_H -#define _UAPI_LINUX_USER_EVENTS_H =20 -#include -#include +#ifndef _LINUX_USER_EVENTS_H +#define _LINUX_USER_EVENTS_H =20 -#ifdef __KERNEL__ -#include -#else -#include -#endif +#include =20 -#define USER_EVENTS_SYSTEM "user_events" -#define USER_EVENTS_PREFIX "u:" - -/* Create dynamic location entry within a 32-bit value */ -#define DYN_LOC(offset, size) ((size) << 16 | (offset)) - -/* - * Describes an event registration and stores the results of the registrat= ion. - * This structure is passed to the DIAG_IOCSREG ioctl, callers at a minimum - * must set the size and name_args before invocation. - */ -struct user_reg { - - /* Input: Size of the user_reg structure being used */ - __u32 size; - - /* Input: Pointer to string with event name, description and flags */ - __u64 name_args; - - /* Output: Bitwise index of the event within the status page */ - __u32 status_bit; - - /* Output: Index of the event to use when writing data */ - __u32 write_index; -} __attribute__((__packed__)); - -#define DIAG_IOC_MAGIC '*' - -/* Requests to register a user_event */ -#define DIAG_IOCSREG _IOWR(DIAG_IOC_MAGIC, 0, struct user_reg*) - -/* Requests to delete a user_event */ -#define DIAG_IOCSDEL _IOW(DIAG_IOC_MAGIC, 1, char*) - -#endif /* _UAPI_LINUX_USER_EVENTS_H */ +#endif /* _LINUX_USER_EVENTS_H */ diff --git a/include/uapi/linux/user_events.h b/include/uapi/linux/user_eve= nts.h new file mode 100644 index 000000000000..03f92366068d --- /dev/null +++ b/include/uapi/linux/user_events.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Copyright (c) 2021-2022, Microsoft Corporation. + * + * Authors: + * Beau Belgrave + */ +#ifndef _UAPI_LINUX_USER_EVENTS_H +#define _UAPI_LINUX_USER_EVENTS_H + +#include +#include + +#define USER_EVENTS_SYSTEM "user_events" +#define USER_EVENTS_PREFIX "u:" + +/* Create dynamic location entry within a 32-bit value */ +#define DYN_LOC(offset, size) ((size) << 16 | (offset)) + +/* + * Describes an event registration and stores the results of the registrat= ion. + * This structure is passed to the DIAG_IOCSREG ioctl, callers at a minimum + * must set the size and name_args before invocation. + */ +struct user_reg { + + /* Input: Size of the user_reg structure being used */ + __u32 size; + + /* Input: Pointer to string with event name, description and flags */ + __u64 name_args; + + /* Output: Bitwise index of the event within the status page */ + __u32 status_bit; + + /* Output: Index of the event to use when writing data */ + __u32 write_index; +} __attribute__((__packed__)); + +#define DIAG_IOC_MAGIC '*' + +/* Request to register a user_event */ +#define DIAG_IOCSREG _IOWR(DIAG_IOC_MAGIC, 0, struct user_reg *) + +/* Request to delete a user_event */ +#define DIAG_IOCSDEL _IOW(DIAG_IOC_MAGIC, 1, char *) + +#endif /* _UAPI_LINUX_USER_EVENTS_H */ diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_u= ser.c index 908e8a13c675..070551480747 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -19,12 +19,7 @@ #include #include #include -/* Reminder to move to uapi when everything works */ -#ifdef CONFIG_COMPILE_TEST #include -#else -#include -#endif #include "trace.h" #include "trace_dynevent.h" =20 --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D71DDC6FD1C for ; Fri, 24 Mar 2023 22:30:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232224AbjCXWar (ORCPT ); Fri, 24 Mar 2023 18:30:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231572AbjCXWaj (ORCPT ); Fri, 24 Mar 2023 18:30:39 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CF76F16AF0; Fri, 24 Mar 2023 15:30:38 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 14DEF20FC46C; Fri, 24 Mar 2023 15:30:38 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 14DEF20FC46C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697038; bh=S8OIOLUkRs5CBqJ2LYBXQxLSyKFNFEQS/E/TUK8EqMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LcE2qcNrWlZ8cc8HJUNHUatSIGDn31YUbtU3h1i+6eTnLG5l9y0M19IpF2UWU+ake f8A98v+bev9xhJ/oPs352rqqjwX4B+9Pw4/2BCXIU9rspttdoL7Af2xNlKM505xJIf tVP54HuAxrozzsSeltltIQrSeVCfQjmpAu0hK140= 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 02/11] tracing/user_events: Track fork/exec/exit for mm lifetime Date: Fri, 24 Mar 2023 15:30:19 -0700 Message-Id: <20230324223028.172-3-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" During tracefs discussions it was decided instead of requiring a mapping within a user-process to track the lifetime of memory descriptors we should hook the appropriate calls. Do this by adding the minimal stubs required for task fork, exec, and exit. Currently this is just a NOP. Future patches will implement these calls fully. Suggested-by: Mathieu Desnoyers Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- fs/exec.c | 2 ++ include/linux/sched.h | 5 +++++ include/linux/user_events.h | 18 ++++++++++++++++++ kernel/exit.c | 2 ++ kernel/fork.c | 2 ++ 5 files changed, 29 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index ab913243a367..d1c83e0dbae5 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -65,6 +65,7 @@ #include #include #include +#include =20 #include #include @@ -1856,6 +1857,7 @@ static int bprm_execve(struct linux_binprm *bprm, current->fs->in_exec =3D 0; current->in_execve =3D 0; rseq_execve(current); + user_events_execve(current); acct_update_integrals(current); task_numa_free(current, false); return retval; diff --git a/include/linux/sched.h b/include/linux/sched.h index 853d08f7562b..a8e683b4291c 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -69,6 +69,7 @@ struct sighand_struct; struct signal_struct; struct task_delay_info; struct task_group; +struct user_event_mm; =20 /* * Task state bitmask. NOTE! These bits are also @@ -1522,6 +1523,10 @@ struct task_struct { union rv_task_monitor rv[RV_PER_TASK_MONITORS]; #endif =20 +#ifdef CONFIG_USER_EVENTS + struct user_event_mm *user_event_mm; +#endif + /* * New fields for task_struct should be added above here, so that * they are included in the randomized portion of task_struct. diff --git a/include/linux/user_events.h b/include/linux/user_events.h index 13689589d36e..3d747c45d2fa 100644 --- a/include/linux/user_events.h +++ b/include/linux/user_events.h @@ -11,4 +11,22 @@ =20 #include =20 +#ifdef CONFIG_USER_EVENTS +struct user_event_mm { +}; +#endif + +static inline void user_events_fork(struct task_struct *t, + unsigned long clone_flags) +{ +} + +static inline void user_events_execve(struct task_struct *t) +{ +} + +static inline void user_events_exit(struct task_struct *t) +{ +} + #endif /* _LINUX_USER_EVENTS_H */ diff --git a/kernel/exit.c b/kernel/exit.c index 15dc2ec80c46..e2aaaa81b281 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -68,6 +68,7 @@ #include #include #include +#include =20 #include #include @@ -816,6 +817,7 @@ void __noreturn do_exit(long code) =20 coredump_task_exit(tsk); ptrace_event(PTRACE_EVENT_EXIT, code); + user_events_exit(tsk); =20 validate_creds_for_do_exit(tsk); =20 diff --git a/kernel/fork.c b/kernel/fork.c index 9f7fe3541897..180f6d86fbad 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -97,6 +97,7 @@ #include #include #include +#include =20 #include #include @@ -2502,6 +2503,7 @@ static __latent_entropy struct task_struct *copy_proc= ess( =20 trace_task_newtask(p, clone_flags); uprobe_copy_process(p, clone_flags); + user_events_fork(p, clone_flags); =20 copy_oom_score_adj(clone_flags, p); =20 --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DBF81C77B60 for ; Fri, 24 Mar 2023 22:31:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232381AbjCXWa7 (ORCPT ); Fri, 24 Mar 2023 18:30:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232034AbjCXWam (ORCPT ); Fri, 24 Mar 2023 18:30:42 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4850A16AF9; Fri, 24 Mar 2023 15:30:39 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 7DA9120FC4D5; Fri, 24 Mar 2023 15:30:38 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7DA9120FC4D5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697038; bh=R9INv6oij2jvfqxRYN7sTvDkp32JqHmiAHAQNiuLI7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ojCAXP9LAPpVzCFRnYWQ7g/HMbKr+csEOqDqIKUSeMzKQonLLH53QFb2nAYOLm6nD wtLTjY8sroa+Q3OokZvN6kMHl5/tFMIdS+atNoZ1ycjOGTpP8KXSmbY/JZ+TMwyJ+o tXqr/nOy3iDZeL5PgZNcKXqSyx77CyHjfYmKqM+M= 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 03/11] tracing/user_events: Use remote writes for event enablement Date: Fri, 24 Mar 2023 15:30:20 -0700 Message-Id: <20230324223028.172-4-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" As part of the discussions for user_events aligned with user space tracers, it was determined that user programs should register a aligned value to set or clear a bit when an event becomes enabled. Currently a shared page is being used that requires mmap(). Remove the shared page implementation and move to a user registered address implementation. In this new model during the event registration from user programs 3 new values are specified. The first is the address to update when the event is either enabled or disabled. The second is the bit to set/clear to reflect the event being enabled. The third is the size of the value at the specified address. This allows for a local 32/64-bit value in user programs to support both kernel and user tracers. As an example, setting bit 31 for kernel tracers when the event becomes enabled allows for user tracers to use the other bits for ref counts or other flags. The kernel side updates the bit atomically, user programs need to also update these values atomically. User provided addresses must be aligned on a natural boundary, this allows for single page checking and prevents odd behaviors such as a enable value straddling 2 pages instead of a single page. Currently page faults are only logged, future patches will handle these. Suggested-by: Mathieu Desnoyers Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- include/linux/user_events.h | 53 ++- include/uapi/linux/user_events.h | 15 +- kernel/trace/Kconfig | 5 +- kernel/trace/trace_events_user.c | 586 ++++++++++++++++++++++++------- 4 files changed, 517 insertions(+), 142 deletions(-) diff --git a/include/linux/user_events.h b/include/linux/user_events.h index 3d747c45d2fa..0120b3dd5b03 100644 --- a/include/linux/user_events.h +++ b/include/linux/user_events.h @@ -9,13 +9,63 @@ #ifndef _LINUX_USER_EVENTS_H #define _LINUX_USER_EVENTS_H =20 +#include +#include +#include +#include #include =20 #ifdef CONFIG_USER_EVENTS struct user_event_mm { + struct list_head link; + struct list_head enablers; + struct mm_struct *mm; + struct user_event_mm *next; + refcount_t refcnt; + refcount_t tasks; + struct rcu_work put_rwork; }; -#endif =20 +extern void user_event_mm_dup(struct task_struct *t, + struct user_event_mm *old_mm); + +extern void user_event_mm_remove(struct task_struct *t); + +static inline void user_events_fork(struct task_struct *t, + unsigned long clone_flags) +{ + struct user_event_mm *old_mm; + + if (!t || !current->user_event_mm) + return; + + old_mm =3D current->user_event_mm; + + if (clone_flags & CLONE_VM) { + t->user_event_mm =3D old_mm; + refcount_inc(&old_mm->tasks); + return; + } + + user_event_mm_dup(t, old_mm); +} + +static inline void user_events_execve(struct task_struct *t) +{ + if (!t || !t->user_event_mm) + return; + + user_event_mm_remove(t); +} + +static inline void user_events_exit(struct task_struct *t) +{ + if (!t || !t->user_event_mm) + return; + + user_event_mm_remove(t); +} +#else static inline void user_events_fork(struct task_struct *t, unsigned long clone_flags) { @@ -28,5 +78,6 @@ static inline void user_events_execve(struct task_struct = *t) static inline void user_events_exit(struct task_struct *t) { } +#endif /* CONFIG_USER_EVENTS */ =20 #endif /* _LINUX_USER_EVENTS_H */ diff --git a/include/uapi/linux/user_events.h b/include/uapi/linux/user_eve= nts.h index 03f92366068d..22521bc622db 100644 --- a/include/uapi/linux/user_events.h +++ b/include/uapi/linux/user_events.h @@ -27,12 +27,21 @@ struct user_reg { /* Input: Size of the user_reg structure being used */ __u32 size; =20 + /* Input: Bit in enable address to use */ + __u8 enable_bit; + + /* Input: Enable size in bytes at address */ + __u8 enable_size; + + /* Input: Flags for future use, set to 0 */ + __u16 flags; + + /* Input: Address to update when enabled */ + __u64 enable_addr; + /* Input: Pointer to string with event name, description and flags */ __u64 name_args; =20 - /* Output: Bitwise index of the event within the status page */ - __u32 status_bit; - /* Output: Index of the event to use when writing data */ __u32 write_index; } __attribute__((__packed__)); diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index d7043043f59c..b61a1bfbfc22 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -791,9 +791,10 @@ config USER_EVENTS can be used like an existing kernel trace event. User trace events are generated by writing to a tracefs file. User processes can determine if their tracing events should be - generated by memory mapping a tracefs file and checking for - an associated byte being non-zero. + generated by registering a value and bit with the kernel + that reflects when it is enabled or not. =20 + See Documentation/trace/user_events.rst. If in doubt, say N. =20 config HIST_TRIGGERS diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_u= ser.c index 070551480747..553a82ee7aeb 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "trace.h" #include "trace_dynevent.h" @@ -29,34 +30,11 @@ #define FIELD_DEPTH_NAME 1 #define FIELD_DEPTH_SIZE 2 =20 -/* - * Limits how many trace_event calls user processes can create: - * Must be a power of two of PAGE_SIZE. - */ -#define MAX_PAGE_ORDER 0 -#define MAX_PAGES (1 << MAX_PAGE_ORDER) -#define MAX_BYTES (MAX_PAGES * PAGE_SIZE) -#define MAX_EVENTS (MAX_BYTES * 8) - /* Limit how long of an event name plus args within the subsystem. */ #define MAX_EVENT_DESC 512 #define EVENT_NAME(user_event) ((user_event)->tracepoint.name) #define MAX_FIELD_ARRAY_SIZE 1024 =20 -/* - * The MAP_STATUS_* macros are used for taking a index and determining the - * appropriate byte and the bit in the byte to set/reset for an event. - * - * The lower 3 bits of the index decide which bit to set. - * The remaining upper bits of the index decide which byte to use for the = bit. - * - * This is used when an event has a probe attached/removed to reflect live - * status of the event wanting tracing or not to user-programs via shared - * memory maps. - */ -#define MAP_STATUS_BYTE(index) ((index) >> 3) -#define MAP_STATUS_MASK(index) BIT((index) & 7) - /* * Internal bits (kernel side only) to keep track of connected probes: * These are used when status is requested in text form about an event. Th= ese @@ -70,20 +48,14 @@ #define EVENT_STATUS_OTHER BIT(7) =20 /* - * Stores the pages, tables, and locks for a group of events. - * Each logical grouping of events has its own group, with a - * matching page for status checks within user programs. This - * allows for isolation of events to user programs by various - * means. + * Stores the system name, tables, and locks for a group of events. This + * allows isolation for events by various means. */ struct user_event_group { - struct page *pages; - char *register_page_data; char *system_name; struct hlist_node node; struct mutex reg_mutex; DECLARE_HASHTABLE(register_table, 8); - DECLARE_BITMAP(page_bitmap, MAX_EVENTS); }; =20 /* Group for init_user_ns mapping, top-most group */ @@ -106,12 +78,34 @@ struct user_event { struct list_head fields; struct list_head validators; refcount_t refcnt; - int index; - int flags; int min_size; char status; }; =20 +/* + * Stores per-mm/event properties that enable an address to be + * updated properly for each task. As tasks are forked, we use + * these to track enablement sites that are tied to an event. + */ +struct user_event_enabler { + struct list_head link; + struct user_event *event; + unsigned long addr; + + /* Track enable bit, flags, etc. Aligned for bitops. */ + unsigned int values; +}; + +/* Bits 0-5 are for the bit to update upon enable/disable (0-63 allowed) */ +#define ENABLE_VAL_BIT_MASK 0x3F + +/* Only duplicate the bit value */ +#define ENABLE_VAL_DUP_MASK ENABLE_VAL_BIT_MASK + +/* Global list of memory descriptors using user_events */ +static LIST_HEAD(user_event_mms); +static DEFINE_SPINLOCK(user_event_mms_lock); + /* * Stores per-file events references, as users register events * within a file this structure is modified and freed via RCU. @@ -145,33 +139,17 @@ static int user_event_parse(struct user_event_group *= group, char *name, char *args, char *flags, struct user_event **newuser); =20 +static struct user_event_mm *user_event_mm_get(struct user_event_mm *mm); +static struct user_event_mm *user_event_mm_get_all(struct user_event *user= ); +static void user_event_mm_put(struct user_event_mm *mm); + static u32 user_event_key(char *name) { return jhash(name, strlen(name), 0); } =20 -static void set_page_reservations(char *pages, bool set) -{ - int page; - - for (page =3D 0; page < MAX_PAGES; ++page) { - void *addr =3D pages + (PAGE_SIZE * page); - - if (set) - SetPageReserved(virt_to_page(addr)); - else - ClearPageReserved(virt_to_page(addr)); - } -} - static void user_event_group_destroy(struct user_event_group *group) { - if (group->register_page_data) - set_page_reservations(group->register_page_data, false); - - if (group->pages) - __free_pages(group->pages, MAX_PAGE_ORDER); - kfree(group->system_name); kfree(group); } @@ -242,19 +220,6 @@ static struct user_event_group if (!group->system_name) goto error; =20 - group->pages =3D alloc_pages(GFP_KERNEL | __GFP_ZERO, MAX_PAGE_ORDER); - - if (!group->pages) - goto error; - - group->register_page_data =3D page_address(group->pages); - - set_page_reservations(group->register_page_data, true); - - /* Zero all bits beside 0 (which is reserved for failures) */ - bitmap_zero(group->page_bitmap, MAX_EVENTS); - set_bit(0, group->page_bitmap); - mutex_init(&group->reg_mutex); hash_init(group->register_table); =20 @@ -266,20 +231,367 @@ static struct user_event_group return NULL; }; =20 -static __always_inline -void user_event_register_set(struct user_event *user) +static void user_event_enabler_destroy(struct user_event_enabler *enabler) +{ + list_del_rcu(&enabler->link); + + /* No longer tracking the event via the enabler */ + refcount_dec(&enabler->event->refcnt); + + kfree(enabler); +} + +static int user_event_mm_fault_in(struct user_event_mm *mm, unsigned long = uaddr) +{ + bool unlocked; + int ret; + + mmap_read_lock(mm->mm); + + /* Ensure MM has tasks, cannot use after exit_mm() */ + if (refcount_read(&mm->tasks) =3D=3D 0) { + ret =3D -ENOENT; + goto out; + } + + ret =3D fixup_user_fault(mm->mm, uaddr, FAULT_FLAG_WRITE | FAULT_FLAG_REM= OTE, + &unlocked); +out: + mmap_read_unlock(mm->mm); + + return ret; +} + +static int user_event_enabler_write(struct user_event_mm *mm, + struct user_event_enabler *enabler) +{ + unsigned long uaddr =3D enabler->addr; + unsigned long *ptr; + struct page *page; + void *kaddr; + int ret; + + lockdep_assert_held(&event_mutex); + mmap_assert_locked(mm->mm); + + /* Ensure MM has tasks, cannot use after exit_mm() */ + if (refcount_read(&mm->tasks) =3D=3D 0) + return -ENOENT; + + ret =3D pin_user_pages_remote(mm->mm, uaddr, 1, FOLL_WRITE | FOLL_NOFAULT, + &page, NULL, NULL); + + if (ret <=3D 0) { + pr_warn("user_events: Enable write failed\n"); + return -EFAULT; + } + + kaddr =3D kmap_local_page(page); + ptr =3D kaddr + (uaddr & ~PAGE_MASK); + + /* Update bit atomically, user tracers must be atomic as well */ + if (enabler->event && enabler->event->status) + set_bit(enabler->values & ENABLE_VAL_BIT_MASK, ptr); + else + clear_bit(enabler->values & ENABLE_VAL_BIT_MASK, ptr); + + kunmap_local(kaddr); + unpin_user_pages_dirty_lock(&page, 1, true); + + return 0; +} + +static void user_event_enabler_update(struct user_event *user) +{ + struct user_event_enabler *enabler; + struct user_event_mm *mm =3D user_event_mm_get_all(user); + struct user_event_mm *next; + + while (mm) { + next =3D mm->next; + mmap_read_lock(mm->mm); + rcu_read_lock(); + + list_for_each_entry_rcu(enabler, &mm->enablers, link) + if (enabler->event =3D=3D user) + user_event_enabler_write(mm, enabler); + + rcu_read_unlock(); + mmap_read_unlock(mm->mm); + user_event_mm_put(mm); + mm =3D next; + } +} + +static bool user_event_enabler_dup(struct user_event_enabler *orig, + struct user_event_mm *mm) +{ + struct user_event_enabler *enabler; + + enabler =3D kzalloc(sizeof(*enabler), GFP_NOWAIT); + + if (!enabler) + return false; + + enabler->event =3D orig->event; + enabler->addr =3D orig->addr; + + /* Only dup part of value (ignore future flags, etc) */ + enabler->values =3D orig->values & ENABLE_VAL_DUP_MASK; + + refcount_inc(&enabler->event->refcnt); + list_add_rcu(&enabler->link, &mm->enablers); + + return true; +} + +static struct user_event_mm *user_event_mm_get(struct user_event_mm *mm) +{ + refcount_inc(&mm->refcnt); + + return mm; +} + +static struct user_event_mm *user_event_mm_get_all(struct user_event *user) +{ + struct user_event_mm *found =3D NULL; + struct user_event_enabler *enabler; + struct user_event_mm *mm; + + /* + * We do not want to block fork/exec while enablements are being + * updated, so we use RCU to walk the current tasks that have used + * user_events ABI for 1 or more events. Each enabler found in each + * task that matches the event being updated has a write to reflect + * the kernel state back into the process. Waits/faults must not occur + * during this. So we scan the list under RCU for all the mm that have + * the event within it. This is needed because mm_read_lock() can wait. + * Each user mm returned has a ref inc to handle remove RCU races. + */ + rcu_read_lock(); + + list_for_each_entry_rcu(mm, &user_event_mms, link) + list_for_each_entry_rcu(enabler, &mm->enablers, link) + if (enabler->event =3D=3D user) { + mm->next =3D found; + found =3D user_event_mm_get(mm); + break; + } + + rcu_read_unlock(); + + return found; +} + +static struct user_event_mm *user_event_mm_create(struct task_struct *t) +{ + struct user_event_mm *user_mm; + unsigned long flags; + + user_mm =3D kzalloc(sizeof(*user_mm), GFP_KERNEL); + + if (!user_mm) + return NULL; + + user_mm->mm =3D t->mm; + INIT_LIST_HEAD(&user_mm->enablers); + refcount_set(&user_mm->refcnt, 1); + refcount_set(&user_mm->tasks, 1); + + spin_lock_irqsave(&user_event_mms_lock, flags); + list_add_rcu(&user_mm->link, &user_event_mms); + spin_unlock_irqrestore(&user_event_mms_lock, flags); + + t->user_event_mm =3D user_mm; + + /* + * The lifetime of the memory descriptor can slightly outlast + * the task lifetime if a ref to the user_event_mm is taken + * between list_del_rcu() and call_rcu(). Therefore we need + * to take a reference to it to ensure it can live this long + * under this corner case. This can also occur in clones that + * outlast the parent. + */ + mmgrab(user_mm->mm); + + return user_mm; +} + +static struct user_event_mm *current_user_event_mm(void) +{ + struct user_event_mm *user_mm =3D current->user_event_mm; + + if (user_mm) + goto inc; + + user_mm =3D user_event_mm_create(current); + + if (!user_mm) + goto error; +inc: + refcount_inc(&user_mm->refcnt); +error: + return user_mm; +} + +static void user_event_mm_destroy(struct user_event_mm *mm) +{ + struct user_event_enabler *enabler, *next; + + list_for_each_entry_safe(enabler, next, &mm->enablers, link) + user_event_enabler_destroy(enabler); + + mmdrop(mm->mm); + kfree(mm); +} + +static void user_event_mm_put(struct user_event_mm *mm) +{ + if (mm && refcount_dec_and_test(&mm->refcnt)) + user_event_mm_destroy(mm); +} + +static void delayed_user_event_mm_put(struct work_struct *work) +{ + struct user_event_mm *mm; + + mm =3D container_of(to_rcu_work(work), struct user_event_mm, put_rwork); + user_event_mm_put(mm); +} + +void user_event_mm_remove(struct task_struct *t) { - int i =3D user->index; + struct user_event_mm *mm; + unsigned long flags; + + might_sleep(); + + mm =3D t->user_event_mm; + t->user_event_mm =3D NULL; + + /* Clone will increment the tasks, only remove if last clone */ + if (!refcount_dec_and_test(&mm->tasks)) + return; + + /* Remove the mm from the list, so it can no longer be enabled */ + spin_lock_irqsave(&user_event_mms_lock, flags); + list_del_rcu(&mm->link); + spin_unlock_irqrestore(&user_event_mms_lock, flags); + + /* + * We need to wait for currently occurring writes to stop within + * the mm. This is required since exit_mm() snaps the current rss + * stats and clears them. On the final mmdrop(), check_mm() will + * report a bug if these increment. + * + * All writes/pins are done under mmap_read lock, take the write + * lock to ensure in-progress faults have completed. Faults that + * are pending but yet to run will check the task count and skip + * the fault since the mm is going away. + */ + mmap_write_lock(mm->mm); + mmap_write_unlock(mm->mm); =20 - user->group->register_page_data[MAP_STATUS_BYTE(i)] |=3D MAP_STATUS_MASK(= i); + /* + * Put for mm must be done after RCU delay to handle new refs in + * between the list_del_rcu() and now. This ensures any get refs + * during rcu_read_lock() are accounted for during list removal. + * + * CPU A | CPU B + * --------------------------------------------------------------- + * user_event_mm_remove() | rcu_read_lock(); + * list_del_rcu() | list_for_each_entry_rcu(); + * call_rcu() | refcount_inc(); + * . | rcu_read_unlock(); + * schedule_work() | . + * user_event_mm_put() | . + * + * mmdrop() cannot be called in the softirq context of call_rcu() + * so we use a work queue after call_rcu() to run within. + */ + INIT_RCU_WORK(&mm->put_rwork, delayed_user_event_mm_put); + queue_rcu_work(system_wq, &mm->put_rwork); } =20 -static __always_inline -void user_event_register_clear(struct user_event *user) +void user_event_mm_dup(struct task_struct *t, struct user_event_mm *old_mm) { - int i =3D user->index; + struct user_event_mm *mm =3D user_event_mm_create(t); + struct user_event_enabler *enabler; =20 - user->group->register_page_data[MAP_STATUS_BYTE(i)] &=3D ~MAP_STATUS_MASK= (i); + if (!mm) + return; + + rcu_read_lock(); + + list_for_each_entry_rcu(enabler, &old_mm->enablers, link) + if (!user_event_enabler_dup(enabler, mm)) + goto error; + + rcu_read_unlock(); + + return; +error: + rcu_read_unlock(); + user_event_mm_remove(t); +} + +static struct user_event_enabler +*user_event_enabler_create(struct user_reg *reg, struct user_event *user, + int *write_result) +{ + struct user_event_enabler *enabler; + struct user_event_mm *user_mm; + unsigned long uaddr =3D (unsigned long)reg->enable_addr; + + user_mm =3D current_user_event_mm(); + + if (!user_mm) + return NULL; + + enabler =3D kzalloc(sizeof(*enabler), GFP_KERNEL); + + if (!enabler) + goto out; + + enabler->event =3D user; + enabler->addr =3D uaddr; + enabler->values =3D reg->enable_bit; +retry: + /* Prevents state changes from racing with new enablers */ + mutex_lock(&event_mutex); + + /* Attempt to reflect the current state within the process */ + mmap_read_lock(user_mm->mm); + *write_result =3D user_event_enabler_write(user_mm, enabler); + mmap_read_unlock(user_mm->mm); + + /* + * If the write works, then we will track the enabler. A ref to the + * underlying user_event is held by the enabler to prevent it going + * away while the enabler is still in use by a process. The ref is + * removed when the enabler is destroyed. This means a event cannot + * be forcefully deleted from the system until all tasks using it + * exit or run exec(), which includes forks and clones. + */ + if (!*write_result) { + refcount_inc(&enabler->event->refcnt); + list_add_rcu(&enabler->link, &user_mm->enablers); + } + + mutex_unlock(&event_mutex); + + if (*write_result) { + /* Attempt to fault-in and retry if it worked */ + if (!user_event_mm_fault_in(user_mm, uaddr)) + goto retry; + + kfree(enabler); + enabler =3D NULL; + } +out: + user_event_mm_put(user_mm); + + return enabler; } =20 static __always_inline __must_check @@ -824,9 +1136,6 @@ static int destroy_user_event(struct user_event *user) return ret; =20 dyn_event_remove(&user->devent); - - user_event_register_clear(user); - clear_bit(user->index, user->group->page_bitmap); hash_del(&user->node); =20 user_event_destroy_validators(user); @@ -972,9 +1281,9 @@ static void user_event_perf(struct user_event *user, s= truct iov_iter *i, #endif =20 /* - * Update the register page that is shared between user processes. + * Update the enabled bit among all user processes. */ -static void update_reg_page_for(struct user_event *user) +static void update_enable_bit_for(struct user_event *user) { struct tracepoint *tp =3D &user->tracepoint; char status =3D 0; @@ -1005,12 +1314,9 @@ static void update_reg_page_for(struct user_event *u= ser) rcu_read_unlock_sched(); } =20 - if (status) - user_event_register_set(user); - else - user_event_register_clear(user); - user->status =3D status; + + user_event_enabler_update(user); } =20 /* @@ -1067,10 +1373,10 @@ static int user_event_reg(struct trace_event_call *= call, return ret; inc: refcount_inc(&user->refcnt); - update_reg_page_for(user); + update_enable_bit_for(user); return 0; dec: - update_reg_page_for(user); + update_enable_bit_for(user); refcount_dec(&user->refcnt); return 0; } @@ -1266,7 +1572,6 @@ static int user_event_parse(struct user_event_group *= group, char *name, struct user_event **newuser) { int ret; - int index; u32 key; struct user_event *user; =20 @@ -1285,11 +1590,6 @@ static int user_event_parse(struct user_event_group = *group, char *name, return 0; } =20 - index =3D find_first_zero_bit(group->page_bitmap, MAX_EVENTS); - - if (index =3D=3D MAX_EVENTS) - return -EMFILE; - user =3D kzalloc(sizeof(*user), GFP_KERNEL); =20 if (!user) @@ -1335,14 +1635,11 @@ static int user_event_parse(struct user_event_group= *group, char *name, if (ret) goto put_user_lock; =20 - user->index =3D index; - /* Ensure we track self ref and caller ref (2) */ refcount_set(&user->refcnt, 2); =20 dyn_event_init(&user->devent, &user_event_dops); dyn_event_add(&user->devent, &user->call); - set_bit(user->index, group->page_bitmap); hash_add(group->register_table, &user->node, key); =20 mutex_unlock(&event_mutex); @@ -1559,6 +1856,37 @@ static long user_reg_get(struct user_reg __user *ure= g, struct user_reg *kreg) if (ret) return ret; =20 + /* Ensure no flags, since we don't support any yet */ + if (kreg->flags !=3D 0) + return -EINVAL; + + /* Ensure supported size */ + switch (kreg->enable_size) { + case 4: + /* 32-bit */ + break; +#if BITS_PER_LONG >=3D 64 + case 8: + /* 64-bit */ + break; +#endif + default: + return -EINVAL; + } + + /* Ensure natural alignment */ + if (kreg->enable_addr % kreg->enable_size) + return -EINVAL; + + /* Ensure bit range for size */ + if (kreg->enable_bit > (kreg->enable_size * BITS_PER_BYTE) - 1) + return -EINVAL; + + /* Ensure accessible */ + if (!access_ok((const void __user *)(uintptr_t)kreg->enable_addr, + kreg->enable_size)) + return -EFAULT; + kreg->size =3D size; =20 return 0; @@ -1573,8 +1901,10 @@ static long user_events_ioctl_reg(struct user_event_= file_info *info, struct user_reg __user *ureg =3D (struct user_reg __user *)uarg; struct user_reg reg; struct user_event *user; + struct user_event_enabler *enabler; char *name; long ret; + int write_result; =20 ret =3D user_reg_get(ureg, ®); =20 @@ -1605,8 +1935,28 @@ static long user_events_ioctl_reg(struct user_event_= file_info *info, if (ret < 0) return ret; =20 + /* + * user_events_ref_add succeeded: + * At this point we have a user_event, it's lifetime is bound by the + * reference count, not this file. If anything fails, the user_event + * still has a reference until the file is released. During release + * any remaining references (from user_events_ref_add) are decremented. + * + * Attempt to create an enabler, which too has a lifetime tied in the + * same way for the event. Once the task that caused the enabler to be + * created exits or issues exec() then the enablers it has created + * will be destroyed and the ref to the event will be decremented. + */ + enabler =3D user_event_enabler_create(®, user, &write_result); + + if (!enabler) + return -ENOMEM; + + /* Write failed/faulted, give error back to caller */ + if (write_result) + return write_result; + put_user((u32)ret, &ureg->write_index); - put_user(user->index, &ureg->status_bit); =20 return 0; } @@ -1720,38 +2070,6 @@ static const struct file_operations user_data_fops = =3D { .release =3D user_events_release, }; =20 -static struct user_event_group *user_status_group(struct file *file) -{ - struct seq_file *m =3D file->private_data; - - if (!m) - return NULL; - - return m->private; -} - -/* - * Maps the shared page into the user process for checking if event is ena= bled. - */ -static int user_status_mmap(struct file *file, struct vm_area_struct *vma) -{ - char *pages; - struct user_event_group *group =3D user_status_group(file); - unsigned long size =3D vma->vm_end - vma->vm_start; - - if (size !=3D MAX_BYTES) - return -EINVAL; - - if (!group) - return -EINVAL; - - pages =3D group->register_page_data; - - return remap_pfn_range(vma, vma->vm_start, - virt_to_phys(pages) >> PAGE_SHIFT, - size, vm_get_page_prot(VM_READ)); -} - static void *user_seq_start(struct seq_file *m, loff_t *pos) { if (*pos) @@ -1775,7 +2093,7 @@ static int user_seq_show(struct seq_file *m, void *p) struct user_event_group *group =3D m->private; struct user_event *user; char status; - int i, active =3D 0, busy =3D 0, flags; + int i, active =3D 0, busy =3D 0; =20 if (!group) return -EINVAL; @@ -1784,11 +2102,10 @@ static int user_seq_show(struct seq_file *m, void *= p) =20 hash_for_each(group->register_table, i, user, node) { status =3D user->status; - flags =3D user->flags; =20 - seq_printf(m, "%d:%s", user->index, EVENT_NAME(user)); + seq_printf(m, "%s", EVENT_NAME(user)); =20 - if (flags !=3D 0 || status !=3D 0) + if (status !=3D 0) seq_puts(m, " #"); =20 if (status !=3D 0) { @@ -1811,7 +2128,6 @@ static int user_seq_show(struct seq_file *m, void *p) seq_puts(m, "\n"); seq_printf(m, "Active: %d\n", active); seq_printf(m, "Busy: %d\n", busy); - seq_printf(m, "Max: %ld\n", MAX_EVENTS); =20 return 0; } @@ -1847,7 +2163,6 @@ static int user_status_open(struct inode *node, struc= t file *file) =20 static const struct file_operations user_status_fops =3D { .open =3D user_status_open, - .mmap =3D user_status_mmap, .read =3D seq_read, .llseek =3D seq_lseek, .release =3D seq_release, @@ -1868,8 +2183,7 @@ static int create_user_tracefs(void) goto err; } =20 - /* mmap with MAP_SHARED requires writable fd */ - emmap =3D tracefs_create_file("user_events_status", TRACE_MODE_WRITE, + emmap =3D tracefs_create_file("user_events_status", TRACE_MODE_READ, NULL, NULL, &user_status_fops); =20 if (!emmap) { --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF9EFC76196 for ; Fri, 24 Mar 2023 22:30:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232134AbjCXWau (ORCPT ); Fri, 24 Mar 2023 18:30:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231946AbjCXWak (ORCPT ); Fri, 24 Mar 2023 18:30:40 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A4E2117146; Fri, 24 Mar 2023 15:30:39 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id EAA8820FC4D8; Fri, 24 Mar 2023 15:30:38 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com EAA8820FC4D8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697039; bh=LAT6es1zlcxHgyXQ+ccAd1DGKcAvO/9IswEU0C+E8Yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oKE14Q0TJsMW8crS4IdUxVfjibtp1Adb+m5vPIz1w7PWd/gaLD6ByRKpKoZ6hJIAV QSx/BQajYSaVfPowsEjY6WpZNyGtPpkiTD9St5vX9YoeYvRN9mpudZM7KR2JtuzcyX niHh77ZnPhF/Y7dm4J0dw725jyKQ5OmH50Vyk/GQ= 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 04/11] tracing/user_events: Fixup enable faults asyncly Date: Fri, 24 Mar 2023 15:30:21 -0700 Message-Id: <20230324223028.172-5-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When events are enabled within the various tracing facilities, such as ftrace/perf, the event_mutex is held. As events are enabled pages are accessed. We do not want page faults to occur under this lock. Instead queue the fault to a workqueue to be handled in a process context safe way without the lock. The enable address is marked faulting while the async fault-in occurs. This ensures that we don't attempt to fault-in more than is necessary. Once the page has been faulted in, an address write is re-attempted. If the page couldn't fault-in, then we wait until the next time the event is enabled to prevent any potential infinite loops. Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- kernel/trace/trace_events_user.c | 120 +++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 6 deletions(-) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_u= ser.c index 553a82ee7aeb..86bda1660536 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -99,9 +99,23 @@ struct user_event_enabler { /* Bits 0-5 are for the bit to update upon enable/disable (0-63 allowed) */ #define ENABLE_VAL_BIT_MASK 0x3F =20 +/* Bit 6 is for faulting status of enablement */ +#define ENABLE_VAL_FAULTING_BIT 6 + /* Only duplicate the bit value */ #define ENABLE_VAL_DUP_MASK ENABLE_VAL_BIT_MASK =20 +#define ENABLE_BITOPS(e) ((unsigned long *)&(e)->values) + +/* Used for asynchronous faulting in of pages */ +struct user_event_enabler_fault { + struct work_struct work; + struct user_event_mm *mm; + struct user_event_enabler *enabler; +}; + +static struct kmem_cache *fault_cache; + /* Global list of memory descriptors using user_events */ static LIST_HEAD(user_event_mms); static DEFINE_SPINLOCK(user_event_mms_lock); @@ -263,7 +277,85 @@ static int user_event_mm_fault_in(struct user_event_mm= *mm, unsigned long uaddr) } =20 static int user_event_enabler_write(struct user_event_mm *mm, - struct user_event_enabler *enabler) + struct user_event_enabler *enabler, + bool fixup_fault); + +static void user_event_enabler_fault_fixup(struct work_struct *work) +{ + struct user_event_enabler_fault *fault =3D container_of( + work, struct user_event_enabler_fault, work); + struct user_event_enabler *enabler =3D fault->enabler; + struct user_event_mm *mm =3D fault->mm; + unsigned long uaddr =3D enabler->addr; + int ret; + + ret =3D user_event_mm_fault_in(mm, uaddr); + + if (ret && ret !=3D -ENOENT) { + struct user_event *user =3D enabler->event; + + pr_warn("user_events: Fault for mm: 0x%pK @ 0x%llx event: %s\n", + mm->mm, (unsigned long long)uaddr, EVENT_NAME(user)); + } + + /* Prevent state changes from racing */ + mutex_lock(&event_mutex); + + /* + * If we managed to get the page, re-issue the write. We do not + * want to get into a possible infinite loop, which is why we only + * attempt again directly if the page came in. If we couldn't get + * the page here, then we will try again the next time the event is + * enabled/disabled. + */ + clear_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); + + if (!ret) { + mmap_read_lock(mm->mm); + user_event_enabler_write(mm, enabler, true); + mmap_read_unlock(mm->mm); + } + + mutex_unlock(&event_mutex); + + /* In all cases we no longer need the mm or fault */ + user_event_mm_put(mm); + kmem_cache_free(fault_cache, fault); +} + +static bool user_event_enabler_queue_fault(struct user_event_mm *mm, + struct user_event_enabler *enabler) +{ + struct user_event_enabler_fault *fault; + + fault =3D kmem_cache_zalloc(fault_cache, GFP_NOWAIT | __GFP_NOWARN); + + if (!fault) + return false; + + INIT_WORK(&fault->work, user_event_enabler_fault_fixup); + fault->mm =3D user_event_mm_get(mm); + fault->enabler =3D enabler; + + /* Don't try to queue in again while we have a pending fault */ + set_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); + + if (!schedule_work(&fault->work)) { + /* Allow another attempt later */ + clear_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); + + user_event_mm_put(mm); + kmem_cache_free(fault_cache, fault); + + return false; + } + + return true; +} + +static int user_event_enabler_write(struct user_event_mm *mm, + struct user_event_enabler *enabler, + bool fixup_fault) { unsigned long uaddr =3D enabler->addr; unsigned long *ptr; @@ -278,11 +370,19 @@ static int user_event_enabler_write(struct user_event= _mm *mm, if (refcount_read(&mm->tasks) =3D=3D 0) return -ENOENT; =20 + if (unlikely(test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)))) + return -EBUSY; + ret =3D pin_user_pages_remote(mm->mm, uaddr, 1, FOLL_WRITE | FOLL_NOFAULT, &page, NULL, NULL); =20 - if (ret <=3D 0) { - pr_warn("user_events: Enable write failed\n"); + if (unlikely(ret <=3D 0)) { + if (!fixup_fault) + return -EFAULT; + + if (!user_event_enabler_queue_fault(mm, enabler)) + pr_warn("user_events: Unable to queue fault handler\n"); + return -EFAULT; } =20 @@ -314,7 +414,7 @@ static void user_event_enabler_update(struct user_event= *user) =20 list_for_each_entry_rcu(enabler, &mm->enablers, link) if (enabler->event =3D=3D user) - user_event_enabler_write(mm, enabler); + user_event_enabler_write(mm, enabler, true); =20 rcu_read_unlock(); mmap_read_unlock(mm->mm); @@ -562,7 +662,7 @@ static struct user_event_enabler =20 /* Attempt to reflect the current state within the process */ mmap_read_lock(user_mm->mm); - *write_result =3D user_event_enabler_write(user_mm, enabler); + *write_result =3D user_event_enabler_write(user_mm, enabler, false); mmap_read_unlock(user_mm->mm); =20 /* @@ -2201,16 +2301,24 @@ static int __init trace_events_user_init(void) { int ret; =20 + fault_cache =3D KMEM_CACHE(user_event_enabler_fault, 0); + + if (!fault_cache) + return -ENOMEM; + init_group =3D user_event_group_create(&init_user_ns); =20 - if (!init_group) + if (!init_group) { + kmem_cache_destroy(fault_cache); return -ENOMEM; + } =20 ret =3D create_user_tracefs(); =20 if (ret) { pr_warn("user_events could not register with tracefs\n"); user_event_group_destroy(init_group); + kmem_cache_destroy(fault_cache); init_group =3D NULL; return ret; } --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8183EC761AF for ; Fri, 24 Mar 2023 22:30:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232341AbjCXWax (ORCPT ); Fri, 24 Mar 2023 18:30:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231959AbjCXWal (ORCPT ); Fri, 24 Mar 2023 18:30:41 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 020FB9030; Fri, 24 Mar 2023 15:30:40 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 4BAA320FC4DD; Fri, 24 Mar 2023 15:30:39 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4BAA320FC4DD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697039; bh=hPint7oilCioEigkIIslQUbcyG4C0cJ57YmYs/t8wm4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T1h0cFi4IJCvrrodn8sP35NqQ8H3yTURpNJVbQP8fli2BFNBDSwP5G0dK/5EcVkcN xzmLqb1hU09rPc0J+fRC13LdXN1uc0Bu+MF9PMC/aa3bYjWQfdUeosRjkwvewFsjUC wWGcICmEV4CWi+9SAfDX6w/0+ftkvkk+3dqTe88I= 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 05/11] tracing/user_events: Add ioctl for disabling addresses Date: Fri, 24 Mar 2023 15:30:22 -0700 Message-Id: <20230324223028.172-6-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Enablements are now tracked by the lifetime of the task/mm. User processes need to be able to disable their addresses if tracing is requested to be turned off. Before unmapping the page would suffice. However, we now need a stronger contract. Add an ioctl to enable this. A new flag bit is added, freeing, to user_event_enabler to ensure that if the event is attempted to be removed while a fault is being handled that the remove is delayed until after the fault is reattempted. Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- include/uapi/linux/user_events.h | 24 +++++++++ kernel/trace/trace_events_user.c | 93 +++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/user_events.h b/include/uapi/linux/user_eve= nts.h index 22521bc622db..3e7275e3234a 100644 --- a/include/uapi/linux/user_events.h +++ b/include/uapi/linux/user_events.h @@ -46,6 +46,27 @@ struct user_reg { __u32 write_index; } __attribute__((__packed__)); =20 +/* + * Describes an event unregister, callers must set the size, address and b= it. + * This structure is passed to the DIAG_IOCSUNREG ioctl to disable bit upd= ates. + */ +struct user_unreg { + /* Input: Size of the user_unreg structure being used */ + __u32 size; + + /* Input: Bit to unregister */ + __u8 disable_bit; + + /* Input: Reserved, set to 0 */ + __u8 __reserved; + + /* Input: Reserved, set to 0 */ + __u16 __reserved2; + + /* Input: Address to unregister */ + __u64 disable_addr; +} __attribute__((__packed__)); + #define DIAG_IOC_MAGIC '*' =20 /* Request to register a user_event */ @@ -54,4 +75,7 @@ struct user_reg { /* Request to delete a user_event */ #define DIAG_IOCSDEL _IOW(DIAG_IOC_MAGIC, 1, char *) =20 +/* Requests to unregister a user_event */ +#define DIAG_IOCSUNREG _IOW(DIAG_IOC_MAGIC, 2, struct user_unreg*) + #endif /* _UAPI_LINUX_USER_EVENTS_H */ diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_u= ser.c index 86bda1660536..e4ee25d16f3b 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -102,6 +102,9 @@ struct user_event_enabler { /* Bit 6 is for faulting status of enablement */ #define ENABLE_VAL_FAULTING_BIT 6 =20 +/* Bit 7 is for freeing status of enablement */ +#define ENABLE_VAL_FREEING_BIT 7 + /* Only duplicate the bit value */ #define ENABLE_VAL_DUP_MASK ENABLE_VAL_BIT_MASK =20 @@ -301,6 +304,12 @@ static void user_event_enabler_fault_fixup(struct work= _struct *work) /* Prevent state changes from racing */ mutex_lock(&event_mutex); =20 + /* User asked for enabler to be removed during fault */ + if (test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler))) { + user_event_enabler_destroy(enabler); + goto out; + } + /* * If we managed to get the page, re-issue the write. We do not * want to get into a possible infinite loop, which is why we only @@ -315,7 +324,7 @@ static void user_event_enabler_fault_fixup(struct work_= struct *work) user_event_enabler_write(mm, enabler, true); mmap_read_unlock(mm->mm); } - +out: mutex_unlock(&event_mutex); =20 /* In all cases we no longer need the mm or fault */ @@ -370,7 +379,8 @@ static int user_event_enabler_write(struct user_event_m= m *mm, if (refcount_read(&mm->tasks) =3D=3D 0) return -ENOENT; =20 - if (unlikely(test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)))) + if (unlikely(test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)) || + test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler)))) return -EBUSY; =20 ret =3D pin_user_pages_remote(mm->mm, uaddr, 1, FOLL_WRITE | FOLL_NOFAULT, @@ -428,6 +438,10 @@ static bool user_event_enabler_dup(struct user_event_e= nabler *orig, { struct user_event_enabler *enabler; =20 + /* Skip pending frees */ + if (unlikely(test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(orig)))) + return true; + enabler =3D kzalloc(sizeof(*enabler), GFP_NOWAIT); =20 if (!enabler) @@ -2086,6 +2100,75 @@ static long user_events_ioctl_del(struct user_event_= file_info *info, return ret; } =20 +static long user_unreg_get(struct user_unreg __user *ureg, + struct user_unreg *kreg) +{ + u32 size; + long ret; + + ret =3D get_user(size, &ureg->size); + + if (ret) + return ret; + + if (size > PAGE_SIZE) + return -E2BIG; + + if (size < offsetofend(struct user_unreg, disable_addr)) + return -EINVAL; + + ret =3D copy_struct_from_user(kreg, sizeof(*kreg), ureg, size); + + return ret; +} + +/* + * Unregisters an enablement address/bit within a task/user mm. + */ +static long user_events_ioctl_unreg(unsigned long uarg) +{ + struct user_unreg __user *ureg =3D (struct user_unreg __user *)uarg; + struct user_event_mm *mm =3D current->user_event_mm; + struct user_event_enabler *enabler, *next; + struct user_unreg reg; + long ret; + + ret =3D user_unreg_get(ureg, ®); + + if (ret) + return ret; + + if (!mm) + return -ENOENT; + + ret =3D -ENOENT; + + /* + * Flags freeing and faulting are used to indicate if the enabler is in + * use at all. When faulting is set a page-fault is occurring asyncly. + * During async fault if freeing is set, the enabler will be destroyed. + * If no async fault is happening, we can destroy it now since we hold + * the event_mutex during these checks. + */ + mutex_lock(&event_mutex); + + list_for_each_entry_safe(enabler, next, &mm->enablers, link) + if (enabler->addr =3D=3D reg.disable_addr && + (enabler->values & ENABLE_VAL_BIT_MASK) =3D=3D reg.disable_bit) { + set_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler)); + + if (!test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler))) + user_event_enabler_destroy(enabler); + + /* Removed at least one */ + ret =3D 0; + } + + mutex_unlock(&event_mutex); + + return ret; +} + /* * Handles the ioctl from user mode to register or alter operations. */ @@ -2108,6 +2191,12 @@ static long user_events_ioctl(struct file *file, uns= igned int cmd, ret =3D user_events_ioctl_del(info, uarg); mutex_unlock(&group->reg_mutex); break; + + case DIAG_IOCSUNREG: + mutex_lock(&group->reg_mutex); + ret =3D user_events_ioctl_unreg(uarg); + mutex_unlock(&group->reg_mutex); + break; } =20 return ret; --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CABEC761AF for ; Fri, 24 Mar 2023 22:31:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232400AbjCXWbB (ORCPT ); Fri, 24 Mar 2023 18:31:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232056AbjCXWam (ORCPT ); Fri, 24 Mar 2023 18:30:42 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 66AB016AEA; Fri, 24 Mar 2023 15:30:40 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id A65E120FC4E1; Fri, 24 Mar 2023 15:30:39 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A65E120FC4E1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697040; bh=4Tw0x0REBVNQbPmE7YKz+0Y5T7GJmSyJL4LF57PSOTg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HRbZanlT+ZZI3iwVyPol6+HtafjrbTszHgAzyzcgDF6BXzD6PMknyNvu8Q6eYHKUG YinkwGuDCxEczZC6Iz1GLlNjwA6/MJoZZsI+CrFgGsSKL+Um1gzfOdWwvTtsMARbvM AkeFdsayVf1oqJyT9JtV9bbeeAOPZ67C0rC0WnXk= 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 06/11] tracing/user_events: Update self-tests to write ABI Date: Fri, 24 Mar 2023 15:30:23 -0700 Message-Id: <20230324223028.172-7-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" ABI has been changed to remote writes, update existing test cases to use this new ABI to ensure existing functionality continues to work. Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- .../testing/selftests/user_events/dyn_test.c | 2 +- .../selftests/user_events/ftrace_test.c | 162 ++++++++++-------- .../testing/selftests/user_events/perf_test.c | 39 ++--- 3 files changed, 105 insertions(+), 98 deletions(-) diff --git a/tools/testing/selftests/user_events/dyn_test.c b/tools/testing= /selftests/user_events/dyn_test.c index d6265d14cd51..8879a7b04c6a 100644 --- a/tools/testing/selftests/user_events/dyn_test.c +++ b/tools/testing/selftests/user_events/dyn_test.c @@ -16,7 +16,7 @@ =20 #include "../kselftest_harness.h" =20 -const char *dyn_file =3D "/sys/kernel/debug/tracing/dynamic_events"; +const char *dyn_file =3D "/sys/kernel/tracing/dynamic_events"; const char *clear =3D "!u:__test_event"; =20 static int Append(const char *value) diff --git a/tools/testing/selftests/user_events/ftrace_test.c b/tools/test= ing/selftests/user_events/ftrace_test.c index 404a2713dcae..aceafacfb126 100644 --- a/tools/testing/selftests/user_events/ftrace_test.c +++ b/tools/testing/selftests/user_events/ftrace_test.c @@ -12,20 +12,16 @@ #include #include #include +#include #include =20 #include "../kselftest_harness.h" =20 -const char *data_file =3D "/sys/kernel/debug/tracing/user_events_data"; -const char *status_file =3D "/sys/kernel/debug/tracing/user_events_status"; -const char *enable_file =3D "/sys/kernel/debug/tracing/events/user_events/= __test_event/enable"; -const char *trace_file =3D "/sys/kernel/debug/tracing/trace"; -const char *fmt_file =3D "/sys/kernel/debug/tracing/events/user_events/__t= est_event/format"; - -static inline int status_check(char *status_page, int status_bit) -{ - return status_page[status_bit >> 3] & (1 << (status_bit & 7)); -} +const char *data_file =3D "/sys/kernel/tracing/user_events_data"; +const char *status_file =3D "/sys/kernel/tracing/user_events_status"; +const char *enable_file =3D "/sys/kernel/tracing/events/user_events/__test= _event/enable"; +const char *trace_file =3D "/sys/kernel/tracing/trace"; +const char *fmt_file =3D "/sys/kernel/tracing/events/user_events/__test_ev= ent/format"; =20 static int trace_bytes(void) { @@ -106,13 +102,23 @@ static int get_print_fmt(char *buffer, int len) return -1; } =20 -static int clear(void) +static int clear(int *check) { + struct user_unreg unreg =3D {0}; + + unreg.size =3D sizeof(unreg); + unreg.disable_bit =3D 31; + unreg.disable_addr =3D (__u64)check; + int fd =3D open(data_file, O_RDWR); =20 if (fd =3D=3D -1) return -1; =20 + if (ioctl(fd, DIAG_IOCSUNREG, &unreg) =3D=3D -1) + if (errno !=3D ENOENT) + return -1; + if (ioctl(fd, DIAG_IOCSDEL, "__test_event") =3D=3D -1) if (errno !=3D ENOENT) return -1; @@ -122,7 +128,7 @@ static int clear(void) return 0; } =20 -static int check_print_fmt(const char *event, const char *expected) +static int check_print_fmt(const char *event, const char *expected, int *c= heck) { struct user_reg reg =3D {0}; char print_fmt[256]; @@ -130,7 +136,7 @@ static int check_print_fmt(const char *event, const cha= r *expected) int fd; =20 /* Ensure cleared */ - ret =3D clear(); + ret =3D clear(check); =20 if (ret !=3D 0) return ret; @@ -142,14 +148,19 @@ static int check_print_fmt(const char *event, const c= har *expected) =20 reg.size =3D sizeof(reg); reg.name_args =3D (__u64)event; + reg.enable_bit =3D 31; + reg.enable_addr =3D (__u64)check; + reg.enable_size =3D sizeof(*check); =20 /* Register should work */ ret =3D ioctl(fd, DIAG_IOCSREG, ®); =20 close(fd); =20 - if (ret !=3D 0) + if (ret !=3D 0) { + printf("Reg failed in fmt\n"); return ret; + } =20 /* Ensure correct print_fmt */ ret =3D get_print_fmt(print_fmt, sizeof(print_fmt)); @@ -164,6 +175,7 @@ FIXTURE(user) { int status_fd; int data_fd; int enable_fd; + int check; }; =20 FIXTURE_SETUP(user) { @@ -185,59 +197,56 @@ FIXTURE_TEARDOWN(user) { close(self->enable_fd); } =20 - ASSERT_EQ(0, clear()); + if (clear(&self->check) !=3D 0) + printf("WARNING: Clear didn't work!\n"); } =20 TEST_F(user, register_events) { struct user_reg reg =3D {0}; - int page_size =3D sysconf(_SC_PAGESIZE); - char *status_page; + struct user_unreg unreg =3D {0}; =20 reg.size =3D sizeof(reg); reg.name_args =3D (__u64)"__test_event u32 field1; u32 field2"; + reg.enable_bit =3D 31; + reg.enable_addr =3D (__u64)&self->check; + reg.enable_size =3D sizeof(self->check); =20 - status_page =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, - self->status_fd, 0); + unreg.size =3D sizeof(unreg); + unreg.disable_bit =3D 31; + unreg.disable_addr =3D (__u64)&self->check; =20 /* Register should work */ ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®)); ASSERT_EQ(0, reg.write_index); - ASSERT_NE(0, reg.status_bit); =20 /* Multiple registers should result in same index */ ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®)); ASSERT_EQ(0, reg.write_index); - ASSERT_NE(0, reg.status_bit); =20 /* Ensure disabled */ self->enable_fd =3D open(enable_file, O_RDWR); ASSERT_NE(-1, self->enable_fd); ASSERT_NE(-1, write(self->enable_fd, "0", sizeof("0"))) =20 - /* MMAP should work and be zero'd */ - ASSERT_NE(MAP_FAILED, status_page); - ASSERT_NE(NULL, status_page); - ASSERT_EQ(0, status_check(status_page, reg.status_bit)); - /* Enable event and ensure bits updated in status */ ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1"))) - ASSERT_NE(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(1 << reg.enable_bit, self->check); =20 /* Disable event and ensure bits updated in status */ ASSERT_NE(-1, write(self->enable_fd, "0", sizeof("0"))) - ASSERT_EQ(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(0, self->check); =20 /* File still open should return -EBUSY for delete */ ASSERT_EQ(-1, ioctl(self->data_fd, DIAG_IOCSDEL, "__test_event")); ASSERT_EQ(EBUSY, errno); =20 - /* Delete should work only after close */ + /* Unregister */ + ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSUNREG, &unreg)); + + /* Delete should work only after close and unregister */ close(self->data_fd); self->data_fd =3D open(data_file, O_RDWR); ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSDEL, "__test_event")); - - /* Unmap should work */ - ASSERT_EQ(0, munmap(status_page, page_size)); } =20 TEST_F(user, write_events) { @@ -245,11 +254,12 @@ TEST_F(user, write_events) { struct iovec io[3]; __u32 field1, field2; int before =3D 0, after =3D 0; - int page_size =3D sysconf(_SC_PAGESIZE); - char *status_page; =20 reg.size =3D sizeof(reg); reg.name_args =3D (__u64)"__test_event u32 field1; u32 field2"; + reg.enable_bit =3D 31; + reg.enable_addr =3D (__u64)&self->check; + reg.enable_size =3D sizeof(self->check); =20 field1 =3D 1; field2 =3D 2; @@ -261,18 +271,10 @@ TEST_F(user, write_events) { io[2].iov_base =3D &field2; io[2].iov_len =3D sizeof(field2); =20 - status_page =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, - self->status_fd, 0); - /* Register should work */ ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®)); ASSERT_EQ(0, reg.write_index); - ASSERT_NE(0, reg.status_bit); - - /* MMAP should work and be zero'd */ - ASSERT_NE(MAP_FAILED, status_page); - ASSERT_NE(NULL, status_page); - ASSERT_EQ(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(0, self->check); =20 /* Write should fail on invalid slot with ENOENT */ io[0].iov_base =3D &field2; @@ -287,7 +289,7 @@ TEST_F(user, write_events) { ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1"))) =20 /* Event should now be enabled */ - ASSERT_NE(0, status_check(status_page, reg.status_bit)); + ASSERT_NE(1 << reg.enable_bit, self->check); =20 /* Write should make it out to ftrace buffers */ before =3D trace_bytes(); @@ -304,6 +306,9 @@ TEST_F(user, write_fault) { =20 reg.size =3D sizeof(reg); reg.name_args =3D (__u64)"__test_event u64 anon"; + reg.enable_bit =3D 31; + reg.enable_addr =3D (__u64)&self->check; + reg.enable_size =3D sizeof(self->check); =20 anon =3D mmap(NULL, l, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); ASSERT_NE(MAP_FAILED, anon); @@ -316,7 +321,6 @@ TEST_F(user, write_fault) { /* Register should work */ ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®)); ASSERT_EQ(0, reg.write_index); - ASSERT_NE(0, reg.status_bit); =20 /* Write should work normally */ ASSERT_NE(-1, writev(self->data_fd, (const struct iovec *)io, 2)); @@ -333,24 +337,17 @@ TEST_F(user, write_validator) { int loc, bytes; char data[8]; int before =3D 0, after =3D 0; - int page_size =3D sysconf(_SC_PAGESIZE); - char *status_page; - - status_page =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, - self->status_fd, 0); =20 reg.size =3D sizeof(reg); reg.name_args =3D (__u64)"__test_event __rel_loc char[] data"; + reg.enable_bit =3D 31; + reg.enable_addr =3D (__u64)&self->check; + reg.enable_size =3D sizeof(self->check); =20 /* Register should work */ ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®)); ASSERT_EQ(0, reg.write_index); - ASSERT_NE(0, reg.status_bit); - - /* MMAP should work and be zero'd */ - ASSERT_NE(MAP_FAILED, status_page); - ASSERT_NE(NULL, status_page); - ASSERT_EQ(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(0, self->check); =20 io[0].iov_base =3D ®.write_index; io[0].iov_len =3D sizeof(reg.write_index); @@ -369,7 +366,7 @@ TEST_F(user, write_validator) { ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1"))) =20 /* Event should now be enabled */ - ASSERT_NE(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(1 << reg.enable_bit, self->check); =20 /* Full in-bounds write should work */ before =3D trace_bytes(); @@ -409,71 +406,88 @@ TEST_F(user, print_fmt) { int ret; =20 ret =3D check_print_fmt("__test_event __rel_loc char[] data", - "print fmt: \"data=3D%s\", __get_rel_str(data)"); + "print fmt: \"data=3D%s\", __get_rel_str(data)", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event __data_loc char[] data", - "print fmt: \"data=3D%s\", __get_str(data)"); + "print fmt: \"data=3D%s\", __get_str(data)", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event s64 data", - "print fmt: \"data=3D%lld\", REC->data"); + "print fmt: \"data=3D%lld\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event u64 data", - "print fmt: \"data=3D%llu\", REC->data"); + "print fmt: \"data=3D%llu\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event s32 data", - "print fmt: \"data=3D%d\", REC->data"); + "print fmt: \"data=3D%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event u32 data", - "print fmt: \"data=3D%u\", REC->data"); + "print fmt: \"data=3D%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event int data", - "print fmt: \"data=3D%d\", REC->data"); + "print fmt: \"data=3D%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event unsigned int data", - "print fmt: \"data=3D%u\", REC->data"); + "print fmt: \"data=3D%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event s16 data", - "print fmt: \"data=3D%d\", REC->data"); + "print fmt: \"data=3D%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event u16 data", - "print fmt: \"data=3D%u\", REC->data"); + "print fmt: \"data=3D%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event short data", - "print fmt: \"data=3D%d\", REC->data"); + "print fmt: \"data=3D%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event unsigned short data", - "print fmt: \"data=3D%u\", REC->data"); + "print fmt: \"data=3D%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event s8 data", - "print fmt: \"data=3D%d\", REC->data"); + "print fmt: \"data=3D%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event u8 data", - "print fmt: \"data=3D%u\", REC->data"); + "print fmt: \"data=3D%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event char data", - "print fmt: \"data=3D%d\", REC->data"); + "print fmt: \"data=3D%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event unsigned char data", - "print fmt: \"data=3D%u\", REC->data"); + "print fmt: \"data=3D%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); =20 ret =3D check_print_fmt("__test_event char[4] data", - "print fmt: \"data=3D%s\", REC->data"); + "print fmt: \"data=3D%s\", REC->data", + &self->check); ASSERT_EQ(0, ret); } =20 diff --git a/tools/testing/selftests/user_events/perf_test.c b/tools/testin= g/selftests/user_events/perf_test.c index 8b4c7879d5a7..a070258d4449 100644 --- a/tools/testing/selftests/user_events/perf_test.c +++ b/tools/testing/selftests/user_events/perf_test.c @@ -18,10 +18,9 @@ =20 #include "../kselftest_harness.h" =20 -const char *data_file =3D "/sys/kernel/debug/tracing/user_events_data"; -const char *status_file =3D "/sys/kernel/debug/tracing/user_events_status"; -const char *id_file =3D "/sys/kernel/debug/tracing/events/user_events/__te= st_event/id"; -const char *fmt_file =3D "/sys/kernel/debug/tracing/events/user_events/__t= est_event/format"; +const char *data_file =3D "/sys/kernel/tracing/user_events_data"; +const char *id_file =3D "/sys/kernel/tracing/events/user_events/__test_eve= nt/id"; +const char *fmt_file =3D "/sys/kernel/tracing/events/user_events/__test_ev= ent/format"; =20 struct event { __u32 index; @@ -35,11 +34,6 @@ static long perf_event_open(struct perf_event_attr *pe, = pid_t pid, return syscall(__NR_perf_event_open, pe, pid, cpu, group_fd, flags); } =20 -static inline int status_check(char *status_page, int status_bit) -{ - return status_page[status_bit >> 3] & (1 << (status_bit & 7)); -} - static int get_id(void) { FILE *fp =3D fopen(id_file, "r"); @@ -88,45 +82,38 @@ static int get_offset(void) } =20 FIXTURE(user) { - int status_fd; int data_fd; + int check; }; =20 FIXTURE_SETUP(user) { - self->status_fd =3D open(status_file, O_RDONLY); - ASSERT_NE(-1, self->status_fd); - self->data_fd =3D open(data_file, O_RDWR); ASSERT_NE(-1, self->data_fd); } =20 FIXTURE_TEARDOWN(user) { - close(self->status_fd); close(self->data_fd); } =20 TEST_F(user, perf_write) { struct perf_event_attr pe =3D {0}; struct user_reg reg =3D {0}; - int page_size =3D sysconf(_SC_PAGESIZE); - char *status_page; struct event event; struct perf_event_mmap_page *perf_page; + int page_size =3D sysconf(_SC_PAGESIZE); int id, fd, offset; __u32 *val; =20 reg.size =3D sizeof(reg); reg.name_args =3D (__u64)"__test_event u32 field1; u32 field2"; - - status_page =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, - self->status_fd, 0); - ASSERT_NE(MAP_FAILED, status_page); + reg.enable_bit =3D 31; + reg.enable_addr =3D (__u64)&self->check; + reg.enable_size =3D sizeof(self->check); =20 /* Register should work */ ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®)); ASSERT_EQ(0, reg.write_index); - ASSERT_NE(0, reg.status_bit); - ASSERT_EQ(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(0, self->check); =20 /* Id should be there */ id =3D get_id(); @@ -149,7 +136,7 @@ TEST_F(user, perf_write) { ASSERT_NE(MAP_FAILED, perf_page); =20 /* Status should be updated */ - ASSERT_NE(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(1 << reg.enable_bit, self->check); =20 event.index =3D reg.write_index; event.field1 =3D 0xc001; @@ -165,6 +152,12 @@ TEST_F(user, perf_write) { /* Ensure correct */ ASSERT_EQ(event.field1, *val++); ASSERT_EQ(event.field2, *val++); + + munmap(perf_page, page_size * 2); + close(fd); + + /* Status should be updated */ + ASSERT_EQ(0, self->check); } =20 int main(int argc, char **argv) --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E62E6C76196 for ; Fri, 24 Mar 2023 22:30:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232220AbjCXWa5 (ORCPT ); Fri, 24 Mar 2023 18:30:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232045AbjCXWam (ORCPT ); Fri, 24 Mar 2023 18:30:42 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CA8AA16AF0; Fri, 24 Mar 2023 15:30:40 -0700 (PDT) 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Acked-by: Masami Hiramatsu (Google) --- 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/s= elftests/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 +=3D -Wl,-no-as-needed -Wall -I../../../../usr/include LDLIBS +=3D -lrt -lpthread -lm =20 -TEST_GEN_PROGS =3D ftrace_test dyn_test perf_test +TEST_GEN_PROGS =3D ftrace_test dyn_test perf_test abi_test =20 TEST_FILES :=3D settings =20 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 =3D "/sys/kernel/tracing/user_events_data"; +const char *enable_file =3D "/sys/kernel/tracing/events/user_events/__abi_= event/enable"; + +static int change_event(bool enable) +{ + int fd =3D open(enable_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + if (enable) + ret =3D write(fd, "1", 1); + else + ret =3D write(fd, "0", 1); + + close(fd); + + if (ret =3D=3D 1) + ret =3D 0; + else + ret =3D -1; + + return ret; +} + +static int reg_enable(long *enable, int size, int bit) +{ + struct user_reg reg =3D {0}; + int fd =3D open(data_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + reg.size =3D sizeof(reg); + reg.name_args =3D (__u64)"__abi_event"; + reg.enable_bit =3D bit; + reg.enable_addr =3D (__u64)enable; + reg.enable_size =3D size; + + ret =3D ioctl(fd, DIAG_IOCSREG, ®); + + close(fd); + + return ret; +} + +static int reg_disable(long *enable, int bit) +{ + struct user_unreg reg =3D {0}; + int fd =3D open(data_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + reg.size =3D sizeof(reg); + reg.disable_bit =3D bit; + reg.disable_addr =3D (__u64)enable; + + ret =3D ioctl(fd, DIAG_IOCSUNREG, ®); + + close(fd); + + return ret; +} + +FIXTURE(user) { + long check; +}; + +FIXTURE_SETUP(user) { + change_event(false); + self->check =3D 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 =3D 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 =3D=3D 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() =3D=3D 0) { + /* Force COW */ + self->check =3D 0; + + /* Up to 1 sec for enablement */ + for (i =3D 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() =3D=3D 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 =3D 0; i < 10; ++i) { + usleep(100000); + + if (*(long *)check) + return 0; + } + + return 1; +} + +TEST_F(user, clones) { + int i, stack_size =3D 4096; + void *stack =3D 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); +} --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58C95C76195 for ; Fri, 24 Mar 2023 22:31:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232406AbjCXWbF (ORCPT ); Fri, 24 Mar 2023 18:31:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232060AbjCXWan (ORCPT ); Fri, 24 Mar 2023 18:30:43 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 39D9917152; Fri, 24 Mar 2023 15:30:41 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 7D5A920FC4EC; Fri, 24 Mar 2023 15:30:40 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7D5A920FC4EC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697040; bh=dmkEXknb1A0Qhw2AWPTXryudmpuAFo4IqxhDoe6dNiw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eGbWIZ+r+NMvwDlV7XV+fCUEiVJiQbMzovpYLN/E3H38bh/7YBwM0+1hJuMB4rsfB jJVsETEGjoRj8UKVYM4pBFS5ASkm/fA6tWAGr35SDqDY5I78WDoAXrtQG4hHsz/05i UPzzlNG3HxCT4Sr4nBjRwpdhvvsCOMndyRzJPfvE= 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 08/11] tracing/user_events: Use write ABI in example Date: Fri, 24 Mar 2023 15:30:25 -0700 Message-Id: <20230324223028.172-9-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The ABI has changed to use a remote write approach. Update the example to show the expected use of this new ABI. Also remove debugfs path and use tracefs to ensure example works in more environments. Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- samples/user_events/example.c | 47 +++++++---------------------------- 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/samples/user_events/example.c b/samples/user_events/example.c index d06dc24156ec..28165a096697 100644 --- a/samples/user_events/example.c +++ b/samples/user_events/example.c @@ -9,51 +9,28 @@ #include #include #include +#include #include #include #include -#include -#include #include =20 -#if __BITS_PER_LONG =3D=3D 64 -#define endian_swap(x) htole64(x) -#else -#define endian_swap(x) htole32(x) -#endif +const char *data_file =3D "/sys/kernel/tracing/user_events_data"; +int enabled =3D 0; =20 -/* Assumes debugfs is mounted */ -const char *data_file =3D "/sys/kernel/debug/tracing/user_events_data"; -const char *status_file =3D "/sys/kernel/debug/tracing/user_events_status"; - -static int event_status(long **status) -{ - int fd =3D open(status_file, O_RDONLY); - - *status =3D mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ, - MAP_SHARED, fd, 0); - - close(fd); - - if (*status =3D=3D MAP_FAILED) - return -1; - - return 0; -} - -static int event_reg(int fd, const char *command, long *index, long *mask, - int *write) +static int event_reg(int fd, const char *command, int *write, int *enabled) { struct user_reg reg =3D {0}; =20 reg.size =3D sizeof(reg); + reg.enable_bit =3D 31; + reg.enable_size =3D sizeof(*enabled); + reg.enable_addr =3D (__u64)enabled; reg.name_args =3D (__u64)command; =20 if (ioctl(fd, DIAG_IOCSREG, ®) =3D=3D -1) return -1; =20 - *index =3D reg.status_bit / __BITS_PER_LONG; - *mask =3D endian_swap(1L << (reg.status_bit % __BITS_PER_LONG)); *write =3D reg.write_index; =20 return 0; @@ -62,17 +39,12 @@ static int event_reg(int fd, const char *command, long = *index, long *mask, int main(int argc, char **argv) { int data_fd, write; - long index, mask; - long *status_page; struct iovec io[2]; __u32 count =3D 0; =20 - if (event_status(&status_page) =3D=3D -1) - return errno; - data_fd =3D open(data_file, O_RDWR); =20 - if (event_reg(data_fd, "test u32 count", &index, &mask, &write) =3D=3D -1) + if (event_reg(data_fd, "test u32 count", &write, &enabled) =3D=3D -1) return errno; =20 /* Setup iovec */ @@ -80,13 +52,12 @@ int main(int argc, char **argv) io[0].iov_len =3D sizeof(write); io[1].iov_base =3D &count; io[1].iov_len =3D sizeof(count); - ask: printf("Press enter to check status...\n"); getchar(); =20 /* Check if anyone is listening */ - if (status_page[index] & mask) { + if (enabled) { /* Yep, trace out our data */ writev(data_fd, (const struct iovec *)io, 2); =20 --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD035C76195 for ; Fri, 24 Mar 2023 22:31:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232433AbjCXWbP (ORCPT ); Fri, 24 Mar 2023 18:31:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232120AbjCXWao (ORCPT ); Fri, 24 Mar 2023 18:30:44 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7EE2017163; Fri, 24 Mar 2023 15:30:41 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id E7D1B20FC4F1; Fri, 24 Mar 2023 15:30:40 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com E7D1B20FC4F1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697041; bh=qI96osokKp98pPQQ0gcTi7CZ/dblw6z+BDokZ8I1srE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ie/CIH24zdvlGFQn5eSAqHVAvlyaL7K0e3+XsRA5ry5eMPCJweq3J/G79Vm7963I0 JlWXyv7wrxEZ22fo4KB5UaplwJ5joYI70y2C7fR6buvwBzjaO6IMKJ66O3t2Z5icHB Qrq8D9C/yHPBAyW1A8bNEkN2zpReghQd0bU9Vusk= 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 09/11] tracing/user_events: Update documentation for ABI Date: Fri, 24 Mar 2023 15:30:26 -0700 Message-Id: <20230324223028.172-10-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The ABI for user_events has changed from mmap() based to remote writes. Update the documentation to reflect these changes, add new section for unregistering events since lifetime is now tied to tasks instead of files. Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- Documentation/trace/user_events.rst | 179 ++++++++++++++++------------ 1 file changed, 103 insertions(+), 76 deletions(-) diff --git a/Documentation/trace/user_events.rst b/Documentation/trace/user= _events.rst index 9f181f342a70..f79987e16cf4 100644 --- a/Documentation/trace/user_events.rst +++ b/Documentation/trace/user_events.rst @@ -11,20 +11,19 @@ that can be viewed via existing tools, such as ftrace a= nd perf. To enable this feature, build your kernel with CONFIG_USER_EVENTS=3Dy. =20 Programs can view status of the events via -/sys/kernel/debug/tracing/user_events_status and can both register and wri= te -data out via /sys/kernel/debug/tracing/user_events_data. +/sys/kernel/tracing/user_events_status and can both register and write +data out via /sys/kernel/tracing/user_events_data. =20 -Programs can also use /sys/kernel/debug/tracing/dynamic_events to register= and +Programs can also use /sys/kernel/tracing/dynamic_events to register and delete user based events via the u: prefix. The format of the command to dynamic_events is the same as the ioctl with the u: prefix applied. =20 Typically programs will register a set of events that they wish to expose = to tools that can read trace_events (such as ftrace and perf). The registrati= on -process gives back two ints to the program for each event. The first int is -the status bit. This describes which bit in little-endian format in the -/sys/kernel/debug/tracing/user_events_status file represents this event. T= he -second int is the write index which describes the data when a write() or -writev() is called on the /sys/kernel/debug/tracing/user_events_data file. +process tells the kernel which address and bit to reflect if any tool has +enabled the event and data should be written. The registration will give b= ack +a write index which describes the data when a write() or writev() is called +on the /sys/kernel/tracing/user_events_data file. =20 The structures referenced in this document are contained within the /include/uapi/linux/user_events.h file in the source tree. @@ -35,29 +34,70 @@ filesystem and may be mounted at different paths than a= bove.* Registering ----------- Registering within a user process is done via ioctl() out to the -/sys/kernel/debug/tracing/user_events_data file. The command to issue is +/sys/kernel/tracing/user_events_data file. The command to issue is DIAG_IOCSREG. =20 This command takes a packed struct user_reg as an argument:: =20 struct user_reg { - u32 size; - u64 name_args; - u32 status_bit; - u32 write_index; - }; + /* Input: Size of the user_reg structure being used */ + __u32 size; + + /* Input: Bit in enable address to use */ + __u8 enable_bit; + + /* Input: Enable size in bytes at address */ + __u8 enable_size; + + /* Input: Flags for future use, set to 0 */ + __u16 flags; + + /* Input: Address to update when enabled */ + __u64 enable_addr; + + /* Input: Pointer to string with event name, description and flags= */ + __u64 name_args; + + /* Output: Index of the event to use when writing data */ + __u32 write_index; + } __attribute__((__packed__)); + +The struct user_reg requires all the above inputs to be set appropriately. + ++ size: This must be set to sizeof(struct user_reg). =20 -The struct user_reg requires two inputs, the first is the size of the stru= cture -to ensure forward and backward compatibility. The second is the command st= ring -to issue for registering. Upon success two outputs are set, the status bit -and the write index. ++ enable_bit: The bit to reflect the event status at the address specified= by + enable_addr. + ++ enable_size: The size of the value specified by enable_addr. + This must be 4 (32-bit) or 8 (64-bit). 64-bit values are only allowed to= be + used on 64-bit kernels, however, 32-bit can be used on all kernels. + ++ flags: The flags to use, if any. For the initial version this must be 0. + Callers should first attempt to use flags and retry without flags to ens= ure + support for lower versions of the kernel. If a flag is not supported -EI= NVAL + is returned. + ++ enable_addr: The address of the value to use to reflect event status. Th= is + must be naturally aligned and write accessible within the user program. + ++ name_args: The name and arguments to describe the event, see command for= mat + for details. + +Upon successful registration the following is set. + ++ write_index: The index to use for this file descriptor that represents t= his + event when writing out data. The index is unique to this instance of the= file + descriptor that was used for the registration. See writing data for deta= ils. =20 User based events show up under tracefs like any other event under the subsystem named "user_events". This means tools that wish to attach to the -events need to use /sys/kernel/debug/tracing/events/user_events/[name]/ena= ble +events need to use /sys/kernel/tracing/events/user_events/[name]/enable or perf record -e user_events:[name] when attaching/recording. =20 -**NOTE:** *The write_index returned is only valid for the FD that was used* +**NOTE:** The event subsystem name by default is "user_events". Callers sh= ould +not assume it will always be "user_events". Operators reserve the right in= the +future to change the subsystem name per-process to accomodate event isolat= ion. =20 Command Format ^^^^^^^^^^^^^^ @@ -94,9 +134,9 @@ Would be represented by the following field:: struct mytype myname 20 =20 Deleting ------------ +-------- Deleting an event from within a user process is done via ioctl() out to the -/sys/kernel/debug/tracing/user_events_data file. The command to issue is +/sys/kernel/tracing/user_events_data file. The command to issue is DIAG_IOCSDEL. =20 This command only requires a single string specifying the event to delete = by @@ -104,92 +144,79 @@ its name. Delete will only succeed if there are no re= ferences left to the event (in both user and kernel space). User programs should use a separate= file to request deletes than the one used for registration due to this. =20 -Status ------- -When tools attach/record user based events the status of the event is upda= ted -in realtime. This allows user programs to only incur the cost of the write= () or -writev() calls when something is actively attached to the event. - -User programs call mmap() on /sys/kernel/debug/tracing/user_events_status = to -check the status for each event that is registered. The bit to check in the -file is given back after the register ioctl() via user_reg.status_bit. The= bit -is always in little-endian format. Programs can check if the bit is set ei= ther -using a byte-wise index with a mask or a long-wise index with a little-end= ian -mask. +Unregistering +------------- +If after registering an event it is no longer wanted to be updated then it= can +be disabled via ioctl() out to the /sys/kernel/tracing/user_events_data fi= le. +The command to issue is DIAG_IOCSUNREG. This is different than deleting, w= here +deleting actually removes the event from the system. Unregistering simply = tells +the kernel your process is no longer interested in updates to the event. =20 -Currently the size of user_events_status is a single page, however, custom -kernel configurations can change this size to allow more user based events= . In -all cases the size of the file is a multiple of a page size. +This command takes a packed struct user_unreg as an argument:: =20 -For example, if the register ioctl() gives back a status_bit of 3 you would -check byte 0 (3 / 8) of the returned mmap data and then AND the result wit= h 8 -(1 << (3 % 8)) to see if anything is attached to that event. + struct user_unreg { + /* Input: Size of the user_unreg structure being used */ + __u32 size; =20 -A byte-wise index check is performed as follows:: + /* Input: Bit to unregister */ + __u8 disable_bit; =20 - int index, mask; - char *status_page; + /* Input: Reserved, set to 0 */ + __u8 __reserved; =20 - index =3D status_bit / 8; - mask =3D 1 << (status_bit % 8); - - ... + /* Input: Reserved, set to 0 */ + __u16 __reserved2; =20 - if (status_page[index] & mask) { - /* Enabled */ - } + /* Input: Address to unregister */ + __u64 disable_addr; + } __attribute__((__packed__)); =20 -A long-wise index check is performed as follows:: +The struct user_unreg requires all the above inputs to be set appropriatel= y. =20 - #include - #include ++ size: This must be set to sizeof(struct user_unreg). =20 - #if __BITS_PER_LONG =3D=3D 64 - #define endian_swap(x) htole64(x) - #else - #define endian_swap(x) htole32(x) - #endif ++ disable_bit: This must be set to the bit to disable (same bit that was + previously registered via enable_bit). =20 - long index, mask, *status_page; ++ disable_addr: This must be set to the address to disable (same address t= hat was + previously registered via enable_addr). =20 - index =3D status_bit / __BITS_PER_LONG; - mask =3D 1L << (status_bit % __BITS_PER_LONG); - mask =3D endian_swap(mask); +**NOTE:** Events are automatically unregistered when execve() is invoked. = During +fork() the registered events will be retained and must be unregistered man= ually +in each process if wanted. =20 - ... +Status +------ +When tools attach/record user based events the status of the event is upda= ted +in realtime. This allows user programs to only incur the cost of the write= () or +writev() calls when something is actively attached to the event. =20 - if (status_page[index] & mask) { - /* Enabled */ - } +The kernel will update the specified bit that was registered for the event= as +tools attach/detach from the event. User programs simply check if the bit = is set +to see if something is attached or not. =20 Administrators can easily check the status of all registered events by rea= ding the user_events_status file directly via a terminal. The output is as foll= ows:: =20 - Byte:Name [# Comments] + Name [# Comments] ... =20 Active: ActiveCount Busy: BusyCount - Max: MaxCount =20 For example, on a system that has a single event the output looks like thi= s:: =20 - 1:test + test =20 Active: 1 Busy: 0 - Max: 32768 =20 If a user enables the user event via ftrace, the output would change to th= is:: =20 - 1:test # Used by ftrace + test # Used by ftrace =20 Active: 1 Busy: 1 - Max: 32768 - -**NOTE:** *A status bit of 0 will never be returned. This allows user prog= rams -to have a bit that can be used on error cases.* =20 Writing Data ------------ @@ -217,7 +244,7 @@ For example, if I have a struct like this:: int src; int dst; int flags; - }; + } __attribute__((__packed__)); =20 It's advised for user programs to do the following:: =20 --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7280EC76195 for ; Fri, 24 Mar 2023 22:31:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232420AbjCXWbK (ORCPT ); Fri, 24 Mar 2023 18:31:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232081AbjCXWan (ORCPT ); Fri, 24 Mar 2023 18:30:43 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E6A6E17CD2; Fri, 24 Mar 2023 15:30:41 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 5E8C520FC4F6; Fri, 24 Mar 2023 15:30:41 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5E8C520FC4F6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697041; bh=IeyP4hszPxXQh3EfDrorhwGAyqHtWNJ8d/cCBZiRrsA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sI8mD6XbffTrTe6OV9fXtMnS45eeEG7fqfdRdFdd2DhDmRcMeiKnuvsF8EsWN9Xm/ QrVESBXRc5aGxq8fimVJdnf+vapiCdTa/vFnANgrNRXFWxFrY2PMTJyZSakgWmcS9Q 6X5aYMJwRcGgw9nQlU98ZqSA7dUO3AAwTqx0Zbyg= 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 10/11] tracing/user_events: Charge event allocs to cgroups Date: Fri, 24 Mar 2023 15:30:27 -0700 Message-Id: <20230324223028.172-11-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Operators need a way to limit how much memory cgroups use. User events need to be included into that accounting. Fix this by using GFP_KERNEL_ACCOUNT for allocations generated by user programs for user_event tracing. Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- kernel/trace/trace_events_user.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_u= ser.c index e4ee25d16f3b..222f2eb59c7c 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -442,7 +442,7 @@ static bool user_event_enabler_dup(struct user_event_en= abler *orig, if (unlikely(test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(orig)))) return true; =20 - enabler =3D kzalloc(sizeof(*enabler), GFP_NOWAIT); + enabler =3D kzalloc(sizeof(*enabler), GFP_NOWAIT | __GFP_ACCOUNT); =20 if (!enabler) return false; @@ -502,7 +502,7 @@ static struct user_event_mm *user_event_mm_create(struc= t task_struct *t) struct user_event_mm *user_mm; unsigned long flags; =20 - user_mm =3D kzalloc(sizeof(*user_mm), GFP_KERNEL); + user_mm =3D kzalloc(sizeof(*user_mm), GFP_KERNEL_ACCOUNT); =20 if (!user_mm) return NULL; @@ -662,7 +662,7 @@ static struct user_event_enabler if (!user_mm) return NULL; =20 - enabler =3D kzalloc(sizeof(*enabler), GFP_KERNEL); + enabler =3D kzalloc(sizeof(*enabler), GFP_KERNEL_ACCOUNT); =20 if (!enabler) goto out; @@ -870,7 +870,7 @@ static int user_event_add_field(struct user_event *user= , const char *type, struct ftrace_event_field *field; int validator_flags =3D 0; =20 - field =3D kmalloc(sizeof(*field), GFP_KERNEL); + field =3D kmalloc(sizeof(*field), GFP_KERNEL_ACCOUNT); =20 if (!field) return -ENOMEM; @@ -889,7 +889,7 @@ static int user_event_add_field(struct user_event *user= , const char *type, if (strstr(type, "char") !=3D NULL) validator_flags |=3D VALIDATOR_ENSURE_NULL; =20 - validator =3D kmalloc(sizeof(*validator), GFP_KERNEL); + validator =3D kmalloc(sizeof(*validator), GFP_KERNEL_ACCOUNT); =20 if (!validator) { kfree(field); @@ -1175,7 +1175,7 @@ static int user_event_create_print_fmt(struct user_ev= ent *user) =20 len =3D user_event_set_print_fmt(user, NULL, 0); =20 - print_fmt =3D kmalloc(len, GFP_KERNEL); + print_fmt =3D kmalloc(len, GFP_KERNEL_ACCOUNT); =20 if (!print_fmt) return -ENOMEM; @@ -1508,7 +1508,7 @@ static int user_event_create(const char *raw_command) raw_command +=3D USER_EVENTS_PREFIX_LEN; raw_command =3D skip_spaces(raw_command); =20 - name =3D kstrdup(raw_command, GFP_KERNEL); + name =3D kstrdup(raw_command, GFP_KERNEL_ACCOUNT); =20 if (!name) return -ENOMEM; @@ -1704,7 +1704,7 @@ static int user_event_parse(struct user_event_group *= group, char *name, return 0; } =20 - user =3D kzalloc(sizeof(*user), GFP_KERNEL); + user =3D kzalloc(sizeof(*user), GFP_KERNEL_ACCOUNT); =20 if (!user) return -ENOMEM; @@ -1874,7 +1874,7 @@ static int user_events_open(struct inode *node, struc= t file *file) if (!group) return -ENOENT; =20 - info =3D kzalloc(sizeof(*info), GFP_KERNEL); + info =3D kzalloc(sizeof(*info), GFP_KERNEL_ACCOUNT); =20 if (!info) return -ENOMEM; @@ -1927,7 +1927,7 @@ static int user_events_ref_add(struct user_event_file= _info *info, =20 size =3D struct_size(refs, events, count + 1); =20 - new_refs =3D kzalloc(size, GFP_KERNEL); + new_refs =3D kzalloc(size, GFP_KERNEL_ACCOUNT); =20 if (!new_refs) return -ENOMEM; --=20 2.25.1 From nobody Thu May 2 20:46:23 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B40DDC761AF for ; Fri, 24 Mar 2023 22:31:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232454AbjCXWbS (ORCPT ); Fri, 24 Mar 2023 18:31:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232123AbjCXWao (ORCPT ); Fri, 24 Mar 2023 18:30:44 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8B05117CF0; Fri, 24 Mar 2023 15:30:42 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id C98CD20FC4FA; Fri, 24 Mar 2023 15:30:41 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com C98CD20FC4FA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697042; bh=9hn7lg9dudknRxZST2EvcXuTZnhH2Dpn1zkQtZUpKR0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FiFa0lj4u0e+Ncsfd5AUeJfL964Tk6p0G1c6Bp/6p7o7ZptOxS7NqaVNgYvxsX814 uyUEsOSMgRPHo8IBeiPvPTHbXGzKwnhAAOiKTnIoGz9esjUu721Zcbbgt6s5YAdK/B +8yreuxzE1R2hXNL6IR3qji9QLfg6iJG+rQv16ik= 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 11/11] tracing/user_events: Limit global user_event count Date: Fri, 24 Mar 2023 15:30:28 -0700 Message-Id: <20230324223028.172-12-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Operators want to be able to ensure enough tracepoints exist on the system for kernel components as well as for user components. Since there are only up to 64K events, by default allow up to half to be used by user events. Add a kernel sysctl parameter (kernel.user_events_max) to set a global limit that is honored among all groups on the system. This ensures hard limits can be setup to prevent user processes from consuming all event IDs on the system. Signed-off-by: Beau Belgrave Acked-by: Masami Hiramatsu (Google) --- kernel/trace/trace_events_user.c | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_u= ser.c index 222f2eb59c7c..11922f7cf496 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "trace.h" #include "trace_dynevent.h" @@ -61,6 +62,12 @@ struct user_event_group { /* Group for init_user_ns mapping, top-most group */ static struct user_event_group *init_group; =20 +/* Max allowed events for the whole system */ +static unsigned int max_user_events =3D 32768; + +/* Current number of events on the whole system */ +static unsigned int current_user_events; + /* * Stores per-event properties, as users register events * within a file a user_event might be created if it does not @@ -1241,6 +1248,8 @@ static int destroy_user_event(struct user_event *user) { int ret =3D 0; =20 + lockdep_assert_held(&event_mutex); + /* Must destroy fields before call removal */ user_event_destroy_fields(user); =20 @@ -1257,6 +1266,11 @@ static int destroy_user_event(struct user_event *use= r) kfree(EVENT_NAME(user)); kfree(user); =20 + if (current_user_events > 0) + current_user_events--; + else + pr_alert("BUG: Bad current_user_events\n"); + return ret; } =20 @@ -1744,6 +1758,11 @@ static int user_event_parse(struct user_event_group = *group, char *name, =20 mutex_lock(&event_mutex); =20 + if (current_user_events >=3D max_user_events) { + ret =3D -EMFILE; + goto put_user_lock; + } + ret =3D user_event_trace_register(user); =20 if (ret) @@ -1755,6 +1774,7 @@ static int user_event_parse(struct user_event_group *= group, char *name, dyn_event_init(&user->devent, &user_event_dops); dyn_event_add(&user->devent, &user->call); hash_add(group->register_table, &user->node, key); + current_user_events++; =20 mutex_unlock(&event_mutex); =20 @@ -2386,6 +2406,31 @@ static int create_user_tracefs(void) return -ENODEV; } =20 +static int set_max_user_events_sysctl(struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos) +{ + int ret; + + mutex_lock(&event_mutex); + + ret =3D proc_douintvec(table, write, buffer, lenp, ppos); + + mutex_unlock(&event_mutex); + + return ret; +} + +static struct ctl_table user_event_sysctls[] =3D { + { + .procname =3D "user_events_max", + .data =3D &max_user_events, + .maxlen =3D sizeof(unsigned int), + .mode =3D 0644, + .proc_handler =3D set_max_user_events_sysctl, + }, + {} +}; + static int __init trace_events_user_init(void) { int ret; @@ -2415,6 +2460,8 @@ static int __init trace_events_user_init(void) if (dyn_event_register(&user_event_dops)) pr_warn("user_events could not register with dyn_events\n"); =20 + register_sysctl_init("kernel", user_event_sysctls); + return 0; } =20 --=20 2.25.1