Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp351075rwd; Wed, 17 May 2023 20:01:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7oUA/e/eRyXdtwM8rU4S272jrt0DxLtOESwg3x4reY2GFaeI07DRevdDDLH5YN5oPJDq1N X-Received: by 2002:a05:6a20:7489:b0:104:f9d4:1423 with SMTP id p9-20020a056a20748900b00104f9d41423mr630519pzd.28.1684378913035; Wed, 17 May 2023 20:01:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684378913; cv=none; d=google.com; s=arc-20160816; b=DbfuU8hV91HQzDNsMgxOZyPJNAjXo+F8tKb0uujBDxMzt1l69oCAE8UajpqKf7Q9l5 uK95MaAVdKbbzYnIQY5CdrpkD3MmQR/76USVU1MUZOol3s2lkKqd+9v74lE9+00RwwgP 2esh2UeqxV4ip/DO9GC56kRG4Yvqe4RKqvrooOyxldACwv1pk9uzVCk1P17j+qr8F1lX 03o7TKu5+A5C/gpz0XanA8CqXmpfvPPXwGwUhVGuIjyyxXid/jE9JTIn0HLmMi90Cu7j pxYo+B4mQihjokkgQx6Ll2YvCTcJNTFPLshuhR2dBKpsbuDnhBf7GgWxCng4uoeaDfX8 YjNQ== 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=/7liFIXx8tC5EuopqHF+4OZT7BnqM1nFKp7piVkw/qM=; b=o/lB6tgL1BaDjKzs5l49BpjVyoHyITPT2yUpENmysgyHTUFvQnDgq0NL9Anxlf/8+O PA5/x/uJhL8jn0sZ4IiEgOxtr2s+CrzFAh/8jwOYsyfD4bAjuW1uXKAs2XsZ0dPJI6WW XkXtpHe8DOIzbfxPGDubSB2yqI0ge4upS+okIFHYTlOd6NJ1dYEvl63l3dKfQhSZJdKx 3aZrNOrs7vTWY4KDfDpK5AKi08pwRT/+6FWYhbQy+YwS49x1eR4kGqj9AJN43cbnED79 yeqHZoI6Mm3dxTcyUJrucr3tNHQd3nrnt42cEb2UFaBhyZAvZjTWFapH3Gr5H72pD1nr kOuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=GLLrnD3C; 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 z11-20020a6552cb000000b005303a12ee8dsi333726pgp.293.2023.05.17.20.01.38; Wed, 17 May 2023 20:01:53 -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=GLLrnD3C; 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 S229574AbjERCtt (ORCPT + 99 others); Wed, 17 May 2023 22:49:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229545AbjERCts (ORCPT ); Wed, 17 May 2023 22:49:48 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47431E5; Wed, 17 May 2023 19:49:47 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-50bc0117683so2710748a12.1; Wed, 17 May 2023 19:49:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684378186; x=1686970186; 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=/7liFIXx8tC5EuopqHF+4OZT7BnqM1nFKp7piVkw/qM=; b=GLLrnD3CmE0XvGT+JxYZ7cQgV+1xvzVZ3Txzfjp4d0gGxnjEpPejY1rn9MygPZ3+ZD I/V1uQlegefxNQba9jqi8ad8/5Yy9WkJQlhMQ6d0+huqeN5Gmbe9JxiubjKUtiEvaYX5 Gdm/xO0LN8lv4Ht86D77Xs/pQYkwA6mnZZma92DQrRYjkGCuwiizsWB9/h3mUEyUlGjE G3UM6QEPAGQwJnQNWOjO/et4vCOjmS6O3gRh8jXFuQWCC56B/iaCunmXizBsm+LgCXqd WT3d6WMhuKJBqAyIOGMy/A04umW083VL+C1uTgcEkPDv1qVysbQbNm9SyUslkbQbDYMC i1aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684378186; x=1686970186; 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=/7liFIXx8tC5EuopqHF+4OZT7BnqM1nFKp7piVkw/qM=; b=ih6rF6ZxsHwDT1uVnBb/iskPdwq36N7e4VSPPIy8E4y3Ajs5/OmG9+j5ESDk8oVwiT 4N6MeMxU2mUd7H1uG+2XWINVkn0gxHsHv22QDV+5XDbdatrs+BM2CzkdRGq+DXO3CTXC NzOm4R9xVuTF8MEnIXM3ZE9ztDAG5PCJfkj2fewqdpN/frLINsqNd90HD7wdKgz95+Fe mJRqGZS0gMrynGRo7Txl7SqSId6EhvUODMimhS3PQOBcyXR6j7tJ3//vsRS/vERBBVKG G/ecbyCiwf/nCHk43eUVwRwGbsmSCaey6f5qQz4KPLL5JIpvCQ6GtK5ZVyoFS8Rdy0sE JjVw== X-Gm-Message-State: AC+VfDyhK6yji/2AHy2vSiwbnTUK8KEuln/C7runwkS3Od9Uy92Me3Ss kPP3tEi72X1UEtTD9cd2vnCz0lpjIiiooc6WFVU= X-Received: by 2002:a05:6402:749:b0:50b:d303:d2a4 with SMTP id p9-20020a056402074900b0050bd303d2a4mr4318229edy.27.1684378185524; Wed, 17 May 2023 19:49:45 -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: Ze Gao Date: Thu, 18 May 2023 10:49:34 +0800 Message-ID: Subject: Re: [PATCH v3 2/4] fprobe: make fprobe_kprobe_handler recursion free To: Masami Hiramatsu Cc: 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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, 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 Glad to hear that, hooray! :) Thanks Ze On Wed, May 17, 2023 at 10:27=E2=80=AFPM 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. > > 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)