Skip to content

Commit

Permalink
mptcp: avoid work queue scheduling if possible
Browse files Browse the repository at this point in the history
We can't lock_sock() the mptcp socket from the subflow data_ready callback,
it would result in ABBA deadlock with the subflow socket lock.

We can however grab the spinlock: if that succeeds and the mptcp socket
is not owned at the moment, we can process the new skbs right away
without deferring this to the work queue.

This avoids the schedule_work and hence the small delay until the
work item is processed.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Mat Martineau <mathew.j.martineau@linux.intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Florian Westphal authored and David S. Miller committed Feb 27, 2020
1 parent bfae9da commit 2e52213
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
29 changes: 28 additions & 1 deletion net/mptcp/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,39 @@ 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 (READ_ONCE(sk->sk_lock.owned))
return false;

if (unlikely(!spin_trylock_bh(&sk->sk_lock.slock)))
return false;

/* must re-check after taking the lock */
if (!READ_ONCE(sk->sk_lock.owned))
__mptcp_move_skbs_from_subflow(msk, ssk, &moved);

spin_unlock_bh(&sk->sk_lock.slock);

return moved > 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);

if (atomic_read(&sk->sk_rmem_alloc) < READ_ONCE(sk->sk_rcvbuf) &&
move_skbs_to_msk(msk, ssk))
goto wake;

/* don't schedule if mptcp sk is (still) over limit */
if (atomic_read(&sk->sk_rmem_alloc) > READ_ONCE(sk->sk_rcvbuf))
goto wake;
Expand Down
2 changes: 1 addition & 1 deletion net/mptcp/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions net/mptcp/subflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 2e52213

Please sign in to comment.