From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796383229; b=bt/CNOJw9PqjllsMhyvTq/pTs+8NsatY0dxA6KSoOCfLOuFyqlCT8dG81EvZp1IfvI7r zcBz/DDftwAIBVW+q0p6ceC77CugRttHjwxv6JEcwIQV46h/SIK1LBsm3IdcRlfH/1lix f5YmweVwQmNiJ4VtTB0JkhiTY/VMzjRpdWCgDrlQ9Z4KCAuLOSRIMMt7OnclWuAJfXYdh Q4cbLeuN77DmtAKqd/u+cc5/+dWYZnmaVf2q05k3qs2ltQGJhj4qv44Y2sGr7DWX82unk b7Q6zq39mOn3bGrNCEVLJooKzCr+M45HzjvbckmtTHP9HRWyLxpIvDJCJFdWz/wFhcA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383229; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=FlI9OXFGpX45FA3aCsTqWEdsqq3mEUZZKMOy1WFeRxE=; b=DHwcZZV867Ofqt72R7FEqZ4rXjIwTKCZoFxE3gb+ZEE5dFBIl7bHIrDGENHv5n9tsqnt pENg9A35AQ79/RzN0HW7du/Qz5g37afI+i69uTLEzYw20y3xqXeXyHXj93d5C1FCnXtF5 CvJGxUUry5jKKxBgWO6lXyyRpl0gBYslYnehZ16Xbw+D5xB0f7h+oFhIxwHSqWlqgXN7L tX4KXaeUNCc4ZVaDQkglzc7h2OEUhosvFVSmeWG6TvKCY7JRvuCNufbYFib8q7X3caSVF hVUw97R62qm2feOBdWhRf4v2tAXziYMSrVI8xJ9vLMxSS4ExlxhNyvXinIinoFABhxA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Fmm3OCFO; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EAB2606A32D; Fri, 4 Dec 2026 11:20:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=Fmm3OCFO Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8D321F000C2; Fri, 4 Dec 2026 11:20:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383229; bh=FlI9OXFGpX45FA3aCsTqWEdsqq3mEUZZKMOy1WFeRxE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Fmm3OCFOkRFRhKG0uSJLDc1NSWslxso72eF+zFkzefvrvNusj1xEHZGp0iClpQTJS Dg4cYK9eRpZIgsemsEiApsJ4ehhXez+M8yCinfoXgn9mbWVlvsqpOwBxYSpNC9nJLe LNoJ/Gog24M+tDIbHB+gBa/A4m/XuZeMhC1zCDBM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Sebastian Andrzej Siewior , "Masami Hiramatsu (Google)" , Sasha Levin Subject: [PATCH 6.12 086/555] kprobes: Reduce preempt disable scope in check_kprobe_access_safe() Date: Fri, 4 Dec 2026 19:02:09 +0900 Message-ID: <20261204100610.417996409@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Thomas Weißschuh [ Upstream commit 1703abb4dead917ec7e6d9ccb4c13632a6f0994d ] Commit a189d0350f387 ("kprobes: disable preempt for module_text_address() and kernel_text_address()") introduced a preempt_disable() region to protect against concurrent module unloading. However this region also includes the call to jump_label_text_reserved() which takes a long time; up to 400us, iterating over approx 6000 jump tables. The scope protected by preempt_disable() is largen than necessary. core_kernel_text() does not need to be protected as it does not interact with module code at all. Only the scope from __module_text_address() to try_module_get() needs to be protected. By limiting the critical section to __module_text_address() and try_module_get() the function responsible for the latency spike remains preemptible. This works fine even when !CONFIG_MODULES as in that case try_module_get() will always return true and that block can be optimized away. Limit the critical section to __module_text_address() and try_module_get(). Use guard(preempt)() for easier error handling. While at it also remove a spurious *probed_mod = NULL in an error path. On errors the output parameter is never inspected by the caller. Some error paths were clearing the parameters, some didn't. Align them for clarity. Link: https://lore.kernel.org/all/20241121-kprobes-preempt-v1-1-fd581ee7fcbb@linutronix.de/ Signed-off-by: Thomas Weißschuh Reviewed-by: Sebastian Andrzej Siewior Signed-off-by: Masami Hiramatsu (Google) Stable-dep-of: f60f98881b54 ("kprobes: Switch ftrace ops when adding probe with post_handler") Signed-off-by: Sasha Levin --- kernel/kprobes.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index cfcfb7be5f519..064f0866c9c90 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -1581,16 +1582,25 @@ static int check_kprobe_address_safe(struct kprobe *p, if (ret) return ret; jump_label_lock(); - preempt_disable(); /* Ensure the address is in a text area, and find a module if exists. */ *probed_mod = NULL; if (!core_kernel_text((unsigned long) p->addr)) { + guard(preempt)(); *probed_mod = __module_text_address((unsigned long) p->addr); if (!(*probed_mod)) { ret = -EINVAL; goto out; } + + /* + * We must hold a refcount of the probed module while updating + * its code to prohibit unexpected unloading. + */ + if (unlikely(!try_module_get(*probed_mod))) { + ret = -ENOENT; + goto out; + } } /* Ensure it is not in reserved area. */ if (in_gate_area_no_mm((unsigned long) p->addr) || @@ -1599,21 +1609,13 @@ static int check_kprobe_address_safe(struct kprobe *p, static_call_text_reserved(p->addr, p->addr) || find_bug((unsigned long)p->addr) || is_cfi_preamble_symbol((unsigned long)p->addr)) { + module_put(*probed_mod); ret = -EINVAL; goto out; } /* Get module refcount and reject __init functions for loaded modules. */ if (IS_ENABLED(CONFIG_MODULES) && *probed_mod) { - /* - * We must hold a refcount of the probed module while updating - * its code to prohibit unexpected unloading. - */ - if (unlikely(!try_module_get(*probed_mod))) { - ret = -ENOENT; - goto out; - } - /* * If the module freed '.init.text', we couldn't insert * kprobes in there. @@ -1621,13 +1623,11 @@ static int check_kprobe_address_safe(struct kprobe *p, if (within_module_init((unsigned long)p->addr, *probed_mod) && !module_is_coming(*probed_mod)) { module_put(*probed_mod); - *probed_mod = NULL; ret = -ENOENT; } } out: - preempt_enable(); jump_label_unlock(); return ret; -- 2.53.0