From: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
To: Mostafa Saleh <smostafa@google.com>,
iommu@lists.linux.dev, linux-kernel@vger.kernel.org
Cc: 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,
Mostafa Saleh <smostafa@google.com>
Subject: Re: [RFC PATCH v3 3/5] dma-mapping: Decrypt memory on remap
Date: Tue, 14 Apr 2026 15:01:15 +0530 [thread overview]
Message-ID: <yq5atstdanx8.fsf@kernel.org> (raw)
In-Reply-To: <20260408194750.2280873-4-smostafa@google.com>
Mostafa Saleh <smostafa@google.com> 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.
>
For ARM CCA, we cannot mark a vmap address as decrypted. I don’t expect
non-coherent DMA devices to be used in an ARM CCA configuration, but we
may need a way to document this in the code.
>
> With memory decryption, don't allow highmem allocations, but that
> shouldn't be a problem on such modern systems.
>
> Also, move force_dma_unencrypted() outside of dma_set_* to make it
> clear to be able to use more generic logic to decided memory
> state.
>
> Reported-by: Catalin Marinas <catalin.marinas@arm.com>
> Fixes: f3c962226dbe ("dma-direct: clean up the remapping checks in dma_direct_alloc")
> Signed-off-by: Mostafa Saleh <smostafa@google.com>
> ---
> kernel/dma/direct.c | 31 ++++++++++++++-----------------
> 1 file changed, 14 insertions(+), 17 deletions(-)
>
> diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
> index ce74f213ec40..de63e0449700 100644
> --- a/kernel/dma/direct.c
> +++ b/kernel/dma/direct.c
> @@ -79,8 +79,6 @@ 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))
> - return 0;
> return set_memory_decrypted((unsigned long)vaddr, PFN_UP(size));
> }
>
> @@ -88,8 +86,6 @@ static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size)
> {
> int ret;
>
> - if (!force_dma_unencrypted(dev))
> - return 0;
> ret = set_memory_encrypted((unsigned long)vaddr, PFN_UP(size));
> if (ret)
> pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n");
> @@ -206,7 +202,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 remap = false, set_uncached = false, encrypt = false;
> + bool remap = false, set_uncached = false, decrypt = force_dma_unencrypted(dev);
> struct page *page;
> void *ret;
>
> @@ -215,7 +211,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,
> gfp |= __GFP_NOWARN;
>
> if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
> - !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev))
> + !decrypt && !is_swiotlb_for_alloc(dev))
> return dma_direct_alloc_no_mapping(dev, size, dma_handle, gfp);
>
> if (!dev_is_dma_coherent(dev)) {
> @@ -249,12 +245,15 @@ void *dma_direct_alloc(struct device *dev, size_t size,
> * Remapping or decrypting memory may block, allocate the memory from
> * the atomic pools instead if we aren't allowed block.
> */
> - if ((remap || force_dma_unencrypted(dev)) &&
> + if ((remap || decrypt) &&
> dma_direct_use_pool(dev, gfp))
> 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);
> + /*
> + * we always manually zero the memory once we are done, and only allow
> + * high mem if pages doesn't need decryption.
> + */
> + page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, !decrypt);
> if (!page)
> return NULL;
>
> @@ -268,10 +267,12 @@ void *dma_direct_alloc(struct device *dev, size_t size,
> set_uncached = false;
> }
>
> + if (decrypt && dma_set_decrypted(dev, page_address(page), size))
> + goto out_leak_pages;
> if (remap) {
> pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs);
>
> - if (force_dma_unencrypted(dev))
> + if (decrypt)
> prot = pgprot_decrypted(prot);
>
> /* remove any dirty cache lines on the kernel alias */
> @@ -281,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);
> @@ -301,9 +300,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,
> return ret;
>
> out_encrypt_pages:
> - encrypt = true;
> -out_free_pages:
> - __dma_direct_free_pages(dev, page, size, encrypt);
> + __dma_direct_free_pages(dev, page, size, decrypt);
> return NULL;
> out_leak_pages:
> return NULL;
> @@ -366,7 +363,7 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
> return NULL;
>
> ret = page_address(page);
> - if (dma_set_decrypted(dev, ret, size))
> + if (force_dma_unencrypted(dev) && dma_set_decrypted(dev, ret, size))
> goto out_leak_pages;
> memset(ret, 0, size);
> *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
> --
> 2.53.0.1213.gd9a14994de-goog
next prev parent reply other threads:[~2026-04-14 9:31 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-08 19:47 [RFC PATCH v3 0/5] dma-mapping: Fixes for memory encryption Mostafa Saleh
2026-04-08 19:47 ` [RFC PATCH v3 1/5] swiotlb: Return state of memory from swiotlb_alloc() Mostafa Saleh
2026-04-14 9:25 ` Aneesh Kumar K.V
2026-04-08 19:47 ` [RFC PATCH v3 2/5] dma-mapping: Move encryption in __dma_direct_free_pages() Mostafa Saleh
2026-04-10 17:45 ` Jason Gunthorpe
2026-04-08 19:47 ` [RFC PATCH v3 3/5] dma-mapping: Decrypt memory on remap Mostafa Saleh
2026-04-14 9:31 ` Aneesh Kumar K.V [this message]
2026-04-14 12:22 ` Jason Gunthorpe
2026-04-14 13:13 ` Aneesh Kumar K.V
2026-04-14 13:53 ` Jason Gunthorpe
2026-04-08 19:47 ` [RFC PATCH v3 4/5] dma-mapping: Encapsulate memory state during allocation Mostafa Saleh
2026-04-10 18:05 ` Jason Gunthorpe
2026-04-08 19:47 ` [RFC PATCH v3 5/5] dma-mapping: Fix memory decryption issues Mostafa Saleh
2026-04-13 7:19 ` Aneesh Kumar K.V
2026-04-13 12:42 ` Jason Gunthorpe
2026-04-14 9:37 ` Aneesh Kumar K.V
2026-04-10 17:43 ` [RFC PATCH v3 0/5] dma-mapping: Fixes for memory encryption Jason Gunthorpe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=yq5atstdanx8.fsf@kernel.org \
--to=aneesh.kumar@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@ziepe.ca \
--cc=jiri@resnulli.us \
--cc=linux-kernel@vger.kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=maz@kernel.org \
--cc=robin.murphy@arm.com \
--cc=smostafa@google.com \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox