Skip to contentSkip to conversation

mptcp: pm: fix data race in add_addr timer callback

PATCH Superseded luoqing 3 participants 7 messages Jul 22 – Jul 23

Subsystems (2)
lore
Share & tools
lore

luoqing

author
#0 lore raw

PATCH v3 1/1 mptcp: pm: fix data race in add_addr timer callback

From: Qing Luo <[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 when retrans_times reaches ADD_ADDR_RETRANS_MAX. This ensures that mptcp_pm_subflow_established() is only called when the retransmission naturally exhausts.

Fixes 348d5c1dec60 ("mptcp: move to next addr when timeout")
---
 net/mptcp/pm.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/mptcp/pm.c +5 -2
index 6afd39aea110..f93fefbb727e 100644
@@ -380,6 +380,7 @@ 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;
 
 	pr_debug("msk=%p\n", msk);
 
@@ -414,12 +415,14 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)
 
 	if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
 		timeout <<= entry->retrans_times;
-	else
+	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
maintainer
#1 lore raw

Hi Qing,

Thank you for the new version.

Quoted 6 lines · luoqing

From: Qing Luo <[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.

Don't forget to reply to each question from previous reviews ;)

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 24 lines

Use a local 'completed' flag set inside pm.lock when retrans_times reaches ADD_ADDR_RETRANS_MAX. This ensures that mptcp_pm_subflow_established() is only called when the retransmission naturally exhausts.

Fixes 348d5c1dec60 ("mptcp: move to next addr when timeout")
---
 net/mptcp/pm.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/mptcp/pm.c +1 -0
index 6afd39aea110..f93fefbb727e 100644
@@ -380,6 +380,7 @@ 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;
 
 	pr_debug("msk=%p\n", msk);
 

net/mptcp/pm.c static void mptcp_pm_add_addr_timer(struct timer_list *timer) · line 421

Quoted 8 lines
net/mptcp/pm.c +3 -1
@@ -414,12 +415,14 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)
 
 	if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
 		timeout <<= entry->retrans_times;
-	else
+	else {
 		timeout = 0;
+		completed = true;
+	}

Here, you still have the checkpatch warning Gang reported:

CHECK: braces {} should be used on all arms of this statement

To avoid all these modifications, I think it would be better to have:

bool completed;

(...)

  completed = entry->retrans_times >= ADD_ADDR_RETRANS_MAX;
  if (!completed)
      timeout <<= entry->retrans_times;
  else
      timeout = 0;

Or maybe better with the opposite:

bool retransmit;

(...)

  retransmit = entry->retrans_times < ADD_ADDR_RETRANS_MAX;
  if (retransmit)
      timeout <<= entry->retrans_times;
  else
      timeout = 0;

(...)

  if (!retransmit)
     mptcp_pm_subflow_established(msk);

By doing that, you only need to modify the if conditions, not the "else" parts.

net/mptcp/pm.c static void mptcp_pm_add_addr_timer(struct timer_list *timer) · line 428

Quoted 8 lines
net/mptcp/pm.c +1 -1
 	spin_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.
author
#2 lore raw

PATCH v4 1/1 mptcp: pm: fix data race in add_addr timer callback

From: Qing Luo <[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 'retransmit' flag set inside pm.lock to capture whether retransmission is still possible. This ensures that mptcp_pm_subflow_established() is only called when the retransmission naturally exhausts.

Fixes 348d5c1dec60 ("mptcp: move to next addr when timeout")
---
 net/mptcp/pm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/mptcp/pm.c +4 -2
index 6afd39aea110..c71dcf887683 100644
@@ -380,6 +380,7 @@ 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 retransmit;
 
 	pr_debug("msk=%p\n", msk);
 
@@ -412,14 +413,15 @@ static void mptcp_pm_add_addr_timer(struct timer_list *timer)
 		entry->retrans_times++;
 	}
 
-	if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
+	retransmit = entry->retrans_times < ADD_ADDR_RETRANS_MAX;
+	if (retransmit)
 		timeout <<= entry->retrans_times;
 	else
 		timeout = 0;
 
 	spin_unlock_bh(&msk->pm.lock);
 
-	if (entry->retrans_times == ADD_ADDR_RETRANS_MAX)
+	if (!retransmit)
 		mptcp_pm_subflow_established(msk);
 
 out:
Signature · 4 lines
2.25.1
Hi,

Thank you for your review and suggestions. I'm very sorry for not replying to your question in time and causing you inconvenience. It's because some issues are difficult to explain, so I merely followed your suggestion to analyze and submit the code.
Quoted 6 lines

Don't forget to reply to each question from previous reviews 😉

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.

I discovered this potential issue while studying the new MPTCP path manager code. Through code analysis, I noticed that entry->retrans_times could be accessed both inside and outside the pm.lock, which appeared to create a race condition with mptcp_pm_announced_del_timer().

