tls: rx: strp: fix inline crypto offload
Subsystems (3)
Jakub Kicinski
maintainercover tls: rx: strp: fix inline crypto offload
Tariq, here are the fixes for the bug you reported. I managed to test with mlx5 (and selftest, obviously). I hacked things up for testing to trigger the copy and reencrypt paths.
Could you run it thru your tests and LMK if there are any more regressions?
Jakub Kicinski (7):
tls: rx: device: fix checking decryption status
tls: rx: strp: set the skb->len of detached / CoW'ed skbs
tls: rx: strp: force mixed decrypted records into copy mode
tls: rx: strp: fix determining record length in copy mode
tls: rx: strp: factor out copying skb data
tls: rx: strp: preserve decryption status of skbs when needed
tls: rx: strp: don't use GFP_KERNEL in softirq context
include/linux/skbuff.h | 10 +++ include/net/tls.h | 1 + net/tls/tls.h | 5 ++ net/tls/tls_device.c | 22 ++--- net/tls/tls_strp.c | 185 +++++++++++++++++++++++++++++++++-------- net/tls/tls_sw.c | 4 + 6 files changed, 177 insertions(+), 50 deletions(-)
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerRFC 1/7 tls: rx: device: fix checking decryption status
skb->len covers the entire skb, including the frag_list. In fact we're guaranteed that rxm->full_len <= skb->len, so since the change under Fixes we were not checking decrypt status of any skb but the first.
Note that the skb_pagelen() added here may feel a bit costly, but it's removed by subsequent fixes, anyway.
--- net/tls/tls_device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/tls/tls_device.c +1 -1
@@ -1012,7 +1012,7 @@ int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)struct sk_buff *skb_iter;int left; - left = rxm->full_len - skb->len; + left = rxm->full_len + rxm->offset -skb_pagelen (skb);/* Check if all the data is decrypted already */ skb_iter =skb_shinfo (skb)->frag_list;while (skb_iter && left >0 ) {
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerRFC 2/7 tls: rx: strp: set the skb->len of detached / CoW'ed skbs
In reply to Jakub Kicinskialloc_skb_with_frags() fills in page frag sizes but does not set skb->len and skb->data_len. Set those correctly otherwise device offload will most likely generate an empty skb and hit the BUG() at the end of __skb_nsg().
--- net/tls/tls_strp.c | 2 ++ 1 file changed, 2 insertions(+)
net/tls/tls_strp.c +2 -0
@@ -56,6 +56,8 @@ static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp) offset +=skb_frag_size (frag); } + skb->len = len; + skb->data_len = len;skb_copy_header (skb, strp->anchor); rxm =strp_msg (skb); rxm->offset =0 ;
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerRFC 3/7 tls: rx: strp: force mixed decrypted records into copy mode
In reply to Jakub KicinskiIf a record is partially decrypted we'll have to CoW it, anyway, so go into copy mode and allocate a writable skb right away.
This will make subsequent fix simpler because we won't have to teach tls_strp_msg_make_copy() how to copy skbs while preserving decrypt status.
--- include/linux/skbuff.h | 10 ++++++++++ net/tls/tls_strp.c | 16 +++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
include/linux/skbuff.h +10 -0
@@ -1587,6 +1587,16 @@ static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from) to->l4_hash = from->l4_hash; }; +static inline int skb_cmp_decrypted (const struct sk_buff *skb1, +const struct sk_buff *skb2) +{ +#ifdef CONFIG_TLS_DEVICE +return skb2->decrypted - skb1->decrypted; +#else +return 0 ; +#endif +} +static inline void skb_copy_decrypted (struct sk_buff *to,const struct sk_buff *from) {
net/tls/tls_strp.c +11 -5
@@ -317,15 +317,19 @@ static int tls_strp_read_copy(struct tls_strparser *strp, bool qshort)return 0 ; } -static bool tls_strp_check_no_dup (struct tls_strparser *strp) +static bool tls_strp_check_queue_ok (struct tls_strparser *strp) {unsigned int len = strp->stm.offset + strp->stm.full_len; -struct sk_buff *skb; +struct sk_buff *first, *skb;u32 seq; - skb =skb_shinfo (strp->anchor)->frag_list; - seq =TCP_SKB_CB (skb)->seq; + first =skb_shinfo (strp->anchor)->frag_list; + skb = first; + seq =TCP_SKB_CB (first)->seq; +/* Make sure there's no duplicate data in the queue, +* and the decrypted status matches. +*/ while (skb->len < len) { seq += skb->len; len -= skb->len;
@@ -333,6 +337,8 @@ static bool tls_strp_check_no_dup(struct tls_strparser *strp)if (TCP_SKB_CB (skb)->seq != seq)return false; +if (skb_cmp_decrypted (first, skb)) +return false; }return true;
@@ -413,7 +419,7 @@ static int tls_strp_read_sock(struct tls_strparser *strp)return tls_strp_read_copy (strp, true); } -if (!tls_strp_check_no_dup (strp)) +if (!tls_strp_check_queue_ok (strp))return tls_strp_read_copy (strp, false); strp->msg_ready =1 ;
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerRFC 4/7 tls: rx: strp: fix determining record length in copy mode
In reply to Jakub KicinskiWe call tls_rx_msg_size(skb) before doing skb->len += chunk. So the tls_rx_msg_size() code will see old skb->len, most likely leading to an over-read.
Worst case we will over read an entire record, next iteration will try to trim the skb but may end up turning frag len negative or discarding the subsequent record (since we already told TCP we've read it during previous read but now we'll trim it out of the skb).
--- net/tls/tls_strp.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
net/tls/tls_strp.c +15 -6
@@ -210,19 +210,28 @@ static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,skb_frag_size (frag), chunk)); - sz =tls_rx_msg_size (strp, strp->anchor); + skb->len += chunk; + skb->data_len += chunk; +skb_frag_size_add (frag, chunk); + + sz =tls_rx_msg_size (strp, skb);if (sz <0 ) { desc->error = sz;return 0 ; }/* We may have over-read, sz == 0 is guaranteed under-read */ -if (sz >0 ) - chunk =min_t (size_t, chunk, sz - skb->len); +if (unlikely (sz && sz < skb->len)) { +int over = skb->len - sz; + +WARN_ON_ONCE (over > chunk); + skb->len -= over; + skb->data_len -= over; +skb_frag_size_add (frag, -over); + + chunk -= over; + } - skb->len += chunk; - skb->data_len += chunk; -skb_frag_size_add (frag, chunk); frag++; len -= chunk; offset += chunk;
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerRFC 5/7 tls: rx: strp: factor out copying skb data
In reply to Jakub KicinskiWe'll need to copy input skbs individually in the next patch. Factor that code out (without assuming we're copying a full record).
--- net/tls/tls_strp.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
net/tls/tls_strp.c +21 -8
@@ -34,23 +34,22 @@ static void tls_strp_anchor_free(struct tls_strparser *strp) strp->anchor =NULL ; } -/* Create a new skb with the contents of input copied to its page frags */ -static struct sk_buff *tls_strp_msg_make_copy (struct tls_strparser *strp) +static struct sk_buff * +tls_strp_skb_copy (struct tls_strparser *strp,struct sk_buff *in_skb, +int offset,int len) { -struct strp_msg *rxm;struct sk_buff *skb; -int i, err, offset; +int i, err; - skb =alloc_skb_with_frags (0 , strp->stm.full_len,TLS_PAGE_ORDER , + skb =alloc_skb_with_frags (0 , len,TLS_PAGE_ORDER , &err, strp->sk->sk_allocation);if (!skb)return NULL ; - offset = strp->stm.offset;for (i =0 ; i <skb_shinfo (skb)->nr_frags; i++) {skb_frag_t *frag = &skb_shinfo (skb)->frags[i]; -WARN_ON_ONCE (skb_copy_bits (strp->anchor, offset, +WARN_ON_ONCE (skb_copy_bits (in_skb, offset,skb_frag_address (frag),skb_frag_size (frag))); offset +=skb_frag_size (frag);
@@ -58,7 +57,21 @@ static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp) skb->len = len; skb->data_len = len; -skb_copy_header (skb, strp->anchor); +skb_copy_header (skb, in_skb); +return skb; +} + +/* Create a new skb with the contents of input copied to its page frags */ +static struct sk_buff *tls_strp_msg_make_copy (struct tls_strparser *strp) +{ +struct strp_msg *rxm; +struct sk_buff *skb; + + skb =tls_strp_skb_copy (strp, strp->anchor, strp->stm.offset, + strp->stm.full_len); +if (!skb) +return NULL ; + rxm =strp_msg (skb); rxm->offset =0 ;return skb;
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerRFC 6/7 tls: rx: strp: preserve decryption status of skbs when needed
In reply to Jakub KicinskiWhen receive buffer is small we try to copy out the data from TCP into a skb maintained by TLS to prevent connection from stalling. Unfortunately if a single record is made up of a mix of decrypted and non-decrypted skbs combining them into a single skb leads to loss of decryption status, resulting in decryption errors or data corruption.
Similarly when trying to use TCP receive queue directly we need to make sure that all the skbs within the record have the same status. If we don't the mixed status will be detected correctly but we'll CoW the anchor, again collapsing it into a single paged skb without decrypted status preserved. So the "fixup" code will not know which parts of skb to re-encrypt.
--- include/net/tls.h | 1 + net/tls/tls.h | 5 ++ net/tls/tls_device.c | 22 +++----- net/tls/tls_strp.c | 117 ++++++++++++++++++++++++++++++++++++------- 4 files changed, 114 insertions(+), 31 deletions(-)
include/net/tls.h +1 -0
@@ -126,6 +126,7 @@ struct tls_strparser { u32 mark : 8; u32 stopped : 1; u32 copy_mode : 1; + u32 mixed_decrypted : 1; u32 msg_ready : 1;struct strp_msg stm;
net/tls/tls.h +5 -0
@@ -167,6 +167,11 @@ static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx)return ctx->strp.msg_ready; } +static inline bool tls_strp_msg_mixed_decrypted (struct tls_sw_context_rx *ctx) +{ +return ctx->strp.mixed_decrypted; +} +#ifdef CONFIG_TLS_DEVICE int tls_device_init (void );void tls_device_cleanup (void );
net/tls/tls_device.c +8 -14
@@ -1007,20 +1007,14 @@ int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)struct tls_sw_context_rx *sw_ctx =tls_sw_ctx_rx (tls_ctx);struct sk_buff *skb =tls_strp_msg (sw_ctx);struct strp_msg *rxm =strp_msg (skb); -int is_decrypted = skb->decrypted; -int is_encrypted = !is_decrypted; -struct sk_buff *skb_iter; -int left; - - left = rxm->full_len + rxm->offset -skb_pagelen (skb); -/* Check if all the data is decrypted already */ - skb_iter =skb_shinfo (skb)->frag_list; -while (skb_iter && left >0 ) { - is_decrypted &= skb_iter->decrypted; - is_encrypted &= !skb_iter->decrypted; - - left -= skb_iter->len; - skb_iter = skb_iter->next; +int is_decrypted, is_encrypted; + +if (!tls_strp_msg_mixed_decrypted (sw_ctx)) { + is_decrypted = skb->decrypted; + is_encrypted = !is_decrypted; + }else { + is_decrypted =0 ; + is_encrypted =0 ; } trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len,
net/tls/tls_strp.c +100 -17
154 lines not on this page · raw message
Signature · 1 line
2.40.1
Jakub Kicinski
maintainerRFC 7/7 tls: rx: strp: don't use GFP_KERNEL in softirq context
In reply to Jakub KicinskiWhen receive buffer is small, or the TCP rx queue looks too complicated to bother using it directly - we allocate a new skb and copy data into it.
We already use sk->sk_allocation... but nothing actually sets it to GFP_ATOMIC on the ->sk_data_ready() path.
Users of HW offload are far more likely to experience problems due to scheduling while atomic. "Copy mode" is very rarely triggered with SW crypto.
--- net/tls/tls_sw.c | 4 ++++ 1 file changed, 4 insertions(+)
net/tls/tls_sw.c +4 -0
@@ -2304,10 +2304,14 @@ static void tls_data_ready(struct sock *sk)struct tls_context *tls_ctx =tls_get_ctx (sk);struct tls_sw_context_rx *ctx =tls_sw_ctx_rx (tls_ctx);struct sk_psock *psock; +gfp_t alloc_save;trace_sk_data_ready (sk); + alloc_save = sk->sk_allocation; + sk->sk_allocation =GFP_ATOMIC ;tls_strp_data_ready (&ctx->strp); + sk->sk_allocation = alloc_save; psock =sk_psock_get (sk);if (psock) {
Signature · 1 line
2.40.1
Quoted 8 lines · Jakub Kicinski
Tariq, here are the fixes for the bug you reported. I managed to test with mlx5 (and selftest, obviously). I hacked things up for testing to trigger the copy and reencrypt paths.
Could you run it thru your tests and LMK if there are any more regressions?
Hi Jakub,
Thanks for your patches!
I see that several changes were needed.
I tested your series with the repro I had, it seems to be resolved.
We are going to run more intensive and comprehensive tests during the weekend, and we'll update on status on Sunday/Monday.
Thanks for the effort!
Regards,
Tariq
Re: [RFC / RFT net 2/7] tls: rx: strp: set the skb->len of detached / CoW'ed skbs
In reply to Jakub KicinskiQuoted 16 lines · Jakub Kicinski
alloc_skb_with_frags()fills in page frag sizes but does not setskb->lenandskb->data_len. Set those correctly otherwise device offload will most likely generate an empty skb and hit theBUG()at the end of__skb_nsg().Fixes 84c61fe1a75b ("tls: rx: do not use the standard strparser")Signed-off-by Jakub Kicinski <[email protected]>--- net/tls/tls_strp.c | 2 ++ 1 file changed, 2 insertions(+)net/tls/tls_strp.c +0
-0
net/tls/tls_strp.c static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp) · line 60
Quoted 5 lines
net/tls/tls_strp.c +2
-0@@ -56,6 +56,8 @@ static struct sk_buff *tls_strp_msg_make_copy(struct tls_strparser *strp) offset +=skb_frag_size (frag); } + skb->len = len; + skb->data_len = len;
len doesn't seem to exist until patch 5/7.
Quoted 7 lines
net/tls/tls_strp.c +0
-0skb_copy_header (skb, strp->anchor); rxm =strp_msg (skb); rxm->offset =0 ;Signature · 1 line
2.40.1
Tariq Toukan
Tested-by Shai AmiramQuoted 12 lines · Tariq Toukan
Quoted 8 lines · Jakub Kicinski
Tariq, here are the fixes for the bug you reported. I managed to test with mlx5 (and selftest, obviously). I hacked things up for testing to trigger the copy and reencrypt paths.
Could you run it thru your tests and LMK if there are any more regressions?
Quoted 9 lines
Hi Jakub,
Thanks for your patches!
I see that several changes were needed.I tested your series with the repro I had, it seems to be resolved.
We are going to run more intensive and comprehensive tests during the weekend, and we'll update on status on Sunday/Monday.
Hi Jakub,
Here's an updated testing status:
1. Reported issue is resolved.
2. All device-offload TLS RX/TX tests passed, except for the one issue below.
Nothing indicates that this issue is new or related directly to your fixes series. It might have been there for some time, hiding behind the existing bugs.
Issue description:
TlsDecryptError / TlsEncryptError increase when simultaneously creating a bond interface.
It doesn't happen each and every time. It reproduced several times in different runs.
The strange part is that the bond is created and attached to a different interface, not the one running the TLS traffic!
I think we should progress with the fixes:
Regards,
Tariq
Jakub Kicinski
maintainerQuoted 10 lines · On Tue, 16 May 2023 15:27:51 +0300 Tariq Toukan wrote:
Here's an updated testing status:
1. Reported issue is resolved.
2. All device-offload TLS RX/TX tests passed, except for the one issue below.Nothing indicates that this issue is new or related directly to your fixes series. It might have been there for some time, hiding behind the existing bugs.
Quoted 7 lines
Issue description:
TlsDecryptError / TlsEncryptError increase when simultaneously creating a bond interface.
It doesn't happen each and every time. It reproduced several times in different runs.
The strange part is that the bond is created and attached to a different interface, not the one running the TLS traffic!
Hm, that's pretty odd / scary. LMK if you hit a dead end, I hope it's not a memory corruption :S
Quoted 2 lines
I think we should progress with the fixes:
Tested-by Shai Amiram <[email protected]>
Thanks!