mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrea Parri <parri.andrea@gmail.com>
To: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@kernel.org>
Cc: Jiayuan Chen <jiayuan.chen@linux.dev>,
	Steven Rostedt <rostedt@goodmis.org>,
	Clark Williams <clrkwllms@kernel.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	linux-rt-devel@lists.linux.dev, linux-kernel@vger.kernel.org,
	Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH] irq_work: Fix use-after-free in irq_work_run_cpu() on PREEMPT_RT
Date: Mon, 30 Nov 2026 10:34:10 +0100	[thread overview]
Message-ID: <20261130093411.5582-1-parri.andrea@gmail.com> (raw)

On PREEMPT_RT, irq_work_sync() on a !IRQ_WORK_HARD_IRQ item waits for
IRQ_WORK_BUSY to clear and then calls synchronize_rcu(), because
irq_work_single() still dereferences @work after it clears
IRQ_WORK_BUSY.  This relies on irq_work_single() running such items
inside an RCU read-side critical section, which run_irq_workd() ensures
with guard(rcu)().

irq_work_run_cpu(), called from smpcfd_dead_cpu() on the control CPU,
runs the dead CPU's lazy_list in preemptible task context without
holding the RCU read lock.  If it is preempted after IRQ_WORK_BUSY is
cleared, irq_work_sync() on another CPU can return before
irq_work_single() is done with @work, because the grace period it waits
for does not wait for irq_work_run_cpu().  The caller can then free
@work while irq_work_single() still uses it:

  CPU 0                               CPU 1
  smpcfd_dead_cpu()
    irq_work_run_cpu()
      irq_work_single(work)
        work->func(work)
        atomic_cmpxchg()  // clear BUSY
        <preempted>
                                      irq_work_sync(work)
                                        rcuwait_wait_event()  // !BUSY
                                        synchronize_rcu()
                                      free work
        irq_work_is_hard(work)  // use-after-free
        rcuwait_wake_up(&work->irqwait)

Take guard(rcu)() in irq_work_run_cpu() as run_irq_workd() does, over
the whole drain.  This should be safe because irq_workd runs the same
callbacks under the RCU read lock across a whole batch, with preemption
enabled and sleeping spinlocks allowed.

The fix, the changelog and the test were written with the help of an
LLM.

Fixes: 40dcc9bdbef3 ("irq_work: Flush lazy work CPU down on PREEMPT_RT")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260911143815.997254-1-bigeasy@linutronix.de?part=2
Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
---
Sashiko reported this on v1 of the series [1] and Sebastian acknowledged
it [2], but the patch was applied unchanged.

Tested on a 4-vCPU PREEMPT_RT=y x86_64 guest (v7.4-rc5, PROVE_LOCKING,
PROVE_RCU, DEBUG_ATOMIC_SLEEP) with the module below.  It queues a
!IRQ_WORK_HARD_IRQ irq_work from a kprobe on smpcfd_dying_cpu(), so that
irq_work_run_cpu() runs it on the control CPU.  100 offline/online
rounds of CPUs 1-3:

  before: irqw_hp: queued=300 runs=300 remote=300 preemptible=300 rcu_read_lock_held=0
  after:  irqw_hp: queued=300 runs=300 remote=300 preemptible=300 rcu_read_lock_held=300

No lockdep, RCU or might_sleep splats in either run.  The use-after-free
itself was not reproduced: the window between the IRQ_WORK_BUSY clear
and rcuwait_wake_up() is a few instructions.

The fix relies on the synchronize_rcu() in irq_work_sync(), whose cost
is under discussion [3].  If that is replaced by a scheme in which
irq_work_single() no longer touches @work after clearing IRQ_WORK_BUSY,
the guard(rcu)() added here and the one in run_irq_workd() can go.

I kept the guard at the call site, as in run_irq_workd(), rather than
taking rcu_read_lock() inside irq_work_single().  The latter would
cover every caller and keep the read-side critical sections short, but
it changes the scheme for all callers, which seems better decided
together with [3] than in an -rc fix.

With the guard at the call sites, nothing checks that irq_work_single()
runs inside an RCU read-side critical section, so lockdep does not flag
a preemptible caller like this one.  If the guards stay at the call
sites, I can follow up with an RCU_LOCKDEP_WARN() on
!rcu_read_lock_any_held() in irq_work_single() for the next merge
window.

[1] https://lore.kernel.org/all/20260911145555.8BE8C1F000FF@smtp.kernel.org/
[2] https://lore.kernel.org/all/20260914072623.OegMPpLL@linutronix.de/
[3] https://lore.kernel.org/all/5c553fca-3a7a-4bd6-b41a-00594f854440@kernel.org/

  // SPDX-License-Identifier: GPL-2.0
  /*
   * Put a !IRQ_WORK_HARD_IRQ irq_work on a dying CPU's lazy_list after its
   * irq_workd thread has parked, so that the PREEMPT_RT flush in
   * smpcfd_dead_cpu() -> irq_work_run_cpu() runs it on the control CPU.
   * The callback records the context it runs in.
   */
  #include <linux/module.h>
  #include <linux/kprobes.h>
  #include <linux/irq_work.h>
  #include <linux/rcupdate.h>
  #include <linux/smp.h>

  static atomic_t queued, runs, remote, preempt_on, rcu_held;
  static int queued_cpu = -1;

  static void irqw_hp_func(struct irq_work *work)
  {
  	atomic_inc(&runs);
  	if (raw_smp_processor_id() != READ_ONCE(queued_cpu))
  		atomic_inc(&remote);
  	if (preemptible())
  		atomic_inc(&preempt_on);
  	if (rcu_read_lock_held())
  		atomic_inc(&rcu_held);
  }

  static struct irq_work irqw_hp_work = IRQ_WORK_INIT(irqw_hp_func);

  /* smpcfd_dying_cpu() runs on the dying CPU, IRQs off, irq_workd parked. */
  static int irqw_hp_pre(struct kprobe *p, struct pt_regs *regs)
  {
  	WRITE_ONCE(queued_cpu, smp_processor_id());
  	if (irq_work_queue(&irqw_hp_work))
  		atomic_inc(&queued);
  	return 0;
  }

  static struct kprobe irqw_hp_kp = {
  	.symbol_name	= "smpcfd_dying_cpu",
  	.pre_handler	= irqw_hp_pre,
  };

  static int __init irqw_hp_init(void)
  {
  	return register_kprobe(&irqw_hp_kp);
  }

  static void __exit irqw_hp_exit(void)
  {
  	unregister_kprobe(&irqw_hp_kp);
  	irq_work_sync(&irqw_hp_work);
  	pr_info("irqw_hp: queued=%d runs=%d remote=%d preemptible=%d rcu_read_lock_held=%d\n",
  		atomic_read(&queued), atomic_read(&runs), atomic_read(&remote),
  		atomic_read(&preempt_on), atomic_read(&rcu_held));
  }

  module_init(irqw_hp_init);
  module_exit(irqw_hp_exit);
  MODULE_DESCRIPTION("Exercise irq_work_run_cpu() on CPU offline");
  MODULE_LICENSE("GPL");

 kernel/irq_work.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/irq_work.c b/kernel/irq_work.c
index 73eabcbdcd50c..121f604569d17 100644
--- a/kernel/irq_work.c
+++ b/kernel/irq_work.c
@@ -271,6 +271,7 @@ void irq_work_run_cpu(unsigned int cpu)
 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
 		return;
 
+	guard(rcu)();
 	irq_work_run_list(per_cpu_ptr(&lazy_list, cpu));
 }
 
-- 
2.53.0


                 reply	other threads:[~2026-11-30  9:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261130093411.5582-1-parri.andrea@gmail.com \
    --to=parri.andrea@gmail.com \
    --cc=bigeasy@linutronix.de \
    --cc=clrkwllms@kernel.org \
    --cc=jiayuan.chen@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sashiko-bot@kernel.org \
    --cc=tglx@kernel.org \
    --cc=vbabka@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®