Commit c57ca512 authored by Jakub Kicinski's avatar Jakub Kicinski Committed by David S. Miller

net: tls: factor out tls_*crypt_async_wait()

Factor out waiting for async encrypt and decrypt to finish.
There are already multiple copies and a subsequent fix will
need more. No functional changes.

Note that crypto_wait_req() returns wait->err
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
Reviewed-by: default avatarSimon Horman <horms@kernel.org>
Reviewed-by: default avatarSabrina Dubroca <sd@queasysnail.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 2fbdc5c6
...@@ -230,6 +230,20 @@ static void tls_decrypt_done(void *data, int err) ...@@ -230,6 +230,20 @@ static void tls_decrypt_done(void *data, int err)
spin_unlock_bh(&ctx->decrypt_compl_lock); spin_unlock_bh(&ctx->decrypt_compl_lock);
} }
static int tls_decrypt_async_wait(struct tls_sw_context_rx *ctx)
{
int pending;
spin_lock_bh(&ctx->decrypt_compl_lock);
reinit_completion(&ctx->async_wait.completion);
pending = atomic_read(&ctx->decrypt_pending);
spin_unlock_bh(&ctx->decrypt_compl_lock);
if (pending)
crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
return ctx->async_wait.err;
}
static int tls_do_decryption(struct sock *sk, static int tls_do_decryption(struct sock *sk,
struct scatterlist *sgin, struct scatterlist *sgin,
struct scatterlist *sgout, struct scatterlist *sgout,
...@@ -495,6 +509,28 @@ static void tls_encrypt_done(void *data, int err) ...@@ -495,6 +509,28 @@ static void tls_encrypt_done(void *data, int err)
schedule_delayed_work(&ctx->tx_work.work, 1); schedule_delayed_work(&ctx->tx_work.work, 1);
} }
static int tls_encrypt_async_wait(struct tls_sw_context_tx *ctx)
{
int pending;
spin_lock_bh(&ctx->encrypt_compl_lock);
ctx->async_notify = true;
pending = atomic_read(&ctx->encrypt_pending);
spin_unlock_bh(&ctx->encrypt_compl_lock);
if (pending)
crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
else
reinit_completion(&ctx->async_wait.completion);
/* There can be no concurrent accesses, since we have no
* pending encrypt operations
*/
WRITE_ONCE(ctx->async_notify, false);
return ctx->async_wait.err;
}
static int tls_do_encryption(struct sock *sk, static int tls_do_encryption(struct sock *sk,
struct tls_context *tls_ctx, struct tls_context *tls_ctx,
struct tls_sw_context_tx *ctx, struct tls_sw_context_tx *ctx,
...@@ -984,7 +1020,6 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, ...@@ -984,7 +1020,6 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
int num_zc = 0; int num_zc = 0;
int orig_size; int orig_size;
int ret = 0; int ret = 0;
int pending;
if (!eor && (msg->msg_flags & MSG_EOR)) if (!eor && (msg->msg_flags & MSG_EOR))
return -EINVAL; return -EINVAL;
...@@ -1163,24 +1198,12 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, ...@@ -1163,24 +1198,12 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
if (!num_async) { if (!num_async) {
goto send_end; goto send_end;
} else if (num_zc) { } else if (num_zc) {
/* Wait for pending encryptions to get completed */ int err;
spin_lock_bh(&ctx->encrypt_compl_lock);
ctx->async_notify = true;
pending = atomic_read(&ctx->encrypt_pending);
spin_unlock_bh(&ctx->encrypt_compl_lock);
if (pending)
crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
else
reinit_completion(&ctx->async_wait.completion);
/* There can be no concurrent accesses, since we have no
* pending encrypt operations
*/
WRITE_ONCE(ctx->async_notify, false);
if (ctx->async_wait.err) { /* Wait for pending encryptions to get completed */
ret = ctx->async_wait.err; err = tls_encrypt_async_wait(ctx);
if (err) {
ret = err;
copied = 0; copied = 0;
} }
} }
...@@ -1229,7 +1252,6 @@ void tls_sw_splice_eof(struct socket *sock) ...@@ -1229,7 +1252,6 @@ void tls_sw_splice_eof(struct socket *sock)
ssize_t copied = 0; ssize_t copied = 0;
bool retrying = false; bool retrying = false;
int ret = 0; int ret = 0;
int pending;
if (!ctx->open_rec) if (!ctx->open_rec)
return; return;
...@@ -1264,22 +1286,7 @@ void tls_sw_splice_eof(struct socket *sock) ...@@ -1264,22 +1286,7 @@ void tls_sw_splice_eof(struct socket *sock)
} }
/* Wait for pending encryptions to get completed */ /* Wait for pending encryptions to get completed */
spin_lock_bh(&ctx->encrypt_compl_lock); if (tls_encrypt_async_wait(ctx))
ctx->async_notify = true;
pending = atomic_read(&ctx->encrypt_pending);
spin_unlock_bh(&ctx->encrypt_compl_lock);
if (pending)
crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
else
reinit_completion(&ctx->async_wait.completion);
/* There can be no concurrent accesses, since we have no pending
* encrypt operations
*/
WRITE_ONCE(ctx->async_notify, false);
if (ctx->async_wait.err)
goto unlock; goto unlock;
/* Transmit if any encryptions have completed */ /* Transmit if any encryptions have completed */
...@@ -2109,16 +2116,10 @@ int tls_sw_recvmsg(struct sock *sk, ...@@ -2109,16 +2116,10 @@ int tls_sw_recvmsg(struct sock *sk,
recv_end: recv_end:
if (async) { if (async) {
int ret, pending; int ret;
/* Wait for all previously submitted records to be decrypted */ /* Wait for all previously submitted records to be decrypted */
spin_lock_bh(&ctx->decrypt_compl_lock); ret = tls_decrypt_async_wait(ctx);
reinit_completion(&ctx->async_wait.completion);
pending = atomic_read(&ctx->decrypt_pending);
spin_unlock_bh(&ctx->decrypt_compl_lock);
ret = 0;
if (pending)
ret = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
__skb_queue_purge(&ctx->async_hold); __skb_queue_purge(&ctx->async_hold);
if (ret) { if (ret) {
...@@ -2435,16 +2436,9 @@ void tls_sw_release_resources_tx(struct sock *sk) ...@@ -2435,16 +2436,9 @@ void tls_sw_release_resources_tx(struct sock *sk)
struct tls_context *tls_ctx = tls_get_ctx(sk); struct tls_context *tls_ctx = tls_get_ctx(sk);
struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
struct tls_rec *rec, *tmp; struct tls_rec *rec, *tmp;
int pending;
/* Wait for any pending async encryptions to complete */ /* Wait for any pending async encryptions to complete */
spin_lock_bh(&ctx->encrypt_compl_lock); tls_encrypt_async_wait(ctx);
ctx->async_notify = true;
pending = atomic_read(&ctx->encrypt_pending);
spin_unlock_bh(&ctx->encrypt_compl_lock);
if (pending)
crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
tls_tx_records(sk, -1); tls_tx_records(sk, -1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment