From patchwork Mon May 15 03:26:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ze Gao X-Patchwork-Id: 93846 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp6640914vqo; Sun, 14 May 2023 20:41:59 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5HnWOIV+LCg7Bkc/4hfJgdJiOgPOPqzRxMzb/fvB798afmSYlKGm6eWVZtm1O40KHGAxa/ X-Received: by 2002:a05:6a20:394d:b0:104:8824:74eb with SMTP id r13-20020a056a20394d00b00104882474ebmr11665916pzg.51.1684122119138; Sun, 14 May 2023 20:41:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684122119; cv=none; d=google.com; s=arc-20160816; b=fvVgoJGXQcTjeSAD644KOdVsFbT2Zk1G6bKZCpdrK/boxaMtKtPERLvJH/iLjQzUnF qQHRwcAX2E20mMo44uxX/i2DPoNwdRPQ2klgDiDgp/yKAnWr/P78w87LP/bq+WQkGRMV zBJNDY0Ah3bMNr1oIjdbFX/sto9iCFCgjVYsx8XJ00dorKChu5V+c4x62ZdAlUhKwST1 gFYGwt3ypwdaPJJqK+69SmF3AlrtNyhgb2szyH/pc2Ntxt1S8qXrdhMd26ez0svHAPkI bwkFuSGMaTL7xH6elNBd2G1UJEymt6rWeg7cyMkIVgdLfhEqBx9HaUW0NRGMH9hM0aD2 N/kA== 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=tAEPEuF6rcMyCpsPX8wyMGe866adfJTgF6X8exC4VQC0oRJABzm92zVTbxr7pC7dxB d9CmhdBKn05HGG/YShI62JMx+nAy0QEbpRSIyxJIoRwNKW+b3c/3JfNtuHNo3JiQoAQl EH2PBhJ0FN2KVun8B3BPujmPZVb/MI7hICFcpRUqB8d9kjfue3k6EchHgN/IYL9tJDj5 gBoyb92YuYkz7K3mB4P3lvCp6VMJeWVr5t+YNYEj7chzqyJoSTwr8PbbSXiCN4OcomPb lWAWkQq2yOpvfNZ6uj5hBmIDof+wUJmF+m0xR/Q6DVDw88hN7Ssjnz6Jr8pkekjQpx02 1WPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=rr80hkcj; 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 bs130-20020a632888000000b0051b9a71329fsi14218201pgb.360.2023.05.14.20.41.47; Sun, 14 May 2023 20:41:59 -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=rr80hkcj; 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 S238294AbjEOD1M (ORCPT + 99 others); Sun, 14 May 2023 23:27:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237723AbjEOD1I (ORCPT ); Sun, 14 May 2023 23:27:08 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B8361997; Sun, 14 May 2023 20:27:07 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-64395e741fcso12688226b3a.2; Sun, 14 May 2023 20:27:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684121226; x=1686713226; 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=rr80hkcjv0wdh0TWDxyo+jfz7J/dCaLoIrPfhOEfAZsmxb2C9sfdYQBYLt27Wpycly hAAJWHk5GTOFY06VkHnCW3Y9W9unD/aJmf+gZ1CadLq0VSE+DcGoZlPHZB0yURl9Oy0p zTUzWL7T8lPZx2ODFhnOAHV+sYGMR+WsYHHI/GHJMLrkMW6zaZbFOsBULsPZWekocpR6 LamOP/q7Ux1HCELYDetYBQL0V8zN9r8oTu4qGLtKzzDv8zHhlYGvkjqqLxqKjG6gKvdP fjnhdZcY5ZbPSMmLjtrBgbl+3S4/FA3L1zKoeCZ4I36ljlPbdczKtaVSdL2VMHBptwGv naQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684121226; x=1686713226; 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=Ufm0CxBFYO4/ucbeBQXrkYGE9Vvu2oMN65zhdWBY+VDHeTcCOd2nz/SKdqbZ3EvS5X UXbjAPUg9Lm1pRqZVQzuMHkYTFNdBOY7MZ2yossp3zOB6dzKXFxRQDMdwBA7Y7ijXHQ7 3RYrcUB6HiJI8lInndgwbwEs/DHd2SKUQBjk8ZJyCuIsG10+4y9OtGBCMFmL2KZK8Cv8 Hq96aicVEjPhpKh+FAKp2ATUo3JEv1/DbvN+mW2vnrLdk+LFa2xFrr6bJ9U5YiPehYxM XYIeroZTK/7u1LLXiCYpt3bnb3wMQN1QrGud6Mug6RkAitkog8ArBQNfdsIblOh+LvL9 984w== X-Gm-Message-State: AC+VfDzKFttLt7SPpZ3jPi4Od8xDSDaM8OLuKXhHDNxyuSIKaOgb+8c+ O4hF3SAa7GfCZLt0Vc1eO1A= X-Received: by 2002:a05:6a00:168b:b0:63d:2d7d:b6f2 with SMTP id k11-20020a056a00168b00b0063d2d7db6f2mr43084736pfc.4.1684121226572; Sun, 14 May 2023 20:27:06 -0700 (PDT) Received: from localhost.localdomain ([43.132.98.42]) by smtp.googlemail.com with ESMTPSA id s6-20020a632146000000b0051416609fb7sm10363825pgm.61.2023.05.14.20.27.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 May 2023 20:27:06 -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:26:39 +0800 Message-Id: <6c428b9f74090b2780b67ca498c45aa5e8c38c44.1684120990.git.zegao@tencent.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: 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?1765930035150107469?= X-GMAIL-MSGID: =?utf-8?q?1765930035150107469?= 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,