Which context are softirq and tasklet in?

13,124

Solution 1

@kai: your qs reg which context bottom-halves execute in?

Technically, softirq's do run in an interrupt context - the "softirq" context; it's just that it's not "hard-irq" context (which is the context when a hardware interrupt occurs).

So, in a softirq handler, in terms of the 'lookup' macros Linux provides:

in_interrupt: yes | in_irq: no | in_softirq: yes | in_serving_softirq: yes

But be aware (beware!!! :): "all of the restrictions that apply to interrupt handlers also apply to bottom halves. Thus, bottom halves cannot sleep, cannot access user space, and cannot invoke the scheduler." -- LDD3.

Jermaine answers the rest of your question.

[Update] In addition, I'd like to point out that one can define simple and elegant macros that help print debug info as and when required. Over the years, I've put these macros and convenience routines into a header file; you can check it out and download it here: "A Header of Convenience".

There are macros / functions to:

  • make debug prints along with funcname / line# info (via the usual printk() or trace_printk()) and only if DEBUG mode is On
    • dump the kernel-mode stack
    • print the current context (process or interrupt along with flags in the form that ftrace uses)
    • a simple assert() macro (!)
    • a cpu-intensive DELAY_LOOP (useful for test rigs that must spin on the processor)
    • an equivalent to usermode sleep functionality
    • a function to calculate the time delta given two timestamps (timeval structs)
    • convert decimal to binary, and
    • a few more.

Whew :-)

Solution 2

I agree with the accepted answer and Kaiwan's answer, but they did not mention ksoftirqd. If the CPU is under heavy load of softirqs and/or tasklets, it schedules its ksoftirqd thread which process the raised softirqs and tasklets in process context.

So I guess the answer to the OP's question would be: softirqs can run in either interrupt or process contexts.

Update: A quick look on run_ksoftirqd() shows that __do_softirq() is called with local irqs disabled. So, while technically running in process context, same restrictions (like no sleeping) apply.

Share:
13,124

Related videos on Youtube

kai
Author by

kai

Updated on June 04, 2022

Comments

  • kai
    kai almost 2 years

    I know that there are process context and interrupt context but I don't understand when executing softirq or tasklet, which context is it run under.

    I have seen some people use the term "bottom-halves context", if there's such term, what's the difference comparing with the others.

    Another question to softirq and tasklet is that why sleep are not allowed during execution??

    Can anyone help me identify these questions, thanks!!

  • Mohammad Ali Asgar
    Mohammad Ali Asgar over 9 years
    Hello srd, this is exactly what has been bothering me! I read in Linux Kernel Development [Robert Love] that softirqs and tasklets run in interrrupt context. The book also mentions ksoftirqd which is a kernel thread (and therefore runs in process context). As you know, this thread is employed for each processor in order to run softirqs/tasklets when many softirqs/tasklets are being raised due to some reason (for example, high traffic load on a network card). But how does a thread (process context) run a softirq/tasklet which is meant to run in interrupt context only?! I still don't understand.
  • Catalin
    Catalin over 3 years
    Hi @MohammadAliAsgar! I'm reading the same book (3rd edition), and from what I understand: a 'softirq' action will be executed when do_softirq() is called; this function can be called either from an interrupt context, on the return path of an interrupt handler ("hardirq"), or from a process context (ksoftirqd kernel thread or other code, like the networking subsystem). This is how the code can run in either context. And since it can run in interrupt context, it must not sleep because the scheduler won't be able to reschedule it. Take this with a grain of salt, but hope it helps
  • Chan Kim
    Chan Kim over 2 years
    @Catalin Hi thanks, what about tasklet? Are they called in the same manner (do_softirq())?
  • Catalin
    Catalin over 2 years
    @ChanKim From my reading of the book mentioned above, yes, tasklets are called in the same manner. They are built on top of sotfirqs, idea being that it is unlikely that you need to add a new softirq to the system, but you are free to add tasklets, which will be executed when processing specific softirqs (HI_SOFTIRQ and TASKLET_SOFTIRQ, afaiu)
  • Chan Kim
    Chan Kim over 2 years
    @Catalin ah, I now rememer tasklet is one of softirqs! thanks!