akp
akp

Reputation: 1823

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. 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.

Upvotes: 3

Views: 10112

Answers (4)

user1477716
user1477716

Reputation: 101

Kernel spinlock is not suitable in user space, since it will disable preempt and disable IRQ if using _irqsave/_irqrestore. Preempt or IRQ disable is not even possible in user space.

The best try is pthread_spin_lock() I think.

Upvotes: 8

user2050283
user2050283

Reputation: 570

The best way to do this is to create an IOCTL interface for locking your kernel resource. I'm assuming your kernel module exposes a char driver interface to userspace. This IOCTL call from userspace will set and reset the kernel spinlock when called. You can also use the same interface for checking if the resource is locked by the kernel.

Upvotes: 0

stdcall
stdcall

Reputation: 28880

spinlooks are kernel structures, and are not to be used in user-space. for user space, a mutex such as pthread mutex is the best way to go.

if you want the same code to work also in user-space and also in kernel mode, you need to use ifdefs. the #ifdef KERNEL will allow you to choose while type you're using.

You should create wrapper functions (can be inline functions) which calls the apropriate functionality (spinlook, or mutex). and the actual implementation is compiled according to the ifdefs.

Upvotes: 1

ugoren
ugoren

Reputation: 16441

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.

But if it's shared, you can't use spinglocks on it, because user space can't be allowed to block the kernel.

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.

Upvotes: 4

Related Questions