drm/amdgpu: Fix NULL pointer dereference in VRAM logic for APU devices
Subsystems (2)
PATCH v4 1/1 drm/amdgpu: Fix NULL pointer dereference in VRAM logic for APU devices
Previously, APU platforms (and other scenarios with uninitialized VRAM managers) triggered a NULL pointer dereference in ttm_resource_manager_usage(). The root cause is not that the struct ttm_resource_manager *man pointer itself is NULL, but that man->bdev (the backing device pointer within the manager) remains uninitialized (NULL) on APUs—since APUs lack dedicated VRAM and do not fully set up VRAM manager structures. When ttm_resource_manager_usage() attempts to acquire man->bdev->lru_lock, it dereferences the NULL man->bdev, leading to a kernel OOPS.
1. **amdgpu_cs.c**: Extend the existing bandwidth control check in amdgpu_cs_get_threshold_for_moves() to include a check for ttm_resource_manager_used(). If the manager is not used (uninitialized bdev), return 0 for migration thresholds immediately—skipping VRAM-specific logic that would trigger the NULL dereference.
2. **amdgpu_kms.c**: Update the AMDGPU_INFO_VRAM_USAGE ioctl and memory info reporting to use a conditional: if the manager is used, return the real VRAM usage; otherwise, return 0. This avoids accessing man->bdev when it is NULL.
3. **amdgpu_virt.c**: Modify the vf2pf (virtual function to physical function) data write path. Use ttm_resource_manager_used() to check validity: if the manager is usable, calculate fb_usage from VRAM usage; otherwise, set fb_usage to 0 (APUs have no discrete framebuffer to report).
This approach is more robust than APU-specific checks because it:
- Works for all scenarios where the VRAM manager is uninitialized (not just APUs),
- Aligns with TTM's design by using its native helper function,
- Preserves correct behavior for discrete GPUs (which have fully initialized man->bdev and pass the ttm_resource_manager_used() check).
v4: use ttm_resource_manager_used(&adev->mman.vram_mgr.manager) instead of checking the adev->gmc.is_app_apu flag (Christian)
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 7 ++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -1
@@ -709,7 +709,7 @@ static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,*/ const s64 us_upper_bound =200000 ; -if (!adev->mm_stats.log2_max_MBps) { +if ((!adev->mm_stats.log2_max_MBps) || (!ttm_resource_manager_used (&adev->mman.vram_mgr.manager))) { *max_bytes =0 ; *max_vis_bytes =0 ;return ;
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4 -3
@@ -758,7 +758,8 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) ui64 =atomic64_read (&adev->num_vram_cpu_page_faults);return copy_to_user (out, &ui64,min (size,8u )) ? -EFAULT :0 ;case AMDGPU_INFO_VRAM_USAGE : - ui64 =ttm_resource_manager_usage (&adev->mman.vram_mgr.manager); + ui64 =ttm_resource_manager_used (&adev->mman.vram_mgr.manager) ? +ttm_resource_manager_usage (&adev->mman.vram_mgr.manager) :0 ;return copy_to_user (out, &ui64,min (size,8u )) ? -EFAULT :0 ;case AMDGPU_INFO_VIS_VRAM_USAGE : ui64 =amdgpu_vram_mgr_vis_usage (&adev->mman.vram_mgr);
@@ -804,8 +805,8 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) mem.vram.usable_heap_size = adev->gmc.real_vram_size -atomic64_read (&adev->vram_pin_size) -AMDGPU_VM_RESERVED_VRAM ; - mem.vram.heap_usage = -ttm_resource_manager_usage (vram_man); + mem.vram.heap_usage =ttm_resource_manager_used (&adev->mman.vram_mgr.manager) ? +ttm_resource_manager_usage (vram_man) :0 ; mem.vram.max_allocation = mem.vram.usable_heap_size *3 /4 ; mem.cpu_accessible_vram.total_heap_size =
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +2 -2
@@ -598,8 +598,8 @@ static int amdgpu_virt_write_vf2pf_data(struct amdgpu_device *adev) vf2pf_info->driver_cert =0 ; vf2pf_info->os_info.all =0 ; - vf2pf_info->fb_usage = -ttm_resource_manager_usage (&adev->mman.vram_mgr.manager) >>20 ; + vf2pf_info->fb_usage =ttm_resource_manager_used (&adev->mman.vram_mgr.manager) ? +ttm_resource_manager_usage (&adev->mman.vram_mgr.manager) >>20 :0 ; vf2pf_info->fb_vis_usage =amdgpu_vram_mgr_vis_usage (&adev->mman.vram_mgr) >>20 ; vf2pf_info->fb_size = adev->gmc.real_vram_size >>20 ;
Signature · 1 line
2.49.0
Christian König
maintainerQuoted 45 lines · Jesse.Zhang
Previously, APU platforms (and other scenarios with uninitialized VRAM managers) triggered a NULL pointer dereference in
ttm_resource_manager_usage(). The root cause is not that thestruct ttm_resource_manager *manpointer itself is NULL, but thatman->bdev(the backing device pointer within the manager) remains uninitialized (NULL) on APUs—since APUs lack dedicated VRAM and do not fully set up VRAM manager structures. Whenttm_resource_manager_usage()attempts to acquireman->bdev->lru_lock, it dereferences the NULLman->bdev, leading to a kernel OOPS.1. **amdgpu_cs.c**: Extend the existing bandwidth control check in
amdgpu_cs_get_threshold_for_moves()to include a check forttm_resource_manager_used(). If the manager is not used (uninitializedbdev), return 0 for migration thresholds immediately—skipping VRAM-specific logic that would trigger the NULL dereference.2. **amdgpu_kms.c**: Update the
AMDGPU_INFO_VRAM_USAGEioctl and memory info reporting to use a conditional: if the manager is used, return the real VRAM usage; otherwise, return 0. This avoids accessingman->bdevwhen it is NULL.3. **amdgpu_virt.c**: Modify the vf2pf (virtual function to physical function) data write path. Use
ttm_resource_manager_used()to check validity: if the manager is usable, calculatefb_usagefrom VRAM usage; otherwise, setfb_usageto 0 (APUs have no discrete framebuffer to report).This approach is more robust than APU-specific checks because it:
- Works for all scenarios where the VRAM manager is uninitialized (not just APUs),
- Aligns with TTM's design by using its native helper function,
- Preserves correct behavior for discrete GPUs (which have fully initializedman->bdevand pass thettm_resource_manager_used()check).v4: use
ttm_resource_manager_used(&adev->mman.vram_mgr.manager)instead of checking theadev->gmc.is_app_apuflag (Christian)Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 7 ++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev, · line 712
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1
-1@@ -709,7 +709,7 @@ static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,*/ const s64 us_upper_bound =200000 ; -if (!adev->mm_stats.log2_max_MBps) { +if ((!adev->mm_stats.log2_max_MBps) || (!ttm_resource_manager_used (&adev->mman.vram_mgr.manager))) {
The extra () can be dropped, ! binds stronger than ||.
Please use checkpatch.pl to double check that the line is now not to long.
With that done Reviewed-by: Christian König <[email protected]>.
Regards,
Christian.
Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +0
-0*max_bytes =0 ; *max_vis_bytes =0 ;return ;drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4
-3@@ -758,7 +758,8 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) ui64 =atomic64_read (&adev->num_vram_cpu_page_faults);return copy_to_user (out, &ui64,min (size,8u )) ? -EFAULT :0 ;case AMDGPU_INFO_VRAM_USAGE : - ui64 =ttm_resource_manager_usage (&adev->mman.vram_mgr.manager); + ui64 =ttm_resource_manager_used (&adev->mman.vram_mgr.manager) ? +ttm_resource_manager_usage (&adev->mman.vram_mgr.manager) :0 ;return copy_to_user (out, &ui64,min (size,8u )) ? -EFAULT :0 ;case AMDGPU_INFO_VIS_VRAM_USAGE : ui64 =amdgpu_vram_mgr_vis_usage (&adev->mman.vram_mgr);@@ -804,8 +805,8 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) mem.vram.usable_heap_size = adev->gmc.real_vram_size -atomic64_read (&adev->vram_pin_size) -AMDGPU_VM_RESERVED_VRAM ; - mem.vram.heap_usage = -ttm_resource_manager_usage (vram_man); + mem.vram.heap_usage =ttm_resource_manager_used (&adev->mman.vram_mgr.manager) ? +ttm_resource_manager_usage (vram_man) :0 ; mem.vram.max_allocation = mem.vram.usable_heap_size *3 /4 ; mem.cpu_accessible_vram.total_heap_size =drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +2
-2@@ -598,8 +598,8 @@ static int amdgpu_virt_write_vf2pf_data(struct amdgpu_device *adev) vf2pf_info->driver_cert =0 ; vf2pf_info->os_info.all =0 ; - vf2pf_info->fb_usage = -ttm_resource_manager_usage (&adev->mman.vram_mgr.manager) >>20 ; + vf2pf_info->fb_usage =ttm_resource_manager_used (&adev->mman.vram_mgr.manager) ? +ttm_resource_manager_usage (&adev->mman.vram_mgr.manager) >>20 :0 ; vf2pf_info->fb_vis_usage =amdgpu_vram_mgr_vis_usage (&adev->mman.vram_mgr) >>20 ; vf2pf_info->fb_size = adev->gmc.real_vram_size >>20 ;
[AMD Official Use Only - AMD Internal Distribution Only]
Quoted 92 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Jesse.Zhang
Sent: Monday, October 13, 2025 1:31 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
Subject: [PATCH v4] drm/amdgpu: Fix NULL pointer dereference in VRAM logic for APU devicesPreviously, APU platforms (and other scenarios with uninitialized VRAM managers) triggered a NULL pointer dereference in
ttm_resource_manager_usage(). The root cause is not that thestruct ttm_resource_manager *manpointer itself is NULL, but thatman->bdev(the backing device pointer within the manager) remains uninitialized (NULL) on APUs—since APUs lack dedicated VRAM and do not fully set up VRAM manager structures. Whenttm_resource_manager_usage()attempts to acquireman->bdev->lru_lock, it dereferences the NULLman->bdev, leading to a kernel OOPS.1. **amdgpu_cs.c**: Extend the existing bandwidth control check in
amdgpu_cs_get_threshold_for_moves()to include a check forttm_resource_manager_used(). If the manager is not used (uninitializedbdev), return 0 for migration thresholds immediately—skipping VRAM-
specific
logic that would trigger the NULL dereference.2. **amdgpu_kms.c**: Update the
AMDGPU_INFO_VRAM_USAGEioctl and memory info
reporting to use a conditional: if the manager is used, return the real VRAM usage; otherwise, return 0. This avoids accessingman->bdevwhen it is NULL.3. **amdgpu_virt.c**: Modify the vf2pf (virtual function to physical function) data write path. Use
ttm_resource_manager_used()to check validity: if the manager is usable, calculatefb_usagefrom VRAM usage; otherwise, setfb_usageto 0 (APUs have no discrete framebuffer to report).This approach is more robust than APU-specific checks because it:
- Works for all scenarios where the VRAM manager is uninitialized (not just APUs),
- Aligns with TTM's design by using its native helper function,
- Preserves correct behavior for discrete GPUs (which have fully initializedman->bdevand pass thettm_resource_manager_used()check).v4: use
ttm_resource_manager_used(&adev->mman.vram_mgr.manager)instead of checking theadev->gmc.is_app_apuflag (Christian)Signed-off-by Jesse Zhang <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 7 ++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2
-1@@ -709,7 +709,7 @@ static voidamdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev, */ const s64 us_upper_bound =200000 ; -if (!adev->mm_stats.log2_max_MBps) { +if ((!adev->mm_stats.log2_max_MBps) || +(!ttm_resource_manager_used (&adev->mman.vram_mgr.manager))) { *max_bytes =0 ; *max_vis_bytes =0 ;return ;drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1
-1@@ -758,7 +758,8 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) ui64 =atomic64_read (&adev->num_vram_cpu_page_faults);return copy_to_user (out, &ui64,min (size,8u )) ? -EFAULT :0 ; case AMDGPU_INFO_VRAM_USAGE: - ui64 =ttm_resource_manager_usage (&adev- >mman.vram_mgr.manager); + ui64 = ttm_resource_manager_used(&adev-Quoted 1 line
mman.vram_mgr.manager) ?
+ ttm_resource_manager_usage(&adev-
Quoted 1 line
mman.vram_mgr.manager) : 0;
return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; case AMDGPU_INFO_VIS_VRAM_USAGE: ui64 = amdgpu_vram_mgr_vis_usage(&adev-Quoted 1 line
mman.vram_mgr);
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void · line 809
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +0
-2@@ -804,8 +805,8 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) mem.vram.usable_heap_size = adev->gmc.real_vram_size -atomic64_read (&adev->vram_pin_size) -AMDGPU_VM_RESERVED_VRAM ; - mem.vram.heap_usage = -ttm_resource_manager_usage (vram_man);
[lijo]
For this one, probably return much earlier if there is no vram manager usage? Filling in other parameters does not look required.
Thanks,
Lijo
Quoted 30 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1
-0+ mem.vram.heap_usage = ttm_resource_manager_used(&adev-Quoted 1 line
mman.vram_mgr.manager) ?
+
ttm_resource_manager_usage(vram_man): 0;
mem.vram.max_allocation = mem.vram.usable_heap_size * 3 /
4;mem.cpu_accessible_vram.total_heap_size = diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.cb/drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c
index3328ab63376b..f96beb96c75c100644drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +1
-2@@ -598,8 +598,8 @@ static int amdgpu_virt_write_vf2pf_data(struct amdgpu_device *adev) vf2pf_info->driver_cert = 0; vf2pf_info->os_info.all =0 ; - vf2pf_info->fb_usage = -ttm_resource_manager_usage (&adev- >mman.vram_mgr.manager) >> 20; + vf2pf_info->fb_usage = ttm_resource_manager_used(&adev-Quoted 1 line
mman.vram_mgr.manager) ?
+ ttm_resource_manager_usage(&adev-
Quoted 1 line
mman.vram_mgr.manager) >> 20 : 0;
vf2pf_info->fb_vis_usage = amdgpu_vram_mgr_vis_usage(&adev->mman.vram_mgr) >> 20; vf2pf_info->fb_size = adev->gmc.real_vram_size >> 20;--
2.49.0