Skip to content

Commit

Permalink
scsi: sbitmap: Add sbitmap_calculate_shift() helper
Browse files Browse the repository at this point in the history
Move code for calculating default shift into a public helper which can be
used by SCSI.

Link: https://lore.kernel.org/r/20210122023317.687987-7-ming.lei@redhat.com
Cc: Omar Sandoval <osandov@fb.com>
Cc: Kashyap Desai <kashyap.desai@broadcom.com>
Cc: Sumanesh Samanta <sumanesh.samanta@broadcom.com>
Cc: Ewan D. Milne <emilne@redhat.com>
Tested-by: Sumanesh Samanta <sumanesh.samanta@broadcom.com>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
  • Loading branch information
Ming Lei authored and Martin K. Petersen committed Mar 4, 2021
1 parent cbb9950 commit 2d13b1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
18 changes: 18 additions & 0 deletions include/linux/sbitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@ static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr)
return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
}

static inline int sbitmap_calculate_shift(unsigned int depth)
{
int shift = ilog2(BITS_PER_LONG);

/*
* If the bitmap is small, shrink the number of bits per word so
* we spread over a few cachelines, at least. If less than 4
* bits, just forget about it, it's not going to work optimally
* anyway.
*/
if (depth >= 4) {
while ((4U << shift) > depth)
shift--;
}

return shift;
}

/**
* sbitmap_show() - Dump &struct sbitmap information to a &struct seq_file.
* @sb: Bitmap to show.
Expand Down
16 changes: 3 additions & 13 deletions lib/sbitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,9 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
unsigned int bits_per_word;
unsigned int i;

if (shift < 0) {
shift = ilog2(BITS_PER_LONG);
/*
* If the bitmap is small, shrink the number of bits per word so
* we spread over a few cachelines, at least. If less than 4
* bits, just forget about it, it's not going to work optimally
* anyway.
*/
if (depth >= 4) {
while ((4U << shift) > depth)
shift--;
}
}
if (shift < 0)
shift = sbitmap_calculate_shift(depth);

bits_per_word = 1U << shift;
if (bits_per_word > BITS_PER_LONG)
return -EINVAL;
Expand Down

0 comments on commit 2d13b1e

Please sign in to comment.