Skip to contentSkip to conversation

drm/amdkfd: Try to schedule bottom half on same core

PATCH Superseded Felix Kuehling 3 participants 6 messages Aug 10, 2022 – Aug 12, 2022

Subsystems (3)
lore
Share & tools
lore

Felix Kuehling

author maintainer
#0 lore raw

PATCH 1/1 drm/amdkfd: Try to schedule bottom half on same core

On systems that support SMT (hyperthreading) schedule the bottom half of the KFD interrupt handler on the same core. This makes it possible to reserve a core for interrupt handling and have the bottom half run on that same core.

On systems without SMT, pick another core in the same NUMA node, as before.

Use for_each_cpu_wrap instead of open-coding it.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +16 -4
index f5853835f03a..5b12ae69a301 100644
@@ -24,6 +24,7 @@
 #include <linux/bsearch.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
+#include <linux/topology.h>
 #include "kfd_priv.h"
 #include "kfd_device_queue_manager.h"
 #include "kfd_pm4_headers_vi.h"
@@ -801,13 +802,24 @@ static inline void kfd_queue_work(struct workqueue_struct *wq,
 				  struct work_struct *work)
 {
 	int cpu, new_cpu;
+	const struct cpumask *mask = NULL;
 
 	cpu = new_cpu = smp_processor_id();
-	do {
-		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
-		if (cpu_to_node(new_cpu) == numa_node_id())
+
+#if defined(CONFIG_SCHED_SMT)
+	/* CPU threads in the same core */
+	mask = cpu_smt_mask(cpu);
+#endif
+	if (!mask || cpumask_weight(mask) <= 1) {
+		/* CPU threads in the same NUMA node */
+		mask = cpu_cpu_mask(cpu);
+	/* Pick the next online CPU thread in the same core or NUMA node */
+	for_each_cpu_wrap(cpu, mask, cpu+1) {
+		if (cpu != new_cpu && cpu_online(cpu)) {
+			new_cpu = cpu;
 			break;
-	} while (cpu != new_cpu);
+		}
+	}
 
 	queue_work_on(new_cpu, wq, work);
 }
Signature · 1 line
2.32.0
author maintainer
#1 lore raw

Am 2022-08-10 um 19:41 schrieb Felix Kuehling:

Quoted 33 lines

On systems that support SMT (hyperthreading) schedule the bottom half of the KFD interrupt handler on the same core. This makes it possible to reserve a core for interrupt handling and have the bottom half run on that same core.

On systems without SMT, pick another core in the same NUMA node, as before.

Use for_each_cpu_wrap instead of open-coding it.

---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +2 -0
index f5853835f03a..5b12ae69a301 100644
@@ -24,6 +24,7 @@
  #include <linux/bsearch.h>
  #include <linux/pci.h>
  #include <linux/slab.h>
+#include <linux/topology.h>
  #include "kfd_priv.h"
  #include "kfd_device_queue_manager.h"
  #include "kfd_pm4_headers_vi.h"
@@ -801,13 +802,24 @@ static inline void kfd_queue_work(struct workqueue_struct *wq,
  				  struct work_struct *work)
  {
  	int cpu, new_cpu;
+	const struct cpumask *mask = NULL;
 

drivers/gpu/drm/amd/amdkfd/kfd_device.c static inline void kfd_queue_work(struct workqueue_struct *wq, · line 813

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +6 -3
  	cpu = new_cpu = smp_processor_id();
-	do {
-		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
-		if (cpu_to_node(new_cpu) == numa_node_id())
+
+#if defined(CONFIG_SCHED_SMT)
+	/* CPU threads in the same core */
+	mask = cpu_smt_mask(cpu);
+#endif
+	if (!mask || cpumask_weight(mask) <= 1) {

There is a stray { here, left over from when I removed some instrumentation and switched branches. I'll fix that before submitting.

Regards,
  Felix

Quoted 13 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +8 -1
+		/* CPU threads in the same NUMA node */
+		mask = cpu_cpu_mask(cpu);
+	/* Pick the next online CPU thread in the same core or NUMA node */
+	for_each_cpu_wrap(cpu, mask, cpu+1) {
+		if (cpu != new_cpu && cpu_online(cpu)) {
+			new_cpu = cpu;
  			break;
-	} while (cpu != new_cpu);
+		}
+	}
 
  	queue_work_on(new_cpu, wq, work);
  }
author maintainer
#2 lore raw

PATCH v2 1/1 drm/amdkfd: Try to schedule bottom half on same core

In reply to Felix Kuehling

On systems that support SMT (hyperthreading) schedule the bottom half of the KFD interrupt handler on the same core. This makes it possible to reserve a core for interrupt handling and have the bottom half run on that same core.

On systems without SMT, pick another core in the same NUMA node, as before.

Use for_each_cpu_wrap instead of open-coding it.

---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +16 -4
index f5853835f03a..4d1284714e7a 100644
@@ -24,6 +24,7 @@
 #include <linux/bsearch.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
+#include <linux/topology.h>
 #include "kfd_priv.h"
 #include "kfd_device_queue_manager.h"
 #include "kfd_pm4_headers_vi.h"
@@ -801,13 +802,24 @@ static inline void kfd_queue_work(struct workqueue_struct *wq,
 				  struct work_struct *work)
 {
 	int cpu, new_cpu;
+	const struct cpumask *mask = NULL;
 
 	cpu = new_cpu = smp_processor_id();
-	do {
-		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
-		if (cpu_to_node(new_cpu) == numa_node_id())
+
+#if defined(CONFIG_SCHED_SMT)
+	/* CPU threads in the same core */
+	mask = cpu_smt_mask(cpu);
+#endif
+	if (!mask || cpumask_weight(mask) <= 1)
+		/* CPU threads in the same NUMA node */
+		mask = cpu_cpu_mask(cpu);
+	/* Pick the next online CPU thread in the same core or NUMA node */
+	for_each_cpu_wrap(cpu, mask, cpu+1) {
+		if (cpu != new_cpu && cpu_online(cpu)) {
+			new_cpu = cpu;
 			break;
-	} while (cpu != new_cpu);
+		}
+	}
 
 	queue_work_on(new_cpu, wq, work);
 }
Signature · 1 line
2.32.0
Reviewed-by on v2 1/1
#3 lore raw
Quoted 11 lines · Felix Kuehling

On systems that support SMT (hyperthreading) schedule the bottom half of the KFD interrupt handler on the same core. This makes it possible to reserve a core for interrupt handling and have the bottom half run on that same core.

On systems without SMT, pick another core in the same NUMA node, as before.

Use for_each_cpu_wrap instead of open-coding it.

nit-pick below, looks better to use new_cpu as iterator, either way this is

Quoted 35 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 20 ++++++++++++++++----
  1 file changed, 16 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +11 -3
index f5853835f03a..4d1284714e7a 100644
@@ -24,6 +24,7 @@
  #include <linux/bsearch.h>
  #include <linux/pci.h>
  #include <linux/slab.h>
+#include <linux/topology.h>
  #include "kfd_priv.h"
  #include "kfd_device_queue_manager.h"
  #include "kfd_pm4_headers_vi.h"
@@ -801,13 +802,24 @@ static inline void kfd_queue_work(struct workqueue_struct *wq,
  				  struct work_struct *work)
  {
  	int cpu, new_cpu;
+	const struct cpumask *mask = NULL;
 
  	cpu = new_cpu = smp_processor_id();
-	do {
-		new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
-		if (cpu_to_node(new_cpu) == numa_node_id())
+
+#if defined(CONFIG_SCHED_SMT)
+	/* CPU threads in the same core */
+	mask = cpu_smt_mask(cpu);
+#endif
+	if (!mask || cpumask_weight(mask) <= 1)
+		/* CPU threads in the same NUMA node */
+		mask = cpu_cpu_mask(cpu);
+	/* Pick the next online CPU thread in the same core or NUMA node */

drivers/gpu/drm/amd/amdkfd/kfd_device.c static inline void kfd_queue_work(struct workqueue_struct *wq, · line 825

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +5 -1
+	for_each_cpu_wrap(cpu, mask, cpu+1) {
+		if (cpu != new_cpu && cpu_online(cpu)) {
+			new_cpu = cpu;
  			break;
-	} while (cpu != new_cpu);
+		}
+	}
 
  	queue_work_on(new_cpu, wq, work);
  }
	for_each_cpu_wrap(new_cpu, mask, cpu + 1) {
		if (cpu != new_cpu && cpu_online(new_cpu)) {
			cpu = new_cpu;
 			break;
		}
	}
	queue_work_on(cpu, wq, work);
author maintainer
#4 lore raw
Quoted 10 lines · Philip Yang
Quoted 8 lines · Felix Kuehling

On systems that support SMT (hyperthreading) schedule the bottom half of the KFD interrupt handler on the same core. This makes it possible to reserve a core for interrupt handling and have the bottom half run on that same core.

On systems without SMT, pick another core in the same NUMA node, as before.

Quoted 7 lines
Quoted 3 lines

Use for_each_cpu_wrap instead of open-coding it.

nit-pick below, looks better to use new_cpu as iterator, either way this is

Thank you. I think I prefer cpu as the iterator and new_cpu as the variable that holds the CPU we choose to schedule to.

Regards,
  Felix

Quoted 54 lines
Quoted 35 lines

---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 20 ++++++++++++++++----

  1 file changed, 16 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +10 -3
index f5853835f03a..4d1284714e7a 100644
@@ -24,6 +24,7 @@
   #include <linux/bsearch.h>
   #include <linux/pci.h>
   #include <linux/slab.h>
+#include <linux/topology.h>
   #include "kfd_priv.h"
   #include "kfd_device_queue_manager.h"
   #include "kfd_pm4_headers_vi.h"
@@ -801,13 +802,24 @@ static inline void kfd_queue_work(struct 
 workqueue_struct *wq,
                     struct work_struct *work)
   {
       int cpu, new_cpu;
+    const struct cpumask *mask = NULL;
         cpu = new_cpu = smp_processor_id();
-    do {
-        new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
-        if (cpu_to_node(new_cpu) == numa_node_id())
+
+#if defined(CONFIG_SCHED_SMT)
+    /* CPU threads in the same core */
+    mask = cpu_smt_mask(cpu);
+#endif
+    if (!mask || cpumask_weight(mask) <= 1)
+        /* CPU threads in the same NUMA node */
+        mask = cpu_cpu_mask(cpu);

drivers/gpu/drm/amd/amdkfd/kfd_device.c static inline void kfd_queue_work(struct · line 824

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device.c +6 -1
+    /* Pick the next online CPU thread in the same core or NUMA node */
+    for_each_cpu_wrap(cpu, mask, cpu+1) {
+        if (cpu != new_cpu && cpu_online(cpu)) {
+            new_cpu = cpu;
               break;
-    } while (cpu != new_cpu);
+        }
+    }
         queue_work_on(new_cpu, wq, work);
   }

    for_each_cpu_wrap(new_cpu, mask, cpu + 1) { if (cpu != new_cpu && cpu_online(new_cpu)) { cpu = new_cpu;
             break;
        }
    }
    queue_work_on(cpu, wq, work);

maintainer
#5 lore raw
Quoted 17 lines · Felix Kuehling
Quoted 14 lines · Philip Yang
Quoted 11 lines · Felix Kuehling

On systems that support SMT (hyperthreading) schedule the bottom half of the KFD interrupt handler on the same core. This makes it possible to reserve a core for interrupt handling and have the bottom half run on that same core.

On systems without SMT, pick another core in the same NUMA node, as before.

Use for_each_cpu_wrap instead of open-coding it.

Quoted 7 lines
Quoted 4 lines

nit-pick below, looks better to use new_cpu as iterator, either way this is

Thank you. I think I prefer cpu as the iterator and new_cpu as the variable that holds the CPU we choose to schedule to.

I don't think this sort of thing should be in a driver.

queue_work_node seems like it should be used or enhanced. Doing this sort of thing in driver code should be the last place to do it.

At least please task someone to work on an upstream answer to this sort of hacky downstream thing.

Dave.