From patchwork Tue Feb 21 21:11:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13148408 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 D038DC636D7 for ; Tue, 21 Feb 2023 21:12:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E6FF36B0082; Tue, 21 Feb 2023 16:11:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DDCF16B0088; Tue, 21 Feb 2023 16:11:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B16CE6B0085; Tue, 21 Feb 2023 16:11:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 95F5F6B0083 for ; Tue, 21 Feb 2023 16:11:55 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7561AA09FE for ; Tue, 21 Feb 2023 21:11:55 +0000 (UTC) X-FDA: 80492546190.27.B1FEA70 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf25.hostedemail.com (Postfix) with ESMTP id AC18BA0024 for ; Tue, 21 Feb 2023 21:11:53 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=dCRzQGcI; spf=pass (imf25.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677013913; 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=tQRD9JE7P5KGu915Y4B8hEkdTXGBR44vXl+RSatCucDGZeH5Wn38QD/iT7G6ieGelHgV8F r8Tzk0KRXjmY0zBuwpT/WzJvVpwtjsZSHYwdLy5RyDwJDFZq6yBnP6FFpiGTDe+Zb5T1FB Oe1mtzg5eQXi5zobOEr+24xUAQa0MrI= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=dCRzQGcI; spf=pass (imf25.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677013913; a=rsa-sha256; cv=none; b=c6Hxc2OkjPKd6DIimSjeH/sSKkb61oovgoDFK2EPxn/sW44NtSF0wVQHnM1UuByqEmglX8 k476osJ5WX+H1AfV94dbJ161cyKRT5kJXAZ5IIZ2RuCLIdslwwsS3px6VPwhnRzmjL+jxU prdHCDZO5rHMIvHTSdeiCSR+nYXrNto= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id BEE1620BC5E7; Tue, 21 Feb 2023 13:11:52 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com BEE1620BC5E7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1677013913; bh=I/N19C63THCRBfckR+N4mJDWmIQC1iuXco7+RGR9L6U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dCRzQGcIfvRpOp9kpdwMTF1aaihpuvaIfHxS1euR/JCwnaOjxQUy3XkUPyUcF+G0t U2AkicFcPC9hBHiH7CZP+EKRF6aPM1gjYIrEUJz8BSq2VL0LT7plTe302im6xToFwl QP9/1kHRZa3txCLzPg3NMGhV5c8lGlBU2QrG8+Po= 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-trace-devel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v8 11/11] tracing/user_events: Limit global user_event count Date: Tue, 21 Feb 2023 13:11:43 -0800 Message-Id: <20230221211143.574-12-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230221211143.574-1-beaub@linux.microsoft.com> References: <20230221211143.574-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: AC18BA0024 X-Stat-Signature: 39a6yfw616estatf64ymcmshp6r4hj5a X-HE-Tag: 1677013913-100875 X-HE-Meta: U2FsdGVkX1/OLS+4eCNG9bWDk48Dc8I22SFsywe4yiRR1uL/IDCk9Yhgy5zfk+4+KdF9B0xkWp1HfKuHE61jWWk3vAMnq45RJflXsMLXim8AuKmVtNCudqRF3TMc2Zc5/I+JeQetS6vpWT7X54Eaan6oOAGYUJIvKMGOuLUPmxaetUG+gmAKJi0t+bj4iTGSGIkodGjGQAy/K5zveCGaUUStyBSkTjSd/omz0JS2ldfkxm4frf0o04Ngg3pOJ0z+bjFjy6Eo3vrb4iHcmjPeVlrLsSpXGUC4YbNXwoVboqD9MG9uOx0sVEYeNNvYaW2o18Whg2z3NxIPDfRI9rTU6QU45ZoylTsmByN6Nw9L/DnoDFSEwTo41sUtMaFGDWYKcFcx6DG1L7M6sFX2dnul1/090QwUQQuSK5NuhrEON8sHODnj1qTgEdZUJyfVx+jgyfXCWN21lUlTJFfxYbEB7KGFji+aiax4399UZ87Y82KaI/tmOiaOrZqfU3PTKYsw/7gQuhvHwS8XtNv2Rh9oc1pLbR6o/4IEIEffwVfRABKjil9FvgfoltImK1LZmSRfESt/dGHsDynbVzMxv3WRiTuibZXqUsIfBTB6mIRMm54bfo9iBXk90XKs2HwKptB65V1XqxPeurcX1OWnahvnOXj6qp9a1PPhXoiN9IzTlV52JKAdVed/nsc/Z5L8MIiZN+WmpJbJRKH4sk+yo6WwOCbjYIHKP0DZfs98cfG8V4OOPYFvjSycOSNoC2e0VNZtAjYWIBFUM6Ha4BlolqeQrdlg6eG5NVx+FxQdJY0IaYfCMRDSvWxpCudeHijl6xlvxPNpJV1wrZE9il4ta+qUTBl8lmLF/R/wYqMwuypIR/v9zM6HmnqZqKjnQ8udfYjA1Br20vvsdtW1TeSMbWWCBN+Gp/joApp0k7Gf8ODn0FjXzAbfxM9NstAfUBYV7ybPdr50+Qw304PyUfJZX3D MXAUbsPu lhG5nEdoWOOKpAWhitjPDzS5i2srmt09gp0TmzRg1VdvLsx4PLFBzQN/rWK259KeyoPGT4lxDLI4HpuVo04QCTXqG24j1vN8WpGLhvH0DoJi+/CIA2rcQW2e3KSRugPwq4Fav8rjaKg3JIXZXFtRJIn5xCTAro79cAXxzXFZGVxD5I7ppoQkziPGqRIdPIHDRQ7T/G4jj23UAuXVvr3mbVWVWnous4qJQii/XiWJSDjC8dfcw4MPFQcFxL8wFtZfeq55sACdBTG/y71F8cZOhQGVI74bOwh5DVkjIBUWwpy2LC/Ex8TXPyNl9B5kXMrBSPhua89nrrZ+k0M49hkhMhr3fHA== 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; }