Skip to contentSkip to conversation

crypto: AF_ALG - consolidation

PATCH In review Stephan Müller 2 participants 22 messages Jul 31, 2017 – Aug 1, 2017 Crypto API

lore
Share & tools
lore
#12 lore raw

PATCH 12/16 crypto: AF_ALG - consolidate waking up caller for TX data

In reply to Stephan Müller

Consoliate aead_data_wakeup, skcipher_data_wakeup
==> af_alg_data_wakeup

---
 crypto/af_alg.c         | 26 ++++++++++++++++++++++++++
 crypto/algif_aead.c     | 24 ++----------------------
 crypto/algif_skcipher.c | 24 ++----------------------
 include/crypto/if_alg.h |  1 +
 4 files changed, 31 insertions(+), 44 deletions(-)
crypto/af_alg.c +26 -0
index c1fe7c5f1b2e..d1e0c176495f 100644
@@ -808,6 +808,32 @@ int af_alg_wait_for_data(struct sock *sk, unsigned flags)
 }
 EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
 
+/**
+ * af_alg_data_wakeup - wakeup caller when new data can be sent to kernel
+ *
+ * @sk socket of connection to user space
+ */
+
+void af_alg_data_wakeup(struct sock *sk)
+{
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	struct socket_wq *wq;
+
+	if (!ctx->used)
+		return;
+
+	rcu_read_lock();
+	wq = rcu_dereference(sk->sk_wq);
+	if (skwq_has_sleeper(wq))
+		wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
+							   POLLRDNORM |
+							   POLLRDBAND);
+	sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
+	rcu_read_unlock();
+}
+EXPORT_SYMBOL_GPL(af_alg_data_wakeup);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +2 -22
index 8db8c10401d6..7a3b81545363 100644
@@ -35,7 +35,6 @@
 #include <linux/init.h>
 #include <linux/list.h>
 #include <linux/kernel.h>
-#include <linux/sched/signal.h>
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
@@ -64,25 +63,6 @@ static inline bool aead_sufficient_data(struct sock *sk)
 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
 }
 
-static void aead_data_wakeup(struct sock *sk)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	struct socket_wq *wq;
-
-	if (!ctx->used)
-		return;
-
-	rcu_read_lock();
-	wq = rcu_dereference(sk->sk_wq);
-	if (skwq_has_sleeper(wq))
-		wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
-							   POLLRDNORM |
-							   POLLRDBAND);
-	sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
-	rcu_read_unlock();
-}
-
 static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 {
 	struct sock *sk = sock->sk;
@@ -220,7 +200,7 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	ctx->more = msg->msg_flags & MSG_MORE;
 
 unlock:
-	aead_data_wakeup(sk);
+	af_alg_data_wakeup(sk);
 	release_sock(sk);
 
 	return err ?: copied;
@@ -273,7 +253,7 @@ static ssize_t aead_sendpage(struct socket *sock, struct page *page,
 done:
 	ctx->more = flags & MSG_MORE;
 unlock:
-	aead_data_wakeup(sk);
+	af_alg_data_wakeup(sk);
 	release_sock(sk);
 
 	return err ?: size;
crypto/algif_skcipher.c +2 -22
index 572a5a632ea1..1b16fb1161b9 100644
@@ -33,7 +33,6 @@
 #include <linux/init.h>
 #include <linux/list.h>
 #include <linux/kernel.h>
-#include <linux/sched/signal.h>
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/net.h>
@@ -44,25 +43,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static void skcipher_data_wakeup(struct sock *sk)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	struct socket_wq *wq;
-
-	if (!ctx->used)
-		return;
-
-	rcu_read_lock();
-	wq = rcu_dereference(sk->sk_wq);
-	if (skwq_has_sleeper(wq))
-		wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
-							   POLLRDNORM |
-							   POLLRDBAND);
-	sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
-	rcu_read_unlock();
-}
-
 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
 			    size_t size)
 {
@@ -196,7 +176,7 @@ static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
 	ctx->more = msg->msg_flags & MSG_MORE;
 
 unlock:
-	skcipher_data_wakeup(sk);
+	af_alg_data_wakeup(sk);
 	release_sock(sk);
 
 	return copied ?: err;
@@ -247,7 +227,7 @@ static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
 	ctx->more = flags & MSG_MORE;
 
 unlock:
-	skcipher_data_wakeup(sk);
+	af_alg_data_wakeup(sk);
 	release_sock(sk);
 
 	return err ?: size;
include/crypto/if_alg.h +1 -0
index c3b02276f38f..dae18aec9792 100644
@@ -248,5 +248,6 @@ void af_alg_free_areq_sgls(struct af_alg_async_req *areq);
 int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags);
 void af_alg_wmem_wakeup(struct sock *sk);
 int af_alg_wait_for_data(struct sock *sk, unsigned flags);
+void af_alg_data_wakeup(struct sock *sk);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#13 lore raw

PATCH 13/16 crypto: AF_ALG - consolidate sendmsg implementation

In reply to Stephan Müller

Consoliate aead_sendmsg, skcipher_sendmsg
==> af_alg_sendmsg

The following changes to the sendmsg implementation aead_sendmsg have been applied:

* uses size_t in min_t calculation for obtaining len

* switch the use of the variable size to variable len to determine the sndbuf size (both variables have the same content here, consolidate implementation with skcipher_sendmsg)

* return code determination is changed to be identical to skcipher_sendmsg

The following changes to the sendmsg implementation skcipher_sendmsg have been applied:

* use of size_t instead of unsigned long for calculating the available memory size in the page storing the user data

* scope of variable i is reduced to be compliant with aead_sendmsg

* type of variable is switched from int to unsigned int to be compliant with aead_sendmsg

---
 crypto/af_alg.c         | 157 ++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 132 +---------------------------------------
 crypto/algif_skcipher.c | 127 +--------------------------------------
 include/crypto/if_alg.h |   2 +
 4 files changed, 161 insertions(+), 257 deletions(-)
crypto/af_alg.c +157 -0
index d1e0c176495f..716a73ff5309 100644
@@ -834,6 +834,163 @@ void af_alg_data_wakeup(struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(af_alg_data_wakeup);
 
+/**
+ * af_alg_sendmsg - implementation of sendmsg system call handler
+ *
+ * The sendmsg system call handler obtains the user data and stores it
+ * in ctx->tsgl_list. This implies allocation of the required numbers of
+ * struct af_alg_tsgl.
+ *
+ * In addition, the ctx is filled with the information sent via CMSG.
+ *
+ * @sock socket of connection to user space
+ * @msg message from user space
+ * @size size of message from user space
+ * @ivsize the size of the IV for the cipher operation to verify that the
+ *	   user-space-provided IV has the right size
+ * @return the number of copied data upon success, < 0 upon error
+ */
+int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
+		   unsigned int ivsize)
+{
+	struct sock *sk = sock->sk;
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	struct af_alg_tsgl *sgl;
+	struct af_alg_control con = {};
+	long copied = 0;
+	bool enc = 0;
+	bool init = 0;
+	int err = 0;
+
+	if (msg->msg_controllen) {
+		err = af_alg_cmsg_send(msg, &con);
+		if (err)
+			return err;
+
+		init = 1;
+		switch (con.op) {
+		case ALG_OP_ENCRYPT:
+			enc = 1;
+			break;
+		case ALG_OP_DECRYPT:
+			enc = 0;
+			break;
+		default:
+			return -EINVAL;
+		}
+
+		if (con.iv && con.iv->ivlen != ivsize)
+			return -EINVAL;
+	}
+
+	lock_sock(sk);
+	if (!ctx->more && ctx->used) {
+		err = -EINVAL;
+		goto unlock;
+	}
+
+	if (init) {
+		ctx->enc = enc;
+		if (con.iv)
+			memcpy(ctx->iv, con.iv->iv, ivsize);
+
+		ctx->aead_assoclen = con.aead_assoclen;
+	}
+
+	while (size) {
+		struct scatterlist *sg;
+		size_t len = size;
+		size_t plen;
+
+		/* use the existing memory in an allocated page */
+		if (ctx->merge) {
+			sgl = list_entry(ctx->tsgl_list.prev,
+					 struct af_alg_tsgl, list);
+			sg = sgl->sg + sgl->cur - 1;
+			len = min_t(size_t, len,
+				    PAGE_SIZE - sg->offset - sg->length);
+
+			err = memcpy_from_msg(page_address(sg_page(sg)) +
+					      sg->offset + sg->length,
+					      msg, len);
+			if (err)
+				goto unlock;
+
+			sg->length += len;
+			ctx->merge = (sg->offset + sg->length) &
+				     (PAGE_SIZE - 1);
+
+			ctx->used += len;
+			copied += len;
+			size -= len;
+			continue;
+		}
+
+		if (!af_alg_writable(sk)) {
+			err = af_alg_wait_for_wmem(sk, msg->msg_flags);
+			if (err)
+				goto unlock;
+		}
+
+		/* allocate a new page */
+		len = min_t(unsigned long, len, af_alg_sndbuf(sk));
+
+		err = af_alg_alloc_tsgl(sk);
+		if (err)
+			goto unlock;
+
+		sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
+				 list);
+		sg = sgl->sg;
+		if (sgl->cur)
+			sg_unmark_end(sg + sgl->cur - 1);
+
+		do {
+			unsigned int i = sgl->cur;
+
+			plen = min_t(size_t, len, PAGE_SIZE);
+
+			sg_assign_page(sg + i, alloc_page(GFP_KERNEL));
+			if (!sg_page(sg + i)) {
+				err = -ENOMEM;
+				goto unlock;
+			}
+
+			err = memcpy_from_msg(page_address(sg_page(sg + i)),
+					      msg, plen);
+			if (err) {
+				__free_page(sg_page(sg + i));
+				sg_assign_page(sg + i, NULL);
+				goto unlock;
+			}
+
+			sg[i].length = plen;
+			len -= plen;
+			ctx->used += plen;
+			copied += plen;
+			size -= plen;
+			sgl->cur++;
+		} while (len && sgl->cur < MAX_SGL_ENTS);
+
+		if (!size)
+			sg_mark_end(sg + sgl->cur - 1);
+
+		ctx->merge = plen & (PAGE_SIZE - 1);
+	}
+
+	err = 0;
+
+	ctx->more = msg->msg_flags & MSG_MORE;
+
+unlock:
+	af_alg_data_wakeup(sk);
+	release_sock(sk);
+
+	return copied ?: err;
+}
+EXPORT_SYMBOL_GPL(af_alg_sendmsg);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +1 -131

142 lines not on this page · raw message

crypto/algif_skcipher.c +1 -126

137 lines not on this page · raw message

include/crypto/if_alg.h +2 -0
index dae18aec9792..c67d9a35ffd3 100644
@@ -249,5 +249,7 @@ int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags);
 void af_alg_wmem_wakeup(struct sock *sk);
 int af_alg_wait_for_data(struct sock *sk, unsigned flags);
 void af_alg_data_wakeup(struct sock *sk);
