diff mbox series

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

Message ID 20210913033623.296390-1-xinhui.pan@amd.com (mailing list archive)
State New, archived
Headers show
Series [RFC] drm/ttm: Try to check if new ttm man out of bounds during compile | expand

Commit Message

Pan, Xinhui Sept. 13, 2021, 3:36 a.m. UTC
Allow TTM know if vendor set new ttm mananger out of bounds by adding
build_bug_on.

Signed-off-by: xinhui pan <xinhui.pan@amd.com>
---
 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(+)

Comments

Christian König Sept. 13, 2021, 6:35 a.m. UTC | #1
Am 13.09.21 um 05:36 schrieb xinhui pan:
> 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.

>
> Signed-off-by: xinhui pan <xinhui.pan@amd.com>
> ---
>   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(+)
>
> diff --git a/drivers/gpu/drm/ttm/ttm_range_manager.c b/drivers/gpu/drm/ttm/ttm_range_manager.c
> index 03395386e8a7..47e304719b88 100644
> --- a/drivers/gpu/drm/ttm/ttm_range_manager.c
> +++ b/drivers/gpu/drm/ttm/ttm_range_manager.c
> @@ -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
>    *
> diff --git a/include/drm/ttm/ttm_device.h b/include/drm/ttm/ttm_device.h
> index 07d722950d5b..6f23724f5a06 100644
> --- a/include/drm/ttm/ttm_device.h
> +++ b/include/drm/ttm/ttm_device.h
> @@ -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;
>   }
>   
> diff --git a/include/drm/ttm/ttm_range_manager.h b/include/drm/ttm/ttm_range_manager.h
> index 22b6fa42ac20..9250ade54e2c 100644
> --- a/include/drm/ttm/ttm_range_manager.h
> +++ b/include/drm/ttm/ttm_range_manager.h
> @@ -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
Pan, Xinhui Sept. 13, 2021, 6:44 a.m. UTC | #2
[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.
diff mbox series

Patch

diff --git a/drivers/gpu/drm/ttm/ttm_range_manager.c b/drivers/gpu/drm/ttm/ttm_range_manager.c
index 03395386e8a7..47e304719b88 100644
--- a/drivers/gpu/drm/ttm/ttm_range_manager.c
+++ b/drivers/gpu/drm/ttm/ttm_range_manager.c
@@ -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
  *
diff --git a/include/drm/ttm/ttm_device.h b/include/drm/ttm/ttm_device.h
index 07d722950d5b..6f23724f5a06 100644
--- a/include/drm/ttm/ttm_device.h
+++ b/include/drm/ttm/ttm_device.h
@@ -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;
 }
 
diff --git a/include/drm/ttm/ttm_range_manager.h b/include/drm/ttm/ttm_range_manager.h
index 22b6fa42ac20..9250ade54e2c 100644
--- a/include/drm/ttm/ttm_range_manager.h
+++ b/include/drm/ttm/ttm_range_manager.h
@@ -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