Skip to contentSkip to conversation

wifi: iwlwifi: ptp: Fix potential race condition in PTP removal

PATCH Superseded Junjie Cao 4 participants 9 messages Jan 15 – Mar 30

Subsystems (4)
lore
Share & tools
lore

Junjie Cao

author
#0 lore raw

PATCH 1/1 wifi: iwlwifi: ptp: Fix potential race condition in PTP removal

iwl_mvm_ptp_remove() and iwl_mld_ptp_remove() call cancel_delayed_work_sync() only after ptp_clock_unregister() and after partially clearing ptp_data state.

This creates a race where the delayed work (iwl_mvm_ptp_work / iwl_mld_ptp_work) can run while teardown is in progress and observe a partially modified PTP state. In addition, the work may re-arm itself, extending the teardown window and risking execution after driver resources have been released.

Move cancel_delayed_work_sync() before ptp_clock_unregister() to ensure the delayed work is fully stopped before any PTP cleanup begins. This follows the standard pattern used by other Intel PTP drivers such as e1000e, igb, ixgbe, and ice.

Fixes d1e879ec600f ("wifi: iwlwifi: add iwlmld sub-driver")
Fixes 1595ecce1cf3 ("wifi: iwlwifi: mvm: add support for PTP HW clock (PHC)")
---
 drivers/net/wireless/intel/iwlwifi/mld/ptp.c | 2 +-
 drivers/net/wireless/intel/iwlwifi/mvm/ptp.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/net/wireless/intel/iwlwifi/mld/ptp.c +1 -1
index 231920425c06..b40182320801 100644
@@ -319,10 +319,10 @@ void iwl_mld_ptp_remove(struct iwl_mld *mld)
 			       mld->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mld->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mld->ptp_data.dwork);
 		ptp_clock_unregister(mld->ptp_data.ptp_clock);
 		mld->ptp_data.ptp_clock = NULL;
 		mld->ptp_data.last_gp2 = 0;
 		mld->ptp_data.wrap_counter = 0;
-		cancel_delayed_work_sync(&mld->ptp_data.dwork);
 	}
 }
drivers/net/wireless/intel/iwlwifi/mvm/ptp.c +1 -1
index 1da6260e238c..2b01ca36a1b5 100644
@@ -325,11 +325,11 @@ void iwl_mvm_ptp_remove(struct iwl_mvm *mvm)
 			       mvm->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mvm->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 		ptp_clock_unregister(mvm->ptp_data.ptp_clock);
 		mvm->ptp_data.ptp_clock = NULL;
 		memset(&mvm->ptp_data.ptp_clock_info, 0,
 		       sizeof(mvm->ptp_data.ptp_clock_info));
 		mvm->ptp_data.last_gp2 = 0;
-		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 	}
 }
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/1
#1 lore raw

+ Yao Zi and Benjamin Berg

Quoted 15 lines · Junjie Cao

iwl_mvm_ptp_remove() and iwl_mld_ptp_remove() call cancel_delayed_work_sync() only after ptp_clock_unregister() and after partially clearing ptp_data state.

This creates a race where the delayed work (iwl_mvm_ptp_work / iwl_mld_ptp_work) can run while teardown is in progress and observe a partially modified PTP state. In addition, the work may re-arm itself, extending the teardown window and risking execution after driver resources have been released.

Move cancel_delayed_work_sync() before ptp_clock_unregister() to ensure the delayed work is fully stopped before any PTP cleanup begins. This follows the standard pattern used by other Intel PTP drivers such as e1000e, igb, ixgbe, and ice.

Quoted 4 lines
Fixes d1e879ec600f ("wifi: iwlwifi: add iwlmld sub-driver")
Fixes 1595ecce1cf3 ("wifi: iwlwifi: mvm: add support for PTP HW clock (PHC)")

Thinking out loud: The two cited commits were introduced in the same upstream release - v6.4 - so from a backporting PoV it seems reasonable to address these issues in one patch. Though I do think it would be best to think of these as two things and thus warranting two patches.

That notwithstanding, the changes look good to me.

Context left intact below for the benefit of Yao Zi and Benjamin Berg.

Quoted 41 lines
---
 drivers/net/wireless/intel/iwlwifi/mld/ptp.c | 2 +-
 drivers/net/wireless/intel/iwlwifi/mvm/ptp.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/net/wireless/intel/iwlwifi/mld/ptp.c +1 -1
index 231920425c06..b40182320801 100644
@@ -319,10 +319,10 @@ void iwl_mld_ptp_remove(struct iwl_mld *mld)
 			       mld->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mld->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mld->ptp_data.dwork);
 		ptp_clock_unregister(mld->ptp_data.ptp_clock);
 		mld->ptp_data.ptp_clock = NULL;
 		mld->ptp_data.last_gp2 = 0;
 		mld->ptp_data.wrap_counter = 0;
-		cancel_delayed_work_sync(&mld->ptp_data.dwork);
 	}
 }
drivers/net/wireless/intel/iwlwifi/mvm/ptp.c +1 -1
index 1da6260e238c..2b01ca36a1b5 100644
@@ -325,11 +325,11 @@ void iwl_mvm_ptp_remove(struct iwl_mvm *mvm)
 			       mvm->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mvm->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 		ptp_clock_unregister(mvm->ptp_data.ptp_clock);
 		mvm->ptp_data.ptp_clock = NULL;
 		memset(&mvm->ptp_data.ptp_clock_info, 0,
 		       sizeof(mvm->ptp_data.ptp_clock_info));
 		mvm->ptp_data.last_gp2 = 0;
-		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 	}
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Junjie Cao
Quoted 15 lines · Junjie Cao

iwl_mvm_ptp_remove() and iwl_mld_ptp_remove() call cancel_delayed_work_sync() only after ptp_clock_unregister() and after partially clearing ptp_data state.

This creates a race where the delayed work (iwl_mvm_ptp_work / iwl_mld_ptp_work) can run while teardown is in progress and observe a partially modified PTP state. In addition, the work may re-arm itself, extending the teardown window and risking execution after driver resources have been released.

Move cancel_delayed_work_sync() before ptp_clock_unregister() to ensure the delayed work is fully stopped before any PTP cleanup begins. This follows the standard pattern used by other Intel PTP drivers such as e1000e, igb, ixgbe, and ice.

Quoted 4 lines
Fixes d1e879ec600f ("wifi: iwlwifi: add iwlmld sub-driver")
Fixes 1595ecce1cf3 ("wifi: iwlwifi: mvm: add support for PTP HW clock (PHC)")
#3 lore raw
In reply to Junjie Cao
Quoted 20 lines

-----Original Message-----
From: Cao, Junjie <[email protected]>
Sent: Thursday, January 15, 2026 6:15 PM
To: Korenblit, Miriam Rachel <[email protected]>; Berg, Johannes <[email protected]>; [email protected]; Richard Cochran <[email protected]>
Cc: Simon Horman <[email protected]>; [email protected]; linux- [email protected]; Ben Shimol, Yedidya <[email protected]>; Stern, Avraham <[email protected]>; Gabay, Daniel <[email protected]>; Prabhu, Krishnanand <[email protected]>; Coelho, Luciano <[email protected]>; Gregory Greenman <[email protected]>; [email protected]
Subject: [PATCH] wifi: iwlwifi: ptp: Fix potential race condition in PTP removal

iwl_mvm_ptp_remove() and iwl_mld_ptp_remove() call cancel_delayed_work_sync() only after ptp_clock_unregister() and after partially clearing ptp_data state.

Quoted 5 lines

This creates a race where the delayed work (iwl_mvm_ptp_work / iwl_mld_ptp_work) can run while teardown is in progress and observe a partially modified PTP state. In addition, the work may re-arm itself, extending the teardown window and risking execution after driver resources have been released.

Not sure I understand how is the fact that work re-arm itself is related/makes the bug worse?

Quoted 52 lines

Move cancel_delayed_work_sync() before ptp_clock_unregister() to ensure the delayed work is fully stopped before any PTP cleanup begins. This follows the standard pattern used by other Intel PTP drivers such as e1000e, igb, ixgbe, and ice.

Fixes d1e879ec600f ("wifi: iwlwifi: add iwlmld sub-driver")
Fixes 1595ecce1cf3 ("wifi: iwlwifi: mvm: add support for PTP HW clock (PHC)")
---
 drivers/net/wireless/intel/iwlwifi/mld/ptp.c | 2 +-
drivers/net/wireless/intel/iwlwifi/mvm/ptp.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/net/wireless/intel/iwlwifi/mld/ptp.c +1 -1
index 231920425c06..b40182320801 100644
@@ -319,10 +319,10 @@ void iwl_mld_ptp_remove(struct iwl_mld *mld)
 			       mld->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mld->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mld->ptp_data.dwork);
 		ptp_clock_unregister(mld->ptp_data.ptp_clock);
 		mld->ptp_data.ptp_clock = NULL;
 		mld->ptp_data.last_gp2 = 0;
 		mld->ptp_data.wrap_counter = 0;
-		cancel_delayed_work_sync(&mld->ptp_data.dwork);
 	}
 }
