Skip to contentSkip to conversation

drm/ttm: add a BUG_ON in ttm_set_driver_manager when array bounds

PATCH In review Guchun Chen 3 participants 4 messages Sep 10, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/ttm: add a BUG_ON in ttm_set_driver_manager when array bounds

Vendor will define their own memory types on top of TTM_PL_PRIV, but call ttm_set_driver_manager directly without checking mem_type value when setting up memory manager. So add such check to aware the case when array bounds.

---
 include/drm/ttm/ttm_device.h | 1 +
 1 file changed, 1 insertion(+)
include/drm/ttm/ttm_device.h +1 -0
index 7a0f561c57ee..24ad76ca8022 100644
@@ -308,6 +308,7 @@ ttm_manager_type(struct ttm_device *bdev, int mem_type)
 static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
 					  struct ttm_resource_manager *manager)
 {
+	BUG_ON(type >= TTM_NUM_MEM_TYPES);
 	bdev->man_drv[type] = manager;
 }
 
Signature · 1 line
2.17.1
#1 lore raw

[AMD Official Use Only]

looks good to me.
But maybe build_bug_on works too and more reasonable to detect such wrong usage.
________________________________
From: Chen, Guchun <[email protected]>
Sent: Friday, September 10, 2021 12:30:14 PM
To: [email protected] <[email protected]>; [email protected] <[email protected]>; Koenig, Christian <[email protected]>; Pan, Xinhui <[email protected]>; Deucher, Alexander <[email protected]>
Cc: Chen, Guchun <[email protected]>; Shi, Leslie <[email protected]>
Subject: [PATCH] drm/ttm: add a BUG_ON in ttm_set_driver_manager when array bounds

Vendor will define their own memory types on top of TTM_PL_PRIV, but call ttm_set_driver_manager directly without checking mem_type value when setting up memory manager. So add such check to aware the case when array bounds.

---
 include/drm/ttm/ttm_device.h | 1 +
 1 file changed, 1 insertion(+)
include/drm/ttm/ttm_device.h +1 -0
index 7a0f561c57ee..24ad76ca8022 100644
@@ -308,6 +308,7 @@ ttm_manager_type(struct ttm_device *bdev, int mem_type)
 static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
                                           struct ttm_resource_manager *manager)
 {
+       BUG_ON(type >= TTM_NUM_MEM_TYPES);
         bdev->man_drv[type] = manager;
 }
 

--
2.17.1

#2 lore raw

Yeah, that's a good point.

If build_bug_on() doesn't works for some reason then we at least need to lower this to a WARN_ON.

A BUG_ON() is only justified if we prevent strong data corruption with it or note a NULL pointer earlier on or similar.

Regards,
Christian.

Am 10.09.21 um 06:36 schrieb Pan, Xinhui:

Quoted 39 lines

[AMD Official Use Only]

looks good to me.
But maybe build_bug_on works too and more reasonable to detect such wrong usage.
------------------------------------------------------------------------
*From:* Chen, Guchun <[email protected]>
*Sent:* Friday, September 10, 2021 12:30:14 PM
*To:* [email protected] <[email protected]>; [email protected] <[email protected]>; Koenig, Christian <[email protected]>; Pan, Xinhui <[email protected]>; Deucher, Alexander <[email protected]> *Cc:* Chen, Guchun <[email protected]>; Shi, Leslie <[email protected]>
*Subject:* [PATCH] drm/ttm: add a BUG_ON in ttm_set_driver_manager when array bounds
Vendor will define their own memory types on top of TTM_PL_PRIV,
but call ttm_set_driver_manager directly without checking mem_type
value when setting up memory manager. So add such check to aware
the case when array bounds.

---
 include/drm/ttm/ttm_device.h | 1 +

 1 file changed, 1 insertion(+)
include/drm/ttm/ttm_device.h +1 -0
index 7a0f561c57ee..24ad76ca8022 100644
@@ -308,6 +308,7 @@ ttm_manager_type(struct ttm_device *bdev, int 
 mem_type)
  static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
                                            struct ttm_resource_manager *manager)
  {
+       BUG_ON(type >= TTM_NUM_MEM_TYPES);
          bdev->man_drv[type] = manager;
  }
Signature · 2 lines
-- 
2.17.1
#3 lore raw

[Public]

Hi Christian and Xinhui,

Thanks for your suggestion. The cause is I saw data corruption in several proprietary use cases. BUILD_BUG_ON will have build variation per gcc difference?

Anyway, WARN_ON is fine to me, and I will send a new patch set soon to address this.

Regards,
Guchun

From: Koenig, Christian <[email protected]>
Sent: Friday, September 10, 2021 2:37 PM
To: Pan, Xinhui <[email protected]>; [email protected]; [email protected]; Deucher, Alexander <[email protected]>; Chen, Guchun <[email protected]>
Cc: Shi, Leslie <[email protected]>
Subject: Re: [PATCH] drm/ttm: add a BUG_ON in ttm_set_driver_manager when array bounds

Yeah, that's a good point.

If build_bug_on() doesn't works for some reason then we at least need to lower this to a WARN_ON.

A BUG_ON() is only justified if we prevent strong data corruption with it or note a NULL pointer earlier on or similar.

Regards,
Christian.
Am 10.09.21 um 06:36 schrieb Pan, Xinhui:

[AMD Official Use Only]

looks good to me.
But maybe build_bug_on works too and more reasonable to detect such wrong usage.
________________________________
From: Chen, Guchun <[email protected]><mailto:[email protected]>
Sent: Friday, September 10, 2021 12:30:14 PM
To: [email protected]<mailto:[email protected]> <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]> <[email protected]><mailto:[email protected]>; Koenig, Christian <[email protected]><mailto:[email protected]>; Pan, Xinhui <[email protected]><mailto:[email protected]>; Deucher, Alexander <[email protected]><mailto:[email protected]>
Cc: Chen, Guchun <[email protected]><mailto:[email protected]>; Shi, Leslie <[email protected]><mailto:[email protected]>
Subject: [PATCH] drm/ttm: add a BUG_ON in ttm_set_driver_manager when array bounds

Vendor will define their own memory types on top of TTM_PL_PRIV, but call ttm_set_driver_manager directly without checking mem_type value when setting up memory manager. So add such check to aware the case when array bounds.

---
 include/drm/ttm/ttm_device.h | 1 +
 1 file changed, 1 insertion(+)
include/drm/ttm/ttm_device.h +1 -0
index 7a0f561c57ee..24ad76ca8022 100644
@@ -308,6 +308,7 @@ ttm_manager_type(struct ttm_device *bdev, int mem_type)
 static inline void ttm_set_driver_manager(struct ttm_device *bdev, int type,
                                           struct ttm_resource_manager *manager)
 {
+       BUG_ON(type >= TTM_NUM_MEM_TYPES);
         bdev->man_drv[type] = manager;
 }
 

--
2.17.1