Skip to content

Commit d578c77

Browse files
Ming Leiaxboe
authored andcommitted
block: avoid calling blkg_free() in atomic context
blkg_free() can currently be called in atomic context, either spin lock is held, or run in rcu callback. Meantime either request queue's release handler or ->pd_free_fn can sleep. Fix the issue by scheduling a work function for freeing blkcg_gq the instance. [ 148.553894] BUG: sleeping function called from invalid context at block/blk-sysfs.c:767 [ 148.557381] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 0, name: swapper/13 [ 148.560741] preempt_count: 101, expected: 0 [ 148.562577] RCU nest depth: 0, expected: 0 [ 148.564379] 1 lock held by swapper/13/0: [ 148.566127] #0: ffffffff82615f80 (rcu_callback){....}-{0:0}, at: rcu_lock_acquire+0x0/0x1b [ 148.569640] Preemption disabled at: [ 148.569642] [<ffffffff8123f9c3>] ___slab_alloc+0x554/0x661 [ 148.573559] CPU: 13 PID: 0 Comm: swapper/13 Kdump: loaded Not tainted 5.17.0_up+ #110 [ 148.576834] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.14.0-1.fc33 04/01/2014 [ 148.579768] Call Trace: [ 148.580567] <IRQ> [ 148.581262] dump_stack_lvl+0x56/0x7c [ 148.582367] ? ___slab_alloc+0x554/0x661 [ 148.583526] __might_resched+0x1af/0x1c8 [ 148.584678] blk_release_queue+0x24/0x109 [ 148.585861] kobject_cleanup+0xc9/0xfe [ 148.586979] blkg_free+0x46/0x63 [ 148.587962] rcu_do_batch+0x1c5/0x3db [ 148.589057] rcu_core+0x14a/0x184 [ 148.590065] __do_softirq+0x14d/0x2c7 [ 148.591167] __irq_exit_rcu+0x7a/0xd4 [ 148.592264] sysvec_apic_timer_interrupt+0x82/0xa5 [ 148.593649] </IRQ> [ 148.594354] <TASK> [ 148.595058] asm_sysvec_apic_timer_interrupt+0x12/0x20 Cc: Tejun Heo <tj@kernel.org> Fixes: 0a9a25c ("block: let blkcg_gq grab request queue's refcnt") Reported-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/linux-block/20220322093322.GA27283@lst.de/ Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://lore.kernel.org/r/20220323011308.2010380-1-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 863a66c commit d578c77

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

block/blk-cgroup.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,12 @@ static bool blkcg_policy_enabled(struct request_queue *q,
6565
return pol && test_bit(pol->plid, q->blkcg_pols);
6666
}
6767

68-
/**
69-
* blkg_free - free a blkg
70-
* @blkg: blkg to free
71-
*
72-
* Free @blkg which may be partially allocated.
73-
*/
74-
static void blkg_free(struct blkcg_gq *blkg)
68+
static void blkg_free_workfn(struct work_struct *work)
7569
{
70+
struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
71+
free_work);
7672
int i;
7773

78-
if (!blkg)
79-
return;
80-
8174
for (i = 0; i < BLKCG_MAX_POLS; i++)
8275
if (blkg->pd[i])
8376
blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
@@ -89,6 +82,25 @@ static void blkg_free(struct blkcg_gq *blkg)
8982
kfree(blkg);
9083
}
9184

85+
/**
86+
* blkg_free - free a blkg
87+
* @blkg: blkg to free
88+
*
89+
* Free @blkg which may be partially allocated.
90+
*/
91+
static void blkg_free(struct blkcg_gq *blkg)
92+
{
93+
if (!blkg)
94+
return;
95+
96+
/*
97+
* Both ->pd_free_fn() and request queue's release handler may
98+
* sleep, so free us by scheduling one work func
99+
*/
100+
INIT_WORK(&blkg->free_work, blkg_free_workfn);
101+
schedule_work(&blkg->free_work);
102+
}
103+
92104
static void __blkg_release(struct rcu_head *rcu)
93105
{
94106
struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);

include/linux/blk-cgroup.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ struct blkcg_gq {
9595

9696
spinlock_t async_bio_lock;
9797
struct bio_list async_bios;
98-
struct work_struct async_bio_work;
98+
union {
99+
struct work_struct async_bio_work;
100+
struct work_struct free_work;
101+
};
99102

100103
atomic_t use_delay;
101104
atomic64_t delay_nsec;

0 commit comments

Comments
 (0)