summaryrefslogtreecommitdiff
path: root/include/linux/sbitmap.h
diff options
context:
space:
mode:
authorMing Lei <ming.lei@redhat.com>2021-01-22 05:33:10 +0300
committerMartin K. Petersen <martin.petersen@oracle.com>2021-03-05 01:36:59 +0300
commit2d13b1ea9f4affdaa7af0e0e4a1358d28f80c54f (patch)
tree696d93ba7b70a2c7b7e631e1e9ac5d500c5f95c4 /include/linux/sbitmap.h
parentcbb9950b41dd9dfb7c2be3429ba09f83b8b1ff98 (diff)
downloadlinux-2d13b1ea9f4affdaa7af0e0e4a1358d28f80c54f.tar.xz
scsi: sbitmap: Add sbitmap_calculate_shift() helper
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>
Diffstat (limited to 'include/linux/sbitmap.h')
-rw-r--r--include/linux/sbitmap.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h
index c65ba887dcc3..3087e1f15fdd 100644
--- a/include/linux/sbitmap.h
+++ b/include/linux/sbitmap.h
@@ -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.