Skip to contentSkip to conversation

drm/amdgpu: copy debugfs register data outside the GRBM/SRBM locks

PATCH Superseded Mikhail Gavrilov 4 participants 9 messages Sep 30 – Oct 1

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: copy debugfs register data outside the GRBM/SRBM locks

Reading or writing a register through the amdgpu_regs2 debugfs file with GRBM bank selection, which umr does for every banked access (a single "umr -b x x x -r <reg>" is enough, as is the wave dump with "-O halt_waves"), makes lockdep report a circular locking dependency, after which lockdep stays off until reboot:

WARNING: possible circular locking dependency detected
umr/63306 is trying to acquire lock:
ffff8882cf4b8350 (&mm->mmap_lock){++++}-{4:4}, at: __might_fault+0x9b/0x140
but task is already holding lock:
ffff8881aec010d0 (&amdgpu_grbm_lock_key){+.+.}-{4:4}, at: amdgpu_debugfs_regs2_op.isra.0+0x20d/0xd00 [amdgpu]
...

  Chain exists of:
    &mm->mmap_lock --> &amdgpu_srbm_lock_key --> &amdgpu_grbm_lock_key
  ...
   __might_fault+0xc5/0x140
   amdgpu_debugfs_regs2_op.isra.0+0x441/0xd00 [amdgpu]
   full_proxy_write+0x10e/0x1d0
   vfs_write+0x1d0/0xf50
   ksys_write+0x116/0x250

amdgpu_debugfs_regs2_op() and amdgpu_debugfs_process_reg_op() call get_user()/put_user() for every dword while holding grbm_idx_mutex or srbm_mutex, and pm.mutex if requested, so a fault there takes mmap_lock inside those locks. The lock hierarchy declared in amdgpu_lockdep.c since commit 1d0f5838b126
("drm/amdgpu: Add lockdep annotations for lock ordering validation") puts srbm_mutex and grbm_idx_mutex below notifier_lock, which amdgpu_cs_submit() takes with the BO reservations held, and dma_resv_lockdep() orders the reservations inside mmap_lock. So mmap_lock is outside grbm_idx_mutex and srbm_mutex, and these two debugfs paths nest it the other way round.

Copy the user buffer in before any of the locks is taken and the result out after all of them are dropped, as amdgpu_debugfs_wave_read(), amdgpu_debugfs_gpr_read() and amdgpu_debugfs_gprwave_read() already do. A fault on the user buffer of a write now fails with -EFAULT before any register is written, instead of after a partial write.

Fixes 1d0f5838b126 ("drm/amdgpu: Add lockdep annotations for lock ordering validation")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 71 ++++++++++++---------
 1 file changed, 40 insertions(+), 31 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +40 -31
index aca1a8045afa..5618bd3e2610 100644
@@ -121,6 +121,17 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 
 	*pos &= (1UL << 22) - 1;
 
+	/*
+	 * Access the user buffer only while none of the locks below are held:
+	 * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+	 * inside it.
+	 */
+	u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+	if (!read && copy_from_user(data, buf, size))
+		return -EFAULT;
+
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
 	if (r < 0) {
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -152,28 +163,16 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 		mutex_lock(&adev->pm.mutex);
 
 	while (size) {
-		uint32_t value;
-
-		if (read) {
-			value = RREG32(*pos >> 2);
-			r = put_user(value, (uint32_t *)buf);
-		} else {
-			r = get_user(value, (uint32_t *)buf);
-			if (!r)
-				amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value, 0);
-		}
-		if (r) {
-			result = r;
-			goto end;
-		}
+		if (read)
+			data[result >> 2] = RREG32(*pos >> 2);
+		else
+			amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, data[result >> 2], 0);
 
 		result += 4;
-		buf += 4;
 		*pos += 4;
 		size -= 4;
 	}
 
-end:
 	if (use_bank) {
 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0);
 		mutex_unlock(&adev->grbm_idx_mutex);
@@ -188,6 +187,10 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
 	amdgpu_virt_disable_access_debugfs(adev);
+
+	if (read && copy_to_user(buf, data, result))
+		return -EFAULT;
+
 	return result;
 }
 
@@ -238,11 +241,21 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 	struct amdgpu_device *adev = rd->adev;
 	ssize_t result = 0;
 	int r;
-	uint32_t value;
 
 	if (size & 0x3 || offset & 0x3)
 		return -EINVAL;
 
+	/*
+	 * Access the user buffer only while none of the locks below are held:
+	 * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+	 * inside it.
+	 */
+	u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+	if (write_en && copy_from_user(data, buf, size))
+		return -EFAULT;
+
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
 	if (r < 0) {
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -281,24 +294,16 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 		mutex_lock(&adev->pm.mutex);
 
 	while (size) {
-		if (!write_en) {
-			value = RREG32(offset >> 2);
-			r = put_user(value, (uint32_t *)buf);
-		} else {
-			r = get_user(value, (uint32_t *)buf);
-			if (!r)
-				amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value, rd->id.xcc_id);
-		}
-		if (r) {
-			result = r;
-			goto end;
-		}
+		if (!write_en)
+			data[result >> 2] = RREG32(offset >> 2);
+		else
+			amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, data[result >> 2],
+						rd->id.xcc_id);
 		offset += 4;
 		size -= 4;
 		result += 4;
-		buf += 4;
 	}
-end:
+
 	if (rd->id.use_grbm) {
 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, rd->id.xcc_id);
 		mutex_unlock(&adev->grbm_idx_mutex);
@@ -317,6 +322,10 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
 	amdgpu_virt_disable_access_debugfs(adev);
+
+	if (!write_en && copy_to_user(buf, data, result))
+		return -EFAULT;
+
 	return result;
 }
 
Signature · 1 line
2.55.0
#1 lore raw

Tested on Navi 31 (gfx1100), 7.3.0-rc5 at 551c722f4080, with lockdep.

Without this patch the first GRBM-banked access through amdgpu_regs2 produces the report quoted in the commit message: from the write branch when umr halts waves, and from the read branch for a single "umr -b x x x -r '*.gfx1100.regGRBM_STATUS'" on a fresh boot.

With this patch, and with the chain from reservation_ww_class_mutex through &amdgpu_notifier_lock_key down to &amdgpu_grbm_lock_key present in /proc/lockdep, none of the following produced a report, and debug_locks stayed 1:
- the banked read of GRBM_STATUS* above;
- "umr -O halt_waves -wa gfx_0.0.0" (banked SQ_CMD writes);
- writing SCRATCH_REG0 and reading it back through amdgpu_regs2 with GRBM broadcast, and through amdgpu_regs with the bank bits in the offset; both reads returned the value written.

The patch is against 551c722f4080 and also applies to amd-staging-drm-next and next-20260929.

maintainer
#2 lore raw
In reply to Mikhail Gavrilov
Quoted 33 lines · Mikhail Gavrilov

Reading or writing a register through the amdgpu_regs2 debugfs file with GRBM bank selection, which umr does for every banked access (a single "umr -b x x x -r <reg>" is enough, as is the wave dump with "-O halt_waves"), makes lockdep report a circular locking dependency, after which lockdep stays off until reboot:

WARNING: possible circular locking dependency detected
umr/63306 is trying to acquire lock:
ffff8882cf4b8350 (&mm->mmap_lock){++++}-{4:4}, at: __might_fault+0x9b/0x140
but task is already holding lock:
ffff8881aec010d0 (&amdgpu_grbm_lock_key){+.+.}-{4:4}, at: amdgpu_debugfs_regs2_op.isra.0+0x20d/0xd00 [amdgpu]
...

  Chain exists of:
    &mm->mmap_lock --> &amdgpu_srbm_lock_key --> &amdgpu_grbm_lock_key
  ...
   __might_fault+0xc5/0x140
   amdgpu_debugfs_regs2_op.isra.0+0x441/0xd00 [amdgpu]
   full_proxy_write+0x10e/0x1d0
   vfs_write+0x1d0/0xf50
   ksys_write+0x116/0x250

amdgpu_debugfs_regs2_op() and amdgpu_debugfs_process_reg_op() call get_user()/put_user() for every dword while holding grbm_idx_mutex or srbm_mutex, and pm.mutex if requested, so a fault there takes mmap_lock inside those locks. The lock hierarchy declared in amdgpu_lockdep.c since commit 1d0f5838b126
("drm/amdgpu: Add lockdep annotations for lock ordering validation") puts srbm_mutex and grbm_idx_mutex below notifier_lock, which amdgpu_cs_submit() takes with the BO reservations held, and dma_resv_lockdep() orders the reservations inside mmap_lock. So mmap_lock is outside grbm_idx_mutex and srbm_mutex, and these two debugfs paths nest it the other way round.

Quoted 7 lines

Copy the user buffer in before any of the locks is taken and the result out after all of them are dropped, as amdgpu_debugfs_wave_read(), amdgpu_debugfs_gpr_read() and amdgpu_debugfs_gprwave_read() already do. A fault on the user buffer of a write now fails with -EFAULT before any register is written, instead of after a partial write.

Fixes 1d0f5838b126 ("drm/amdgpu: Add lockdep annotations for lock ordering validation")

Well that's not really correct since that patch only makes the problem visible and not introduce it :)

But apart from that rather good catch, Vitalies work to add lockdep annotations pays of.

Quoted 1 line

I don't have time to review this in detail. Vitaly and Tom please take a look at this.

