From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1ED9B3E8C56; Wed, 25 Feb 2026 17:08:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772039333; cv=none; b=CNkIh+sk76mwMKF4pZBeccdRgdCNzF61L7C+Ag9phGqXWWTr1virtIjunGnSrbJ5kbCtzcHl+8KKl+lS2P8DdtL0Bvy0oZdlpCfiMoOAwjwXGbIkJSaTqky0FioYEVFfUmEaqEpmAUxEMHv2Dl1eNJucm4RKoNbrHaiawH7oWYs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772039333; c=relaxed/simple; bh=e86dy79TKlUxU3xf80Vcy6z1TEZmqr9y+GYyp3xRW3I=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=dmfw4U9RzJcpqxJrb9afpb1VNCrd7chjWM46KD0ByJkMMBsNE46nxDsNWA2Xhlhmm40Q3M3ja57jG3Ac9wiw6VLvQahCViqHjYwd1q8+1A0Bcf8IYtd0exsFzTh0bAfvO/0+Rfbk5npjZN5YV5DBL9qn+PPFd8tMCRVqUmTGEQw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 162C515A1; Wed, 25 Feb 2026 09:08:43 -0800 (PST) Received: from [10.57.58.107] (unknown [10.57.58.107]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3A8BC3F62B; Wed, 25 Feb 2026 09:08:40 -0800 (PST) Message-ID: Date: Wed, 25 Feb 2026 17:08:37 +0000 Precedence: bulk X-Mailing-List: iommu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH kernel 6/9] x86/dma-direct: Stop changing encrypted page state for TDISP devices To: Alexey Kardashevskiy , x86@kernel.org Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-pci@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra , Bjorn Helgaas , Dan Williams , Marek Szyprowski , Andrew Morton , Catalin Marinas , Michael Ellerman , Mike Rapoport , Tom Lendacky , Ard Biesheuvel , Neeraj Upadhyay , Ashish Kalra , Stefano Garzarella , Melody Wang , Seongman Lee , Joerg Roedel , Nikunj A Dadhania , Michael Roth , Suravee Suthikulpanit , Andi Kleen , Kuppuswamy Sathyanarayanan , Tony Luck , David Woodhouse , Greg Kroah-Hartman , Denis Efremov , Geliang Tang , Piotr Gregor , "Michael S. Tsirkin" , Alex Williamson , Arnd Bergmann , Jesse Barnes , Jacob Pan , Yinghai Lu , Kevin Brodsky , Jonathan Cameron , "Aneesh Kumar K.V (Arm)" , Xu Yilun , Herbert Xu , Kim Phillips , Konrad Rzeszutek Wilk , Stefano Stabellini , Claire Chang , linux-coco@lists.linux.dev, iommu@lists.linux.dev References: <20260225053806.3311234-1-aik@amd.com> <20260225053806.3311234-7-aik@amd.com> From: Robin Murphy Content-Language: en-GB In-Reply-To: <20260225053806.3311234-7-aik@amd.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2026-02-25 5:37 am, Alexey Kardashevskiy wrote: > TDISP devices operate in CoCo VMs only and capable of accessing > encrypted guest memory. > > Currently when SME is on, the DMA subsystem forces the SME mask in > DMA handles in phys_to_dma() which assumes IOMMU pass through > which is never the case with CoCoVM running with a TDISP device. > > Define X86's version of phys_to_dma() to skip leaking SME mask to > the device. > > Signed-off-by: Alexey Kardashevskiy > --- > > Doing this in the generic version breaks ARM which uses > the SME mask in DMA handles, hence ARCH_HAS_PHYS_TO_DMA. That smells a bit off... In CCA we should be in the same boat, wherein a trusted device can access memory at a DMA address based on its "normal" (private) GPA, rather than having to be redirected to the shared alias (it's really not an "SME mask" in that sense at all). I guess this comes back to the point I just raised on the previous patch - the current assumption is that devices cannot access private memory at all, and thus phys_to_dma() is implicitly only dealing with the mechanics of how the given device accesses shared memory. Once that no longer holds, I don't see how we can find the right answer without also consulting the relevant state of paddr itself, and that really *should* be able to be commonly abstracted across CoCo environments. And if in the process of that we could untangle the "implicit vs. explicit SME mask for shared memory or non-CoCo SME" case from common code and punt *that* into an x86-specific special case, all the better :) Thanks, Robin. > pci_device_add() enforces the FFFF_FFFF coherent DMA mask so > dma_alloc_coherent() fails when SME=on, this is how I ended up fixing > phys_to_dma() and not quite sure it is the right fix. > --- > arch/x86/Kconfig | 1 + > arch/x86/include/asm/dma-direct.h | 39 ++++++++++++++++++++ > 2 files changed, 40 insertions(+) > > diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig > index fa3b616af03a..c46283064518 100644 > --- a/arch/x86/Kconfig > +++ b/arch/x86/Kconfig > @@ -112,6 +112,7 @@ config X86 > select ARCH_HAS_UBSAN > select ARCH_HAS_DEBUG_WX > select ARCH_HAS_ZONE_DMA_SET if EXPERT > + select ARCH_HAS_PHYS_TO_DMA > select ARCH_HAVE_NMI_SAFE_CMPXCHG > select ARCH_HAVE_EXTRA_ELF_NOTES > select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE > diff --git a/arch/x86/include/asm/dma-direct.h b/arch/x86/include/asm/dma-direct.h > new file mode 100644 > index 000000000000..f50e03d643c1 > --- /dev/null > +++ b/arch/x86/include/asm/dma-direct.h > @@ -0,0 +1,39 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef ASM_X86_DMA_DIRECT_H > +#define ASM_X86_DMA_DIRECT_H 1 > + > +static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) > +{ > + if (dev->dma_range_map) > + return translate_phys_to_dma(dev, paddr); > + return paddr; > +} > + > +static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) > +{ > + /* > + * TDISP devices only work in CoCoVMs and rely on IOMMU to > + * decide on the memory encryption. > + * Stop leaking the SME mask in DMA handles and return > + * the real address. > + */ > + if (device_cc_accepted(dev)) > + return dma_addr_unencrypted(__phys_to_dma(dev, paddr)); > + > + return dma_addr_encrypted(__phys_to_dma(dev, paddr)); > +} > + > +static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) > +{ > + return daddr; > +} > + > +static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev, > + phys_addr_t paddr) > +{ > + return dma_addr_unencrypted(__phys_to_dma(dev, paddr)); > +} > + > +#define phys_to_dma_unencrypted phys_to_dma_unencrypted > + > +#endif /* ASM_X86_DMA_DIRECT_H */