Skip to contentSkip to conversation

crypto: virtio-crypto: Handle dataq logic with tasklet

PATCH Superseded Gonglei (Arei) 3 participants 5 messages Nov 20, 2023 – Dec 6, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gonglei (Arei)

author maintainer
#0 lore raw

PATCH 1/1 crypto: virtio-crypto: Handle dataq logic with tasklet

Doing ipsec produces a spinlock recursion warning.
This is due to crypto_finalize_request() being called in the upper half. Move virtual data queue processing of virtio-crypto driver to tasklet.

Fixes dbaf0624ffa57 ("crypto: add virtio-crypto driver")
---
 drivers/crypto/virtio/virtio_crypto_common.h |  2 ++
 drivers/crypto/virtio/virtio_crypto_core.c   | 23 +++++++++++++----------
 2 files changed, 15 insertions(+), 10 deletions(-)
drivers/crypto/virtio/virtio_crypto_common.h +2 -0
index 59a4c02..5c17c6e 100644
@@ -10,6 +10,7 @@
 #include <linux/virtio.h>
 #include <linux/crypto.h>
 #include <linux/spinlock.h>
+#include <linux/interrupt.h>
 #include <crypto/aead.h>
 #include <crypto/aes.h>
 #include <crypto/engine.h>
@@ -28,6 +29,7 @@ struct data_queue {
 	char name[32];
 
 	struct crypto_engine *engine;
+	struct tasklet_struct done_task;
 };
 
 struct virtio_crypto {
drivers/crypto/virtio/virtio_crypto_core.c +13 -10
index 1198bd3..e747f4f 100644
@@ -72,27 +72,28 @@ int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterl
 	return 0;
 }
 
-static void virtcrypto_dataq_callback(struct virtqueue *vq)
+static void virtcrypto_done_task(unsigned long data)
 {
-	struct virtio_crypto *vcrypto = vq->vdev->priv;
+	struct data_queue *data_vq = (struct data_queue *)data;
+	struct virtqueue *vq = data_vq->vq;
 	struct virtio_crypto_request *vc_req;
-	unsigned long flags;
 	unsigned int len;
-	unsigned int qid = vq->index;
 
-	spin_lock_irqsave(&vcrypto->data_vq[qid].lock, flags);
 	do {
 		virtqueue_disable_cb(vq);
 		while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
-			spin_unlock_irqrestore(
-				&vcrypto->data_vq[qid].lock, flags);
 			if (vc_req->alg_cb)
 				vc_req->alg_cb(vc_req, len);
-			spin_lock_irqsave(
-				&vcrypto->data_vq[qid].lock, flags);
 		}
 	} while (!virtqueue_enable_cb(vq));
-	spin_unlock_irqrestore(&vcrypto->data_vq[qid].lock, flags);
+}
+
+static void virtcrypto_dataq_callback(struct virtqueue *vq)
+{
+	struct virtio_crypto *vcrypto = vq->vdev->priv;
+	struct data_queue *dq = &vcrypto->data_vq[vq->index];
+
+	tasklet_schedule(&dq->done_task);
 }
 
 static int virtcrypto_find_vqs(struct virtio_crypto *vi)
@@ -150,6 +151,8 @@ static int virtcrypto_find_vqs(struct virtio_crypto *vi)
 			ret = -ENOMEM;
 			goto err_engine;
 		}
+		tasklet_init(&vi->data_vq[i].done_task, virtcrypto_done_task,
+				(unsigned long)&vi->data_vq[i]);
 	}
 
 	kfree(names);
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 4 lines · Gonglei Arei

Doing ipsec produces a spinlock recursion warning.
This is due to crypto_finalize_request() being called in the upper half. Move virtual data queue processing of virtio-crypto driver to tasklet.

Quoted 8 lines
Fixes dbaf0624ffa57 ("crypto: add virtio-crypto driver")

---
drivers/crypto/virtio/virtio_crypto_common.h | 2 ++
drivers/crypto/virtio/virtio_crypto_core.c | 23 +++++++++++++----------

