How should an exit routine wait for release() callbacks?

Alan Stern <[email protected]>
Newsgroups gmane.linux.kernel,gmane.linux.usb.devel
Message-ID <[email protected]>
Here's a not-so-theoretical question.

I've got a module which registers a struct device.  (It represents a
virtual device, not a real one, but that doesn't matter.)  Obviously the
module's exit routine has to wait until the release() routine for that
device has been invoked -- if it returned too early then the release()
call would oops.

How should it wait?

The most straightforward approach is to use a struct completion, like 
this:

	static struct {
		struct device dev;
		...
	} my_dev;

	static DECLARE_COMPLETION(my_completion);

	static void my_release(struct device *dev)
	{
		complete(&my_completion);
	}

	static void __exit my_exit(void)
	{
		device_unregister(&my_dev.dev);
		wait_for_completion(&my_completion);
	}

The problem is that there is no guarantee a context switch won't take
place after my_release() has called complete() and before my_release()  
returns.  If that happens and my_exit() finishes running, then the module
will be unloaded and the next context switch back to finish off
my_release() will oops.

Other approaches have similar defects.  So how can this problem be solved?

Alan Stern
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.