git: 353c1239fb7d - stable/13 - mlx5en: Fix TLS worker thread race.

From: Hans Petter Selasky <hselasky_at_FreeBSD.org>
Date: Thu, 24 Feb 2022 10:04:25 UTC
The branch stable/13 has been updated by hselasky:

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

commit 353c1239fb7d7660ecf032ca9ce6e9070bd48cce
Author:     Hans Petter Selasky <hselasky@FreeBSD.org>
AuthorDate: 2022-02-24 09:59:14 +0000
Commit:     Hans Petter Selasky <hselasky@FreeBSD.org>
CommitDate: 2022-02-24 09:59:14 +0000

    mlx5en: Fix TLS worker thread race.
    
    Create a dedicated free state, in case the taskqueue worker is still pending,
    to avoid re-activation of a freed send tag.
    
    Sponsored by:   NVIDIA Networking
    
    (cherry picked from commit 015f22f5d0338882ab4a1e1585622ff3568dface)
---
 sys/dev/mlx5/mlx5_en/en_hw_tls.h      |  3 ++-
 sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c | 11 +++++++----
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/sys/dev/mlx5/mlx5_en/en_hw_tls.h b/sys/dev/mlx5/mlx5_en/en_hw_tls.h
index 7ba00fc1f363..8784c07b750d 100644
--- a/sys/dev/mlx5/mlx5_en/en_hw_tls.h
+++ b/sys/dev/mlx5/mlx5_en/en_hw_tls.h
@@ -55,7 +55,8 @@ struct mlx5e_tls_tag {
 #define	MLX5E_TLS_ST_INIT 0
 #define	MLX5E_TLS_ST_SETUP 1
 #define	MLX5E_TLS_ST_TXRDY 2
-#define	MLX5E_TLS_ST_FREED 3
+#define	MLX5E_TLS_ST_RELEASE 3
+#define	MLX5E_TLS_ST_FREED 4
 	struct work_struct work;
 
 	uint32_t dek_index_ok:1;
diff --git a/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c b/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c
index e28fddcfd116..359bd9bddc7f 100644
--- a/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c
+++ b/sys/dev/mlx5/mlx5_en/mlx5_en_hw_tls.c
@@ -98,9 +98,10 @@ mlx5e_tls_tag_zfini(void *mem, int size)
 static void
 mlx5e_tls_tag_zfree(struct mlx5e_tls_tag *ptag)
 {
+	/* make sure any unhandled taskqueue events are ignored */
+	ptag->state = MLX5E_TLS_ST_FREED;
 
 	/* reset some variables */
-	ptag->state = MLX5E_TLS_ST_INIT;
 	ptag->dek_index = 0;
 	ptag->dek_index_ok = 0;
 
@@ -225,7 +226,7 @@ mlx5e_tls_work(struct work_struct *work)
 		MLX5E_TLS_TAG_UNLOCK(ptag);
 		break;
 
-	case MLX5E_TLS_ST_FREED:
+	case MLX5E_TLS_ST_RELEASE:
 		/* try to destroy DEK context by ID */
 		if (ptag->dek_index_ok)
 			err = mlx5_encryption_key_destroy(priv->mdev, ptag->dek_index);
@@ -298,7 +299,6 @@ mlx5e_tls_snd_tag_alloc(struct ifnet *ifp,
 		return (ENOMEM);
 
 	/* sanity check default values */
-	MPASS(ptag->state == MLX5E_TLS_ST_INIT);
 	MPASS(ptag->dek_index == 0);
 	MPASS(ptag->dek_index_ok == 0);
 
@@ -404,6 +404,9 @@ mlx5e_tls_snd_tag_alloc(struct ifnet *ifp,
 	m_snd_tag_init(&ptag->tag, ifp, params->hdr.type);
 	*ppmt = &ptag->tag;
 
+	/* reset state */
+	ptag->state = MLX5E_TLS_ST_INIT;
+
 	queue_work(priv->tls.wq, &ptag->work);
 	flush_work(&ptag->work);
 
@@ -470,7 +473,7 @@ mlx5e_tls_snd_tag_free(struct m_snd_tag *pmt)
 	m_snd_tag_rele(ptag->rl_tag);
 
 	MLX5E_TLS_TAG_LOCK(ptag);
-	ptag->state = MLX5E_TLS_ST_FREED;
+	ptag->state = MLX5E_TLS_ST_RELEASE;
 	MLX5E_TLS_TAG_UNLOCK(ptag);
 
 	priv = ptag->tag.ifp->if_softc;