Received: from eggs.gnu.org ([2001:4830:134:3::10]:33889) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUAuw-0003rT-Jp for qemu-devel@nongnu.org; Mon, 01 Aug 2016 06:57:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bUAut-0004Sj-F3 for qemu-devel@nongnu.org; Mon, 01 Aug 2016 06:57:54 -0400 Received: from mail-db5eur01on0096.outbound.protection.outlook.com ([104.47.2.96]:51516 helo=EUR01-DB5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUAus-0004Rm-Px for qemu-devel@nongnu.org; Mon, 01 Aug 2016 06:57:51 -0400 References: <1469638520-32706-1-git-send-email-den@openvz.org> From: "Denis V. Lunev" Message-ID: Date: Mon, 1 Aug 2016 10:24:55 +0200 MIME-Version: 1.0 In-Reply-To: <1469638520-32706-1-git-send-email-den@openvz.org> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH 1/1] ehci: faster frame index calculation for skipped frames List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Evgeny Yakovlev , Gerd Hoffmann On 07/27/2016 06:55 PM, Denis V. Lunev wrote: > From: Evgeny Yakovlev > > ehci_update_frindex takes time linearly proportional to a number > of uframes to calculate new frame index and raise FLR interrupts, > which is a problem for large amounts of uframes. > > If we experience large delays between echi timer callbacks (i.e. because > other periodic handlers have taken a lot of time to complete) we > get a lot of skipped frames which then delay ehci timer callback more > and this leads to deadlocking the system when ehci schedules next > callback to be too soon. > > Observable behaviour is qemu consuming 100% host CPU time while guest > is unresponsive. This misbehavior could happen for a while and QEMU does > not get out from this state automatically without the patch. > > This change makes ehci_update_frindex execute in constant time. > > Signed-off-by: Evgeny Yakovlev > Signed-off-by: Denis V. Lunev > CC: Gerd Hoffmann > --- > hw/usb/hcd-ehci.c | 31 +++++++++++++++---------------- > 1 file changed, 15 insertions(+), 16 deletions(-) > > diff --git a/hw/usb/hcd-ehci.c b/hw/usb/hcd-ehci.c > index 43a8f7a..b093db7 100644 > --- a/hw/usb/hcd-ehci.c > +++ b/hw/usb/hcd-ehci.c > @@ -2206,29 +2206,28 @@ static void ehci_advance_periodic_state(EHCIState *ehci) > > static void ehci_update_frindex(EHCIState *ehci, int uframes) > { > - int i; > - > if (!ehci_enabled(ehci) && ehci->pstate == EST_INACTIVE) { > return; > } > > - for (i = 0; i < uframes; i++) { > - ehci->frindex++; > - > - if (ehci->frindex == 0x00002000) { > - ehci_raise_irq(ehci, USBSTS_FLR); > - } > + /* Generate FLR interrupt if frame index rolls over 0x2000 */ > + if ((ehci->frindex % 0x2000) + uframes >= 0x2000) { > + ehci_raise_irq(ehci, USBSTS_FLR); > + } > > - if (ehci->frindex == 0x00004000) { > - ehci_raise_irq(ehci, USBSTS_FLR); > - ehci->frindex = 0; > - if (ehci->usbsts_frindex >= 0x00004000) { > - ehci->usbsts_frindex -= 0x00004000; > - } else { > - ehci->usbsts_frindex = 0; > - } > + /* How many times will frindex roll over 0x4000 with this frame count? > + * usbsts_frindex is decremented by 0x4000 on rollover until it reaches 0 > + */ > + int rollovers = (ehci->frindex + uframes) / 0x4000; > + if (rollovers > 0) { > + if (ehci->usbsts_frindex >= (rollovers * 0x4000)) { > + ehci->usbsts_frindex -= 0x4000 * rollovers; > + } else { > + ehci->usbsts_frindex = 0; > } > } > + > + ehci->frindex = (ehci->frindex + uframes) % 0x4000; > } > > static void ehci_frame_timer(void *opaque) ping