From patchwork Fri Mar 24 22:30:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13187434 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 AD188C6FD1C for ; Fri, 24 Mar 2023 22:31:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 169C06B0081; Fri, 24 Mar 2023 18:30:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0CB97900005; Fri, 24 Mar 2023 18:30:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E5E06900004; Fri, 24 Mar 2023 18:30:44 -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 D18036B0081 for ; Fri, 24 Mar 2023 18:30:44 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B4D41C0B62 for ; Fri, 24 Mar 2023 22:30:44 +0000 (UTC) X-FDA: 80605237608.05.1A53124 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf01.hostedemail.com (Postfix) with ESMTP id DC52040006 for ; Fri, 24 Mar 2023 22:30:42 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=FiFa0lj4; 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=1679697043; a=rsa-sha256; cv=none; b=OsKv6ZKanNAUs9KfoaBr1ssKN8gSd02FyoHCRO+KT9NnpYMDNdz6s4UG/F9bbFvOgilXw2 v2hgYse2TlY0MJgLdYv2TXJK/fVT2A9vfl79ZYZmWSxyyoyjjiWUvnoT7PSYswtOYAB6lA Q6thqyt7PdUAaWJIExYcLtWIM9Nm2sw= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=FiFa0lj4; 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=1679697043; 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=9hn7lg9dudknRxZST2EvcXuTZnhH2Dpn1zkQtZUpKR0=; b=8ESfPhd9HmGls0Ulh9OXfbZ6tRfaa/Rz6YaMcwLi6oDx2PVNxBGw3GeCnsOghGGxMMNwoF E/6OVtEHuLmDX0CKqDjPj8aGbAa+uQ2oCLXmAegxzKj9CY7/Rk3dQauUsDCVleYjzapZ/R lvIr0oScC6AJj844jZuyD6ze9yJkXxA= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id C98CD20FC4FA; Fri, 24 Mar 2023 15:30:41 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com C98CD20FC4FA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679697042; bh=9hn7lg9dudknRxZST2EvcXuTZnhH2Dpn1zkQtZUpKR0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FiFa0lj4u0e+Ncsfd5AUeJfL964Tk6p0G1c6Bp/6p7o7ZptOxS7NqaVNgYvxsX814 uyUEsOSMgRPHo8IBeiPvPTHbXGzKwnhAAOiKTnIoGz9esjUu721Zcbbgt6s5YAdK/B +8yreuxzE1R2hXNL6IR3qji9QLfg6iJG+rQv16ik= 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 v9 11/11] tracing/user_events: Limit global user_event count Date: Fri, 24 Mar 2023 15:30:28 -0700 Message-Id: <20230324223028.172-12-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230324223028.172-1-beaub@linux.microsoft.com> References: <20230324223028.172-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: DC52040006 X-Rspamd-Server: rspam01 X-Stat-Signature: 7uriqmbo1ji31j4owwbgxta3zyj8texb X-HE-Tag: 1679697042-623515 X-HE-Meta: U2FsdGVkX19Ezy8ieMNOIvcWssYqy5E2oIAGDkEjr7ldvYWjpoLSS8Mo6b00KxmQv3ssloCqdX6WI3HIaGxzTTTjk4ZBtyUaRCiJGVOQ9F+Ys9L/CLIuGz6A+nkwlzFsFU3H0tqMNahD29pgHi/nNvHNb46A0WOH16a/iI9A7I8M24YKvnfvjBkP9ABfR3csdYuRWebzH2NyUQFvmsK7ebXBZPkerP4FfqDxAcZMsxtIi/3v0NDkDRIzUDwvlJlpjIhC/ZyGNVE6m6PaFA3nDpsS1PoZbWtUUNSheteWwdalNY0hPLBuJEj+boBqlFdRcuK5P27BzvZnN0qPVxZmSYIAxV2dCc3DygHaxGaKXSZwcA235b/IMvwNAMQMGMIHKh1hB94K44FXT4lLVK0yRUTosXFglNqexQr7XVWwlBg7BiymMyqBksGSkcwvP5ssXWAFwPp4e40tCUzABQiUMiCFE68ifFQnkPG7KGVns5J5o54GL6RRnbhhhYUF8EtUNq5FK9ujDzb007VbrYxMqQQ4n3a5R88vZ785UvwYAOQDcqJmZy2pCt3ZFqeu5xJGtBVwIR35M6hfY92f1o/QQqPYtemmX4J0g6PN/eKSTUsqd6DKFXzzWbmq1Jony2Naff2waihueOxgr7FWH3U5AwGkRoNZImEM8jfhtaOEedEsx0jT1/7gU51DteNEGq9mEdNL+qSss4A5LStsKLd9NxPy5JkKWIp1w7lY43REoYNg3Ldl3cgzNDf40XCZ68ZcFwUcopk6qHJ7BORES2GoysxswtnSfmnLsFCzlndp2Ffe0/oygF/7igDMij3W8oBQf+ckIFCMLKs7BoqhGPCLuKYrGxpMLlFCxzu6h3mylSjMpEt8RS1Pyy0EtKJhznLCCiO/0C0QZpqq9heJAhh93nTVSlIuqePYLryXptcDJ64p6wD07hL5p0nACmxkqTHRmKnJfQ+E/zQxfQhc8bg JM4lIlU/ nYIG49fYRIxpByNr+laJMsxGfDx/T4rfgF91KPxH0fVIYetEJyIXS8cvh+2s7qtLahGKxAal17fTUAO5x0rfemCsCv/saMK7XnChjIRMmK8/N3jj6HFm98UMLS9SZ89SB/jmQQiqyJV7RRTN7Ogzki5iLOUzgKZkQ9ARO2N+nfwZIKjVfyTk7VL00oEuujvkh1Qa6nsvrPgDextu3rz/0x46+MiiMK+PvDTX2LH5XTixMsWgMvIA16epciQyAqNMr+jhxnZhidh2X6FEsh0Qp7ktmrO4lMpoAu+YHxHs5NoYrl+ihQJ6mqMnjhTTRWVzf8guDcE3eRUN6jcs= 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 222f2eb59c7c..11922f7cf496 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,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; @@ -2415,6 +2460,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; }