From nobody Sun Feb 8 06:22:48 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 10495279DAE for ; Tue, 30 Dec 2025 10:15:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767089760; cv=none; b=OvoLkICJEU7YL8Pdg1JEnlDsME4o5b3LWQ2HtOSRKO3K3I0l8DPp9pcWpYh0uwmQJ3xFOIEKu+s8p7k/spDZZz4ydddAT/jx4O7Of+LFOVuy0JqkOH9v8z5CZbxG32ycOfKwEn51vtkvZeRC4uHg7Z9eS+HTFfTGkU2wY9Ao2sQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767089760; c=relaxed/simple; bh=1JUA4c0efATkxneQm73kN9tTpnR2rWTicL6pMgMgIPw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=DGK050gCwEhggd64xrDkW8jEyrGV271nRpfkcQ3ZCgrAfOy3onEYfFAZ6gC696A8bbL4vQBbkpvYawDME5SaW4O1K0JZVufhp0VvhtM+nRGRfH1o8OcVfU4OXL7DWLLY75VZFGNMeUKateGKvlWHEETaUzAqfAyJI2wXb26lWzk= 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=hvoCvzfd; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=km1vHaGw; arc=none smtp.client-ip=170.10.133.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="hvoCvzfd"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="km1vHaGw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767089756; 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=lIFloJTLYi5ioijSED2t687CZ+kUT7GxSlzISMtKW5A=; b=hvoCvzfdYVI5QnmibP+2Yajm1ZhOWvPiSLWw22wJxk4akt6ZEkoNRCt/E6rQe8E02O2dG1 u0+tK8UWATMvs3Q7iUbi4PnqB2fic9QoSW+7L/5kdiCPHLjOpweM/DXA0SwBxigpcCz3qn 5s1u/+cCMULSkZyYkuzBAFP+x5qYBPM= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-166-RE6CxXokOJOTKMj8MdVuNg-1; Tue, 30 Dec 2025 05:15:55 -0500 X-MC-Unique: RE6CxXokOJOTKMj8MdVuNg-1 X-Mimecast-MFC-AGG-ID: RE6CxXokOJOTKMj8MdVuNg_1767089754 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-430f527f5easo5873790f8f.1 for ; Tue, 30 Dec 2025 02:15:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1767089754; x=1767694554; 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=lIFloJTLYi5ioijSED2t687CZ+kUT7GxSlzISMtKW5A=; b=km1vHaGwMEO7mfXobXgRu2GF7yYLs+HkCTr39aGgXIkNfyh4MCjSseN2GIB2WtRFSB QYd42Kf3alEVord81XaouvsCcmA3h+Xnno+9YgpNQtjvo4sqLgAY0LhKgNU2UyZJKPu8 xK8BtD3XMZHDaRUcQwN/nsTpeBiSDoHpB3HTtur7rtj+2Bp8j2u1zc0wiAwPtmrJnI6J sT4d82FeSZkdH53JxTvjOAJeLgjdD12T+wgmpkjIpwQC2epj0el+N4GG3yUbrL8VgamH nm9NFPzcebrHNzdpgOOBgUyPU9c3UadTicdwhiCKwVZ+iFNiYzE/6lbWPCQPoec4VOn9 Z75A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767089754; x=1767694554; 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=lIFloJTLYi5ioijSED2t687CZ+kUT7GxSlzISMtKW5A=; b=QYTFPqNqzsrg5lBLKpBpo6fcGtPx6OxN1DtjtC5noBcbBaQvE9bJKMH2346heKMgu8 Iyc9AVd1zDGWOSEnbK0PQAc4ICTudMiLSNg5ShYJ5zU6/oqNjFJTAmlDVheucObXnExN 0BLDwKm7jpJb2BGXGb6j4VjX3oAOc/IoMJkBG+SLr9NByEtEuIemWASRgoNM/rWAnY1Q KCSntNALVToSIS6BRfApsHh3FTzMO/M+P25q9VKIlp33S1S1L8i+3HscN71onpXEQUFW oqoxFz19VJYS/+MBh0xWvD3nr+Y4w9lavEwitVdiL1Qvd1HDPMz0DdI47bUlBWrJO/w9 8IyA== X-Gm-Message-State: AOJu0YxZZVTb5sIYHTufqkwn7MZ22QdEqBXoRPKMvtCPV30Yw1iO31+4 fELGIEDcMZHL9glt/NSl+Mbpoou8f/XfnuzxcgaXwPKDTVhF/RL9NW3K/hDSzR1V6t2tHV7evOd A9b/HYg/RJcU973Up2PjZ72p1W7QvBUMEXZ1Rq2BCzmuU3OI5o79wL1eLz/9ZK5r1i3L+X7YVO+ opeIUBkM8xEawWXhgx4GYsd7zu5IowF/oZF6AtRmSCFWg= X-Gm-Gg: AY/fxX5++qw4pNjEjWrdXs4/VWP+cbfw05W9Q1ClPC77J4XTcBUyfZQjt9L0LO8W+S9 TVxJyZhWarmxf7p5e8ES4r2FaPaJDuON2kAvy1y/iW64gslzka1H04OGI2ZbgUC45ks0lO0DHng +q/MwuL7TdlQ69wpS+AyM/t0eOr8PQ4wq5532gGfDsuMZ5bA1V4Eg6WQ1grDn9L4PKpd5JObpMb BXdrh/bQBrbcJzQYWAKFrVqOMKi7c6zvD6ew4xeOMBK3iWJKVpSk8xz+aqPdmKrmKm0YsAb1qIY 3Hu2ZCl1kzjW/y0i4r8B5po7vsM8kC5T+U7Sg/y5zH6MhKgvdHR0cNH1Q69oWOpSxtCQfc7OlwF wWYZH32YpPMfJ7zmGKJIkgbDKt5kUtZX3EA== X-Received: by 2002:a05:600c:46cf:b0:477:9e0c:f59 with SMTP id 5b1f17b1804b1-47d18b833a6mr393753035e9.2.1767089753654; Tue, 30 Dec 2025 02:15:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IF0nrcJnS7RjupXcIyxgJ/7OwvI0dkgJ3plFmvdD3JvcGLBLhWJUwOhq1auRl2SqH+pkIX7dw== X-Received: by 2002:a05:600c:46cf:b0:477:9e0c:f59 with SMTP id 5b1f17b1804b1-47d18b833a6mr393752425e9.2.1767089752979; Tue, 30 Dec 2025 02:15:52 -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 5b1f17b1804b1-47be272eaf8sm630001695e9.5.2025.12.30.02.15.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Dec 2025 02:15:52 -0800 (PST) Date: Tue, 30 Dec 2025 05:15:49 -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 , 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 RFC 02/13] docs: dma-api: document __dma_align_begin/end Message-ID: <192a335d783a2e54f539dc5ff81bf3207dafa88f.1767089672.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_align_begin/__dma_align_end annotations introduced by the previous patch. Signed-off-by: Michael S. Tsirkin --- Documentation/core-api/dma-api-howto.rst | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Documentation/core-api/dma-api-howto.rst b/Documentation/core-= api/dma-api-howto.rst index 96fce2a9aa90..99eda4c5c8e7 100644 --- a/Documentation/core-api/dma-api-howto.rst +++ b/Documentation/core-api/dma-api-howto.rst @@ -146,6 +146,48 @@ 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_aligned_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=3D=3D + +As explained previously, when a structure contains a DMA_FROM_DEVICE buffer +(device writes to memory) alongside fields that the CPU writes to, cache l= ine +sharing between the DMA buffer and CPU-written fields can cause data corru= ption +on CPUs with DMA-incoherent caches. + +The ``__dma_from_device_aligned_begin/__dma_from_device_aligned_end`` +annotations ensure proper alignment to prevent this:: + + struct my_device { + spinlock_t lock1; + __dma_from_device_aligned_begin char dma_buffer1[16]; + char dma_buffer2[16]; + __dma_from_device_aligned_end spinlock_t lock2; + }; + +On cache-coherent platforms these macros expand to nothing. On non-coherent +platforms, they ensure the minimal DMA alignment, which can be as large as= 128 +bytes. + +.. note:: + + To isolate a DMA buffer from adjacent fields, you must apply + ``__dma_from_device_aligned_begin`` to the first DMA buffer field + **and additionally** apply ``__dma_from_device_aligned_end`` to the + **next** field in the structure, **beyond** the DMA buffer (as opposed + to the last field of the DMA buffer!). This protects both the head and + tail of the buffer from cache line sharing. + + When the DMA buffer is the **last field** in the structure, just + ``__dma_from_device_aligned_begin`` is enough - the compiler's struct + padding protects the tail:: + + struct my_device { + spinlock_t lock; + struct mutex mlock; + __dma_from_device_aligned_begin char dma_buffer1[16]; + char dma_buffer2[16]; + }; + 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