Skip to contentSkip to conversation

xhci: Fix race related to abort operation

PATCH In review Lu Baolu 2 participants 3 messages Dec 23, 2016

Subsystems (3)
lore
Share & tools
lore
author
#1 lore raw

PATCH 1/1 xhci: Fix race related to abort operation

In reply to Lu Baolu

From: OGAWA Hirofumi <[email protected]>

Current abort operation has race.

    xhci_handle_command_timeout()
      xhci_abort_cmd_ring()
        xhci_write_64(CMD_RING_ABORT)
        xhci_handshake(5s)
	  do {
	    check CMD_RING_RUNNING
            udelay(1)
					 ...
					 COMP_CMD_ABORT event
					 COMP_CMD_STOP event
					 xhci_handle_stopped_cmd_ring()
					   restart cmd_ring
                                           CMD_RING_RUNNING become 1 again
	  } while ()
          return -ETIMEDOUT
        xhci_write_64(CMD_RING_ABORT)
        /* can abort random command */

To do abort operation correctly, we have to wait both of COMP_CMD_STOP event and negation of CMD_RING_RUNNING.

But like above, while timeout handler is waiting negation of CMD_RING_RUNNING, event handler can restart cmd_ring. So timeout handler never be notice negation of CMD_RING_RUNNING, and retry of CMD_RING_ABORT can abort random command (BTW, I guess retry of CMD_RING_ABORT was workaround of this race).

To fix this race, this moves xhci_handle_stopped_cmd_ring() to xhci_abort_cmd_ring(). And timeout handler waits COMP_CMD_STOP event.

At this point, timeout handler is owner of cmd_ring, and safely restart cmd_ring by using xhci_handle_stopped_cmd_ring().

[FWIW, as bonus, this way would be easily extend to add CMD_RING_PAUSE operation]

[baolu: release xhci->lock before wait and apply it back again after wait. This is the reason my signed-off-by is added.]

---
 drivers/usb/host/xhci-mem.c  |   1 +
 drivers/usb/host/xhci-ring.c | 171 ++++++++++++++++++++++---------------------
 drivers/usb/host/xhci.h      |   1 +
 3 files changed, 91 insertions(+), 82 deletions(-)
drivers/usb/host/xhci-mem.c +1 -0
index 536f572..70c9204 100644
@@ -2379,6 +2379,7 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
 
 	/* init command timeout work */
 	INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout);
+	init_completion(&xhci->cmd_ring_stop_completion);
 
 	page_size = readl(&xhci->op_regs->page_size);
 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
drivers/usb/host/xhci-ring.c +89 -82

227 lines not on this page · raw message

drivers/usb/host/xhci.h +1 -0
index da5bfd6..c525722 100644
@@ -1572,6 +1572,7 @@ struct xhci_hcd {
 	struct list_head        cmd_list;
 	unsigned int		cmd_ring_reserved_trbs;
 	struct delayed_work	cmd_timer;
+	struct completion	cmd_ring_stop_completion;
 	struct xhci_command	*current_cmd;
 	struct xhci_ring	*event_ring;
 	struct xhci_erst	erst;
Signature · 1 line
2.1.4

Lu Baolu

author
#0 lore raw

cover xhci: Fix race related to abort operation

Hi Mathias,

This is a follow-up patch for below comment

"rebase OGAWA Hirofumi's changes on top of that, and send to usb-linus only"

in below discussion thread.

https://lkml.org/lkml/2016/12/21/186

I rebased the patch and added unlock-before and lock-after xhci->lock during waiting for the command stopped event.

It sits at the same place in your timeout_race_fixes branch.

(git://git.kernel.org/pub/scm/linux/kernel/git/mnyman/xhci.git timeout_race_fixes)

Best regards,
Lu Baolu

OGAWA Hirofumi (1):
xhci: Fix race related to abort operation

 drivers/usb/host/xhci-mem.c  |   1 +
 drivers/usb/host/xhci-ring.c | 171 ++++++++++++++++++++++---------------------
 drivers/usb/host/xhci.h      |   1 +
 3 files changed, 91 insertions(+), 82 deletions(-)
Signature · 1 line
2.1.4
#2 lore raw
Quoted 4 lines · Lu Baolu

Hi Mathias,

This is a follow-up patch for below comment

Quoted 9 lines

"rebase OGAWA Hirofumi's changes on top of that, and send to usb-linus only"

in below discussion thread.

https://lkml.org/lkml/2016/12/21/186

I rebased the patch and added unlock-before and lock-after xhci->lock during waiting for the command stopped event.

Thanks,
I edited the patch in a very similar way already,

-Mathias