Re: [PATCH v3 23/24] drm/amdkfd: set debug trap bit when enabling PC Sampling

2024-01-02 Thread James Zhu


On 2023-12-15 10:59, James Zhu wrote:

From: David Yat Sin

We need the SPI_GDBG_PER_VMID_CNTL.TRAP_EN bit to be set during PC
Sampling so that the TTMP registers are valid inside the sampling data.
runtime_info.ttmp_setup will be cleared when the user application
does the AMDKFD_IOC_RUNTIME_ENABLE ioctl without
KFD_RUNTIME_ENABLE_MODE_ENABLE_MASK flag on exit.

It is also not valid to have the debugger attached to a process while PC
sampling is enabled so adding some checks to prevent this.

Signed-off-by: David Yat Sin
---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 31 --
  drivers/gpu/drm/amd/amdkfd/kfd_debug.c   | 22 ++
  drivers/gpu/drm/amd/amdkfd/kfd_debug.h   |  3 ++
  drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c | 43 +---
  drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.h |  4 +-
  5 files changed, 75 insertions(+), 28 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 1a3a8ded9c93..f7a8794c2bde 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1775,7 +1775,7 @@ static int kfd_ioctl_pc_sample(struct file *filep,
pr_debug("failed to bind process %p with gpu id 0x%x", p, 
args->gpu_id);
ret = -ESRCH;
} else {
-   ret = kfd_pc_sample(pdd, args);
+   ret = kfd_pc_sample(p, pdd, args);
}
}
mutex_unlock(>mutex);
@@ -2808,26 +2808,9 @@ static int runtime_enable(struct kfd_process *p, 
uint64_t r_debug,
  
  	p->runtime_info.runtime_state = DEBUG_RUNTIME_STATE_ENABLED;

p->runtime_info.r_debug = r_debug;
-   p->runtime_info.ttmp_setup = enable_ttmp_setup;
  
-	if (p->runtime_info.ttmp_setup) {

-   for (i = 0; i < p->n_pdds; i++) {
-   struct kfd_process_device *pdd = p->pdds[i];
-
-   if (!kfd_dbg_is_rlc_restore_supported(pdd->dev)) {
-   amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
-   pdd->dev->kfd2kgd->enable_debug_trap(
-   pdd->dev->adev,
-   true,
-   
pdd->dev->vm_info.last_vmid_kfd);
-   } else if (kfd_dbg_is_per_vmid_supported(pdd->dev)) {
-   pdd->spi_dbg_override = 
pdd->dev->kfd2kgd->enable_debug_trap(
-   pdd->dev->adev,
-   false,
-   0);
-   }
-   }
-   }
+   if (enable_ttmp_setup)
+   kfd_dbg_enable_ttmp_setup(p);
  
  retry:

if (p->debug_trap_enabled) {
@@ -2976,9 +2959,13 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, 
struct kfd_process *p, v
goto out;
}
  
-	/* Check if target is still PTRACED. */

rcu_read_lock();
-   if (target != p && args->op != KFD_IOC_DBG_TRAP_DISABLE
+
+   if (kfd_pc_sampling_enabled(target)) {
+   pr_debug("Cannot enable debug trap on PID:%d because PC Sampling 
active\n", args->pid);
+   r = -EBUSY;
+   /* Check if target is still PTRACED. */
+   } else if (target != p && args->op != KFD_IOC_DBG_TRAP_DISABLE
&& ptrace_parent(target->lead_thread) != 
current) {
pr_err("PID %i is not PTRACED and cannot be debugged\n", 
args->pid);
r = -EPERM;
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
index 9ec750666382..092c2dc84d24 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
@@ -1118,3 +1118,25 @@ void kfd_dbg_set_enabled_debug_exception_mask(struct 
kfd_process *target,
  
  	mutex_unlock(>event_mutex);

  }
+
+void kfd_dbg_enable_ttmp_setup(struct kfd_process *p)
+{
+   int i;
+   p->runtime_info.ttmp_setup = true;
+   for (i = 0; i < p->n_pdds; i++) {
+   struct kfd_process_device *pdd = p->pdds[i];
+
+   if (!kfd_dbg_is_rlc_restore_supported(pdd->dev)) {
+   amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
+   pdd->dev->kfd2kgd->enable_debug_trap(
+   pdd->dev->adev,
+   true,
+   pdd->dev->vm_info.last_vmid_kfd);
+   } else if (kfd_dbg_is_per_vmid_supported(pdd->dev)) {
+   pdd->spi_dbg_override = 
pdd->dev->kfd2kgd->enable_debug_trap(
+   pdd->dev->adev,
+   false,
+   0);
+   }
+   }
+}
\ No newline at end of 

[PATCH v3 23/24] drm/amdkfd: set debug trap bit when enabling PC Sampling

2023-12-15 Thread James Zhu
From: David Yat Sin 

We need the SPI_GDBG_PER_VMID_CNTL.TRAP_EN bit to be set during PC
Sampling so that the TTMP registers are valid inside the sampling data.
runtime_info.ttmp_setup will be cleared when the user application
does the AMDKFD_IOC_RUNTIME_ENABLE ioctl without
KFD_RUNTIME_ENABLE_MODE_ENABLE_MASK flag on exit.

It is also not valid to have the debugger attached to a process while PC
sampling is enabled so adding some checks to prevent this.

Signed-off-by: David Yat Sin 
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 31 --
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c   | 22 ++
 drivers/gpu/drm/amd/amdkfd/kfd_debug.h   |  3 ++
 drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c | 43 +---
 drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.h |  4 +-
 5 files changed, 75 insertions(+), 28 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 1a3a8ded9c93..f7a8794c2bde 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -1775,7 +1775,7 @@ static int kfd_ioctl_pc_sample(struct file *filep,
pr_debug("failed to bind process %p with gpu id 0x%x", 
p, args->gpu_id);
ret = -ESRCH;
} else {
-   ret = kfd_pc_sample(pdd, args);
+   ret = kfd_pc_sample(p, pdd, args);
}
}
mutex_unlock(>mutex);
@@ -2808,26 +2808,9 @@ static int runtime_enable(struct kfd_process *p, 
uint64_t r_debug,
 
p->runtime_info.runtime_state = DEBUG_RUNTIME_STATE_ENABLED;
p->runtime_info.r_debug = r_debug;
-   p->runtime_info.ttmp_setup = enable_ttmp_setup;
 
-   if (p->runtime_info.ttmp_setup) {
-   for (i = 0; i < p->n_pdds; i++) {
-   struct kfd_process_device *pdd = p->pdds[i];
-
-   if (!kfd_dbg_is_rlc_restore_supported(pdd->dev)) {
-   amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
-   pdd->dev->kfd2kgd->enable_debug_trap(
-   pdd->dev->adev,
-   true,
-   
pdd->dev->vm_info.last_vmid_kfd);
-   } else if (kfd_dbg_is_per_vmid_supported(pdd->dev)) {
-   pdd->spi_dbg_override = 
pdd->dev->kfd2kgd->enable_debug_trap(
-   pdd->dev->adev,
-   false,
-   0);
-   }
-   }
-   }
+   if (enable_ttmp_setup)
+   kfd_dbg_enable_ttmp_setup(p);
 
 retry:
if (p->debug_trap_enabled) {
@@ -2976,9 +2959,13 @@ static int kfd_ioctl_set_debug_trap(struct file *filep, 
struct kfd_process *p, v
goto out;
}
 
-   /* Check if target is still PTRACED. */
rcu_read_lock();
-   if (target != p && args->op != KFD_IOC_DBG_TRAP_DISABLE
+
+   if (kfd_pc_sampling_enabled(target)) {
+   pr_debug("Cannot enable debug trap on PID:%d because PC 
Sampling active\n", args->pid);
+   r = -EBUSY;
+   /* Check if target is still PTRACED. */
+   } else if (target != p && args->op != KFD_IOC_DBG_TRAP_DISABLE
&& ptrace_parent(target->lead_thread) != 
current) {
pr_err("PID %i is not PTRACED and cannot be debugged\n", 
args->pid);
r = -EPERM;
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
index 9ec750666382..092c2dc84d24 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
@@ -1118,3 +1118,25 @@ void kfd_dbg_set_enabled_debug_exception_mask(struct 
kfd_process *target,
 
mutex_unlock(>event_mutex);
 }
+
+void kfd_dbg_enable_ttmp_setup(struct kfd_process *p)
+{
+   int i;
+   p->runtime_info.ttmp_setup = true;
+   for (i = 0; i < p->n_pdds; i++) {
+   struct kfd_process_device *pdd = p->pdds[i];
+
+   if (!kfd_dbg_is_rlc_restore_supported(pdd->dev)) {
+   amdgpu_gfx_off_ctrl(pdd->dev->adev, false);
+   pdd->dev->kfd2kgd->enable_debug_trap(
+   pdd->dev->adev,
+   true,
+   pdd->dev->vm_info.last_vmid_kfd);
+   } else if (kfd_dbg_is_per_vmid_supported(pdd->dev)) {
+   pdd->spi_dbg_override = 
pdd->dev->kfd2kgd->enable_debug_trap(
+   pdd->dev->adev,
+   false,
+   0);
+   }
+   }
+}
\ No newline at end of file
diff --git