From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 D60373E0249 for ; Wed, 11 Mar 2026 12:24:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773231884; cv=none; b=lZ4SnOGlQuuom9U/ZBCWpE/J3PVGGlNS6G0WXKl9fQ/fRdCo/2D3kc9V4NCdvIu1LhlB+NEsP5wdjvL5JBvshSWvWiD02IzmDEyKf65rFiIGi9t8YyW/Qjq9C8WNmylr4InNvdosxnRF0aDJIRJP1V3NLaY9TWtMlZGluToQgu0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773231884; c=relaxed/simple; bh=rmLt2Zd1Svv94StO58JTVqPThQKGl0ZJLa2inCyuhrU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VT+gUyvSa2dhwKQul32kaIELt1/l4MHxQHXhkolex/KOAW5GAZatnr05vy4sHhp4VtL8b7umREuPPphtVKpeb+eQz9HxWLJdf3HwehbF2bjda/5greFzZn4/fhgfOviRoGZ0+nj5ULSgm3rimB7+gCL+iYiLKcr+Pu5bMjq7iKY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Dd7LwCXR; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Dd7LwCXR" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4852ef20fe8so61095e9.1 for ; Wed, 11 Mar 2026 05:24:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1773231879; x=1773836679; 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=HeBGnYAajUaTqEiFUt5o86dxRkDw0LV7oFcPjSzEHkE=; b=Dd7LwCXRfx5xXIjfDNF4rZPOKZwNZh3NhI1ZYyosaXFh6rY6xFQzpll9wvvmu7crMH HI9yBtH/1NSa+QEoC1t/AcVhep1JRHK85dEpP0MqohFdsx7bxEfgKN7m0Jq+Ae5m3tqR w3xpwd/9UXmF8v2EeWRjm1gkpO+A3VO4zfVm96shoErc3t8fV4MAbCSEyXGfZw05KnKz lt3P8ccPSRb5HlvAxX/eLg5Pbqh/NKQArh9WqNl+pG77ykiJEdB2vcGdHzzMvZGcYJ2f 8snMxzu+P+O+9IvrKVk04wHzuupsZC3drlDWWGEAGOQzSpU6Fj/w5EUUyq7ynIeInneH aIsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773231879; x=1773836679; 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=HeBGnYAajUaTqEiFUt5o86dxRkDw0LV7oFcPjSzEHkE=; b=dGGMg5KqYBmD/4Rnc612yTOUL6jdESBv/flUN/nHmD+UprAvpc4Evp1EZKnujV+mQ1 +0JSOpJXqxNjJOURRBl4h/EEniwhw/PkZV2bgp8aYPwkrT5JFbyR2fQ3eguLB9xTeamm QCbMzZV4KBqfHsqpldsvEXGkGBQpjatu+L8yH/bxLmAcqLFVqEEFHitLcbOuBvD3Qbgj man99s4lUqlDxylywBXfs68HturE+ElEH/JL5xZpybPT1ygLr8mgVdyKPt7e3IIi1STm hhzRgG7GqmHT27kgxcxfgNE+7UGLYJpHxK7UV4frLlvpg47+o6OYLxEio0Gz6g2j7esG qqDg== X-Forwarded-Encrypted: i=1; AJvYcCW+HjP7EGpLftIvAGRXOC7TxGfUztaPxrO8PTTRndGCiIRbPZarsQK8mGsQ975uXJuTFZeMwMfkaIXJ3aE=@vger.kernel.org X-Gm-Message-State: AOJu0YwDgBpOMPwyeTTZUlv7Nylk16qS+PuUwVnAiAeTqNFBck/6HzHW Al+K3RnP0wkDwl3uHyYMwN0WKgNcNjVIcQblP+f6A90SXgQg+fTsa/MywQRR4U30bA== X-Gm-Gg: ATEYQzzMhuIaKJsVjowbeytsvmOx2vOrT0fVzeCRmXz9lb59WHaYbpbw2B3jkZ0j54n YB5/ZMN7yu8HbYX3+FuvwTcopx02NaY9vQ8lkhk7mqbjq1rRh71qSbTr9r53NwzDln3L2xWOqQD 07mZKg+C+wsLWNlaTciSnh2tUWzGNXX7QuzZ5Knu/7A3iA1o1xQa9sERPK3GctG01J5kkKL30Jh lxevBohLFGmhPW+rtKGibTJ9A/oEyNJIX3AXOMI7dAeTUEF1TOQFw9JGD38Mu7RryCpWnD42QnP Rx3pVjGpZ8uz8Ge8ZdgdPraJlQyq/0sLYtI2RWCjKw9hsPU0dyeqE75kSz1PBpprjB/3Pxdd/wE 8v5bTeGk+yoz88RHmxq89jGfSO1qAmzCYL/653k27QaMRRJNqyvEa2YlHgFzARzL/9RoCW7azJT dq0z6uDscCY6MegChK3uV3MT9+ha5uFwflgoXCZnhaVsrqQsLmy+QkLdoZ X-Received: by 2002:a05:600d:8446:10b0:477:86fd:fb47 with SMTP id 5b1f17b1804b1-48549e6f504mr699505e9.8.1773231878612; Wed, 11 Mar 2026 05:24:38 -0700 (PDT) Received: from google.com (54.95.38.34.bc.googleusercontent.com. [34.38.95.54]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439f81f1318sm7153025f8f.21.2026.03.11.05.24.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2026 05:24:37 -0700 (PDT) Date: Wed, 11 Mar 2026 12:24:33 +0000 From: Mostafa Saleh To: "Aneesh Kumar K.V" Cc: Suzuki K Poulose , linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-coco@lists.linux.dev, Catalin Marinas , will@kernel.org, maz@kernel.org, tglx@linutronix.de, robin.murphy@arm.com, akpm@linux-foundation.org, jgg@ziepe.ca, steven.price@arm.com Subject: Re: [PATCH v2 4/4] dma: direct: set decrypted flag for remapped dma allocations Message-ID: References: <20251221160920.297689-1-aneesh.kumar@kernel.org> <20251221160920.297689-5-aneesh.kumar@kernel.org> <5820e8f3-9cf8-423f-89df-0df7ca78a84b@arm.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: On Fri, Dec 26, 2025 at 02:29:24PM +0530, Aneesh Kumar K.V wrote: > Aneesh Kumar K.V writes: > > > Suzuki K Poulose writes: > > > >> On 21/12/2025 16:09, Aneesh Kumar K.V (Arm) wrote: > >>> Devices that are DMA non-coherent and need a remap were skipping > >>> dma_set_decrypted(), leaving buffers encrypted even when the device > >>> requires unencrypted access. Move the call after the remap > >>> branch so both paths mark the allocation decrypted (or fail cleanly) > >>> before use. > >>> > >>> Fixes: f3c962226dbe ("dma-direct: clean up the remapping checks in dma_direct_alloc") > >>> Signed-off-by: Aneesh Kumar K.V (Arm) > >>> --- > >>> kernel/dma/direct.c | 8 +++----- > >>> 1 file changed, 3 insertions(+), 5 deletions(-) > >>> > >>> diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > >>> index 3448d877c7c6..a62dc25524cc 100644 > >>> --- a/kernel/dma/direct.c > >>> +++ b/kernel/dma/direct.c > >>> @@ -271,9 +271,6 @@ void *dma_direct_alloc(struct device *dev, size_t size, > >>> if (remap) { > >>> pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs); > >>> > >>> - if (force_dma_unencrypted(dev)) > >>> - prot = pgprot_decrypted(prot); > >> > >> This would be problematic, isn't it ? We don't support decrypted on a > >> vmap area for arm64. If we move this down, we might actually use the > >> vmapped area. Not sure if other archs are fine with "decrypting" a > >> "vmap" address. > >> > >> If we map the "vmap" address with pgprot_decrypted, we could go ahead > >> and further map the linear map (i.e., page_address(page)) decrypted > >> and get everything working. > > > > We still have the problem w.r.t free > > > > dma_direct_free(): > > > > if (is_vmalloc_addr(cpu_addr)) { > > vunmap(cpu_addr); > > } else { > > if (dma_set_encrypted(dev, cpu_addr, size)) > > return; > > } > > > > How about the below change? > > commit 8261c528961c6959b85de87c5659ce9081dc85b7 > Author: Aneesh Kumar K.V (Arm) > Date: Fri Dec 19 14:46:20 2025 +0530 > > dma: direct: set decrypted flag for remapped DMA allocations > > Devices that are DMA non-coherent and require a remap were skipping > dma_set_decrypted(), leaving DMA buffers encrypted even when the device > requires unencrypted access. Move the call after the if (remap) branch > so that both direct and remapped allocation paths correctly mark the > allocation as decrypted (or fail cleanly) before use. > > If CMA allocations return highmem pages, treat this as an allocation > error so that dma_direct_alloc() falls back to the standard allocation > path. This is required because some architectures (e.g. arm64) cannot > mark vmap addresses as decrypted, and highmem pages necessarily require > a vmap remap. As a result, such allocations cannot be safely marked > unencrypted for DMA. > > Other architectures (e.g. x86) do not have this limitation, but instead > of making this architecture-specific, I have made the restriction apply > when the device requires unencrypted DMA access. This was done for > simplicity, > > Fixes: f3c962226dbe ("dma-direct: clean up the remapping checks in dma_direct_alloc") > Signed-off-by: Aneesh Kumar K.V (Arm) Are there any cases this happened in CCA, the only cases I can see remap is true are: - PageHighMem(): Where that fails for CCA - !dev_is_dma_coherent(): AFAIK, all devices with CCA must have an SMMU, so direct DMA is only for virtualized devices which cannot be incoherent. Thanks, Mostafa > > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > index 7c0b55ca121f..811de37ad81c 100644 > --- a/kernel/dma/direct.c > +++ b/kernel/dma/direct.c > @@ -264,6 +264,15 @@ void *dma_direct_alloc(struct device *dev, size_t size, > * remapped to return a kernel virtual address. > */ > if (PageHighMem(page)) { > + /* > + * Unencrypted/shared DMA requires a linear-mapped buffer > + * address to look up the PFN and set architecture-required PFN > + * attributes. This is not possible with HighMem, so return > + * failure. > + */ > + if (force_dma_unencrypted(dev)) > + goto out_free_pages; > + > remap = true; > set_uncached = false; > } > @@ -284,7 +293,13 @@ void *dma_direct_alloc(struct device *dev, size_t size, > goto out_free_pages; > } else { > ret = page_address(page); > - if (dma_set_decrypted(dev, ret, size)) > + } > + > + if (force_dma_unencrypted(dev)) { > + void *lm_addr; > + > + lm_addr = page_address(page); > + if (set_memory_decrypted((unsigned long)lm_addr, PFN_UP(size))) > goto out_leak_pages; > } > > @@ -349,8 +364,16 @@ void dma_direct_free(struct device *dev, size_t size, > } else { > if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED)) > arch_dma_clear_uncached(cpu_addr, size); > - if (dma_set_encrypted(dev, cpu_addr, size)) > + } > + > + if (force_dma_unencrypted(dev)) { > + void *lm_addr; > + > + lm_addr = phys_to_virt(dma_to_phys(dev, dma_addr)); > + if (set_memory_encrypted((unsigned long)lm_addr, PFN_UP(size))) { > + pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n"); > return; > + } > } > > __dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size);