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=1795602900; b=dE/5122AKTyXuj2BDJDtE+0EDw56+T5x4gwAlotj96giZ2m8g/IL4m+8lzRpvnWsQl40 RzNCOo3+ivD+YVDpz7LUMez1NCmcbvYMcU1p6pcRANYoJ0F9p7HWkXVbsx6dAsXPem7hD PnMTPHpMNsAD6n9fg8PUqhvxnD6oBSUMyBpENyVkTwYd1+UnV7M9zWtHoG8B/kIrhuV0y +Ov7vlJqcy6ZJcK6JYYCZaqe4WEJsKWFUtMmiutYWHlK3jjCf2z27kZyq5dFaJxKrRrVA fDzq/r1JjPq8tw4r7SLIeL0ZBt65KRfTpqVuEUvaN+n2ygE+88bZpWQFDaAupnauIPQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795602900; h=Received:DKIM-Signature:Subject:To:Cc:From:Date:Message-ID: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=z1pdvccz9fYrtq7ZWqiE4PZ9BPpQaoT6rI4aq/rLiM0=; b=0GOkcE2ubVlnNtJV9IoRSlD8bwmJV0m6YnpDIAiwJKV6AOtnWaRnkjemTHqob9gvrAiL BLoY8Xs1ELiVe6x/RlRshADl+jRYlb/0EnOvEWhknZGxLJ0I0pNdHpA38isMKHL0iJ1el rpZKkp58nmUoF+6oZWFErfkvs6hzDqI1qNLxBxhQH4BYHseM3zC5T7OzOe1MB/+kVb2wI TX/nrlj9BFaXjpR6H7orgTChmztR0fbVos2WnSGwU3C6lso4/xdSYXWVX1LHqnwgnXb4T kvSeim7tqN0jblgd4icxiDrl12tI98CrEZtuxKM+JE2APnpL3S9xwiXh00QblwJ0Vnw== 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="Zoxl/Wzj"; 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 1A8CF606A336 for ; Wed, 25 Nov 2026 10:35:00 +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=Zoxl/Wzj Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF9DF1F000C4; Wed, 25 Nov 2026 10:34:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795602899; bh=z1pdvccz9fYrtq7ZWqiE4PZ9BPpQaoT6rI4aq/rLiM0=; h=Subject:To:Cc:From:Date; b=Zoxl/WzjEpTa0pfGUiU1Z2EtMMsgBED8fPeNl5g+HgpqndUELIwIS33eep70ttXhH dZ4gpBZ2SWtViphjfX9UdByQ0LR99iTphEAsLN/f/Kl/7nxCBGE3QTGepgRlB5HzXl WajyfXMgCdOoO4IqOXWl8NPcvsTHJPviNkV7pvs8= Subject: FAILED: patch "[PATCH] kprobes: Switch ftrace ops when adding probe with" failed to apply to 5.10-stable tree To: mhiramat@kernel.org Cc: From: Date: Wed, 25 Nov 2026 11:17:29 +0100 Message-ID: <2026112529-fragility-commence-4ad2@gregkh> 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=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit The patch below does not apply to the 5.10-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . To reproduce the conflict and resubmit, you may use the following commands: git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y git checkout FETCH_HEAD git cherry-pick -x f60f98881b54b0cf36b61f41c91fbd93b76a2d9c # git commit -s git send-email --to '' --in-reply-to '2026112529-fragility-commence-4ad2@gregkh' --subject-prefix 'PATCH 5.10.y' 'HEAD^..' Possible dependencies: thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From f60f98881b54b0cf36b61f41c91fbd93b76a2d9c Mon Sep 17 00:00:00 2001 From: "Masami Hiramatsu (Google)" Date: Wed, 11 Nov 2026 23:13:31 +0900 Subject: [PATCH] kprobes: Switch ftrace ops when adding probe with post_handler When an ftrace-based kprobe is registered without a post_handler, it is armed using kprobe_ftrace_ops. If a second probe with a post_handler is subsequently registered at the same address, register_aggr_kprobe() sets ap->post_handler to aggr_post_handler, but leaves the probe armed using kprobe_ftrace_ops. When the probe is later disarmed or disabled, disarm_kprobe_ftrace() checks whether p->post_handler is non-NULL and attempts to disarm from kprobe_ipmodify_ops. This fails with -ENOENT, decrements kprobe_ipmodify_enabled below zero, leaves the probe armed on kprobe_ftrace_ops, and aborts unregistration, leading to a use-after-free if the probe module is unloaded. Switch the armed probe from kprobe_ftrace_ops to kprobe_ipmodify_ops in register_aggr_kprobe() outside of text_mutex and cpus_read_lock to avoid lockdep and deadlock issues when modifying ftrace code. Defer adding the new probe onto ap->list until the ftrace ops switch completes so that the new probe's handlers are not published prematurely while ap is still on kprobe_ftrace_ops, and mark ap disabled if rollback re-arming fails. Also restore ftrace ops registration and counter if ftrace_set_filter_ip() fails in __disarm_kprobe_ftrace(). Link: https://lore.kernel.org/all/179440641120.328088.15542754095040659094.stgit@devnote2/ Fixes: 5dd7caf0bdc5 ("kprobes: Skip clearing aggrprobe's post_handler in kprobe-on-ftrace case") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index dddad1e01866..b6ff971df451 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1444,9 +1444,14 @@ static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, (*cnt)--; ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); - WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (error %d)\n", - p->addr, ret); - return ret; + if (WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (error %d)\n", + p->addr, ret)) { + if (*cnt == 0) + register_ftrace_function(ops); + (*cnt)++; + return ret; + } + return 0; } static int disarm_kprobe_ftrace(struct kprobe *p) @@ -1575,17 +1580,13 @@ void kprobe_busy_end(void) } /* Add the new probe to 'ap->list'. */ -static int add_new_kprobe(struct kprobe *ap, struct kprobe *p) +static void add_new_kprobe(struct kprobe *ap, struct kprobe *p) { /* A reused cooling probe is already unoptimized */ if (p->post_handler && !kprobe_unused(ap)) unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */ list_add_rcu(&p->list, &ap->list); - if (p->post_handler && !ap->post_handler) - ap->post_handler = aggr_post_handler; - - return 0; } /* @@ -1618,6 +1619,8 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) { int ret = 0; struct kprobe *ap = orig_p; + bool reset_post_handler = false; + bool ftrace_switch_ops = false; scoped_guard(cpus_read_lock) { /* For preparing optimization, jump_label_text_reserved() is called */ @@ -1664,18 +1667,50 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) | KPROBE_FLAG_DISABLED; } + if (p->post_handler && !ap->post_handler) { + reset_post_handler = true; + if (kprobe_ftrace(ap) && !kprobes_all_disarmed && + !kprobe_disabled(ap)) + ftrace_switch_ops = true; + else + ap->post_handler = aggr_post_handler; + } + /* Copy the insn slot of 'p' to 'ap'. */ copy_kprobe(ap, p); - ret = add_new_kprobe(ap, p); + if (!ftrace_switch_ops) + add_new_kprobe(ap, p); } - if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) { + if (ftrace_switch_ops) { + /* + * Must switch ftrace ops outside of text_mutex and cpus_read_lock + * to prevent deadlocks when ftrace code modification acquires text_mutex. + */ + ret = disarm_kprobe_ftrace(ap); + if (ret) + return ret; + + ap->post_handler = aggr_post_handler; + ret = arm_kprobe_ftrace(ap); + if (ret) { + ap->post_handler = NULL; + if (arm_kprobe_ftrace(ap)) + ap->flags |= KPROBE_FLAG_DISABLED; + return ret; + } + add_new_kprobe(ap, p); + } + + if (kprobe_disabled(ap) && !kprobe_disabled(p)) { ap->flags &= ~KPROBE_FLAG_DISABLED; if (!kprobes_all_disarmed) { /* Arm the breakpoint again. */ ret = arm_kprobe(ap); if (ret) { ap->flags |= KPROBE_FLAG_DISABLED; + if (reset_post_handler) + ap->post_handler = NULL; list_del_rcu(&p->list); synchronize_rcu(); }