From patchwork Tue Mar 28 23:52:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191669 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9EF26C77B6C for ; Tue, 28 Mar 2023 23:52:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F2A96B0078; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 94FAB6B007E; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 814466B0078; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5E8486B0075 for ; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 169391204CE for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) X-FDA: 80619958776.22.1721D69 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf23.hostedemail.com (Postfix) with ESMTP id 5AA80140016 for ; Tue, 28 Mar 2023 23:52:26 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=G6twUb0k; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf23.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047546; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OSmDP26J8YzoMhJc3UKK7slznwSGVE3KNwggNF1IdJc=; b=KE7KH8o+Df/O0kWHQysVmqrs9LUpw6p+ySpm6VrZx0M7MliHobZm7LO8YZuUT2qGhg/GL2 aO9dbRrZQPycOiYI++H96l/WBzjIK1fncc4aFC+qi6G78hUTF3O7w9QVnE7F4qKjSx7TLU ml1Tx2j7lxKsRpN8ppM84DqjEJ4vaT0= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=G6twUb0k; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf23.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047546; a=rsa-sha256; cv=none; b=zsaGpZZ9ndmFI1oYUCp5EfQY3Sz/eoYGvOfY3QOdkMPIh86q6gb5SwbaE5HxLgwhk1jrg5 csCxqgOK2c2guR6tVsJW9y9/XPDuHpF2mpDt+2+wFWAle70h/mgPuWXTE5WcKRGgZj6OTb 5sQp+yvvHqaRvPDpysaVa/THjnM05vk= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id BFB5920FDAA3; Tue, 28 Mar 2023 16:52:24 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com BFB5920FDAA3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047545; bh=OSmDP26J8YzoMhJc3UKK7slznwSGVE3KNwggNF1IdJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G6twUb0ke7eK5ynz9/PeHHdyqh6KQ95zr0iOHhkyuxOiVKml3Xc5Fptvp8sR+DX9z HbY+oR9AqNC+ixwArDWbmpgy9CbMHTMw45k8BgakrhTAN7tbRtHxDCy1121cQ7VOB9 6piQBcmQptm2Bl1DBPVjQQwn+jIkf6LcGJAezy2M= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 01/12] tracing/user_events: Split header into uapi and kernel Date: Tue, 28 Mar 2023 16:52:08 -0700 Message-Id: <20230328235219.203-2-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5AA80140016 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 4whgckocmgq3cuhatpbk3r9o96w1mh95 X-HE-Tag: 1680047546-870898 X-HE-Meta: U2FsdGVkX199m69HS45DyiEVZfpmHpCqs7opGUIau6EgYwY4Ye0E2Dsy29a7Pn8pC+jLw1mL9Y4IrTDtQn6bb0l/4QmMSMk0M/SbMSxWnTjTtXe5a6FfDSp6xmG8yphvHguJBNfwj2nIlYVS05G2ImB/iTa3V5RBkBMtiPCGgx6T8QjeaAHX/K/jeYA+lhGd8W1uAeiDnBk5qjhWW97WHUQwR1aT5Q15I5PQ+zK1arCIYfOnd6+aFybx2dZjcF6DaslNwFP80Z2O2aIvN39OH3f3iuVfoVFZiYu7fm/OQCfDCVtysFob/crdZto38dV36zc0HnQTe+i/40NsN993rR3DroatxHhTX8qG8DM252uU9LenMu/DXGHhtbpM6ovHb9qCNIwtmkBF7+s53qEuoDhZmYpES9JYkwnCKV9qTKEUNcR0ntIDkjdl3b5QthP+IE7/dpphTE+F5jzra4WeeFGDT102tXnCxCBeTTyoUpoWvHCPX+B+ofHszH1iB8GNlpyo7TMnghaxqXjhA7e7PBmbVFetmo3F7SZ1cmMNjqmACuJ6u+9TIcicJxuRk7799LCoqd6rba7sPkHTx+7N8QTxUV0DKtB78rFoVGtKJVEshcgzyOi1AgxEghKdWu5wKhLLLOh+rxPMJySUcOvJMub9j8y9Zlyeq/yT5pzJ6HSJEnxBposgrlpfB/xNN+s2CgdjykSsIDZr+xNacRS0vMRLkSyWVMSrdwiQfLpTxoA+36AGJ1cxfXZieXDuPLXxZPEdhx3E+vbmVI/JfkWcXUFSzFm1urV/tx3sqvM5SKlYJ9cIuDgocfDNFhO5D48/u0ffO2dmUP1eeXFmfnGSXflcw/oG3TRQ59rnLQkiYnu1j9OcMk+N+Ra7RlaEGIvOGPaHztsKbFi+601zDJek5u786D3TSEXp58CGdbje7opMUIbbhamkK+GF2cWoj5OFBVD2op1x7i45l3QErYy TN1YVN6Z ezPFDJwotWgE1Nesq9gOMRrrpEQI9mbmZSiQZI+Up2gwyZrYBEdT4vkf/bQQVD0Krzyy+UNxeRnwHPnIsYmbadaKYSeAfmaM9+Pk2013+/9WJd384q8Qn/O8eDFt1ZxdY0JwyogCFhHhtF+d7TyTBTv8gYXIjzIDkx7+VnSE2JJx0Cp2DYp363Cu7GauZroEn3dbLmIm86L1wkouKgCjgFsbwZn0/eKvA4ZBwDqrRu0ygscIx1J8l0OKo39iZG2T+URG/gYGkpBv+HGJqwvV6qWBrLSfazf7rMjQn9r0p2u8mziI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 -#include -#include +#ifndef _LINUX_USER_EVENTS_H +#define _LINUX_USER_EVENTS_H -#ifdef __KERNEL__ -#include -#else -#include -#endif +#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 registration. - * 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_events.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 registration. + * 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_user.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" From patchwork Tue Mar 28 23:52:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B78ECC761A6 for ; Tue, 28 Mar 2023 23:52:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F04E6B0075; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A0536B0080; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 64F606B0078; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 518DF6B0078 for ; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1199340B1A for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) X-FDA: 80619958776.21.C27D491 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf25.hostedemail.com (Postfix) with ESMTP id 50C52A0002 for ; Tue, 28 Mar 2023 23:52:26 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=nOYZCYBZ; spf=pass (imf25.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047546; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=S8OIOLUkRs5CBqJ2LYBXQxLSyKFNFEQS/E/TUK8EqMU=; b=l4gg236ba8BuZRYVLcQ/Oh/dVhqRKxdbzzez75tFdtlMBqAJE8SVvWeZ77j8J8/4yT/2Yd gJ8RQPs2lGAWVGWTyJ/3o3Ua3/cl6RdfPN2NAQPok4ZZkdHlZ1cxP7T+/0zLjOoloEBlrL hn38xE9S6b3tE5mGGICSXpEuQwOhC70= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=nOYZCYBZ; spf=pass (imf25.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047546; a=rsa-sha256; cv=none; b=g3HU9M85/BWn5cEdCMO25kLGNKMe4WmbUB4GxEo9qXPOifk9GO274ZfCJg8qlSOA66/V5t EIdckjZZEmxntoohxvHQrcJ+sc4nGTx+dDMgXb1iuguLniQfk5M/9XHvDLvCOf/couvQyI nH6+tDOM+awlZGEVHH8LfqbItu4w5HI= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 284C220FDAA8; Tue, 28 Mar 2023 16:52:25 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 284C220FDAA8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047545; bh=S8OIOLUkRs5CBqJ2LYBXQxLSyKFNFEQS/E/TUK8EqMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nOYZCYBZnNL8mjXUQ7/DviplLsTB0V+Wfee3duOsmj42pfZJSfpTL3C/CWOFesECC OcarkvXVhOyaclMYZKL9aC8LkIsIGIKVZoMc6dS64oJ89B6sPg4/svycrBYyGUYQlC jm3KXGBFtgxgoaG+JAGtk3PRRUQcfQhKzIHCxecU= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 02/12] tracing/user_events: Track fork/exec/exit for mm lifetime Date: Tue, 28 Mar 2023 16:52:09 -0700 Message-Id: <20230328235219.203-3-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: 7aqot7oqqcj74itqtdpngshf8t8ow5et X-Rspam-User: X-Rspamd-Queue-Id: 50C52A0002 X-Rspamd-Server: rspam06 X-HE-Tag: 1680047546-705644 X-HE-Meta: U2FsdGVkX19wChKEyjxGFgE5g/16sADfXtwyiYaj8RuZIMgzsbf7N4cb4X8azc+cy8BJmRA5IkK+3wjPzCgyWnAPOlXxqWHMqmcmlb/yoeGQX+HGmqIJAtdKSSXeZmTyg19zKo35KxYHnOUsl9j8gnpNzVs4qGpzW2HYO1OKzJF8C96DWIlDAhwAiL2uI5+MnSNRSEP2mmm/I8UQGvU/OeP7x64chN6O9eIvCRFWLMcUsFWI012sUGvHDjIsCFgYYXmZkJ5WzJfxaYH5iePkheu6HklAIpcKOFGk9Skxkv3WUkxjTvd8CfvztQAaMGnn7dkQ3+n1hGSG+iwXFbyfxGR9oG9N2b4/VWCv8vjFaFSa+OCsRSzF4/mB6KeaZDr26w8xfFFZ4qlqDw9+C+mI20/twXqTTjRzbx/D1oC5nA0RfamLkl1zSW9oygRqbFgNd+UbHNfLVTps5O6bm8JJyE3/Pe00sFCfEtb3pjHGXhH+YybO/kfbHONr3Y/xS9ry1WFa5OuhrZZe3eYIXO+PWsmvzVHlI+9YKwZHDLenk5mWuG+ZG+K9Dd7NROPFeutaAZnl0R0ZqbTuH/Cg24GopQ/+d75yv8Vs+Tv8+J8nCiaUpaC+YBwZH2Wmrgf87ej/nLDgpWtkQITpZE4/8IM4vq0EEY9BUp/jF1wHvoIK3hBvbSErLT6633kGUxDR0Z4gUuhxzEGS3GXQeorPXGbZSKnyf5rh9r6jDGLim2x+VnGsUlsP7ARMrgm/lON+s+aUxeXZDJhk+oD7rUw9bXDDAio30LG5r9J3JkDk7vAMo7VVxdhN9kACqSC5mzztZlGkzlVR5MI64yUa7OwhdRuFiymLG5eAR7tbivzOW4Q3fDV5WlxJoGiuN9c6q/WhyW0GA/FvS55pVMz4dU7gHc/FC7GBWun9b8CU123Intwlo5zy/t0dHWYPsl6Zde0/KvlXn9pMXvbSMlSzlWn9dV4 vk1YYM/u 1niVcXMqDfPVyzs+Ks7TL6jrx7kGSX61ZkdSxZxaJXkPFo95X8faf0PLdYbJW1BLT6mBEm94rIe5KPT3dlzMRvy25NxCPEOsyAYNnlv//zWAHvFBECEwVdrk1eREN/Zckdv1L6RILlmnsglaoR4nAfKEpOvs1uaVZlAYzVHUbWAeiosL4P7O3UKYjUDMIMI1Y0PDx7kyGSPCLTpetdFfub+TB5WWHiHbEx2qM1hmJxYrhO/RhqAdFie59Z0K3bmvF3dsTwh2MLDg8HUpaHksyJitS9ANfNRkqOnp0uoPzMZE/uCxOem6gJnCvqM0iv4Mi4BoC/mY3YgL1fPFlJPbaNfkZpp4JzOUKWwjFFeLr64c/Qd4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000004, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 #include #include @@ -1856,6 +1857,7 @@ static int bprm_execve(struct linux_binprm *bprm, current->fs->in_exec = 0; current->in_execve = 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; /* * 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 +#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 @@ #include +#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 #include #include @@ -816,6 +817,7 @@ void __noreturn do_exit(long code) coredump_task_exit(tsk); ptrace_event(PTRACE_EVENT_EXIT, code); + user_events_exit(tsk); validate_creds_for_do_exit(tsk); 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 #include #include @@ -2502,6 +2503,7 @@ static __latent_entropy struct task_struct *copy_process( trace_task_newtask(p, clone_flags); uprobe_copy_process(p, clone_flags); + user_events_fork(p, clone_flags); copy_oom_score_adj(clone_flags, p); From patchwork Tue Mar 28 23:52:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191671 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33CF1C77B70 for ; Tue, 28 Mar 2023 23:52:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EDE56B007D; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 075CE900002; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E57876B0080; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D33446B007D for ; Tue, 28 Mar 2023 19:52:28 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A32AC1A047B for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) X-FDA: 80619958776.19.A414126 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf22.hostedemail.com (Postfix) with ESMTP id DFDE1C000F for ; Tue, 28 Mar 2023 23:52:26 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=sVdP9l1b; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf22.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047547; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=R9INv6oij2jvfqxRYN7sTvDkp32JqHmiAHAQNiuLI7o=; b=oTG1FDUcg0MvHPaMwaFRPcObdoWytNk2JG+7YPl6Vu0f8G9KqzMKKGoN5oBpthBMGWtcKe vRjuKOP8aMJdZzNv0jRLs/1+WJU/CwaQRRM/xgeleKodCd5caMY53AE2VkzpETvi9Aywo3 qk/kgNi3kUAPRJdS0yojch9YOfLYz6g= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=sVdP9l1b; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf22.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047547; a=rsa-sha256; cv=none; b=huk6YkO6WKd385vt7a6bKN/gS55sdRiDWrTCDN4/SXcgwKCKrZGfYkwDByPzG5agXQSTh9 6eoyF3aSFmf/RdUlwwwVtbiMSZon7CLWCyUL+LDcDClrKTmZF/v0QRcWyi8iw+5K1cWF0z +Pi6Eq55Tv4Ak/iqz52LJLq3pBnOddA= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 88F6E20FDAAD; Tue, 28 Mar 2023 16:52:25 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 88F6E20FDAAD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047545; bh=R9INv6oij2jvfqxRYN7sTvDkp32JqHmiAHAQNiuLI7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sVdP9l1biReoiPhgTNLfb2M8N0EPi4eKoILkeMvx1st/ATCkMW2Tel3jidMugNYOw 2MliuwIYexwc+uv0piwdKaKVMaNRJW8v56QiNQYHoXKhqo4wRiykXf78J+b9Y+k77/ rZcPrkp66VFEqpJ4JgQULrfbcQZzjrmzBngpRfWw= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 03/12] tracing/user_events: Use remote writes for event enablement Date: Tue, 28 Mar 2023 16:52:10 -0700 Message-Id: <20230328235219.203-4-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: DFDE1C000F X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: h4q7yf1hcio4q8qoobn3xmquojrcudrb X-HE-Tag: 1680047546-162357 X-HE-Meta: U2FsdGVkX18nynBe96v+kyuq0fhTl7uJpujEi0wD1VAcNX6aM/vx8d1pp61s5lkht3uFDZG8iafRyC1D30Is+jxeLeoTdebN7bR9kiWFjMgAradE0RQhnkd00vArMx/O7Q8Tq8BJEVR9a8zY7h4zehZ1wZCiLfKc+ODI0NUfN60lt+Vs+azgEFnRoR/FPnS9oGfrSuBUI7qvwiv6zdBe0uYC2kV3nl1b3UIoRXgZV45Up0Qq40XqOCl5rNOZ+xUNX6SWpEDmHgxbtp/zZ9kTvEFxJyt1lUyPa6UcwE6aJ3laTUqRTjAG/OLje56C8soOzRS9UNCPBL75DOVCWdoLDIMyNz2bmB/hD8SuMpXx82WnZorSUbb3zeUgtdrTgnalWH7qj09+SeNlvtee15ElYSG45N/jo3BJl9rQoAsu1QlrRfnaAWYVADKUijuIemqQSmZCIbDDf1ONcYObLiP0W/6m1uvIqKYZ6vhX5xjXXK2PzNzChUgnc80ksWPtar/8Tj/S0Vs/MNLnRdkgRvZ7AiALcsnX2cIo01TCO1GhTIrxO98hn4knEC6ubXo+YzkOZfmgeSpe7PklmTbPH0cOAwF/z8e4VPUimWyn9bh3XUe+fDVY7huV34g8CvpqMOWeCAJM1Rr9+eit5znW7BxITceCLiMkOrU0EypxKDX9aJ5avVvf4obtNMHdgHn3pOmXIu/W6hIhcE8E9Ul9+fLjAktN5d8X3W8k7M09iGqdsnDbYxjb6JHfpUhaKMDC8wtirkE5S/RmV7JbGkjsVmdX/IqRCMpxhxQ4PEA36nxHQGuusA5+dsC/4lJ2Zd8FdEtQ4fgtvLsjlKO5CPXP/ULnDcJJTsAyxHftGtqtuqK6T7sxyevzq3AJxyifZRrjvXlzbEzRJOywakXyMUQktppTByTMKZjAGfKp/ccUEbVdi47ccWoWNvAwAI4WQHXXX2C8EWIcHaq8vyLWVUWXvYi pI8ykmUN gVrZwS6K//2twvqmm+tizuvB1VrhLR3899dHs+b8SVyBlQGkyMSmAQlGCJ71ud/eW3biIpqqBmo4GhRO97gA2JZeySy0MnoyHH8kUF0gr1weBQZsEn+Va/SCHrneVOJSFDdEbo6T5J5q5LduUNZ/r/f9qIH82P29d5rgyHz9zfgjgTQCbCE/Rsy5cbECe0uWvDrp5z+p0zyE3mUq0R+5yICVJaZqfpFB0KWDrVbIEMbqJslLItvFPSdz2WFupu3JEVvJNobCFHcD0QECYC4SWRjNaszpO9st5+TGHyCb6kKqmgVKJO2BgAXoU3wd7xttZQK40YfcV1spJ8q2nUVtYtG84s9YlpX7TLRO5idiLQa2VQSI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 +#include +#include +#include +#include #include #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 +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 = current->user_event_mm; + + if (clone_flags & CLONE_VM) { + t->user_event_mm = 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 */ #endif /* _LINUX_USER_EVENTS_H */ diff --git a/include/uapi/linux/user_events.h b/include/uapi/linux/user_events.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; + /* 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: 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. + See Documentation/trace/user_events.rst. If in doubt, say N. config HIST_TRIGGERS diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.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 -/* - * 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 -/* - * 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. These @@ -70,20 +48,14 @@ #define EVENT_STATUS_OTHER BIT(7) /* - * 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); }; /* 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; }; +/* + * 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); +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); } -static void set_page_reservations(char *pages, bool set) -{ - int page; - - for (page = 0; page < MAX_PAGES; ++page) { - void *addr = 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; - group->pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, MAX_PAGE_ORDER); - - if (!group->pages) - goto error; - - group->register_page_data = 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); @@ -266,20 +231,367 @@ static struct user_event_group return NULL; }; -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) == 0) { + ret = -ENOENT; + goto out; + } + + ret = fixup_user_fault(mm->mm, uaddr, FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE, + &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 = 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) == 0) + return -ENOENT; + + ret = pin_user_pages_remote(mm->mm, uaddr, 1, FOLL_WRITE | FOLL_NOFAULT, + &page, NULL, NULL); + + if (ret <= 0) { + pr_warn("user_events: Enable write failed\n"); + return -EFAULT; + } + + kaddr = kmap_local_page(page); + ptr = 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 = user_event_mm_get_all(user); + struct user_event_mm *next; + + while (mm) { + next = mm->next; + mmap_read_lock(mm->mm); + rcu_read_lock(); + + list_for_each_entry_rcu(enabler, &mm->enablers, link) + if (enabler->event == user) + user_event_enabler_write(mm, enabler); + + rcu_read_unlock(); + mmap_read_unlock(mm->mm); + user_event_mm_put(mm); + mm = next; + } +} + +static bool user_event_enabler_dup(struct user_event_enabler *orig, + struct user_event_mm *mm) +{ + struct user_event_enabler *enabler; + + enabler = kzalloc(sizeof(*enabler), GFP_NOWAIT); + + if (!enabler) + return false; + + enabler->event = orig->event; + enabler->addr = orig->addr; + + /* Only dup part of value (ignore future flags, etc) */ + enabler->values = 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 = 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 == user) { + mm->next = found; + found = 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 = kzalloc(sizeof(*user_mm), GFP_KERNEL); + + if (!user_mm) + return NULL; + + user_mm->mm = 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 = 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 = current->user_event_mm; + + if (user_mm) + goto inc; + + user_mm = 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 = 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 = user->index; + struct user_event_mm *mm; + unsigned long flags; + + might_sleep(); + + mm = t->user_event_mm; + t->user_event_mm = 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); - user->group->register_page_data[MAP_STATUS_BYTE(i)] |= 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); } -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 = user->index; + struct user_event_mm *mm = user_event_mm_create(t); + struct user_event_enabler *enabler; - user->group->register_page_data[MAP_STATUS_BYTE(i)] &= ~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 = (unsigned long)reg->enable_addr; + + user_mm = current_user_event_mm(); + + if (!user_mm) + return NULL; + + enabler = kzalloc(sizeof(*enabler), GFP_KERNEL); + + if (!enabler) + goto out; + + enabler->event = user; + enabler->addr = uaddr; + enabler->values = 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 = 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 = NULL; + } +out: + user_event_mm_put(user_mm); + + return enabler; } static __always_inline __must_check @@ -824,9 +1136,6 @@ static int destroy_user_event(struct user_event *user) return ret; dyn_event_remove(&user->devent); - - user_event_register_clear(user); - clear_bit(user->index, user->group->page_bitmap); hash_del(&user->node); user_event_destroy_validators(user); @@ -972,9 +1281,9 @@ static void user_event_perf(struct user_event *user, struct iov_iter *i, #endif /* - * 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 = &user->tracepoint; char status = 0; @@ -1005,12 +1314,9 @@ static void update_reg_page_for(struct user_event *user) rcu_read_unlock_sched(); } - if (status) - user_event_register_set(user); - else - user_event_register_clear(user); - user->status = status; + + user_event_enabler_update(user); } /* @@ -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; @@ -1285,11 +1590,6 @@ static int user_event_parse(struct user_event_group *group, char *name, return 0; } - index = find_first_zero_bit(group->page_bitmap, MAX_EVENTS); - - if (index == MAX_EVENTS) - return -EMFILE; - user = kzalloc(sizeof(*user), GFP_KERNEL); if (!user) @@ -1335,14 +1635,11 @@ static int user_event_parse(struct user_event_group *group, char *name, if (ret) goto put_user_lock; - user->index = index; - /* Ensure we track self ref and caller ref (2) */ refcount_set(&user->refcnt, 2); 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); mutex_unlock(&event_mutex); @@ -1559,6 +1856,37 @@ static long user_reg_get(struct user_reg __user *ureg, struct user_reg *kreg) if (ret) return ret; + /* Ensure no flags, since we don't support any yet */ + if (kreg->flags != 0) + return -EINVAL; + + /* Ensure supported size */ + switch (kreg->enable_size) { + case 4: + /* 32-bit */ + break; +#if BITS_PER_LONG >= 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 = size; return 0; @@ -1573,8 +1901,10 @@ static long user_events_ioctl_reg(struct user_event_file_info *info, struct user_reg __user *ureg = (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; ret = user_reg_get(ureg, ®); @@ -1605,8 +1935,28 @@ static long user_events_ioctl_reg(struct user_event_file_info *info, if (ret < 0) return ret; + /* + * 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 = 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); return 0; } @@ -1720,38 +2070,6 @@ static const struct file_operations user_data_fops = { .release = user_events_release, }; -static struct user_event_group *user_status_group(struct file *file) -{ - struct seq_file *m = file->private_data; - - if (!m) - return NULL; - - return m->private; -} - -/* - * Maps the shared page into the user process for checking if event is enabled. - */ -static int user_status_mmap(struct file *file, struct vm_area_struct *vma) -{ - char *pages; - struct user_event_group *group = user_status_group(file); - unsigned long size = vma->vm_end - vma->vm_start; - - if (size != MAX_BYTES) - return -EINVAL; - - if (!group) - return -EINVAL; - - pages = 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 = m->private; struct user_event *user; char status; - int i, active = 0, busy = 0, flags; + int i, active = 0, busy = 0; if (!group) return -EINVAL; @@ -1784,11 +2102,10 @@ static int user_seq_show(struct seq_file *m, void *p) hash_for_each(group->register_table, i, user, node) { status = user->status; - flags = user->flags; - seq_printf(m, "%d:%s", user->index, EVENT_NAME(user)); + seq_printf(m, "%s", EVENT_NAME(user)); - if (flags != 0 || status != 0) + if (status != 0) seq_puts(m, " #"); if (status != 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); return 0; } @@ -1847,7 +2163,6 @@ static int user_status_open(struct inode *node, struct file *file) static const struct file_operations user_status_fops = { .open = user_status_open, - .mmap = user_status_mmap, .read = seq_read, .llseek = seq_lseek, .release = seq_release, @@ -1868,8 +2183,7 @@ static int create_user_tracefs(void) goto err; } - /* mmap with MAP_SHARED requires writable fd */ - emmap = tracefs_create_file("user_events_status", TRACE_MODE_WRITE, + emmap = tracefs_create_file("user_events_status", TRACE_MODE_READ, NULL, NULL, &user_status_fops); if (!emmap) { From patchwork Tue Mar 28 23:52:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191670 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E0AEC761A6 for ; Tue, 28 Mar 2023 23:52:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 693226B007E; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 641696B0080; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BB3E6B0081; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2A2496B007E for ; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F41651C5E6F for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) X-FDA: 80619958776.17.258C0E8 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf01.hostedemail.com (Postfix) with ESMTP id 38C6640003 for ; Tue, 28 Mar 2023 23:52:27 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=goI6L3YH; spf=pass (imf01.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047547; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LAT6es1zlcxHgyXQ+ccAd1DGKcAvO/9IswEU0C+E8Yk=; b=spol5Qx+Z5Xyl0Y29xgZsGzJ2+fB1/FLm5KBcBePFeo0Fo9A9Ntdn/+TPGqvh6wJfXwA57 QARHmi6z6Z4R2LCN1F6PqFv50ZRk/2OQJ/6ixy6lPl9lH5gn7Lr55APZ83gELBIxjDILxO wlpNg4TlbPrqyckQ7r4oFulyvNtT9po= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=goI6L3YH; spf=pass (imf01.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047547; a=rsa-sha256; cv=none; b=fTsLoGPPmI7MF6vFS8hJLEhm3Qh8pBvXlbzP8lrYRVUg4xIyOP2ZV4omIThqRvh3+DPR9J +pHL0XAxNrNeGfjBPH5+3qwd5M4hOos5xr+wOoNt4HrpMZJAvhrHylrfiLb3U48mkCYuSs SULYOEtz2Vlx7MJgB4QllGVl1aX12UQ= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id F070820FDAB4; Tue, 28 Mar 2023 16:52:25 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com F070820FDAB4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047546; bh=LAT6es1zlcxHgyXQ+ccAd1DGKcAvO/9IswEU0C+E8Yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=goI6L3YHE0QXrAG4uRyN8KhwBCKsJbfeylHtLhxjZnzS5bQH+mMKgRk33MDMC5gMr obbzbehtVxM9ZGkBBqlJMRjbHERiakZS8yAS/bcCB+EBzR6/acZlKl4B1Klu78Zhpa HrKj/wcAH2+BBxvCLe2M5RVKsjYX36edJGxdUfDk= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 04/12] tracing/user_events: Fixup enable faults asyncly Date: Tue, 28 Mar 2023 16:52:11 -0700 Message-Id: <20230328235219.203-5-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: 4zb6urg6doccgskrfujrmqwz1biu7ed5 X-Rspam-User: X-Rspamd-Queue-Id: 38C6640003 X-Rspamd-Server: rspam06 X-HE-Tag: 1680047547-988005 X-HE-Meta: U2FsdGVkX1/rxtfKDl7eRNp/klajmiapiKupxhQ0jc/8CXQb2OC4m2HbZSzZZUOMhfxiG5ZbBB7W8DnwcVIPkKSWsj+aINFX/rr0pUZOenwdWrBVaRM1iHp7lEQ1mWaSb85eU8/3Z8qc9dATQrvNRya3MQ9aOSaUCItFwA6JhvAMxJaq7/9C7p/770xGkIC3RlK80/TL57zryhOe/UEBAxo/P74RizjlTIXUjIQ++lQeEChFaX1PmxdEku4GPBuNt05rQAlwgm0h9UENz+C7wRlIdaRYC0Q2RxnTjIh6SaxhrvpdJTumJJp2ce//s+efwU/UCN7rJOILr8Sn5CH+L45bnt3U+adECKfPGwZAWvLddM600ERrxFpafIcQrHMLX/Ra6e7dsZHg5ajsCjOQOFp2mGO2sSDfqcp+m/DCpsHMN15Xr0dsi0aayryDVPBVAg42awU4mCAuqCN6DOW812r8Vpyc4peUQetGelpMKp8ZgSCM92O+HU+7jLWIlLpLbDG6REMeDPQJ/FUWWz3j0zJ3FmQ98HWqGigyb5LJNMFawMVE8VZ2c4RJ2g89MYB6vmIMnIW4zpfgFq8doTEKQ2hyV2Cq/SBScq8xUTxQ8IClm7xMq79GrsQ04xENFjcNSf6Eodds4PPWHq1Lw/5+Ixaz3DoPQgTGInEl69wrvZTU17o/QZLlogVnq2oDjCZrr+GEcyDThJ4dZlK+Y+9vCS0UrpJp/uQHMRH9bEg44QPUcr9B4567yN9n/iZj8KVI1ZAtdNfUujQGuECjb3ys3LkmW8FWfYGeKq9SW8EgQIGjYFyp6t0hO50rbOvG5xxDS4bj2xgTqTfE/7J6/hUl3WCDFF9i3sC16TWHXh8iG1NWf0KqOjrezpY0zI6RB1PL6MwtJYrlK6mmQK+rM4ZpQMCaYbFQtW7xecQtej6bScYKjyTVzg5GE3hGBk4ymYvpfakplM9PbV7ZPt3e7kh rQj3MZm4 VnpsGE/Fd+UJMwPClBM9p3LDMBxopT7g1RczOBwGrUl0f3ULsxxx83FbEqo/w81qne3K3IQc2UX4kbQi8rLhozTIHfJsqlkfQZnrVDYzVMHzgCQPqZ8yZ9mftjdJbr94Ge5iVU/IBlyi75Zu8cVpvLbzyMED3uHycey5VhaXsB9yGA0iBwMpTPLBnKaTtELyssMJsnUxYOiiAGw1+B5Hp2NucGQIOhOZCSydKNJoMScXLJlXs+RdLhNEIL7l06Isq5hif2zAklY6gkaYm/g1bTcV2fcupa//6N3V45SDmpK9AF1k= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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_user.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 +/* 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 +#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) } 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 = container_of( + work, struct user_event_enabler_fault, work); + struct user_event_enabler *enabler = fault->enabler; + struct user_event_mm *mm = fault->mm; + unsigned long uaddr = enabler->addr; + int ret; + + ret = user_event_mm_fault_in(mm, uaddr); + + if (ret && ret != -ENOENT) { + struct user_event *user = 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 = kmem_cache_zalloc(fault_cache, GFP_NOWAIT | __GFP_NOWARN); + + if (!fault) + return false; + + INIT_WORK(&fault->work, user_event_enabler_fault_fixup); + fault->mm = user_event_mm_get(mm); + fault->enabler = 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 = 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) == 0) return -ENOENT; + if (unlikely(test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)))) + return -EBUSY; + ret = pin_user_pages_remote(mm->mm, uaddr, 1, FOLL_WRITE | FOLL_NOFAULT, &page, NULL, NULL); - if (ret <= 0) { - pr_warn("user_events: Enable write failed\n"); + if (unlikely(ret <= 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; } @@ -314,7 +414,7 @@ static void user_event_enabler_update(struct user_event *user) list_for_each_entry_rcu(enabler, &mm->enablers, link) if (enabler->event == user) - user_event_enabler_write(mm, enabler); + user_event_enabler_write(mm, enabler, true); rcu_read_unlock(); mmap_read_unlock(mm->mm); @@ -562,7 +662,7 @@ static struct user_event_enabler /* Attempt to reflect the current state within the process */ mmap_read_lock(user_mm->mm); - *write_result = user_event_enabler_write(user_mm, enabler); + *write_result = user_event_enabler_write(user_mm, enabler, false); mmap_read_unlock(user_mm->mm); /* @@ -2201,16 +2301,24 @@ static int __init trace_events_user_init(void) { int ret; + fault_cache = KMEM_CACHE(user_event_enabler_fault, 0); + + if (!fault_cache) + return -ENOMEM; + init_group = user_event_group_create(&init_user_ns); - if (!init_group) + if (!init_group) { + kmem_cache_destroy(fault_cache); return -ENOMEM; + } ret = create_user_tracefs(); 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 = NULL; return ret; } From patchwork Tue Mar 28 23:52:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191675 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96928C77B6E for ; Tue, 28 Mar 2023 23:52:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7110D900004; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64E40900003; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 188F8900004; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E629A6B0081 for ; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BEC9AA04AA for ; Tue, 28 Mar 2023 23:52:30 +0000 (UTC) X-FDA: 80619958860.07.81CECAF Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf21.hostedemail.com (Postfix) with ESMTP id 0115E1C001B for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=JP+RRL0d; spf=pass (imf21.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047549; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=15vuXUvlHpOtcTes1qNxWgKfuObVluuULdx0ecG9/h0=; b=nlkCSkRcOHz3/Nwj9N70vMQkT7q5tN0Au+q8vWv5HPe+6+RUbijLwnafktrnoNLMeb07a4 dj2Yuia+E8d9O+LntYR39+NHEna2RCDcYHOSJFL4mMBaUGZi/Bfa8pmCQq89k9BJj64RcK 1DW0P5UIaRUcZJNecTxKqIWvB4Md224= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=JP+RRL0d; spf=pass (imf21.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047549; a=rsa-sha256; cv=none; b=tyAqn+dtYVzzF8iJc8nuUIuEAAcpGLngXsYyIa7xYOTFJv7ngREE6QCNYsOcoj+GrF7Az2 4kWKdrCGLU7nKXbY8K1EFZs+4orSRmaWS19JBIdqSPr7z/gUaNUnxavxIJBkbtom2WIRk+ GUXuiUeShWYrDvq9ftj1fCo0jUnmcW8= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 5341820FDAB8; Tue, 28 Mar 2023 16:52:26 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5341820FDAB8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047546; bh=15vuXUvlHpOtcTes1qNxWgKfuObVluuULdx0ecG9/h0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JP+RRL0dQnhia5qJ8vX1f86o8bT0Zt28VWd7/uHRdB2xhO5pXMwdVH+1vmdk8KsQ0 A2mnUm2Sdopm+H1hkCvmN8n05f8YJVQS9Fb2PLRhmHrnkkzoKbqSXHyqX13tcfhVSI bFaplcazwYIXcu5e9X36GGYlPqKhhqjIgD0q/F3Q= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 05/12] tracing/user_events: Add ioctl for disabling addresses Date: Tue, 28 Mar 2023 16:52:12 -0700 Message-Id: <20230328235219.203-6-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0115E1C001B X-Stat-Signature: wns3u38upzfbi5ywminapadsd51e3n6u X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1680047548-335050 X-HE-Meta: U2FsdGVkX1/LGxNbMEkZuOTKCX1tnWQ/xFAwphDd+kq3ThVtQm/V4APTPbNCrZz7TN8Tiu1ofClFg3XZ8dmjWoqHl4ACKlh+OXDy4mL7lTtpbajZlBviOLI/tQ0ZPCYTG54EKOlrToTtHteWPmA9Y6OEjv6/VoU6Pqp8zZy6H2OLNQmVExjgtAijCFB0/GmnzIPTL14Ycnag6QPS86NulqhZrGNl/L/H3Py7C/Z53uSoarRQAr8MY4B76LEl3tzb4qaCnX21vwkPod0YbpCYhDs+ze1/4iZXr588Da+m5jCIoClLL+VzWPeQY9jsRsmz2HLbDMeVH7+WEAWdmFnjLMSJo/jGVYngyZFrqn0o71AgZpuDW0VsPjPba+enj3EHstxxL4V+SH6vPQBz/eV13Fdxd38SxjEEGbv8inkmnr6lhOJ0ybUgiL5yXGdu7iR61OCii8aRn/C9IEfyY2NZVMM7fJOQ1n759c+nCprV52tesRcu/9AfPBPbtyOiCDVF7no9bCQ7s7gqGsSkplTs/9UJW3n6uadtzRBxbxMUETxQ4BLinh0bzTOBTUgkmv+6mdFpRFAwnKZLAAcFdFz9VmdFUclOsvS4PtyKIUs4gVovEHEw0mqM2PgF5Q55LrZiqNJA0r0xfuNqw5NbvNtkn7Pe0EzMMcv8aAD0CpTbRfLbT4nIpUb2qe3zSQ17cw7Qi5uplf3K3jZxsdz4I9wZNu5G4ZBH1omT1xDvWKQO73kTHqn3GdsN+0jWo02AmESqn0A+XhbaJuyFcs9qGA0aoBR94dm8bPRu2Twj+xDGqPNUqsL9hadKL3s2w42Lyr7p/e/+6sSjh2lP1RKnPsIY2q8mE/XUxPhbnAUM/8Szk/R0V900A8g3jNcet/KJmaJAPOT2Jfpxh/l67KydRPab+kP2XaqJrSVnUADJNMUYP4sVA8Y4fh30FdAQDlTGReJZS902RQMGs3wUa1mENDb 1R0rI5LR FXamXRw079ZZoEHKSLSTfJ6tG+6jt0sUVXz5GxjTnV/mX0pOWOUMmNVjSkMOWuq/XPrKWNlYK4MSTbBpR/v6sv5ea2kOmC8f0nXMEs9TeOXA86qujk7UsILpUUGHLB/SeoxHckng5vt7cyW5BTCYjeiXDvVMB+bfWScJ2FWXGsHfgxtEUhgOdrGBK9Dp5iyj7U+qpFTfHjbGnF6YEyNu9IOk1A8ByHEWAL/QNj6iFFFj7r7KPDFMNiK7ur9Bk9BR9ZcYywVKciW93kvx4YiKcvQDrfW2stxZ/sCn733anTOSnvJM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- include/uapi/linux/user_events.h | 24 ++++++++ kernel/trace/trace_events_user.c | 97 +++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/user_events.h b/include/uapi/linux/user_events.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__)); +/* + * Describes an event unregister, callers must set the size, address and bit. + * This structure is passed to the DIAG_IOCSUNREG ioctl to disable bit updates. + */ +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 '*' /* 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 *) +/* 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_user.c index 86bda1660536..f88bab3f1fe1 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 +/* 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 @@ -301,6 +304,12 @@ static void user_event_enabler_fault_fixup(struct work_struct *work) /* Prevent state changes from racing */ mutex_lock(&event_mutex); + /* 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); /* In all cases we no longer need the mm or fault */ @@ -370,7 +379,8 @@ static int user_event_enabler_write(struct user_event_mm *mm, if (refcount_read(&mm->tasks) == 0) return -ENOENT; - 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; ret = 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_enabler *orig, { struct user_event_enabler *enabler; + /* Skip pending frees */ + if (unlikely(test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(orig)))) + return true; + enabler = kzalloc(sizeof(*enabler), GFP_NOWAIT); if (!enabler) @@ -2086,6 +2100,79 @@ static long user_events_ioctl_del(struct user_event_file_info *info, return ret; } +static long user_unreg_get(struct user_unreg __user *ureg, + struct user_unreg *kreg) +{ + u32 size; + long ret; + + ret = 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 = copy_struct_from_user(kreg, sizeof(*kreg), ureg, size); + + /* Ensure no reserved values, since we don't support any yet */ + if (kreg->__reserved || kreg->__reserved2) + return -EINVAL; + + 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 = (struct user_unreg __user *)uarg; + struct user_event_mm *mm = current->user_event_mm; + struct user_event_enabler *enabler, *next; + struct user_unreg reg; + long ret; + + ret = user_unreg_get(ureg, ®); + + if (ret) + return ret; + + if (!mm) + return -ENOENT; + + ret = -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 == reg.disable_addr && + (enabler->values & ENABLE_VAL_BIT_MASK) == 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 = 0; + } + + mutex_unlock(&event_mutex); + + return ret; +} + /* * Handles the ioctl from user mode to register or alter operations. */ @@ -2108,6 +2195,12 @@ static long user_events_ioctl(struct file *file, unsigned int cmd, ret = user_events_ioctl_del(info, uarg); mutex_unlock(&group->reg_mutex); break; + + case DIAG_IOCSUNREG: + mutex_lock(&group->reg_mutex); + ret = user_events_ioctl_unreg(uarg); + mutex_unlock(&group->reg_mutex); + break; } return ret; From patchwork Tue Mar 28 23:52:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191672 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0A1FC77B6E for ; Tue, 28 Mar 2023 23:52:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A6438900002; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A16096B0081; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8174A6B0082; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 704BB6B0080 for ; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3F95280B66 for ; Tue, 28 Mar 2023 23:52:30 +0000 (UTC) X-FDA: 80619958860.10.C156B4C Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf15.hostedemail.com (Postfix) with ESMTP id 7085BA000E for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=PPhU+Txa; spf=pass (imf15.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047548; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4Tw0x0REBVNQbPmE7YKz+0Y5T7GJmSyJL4LF57PSOTg=; b=D1BD8lkO3jnpiq+Q1/7l30OzHaYLAkPDjgeXmwR/7dOZAYGmUBPvXMkBSXtpQgJtoA41hc MhyaE4bO8dIeJE35+aVIGOH09iObtJ5u0FHeB/fAgXqrQvsIHGRr4ZbsyS94L/+lTEGlw0 02XqSZNA5zcahWYAdmzMZBTvGpgo4Zo= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=PPhU+Txa; spf=pass (imf15.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047548; a=rsa-sha256; cv=none; b=QqaxR2tlq/88Hm//syO3sUFQeHNXateOJY5L8poCV+et1Bb4MSrtiM6lXVQCiG2KgSLVHS dhnqCudg91mc2xcoNKI6XtdY6lxrlCArqPeFYuVSXTB87+GFReCeR2BCvl6J75/QejCDqJ rTDldifP8jatGE7AqibAh3i+Im8PG9c= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id B3F3F20FDABC; Tue, 28 Mar 2023 16:52:26 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com B3F3F20FDABC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047547; bh=4Tw0x0REBVNQbPmE7YKz+0Y5T7GJmSyJL4LF57PSOTg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PPhU+Txa9J+OmhxNlaZoPcjDXyHyCZ9FDOVMk0XDLbiw91/+Kmr6E4Tc4EEOXPHUa IcstD4kqI+CXd8qyR0WvK8eMzo7/EXyThPHVnUdVf303yaWS6G642Hl1YX9s0/abnb 87OFyFAmbkhmRT3lzBdDWQusizzo2/or3m8RXSbw= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 06/12] tracing/user_events: Update self-tests to write ABI Date: Tue, 28 Mar 2023 16:52:13 -0700 Message-Id: <20230328235219.203-7-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 396sjoujqk1yrjd1zro8extzhk8eio1d X-Rspamd-Queue-Id: 7085BA000E X-HE-Tag: 1680047548-927440 X-HE-Meta: U2FsdGVkX19VooRQOyegZ9HLlx3KDktQknf2EUjT16+PH+yNUlW03OfHbUJ7iAeAYT2JVqi3S2zpHbuzWWIy6psbUFcWb+Zuw3OuLvc6Dian4R7D3RvavcMCNCd/ItKn4kej/XVBJE18eAPiDPhL7B9UoES7lrgpXuhxaVhx0sZkaCPzhApr4JEv4cqe7PEP4QAImgMTp9HApHaT8rhe9puJuUSZfsz+L/Til3V7byY6YaGJtD3gsw5ixQ7Nuxw97J7A2BbDiyELvE6+drEwx248FfhtPZLih9nzka1012gFevac29EWZQJTmxWsr7Vnz4CHvhdb9+2rSBIjKGIHGXyHQ78+QsQxlWGat4EiYiSgBXw2nejSbghLLqppZ9dO8AwMX8CNHUt70s581AR48ArmsKguTMWfhhmvuXNPdy1rXHqYvd65cH23ZB0NwkXe2TsPtRxVBReiSujKayP4C48fNefR47/s9Vfk29CmplFQAdhSKKgm21sV/pzNSgPSAwN+qw8QuMfqx/XCpKn+gvAT4mWgxV0Av1RnA9giKMnBq421o0HmnkwJntOLcZQO89+3sjToa390mD56GwGmjFrqPVJfrPB1iZ9fqwTTTTPbRivotYxc/xPx9c5iR01QI5mIa84GsbX8a4rr/qMBSj7oQD3FyaRmusLiet/Xix6dIzUkFE/zCSpUTHppFcMIE0ayleV5LKYznIaKDFDUyfVIEUCavprN/gI0J9FNPM21+jeEDWzdByKy01AIfZFti2Hwjrxgmb/0wLVmEEzvBwBj1usU0sn6A2W0sZldaRpmAXb4lz8r55zIJJUp+lblIMZH8F88QJ9JsUj6QymIqnTwGvmQT6GYz/jp4JDSyToNwZHSn79k2IcsjeRHoTfjov6HENA4TUjS93XHrinhMkpcry3d13sQoeXywuchpdU542Dg0sejhBCQNVKRe3GMXRU+gCZOugo6HcLMbLj 6ZiM8euC QneazuYNC8SyxKevhC3lsAFbQOhR3vXSB+UsmzIfAMppLERtJ0+NnYvcuv6T9AuFPOks6pVzB5NKHgxLDxJOXA7XlImvI+zXvyLXRjTfS4S2H/EW1mf0uNfy+hAyvAkEhyW3PfKC9iAk1Yncj8HFyR2JukOHJHoN5aq2R2NMIMqu6pTf3+bUN+em3GflX+WvIjvBlTLbMIKnxyhxwrT0x+NiuAtchOlYRlloGp3uH8vGZJ+Qwc8pxD92SdgS110nRAytdNbVD+UktOLgGr45PVv89fV6/pdAUs4M6R1o2munA2n8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- .../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 @@ #include "../kselftest_harness.h" -const char *dyn_file = "/sys/kernel/debug/tracing/dynamic_events"; +const char *dyn_file = "/sys/kernel/tracing/dynamic_events"; const char *clear = "!u:__test_event"; static int Append(const char *value) diff --git a/tools/testing/selftests/user_events/ftrace_test.c b/tools/testing/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 #include "../kselftest_harness.h" -const char *data_file = "/sys/kernel/debug/tracing/user_events_data"; -const char *status_file = "/sys/kernel/debug/tracing/user_events_status"; -const char *enable_file = "/sys/kernel/debug/tracing/events/user_events/__test_event/enable"; -const char *trace_file = "/sys/kernel/debug/tracing/trace"; -const char *fmt_file = "/sys/kernel/debug/tracing/events/user_events/__test_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 = "/sys/kernel/tracing/user_events_data"; +const char *status_file = "/sys/kernel/tracing/user_events_status"; +const char *enable_file = "/sys/kernel/tracing/events/user_events/__test_event/enable"; +const char *trace_file = "/sys/kernel/tracing/trace"; +const char *fmt_file = "/sys/kernel/tracing/events/user_events/__test_event/format"; static int trace_bytes(void) { @@ -106,13 +102,23 @@ static int get_print_fmt(char *buffer, int len) return -1; } -static int clear(void) +static int clear(int *check) { + struct user_unreg unreg = {0}; + + unreg.size = sizeof(unreg); + unreg.disable_bit = 31; + unreg.disable_addr = (__u64)check; + int fd = open(data_file, O_RDWR); if (fd == -1) return -1; + if (ioctl(fd, DIAG_IOCSUNREG, &unreg) == -1) + if (errno != ENOENT) + return -1; + if (ioctl(fd, DIAG_IOCSDEL, "__test_event") == -1) if (errno != ENOENT) return -1; @@ -122,7 +128,7 @@ static int clear(void) return 0; } -static int check_print_fmt(const char *event, const char *expected) +static int check_print_fmt(const char *event, const char *expected, int *check) { struct user_reg reg = {0}; char print_fmt[256]; @@ -130,7 +136,7 @@ static int check_print_fmt(const char *event, const char *expected) int fd; /* Ensure cleared */ - ret = clear(); + ret = clear(check); if (ret != 0) return ret; @@ -142,14 +148,19 @@ static int check_print_fmt(const char *event, const char *expected) reg.size = sizeof(reg); reg.name_args = (__u64)event; + reg.enable_bit = 31; + reg.enable_addr = (__u64)check; + reg.enable_size = sizeof(*check); /* Register should work */ ret = ioctl(fd, DIAG_IOCSREG, ®); close(fd); - if (ret != 0) + if (ret != 0) { + printf("Reg failed in fmt\n"); return ret; + } /* Ensure correct print_fmt */ ret = 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; }; FIXTURE_SETUP(user) { @@ -185,59 +197,56 @@ FIXTURE_TEARDOWN(user) { close(self->enable_fd); } - ASSERT_EQ(0, clear()); + if (clear(&self->check) != 0) + printf("WARNING: Clear didn't work!\n"); } TEST_F(user, register_events) { struct user_reg reg = {0}; - int page_size = sysconf(_SC_PAGESIZE); - char *status_page; + struct user_unreg unreg = {0}; reg.size = sizeof(reg); reg.name_args = (__u64)"__test_event u32 field1; u32 field2"; + reg.enable_bit = 31; + reg.enable_addr = (__u64)&self->check; + reg.enable_size = sizeof(self->check); - status_page = mmap(NULL, page_size, PROT_READ, MAP_SHARED, - self->status_fd, 0); + unreg.size = sizeof(unreg); + unreg.disable_bit = 31; + unreg.disable_addr = (__u64)&self->check; /* Register should work */ ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, ®)); ASSERT_EQ(0, reg.write_index); - ASSERT_NE(0, reg.status_bit); /* 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); /* Ensure disabled */ self->enable_fd = open(enable_file, O_RDWR); ASSERT_NE(-1, self->enable_fd); ASSERT_NE(-1, write(self->enable_fd, "0", sizeof("0"))) - /* 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); /* 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); /* File still open should return -EBUSY for delete */ ASSERT_EQ(-1, ioctl(self->data_fd, DIAG_IOCSDEL, "__test_event")); ASSERT_EQ(EBUSY, errno); - /* 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 = 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)); } TEST_F(user, write_events) { @@ -245,11 +254,12 @@ TEST_F(user, write_events) { struct iovec io[3]; __u32 field1, field2; int before = 0, after = 0; - int page_size = sysconf(_SC_PAGESIZE); - char *status_page; reg.size = sizeof(reg); reg.name_args = (__u64)"__test_event u32 field1; u32 field2"; + reg.enable_bit = 31; + reg.enable_addr = (__u64)&self->check; + reg.enable_size = sizeof(self->check); field1 = 1; field2 = 2; @@ -261,18 +271,10 @@ TEST_F(user, write_events) { io[2].iov_base = &field2; io[2].iov_len = sizeof(field2); - status_page = 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); /* Write should fail on invalid slot with ENOENT */ io[0].iov_base = &field2; @@ -287,7 +289,7 @@ TEST_F(user, write_events) { ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1"))) /* Event should now be enabled */ - ASSERT_NE(0, status_check(status_page, reg.status_bit)); + ASSERT_NE(1 << reg.enable_bit, self->check); /* Write should make it out to ftrace buffers */ before = trace_bytes(); @@ -304,6 +306,9 @@ TEST_F(user, write_fault) { reg.size = sizeof(reg); reg.name_args = (__u64)"__test_event u64 anon"; + reg.enable_bit = 31; + reg.enable_addr = (__u64)&self->check; + reg.enable_size = sizeof(self->check); anon = 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); /* 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 = 0, after = 0; - int page_size = sysconf(_SC_PAGESIZE); - char *status_page; - - status_page = mmap(NULL, page_size, PROT_READ, MAP_SHARED, - self->status_fd, 0); reg.size = sizeof(reg); reg.name_args = (__u64)"__test_event __rel_loc char[] data"; + reg.enable_bit = 31; + reg.enable_addr = (__u64)&self->check; + reg.enable_size = sizeof(self->check); /* 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); io[0].iov_base = ®.write_index; io[0].iov_len = sizeof(reg.write_index); @@ -369,7 +366,7 @@ TEST_F(user, write_validator) { ASSERT_NE(-1, write(self->enable_fd, "1", sizeof("1"))) /* Event should now be enabled */ - ASSERT_NE(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(1 << reg.enable_bit, self->check); /* Full in-bounds write should work */ before = trace_bytes(); @@ -409,71 +406,88 @@ TEST_F(user, print_fmt) { int ret; ret = check_print_fmt("__test_event __rel_loc char[] data", - "print fmt: \"data=%s\", __get_rel_str(data)"); + "print fmt: \"data=%s\", __get_rel_str(data)", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event __data_loc char[] data", - "print fmt: \"data=%s\", __get_str(data)"); + "print fmt: \"data=%s\", __get_str(data)", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event s64 data", - "print fmt: \"data=%lld\", REC->data"); + "print fmt: \"data=%lld\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event u64 data", - "print fmt: \"data=%llu\", REC->data"); + "print fmt: \"data=%llu\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event s32 data", - "print fmt: \"data=%d\", REC->data"); + "print fmt: \"data=%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event u32 data", - "print fmt: \"data=%u\", REC->data"); + "print fmt: \"data=%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event int data", - "print fmt: \"data=%d\", REC->data"); + "print fmt: \"data=%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event unsigned int data", - "print fmt: \"data=%u\", REC->data"); + "print fmt: \"data=%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event s16 data", - "print fmt: \"data=%d\", REC->data"); + "print fmt: \"data=%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event u16 data", - "print fmt: \"data=%u\", REC->data"); + "print fmt: \"data=%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event short data", - "print fmt: \"data=%d\", REC->data"); + "print fmt: \"data=%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event unsigned short data", - "print fmt: \"data=%u\", REC->data"); + "print fmt: \"data=%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event s8 data", - "print fmt: \"data=%d\", REC->data"); + "print fmt: \"data=%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event u8 data", - "print fmt: \"data=%u\", REC->data"); + "print fmt: \"data=%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event char data", - "print fmt: \"data=%d\", REC->data"); + "print fmt: \"data=%d\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event unsigned char data", - "print fmt: \"data=%u\", REC->data"); + "print fmt: \"data=%u\", REC->data", + &self->check); ASSERT_EQ(0, ret); ret = check_print_fmt("__test_event char[4] data", - "print fmt: \"data=%s\", REC->data"); + "print fmt: \"data=%s\", REC->data", + &self->check); ASSERT_EQ(0, ret); } diff --git a/tools/testing/selftests/user_events/perf_test.c b/tools/testing/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 @@ #include "../kselftest_harness.h" -const char *data_file = "/sys/kernel/debug/tracing/user_events_data"; -const char *status_file = "/sys/kernel/debug/tracing/user_events_status"; -const char *id_file = "/sys/kernel/debug/tracing/events/user_events/__test_event/id"; -const char *fmt_file = "/sys/kernel/debug/tracing/events/user_events/__test_event/format"; +const char *data_file = "/sys/kernel/tracing/user_events_data"; +const char *id_file = "/sys/kernel/tracing/events/user_events/__test_event/id"; +const char *fmt_file = "/sys/kernel/tracing/events/user_events/__test_event/format"; 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); } -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 = fopen(id_file, "r"); @@ -88,45 +82,38 @@ static int get_offset(void) } FIXTURE(user) { - int status_fd; int data_fd; + int check; }; FIXTURE_SETUP(user) { - self->status_fd = open(status_file, O_RDONLY); - ASSERT_NE(-1, self->status_fd); - self->data_fd = open(data_file, O_RDWR); ASSERT_NE(-1, self->data_fd); } FIXTURE_TEARDOWN(user) { - close(self->status_fd); close(self->data_fd); } TEST_F(user, perf_write) { struct perf_event_attr pe = {0}; struct user_reg reg = {0}; - int page_size = sysconf(_SC_PAGESIZE); - char *status_page; struct event event; struct perf_event_mmap_page *perf_page; + int page_size = sysconf(_SC_PAGESIZE); int id, fd, offset; __u32 *val; reg.size = sizeof(reg); reg.name_args = (__u64)"__test_event u32 field1; u32 field2"; - - status_page = mmap(NULL, page_size, PROT_READ, MAP_SHARED, - self->status_fd, 0); - ASSERT_NE(MAP_FAILED, status_page); + reg.enable_bit = 31; + reg.enable_addr = (__u64)&self->check; + reg.enable_size = sizeof(self->check); /* 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); /* Id should be there */ id = get_id(); @@ -149,7 +136,7 @@ TEST_F(user, perf_write) { ASSERT_NE(MAP_FAILED, perf_page); /* Status should be updated */ - ASSERT_NE(0, status_check(status_page, reg.status_bit)); + ASSERT_EQ(1 << reg.enable_bit, self->check); event.index = reg.write_index; event.field1 = 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); } int main(int argc, char **argv) From patchwork Tue Mar 28 23:52:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191674 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5E7C1C761A6 for ; Tue, 28 Mar 2023 23:52:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 46F2B6B0080; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 447BE6B0081; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 051D56B0082; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D2F0F6B0080 for ; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 99BE81A047B for ; Tue, 28 Mar 2023 23:52:30 +0000 (UTC) X-FDA: 80619958860.03.664C50F Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf24.hostedemail.com (Postfix) with ESMTP id CA3C6180004 for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=i6MpK4pf; spf=pass (imf24.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047549; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yLsV99bpcauClh1MlrmzrNiAoYsutXdpDe7QcYfID8o=; b=ybG5+eUOhrRp012Obsz3AX1F0Qf1gQeUFXkawLV+7wsw40Qjz+J+uAHzvy8MpnDNKf8Jle krqO1UVS2JSkwBb9/VBPm8aKWiIWzFx8uIt1zST/NSU9tYzwrC/XCHmrCmZEg8+wp+kUdg tCzQrVU5bBA3st9LO51LVpYG+MT3YzE= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=i6MpK4pf; spf=pass (imf24.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047549; a=rsa-sha256; cv=none; b=OSbABf1h4Y+wpM5ywS+KOwrXs6enz3Wi2jORFVPqO7U6VyAWhGjgc2zgA9O2VxtY9Ntw/Z X1AWVd6zmUNNxO5BZDwYG0tY+AmgcdjQ9UWz4XGPOOc7l32ah0vJuojAX75gYVBD8x6x7d 3+JYF0a7VBWpQp1lw/txx4txpfAaswU= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 2C38520FDC21; Tue, 28 Mar 2023 16:52:27 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2C38520FDC21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047547; bh=yLsV99bpcauClh1MlrmzrNiAoYsutXdpDe7QcYfID8o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i6MpK4pfOWHADyzKWlDD34ASp6Fi+iHOshXZoGrS+ecaPzj5oVvyxVaVAxAgPBNzy v6K43iulDXumEgDf7BCcG6XDam3aUbptyOWXWt+Fs909i4qa4Cc38ftt+STQ+1pFUT a5qU8X+av+WTqdTcsVSxVNbV29vHr4AJrpyjxpaM= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 07/12] tracing/user_events: Add ABI self-test Date: Tue, 28 Mar 2023 16:52:14 -0700 Message-Id: <20230328235219.203-8-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: ofzgdhmyg1dkkkfuanmt1hweneu5y3kz X-Rspam-User: X-Rspamd-Queue-Id: CA3C6180004 X-Rspamd-Server: rspam06 X-HE-Tag: 1680047548-647598 X-HE-Meta: U2FsdGVkX19kY1Frh30h/SGLTwS4uHXZjeKEU22CPr0fa+7ROUQYx/RZ6GmuVYzVg/HSD6CNaalJwKBDdabg98tg6J6U8gO6zZul/By83mLVXW0hkzwGBoK42x70Pqq7KR63cUg85fjnFPK17dgRlVhTUWOvtRhs15LuRFocoT56GnmgDeFZWMGnBjHEjyakpyTgtvEE2qCPS2sE9uLkHeu0X1Dma/M9T92Ch1Xa5ahd2os5TkJKuhrHIs4rsUiCYI8o+w3gtMSt/CdiAMNYQIciKeUo2jocoIFdi06CxIAYqRmOcY5kJpyRgzNED3aCU+VpCwH6HrC1GejmRYeRoYHmzDNcXpzKVDWR1yr+hfeoJaAeyq4UjrmvyjCEYsyG1261VF1Yu1eyl/35Wjde8SJ+QtMQXZRxjo7D/V6gycyEvlj4647scYMjwSTqwGnlDFdO/64oRfaJR0Trt65vd3Es0cuwRS5b6injhl8k2QAb9bKze1RNmyaOhLA1w9FERuNDj/eQlvjEY5bJJEn2NBLtrCyY6EChjy0DDlpJndSY7Qe4QHe7b3W3lKKnxvXexRuYWXs+iNV5j6S4wVF7d5KR9aR5NsHioUTqaomcY5kEpM1Z7hPn7K2f0BjZOq75ezCo8OvZ1kGy9VMisKOhsw9TnAIW6XcaARuOJVCXlJuKtGbY8g86cqxttlw96Fm0ly9QZlv6IEyuks84drLqbId0Mn8EN3nAJCRfCVNVD25HbM0AEM2vqmUucInf8FuZ4fj1jVI+vaXWC0CvxnQuWU2PnoDHJbWU/XlhhBPJAKpN+JvDEris+2aD0ctdGF9yRqk74qwQpquaKQFWkKqTEOLZ2QLRR8Srn9QUzor6c0pIEwjTT1bBbVAvDhjvtxqjjJegDyLhGiOoJ4UjHBQqbzWdjeEvrsfGArPsWr29y+/t6GM5SACLqrejtQnwWZizCM55T5s2azWL+PpjWeS /TsyFrfP suLenjTNi2rYjEI5wD9p+Xckl5KoyKxbuyTHSkUX02Xl5sRT5p71dLo28G4efGr9YMR72nDI0MynctRdHpztu/jT0nINFmqJPAJljZDMiDFCpcKtOIsO0aJZD2xZvMbh9LiCiJZxXJH3IBqslQm5TqJpoTbtitL3jaIHOzYyy4zP+UHHnVNgaax8IvaIQjANq+T6nMaH60Pq2x+Yo9v5uGP1bY57kGMZFly4QpfTfw1GE5ldUEEdBqRq91UrRqAhoLxOaAXEQJoylq9whaddQIb7GMB/OjzIoHSkGmYZewjGuVM5GV88w+pdUhqfvGf5KRfLLDJE22W7x2Tp2W+QD8GWhe36f9YsQ/RpvjipF5JhxF2XTL1A4eVIrteUhzGxFmM/X X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add ABI specific self-test to ensure enablements work in various scenarios such as fork, VM_CLONE, and basic event enable/disable. Ensure ABI contracts/limits are also being upheld, such as bit limits and data size limits. Signed-off-by: Beau Belgrave --- tools/testing/selftests/user_events/Makefile | 2 +- .../testing/selftests/user_events/abi_test.c | 226 ++++++++++++++++++ 2 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/user_events/abi_test.c diff --git a/tools/testing/selftests/user_events/Makefile b/tools/testing/selftests/user_events/Makefile index c765d8635d9a..d5f64ef93197 100644 --- a/tools/testing/selftests/user_events/Makefile +++ b/tools/testing/selftests/user_events/Makefile @@ -2,7 +2,7 @@ CFLAGS += -Wl,-no-as-needed -Wall -I../../../../usr/include LDLIBS += -lrt -lpthread -lm -TEST_GEN_PROGS = ftrace_test dyn_test perf_test +TEST_GEN_PROGS = ftrace_test dyn_test perf_test abi_test TEST_FILES := settings diff --git a/tools/testing/selftests/user_events/abi_test.c b/tools/testing/selftests/user_events/abi_test.c new file mode 100644 index 000000000000..e0323d3777a7 --- /dev/null +++ b/tools/testing/selftests/user_events/abi_test.c @@ -0,0 +1,226 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * User Events ABI Test Program + * + * Copyright (c) 2022 Beau Belgrave + */ + +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +const char *data_file = "/sys/kernel/tracing/user_events_data"; +const char *enable_file = "/sys/kernel/tracing/events/user_events/__abi_event/enable"; + +static int change_event(bool enable) +{ + int fd = open(enable_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + if (enable) + ret = write(fd, "1", 1); + else + ret = write(fd, "0", 1); + + close(fd); + + if (ret == 1) + ret = 0; + else + ret = -1; + + return ret; +} + +static int reg_enable(long *enable, int size, int bit) +{ + struct user_reg reg = {0}; + int fd = open(data_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + reg.size = sizeof(reg); + reg.name_args = (__u64)"__abi_event"; + reg.enable_bit = bit; + reg.enable_addr = (__u64)enable; + reg.enable_size = size; + + ret = ioctl(fd, DIAG_IOCSREG, ®); + + close(fd); + + return ret; +} + +static int reg_disable(long *enable, int bit) +{ + struct user_unreg reg = {0}; + int fd = open(data_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + reg.size = sizeof(reg); + reg.disable_bit = bit; + reg.disable_addr = (__u64)enable; + + ret = ioctl(fd, DIAG_IOCSUNREG, ®); + + close(fd); + + return ret; +} + +FIXTURE(user) { + long check; +}; + +FIXTURE_SETUP(user) { + change_event(false); + self->check = 0; +} + +FIXTURE_TEARDOWN(user) { +} + +TEST_F(user, enablement) { + /* Changes should be reflected immediately */ + ASSERT_EQ(0, self->check); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, change_event(true)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(0, self->check); + + /* Should not change after disable */ + ASSERT_EQ(0, change_event(true)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, reg_disable(&self->check, 0)); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(1, self->check); + self->check = 0; +} + +TEST_F(user, bit_sizes) { + /* Allow 0-31 bits for 32-bit */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 31)); + ASSERT_NE(0, reg_enable(&self->check, sizeof(int), 32)); + ASSERT_EQ(0, reg_disable(&self->check, 0)); + ASSERT_EQ(0, reg_disable(&self->check, 31)); + +#if BITS_PER_LONG == 8 + /* Allow 0-64 bits for 64-bit */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(long), 63)); + ASSERT_NE(0, reg_enable(&self->check, sizeof(long), 64)); + ASSERT_EQ(0, reg_disable(&self->check, 63)); +#endif + + /* Disallowed sizes (everything beside 4 and 8) */ + ASSERT_NE(0, reg_enable(&self->check, 1, 0)); + ASSERT_NE(0, reg_enable(&self->check, 2, 0)); + ASSERT_NE(0, reg_enable(&self->check, 3, 0)); + ASSERT_NE(0, reg_enable(&self->check, 5, 0)); + ASSERT_NE(0, reg_enable(&self->check, 6, 0)); + ASSERT_NE(0, reg_enable(&self->check, 7, 0)); + ASSERT_NE(0, reg_enable(&self->check, 9, 0)); + ASSERT_NE(0, reg_enable(&self->check, 128, 0)); +} + +TEST_F(user, forks) { + int i; + + /* Ensure COW pages get updated after fork */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, self->check); + + if (fork() == 0) { + /* Force COW */ + self->check = 0; + + /* Up to 1 sec for enablement */ + for (i = 0; i < 10; ++i) { + usleep(100000); + + if (self->check) + exit(0); + } + + exit(1); + } + + /* Allow generous time for COW, then enable */ + usleep(100000); + ASSERT_EQ(0, change_event(true)); + + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + + /* Ensure child doesn't disable parent */ + if (fork() == 0) + exit(reg_disable(&self->check, 0)); + + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(0, self->check); +} + +/* Waits up to 1 sec for enablement */ +static int clone_check(void *check) +{ + int i; + + for (i = 0; i < 10; ++i) { + usleep(100000); + + if (*(long *)check) + return 0; + } + + return 1; +} + +TEST_F(user, clones) { + int i, stack_size = 4096; + void *stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, + -1, 0); + + ASSERT_NE(MAP_FAILED, stack); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, self->check); + + /* Shared VM should see enablements */ + ASSERT_NE(-1, clone(&clone_check, stack + stack_size, + CLONE_VM | SIGCHLD, &self->check)); + + ASSERT_EQ(0, change_event(true)); + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + munmap(stack, stack_size); + ASSERT_EQ(0, change_event(false)); +} + +int main(int argc, char **argv) +{ + return test_harness_run(argc, argv); +} From patchwork Tue Mar 28 23:52:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191673 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 38DE0C76196 for ; Tue, 28 Mar 2023 23:52:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 05BDF6B0087; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E33A36B0083; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CAE446B0081; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B614C900003 for ; Tue, 28 Mar 2023 19:52:30 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 706B1ABDA8 for ; Tue, 28 Mar 2023 23:52:30 +0000 (UTC) X-FDA: 80619958860.07.70A9DAA Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf05.hostedemail.com (Postfix) with ESMTP id B221D100006 for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=fw5Hu2PH; spf=pass (imf05.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047548; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dmkEXknb1A0Qhw2AWPTXryudmpuAFo4IqxhDoe6dNiw=; b=IRZk7BFbyL7dULje+pVU8mjA+p0JDRZDJRSE/k81nqCTqP66iMAn4La4MqTje0E/W6bxVX rVAhfhZL8l0TgqdKBXXHc+kfT9lEhsN4+p8ZeXhmukfDSWOv5+mFHG6rU5YDvpuxdHaEDs T35S+U4+IvG/hT95/FEUzKGbLJCBBq4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=fw5Hu2PH; spf=pass (imf05.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047548; a=rsa-sha256; cv=none; b=bwz2QpspM0u1lw1Norz0BwCio8Vo5Xct4SRyA8DGPsJgtaP/toySWD+tWlwJzZapGZHeCJ H8XWLN+XisUMlJDztbqFxsO4U3MCYK+adIGzIAKX7bPKUmb2T+qSEtn2jNykCLzfZ/FL2o qn7WKXPIgptxsZ2Y11MauIfFNAnUTKY= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 840C020FDC26; Tue, 28 Mar 2023 16:52:27 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 840C020FDC26 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047547; bh=dmkEXknb1A0Qhw2AWPTXryudmpuAFo4IqxhDoe6dNiw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fw5Hu2PHEUphs88pUUTVc1A++7+3aYnR0xPiH7iapsn3ImWO0zjYiFI1POXN/rGK+ IyFSibY7nreLfH+9/gTtweT0J02Xe+1Dazlnwne3kXXuxztr3jowfd0h8ZKAovdhhk sJzvvVFl1u2gHKPj8svpyDZsVB9/Un6iIlU0Mae4= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 08/12] tracing/user_events: Use write ABI in example Date: Tue, 28 Mar 2023 16:52:15 -0700 Message-Id: <20230328235219.203-9-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B221D100006 X-Rspam-User: X-Stat-Signature: hm77im5r4ct9ahtrw8sw8eykkad3nr7h X-HE-Tag: 1680047548-615069 X-HE-Meta: U2FsdGVkX1/86Oj/mxsrSAdcMB5g596Fra3M+W6YuFpYpKG4CSMszIYMtyUbLqBjsW9S8Ozj3aOWxH8QRIkaRUFnJYdOpRpJGfSpXL5zXoEtlr5i7DVodBa1tXoG3KEFt4O6hkPbDHghxNk5I0YduQO1qNgNvBy2eIk+F78ZlTGiQwl9Xc1prmY7t6blP/ZxnJThYxRurcWYUejh7CAUW/vwOAOYpnawA7+4PWQ8d5jZsj/9AIQrEpyXtOqWil+6Df2bWyPkSzMPyaKcwxmAv2dntRmaj1SiGZfaE6jNiEutdHrVdLiy9OaLwzuJp9y+WiId0o3VlTo0y7u0bTE8t7mksguWOb6CiaRUu2HHfsJUoGyFRDdsmCpl6j6d5dPRGsCQR5G74tBCzYalG0mDiBfjsSa9R7JyONbuF3ivqOsCK8UaxwNHEDlRt6dJSL4gU9e0lIi8rnZDDvZSgW6u79OND/pBb2ORT7sOMHCBtMjGaOs5FKQM5ZajcZrBK06xCZOKOS0FhRxXLwjeuxZY+lpxuRa2rtNadj5YNg7CvIv62t1Xu2MZ4KGKRHmkAkpnUnCZlvQmoVNO30yPxWoHofRnZMYlFlNI5+D/HoiQ5ojCLusBXNUKdlDckLcZVCY2LEesgNwX862/mOi3Bc4pMOPjh6kQp+ESMB3tNhMCKi9i/bGxNHsAmiSp9AGGFwhWAUnmtuxiCUyVksrYR0asgY/qqpJ7m3LkFCxjZ+nK0VJkS6bJFGA2tlMV5eIDEiYIoCU6YXruSxddcHAxhBBdsn6sV5MNl03OIJSBxqt3XX6IAZRt+3nMGwQwuKjWpIp3RxIhVH/UfaMSSRkZpPL7NqhwkiznkUr/ltkSAMLX9c/RwGIMzKKXo64oK5eDOUGp14AbCWt03n2ZXl9Eegxv3m51OH28cO5zwR8jvAGhAz53/6zvxz6zOe8fNcnkFwDgwl40vUDNZoN4THxqu+n r7/LNqsR 1zdcbXoySYjzXwDek+rBpZaXPIzvDGYoyOoeTuAvKnmpR9LLrYAJMbIt20OlLHjpmHhNBGlg/T1kjHbfSgTA4/z61Fo4rFfXn1eX3yq+CDGf1Pbz3nQXwnD2dKAkSAdP0cSvEE62j5xDzKbOvZLMQmK+Fve7x8/VqjzbpnJ8ZxI2Ilx1iRbAFpX+jTuZ/h0VGgGOYBPtRG9zVXKXI2xWUQkzfG0ilITQs33hMVY3m8YzXQrONfB25p/eX/3f9a3pU5jrDFGcl13sY+kW+id6CDO0ZrRjfOO6NqDqivdqxUiypvNZsikvUDquCThesL9Tiz5dNnoNAqU+kPjbvJbg6Zs/Zjn38+3gJ3ihjBRFRVY9Z+k38ytiUMYX8SA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 -#if __BITS_PER_LONG == 64 -#define endian_swap(x) htole64(x) -#else -#define endian_swap(x) htole32(x) -#endif +const char *data_file = "/sys/kernel/tracing/user_events_data"; +int enabled = 0; -/* Assumes debugfs is mounted */ -const char *data_file = "/sys/kernel/debug/tracing/user_events_data"; -const char *status_file = "/sys/kernel/debug/tracing/user_events_status"; - -static int event_status(long **status) -{ - int fd = open(status_file, O_RDONLY); - - *status = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ, - MAP_SHARED, fd, 0); - - close(fd); - - if (*status == 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 = {0}; reg.size = sizeof(reg); + reg.enable_bit = 31; + reg.enable_size = sizeof(*enabled); + reg.enable_addr = (__u64)enabled; reg.name_args = (__u64)command; if (ioctl(fd, DIAG_IOCSREG, ®) == -1) return -1; - *index = reg.status_bit / __BITS_PER_LONG; - *mask = endian_swap(1L << (reg.status_bit % __BITS_PER_LONG)); *write = reg.write_index; 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 = 0; - if (event_status(&status_page) == -1) - return errno; - data_fd = open(data_file, O_RDWR); - if (event_reg(data_fd, "test u32 count", &index, &mask, &write) == -1) + if (event_reg(data_fd, "test u32 count", &write, &enabled) == -1) return errno; /* Setup iovec */ @@ -80,13 +52,12 @@ int main(int argc, char **argv) io[0].iov_len = sizeof(write); io[1].iov_base = &count; io[1].iov_len = sizeof(count); - ask: printf("Press enter to check status...\n"); getchar(); /* 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); From patchwork Tue Mar 28 23:52:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191676 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E06EDC76196 for ; Tue, 28 Mar 2023 23:52:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5C55900005; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB26E900003; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D2886B0083; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 11C9E900003 for ; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D88101A042D for ; Tue, 28 Mar 2023 23:52:30 +0000 (UTC) X-FDA: 80619958860.04.6763270 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf14.hostedemail.com (Postfix) with ESMTP id 211B310000C for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=lmrNtkNN; spf=pass (imf14.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047549; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qI96osokKp98pPQQ0gcTi7CZ/dblw6z+BDokZ8I1srE=; b=cdRzNDq/ai8HsPM0p6TGHBPHBMI2z0sSe7wkO2Uuz/W7zdlvsanx+EEv7hItNl4jd3gj26 ZMmWYcLms+ccPzoxasH43cmCcv9IrO71oOlXs7kOXZEILnhPJlhC+M78JSfPYb47IKNmmi 2qxwVSEExfLXW1TnZRPRXuE9nJl6bcs= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=lmrNtkNN; spf=pass (imf14.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047549; a=rsa-sha256; cv=none; b=hQaAZ5qCRB5GrLhKoj9pHstArziZ6QB6ETxVrv17pGnaY7Kcw5B1ZTUhVGziV5FoXrSCEl 1NdBKveVKoQJ2Hg4jCYnc30lYmIEk37FtpiCD8jdUYa5pf57a86nTlESAS4j7a32MMPgEl MrvDUqKIGNqNael+Y6oD889BDRVKhCs= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id DFDE520FDC2B; Tue, 28 Mar 2023 16:52:27 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com DFDE520FDC2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047548; bh=qI96osokKp98pPQQ0gcTi7CZ/dblw6z+BDokZ8I1srE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lmrNtkNNG4dzoN7913JS2gWP+R8KGWlUDqKaxO8sCMrmKxOVyLwVtjKYhmuMO865J Tji3YhaEy0FIY1/Knn034TsTx2mXweD//zlirKc8oTd+P7RU8z18hF9vmwovu92SI2 dqRA68YgPlxdY/pvAjSzhDOj6IayloDfARS7MtB0= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 09/12] tracing/user_events: Update documentation for ABI Date: Tue, 28 Mar 2023 16:52:16 -0700 Message-Id: <20230328235219.203-10-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 211B310000C X-Stat-Signature: ohoicazq5q147zheeh4j5fo61h3bfoij X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1680047548-61970 X-HE-Meta: U2FsdGVkX1/VD+I8qAnYneyKpDkpInjOKOfGznSX/l/uXokttSw3c63rlAya4BK2ml1qoxP1VieRi0uMmoeT49TIBBBWpAtU5GsZ67n0+plLbQw2HE3zDLcg0bLIRaeEMaS8B0S4J/XKU5oPMwr56V2BWZLRlOYNuNUIduA/JSTQxkPmUhrG1eooScIsVR8XKTcH0GSVc3YOALxqp+58cv/9Qj0JySrNJ9+2M6F22PxNMlzEu8tWhIISAKDtzLcR5S05usCTQZEQ45XP05xII0Zwxvvfjhk7D16i3unS9iVnKaJsRx/7bbMGX3oWt/WswSHfuL+tNHnQeQGqgxLyWr0xAvXrRLsthi2TjCeF04msrPYm9mVAMsH2a5Y4nxMbjODjLy4dX221RyffMj3k8wtW8705EUPVyOWCg4/xQHbWm48Oe/e07tmm4NEZ9anMYLrlZOZb42G8eXMEairZpYbECAjLhqZCCgMMk4X8xa5RsjJIoyowhyxYg9uR3Qd3+se7rV8mrMuOIyKphNZHUq61uJ2SynmqEVt1764kE7ytudopMPM+F681va7akZ/MC+pLBPdQ5fQHyNxcOTGvlmILGagGuqgTZA1vFAPs1L2m+f8v9ll/34WoilU2Fgp70PSGnbdAnWm/UzHxUtl3ThpSpjZHGEKbX4WE0Med0NcktoefBFRVPCQ/WSRsrOB7j94S3xWSuqjZ2BHbdDTlnOCa4uo76QjQn9n6Q4IPaB4CvuH98TISpFi1AvcnXggQcGt1Ncwr8bIWoOayMf9HSInsQ3u9EeOIgqGkZRQefvzmcOrE4Dw3xM9huYe4XG3AHgbGr6wIlcmx1MtPAGzY6jkRHjSfyYVBNLpaTOyqcvsB17O00pyu1w2s5Gz2SJOFWfYu7cjRLm8naIxSOsfzuuiLkVyk+UGzRlnO71XpEGg28kOBWIRf4q3yQdSdOjMNOki/EmQK1tsmjWfCUCE Yonh3+XT L58SW+TqNGCSS7Zl4zjtyoDk5yJsTETWEeHIY2cDmOmc3p7HPCzpGThuTl+heG4UZEN0P6AgY6rs5pzObTwYEEP3hhhqsrADKBOtVVpDIQ+7KdwvT/HAKboEqkDi2/LhYBqH9M7uP+ujAng5lso11v3QtJ31cpi7ifjIe5UORFMNEvK4lCmy+CtjTs0xujN6Ih4CkThIFfpKUGTHva9n7c4guXtDEd7xIe0AqiRAHBpgGPcUACCA8xi8+4HZYwLjyngBHr5X+MCe4NiYC9Zd01BWIwQp0DTNjoHAE8c3hW/pS0YU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 and perf. To enable this feature, build your kernel with CONFIG_USER_EVENTS=y. Programs can view status of the events via -/sys/kernel/debug/tracing/user_events_status and can both register and write -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. -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. 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 registration -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. The -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 back +a write index which describes the data when a write() or writev() is called +on the /sys/kernel/tracing/user_events_data file. 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 above.* 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. This command takes a packed struct user_reg as an argument:: 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). -The struct user_reg requires two inputs, the first is the size of the structure -to ensure forward and backward compatibility. The second is the command string -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 ensure + support for lower versions of the kernel. If a flag is not supported -EINVAL + is returned. + ++ enable_addr: The address of the value to use to reflect event status. This + must be naturally aligned and write accessible within the user program. + ++ name_args: The name and arguments to describe the event, see command format + for details. + +Upon successful registration the following is set. + ++ write_index: The index to use for this file descriptor that represents this + 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 details. 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]/enable +events need to use /sys/kernel/tracing/events/user_events/[name]/enable or perf record -e user_events:[name] when attaching/recording. -**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 should +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 isolation. Command Format ^^^^^^^^^^^^^^ @@ -94,9 +134,9 @@ Would be represented by the following field:: struct mytype myname 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. 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 references 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. -Status ------- -When tools attach/record user based events the status of the event is updated -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 either -using a byte-wise index with a mask or a long-wise index with a little-endian -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 file. +The command to issue is DIAG_IOCSUNREG. This is different than deleting, where +deleting actually removes the event from the system. Unregistering simply tells +the kernel your process is no longer interested in updates to the event. -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:: -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 with 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; -A byte-wise index check is performed as follows:: + /* Input: Bit to unregister */ + __u8 disable_bit; - int index, mask; - char *status_page; + /* Input: Reserved, set to 0 */ + __u8 __reserved; - index = status_bit / 8; - mask = 1 << (status_bit % 8); - - ... + /* Input: Reserved, set to 0 */ + __u16 __reserved2; - if (status_page[index] & mask) { - /* Enabled */ - } + /* Input: Address to unregister */ + __u64 disable_addr; + } __attribute__((__packed__)); -A long-wise index check is performed as follows:: +The struct user_unreg requires all the above inputs to be set appropriately. - #include - #include ++ size: This must be set to sizeof(struct user_unreg). - #if __BITS_PER_LONG == 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). - long index, mask, *status_page; ++ disable_addr: This must be set to the address to disable (same address that was + previously registered via enable_addr). - index = status_bit / __BITS_PER_LONG; - mask = 1L << (status_bit % __BITS_PER_LONG); - mask = endian_swap(mask); +**NOTE:** Events are automatically unregistered when execve() is invoked. During +fork() the registered events will be retained and must be unregistered manually +in each process if wanted. - ... +Status +------ +When tools attach/record user based events the status of the event is updated +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. - 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. Administrators can easily check the status of all registered events by reading the user_events_status file directly via a terminal. The output is as follows:: - Byte:Name [# Comments] + Name [# Comments] ... Active: ActiveCount Busy: BusyCount - Max: MaxCount For example, on a system that has a single event the output looks like this:: - 1:test + test Active: 1 Busy: 0 - Max: 32768 If a user enables the user event via ftrace, the output would change to this:: - 1:test # Used by ftrace + test # Used by ftrace Active: 1 Busy: 1 - Max: 32768 - -**NOTE:** *A status bit of 0 will never be returned. This allows user programs -to have a bit that can be used on error cases.* Writing Data ------------ @@ -217,7 +244,7 @@ For example, if I have a struct like this:: int src; int dst; int flags; - }; + } __attribute__((__packed__)); It's advised for user programs to do the following:: From patchwork Tue Mar 28 23:52:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191677 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 448BCC761A6 for ; Tue, 28 Mar 2023 23:52:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA62B900003; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D0451900007; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B182900006; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 66772900005 for ; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3814C140B20 for ; Tue, 28 Mar 2023 23:52:31 +0000 (UTC) X-FDA: 80619958902.07.DA79FB2 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf28.hostedemail.com (Postfix) with ESMTP id 6377CC000D for ; Tue, 28 Mar 2023 23:52:29 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=lnDSw7kX; spf=pass (imf28.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047549; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GLwBDStl/9V7AvgFeNpxkZhcBlWV4hLj39iIaU1+mbw=; b=77+4K1YdsydZ4o8xM+vajlLpZ2BhlcnesC0JiuZ1i6UQ71NfV7ojc2GbOjdeLHtd+rdd+C 3JxE2VP1Z2aq+Nrwp1tOfoaaEgo7uWsb7v7wijgsH0aBS1YpdSXg2lTaeuphXuQox1gght 6cjnbY3jlxdMUes/GEPowZsnWzvneJA= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=lnDSw7kX; spf=pass (imf28.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047549; a=rsa-sha256; cv=none; b=rGx49BSYFVAUJxeREtWdpB42vsJv4m3Poe4hHi/x9Aop/bqUeq6dhpTmgMNzd9n/eHTTvG qd+vgu7CH3ejuY3Ot0EW56g0mY0HA1Jlv0OLZMJ223gK2qycERW+hB8dCNiy5Xd4hwfHF3 22Au35mCXnh3zRk+ywtg6zMoHEl+f20= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 4C5F720FDC30; Tue, 28 Mar 2023 16:52:28 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4C5F720FDC30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047548; bh=GLwBDStl/9V7AvgFeNpxkZhcBlWV4hLj39iIaU1+mbw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lnDSw7kXl190S16SZ7QnjEeUWDeHKekuhDoSRnQdy0JADJ4C+EbHZWuBRIV+1REf9 kmaJYUE4Re0TJ7uYfzHBYqpzmFv1uu0IIHJDX/8wyApO9YfoSjrVSY1a/5EGpJURIc eyvkQqfYUHu7M4obR4ElQLCSixC9mOlhEv/wyPiw= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 10/12] tracing/user_events: Charge event allocs to cgroups Date: Tue, 28 Mar 2023 16:52:17 -0700 Message-Id: <20230328235219.203-11-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6377CC000D X-Stat-Signature: wkres5pfi6xwy1e1ghkh3s4iwbjhuew8 X-HE-Tag: 1680047549-378092 X-HE-Meta: U2FsdGVkX18MPtiOjS9oWA3ILjOo5d61DSqVV7qVB+hM5a0tBQEmt+qC9KonAEhytyVWHszWl4H7JIVKS32OZozjSk6+7VUGDg8OOpE4ZUPqquPv4yr+ZJS+9r179cvmff7BOh6Qign6CWCEVyoGnp7OawvuXCRfhfUpjkY7ev45uXjDiGdZJq2KjJPAtogYK02uU6FR0iImcibd9VfjL2lccHYYdqz3Qq1sTx70NamXGxIK2PR6hbSqeWdDnDicz6BDHPwvcPbWnq/korq6Y35ndLDcmt8BxRDnE0Ex7hl8LFbkuxKWNrrzdvNRKUct+yBeXPi3br8lPv16lv42oDdhmXPJXURi2rzIG9epxv6Z/SIietYJq/E3VQlXIEukNrHFZ4CVNW/7iUdJARRhqOkLRNPgKsw/mvEdof2Clh20PrXVyT9ZgWr41TzDcNR2wuxj3MeS+HVALJDrqD3WBcnOuwuVih8iyTVGyYC4OsqOBDNuoP/aLHOrMpw5qEebpN/5Zkyqd6oe5DRot933SWPUeEYijWL40uB8JJibssUL/mO7y72iHAfWK08quyzhoAJZYXWv4p/+2t15BM99xcGf1BKmnVFr7gcqWp/T6Krv/b9W4erGcxgodcwe2Sib++X9SCrBqIJf4AJAplauJDBaoofa2GlmRmw5R/T0Za7dU3HJrn23Z/nWLls7sxH2f5bOd+W3/WMDIwYABDBGVydW9C1XnMyz8GM2/6SHXGIwd8ZxJGhVHUN+wIXNNi3Y8GDFjgxY/CTnJl6peOy7irSLca8zn9OT6bCj+CbDs+dyKVX+GyCn0jMUjP4qOrYZoXnzi/Dioy3KASLL6IP+FP+IfLVu/N4kUe5A21+IpR4dWu1fb1gSV+08N2x6SRZYMOOt6+xfkd3DRVwNZUQisyCAAzKctCHIo9yQz6v6Kcd1GLNjjdyTa2XezVwrq83cU7Pm0kx9rd8EK3TZULH dF64XSpd zrkczgXEzFiY9sm3nXmraw53hMzBDaUKtrhrOybJu4OJ5b21cUBVrRGy9ACLORQ6iwOd/1nJ2JzK8s74YKdVde6aT3dg4n7yBwW5fmGZ/bzVnQ3iV7Rv2X+h/49pu1TR8YOi8T2A3ER6sji4UU2SqOr1V2aE2DRMyei+ed5iV1FxTNwHDMrIP+FY4s2ne2B6gzIKnT2b+dg06fAr6P/+zbh9Eg56Io8CnbvUpC9u7jaWgYhgtJxrj6+ozmKm0RkSPOGZYyjlElctiXtExm0vs49AfmcbiD9HY2A0WS3Z/IhE0tZc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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_user.c index f88bab3f1fe1..3a01c2df4a90 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_enabler *orig, if (unlikely(test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(orig)))) return true; - enabler = kzalloc(sizeof(*enabler), GFP_NOWAIT); + enabler = kzalloc(sizeof(*enabler), GFP_NOWAIT | __GFP_ACCOUNT); if (!enabler) return false; @@ -502,7 +502,7 @@ static struct user_event_mm *user_event_mm_create(struct task_struct *t) struct user_event_mm *user_mm; unsigned long flags; - user_mm = kzalloc(sizeof(*user_mm), GFP_KERNEL); + user_mm = kzalloc(sizeof(*user_mm), GFP_KERNEL_ACCOUNT); if (!user_mm) return NULL; @@ -662,7 +662,7 @@ static struct user_event_enabler if (!user_mm) return NULL; - enabler = kzalloc(sizeof(*enabler), GFP_KERNEL); + enabler = kzalloc(sizeof(*enabler), GFP_KERNEL_ACCOUNT); 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 = 0; - field = kmalloc(sizeof(*field), GFP_KERNEL); + field = kmalloc(sizeof(*field), GFP_KERNEL_ACCOUNT); 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") != NULL) validator_flags |= VALIDATOR_ENSURE_NULL; - validator = kmalloc(sizeof(*validator), GFP_KERNEL); + validator = kmalloc(sizeof(*validator), GFP_KERNEL_ACCOUNT); if (!validator) { kfree(field); @@ -1175,7 +1175,7 @@ static int user_event_create_print_fmt(struct user_event *user) len = user_event_set_print_fmt(user, NULL, 0); - print_fmt = kmalloc(len, GFP_KERNEL); + print_fmt = kmalloc(len, GFP_KERNEL_ACCOUNT); if (!print_fmt) return -ENOMEM; @@ -1508,7 +1508,7 @@ static int user_event_create(const char *raw_command) raw_command += USER_EVENTS_PREFIX_LEN; raw_command = skip_spaces(raw_command); - name = kstrdup(raw_command, GFP_KERNEL); + name = kstrdup(raw_command, GFP_KERNEL_ACCOUNT); if (!name) return -ENOMEM; @@ -1704,7 +1704,7 @@ static int user_event_parse(struct user_event_group *group, char *name, return 0; } - user = kzalloc(sizeof(*user), GFP_KERNEL); + user = kzalloc(sizeof(*user), GFP_KERNEL_ACCOUNT); if (!user) return -ENOMEM; @@ -1874,7 +1874,7 @@ static int user_events_open(struct inode *node, struct file *file) if (!group) return -ENOENT; - info = kzalloc(sizeof(*info), GFP_KERNEL); + info = kzalloc(sizeof(*info), GFP_KERNEL_ACCOUNT); if (!info) return -ENOMEM; @@ -1927,7 +1927,7 @@ static int user_events_ref_add(struct user_event_file_info *info, size = struct_size(refs, events, count + 1); - new_refs = kzalloc(size, GFP_KERNEL); + new_refs = kzalloc(size, GFP_KERNEL_ACCOUNT); if (!new_refs) return -ENOMEM; From patchwork Tue Mar 28 23:52:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191678 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8220AC77B6C for ; Tue, 28 Mar 2023 23:52:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 385F7900008; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E65B900007; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B022900006; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D9A95900008 for ; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A5E9DAC0A2 for ; Tue, 28 Mar 2023 23:52:31 +0000 (UTC) X-FDA: 80619958902.30.39871B3 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf30.hostedemail.com (Postfix) with ESMTP id E20368000A for ; Tue, 28 Mar 2023 23:52:29 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=nOsuB16y; spf=pass (imf30.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047550; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xpyxft+n8GA+tebj3uBF7xj0cPJnmjMw5o/uJtPt8qA=; b=U3tiLDoQ4XkJzkDCQbV19BIv8GsAw7RWiJrGWsFDsNzckWRD2PSL3Jfg9nNZWsXg1I3Bxq 4oi9Rdt1JdaPQbeHxBLuR0/D0i6zs5JbzKIxpx0MN2sLfRC+3m67HEDztTI8YxzLc+1rR+ UzJT3Oc3EwftKIviKP6OIONBg22Tjnk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=nOsuB16y; spf=pass (imf30.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047550; a=rsa-sha256; cv=none; b=xSvQy/mVLIYxj8oUfg1QSPZU/XJFlZYufbDs91isKktQjnOMRfuRIo7i7JFwJCGv2I3zl9 gLUWmIWlRWBjsHoVY1XFwXOp0gDg72wiv6ZzjfJoQBjeVHSA25n56on8nDVAakC6+DWYUW /m2wyyEHUniY35rk/WI3OGQIoviEnXQ= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id A837820FDC35; Tue, 28 Mar 2023 16:52:28 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A837820FDC35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047549; bh=xpyxft+n8GA+tebj3uBF7xj0cPJnmjMw5o/uJtPt8qA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nOsuB16ym00tQHByywSl/4cvENvbYlSRWVVQdjexGw1KSt+RtISzhMYb2LmJAXocp LeAN/1oWA+q+uZamq1fJLiYZ6kOS72qsDslNCh/Ujjkz+zL7rTEGfKHfu6KOC3K6oo lRBzqKrWX9gQ3BSDOOf8E6jmxB1pur/oOrAZewng= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 11/12] tracing/user_events: Limit global user_event count Date: Tue, 28 Mar 2023 16:52:18 -0700 Message-Id: <20230328235219.203-12-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: 8b6sogrnxqb5onq4qinwnbmwzwkmns87 X-Rspam-User: X-Rspamd-Queue-Id: E20368000A X-Rspamd-Server: rspam06 X-HE-Tag: 1680047549-264213 X-HE-Meta: U2FsdGVkX19DUFPLEezCHzPHx5ZuLYCye124EnuXnpWwwJMQuCk5Grf/UXWYwJUwP6hRZmX1zNFHbm5C0AO5gOkngHP0EfPT/RBXHJh26EUnOUPhSZL/kta807z417sXDxv+hOxyOu21vs0u/lFXJQ7WTMfTbLehfPCd1Q2uJhD/h9jwFjEnsiHaEbCBYtrRqm7LRJywBElFh2+giMbX9+J9+za4ldUa+ul9KKN/3cUnX305Y3rsrBJqA4SWXrc8asyGGdSrwBUE+9+T2DFmNh9opHpVQFuobu7yyDdT3pQAJdBoUYFaP/HgLVCRPZRZPyLydqTBZK1MoHRtuyPqBpb90CuU5+bKjGa4ax0Duhc6aaY6VcWSMrlsYI0Zk/VnupP6S/c9fUJky0Tbzv/V5wSu5QwjixdIY+Un4Y9GTKgB5jir9nmu0JNEirHM7zECF9CndoE8AaBLyEqywZg3/jN3Cf+OOrZMMx/MxKBX+SQMxkRXUeVwlI8xr6OYMxWDpq1CgsPMDO+vKbarBBnn9obGP4PAFQtuUiB0XeNudgTm3+UHs7G/Pw/Vaonilbma+Xjo2XJaUMqZ0gqLX0SNqxquEmTpf3q4mjnKbMyHC/vwbOSQ1i5emg2X6u7m/Pc9mHXPwx3S9p1sIlTLxrdONOB9eD4hCVaQKI6vMQyeIDb/BRnC8XYhX1PYgA5oaNt5hRodve+30/WKH9g0/1yxfKfCRjYVyiS0x+7d4DUgCXtyU3SMJVlpnyIWsFaeocmFs6Es+xP9H1FcDZJtdLuNIa0s3oF70hzJ3HiJZatflFp+FxOR3ha1mmH5xhtRmNUqTM99CamboWOFE2p0LUpGmb8JN39O/zJbJGSDDSOOrqtAY64GfSRW1+TQjtxM7M/uq9vjQvAU7tQ0+DHeo5AbCwiuACRbdfACTFYXbPNYy93qSFIZSGsBSIUHg8K62iew4A50Knl3Q6BEBjLW1Ut wtVD2gMO /H5zHEMs/q7XfIw5gRjaH7/nH1Ye6wo/rCY482zp/PE/lo940cuMc6a67+HKhyS5gTWXrAEV3MSFEowpp/wI/2PKdlI1y4vHRm+pvoq8OGxQMox8LQ+yj6cZQOK5IL2j9U2tTFNgAbqfkODAYrxvZN9wfeX4LdFsWQPObtFpLyX/P7/gaLFW4kjDTk2FzwWeA3zJ7KlMfOy+gKMxk4fjOSVANwAafw2ZPNt8nv9izfk5kilnnAzijf33FJjE4iNX2oU44aiuwfE6VpqJ9lLoCCxkTmL/wcc2wrGW7CS+1PNmEuB2Y1wpt8Tcv9b0hqD3obKxSyV6dEyMaCwPsspNk171Jnw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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_user.c index 3a01c2df4a90..9b43a02e1597 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; +/* Max allowed events for the whole system */ +static unsigned int max_user_events = 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 = 0; + lockdep_assert_held(&event_mutex); + /* Must destroy fields before call removal */ user_event_destroy_fields(user); @@ -1257,6 +1266,11 @@ static int destroy_user_event(struct user_event *user) kfree(EVENT_NAME(user)); kfree(user); + if (current_user_events > 0) + current_user_events--; + else + pr_alert("BUG: Bad current_user_events\n"); + return ret; } @@ -1744,6 +1758,11 @@ static int user_event_parse(struct user_event_group *group, char *name, mutex_lock(&event_mutex); + if (current_user_events >= max_user_events) { + ret = -EMFILE; + goto put_user_lock; + } + ret = user_event_trace_register(user); 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++; mutex_unlock(&event_mutex); @@ -2390,6 +2410,31 @@ static int create_user_tracefs(void) return -ENODEV; } +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 = proc_douintvec(table, write, buffer, lenp, ppos); + + mutex_unlock(&event_mutex); + + return ret; +} + +static struct ctl_table user_event_sysctls[] = { + { + .procname = "user_events_max", + .data = &max_user_events, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = set_max_user_events_sysctl, + }, + {} +}; + static int __init trace_events_user_init(void) { int ret; @@ -2419,6 +2464,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"); + register_sysctl_init("kernel", user_event_sysctls); + return 0; } From patchwork Tue Mar 28 23:52:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191679 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B16E1C76196 for ; Tue, 28 Mar 2023 23:52:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 72E5B900007; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 70540900006; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5AFDA900007; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 347B6900006 for ; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 11A62A0483 for ; Tue, 28 Mar 2023 23:52:32 +0000 (UTC) X-FDA: 80619958944.28.34E0C76 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf07.hostedemail.com (Postfix) with ESMTP id 5B42F40009 for ; Tue, 28 Mar 2023 23:52:30 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=Jqyl4KPw; spf=pass (imf07.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047550; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MYYDTYE8Up+wGqx+f1YsuHPS1P/30RHQ/hjmHTBgUyY=; b=4xdnkwuq2HPQsKulnL9/euOk01ax0uBmCzE6zyDSCQdzG++GQ84B99lhsnBsX2yeU70o9l aDDVMgVqP+YeP73bBm2MrA+Ot87isSj9n3Y4F+pyDTFbrenVXqWIzwCrKbNYAxYbnwnb8Y H5QR3ph1yuWT1ZE5d+3iZSZY37KjKmc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=Jqyl4KPw; spf=pass (imf07.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047550; a=rsa-sha256; cv=none; b=K2lSE7pscd3J3mRELEpBcq4Inm92pZniziJNKQzJnZamwbq/izIM26U6maYd4Xr6rd/AK1 rzTDfrWtOI4GDd6vWidMBUGjfrXY3aVL5dibARMNMc+hYV7o5eFZVGsg+EMcFAt5SOuTv9 IsWXIzd8hMZMpx47vUjM0tYTL+UbU5M= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 1B87320FDC3A; Tue, 28 Mar 2023 16:52:29 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 1B87320FDC3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047549; bh=MYYDTYE8Up+wGqx+f1YsuHPS1P/30RHQ/hjmHTBgUyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Jqyl4KPw+E+tV+T5GSisoQ5Rmyf37Cjz/I/bQERDIT2eym1qrzjyLDap9dlmJASTX A9VuVoCBgoqlVRQYTkaSH3yKyXSUBt8Uo4fYLa/UeSoopA5O3npO1Ucx2L/wG+nl0X v2RTgH45YmKPVqpwAhVUJYnsMKK7rdsu5GC8WwnU= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 12/12] tracing/user_events: Align structs with tabs for readability Date: Tue, 28 Mar 2023 16:52:19 -0700 Message-Id: <20230328235219.203-13-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 5B42F40009 X-Stat-Signature: hymycify15h73px3xz357zwzb83zf167 X-HE-Tag: 1680047550-285523 X-HE-Meta: U2FsdGVkX197AOZGm2Sl20lzMpVmUeHqaOjJmxZ5BT/lcTSp5jdSW9r0h+FvNWWM7AlrcbL2AwtHMGoUthzkc/U3ns35uWR7vLdxv1sAtUNB3eDYB4zUcbDDAPIaTuat5M+oDyhJEYTosGXs18ftCQ4OrvLePm68lzbz3RRGjC46ZwXg09sjfveNxbG5g+sPOpC5wDl8dGp4MnxgpeEH52bK/DJwQZu6hpdrr8LatR94WzKtGUieTBnnm5i0vERHdkNiy9x62X8sieQF6YC8ibPLCVwxqsWXbSB4RB5j8rpO8yJ/jH2xU8d+lL9DD0wxT1mmEkFdVt3hSEqFhQcpoA6T90UjW6rP4vxBgR6B42GqrdyBC8Fp6B04KF7NVRIbnKn8za2bWOHjfzUoW06eC49dS1US/3H0KTOrUvmis+jNKsjHUHkIVczCTzXPl42bCHqX3TupEBC3Aej3zj2xSlQK1HGGqahUvdF/4mYJ08YuuvuawZ7t+ztbUlXkkRbIRFbQeASxuP6PKwYB6Lv56D54KdQMnXUo0fSoglpylrLZYXatiQ5f+de7/CfQIcMmAQQmk8QbtZWmgbWKQgnB1TXCdsfyyG+OMjVfaC5z31iHdWk47H2wRVQEosUz2UvOzkADBm8T6ZvJrUI+S8M26Ss+bqVjxAyzxwtPRbxi7JMqqem5iQtL1BThfjb/XdTD/Y42Q5Jofkgu56/MAQBeJnErfvq3moqdXl/Fo1ejh7lSDDEN3ouAFjXd/zcmdikvqcvFkAP6LHTG9dqSd/LNr9/3wJLB2S4Bupih6ig8wgWFeeLUpB/yZeOUJj04Lz/CievU4plt9FB8kgIeTTkFvfDhR9wcrs+xrRr3nhJJj+cbnfRBdmsYn4L9LFFxOU8d/AC/Wi2u2XRdT8jpjkJRuPw57d/IGgbkgbr7uGUAdwuk6ow/ZSq8U54X1Fr8l3Iaj6uc8WjBR5das0lTSUp adODyQoG qgoibOP43JVcfirUwv4KNq4T7vinTqySS/nL6ep6tauhktpgM7BlN3icNjUiwow7pTfeRWM5JtEvCP7d3xmm5Hlic0i2tmyqhtMybVbmp2DWEAeyXKMvRl1ASkFGYpAHP2JTdCmOVJ4FPEpeFU3ciXK6oKo4ktZvsFHbfJQ9kjccFfkhTBabNLgxcPoMKz8Vl7bJB+HacLeI/QvXGt0hwitbrJPvCgn6HbvE3Dbus2cWsYtlIfBN8+r7QO+Mg5j8UE4qOgHsL+0K9VXRYkjtyRKWmddQ+qs55MQaDpwNNEBt66Tk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add tabs to make struct members easier to read and unify the style of the code. Signed-off-by: Beau Belgrave --- include/linux/user_events.h | 14 +++--- include/uapi/linux/user_events.h | 24 +++++----- kernel/trace/trace_events_user.c | 82 ++++++++++++++++---------------- 3 files changed, 60 insertions(+), 60 deletions(-) diff --git a/include/linux/user_events.h b/include/linux/user_events.h index 0120b3dd5b03..2847f5a18a86 100644 --- a/include/linux/user_events.h +++ b/include/linux/user_events.h @@ -17,13 +17,13 @@ #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; + 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; }; extern void user_event_mm_dup(struct task_struct *t, diff --git a/include/uapi/linux/user_events.h b/include/uapi/linux/user_events.h index 3e7275e3234a..2984aae4a2b4 100644 --- a/include/uapi/linux/user_events.h +++ b/include/uapi/linux/user_events.h @@ -25,25 +25,25 @@ struct user_reg { /* Input: Size of the user_reg structure being used */ - __u32 size; + __u32 size; /* Input: Bit in enable address to use */ - __u8 enable_bit; + __u8 enable_bit; /* Input: Enable size in bytes at address */ - __u8 enable_size; + __u8 enable_size; /* Input: Flags for future use, set to 0 */ - __u16 flags; + __u16 flags; /* Input: Address to update when enabled */ - __u64 enable_addr; + __u64 enable_addr; /* Input: Pointer to string with event name, description and flags */ - __u64 name_args; + __u64 name_args; /* Output: Index of the event to use when writing data */ - __u32 write_index; + __u32 write_index; } __attribute__((__packed__)); /* @@ -52,19 +52,19 @@ struct user_reg { */ struct user_unreg { /* Input: Size of the user_unreg structure being used */ - __u32 size; + __u32 size; /* Input: Bit to unregister */ - __u8 disable_bit; + __u8 disable_bit; /* Input: Reserved, set to 0 */ - __u8 __reserved; + __u8 __reserved; /* Input: Reserved, set to 0 */ - __u16 __reserved2; + __u16 __reserved2; /* Input: Address to unregister */ - __u64 disable_addr; + __u64 disable_addr; } __attribute__((__packed__)); #define DIAG_IOC_MAGIC '*' diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index 9b43a02e1597..67cb7b53caf6 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -53,9 +53,9 @@ * allows isolation for events by various means. */ struct user_event_group { - char *system_name; - struct hlist_node node; - struct mutex reg_mutex; + char *system_name; + struct hlist_node node; + struct mutex reg_mutex; DECLARE_HASHTABLE(register_table, 8); }; @@ -76,17 +76,17 @@ static unsigned int current_user_events; * refcnt reaches one. */ struct user_event { - struct user_event_group *group; - struct tracepoint tracepoint; - struct trace_event_call call; - struct trace_event_class class; - struct dyn_event devent; - struct hlist_node node; - struct list_head fields; - struct list_head validators; - refcount_t refcnt; - int min_size; - char status; + struct user_event_group *group; + struct tracepoint tracepoint; + struct trace_event_call call; + struct trace_event_class class; + struct dyn_event devent; + struct hlist_node node; + struct list_head fields; + struct list_head validators; + refcount_t refcnt; + int min_size; + char status; }; /* @@ -95,12 +95,12 @@ struct user_event { * 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; + struct list_head link; + struct user_event *event; + unsigned long addr; /* Track enable bit, flags, etc. Aligned for bitops. */ - unsigned int values; + unsigned int values; }; /* Bits 0-5 are for the bit to update upon enable/disable (0-63 allowed) */ @@ -119,9 +119,9 @@ struct user_event_enabler { /* 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; + struct work_struct work; + struct user_event_mm *mm; + struct user_event_enabler *enabler; }; static struct kmem_cache *fault_cache; @@ -137,23 +137,23 @@ static DEFINE_SPINLOCK(user_event_mms_lock); * These are not shared and only accessible by the file that created it. */ struct user_event_refs { - struct rcu_head rcu; - int count; - struct user_event *events[]; + struct rcu_head rcu; + int count; + struct user_event *events[]; }; struct user_event_file_info { - struct user_event_group *group; - struct user_event_refs *refs; + struct user_event_group *group; + struct user_event_refs *refs; }; #define VALIDATOR_ENSURE_NULL (1 << 0) #define VALIDATOR_REL (1 << 1) struct user_event_validator { - struct list_head link; - int offset; - int flags; + struct list_head link; + int offset; + int flags; }; typedef void (*user_event_func_t) (struct user_event *user, struct iov_iter *i, @@ -2276,11 +2276,11 @@ static int user_events_release(struct inode *node, struct file *file) } static const struct file_operations user_data_fops = { - .open = user_events_open, - .write = user_events_write, - .write_iter = user_events_write_iter, + .open = user_events_open, + .write = user_events_write, + .write_iter = user_events_write_iter, .unlocked_ioctl = user_events_ioctl, - .release = user_events_release, + .release = user_events_release, }; static void *user_seq_start(struct seq_file *m, loff_t *pos) @@ -2346,10 +2346,10 @@ static int user_seq_show(struct seq_file *m, void *p) } static const struct seq_operations user_seq_ops = { - .start = user_seq_start, - .next = user_seq_next, - .stop = user_seq_stop, - .show = user_seq_show, + .start = user_seq_start, + .next = user_seq_next, + .stop = user_seq_stop, + .show = user_seq_show, }; static int user_status_open(struct inode *node, struct file *file) @@ -2375,10 +2375,10 @@ static int user_status_open(struct inode *node, struct file *file) } static const struct file_operations user_status_fops = { - .open = user_status_open, - .read = seq_read, - .llseek = seq_lseek, - .release = seq_release, + .open = user_status_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, }; /*