public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/3] sbitmap: fix hint wrap in the failure case
@ 2023-07-27 15:20 chengming.zhou
  2023-07-27 15:20 ` [PATCH v2 2/3] sbitmap: fix strict round-robin non-wrap with hint > 0 chengming.zhou
  2023-07-27 15:20 ` [PATCH v2 3/3] sbitmap: drop wrap logic in __sbitmap_get_word() chengming.zhou
  0 siblings, 2 replies; 3+ messages in thread
From: chengming.zhou @ 2023-07-27 15:20 UTC (permalink / raw)
  To: axboe, osandov, ming.lei, kbusch, krisman
  Cc: linux-block, linux-kernel, zhouchengming

From: Chengming Zhou <zhouchengming@bytedance.com>

```
hint = nr + 1;
if (hint >= depth - 1)
	hint = 0;
```

Now we wrap the hint to 0 in the failure case, but has two problems:
1. hint == depth - 1, is actually an available offset hint, in which
   case we shouldn't wrap hint to 0.
2. In the strict round_robin non-wrap case, we shouldn't wrap at all.

```
wrap = wrap && hint;
```

We only need to check wrap based on the original hint ( > 0), don't need
to recheck the new hint which maybe updated in the failure case, which
may cause second wrap. We set wrap to false after we wrap once to avoid
repeated wrap, which is clearer than rechecking the hint.

Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
---
 lib/sbitmap.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/lib/sbitmap.c b/lib/sbitmap.c
index d0a5081dfd12..ac4027884765 100644
--- a/lib/sbitmap.c
+++ b/lib/sbitmap.c
@@ -149,7 +149,8 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
 			 * offset to 0 in a failure case, so start from 0 to
 			 * exhaust the map.
 			 */
-			if (hint && wrap) {
+			if (wrap) {
+				wrap = false;
 				hint = 0;
 				continue;
 			}
@@ -160,8 +161,14 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
 			break;
 
 		hint = nr + 1;
-		if (hint >= depth - 1)
-			hint = 0;
+		if (unlikely(hint >= depth)) {
+			if (wrap) {
+				wrap = false;
+				hint = 0;
+				continue;
+			}
+			return -1;
+		}
 	}
 
 	return nr;
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-07-27 15:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-27 15:20 [PATCH v2 1/3] sbitmap: fix hint wrap in the failure case chengming.zhou
2023-07-27 15:20 ` [PATCH v2 2/3] sbitmap: fix strict round-robin non-wrap with hint > 0 chengming.zhou
2023-07-27 15:20 ` [PATCH v2 3/3] sbitmap: drop wrap logic in __sbitmap_get_word() chengming.zhou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox