Skip to contentSkip to conversation

drm/ttm: Add safety check for NULL man->bdev in ttm_resource_manager_usage

PATCH In review Jesse.Zhang 3 participants 3 messages Oct 13, 2025 – Oct 14, 2025

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

PATCH v2 1/1 drm/ttm: Add safety check for NULL man->bdev in ttm_resource_manager_usage

The ttm_resource_manager_usage() function currently assumes man->bdev is non-NULL when accessing man->bdev->lru_lock. However, in scenarios where the resource manager is not fully initialized (e.g., APU platforms that lack dedicated VRAM, or incomplete manager setup),
man->bdev may remain NULL. This leads to a NULL pointer dereference when attempting to acquire the lru_lock, triggering kernel OOPS.

Fix this by adding an explicit safety check for man->bdev before accessing its members:
- Use WARN_ON_ONCE(!man->bdev) to emit a one-time warning (a soft assertion) when man->bdev is NULL. This helps catch invalid usage patterns during debugging without breaking production workflows.
- Return 0 immediately if man->bdev is NULL, as a non-initialized manager cannot have valid resource usage to report.

---
 drivers/gpu/drm/ttm/ttm_resource.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index e2c82ad07eb4..d93d1bef6768 100644
@@ -587,6 +587,9 @@ uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
 {
 	uint64_t usage;
 
+	if (WARN_ON_ONCE(!man->bdev))
+		return 0;
+
 	spin_lock(&man->bdev->lru_lock);
 	usage = man->usage;
 	spin_unlock(&man->bdev->lru_lock);
Signature · 1 line
2.49.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Jesse.Zhang

The ttm_resource_manager_usage() function currently assumes man->bdev is non-NULL when accessing man->bdev->lru_lock. However, in scenarios where the resource manager is not fully initialized (e.g., APU platforms that lack dedicated VRAM, or incomplete manager setup),
man->bdev may remain NULL. This leads to a NULL pointer dereference when attempting to acquire the lru_lock, triggering kernel OOPS.

Fix this by adding an explicit safety check for man->bdev before accessing its members:
- Use WARN_ON_ONCE(!man->bdev) to emit a one-time warning (a soft assertion) when man->bdev is NULL. This helps catch invalid usage patterns during debugging without breaking production workflows.
- Return 0 immediately if man->bdev is NULL, as a non-initialized manager cannot have valid resource usage to report.

@Arun could you push this one to drm-misc-next if nobody objects?

Thanks in advance,
Christian

Quoted 18 lines
---
 drivers/gpu/drm/ttm/ttm_resource.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index e2c82ad07eb4..d93d1bef6768 100644
@@ -587,6 +587,9 @@ uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
 {
 	uint64_t usage;
 
+	if (WARN_ON_ONCE(!man->bdev))
+		return 0;
+
 	spin_lock(&man->bdev->lru_lock);
 	usage = man->usage;
 	spin_unlock(&man->bdev->lru_lock);
#2 lore raw

Hi Christian,

Quoted 9 lines · Christian König
Quoted 8 lines · Jesse.Zhang

The ttm_resource_manager_usage() function currently assumes man->bdev is non-NULL when accessing man->bdev->lru_lock. However, in scenarios where the resource manager is not fully initialized (e.g., APU platforms that lack dedicated VRAM, or incomplete manager setup),
man->bdev may remain NULL. This leads to a NULL pointer dereference when attempting to acquire the lru_lock, triggering kernel OOPS.

Fix this by adding an explicit safety check for man->bdev before accessing its members:
- Use WARN_ON_ONCE(!man->bdev) to emit a one-time warning (a soft assertion) when man->bdev is NULL. This helps catch invalid usage patterns during debugging without breaking production workflows.
- Return 0 immediately if man->bdev is NULL, as a non-initialized manager cannot have valid resource usage to report.

Quoted 6 lines
Quoted 3 lines

@Arun could you push this one to drm-misc-next if nobody objects?

I pushed this patch into drm-misc-next.

Regards,
Arun.

Quoted 22 lines

Thanks in advance,
Christian

Quoted 18 lines
---
  drivers/gpu/drm/ttm/ttm_resource.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index e2c82ad07eb4..d93d1bef6768 100644
@@ -587,6 +587,9 @@ uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
  {
  	uint64_t usage;
 
+	if (WARN_ON_ONCE(!man->bdev))
+		return 0;
+
  	spin_lock(&man->bdev->lru_lock);
  	usage = man->usage;
  	spin_unlock(&man->bdev->lru_lock);