Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp249252rwd; Wed, 17 May 2023 18:03:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4KF5T84GDslqpies4jkM9Fedpesctepwmz70KUfj/kiXv/SLcE8CjILvEcutfgRH+VeTu5 X-Received: by 2002:a05:6a20:60f:b0:107:35ed:285c with SMTP id 15-20020a056a20060f00b0010735ed285cmr201610pzl.32.1684371836707; Wed, 17 May 2023 18:03:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684371836; cv=none; d=google.com; s=arc-20160816; b=b2kqRhqLGI57+ucuju44xVJ5+X+GcsNmCX7vWdHp8P4WgpXtL971HwxyeGqm9Y7U56 xAIPDCJ44hcYcAwEbbA1xHw9qJ0nZOGVbSIqZU4d6caMNDdCqhcQMe0IkfogWQL20Y8W EcTuhHF/evsZ4w9Xc81iY7YTMJm+aU38is6fbXNUOptquO8kCAbXfDb2N7Hug7OtOpki LuBOoY8G8JL9KXC2HxtgV6+nMT7IpN6i2q3LdcEK4qslyrAKTUDR/3BC0so08UWZLq1N bwUM9/s7CwiGoDlILCTqNb7dZz2KdAjNBdUDfl1u3+oxwnaY/2HgbCCy/abmWsypbHvR rYSw== 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=Oh43Wrfv+5aBpD3zk+YIOr3pgIKi6Fm6XvjoyCfmrss=; b=QdCuRra03cy4bVpq85UxETamqlKvFhbLWPV3jzIg8jKHO3TrdGK+Pd5DR58K5hLSQb /HbAzL+Qh5pC2UQu44m2MDgJVL1va9rjXWiBZ/mjuDy3oE4YXPyBXWWLZcXMCScx8loE hzZ7VI1e9YKBN5eqtyKg6RXZodzSMQ1WyOsCFbIXnKn6/ZFS7Bi1Mbh5JFhTCd/A5svx dErA2D5ugwJ4Cc49KmvH4I4m97MoPTpWdzdjnrNTdKAEghNHcse8fpTWhlgqyA3nwfTa agXBc/vgHx3yP4nLbsj8btMbQhcpS5flt7E2omz/rvdpiFYW4wP94qhMjAv1AIEijpke 7PCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="O2q6Qm5/"; 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 i3-20020a636d03000000b0050f692470fasi145767pgc.301.2023.05.17.18.03.44; Wed, 17 May 2023 18:03:56 -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="O2q6Qm5/"; 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 S229525AbjERARE (ORCPT + 99 others); Wed, 17 May 2023 20:17:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjERARD (ORCPT ); Wed, 17 May 2023 20:17:03 -0400 Received: from mail-ej1-x634.google.com (mail-ej1-x634.google.com [IPv6:2a00:1450:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DC87E8; Wed, 17 May 2023 17:17:01 -0700 (PDT) Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-96598a7c5e0so227321466b.3; Wed, 17 May 2023 17:17:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684369020; x=1686961020; 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=Oh43Wrfv+5aBpD3zk+YIOr3pgIKi6Fm6XvjoyCfmrss=; b=O2q6Qm5/BCw7eL+R4iqW2KRYtOD7PxCsAMTNdmblRTQBQ9SXxj97E+uiLaHvdw+0vH IZAgUyLD9tdpA6WzvAnycAcJlidqsKRKjEyDwYPcgFRoiRvn+aGoAETE0zL7U0uecutc llNmsUorv3L9LvscGZeLrSn73CHxCEwpMLc242B+rU5ye9EYoqGyqECI6UmFyVNI137l BrXk4d74hq54C4WH4aTFrG6aAlMIlKYkIqfTmGGIoIU7n6R2fgdc8MiqqwfXql+JUk9B mbUicBybFzH8SUZx97fcymQ4dD0Koiso43k2URk/mqAAZSJcQr2pCjX4Y8H2fcQR9saN B11g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684369020; x=1686961020; 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=Oh43Wrfv+5aBpD3zk+YIOr3pgIKi6Fm6XvjoyCfmrss=; b=hGWaBkA9Nl8f1lfmM8bb2W3g+Rw7y8f1R0wFaQ+vb0ETaUqBomKp2BhZ9XCYeeShmv Jazzn3PejGSIMeDaydMaQ9qm3hGP9BlqWmos1au5TYk+mLopHhSTaIxmUKks0sM0eAp+ DR3X8muvNP4YpUuDE3lMMlo+82nbq2uMusUmpDO2ZkNyqKF9cc+z0cWackSUkkbnbuR/ 0jFOPB3k1PuY6l7f8JMRHLGFA/XGOnU96qid5PCQcrrXFKpEEClz0lHG5b6SsmOMSDPQ 1YbNSJK7BTEE9RUsz+f+AZGsWm8PksfzON00kY/WgGKWtGsqr04QfPFnWAJA1PS+RHFv ohwg== X-Gm-Message-State: AC+VfDwzpZhuZhgNDG5lhabffeZGEEmWWNpF5YSdiF9Ld5xQXDyjImO5 SrTLOL1WF4wAQKCFQchCMvjaKFBykA99Jm1NztQ= X-Received: by 2002:a17:907:7250:b0:966:b4b:eb0 with SMTP id ds16-20020a170907725000b009660b4b0eb0mr42160555ejc.30.1684369019531; Wed, 17 May 2023 17:16:59 -0700 (PDT) MIME-Version: 1.0 References: <20230517034510.15639-1-zegao@tencent.com> <20230517034510.15639-3-zegao@tencent.com> <20230517232751.09126a6cec8786a954e54bcf@kernel.org> In-Reply-To: <20230517232751.09126a6cec8786a954e54bcf@kernel.org> From: Andrii Nakryiko Date: Wed, 17 May 2023 17:16:47 -0700 Message-ID: Subject: Re: [PATCH v3 2/4] fprobe: make fprobe_kprobe_handler recursion free To: Masami Hiramatsu Cc: Ze Gao , Steven Rostedt , Albert Ou , Alexander Gordeev , Alexei Starovoitov , Borislav Petkov , Christian Borntraeger , Dave Hansen , Heiko Carstens , "H. Peter Anvin" , Ingo Molnar , Palmer Dabbelt , Paul Walmsley , Sven Schnelle , Thomas Gleixner , Vasily Gorbik , x86@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Conor Dooley , Jiri Olsa , Yonghong Song , Ze Gao 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 Wed, May 17, 2023 at 7:28=E2=80=AFAM Masami Hiramatsu wrote: > > On Wed, 17 May 2023 11:45:07 +0800 > Ze Gao wrote: > > > Current implementation calls kprobe related functions before doing > > ftrace recursion check in fprobe_kprobe_handler, which opens door > > to kernel crash due to stack recursion if preempt_count_{add, sub} > > is traceable in kprobe_busy_{begin, end}. > > > > Things goes like this without this patch quoted from Steven: > > " > > fprobe_kprobe_handler() { > > kprobe_busy_begin() { > > preempt_disable() { > > preempt_count_add() { <-- trace > > fprobe_kprobe_handler() { > > [ wash, rinse, repeat, CRASH!!! ] > > " > > > > By refactoring the common part out of fprobe_kprobe_handler and > > fprobe_handler and call ftrace recursion detection at the very beginnin= g, > > the whole fprobe_kprobe_handler is free from recursion. > > > > Signed-off-by: Ze Gao > > Acked-by: Masami Hiramatsu (Google) > > Link: https://lore.kernel.org/linux-trace-kernel/20230516071830.8190-3-= zegao@tencent.com > > --- > > kernel/trace/fprobe.c | 59 ++++++++++++++++++++++++++++++++----------- > > 1 file changed, 44 insertions(+), 15 deletions(-) > > > > diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c > > index 9abb3905bc8e..097c740799ba 100644 > > --- a/kernel/trace/fprobe.c > > +++ b/kernel/trace/fprobe.c > > @@ -20,30 +20,22 @@ struct fprobe_rethook_node { > > char data[]; > > }; > > > > -static void fprobe_handler(unsigned long ip, unsigned long parent_ip, > > - struct ftrace_ops *ops, struct ftrace_regs *fr= egs) > > +static inline void __fprobe_handler(unsigned long ip, unsigned long > > + parent_ip, struct ftrace_ops *ops, struct ftrace_regs *fr= egs) > > OK, I picked up this series to probes/fixes. Note that I fixed this line > because the "unsigned long parent_ip" was split into 2 lines. > Hey Masami, Regarding [0], I was bisecting BPF CI failures related to multi-kprobes, and it turned out that [0] is the fix we need. It would be great if you can make sure this fix gets into Linus' tree ASAP, so that we can get it back into bpf/bpf-next trees and fix BPF selftests for everyone (we mitigated this for BPF CI as a temporary workaround for now). Thanks! [0] https://lore.kernel.org/all/168100731160.79534.374827110083836722.stg= it@devnote2/ > Thank you, > > > > { > > struct fprobe_rethook_node *fpr; > > struct rethook_node *rh =3D NULL; > > struct fprobe *fp; > > void *entry_data =3D NULL; > > - int bit, ret; > > + int ret; > > > > fp =3D container_of(ops, struct fprobe, ops); > > - if (fprobe_disabled(fp)) > > - return; > > - > > - bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > > - if (bit < 0) { > > - fp->nmissed++; > > - return; > > - } > > > > if (fp->exit_handler) { > > rh =3D rethook_try_get(fp->rethook); > > if (!rh) { > > fp->nmissed++; > > - goto out; > > + return; > > } > > fpr =3D container_of(rh, struct fprobe_rethook_node, node= ); > > fpr->entry_ip =3D ip; > > @@ -61,23 +53,60 @@ static void fprobe_handler(unsigned long ip, unsign= ed long parent_ip, > > else > > rethook_hook(rh, ftrace_get_regs(fregs), true); > > } > > -out: > > +} > > + > > +static void fprobe_handler(unsigned long ip, unsigned long parent_ip, > > + struct ftrace_ops *ops, struct ftrace_regs *fregs) > > +{ > > + struct fprobe *fp; > > + int bit; > > + > > + fp =3D container_of(ops, struct fprobe, ops); > > + if (fprobe_disabled(fp)) > > + return; > > + > > + /* recursion detection has to go before any traceable function an= d > > + * all functions before this point should be marked as notrace > > + */ > > + bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > > + if (bit < 0) { > > + fp->nmissed++; > > + return; > > + } > > + __fprobe_handler(ip, parent_ip, ops, fregs); > > ftrace_test_recursion_unlock(bit); > > + > > } > > NOKPROBE_SYMBOL(fprobe_handler); > > > > static void fprobe_kprobe_handler(unsigned long ip, unsigned long pare= nt_ip, > > struct ftrace_ops *ops, struct ftrace_r= egs *fregs) > > { > > - struct fprobe *fp =3D container_of(ops, struct fprobe, ops); > > + struct fprobe *fp; > > + int bit; > > + > > + fp =3D container_of(ops, struct fprobe, ops); > > + if (fprobe_disabled(fp)) > > + return; > > + > > + /* recursion detection has to go before any traceable function an= d > > + * all functions called before this point should be marked as not= race > > + */ > > + bit =3D ftrace_test_recursion_trylock(ip, parent_ip); > > + if (bit < 0) { > > + fp->nmissed++; > > + return; > > + } > > > > if (unlikely(kprobe_running())) { > > fp->nmissed++; > > return; > > } > > + > > kprobe_busy_begin(); > > - fprobe_handler(ip, parent_ip, ops, fregs); > > + __fprobe_handler(ip, parent_ip, ops, fregs); > > kprobe_busy_end(); > > + ftrace_test_recursion_unlock(bit); > > } > > > > static void fprobe_exit_handler(struct rethook_node *rh, void *data, > > -- > > 2.40.1 > > > > > -- > Masami Hiramatsu (Google) >