Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1970469rwd; Wed, 17 May 2023 04:07:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ79dvhG/PCaU+GGSrpC+kmvffNC2cnrg1xpF8j+OhTE7oa/jKfAgUm7WMdiaR/Gk44qR0JO X-Received: by 2002:a05:6870:822b:b0:187:bda6:db2d with SMTP id n43-20020a056870822b00b00187bda6db2dmr18167131oae.13.1684321674104; Wed, 17 May 2023 04:07:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684321674; cv=none; d=google.com; s=arc-20160816; b=dKQqk/ZHzau7q6Bqhconw0Me4nSiBFYmWDhHLI5FJqB1MPXEyk6xhoU8q+AlONmzFV 4RdaGlzOIE1t6dNVy9/TI2VlEvi+kkhizC8pZqpVIxBHgY5wHnpSl6uikoouEAFo2Mzj 4DXWA/FpWhmvYctmWg8wG3Hmx80CJ3ZtmB6dc/LaJjvFXuzS8ONfv76BuYGIsm+ct0a1 WM8L1mP2k8je9fIJ0XzaCwb1MUvZcO2vMhVtaDw4B5qXyYWzgaLbhgSKvBvLRqv0mN9p EqzYu1pK6WXxDstHj+/TBVoFUgxhpz/Jy/OoX015RMlRJiVp2mjkWOJebqksK02kY2Ln bpyw== 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:date:from:dkim-signature; bh=hqvkmynEV6/dEj1F324kSuUIEax/RriRwvtPl5SBD9Q=; b=e2AEUAKjCF0lfB068XiFlCsrlRsI3jdLv85ruyPtJeEMxdh5l3B6yM1uVcaxbiOEfV 4hG00N0gpDGx8FOp82OxENpcQi8+58rMFGWo/Ot7LLBsO49eLbwuL0rNhSTPuc8FIj4U N2I/VdfpXs4qcitqSTccQjBKfYSUzpvQwz1FxxvrFraY+sq7ajb9uWxD79D46feQZab6 KDETvpviOJ2hAXIJ1l9sUjnXrbcchglufBWueXx4p0y/CU4uyvOKX4wmROciLpIYe98e j23YOFoGMlHLUv+tb5j86zjMDlpSF+w1Y2VeZ7oslxjSH0Isuj+7JumAHyRE9mmQC9bY 3SyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=DOV6yM9d; 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 y14-20020a056871010e00b001925573ce30si19668057oab.232.2023.05.17.04.07.40; Wed, 17 May 2023 04:07:54 -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=DOV6yM9d; 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 S229485AbjEQKrt (ORCPT + 99 others); Wed, 17 May 2023 06:47:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229782AbjEQKrs (ORCPT ); Wed, 17 May 2023 06:47:48 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 384D72D49; Wed, 17 May 2023 03:47:47 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-30786c87cdaso560048f8f.2; Wed, 17 May 2023 03:47:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684320465; x=1686912465; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=hqvkmynEV6/dEj1F324kSuUIEax/RriRwvtPl5SBD9Q=; b=DOV6yM9dgDqxu/o3argXq7spycp9mQOqF4YFCCozRgcJGMQsomFhVXykW6RfhwpEiv /a0f2cTkEepZnT5QgNXf6RvLzzvSTWoL3P8Z7Gpx1I/muxcTBMVR7MDYXWnMXnxzmRMe VVg3i4z3QFNpTD2MMb0J7LWn3oHMsgaN+mQIG3cRjsDS48ZqfNyVAv9/qBYvzkYzyZr9 dP8SJvZrVYNW0sNEHZpQRnv+MwzSP83lXswk9OCHG95Tw24QwwkZSeYT8hX8qu/CYbaG tgOzYr1po54vgH0Ki+BM7IZ3IeaMHl/yIlAjowj8b7gZ4s1UsQWjCCTW10o3+M2iAfOp oijg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684320465; x=1686912465; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hqvkmynEV6/dEj1F324kSuUIEax/RriRwvtPl5SBD9Q=; b=crzLWt+caaxsWNoEDVl/jOmHinEqH7PdxuGrfzn9N8y9/rT1RHC7nfWs1/KHdkVUN/ g7PTTM7NHgRp3oKigOvxW5Twd+VkoKGHsO+I3884J/zP+xufwprS5XmgTcIoYpKIvA1f B22qSpnglwBMIkYLZcHz9c1QoXQejKkdmDjiOyVouDxe4c5nbXDFpkp1/OIhdCab/4bf MXWJ2ERm/hExW+7/Bi8prL9ESiaZ7IJqy/UXprJog6RlTVXJ9UaU3IrWMMIoaWNbXCA4 Mqjc2YuYdF/HPASbZYIu3R8uLsW5B4UtT+AVb61nfSi8vmhZGjANOXhDfESZ3vOz/EEV w9xA== X-Gm-Message-State: AC+VfDxLCAm0z8njI2YYPG+YZ4tfl1b29IgcTQ+iCncIFuGFYmJ8ZCU0 d68cZL4dPmLI7M3Fs3kaHHo= X-Received: by 2002:adf:f8d1:0:b0:307:8b3e:285a with SMTP id f17-20020adff8d1000000b003078b3e285amr261055wrq.67.1684320465184; Wed, 17 May 2023 03:47:45 -0700 (PDT) Received: from krava (2001-1ae9-1c2-4c00-726e-c10f-8833-ff22.ip6.tmcz.cz. [2001:1ae9:1c2:4c00:726e:c10f:8833:ff22]) by smtp.gmail.com with ESMTPSA id g3-20020a5d6983000000b002ff2c39d072sm2334322wru.104.2023.05.17.03.47.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 03:47:44 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa Date: Wed, 17 May 2023 12:47:42 +0200 To: Ze Gao , 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 , Yonghong Song , Ze Gao Subject: Re: [PATCH v3 2/4] fprobe: make fprobe_kprobe_handler recursion free Message-ID: References: <20230517034510.15639-1-zegao@tencent.com> <20230517034510.15639-3-zegao@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230517034510.15639-3-zegao@tencent.com> 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 11:45:07AM +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 beginning, > 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 *fregs) > +static inline void __fprobe_handler(unsigned long ip, unsigned long > + parent_ip, struct ftrace_ops *ops, struct ftrace_regs *fregs) > { > struct fprobe_rethook_node *fpr; > struct rethook_node *rh = NULL; > struct fprobe *fp; > void *entry_data = NULL; > - int bit, ret; > + int ret; > this change uncovered bug for me introduced by [1] the bpf's kprobe multi uses either fprobe's entry_handler or exit_handler, so the 'ret' value is undefined for return probe path and occasionally we won't setup rethook and miss the return probe we can either squash this change into your patch or I can make separate patch for that.. but given that [1] is quite recent we could just silently fix that ;-) jirka [1] 39d954200bf6 fprobe: Skip exit_handler if entry_handler returns !0 --- diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 9abb3905bc8e..293184227394 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -27,7 +27,7 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip, struct rethook_node *rh = NULL; struct fprobe *fp; void *entry_data = NULL; - int bit, ret; + int bit, ret = 0; fp = container_of(ops, struct fprobe, ops); if (fprobe_disabled(fp))