All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2][v2] btrfs: convert block group refcount to refcount_t
@ 2020-07-06 13:14 Josef Bacik
  2020-07-06 13:14 ` [PATCH 2/2] btrfs: fix block group UAF bug with nocow Josef Bacik
  2020-07-09 15:39 ` [PATCH 1/2][v2] btrfs: convert block group refcount to refcount_t David Sterba
  0 siblings, 2 replies; 3+ messages in thread
From: Josef Bacik @ 2020-07-06 13:14 UTC (permalink / raw)
  To: linux-btrfs, kernel-team; +Cc: Filipe Manana

We have refcount_t now with the associated library to handle refcounts,
which gives us extra debugging around reference count mistakes that may
be made.  For example it'll warn on any transition from 0->1 or 0->-1,
which is handy for noticing cases where we've messed up reference
counting.  Convert the block group ref counting from an atomic_t to
refcount_t and use the appropriate helpers.

Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
- rename ->count to ->refs.
- updated commit message.

 fs/btrfs/block-group.c | 8 ++++----
 fs/btrfs/block-group.h | 3 +--
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 3aa78952a2b7..0a67a50f448a 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -118,12 +118,12 @@ u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
 
 void btrfs_get_block_group(struct btrfs_block_group *cache)
 {
-	atomic_inc(&cache->count);
+	refcount_inc(&cache->refs);
 }
 
 void btrfs_put_block_group(struct btrfs_block_group *cache)
 {
-	if (atomic_dec_and_test(&cache->count)) {
+	if (refcount_dec_and_test(&cache->refs)) {
 		WARN_ON(cache->pinned > 0);
 		WARN_ON(cache->reserved > 0);
 
@@ -1805,7 +1805,7 @@ static struct btrfs_block_group *btrfs_create_block_group_cache(
 
 	cache->discard_index = BTRFS_DISCARD_INDEX_UNUSED;
 
-	atomic_set(&cache->count, 1);
+	refcount_set(&cache->refs, 1);
 	spin_lock_init(&cache->lock);
 	init_rwsem(&cache->data_rwsem);
 	INIT_LIST_HEAD(&cache->list);
@@ -3428,7 +3428,7 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
 		ASSERT(list_empty(&block_group->dirty_list));
 		ASSERT(list_empty(&block_group->io_list));
 		ASSERT(list_empty(&block_group->bg_list));
-		ASSERT(atomic_read(&block_group->count) == 1);
+		ASSERT(refcount_read(&block_group->refs) == 1);
 		btrfs_put_block_group(block_group);
 
 		spin_lock(&info->block_group_cache_lock);
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index b6ee70a039c7..adfd7583a17b 100644
--- a/fs/btrfs/block-group.h
+++ b/fs/btrfs/block-group.h
@@ -114,8 +114,7 @@ struct btrfs_block_group {
 	/* For block groups in the same raid type */
 	struct list_head list;
 
-	/* Usage count */
-	atomic_t count;
+	refcount_t refs;
 
 	/*
 	 * List of struct btrfs_free_clusters for this block group.
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] btrfs: fix block group UAF bug with nocow
  2020-07-06 13:14 [PATCH 1/2][v2] btrfs: convert block group refcount to refcount_t Josef Bacik
@ 2020-07-06 13:14 ` Josef Bacik
  2020-07-09 15:39 ` [PATCH 1/2][v2] btrfs: convert block group refcount to refcount_t David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: Josef Bacik @ 2020-07-06 13:14 UTC (permalink / raw)
  To: linux-btrfs, kernel-team; +Cc: Filipe Manana

While debugging a patch that I wrote I was hitting UAF panics when
accessing block groups on unmount.  This turned out to be because in the
nocow case if we bail out of doing the nocow for whatever reason we need
to call btrfs_dec_nocow_writers() if we called the inc.  This puts our
block group, but a few error cases does

if (nocow) {
    btrfs_dec_nocow_writers();
    goto error;
}

unfortunately, error is

error:
	if (nocow)
		btrfs_dec_nocow_writers();

so we get a double put on our block group.  Fix this by dropping the
error cases calling of btrfs_dec_nocow_writers(), as it's handled at the
error label now.

Fixes: 762bf09893b4 ("btrfs: improve error handling in run_delalloc_nocow")
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/inode.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index d894d9e41aad..7c03b402529e 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1688,12 +1688,8 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
 			ret = fallback_to_cow(inode, locked_page,
 					      cow_start, found_key.offset - 1,
 					      page_started, nr_written);
-			if (ret) {
-				if (nocow)
-					btrfs_dec_nocow_writers(fs_info,
-								disk_bytenr);
+			if (ret)
 				goto error;
-			}
 			cow_start = (u64)-1;
 		}
 
@@ -1709,9 +1705,6 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
 					  ram_bytes, BTRFS_COMPRESS_NONE,
 					  BTRFS_ORDERED_PREALLOC);
 			if (IS_ERR(em)) {
-				if (nocow)
-					btrfs_dec_nocow_writers(fs_info,
-								disk_bytenr);
 				ret = PTR_ERR(em);
 				goto error;
 			}
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2][v2] btrfs: convert block group refcount to refcount_t
  2020-07-06 13:14 [PATCH 1/2][v2] btrfs: convert block group refcount to refcount_t Josef Bacik
  2020-07-06 13:14 ` [PATCH 2/2] btrfs: fix block group UAF bug with nocow Josef Bacik
@ 2020-07-09 15:39 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2020-07-09 15:39 UTC (permalink / raw)
  To: Josef Bacik; +Cc: linux-btrfs, kernel-team, Filipe Manana

On Mon, Jul 06, 2020 at 09:14:11AM -0400, Josef Bacik wrote:
> We have refcount_t now with the associated library to handle refcounts,
> which gives us extra debugging around reference count mistakes that may
> be made.  For example it'll warn on any transition from 0->1 or 0->-1,
> which is handy for noticing cases where we've messed up reference
> counting.  Convert the block group ref counting from an atomic_t to
> refcount_t and use the appropriate helpers.
> 
> Reviewed-by: Filipe Manana <fdmanana@suse.com>
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>

1 and 2 added to msic-next, thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-07-09 15:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-06 13:14 [PATCH 1/2][v2] btrfs: convert block group refcount to refcount_t Josef Bacik
2020-07-06 13:14 ` [PATCH 2/2] btrfs: fix block group UAF bug with nocow Josef Bacik
2020-07-09 15:39 ` [PATCH 1/2][v2] btrfs: convert block group refcount to refcount_t David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.