Skip to contentSkip to conversation

bh: Ensure bh fits within a page

BLOCK Herbert Xu 9 participants 22 messages Aug 1, 2006 – Aug 18, 2006 VFS

lore
Share & tools
lore

Herbert Xu

author
#0 lore raw

Hi Andrew:

[BLOCK] bh: Ensure bh fits within a page

There is a bug in jbd with slab debugging enabled where it was submitting a bh obtained via jbd_rep_kmalloc which crossed a page boundary. A lot of time was spent on tracking this down because the symptoms were far off from where the problem was.

This patch adds a sanity check to submit_bh so we can immediately spot anyone doing similar things in future.

While you're at it, could you fix that jbd bug for us :)

Cheers,

Signature · 5 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
fs/buffer.c +1 -0
index 71649ef..b998f08 100644
@@ -2790,6 +2790,7 @@ int submit_bh(int rw, struct buffer_head
 	BUG_ON(!buffer_locked(bh));
 	BUG_ON(!buffer_mapped(bh));
 	BUG_ON(!bh->b_end_io);
+	WARN_ON(bh_offset(bh) + bh->b_size > PAGE_SIZE);
 
 	if (buffer_ordered(bh) && (rw == WRITE))
 		rw = WRITE_BARRIER;
#1 lore raw
Quoted 8 lines · On Tue, 1 Aug 2006 13:04:43 +1000 Herbert Xu <[email protected]>…

There is a bug in jbd with slab debugging enabled where it was submitting a bh obtained via jbd_rep_kmalloc which crossed a page boundary. A lot of time was spent on tracking this down because the symptoms were far off from where the problem was.

This patch adds a sanity check to submit_bh so we can immediately spot anyone doing similar things in future.

Seems sane.

Quoted 3 lines

While you're at it, could you fix that jbd bug for us :)

Could we have a more detailed description?

author
#2 lore raw
Quoted 2 lines · Andrew Morton

Could we have a more detailed description?

Sure, this particular instance is in journal_write_metadata_buffer where the bh may be constructed from kmalloc memory (search for the call to jbd_rep_kmalloc). Because the memory returned by kmalloc may straddle a page (when slab debugging is enabled that is), this causes a broken bh to be injected into submit_bh.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 10 lines · On Tue, 1 Aug 2006 15:02:59 +1000 Herbert Xu <[email protected]>…
Quoted 2 lines · Andrew Morton

Could we have a more detailed description?

Sure, this particular instance is in journal_write_metadata_buffer where the bh may be constructed from kmalloc memory (search for the call to jbd_rep_kmalloc). Because the memory returned by kmalloc may straddle a page (when slab debugging is enabled that is), this causes a broken bh to be injected into submit_bh.

Crap, that's hard to fix. Am I allowed to blame submit_bh()? ;)

uhm, we don't want to lose kmalloc redzoning, so I guess we need to create on-demand ext3-private slab caches for 1024, 2048, and 4096 bytes. With the appropriate slab flags to defeat the redzoning.

author
#4 lore raw
Quoted 2 lines · Andrew Morton

Crap, that's hard to fix. Am I allowed to blame submit_bh()? ;)

Sure you're allowed to do anything :)

Quoted 3 lines

uhm, we don't want to lose kmalloc redzoning, so I guess we need to create on-demand ext3-private slab caches for 1024, 2048, and 4096 bytes. With the appropriate slab flags to defeat the redzoning.

Either that or we should fix redzoning so that it actually preserves alignment, rather than turning off debugging whenever we want alignment. Basically it means that we need to use twice the amount of memory for each object (so 2K for each 1K object). Is this acceptable for debugging?

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#5 lore raw
Quoted 6 lines · On Tue, 1 Aug 2006 21:06:58 +1000 Herbert Xu <[email protected]>…
Quoted 2 lines · Andrew Morton

Crap, that's hard to fix. Am I allowed to blame submit_bh()? ;)

Sure you're allowed to do anything :)

Quoted 8 lines
Quoted 3 lines

uhm, we don't want to lose kmalloc redzoning, so I guess we need to create on-demand ext3-private slab caches for 1024, 2048, and 4096 bytes. With the appropriate slab flags to defeat the redzoning.

Either that or we should fix redzoning so that it actually preserves alignment, rather than turning off debugging whenever we want alignment. Basically it means that we need to use twice the amount of memory for each object (so 2K for each 1K object). Is this acceptable for debugging?

It doesn't sound like a good idea. At present CONFIG_DEBUG_SLAB is acceptable for production use (although it makes kernel profiles look pretty sad). Doubling the size of the slabs would rather alter that.

#6 lore raw
In reply to Andrew Morton
Quoted 7 lines · Andrew Morton
Quoted 6 lines

Sure, this particular instance is in journal_write_metadata_buffer where the bh may be constructed from kmalloc memory (search for the call to jbd_rep_kmalloc). Because the memory returned by kmalloc may straddle a page (when slab debugging is enabled that is), this causes a broken bh to be injected into submit_bh.

Quoted 5 lines

Crap, that's hard to fix. Am I allowed to blame submit_bh()? ;)

uhm, we don't want to lose kmalloc redzoning, so I guess we need to create on-demand ext3-private slab caches for 1024, 2048, and 4096 bytes. With the appropriate slab flags to defeat the redzoning.

The slab allocator gives no guarantee that a structure is not straddling a page boundary regardless of debug or not. It may just happen that the objects are arranged if kmem_cache_cretae() is called with certain parameters. Another arch with other cacheline alignment and another page size may arrange the objects differently.

#7 lore raw

Hi,

Quoted 5 lines · Christoph Lameter

The slab allocator gives no guarantee that a structure is not straddling a page boundary regardless of debug or not. It may just happen that the objects are arranged if kmem_cache_cretae() is called with certain parameters. Another arch with other cacheline alignment and another page size may arrange the objects differently.

Indeed. You could try to force zero-order page allocations for a cache, but we are probably better of using the page allocator directly or crafting a special purpose allocator for the block layer.

Pekka

#8 lore raw
In reply to Christoph Lameter
Quoted 15 lines · Christoph Lameter
Quoted 7 lines · Andrew Morton
Quoted 6 lines

Sure, this particular instance is in journal_write_metadata_buffer where the bh may be constructed from kmalloc memory (search for the call to jbd_rep_kmalloc). Because the memory returned by kmalloc may straddle a page (when slab debugging is enabled that is), this causes a broken bh to be injected into submit_bh.

Quoted 5 lines

Crap, that's hard to fix. Am I allowed to blame submit_bh()? ;)

uhm, we don't want to lose kmalloc redzoning, so I guess we need to create on-demand ext3-private slab caches for 1024, 2048, and 4096 bytes. With the appropriate slab flags to defeat the redzoning.

Quoted 5 lines

The slab allocator gives no guarantee that a structure is not straddling a page boundary regardless of debug or not. It may just happen that the objects are arranged if kmem_cache_cretae() is called with certain parameters. Another arch with other cacheline alignment and another page size may arrange the objects differently.

If you set the alignment for ext3 the same as the size (ie 1024, 2048, 4096 for the above respectively) then wouldn't that guarantee not straddling a page?

-- Steve

#9 lore raw
Quoted 3 lines · Steven Rostedt

If you set the alignment for ext3 the same as the size (ie 1024, 2048, 4096 for the above respectively) then wouldn't that guarantee not straddling a page?

Yes. But then that number must always be a fraction of pagesize.

#10 lore raw
Quoted 8 lines · Christoph Lameter
Quoted 3 lines · Steven Rostedt

If you set the alignment for ext3 the same as the size (ie 1024, 2048, 4096 for the above respectively) then wouldn't that guarantee not straddling a page?

Yes. But then that number must always be a fraction of pagesize.

understood, as is 1024, 2048, and 4096 are. Well, if pagesize is 4096 is 4096 really a fraction of 4096? :)

