Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp252352ybf; Thu, 27 Feb 2020 20:28:45 -0800 (PST) X-Google-Smtp-Source: APXvYqwQJ/fA196r+P0+MTZy0vQaWpOR0GXmVbpbUaZMKH6Jx9uBW8i/jti4FYysq6SGr/XKxOQ3 X-Received: by 2002:a54:4895:: with SMTP id r21mr1823382oic.107.1582864125228; Thu, 27 Feb 2020 20:28:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582864125; cv=none; d=google.com; s=arc-20160816; b=Th89jQyiAfONHm5QdY3kbaP5fh68uO4ElpzvoXWo2S38Auc1fS//IemJyOwNrraQWM +TVKwK4K6BK7knDP9GZ0J4iYKkOBy7h0/9qRazfHwL2BEFjz5HuMMmdBxRanUEtEzwEG QEMucXQ0m7qOQVe/4BVPQHp3GilbU4EvGP8zyV7hKbgO+hlom6zXvZS8IrLXEiLYJtWW gsguZkY+TKbiEu3C/uB2y50wcB+Pwp6RqdFqQV28N0aRF8w6GanmErXxS8J3QWIdwvEP eWTm4Z7M5zMlXfpVLBD5zOfso+vH9V8uB2OtwD3gCRWYnhNVe4JztsX9FHICrmFMZ51S rFSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:references:cc:to:from:subject:dkim-signature; bh=xB59DL08w8Srn0sPbs6mHd2xqJxzIRIo8yXpOuZzGy0=; b=dOpFUXeZsxqt0iTel6nGU5VYSQTW4kAewTjcE9xHyZLYe3P7OL8jtU82UlAj04fRMq r64+fooxh1pl0EMFFhfDHk6CikYZRYrhCnxNhLO3efKp0XY6mMEeRvbtpY2LJiEXHFCP BiYTIDdMdSkXfknYtVG/faBDgsPQKZGdUhPZzdyQo7oORCruqvWD2r7y4Y1tsJAlFiJb zu0RG0kdNEgcawi9MTI9U+95GjLB3SshM4e9vcDnZAJBIGhR9sJneeDSAxGb1Q/SiHzs 6TBH/S5My0wco0D9l+TG9enIXwGAskR34JODZCqOxe5cdmn0vLESwEzaLYTCS28M5PBC v2Mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=XQY5q0xM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 4si1127319oiy.72.2020.02.27.20.28.32; Thu, 27 Feb 2020 20:28:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=XQY5q0xM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730872AbgB1E2V (ORCPT + 99 others); Thu, 27 Feb 2020 23:28:21 -0500 Received: from mail-pg1-f195.google.com ([209.85.215.195]:40154 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730802AbgB1E2U (ORCPT ); Thu, 27 Feb 2020 23:28:20 -0500 Received: by mail-pg1-f195.google.com with SMTP id t24so824084pgj.7; Thu, 27 Feb 2020 20:28:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:from:to:cc:references:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=xB59DL08w8Srn0sPbs6mHd2xqJxzIRIo8yXpOuZzGy0=; b=XQY5q0xMc2Sfjt8wYX1Ku7gaANoX7cLqSv65XNIbjaBc+KmDfoIJvXQTmx9XXYAo4Z dJ6JjfaYOZLxap/o+S/yEjeYLnnO53OfS32QYyHfAUl4ui/QNTjqa7DZfJuc9jfzOtsu PWmT5Wf85fCC80RCHn1w9ynJUhYWyUY7wbByYIaxrYtDtRxUQOwKQd0gNRyxS7wOb4r8 gBeIFDlmt7M1XfWgV9iSZ2ruyktuWnEaQvBfwy1/TlbjeEJkkKQ5kvMySbt13cjjQg93 AtXISitkbN/hfc07tSCMmoIwcrAr4Q9lgc4yuU8DTJv6vQTu+UwgCMFVR7HKF78+Ka7X N/bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:from:to:cc:references:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=xB59DL08w8Srn0sPbs6mHd2xqJxzIRIo8yXpOuZzGy0=; b=cIV9ovPfT8HAMxOHDQvT/uij8v9AG83gDZ8CceoPSG5haPQE9CUBpvkI7FFU16mFG1 cYtK8eBWZJmf6MXtcmm4Vms2256rwK1Kl32YCsdFFBFyGO8asvuSDAlTA/oLWbZdQwkj IfmZyvOeFJbOxrGAtXJ5sA0a1t1/RE/4JpQB6Cl3+LN5595hfkGWUkbnDohU/jkHZhkP MX+3zvMqtT8bJZxY1EfFC8SUyK2IvS3r+/OI34ygVViUsPPNKkRm7ZrxuVk/B1Ayjca6 lrV8RziHv0fmQaIvngI6eq3+DMqIkpEV+DKSv0t7LgYT2YbSjJT1UdV1HvSsaFcT87KQ SEyA== X-Gm-Message-State: APjAAAXuJNHzRo+VTcRFOVAqkdLh6ME9ubabSXP43zg8bRcPTlrrg/Z0 H5WcLDznggIPCOlSR81q7tw= X-Received: by 2002:a62:1883:: with SMTP id 125mr2476481pfy.166.1582864099588; Thu, 27 Feb 2020 20:28:19 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id x6sm8756284pfi.83.2020.02.27.20.28.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Feb 2020 20:28:18 -0800 (PST) Subject: Re: [RFT PATCH 1/4] usb: dwc2: Simplify and fix DMA alignment code From: Guenter Roeck To: Doug Anderson Cc: Minas Harutyunyan , Greg Kroah-Hartman , =?UTF-8?B?QW50dGkgU2VwcMOkbMOk?= , Boris ARZUR , linux-usb@vger.kernel.org, LKML , Felipe Balbi , Stefan Wahren , Martin Schiller References: <20200226210414.28133-1-linux@roeck-us.net> <20200226210414.28133-2-linux@roeck-us.net> Autocrypt: addr=linux@roeck-us.net; keydata= xsFNBE6H1WcBEACu6jIcw5kZ5dGeJ7E7B2uweQR/4FGxH10/H1O1+ApmcQ9i87XdZQiB9cpN RYHA7RCEK2dh6dDccykQk3bC90xXMPg+O3R+C/SkwcnUak1UZaeK/SwQbq/t0tkMzYDRxfJ7 nyFiKxUehbNF3r9qlJgPqONwX5vJy4/GvDHdddSCxV41P/ejsZ8PykxyJs98UWhF54tGRWFl 7i1xvaDB9lN5WTLRKSO7wICuLiSz5WZHXMkyF4d+/O5ll7yz/o/JxK5vO/sduYDIlFTvBZDh gzaEtNf5tQjsjG4io8E0Yq0ViobLkS2RTNZT8ICq/Jmvl0SpbHRvYwa2DhNsK0YjHFQBB0FX IdhdUEzNefcNcYvqigJpdICoP2e4yJSyflHFO4dr0OrdnGLe1Zi/8Xo/2+M1dSSEt196rXaC kwu2KgIgmkRBb3cp2vIBBIIowU8W3qC1+w+RdMUrZxKGWJ3juwcgveJlzMpMZNyM1jobSXZ0 VHGMNJ3MwXlrEFPXaYJgibcg6brM6wGfX/LBvc/haWw4yO24lT5eitm4UBdIy9pKkKmHHh7s jfZJkB5fWKVdoCv/omy6UyH6ykLOPFugl+hVL2Prf8xrXuZe1CMS7ID9Lc8FaL1ROIN/W8Vk BIsJMaWOhks//7d92Uf3EArDlDShwR2+D+AMon8NULuLBHiEUQARAQABzTJHdWVudGVyIFJv ZWNrIChMaW51eCBhY2Nv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V1TVbjiDI HP/aVnm6NC8of26P40Pg8EdAhajZnHHjA7FrJXsy3cyIGqvg9os4rNkUWmrCfLLsZDHD8FnU mDW4+i+XlNFUPUYMrIKi9joBhu18ssf5i5Q= Message-ID: Date: Thu, 27 Feb 2020 20:28:17 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/27/20 2:27 PM, Guenter Roeck wrote: > On 2/27/20 2:06 PM, Doug Anderson wrote: [ ... ] >>> - if (urb->num_sgs || urb->sg || >>> - urb->transfer_buffer_length == 0 || >>> + if (urb->num_sgs || urb->sg || urb->transfer_buffer_length == 0 || >>> + (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) || >>> !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1))) >> >> Maybe I'm misunderstanding things, but it feels like we need something >> more here. Specifically I'm worried about the fact when the transfer >> buffer is already aligned but the length is not a multiple of the >> endpoint's maximum transfer size. You need to handle that, right? >> AKA something like this (untested): >> >> /* Simple case of not having to allocate a bounce buffer */ >> if (urb->num_sgs || urb->sg || urb->transfer_buffer_length == 0 || >> (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) >> return 0; >> >> /* Can also avoid bounce buffer if alignment and size are good */ >> maxp = usb_endpoint_maxp(&ep->desc); >> if (maxp == urb->transfer_buffer_length && > > No, transfer_buffer_length would have to be a multiple of maxp. There > are many situations where roundup(transfer_buffer_length, maxp) != > transfer_buffer_length. I agree, this would be the prudent approach > (and it was my original implementation), but then it didn't seem to > cause trouble so far, and I was hesitant to add it in because it results > in creating temporary buffers for almost every receive operation. > I'd like to get some test feedback from Boris - if the current code > causes crashes with his use case, we'll know that it is needed. > Otherwise, we'll have to decide if the current approach (with fewer > copies) is worth the risk, or if we want to play save and always > copy if roundup(transfer_buffer_length, maxp) != transfer_buffer_length. > Thinking more about this, the situation is actually much worse: In Boris' testing, he found inbound transactions requested by usb storage code with a requested transfer size of 13 bytes ... with URB_NO_TRANSFER_DMA_MAP set. This means the requesting code has provided a DMA ready buffer, transfer_buffer isn't even used, and we can not reallocate it. In this situation we can just hope that the chip (and the connected USB device) don't send more data than requested. With that in mind, I think we should stick with the current scheme (ie only allocate a new buffer if the provided buffer is unaligned) unless Boris comes back and tells us that it doesn't work. Thanks, Guenter