1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra 4 * 5 * Provides a framework for enqueueing and running callbacks from hardirq 6 * context. The enqueueing is NMI-safe. 7 */ 8 9 #include <linux/bug.h> 10 #include <linux/kernel.h> 11 #include <linux/export.h> 12 #include <linux/irq_work.h> 13 #include <linux/percpu.h> 14 #include <linux/hardirq.h> 15 #include <linux/irqflags.h> 16 #include <linux/sched.h> 17 #include <linux/tick.h> 18 #include <linux/cpu.h> 19 #include <linux/notifier.h> 20 #include <linux/smp.h> 21 #include <linux/smpboot.h> 22 #include <asm/processor.h> 23 #include <linux/kasan.h> 24 25 #include <trace/events/ipi.h> 26 27 static DEFINE_PER_CPU(struct llist_head, raised_list); 28 static DEFINE_PER_CPU(struct llist_head, lazy_list); 29 static DEFINE_PER_CPU(struct task_struct *, irq_workd); 30 31 static void wake_irq_workd(void) 32 { 33 struct task_struct *tsk = __this_cpu_read(irq_workd); 34 35 if (!llist_empty(this_cpu_ptr(&lazy_list)) && tsk) 36 wake_up_process(tsk); 37 } 38 39 #ifdef CONFIG_SMP 40 static void irq_work_wake(struct irq_work *entry) 41 { 42 wake_irq_workd(); 43 } 44 45 static DEFINE_PER_CPU(struct irq_work, irq_work_wakeup) = 46 IRQ_WORK_INIT_HARD(irq_work_wake); 47 #endif 48 49 static int irq_workd_should_run(unsigned int cpu) 50 { 51 return !llist_empty(this_cpu_ptr(&lazy_list)); 52 } 53 54 /* 55 * Claim the entry so that no one else will poke at it. 56 */ 57 static bool irq_work_claim(struct irq_work *work) 58 { 59 int oflags; 60 61 oflags = atomic_fetch_or(IRQ_WORK_CLAIMED | CSD_TYPE_IRQ_WORK, &work->node.a_flags); 62 /* 63 * If the work is already pending, no need to raise the IPI. 64 * The pairing smp_mb() in irq_work_single() makes sure 65 * everything we did before is visible. 66 */ 67 if (oflags & IRQ_WORK_PENDING) 68 return false; 69 return true; 70 } 71 72 void __weak arch_irq_work_raise(void) 73 { 74 /* 75 * Lame architectures will get the timer tick callback 76 */ 77 } 78 79 static __always_inline void irq_work_raise(struct irq_work *work) 80 { 81 if (trace_ipi_send_cpumask_enabled() && arch_irq_work_has_interrupt()) 82 trace_ipi_send_cpumask(cpumask_of(smp_processor_id()), 83 _RET_IP_, 84 work->func); 85 86 arch_irq_work_raise(); 87 } 88 89 /* Enqueue on current CPU, work must already be claimed and preempt disabled */ 90 static void __irq_work_queue_local(struct irq_work *work) 91 { 92 struct llist_head *list; 93 bool rt_lazy_work = false; 94 bool lazy_work = false; 95 int work_flags; 96 97 work_flags = atomic_read(&work->node.a_flags); 98 if (work_flags & IRQ_WORK_LAZY) 99 lazy_work = true; 100 else if (IS_ENABLED(CONFIG_PREEMPT_RT) && 101 !(work_flags & IRQ_WORK_HARD_IRQ)) 102 rt_lazy_work = true; 103 104 if (lazy_work || rt_lazy_work) 105 list = this_cpu_ptr(&lazy_list); 106 else 107 list = this_cpu_ptr(&raised_list); 108 109 if (!llist_add(&work->node.llist, list)) 110 return; 111 112 /* If the work is "lazy", handle it from next tick if any */ 113 if (!lazy_work || tick_nohz_tick_stopped()) 114 irq_work_raise(work); 115 } 116 117 /* Enqueue the irq work @work on the current CPU */ 118 bool irq_work_queue(struct irq_work *work) 119 { 120 /* Only queue if not already pending */ 121 if (!irq_work_claim(work)) 122 return false; 123 124 /* Queue the entry and raise the IPI if needed. */ 125 preempt_disable(); 126 __irq_work_queue_local(work); 127 preempt_enable(); 128 129 return true; 130 } 131 EXPORT_SYMBOL_GPL(irq_work_queue); 132 133 /* 134 * Enqueue the irq_work @work on @cpu unless it's already pending 135 * somewhere. 136 * 137 * Can be re-enqueued while the callback is still in progress. 138 */ 139 bool irq_work_queue_on(struct irq_work *work, int cpu) 140 { 141 #ifndef CONFIG_SMP 142 return irq_work_queue(work); 143 144 #else /* CONFIG_SMP: */ 145 /* All work should have been flushed before going offline */ 146 WARN_ON_ONCE(cpu_is_offline(cpu)); 147 148 /* Only queue if not already pending */ 149 if (!irq_work_claim(work)) 150 return false; 151 152 kasan_record_aux_stack_noalloc(work); 153 154 preempt_disable(); 155 if (cpu != smp_processor_id()) { 156 /* Arch remote IPI send/receive backend aren't NMI safe */ 157 WARN_ON_ONCE(in_nmi()); 158 159 /* 160 * On PREEMPT_RT the items which are not marked as 161 * IRQ_WORK_HARD_IRQ are added to the lazy list and a HARD work 162 * item is used on the remote CPU to wake the thread. 163 */ 164 if (IS_ENABLED(CONFIG_PREEMPT_RT) && 165 !(atomic_read(&work->node.a_flags) & IRQ_WORK_HARD_IRQ)) { 166 167 if (!llist_add(&work->node.llist, &per_cpu(lazy_list, cpu))) 168 goto out; 169 170 work = &per_cpu(irq_work_wakeup, cpu); 171 if (!irq_work_claim(work)) 172 goto out; 173 } 174 175 __smp_call_single_queue(cpu, &work->node.llist); 176 } else { 177 __irq_work_queue_local(work); 178 } 179 out: 180 preempt_enable(); 181 182 return true; 183 #endif /* CONFIG_SMP */ 184 } 185 186 bool irq_work_needs_cpu(void) 187 { 188 struct llist_head *raised, *lazy; 189 190 raised = this_cpu_ptr(&raised_list); 191 lazy = this_cpu_ptr(&lazy_list); 192 193 if (llist_empty(raised) || arch_irq_work_has_interrupt()) 194 if (llist_empty(lazy)) 195 return false; 196 197 /* All work should have been flushed before going offline */ 198 WARN_ON_ONCE(cpu_is_offline(smp_processor_id())); 199 200 return true; 201 } 202 203 void irq_work_single(void *arg) 204 { 205 struct irq_work *work = arg; 206 int flags; 207 208 /* 209 * Clear the PENDING bit, after this point the @work can be re-used. 210 * The PENDING bit acts as a lock, and we own it, so we can clear it 211 * without atomic ops. 212 */ 213 flags = atomic_read(&work->node.a_flags); 214 flags &= ~IRQ_WORK_PENDING; 215 atomic_set(&work->node.a_flags, flags); 216 217 /* 218 * See irq_work_claim(). 219 */ 220 smp_mb(); 221 222 lockdep_irq_work_enter(flags); 223 work->func(work); 224 lockdep_irq_work_exit(flags); 225 226 /* 227 * Clear the BUSY bit, if set, and return to the free state if no-one 228 * else claimed it meanwhile. 229 */ 230 (void)atomic_cmpxchg(&work->node.a_flags, flags, flags & ~IRQ_WORK_BUSY); 231 232 if ((IS_ENABLED(CONFIG_PREEMPT_RT) && !irq_work_is_hard(work)) || 233 !arch_irq_work_has_interrupt()) 234 rcuwait_wake_up(&work->irqwait); 235 } 236 237 static void irq_work_run_list(struct llist_head *list) 238 { 239 struct irq_work *work, *tmp; 240 struct llist_node *llnode; 241 242 /* 243 * On PREEMPT_RT IRQ-work which is not marked as HARD will be processed 244 * in a per-CPU thread in preemptible context. Only the items which are 245 * marked as IRQ_WORK_HARD_IRQ will be processed in hardirq context. 246 */ 247 BUG_ON(!irqs_disabled() && !IS_ENABLED(CONFIG_PREEMPT_RT)); 248 249 if (llist_empty(list)) 250 return; 251 252 llnode = llist_del_all(list); 253 llist_for_each_entry_safe(work, tmp, llnode, node.llist) 254 irq_work_single(work); 255 } 256 257 /* 258 * hotplug calls this through: 259 * hotplug_cfd() -> flush_smp_call_function_queue() 260 */ 261 void irq_work_run(void) 262 { 263 irq_work_run_list(this_cpu_ptr(&raised_list)); 264 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 265 irq_work_run_list(this_cpu_ptr(&lazy_list)); 266 else 267 wake_irq_workd(); 268 } 269 EXPORT_SYMBOL_GPL(irq_work_run); 270 271 void irq_work_tick(void) 272 { 273 struct llist_head *raised = this_cpu_ptr(&raised_list); 274 275 if (!llist_empty(raised) && !arch_irq_work_has_interrupt()) 276 irq_work_run_list(raised); 277 278 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 279 irq_work_run_list(this_cpu_ptr(&lazy_list)); 280 else 281 wake_irq_workd(); 282 } 283 284 /* 285 * Synchronize against the irq_work @entry, ensures the entry is not 286 * currently in use. 287 */ 288 void irq_work_sync(struct irq_work *work) 289 { 290 lockdep_assert_irqs_enabled(); 291 might_sleep(); 292 293 if ((IS_ENABLED(CONFIG_PREEMPT_RT) && !irq_work_is_hard(work)) || 294 !arch_irq_work_has_interrupt()) { 295 rcuwait_wait_event(&work->irqwait, !irq_work_is_busy(work), 296 TASK_UNINTERRUPTIBLE); 297 return; 298 } 299 300 while (irq_work_is_busy(work)) 301 cpu_relax(); 302 } 303 EXPORT_SYMBOL_GPL(irq_work_sync); 304 305 static void run_irq_workd(unsigned int cpu) 306 { 307 irq_work_run_list(this_cpu_ptr(&lazy_list)); 308 } 309 310 static void irq_workd_setup(unsigned int cpu) 311 { 312 sched_set_fifo_low(current); 313 } 314 315 static struct smp_hotplug_thread irqwork_threads = { 316 .store = &irq_workd, 317 .setup = irq_workd_setup, 318 .thread_should_run = irq_workd_should_run, 319 .thread_fn = run_irq_workd, 320 .thread_comm = "irq_work/%u", 321 }; 322 323 static __init int irq_work_init_threads(void) 324 { 325 if (IS_ENABLED(CONFIG_PREEMPT_RT)) 326 BUG_ON(smpboot_register_percpu_thread(&irqwork_threads)); 327 return 0; 328 } 329 early_initcall(irq_work_init_threads); 330