Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1385689rwl; Fri, 24 Mar 2023 09:49:40 -0700 (PDT) X-Google-Smtp-Source: AK7set9fkvyXyWR4qJZO4/Ab9XFsjDcvXCuDrlaNJnS9jk3Ekl4UAJBcyFBwWuyEKYFR5bzoYeDs X-Received: by 2002:a05:6a20:b70f:b0:da:318a:981f with SMTP id fg15-20020a056a20b70f00b000da318a981fmr3450820pzb.42.1679676580237; Fri, 24 Mar 2023 09:49:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679676580; cv=none; d=google.com; s=arc-20160816; b=MlqGVw/PBK2w4wQM4q3h7s4sppTpCRQkdoJfH7K07MOs/gQnLoBE+qM10xg/YzjJ6Z Mt2gKq7ciWC8w8X1xFWfoz5qkDQ17Ym1Ay5wTOMBegS5SLCI9ttmH6aD1CdZZyalP6+f 5PUY77R0ybELyL+xD/9vq+kfvVNX2Qms47CdpG9IVgCGigVt62N2PdSC7M7ASwqcNFeQ bYsPip/MdDiiZXY0uHTQ51R48ZSBBYIUE5lSMUy+qu7JU3rv4ItmulZkT8+Re4GUtCdf ECtyuIuowlnzY80GRGqU1v45QWyU4zhY8N2AwvywPkcLgDCzEg+BKXDwrN6N+/PMVuXu Q03w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-filter; bh=xxBeia2D78dBK8d70oXp4Atwc+GmrqLlCnWbFHd1K2o=; b=IuHH1Wm2/JnbmDsBqoCMSr94G0vrwiIR4faY/uVG8QKqwpgm+ML05WZJuf36J+xz8I D6pfscoIiO0OHK3qVSBNs5M5DfOEvPM+jt8Q8e5DuavfrMo5WontJCRn7BSoiZUe6BEi dCeBudyoSfJiZ/VL9V79TUSqNB14bEtgmcusdZoH+fZvrF74G3GHwdg+I6KE1/OfBuUY ykxg12MwdwMShthgAN8VVVnsf7mMFMfL+6jejHmjHcBcNa/ys7jczBXjF4ss8VnIgPXj UzV5dfTUn3i0t7B29Kht2CIADfSE6oa7dV0nRTAigLEqnsq3TVy5Dl4HPdBM3FWRLv2Q dJDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=bW5ncnm9; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n127-20020a632785000000b004fb3845932esi22334759pgn.576.2023.03.24.09.49.19; Fri, 24 Mar 2023 09:49:40 -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=bW5ncnm9; 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 S231494AbjCXQoF (ORCPT + 99 others); Fri, 24 Mar 2023 12:44:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229689AbjCXQoE (ORCPT ); Fri, 24 Mar 2023 12:44:04 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8330E113F2; Fri, 24 Mar 2023 09:44:02 -0700 (PDT) Received: from kbox (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 6BC8B20FC3DB; Fri, 24 Mar 2023 09:44:01 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6BC8B20FC3DB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679676241; bh=xxBeia2D78dBK8d70oXp4Atwc+GmrqLlCnWbFHd1K2o=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bW5ncnm9lcwFOnDxV+eVEh8UBtc2Iu7iL+S/bCe4UCuDWfojCFbM6JvkRb65/LVTa NsZm3LNC32boN3XjC5i3bQuEGAfrbYgIvC067oIT5o3bXNxoi/iTyUE82uAIUzgbZo 7cxBwsqCkY4CdBmFifyrYG1vX6fXwd+vYAHfVaOA= Date: Fri, 24 Mar 2023 09:43:53 -0700 From: Beau Belgrave To: Vlastimil Babka Cc: "Masami Hiramatsu (Google)" , rostedt@goodmis.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, linux-trace-devel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v8 11/11] tracing/user_events: Limit global user_event count Message-ID: <20230324164353.GA1790@kbox> References: <20230221211143.574-1-beaub@linux.microsoft.com> <20230221211143.574-12-beaub@linux.microsoft.com> <20230324081824.b917c2944da217e5239e1223@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 On Fri, Mar 24, 2023 at 09:54:48AM +0100, Vlastimil Babka wrote: > On 3/24/23 01:18, Masami Hiramatsu (Google) wrote: > > Hi Beau, > > > > On Tue, 21 Feb 2023 13:11:43 -0800 > > Beau Belgrave wrote: > > > >> 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. > > > > sysctl is good to me, but would we really need the kernel parameter? > > The user_events starts using when user-space is up, so I think setting > > the limit with sysctl is enough. > > > > BTW, Vlastimil tried to add 'sysctl.*' kernel parameter support(*). If we > > need a kernel cmdline support, I think this is more generic way. But it > > seems the discussion has been stopped. > > It was actually merged in 5.8. So sysctl should be sufficient with that. > But maybe it's weird to start adding sysctls, when the rest of tracing > tunables is AFAIK under /sys/kernel/tracing/ ? > During the TraceFS meetings Steven runs I was asked to add a boot parameter and sysctl for user_events to limit the max. To me, it seems when user_events moves toward namespace awareness sysctl might be easier to use from within a namespace to turn knobs. Happy to change to whatever, but I want to see Steven and Masami agree on the approach before doing so. Steven, do you agree with Masami to move to just sysctl? Thanks, -Beau > > > (*) https://patchwork.kernel.org/project/linux-mm/patch/20200427180433.7029-2-vbabka@suse.cz/ > > > > Thank you, > > > >> > >> 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; > >> } > >> > >> -- > >> 2.25.1 > >> > > > >