发生中断时,内核如何处理进程上下文中的锁定? [英] How does Kernel handle the lock in process context when an interrupt comes?

查看:274
本文介绍了发生中断时,内核如何处理进程上下文中的锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对问题有点含糊不清...我要了解的是以下情况

First of all sorry for a little bit ambiguity in Question... What I want to understand is the below scenario

假设porcess正在运行,它持有一个锁,现在在获取锁后生成了HW中断,那么内核将如何处理这种情况,它将等待锁吗?如果是,如果中断处理程序需要访问该锁或进程中受该锁保护的共享数据,该怎么办?

Suppose porcess is running, it holds one lock, Now after acquiring the lock HW interrupt is generated, So How kernel will handle this situation, will it wait for lock ? if yes, what if the interrupt handler need to access that lock or the shared data protected by that lock in process ?

推荐答案

Linux内核具有一些用于获取自旋锁的功能,以处理您在此处提出的问题.特别是spin_lock_irq(),它禁用中断(在进程正在运行的CPU上)并获取自旋锁.当代码在获取自旋锁之前知道中断已启用时,可以使用此方法.如果可能在不同的上下文中调用该函数,则还有spin_lock_irqsave(),它在禁用它们之前隐藏了中断的当前状态,以便可以由spin_unlock_irqrestore()重新启用它们.

The Linux kernel has a few functions for acquiring spinlocks, to deal with issues like the one you're raising here. In particular, there is spin_lock_irq(), which disables interrupts (on the CPU the process is running on) and acquires the spinlock. This can be used when the code knows interrupts are enabled before the spinlock is acquired; in case the function might be called in different contexts, there is also spin_lock_irqsave(), which stashes away the current state of interrupts before disabling them, so that they can be reenabled by spin_unlock_irqrestore().

无论如何,如果在进程和中断上下文中都使用了锁(如果在上下文之间需要共享数据,这是一个很好且很常见的设计),则进程上下文必须禁用中断(在本地中断)获取自旋锁以避免死锁时,它所运行的CPU).实际上,lockdep("CONFIG_PROVE_LOCKING")会对此进行验证,并警告是否以一种容易受进程上下文持有锁中断的情况"死锁的方式使用自旋锁.

In any case, if a lock is used in both process and interrupt context (which is a good and very common design if there is data that needs to be shared between the contexts), then process context must disable interrupts (locally on the CPU it's running on) when acquiring the spinlock to avoid deadlocks. In fact, lockdep ("CONFIG_PROVE_LOCKING") will verify this and warn if a spinlock is used in a way that is susceptible to the "interrupt while process context holds a lock" deadlock.

这篇关于发生中断时,内核如何处理进程上下文中的锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