Skip to contentSkip to conversation

ice: use better bitmap API

PATCH In review Yury Norov (NVIDIA) 4 participants 8 messages Dec 23, 2025 – Feb 24

Subsystems (5)
lore
Share & tools
lore

Yury Norov (NVIDIA)

author maintainer
#0 lore raw

cover ice: use better bitmap API

Use better bitmap API where appropriate.

Yury Norov (NVIDIA) (3):
bitmap_weighted_xor()
ice: use bitmap_weighted_xor()
ice: use bitmap_empty() in ice_vf_has_no_qs_ena

 drivers/net/ethernet/intel/ice/ice_switch.c |  4 +---
 drivers/net/ethernet/intel/ice/ice_vf_lib.c |  4 ++--
 include/linux/bitmap.h                      | 14 ++++++++++++++
 lib/bitmap.c                                |  7 +++++++
 4 files changed, 24 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/3 bitmap: introduce bitmap_weighted_xor()

The function helps to XOR bitmaps and calculate Hamming weight of the result in one pass.

---
 include/linux/bitmap.h | 14 ++++++++++++++
 lib/bitmap.c           |  7 +++++++
 2 files changed, 21 insertions(+)
include/linux/bitmap.h +14 -0
index 0f4789e1f7cb..7ecf56e0d3b5 100644
@@ -169,6 +169,8 @@ void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
 		 const unsigned long *bitmap2, unsigned int nbits);
 unsigned int __bitmap_weighted_or(unsigned long *dst, const unsigned long *bitmap1,
 				  const unsigned long *bitmap2, unsigned int nbits);
+unsigned int __bitmap_weighted_xor(unsigned long *dst, const unsigned long *bitmap1,
+				  const unsigned long *bitmap2, unsigned int nbits);
 void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
 		  const unsigned long *bitmap2, unsigned int nbits);
 bool __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
@@ -355,6 +357,18 @@ unsigned int bitmap_weighted_or(unsigned long *dst, const unsigned long *src1,
 	}
 }
 
+static __always_inline
+unsigned int bitmap_weighted_xor(unsigned long *dst, const unsigned long *src1,
+				const unsigned long *src2, unsigned int nbits)
+{
+	if (small_const_nbits(nbits)) {
+		*dst = *src1 ^ *src2;
+		return hweight_long(*dst & BITMAP_LAST_WORD_MASK(nbits));
+	} else {
+		return __bitmap_weighted_xor(dst, src1, src2, nbits);
+	}
+}
+
 static __always_inline
 void bitmap_xor(unsigned long *dst, const unsigned long *src1,
 		const unsigned long *src2, unsigned int nbits)
lib/bitmap.c +7 -0
index 698d15933c84..bed32b8cd23a 100644
@@ -382,6 +382,13 @@ unsigned int __bitmap_weighted_or(unsigned long *dst, const unsigned long *bitma
 	return BITMAP_WEIGHT(({dst[idx] = bitmap1[idx] | bitmap2[idx]; dst[idx]; }), bits);
 }
 
+unsigned int __bitmap_weighted_xor(unsigned long *dst, const unsigned long *bitmap1,
+				  const unsigned long *bitmap2, unsigned int bits)
+{
+	return BITMAP_WEIGHT(({dst[idx] = bitmap1[idx] ^ bitmap2[idx]; dst[idx]; }), bits);
+}
+EXPORT_SYMBOL(__bitmap_weighted_xor);
+
 unsigned long __bitmap_weight_from(const unsigned long *bitmap,
 				   unsigned int start, unsigned int nbits)
 {
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/3 ice: use bitmap_weighted_xor() in ice_find_free_recp_res_idx()

In reply to Yury Norov (NVIDIA)

Use the right helper and save one bitmaps traverse.

---
 drivers/net/ethernet/intel/ice/ice_switch.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/ethernet/intel/ice/ice_switch.c +1 -3
index 84848f0123e7..903417477929 100644
@@ -4984,10 +4984,8 @@ ice_find_free_recp_res_idx(struct ice_hw *hw, const unsigned long *profiles,
 			  hw->switch_info->recp_list[bit].res_idxs,
 			  ICE_MAX_FV_WORDS);
 
-	bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
-
 	/* return number of free indexes */
-	return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS);
+	return (u16)bitmap_weighted_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
 }
 
 /**
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/3] ice: use bitmap_weighted_xor() in ice_find_free_recp_res_idx()

Quoted 1 line · On Tue, 23 Dec 2025 11:23:01 -0500 "Yury Norov (NVIDIA)"…

Use the right helper and save one bitmaps traverse.

It makes no difference here.
The bitmap has 48 entries and is just a single 'long' on 64bit. It is also already in a very slow path that has iterated all the 'set' bit of two bitmaps.

The code is also pretty convoluted and confusing already. One of the other bitmaps has 64 entries, recoding using u64 would make it a bit more readable.

Doing the 'weight' here is also just optimising for failure.

Oh, and using u8 and u16 for function parameters, return values and maths requires extra instructions and is usually a bad idea.

Dvaid

Quoted 22 lines
---
 drivers/net/ethernet/intel/ice/ice_switch.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/ethernet/intel/ice/ice_switch.c +1 -3
index 84848f0123e7..903417477929 100644
@@ -4984,10 +4984,8 @@ ice_find_free_recp_res_idx(struct ice_hw *hw, const unsigned long *profiles,
 			  hw->switch_info->recp_list[bit].res_idxs,
 			  ICE_MAX_FV_WORDS);
 
-	bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
-
 	/* return number of free indexes */
-	return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS);
+	return (u16)bitmap_weighted_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
 }
 
 /**
#4 lore raw

Re: [PATCH 2/3] ice: use bitmap_weighted_xor() in ice_find_free_recp_res_idx()

Quoted 6 lines · David Laight
Quoted 1 line · On Tue, 23 Dec 2025 11:23:01 -0500 "Yury Norov (NVIDIA)"…

Use the right helper and save one bitmaps traverse.

It makes no difference here.

No it makes.

Quoted 9 lines

The bitmap has 48 entries and is just a single 'long' on 64bit. It is also already in a very slow path that has iterated all the 'set' bit of two bitmaps.

The code is also pretty convoluted and confusing already. One of the other bitmaps has 64 entries, recoding using u64 would make it a bit more readable.

Doing the 'weight' here is also just optimising for failure.

We've introduced bitmap_weighted_or() recently, and now we need to add siblings to the new sub-class. Regardless of performance impact in this case, having a dedicated helper for a typical operations saves a lot in debugging. This is proven by decades of code development.

Quoted 2 lines

Oh, and using u8 and u16 for function parameters, return values and maths requires extra instructions and is usually a bad idea.

Changing function signatures is not the patch purpose. Feel free to submit a separate patch.

Quoted 24 lines

Dvaid

Quoted 22 lines
---
 drivers/net/ethernet/intel/ice/ice_switch.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/ethernet/intel/ice/ice_switch.c +1 -3
index 84848f0123e7..903417477929 100644
@@ -4984,10 +4984,8 @@ ice_find_free_recp_res_idx(struct ice_hw *hw, const unsigned long *profiles,
 			  hw->switch_info->recp_list[bit].res_idxs,
 			  ICE_MAX_FV_WORDS);
 
-	bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
-
 	/* return number of free indexes */
-	return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS);
+	return (u16)bitmap_weighted_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
 }
 
 /**
maintainer
#5 lore raw

Re: [PATCH 2/3] ice: use bitmap_weighted_xor() in ice_find_free_recp_res_idx()

Quoted 18 lines · Yury Norov
Quoted 6 lines · David Laight
Quoted 1 line · On Tue, 23 Dec 2025 11:23:01 -0500 "Yury Norov (NVIDIA)"…

Use the right helper and save one bitmaps traverse.

It makes no difference here.

No it makes.

Quoted 8 lines

The bitmap has 48 entries and is just a single 'long' on 64bit. It is also already in a very slow path that has iterated all the 'set' bit of two bitmaps.

The code is also pretty convoluted and confusing already. One of the other bitmaps has 64 entries, recoding using u64 would make it a bit more readable.

Quoted 6 lines
Quoted 1 line

Doing the 'weight' here is also just optimising for failure.

We've introduced bitmap_weighted_or() recently, and now we need to add siblings to the new sub-class. Regardless of performance impact in this case, having a dedicated helper for a typical operations saves a lot in debugging. This is proven by decades of code development.

I agree, performance issues aside, I could see the utility in having a combined helper. It makes is a little cleaner and removes a point for introducing problems. Assuming there are no objections, could you resend the series? It'll be easier than digging back to get these.

Thanks,
Tony

Quoted 30 lines
Quoted 2 lines

Oh, and using u8 and u16 for function parameters, return values and maths requires extra instructions and is usually a bad idea.

Changing function signatures is not the patch purpose. Feel free to submit a separate patch.

Quoted 24 lines

Dvaid

Quoted 22 lines
---
  drivers/net/ethernet/intel/ice/ice_switch.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/ethernet/intel/ice/ice_switch.c +1 -3
index 84848f0123e7..903417477929 100644
@@ -4984,10 +4984,8 @@ ice_find_free_recp_res_idx(struct ice_hw *hw, const unsigned long *profiles,
  			  hw->switch_info->recp_list[bit].res_idxs,
  			  ICE_MAX_FV_WORDS);
 
-	bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
-
  	/* return number of free indexes */
-	return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS);
+	return (u16)bitmap_weighted_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
  }
 
  /**
author maintainer
#6 lore raw

PATCH 3/3 ice: use bitmap_empty() in ice_vf_has_no_qs_ena

In reply to Yury Norov (NVIDIA)

bitmap_empty() is more verbose and efficient, as it stops traversing {r,t}xq_ena as soon as the 1st set bit found.

---
 drivers/net/ethernet/intel/ice/ice_vf_lib.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/ice/ice_vf_lib.c +2 -2
index de9e81ccee66..2b359752a158 100644
@@ -1210,8 +1210,8 @@ bool ice_is_vf_trusted(struct ice_vf *vf)
  */
 bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
 {
-	return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
-		!bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
+	return bitmap_empty(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
+		bitmap_empty(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
 }
 
 /**
Signature · 1 line
2.43.0
#7 lore raw
In reply to Yury Norov (NVIDIA)

Ping?

Quoted 15 lines · Yury Norov (NVIDIA)

Use better bitmap API where appropriate.

Yury Norov (NVIDIA) (3):
bitmap_weighted_xor()
ice: use bitmap_weighted_xor()
ice: use bitmap_empty() in ice_vf_has_no_qs_ena

 drivers/net/ethernet/intel/ice/ice_switch.c |  4 +---
 drivers/net/ethernet/intel/ice/ice_vf_lib.c |  4 ++--
 include/linux/bitmap.h                      | 14 ++++++++++++++
 lib/bitmap.c                                |  7 +++++++
 4 files changed, 24 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0