Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 459b724

Browse files
Syed Nayyar Warisgregkh
Syed Nayyar Waris
authored andcommittedOct 4, 2024
lib/bitmap: add bitmap_{read,write}()
[ Upstream commit 63c1582 ] The two new functions allow reading/writing values of length up to BITS_PER_LONG bits at arbitrary position in the bitmap. The code was taken from "bitops: Introduce the for_each_set_clump macro" by Syed Nayyar Waris with a number of changes and simplifications: - instead of using roundup(), which adds an unnecessary dependency on <linux/math.h>, we calculate space as BITS_PER_LONG-offset; - indentation is reduced by not using else-clauses (suggested by checkpatch for bitmap_get_value()); - bitmap_get_value()/bitmap_set_value() are renamed to bitmap_read() and bitmap_write(); - some redundant computations are omitted. Cc: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Syed Nayyar Waris <syednwaris@gmail.com> Signed-off-by: William Breathitt Gray <william.gray@linaro.org> Link: https://lore.kernel.org/lkml/fe12eedf3666f4af5138de0e70b67a07c7f40338.1592224129.git.syednwaris@gmail.com/ Suggested-by: Yury Norov <yury.norov@gmail.com> Co-developed-by: Alexander Potapenko <glider@google.com> Signed-off-by: Alexander Potapenko <glider@google.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Acked-by: Yury Norov <yury.norov@gmail.com> Signed-off-by: Yury Norov <yury.norov@gmail.com> Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com> Signed-off-by: David S. Miller <davem@davemloft.net> Stable-dep-of: 77b0b98 ("btrfs: subpage: fix the bitmap dump which can cause bitmap corruption") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 32e93ca commit 459b724

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
 

‎include/linux/bitmap.h

+77
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ struct device;
7777
* bitmap_to_arr64(buf, src, nbits) Copy nbits from buf to u64[] dst
7878
* bitmap_get_value8(map, start) Get 8bit value from map at start
7979
* bitmap_set_value8(map, value, start) Set 8bit value to map at start
80+
* bitmap_read(map, start, nbits) Read an nbits-sized value from
81+
* map at start
82+
* bitmap_write(map, value, start, nbits) Write an nbits-sized value to
83+
* map at start
8084
*
8185
* Note, bitmap_zero() and bitmap_fill() operate over the region of
8286
* unsigned longs, that is, bits behind bitmap till the unsigned long
@@ -613,6 +617,79 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
613617
map[index] |= value << offset;
614618
}
615619

620+
/**
621+
* bitmap_read - read a value of n-bits from the memory region
622+
* @map: address to the bitmap memory region
623+
* @start: bit offset of the n-bit value
624+
* @nbits: size of value in bits, nonzero, up to BITS_PER_LONG
625+
*
626+
* Returns: value of @nbits bits located at the @start bit offset within the
627+
* @map memory region. For @nbits = 0 and @nbits > BITS_PER_LONG the return
628+
* value is undefined.
629+
*/
630+
static inline unsigned long bitmap_read(const unsigned long *map,
631+
unsigned long start,
632+
unsigned long nbits)
633+
{
634+
size_t index = BIT_WORD(start);
635+
unsigned long offset = start % BITS_PER_LONG;
636+
unsigned long space = BITS_PER_LONG - offset;
637+
unsigned long value_low, value_high;
638+
639+
if (unlikely(!nbits || nbits > BITS_PER_LONG))
640+
return 0;
641+
642+
if (space >= nbits)
643+
return (map[index] >> offset) & BITMAP_LAST_WORD_MASK(nbits);
644+
645+
value_low = map[index] & BITMAP_FIRST_WORD_MASK(start);
646+
value_high = map[index + 1] & BITMAP_LAST_WORD_MASK(start + nbits);
647+
return (value_low >> offset) | (value_high << space);
648+
}
649+
650+
/**
651+
* bitmap_write - write n-bit value within a memory region
652+
* @map: address to the bitmap memory region
653+
* @value: value to write, clamped to nbits
654+
* @start: bit offset of the n-bit value
655+
* @nbits: size of value in bits, nonzero, up to BITS_PER_LONG.
656+
*
657+
* bitmap_write() behaves as-if implemented as @nbits calls of __assign_bit(),
658+
* i.e. bits beyond @nbits are ignored:
659+
*
660+
* for (bit = 0; bit < nbits; bit++)
661+
* __assign_bit(start + bit, bitmap, val & BIT(bit));
662+
*
663+
* For @nbits == 0 and @nbits > BITS_PER_LONG no writes are performed.
664+
*/
665+
static inline void bitmap_write(unsigned long *map, unsigned long value,
666+
unsigned long start, unsigned long nbits)
667+
{
668+
size_t index;
669+
unsigned long offset;
670+
unsigned long space;
671+
unsigned long mask;
672+
bool fit;
673+
674+
if (unlikely(!nbits || nbits > BITS_PER_LONG))
675+
return;
676+
677+
mask = BITMAP_LAST_WORD_MASK(nbits);
678+
value &= mask;
679+
offset = start % BITS_PER_LONG;
680+
space = BITS_PER_LONG - offset;
681+
fit = space >= nbits;
682+
index = BIT_WORD(start);
683+
684+
map[index] &= (fit ? (~(mask << offset)) : ~BITMAP_FIRST_WORD_MASK(start));
685+
map[index] |= value << offset;
686+
if (fit)
687+
return;
688+
689+
map[index + 1] &= BITMAP_FIRST_WORD_MASK(start + nbits);
690+
map[index + 1] |= (value >> space);
691+
}
692+
616693
#endif /* __ASSEMBLY__ */
617694

618695
#endif /* __LINUX_BITMAP_H */

0 commit comments

Comments
 (0)
This repository has been archived.