Skip to content

Commit b4c6f86

Browse files
Sebastian Andrzej SiewiorPeter Zijlstra
authored andcommitted
irq_work: Handle some irq_work in a per-CPU thread on PREEMPT_RT
The irq_work callback is invoked in hard IRQ context. By default all callbacks are scheduled for invocation right away (given supported by the architecture) except for the ones marked IRQ_WORK_LAZY which are delayed until the next timer-tick. While looking over the callbacks, some of them may acquire locks (spinlock_t, rwlock_t) which are transformed into sleeping locks on PREEMPT_RT and must not be acquired in hard IRQ context. Changing the locks into locks which could be acquired in this context will lead to other problems such as increased latencies if everything in the chain has IRQ-off locks. This will not solve all the issues as one callback has been noticed which invoked kref_put() and its callback invokes kfree() and this can not be invoked in hardirq context. Some callbacks are required to be invoked in hardirq context even on PREEMPT_RT to work properly. This includes for instance the NO_HZ callback which needs to be able to observe the idle context. The callbacks which require to be run in hardirq have already been marked. Use this information to split the callbacks onto the two lists on PREEMPT_RT: - lazy_list Work items which are not marked with IRQ_WORK_HARD_IRQ will be added to this list. Callbacks on this list will be invoked from a per-CPU thread. The handler here may acquire sleeping locks such as spinlock_t and invoke kfree(). - raised_list Work items which are marked with IRQ_WORK_HARD_IRQ will be added to this list. They will be invoked in hardirq context and must not acquire any sleeping locks. The wake up of the per-CPU thread occurs from irq_work handler/ hardirq context. The thread runs with lowest RT priority to ensure it runs before any SCHED_OTHER tasks do. [bigeasy: melt tglx's irq_work_tick_soft() which splits irq_work_tick() into a hard and soft variant. Collected fixes over time from Steven Rostedt and Mike Galbraith. Move to per-CPU threads instead of softirq as suggested by PeterZ.] Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 8109796 commit b4c6f86

File tree

1 file changed

+106
-12
lines changed

1 file changed

+106
-12
lines changed

kernel/irq_work.c

Lines changed: 106 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,36 @@
1818
#include <linux/cpu.h>
1919
#include <linux/notifier.h>
2020
#include <linux/smp.h>
21+
#include <linux/smpboot.h>
2122
#include <asm/processor.h>
2223
#include <linux/kasan.h>
2324

2425
static DEFINE_PER_CPU(struct llist_head, raised_list);
2526
static DEFINE_PER_CPU(struct llist_head, lazy_list);
27+
static DEFINE_PER_CPU(struct task_struct *, irq_workd);
28+
29+
static void wake_irq_workd(void)
30+
{
31+
struct task_struct *tsk = __this_cpu_read(irq_workd);
32+
33+
if (!llist_empty(this_cpu_ptr(&lazy_list)) && tsk)
34+
wake_up_process(tsk);
35+
}
36+
37+
#ifdef CONFIG_SMP
38+
static void irq_work_wake(struct irq_work *entry)
39+
{
40+
wake_irq_workd();
41+
}
42+
43+
static DEFINE_PER_CPU(struct irq_work, irq_work_wakeup) =
44+
IRQ_WORK_INIT_HARD(irq_work_wake);
45+
#endif
46+
47+
static int irq_workd_should_run(unsigned int cpu)
48+
{
49+
return !llist_empty(this_cpu_ptr(&lazy_list));
50+
}
2651

2752
/*
2853
* Claim the entry so that no one else will poke at it.
@@ -52,15 +77,29 @@ void __weak arch_irq_work_raise(void)
5277
/* Enqueue on current CPU, work must already be claimed and preempt disabled */
5378
static void __irq_work_queue_local(struct irq_work *work)
5479
{
80+
struct llist_head *list;
81+
bool rt_lazy_work = false;
82+
bool lazy_work = false;
83+
int work_flags;
84+
85+
work_flags = atomic_read(&work->node.a_flags);
86+
if (work_flags & IRQ_WORK_LAZY)
87+
lazy_work = true;
88+
else if (IS_ENABLED(CONFIG_PREEMPT_RT) &&
89+
!(work_flags & IRQ_WORK_HARD_IRQ))
90+
rt_lazy_work = true;
91+
92+
if (lazy_work || rt_lazy_work)
93+
list = this_cpu_ptr(&lazy_list);
94+
else
95+
list = this_cpu_ptr(&raised_list);
96+
97+
if (!llist_add(&work->node.llist, list))
98+
return;
99+
55100
/* If the work is "lazy", handle it from next tick if any */
56-
if (atomic_read(&work->node.a_flags) & IRQ_WORK_LAZY) {
57-
if (llist_add(&work->node.llist, this_cpu_ptr(&lazy_list)) &&
58-
tick_nohz_tick_stopped())
59-
arch_irq_work_raise();
60-
} else {
61-
if (llist_add(&work->node.llist, this_cpu_ptr(&raised_list)))
62-
arch_irq_work_raise();
63-
}
101+
if (!lazy_work || tick_nohz_tick_stopped())
102+
arch_irq_work_raise();
64103
}
65104

66105
/* Enqueue the irq work @work on the current CPU */
@@ -104,17 +143,34 @@ bool irq_work_queue_on(struct irq_work *work, int cpu)
104143
if (cpu != smp_processor_id()) {
105144
/* Arch remote IPI send/receive backend aren't NMI safe */
106145
WARN_ON_ONCE(in_nmi());
146+
147+
/*
148+
* On PREEMPT_RT the items which are not marked as
149+
* IRQ_WORK_HARD_IRQ are added to the lazy list and a HARD work
150+
* item is used on the remote CPU to wake the thread.
151+
*/
152+
if (IS_ENABLED(CONFIG_PREEMPT_RT) &&
153+
!(atomic_read(&work->node.a_flags) & IRQ_WORK_HARD_IRQ)) {
154+
155+
if (!llist_add(&work->node.llist, &per_cpu(lazy_list, cpu)))
156+
goto out;
157+
158+
work = &per_cpu(irq_work_wakeup, cpu);
159+
if (!irq_work_claim(work))
160+
goto out;
161+
}
162+
107163
__smp_call_single_queue(cpu, &work->node.llist);
108164
} else {
109165
__irq_work_queue_local(work);
110166
}
167+
out:
111168
preempt_enable();
112169

113170
return true;
114171
#endif /* CONFIG_SMP */
115172
}
116173

