Skip to contentSkip to conversation

drm/ttm: Make sure the mapped tt pages are decrypted when needed

PATCH Superseded Zack Rusin 6 participants 11 messages Sep 26, 2023 – Jan 26, 2024

Subsystems (4)
lore
Share & tools
lore

Zack Rusin

author
#0 lore raw

PATCH 1/1 drm/ttm: Make sure the mapped tt pages are decrypted when needed

From: Zack Rusin <[email protected]>

Some drivers require the mapped tt pages to be decrypted. In an ideal world this would have been handled by the dma layer, but the TTM page fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation per order (via ttm_pool_alloc_page) which makes it impossible to just trivially use dma_mmap_attrs. As a result ttm has to be very careful about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

Fixes 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for page-based iomem")
Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Huang Rui <[email protected]>
Cc <[email protected]> # v5.14+
---
 drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++--
 drivers/gpu/drm/ttm/ttm_tt.c      |  7 +++++++
 include/drm/ttm/ttm_tt.h          |  9 ++++++++-
 3 files changed, 26 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +11 -2
index fd9fd3d15101..0b3f4267130c 100644
@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
 	enum ttm_caching caching;
 
 	man = ttm_manager_type(bo->bdev, res->mem_type);
-	caching = man->use_tt ? bo->ttm->caching : res->bus.caching;
+	if (man->use_tt) {
+		caching = bo->ttm->caching;
+		if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
+			tmp = pgprot_decrypted(tmp);
+	} else  {
+		caching = res->bus.caching;
+	}
 
 	return ttm_prot_from_caching(caching, tmp);
 }
@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 		.no_wait_gpu = false
 	};
 	struct ttm_tt *ttm = bo->ttm;
+	struct ttm_resource_manager *man =
+			ttm_manager_type(bo->bdev, bo->resource->mem_type);
 	pgprot_t prot;
 	int ret;
 
@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 	if (ret)
 		return ret;
 
-	if (num_pages == 1 && ttm->caching == ttm_cached) {
+	if (num_pages == 1 && ttm->caching == ttm_cached &&
+	    !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
 		/*
 		 * We're mapping a single page, and the desired
 		 * page protection is consistent with the bo.
drivers/gpu/drm/ttm/ttm_tt.c +7 -0
index e0a77671edd6..02dcb728e29c 100644
@@ -81,6 +81,13 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
 		pr_err("Illegal buffer object type\n");
 		return -EINVAL;
 	}
+	/*
+	 * When using dma_alloc_coherent with memory encryption the
+	 * mapped TT pages need to be decrypted or otherwise the drivers
+	 * will end up sending encrypted mem to the gpu.
+	 */
+	if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))
+		page_flags |= TTM_TT_FLAG_DECRYPTED;
 
 	bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
 	if (unlikely(bo->ttm == NULL))
include/drm/ttm/ttm_tt.h +8 -1
index a4eff85b1f44..2b9d856ff388 100644
@@ -79,6 +79,12 @@ struct ttm_tt {
 	 *   page_flags = TTM_TT_FLAG_EXTERNAL |
 	 *		  TTM_TT_FLAG_EXTERNAL_MAPPABLE;
 	 *
+	 * TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be marked as
+	 * not encrypted. The framework will try to match what the dma layer
+	 * is doing, but note that it is a little fragile because ttm page
+	 * fault handling abuses the DMA api a bit and dma_map_attrs can't be
+	 * used to assure pgprot always matches.
+	 *
 	 * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is
 	 * set by TTM after ttm_tt_populate() has successfully returned, and is
 	 * then unset when TTM calls ttm_tt_unpopulate().
@@ -87,8 +93,9 @@ struct ttm_tt {
 #define TTM_TT_FLAG_ZERO_ALLOC		BIT(1)
 #define TTM_TT_FLAG_EXTERNAL		BIT(2)
 #define TTM_TT_FLAG_EXTERNAL_MAPPABLE	BIT(3)
+#define TTM_TT_FLAG_DECRYPTED		BIT(4)
 
-#define TTM_TT_FLAG_PRIV_POPULATED	BIT(4)
+#define TTM_TT_FLAG_PRIV_POPULATED	BIT(5)
 	uint32_t page_flags;
 	/** @num_pages: Number of pages in the page array. */
 	uint32_t num_pages;
Signature · 1 line
2.39.2
#1 lore raw
kernel test robot noticed the following build errors:

Hi Zack,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.6-rc3 next-20230926]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Zack-Rusin/drm-ttm-Make-sure-the-mapped-tt-pages-are-decrypted-when-needed/20230926-120619
base:   linus/master
patch link:    https://lore.kernel.org/r/20230926040359.3040017-1-zack%40kde.org
patch subject: [PATCH] drm/ttm: Make sure the mapped tt pages are decrypted when needed
config: mips-allyesconfig (https://download.01.org/0day-ci/archive/20230926/[email protected]/config)
compiler: mips-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230926/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/gpu/drm/ttm/ttm_tt.c: In function 'ttm_tt_create':

>> drivers/gpu/drm/ttm/ttm_tt.c:89:41: error: implicit declaration of function 'cc_platform_has' [-Werror=implicit-function-declaration]
      89 |         if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))
         |                                         ^~~~~~~~~~~~~~~
