btrfs: fix sleep from invalid context bug in update_qgroup_limit_item()
v4 cover btrfs: fix sleep from invalid context bug in update_qgroup_limit_item()
At least 3 places might sleep in update_qgroup_limit_item(), as shown below:
update_qgroup_limit_item
btrfs_alloc_path
/* allocate memory non-atomically, might sleep */
kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS)
btrfs_search_slot
setup_nodes_for_search
reada_for_balance
btrfs_readahead_node_child
btrfs_readahead_tree_block
btrfs_find_create_tree_block
alloc_extent_buffer
kmem_cache_zalloc
/* allocate memory non-atomically, might sleep */
kmem_cache_alloc(GFP_NOFS|__GFP_NOFAIL|__GFP_ZERO)
read_extent_buffer_pages
submit_extent_page
/* disk IO, might sleep */
submit_one_bioFix this by delaying the limit item updates until unlock the spin lock.
By the way, add might_sleep() to some places.
ChenXiaoSong (3):
btrfs: add might_sleep() to some places in update_qgroup_limit_item()
btrfs: qgroup: introduce btrfs_update_quoto_limit() helper
btrfs: qgroup: fix sleep from invalid context bug in
update_qgroup_limit_item()
fs/btrfs/ctree.c | 2 ++ fs/btrfs/qgroup.c | 45 ++++++++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 19 deletions(-)
Signature · 1 line
2.31.1
PATCH v4 1/3 btrfs: add might_sleep() to some places in update_qgroup_limit_item()
As the potential sleeping under spin lock is hard to spot, we should add might_sleep() to some places.
--- fs/btrfs/ctree.c | 2 ++ fs/btrfs/qgroup.c | 2 ++ 2 files changed, 4 insertions(+)
fs/btrfs/ctree.c +2 -0
@@ -1934,6 +1934,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,int min_write_lock_level;int prev_cmp; +might_sleep (); + lowest_level = p->lowest_level;WARN_ON (lowest_level && ins_len >0 );WARN_ON (p->nodes[0 ] !=NULL );
fs/btrfs/qgroup.c +2 -0
@@ -779,6 +779,8 @@ static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,int ret;int slot; +might_sleep (); + key.objectid =0 ; key.type =BTRFS_QGROUP_LIMIT_KEY ; key.offset = qgroup->qgroupid;
Signature · 1 line
2.31.1
Re: [PATCH v4 1/3] btrfs: add might_sleep() to some places in update_qgroup_limit_item()
Quoted 14 lines · ChenXiaoSong
As the potential sleeping under spin lock is hard to spot, we should add
might_sleep()to some places.Signed-off-by ChenXiaoSong <[email protected]>--- fs/btrfs/ctree.c | 2 ++ fs/btrfs/qgroup.c | 2 ++ 2 files changed, 4 insertions(+)fs/btrfs/ctree.c +0
-0
fs/btrfs/ctree.c int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, · line 1937
Quoted 4 lines
fs/btrfs/ctree.c +1
-0@@ -1934,6 +1934,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,int min_write_lock_level;int prev_cmp; +might_sleep ();
This needs some explanation in the changelog, the reason was mentioned in some past patch iteration that it's due to potential IO fi the blocks are not cached.
fs/btrfs/qgroup.c static int update_qgroup_limit_item(struct btrfs_trans_handle *trans, · line 782
Quoted 13 lines
fs/btrfs/ctree.c +1
-0+ lowest_level = p->lowest_level;WARN_ON (lowest_level && ins_len >0 );WARN_ON (p->nodes[0 ] !=NULL );fs/btrfs/qgroup.c +1
-0@@ -779,6 +779,8 @@ static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,int ret;int slot; +might_sleep ();
This one is redundant, no? There's call to btrfs_search_slot a few lines below.
Quoted 7 lines
fs/btrfs/qgroup.c +1
-0+ key.objectid =0 ; key.type =BTRFS_QGROUP_LIMIT_KEY ; key.offset = qgroup->qgroupid;Signature · 1 line
2.31.1
Re: [PATCH v4 1/3] btrfs: add might_sleep() to some places in update_qgroup_limit_item()
In reply to ChenXiaoSongQuoted 4 lines · ChenXiaoSong
As the potential sleeping under spin lock is hard to spot, we should add
might_sleep()to some places.Signed-off-by ChenXiaoSong <[email protected]>
Looks good.
We may want to add more in other locations, but this is really a good start.
Thanks,
Qu
Quoted 31 lines
--- fs/btrfs/ctree.c | 2 ++ fs/btrfs/qgroup.c | 2 ++ 2 files changed, 4 insertions(+)fs/btrfs/ctree.c +2
-0@@ -1934,6 +1934,8 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,int min_write_lock_level;int prev_cmp; +might_sleep (); + lowest_level = p->lowest_level;WARN_ON (lowest_level && ins_len >0 );WARN_ON (p->nodes[0 ] !=NULL );fs/btrfs/qgroup.c +2
-0@@ -779,6 +779,8 @@ static int update_qgroup_limit_item(struct btrfs_trans_handle *trans,int ret;int slot; +might_sleep (); + key.objectid =0 ; key.type =BTRFS_QGROUP_LIMIT_KEY ; key.offset = qgroup->qgroupid;
Re: [PATCH v4 1/3] btrfs: add might_sleep() to some places in update_qgroup_limit_item()
Quoted 8 lines · Qu Wenruo
Looks good.
We may want to add more in other locations, but this is really a good start.
Reviewed-by Qu Wenruo <[email protected]>Thanks,
Qu
If I just add might_sleep() in btrfs_alloc_path() and btrfs_search_slot(), is it reasonable?
Or just add might_sleep() to one place in update_qgroup_limit_item() ?
Re: [PATCH v4 1/3] btrfs: add might_sleep() to some places in update_qgroup_limit_item()
Quoted 11 lines · ChenXiaoSong
Quoted 8 lines · Qu Wenruo
Looks good.
We may want to add more in other locations, but this is really a good start.
Reviewed-by Qu Wenruo <[email protected]>Thanks,
QuIf I just add
might_sleep()inbtrfs_alloc_path()andbtrfs_search_slot(), is it reasonable?
Adding it to btrfs_search_slot() is definitely correct.
But why for btrfs_alloc_path()? Wouldn't kmem_cache_zalloc() itself already do the might_sleep_if() somewhere?
I just looked the call chain, and indeed it is doing the check already:
btrfs_alloc_path()
|- kmem_cache_zalloc()
|- kmem_cache_alloc()
|- __kmem_cache_alloc_lru()
|- slab_alloc()
|- slab_alloc_node()
|- slab_pre_alloc_hook()
|- might_alloc()
|- might_sleep_if()Thanks,
Qu
Quoted 2 lines
Or just add
might_sleep()to one place inupdate_qgroup_limit_item()?
Re: [PATCH v4 1/3] btrfs: add might_sleep() to some places in update_qgroup_limit_item()
Quoted 26 lines · Qu Wenruo
Quoted 6 lines · ChenXiaoSong
Quoted 5 lines · Qu Wenruo
Looks good.
We may want to add more in other locations, but this is really a good start.
Quoted 7 lines
Quoted 4 lines
Reviewed-by Qu Wenruo <[email protected]>Thanks,
QuIf I just add
might_sleep()inbtrfs_alloc_path()andbtrfs_search_slot(), is it reasonable?Adding it to
btrfs_search_slot()is definitely correct.But why for
btrfs_alloc_path()? Wouldn'tkmem_cache_zalloc()itself already do themight_sleep_if()somewhere?I just looked the call chain, and indeed it is doing the check already:
btrfs_alloc_path()
|-kmem_cache_zalloc()
Quoted 7 lines
|- kmem_cache_alloc()
|- __kmem_cache_alloc_lru()
|- slab_alloc()
|- slab_alloc_node()
|- slab_pre_alloc_hook()
|- might_alloc()
|- might_sleep_if()The call chaing is unconditional so the check will always happen but the condition itself in might_sleep_if does not recognize GFP_NOFS:
34 static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags) 35 {
36 return !!(gfp_flags & __GFP_DIRECT_RECLAIM);
37 }
#define GFP_NOFS (__GFP_RECLAIM | __GFP_IO)
And I think the qgroup limit was exactly a spin lock over btrfs_path_alloc so it did not help. An might_sleep() inside btrfs_path_alloc() is a very minimal but reliable check we could add, the paths are used in many places so it would increase the coverage.
Re: [PATCH v4 1/3] btrfs: add might_sleep() to some places in update_qgroup_limit_item()
Quoted 43 lines · David Sterba
Quoted 26 lines · Qu Wenruo
Quoted 6 lines · ChenXiaoSong
Quoted 5 lines · Qu Wenruo
Looks good.
We may want to add more in other locations, but this is really a good start.
Quoted 7 lines
Quoted 4 lines
Reviewed-by Qu Wenruo <[email protected]>Thanks,
QuIf I just add
might_sleep()inbtrfs_alloc_path()andbtrfs_search_slot(), is it reasonable?Adding it to
btrfs_search_slot()is definitely correct.But why for
btrfs_alloc_path()? Wouldn'tkmem_cache_zalloc()itself already do themight_sleep_if()somewhere?I just looked the call chain, and indeed it is doing the check already:
btrfs_alloc_path()
|-kmem_cache_zalloc()Quoted 7 lines
|- kmem_cache_alloc() |- __kmem_cache_alloc_lru() |- slab_alloc() |- slab_alloc_node() |- slab_pre_alloc_hook() |- might_alloc() |- might_sleep_if()The call chaing is unconditional so the check will always happen but the condition itself in
might_sleep_ifdoes not recognizeGFP_NOFS:34 static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags) 35 { 36 return !!(gfp_flags & __GFP_DIRECT_RECLAIM); 37 }
Quoted 6 lines
#define
GFP_NOFS(__GFP_RECLAIM|__GFP_IO)And I think the qgroup limit was exactly a spin lock over
btrfs_path_allocso it did not help. Anmight_sleep()insidebtrfs_path_alloc()is a very minimal but reliable check we could add, the paths are used in many places so it would increase the coverage.
OK, then it makes sense now for btrfs_alloc_path().
But I still believe this looks like a bug in gfpflags_allow_blocking()...
Thanks,
Qu
PATCH v4 2/3 btrfs: qgroup: introduce btrfs_update_quoto_limit() helper
In reply to ChenXiaoSongNo functional changed. Just simplify the code.
--- fs/btrfs/qgroup.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
fs/btrfs/qgroup.c +17 -17
@@ -1677,6 +1677,19 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)return ret; } +static int btrfs_update_quoto_limit (struct btrfs_trans_handle *trans, +struct btrfs_qgroup *qgroup, +struct btrfs_fs_info *fs_info) +{ +int ret =update_qgroup_limit_item (trans, qgroup); +if (ret) { +qgroup_mark_inconsistent (fs_info); +btrfs_info (fs_info,"unable to update quota limit for %llu" , + qgroup->qgroupid); + } +return ret; +} +int btrfs_limit_qgroup (struct btrfs_trans_handle *trans,u64 qgroupid,struct btrfs_qgroup_limit *limit) {
@@ -1742,13 +1755,7 @@ int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,spin_unlock (&fs_info->qgroup_lock); - ret =update_qgroup_limit_item (trans, qgroup); -if (ret) { -qgroup_mark_inconsistent (fs_info); -btrfs_info (fs_info,"unable to update quota limit for %llu" , - qgroupid); - } - + ret =btrfs_update_quoto_limit (trans, qgroup, fs_info); out:mutex_unlock (&fs_info->qgroup_ioctl_lock);return ret;
@@ -2824,9 +2831,7 @@ int btrfs_run_qgroups(struct btrfs_trans_handle *trans) ret =update_qgroup_info_item (trans, qgroup);if (ret)qgroup_mark_inconsistent (fs_info); - ret =update_qgroup_limit_item (trans, qgroup); -if (ret) -qgroup_mark_inconsistent (fs_info); + ret =btrfs_update_quoto_limit (trans, qgroup, fs_info);spin_lock (&fs_info->qgroup_lock); }if (test_bit (BTRFS_FS_QUOTA_ENABLED , &fs_info->flags))
@@ -2953,14 +2958,9 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->rsv_rfer = inherit->lim.rsv_rfer; dstgroup->rsv_excl = inherit->lim.rsv_excl; - ret =update_qgroup_limit_item (trans, dstgroup); -if (ret) { -qgroup_mark_inconsistent (fs_info); -btrfs_info (fs_info, -"unable to update quota limit for %llu" , - dstgroup->qgroupid); + ret =btrfs_update_quoto_limit (trans, dstgroup, fs_info); +if (ret) goto unlock; - } }if (srcid) {
Signature · 1 line
2.31.1
Re: [PATCH v4 2/3] btrfs: qgroup: introduce btrfs_update_quoto_limit() helper
Quoted 1 line · ChenXiaoSong
No functional changed. Just simplify the code.
Please write some description of the change, like "factor out quota limit update that also marks qgroup as inconsistent if it fals".
Quoted 10 lines
Signed-off-by ChenXiaoSong <[email protected]>--- fs/btrfs/qgroup.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)fs/btrfs/qgroup.c +0
-0
fs/btrfs/qgroup.c int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid) · line 1680
Quoted 4 lines
fs/btrfs/qgroup.c +1
-0@@ -1677,6 +1677,19 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)return ret; } +static int btrfs_update_quoto_limit(struct btrfs_trans_handle *trans,
Typo 'quoto' and it should rather be qgroup as the limit is related to a qgroup, quota is usually in connection with the whole subsystem.
Quoted 61 lines
fs/btrfs/qgroup.c +16
-17+struct btrfs_qgroup *qgroup, + struct btrfs_fs_info *fs_info) +{ + int ret = update_qgroup_limit_item(trans, qgroup); +if (ret) { +qgroup_mark_inconsistent (fs_info); +btrfs_info (fs_info,"unable to update quota limit for %llu" , + qgroup->qgroupid); + } +return ret; +} +int btrfs_limit_qgroup (struct btrfs_trans_handle *trans,u64 qgroupid,struct btrfs_qgroup_limit *limit) {@@ -1742,13 +1755,7 @@ int btrfs_limit_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid,spin_unlock (&fs_info->qgroup_lock); - ret =update_qgroup_limit_item (trans, qgroup); -if (ret) { -qgroup_mark_inconsistent (fs_info); -btrfs_info (fs_info,"unable to update quota limit for %llu" , - qgroupid); - } - + ret =btrfs_update_quoto_limit (trans, qgroup, fs_info); out:mutex_unlock (&fs_info->qgroup_ioctl_lock);return ret;@@ -2824,9 +2831,7 @@ int btrfs_run_qgroups(struct btrfs_trans_handle *trans) ret =update_qgroup_info_item (trans, qgroup);if (ret)qgroup_mark_inconsistent (fs_info); - ret =update_qgroup_limit_item (trans, qgroup); -if (ret) -qgroup_mark_inconsistent (fs_info); + ret =btrfs_update_quoto_limit (trans, qgroup, fs_info);spin_lock (&fs_info->qgroup_lock); }if (test_bit (BTRFS_FS_QUOTA_ENABLED , &fs_info->flags))@@ -2953,14 +2958,9 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->rsv_rfer = inherit->lim.rsv_rfer; dstgroup->rsv_excl = inherit->lim.rsv_excl; - ret =update_qgroup_limit_item (trans, dstgroup); -if (ret) { -qgroup_mark_inconsistent (fs_info); -btrfs_info (fs_info, -"unable to update quota limit for %llu" , - dstgroup->qgroupid); + ret =btrfs_update_quoto_limit (trans, dstgroup, fs_info); +if (ret) goto unlock; - } }if (srcid) {Signature · 1 line
2.31.1
PATCH v4 3/3 btrfs: qgroup: fix sleep from invalid context bug in update_qgroup_limit_item()
In reply to ChenXiaoSongSyzkaller reported BUG as follows:
BUG: sleeping function called from invalid context at
include/linux/sched/mm.h:274
Call Trace:
<TASK>
dump_stack_lvl+0xcd/0x134
__might_resched.cold+0x222/0x26b
kmem_cache_alloc+0x2e7/0x3c0
update_qgroup_limit_item+0xe1/0x390
btrfs_qgroup_inherit+0x147b/0x1ee0
create_subvol+0x4eb/0x1710
btrfs_mksubvol+0xfe5/0x13f0
__btrfs_ioctl_snap_create+0x2b0/0x430
btrfs_ioctl_snap_create_v2+0x25a/0x520
btrfs_ioctl+0x2a1c/0x5ce0
__x64_sys_ioctl+0x193/0x200
do_syscall_64+0x35/0x80Fix this by delaying the limit item updates until unlock the spin lock.
--- fs/btrfs/qgroup.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
fs/btrfs/qgroup.c +9 -4
@@ -2867,6 +2867,8 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,bool need_rescan = false;u32 level_size =0 ;u64 nums; +bool update_limit = false; +int err;/* * There are only two callers of this function.
@@ -2957,10 +2959,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->max_excl = inherit->lim.max_excl; dstgroup->rsv_rfer = inherit->lim.rsv_rfer; dstgroup->rsv_excl = inherit->lim.rsv_excl; - - ret =btrfs_update_quoto_limit (trans, dstgroup, fs_info); -if (ret) - goto unlock; + update_limit = true; }if (srcid) {
@@ -2987,6 +2986,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->max_excl = srcgroup->max_excl; dstgroup->rsv_rfer = srcgroup->rsv_rfer; dstgroup->rsv_excl = srcgroup->rsv_excl; + update_limit = false;qgroup_dirty (fs_info, dstgroup);qgroup_dirty (fs_info, srcgroup);
@@ -3055,6 +3055,11 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, unlock:spin_unlock (&fs_info->qgroup_lock); +if (update_limit) { + err =btrfs_update_quoto_limit (trans, dstgroup, fs_info); +if (err) + ret = err; + }if (!ret) ret =btrfs_sysfs_add_one_qgroup (fs_info, dstgroup); out:
Signature · 1 line
2.31.1
Re: [PATCH v4 3/3] btrfs: qgroup: fix sleep from invalid context bug in update_qgroup_limit_item()
fs/btrfs/qgroup.c int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · line 2992
Quoted 8 lines · ChenXiaoSong
fs/btrfs/qgroup.c +1
-0@@ -2987,6 +2986,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->max_excl = srcgroup->max_excl; dstgroup->rsv_rfer = srcgroup->rsv_rfer; dstgroup->rsv_excl = srcgroup->rsv_excl; + update_limit = false;qgroup_dirty (fs_info, dstgroup);qgroup_dirty (fs_info, srcgroup);
No need to call update_qgroup_limit_item() when condition "if (srcid)" is true according to Qu Wenruo's suggestions, btrfs_run_qgroups() will update the quota tree to reflect the result after calling qgroup_dirty().
Re: [PATCH v4 3/3] btrfs: qgroup: fix sleep from invalid context bug in update_qgroup_limit_item()
In reply to ChenXiaoSongQuoted 40 lines · ChenXiaoSong
Syzkaller reported BUG as follows:
BUG: sleeping function called from invalid context at include/linux/sched/mm.h:274 Call Trace: <TASK> dump_stack_lvl+0xcd/0x134 __might_resched.cold+0x222/0x26b kmem_cache_alloc+0x2e7/0x3c0 update_qgroup_limit_item+0xe1/0x390 btrfs_qgroup_inherit+0x147b/0x1ee0 create_subvol+0x4eb/0x1710 btrfs_mksubvol+0xfe5/0x13f0 __btrfs_ioctl_snap_create+0x2b0/0x430 btrfs_ioctl_snap_create_v2+0x25a/0x520 btrfs_ioctl+0x2a1c/0x5ce0 __x64_sys_ioctl+0x193/0x200 do_syscall_64+0x35/0x80Fix this by delaying the limit item updates until unlock the spin lock.
Signed-off-by ChenXiaoSong <[email protected]>--- fs/btrfs/qgroup.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)fs/btrfs/qgroup.c +2
-0@@ -2867,6 +2867,8 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,bool need_rescan = false;u32 level_size =0 ;u64 nums; +bool update_limit = false; +int err;/* * There are only two callers of this function.
fs/btrfs/qgroup.c int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, · line 2962
Quoted 8 lines
fs/btrfs/qgroup.c +1
-4@@ -2957,10 +2959,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->max_excl = inherit->lim.max_excl; dstgroup->rsv_rfer = inherit->lim.rsv_rfer; dstgroup->rsv_excl = inherit->lim.rsv_excl; - - ret =btrfs_update_quoto_limit (trans, dstgroup, fs_info); -if (ret) - goto unlock; + update_limit = true;
Nope, just call qgroup_dirty() for @dstgroup.
Thanks,
Qu
Quoted 23 lines
fs/btrfs/qgroup.c +6
-0}if (srcid) {@@ -2987,6 +2986,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, dstgroup->max_excl = srcgroup->max_excl; dstgroup->rsv_rfer = srcgroup->rsv_rfer; dstgroup->rsv_excl = srcgroup->rsv_excl; + update_limit = false;qgroup_dirty (fs_info, dstgroup);qgroup_dirty (fs_info, srcgroup);@@ -3055,6 +3055,11 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid, unlock:spin_unlock (&fs_info->qgroup_lock); +if (update_limit) { + err =btrfs_update_quoto_limit (trans, dstgroup, fs_info); +if (err) + ret = err; + }if (!ret) ret =btrfs_sysfs_add_one_qgroup (fs_info, dstgroup); out: