Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2656812rdb; Mon, 5 Feb 2024 13:22:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPtXJkPChZpyQ6f2w9UwauC0feF6dilHFf7dx1Os7vIdX/5TGzsah/DtShiw9DXzxVAqBj X-Received: by 2002:a05:6e02:1ba2:b0:363:abc0:4faf with SMTP id n2-20020a056e021ba200b00363abc04fafmr1262196ili.7.1707168145291; Mon, 05 Feb 2024 13:22:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707168145; cv=pass; d=google.com; s=arc-20160816; b=X04pvcw5tLV5f4MLzqIkgsIHIH2MD8WSL/YGpq8ARabZOzHlxjNBuilZrsn6uIxR0F JsGbshFMCZkD8VGF9coF69Sa7tGyZwy4iVwTiyndI+JDQsf+I3BmGU1wdiMTVxzJhqOX Uxyi2GxV+5huwDlO0DRQw2E0laclP+dGNPt3sf2yVzzte5SOrZWNG1AlRes67MQOg4Z6 ao3l6ruRwZGsmRCx/TgefbNhj0nsKljPxcR92os6ARyjSccKdlN860zKAzlkiRfReNx/ DFK+B/3ZMQPLlgCuciGalyLFcmcZN6oM7R3mGHGpvry+c+DWo0Yzl6X7wsDFEgqpL6Ah jobg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=jDn/H+533WiSQe64x85tBjr0VQMs7P8+fBEaYS85mkI=; fh=zK/1+zbJ1AN6J423DHxMJbIQ+Ju935v9M2WrvrVWN1k=; b=sJ0TJY1c16YneMF1oo/UUod2aS9Y11bVp2QIygijVIu4YebmOn26a+w36MQZpk4sBZ CFTiaBZ2zeJQMETMRcjY5Rb5kga2gTdQZMw2GOc04WRmiSumLGhJQQ5XJX90LTT1yUQn oXCKGBNm44w8dYFIrUrLzlH8sQEl/oVsBpeD2+csng16/2T2hq6XNiX6ID0/QfRWlgZ4 QGqOi6RFqJFINMyBrCy9+lnyTeGn12xH1azf463M4v0YAtLzQ3pna0c1SJxZ2nJLwQ5Q UoXac5RbpmQgRX22znHk0YTGml+g7Wmv8IS274Yz79L4OrE8XWIhxfAykU9Ih+9ye/Xf BEJA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=HMXKVwYD; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-53821-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53821-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCWuqe5/bWWNdzYH67HHuIsePqS8lu+ShX7M8gCzgw0mVHZWIeRVc8b/D3kPx3xj0YWyKAuTXi+kl8qLfjfJq89fhuhttYueDOd2t2rPzA== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 20-20020a631354000000b005ce0b38a6dbsi440382pgt.580.2024.02.05.13.22.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 13:22:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-53821-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=HMXKVwYD; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-53821-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-53821-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 733E828B61C for ; Mon, 5 Feb 2024 21:19:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B48AA5D479; Mon, 5 Feb 2024 19:33:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HMXKVwYD" Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F01605C90C for ; Mon, 5 Feb 2024 19:33:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707161612; cv=none; b=Rl6CNqyMiAaDUlbc+NWfWIHwYsIt7Pf3rjY070CrLquFZ0AEW7+ZeuUlWuk9Uk5SXjRKMV/jbJINGEMhaNAFbG1v51iq/gog//4VeKX7qLccp6XQIX3JTR/wd981bKWA5ut7Rh4mBYtJflMlVfSx/FnGGkmpjeiOTkcbKjsVoY8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707161612; c=relaxed/simple; bh=S8VBDrPY3rWixgeirT65Bi+aQAfk/nawdnTwZWCHHu4=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=lBGR9vsvKPja2y1OPogxKe4heaVpJ3sxfw/UJW4lKtLSlDQQM9CbYuyBzu8bJ+fXWTfTDPVq/JBtW+7Hny5yYrWtu9IKwHe3LA7Wm05KRCIyZg8R/cE28b0DUKEINJyN6Gqdvv3Nqx25pYwzY5FtNBikAH4sZzGfCb53uVrhmeM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HMXKVwYD; arc=none smtp.client-ip=209.85.208.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-56012eeb755so3877a12.0 for ; Mon, 05 Feb 2024 11:33:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707161609; x=1707766409; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=jDn/H+533WiSQe64x85tBjr0VQMs7P8+fBEaYS85mkI=; b=HMXKVwYD/vSIYBdvMOzx2mxitL9WuA11qEfuvzgK2Rt6d3druHuRF6lCC/23e3sM90 Xm0LSBh8xrfJyPc9zNBbhPgfgWhWhh6NHbJrvIHBY14tmTG4u/dHNCLKdcldyyT6CyKg YcN8ogbGEAgKvbs3a8kXYM+8eW0zf9ac/imFcj/2Rg0KLiHPmyDwHKgtB8AibWzyZvFM fjttwOjZOJAnQMJ2+cJIRSFSEDHkvjH2sDF+L6ShxtRMwMFaqSc05OnPvAuqrO/q30/O THHVz0NuGzfmnwA6tnddVTZ+ZIiCiHq60gIszUAbB8DjMaU6FoIlejXPZSDy73h0l2Af VC3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707161609; x=1707766409; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jDn/H+533WiSQe64x85tBjr0VQMs7P8+fBEaYS85mkI=; b=oUESjXn1+KAGM/j1ZvcCEneSgiBm3HolWVJNLfLrWPwUEILlun8RHHKY5IqnGa/PsP RjIHaG9PSiRNlnKB0nD2HKleudzDV69wvh3j/OML+xnzpsNwrD4uVCzicudoW1va/Iqo WCNNCzoYNvVMhYb3mEDbNijAFd+XT6Z+RcNiVatZeEyFOBxXjZT2jmY4v7N4YDXAgZIM VAN7is4lkEZf6KHd4hAVkh8NrVsW3LkW5/DjqAVdyc4nRy/Qm6Dpj24lCirdh3Y0UgGE VkPqUotmrMdncOAivKISYqJJXii2IwPtxsxJQYKDgwg9C/7PddAIENFk1/Pzsd9nI0DT 1rYA== X-Gm-Message-State: AOJu0YwU5ewGbS/LJoDuXsffBPANnt8AtdVV/3g5Ph+W1zV5/CPhmPz8 WVOzvXtGVW37eRhpgc5sMuAQzvijYnwNS/uep6KNQuU/wAQlKKKE8WKOnoAHPI6z1gMamm05axn O5rdZV9eJYfno17ECmMvdf4rmRcWgWdbJjdV1 X-Received: by 2002:a50:f60b:0:b0:55f:9918:dadd with SMTP id c11-20020a50f60b000000b0055f9918daddmr44292edn.2.1707161608933; Mon, 05 Feb 2024 11:33:28 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240205074650.200304-1-quic_kriskura@quicinc.com> In-Reply-To: <20240205074650.200304-1-quic_kriskura@quicinc.com> From: =?UTF-8?Q?Maciej_=C5=BBenczykowski?= Date: Mon, 5 Feb 2024 11:33:17 -0800 Message-ID: Subject: Re: [PATCH v3] usb: gadget: ncm: Avoid dropping datagrams of properly parsed NTBs To: Krishna Kurapati Cc: Greg Kroah-Hartman , Hardik Gajjar , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, quic_ppratap@quicinc.com, quic_wcheng@quicinc.com, quic_jackp@quicinc.com, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Feb 4, 2024 at 11:47=E2=80=AFPM Krishna Kurapati wrote: > > It is observed sometimes when tethering is used over NCM with Windows 11 > as host, at some instances, the gadget_giveback has one byte appended at > the end of a proper NTB. When the NTB is parsed, unwrap call looks for > any leftover bytes in SKB provided by u_ether and if there are any pendin= g > bytes, it treats them as a separate NTB and parses it. But in case the > second NTB (as per unwrap call) is faulty/corrupt, all the datagrams that > were parsed properly in the first NTB and saved in rx_list are dropped. > > Adding a few custom traces showed the following: > [002] d..1 7828.532866: dwc3_gadget_giveback: ep1out: > req 000000003868811a length 1025/16384 zsI =3D=3D> 0 > [002] d..1 7828.532867: ncm_unwrap_ntb: K: ncm_unwrap_ntb toprocess: 102= 5 > [002] d..1 7828.532867: ncm_unwrap_ntb: K: ncm_unwrap_ntb nth: 175199934= 2 > [002] d..1 7828.532868: ncm_unwrap_ntb: K: ncm_unwrap_ntb seq: 0xce67 > [002] d..1 7828.532868: ncm_unwrap_ntb: K: ncm_unwrap_ntb blk_len: 0x400 > [002] d..1 7828.532868: ncm_unwrap_ntb: K: ncm_unwrap_ntb ndp_len: 0x10 > [002] d..1 7828.532869: ncm_unwrap_ntb: K: Parsed NTB with 1 frames > > In this case, the giveback is of 1025 bytes and block length is 1024. > The rest 1 byte (which is 0x00) won't be parsed resulting in drop of > all datagrams in rx_list. > > Same is case with packets of size 2048: > [002] d..1 7828.557948: dwc3_gadget_giveback: ep1out: > req 0000000011dfd96e length 2049/16384 zsI =3D=3D> 0 > [002] d..1 7828.557949: ncm_unwrap_ntb: K: ncm_unwrap_ntb nth: 175199934= 2 > [002] d..1 7828.557950: ncm_unwrap_ntb: K: ncm_unwrap_ntb blk_len: 0x800 > > Lecroy shows one byte coming in extra confirming that the byte is coming > in from PC: > > Transfer 2959 - Bytes Transferred(1025) Timestamp((18.524 843 590) > - Transaction 8391 - Data(1025 bytes) Timestamp(18.524 843 590) > --- Packet 4063861 > Data(1024 bytes) > Duration(2.117us) Idle(14.700ns) Timestamp(18.524 843 590) > --- Packet 4063863 > Data(1 byte) > Duration(66.160ns) Time(282.000ns) Timestamp(18.524 845 722) > > According to Windows driver, no ZLP is needed if wBlockLength is non-zero= , > because the non-zero wBlockLength has already told the function side the > size of transfer to be expected. However, there are in-market NCM devices > that rely on ZLP as long as the wBlockLength is multiple of wMaxPacketSiz= e. > To deal with such devices, it pads an extra 0 at end so the transfer is n= o > longer multiple of wMaxPacketSize. > > Cc: > Fixes: 9f6ce4240a2b ("usb: gadget: f_ncm.c added") > Signed-off-by: Krishna Kurapati > --- > Link to v2: > https://lore.kernel.org/all/20240131150332.1326523-1-quic_kriskura@quicin= c.com/ > > Changes in v2: > Added check to see if the padded byte is 0x00. > > Changes in v3: > Removed wMaxPacketSize check from v2. > > drivers/usb/gadget/function/f_ncm.c | 10 +++++++++- > 1 file changed, 9 insertions(+), 1 deletion(-) > > diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/fun= ction/f_ncm.c > index ca5d5f564998..e2a059cfda2c 100644 > --- a/drivers/usb/gadget/function/f_ncm.c > +++ b/drivers/usb/gadget/function/f_ncm.c > @@ -1338,7 +1338,15 @@ static int ncm_unwrap_ntb(struct gether *port, > "Parsed NTB with %d frames\n", dgram_counter); > > to_process -=3D block_len; > - if (to_process !=3D 0) { > + > + /* > + * Windows NCM driver avoids USB ZLPs by adding a 1-byte > + * zero pad as needed. > + */ > + if (to_process =3D=3D 1 && > + (*(unsigned char *)(ntb_ptr + block_len) =3D=3D 0x00)) { > + to_process--; > + } else if (to_process > 0) { > ntb_ptr =3D (unsigned char *)(ntb_ptr + block_len); > goto parse_ntb; > } > -- > 2.34.1 > Reviewed-by: Maciej =C5=BBenczykowski Let's get this fix out. Greg, there's further code cleanups (here and elsewhere) I'll send once this is merged. I don't want to annoy Krishna further ;-)