@@ -208,8 +208,28 @@ static int sbitmap_find_bit_in_word(struct sbitmap_word *map,
208208 return nr ;
209209}
210210
211+ static unsigned int __map_depth_with_shallow (const struct sbitmap * sb ,
212+ int index ,
213+ unsigned int shallow_depth )
214+ {
215+ u64 shallow_word_depth ;
216+ unsigned int word_depth , reminder ;
217+
218+ word_depth = __map_depth (sb , index );
219+ if (shallow_depth >= sb -> depth )
220+ return word_depth ;
221+
222+ shallow_word_depth = word_depth * shallow_depth ;
223+ reminder = do_div (shallow_word_depth , sb -> depth );
224+
225+ if (reminder >= (index + 1 ) * word_depth )
226+ shallow_word_depth ++ ;
227+
228+ return (unsigned int )shallow_word_depth ;
229+ }
230+
211231static int sbitmap_find_bit (struct sbitmap * sb ,
212- unsigned int depth ,
232+ unsigned int shallow_depth ,
213233 unsigned int index ,
214234 unsigned int alloc_hint ,
215235 bool wrap )
@@ -218,12 +238,12 @@ static int sbitmap_find_bit(struct sbitmap *sb,
218238 int nr = -1 ;
219239
220240 for (i = 0 ; i < sb -> map_nr ; i ++ ) {
221- nr = sbitmap_find_bit_in_word (& sb -> map [index ],
222- min_t (unsigned int ,
223- __map_depth (sb , index ),
224- depth ),
225- alloc_hint , wrap );
241+ unsigned int depth = __map_depth_with_shallow (sb , index ,
242+ shallow_depth );
226243
244+ if (depth )
245+ nr = sbitmap_find_bit_in_word (& sb -> map [index ], depth ,
246+ alloc_hint , wrap );
227247 if (nr != -1 ) {
228248 nr += index << sb -> shift ;
229249 break ;
@@ -287,7 +307,22 @@ static int __sbitmap_get_shallow(struct sbitmap *sb,
287307 return sbitmap_find_bit (sb , shallow_depth , index , alloc_hint , true);
288308}
289309
290- int sbitmap_get_shallow (struct sbitmap * sb , unsigned long shallow_depth )
310+ /**
311+ * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap,
312+ * limiting the depth used from each word.
313+ * @sb: Bitmap to allocate from.
314+ * @shallow_depth: The maximum number of bits to allocate from the bitmap.
315+ *
316+ * This rather specific operation allows for having multiple users with
317+ * different allocation limits. E.g., there can be a high-priority class that
318+ * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow()
319+ * with a @shallow_depth of (sb->depth >> 1). Then, the low-priority
320+ * class can only allocate half of the total bits in the bitmap, preventing it
321+ * from starving out the high-priority class.
322+ *
323+ * Return: Non-negative allocated bit number if successful, -1 otherwise.
324+ */
325+ static int sbitmap_get_shallow (struct sbitmap * sb , unsigned long shallow_depth )
291326{
292327 int nr ;
293328 unsigned int hint , depth ;
@@ -302,7 +337,6 @@ int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth)
302337
303338 return nr ;
304339}
305- EXPORT_SYMBOL_GPL (sbitmap_get_shallow );
306340
307341bool sbitmap_any_bit_set (const struct sbitmap * sb )
308342{
@@ -406,27 +440,9 @@ EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
406440static unsigned int sbq_calc_wake_batch (struct sbitmap_queue * sbq ,
407441 unsigned int depth )
408442{
409- unsigned int wake_batch ;
410- unsigned int shallow_depth ;
411-
412- /*
413- * Each full word of the bitmap has bits_per_word bits, and there might
414- * be a partial word. There are depth / bits_per_word full words and
415- * depth % bits_per_word bits left over. In bitwise arithmetic:
416- *
417- * bits_per_word = 1 << shift
418- * depth / bits_per_word = depth >> shift
419- * depth % bits_per_word = depth & ((1 << shift) - 1)
420- *
421- * Each word can be limited to sbq->min_shallow_depth bits.
422- */
423- shallow_depth = min (1U << sbq -> sb .shift , sbq -> min_shallow_depth );
424- depth = ((depth >> sbq -> sb .shift ) * shallow_depth +
425- min (depth & ((1U << sbq -> sb .shift ) - 1 ), shallow_depth ));
426- wake_batch = clamp_t (unsigned int , depth / SBQ_WAIT_QUEUES , 1 ,
427- SBQ_WAKE_BATCH );
428-
429- return wake_batch ;
443+ return clamp_t (unsigned int ,
444+ min (depth , sbq -> min_shallow_depth ) / SBQ_WAIT_QUEUES ,
445+ 1 , SBQ_WAKE_BATCH );
430446}
431447
432448int sbitmap_queue_init_node (struct sbitmap_queue * sbq , unsigned int depth ,
0 commit comments