"Linux Kernel design and implementation" Learning notes-Interrupt, interrupt handler __linux

Source: Internet
Author: User
interrupt and interrupt handlers interruptedCan be generated at any time, interrupt the execution of the CPU, the CPU instead of processing interrupts. Different devices correspond to interrupts, and each interrupt is passed through a unique digital flag.
These interrupt values are called Interrupt Request (IRQ) line, each IRQ line is associated with a numeric value. Interrupt Handler
When the response is interrupted, the kernel performs a function, interrupt handler/Interrupt Service routine ISR, and a device interrupt handler is part of his device driver. IO resources include:Interrupts, I/O ports, shared RAM,DMA. The driver needs to manage the registration to release these resources.

Upper Half : Execute immediately when an interrupt is received, and do only work with strict deadlines, such as on interrupt answering or resetting the hardware.
Lower half : The work that can be allowed to be done later is deferred to the lower half of the execution. Registering an interrupt handler
REQUEST_IRQ (UINT IRQ, irq_handlet_t handler, ulong flasgs,void* Dev) registers the interrupt handler to activate the given interrupt line; This function may sleep, not in the interrupt context/ Execution in other code that is not allowed to block. When you uninstall the driver
Log off the corresponding interrupt handler and release the disconnection. void Free_irq (UINT IRQ, void* Dev); Linux interrupt handlers are not required to be reentrant, and given interrupt handlers are executed, the corresponding disconnection will be blocked on all processors

Interrupt Context: No fallback process, not sleep. The interrupt handler interrupts the other code. implementation of interrupt processing mechanism

The device interrupts and sends an electrical signal to the interrupt controller via the bus if the interrupt line is active, the interrupt controller sends an interrupt to the processor (the processor-specific pin) if the processor does not prohibit the interrupt, the processor stops what is being done to shut down the interrupt system and tune to the predefined interrupt handler entry. Each break line is tuned to a unique location. The initial entry point holds the disconnection number, and the value of the register is called DO_IRQ ()
Calculate the interrupt number, answer the interrupt, and disable the interrupt delivery on this line to ensure that there is a valid handler on the interrupt line, but not executed. Call the interrupt handler in the Handle_irq_event () call to install the disconnection. The interrupt is blocked and returned to DO_IRQ. Do cleanup work, return to the initial entry point, skip to Ret_from_intr () to check whether the schedule hangs, restore registers, and restore the kernel to the point of interruption. disables current processor interrupts and activation interrupts.

Local_irq_disable (); local_irq_enable ();
Unsigned long flags; Local_irq_save (flags); Local_irq_restore (flags); prohibit the designation of interrupts

DISABLE_IRQ (int); Prevents interrupts to all processors. Interrupt Handler process logical allocation principle on and below: top half:
Tasks are sensitive to time-sensitive tasks and hardware-related tasks that are not interrupted by other interrupts, are not concurrent, and do not block the lower part:
For time insensitive and hardware-independent can be interrupted by other interrupts, can sleep, can be concurrent

The top half of Linux is an interrupt handler, and the lower half has several mechanisms: soft interrupts

A soft interrupt is a set of statically defined lower-half interfaces, with 32 of them that can be executed concurrently on all processors, the same type, and statically registered at compile time. implementation:

