From nobody Wed Apr 1 11:13:38 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 C8D483264EF for ; Mon, 30 Mar 2026 14:51:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774882314; cv=none; b=cayPlnSkVs0QqWot92SfbImQFUGB1+bznoiXug5ByMp6qi7nJJis8v6V+crv285YOU85/YjxyC9TtUBjWzLS80irkwUbv/rfEAeE7UdeFbeAL0znAMBfrLpM+sjWYJSAN9lIPCuRi4UU+Qt+N6JsRdQ97lGpB+Ai7ANm5hKdKV4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774882314; c=relaxed/simple; bh=KCuGLCDrnXVXD6TiJbaeXO/0PpT9+aGSay1SncYRiiA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FA5YSzODKvGRdV7WNwD68sM9xrfDE3Jzh6OCbwssC94bQ3AJWw6W9+XaaVXZYwtRFKgowqAEA0/MrkaSrrZzHOcfYwnHolEf/PpIFwotlE/DXVQ9R6T0tNSvdROYq5G5EtPalcIjqedHOP2EHJzsrIaoO7S6KnynbFe4VmNG5x8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GDq44Z3o; arc=none smtp.client-ip=209.85.128.74 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=flex--smostafa.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GDq44Z3o" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-486fa35b005so56508525e9.2 for ; Mon, 30 Mar 2026 07:51:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774882311; x=1775487111; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6tg98HiSUgt/GbbZQxDpMM7Lbcp5/dXhobOqHvI8sRU=; b=GDq44Z3ogiKf7du5sxgo6I6NGoO/6OObg09G2eYfNb6Bqt9RFd2sEeZOJr2UEumk1X yRQ8/Lmljs/sgjBinGbQ+hYKnAcij4S6CCcjCvOdXyOMB1KUTNAeS0IG1V59VtrJvKAe ZfmohCDBwrFxVtlfKBMCkM6dpDOadsUsSADdOUWHKMm7j5k4ckmR5R6j0wZs1MuuXQ29 AaJ1apnGOG2uivaPivvcxippT8ZZkuYQQ7OOgDPHIjgDVtZHsybujh8/KuNcPya8Wy5t 30/eFUgR6voWAMdfSPNKF5++bS1QeQm1bRIGGm4d74Bho8OBlqEp4uyaoG/Ua/8451Xc Rm0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774882311; x=1775487111; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6tg98HiSUgt/GbbZQxDpMM7Lbcp5/dXhobOqHvI8sRU=; b=mERW2ybaI9mO0JVn8ftoDLdRLq0W2wO/vO02Sci1E8bvHMj6y9oDaZPLP/bEcyFh4y vpNWyilTdy19C+XZl6h7g7GRsqnZl8SrPCHGeIHudaYKRP21qP2gPwN4dMQ4SZdsbNXy LKvcyUd7zrUHy74O7SPR767XRRzKpcCXbrUmr/4Cxq8SyM/XKW4REagL4OlvBWr8/5px +xOmwIXJz2TeGD4kg47ihh6ynE44WS+HYqExWHOv9WFXBcdRxP1E1DJ1T6ew2enc6LNA VWjPQJRyN6B0xBzh0he+Ys93/ZCwLUTQdxzRXbjGEy3/eFCmPqyo9PBtavcJhynb9dbu CH5w== X-Forwarded-Encrypted: i=1; AJvYcCWfZFHBSfn6iNPETAePVq8RRGY2wrnqiIUZAnJrvXwBvzv53I3C7mhM9J8Q0aTLiS8dXcTw03kOK4Qcges=@vger.kernel.org X-Gm-Message-State: AOJu0YwZ2P0t5Ai+zege0so8hiJoBbmld7HMDqzpTCUCUH2AZVJZcNyU dpD6QOwHDlvcOeNam1Uvt8/tuoVN7t93H8J6al74gH2IaIQTOawp5B5TVVp4si5bqeGZWs8nB2B 2v8bgn7qaItYHIg== X-Received: from wmbz18.prod.google.com ([2002:a05:600c:c092:b0:487:38f4:95a5]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3d87:b0:486:5f71:5829 with SMTP id 5b1f17b1804b1-48727d5a307mr212298425e9.5.1774882311107; Mon, 30 Mar 2026 07:51:51 -0700 (PDT) Date: Mon, 30 Mar 2026 14:50:43 +0000 In-Reply-To: <20260330145043.1586623-1-smostafa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260330145043.1586623-1-smostafa@google.com> X-Mailer: git-send-email 2.53.0.1185.g05d4b7b318-goog Message-ID: <20260330145043.1586623-6-smostafa@google.com> Subject: [RFC PATCH v2 5/5] dma-mapping: Add doc for memory encryption From: Mostafa Saleh 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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a document for memory encryption usage with dma-direct. Signed-off-by: Mostafa Saleh --- .../core-api/dma-direct-memory-encryption.rst | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Documentation/core-api/dma-direct-memory-encryption.rst diff --git a/Documentation/core-api/dma-direct-memory-encryption.rst b/Docu= mentation/core-api/dma-direct-memory-encryption.rst new file mode 100644 index 000000000000..a780279292b5 --- /dev/null +++ b/Documentation/core-api/dma-direct-memory-encryption.rst @@ -0,0 +1,77 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +DMA Direct and Memory Encryption Integration +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Introduction +------------ +Modern platforms introduce memory encryption features (e.g., AMD SEV, Inte= l TDX, +ARM CCA, and pKVM) typically for CoCo when running protected virtual machi= nes. + +These guests typically boot with their memory encrypted by default. + +In some cases this memory needs to be accessed by the untrusted host or the +VMM which then requires this memory to be decrypted. One typical case is +dealing with emulated device (e.g., virtio) which are handled by direct-dma +code as these devices are not behind an IOMMU. + +That means, the memory used by these devices must be decrypted before acce= ssed +by the untrusted host. + +It must be clarified that encrypted/decrypted may not always be +cryptographic; in a broader sense, a decrypted page means that it is +accessible or "shared" with the untrusted host. + +Ownership +--------- +The direct-dma layer deals with memory encryption in two distinct scenario= s: + +1. **Externally Managed Decryption (e.g., Restricted DMA Pools)** + In some setups (like a device restricted to a specific SWIOTLB pool, i.= e., + `DMA_RESTRICTED_POOL`), an entire region of memory is pre-decrypted dur= ing + boot or pool initialization. The memory is owned by the pool, and the + transitions (encryption/decryption) are **not** managed by direct-dma on + a per-allocation basis. + See Documentation/core-api/swiotlb.rst + +2. **DMA Direct Managed Decryption (e.g., `force_dma_unencrypted()`)** + For standard coherent DMA allocations the direct-dma layer is explicitly + responsible for managing the decryption. It must decrypt the pages upon + allocation and re-encrypt them upon freeing. + +To cleanly separate these concerns, the core logic is abstracted via three +internal helpers: + +* ``dma_external_decryption(dev)``: Returns true if the pages are decrypte= d but + managed externally. For example, if the device allocates from a restrict= ed + DMA pool. +* ``dma_owns_decryption(dev)``: Returns true if the pages need to be expli= citly + decrypted and managed by the direct-dma layer (i.e., the architecture fo= rces + unencrypted DMA, and it's not handled by an external pool). +* ``is_dma_decrypted(dev)``: Returns true if the memory being used is in a + decrypted state, regardless of who manages it. + +Addressing and Page Protections +------------------------------- +When memory is decrypted (whether externally or by direct-dma), the layer = must +adjust physical-to-DMA address conversions and page protections: + +* **DMA Address Conversion:** + Decrypted memory often requires a specific bit to be cleared or set in t= he DMA + address (e.g., stripping the encryption bit). If ``is_dma_decrypted(dev)= `` is + true, the conversion uses ``phys_to_dma_unencrypted()`` instead of the s= tandard + ``phys_to_dma()``. + +* **Page Protections (Remap and Mmap):** + When remapping decrypted pages into the kernel virtual address space (vm= alloc) + or mapping them to user space via ``mmap()``, the page protection attrib= utes + must reflect the decrypted state. If ``is_dma_decrypted(dev)`` is true, = the + layer applies ``pgprot_decrypted(prot)`` to ensure the CPU accesses the = memory + with the correct encryption attributes. + +Notes +----- +In many cases when memory encryption/decryption fails the page will be lea= ked, +that's was added for TDX, where ``set_memory_encrypted()`` or +``set_memory_decrypted()``may fail while the page remains shared. --=20 2.53.0.1185.g05d4b7b318-goog