drivers/net/wireless/intel/iwlwifi/mvm/ptp.c +1 -2
index 1da6260e238c..2b01ca36a1b5 100644
@@ -325,11 +325,11 @@ void iwl_mvm_ptp_remove(struct iwl_mvm *mvm)
 			       mvm->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mvm->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 		ptp_clock_unregister(mvm->ptp_data.ptp_clock);
 		mvm->ptp_data.ptp_clock = NULL;
 		memset(&mvm->ptp_data.ptp_clock_info, 0,
 		       sizeof(mvm->ptp_data.ptp_clock_info));
 		mvm->ptp_data.last_gp2 = 0;
-		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 	}
 }
--

2.43.0

author
#4 lore raw

Hi Miri,

You're right -- cancel_delayed_work_sync() handles re-arming by design, and the work is scheduled with a 1-hour delay (IWL_PTP_WRAP_TIME = 3600 * HZ), so it cannot realistically fire again during the microsecond-scale teardown window.

The real issue is simply that the delayed work can execute between ptp_clock_unregister() and cancel_delayed_work_sync(), observing partially cleared PTP state.

I'll send a v2 with a corrected commit message, and also split the patch into two (one per sub-driver) as Simon suggested.

Thanks for the review!

Junjie

author
#5 lore raw

PATCH v2 1/2 wifi: iwlwifi: mvm: fix race condition in PTP removal

In reply to Korenblit, Miriam Rachel

iwl_mvm_ptp_remove() calls cancel_delayed_work_sync() only after ptp_clock_unregister() and clearing ptp_data state (ptp_clock, ptp_clock_info, last_gp2).

This creates a race where the delayed work iwl_mvm_ptp_work() can execute between ptp_clock_unregister() and cancel_delayed_work_sync(), observing partially cleared PTP state.

Move cancel_delayed_work_sync() before ptp_clock_unregister() to ensure the delayed work is fully stopped before any PTP cleanup begins.

Fixes 1595ecce1cf3 ("wifi: iwlwifi: mvm: add support for PTP HW clock (PHC)")
---
 drivers/net/wireless/intel/iwlwifi/mvm/ptp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/intel/iwlwifi/mvm/ptp.c +1 -1
index ad156b82eaa9..efb291ceb0e5 100644
@@ -323,11 +323,11 @@ void iwl_mvm_ptp_remove(struct iwl_mvm *mvm)
 			       mvm->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mvm->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 		ptp_clock_unregister(mvm->ptp_data.ptp_clock);
 		mvm->ptp_data.ptp_clock = NULL;
 		memset(&mvm->ptp_data.ptp_clock_info, 0,
 		       sizeof(mvm->ptp_data.ptp_clock_info));
 		mvm->ptp_data.last_gp2 = 0;
-		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 	}
 }
Signature · 1 line
2.48.1
author
#6 lore raw

PATCH v2 2/2 wifi: iwlwifi: mld: fix race condition in PTP removal

iwl_mld_ptp_remove() calls cancel_delayed_work_sync() only after ptp_clock_unregister() and clearing ptp_data state (ptp_clock, last_gp2, wrap_counter).

This creates a race where the delayed work iwl_mld_ptp_work() can execute between ptp_clock_unregister() and cancel_delayed_work_sync(), observing partially cleared PTP state.

Move cancel_delayed_work_sync() before ptp_clock_unregister() to ensure the delayed work is fully stopped before any PTP cleanup begins.

Fixes d1e879ec600f ("wifi: iwlwifi: add iwlmld sub-driver")
---
 drivers/net/wireless/intel/iwlwifi/mld/ptp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/intel/iwlwifi/mld/ptp.c +1 -1
index 231920425c06..b40182320801 100644
@@ -319,10 +319,10 @@ void iwl_mld_ptp_remove(struct iwl_mld *mld)
 			       mld->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mld->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mld->ptp_data.dwork);
 		ptp_clock_unregister(mld->ptp_data.ptp_clock);
 		mld->ptp_data.ptp_clock = NULL;
 		mld->ptp_data.last_gp2 = 0;
 		mld->ptp_data.wrap_counter = 0;
-		cancel_delayed_work_sync(&mld->ptp_data.dwork);
 	}
 }
Signature · 1 line
2.48.1
#7 lore raw

RE: [PATCH v2 1/2] wifi: iwlwifi: mvm: fix race condition in PTP removal

In reply to Junjie Cao
Quoted 16 lines

-----Original Message-----
From: Cao, Junjie <[email protected]>
Sent: Thursday, February 12, 2026 2:51 PM
To: Korenblit, Miriam Rachel <[email protected]>; Berg, Johannes <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Stern, Avraham <[email protected]>; Gabay, Daniel <[email protected]>; Prabhu, Krishnanand <[email protected]>; Coelho, Luciano <[email protected]>; [email protected]; [email protected]; Cao, Junjie <[email protected]>; Vadim Fedorenko <[email protected]>
Subject: [PATCH v2 1/2] wifi: iwlwifi: mvm: fix race condition in PTP removal

Quoted 7 lines

