Re: [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted()

From: Marek Szyprowski

Date: Wed Nov 18 2026 - 03:05:46 EST


On 17.11.2026 16:54, Ying Xiao wrote:
> system_heap_allocate() allocates pages via order_flags[] with __GFP_ZERO
> while the direct map still has the memory encryption bit set. For the
> system_cc_shared heap, set_memory_decrypted() subsequently clears the
> encryption bit without re-zeroing, leaving ciphertext in DRAM on
> confidential computing (CoCo) guests instead of plaintext zeros.
>
> Skip __GFP_ZERO during alloc_pages() for cc_shared allocations, and zero
> the pages once in system_heap_set_page_decrypted() after
> set_memory_decrypted() succeeds.
>
> Fixes: 78b30c50a7ac ("dma-buf: heaps: system: add system_cc_shared heap for explicitly shared memory")
> Reviewed-by: Jacob Moroni <jmoroni@xxxxxxxxxx>
> Signed-off-by: Ying Xiao <eagleshaw@xxxxxxxxxx>

Acked-by: Marek Szyprowski <m.szyprowski@xxxxxxxxxxx>


> ---
> drivers/dma-buf/heaps/system_heap.c | 24 ++++++++++++++++++++----
> 1 file changed, 20 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c
> index c8959eadc71d..5f98d0df11e7 100644
> --- a/drivers/dma-buf/heaps/system_heap.c
> +++ b/drivers/dma-buf/heaps/system_heap.c
> @@ -72,11 +72,19 @@ static int system_heap_set_page_decrypted(struct page *page)
> int ret;
>
> ret = set_memory_decrypted(addr, nr_pages);
> - if (ret)
> + if (ret) {
> pr_warn_ratelimited("dma-buf system heap: failed to decrypt page at %p\n",
> page_address(page));
> + return ret;
> + }
>
> - return ret;
> + /*
> + * Clearing the encryption bit does not clear DRAM, leaving ciphertext
> + * from when the page was encrypted. Zero it now that it is decrypted.
> + */
> + memset((void *)addr, 0, (size_t)nr_pages << PAGE_SHIFT);
> +
> + return 0;
> }
>
> static int system_heap_set_page_encrypted(struct page *page)
> @@ -376,7 +384,8 @@ static const struct dma_buf_ops system_heap_buf_ops = {
> };
>
> static struct page *alloc_largest_available(unsigned long size,
> - unsigned int max_order)
> + unsigned int max_order,
> + bool cc_shared)
> {
> struct page *page;
> int i;
> @@ -388,6 +397,12 @@ static struct page *alloc_largest_available(unsigned long size,
> if (max_order < orders[i])
> continue;
> flags = order_flags[i];
> + /*
> + * Zeroing while encrypted is wasted since decryption leaves
> + * ciphertext; system_heap_set_page_decrypted() zeroes instead.
> + */
> + if (cc_shared)
> + flags &= ~__GFP_ZERO;
> if (mem_accounting)
> flags |= __GFP_ACCOUNT;
> page = alloc_pages(flags, orders[i]);
> @@ -438,7 +453,8 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap,
> goto free_buffer;
> }
>
> - page = alloc_largest_available(size_remaining, max_order);
> + page = alloc_largest_available(size_remaining, max_order,
> + cc_shared_buffer(buffer));
> if (!page)
> goto free_buffer;
>

Best regards
--
Marek Szyprowski, PhD
Samsung R&D Institute Poland