git: f76826b892de - main - ipsec offload: use private taskqueue thread

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 04 Sep 2024 08:50:25 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=f76826b892de38ed25e407c9d5e73aec1229f728

commit f76826b892de38ed25e407c9d5e73aec1229f728
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-08-30 17:17:50 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-09-04 08:49:38 +0000

    ipsec offload: use private taskqueue thread
    
    Using global taskqueue_thread XXX with the vnet tasks scheduled during
    VNET destruction.  VNET shutdown needs to wait for all vnet-scoped
    SAs/SPs to be handled, and doing that from taskqueue_thread task
    deadlocks because the same thread proceeds the removals.
    
    Reviewed by:    markj
    Sponsored by:   NVidia networking
    Differential revision:  https://reviews.freebsd.org/D46494
---
 sys/netipsec/ipsec_offload.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/sys/netipsec/ipsec_offload.c b/sys/netipsec/ipsec_offload.c
index 5e8755d5f243..a06e91ada1a1 100644
--- a/sys/netipsec/ipsec_offload.c
+++ b/sys/netipsec/ipsec_offload.c
@@ -69,6 +69,7 @@
 static struct mtx ipsec_accel_sav_tmp;
 static struct unrhdr *drv_spi_unr;
 static struct mtx ipsec_accel_cnt_lock;
+static struct taskqueue *ipsec_accel_tq;
 
 struct ipsec_accel_install_newkey_tq {
 	struct secasvar *sav;
@@ -166,6 +167,11 @@ ipsec_accel_init(void *arg)
 	mtx_init(&ipsec_accel_cnt_lock, "ipascn", MTX_DEF, 0);
 	drv_spi_unr = new_unrhdr(IPSEC_ACCEL_DRV_SPI_MIN,
 	    IPSEC_ACCEL_DRV_SPI_MAX, &ipsec_accel_sav_tmp);
+	ipsec_accel_tq = taskqueue_create("ipsec_offload", M_WAITOK,
+	    taskqueue_thread_enqueue, &ipsec_accel_tq);
+	(void)taskqueue_start_threads(&ipsec_accel_tq,
+	    1 /* Must be single-threaded */, PWAIT,
+	    "ipsec_offload");
 	ipsec_accel_sa_newkey_p = ipsec_accel_sa_newkey_impl;
 	ipsec_accel_forget_sav_p = ipsec_accel_forget_sav_impl;
 	ipsec_accel_spdadd_p = ipsec_accel_spdadd_impl;
@@ -207,6 +213,8 @@ ipsec_accel_fini(void *arg)
 	clean_unrhdr(drv_spi_unr);	/* avoid panic, should go later */
 	clear_unrhdr(drv_spi_unr);
 	delete_unrhdr(drv_spi_unr);
+	taskqueue_drain_all(ipsec_accel_tq);
+	taskqueue_free(ipsec_accel_tq);
 	mtx_destroy(&ipsec_accel_sav_tmp);
 	mtx_destroy(&ipsec_accel_cnt_lock);
 }
@@ -344,7 +352,7 @@ ipsec_accel_sa_newkey_act(void *context, int pending)
 		/*
 		 * If ipsec_accel_forget_sav() raced with us and set
 		 * the flag, do its work.  Its task cannot execute in
-		 * parallel since taskqueue_thread is single-threaded.
+		 * parallel since ipsec_accel taskqueue is single-threaded.
 		 */
 		if ((sav->accel_flags & SADB_KEY_ACCEL_DEINST) != 0) {
 			tqf = (void *)sav->accel_forget_tq;
@@ -385,7 +393,7 @@ ipsec_accel_sa_newkey_impl(struct secasvar *sav)
 	TASK_INIT(&tq->install_task, 0, ipsec_accel_sa_newkey_act, tq);
 	tq->sav = sav;
 	tq->install_vnet = curthread->td_vnet;	/* XXXKIB liveness */
-	taskqueue_enqueue(taskqueue_thread, &tq->install_task);
+	taskqueue_enqueue(ipsec_accel_tq, &tq->install_task);
 }
 
 static int
@@ -508,7 +516,7 @@ ipsec_accel_forget_sav_impl(struct secasvar *sav)
 	TASK_INIT(&tq->forget_task, 0, ipsec_accel_forget_sav_act, tq);
 	tq->forget_vnet = curthread->td_vnet;
 	tq->sav = sav;
-	taskqueue_enqueue(taskqueue_thread, &tq->forget_task);
+	taskqueue_enqueue(ipsec_accel_tq, &tq->forget_task);
 }
 
 static void
@@ -696,7 +704,7 @@ ipsec_accel_spdadd_impl(struct secpolicy *sp, struct inpcb *inp)
 		in_pcbref(inp);
 	TASK_INIT(&tq->adddel_task, 0, ipsec_accel_spdadd_act, sp);
 	key_addref(sp);
-	taskqueue_enqueue(taskqueue_thread, &tq->adddel_task);
+	taskqueue_enqueue(ipsec_accel_tq, &tq->adddel_task);
 }
 
 static void
@@ -751,7 +759,7 @@ ipsec_accel_spddel_impl(struct secpolicy *sp)
 	tq->adddel_vnet = curthread->td_vnet;
 	TASK_INIT(&tq->adddel_task, 0, ipsec_accel_spddel_act, sp);
 	key_addref(sp);
-	taskqueue_enqueue(taskqueue_thread, &tq->adddel_task);
+	taskqueue_enqueue(ipsec_accel_tq, &tq->adddel_task);
 }
 
 static void
@@ -1124,7 +1132,7 @@ ipsec_accel_sa_lifetime_op_impl(struct secasvar *sav,
 static void
 ipsec_accel_sync_imp(void)
 {
-	taskqueue_drain_all(taskqueue_thread);
+	taskqueue_drain_all(ipsec_accel_tq);
 }
 
 static struct mbuf *