+int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
+		   unsigned int ivsize);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#14 lore raw

PATCH 14/16 crypto: AF_ALG - consolidate sendpage implementation

In reply to Stephan Müller

Consoliate aead_sendpage, skcipher_sendpage
==> af_alg_sendpage

The following changes to aead_sendpage have been applied:

* remove superfluous err = 0

---
 crypto/af_alg.c         | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 57 ++----------------------------------------------
 crypto/algif_skcipher.c | 55 ++--------------------------------------------
 include/crypto/if_alg.h |  2 ++
 4 files changed, 64 insertions(+), 108 deletions(-)
crypto/af_alg.c +58 -0
index 716a73ff5309..5a33d6629a67 100644
@@ -991,6 +991,64 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
 }
 EXPORT_SYMBOL_GPL(af_alg_sendmsg);
 
+/**
+ * af_alg_sendpage - sendpage system call handler
+ *
+ * This is a generic implementation of sendpage to fill ctx->tsgl_list.
+ */
+ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
+			int offset, size_t size, int flags)
+{
+	struct sock *sk = sock->sk;
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	struct af_alg_tsgl *sgl;
+	int err = -EINVAL;
+
+	if (flags & MSG_SENDPAGE_NOTLAST)
+		flags |= MSG_MORE;
+
+	lock_sock(sk);
+	if (!ctx->more && ctx->used)
+		goto unlock;
+
+	if (!size)
+		goto done;
+
+	if (!af_alg_writable(sk)) {
+		err = af_alg_wait_for_wmem(sk, flags);
+		if (err)
+			goto unlock;
+	}
+
+	err = af_alg_alloc_tsgl(sk);
+	if (err)
+		goto unlock;
+
+	ctx->merge = 0;
+	sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
+
+	if (sgl->cur)
+		sg_unmark_end(sgl->sg + sgl->cur - 1);
+
+	sg_mark_end(sgl->sg + sgl->cur);
+
+	get_page(page);
+	sg_set_page(sgl->sg + sgl->cur, page, size, offset);
+	sgl->cur++;
+	ctx->used += size;
+
+done:
+	ctx->more = flags & MSG_MORE;
+
+unlock:
+	af_alg_data_wakeup(sk);
+	release_sock(sk);
+
+	return err ?: size;
+}
+EXPORT_SYMBOL_GPL(af_alg_sendpage);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +2 -55

75 lines not on this page · raw message

crypto/algif_skcipher.c +2 -53

73 lines not on this page · raw message

include/crypto/if_alg.h +2 -0
index c67d9a35ffd3..d271af60bfaf 100644
@@ -251,5 +251,7 @@ int af_alg_wait_for_data(struct sock *sk, unsigned flags);
 void af_alg_data_wakeup(struct sock *sk);
 int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
 		   unsigned int ivsize);
+ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
+			int offset, size_t size, int flags);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#15 lore raw

PATCH 15/16 crypto: AF_ALG - consolidate AIO callback handler

In reply to Stephan Müller

Consoliate aead_async_cb, skcipher_async_cb ==> af_alg_async_cb

algif_skcipher has been changed to store the number of output bytes in areq->outlen before the AIO callback is triggered.

---
 crypto/af_alg.c         | 31 +++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 23 +----------------------
 crypto/algif_skcipher.c | 27 +++++----------------------
 include/crypto/if_alg.h |  1 +
 4 files changed, 38 insertions(+), 44 deletions(-)
crypto/af_alg.c +31 -0
index 5a33d6629a67..ef37fc3a9015 100644
@@ -1049,6 +1049,37 @@ ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
 }
 EXPORT_SYMBOL_GPL(af_alg_sendpage);
 
+/**
+ * af_alg_async_cb - AIO callback handler
+ *
+ * This handler cleans up the struct af_alg_async_req upon completion of the
+ * AIO operation.
+ *
+ * The number of bytes to be generated with the AIO operation must be set
+ * in areq->outlen before the AIO callback handler is invoked.
+ */
+void af_alg_async_cb(struct crypto_async_request *_req, int err)
+{
+	struct af_alg_async_req *areq = _req->data;
+	struct sock *sk = areq->sk;
+	struct kiocb *iocb = areq->iocb;
+	unsigned int resultlen;
+
+	lock_sock(sk);
+
+	/* Buffer size written by crypto operation. */
+	resultlen = areq->outlen;
+
+	af_alg_free_areq_sgls(areq);
+	sock_kfree_s(sk, areq, areq->areqlen);
+	__sock_put(sk);
+
+	iocb->ki_complete(iocb, err ? err : resultlen, 0);
+
+	release_sock(sk);
+}
+EXPORT_SYMBOL_GPL(af_alg_async_cb);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +1 -22
index f655fadb9075..8add641cc667 100644
@@ -76,27 +76,6 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	return af_alg_sendmsg(sock, msg, size, ivsize);
 }
 
-static void aead_async_cb(struct crypto_async_request *_req, int err)
-{
-	struct af_alg_async_req *areq = _req->data;
-	struct sock *sk = areq->sk;
-	struct kiocb *iocb = areq->iocb;
-	unsigned int resultlen;
-
-	lock_sock(sk);
-
-	/* Buffer size written by crypto operation. */
-	resultlen = areq->outlen;
-
-	af_alg_free_areq_sgls(areq);
-	sock_kfree_s(sk, areq, areq->areqlen);
-	__sock_put(sk);
-
-	iocb->ki_complete(iocb, err ? err : resultlen, 0);
-
-	release_sock(sk);
-}
-
 static int crypto_aead_copy_sgl(struct crypto_skcipher *null_tfm,
 				struct scatterlist *src,
 				struct scatterlist *dst, unsigned int len)
@@ -341,7 +320,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 		areq->iocb = msg->msg_iocb;
 		aead_request_set_callback(&areq->cra_u.aead_req,
 					  CRYPTO_TFM_REQ_MAY_BACKLOG,
-					  aead_async_cb, areq);
+					  af_alg_async_cb, areq);
 		err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
 				 crypto_aead_decrypt(&areq->cra_u.aead_req);
 	} else {
crypto/algif_skcipher.c +5 -22
index c577aaaa9fd8..5134df529833 100644
@@ -57,27 +57,6 @@ static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
 	return af_alg_sendmsg(sock, msg, size, ivsize);
 }
 
-static void skcipher_async_cb(struct crypto_async_request *req, int err)
-{
-	struct af_alg_async_req *areq = req->data;
-	struct sock *sk = areq->sk;
-	struct kiocb *iocb = areq->iocb;
-	unsigned int resultlen;
-
-	lock_sock(sk);
-
-	/* Buffer size written by crypto operation. */
-	resultlen = areq->cra_u.skcipher_req.cryptlen;
-
-	af_alg_free_areq_sgls(areq);
-	sock_kfree_s(sk, areq, areq->areqlen);
-	__sock_put(sk);
-
-	iocb->ki_complete(iocb, err ? err : resultlen, 0);
-
-	release_sock(sk);
-}
-
 static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 			     size_t ignored, int flags)
 {
@@ -189,7 +168,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 		areq->iocb = msg->msg_iocb;
 		skcipher_request_set_callback(&areq->cra_u.skcipher_req,
 					      CRYPTO_TFM_REQ_MAY_SLEEP,
-					      skcipher_async_cb, areq);
+					      af_alg_async_cb, areq);
 		err = ctx->enc ?
 			crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
 			crypto_skcipher_decrypt(&areq->cra_u.skcipher_req);
@@ -209,6 +188,10 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 	/* AIO operation in progress */
 	if (err == -EINPROGRESS) {
 		sock_hold(sk);
+
+		/* Remember output size that will be generated. */
+		areq->outlen = len;
+
 		return -EIOCBQUEUED;
 	}
 
include/crypto/if_alg.h +1 -0
index d271af60bfaf..ef08fb4a599e 100644
@@ -253,5 +253,6 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
 		   unsigned int ivsize);
 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
 			int offset, size_t size, int flags);
+void af_alg_async_cb(struct crypto_async_request *_req, int err);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#16 lore raw

PATCH 16/16 crypto: AF_ALG - consolidate poll syscall handler

In reply to Stephan Müller

Consoliate aead_poll, skcipher_poll ==> af_alg_poll

The POLLIN / POLLRDNORM is now set when either not more data is given or the kernel is supplied with data. This is consistent to the wakeup from sleep when the kernel waits for data.

---
 crypto/af_alg.c         | 24 ++++++++++++++++++++++++
 crypto/algif_aead.c     | 24 ++----------------------
 crypto/algif_skcipher.c | 23 ++---------------------
 include/crypto/if_alg.h |  2 ++
 4 files changed, 30 insertions(+), 43 deletions(-)
crypto/af_alg.c +24 -0
index ef37fc3a9015..ae0e93103c76 100644
@@ -1080,6 +1080,30 @@ void af_alg_async_cb(struct crypto_async_request *_req, int err)
 }
 EXPORT_SYMBOL_GPL(af_alg_async_cb);
 
+/**
+ * af_alg_poll - poll system call handler
+ */
+unsigned int af_alg_poll(struct file *file, struct socket *sock,
+			 poll_table *wait)
+{
+	struct sock *sk = sock->sk;
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	unsigned int mask;
+
+	sock_poll_wait(file, sk_sleep(sk), wait);
+	mask = 0;
+
+	if (!ctx->more || ctx->used)
+		mask |= POLLIN | POLLRDNORM;
+
+	if (af_alg_writable(sk))
+		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
+
+	return mask;
+}
+EXPORT_SYMBOL_GPL(af_alg_poll);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +2 -22
index 8add641cc667..478bacf30079 100644
@@ -385,26 +385,6 @@ static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
 	return ret;
 }
 
-static unsigned int aead_poll(struct file *file, struct socket *sock,
-			      poll_table *wait)
-{
-	struct sock *sk = sock->sk;
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	unsigned int mask;
-
-	sock_poll_wait(file, sk_sleep(sk), wait);
-	mask = 0;
-
-	if (!ctx->more)
-		mask |= POLLIN | POLLRDNORM;
-
-	if (af_alg_writable(sk))
-		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
-
-	return mask;
-}
-
 static struct proto_ops algif_aead_ops = {
 	.family		=	PF_ALG,
 
@@ -424,7 +404,7 @@ static struct proto_ops algif_aead_ops = {
 	.sendmsg	=	aead_sendmsg,
 	.sendpage	=	af_alg_sendpage,
 	.recvmsg	=	aead_recvmsg,
-	.poll		=	aead_poll,
+	.poll		=	af_alg_poll,
 };
 
 static int aead_check_key(struct socket *sock)
@@ -520,7 +500,7 @@ static struct proto_ops algif_aead_ops_nokey = {
 	.sendmsg	=	aead_sendmsg_nokey,
 	.sendpage	=	aead_sendpage_nokey,
 	.recvmsg	=	aead_recvmsg_nokey,
-	.poll		=	aead_poll,
+	.poll		=	af_alg_poll,
 };
 
 static void *aead_bind(const char *name, u32 type, u32 mask)
crypto/algif_skcipher.c +2 -21
index 5134df529833..5bd85c1dd188 100644
@@ -236,25 +236,6 @@ static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 	return ret;
 }
 
-static unsigned int skcipher_poll(struct file *file, struct socket *sock,
-				  poll_table *wait)
-{
-	struct sock *sk = sock->sk;
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	unsigned int mask;
-
-	sock_poll_wait(file, sk_sleep(sk), wait);
-	mask = 0;
-
-	if (ctx->used)
-		mask |= POLLIN | POLLRDNORM;
-
-	if (af_alg_writable(sk))
-		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
-
-	return mask;
-}
 
 static struct proto_ops algif_skcipher_ops = {
 	.family		=	PF_ALG,
@@ -275,7 +256,7 @@ static struct proto_ops algif_skcipher_ops = {
 	.sendmsg	=	skcipher_sendmsg,
 	.sendpage	=	af_alg_sendpage,
 	.recvmsg	=	skcipher_recvmsg,
-	.poll		=	skcipher_poll,
+	.poll		=	af_alg_poll,
 };
 
 static int skcipher_check_key(struct socket *sock)
@@ -371,7 +352,7 @@ static struct proto_ops algif_skcipher_ops_nokey = {
 	.sendmsg	=	skcipher_sendmsg_nokey,
 	.sendpage	=	skcipher_sendpage_nokey,
 	.recvmsg	=	skcipher_recvmsg_nokey,
-	.poll		=	skcipher_poll,
+	.poll		=	af_alg_poll,
 };
 
 static void *skcipher_bind(const char *name, u32 type, u32 mask)
include/crypto/if_alg.h +2 -0
index ef08fb4a599e..7bac3fee6061 100644
@@ -254,5 +254,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
 ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
 			int offset, size_t size, int flags);
 void af_alg_async_cb(struct crypto_async_request *_req, int err);
+unsigned int af_alg_poll(struct file *file, struct socket *sock,
+			 poll_table *wait);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#4 lore raw

PATCH 4/16 crypto: AF_ALG - consolidate RX buffer service functions

In reply to Stephan Müller

Consolidate the common functions verifying the RX buffers:

* skcipher_rcvbuf, aead_rcvbuf ==> af_alg_rcvbuf

* skcipher_readable, aead_readable ==> af_alg_readable

---
 crypto/algif_aead.c     | 16 +---------------
 crypto/algif_skcipher.c | 16 +---------------
 include/crypto/if_alg.h | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+), 30 deletions(-)
crypto/algif_aead.c +1 -15
index c923ce29bfe3..cdcf186296bd 100644
@@ -47,20 +47,6 @@ struct aead_tfm {
 	struct crypto_skcipher *null_tfm;
 };
 
-static inline int aead_rcvbuf(struct sock *sk)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-
-	return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
-			  ctx->rcvused, 0);
-}
-
-static inline bool aead_readable(struct sock *sk)
-{
-	return PAGE_SIZE <= aead_rcvbuf(sk);
-}
-
 static inline bool aead_sufficient_data(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
@@ -655,7 +641,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 		size_t seglen;
 
 		/* limit the amount of readable buffers */
-		if (!aead_readable(sk))
+		if (!af_alg_readable(sk))
 			break;
 
 		if (!ctx->used) {
crypto/algif_skcipher.c +1 -15
index a5c6643f2abe..081df927fb8b 100644
@@ -44,20 +44,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static inline int skcipher_rcvbuf(struct sock *sk)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-
-	return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
-			  ctx->rcvused, 0);
-}
-
-static inline bool skcipher_readable(struct sock *sk)
-{
-	return PAGE_SIZE <= skcipher_rcvbuf(sk);
-}
-
 static int skcipher_alloc_tsgl(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
@@ -532,7 +518,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 		size_t seglen;
 
 		/* limit the amount of readable buffers */
-		if (!skcipher_readable(sk))
+		if (!af_alg_readable(sk))
 			break;
 
 		if (!ctx->used) {
include/crypto/if_alg.h +26 -0
index 79d215f65acf..e1ac57c32d85 100644
@@ -214,4 +214,30 @@ static inline bool af_alg_writable(struct sock *sk)
 	return PAGE_SIZE <= af_alg_sndbuf(sk);
 }
 
+/**
+ * Size of available buffer used by kernel for the RX user space operation.
+ *
+ * @sk socket of connection to user space
+ * @return number of bytes still available
+ */
+static inline int af_alg_rcvbuf(struct sock *sk)
+{
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+
+	return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
+			  ctx->rcvused, 0);
+}
+
+/**
+ * Can the RX buffer still be written to?
+ *
+ * @sk socket of connection to user space
+ * @return true => writable, false => not writable
+ */
+static inline bool af_alg_readable(struct sock *sk)
+{
+	return PAGE_SIZE <= af_alg_rcvbuf(sk);
+}
+
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#5 lore raw

PATCH 5/16 crypto: AF_ALG - consolidate TX SGL allocation

In reply to Stephan Müller

Consoliate aead_alloc_tsgl, skcipher_alloc_tsgl ==> af_alg_alloc_tsgl

---
 crypto/af_alg.c         | 37 +++++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 34 ++--------------------------------
 crypto/algif_skcipher.c | 34 ++--------------------------------
 include/crypto/if_alg.h |  2 ++
 4 files changed, 43 insertions(+), 64 deletions(-)
crypto/af_alg.c +37 -0
index 92a3d540d920..87138c4b5a0f 100644
@@ -507,6 +507,43 @@ void af_alg_complete(struct crypto_async_request *req, int err)
 }
 EXPORT_SYMBOL_GPL(af_alg_complete);
 
+/**
+ * af_alg_alloc_tsgl - allocate the TX SGL
+ *
+ * @sk socket of connection to user space
+ * @return: 0 upon success, < 0 upon error
+ */
+int af_alg_alloc_tsgl(struct sock *sk)
+{
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	struct af_alg_tsgl *sgl;
+	struct scatterlist *sg = NULL;
+
+	sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
+	if (!list_empty(&ctx->tsgl_list))
+		sg = sgl->sg;
+
+	if (!sg || sgl->cur >= MAX_SGL_ENTS) {
+		sgl = sock_kmalloc(sk, sizeof(*sgl) +
+				       sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
+				   GFP_KERNEL);
+		if (!sgl)
+			return -ENOMEM;
+
+		sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
+		sgl->cur = 0;
+
+		if (sg)
+			sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
+
+		list_add_tail(&sgl->list, &ctx->tsgl_list);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(af_alg_alloc_tsgl);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +2 -32
index cdcf186296bd..a722df95e55c 100644
@@ -64,36 +64,6 @@ static inline bool aead_sufficient_data(struct sock *sk)
 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
 }
 
-static int aead_alloc_tsgl(struct sock *sk)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	struct af_alg_tsgl *sgl;
-	struct scatterlist *sg = NULL;
-
-	sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
-	if (!list_empty(&ctx->tsgl_list))
-		sg = sgl->sg;
-
-	if (!sg || sgl->cur >= MAX_SGL_ENTS) {
-		sgl = sock_kmalloc(sk, sizeof(*sgl) +
-				       sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
-				   GFP_KERNEL);
-		if (!sgl)
-			return -ENOMEM;
-
-		sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
-		sgl->cur = 0;
-
-		if (sg)
-			sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
-
-		list_add_tail(&sgl->list, &ctx->tsgl_list);
-	}
-
-	return 0;
-}
-
 /**
  * Count number of SG entries from the beginning of the SGL to @bytes. If
  * an offset is provided, the counting of the SG entries starts at the offset.
@@ -422,7 +392,7 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		/* allocate a new page */
 		len = min_t(unsigned long, size, af_alg_sndbuf(sk));
 
-		err = aead_alloc_tsgl(sk);
+		err = af_alg_alloc_tsgl(sk);
 		if (err)
 			goto unlock;
 
@@ -501,7 +471,7 @@ static ssize_t aead_sendpage(struct socket *sock, struct page *page,
 			goto unlock;
 	}
 
-	err = aead_alloc_tsgl(sk);
+	err = af_alg_alloc_tsgl(sk);
 	if (err)
 		goto unlock;
 
crypto/algif_skcipher.c +2 -32
index 081df927fb8b..d511f665a190 100644
@@ -44,36 +44,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static int skcipher_alloc_tsgl(struct sock *sk)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	struct af_alg_tsgl *sgl;
-	struct scatterlist *sg = NULL;
-
-	sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
-	if (!list_empty(&ctx->tsgl_list))
-		sg = sgl->sg;
-
-	if (!sg || sgl->cur >= MAX_SGL_ENTS) {
-		sgl = sock_kmalloc(sk, sizeof(*sgl) +
-				       sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
-				   GFP_KERNEL);
-		if (!sgl)
-			return -ENOMEM;
-
-		sg_init_table(sgl->sg, MAX_SGL_ENTS + 1);
-		sgl->cur = 0;
-
-		if (sg)
-			sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg);
-
-		list_add_tail(&sgl->list, &ctx->tsgl_list);
-	}
-
-	return 0;
-}
-
 static unsigned int skcipher_count_tsgl(struct sock *sk, size_t bytes)
 {
 	struct alg_sock *ask = alg_sk(sk);
@@ -361,7 +331,7 @@ static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
 
 		len = min_t(unsigned long, len, af_alg_sndbuf(sk));
 
-		err = skcipher_alloc_tsgl(sk);
+		err = af_alg_alloc_tsgl(sk);
 		if (err)
 			goto unlock;
 
@@ -437,7 +407,7 @@ static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
 			goto unlock;
 	}
 
-	err = skcipher_alloc_tsgl(sk);
+	err = af_alg_alloc_tsgl(sk);
 	if (err)
 		goto unlock;
 
include/crypto/if_alg.h +2 -0
index e1ac57c32d85..cfebf65303c7 100644
@@ -240,4 +240,6 @@ static inline bool af_alg_readable(struct sock *sk)
 	return PAGE_SIZE <= af_alg_rcvbuf(sk);
 }
 
+int af_alg_alloc_tsgl(struct sock *sk);
+
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#7 lore raw

PATCH 7/16 crypto: AF_ALG - consolidate counting TX SG entries

In reply to Stephan Müller

Consoliate aead_pull_tsgl, skcipher_pull_tsgl ==> af_alg_pull_tsgl

---
 crypto/af_alg.c         | 80 +++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 79 ++----------------------------------------------
 crypto/algif_skcipher.c | 55 ++--------------------------------
 include/crypto/if_alg.h |  2 ++
 4 files changed, 87 insertions(+), 129 deletions(-)
crypto/af_alg.c +80 -0
index 861167fc12f7..73d4434df380 100644
@@ -596,6 +596,86 @@ unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
 }
 EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
 
+/**
+ * aead_pull_tsgl - Release the specified buffers from TX SGL
+ *
+ * If @dst is non-null, reassign the pages to dst. The caller must release
+ * the pages. If @dst_offset is given only reassign the pages to @dst starting
+ * at the @dst_offset (byte). The caller must ensure that @dst is large
+ * enough (e.g. by using af_alg_count_tsgl with the same offset).
+ *
+ * @sk socket of connection to user space
+ * @used Number of bytes to pull from TX SGL
+ * @dst If non-NULL, buffer is reassigned to dst SGL instead of releasing. The
+ *	caller must release the buffers in dst.
+ * @dst_offset Reassign the TX SGL from given offset. All buffers before
+ *	       reaching the offset is released.
+ */
+void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
+		      size_t dst_offset)
+{
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	struct af_alg_tsgl *sgl;
+	struct scatterlist *sg;
+	unsigned int i, j;
+
+	while (!list_empty(&ctx->tsgl_list)) {
+		sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
+				       list);
+		sg = sgl->sg;
+
+		for (i = 0, j = 0; i < sgl->cur; i++) {
+			size_t plen = min_t(size_t, used, sg[i].length);
+			struct page *page = sg_page(sg + i);
+
+			if (!page)
+				continue;
+
+			/*
+			 * Assumption: caller created af_alg_count_tsgl(len)
+			 * SG entries in dst.
+			 */
+			if (dst) {
+				if (dst_offset >= plen) {
+					/* discard page before offset */
+					dst_offset -= plen;
+					put_page(page);
+				} else {
+					/* reassign page to dst after offset */
+					sg_set_page(dst + j, page,
+						    plen - dst_offset,
+						    sg[i].offset + dst_offset);
+					dst_offset = 0;
+					j++;
+				}
+			}
+
+			sg[i].length -= plen;
+			sg[i].offset += plen;
+
+			used -= plen;
+			ctx->used -= plen;
+
+			if (sg[i].length)
+				return;
+
+			if (!dst)
+				put_page(page);
+
+			sg_assign_page(sg + i, NULL);
+		}
+
+		list_del(&sgl->list);
+		sock_kfree_s(sk, sgl, sizeof(*sgl) + sizeof(sgl->sg[0]) *
+						     (MAX_SGL_ENTS + 1));
+	}
+
+	if (!ctx->used)
+		ctx->merge = 0;
+}
+EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +3 -76

103 lines not on this page · raw message

crypto/algif_skcipher.c +2 -53

73 lines not on this page · raw message

include/crypto/if_alg.h +2 -0
index 3029cf4f2890..534aa3810c6b 100644
@@ -242,5 +242,7 @@ static inline bool af_alg_readable(struct sock *sk)
 
 int af_alg_alloc_tsgl(struct sock *sk);
 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
+void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
+		      size_t dst_offset);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#8 lore raw

PATCH 8/16 crypto: AF_ALG - consolidate freeing TX/RX SGLs

In reply to Stephan Müller

Consoliate aead_free_areq_sgls, skcipher_free_areq_sgls ==> af_alg_free_areq_sgls

---
 crypto/af_alg.c         | 35 +++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 33 ++-------------------------------
 crypto/algif_skcipher.c | 33 ++-------------------------------
 include/crypto/if_alg.h |  1 +
 4 files changed, 40 insertions(+), 62 deletions(-)
crypto/af_alg.c +35 -0
index 73d4434df380..07c0e965c336 100644
@@ -676,6 +676,41 @@ void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
 }
 EXPORT_SYMBOL_GPL(af_alg_pull_tsgl);
 
+/**
+ * af_alg_free_areq_sgls - Release TX and RX SGLs of the request
+ *
+ * @areq Request holding the TX and RX SGL
+ */
+void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
+{
+	struct sock *sk = areq->sk;
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	struct af_alg_rsgl *rsgl, *tmp;
+	struct scatterlist *tsgl;
+	struct scatterlist *sg;
+	unsigned int i;
+
+	list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
+		ctx->rcvused -= rsgl->sg_num_bytes;
+		af_alg_free_sg(&rsgl->sgl);
+		list_del(&rsgl->list);
+		if (rsgl != &areq->first_rsgl)
+			sock_kfree_s(sk, rsgl, sizeof(*rsgl));
+	}
+
+	tsgl = areq->tsgl;
+	for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
+		if (!sg_page(sg))
+			continue;
+		put_page(sg_page(sg));
+	}
+
+	if (areq->tsgl && areq->tsgl_entries)
+		sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
+}
+EXPORT_SYMBOL_GPL(af_alg_free_areq_sgls);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +2 -31
index b78acb3336d6..5ccac7f0047e 100644
@@ -64,35 +64,6 @@ static inline bool aead_sufficient_data(struct sock *sk)
 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
 }
 
-static void aead_free_areq_sgls(struct af_alg_async_req *areq)
-{
-	struct sock *sk = areq->sk;
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	struct af_alg_rsgl *rsgl, *tmp;
-	struct scatterlist *tsgl;
-	struct scatterlist *sg;
-	unsigned int i;
-
-	list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
-		ctx->rcvused -= rsgl->sg_num_bytes;
-		af_alg_free_sg(&rsgl->sgl);
-		list_del(&rsgl->list);
-		if (rsgl != &areq->first_rsgl)
-			sock_kfree_s(sk, rsgl, sizeof(*rsgl));
-	}
-
-	tsgl = areq->tsgl;
-	for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
-		if (!sg_page(sg))
-			continue;
-		put_page(sg_page(sg));
-	}
-
-	if (areq->tsgl && areq->tsgl_entries)
-		sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
-}
-
 static int aead_wait_for_wmem(struct sock *sk, unsigned int flags)
 {
 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
@@ -393,7 +364,7 @@ static void aead_async_cb(struct crypto_async_request *_req, int err)
 	/* Buffer size written by crypto operation. */
 	resultlen = areq->outlen;
 
-	aead_free_areq_sgls(areq);
+	af_alg_free_areq_sgls(areq);
 	sock_kfree_s(sk, areq, areq->areqlen);
 	__sock_put(sk);
 
@@ -671,7 +642,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 	}
 
 free:
-	aead_free_areq_sgls(areq);
+	af_alg_free_areq_sgls(areq);
 	if (areq)
 		sock_kfree_s(sk, areq, areqlen);
 
crypto/algif_skcipher.c +2 -31
index bc7bbd16f2eb..ea7cfe7c1971 100644
@@ -44,35 +44,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static void skcipher_free_areq_sgls(struct af_alg_async_req *areq)
-{
-	struct sock *sk = areq->sk;
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	struct af_alg_rsgl *rsgl, *tmp;
-	struct scatterlist *tsgl;
-	struct scatterlist *sg;
-	unsigned int i;
-
-	list_for_each_entry_safe(rsgl, tmp, &areq->rsgl_list, list) {
-		ctx->rcvused -= rsgl->sg_num_bytes;
-		af_alg_free_sg(&rsgl->sgl);
-		list_del(&rsgl->list);
-		if (rsgl != &areq->first_rsgl)
-			sock_kfree_s(sk, rsgl, sizeof(*rsgl));
-	}
-
-	tsgl = areq->tsgl;
-	for_each_sg(tsgl, sg, areq->tsgl_entries, i) {
-		if (!sg_page(sg))
-			continue;
-		put_page(sg_page(sg));
-	}
-
-	if (areq->tsgl && areq->tsgl_entries)
-		sock_kfree_s(sk, tsgl, areq->tsgl_entries * sizeof(*tsgl));
-}
-
 static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
 {
 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
@@ -368,7 +339,7 @@ static void skcipher_async_cb(struct crypto_async_request *req, int err)
 	/* Buffer size written by crypto operation. */
 	resultlen = areq->cra_u.skcipher_req.cryptlen;
 
-	skcipher_free_areq_sgls(areq);
+	af_alg_free_areq_sgls(areq);
 	sock_kfree_s(sk, areq, areq->areqlen);
 	__sock_put(sk);
 
@@ -512,7 +483,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 	}
 
 free:
-	skcipher_free_areq_sgls(areq);
+	af_alg_free_areq_sgls(areq);
 	if (areq)
 		sock_kfree_s(sk, areq, areqlen);
 
include/crypto/if_alg.h +1 -0
index 534aa3810c6b..93379804a0ee 100644
@@ -244,5 +244,6 @@ int af_alg_alloc_tsgl(struct sock *sk);
 unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
 		      size_t dst_offset);
+void af_alg_free_areq_sgls(struct af_alg_async_req *areq);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#9 lore raw

PATCH 9/16 crypto: AF_ALG - consolidate waiting for wmem

In reply to Stephan Müller

Consoliate aead_wait_for_wmem, skcipher_wait_for_wmem ==> af_alg_wait_for_wmem

---
 crypto/af_alg.c         | 35 +++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 30 ++----------------------------
 crypto/algif_skcipher.c | 30 ++----------------------------
 include/crypto/if_alg.h |  1 +
 4 files changed, 40 insertions(+), 56 deletions(-)
crypto/af_alg.c +35 -0
index 07c0e965c336..518c9de4bb6e 100644
@@ -21,6 +21,7 @@
 #include <linux/module.h>
 #include <linux/net.h>
 #include <linux/rwsem.h>
+#include <linux/sched/signal.h>
 #include <linux/security.h>
 
 struct alg_type_list {
@@ -711,6 +712,40 @@ void af_alg_free_areq_sgls(struct af_alg_async_req *areq)
 }
 EXPORT_SYMBOL_GPL(af_alg_free_areq_sgls);
 
+/**
+ * af_alg_wait_for_wmem - wait for availability of writable memory
+ *
+ * @sk socket of connection to user space
+ * @flags If MSG_DONTWAIT is set, then only report if function would sleep
+ * @return 0 when writable memory is available, < 0 upon error
+ */
+int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
+{
+	DEFINE_WAIT_FUNC(wait, woken_wake_function);
+	int err = -ERESTARTSYS;
+	long timeout;
+
+	if (flags & MSG_DONTWAIT)
+		return -EAGAIN;
+
+	sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
+
+	add_wait_queue(sk_sleep(sk), &wait);
+	for (;;) {
+		if (signal_pending(current))
+			break;
+		timeout = MAX_SCHEDULE_TIMEOUT;
+		if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
+			err = 0;
+			break;
+		}
+	}
+	remove_wait_queue(sk_sleep(sk), &wait);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(af_alg_wait_for_wmem);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +2 -28
index 5ccac7f0047e..5474fec42fe3 100644
@@ -64,32 +64,6 @@ static inline bool aead_sufficient_data(struct sock *sk)
 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
 }
 
-static int aead_wait_for_wmem(struct sock *sk, unsigned int flags)
-{
-	DEFINE_WAIT_FUNC(wait, woken_wake_function);
-	int err = -ERESTARTSYS;
-	long timeout;
-
-	if (flags & MSG_DONTWAIT)
-		return -EAGAIN;
-
-	sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
-
-	add_wait_queue(sk_sleep(sk), &wait);
-	for (;;) {
-		if (signal_pending(current))
-			break;
-		timeout = MAX_SCHEDULE_TIMEOUT;
-		if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
-			err = 0;
-			break;
-		}
-	}
-	remove_wait_queue(sk_sleep(sk), &wait);
-
-	return err;
-}
-
 static void aead_wmem_wakeup(struct sock *sk)
 {
 	struct socket_wq *wq;
@@ -237,7 +211,7 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		}
 
 		if (!af_alg_writable(sk)) {
-			err = aead_wait_for_wmem(sk, msg->msg_flags);
+			err = af_alg_wait_for_wmem(sk, msg->msg_flags);
 			if (err)
 				goto unlock;
 		}
@@ -319,7 +293,7 @@ static ssize_t aead_sendpage(struct socket *sock, struct page *page,
 		goto done;
 
 	if (!af_alg_writable(sk)) {
-		err = aead_wait_for_wmem(sk, flags);
+		err = af_alg_wait_for_wmem(sk, flags);
 		if (err)
 			goto unlock;
 	}
crypto/algif_skcipher.c +2 -28
index ea7cfe7c1971..10d3c6f477ac 100644
@@ -44,32 +44,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
-{
-	DEFINE_WAIT_FUNC(wait, woken_wake_function);
-	int err = -ERESTARTSYS;
-	long timeout;
-
-	if (flags & MSG_DONTWAIT)
-		return -EAGAIN;
-
-	sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
-
-	add_wait_queue(sk_sleep(sk), &wait);
-	for (;;) {
-		if (signal_pending(current))
-			break;
-		timeout = MAX_SCHEDULE_TIMEOUT;
-		if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
-			err = 0;
-			break;
-		}
-	}
-	remove_wait_queue(sk_sleep(sk), &wait);
-
-	return err;
-}
-
 static void skcipher_wmem_wakeup(struct sock *sk)
 {
 	struct socket_wq *wq;
@@ -218,7 +192,7 @@ static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
 		}
 
 		if (!af_alg_writable(sk)) {
-			err = skcipher_wait_for_wmem(sk, msg->msg_flags);
+			err = af_alg_wait_for_wmem(sk, msg->msg_flags);
 			if (err)
 				goto unlock;
 		}
@@ -296,7 +270,7 @@ static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
 		goto done;
 
 	if (!af_alg_writable(sk)) {
-		err = skcipher_wait_for_wmem(sk, flags);
+		err = af_alg_wait_for_wmem(sk, flags);
 		if (err)
 			goto unlock;
 	}
include/crypto/if_alg.h +1 -0
index 93379804a0ee..1008fab90eab 100644
@@ -245,5 +245,6 @@ unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
 void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
 		      size_t dst_offset);
 void af_alg_free_areq_sgls(struct af_alg_async_req *areq);
+int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#10 lore raw

PATCH 10/16 crypto: AF_ALG - consolidate waking up on writable memory

In reply to Stephan Müller

Consoliate aead_wmem_wakeup, skcipher_wmem_wakeup

    ==> af_alg_wmem_wakeup
---
 crypto/af_alg.c         | 23 +++++++++++++++++++++++
 crypto/algif_aead.c     | 19 +------------------
 crypto/algif_skcipher.c | 19 +------------------
 include/crypto/if_alg.h |  1 +
 4 files changed, 26 insertions(+), 36 deletions(-)
crypto/af_alg.c +23 -0
index 518c9de4bb6e..6645454bc445 100644
@@ -746,6 +746,29 @@ int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags)
 }
 EXPORT_SYMBOL_GPL(af_alg_wait_for_wmem);
 
+/**
+ * af_alg_wmem_wakeup - wakeup caller when writable memory is available
+ *
+ * @sk socket of connection to user space
+ */
+void af_alg_wmem_wakeup(struct sock *sk)
+{
+	struct socket_wq *wq;
+
+	if (!af_alg_writable(sk))
+		return;
+
+	rcu_read_lock();
+	wq = rcu_dereference(sk->sk_wq);
+	if (skwq_has_sleeper(wq))
+		wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
+							   POLLRDNORM |
+							   POLLRDBAND);
+	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
+	rcu_read_unlock();
+}
+EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +1 -18
index 5474fec42fe3..8c7fac1053f0 100644
@@ -64,23 +64,6 @@ static inline bool aead_sufficient_data(struct sock *sk)
 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
 }
 
-static void aead_wmem_wakeup(struct sock *sk)
-{
-	struct socket_wq *wq;
-
-	if (!af_alg_writable(sk))
-		return;
-
-	rcu_read_lock();
-	wq = rcu_dereference(sk->sk_wq);
-	if (skwq_has_sleeper(wq))
-		wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
-							   POLLRDNORM |
-							   POLLRDBAND);
-	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
-	rcu_read_unlock();
-}
-
 static int aead_wait_for_data(struct sock *sk, unsigned flags)
 {
 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
@@ -651,7 +634,7 @@ static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
 	}
 
 out:
-	aead_wmem_wakeup(sk);
+	af_alg_wmem_wakeup(sk);
 	release_sock(sk);
 	return ret;
 }
crypto/algif_skcipher.c +1 -18
index 10d3c6f477ac..258a27d0f553 100644
@@ -44,23 +44,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static void skcipher_wmem_wakeup(struct sock *sk)
-{
-	struct socket_wq *wq;
-
-	if (!af_alg_writable(sk))
-		return;
-
-	rcu_read_lock();
-	wq = rcu_dereference(sk->sk_wq);
-	if (skwq_has_sleeper(wq))
-		wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
-							   POLLRDNORM |
-							   POLLRDBAND);
-	sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
-	rcu_read_unlock();
-}
-
 static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
 {
 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
@@ -492,7 +475,7 @@ static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 	}
 
 out:
-	skcipher_wmem_wakeup(sk);
+	af_alg_wmem_wakeup(sk);
 	release_sock(sk);
 	return ret;
 }
include/crypto/if_alg.h +1 -0
index 1008fab90eab..88eda219b90f 100644
@@ -246,5 +246,6 @@ void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
 		      size_t dst_offset);
 void af_alg_free_areq_sgls(struct af_alg_async_req *areq);
 int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags);
+void af_alg_wmem_wakeup(struct sock *sk);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#11 lore raw

PATCH 11/16 crypto: AF_ALG - consolidate waiting for TX data

In reply to Stephan Müller

Consoliate aead_wait_for_data, skcipher_wait_for_data ==> af_alg_wait_for_data

The wakeup is triggered when either more data is received or the indicator that more data is to be expected is released. The first is triggered by user space, the second is triggered by the kernel upon finishing the processing of data (i.e. the kernel is ready for more).

---
 crypto/af_alg.c         | 39 +++++++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 32 +-------------------------------
 crypto/algif_skcipher.c | 33 +--------------------------------
 include/crypto/if_alg.h |  1 +
 4 files changed, 42 insertions(+), 63 deletions(-)
crypto/af_alg.c +39 -0
index 6645454bc445..c1fe7c5f1b2e 100644
@@ -769,6 +769,45 @@ void af_alg_wmem_wakeup(struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(af_alg_wmem_wakeup);
 
+/**
+ * af_alg_wait_for_data - wait for availability of TX data
+ *
+ * @sk socket of connection to user space
+ * @flags If MSG_DONTWAIT is set, then only report if function would sleep
+ * @return 0 when writable memory is available, < 0 upon error
+ */
+int af_alg_wait_for_data(struct sock *sk, unsigned flags)
+{
+	DEFINE_WAIT_FUNC(wait, woken_wake_function);
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	long timeout;
+	int err = -ERESTARTSYS;
+
+	if (flags & MSG_DONTWAIT)
+		return -EAGAIN;
+
+	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
+
+	add_wait_queue(sk_sleep(sk), &wait);
+	for (;;) {
+		if (signal_pending(current))
+			break;
+		timeout = MAX_SCHEDULE_TIMEOUT;
+		if (sk_wait_event(sk, &timeout, (ctx->used || !ctx->more),
+				  &wait)) {
+			err = 0;
+			break;
+		}
+	}
+	remove_wait_queue(sk_sleep(sk), &wait);
+
+	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(af_alg_wait_for_data);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +1 -31
index 8c7fac1053f0..8db8c10401d6 100644
@@ -64,36 +64,6 @@ static inline bool aead_sufficient_data(struct sock *sk)
 	return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
 }
 
-static int aead_wait_for_data(struct sock *sk, unsigned flags)
-{
-	DEFINE_WAIT_FUNC(wait, woken_wake_function);
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	long timeout;
-	int err = -ERESTARTSYS;
-
-	if (flags & MSG_DONTWAIT)
-		return -EAGAIN;
-
-	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
-
-	add_wait_queue(sk_sleep(sk), &wait);
-	for (;;) {
-		if (signal_pending(current))
-			break;
-		timeout = MAX_SCHEDULE_TIMEOUT;
-		if (sk_wait_event(sk, &timeout, !ctx->more, &wait)) {
-			err = 0;
-			break;
-		}
-	}
-	remove_wait_queue(sk_sleep(sk), &wait);
-
-	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
-
-	return err;
-}
-
 static void aead_data_wakeup(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
@@ -425,7 +395,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 			break;
 
 		if (!ctx->used) {
-			err = aead_wait_for_data(sk, flags);
+			err = af_alg_wait_for_data(sk, flags);
 			if (err)
 				goto free;
 		}
crypto/algif_skcipher.c +1 -32
index 258a27d0f553..572a5a632ea1 100644
@@ -44,37 +44,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static int skcipher_wait_for_data(struct sock *sk, unsigned flags)
-{
-	DEFINE_WAIT_FUNC(wait, woken_wake_function);
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	long timeout;
-	int err = -ERESTARTSYS;
-
-	if (flags & MSG_DONTWAIT) {
-		return -EAGAIN;
-	}
-
-	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
-
-	add_wait_queue(sk_sleep(sk), &wait);
-	for (;;) {
-		if (signal_pending(current))
-			break;
-		timeout = MAX_SCHEDULE_TIMEOUT;
-		if (sk_wait_event(sk, &timeout, ctx->used, &wait)) {
-			err = 0;
-			break;
-		}
-	}
-	remove_wait_queue(sk_sleep(sk), &wait);
-
-	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
-
-	return err;
-}
-
 static void skcipher_data_wakeup(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
@@ -343,7 +312,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 			break;
 
 		if (!ctx->used) {
-			err = skcipher_wait_for_data(sk, flags);
+			err = af_alg_wait_for_data(sk, flags);
 			if (err)
 				goto free;
 		}
include/crypto/if_alg.h +1 -0
index 88eda219b90f..c3b02276f38f 100644
@@ -247,5 +247,6 @@ void af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
 void af_alg_free_areq_sgls(struct af_alg_async_req *areq);
 int af_alg_wait_for_wmem(struct sock *sk, unsigned int flags);
 void af_alg_wmem_wakeup(struct sock *sk);
+int af_alg_wait_for_data(struct sock *sk, unsigned flags);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#2 lore raw

PATCH 2/16 crypto: AF_ALG - consolidation of context data structure

In reply to Stephan Müller

Consolidate skcipher_ctx, aead_ctx ==> af_alg_ctx

---
 crypto/algif_aead.c     | 48 +++++++++++++++---------------------------------
 crypto/algif_skcipher.c | 45 ++++++++++++++-------------------------------
 include/crypto/if_alg.h | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+), 64 deletions(-)
crypto/algif_aead.c +15 -33
index 42f69a4f87d5..adbf87a0eeee 100644
@@ -47,28 +47,10 @@ struct aead_tfm {
 	struct crypto_skcipher *null_tfm;
 };
 
-struct aead_ctx {
-	struct list_head tsgl_list;	/* Link to TX SGL */
-
-	void *iv;
-	size_t aead_assoclen;
-
-	struct af_alg_completion completion;	/* sync work queue */
-
-	size_t used;		/* TX bytes sent to kernel */
-	size_t rcvused;		/* total RX bytes to be processed by kernel */
-
-	bool more;		/* More data to be expected? */
-	bool merge;		/* Merge new data into existing SG */
-	bool enc;		/* Crypto operation: enc, dec */
-
-	unsigned int len;	/* Length of allocated memory for this struct */
-};
-
 static inline int aead_sndbuf(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 
 	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
 			  ctx->used, 0);
@@ -82,7 +64,7 @@ static inline bool aead_writable(struct sock *sk)
 static inline int aead_rcvbuf(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 
 	return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
 			  ctx->rcvused, 0);
@@ -98,7 +80,7 @@ static inline bool aead_sufficient_data(struct sock *sk)
 	struct alg_sock *ask = alg_sk(sk);
 	struct sock *psk = ask->parent;
 	struct alg_sock *pask = alg_sk(psk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct aead_tfm *aeadc = pask->private;
 	struct crypto_aead *tfm = aeadc->aead;
 	unsigned int as = crypto_aead_authsize(tfm);
@@ -113,7 +95,7 @@ static inline bool aead_sufficient_data(struct sock *sk)
 static int aead_alloc_tsgl(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct af_alg_tsgl *sgl;
 	struct scatterlist *sg = NULL;
 
@@ -148,7 +130,7 @@ static unsigned int aead_count_tsgl(struct sock *sk, size_t bytes,
 				    size_t offset)
 {
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct af_alg_tsgl *sgl, *tmp;
 	unsigned int i;
 	unsigned int sgl_count = 0;
@@ -198,7 +180,7 @@ static void aead_pull_tsgl(struct sock *sk, size_t used,
 			   struct scatterlist *dst, size_t dst_offset)
 {
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct af_alg_tsgl *sgl;
 	struct scatterlist *sg;
 	unsigned int i, j;
@@ -262,7 +244,7 @@ static void aead_free_areq_sgls(struct af_alg_async_req *areq)
 {
 	struct sock *sk = areq->sk;
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct af_alg_rsgl *rsgl, *tmp;
 	struct scatterlist *tsgl;
 	struct scatterlist *sg;
@@ -334,7 +316,7 @@ static int aead_wait_for_data(struct sock *sk, unsigned flags)
 {
 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	long timeout;
 	int err = -ERESTARTSYS;
 
@@ -363,7 +345,7 @@ static int aead_wait_for_data(struct sock *sk, unsigned flags)
 static void aead_data_wakeup(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct socket_wq *wq;
 
 	if (!ctx->used)
@@ -385,7 +367,7 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	struct alg_sock *ask = alg_sk(sk);
 	struct sock *psk = ask->parent;
 	struct alg_sock *pask = alg_sk(psk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct aead_tfm *aeadc = pask->private;
 	struct crypto_aead *tfm = aeadc->aead;
 	unsigned int ivsize = crypto_aead_ivsize(tfm);
@@ -527,7 +509,7 @@ static ssize_t aead_sendpage(struct socket *sock, struct page *page,
 {
 	struct sock *sk = sock->sk;
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct af_alg_tsgl *sgl;
 	int err = -EINVAL;
 
@@ -617,7 +599,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 	struct alg_sock *ask = alg_sk(sk);
 	struct sock *psk = ask->parent;
 	struct alg_sock *pask = alg_sk(psk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct aead_tfm *aeadc = pask->private;
 	struct crypto_aead *tfm = aeadc->aead;
 	struct crypto_skcipher *null_tfm = aeadc->null_tfm;
@@ -910,7 +892,7 @@ static unsigned int aead_poll(struct file *file, struct socket *sock,
 {
 	struct sock *sk = sock->sk;
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	unsigned int mask;
 
 	sock_poll_wait(file, sk_sleep(sk), wait);
@@ -1101,7 +1083,7 @@ static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
 static void aead_sock_destruct(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
-	struct aead_ctx *ctx = ask->private;
+	struct af_alg_ctx *ctx = ask->private;
 	struct sock *psk = ask->parent;
 	struct alg_sock *pask = alg_sk(psk);
 	struct aead_tfm *aeadc = pask->private;
@@ -1117,7 +1099,7 @@ static void aead_sock_destruct(struct sock *sk)
 
 static int aead_accept_parent_nokey(void *private, struct sock *sk)
 {
-	struct aead_ctx *ctx;
+	struct af_alg_ctx *ctx;
 	struct alg_sock *ask = alg_sk(sk);
 	struct aead_tfm *tfm = private;
 	struct crypto_aead *aead = tfm->aead;
crypto/algif_skcipher.c +14 -31

132 lines not on this page · raw message

include/crypto/if_alg.h +41 -0

47 lines not on this page · raw message

Signature · 1 line
2.13.3
#3 lore raw

PATCH 3/16 crypto: AF_ALG - consolidate send buffer service functions

In reply to Stephan Müller

Consolidate the common functions verifying the send buffers:

* skcipher_sndbuf, aead_sndbuf ==> af_alg_sndbuf

* skcipher_writable, aead_writable ==> af_alg_writable

---
 crypto/algif_aead.c     | 26 ++++++--------------------
 crypto/algif_skcipher.c | 26 ++++++--------------------
 include/crypto/if_alg.h | 26 ++++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 40 deletions(-)
crypto/algif_aead.c +6 -20
index adbf87a0eeee..c923ce29bfe3 100644
@@ -47,20 +47,6 @@ struct aead_tfm {
 	struct crypto_skcipher *null_tfm;
 };
 
-static inline int aead_sndbuf(struct sock *sk)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-
-	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
-			  ctx->used, 0);
-}
-
-static inline bool aead_writable(struct sock *sk)
-{
-	return PAGE_SIZE <= aead_sndbuf(sk);
-}
-
 static inline int aead_rcvbuf(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
@@ -285,7 +271,7 @@ static int aead_wait_for_wmem(struct sock *sk, unsigned int flags)
 		if (signal_pending(current))
 			break;
 		timeout = MAX_SCHEDULE_TIMEOUT;
-		if (sk_wait_event(sk, &timeout, aead_writable(sk), &wait)) {
+		if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
 			err = 0;
 			break;
 		}
@@ -299,7 +285,7 @@ static void aead_wmem_wakeup(struct sock *sk)
 {
 	struct socket_wq *wq;
 
-	if (!aead_writable(sk))
+	if (!af_alg_writable(sk))
 		return;
 
 	rcu_read_lock();
@@ -441,14 +427,14 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 			continue;
 		}
 
-		if (!aead_writable(sk)) {
+		if (!af_alg_writable(sk)) {
 			err = aead_wait_for_wmem(sk, msg->msg_flags);
 			if (err)
 				goto unlock;
 		}
 
 		/* allocate a new page */
-		len = min_t(unsigned long, size, aead_sndbuf(sk));
+		len = min_t(unsigned long, size, af_alg_sndbuf(sk));
 
 		err = aead_alloc_tsgl(sk);
 		if (err)
@@ -523,7 +509,7 @@ static ssize_t aead_sendpage(struct socket *sock, struct page *page,
 	if (!size)
 		goto done;
 
-	if (!aead_writable(sk)) {
+	if (!af_alg_writable(sk)) {
 		err = aead_wait_for_wmem(sk, flags);
 		if (err)
 			goto unlock;
@@ -901,7 +887,7 @@ static unsigned int aead_poll(struct file *file, struct socket *sock,
 	if (!ctx->more)
 		mask |= POLLIN | POLLRDNORM;
 
-	if (aead_writable(sk))
+	if (af_alg_writable(sk))
 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
 
 	return mask;
crypto/algif_skcipher.c +6 -20
index 0256959b0925..a5c6643f2abe 100644
@@ -44,20 +44,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static inline int skcipher_sndbuf(struct sock *sk)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-
-	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
-			  ctx->used, 0);
-}
-
-static inline bool skcipher_writable(struct sock *sk)
-{
-	return PAGE_SIZE <= skcipher_sndbuf(sk);
-}
-
 static inline int skcipher_rcvbuf(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
@@ -224,7 +210,7 @@ static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags)
 		if (signal_pending(current))
 			break;
 		timeout = MAX_SCHEDULE_TIMEOUT;
-		if (sk_wait_event(sk, &timeout, skcipher_writable(sk), &wait)) {
+		if (sk_wait_event(sk, &timeout, af_alg_writable(sk), &wait)) {
 			err = 0;
 			break;
 		}
@@ -238,7 +224,7 @@ static void skcipher_wmem_wakeup(struct sock *sk)
 {
 	struct socket_wq *wq;
 
-	if (!skcipher_writable(sk))
+	if (!af_alg_writable(sk))
 		return;
 
 	rcu_read_lock();
@@ -381,13 +367,13 @@ static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
 			continue;
 		}
 
-		if (!skcipher_writable(sk)) {
+		if (!af_alg_writable(sk)) {
 			err = skcipher_wait_for_wmem(sk, msg->msg_flags);
 			if (err)
 				goto unlock;
 		}
 
-		len = min_t(unsigned long, len, skcipher_sndbuf(sk));
+		len = min_t(unsigned long, len, af_alg_sndbuf(sk));
 
 		err = skcipher_alloc_tsgl(sk);
 		if (err)
@@ -459,7 +445,7 @@ static ssize_t skcipher_sendpage(struct socket *sock, struct page *page,
 	if (!size)
 		goto done;
 
-	if (!skcipher_writable(sk)) {
+	if (!af_alg_writable(sk)) {
 		err = skcipher_wait_for_wmem(sk, flags);
 		if (err)
 			goto unlock;
@@ -701,7 +687,7 @@ static unsigned int skcipher_poll(struct file *file, struct socket *sock,
 	if (ctx->used)
 		mask |= POLLIN | POLLRDNORM;
 
-	if (skcipher_writable(sk))
+	if (af_alg_writable(sk))
 		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
 
 	return mask;
include/crypto/if_alg.h +26 -0
index b3ab34ff9d35..79d215f65acf 100644
@@ -188,4 +188,30 @@ static inline void af_alg_init_completion(struct af_alg_completion *completion)
 	init_completion(&completion->completion);
 }
 
+/**
+ * Size of available buffer for sending data from user space to kernel.
+ *
+ * @sk socket of connection to user space
+ * @return number of bytes still available
+ */
+static inline int af_alg_sndbuf(struct sock *sk)
+{
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+
+	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
+			  ctx->used, 0);
+}
+
+/**
+ * Can the send buffer still be written to?
+ *
+ * @sk socket of connection to user space
+ * @return true => writable, false => not writable
+ */
+static inline bool af_alg_writable(struct sock *sk)
+{
+	return PAGE_SIZE <= af_alg_sndbuf(sk);
+}
+
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#6 lore raw

PATCH 6/16 crypto: AF_ALG - consolidate counting TX SG entries

In reply to Stephan Müller

Consoliate aead_count_tsgl, skcipher_count_tsgl ==> af_alg_count_tsgl

---
 crypto/af_alg.c         | 52 ++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 53 ++++---------------------------------------------
 crypto/algif_skcipher.c | 30 ++--------------------------
 include/crypto/if_alg.h |  1 +
 4 files changed, 59 insertions(+), 77 deletions(-)
crypto/af_alg.c +52 -0
index 87138c4b5a0f..861167fc12f7 100644
@@ -544,6 +544,58 @@ int af_alg_alloc_tsgl(struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(af_alg_alloc_tsgl);
 
+/**
+ * aead_count_tsgl - Count number of TX SG entries
+ *
+ * The counting starts from the beginning of the SGL to @bytes. If
+ * an offset is provided, the counting of the SG entries starts at the offset.
+ *
+ * @sk socket of connection to user space
+ * @bytes Count the number of SG entries holding given number of bytes.
+ * @offset Start the counting of SG entries from the given offset.
+ * @return Number of TX SG entries found given the constraints
+ */
+unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset)
+{
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	struct af_alg_tsgl *sgl, *tmp;
+	unsigned int i;
+	unsigned int sgl_count = 0;
+
+	if (!bytes)
+		return 0;
+
+	list_for_each_entry_safe(sgl, tmp, &ctx->tsgl_list, list) {
+		struct scatterlist *sg = sgl->sg;
+
+		for (i = 0; i < sgl->cur; i++) {
+			size_t bytes_count;
+
+			/* Skip offset */
+			if (offset >= sg[i].length) {
+				offset -= sg[i].length;
+				bytes -= sg[i].length;
+				continue;
+			}
+
+			bytes_count = sg[i].length - offset;
+
+			offset = 0;
+			sgl_count++;
+
+			/* If we have seen requested number of bytes, stop */
+			if (bytes_count >= bytes)
+				return sgl_count;
+
+			bytes -= bytes_count;
+		}
+	}
+
+	return sgl_count;
+}
+EXPORT_SYMBOL_GPL(af_alg_count_tsgl);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +4 -49

77 lines not on this page · raw message

crypto/algif_skcipher.c +2 -28
index d511f665a190..8e086d9f7b71 100644
@@ -44,32 +44,6 @@ struct skcipher_tfm {
 	bool has_key;
 };
 
-static unsigned int skcipher_count_tsgl(struct sock *sk, size_t bytes)
-{
-	struct alg_sock *ask = alg_sk(sk);
-	struct af_alg_ctx *ctx = ask->private;
-	struct af_alg_tsgl *sgl, *tmp;
-	unsigned int i;
-	unsigned int sgl_count = 0;
-
-	if (!bytes)
-		return 0;
-
-	list_for_each_entry_safe(sgl, tmp, &ctx->tsgl_list, list) {
-		struct scatterlist *sg = sgl->sg;
-
-		for (i = 0; i < sgl->cur; i++) {
-			sgl_count++;
-			if (sg[i].length >= bytes)
-				return sgl_count;
-
-			bytes -= sg[i].length;
-		}
-	}
-
-	return sgl_count;
-}
-
 static void skcipher_pull_tsgl(struct sock *sk, size_t used,
 			       struct scatterlist *dst)
 {
@@ -92,7 +66,7 @@ static void skcipher_pull_tsgl(struct sock *sk, size_t used,
 				continue;
 
 			/*
-			 * Assumption: caller created skcipher_count_tsgl(len)
+			 * Assumption: caller created af_alg_count_tsgl(len)
 			 * SG entries in dst.
 			 */
 			if (dst)
@@ -543,7 +517,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
 	 * Create a per request TX SGL for this request which tracks the
 	 * SG entries from the global TX SGL.
 	 */
-	areq->tsgl_entries = skcipher_count_tsgl(sk, len);
+	areq->tsgl_entries = af_alg_count_tsgl(sk, len, 0);
 	if (!areq->tsgl_entries)
 		areq->tsgl_entries = 1;
 	areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * areq->tsgl_entries,
include/crypto/if_alg.h +1 -0
index cfebf65303c7..3029cf4f2890 100644
@@ -241,5 +241,6 @@ static inline bool af_alg_readable(struct sock *sk)
 }
 
 int af_alg_alloc_tsgl(struct sock *sk);
+unsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3
#0 lore raw

cover crypto: AF_ALG - consolidation

Hi,

with the update of the algif_aead and algif_skcipher memory management, a lot of code duplication has been introduced deliberately.

This patch set cleans up the low-hanging fruits. The cleanup of the recvmsg RX SGL copy will come separately as this is not a simple copy and paste operation.

Each patch was tested individually with libkcapi's test suite.

The patch set goes on top of patches "crypto: AF_ALG - return error code when no data was processed" and "crypto: algif_aead - copy AAD from src to dst".

Stephan Mueller (16):
crypto: AF_ALG - consolidation of common data structures
crypto: AF_ALG - consolidation of context data structure
crypto: AF_ALG - consolidate send buffer service functions
crypto: AF_ALG - consolidate RX buffer service functions
crypto: AF_ALG - consolidate TX SGL allocation
crypto: AF_ALG - consolidate counting TX SG entries
crypto: AF_ALG - consolidate counting TX SG entries
crypto: AF_ALG - consolidate freeing TX/RX SGLs
crypto: AF_ALG - consolidate waiting for wmem
crypto: AF_ALG - consolidate waking up on writable memory
crypto: AF_ALG - consolidate waiting for TX data
crypto: AF_ALG - consolidate waking up caller for TX data
crypto: AF_ALG - consolidate sendmsg implementation
crypto: AF_ALG - consolidate sendpage implementation
crypto: AF_ALG - consolidate AIO callback handler
crypto: AF_ALG - consolidate poll syscall handler

 crypto/af_alg.c         | 597 ++++++++++++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 641 +++---------------------------------------------
 crypto/algif_skcipher.c | 585 +++----------------------------------------
 include/crypto/if_alg.h | 163 ++++++++++++
 4 files changed, 830 insertions(+), 1156 deletions(-)
Signature · 1 line
2.13.3
#1 lore raw

PATCH 1/16 crypto: AF_ALG - consolidation of common data structures

Consolidate following data structures:

- skcipher_async_req, aead_async_req -> af_alg_async_req

- skcipher_rsgl, aead_rsql -> af_alg_rsgl

- skcipher_tsgl, aead_tsql -> af_alg_tsgl

---
 crypto/algif_aead.c     | 89 ++++++++++++++++---------------------------------
 crypto/algif_skcipher.c | 88 +++++++++++++++++-------------------------------
 include/crypto/if_alg.h | 53 +++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+), 118 deletions(-)
crypto/algif_aead.c +29 -60
index 1f0696dd64f4..42f69a4f87d5 100644
@@ -41,34 +41,6 @@
 #include <linux/net.h>
 #include <net/sock.h>
 
-struct aead_tsgl {
-	struct list_head list;
-	unsigned int cur;		/* Last processed SG entry */
-	struct scatterlist sg[0];	/* Array of SGs forming the SGL */
-};
-
-struct aead_rsgl {
-	struct af_alg_sgl sgl;
-	struct list_head list;
-	size_t sg_num_bytes;		/* Bytes of data in that SGL */
-};
-
-struct aead_async_req {
-	struct kiocb *iocb;
-	struct sock *sk;
-
-	struct aead_rsgl first_rsgl;	/* First RX SG */
-	struct list_head rsgl_list;	/* Track RX SGs */
-
-	struct scatterlist *tsgl;	/* priv. TX SGL of buffers to process */
-	unsigned int tsgl_entries;	/* number of entries in priv. TX SGL */
-
-	unsigned int outlen;		/* Filled output buf length */
-
-	unsigned int areqlen;		/* Length of this data struct */
-	struct aead_request aead_req;	/* req ctx trails this struct */
-};
-
 struct aead_tfm {
 	struct crypto_aead *aead;
 	bool has_key;
@@ -93,9 +65,6 @@ struct aead_ctx {
 	unsigned int len;	/* Length of allocated memory for this struct */
 };
 
-#define MAX_SGL_ENTS ((4096 - sizeof(struct aead_tsgl)) / \
-		      sizeof(struct scatterlist) - 1)
-
 static inline int aead_sndbuf(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
@@ -145,10 +114,10 @@ static int aead_alloc_tsgl(struct sock *sk)
 {
 	struct alg_sock *ask = alg_sk(sk);
 	struct aead_ctx *ctx = ask->private;
-	struct aead_tsgl *sgl;
+	struct af_alg_tsgl *sgl;
 	struct scatterlist *sg = NULL;
 
-	sgl = list_entry(ctx->tsgl_list.prev, struct aead_tsgl, list);
+	sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
 	if (!list_empty(&ctx->tsgl_list))
 		sg = sgl->sg;
 
@@ -180,7 +149,7 @@ static unsigned int aead_count_tsgl(struct sock *sk, size_t bytes,
 {
 	struct alg_sock *ask = alg_sk(sk);
 	struct aead_ctx *ctx = ask->private;
-	struct aead_tsgl *sgl, *tmp;
+	struct af_alg_tsgl *sgl, *tmp;
 	unsigned int i;
 	unsigned int sgl_count = 0;
 
@@ -230,12 +199,12 @@ static void aead_pull_tsgl(struct sock *sk, size_t used,
 {
 	struct alg_sock *ask = alg_sk(sk);
 	struct aead_ctx *ctx = ask->private;
-	struct aead_tsgl *sgl;
+	struct af_alg_tsgl *sgl;
 	struct scatterlist *sg;
 	unsigned int i, j;
 
 	while (!list_empty(&ctx->tsgl_list)) {
-		sgl = list_first_entry(&ctx->tsgl_list, struct aead_tsgl,
+		sgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl,
 				       list);
 		sg = sgl->sg;
 
@@ -289,12 +258,12 @@ static void aead_pull_tsgl(struct sock *sk, size_t used,
 		ctx->merge = 0;
 }
 
-static void aead_free_areq_sgls(struct aead_async_req *areq)
+static void aead_free_areq_sgls(struct af_alg_async_req *areq)
 {
 	struct sock *sk = areq->sk;
 	struct alg_sock *ask = alg_sk(sk);
 	struct aead_ctx *ctx = ask->private;
-	struct aead_rsgl *rsgl, *tmp;
+	struct af_alg_rsgl *rsgl, *tmp;
 	struct scatterlist *tsgl;
 	struct scatterlist *sg;
 	unsigned int i;
@@ -420,7 +389,7 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 	struct aead_tfm *aeadc = pask->private;
 	struct crypto_aead *tfm = aeadc->aead;
 	unsigned int ivsize = crypto_aead_ivsize(tfm);
-	struct aead_tsgl *sgl;
+	struct af_alg_tsgl *sgl;
 	struct af_alg_control con = {};
 	long copied = 0;
 	bool enc = 0;
@@ -470,7 +439,7 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		/* use the existing memory in an allocated page */
 		if (ctx->merge) {
 			sgl = list_entry(ctx->tsgl_list.prev,
-					 struct aead_tsgl, list);
+					 struct af_alg_tsgl, list);
 			sg = sgl->sg + sgl->cur - 1;
 			len = min_t(unsigned long, len,
 				    PAGE_SIZE - sg->offset - sg->length);
@@ -503,7 +472,7 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 		if (err)
 			goto unlock;
 
-		sgl = list_entry(ctx->tsgl_list.prev, struct aead_tsgl,
+		sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl,
 				 list);
 		sg = sgl->sg;
 		if (sgl->cur)
@@ -559,7 +528,7 @@ static ssize_t aead_sendpage(struct socket *sock, struct page *page,
 	struct sock *sk = sock->sk;
 	struct alg_sock *ask = alg_sk(sk);
 	struct aead_ctx *ctx = ask->private;
-	struct aead_tsgl *sgl;
+	struct af_alg_tsgl *sgl;
 	int err = -EINVAL;
 
 	if (flags & MSG_SENDPAGE_NOTLAST)
@@ -583,7 +552,7 @@ static ssize_t aead_sendpage(struct socket *sock, struct page *page,
 		goto unlock;
 
 	ctx->merge = 0;
-	sgl = list_entry(ctx->tsgl_list.prev, struct aead_tsgl, list);
+	sgl = list_entry(ctx->tsgl_list.prev, struct af_alg_tsgl, list);
 
 	if (sgl->cur)
 		sg_unmark_end(sgl->sg + sgl->cur - 1);
@@ -608,7 +577,7 @@ static ssize_t aead_sendpage(struct socket *sock, struct page *page,
 
 static void aead_async_cb(struct crypto_async_request *_req, int err)
 {
-	struct aead_async_req *areq = _req->data;
+	struct af_alg_async_req *areq = _req->data;
 	struct sock *sk = areq->sk;
 	struct kiocb *iocb = areq->iocb;
 	unsigned int resultlen;
@@ -654,10 +623,10 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 	struct crypto_skcipher *null_tfm = aeadc->null_tfm;
 	unsigned int as = crypto_aead_authsize(tfm);
 	unsigned int areqlen =
-		sizeof(struct aead_async_req) + crypto_aead_reqsize(tfm);
-	struct aead_async_req *areq;
-	struct aead_rsgl *last_rsgl = NULL;
-	struct aead_tsgl *tsgl;
+		sizeof(struct af_alg_async_req) + crypto_aead_reqsize(tfm);
+	struct af_alg_async_req *areq;
+	struct af_alg_rsgl *last_rsgl = NULL;
+	struct af_alg_tsgl *tsgl;
 	struct scatterlist *src;
 	int err = 0;
 	size_t used = 0;		/* [in]  TX bufs to be en/decrypted */
@@ -714,7 +683,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 
 	/* convert iovecs of output buffers into RX SGL */
 	while (outlen > usedpages && msg_data_left(msg)) {
-		struct aead_rsgl *rsgl;
+		struct af_alg_rsgl *rsgl;
 		size_t seglen;
 
 		/* limit the amount of readable buffers */
@@ -778,7 +747,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 	}
 
 	processed = used + ctx->aead_assoclen;
-	tsgl = list_first_entry(&ctx->tsgl_list, struct aead_tsgl, list);
+	tsgl = list_first_entry(&ctx->tsgl_list, struct af_alg_tsgl, list);
 
 	/*
 	 * Copy of AAD from source to destination
@@ -861,28 +830,28 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
 	}
 
 	/* Initialize the crypto operation */
-	aead_request_set_crypt(&areq->aead_req, src,
+	aead_request_set_crypt(&areq->cra_u.aead_req, src,
 			       areq->first_rsgl.sgl.sg, used, ctx->iv);
-	aead_request_set_ad(&areq->aead_req, ctx->aead_assoclen);
-	aead_request_set_tfm(&areq->aead_req, tfm);
+	aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
+	aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
 
 	if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
 		/* AIO operation */
 		areq->iocb = msg->msg_iocb;
-		aead_request_set_callback(&areq->aead_req,
+		aead_request_set_callback(&areq->cra_u.aead_req,
 					  CRYPTO_TFM_REQ_MAY_BACKLOG,
 					  aead_async_cb, areq);
-		err = ctx->enc ? crypto_aead_encrypt(&areq->aead_req) :
-				 crypto_aead_decrypt(&areq->aead_req);
+		err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
+				 crypto_aead_decrypt(&areq->cra_u.aead_req);
 	} else {
 		/* Synchronous operation */
-		aead_request_set_callback(&areq->aead_req,
+		aead_request_set_callback(&areq->cra_u.aead_req,
 					  CRYPTO_TFM_REQ_MAY_BACKLOG,
 					  af_alg_complete, &ctx->completion);
 		err = af_alg_wait_for_completion(ctx->enc ?
-					 crypto_aead_encrypt(&areq->aead_req) :
-					 crypto_aead_decrypt(&areq->aead_req),
-					 &ctx->completion);
+				crypto_aead_encrypt(&areq->cra_u.aead_req) :
+				crypto_aead_decrypt(&areq->cra_u.aead_req),
+						 &ctx->completion);
 	}
 
 	/* AIO operation in progress */
crypto/algif_skcipher.c +30 -58

220 lines not on this page · raw message

include/crypto/if_alg.h +53 -0

65 lines not on this page · raw message

Signature · 1 line
2.13.3
maintainer
#17 lore raw
In reply to Stephan Müller
Quoted 23 lines · Stephan Müller

Hi,

with the update of the algif_aead and algif_skcipher memory management, a lot of code duplication has been introduced deliberately.

This patch set cleans up the low-hanging fruits. The cleanup of the recvmsg RX SGL copy will come separately as this is not a simple copy and paste operation.

Each patch was tested individually with libkcapi's test suite.

The patch set goes on top of patches "crypto: AF_ALG - return error code when no data was processed" and "crypto: algif_aead - copy AAD from src to dst".

Stephan Mueller (16):
crypto: AF_ALG - consolidation of common data structures
crypto: AF_ALG - consolidation of context data structure
crypto: AF_ALG - consolidate send buffer service functions
crypto: AF_ALG - consolidate RX buffer service functions
crypto: AF_ALG - consolidate TX SGL allocation
crypto: AF_ALG - consolidate counting TX SG entries
crypto: AF_ALG - consolidate counting TX SG entries
crypto: AF_ALG - consolidate freeing TX/RX SGLs

Quoted 8 lines

crypto: AF_ALG - consolidate waiting for wmem
crypto: AF_ALG - consolidate waking up on writable memory
crypto: AF_ALG - consolidate waiting for TX data
crypto: AF_ALG - consolidate waking up caller for TX data
crypto: AF_ALG - consolidate sendmsg implementation
crypto: AF_ALG - consolidate sendpage implementation
crypto: AF_ALG - consolidate AIO callback handler
crypto: AF_ALG - consolidate poll syscall handler

This split makes no sense. I don't see why you can't merge them into a single patch if it's just rearranging the code.

Cheers,

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
#18 lore raw

Am Dienstag, 1. August 2017, 10:58:58 CEST schrieb Herbert Xu:

Hi Herbert,

Quoted 3 lines

This split makes no sense. I don't see why you can't merge them into a single patch if it's just rearranging the code.

If you want to merge all into one patch, I am fine.

I thought that separating all out makes the patch review easier, considering that in two or three of the patches, small changes are implemented to allow a common code base. These changes, however, should not have any functional effect.

Ciao
Stephan

maintainer
#19 lore raw
Quoted 7 lines · Stephan Müller

Am Dienstag, 1. August 2017, 10:58:58 CEST schrieb Herbert Xu:

Hi Herbert,

Quoted 3 lines

This split makes no sense. I don't see why you can't merge them into a single patch if it's just rearranging the code.

Quoted 6 lines

If you want to merge all into one patch, I am fine.

I thought that separating all out makes the patch review easier, considering that in two or three of the patches, small changes are implemented to allow a common code base. These changes, however, should not have any functional effect.

How about you separate into three patches? The first two make changes to algif_skcipher/algif_aead so that they can be merged, and the last one does the actual merging.

Cheers,

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
#20 lore raw

Am Dienstag, 1. August 2017, 11:08:33 CEST schrieb Herbert Xu:

Hi Herbert,

Quoted 5 lines

How about you separate into three patches? The first two make changes to algif_skcipher/algif_aead so that they can be merged, and the last one does the actual merging.

Ok, I can do that.

How would you want to handle the following: the new af_alg_wait_for_data will include:

if (sk_wait_event(sk, &timeout, (ctx->used || !ctx->more)

whereas algif_aead only contains !ctx->more and algif_skcipher only contains ctx->used in the check? When merging both, having the common check is good for both, but not needed individually.

Shall I only make the change during the merger or add these changes to the modification of algif_skcipher/algif_aead?

Please then also disregard the recvmsg consolidation patch from this morning.

Ciao
Stephan

maintainer
#21 lore raw
Quoted 3 lines · Stephan Müller

Shall I only make the change during the merger or add these changes to the modification of algif_skcipher/algif_aead?

Either way is fine.

Cheers,

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