Skip to content

Commit d75abd0

Browse files
Waiman-Longakpm00
authored andcommitted
mm/memory-failure: use raw_spinlock_t in struct memory_failure_cpu
The memory_failure_cpu structure is a per-cpu structure. Access to its content requires the use of get_cpu_var() to lock in the current CPU and disable preemption. The use of a regular spinlock_t for locking purpose is fine for a non-RT kernel. Since the integration of RT spinlock support into the v5.15 kernel, a spinlock_t in a RT kernel becomes a sleeping lock and taking a sleeping lock in a preemption disabled context is illegal resulting in the following kind of warning. [12135.732244] BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48 [12135.732248] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 270076, name: kworker/0:0 [12135.732252] preempt_count: 1, expected: 0 [12135.732255] RCU nest depth: 2, expected: 2 : [12135.732420] Hardware name: Dell Inc. PowerEdge R640/0HG0J8, BIOS 2.10.2 02/24/2021 [12135.732423] Workqueue: kacpi_notify acpi_os_execute_deferred [12135.732433] Call Trace: [12135.732436] <TASK> [12135.732450] dump_stack_lvl+0x57/0x81 [12135.732461] __might_resched.cold+0xf4/0x12f [12135.732479] rt_spin_lock+0x4c/0x100 [12135.732491] memory_failure_queue+0x40/0xe0 [12135.732503] ghes_do_memory_failure+0x53/0x390 [12135.732516] ghes_do_proc.constprop.0+0x229/0x3e0 [12135.732575] ghes_proc+0xf9/0x1a0 [12135.732591] ghes_notify_hed+0x6a/0x150 [12135.732602] notifier_call_chain+0x43/0xb0 [12135.732626] blocking_notifier_call_chain+0x43/0x60 [12135.732637] acpi_ev_notify_dispatch+0x47/0x70 [12135.732648] acpi_os_execute_deferred+0x13/0x20 [12135.732654] process_one_work+0x41f/0x500 [12135.732695] worker_thread+0x192/0x360 [12135.732715] kthread+0x111/0x140 [12135.732733] ret_from_fork+0x29/0x50 [12135.732779] </TASK> Fix it by using a raw_spinlock_t for locking instead. Also move the pr_err() out of the lock critical section and after put_cpu_ptr() to avoid indeterminate latency and the possibility of sleep with this call. [longman@redhat.com: don't hold percpu ref across pr_err(), per Miaohe] Link: https://lkml.kernel.org/r/20240807181130.1122660-1-longman@redhat.com Link: https://lkml.kernel.org/r/20240806164107.1044956-1-longman@redhat.com Fixes: 0f383b6 ("locking/spinlock: Provide RT variant") Signed-off-by: Waiman Long <longman@redhat.com> Acked-by: Miaohe Lin <linmiaohe@huawei.com> Cc: "Huang, Ying" <ying.huang@intel.com> Cc: Juri Lelli <juri.lelli@redhat.com> Cc: Len Brown <len.brown@intel.com> Cc: Naoya Horiguchi <nao.horiguchi@gmail.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 9d85731 commit d75abd0

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

mm/memory-failure.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ struct memory_failure_entry {
24172417
struct memory_failure_cpu {
24182418
DECLARE_KFIFO(fifo, struct memory_failure_entry,
24192419
MEMORY_FAILURE_FIFO_SIZE);
2420-
spinlock_t lock;
2420+
raw_spinlock_t lock;
24212421
struct work_struct work;
24222422
};
24232423

@@ -2443,20 +2443,22 @@ void memory_failure_queue(unsigned long pfn, int flags)
24432443
{
24442444
struct memory_failure_cpu *mf_cpu;
24452445
unsigned long proc_flags;
2446+
bool buffer_overflow;
24462447
struct memory_failure_entry entry = {
24472448
.pfn = pfn,
24482449
.flags = flags,
24492450
};
24502451

24512452
mf_cpu = &get_cpu_var(memory_failure_cpu);
2452-
spin_lock_irqsave(&mf_cpu->lock, proc_flags);
2453-
if (kfifo_put(&mf_cpu->fifo, entry))
2453+
raw_spin_lock_irqsave(&mf_cpu->lock, proc_flags);
2454+
buffer_overflow = !kfifo_put(&mf_cpu->fifo, entry);
2455+
if (!buffer_overflow)
24542456
schedule_work_on(smp_processor_id(), &mf_cpu->work);
2455-
else
2457+
raw_spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
2458+
put_cpu_var(memory_failure_cpu);
2459+
if (buffer_overflow)
24562460
pr_err("buffer overflow when queuing memory failure at %#lx\n",
24572461
pfn);
2458-
spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
2459-
put_cpu_var(memory_failure_cpu);
24602462
}
24612463
EXPORT_SYMBOL_GPL(memory_failure_queue);
24622464

@@ -2469,9 +2471,9 @@ static void memory_failure_work_func(struct work_struct *work)
24692471

24702472
mf_cpu = container_of(work, struct memory_failure_cpu, work);
24712473
for (;;) {
2472-
spin_lock_irqsave(&mf_cpu->lock, proc_flags);
2474+
raw_spin_lock_irqsave(&mf_cpu->lock, proc_flags);
24732475
gotten = kfifo_get(&mf_cpu->fifo, &entry);
2474-
spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
2476+
raw_spin_unlock_irqrestore(&mf_cpu->lock, proc_flags);
24752477
if (!gotten)
24762478
break;
24772479
if (entry.flags & MF_SOFT_OFFLINE)
@@ -2501,7 +2503,7 @@ static int __init memory_failure_init(void)
25012503

25022504
for_each_possible_cpu(cpu) {
25032505
mf_cpu = &per_cpu(memory_failure_cpu, cpu);
2504-
spin_lock_init(&mf_cpu->lock);
2506+
raw_spin_lock_init(&mf_cpu->lock);
25052507
INIT_KFIFO(mf_cpu->fifo);
25062508
INIT_WORK(&mf_cpu->work, memory_failure_work_func);
25072509
}

0 commit comments

Comments
 (0)