关抢占 自旋锁_为什么禁用中断会禁用内核抢占以及自旋锁如何禁用抢占

bd96500e110b49cbb3cd949968f18be7.png

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

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.

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

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?

解决方案

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

Here are several things.

preempt_disable() doesn't disable IRQ. It just increases a thread_info->preempt_count variable.

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值