Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp410646rwr; Thu, 27 Apr 2023 03:11:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4w9Xr95apE4or5X/C2YiP+B3R457M3UeM6rUltAryPzglUEB7jsu6q9KzrMhEcv1UgcWXn X-Received: by 2002:a05:6a00:1acb:b0:63f:21e:cad8 with SMTP id f11-20020a056a001acb00b0063f021ecad8mr1529806pfv.3.1682590293805; Thu, 27 Apr 2023 03:11:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682590293; cv=none; d=google.com; s=arc-20160816; b=j4ojiAdnIsMyJownfVMwS7JS9O2BxpiH8PKhCaTYd5YSuoGjGoqZ1bpAyx5bd812cK 8fVSHmed1EWdGbaDn2hoYUOaGOvyu8seVylsfOkb3qiGzwJSKlx4cvH30tdg1XYgJJNX 5UewEP5/nyDNOI87dnTD8LtmiRACr85vHyUdlpNgARpFizpqOa4qC4Uj7wn7KO86XMzw 1xJrpVOTrztc0qmlPpWwJNk4xD1jbGQrbsMY7rZUvJUrT4+lfYEnRLmpFR/8BUA0ZaAY +3jxEmUFFMEJkgOAZWyt72ih7jbkwisUltUnaDBarsii4GS6l8fe+Px6KcBR4O052ocn FuBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=5H4urfMx4Jh2e9EbQKPkIyXjRe2bh/qiE1leyX/3eWw=; b=FBkcn4SOdAs4KQjNq57KLo6m8bdgKkyIeKvAcB3p8Kb7PftuGPnc1k9N1X6BmCD9kY O2n67Ym3oEhilDv0Fj9/tiQ0xfwJFdGHG+KA9cXQlrunOPfL7Y+HGYnhRUgszbx5DvIa 6L5wSYimT/YWAdnukM4R6QaDbZEsM7Twtaaq9ocDXCYvgJJSiu3gbXl8SpmsQmw9WWle NJbYJ0n5a57an6kR29xhgdJ/ZXXi4xtJPIftLUxhZQLrWUU94DFSKlCI/jZFGWCfc/MD RqlvOYqpoIqFWkgJ4qzRig6Hr9XlGTffMP6dtBnG4qBYQDn6sCw01oqKVUv6dlQXH7xI cXqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=ZZ4KYW42; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r17-20020aa79891000000b0063b5875dd22si15786536pfl.308.2023.04.27.03.11.19; Thu, 27 Apr 2023 03:11:33 -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=@gmail.com header.s=20221208 header.b=ZZ4KYW42; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243459AbjD0J6b (ORCPT + 99 others); Thu, 27 Apr 2023 05:58:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243445AbjD0J6Z (ORCPT ); Thu, 27 Apr 2023 05:58:25 -0400 Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C3171BC5; Thu, 27 Apr 2023 02:58:24 -0700 (PDT) Received: by mail-qv1-xf2a.google.com with SMTP id 6a1803df08f44-5ef8aaf12bdso35177926d6.3; Thu, 27 Apr 2023 02:58:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682589503; x=1685181503; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5H4urfMx4Jh2e9EbQKPkIyXjRe2bh/qiE1leyX/3eWw=; b=ZZ4KYW4275a1rgyrsslbI7/3BzMp2MIeDquIgk3X8jaeJ1B+fKh7sTOfSzsiodmmP9 pO/XqdQWyUwE60nK72vKdRvGvU935a23v2EmrkspzAptxdj+eds0XbQxbQs3vfElNMe7 AH7gOE4+u691GNUIBO10Vuuqy7G1jhEaQcmQoSFWVAdkWCDDLdxLyOc7btvypV3VoRJ/ AnzZIn7D3C5LZ/HxVZRieXTG3Rb7DQRtSU03RIXFKTMCVKOSfdvu8CyPhsj1HJ0YWwEH q6yz8sL4uDFcSJ4elutLXi7PSNC2oxRn1UFlacEzUgHr06JClFNv+KA0bdH8Jj8qVuUe cDEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682589503; x=1685181503; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5H4urfMx4Jh2e9EbQKPkIyXjRe2bh/qiE1leyX/3eWw=; b=Xo0F8GBuZGkHnTVFbSZCh5oGpcS9x1DoiymVx01tKSeAzxsR3p0nndSOzL1UzourKp 7hL6WayRXonbo21hhlDenynjZDVIHr9GNAEfAbs62e0wBbHiH6wPKrN1iAzMVdGbbCdP t/L4HYVGP4BwybmiOz9hJQMyN/JYyrSHsLNCr782Nf1wv8CdnGzZG7SdQb3r1LxjyGdp k1AtVR3u5NzYOZvefIYXoeiutiERfdHIRG1ZUCltlJxrknBTXjz2QUop1nySjAFI2jAz lFnVh2Qz0wa07+BB7Z3vFi0BSgQxax510LTUuUwGRtd+racpOuPGc00EabOt4lo00Ktu cmHA== X-Gm-Message-State: AC+VfDySbNeZs4/kg//hugK3JT2ZuJ2go39F/01Q8pIEl/un8iRrTPbG PKUu/aBRg4Y6CjObUTovQJ62imjVX3henF2x/easWpTcufBvZg== X-Received: by 2002:a05:6214:2589:b0:5f1:6bee:f58e with SMTP id fq9-20020a056214258900b005f16beef58emr885006qvb.35.1682589503483; Thu, 27 Apr 2023 02:58:23 -0700 (PDT) MIME-Version: 1.0 References: <20230417154737.12740-1-laoar.shao@gmail.com> <20230417154737.12740-6-laoar.shao@gmail.com> <20230417201457.c43xfcukjzm4u6vx@dhcp-172-26-102-232.dhcp.thefacebook.com> <20230424174049.1c9e54dd@rorschach.local.home> In-Reply-To: <20230424174049.1c9e54dd@rorschach.local.home> From: Yafang Shao Date: Thu, 27 Apr 2023 17:57:47 +0800 Message-ID: Subject: Re: [PATCH bpf-next 5/6] bpf: Improve tracing recursion prevention mechanism To: Steven Rostedt Cc: Alexei Starovoitov , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Masami Hiramatsu , bpf , linux-trace-kernel@vger.kernel.org, LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 Tue, Apr 25, 2023 at 5:40=E2=80=AFAM Steven Rostedt wrote: > > On Wed, 19 Apr 2023 15:46:34 -0700 > Alexei Starovoitov wrote: > > > No. Just one prog at entry into any of the kernel functions > > and another prog at entry of funcs that 1st bpf prog called indirectly. > > Like one prog is tracing networking events while another > > is focusing on mm. They should not conflict. > > You mean that you have: > > function start: > __bpf_prog_enter_recur() > bpf_program1() > __bpf_prog_enter_recur() > bpf_program2(); > __bpf_prog_exit_recur() > __bpf_prog_exit_recur() > > rest of function > > That is, a bpf program can be called within another bpf pogram between > the prog_enter and prog_exit(), that is in the same context (normal, > softirq, irq, etc)? > Right, that can happen per my verification. Below is a simple bpf program to verify it. struct { __uint(type, BPF_MAP_TYPE_LPM_TRIE); __type(key, __u64); __type(value, __u64); __uint(max_entries, 1024); __uint(map_flags, BPF_F_NO_PREALLOC); } write_map SEC(".maps"); __u64 key; SEC("fentry/kernel_clone") int program1() { __u64 value =3D 1; bpf_printk("before update"); // It will call trie_update_elem and thus trigger program2. bpf_map_update_elem(&write_map, &key, &value, BPF_ANY); __sync_fetch_and_add(&key, 1); bpf_printk("after update"); return 0; } SEC("fentry/trie_update_elem") int program2() { bpf_printk("trie_update_elem"); return 0; } The result as follows, kubelet-203203 [018] ....1 9579.862862: __bpf_prog_enter_recur: __bpf_prog_enter_recur kubelet-203203 [018] ...11 9579.862869: bpf_trace_printk: before update kubelet-203203 [018] ....2 9579.862869: __bpf_prog_enter_recur: __bpf_prog_enter_recur kubelet-203203 [018] ...12 9579.862870: bpf_trace_printk: trie_update_elem kubelet-203203 [018] ....2 9579.862870: __bpf_prog_exit_recur: __bpf_prog_exit_recur kubelet-203203 [018] ...11 9579.862870: bpf_trace_printk: after update kubelet-203203 [018] ....1 9579.862871: __bpf_prog_exit_recur: __bpf_prog_exit_recur Note that we can't trace __bpf_prog_enter_recur and __bpf_prog_exit_recur, so we have to modify the kernel to print them. > The protection is on the trampoline where the bpf program is called. > Not sure how ftrace can stop BPF or BPF stop ftrace, unless bpf is > tracing a ftrace callback, or ftrace is tracing a bpf function. > > -- Steve --=20 Regards Yafang