Skip to contentSkip to conversation

Reduce the differences between TCP and MPTCP for TLS usage

PATCH In review Geliang Tang 2 participants 11 messages Dec 2

Subsystems (3)
lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

v30 cover Reduce the differences between TCP and MPTCP for TLS usage

From: Geliang Tang <[email protected]>

v30:
- Patches 1-4: SIOCINQ patches.
- Patch 8: change copied_seq from u64 to u32 to match TCP and ease future sockmap support. Add mptcp_post_data_fin() helper to reject post-DATA_FIN data at admission in __mptcp_move_skb() and __mptcp_ofo_queue(). Clamp the partial-packet copy_len to max(copied_seq, ack_seq). Rework the OFO window check to drop when ack_seq overtakes rcv_wnd_sent or the unsigned distance 'end_seq - ack_seq' exceeds 'max_seq - ack_seq', catching DSNs more than 2^63 ahead of the window edge.
- Patch 9 (sync CB layout): keep copied_seq as the u32 map_seq rather than map_seq64.

v29:
- Add patch 1: clear rcv_data_fin_seq in mptcp_disconnect() to prevent
stale value from inflating mptcp_inq_hint() after socket reuse.
- Drop v28 patch 2 (selftests): will be sent separately after kernel fix.
- Drop v28 patch 3 (fallback dummy mapping fix): upstreamed.
- Drop v28 patch 6 (add flags field): folded into patch 6.
- Drop v28 patch 9 (drop data after DATA_FIN): underflow protection
moved to consume path in patch 6.
- Patch 2: guard FIN byte (+1) with rcv_data_fin_seq != 0 instead of RCV_SHUTDOWN/TCP_CLOSE, so local shutdown(SHUT_RD) and MP_FASTCLOSE
no longer inflate the hint.
- Patch 6: integrate flags field addition and add underflow protection
in __mptcp_recvmsg_mskq() and mptcp_recv_skb() using before64() to
detect post-DATA-FIN skbs. Update commit message to reflect actual
changes.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v28:
- Split "mptcp: remove CB offset field" into 5 patches.
- patch 2: fix wait_for_remote_key() to return 1 on timeout instead of 0,
and update caller to handle timeout as skip case. Add MPTCP expect_eof
branch in do_recvmsg_cmsg() for new inq semantics.
- patch 5: change set_bit() to __set_bit() in subflow_set_remote_key() to
match other cb_flags operations under msk data lock.
- patch 6: add flags field and MPTCP_SKB_CB_F_OFFSET flag (new patch).
- patch 7: encapsulate DSN access with helpers (new patch).
- patch 8: add copied_seq tracking (new patch).
- patch 10: move DATA_FIN check from __mptcp_move_skb() to __mptcp_move_skbs_from_subflow() before mptcp_init_skb(). Add mptcp_dsn_after_fin() helper. Use sk_eat_skb() instead of mptcp_drop(). Combine conditions using len variable.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v27:
- patch 6: introduce mptcp_dsn() and mptcp_end_dsn() helpers, replacing
all direct MPTCP_SKB_CB(skb)->map_seq and end_seq accesses throughout
the receive path. Check both from and to skbs for MPTCP_SKB_CB_F_OFFSET
in __mptcp_try_coalesce() instead of only from. Restructure __mptcp_recvmsg_mskq() to skip and free already-consumed skbs for non-MSG_PEEK reads. Drop the DATA_FIN validation check from v26.
- patch 7: use mptcp_dsn() and mptcp_end_dsn() helpers instead of direct
field accesses. Switch from before64()/after64() to 32-bit before()/
after() with explicit (u32) casts.
Update mptcp_end_dsn() to return map_seq64 + skb->len.
- patch 8: change read_copied type from int to u32.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v26:
- patch 2: move error printing into wait_for_remote_key() for accurate diagnostics. Simplify return values: 0 for success/timeout/skip,
-1 for fatal errors.
- patch 5: add offset parameter to __mptcp_sync_rcv_sequence() to
correctly handle partial reads of TFO skb before sync.
- patch 6: add DATA_FIN validation in __mptcp_move_skb() to reject skbs
with map_seq64 >= rcv_data_fin_seq, preventing offset calculation
underflow when a protocol-violating peer sends data after DATA_FIN.
- patch 7: update DATA_FIN check to use map_seq64 instead of map_seq.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v25:
- patch 2: replace usleep() with poll() in wait_for_remote_key() to
detect dead connections via POLLHUP/POLLERR, preventing infinite loops
when the connection is reset or the client exits early.
- patch 6: instead of rewinding map_seq by offset (which caused OFO
merge_right to replace validated data with unvalidated prefix), keep
map_seq pointing to the actual start of new data (mapped_dsn). Set
end_seq = map_seq + copy_len to exclude consumed prefix bytes. Add mptcp_offset() helper to compute prefix length on-the-fly as
skb->len - (end_seq - map_seq), used in recvmsg path to skip prefix
bytes. Remove mapping boundary pull logic (no longer needed). Remove
OFO traversal F_OFFSET check (no longer needed with non-rewound
map_seq). Keep F_OFFSET flag only for __mptcp_try_coalesce() to
prevent coalescing skbs with prefix. Change MPTCP_SKB_CB_F_OFFSET from
0x01 to 0x0100 to avoid collision with TCPHDR_FIN when flags field
aliases tcp_flags.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v24:
- patch 1: fix mptcp_inq_hint() EOF hint logic. Only return 1 when
queue is empty AND (TCP_CLOSE OR RCV_SHUTDOWN), not unconditionally
when RCV_SHUTDOWN is set. This avoids returning N+1 instead of N when
there is unread data after local shutdown or MP_FASTCLOSE.
- patch 2: fix IPv4 TCP socket handling in wait_for_remote_key() by
checking EOPNOTSUPP in addition to ENOPROTOOPT. Use poll_timeout
instead of fixed 100ms timeout to avoid false failures on slow CI
systems. Return different values for success/timeout/error to provide
better diagnostics. Timeout is now a warning, not a failure, allowing
the SIOCINQ check to proceed.
- patch 6: fix pskb_pull() truesize corruption in mptcp_init_skb() when
offset > skb_headlen(). Use skb_orphan() before pskb_pull() to allow pskb_expand_head() to properly update truesize, then recompute truesize manually and reattach with skb_set_owner_r(). Separate linear pull
(skb_pull) from paged pull (pskb_pull) handling. Add MPTCP_MIB_RCVPRUNED counter for failure tracking. Fix failure path in __mptcp_move_skbs_from_subflow() to increment seq and maintain TCP/MPTCP state consistency, allowing MPTCP retransmission to fill gaps.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v23:
- patch 2: add SOL_MPTCP fallback definition for compatibility with
older libc headers. Handle ENOPROTOOPT error from getsockopt() to
skip the wait for TCP sockets or MPTCP sockets that fell back to TCP.
- patch 5: move end_seq calculation change from patch 6 to patch 5,
using MPTCP_SKB_CB(skb)->map_seq + skb->len instead of msk->tfo_iasn
in __mptcp_sync_rcv_sequence().
- patch 6: fix error handling in __mptcp_move_skbs_from_subflow() when mptcp_init_skb() fails. Instead of using continue which skips updating
seq and more_data_avail, update seq and jump to the cleanup path to
ensure tp->copied_seq and more_data_avail are properly updated.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v22:
- Squashed "mptcp: fix TFO map_seq sign extension in fastopen" into patch
6 "mptcp: remove CB offset field" (series reduced from 9 to 8 patches).
- patch 1: add RCV_SHUTDOWN check to increment hint_val by 1 when
DATA_FIN is received, matching the previous ack_seq - map_seq behavior
that included the FIN byte.
- patch 2: replace fixed 10ms sleep with MPTCP_INFO polling for MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED flag. Add timeout handling with
retries to ensure third ACK is processed before checking SIOCINQ.
- patch 6: use before() for wrap-safe sequence comparison in mapping
boundary detection. Use pskb_pull() instead of skb_pull() to handle non-linear skbs safely. Add bool return value to indicate
success/failure.
- patch 8: restore backlog flush in mptcp_recv_skb() with
sock_owned_by_user() check to maintain existing behavior for process
context callers while avoiding deadlock for future BH context callers.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v21:
- patch 1: new patch. Fix mptcp_inq_hint() to use mptcp_data_avail()
which computes bytes_received - bytes_consumed instead of
ack_seq - map_seq. This fixes INT_MAX overflow on passive TFO
connections before the MPC 3rd ACK arrives.
- patch 2: new patch. Add selftest to check TFO SIOCINQ doesn't return
INT_MAX after accept().
- patch 4: new patch. Fix fallback dummy mapping anchor in subflow_check_data_avail() by anchoring map_subflow_seq at TCP_SKB_CB(skb)->seq instead of copied_seq. This ensures mptcp_subflow_get_mapped_dsn() returns the correct DSN at copied_seq.
- patch 7: added mapping boundary detection in mptcp_init_skb(). When
offset > 0 and skb starts before current mapping, prefix bytes are
removed via skb_pull() to prevent data corruption. Removed redundant
fallback initialization in mptcp_propagate_state().
- patch 9: kept msk_owned_by_me() assertion (reverted the DEBUG_NET_WARN_ON_ONCE replacement from v20).
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v20:
- patch 2: fix Medium issues from review. Move MPTCP_SYNC_SEQ check
inside the loop under mptcp_data_lock() protection to avoid race with concurrent non-atomic cb_flags updates. Change from atomic test_and_clear_bit() to non-atomic __test_and_clear_bit() since we're
under lock. Add msk->tfo_iasn = 0 reset in mptcp_disconnect() to
prevent stale tfo_iasn from corrupting passive TFO fallback.
- patch 3: new patch. Fix TFO map_seq sign extension in fastopen.c by
casting to u64 before negation: -(u64)skb->len instead of -skb->len.
- patch 4: updated from v19 patch 3. Moved the -(u64)skb->len fix to
patch 3. Added comment in mptcp_init_skb() explaining why offset > 0
with out-of-order mapping is correct: mptcp_subflow_get_mapped_dsn()
returns the DSN for the byte at tp->copied_seq (position 'offset' in
the skb), and consumed prefix bytes are part of the same logical DSN
range.
- patch 6: updated from v19 patch 5. Replace msk_owned_by_me() assertion
with DEBUG_NET_WARN_ON_ONCE using lockdep_sock_is_held() for both
process and BH contexts. Add conditional defer logic: inline cleanup
when caller owns socket lock, defer to worker when caller doesn't.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v19:
- Replace mptcp_iasn() computed function with msk->tfo_iasn immutable
snapshot (set in subflow_set_remote_key()), removing tfo_skb_len field
and the DATA_FIN off-by-one concern.
- Move MPTCP_SYNC_SEQ handling (test_and_clear_bit + copied_seq update)
out of __mptcp_sync_rcv_sequence() into callers, since the function
can be called without the socket lock held.
- Patch 2: move MPTCP_SYNC_SEQ check in mptcp_release_cb() from
post-loop to loop start (before backlog spooling). Add the same
check in mptcp_move_skbs() to sync copied_seq before backlog
processing when user holds the socket lock.
- Patch 4: clamp max_seq to ack_seq in mptcp_data_queue_ofo() to
prevent 64-bit after64() wrap-around when ack_seq > rcv_wnd_sent.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v18:
- Re-add patch 2 "mptcp: drop the cant_coalesce CB field".
- Patch 3: simplify TFO sync by relying on patch 2's __mptcp_sync_rcv_sequence() instead of complex copied_seq checks. Use copied_seq += mptcp_iasn(msk).
- Patch 4: move subflow.c changes to patch 2, use map_seq64 for TFO init.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v17:
- Patch 2: add TFO synchronization: subflow_set_remote_key() preserves
partial read progress by checking copied_seq against the initial TFO
value before updating. Add WRITE_ONCE/READ_ONCE for ordering between
BH (3rd ACK processing) and user-space readers. Add underflow guard (copied_seq >= map_seq) before offset subtraction in
__mptcp_recvmsg_mskq() to protect MSG_PEEK traversals. Skip skbs
entirely before copied_seq. Update subflow.c to set both map_seq
(u32) and map_seq64 (u64) for TFO.
- Patch 3: adapt to the new map_seq (u32) + map_seq64 (u64) layout
introduced in patch 2.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v16:
- Simplified the series to retain only the core changes required by both
kTLS and sockmap: drop the CB offset field, add copied_seq tracking,
and sync the MPTCP skb CB layout with the TCP one.
- Dropped patch 2, leaving the fastopen code unchanged.
- Moved patches 6-11 into a separate series "prepare interfaces for kTLS
and sockmap support".
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v15:
- Patch 2: drop mptcp_data_lock() in mptcp_inq_hint(), use READ_ONCE()
and __test_and_clear_bit() instead. This issue is unrelated to this
series and will be addressed in a separate patch later.
- Patch 5: Guard mptcp_cleanup_rbuf() with read_copied > 0. Initialize/
reset msk->read_copied. Drop redundant mptcp_recv_skb() in __mptcp_read_sock().
- Patch 6: simplify memory ordering, drop smp_mb()/smp_rmb(), keep
existing smp_mb__before_atomic(). Check SOCK_DONE inline.
- Patch 9: drop tcp_sock_rate_check_app_limited() split, call tcp_rate_check_app_limited() directly as Paolo suggested. Add
write_seq != snd_nxt early bail-out.
- Patch 10: wrap assertion behind expect_all_features().
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v14:
- v13 can no longer be applied to the export branch, so it has been
rebased here with the following corrections:
- Patch 2: move locking in mptcp_inq_hint() to avoid races with ack_seq
change from patch 3 to here. Update commit log.
- Patch 3: add mptcp_data_lock() in __mptcp_move_skb() around copied_seq update. Remove local delta variable in __mptcp_ofo_queue(). Initialize msk->copied_seq to 0 in mptcp_propagate_state() fallback path.
- Patch 4: add map_seq64 comparison in __mptcp_try_coalesce() for 64-bit sequence consistency.
- New patch 11: rename mptcp_check_readable to mptcp_stream_is_readable,
and align with TCP by calling sk_is_readable() for KTLS support.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v13:
- use smp_mb instead of smp_mb__before_atomic in mptcp_check_data_fin.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v12:
- Move sock_set_flag(SOCK_DONE) to after smp_mb__before_atomic() in mptcp_check_data_fin() and reuse the existing barrier rather than adding
a new smp_wmb().
- Sample SOCK_DONE before smp_rmb() in mptcp_inq_hint() and mptcp_inq(),
so the smp_rmb() only orders SOCK_DONE read relative to the subsequent ack_seq/copied_seq reads.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v11:
- __mptcp_move_skb(): gate __mptcp_sync_rcv_sequence() on test_and_clear_bit(MPTCP_SYNC_SEQ) so the dummy mapping is only re-based after ack_seq = iasn.
- a new patch to set and test SOCK_DONE.
- use SOCK_DONE in mptcp_inq_hint() and mptcp_inq().
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v10:
- Drop "mptcp: use atomic64_t for locklessly accessed u64 fields"
(v9 patch 1) and the squash to bpf_burst test (v9 patch 11).
Series reduced from 11 to 9 patches.
- Revert all atomic64_read()/atomic64_set() on msk->ack_seq back to
plain reads and WRITE_ONCE() in patches 1-4. Remove the local u64
ack_seq variables that only existed to hold atomic64_read() results.
- mptcp_inq_hint: move test_bit(MPTCP_SYNC_SEQ) inside mptcp_data_lock()
to close the race between the flag check and the ack_seq read. Revert atomic64_read() to plain msk->ack_seq under the lock.
- mptcp_inq (patch 6): use READ_ONCE() for both ack_seq and copied_seq
instead of atomic64_read(). Add comment for the FIN subtraction,
mirroring tcp_inq(). Update commit log.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v9:
- Extend atomic64_t conversion from only msk->ack_seq to all locklessly accessed u64 fields: write_seq, snd_nxt, snd_una, wnd_end,
bytes_received, bytes_consumed.
- Add mptcp_data_lock() in mptcp_inq_hint() to read ack_seq and
copied_seq atomically.
- Add read_copied field and drain it under mptcp_data_lock() in mptcp_read_complete() to fix the BH vs worker data race.
- New patches: mptcp_inq/peek_len and sendmsg_locked proto_ops.
- Squash to "selftests/bpf: Add bpf_burst scheduler & test".
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v8:
- check MPTCP_SYNC_SEQ in mptcp_inq_hint:

       if (hint_val >= INT_MAX) {
               if (test_bit(MPTCP_SYNC_SEQ, &msk->cb_flags))
                       return 0;
               return INT_MAX;
       }

