2018-04-03 20:16:55 +03:00
/* SPDX-License-Identifier: GPL-2.0 */
2008-06-12 00:50:36 +04:00
/*
* Copyright ( C ) 2007 Oracle . All rights reserved .
2014-02-28 06:46:03 +04:00
* Copyright ( C ) 2014 Fujitsu . All rights reserved .
2008-06-12 00:50:36 +04:00
*/
2018-04-03 20:16:55 +03:00
# ifndef BTRFS_ASYNC_THREAD_H
# define BTRFS_ASYNC_THREAD_H
Btrfs: fix task hang under heavy compressed write
This has been reported and discussed for a long time, and this hang occurs in
both 3.15 and 3.16.
Btrfs now migrates to use kernel workqueue, but it introduces this hang problem.
Btrfs has a kind of work queued as an ordered way, which means that its
ordered_func() must be processed in the way of FIFO, so it usually looks like --
normal_work_helper(arg)
work = container_of(arg, struct btrfs_work, normal_work);
work->func() <---- (we name it work X)
for ordered_work in wq->ordered_list
ordered_work->ordered_func()
ordered_work->ordered_free()
The hang is a rare case, first when we find free space, we get an uncached block
group, then we go to read its free space cache inode for free space information,
so it will
file a readahead request
btrfs_readpages()
for page that is not in page cache
__do_readpage()
submit_extent_page()
btrfs_submit_bio_hook()
btrfs_bio_wq_end_io()
submit_bio()
end_workqueue_bio() <--(ret by the 1st endio)
queue a work(named work Y) for the 2nd
also the real endio()
So the hang occurs when work Y's work_struct and work X's work_struct happens
to share the same address.
A bit more explanation,
A,B,C -- struct btrfs_work
arg -- struct work_struct
kthread:
worker_thread()
pick up a work_struct from @worklist
process_one_work(arg)
worker->current_work = arg; <-- arg is A->normal_work
worker->current_func(arg)
normal_work_helper(arg)
A = container_of(arg, struct btrfs_work, normal_work);
A->func()
A->ordered_func()
A->ordered_free() <-- A gets freed
B->ordered_func()
submit_compressed_extents()
find_free_extent()
load_free_space_inode()
... <-- (the above readhead stack)
end_workqueue_bio()
btrfs_queue_work(work C)
B->ordered_free()
As if work A has a high priority in wq->ordered_list and there are more ordered
works queued after it, such as B->ordered_func(), its memory could have been
freed before normal_work_helper() returns, which means that kernel workqueue
code worker_thread() still has worker->current_work pointer to be work
A->normal_work's, ie. arg's address.
Meanwhile, work C is allocated after work A is freed, work C->normal_work
and work A->normal_work are likely to share the same address(I confirmed this
with ftrace output, so I'm not just guessing, it's rare though).
When another kthread picks up work C->normal_work to process, and finds our
kthread is processing it(see find_worker_executing_work()), it'll think
work C as a collision and skip then, which ends up nobody processing work C.
So the situation is that our kthread is waiting forever on work C.
Besides, there're other cases that can lead to deadlock, but the real problem
is that all btrfs workqueue shares one work->func, -- normal_work_helper,
so this makes each workqueue to have its own helper function, but only a
wraper pf normal_work_helper.
With this patch, I no long hit the above hang.
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
2014-08-15 19:36:53 +04:00
# include <linux/workqueue.h>
2008-06-12 00:50:36 +04:00
2016-06-09 23:22:11 +03:00
struct btrfs_fs_info ;
2014-02-28 06:46:19 +04:00
struct btrfs_workqueue ;
2014-02-28 06:46:04 +04:00
/* Internal use only */
2014-02-28 06:46:19 +04:00
struct __btrfs_workqueue ;
2014-03-06 08:19:50 +04:00
struct btrfs_work ;
typedef void ( * btrfs_func_t ) ( struct btrfs_work * arg ) ;
Btrfs: fix task hang under heavy compressed write
This has been reported and discussed for a long time, and this hang occurs in
both 3.15 and 3.16.
Btrfs now migrates to use kernel workqueue, but it introduces this hang problem.
Btrfs has a kind of work queued as an ordered way, which means that its
ordered_func() must be processed in the way of FIFO, so it usually looks like --
normal_work_helper(arg)
work = container_of(arg, struct btrfs_work, normal_work);
work->func() <---- (we name it work X)
for ordered_work in wq->ordered_list
ordered_work->ordered_func()
ordered_work->ordered_free()
The hang is a rare case, first when we find free space, we get an uncached block
group, then we go to read its free space cache inode for free space information,
so it will
file a readahead request
btrfs_readpages()
for page that is not in page cache
__do_readpage()
submit_extent_page()
btrfs_submit_bio_hook()
btrfs_bio_wq_end_io()
submit_bio()
end_workqueue_bio() <--(ret by the 1st endio)
queue a work(named work Y) for the 2nd
also the real endio()
So the hang occurs when work Y's work_struct and work X's work_struct happens
to share the same address.
A bit more explanation,
A,B,C -- struct btrfs_work
arg -- struct work_struct
kthread:
worker_thread()
pick up a work_struct from @worklist
process_one_work(arg)
worker->current_work = arg; <-- arg is A->normal_work
worker->current_func(arg)
normal_work_helper(arg)
A = container_of(arg, struct btrfs_work, normal_work);
A->func()
A->ordered_func()
A->ordered_free() <-- A gets freed
B->ordered_func()
submit_compressed_extents()
find_free_extent()
load_free_space_inode()
... <-- (the above readhead stack)
end_workqueue_bio()
btrfs_queue_work(work C)
B->ordered_free()
As if work A has a high priority in wq->ordered_list and there are more ordered
works queued after it, such as B->ordered_func(), its memory could have been
freed before normal_work_helper() returns, which means that kernel workqueue
code worker_thread() still has worker->current_work pointer to be work
A->normal_work's, ie. arg's address.
Meanwhile, work C is allocated after work A is freed, work C->normal_work
and work A->normal_work are likely to share the same address(I confirmed this
with ftrace output, so I'm not just guessing, it's rare though).
When another kthread picks up work C->normal_work to process, and finds our
kthread is processing it(see find_worker_executing_work()), it'll think
work C as a collision and skip then, which ends up nobody processing work C.
So the situation is that our kthread is waiting forever on work C.
Besides, there're other cases that can lead to deadlock, but the real problem
is that all btrfs workqueue shares one work->func, -- normal_work_helper,
so this makes each workqueue to have its own helper function, but only a
wraper pf normal_work_helper.
With this patch, I no long hit the above hang.
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
2014-08-15 19:36:53 +04:00
typedef void ( * btrfs_work_func_t ) ( struct work_struct * arg ) ;
2014-02-28 06:46:03 +04:00
2014-02-28 06:46:19 +04:00
struct btrfs_work {
2014-03-06 08:19:50 +04:00
btrfs_func_t func ;
btrfs_func_t ordered_func ;
btrfs_func_t ordered_free ;
2014-02-28 06:46:03 +04:00
/* Don't touch things below */
struct work_struct normal_work ;
struct list_head ordered_list ;
2014-02-28 06:46:19 +04:00
struct __btrfs_workqueue * wq ;
2014-02-28 06:46:03 +04:00
unsigned long flags ;
} ;
2016-06-09 23:22:11 +03:00
struct btrfs_workqueue * btrfs_alloc_workqueue ( struct btrfs_fs_info * fs_info ,
const char * name ,
2015-02-16 20:34:01 +03:00
unsigned int flags ,
2015-08-20 04:30:39 +03:00
int limit_active ,
2014-03-06 08:19:50 +04:00
int thresh ) ;
2019-09-16 21:30:57 +03:00
void btrfs_init_work ( struct btrfs_work * work , btrfs_func_t func ,
btrfs_func_t ordered_func , btrfs_func_t ordered_free ) ;
2014-02-28 06:46:19 +04:00
void btrfs_queue_work ( struct btrfs_workqueue * wq ,
struct btrfs_work * work ) ;
void btrfs_destroy_workqueue ( struct btrfs_workqueue * wq ) ;
void btrfs_workqueue_set_max ( struct btrfs_workqueue * wq , int max ) ;
void btrfs_set_work_high_priority ( struct btrfs_work * work ) ;
2019-10-01 20:57:39 +03:00
struct btrfs_fs_info * __pure btrfs_work_owner ( const struct btrfs_work * work ) ;
struct btrfs_fs_info * __pure btrfs_workqueue_owner ( const struct __btrfs_workqueue * wq ) ;
2017-06-29 06:56:54 +03:00
bool btrfs_workqueue_normal_congested ( const struct btrfs_workqueue * wq ) ;
2018-04-03 20:16:55 +03:00
2008-06-12 00:50:36 +04:00
# endif