Skip to contentSkip to conversation

Reduce the differences between TCP and MPTCP for TLS usage

PATCH Superseded Geliang Tang 4 participants 24 messages Nov 16 – Nov 17

Subsystems (4)

v28 of 29 Superseded Latest is v29 →

lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

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

From: Geliang Tang <[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.

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 (7):
mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint
selftests: mptcp: connect: check TFO SIOCINQ overflow
mptcp: fix fallback dummy mapping anchor
mptcp: add flags field to mptcp_skb_cb
mptcp: encapsulate CB DSN field access with helpers
mptcp: drop data after DATA_FIN before skb init
mptcp: defer read_sock cleanup to mptcp_worker

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                          | 292 +++++++++++-------
 net/mptcp/protocol.h                          |  29 +-
 net/mptcp/subflow.c                           |  12 +-
 .../selftests/net/mptcp/mptcp_connect.c       |  69 +++++
 7 files changed, 302 insertions(+), 115 deletions(-)
Signature · 1 line
2.55.0
author reviewer
#1 lore raw

PATCH v28 1/11 mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint

From: Geliang Tang <[email protected]>

Fix mptcp_inq_hint() to compute available bytes using mptcp_data_avail() (bytes_received - bytes_consumed) instead of ack_seq minus the head skb's map_seq.

On passive TFO connections, the TFO skb has map_seq set to -skb->len. Without the removed __mptcp_fastopen_gen_msk_ackseq() to adjust the TFO skb's map_seq, mptcp_inq_hint() computes "ack_seq - map_seq = iasn + len", almost always >= INT_MAX, causing SIOCINQ/FIONREAD and TCP_CM_INQ to report INT_MAX. This also breaks receive buffer autotuning, as mptcp_rcv_space_adjust() uses mptcp_inq_hint() to measure consumed data. The bogus hint prevents mptcp_rcvbuf_grow() from being called until the entire TFO skb has been read.

Using mptcp_data_avail() avoids this by tracking bytes directly. This change also affects non-TFO connections after DATA_FIN: with N bytes unread, both SIOCINQ and TCP_CM_INQ now report N instead of N+1. This differs from TCP, which reports N+1 because the FIN consumes a sequence number. The EOF hint of 1 is preserved for the empty-queue case when the socket is closed or shutdown.

Fixes f03afb3aeb9d ("mptcp: drop __mptcp_fastopen_gen_msk_ackseq()")
---
 net/mptcp/protocol.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
net/mptcp/protocol.c +6 -12
index 50de5cedfd0f..f2644e7a6a05 100644
@@ -2404,22 +2404,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;
-
-		return (unsigned int)hint_val;
-	}
-
-	if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
+	if (!hint_val &&
+	    (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)))
 		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
#2 lore raw

Re: [PATCH mptcp-net v28 01/11] mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message gets the TCP comparison wrong for SIOCINQ.
- [Low] The From: and Signed-off-by addresses do not match.
- [Low] After this patch, MPTCP's TCP_CM_INQ cmsg no longer matches TCP once a DATA_FIN has arrived with N > 0 bytes unread.

--

mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint

This commit changes mptcp_inq_hint() to compute the available bytes with mptcp_data_avail() instead of ack_seq minus the head skb's map_seq. On passive TFO connections the old formula returned INT_MAX. As a side effect, the value reported after DATA_FIN changes from N+1 to N.

Quoted 6 lines

Using mptcp_data_avail() avoids this by tracking bytes directly. This change also affects non-TFO connections after DATA_FIN: with N bytes unread, both SIOCINQ and TCP_CM_INQ now report N instead of N+1. This differs from TCP, which reports N+1 because the FIN consumes a sequence number. The EOF hint of 1 is preserved for the empty-queue case when the socket is closed or shutdown.

[Severity: Low]
Is the comparison with TCP accurate for SIOCINQ? TCP's SIOCINQ path is tcp_inq(), and it subtracts the FIN:

include/net/tcp.h:tcp_inq() {
    ...
		answ = tp->rcv_nxt - tp->copied_seq;

		/* Subtract 1, if FIN was received */
		if (answ && sock_flag(sk, SOCK_DONE))
			answ--;
    ...
}

After a FIN, TCP reports N for SIOCINQ. Only TCP_CM_INQ, through tcp_inq_hint() in net/ipv4/tcp.c, reports N+1.

Before this patch, mptcp_inq_hint() returned N+1 on both interfaces, because mptcp_check_data_fin() bumps ack_seq by 1 for DATA_FIN. So for SIOCINQ/FIONREAD through mptcp_ioctl(), this patch actually makes MPTCP match TCP. Only TCP_CM_INQ is a new difference.

The same applies to the EOF hint of 1. It matches TCP only for TCP_CM_INQ. TCP's SIOCINQ reports 0 in that state.

Could the commit message say which interface now matches TCP and which one differs?

Quoted 4 lines
Fixes f03afb3aeb9d ("mptcp: drop __mptcp_fastopen_gen_msk_ackseq()")

[Severity: Low]
This isn't a bug, but the author address is [email protected] and the only author Signed-off-by uses [email protected]. checkpatch.pl will report this as FROM_SIGN_OFF_MISMATCH. Should a Signed-off-by with the
From: address be added, or the author address be changed to match?

net/mptcp/protocol.c static bool mptcp_move_skbs(struct sock *sk) · line 2407

Quoted 10 lines
net/mptcp/protocol.c +1 -1
index 50de5cedfd0fb..f2644e7a6a05a 100644
@@ -2404,22 +2404,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);

[Severity: Low]
Does this break TCP_CM_INQ parity with TCP once a DATA_FIN has arrived and N > 0 bytes are still unread?

mptcp_recvmsg() builds the cmsg from this helper:

net/mptcp/protocol.c:mptcp_recvmsg() {
    ...
		if (cmsg_flags & MPTCP_CMSG_INQ) {
			unsigned int inq = mptcp_inq_hint(sk);

			put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
		}
    ...
}

With this patch the helper returns bytes_received - bytes_consumed = N. TCP's tcp_inq_hint() returns rcv_nxt - copied_seq, which is N+1 because the FIN consumes a sequence number.

Before this patch MPTCP also returned N+1, because mptcp_check_data_fin() does:

	WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);

mptcp_inq_hint() serves both mptcp_ioctl(SIOCINQ) and the TCP_CM_INQ cmsg. TCP uses different semantics for the two: tcp_inq() subtracts the FIN and tcp_inq_hint() does not. One shared helper therefore cannot match TCP on both interfaces. This patch fixes SIOCINQ but breaks TCP_CM_INQ parity.

Would it make sense to give the cmsg path its own value? For example, it could add 1 when hint_val > 0 and DATA_FIN has been received, so that it matches tcp_inq_hint().

Userspace can rely on the exact +1. The mptcp_connect selftest's do_recvmsg_cmsg() uses it to detect a pending FIN:

	if (ret + 1 != (int)last_hint) {

The next patch in the series, "selftests: mptcp: connect: check TFO SIOCINQ overflow", adds a separate branch for the new inq semantics.

Quoted 6 lines
net/mptcp/protocol.c +2 -3
-	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;

[ ... ]

Quoted 8 lines
net/mptcp/protocol.c +3 -2
-	if (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN))
+	if (!hint_val &&
+	    (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)))
 		return 1;
 
-	return 0;
+	return (unsigned int)hint_val;
 }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794810809.git.tanggeliang%40kylinos.cn
author reviewer
#3 lore raw

PATCH v28 2/11 selftests: mptcp: connect: check TFO SIOCINQ overflow

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Add a SIOCINQ check in mptcp_connect's server mode when MPTFO is enabled to detect TFO SIOCINQ overflow regressions.

Wait for MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED using poll() bounded by poll_timeout, then check if SIOCINQ returns INT_MAX. Use poll() instead of usleep() to detect dead connections via POLLHUP/POLLERR. Add SOL_MPTCP fallback definition and handle ENOPROTOOPT/EOPNOTSUPP errors to skip the wait for TCP or fallback sockets. Skip SIOCINQ check on timeout rather than failing the test.

Add MPTCP expect_eof branch in do_recvmsg_cmsg() for new inq semantics:
set expect_eof when short read consumes exactly last_hint bytes and tcp_inq.last becomes 0.

---
 .../selftests/net/mptcp/mptcp_connect.c       | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
tools/testing/selftests/net/mptcp/mptcp_connect.c +69 -0
index 178d98d91fea..3ae018a2a215 100644
@@ -34,6 +34,7 @@
 #include <linux/time_types.h>
 #include <linux/sockios.h>
 #include <linux/compiler.h>
+#include <linux/mptcp.h>
 
 extern int optind;
 
@@ -43,6 +44,9 @@ extern int optind;
 #ifndef TCP_ULP
 #define TCP_ULP 31
 #endif
+#ifndef SOL_MPTCP
+#define SOL_MPTCP 284
+#endif
 
 static int  poll_timeout = 10 * 1000;
 static bool listen_mode;
@@ -571,6 +575,10 @@ static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len)
 			} else {
 				tcp_inq.expect_eof = true;
 			}
+		} else if ((size_t)ret < len &&
+			   last_hint == (unsigned int)ret &&
+			   tcp_inq.last == 0) {
+			tcp_inq.expect_eof = true;
 		}
 	}
 
@@ -1176,6 +1184,48 @@ static void maybe_close(int fd)
 		close(fd);
 }
 
+static int wait_for_remote_key(int fd)
+{
+	struct mptcp_info info;
+	int timeout_ms = 10;
+	struct pollfd pfd;
+	int elapsed = 0;
+	socklen_t olen;
+	int ret;
+
+	pfd.fd = fd;
+	pfd.events = 0;
+
+	while (elapsed < poll_timeout || poll_timeout < 0) {
+		ret = poll(&pfd, 1, timeout_ms);
+		if (ret < 0) {
+			perror("TFO: poll");
+			return -1;
+		}
+
+		if (pfd.revents & (POLLHUP | POLLERR)) {
+			fprintf(stderr, "TFO: connection closed or reset\n");
+			return -1;
+		}
+
+		olen = sizeof(info);
+		if (getsockopt(fd, SOL_MPTCP, MPTCP_INFO, &info, &olen) < 0) {
+			/* Not an MPTCP socket (TCP or fallback), skip wait */
+			if (errno == ENOPROTOOPT || errno == EOPNOTSUPP)
+				return 0;
+			perror("TFO: getsockopt(MPTCP_INFO)");
+			return -1;
+		}
+		if (info.mptcpi_flags & MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED)
+			return 0;
+
+		elapsed += timeout_ms;
+	}
+	fprintf(stderr,
+		"TFO: timeout waiting for remote key, skip SIOCINQ check\n");
+	return 1;
+}
+
 int main_loop_s(int listensock)
 {
 	struct sockaddr_storage ss;
@@ -1207,6 +1257,25 @@ int main_loop_s(int listensock)
 		check_sockaddr(pf, &ss, salen);
 		check_getpeername(remotesock, &ss, salen);
 
+		if (cfg_sockopt_types.mptfo) {
+			int avail;
+			int ret;
+
+			ret = wait_for_remote_key(remotesock);
+			if (ret < 0) {
+				close(remotesock);
+				return 1;
+			}
+
+			if (ret == 0 &&
+			    ioctl(remotesock, SIOCINQ, &avail) == 0 &&
+			    avail == INT_MAX) {
+				fprintf(stderr, "TFO SIOCINQ overflow\n");
+				close(remotesock);
+				return 1;
+			}
+		}
+
 		if (cfg_input) {
 			fd = open(cfg_input, O_RDONLY);
 			if (fd < 0)
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH mptcp-next v28 02/11] selftests: mptcp: connect: check TFO SIOCINQ overflow

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests: mptcp: wrong tcp_inq.last check in do_recvmsg_cmsg() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests: mptcp: wrong tcp_inq.last check in do_recvmsg_cmsg()
--

commit 433dfaca5d6ee692d7ae8856021861abcc097f8b
Author: Geliang Tang <[email protected]>

selftests: mptcp: connect: check TFO SIOCINQ overflow

This commit adds a SIOCINQ overflow check in mptcp_connect's server mode when MPTFO is enabled and updates TCP_INQ EOF tracking in do_recvmsg_cmsg().

Quoted 4 lines
tools/testing/selftests/net/mptcp/mptcp_connect.c +0 -0
index 178d98d91fea2..3ae018a2a2155 100644

[ ... ]

tools/testing/selftests/net/mptcp/mptcp_connect.c static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len) · line 583

Quoted 10 lines
tools/testing/selftests/net/mptcp/mptcp_connect.c +4 -0
@@ -571,6 +575,10 @@ static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len)
 			} else {
 				tcp_inq.expect_eof = true;
 			}
+		} else if ((size_t)ret < len &&
+			   last_hint == (unsigned int)ret &&
+			   tcp_inq.last == 0) {
+			tcp_inq.expect_eof = true;
 		}
 	}

[Severity: Medium]
Should this check tcp_inq.last == 1 instead of tcp_inq.last == 0?

When the receive queue is drained after DATA_FIN (RCV_SHUTDOWN), mptcp_inq_hint() returns 1, whereas it returns 0 when the receive queue is empty on a still-open connection:

net/mptcp/protocol.c:mptcp_inq_hint() {
    ...
	if (!hint_val &&
	    (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)))
		return 1;

	return (unsigned int)hint_val;
}

If a read partially consumes the queued data (setting last_hint = N) and the next read drains the remaining N bytes before the peer's next write arrives, tcp_inq.last is 0 while the connection is still open. Setting tcp_inq.expect_eof = true here causes the next do_recvmsg_cmsg() call that receives subsequent data to abort the test with xerror("expected EOF..."), while actual EOF after DATA_FIN sets tcp_inq.last = 1 and never matches this branch.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

Re: [PATCH mptcp-next v28 02/11] selftests: mptcp: connect: check TFO SIOCINQ overflow

In reply to Geliang Tang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] In do_recvmsg_cmsg() (tools/testing/selftests/net/mptcp/mptcp_connect.c, lines 578-582),…

--

selftests: mptcp: connect: check TFO SIOCINQ overflow

This commit adds a SIOCINQ check to mptcp_connect's server mode when MPTFO is enabled. Before it checks whether SIOCINQ returns INT_MAX, it waits for MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED. It also adds a new expect_eof branch to do_recvmsg_cmsg() for the new inq semantics.

Quoted 4 lines
tools/testing/selftests/net/mptcp/mptcp_connect.c +0 -0
index 178d98d91fea2..3ae018a2a2155 100644

[ ... ]

tools/testing/selftests/net/mptcp/mptcp_connect.c static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len) · line 584

Quoted 11 lines
tools/testing/selftests/net/mptcp/mptcp_connect.c +4 -0
@@ -571,6 +575,10 @@ static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len)
 			} else {
 				tcp_inq.expect_eof = true;
 			}
+		} else if ((size_t)ret < len &&
+			   last_hint == (unsigned int)ret &&
+			   tcp_inq.last == 0) {
+			tcp_inq.expect_eof = true;
 		}
 	}
 

[Severity: Medium]
Can this new branch in do_recvmsg_cmsg() mark a live connection as finished?

The companion kernel change ("mptcp: fix TFO SIOCINQ overflow in mptcp_inq_hint") makes mptcp_inq_hint() report 1, not 0, for an empty queue at EOF:

net/mptcp/protocol.c:mptcp_inq_hint() {
    ...
	if (!hint_val &&
	    (sk->sk_state == TCP_CLOSE || (sk->sk_shutdown & RCV_SHUTDOWN)))
		return 1;

	return (unsigned int)hint_val;
}

So a post-read hint of 0 only means the receive queue was drained while the connection is still open. The in-tree mptcp_inq.c test relies on the same rule in process_one_client():

	/* tcp_inq should be 1 due to received fin. */
	assert(tcp_inq == 1);

do_rnd_read() picks its read size with cap = rand() & 0xffff. That makes this sequence look possible:

read A: cap is smaller than the queued data, X bytes stay queued,
tcp_inq.last = X
read B: cap > X, recvmsg() returns exactly X, no new data has arrived,
hint is 0
-> tcp_inq.expect_eof = true
read C: the peer's next chunk arrives, recvmsg() returns > 0

        -> xerror("expected EOF, last_hint %u, now %u\n", ...)

mptcp_sockopt.sh runs mptcp_connect with -r 20 and
-c TIMESTAMPNS,TCPINQ. In that mode do_rnd_write() sends 20-byte chunks and sleeps 200ms after each one, so the receiver often drains the queue before more data arrives.

Could this make the test fail now and then, even though the kernel behaves correctly? Before this patch, that state matched neither branch and the test passed.

Is the new branch needed at all? After DATA_FIN, reading the last bytes leaves a post-read hint of 1. If DATA_FIN has not been processed yet, the hint is 0. The existing EOF check already accepts both cases:

	if (ret == 0 && cfg_cmsg_types.tcp_inq)
		if (last_hint != 1 && last_hint != 0)
			xerror("EOF but last tcp_inq hint was %u\n", last_hint);

The commit message also says:

Add MPTCP expect_eof branch in do_recvmsg_cmsg() for new inq semantics:
set expect_eof when short read consumes exactly last_hint bytes and tcp_inq.last becomes 0.

Nothing limits the branch to MPTCP sockets. Its premise, that a hint of 0 means EOF, also doesn't match mptcp_inq_hint().

No later patch in the series touches mptcp_connect.c, so this branch is still there at the end of the series.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794810809.git.tanggeliang%40kylinos.cn
author reviewer
#6 lore raw

PATCH v28 3/11 mptcp: fix fallback dummy mapping anchor

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Commit 2834f8edd74d ("mptcp: cleanup fallback dummy mapping generation") anchored map_seq at the DSN of skb->seq but left map_subflow_seq anchored at copied_seq. This caused mptcp_subflow_get_mapped_dsn() to return the DSN at skb->seq instead of copied_seq.

When TCP queues a partially overlapping segment (seq < rcv_nxt), the skb starts before copied_seq, giving offset > 0. mptcp_init_skb() then sets map_seq to the wrong DSN, causing __mptcp_move_skb() to skip data or drop the skb, leaving a hole in the stream.

Fix by anchoring map_subflow_seq at TCP_SKB_CB(skb)->seq, ensuring mptcp_subflow_get_mapped_dsn() returns the correct DSN at copied_seq.

Fixes 2834f8edd74d ("mptcp: cleanup fallback dummy mapping generation")
---
 net/mptcp/subflow.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/mptcp/subflow.c +1 -1
index 00677fb3efcd..6f07544bfe40 100644
@@ -1496,7 +1496,7 @@ static bool subflow_check_data_avail(struct sock *ssk)
 	skb = skb_peek(&ssk->sk_receive_queue);
 	subflow->map_valid = 1;
 	subflow->map_data_len = skb->len;
-	subflow->map_subflow_seq = tcp_sk(ssk)->copied_seq - subflow->ssn_offset;
+	subflow->map_subflow_seq = TCP_SKB_CB(skb)->seq - subflow->ssn_offset;
 	subflow->map_seq = __mptcp_expand_seq(subflow->map_seq,
 					      subflow->iasn +
 					      TCP_SKB_CB(skb)->seq -
Signature · 1 line
2.55.0
author reviewer
#7 lore raw

PATCH v28 4/11 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 f2644e7a6a05..16b81e8dc90e 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))
@@ -200,15 +201,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
@@ -348,7 +340,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;
@@ -394,7 +386,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;
 		}
@@ -777,8 +769,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);
@@ -902,7 +893,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
#8 lore raw

PATCH v28 5/11 mptcp: drop the cant_coalesce CB field

In reply to Geliang Tang

From: Paolo Abeni <[email protected]>

Such field is used to ensure in-sequence processing in case of fastopen. Instead let's perform synchronization of the fastopen skb sequence when the IASN becomes available with the 3rd ack.

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 don'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 such extent, add __mptcp_sync_rcv_sequence() to update the TFO skb map_seq and end_seq using the IASN stored in msk->tfo_iasn, and call it from __mptcp_move_skb() before any new skb lands into the msk.

When the 3rd ack arrives while the msk is owned by user space, defer the sync via MPTCP_SYNC_SEQ in cb_flags. subflow_set_remote_key() stores tfo_iasn and either calls __mptcp_sync_rcv_sequence() directly (when the socket is not locked) or sets MPTCP_SYNC_SEQ for deferred processing.

mptcp_move_skbs() and mptcp_release_cb() check MPTCP_SYNC_SEQ at each iteration before spooling the backlog, using __test_and_clear_bit() since the cb_flags are protected by the msk data lock. A rcvd_dummy_seq flag tracks whether the TFO skb still carries the dummy mapping and needs synchronization.

---
 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 16b81e8dc90e..41fc2429d2fe 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))
@@ -430,7 +429,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);
 
@@ -438,6 +436,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;
@@ -446,6 +463,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:
@@ -2377,7 +2400,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);
 
@@ -3676,6 +3705,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 */
@@ -3878,6 +3909,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 3be60da12924..b4e49d719200 100644
@@ -126,13 +126,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]))
@@ -325,6 +325,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 */
@@ -336,6 +337,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
 					*/
@@ -1193,6 +1195,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 6f07544bfe40..8f64a9aef2c1 100644
@@ -519,6 +519,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
 	 */
@@ -537,6 +539,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
#9 lore raw

PATCH v28 6/11 mptcp: add flags field to mptcp_skb_cb

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Add a flags field to mptcp_skb_cb and define MPTCP_SKB_CB_F_OFFSET to mark skbs with a non-zero offset. The flag is set in mptcp_init_skb() when offset > 0, and checked in __mptcp_try_coalesce() to prevent coalescing skbs with consumed prefixes.

This is in preparation for the removal of the offset field and the introduction of copied_seq tracking in subsequent patches.

---
 net/mptcp/fastopen.c | 1 +
 net/mptcp/protocol.c | 4 +++-
 net/mptcp/protocol.h | 3 +++
 3 files changed, 7 insertions(+), 1 deletion(-)
net/mptcp/fastopen.c +1 -0
index e18b31028e7e..ac0b23882bfd 100644
@@ -49,6 +49,7 @@ void mptcp_fastopen_subflow_synack_set_params(struct mptcp_subflow_context *subf
 	MPTCP_SKB_CB(skb)->map_seq = -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);
net/mptcp/protocol.c +3 -1
index 41fc2429d2fe..395bdcd0080c 100644
@@ -168,7 +168,8 @@ 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 ||
-	    MPTCP_SKB_CB(from)->offset ||
+	    (MPTCP_SKB_CB(from)->flags | MPTCP_SKB_CB(to)->flags) &
+	     MPTCP_SKB_CB_F_OFFSET ||
 	    ((to->len + from->len) > (limit >> 3)) ||
 	    !skb_try_coalesce(to, from, fragstolen, delta))
 		return false;
@@ -428,6 +429,7 @@ static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset,
 	MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
 	MPTCP_SKB_CB(skb)->offset = offset;
+	MPTCP_SKB_CB(skb)->flags = offset ? MPTCP_SKB_CB_F_OFFSET : 0;
 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;
 
 	__skb_unlink(skb, &ssk->sk_receive_queue);
net/mptcp/protocol.h +3 -0
index b4e49d719200..241437deaf2d 100644
@@ -128,10 +128,13 @@
 #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;
 };
 
Signature · 1 line
2.55.0
#10 lore raw

Re: [PATCH mptcp-next v28 06/11] mptcp: add flags field to mptcp_skb_cb

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 1 · High: 0 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Critical] In net/mptcp/protocol.c, __mptcp_try_coalesce() used to refuse coalescing when MPTCP_SKB_CB(from)->offset was non-zero.
- [Low] The commit message says MPTCP_SKB_CB_F_OFFSET marks "skbs with a non-zero offset".

--

mptcp: add flags field to mptcp_skb_cb

This commit adds a flags field to struct mptcp_skb_cb and a MPTCP_SKB_CB_F_OFFSET flag. mptcp_init_skb() sets the flag when the skb has a non-zero offset. __mptcp_try_coalesce() now tests this flag on both skbs instead of testing from->offset directly, as preparation for removing the offset field later in the series.

[ ... ]

Quoted 5 lines
net/mptcp/protocol.c +0 -0
index 41fc2429d2fed..395bdcd0080c1 100644

net/mptcp/protocol.c static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, · line 175

Quoted 9 lines
net/mptcp/protocol.c +2 -1
@@ -168,7 +168,8 @@ 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 ||
-	    MPTCP_SKB_CB(from)->offset ||
+	    (MPTCP_SKB_CB(from)->flags | MPTCP_SKB_CB(to)->flags) &
+	     MPTCP_SKB_CB_F_OFFSET ||
 	    ((to->len + from->len) > (limit >> 3)) ||
 	    !skb_try_coalesce(to, from, fragstolen, delta))
 		return false;

[Severity: Critical]
Can this coalesce an skb whose prefix the msk has already received?

The flag is only computed in mptcp_init_skb(). The partial packet branch of __mptcp_move_skb() raises offset after that, leaves flags alone, and then jumps to the coalesce attempt:

net/mptcp/protocol.c:__mptcp_move_skb() {
    ...
	/* Partial packet */
	if (after64(MPTCP_SKB_CB(skb)->end_seq, msk->ack_seq)) {
		copy_len = MPTCP_SKB_CB(skb)->end_seq - 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 -
					      MPTCP_SKB_CB(skb)->map_seq;
		goto insert;
	}
    ...
insert:
    ...
		tail = skb_peek_tail(&sk->sk_receive_queue);
		if (tail && mptcp_try_coalesce(sk, tail, skb))
			return true;
    ...
}

At that point from->map_seq equals the old ack_seq, which is also tail->end_seq, and both flags are 0. Before this patch the from->offset test refused the merge. Now skb_try_coalesce() appends all from->len bytes to the tail, already-received prefix included.

__mptcp_recvmsg_mskq() and mptcp_read_sock() copy skb->len - offset bytes from the tail. Wouldn't userspace then get the overlapping bytes twice in the middle of the stream? bytes_consumed could also grow past bytes_received, so mptcp_data_avail() could wrap.

One way to reach this is while the msk is owned by the user. In that case mptcp_data_ready() puts subflow skbs on the backlog without advancing msk->ack_seq. The old-data discard in subflow_check_data_avail() then compares against a stale ack_seq.

Suppose the peer sends DSN [0,1000) on one subflow and [500,1500) on another, either as a normal reinjection or through a crafted DSS. When the backlog is spooled, the second skb takes the partial path: offset goes from 0 to 500, flags stays 0, and the whole skb is coalesced into the first one.

This looks resolved later in the series by "mptcp: remove CB offset field". There, the partial path no longer modifies offset or map_seq, and the from->map_seq != mptcp_end_dsn(to) check rejects the partial skb. The window is limited to this commit and "mptcp: encapsulate CB DSN field access with helpers".

Could this commit keep the from->offset test, or set MPTCP_SKB_CB_F_OFFSET in the partial path? Then a bisect that lands here would not hit the duplicated data.

net/mptcp/protocol.c static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset, · line 433

Quoted 6 lines
net/mptcp/protocol.c +1 -0
@@ -428,6 +429,7 @@ static void mptcp_init_skb(struct sock *ssk, struct sk_buff *skb, int offset,
 	MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow);
 	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;
 	MPTCP_SKB_CB(skb)->offset = offset;
+	MPTCP_SKB_CB(skb)->flags = offset ? MPTCP_SKB_CB_F_OFFSET : 0;
 	MPTCP_SKB_CB(skb)->has_rxtstamp = has_rxtstamp;

[Severity: Low]
This isn't a bug, but the commit message says:

Add a flags field to mptcp_skb_cb and define MPTCP_SKB_CB_F_OFFSET to mark skbs with a non-zero offset.

At this commit the flag only records whether offset was non-zero when mptcp_init_skb() ran. Several places still raise MPTCP_SKB_CB(skb)->offset later without updating flags:

- __mptcp_move_skb(), on the partial packet path
- __mptcp_ofo_queue()
- __mptcp_recvmsg_mskq(), on partial reads
- mptcp_read_sock(), on partial reads

Could the description say that the flag reflects the offset at init time?

Two smaller points. The new to->flags test goes further than the old check, which only looked at from. It costs a few coalescing opportunities. Also, the reason for the value TCPHDR_FLAGS_MASK + 1 is only given in the later "mptcp: sync mptcp skb cb layout with tcp one" patch.

Both points go away once "mptcp: remove CB offset field" removes the offset field. This is only about the wording of this intermediate commit.

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794810809.git.tanggeliang%40kylinos.cn
author reviewer
#11 lore raw

PATCH v28 7/11 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 throughout the receive path. 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..cb990186ec30 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 -
+				     (u32)mptcp_end_dsn(msk->ooo_last_skb) -
 				     msk->ack_seq;
 
 		__entry->rcvbuf = sk->sk_rcvbuf;
net/mptcp/protocol.c +18 -19
index 395bdcd0080c..c780529342d6 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)->flags | MPTCP_SKB_CB(to)->flags) &
 	     MPTCP_SKB_CB_F_OFFSET ||
 	    ((to->len + from->len) > (limit >> 3)) ||
@@ -256,8 +256,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;
@@ -306,8 +305,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,
@@ -347,7 +346,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;
@@ -359,18 +358,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    |
@@ -401,7 +400,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);
@@ -461,6 +460,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);
@@ -471,7 +471,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)) {
@@ -489,14 +489,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 -
@@ -903,20 +903,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 241437deaf2d..755d524ef6b7 100644
@@ -1384,6 +1384,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
#12 lore raw

Re: [PATCH mptcp-next v28 07/11] mptcp: encapsulate CB DSN field access with helpers

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message says the patch replaces direct accesses to MPTCP_SKB_CB(skb)->map_seq and end_seq with…

--

mptcp: encapsulate CB DSN field access with helpers

This commit adds two helpers, mptcp_dsn() and mptcp_end_dsn(). They return the map_seq and end_seq fields of the MPTCP skb CB. The receive path is switched over to these helpers to prepare for later CB layout changes, and no behavior change is intended.

net/mptcp/protocol.c static bool __mptcp_try_coalesce(struct sock *sk, struct sk_buff *to, · line 170

Quoted 10 lines
net/mptcp/protocol.c +1 -1
index 395bdcd0080c1..c780529342d68 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) ||

[Severity: Low]
This isn't a bug, but should the from side of this comparison also use mptcp_dsn(from)? The to side was converted on this same line.

The commit message says:

Replace direct accesses to MPTCP_SKB_CB(skb)->map_seq and end_seq with these helpers throughout the receive path.

Some direct reads in the receive path are still there after this patch.

In __mptcp_move_skb(), just above the new dsn local:

	u64 copy_len = MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq;

and in the partial packet branch of the same function:

		MPTCP_SKB_CB(skb)->offset += msk->ack_seq -
					     MPTCP_SKB_CB(skb)->map_seq;

In __mptcp_ofo_queue(), the uncoalesced path still reads map_seq directly, both for delta and in the pr_debug() that follows:

			int delta = msk->ack_seq - MPTCP_SKB_CB(skb)->map_seq;

The pr_debug() in __mptcp_try_coalesce() also still reads the raw fields.

At this point the helpers return exactly the raw fields, so nothing changes functionally. Even so, the claim about the whole receive path seems too strong, and some functions now use both access styles.

Later in the series, "mptcp: remove CB offset field" removes the partial packet read-modify-write site and the __mptcp_ofo_queue() one. Then "mptcp:
sync mptcp skb cb layout with tcp one" changes this comparison into a deliberate MPTCP_SKB_CB(from)->map_seq64 != mptcp_end_dsn(to) check.

By the end of the series, raw reads are still left in the copy_len computation in __mptcp_move_skb() and in the pr_debug() calls in __mptcp_try_coalesce() and __mptcp_ofo_queue(). The layout change also had to edit these sites directly.

Could the remaining sites be converted as well? If not, could the commit message be reworded to say the conversion is partial?

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794810809.git.tanggeliang%40kylinos.cn
author reviewer
#13 lore raw

PATCH v28 8/11 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 always accessed under the msk socket lock, delegating the synchronization with IASN to mptcp_move_skbs() and the msk release CB, when the socket is owned by the user-space at remote key reception time. Such synchronization preserves any partial progress (copy) made on the TFO packet.

In mptcp_init_skb(), map_seq is computed as mptcp_subflow_get_mapped_dsn() minus offset, effectively encoding the consumed prefix into the sequence number. When offset > 0, the MPTCP_SKB_CB_F_OFFSET flag is set to mark the skb as having a consumed prefix.

Helper functions mptcp_dsn() and mptcp_end_dsn() are updated to compute the actual DSN and end DSN from map_seq and skb length.

__mptcp_recvmsg_mskq() is refactored to take a seq parameter (pointer to copied_seq or peek_seq) and computes the offset as seq minus map_seq. Already consumed skbs (where offset >= skb->len) are skipped and freed for non-MSG_PEEK reads.

Throughout the receive path, direct accesses to MPTCP_SKB_CB(skb)->map_seq and end_seq are replaced with calls to mptcp_dsn() and mptcp_end_dsn().

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 |  4 +--
 net/mptcp/protocol.c | 78 ++++++++++++++++++++++----------------------
 net/mptcp/protocol.h |  7 ++--
 net/mptcp/subflow.c  |  6 ++--
 4 files changed, 49 insertions(+), 46 deletions(-)
net/mptcp/fastopen.c +2 -2
index ac0b23882bfd..8dcfcff9087f 100644
@@ -46,9 +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 = -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;
 
@@ -57,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 = 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 +39 -39

199 lines not on this page · raw message

net/mptcp/protocol.h +4 -3
index 755d524ef6b7..0c7c58b51f61 100644
@@ -133,7 +133,6 @@
 struct mptcp_skb_cb {
 	u64 map_seq;
 	u64 end_seq;
-	u32 offset;
 	u16 flags;
 	u8  has_rxtstamp;
 };
@@ -314,6 +313,7 @@ struct mptcp_sock {
 	u64		rcv_data_fin_seq;
 	u64		bytes_retrans;
 	u64		bytes_consumed;
+	u64		copied_seq;
 	int		snd_burst;
 	int		old_wspace;
 	u64		recovery_snd_nxt;	/* in recovery mode accept up to this seq;
@@ -1386,12 +1386,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 8f64a9aef2c1..a7e8d03452ec 100644
@@ -541,10 +541,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 += 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
#14 lore raw

Re: [PATCH mptcp-next v28 08/11] mptcp: remove CB offset field

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The commit message of "mptcp: remove CB offset field" claims work this patch does not do, and leaves out two changes it does make.
- [Low] The title says "remove CB offset field", but the per-skb offset is still stored in the CB, now implicitly.

--

mptcp: remove CB offset field

This patch drops the per-skb offset field from struct mptcp_skb_cb and tracks consumed bytes with a new msk->copied_seq field, similar to TCP. The skb map_seq now encodes the consumed prefix, and mptcp_dsn() and mptcp_end_dsn() are reworked to derive the DSN from map_seq, end_seq and skb->len. __mptcp_recvmsg_mskq() now takes a seq pointer (copied_seq or a local peek_seq) instead of the copied_total offset.

Quoted 4 lines

In mptcp_init_skb(), map_seq is computed as mptcp_subflow_get_mapped_dsn() minus offset, effectively encoding the consumed prefix into the sequence number. When offset > 0, the MPTCP_SKB_CB_F_OFFSET flag is set to mark the skb as having a consumed prefix.

[ ... ]

Quoted 2 lines

Throughout the receive path, direct accesses to MPTCP_SKB_CB(skb)->map_seq and end_seq are replaced with calls to mptcp_dsn() and mptcp_end_dsn().

[Severity: Medium]
Do these two paragraphs describe this patch, or the earlier ones in the series?

In this diff, the line in mptcp_init_skb() that sets MPTCP_SKB_CB_F_OFFSET is unchanged context:

    MPTCP_SKB_CB(skb)->flags = offset ? MPTCP_SKB_CB_F_OFFSET : 0;

It looks like that came from the earlier "mptcp: add flags field to mptcp_skb_cb" commit.

The mptcp_dsn() and mptcp_end_dsn() call sites are also already in the parent tree. They were added by "mptcp: encapsulate CB DSN field access with helpers", whose message has a nearly identical sentence. This patch only changes the helper bodies.

Some direct accesses are also still there after this patch, for example in __mptcp_try_coalesce():

    if (MPTCP_SKB_CB(from)->map_seq != mptcp_end_dsn(to) ||
    ...
    MPTCP_SKB_CB(to)->end_seq = MPTCP_SKB_CB(from)->end_seq;

and in __mptcp_move_skb():

    u64 copy_len = MPTCP_SKB_CB(skb)->end_seq - MPTCP_SKB_CB(skb)->map_seq;

The message also leaves out a change that the patch needs, in mptcp_fastopen_subflow_synack_set_params():

    -	MPTCP_SKB_CB(skb)->map_seq = -skb->len;
    +	MPTCP_SKB_CB(skb)->map_seq = -(u64)skb->len;

skb->len is unsigned int, so without the cast map_seq would get 2^32 - len. msk->copied_seq is now seeded from map_seq. After the IASN resync, copied_seq - map_seq would then be 2^32, and __mptcp_recvmsg_mskq() would treat the whole TFO payload as already consumed.

The message also does not say that end_seq changes meaning (see below).

Could the commit message be updated to cover what this patch actually changes, including the fastopen cast and the end_seq change?

Quoted 12 lines
net/mptcp/protocol.h +0 -1
index 755d524ef6b7f..0c7c58b51f61c 100644
@@ -133,7 +133,6 @@
 struct mptcp_skb_cb {
 	u64 map_seq;
 	u64 end_seq;
-	u32 offset;
 	u16 flags;
 	u8  has_rxtstamp;
 };

[ ... ]

Quoted 4 lines
net/mptcp/protocol.h +0 -0
@@ -1386,12 +1386,13 @@ static inline bool subflow_simultaneous_connect(struct sock *sk)
 
 static inline u64 mptcp_end_dsn(const struct sk_buff *skb)
 {

net/mptcp/protocol.h static inline bool subflow_simultaneous_connect(struct sock *sk) · line 1396

Quoted 10 lines
net/mptcp/protocol.h +3 -2
-	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);
 }

[Severity: Low]
This isn't a bug, but the per-skb offset still seems to be stored in the CB, just implicitly in end_seq. mptcp_init_skb() now does:

    MPTCP_SKB_CB(skb)->map_seq = mptcp_subflow_get_mapped_dsn(subflow) -
                                 offset;
    MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;

copy_len is skb->len - offset. So for an skb with a consumed prefix, end_seq is end DSN - offset rather than the end DSN, and mptcp_dsn() gets the offset back from end_seq - map_seq.

Some raw users of end_seq remain. __mptcp_try_coalesce() copies from->end_seq into to and logs it as "new end seq". __mptcp_move_skb() uses end_seq - map_seq as the count of new bytes. These are correct only because the MPTCP_SKB_CB_F_OFFSET check in __mptcp_try_coalesce() refuses to coalesce skbs that have a prefix offset.

Nothing documents that dependency: there is no comment and no DEBUG_NET_WARN, and the field keeps the end_seq name. If a later change coalesces offset skbs, or reads end_seq as an end DSN, wouldn't mptcp_dsn() and the OFO ordering silently break?

Would a comment on struct mptcp_skb_cb, or a rename of end_seq, help here? The same encoding is still used at the end of the series, after "mptcp: sync mptcp skb cb layout with tcp one".

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794810809.git.tanggeliang%40kylinos.cn
author reviewer
#15 lore raw

PATCH v28 9/11 mptcp: drop data after DATA_FIN before skb init

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

After the previous commit, data arriving at or after DATA_FIN must be explicitly dropped to prevent state corruption. Without this check, such data could cause offset wraparound in __mptcp_recvmsg_mskq(), leading to bytes_consumed exceeding bytes_received, mptcp_data_avail() wrapping to U64_MAX, SIOCINQ reporting unread data after EOF, and improper connection closure.

Add mptcp_seq_after_data_fin() helper that checks if a DSN range [seq, seq+len) overlaps with or is beyond the DATA_FIN sequence number, and call it in __mptcp_move_skbs_from_subflow() before mptcp_init_skb() to avoid unnecessary skb initialization for data that will be discarded. The helper uses memory barriers (smp_wmb/smp_rmb) to ensure proper ordering between rcv_data_fin_seq and rcv_data_fin flag.

Also update the fin check condition in the else branch to only warn when len == 0 (skb fully consumed) and no FIN flag is present, avoiding false warnings when skbs are dropped due to DATA_FIN.

---
 net/mptcp/options.c  |  1 +
 net/mptcp/protocol.c | 30 +++++++++++++++++++++++++-----
 2 files changed, 26 insertions(+), 5 deletions(-)
net/mptcp/options.c +1 -0
index 71579238a951..425a7fe94e0c 100644
@@ -1177,6 +1177,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 +25 -5
index ddc98497f0eb..78ca580c6d9c 100644
@@ -811,6 +811,25 @@ static void __mptcp_add_backlog(struct sock *sk,
 		msk->backlog_unaccounted += delta;
 }
 
+static bool mptcp_seq_after_data_fin(struct mptcp_sock *msk,
+				     struct sock *ssk, u32 len)
+{
+	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
+	struct sock *sk = (struct sock *)msk;
+
+	if (READ_ONCE(msk->rcv_data_fin) || (sk->sk_shutdown & RCV_SHUTDOWN)) {
+		u64 seq = mptcp_subflow_get_mapped_dsn(subflow);
+		u64 fin_seq;
+
+		smp_rmb(); /* ensure we see the updated rcv_data_fin_seq */
+		fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
+		if (!before64(seq, fin_seq) ||
+		    !before64(seq + len, fin_seq + 1))
+			return true;
+	}
+	return false;
+}
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk, bool own_msk)
 {
@@ -826,7 +845,8 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		u32 map_remaining, offset;
 		u32 seq = tp->copied_seq;
 		struct sk_buff *skb;
-		bool fin;
+		bool fin, data_fin;
+		size_t len;
 
 		/* try to move as much data as available */
 		map_remaining = subflow->map_data_len -
@@ -850,9 +870,9 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		if (fin)
 			seq++;
 
-		if (offset < skb->len) {
-			size_t len = skb->len - offset;
-
+		len = skb->len - offset;
+		data_fin = mptcp_seq_after_data_fin(msk, ssk, len);
+		if (len > 0 && !data_fin) {
 			mptcp_init_skb(ssk, skb, offset, len);
 
 			if (own_msk) {
@@ -870,7 +890,7 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		} else {
 			sk_eat_skb(ssk, skb);
 
-			if (unlikely(!fin)) {
+			if (len == 0 && unlikely(!fin)) {
 				DEBUG_NET_WARN_ON_ONCE(1);
 				mptcp_dss_corruption(msk, ssk);
 			}
Signature · 1 line
2.55.0
#16 lore raw

Re: [PATCH mptcp-next v28 09/11] mptcp: drop data after DATA_FIN before skb init

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Critical] mptcp: unadvanced tp->copied_seq when dropping data after DATA_FIN - [High] mptcp: premature…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Critical] mptcp: unadvanced tp->copied_seq when dropping data after DATA_FIN
- [High] mptcp: premature data drop on local RCV_SHUTDOWN in mptcp_seq_after_data_fin()
- [High] mptcp: post-DATA_FIN OOO, backlog, and straddling skbs bypass filtering
- [High] mptcp: rcv_data_fin clearing in mptcp_check_data_fin() allows DATA_FIN bypass
--

commit 16698b96108faa15e512d4467eb72e9ebc40cd5c
Author: Geliang Tang <[email protected]>

mptcp: drop data after DATA_FIN before skb init

This commit adds mptcp_seq_after_data_fin() and calls it in __mptcp_move_skbs_from_subflow() before mptcp_init_skb() to drop incoming subflow data at or after the DATA_FIN sequence number.

net/mptcp/options.c bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool us · line 1183

Quoted 12 lines
net/mptcp/options.c +1 -0
index 71579238a951c..425a7fe94e0c4 100644
@@ -1177,6 +1177,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;

[Severity: High]
Can data at or after the initial DATA_FIN bypass mptcp_seq_after_data_fin() once mptcp_check_data_fin() clears msk->rcv_data_fin?

net/mptcp/protocol.c:mptcp_check_data_fin() {
    ...
	if (mptcp_pending_data_fin(sk, &rcv_data_fin_seq)) {
		WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
		WRITE_ONCE(msk->rcv_data_fin, 0);

		WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
    ...
}

mptcp_update_rcv_data_fin() only checks READ_ONCE(msk->rcv_data_fin), so after mptcp_check_data_fin() clears msk->rcv_data_fin to 0, a peer sending a second DATA_FIN with a higher data_fin_seq overwrites msk->rcv_data_fin_seq.

In addition, mptcp_check_data_fin() clears msk->rcv_data_fin before setting RCV_SHUTDOWN in sk->sk_shutdown without mptcp_data_lock(sk) or a write barrier, so a concurrent mptcp_seq_after_data_fin() can observe both as 0:

CPU0 CPU1
mptcp_check_data_fin()
WRITE_ONCE(msk->ack_seq, ...)
WRITE_ONCE(msk->rcv_data_fin, 0)

                                  __mptcp_move_skbs_from_subflow()
                                    mptcp_seq_after_data_fin()
                                      READ_ONCE(msk->rcv_data_fin) == 0
                                      (sk->sk_shutdown & RCV_SHUTDOWN) == 0
                                      return false  <-- bypasses check
  WRITE_ONCE(sk->sk_shutdown,
             ... | RCV_SHUTDOWN)

In either case, data at or after the original DATA_FIN is queued into sk->sk_receive_queue after msk->ack_seq has advanced past msk->copied_seq, triggering offset wraparound in __mptcp_recvmsg_mskq() and wrapping mptcp_data_avail() to U64_MAX.

Quoted 10 lines
net/mptcp/protocol.c +2 -0
index ddc98497f0eb0..78ca580c6d9c5 100644
@@ -811,6 +811,25 @@ static void __mptcp_add_backlog(struct sock *sk,
 		msk->backlog_unaccounted += delta;
 }
 
+static bool mptcp_seq_after_data_fin(struct mptcp_sock *msk,
+				     struct sock *ssk, u32 len)

net/mptcp/protocol.c static void __mptcp_add_backlog(struct sock *sk, · line 825

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+{
+	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
+	struct sock *sk = (struct sock *)msk;
+
+	if (READ_ONCE(msk->rcv_data_fin) || (sk->sk_shutdown & RCV_SHUTDOWN)) {
+		u64 seq = mptcp_subflow_get_mapped_dsn(subflow);
+		u64 fin_seq;
+
+		smp_rmb(); /* ensure we see the updated rcv_data_fin_seq */
+		fin_seq = READ_ONCE(msk->rcv_data_fin_seq);

[Severity: High]
Does checking (sk->sk_shutdown & RCV_SHUTDOWN) drop valid in-sequence peer data when RCV_SHUTDOWN was set locally before any DATA_FIN arrived?

__mptcp_close() and inet_shutdown() both set RCV_SHUTDOWN on sk->sk_shutdown while msk->rcv_data_fin_seq is still 0 (or holds a stale value from a previous connection after mptcp_disconnect()). When fin_seq == 0, !before64(seq, 0) is (__s64)seq >= 0, which is true for half of all random initial 64-bit DSNs. Dropping the peer's pre-DATA_FIN data prevents msk->ack_seq from reaching the peer's DATA_FIN sequence number, so the connection hangs in TCP_FIN_WAIT1 or TCP_FIN_WAIT2 until the close timer expires.

net/mptcp/protocol.c static void __mptcp_add_backlog(struct sock *sk, · line 831

Quoted 6 lines
net/mptcp/protocol.c +6 -0
+		if (!before64(seq, fin_seq) ||
+		    !before64(seq + len, fin_seq + 1))
+			return true;
+	}
+	return false;
+}

[Severity: High]
What happens when post-DATA_FIN segments are already queued in msk->out_of_order_queue or msk->backlog_list before DATA_FIN arrives, or when a single skb straddles fin_seq?

Segments that arrived before the packet carrying DATA_FIN have already passed __mptcp_move_skbs_from_subflow(). When DATA_FIN later arrives, __mptcp_ofo_queue() and __mptcp_move_skbs() move those post-DATA_FIN segments into sk->sk_receive_queue without checking rcv_data_fin_seq, causing the same offset underflow in __mptcp_recvmsg_mskq(), mptcp_data_avail() wraparound to U64_MAX, and skipped DATA_FIN acknowledgment.

Conversely, if a single segment straddles fin_seq (seq < fin_seq and seq + len > fin_seq), !before64(seq + len, fin_seq + 1) drops the entire skb including the valid [seq, fin_seq) prefix, preventing msk->ack_seq from reaching fin_seq.

Quoted 28 lines
net/mptcp/protocol.c +6 -4
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk, bool own_msk)
 {
@@ -826,7 +845,8 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		u32 map_remaining, offset;
 		u32 seq = tp->copied_seq;
 		struct sk_buff *skb;
-		bool fin;
+		bool fin, data_fin;
+		size_t len;
 
 		/* try to move as much data as available */
 		map_remaining = subflow->map_data_len -
@@ -850,9 +870,9 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		if (fin)
 			seq++;
 
-		if (offset < skb->len) {
-			size_t len = skb->len - offset;
-
+		len = skb->len - offset;
+		data_fin = mptcp_seq_after_data_fin(msk, ssk, len);
+		if (len > 0 && !data_fin) {
 			mptcp_init_skb(ssk, skb, offset, len);
 
 			if (own_msk) {

net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 896

Quoted 8 lines
net/mptcp/protocol.c +1 -1
@@ -870,7 +890,7 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		} else {
 			sk_eat_skb(ssk, skb);
 
-			if (unlikely(!fin)) {
+			if (len == 0 && unlikely(!fin)) {
 				DEBUG_NET_WARN_ON_ONCE(1);
 				mptcp_dss_corruption(msk, ssk);
 			}

[Severity: Critical]
When len > 0 and data_fin is true, does the else branch eat skb without advancing seq by len before writing tp->copied_seq?

Leaving tp->copied_seq behind by len prevents mptcp_subflow_data_available() from clearing subflow->map_valid and causes offset = tp->copied_seq - TCP_SKB_CB(skb)->seq to underflow u32 on the next segment on that subflow. In subflow_check_data_avail() -> mptcp_subflow_discard_data(), that underflow triggers WARN_ON_ONCE(offset > skb->len) and returns without advancing tp->copied_seq or freeing the skb, causing the caller's loop to spin forever in softirq context:

net/mptcp/subflow.c:subflow_check_data_avail() {
    ...
		old_ack = READ_ONCE(msk->ack_seq);
		ack_seq = mptcp_subflow_get_mapped_dsn(subflow);
		if (unlikely(before64(ack_seq, old_ack))) {
			mptcp_subflow_discard_data(ssk, skb, old_ack - ack_seq);
			continue;
		}
    ...
}

Replacing "if (offset < skb->len)" with an unconditional
"len = skb->len - offset" also wraps len in 32-bit unsigned arithmetic if offset ever exceeds skb->len.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#17 lore raw

Re: [PATCH mptcp-next v28 09/11] mptcp: drop data after DATA_FIN before skb init

In reply to Geliang Tang
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider.

Critical: 1 · High: 2 · Medium: 3 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Critical] In __mptcp_move_skbs_from_subflow() (net/mptcp/protocol.c), the else branch now also runs when len > 0 && data_fin.
- [High] mptcp_seq_after_data_fin() goes into its fin_seq comparison whenever sk->sk_shutdown & RCV_SHUTDOWN is set.
- [High] The fix does not close the bug class its commit message claims to close: data at or after DATA_FIN still reaching the msk receive…
- [Medium] Bisectability / series ordering.
- [Medium] mptcp_seq_after_data_fin() returns true when the DSN range [seq, seq+len) only straddles fin_seq (`!before64(seq + len, fin_seq +…
- [Medium] The explicit if (offset < skb->len) guard was replaced by len = skb->len - offset; ... if (len > 0 && !data_fin) in…
- [Low] The patch adds smp_wmb() in mptcp_update_rcv_data_fin() (options.c:1180), and the commit message says the barriers order…

--

mptcp: drop data after DATA_FIN before skb init

This commit adds mptcp_seq_after_data_fin() and calls it from __mptcp_move_skbs_from_subflow(). Subflow skbs whose DSN range overlaps or follows the recorded DATA_FIN are now freed before mptcp_init_skb(). It also adds an smp_wmb()/smp_rmb() pair around rcv_data_fin_seq and rcv_data_fin, and limits the DSS corruption warning in the drop branch to len == 0.

Quoted 6 lines

After the previous commit, data arriving at or after DATA_FIN must be explicitly dropped to prevent state corruption. Without this check, such data could cause offset wraparound in __mptcp_recvmsg_mskq(), leading to bytes_consumed exceeding bytes_received, mptcp_data_avail() wrapping to U64_MAX, SIOCINQ reporting unread data after EOF, and improper connection closure.

[Severity: Medium]
Doesn't this leave the previous commit ("mptcp: remove CB offset field") in a state that breaks bisection?

In that commit, once DATA_FIN has been processed, msk->ack_seq is F + 1 while msk->copied_seq stays at F. __mptcp_move_skb() accepts an skb with map_seq F + 1 as in sequence. Then __mptcp_recvmsg_mskq() does:

net/mptcp/protocol.c:__mptcp_recvmsg_mskq() {
    ...
		u64 offset = *seq - MPTCP_SKB_CB(skb)->map_seq;
    ...
		if (offset >= skb->len) {
			if (!(flags & MSG_PEEK))
				mptcp_eat_recv_skb(sk, skb);
    ...
}

The offset wraps, and the skb is eaten without bytes_consumed being updated. A bisect or backport that lands only on the previous commit would get the peer-triggerable accounting corruption described above.

Could this fix be folded into, or ordered before, the commit that introduces the problem? Also, this commit does not seem to close the problem completely; see the comments below about the race and the OFO queue.

net/mptcp/options.c bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool us · line 1181

Quoted 10 lines
net/mptcp/options.c +1 -0
index 71579238a951c..425a7fe94e0c4 100644
@@ -1177,6 +1177,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);

[Severity: Low]
This barrier pairs with the new smp_rmb() in mptcp_seq_after_data_fin(). However, mptcp_pending_data_fin() is documented as "can be called with no lock acquired", and it reads the same pair without a read barrier:

net/mptcp/protocol.c:mptcp_pending_data_fin() {
    ...
	if (READ_ONCE(msk->rcv_data_fin) &&
	    ...
		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
    ...
}

Should it get a matching smp_rmb() as well?

In mptcp_seq_after_data_fin(), the condition can be true only because of RCV_SHUTDOWN. In that case the smp_rmb() follows a plain read of sk->sk_shutdown, and no writer issues a write barrier before setting RCV_SHUTDOWN. The smp_mb__before_atomic() in mptcp_check_data_fin() comes after the store. So the "ensure we see the updated rcv_data_fin_seq" comment doesn't hold on that path.

KCSAN would also report the plain sk->sk_shutdown read, since the writers use WRITE_ONCE().

Quoted 7 lines
net/mptcp/options.c +0 -0
 	return true;
net/mptcp/protocol.c +0 -0
index ddc98497f0eb0..78ca580c6d9c5 100644

net/mptcp/protocol.c static void __mptcp_add_backlog(struct sock *sk, · line 820

Quoted 10 lines
net/mptcp/protocol.c +7 -0
@@ -811,6 +811,25 @@ static void __mptcp_add_backlog(struct sock *sk,
 		msk->backlog_unaccounted += delta;
 }
 
+static bool mptcp_seq_after_data_fin(struct mptcp_sock *msk,
+				     struct sock *ssk, u32 len)
+{
+	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
+	struct sock *sk = (struct sock *)msk;
+
+	if (READ_ONCE(msk->rcv_data_fin) || (sk->sk_shutdown & RCV_SHUTDOWN)) {

[Severity: High]
What value does rcv_data_fin_seq have when only RCV_SHUTDOWN is set?

Its only writer is mptcp_update_rcv_data_fin(), which runs only when a DATA_FIN is received. On a fresh socket it is 0. mptcp_disconnect() clears rcv_data_fin but leaves rcv_data_fin_seq stale.

RCV_SHUTDOWN can be set without any DATA_FIN:

- a local shutdown(fd, SHUT_RD) through inet_shutdown(), since mptcp_shutdown() ignores RCV_SHUTDOWN
- mptcp_do_fastclose() and __mptcp_close(), which set SHUTDOWN_MASK

With fin_seq == 0, !before64(seq, 0) is true for every DSN with bit 63 clear. That means on roughly half of all connections, every incoming skb would be dropped after SHUT_RD.

msk->ack_seq then stops advancing, so the peer gets no DATA_ACK and keeps retransmitting. Each drop also hits the copied_seq problem described further below. Before this patch, data received after SHUT_RD was still queued, as with TCP.

Is the RCV_SHUTDOWN arm intended here? The commit message doesn't mention it.

[Severity: High]
Does this check fully stop data at or after DATA_FIN from reaching the msk receive queue, as the commit message describes?

There seem to be two ways around it.

First, mptcp_check_data_fin() runs in process context with the msk owned, but without the msk data lock:

net/mptcp/protocol.c:mptcp_check_data_fin() {
    ...
		WRITE_ONCE(msk->ack_seq, msk->ack_seq + 1);
		WRITE_ONCE(msk->rcv_data_fin, 0);

		WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
    ...
}

At the same time, mptcp_data_ready() for another subflow can run in BH, see that the socket is owned by the user, and call __mptcp_move_skbs_from_subflow(msk, ssk, false).

Suppose that reader sees rcv_data_fin already cleared but RCV_SHUTDOWN not yet set. Both checks are false, so an skb at fin_seq + 1 goes to msk->backlog_list. __mptcp_move_skb() later accepts it as in sequence, since dsn == ack_seq.

Second, an skb at DSN F + 1 that arrives before the DATA_FIN goes into the msk OFO queue without this check. After mptcp_check_data_fin() sets ack_seq = F + 1, the next __mptcp_ofo_queue() call (move_skbs_to_msk() calls it unconditionally) does:

net/mptcp/protocol.c:__mptcp_ofo_queue() {
    ...
		if (after64(mptcp_dsn(skb), msk->ack_seq))
			break;
    ...
			__skb_queue_tail(&sk->sk_receive_queue, skb);
    ...
}

This moves the F + 1 skb into sk_receive_queue. __mptcp_recvmsg_mskq() then computes offset = *seq - map_seq as a wrapped u64 and eats the skb without updating bytes_consumed.

Could a peer still trigger the bytes_received/bytes_consumed corruption this way, by sending data beyond its DATA_FIN out of order and then one more segment?

net/mptcp/protocol.c static void __mptcp_add_backlog(struct sock *sk, · line 828

Quoted 8 lines
net/mptcp/protocol.c +8 -0
+		u64 seq = mptcp_subflow_get_mapped_dsn(subflow);
+		u64 fin_seq;
+
+		smp_rmb(); /* ensure we see the updated rcv_data_fin_seq */
+		fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
+		if (!before64(seq, fin_seq) ||
+		    !before64(seq + len, fin_seq + 1))
+			return true;

[Severity: Medium]
If [seq, seq + len) only straddles fin_seq, this returns true and __mptcp_move_skbs_from_subflow() frees the whole skb. That includes the valid bytes in [seq, fin_seq). Should the skb be trimmed at fin_seq instead?

Within a single DATA_FIN mapping this can't happen, because get_mapping_status() sets the fin seq to data_seq + data_len - 1.

It can happen when the DATA_FIN comes from a separate data-less DSS, or from another subflow, and falls inside an existing mapping.

Unless the peer resends those bytes under a different mapping, msk->ack_seq stays below rcv_data_fin_seq. This check in mptcp_pending_data_fin() then never matches:

	if (READ_ONCE(msk->ack_seq) == rcv_data_fin_seq) {

so the receive side stalls instead of closing.

The drop also goes through sk_eat_skb() with no drop reason and no MPTCP MIB counter, unlike other MPTCP receive drops such as MPTCP_MIB_DUPDATA.

Quoted 18 lines
net/mptcp/protocol.c +6 -1
+	}
+	return false;
+}
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk, bool own_msk)
 {
@@ -826,7 +845,8 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		u32 map_remaining, offset;
 		u32 seq = tp->copied_seq;
 		struct sk_buff *skb;
-		bool fin;
+		bool fin, data_fin;
+		size_t len;
 
 		/* try to move as much data as available */
 		map_remaining = subflow->map_data_len -

net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 876

Quoted 10 lines
net/mptcp/protocol.c +3 -3
@@ -850,9 +870,9 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		if (fin)
 			seq++;
 
-		if (offset < skb->len) {
-			size_t len = skb->len - offset;
-
+		len = skb->len - offset;
+		data_fin = mptcp_seq_after_data_fin(msk, ssk, len);
+		if (len > 0 && !data_fin) {
 			mptcp_init_skb(ssk, skb, offset, len);

[Severity: Medium]
Is this equivalent to the removed offset < skb->len check?

skb->len and offset are both u32. When offset > skb->len, the subtraction wraps to a large value and len > 0 is true.

That wrapped value then goes to mptcp_init_skb(), whose offset and copy_len parameters are int. The result is a negative copy_len and an skb with end_seq below map_seq, which is then handed to __mptcp_move_skb() or the backlog.

mptcp_subflow_discard_data() still guards the same computation with WARN_ON_ONCE(offset > skb->len). The copied_seq issue below produces exactly offset > skb->len for later skbs.

If mptcp_seq_after_data_fin() returns false in that state, for example in the rcv_data_fin/RCV_SHUTDOWN window, can these values reach mptcp_init_skb()?

The else branch now only reports DSS corruption for len == 0, so this case would no longer trigger fallback or reset either.

[ ... ]

net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 896

Quoted 9 lines
net/mptcp/protocol.c +1 -1
@@ -870,7 +890,7 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 		} else {
 			sk_eat_skb(ssk, skb);
 
-			if (unlikely(!fin)) {
+			if (len == 0 && unlikely(!fin)) {
 				DEBUG_NET_WARN_ON_ONCE(1);
 				mptcp_dss_corruption(msk, ssk);
 			}

[Severity: Critical]
When data_fin is true and len > 0, this else branch now frees an skb that still holds len unread bytes. seq += len only happens in the accept branch, so the write-back after the if/else:

		WRITE_ONCE(tp->copied_seq, seq);

stores the old copied_seq, or old + 1 if the skb carries a TCP FIN. Doesn't that leave tp->copied_seq behind TCP_SKB_CB(skb)->seq for every later skb on this subflow?

mptcp_subflow_discard_data() handles the same situation by advancing copied_seq by the discarded amount, plus the FIN byte, before calling sk_eat_skb().

With copied_seq stuck, mptcp_subflow_get_map_offset() stops moving, and copied_seq - skb->seq wraps for later skbs. One remote sequence that gets there, with checksums off and two subflows:

1. the peer sends a data-less DATA_FIN at F on subflow B, which mptcp_update_rcv_data_fin() records
2. the peer sends seg1 of a mapping [S, S+100) with S < F < S+50 on subflow A; mptcp_seq_after_data_fin() returns true, seg1 is eaten, and copied_seq stays at seg1's seq
3. the peer sends [S, F) on subflow B; ack_seq reaches F and mptcp_check_data_fin() sets ack_seq = F + 1
4. the peer sends seg2 of the same mapping on subflow A

In step 4, subflow_check_data_avail() sees an identical mapping. The mapped DSN S is below old_ack, so it calls mptcp_subflow_discard_data():

net/mptcp/subflow.c:mptcp_subflow_discard_data() {
    ...
	offset = tp->copied_seq - TCP_SKB_CB(skb)->seq;
	if (WARN_ON_ONCE(offset > skb->len))
		goto out;
    ...
out:
	if (!before(tcp_sk(ssk)->copied_seq, TCP_SKB_CB(skb)->end_seq))
		sk_eat_skb(ssk, skb);
	if (mptcp_subflow_get_map_offset(subflow) >= subflow->map_data_len)
		subflow->map_valid = 0;
}

Here the offset wraps, nothing is eaten, copied_seq does not move, and map_valid stays set. subflow_check_data_avail() then continues its for (;;) loop.

Can that loop spin forever in softirq with the ssk lock held?

The same desync has other effects:

- a new mapping can see a wrapped skb_consumed in skb_is_fully_mapped() and get MAPPING_INVALID, which leads to fallback or a subflow reset
- tcp_prune_queue()->tcp_collapse() is called with tp->copied_seq as start, which could reach BUG_ON(offset < 0) in tcp_collapse()
- rcv_nxt - copied_seq stays non-zero for good

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794810809.git.tanggeliang%40kylinos.cn
author reviewer
#18 lore raw

PATCH v28 10/11 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 | 76 ++++++++++++++++++++++++++------------------
 net/mptcp/protocol.h | 10 +++---
 3 files changed, 54 insertions(+), 37 deletions(-)
net/mptcp/fastopen.c +3 -2
index 8dcfcff9087f..952f04dffc9e 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 = MPTCP_SKB_CB(skb)->map_seq;
+	msk->copied_seq = MPTCP_SKB_CB(skb)->map_seq64;
 	mptcp_borrow_fwdmem(sk, skb);
 	skb_set_owner_r(skb, sk);
 	__skb_queue_tail(&sk->sk_receive_queue, skb);
net/mptcp/protocol.c +45 -31

205 lines not on this page · raw message

net/mptcp/protocol.h +6 -4
index 0c7c58b51f61..86634c04f499 100644
@@ -131,10 +131,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]))
@@ -1386,12 +1388,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
#19 lore raw

Re: [PATCH mptcp-next v28 10/11] mptcp: sync mptcp skb cb layout with tcp one

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mptcp: copied_seq offset underflow in recv path for OoO data after DATA_FIN --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mptcp: copied_seq offset underflow in recv path for OoO data after DATA_FIN
--

commit 4e18ccd1c70ef371066d9f0f5c43875b2c871587
Author: Paolo Abeni <[email protected]>

mptcp: sync mptcp skb cb layout with tcp one

This commit aligns struct mptcp_skb_cb with struct tcp_skb_cb by converting map_seq and end_seq to u32, adding map_seq64 for the 64-bit DSN, and using 32-bit sequence comparisons and offset calculations in the receive path.

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index 78ca580c6d9c5..50ae8cdec8a4a 100644

[ ... ]

Quoted 10 lines
net/mptcp/protocol.c +1 -1
@@ -2231,7 +2233,7 @@ static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg,
 	int copied = 0;
 
 	skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) {
-		u64 offset = *seq - MPTCP_SKB_CB(skb)->map_seq;
+		u32 offset = (u32)(*seq) - MPTCP_SKB_CB(skb)->map_seq;
 		u32 data_len = skb->len - offset;
 		u32 count;
 		int err;

net/mptcp/protocol.c static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off) · line 4662

Quoted 8 lines
net/mptcp/protocol.c +1 -1
@@ -4654,7 +4656,7 @@ static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off)
 		mptcp_move_skbs(sk);
 
 	while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
-		offset = msk->copied_seq - MPTCP_SKB_CB(skb)->map_seq;
+		offset = (u32)msk->copied_seq - MPTCP_SKB_CB(skb)->map_seq;
 		if (offset < skb->len) {
 			*off = offset;
 			return skb;

[Severity: Medium]
Can offset underflow in __mptcp_recvmsg_mskq() and mptcp_recv_skb() when an out-of-order segment at or after DATA_FIN is queued before the DATA_FIN arrives?

Commit "mptcp: drop data after DATA_FIN before skb init" checks mptcp_seq_after_data_fin() when dequeuing from the subflow, which only acts if msk->rcv_data_fin or RCV_SHUTDOWN is already set. If a peer sends an out-of-order segment with dsn == rcv_data_fin_seq + 1 before sending the DATA_FIN, the segment is placed into msk->out_of_order_queue or msk->backlog_list. Once the DATA_FIN arrives and pre-DATA_FIN data is received, mptcp_check_data_fin() advances msk->ack_seq to rcv_data_fin_seq + 1 while msk->copied_seq reaches at most rcv_data_fin_seq. __mptcp_ofo_queue() or __mptcp_move_skb() then moves that queued segment into sk->sk_receive_queue and adds its length to msk->bytes_received.

When offset == 0 in mptcp_init_skb(), MPTCP_SKB_CB(skb)->map_seq is (u32)(rcv_data_fin_seq + 1), so (u32)copied_seq - map_seq underflows to 0xffffffff >= skb->len. Both receive helpers then free the skb via mptcp_eat_recv_skb() without advancing msk->bytes_consumed, leaving mptcp_data_avail(msk) non-zero after EOF and causing __mptcp_close() to send MP_FASTCLOSE. If the subflow skb had a non-zero offset in mptcp_init_skb() so that map_seq <= (u32)rcv_data_fin_seq, the computed offset is 1 byte too small, copying a discarded prefix byte to userspace and underflowing mptcp_data_avail(msk).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#20 lore raw

Re: [PATCH mptcp-next v28 10/11] mptcp: sync mptcp skb cb layout with tcp one

In reply to Geliang Tang
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 2

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] mptcp: copied_seq offset underflow in recv path for OoO data after DATA_FIN
- [Low] This finding is about the commit message and design.
- [Low] struct mptcp_skb_cb in net/mptcp/protocol.h is now tied to struct tcp_skb_cb.

--

mptcp: sync mptcp skb cb layout with tcp one

This patch changes struct mptcp_skb_cb so that map_seq, end_seq and flags sit at the same offsets as seq, end_seq and tcp_flags in struct tcp_skb_cb. map_seq and end_seq shrink to 32 bits, and a new map_seq64 field holds the full DSN. Most receive path comparisons now use the 32-bit before()/after() helpers, and mptcp_proto_init() gains a build time layout check.

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index 78ca580c6d9c5..50ae8cdec8a4a 100644

[ ... ]

Quoted 11 lines
net/mptcp/protocol.c +7 -0
@@ -4904,11 +4906,23 @@ static int mptcp_napi_poll(struct napi_struct *napi, int budget)
 	return work_done;
 }
 
+#define CHK_CB_FIELD(mptcp_field, tcp_field)	\
+	({					\
+		BUILD_BUG_ON(offsetof(struct mptcp_skb_cb, mptcp_field) !=    \
+			     offsetof(struct tcp_skb_cb, tcp_field));	      \
+		BUILD_BUG_ON(offsetofend(struct mptcp_skb_cb, mptcp_field) != \
+			     offsetofend(struct tcp_skb_cb, tcp_field));      \
+	})

net/mptcp/protocol.c static int mptcp_napi_poll(struct napi_struct *napi, int budget) · line 4925

Quoted 10 lines
net/mptcp/protocol.c +5 -0
+
 void __init mptcp_proto_init(void)
 {
 	struct mptcp_delegated_action *delegated;
 	int cpu;
 
+	CHK_CB_FIELD(map_seq, seq);
+	CHK_CB_FIELD(end_seq, end_seq);
+	CHK_CB_FIELD(flags, tcp_flags);
+

[Severity: Low]
Which consumer needs struct mptcp_skb_cb to match struct tcp_skb_cb? The commit message says what changes, but not why the layout has to follow TCP's.

In net/mptcp/protocol.c, nothing reads an msk receive queue skb through TCP_SKB_CB() after mptcp_init_skb() has run. The remaining TCP_SKB_CB() users are __mptcp_move_skbs_from_subflow(), which works on subflow skbs before mptcp_init_skb(), and the transmit path.

The last patch in the series ("mptcp: defer read_sock cleanup to mptcp_worker") mentions an upcoming MPTCP TLS series. So the intended user looks like the kTLS strparser, where tls_strp_check_queue_ok() reads TCP_SKB_CB(skb)->seq.

Could the commit message say so? This change moves the receive path from 64-bit to 32-bit sequence arithmetic. It also keeps two copies of the DSN (map_seq and map_seq64), which mptcp_init_skb(), __mptcp_sync_rcv_sequence() and mptcp_fastopen_subflow_synack_set_params() all have to keep in sync.

The commit message also says:

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.

Do these fields share anything beyond their offsets? In __mptcp_move_skbs_from_subflow():

		len = skb->len - offset;
		...
			mptcp_init_skb(ssk, skb, offset, len);

and in mptcp_init_skb():

	MPTCP_SKB_CB(skb)->map_seq64 = mptcp_subflow_get_mapped_dsn(subflow) -
				       offset;
	MPTCP_SKB_CB(skb)->map_seq = (u32)MPTCP_SKB_CB(skb)->map_seq64;
	MPTCP_SKB_CB(skb)->end_seq = MPTCP_SKB_CB(skb)->map_seq + copy_len;

Here map_seq is the DSN of skb->data[0], while end_seq is map_seq + skb->len - offset. For an skb with MPTCP_SKB_CB_F_OFFSET set, TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq gives the copy length, not skb->len as it would under TCP semantics.

The other fields don't line up either. has_rxtstamp sits on the byte where tcp_skb_cb keeps sacked. map_seq64 overlaps TCP's has_rxtstamp bitfield byte and ack_seq.

CHK_CB_FIELD() only compares offsetof() and offsetofend(), so it can't catch any of these mismatches. A later patch might reuse TCP receive or ofo helpers because of the advertised compatibility. Would such a patch misread skbs that carry an offset?

Should end_seq follow TCP's seq + skb->len? If not, should it be documented as not TCP compatible for offset skbs?

One smaller wording issue. The commit message says:

__mptcp_try_coalesce() gains a map_seq64 contiguity check to ensure skbs are truly adjacent in the 64-bit DSN space.

The baseline already had this check, with map_seq as a u64:

	if (MPTCP_SKB_CB(from)->map_seq != mptcp_end_dsn(to) ||

So the patch only renames the field in the existing check.

Nothing breaks at runtime today, because every reader goes through MPTCP_SKB_CB(), mptcp_dsn() and mptcp_end_dsn(). The last commit in the series leaves all of this unchanged.

[ ... ]

Quoted 7 lines
net/mptcp/protocol.h +0 -0
index 0c7c58b51f61c..86634c04f499e 100644
@@ -131,10 +131,12 @@
 #define MPTCP_SKB_CB_F_OFFSET	(TCPHDR_FLAGS_MASK + 1)
 

net/mptcp/protocol.h line 140

Quoted 10 lines
net/mptcp/protocol.h +4 -2
 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;
 };

[Severity: Low]
This isn't a bug, but could struct mptcp_skb_cb get a comment? It would say that map_seq, end_seq and flags must keep the same offsets and sizes as seq, end_seq and tcp_flags in struct tcp_skb_cb.

The new unused member exists only to pad over TCP's tcp_tw_isn / tcp_gso_segs+tcp_gso_size union. The constraint is written down only in the CHK_CB_FIELD() block in mptcp_proto_init().

Someone who reorders fields here, drops unused, or widens has_rxtstamp would only find out from a BUILD_BUG_ON in net/mptcp/protocol.c. A short comment on the struct and on unused, pointing at that check, would cover it.

[ ... ]

net/mptcp/protocol.h static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg, · line 2239

Quoted 9 lines
net/mptcp/protocol.h +1 -1
@@ -2231,7 +2233,7 @@ static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg,
 	int copied = 0;
 
 	skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) {
-		u64 offset = *seq - MPTCP_SKB_CB(skb)->map_seq;
+		u32 offset = (u32)(*seq) - MPTCP_SKB_CB(skb)->map_seq;
 		u32 data_len = skb->len - offset;
 		u32 count;
 		int err;

[Severity: Medium]
The commit message says this u32 offset is safe because copied_seq always falls inside or at the edge of the head skb. Does that still hold once a DATA_FIN has been processed?

mptcp_check_data_fin() moves msk->ack_seq to rcv_data_fin_seq + 1 to account for the DATA_FIN. msk->copied_seq stops at rcv_data_fin_seq, because no payload byte goes with the FIN.

The check added earlier in the series, mptcp_seq_after_data_fin(), only runs in __mptcp_move_skbs_from_subflow(). It only acts once msk->rcv_data_fin or RCV_SHUTDOWN is already set.

A peer can send an out-of-order segment whose DSS mapping is at or beyond the DATA_FIN sequence, before the DATA_FIN itself arrives. That segment passes the check and waits in msk->out_of_order_queue or msk->backlog_list. When the DATA_FIN later arrives, there are a few ways this goes wrong.

If the queued segment covers rcv_data_fin_seq, __mptcp_ofo_queue() moves msk->ack_seq past it before mptcp_pending_data_fin() runs. Then ack_seq never equals rcv_data_fin_seq, and the DATA_FIN is never acted on.

If the queued segment starts at rcv_data_fin_seq + 1 with no offset, mptcp_check_data_fin() bumps ack_seq to that value first. Then __mptcp_ofo_queue() or __mptcp_move_skb() treats the segment as in sequence and moves it to sk->sk_receive_queue. Its map_seq is one past msk->copied_seq.

__mptcp_recvmsg_mskq() and mptcp_recv_skb() then compute an offset of 0xffffffff. The skb looks fully consumed and is freed through mptcp_eat_recv_skb(). Neither msk->bytes_consumed nor msk->copied_seq is updated, so mptcp_data_avail() stays non-zero after EOF. SIOCINQ through mptcp_inq_hint() reports unread data. __mptcp_close() also sees unread data and resets the connection through mptcp_do_fastclose() instead of closing cleanly.

If instead the skb carries MPTCP_SKB_CB_F_OFFSET, map_seq is at or before rcv_data_fin_seq, and the computed offset is one byte short. recvmsg copies the byte at the DATA_FIN sequence, which belongs to the prefix mptcp_init_skb() meant to skip. bytes_consumed then gets ahead of bytes_received, and mptcp_data_avail() underflows to U64_MAX.

All of this is reachable by a remote peer that only reorders its own DSS mappings around the DATA_FIN.

What is the intended fix? One option is to apply the mptcp_seq_after_data_fin() test again when skbs leave the ofo queue or the backlog, in __mptcp_ofo_queue() and __mptcp_move_skb(). Another is to purge anything at or after rcv_data_fin_seq when the DATA_FIN is accepted. Either way, the safety argument for the 32-bit offset in this patch depends on that invariant, so it should actually hold before the receive path relies on it.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794810809.git.tanggeliang%40kylinos.cn
author reviewer
#21 lore raw

PATCH v28 11/11 mptcp: defer read_sock cleanup to mptcp_worker

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

This patch prepares __mptcp_read_sock() for future MPTCP TLS support, where it may be reached from BH context. In that case, calling mptcp_rcv_space_adjust() and mptcp_cleanup_rbuf() is not safe as both expect process context.

Conditionally defer cleanup to mptcp_worker(): when the caller doesn't own the socket lock (BH context), accumulate consumed bytes in msk->read_copied and signal the worker through MPTCP_WORK_READ_COMPLETE. When the caller owns the lock (process context), perform inline cleanup to avoid delaying window updates and autotuning.

The existing backlog flush in mptcp_recv_skb() is now gated on sock_owned_by_user(). This ensures process context callers flush the backlog before processing skbs, while future BH context callers skip it. The reason is that BH callers would already hold the msk spinlock, so mptcp_data_lock() inside mptcp_move_skbs() would self-deadlock.

Initialize msk->read_copied in __mptcp_init_sock() and reset it in mptcp_disconnect().

Note: The BH call path does not exist in the current tree. This is groundwork for the upcoming MPTCP TLS series. Currently, the only in-tree caller is mptcp_splice_read(), which holds lock_sock() and runs in process context.

---
 net/mptcp/protocol.c | 38 +++++++++++++++++++++++++++++++++-----
 net/mptcp/protocol.h |  2 ++
 2 files changed, 35 insertions(+), 5 deletions(-)
net/mptcp/protocol.c +33 -5
index 50ae8cdec8a4..6988f6128368 100644
@@ -3165,6 +3165,21 @@ static void mptcp_backlog_purge(struct sock *sk)
 	sk_mem_reclaim(sk);
 }
 
+static void mptcp_read_complete(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	u32 read_copied;
+
+	mptcp_data_lock(sk);
+	read_copied = msk->read_copied;
+	msk->read_copied = 0;
+	mptcp_data_unlock(sk);
+
+	mptcp_rcv_space_adjust(msk, read_copied);
+	if (read_copied > 0)
+		mptcp_cleanup_rbuf(msk, read_copied);
+}
+
 static void mptcp_do_fastclose(struct sock *sk)
 {
 	struct mptcp_subflow_context *subflow, *tmp;
@@ -3240,6 +3255,9 @@ static void mptcp_worker(struct work_struct *work)
 	if (test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
 		__mptcp_retrans(sk);
 
+	if (test_and_clear_bit(MPTCP_WORK_READ_COMPLETE, &msk->flags))
+		mptcp_read_complete(sk);
+
 	fail_tout = msk->first ? READ_ONCE(mptcp_subflow_ctx(msk->first)->fail_tout) : 0;
 	if (fail_tout && time_after(jiffies, fail_tout))
 		mptcp_mp_fail_no_response(msk);
@@ -3283,6 +3301,7 @@ static void __mptcp_init_sock(struct sock *sk)
 	msk->last_data_sent = tcp_jiffies32;
 	msk->last_data_recv = tcp_jiffies32;
 	msk->last_ack_recv = tcp_jiffies32;
+	msk->read_copied = 0;
 
 	mptcp_pm_data_init(msk);
 	spin_lock_init(&msk->fallback_lock);
@@ -3730,6 +3749,7 @@ static int mptcp_disconnect(struct sock *sk, int flags)
 	msk->fastclosing = 0;
 	msk->tfo_iasn = 0;
 	msk->rcvd_dummy_seq = false;
+	msk->read_copied = 0;
 	mptcp_init_rtt_est(msk);
 
 	/* for fallback's sake */
@@ -4652,7 +4672,7 @@ static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off)
 	struct sk_buff *skb;
 	u32 offset;
 
-	if (!list_empty(&msk->backlog_list))
+	if (sock_owned_by_user(sk) && !list_empty(&msk->backlog_list))
 		mptcp_move_skbs(sk);
 
 	while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
@@ -4669,6 +4689,7 @@ static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off)
 /*
  * Note:
  *	- It is assumed that the socket was locked by the caller.
+ *	- Can be invoked in BH scope.
  */
 static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
 			     sk_read_actor_t recv_actor, bool noack)
@@ -4710,11 +4731,18 @@ static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
 	if (noack)
 		goto out;
 
-	mptcp_rcv_space_adjust(msk, copied);
-
 	if (copied > 0) {
-		mptcp_recv_skb(sk, &offset);
-		mptcp_cleanup_rbuf(msk, copied);
+		if (sock_owned_by_user(sk)) {
+			/* Process context: inline cleanup */
+			mptcp_rcv_space_adjust(msk, copied);
+			mptcp_recv_skb(sk, &offset);
+			mptcp_cleanup_rbuf(msk, copied);
+		} else {
+			/* BH context: defer to worker */
+			msk->read_copied += copied;
+			set_bit(MPTCP_WORK_READ_COMPLETE, &msk->flags);
+			mptcp_schedule_work(sk);
+		}
 	}
 out:
 	return copied;
net/mptcp/protocol.h +2 -0
index 86634c04f499..4b4b591b1e55 100644
@@ -117,6 +117,7 @@
 #define MPTCP_FALLBACK_DONE	2
 #define MPTCP_WORK_CLOSE_SUBFLOW 3
 #define MPTCP_RTX_ENABLED	4
+#define MPTCP_WORK_READ_COMPLETE 5
 
 /* MPTCP socket release cb flags */
 #define MPTCP_PUSH_PENDING	1
@@ -328,6 +329,7 @@ struct mptcp_sock {
 	u32		last_data_sent;
 	u32		last_data_recv;
 	u32		last_ack_recv;
+	u32		read_copied;
 	unsigned long	timer_ival;
 	u32		token;
 	u64		tfo_iasn;
Signature · 1 line
2.55.0
#22 lore raw

Re: [PATCH mptcp-next v28 11/11] mptcp: defer read_sock cleanup to mptcp_worker

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The patch adds code that nothing in the current tree can run.

--

mptcp: defer read_sock cleanup to mptcp_worker

This prepares __mptcp_read_sock() to be called from BH context for a future MPTCP TLS series. When the caller does not own the socket lock, it defers mptcp_rcv_space_adjust() and mptcp_cleanup_rbuf() to mptcp_worker() through msk->read_copied and MPTCP_WORK_READ_COMPLETE. It also gates the backlog flush in mptcp_recv_skb() on sock_owned_by_user().

Quoted 4 lines
net/mptcp/protocol.c +0 -0
index 50ae8cdec8a4a..6988f6128368e 100644

[ ... ]

Quoted 8 lines
net/mptcp/protocol.c +1 -0
@@ -4669,6 +4689,7 @@ static struct sk_buff *mptcp_recv_skb(struct sock *sk, u32 *off)
 /*
  * Note:
  *	- It is assumed that the socket was locked by the caller.
+ *	- Can be invoked in BH scope.
  */
 static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
 			     sk_read_actor_t recv_actor, bool noack)

[ ... ]

Quoted 10 lines
net/mptcp/protocol.c +1 -4
@@ -4710,11 +4731,18 @@ static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,
 	if (noack)
 		goto out;
 
-	mptcp_rcv_space_adjust(msk, copied);
-
 	if (copied > 0) {
-		mptcp_recv_skb(sk, &offset);
-		mptcp_cleanup_rbuf(msk, copied);
+		if (sock_owned_by_user(sk)) {

net/mptcp/protocol.c static int __mptcp_read_sock(struct sock *sk, read_descriptor_t *desc, · line 4745

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+			/* Process context: inline cleanup */
+			mptcp_rcv_space_adjust(msk, copied);
+			mptcp_recv_skb(sk, &offset);
+			mptcp_cleanup_rbuf(msk, copied);
+		} else {
+			/* BH context: defer to worker */
+			msk->read_copied += copied;
+			set_bit(MPTCP_WORK_READ_COMPLETE, &msk->flags);
+			mptcp_schedule_work(sk);
+		}

[Severity: Low]
This isn't a bug, but can this else branch be reached by anything in the current tree?

The only direct caller of __mptcp_read_sock() is mptcp_read_sock(). That is reached from mptcp_splice_read() and from the generic proto_ops->read_sock users. Each of them either holds lock_sock() or cannot attach to an MPTCP socket:

  mptcp_splice_read()  - holds lock_sock()
  nvme_tcp_try_recv()  - calls lock_sock() before ops->read_sock()
  svc_tcp_read_msg()   - calls lock_sock() before ops->read_sock()
  strparser BH users   - KCM rejects csk->sk_protocol != IPPROTO_TCP

So sock_owned_by_user(sk) looks to be always true here. That would make the BH branch, mptcp_read_complete(), the MPTCP_WORK_READ_COMPLETE bit, and the msk->read_copied init and reset in __mptcp_init_sock() and mptcp_disconnect() all unused for now.

The new "Can be invoked in BH scope" comment above __mptcp_read_sock() also describes something no current caller does.

The commit message says this is groundwork for the MPTCP TLS series. Would it make sense to merge this together with the first BH caller, so the deferred cleanup can be tested and reviewed against that caller's locking rules?

Quoted 3 lines
net/mptcp/protocol.c +0 -0
 	}
 out:
 	return copied;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794810809.git.tanggeliang%40kylinos.cn
#23 lore raw
In reply to Geliang Tang

Hi Geliang,

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): Success! ✅
- 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: 1 failed test(s): error:sched-mem-pressure - Critical: mptcp-perf/01-Test_1/client.log: 1 Call Trace(s) - Critical: sched-baseline/15-slow_primary_upload/server.log: 1 Call Trace(s) - Critical: sched-baseline/16-balanced_upload/server.log: 1 Call Trace(s) - Critical: sched-baseline/18-balanced_parallel_4/client.log: 1 Call Trace(s) - Critical: sched-mem-pressure/03-bloated_slow_secondary_receiver_tcp_mem_parallel_99/client.log: 1 Call Trace(s) - Critical: sched-mem-pressure/09-balanced_both_tcp_mem_parallel_10/client.log: 1 Call Trace(s) - Critical: sched-weak-secondary/01-weak_secondary/client.log: 1 Call Trace(s) ❌
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/43820031687

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

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)