Am Montag, 23. Juni 2003 20:11 schrieb Tuukka Toivonen:
> On Mon, 23 Jun 2003, Pete Zaitcev wrote:
> >> If usb_disconnect() can happen while already executing in open(), and if
> >> the former frees the data structures used by the driver, then the latter
> >> call might continue working with already freed memory region which could
> >> lead to catastrophe.
> >
> >This is why God has given you semaphores, spinlocks, and
> >reference counts.
>
> I guess I'm a newbie in kernel programming, but I can't think of any way to
> fix it.
>
> Let's suppose user calls open() on my device. The first line of open()
> call could lock semaphores, increase reference counts, whatever, but all is
> useless if usb_disconnect() happens _after_ open() system call but _before_
> the first line of the open()-function has been executed.

Exactly. Very good. Therefore you must never free the lock and that piece
of data (usually a pointer to a device descriptor) that allows you to
tell that the device is still there. Like:

open():

down(&sem);
if (dev_descr == NULL) {
        up(&sem);
        return -ENODEV;
}
if (dev_descr->remove_pending) {
        up(&sem);
        return -ENODEV;
}
dev_descr->in_use = 1;
up(&sem);

disconnect():

down(&sem);
if (dev_descr->in_use) {
        dev_descr->remove_pending = 1;
} else {
        kfree(dev_descr);
        dev_descr = NULL;
}
up(&sem);

release():

down(&sem);
if (dev_descr->remove_pending) {
        kfree(dev_descr);
        dev_descr = NULL;
}
up(&sem);

Many examples can be found in the kernel sources.

> And wasn't it Linus not God... oh nevermind. I just remembered the
> principle of equivalence ;)

Blessed be he and all code that floweth from him.

        HTH
                Oliver



-------------------------------------------------------
This SF.Net email is sponsored by: INetU
Attention Web Developers & Consultants: Become An INetU Hosting Partner.
Refer Dedicated Servers. We Manage Them. You Get 10% Monthly Commission!
INetU Dedicated Managed Hosting http://www.inetu.net/partner/index.php
_______________________________________________
[EMAIL PROTECTED]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel

Reply via email to