>> drivers/gpu/drm/ttm/ttm_tt.c:89:57: error: 'CC_ATTR_MEM_ENCRYPT' undeclared (first use in this function)
      89 |         if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))
         |                                                         ^~~~~~~~~~~~~~~~~~~

drivers/gpu/drm/ttm/ttm_tt.c:89:57: note: each undeclared identifier is reported only once for each function it appears in
cc1: some warnings being treated as errors

vim +/cc_platform_has +89 drivers/gpu/drm/ttm/ttm_tt.c

    56	
    57	/*
    58	 * Allocates a ttm structure for the given BO.
    59	 */
    60	int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
    61	{
    62		struct ttm_device *bdev = bo->bdev;
    63		uint32_t page_flags = 0;
    64	
    65		dma_resv_assert_held(bo->base.resv);
    66	
    67		if (bo->ttm)
    68			return 0;
    69	
    70		switch (bo->type) {
    71		case ttm_bo_type_device:
    72			if (zero_alloc)
    73				page_flags |= TTM_TT_FLAG_ZERO_ALLOC;
    74			break;
    75		case ttm_bo_type_kernel:
    76			break;
    77		case ttm_bo_type_sg:
    78			page_flags |= TTM_TT_FLAG_EXTERNAL;
    79			break;
    80		default:
    81			pr_err("Illegal buffer object type\n");
    82			return -EINVAL;
    83		}
    84		/*
    85		 * When using dma_alloc_coherent with memory encryption the
    86		 * mapped TT pages need to be decrypted or otherwise the drivers
    87		 * will end up sending encrypted mem to the gpu.
    88		 */
  > 89		if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))
    90			page_flags |= TTM_TT_FLAG_DECRYPTED;
    91	
    92		bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
    93		if (unlikely(bo->ttm == NULL))
    94			return -ENOMEM;
    95	
    96		WARN_ON(bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE &&
    97			!(bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL));
    98	
    99		return 0;

100 }
101

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#2 lore raw

PATCH v2 1/1 drm/ttm: Make sure the mapped tt pages are decrypted when needed

In reply to Zack Rusin

From: Zack Rusin <[email protected]>

Some drivers require the mapped tt pages to be decrypted. In an ideal world this would have been handled by the dma layer, but the TTM page fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation per order (via ttm_pool_alloc_page) which makes it impossible to just trivially use dma_mmap_attrs. As a result ttm has to be very careful about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h

Fixes 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for page-based iomem")
Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Huang Rui <[email protected]>
Cc <[email protected]> # v5.14+
---
 drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++--
 drivers/gpu/drm/ttm/ttm_tt.c      |  8 ++++++++
 include/drm/ttm/ttm_tt.h          |  9 ++++++++-
 3 files changed, 27 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +11 -2
index fd9fd3d15101..0b3f4267130c 100644
@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
 	enum ttm_caching caching;
 
 	man = ttm_manager_type(bo->bdev, res->mem_type);
-	caching = man->use_tt ? bo->ttm->caching : res->bus.caching;
+	if (man->use_tt) {
+		caching = bo->ttm->caching;
+		if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
+			tmp = pgprot_decrypted(tmp);
+	} else  {
+		caching = res->bus.caching;
+	}
 
 	return ttm_prot_from_caching(caching, tmp);
 }
@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 		.no_wait_gpu = false
 	};
 	struct ttm_tt *ttm = bo->ttm;
+	struct ttm_resource_manager *man =
+			ttm_manager_type(bo->bdev, bo->resource->mem_type);
 	pgprot_t prot;
 	int ret;
 
@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 	if (ret)
 		return ret;
 
-	if (num_pages == 1 && ttm->caching == ttm_cached) {
+	if (num_pages == 1 && ttm->caching == ttm_cached &&
+	    !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
 		/*
 		 * We're mapping a single page, and the desired
 		 * page protection is consistent with the bo.
drivers/gpu/drm/ttm/ttm_tt.c +8 -0
index e0a77671edd6..e4966e2c988d 100644
@@ -31,6 +31,7 @@
 
 #define pr_fmt(fmt) "[TTM] " fmt
 
+#include <linux/cc_platform.h>
 #include <linux/sched.h>
 #include <linux/shmem_fs.h>
 #include <linux/file.h>
@@ -81,6 +82,13 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
 		pr_err("Illegal buffer object type\n");
 		return -EINVAL;
 	}
+	/*
+	 * When using dma_alloc_coherent with memory encryption the
+	 * mapped TT pages need to be decrypted or otherwise the drivers
+	 * will end up sending encrypted mem to the gpu.
+	 */
+	if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))
+		page_flags |= TTM_TT_FLAG_DECRYPTED;
 
 	bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
 	if (unlikely(bo->ttm == NULL))
