Skip to contentSkip to conversation

Rework ctrl_pf pointer usage in struct ice_adapter

PATCH Superseded Sergey Temerkhanov 2 participants 7 messages Jun 1 – Jul 13

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Rework ctrl_pf pointer usage in struct ice_adapter

Rework usage of the control PF pointer in struct ice_adapter, so that it is always has a consistent state, since it is global for an adapter. Utilize RCU for reading the pointer value and atomic operations for changing it. Zero out the ctrl_pf pointer when the control PF is removed.

Previous version:
https://lists.osuosl.org/pipermail/intel-wired-lan/Week-of-Mon-20260511/055077.html

v1->v2: Rebase onto the latest dev-queue branch
v2->v3: Cosmetic fixes

Sergey Temerkhanov (3):
ice: Convert ctrl_pf pointer in struct ice_adapter to RCU
ice: Zero out the PTP control PF pointer at ice_adapter cleanup
ice: Cache struct ice_hw pointer for split register reads

 drivers/net/ethernet/intel/ice/ice.h         |  10 +-
 drivers/net/ethernet/intel/ice/ice_adapter.h |   2 +-
 drivers/net/ethernet/intel/ice/ice_ptp.c     | 116 +++++++++++++------
 drivers/net/ethernet/intel/ice/ice_ptp_hw.c  |  26 ++++-
 4 files changed, 114 insertions(+), 40 deletions(-)

base-commit: 03d0beff9856a569d0da91ea0ccdc28045c2b2d0

Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/3 ice: Convert ctrl_pf pointer in struct ice_adapter to RCU

Use RCU to ensure the consistent state of the control PF global pointer contained in struct ice_adapter. Enforce RCU usage on the callers.

Fix a potential invalid pointer return due a TOCTOU issue

Fixes e2193f9f9ec9 ("ice: enable timesync operation on 2xNAC E825 devices")
---
 drivers/net/ethernet/intel/ice/ice.h         | 10 +-
 drivers/net/ethernet/intel/ice/ice_adapter.h |  2 +-
 drivers/net/ethernet/intel/ice/ice_ptp.c     | 99 +++++++++++++-------
 drivers/net/ethernet/intel/ice/ice_ptp_hw.c  |  9 ++
 4 files changed, 84 insertions(+), 36 deletions(-)
drivers/net/ethernet/intel/ice/ice.h +8 -2
index f9a43daf04fe..c4ae5f9e095c 100644
@@ -40,6 +40,7 @@
 #include <linux/cpu_rmap.h>
 #include <linux/dim.h>
 #include <linux/gnss.h>
+#include <linux/rcupdate.h>
 #include <net/pkt_cls.h>
 #include <net/pkt_sched.h>
 #include <net/tc_act/tc_mirred.h>
@@ -1160,14 +1161,19 @@ static inline bool ice_pf_src_tmr_owned(struct ice_pf *pf)
  * ice_get_primary_hw - Get pointer to primary ice_hw structure
  * @pf: pointer to PF structure
  *
+ * The function must be called from an RCU read-side critical section.
+ * hw is embedded in struct ice_pf, so it is protected by the RCU.
+ *
  * Return: A pointer to ice_hw structure with access to timesync
  * register space.
  */
 static inline struct ice_hw *ice_get_primary_hw(struct ice_pf *pf)
 {
-	if (!pf->adapter->ctrl_pf)
+	struct ice_pf *ctrl_pf = rcu_dereference(pf->adapter->ctrl_pf);
+
+	if (!ctrl_pf)
 		return &pf->hw;
 	else
-		return &pf->adapter->ctrl_pf->hw;
+		return &ctrl_pf->hw;
 }
 #endif /* _ICE_H_ */
