From patchwork Tue May 16 07:18:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ze Gao X-Patchwork-Id: 94485 Return-Path: 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 + 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 ); 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 X-Google-Original-From: Ze Gao To: Steven Rostedt , Masami Hiramatsu Cc: 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, 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 , Jiri Olsa , Yonghong Song , Ze Gao 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766035967305775675?= X-GMAIL-MSGID: =?utf-8?q?1766035967305775675?= 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 Acked-by: Masami Hiramatsu (Google) --- 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; 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,