Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp4782795rwj; Tue, 20 Dec 2022 15:08:04 -0800 (PST) X-Google-Smtp-Source: AMrXdXteHp+8gt9hCPRGgTnitZa/merggA1GfnPFKr6zZrklNAGKeKacP6jWiB+J9QjNZ/anktC9 X-Received: by 2002:a17:906:2514:b0:7c1:1b89:60ed with SMTP id i20-20020a170906251400b007c11b8960edmr3572072ejb.18.1671577684471; Tue, 20 Dec 2022 15:08:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671577684; cv=none; d=google.com; s=arc-20160816; b=nLRZJnMrT8HwLZNImGoXm3s0Ef3AJwK8TEPQRoPGOtnUkHQQCFwG939mTXdxdZYMiC 2YKpURd0nPHskAU2VX26Ei0veGUf37iTKIViyKqMgvYipr5JSGcu8bB+Ppu9WKU1+LOp uveQ1HgwvlnFPRP97tk1veWAhTJrMU/fumDq+GcrJE1yQscMl0RUWFLeYOXjCRMzu+F3 QqTOtE5q2DAaQm52z/0OZw8nqMmpgwo4sz+SgCTNW4g4EOBewFWU8GOdlVARARDC9Ayt 1SD+pFX1U0g8Qm/aAXGsmIlFVWWWhYpZTeJRFsedRKXfq6Mk4NBPTHYv+eIg8w5l3C2P DOdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:message-id:content-transfer-encoding :mime-version:subject:date:from:dkim-signature; bh=A+s91cNikbCmUympaj0zn/R/D4VCLupmyCFlgATkzu8=; b=wiLS3rUyvLFERiH/zDByBenHket7bzNSbgSdNG7h9nR4v3MiOVdOWJwngZhtzv9lxJ hV3aRGKhOwTTDyOYmP/5+0IzkDx8TlJWI9mBVq/UJbrc+1IlcDY//MVuxRSII79L5KuN shg4cSSZDD33guq1KF2A3c3tEKwma7DO1xS7Ad3laEED2lyW+Z5/SH4mfZqNT+4Qbzs7 ep0VbZ4qz9M3kThKgN/BTsxz8FCOilmw44AR0LlAXopvbZtOuz9AfxcdxWei+4el7p6f bGFd1VdMSRhgT4nDH+2viF3KvbM3pnxJLpqzJ1KDMSGGauMA2PDF4NIe9KZXmhbLcxEv 2GEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QPK3UgMP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o6-20020a170906974600b00787bacce740si231209ejy.537.2022.12.20.15.07.48; Tue, 20 Dec 2022 15:08:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=QPK3UgMP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229983AbiLTWwN (ORCPT + 68 others); Tue, 20 Dec 2022 17:52:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230130AbiLTWwL (ORCPT ); Tue, 20 Dec 2022 17:52:11 -0500 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E76021F2C8 for ; Tue, 20 Dec 2022 14:52:09 -0800 (PST) Received: by mail-ej1-x636.google.com with SMTP id tz12so32757077ejc.9 for ; Tue, 20 Dec 2022 14:52:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=A+s91cNikbCmUympaj0zn/R/D4VCLupmyCFlgATkzu8=; b=QPK3UgMPen4VNOV9F949K5vtIoCz025W+UMP6wGYFo3XfVnOQ5KYPXpNWzEDMApYOk +s0u+IENdFf6y6qjpG0GnMggKprb4dDqJCD3KTjjGO5bXkUUd556l2X3Q0PXirMnROd3 WeAWoAmVY0WhmAXDyQ+6vMsJBsDtrvTd8pe7s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=A+s91cNikbCmUympaj0zn/R/D4VCLupmyCFlgATkzu8=; b=bCcSmy/lnujrzhBeBdPNeFDTEGk8T5k7aF4/8RVvmdSwmK7jemIg/MWClVVBh5kfQk lfxI1khSHkz+OP1KrG58ttgnd1EkLJqAvENhnTd0CXit151asQCaHt9AeIQs/3qOUxIN 3XqKkcIYfaDwIuduVnhxKuGaWqmxVKHgFC9BGidwD4FqaEmj+P+gFZs4x0UJGYW/7k46 7iHYyxRYaJgQqWg0CUW7x+eq4EUj342OZZCocBHSxHfzTglG/cFwEenRGt9eBJXOFNs6 f83OTccuIIpliJC5DPHc8ymcekH42xmpVSpkYnR0j9tCSr3i1PaUm0hIBWeODWVMNzoF nY4Q== X-Gm-Message-State: AFqh2kpTMqjqEpziQK/EFJdaQ39nUkvlEakbAR1y6VE63yuIjaoiAbfT 2zXzLCPs2me4mBZmpIgux3MN4w== X-Received: by 2002:a17:906:8447:b0:7c8:9f04:ae7e with SMTP id e7-20020a170906844700b007c89f04ae7emr3038732ejy.22.1671576728539; Tue, 20 Dec 2022 14:52:08 -0800 (PST) Received: from alco.roam.corp.google.com (80.71.134.83.ipv4.parknet.dk. [80.71.134.83]) by smtp.gmail.com with ESMTPSA id r18-20020a17090609d200b007be301a1d51sm6176494eje.211.2022.12.20.14.52.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Dec 2022 14:52:08 -0800 (PST) From: Ricardo Ribalda Date: Tue, 20 Dec 2022 23:52:03 +0100 Subject: [PATCH] USB: Improve usb_fill_* documentation MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20221220-usb-dmadoc-v1-0-28386d2eb6cd@chromium.org> To: Greg Kroah-Hartman , Laurent Pinchart , Christoph Hellwig , Alan Stern Cc: Ricardo Ribalda , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=4666; i=ribalda@chromium.org; h=from:subject:message-id; bh=Fm1wjAu2tpXMJ7r7AJYyOC60wItutOtoAsZzXKfKUUs=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjojyVwwQ7gOgmK3EGIWA2mRmmAwR2rS2U5MvjZpXe iLm0JZeJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY6I8lQAKCRDRN9E+zzrEiBLnD/ 4yqHU8rEFkzt2+2DnfLPlhRQwCKPk8IWziMMKMbpfbFksrpnICQWuH30JI7y5wwnMxFXB6vxqjQMHN Dw+poGLqdFASQx+FCs0j3QxAJ1lA4wfPH7iZxLFVXq1N+JwRJGX/X5WzdJ7l+tDLUEtar4rk9vusRq lu6bbV8DSGLvNr7F9drSGbkXD+VJpFI4uZFUB6uBwZWLi8Vjgos5GHBLEU0OvBlk8ugWdf+44jWnq1 lJseR3fJHQq/oYB8T0/SzDHBewwRThuEPR3TQ10N3UmEeJE8GnWWNUspRkDPn4TjY/FncqmyDPcyeh uXAb+wvfrZxdh7p/aVVsMXfW3ImiWHAa4okbJ86tl9YOPVwtk4Xmw9ROMTnOMT4r/rZ86fM26KGje3 M3fI/Ra/P64QUdQQ/h+oqO7iloVc0aFCDdnBGMeIpDjk09k05pCGApRdGPkWYpz9jRQ5Nw9QB9NYt+ +jStOA38CRk1thcouAYkm/YwkAeXnQ9Z4a0Be8e2A43Thzij+WtXbkTc8yI3uLSSxs/Lc37eDdbcE6 npvQ7PIZCBJVs+WUoXLS2CUi9jmr0zOmLi1/gZqeRn7C3JnMu2P2R/4EKJRxUDOOurjlD2rhjbZp5B kdKWNFT0vDNvdYjN8pC3gmDctGommf4yW4qibvVGnwFezJGf+9pik87AVzsA== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Make the developer aware of the requirements of transfer buffer. The buffer must be DMAble, if the developer uses an invalid buffer, data corruption might happen. Signed-off-by: Ricardo Ribalda --- USB: Improve usb_fill_* documentation After trying to "cleanup" the uvc code, I was patiently explained about the requirements of the urb transfer buffers. Lets make this explicit, so other developers do not make the same mistake. To: Alan Stern To: Christoph Hellwig To: Laurent Pinchart To: Greg Kroah-Hartman Cc: linux-usb@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- include/linux/usb.h | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/include/linux/usb.h b/include/linux/usb.h index 7d5325d47c45..033ca69b563d 100644 --- a/include/linux/usb.h +++ b/include/linux/usb.h @@ -1627,13 +1627,20 @@ struct urb { * @dev: pointer to the struct usb_device for this urb. * @pipe: the endpoint pipe * @setup_packet: pointer to the setup_packet buffer - * @transfer_buffer: pointer to the transfer buffer + * @transfer_buffer: pointer to the transfer buffer. Must be suitable for DMA. * @buffer_length: length of the transfer buffer * @complete_fn: pointer to the usb_complete_t function * @context: what to set the urb context to. * * Initializes a control urb with the proper information needed to submit * it to a device. + * + * The transfer buffer might be filled via DMA. The simplest way to get + * a buffer that can be DMAed to, is allocatiing it via kmalloc() or + * equivalent, even for very small buffers. If transfer_buffer is embedded + * in a bigger structure, there is a risk that the previous and following + * fields are left in a corrupted state by the DMA engine, if the platform + * is not cache coherent. */ static inline void usb_fill_control_urb(struct urb *urb, struct usb_device *dev, @@ -1658,13 +1665,20 @@ static inline void usb_fill_control_urb(struct urb *urb, * @urb: pointer to the urb to initialize. * @dev: pointer to the struct usb_device for this urb. * @pipe: the endpoint pipe - * @transfer_buffer: pointer to the transfer buffer + * @transfer_buffer: pointer to the transfer buffer. Must be suitable for DMA. * @buffer_length: length of the transfer buffer * @complete_fn: pointer to the usb_complete_t function * @context: what to set the urb context to. * * Initializes a bulk urb with the proper information needed to submit it * to a device. + * + * The transfer buffer might be filled via DMA. The simplest way to get + * a buffer that can be DMAed to, is allocatiing it via kmalloc() or + * equivalent, even for very small buffers. If transfer_buffer is embedded + * in a bigger structure, there is a risk that the previous and following + * fields are left in a corrupted state by the DMA engine, if the platform + * is not cache coherent. */ static inline void usb_fill_bulk_urb(struct urb *urb, struct usb_device *dev, @@ -1687,7 +1701,7 @@ static inline void usb_fill_bulk_urb(struct urb *urb, * @urb: pointer to the urb to initialize. * @dev: pointer to the struct usb_device for this urb. * @pipe: the endpoint pipe - * @transfer_buffer: pointer to the transfer buffer + * @transfer_buffer: pointer to the transfer buffer. Must be suitable for DMA. * @buffer_length: length of the transfer buffer * @complete_fn: pointer to the usb_complete_t function * @context: what to set the urb context to. @@ -1697,6 +1711,13 @@ static inline void usb_fill_bulk_urb(struct urb *urb, * Initializes a interrupt urb with the proper information needed to submit * it to a device. * + * The transfer buffer might be filled via DMA. The simplest way to get + * a buffer that can be DMAed to, is allocatiing it via kmalloc() or + * equivalent, even for very small buffers. If transfer_buffer is embedded + * in a bigger structure, there is a risk that the previous and following + * fields are left in a corrupted state by the DMA engine, if the platform + * is not cache coherent. + * * Note that High Speed and SuperSpeed(+) interrupt endpoints use a logarithmic * encoding of the endpoint interval, and express polling intervals in * microframes (eight per millisecond) rather than in frames (one per --- base-commit: b6bb9676f2165d518b35ba3bea5f1fcfc0d969bf change-id: 20221220-usb-dmadoc-29384acebd48 Best regards, -- Ricardo Ribalda