diff options
author | Omar Sandoval <osandov@osandov.com> | 2015-02-24 05:47:05 -0500 |
---|---|---|
committer | Chris Mason <clm@fb.com> | 2015-04-26 09:27:00 -0400 |
commit | 5ca64f45e92dc52bd3bc1ad93f4f9e5a57955f28 (patch) | |
tree | 3e197015904be5b68c3ed94314c4ca2ef0c06051 /fs/btrfs | |
parent | 67b7859e9bfa0dcee5a8256932e393e98739be1b (diff) |
btrfs: fix race on ENOMEM in alloc_extent_buffer
Consider the following interleaving of overlapping calls to
alloc_extent_buffer:
Call 1:
- Successfully allocates a few pages with find_or_create_page
- find_or_create_page fails, goto free_eb
- Unlocks the allocated pages
Call 2:
- Calls find_or_create_page and gets a page in call 1's extent_buffer
- Finds that the page is already associated with an extent_buffer
- Grabs a reference to the half-written extent_buffer and calls
mark_extent_buffer_accessed on it
mark_extent_buffer_accessed will then try to call mark_page_accessed on
a null page and panic.
The fix is to decrement the reference count on the half-written
extent_buffer before unlocking the pages so call 2 won't use it. We
should also set exists = NULL in the case that we don't use exists to
avoid accidentally returning a freed extent_buffer in an error case.
Signed-off-by: Omar Sandoval <osandov@osandov.com>
Reviewed-by: David Sterba <dsterba@suse.cz>
Reviewed-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r-- | fs/btrfs/extent_io.c | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c index 782f3bc4651d..ea100eb188de 100644 --- a/fs/btrfs/extent_io.c +++ b/fs/btrfs/extent_io.c | |||
@@ -4870,6 +4870,7 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, | |||
4870 | mark_extent_buffer_accessed(exists, p); | 4870 | mark_extent_buffer_accessed(exists, p); |
4871 | goto free_eb; | 4871 | goto free_eb; |
4872 | } | 4872 | } |
4873 | exists = NULL; | ||
4873 | 4874 | ||
4874 | /* | 4875 | /* |
4875 | * Do this so attach doesn't complain and we need to | 4876 | * Do this so attach doesn't complain and we need to |
@@ -4933,12 +4934,12 @@ again: | |||
4933 | return eb; | 4934 | return eb; |
4934 | 4935 | ||
4935 | free_eb: | 4936 | free_eb: |
4937 | WARN_ON(!atomic_dec_and_test(&eb->refs)); | ||
4936 | for (i = 0; i < num_pages; i++) { | 4938 | for (i = 0; i < num_pages; i++) { |
4937 | if (eb->pages[i]) | 4939 | if (eb->pages[i]) |
4938 | unlock_page(eb->pages[i]); | 4940 | unlock_page(eb->pages[i]); |
4939 | } | 4941 | } |
4940 | 4942 | ||
4941 | WARN_ON(!atomic_dec_and_test(&eb->refs)); | ||
4942 | btrfs_release_extent_buffer(eb); | 4943 | btrfs_release_extent_buffer(eb); |
4943 | return exists; | 4944 | return exists; |
4944 | } | 4945 | } |