Also, isn't all sizes for kmalloc that are under pagesize a fraction of the page size? Or more correctly, a power of 2?

-- Steve

#11 lore raw
Quoted 8 lines · Steven Rostedt
Quoted 2 lines

Yes. But then that number must always be a fraction of pagesize.

understood, as is 1024, 2048, and 4096 are. Well, if pagesize is 4096 is 4096 really a fraction of 4096? :)

Also, isn't all sizes for kmalloc that are under pagesize a fraction of the page size? Or more correctly, a power of 2?

Well the size of the object and the alignment must be a fraction of the pagesize. If you get page aligned pages then I wonder why use the slab allocator? The page allocator will be much better suited.

#12 lore raw
In reply to Herbert Xu
Quoted 28 lines · Herbert Xu

Hi Andrew:

[BLOCK] bh: Ensure bh fits within a page

There is a bug in jbd with slab debugging enabled where it was submitting a bh obtained via jbd_rep_kmalloc which crossed a page boundary. A lot of time was spent on tracking this down because the symptoms were far off from where the problem was.

This patch adds a sanity check to submit_bh so we can immediately spot anyone doing similar things in future.

While you're at it, could you fix that jbd bug for us :)

Cheers,

Signature · 5 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
fs/buffer.c +0 -0
index 71649ef..b998f08 100644

fs/buffer.c int submit_bh(int rw, struct buffer_head · line 2793

Quoted 4 lines
fs/buffer.c +1 -0
@@ -2790,6 +2790,7 @@ int submit_bh(int rw, struct buffer_head
 	BUG_ON(!buffer_locked(bh));
 	BUG_ON(!buffer_mapped(bh));
 	BUG_ON(!bh->b_end_io);
+	WARN_ON(bh_offset(bh) + bh->b_size > PAGE_SIZE);

That looks really dangerous, I'd prefer that to be a BUG_ON() as well to prevent nastiness further down.

Signature · 1 line
Jens Axboe
author
#13 lore raw
Quoted 3 lines · Jens Axboe

That looks really dangerous, I'd prefer that to be a BUG_ON() as well to prevent nastiness further down.

OK, I used a WARN_ON mainly because ext3 has been doing this for years without killing anyone until now :)

[BLOCK] bh: Ensure bh fits within a page

There is a bug in jbd with slab debugging enabled where it was submitting a bh obtained via jbd_rep_kmalloc which crossed a page boundary. A lot of time was spent on tracking this down because the symptoms were far off from where the problem was.

This patch adds a sanity check to submit_bh so we can immediately spot anyone doing similar things in future.

Cheers,

Signature · 5 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
fs/buffer.c +1 -0
index 71649ef..ff34881 100644
@@ -2790,6 +2790,7 @@ int submit_bh(int rw, struct buffer_head
 	BUG_ON(!buffer_locked(bh));
 	BUG_ON(!buffer_mapped(bh));
 	BUG_ON(!bh->b_end_io);
+	BUG_ON(bh_offset(bh) + bh->b_size > PAGE_SIZE);
 
 	if (buffer_ordered(bh) && (rw == WRITE))
 		rw = WRITE_BARRIER;
author
#14 lore raw
Quoted 5 lines · Herbert Xu

OK, I used a WARN_ON mainly because ext3 has been doing this for years without killing anyone until now :)

[BLOCK] bh: Ensure bh fits within a page

Actually, the other reason is that we can't BUG_ON until ext3 is fixed to not do this. So I suppose we should keep the WARN_ON until that happens.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#15 lore raw
Quoted 10 lines · Herbert Xu
Quoted 5 lines · Herbert Xu

OK, I used a WARN_ON mainly because ext3 has been doing this for years without killing anyone until now :)

[BLOCK] bh: Ensure bh fits within a page

Actually, the other reason is that we can't BUG_ON until ext3 is fixed to not do this. So I suppose we should keep the WARN_ON until that happens.

Yep, the ext3 bits need to go in first.

Signature · 1 line
Jens Axboe
#16 lore raw
In reply to Herbert Xu
Quoted 7 lines · On Wed, 02 Aug 2006 09:30:51 +1000 Herbert Xu <[email protected]>…
Quoted 3 lines · Jens Axboe

That looks really dangerous, I'd prefer that to be a BUG_ON() as well to prevent nastiness further down.

OK, I used a WARN_ON mainly because ext3 has been doing this for years without killing anyone until now :)

I can't apply either of these patches, because we _know_ it'll trigger.

Once the JBD fix is in hand, we can go with a WARN_ON_ONCE and if that is all clear, then we can make it a BUG_ON().

#17 lore raw
In reply to Herbert Xu
Quoted 15 lines · Herbert Xu
Quoted 3 lines · Jens Axboe

That looks really dangerous, I'd prefer that to be a BUG_ON() as well to prevent nastiness further down.

OK, I used a WARN_ON mainly because ext3 has been doing this for years without killing anyone until now :)

[BLOCK] bh: Ensure bh fits within a page

There is a bug in jbd with slab debugging enabled where it was submitting a bh obtained via jbd_rep_kmalloc which crossed a page boundary. A lot of time was spent on tracking this down because the symptoms were far off from where the problem was.

Quoted 6 lines

This patch adds a sanity check to submit_bh so we can immediately spot anyone doing similar things in future.

Cheers,

Hi,

I am working on a fix to address this problem. Do you have any testcases to reproduce the problem ? I have been running bunch of tests with CONFIG_DEBUG_SLAB with WARN_ON() and I haven't seen it yet :(

Wondering, if there is any special thing I need to do to reproduce the problem ? Please let me know.

Thanks,
Badari

author
#18 lore raw
Quoted 3 lines · Badari Pulavarty

Wondering, if there is any special thing I need to do to reproduce the problem ? Please let me know.

I must say that I didn't try to reproduce the problem. However, here is the bugzilla entry for it. It apparently shows up during installation:

https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=200127

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#19 lore raw
In reply to Herbert Xu

fs/buffer.c int submit_bh(int rw, struct buffer_head · line 2793

Quoted 10 lines · Herbert Xu

--

fs/buffer.c +1 -0
index 71649ef..b998f08 100644
@@ -2790,6 +2790,7 @@ int submit_bh(int rw, struct buffer_head
 	BUG_ON(!buffer_locked(bh));
 	BUG_ON(!buffer_mapped(bh));
 	BUG_ON(!bh->b_end_io);
+	WARN_ON(bh_offset(bh) + bh->b_size > PAGE_SIZE);

What happens when someone implements direct large page IO?

-Andi

#20 lore raw
Quoted 11 lines · Andi Kleen

fs/buffer.c int submit_bh(int rw, struct buffer_head · line 2793

Quoted 9 lines
fs/buffer.c +1 -0
index 71649ef..b998f08 100644
@@ -2790,6 +2790,7 @@ int submit_bh(int rw, struct buffer_head
 	BUG_ON(!buffer_locked(bh));
 	BUG_ON(!buffer_mapped(bh));
 	BUG_ON(!bh->b_end_io);
+	WARN_ON(bh_offset(bh) + bh->b_size > PAGE_SIZE);

What happens when someone implements direct large page IO?

Then they'll need to change submit_bh to generate more than one bvec. At that time they can remove this warning :)

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#21 lore raw
Quoted 13 lines · Herbert Xu
Quoted 11 lines · Andi Kleen

fs/buffer.c int submit_bh(int rw, struct buffer_head · line 2793

Quoted 9 lines
fs/buffer.c +1 -0
index 71649ef..b998f08 100644
@@ -2790,6 +2790,7 @@ int submit_bh(int rw, struct buffer_head
 	BUG_ON(!buffer_locked(bh));
 	BUG_ON(!buffer_mapped(bh));
 	BUG_ON(!bh->b_end_io);
+	WARN_ON(bh_offset(bh) + bh->b_size > PAGE_SIZE);

What happens when someone implements direct large page IO?

Quoted 2 lines

Then they'll need to change submit_bh to generate more than one bvec. At that time they can remove this warning :)

Or just use a proper interface, no new code should touch submit_bh().

Signature · 1 line
Jens Axboe