include/drm/ttm/ttm_tt.h +8 -1
index a4eff85b1f44..2b9d856ff388 100644
@@ -79,6 +79,12 @@ struct ttm_tt {
 	 *   page_flags = TTM_TT_FLAG_EXTERNAL |
 	 *		  TTM_TT_FLAG_EXTERNAL_MAPPABLE;
 	 *
+	 * TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be marked as
+	 * not encrypted. The framework will try to match what the dma layer
+	 * is doing, but note that it is a little fragile because ttm page
+	 * fault handling abuses the DMA api a bit and dma_map_attrs can't be
+	 * used to assure pgprot always matches.
+	 *
 	 * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is
 	 * set by TTM after ttm_tt_populate() has successfully returned, and is
 	 * then unset when TTM calls ttm_tt_unpopulate().
@@ -87,8 +93,9 @@ struct ttm_tt {
 #define TTM_TT_FLAG_ZERO_ALLOC		BIT(1)
 #define TTM_TT_FLAG_EXTERNAL		BIT(2)
 #define TTM_TT_FLAG_EXTERNAL_MAPPABLE	BIT(3)
+#define TTM_TT_FLAG_DECRYPTED		BIT(4)
 
-#define TTM_TT_FLAG_PRIV_POPULATED	BIT(4)
+#define TTM_TT_FLAG_PRIV_POPULATED	BIT(5)
 	uint32_t page_flags;
 	/** @num_pages: Number of pages in the page array. */
 	uint32_t num_pages;
Signature · 1 line
2.39.2
reviewer Reviewed-by on v2 1/1
#3 lore raw

Hi, Zack

Quoted 88 lines · Zack Rusin

From: Zack Rusin <[email protected]>

Some drivers require the mapped tt pages to be decrypted. In an ideal world this would have been handled by the dma layer, but the TTM page fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation per order (via ttm_pool_alloc_page) which makes it impossible to just trivially use dma_mmap_attrs. As a result ttm has to be very careful about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h

Fixes 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for page-based iomem")
Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Huang Rui <[email protected]>
Cc <[email protected]> # v5.14+
---
  drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++--
  drivers/gpu/drm/ttm/ttm_tt.c      |  8 ++++++++
  include/drm/ttm/ttm_tt.h          |  9 ++++++++-
  3 files changed, 27 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +11 -2
index fd9fd3d15101..0b3f4267130c 100644
@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
  	enum ttm_caching caching;
 
  	man = ttm_manager_type(bo->bdev, res->mem_type);
-	caching = man->use_tt ? bo->ttm->caching : res->bus.caching;
+	if (man->use_tt) {
+		caching = bo->ttm->caching;
+		if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
+			tmp = pgprot_decrypted(tmp);
+	} else  {
+		caching = res->bus.caching;
+	}
 
  	return ttm_prot_from_caching(caching, tmp);
  }
@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  		.no_wait_gpu = false
  	};
  	struct ttm_tt *ttm = bo->ttm;
+	struct ttm_resource_manager *man =
+			ttm_manager_type(bo->bdev, bo->resource->mem_type);
  	pgprot_t prot;
  	int ret;
 
@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  	if (ret)
  		return ret;
 
-	if (num_pages == 1 && ttm->caching == ttm_cached) {
+	if (num_pages == 1 && ttm->caching == ttm_cached &&
+	    !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
  		/*
  		 * We're mapping a single page, and the desired
  		 * page protection is consistent with the bo.
drivers/gpu/drm/ttm/ttm_tt.c +1 -0
index e0a77671edd6..e4966e2c988d 100644
@@ -31,6 +31,7 @@
 
  #define pr_fmt(fmt) "[TTM] " fmt
 
+#include <linux/cc_platform.h>
  #include <linux/sched.h>
  #include <linux/shmem_fs.h>
  #include <linux/file.h>

drivers/gpu/drm/ttm/ttm_tt.c int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc) · line 90

Quoted 9 lines
drivers/gpu/drm/ttm/ttm_tt.c +6 -0
@@ -81,6 +82,13 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
  		pr_err("Illegal buffer object type\n");
  		return -EINVAL;
  	}
+	/*
+	 * When using dma_alloc_coherent with memory encryption the
+	 * mapped TT pages need to be decrypted or otherwise the drivers
+	 * will end up sending encrypted mem to the gpu.
+	 */
+	if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))

You need to use CC_ATTR_GUEST_MEM_ENCRYPT here rather than CC_ATTR_MEM_ENCRYPT to avoid touching and breaking the SME case and only fix the SEV / SEV-ES case. I'd also hold off the stable inclusion until it's completely verified that this doesn't break anything because if it does, I suspect all hell will break loose.

With that said, for the functionality

But I think this needs a wider Ack at the ttm / drm level for the approach taken.

/Thomas.

Quoted 32 lines
drivers/gpu/drm/ttm/ttm_tt.c +1 -0
+		page_flags |= TTM_TT_FLAG_DECRYPTED;
 
  	bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
  	if (unlikely(bo->ttm == NULL))
