[RFC] usbfs2 aio cancellation code

Sarah Bailey <[email protected]>
Newsgroups gmane.linux.usb.devel
Message-ID <20071002020124.GA14173@localdomain>
I've been trying to figure out how usbfs2's code would look with the new
in-kernel aio cancellation and serialization rules. [1]  It's making my
head hurt, so I'd appreciate some code review.

Background:

The in-kernel aio patches that Zack is working will ensure that the
retry function and the cancel function will run serially.  If the cancel
function returns 0, the iocb will be destroyed.  If cancel returns zero,
it guarantees that the iocb will never be referenced again, specifically
by making sure that kick_iocb() and aio_complete() have never been and
never will be called.

Some USB rules apply here too.  To cancel a USB transaction, you can
call either usb_kill_urb() or usb_unlink_urb().  usb_kill_urb() will
wait for the URB callback to complete, but it cannot be called with a
spin lock held.  usb_unlink_urb() guarantees that the URB callback will
be called sometime in the future with an URB status that indicates it
was cancelled.  usb_unlink_urb() will return -EBUSY if the URB callback
was about to run or was in the middle of running.


I *think* I can follow the new aio rules, if the aio core provides a
spinlock for the iocb priv pointer.  Here's how it would work:

cancel
	ret = 0;
	lock and disable interrupts
	/* Check to see if the URB has been freed yet.
	 * If not, attempt to cancel the URB;
	 * fail if the URB callback has already started.
	 * We know that read_retry can't run while cancel is running. */
	if(iocb->priv == NULL ||
			usb_unlink_urb(iocb->priv->urb) == -EBUSY)
		ret = -EBUSY; 
	unlock
	return ret

write_callback
	if the URB is not canceled
		lock
		store priv pointer in local variable
		urb->context->kiocb->priv = NULL;
		unlock
		aio_complete(urb->context->iocb);
	free stuff

read_callback
	if the URB is not cancelled
		kick_iocb();
	else
		free stuff

read_retry
	copy data
	free stuff
	iocb->priv = NULL;
	aio_complete();


Does the cancel function follow the new aio cancel rules (i.e. it
doesn't return 0 if kick_iocb() or aio_complete() has been called or
will be called)?  Are there any races?  I can't see any, but I've been
banging my head against this a lot this weekend.

Sarah

p.s. Zack - did you mean to do something with the return value from the
cancel function?  AFAIK, it simply checks that it's nonzero.

[1] http://marc.info/?l=linux-aio&m=119093658429265&w=2

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
[email protected]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel
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.