mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: jpoimboe@kernel.org, mbenes@suse.cz, pmladek@suse.com,
	joe.lawrence@redhat.com, song@kernel.org
Cc: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yafang Shao <laoar.shao@gmail.com>,
	sashiko-bot <sashiko-bot@kernel.org>
Subject: [PATCH v3 1/2] livepatch: Protect the per-CPU stack buffer in klp_check_stack()
Date: Thu, 26 Nov 2026 15:28:28 +0800	[thread overview]
Message-ID: <20261126072829.97792-2-laoar.shao@gmail.com> (raw)
In-Reply-To: <20261126072829.97792-1-laoar.shao@gmail.com>

klp_check_stack() stores the unwound stack into a per-CPU buffer and
requires preemption to be disabled. It currently relies on the caller for
that. However, on PREEMPT_RT even read_lock(&tasklist_lock) is a sleeping
lock. Therefore, we must disable preempt explicitly in klp_check_stack().

Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/live-patching/sashiko-outbox-228569@kernel.org/
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 kernel/livepatch/transition.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index 2351a19ac2a9..4739fc170f48 100644
--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -253,17 +253,27 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
  */
 static int klp_check_stack(struct task_struct *task, const char **oldname)
 {
-	unsigned long *entries = this_cpu_ptr(klp_stack_entries);
+	unsigned long *entries;
 	struct klp_object *obj;
 	struct klp_func *func;
-	int ret, nr_entries;
+	int nr_entries;
+	int ret = 0;
 
-	/* Protect 'klp_stack_entries' */
-	lockdep_assert_preemption_disabled();
+	/*
+	 * The unwound stack is stored in a per-CPU buffer, so preemption
+	 * must be disabled.  Do not rely on the callers for that: on
+	 * PREEMPT_RT even read_lock(&tasklist_lock) is a sleeping lock.
+	 * When called via task_call_func(), this is nested inside its
+	 * raw spinlock and is a no-op.
+	 */
+	preempt_disable();
+	entries = this_cpu_ptr(klp_stack_entries);
 
 	ret = stack_trace_save_tsk_reliable(task, entries, MAX_STACK_ENTRIES);
-	if (ret < 0)
-		return -EINVAL;
+	if (ret < 0) {
+		ret = -EINVAL;
+		goto out;
+	}
 	nr_entries = ret;
 
 	klp_for_each_object(klp_transition_patch, obj) {
@@ -273,12 +283,16 @@ static int klp_check_stack(struct task_struct *task, const char **oldname)
 			ret = klp_check_stack_func(func, entries, nr_entries);
 			if (ret) {
 				*oldname = func->old_name;
-				return -EADDRINUSE;
+				ret = -EADDRINUSE;
+				goto out;
 			}
 		}
 	}
 
-	return 0;
+	ret = 0;
+out:
+	preempt_enable();
+	return ret;
 }
 
 static int klp_check_and_switch_task(struct task_struct *task, void *arg)
-- 
2.52.0


  reply	other threads:[~2026-11-26  7:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  7:28 [PATCH v3 0/2] livepatch: Reduce tasklist_lock contention Yafang Shao
2026-11-26  7:28 ` Yafang Shao [this message]
2026-11-26  7:28 ` [PATCH v3 2/2] livepatch: Avoid blocking tasklist_lock too long Yafang Shao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126072829.97792-2-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=pmladek@suse.com \
    --cc=sashiko-bot@kernel.org \
    --cc=song@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®