Skip to content

Commit 49573ff

Browse files
committed
Merge branch 'tls-splice_read-fixes'
Jakub Kicinski says: ==================== tls: splice_read fixes As I work my way to unlocked and zero-copy TLS Rx the obvious bugs in the splice_read implementation get harder and harder to ignore. This is to say the fixes here are discovered by code inspection, I'm not aware of anyone actually using splice_read. ==================== Link: https://lore.kernel.org/r/20211124232557.2039757-1-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents 9dbe33c + f884a34 commit 49573ff

File tree

3 files changed

+456
-152
lines changed

3 files changed

+456
-152
lines changed

net/tls/tls_main.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static DEFINE_MUTEX(tcpv6_prot_mutex);
6161
static const struct proto *saved_tcpv4_prot;
6262
static DEFINE_MUTEX(tcpv4_prot_mutex);
6363
static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
64-
static struct proto_ops tls_sw_proto_ops;
64+
static struct proto_ops tls_proto_ops[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
6565
static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
6666
const struct proto *base);
6767

@@ -71,6 +71,8 @@ void update_sk_prot(struct sock *sk, struct tls_context *ctx)
7171

7272
WRITE_ONCE(sk->sk_prot,
7373
&tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf]);
74+
WRITE_ONCE(sk->sk_socket->ops,
75+
&tls_proto_ops[ip_ver][ctx->tx_conf][ctx->rx_conf]);
7476
}
7577

7678
int wait_on_pending_writer(struct sock *sk, long *timeo)
@@ -669,8 +671,6 @@ static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
669671
if (tx) {
670672
ctx->sk_write_space = sk->sk_write_space;
671673
sk->sk_write_space = tls_write_space;
672-
} else {
673-
sk->sk_socket->ops = &tls_sw_proto_ops;
674674
}
675675
goto out;
676676

@@ -728,6 +728,39 @@ struct tls_context *tls_ctx_create(struct sock *sk)
728728
return ctx;
729729
}
730730

731+
static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
732+
const struct proto_ops *base)
733+
{
734+
ops[TLS_BASE][TLS_BASE] = *base;
735+
736+
ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
737+
ops[TLS_SW ][TLS_BASE].sendpage_locked = tls_sw_sendpage_locked;
738+
739+
ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
740+
ops[TLS_BASE][TLS_SW ].splice_read = tls_sw_splice_read;
741+
742+
ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE];
743+
ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read;
744+
745+
#ifdef CONFIG_TLS_DEVICE
746+
ops[TLS_HW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
747+
ops[TLS_HW ][TLS_BASE].sendpage_locked = NULL;
748+
749+
ops[TLS_HW ][TLS_SW ] = ops[TLS_BASE][TLS_SW ];
750+
ops[TLS_HW ][TLS_SW ].sendpage_locked = NULL;
751+
752+
ops[TLS_BASE][TLS_HW ] = ops[TLS_BASE][TLS_SW ];
753+
754+
ops[TLS_SW ][TLS_HW ] = ops[TLS_SW ][TLS_SW ];
755+
756+
ops[TLS_HW ][TLS_HW ] = ops[TLS_HW ][TLS_SW ];
757+
ops[TLS_HW ][TLS_HW ].sendpage_locked = NULL;
758+
#endif
759+
#ifdef CONFIG_TLS_TOE
760+
ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
761+
#endif
762+
}
763+
731764
static void tls_build_proto(struct sock *sk)
732765
{
733766
int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
@@ -739,6 +772,8 @@ static void tls_build_proto(struct sock *sk)
739772
mutex_lock(&tcpv6_prot_mutex);
740773
if (likely(prot != saved_tcpv6_prot)) {
741774
build_protos(tls_prots[TLSV6], prot);
775+
build_proto_ops(tls_proto_ops[TLSV6],
776+
sk->sk_socket->ops);
742777
smp_store_release(&saved_tcpv6_prot, prot);
743778
}
744779
mutex_unlock(&tcpv6_prot_mutex);
@@ -749,6 +784,8 @@ static void tls_build_proto(struct sock *sk)
749784
mutex_lock(&tcpv4_prot_mutex);
750785
if (likely(prot != saved_tcpv4_prot)) {
751786
build_protos(tls_prots[TLSV4], prot);
787+
build_proto_ops(tls_proto_ops[TLSV4],
788+
sk->sk_socket->ops);
752789
smp_store_release(&saved_tcpv4_prot, prot);
753790
}
754791
mutex_unlock(&tcpv4_prot_mutex);
@@ -959,10 +996,6 @@ static int __init tls_register(void)
959996
if (err)
960997
return err;
961998

962-
tls_sw_proto_ops = inet_stream_ops;
963-
tls_sw_proto_ops.splice_read = tls_sw_splice_read;
964-
tls_sw_proto_ops.sendpage_locked = tls_sw_sendpage_locked;
965-
966999
tls_device_init();
9671000
tcp_register_ulp(&tcp_tls_ulp_ops);
9681001

net/tls/tls_sw.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,7 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
20052005
struct sock *sk = sock->sk;
20062006
struct sk_buff *skb;
20072007
ssize_t copied = 0;
2008+
bool from_queue;
20082009
int err = 0;
20092010
long timeo;
20102011
int chunk;
@@ -2014,33 +2015,46 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
20142015

20152016
timeo = sock_rcvtimeo(sk, flags & SPLICE_F_NONBLOCK);
20162017

2017-
skb = tls_wait_data(sk, NULL, flags & SPLICE_F_NONBLOCK, timeo, &err);
2018-
if (!skb)
2019-
goto splice_read_end;
2020-
2021-
if (!ctx->decrypted) {
2022-
err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc, false);
2023-
2024-
/* splice does not support reading control messages */
2025-
if (ctx->control != TLS_RECORD_TYPE_DATA) {
2026-
err = -EINVAL;
2018+
from_queue = !skb_queue_empty(&ctx->rx_list);
2019+
if (from_queue) {
2020+
skb = __skb_dequeue(&ctx->rx_list);
2021+
} else {
2022+
skb = tls_wait_data(sk, NULL, flags & SPLICE_F_NONBLOCK, timeo,
2023+
&err);
2024+
if (!skb)
20272025
goto splice_read_end;
2028-
}
20292026

2027+
err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc, false);
20302028
if (err < 0) {
20312029
tls_err_abort(sk, -EBADMSG);
20322030
goto splice_read_end;
20332031
}
2034-
ctx->decrypted = 1;
20352032
}
2033+
2034+
/* splice does not support reading control messages */
2035+
if (ctx->control != TLS_RECORD_TYPE_DATA) {
2036+
err = -EINVAL;
2037+
goto splice_read_end;
2038+
}
2039+
20362040
rxm = strp_msg(skb);
20372041

20382042
chunk = min_t(unsigned int, rxm->full_len, len);
20392043
copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
20402044
if (copied < 0)
20412045
goto splice_read_end;
20422046

2043-
tls_sw_advance_skb(sk, skb, copied);
2047+
if (!from_queue) {
2048+
ctx->recv_pkt = NULL;
2049+
__strp_unpause(&ctx->strp);
2050+
}
2051+
if (chunk < rxm->full_len) {
2052+
__skb_queue_head(&ctx->rx_list, skb);
2053+
rxm->offset += len;
2054+
rxm->full_len -= len;
2055+
} else {
2056+
consume_skb(skb);
2057+
}
20442058

20452059
splice_read_end:
20462060
release_sock(sk);

0 commit comments

Comments
 (0)