linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block_dev: fix crash on chained bios with O_DIRECT
@ 2019-03-20  8:12 Hannes Reinecke
  2019-03-20  8:45 ` Johannes Thumshirn
  0 siblings, 1 reply; 8+ messages in thread
From: Hannes Reinecke @ 2019-03-20  8:12 UTC (permalink / raw)
  To: Jan Kara
  Cc: Jens Axboe, linux-block, linux-fsdevel, Hannes Reinecke, Hannes Reinecke

__blkdev_direct_IO_simple() is allocating a bio on the stack.
When that bio needs to be split bio_chain_endio() invokes bio_put()
on this bio, causing the kernel to crash in mempool_free() as the
bio was never allocated from a mempool in the first place.
So call bio_get() before submitting to avoid this problem.

Signed-off-by: Hannes Reinecke <hare@suse.com>
---
 fs/block_dev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/block_dev.c b/fs/block_dev.c
index c546cdce77e6..4b3a04c3b8bd 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -235,6 +235,7 @@ __blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
 	if (iocb->ki_flags & IOCB_HIPRI)
 		bio.bi_opf |= REQ_HIPRI;
 
+	bio_get(&bio);
 	qc = submit_bio(&bio);
 	for (;;) {
 		set_current_state(TASK_UNINTERRUPTIBLE);
@@ -254,7 +255,7 @@ __blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
 
 	if (unlikely(bio.bi_status))
 		ret = blk_status_to_errno(bio.bi_status);
-
+	bio_put(&bio);
 out:
 	if (vecs != inline_vecs)
 		kfree(vecs);
-- 
2.16.4


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] block_dev: fix crash on chained bios with O_DIRECT
  2019-03-20  8:12 [PATCH] block_dev: fix crash on chained bios with O_DIRECT Hannes Reinecke
@ 2019-03-20  8:45 ` Johannes Thumshirn
  2019-03-20  8:51   ` Hannes Reinecke
  0 siblings, 1 reply; 8+ messages in thread
From: Johannes Thumshirn @ 2019-03-20  8:45 UTC (permalink / raw)
  To: Hannes Reinecke, Jan Kara
  Cc: Jens Axboe, linux-block, linux-fsdevel, Hannes Reinecke

On 20/03/2019 09:12, Hannes Reinecke wrote:
> __blkdev_direct_IO_simple() is allocating a bio on the stack.
> When that bio needs to be split bio_chain_endio() invokes bio_put()
> on this bio, causing the kernel to crash in mempool_free() as the
> bio was never allocated from a mempool in the first place.
> So call bio_get() before submitting to avoid this problem.

Hmm this sounds as if we're just papering over the real issue here,
which is calling bio_free() for bios not allocated using bio_alloc_bioset().

How about the following untested patch:

From 9c8434e5bf81595e97ea5647437d12bfce0e37b6 Mon Sep 17 00:00:00 2001
From: Johannes Thumshirn <jthumshirn@suse.de>
Date: Wed, 20 Mar 2019 09:40:18 +0100
Subject: [PATCH] bio: Introduce BIO_ALLOCED flag and check it in bio_free

When we're submitting a bio from stack and this ends up being split, we
call bio_put(). bio_put() will eventually call bio_free() if the reference
count drops to 0. But freeing the bio is wrong, as it was never allocated
out of the bio's mempool.

Flag each normally allocated bio as 'BIO_ALLOCATED' and skip freeing if the
flag isn't set.

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
---
 block/bio.c               | 4 ++++
 include/linux/blk_types.h | 1 +
 2 files changed, 5 insertions(+)

diff --git a/block/bio.c b/block/bio.c
index 4db1008309ed..caa8bc076377 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -253,6 +253,9 @@ static void bio_free(struct bio *bio)
 	struct bio_set *bs = bio->bi_pool;
 	void *p;

+	if (!bio_flagged(bio, BIO_ALLOCED))
+		return;
+
 	bio_uninit(bio);

 	if (bs) {
@@ -521,6 +524,7 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask,
unsigned int nr_iovecs,
 		bvl = bio->bi_inline_vecs;
 	}

+	bio_set_flag(bio, BIO_ALLOCED);
 	bio->bi_pool = bs;
 	bio->bi_max_vecs = nr_iovecs;
 	bio->bi_io_vec = bvl;
diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h
index d66bf5f32610..14b4f87a1eab 100644
--- a/include/linux/blk_types.h
+++ b/include/linux/blk_types.h
@@ -229,6 +229,7 @@ struct bio {
 				 * of this bio. */
 #define BIO_QUEUE_ENTERED 11	/* can use blk_queue_enter_live() */
 #define BIO_TRACKED 12		/* set if bio goes through the rq_qos path */
+#define BIO_ALLOCED 13		/* set if the bio was allocated by
bio_alloc_bioset */

 /* See BVEC_POOL_OFFSET below before adding new flags */

-- 
2.16.4



-- 
Johannes Thumshirn                            SUSE Labs Filesystems
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] block_dev: fix crash on chained bios with O_DIRECT
  2019-03-20  8:45 ` Johannes Thumshirn
@ 2019-03-20  8:51   ` Hannes Reinecke
  2019-03-20  8:53     ` Johannes Thumshirn
  0 siblings, 1 reply; 8+ messages in thread
From: Hannes Reinecke @ 2019-03-20  8:51 UTC (permalink / raw)
  To: Johannes Thumshirn, Hannes Reinecke, Jan Kara
  Cc: Jens Axboe, linux-block, linux-fsdevel

On 3/20/19 9:45 AM, Johannes Thumshirn wrote:
> On 20/03/2019 09:12, Hannes Reinecke wrote:
>> __blkdev_direct_IO_simple() is allocating a bio on the stack.
>> When that bio needs to be split bio_chain_endio() invokes bio_put()
>> on this bio, causing the kernel to crash in mempool_free() as the
>> bio was never allocated from a mempool in the first place.
>> So call bio_get() before submitting to avoid this problem.
> 
> Hmm this sounds as if we're just papering over the real issue here,
> which is calling bio_free() for bios not allocated using bio_alloc_bioset().
> 
> How about the following untested patch:
> 
>  From 9c8434e5bf81595e97ea5647437d12bfce0e37b6 Mon Sep 17 00:00:00 2001
> From: Johannes Thumshirn <jthumshirn@suse.de>
> Date: Wed, 20 Mar 2019 09:40:18 +0100
> Subject: [PATCH] bio: Introduce BIO_ALLOCED flag and check it in bio_free
> 
> When we're submitting a bio from stack and this ends up being split, we
> call bio_put(). bio_put() will eventually call bio_free() if the reference
> count drops to 0. But freeing the bio is wrong, as it was never allocated
> out of the bio's mempool.
> 
> Flag each normally allocated bio as 'BIO_ALLOCATED' and skip freeing if the
> flag isn't set.
> 
> Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
> ---
>   block/bio.c               | 4 ++++
>   include/linux/blk_types.h | 1 +
>   2 files changed, 5 insertions(+)
> 
> diff --git a/block/bio.c b/block/bio.c
> index 4db1008309ed..caa8bc076377 100644
> --- a/block/bio.c
> +++ b/block/bio.c
> @@ -253,6 +253,9 @@ static void bio_free(struct bio *bio)
>   	struct bio_set *bs = bio->bi_pool;
>   	void *p;
> 
> +	if (!bio_flagged(bio, BIO_ALLOCED))
> +		return;
> +
>   	bio_uninit(bio);
> 
>   	if (bs) {
> @@ -521,6 +524,7 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask,
> unsigned int nr_iovecs,
>   		bvl = bio->bi_inline_vecs;
>   	}
> 
> +	bio_set_flag(bio, BIO_ALLOCED);
>   	bio->bi_pool = bs;
>   	bio->bi_max_vecs = nr_iovecs;
>   	bio->bi_io_vec = bvl;
> diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h
> index d66bf5f32610..14b4f87a1eab 100644
> --- a/include/linux/blk_types.h
> +++ b/include/linux/blk_types.h
> @@ -229,6 +229,7 @@ struct bio {
>   				 * of this bio. */
>   #define BIO_QUEUE_ENTERED 11	/* can use blk_queue_enter_live() */
>   #define BIO_TRACKED 12		/* set if bio goes through the rq_qos path */
> +#define BIO_ALLOCED 13		/* set if the bio was allocated by
> bio_alloc_bioset */
> 
>   /* See BVEC_POOL_OFFSET below before adding new flags */
> 
Yeah, should work, too.
But we should be calling bio_uninit() for all bios.

Will you be sending an updated patch?

Cheers,

Hannes
-- 
Dr. Hannes Reinecke		               zSeries & Storage
hare@suse.com			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] block_dev: fix crash on chained bios with O_DIRECT
  2019-03-20  8:51   ` Hannes Reinecke
@ 2019-03-20  8:53     ` Johannes Thumshirn
  2019-03-20 11:47       ` Jan Kara
  0 siblings, 1 reply; 8+ messages in thread
From: Johannes Thumshirn @ 2019-03-20  8:53 UTC (permalink / raw)
  To: Hannes Reinecke, Hannes Reinecke, Jan Kara
  Cc: Jens Axboe, linux-block, linux-fsdevel

On 20/03/2019 09:51, Hannes Reinecke wrote:
> Yeah, should work, too.
> But we should be calling bio_uninit() for all bios.

Yup, probably.

> Will you be sending an updated patch?

Let's wait what other's thing first.

-- 
Johannes Thumshirn                            SUSE Labs Filesystems
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] block_dev: fix crash on chained bios with O_DIRECT
  2019-03-20  8:53     ` Johannes Thumshirn
@ 2019-03-20 11:47       ` Jan Kara
  2019-03-20 13:19         ` Johannes Thumshirn
  0 siblings, 1 reply; 8+ messages in thread
From: Jan Kara @ 2019-03-20 11:47 UTC (permalink / raw)
  To: Johannes Thumshirn
  Cc: Hannes Reinecke, Hannes Reinecke, Jan Kara, Jens Axboe,
	linux-block, linux-fsdevel

On Wed 20-03-19 09:53:10, Johannes Thumshirn wrote:
> On 20/03/2019 09:51, Hannes Reinecke wrote:
> > Yeah, should work, too.
> > But we should be calling bio_uninit() for all bios.
> 
> Yup, probably.
> 
> > Will you be sending an updated patch?
> 
> Let's wait what other's thing first.

FWIW I'm OK with either solution. Yours seems a bit more future-proof so I
like it a bit more.

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] block_dev: fix crash on chained bios with O_DIRECT
  2019-03-20 11:47       ` Jan Kara
@ 2019-03-20 13:19         ` Johannes Thumshirn
  2019-03-20 19:57           ` Jens Axboe
  0 siblings, 1 reply; 8+ messages in thread
From: Johannes Thumshirn @ 2019-03-20 13:19 UTC (permalink / raw)
  To: Jan Kara
  Cc: Hannes Reinecke, Hannes Reinecke, Jan Kara, Jens Axboe,
	linux-block, linux-fsdevel

On 20/03/2019 12:47, Jan Kara wrote:
> On Wed 20-03-19 09:53:10, Johannes Thumshirn wrote:
>> On 20/03/2019 09:51, Hannes Reinecke wrote:
>>> Yeah, should work, too.
>>> But we should be calling bio_uninit() for all bios.
>>
>> Yup, probably.
>>
>>> Will you be sending an updated patch?
>>
>> Let's wait what other's thing first.
> 
> FWIW I'm OK with either solution. Yours seems a bit more future-proof so I
> like it a bit more.

FWIW Bit 13 for the Flag doesn't work, need to find a free one before
doing a proper submission.



-- 
Johannes Thumshirn                            SUSE Labs Filesystems
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] block_dev: fix crash on chained bios with O_DIRECT
  2019-03-20 13:19         ` Johannes Thumshirn
@ 2019-03-20 19:57           ` Jens Axboe
  2019-03-21  8:28             ` Johannes Thumshirn
  0 siblings, 1 reply; 8+ messages in thread
From: Jens Axboe @ 2019-03-20 19:57 UTC (permalink / raw)
  To: Johannes Thumshirn, Jan Kara
  Cc: Hannes Reinecke, Hannes Reinecke, Jan Kara, linux-block, linux-fsdevel

On 3/20/19 7:19 AM, Johannes Thumshirn wrote:
> On 20/03/2019 12:47, Jan Kara wrote:
>> On Wed 20-03-19 09:53:10, Johannes Thumshirn wrote:
>>> On 20/03/2019 09:51, Hannes Reinecke wrote:
>>>> Yeah, should work, too.
>>>> But we should be calling bio_uninit() for all bios.
>>>
>>> Yup, probably.
>>>
>>>> Will you be sending an updated patch?
>>>
>>> Let's wait what other's thing first.
>>
>> FWIW I'm OK with either solution. Yours seems a bit more future-proof so I
>> like it a bit more.
> 
> FWIW Bit 13 for the Flag doesn't work, need to find a free one before
> doing a proper submission.

Yeah, you're going to overlap and crash... We really should have a build
bug on for that.

We don't have any free ones. I've got a patch in io_uring-next that
uses the last one.

That said, I do greatly prefer your approach to solving the issue.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] block_dev: fix crash on chained bios with O_DIRECT
  2019-03-20 19:57           ` Jens Axboe
@ 2019-03-21  8:28             ` Johannes Thumshirn
  0 siblings, 0 replies; 8+ messages in thread
From: Johannes Thumshirn @ 2019-03-21  8:28 UTC (permalink / raw)
  To: Jens Axboe, Jan Kara
  Cc: Hannes Reinecke, Hannes Reinecke, Jan Kara, linux-block, linux-fsdevel

On 20/03/2019 20:57, Jens Axboe wrote:
> Yeah, you're going to overlap and crash... We really should have a build
> bug on for that.
> 
> We don't have any free ones. I've got a patch in io_uring-next that
> uses the last one.

Damn it, I have updated the patch to use 0 as well.

> That said, I do greatly prefer your approach to solving the issue.

Any ideas to proceed from here?

-- 
Johannes Thumshirn                            SUSE Labs Filesystems
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-03-21  8:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-20  8:12 [PATCH] block_dev: fix crash on chained bios with O_DIRECT Hannes Reinecke
2019-03-20  8:45 ` Johannes Thumshirn
2019-03-20  8:51   ` Hannes Reinecke
2019-03-20  8:53     ` Johannes Thumshirn
2019-03-20 11:47       ` Jan Kara
2019-03-20 13:19         ` Johannes Thumshirn
2019-03-20 19:57           ` Jens Axboe
2019-03-21  8:28             ` Johannes Thumshirn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).