Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5ca64f45 authored by Omar Sandoval's avatar Omar Sandoval Committed by Chris Mason
Browse files

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: default avatarOmar Sandoval <osandov@osandov.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.cz>
Reviewed-by: default avatarLiu Bo <bo.li.liu@oracle.com>
Signed-off-by: default avatarChris Mason <clm@fb.com>
parent 67b7859e
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -4870,6 +4870,7 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
				mark_extent_buffer_accessed(exists, p);
				mark_extent_buffer_accessed(exists, p);
				goto free_eb;
				goto free_eb;
			}
			}
			exists = NULL;


			/*
			/*
			 * Do this so attach doesn't complain and we need to
			 * Do this so attach doesn't complain and we need to
@@ -4933,12 +4934,12 @@ again:
	return eb;
	return eb;


free_eb:
free_eb:
	WARN_ON(!atomic_dec_and_test(&eb->refs));
	for (i = 0; i < num_pages; i++) {
	for (i = 0; i < num_pages; i++) {
		if (eb->pages[i])
		if (eb->pages[i])
			unlock_page(eb->pages[i]);
			unlock_page(eb->pages[i]);
	}
	}


	WARN_ON(!atomic_dec_and_test(&eb->refs));
	btrfs_release_extent_buffer(eb);
	btrfs_release_extent_buffer(eb);
	return exists;
	return exists;
}
}