Received: by 2002:ab2:2997:0:b0:1ec:cbc4:63fb with SMTP id n23csp532077lqb; Thu, 29 Feb 2024 07:52:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWQ4HOTs4mxyxfZLqzIGZlkKuPSjTbd+AudmZxviU06l4hibKfUL964Q256/7f1I4CjVoPdsb8lUvJaSLMBz2T5dI0ldmn+agsdKaEtzw== X-Google-Smtp-Source: AGHT+IFdVHu92oq/9OSEAFwqfJ3Y0vJqJxR+ZTwYc0K69BEtjZkPVpnC61vREvKk068vEIa5CL3T X-Received: by 2002:a05:6870:1492:b0:21f:d2ad:6b73 with SMTP id k18-20020a056870149200b0021fd2ad6b73mr2437999oab.54.1709221920739; Thu, 29 Feb 2024 07:52:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709221920; cv=pass; d=google.com; s=arc-20160816; b=CSIZf1DdIpbW54hx7XrqcrvvScL/QlGeYmwAeLtQaXNmgrFF9j0rr/+5IKF30UY5N2 nlPDPyo6w3gJPKZpKzctzmLJVcwDorb3RVTsU0DnMPH/3fY631bWr00+6cOE7pnvJKrS 0gJ06posOzgKumdkaHRZgomCffBpI/xvbEFCpe84F93NRzzMF+t3ZgOYo+0gat2wd9BD KHoamyUqlnMg5+r9WtFpN9VCdD2ErGyYppb9VQoj+wAGXkYFWJC5RRLTTh6TE6DWW4k7 Gl8CAi6LmvObKCQbWennZz4v4jztUB4lVapS/bSkyhK2KJENTLMVXhDzpLqbomcC/Btz S8Wg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=FgjGRFmOrQc8hUJqptzasYaHufvi39R9qt6y10bKsZE=; fh=eX8XBuX7NQWvemfzKuYbNQt9OqkjL/T9V8667WQur3E=; b=s6ixDLD6pUBghDqlZ6EHBHchLwKm+3hCrwXyb2MuyQP5TDgD84vaTTzgdS7qDVrZJC DkhQ0nA81NtMfIwog09pzjP4a1YeSTUm0qhi/OeiFTJWnxy2nn3qh+cKxL+XqR2Iahaj FMyuF+18PTv4nGGOczPvdhtKB1ES3rU5U59PsMQo3svMNq/MFhfebB4EFrjKA/i5OYhX RZ+2XUV5y6brPh4XH61uOD9gV5qX6KRax9bMNbUhnl9uRGXWD4/hZfFJLXk2AvK5lH7w 8VMjJ2YcfioBqOBXHlihDf6RuvvNAGyv5a7P64oiP6G4t7DrpqJTn9WYuB+sAArwpVDA IVsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hlpnsrw3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-86959-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86959-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ey8-20020a0562140b6800b0068fe583c0bbsi1676944qvb.314.2024.02.29.07.52.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 07:52:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86959-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hlpnsrw3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-86959-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86959-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5B5A81C20F50 for ; Thu, 29 Feb 2024 15:52:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D9A361586D4; Thu, 29 Feb 2024 15:49:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Hlpnsrw3" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E287315697B; Thu, 29 Feb 2024 15:49:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709221754; cv=none; b=pkqNRxLzUm7EqkWXgd+autiJPgebSrZfd0DybCDxYB1LvPmBK1T56nn88WTXuMNZo6A46PWaC+wZXMXMPB46f7sVeg4V89b9WgP7daEg+WplCb05axfGwDx0DQymdRwe6u7pASi8QUhbC1fjQHWlrtp6mvQlJsOQMmc1II0ThXY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709221754; c=relaxed/simple; bh=9qydeZ7dVGJrZhYf5SEPFF/yp8kpRzWIKk8rEXwn+Ec=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZOpzSxVWwYoMR2E2wX6ohZhHm5OlpYt7NEGbIr2kJ4IZc3GPemgugdMXDdoybCRr00iEVqgHXvRvhbUoZkU6pZVY5ITyLywr6QBbVhdB6pnVoD++P8NUDWVYhgOVq+4dF/PZVLsXLaIbmby3mFxofAUTD31fmbFrXk0MCHbfXNs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hlpnsrw3; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 157B0C433C7; Thu, 29 Feb 2024 15:49:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709221753; bh=9qydeZ7dVGJrZhYf5SEPFF/yp8kpRzWIKk8rEXwn+Ec=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hlpnsrw3jTlexkwNCRD5wkL+o26sGTWE3XQlB4R5y2ud0g6M/FdaBhv2aIOmdNwQ5 Ba4NaJuX5hNYAbTcxkAumCvNg4noGIl723UvUv8Hv2/A6M6/4d4oKqf4ZRtKDVayrG K/a3y1EQCPpjUSWnmzCNjKQz22stJNr3ZiJ0r7WM23UTWgSZE15csJ6CKkPpLPEnuL ttk2y0BygGkDxHziJ3hVYR9+yhN30lLQP3TXihcGjKmpU2fEj/ag6yWgrGU3zBgGrS DkDNwXnxms5FG40sm6g3SoN93ldZyjsHJhevqZikMIwmvLjSAWEFr3GANbIfBheNaz JNwwu9mT1qb7Q== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jakub Kicinski , Simon Horman , Sabrina Dubroca , "David S . Miller" , Sasha Levin , borisp@nvidia.com, john.fastabend@gmail.com, edumazet@google.com, pabeni@redhat.com, netdev@vger.kernel.org Subject: [PATCH AUTOSEL 6.7 11/26] net: tls: factor out tls_*crypt_async_wait() Date: Thu, 29 Feb 2024 10:48:30 -0500 Message-ID: <20240229154851.2849367-11-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240229154851.2849367-1-sashal@kernel.org> References: <20240229154851.2849367-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.7.6 Content-Transfer-Encoding: 8bit From: Jakub Kicinski [ Upstream commit c57ca512f3b68ddcd62bda9cc24a8f5584ab01b1 ] 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: Jakub Kicinski Reviewed-by: Simon Horman Reviewed-by: Sabrina Dubroca Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/tls/tls_sw.c | 96 +++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index 31e8a94dfc111..6a73714f34cc4 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -230,6 +230,20 @@ static void tls_decrypt_done(void *data, int err) 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, struct scatterlist *sgin, struct scatterlist *sgout, @@ -495,6 +509,28 @@ static void tls_encrypt_done(void *data, int err) 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, struct tls_context *tls_ctx, struct tls_sw_context_tx *ctx, @@ -984,7 +1020,6 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, int num_zc = 0; int orig_size; int ret = 0; - int pending; if (!eor && (msg->msg_flags & MSG_EOR)) return -EINVAL; @@ -1163,24 +1198,12 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, if (!num_async) { goto send_end; } else if (num_zc) { - /* Wait for pending encryptions to get completed */ - 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); + int err; - if (ctx->async_wait.err) { - ret = ctx->async_wait.err; + /* Wait for pending encryptions to get completed */ + err = tls_encrypt_async_wait(ctx); + if (err) { + ret = err; copied = 0; } } @@ -1229,7 +1252,6 @@ void tls_sw_splice_eof(struct socket *sock) ssize_t copied = 0; bool retrying = false; int ret = 0; - int pending; if (!ctx->open_rec) return; @@ -1264,22 +1286,7 @@ void tls_sw_splice_eof(struct socket *sock) } /* Wait for pending encryptions to get completed */ - 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) + if (tls_encrypt_async_wait(ctx)) goto unlock; /* Transmit if any encryptions have completed */ @@ -2109,16 +2116,10 @@ int tls_sw_recvmsg(struct sock *sk, recv_end: if (async) { - int ret, pending; + int ret; /* Wait for all previously submitted records to be decrypted */ - 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); - ret = 0; - if (pending) - ret = crypto_wait_req(-EINPROGRESS, &ctx->async_wait); + ret = tls_decrypt_async_wait(ctx); __skb_queue_purge(&ctx->async_hold); if (ret) { @@ -2435,16 +2436,9 @@ void tls_sw_release_resources_tx(struct sock *sk) struct tls_context *tls_ctx = tls_get_ctx(sk); struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); struct tls_rec *rec, *tmp; - int pending; /* Wait for any pending async encryptions to complete */ - 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); + tls_encrypt_async_wait(ctx); tls_tx_records(sk, -1); -- 2.43.0