From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 7BDB731D372 for ; Wed, 15 Apr 2026 20:31:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776285085; cv=none; b=PD2xiaDK3OpZgDpBk558D1jJdrMWvyIA0gYZa/zLomnw4mWvGC25/aMftYLCTwWXUrNSMgm0SeA6D2ea5bRXLBCNXoEmkLOgogHUyvGv4UrmdcPsBpCgREGuOhNT3g5WFXU3hZGFvogBrR9tSKxbyCj/DmFyOxjCmkTx0QSQ4kc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776285085; c=relaxed/simple; bh=J4JAh0k8ECvYiDro/BanJC1prOJ+uW424pa2aQLT0ro=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=bdh+7qrW6vqufv80ouI/3Py8AA75v4e7OeEi8rTI4zKtePKfuAxTo8n59VcB+SjHTCOWZv8FqV++qolsLfo7DIOtIPEpBbuM97wwYg+La34K+jbXMJph/XpKFwRAhNxldzCF/o4IuflPXLuf4mSmG7l5V2vDXnmJOOe35nmY0tU= 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=eFbpL1BE; arc=none smtp.client-ip=209.85.128.46 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="eFbpL1BE" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-488879dcbc3so54975e9.0 for ; Wed, 15 Apr 2026 13:31:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776285083; x=1776889883; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=dA240JTANsz7Fy6selVjd2W4K7WhqmCNOmhAF00iECU=; b=eFbpL1BEpvvOevqsTjfniC11kreElVIju+nX6u+SbXgkk2B6u/ICyYhP7h9kSvJu9U gpfsvDejsht9V7LqGPbkFQ1sz04XOyJizZbTWMRJ8IZu1lGIk3U9AvupAIRaowlhMyZJ QZw8CXP9og3o91CpASbA+Vu0yPPePraAhF93hZ8NRDW73jXL3fL6PR9XuWt5Srk0a6Bk ayC27XB2j3cJBXD3k5+E1euSoJpjQfFl4nQqUCFS+DDtc53EpU/MCfL9yE1XTXPA0iMD 3gYCvUB2XPtP6QWTc+tvbSk/UNz/tYHubwKnGL+/h1LFGbNQnel6DkX426bHfjVLn6EN yEDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776285083; x=1776889883; h=in-reply-to:content-transfer-encoding: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=dA240JTANsz7Fy6selVjd2W4K7WhqmCNOmhAF00iECU=; b=CVbEwGiqqKJYdU14P4zgQNRRgRJDygAhcZp0GYLU5F65bA9N17xX/zmAT3tcf4l7WI UV643R41BM5luRFdDiu8eeYFHBqMrCTaxbSNWnWzCWjTw86jqkNbeXR3ANWhPyTX2mXV zwe/nho0I5Z/wzJOMX5/8lp6gBUA9vcJ0I01Hzk3V+St4xDDw+876/9W2QRcvSl6DFXC 7oHNVWt64KkmSIRe+RbPaAHuomR7llBBliX7RbEEotCWsFl3OKxf5XuNbyTYTcyTU9Vv +PsZk3gOwRwrQlLFyVEQ9MVz1J7TNkvnRHa++170Izy+bhCtmEb3+RCvh8Ee0q+4197f nusQ== X-Forwarded-Encrypted: i=1; AFNElJ/RWAh+GVvdRVfhuGWwS+ZNf1RDGFOiwCICqwsWiDupQy4Dc63aYdUJM1neKivIuzEmbSkJHgoyfL/hkrA=@vger.kernel.org X-Gm-Message-State: AOJu0YxJMFewuJMH2Q2pqkgzGJSQ4HfYF5254rfUKa71EHGa0jZctwV1 UsBMye2uLOcf53QGdIDRf0hQCQMZhswxuNHJWQHIZdHySzi/vd/jmA7yN5xBed3SFZihlXFo1QE WOU1Y0g== X-Gm-Gg: AeBDiesy+ATvB8Q8mtq2sAQRlSeMlDgPm4MavY73dfowccuaS2H0S0GXgLbq5GmVs6E tBd5eErGDDvK2iKOk+neIu1rtPTt2DKnyA/o8ImrxKtwONQOTHYyx3yqDtbC7MG3yaIREXzOAtC qAJrmZGb8K22euUwX1KE8+IWO8Cnc6EN3NcqV7ek6Fi6YROBVLf6+m8wDSgw8hk2wzLHFnVQKhb mndDGvIc1RM50zc4GPTWxrmqXXuLyeLfozS+L3yL1KcNMD81ftbozHA2TxzZVkJAYNTdDB3ODLk 3rNo2bTZbxGkq4SHQGCSB6GdbVyRrM9qQN90nY0qJapP3IT/ucz9/na5ZQ9gAwXXD3JbJCGCjwn MNyxBSI6DrVhTlGwUgTXmeMY/E21ZSEk2oVNsQtK3g+w3Yh21NEAvZdCycbsgS83McwLCmDTNst dKqP3caS8J3HFhaIjQbBS+fJFEtPnfdTTDee9b68GXa1kEFf8oRENKsJT1GYDrA8sz9fB1L8zzv Ve0dQ== X-Received: by 2002:a05:600d:8445:20b0:485:32b7:5b87 with SMTP id 5b1f17b1804b1-488f4aaae31mr152765e9.14.1776285082458; Wed, 15 Apr 2026 13:31:22 -0700 (PDT) Received: from google.com (8.181.38.34.bc.googleusercontent.com. [34.38.181.8]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43ead3d5f06sm8126874f8f.17.2026.04.15.13.31.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Apr 2026 13:31:21 -0700 (PDT) Date: Wed, 15 Apr 2026 20:31:18 +0000 From: Mostafa Saleh To: "Aneesh Kumar K.V" 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, jgg@ziepe.ca Subject: Re: [RFC PATCH v2 3/5] dma-mapping: Decrypt memory on remap Message-ID: References: <20260330145043.1586623-1-smostafa@google.com> <20260330145043.1586623-4-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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Mon, Apr 13, 2026 at 11:53:39AM +0530, Aneesh Kumar K.V wrote: > Mostafa Saleh writes: > > > In case memory needs to be remapped on systems with > > force_dma_unencrypted(), where this memory is not allocated > > from a restricted-dma pool, this was currently ignored, while only > > setting the decrypted pgprot in the remapped alias. > > > > The memory still needs to be decrypted in that case. > > > > With memory decryption, don't allow highmem allocations, but that > > shouldn't be a problem on such modern systems. > > > > Reported-by: Catalin Marinas > > Fixes: f3c962226dbe ("dma-direct: clean up the remapping checks in dma_direct_alloc") > > Signed-off-by: Mostafa Saleh > > --- > > kernel/dma/direct.c | 16 +++++++++++----- > > 1 file changed, 11 insertions(+), 5 deletions(-) > > > > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c > > index 1a402bb956d9..a4260689bcc8 100644 > > --- a/kernel/dma/direct.c > > +++ b/kernel/dma/direct.c > > @@ -203,6 +203,7 @@ static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size, > > void *dma_direct_alloc(struct device *dev, size_t size, > > dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) > > { > > + bool allow_highmem = !force_dma_unencrypted(dev); > > bool remap = false, set_uncached = false; > > struct page *page; > > void *ret; > > @@ -251,7 +252,7 @@ void *dma_direct_alloc(struct device *dev, size_t size, > > return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp); > > > > /* we always manually zero the memory once we are done */ > > - page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true); > > + page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, allow_highmem); > > if (!page) > > return NULL; > > > > @@ -265,6 +266,9 @@ void *dma_direct_alloc(struct device *dev, size_t size, > > set_uncached = false; > > } > > > > + if (dma_set_decrypted(dev, page_address(page), size)) > > + goto out_leak_pages; > > + > > if (remap) { > > pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs); > > > > @@ -278,11 +282,9 @@ void *dma_direct_alloc(struct device *dev, size_t size, > > ret = dma_common_contiguous_remap(page, size, prot, > > __builtin_return_address(0)); > > if (!ret) > > - goto out_free_pages; > > + goto out_encrypt_pages; > > } else { > > ret = page_address(page); > > - if (dma_set_decrypted(dev, ret, size)) > > - goto out_leak_pages; > > } > > > > memset(ret, 0, size); > > @@ -300,7 +302,6 @@ void *dma_direct_alloc(struct device *dev, size_t size, > > out_encrypt_pages: > > if (dma_set_encrypted(dev, page_address(page), size)) > > return NULL; > > -out_free_pages: > > __dma_direct_free_pages(dev, page, size); > > return NULL; > > out_leak_pages: > > @@ -339,7 +340,12 @@ void dma_direct_free(struct device *dev, size_t size, > > return; > > > > if (is_vmalloc_addr(cpu_addr)) { > > + void *vaddr = page_address(dma_direct_to_page(dev, dma_addr)); > > + > > vunmap(cpu_addr); > > + > > + if (dma_set_encrypted(dev, vaddr, size)) > > + return; > > > Right now, a remap is required under two conditions: > > 1. HighMem — I assume we are avoiding this for devices that require memory decryption. > 2. The device is not DMA-coherent. > > Can we assume that condition (2) will also not be supported alongside > memory encryption/decryption? That would allow us to simplify all of > this. We would then only need to carry the patch that disables HighMem > for devices requiring unencrypted DMA buffers. > > I did post a patch along similar lines some time back. There is also the > challenge of presenting a vmap address as decrypted on ARM. > https://lore.kernel.org/all/20260102155037.2551524-1-aneesh.kumar@kernel.org In v3, I prevent highmem allocations for devices that need decryption, I believe that modern systems with CCA won’t have a problem with that. With pKVM, it’s possible to have non-coherent devices, but they always have an IOMMU, so direct-dma is only used for virtualized devices which are coherent. So, this case is not important. This is a theoretical bug, I am not sure if any systems are impacted by, it but I included since Catalin mentioned it on v1 as I am already including other fixes. But I am happy to drop it or just add an assertion that it never happens. Thanks, Mostafa > > > > } else { > > if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED)) > > arch_dma_clear_uncached(cpu_addr, size); > > -- > > 2.53.0.1185.g05d4b7b318-goog