Few points needs to be addressed here about the kernel module
programming and the device driver which has a great impact on writing
the code for the kernel on reentrant.!!!!!
Yes, the question is does a device driver a reentrant ?
OR does any part (function) of the module (device driver) can be
reentrant ?
If yes, how do we write the code / measures to make them reentrant ?
What is reentrant mean to me here ?
It mean calling a device driver program again when a program (driver)
is already running.
Calling a function is the module again while the same is still under
run ?
Lets put across the views and opinios on this.
Regards
Lokesh
Well, there are several considerations and ways to achieve re-entrant
code in a SMP preemptive kernel.
For example atomic operators is one of those. See for example the one
line code "x++". If x would be accessible, we read x, increment it and
write it back to memory in one uninterrupted operation. There's no
race condition in this matter. Atomic operators provide these
uninterrupted operations in this way:
atomic_t x;
atomic_set(&x, 1);
atomic_dec(&x);
printk(KERN_DEBUG "echo 2: %u\n", atomic_read(&x));
Another method are the spinlocks. Basically, a spinlock is like a
thread mutex, and its usage is pretty similar:
spinlock_t mylock = SPIN_LOCK_UNLOCKED;
unsigned long flags;
spin_lock_irqsave(&mylock, flags);
/* Re-entrant code here */
spin_unlock_irqrestore(&mylock, flags);
Using spin_lock_irqsave() will disable interrupts locally and provide
the spinlock on SMP, which covers both, interrupt and SMP concurrency
issues, while spin_unlock_irqrestore do the "unlock".
Spinlocks is these can be used anywhere, however, do not do anything
that will sleep while holding a spinlock, since the usage of spinlocks
bounded to situations of a small amount of time.
Anyways, there are several other ways to achieve re-entrant code in
device drivers.
I hope this light up the situation a little.