include/drm/ttm/ttm_tt.h +8 -1
index a4eff85b1f44..2b9d856ff388 100644
@@ -79,6 +79,12 @@ struct ttm_tt {
  	 *   page_flags = TTM_TT_FLAG_EXTERNAL |
  	 *		  TTM_TT_FLAG_EXTERNAL_MAPPABLE;
  	 *
+	 * TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be marked as
+	 * not encrypted. The framework will try to match what the dma layer
+	 * is doing, but note that it is a little fragile because ttm page
+	 * fault handling abuses the DMA api a bit and dma_map_attrs can't be
+	 * used to assure pgprot always matches.
+	 *
  	 * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is
  	 * set by TTM after ttm_tt_populate() has successfully returned, and is
  	 * then unset when TTM calls ttm_tt_unpopulate().
@@ -87,8 +93,9 @@ struct ttm_tt {
  #define TTM_TT_FLAG_ZERO_ALLOC		BIT(1)
  #define TTM_TT_FLAG_EXTERNAL		BIT(2)
  #define TTM_TT_FLAG_EXTERNAL_MAPPABLE	BIT(3)
+#define TTM_TT_FLAG_DECRYPTED		BIT(4)
 
-#define TTM_TT_FLAG_PRIV_POPULATED	BIT(4)
+#define TTM_TT_FLAG_PRIV_POPULATED	BIT(5)
  	uint32_t page_flags;
  	/** @num_pages: Number of pages in the page array. */
  	uint32_t num_pages;
#4 lore raw
Quoted 128 lines · Thomas Hellström

Hi, Zack

Quoted 110 lines · Zack Rusin

From: Zack Rusin <[email protected]>

Some drivers require the mapped tt pages to be decrypted. In an ideal
world this would have been handled by the dma layer, but the TTM page
fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation
per order (via ttm_pool_alloc_page) which makes it impossible to just
trivially use dma_mmap_attrs. As a result ttm has to be very careful
about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations
for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h

Fixes 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for

page-based iomem")
Cc: Christian König <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Huang Rui <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: <[email protected]> # v5.14+
---
  drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++-- drivers/gpu/drm/ttm/ttm_tt.c      |  8 ++++++++ include/drm/ttm/ttm_tt.h          |  9 ++++++++-

  3 files changed, 27 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +0 -0
index fd9fd3d15101..0b3f4267130c 100644
@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object
 *bo, struct ttm_resource *res,
         enum ttm_caching caching;


        man = ttm_manager_type(bo->bdev, res->mem_type); -       caching = man->use_tt ? bo->ttm->caching : res-

Quoted 1 line

bus.caching;

+       if (man->use_tt) {
+               caching = bo->ttm->caching;
+               if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED) +                       tmp = pgprot_decrypted(tmp);
+       } else  {
+               caching = res->bus.caching;
+       }

        return ttm_prot_from_caching(caching, tmp);
  }

drivers/gpu/drm/ttm/ttm_bo_util.c +0 -0
@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct
 ttm_buffer_object *bo,

                .no_wait_gpu = false
        };
        struct ttm_tt *ttm = bo->ttm;
+       struct ttm_resource_manager *man =
+                       ttm_manager_type(bo->bdev, bo->resource-

Quoted 1 line

mem_type);

        pgprot_t prot;
        int ret;

drivers/gpu/drm/ttm/ttm_bo_util.c +0 -0
@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct
 ttm_buffer_object *bo,
         if (ret)
                 return ret;


-       if (num_pages == 1 && ttm->caching == ttm_cached) { +       if (num_pages == 1 && ttm->caching == ttm_cached && +           !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
                /*
                 * We're mapping a single page, and the desired
                 * page protection is consistent with the bo.

drivers/gpu/drm/ttm/ttm_tt.c +0 -0
index e0a77671edd6..e4966e2c988d 100644
@@ -31,6 +31,7 @@


  #define pr_fmt(fmt) "[TTM] " fmt

+#include <linux/cc_platform.h>
  #include <linux/sched.h>
  #include <linux/shmem_fs.h>
  #include <linux/file.h>

drivers/gpu/drm/ttm/ttm_tt.c +3 -0
@@ -81,6 +82,13 @@ int ttm_tt_create(struct ttm_buffer_object *bo,
 bool zero_alloc)
                 pr_err("Illegal buffer object type\n");
                 return -EINVAL;
         }
+       /*
+        * When using dma_alloc_coherent with memory encryption the
+        * mapped TT pages need to be decrypted or otherwise the
Quoted 5 lines

drivers
+        * will end up sending encrypted mem to the gpu. +        */
+       if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))

You need to use CC_ATTR_GUEST_MEM_ENCRYPT here rather than CC_ATTR_MEM_ENCRYPT to avoid touching and breaking the SME case and only
fix the SEV / SEV-ES case. I'd also hold off the stable inclusion until
it's completely verified that this doesn't break anything because if it
does, I suspect all hell will break loose.

Quoted 8 lines

With that said, for the functionality

But I think this needs a wider Ack at the ttm / drm level for the approach taken.

/Thomas.

