Skip to contentSkip to conversation

ext4: Fix mb_find_next_bit not to return larger than max

PATCH In review Aneesh Kumar K.V 2 participants 3 messages Jul 8, 2008 – Jul 10, 2008 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: Fix mb_find_next_bit not to return larger than max

Some architectures implement ext4_find_next_bit and ext4_find_next_zero_bit in such a way that they return greater than max for some input values. Make sure mb_find_next_bit and mb_find_next_zero_bit return the right values.

On 2.6.25 we have include/asm-x86/bitops_32.h
static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)

{
	unsigned x = 0;

	while (x < size) {
		unsigned long val = *addr++;
		if (val)
			return __ffs(val) + x;
		x += (sizeof(*addr)<<3);
	}
	return x;
}

This can return value greater than size.

Reported and fixed here for lustre

https://bugzilla.lustre.org/show_bug.cgi?id=15932
https://bugzilla.lustre.org/attachment.cgi?id=17205

---
 fs/ext4/mballoc.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)
fs/ext4/mballoc.c +12 -8
index a1e58fb..d2c61eb 100644
@@ -381,22 +381,28 @@ static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
 
 static inline int mb_find_next_zero_bit(void *addr, int max, int start)
 {
-	int fix = 0;
+	int fix = 0, ret, tmpmax;
 	addr = mb_correct_addr_and_bit(&fix, addr);
-	max += fix;
+	tmpmax = max + fix;
 	start += fix;
 
-	return ext4_find_next_zero_bit(addr, max, start) - fix;
+	ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
+	if (ret > max)
+		return max;
+	return ret;
 }
 
 static inline int mb_find_next_bit(void *addr, int max, int start)
 {
-	int fix = 0;
+	int fix = 0, ret, tmpmax;
 	addr = mb_correct_addr_and_bit(&fix, addr);
-	max += fix;
+	tmpmax = max + fix;
 	start += fix;
 
-	return ext4_find_next_bit(addr, max, start) - fix;
+	ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
+	if (ret > max)
+		return max;
+	return ret;
 }
 
 static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
@@ -3633,8 +3639,6 @@ ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
 		if (bit >= end)
 			break;
 		next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
-		if (next > end)
-			next = end;
 		start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
 				le32_to_cpu(sbi->s_es->s_first_data_block);
 		mb_debug("    free preallocated %u/%u in group %u\n",
Signature · 1 line
1.5.6.2.255.gbed62.dirty
#1 lore raw
Quoted 6 lines · 在 2008-07-08二的 22:14 +0530,Aneesh Kumar K.V写道:

Some architectures implement ext4_find_next_bit and ext4_find_next_zero_bit in such a way that they return greater than max for some input values. Make sure mb_find_next_bit and mb_find_next_zero_bit return the right values.

I am not quite clear what bad things happen when ext4_find_next_bit()
returns greater than max? Not sure why we need to force it return
within the range..

Just looking at the code it seems all handles properly that if the return > max, only one is missing is checking in t ext4_find_next_bit (), (as the lustr e patch does), no?

Quoted 38 lines

On 2.6.25 we have include/asm-x86/bitops_32.h
static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)

{
	unsigned x = 0;

	while (x < size) {
		unsigned long val = *addr++;
		if (val)
			return __ffs(val) + x;
		x += (sizeof(*addr)<<3);
	}
	return x;
}

This can return value greater than size.

Reported and fixed here for lustre

https://bugzilla.lustre.org/show_bug.cgi?id=15932
https://bugzilla.lustre.org/attachment.cgi?id=17205

---
 fs/ext4/mballoc.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)
fs/ext4/mballoc.c +1 -2
index a1e58fb..d2c61eb 100644
@@ -381,22 +381,28 @@ static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
 
 static inline int mb_find_next_zero_bit(void *addr, int max, int start)
 {
-	int fix = 0;
+	int fix = 0, ret, tmpmax;
 	addr = mb_correct_addr_and_bit(&fix, addr);
-	max += fix;

fs/ext4/mballoc.c static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr) · line 394

Quoted 10 lines
fs/ext4/mballoc.c +5 -1
+	tmpmax = max + fix;
 	start += fix;
 
-	return ext4_find_next_zero_bit(addr, max, start) - fix;
+	ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
+	if (ret > max)
+		return max;
+	return ret;
 }
 

I missed something basic here, why we need to check ret against the tmpmax instead of the max after adjust? (Actually not quite sure why we need to correct the address in the first place).

Quoted 18 lines
fs/ext4/mballoc.c +6 -3
 static inline int mb_find_next_bit(void *addr, int max, int start)
 {
-	int fix = 0;
+	int fix = 0, ret, tmpmax;
 	addr = mb_correct_addr_and_bit(&fix, addr);
-	max += fix;
+	tmpmax = max + fix;
 	start += fix;
 
-	return ext4_find_next_bit(addr, max, start) - fix;
+	ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
+	if (ret > max)
+		return max;
+	return ret;
 }
 
 static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)

fs/ext4/mballoc.c ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh, · line 3644

Quoted 8 lines
fs/ext4/mballoc.c +0 -2
@@ -3633,8 +3639,6 @@ ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
 		if (bit >= end)
 			break;
 		next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
-		if (next > end)
-			next = end;
 		start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
 				le32_to_cpu(sbi->s_es->s_first_data_block);
 		mb_debug("    free preallocated %u/%u in group %u\n",

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 9 lines · Mingming Cao
Quoted 6 lines · 在 2008-07-08二的 22:14 +0530,Aneesh Kumar K.V写道:

Some architectures implement ext4_find_next_bit and ext4_find_next_zero_bit in such a way that they return greater than max for some input values. Make sure mb_find_next_bit and mb_find_next_zero_bit return the right values.

Quoted 7 lines

I am not quite clear what bad things happen when ext4_find_next_bit()
returns greater than max? Not sure why we need to force it return
within the range..

Just looking at the code it seems all handles properly that if the return > max, only one is missing is checking in t ext4_find_next_bit (), (as the lustr e patch does), no?

If we return more than max in ext4_mb_generate_buddy we end up with wrong length for the free block extent. That would mean we could end up corrupting the file system.

I guess this would be related to that
http://bugzilla.kernel.org/show_bug.cgi?id=11053
We don't have a clear reproducer for that bug.

The max argument implies that we cannot search beyond max.

Quoted 42 lines
Quoted 42 lines

On 2.6.25 we have include/asm-x86/bitops_32.h
static inline unsigned find_first_bit(const unsigned long *addr, unsigned size)

{
	unsigned x = 0;

	while (x < size) {
		unsigned long val = *addr++;
		if (val)
			return __ffs(val) + x;
		x += (sizeof(*addr)<<3);
	}
	return x;
}

This can return value greater than size.

Reported and fixed here for lustre

https://bugzilla.lustre.org/show_bug.cgi?id=15932
https://bugzilla.lustre.org/attachment.cgi?id=17205

---
 fs/ext4/mballoc.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)
fs/ext4/mballoc.c +2 -2
index a1e58fb..d2c61eb 100644
@@ -381,22 +381,28 @@ static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
 
 static inline int mb_find_next_zero_bit(void *addr, int max, int start)
 {
-	int fix = 0;
+	int fix = 0, ret, tmpmax;
 	addr = mb_correct_addr_and_bit(&fix, addr);
-	max += fix;
+	tmpmax = max + fix;
 	start += fix;
 
Quoted 11 lines

fs/ext4/mballoc.c static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr) · line 394

Quoted 7 lines
fs/ext4/mballoc.c +4 -1
-	return ext4_find_next_zero_bit(addr, max, start) - fix;
+	ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
+	if (ret > max)
+		return max;
+	return ret;
 }
 

I missed something basic here, why we need to check ret against the tmpmax instead of the max after adjust? (Actually not quite sure why we need to correct the address in the first place).

Some architectures implement ext4_find_next_zero_bit to take unsinged long pointers. That means the address should be divisible by sizeof(long). The mb_correct_addr_and_bit just adjust address by moving it back to an address divisible by sizeof(long). Now that we adjust address we need to update bit and the max. But in any case we need to make sure the value returned is not greater than max because rest of the code assumes and even the API is supposed to guarantee that returned value is <= max.

Instead of auditing all the implementation of ext4_find_next_zero_bit we just ensures that mb_find_next_zero_bit returns <= max.

-aneesh
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html