Skip to contentSkip to conversation

sbitmap: don't loop for find_next_zero_bit() for !round_robin

PATCH Superseded Jens Axboe 2 participants 3 messages Nov 29, 2018

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 1/1 sbitmap: don't loop for find_next_zero_bit() for !round_robin

If we aren't forced to do round robin tag allocation, just use the allocation hint to find the index for the tag word, don't use it for the offset inside the word. This avoids a potential extra round trip in the bit looping.

---

lib/sbitmap.c +12 -7
index fdd1b8aa8ac6..2987b2ac8ed7 100644
@@ -118,10 +118,19 @@ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
 
 	index = SB_NR_TO_INDEX(sb, alloc_hint);
 
+	/*
+	 * Unless we're doing round robin tag allocation, just use the
+	 * alloc_hint to find the right word index. No point in looping
+	 * twice in find_next_zero_bit() for that case.
+	 */
+	if (round_robin)
+		alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
+	else
+		alloc_hint = 0;
+
 	for (i = 0; i < sb->map_nr; i++) {
 		nr = __sbitmap_get_word(&sb->map[index].word,
-					sb->map[index].depth,
-					SB_NR_TO_BIT(sb, alloc_hint),
+					sb->map[index].depth, alloc_hint,
 					!round_robin);
 		if (nr != -1) {
 			nr += index << sb->shift;
@@ -130,12 +139,8 @@ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
 
 		/* Jump to next index. */
 		index++;
-		alloc_hint = index << sb->shift;
-
-		if (index >= sb->map_nr) {
+		if (index >= sb->map_nr)
 			index = 0;
-			alloc_hint = 0;
-		}
 	}
 
 	return nr;
Signature · 1 line
Jens Axboe
#1 lore raw
Quoted 3 lines · Jens Axboe

If we aren't forced to do round robin tag allocation, just use the allocation hint to find the index for the tag word, don't use it for the offset inside the word.

Maybe also add "We're already fetching that cache line, so we might as well check the whole word."

Quoted 34 lines

This avoids a potential extra round trip in the
bit looping.

---

lib/sbitmap.c +11 -2
index fdd1b8aa8ac6..2987b2ac8ed7 100644
@@ -118,10 +118,19 @@ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
 
 	index = SB_NR_TO_INDEX(sb, alloc_hint);
 
+	/*
+	 * Unless we're doing round robin tag allocation, just use the
+	 * alloc_hint to find the right word index. No point in looping
+	 * twice in find_next_zero_bit() for that case.
+	 */
+	if (round_robin)
+		alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
+	else
+		alloc_hint = 0;
+
 	for (i = 0; i < sb->map_nr; i++) {
 		nr = __sbitmap_get_word(&sb->map[index].word,
-					sb->map[index].depth,
-					SB_NR_TO_BIT(sb, alloc_hint),
+					sb->map[index].depth, alloc_hint,
 					!round_robin);
 		if (nr != -1) {
 			nr += index << sb->shift;

lib/sbitmap.c int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) · line 138

Quoted 10 lines
lib/sbitmap.c +1 -5
@@ -130,12 +139,8 @@ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
 
 		/* Jump to next index. */
 		index++;
-		alloc_hint = index << sb->shift;
-
-		if (index >= sb->map_nr) {
+		if (index >= sb->map_nr)
 			index = 0;
-			alloc_hint = 0;
-		}

We need to set alloc_hint = 0 here for the round_robin case.

Quoted 7 lines
lib/sbitmap.c +0 -0
 	}
 
 	return nr;
Signature · 2 lines
-- 
Jens Axboe
author maintainer
#2 lore raw
Quoted 7 lines · Omar Sandoval
Quoted 3 lines · Jens Axboe

If we aren't forced to do round robin tag allocation, just use the allocation hint to find the index for the tag word, don't use it for the offset inside the word.

Maybe also add "We're already fetching that cache line, so we might as well check the whole word."

Sure, will add.

Quoted 13 lines

lib/sbitmap.c int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) · line 138

Quoted 11 lines
lib/sbitmap.c +1 -5
@@ -130,12 +139,8 @@ int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
 
 		/* Jump to next index. */
 		index++;
-		alloc_hint = index << sb->shift;
-
-		if (index >= sb->map_nr) {
+		if (index >= sb->map_nr)
 			index = 0;
-			alloc_hint = 0;
-		}

We need to set alloc_hint = 0 here for the round_robin case.

Good point, does need a reset for the loop for that case.

Signature · 1 line
Jens Axboe