From patchwork Sun Jun 9 15:18:30 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 2694321 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) by patchwork2.kernel.org (Postfix) with ESMTP id 5931ADF2A1 for ; Sun, 9 Jun 2013 15:22:02 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UlhQ8-0008C4-9Q; Sun, 09 Jun 2013 15:20:41 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UlhPd-0006lA-9D; Sun, 09 Jun 2013 15:20:09 +0000 Received: from mail-pa0-x22f.google.com ([2607:f8b0:400e:c03::22f]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UlhPZ-0006ij-Gv for linux-arm-kernel@lists.infradead.org; Sun, 09 Jun 2013 15:20:06 +0000 Received: by mail-pa0-f47.google.com with SMTP id kl14so449663pab.6 for ; Sun, 09 Jun 2013 08:19:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=l4ctAXTf7f/lRPugg3nPUm5uvRappnIpdXc61/JWOL0=; b=NXIHHks5Z/JQSGpHBjqFffXA1wAIyLXnYdM53LqQrJaD8ZeICZnpikaZH7SoDFB8g+ pXDT+zcPhwmdOFZL83zc5NboZBijy0j7m1TSbbYBQ8fCdMp5vAQXsuK6GvE+wazi2ync lKikh83FB5yFA8KmRIUYKEnHfRyZOf1EMGMUJFkq1bZfeFTB5Ko/rJLWgzsDsnA1/rKi WAgJSlumjKWx8sAsqPtA+KwaOCD4LJjzfg43KjYexhnOZ8nYl8D1s35YkNrEJb1bYTaU 2u6w0ss3L48OFQg87vRBoWNmrmWffrfdRjs2OnVYC0TYTII6TRDLYcf/j0yesK7F1GTb y6SQ== X-Received: by 10.66.8.41 with SMTP id o9mr10642204paa.13.1370791183922; Sun, 09 Jun 2013 08:19:43 -0700 (PDT) Received: from localhost ([183.37.199.34]) by mx.google.com with ESMTPSA id qp4sm6892143pbc.41.2013.06.09.08.19.38 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sun, 09 Jun 2013 08:19:43 -0700 (PDT) From: Ming Lei To: Greg Kroah-Hartman Subject: [RFC PATCH 3/4] USB: EHCI: improve interrupt qh unlink Date: Sun, 9 Jun 2013 23:18:30 +0800 Message-Id: <1370791112-18464-4-git-send-email-ming.lei@canonical.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1370791112-18464-1-git-send-email-ming.lei@canonical.com> References: <1370791112-18464-1-git-send-email-ming.lei@canonical.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130609_112005_746366_87ED4149 X-CRM114-Status: GOOD ( 24.72 ) X-Spam-Score: -1.9 (-) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-1.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (tom.leiming[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Ming Lei , linux-usb@vger.kernel.org, Alan Stern , linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Given interrupt URB will be resubmitted from tasklet context which is scheduled by ehci hardware interrupt handler, and commonly only one interrupt URB is scheduled on qh, so the qh may be unlinked immediately once qh_completions() returns from ehci_irq(), then the intr URB to be resubmitted in complete() can only be scheduled and linked to hardware until the qh unlink is completed. This patch applied the QH_STATE_UNLINK_WAIT(not used by interrupt qh) state to improve this above situation, and the qh will wait for 5 milliseconds before being unlinked from hardware, if one URB is submitted during the period, the qh is move out of unlink wait state and the interrupt transfer can be scheduled immediately, not like before: the transfer is only linked to hardware until previous unlink is completed. Only enable the improvement in case that HCD supports to run giveback of URB in tasklet context. Cc: Alan Stern Signed-off-by: Ming Lei --- drivers/usb/host/ehci-hcd.c | 16 ++++++++--- drivers/usb/host/ehci-hub.c | 1 + drivers/usb/host/ehci-sched.c | 60 ++++++++++++++++++++++++++++++++++++++--- drivers/usb/host/ehci-timer.c | 43 +++++++++++++++++++++++++++++ drivers/usb/host/ehci.h | 3 +++ 5 files changed, 115 insertions(+), 8 deletions(-) diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c index 246e124..0ab82de 100644 --- a/drivers/usb/host/ehci-hcd.c +++ b/drivers/usb/host/ehci-hcd.c @@ -304,7 +304,9 @@ static void end_unlink_async(struct ehci_hcd *ehci); static void unlink_empty_async(struct ehci_hcd *ehci); static void unlink_empty_async_suspended(struct ehci_hcd *ehci); static void ehci_work(struct ehci_hcd *ehci); -static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh); +static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh, + bool wait); +static void start_do_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh); static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh); #include "ehci-timer.c" @@ -484,6 +486,7 @@ static int ehci_init(struct usb_hcd *hcd) ehci->periodic_size = DEFAULT_I_TDPS; INIT_LIST_HEAD(&ehci->async_unlink); INIT_LIST_HEAD(&ehci->async_idle); + INIT_LIST_HEAD(&ehci->intr_unlink_wait); INIT_LIST_HEAD(&ehci->intr_unlink); INIT_LIST_HEAD(&ehci->intr_qh_list); INIT_LIST_HEAD(&ehci->cached_itd_list); @@ -908,15 +911,20 @@ static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) switch (qh->qh_state) { case QH_STATE_LINKED: if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) - start_unlink_intr(ehci, qh); + start_unlink_intr(ehci, qh, false); else start_unlink_async(ehci, qh); break; case QH_STATE_COMPLETING: qh->dequeue_during_giveback = 1; break; - case QH_STATE_UNLINK: case QH_STATE_UNLINK_WAIT: + if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) { + cancel_unlink_wait_intr(ehci, qh); + start_unlink_intr(ehci, qh, false); + } + break; + case QH_STATE_UNLINK: /* already started */ break; case QH_STATE_IDLE: @@ -1042,7 +1050,7 @@ ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep) if (eptype == USB_ENDPOINT_XFER_BULK) start_unlink_async(ehci, qh); else - start_unlink_intr(ehci, qh); + start_unlink_intr(ehci, qh, false); } } spin_unlock_irqrestore(&ehci->lock, flags); diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c index b2f6450..4104c66 100644 --- a/drivers/usb/host/ehci-hub.c +++ b/drivers/usb/host/ehci-hub.c @@ -345,6 +345,7 @@ static int ehci_bus_suspend (struct usb_hcd *hcd) end_unlink_async(ehci); unlink_empty_async_suspended(ehci); + ehci_handle_start_intr_unlinks(ehci); ehci_handle_intr_unlinks(ehci); end_free_itds(ehci); diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c index acff5b8..d132c6f 100644 --- a/drivers/usb/host/ehci-sched.c +++ b/drivers/usb/host/ehci-sched.c @@ -601,10 +601,10 @@ static void qh_unlink_periodic(struct ehci_hcd *ehci, struct ehci_qh *qh) list_del(&qh->intr_node); } -static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh) +static void start_do_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh) { - /* If the QH isn't linked then there's nothing we can do. */ - if (qh->qh_state != QH_STATE_LINKED) + /* If the QH isn't unlink wait then there's nothing we can do. */ + if (qh->qh_state != QH_STATE_UNLINK_WAIT) return; qh_unlink_periodic (ehci, qh); @@ -632,6 +632,55 @@ static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh) } } +static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh, + bool wait) +{ + /* If the QH isn't linked then there's nothing we can do. */ + if (qh->qh_state != QH_STATE_LINKED) + return; + + /* + * It is common only one intr URB is scheduled on one qh, and given + * complete() is run in tasklet context, introduce QH_STATE_UNLINK_WAIT + * to avoid unlink qh too early. + */ + qh->qh_state = QH_STATE_UNLINK_WAIT; + + /* bypass the optimization without URB giveback in tasklet */ + if (!hcd_giveback_urb_in_bh(ehci_to_hcd(ehci)) || !wait) { + start_do_unlink_intr(ehci, qh); + return; + } + + qh->unlink_cycle = ehci->intr_unlink_wait_cycle; + + /* New entries go at the end of the intr_unlink_wait list */ + list_add_tail(&qh->unlink_node, &ehci->intr_unlink_wait); + + if (ehci->rh_state < EHCI_RH_RUNNING) + ehci_handle_start_intr_unlinks(ehci); + else if (ehci->intr_unlink_wait.next == &qh->unlink_node) { + ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true); + ++ehci->intr_unlink_wait_cycle; + } +} + +/* must be called with holding ehci->lock */ +static void cancel_unlink_wait_intr(struct ehci_hcd *ehci, struct ehci_qh *qh) +{ + /* If the QH isn't waited for unlink then do nothing */ + if (qh->qh_state != QH_STATE_UNLINK_WAIT) + return; + + /* restored to linked state */ + list_del(&qh->unlink_node); + qh->qh_state = QH_STATE_LINKED; + + /* avoid unnecessary CPU wakeup */ + if (list_empty(&ehci->intr_unlink_wait)) + ehci_disable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR); +} + static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh) { struct ehci_qh_hw *hw = qh->hw; @@ -876,6 +925,9 @@ static int intr_submit ( goto done; } + /* put back qh from unlink wait list */ + cancel_unlink_wait_intr(ehci, qh); + /* then queue the urb's tds to the qh */ qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv); BUG_ON (qh == NULL); @@ -921,7 +973,7 @@ static void scan_intr(struct ehci_hcd *ehci) temp = qh_completions(ehci, qh); if (unlikely(temp || (list_empty(&qh->qtd_list) && qh->qh_state == QH_STATE_LINKED))) - start_unlink_intr(ehci, qh); + start_unlink_intr(ehci, qh, true); } } } diff --git a/drivers/usb/host/ehci-timer.c b/drivers/usb/host/ehci-timer.c index 11e5b32..c2320f3 100644 --- a/drivers/usb/host/ehci-timer.c +++ b/drivers/usb/host/ehci-timer.c @@ -72,6 +72,7 @@ static unsigned event_delays_ns[] = { 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_DEAD */ 1125 * NSEC_PER_USEC, /* EHCI_HRTIMER_UNLINK_INTR */ 2 * NSEC_PER_MSEC, /* EHCI_HRTIMER_FREE_ITDS */ + 5 * NSEC_PER_MSEC, /* EHCI_HRTIMER_START_UNLINK_INTR */ 6 * NSEC_PER_MSEC, /* EHCI_HRTIMER_ASYNC_UNLINKS */ 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IAA_WATCHDOG */ 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */ @@ -98,6 +99,17 @@ static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event, } } +/* Warning: don't call this function from hrtimer handler context */ +static void ehci_disable_event(struct ehci_hcd *ehci, unsigned event) +{ + if (!hcd_giveback_urb_in_bh(ehci_to_hcd(ehci))) + return; + + ehci->enabled_hrtimer_events &= ~(1 << event); + if (!ehci->enabled_hrtimer_events) + hrtimer_cancel(&ehci->hrtimer); +} + /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */ static void ehci_poll_ASS(struct ehci_hcd *ehci) @@ -215,6 +227,36 @@ static void ehci_handle_controller_death(struct ehci_hcd *ehci) /* Not in process context, so don't try to reset the controller */ } +/* start to unlink interrupt QHs */ +static void ehci_handle_start_intr_unlinks(struct ehci_hcd *ehci) +{ + bool stopped = (ehci->rh_state < EHCI_RH_RUNNING); + + /* + * Process all the QHs on the intr_unlink list that were added + * before the current unlink cycle began. The list is in + * temporal order, so stop when we reach the first entry in the + * current cycle. But if the root hub isn't running then + * process all the QHs on the list. + */ + while (!list_empty(&ehci->intr_unlink_wait)) { + struct ehci_qh *qh; + + qh = list_first_entry(&ehci->intr_unlink_wait, + struct ehci_qh, unlink_node); + if (!stopped && + qh->unlink_cycle == ehci->intr_unlink_wait_cycle) + break; + list_del(&qh->unlink_node); + start_do_unlink_intr(ehci, qh); + } + + /* Handle remaining entries later */ + if (!list_empty(&ehci->intr_unlink_wait)) { + ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true); + ++ehci->intr_unlink_wait_cycle; + } +} /* Handle unlinked interrupt QHs once they are gone from the hardware */ static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci) @@ -363,6 +405,7 @@ static void (*event_handlers[])(struct ehci_hcd *) = { ehci_handle_controller_death, /* EHCI_HRTIMER_POLL_DEAD */ ehci_handle_intr_unlinks, /* EHCI_HRTIMER_UNLINK_INTR */ end_free_itds, /* EHCI_HRTIMER_FREE_ITDS */ + ehci_handle_start_intr_unlinks, /* EHCI_HRTIMER_START_UNLINK_INTR */ unlink_empty_async, /* EHCI_HRTIMER_ASYNC_UNLINKS */ ehci_iaa_watchdog, /* EHCI_HRTIMER_IAA_WATCHDOG */ ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */ diff --git a/drivers/usb/host/ehci.h b/drivers/usb/host/ehci.h index 7c978b2..1d2c164 100644 --- a/drivers/usb/host/ehci.h +++ b/drivers/usb/host/ehci.h @@ -88,6 +88,7 @@ enum ehci_hrtimer_event { EHCI_HRTIMER_POLL_DEAD, /* Wait for dead controller to stop */ EHCI_HRTIMER_UNLINK_INTR, /* Wait for interrupt QH unlink */ EHCI_HRTIMER_FREE_ITDS, /* Wait for unused iTDs and siTDs */ + EHCI_HRTIMER_START_UNLINK_INTR, /* wait for new intr schedule */ EHCI_HRTIMER_ASYNC_UNLINKS, /* Unlink empty async QHs */ EHCI_HRTIMER_IAA_WATCHDOG, /* Handle lost IAA interrupts */ EHCI_HRTIMER_DISABLE_PERIODIC, /* Wait to disable periodic sched */ @@ -143,6 +144,8 @@ struct ehci_hcd { /* one per controller */ unsigned i_thresh; /* uframes HC might cache */ union ehci_shadow *pshadow; /* mirror hw periodic table */ + struct list_head intr_unlink_wait; + unsigned intr_unlink_wait_cycle; struct list_head intr_unlink; unsigned intr_unlink_cycle; unsigned now_frame; /* frame from HC hardware */