Check pcs_entry valid for pc sampling ioctl.

Signed-off-by: James Zhu <james....@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c | 33 ++++++++++++++++++--
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c 
b/drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c
index b44dfea15539..e5aa87b2da4f 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_pc_sampling.c
@@ -178,6 +178,24 @@ static int kfd_pc_sample_destroy(struct kfd_process_device 
*pdd, uint32_t trace_
 int kfd_pc_sample(struct kfd_process_device *pdd,
                                        struct kfd_ioctl_pc_sample_args __user 
*args)
 {
+       struct pc_sampling_entry *pcs_entry;
+
+       if (args->op != KFD_IOCTL_PCS_OP_QUERY_CAPABILITIES &&
+               args->op != KFD_IOCTL_PCS_OP_CREATE) {
+
+               mutex_lock(&pdd->dev->pcs_data.mutex);
+               pcs_entry = 
idr_find(&pdd->dev->pcs_data.hosttrap_entry.base.pc_sampling_idr,
+                               args->trace_id);
+               mutex_unlock(&pdd->dev->pcs_data.mutex);
+
+               /* pcs_entry is only for this pc sampling process,
+                * which has kfd_process->mutex protected here.
+                */
+               if (!pcs_entry ||
+                       pcs_entry->pdd != pdd)
+                       return -EINVAL;
+       }
+
        switch (args->op) {
        case KFD_IOCTL_PCS_OP_QUERY_CAPABILITIES:
                return kfd_pc_sample_query_cap(pdd, args);
@@ -186,13 +204,22 @@ int kfd_pc_sample(struct kfd_process_device *pdd,
                return kfd_pc_sample_create(pdd, args);
 
        case KFD_IOCTL_PCS_OP_DESTROY:
-               return kfd_pc_sample_destroy(pdd, args->trace_id);
+               if (pcs_entry->enabled)
+                       return -EBUSY;
+               else
+                       return kfd_pc_sample_destroy(pdd, args->trace_id);
 
        case KFD_IOCTL_PCS_OP_START:
-               return kfd_pc_sample_start(pdd);
+               if (pcs_entry->enabled)
+                       return -EALREADY;
+               else
+                       return kfd_pc_sample_start(pdd);
 
        case KFD_IOCTL_PCS_OP_STOP:
-               return kfd_pc_sample_stop(pdd);
+               if (!pcs_entry->enabled)
+                       return -EALREADY;
+               else
+                       return kfd_pc_sample_stop(pdd);
        }
 
        return -EINVAL;
-- 
2.25.1

Reply via email to