From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79E0C32D7FB for ; Mon, 30 Mar 2026 15:06:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774883219; cv=none; b=hCajXmnpXuNp0GpmQ7M63o6bo/dVaV9aYC4ZDHcSHyxPogUe+TWXWJ7GWkqa0TXQGqeHqU2uhL5RiZ5i/L10RfJE2pwpWAC4JoLPoA2/mLFFuOCLbZXuZVJ6KDOA5DrZClTKM/6nfGLceWfq7dxhaZs0JV9tMPHBeuqMxC3oWxM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774883219; c=relaxed/simple; bh=oMFumgtcsviaIMOGVLWdowRzwi9JxPG7CRNRp/XrgAg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=JzeJkf/9ESbkHQiPjy7lRzvB+E1ZVxAJ06MX74RuKmvOchJuozCEeV/YflLipl6aFmqAO9xlFoUZRYFZXKa6VBvw1tNMD//rBQWpq7rO+eZLAHrbmkkcvzQzyUqz9zKn0PCAtoyKUEBTb0j5UkQg9Fd1fkxDg6KK4PprDwEbeX8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca; spf=pass smtp.mailfrom=ziepe.ca; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b=ZWZnN6xn; arc=none smtp.client-ip=209.85.222.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="ZWZnN6xn" Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-8c70b5594f4so544725585a.1 for ; Mon, 30 Mar 2026 08:06:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; t=1774883216; x=1775488016; 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=Vvmm6aMgtMQBn9OgUBPy3rK/Y3NLNwW+81YPPtVWi5U=; b=ZWZnN6xnI5HY69G8bcmqyFJMkL1TLbDt7N+v2DCZmHKDKT8crwqtH6Z0Q3z0FsAtDJ Qz2oQAwKXf2/XqqprUUmZR814R+jsxPJ87aBfJtdwfEpMeisPPEdXJS8pTHBg7hVsbd0 70MazPnCR5T792wC6t1kI2yYIINX16HqmgzcpkIceCdNV+e9qA4OvLBCPnHgG8WTAKES LnSp/JcYc9dYVyZopDCJIJAUz3NIueLR3o5TG1bh7mhzDNdSfakPujkNOcHo+8zZ5vg1 1VL0mdtd8CknDyWGxU4MeQnqZ23iJIUNofFW+TU77AP99VtPaFCEAT9S/nHIkcWvePh0 kryA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774883216; x=1775488016; 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=Vvmm6aMgtMQBn9OgUBPy3rK/Y3NLNwW+81YPPtVWi5U=; b=CPnquY2+yteqi/xMG1kHCvMDh53Wuo9m2DFFibgWS/9kQejD9o2QsPmiApPx6vEKwo B70BAad5YSf20wXt4kyyWWrX1tfUfXZDgewU1NwcmCF2ROc8gHZCTUvt7Sfw8OoRVVb1 cDEBQOJxD01w3HQJHsNUmYly4DU3aTChmumUEOAN2Hr8+lh3TU3zV6HbtlPXCVKuA4Is 5FaI9c1CpA7r/yhASvu0u+g6wOdXsCixpNfCdvqlbNANQ78dGnr5PYTU9qJ99pZoVa2T 4+v1HQsmjPCvfnN0KddBtDkLbHZ6UfJeSH5lJYkG88jBKEGaPvDsGnCS4AYrqKmJvVDz iBAg== X-Forwarded-Encrypted: i=1; AJvYcCXjnfuX48NTkU/KQk4i40L38fvJ602rnyju9S9n4DZ9wvnztB/mEf9G6MMVZt59BDavbU67/4EzSYzSfuQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwRCdhuqGpNPpoU94zO8pcF76M+EZi0HsSucHg/Zg6VFgE0VFNq D8s+VI6lnPRe9owTXoKYfu+EU70uj+K31XeoztCLi/YAzxwzhr2hLionBR194f/jx5E= X-Gm-Gg: ATEYQzyKn/k6CWg+bP2UbDCa6hnrQU7R4PMcFALt0HNmYM1zjjRWuNiFeut/2BH33+D Xb4/N+dWNBgaZN3mHXdM12pZpzYb5OM6rTzTb7RVd2qwN3wVeIPKBLDXy+mLZCXP+iyhpuvfvkV 5AoXqU7Ht3ri5La7zOGn6vfqc2l62OMlR/+O9u+SIyjOTeNmShbQ2q/f3vB6fL7LIwYsEVfKriv VDUcenp1vOspsk6TiixaqR0TKm6PJwtVmP8vZ0Vp2fLbxaoccoXDNwtItUgFWV+vbsjhX+rPugg 9MEaEvSfBAnys0HENcYcRYrJ7X9kLbGrcH/RX27A3nerkEniffNElFoo4mtC4qX847EVk7socPm KVmV6twdkXe8H3rfvCWL9CrXpaXtR1f7vpY7TgGZcpBMZNDDBL1xHKOQ3TQTAS0AGVDKrzOGvAT XieJZBroQzl57Ya+QJiMpKj0aTURJYRg5KErJ8dhxU1hp1KZCxBor8/yngV4hkuo+/Ph8ywQ== X-Received: by 2002:a05:620a:179f:b0:8cd:b6da:7714 with SMTP id af79cd13be357-8d01ce265bcmr1628614085a.18.1774883216113; Mon, 30 Mar 2026 08:06:56 -0700 (PDT) Received: from ziepe.ca (mctnnbsa70w-159-2-73-22.dhcp-dynamic.fibreop.nb.bellaliant.net. [159.2.73.22]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8d02806a37bsm615516085a.34.2026.03.30.08.06.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Mar 2026 08:06:55 -0700 (PDT) Received: from jgg by wakko with local (Exim 4.97) (envelope-from ) id 1w7ECc-00000003PSU-29gv; Mon, 30 Mar 2026 12:06:54 -0300 Date: Mon, 30 Mar 2026 12:06:54 -0300 From: Jason Gunthorpe To: Mostafa Saleh Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, robin.murphy@arm.com, m.szyprowski@samsung.com, will@kernel.org, maz@kernel.org, suzuki.poulose@arm.com, catalin.marinas@arm.com, jiri@resnulli.us, aneesh.kumar@kernel.org Subject: Re: [RFC PATCH v2 1/5] dma-mapping: Avoid double decrypting with DMA_RESTRICTED_POOL Message-ID: <20260330150654.GA809900@ziepe.ca> References: <20260330145043.1586623-1-smostafa@google.com> <20260330145043.1586623-2-smostafa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@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: <20260330145043.1586623-2-smostafa@google.com> On Mon, Mar 30, 2026 at 02:50:39PM +0000, Mostafa Saleh wrote: > In case a device have a restricted DMA pool, it will be decrypted > by default. > > However, in the path of dma_direct_alloc() memory can be allocated > from this pool using, __dma_direct_alloc_pages() => > dma_direct_alloc_swiotlb() > > After that from the same function, it will attempt to decrypt it > using dma_set_decrypted() if force_dma_unencrypted(). > > Which results in the memory being decrypted twice. > > It's not clear how the does realm world/hypervisors deal with that, > for example: > - CCA: Clear a bit in the page table and call realm IPA_STATE_SET. > - TDX: Issue a hypercall. > - pKVM: Which doesn't implement force_dma_unencrypted() at the moment, > uses a share hypercall. > > Change that to only encrypt/decrypt memory that are not allocated > from the restricted dma pools. > > Fixes: f4111e39a52a ("swiotlb: Add restricted DMA alloc/free support") > Signed-off-by: Mostafa Saleh > --- > kernel/dma/direct.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > index 8f43a930716d..27d804f0473f 100644 > --- a/kernel/dma/direct.c > +++ b/kernel/dma/direct.c > @@ -79,7 +79,7 @@ bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size) > > static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size) > { > - if (!force_dma_unencrypted(dev)) > + if (!force_dma_unencrypted(dev) || is_swiotlb_for_alloc(dev)) > return 0; This seems really obtuse, I would expect the decryption state of the memory to be known by the caller. If dma_direct_alloc_swiotlb() can return decrypted or encrypted memory it needs to return a flag saying that. It shouldn't be deduced by checking dev flags in random places like this. Double decryption is certainly a bug, I do not expect that to work. Jason