Skip to contentSkip to conversation

drm/amdkfd: Asynchronously free events

PATCH In review Felix Kuehling 2 participants 3 messages Apr 8, 2022 – Apr 11, 2022

Subsystems (3)
lore
Share & tools
lore

Felix Kuehling

author maintainer
#0 lore raw

PATCH 1/1 drm/amdkfd: Asynchronously free events

The synchronize_rcu call in destroy_events can take several ms, which noticeably slows down applications destroying many events. Use kfree_rcu to free the event structure asynchronously and eliminate the synchronize_rcu call in the user thread.

---
 drivers/gpu/drm/amd/amdkfd/kfd_events.c | 3 +--
 drivers/gpu/drm/amd/amdkfd/kfd_events.h | 2 ++
 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +1 -2
index 0fef24b0b915..75847c5d5957 100644
@@ -262,8 +262,7 @@ static void destroy_event(struct kfd_process *p, struct kfd_event *ev)
 		p->signal_event_count--;
 
 	idr_remove(&p->event_idr, ev->event_id);
-	synchronize_rcu();
-	kfree(ev);
+	kfree_rcu(ev, rcu);
 }
 
 static void destroy_events(struct kfd_process *p)
drivers/gpu/drm/amd/amdkfd/kfd_events.h +2 -0
index 55d376f56021..1c62c8dd6460 100644
@@ -70,6 +70,8 @@ struct kfd_event {
 		struct kfd_hsa_memory_exception_data memory_exception_data;
 		struct kfd_hsa_hw_exception_data hw_exception_data;
 	};
+
+	struct rcu_head rcu; /* for asynchronous kfree_rcu */
 };
 
 #define KFD_EVENT_TIMEOUT_IMMEDIATE 0
Signature · 1 line
2.32.0
Reviewed-by on v1 1/1
#1 lore raw

On 2022-04-07 22:39, Felix Kuehling wrote:
The synchronize_rcu call in destroy_events can take several ms, which noticeably slows down applications destroying many events. Use kfree_rcu to free the event structure asynchronously and eliminate the synchronize_rcu call in the user thread. Will need similar fix in kfd_smi_ev_release, as I noticed sometime it is slower to exit the app receiving SMI event.

author maintainer
#2 lore raw

Am 2022-04-11 um 08:52 schrieb philip yang:

Quoted 8 lines · Felix Kuehling
Quoted 4 lines

The synchronize_rcu call in destroy_events can take several ms, which noticeably slows down applications destroying many events. Use kfree_rcu to free the event structure asynchronously and eliminate the synchronize_rcu call in the user thread.

Quoted 4 lines

Will need similar fix in kfd_smi_ev_release, as I noticed sometime it is slower to exit the app receiving SMI event.

Thanks. In the case of kfd_smi_ev_release you'd need to use call_rcu because it's more than a simple kfree when you're freeing the client->fifo. That would have some implications for module unloading, too. Maybe you can simplify it by using DECLARE_KFIFO in struct kfd_smi_client, since we create the fifo with a fixed size anway. That way the cleanup is just a kfree_rcu(client).

Regards,
  Felix

Quoted 33 lines
Quoted 33 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_events.c | 3 +--
  drivers/gpu/drm/amd/amdkfd/kfd_events.h | 2 ++
  2 files changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_events.c +1 -2
index 0fef24b0b915..75847c5d5957 100644
@@ -262,8 +262,7 @@ static void destroy_event(struct kfd_process *p, struct kfd_event *ev)
  		p->signal_event_count--;
 
  	idr_remove(&p->event_idr, ev->event_id);
-	synchronize_rcu();
-	kfree(ev);
+	kfree_rcu(ev, rcu);
  }
 
  static void destroy_events(struct kfd_process *p)
drivers/gpu/drm/amd/amdkfd/kfd_events.h +2 -0
index 55d376f56021..1c62c8dd6460 100644
@@ -70,6 +70,8 @@ struct kfd_event {
  		struct kfd_hsa_memory_exception_data memory_exception_data;
  		struct kfd_hsa_hw_exception_data hw_exception_data;
  	};
+
+	struct rcu_head rcu; /* for asynchronous kfree_rcu */
  };
 
  #define KFD_EVENT_TIMEOUT_IMMEDIATE 0