Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2734748pxj; Mon, 31 May 2021 09:24:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwD+4kIHl4BAoEP2GMkrj3Y30kMAOFsv6eWqlYP1cKRESMVpxGU+3kcFIhiu0NoSjJynAik X-Received: by 2002:a05:6e02:20ea:: with SMTP id q10mr2813660ilv.42.1622478242552; Mon, 31 May 2021 09:24:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622478242; cv=none; d=google.com; s=arc-20160816; b=akIpl49Ds4wwkNlS9gj2FXMgj6B/7nLJn+NACBGOexiplPMG1MFDZe4NTaEfAnZrMA WA4UYI2nYJEBoarITuCyAqH+JX5ELsP2ZgeEoz77fju5Eg0HyTbu14iJ28+0LZYth9NE eS7v6viMIlw7eK+0zr3Z/y2dTRcHv4jL8wC0AY1v/az1stpy3Lnt5MbT9Yydr53hWcKx P9XHqj+GagV4oY85gMhwFOsEm5CupwOIMiNk9vf6xGa2Y9mZVt2cZYhSfElEK4IO6x4k hghGZiFK83ngxs6BcktZXQfVFdrs1ipjN7nEyCsz00gOKJ6LVQ9xT1oYnSeoHL6keBsD /sSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MAN6GcjSvpEtUW6Im+gMjjGYjdD45u6v1BxtAvqp/NY=; b=KTPYhO99n2htQUcYA2Y0ciY7CvJ1k1cSYTOD3IE7Hbjo0PO8sR14TuB4a2zNDZouWI 8tpZGUu5Jmm5U4wk5W805U2cBJOdXQywNNRRJtIeEtD/uDBTN3T4pztywnXOlesczkck Dem5xKjqxnuJxqQjsOAgQvZFrIGIyGWHY9J+h8tM8fw3lY07ZsgD4xyA9h806PkBctBR tiFU+Akw9i9YVtYkLlW4l2GaxkSekGs1Qi4IUR6eGRafCy6IxeEnrgnTpNEnGl8bFl2N xr0h1UP2k+DTSgg2Hbf10wHV8bDGbMyOONDZSK9m5iCUE1tIihC6ryZe4wPflcDDqVlp MzmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="fDjaxtC/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a11si1989986jap.60.2021.05.31.09.23.49; Mon, 31 May 2021 09:24:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="fDjaxtC/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234589AbhEaQY6 (ORCPT + 99 others); Mon, 31 May 2021 12:24:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:38018 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232836AbhEaOnh (ORCPT ); Mon, 31 May 2021 10:43:37 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id AB6AD61C70; Mon, 31 May 2021 13:54:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622469279; bh=LI6tbHe4ZMdS7xGy7ZTHhToBgx0h3n6G75mb/ZqC3HA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fDjaxtC/dMnMQ0urOVO9Uln8Sbow9oU8bs7AxCB3/mlJpnMsxeN/8ByPdSmOX9wv3 z7xZLoQifNYfTjEmWSE47CH3lliKXazAw7hdO93yrPxIoGOSE8OP/bLfpSczhkNCb2 uSgMnqnXPsItAN9ltLxN8PZCRamFnYdSE0I7yMCI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Michael Grzeschik , Felipe Balbi , Thinh Nguyen Subject: [PATCH 5.12 103/296] usb: dwc3: gadget: Properly track pending and queued SG Date: Mon, 31 May 2021 15:12:38 +0200 Message-Id: <20210531130707.398562809@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130703.762129381@linuxfoundation.org> References: <20210531130703.762129381@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thinh Nguyen commit 25dda9fc56bd90d45f9a4516bcfa5211e61b4290 upstream. The driver incorrectly uses req->num_pending_sgs to track both the number of pending and queued SG entries. It only prepares the next request if the previous is done, and it doesn't update num_pending_sgs until there is TRB completion interrupt. This may starve the controller of more TRBs until the num_pending_sgs is decremented. Fix this by decrementing the num_pending_sgs after they are queued and properly track both num_mapped_sgs and num_queued_sgs. Fixes: c96e6725db9d ("usb: dwc3: gadget: Correct the logic for queuing sgs") Cc: Reported-by: Michael Grzeschik Tested-by: Michael Grzeschik Acked-by: Felipe Balbi Signed-off-by: Thinh Nguyen Link: https://lore.kernel.org/r/ba24591dbcaad8f244a3e88bd449bb7205a5aec3.1620874069.git.Thinh.Nguyen@synopsys.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/usb/dwc3/gadget.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1236,6 +1236,7 @@ static int dwc3_prepare_trbs_sg(struct d req->start_sg = sg_next(s); req->num_queued_sgs++; + req->num_pending_sgs--; /* * The number of pending SG entries may not correspond to the @@ -1243,7 +1244,7 @@ static int dwc3_prepare_trbs_sg(struct d * don't include unused SG entries. */ if (length == 0) { - req->num_pending_sgs -= req->request.num_mapped_sgs - req->num_queued_sgs; + req->num_pending_sgs = 0; break; } @@ -2839,15 +2840,15 @@ static int dwc3_gadget_ep_reclaim_trb_sg struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; struct scatterlist *sg = req->sg; struct scatterlist *s; - unsigned int pending = req->num_pending_sgs; + unsigned int num_queued = req->num_queued_sgs; unsigned int i; int ret = 0; - for_each_sg(sg, s, pending, i) { + for_each_sg(sg, s, num_queued, i) { trb = &dep->trb_pool[dep->trb_dequeue]; req->sg = sg_next(s); - req->num_pending_sgs--; + req->num_queued_sgs--; ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb, event, status, true); @@ -2870,7 +2871,7 @@ static int dwc3_gadget_ep_reclaim_trb_li static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) { - return req->num_pending_sgs == 0; + return req->num_pending_sgs == 0 && req->num_queued_sgs == 0; } static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, @@ -2879,7 +2880,7 @@ static int dwc3_gadget_ep_cleanup_comple { int ret; - if (req->num_pending_sgs) + if (req->request.num_mapped_sgs) ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, status); else