blk-mq: Rework blk-mq timeout handling again
PATCH v8 1/1 blk-mq: Rework blk-mq timeout handling again
Recently the blk-mq timeout handling code was reworked. See also Tejun Heo, "[PATCHSET v4] blk-mq: reimplement timeout handling", 08 Jan 2018
(https://www.mail-archive.com/[email protected]/msg16985.html).
This patch reworks the blk-mq timeout handling code again. The timeout handling code is simplified by introducing a state machine per request. This change avoids that the blk-mq timeout handling code ignores completions that occur after blk_mq_check_expired() has been called and before blk_mq_rq_timed_out() has reset rq->aborted_gstate. If a block driver timeout handler always returns BLK_EH_RESET_TIMER then the result will be that the request never terminates.
Fix this race as follows:
- Replace the __deadline member of struct request by a new member called das that contains the generation number, state and deadline. Only 32 bits are used for the deadline field such that all three fields occupy only 64 bits. This change reduces the maximum supported request timeout value from (2**63/HZ) to (2**31/HZ).
- Remove all request member variables that became superfluous due to this change: gstate, gstate_seq and aborted_gstate_sync.
- Remove the request state information that became superfluous due to this patch, namely RQF_MQ_TIMEOUT_EXPIRED.
- Remove the code that became superfluous due to this change, namely the RCU lock and unlock statements in blk_mq_complete_request() and also the synchronize_rcu() call in the timeout handler.
Notes:
- A spinlock is used to protect atomic changes of rq->das on those architectures that do not provide a cmpxchg64() implementation.
- Atomic instructions are only used to update the request state if a concurrent request state change could be in progress.
- blk_add_timer() has been split into two functions - one for the legacy block layer and one for blk-mq.
---
Changes compared to v7:
- Fixed the generation number mechanism. Note: with this patch applied the behavior of the block layer does not depend on the generation number.
- Added more 32-bit architectures to the list of architectures on which cmpxchg64() should not be used.
Changes compared to v6:
- Used a union instead of bit manipulations to store multiple values into a single 64-bit field.
- Reduced the size of the timeout field from 64 to 32 bits.
- Made sure that the block layer still builds with this patch applied for the sh and mips architectures.
- Fixed two sparse warnings that were introduced by this patch in the WRITE_ONCE() calls.
Changes compared to v5:
- Restored the synchronize_rcu() call between marking a request for timeout handling and the actual timeout handling to avoid that timeout handling starts while .queue_rq() is still in progress if the timeout is very short.
- Only use cmpxchg() if another context could attempt to change the request state concurrently. Use WRITE_ONCE() otherwise.
Changes compared to v4:
- Addressed multiple review comments from Christoph. The most important are that atomic_long_cmpxchg() has been changed into cmpxchg() and also that there is now a nice and clean split between the legacy and blk-mq versions of blk_add_timer().
- Changed the patch name and modified the patch description because there is disagreement about whether or not the v4.16 blk-mq core can complete a single request twice. Kept the "Cc: stable" tag because of
https://bugzilla.kernel.org/show_bug.cgi?id=199077.
Changes compared to v3 (see also https://www.mail-archive.com/[email protected]/msg20073.html):
- Removed the spinlock again that was introduced to protect the request state.
v4 uses atomic_long_cmpxchg() instead.
- Split __deadline into two variables - one for the legacy block layer and one
for blk-mq.
Changes compared to v2 (https://www.mail-archive.com/[email protected]/msg18338.html):
- Rebased and retested on top of kernel v4.16.
Changes compared to v1 (https://www.mail-archive.com/[email protected]/msg18089.html):
- Removed the gstate and aborted_gstate members of struct request and used
the __deadline member to encode both the generation and state information.
block/blk-core.c | 6 -- block/blk-mq-debugfs.c | 1 - block/blk-mq.c | 178 +++++++++++++------------------------------------ block/blk-mq.h | 130 ++++++++++++++++++++++++++++-------- block/blk-timeout.c | 89 ++++++++++++++----------- block/blk.h | 21 +++--- include/linux/blkdev.h | 44 ++++++------ 7 files changed, 233 insertions(+), 236 deletions(-)
block/blk-core.c +0 -6
@@ -198,12 +198,6 @@ void blk_rq_init(struct request_queue *q, struct request *rq) rq->internal_tag =-1 ; rq->start_time_ns =ktime_get_ns (); rq->part =NULL ; -seqcount_init (&rq->gstate_seq); -u64_stats_init (&rq->aborted_gstate_sync); -/* -* See comment of blk_mq_init_request -*/ -WRITE_ONCE (rq->gstate,MQ_RQ_GEN_INC ); }EXPORT_SYMBOL (blk_rq_init);
block/blk-mq-debugfs.c +0 -1
@@ -344,7 +344,6 @@ static const char *const rqf_name[] = {RQF_NAME (STATS ),RQF_NAME (SPECIAL_PAYLOAD ),RQF_NAME (ZONE_WRITE_LOCKED ), -RQF_NAME (MQ_TIMEOUT_EXPIRED ), RQF_NAME(MQ_POLL_SLEPT), };#undef RQF_NAME
block/blk-mq.c +46 -132
281 lines not on this page · raw message
block/blk-mq.h +103 -27
164 lines not on this page · raw message
block/blk-timeout.c +51 -38
116 lines not on this page · raw message
block/blk.h +12 -9
@@ -170,6 +170,8 @@ static inline bool bio_integrity_endio(struct bio *bio)void blk_timeout_work (struct work_struct *work);unsigned long blk_rq_timeout (unsigned long timeout);void blk_add_timer (struct request *req); +void blk_mq_add_timer (struct request *req,enum mq_rq_state old, +enum mq_rq_state new);void blk_delete_timer (struct request *);
@@ -191,21 +193,21 @@ void blk_account_io_done(struct request *req, u64 now);/* * EH timer and IO completion will both attempt to 'grab' the request, make * sure that only one of them succeeds. Steal the bottom bit of the -* __deadline field for this. +* das field for this. */ static inline int blk_mark_rq_complete (struct request *rq) { -return test_and_set_bit (0 , &rq->__deadline); +return test_and_set_bit (0 , &rq->das.legacy_deadline); }static inline void blk_clear_rq_complete (struct request *rq) { -clear_bit (0 , &rq->__deadline); +clear_bit (0 , &rq->das.legacy_deadline); }static inline bool blk_rq_is_complete (struct request *rq) { -return test_bit (0 , &rq->__deadline); +return test_bit (0 , &rq->das.legacy_deadline); }/*
@@ -308,18 +310,19 @@ static inline void req_set_nomerge(struct request_queue *q, struct request *req) }/* -* Steal a bit from this field for legacy IO path atomic IO marking. Note that -* setting the deadline clears the bottom bit, potentially clearing the -* completed bit. The user has to be OK with this (current ones are fine). +* Steal two bits from this field. The legacy IO path uses the lowest bit for +* atomic IO marking. Note that setting the deadline clears the bottom bit, +* potentially clearing the completed bit. The current legacy block layer is +* fine with that. Must be called with the request queue lock held. */ static inline void blk_rq_set_deadline (struct request *rq,unsigned long time) { - rq->__deadline = time & ~0x1UL ; + rq->das.legacy_deadline = time & ~0x1UL ; }static inline unsigned long blk_rq_deadline (struct request *rq) { -return rq->__deadline & ~0x1UL ; +return rq->das.legacy_deadline & ~0x1UL ; }/*
include/linux/blkdev.h +21 -23
73 lines not on this page · raw message
Signature · 1 line
2.16.3
Jens Axboe
maintainerOn 5/11/18 3:08 PM, Bart Van Assche wrote:blk_mq_rq_update_aborted_gstate(rq, gstate);
block/blk-mq.c static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx, · line 800
Quoted 5 lines
block/blk-mq.c +5
-0+union blk_deadline_and_state das =READ_ONCE (rq->das); +unsigned long now = jiffies; +int32_t diff_jiffies = das.deadline - now; +int32_t diff_next = das.deadline - data->next; +enum mq_rq_state rq_state = das.state;
Why the int32_t types? Just use an int/unsigned int?
Quoted 6 lines
block/blk-mq.h +2
-0@@ -103,37 +97,119 @@ extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx);void blk_mq_release (struct request_queue *q); +#if defined(CONFIG_ARC) || \ + defined(CONFIG_ARM) && defined(CONFIG_CPU_THUMBONLY) || \
block/blk-mq.h extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx); · line 111
Quoted 10 lines
block/blk-mq.h +10
-0+defined (CONFIG_C6x) ||defined (CONFIG_H8300 ) || \ +defined (CONFIG_HEXAGON ) ||defined (CONFIG_MICROBLAZE ) || \ +defined (CONFIG_MIPS ) && !defined (CONFIG_64BIT ) || \ +defined (CONFIG_NDS32 ) ||defined (CONFIG_NIOS ) || \ +defined (CONFIG_OPENRISC ) ||defined (CONFIG_PPC32 ) || \ + defined(CONFIG_SUPERH) || defined(CONFIG_UML) || defined(CONFIG_UNICORE32) +#undef CONFIG_ARCH_HAS_CMPXCHG64 +#else +#define CONFIG_ARCH_HAS_CMPXCHG64 +#endif
I think the encapsulation of blk_mq_set_rq_state() in terms of lock vs cmpxchg64 is fine, but this does not belong in the block layer.
include/linux/blkdev.h struct request { · line 252
Quoted 2 lines
include/linux/blkdev.h +2
-0+/* Only used if CONFIG_ARCH_HAS_CMPXCHG64 is not defined. */ +spinlock_t das_lock;
Let's put it under an if defined() then.
Signature · 1 line
Jens Axboe
T24gRnJpLCAyMDE4LTA1LTExIGF0IDE1OjIxIC0wNjAwLCBKZW5zIEF4Ym9lIHdyb3RlOg0KPiBP biA1LzExLzE4ID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