Btrfs: fix possible panic on unmount
PATCH 1/1 Btrfs: fix possible panic on unmount
We can race with the unmount of an fs and the stopping of a kthread where we will free the block group before we're done using it. The reason for this is because we do not hold a reference on the block group while its caching, since the allocator drops its reference once it exits or moves on to the next block group. This patch fixes the problem by taking a reference to the block group before we start caching and dropping it when we're done to make sure all accesses to the block group are safe. Thanks,
--- fs/btrfs/extent-tree.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
fs/btrfs/extent-tree.c +3 -0
@@ -399,6 +399,8 @@ err:put_caching_control (caching_ctl);atomic_dec (&block_group->space_info->caching_threads); +btrfs_put_block_group (block_group); +return 0 ; }
@@ -439,6 +441,7 @@ static int cache_block_group(struct btrfs_block_group_cache *cache)up_write (&fs_info->extent_commit_sem);atomic_inc (&cache->space_info->caching_threads); +atomic_inc (&cache->count); tsk =kthread_run (caching_kthread, cache,"btrfs-cache-%llu\n" , cache->key.objectid);
Signature · 1 line
1.5.4.3
Quoted 1 line · Josef Bacik
We can race with the unmount of an fs and the stopping of a kthread w=
here we
Quoted 1 line
will free the block group before we're done using it. =A0The reason f=
or this is
Quoted 1 line
because we do not hold a reference on the block group while its cachi=
ng, since
Quoted 1 line
the allocator drops its reference once it exits or moves on to the ne=
xt block
Quoted 1 line
group. =A0This patch fixes the problem by taking a reference to the b=
lock group
Quoted 1 line
before we start caching and dropping it when we're done to make sure =
all
fs/btrfs/extent-tree.c static int cache_block_group(struct btrfs_block_g=
Quoted 21 lines
accesses to the block group are safe. =A0Thanks,
Signed-off-by Josef Bacik <[email protected]>---
=A0fs/btrfs/extent-tree.c | =A0 =A03 +++ =A01 files changed, 3 insertions(+), 0 deletions(-)fs/btrfs/extent-tree.c +2
-0@@ -399,6 +399,8 @@ err: =A0 =A0 =A0 =A0put_caching_control (caching_ctl); =A0 =A0 =A0 =A0atomic_dec(&block_group->space_info->caching_threads); + =A0 =A0 =A0 btrfs_put_block_group(block_group); + =A0 =A0 =A0 =A0return 0; =A0}@@ -439,6 +441,7 @@ static int cache_block_group(struct btrfs_block_g=
roup_cache *cache)
fs/btrfs/extent-tree.c static int cache_block_group(struct btrfs_block_g= · line 444
Quoted 4 lines
fs/btrfs/extent-tree.c +1
-0=A0 =A0 =A0 =A0up_write (&fs_info->extent_commit_sem); =A0 =A0 =A0 =A0atomic_inc(&cache->space_info->caching_threads); + =A0 =A0 =A0 atomic_inc(&cache->count);
It would be slightly more informative/stylish if atomic_inc(&cache->count) is replaced by a cal to
static inline void btrfs_get_block_group(struct btrfs_block_group_cache=
*cache)
{
atomic_inc(&cache->count);
}Regards,
Andrey
Quoted 2 lines
=A0 =A0 =A0 =A0tsk =3D kthread_run(caching_kthread, cache, "btrfs-cac=
he-%llu\n",
Quoted 1 line
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0cache->key.objecti=
d);
Quoted 5 lines
--
1.5.4.3--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs=
" in
Quoted 3 lines
the body of a message to [email protected]
More majordomo info at =A0http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 1 line · Josef Bacik
We can race with the unmount of an fs and the stopping of a kthread w=
here we
Quoted 1 line
will free the block group before we're done using it. =A0The reason f=
or this is
Quoted 1 line
because we do not hold a reference on the block group while its cachi=
ng, since
Quoted 1 line
the allocator drops its reference once it exits or moves on to the ne=
xt block
Quoted 1 line
group. =A0This patch fixes the problem by taking a reference to the b=
lock group
Quoted 1 line
before we start caching and dropping it when we're done to make sure =
all
fs/btrfs/extent-tree.c static int cache_block_group(struct btrfs_block_g=
Quoted 21 lines
accesses to the block group are safe. =A0Thanks,
Signed-off-by Josef Bacik <[email protected]>---
=A0fs/btrfs/extent-tree.c | =A0 =A03 +++ =A01 files changed, 3 insertions(+), 0 deletions(-)fs/btrfs/extent-tree.c +2
-0@@ -399,6 +399,8 @@ err: =A0 =A0 =A0 =A0put_caching_control (caching_ctl); =A0 =A0 =A0 =A0atomic_dec(&block_group->space_info->caching_threads); + =A0 =A0 =A0 btrfs_put_block_group(block_group); + =A0 =A0 =A0 =A0return 0; =A0}@@ -439,6 +441,7 @@ static int cache_block_group(struct btrfs_block_g=
roup_cache *cache)
Quoted 6 lines
fs/btrfs/extent-tree.c +1
-0=A0 =A0 =A0 =A0up_write (&fs_info->extent_commit_sem); =A0 =A0 =A0 =A0atomic_inc(&cache->space_info->caching_threads); + =A0 =A0 =A0 atomic_inc(&cache->count);=A0 =A0 =A0 =A0tsk =3D kthread_run(caching_kthread, cache, "btrfs-cac=
he-%llu\n",
Quoted 1 line
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0cache->key.objecti=
d);
Quoted 1 line
--
This won't work since btrfs_free_block_groups call kfree without checking the reference
count. I think the correct way to fix the race is waiting until caching threads exit
Yan, Zheng
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 1 line · Zheng
On Sat, Nov 14, 2009 at 1:06 AM, Josef Bacik <[email protected]> wrote=
:
Quoted 1 line
Quoted 1 line
We can race with the unmount of an fs and the stopping of a kthread=
where we
Quoted 1 line
Quoted 1 line
will free the block group before we're done using it. =A0The reason=
for this is
Quoted 1 line
Quoted 1 line
because we do not hold a reference on the block group while its cac=
hing, since
Quoted 1 line
Quoted 1 line
the allocator drops its reference once it exits or moves on to the =
next block
Quoted 1 line
Quoted 1 line
group. =A0This patch fixes the problem by taking a reference to the=
block group
Quoted 1 line
Quoted 1 line
before we start caching and dropping it when we're done to make sur=
e all
Quoted 7 lines
Quoted 7 lines
accesses to the block group are safe. =A0Thanks,
Signed-off-by Josef Bacik <[email protected]>---
=A0fs/btrfs/extent-tree.c | =A0 =A03 +++ =A01 files changed, 3 insertions(+), 0 deletions(-)
Quoted 8 lines
Quoted 8 lines
fs/btrfs/extent-tree.c +0
-0@@ -399,6 +399,8 @@ err: =A0 =A0 =A0 =A0put_caching_control(caching_ctl);=A0 =A0 =A0 =A0atomic_dec(&block_group->space_info->caching_threads=
);
Quoted 6 lines
fs/btrfs/extent-tree.c static int cache_block_group(struct btrfs_block=
Quoted 6 lines
+ =A0 =A0 =A0
btrfs_put_block_group(block_group);
+
=A0 =A0 =A0 =A0return 0;
=A0}fs/btrfs/extent-tree.c +0
-0@@ -439,6 +441,7 @@ static int cache_block_group(struct btrfs_block=
_group_cache *cache)
Quoted 6 lines
Quoted 6 lines
fs/btrfs/extent-tree.c +1
-0=A0 =A0 =A0 =A0up_write (&fs_info->extent_commit_sem); =A0 =A0 =A0 =A0atomic_inc(&cache->space_info->caching_threads); + =A0 =A0 =A0 atomic_inc(&cache->count);=A0 =A0 =A0 =A0tsk =3D kthread_run(caching_kthread, cache, "btrfs-c=
ache-%llu\n",
Quoted 1 line
Quoted 1 line
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0cache->key.objec=
tid);
Quoted 7 lines
Quoted 1 line
--
=20
This won't work sincebtrfs_free_block_groupscall kfree without checking the reference
count. I think the correct way to fix the race is waiting until caching threads exit
=20
We do already do that, but the thing is we mark the block group as bein= g
finished, and then go on and do other things with it, hence the race. = The
correct thing to do is do a put in btrfs_free_block_groups so this prob= lem
doesn't happen. I will fix that. Thanks,
Josef
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html