2 files changed, 15 insertions(+), 10 deletions(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
In reply to Gonglei (Arei)
Quoted 65 lines · Gonglei (Arei)

Doing ipsec produces a spinlock recursion warning.
This is due to crypto_finalize_request() being called in the upper half. Move virtual data queue processing of virtio-crypto driver to tasklet.

Fixes dbaf0624ffa57 ("crypto: add virtio-crypto driver")
---
 drivers/crypto/virtio/virtio_crypto_common.h |  2 ++
 drivers/crypto/virtio/virtio_crypto_core.c   | 23 +++++++++++++----------
 2 files changed, 15 insertions(+), 10 deletions(-)
drivers/crypto/virtio/virtio_crypto_common.h +2 -0
index 59a4c02..5c17c6e 100644
@@ -10,6 +10,7 @@
 #include <linux/virtio.h>
 #include <linux/crypto.h>
 #include <linux/spinlock.h>
+#include <linux/interrupt.h>
 #include <crypto/aead.h>
 #include <crypto/aes.h>
 #include <crypto/engine.h>
@@ -28,6 +29,7 @@ struct data_queue {
 	char name[32];
 
 	struct crypto_engine *engine;
+	struct tasklet_struct done_task;
 };
 
 struct virtio_crypto {
drivers/crypto/virtio/virtio_crypto_core.c +3 -10
index 1198bd3..e747f4f 100644
@@ -72,27 +72,28 @@ int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterl
 	return 0;
 }
 
-static void virtcrypto_dataq_callback(struct virtqueue *vq)
+static void virtcrypto_done_task(unsigned long data)
 {
-	struct virtio_crypto *vcrypto = vq->vdev->priv;
+	struct data_queue *data_vq = (struct data_queue *)data;
+	struct virtqueue *vq = data_vq->vq;
 	struct virtio_crypto_request *vc_req;
-	unsigned long flags;
 	unsigned int len;
-	unsigned int qid = vq->index;
 
-	spin_lock_irqsave(&vcrypto->data_vq[qid].lock, flags);
 	do {
 		virtqueue_disable_cb(vq);
 		while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
-			spin_unlock_irqrestore(
-				&vcrypto->data_vq[qid].lock, flags);
 			if (vc_req->alg_cb)
 				vc_req->alg_cb(vc_req, len);
-			spin_lock_irqsave(
-				&vcrypto->data_vq[qid].lock, flags);
 		}
 	} while (!virtqueue_enable_cb(vq));
-	spin_unlock_irqrestore(&vcrypto->data_vq[qid].lock, flags);

drivers/crypto/virtio/virtio_crypto_core.c int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterl · line 98

Quoted 10 lines
drivers/crypto/virtio/virtio_crypto_core.c +8 -0
+}
+
+static void virtcrypto_dataq_callback(struct virtqueue *vq)
+{
+	struct virtio_crypto *vcrypto = vq->vdev->priv;
+	struct data_queue *dq = &vcrypto->data_vq[vq->index];
+
+	tasklet_schedule(&dq->done_task);
 }
 

Don't we then need to wait for tasklet to complete on device remove?

Quoted 14 lines
drivers/crypto/virtio/virtio_crypto_core.c +2 -0
 static int virtcrypto_find_vqs(struct virtio_crypto *vi)
@@ -150,6 +151,8 @@ static int virtcrypto_find_vqs(struct virtio_crypto *vi)
 			ret = -ENOMEM;
 			goto err_engine;
 		}
+		tasklet_init(&vi->data_vq[i].done_task, virtcrypto_done_task,
+				(unsigned long)&vi->data_vq[i]);
 	}
 
 	kfree(names);
Signature · 1 line
1.8.3.1
author maintainer
#4 lore raw
Quoted 84 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Friday, December 1, 2023 10:51 PM
To: Gonglei (Arei) <[email protected]>
Cc: Herbert Xu <[email protected]>; [email protected]; Halil Pasic <[email protected]>; Jason Wang <[email protected]>; [email protected]; [email protected]; wangyangxin <[email protected]>
Subject: Re: [PATCH] crypto: virtio-crypto: Handle dataq logic with tasklet

drivers/crypto/virtio/virtio_crypto_core.c int virtio_crypto_ctrl_vq_request(struct virtio_crypto

Quoted 40 lines · Gonglei (Arei)

Doing ipsec produces a spinlock recursion warning.
This is due to crypto_finalize_request() being called in the upper half. Move virtual data queue processing of virtio-crypto driver to tasklet.

Fixes dbaf0624ffa57 ("crypto: add virtio-crypto driver")
---
 drivers/crypto/virtio/virtio_crypto_common.h |  2 ++
 drivers/crypto/virtio/virtio_crypto_core.c   | 23 +++++++++++++----------
 2 files changed, 15 insertions(+), 10 deletions(-)
drivers/crypto/virtio/virtio_crypto_common.h +2 -0
index 59a4c02..5c17c6e 100644
@@ -10,6 +10,7 @@
 #include <linux/virtio.h>
 #include <linux/crypto.h>
 #include <linux/spinlock.h>
+#include <linux/interrupt.h>
 #include <crypto/aead.h>
 #include <crypto/aes.h>
 #include <crypto/engine.h>
@@ -28,6 +29,7 @@ struct data_queue {
 	char name[32];
 
 	struct crypto_engine *engine;
+	struct tasklet_struct done_task;
 };
 
 struct virtio_crypto {
drivers/crypto/virtio/virtio_crypto_core.c +0 -0
index 1198bd3..e747f4f 100644
@@ -72,27 +72,28 @@ int virtio_crypto_ctrl_vq_request(struct virtio_crypto

*vcrypto, struct scatterl

Quoted 30 lines
drivers/crypto/virtio/virtio_crypto_core.c +6 -10
 	return 0;
 }
 
-static void virtcrypto_dataq_callback(struct virtqueue *vq)
+static void virtcrypto_done_task(unsigned long data)
 {
-	struct virtio_crypto *vcrypto = vq->vdev->priv;
+	struct data_queue *data_vq = (struct data_queue *)data;
+	struct virtqueue *vq = data_vq->vq;
 	struct virtio_crypto_request *vc_req;
-	unsigned long flags;
 	unsigned int len;
-	unsigned int qid = vq->index;
 
-	spin_lock_irqsave(&vcrypto->data_vq[qid].lock, flags);
 	do {
 		virtqueue_disable_cb(vq);
 		while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
-			spin_unlock_irqrestore(
-				&vcrypto->data_vq[qid].lock, flags);
 			if (vc_req->alg_cb)
 				vc_req->alg_cb(vc_req, len);
-			spin_lock_irqsave(
-				&vcrypto->data_vq[qid].lock, flags);
 		}
 	} while (!virtqueue_enable_cb(vq));
-	spin_unlock_irqrestore(&vcrypto->data_vq[qid].lock, flags);
+}
+
+static void virtcrypto_dataq_callback(struct virtqueue *vq) {
Quoted 10 lines

drivers/crypto/virtio/virtio_crypto_core.c int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterl · line 98

Quoted 6 lines
drivers/crypto/virtio/virtio_crypto_core.c +4 -0
+	struct virtio_crypto *vcrypto = vq->vdev->priv;
+	struct data_queue *dq = &vcrypto->data_vq[vq->index];
+
+	tasklet_schedule(&dq->done_task);
 }
 

Don't we then need to wait for tasklet to complete on device remove?

Yes, we should do it. V2 will be sent soon.

Regards,
-Gonglei

Quoted 15 lines
Quoted 14 lines
drivers/crypto/virtio/virtio_crypto_core.c +1 -0
 static int virtcrypto_find_vqs(struct virtio_crypto *vi) @@ -150,6
+151,8 @@ static int virtcrypto_find_vqs(struct virtio_crypto *vi)
drivers/crypto/virtio/virtio_crypto_core.c +2 -1
 			ret = -ENOMEM;
 			goto err_engine;
 		}
+		tasklet_init(&vi->data_vq[i].done_task, virtcrypto_done_task,
+				(unsigned long)&vi->data_vq[i]);
 	}
 
 	kfree(names);
--

1.8.3.1

author maintainer
#2 lore raw
In reply to Herbert Xu

Hi Herbert,

Quoted 24 lines

-----Original Message-----
From: Herbert Xu [mailto:[email protected]]
Sent: Friday, December 1, 2023 6:11 PM
To: Gonglei (Arei) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; wangyangxin <[email protected]>; Gonglei (Arei) <[email protected]>
Subject: Re: [PATCH] crypto: virtio-crypto: Handle dataq logic with tasklet

Quoted 4 lines · Gonglei Arei

Doing ipsec produces a spinlock recursion warning.
This is due to crypto_finalize_request() being called in the upper half. Move virtual data queue processing of virtio-crypto driver to tasklet.

Quoted 8 lines
Fixes dbaf0624ffa57 ("crypto: add virtio-crypto driver")

---
drivers/crypto/virtio/virtio_crypto_common.h | 2 ++
drivers/crypto/virtio/virtio_crypto_core.c | 23 +++++++++++++----------

2 files changed, 15 insertions(+), 10 deletions(-)
Quoted 1 line

Patch applied. Thanks.

Sorry, pls apply version 2 if possible.

Regards,
-Gonglei

Quoted 4 lines

--
Email: Herbert Xu <[email protected]> Home Page:
http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt