From patchwork Fri Nov 24 01:03:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 169141 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp815286vqx; Thu, 23 Nov 2023 17:03:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IEUVFA8hlCGMiHvdxPUpFLmmPQMi2saWbtx4J39Jv5zSLYC37+EWppV+0yPX4+9ZYCD8dr1 X-Received: by 2002:a17:90b:350b:b0:27f:ecd9:6d0e with SMTP id ls11-20020a17090b350b00b0027fecd96d0emr1236038pjb.34.1700787826762; Thu, 23 Nov 2023 17:03:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700787826; cv=none; d=google.com; s=arc-20160816; b=cdgAu7AguTBrE2RS0G42R40g9mTrxets1x+YY7igUuT1HXj6QaEMf/+9rmDo0+4ymf kRm7uIlAIGihFzkzZjOIEjFq1QaPxCVAPgZoM5lbPnBhPAT0rDlqC433ixaGB/phAKfO seoA24CX2mmlQfS2Usq09fRN2BP83m6sbaCEb0vFaGxXNXIZKKJoG9qoaHLUmMG8W5iT 8xfDRIOj8Ei2sQq8xrPhV37ySHcXg0LEItRL75+8vbCDlCHHNXeUdSMHSIjBIjP1z6jL 4/3E5GL2Jlw3ntiGxVosa6QcDiSsaw8N3eoDi3RXMDnne7jWlKh1kiWYR9mQ5kgdl0n/ zFBg== 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 :user-agent:message-id:date:subject:cc:to:from:dkim-signature; bh=wlUnvuEymTF0yoswxmmGtdsIl5sRJA1yvyNPic9kqqQ=; fh=JVBpVNNWHUMwFwwsMyL8WD0n5dEDFSIpaQW1hb+uLZc=; b=AUQ3iTnhc/kqbrjpVM0SdgCjr8jEEhnl383sTLR0EEPVcPWm/xgaw9S0eGcWF9JcxG EXplOZ83SaGT2QZSezsbsNai5KhjyB1y4CYNZqBSWfyXBnL0ZpoHXFUDg8MS6M+tTmxC Vo3LRCqnoxlGBXgu2I/T8ywqPoDWPKrBwJ53DSUjkfWuVQhNCoYX47oOV9iPJN4owoUW egvzEqLTUeLJbA9R5DyF1nkLO8+ApQRAsR6+AN4/1QfGwSA9Vhw2VdfExhPLo6i0W5Ub 97PGGICkftX/J1YtFflkPJ00oPT++ub4hSJ68hm1yHzPMFwqE6p6ZaO0Eg2x/VtuW5FV /sVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="DuOxk/DR"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id ck19-20020a17090afe1300b00276b35bc424si2360341pjb.26.2023.11.23.17.03.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 17:03:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="DuOxk/DR"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id BDA0A80A2196; Thu, 23 Nov 2023 17:03:17 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229722AbjKXBDI (ORCPT + 99 others); Thu, 23 Nov 2023 20:03:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229453AbjKXBDG (ORCPT ); Thu, 23 Nov 2023 20:03:06 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A4F91B6 for ; Thu, 23 Nov 2023 17:03:11 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A7454C433C8; Fri, 24 Nov 2023 01:03:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700787791; bh=MK5tjkjB52MplO5zrLGX/U/QKetESX/No8t7aMgG+PE=; h=From:To:Cc:Subject:Date:From; b=DuOxk/DRRLcNBbp2QjtM0XWSEBjeNGb3i/iSFwfdsRhmFFcZ4J/kMkEAlTVwMA5wc IWAg2Iux0WS3sJAnKFWuet3UQsfQt0/WxY3MXcSRqk2IIUnBY9/zKNVgj89K6mWPsy 6TFrUBPY3bMsYNwqxySVEdx0OT1OGnBwQ9KLfGGuvc++4zjT84wGAf3Tx2PEa7HjIx eCjSJd0Ls67dDcSR8FhPlJ+6CLYNYClJCSmdjixCWA5gYEs1kGTScRoqWLncyYwKqO no//WB50pWt1jwcGPjIR3gd7dNKQMXE6GJZMrnYcm66XwlxHMbJky32mNDIlgcre3E DcM+dZO8WQYcw== From: "Masami Hiramatsu (Google)" To: linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org Cc: JP Kobryn , bpf@vger.kernel.org, kernel-team@meta.com, rostedt@goodmis.org, peterz@infradead.org Subject: [PATCH] rethook: Use __rcu pointer for rethook::handler Date: Fri, 24 Nov 2023 10:03:06 +0900 Message-Id: <170078778632.209874.7893551840863388753.stgit@devnote2> X-Mailer: git-send-email 2.34.1 User-Agent: StGit/0.19 MIME-Version: 1.0 X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 23 Nov 2023 17:03:17 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783405296161624999 X-GMAIL-MSGID: 1783405296161624999 From: Masami Hiramatsu (Google) Since the rethook::handler is an RCU-maganged pointer so that it will notice readers the rethook is stopped (unregistered) or not, it should be an __rcu pointer and use appropriate functions to be accessed. This will use appropriate memory barrier when accessing it. OTOH, rethook::data is never changed, so we don't need to check it in get_kretprobe(). Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook") Signed-off-by: Masami Hiramatsu (Google) --- include/linux/kprobes.h | 6 ++---- include/linux/rethook.h | 2 +- kernel/trace/rethook.c | 21 ++++++++++++--------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h index 64672bace560..0ff44d6633e3 100644 --- a/include/linux/kprobes.h +++ b/include/linux/kprobes.h @@ -197,10 +197,8 @@ extern int arch_trampoline_kprobe(struct kprobe *p); #ifdef CONFIG_KRETPROBE_ON_RETHOOK static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri) { - RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(), - "Kretprobe is accessed from instance under preemptive context"); - - return (struct kretprobe *)READ_ONCE(ri->node.rethook->data); + /* rethook::data is non-changed field, so that you can access it freely. */ + return (struct kretprobe *)ri->node.rethook->data; } static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri) { diff --git a/include/linux/rethook.h b/include/linux/rethook.h index ce69b2b7bc35..164cd32c25cd 100644 --- a/include/linux/rethook.h +++ b/include/linux/rethook.h @@ -28,7 +28,7 @@ typedef void (*rethook_handler_t) (struct rethook_node *, void *, unsigned long, */ struct rethook { void *data; - rethook_handler_t handler; + rethook_handler_t __rcu handler; struct objpool_head pool; struct rcu_head rcu; }; diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c index 6fd7d4ecbbc6..77f0e987bbff 100644 --- a/kernel/trace/rethook.c +++ b/kernel/trace/rethook.c @@ -48,7 +48,7 @@ static void rethook_free_rcu(struct rcu_head *head) */ void rethook_stop(struct rethook *rh) { - WRITE_ONCE(rh->handler, NULL); + rcu_assign_pointer(rh->handler, NULL); } /** @@ -63,7 +63,7 @@ void rethook_stop(struct rethook *rh) */ void rethook_free(struct rethook *rh) { - WRITE_ONCE(rh->handler, NULL); + rcu_assign_pointer(rh->handler, NULL); call_rcu(&rh->rcu, rethook_free_rcu); } @@ -107,7 +107,7 @@ struct rethook *rethook_alloc(void *data, rethook_handler_t handler, return ERR_PTR(-ENOMEM); rh->data = data; - rh->handler = handler; + rcu_assign_pointer(rh->handler, handler); /* initialize the objpool for rethook nodes */ if (objpool_init(&rh->pool, num, size, GFP_KERNEL, rh, @@ -135,9 +135,12 @@ static void free_rethook_node_rcu(struct rcu_head *head) */ void rethook_recycle(struct rethook_node *node) { - lockdep_assert_preemption_disabled(); + rethook_handler_t handler; + + handler = rcu_dereference_check(node->rethook->handler, + rcu_read_lock_any_held()); - if (likely(READ_ONCE(node->rethook->handler))) + if (likely(handler)) objpool_push(node, &node->rethook->pool); else call_rcu(&node->rcu, free_rethook_node_rcu); @@ -153,10 +156,9 @@ NOKPROBE_SYMBOL(rethook_recycle); */ struct rethook_node *rethook_try_get(struct rethook *rh) { - rethook_handler_t handler = READ_ONCE(rh->handler); - - lockdep_assert_preemption_disabled(); + rethook_handler_t handler; + handler = rcu_dereference_check(rh->handler, rcu_read_lock_any_held()); /* Check whether @rh is going to be freed. */ if (unlikely(!handler)) return NULL; @@ -300,7 +302,8 @@ unsigned long rethook_trampoline_handler(struct pt_regs *regs, rhn = container_of(first, struct rethook_node, llist); if (WARN_ON_ONCE(rhn->frame != frame)) break; - handler = READ_ONCE(rhn->rethook->handler); + handler = rcu_dereference_check(rhn->rethook->handler, + rcu_read_lock_any_held()); if (handler) handler(rhn, rhn->rethook->data, correct_ret_addr, regs);