Linux Soft Interrupt

Source: Internet
Author: User

This article reprinted from: http://blog.chinaunix.net/uid-9620812-id-3833377.html, if necessary, please visit.

Technorati Tags: Linux soft interrupt

---------------------------------------I'm a split line----------------------------------------

first, soft interrupt registration
Similar to hard interrupts, soft interrupts also have a similar interrupt vector table, but are implemented using "software".
struct Softirq_action softirq_vec[32] is a soft interrupt vector table
(File linux_2_6_24/kernel/softirq.c)

    1. struct softirq_action
    2. {
    3. void (*action) (struct softirq_action *); hook function
    4. void *data; The formal parameters of the hook function
    5. };

The interrupt vector used by the kernel (which is actually the array subscript) is shown below
(File linux_2_6_24/include/linux/interrupt.h)

    1. enum{
    2. Hi_softirq=0,//high-priority Tasklet
    3. TIMER_SOFTIRQ,//CORE timer
    4. NET_TX_SOFTIRQ,//Network send
    5. NET_RX_SOFTIRQ,//network receive
    6. BLOCK_SOFTIRQ,//???
    7. TASKLET_SOFTIRQ,//Common Tasklet
    8. Sched_softirq
    9. }

The kernel registers a soft interrupt function, which is essentially initializing an array of elements

    1. void Open_softirq (int nr, void (*action) (struct softirq_action*), void *data)
    2. {//NR is the soft interrupt vector number
    3. Softirq_vec[nr].data = data;
    4. Softirq_vec[nr].action = action;
    5. }

Where the kernel registers soft interrupts are:

    1. Start_kernel ()
    2. -->init_timers ()
    3. -->OPEN_SOFTIRQ (Timer_softirq,run_timer_softirq,null)
    4. -->softirq_init ()
    5. -->OPEN_SOFTIRQ (TASKLET_SOFTIRQ, Tasklet_action,null)
    6. -->OPEN_SOFTIRQ (Hi_softirq,tasklet_hi_action,null)
    7. -->do_initcall ()
    8. -->net_dev_init ()
    9. -->OPEN_SOFTIRQ (NET_TX_SOFTIRQ, net_tx_action, NULL);
    10. -->OPEN_SOFTIRQ (NET_RX_SOFTIRQ, net_rx_action, NULL);
    11. -->blk_dev_init ()
    12. -->OPEN_SOFTIRQ (BLOCK_SOFTIRQ, Blk_done_softirq,null)

second, soft interrupt trigger
The previous registration is over, and now it starts firing.
The kernel uses a data structure to mark that a "soft interrupt" has occurred (or that a soft interrupt has been triggered).
__softirq_pending A total of 32bit, that is, each bit corresponds to a soft interrupt vector, the actual use of 6 bit
The nth bit 1, or softirq_vec[n] has a soft interrupt to occur.

    1. typedef struct {
    2. unsigned int __softirq_pending; /* Set_bit is used on this */
    3. unsigned int __last_jiffy_stamp;
    4. } ____cacheline_aligned irq_cpustat_t;
    5. extern irq_cpustat_t irq_stat[]; /* defined in Asm/hardirq.h */
    6. #define __IRQ_STAT (CPU, member) (Irq_stat[cpu].member)
    7. #define LOCAL_SOFTIRQ_PENDING () \
    8. __irq_stat (smp_processor_id (), __softirq_pending)
    9. #define SET_SOFTIRQ_PENDING (x) (local_softirq_pending () = (x))
    10. #define OR_SOFTIRQ_PENDING (x) (Local_softirq_pending () |= (x))
    11. #define __raise_softirq_irqoff (NR) do {or_softirq_pending (1UL << (NR)),} while (0)

Commonly used soft interrupt trigger function

    1. void Raise_softirq_irqoff (int nr) {//nr = soft interrupt vector number
    2. __raise_softirq_irqoff (NR);
    3. }

But this is just a "trigger" soft interrupt, and the soft interrupt is not immediately processed
third, soft interrupt processing
function _ _do_softirq is a one-time processing of all soft interrupts (subtext, soft interrupts not nested) from high to low in the vector table
_ _do_softirq () Call timing:
1. Irq_exit () hardware interrupt is finished, call on return

    1. DO_IRQ ()-->irq_exit ()
    2. -->local_softirq_pending ()
    3. -->_ _DO_SOFTIRQ ()

2. KSOFTIRQD () is used to assist the kernel thread that handles soft interrupts, and a KSOFTIRQD is running on each of the CPUs.

    1. Start_kernel ()-Kernel_init ()-->do_pre_smp_initcalls ()
    2. -->SPAWN_KSOFTIRQD ()-->cpu_callback ()
    3. -->kthread_create (KSOFTIRQD, Hcpu, "ksoftirqd/%d", HOTCPU)
    4. -->KSOFTIRQD ()
    5. -->local_softirq_pending ()
    6. -->_ _DO_SOFTIRQ ()

3. Local_bh_enable (), found a soft interrupt to be processed and was not in the context of a soft and hard interrupt

    1. Local_bh_enable ()
    2. -->local_softirq_pending ()
    3. -->_ _DO_SOFTIRQ ()

Handling process code in a detailed

  1. asmlinkage void __do_softirq (void)
  2. {
  3. struct Softirq_action *h;
  4. __U32 pending;
  5. int max_restart = Max_softirq_restart;
  6. int CPU;
  7. Pending = Local_softirq_pending ();
  8. Account_system_vtime (current);
  9. /* Soft interrupt processing, disable soft interrupt re-entry, soft interrupt processing is non-reentrant */
  10. __local_bh_disable ((unsigned long) __builtin_return_address (0));
  11. Trace_softirq_enter ();
  12. CPU = smp_processor_id ();
  13. Restart
  14. /* Reset the pending bitmask before enabling IRQs
  15. The following first clears the pending so that the system can activate other software interrupts,
  16. Then enable external interrupts
  17. System in the following processing, will enable external interrupts to improve the system's response,
  18. Note that you must first clear the pending, and then enable the external interrupt, or the deadlock */
  19. Set_softirq_pending (0);
  20. Local_irq_enable ();
  21. h = Softirq_vec;
  22. /* Follow the handle registered with OPEN_SOFTIRQ from high to low key */
  23. do {
  24. if (pending & 1) {
  25. h->action (h); //Key sentence, Tasklet, kernel timer, network interrupt is here.
  26. Rcu_bh_qsctr_inc (CPU);
  27. }
  28. h++;
  29. Pending >>= 1;
  30. } while (pending);
  31. Local_irq_disable ();
  32. Pending = Local_softirq_pending ();
  33. if (pending &&--max_restart)
  34. Goto Restart;
  35. if (pending)
  36. WAKEUP_SOFTIRQD ();
  37. Trace_softirq_exit ();
  38. Account_system_vtime (current);
  39. _local_bh_enable ();
  40. }

Linux Soft Interrupt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.