Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2700105pxj; Mon, 31 May 2021 08:33:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwaJH7Y/FM7F9z2AnVVwCJOgIClVWbxj1J3/qOCo/cw1EO59T5FNnjRYOESY0W243y4l7Fw X-Received: by 2002:a17:906:5391:: with SMTP id g17mr10974654ejo.461.1622475221692; Mon, 31 May 2021 08:33:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622475221; cv=none; d=google.com; s=arc-20160816; b=Le+XD5DrwFZLUDQidxhyEn48T8fl4j3avIP8nY0QhAi8JfAcHXDtOVzkY+oGteoGyW PfSB6tMIFruUJosv+mpXnvtJ6T30uro7MA08a6cS5R/SoRNwAN4qo6xLKiXwOmOL+4zM GgoCMkRYulw1C89JYhatdCcTRnvzODpPwBypK1JRjG7DZJA19Q+dr1A1WUXFb8PZFFcP 7tqhV5FiWdWvXrp6qeJULhaymD2WV7MS96X0NGbW96Ma9t4FDccy0gEpAb9Y/+elMJNx YnhjecaR/ugFC6VL0qSYf+gF0/cSdkSEUjV3Z5Dz73RGTN/3jRzYnFx4oeBlwWWe46Lb T6Kg== 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=Q6BPWqrVOrP6jRtVlFUlELADHQZAPsp6TAl1TvYbqDM=; b=s1NmC1u5Y1/MPqr6PySRD8lWKEpj5wMlbjEv7V2q2ip50tHU5ZjJ6ePh+cMVUjq7mO U8bqvOln/I2a+wFccZ4tqKLLoqgNyO29eCZ1OcfL7nb5RvunLD5ClnyJgDEa2PAeKVfX XDgd3urPeS/5JiNn78iXxDbMGrQKDSu8SQDic1CbDdus8KNoRNFrVc19nCln/rBMk1wS uBJmtvm0W9pmNIRC5nqIr1EsWiBGscLSElCK9xYRBMqOnFw67yYaJF2l9+4GUeArV32Z 4l8SDmMGtvQJFApGvOttoIrxwwBZCNYeijG7NRFVlq7rLJI0zn/kP8zUbDi1kJt7YBBB a2MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=g53RDwfx; 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 n15si12643114edt.229.2021.05.31.08.33.14; Mon, 31 May 2021 08:33:41 -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=g53RDwfx; 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 S234508AbhEaPbi (ORCPT + 99 others); Mon, 31 May 2021 11:31:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:43228 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232971AbhEaORg (ORCPT ); Mon, 31 May 2021 10:17:36 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B1AD8613E4; Mon, 31 May 2021 13:43:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622468619; bh=aEFSvsSsem7Qli/Aw9qGJLdRqG0zPZdwFN513yQQtiY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g53RDwfxhemh0J2JkgSK6NFmY/rouRaXJ3vxBjkz/E1BTX4MSVcxw7YaZI/BN9/AD MS6Utqi/F8Skc6/AW5s8VyqerAjxw/ilgJnKwDVBQ2osbjXf4j6kOtSe7d9xJoAQUG d5sAbPrU7BqHWw0FUXsNZrbdxESW19fMR50eoLLo= 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.4 061/177] usb: dwc3: gadget: Properly track pending and queued SG Date: Mon, 31 May 2021 15:13:38 +0200 Message-Id: <20210531130650.013959872@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130647.887605866@linuxfoundation.org> References: <20210531130647.887605866@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 @@ -1162,6 +1162,7 @@ static void dwc3_prepare_one_trb_sg(stru 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 @@ -1169,7 +1170,7 @@ static void dwc3_prepare_one_trb_sg(stru * 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; } @@ -2602,15 +2603,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); @@ -2633,7 +2634,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, @@ -2642,7 +2643,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