[PATCH v6 3/9] drm/amdgpu: add new IOCTL for usermode queue

Sharma, Shashank Shashank.Sharma at amd.com
Wed Sep 20 14:53:54 UTC 2023


[AMD Official Use Only - General]

-----Original Message-----
From: Zhang, Yifan <Yifan1.Zhang at amd.com>
Sent: Wednesday, September 20, 2023 4:48 PM
To: Sharma, Shashank <Shashank.Sharma at amd.com>; amd-gfx at lists.freedesktop.org
Cc: Deucher, Alexander <Alexander.Deucher at amd.com>; Koenig, Christian <Christian.Koenig at amd.com>; Yadav, Arvind <Arvind.Yadav at amd.com>; Sharma, Shashank <Shashank.Sharma at amd.com>
Subject: RE: [PATCH v6 3/9] drm/amdgpu: add new IOCTL for usermode queue

[AMD Official Use Only - General]

-----Original Message-----
From: amd-gfx <amd-gfx-bounces at lists.freedesktop.org> On Behalf Of Shashank Sharma
Sent: Saturday, September 9, 2023 12:05 AM
To: amd-gfx at lists.freedesktop.org
Cc: Deucher, Alexander <Alexander.Deucher at amd.com>; Koenig, Christian <Christian.Koenig at amd.com>; Yadav, Arvind <Arvind.Yadav at amd.com>; Sharma, Shashank <Shashank.Sharma at amd.com>
Subject: [PATCH v6 3/9] drm/amdgpu: add new IOCTL for usermode queue

This patch adds:
- A new IOCTL function to create and destroy
- A new structure to keep all the user queue data in one place.
- A function to generate unique index for the queue.

V1: Worked on review comments from RFC patch series:
  - Alex: Keep a list of queues, instead of single queue per process.
  - Christian: Use the queue manager instead of global ptrs,
           Don't keep the queue structure in amdgpu_ctx

V2: Worked on review comments:
 - Christian:
   - Formatting of text
   - There is no need for queuing of userqueues, with idr in place
 - Alex:
   - Remove use_doorbell, its unnecessary
   - Reuse amdgpu_mqd_props for saving mqd fields

 - Code formatting and re-arrangement

V3:
 - Integration with doorbell manager

V4:
 - Accommodate MQD union related changes in UAPI (Alex)
 - Do not set the queue size twice (Bas)

V5:
- Remove wrapper functions for queue indexing (Christian)
- Do not save the queue id/idr in queue itself (Christian)
- Move the idr allocation in the IP independent generic space
  (Christian)

V6:
- Check the validity of input IP type (Christian)

Cc: Alex Deucher <alexander.deucher at amd.com>
Cc: Christian Koenig <christian.koenig at amd.com>
Signed-off-by: Shashank Sharma <shashank.sharma at amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c       |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c | 116 ++++++++++++++++++
 .../gpu/drm/amd/include/amdgpu_userqueue.h    |   2 +
 3 files changed, 119 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
index 2a89e303c3db..214a66b33dfa 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
@@ -2830,6 +2830,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
        DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+       DRM_IOCTL_DEF_DRV(AMDGPU_USERQ, amdgpu_userq_ioctl,
+DRM_AUTH|DRM_RENDER_ALLOW),
 };

 static const struct drm_driver amdgpu_kms_driver = { diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c
index effc0c7c02cf..44769423ba30 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userqueue.c
@@ -23,6 +23,122 @@
  */

 #include "amdgpu.h"
+#include "amdgpu_vm.h"
+#include "amdgpu_userqueue.h"
+
+static struct amdgpu_usermode_queue *
+amdgpu_userqueue_find(struct amdgpu_userq_mgr *uq_mgr, int qid) {
+       return idr_find(&uq_mgr->userq_idr, qid); }
+
+static int
+amdgpu_userqueue_destroy(struct drm_file *filp, int queue_id) {
+       struct amdgpu_fpriv *fpriv = filp->driver_priv;
+       struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+       const struct amdgpu_userq_funcs *uq_funcs;
+       struct amdgpu_usermode_queue *queue;
+
+       mutex_lock(&uq_mgr->userq_mutex);
+
+       queue = amdgpu_userqueue_find(uq_mgr, queue_id);
+       if (!queue) {
+               DRM_DEBUG_DRIVER("Invalid queue id to destroy\n");
+               mutex_unlock(&uq_mgr->userq_mutex);
+               return -EINVAL;
+       }
+       uq_funcs = uq_mgr->userq_funcs[queue->queue_type];
+       uq_funcs->mqd_destroy(uq_mgr, queue);
+       idr_remove(&uq_mgr->userq_idr, queue_id);
+       kfree(queue);
+
+       mutex_unlock(&uq_mgr->userq_mutex);
+       return 0;
+}
+
+static int
+amdgpu_userqueue_create(struct drm_file *filp, union drm_amdgpu_userq
+*args) {
+       struct amdgpu_fpriv *fpriv = filp->driver_priv;
+       struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+       const struct amdgpu_userq_funcs *uq_funcs;
+       struct amdgpu_usermode_queue *queue;
+       int qid, r = 0;
+
+       /* Usermode queues are only supported for GFX/SDMA engines as of now */
+       if (args->in.ip_type != AMDGPU_HW_IP_GFX && args->in.ip_type != AMDGPU_HW_IP_DMA) {
+               DRM_ERROR("Usermode queue doesn't support IP type %u\n", args->in.ip_type);
+               return -EINVAL;
+       }
+
+       mutex_lock(&uq_mgr->userq_mutex);
+
+       uq_funcs = uq_mgr->userq_funcs[args->in.ip_type];
+       if (!uq_funcs) {
+               DRM_ERROR("Usermode queue is not supported for this IP (%u)\n", args->in.ip_type);
+               r = -EINVAL;
+               goto unlock;
+       }
+
+       queue = kzalloc(sizeof(struct amdgpu_usermode_queue), GFP_KERNEL);
+       if (!queue) {
+               DRM_ERROR("Failed to allocate memory for queue\n");
+               r = -ENOMEM;
+               goto unlock;
+       }
+       queue->doorbell_handle = args->in.doorbell_handle;
+       queue->doorbell_index = args->in.doorbell_offset;
+       queue->queue_type = args->in.ip_type;
+       queue->flags = args->in.flags;
+       queue->vm = &fpriv->vm;
+
+       r = uq_funcs->mqd_create(uq_mgr, &args->in, queue);
+       if (r) {
+               DRM_ERROR("Failed to create Queue\n");
+               goto unlock;

Should kfree(queue) in this path to avoid memory leak

+       }
+
+       qid = idr_alloc(&uq_mgr->userq_idr, queue, 1, AMDGPU_MAX_USERQ_COUNT, GFP_KERNEL);
+       if (qid < 0) {
+               DRM_ERROR("Failed to allocate a queue id\n");
+               uq_funcs->mqd_destroy(uq_mgr, queue);
+               r = -ENOMEM;
+               goto unlock;

Should kfree(queue)  in this path to avoid memory leak

+       }
+       args->out.queue_id = qid;
+

Should add
free_queue:
        kfree(queue) ;
to avoid memory leak



That's correct, looks like it got removed when I moved the code shift to accommodate the cleanup patch.

Thank you for pointing that.

- Shashank




+unlock:
+       mutex_unlock(&uq_mgr->userq_mutex);
+       return r;
+}
+
+int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
+                      struct drm_file *filp) {
+       union drm_amdgpu_userq *args = data;
+       int r = 0;
+
+       switch (args->in.op) {
+       case AMDGPU_USERQ_OP_CREATE:
+               r = amdgpu_userqueue_create(filp, args);
+               if (r)
+                       DRM_ERROR("Failed to create usermode queue\n");
+               break;
+
+       case AMDGPU_USERQ_OP_FREE:
+               r = amdgpu_userqueue_destroy(filp, args->in.queue_id);
+               if (r)
+                       DRM_ERROR("Failed to destroy usermode queue\n");
+               break;
+
+       default:
+               DRM_ERROR("Invalid user queue op specified: %d\n", args->in.op);
+               return -EINVAL;
+       }
+
+       return r;
+}

 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct amdgpu_device *adev)  { diff --git a/drivers/gpu/drm/amd/include/amdgpu_userqueue.h b/drivers/gpu/drm/amd/include/amdgpu_userqueue.h
index 79ffa131a514..55ed6512a565 100644
--- a/drivers/gpu/drm/amd/include/amdgpu_userqueue.h
+++ b/drivers/gpu/drm/amd/include/amdgpu_userqueue.h
@@ -55,6 +55,8 @@ struct amdgpu_userq_mgr {
        const struct amdgpu_userq_funcs *userq_funcs[AMDGPU_HW_IP_NUM];  };

+int amdgpu_userq_ioctl(struct drm_device *dev, void *data, struct
+drm_file *filp);
+
 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct amdgpu_device *adev);

 void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr);
--
2.42.0




More information about the amd-gfx mailing list