ice_ptp_lock() conditionally retains ctrl_pf_lock for read when it
successfully acquires the hardware semaphore. The matching unlock occurs
in ice_ptp_unlock(). Describe this cross-function handoff with context
analysis annotations so static analysis can verify callers.

The annotations are inert for now: drivers/net/ethernet/intel/ice/ does
not set CONTEXT_ANALYSIS := y, so CONFIG_WARN_CONTEXT_ANALYSIS does not
cover these files. Opting the directory in requires annotating the rest
of the ice locking first and is left for a follow-up.

Signed-off-by: Sergey Temerkhanov <[email protected]>
Reviewed-by: Przemyslaw Korba <[email protected]>
---
 drivers/net/ethernet/intel/ice/ice.h        | 12 ++++++++++
 drivers/net/ethernet/intel/ice/ice_ptp_hw.c | 26 +++++++++++++--------
 drivers/net/ethernet/intel/ice/ice_ptp_hw.h |  6 +++--
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/drivers/net/ethernet/intel/ice/ice.h 
b/drivers/net/ethernet/intel/ice/ice.h
index aa81b6db7502..b63dd40b606d 100644
--- a/drivers/net/ethernet/intel/ice/ice.h
+++ b/drivers/net/ethernet/intel/ice/ice.h
@@ -1180,4 +1180,16 @@ static inline struct ice_pf *ice_get_ctrl_pf(struct 
ice_pf *pf)
                rcu_dereference_check(pf->adapter->ctrl_pf,
                                      
lockdep_is_held(&pf->adapter->ctrl_pf_lock));
 }
+
+/* container_of() expands to a statement expression, which clang cannot parse
+ * inside a context analysis attribute argument, so open-code the cast here.
+ */
+#define ice_hw_ctrl_pf_lock(_hw)                                       \
+       (&((struct ice_pf *)((void *)(_hw) -                            \
+                            offsetof(struct ice_pf, 
hw)))->adapter->ctrl_pf_lock)
+
+bool ice_ptp_lock(struct ice_hw *hw)
+       __cond_acquires_shared(true, ice_hw_ctrl_pf_lock(hw));
+void ice_ptp_unlock(struct ice_hw *hw)
+       __releases_shared(ice_hw_ctrl_pf_lock(hw));
 #endif /* _ICE_H_ */
diff --git a/drivers/net/ethernet/intel/ice/ice_ptp_hw.c 
b/drivers/net/ethernet/intel/ice/ice_ptp_hw.c
index dde6bddd4ecf..13d171d8386a 100644
--- a/drivers/net/ethernet/intel/ice/ice_ptp_hw.c
+++ b/drivers/net/ethernet/intel/ice/ice_ptp_hw.c
@@ -5375,15 +5375,16 @@ static void ice_ptp_init_phy_e830(struct ice_ptp_hw 
*ptp)
 bool ice_ptp_lock(struct ice_hw *hw)
 {
        struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
+       struct ice_hw *pri_hw = hw;
        u32 hw_lock;
        int i;
 
-       down_read(&pf->adapter->ctrl_pf_lock);
+       down_read(ice_hw_ctrl_pf_lock(hw));
 
        if (!ice_is_primary(hw)) {
-               hw = ice_get_primary_hw(pf);
-               if (!hw) {
-                       up_read(&pf->adapter->ctrl_pf_lock);
+               pri_hw = ice_get_primary_hw(pf);
+               if (!pri_hw) {
+                       up_read(ice_hw_ctrl_pf_lock(hw));
                        return false;
                }
        }
@@ -5391,7 +5392,8 @@ bool ice_ptp_lock(struct ice_hw *hw)
 #define MAX_TRIES 15
 
        for (i = 0; i < MAX_TRIES; i++) {
-               hw_lock = rd32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id));
+               hw_lock = rd32(pri_hw,
+                              PFTSYN_SEM + (PFTSYN_SEM_BYTES * pri_hw->pf_id));
                hw_lock = hw_lock & PFTSYN_SEM_BUSY_M;
                if (hw_lock) {
                        /* Somebody is holding the lock */
@@ -5403,7 +5405,7 @@ bool ice_ptp_lock(struct ice_hw *hw)
        }
 
        if (hw_lock)
-               up_read(&pf->adapter->ctrl_pf_lock);
+               up_read(ice_hw_ctrl_pf_lock(hw));
 
        return !hw_lock;
 }
@@ -5418,14 +5420,18 @@ bool ice_ptp_lock(struct ice_hw *hw)
 void ice_ptp_unlock(struct ice_hw *hw)
 {
        struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
+       struct ice_hw *pri_hw = hw;
 
-       lockdep_assert_held(&pf->adapter->ctrl_pf_lock);
+       lockdep_assert_held(ice_hw_ctrl_pf_lock(hw));
 
+       /* ctrl_pf cannot be cleared while ctrl_pf_lock is held for read, so a
+        * successful ice_ptp_lock() guarantees a primary hw is still there.
+        */
        if (!ice_is_primary(hw))
-               hw = ice_get_primary_hw(pf);
+               pri_hw = ice_get_primary_hw(pf);
 
-       wr32(hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * hw->pf_id), 0);
-       up_read(&pf->adapter->ctrl_pf_lock);
+       wr32(pri_hw, PFTSYN_SEM + (PFTSYN_SEM_BYTES * pri_hw->pf_id), 0);
+       up_read(ice_hw_ctrl_pf_lock(hw));
 }
 
 /**
diff --git a/drivers/net/ethernet/intel/ice/ice_ptp_hw.h 
b/drivers/net/ethernet/intel/ice/ice_ptp_hw.h
index c607627350df..f8ed04f6943b 100644
--- a/drivers/net/ethernet/intel/ice/ice_ptp_hw.h
+++ b/drivers/net/ethernet/intel/ice/ice_ptp_hw.h
@@ -293,8 +293,10 @@ extern const struct ice_vernier_info_e82x 
e822_vernier[NUM_ICE_PTP_LNK_SPD];
 
 /* Device agnostic functions */
 u8 ice_get_ptp_src_clock_index(struct ice_hw *hw);
-bool ice_ptp_lock(struct ice_hw *hw);
-void ice_ptp_unlock(struct ice_hw *hw);
+/* ice_ptp_lock()/ice_ptp_unlock() are declared in ice.h, where struct ice_pf
+ * and struct ice_adapter are complete and ice_hw_ctrl_pf_lock() can be used
+ * in their context analysis annotations.
+ */
 void ice_ptp_src_cmd(struct ice_hw *hw, enum ice_ptp_tmr_cmd cmd);
 int ice_ptp_init_time(struct ice_hw *hw, u64 time);
 int ice_ptp_write_incval(struct ice_hw *hw, u64 incval);
-- 
2.53.0

Reply via email to