Skip to contentSkip to conversation

drm/amdkfd: Remove GPU ID in GWS queue creation

PATCH Superseded Greathouse, Joseph 2 participants 4 messages Jul 27, 2019 – Aug 6, 2019 3 messages not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdkfd: Remove GPU ID in GWS queue creation

The gpu_id argument is not needed when enabling GWS on a queue. The queue can only be associated with one device, so the only possible situations for the call as previously defined were:
1) the gpu_id was for the device associated with the target queue and things worked as expected, or 2) the gpu_id was for a device not associated with the target queue and the request was undefined.

In particular, the previous result of the undefined operation is that you would allocate the number of GWS entries available on the gpu_id device, even if the queue was on a device with a different number available. For example: a queue on a device without GWS capability, but the user passes in a gpu_id for a device with GWS. We would end up trying to allocate GWS on the device that does not support it.

Rather than leaving this footgun around and making life more difficult for user space, we can instead grab the gpu_id from the target queue. The gpu_id argument being passed in is thus not needed. We thus change the field in the ioctl struct to be reserved so that nobody expects it to do anything. However, we do not remove because that would break user-land API compatibility.

Change-Id I861cebc8a0a7eab5360da10971a73d5a4700c6d8
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 19 +++++++++++++------
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  2 ++
 .../amd/amdkfd/kfd_process_queue_manager.c    | 12 ++++++++++++
 include/uapi/linux/kfd_ioctl.h                |  4 ++--
 4 files changed, 29 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +13 -6
index f91126f5f1be..46005b1dcf79 100644
@@ -1572,20 +1572,27 @@ static int kfd_ioctl_alloc_queue_gws(struct file *filep,
 {
 	int retval;
 	struct kfd_ioctl_alloc_queue_gws_args *args = data;
+	struct queue *q;
 	struct kfd_dev *dev;
 
 	if (!hws_gws_support)
 		return -ENODEV;
 
-	dev = kfd_device_by_id(args->gpu_id);
-	if (!dev) {
-		pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
-		return -ENODEV;
+	mutex_lock(&p->mutex);
+	q = pqm_get_user_queue(&p->pqm, args->queue_id);
+
+	if (q)
+		dev = q->device;
+	else {
+		mutex_unlock(&p->mutex);
+		return -EINVAL;
 	}
-	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
+
+	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
+		mutex_unlock(&p->mutex);
 		return -ENODEV;
+	}
 
-	mutex_lock(&p->mutex);
 	retval = pqm_set_gws(&p->pqm, args->queue_id, args->num_gws ? dev->gws : NULL);
 	mutex_unlock(&p->mutex);
 
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2 -0
index aa7bf20d20f8..9b9a8da187c8 100644
@@ -915,6 +915,8 @@ int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
 			void *gws);
 struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
 						unsigned int qid);
+struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
+						unsigned int qid);
 int pqm_get_wave_state(struct process_queue_manager *pqm,
 		       unsigned int qid,
 		       void __user *ctl_stack,
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +12 -0
index 7e6c3ee82f5b..20dae1fdb16a 100644
@@ -473,6 +473,18 @@ struct kernel_queue *pqm_get_kernel_queue(
 	return NULL;
 }
 
+struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
+					unsigned int qid)
+{
+	struct process_queue_node *pqn;
+
+	pqn = get_queue_by_qid(pqm, qid);
+	if (pqn && pqn->q)
+		return pqn->q;
+
+	return NULL;
+}
+
 int pqm_get_wave_state(struct process_queue_manager *pqm,
 		       unsigned int qid,
 		       void __user *ctl_stack,
include/uapi/linux/kfd_ioctl.h +2 -2
index 070d1bc7e725..0cbd25d2bb38 100644
@@ -412,14 +412,14 @@ struct kfd_ioctl_unmap_memory_from_gpu_args {
 
 /* Allocate GWS for specific queue
  *
- * @gpu_id:      device identifier
+ * @reserved:    reserved for ABI compatibility. value is ignored.
  * @queue_id:    queue's id that GWS is allocated for
  * @num_gws:     how many GWS to allocate
  * @first_gws:   index of the first GWS allocated.
  *               only support contiguous GWS allocation
  */
 struct kfd_ioctl_alloc_queue_gws_args {
-	__u32 gpu_id;		/* to KFD */
+	__u32 reserved;		/* to KFD */
 	__u32 queue_id;		/* to KFD */
 	__u32 num_gws;		/* to KFD */
 	__u32 first_gws;	/* from KFD */
Signature · 6 lines
2.19.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 20190727012628.5896-1-Joseph.Greathouse-5C7GfCeVMHo@public.gmane.org

#2 lore raw
Quoted 22 lines · Joseph

The gpu_id argument is not needed when enabling GWS on a queue. The queue can only be associated with one device, so the only possible situations for the call as previously defined were:
1) the gpu_id was for the device associated with the target queue and things worked as expected, or 2) the gpu_id was for a device not associated with the target queue and the request was undefined.

In particular, the previous result of the undefined operation is that you would allocate the number of GWS entries available on the gpu_id device, even if the queue was on a device with a different number available. For example: a queue on a device without GWS capability, but the user passes in a gpu_id for a device with GWS. We would end up trying to allocate GWS on the device that does not support it.

Rather than leaving this footgun around and making life more difficult for user space, we can instead grab the gpu_id from the target queue. The gpu_id argument being passed in is thus not needed. We thus change the field in the ioctl struct to be reserved so that nobody expects it to do anything. However, we do not remove because that would break user-land API compatibility.

Quoted 2 lines
Change-Id I861cebc8a0a7eab5360da10971a73d5a4700c6d8

Cosmetic comments inline. Otherwise this looks good to me.

Quoted 20 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 19 +++++++++++++------
  drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  2 ++
  .../amd/amdkfd/kfd_process_queue_manager.c    | 12 ++++++++++++
  include/uapi/linux/kfd_ioctl.h                |  4 ++--
  4 files changed, 29 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -0
index f91126f5f1be..46005b1dcf79 100644
@@ -1572,20 +1572,27 @@ static int kfd_ioctl_alloc_queue_gws(struct file *filep,
  {
  	int retval;
  	struct kfd_ioctl_alloc_queue_gws_args *args = data;
+	struct queue *q;
  	struct kfd_dev *dev;
 
  	if (!hws_gws_support)
  		return -ENODEV;

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_alloc_queue_gws(struct file *filep, · line 1585

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +5 -4
 
-	dev = kfd_device_by_id(args->gpu_id);
-	if (!dev) {
-		pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
-		return -ENODEV;
+	mutex_lock(&p->mutex);
+	q = pqm_get_user_queue(&p->pqm, args->queue_id);
+
+	if (q)
+		dev = q->device;

Please use {} around the if-block for consistency with the else-block.

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_alloc_queue_gws(struct file *filep, · line 1587

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
+	else {
+		mutex_unlock(&p->mutex);

I'd prefer the error handling with a goto out_unlock. That's a common convention in kernel code to minimize error prone duplication of unwinding code for error handling.

Quoted 31 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +5 -2
+		return -EINVAL;
  	}
-	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
+
+	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
+		mutex_unlock(&p->mutex);
  		return -ENODEV;
+	}
 
-	mutex_lock(&p->mutex);
  	retval = pqm_set_gws(&p->pqm, args->queue_id, args->num_gws ? dev->gws : NULL);
  	mutex_unlock(&p->mutex);
 
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2 -0
index aa7bf20d20f8..9b9a8da187c8 100644
@@ -915,6 +915,8 @@ int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
  			void *gws);
  struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
  						unsigned int qid);
+struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
+						unsigned int qid);
  int pqm_get_wave_state(struct process_queue_manager *pqm,
  		       unsigned int qid,
  		       void __user *ctl_stack,
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +0 -0
index 7e6c3ee82f5b..20dae1fdb16a 100644

drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c struct kernel_queue *pqm_get_kernel_queue( · line 482

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +7 -0
@@ -473,6 +473,18 @@ struct kernel_queue *pqm_get_kernel_queue(
  	return NULL;
  }
 
+struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
+					unsigned int qid)
+{
+	struct process_queue_node *pqn;
+
+	pqn = get_queue_by_qid(pqm, qid);
+	if (pqn && pqn->q)

It would be sufficient to check if (pqn) here. If pqn->q is NULL, you'll return NULL either way. You could even condense it into a single return statement:

    return pqn ? pqn->q : NULL;

Regards,
  Felix

Quoted 19 lines
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +5 -0
+		return pqn->q;
+
+	return NULL;
+}
+
  int pqm_get_wave_state(struct process_queue_manager *pqm,
  		       unsigned int qid,
  		       void __user *ctl_stack,
include/uapi/linux/kfd_ioctl.h +1 -1
index 070d1bc7e725..0cbd25d2bb38 100644
@@ -412,14 +412,14 @@ struct kfd_ioctl_unmap_memory_from_gpu_args {
 
  /* Allocate GWS for specific queue
   *
- * @gpu_id:      device identifier
+ * @reserved:    reserved for ABI compatibility. value is ignored.
   * @queue_id:    queue's id that GWS is allocated for

include/uapi/linux/kfd_ioctl.h struct kfd_ioctl_unmap_memory_from_gpu_args { · line 425

Quoted 10 lines
include/uapi/linux/kfd_ioctl.h +1 -1
   * @num_gws:     how many GWS to allocate
   * @first_gws:   index of the first GWS allocated.
   *               only support contiguous GWS allocation
   */
  struct kfd_ioctl_alloc_queue_gws_args {
-	__u32 gpu_id;		/* to KFD */
+	__u32 reserved;		/* to KFD */
  	__u32 queue_id;		/* to KFD */
  	__u32 num_gws;		/* to KFD */
  	__u32 first_gws;	/* from KFD */

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): d4b6a8a2-20da-027b-bfe9-e77517411884-5C7GfCeVMHo@public.gmane.org

#4 lore raw

PATCH v2 1/1 drm/amdkfd: Remove GPU ID in GWS queue creation

The gpu_id argument is not needed when enabling GWS on a queue. The queue can only be associated with one device, so the only possible situations for the call as previously defined were:
1) the gpu_id was for the device associated with the target queue and things worked as expected, or 2) the gpu_id was for a device not associated with the target queue and the request was undefined.

In particular, the previous result of the undefined operation is that you would allocate the number of GWS entries available on the gpu_id device, even if the queue was on a device with a different number available. For example: a queue on a device without GWS capability, but the user passes in a gpu_id for a device with GWS. We would end up trying to allocate GWS on the device that does not support it.

Rather than leaving this footgun around and making life more difficult for user space, we can instead grab the gpu_id from the target queue. The gpu_id argument being passed in is thus not needed.

v2: Fixed styling, removed gpu_id since it never hit main release

Change-Id I73c032d7115b62505a6e7c48d2c060fc3c6ee915
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 26 ++++++++++++++-----
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  2 ++
 .../amd/amdkfd/kfd_process_queue_manager.c    |  9 +++++++
 include/uapi/linux/kfd_ioctl.h                |  3 +--
 4 files changed, 31 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +19 -7
index f91126f5f1be..b464ad1e2c4c 100644
@@ -1572,25 +1572,37 @@ static int kfd_ioctl_alloc_queue_gws(struct file *filep,
 {
 	int retval;
 	struct kfd_ioctl_alloc_queue_gws_args *args = data;
+	struct queue *q;
 	struct kfd_dev *dev;
 
 	if (!hws_gws_support)
 		return -ENODEV;
 
-	dev = kfd_device_by_id(args->gpu_id);
-	if (!dev) {
-		pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
-		return -ENODEV;
+	mutex_lock(&p->mutex);
+	q = pqm_get_user_queue(&p->pqm, args->queue_id);
+
+	if (q) {
+		dev = q->device;
+	}
+	else {
+		retval = -EINVAL;
+		goto out_unlock;
+	}
+
+	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
+		retval = -ENODEV;
+		goto out_unlock;
 	}
-	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
-		return -ENODEV;
 
-	mutex_lock(&p->mutex);
 	retval = pqm_set_gws(&p->pqm, args->queue_id, args->num_gws ? dev->gws : NULL);
 	mutex_unlock(&p->mutex);
 
 	args->first_gws = 0;
 	return retval;
+
+out_unlock:
+	mutex_unlock(&p->mutex);
+	return retval;
 }
 
 static int kfd_ioctl_get_dmabuf_info(struct file *filep,
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2 -0
index aa7bf20d20f8..9b9a8da187c8 100644
@@ -915,6 +915,8 @@ int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
 			void *gws);
 struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
 						unsigned int qid);
+struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
+						unsigned int qid);
 int pqm_get_wave_state(struct process_queue_manager *pqm,
 		       unsigned int qid,
 		       void __user *ctl_stack,
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +9 -0
index 7e6c3ee82f5b..7a61a5b09ed8 100644
@@ -473,6 +473,15 @@ struct kernel_queue *pqm_get_kernel_queue(
 	return NULL;
 }
 
+struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
+					unsigned int qid)
+{
+	struct process_queue_node *pqn;
+
+	pqn = get_queue_by_qid(pqm, qid);
+	return pqn ? pqn->q : NULL;
+}
+
 int pqm_get_wave_state(struct process_queue_manager *pqm,
 		       unsigned int qid,
 		       void __user *ctl_stack,
include/uapi/linux/kfd_ioctl.h +1 -2
index 070d1bc7e725..4f6676428c5c 100644
@@ -412,17 +412,16 @@ struct kfd_ioctl_unmap_memory_from_gpu_args {
 
 /* Allocate GWS for specific queue
  *
- * @gpu_id:      device identifier
  * @queue_id:    queue's id that GWS is allocated for
  * @num_gws:     how many GWS to allocate
  * @first_gws:   index of the first GWS allocated.
  *               only support contiguous GWS allocation
  */
 struct kfd_ioctl_alloc_queue_gws_args {
-	__u32 gpu_id;		/* to KFD */
 	__u32 queue_id;		/* to KFD */
 	__u32 num_gws;		/* to KFD */
 	__u32 first_gws;	/* from KFD */
+	__u32 pad;
 };
 
 struct kfd_ioctl_get_dmabuf_info_args {
Signature · 6 lines
2.19.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 20190801211417.4781-1-Joseph.Greathouse-5C7GfCeVMHo@public.gmane.org

Reviewed-by Felix Kuehling
#6 lore raw
Quoted 20 lines · Joseph

The gpu_id argument is not needed when enabling GWS on a queue. The queue can only be associated with one device, so the only possible situations for the call as previously defined were:
1) the gpu_id was for the device associated with the target queue and things worked as expected, or 2) the gpu_id was for a device not associated with the target queue and the request was undefined.

In particular, the previous result of the undefined operation is that you would allocate the number of GWS entries available on the gpu_id device, even if the queue was on a device with a different number available. For example: a queue on a device without GWS capability, but the user passes in a gpu_id for a device with GWS. We would end up trying to allocate GWS on the device that does not support it.

Rather than leaving this footgun around and making life more difficult for user space, we can instead grab the gpu_id from the target queue. The gpu_id argument being passed in is thus not needed.

Quoted 4 lines

v2: Fixed styling, removed gpu_id since it never hit main release

Change-Id I73c032d7115b62505a6e7c48d2c060fc3c6ee915

See one style comment inline. With that fixed, this patch is

Quoted 22 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      | 26 ++++++++++++++-----
  drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  2 ++
  .../amd/amdkfd/kfd_process_queue_manager.c    |  9 +++++++
  include/uapi/linux/kfd_ioctl.h                |  3 +--
  4 files changed, 31 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
index f91126f5f1be..b464ad1e2c4c 100644
@@ -1572,25 +1572,37 @@ static int kfd_ioctl_alloc_queue_gws(struct file *filep,
  {
  	int retval;
  	struct kfd_ioctl_alloc_queue_gws_args *args = data;
+	struct queue *q;
  	struct kfd_dev *dev;
 
  	if (!hws_gws_support)
  		return -ENODEV;
 
-	dev = kfd_device_by_id(args->gpu_id);

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_alloc_queue_gws(struct file *filep, · line 1587

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +7 -3
-	if (!dev) {
-		pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
-		return -ENODEV;
+	mutex_lock(&p->mutex);
+	q = pqm_get_user_queue(&p->pqm, args->queue_id);
+
+	if (q) {
+		dev = q->device;
+	}
+	else {

The closing brace } and "else" should be on the same line.

Thanks,
  Felix

Quoted 70 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +11 -3
+		retval = -EINVAL;
+		goto out_unlock;
+	}
+
+	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
+		retval = -ENODEV;
+		goto out_unlock;
  	}
-	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
-		return -ENODEV;
 
-	mutex_lock(&p->mutex);
  	retval = pqm_set_gws(&p->pqm, args->queue_id, args->num_gws ? dev->gws : NULL);
  	mutex_unlock(&p->mutex);
 
  	args->first_gws = 0;
  	return retval;
+
+out_unlock:
+	mutex_unlock(&p->mutex);
+	return retval;
  }
 
  static int kfd_ioctl_get_dmabuf_info(struct file *filep,
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2 -0
index aa7bf20d20f8..9b9a8da187c8 100644
@@ -915,6 +915,8 @@ int pqm_set_gws(struct process_queue_manager *pqm, unsigned int qid,
  			void *gws);
  struct kernel_queue *pqm_get_kernel_queue(struct process_queue_manager *pqm,
  						unsigned int qid);
+struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
+						unsigned int qid);
  int pqm_get_wave_state(struct process_queue_manager *pqm,
  		       unsigned int qid,
  		       void __user *ctl_stack,
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +9 -0
index 7e6c3ee82f5b..7a61a5b09ed8 100644
@@ -473,6 +473,15 @@ struct kernel_queue *pqm_get_kernel_queue(
  	return NULL;
  }
 
+struct queue *pqm_get_user_queue(struct process_queue_manager *pqm,
+					unsigned int qid)
+{
+	struct process_queue_node *pqn;
+
+	pqn = get_queue_by_qid(pqm, qid);
+	return pqn ? pqn->q : NULL;
+}
+
  int pqm_get_wave_state(struct process_queue_manager *pqm,
  		       unsigned int qid,
  		       void __user *ctl_stack,
include/uapi/linux/kfd_ioctl.h +0 -1
index 070d1bc7e725..4f6676428c5c 100644
@@ -412,17 +412,16 @@ struct kfd_ioctl_unmap_memory_from_gpu_args {
 
  /* Allocate GWS for specific queue
   *
- * @gpu_id:      device identifier
   * @queue_id:    queue's id that GWS is allocated for
   * @num_gws:     how many GWS to allocate
   * @first_gws:   index of the first GWS allocated.
   *               only support contiguous GWS allocation

include/uapi/linux/kfd_ioctl.h struct kfd_ioctl_unmap_memory_from_gpu_args { · line 427

Quoted 10 lines
include/uapi/linux/kfd_ioctl.h +1 -1
   */
  struct kfd_ioctl_alloc_queue_gws_args {
-	__u32 gpu_id;		/* to KFD */
  	__u32 queue_id;		/* to KFD */
  	__u32 num_gws;		/* to KFD */
  	__u32 first_gws;	/* from KFD */
+	__u32 pad;
  };
 
  struct kfd_ioctl_get_dmabuf_info_args {

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx