Re: [PATCH v2] usb: core: Solve race condition in anchor cleanup functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 29/07/20 16:38, Alan Stern wrote:
With a small amount of restructuring you can eliminate three unlock-lock
pairs and avoid the need for usb_anchor_safe_empty():
[ ... ]
All you have to do is move this spin_lock_irq() above the start of the
outer loop...
[ ... ]
.. and move this spin_unlock_irq() below the end of the outer loop.
Likewise for the two other routines.

I'm afraid that might not work. The whole purpose of the outer loop is to kick in when urb_list is empty, but there's this unanchor-completer race going on. So the inner loop will be skipped, because list_empty(&anchor->urb_list) will evaluate true. As a result, the spinlock will be held as the loop spins, until the completer has finished.

But if the completer tries to take the same lock, we're deadlocked. For example, if it resubmits the URB, which is pretty much the point of this extra while loop.

This is also the reason why I didn't just modify the original while-loop's condition, so it would go on spinning as long the race condition is in effect. It mustn't spin with the lock held.
>  +	} while (unlikely(!usb_anchor_safe_empty(anchor)));
likely() and unlikely() are frowned upon unless you can provide actual
measurements showing that they make a significant difference.  In this
case they don't matter, since the bottleneck is the usb_kill_urb() call.
The irony is that I added this "unlikely" for the human reader, and not for the compiler: I wanted to communicate that the outer loop is unlikely to kick in. I'll keep that in mind for v3 of this patch.

Thanks,
   Eli




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux