2 * linux/kernel/softirq.c
4 * Copyright (C) 1992 Linus Torvalds
6 * Rewritten. Old one was good in 2.2, but in 2.3 it was immoral. --ANK (990903)
9 #include <linux/module.h>
10 #include <linux/kernel_stat.h>
11 #include <linux/interrupt.h>
12 #include <linux/init.h>
14 #include <linux/notifier.h>
15 #include <linux/percpu.h>
16 #include <linux/cpu.h>
17 #include <linux/kthread.h>
21 #include <linux/syscalls.h>
24 - No shared variables, all the data are CPU local.
25 - If a softirq needs serialization, let it serialize itself
27 - Even if softirq is serialized, only local cpu is marked for
28 execution. Hence, we get something sort of weak cpu binding.
29 Though it is still not clear, will it result in better locality
33 - NET RX softirq. It is multithreaded and does not require
34 any global serialization.
35 - NET TX softirq. It kicks software netdevice queues, hence
36 it is logically serialized per device, but this serialization
37 is invisible to common code.
38 - Tasklets: serialized wrt itself.
41 #ifndef __ARCH_IRQ_STAT
42 irq_cpustat_t irq_stat[NR_CPUS] ____cacheline_aligned;
43 EXPORT_SYMBOL(irq_stat);
46 static struct softirq_action softirq_vec[32] __cacheline_aligned_in_smp;
48 static DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
50 int defer_softirqs = 1;
53 * we cannot loop indefinitely here to avoid userspace starvation,
54 * but we also don't want to introduce a worst case 1/HZ latency
55 * to the pending events, so lets the scheduler to balance
56 * the softirq load for us.
58 static inline void wakeup_softirqd(void)
60 /* Interrupts are disabled: no need to stop preemption */
61 struct task_struct *tsk = __get_cpu_var(ksoftirqd);
63 if (tsk && tsk->state != TASK_RUNNING)
68 * We restart softirq processing MAX_SOFTIRQ_RESTART times,
69 * and we fall back to softirqd after that.
71 * This number has been established via experimentation.
72 * The two things to balance is latency against fairness -
73 * we want to handle softirqs as soon as possible, but they
74 * should not be able to lock up the box.
76 #define MAX_SOFTIRQ_RESTART 10
78 asmlinkage void ___do_softirq(void)
80 struct softirq_action *h;
82 int max_restart = MAX_SOFTIRQ_RESTART;
84 pending = local_softirq_pending();
88 /* Reset the pending bitmask before enabling irqs */
89 local_softirq_pending() = 0;
98 pending ^= mask; /* clear the bit */
100 if (unlikely(defer_softirqs && need_resched())) {
102 * We are interrupting softirq processing
103 * to allow for preemption to occur. Add
104 * back any non-processed pending bits to
108 local_softirq_pending() |= pending;
119 pending = local_softirq_pending();
120 if (pending && --max_restart)
130 asmlinkage void __do_softirq(void)
132 if (likely(defer_softirqs)) {
134 * 'preempt harder'. Push all softirq processing off
135 * to ksoftirqd. This makes softirq related latencies
136 * much more predictable since they run from process
137 * context instead of hardirq context.
139 if (local_softirq_pending())
146 asmlinkage void _do_softirq(void)
154 #ifndef __ARCH_HAS_DO_SOFTIRQ
156 asmlinkage void do_softirq(void)
164 local_irq_save(flags);
166 pending = local_softirq_pending();
171 local_irq_restore(flags);
174 EXPORT_SYMBOL(do_softirq);
178 void local_bh_enable(void)
181 WARN_ON(irqs_disabled());
182 if (unlikely(!in_interrupt() &&
183 local_softirq_pending()))
185 preempt_check_resched();
187 EXPORT_SYMBOL(local_bh_enable);
190 * This function must run with irqs disabled!
192 inline fastcall void raise_softirq_irqoff(unsigned int nr)
194 __raise_softirq_irqoff(nr);
197 * If we're in an interrupt or softirq, we're done
198 * (this also catches softirq-disabled code). We will
199 * actually run the softirq once we return from
200 * the irq or softirq.
202 * Otherwise we wake up ksoftirqd to make sure we
203 * schedule the softirq soon.
209 EXPORT_SYMBOL(raise_softirq_irqoff);
211 void fastcall raise_softirq(unsigned int nr)
215 local_irq_save(flags);
216 raise_softirq_irqoff(nr);
217 local_irq_restore(flags);
220 EXPORT_SYMBOL(raise_softirq);
222 void open_softirq(int nr, void (*action)(struct softirq_action*), void *data)
224 softirq_vec[nr].data = data;
225 softirq_vec[nr].action = action;
228 EXPORT_SYMBOL(open_softirq);
233 struct tasklet_struct *list;
236 /* Some compilers disobey section attribute on statics when not
238 static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec) = { NULL };
239 static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec) = { NULL };
241 void fastcall __tasklet_schedule(struct tasklet_struct *t)
245 local_irq_save(flags);
246 t->next = __get_cpu_var(tasklet_vec).list;
247 __get_cpu_var(tasklet_vec).list = t;
248 raise_softirq_irqoff(TASKLET_SOFTIRQ);
249 local_irq_restore(flags);
252 EXPORT_SYMBOL(__tasklet_schedule);
254 void fastcall __tasklet_hi_schedule(struct tasklet_struct *t)
258 local_irq_save(flags);
259 t->next = __get_cpu_var(tasklet_hi_vec).list;
260 __get_cpu_var(tasklet_hi_vec).list = t;
261 raise_softirq_irqoff(HI_SOFTIRQ);
262 local_irq_restore(flags);
265 EXPORT_SYMBOL(__tasklet_hi_schedule);
267 static void tasklet_action(struct softirq_action *a)
269 struct tasklet_struct *list;
272 list = __get_cpu_var(tasklet_vec).list;
273 __get_cpu_var(tasklet_vec).list = NULL;
277 struct tasklet_struct *t = list;
281 if (tasklet_trylock(t)) {
282 if (!atomic_read(&t->count)) {
283 if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
293 t->next = __get_cpu_var(tasklet_vec).list;
294 __get_cpu_var(tasklet_vec).list = t;
295 __raise_softirq_irqoff(TASKLET_SOFTIRQ);
300 static void tasklet_hi_action(struct softirq_action *a)
302 struct tasklet_struct *list;
305 list = __get_cpu_var(tasklet_hi_vec).list;
306 __get_cpu_var(tasklet_hi_vec).list = NULL;
310 struct tasklet_struct *t = list;
314 if (tasklet_trylock(t)) {
315 if (!atomic_read(&t->count)) {
316 if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
326 t->next = __get_cpu_var(tasklet_hi_vec).list;
327 __get_cpu_var(tasklet_hi_vec).list = t;
328 __raise_softirq_irqoff(HI_SOFTIRQ);
334 void tasklet_init(struct tasklet_struct *t,
335 void (*func)(unsigned long), unsigned long data)
339 atomic_set(&t->count, 0);
344 EXPORT_SYMBOL(tasklet_init);
346 void tasklet_kill(struct tasklet_struct *t)
349 printk("Attempt to kill tasklet from interrupt\n");
351 while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
354 while (test_bit(TASKLET_STATE_SCHED, &t->state));
356 tasklet_unlock_wait(t);
357 clear_bit(TASKLET_STATE_SCHED, &t->state);
360 EXPORT_SYMBOL(tasklet_kill);
362 void __init softirq_init(void)
364 open_softirq(TASKLET_SOFTIRQ, tasklet_action, NULL);
365 open_softirq(HI_SOFTIRQ, tasklet_hi_action, NULL);
368 static int ksoftirqd(void * __bind_cpu)
371 set_user_nice(current, -5);
372 current->flags |= PF_NOFREEZE;
373 #else /* alternative if we want more priority for ksoftirqd */
374 struct sched_param param = { .sched_priority = MAX_RT_PRIO/4-1 };
375 param.sched_priority = 1;
376 sys_sched_setscheduler(current->pid, SCHED_RR, ¶m);
377 current->flags |= PF_NOFREEZE;
380 set_current_state(TASK_INTERRUPTIBLE);
382 while (!kthread_should_stop()) {
383 if (!local_softirq_pending())
386 __set_current_state(TASK_RUNNING);
388 while (local_softirq_pending()) {
389 /* Preempt disable stops cpu going offline.
390 If already offline, we'll be on wrong CPU:
393 if (cpu_is_offline((long)__bind_cpu))
400 set_current_state(TASK_INTERRUPTIBLE);
402 __set_current_state(TASK_RUNNING);
407 /* Wait for kthread_stop */
408 set_current_state(TASK_INTERRUPTIBLE);
409 while (!kthread_should_stop()) {
411 set_current_state(TASK_INTERRUPTIBLE);
413 __set_current_state(TASK_RUNNING);
417 #ifdef CONFIG_HOTPLUG_CPU
419 * tasklet_kill_immediate is called to remove a tasklet which can already be
420 * scheduled for execution on @cpu.
422 * Unlike tasklet_kill, this function removes the tasklet
423 * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
425 * When this function is called, @cpu must be in the CPU_DEAD state.
427 void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
429 struct tasklet_struct **i;
431 BUG_ON(cpu_online(cpu));
432 BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
434 if (!test_bit(TASKLET_STATE_SCHED, &t->state))
437 /* CPU is dead, so no lock needed. */
438 for (i = &per_cpu(tasklet_vec, cpu).list; *i; i = &(*i)->next) {
447 static void takeover_tasklets(unsigned int cpu)
449 struct tasklet_struct **i;
451 /* CPU is dead, so no lock needed. */
454 /* Find end, append list for that CPU. */
455 for (i = &__get_cpu_var(tasklet_vec).list; *i; i = &(*i)->next);
456 *i = per_cpu(tasklet_vec, cpu).list;
457 per_cpu(tasklet_vec, cpu).list = NULL;
458 raise_softirq_irqoff(TASKLET_SOFTIRQ);
460 for (i = &__get_cpu_var(tasklet_hi_vec).list; *i; i = &(*i)->next);
461 *i = per_cpu(tasklet_hi_vec, cpu).list;
462 per_cpu(tasklet_hi_vec, cpu).list = NULL;
463 raise_softirq_irqoff(HI_SOFTIRQ);
467 #endif /* CONFIG_HOTPLUG_CPU */
469 static int __devinit cpu_callback(struct notifier_block *nfb,
470 unsigned long action,
473 int hotcpu = (unsigned long)hcpu;
474 struct task_struct *p;
478 BUG_ON(per_cpu(tasklet_vec, hotcpu).list);
479 BUG_ON(per_cpu(tasklet_hi_vec, hotcpu).list);
480 p = kthread_create(ksoftirqd, hcpu, "ksoftirqd/%d", hotcpu);
482 printk("ksoftirqd for %i failed\n", hotcpu);
485 kthread_bind(p, hotcpu);
486 per_cpu(ksoftirqd, hotcpu) = p;
489 wake_up_process(per_cpu(ksoftirqd, hotcpu));
491 #ifdef CONFIG_HOTPLUG_CPU
492 case CPU_UP_CANCELED:
493 /* Unbind so it can run. Fall thru. */
494 kthread_bind(per_cpu(ksoftirqd, hotcpu), smp_processor_id());
496 p = per_cpu(ksoftirqd, hotcpu);
497 per_cpu(ksoftirqd, hotcpu) = NULL;
499 takeover_tasklets(hotcpu);
501 #endif /* CONFIG_HOTPLUG_CPU */
506 static struct notifier_block __devinitdata cpu_nfb = {
507 .notifier_call = cpu_callback
510 __init int spawn_ksoftirqd(void)
512 void *cpu = (void *)(long)smp_processor_id();
513 cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
514 cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
515 register_cpu_notifier(&cpu_nfb);