Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1442428rwd; Sun, 14 May 2023 20:18:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4h3tXIuJMfjBv9Zooirtt6+GkWEvnCiJ49Fz5hxk6M25sLJHLQhBs+XeYiY01XXovA7Bqi X-Received: by 2002:a17:90a:fa85:b0:24d:eb38:bfa0 with SMTP id cu5-20020a17090afa8500b0024deb38bfa0mr33091318pjb.11.1684120688598; Sun, 14 May 2023 20:18:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684120688; cv=none; d=google.com; s=arc-20160816; b=OzJGXrzVG7lqqM3RQYzbxn9RwAMvaUeEKoJE60aEJA2B1PYy2oShSkRUm9Sj8Ycsst Ap1jd2phuwfj6alC+TbznUGO0EGQoxBh+CgNCfqiEtacpbH8g3RqFtszK7BREVxOoDRf 9n32tL9xs0rhVOxL1ZNdKfdRRaK51AeFqhZmfYgo78mJctp6g3iOnjX8fqf/jjMwu2sn FBqt5FhSZ6UQJtCeTaf0JiRmPqVH2b9LXZ3s4b9PXlBYFEdYIWVvntnn+Zw58zEvU+pT XkcVmgS24jis1QG4pjE68Lgg5YBAbN34F8HDWMkFwgyzSMEVSAeh1Ec7L13+7bnXV0a0 6Fhg== 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=LiiaLoe2FHTpcejFRmixK8pQOtJa58cAbjMGJsmgl/s=; b=BY1WhlAAxtdMwqXagyFJEI0mS/iW9BhDspUKmeRzN3zPiGhx7v0tZul8sKT9lP99fM 2Fj6UNAx7PrtqvSCKfvoiXwEbKg33se0ZkPBRXD7eLmQvFgJipCn87bnBmWN9qc8O+Ff 9jlD2umy773SvQCrEy3o2pDNo56Q7rUvOSNDqrsO3euVRvCWtivo2F98ZN08byCpmv/2 wJ296EMnvwC1J4oSovqWbZyomIzumwng+rRvA1ozCgHga4/J2VwyxVQkl24d9i7x31SG aaYuqyPLDHpmCmsWLqmSp1dbxzRGhS0UB9unLGzz8KcDJ/4/2UsDreIkG2LGZez0Vuxj owhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=J1BfnOlc; 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 l10-20020a17090a850a00b0024715c041c3si28655532pjn.137.2023.05.14.20.17.54; Sun, 14 May 2023 20:18:08 -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=J1BfnOlc; 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 S238112AbjEODNy (ORCPT + 99 others); Sun, 14 May 2023 23:13:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232572AbjEODNt (ORCPT ); Sun, 14 May 2023 23:13:49 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66AF5172C; Sun, 14 May 2023 20:13:47 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-250252e4113so8231306a91.0; Sun, 14 May 2023 20:13:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684120427; x=1686712427; 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=LiiaLoe2FHTpcejFRmixK8pQOtJa58cAbjMGJsmgl/s=; b=J1BfnOlcIOIhgtjq+rMKJpJhtUDWEYN1i5pZ9WI5qw7lQuiyE9W5ziwS9tHiXxr0j8 kHM6HMERPCSvGOdHc+OjUornJZDjV9AYJgg0tiPvQvdzlf0jgyzuUOHg7IFdOuLf4MO0 DR6nLPR0awPaWx9lkWZXsRH1s0dXeaGY5lnKKCu5/49ULoRSChhv75hW/SUQafxI1mo4 b7sJT1eqK/UnkuwQoVFZUrZ3NUZcpbqZw5fxtjodfvEyoQ3WIwl2BG51jz9Jk1G7o45g 3Kv7palxqk2ajwcWYsxS2aG2HGmEKuTUsaxz3QTAGjlYGnSynojEFf8XshUnSyqea3VR Zgxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684120427; x=1686712427; 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=LiiaLoe2FHTpcejFRmixK8pQOtJa58cAbjMGJsmgl/s=; b=PW4fF3u5NJyBxcUfMOE+O1PhZ+5pgd5iksIJB8S/d3RiEDLFZevsUrFcX9QDmyMfVl dm/vA0hmrUqB9qPzZXp+ARiDnxS294y/x5/wIX93ii+hfSxC70gv6iz6uGFtTlO+Ef8B YpJvzm9Yyc2sBv9CC93Dnzkg/ByvMTJD0LkMV11SjGxnJ6m20o3JnAxFa7seOb6UQtIt HAeq9XD/BTlyyyTpv+gDe8CiBaGMC2JINW24aTN4AM/H9iE5hory4xl3LTr2TC7bNYjl J29360Vx4cmGJ+rVr9gfjVTZmqDGbbR/vJd/T0oAcmgfT2FA4kjzrbbY6ssapJfSQk/k J++Q== X-Gm-Message-State: AC+VfDy5xqOh9JZnMGa9JFMNx5YaIko5jq7MK0sYBJQmcIGNTcwia83f rAtBs8XaE+jcE5Vzo2qmKWU= X-Received: by 2002:a17:90a:6a8e:b0:234:ba34:71bf with SMTP id u14-20020a17090a6a8e00b00234ba3471bfmr33197027pjj.1.1684120426786; Sun, 14 May 2023 20:13:46 -0700 (PDT) Received: from localhost.localdomain ([43.132.98.42]) by smtp.googlemail.com with ESMTPSA id o4-20020a17090a744400b00252b3328ad8sm4943577pjk.0.2023.05.14.20.13.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 May 2023 20:13:46 -0700 (PDT) From: Ze Gao X-Google-Original-From: Ze Gao To: Steven Rostedt , Masami Hiramatsu Cc: Ze Gao , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH 2/4] fprobe: make fprobe_kprobe_handler recursion free Date: Mon, 15 May 2023 11:13:11 +0800 Message-Id: <20230515031314.7836-3-zegao@tencent.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230515031314.7836-1-zegao@tencent.com> References: <20230515031314.7836-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: X-Mailing-List: linux-kernel@vger.kernel.org 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, and also mark these functions notrace so that the whole fprobe_k- probe_handler is free from recusion. And Signed-off-by: Ze Gao --- kernel/trace/fprobe.c | 61 +++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 9abb3905bc8e..ad9a36c87ad9 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 notrace 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 notrace 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, +static void notrace 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, -- 2.40.1