drivers/net/ethernet/intel/ice/ice_adapter.h +1 -1
index e95266c7f20b..349d49d57f11 100644
@@ -42,7 +42,7 @@ struct ice_adapter {
 	/* For access to GLCOMM_QTX_CNTX_CTL register */
 	spinlock_t txq_ctx_lock;
 
-	struct ice_pf *ctrl_pf;
+	struct ice_pf __rcu *ctrl_pf;
 	struct ice_port_list ports;
 	u64 index;
 };
drivers/net/ethernet/intel/ice/ice_ptp.c +66 -33

151 lines not on this page · raw message

drivers/net/ethernet/intel/ice/ice_ptp_hw.c +9 -0
index 40181431f4ca..9ea72d5f59cc 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (C) 2021, Intel Corporation. */
 
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/iopoll.h>
 #include "ice_common.h"
@@ -351,6 +352,8 @@ void ice_ptp_src_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd)
 	struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
 	u32 cmd_val = ice_ptp_tmr_cmd_to_src_reg(hw, cmd);
 
+	guard(rcu)();
+
 	if (!ice_is_primary(hw))
 		hw = ice_get_primary_hw(pf);
 
@@ -383,6 +386,8 @@ static void ice_ptp_exec_tmr_cmd(struct ice_hw *hw)
 			dev_warn(ice_hw_to_dev(hw), "Failed to flush SBQ: %d\n", err);
 	}
 
+	guard(rcu)();
+
 	if (!ice_is_primary(hw))
 		hw = ice_get_primary_hw(pf);
 
@@ -1952,6 +1957,8 @@ static int ice_read_phy_and_phc_time_eth56g(struct ice_hw *hw, u8 port,
 		zo = rd32(hw, GLTSYN_SHTIME_0(tmr_idx));
 		lo = rd32(hw, GLTSYN_SHTIME_L(tmr_idx));
 	} else {
+		guard(rcu)();
+
 		zo = rd32(ice_get_primary_hw(pf), GLTSYN_SHTIME_0(tmr_idx));
 		lo = rd32(ice_get_primary_hw(pf), GLTSYN_SHTIME_L(tmr_idx));
 	}
@@ -2121,6 +2128,8 @@ int ice_start_phy_timer_eth56g(struct ice_hw *hw, u8 port)
 		lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
 		hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
 	} else {
+		guard(rcu)();
+
 		lo = rd32(ice_get_primary_hw(pf), GLTSYN_INCVAL_L(tmr_idx));
 		hi = rd32(ice_get_primary_hw(pf), GLTSYN_INCVAL_H(tmr_idx));
 	}
Signature · 1 line
2.53.0
Tested-by on v3 1/3
#2 lore raw

RE: [Intel-wired-lan] [PATCH iwl-net v3 1/3] ice: Convert ctrl_pf pointer in struct ice_adapter to RCU

Quoted 15 lines

-----Original Message-----
From: Intel-wired-lan <[email protected]> On Behalf Of Sergey Temerkhanov
Sent: 01 June 2026 14:50
To: [email protected]
Cc: [email protected]
Subject: [Intel-wired-lan] [PATCH iwl-net v3 1/3] ice: Convert ctrl_pf pointer in struct ice_adapter to RCU

Use RCU to ensure the consistent state of the control PF global pointer contained in struct ice_adapter. Enforce RCU usage on the callers.

Fix a potential invalid pointer return due a TOCTOU issue

Fixes e2193f9f9ec9 ("ice: enable timesync operation on 2xNAC E825 devices")
Quoted 7 lines

---

drivers/net/ethernet/intel/ice/ice.h         | 10 +-
drivers/net/ethernet/intel/ice/ice_adapter.h |  2 +-
drivers/net/ethernet/intel/ice/ice_ptp.c     | 99 +++++++++++++-------
drivers/net/ethernet/intel/ice/ice_ptp_hw.c  |  9 ++
4 files changed, 84 insertions(+), 36 deletions(-)
#3 lore raw

PATCH v3 2/3 ice: Zero out the PTP control PF pointer at ice_adapter cleanup

In reply to Sergey Temerkhanov

Zero out the ctrl_pf pointer in ice_adapter when the control PF is removed. This prevents potential dangling pointer dereference when accessing PTP-related structures from other PFs of the same adapter.

---
 drivers/net/ethernet/intel/ice/ice_ptp.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/net/ethernet/intel/ice/ice_ptp.c +17 -0
index 348fc73607b5..5f14f58f4343 100644
@@ -3107,6 +3107,18 @@ static void ice_ptp_setup_adapter(struct ice_pf *pf)
 	rcu_assign_pointer(pf->adapter->ctrl_pf, pf);
 }
 
+static void ice_ptp_cleanup_adapter(struct ice_pf *pf)
+{
+	/* Zero out adapter->ctrl_pf pointer when the ctrl_pf itself
+	 * is being removed to prevent any secondary PFs from accessing
+	 * it after it is deleted.
+	 */
+	if (cmpxchg(&pf->adapter->ctrl_pf,
+		    (struct ice_pf __rcu *)pf, NULL) ==
+			(struct ice_pf __rcu *)pf)
+		synchronize_rcu();
+}
+
 static int ice_ptp_setup_pf(struct ice_pf *pf)
 {
 	struct ice_ptp *ptp = &pf->ptp;
@@ -3386,6 +3398,8 @@ void ice_ptp_init(struct ice_pf *pf)
 err_clean_pf:
 	mutex_destroy(&ptp->port.ps_lock);
 	ice_ptp_cleanup_pf(pf);
+
+	ice_ptp_cleanup_adapter(pf);
 err_exit:
 	/* If we registered a PTP clock, release it */
 	if (pf->ptp.clock) {
@@ -3414,6 +3428,7 @@ void ice_ptp_release(struct ice_pf *pf)
 	if (pf->ptp.state != ICE_PTP_READY) {
 		mutex_destroy(&pf->ptp.port.ps_lock);
 		ice_ptp_cleanup_pf(pf);
+		ice_ptp_cleanup_adapter(pf);
 		if (pf->ptp.clock) {
 			ptp_clock_unregister(pf->ptp.clock);
 			pf->ptp.clock = NULL;
@@ -3428,6 +3443,8 @@ void ice_ptp_release(struct ice_pf *pf)
 
 	ice_ptp_cleanup_pf(pf);
 
+	ice_ptp_cleanup_adapter(pf);
+
 	ice_ptp_release_tx_tracker(pf, &pf->ptp.port.tx);
 
 	ice_ptp_disable_all_extts(pf);
Signature · 1 line
2.53.0
Tested-by on v3 2/3
#4 lore raw

RE: [Intel-wired-lan] [PATCH iwl-net v3 2/3] ice: Zero out the PTP control PF pointer at ice_adapter cleanup

Quoted 17 lines

-----Original Message-----
From: Intel-wired-lan <[email protected]> On Behalf Of Sergey Temerkhanov
Sent: 01 June 2026 14:50
To: [email protected]
Cc: [email protected]
Subject: [Intel-wired-lan] [PATCH iwl-net v3 2/3] ice: Zero out the PTP control PF pointer at ice_adapter cleanup

Zero out the ctrl_pf pointer in ice_adapter when the control PF is removed.
This prevents potential dangling pointer dereference when accessing PTP-related structures from other PFs of the same adapter.

Quoted 4 lines

---
drivers/net/ethernet/intel/ice/ice_ptp.c | 17 +++++++++++++++++

1 file changed, 17 insertions(+)
#5 lore raw

PATCH v3 3/3 ice: Cache struct ice_hw pointer for split register reads

In reply to Sergey Temerkhanov

Cache the primary ice_hw pointer to ensure consistency between calls (both parts of a value will be read from the same NAC). ice_get_primary_hw() will never return NULL, but during the ctrl_pf cleanup there may be a case when one call will return the pointer to the ctrl_pf->hw and the subsequent one - to the pf->hw which generally are not the same.
Struct ice_hw is embedded in the struct ice_pf so it is protected by the same critical section - no additional synchronization is needed.

Fixes e2193f9f9ec9 ("ice: enable timesync operation on 2xNAC E825 devices")
---
 drivers/net/ethernet/intel/ice/ice_ptp_hw.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/net/ethernet/intel/ice/ice_ptp_hw.c +13 -4
index 9ea72d5f59cc..a2b7f709f3c0 100644
@@ -4,6 +4,7 @@
 #include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/iopoll.h>
+#include "ice.h"
 #include "ice_common.h"
 #include "ice_ptp_hw.h"
 #include "ice_ptp_consts.h"
@@ -1957,10 +1958,14 @@ static int ice_read_phy_and_phc_time_eth56g(struct ice_hw *hw, u8 port,
 		zo = rd32(hw, GLTSYN_SHTIME_0(tmr_idx));
 		lo = rd32(hw, GLTSYN_SHTIME_L(tmr_idx));
 	} else {
+		struct ice_hw *pri_hw;
+
 		guard(rcu)();
 
-		zo = rd32(ice_get_primary_hw(pf), GLTSYN_SHTIME_0(tmr_idx));
-		lo = rd32(ice_get_primary_hw(pf), GLTSYN_SHTIME_L(tmr_idx));
+		pri_hw = ice_get_primary_hw(pf);
+
+		zo = rd32(pri_hw, GLTSYN_SHTIME_0(tmr_idx));
+		lo = rd32(pri_hw, GLTSYN_SHTIME_L(tmr_idx));
 	}
 	*phc_time = (u64)lo << 32 | zo;
 
@@ -2128,10 +2133,14 @@ int ice_start_phy_timer_eth56g(struct ice_hw *hw, u8 port)
 		lo = rd32(hw, GLTSYN_INCVAL_L(tmr_idx));
 		hi = rd32(hw, GLTSYN_INCVAL_H(tmr_idx));
 	} else {
+		struct ice_hw *pri_hw;
+
 		guard(rcu)();
 
-		lo = rd32(ice_get_primary_hw(pf), GLTSYN_INCVAL_L(tmr_idx));
-		hi = rd32(ice_get_primary_hw(pf), GLTSYN_INCVAL_H(tmr_idx));
+		pri_hw = ice_get_primary_hw(pf);
+
+		lo = rd32(pri_hw, GLTSYN_INCVAL_L(tmr_idx));
+		hi = rd32(pri_hw, GLTSYN_INCVAL_H(tmr_idx));
 	}
 	incval = (u64)hi << 32 | lo;
 
Signature · 1 line
2.53.0
Tested-by on v3 3/3
#6 lore raw

RE: [Intel-wired-lan] [PATCH iwl-net v3 3/3] ice: Cache struct ice_hw pointer for split register reads

Quoted 12 lines

-----Original Message-----
From: Intel-wired-lan <[email protected]> On Behalf Of Sergey Temerkhanov
Sent: 01 June 2026 14:50
To: [email protected]
Cc: [email protected]
Subject: [Intel-wired-lan] [PATCH iwl-net v3 3/3] ice: Cache struct ice_hw pointer for split register reads

Cache the primary ice_hw pointer to ensure consistency between calls (both parts of a value will be read from the same NAC).
ice_get_primary_hw() will never return NULL, but during the ctrl_pf cleanup there may be a case when one call will return the pointer to the ctrl_pf->hw and the subsequent one - to the pf->hw which generally are not the same.
Struct ice_hw is embedded in the struct ice_pf so it is protected by the same critical section - no additional synchronization is needed.

Quoted 9 lines
Fixes e2193f9f9ec9 ("ice: enable timesync operation on 2xNAC E825 devices")

---
drivers/net/ethernet/intel/ice/ice_ptp_hw.c | 17 +++++++++++++----

1 file changed, 13 insertions(+), 4 deletions(-)