Commit b7ac31b7 authored by Elena Reshetova's avatar Elena Reshetova Committed by David Sterba

btrfs: convert extent_state.refs from atomic_t to refcount_t

refcount_t type and corresponding API should be
used instead of atomic_t when the variable is used as
a reference counter. This allows to avoid accidental
refcounter overflows that might lead to use-after-free
situations.
Signed-off-by: default avatarElena Reshetova <elena.reshetova@intel.com>
Signed-off-by: default avatarHans Liljestrand <ishkamiel@gmail.com>
Signed-off-by: default avatarKees Cook <keescook@chromium.org>
Signed-off-by: default avatarDavid Windsor <dwindsor@gmail.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 0700cea7
...@@ -68,7 +68,7 @@ void btrfs_leak_debug_check(void) ...@@ -68,7 +68,7 @@ void btrfs_leak_debug_check(void)
pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n", pr_err("BTRFS: state leak: start %llu end %llu state %u in tree %d refs %d\n",
state->start, state->end, state->state, state->start, state->end, state->state,
extent_state_in_tree(state), extent_state_in_tree(state),
atomic_read(&state->refs)); refcount_read(&state->refs));
list_del(&state->leak_list); list_del(&state->leak_list);
kmem_cache_free(extent_state_cache, state); kmem_cache_free(extent_state_cache, state);
} }
...@@ -238,7 +238,7 @@ static struct extent_state *alloc_extent_state(gfp_t mask) ...@@ -238,7 +238,7 @@ static struct extent_state *alloc_extent_state(gfp_t mask)
state->failrec = NULL; state->failrec = NULL;
RB_CLEAR_NODE(&state->rb_node); RB_CLEAR_NODE(&state->rb_node);
btrfs_leak_debug_add(&state->leak_list, &states); btrfs_leak_debug_add(&state->leak_list, &states);
atomic_set(&state->refs, 1); refcount_set(&state->refs, 1);
init_waitqueue_head(&state->wq); init_waitqueue_head(&state->wq);
trace_alloc_extent_state(state, mask, _RET_IP_); trace_alloc_extent_state(state, mask, _RET_IP_);
return state; return state;
...@@ -248,7 +248,7 @@ void free_extent_state(struct extent_state *state) ...@@ -248,7 +248,7 @@ void free_extent_state(struct extent_state *state)
{ {
if (!state) if (!state)
return; return;
if (atomic_dec_and_test(&state->refs)) { if (refcount_dec_and_test(&state->refs)) {
WARN_ON(extent_state_in_tree(state)); WARN_ON(extent_state_in_tree(state));
btrfs_leak_debug_del(&state->leak_list); btrfs_leak_debug_del(&state->leak_list);
trace_free_extent_state(state, _RET_IP_); trace_free_extent_state(state, _RET_IP_);
...@@ -641,7 +641,7 @@ static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -641,7 +641,7 @@ static int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
if (cached && extent_state_in_tree(cached) && if (cached && extent_state_in_tree(cached) &&
cached->start <= start && cached->end > start) { cached->start <= start && cached->end > start) {
if (clear) if (clear)
atomic_dec(&cached->refs); refcount_dec(&cached->refs);
state = cached; state = cached;
goto hit_next; goto hit_next;
} }
...@@ -793,7 +793,7 @@ static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -793,7 +793,7 @@ static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
if (state->state & bits) { if (state->state & bits) {
start = state->start; start = state->start;
atomic_inc(&state->refs); refcount_inc(&state->refs);
wait_on_state(tree, state); wait_on_state(tree, state);
free_extent_state(state); free_extent_state(state);
goto again; goto again;
...@@ -834,7 +834,7 @@ static void cache_state_if_flags(struct extent_state *state, ...@@ -834,7 +834,7 @@ static void cache_state_if_flags(struct extent_state *state,
if (cached_ptr && !(*cached_ptr)) { if (cached_ptr && !(*cached_ptr)) {
if (!flags || (state->state & flags)) { if (!flags || (state->state & flags)) {
*cached_ptr = state; *cached_ptr = state;
atomic_inc(&state->refs); refcount_inc(&state->refs);
} }
} }
} }
...@@ -1538,7 +1538,7 @@ static noinline u64 find_delalloc_range(struct extent_io_tree *tree, ...@@ -1538,7 +1538,7 @@ static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
if (!found) { if (!found) {
*start = state->start; *start = state->start;
*cached_state = state; *cached_state = state;
atomic_inc(&state->refs); refcount_inc(&state->refs);
} }
found++; found++;
*end = state->end; *end = state->end;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define __EXTENTIO__ #define __EXTENTIO__
#include <linux/rbtree.h> #include <linux/rbtree.h>
#include <linux/refcount.h>
#include "ulist.h" #include "ulist.h"
/* bits for the extent state */ /* bits for the extent state */
...@@ -143,7 +144,7 @@ struct extent_state { ...@@ -143,7 +144,7 @@ struct extent_state {
/* ADD NEW ELEMENTS AFTER THIS */ /* ADD NEW ELEMENTS AFTER THIS */
wait_queue_head_t wq; wait_queue_head_t wq;
atomic_t refs; refcount_t refs;
unsigned state; unsigned state;
struct io_failure_record *failrec; struct io_failure_record *failrec;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment