2010-08-12 12:31:49

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH v2] MD: raid, fix BUG caused by flags handling

Commit 74450be1 (block: unify flags for struct bio and struct request)
added direct test of flags in the & form:
const bool do_sync = (bio->bi_rw & REQ_SYNC);
But this doesn't fit into bool with my compiler (gcc 4.5). So change
the type to ulong to avoid the bug.

The BUG looks like:
EXT3-fs (md1): using internal journal
------------[ cut here ]------------
kernel BUG at drivers/scsi/scsi_lib.c:1113!
...
Pid: 879, comm: md3_raid1 Tainted: G W 2.6.35-rc5-mm1_64+ #1265
RIP: 0010:[<ffffffff813312d6>] [<ffffffff813312d6>] scsi_setup_fs_cmnd+0x96/0xd0
Process md3_raid1 (pid: 879, threadinfo ffff8801c4716000, task ffff8801cbd5a6a0)
...
Call Trace:
[<ffffffff81339a78>] sd_prep_fn+0xa8/0x800
[<ffffffff812252e9>] ? cfq_dispatch_request+0x49/0xb0
[<ffffffff8121a24a>] blk_peek_request+0xca/0x1a0
[<ffffffff81330a56>] scsi_request_fn+0x56/0x400
[<ffffffff81219dad>] __generic_unplug_device+0x2d/0x40
[<ffffffff8121a059>] generic_unplug_device+0x29/0x40
[<ffffffff81217682>] blk_unplug+0x12/0x20
[<ffffffff813f14c8>] unplug_slaves+0x78/0xc0
[<ffffffff813f3ecb>] raid1d+0x37b/0x420
[<ffffffff813fb6e3>] md_thread+0x53/0x120
...
Code: e8 d0 fe ff ff 5b 41 5c c9 c3 0f 1f 00 4c 89 e7 be 20 00 00 00 e8 db 9f ff ff 48 89 c7 48 85 c0 74 35 48 89 83 c8 00 00 00 eb a3 <0f> 0b 48 8b 00 48 85 c0 74 83 48 8b 40 48 48 85 c0 0f 84 76 ff
RIP [<ffffffff813312d6>] scsi_setup_fs_cmnd+0x96/0xd0

Signed-off-by: Jiri Slaby <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Neil Brown <[email protected]>
---
drivers/md/raid1.c | 10 ++++++----
drivers/md/raid10.c | 5 +++--
2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 73cc74f..4bfebce 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -787,8 +787,8 @@ static int make_request(mddev_t *mddev, struct bio * bio)
struct bio_list bl;
struct page **behind_pages = NULL;
const int rw = bio_data_dir(bio);
- const bool do_sync = (bio->bi_rw & REQ_SYNC);
- bool do_barriers;
+ const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
+ unsigned long do_barriers;
mdk_rdev_t *blocked_rdev;

/*
@@ -1640,7 +1640,8 @@ static void raid1d(mddev_t *mddev)
* We already have a nr_pending reference on these rdevs.
*/
int i;
- const bool do_sync = (r1_bio->master_bio->bi_rw & REQ_SYNC);
+ const unsigned long do_sync =
+ (r1_bio->master_bio->bi_rw & REQ_SYNC);
clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
clear_bit(R1BIO_Barrier, &r1_bio->state);
for (i=0; i < conf->raid_disks; i++)
@@ -1696,7 +1697,8 @@ static void raid1d(mddev_t *mddev)
(unsigned long long)r1_bio->sector);
raid_end_bio_io(r1_bio);
} else {
- const bool do_sync = r1_bio->master_bio->bi_rw & REQ_SYNC;
+ const unsigned long do_sync =
+ r1_bio->master_bio->bi_rw & REQ_SYNC;
r1_bio->bios[r1_bio->read_disk] =
mddev->ro ? IO_BLOCKED : NULL;
r1_bio->read_disk = disk;
diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index a88aeb5..6513c3c 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -799,7 +799,7 @@ static int make_request(mddev_t *mddev, struct bio * bio)
int i;
int chunk_sects = conf->chunk_mask + 1;
const int rw = bio_data_dir(bio);
- const bool do_sync = (bio->bi_rw & REQ_SYNC);
+ const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
struct bio_list bl;
unsigned long flags;
mdk_rdev_t *blocked_rdev;
@@ -1734,7 +1734,8 @@ static void raid10d(mddev_t *mddev)
raid_end_bio_io(r10_bio);
bio_put(bio);
} else {
- const bool do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
+ const unsigned long do_sync =
+ (r10_bio->master_bio->bi_rw & REQ_SYNC);
bio_put(bio);
rdev = conf->mirrors[mirror].rdev;
if (printk_ratelimit())
--
1.7.2.1


2010-08-12 12:31:16

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH v2] SCSI: fix bio.bi_rw handling

Return of the bi_rw tests is no longer bool after commit 74450be1. So
testing against constants doesn't make sense anymore. Fix this bug in
osd_req_read by removing "== 1" in test.

This is not a problem now, where REQ_WRITE is 1, but this can change
in the future and we don't want to rely on that.

Signed-off-by: Jiri Slaby <[email protected]>
Cc: "James E.J. Bottomley" <[email protected]>
Cc: Christoph Hellwig <[email protected]>
---
drivers/scsi/osd/osd_initiator.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/drivers/scsi/osd/osd_initiator.c b/drivers/scsi/osd/osd_initiator.c
index fda4de3..e88bbdd 100644
--- a/drivers/scsi/osd/osd_initiator.c
+++ b/drivers/scsi/osd/osd_initiator.c
@@ -865,7 +865,7 @@ void osd_req_read(struct osd_request *or,
{
_osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
WARN_ON(or->in.bio || or->in.total_bytes);
- WARN_ON(1 == (bio->bi_rw & REQ_WRITE));
+ WARN_ON(bio->bi_rw & REQ_WRITE);
or->in.bio = bio;
or->in.total_bytes = len;
}
--
1.7.2.1

2010-08-12 12:31:46

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH v2] BLOCK: fix bio.bi_rw handling

Return of the bi_rw tests is no longer bool after commit 74450be1. But
results of such tests are stored in bools. This doesn't fit in there
for some compilers (gcc 4.5 here), so either use !! magic to get real
bools or use ulong where the result is assigned somewhere.

Signed-off-by: Jiri Slaby <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: Jens Axboe <[email protected]>
---
block/blk-core.c | 6 +++---
drivers/block/loop.c | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/block/blk-core.c b/block/blk-core.c
index 207af0d..29fd2cc 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -1198,9 +1198,9 @@ static int __make_request(struct request_queue *q, struct bio *bio)
int el_ret;
unsigned int bytes = bio->bi_size;
const unsigned short prio = bio_prio(bio);
- const bool sync = (bio->bi_rw & REQ_SYNC);
- const bool unplug = (bio->bi_rw & REQ_UNPLUG);
- const unsigned int ff = bio->bi_rw & REQ_FAILFAST_MASK;
+ const bool sync = !!(bio->bi_rw & REQ_SYNC);
+ const bool unplug = !!(bio->bi_rw & REQ_UNPLUG);
+ const unsigned long ff = bio->bi_rw & REQ_FAILFAST_MASK;
int rw_flags;

if ((bio->bi_rw & REQ_HARDBARRIER) &&
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index e647537..0ba2899 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -476,7 +476,7 @@ static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;

if (bio_rw(bio) == WRITE) {
- bool barrier = (bio->bi_rw & REQ_HARDBARRIER);
+ bool barrier = !!(bio->bi_rw & REQ_HARDBARRIER);
struct file *file = lo->lo_backing_file;

if (barrier) {
--
1.7.2.1

2010-08-12 13:27:37

by Jeff Moyer

[permalink] [raw]
Subject: Re: [PATCH v2] MD: raid, fix BUG caused by flags handling

Jiri Slaby <[email protected]> writes:

> Commit 74450be1 (block: unify flags for struct bio and struct request)
> added direct test of flags in the & form:
> const bool do_sync = (bio->bi_rw & REQ_SYNC);
> But this doesn't fit into bool with my compiler (gcc 4.5). So change
> the type to ulong to avoid the bug.

At first I wondered why you didn't use the !! trick, but after looking
at the code, I see that the result is |'d into bi_rw.

Looks good. Sounds like it might have been a real bear to track down.

Reviewed-by: Jeff Moyer <[email protected]>

2010-08-12 13:30:57

by Jeff Moyer

[permalink] [raw]
Subject: Re: [PATCH v2] SCSI: fix bio.bi_rw handling

Jiri Slaby <[email protected]> writes:

> Return of the bi_rw tests is no longer bool after commit 74450be1. So
> testing against constants doesn't make sense anymore. Fix this bug in
> osd_req_read by removing "== 1" in test.
>
> This is not a problem now, where REQ_WRITE is 1, but this can change
> in the future and we don't want to rely on that.
>
> Signed-off-by: Jiri Slaby <[email protected]>
> Cc: "James E.J. Bottomley" <[email protected]>
> Cc: Christoph Hellwig <[email protected]>

Reviewed-by: Jeff Moyer <[email protected]>

2010-08-12 13:37:04

by Jeff Moyer

[permalink] [raw]
Subject: Re: [PATCH v2] BLOCK: fix bio.bi_rw handling

Jiri Slaby <[email protected]> writes:

> Return of the bi_rw tests is no longer bool after commit 74450be1. But
> results of such tests are stored in bools. This doesn't fit in there
> for some compilers (gcc 4.5 here), so either use !! magic to get real
> bools or use ulong where the result is assigned somewhere.
>
> Signed-off-by: Jiri Slaby <[email protected]>
> Cc: Christoph Hellwig <[email protected]>
> Cc: Jens Axboe <[email protected]>

Reviewed-by: Jeff Moyer <[email protected]>

Cheers,
Jeff

2010-08-12 16:06:41

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v2] MD: raid, fix BUG caused by flags handling

Thanks for sending this again. It's been sent at least twice before,
but we really need to get it into the block tree, and now that it's been
merged to mainline there, too.


Reviewed-by: Christoph Hellwig <[email protected]>

2010-08-12 16:06:44

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v2] SCSI: fix bio.bi_rw handling

On Thu, Aug 12, 2010 at 02:31:05PM +0200, Jiri Slaby wrote:
> Return of the bi_rw tests is no longer bool after commit 74450be1. So
> testing against constants doesn't make sense anymore. Fix this bug in
> osd_req_read by removing "== 1" in test.
>
> This is not a problem now, where REQ_WRITE is 1, but this can change
> in the future and we don't want to rely on that.

And it's much cleaner anyway. Note that I think we particularly need
the WARN_ON anyway.


Reviewed-by: Christoph Hellwig <[email protected]>

2010-08-12 16:08:35

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v2] BLOCK: fix bio.bi_rw handling

On Thu, Aug 12, 2010 at 02:31:06PM +0200, Jiri Slaby wrote:
> Return of the bi_rw tests is no longer bool after commit 74450be1. But
> results of such tests are stored in bools. This doesn't fit in there
> for some compilers (gcc 4.5 here), so either use !! magic to get real
> bools or use ulong where the result is assigned somewhere.

I'd have to look at my copy of the C standard if it's guaranteed, but
at least currently the values just get truncated down and bool still
is set to true. I'd much prefer just making these flags unsigned longs
instead of adding syntactic surage to make them true bools.

2010-08-12 16:24:46

by Jiri Slaby

[permalink] [raw]
Subject: Re: [PATCH v2] BLOCK: fix bio.bi_rw handling

On 08/12/2010 06:08 PM, Christoph Hellwig wrote:
> On Thu, Aug 12, 2010 at 02:31:06PM +0200, Jiri Slaby wrote:
>> Return of the bi_rw tests is no longer bool after commit 74450be1. But
>> results of such tests are stored in bools. This doesn't fit in there
>> for some compilers (gcc 4.5 here), so either use !! magic to get real
>> bools or use ulong where the result is assigned somewhere.
>
> I'd have to look at my copy of the C standard if it's guaranteed,

?6.5.3.3 of ANSI C99, par 5:
The result of the logical negation operator ! is 0 if the value of its
operand compares unequal to 0, 1 if the value of its operand compares
equal to 0. The result has type int. The expression !E is equivalent to
(0==E).

On == (?6.5.9 par 3):
The == (equal to) and != (not equal to) operators are analogous to the
relational operators except for their lower precedence. Each of the
operators yields 1 if the specified relation is true and 0 if it is
false. The result has type int. For any pair of operands, exactly one of
the relations is true.

On bool => _Bool (?6.2.5 par 2)
An object declared as type _Bool is large enough to store the values 0
and 1.

So it should be safe :).

BTW just of curiosity, sizeof(bool) is 1 here (8 bits).

regards,
--
js

2010-08-23 10:33:31

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH v2] BLOCK: fix bio.bi_rw handling

On 2010-08-12 14:31, Jiri Slaby wrote:
> Return of the bi_rw tests is no longer bool after commit 74450be1. But
> results of such tests are stored in bools. This doesn't fit in there
> for some compilers (gcc 4.5 here), so either use !! magic to get real
> bools or use ulong where the result is assigned somewhere.

Added, sorry for the delay!

--
Jens Axboe