2003-09-08 15:14:15

by Rolf Eike Beer

[permalink] [raw]
Subject: [2.4.23-pre3] Possible bug in fs/buffer.c

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


2003-09-08 15:59:52

by Alan

[permalink] [raw]
Subject: Re: [2.4.23-pre3] Possible bug in fs/buffer.c

On Llu, 2003-09-08 at 16:42, Andreas Schwab wrote:
> 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

2003-09-08 15:43:23

by Andreas Schwab

[permalink] [raw]
Subject: Re: [2.4.23-pre3] Possible bug in fs/buffer.c

Rolf Eike Beer <[email protected]> writes:

> 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.

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.

--
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."

2003-09-08 16:04:49

by Andreas Schwab

[permalink] [raw]
Subject: Re: [2.4.23-pre3] Possible bug in fs/buffer.c

Alan Cox <[email protected]> writes:

> On Llu, 2003-09-08 at 16:42, Andreas Schwab wrote:
>> 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.

--
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."

2003-09-08 16:12:53

by Rolf Eike Beer

[permalink] [raw]
Subject: Re: [2.4.23-pre3] Possible bug in fs/buffer.c

Am Montag, 8. September 2003 17:58 schrieb Alan Cox:
> On Llu, 2003-09-08 at 16:42, Andreas Schwab wrote:
> > 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

2003-09-08 22:14:15

by J.A. Magallon

[permalink] [raw]
Subject: Re: [2.4.23-pre3] Possible bug in fs/buffer.c


On 09.08, Andreas Schwab wrote:
> Alan Cox <[email protected]> writes:
>
> > On Llu, 2003-09-08 at 16:42, Andreas Schwab wrote:
> >> 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 ?

--
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))

2003-09-09 00:54:10

by Alan

[permalink] [raw]
Subject: Re: [2.4.23-pre3] Possible bug in fs/buffer.c

On Llu, 2003-09-08 at 17:12, Rolf Eike Beer wrote:
> 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.

2003-09-09 00:55:08

by Alan

[permalink] [raw]
Subject: Re: [2.4.23-pre3] Possible bug in fs/buffer.c

On Llu, 2003-09-08 at 17:04, Andreas Schwab wrote:
> > 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.

2003-09-09 13:38:43

by Rolf Eike Beer

[permalink] [raw]
Subject: Re: [2.4.23-pre3] Possible bug in fs/buffer.c

Am Dienstag, 9. September 2003 02:53 schrieb Alan Cox:
> On Llu, 2003-09-08 at 17:04, Andreas Schwab wrote:
> > > 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