Skip to contentSkip to conversation

further issues in uss720 disconnect()

DISCUSSION Oliver Neukum 2 participants 4 messages Feb 24 USB

lore
Share & tools
lore
#0 lore raw

Hi,

looking at this driver

                parport_remove_port(pp);
                parport_put_port(pp);
                kill_all_async_requests_priv(priv);
                kref_put(&priv->ref_count, destroy_priv);

Now looking at kill_all_async_requests_priv()

        spin_lock_irqsave(&priv->asynclock, flags);
        list_for_each_entry(rq, &priv->asynclist, asynclist) {
                usb_unlink_urb(rq->urb);
                ret++;
        }
        spin_unlock_irqrestore(&priv->asynclock, flags);
        return ret;

we see that the driver is using usb_unlink_urb(), not usb_kill_urb() In other words, once we return from this function, the completion handlers can still be running.
After that we do destroy_priv(), which will happily free the descriptor

        usb_put_dev(priv->usbdev);
        priv->usbdev = NULL;
        kfree(priv);

Assuming that completion handler runs after this

        struct parport_uss720_private *priv;
        int status = urb->status;

        rq = urb->context;
        priv = rq->priv;
        pp = priv->pp;

we see that it will happily access kfreed memory.

I think the driver needs a wait queue for asynchronous requests. Or should I look for a way to use usb_kill_urb()?
What do you think?

Regards
Oliver

#1 lore raw
Quoted 37 lines · Oliver Neukum

Hi,

looking at this driver

                parport_remove_port(pp);
                parport_put_port(pp);
                kill_all_async_requests_priv(priv);
                kref_put(&priv->ref_count, destroy_priv);

Now looking at kill_all_async_requests_priv()

        spin_lock_irqsave(&priv->asynclock, flags);
        list_for_each_entry(rq, &priv->asynclist, asynclist) {
                usb_unlink_urb(rq->urb);
                ret++;
        }
        spin_unlock_irqrestore(&priv->asynclock, flags);
        return ret;

we see that the driver is using usb_unlink_urb(), not usb_kill_urb() In other words, once we return from this function, the completion handlers can still be running.
After that we do destroy_priv(), which will happily free the descriptor

        usb_put_dev(priv->usbdev);
        priv->usbdev = NULL;
        kfree(priv);

Assuming that completion handler runs after this

        struct parport_uss720_private *priv;
        int status = urb->status;

        rq = urb->context;
        priv = rq->priv;
        pp = priv->pp;
Quoted 5 lines

we see that it will happily access kfreed memory.

I think the driver needs a wait queue for asynchronous requests. Or should I look for a way to use usb_kill_urb()?
What do you think?

Another possible solution: Keep track of the number of outstanding async requests. Then take a reference to priv->usbdev whenever the number goes above 0 and release the reference whenever the number drops to 0.

Alan Stern

#2 lore raw
Quoted 11 lines · Alan Stern
Quoted 5 lines · Oliver Neukum

we see that it will happily access kfreed memory.

I think the driver needs a wait queue for asynchronous requests. Or should I look for a way to use usb_kill_urb()?
What do you think?

Another possible solution: Keep track of the number of outstanding async requests. Then take a reference to priv->usbdev whenever the number goes above 0 and release the reference whenever the number drops to 0.

This seems a bit complicated. Why not just get a kref in every case?

Regards
Oliver

#3 lore raw
Quoted 9 lines · Oliver Neukum
Quoted 8 lines · Alan Stern
Quoted 5 lines · Oliver Neukum

we see that it will happily access kfreed memory.

I think the driver needs a wait queue for asynchronous requests. Or should I look for a way to use usb_kill_urb()?
What do you think?

Quoted 5 lines
Quoted 3 lines

Another possible solution: Keep track of the number of outstanding async requests. Then take a reference to priv->usbdev whenever the number goes above 0 and release the reference whenever the number drops to 0.

This seems a bit complicated. Why not just get a kref in every case?

You could. On some platforms the atomic operations used for krefs are a little more expensive than an ordinary increment or decrement.

On the other hand, if async requests can start and complete concurrently then the counter would also have to be atomic, which would remove any advantage. (Unless there's already a spinlock protecting everything.)

Either way, it seems easier than using a wait queue or work thread.

Alan Stern