Skip to contentSkip to conversation

bnxt_en: replace PTP spinlock with seqlock

PATCH In review Vadim Fedorenko 5 participants 7 messages Oct 14, 2024 – Oct 18, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bnxt_en: replace PTP spinlock with seqlock

We can see high contention on ptp_lock while doing RX timestamping on high packet rates over several queues. Spinlock is not effecient to protect timecounter for RX timestamps when reads are the most usual operations and writes are only occasional. It's better to use seqlock in such cases.

---
 drivers/net/ethernet/broadcom/bnxt/bnxt.c     | 30 +++-----
 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c | 76 +++++++++----------
 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h | 26 +++++--
 3 files changed, 67 insertions(+), 65 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt.c +11 -19
index 6e422e24750a..0c1a52681822 100644
@@ -2255,9 +2255,7 @@ static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
 
-				spin_lock_bh(&ptp->ptp_lock);
-				ns = timecounter_cyc2time(&ptp->tc, ts);
-				spin_unlock_bh(&ptp->ptp_lock);
+				ns = bnxt_timecounter_cyc2time(ptp, ts);
 				memset(skb_hwtstamps(skb), 0,
 				       sizeof(*skb_hwtstamps(skb)));
 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
@@ -2757,17 +2755,18 @@ static int bnxt_async_event_process(struct bnxt *bp,
 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
 			if (BNXT_PTP_USE_RTC(bp)) {
 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
+				unsigned long flags;
 				u64 ns;
 
 				if (!ptp)
 					goto async_event_process_exit;
 
-				spin_lock_bh(&ptp->ptp_lock);
+				write_seqlock_irqsave(&ptp->ptp_lock, flags);
 				bnxt_ptp_update_current_time(bp);
 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
 				       BNXT_PHC_BITS) | ptp->current_time);
 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
-				spin_unlock_bh(&ptp->ptp_lock);
+				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
 			}
 			break;
 		}
@@ -13493,13 +13492,9 @@ static void bnxt_force_fw_reset(struct bnxt *bp)
 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
 		return;
 
-	if (ptp) {
-		spin_lock_bh(&ptp->ptp_lock);
-		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
-		spin_unlock_bh(&ptp->ptp_lock);
-	} else {
-		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
-	}
+	set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
+	/* Make sure TS reader can see RESET bit set */
+	smp_mb__after_atomic();
 	bnxt_fw_reset_close(bp);
 	wait_dsecs = fw_health->master_func_wait_dsecs;
 	if (fw_health->primary) {
@@ -13560,13 +13555,10 @@ void bnxt_fw_reset(struct bnxt *bp)
 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
 		int n = 0, tmo;
 
-		if (ptp) {
-			spin_lock_bh(&ptp->ptp_lock);
-			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
-			spin_unlock_bh(&ptp->ptp_lock);
-		} else {
-			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
-		}
+		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
+		/* Make sure TS reader can see RESET bit set */
+		smp_mb__after_atomic();
+
 		if (bp->pf.active_vfs &&
 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
 			n = bnxt_get_registered_vfs(bp);
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +36 -40

228 lines not on this page · raw message

drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h +20 -6
index a9a2f9a18c9c..103eff803a3b 100644
@@ -102,7 +102,7 @@ struct bnxt_ptp_cfg {
 	struct timecounter	tc;
 	struct bnxt_pps		pps_info;
 	/* serialize timecounter access */
-	spinlock_t		ptp_lock;
+	seqlock_t		ptp_lock;
 	/* serialize ts tx request queuing */
 	spinlock_t		ptp_tx_lock;
 	u64			current_time;
@@ -146,11 +146,13 @@ struct bnxt_ptp_cfg {
 };
 
 #if BITS_PER_LONG == 32
-#define BNXT_READ_TIME64(ptp, dst, src)		\
-do {						\
-	spin_lock_bh(&(ptp)->ptp_lock);		\
-	(dst) = (src);				\
-	spin_unlock_bh(&(ptp)->ptp_lock);	\
+#define BNXT_READ_TIME64(ptp, dst, src)			\
+do {							\
+	unsigned int seq;				\
+	do {						\
+		seq = read_seqbegin(&(ptp)->ptp_lock);	\
+		(dst) = (src);				\
+	while (read_seqretry(&(ptp)->ptp_lock, seq);	\
 } while (0)
 #else
 #define BNXT_READ_TIME64(ptp, dst, src)		\
@@ -180,4 +182,16 @@ void bnxt_ptp_rtc_timecounter_init(struct bnxt_ptp_cfg *ptp, u64 ns);
 int bnxt_ptp_init_rtc(struct bnxt *bp, bool phc_cfg);
 int bnxt_ptp_init(struct bnxt *bp, bool phc_cfg);
 void bnxt_ptp_clear(struct bnxt *bp);
+static inline u64 bnxt_timecounter_cyc2time(struct bnxt_ptp_cfg *ptp, u64 ts)
+{
+	unsigned int seq;
+	u64 ns;
+
+	do {
+		seq = read_seqbegin(&ptp->ptp_lock);
+		ns = timecounter_cyc2time(&ptp->tc, ts);
+	} while (read_seqretry(&ptp->ptp_lock, seq));
+
+	return ns;
+}
 #endif
Signature · 1 line
2.43.5
maintainer
#1 lore raw

drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c static int bnxt_ptp_adjtime(struct ptp_clock_info *ptp_info, s64 delta) · line 212

Quoted 5 lines · On Mon, 14 Oct 2024 16:29:47 -0700 Vadim Fedorenko wrote:
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +2 -2
-	spin_lock_bh(&ptp->ptp_lock);
+	write_seqlock_irqsave(&ptp->ptp_lock, flags);
 	timecounter_adjtime(&ptp->tc, delta);
-	spin_unlock_bh(&ptp->ptp_lock);
+	write_sequnlock_irqrestore(&ptp->ptp_lock, flags);

I think when you adjtime / adjfine (IOW on all the write path) you still need the spin lock. But in addition also the seq lock. And then the read path can take just the seq lock.

This will also remove any uncertainty about the bit ops.

#2 lore raw
Quoted 10 lines · Jakub Kicinski

drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c static int bnxt_ptp_adjtime(struct ptp_clock_info *ptp_info, s64 delta) · line 212

Quoted 5 lines · On Mon, 14 Oct 2024 16:29:47 -0700 Vadim Fedorenko wrote:
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +2 -2
-	spin_lock_bh(&ptp->ptp_lock);
+	write_seqlock_irqsave(&ptp->ptp_lock, flags);
  	timecounter_adjtime(&ptp->tc, delta);
-	spin_unlock_bh(&ptp->ptp_lock);
+	write_sequnlock_irqrestore(&ptp->ptp_lock, flags);

I think when you adjtime / adjfine (IOW on all the write path) you still need the spin lock. But in addition also the seq lock. And then the read path can take just the seq lock.

I think there is a spinlock in seqlock_t which is used to prevent multiple writers.

Quoted 1 line

This will also remove any uncertainty about the bit ops.

Should I use read_seqlock_excl_bh()/write_seqlock_bh() for the bit ops then?

maintainer
#3 lore raw
Quoted 6 lines · On Tue, 15 Oct 2024 11:25:00 +0100 Vadim Fedorenko wrote:
Quoted 3 lines

I think when you adjtime / adjfine (IOW on all the write path) you still need the spin lock. But in addition also the seq lock. And then the read path can take just the seq lock.

I think there is a spinlock in seqlock_t which is used to prevent multiple writers.

My bad.

Quoted 4 lines
Quoted 1 line

This will also remove any uncertainty about the bit ops.

Should I use read_seqlock_excl_bh()/write_seqlock_bh() for the bit ops then?

Yup, modulo what Micheal said in the other leg of the thread, but SGTM.

maintainer
#4 lore raw
In reply to Vadim Fedorenko
Quoted 9 lines · Vadim Fedorenko

We can see high contention on ptp_lock while doing RX timestamping on high packet rates over several queues. Spinlock is not effecient to protect timecounter for RX timestamps when reads are the most usual operations and writes are only occasional. It's better to use seqlock in such cases.

drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info, · line 83

Quoted 9 lines
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +2 -1
-/* Caller holds ptp_lock */
 static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
                            u64 *ns)
 {
        struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
        u32 high_before, high_now, low;
 
+       /* Make sure the RESET bit is set */
+       smp_mb__before_atomic();

This may not be sufficient. MMIO read of any register (clock register in this case) can hang the chip if it is undergoing reset.

Quoted 2 lines
        if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
                return -EIO;

We could have missed the flag and got here while the chip is about to be reset.

I will review the patch in more detail tomorrow. Thanks.

#5 lore raw
Quoted 23 lines · Michael Chan
Quoted 9 lines · Vadim Fedorenko

We can see high contention on ptp_lock while doing RX timestamping on high packet rates over several queues. Spinlock is not effecient to protect timecounter for RX timestamps when reads are the most usual operations and writes are only occasional. It's better to use seqlock in such cases.

drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info, · line 83

Quoted 9 lines
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c +2 -1
-/* Caller holds ptp_lock */
  static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
                             u64 *ns)
  {
         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
         u32 high_before, high_now, low;
 
+       /* Make sure the RESET bit is set */
+       smp_mb__before_atomic();

This may not be sufficient. MMIO read of any register (clock register in this case) can hang the chip if it is undergoing reset.

Quoted 6 lines
Quoted 2 lines
         if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
                 return -EIO;

We could have missed the flag and got here while the chip is about to be reset.

I will review the patch in more detail tomorrow. Thanks.

Ok, so we have to serialize bnxt_refclk_read() and FW RESETS, but don't block readers of ptp, especially on RX hot path. So it looks like read_seqcount_excl_bh() can help us with it.

#6 lore raw
In reply to Vadim Fedorenko
kernel test robot noticed the following build errors:

Hi Vadim,

kernel test robot noticed the following build errors:

[auto build test ERROR on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Vadim-Fedorenko/bnxt_en-replace-PTP-spinlock-with-seqlock/20241015-073207
base:   net-next/main
patch link:    https://lore.kernel.org/r/20241014232947.4059941-1-vadfed%40meta.com
patch subject: [PATCH net-next] bnxt_en: replace PTP spinlock with seqlock
config: i386-allmodconfig (https://download.01.org/0day-ci/archive/20241018/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241018/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All error/warnings (new ones prefixed by >>):

drivers/net/ethernet/broadcom/bnxt/bnxt.c: In function 'bnxt_force_fw_reset':

>> drivers/net/ethernet/broadcom/bnxt/bnxt.c:13488:30: warning: unused variable 'ptp' [-Wunused-variable]

13488 | struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
| ^~~
drivers/net/ethernet/broadcom/bnxt/bnxt.c: In function 'bnxt_fw_reset':
drivers/net/ethernet/broadcom/bnxt/bnxt.c:13555:38: warning: unused variable 'ptp' [-Wunused-variable]
13555 | struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
| ^~~
--
In file included from drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:21:
drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c: In function 'bnxt_get_rx_ts_p5':

>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h:155:52: error: expected ')' before ';' token
     155 |         while (read_seqretry(&(ptp)->ptp_lock, seq);    \
         |               ~                                    ^
   drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:802:9: note: in expansion of macro 'BNXT_READ_TIME64'
     802 |         BNXT_READ_TIME64(ptp, time, ptp->old_time);
         |         ^~~~~~~~~~~~~~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h:156:1: error: expected expression before '}' token
     156 | } while (0)
         | ^
   drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:802:9: note: in expansion of macro 'BNXT_READ_TIME64'
     802 |         BNXT_READ_TIME64(ptp, time, ptp->old_time);
         |         ^~~~~~~~~~~~~~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:810:1: error: expected 'while' before 'void'
     810 | void bnxt_tx_ts_cmp(struct bnxt *bp, struct bnxt_napi *bnapi,
         | ^~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:857:12: error: invalid storage class for function 'bnxt_ptp_verify'
     857 | static int bnxt_ptp_verify(struct ptp_clock_info *ptp_info, unsigned int pin,
         |            ^~~~~~~~~~~~~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:870:12: error: invalid storage class for function 'bnxt_ptp_pps_init'
     870 | static int bnxt_ptp_pps_init(struct bnxt *bp)
         |            ^~~~~~~~~~~~~~~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:931:13: error: invalid storage class for function 'bnxt_pps_config_ok'
     931 | static bool bnxt_pps_config_ok(struct bnxt *bp)
         |             ^~~~~~~~~~~~~~~~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:938:13: error: invalid storage class for function 'bnxt_ptp_timecounter_init'
     938 | static void bnxt_ptp_timecounter_init(struct bnxt *bp, bool init_tc)
         |             ^~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:998:13: error: invalid storage class for function 'bnxt_ptp_free'
     998 | static void bnxt_ptp_free(struct bnxt *bp)
         |             ^~~~~~~~~~~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:1100:1: error: expected declaration or statement at end of input
    1100 | }
         | ^
   drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c: At top level:
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:1077:6: warning: 'bnxt_ptp_clear' defined but not used [-Wunused-function]
    1077 | void bnxt_ptp_clear(struct bnxt *bp)
         |      ^~~~~~~~~~~~~~
>> drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.c:1010:5: warning: 'bnxt_ptp_init' defined but not used [-Wunused-function]

1010 | int bnxt_ptp_init(struct bnxt *bp, bool phc_cfg)
| ^~~~~~~~~~~~~

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for GET_FREE_REGION Depends on [n]: SPARSEMEM [=n]
Selected by [m]:
- RESOURCE_KUNIT_TEST [=m] && RUNTIME_TESTING_MENU [=y] && KUNIT [=m]

vim +155 drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h

147
148 #if BITS_PER_LONG == 32
149 #define BNXT_READ_TIME64(ptp, dst, src) \
150 do { \
151 unsigned int seq; \
152 do { \
153 seq = read_seqbegin(&(ptp)->ptp_lock); \
154 (dst) = (src); \

 > 155		while (read_seqretry(&(ptp)->ptp_lock, seq);	\
 > 156	} while (0)

157 #else
158 #define BNXT_READ_TIME64(ptp, dst, src) \
159 ((dst) = READ_ONCE(src))
160 #endif
161

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki