how to safely use unregister_chrdev?
Hi everyone,
I want to write a simple character device driver (based on the one in LDD3). How can I use unregister_chrdev safely? For example, consider the following code: Code:
static int init(void) { In other words, is it possible to stop accepting "open" calls and to wait for all existing file handles to be released? Thanks in advance, Jan |
Usually, anything allocated in the module_init function is free in the module_exit function.
By making your module the owner of opened objects, or by incrementing the reference count of the module yourself when something is opened, you ensure that the module cannot be unloaded while it's still used. |
Thx for your reply.
I was wondering about the situation where you have to call unregister_chrdev in module_init (for example, because module_init detects a problem after register_chrdev and needs to deregister/free all resources it already acquired). Note that the specification of register_chrdev states that the device may start being used immediately after register_chrdev. |
Good question. ;-)
It is recommended that any calls that allow userspace to access your device should come last, i.e., make all resource allocations and other checks before that. (And if you don't do this, your device file's open callback could get into problems if it needs such resources.) If that is not possible, you have to write your driver in such a way that it waits until there are no acesses from userspace. The file object is reference-counted, so you just have to wait for the last close callback of your device file. With regard to your first post: If register_chrdev returns an error, it is impossible for this device to have been opened. |
Quote:
Again, thanks for your help! :-) Quote:
A solution to the last question might be to let release notify init_module if it releases a connection. |
Quote:
Quote:
In other words, as long as all connections from userspace are properly module-reference counted, you can safely free everything in the module_exit function, without having to wait. The module reference count helps only with module loading/unloading; if you want to unregister some device before module unloading (for example, because your USB device was unplugged), you actually have to wait for all userspace connections to be closed before you can free the resources they might access. |
All times are GMT -5. The time now is 01:23 AM. |