Skip to contentSkip to conversation

Possible bug in fs/buffer.c

2.4.23-pre3 Rolf Eike Beer 4 participants 9 messages Sep 8, 2003 – Sep 9, 2003

lore
Share & tools
lore
#0 lore raw

This is __put_unused_buffer_head from fs/buffer.c, lines 1156 to 1171:

static void __put_unused_buffer_head(struct buffer_head * bh)

{
	if (unlikely(buffer_attached(bh)))
		BUG();
	if (nr_unused_buffer_heads >= MAX_UNUSED_BUFFERS) {
		kmem_cache_free(bh_cachep, bh);
	} else {
		bh->b_dev = B_FREE;
===>		bh->b_blocknr = -1;		<===
		bh->b_this_page = NULL;

		nr_unused_buffer_heads++;
		bh->b_next_free = unused_list;
		unused_list = bh;
	}
}

In include/linux/fs.h "struct buffer_head" is defined this way:

struct buffer_head {
/* First cache line: */
struct buffer_head *b_next; /* Hash queue list */
unsigned long b_blocknr; /* block number */
...

So this line (and line 1205, which is the same) is either ugly (and someone meant ~0UL or something similar) or completely bogus. Same way in 2.6.0-test4-bk10/fs/buffer.c, line 1031 (b_blocknr is a sector_t, which is an unsigned long).

Comments?

Eike

#1 lore raw
Quoted 22 lines · Rolf Eike Beer

This is __put_unused_buffer_head from fs/buffer.c, lines 1156 to 1171:

static void __put_unused_buffer_head(struct buffer_head * bh)

{
	if (unlikely(buffer_attached(bh)))
		BUG();
	if (nr_unused_buffer_heads >= MAX_UNUSED_BUFFERS) {
		kmem_cache_free(bh_cachep, bh);
	} else {
		bh->b_dev = B_FREE;
===>		bh->b_blocknr = -1;		<===
		bh->b_this_page = NULL;

		nr_unused_buffer_heads++;
		bh->b_next_free = unused_list;
		unused_list = bh;
	}
}

In include/linux/fs.h "struct buffer_head" is defined this way:

Quoted 8 lines

struct buffer_head {
/* First cache line: */
struct buffer_head *b_next; /* Hash queue list */
unsigned long b_blocknr; /* block number */
...

So this line (and line 1205, which is the same) is either ugly (and someone meant ~0UL or something similar) or completely bogus.

It's neither ugly, nor bogus. The only 100% reliable way to assign the maximum value to an unsigned integer is to use -1.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#2 lore raw
Quoted 2 lines · Andreas Schwab

It's neither ugly, nor bogus. The only 100% reliable way to assign the maximum value to an unsigned integer is to use -1.

Its not 100% reliable either 8). Properly you should use the limits.h values. The kernel assumes 2's complement so just adding a cast would probably keep gcc happy

#3 lore raw
Quoted 5 lines · Alan Cox
Quoted 2 lines · Andreas Schwab

It's neither ugly, nor bogus. The only 100% reliable way to assign the maximum value to an unsigned integer is to use -1.

Its not 100% reliable either 8).

Could you please elaborate? Casting -1 to an unsigned type is guaranteed to yield the maximum value for that type, at least since C89, but I think even K&R C did get it right.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#7 lore raw
In reply to Alan Cox

Am Montag, 8. September 2003 17:58 schrieb Alan Cox:

Quoted 7 lines · Andreas Schwab
Quoted 2 lines

It's neither ugly, nor bogus. The only 100% reliable way to assign the maximum value to an unsigned integer is to use -1.

Its not 100% reliable either 8). Properly you should use the limits.h values. The kernel assumes 2's complement so just adding a cast would probably keep gcc happy

gcc didn't even find that. He complained about a line slightly above this one. In limits.h there is no value equal to -1UL.

Eike

#4 lore raw
In reply to Andreas Schwab
Quoted 12 lines · Andreas Schwab
Quoted 5 lines · Alan Cox
Quoted 2 lines · Andreas Schwab

It's neither ugly, nor bogus. The only 100% reliable way to assign the maximum value to an unsigned integer is to use -1.

Its not 100% reliable either 8).

Could you please elaborate? Casting -1 to an unsigned type is guaranteed to yield the maximum value for that type, at least since C89, but I think even K&R C did get it right.

Would not be much cleaner to do use ~0UL ?

Signature · 4 lines
J.A. Magallon <[email protected]>      \            Software is like sex:
werewolf.able.es                         \      It's better when it's free
Mandrake Linux release 9.2 (Cooker) for i586
Linux 2.4.23-pre2-jam1m (gcc 3.3.1 (Mandrake Linux 9.2 3.3.1-1mdk))
#8 lore raw
In reply to Rolf Eike Beer
Quoted 2 lines · Rolf Eike Beer

gcc didn't even find that. He complained about a line slightly above this one. In limits.h there is no value equal to -1UL.

UINT_MAX in a standard C limits.h. Right now I dont think the kernel defines it but it could and ANSI C deals with this stuff.

#5 lore raw
In reply to Andreas Schwab
Quoted 5 lines · Andreas Schwab
Quoted 1 line

Its not 100% reliable either 8).

Could you please elaborate? Casting -1 to an unsigned type is guaranteed to yield the maximum value for that type, at least since C89, but I think even K&R C did get it right.

My error - its ~0 that is unreliable.

#6 lore raw

Am Dienstag, 9. September 2003 02:53 schrieb Alan Cox:

Quoted 8 lines · Andreas Schwab
Quoted 5 lines
Quoted 1 line

Its not 100% reliable either 8).

Could you please elaborate? Casting -1 to an unsigned type is guaranteed to yield the maximum value for that type, at least since C89, but I think even K&R C did get it right.

My error - its ~0 that is unreliable.

Uh-oh:

eike@bilbo:/mnt/kernel/linux-2.4.23-pre3> grep -r "~0[^xX]" *| wc -l

    713

Eike