简体   繁体   English

在用户空间应用程序中使用自旋锁

[英]using spinlocks in user-space application

I am trying to create a kernel-module which has an structure & i want to use the same structure in user-space application and this application works with the given module. 我正在尝试创建一个具有结构的内核模块,并且我想在用户空间应用程序中使用相同的结构,并且此应用程序与给定模块一起使用。

the main problem is that this structure is containing a variable named spinlock_t type . 主要问题是这个结构包含一个名为spinlock_t type的变量。 which is used in kernel for locking but dont know how to use it in user-space application. 在内核中用于锁定但不知道如何在用户空间应用程序中使用它。

struct new_struct
{
  ...some variable...
  spinlock_t u_lock;
};

Is there any way to use spinlocks in user-space application. 有没有办法在用户空间应用程序中使用自旋锁。

or is there any another locking technique which could be used in both kernel & user-space so that if the lock is held by user-application the kernel-module should not be able to get it and vice-versa. 或者是否存在可以在内核和用户空间中使用的任何其他锁定技术,以便如果用户应用程序持有锁,则内核模块应该无法获取它,反之亦然。

I understand that this structure is in memory, which is shared between the kernel and the user process. 我知道这种结构在内存中,在内核和用户进程之间共享。 If not, you can use ifdef KERNEL to use different locking in kernel and user space. 如果没有,您可以使用ifdef KERNEL在内核和用户空间中使用不同的锁定。

But if it's shared, you can't use spinglocks on it, because user space can't be allowed to block the kernel. 但是,如果它是共享的,则不能在其上使用spinglocks,因为不允许用户空间阻止内核。

The simple way to handle it is to have all the information in the kernel, and have user space code issue system calls to access it. 处理它的简单方法是将所有信息都包含在内核中,并通过用户空间代码发出系统调用来访问它。

Another way is to use lockless data structures (a ring buffer is popular) for this communication. 另一种方法是使用无锁数据结构(流行的是环形缓冲区)进行此通信。

Kernel spinlock is not suitable in user space, since it will disable preempt and disable IRQ if using _irqsave/_irqrestore. 内核自旋锁不适用于用户空间,因为如果使用_irqsave / _irqrestore,它将禁用抢占并禁用IRQ。 Preempt or IRQ disable is not even possible in user space. 在用户空间中甚至无法进行抢占或IRQ禁用。

The best try is pthread_spinlock I think. 我认为最好的尝试是pthread_spinlock。

spinlooks are kernel structures, and are not to be used in user-space. spinlooks是内核结构,不能在用户空间中使用。 for user space, a mutex such as pthread mutex is the best way to go. 对于用户空间,最好使用pthread互斥锁等互斥锁。

if you want the same code to work also in user-space and also in kernel mode, you need to use ifdefs. 如果您希望相同的代码也可以在用户空间以及内核模式下工作,则需要使用ifdefs。 the #ifdef KERNEL will allow you to choose while type you're using. #ifdef KERNEL允许您选择正在使用的类型。

You should create wrapper functions (can be inline functions) which calls the apropriate functionality (spinlook, or mutex). 您应该创建调用适当功能(spinlook或mutex)的包装函数(可以是内联函数)。 and the actual implementation is compiled according to the ifdefs. 并根据ifdefs编译实际的实现。

The best way to do this is to create an IOCTL interface for locking your kernel resource. 最好的方法是创建一个用于锁定内核资源的IOCTL接口。 I'm assuming your kernel module exposes a char driver interface to userspace. 我假设您的内核模块向用户空间公开了一个char驱动程序接口。 This IOCTL call from userspace will set and reset the kernel spinlock when called. 从用户空间进行的此IOCTL调用将在调用时设置并重置内核自旋锁。 You can also use the same interface for checking if the resource is locked by the kernel. 您还可以使用相同的接口来检查资源是否被内核锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM