From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796031260; b=x+jZcCSNSoSOWpCe4KI7eA2kborz4QwNHInXq61jQopUBxMPH0l/D//J0mnlChQYrbwH bctrQbM4Dm8Uy8jZtsTpCasn2Druj3wHqhI/f+wGpl1LE+LnwW1pMLfsJyRFtFHIm1+BF iAUGxWD7dY3PFLXZORnuh/85fMmuBhIwI4ieT1oXNdsuqybXx7H8aYTYxFkWbk5gw5byz r0TNUIjPFr5mkPN3bJ68hv7RhCSREVrX+4xSvAUU92C7EchoIxP99we9Jdkz79tQSpOpy rNIoxCoBnQ/hHlOFlqEuvsOqxV3KrANxk+kv+VGJ/22UV2gMEOA8vzpV13vgUF2goAQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796031260; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=j5+ryoMdplJQQ62fiISgLypJTDyFIwsUf7qpzfNeQdQ=; b=G8tQt3/IxgNQX286y24xZpq3DJkPf7PCVQY2CNO0wrmfjU7UYYiiHk93ZxEhT9Wr8mW3 w5PMAk0zqXXE1LLss4/wFU7ipFghG0lYSLxBJlk4o1io6+N2q4W6/2EgtEl02uYDo6E2H 1c+nNlQgHMdVEXCqXyz/FCrKvatyUeJmsXeuvkm00SJVZ0/uPMfQImB59pa4PPhZOfj/l x8Eb2BlW0yVusOjp06SBdYnk6NK1fS6OR+6qk0VmDGmfyX2Y900anVRn8hO7VnoH6DdDD aa4pu6PKKq4a0QQ88WVq2wyMt2B+oIJ9WYC5jog4Yi3jAFzDR24LeqqskrAPGndIlpw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=DFcHn+Yy; arc=none smtp.remote-ip=209.85.221.43 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C82B9606A33D for ; Mon, 30 Nov 2026 09:34:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C82B9606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=DFcHn+Yy Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-48dee593f18so9774587f8f.3 for ; Mon, 30 Nov 2026 01:34:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796031257; x=1796636057; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=j5+ryoMdplJQQ62fiISgLypJTDyFIwsUf7qpzfNeQdQ=; b=DFcHn+YyuBUuTUd6bjIZxwZTuVOKDFaXjrO0vzYNqvNg/bkbmZsV0mVGOZPx3dOwjV FA+L8wikOLZytGatZZOgRLF8Sf9KCgR8vprFqThJc1tnIMetQT8hybeRXtwJYea8zAfd Fd1mNx5W5t65Vh/Ovn4mUkkPluTGgCmxyT+Riy8t0xm4tEa5tHRZFInRZhnJua4N5GnU TKpFFv/uVvAr8GfYiBAVa8M8+oGjazrNP+sZjVswxABUK+CE/B5/p1zS/o/gjylWy5Nb m+ENvGNgisLjWw8RyN97WXRNYp/bCPXD3GtVqDWPpotLyBIx7K0Y6Gygi3gYVoZMSjZo qoqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796031257; x=1796636057; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=j5+ryoMdplJQQ62fiISgLypJTDyFIwsUf7qpzfNeQdQ=; b=hCSh81RlMZIni9xOWeuUQpp5dkxpKledveMUvNIyePXAy3qmWOAWJfUbPMZ6bcQcA5 UYQVqcgUCjc0zGUHmRs68L0OVsuSF3mRb6PD49tA79Kgnbz7NbUqwG8PZVSd5OjKTl13 K34ccnu+EMEPrf9N3xnv8sLfeWKkESKWop1bi9V9Ba6zc6VxSB2rlX+MMyuxPV6Eq+Nu HCsCGAY2BDBSJdoMSwYLX8vDJK2bg5DQWHrlT79KuqdEyR0HDfE/cNOM0f+rX3nGBgdP qtFYkW+yTippHcn521GPHSVjbLWbHsK5pM/mkJ6SWO6D6LNzQzJBPAG2p/Yu1Ut/PUAz 41Eg== X-Forwarded-Encrypted: i=1; AL+hErHUclX1/t4WPmQlJ/pLtyGuJFuoJMZM2gaX4czBTd6sEi18lqDHao8GvSHoSJNnkY9dUGdCNzpdH1eHw4Y=@vger.kernel.org X-Gm-Message-State: APuU4Z9BwOfTUvv0rPsNCSdv/q2N1x1bemG0wFmPs9BPpQ6vVNd/jmEG BR9ZMCu2gFsySkI0cDl1/SONkk7BN73ovVWouBR0ag3mU76hf73YQBaG X-Gm-Gg: AZFdTZdGk4RubR2LuWDOcX9v4jbwP5gRh5dq6CQSo3x6YnmW4yEmYeWhBXCc61PWPR/ MZldO9B5jUSvfaQQ27qxfKcEmrfR+v1SvTfSiJc9O9TxMeQS4hTh4LLVhrT1n0wF2BNLOeWV0aK nTtDAaItQYNzf/R+DcEyISHgjl9rQw5/mmH3a+bqR1+wc8lPi5CKZvZZbY9Z1NBjOC7oZYzeZOs qCjMaCC5TFdnjUOpMdpTVtS1m4aEZo+BtwcSJmHgbjXDH+xTWbO2rlGw9HJdEJ/WEgSBjw4Unia KwPM3vL5FvGUiz8mEvx/kSdQOrC3q1tr113PKWPnkpm2BmzrdMOFJrppp916V2+ctOngThlTfEk hj6mI6KTiNGoCsVx8PJhCjc8PF5BHcdgfnXpH/X5depYAH1awBI7xozZkme/dL72GgDiC8KpFbE I00Tu3EVfYUgayaWOEjCJzMthqdPQ70ZEL/ndzTNQ0hLJxw37GjU4OsmoSZJhV+TBhtGTE5TTKK rERtZbA4adxlUSa0BoUZOgwJ93J+VZEZiAB2ouG9cYYUlKxohTnlvWKQX9YsSUzUNdLQfVeC5TR U0heFGUhYjvL3SpZ6BW+w4K2kJLvQtf5+v5idHWs/7fc5Ni4VmSAt0yzqvKbo4h3GGEfXKKlStZ 0Ky1i4BWftePAeVgVMj1Xay1C43ulGNwr/AIGYYk= X-Received: by 2002:a5d:574e:0:b0:4a4:f208:6a4c with SMTP id ffacd0b85a97d-4abb41f4dbbmr49884739f8f.43.1796031256864; Mon, 30 Nov 2026 01:34:16 -0800 (PST) Received: from andreayoga.localdomain (93-42-14-189.ip84.fastwebnet.it. [93.42.14.189]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4b8e564e39asm27934876f8f.57.2026.11.30.01.34.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 01:34:16 -0800 (PST) From: Andrea Parri To: Sebastian Andrzej Siewior , Peter Zijlstra , Thomas Gleixner Cc: Jiayuan Chen , Steven Rostedt , Clark Williams , Vlastimil Babka , linux-rt-devel@lists.linux.dev, linux-kernel@vger.kernel.org, Sashiko 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 Message-ID: <20261130093411.5582-1-parri.andrea@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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 Closes: https://sashiko.dev/#/patchset/20260911143815.997254-1-bigeasy@linutronix.de?part=2 Signed-off-by: Andrea Parri --- 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 #include #include #include #include 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