Skip to contentSkip to conversation

drm/amdkfd: add event age tracking

PATCH In review James Zhu 3 participants 9 messages Jun 1, 2023

Subsystems (3)
lore
Share & tools
lore

James Zhu

author
#0 lore raw

PATCH 1/3 drm/amdkfd: add event age tracking

Add event age tracking

---
 include/uapi/linux/kfd_ioctl.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/uapi/linux/kfd_ioctl.h +3 -1
index 7e19a2d1e907..bfbe0006370e 100644
@@ -38,9 +38,10 @@
  * - 1.10 - Add SMI profiler event log
  * - 1.11 - Add unified memory for ctx save/restore area
  * - 1.12 - Add DMA buf export ioctl
+ * - 1.13 - Update kfd_event_data
  */
 #define KFD_IOCTL_MAJOR_VERSION 1
-#define KFD_IOCTL_MINOR_VERSION 12
+#define KFD_IOCTL_MINOR_VERSION 13
 
 /*
  * Debug revision change log
@@ -693,6 +694,7 @@ struct kfd_event_data {
 	union {
 		struct kfd_hsa_memory_exception_data memory_exception_data;
 		struct kfd_hsa_hw_exception_data hw_exception_data;
+		__u64 last_event_age;	/* to and from KFD */
 	};				/* From KFD */
 	__u64 kfd_event_data_ext;	/* pointer to an extension structure
 					   for future exception types */
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/3 drm/amdkfd: add event_age tracking when receiving interrupt

Add event_age tracking when receiving interrupt.

---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 6 ++++++
 drivers/gpu/drm/amd/amdkfd/kfd_events.h | 1 +
 2 files changed, 7 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +6 -0
index d23202613ed7..b27a79c5f826 100644
@@ -439,6 +439,7 @@ int kfd_event_create(struct file *devkfd, struct kfd_process *p,
 	if (!ret) {
 		*event_id = ev->event_id;
 		*event_trigger_data = ev->event_id;
+		ev->event_age = 1;
 	} else {
 		kfree(ev);
 	}
@@ -637,6 +638,11 @@ static void set_event(struct kfd_event *ev)
 	 * updating the wait queues in kfd_wait_on_events.
 	 */
 	ev->signaled = !ev->auto_reset || !waitqueue_active(&ev->wq);
+	if (!(++ev->event_age)) {
+		/* Never wrap back to reserved/default event age 0/1 */
+		ev->event_age = 2;
+		WARN_ONCE(1, "event_age wrap back!");
+	}
 
 #if !defined(HAVE_WAIT_QUEUE_ENTRY)
 	list_for_each_entry(waiter, &ev->wq.task_list, wait.task_list)
drivers/gpu/drm/amd/amdkfd/kfd_events.h +1 -0
index 1c62c8dd6460..52ccfd397c2b 100644
@@ -53,6 +53,7 @@ struct signal_page;
 
 struct kfd_event {
 	u32 event_id;
+	u64 event_age;
 
 	bool signaled;
 	bool auto_reset;
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 3/3 drm/amdkfd: don't sleep when event age unmatch

In reply to James Zhu

Don't sleep when event age unmatch, and update last_event_age. It is only for KFD_EVENT_TYPE_SIGNAL which is checked by user space.

---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +13 -0
index b27a79c5f826..23e154811471 100644
@@ -964,6 +964,19 @@ int kfd_wait_on_events(struct kfd_process *p,
 					event_data.event_id);
 		if (ret)
 			goto out_unlock;
+
+		/* last_event_age = 0 reserved for backward compatible */
+		if (event_data.last_event_age &&
+			event_waiters[i].event->event_age != event_data.last_event_age) {
+			event_data.last_event_age = event_waiters[i].event->event_age;
+			WRITE_ONCE(event_waiters[i].activated, true);
+
+			if (copy_to_user(&events[i], &event_data,
+				sizeof(struct kfd_event_data))) {
+				ret = -EFAULT;
+				goto out_unlock;
+			}
+		}
 	}
 
 	/* Check condition once. */
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH 3/3] drm/amdkfd: don't sleep when event age unmatch

Quoted 13 lines · James Zhu

Don't sleep when event age unmatch, and update last_event_age. It is only for KFD_EVENT_TYPE_SIGNAL which is checked by user space.

---
  drivers/gpu/drm/amd/amdkfd/kfd_events.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +0 -0
index b27a79c5f826..23e154811471 100644

drivers/gpu/drm/amd/amdkfd/kfd_events.c int kfd_wait_on_events(struct kfd_process *p, · line 972

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +6 -0
@@ -964,6 +964,19 @@ int kfd_wait_on_events(struct kfd_process *p,
  					event_data.event_id);
  		if (ret)
  			goto out_unlock;
+
+		/* last_event_age = 0 reserved for backward compatible */
+		if (event_data.last_event_age &&
+			event_waiters[i].event->event_age != event_data.last_event_age) {
+			event_data.last_event_age = event_waiters[i].event->event_age;
+			WRITE_ONCE(event_waiters[i].activated, true);

I think this is probably not necessary if you're returning before the first call to test_event_condition.

drivers/gpu/drm/amd/amdkfd/kfd_events.c int kfd_wait_on_events(struct kfd_process *p, · line 979

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +7 -0
+
+			if (copy_to_user(&events[i], &event_data,
+				sizeof(struct kfd_event_data))) {
+				ret = -EFAULT;
+				goto out_unlock;
+			}
+		}

When waiting on multiple events, it would be more efficient to catch all events with outdated age in a single system call, instead of returning after finding the first one. Then return after the loop if it found one or more outdated events.

Also EFAULT is the wrong error code. It means "bad address". I think we could return 0 here because there is really no error. It's just a normal condition to allow user mode to update its event information before going to sleep. If you want a non-0 return code, I'd recommend -EDEADLK, because sleeping without outdated event information can lead to deadlocks. We'd also need to translate that into a meaningful status code in the Thunk to let ROCr know what's going on. I don't see a suitable status code in the current Thunk API for this.

Regards,
  Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +0 -0
  	}
 
  	/* Check condition once. */
#4 lore raw

Re: [PATCH 3/3] drm/amdkfd: don't sleep when event age unmatch

Quoted 19 lines · Felix Kuehling
Quoted 18 lines · James Zhu

Don't sleep when event age unmatch, and update last_event_age. It is only for KFD_EVENT_TYPE_SIGNAL which is checked by user space.

---
  drivers/gpu/drm/amd/amdkfd/kfd_events.c | 13 +++++++++++++

  1 file changed, 13 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +2 -0
index b27a79c5f826..23e154811471 100644
@@ -964,6 +964,19 @@ int kfd_wait_on_events(struct kfd_process *p,
                       event_data.event_id);
           if (ret)
               goto out_unlock;
+
+        /* last_event_age = 0 reserved for backward compatible */
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_events.c int kfd_wait_on_events(struct kfd_process *p, · line 974

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +4 -0
+        if (event_data.last_event_age &&
+            event_waiters[i].event->event_age != 
 event_data.last_event_age) {
+            event_data.last_event_age = 
 event_waiters[i].event->event_age;
+            WRITE_ONCE(event_waiters[i].activated, true);

I think this is probably not necessary if you're returning before the first call to test_event_condition.

[JZ] Currently, the returning is with test_event_condition. Since some conditions needs return with all events signaled.

so all waiters need check and update if any event interrupts are missing here(unmatched event age).

Quoted 11 lines

drivers/gpu/drm/amd/amdkfd/kfd_events.c int kfd_wait_on_events(struct kfd_process *p, · line 979

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +7 -0
+
+            if (copy_to_user(&events[i], &event_data,
+                sizeof(struct kfd_event_data))) {
+                ret = -EFAULT;
+                goto out_unlock;
+            }
+        }

When waiting on multiple events, it would be more efficient to catch all events with outdated age in a single system call, instead of returning after finding the first one. Then return after the loop if it found one or more outdated events.

[JZ] Yes, the code is working in this way, when all events' waiters are activated, the following test_event_condition == KFD_IOC_WAIT_RESULT_COMPLETE, then return to user mode without sleep.

Quoted 5 lines

Also EFAULT is the wrong error code. It means "bad address". I think we could return 0 here because there is really no error. It's just a normal condition to allow user mode to update its event information before going to sleep. If you want a non-0 return code, I'd recommend -EDEADLK, because sleeping without outdated event information can lead
to deadlocks. We'd also need to translate that into a meaningful status code in the Thunk to let ROCr know what's going on. I don't see a suitable status code in the current Thunk API for this.

[JZ] Basically, this failure is for copy_to_user. It will lead to busy wait instead of deadlock.

Quoted 7 lines

**
Regards,
  Felix

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +0 -0
       }

        /* Check condition once. */

maintainer
#5 lore raw

Re: [PATCH 3/3] drm/amdkfd: don't sleep when event age unmatch

Quoted 45 lines · James Zhu
Quoted 19 lines · Felix Kuehling
Quoted 18 lines · James Zhu

Don't sleep when event age unmatch, and update last_event_age. It is only for KFD_EVENT_TYPE_SIGNAL which is checked by user space.

---
  drivers/gpu/drm/amd/amdkfd/kfd_events.c | 13 +++++++++++++

  1 file changed, 13 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +2 -0
index b27a79c5f826..23e154811471 100644
@@ -964,6 +964,19 @@ int kfd_wait_on_events(struct kfd_process *p,
                       event_data.event_id);
           if (ret)
               goto out_unlock;
+
+        /* last_event_age = 0 reserved for backward compatible */
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_events.c int kfd_wait_on_events(struct kfd_process *p, · line 974

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +4 -0
+        if (event_data.last_event_age &&
+            event_waiters[i].event->event_age != 
 event_data.last_event_age) {
+            event_data.last_event_age = 
 event_waiters[i].event->event_age;
+            WRITE_ONCE(event_waiters[i].activated, true);

I think this is probably not necessary if you're returning before the first call to test_event_condition.

[JZ] Currently, the returning is with test_event_condition. Since some conditions needs return with all events signaled.

so all waiters need check and update if any event interrupts are missing here(unmatched event age).

Quoted 10 lines
Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +7 -0
+
+            if (copy_to_user(&events[i], &event_data,
+                sizeof(struct kfd_event_data))) {
+                ret = -EFAULT;
+                goto out_unlock;
+            }
+        }
Quoted 8 lines
Quoted 1 line

When waiting on multiple events, it would be more efficient to catch all events with outdated age in a single system call, instead of returning after finding the first one. Then return after the loop if it found one or more outdated events.

[JZ] Yes, the code is working in this way, when all events' waiters are activated, the following test_event_condition == KFD_IOC_WAIT_RESULT_COMPLETE, then return to user mode without sleep.

Quoted 5 lines

Also EFAULT is the wrong error code. It means "bad address". I think we could return 0 here because there is really no error. It's just a normal condition to allow user mode to update its event information before going to sleep. If you want a non-0 return code, I'd recommend -EDEADLK, because sleeping without outdated event information can
lead to deadlocks. We'd also need to translate that into a meaningful status code in the Thunk to let ROCr know what's going on. I don't see a suitable status code in the current Thunk API for this.

[JZ] Basically, this failure is for copy_to_user. It will lead to busy wait instead of deadlock.

Oh, right, I missed that this is only for the case that copy_to_user fails. EFAULT is the correct error code for this. Then this patch looks good as is.

Regards,
  Felix

Quoted 7 lines
Quoted 7 lines

**
Regards,
  Felix

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_events.c +0 -0
       }

        /* Check condition once. */

maintainer
#6 lore raw
In reply to James Zhu
Quoted 12 lines · James Zhu

Add event age tracking

---
  include/uapi/linux/kfd_ioctl.h | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
include/uapi/linux/kfd_ioctl.h +0 -0
index 7e19a2d1e907..bfbe0006370e 100644

include/uapi/linux/kfd_ioctl.h line 44

Quoted 8 lines
include/uapi/linux/kfd_ioctl.h +2 -1
@@ -38,9 +38,10 @@
   * - 1.10 - Add SMI profiler event log
   * - 1.11 - Add unified memory for ctx save/restore area
   * - 1.12 - Add DMA buf export ioctl
+ * - 1.13 - Update kfd_event_data
   */
  #define KFD_IOCTL_MAJOR_VERSION 1
-#define KFD_IOCTL_MINOR_VERSION 12
+#define KFD_IOCTL_MINOR_VERSION 13

I think minor version 13 is used for the debugger changes that are making their way into the upstream branch right now. You'll probably have to rebase this and use minor version 14 for this.

include/uapi/linux/kfd_ioctl.h struct kfd_event_data { · line 697

Quoted 8 lines
include/uapi/linux/kfd_ioctl.h +1 -0
  /*
   * Debug revision change log
@@ -693,6 +694,7 @@ struct kfd_event_data {
  	union {
  		struct kfd_hsa_memory_exception_data memory_exception_data;
  		struct kfd_hsa_hw_exception_data hw_exception_data;
+		__u64 last_event_age;	/* to and from KFD */

It would be better to create a new struct kfd_hsa_signal_event_data for this to make it obvious what type of event it applies to. And to keep the union more uniform.

Regards,
  Felix

Quoted 3 lines
include/uapi/linux/kfd_ioctl.h +0 -0
  	};				/* From KFD */
  	__u64 kfd_event_data_ext;	/* pointer to an extension structure
  					   for future exception types */
maintainer
#7 lore raw

We'll also need a pointer to the user mode changes in some public repo, or a public email code review of the user mode changes.

Thanks,
  Felix

Quoted 42 lines · Felix Kuehling
Quoted 12 lines · James Zhu

Add event age tracking

---
  include/uapi/linux/kfd_ioctl.h | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
include/uapi/linux/kfd_ioctl.h +0 -0
index 7e19a2d1e907..bfbe0006370e 100644

include/uapi/linux/kfd_ioctl.h line 44

Quoted 8 lines
include/uapi/linux/kfd_ioctl.h +2 -1
@@ -38,9 +38,10 @@
    * - 1.10 - Add SMI profiler event log
    * - 1.11 - Add unified memory for ctx save/restore area
    * - 1.12 - Add DMA buf export ioctl
+ * - 1.13 - Update kfd_event_data
    */
   #define KFD_IOCTL_MAJOR_VERSION 1
-#define KFD_IOCTL_MINOR_VERSION 12
+#define KFD_IOCTL_MINOR_VERSION 13

I think minor version 13 is used for the debugger changes that are making their way into the upstream branch right now. You'll probably have to rebase this and use minor version 14 for this.

include/uapi/linux/kfd_ioctl.h struct kfd_event_data { · line 697

Quoted 7 lines
include/uapi/linux/kfd_ioctl.h +1 -0
     /*
    * Debug revision change log
@@ -693,6 +694,7 @@ struct kfd_event_data {
       union {
           struct kfd_hsa_memory_exception_data memory_exception_data;
           struct kfd_hsa_hw_exception_data hw_exception_data;
+        __u64 last_event_age;    /* to and from KFD */

It would be better to create a new struct kfd_hsa_signal_event_data for this to make it obvious what type of event it applies to. And to keep the union more uniform.

Regards,
  Felix

Quoted 3 lines
include/uapi/linux/kfd_ioctl.h +0 -0
       };                /* From KFD */

      __u64 kfd_event_data_ext;    /* pointer to an extension structure for future exception types */

#8 lore raw
In reply to Felix Kuehling
Quoted 8 lines · Felix Kuehling
Quoted 7 lines · James Zhu

Add event age tracking

---
  include/uapi/linux/kfd_ioctl.h | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

include/uapi/linux/kfd_ioctl.h line 44

Quoted 13 lines
include/uapi/linux/kfd_ioctl.h +2 -1
index 7e19a2d1e907..bfbe0006370e 100644
@@ -38,9 +38,10 @@
    * - 1.10 - Add SMI profiler event log
    * - 1.11 - Add unified memory for ctx save/restore area
    * - 1.12 - Add DMA buf export ioctl
+ * - 1.13 - Update kfd_event_data
    */
   #define KFD_IOCTL_MAJOR_VERSION 1
-#define KFD_IOCTL_MINOR_VERSION 12
+#define KFD_IOCTL_MINOR_VERSION 13

I think minor version 13 is used for the debugger changes that are making their way into the upstream branch right now. You'll probably have to rebase this and use minor version 14 for this.

[JZ] Sure, I will change to version 14.

Quoted 11 lines

include/uapi/linux/kfd_ioctl.h struct kfd_event_data { · line 697

Quoted 9 lines
include/uapi/linux/kfd_ioctl.h +1 -0
     /*
    * Debug revision change log
@@ -693,6 +694,7 @@ struct kfd_event_data {
       union {
           struct kfd_hsa_memory_exception_data memory_exception_data;
           struct kfd_hsa_hw_exception_data hw_exception_data;
+        __u64 last_event_age;    /* to and from KFD */

It would be better to create a new struct kfd_hsa_signal_event_data for this to make it obvious what type of event it applies to. And to keep the union more uniform.

[JZ]Sure. I will update it.

Quoted 8 lines

Regards,
  Felix

Quoted 3 lines
include/uapi/linux/kfd_ioctl.h +0 -0
       };                /* From KFD */

      __u64 kfd_event_data_ext;    /* pointer to an extension structure for future exception types */