mptcp: update mptcp ack sequence outside of recv path
Subsystems (2)
Florian Westphal
cover mptcp: update mptcp ack sequence outside of recv path
Hi,
this is a rework of the earlier patch set that moved mptcp-level ack processing to work queue.
This is a slight variation of the same patchset, only that this time the update is done from the ssk data_ready notification directly, if possible.
I've split this into several steps to ease review, so patch #3 will move the ack processing to the work queue, then patch 6 will add the first 'avoid schedule_work()' scheme by attempting to handle the ack update/skb move to mptcp receive queue directly in the data_ready callback.
Last patch then moves the schedule_work to the mptcp socket release callback, where we can avoid the schedule in case last recv emptied all subflows (i.e., managed to move all skbs to the mptcp receive queue).
The work queue is still scheduled from time to time, but way less frequent than before.
When more work queue users get added (e.g. for retransmit support), the amount of times where the work queue is run without any work to be is almost at 0 for the entire mptcp selftest script invocation.
I've dropped 'mptcp: protocol: re-check dsn before reading from subflow', its not related to this work and not needed until multiple subflows get added and the
'mptcp: send acks when moving skbs to mptcp receive queue' change, which can be done as followup.
Florian Westphal
PATCH 1/8 mptcp: add and use mptcp_data_ready helper
allows us to schedule the work queue to drain the ssk receive queue in a followup patch.
This is needed to avoid sending all-to-pessimistic mptcp-level acknowledgements. At this time, the ack_seq is what was last read by userspace instead of the highest in-sequence number queued for reading.
--- net/mptcp/protocol.c | 8 ++++++++ net/mptcp/protocol.h | 1 + net/mptcp/subflow.c | 14 ++++---------- 3 files changed, 13 insertions(+), 10 deletions(-)
net/mptcp/protocol.c +8 -0
@@ -111,6 +111,14 @@ static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk)return NULL ; } +void mptcp_data_ready (struct sock *sk) +{ +struct mptcp_sock *msk =mptcp_sk (sk); + +set_bit (MPTCP_DATA_READY , &msk->flags); + sk->sk_data_ready (sk); +} +static bool mptcp_ext_cache_refill (struct mptcp_sock *msk) {if (!msk->cached_ext)
net/mptcp/protocol.h +1 -0
@@ -201,6 +201,7 @@ void mptcp_get_options(const struct sk_buff *skb, struct tcp_options_received *opt_rx);void mptcp_finish_connect (struct sock *sk); +void mptcp_data_ready (struct sock *sk);int mptcp_token_new_request (struct request_sock *req);void mptcp_token_destroy_request (u32 token);
net/mptcp/subflow.c +4 -10
@@ -554,11 +554,8 @@ static void subflow_data_ready(struct sock *sk)return ; } -if (mptcp_subflow_data_available (sk)) { -set_bit (MPTCP_DATA_READY , &mptcp_sk (parent)->flags); - - parent->sk_data_ready (parent); - } +if (mptcp_subflow_data_available (sk)) +mptcp_data_ready (parent); }static void subflow_write_space (struct sock *sk)
@@ -690,11 +687,8 @@ static void subflow_state_change(struct sock *sk)* a fin packet carrying a DSS can be unnoticed if we don't trigger * the data available machinery here. */ -if (parent && subflow->mp_capable &&mptcp_subflow_data_available (sk)) { -set_bit (MPTCP_DATA_READY , &mptcp_sk (parent)->flags); - - parent->sk_data_ready (parent); - } +if (parent && subflow->mp_capable &&mptcp_subflow_data_available (sk)) +mptcp_data_ready (parent);if (parent && !(parent->sk_shutdown &RCV_SHUTDOWN ) && !subflow->rx_eof &&subflow_is_done (sk)) {
Signature · 1 line
2.24.1
Florian Westphal
PATCH 2/8 mptcp: add work queue skeleton
In reply to Florian WestphalFrom: Paolo Abeni <pabeni(a)redhat.com>
Will be extended with functionality in followup patches. Initial user is moving skbs from subflows receive queue to the mptcp-level receive queue.
--- net/mptcp/protocol.c | 22 ++++++++++++++++++++++ net/mptcp/protocol.h | 1 + 2 files changed, 23 insertions(+)
net/mptcp/protocol.c +22 -0
@@ -551,12 +551,24 @@ static void __mptcp_close_ssk(struct sock *sk, struct sock *ssk, } } +static void mptcp_worker (struct work_struct *work) +{ + struct mptcp_sock *msk = container_of(work, struct mptcp_sock, work); +struct sock *sk = &msk->sk.icsk_inet.sk; + +lock_sock (sk); + +release_sock (sk); +sock_put (sk); +} +static int __mptcp_init_sock (struct sock *sk) {struct mptcp_sock *msk =mptcp_sk (sk);INIT_LIST_HEAD (&msk->conn_list);__set_bit (MPTCP_SEND_SPACE , &msk->flags); +INIT_WORK (&msk->work, mptcp_worker); msk->first =NULL ;
@@ -571,6 +583,14 @@ static int mptcp_init_sock(struct sock *sk)return __mptcp_init_sock (sk); } +static void mptcp_cancel_work (struct sock *sk) +{ +struct mptcp_sock *msk =mptcp_sk (sk); + +if (cancel_work_sync (&msk->work)) +sock_put (sk); +} +static void mptcp_subflow_shutdown (struct sock *ssk,int how) {lock_sock (ssk);
@@ -616,6 +636,8 @@ static void mptcp_close(struct sock *sk, long timeout)__mptcp_close_ssk (sk, ssk, subflow, timeout); } +mptcp_cancel_work (sk); +sk_common_release (sk); }
net/mptcp/protocol.h +1 -0
@@ -70,6 +70,7 @@ struct mptcp_sock {u32 token;unsigned long flags;bool can_ack; +struct work_struct work;struct list_head conn_list;struct skb_ext *cached_ext;/* for the next sendmsg */ struct socket *subflow;/* outgoing connect/listener/!mp_capable */
Signature · 1 line
2.24.1
Florian Westphal
PATCH 3/8 mptcp: update mptcp ack sequence from work queue
In reply to Florian WestphalIf userspace is not reading data, all the mptcp-level acks contain the ack_seq from the last time userspace read data rather than the most recent in-sequence value.
This causes pointless retransmissions for data that is already queued.
The reason for this is that all the mptcp protocol level processing happens at mptcp_recv time.
This adds work queue to move skbs from the subflow sockets receive queue on the mptcp socket receive queue (which was not used so far).
This allows us to announce the correct mptcp ack sequence in a timely fashion, even when the application does not call recv() on the mptcp socket for some time.
We still wake userspace tasks waiting for POLLIN immediately:
If the mptcp level receive queue is empty (because the work queue is still pending) it can be filled from in-sequence subflow sockets at recv time without a need to wait for the worker.
The skb_orphan when moving skbs from subflow to mptcp level is needed, because the destructor (sock_rfree) relies on skb->sk (ssk!) lock being taken.
A followup patch will add needed rmem accouting for the moved skbs.
Other problem: In case application behaves as expected, and calls recv() as soon as mptcp socket becomes readable, the work queue will only waste cpu cycles. This will also be addressed in followup patches.
--- net/mptcp/protocol.c | 233 ++++++++++++++++++++++++++++++------------- 1 file changed, 164 insertions(+), 69 deletions(-)
net/mptcp/protocol.c +164 -69
@@ -31,6 +31,12 @@ struct mptcp6_sock { };#endif +struct mptcp_skb_cb { +u32 offset; +}; + +#define MPTCP_SKB_CB (__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) +/* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not * completed yet or has failed, return the subflow socket. * Otherwise return NULL.
@@ -111,12 +117,88 @@ static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk)return NULL ; } +static void __mptcp_move_skb (struct mptcp_sock *msk,struct sock *ssk, +struct sk_buff *skb, +unsigned int offset,size_t copy_len) +{ +struct sock *sk = (struct sock *)msk; + +__skb_unlink (skb, &ssk->sk_receive_queue); +skb_orphan (skb); +__skb_queue_tail (&sk->sk_receive_queue, skb); + + msk->ack_seq += copy_len; +MPTCP_SKB_CB (skb)->offset = offset; +} + +static bool __mptcp_move_skbs_from_subflow (struct mptcp_sock *msk, +struct sock *ssk, +unsigned int *bytes) +{ +struct mptcp_subflow_context *subflow =mptcp_subflow_ctx (ssk); +unsigned int moved =0 ; +bool more_data_avail; +struct tcp_sock *tp; +bool done = false; + + tp =tcp_sk (ssk); +do { +u32 map_remaining, offset; +u32 seq = tp->copied_seq; +struct sk_buff *skb; +bool fin; + +/* try to move as much data as available */ + map_remaining = subflow->map_data_len - +mptcp_subflow_get_map_offset (subflow); + + skb =skb_peek (&ssk->sk_receive_queue); +if (!skb) +break ; + + offset = seq -TCP_SKB_CB (skb)->seq; + fin =TCP_SKB_CB (skb)->tcp_flags &TCPHDR_FIN ; +if (fin) { + done = true; + seq++; + } + +if (offset < skb->len) { +size_t len = skb->len - offset; + +if (tp->urg_data) + done = true; + +__mptcp_move_skb (msk, ssk, skb, offset, len); + seq += len; + moved += len; + +if (WARN_ON_ONCE (map_remaining < len)) +break ; + }else { +WARN_ON_ONCE (!fin); +sk_eat_skb (ssk, skb); + done = true; + } + +WRITE_ONCE (tp->copied_seq, seq); + more_data_avail =mptcp_subflow_data_available (ssk); + }while (more_data_avail); + + *bytes = moved; + +return done; +} +void mptcp_data_ready (struct sock *sk) {struct mptcp_sock *msk =mptcp_sk (sk);set_bit (MPTCP_DATA_READY , &msk->flags); sk->sk_data_ready (sk); + +if (schedule_work (&msk->work)) +sock_hold ((struct sock *)msk); }static bool mptcp_ext_cache_refill (struct mptcp_sock *msk)
@@ -373,19 +455,68 @@ static void mptcp_wait_data(struct sock *sk, long *timeo)remove_wait_queue (sk_sleep (sk), &wait); } +static int __mptcp_recvmsg_mskq (struct mptcp_sock *msk, +struct msghdr *msg, +size_t len) +{ +struct sock *sk = (struct sock *)msk; +struct sk_buff *skb; +int copied =0 ; + +while ((skb =skb_peek (&sk->sk_receive_queue)) !=NULL ) { +u32 offset =MPTCP_SKB_CB (skb)->offset; +u32 data_len = skb->len - offset; +u32 count =min_t (size_t, len - copied, data_len); +int err; + + err =skb_copy_datagram_msg (skb, offset, msg, count); +if (unlikely (err <0 )) { +if (!copied) +return err; +break ; + } + + copied += count; + +if (count < data_len) { +MPTCP_SKB_CB (skb)->offset += count; +break ; + } + +__skb_unlink (skb, &sk->sk_receive_queue); +__kfree_skb (skb); + +if (copied >= len) +break ; + } + +return copied; +} + +static bool __mptcp_move_skbs (struct mptcp_sock *msk) +{ +unsigned int moved =0 ; +bool done; + +do { +struct sock *ssk =mptcp_subflow_recv_lookup (msk); + +if (!ssk) +break ; + +lock_sock (ssk); + done =__mptcp_move_skbs_from_subflow (msk, ssk, &moved); +release_sock (ssk); + }while (!done); + +return moved >0 ; +} +static int mptcp_recvmsg (struct sock *sk,struct msghdr *msg,size_t len,int nonblock,int flags,int *addr_len) {struct mptcp_sock *msk =mptcp_sk (sk); -struct mptcp_subflow_context *subflow; -bool more_data_avail = false; -struct mptcp_read_arg arg; -read_descriptor_t desc; -bool wait_data = false;struct socket *ssock; -struct tcp_sock *tp; -bool done = false; -struct sock *ssk;int copied =0 ;int target;long timeo;
@@ -403,65 +534,26 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,return copied; } - arg.msg = msg; - desc.arg.data = &arg; - desc.error =0 ; - timeo =sock_rcvtimeo (sk, nonblock); len =min_t (size_t, len,INT_MAX ); target =sock_rcvlowat (sk, flags &MSG_WAITALL , len); -while (!done) { -u32 map_remaining; +while (len > (size_t )copied) {int bytes_read; - ssk =mptcp_subflow_recv_lookup (msk); -pr_debug ("msk=%p ssk=%p" , msk, ssk); -if (!ssk) - goto wait_for_data; + bytes_read =__mptcp_recvmsg_mskq (msk, msg, len - copied); +if (unlikely (bytes_read <0 )) { +if (!copied) + copied = bytes_read; + goto out_err; + } - subflow =mptcp_subflow_ctx (ssk); - tp =tcp_sk (ssk); + copied += bytes_read; -lock_sock (ssk); -do { -/* try to read as much data as available */ - map_remaining = subflow->map_data_len - -mptcp_subflow_get_map_offset (subflow); - desc.count =min_t (size_t, len - copied, map_remaining); -pr_debug ("reading %zu bytes, copied %d" , desc.count, - copied); - bytes_read =tcp_read_sock (ssk, &desc, - mptcp_read_actor); -if (bytes_read <0 ) { -if (!copied) - copied = bytes_read; - done = true; - goto next; - } - -pr_debug ("msk ack_seq=%llx -> %llx" , msk->ack_seq, - msk->ack_seq + bytes_read); - msk->ack_seq += bytes_read; - copied += bytes_read; -if (copied >= len) { - done = true; - goto next; - } -if (tp->urg_data && tp->urg_seq == tp->copied_seq) { -pr_err ("Urgent data present, cannot proceed" ); - done = true; - goto next; - } -next: - more_data_avail =mptcp_subflow_data_available (ssk); - }while (more_data_avail && !done); -release_sock (ssk); -continue ; - -wait_for_data: - more_data_avail = false; +if (skb_queue_empty (&sk->sk_receive_queue) && +__mptcp_move_skbs (msk)) +continue ;/* only the master socket status is relevant here. The exit * conditions mirror closely tcp_recvmsg()
@@ -502,26 +594,25 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, }pr_debug ("block timeout %ld" , timeo); - wait_data = true;mptcp_wait_data (sk, &timeo);if (unlikely (__mptcp_tcp_fallback (msk))) goto fallback; } -if (more_data_avail) { -if (!test_bit (MPTCP_DATA_READY , &msk->flags)) -set_bit (MPTCP_DATA_READY , &msk->flags); - }else if (!wait_data) { +if (skb_queue_empty (&sk->sk_receive_queue)) { +/* entire backlog drained, clear DATA_READY. */ clear_bit (MPTCP_DATA_READY , &msk->flags); -/* .. race-breaker: ssk might get new data after last -* data_available() returns false. +/* .. race-breaker: ssk might have gotten new data +* after last __mptcp_move_skbs() returned false. */ - ssk =mptcp_subflow_recv_lookup (msk); -if (unlikely (ssk)) +if (unlikely (__mptcp_move_skbs (msk)))set_bit (MPTCP_DATA_READY , &msk->flags); + }else if (unlikely (!test_bit (MPTCP_DATA_READY , &msk->flags))) { +/* data to read but mptcp_wait_data() cleared DATA_READY */ +set_bit (MPTCP_DATA_READY , &msk->flags); } - +out_err:release_sock (sk);return copied; }
@@ -557,7 +648,7 @@ static void mptcp_worker(struct work_struct *work)struct sock *sk = &msk->sk.icsk_inet.sk;lock_sock (sk); - +__mptcp_move_skbs (msk);release_sock (sk);sock_put (sk); }
@@ -638,6 +729,8 @@ static void mptcp_close(struct sock *sk, long timeout)mptcp_cancel_work (sk); +__skb_queue_purge (&sk->sk_receive_queue); +sk_common_release (sk); }
@@ -1204,6 +1297,8 @@ void mptcp_proto_init(void)panic ("Failed to register MPTCP proto.\n" );inet_register_protosw (&mptcp_protosw); + + BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); }#if IS_ENABLED (CONFIG_MPTCP_IPV6 )
Signature · 1 line
2.24.1
Paolo Abeni
[MPTCP] Re: [PATCH net-next 3/8] mptcp: update mptcp ack sequence from work queue
net/mptcp/protocol.c static void mptcp_wait_data(struct sock *sk, long *timeo) · line 507
Quoted 12 lines · Florian Westphal
net/mptcp/protocol.c +12
-0+static bool __mptcp_move_skbs (struct mptcp_sock *msk) +{ +unsigned int moved =0 ; +bool done; + + do { + struct sock *ssk = mptcp_subflow_recv_lookup(msk); + + if (!ssk) + break; + + lock_sock(ssk);
I'm wondering if bh_lock_sock(ssk) would suffice here?
If so we could do the skbs move from mptcp_release_cb() in patch 8.
But it looks like the full lock_sock() is required by the skb_orphan() call, which in turn modifies ssk->sk_forward_alloc, so no luck right?
Thanks!
Paolo
Florian Westphal
[MPTCP] Re: [PATCH net-next 3/8] mptcp: update mptcp ack sequence from work queue
Quoted 10 lines · Paolo Abeni
net/mptcp/protocol.c static void mptcp_wait_data(struct sock *sk, long *timeo) · line 507
Quoted 7 lines
net/mptcp/protocol.c +7
-0+ do { + struct sock *ssk = mptcp_subflow_recv_lookup(msk); + + if (!ssk) + break; + + lock_sock(ssk);I'm wondering if
bh_lock_sock(ssk)would suffice here?
Quoted 4 lines
If so we could do the skbs move from
mptcp_release_cb()in patch 8.But it looks like the full
lock_sock()is required by theskb_orphan()call, which in turn modifiesssk->sk_forward_alloc, so no luck right?
Yep, the destructor of the skbs on the ssk rx queue assumes its called with the ssk owned.
We could of course do bh_lock+then check 'owned' but I'm not sure its worth it.
I'd suggest to keep the 'direct move from release_cb' in mind as something we could try to do later.
When I tested an older version of this patch set on top of mptcp-next then the number of times the work queue ran with nothing todo (no data in any subflow, no retransmission pending, nothing to reclaim from rtx queue etc. was really small -- a low 3-digit number for entire mptcp selftest run iirc. (with poll).
Paolo Abeni
[MPTCP] Re: [PATCH net-next 3/8] mptcp: update mptcp ack sequence from work queue
Quoted 13 lines · Florian Westphal
Quoted 12 lines · Paolo Abeni
net/mptcp/protocol.c static void mptcp_wait_data(struct sock *sk, long *timeo) · line 507
Quoted 7 lines
net/mptcp/protocol.c +7
-0+ do { + struct sock *ssk = mptcp_subflow_recv_lookup(msk); + + if (!ssk) + break; + + lock_sock(ssk);I'm wondering if
bh_lock_sock(ssk)would suffice here?If so we could do the skbs move from
mptcp_release_cb()in patch 8.
Quoted 8 lines
Quoted 2 lines
But it looks like the full
lock_sock()is required by theskb_orphan()call, which in turn modifiesssk->sk_forward_alloc, so no luck right?Yep, the destructor of the skbs on the ssk rx queue assumes its called with the ssk owned.
We could of course do bh_lock+then check 'owned' but I'm not sure its worth it.
Yup, also I think is not trivial take the correct delayed action with multiple subflows.
Quoted 2 lines
I'd suggest to keep the 'direct move from
release_cb' in mind as something we could try to do later.
Agreed! definitely not needed now - and possibly even not needed later, given the complexity.
Thanks,
Paolo
Florian Westphal
PATCH 4/8 mptcp: add rmem queue accounting
In reply to Florian WestphalIf userspace never drains the receive buffers we must stop draining the subflow socket(s) at some point.
This adds the needed rmem accouting for this.
If the threshold is reached, we stop draining the subflows.
--- net/mptcp/protocol.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +16 -1
@@ -124,7 +124,7 @@ static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,struct sock *sk = (struct sock *)msk;__skb_unlink (skb, &ssk->sk_receive_queue); -skb_orphan (skb); +skb_set_owner_r (skb, sk);__skb_queue_tail (&sk->sk_receive_queue, skb); msk->ack_seq += copy_len;
@@ -136,10 +136,16 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, unsigned int *bytes) {struct mptcp_subflow_context *subflow =mptcp_subflow_ctx (ssk); +struct sock *sk = (struct sock *)msk;unsigned int moved =0 ;bool more_data_avail;struct tcp_sock *tp;bool done = false; +int rcvbuf; + + rcvbuf =max (ssk->sk_rcvbuf, sk->sk_rcvbuf); +if (rcvbuf > sk->sk_rcvbuf) + sk->sk_rcvbuf = rcvbuf; tp =tcp_sk (ssk); do {
@@ -183,6 +189,11 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,WRITE_ONCE (tp->copied_seq, seq); more_data_avail =mptcp_subflow_data_available (ssk); + +if (atomic_read (&sk->sk_rmem_alloc) >READ_ONCE (sk->sk_rcvbuf)) { + done = true; +break ; + } }while (more_data_avail); *bytes = moved;
@@ -197,6 +208,10 @@ void mptcp_data_ready(struct sock *sk)set_bit (MPTCP_DATA_READY , &msk->flags); sk->sk_data_ready (sk); +/* don't schedule if mptcp sk is (still) over limit */ +if (atomic_read (&sk->sk_rmem_alloc) >READ_ONCE (sk->sk_rcvbuf)) +return ; +if (schedule_work (&msk->work))sock_hold ((struct sock *)msk); }
Signature · 1 line
2.24.1
Florian Westphal
PATCH 5/8 mptcp: remove mptcp_read_actor
In reply to Florian WestphalOnly used to discard stale data from the subflow, so move it where needed.
--- net/mptcp/protocol.c | 27 --------------------------- net/mptcp/protocol.h | 7 ------- net/mptcp/subflow.c | 18 +++++++++++++----- 3 files changed, 13 insertions(+), 39 deletions(-)
net/mptcp/protocol.c +0 -27
@@ -428,33 +428,6 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)return ret; } -int mptcp_read_actor (read_descriptor_t *desc,struct sk_buff *skb, -unsigned int offset,size_t len) -{ -struct mptcp_read_arg *arg = desc->arg.data; -size_t copy_len; - - copy_len =min (desc->count, len); - -if (likely (arg->msg)) { -int err; - - err =skb_copy_datagram_msg (skb, offset, arg->msg, copy_len); -if (err) { -pr_debug ("error path" ); - desc->error = err; -return err; - } - }else { -pr_debug ("Flushing skb payload" ); - } - - desc->count -= copy_len; - -pr_debug ("consumed %zu bytes, %zu left" , copy_len, desc->count); -return copy_len; -} -static void mptcp_wait_data (struct sock *sk,long *timeo) {DEFINE_WAIT_FUNC (wait, woken_wake_function);
net/mptcp/protocol.h +0 -7
@@ -191,13 +191,6 @@ void mptcp_proto_init(void);int mptcp_proto_v6_init (void );#endif -struct mptcp_read_arg { -struct msghdr *msg; -}; - -int mptcp_read_actor (read_descriptor_t *desc,struct sk_buff *skb, -unsigned int offset,size_t len); -void mptcp_get_options (const struct sk_buff *skb,struct tcp_options_received *opt_rx);
net/mptcp/subflow.c +13 -5
@@ -408,6 +408,18 @@ static enum mapping_status get_mapping_status(struct sock *ssk)return MAPPING_OK ; } +static int subflow_read_actor (read_descriptor_t *desc, +struct sk_buff *skb, +unsigned int offset,size_t len) +{ +size_t copy_len =min (desc->count, len); + + desc->count -= copy_len; + +pr_debug ("flushed %zu bytes, %zu left" , copy_len, desc->count); +return copy_len; +} +static bool subflow_check_data_avail (struct sock *ssk) {struct mptcp_subflow_context *subflow =mptcp_subflow_ctx (ssk);
@@ -482,16 +494,12 @@ static bool subflow_check_data_avail(struct sock *ssk)pr_debug ("discarding %zu bytes, current map len=%d" , delta, map_remaining);if (delta) { -struct mptcp_read_arg arg = { - .msg =NULL , - };read_descriptor_t desc = { .count = delta, - .arg.data = &arg, };int ret; - ret =tcp_read_sock (ssk, &desc, mptcp_read_actor); + ret =tcp_read_sock (ssk, &desc, subflow_read_actor);if (ret <0 ) { ssk->sk_err = -ret; goto fatal;
Signature · 1 line
2.24.1
Florian Westphal
PATCH 6/8 mptcp: avoid work queue scheduling if possible
In reply to Florian WestphalWe can't grab the mptcp socket lock from the subflow data_ready callback, it would result in ABBA deadlock.
But we can try to grab the mptcp lock.
If we can't, we can defer to work queue.
If the lock succeeds however we can avoid the schedule_work() call and move all in-sequence skbs to the mptcp receive queue.
--- net/mptcp/protocol.c | 30 +++++++++++++++++++++++++++++- net/mptcp/protocol.h | 2 +- net/mptcp/subflow.c | 4 ++-- 3 files changed, 32 insertions(+), 4 deletions(-)
net/mptcp/protocol.c +29 -1
@@ -201,17 +201,45 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,return done; } -void mptcp_data_ready (struct sock *sk) +/* In most cases we will be able to lock the mptcp socket. If its already +* owned, we need to defer to the work queue to avoid ABBA deadlock. +*/ +static bool move_skbs_to_msk (struct mptcp_sock *msk,struct sock *ssk) +{ +struct sock *sk = (struct sock *)msk; +unsigned int moved =0 ; + +if (sock_owned_by_user_nocheck (sk)) +return false; + +if (unlikely (!spin_trylock_bh (&sk->sk_lock.slock))) +return false; + +/* must re-check after taking the lock */ +if (!sock_owned_by_user_nocheck (sk)) +__mptcp_move_skbs_from_subflow (msk, ssk, &moved); + +spin_unlock_bh (&sk->sk_lock.slock); + +return skb_queue_empty (&ssk->sk_receive_queue); +} + +void mptcp_data_ready (struct sock *sk,struct sock *ssk) {struct mptcp_sock *msk =mptcp_sk (sk);set_bit (MPTCP_DATA_READY , &msk->flags); sk->sk_data_ready (sk); +if (atomic_read (&sk->sk_rmem_alloc) <READ_ONCE (sk->sk_rcvbuf) && +move_skbs_to_msk (msk, ssk)) +return ; +/* don't schedule if mptcp sk is (still) over limit */ if (atomic_read (&sk->sk_rmem_alloc) >READ_ONCE (sk->sk_rcvbuf))return ; +/* mptcp socket is busy, schedule worker */ if (schedule_work (&msk->work))sock_hold ((struct sock *)msk); }
net/mptcp/protocol.h +1 -1
@@ -195,7 +195,7 @@ void mptcp_get_options(const struct sk_buff *skb, struct tcp_options_received *opt_rx);void mptcp_finish_connect (struct sock *sk); -void mptcp_data_ready (struct sock *sk); +void mptcp_data_ready (struct sock *sk,struct sock *ssk);int mptcp_token_new_request (struct request_sock *req);void mptcp_token_destroy_request (u32 token);
net/mptcp/subflow.c +2 -2
@@ -563,7 +563,7 @@ static void subflow_data_ready(struct sock *sk) }if (mptcp_subflow_data_available (sk)) -mptcp_data_ready (parent); +mptcp_data_ready (parent, sk); }static void subflow_write_space (struct sock *sk)
@@ -696,7 +696,7 @@ static void subflow_state_change(struct sock *sk)* the data available machinery here. */ if (parent && subflow->mp_capable &&mptcp_subflow_data_available (sk)) -mptcp_data_ready (parent); +mptcp_data_ready (parent, sk);if (parent && !(parent->sk_shutdown &RCV_SHUTDOWN ) && !subflow->rx_eof &&subflow_is_done (sk)) {
Signature · 1 line
2.24.1
Paolo Abeni
[MPTCP] Re: [PATCH net-next 6/8] mptcp: avoid work queue scheduling if possible
Quoted 30 lines · Florian Westphal
We can't grab the mptcp socket lock from the subflow
data_readycallback, it would result in ABBA deadlock.But we can try to grab the mptcp lock.
If we can't, we can defer to work queue.
If the lock succeeds however we can avoid theschedule_work()call and move all in-sequence skbs to the mptcp receive queue.Signed-off-by Florian Westphal <fw(a)strlen.de>--- net/mptcp/protocol.c | 30 +++++++++++++++++++++++++++++- net/mptcp/protocol.h | 2 +- net/mptcp/subflow.c | 4 ++-- 3 files changed, 32 insertions(+), 4 deletions(-)net/mptcp/protocol.c +6
-1@@ -201,17 +201,45 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,return done; } -void mptcp_data_ready (struct sock *sk) +/* In most cases we will be able to lock the mptcp socket. If its already +* owned, we need to defer to the work queue to avoid ABBA deadlock. +*/ +static bool move_skbs_to_msk (struct mptcp_sock *msk,struct sock *ssk) +{ +struct sock *sk = (struct sock *)msk;
net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 219
Quoted 10 lines
net/mptcp/protocol.c +10
-0+unsigned int moved =0 ; + +if (sock_owned_by_user_nocheck (sk)) +return false; + +if (unlikely (!spin_trylock_bh (&sk->sk_lock.slock))) +return false; + +/* must re-check after taking the lock */ +if (!sock_owned_by_user_nocheck (sk))
Don't we additionally need a READ_ONCE() here and above ? Otherwise the compiler and/or CPU could merge the read?
AFAICS sock_owned_by_user_nocheck() does not include the additional annotation.
I think we could simply drop the first check (and always do the trylock)
Cheers,
Paolo
Florian Westphal
[MPTCP] Re: [PATCH net-next 6/8] mptcp: avoid work queue scheduling if possible
Quoted 11 lines · Paolo Abeni
Quoted 11 lines
net/mptcp/protocol.c +11
-0+/* In most cases we will be able to lock the mptcp socket. If its already +* owned, we need to defer to the work queue to avoid ABBA deadlock. +*/ +static bool move_skbs_to_msk (struct mptcp_sock *msk,struct sock *ssk) +{ +struct sock *sk = (struct sock *)msk; +unsigned int moved =0 ; + +if (sock_owned_by_user_nocheck (sk)) +return false; +
Quoted 8 lines
net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 219
Quoted 5 lines
net/mptcp/protocol.c +5
-0+if (unlikely (!spin_trylock_bh (&sk->sk_lock.slock))) +return false; + +/* must re-check after taking the lock */ +if (!sock_owned_by_user_nocheck (sk))Don't we additionally need a
READ_ONCE()here and above ? Otherwise the compiler and/or CPU could merge the read?
Oh, right, at least first check needs READ_ONCE annotation, thanks.
Quoted 5 lines
AFAICS
sock_owned_by_user_nocheck()does not include the additional annotation.I think we could simply drop the first check (and always do the trylock)
The trylock would succeed however when socket is owned, leading to a uneeded lock operation.
Florian Westphal
[MPTCP] Re: [PATCH net-next 6/8] mptcp: avoid work queue scheduling if possible
In reply to Florian Westphalnet/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 237
Quoted 11 lines · Florian Westphal
net/mptcp/protocol.c +5
-0+void mptcp_data_ready (struct sock *sk,struct sock *ssk) {struct mptcp_sock *msk =mptcp_sk (sk);set_bit (MPTCP_DATA_READY , &msk->flags); sk->sk_data_ready (sk); +if (atomic_read (&sk->sk_rmem_alloc) <READ_ONCE (sk->sk_rcvbuf) && +move_skbs_to_msk (msk, ssk)) +return ; +
It just occured to me that it might be better to move the sk->sk_data_ready(sk); call to the end of mptcp_data_ready(), I would do this for next iteration.
Florian Westphal
PATCH 7/8 mptcp: add mptcp_release_cb skeleton
In reply to Florian WestphalFrom: Paolo Abeni <pabeni(a)redhat.com>
It will be used to check if we still have a subflow with pending data.
--- net/mptcp/protocol.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
net/mptcp/protocol.c +20 -0
@@ -940,6 +940,25 @@ static int mptcp_getsockopt(struct sock *sk, int level, int optname,return -EOPNOTSUPP ; } +#define MPTCP_DEFERRED_ALL TCPF_DELACK_TIMER_DEFERRED + +/* this is very alike tcp_release_cb() but we must handle differently a +* different set of events +*/ +static void mptcp_release_cb (struct sock *sk) +{ +unsigned long flags, nflags; + +do { + flags = sk->sk_tsq_flags; +if (!(flags &MPTCP_DEFERRED_ALL )) +return ; + nflags = flags & ~MPTCP_DEFERRED_ALL ; + }while (cmpxchg (&sk->sk_tsq_flags, flags, nflags) != flags); + +sock_release_ownership (sk); +} +static int mptcp_get_port (struct sock *sk,unsigned short snum) {struct mptcp_sock *msk =mptcp_sk (sk);
@@ -1015,6 +1034,7 @@ static struct proto mptcp_prot = { .destroy = mptcp_destroy, .sendmsg = mptcp_sendmsg, .recvmsg = mptcp_recvmsg, + .release_cb = mptcp_release_cb, .hash = inet_hash, .unhash = inet_unhash, .get_port = mptcp_get_port,
Signature · 1 line
2.24.1
Florian Westphal
PATCH 8/8 mptcp: defer work schedule until mptcp lock is released
In reply to Florian WestphalDon't schedule the work queue right away, instead defer this to the lock release callback.
This has the advantage that it will give recv path a chance to complete -- this might have moved all pending packets from the subflow to the mptcp receive queue, which allows to avoid another schedule_work().
--- net/mptcp/protocol.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +18 -2
@@ -240,8 +240,15 @@ void mptcp_data_ready(struct sock *sk, struct sock *ssk)return ;/* mptcp socket is busy, schedule worker */ -if (schedule_work (&msk->work)) -sock_hold ((struct sock *)msk); +if (!test_and_set_bit (TCP_DELACK_TIMER_DEFERRED , + &sk->sk_tsq_flags)) { +sock_hold (sk); + +/* need to try again, its possible release_cb() has already +* been called after the test_and_set_bit() above. +*/ +move_skbs_to_msk (msk, ssk); + } }static bool mptcp_ext_cache_refill (struct mptcp_sock *msk)
@@ -957,6 +964,15 @@ static void mptcp_release_cb(struct sock *sk) }while (cmpxchg (&sk->sk_tsq_flags, flags, nflags) != flags);sock_release_ownership (sk); + +if (flags &TCPF_DELACK_TIMER_DEFERRED ) { +struct mptcp_sock *msk =mptcp_sk (sk); +struct sock *ssk; + + ssk =mptcp_subflow_recv_lookup (msk); +if (!ssk || !schedule_work (&msk->work)) +__sock_put (sk); + } }static int mptcp_get_port (struct sock *sk,unsigned short snum)
Signature · 1 line
2.24.1
Paolo Abeni
[MPTCP] Re: [PATCH net-next 8/8] mptcp: defer work schedule until mptcp lock is released
Quoted 18 lines · Florian Westphal
Don't schedule the work queue right away, instead defer this to the lock release callback.
This has the advantage that it will give recv path a chance to complete -- this might have moved all pending packets from the subflow to the mptcp receive queue, which allows to avoid another
schedule_work().Signed-off-by Florian Westphal <fw(a)strlen.de>--- net/mptcp/protocol.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)net/mptcp/protocol.c +0
-0
net/mptcp/protocol.c void mptcp_data_ready(struct sock *sk, struct sock *ssk) · line 245
Quoted 8 lines
net/mptcp/protocol.c +3
-2@@ -240,8 +240,15 @@ void mptcp_data_ready(struct sock *sk, struct sock *ssk)return ;/* mptcp socket is busy, schedule worker */ -if (schedule_work (&msk->work)) -sock_hold ((struct sock *)msk); +if (!test_and_set_bit (TCP_DELACK_TIMER_DEFERRED , + &sk->sk_tsq_flags)) { +sock_hold (sk);
Here is the msk/sk socket lock always held by some other process?
I think the trylock in patch 6 from different subflows can collide with each other, so a try lock can fail even with !sk->sk_lock.owned
Or the process owning the msk sock lock when the previous check has been done can release it in the meanwhile.
In one of the above happens, can the TCP_DELACK_TIMER_DEFERRED delayed for a long time? Do we need to address that scenario?
Thanks,
Paolo
Florian Westphal
[MPTCP] Re: [PATCH net-next 8/8] mptcp: defer work schedule until mptcp lock is released
Quoted 21 lines · Paolo Abeni
Quoted 20 lines · Florian Westphal
Don't schedule the work queue right away, instead defer this to the lock release callback.
This has the advantage that it will give recv path a chance to complete -- this might have moved all pending packets from the subflow to the mptcp receive queue, which allows to avoid another
schedule_work().Signed-off-by Florian Westphal <fw(a)strlen.de>--- net/mptcp/protocol.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)net/mptcp/protocol.c +0
-0@@ -240,8 +240,15 @@ void mptcp_data_ready(struct sock *sk, struct sock *ssk)return ;
Quoted 8 lines
net/mptcp/protocol.c void mptcp_data_ready(struct sock *sk, struct sock *ssk) · line 245
Quoted 6 lines
net/mptcp/protocol.c +3
-2/* mptcp socket is busy, schedule worker */ -if (schedule_work (&msk->work)) -sock_hold ((struct sock *)msk); +if (!test_and_set_bit (TCP_DELACK_TIMER_DEFERRED , + &sk->sk_tsq_flags)) { +sock_hold (sk);Here is the msk/sk socket lock always held by some other process?
Yes, why?
Quoted 2 lines
I think the trylock in patch 6 from different subflows can collide with each other, so a try lock can fail even with !sk->sk_lock.owned
Not following, sorry.
Quoted 2 lines
Or the process owning the msk sock lock when the previous check has been done can release it in the meanwhile.
Yes, why?
Quoted 2 lines
In one of the above happens, can the
TCP_DELACK_TIMER_DEFERREDdelayed for a long time? Do we need to address that scenario?
Hmm:
/* mptcp socket is busy, schedule worker */
if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED,
&sk->sk_tsq_flags)) {
sock_hold(sk);
/* need to try again, its possible release_cb() has already
* been called after the test_and_set_bit() above.
*/
move_skbs_to_msk(msk, ssk);
}I thought the last move_skbs_to_msk() would address that? (If it fails, msk is still owned so release cb will be called 'soon').
Paolo Abeni
[MPTCP] Re: [PATCH net-next 8/8] mptcp: defer work schedule until mptcp lock is released
Quoted 31 lines · Florian Westphal
Quoted 21 lines · Paolo Abeni
Quoted 20 lines · Florian Westphal
Don't schedule the work queue right away, instead defer this to the lock release callback.
This has the advantage that it will give recv path a chance to complete -- this might have moved all pending packets from the subflow to the mptcp receive queue, which allows to avoid another
schedule_work().Signed-off-by Florian Westphal <fw(a)strlen.de>--- net/mptcp/protocol.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)net/mptcp/protocol.c +0
-0@@ -240,8 +240,15 @@ void mptcp_data_ready(struct sock *sk, struct sock *ssk)return ;Quoted 8 lines
net/mptcp/protocol.c void mptcp_data_ready(struct sock *sk, struct sock *ssk) · line 245
Quoted 6 lines
net/mptcp/protocol.c +3
-2/* mptcp socket is busy, schedule worker */ -if (schedule_work (&msk->work)) -sock_hold ((struct sock *)msk); +if (!test_and_set_bit (TCP_DELACK_TIMER_DEFERRED , + &sk->sk_tsq_flags)) { +sock_hold (sk);Here is the msk/sk socket lock always held by some other process?
Quoted 6 lines
Yes, why?
Quoted 2 lines
I think the trylock in patch 6 from different subflows can collide with each other, so a try lock can fail even with !sk->sk_lock.owned
Not following, sorry.
I mean something alike
CPU0 (subflow 0) CPU1 (subflow 1)
trylock trylock
// enqueue the // lock busy
// data
test_and_set_bit(TCP_DELACK_TIMER_DEFERRED)
but nobody will call mptcp_release_cb() soon.
Quoted 20 lines
Quoted 2 lines
Or the process owning the msk sock lock when the previous check has been done can release it in the meanwhile.
Yes, why?
Quoted 2 lines
In one of the above happens, can the
TCP_DELACK_TIMER_DEFERREDdelayed for a long time? Do we need to address that scenario?Hmm:
/* mptcp socket is busy, schedule worker */ if (!test_and_set_bit(TCP_DELACK_TIMER_DEFERRED, &sk->sk_tsq_flags)) { sock_hold(sk); /* need to try again, its possible release_cb() has already * been called after the test_and_set_bit() above. */ move_skbs_to_msk(msk, ssk); }
Quoted 2 lines
I thought the last
move_skbs_to_msk()would address that? (If it fails, msk is still owned so release cb will be called 'soon').
Thank for pointing this out! I underlooked this part. It looks like the 2nd scenario I feared is not possible!
I'm unsure about the first one ?!? can the move_skbs_to_msk() above still race with another subflows ?!?
Thanks,
Paolo
Florian Westphal
[MPTCP] Re: [PATCH net-next 8/8] mptcp: defer work schedule until mptcp lock is released
Quoted 9 lines · Paolo Abeni
Quoted 5 lines · Florian Westphal
Quoted 2 lines · Paolo Abeni
I think the trylock in patch 6 from different subflows can collide with each other, so a try lock can fail even with !sk->sk_lock.owned
Not following, sorry.
I mean something alike
Quoted 7 lines
CPU0 (subflow 0) CPU1 (subflow 1)
trylock trylock
// enqueue the // lock busy
// data
test_and_set_bit(TCP_DELACK_TIMER_DEFERRED)but nobody will call
mptcp_release_cb()soon.
This would only happen if both subflows have in-sequence data at the time they enter this function.
Even if it happens, then:
1. We enqueued new data (mptcp->ack_seq update)
2. We unblock/notify userspace there is new data to read
If userspaces goes sleep(3600);
then its possible that we leave 'unacked' data behind in case the 'losing' subflow had more data than that.
If thats a concern, the fix is simple:
get rid of TCP_DELACK_TIMER_DEFERRED/release_cb and schedule work directly.
I will do that in next series.
Paolo Abeni
[MPTCP] Re: [PATCH net-next 8/8] mptcp: defer work schedule until mptcp lock is released
Quoted 21 lines · Florian Westphal
Quoted 9 lines · Paolo Abeni
Quoted 5 lines · Florian Westphal
Quoted 2 lines · Paolo Abeni
I think the trylock in patch 6 from different subflows can collide with each other, so a try lock can fail even with !sk->sk_lock.owned
Not following, sorry.
I mean something alike
Quoted 7 lines
CPU0 (subflow 0) CPU1 (subflow 1)
trylock trylock
// enqueue the // lock busy
// data
test_and_set_bit(TCP_DELACK_TIMER_DEFERRED)but nobody will call
mptcp_release_cb()soon.This would only happen if both subflows have in-sequence data at the time they enter this function.
Quoted 8 lines
Even if it happens, then:
1. We enqueued new data (mptcp->ack_sequpdate)
2. We unblock/notify userspace there is new data to readIf userspaces goes sleep(3600);
then its possible that we leave 'unacked' data behind in case the 'losing' subflow had more data than that.
Yep, I see. This looks like a non existing issue.
I think the posted code is fine.
Thanks for the explaination.
Paolo