From patchwork Tue Mar 28 23:52:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191678 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8220AC77B6C for ; Tue, 28 Mar 2023 23:52:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 385F7900008; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E65B900007; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B022900006; Tue, 28 Mar 2023 19:52:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D9A95900008 for ; Tue, 28 Mar 2023 19:52:31 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A5E9DAC0A2 for ; Tue, 28 Mar 2023 23:52:31 +0000 (UTC) X-FDA: 80619958902.30.39871B3 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf30.hostedemail.com (Postfix) with ESMTP id E20368000A for ; Tue, 28 Mar 2023 23:52:29 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=nOsuB16y; spf=pass (imf30.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680047550; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xpyxft+n8GA+tebj3uBF7xj0cPJnmjMw5o/uJtPt8qA=; b=U3tiLDoQ4XkJzkDCQbV19BIv8GsAw7RWiJrGWsFDsNzckWRD2PSL3Jfg9nNZWsXg1I3Bxq 4oi9Rdt1JdaPQbeHxBLuR0/D0i6zs5JbzKIxpx0MN2sLfRC+3m67HEDztTI8YxzLc+1rR+ UzJT3Oc3EwftKIviKP6OIONBg22Tjnk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=nOsuB16y; spf=pass (imf30.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680047550; a=rsa-sha256; cv=none; b=xSvQy/mVLIYxj8oUfg1QSPZU/XJFlZYufbDs91isKktQjnOMRfuRIo7i7JFwJCGv2I3zl9 gLUWmIWlRWBjsHoVY1XFwXOp0gDg72wiv6ZzjfJoQBjeVHSA25n56on8nDVAakC6+DWYUW /m2wyyEHUniY35rk/WI3OGQIoviEnXQ= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id A837820FDC35; Tue, 28 Mar 2023 16:52:28 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A837820FDC35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047549; bh=xpyxft+n8GA+tebj3uBF7xj0cPJnmjMw5o/uJtPt8qA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nOsuB16ym00tQHByywSl/4cvENvbYlSRWVVQdjexGw1KSt+RtISzhMYb2LmJAXocp LeAN/1oWA+q+uZamq1fJLiYZ6kOS72qsDslNCh/Ujjkz+zL7rTEGfKHfu6KOC3K6oo lRBzqKrWX9gQ3BSDOOf8E6jmxB1pur/oOrAZewng= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v10 11/12] tracing/user_events: Limit global user_event count Date: Tue, 28 Mar 2023 16:52:18 -0700 Message-Id: <20230328235219.203-12-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: 8b6sogrnxqb5onq4qinwnbmwzwkmns87 X-Rspam-User: X-Rspamd-Queue-Id: E20368000A X-Rspamd-Server: rspam06 X-HE-Tag: 1680047549-264213 X-HE-Meta: U2FsdGVkX19DUFPLEezCHzPHx5ZuLYCye124EnuXnpWwwJMQuCk5Grf/UXWYwJUwP6hRZmX1zNFHbm5C0AO5gOkngHP0EfPT/RBXHJh26EUnOUPhSZL/kta807z417sXDxv+hOxyOu21vs0u/lFXJQ7WTMfTbLehfPCd1Q2uJhD/h9jwFjEnsiHaEbCBYtrRqm7LRJywBElFh2+giMbX9+J9+za4ldUa+ul9KKN/3cUnX305Y3rsrBJqA4SWXrc8asyGGdSrwBUE+9+T2DFmNh9opHpVQFuobu7yyDdT3pQAJdBoUYFaP/HgLVCRPZRZPyLydqTBZK1MoHRtuyPqBpb90CuU5+bKjGa4ax0Duhc6aaY6VcWSMrlsYI0Zk/VnupP6S/c9fUJky0Tbzv/V5wSu5QwjixdIY+Un4Y9GTKgB5jir9nmu0JNEirHM7zECF9CndoE8AaBLyEqywZg3/jN3Cf+OOrZMMx/MxKBX+SQMxkRXUeVwlI8xr6OYMxWDpq1CgsPMDO+vKbarBBnn9obGP4PAFQtuUiB0XeNudgTm3+UHs7G/Pw/Vaonilbma+Xjo2XJaUMqZ0gqLX0SNqxquEmTpf3q4mjnKbMyHC/vwbOSQ1i5emg2X6u7m/Pc9mHXPwx3S9p1sIlTLxrdONOB9eD4hCVaQKI6vMQyeIDb/BRnC8XYhX1PYgA5oaNt5hRodve+30/WKH9g0/1yxfKfCRjYVyiS0x+7d4DUgCXtyU3SMJVlpnyIWsFaeocmFs6Es+xP9H1FcDZJtdLuNIa0s3oF70hzJ3HiJZatflFp+FxOR3ha1mmH5xhtRmNUqTM99CamboWOFE2p0LUpGmb8JN39O/zJbJGSDDSOOrqtAY64GfSRW1+TQjtxM7M/uq9vjQvAU7tQ0+DHeo5AbCwiuACRbdfACTFYXbPNYy93qSFIZSGsBSIUHg8K62iew4A50Knl3Q6BEBjLW1Ut wtVD2gMO /H5zHEMs/q7XfIw5gRjaH7/nH1Ye6wo/rCY482zp/PE/lo940cuMc6a67+HKhyS5gTWXrAEV3MSFEowpp/wI/2PKdlI1y4vHRm+pvoq8OGxQMox8LQ+yj6cZQOK5IL2j9U2tTFNgAbqfkODAYrxvZN9wfeX4LdFsWQPObtFpLyX/P7/gaLFW4kjDTk2FzwWeA3zJ7KlMfOy+gKMxk4fjOSVANwAafw2ZPNt8nv9izfk5kilnnAzijf33FJjE4iNX2oU44aiuwfE6VpqJ9lLoCCxkTmL/wcc2wrGW7CS+1PNmEuB2Y1wpt8Tcv9b0hqD3obKxSyV6dEyMaCwPsspNk171Jnw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Operators want to be able to ensure enough tracepoints exist on the system for kernel components as well as for user components. Since there are only up to 64K events, by default allow up to half to be used by user events. Add a kernel sysctl parameter (kernel.user_events_max) to set a global limit that is honored among all groups on the system. This ensures hard limits can be setup to prevent user processes from consuming all event IDs on the system. Signed-off-by: Beau Belgrave --- kernel/trace/trace_events_user.c | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index 3a01c2df4a90..9b43a02e1597 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "trace.h" #include "trace_dynevent.h" @@ -61,6 +62,12 @@ struct user_event_group { /* Group for init_user_ns mapping, top-most group */ static struct user_event_group *init_group; +/* Max allowed events for the whole system */ +static unsigned int max_user_events = 32768; + +/* Current number of events on the whole system */ +static unsigned int current_user_events; + /* * Stores per-event properties, as users register events * within a file a user_event might be created if it does not @@ -1241,6 +1248,8 @@ static int destroy_user_event(struct user_event *user) { int ret = 0; + lockdep_assert_held(&event_mutex); + /* Must destroy fields before call removal */ user_event_destroy_fields(user); @@ -1257,6 +1266,11 @@ static int destroy_user_event(struct user_event *user) kfree(EVENT_NAME(user)); kfree(user); + if (current_user_events > 0) + current_user_events--; + else + pr_alert("BUG: Bad current_user_events\n"); + return ret; } @@ -1744,6 +1758,11 @@ static int user_event_parse(struct user_event_group *group, char *name, mutex_lock(&event_mutex); + if (current_user_events >= max_user_events) { + ret = -EMFILE; + goto put_user_lock; + } + ret = user_event_trace_register(user); if (ret) @@ -1755,6 +1774,7 @@ static int user_event_parse(struct user_event_group *group, char *name, dyn_event_init(&user->devent, &user_event_dops); dyn_event_add(&user->devent, &user->call); hash_add(group->register_table, &user->node, key); + current_user_events++; mutex_unlock(&event_mutex); @@ -2390,6 +2410,31 @@ static int create_user_tracefs(void) return -ENODEV; } +static int set_max_user_events_sysctl(struct ctl_table *table, int write, + void *buffer, size_t *lenp, loff_t *ppos) +{ + int ret; + + mutex_lock(&event_mutex); + + ret = proc_douintvec(table, write, buffer, lenp, ppos); + + mutex_unlock(&event_mutex); + + return ret; +} + +static struct ctl_table user_event_sysctls[] = { + { + .procname = "user_events_max", + .data = &max_user_events, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = set_max_user_events_sysctl, + }, + {} +}; + static int __init trace_events_user_init(void) { int ret; @@ -2419,6 +2464,8 @@ static int __init trace_events_user_init(void) if (dyn_event_register(&user_event_dops)) pr_warn("user_events could not register with dyn_events\n"); + register_sysctl_init("kernel", user_event_sysctls); + return 0; }