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; }