struct softirq_action{//<linux/interrupt.h> represents soft interrupt
    void (*action) (struct softirq_action*);

32 of them currently use 6.

static struct softirq_action soft_irq_vec[nr_softirqs];//kernel/softirq.c soft interrupt Array
Interrupt handler: The kernel executes the action function when it runs a soft interrupt handler.
A soft interrupt does not preempt another soft interrupt. The only thing that can preempt a soft interrupt is an interrupt handler. Other soft interrupts even the same type can execute software interrupts on other processors simultaneously: a registered software interrupt is executed after marking, which is called a triggering interrupt.
The interrupt handler marks a soft interrupt before returning.
In: When the hardware interrupt code returns; in the KSOFTIRQ kernel thread; Display check execution soft interrupts, pending soft interrupts will be checked and executed soft interrupt execution in DO_SOFTIRQ
U32 pending;
Pending = Local_sofqirq_pending ();
if (pending) {
    struct softirq_action* h;
    Set_softirq_pending (0);
    h = Softirq_vec;
    do{
        if (pending & 1) {
            h->action ();
        }
        h++;
        Pending >>=1;
    } while (pending);
}
use soft interrupts

Soft interrupts are left to the most demanding and important lower half of the time. Currently, only the network, SCSI use kernel timer and Tasklet are all based on soft interrupts. Static declaration of soft interrupts by enumeration type and allocation of index registration handlers
OPEN_SOFTIRQ (net_tx_softirq,net_tx_action); When the program executes in a soft interrupt, it allows the response to be interrupted but cannot sleep. Because only the current processor is blocked from running, other processors can run handlers at the same time, requiring lock protection. RASE_SOFTIRQ (NET_TX_SOFTIRQ) sets the soft interrupt to a pending state, which is executed the next time the DO_SOFTIRQ is called. Tasklet

The lower half implementation mechanism based on the software interrupt implementation, the flexibility is strong, dynamic creation. Two different types of tasklet can be run on different processors, but the same is not possible. You can register dynamically through code. implementation: Based on soft interrupts

struct Tasklet_struct {
  struct tasklet_struct *next;
  unsigned long sate;//(0/tasklet_state_sched/tasklet_state_run)
  atomic_t count;/* Reference counter, 0 allow execution, otherwise prohibit
  /void (* Func) (unsigned long);////
  unsigned long data;//func parameter
};

scheduling : Each processor has a tasklet_vec and TASKLET_HI_VEC structure, low, high priority Tasklet_strucu linked list by Tasklet_schedule () and Tasklet_hi_ Schdule () to check whether the Tasklet is tasklet_state_sched. If it is a return call _tasklet_schedule Save the interrupt state, disable the local interrupt to add the processor that needs to be dispatched to the Tasklet_vec Or Tasklet_hi_vec the head of the linked list to evoke TASKLET_SOFTIRQ or TASKLET_HI_SOFTIRQ soft interrupt recovery interrupt State and return the soft interrupt handler:

Tasklet_action (), tasklet_hi_action () operation to prevent the interruption of the current processor to null, empty the list allows the interrupt loop traversal of the list of each pending tasklet
If it is a multiprocessor system, judge if it is tasklet_state_sched, if it is running, skip. If not in execution, set Tasklet_state_run. Check count==0, otherwise tasklet is forbidden, skip. Execute Tasklet, empty the Tasklet_state_run flag to perform the next tasklet use tasklet declaration tasklet:
Declare_tasklet (Name,func,data) declare_tasklet_disabled (.)
Tasklet_init (t, Tasklet_handler, Dev); Write the handler: because it is implemented on soft interrupts, the handler cannot sleep. Tasklet allows the response to be interrupted. Call Tasklet_schedule (&my_tasklet); dispatch Tasklet, which is actually marked/suspended, will be executed as soon as the opportunity is available to my_tasklet.

Tasklet_disable (&my_tasklet); Tasklet_enable (&my_tasklet); prohibit and activate compromise

Frequent interruption or frequent occurrence of tasklet: processing as soon as possible, the user process is not responding, lag execution, interrupt processing is also unhappy.
= = "Use the core process of low priority to deal with soft interrupts specifically ksoftirqd/n

for (;;) {
    if (!softirq_pending (CPU)) {
        schedule ();
    }
    Set_current_state (task_running);
    while (softirq_pending (CPU)) {
        do_softirq ();
        if (Need_schdule ()) schedule ();
    }
}
Task Force columns:

The lower half of the function to the kernel thread execution, has a thread context environment, you can sleep.
Provides an interface to create a worker threads, providing an interface to queue tasks that need to be deferred, providing default worker threading to the lower half of the queue. implementation: data structure each worker thread has a workqueue_struct structure inside there is a nr_cpus cpu_work_queue_strcut corresponding to each processor worker thread work with work_struct representation, Contains an executive function fuc, each CPU's worker thread corresponds to a work_struct list.

the core of Worker_thread () is a dead loop thread puts itself into hibernation if the linked list is empty, hibernation is not empty, call the Run_workqueue function to perform the work.
Loop execution when the list is not empty
Select the next node object, get execution functions and parameters to be processed flag 0 call function repeat use: Create deferred work
Declare_work (name, Void (func) (void), void*data);
Init_work (Strut work_struct* task,...); Dynamic Work Queue processing functions
Run in the process context, allow the response to break, do not hold the lock, can sleep. Unable to access user space to schedule work
Schedule_work (&work); submitted to worker process
Schedule_delay_work (&work,delay) Refresh operation
Flush_scheduled_work (), the function waits for all objects in the queue to be executed and then returns to create a new work queue
If the default queue does not meet your needs, you should create a new work queue and a corresponding worker thread. comparison of the various mechanisms

Lower half part Context Sequential Implementation guarantees
Soft interrupt Interrupt No
Tasklet Interrupt Same type cannot be executed concurrently
Work queues Process Not (same as process context, scheduled)

If the task needs to be deferred to the process context, the required work queue for Hibernation
Task Queue interface is simple, the same type cannot perform tasklet at the same time
Soft interrupt provides minimal security for execution serialization, and must take extra care to ensure shared data

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.