Basic Principles of Linux kernel preemptible Patches

Source: Internet
Author: User
Article Title: Basic Principles of Linux kernel preemptible Patches. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems and open source, and some other basic classification by jkl

When the CPU runs in the kernel, it cannot be preemptible everywhere. There are some gaps in the kernel.
All, the basic principle of kernel preemption patch is to regard SMP code segments that can be parallel as areas where kernel preemption can be performed.
Domain.
2.4 The kernel precisely refines the kernel thread synchronization mechanism under multiple CPUs and uses the spinlock and rw for non-parallel instruction blocks.
The lock is a detailed representation, and the implementation of the patch can be easily described.
The specific method is to add a preempt_count variable in the task structure of the process as the kernel preemption lock.
Along with spinlock and rwlock to lock and unlock. When preempt_count is 0, kernel scheduling can be performed.
The entry of the kernel scheduler is preempt_schedule (), which marks the current process as TASK_PREEMPTED and
Call schedule (). In the TASK_PREEMPTED status, schedule () will not delete the process from the running queue.

The following shows the main code for Kernel preemptible patches:
Arch/i386/kernel/entry. S:
Preempt_count = 4 # use flags in task_struct as preempt_count, and flags are moved to another
Location
Ret_from_exception: # returned from an exception
# Ifdef CONFIG_SMP
GET_CURRENT (% ebx)
Movl processor (% ebx), % eax
Shll $ CONFIG_X86_L1_CACHE_SHIFT, % eax
Movl SYMBOL_NAME (irq_stat) (, % eax), % ecx # softirq_active
Testl SYMBOL_NAME (irq_stat) + 4 (, % eax), % ecx # softirq_mask
# Else
Movl SYMBOL_NAME (irq_stat), % ecx # softirq_active
Testl SYMBOL_NAME (irq_stat) + 4, % ecx # softirq_mask
# Endif
Jne handle_softirq
# Ifdef CONFIG_PREEMPT
Cli
Incl preempt_count (% ebx) # The exception entry does not contain commands that disable kernel scheduling, and ret_from_intr
Match
# Endif
ENTRY (ret_from_intr) # Return of hardware interruption
GET_CURRENT (% ebx)
# Ifdef CONFIG_PREEMPT
Cli
Decl preempt_count (% ebx) # restore the kernel preemption flag
# Endif
Movl EFLAGS (% esp), % eax # mix EFLAGS and CS
Movb CS (% esp), % al
Testl $ (VM_MASK | 3), % eax # return to VM86 mode or non-supervisor?
Jne ret_with_reschedule
# Ifdef CONFIG_PREEMPT
Cmpl $0, preempt_count (% ebx)
Jnz restore_all # kernel preemption is prohibited if preempt_count is non-zero
Cmpl $0, need_resched (% ebx)
Jz restore_all #
Movl SYMBOL_NAME (irq_stat) + irq_stat_local_bh_count CPU_INDX, % ecx
Addl SYMBOL_NAME (irq_stat) + irq_stat_local_irq_count CPU_INDX, % ecx
Jnz restore_all
Incl preempt_count (% ebx)
Sti
Call SYMBOL_NAME (preempt_schedule)
Jmp ret_from_intr # The new process returns the ret_from_intr preemptible flag and returns it again.
# Else
Jmp restore_all
# Endif
ALIGN
Handle_softirq:
# Ifdef CONFIG_PREEMPT
Cli
GET_CURRENT (% ebx)
Incl preempt_count (% ebx)
Sti
# Endif
Call SYMBOL_NAME (do_softirq)
Jmp ret_from_intr
ALIGN
Reschedule:
Call SYMBOL_NAME (schedule) # test
Jmp ret_from_sys_call
Include/asm/hw_irq.h:
...
# Ifdef CONFIG_PREEMPT
# Define BUMP_CONTEX_SWITCH_LOCK \
GET_CURRENT \
"Incl 4 (% ebx) \ n \ t"
# Else
# Define BUMP_CONTEX_SWITCH_LOCK
# Endif
# Define SAVE_ALL \ hardware interrupt protection entry site
"Cld \ n \ t "\
"Pushl % es \ n \ t "\
"Pushl % ds \ n \ t "\
"Pushl % eax \ n \ t "\
"Pushl % ebp \ n \ t "\
"Pushl % edi \ n \ t "\
"Pushl % esi \ n \ t "\
"Pushl % edx \ n \ t "\
"Pushl % ecx \ n \ t "\
"Pushl % ebx \ n \ t "\
"Movl $" STR (_ KERNEL_DS) ", % edx \ n \ t "\
"Movl % edx, % ds \ n \ t "\
"Movl % edx, % es \ n \ t "\
BUMP_CONTEX_SWITCH_LOCK # kernel preemption is prohibited at the entry to hardware interruption
Include/linux/spinlock. h:
# Ifdef CONFIG_PREEMPT
# Define switch_lock_count () current-> preempt_count
# Define in_ctx_sw_off () (switch_lock_count (). counter) determine the preemption count of the current process
Non-zero
# Define atomic_ptr_in_ctx_sw_off () (& switch_lock_count ())
# Define ctx_sw_off () \ disable kernel preemption
Do {\
Atomic_inc (atomic_ptr_in_ctx_sw_off (); \ the current process's kernel preemption count increases by 1
} While (0)
# Define ctx_sw_on_no_preempt () \ Allow kernel preemption
Do {\
Atomic_dec (atomic_ptr_in_ctx_sw_off (); \ the current process's kernel preemption count minus 1
} While (0)
# Define ctx_sw_on () \ Allow and complete kernel preemption
Do {\
If (atomic_dec_and_test (atomic_ptr_in_ctx_sw_off ())&&\
Current-> need_resched )\
Preempt_schedule ();\
} While (0)
# Define spin_lock (lock )\
Do {\
Ctx_sw_off (); \ preemption is prohibited when the spin lock is entered
_ Raw_spin_lock (lock );\
} While (0)
# Define spin_trylock (lock) ({ctx_sw_off (); _ raw_spin_trylock (lock )? \ Lock and
Test whether the lock is locked.
1: ({ctx_sw_on (); 0 ;});})
# Define spin_unlock (lock )\
Do {\
_ Raw_spin_unlock (lock );\
Ctx_sw_on (); \ Allow and complete kernel preemption when leaving the spin lock
} While (0)
# Define read_lock (lock) ({ctx_sw_off (); _ raw_read_lock (lock );})
# Define read_unlock (lock) ({_ raw_read_unlock (lock); ctx_sw_on ();})
# Define write_lock (lock) ({ctx_sw_off (); _ raw_write_lock (lock );})
# Define write_unlock (lock) ({_ raw_write_unlock (lock); ctx_sw_on ();})
# Define write_trylock (lock) ({ctx_sw_off (); _ raw_write_trylock (lock )? \
1: ({ctx_sw_on (); 0 ;});})
...
Include/asm/softirq. h:
# Define cpu_bh_disable (cpu) do {ctx_sw_off (); local_bh_count (cpu) ++; barrie
R ();} while (0)
# Define cpu_bh_enable (cpu) do {barrier (); local_bh_count (cpu) --; ctx_sw_on ()
;} While (0)
Kernel/schedule. c:
# Ifdef CONFIG_PREEMPT
Asmlinkage void preempt_schedule (void)
{
While (current-> need_resched ){
Ctx_sw_off ();
Current-> state | = TASK_PREEMPTED;
Schedule ();
Current-> state & = ~ TASK_PREEMPTED;
Ctx_sw_on_no_preempt ();
}
}
# Endif
Asmlinkage void schedule (void)
{
Struct schedule_data * sched_data;
Struct task_struct * prev, * next, * p;
Struct list_head * tmp;
Int this_cpu, c;
# Ifdef CONFIG_PREEMPT
Ctx_sw_off ();
# Endif
If (! Current-> active_mm) BUG ();
Need_resched_back:
Prev = current;
This_cpu = prev-> processor;
If (in_interrupt ())
Goto scheduling_in_interrupt;
Release_kernel_lock (prev, this_cpu );
/* Do "administrative" work here while we don't hold any locks */
If (softirq_active (this_cpu) & softirq_mask (this_cpu ))
Goto handle_softirq;
Handle_softirq_back:
/*
* 'Sched _ data' is protected by the fact that we can run
* Only one process per CPU.
*/
Sched_data = & aligned_data [this_cpu]. schedule_data;
Spin_lock_irq (& runqueue_lock );
/* Move an exhausted RR process to be last ..*/
If (prev-> policy = SCHED_RR)
Goto move_rr_last;
Move_rr_back:
Switch (prev-> state ){
Case TASK_INTERRUPTIBLE:
If (signal_pending (prev )){
Prev-> state = TASK_RUNNING;
Break;
}
Default:
# Ifdef CONFIG_PREEMPT
If (prev-> state & TASK_PREEMPTED)
Break; if it is a kernel preemptive scheduling, the running queue is retained.
# Endif
Del_from_runqueue (prev );
# Ifdef CONFIG_PREEMPT
Case TASK_PREEMPTED:
# Endif
Case TASK_RUNNING:
}
Prev-> need_resched = 0;
/*
* This is the scheduler proper:
*/
Repeat_schedule:
/*
* Default process to select ..
*/
Next = idle_task (this_cpu );
C =-1000;
If (task_on_runqueue (prev ))
Goto still_running;
Still_running_back:
List_for_each (tmp, & runqueue_head ){
P = list_entry (tmp, struct task_struct, run_list );
If (can_schedule (p, this_cpu )){
Int weight = goodness (p, this_cpu, prev-> active_mm );
If (weight> c)
C = weight, next = p;
}
}
/* Do we need to re-calculate counters? */
If (! C)
Goto recalculate;
/*
* From this point on nothing can prevent us from
* Switching to the next task, save this fact in
* Sched_data.
*/
Sched_data-> curr = next;
# Ifdef CONFIG_SMP
Next-> has_cpu = 1;
Next-> processor = this_cpu;
# Endif
Spin_unlock_irq (& runqueue_lock );
If (prev = next)
Goto same_process;
# Ifdef CONFIG_SMP
/*
* Maintain the per-process 'last schedule' value.
* (This has to be recalculated even if we reschedule
* The same process) Currently this is only used on SMP,
* And it's approximate, so we do not have to maintain
* It while holding the runqueue spinlock.
*/
Sched_data-> last_schedule = get_cycles ();
/*
* We drop the scheduler lock early (it's a global spinlock ),
* Thus we have to lock the previous process from getting
* Rescheduled during switch_to ().
*/
# Endif/* CONFIG_SMP */
Kstat. context_swtch ++;
/*
* There are 3 processes which are affected by a context switch:
*
* Prev =... => (last => next)
*
* It's the 'much more previous ''prev' that is on next's stack,
* But prev is set to (the just run) 'last' process by switch_to ().
* This might sound slightly confusing but makes tons of sense.
*/
Prepare_to_switch ();
{
Struct mm_struct * mm = next-> mm;
Struct mm_struct * oldmm = prev-> active_mm;
If (! Mm ){
If (next-> active_mm) BUG ();
Next-> active_mm = oldmm;
Atomic_inc (& oldmm-> mm_count );
Enter_lazy_tlb (oldmm, next, this_cpu );
} Else {
If (next-> active_mm! = Mm) BUG ();
Switch_mm (oldmm, mm, next, this_cpu );
}
If (! Prev-> mm ){
Prev-> active_mm = NULL;
Mmdrop (oldmm );
}
}
/*
* This just switches the register state and
* Stack.
*/
Switch_to (prev, next, prev );
_ Schedule_tail (prev );
Same_process:
Reacquire_kernel_lock (current );
If (current-> need_resched)
Goto need_resched_back;
# Ifdef CONFIG_PREEMPT
Ctx_sw_on_no_preempt ();
# Endif
Return;
Recalculate:
{
Struct task_struct * p;
Spin_unlock_irq (& runqueue_lock );
Read_lock (& tasklist_lock );
For_each_task (p)
P-> counter = (p-> counter> 1) + NICE_TO_TICKS (p-> nice );
Read_unlock (& tasklist_lock );
Spin_lock_irq (& runqueue_lock );
}
Goto repeat_schedule;
Still_running:
C = goodness (prev, this_cpu, prev-> active_mm );
Next = prev;
Goto still_running_back;
Handle_softirq:
Do_softirq ();
Goto handle_softirq_back;
Move_rr_last:
If (! Prev-> counter ){
Prev-> counter = NICE_TO_TICKS (prev-> nice );
Move_last_runqueue (prev );
}
Goto move_rr_back;
Scheduling_in_interrupt:
Printk ("Scheduling in interrupt \ n ");
BUG ();
Return;
}
Void schedule_tail (struct task_struct * prev)
{
_ Schedule_tail (prev );
# Ifdef CONFIG_PREEMPT
Ctx_sw_on ();
# Endif
}

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.