From: Mostafa Saleh <smostafa@google.com>
To: 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, aneesh.kumar@kernel.org,
Mostafa Saleh <smostafa@google.com>
Subject: [RFC PATCH v3 2/5] dma-mapping: Move encryption in __dma_direct_free_pages()
Date: Wed, 8 Apr 2026 19:47:39 +0000 [thread overview]
Message-ID: <20260408194750.2280873-3-smostafa@google.com> (raw)
In-Reply-To: <20260408194750.2280873-1-smostafa@google.com>
In the next patches, we will need to avoid encrypting memory allocated
from SWIOTLB, so instead of calling dma_set_encrypted() before
__dma_direct_free_pages(), call it inside, conditional on the memory
state passed to the function.
Signed-off-by: Mostafa Saleh <smostafa@google.com>
---
kernel/dma/direct.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 6efb5973fbd3..ce74f213ec40 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -97,8 +97,11 @@ static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size)
}
static void __dma_direct_free_pages(struct device *dev, struct page *page,
- size_t size)
+ size_t size, bool encrypt)
{
+ if (encrypt && dma_set_encrypted(dev, page_address(page), size))
+ return;
+
if (swiotlb_free(dev, page, size))
return;
dma_free_contiguous(dev, page, size);
@@ -203,7 +206,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;
+ bool remap = false, set_uncached = false, encrypt = false;
struct page *page;
void *ret;
@@ -298,10 +301,9 @@ void *dma_direct_alloc(struct device *dev, size_t size,
return ret;
out_encrypt_pages:
- if (dma_set_encrypted(dev, page_address(page), size))
- return NULL;
+ encrypt = true;
out_free_pages:
- __dma_direct_free_pages(dev, page, size);
+ __dma_direct_free_pages(dev, page, size, encrypt);
return NULL;
out_leak_pages:
return NULL;
@@ -311,6 +313,7 @@ void dma_direct_free(struct device *dev, size_t size,
void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs)
{
unsigned int page_order = get_order(size);
+ bool encrypt = false;
if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
!force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) {
@@ -343,11 +346,10 @@ 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))
- return;
+ encrypt = true;
}
- __dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size);
+ __dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size, encrypt);
}
struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
@@ -384,9 +386,7 @@ void dma_direct_free_pages(struct device *dev, size_t size,
dma_free_from_pool(dev, vaddr, size))
return;
- if (dma_set_encrypted(dev, vaddr, size))
- return;
- __dma_direct_free_pages(dev, page, size);
+ __dma_direct_free_pages(dev, page, size, true);
}
#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
--
2.53.0.1213.gd9a14994de-goog
next prev parent reply other threads:[~2026-04-08 19:47 UTC|newest]
Thread overview: 32+ 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-15 20:43 ` Mostafa Saleh
2026-04-16 8:53 ` Aneesh Kumar K.V
2026-04-17 15:05 ` Mostafa Saleh
2026-04-08 19:47 ` Mostafa Saleh [this message]
2026-04-10 17:45 ` [RFC PATCH v3 2/5] dma-mapping: Move encryption in __dma_direct_free_pages() Jason Gunthorpe
2026-04-15 20:49 ` Mostafa Saleh
2026-04-16 0:11 ` Jason Gunthorpe
2026-04-17 15:07 ` Mostafa Saleh
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
2026-04-14 12:22 ` Jason Gunthorpe
2026-04-14 13:13 ` Aneesh Kumar K.V
2026-04-14 13:53 ` Jason Gunthorpe
2026-05-07 17:18 ` Catalin Marinas
2026-05-08 4:03 ` Aneesh Kumar K.V
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-15 9:38 ` Aneesh Kumar K.V
2026-04-17 15:45 ` Mostafa Saleh
2026-05-07 17:36 ` Catalin Marinas
2026-05-11 10:48 ` Mostafa Saleh
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-15 12:43 ` Aneesh Kumar K.V
2026-04-15 13:53 ` 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
2026-04-15 20:25 ` Mostafa Saleh
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=20260408194750.2280873-3-smostafa@google.com \
--to=smostafa@google.com \
--cc=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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.