Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp645413rwr; Thu, 27 Apr 2023 06:29:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6boX2dhtjaon40J5Zvc3ZRhDWyEPiutC9z/+DPygT3IDXfmQetW3ki4KayFGFC0xn4wHj2 X-Received: by 2002:a17:90a:183:b0:236:1ec1:6d30 with SMTP id 3-20020a17090a018300b002361ec16d30mr1718524pjc.3.1682602159545; Thu, 27 Apr 2023 06:29:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682602159; cv=none; d=google.com; s=arc-20160816; b=CfbNYtqYRvd7EX9PP6Op05XlvoBlyxrkP7KZ94Iufo4gMiV4AwMuag5knIpd0rzUsf 2kxluwawoJWncIbWzoHBYcwNU34M0mW8ATCAZMAsMny7CYep+VFoibQtfVJ06htNezmN wyRTHWNYbLmD0bsAtvRyCI4k3Rnf+6MrupPP2I5d0VZSY2tmA9pPS67hmUcelLXPKEZZ Zg+jMkqctfKEECcZ4kwBvbDw8GZro7OtICQnftK54sas6gZWJ2P1QfZaU+z9XdGQU9t4 UWK4Qo2KJixgjVDVv6Q/Y4qFQgaFZpSbN0RFEKYYonhPranc9BZwxF1glXw5NZdlgloi 6pbg== 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:subject:cc:to:from:date; bh=kY4Hex0c4qQTJc6fkoh5v83kSDIh+d1XrBFLWz3Xc94=; b=G7qs9G/kcBy+2ySZcA8Jyc/wNDfZt2Te7vEdZarc86h6a3aqWjGt2/G8CGlSqD4cW+ PZmuj0OWYA6NQNx3ZY3EnXCjrOLhhJW/+v2fQ6PpyxndSnL8CAFyWVtLptFbintEx7Wr TO8SF0W7QHZrQVNhTWvmUqaOBxbSGSv3/JHJEMU6/w9SmhPvY7QwFcDulRM7pS/a5+Uv 7ImfvVl6HGOMlUwmBQllyRRM1ZcbUeK/kPOmw2VROVIehBiUcXmN/UHhC7HUqRd3oS8D iAaHY2upiODYsVSZJ8GyuUZrwokADKGCitBBoCPvRAPll/fJZDbQVMgjMlk/D9gaBlOm FWrg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b5-20020a170902e94500b0019ac13b77efsi18536488pll.158.2023.04.27.06.29.04; Thu, 27 Apr 2023 06:29:19 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243543AbjD0N0e (ORCPT + 99 others); Thu, 27 Apr 2023 09:26:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232885AbjD0N0d (ORCPT ); Thu, 27 Apr 2023 09:26:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4103449E; Thu, 27 Apr 2023 06:26:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 52F0C63D68; Thu, 27 Apr 2023 13:26:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11B31C4339B; Thu, 27 Apr 2023 13:26:29 +0000 (UTC) Date: Thu, 27 Apr 2023 09:26:28 -0400 From: Steven Rostedt To: Yafang Shao Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH bpf-next 5/6] bpf: Improve tracing recursion prevention mechanism Message-ID: <20230427092628.21fd23e4@gandalf.local.home> In-Reply-To: <20230417154737.12740-6-laoar.shao@gmail.com> References: <20230417154737.12740-1-laoar.shao@gmail.com> <20230417154737.12740-6-laoar.shao@gmail.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 Mon, 17 Apr 2023 15:47:36 +0000 Yafang Shao wrote: > diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c > index f61d513..3df39a5 100644 > --- a/kernel/bpf/trampoline.c > +++ b/kernel/bpf/trampoline.c > @@ -842,15 +842,21 @@ static __always_inline u64 notrace bpf_prog_start_time(void) > static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx) > __acquires(RCU) Because __bpf_prog_enter_recur() and __bpf_prog_exit_recur() can legitimately nest (as you pointed out later in the thread), I think my original plan is the way to go. > { > - rcu_read_lock(); > - migrate_disable(); > - > - run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); > + int bit; > > - if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) { > + rcu_read_lock(); > + bit = test_recursion_try_acquire(_THIS_IP_, _RET_IP_); > + run_ctx->recursion_bit = bit; > + if (bit < 0) { > + preempt_disable_notrace(); > bpf_prog_inc_misses_counter(prog); > + preempt_enable_notrace(); > return 0; > } > + > + migrate_disable(); Just encompass the migrate_disable/enable() with the recursion protection. That is, here add: test_recursion_release(recursion_bit); No need to save it in the run_ctx, as you can use a local variable. As I mentioned, if it passes when checking migrate_disable() it will also pass when checking around migrate_enable() so the two will still be paired properly, even if only the migrate_enable() starts recursing. bit = test_recursion_try_acquire() // OK if (bit < 0) return; migrate_disable(); test_recursion_release(bit); [..] bit = test_recursion_try_acquire() // OK migrate_enable() // traced and recurses... bit = test_recursion_try_acquire() // fails if (bit < 0) return; // returns here migrate_disable() // does not get called. The recursion around migrate_disable/enable() is needed because it's done before other checks. You can't attach the test_recursion logic to the __bpf_prog_enter/exit() routines, because those can legitimately recurse. -- Steve > + > + run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); > return bpf_prog_start_time(); > }