From mboxrd@z Thu Jan 1 00:00:00 1970 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 B2CA222D780 for ; Wed, 31 Dec 2025 14:40:42 +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=1767192044; cv=none; b=PAQ1vPqsc5+iE29mUUB8Yns+U7gX7lymQJB7kRMrYrSAp7tqCxsFJ+2/lWM0TF7hYreIXrutFEf4eeh1/5RjzftcdHXh6OifoyH18uxMSxX/UDngsJBa2lFGjw0CEvywPn0hQabvdbdNaveWlP13Vu9BRDpxLnUAVY72Jddo9AM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767192044; c=relaxed/simple; bh=uCJpKbQgK1JDZeWrhG0sD4sWy8cW8kDi8pCaSJxqzWY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=XQQPkhiW96wswGIAQ/UYm/5HAasQwUoVSTOe6bd8zag5bwUfsY2oM8H/j8B3aDvzE/xbFzReFUsdtV8oUZ2kdym2sVHFojHIEKoRfsKJD2gdexWjQtpJWUtxRcQj9z3PdrU5BOgT3GmLErgzXcPIZzJdV7ByEx1RpESQBM0j2cM= 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=HDERpxxv; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=OUPcxx4T; 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="HDERpxxv"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="OUPcxx4T" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767192041; 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=n0eNh1PwPkp66fDhwYL8c44iVldZ6MxzxhBv6XJLUek=; b=HDERpxxvpZ16olNTd6QLpq73Awqnw5dRWTOR76RV1UuLBRgsws7CIsIblbN4ZEpQaRgAkW MefHiem0jiigZlMiGVM8zhbdO6Mxt7Ov+lsmR4cdkv7MEJB7ZX6RC+ZW2yZ4CQuEsSnjRF SOX+ElHtx1Hc5HAnT4gOSpMlO/YCH3I= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-599-W4ZnIDPKO068G7zyiNpsFw-1; Wed, 31 Dec 2025 09:40:40 -0500 X-MC-Unique: W4ZnIDPKO068G7zyiNpsFw-1 X-Mimecast-MFC-AGG-ID: W4ZnIDPKO068G7zyiNpsFw_1767192039 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-477cf2230c8so109869845e9.0 for ; Wed, 31 Dec 2025 06:40:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1767192039; x=1767796839; 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=n0eNh1PwPkp66fDhwYL8c44iVldZ6MxzxhBv6XJLUek=; b=OUPcxx4TcY8oIazW0j05RQ+fBwXK6dVnvRKGVVtsfOea04Jdvd7GJs4iaLtSJ27Yfy SMhJHXnaZyuB38co1vH7ROc+20AWAIKUV3FLm0wqz89/a4OoH0rejlT0OatejEoZye+r +S0PJZgV5f8riwSnNknTY1CeMcu0Y/KSglDf/Z8U+XGy2byq8cK/+zmIcpOIKYuKlSQ3 8alwrl9vDI+MpaonjOc/8FuYyyjoJMGNMImUFcUG7ExRe3p6KMTgYLbmpsjUmItGvwpb JrlzLRpaxJe09bL7vC5PaXdLyK470z4j3bo8x3RQAwqjeSWPIwqDGquJ4ghBMjhXXWJI /lGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767192039; x=1767796839; 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=n0eNh1PwPkp66fDhwYL8c44iVldZ6MxzxhBv6XJLUek=; b=fUVvMxUiJKnx5iwOESQuxFONgsKZRPFQtWNRAQtKN2mmnlvWtB0ulqswHccsVF8m/u jDdeRG9hi1Es1w6DTTCel8AGpd38lRYrjM4Gxc+kWdikZF7BdNVKBuW1fnnVqbfgRJpP ffkLBf44liyuc2s5xKnQL2atOhLpuUaBKBRRSpoF45P8AyBOEl7lBnfjQKLHEqEc+1MO NvSNo2IKNEvbtvwagWhp+pi0I5C3wRvs1Lp1EkOKD3rVe0suEqHRyViMNHiPUh7jQq65 TpCNHEo9524MW58ZL+HXOY5NS8Xn8yz4si7mQUUZ1P8slLUhtf40cV5a3Xf6jgxiEz79 4ClA== X-Forwarded-Encrypted: i=1; AJvYcCWUuyJJhdMYomQ1i6JGjpPV5SrJuB+V05zWIoty1BOjaMM+W7xiMX9we9MjVOUHso7RNUKh6iiOF4w=@vger.kernel.org X-Gm-Message-State: AOJu0YxgsKM68Fr0wSoizjOpIbtXnztohmt9LlX+HcZrwDodScE8bPne kXFRmFoIegrtTy1Kwyw0fM3ubpltzx3i/USRLMPgt42pSj8HH/uhceuaONhIrx/EbJcNml/l6ym uu9niE/oFLf6wssRAThdsU03ayLsP+/5lXC+naoOK2HUVRuCGf+xMvHh1ifk7Kw== X-Gm-Gg: AY/fxX7x7HBQaxZN+gsVjWcpNGJseIdNbBoVQMQzFgHx3svnJHgbgTNC1cgh/HPFCnV oACK21oFxTtBNbp2JP0mII7IVDl0ZX/goigshDPJ+cHYRC99FPTwmqz/hb7/5UjebYvmBo64QS4 mEsE5IjFQJickTOiqs5R8rwIClv4qcvLvNaOInmr+4ZY1AgomOBk8Pu/L7KhysDSSXgB2Om+cS3 ZJaBPKaZiQlGodEFIYdMP9guJH527IZBdKVqEPfXKRQQa/f5aWjJK3iqbBBe52GCXr3TPw3x9lG 0xmStGASArQ7qWWl5PYGd77vN5WttWqRha2mYmiwvTI/9AcoGsU9Q3kaKb/Y6JZYQKT4R8Cz+Vt OSqbGQRVE40GgXOH5vD4qCZTtBXhzvMjUvQ== X-Received: by 2002:a05:600c:1d29:b0:477:1af2:f40a with SMTP id 5b1f17b1804b1-47d1957f946mr504015355e9.17.1767192039240; Wed, 31 Dec 2025 06:40:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IFftjQcUwvXMCaEXUnp/nGSfuL085M1yJ9YObPox1uDg3/ci/kM4qMkiO89pT6mz04eQEDKgA== X-Received: by 2002:a05:600c:1d29:b0:477:1af2:f40a with SMTP id 5b1f17b1804b1-47d1957f946mr504014855e9.17.1767192038655; Wed, 31 Dec 2025 06:40:38 -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-47be2724fe8sm835831745e9.1.2025.12.31.06.40.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Dec 2025 06:40:38 -0800 (PST) Date: Wed, 31 Dec 2025 09:40:34 -0500 From: "Michael S. Tsirkin" To: Petr Tesarik Cc: linux-kernel@vger.kernel.org, Cong Wang , Jonathan Corbet , Olivia Mackall , Herbert Xu , Jason Wang , Paolo Bonzini , Stefan Hajnoczi , Eugenio =?iso-8859-1?Q?P=E9rez?= , "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 , 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: Re: [PATCH RFC 01/13] dma-mapping: add __dma_from_device_align_begin/end Message-ID: <20251231092346-mutt-send-email-mst@kernel.org> References: <20251231150159.1779b585@mordecai> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20251231150159.1779b585@mordecai> On Wed, Dec 31, 2025 at 03:01:59PM +0100, Petr Tesarik wrote: > On Tue, 30 Dec 2025 05:15:46 -0500 > "Michael S. Tsirkin" wrote: > > > When a structure contains a buffer that DMA writes to alongside fields > > that the CPU writes to, cache line sharing between the DMA buffer and > > CPU-written fields can cause data corruption on non-cache-coherent > > platforms. > > > > Add __dma_from_device_aligned_begin/__dma_from_device_aligned_end > > annotations to 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; > > }; > > > > 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]; > > }; > > This works, but it's a bit hard to read. Can we reuse the > __cacheline_group_{begin, end}() macros from ? > Something like this: > > #define __dma_from_device_group_begin(GROUP) \ > __cacheline_group_begin(GROUP) \ > ____dma_from_device_aligned > #define __dma_from_device_group_end(GROUP) \ > __cacheline_group_end(GROUP) \ > ____dma_from_device_aligned > > And used like this (the "rxbuf" group id was chosen arbitrarily): > > struct my_device { > spinlock_t lock1; > __dma_from_device_group_begin(rxbuf); > char dma_buffer1[16]; > char dma_buffer2[16]; > __dma_from_device_group_end(rxbuf); > spinlock_t lock2; > }; > > Petr T Oh, that's a clever idea! Will do! And GROUP is optional if there's only one group in a structure. > > Signed-off-by: Michael S. Tsirkin > > --- > > include/linux/dma-mapping.h | 10 ++++++++++ > > 1 file changed, 10 insertions(+) > > > > diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h > > index aa36a0d1d9df..47b7de3786a1 100644 > > --- a/include/linux/dma-mapping.h > > +++ b/include/linux/dma-mapping.h > > @@ -703,6 +703,16 @@ static inline int dma_get_cache_alignment(void) > > } > > #endif > > > > +#ifdef ARCH_HAS_DMA_MINALIGN > > +#define ____dma_from_device_aligned __aligned(ARCH_DMA_MINALIGN) > > +#else > > +#define ____dma_from_device_aligned > > +#endif > > +/* Apply to the 1st field of the DMA buffer */ > > +#define __dma_from_device_aligned_begin ____dma_from_device_aligned > > +/* Apply to the 1st field beyond the DMA buffer */ > > +#define __dma_from_device_aligned_end ____dma_from_device_aligned > > + > > static inline void *dmam_alloc_coherent(struct device *dev, size_t size, > > dma_addr_t *dma_handle, gfp_t gfp) > > {