But feel free to add Acked-by: Christian König <[email protected]>

Thanks in advance,
Christian.

Quoted 135 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 71 ++++++++++++---------
 1 file changed, 40 insertions(+), 31 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +40 -31
index aca1a8045afa..5618bd3e2610 100644
@@ -121,6 +121,17 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 
 	*pos &= (1UL << 22) - 1;
 
+	/*
+	 * Access the user buffer only while none of the locks below are held:
+	 * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+	 * inside it.
+	 */
+	u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+	if (!read && copy_from_user(data, buf, size))
+		return -EFAULT;
+
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
 	if (r < 0) {
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -152,28 +163,16 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 		mutex_lock(&adev->pm.mutex);
 
 	while (size) {
-		uint32_t value;
-
-		if (read) {
-			value = RREG32(*pos >> 2);
-			r = put_user(value, (uint32_t *)buf);
-		} else {
-			r = get_user(value, (uint32_t *)buf);
-			if (!r)
-				amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value, 0);
-		}
-		if (r) {
-			result = r;
-			goto end;
-		}
+		if (read)
+			data[result >> 2] = RREG32(*pos >> 2);
+		else
+			amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, data[result >> 2], 0);
 
 		result += 4;
-		buf += 4;
 		*pos += 4;
 		size -= 4;
 	}
 
-end:
 	if (use_bank) {
 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0);
 		mutex_unlock(&adev->grbm_idx_mutex);
@@ -188,6 +187,10 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
 	amdgpu_virt_disable_access_debugfs(adev);
+
+	if (read && copy_to_user(buf, data, result))
+		return -EFAULT;
+
 	return result;
 }
 
@@ -238,11 +241,21 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 	struct amdgpu_device *adev = rd->adev;
 	ssize_t result = 0;
 	int r;
-	uint32_t value;
 
 	if (size & 0x3 || offset & 0x3)
 		return -EINVAL;
 
+	/*
+	 * Access the user buffer only while none of the locks below are held:
+	 * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+	 * inside it.
+	 */
+	u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+	if (write_en && copy_from_user(data, buf, size))
+		return -EFAULT;
+
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
 	if (r < 0) {
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -281,24 +294,16 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 		mutex_lock(&adev->pm.mutex);
 
 	while (size) {
-		if (!write_en) {
-			value = RREG32(offset >> 2);
-			r = put_user(value, (uint32_t *)buf);
-		} else {
-			r = get_user(value, (uint32_t *)buf);
-			if (!r)
-				amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value, rd->id.xcc_id);
-		}
-		if (r) {
-			result = r;
-			goto end;
-		}
+		if (!write_en)
+			data[result >> 2] = RREG32(offset >> 2);
+		else
+			amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, data[result >> 2],
+						rd->id.xcc_id);
 		offset += 4;
 		size -= 4;
 		result += 4;
-		buf += 4;
 	}
-end:
+
 	if (rd->id.use_grbm) {
 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, rd->id.xcc_id);
 		mutex_unlock(&adev->grbm_idx_mutex);
@@ -317,6 +322,10 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
 	amdgpu_virt_disable_access_debugfs(adev);
+
+	if (!write_en && copy_to_user(buf, data, result))
+		return -EFAULT;
+
 	return result;
 }
 
#3 lore raw
Quoted 3 lines · Christian König
Quoted 1 line
Fixes 1d0f5838b126 ("drm/amdgpu: Add lockdep annotations for lock ordering validation")

Well that's not really correct since that patch only makes the problem visible and not introduce it :)

Right. The user copies under the locks come from

566281595037 ("drm/amd/amdgpu: Add bank selection for MMIO debugfs (v3)")

for amdgpu_regs and

37df9560cd3e ("drm/amd/amdgpu: New debugfs interface for MMIO registers (v5)")

for amdgpu_regs2. I'll use those as the Fixes tags in v2, together with your Acked-by, once Tom and Vitaly have had a look.

Thanks,
Mikhail

Acked-by
#4 lore raw

Hi Guys,

I just tried the patch out as positive flow tests (basically make sure umr commands still work before/after) on my gfx12 devel machine and I didn't notice any problems. I've read through the patch and it looks good to me but I'm not super versed in the depths of lockdep. You may add my

Tom

________________________________________
From: Mikhail Gavrilov <[email protected]>
Sent: Thursday, October 1, 2026 02:27
To: Koenig, Christian
Cc: [email protected]; Deucher, Alexander; Prosyak, Vitaly; StDenis, Tom; Mikhail Gavrilov
Subject: Re: [PATCH] drm/amdgpu: copy debugfs register data outside the GRBM/SRBM locks

Quoted 3 lines · Christian König
Quoted 1 line
Fixes 1d0f5838b126 ("drm/amdgpu: Add lockdep annotations for lock ordering validation")

Well that's not really correct since that patch only makes the problem visible and not introduce it :)

Right. The user copies under the locks come from

566281595037 ("drm/amd/amdgpu: Add bank selection for MMIO debugfs (v3)")

for amdgpu_regs and

37df9560cd3e ("drm/amd/amdgpu: New debugfs interface for MMIO registers (v5)")

for amdgpu_regs2. I'll use those as the Fixes tags in v2, together with your Acked-by, once Tom and Vitaly have had a look.

Thanks,
Mikhail

Reviewed-by
#5 lore raw
In reply to Christian König

Mikhail's proposed kvmalloc(size, GFP_KERNEL) correctly stages user data outside the GRBM/SRBM locks, avoiding a potential page fault while holding those locks. However, since size is derived directly from a userspace read or write request, we must validate it before allocating memory in both amdgpu_debugfs_process_reg_op() and amdgpu_debugfs_regs2_op().

Tom, do you agree with this? Will adding a restriction here break any UMR  requests?
Please add a bounds check in each handler after the existing alignment/offset validation and immediately before kvmalloc():

if (size > PAGE_SIZE)
    return -EINVAL;

data = kvmalloc(size, GFP_KERNEL);
if (!data)
    return -ENOMEM;

With that input validation addressed:

Quoted 188 lines · Christian König
Quoted 33 lines · Mikhail Gavrilov

Reading or writing a register through the amdgpu_regs2 debugfs file with GRBM bank selection, which umr does for every banked access (a single "umr -b x x x -r <reg>" is enough, as is the wave dump with "-O halt_waves"), makes lockdep report a circular locking dependency, after which lockdep stays off until reboot:

WARNING: possible circular locking dependency detected
umr/63306 is trying to acquire lock:
ffff8882cf4b8350 (&mm->mmap_lock){++++}-{4:4}, at: __might_fault+0x9b/0x140
but task is already holding lock:
ffff8881aec010d0 (&amdgpu_grbm_lock_key){+.+.}-{4:4}, at: amdgpu_debugfs_regs2_op.isra.0+0x20d/0xd00 [amdgpu]
...

  Chain exists of:
    &mm->mmap_lock --> &amdgpu_srbm_lock_key --> &amdgpu_grbm_lock_key
  ...
   __might_fault+0xc5/0x140
   amdgpu_debugfs_regs2_op.isra.0+0x441/0xd00 [amdgpu]
   full_proxy_write+0x10e/0x1d0
   vfs_write+0x1d0/0xf50
   ksys_write+0x116/0x250

amdgpu_debugfs_regs2_op() and amdgpu_debugfs_process_reg_op() call get_user()/put_user() for every dword while holding grbm_idx_mutex or srbm_mutex, and pm.mutex if requested, so a fault there takes mmap_lock inside those locks. The lock hierarchy declared in amdgpu_lockdep.c since commit 1d0f5838b126
("drm/amdgpu: Add lockdep annotations for lock ordering validation") puts srbm_mutex and grbm_idx_mutex below notifier_lock, which amdgpu_cs_submit() takes with the BO reservations held, and dma_resv_lockdep() orders the reservations inside mmap_lock. So mmap_lock is outside grbm_idx_mutex and srbm_mutex, and these two debugfs paths nest it the other way round.

Quoted 7 lines

Copy the user buffer in before any of the locks is taken and the result out after all of them are dropped, as amdgpu_debugfs_wave_read(), amdgpu_debugfs_gpr_read() and amdgpu_debugfs_gprwave_read() already do. A fault on the user buffer of a write now fails with -EFAULT before any register is written, instead of after a partial write.

Fixes 1d0f5838b126 ("drm/amdgpu: Add lockdep annotations for lock ordering validation")

Well that's not really correct since that patch only makes the problem visible and not introduce it :)

But apart from that rather good catch, Vitalies work to add lockdep annotations pays of.

Quoted 1 line

I don't have time to review this in detail. Vitaly and Tom please take a look at this.

But feel free to add Acked-by: Christian König <[email protected]>

Thanks in advance,
Christian.

Quoted 135 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 71 ++++++++++++---------
 1 file changed, 40 insertions(+), 31 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +40 -31
index aca1a8045afa..5618bd3e2610 100644
@@ -121,6 +121,17 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 
 	*pos &= (1UL << 22) - 1;
 
+	/*
+	 * Access the user buffer only while none of the locks below are held:
+	 * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+	 * inside it.
+	 */
+	u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+	if (!read && copy_from_user(data, buf, size))
+		return -EFAULT;
+
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
 	if (r < 0) {
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -152,28 +163,16 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 		mutex_lock(&adev->pm.mutex);
 
 	while (size) {
-		uint32_t value;
-
-		if (read) {
-			value = RREG32(*pos >> 2);
-			r = put_user(value, (uint32_t *)buf);
-		} else {
-			r = get_user(value, (uint32_t *)buf);
-			if (!r)
-				amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value, 0);
-		}
-		if (r) {
-			result = r;
-			goto end;
-		}
+		if (read)
+			data[result >> 2] = RREG32(*pos >> 2);
+		else
+			amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, data[result >> 2], 0);
 
 		result += 4;
-		buf += 4;
 		*pos += 4;
 		size -= 4;
 	}
 
-end:
 	if (use_bank) {
 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0);
 		mutex_unlock(&adev->grbm_idx_mutex);
@@ -188,6 +187,10 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
 	amdgpu_virt_disable_access_debugfs(adev);
+
+	if (read && copy_to_user(buf, data, result))
+		return -EFAULT;
+
 	return result;
 }
 
@@ -238,11 +241,21 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 	struct amdgpu_device *adev = rd->adev;
 	ssize_t result = 0;
 	int r;
-	uint32_t value;
 
 	if (size & 0x3 || offset & 0x3)
 		return -EINVAL;
 
+	/*
+	 * Access the user buffer only while none of the locks below are held:
+	 * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+	 * inside it.
+	 */
+	u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+	if (write_en && copy_from_user(data, buf, size))
+		return -EFAULT;
+
 	r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
 	if (r < 0) {
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -281,24 +294,16 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 		mutex_lock(&adev->pm.mutex);
 
 	while (size) {
-		if (!write_en) {
-			value = RREG32(offset >> 2);
-			r = put_user(value, (uint32_t *)buf);
-		} else {
-			r = get_user(value, (uint32_t *)buf);
-			if (!r)
-				amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value, rd->id.xcc_id);
-		}
-		if (r) {
-			result = r;
-			goto end;
-		}
+		if (!write_en)
+			data[result >> 2] = RREG32(offset >> 2);
+		else
+			amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, data[result >> 2],
+						rd->id.xcc_id);
 		offset += 4;
 		size -= 4;
 		result += 4;
-		buf += 4;
 	}
-end:
+
 	if (rd->id.use_grbm) {
 		amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, rd->id.xcc_id);
 		mutex_unlock(&adev->grbm_idx_mutex);
@@ -317,6 +322,10 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
 	amdgpu_virt_disable_access_debugfs(adev);
+
+	if (!write_en && copy_to_user(buf, data, result))
+		return -EFAULT;
+
 	return result;
 }
 
Reviewed-by Vitaly Prosyak
#6 lore raw

There is minimal checking on top (e.g. size & 3 and offset & 3) which is required by both the hardware and umr. I agree the PAGE_SIZE check is a good idea.

umr really should only be doing 4 byte accesses at a time which is how the inner library API is designed it's generally assumed registers aren't contiguous.

I wonder if a similar fix is needed for gprwave reads as well?

Tom

________________________________________
From: Prosyak, Vitaly <[email protected]>
Sent: Thursday, October 1, 2026 10:00
To: Koenig, Christian; Mikhail Gavrilov; [email protected]
Cc: Deucher, Alexander; Prosyak, Vitaly; StDenis, Tom
Subject: Re: [PATCH] drm/amdgpu: copy debugfs register data outside the GRBM/SRBM locks

Mikhail's proposed kvmalloc(size, GFP_KERNEL) correctly stages user data outside the GRBM/SRBM locks, avoiding a potential page fault while holding those locks. However, since size is derived directly from a userspace read or write request, we must validate it before allocating memory in both amdgpu_debugfs_process_reg_op() and amdgpu_debugfs_regs2_op().

Tom, do you agree with this? Will adding a restriction here break any UMR requests?
Please add a bounds check in each handler after the existing alignment/offset validation and immediately before kvmalloc():

if (size > PAGE_SIZE)

    return -EINVAL;

data = kvmalloc(size, GFP_KERNEL);
if (!data)

    return -ENOMEM;

With that input validation addressed:

Quoted 188 lines · Christian König
Quoted 33 lines · Mikhail Gavrilov

Reading or writing a register through the amdgpu_regs2 debugfs file with GRBM bank selection, which umr does for every banked access (a single "umr -b x x x -r <reg>" is enough, as is the wave dump with "-O halt_waves"), makes lockdep report a circular locking dependency, after which lockdep stays off until reboot:

WARNING: possible circular locking dependency detected
umr/63306 is trying to acquire lock:
ffff8882cf4b8350 (&mm->mmap_lock){++++}-{4:4}, at: __might_fault+0x9b/0x140
but task is already holding lock:
ffff8881aec010d0 (&amdgpu_grbm_lock_key){+.+.}-{4:4}, at: amdgpu_debugfs_regs2_op.isra.0+0x20d/0xd00 [amdgpu]
...

  Chain exists of:
    &mm->mmap_lock --> &amdgpu_srbm_lock_key --> &amdgpu_grbm_lock_key
  ...
   __might_fault+0xc5/0x140
   amdgpu_debugfs_regs2_op.isra.0+0x441/0xd00 [amdgpu]
   full_proxy_write+0x10e/0x1d0
   vfs_write+0x1d0/0xf50
   ksys_write+0x116/0x250

amdgpu_debugfs_regs2_op() and amdgpu_debugfs_process_reg_op() call get_user()/put_user() for every dword while holding grbm_idx_mutex or srbm_mutex, and pm.mutex if requested, so a fault there takes mmap_lock inside those locks. The lock hierarchy declared in amdgpu_lockdep.c since commit 1d0f5838b126
("drm/amdgpu: Add lockdep annotations for lock ordering validation") puts srbm_mutex and grbm_idx_mutex below notifier_lock, which amdgpu_cs_submit() takes with the BO reservations held, and dma_resv_lockdep() orders the reservations inside mmap_lock. So mmap_lock is outside grbm_idx_mutex and srbm_mutex, and these two debugfs paths nest it the other way round.

Quoted 7 lines

Copy the user buffer in before any of the locks is taken and the result out after all of them are dropped, as amdgpu_debugfs_wave_read(), amdgpu_debugfs_gpr_read() and amdgpu_debugfs_gprwave_read() already do. A fault on the user buffer of a write now fails with -EFAULT before any register is written, instead of after a partial write.

Fixes 1d0f5838b126 ("drm/amdgpu: Add lockdep annotations for lock ordering validation")

Well that's not really correct since that patch only makes the problem visible and not introduce it :)

But apart from that rather good catch, Vitalies work to add lockdep annotations pays of.

Quoted 1 line

I don't have time to review this in detail. Vitaly and Tom please take a look at this.

But feel free to add Acked-by: Christian König <[email protected]>

Thanks in advance,
Christian.

Quoted 135 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 71 ++++++++++++---------
 1 file changed, 40 insertions(+), 31 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +40 -31
index aca1a8045afa..5618bd3e2610 100644
@@ -121,6 +121,17 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 
     *pos &= (1UL << 22) - 1;
 
+    /*
+     * Access the user buffer only while none of the locks below are held:
+     * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+     * inside it.
+     */
+    u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+    if (!data)
+            return -ENOMEM;
+    if (!read && copy_from_user(data, buf, size))
+            return -EFAULT;
+
     r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
     if (r < 0) {
             pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -152,28 +163,16 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
             mutex_lock(&adev->pm.mutex);
 
     while (size) {
-            uint32_t value;
-
-            if (read) {
-                    value = RREG32(*pos >> 2);
-                    r = put_user(value, (uint32_t *)buf);
-            } else {
-                    r = get_user(value, (uint32_t *)buf);
-                    if (!r)
-                            amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value, 0);
-            }
-            if (r) {
-                    result = r;
-                    goto end;
-            }
+            if (read)
+                    data[result >> 2] = RREG32(*pos >> 2);
+            else
+                    amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, data[result >> 2], 0);
 
             result += 4;
-            buf += 4;
             *pos += 4;
             size -= 4;
     }
 
-end:
     if (use_bank) {
             amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0);
             mutex_unlock(&adev->grbm_idx_mutex);
@@ -188,6 +187,10 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
     pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
     amdgpu_virt_disable_access_debugfs(adev);
+
+    if (read && copy_to_user(buf, data, result))
+            return -EFAULT;
+
     return result;
 }
 
@@ -238,11 +241,21 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
     struct amdgpu_device *adev = rd->adev;
     ssize_t result = 0;
     int r;
-    uint32_t value;
 
     if (size & 0x3 || offset & 0x3)
             return -EINVAL;
 
+    /*
+     * Access the user buffer only while none of the locks below are held:
+     * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+     * inside it.
+     */
+    u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+    if (!data)
+            return -ENOMEM;
+    if (write_en && copy_from_user(data, buf, size))
+            return -EFAULT;
+
     r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
     if (r < 0) {
             pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -281,24 +294,16 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
             mutex_lock(&adev->pm.mutex);
 
     while (size) {
-            if (!write_en) {
-                    value = RREG32(offset >> 2);
-                    r = put_user(value, (uint32_t *)buf);
-            } else {
-                    r = get_user(value, (uint32_t *)buf);
-                    if (!r)
-                            amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value, rd->id.xcc_id);
-            }
-            if (r) {
-                    result = r;
-                    goto end;
-            }
+            if (!write_en)
+                    data[result >> 2] = RREG32(offset >> 2);
+            else
+                    amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, data[result >> 2],
+                                            rd->id.xcc_id);
             offset += 4;
             size -= 4;
             result += 4;
-            buf += 4;
     }
-end:
+
     if (rd->id.use_grbm) {
             amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, rd->id.xcc_id);
             mutex_unlock(&adev->grbm_idx_mutex);
@@ -317,6 +322,10 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
     pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
     amdgpu_virt_disable_access_debugfs(adev);
+
+    if (!write_en && copy_to_user(buf, data, result))
+            return -EFAULT;
+
     return result;
 }
 
maintainer
#7 lore raw

Limiting it to a PAGE_SIZE sounds reasonable to me as well.

BTW: kvmalloc() is only needed for much larger allocations (up to 2GiB), for a page kmalloc() should perfectly do it.

And I agree as well that this is probably needed for gprwave handling as well.

Christian.

Quoted 222 lines · Tom

There is minimal checking on top (e.g. size & 3 and offset & 3) which is required by both the hardware and umr. I agree the PAGE_SIZE check is a good idea.

umr really should only be doing 4 byte accesses at a time which is how the inner library API is designed it's generally assumed registers aren't contiguous.

I wonder if a similar fix is needed for gprwave reads as well?

Tom

________________________________________
From: Prosyak, Vitaly <[email protected]>
Sent: Thursday, October 1, 2026 10:00
To: Koenig, Christian; Mikhail Gavrilov; [email protected]
Cc: Deucher, Alexander; Prosyak, Vitaly; StDenis, Tom
Subject: Re: [PATCH] drm/amdgpu: copy debugfs register data outside the GRBM/SRBM locks

Mikhail's proposed kvmalloc(size, GFP_KERNEL) correctly stages user data outside the GRBM/SRBM locks, avoiding a potential page fault while holding those locks. However, since size is derived directly from a userspace read or write request, we must validate it before allocating memory in both amdgpu_debugfs_process_reg_op() and amdgpu_debugfs_regs2_op().

Tom, do you agree with this? Will adding a restriction here break any UMR requests?
Please add a bounds check in each handler after the existing alignment/offset validation and immediately before kvmalloc():

if (size > PAGE_SIZE)

    return -EINVAL;

data = kvmalloc(size, GFP_KERNEL);
if (!data)

    return -ENOMEM;

With that input validation addressed:

Quoted 188 lines · Christian König
Quoted 33 lines · Mikhail Gavrilov

Reading or writing a register through the amdgpu_regs2 debugfs file with GRBM bank selection, which umr does for every banked access (a single "umr -b x x x -r <reg>" is enough, as is the wave dump with "-O halt_waves"), makes lockdep report a circular locking dependency, after which lockdep stays off until reboot:

WARNING: possible circular locking dependency detected
umr/63306 is trying to acquire lock:
ffff8882cf4b8350 (&mm->mmap_lock){++++}-{4:4}, at: __might_fault+0x9b/0x140
but task is already holding lock:
ffff8881aec010d0 (&amdgpu_grbm_lock_key){+.+.}-{4:4}, at: amdgpu_debugfs_regs2_op.isra.0+0x20d/0xd00 [amdgpu]
...

  Chain exists of:
    &mm->mmap_lock --> &amdgpu_srbm_lock_key --> &amdgpu_grbm_lock_key
  ...
   __might_fault+0xc5/0x140
   amdgpu_debugfs_regs2_op.isra.0+0x441/0xd00 [amdgpu]
   full_proxy_write+0x10e/0x1d0
   vfs_write+0x1d0/0xf50
   ksys_write+0x116/0x250

amdgpu_debugfs_regs2_op() and amdgpu_debugfs_process_reg_op() call get_user()/put_user() for every dword while holding grbm_idx_mutex or srbm_mutex, and pm.mutex if requested, so a fault there takes mmap_lock inside those locks. The lock hierarchy declared in amdgpu_lockdep.c since commit 1d0f5838b126
("drm/amdgpu: Add lockdep annotations for lock ordering validation") puts srbm_mutex and grbm_idx_mutex below notifier_lock, which amdgpu_cs_submit() takes with the BO reservations held, and dma_resv_lockdep() orders the reservations inside mmap_lock. So mmap_lock is outside grbm_idx_mutex and srbm_mutex, and these two debugfs paths nest it the other way round.

Quoted 7 lines

Copy the user buffer in before any of the locks is taken and the result out after all of them are dropped, as amdgpu_debugfs_wave_read(), amdgpu_debugfs_gpr_read() and amdgpu_debugfs_gprwave_read() already do. A fault on the user buffer of a write now fails with -EFAULT before any register is written, instead of after a partial write.

Fixes 1d0f5838b126 ("drm/amdgpu: Add lockdep annotations for lock ordering validation")

Well that's not really correct since that patch only makes the problem visible and not introduce it :)

But apart from that rather good catch, Vitalies work to add lockdep annotations pays of.

Quoted 1 line

I don't have time to review this in detail. Vitaly and Tom please take a look at this.

But feel free to add Acked-by: Christian König <[email protected]>

Thanks in advance,
Christian.

Quoted 135 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 71 ++++++++++++---------
 1 file changed, 40 insertions(+), 31 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +40 -31
index aca1a8045afa..5618bd3e2610 100644
@@ -121,6 +121,17 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
 
     *pos &= (1UL << 22) - 1;
 
+    /*
+     * Access the user buffer only while none of the locks below are held:
+     * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+     * inside it.
+     */
+    u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+    if (!data)
+            return -ENOMEM;
+    if (!read && copy_from_user(data, buf, size))
+            return -EFAULT;
+
     r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
     if (r < 0) {
             pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -152,28 +163,16 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
             mutex_lock(&adev->pm.mutex);
 
     while (size) {
-            uint32_t value;
-
-            if (read) {
-                    value = RREG32(*pos >> 2);
-                    r = put_user(value, (uint32_t *)buf);
-            } else {
-                    r = get_user(value, (uint32_t *)buf);
-                    if (!r)
-                            amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, value, 0);
-            }
-            if (r) {
-                    result = r;
-                    goto end;
-            }
+            if (read)
+                    data[result >> 2] = RREG32(*pos >> 2);
+            else
+                    amdgpu_mm_wreg_mmio_rlc(adev, *pos >> 2, data[result >> 2], 0);
 
             result += 4;
-            buf += 4;
             *pos += 4;
             size -= 4;
     }
 
-end:
     if (use_bank) {
             amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, 0);
             mutex_unlock(&adev->grbm_idx_mutex);
@@ -188,6 +187,10 @@ static int  amdgpu_debugfs_process_reg_op(bool read, struct file *f,
     pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
     amdgpu_virt_disable_access_debugfs(adev);
+
+    if (read && copy_to_user(buf, data, result))
+            return -EFAULT;
+
     return result;
 }
 
@@ -238,11 +241,21 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
     struct amdgpu_device *adev = rd->adev;
     ssize_t result = 0;
     int r;
-    uint32_t value;
 
     if (size & 0x3 || offset & 0x3)
             return -EINVAL;
 
+    /*
+     * Access the user buffer only while none of the locks below are held:
+     * a fault takes mmap_lock, and grbm_idx_mutex and srbm_mutex nest
+     * inside it.
+     */
+    u32 *data __free(kvfree) = kvmalloc(size, GFP_KERNEL);
+    if (!data)
+            return -ENOMEM;
+    if (write_en && copy_from_user(data, buf, size))
+            return -EFAULT;
+
     r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
     if (r < 0) {
             pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -281,24 +294,16 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
             mutex_lock(&adev->pm.mutex);
 
     while (size) {
-            if (!write_en) {
-                    value = RREG32(offset >> 2);
-                    r = put_user(value, (uint32_t *)buf);
-            } else {
-                    r = get_user(value, (uint32_t *)buf);
-                    if (!r)
-                            amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, value, rd->id.xcc_id);
-            }
-            if (r) {
-                    result = r;
-                    goto end;
-            }
+            if (!write_en)
+                    data[result >> 2] = RREG32(offset >> 2);
+            else
+                    amdgpu_mm_wreg_mmio_rlc(adev, offset >> 2, data[result >> 2],
+                                            rd->id.xcc_id);
             offset += 4;
             size -= 4;
             result += 4;
-            buf += 4;
     }
-end:
+
     if (rd->id.use_grbm) {
             amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff, rd->id.xcc_id);
             mutex_unlock(&adev->grbm_idx_mutex);
@@ -317,6 +322,10 @@ static ssize_t amdgpu_debugfs_regs2_op(struct file *f, char __user *buf, u32 off
     pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
     amdgpu_virt_disable_access_debugfs(adev);
+
+    if (!write_en && copy_to_user(buf, data, result))
+            return -EFAULT;
+
     return result;
 }
 
#8 lore raw
In reply to StDenis, Tom
Quoted 1 line · Tom

I wonder if a similar fix is needed for gprwave reads as well?

I don't think so. amdgpu_debugfs_gprwave_read() reads into a kcalloc()ed buffer under grbm_idx_mutex and calls put_user() only after mutex_unlock(); amdgpu_debugfs_wave_read() and amdgpu_debugfs_gpr_read() do the same. A grep-level scan of drivers/gpu/drm/amd for get_user()/put_user()/copy_{to,from}_user() between taking and dropping grbm_idx_mutex or srbm_mutex finds only the four sites this patch changes. The scan does not follow function calls made under the locks.

v2 with Vitaly's PAGE_SIZE check is being tested; I'll send it once that run is done.

Thanks,
Mikhail