Skip to content

Commit

Permalink
lib/bitmap: extend comment for bitmap_(from,to)_arr32()
Browse files Browse the repository at this point in the history
On LE systems bitmaps are naturally ordered, therefore we can potentially
use bitmap_copy routines when converting from 32-bit arrays, even if host
system is 64-bit. But it may lead to out-of-bond access due to unsafe
typecast, and the to the bitmap_(from,to)_arr32 comment doesn't explain
that clearly
  • Loading branch information
YuryNorov committed Apr 20, 2022
1 parent 28bf211 commit 59fa26e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions include/linux/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,14 @@ static inline void bitmap_copy_clear_tail(unsigned long *dst,
}

/*
* On 32-bit systems bitmaps are represented as u32 arrays internally, and
* therefore conversion is not needed when copying data from/to arrays of u32.
* On 32-bit systems bitmaps are represented as u32 arrays internally. On LE64
* machines the order of hi and lo parts of nubmers match the bitmap structure.
* In both cases conversion is not needed when copying data from/to arrays of
* u32. But in LE64 case, typecast in bitmap_copy_clear_tail() may lead
* to out-of-bound access. To avoid that, both LE and BE variants of 64-bit
* architectures are not using bitmap_copy_clear_tail().
*/
#if BITS_PER_LONG == 64
#if (BITS_PER_LONG == 64)
void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
unsigned int nbits);
void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
Expand Down
2 changes: 1 addition & 1 deletion lib/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ unsigned long *devm_bitmap_zalloc(struct device *dev,
}
EXPORT_SYMBOL_GPL(devm_bitmap_zalloc);

#if BITS_PER_LONG == 64
#if (BITS_PER_LONG == 64)
/**
* bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
* @bitmap: array of unsigned longs, the destination bitmap
Expand Down

0 comments on commit 59fa26e

Please sign in to comment.