iwl_mvm_ptp_remove() calls cancel_delayed_work_sync() only after ptp_clock_unregister() and clearing ptp_data state (ptp_clock, ptp_clock_info, last_gp2).

This creates a race where the delayed work iwl_mvm_ptp_work() can execute between ptp_clock_unregister() and cancel_delayed_work_sync(), observing partially cleared PTP state.

But the work runs under the mvm mutex, and so does iwl_mvm_ptp_remove, so not sure how such a race can happen?

Quoted 33 lines

Move cancel_delayed_work_sync() before ptp_clock_unregister() to ensure the delayed work is fully stopped before any PTP cleanup begins.

Fixes 1595ecce1cf3 ("wifi: iwlwifi: mvm: add support for PTP HW clock (PHC)")
---
 drivers/net/wireless/intel/iwlwifi/mvm/ptp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/intel/iwlwifi/mvm/ptp.c +1 -2
index ad156b82eaa9..efb291ceb0e5 100644
@@ -323,11 +323,11 @@ void iwl_mvm_ptp_remove(struct iwl_mvm *mvm)
 			       mvm->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mvm->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 		ptp_clock_unregister(mvm->ptp_data.ptp_clock);
 		mvm->ptp_data.ptp_clock = NULL;
 		memset(&mvm->ptp_data.ptp_clock_info, 0,
 		       sizeof(mvm->ptp_data.ptp_clock_info));
 		mvm->ptp_data.last_gp2 = 0;
-		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 	}
 }
--

2.48.1

#8 lore raw

RE: [PATCH v2 1/2] wifi: iwlwifi: mvm: fix race condition in PTP removal

Quoted 39 lines

-----Original Message-----
From: Korenblit, Miriam Rachel
Sent: Monday, March 30, 2026 9:44 AM
To: Cao, Junjie <[email protected]>; Berg, Johannes <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Stern, Avraham <[email protected]>; Gabay, Daniel <[email protected]>; Prabhu, Krishnanand <[email protected]>; Coelho, Luciano <[email protected]>; [email protected]; [email protected]; Vadim Fedorenko <[email protected]>
Subject: RE: [PATCH v2 1/2] wifi: iwlwifi: mvm: fix race condition in PTP removal

Quoted 21 lines

-----Original Message-----
From: Cao, Junjie <[email protected]>
Sent: Thursday, February 12, 2026 2:51 PM
To: Korenblit, Miriam Rachel <[email protected]>; Berg, Johannes <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Stern, Avraham <[email protected]>; Gabay, Daniel <[email protected]>; Prabhu, Krishnanand <[email protected]>; Coelho, Luciano <[email protected]>; [email protected]; [email protected]; Cao, Junjie <[email protected]>; Vadim Fedorenko <[email protected]>
Subject: [PATCH v2 1/2] wifi: iwlwifi: mvm: fix race condition in PTP removal

iwl_mvm_ptp_remove() calls cancel_delayed_work_sync() only after ptp_clock_unregister() and clearing ptp_data state (ptp_clock, ptp_clock_info, last_gp2).

Quoted 6 lines
Quoted 3 lines

This creates a race where the delayed work iwl_mvm_ptp_work() can execute between ptp_clock_unregister() and cancel_delayed_work_sync(), observing partially cleared PTP state.

But the work runs under the mvm mutex, and so does iwl_mvm_ptp_remove, so not sure how such a race can happen?

Oops, err. It does not run under the mutex. Sorry.
Still note that even with the zeroed data no harm will be done by the worker.

Will apply the patch.

Quoted 34 lines
Quoted 34 lines

Move cancel_delayed_work_sync() before ptp_clock_unregister() to ensure the delayed work is fully stopped before any PTP cleanup begins.

Fixes 1595ecce1cf3 ("wifi: iwlwifi: mvm: add support for PTP HW clock

(PHC)")

---
 drivers/net/wireless/intel/iwlwifi/mvm/ptp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/intel/iwlwifi/mvm/ptp.c +1 -2
index ad156b82eaa9..efb291ceb0e5 100644
@@ -323,11 +323,11 @@ void iwl_mvm_ptp_remove(struct iwl_mvm *mvm)
 			       mvm->ptp_data.ptp_clock_info.name,
 			       ptp_clock_index(mvm->ptp_data.ptp_clock));
 
+		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 		ptp_clock_unregister(mvm->ptp_data.ptp_clock);
 		mvm->ptp_data.ptp_clock = NULL;
 		memset(&mvm->ptp_data.ptp_clock_info, 0,
 		       sizeof(mvm->ptp_data.ptp_clock_info));
 		mvm->ptp_data.last_gp2 = 0;
-		cancel_delayed_work_sync(&mvm->ptp_data.dwork);
 	}
 }
--

2.48.1