From patchwork Thu Mar 23 19:06:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186000 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 E165AC76196 for ; Thu, 23 Mar 2023 19:06:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D6DE6B0078; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 88F616B007E; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 68ABC6B007B; Thu, 23 Mar 2023 15:06:19 -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 547386B0075 for ; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2D403C07BB for ; Thu, 23 Mar 2023 19:06:19 +0000 (UTC) X-FDA: 80601093678.29.E35476F Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf28.hostedemail.com (Postfix) with ESMTP id 62F27C001C for ; Thu, 23 Mar 2023 19:06:16 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=jaYGG4ug; 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=1679598376; 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=3GD3EJrLDd61BRUHYND8gYOabKVhrOAN+Zb828ClMiUqZw+DL3JSKh7YdWQREObIxNCnZt Dthoh6J8alWM0Zjycgmy01zC4EACX8tRq7bu7ELWiVHtUrhLqN5HRw2k0W5JOMXyEq681v XoquHvTME+K2W/vuhAGa3nCUxPdq+FI= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=jaYGG4ug; 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=1679598376; a=rsa-sha256; cv=none; b=0y/ARYwAC08d9WZvklpP53jN+2vQ2Z9PW+ZXI99VOWuZlG05ecnJ7vbrGUUPCE6EJMQU1i NusrZu3Z6jXCCCPholYeFOfUY1dx7FMfZrW2o7G06+LBqH2P9Bqq7uGVj4ANaHcvWV9zE/ /6FD342qI/mNxlo067uJtn0kEvyNMYQ= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id D122F20FC0E8; Thu, 23 Mar 2023 12:06:14 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D122F20FC0E8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598375; bh=OSmDP26J8YzoMhJc3UKK7slznwSGVE3KNwggNF1IdJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jaYGG4ugjMVVO4MX7alWaNGDAMI91nFHXJ5k/Q0X58o4L6YtUg810AL+vFiWmibas M263Z25IAH5rp/rDqBuaLHMPJdf3JY6xBtR/+SXdSDxyh2gZZuRrZsMu0+QQyRGdbY H/LunFHwqJyGl6+oj1Z3xaPy0h8onfGLTFLAlA+U= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 01/11] tracing/user_events: Split header into uapi and kernel Date: Thu, 23 Mar 2023 12:06:00 -0700 Message-Id: <20230323190610.251-2-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: 3c4hjgka9qn4a3mjwu6ri3dhbrdyemmb X-Rspam-User: X-Rspamd-Queue-Id: 62F27C001C X-Rspamd-Server: rspam06 X-HE-Tag: 1679598376-278092 X-HE-Meta: U2FsdGVkX1+LUXYwlH9h98NtQOWe2+JMjv4cgo/j8OZ+no2rapK26rMrcqwa3maeiOriaPpbwb+vRpE7FjrnhKdUMYZnOjkFlVL/Ma1uxPaLF1xNJfkuH5dZ/jQxINSwFITG5Hge44d51eyhHIgmJ0AmoSkO96GN1jZB2gFJNEnOeLzZ9e0o5RSfI3TKf+Fve8vyobLnJxGUwngP70QY05hn95/lVSK07Aqqx5xXmZKCndJyIG7oWmyZeE0H4hopCMbXmVzvVaFlFVMbFF5nwgxjqVfrHzRYjzta/3WWcAj3ER/ECokpxSwERinMbW4bOlwL8aFU2ytFd4X86IGOxMZ9pAiAiuMrT+xQaqhs0ZoiA2fcZboVm1tbSb/qqOJRnSeAJ7gEluu0+HlLG4Ja6tX3WrcNm1GIVn1+bKOZmVgKetu9a7tB2YyJKALw2JKXFeRAGGH2wgCx6ik84MbTwGI6n3LqWvWjFaWhkopvxO3isskRBuAZnxnfNOJpLbGXghzPzk0jayyaqbMJGZkqzvdsAOKSOmSiuSBvtceUgwc/nvt7UxoGcO5nD21S+dGPpfK46lhpv2mh87ewC0rLEyL74KwWNvh+BHVf7K42TfAIpEZCQne7EDi4NvIh0uXutfKGOqtgJh5KJ5ohhwLbmIo8ZZRgobwKP+P4+ttORBfvZ21+9IjcCrWYD77HMaFKNUZ3jb80ylDDGupR00f+k/WrboG1EsrjgeVBEhPE29CHUsVdX5Bb4IxckAO5gXy6YaNgPNEob66NwAPKl9+jUweqECZdWdzI8SahiLjNzJCa9UAisrRTLuFFWJAtjVn60B40PtIx6A/yiY4wQLJkuAFB0MWOcIbZofCm8ckGcjMQPq4hu6esg9KL/Dv3h2pXZpvFRoHH3SspsQUf80ZLj+xvB/HafFTGfKr1M01uFDygt+tHTpAcLBDtWz7NCupzo2+NtJZ4juB1zZFOtbe MgAGNxpb MEf9TOMB0IF0/S7n8CW0f1ohsycT0xP1UX4W+T5dVx+r4aLz8jRiwoDsHrVGGxqmDU7Smw9k0Ta36iKQO8owziYwu6xpvvVZAny3rhpCvWVAydflPbUkKDP5hZ6WZGNLrpd4SRSD2sKlyzhQZNvoJ0wlRPx6dpX5VxaYOVxIGB/N2GVtJF93bjbyinTMaf05ba23TibqA0TR/MFdbxt1WJGF3zUUZFkxl4pRAK2FQgzbNKmWu9pmrKtzqTJGc9r5yeB5M7FG02qiYEkmf0+66UXz15rG0MA7u3gV6CJs1r2IcrJw= 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 Thu Mar 23 19:06:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13185998 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 F0947C74A5B for ; Thu, 23 Mar 2023 19:06:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0D9F6B0074; Thu, 23 Mar 2023 15:06:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BFEB6B0075; Thu, 23 Mar 2023 15:06:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83C606B007B; Thu, 23 Mar 2023 15:06:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6B0E76B0075 for ; Thu, 23 Mar 2023 15:06:18 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 466EEC07CB for ; Thu, 23 Mar 2023 19:06:18 +0000 (UTC) X-FDA: 80601093636.08.259E638 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf20.hostedemail.com (Postfix) with ESMTP id 73B301C001E for ; Thu, 23 Mar 2023 19:06:16 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=XnLSeKH0; spf=pass (imf20.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=1679598376; 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=WKxtCxoTiac5E4VP1RsPAJoRdBS5rrNDnl5nS2aM4T8TPRbakXc5Di+vDS7GbrKk6quFvZ 5s9nWyD5niYx+YKpOkWhEG1iRRBWa4M25Rommbf35vwE8fl5l9Gz0PmgFV5M/qLAQsBFgj tEeHUzKDt2QFUpPG/A5hKZltGp0S740= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=XnLSeKH0; spf=pass (imf20.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=1679598376; a=rsa-sha256; cv=none; b=fjeEjuV76GSBtoZyHknnm16rpX3BeKsf76GIzW61v1qYxokQKmR8nvtd9XEdGPgVuza6uG 7pnGHVi6GylGYddjM8j2fh/lWRc23AVmlwYUrb0+mg0FTXGKkxBufPbVJoElOdD42sN6uh 6GcusnU4gCdR+dfAKkrBrrUDXM5/ZEg= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 416C920FC0F0; Thu, 23 Mar 2023 12:06:15 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 416C920FC0F0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598375; bh=S8OIOLUkRs5CBqJ2LYBXQxLSyKFNFEQS/E/TUK8EqMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XnLSeKH0mm6cBni98TS1qnpvJMydCDHzFlXP+uwHPBOwrx7c7e1Hlo2DfpDtnETWr k4jNmpso/FRIMfmOxIFLCShwaTuWYXjbgLebyBLV0fqLLdQLX4DinvqGvSyf3rgzES vXaJ9H3GW3BHmPpML/kbpTY4mRA8wefD5tVKtdto= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 02/11] tracing/user_events: Track fork/exec/exit for mm lifetime Date: Thu, 23 Mar 2023 12:06:01 -0700 Message-Id: <20230323190610.251-3-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: d3p5jsd9h6jej5nigr4rwmkr4bqnyegc X-Rspamd-Queue-Id: 73B301C001E X-HE-Tag: 1679598376-810282 X-HE-Meta: U2FsdGVkX19EI3ju5wRCYMwSIgntJnF2rYNXdTV7OB6uhk3TgxPezieMFW6M3KI8hKA2Rwln0ZztV+xE2pMEH2QOIc4yL2Ir2bY8WHgJ8v9fJAxCzstWps6ad1f5pzDOH6zEwVOs1MBhhSnFKv1/0R/PegweFnur+T7GGKGGCpdjmW1UDFVY9w5QU9sogsRPDL8qBCsyoByh8zHvJkY/j9vqQtL1lqhjEy4JLKRJPGWOIKrEOJnziiROMarEKIiRAoMFqWgIqYyxDj38nft0AA9zbTVfxKXyKR95ubA/bvOlsQ/NxgIK9a6QEdZvpMKCJg3fRuYGWsUW31UzWUdxxmcYCLLLmN8Omdu/XzyItWVCtnRBzC2wLDKs4NGxM9SiTTJa+5bolQkZiOyNlSEySJLVTvvQ/3Kvhmo7OFsAougu50jofM1kirnw35eXi964ebiTHiO8fd20h/JBZyt8NbGqWzzHBy0qUH9JSEPHGhT8mdgojzTeW4EdB/I8BlgPVmNf6tD1/adailP5507v0j9Lo/j7P9XJzEbII5N+lJB7KcdJPbV1+WKs9Zy6y1Yn8DomFcp8Rd2IQA21dczdmOjImhHloBjk60t8GgMAsO7jmh3wUaHXgQwLQnysZxDOoumL0JZl7iSF2svyA+2yiJxycMpeGC4ANEZ/hiX50gL+rY6tITacVTOuKBA8oS8nwuz/36T+EnFe4mtNnEbKAZXSRZMBjGig9aVVZMEZi89gnfqPB5t7WmQN33xtRDq4kpu/5IAtf5SjypoSnXCeCvAJiiae0WrgdCLGVTaK8mBqmKVaDpwaEeyIS4VjjparDVWjdVXf20IW95suX7DnooA7XX9YzGT1AWC/Uhn0nMxAjvuAxJkjZen8IMkP0xtcEPMhWQqDPJa6Ni++fORD2oXSkyHlKIKP6GdbyMShcF/lgYWy/lOf7jAPAzCdP2rPlGbwYwVO7KgMmljb7Ip wwkXCGMl /xOGt5urMgBvnapDMeHEOTlK7YUz7alqoAhcKuPjfApuqV6avHZEJJrQnyhdPUBw4N2ssBhyTBmIndDgCA+55yBamE6SsO61iBKEg82tF9pNpyiiHI6OKUQt4rzNXYBJQNO+agF+K5SYKphUp55M7G1ZpMTuJF+V6WelpC83A7NqkPe+ARsWG/3cE3dZFi0169TMatZnUcla8IOASY7Je+5EXn2+7Q/mOyiz+IMHfhX8XLk0eABeh76gdZSWdNRYXEurXOlQFWnnUxkRrp9ev2UNTkjgzYImg3dFOziXqfiTGhXYAE4qqg23ldJNaVy4M14fRzF1fn+oe3KoManZsMavh8q5r0qYkQG1D6+F99Xnp9uM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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 Thu Mar 23 19:06:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186002 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 1530AC761AF for ; Thu, 23 Mar 2023 19:06:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 134496B0075; Thu, 23 Mar 2023 15:06:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D8FFA6B007E; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6F656B0081; Thu, 23 Mar 2023 15:06:19 -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 73A386B0075 for ; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0DE45AB3E3 for ; Thu, 23 Mar 2023 19:06:19 +0000 (UTC) X-FDA: 80601093678.26.129EDD8 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf17.hostedemail.com (Postfix) with ESMTP id 377D140007 for ; Thu, 23 Mar 2023 19:06:17 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=iaVgOoxx; spf=pass (imf17.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=1679598377; 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=eZiU+vrW8sWxuMZGDngRvM0l0W/FRH1Zj0KVG7p+y9ahoY3jdSgDrmW/a9+pNIlSNIiBjk mSOo1K8FGvPQfYnqV9BbzDDYSvRUlB0vWg9365Qx2gNSZ/suSg+cYyiUdvJsCCG0vU6Mmg sqPL5q79QOVWhhc2JoDzkqzBDW2j6WM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=iaVgOoxx; spf=pass (imf17.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=1679598377; a=rsa-sha256; cv=none; b=3BIaa9oPSOqHiUz6OdBipX7hvpBdcw80/5DxjNFbK2IkusFE0V5j7HlcyukBHqvY0YOLI+ fqD+akMRyJ31wrSR24OWQUjZDgUfjF2GNO13QqwfcJQqkbSyjZ7ocLzfWj5KCA2zdprvRv CvUG+Fi4IucFdUuVhrbzoxtp7cpskCM= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 95F2A20FC0F4; Thu, 23 Mar 2023 12:06:15 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 95F2A20FC0F4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598375; bh=R9INv6oij2jvfqxRYN7sTvDkp32JqHmiAHAQNiuLI7o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iaVgOoxxU3nNet5i7P9FgbXF1RE9r2fIuCiQuaRU8XmZ90qijFiINxR72z4ZDr5Mf kbS3BVJguOCo4XWD2H3rtWUeNiXhkodjbZRyQFf/BOqFNsP2rwMjDO28iKh0BjJJLv 6Iinw77lAvp4k98HhrbfDtq1AoGXA1L61pOrZEwU= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 03/11] tracing/user_events: Use remote writes for event enablement Date: Thu, 23 Mar 2023 12:06:02 -0700 Message-Id: <20230323190610.251-4-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 377D140007 X-Stat-Signature: w7cja177zkf4wmzfdyh3unuwc8tw5c43 X-HE-Tag: 1679598377-835375 X-HE-Meta: U2FsdGVkX19fX5t5nL9NgS/mjKWQEJIp7mhJVIZTDdeLssCqtblQsIeikm8mkBSPxKp31nwvB8eDollydvdMdWK73T9kofyqPuWi9t9Ke3zvkV13gqrb3ewh5x6jFh7XUxTO0viuq6pOFYyXk7TiGDpryldDqLkhydlb7KZ92ID17wPqLxVwQU63oDscXzCMuSEQXGZRUezk72e/xsrZiXve5opoCBZXXiua14Gn18O1E+jwKEhJKpsbpbs1C3zHB4Y3MZN6/1jsz32No9nD6qCmLYvbXVJ3xWSM/yVtFeB++EuNBGwWYuT7v14oTExVLfQ5PLqVBOG6HvrDOWEdjA9g60xY3YWY0lAQktnAHDFdWe+4VQ7+6w3AvcqY2puA2YmyQRSJdtCsyyr4OjJTTBv4jagjtl0jpYpb8/qb/9agjllDfKxsgw6o74Gc8lXfgRRjmMexsPlIyFLLbdLA+VnlKxoc99tKMGjVtYOWIucmWqOp5AnY0IoxzwtmWXGXEKejnSbQ9wC24BaLYcKzuQZhrAWfup2g0UAJt8lraYwsoryntYVspTm24OH87Z5ErtSOK1icKc63gmIBFtoOpllsd4N2CbRLQ4vREMsdQ8qmJcce1MIIV5pDot1xjRaO3iGaOPoa7Hr61B0bh22QfI2/x1wZrCuigKzXYlYnmc5QP/Ip5ziLK1tcj+NsL5ZZptbpE2bTBlmcSLppqsykdY1unkfp1Qf/u/tTh4s6yrTO8V41oHcALmGB3ChAn3aQMPbztrVBBrriyg97Q4/ziu8mU8IC9RwMJP4+QoXU6v7izeFIwRclMOx5lecN0VuS3Ofp/VXUadUoPZaQF4YRq86tGn2hv1W7A6ErCGCIwCR0KEW9vJwqhfenYVDc0vos7099fr8IUOJJjdIVRzlVil/7OSQj0t5ZpWAWeD0ozH0WwbokCeszIwpmOsqLFjPkPOJKBZRFnNvJbAzS1Qn JNZpG9iz fAGcXvIzUqevBUh4y4B5jRbSah3nOKaae6xdkS9SDRqDJzDjcCyRbaB/U2FIKNwG9NrgGbBLfEVmOzNCBINZioG4TdZ5sRJCCioVvjvlAu6mapCuFv36Mj/JBhyrQDu5crNzAVXiu0Niv9loJTdH/hRktqridl7Vj2Wq17B1N3DAsHJjIXYnuVYicnu0EzM3/i/P6vJRTfnGQ+h8rYtJGfUYSZK1ybBs5YZCAUOU7bew5u4V8qc7OeskRUk9HSwB4fHuUe7+UGhABWRunRqAsAe7WojQ0Vr7oRQW+ZHS0lJ3LFKYtfn8uYYdDGgR7ECoUa2coGWZ+35JORk86ZmmpkdnHeBByf4PQuE7z77k4nNpoR3Q= 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 Thu Mar 23 19:06:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186001 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 404ADC74A5B for ; Thu, 23 Mar 2023 19:06:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1FF56B007B; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C09176B0075; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 922186B0080; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7FC1E6B007D for ; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 56297A07CC for ; Thu, 23 Mar 2023 19:06:19 +0000 (UTC) X-FDA: 80601093678.26.31E11FC Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf08.hostedemail.com (Postfix) with ESMTP id 61B4F160034 for ; Thu, 23 Mar 2023 19:06:17 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=QZIgN7CH; spf=pass (imf08.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=1679598377; 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=L7mHVFQfl5m9HwXYvdy4o8+UMDB9XT/ZPQxAmmZ1IPVVWwyxTyjNXVLF98hSl6y3WT3mkI /kKUdcG+wz1CiRihn0K0J5zK+kfVEIbNpxH5k2A1AAQlPRCH+bAGoOOOa9SNPq+OHc+Cdm 3xvwA5HIBWrG9LRCR6ZynanO1Sy69G4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=QZIgN7CH; spf=pass (imf08.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=1679598377; a=rsa-sha256; cv=none; b=1XB9E1Ipv0Qibe5ihlxtyY9iSoc1AmidnY/yPYA1w7TiZmVfN+z+TRgjiAaTCFhJCkCvT+ 4u8KhC9IARkO7gxbC2CDhKheX13GQE72ks7T9aheKY8odc+0LO3SDaygHG0pNEmB116V7d 8/R4MEc56/Kfc2WXb3PUw+optEf8fDI= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 05F3920FC0FA; Thu, 23 Mar 2023 12:06:16 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 05F3920FC0FA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598376; bh=LAT6es1zlcxHgyXQ+ccAd1DGKcAvO/9IswEU0C+E8Yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QZIgN7CH8swt7ZObJnHA7Bj18umfbk0i097vthWVnXmjTr+X1av6eksfXuo1P0P93 6yo+alXHSCzqiTPziV9kWyyrxp6PFXlGgbnFpjhDiEyX8YOf4Mb2B5z9g9H2l/6tXg u8atbDgg+XG/qTdLa5JLigkXEeHoKIGW4ksKz3CQ= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 04/11] tracing/user_events: Fixup enable faults asyncly Date: Thu, 23 Mar 2023 12:06:03 -0700 Message-Id: <20230323190610.251-5-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 61B4F160034 X-Stat-Signature: 3rptwdm7oo8zfun39iyx5q67ewuzc13t X-HE-Tag: 1679598377-298762 X-HE-Meta: U2FsdGVkX1+bbbs6CZVpiP0muFHh1JYvox+f58Jf6Mdmg/+XgPObhyFrfSje8klLm20uKG3L5Qzq94WkVWiIMRO+a3klLV6ExuYult+qrFFU2fCx4QauilqtdbRckD/iKSbtKNHlgGZLOqZYIb5fOnyaP1oGuAL9eVFYSMq/4E/qSHvFPXe6w6ssHn/jC/NzzsIDpe48+6O+LHvkDJpBgEmh2KUe4LFbnTN2zh3RMv81cQ8yJ5xjX+t1bvbcYDKYTMlFMMkpQMI7kAxgMReTyUWAsWbkHHRQrhB08Mr4w0UhZEavJyU9A4oN4EAvuDO4mpksn8rzuUzJoEB50RN5sDuJ4EME7SP8CTNsHiVY2gQGVow3yyVpd6z00cB4poQE/NjzR7Ff80yA184dwYZhBP7UWb30x+TxR/VO169MYKBW2PkAuFg0zKhZrpeZc27LumYj+ABBZzBmFg8eyIr6A5x9AvTk9QRL+YsUHxAT5zSLRmkj+JZKmp+3MOwoUEAjmvmE4kgWlO19CRT9LysIUPEej2JPhfc/cBht5WHirrn5PamOicedmR8TcnUOOOUSHhKjvzdvq/tQYJuIMTMQFIYFmjWa5CQOegU7KM7WggU9brxr0SgeKIA5mqnyc0NqfniKU+9F+F7+TXLCABT8RoQ1GYJAlT5m1SE++E/F+NpCNaCJIgc4dbHzs3gOO9+LsdeRxnvqDuH0exPRuCJ2ZfLu8R1tmr8fx3MBck69dinzRPh8bjzzOryyILLzCePM34EwEkRDxu37gGC0ItVeaH1aWh4ndOSjppcnXJcRsecmY4Mp+iRNKh7Yo49YprKJS9gEYWgyaBSjRUGTrYGLBuMby1hPk3kSNABVg10yTOZArnW/PPQivPdQ+fbcPu5fLA74b69ajxPKnPkzusbyf9YbWB1XIppBDosElEla6zYjBjRgEkL3jK7AjBJzAFIrOeU+2liA2qH2DDbQWgZ lHM/rGsC DGMtHMDN2+/W80QeWyTQ+rmC38TKo2jqHzM5pEXBHtqjhOJChKPxEgNS3cnzrV9pPUNqfevra01KUi2EZyvvV7wQIWdhfrO1OAPLxeojONlggLVVToHxNSZsNLvvZpM/Ph6XeKIPfegZg4Yf4SAVTEWCf4ZkFB0P4BSr7lpArhbtAQxL08yzhq2aT1BMTNFTkwPwnRsOZfrDfImWEv2F45kv/YHXIco1/ZocX1XWoUphDF3rGztzkt9tQrHXe4n9HXcChhtUUg4C8DmUC2o58k4/yeuceY/UA0tNKOzJ5BfnEYWk= 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 Thu Mar 23 19:06:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186003 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 4710CC6FD1C for ; Thu, 23 Mar 2023 19:06:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6F3C6B007D; Thu, 23 Mar 2023 15:06:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AF8B76B007E; Thu, 23 Mar 2023 15:06:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AD556B0080; Thu, 23 Mar 2023 15:06:20 -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 703786B007D for ; Thu, 23 Mar 2023 15:06:20 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 49DC4A07D8 for ; Thu, 23 Mar 2023 19:06:20 +0000 (UTC) X-FDA: 80601093720.12.FCB45BD Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf11.hostedemail.com (Postfix) with ESMTP id 90BBF40014 for ; Thu, 23 Mar 2023 19:06:18 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=ZAy6bwU6; spf=pass (imf11.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=1679598378; 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=hPint7oilCioEigkIIslQUbcyG4C0cJ57YmYs/t8wm4=; b=w5DEZknAgPYr8gQbw067CPOBlm1Zhar3Oi4YJYyRucspqp8fbK1V2Iuh8B+DbhTCqHpkFP RSNdeBj6czKx52WqnO5hg0d9LyxeyMEPLC59wfEp83wN4zwGWPk9qQAx6hdR/R85crdlhc AWC8sLEe+hQYEpG+R6BKGkMnwYMI5Ss= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=ZAy6bwU6; spf=pass (imf11.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=1679598378; a=rsa-sha256; cv=none; b=v29lF1EWMWlvO3cfJJF8cYz7GFcXR1gGcvaiOWsJgz8fILNyOaZFirnOH6w2QK+Ab4rIW5 XmsC0zJVv9W20fuj7i4EAT5R2djCtA3A7GYGxhHSltaPVnC+JMfvpXTL8S3GWsnMl/wDhk B31gw9rF+OBeAxZOL3R4LwgizpjPETA= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 5CC6820FC0FE; Thu, 23 Mar 2023 12:06:16 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5CC6820FC0FE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598376; bh=hPint7oilCioEigkIIslQUbcyG4C0cJ57YmYs/t8wm4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZAy6bwU6z8ND9vjLrj7wihLErKBo+N3lmm9I+oh30UiG1ICDQxbVHncDqLEhdC5x/ FBQYs5o6nX1VlqK93g7ROM1FOLKcZK6wUg0xSta4wcdBoab/RA2Iks6qhCXSn3DWFy cDQIp3OMVvEFp3PlEcf4vRxkwlL1JwewPT2obYvI= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 05/11] tracing/user_events: Add ioctl for disabling addresses Date: Thu, 23 Mar 2023 12:06:04 -0700 Message-Id: <20230323190610.251-6-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 90BBF40014 X-Stat-Signature: otihiw3pyuok693h1strs3xrzr8kn7z5 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1679598378-867993 X-HE-Meta: U2FsdGVkX1/kPR47GtZfI4t1FzYuUa5Xa5lkARE5VeuVWfsSoqKEhE/1aL+M+vHuKh0dsjU/FN76GRfHD0I1oTRqQ3jJequMnWD+AbJucLNJWznRZn7jrOunm/3baX2totHx3G2Iusl/q8QvWDhacxzHk+4m/c3EvW1rN4Nt9W9L33jtQBC9yE1503ziM+FN1FT4tAxfgpfliFryVv+Pq70L+QmK0+FAMvvngv2HD68YJcmxHZwnc9WhplodG/PHgpffufD1laVzjbOAN2JLhDFNu4BK3fZFzfrackqYqRgVGiEcmsRoMiJ1n0E27EtBpF0tgik+tYNbCKZ+/5BjgpVqh9oD/n71xHIMuZOLtcKCjhQGAgaFsd/OoMHjZRGi5EKNOAFcBOMDh3yWF2kzEi/1qrWnIRaaWiOQm0ORgTlT1Hy6J4P3KBihLesM339tJrA7cI2Sgz0hUyfM2+GZ3vQdKCdyw95ktL9NOwhfcZQhp2Csr7PuDEWYuMAUvQdKAfNIzVe0D/B+o/Eajn0ojaxnOxOLaBeGlC+DxVUDlABEa45HnNdjcfz474wzkicAoNZ/MVTp3IT281BfclGRZcOGgG764beO3aH8zLEEVngBtoD6vnZV6udCc4WmSwBuDSpS/o3MY94EZjqN9ojR0uWLv45uZ7iCS3+cOejmTnNxY2B7u/iXjM+ijSoYt+Tlsm2liADTi9Gt5DCi64lVW3tuwnoA7mISd+6EE5ST6asX5GceXsaiSa2hfGdpWI5ITRJheD+U3uxA3LN8LOu2PHEVwGCKdPcql3z+Bjioxq12u0Q6YoWBPWLWFj6s8giG8E23aUChx70ZQZRVCAE9hukWqcw1PAoKyHE5H22xjj9LL4HqNMxXG9muou6T78t2TpCVl+Rc0Cgsz8sD+y2mitVfHmR9szoW5+Moy1zsuGedaKj2Urwbm28B/idFCLuH1kkiKJHvbjaVmPx21EW PslFefoU apti4e/OM8+z1vKcQRSR57XuRyFbHUODnGsNtEI1kRkZEZ2TU5vR5dtLQG215sKvuwqmvc9xxPONxMUBgjZmYBZJsMEFJVDcmMd4DynIDUeLznjJjbLFPYiUEDnvGbN+yVHStOiGPY3Q9CWxZtnqPUVzsfxzhgAewJR3sX8RqLf1biGoM/yNZlNcL9XpltgaQ+toHEOMQS6AP5wfAXzWVF366CHeZrE8TkZD80bffJc/W4MHATz9r4/RGBV5O1uyvO4si/5KNcaHTwoZT9JihKfTzRRbXTgbRXqH73Pgd4bsnp1k= 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 | 93 +++++++++++++++++++++++++++++++- 2 files changed, 115 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..e4ee25d16f3b 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -102,6 +102,9 @@ struct user_event_enabler { /* Bit 6 is for faulting status of enablement */ #define ENABLE_VAL_FAULTING_BIT 6 +/* 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,75 @@ 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); + + 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 +2191,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 Thu Mar 23 19:06:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186004 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 E7A47C76196 for ; Thu, 23 Mar 2023 19:06:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E52B6B0085; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 22D4F6B0083; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E05146B0081; Thu, 23 Mar 2023 15:06:20 -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 CAD246B0080 for ; Thu, 23 Mar 2023 15:06:20 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9B629C0710 for ; Thu, 23 Mar 2023 19:06:20 +0000 (UTC) X-FDA: 80601093720.09.E6F8643 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf30.hostedemail.com (Postfix) with ESMTP id CF4D18000A for ; Thu, 23 Mar 2023 19:06:18 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=DyCwLdgB; 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=1679598379; a=rsa-sha256; cv=none; b=jxLqtcnXvVpWsoEwNI1dBD2+3kROxe2edujJuKD4aW/dL6UGyFs24abbz/OQt4GEsfZaVa 9gYPG51Ua+jBIj9fzZSJDm5byY9XqRvcZwfduzCrnnMchx1VyEG8+tvLIApGE1Npc0hpAn 2HAYm3n2hZPuCw1ys1do4bXabDvEE40= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=DyCwLdgB; 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=1679598379; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4Tw0x0REBVNQbPmE7YKz+0Y5T7GJmSyJL4LF57PSOTg=; b=nXZJ0qxOaSiuYkTtmQ/k+yYkzc+NnLqK1Wlkvvw44aV5ZowK1A+jFAI99MuxMEE4OdUCUH nUvbUhoLA14QHpzneUeY9WYhW5jSRhzYCS/HvYE8kN43A7FsopYU++aJzGsXR3XXZwTxRV 2f+9N60hNkAcW3nBmEs7FLidociQVqA= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id B5B0920FC3C3; Thu, 23 Mar 2023 12:06:16 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com B5B0920FC3C3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598377; bh=4Tw0x0REBVNQbPmE7YKz+0Y5T7GJmSyJL4LF57PSOTg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DyCwLdgBT1jaPLxNLMNzMGU77hOtPvuZokFKJENh9nzmft9bapJLMLkaIgDwt0ArW r/kV/uov3hJ11WT+OPSZFg8YyVRkN60UHxJtk2/dK2U1UPrLJoFM7sdG7zFkTgLDq7 6FO2OG5SThGULJYkwutsG8B5daJV1Cm3WO0BHF4Q= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 06/11] tracing/user_events: Update self-tests to write ABI Date: Thu, 23 Mar 2023 12:06:05 -0700 Message-Id: <20230323190610.251-7-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: CF4D18000A X-Rspamd-Server: rspam01 X-Stat-Signature: 7mtkggtmxodu595ys184bzz6xj3gxzia X-HE-Tag: 1679598378-546988 X-HE-Meta: U2FsdGVkX1/zrgxxC2MQ+ECSDahpBO82PFY3Xp/8quyoda/qxMERDv5cxccfHf/QOk+nvWr6XEGTflzJUjqmnjDgtiwWCpMI1A9NyU6BS239JJdpUNm8VO1R2M5viqt12kmQ0apkUkRQ9C+ntm7v6h+1BqqrdrMpZ6gF1FqsTe72pHBWWASe2H6HAYOR4ohFwHyV2vkqlsNVZq/ukEyEf1tKCZW17YfaNUgj6rq8kp9kevbNxUj7sP62AEBUvcEBcCOmh2UXMyKaXnOpN0YLHkE/wYTFsNU3LNM38gH7FdIzMIgLGTfwWTcddwVjTkwHt9Ow8PgAQg4gjkMVM/Ja65A2EV1LnmoClCtUUKn7CTODuv6zvh4dt2Z+Y+akSv9MQSEZyDv7rSKxTF0Lz0gFcKBfuHySmg6pLVGahyXfn67LEK3iKNNt7X3cq/a43LMYeP8pJpdsYz4/w9kO7Gg0ywZH7cn2QrhjRhT5ku4RuU0Ma6EOZzz0S9O3zpP85O5WJIW1DuW93oQ5vM90XWKvjGao7VS2d2xwqwS5rG2BQoUg+XRFpneAzGXLLWPwiFWhqQBV2bgwII5KV5YrLVgFvaz850mVgEMsN8N3VGzKiASP7hQ1ytDbUyXPZpRygZPJwsjVov5manadPfsNg+bXpP0kNFoHlWikSIK0rm8vrjzco/3E0Fmb7AJI+9FUNJdqzdT0XuzmFEA1cQRAgVZLojbeeAWzNciGEQSLIOBRZKdWRC4CkmoDQlscBHCpKUkFSwhjmdsap+vwtR/gByi5I5PBNbtoNdwAKEqSY1rAWkqae+1tjVlzEscp3D/c9kzTclTg3CxCuaUXK2Yykkxf4eJ3X/0YBciAmD+kND5lyG4k3+eIz9154suui/RfdoVn6Wqfk+7+rifFUeEh2ZRGBRH/Bsi0i2plN4Uv/rgGGMmwTw0R4bCTu05vZChmlKT2rPXq3ooCm8md9pmoAnF hVrIsW/h d7deEuhRNWYfrMk/LTEzERr8MrmbDIsVRPYSRBGySzSFDTUlBbSKq/cj8tsSLAiSkRcnzLinwe9Z5iLMrH2dOyPlfXhFYafzE6fdPRld/rQjRYsTQH5voLVmy49R4H1tib1Vo7FoKEttXTN2fsbFfa9pBn6BmJUNB2uBeAW491sHpqICJJ73Q1nRd9CMZi9XVs8iMpHk/0BUlKFhW38Y7/6LZyG2ARpnq+Hjf5gkoEvqtQ4W2Hxg4ehKl+am5kj2YhQZdkXCzkxONMW0NfnyizcNdewrnwuPLCUGA0fU6TiWrJG0= 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 Thu Mar 23 19:06:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186005 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88331C6FD1C for ; Thu, 23 Mar 2023 19:06:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D8F66B007E; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 450A56B0082; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E1C56B007E; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 173AD6B0082 for ; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C7E2116058F for ; Thu, 23 Mar 2023 19:06:20 +0000 (UTC) X-FDA: 80601093720.03.94C8F53 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf15.hostedemail.com (Postfix) with ESMTP id 1215FA0022 for ; Thu, 23 Mar 2023 19:06:18 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=b8bFnfII; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf15.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679598379; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yLsV99bpcauClh1MlrmzrNiAoYsutXdpDe7QcYfID8o=; b=GWgDDu2j0Zbhv8Ed5hJ9RpUDQ1SIrtpnFVXm0JO2cXQBW2fQhQfGU767xuNBTPTtbwMmLM P3VlR1aN6E4yjtx5itR4u1RfgNcSiMVw512nckuzOj0C9wjtjQ+1XmpBb2XRc7dnNc2iEX bEYjwB45OE3Gkg5IewFoOaPq+ibR/lM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=b8bFnfII; dmarc=pass (policy=none) header.from=linux.microsoft.com; spf=pass (imf15.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679598379; a=rsa-sha256; cv=none; b=k4/y7wIsk0ps8rdMqhEj/gDDcYwaw23wWFOWeRJZ8+LpaoG+P4wcdqVDtjZ4N78jCS2DfY Zn4EXVz8lTlivfgXCBLbWNqLlcEafBs9bTjBcAb383amtwwF3qB0UVLOatHaxnvontl5MC OywzxCH6AwUBq5K3CWXAVrOfK7IesQA= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 2813020FC3CA; Thu, 23 Mar 2023 12:06:17 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2813020FC3CA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598377; bh=yLsV99bpcauClh1MlrmzrNiAoYsutXdpDe7QcYfID8o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b8bFnfIIBtYwXJQLdnYf0l+f/B38p//PJ27erGrmpVPj4MHh5+0YGKRxqFdLjrx4U vkv9MUNzZgu/TXVlqAL1W1cxfXE5WaCm+rjzv5h6PKkPquJ5CPeQkqbe+awLmpHGxY xdfikiGk9OoIu6EDLr+5BXYr84l4QPY948QRAl8U= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 07/11] tracing/user_events: Add ABI self-test Date: Thu, 23 Mar 2023 12:06:06 -0700 Message-Id: <20230323190610.251-8-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1215FA0022 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: q5u757boj6mwy6gd3yrukc7xqwww184i X-HE-Tag: 1679598378-345718 X-HE-Meta: U2FsdGVkX1+EqPEcPsP2EIoY1EM6HEUT1POkouH2w+OlEg0ndRgouBiR5TGIiUypGgWOVETnh81w44HAyGNF17iTKwvrWqZUlj78IkBGyomejwnSDIXb9E6GI7KS0S8vbltP+v9aUPcdG0yUAb/NhhSCLsrVG5pransTm2HJw5J/K46OK9CfrbCl3X+LX8qD0BeJjz4gpf06n0X3su5/Rk+D908qj1n4XtZfMUd96NL/QktgstixtLtClXNmU4wdHagq6XUY0+MTZtaHAZM9Jfk1n9M3tK3pcWedruMic56vyzrroijH3b+cUU6JowJz1EYL50V77CPuT9Hnr0Usf7oimBH/3qNqmAtPeFbK1zYLhpDuVq7DQXLTqu9JonjhtHoyxDwkl7Clx5AnygWSSMLLCv25efV8Yl3XyLFkn6KzmDdTZ4TTT70jzcSNCN+WPU43tKB0LcTLJv0lcPEsSHdGcWrUKXMdz2P3yCBONbjCliRCfSg3c8AM1gRkN1A31ePjSgJ1WEt6lA9sTmZ7hYfPA/TEifFDsE6ALTpdBsjyRZT3NNvA6V6s+HM1TQAXgc+x1al4bsObL48CKhLIObvic0jCOdl5mjneR2xTrBN82FaXrSU+DezYF3vniQOmDZRgKcV6IAFj/oVgQ8FKTe8sAfkaaqiY8EBWhEd+6fzTsmBxr+EXqEQuA1w2L3o7mXiYHQn4piup21Qr76brd6pHBclKVtUJXyC2vbQLt2ZgL7tPtFoD3r92rb+hq/UnWtJXiX/9wEGyjRwFWBwKy09PhxjXeLDQBwhoqGU4SXoVmlPFEj8iM3owsavcyUoI9ILW4353Ce4AXnqAIGxZG1zpdD4K04lGAziVgaQFWfHRBtPfDyTvzvXbMuJYvPMTxcPpTf6EW/mOOpQsQCUVGkyZMSBau0gESHoqgqMlGOvFq7rMnuuBNrKWxYSDp5AaXidgX18GmjkTWOLECnY ZiHvNbta +CG4VPFH8zfFDCRhewdeZDMFs2AQD7IW0czocdrAMLv44fNpxdtFdmLBvc3Ryt2YkBfVE7FMUBmQatExoEkjXSGXHPqUdDFML1G9xeQs5bHSY0xmlJCWLvCSfQWX3Q3+5jZeAxtses1X72ouLzG/+Q7tEDCva3LvcbjStegNaa6o3ZkOgk6aQTzIklOfMSWbkrr1k3UYwjFG/5JFY9mNvkruYPEsJYv4zJBzkHTK53uGwa83FP/x30amFEmiD2ZenPkAjEFiMs/XnzzhEibom9bkjiNaNCOjE0fw4FcGiMqD7k0E= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add ABI specific self-test to ensure enablements work in various scenarios such as fork, VM_CLONE, and basic event enable/disable. Ensure ABI contracts/limits are also being upheld, such as bit limits and data size limits. Signed-off-by: Beau Belgrave --- tools/testing/selftests/user_events/Makefile | 2 +- .../testing/selftests/user_events/abi_test.c | 226 ++++++++++++++++++ 2 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/user_events/abi_test.c diff --git a/tools/testing/selftests/user_events/Makefile b/tools/testing/selftests/user_events/Makefile index c765d8635d9a..d5f64ef93197 100644 --- a/tools/testing/selftests/user_events/Makefile +++ b/tools/testing/selftests/user_events/Makefile @@ -2,7 +2,7 @@ CFLAGS += -Wl,-no-as-needed -Wall -I../../../../usr/include LDLIBS += -lrt -lpthread -lm -TEST_GEN_PROGS = ftrace_test dyn_test perf_test +TEST_GEN_PROGS = ftrace_test dyn_test perf_test abi_test TEST_FILES := settings diff --git a/tools/testing/selftests/user_events/abi_test.c b/tools/testing/selftests/user_events/abi_test.c new file mode 100644 index 000000000000..e0323d3777a7 --- /dev/null +++ b/tools/testing/selftests/user_events/abi_test.c @@ -0,0 +1,226 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * User Events ABI Test Program + * + * Copyright (c) 2022 Beau Belgrave + */ + +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +const char *data_file = "/sys/kernel/tracing/user_events_data"; +const char *enable_file = "/sys/kernel/tracing/events/user_events/__abi_event/enable"; + +static int change_event(bool enable) +{ + int fd = open(enable_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + if (enable) + ret = write(fd, "1", 1); + else + ret = write(fd, "0", 1); + + close(fd); + + if (ret == 1) + ret = 0; + else + ret = -1; + + return ret; +} + +static int reg_enable(long *enable, int size, int bit) +{ + struct user_reg reg = {0}; + int fd = open(data_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + reg.size = sizeof(reg); + reg.name_args = (__u64)"__abi_event"; + reg.enable_bit = bit; + reg.enable_addr = (__u64)enable; + reg.enable_size = size; + + ret = ioctl(fd, DIAG_IOCSREG, ®); + + close(fd); + + return ret; +} + +static int reg_disable(long *enable, int bit) +{ + struct user_unreg reg = {0}; + int fd = open(data_file, O_RDWR); + int ret; + + if (fd < 0) + return -1; + + reg.size = sizeof(reg); + reg.disable_bit = bit; + reg.disable_addr = (__u64)enable; + + ret = ioctl(fd, DIAG_IOCSUNREG, ®); + + close(fd); + + return ret; +} + +FIXTURE(user) { + long check; +}; + +FIXTURE_SETUP(user) { + change_event(false); + self->check = 0; +} + +FIXTURE_TEARDOWN(user) { +} + +TEST_F(user, enablement) { + /* Changes should be reflected immediately */ + ASSERT_EQ(0, self->check); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, change_event(true)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(0, self->check); + + /* Should not change after disable */ + ASSERT_EQ(0, change_event(true)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, reg_disable(&self->check, 0)); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(1, self->check); + self->check = 0; +} + +TEST_F(user, bit_sizes) { + /* Allow 0-31 bits for 32-bit */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 31)); + ASSERT_NE(0, reg_enable(&self->check, sizeof(int), 32)); + ASSERT_EQ(0, reg_disable(&self->check, 0)); + ASSERT_EQ(0, reg_disable(&self->check, 31)); + +#if BITS_PER_LONG == 8 + /* Allow 0-64 bits for 64-bit */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(long), 63)); + ASSERT_NE(0, reg_enable(&self->check, sizeof(long), 64)); + ASSERT_EQ(0, reg_disable(&self->check, 63)); +#endif + + /* Disallowed sizes (everything beside 4 and 8) */ + ASSERT_NE(0, reg_enable(&self->check, 1, 0)); + ASSERT_NE(0, reg_enable(&self->check, 2, 0)); + ASSERT_NE(0, reg_enable(&self->check, 3, 0)); + ASSERT_NE(0, reg_enable(&self->check, 5, 0)); + ASSERT_NE(0, reg_enable(&self->check, 6, 0)); + ASSERT_NE(0, reg_enable(&self->check, 7, 0)); + ASSERT_NE(0, reg_enable(&self->check, 9, 0)); + ASSERT_NE(0, reg_enable(&self->check, 128, 0)); +} + +TEST_F(user, forks) { + int i; + + /* Ensure COW pages get updated after fork */ + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, self->check); + + if (fork() == 0) { + /* Force COW */ + self->check = 0; + + /* Up to 1 sec for enablement */ + for (i = 0; i < 10; ++i) { + usleep(100000); + + if (self->check) + exit(0); + } + + exit(1); + } + + /* Allow generous time for COW, then enable */ + usleep(100000); + ASSERT_EQ(0, change_event(true)); + + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + + /* Ensure child doesn't disable parent */ + if (fork() == 0) + exit(reg_disable(&self->check, 0)); + + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + ASSERT_EQ(1, self->check); + ASSERT_EQ(0, change_event(false)); + ASSERT_EQ(0, self->check); +} + +/* Waits up to 1 sec for enablement */ +static int clone_check(void *check) +{ + int i; + + for (i = 0; i < 10; ++i) { + usleep(100000); + + if (*(long *)check) + return 0; + } + + return 1; +} + +TEST_F(user, clones) { + int i, stack_size = 4096; + void *stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, + -1, 0); + + ASSERT_NE(MAP_FAILED, stack); + ASSERT_EQ(0, reg_enable(&self->check, sizeof(int), 0)); + ASSERT_EQ(0, self->check); + + /* Shared VM should see enablements */ + ASSERT_NE(-1, clone(&clone_check, stack + stack_size, + CLONE_VM | SIGCHLD, &self->check)); + + ASSERT_EQ(0, change_event(true)); + ASSERT_NE(-1, wait(&i)); + ASSERT_EQ(0, WEXITSTATUS(i)); + munmap(stack, stack_size); + ASSERT_EQ(0, change_event(false)); +} + +int main(int argc, char **argv) +{ + return test_harness_run(argc, argv); +} From patchwork Thu Mar 23 19:06:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186006 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 816B4C74A5B for ; Thu, 23 Mar 2023 19:06:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 882A76B0080; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 79D396B0083; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DD3A6B0081; Thu, 23 Mar 2023 15:06:21 -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 145526B0080 for ; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E3EB114078F for ; Thu, 23 Mar 2023 19:06:20 +0000 (UTC) X-FDA: 80601093720.01.EA59A4E Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf01.hostedemail.com (Postfix) with ESMTP id D33A540016 for ; Thu, 23 Mar 2023 19:06:18 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=P6MesIwO; 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=1679598379; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dmkEXknb1A0Qhw2AWPTXryudmpuAFo4IqxhDoe6dNiw=; b=YeArfzNmlzlmwx85t002ZAphXKNyZLO/t1fBMSB55q+0W/S4ugdFHu1VYEfNc69PmSDZkR CHYHnzVaBeb6Eje8DxzGY3A9nLEd5tUaFWtTZQhkF2FLK4dlGamRcnJP697RzP/yxkVSEn SjfrDPsHLnafoVqGxqeR8oH2mKq3n7U= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=P6MesIwO; 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=1679598379; a=rsa-sha256; cv=none; b=u9inZoilBWhQtu1XLvwo+942mn/k71AS3s7A0clmqHjWIKh0ypTQcHryUNEvwpc084QPNX Z/+2+ogZFEBHioDw6E7ShO+r78BOvWhsSMPxkCZCaUU/YMxUk7EF7r+qqR1hCmw4YWwBtQ gsPdQe58jbib+140MqpUiOoFxHWAbnk= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 7CFCE20FC3CF; Thu, 23 Mar 2023 12:06:17 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7CFCE20FC3CF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598377; bh=dmkEXknb1A0Qhw2AWPTXryudmpuAFo4IqxhDoe6dNiw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P6MesIwOojUCuQ3GuY7LjicEPdHYhGGiJQ+CLacm4xFS3stHwHJQoJTlvELbhrp+o QhEPZcaTIQYaIwcGDCBf2ezN2TCYtIuMYH83fwOTCwPa3Mz8GowW+tWbMgGdCwaFrA WBvnVy6owmiFugDjS1/g/mkA32fYLpZvgBT8FRgQ= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 08/11] tracing/user_events: Use write ABI in example Date: Thu, 23 Mar 2023 12:06:07 -0700 Message-Id: <20230323190610.251-9-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: rysnxjsqxyh4dmcq5tdfoprng8oowcnx X-Rspamd-Queue-Id: D33A540016 X-HE-Tag: 1679598378-841845 X-HE-Meta: U2FsdGVkX1/QJfAoKGs2vhMzJjVxCH0Wdt87yrqrx0nFkIDmn1ehjnZsxKRuzSqSiA/+reUeZyiSp1rtmkQto3iT91jEOQ6mQoWpWpwp02K8AEoG7+mdmgnz9jieJDKJlkjYUQ3pKKrRluiT8Q8JrtBIXG4c928NZq4Fm2FThgNbFTAE7A4qUgtrX3imqAwGXqJovEX6GvXJ6Lg5gDeI6necuKiRsO+k1yuV12qtAKoGiIcQl+ULdjiEsPT+SVRN4r08R+kH1sq+vdLZ35zEGHXxUvIvcZgDvADPsft2tZPGgeK+3XwJ5Vpvq+GeOK2Sa0ctl2caj0s6cPJ2xSyJxhk2klQJ0aZi0NTgpJqLBbtrfM8x2w9SC6gfx1tLUEpc/nYbBBqL4KYXDoFdi3GwlZb7s+BXOmZKTowsPtwqGOiFfTMkZDH7F8ENj6/yAKlVRH48uxmZK01n7lh/TZElchx9JRX3G4/JyXegHjTeVJp8Ca8Iqy85cha+KRT9REL1BhmY+JpgR7yvSs+MGQjCBT60vW6eLVEnnwnj4HqvjkLVqSX/ht0sJnE9E7+sT2JOYaveLvV8z5EDs7OBZ/zt21/hrhHrj19FLvDF9jTgbhNxzGA00GNSOKMQA4NnWtM6WLTO409+VZFAyKAO8b+WxQbxofjMcbAVCO4GZj7OgOhbpz6TjVmnvEiFGQJxLqTQF5xN2TkH97dpXjhpGwNosEn3mSzg4ZAbgjblSc2tCV8xDBOSfOE+3EIRVPYyUwMincqwTj7Yi4azamgo8rOzkcq0a+AWWrDs2olM0JIZOEhuHGofkaCoycrwxiEyI6s3Qtz0/SZ2wzttyexNaV6Q2PqQ68K+KuS/TI0U4fz5LrqSNVMeEaXsJ9gpSme1W/bVx9/APxdM0ODgUP6fKGStlcu+FPOXRk+oRsitixUC1XZ6vrnvIgJx4MI6jXovezd5lDDD8nieU0q82K2xbSP LPSwfFls 6gMDqrI4CqS4PYfI7xoeKLXFgtI3gcUlHAdcl6cFL3+yw5ge3OVTdM8BmMJpQLTGuemdA372NROFtWCMy8gi57DJhjYXaXuizKqfzSnJZzQpqxm3r8CXMFvKDJIONR2VheZXKiFXygqbsF6NKdqeOwU09oKp2gBUG4mLTn+O98GQo7BF05gJeSuXYgk9kiLIAzdjTa716uCmdHbYE8EkEfjKouPmo56OXWRQYvOCmwnV3CYn9s1Xp+NzrBAaRIVqtoTd6Ts0oJyQ2Zh2fHvul9fbvsLeti5TyUZQkMt00H9yC7ts= 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 Thu Mar 23 19:06: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: 13186008 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 3BC00C77B60 for ; Thu, 23 Mar 2023 19:06:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E5F26B0089; Thu, 23 Mar 2023 15:06:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D2CE6B0082; Thu, 23 Mar 2023 15:06:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 021226B0088; Thu, 23 Mar 2023 15:06:21 -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 DC4246B0083 for ; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AB703A07C1 for ; Thu, 23 Mar 2023 19:06:21 +0000 (UTC) X-FDA: 80601093762.28.2F2D6C4 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf18.hostedemail.com (Postfix) with ESMTP id B0E171C001C for ; Thu, 23 Mar 2023 19:06:19 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=TunKrUzg; spf=pass (imf18.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=1679598379; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gZQIOerORAHQ2Mn7LD3nOn2GElU/ypPYbhlbPDEbsno=; b=zdBV07oSbUJ5OGImvIrp5/rjLjJWvlviM2OLrRbuCXeITNh+tIsvu8VHNIPy0i/q7e7SlF OxcHaywkii+OhKmkirXscRP0Kvg8D9p6yGBO5jv9EG7419iWAMnfD17USFL49SPUWcsGBE BYaEo+jC/Xa4IHGZRiri6aAsg6Vlnps= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=TunKrUzg; spf=pass (imf18.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=1679598379; a=rsa-sha256; cv=none; b=QrKX5X9CPMnx7E5NPZLsBgqpO3OLMcDOi0ngN3d99jMkfJUqN3iPpR/Fb3rKkgJyPN9H1f nHP2OYQBDZLjZHHKi+xxF7V9w6sGwoQbhEmiaGylauQASU6pd/mOdTj9Z9bojRdSWtDQyB wWogh0jK4jKTmD8C2grc58sMnG8KNYg= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id D6F2C20FC3D4; Thu, 23 Mar 2023 12:06:17 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D6F2C20FC3D4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598378; bh=gZQIOerORAHQ2Mn7LD3nOn2GElU/ypPYbhlbPDEbsno=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TunKrUzgef7qGHbiAciCJc6ZcuA7NpMWMNGNQ/y6S8u7W9ixsPv5W+4y158KCL2VR Qnzxa6XcqkRxWca53orfLfuxg2g3xQiprCFw48VBPlv73vI2RI6zaRrk1DpBoBCHyD 4vna6hWvQa5IebQRUAZcfU0NSTPEDHoPupoLU8kc= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 09/11] tracing/user_events: Update documentation for ABI Date: Thu, 23 Mar 2023 12:06:08 -0700 Message-Id: <20230323190610.251-10-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: 4mgah56cyo1e93a9krp9xwoazyk7sp44 X-Rspam-User: X-Rspamd-Queue-Id: B0E171C001C X-Rspamd-Server: rspam06 X-HE-Tag: 1679598379-337549 X-HE-Meta: U2FsdGVkX18jZEB5PTpErjuUweruw1j/2HHsAnAM6J57ZxvZryENHi8++4gcU07IbpCU/WQZuw5HXMzAIK+nN7FgBCPis1oY66EQ7GKh1itRH7lLBZF6kfY7pexIHXG1LxD8LqpvykXP2PAB3bfVp7kLJMR1fS8qy05B5lZqwC+fZXOiVD9e54L3rEiU+poTuIBEc8OlB9j9rKaVL/CNzQV17IkMnw+8ZxPf9uKYYegNzTEVD549aM5kxc8Z8UkCABqTjN81X/6/ko8n3rdai+V97I6mypbWhHNj6g1M5AgywmTWzYKtRmq5SlabpT/T7+tmsVurDUnwkgwJmRLhL74IFb9uKB1Wa0GsdkEK/MNTRi5sRsgnd+Rh9FvemqigxC58nFUSt+MaVgcloHEWjE6/E8k0lS/iUzSA4aTDfGtUTmGLSPbhbAngbHenG+vFXtp3v0AxqhyHVuIRpcp6RffLiY0ZEYGbwAXFhCz/8ZudLBmOYEDbW33xsyCDnjDoIP9g/JRnx8LqQH4Itie7l4Dwef+QNTx/J7Bx/EFz/BZ1Jx754d5QH+PcHAL5MbefXm4g9jyQwiyoF6XwGep2tZ4/UpbV2a4SSp4hQPf1rLz6B/tEFrStPYsmyQ/jHwiDw+ByxNPcflTIdzkU6QidAI/qmNEYVZjrYZUQwPIc5pteCjkvmLGqTWDzGLx3f424TKCGSOkZ8nULsL5lJQqUmvf3b5hMSu/9cLgCyBxw3SOJ2aDHWQ3mHPZNm4NRuYRHWLqE4Mr5VAnRZLPd3IoeZPjPgseCsEplolmNEJ/tEYlzlnHQrAaW+vNQZ33uWBSAM4hcfQdtI5jphe+aanQmKhRXww1HANLv3qGHJ1Xj/it7HhfRnB4QIudaEhaqK201OEopTTI2oZApukifCW5K2sMu+mojDm3DgVl+80qRyh4XWMI/BdlwTdakxXC35p9Hs6H7qKJy4ZyzX2qeQvV U3gXui/G k0vmOWsu9jMwl7FRUou1FpLEo15LDRpkgNTGw0ZZkTT+U3fO/QJarfol9m4HrlcCFv67ut4qFSG1++PlPoGwSNFWcpcuJ8MdG6YtvErwKZ3mFNjK2nvJERvF76yK8mkV30hgTgCKOa72sFjuaJzLZz6fhfymB4Fn0GtGtJ0BfW+9g4EGbiII6rQzE9/XP2HpPRG7sB1ifKpEA3nW5eBh+WlEbMM3Bujv80BD+D+IEEmG/ugtMoGosJO8hADXbNGaSEZO92jXHv82EQqZUCJ3BMlEpBvFhrPWFzd0S/6oav+qJgmE= 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 | 177 ++++++++++++++++------------ 1 file changed, 102 insertions(+), 75 deletions(-) diff --git a/Documentation/trace/user_events.rst b/Documentation/trace/user_events.rst index 9f181f342a70..0180714f10e3 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. -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. ++ size: This must be set to sizeof(struct user_reg). + ++ 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. - -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. +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. -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. +This command takes a packed struct user_unreg as an argument:: -A byte-wise index check is performed as follows:: + struct user_unreg { + /* Input: Size of the user_unreg structure being used */ + __u32 size; - int index, mask; - char *status_page; + /* Input: Bit to unregister */ + __u8 disable_bit; - index = status_bit / 8; - mask = 1 << (status_bit % 8); + /* Input: Reserved, set to 0 */ + __u8 __reserved; - ... + /* 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 ------------ From patchwork Thu Mar 23 19:06: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: 13186007 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 75206C6FD1C for ; Thu, 23 Mar 2023 19:06:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA7576B0081; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C56DF6B0082; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B1E686B0083; Thu, 23 Mar 2023 15:06:21 -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 9440C6B0081 for ; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 640C2407A2 for ; Thu, 23 Mar 2023 19:06:21 +0000 (UTC) X-FDA: 80601093762.01.4D6FC43 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf23.hostedemail.com (Postfix) with ESMTP id 9F520140020 for ; Thu, 23 Mar 2023 19:06:19 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=EfKnlK7i; 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; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679598379; a=rsa-sha256; cv=none; b=cjcIVGSYXobWPyRv/jjMEu2huZlo1iNgu5SDT+lTGjEJ6wkXcDWoUtOo3RxANQ0xP9+z3L XndWjDz3WIjmf0/Y2vTJsqGjODcJXVz92RuGQ4USzm0ARRblMhS5U6MM27TgZZ8c58dP8Q p3Hc/v/jwMlP884bjD8oZI1XEaSqvLQ= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=EfKnlK7i; 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; 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=1679598379; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IeyP4hszPxXQh3EfDrorhwGAyqHtWNJ8d/cCBZiRrsA=; b=k2Vj7TCi57u0R1rYrbUs4u0Qdh69J9pRK0wVC8fKezARYTukH9Fvg8Xfh/Of2pJbeHHc9w Qm3H2/JsRRMQGDAx5f3roUjKhmo+n6LxQU0WyatygTMYjT5YJwI2otHoxjUw+H2TMPpeFF 7z2ORcR6IyC6RBG/65BEhnuugoT7qy4= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 4A39B20FC3DB; Thu, 23 Mar 2023 12:06:18 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4A39B20FC3DB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598378; bh=IeyP4hszPxXQh3EfDrorhwGAyqHtWNJ8d/cCBZiRrsA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EfKnlK7ie91+JWQuzGeux+r1OouVbdyw5cZV/Gy6GM4l20qRWT/ys7CYOjDmpcVSd E519AOwKHNkh3mEfDJa5NggLTDkb/V4FYswrmE2O6Ow9tc5m2w8gQXGRVBNRuZ5tCl EttGf1S+GuVT7g2/HJxZhVjie1a5zQHbaVu/zYi4= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 10/11] tracing/user_events: Charge event allocs to cgroups Date: Thu, 23 Mar 2023 12:06:09 -0700 Message-Id: <20230323190610.251-11-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 9F520140020 X-Rspamd-Server: rspam01 X-Stat-Signature: 5orcxfz4twsjmziw4pps9ta41wcuehui X-HE-Tag: 1679598379-475977 X-HE-Meta: U2FsdGVkX1+UBjWbgh6iDW4/1jIquMrJ7jw2tQW4BFld/zFwliBzyFDrlOqjsYraO41BMZW0kiMcuX4btJahUush2cxlDW5KBaljUSyEtH1Zs1tya4Wa5kbbBYgToKAy+lu8cTe4R8IHQOZOHRlEaUrK9PNK3KOT5EvG7x2GSTjEXddaEHzcn0QNk/zeM8wMjfLSTkJAm2gKJXiVgq3giKBFL/EwFjHKO3uBnv1pkl41uG43NvmANGp63ELYqQZjC3wkgo8MzU51/nXo0N+NY5AkIUzfbMpZV+4Onmi8TSZSvFHz1JxABRUyFuLavCgAbR/5TapFhZw0saDaCsB0g+nzS5yxUnURgqBjeNPFyo9aX5RmnfkgaAE0A2eAlTjggYhkEGPyER4UERJGaIcBWCgjpKXWdEqY1uxLynv/XdNk6+YZX43FJqWscmaPo7H/Bc2t/hyyvDVKOu5YHJ4aztsCodubZKPKKZ4adM6ISxdTGFHgb2I38c8D20bb3SY3UmJvRu4wd53RjwyxO5A3m1Yn1GTPRM1SZghmPFBKdOQYAuMdTT5M18QzqO+S+eRkOlAQnmaQvbQtnl4WISG8S1+AJIjXVGjIUqIDB6vsEUIIVoW+dn4c+RZNQ49x6Q+BO63/7qO830D6u1tNdZGec6kjzlZh8t1JDdmgaWXdperW9/5mhavUI3cMaeoGQwLhF9D0tBM5Uc8RMPwPke6V38r6DRhL56aQQcmH4t7QA/0DlWr9dtT7H3Wl3b4lB373Oli9KZJesS0C2Cbnkm2ZYYWtHXYNYvoc0NvTb40yWhshvx6M/p7W1XG/CKDAfUQBkbt6jVClgtcJkBOcR4xPmMYKE2q6eWUOCi7wsCkYci7b+dlB8tbBLxGmg5AewA+t6kZlHRQrwc8HfvOFI6ML0fmQpQ2CXzrCZyLeSJ5jupnhUBblnhQ1QLG4rSkeO7+B+lVlG3vMIJX1EgO13BK pL0bK3fr T6odo8XEsBoLgoOmchkdK+s8qCHNJq0YL9xzgWDkX8W+81BBeqVRsVmOjcxLSjo6wq+banC2g5xcAlH+wKUHrgaKL1uejVRZie2p2l0NGSpQSqClqW/8SWnUvMF0STMzBy8shEIfKs2mWEZsrs41kCOOBlBs9B78a5Sbup+DhpX7AF642XMPsZgjZYwSEeR3BzscbjrkBLZidvIrBCJolB7YoegrbaNhG/n0DsldhSPvZTGk/nX7GNFkgnLOcIYQGdxVxyZYNkFGE/Oj1+6ZMCq2xAZ/ppTDnY3BGYJT7Ug8ZPGU= 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 e4ee25d16f3b..222f2eb59c7c 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -442,7 +442,7 @@ static bool user_event_enabler_dup(struct user_event_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 Thu Mar 23 19:06: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: 13186009 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 F26D5C77B62 for ; Thu, 23 Mar 2023 19:06:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5380B6B0082; Thu, 23 Mar 2023 15:06:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E57F6B0087; Thu, 23 Mar 2023 15:06:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E6416B0083; Thu, 23 Mar 2023 15:06:22 -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 E7B056B0087 for ; Thu, 23 Mar 2023 15:06:21 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9158D1A06D4 for ; Thu, 23 Mar 2023 19:06:21 +0000 (UTC) X-FDA: 80601093762.27.1FF7EC8 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf16.hostedemail.com (Postfix) with ESMTP id AFE4C180021 for ; Thu, 23 Mar 2023 19:06:19 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=j9tjnCR3; spf=pass (imf16.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=1679598379; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=I/N19C63THCRBfckR+N4mJDWmIQC1iuXco7+RGR9L6U=; b=yi+O4I/76B+8/qrWrCcb2e+wcQBhAMyKbZd6SsVl4qaz8hC66Q6nG/MuGlgw6JwllU2DIy OmXEeKbXZETg1+qDtwCeVmSdUynVuVNm4rxxzSp7e4kjCbGJL7tJhA0P37BlogWP2lG9uf uL6b+5tww4uJyJmLaIPFVGBKx6dYjiE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=j9tjnCR3; spf=pass (imf16.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=1679598379; a=rsa-sha256; cv=none; b=jyQDffd8G6BZR2ocIR2pdTQaN7WaXYExv1FrmkhrIgCXVuYZ1yst55IKbWX2MeEgPrmXLP NVg1RtIsnGilXS6Ql613j5f4jIFF4y1VB5wrwv1EPMq4Qw2lSsfPI40NtfW80fzLf4R9/w C+/xp3qBdgxt31OUfEZaYxY6P864SEw= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 9C4D420FC3DD; Thu, 23 Mar 2023 12:06:18 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 9C4D420FC3DD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598378; bh=I/N19C63THCRBfckR+N4mJDWmIQC1iuXco7+RGR9L6U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j9tjnCR3CsEDEcvA3k+oaUsSOcOjeNTj1ojEqDHiabzZ7dIvAD5cYt1Op99jOFX5/ LcmOeu0DdoXgGP/L0gNnvIIBmAFdGUpliUOnyr7lP0Mck+y+jxamvGOrU4LEDUjqjq i0QRGSPaXbcZ7+TrOypw604xLApVE5AWW7WlYV1w= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 11/11] tracing/user_events: Limit global user_event count Date: Thu, 23 Mar 2023 12:06:10 -0700 Message-Id: <20230323190610.251-12-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: AFE4C180021 X-Stat-Signature: aksdu6ksx3sducs4i3eton7kr91dp9sh X-Rspam-User: X-HE-Tag: 1679598379-144498 X-HE-Meta: U2FsdGVkX1+iFm/dMlDMQAMc2qxuibqa7dkRud+YQc3uOnGl1B6JS6kYBadNy6MGH8N88a8tK4DxpP3lgNAAXBDZGOGsqOrwPQyAtfE1q3tLh9AZvWCEM+6Pd5m3lm5XZQr+586wSP3j9rIw5emq7JcaqKqN7sPSVnmNFU3YrNfh9QL7nc0MaUTS72KrAOCZOqgG3WQ6RrYRBvSA+dKxxoYLCjxgIumLcdd4brucndNWSkN/pId0VcTDeFgwUb7ctwO/xJFifPmsNAp0M7cr4rYN1BiIGA6MKklgx7JfABEvmEucP9asSpfZnYhQdUeu5Q9Fr+BOnNRY7fi8ReGpRGQNvhLA17eKehin3vQvaAc7Wyu6iEFkjzJ+G3kn2WsFfVznYTHwz8thsIYLTnT3QlGRU5RR3Bxc6lJsKMHGXxAtvyCD9J5uAmfvK0/h9sVe5B0uyIFAlN4KJ9JH+KfWQNLp2fjZ1/jmO1YLwiDf0fJr86iaEy81CBXkiF0sikeb/41kWoRLCzroH2VdJjxbdlNm6C6LQW5QJmqlhOUNxQhvPbR3A5MhJ5WuVIvCFA7Ee7UofgpOaxyS7F46SpMdLIYAI7Q4Ss/yZHlb0DI4KDTEFZLDdWQcBWICC35djgU+FUNkLwZqbi4hbdVTUZxZIEg8KnqAbxzGJZEod/oLY3ZXV8mGVzzEBHOf7d+Nty6m9NgYboPJpgaPq47r/u7GwpsVXCCLL8sRpPVoh3V+EpcV6vccu8jJmfuoLC+h3GTzcWbYkXON4BA2NNaD4KK9msQ70/5d3eP3MVdzcxhrrK5HBgYPbJyLbSBFpfv6JmG+s6DdzjPcw0GRuRbJfcX3mSnarGreHWMhbBbrp/OlQATwcg3UP6coWzLz2fGzmuChuh7mr6xs8yYiyaKZFAqS2kv4q6njtSwCQluLQe2gwivIXtyrnFBMvj927ANl3kaFs+pYoWnaGY4ro0nibzx 8C0A2Sgc UPspV3E4ObGnw5EUKaD5xytthPnXBiPg0hMKuiNP3IYHksrC81pWExye/pROmdTn/BugldbPQVPZd44pu6hCj7YjgyAuotHNkBUB8TPB1y2VujvpH7RYonyYnVFyrAb6SudHTGt/KjrRpXa21Om5JRaLAiEzhTxZnCVcIqww1N1niwzFodERT8zPEmaXycF3Kj8MgFsi8zPowWLY1X3quwyFtkk7+NAwzfkFev2x1DQ2Vqntr0/UlfNZAa0Q4uceFZ6E6rw7exo3OZ44ElIy8HmGvbo0uSFsE6b823Tj28+NsXLsYpVVPoriHp819KMSMXwCd4+Idj6eNvu4ofuvDWXPK+A== 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 boot parameter (user_events_max=%d) and 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 | 59 ++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index 222f2eb59c7c..6a5ebe243999 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); @@ -2386,6 +2406,43 @@ static int create_user_tracefs(void) return -ENODEV; } +static int __init set_max_user_events(char *str) +{ + if (!str) + return 0; + + if (kstrtouint(str, 0, &max_user_events)) + return 0; + + return 1; +} +__setup("user_events_max=", set_max_user_events); + +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; @@ -2415,6 +2472,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; }