Question

I've been trying to implement a linux system_call that has been giving me problems and I suspect it's because there is no locking(or maybe preemption) going on with my code.

There is a critical section I have in a very frequently called function(this custom function gets called every time a system_call is made), and it gets started/stopped by system_calls as well. Is there any way to ensure that a this critical segment that happens every time any system call is made in the linux kernel is non-preemptable and must finish execution before anything else can happen?

Was it helpful?

Solution

If I understand the question correctly, the simplest way is to use a spin-lock:

#include <linux/spinlock.h>

static DEFINE_SPINLOCK(foo_lock);

int my_system_call(...)
{
        ...
        /* critical section starts */
        spin_lock(&foo_lock);
        /* critical section goes here */
        ...
        /* critical section ends */
        spin_unlock(&foo_lock);
        ...
}

Such critical section will be non-preemptable and concurrent executions of critical section won't overlap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top