Re: Doubt Regarding Multithreading and Device Driver

From: Richard B. Johnson
Date: Wed Apr 27 2005 - 10:41:37 EST

On Wed, 27 Apr 2005, k8 s wrote:


I have a doubt regarding user space threads and device drivers
implementation issue.

I have a device driver for /dev/skn
It implements basic driver operations skn_open,skn_release, skn_ioctl.

I am storing something into struct file*filp->private_data.
As this is not shared across processes I am not doing any locking
stuff while accessing or putting anything into it.

Will There be a race condition in a multithreaded program in the ioctl
call on smp kernel accessing filp->private_data.

Of course. But that's not the only race. You need to make certain that
any shared resource (your driver) only allows a single execution-
thread anywhere there is shared data or the hardware itself. This
is generally accomplished with semaphore(s), a.k.a, down() and up().

Note that each open() call provides its own 'struct file' pointer.
The kernel won't get them mixed up. You can use your private-data
pointer available in this structure in each open() and use that
for private data in each access. You can free such private data
in a close(). But, that's just the obvious stuff. User-mode threads
share open files!

That means that your driver has no way of isolating such access
except by using semaphores.


Dick Johnson
Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at