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: 76321 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp56577vqo; Tue, 28 Mar 2023 17:13:05 -0700 (PDT) X-Google-Smtp-Source: AKy350ai3PEnnd/O89G6R7v9HtiipuY4staVdZFcph43et0vew+lubmkJHpluP3DcTt7U0Tl7iPb X-Received: by 2002:a17:902:c94e:b0:1a1:ab85:1e1e with SMTP id i14-20020a170902c94e00b001a1ab851e1emr405570pla.22.1680048785599; Tue, 28 Mar 2023 17:13:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680048785; cv=none; d=google.com; s=arc-20160816; b=oCW9LCrCXnDi8QDPDD7gSHWq17wDWnFUJ3TmM6FivTNYYIcQm0Q5MzxGQXfrrzT5EN 0pqHxQfCsrZ6WdhrQuPSlHRLpEvS86/tde+vl5bo83quzJk4G6or3vyCFRrhxpXjq5w+ Z5Yhobc5t50jXNdzS4PH/CvLo3HnbUQFrdfuM7Wb0xDcEKOi2E5OtUBMoFt8O33jXIQD 3U5NrN3qXsJFPNvU7Tier+H3AkzgxiDdlMxvjvp7m52jAWitSeoFoVbrAAEI8eT4fsd5 UneFYXq0WcqPlpML7g33wBHCy3n2GGi/PkyaUumgWMBb259kEHbWtOz4OyI5/EYx3uQ1 YOoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=xpyxft+n8GA+tebj3uBF7xj0cPJnmjMw5o/uJtPt8qA=; b=0YENoJJzPoF9d8h4xRGF+th+3rkq7jYK3E7Fkc0FSaqP1uBubIOfRwol7izZxWbFAG dL001LKI+ZgAkF2Fk31+/DzVfzPF5P8cMB6GlFUskZflsA7EbfyON2aLB1EnRhGcAX7t TBfy9yxLh2rZTSz32Oz/HrYzhv9irYIRrLxDwKLLlxb5Zd4BBsyfQ+A262qEqS/S7dA5 dewb2vATdo2iBafYLkcToVSPf/m6ubyd8O5Ef9E/5V+Mfgrzc+NNOxTLp7ort8bATTle vkKDIhJGU1SIhgaJQcvEXHczPGQwzJ1kqvHyZ3VIVuYZhwgq65Nivk/flOfISWMhqBnk 1bPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=nOsuB16y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n8-20020a170902d2c800b0019a95ab6b4esi32789393plc.407.2023.03.28.17.12.53; Tue, 28 Mar 2023 17:13:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=nOsuB16y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229873AbjC1Xw7 (ORCPT + 99 others); Tue, 28 Mar 2023 19:52:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229735AbjC1Xwa (ORCPT ); Tue, 28 Mar 2023 19:52:30 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 66B7F2D5A; Tue, 28 Mar 2023 16:52:29 -0700 (PDT) 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-Spam-Status: No, score=-17.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_PASS,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761658835733126468?= X-GMAIL-MSGID: =?utf-8?q?1761658835733126468?= 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; }