PPC arch and spinlocks
Hollis Blanchard
hollis at penguinppc.org
Fri Jun 10 00:13:48 EST 2005
On Jun 8, 2005, at 9:29 AM, Garcia Jérémie wrote:
> I'm new to kernel device driver (linux newbie) and I'd like to control
> concurrent access
> to our hardware resources. (I'm working on a ppc405EP platform
> uniprocessor)
> ...
> So, I decided to declare a spinlock in my module and offer, via ioctl,
> the possibilty to user
> space programs to handle it with spin_trylock(), spin_lock() and
> spin_unlock().
I don't think this makes any sense. Kernel spinlocks are to protect
critical sections of kernel code from race conditions.
> I really need to have an equivalent of the spin_trylock() routine in
> order not to have the processus waiting till the semaphore is
> available when tryin to get it. (ask and take if available, but not
> sleep).
It sounds like what you want is to enforce only a single user of your
device driver. If that's the case, you will need an atomic operation,
but if that fails then simply return an appropriate error code from
your open() routine.
You could use a spinlock like drivers/char/nvram.c:
static int
nvram_open(struct inode *inode, struct file *file)
{
spin_lock(&nvram_state_lock);
if ((nvram_open_cnt && (file->f_flags & O_EXCL)) || ...)
spin_unlock(&nvram_state_lock);
return -EBUSY;
}
...
nvram_open_cnt++;
spin_unlock(&nvram_state_lock);
return 0;
}
In this example, "nvram_open_cnt++" is one of the operations being
protected from race conditions, such as two processes on an SMP system
both entering open() at literally the same time. Of course, since you
aren't building SMP, you know that there will not be two processes in
open() simultaneously, so the spinlocks will be compiled out, and you
will be left with a plain counter returning -EBUSY if the device is
already in use.
-Hollis
More information about the Linuxppc-dev
mailing list