117-
118174
bool irq_work_needs_cpu(void)
119175
{
120176
struct llist_head *raised, *lazy;
@@ -170,7 +226,12 @@ static void irq_work_run_list(struct llist_head *list)
170226
struct irq_work *work, *tmp;
171227
struct llist_node *llnode;
172228

173-
BUG_ON(!irqs_disabled());
229+
/*
230+
* On PREEMPT_RT IRQ-work which is not marked as HARD will be processed
231+
* in a per-CPU thread in preemptible context. Only the items which are
232+
* marked as IRQ_WORK_HARD_IRQ will be processed in hardirq context.
233+
*/
234+
BUG_ON(!irqs_disabled() && !IS_ENABLED(CONFIG_PREEMPT_RT));
174235

175236
if (llist_empty(list))
176237
return;
@@ -187,7 +248,10 @@ static void irq_work_run_list(struct llist_head *list)
187248
void irq_work_run(void)
188249
{
189250
irq_work_run_list(this_cpu_ptr(&raised_list));
190-
irq_work_run_list(this_cpu_ptr(&lazy_list));
251+
if (!IS_ENABLED(CONFIG_PREEMPT_RT))
252+
irq_work_run_list(this_cpu_ptr(&lazy_list));
253+
else
254+
wake_irq_workd();
191255
}
192256
EXPORT_SYMBOL_GPL(irq_work_run);
193257

@@ -197,7 +261,11 @@ void irq_work_tick(void)
197261

198262
if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
199263
irq_work_run_list(raised);
200-
irq_work_run_list(this_cpu_ptr(&lazy_list));
264+
265+
if (!IS_ENABLED(CONFIG_PREEMPT_RT))
266+
irq_work_run_list(this_cpu_ptr(&lazy_list));
267+
else
268+
wake_irq_workd();
201269
}
202270

203271
/*
@@ -219,3 +287,29 @@ void irq_work_sync(struct irq_work *work)
219287
cpu_relax();
220288
}
221289
EXPORT_SYMBOL_GPL(irq_work_sync);
290+
291+
static void run_irq_workd(unsigned int cpu)
292+
{
293+
irq_work_run_list(this_cpu_ptr(&lazy_list));
294+
}
295+
296+
static void irq_workd_setup(unsigned int cpu)
297+
{
298+
sched_set_fifo_low(current);
299+
}
300+
301+
static struct smp_hotplug_thread irqwork_threads = {
302+
.store = &irq_workd,
303+
.setup = irq_workd_setup,
304+
.thread_should_run = irq_workd_should_run,
305+
.thread_fn = run_irq_workd,
306+
.thread_comm = "irq_work/%u",
307+
};
308+
309+
static __init int irq_work_init_threads(void)
310+
{
311+
if (IS_ENABLED(CONFIG_PREEMPT_RT))
312+
BUG_ON(smpboot_register_percpu_thread(&irqwork_threads));
313+
return 0;
314+
}
315+
early_initcall(irq_work_init_threads);

0 commit comments

Comments
 (0)