Unfortunately, I don’t have a reproducer for this issue at the moment, as it’s difficult to construct a test scenario that reliably triggers this specific race condition. I submitted the patch based on code analysis to prevent potential issues.

I used AI tools to help verify and analyze the code logic during my investigation. However, I’m not entirely certain about the extent to which I should acknowledge this assistance, so I didn’t add an Assisted-by tag in the current version. If you think it’s necessary, I’d be happy to add it.

Quoted 2 lines

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?

I apologize for the confusion. “Luoqing” is actually my full name in Chinese convention - “Luo” (罗) is my family name and “Qing” (庆) is my given name. In Chinese, we don’t typically capitalize individual characters or add spaces between them. However, I understand the concern about the formatting, and I’ve updated the Signed-off-by to “Qing Luo [email protected]” to follow the Western name order convention, which I hope is more appropriate.

Best regards,
luoqing

#3 lore raw

Hi Qing,

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): Unstable: 1 failed test(s): selftest_mptcp_connect_splice ⚠️
- 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/29977660387

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

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)

maintainer
#4 lore raw
In reply to luoqing

Hi luoqing,

Thank you for the v4 and the reply.

Please next time do not send a new version as a reply to a previous one, that's easier with a new thread and a link to the previous one.

Quoted 5 lines · luoqing
Quoted 4 lines

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 8 lines

I discovered this potential issue while studying the new MPTCP path manager code. Through code analysis, I noticed that entry->retrans_times could be accessed both inside and outside the pm.lock, which appeared to create a race condition with mptcp_pm_announced_del_timer(). Unfortunately, I don’t have a reproducer for this issue at the moment, as it’s difficult to construct a test scenario that reliably triggers this specific race condition. I submitted the patch based on code analysis to prevent potential issues.

To help categorise data race issues, these questions should be ideally answered in the commit message:

- Is it a potential issue (i.e. code analysis but never reproduced), or do we have a proof it can happen (i.e. KCSAN trace)

- If there is a proof, is it easy to reproduce? Is there a reliable reproducer?

- Could this issue cause damages?

With the increase number of issues reported by AI, potential issues without proofs might not need to be fixed, especially if it introduces a higher complexity. Having a reproducer or at least a calltrace would really help not to waste time on potentially unreproducible bug spot by AI. But AI could also help to find a reproducer (ideally using packetdrill).

Quoted 5 lines

I used AI tools to help verify and
analyze the code logic during my investigation. However, I’m not entirely certain about the extent to which I should acknowledge this assistance, so I didn’t add an Assisted-by tag in the current version. If you think it’s necessary, I’d be happy to add it.

OK, thank you for the explanation. There are ongoing discussions [1] to use this tag like this:

Assisted-by LLM # find issue, code, review, test

In your case, it would be this I suppose:

Assisted-by LLM # review

[1] https://lore.kernel.org/ksummit/[email protected]/

But that's still under discussions. In the meantime, it would help if you write this with word in the commit message, e.g.

I discovered this potential issue while studying the code. AI tools helped me to verify the issue can potentially happen.

(no need to send a v5, I will apply the v4)

Quoted 9 lines
Quoted 2 lines

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?

I apologize for the confusion. “Luoqing” is actually my full name in Chinese convention - “Luo” (罗) is my family name and “Qing” (庆) is my given name. In Chinese, we don’t typically capitalize individual characters or add spaces between them. However, I understand the concern about the formatting, and I’ve updated the Signed-off-by to “Qing Luo [email protected]” to follow the Western name order convention, which I hope is more appropriate.

Thank you for the explanation! I understand that conventions are different. I was asking because usually the "Western name order convention" is used, and the full name is required, not a nickname. To avoid confusions, even if I'm not a big fan of force people using a different convention, I think it is easier with the Western form, if that's OK for you.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
maintainer Reviewed-by on v4 1/1
#5 lore raw
In reply to luoqing

Hi luoqing,

Quoted 11 lines · luoqing

From: Qing Luo <[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 'retransmit' flag set inside pm.lock to capture whether retransmission is still possible. This ensures that mptcp_pm_subflow_established() is only called when the retransmission naturally exhausts.

Now in our tree:

New patches for t/upstream-net and t/upstream:
- f7a2f9a0e5f5: mptcp: pm: fix data race in add_addr timer callback
- Results: 7455a95067b7..e5d7ab308b5b (export-net)
- Results: 52ff22e1c540..2a68b4bc189c (export)

Tests are now in progress:

- export-net:
https://github.com/multipath-tcp/mptcp_net-next/commit/3aa4f7a736529c07204d57a41362869f9b777463/checks
- export:
https://github.com/multipath-tcp/mptcp_net-next/commit/15a03914501c72388780156b04fb56ca6b01c20e/checks

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#6 lore raw
In reply to luoqing

Hi Qing,

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! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/29908453593

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

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)