Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why disabling interrupts disables kernel preemption and how spin lock disables preemption

I am reading Linux Kernel Development recently, and I have a few questions related to disabling preemption.

  1. In the "Interrupt Control" section of chapter 7, it says:

    Moreover, disabling interrupts also disables kernel preemption.

    I also read from the book that kernel preemption can occur in the follow cases:

    When an interrupt handler exits, before returning to kernel-space.
    When kernel code becomes preemptible again.
    If a task in the kernel explicitly calls schedule()
    If a task in ther kernel blocks (which results in a call to schedule())

    But I can't relate disabling interrupts with these cases.

  2. As far as I know, a spinlock would disable preemption with the preempt_disable() function.

    The post What exactly are "spin-locks"? says:

    On a single core machine a spinlock is simply a "disable interrupts" or "raise IRQL" which prevents thread scheduling completely.

    Does preempt_disable() disable preemption by disabling interrupts?

like image 949
feirainy Avatar asked Dec 25 '13 06:12

feirainy


People also ask

Does spinlock disable preemption?

spinlock automatically disables preemption, which avoids deadlock caused by interrupts. when data is shared with interrupt handler, before holding spinlock we must disable interrupts. when data is shared with bottom halves, before holding spinlock we must disable bottom halves.

Does spinlock disable interrupt?

On return from spin_lock, the calling function owns the lock. spin_lock_irqsave(spinlock_t *lock, unsigned long flags); This version also acquires the lock; in addition, it disables interrupts on the local processor and stores the current interrupt state in flags .

Can the kernel disable interrupts?

You can disable interrupts from within a user-mode program, though it can be dangerous (even kernel drivers do it for as short a time as possible).


1 Answers

I am not a scheduler guru, but I would like to explain how I see it. Here are several things.

  1. preempt_disable() doesn't disable IRQ. It just increases a thread_info->preempt_count variable.
  2. Disabling interrupts also disables preemption because scheduler isn't working after that - but only on a single-CPU machine. On the SMP it isn't enough because when you close the interrupts on one CPU the other / others still does / do something asynchronously.
  3. The Big Lock (means - closing all interrupts on all CPUs) is slowing the system down dramatically - so it is why it not anymore in use. This is also the reason why preempt_disable() doesn't close the IRQ.

You can see what is preempt_disable(). Try this: 1. Get a spinlock. 2. Call schedule()

In the dmesg you will see something like "BUG: scheduling while atomic". This happens when scheduler detects that your process in atomic (not preemptive) context but it schedules itself.

Good luck.

like image 199
Sebastian Mountaniol Avatar answered Oct 13 '22 13:10

Sebastian Mountaniol