Message ID | 20230516071830.8190-3-zegao@tencent.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp245847vqo; Tue, 16 May 2023 00:45:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5DRmna2aGo96wXt3cFvUT8bolSIVgyQoPeJU+Tk0aS0QkRv7wXuO1pwtA2Ug01tlg3FFo0 X-Received: by 2002:a05:6a00:15d5:b0:64c:a554:f577 with SMTP id o21-20020a056a0015d500b0064ca554f577mr7942542pfu.11.1684223143731; Tue, 16 May 2023 00:45:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684223143; cv=none; d=google.com; s=arc-20160816; b=j8R5s0LNJHBaIalktnV/6QptLR5amcsOdKtOc3JTedvLM/Z8dJ6yoEdO23bhtzJl5c 5KhwJLKaK02ZU7Xe4ePCFm0Zadn5adcX7pK/jiqy7+uAFFqMIELOEXqkmLbGl3w05hfb dWXuvtNVZSdkM/B2Ts+aqD7T/ibdHCwarRH1i0MOz0J9Q3JyEM2WXlitvCx2l8Mk7M1x U3h9YthyZm4Vp8ZXmsDpB+uryQMlOuI8xlsWG+ltlrBS+/lgbu0LriNQeMp6aseI1uiS 8nafHk/SG9boIb/L91+g6W7F7r91pRF3WOu75hYf2am/uhxMbO1YtyysRgLlFlPG8mjT MAoA== 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:date:subject:cc:to:from :dkim-signature; bh=jr5NPUEwkf/u+a01SSpONco/9bXEcFxv1oXkCOl3FZE=; b=EpZnndgTNrI55KJa5zYzu7h6/ECR7b/q72MUNopKFskJmXYYxrtxJh2EAjro2oI0Pg 1Q2WMmCD78kvPpvihocmpkmrm7CCDddfCeZXG20qc1BdRdaU1hegAhb1g7Ip7Yy6r7g/ tQrbtvdN4nlYKgiwOf83k6GB5oIaj7NmuP6nRkoCgfUUV+pyq9XcE1zXXQkmyDw0eWDi 424uOMctyNXtiw6juEkEmNii0mm4s2sKoQuGKvZGPmtHQEkWUoi2drDXIbvZ8Pr9egxL TBQme+fT3CSzPSOxJraCl3K9CWk/i/pFsU/sfq3e/xFPgy6nJzfCqZtuRB6rAP/JgZpE MIsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=EETW6bHq; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b201-20020a621bd2000000b00643a695e976si18765167pfb.291.2023.05.16.00.45.29; Tue, 16 May 2023 00:45:43 -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=EETW6bHq; 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 S230352AbjEPHTV (ORCPT <rfc822;peekingduck44@gmail.com> + 99 others); Tue, 16 May 2023 03:19:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231251AbjEPHTR (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 16 May 2023 03:19:17 -0400 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2689F46AD; Tue, 16 May 2023 00:19:14 -0700 (PDT) Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-5304913530fso7345378a12.0; Tue, 16 May 2023 00:19:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684221553; x=1686813553; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jr5NPUEwkf/u+a01SSpONco/9bXEcFxv1oXkCOl3FZE=; b=EETW6bHqHomRMsgD7YwoEi5wGWOvuvUQPS5Ju99GCoG1L2KVXB02G3RhY7foR5wcxT 9qk2729vqdHUBs/pd2pWIDxJQ41HkdsgjKd/SkBEF3N5nQkqWYHNMhL/Acajd5GJBRGN Ghq0SqII6TJVkjpQH3NQOcHU8INAKaI6o0N7NPDp3ItBsuJOavR8tQtyLVyQpkYcovQe YZuH5S/jLE9FPzRN5NQwbaHKMJ8KfENjGGKqF9yZk5RxPhjFfZRxnu+CRh4xIA450mDK yx3mN/9CHAvrI7fY45zdVHWZcsvNx7rGyQiP7NBrqlFlf8g/w5bATrXflh9PrNZ6n4cy NdPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684221553; x=1686813553; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jr5NPUEwkf/u+a01SSpONco/9bXEcFxv1oXkCOl3FZE=; b=Gh6JluKcZjkiXjM4gwvn7pqGPzcUS0MaU1hgof+eKQvWDHfImnbyuzfWmkdvuP7j49 kgJ/BqzXQBi+CZzF44i/7AGMkPP9fL20Y+8Bo6eI4ASeLjoAwQLTnSSwiArwDwNi06TI 0bzvYCji8lUINqQvfZwLlwhyLZyj70UG1DeBHddHWPgc83fSuSI9/EI5nvEQAmkETYv5 bCxkd0hgUOGEkmEaupYvXnI9Lx/w+UZ6aMJGvIpcPweJDSMVi2tbB+DyTyiaKEm+NDIn wI10po+RklkVOCndQjGLSBGmscIGrTs0cEbnwTRwE1TRTozxY7u+VIyO1YvZRvE3ygs0 sYoQ== X-Gm-Message-State: AC+VfDzPK0Wmcf9ys+zGFMTcq7wAoaeqh2Cmmce62w1/TOxddxrCDWxM ZDwLl2SEXVslftf1PQ2ciDE= X-Received: by 2002:a05:6a21:6da3:b0:101:438d:2567 with SMTP id wl35-20020a056a216da300b00101438d2567mr31331268pzb.31.1684221553498; Tue, 16 May 2023 00:19:13 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.15]) by smtp.googlemail.com with ESMTPSA id 17-20020aa79251000000b006468222af91sm12849830pfp.48.2023.05.16.00.19.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 00:19:13 -0700 (PDT) From: Ze Gao <zegao2021@gmail.com> X-Google-Original-From: Ze Gao <zegao@tencent.com> To: Steven Rostedt <rostedt@goodmis.org>, Masami Hiramatsu <mhiramat@kernel.org> Cc: Albert Ou <aou@eecs.berkeley.edu>, Alexander Gordeev <agordeev@linux.ibm.com>, Alexei Starovoitov <ast@kernel.org>, Borislav Petkov <bp@alien8.de>, Christian Borntraeger <borntraeger@linux.ibm.com>, Dave Hansen <dave.hansen@linux.intel.com>, Heiko Carstens <hca@linux.ibm.com>, "H. Peter Anvin" <hpa@zytor.com>, Ingo Molnar <mingo@redhat.com>, Palmer Dabbelt <palmer@dabbelt.com>, Paul Walmsley <paul.walmsley@sifive.com>, Sven Schnelle <svens@linux.ibm.com>, Thomas Gleixner <tglx@linutronix.de>, Vasily Gorbik <gor@linux.ibm.com>, x86@kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, Conor Dooley <conor@kernel.org>, Jiri Olsa <jolsa@kernel.org>, Yonghong Song <yhs@fb.com>, Ze Gao <zegao@tencent.com> Subject: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free Date: Tue, 16 May 2023 15:18:28 +0800 Message-Id: <20230516071830.8190-3-zegao@tencent.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230516071830.8190-1-zegao@tencent.com> References: <20230516071830.8190-1-zegao@tencent.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766035967305775675?= X-GMAIL-MSGID: =?utf-8?q?1766035967305775675?= |
Series |
Make fprobe + rethook immune to recursion
|
|
Commit Message
Ze Gao
May 16, 2023, 7:18 a.m. UTC
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.
Refactor the common part out of fprobe_kprobe_handler and fprobe_
handler and call ftrace recursion detection at the very beginning,
so that the whole fprobe_kprobe_handler is free from recursion.
Signed-off-by: Ze Gao <zegao@tencent.com>
---
kernel/trace/fprobe.c | 59 ++++++++++++++++++++++++++++++++-----------
1 file changed, 44 insertions(+), 15 deletions(-)
Comments
On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote: > +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 = container_of(ops, struct fprobe, ops); > + if (fprobe_disabled(fp)) > + return; > + > + /* recursion detection has to go before any traceable function and > + * all functions before this point should be marked as notrace > + */ > + bit = 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 parent_ip, > struct ftrace_ops *ops, struct ftrace_regs *fregs) > { > - struct fprobe *fp = container_of(ops, struct fprobe, ops); > + struct fprobe *fp; > + int bit; > + > + fp = container_of(ops, struct fprobe, ops); > + if (fprobe_disabled(fp)) > + return; > + > + /* recursion detection has to go before any traceable function and > + * all functions called before this point should be marked as notrace > + */ > + bit = ftrace_test_recursion_trylock(ip, parent_ip); > + if (bit < 0) { > + fp->nmissed++; > + return; > + } Please don't use this comment style; multi line comments go like: /* * Multi line comment ... * ... is symmetric. */ Same for your next patch.
On Tue, May 16, 2023 at 03:18:28PM +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. Which preempt_count*() are you referring to? The ones you just made _notrace in the previous patch?
Thanks for pointing this out, I'll get it all fixed ASAP. Regards, Ze On Tue, May 16, 2023 at 5:16 PM Peter Zijlstra <peterz@infradead.org> wrote: > > On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote: > > > +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 = container_of(ops, struct fprobe, ops); > > + if (fprobe_disabled(fp)) > > + return; > > + > > + /* recursion detection has to go before any traceable function and > > + * all functions before this point should be marked as notrace > > + */ > > + bit = 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 parent_ip, > > struct ftrace_ops *ops, struct ftrace_regs *fregs) > > { > > - struct fprobe *fp = container_of(ops, struct fprobe, ops); > > + struct fprobe *fp; > > + int bit; > > + > > + fp = container_of(ops, struct fprobe, ops); > > + if (fprobe_disabled(fp)) > > + return; > > + > > + /* recursion detection has to go before any traceable function and > > + * all functions called before this point should be marked as notrace > > + */ > > + bit = ftrace_test_recursion_trylock(ip, parent_ip); > > + if (bit < 0) { > > + fp->nmissed++; > > + return; > > + } > > Please don't use this comment style; multi line comments go like: > > /* > * Multi line comment ... > * ... is symmetric. > */ > > Same for your next patch.
Precisely, these that are called within kprobe_busy_{begin, end}, which the previous patch does not resolve. I will refine the commit message to make it clear. FYI, details can checked out here: Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/ Regards, Ze On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote: > > On Tue, May 16, 2023 at 03:18:28PM +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. > > Which preempt_count*() are you referring to? The ones you just made > _notrace in the previous patch?
Sorry for paste the wrong link, it's this one instead: Link: https://lore.kernel.org/bpf/20230513001757.75ae0d1b@rorschach.local.home/ It's the original discussions of this problem. Regards, Ze On Tue, May 16, 2023 at 5:47 PM Ze Gao <zegao2021@gmail.com> wrote: > > Precisely, these that are called within kprobe_busy_{begin, end}, > which the previous patch does not resolve. > I will refine the commit message to make it clear. > > FYI, details can checked out here: > Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/ > > Regards, > Ze > > On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote: > > > > On Tue, May 16, 2023 at 03:18:28PM +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. > > > > Which preempt_count*() are you referring to? The ones you just made > > _notrace in the previous patch?
On Tue, 16 May 2023 17:47:52 +0800 Ze Gao <zegao2021@gmail.com> wrote: > Precisely, these that are called within kprobe_busy_{begin, end}, > which the previous patch does not resolve. Note that kprobe_busy_{begin,end} don't need to use notrace version because kprobe itself prohibits probing on preempt_count_{add,sub}. Thank you, > I will refine the commit message to make it clear. > > FYI, details can checked out here: > Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/ > > Regards, > Ze > > On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote: > > > > On Tue, May 16, 2023 at 03:18:28PM +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. > > > > Which preempt_count*() are you referring to? The ones you just made > > _notrace in the previous patch?
Oops, I misunderstood your comments before. Yes, it's not necessary to do this reordering as regards to kprobe. Thanks for your review. I'll rebase onto the latest tree and send v3 ASAP. Regards, Ze On Wed, May 17, 2023 at 12:03 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > On Tue, 16 May 2023 17:47:52 +0800 > Ze Gao <zegao2021@gmail.com> wrote: > > > Precisely, these that are called within kprobe_busy_{begin, end}, > > which the previous patch does not resolve. > > Note that kprobe_busy_{begin,end} don't need to use notrace version > because kprobe itself prohibits probing on preempt_count_{add,sub}. > > Thank you, > > > I will refine the commit message to make it clear. > > > > FYI, details can checked out here: > > Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/ > > > > Regards, > > Ze > > > > On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote: > > > > > > On Tue, May 16, 2023 at 03:18:28PM +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. > > > > > > Which preempt_count*() are you referring to? The ones you just made > > > _notrace in the previous patch? > > > -- > Masami Hiramatsu (Google) <mhiramat@kernel.org>
On Wed, 17 May 2023 09:54:53 +0800 Ze Gao <zegao2021@gmail.com> wrote: > Oops, I misunderstood your comments before. > > Yes, it's not necessary to do this reordering as regards to kprobe. Let me confirm, I meant that your current patch is correct. I just mentioned that kprobe_busy_{begin,end} will continue use standard version because kprobe itself handles that. Please update only the patch description and add my ack. Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> If you add Steve's call graph for the explanation, it will help us to understand what will be fixed. Thank you, > > Thanks for your review. > > I'll rebase onto the latest tree and send v3 ASAP. > > Regards, > Ze > > On Wed, May 17, 2023 at 12:03 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > On Tue, 16 May 2023 17:47:52 +0800 > > Ze Gao <zegao2021@gmail.com> wrote: > > > > > Precisely, these that are called within kprobe_busy_{begin, end}, > > > which the previous patch does not resolve. > > > > Note that kprobe_busy_{begin,end} don't need to use notrace version > > because kprobe itself prohibits probing on preempt_count_{add,sub}. > > > > Thank you, > > > > > I will refine the commit message to make it clear. > > > > > > FYI, details can checked out here: > > > Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/ > > > > > > Regards, > > > Ze > > > > > > On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote: > > > > > > > > On Tue, May 16, 2023 at 03:18:28PM +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. > > > > > > > > Which preempt_count*() are you referring to? The ones you just made > > > > _notrace in the previous patch? > > > > > > -- > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
Got it! :) I will improve the commit message and send v3 ASAP. BTW, which tree should I rebase those patches onto? Is that the for-next branch of git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git. I saw Jiri had troubles applying those since these works are based on v6.4.0. THX, Ze On Wed, May 17, 2023 at 10:54 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > On Wed, 17 May 2023 09:54:53 +0800 > Ze Gao <zegao2021@gmail.com> wrote: > > > Oops, I misunderstood your comments before. > > > > Yes, it's not necessary to do this reordering as regards to kprobe. > > Let me confirm, I meant that your current patch is correct. I just mentioned > that kprobe_busy_{begin,end} will continue use standard version because > kprobe itself handles that. Please update only the patch description and > add my ack. > > Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > If you add Steve's call graph for the explanation, it will help us to > understand what will be fixed. > > Thank you, > > > > > Thanks for your review. > > > > I'll rebase onto the latest tree and send v3 ASAP. > > > > Regards, > > Ze > > > > On Wed, May 17, 2023 at 12:03 AM Masami Hiramatsu <mhiramat@kernel.org> wrote: > > > > > > On Tue, 16 May 2023 17:47:52 +0800 > > > Ze Gao <zegao2021@gmail.com> wrote: > > > > > > > Precisely, these that are called within kprobe_busy_{begin, end}, > > > > which the previous patch does not resolve. > > > > > > Note that kprobe_busy_{begin,end} don't need to use notrace version > > > because kprobe itself prohibits probing on preempt_count_{add,sub}. > > > > > > Thank you, > > > > > > > I will refine the commit message to make it clear. > > > > > > > > FYI, details can checked out here: > > > > Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/ > > > > > > > > Regards, > > > > Ze > > > > > > > > On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote: > > > > > > > > > > On Tue, May 16, 2023 at 03:18:28PM +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. > > > > > > > > > > Which preempt_count*() are you referring to? The ones you just made > > > > > _notrace in the previous patch? > > > > > > > > > -- > > > Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > -- > Masami Hiramatsu (Google) <mhiramat@kernel.org>
On Wed, 17 May 2023 11:10:21 +0800 Ze Gao <zegao2021@gmail.com> wrote: > Got it! :) > > I will improve the commit message and send v3 ASAP. > > BTW, which tree should I rebase those patches onto? Is that the > for-next branch of > git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git. I saw > Jiri had troubles > applying those since these works are based on v6.4.0. > You can submit against 6.4-rc1. We haven't updated the for-next branch yet. Which will be rebased off of one of the 6.4 rc's. -- Steve
On Tue, 16 May 2023 23:25:45 -0400 Steven Rostedt <rostedt@goodmis.org> wrote: > On Wed, 17 May 2023 11:10:21 +0800 > Ze Gao <zegao2021@gmail.com> wrote: > > > Got it! :) > > > > I will improve the commit message and send v3 ASAP. > > > > BTW, which tree should I rebase those patches onto? Is that the > > for-next branch of > > git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git. I saw > > Jiri had troubles > > applying those since these works are based on v6.4.0. > > > > You can submit against 6.4-rc1. We haven't updated the for-next branch > yet. Which will be rebased off of one of the 6.4 rc's. Yeah, I would like to pick it to probes/fixes for 6.4 and stable branches. Thank you,
Hi Jiri, This is the latest version against 6.4-rc1, and you can apply without trouble. https://lore.kernel.org/linux-trace-kernel/20230517034510.15639-1-zegao@tencent.com/T/#t Regards, Ze On Wed, May 17, 2023 at 11:25 AM Steven Rostedt <rostedt@goodmis.org> wrote: > > On Wed, 17 May 2023 11:10:21 +0800 > Ze Gao <zegao2021@gmail.com> wrote: > > > Got it! :) > > > > I will improve the commit message and send v3 ASAP. > > > > BTW, which tree should I rebase those patches onto? Is that the > > for-next branch of > > git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git. I saw > > Jiri had troubles > > applying those since these works are based on v6.4.0. > > > > You can submit against 6.4-rc1. We haven't updated the for-next branch > yet. Which will be rebased off of one of the 6.4 rc's. > > -- Steve
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; fp = container_of(ops, struct fprobe, ops); - if (fprobe_disabled(fp)) - return; - - bit = ftrace_test_recursion_trylock(ip, parent_ip); - if (bit < 0) { - fp->nmissed++; - return; - } if (fp->exit_handler) { rh = rethook_try_get(fp->rethook); if (!rh) { fp->nmissed++; - goto out; + return; } fpr = container_of(rh, struct fprobe_rethook_node, node); fpr->entry_ip = ip; @@ -61,23 +53,60 @@ static void fprobe_handler(unsigned long ip, unsigned 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 = container_of(ops, struct fprobe, ops); + if (fprobe_disabled(fp)) + return; + + /* recursion detection has to go before any traceable function and + * all functions before this point should be marked as notrace + */ + bit = 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 parent_ip, struct ftrace_ops *ops, struct ftrace_regs *fregs) { - struct fprobe *fp = container_of(ops, struct fprobe, ops); + struct fprobe *fp; + int bit; + + fp = container_of(ops, struct fprobe, ops); + if (fprobe_disabled(fp)) + return; + + /* recursion detection has to go before any traceable function and + * all functions called before this point should be marked as notrace + */ + bit = 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,