History

bitmap: fix end condition in bitmap_find_free_region

Guennadi Liakhovetski noticed that the end condition for the loop in bitmap_find_free_region() is wrong, and the "return if error" was also using the wrong conditional that would only trigger if the bitmap was an exact multiple of the allocation size, which is not necessarily the case with dma_alloc_from_coherent(). Such a failure would end up in bitmap_find_free_region() accessing beyond the end of the bitmap.

Linus Torvalds committed 17 years ago

Commit aa8e4fc1 parent 9ead6491 file · +8 −8Browse files

Showing the changes to lib (1 of 1 file). Show all

Unified · Split

lib/bitmap.c+8−8
@@ -948,15 +948,15 @@ done: · 947 unmodified lines
948948 */
949949 int bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
950950 {
951− int pos; /* scans bitmap by regions of size order */
951+ int pos, end; /* scans bitmap by regions of size order */
952952
953− for (pos = 0; pos < bits; pos += (1 << order))
954− if (__reg_op(bitmap, pos, order, REG_OP_ISFREE))
955− break;
956− if (pos == bits)
957− return -ENOMEM;
958− __reg_op(bitmap, pos, order, REG_OP_ALLOC);
959− return pos;
953+ for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
954+ if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
955+ continue;
956+ __reg_op(bitmap, pos, order, REG_OP_ALLOC);
957+ return pos;
958+ }
959+ return -ENOMEM;
960960 }
961961 EXPORT_SYMBOL(bitmap_find_free_region);
962962

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t