Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp1803530rdb; Wed, 31 Jan 2024 09:27:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IEHouJpNKKoJfLBwkBBxMZLiqPDEUgFeN17swhcuCsIBV1gxhyExogHNrEHg/2gjsMB8nUQ X-Received: by 2002:a17:902:e807:b0:1d8:fb06:b8f2 with SMTP id u7-20020a170902e80700b001d8fb06b8f2mr2021257plg.0.1706722039460; Wed, 31 Jan 2024 09:27:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706722039; cv=pass; d=google.com; s=arc-20160816; b=KfmEtUUufqCzjJ11qAEnrjbmuWNBg8qx1mShrRGeNO4XN0L5t9+6SAiSKozWZyIYxu T8GSKmUlfuP5Fc0IScKAHQNAxRbwab5NfOuw9oLjPZyx476JavvapHyEiil1/j66L6o8 o2J+pGsw4Sj3B9m98e4ow5OGP90EXiuaxn/aCUUAQdmKbLp3+XxuDQqRmnnZOYXT2+JX KsAy31euzvDQmoZJRZZJ2Y/kQcedwyjoqXoaOh3c5vPwo8CWDg4tpEeMDy4XBWp8lBi3 Gnvq3bbXKczfc0O89cPSoYZcB+sLyBSxl0SF6CO48ya0dq09+I9NOlAJXdN/ItKmJDR9 eOiw== 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=Y3mGoL3zDkzMLFYV1uUnwg42JaoVUwZ52/NIbjZh/FM=; fh=6qBFakyIRvHlsktI7qe1muRWDVMwQFC49J3paSFBzW8=; b=L7U2Kwmd9X6M3gk8smHepuH5+u1CeZMrzhVsTwH82JDv+4jN8tZPY7QV9XRqOiQKK1 JnR/3dwCVvhOsbTqTox156NG7rgF+bHBrwzNOcWTRnpIRJ6JDYwROSpWTiIYVcjv4hq6 etpleBDk+BmuUf9Td9/X0c1EMXwOGbhkUCyOllWPMxS3AHiSiotipei2RHT9XebjGhpt oNWlnQ/dUSetYDXMdGFSLuXYl9JsC03Gqwd9pAINN8Bgk58orRCjpSz0vTOmv8tfG2+5 1ePKN1jCd/aGkKbvU9EYzYq6zevF9n3Mg2XB9sDKxIhdSUysKu9MTHtiSIsJ+36IQb4a KgpA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Zp5BTwBv; 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-46894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46894-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; AJvYcCWZ4Owb/ZnnvEfLkWiNNDx+b4eg03Iw31IGniTYriqrC3dYhVSN6Ge3t98aw5ii5Xy2shgseziT6Wq0OGE+Iyb2jFjhgNh3+lxYmeTupA== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w12-20020a170902d10c00b001d8a923f0ccsi9020976plw.250.2024.01.31.09.27.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 09:27:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Zp5BTwBv; 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-46894-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46894-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 72F1F2929E0 for ; Wed, 31 Jan 2024 17:19:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D97A412F597; Wed, 31 Jan 2024 17:18:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Zp5BTwBv" Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (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 F2A2712C553 for ; Wed, 31 Jan 2024 17:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706721530; cv=none; b=NnkwmpLHhlKZcpIuMffHvI2H1BgsLmThRXAS348SCoi1MxPbV1AkYXh1qofdzkfbYwVicmgWL3wdNBT/jW2Zi0v7IVy8QKinHa+SExLFck24Q33nrhqJgxFaRODGcltS1+M0f0wHZFCh4aS92m839Ztmm2BIETKuPnsJHWojaE0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706721530; c=relaxed/simple; bh=EUatuG24str+B+qoqT7fZBnQVgvmuUlT/BAN3+vvE0E=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=ky6mud5HS59Vo8MTc4pnLHzO0ku7IanXV3uOlbBgTIlYND2ml8+XAicqfqU5hGeaLMMnQn/aqVYmHw1SznVNzu/9oPEgRKy533iqko8OCVBn4mdjzMsgxhviVw/UFyHAGfb6ucFyQaIHiBjPnYLe/euhbNwzhcYSxI0JxN/5xfs= 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=Zp5BTwBv; arc=none smtp.client-ip=209.85.208.48 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-f48.google.com with SMTP id 4fb4d7f45d1cf-55f85a2a43fso13107a12.1 for ; Wed, 31 Jan 2024 09:18:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706721527; x=1707326327; 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=Y3mGoL3zDkzMLFYV1uUnwg42JaoVUwZ52/NIbjZh/FM=; b=Zp5BTwBvsrD766Afla41NsTjXdJrX6X2uNHTiL7X/FbKOkYYjAXbxfkxnwyA0R594R RLsnI0OVebQKMmhjG26jFoRLo0umThCXSW2CXXMSk3FgcFhDqebZcCPot5Dx4ZYwTsuK nM6C6sqoQ6mzQMKEbCpaTXXFbyy+rwaic3BcoXbDvGEC7zVYES1LZwe2OldzrYvHTWJX shR2aDXAyWo9gVAeEY02DCgbr6v8S4dHWZrJdvZ8XeqIwpDTinyj3c+H9Y5tTkpPkQDK yRrDBLHmjqhq1UIk7Qd2MFG6vE7f7zC3idHOaqant17VTYy3ON1eTcsAr1aHn8nfjhNb 1IeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706721527; x=1707326327; 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=Y3mGoL3zDkzMLFYV1uUnwg42JaoVUwZ52/NIbjZh/FM=; b=S/B+B2CeQsO7ZaD1QeAr0zRJk+FbrTF+YONh/KWZlKJnGBY3mKHtOQHhlvML3YSht4 lSyqgUO4JyuDM4HPZtkp28QuDuezNHKBMIwRvJr9WOwxOP2WQ426sELD5RTlQFEGhooy vGuf7czBZGwz7Fdb7TKA3bbltRV7jH44705tu1K28wZ68S5aHfdi03XbOgVEyMpMu2uK 5JJE40VUt2gEVh/BGJyaX7rHnsaC+fE9JJUZx/d50OTahsHQYgArS9DrkQlk2dcE7s+c aqRLKnpC9tmT4c4uYvOn0HEwd0qzJHqmhwYEBok9ZG1jx0RrsoVQ+inMMmxx2D4+348a YoWg== X-Gm-Message-State: AOJu0Yw3ebe6S1ULAfTO4QDM6NuuHUdGAdofcqxx1begzDAIgiBtXsAi kgREKaVgNAtrHdZ78yiukkhLAD/H5zFkvNGq8a9s/TLMTpNq8p73gvg7sWHPnu4P7oqR8W7OHbD 04eTrq0Z9n9DywoB2TRqfPhBBLVCPSu9gmf7A X-Received: by 2002:a05:6402:312f:b0:55d:2163:7ed1 with SMTP id dd15-20020a056402312f00b0055d21637ed1mr453402edb.1.1706721526917; Wed, 31 Jan 2024 09:18:46 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240131150332.1326523-1-quic_kriskura@quicinc.com> In-Reply-To: From: =?UTF-8?Q?Maciej_=C5=BBenczykowski?= Date: Wed, 31 Jan 2024 09:18:35 -0800 Message-ID: Subject: Re: [PATCH v2] 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jan 31, 2024 at 9:14=E2=80=AFAM Maciej =C5=BBenczykowski wrote: > > On Wed, Jan 31, 2024 at 9:03=E2=80=AFAM Maciej =C5=BBenczykowski wrote: > > > > On Wed, Jan 31, 2024 at 7:03=E2=80=AFAM 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 fo= r > > > any leftover bytes in SKB provided by u_ether and if there are any pe= nding > > > bytes, it treats them as a separate NTB and parses it. But in case th= e > > > 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 droppe= d. > > > > > > 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:= 1025 > > > [002] d..1 7828.532867: ncm_unwrap_ntb: K: ncm_unwrap_ntb nth: 17519= 99342 > > > [002] d..1 7828.532868: ncm_unwrap_ntb: K: ncm_unwrap_ntb seq: 0xce6= 7 > > > [002] d..1 7828.532868: ncm_unwrap_ntb: K: ncm_unwrap_ntb blk_len: 0= x400 > > > [002] d..1 7828.532868: ncm_unwrap_ntb: K: ncm_unwrap_ntb ndp_len: 0= x10 > > > [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: 17519= 99342 > > > [002] d..1 7828.557950: ncm_unwrap_ntb: K: ncm_unwrap_ntb blk_len: 0= x800 > > > > > > Lecroy shows one byte coming in extra confirming that the byte is com= ing > > > 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 dev= ices > > > that rely on ZLP as long as the wBlockLength is multiple of wMaxPacke= tSize. > > > To deal with such devices, it pads an extra 0 at end so the transfer = is no > > > longer multiple of wMaxPacketSize. > > > > > > Signed-off-by: Krishna Kurapati > > > --- > > > drivers/usb/gadget/function/f_ncm.c | 8 +++++++- > > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > > > diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget= /function/f_ncm.c > > > index ca5d5f564998..8c314dc98952 100644 > > > --- a/drivers/usb/gadget/function/f_ncm.c > > > +++ b/drivers/usb/gadget/function/f_ncm.c > > > @@ -1338,11 +1338,17 @@ 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) { > > > + > > > + if (to_process =3D=3D 1 && > > > + (block_len % 512 =3D=3D 0) && > > > + (*(unsigned char *)(ntb_ptr + block_len) =3D=3D 0x00)) { > > > > I'm unconvinced this (block_len % 512) works right... > > imagine you have 2 ntbs back to back (for example 400 + 624) that > > together add up to 1024, > > and then a padding null byte. > > I think block_len will be 624 then and not 1024. > > > > perhaps this actually needs to be: > > (ntp_ptr + block_len - skb->data) % 512 =3D=3D 0 > > > > The point is that AFAICT the multiple of 512/1024 that matters is in > > the size of the USB transfer, > > not the NTB block size itself. > > > > > + goto done; > > > + } else if (to_process > 0) { > > > ntb_ptr =3D (unsigned char *)(ntb_ptr + block_len); > > > > note that ntb_ptr moves forward by block_len here, > > so it's *not* always the start of the packet, so block_len is not > > necessarily the actual on the wire size. > > > > > goto parse_ntb; > > > } > > > > > > +done: > > > dev_consume_skb_any(skb); > > > > > > return 0; > > > -- > > > 2.34.1 > > > > > > > But it would perhaps be worth confirming in the MS driver source what > > exactly they're doing... > > (maybe they never even generate multiple ntbs per usb segment...) > > > > You may also want to mention in the commit message that 512 comes from > > USB2 block size, and also works for USB3 block size of 1024. > > Also since this is a fix, it should probably have a Fixes tag > (possibly on some original sha1 that added the driver, since I think > it's always been broken) or at least a commit title that more clearly > flags it as a 'fix' or cc stable... > > (something like 'usb: gadget: ncm: fix rare win11 packet discard') > > We definitely want this to hit LTS... One more thing: the 'goto done' and 'done' label are not needed - that's already the natural code flow... So the 'goto' could just be replaced with a comment or perhaps with --to_process.