From nobody Sun Feb 8 05:29:04 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88B0429E116 for ; Mon, 5 Jan 2026 08:23:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767601388; cv=none; b=ukT3xvl1q1wREXbUxjOrGe0Xz/4a7qbAJHRAE9dOc9mUYHzSS4l6VF4x26D0/Xl6w5WeMIAc+0Pwe0AnKS9KnxX6Tx296hrjx2Hi8/8tH09EFOgSCwm5SjUn9gMqhsaqw1VP3QEqEsMoecDen/yEAGhaujMjktoJHHLxi4mrs4s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767601388; c=relaxed/simple; bh=prNYGYHAMpr2WjcHI5N+V/0OjaLMaL48hqD0tOu0jIE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ZQO8waxvG8kNCloxbk9BJRs8e9ZsSBcdf3tCs1WNAEZKIBZRxUdrWuR8gfQtdupzBUQLcKkkV931qzOK0jdvT5axIjIO/veZdffGypiF26nB5dutgr+8o4ZNXAZLmbd5DcV+irLSmPqhrLOkrn/lT4kD50aNduI3vdyoLSQl5Lw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=LS3uTdc+; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=LlTKn6Vt; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LS3uTdc+"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="LlTKn6Vt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767601384; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=vW7mLQEFNX/sa5+JF2UQBkqoBaqIc5/VpH7fDXfLlqs=; b=LS3uTdc+FsttqHOfOw/HO7GM9NxeIOzRBH2kw/DWe+ws+Rr11TKPF6fDzwZu9w9Hq9EHdJ vCfDbLVf2yHeZChraF7wHGi1fWqRTVGd5TwviBVIkc0jqMhe0VSYhy4thw9Amx8yDypRMH tP8s3tojCidvhpDNofTpDPxBCNxuAF0= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-363-XSGw0230PvGpGS9bzB-lXw-1; Mon, 05 Jan 2026 03:23:03 -0500 X-MC-Unique: XSGw0230PvGpGS9bzB-lXw-1 X-Mimecast-MFC-AGG-ID: XSGw0230PvGpGS9bzB-lXw_1767601382 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-43284edbbc8so4244378f8f.0 for ; Mon, 05 Jan 2026 00:23:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1767601382; x=1768206182; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=vW7mLQEFNX/sa5+JF2UQBkqoBaqIc5/VpH7fDXfLlqs=; b=LlTKn6Vt5VZSlWqvNo95rjH7QcjTqmevzhcFdafBlRMtrFsg393kNfHpLthMx0LnVV vNGY4I17PqR5/A1CYAvKuZUVDlnoAfWJ6svgNynzEFVm5+qdyMGprnko1uTM9RiNCUKN kF3u7GUAA5b8Gni6esWpiA/RBnDbdbZ5xN0w+G8IAu11M7uHdYjob5i81/H1T+JccJ0j qv5P03j7qTAyWBvd1jE7YK3jTOHSs3/jNRdOM7up8oT1SMJEl/1BY7vMBUDs4tZvsQwh AR3Tkh1LmBBr5VEQzaQkl7VBfvNUn0CJ3kQTSFprNCIEANiCKV0OhDtkGZcUrxuWh867 jLGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767601382; x=1768206182; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vW7mLQEFNX/sa5+JF2UQBkqoBaqIc5/VpH7fDXfLlqs=; b=aUYAI7qOCrS0p8K1CeeyTX1IBH/1WhcR5krXfnuSpUAp5awT+6Tw/1Qa2inf35WFQY rJm+L+xWFJdfXKNvB8HtlpK/cfGTqYJ8z/L+9YktiTllN7O+cuGCepET3XySczsRNhrT h6Yhir8WA1Ahl5Km4IvOQ/bzlwCe4v2nutkT6P1gQcJoo28N6LFd+2K1uv0ntAm1xmz6 bPRIJCGwhtgZjEHXaz60qqtyEnNJuFL0TV8hvjaht2zqxeRj/sviA1jn7dAfYkA/ckuH R7tPSfB48HNuYOBrh9+53QxC4zM2U8XXUNBi1Ms7WjrMJIW0dk0yHciuC0oER7ojLd3v ZBoQ== X-Gm-Message-State: AOJu0YyAu7trgjDTWVhYsfSFLU7y+gHxc8OxW9cS91bfp4WUXyoHqW4T xYwIealB8BH2w7AA9wN2eMaAP00UOF/KMZz2A+NzDdpCXUeyruwXgEA3nprknP8P3zlrgLmULeh 0Wnklbqf6Zai3H7dWoKAYRjlr36mDmMQFjG2IYb/inHYfb1ntUK09SXsNuxyZbQQYuqCRZO1qih 1ftXh/IK4/whU2RO99RboguARC0fzqdxYV5yXl0QIfkcc= X-Gm-Gg: AY/fxX65dWm32yNlhDopggtaVj0uATXf0pGMF9oSUL7S9dbyZrJ7P3XeAV8u+vBUR8u xdSjVINBrA1akoeZ3AmChVVZabOBSskCYkNMd2Ny0zsJ0xX6XyKIuypDq+0do114MZJff/cfq3Q x3dWARaHKRFT8XCOBIvSDkmIEaU6UMOd+GRths1iqUDo8/uYmd2PYgSXzQuaFmnZDj1uUFzL2fe qE0sP/HqPuWG3BjIbRgtkqHJTljCmWqii6pNfChuUCOiG8OfEiSPjKQvkqdT03HiNUUOI9rFfwn F4DSGLAyBLOuQEfiWcZIdBV44QAr0GXJnVBtOet+B8X+P9TxN3KdQ32T2m2NJvRzW9eRvp5nxyg L1diNfK+fcZiMszJOzjuuEacS3OE88dFsHA== X-Received: by 2002:a05:6000:2403:b0:431:35a:4a7d with SMTP id ffacd0b85a97d-4324e708fe3mr58459637f8f.58.1767601382097; Mon, 05 Jan 2026 00:23:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IF+toNYrDF1foh7qEGgsaPaQu5EoHgXBV6IY+Dg2iZDwh/L3E0Nw+HN1rJa40DHNfa5Ktydrg== X-Received: by 2002:a05:6000:2403:b0:431:35a:4a7d with SMTP id ffacd0b85a97d-4324e708fe3mr58459566f8f.58.1767601381336; Mon, 05 Jan 2026 00:23:01 -0800 (PST) Received: from redhat.com (IGLD-80-230-31-118.inter.net.il. [80.230.31.118]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4324eaa08efsm99942627f8f.29.2026.01.05.00.22.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jan 2026 00:23:00 -0800 (PST) Date: Mon, 5 Jan 2026 03:22:57 -0500 From: "Michael S. Tsirkin" To: linux-kernel@vger.kernel.org Cc: Cong Wang , Jonathan Corbet , Olivia Mackall , Herbert Xu , Jason Wang , Paolo Bonzini , Stefan Hajnoczi , Eugenio =?utf-8?B?UMOpcmV6?= , "James E.J. Bottomley" , "Martin K. Petersen" , Gerd Hoffmann , Xuan Zhuo , Marek Szyprowski , Robin Murphy , Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Petr Tesarik , Leon Romanovsky , Jason Gunthorpe , Bartosz Golaszewski , linux-doc@vger.kernel.org, linux-crypto@vger.kernel.org, virtualization@lists.linux.dev, linux-scsi@vger.kernel.org, iommu@lists.linux.dev, kvm@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v2 02/15] docs: dma-api: document __dma_from_device_group_begin()/end() Message-ID: <01ea88055ded4d70cac70ba557680fd5fa7d9ff5.1767601130.git.mst@redhat.com> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Document the __dma_from_device_group_begin()/end() annotations. Signed-off-by: Michael S. Tsirkin Acked-by: Marek Szyprowski Reviewed-by: Petr Tesarik --- Documentation/core-api/dma-api-howto.rst | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Documentation/core-api/dma-api-howto.rst b/Documentation/core-= api/dma-api-howto.rst index 96fce2a9aa90..e97743ab0f26 100644 --- a/Documentation/core-api/dma-api-howto.rst +++ b/Documentation/core-api/dma-api-howto.rst @@ -146,6 +146,58 @@ What about block I/O and networking buffers? The bloc= k I/O and networking subsystems make sure that the buffers they use are valid for you to DMA from/to. =20 +__dma_from_device_group_begin/end annotations +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +As explained previously, when a structure contains a DMA_FROM_DEVICE / +DMA_BIDIRECTIONAL buffer (device writes to memory) alongside fields that t= he +CPU writes to, cache line sharing between the DMA buffer and CPU-written f= ields +can cause data corruption on CPUs with DMA-incoherent caches. + +The ``__dma_from_device_group_begin(GROUP)/__dma_from_device_group_end(GRO= UP)`` +macros ensure proper alignment to prevent this:: + + struct my_device { + spinlock_t lock1; + __dma_from_device_group_begin(); + char dma_buffer1[16]; + char dma_buffer2[16]; + __dma_from_device_group_end(); + spinlock_t lock2; + }; + +To isolate a DMA buffer from adjacent fields, use +``__dma_from_device_group_begin(GROUP)`` before the first DMA buffer +field and ``__dma_from_device_group_end(GROUP)`` after the last DMA +buffer field (with the same GROUP name). This protects both the head +and tail of the buffer from cache line sharing. + +The GROUP parameter is an optional identifier that names the DMA buffer gr= oup +(in case you have several in the same structure):: + + struct my_device { + spinlock_t lock1; + __dma_from_device_group_begin(buffer1); + char dma_buffer1[16]; + __dma_from_device_group_end(buffer1); + spinlock_t lock2; + __dma_from_device_group_begin(buffer2); + char dma_buffer2[16]; + __dma_from_device_group_end(buffer2); + }; + +On cache-coherent platforms these macros expand to zero-length array marke= rs. +On non-coherent platforms, they also ensure the minimal DMA alignment, whi= ch +can be as large as 128 bytes. + +.. note:: + + It is allowed (though somewhat fragile) to include extra fields, n= ot + intended for DMA from the device, within the group (in order to pa= ck the + structure tightly) - but only as long as the CPU does not write th= ese + fields while any fields in the group are mapped for DMA_FROM_DEVIC= E or + DMA_BIDIRECTIONAL. + DMA addressing capabilities =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D =20 --=20 MST