mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted()
@ 2026-11-17 15:54 ` Ying Xiao
  2026-11-18  8:05   ` Marek Szyprowski
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ying Xiao @ 2026-11-17 15:54 UTC (permalink / raw)
  To: Sumit Semwal, T . J . Mercier, Jiri Pirko
  Cc: Jason Gunthorpe, Marek Szyprowski, Benjamin Gaignard,
	Brian Starkey, John Stultz, Christian König,
	Philipp Stanner, Jacob Moroni, Xuehao Hu, dri-devel, linux-media,
	linaro-mm-sig, linux-kernel, Ying Xiao

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@google.com>
Signed-off-by: Ying Xiao <eagleshaw@google.com>
---
 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;
 
-- 
2.56.0.679.gc527c65306-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted()
  2026-11-17 15:54 ` [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted() Ying Xiao
@ 2026-11-18  8:05   ` Marek Szyprowski
  2026-11-18 13:15   ` Jiri Pirko
  2026-11-18 13:44   ` Jason Gunthorpe
  2 siblings, 0 replies; 4+ messages in thread
From: Marek Szyprowski @ 2026-11-18  8:05 UTC (permalink / raw)
  To: Ying Xiao, Sumit Semwal, T . J . Mercier, Jiri Pirko
  Cc: Jason Gunthorpe, Benjamin Gaignard, Brian Starkey, John Stultz,
	Christian König, Philipp Stanner, Jacob Moroni, Xuehao Hu,
	dri-devel, linux-media, linaro-mm-sig, linux-kernel

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@google.com>
> Signed-off-by: Ying Xiao <eagleshaw@google.com>

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


> ---
>  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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted()
  2026-11-17 15:54 ` [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted() Ying Xiao
  2026-11-18  8:05   ` Marek Szyprowski
@ 2026-11-18 13:15   ` Jiri Pirko
  2026-11-18 13:44   ` Jason Gunthorpe
  2 siblings, 0 replies; 4+ messages in thread
From: Jiri Pirko @ 2026-11-18 13:15 UTC (permalink / raw)
  To: Ying Xiao
  Cc: Sumit Semwal, T . J . Mercier, Jiri Pirko, Jason Gunthorpe,
	Marek Szyprowski, Benjamin Gaignard, Brian Starkey, John Stultz,
	Christian König, Philipp Stanner, Jacob Moroni, Xuehao Hu,
	dri-devel, linux-media, linaro-mm-sig, linux-kernel

Tue, Nov 17, 2026 at 04:54:31PM +0100, eagleshaw@google.com 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@google.com>
>Signed-off-by: Ying Xiao <eagleshaw@google.com>

Reviewed-by: Jiri Pirko <jiri@nvidia.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted()
  2026-11-17 15:54 ` [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted() Ying Xiao
  2026-11-18  8:05   ` Marek Szyprowski
  2026-11-18 13:15   ` Jiri Pirko
@ 2026-11-18 13:44   ` Jason Gunthorpe
  2 siblings, 0 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2026-11-18 13:44 UTC (permalink / raw)
  To: Ying Xiao, Aneesh Kumar K.V (Arm)
  Cc: Sumit Semwal, T . J . Mercier, Jiri Pirko, Marek Szyprowski,
	Benjamin Gaignard, Brian Starkey, John Stultz,
	Christian König, Philipp Stanner, Jacob Moroni, Xuehao Hu,
	dri-devel, linux-media, linaro-mm-sig, linux-kernel

On Tue, Nov 17, 2026 at 03:54:31PM +0000, 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@google.com>
> Signed-off-by: Ying Xiao <eagleshaw@google.com>
> ---
>  drivers/dma-buf/heaps/system_heap.c | 24 ++++++++++++++++++++----
>  1 file changed, 20 insertions(+), 4 deletions(-)

This seems fine, but Aneesh is fixing this in a more complete way with
this other series.

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18 13:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20261117155436eucas1p2fa7d030f5557d1c88d943226d65f69ca@eucas1p2.samsung.com>
2026-11-17 15:54 ` [PATCH] dma-buf: heaps: system: zero pages after set_memory_decrypted() Ying Xiao
2026-11-18  8:05   ` Marek Szyprowski
2026-11-18 13:15   ` Jiri Pirko
2026-11-18 13:44   ` Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®