- rename mptcp_sock_rate_check_app_limited to
mptcp_rate_check_app_limited.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v7:
- Patch 1 is a new one to fix the existing issue Sashiko mentioned -
using atomic64_t for msk->ack_seq.
- Fix the map_subflow_seq setting in fallback mode in patch 4.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v6:
- Memory ordering for MPTCP_SYNC_SEQ: Add smp_wmb() before set_bit() in subflow_set_remote_key() and smp_rmb() after test_and_clear_bit() in __mptcp_move_skb() and mptcp_release_cb() to prevent data races on weakly-ordered architectures, ensuring ack_seq updates are visible
before the flag is set and readers see the updated sequence after
observing the flag.
- Lockdep nested locking: Use lock_sock_fast_nested(ssk) instead of lock_sock_fast(ssk) in mptcp_sock_rate_check_app_limited() to suppress
false positive recursive locking warnings when acquiring subflow socket
locks while holding the parent MPTCP socket lock.
- Receive window advertisement order: Swap mptcp_rcv_space_adjust() and mptcp_cleanup_rbuf() in mptcp_read_complete() to expand the receive
buffer before evaluating window updates, ensuring ACKs advertise the
new (larger) window size instead of delaying the advertisement until
the next cycle.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v5:
- only patch 3 changed.
- Fallback offset underflow: Initialize MPTCP sequence space to 0 in mptcp_propagate_state() when mp_opt == NULL, ensuring SKB's map_seq
starts from 0 to match msk->copied_seq and prevent offset calculation underflow in fallback mode.
- Stale peek_seq: Move peek_seq recomputation into the mptcp_move_skbs() continue path and add another recomputation after sk_wait_data(),
ensuring peek_seq stays synchronized with msk->copied_seq updates from MPTCP_SYNC_SEQ processing in both paths.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v4:
- mptcp_recvmsg: move the peek_seq recompute from after sk_wait_data() to before the mptcp_move_skbs() continue check. mptcp_move_skbs() -> __mptcp_move_skb() may shift the head skb's map_seq to the IASN frame via __mptcp_sync_rcv_sequence(); if 'continue' then exits the loop, the recompute is skipped, and the next __mptcp_recvmsg_mskq() computes offset with peek_seq in the old frame minus map_seq in the new frame. The underflow makes the offset test fail and the skb is skipped (and mptcp_recv_skb() actually frees the TFO skb via mptcp_eat_recv_skb()).
- Make all three MPTCP_SYNC_SEQ bit accesses atomic:

   - subflow_set_remote_key:   __set_bit            -> set_bit
   - __mptcp_move_skb:         __test_and_clear_bit -> test_and_clear_bit
   - mptcp_release_cb:         __test_and_clear_bit -> test_and_clear_bit

The flag is set in BH under mptcp_data_lock() (msk slock held) and
cleared in user context under lock_sock() slow path (only owned=1,
slock not held). The non-atomic RMW on the two sides races, which can
lose the IASN sync.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v3:
- Patch 2, handle "offset" in __mptcp_sync_rcv_sequence().
- Patch 3, update __mptcp_move_skb() in response to Sashiko comments:

if (__test_and_clear_bit(MPTCP_SYNC_SEQ, &msk->cb_flags))

		msk->copied_seq += mptcp_iasn(msk);

- Patch 4, replace after64() to after() in mptcp_prune_ofo_queue(). The pre-existing issue raised by Sashiko regarding changing the type of
ack_seq to atomic64_t is not addressed in this series.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v2:
- Patch 3, updated in response to Sashiko comments:

        if (unlikely(msk->rcvd_dummy_seq)) {
                msk->copied_seq += mptcp_iasn(msk);
                __mptcp_sync_rcv_sequence(sk);
                /* Release cb() would otherwise re-base copied_seq
                 * again.
                 */
                test_and_clear_bit(MPTCP_SYNC_SEQ, &msk->cb_flags);
        }

        /* Skip the already peeked data. */
        if (offset >= skb->len) {
                *last = skb;
                continue;
        }

- Patch 5, replaced with Paolo's patch.
- Patch 6, 7, new patches addressing app-limited conditions.
- The patch "trim the duplicated skb head at receive enqueue" has been
dropped, as it is no longer needed after Paolo updated the "mptcp:
out-of-order queue pruning" series.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v1:
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

The goal of this series is to reduce the differences between TCP and MPTCP for TLS usage, in preparation for adding TLS over MPTCP support in the future.

In previous versions [1], a struct tls_prot_ops was defined to represent the interface differences between TCP and MPTCP, which contained the following callbacks:

	struct sk_buff *(*recv_skb)(struct sock *sk, u32 *off);
	bool (*lock_is_held)(struct sock *sk);
	void (*read_done)(struct sock *sk, size_t len);
	u32 (*get_skb_seq)(struct sk_buff *skb);
	int (*skb_get_header)(const struct sk_buff *skb, int offset,
	                      void *to, int len);
	bool (*epollin_ready)(const struct sock *sk);
	void (*check_app_limited)(struct sock *sk);

In reality, some of these callbacks are unnecessary. This series aims to eliminate the get_skb_seq(), skb_get_header(), and lock_is_held() callbacks.

The first four patches come from Paolo's "mptcp: address stall under memory pressure" series v5 [2], with only minor cleanup from my side.

They remove the CB offset field and sync the MPTCP skb CB layout with the TCP one, so that we can obtain the TCP or MPTCP sequence number in a unified way, e.g.:

	struct tls_skb_cb {
	    u32 seq;
	};

	#define TLS_SKB_CB(__skb) ((struct tls_skb_cb *)&((__skb)->cb[0]))

This eliminates the need for a separate get_skb_seq() callback.

Building on the removal of the CB offset field, I also added patch 5 that trims the duplicated skb head at receive enqueue. With that in place, KTLS can retrieve the record header via skb_copy_bits() directly, so there is no longer any need for a dedicated MPTCP helper like mptcp_skb_get_header(). The skb_get_header() callback can thus be removed.

Patch 6 defers sk_data_ready to the worker, which avoids recursive locking when TLS calls back into MPTCP under mptcp_data_lock(). With this change, the lock_is_held() callback is no longer needed and can be removed.

[1]
https://patchwork.kernel.org/project/mptcp/cover/[email protected]/
[2]
https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

Geliang Tang (5):
mptcp: add missing memory barriers for rcv_data_fin_seq
mptcp: extract mptcp_data_fin helper
mptcp: use SOCK_DONE in mptcp_inq_hint
mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
mptcp: encapsulate CB DSN field access with helpers

Paolo Abeni (4):
mptcp: drop the mptcp_ooo_try_coalesce() helper
mptcp: drop the cant_coalesce CB field
mptcp: remove CB offset field
mptcp: sync mptcp skb cb layout with tcp one

 include/trace/events/mptcp.h |   2 +-
 net/mptcp/fastopen.c         |  12 +-
 net/mptcp/options.c          |   1 +
 net/mptcp/protocol.c         | 302 +++++++++++++++++++++++------------
 net/mptcp/protocol.h         |  27 +++-
 net/mptcp/subflow.c          |  10 ++
 6 files changed, 242 insertions(+), 112 deletions(-)
Signature · 1 line
2.55.0
author reviewer
#1 lore raw

PATCH v30 1/9 mptcp: add missing memory barriers for rcv_data_fin_seq

From: Geliang Tang <[email protected]>

mptcp_update_rcv_data_fin() writes rcv_data_fin_seq before setting rcv_data_fin, but there is no write barrier to ensure the ordering is visible to concurrent readers.

mptcp_pending_data_fin() checks rcv_data_fin and then reads rcv_data_fin_seq, but has no read barrier. On weakly-ordered architectures, the reader could observe rcv_data_fin == 1 with a stale rcv_data_fin_seq. Since the workqueue guarantees ordering between mptcp_update_rcv_data_fin() and the worker that calls mptcp_check_data_fin(), the worst case for opportunistic readers is a short delay until the worker runs.

Add smp_wmb() in mptcp_update_rcv_data_fin() between the two writes and a matching smp_rmb() in mptcp_pending_data_fin() between the two reads.

Fixes 3721b9b64676 ("mptcp: Track received DATA_FIN sequence number and add related helpers")
---
 net/mptcp/options.c  | 1 +
 net/mptcp/protocol.c | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
net/mptcp/options.c +1 -0
index 05b9c66aaed6..7c5e748105ea 100644
@@ -1204,6 +1204,7 @@ bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool us
 
 	WRITE_ONCE(msk->rcv_data_fin_seq,
 		   mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
+	smp_wmb(); /* ensure rcv_data_fin_seq is visible before rcv_data_fin */
 	WRITE_ONCE(msk->rcv_data_fin, 1);
 
 	return true;
net/mptcp/protocol.c +3 -1
index b9c6e5a615c8..abc56a1bd07a 100644
@@ -573,8 +573,10 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
 	if (READ_ONCE(msk->rcv_data_fin) &&
 	    ((1 << inet_sk_state_load(sk)) &
 	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
-		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
+		u64 rcv_data_fin_seq;
 
+		smp_rmb(); /* pair with smp_wmb in mptcp_update_rcv_data_fin */
+		rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
 		if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {
 			if (seq)
 				*seq = rcv_data_fin_seq;
Signature · 1 line
2.55.0
author reviewer
#2 lore raw

PATCH v30 2/9 mptcp: extract mptcp_data_fin helper

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Factor the rcv_data_fin and ack_seq == rcv_data_fin_seq check out of mptcp_pending_data_fin() into mptcp_data_fin(), without the socket state restriction. mptcp_pending_data_fin() keeps the state check and calls the helper, so the DATA_FIN condition can also be tested regardless of the socket state, e.g. in mptcp_inq_hint().

---
 net/mptcp/protocol.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
net/mptcp/protocol.c +13 -6
index abc56a1bd07a..a6780579fda1 100644
@@ -565,14 +565,11 @@ static void mptcp_check_data_fin_ack(struct sock *sk)
 	}
 }
 
-/* can be called with no lock acquired */
-static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
+static bool mptcp_data_fin(const struct sock *sk, u64 *seq)
 {
-	struct mptcp_sock *msk = mptcp_sk(sk);
+	const struct mptcp_sock *msk = mptcp_sk(sk);
 
-	if (READ_ONCE(msk->rcv_data_fin) &&
-	    ((1 << inet_sk_state_load(sk)) &
-	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
+	if (READ_ONCE(msk->rcv_data_fin)) {
 		u64 rcv_data_fin_seq;
 
 		smp_rmb(); /* pair with smp_wmb in mptcp_update_rcv_data_fin */
@@ -588,6 +585,16 @@ static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
 	return false;
 }
 
+/* can be called with no lock acquired */
+static bool mptcp_pending_data_fin(struct sock *sk, u64 *seq)
+{
+	if ((1 << inet_sk_state_load(sk)) &
+	    (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))
+		return mptcp_data_fin(sk, seq);
+
+	return false;
+}
+
 static void mptcp_set_datafin_timeout(struct sock *sk)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
Signature · 1 line
2.55.0
author reviewer
#3 lore raw

PATCH v30 3/9 mptcp: use SOCK_DONE in mptcp_inq_hint

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

mptcp_inq_hint() checks sk_state == TCP_CLOSE and RCV_SHUTDOWN to decide whether to return a non-zero hint when the receive queue is empty. Switch to sock_flag(sk, SOCK_DONE) to align with tcp_inq_hint().

SOCK_DONE is set when a peer DATA_FIN or fallback FIN is received, in mptcp_check_data_fin() and __mptcp_subflow_error_report(). It is not set by __mptcp_close() or local shutdown(SHUT_RD), so on an empty receive queue after a local shutdown, on a closed or unconnected socket, or after an abortive close without a prior DATA_FIN, mptcp_inq_hint() now returns 0 instead of 1.

---
 net/mptcp/protocol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/mptcp/protocol.c +1 -1
index a6780579fda1..c7c65ba86b3f 100644
@@ -2499,7 +2499,7 @@ static unsigned int mptcp_inq_hint(const struct sock *sk)
 		return (unsigned int)hint_val;
 	}
 
-	if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
+	if (sock_flag(sk, SOCK_DONE))
 		return 1;
 
 	return 0;
Signature · 1 line
2.55.0
author reviewer
#4 lore raw

PATCH v30 4/9 mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

On passive TFO the head skb's map_seq is -skb->len, so mptcp_inq_hint() computing "ack_seq - map_seq" yields iasn + len, usually >= INT_MAX, and SIOCINQ/FIONREAD/TCP_CM_INQ report INT_MAX. This also breaks receive buffer autotuning via mptcp_rcv_space_adjust().

Use mptcp_data_avail() (bytes_received - bytes_consumed) instead. Add the FIN byte (+1) when SOCK_DONE is set, or when not in fallback and mptcp_data_fin() reports ack_seq caught up; the fallback path is excluded since subflow_sched_work_if_closed() sets rcv_data_fin on any subflow close (including RST), which would count a spurious FIN byte.

Fixes f03afb3aeb9d ("mptcp: drop __mptcp_fastopen_gen_msk_ackseq()")
---
 net/mptcp/protocol.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
net/mptcp/protocol.c +7 -13
index c7c65ba86b3f..c400c6edd034 100644
@@ -2487,22 +2487,16 @@ static bool mptcp_move_skbs(struct sock *sk)
 static unsigned int mptcp_inq_hint(const struct sock *sk)
 {
 	const struct mptcp_sock *msk = mptcp_sk(sk);
-	const struct sk_buff *skb;
+	u64 hint_val = mptcp_data_avail(msk);
 
-	skb = skb_peek(&sk->sk_receive_queue);
-	if (skb) {
-		u64 hint_val = READ_ONCE(msk->ack_seq) - MPTCP_SKB_CB(skb)->map_seq;
+	if (hint_val >= INT_MAX)
+		return INT_MAX;
 
-		if (hint_val >= INT_MAX)
-			return INT_MAX;
+	if (sock_flag(sk, SOCK_DONE) ||
+	    (!__mptcp_check_fallback(msk) && mptcp_data_fin(sk, NULL)))
+		hint_val += 1;
 
-		return (unsigned int)hint_val;
-	}
-
-	if (sock_flag(sk, SOCK_DONE))
-		return 1;
-
-	return 0;
+	return (unsigned int)hint_val;
 }
 
 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
Signature · 1 line
2.55.0
author reviewer
#5 lore raw

PATCH v30 5/9 mptcp: drop the mptcp_ooo_try_coalesce() helper

In reply to Geliang Tang

From: Paolo Abeni <[email protected]>

It's used to save an additional comparison for in-order skbs, but is also a barrier to remove CB offset. Remove the helper, let __mptcp_try_coalesce() always perform the sequence check and remove duplicate checks from the callers.

---
 net/mptcp/protocol.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
net/mptcp/protocol.c +6 -15
index c400c6edd034..bea3ec746a5f 100644
@@ -167,7 +167,8 @@ static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
 {
 	int limit = READ_ONCE(sk->sk_rcvbuf);
 
-	if (unlikely(MPTCP_SKB_CB(to)->cant_coalesce) ||
+	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq ||
+	    unlikely(MPTCP_SKB_CB(to)->cant_coalesce) ||
 	    MPTCP_SKB_CB(from)->offset ||
 	    ((to->len + from->len) > (limit >> 3)) ||
 	    !skb_try_coalesce(to, from, fragstolen, delta))
@@ -203,15 +204,6 @@ static bool mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
 	return true;
 }
 
-static bool mptcp_ooo_try_coalesce(struct mptcp_sock *msk, struct sk_buff *to,
-				   struct sk_buff *from)
-{
-	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq)
-		return false;
-
-	return mptcp_try_coalesce((struct sock *)msk, to, from);
-}
-
 /* "inspired" by tcp_rcvbuf_grow(), main difference:
  * - mptcp does not maintain a msk-level window clamp
  * - returns true when  the receive buffer is actually updated
@@ -351,7 +343,7 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
 	/* with 2 subflows, adding at end of ooo queue is quite likely
 	 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
 	 */
-	if (mptcp_ooo_try_coalesce(msk, msk->ooo_last_skb, skb)) {
+	if (mptcp_try_coalesce(sk, msk->ooo_last_skb, skb)) {
 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
 		return;
@@ -397,7 +389,7 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
 				goto merge_right;
 			}
-		} else if (mptcp_ooo_try_coalesce(msk, skb1, skb)) {
+		} else if (mptcp_try_coalesce(sk, skb1, skb)) {
 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOMERGE);
 			return;
 		}
@@ -790,8 +782,7 @@ static void __mptcp_add_backlog(struct sock *sk,
 	if (!list_empty(&msk->backlog_list))
 		tail = list_last_entry(&msk->backlog_list, struct sk_buff, list);
 
-	if (tail && MPTCP_SKB_CB(skb)->map_seq == MPTCP_SKB_CB(tail)->end_seq &&
-	    ssk == tail->sk &&
+	if (tail && ssk == tail->sk &&
 	    __mptcp_try_coalesce(sk, tail, skb, &fragstolen, &delta)) {
 		skb->truesize -= delta;
 		kfree_skb_partial(skb, fragstolen);
@@ -909,7 +900,7 @@ static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
 
 		end_seq = MPTCP_SKB_CB(skb)->end_seq;
 		tail = skb_peek_tail(&sk->sk_receive_queue);
-		if (!tail || !mptcp_ooo_try_coalesce(msk, tail, skb)) {
+		if (!tail || !mptcp_try_coalesce(sk, tail, skb)) {
 			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
 
 			/* skip overlapping data, if any */
Signature · 1 line
2.55.0
author reviewer
#6 lore raw

PATCH v30 6/9 mptcp: drop the cant_coalesce CB field

In reply to Geliang Tang

From: Paolo Abeni <[email protected]>

Such field is used to prevent coalescing into the TFO skb in case of fastopen. Instead let's perform synchronization of the fastopen skb sequence when the IASN becomes available with the 3rd ack. After the synchronization the TFO skb carries a real mapping and can be coalesced into; this prepares for the later CB layout changes.

When the 'cant_coalesce' field has been introduced, commit f03afb3aeb9d ("mptcp: drop __mptcp_fastopen_gen_msk_ackseq()") noted that updating the already queued skb for passive fastopen socket at 3rd ack time would be difficult and race prone. The main point is that such update doesn't need to be synchronously performed at 3rd ack time, but is sufficient to perform it before the next segment is introduced into the msk.

To that end, add __mptcp_sync_rcv_sequence() to update the TFO skb map_seq and end_seq using the IASN stored in msk->tfo_iasn. Call it from subflow_set_remote_key() when the socket is not owned by user-space, deferring the update via the MPTCP_SYNC_SEQ cb_flags bit otherwise, and from __mptcp_move_skb() before any new skb lands into the msk.

---
 net/mptcp/fastopen.c |  6 ++++--
 net/mptcp/protocol.c | 40 +++++++++++++++++++++++++++++++++++++---
 net/mptcp/protocol.h |  5 ++++-
 net/mptcp/subflow.c  |  8 ++++++++
 4 files changed, 53 insertions(+), 6 deletions(-)
net/mptcp/fastopen.c +4 -2
index f717750906ff..e18b31028e7e 100644
@@ -9,6 +9,7 @@
 void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subflow,
 					      struct request_sock *req)
 {
+	struct mptcp_sock *msk;
 	struct sock *sk, *ssk;
 	struct sk_buff *skb;
 	struct tcp_sock *tp;
@@ -49,15 +50,16 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 	MPTCP_SKB_CB(skb)->end_seq = 0;
 	MPTCP_SKB_CB(skb)->offset = 0;
 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
-	MPTCP_SKB_CB(skb)->cant_coalesce = 1;
 
 	mptcp_data_lock(sk);
 	DEBUG_NET_WARN_ON_ONCE(sock_owned_by_user_nocheck(sk));
 
+	msk = mptcp_sk(sk);
+	msk->rcvd_dummy_seq = true;
 	mptcp_borrow_fwdmem(sk, skb);
 	skb_set_owner_r(skb, sk);
 	__skb_queue_tail(&sk->sk_receive_queue, skb);
-	mptcp_sk(sk)->bytes_received += skb->len;
+	msk->bytes_received += skb->len;
 
 	sk->sk_data_ready(sk);
 
net/mptcp/protocol.c +37 -3
index bea3ec746a5f..42e0d392f716 100644
@@ -168,7 +168,6 @@ static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
 	int limit = READ_ONCE(sk->sk_rcvbuf);
 
 	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq ||
-	    unlikely(MPTCP_SKB_CB(to)->cant_coalesce) ||
 	    MPTCP_SKB_CB(from)->offset ||
 	    ((to->len + from->len) > (limit >> 3)) ||
 	    !skb_try_coalesce(to, from, fragstolen, delta))
@@ -436,7 +435,6 @@ static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset,
 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
 	MPTCP_SKB_CB(skb)->offset = offset;
 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
-	MPTCP_SKB_CB(skb)->cant_coalesce = 0;
 
 	__skb_unlink(skb, &ssk->sk_receive_queue);
 
@@ -444,6 +442,25 @@ static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset,
 	skb_dst_drop(skb);
 }
 
+void __mptcp_sync_rcv_sequence(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct sk_buff *skb;
+
+	if (likely(!msk->rcvd_dummy_seq))
+		return;
+
+	/* User space can have already received the TFO skb. */
+	msk->rcvd_dummy_seq = false;
+	skb = skb_peek_tail(&sk->sk_receive_queue);
+	if (!skb)
+		return;
+
+	MPTCP_SKB_CB(skb)->map_seq = msk->tfo_iasn - skb->len +
+				     MPTCP_SKB_CB(skb)->offset;
+	MPTCP_SKB_CB(skb)->end_seq = msk->tfo_iasn;
+}
+
 static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)
 {
 	u64 copy_len = MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq;
@@ -452,6 +469,12 @@ static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)
 
 	mptcp_borrow_fwdmem(sk, skb);
 
+	/* Be sure to sync the eventual fastopen dummy mapping before any other
+	 * skb lands into the msk.
+	 */
+	if (unlikely(msk->rcvd_dummy_seq))
+		__mptcp_sync_rcv_sequence(sk);
+
 	if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
 		/* in sequence */
 insert:
@@ -2460,7 +2483,13 @@ static bool mptcp_move_skbs(struct sock *sk)
 	u32 moved = 0;
 
 	mptcp_data_lock(sk);
-	while (mptcp_can_spool_backlog(sk, &skbs)) {
+	while (1) {
+		if (__test_and_clear_bit(MPTCP_SYNC_SEQ, &msk->cb_flags))
+			__mptcp_sync_rcv_sequence(sk);
+
+		if (!mptcp_can_spool_backlog(sk, &skbs))
+			break;
+
 		mptcp_data_unlock(sk);
 		enqueued |= __mptcp_move_skbs(sk, &skbs, &moved);
 
@@ -3921,6 +3950,8 @@ static int mptcp_disconnect(struct sock *sk, int flags)
 	msk->bytes_retrans = 0;
 	msk->rcvspace_init = 0;
 	msk->fastclosing = 0;
+	msk->tfo_iasn = 0;
+	msk->rcvd_dummy_seq = false;
 	mptcp_init_rtt_est(msk);
 
 	/* for fallback's sake */
@@ -4128,6 +4159,9 @@ static void mptcp_release_cb(struct sock *sk)
 		struct list_head join_list, skbs;
 		bool spool_bl;
 
+		if (__test_and_clear_bit(MPTCP_SYNC_SEQ, &msk->cb_flags))
+			__mptcp_sync_rcv_sequence(sk);
+
 		spool_bl = mptcp_can_spool_backlog(sk, &skbs);
 		if (!flags && !spool_bl)
 			break;
net/mptcp/protocol.h +4 -1
index 7cdd0a7e8296..26d6cea40b40 100644
@@ -128,13 +128,13 @@
 #define MPTCP_FLUSH_JOIN_LIST	5
 #define MPTCP_SYNC_STATE	6
 #define MPTCP_SYNC_SNDBUF	7
+#define MPTCP_SYNC_SEQ		8
 
 struct mptcp_skb_cb {
 	u64 map_seq;
 	u64 end_seq;
 	u32 offset;
 	u8  has_rxtstamp;
-	u8  cant_coalesce;
 };
 
 #define MPTCP_SKB_CB(__skb)	((struct mptcp_skb_cb *)&((__skb)->cb[0]))
@@ -327,6 +327,7 @@ struct mptcp_sock {
 	u32		last_ack_recv;
 	unsigned long	timer_ival;
 	u32		token;
+	u64		tfo_iasn;
 	unsigned long	flags;
 	unsigned long	cb_flags;
 	bool		recovery;		/* closing subflow write queue reinjected */
@@ -338,6 +339,7 @@ struct mptcp_sock {
 	bool		use_64bit_ack; /* Set when we received a 64-bit DSN */
 	bool		csum_enabled;
 	bool		allow_infinite_fallback;
+	bool		rcvd_dummy_seq;
 	u8		pending_state; /* A subflow asked to set this sk_state,
 					* protected by the msk data lock
 					*/
@@ -1214,6 +1216,7 @@ void mptcp_event_pm_listener(const struct sock *ssk,
 			     enum mptcp_event_type event);
 bool mptcp_userspace_pm_active(const struct mptcp_sock *msk);
 
+void __mptcp_sync_rcv_sequence(struct sock *sk);
 void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subflow,
 					      struct request_sock *req);
 int mptcp_pm_genl_fill_addr(struct sk_buff *msg,
net/mptcp/subflow.c +8 -0
index 85695fe00cc3..600c591d5e5c 100644
@@ -527,6 +527,8 @@ static void subflow_set_remote_key(struct mptcp_sock *msk,
 				   struct mptcp_subflow_context *subflow,
 				   const struct mptcp_options_received *mp_opt)
 {
+	struct sock *sk = (struct sock *)msk;
+
 	/* active MPC subflow will reach here multiple times:
 	 * at subflow_finish_connect() time and at 4th ack time
 	 */
@@ -547,6 +549,12 @@ static void subflow_set_remote_key(struct mptcp_sock *msk,
 	WRITE_ONCE(msk->ack_seq, subflow->iasn);
 	WRITE_ONCE(msk->can_ack, true);
 	atomic64_set(&msk->rcv_wnd_sent, subflow->iasn);
+	msk->tfo_iasn = subflow->iasn;
+
+	if (!sock_owned_by_user(sk))
+		__mptcp_sync_rcv_sequence(sk);
+	else
+		__set_bit(MPTCP_SYNC_SEQ, &msk->cb_flags);
 }
 
 static void mptcp_propagate_state(struct sock *sk, struct sock *ssk,
Signature · 1 line
2.55.0
author reviewer
#7 lore raw

PATCH v30 7/9 mptcp: encapsulate CB DSN field access with helpers

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Add mptcp_dsn() and mptcp_end_dsn() helper functions to access the DSN (Data Sequence Number) from MPTCP skb CB. mptcp_dsn() returns the start DSN (map_seq) and mptcp_end_dsn() returns the end DSN (end_seq).

Replace direct accesses to MPTCP_SKB_CB(skb)->map_seq and end_seq with these helpers where appropriate. This encapsulation will make it easier to change the CB layout in subsequent commits.

This is a pure refactoring patch with no behavior change.

---
 include/trace/events/mptcp.h |  2 +-
 net/mptcp/protocol.c         | 37 ++++++++++++++++++------------------
 net/mptcp/protocol.h         | 10 ++++++++++
 3 files changed, 29 insertions(+), 20 deletions(-)
include/trace/events/mptcp.h +1 -1
index c47fa2672b84..8172a46fe210 100644
@@ -223,7 +223,7 @@ TRACE_EVENT(mptcp_rcvbuf_grow,
 		__entry->space = msk->rcvq_space.space;
 		ofo_empty = RB_EMPTY_ROOT(&msk->out_of_order_queue);
 		__entry->ooo_space = ofo_empty ? 0 :
-				     MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq -
+				     mptcp_end_dsn(msk->ooo_last_skb) -
 				     msk->ack_seq;
 
 		__entry->rcvbuf = sk->sk_rcvbuf;
net/mptcp/protocol.c +18 -19
index 42e0d392f716..734d1891adac 100644
@@ -167,7 +167,7 @@ static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to,
 {
 	int limit = READ_ONCE(sk->sk_rcvbuf);
 
-	if (MPTCP_SKB_CB(from)->map_seq != MPTCP_SKB_CB(to)->end_seq ||
+	if (MPTCP_SKB_CB(from)->map_seq != mptcp_end_dsn(to) ||
 	    MPTCP_SKB_CB(from)->offset ||
 	    ((to->len + from->len) > (limit >> 3)) ||
 	    !skb_try_coalesce(to, from, fragstolen, delta))
@@ -258,8 +258,7 @@ static void mptcp_prune_ofo_queue(struct sock *sk,
 		struct sk_buff *skb = rb_to_skb(node);
 
 		/* Stop pruning if the incoming skb would land in OoO tail. */
-		if (after64(MPTCP_SKB_CB(in_skb)->map_seq,
-			    MPTCP_SKB_CB(skb)->map_seq))
+		if (after64(mptcp_dsn(in_skb), mptcp_dsn(skb)))
 			break;
 
 		pruned = true;
@@ -308,8 +307,8 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
 	u64 seq, end_seq, max_seq;
 	struct sk_buff *skb1;
 
-	seq = MPTCP_SKB_CB(skb)->map_seq;
-	end_seq = MPTCP_SKB_CB(skb)->end_seq;
+	seq = mptcp_dsn(skb);
+	end_seq = mptcp_end_dsn(skb);
 	max_seq = atomic64_read(&msk->rcv_wnd_sent);
 
 	pr_debug("msk=%p seq=%llx limit=%llx empty=%d\n", msk, seq, max_seq,
@@ -349,7 +348,7 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
 	}
 
 	/* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
-	if (!before64(seq, MPTCP_SKB_CB(msk->ooo_last_skb)->end_seq)) {
+	if (!before64(seq, mptcp_end_dsn(msk->ooo_last_skb))) {
 		MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_OFOQUEUETAIL);
 		parent = &msk->ooo_last_skb->rbnode;
 		p = &parent->rb_right;
@@ -361,18 +360,18 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
 	while (*p) {
 		parent = *p;
 		skb1 = rb_to_skb(parent);
-		if (before64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
+		if (before64(seq, mptcp_dsn(skb1))) {
 			p = &parent->rb_left;
 			continue;
 		}
-		if (before64(seq, MPTCP_SKB_CB(skb1)->end_seq)) {
-			if (!after64(end_seq, MPTCP_SKB_CB(skb1)->end_seq)) {
+		if (before64(seq, mptcp_end_dsn(skb1))) {
+			if (!after64(end_seq, mptcp_end_dsn(skb1))) {
 				/* All the bits are present. Drop. */
 				mptcp_drop(sk, skb);
 				MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
 				return;
 			}
-			if (after64(seq, MPTCP_SKB_CB(skb1)->map_seq)) {
+			if (after64(seq, mptcp_dsn(skb1))) {
 				/* partial overlap:
 				 *     |     skb      |
 				 *  |     skb1    |
@@ -403,7 +402,7 @@ static void mptcp_data_queue_ofo(struct mptcp_sock *msk, struct sk_buff *skb)
 merge_right:
 	/* Remove other segments covered by skb. */
 	while ((skb1 = skb_rb_next(skb)) != NULL) {
-		if (before64(end_seq, MPTCP_SKB_CB(skb1)->end_seq))
+		if (before64(end_seq, mptcp_end_dsn(skb1)))
 			break;
 		rb_erase(&skb1->rbnode, &msk->out_of_order_queue);
 		mptcp_drop(sk, skb1);
@@ -465,6 +464,7 @@ static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)
 {
 	u64 copy_len = MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq;
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	u64 dsn = mptcp_dsn(skb);
 	struct sk_buff *tail;
 
 	mptcp_borrow_fwdmem(sk, skb);
@@ -475,7 +475,7 @@ static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)
 	if (unlikely(msk->rcvd_dummy_seq))
 		__mptcp_sync_rcv_sequence(sk);
 
-	if (MPTCP_SKB_CB(skb)->map_seq == msk->ack_seq) {
+	if (dsn == msk->ack_seq) {
 		/* in sequence */
 insert:
 		if (!mptcp_try_rmem_schedule(sk, skb)) {
@@ -493,14 +493,14 @@ static bool __mptcp_move_skb(struct sock *sk, struct sk_buff *skb)
 		skb_set_owner_r(skb, sk);
 		__skb_queue_tail(&sk->sk_receive_queue, skb);
 		return true;
-	} else if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq)) {
+	} else if (after64(dsn, msk->ack_seq)) {
 		mptcp_data_queue_ofo(msk, skb);
 		return false;
 	}
 
 	/* Partial packet */
-	if (after64(MPTCP_SKB_CB(skb)->end_seq, msk->ack_seq)) {
-		copy_len = MPTCP_SKB_CB(skb)->end_seq - msk->ack_seq;
+	if (after64(mptcp_end_dsn(skb), msk->ack_seq)) {
+		copy_len = mptcp_end_dsn(skb) - msk->ack_seq;
 		MPTCP_SKB_CB(skb)->offset += msk->ack_seq -
 					     MPTCP_SKB_CB(skb)->map_seq;
 		MPTCP_SKB_CB(skb)->map_seq += msk->ack_seq -
@@ -908,20 +908,19 @@ static bool __mptcp_ofo_queue(struct mptcp_sock *msk)
 	pr_debug("msk=%p empty=%d\n", msk, RB_EMPTY_ROOT(&msk->out_of_order_queue));
 	while (p) {
 		skb = rb_to_skb(p);
-		if (after64(MPTCP_SKB_CB(skb)->map_seq, msk->ack_seq))
+		if (after64(mptcp_dsn(skb), msk->ack_seq))
 			break;
 
 		p = rb_next(p);
 		rb_erase(&skb->rbnode, &msk->out_of_order_queue);
 
-		if (unlikely(!after64(MPTCP_SKB_CB(skb)->end_seq,
-				      msk->ack_seq))) {
+		if (unlikely(!after64(mptcp_end_dsn(skb), msk->ack_seq))) {
 			mptcp_drop(sk, skb);
 			MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_DUPDATA);
 			continue;
 		}
 
-		end_seq = MPTCP_SKB_CB(skb)->end_seq;
+		end_seq = mptcp_end_dsn(skb);
 		tail = skb_peek_tail(&sk->sk_receive_queue);
 		if (!tail || !mptcp_try_coalesce(sk, tail, skb)) {
 			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;
net/mptcp/protocol.h +10 -0
index 26d6cea40b40..61aeb30486d0 100644
@@ -1416,6 +1416,16 @@ static inline bool subflow_simultaneous_connect(struct sock *sk)
 	return sk->sk_state == TCP_SYN_RECV && is_active_ssk(subflow);
 }
 
+static inline u64 mptcp_end_dsn(const struct sk_buff *skb)
+{
+	return MPTCP_SKB_CB(skb)->end_seq;
+}
+
+static inline u64 mptcp_dsn(const struct sk_buff *skb)
+{
+	return MPTCP_SKB_CB(skb)->map_seq;
+}
+
 #ifdef CONFIG_SYN_COOKIES
 void subflow_init_req_cookie_join_save(const struct mptcp_subflow_request_sock *subflow_req,
 				       struct sk_buff *skb);
Signature · 1 line
2.55.0
author reviewer
#8 lore raw

PATCH v30 8/9 mptcp: remove CB offset field

In reply to Geliang Tang

From: Paolo Abeni <[email protected]>

Instead, use a new msk-level field to track the bytes already consumed inside each skb, carrying the amount of bytes already copied to user-space, alike what TCP is already doing.

The newly introduced 'copied_seq' field is accessed under the msk socket ownership in process context, and under the msk data spinlock in BH context only while the socket is not owned by user-space; updates happening while the socket is owned are deferred to the msk release CB via MPTCP_SYNC_SEQ. This covers the synchronization with IASN at remote key reception time and preserves any partial progress (copy) made on the TFO packet.

Note that the explicit synchronization in __mptcp_move_skb() is needed to ensure that the TFO skb in the receive queue got its map_seq synched before the next skb lands into the receive queue when spooling the backlog at mptcp_release_cb() time, as the release CB synchronization will happen later.

Prior to this patch, the TFO skb dummy mapping was always ignored, now it affects the 'copied_seq' initial update: be sure to extend the sign correctly of such mapping initialization time.

The end_seq field changes meaning: it now stores map_seq + copy_len (where copy_len = skb->len - offset) rather than the end DSN. Helper functions mptcp_dsn() and mptcp_end_dsn() are updated to compute the actual DSN and end DSN from map_seq, end_seq and skb length.

The out-of-window check in mptcp_data_queue_ofo() is extended with the unsigned distance comparison 'end_seq - ack_seq > max_seq - ack_seq', which also catches DSNs ahead of the window edge by more than 2^63 that the signed after64() comparison alone would miss. after64() is kept as well, since it still covers the transient case where ack_seq overtakes rcv_wnd_sent and the unsigned comparison becomes vacuous. The MPTCP_MIB_NODSSWINDOW accounting is preserved.

Overall this simplifies a bit the __mptcp_recvmsg_mskq() and the __mptcp_move_skb() code and will also make possible the next patch.

---
 net/mptcp/fastopen.c |   5 +-
 net/mptcp/protocol.c | 137 ++++++++++++++++++++++++++++++-------------
 net/mptcp/protocol.h |  10 +++-
 net/mptcp/subflow.c  |   6 +-
 4 files changed, 110 insertions(+), 48 deletions(-)
net/mptcp/fastopen.c +3 -2
index e18b31028e7e..1da1dd48daba 100644
@@ -46,9 +46,9 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 	has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
 
 	/* Only the sequence delta is relevant */
-	MPTCP_SKB_CB(skb)->map_seq = -skb->len;
+	MPTCP_SKB_CB(skb)->map_seq = -(u64)skb->len;
 	MPTCP_SKB_CB(skb)->end_seq = 0;
-	MPTCP_SKB_CB(skb)->offset = 0;
+	MPTCP_SKB_CB(skb)->flags = 0;
 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
 
 	mptcp_data_lock(sk);
@@ -56,6 +56,7 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 
 	msk = mptcp_sk(sk);
 	msk->rcvd_dummy_seq = true;
+	msk->copied_seq = (u32)MPTCP_SKB_CB(skb)->map_seq;
 	mptcp_borrow_fwdmem(sk, skb);
 	skb_set_owner_r(skb, sk);
 	__skb_queue_tail(&sk->sk_receive_queue, skb);
net/mptcp/protocol.c +96 -41

290 lines not on this page · raw message

net/mptcp/protocol.h +7 -3
index 61aeb30486d0..1a3161f82252 100644
@@ -130,10 +130,12 @@
 #define MPTCP_SYNC_SNDBUF	7
 #define MPTCP_SYNC_SEQ		8
 
+#define MPTCP_SKB_CB_F_OFFSET	(TCPHDR_FLAGS_MASK + 1)
+
 struct mptcp_skb_cb {
 	u64 map_seq;
 	u64 end_seq;
-	u32 offset;
+	u16 flags;
 	u8  has_rxtstamp;
 };
 
@@ -313,6 +315,7 @@ struct mptcp_sock {
 	u64		rcv_data_fin_seq;
 	u64		bytes_retrans;
 	u64		bytes_consumed;
+	u32		copied_seq;
 	int		snd_burst;
 	int		old_wspace;
 	u64		recovery_snd_nxt;	/* in recovery mode accept up to this seq;
@@ -1418,12 +1421,13 @@ static inline bool subflow_simultaneous_connect(struct sock *sk)
 
 static inline u64 mptcp_end_dsn(const struct sk_buff *skb)
 {
-	return MPTCP_SKB_CB(skb)->end_seq;
+	return MPTCP_SKB_CB(skb)->map_seq + skb->len;
 }
 
 static inline u64 mptcp_dsn(const struct sk_buff *skb)
 {
-	return MPTCP_SKB_CB(skb)->map_seq;
+	return MPTCP_SKB_CB(skb)->map_seq + skb->len -
+	       (MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq);
 }
 
 #ifdef CONFIG_SYN_COOKIES
net/mptcp/subflow.c +4 -2
index 600c591d5e5c..02d48bbb5e60 100644
@@ -551,10 +551,12 @@ static void subflow_set_remote_key(struct mptcp_sock *msk,
 	atomic64_set(&msk->rcv_wnd_sent, subflow->iasn);
 	msk->tfo_iasn = subflow->iasn;
 
-	if (!sock_owned_by_user(sk))
+	if (!sock_owned_by_user(sk)) {
+		msk->copied_seq += (u32)subflow->iasn;
 		__mptcp_sync_rcv_sequence(sk);
-	else
+	} else {
 		__set_bit(MPTCP_SYNC_SEQ, &msk->cb_flags);
+	}
 }
 
 static void mptcp_propagate_state(struct sock *sk, struct sock *ssk,
Signature · 1 line
2.55.0
author reviewer
#9 lore raw

PATCH v30 9/9 mptcp: sync mptcp skb cb layout with tcp one

In reply to Geliang Tang

From: Paolo Abeni <[email protected]>

The MPTCP protocol uses a significantly different CB layout WRT TCP, as it includes different information and uses 64 bits for the sequence numbers.

The CB layout changes map_seq and end_seq from u64 to u32, adds a new map_seq64 field to track the full 64-bit DSN, and aligns the existing flags field with TCP's tcp_flags offset. Add a build time check to ensure the CB layout matches TCP's layout for the three shared fields: map_seq, end_seq, and flags.

The 32-bit sequence numbers are safe because the relative distances remain small. For OFO queue skbs, sequence numbers are validated against rcv_wnd_sent in mptcp_data_queue_ofo(), bounded by the TCP-scaled receive window. For receive queue skbs, no window check is performed, but the unsigned offset calculations in __mptcp_recvmsg_mskq() and mptcp_recv_skb() are safe because copied_seq always falls inside or at the edge of the head skb. This ensures that 32-bit arithmetic (comparisons and subtractions) produces correct results even when 64-bit values cross the 2^32 boundary.

map_seq64 is computed as mptcp_subflow_get_mapped_dsn() in mptcp_init_skb() minus offset, and map_seq is the lower 32 bits.

The receive path now uses 32-bit before()/after() helpers for most sequence comparisons, while map_seq64 is used for window validation and coalescing checks. __mptcp_try_coalesce() gains a map_seq64 contiguity check to ensure skbs are truly adjacent in the 64-bit DSN space.

---
 net/mptcp/fastopen.c |  5 +--
 net/mptcp/protocol.c | 78 ++++++++++++++++++++++++++------------------
 net/mptcp/protocol.h | 10 +++---
 3 files changed, 55 insertions(+), 38 deletions(-)
net/mptcp/fastopen.c +3 -2
index 1da1dd48daba..0179b097414d 100644
@@ -46,7 +46,8 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 	has_rxtstamp = TCP_SKB_CB(skb)->has_rxtstamp;
 
 	/* Only the sequence delta is relevant */
-	MPTCP_SKB_CB(skb)->map_seq = -(u64)skb->len;
+	MPTCP_SKB_CB(skb)->map_seq64 = -(u64)skb->len;
+	MPTCP_SKB_CB(skb)->map_seq = MPTCP_SKB_CB(skb)->map_seq64;
 	MPTCP_SKB_CB(skb)->end_seq = 0;
 	MPTCP_SKB_CB(skb)->flags = 0;
 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
@@ -56,7 +57,7 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 
 	msk = mptcp_sk(sk);
 	msk->rcvd_dummy_seq = true;
-	msk->copied_seq = (u32)MPTCP_SKB_CB(skb)->map_seq;
+	msk->copied_seq = MPTCP_SKB_CB(skb)->map_seq;
 	mptcp_borrow_fwdmem(sk, skb);
 	skb_set_owner_r(skb, sk);
 	__skb_queue_tail(&sk->sk_receive_queue, skb);
net/mptcp/protocol.c +46 -32

217 lines not on this page · raw message

net/mptcp/protocol.h +6 -4
index 1a3161f82252..9ca0f3d71d99 100644
@@ -133,10 +133,12 @@
 #define MPTCP_SKB_CB_F_OFFSET	(TCPHDR_FLAGS_MASK + 1)
 
 struct mptcp_skb_cb {
-	u64 map_seq;
-	u64 end_seq;
+	u32 map_seq;
+	u32 end_seq;
+	u32 unused;
 	u16 flags;
 	u8  has_rxtstamp;
+	u64 map_seq64;
 };
 
 #define MPTCP_SKB_CB(__skb)	((struct mptcp_skb_cb *)&((__skb)->cb[0]))
@@ -1421,12 +1423,12 @@ static inline bool subflow_simultaneous_connect(struct sock *sk)
 
 static inline u64 mptcp_end_dsn(const struct sk_buff *skb)
 {
-	return MPTCP_SKB_CB(skb)->map_seq + skb->len;
+	return MPTCP_SKB_CB(skb)->map_seq64 + skb->len;
 }
 
 static inline u64 mptcp_dsn(const struct sk_buff *skb)
 {
-	return MPTCP_SKB_CB(skb)->map_seq + skb->len -
+	return MPTCP_SKB_CB(skb)->map_seq64 + skb->len -
 	       (MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq);
 }
 
Signature · 1 line
2.55.0
#10 lore raw
In reply to Geliang Tang

Hi Paolo,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Critical: Global Timeout ❌
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Perf: Unstable: regression:sched-bbr validation:sched-loss ⚠️ (5 improvements, 61 new outliers to review)
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/46853848795

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/c7483f6e15a5
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1229196

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)