Skip to content

Commit 287922e

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: defer timeouts to a workqueue
Timer context is not very useful for drivers to perform any meaningful abort action from. So instead of calling the driver from this useless context defer it to a workqueue as soon as possible. Note that while a delayed_work item would seem the right thing here I didn't dare to use it due to the magic in blk_add_timer that pokes deep into timer internals. But maybe this encourages Tejun to add a sensible API for that to the workqueue API and we'll all be fine in the end :) Contains a major update from Keith Bush: "This patch removes synchronizing the timeout work so that the timer can start a freeze on its own queue. The timer enters the queue, so timer context can only start a freeze, but not wait for frozen." Signed-off-by: Christoph Hellwig <[email protected]> Acked-by: Keith Busch <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 8c0b391 commit 287922e

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

block/blk-core.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,13 @@ static void blk_queue_usage_counter_release(struct percpu_ref *ref)
664664
wake_up_all(&q->mq_freeze_wq);
665665
}
666666

667+
static void blk_rq_timed_out_timer(unsigned long data)
668+
{
669+
struct request_queue *q = (struct request_queue *)data;
670+
671+
kblockd_schedule_work(&q->timeout_work);
672+
}
673+
667674
struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
668675
{
669676
struct request_queue *q;
@@ -825,6 +832,7 @@ blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
825832
if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
826833
goto fail;
827834

835+
INIT_WORK(&q->timeout_work, blk_timeout_work);
828836
q->request_fn = rfn;
829837
q->prep_rq_fn = NULL;
830838
q->unprep_rq_fn = NULL;

block/blk-mq.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,15 +615,19 @@ static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
615615
}
616616
}
617617

618-
static void blk_mq_rq_timer(unsigned long priv)
618+
static void blk_mq_timeout_work(struct work_struct *work)
619619
{
620-
struct request_queue *q = (struct request_queue *)priv;
620+
struct request_queue *q =
621+
container_of(work, struct request_queue, timeout_work);
621622
struct blk_mq_timeout_data data = {
622623
.next = 0,
623624
.next_set = 0,
624625
};
625626
int i;
626627

628+
if (blk_queue_enter(q, true))
629+
return;
630+
627631
blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
628632

629633
if (data.next_set) {
@@ -638,6 +642,7 @@ static void blk_mq_rq_timer(unsigned long priv)
638642
blk_mq_tag_idle(hctx);
639643
}
640644
}
645+
blk_queue_exit(q);
641646
}
642647

643648
/*
@@ -2015,7 +2020,7 @@ struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
20152020
hctxs[i]->queue_num = i;
20162021
}
20172022

2018-
setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
2023+
INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
20192024
blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
20202025

20212026
q->nr_queues = nr_cpu_ids;

block/blk-timeout.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,16 @@ static void blk_rq_check_expired(struct request *rq, unsigned long *next_timeout
127127
}
128128
}
129129

130-
void blk_rq_timed_out_timer(unsigned long data)
130+
void blk_timeout_work(struct work_struct *work)
131131
{
132-
struct request_queue *q = (struct request_queue *) data;
132+
struct request_queue *q =
133+
container_of(work, struct request_queue, timeout_work);
133134
unsigned long flags, next = 0;
134135
struct request *rq, *tmp;
135136
int next_set = 0;
136137

138+
if (blk_queue_enter(q, true))
139+
return;
137140
spin_lock_irqsave(q->queue_lock, flags);
138141

139142
list_for_each_entry_safe(rq, tmp, &q->timeout_list, timeout_list)
@@ -143,6 +146,7 @@ void blk_rq_timed_out_timer(unsigned long data)
143146
mod_timer(&q->timeout, round_jiffies_up(next));
144147

145148
spin_unlock_irqrestore(q->queue_lock, flags);
149+
blk_queue_exit(q);
146150
}
147151

148152
/**

block/blk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static inline void blk_flush_integrity(void)
9393
}
9494
#endif
9595

96-
void blk_rq_timed_out_timer(unsigned long data);
96+
void blk_timeout_work(struct work_struct *work);
9797
unsigned long blk_rq_timeout(unsigned long timeout);
9898
void blk_add_timer(struct request *req);
9999
void blk_delete_timer(struct request *);

include/linux/blkdev.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ struct request_queue {
407407

408408
unsigned int rq_timeout;
409409
struct timer_list timeout;
410+
struct work_struct timeout_work;
410411
struct list_head timeout_list;
411412

412413
struct list_head icq_list;

0 commit comments

Comments
 (0)