AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] drm/ttm: Add safety check for NULL man->bdev in ttm_resource_manager_usage
@ 2025-10-13  8:58 Jesse.Zhang
  2025-10-13 13:18 ` Christian König
  0 siblings, 1 reply; 3+ messages in thread
From: Jesse.Zhang @ 2025-10-13  8:58 UTC (permalink / raw)
  To: amd-gfx, dri-devel
  Cc: Alexander.Deucher, Christian Koenig, Jesse.Zhang, Lijo Lazar,
	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.

Suggested-by: Christian König <christian.koenig@amd.com>
Suggested-by: Lijo Lazar <lijo.lazar@amd.com>
Signed-off-by: Jesse Zhang <jesse.zhang@amd.com>
---
 drivers/gpu/drm/ttm/ttm_resource.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/gpu/drm/ttm/ttm_resource.c b/drivers/gpu/drm/ttm/ttm_resource.c
index e2c82ad07eb4..d93d1bef6768 100644
--- a/drivers/gpu/drm/ttm/ttm_resource.c
+++ b/drivers/gpu/drm/ttm/ttm_resource.c
@@ -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.49.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] drm/ttm: Add safety check for NULL man->bdev in ttm_resource_manager_usage
  2025-10-13  8:58 [PATCH v2] drm/ttm: Add safety check for NULL man->bdev in ttm_resource_manager_usage Jesse.Zhang
@ 2025-10-13 13:18 ` Christian König
  2025-10-14  8:30   ` Arunpravin Paneer Selvam
  0 siblings, 1 reply; 3+ messages in thread
From: Christian König @ 2025-10-13 13:18 UTC (permalink / raw)
  To: Jesse.Zhang, amd-gfx, dri-devel
  Cc: Alexander.Deucher, Lijo Lazar, Paneer Selvam, Arunpravin

On 13.10.25 10:58, Jesse.Zhang wrote:
> 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.
> 
> Suggested-by: Christian König <christian.koenig@amd.com>
> Suggested-by: Lijo Lazar <lijo.lazar@amd.com>
> Signed-off-by: Jesse Zhang <jesse.zhang@amd.com>

Reviewed-by: Christian König <christian.koenig@amd.com>

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

Thanks in advance,
Christian

> ---
>  drivers/gpu/drm/ttm/ttm_resource.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/gpu/drm/ttm/ttm_resource.c b/drivers/gpu/drm/ttm/ttm_resource.c
> index e2c82ad07eb4..d93d1bef6768 100644
> --- a/drivers/gpu/drm/ttm/ttm_resource.c
> +++ b/drivers/gpu/drm/ttm/ttm_resource.c
> @@ -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);


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] drm/ttm: Add safety check for NULL man->bdev in ttm_resource_manager_usage
  2025-10-13 13:18 ` Christian König
@ 2025-10-14  8:30   ` Arunpravin Paneer Selvam
  0 siblings, 0 replies; 3+ messages in thread
From: Arunpravin Paneer Selvam @ 2025-10-14  8:30 UTC (permalink / raw)
  To: Christian König, Jesse.Zhang, amd-gfx, dri-devel
  Cc: Alexander.Deucher, Lijo Lazar

Hi Christian,

On 10/13/2025 6:48 PM, Christian König wrote:
> On 13.10.25 10:58, Jesse.Zhang wrote:
>> 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.
>>
>> Suggested-by: Christian König <christian.koenig@amd.com>
>> Suggested-by: Lijo Lazar <lijo.lazar@amd.com>
>> Signed-off-by: Jesse Zhang <jesse.zhang@amd.com>
> Reviewed-by: Christian König <christian.koenig@amd.com>
>
> @Arun could you push this one to drm-misc-next if nobody objects?
I pushed this patch into drm-misc-next.

Regards,
Arun.
>
> Thanks in advance,
> Christian
>
>> ---
>>   drivers/gpu/drm/ttm/ttm_resource.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/ttm/ttm_resource.c b/drivers/gpu/drm/ttm/ttm_resource.c
>> index e2c82ad07eb4..d93d1bef6768 100644
>> --- a/drivers/gpu/drm/ttm/ttm_resource.c
>> +++ b/drivers/gpu/drm/ttm/ttm_resource.c
>> @@ -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);


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-14  8:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-13  8:58 [PATCH v2] drm/ttm: Add safety check for NULL man->bdev in ttm_resource_manager_usage Jesse.Zhang
2025-10-13 13:18 ` Christian König
2025-10-14  8:30   ` Arunpravin Paneer Selvam

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox