mptcp: pm: fix data race in add_addr timer callback
Subsystems (2)
PATCH v2 1/1 mptcp: pm: fix data race in add_addr timer callback
From: luoqing <[email protected]>
The timer callback reads entry->retrans_times outside pm.lock to decide whether to call mptcp_pm_subflow_established(). Since mptcp_pm_announced_del_timer() can concurrently set retrans_times = ADD_ADDR_RETRANS_MAX under pm.lock, a race condition exists.
Use a local 'completed' flag set inside pm.lock only when the timer callback itself increments retrans_times to ADD_ADDR_RETRANS_MAX. This ensures that mptcp_pm_subflow_established() is only called when the retransmission naturally exhausts.
--- net/mptcp/pm.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
net/mptcp/pm.c +13 -6
@@ -380,6 +380,8 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)struct mptcp_sock *msk = entry->sock;struct sock *sk = (struct sock *)msk;unsigned int timeout =0 ; +bool completed = false; +u8 retrans_times;pr_debug ("msk=%p\n" , msk);
@@ -399,27 +401,32 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)spin_lock_bh (&msk->pm.lock); + retrans_times =READ_ONCE (entry->retrans_times); +/* The cancel path (mptcp_pm_announced_del_timer()) can race with this * callback. Once cancel updates retrans_times to MAX, suppress further * retransmissions here. If this callback acquires pm.lock first, one * final transmit attempt is still possible. */ -if (entry->retrans_times <ADD_ADDR_RETRANS_MAX && +if (retrans_times <ADD_ADDR_RETRANS_MAX && !mptcp_pm_should_add_signal_addr (msk)) {pr_debug ("retransmit ADD_ADDR id=%d\n" , entry->addr.id);mptcp_pm_announce_addr (msk, &entry->addr, false);mptcp_pm_add_addr_send_ack (msk); - entry->retrans_times++; + retrans_times++; +WRITE_ONCE (entry->retrans_times, retrans_times); } -if (entry->retrans_times <ADD_ADDR_RETRANS_MAX ) - timeout <<= entry->retrans_times; -else +if (retrans_times <ADD_ADDR_RETRANS_MAX ) + timeout <<= retrans_times; +else { timeout =0 ; + completed = true; + }spin_unlock_bh (&msk->pm.lock); -if (entry->retrans_times ==ADD_ADDR_RETRANS_MAX ) +if (completed)mptcp_pm_subflow_established (msk); out:
Signature · 1 line
2.25.1
Hi luoqing,
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): Unstable: 1 failed test(s): packetdrill_fastclose ⚠️
- 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! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/29561487863
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/1abbe9ec15da
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1129292
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-normalFor 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)
Quoted 52 lines · luoqing
From: luoqing <[email protected]>
The timer callback reads
entry->retrans_timesoutside pm.lock to decide whether to callmptcp_pm_subflow_established(). Sincemptcp_pm_announced_del_timer()can concurrently setretrans_times=ADD_ADDR_RETRANS_MAXunder pm.lock, a race condition exists.Use a local 'completed' flag set inside pm.lock only when the timer callback itself increments
retrans_timestoADD_ADDR_RETRANS_MAX. This ensures thatmptcp_pm_subflow_established()is only called when the retransmission naturally exhausts.Signed-off-by luoqing <[email protected]>--- net/mptcp/pm.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)net/mptcp/pm.c +7
-2@@ -380,6 +380,8 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)struct mptcp_sock *msk = entry->sock;struct sock *sk = (struct sock *)msk;unsigned int timeout =0 ; +bool completed = false; +u8 retrans_times;pr_debug ("msk=%p\n" , msk);@@ -399,27 +401,32 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)spin_lock_bh (&msk->pm.lock); + retrans_times =READ_ONCE (entry->retrans_times); +/* The cancel path (mptcp_pm_announced_del_timer()) can race with this * callback. Once cancel updates retrans_times to MAX, suppress further * retransmissions here. If this callback acquires pm.lock first, one * final transmit attempt is still possible. */ -if (entry->retrans_times <ADD_ADDR_RETRANS_MAX && +if (retrans_times <ADD_ADDR_RETRANS_MAX && !mptcp_pm_should_add_signal_addr (msk)) {pr_debug ("retransmit ADD_ADDR id=%d\n" , entry->addr.id);mptcp_pm_announce_addr (msk, &entry->addr, false);mptcp_pm_add_addr_send_ack (msk); - entry->retrans_times++; + retrans_times++; +WRITE_ONCE (entry->retrans_times, retrans_times); }
net/mptcp/pm.c static void mptcp_pm_add_addr_timer(struct timer_list *timer) · line 425
Quoted 10 lines
net/mptcp/pm.c +5
-3-if (entry->retrans_times <ADD_ADDR_RETRANS_MAX ) - timeout <<= entry->retrans_times; -else +if (retrans_times <ADD_ADDR_RETRANS_MAX ) + timeout <<= retrans_times; +else { timeout =0 ; + completed = true; + }
Hi,
'checkpatch' has reported a 'checks' for this:
'''
CHECK: braces {} should be used on all arms of this statement #60: FILE: net/mptcp/pm.c:420:
'''
But I think a v3 is not necessary for such a minor change. The patch itself looks good to me!
It's okay to wait for other reviewers' feedback.
Thanks
Gang
Quoted 11 lines
net/mptcp/pm.c +1
-1spin_unlock_bh (&msk->pm.lock); -if (entry->retrans_times ==ADD_ADDR_RETRANS_MAX ) +if (completed)mptcp_pm_subflow_established (msk); out:Signature · 1 line
2.25.1
Matthieu Baerts
maintainerHi,
Thank you for sharing this patch.
I have similar comments than the ones I sent on:
https://lore.kernel.org/178410664361.3802972.16804879942885801851.b4-review@b4
Quoted 6 lines · luoqing
From: luoqing <[email protected]>
The timer callback reads
entry->retrans_timesoutside pm.lock to decide whether to callmptcp_pm_subflow_established(). Sincemptcp_pm_announced_del_timer()can concurrently setretrans_times=ADD_ADDR_RETRANS_MAXunder pm.lock, a race condition exists.
How did you find the bug? Do you have a reproducer or is it by analysing the code?
Were you assisted by a tool/LLM? If yes, please add the Assisted-by tag.
Quoted 4 lines
Use a local 'completed' flag set inside pm.lock only when the timer callback itself increments
retrans_timestoADD_ADDR_RETRANS_MAX. This ensures thatmptcp_pm_subflow_established()is only called when the retransmission naturally exhausts.
A fix should have a Fixes tag, please add one.
Quoted 1 line
Signed-off-by luoqing <[email protected]>
For legal reasons, you are supposed to put your full name. Having only one "word" for your full name, without capital letters looks wrong, no?
net/mptcp/pm.c static void mptcp_pm_add_addr_timer(struct timer_list *timer) · line 384
Quoted 10 lines
net/mptcp/pm.c +2
-0@@ -380,6 +380,8 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)struct mptcp_sock *msk = entry->sock;struct sock *sk = (struct sock *)msk;unsigned int timeout =0 ; +bool completed = false; +u8 retrans_times;
Do you need two new variables? One should be enough.
net/mptcp/pm.c static void mptcp_pm_add_addr_timer(struct timer_list *timer) · line 404
Quoted 8 lines
net/mptcp/pm.c +1
-0pr_debug ("msk=%p\n" , msk);@@ -399,27 +401,32 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)spin_lock_bh (&msk->pm.lock); + retrans_times =READ_ONCE (entry->retrans_times);
Why do you require READ_ONCE() here? Can retrans_times be modified without the pm lock?
Quoted 5 lines
net/mptcp/pm.c +1
-0+/* The cancel path (mptcp_pm_announced_del_timer()) can race with this * callback. Once cancel updates retrans_times to MAX, suppress further * retransmissions here. If this callback acquires pm.lock first, one * final transmit attempt is still possible.
net/mptcp/pm.c static void mptcp_pm_add_addr_timer(struct timer_list *timer) · line 417
Quoted 10 lines
net/mptcp/pm.c +3
-2*/ -if (entry->retrans_times <ADD_ADDR_RETRANS_MAX && +if (retrans_times <ADD_ADDR_RETRANS_MAX && !mptcp_pm_should_add_signal_addr (msk)) {pr_debug ("retransmit ADD_ADDR id=%d\n" , entry->addr.id);mptcp_pm_announce_addr (msk, &entry->addr, false);mptcp_pm_add_addr_send_ack (msk); - entry->retrans_times++; + retrans_times++; +WRITE_ONCE (entry->retrans_times, retrans_times);
Same here, why WRITE_ONCE()?
net/mptcp/pm.c static void mptcp_pm_add_addr_timer(struct timer_list *timer) · line 425
Quoted 11 lines
net/mptcp/pm.c +5
-3} -if (entry->retrans_times <ADD_ADDR_RETRANS_MAX ) - timeout <<= entry->retrans_times; -else +if (retrans_times <ADD_ADDR_RETRANS_MAX ) + timeout <<= retrans_times; +else { timeout =0 ; + completed = true; + }
Either you have:
completed = entry->retrans_times >= ADD_ADDR_RETRANS_MAX;
if (!completed)
timeout <<= retrans_times;
else
timeout = 0;(preferred)
or
if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
timeout <<= retrans_times;
else
timeout = 0;
retrans_times = entry->retrans_times;But I don't think you need to have both "completed" and "retrans_times". With only "completed", you can minimise this patch.
net/mptcp/pm.c static void mptcp_pm_add_addr_timer(struct timer_list *timer) · line 432
Quoted 8 lines
net/mptcp/pm.c +1
-1spin_unlock_bh (&msk->pm.lock); -if (entry->retrans_times ==ADD_ADDR_RETRANS_MAX ) +if (completed)mptcp_pm_subflow_established (msk); out:
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.