FWIW, I think that if TTM_TT_FLAG_DECRYPTED is set, it should be possible to add a debug WARN_ON_ONCE() if the first PTE of the dma page's kernel virtual address does not use a decrypted pgprot_t. One way of accessing the PTEs in a platform-generic fashion is apply_to_page_range().

/Thomas

Quoted 39 lines
Quoted 39 lines

+               page_flags |= TTM_TT_FLAG_DECRYPTED;
        bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags); if (unlikely(bo->ttm == NULL))

include/drm/ttm/ttm_tt.h +1 -0
index a4eff85b1f44..2b9d856ff388 100644
@@ -79,6 +79,12 @@ struct ttm_tt {
          *   page_flags = TTM_TT_FLAG_EXTERNAL |
          *                TTM_TT_FLAG_EXTERNAL_MAPPABLE;
          *
+        * TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be

marked as
+        * not encrypted. The framework will try to match what the dma layer
+        * is doing, but note that it is a little fragile because ttm page
+        * fault handling abuses the DMA api a bit and dma_map_attrs can't be
+        * used to assure pgprot always matches.
+        *
         * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is
         * set by TTM after ttm_tt_populate() has successfully returned, and is
         * then unset when TTM calls ttm_tt_unpopulate().

include/drm/ttm/ttm_tt.h +1 -0
@@ -87,8 +93,9 @@ struct ttm_tt {
   #define TTM_TT_FLAG_ZERO_ALLOC                BIT(1)
   #define TTM_TT_FLAG_EXTERNAL          BIT(2)
   #define TTM_TT_FLAG_EXTERNAL_MAPPABLE BIT(3)
+#define TTM_TT_FLAG_DECRYPTED          BIT(4)

include/drm/ttm/ttm_tt.h +1 -1
-#define TTM_TT_FLAG_PRIV_POPULATED     BIT(4)
+#define TTM_TT_FLAG_PRIV_POPULATED     BIT(5)

        uint32_t page_flags;
        /** @num_pages: Number of pages in the page array. */ uint32_t num_pages;

#5 lore raw
Quoted 138 lines · Thomas Hellström

!! External Email

Quoted 135 lines · Thomas Hellström

Hi, Zack

Quoted 110 lines · Zack Rusin

From: Zack Rusin <[email protected]>

Some drivers require the mapped tt pages to be decrypted. In an ideal
world this would have been handled by the dma layer, but the TTM page
fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation
per order (via ttm_pool_alloc_page) which makes it impossible to just
trivially use dma_mmap_attrs. As a result ttm has to be very careful
about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations
for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h

Fixes 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for

page-based iomem")
Cc: Christian König <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Huang Rui <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: <[email protected]> # v5.14+
---
  drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++-- drivers/gpu/drm/ttm/ttm_tt.c      |  8 ++++++++ include/drm/ttm/ttm_tt.h          |  9 ++++++++-

  3 files changed, 27 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +7 -1
index fd9fd3d15101..0b3f4267130c 100644
@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object
 *bo, struct ttm_resource *res,
         enum ttm_caching caching;
 
         man = ttm_manager_type(bo->bdev, res->mem_type);
-       caching = man->use_tt ? bo->ttm->caching : res-
 > bus.caching;
+       if (man->use_tt) {
+               caching = bo->ttm->caching;
+               if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
+                       tmp = pgprot_decrypted(tmp);
+       } else  {
+               caching = res->bus.caching;
+       }
 

        return ttm_prot_from_caching(caching, tmp); }

drivers/gpu/drm/ttm/ttm_bo_util.c +0 -0
@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct
 ttm_buffer_object *bo,

                .no_wait_gpu = false
        };
        struct ttm_tt *ttm = bo->ttm;
+       struct ttm_resource_manager *man =
+                       ttm_manager_type(bo->bdev, bo->resource-

Quoted 1 line

mem_type);

        pgprot_t prot;
        int ret;

drivers/gpu/drm/ttm/ttm_bo_util.c +2 -1
@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct
 ttm_buffer_object *bo,
         if (ret)
                 return ret;
 
-       if (num_pages == 1 && ttm->caching == ttm_cached) {
+       if (num_pages == 1 && ttm->caching == ttm_cached &&
+           !(man->use_tt && (ttm->page_flags &
 TTM_TT_FLAG_DECRYPTED))) {
                 /*

                 * We're mapping a single page, and the desired
                 * page protection is consistent with the bo.

drivers/gpu/drm/ttm/ttm_tt.c +4 -0
index e0a77671edd6..e4966e2c988d 100644
@@ -31,6 +31,7 @@
 
   #define pr_fmt(fmt) "[TTM] " fmt
 
+#include <linux/cc_platform.h>
   #include <linux/sched.h>
   #include <linux/shmem_fs.h>
   #include <linux/file.h>
@@ -81,6 +82,13 @@ int ttm_tt_create(struct ttm_buffer_object *bo,
 bool zero_alloc)
                 pr_err("Illegal buffer object type\n");
                 return -EINVAL;
         }
+       /*
+        * When using dma_alloc_coherent with memory encryption the
+        * mapped TT pages need to be decrypted or otherwise the
Quoted 5 lines

drivers
+        * will end up sending encrypted mem to the gpu. +        */
+       if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))

You need to use CC_ATTR_GUEST_MEM_ENCRYPT here rather than CC_ATTR_MEM_ENCRYPT to avoid touching and breaking the SME case and only
fix the SEV / SEV-ES case. I'd also hold off the stable inclusion until
it's completely verified that this doesn't break anything because if it
does, I suspect all hell will break loose.

With that said, for the functionality

But I think this needs a wider Ack at the ttm / drm level for the approach taken.

Quoted 7 lines
Quoted 1 line

/Thomas.

FWIW, I think that if TTM_TT_FLAG_DECRYPTED is set, it should be possible to add a debug WARN_ON_ONCE() if the first PTE of the dma page's kernel virtual address does not use a decrypted pgprot_t. One way of accessing the PTEs in a platform-generic fashion is apply_to_page_range().

Good point.

Another, trivial solution to that problem of possible regression would simply be introducing:

#define TTM_DEVICE_USE_DMA_ALLOC          BIT(0)
#define TTM_DEVICE_USE_GFP_DMA32          BIT(1)
#define TTM_DEVICE_USE_DECRYPTED_SYS_MEM  BIT(2)

and changing ttm_device_init from:

int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
struct device *dev, struct address_space *mapping, struct drm_vma_offset_manager *vma_manager,
bool use_dma_alloc, bool use_dma32);
to:
int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
struct device *dev, struct address_space *mapping, struct drm_vma_offset_manager *vma_manager,
u32 use_flags);

The driver should have a lot clearer picture of whether TTM_DEVICE_USE_DECRYPTED_SYS_MEM should be used. That change requires porting the drivers to the new ttm_device_init (which is trivial) but guarantees no regressions simply by virture of having vmwgfx use TTM_DEVICE_USE_DECRYPTED_SYS_MEM only (at least initially, I imagine at least qxl would need it as well).

Christian, any thoughts?

z

#6 lore raw
Quoted 165 lines · Zack Rusin
Quoted 137 lines · Thomas Hellström

!! External Email

Quoted 134 lines · Thomas Hellström

Hi, Zack

Quoted 110 lines · Zack Rusin

From: Zack Rusin <[email protected]>

Some drivers require the mapped tt pages to be decrypted. In an ideal
world this would have been handled by the dma layer, but the TTM page
fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation
per order (via ttm_pool_alloc_page) which makes it impossible to just
trivially use dma_mmap_attrs. As a result ttm has to be very careful
about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations
for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h

Fixes 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for

page-based iomem")
Cc: Christian König <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Huang Rui <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: <[email protected]> # v5.14+

---
   drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++--
   drivers/gpu/drm/ttm/ttm_tt.c      |  8 ++++++++
   include/drm/ttm/ttm_tt.h          |  9 ++++++++-
   3 files changed, 27 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +7 -1
index fd9fd3d15101..0b3f4267130c 100644
@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object
 *bo, struct ttm_resource *res,
         enum ttm_caching caching;
 
         man = ttm_manager_type(bo->bdev, res->mem_type);
-       caching = man->use_tt ? bo->ttm->caching : res-
 > bus.caching;
+       if (man->use_tt) {
+               caching = bo->ttm->caching;
+               if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
+                       tmp = pgprot_decrypted(tmp);
+       } else  {
+               caching = res->bus.caching;
+       }
 

        return ttm_prot_from_caching(caching, tmp); }

drivers/gpu/drm/ttm/ttm_bo_util.c +2 -0
@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct
 ttm_buffer_object *bo,
                 .no_wait_gpu = false
         };
         struct ttm_tt *ttm = bo->ttm;
+       struct ttm_resource_manager *man =
+                       ttm_manager_type(bo->bdev, bo->resource-
 > mem_type);
         pgprot_t prot;

        int ret;

drivers/gpu/drm/ttm/ttm_bo_util.c +2 -1
@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct
 ttm_buffer_object *bo,
         if (ret)
                 return ret;
 
-       if (num_pages == 1 && ttm->caching == ttm_cached) {
+       if (num_pages == 1 && ttm->caching == ttm_cached &&
+           !(man->use_tt && (ttm->page_flags &
 TTM_TT_FLAG_DECRYPTED))) {
                 /*

                 * We're mapping a single page, and the desired
                 * page protection is consistent with the bo.

drivers/gpu/drm/ttm/ttm_tt.c +4 -0
index e0a77671edd6..e4966e2c988d 100644
@@ -31,6 +31,7 @@
 
   #define pr_fmt(fmt) "[TTM] " fmt
 
+#include <linux/cc_platform.h>
   #include <linux/sched.h>
   #include <linux/shmem_fs.h>
   #include <linux/file.h>
@@ -81,6 +82,13 @@ int ttm_tt_create(struct ttm_buffer_object *bo,
 bool zero_alloc)
                 pr_err("Illegal buffer object type\n");
                 return -EINVAL;
         }
+       /*
+        * When using dma_alloc_coherent with memory encryption the
+        * mapped TT pages need to be decrypted or otherwise the
Quoted 5 lines

drivers
+        * will end up sending encrypted mem to the gpu. +        */
+       if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_MEM_ENCRYPT))

You need to use CC_ATTR_GUEST_MEM_ENCRYPT here rather than CC_ATTR_MEM_ENCRYPT to avoid touching and breaking the SME case and only
fix the SEV / SEV-ES case. I'd also hold off the stable inclusion until
it's completely verified that this doesn't break anything because if it
does, I suspect all hell will break loose.

With that said, for the functionality

But I think this needs a wider Ack at the ttm / drm level for the approach taken.

Quoted 6 lines
Quoted 1 line

/Thomas.

FWIW, I think that if TTM_TT_FLAG_DECRYPTED is set, it should be possible to add a debug WARN_ON_ONCE() if the first PTE of the dma page's kernel virtual address does not use a decrypted pgprot_t. One way of accessing the PTEs in a platform-generic fashion is apply_to_page_range().

Good point.

Another, trivial solution to that problem of possible regression would simply be introducing:

#define TTM_DEVICE_USE_DMA_ALLOC          BIT(0)
#define TTM_DEVICE_USE_GFP_DMA32          BIT(1)
#define TTM_DEVICE_USE_DECRYPTED_SYS_MEM  BIT(2)

and changing ttm_device_init from:

int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
struct device *dev, struct address_space *mapping, struct drm_vma_offset_manager *vma_manager,
bool use_dma_alloc, bool use_dma32);
to:
int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
struct device *dev, struct address_space *mapping, struct drm_vma_offset_manager *vma_manager,
u32 use_flags);

Quoted 5 lines

The driver should have a lot clearer picture of whether TTM_DEVICE_USE_DECRYPTED_SYS_MEM should be used. That change requires porting the drivers to the new ttm_device_init (which is trivial) but guarantees no regressions simply by virture of having vmwgfx use TTM_DEVICE_USE_DECRYPTED_SYS_MEM only (at least initially, I imagine at least qxl would need it as well).

I've been thinking along those lines as well. But the current direction appears to be to hide all of the encryption interaction in the dma layer, so when / once we do it right, those driver overrides will probably cause even more grief.

/Thomas

Quoted 4 lines

Christian, any thoughts?

z

#7 lore raw

PATCH v3 1/1 drm/ttm: Make sure the mapped tt pages are decrypted when needed

In reply to Thomas Hellström

Some drivers require the mapped tt pages to be decrypted. In an ideal world this would have been handled by the dma layer, but the TTM page fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation per order (via ttm_pool_alloc_page) which makes it impossible to just trivially use dma_mmap_attrs. As a result ttm has to be very careful about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h
v3: Use CC_ATTR_GUEST_MEM_ENCRYPT instead of CC_ATTR_MEM_ENCRYPT to limit the scope to guests and log when memory decryption is enabled.

Fixes 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for page-based iomem")
Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Huang Rui <[email protected]>
Cc <[email protected]> # v5.14+
---
 drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++--
 drivers/gpu/drm/ttm/ttm_tt.c      | 12 ++++++++++++
 include/drm/ttm/ttm_tt.h          |  9 ++++++++-
 3 files changed, 31 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +11 -2
index fd9fd3d15101..0b3f4267130c 100644
@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
 	enum ttm_caching caching;
 
 	man = ttm_manager_type(bo->bdev, res->mem_type);
-	caching = man->use_tt ? bo->ttm->caching : res->bus.caching;
+	if (man->use_tt) {
+		caching = bo->ttm->caching;
+		if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
+			tmp = pgprot_decrypted(tmp);
+	} else  {
+		caching = res->bus.caching;
+	}
 
 	return ttm_prot_from_caching(caching, tmp);
 }
@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 		.no_wait_gpu = false
 	};
 	struct ttm_tt *ttm = bo->ttm;
+	struct ttm_resource_manager *man =
+			ttm_manager_type(bo->bdev, bo->resource->mem_type);
 	pgprot_t prot;
 	int ret;
 
@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 	if (ret)
 		return ret;
 
-	if (num_pages == 1 && ttm->caching == ttm_cached) {
+	if (num_pages == 1 && ttm->caching == ttm_cached &&
+	    !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
 		/*
 		 * We're mapping a single page, and the desired
 		 * page protection is consistent with the bo.
drivers/gpu/drm/ttm/ttm_tt.c +12 -0
index d978dc539a9b..578a7c37f00b 100644
@@ -31,11 +31,13 @@
 
 #define pr_fmt(fmt) "[TTM] " fmt
 
+#include <linux/cc_platform.h>
 #include <linux/sched.h>
 #include <linux/shmem_fs.h>
 #include <linux/file.h>
 #include <linux/module.h>
 #include <drm/drm_cache.h>
+#include <drm/drm_device.h>
 #include <drm/drm_util.h>
 #include <drm/ttm/ttm_bo.h>
 #include <drm/ttm/ttm_tt.h>
@@ -61,6 +63,7 @@ static atomic_long_t ttm_dma32_pages_allocated;
 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
 {
 	struct ttm_device *bdev = bo->bdev;
+	struct drm_device *ddev = bo->base.dev;
 	uint32_t page_flags = 0;
 
 	dma_resv_assert_held(bo->base.resv);
@@ -82,6 +85,15 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
 		pr_err("Illegal buffer object type\n");
 		return -EINVAL;
 	}
+	/*
+	 * When using dma_alloc_coherent with memory encryption the
+	 * mapped TT pages need to be decrypted or otherwise the drivers
+	 * will end up sending encrypted mem to the gpu.
+	 */
+	if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
+		page_flags |= TTM_TT_FLAG_DECRYPTED;
+		drm_info(ddev, "TT memory decryption enabled.");
+	}
 
 	bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
 	if (unlikely(bo->ttm == NULL))
include/drm/ttm/ttm_tt.h +8 -1
index a4eff85b1f44..2b9d856ff388 100644
@@ -79,6 +79,12 @@ struct ttm_tt {
 	 *   page_flags = TTM_TT_FLAG_EXTERNAL |
 	 *		  TTM_TT_FLAG_EXTERNAL_MAPPABLE;
 	 *
+	 * TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be marked as
+	 * not encrypted. The framework will try to match what the dma layer
+	 * is doing, but note that it is a little fragile because ttm page
+	 * fault handling abuses the DMA api a bit and dma_map_attrs can't be
+	 * used to assure pgprot always matches.
+	 *
 	 * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is
 	 * set by TTM after ttm_tt_populate() has successfully returned, and is
 	 * then unset when TTM calls ttm_tt_unpopulate().
@@ -87,8 +93,9 @@ struct ttm_tt {
 #define TTM_TT_FLAG_ZERO_ALLOC		BIT(1)
 #define TTM_TT_FLAG_EXTERNAL		BIT(2)
 #define TTM_TT_FLAG_EXTERNAL_MAPPABLE	BIT(3)
+#define TTM_TT_FLAG_DECRYPTED		BIT(4)
 
-#define TTM_TT_FLAG_PRIV_POPULATED	BIT(4)
+#define TTM_TT_FLAG_PRIV_POPULATED	BIT(5)
 	uint32_t page_flags;
 	/** @num_pages: Number of pages in the page array. */
 	uint32_t num_pages;
Signature · 1 line
2.40.1
#8 lore raw
Quoted 18 lines · Zack Rusin

Some drivers require the mapped tt pages to be decrypted. In an ideal world this would have been handled by the dma layer, but the TTM page fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation per order (via ttm_pool_alloc_page) which makes it impossible to just trivially use dma_mmap_attrs. As a result ttm has to be very careful about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Quoted 5 lines

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h
v3: Use CC_ATTR_GUEST_MEM_ENCRYPT instead of CC_ATTR_MEM_ENCRYPT to limit the scope to guests and log when memory decryption is enabled.

Sorry, this also got a bit lost during the s/VMware/Broadcom/ transition. It seems to be pretty safe in general now. I wasn't able to find a really clean way of adding a warn_once when pte's don't match as suggested by Thomas, but I did add a quick log to at least point out in the logs that we've enabled memory decryption in tt.

z

#9 lore raw
In reply to Zack Rusin
Quoted 18 lines · Zack Rusin

Some drivers require the mapped tt pages to be decrypted. In an ideal world this would have been handled by the dma layer, but the TTM page fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation per order (via ttm_pool_alloc_page) which makes it impossible to just trivially use dma_mmap_attrs. As a result ttm has to be very careful about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Quoted 5 lines

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h
v3: Use CC_ATTR_GUEST_MEM_ENCRYPT instead of CC_ATTR_MEM_ENCRYPT to limit the scope to guests and log when memory decryption is enabled.

Hi, Christian.

Gentle ping on that one. This is probably the cleanest we can get this code. Can we land this or is there anything else you'd like to see?

z

#10 lore raw

Am 26.01.24 um 06:10 schrieb Zack Rusin:

Quoted 20 lines · Zack Rusin
Quoted 19 lines

Some drivers require the mapped tt pages to be decrypted. In an ideal world this would have been handled by the dma layer, but the TTM page fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation per order (via ttm_pool_alloc_page) which makes it impossible to just trivially use dma_mmap_attrs. As a result ttm has to be very careful about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

Quoted 7 lines
Quoted 3 lines

v2: Explicitly include cc_platform.h
v3: Use CC_ATTR_GUEST_MEM_ENCRYPT instead of CC_ATTR_MEM_ENCRYPT to limit the scope to guests and log when memory decryption is enabled.

Hi, Christian.

Gentle ping on that one. This is probably the cleanest we can get this code. Can we land this or is there anything else you'd like to see?

Sorry for the delay.

I'm not too familiar with the technical background, so I can't 100% judge if this is correct or not.

But if it works for you I think we should give it a try, feel free to add my Acked-by and push upstream through whatever branch you like.

Regards,
Christian.

Quoted 2 lines

z