Are file_operations.{open,release} serialized?

Jan Kasprzak (kas@informatics.muni.cz)
Fri, 24 Apr 1998 00:52:16 +0300


Hello,

I am writing a driver for some serial card. I have a per-card
structure in which there is among other things an usage counter for
that card. I want to ask you if it is safe to increment this
counter in the device's file_operations.open() routine and to decrement
in the file_operations.release() routine without any locking
(the same way as MOD_{INC,DEC}_USE_COUNT does). I need this for
allocating the RX/TX buffers at the time of first open() and deallocating
them on the latest release(). The code has the following structure:

mydevice_open()
{
... (determine `mydevice' from the minor number) ...
if (mydevice->usage_count == 0) {
mydevice->rxbuf = kmalloc(..., GFP_KERNEL|GFP_DMA);
...
}
/* when everything is OK, do the following: */
mydevice->usage_count++;
MOD_INC_USE_COUNT;
...
}

mydevice_release()
{
... determine `mydevice' from the minor number) ...
if (!--(mydevice->usage_count)) {
... (disable DMA and IRQ for that particular card) ...
kfree(mydevice->rxbuf);
}
MOD_DEC_USE_COUNT;
...
}

Is there any race condition in the above code? Can for example
one process be in the middle of the open routine kmalloc'ing the rxbuf
while other proces is freeing the rxbuf, or two processes opening
the device and kmalloc'ing the rxbuf?

advaTHANKSnce.

-Yenya

--
\ Jan "Yenya" Kasprzak <kas at fi.muni.cz>       http://www.fi.muni.cz/~kas/
\\ PGP: finger kas at aisa.fi.muni.cz   0D99A7FB206605D7 8B35FCDE05B18A5E //
\\\             Czech Linux Homepage:  http://www.linux.cz/              ///
 Considering exactly how many truly stupid things the BSD guys have done it
 continually makes me surprised  how many people still consider their ideas
 good without thinking too much about them.     --Linus on BSD securelevels

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu