Skip to content

Commit 883f309

Browse files
Jie1zhangalexdeucher
authored andcommitted
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) Reviewed-by: Christian König <[email protected]> Suggested-by: Lijo Lazar <[email protected]> Signed-off-by: Jesse Zhang <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent 33cc891 commit 883f309

3 files changed

Lines changed: 7 additions & 6 deletions

File tree

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
708708
*/
709709
const s64 us_upper_bound = 200000;
710710

711-
if (!adev->mm_stats.log2_max_MBps) {
711+
if ((!adev->mm_stats.log2_max_MBps) || !ttm_resource_manager_used(&adev->mman.vram_mgr.manager)) {
712712
*max_bytes = 0;
713713
*max_vis_bytes = 0;
714714
return;

drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
758758
ui64 = atomic64_read(&adev->num_vram_cpu_page_faults);
759759
return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
760760
case AMDGPU_INFO_VRAM_USAGE:
761-
ui64 = ttm_resource_manager_usage(&adev->mman.vram_mgr.manager);
761+
ui64 = ttm_resource_manager_used(&adev->mman.vram_mgr.manager) ?
762+
ttm_resource_manager_usage(&adev->mman.vram_mgr.manager) : 0;
762763
return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
763764
case AMDGPU_INFO_VIS_VRAM_USAGE:
764765
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)
804805
mem.vram.usable_heap_size = adev->gmc.real_vram_size -
805806
atomic64_read(&adev->vram_pin_size) -
806807
AMDGPU_VM_RESERVED_VRAM;
807-
mem.vram.heap_usage =
808-
ttm_resource_manager_usage(vram_man);
808+
mem.vram.heap_usage = ttm_resource_manager_used(&adev->mman.vram_mgr.manager) ?
809+
ttm_resource_manager_usage(vram_man) : 0;
809810
mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4;
810811

811812
mem.cpu_accessible_vram.total_heap_size =

drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ static int amdgpu_virt_write_vf2pf_data(struct amdgpu_device *adev)
598598
vf2pf_info->driver_cert = 0;
599599
vf2pf_info->os_info.all = 0;
600600

601-
vf2pf_info->fb_usage =
602-
ttm_resource_manager_usage(&adev->mman.vram_mgr.manager) >> 20;
601+
vf2pf_info->fb_usage = ttm_resource_manager_used(&adev->mman.vram_mgr.manager) ?
602+
ttm_resource_manager_usage(&adev->mman.vram_mgr.manager) >> 20 : 0;
603603
vf2pf_info->fb_vis_usage =
604604
amdgpu_vram_mgr_vis_usage(&adev->mman.vram_mgr) >> 20;
605605
vf2pf_info->fb_size = adev->gmc.real_vram_size >> 20;

0 commit comments

Comments
 (0)