Skip to contentSkip to conversation

drm/ttm: Try to check if new ttm man out of bounds during compile

RFC PATCH Superseded xinhui pan 2 participants 3 messages Sep 13, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

xinhui pan

author
#0 lore raw

RFC PATCH 1/1 drm/ttm: Try to check if new ttm man out of bounds during compile

Allow TTM know if vendor set new ttm mananger out of bounds by adding build_bug_on.

---
 drivers/gpu/drm/ttm/ttm_range_manager.c |  2 ++
 include/drm/ttm/ttm_device.h            |  3 +++
 include/drm/ttm/ttm_range_manager.h     | 10 ++++++++++
 3 files changed, 15 insertions(+)
drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
index 03395386e8a7..47e304719b88 100644
@@ -127,6 +127,8 @@ static const struct ttm_resource_manager_func ttm_range_manager_func = {
 	.debug = ttm_range_man_debug
 };
 
+#undef ttm_range_man_init
+#undef ttm_range_man_fini
 /**
  * ttm_range_man_init
  *
include/drm/ttm/ttm_device.h +3 -0
index 07d722950d5b..6f23724f5a06 100644
@@ -285,12 +285,15 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
 static inline struct ttm_resource_manager *
 ttm_manager_type(struct ttm_device *bdev, int mem_type)
 {
+	BUILD_BUG_ON(__builtin_constant_p(mem_type)
+		     && mem_type >= TTM_NUM_MEM_TYPES);
 	return bdev->man_drv[mem_type];
 }
 
 static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
 					  struct ttm_resource_manager *manager)
 {
+	BUILD_BUG_ON(__builtin_constant_p(type) && type >= TTM_NUM_MEM_TYPES);
 	bdev->man_drv[type] = manager;
 }
 
include/drm/ttm/ttm_range_manager.h +10 -0
index 22b6fa42ac20..9250ade54e2c 100644
@@ -38,5 +38,15 @@ int ttm_range_man_init(struct ttm_device *bdev,
 		       unsigned long p_size);
 int ttm_range_man_fini(struct ttm_device *bdev,
 		       unsigned type);
+#define ttm_range_man_init(bdev, type, use_tt, size) ({	\
+	BUILD_BUG_ON(__builtin_constant_p(type)		\
+			&& type >= TTM_NUM_MEM_TYPES);	\
+	ttm_range_man_init(bdev, type, use_tt, size);	\
+})
+#define ttm_range_man_fini(bdev, type) ({		\
+	BUILD_BUG_ON(__builtin_constant_p(type)		\
+			&& type >= TTM_NUM_MEM_TYPES);	\
+	ttm_range_man_fini(bdev, type);			\
+})
 
 #endif
Signature · 1 line
2.25.1
#1 lore raw

Am 13.09.21 um 05:36 schrieb xinhui pan:

Quoted 2 lines

Allow TTM know if vendor set new ttm mananger out of bounds by adding build_bug_on.

I really like the part in the inline functions, but the wrappers around the ttm_range_man_init/fini look a bit awkward of hand.

Christian.

Quoted 61 lines
---
  drivers/gpu/drm/ttm/ttm_range_manager.c |  2 ++
  include/drm/ttm/ttm_device.h            |  3 +++
  include/drm/ttm/ttm_range_manager.h     | 10 ++++++++++
  3 files changed, 15 insertions(+)
drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
index 03395386e8a7..47e304719b88 100644
@@ -127,6 +127,8 @@ static const struct ttm_resource_manager_func ttm_range_manager_func = {
  	.debug = ttm_range_man_debug
  };
 
+#undef ttm_range_man_init
+#undef ttm_range_man_fini
  /**
   * ttm_range_man_init
   *
include/drm/ttm/ttm_device.h +3 -0
index 07d722950d5b..6f23724f5a06 100644
@@ -285,12 +285,15 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
  static inline struct ttm_resource_manager *
  ttm_manager_type(struct ttm_device *bdev, int mem_type)
  {
+	BUILD_BUG_ON(__builtin_constant_p(mem_type)
+		     && mem_type >= TTM_NUM_MEM_TYPES);
  	return bdev->man_drv[mem_type];
  }
 
  static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
  					  struct ttm_resource_manager *manager)
  {
+	BUILD_BUG_ON(__builtin_constant_p(type) && type >= TTM_NUM_MEM_TYPES);
  	bdev->man_drv[type] = manager;
  }
 
include/drm/ttm/ttm_range_manager.h +10 -0
index 22b6fa42ac20..9250ade54e2c 100644
@@ -38,5 +38,15 @@ int ttm_range_man_init(struct ttm_device *bdev,
  		       unsigned long p_size);
  int ttm_range_man_fini(struct ttm_device *bdev,
  		       unsigned type);
+#define ttm_range_man_init(bdev, type, use_tt, size) ({	\
+	BUILD_BUG_ON(__builtin_constant_p(type)		\
+			&& type >= TTM_NUM_MEM_TYPES);	\
+	ttm_range_man_init(bdev, type, use_tt, size);	\
+})
+#define ttm_range_man_fini(bdev, type) ({		\
+	BUILD_BUG_ON(__builtin_constant_p(type)		\
+			&& type >= TTM_NUM_MEM_TYPES);	\
+	ttm_range_man_fini(bdev, type);			\
+})
 
  #endif
#2 lore raw

[AMD Official Use Only]

ttm_range_man_init/fini are exported. Someone else might use it by find_symbol. I just want to not break things.

Developer usually compile the whole kernel. So add a checked version of ttm_range_man_init/fini by the wrappers.

________________________________________
发件人: Christian König <[email protected]>
发送时间: 2021年9月13日 14:35
收件人: Pan, Xinhui; [email protected]
抄送: Koenig, Christian; [email protected]; [email protected]; Chen, Guchun 主题: Re: [RFC PATCH] drm/ttm: Try to check if new ttm man out of bounds during compile

Am 13.09.21 um 05:36 schrieb xinhui pan:

Quoted 2 lines

Allow TTM know if vendor set new ttm mananger out of bounds by adding build_bug_on.

I really like the part in the inline functions, but the wrappers around the ttm_range_man_init/fini look a bit awkward of hand.

Christian.

Quoted 61 lines
---
  drivers/gpu/drm/ttm/ttm_range_manager.c |  2 ++
  include/drm/ttm/ttm_device.h            |  3 +++
  include/drm/ttm/ttm_range_manager.h     | 10 ++++++++++
  3 files changed, 15 insertions(+)
drivers/gpu/drm/ttm/ttm_range_manager.c +2 -0
index 03395386e8a7..47e304719b88 100644
@@ -127,6 +127,8 @@ static const struct ttm_resource_manager_func ttm_range_manager_func = {
      .debug = ttm_range_man_debug
  };
 
+#undef ttm_range_man_init
+#undef ttm_range_man_fini
  /**
   * ttm_range_man_init
   *
include/drm/ttm/ttm_device.h +3 -0
index 07d722950d5b..6f23724f5a06 100644
@@ -285,12 +285,15 @@ int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
  static inline struct ttm_resource_manager *
  ttm_manager_type(struct ttm_device *bdev, int mem_type)
  {
+     BUILD_BUG_ON(__builtin_constant_p(mem_type)
+                  && mem_type >= TTM_NUM_MEM_TYPES);
      return bdev->man_drv[mem_type];
  }
 
  static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
                                        struct ttm_resource_manager *manager)
  {
+     BUILD_BUG_ON(__builtin_constant_p(type) && type >= TTM_NUM_MEM_TYPES);
      bdev->man_drv[type] = manager;
  }
 
include/drm/ttm/ttm_range_manager.h +10 -0
index 22b6fa42ac20..9250ade54e2c 100644
@@ -38,5 +38,15 @@ int ttm_range_man_init(struct ttm_device *bdev,
                     unsigned long p_size);
  int ttm_range_man_fini(struct ttm_device *bdev,
                     unsigned type);
+#define ttm_range_man_init(bdev, type, use_tt, size) ({      \
+     BUILD_BUG_ON(__builtin_constant_p(type)         \
+                     && type >= TTM_NUM_MEM_TYPES);  \
+     ttm_range_man_init(bdev, type, use_tt, size);   \
+})
+#define ttm_range_man_fini(bdev, type) ({            \
+     BUILD_BUG_ON(__builtin_constant_p(type)         \
+                     && type >= TTM_NUM_MEM_TYPES);  \
+     ttm_range_man_fini(bdev, type);                 \
+})
 
  #endif