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 v2 0/5] dma-mapping: Fixes for memory encryption
Date: Mon, 30 Mar 2026 14:50:38 +0000 [thread overview]
Message-ID: <20260330145043.1586623-1-smostafa@google.com> (raw)
Introduction
============
This is the second version of the fixes for direct-dma dealing with
memory encryption and restricted-dma.
In this version, one more fix is included and I go a step further
and attempt to clean up the code to consolidate it and make it less
error prone.
Especially with more users coming, such as using decrypted dma-bufs [1]
which also interacts with dma-direct.
Lastly I added a new documentation for memory encryption based on my
conclusion (Documentation/core-api/dma-direct-memory-encryption.rst)
Background
==========
At the moment the following hypervisor guests will need to deal with
memory encryption:
- pKVM (ARM): Documentation/virt/kvm/arm/hypercalls.rst
- ARM CCA: Documentation/arch/arm64/arm-cca.rst
- Intel TDX: Documentation/arch/x86/tdx.rst
- AMD SEV: Documentation/arch/x86/amd-memory-encryption.rst
- PPC SVM: Documentation/arch/powerpc/ultravisor.rst
- Hyper-V: Documentation/virt/hyperv/coco.rst
AFAICT, all (confidential) guests running under those have the memory
encrypted by default and guests will then explicitly share the memory
back if needed.
The main use cases for decrypting(sharing) memory are:
- Sharing memory back to the host through SWIOTLB (for virtio...)
- Hypervisor specific communication (ex: snp_msg, GHCB, VMBUS...)
- Shared/emulated resources: VGARAM (x86-SEV), GIC ITS tables (arm64)
While encrypting memory is typically used for reverting the
set_memory_decrypted() either in error handling or in freeing shared
resources back to the kernel.
Design
======
This series focuses mainly on dma-direct interaction with memory
encryption which is the complicated case.
At the moment memory encryption and dma-direct interacts in 2 ways:
1) force_dma_direct(): if true, memory will be decrypted by default
on allocation.
2) Restricted DMA: where memory is pre-decrypted and managed by
SWIOTLB.
With a third possible usage on the way [1] where the DMA-API allows
an attr for decrypted memory.
Instead of open coding many checks with is_swiotlb_for_alloc() and
force_dma_unencrypted() which doesn’t have the same exact semantics,
add some helpers to abstract that logic into the code:
* dma_external_decryption(dev): Returns true if the pages are decrypted
but managed externally.
* dma_owns_decryption(dev): Returns true if the pages need to be
explicitly decrypted and managed by the `dma-direct` layer (as the
architecture forces unencrypted DMA).
* is_dma_decrypted(dev): Returns true if the memory being used is in
a decrypted state, regardless of who manages it.
Testing
=======
I was able to test this only under pKVM (arm64) as I have no
access to other systems.
Future work
===========
Two other things I am also looking at which are related to restricted
DMA pools, so they should be a different series.
1) Private pools: Currently all restricted DMA pools are decrypted
(shared) by default. Having private pools would be useful for
device assignment when bouncing is needed (as for non-coherent
devices)
2) Optimizations for memory sharing. In some cases, allocations from
restricted dma-pools are page aligned. For CoCo cases, that means
that it will be cheaper to share memory in-place instead of
bouncing.
Both of these add new semantics which need to be done carefully to
avoid regressions, and might be a good candidate for a topic in the
next LPC.
Patches
=======
- 1-3 Fixes
- 4 Refactoring
- 5 Documentation
v1: https://lore.kernel.org/all/20260305170335.963568-1-smostafa@google.com/
[1] https://lore.kernel.org/all/20260305123641.164164-1-jiri@resnulli.us/
Mostafa Saleh (5):
dma-mapping: Avoid double decrypting with DMA_RESTRICTED_POOL
dma-mapping: Use the correct phys_to_dma() for DMA_RESTRICTED_POOL
dma-mapping: Decrypt memory on remap
dma-mapping: Refactor memory encryption usage
dma-mapping: Add doc for memory encryption
.../core-api/dma-direct-memory-encryption.rst | 77 +++++++++++++++++++
kernel/dma/direct.c | 51 ++++++++----
2 files changed, 114 insertions(+), 14 deletions(-)
create mode 100644 Documentation/core-api/dma-direct-memory-encryption.rst
--
2.53.0.1185.g05d4b7b318-goog
next reply other threads:[~2026-03-30 14:51 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-30 14:50 Mostafa Saleh [this message]
2026-03-30 14:50 ` [RFC PATCH v2 1/5] dma-mapping: Avoid double decrypting with DMA_RESTRICTED_POOL Mostafa Saleh
2026-03-30 15:06 ` Jason Gunthorpe
2026-03-30 20:43 ` Mostafa Saleh
2026-03-31 11:34 ` Suzuki K Poulose
2026-03-31 12:50 ` Mostafa Saleh
2026-03-30 14:50 ` [RFC PATCH v2 2/5] dma-mapping: Use the correct phys_to_dma() for DMA_RESTRICTED_POOL Mostafa Saleh
2026-03-30 15:09 ` Jason Gunthorpe
2026-03-30 20:47 ` Mostafa Saleh
2026-03-30 22:28 ` Jason Gunthorpe
2026-03-30 14:50 ` [RFC PATCH v2 3/5] dma-mapping: Decrypt memory on remap Mostafa Saleh
2026-03-30 15:19 ` Jason Gunthorpe
2026-03-30 20:49 ` Mostafa Saleh
2026-03-30 22:30 ` Jason Gunthorpe
2026-03-30 14:50 ` [RFC PATCH v2 4/5] dma-mapping: Refactor memory encryption usage Mostafa Saleh
2026-03-30 15:27 ` Jason Gunthorpe
2026-03-30 14:50 ` [RFC PATCH v2 5/5] dma-mapping: Add doc for memory encryption 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=20260330145043.1586623-1-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox