Skip to content

Commit 1e4f471

Browse files
ereshetovakdave
authored andcommitted
btrfs: convert btrfs_caching_control.count 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: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: David Windsor <dwindsor@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent e76edab commit 1e4f471

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

fs/btrfs/ctree.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <linux/security.h>
4040
#include <linux/sizes.h>
4141
#include <linux/dynamic_debug.h>
42+
#include <linux/refcount.h>
4243
#include "extent_io.h"
4344
#include "extent_map.h"
4445
#include "async-thread.h"
@@ -518,7 +519,7 @@ struct btrfs_caching_control {
518519
struct btrfs_work work;
519520
struct btrfs_block_group_cache *block_group;
520521
u64 progress;
521-
atomic_t count;
522+
refcount_t count;
522523
};
523524

524525
/* Once caching_thread() finds this much free space, it will wake up waiters. */

fs/btrfs/extent-tree.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,14 @@ get_caching_control(struct btrfs_block_group_cache *cache)
316316
}
317317

318318
ctl = cache->caching_ctl;
319-
atomic_inc(&ctl->count);
319+
refcount_inc(&ctl->count);
320320
spin_unlock(&cache->lock);
321321
return ctl;
322322
}
323323

324324
static void put_caching_control(struct btrfs_caching_control *ctl)
325325
{
326-
if (atomic_dec_and_test(&ctl->count))
326+
if (refcount_dec_and_test(&ctl->count))
327327
kfree(ctl);
328328
}
329329

@@ -599,7 +599,7 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
599599
init_waitqueue_head(&caching_ctl->wait);
600600
caching_ctl->block_group = cache;
601601
caching_ctl->progress = cache->key.objectid;
602-
atomic_set(&caching_ctl->count, 1);
602+
refcount_set(&caching_ctl->count, 1);
603603
btrfs_init_work(&caching_ctl->work, btrfs_cache_helper,
604604
caching_thread, NULL, NULL);
605605

@@ -620,7 +620,7 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
620620
struct btrfs_caching_control *ctl;
621621

622622
ctl = cache->caching_ctl;
623-
atomic_inc(&ctl->count);
623+
refcount_inc(&ctl->count);
624624
prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
625625
spin_unlock(&cache->lock);
626626

@@ -707,7 +707,7 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
707707
}
708708

709709
down_write(&fs_info->commit_root_sem);
710-
atomic_inc(&caching_ctl->count);
710+
refcount_inc(&caching_ctl->count);
711711
list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
712712
up_write(&fs_info->commit_root_sem);
713713

@@ -10416,7 +10416,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
1041610416
&fs_info->caching_block_groups, list)
1041710417
if (ctl->block_group == block_group) {
1041810418
caching_ctl = ctl;
10419-
atomic_inc(&caching_ctl->count);
10419+
refcount_inc(&caching_ctl->count);
1042010420
break;
1042110421
}
1042210422
}

0 commit comments

Comments
 (0)