2019-05-01 02:42:39 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2008-01-29 21:53:40 +08:00
|
|
|
/*
|
2017-08-25 02:09:25 +08:00
|
|
|
* Functions to sequence PREFLUSH and FUA writes.
|
2011-01-25 19:43:54 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Max Planck Institute for Gravitational Physics
|
|
|
|
* Copyright (C) 2011 Tejun Heo <tj@kernel.org>
|
|
|
|
*
|
2017-08-25 02:09:25 +08:00
|
|
|
* REQ_{PREFLUSH|FUA} requests are decomposed to sequences consisted of three
|
2011-01-25 19:43:54 +08:00
|
|
|
* optional steps - PREFLUSH, DATA and POSTFLUSH - according to the request
|
|
|
|
* properties and hardware capability.
|
|
|
|
*
|
2016-06-06 03:32:25 +08:00
|
|
|
* If a request doesn't have data, only REQ_PREFLUSH makes sense, which
|
|
|
|
* indicates a simple flush request. If there is data, REQ_PREFLUSH indicates
|
2011-01-25 19:43:54 +08:00
|
|
|
* that the device cache should be flushed before the data is executed, and
|
|
|
|
* REQ_FUA means that the data must be on non-volatile media on request
|
|
|
|
* completion.
|
|
|
|
*
|
2017-08-25 02:09:25 +08:00
|
|
|
* If the device doesn't have writeback cache, PREFLUSH and FUA don't make any
|
|
|
|
* difference. The requests are either completed immediately if there's no data
|
|
|
|
* or executed as normal requests otherwise.
|
2011-01-25 19:43:54 +08:00
|
|
|
*
|
2016-06-06 03:32:25 +08:00
|
|
|
* If the device has writeback cache and supports FUA, REQ_PREFLUSH is
|
2011-01-25 19:43:54 +08:00
|
|
|
* translated to PREFLUSH but REQ_FUA is passed down directly with DATA.
|
|
|
|
*
|
2016-06-06 03:32:25 +08:00
|
|
|
* If the device has writeback cache and doesn't support FUA, REQ_PREFLUSH
|
|
|
|
* is translated to PREFLUSH and REQ_FUA to POSTFLUSH.
|
2011-01-25 19:43:54 +08:00
|
|
|
*
|
|
|
|
* The actual execution of flush is double buffered. Whenever a request
|
|
|
|
* needs to execute PRE or POSTFLUSH, it queues at
|
2014-09-25 23:23:43 +08:00
|
|
|
* fq->flush_queue[fq->flush_pending_idx]. Once certain criteria are met, a
|
2016-06-06 03:32:23 +08:00
|
|
|
* REQ_OP_FLUSH is issued and the pending_idx is toggled. When the flush
|
2011-01-25 19:43:54 +08:00
|
|
|
* completes, all the requests which were pending are proceeded to the next
|
2017-08-25 02:09:25 +08:00
|
|
|
* step. This allows arbitrary merging of different types of PREFLUSH/FUA
|
2011-01-25 19:43:54 +08:00
|
|
|
* requests.
|
|
|
|
*
|
|
|
|
* Currently, the following conditions are used to determine when to issue
|
|
|
|
* flush.
|
|
|
|
*
|
|
|
|
* C1. At any given time, only one flush shall be in progress. This makes
|
|
|
|
* double buffering sufficient.
|
|
|
|
*
|
|
|
|
* C2. Flush is deferred if any request is executing DATA of its sequence.
|
|
|
|
* This avoids issuing separate POSTFLUSHes for requests which shared
|
|
|
|
* PREFLUSH.
|
|
|
|
*
|
|
|
|
* C3. The second condition is ignored if there is a request which has
|
|
|
|
* waited longer than FLUSH_PENDING_TIMEOUT. This is to avoid
|
|
|
|
* starvation in the unlikely case where there are continuous stream of
|
2017-08-25 02:09:25 +08:00
|
|
|
* FUA (without PREFLUSH) requests.
|
2011-01-25 19:43:54 +08:00
|
|
|
*
|
|
|
|
* For devices which support FUA, it isn't clear whether C2 (and thus C3)
|
|
|
|
* is beneficial.
|
|
|
|
*
|
2017-08-25 02:09:25 +08:00
|
|
|
* Note that a sequenced PREFLUSH/FUA request with DATA is completed twice.
|
2011-01-25 19:43:54 +08:00
|
|
|
* Once while executing DATA and again after the whole sequence is
|
|
|
|
* complete. The first completion updates the contained bio but doesn't
|
|
|
|
* finish it so that the bio submitter is notified only after the whole
|
2016-10-20 21:12:13 +08:00
|
|
|
* sequence is complete. This is implemented by testing RQF_FLUSH_SEQ in
|
2011-01-25 19:43:54 +08:00
|
|
|
* req_bio_endio().
|
|
|
|
*
|
2017-08-25 02:09:25 +08:00
|
|
|
* The above peculiarity requires that each PREFLUSH/FUA request has only one
|
2011-01-25 19:43:54 +08:00
|
|
|
* bio attached to it, which is guaranteed as they aren't allowed to be
|
|
|
|
* merged in the usual way.
|
2008-01-29 21:53:40 +08:00
|
|
|
*/
|
2011-01-25 19:43:54 +08:00
|
|
|
|
2008-01-29 21:53:40 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/blkdev.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/gfp.h>
|
2021-11-24 02:53:12 +08:00
|
|
|
#include <linux/part_stat.h>
|
2008-01-29 21:53:40 +08:00
|
|
|
|
|
|
|
#include "blk.h"
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
#include "blk-mq.h"
|
2017-01-17 21:03:22 +08:00
|
|
|
#include "blk-mq-sched.h"
|
2008-01-29 21:53:40 +08:00
|
|
|
|
2017-08-25 02:09:25 +08:00
|
|
|
/* PREFLUSH/FUA sequences */
|
2010-09-03 17:56:17 +08:00
|
|
|
enum {
|
2011-01-25 19:43:54 +08:00
|
|
|
REQ_FSEQ_PREFLUSH = (1 << 0), /* pre-flushing in progress */
|
|
|
|
REQ_FSEQ_DATA = (1 << 1), /* data write in progress */
|
|
|
|
REQ_FSEQ_POSTFLUSH = (1 << 2), /* post-flushing in progress */
|
|
|
|
REQ_FSEQ_DONE = (1 << 3),
|
|
|
|
|
|
|
|
REQ_FSEQ_ACTIONS = REQ_FSEQ_PREFLUSH | REQ_FSEQ_DATA |
|
|
|
|
REQ_FSEQ_POSTFLUSH,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If flush has been pending longer than the following timeout,
|
|
|
|
* it's issued even if flush_data requests are still in flight.
|
|
|
|
*/
|
|
|
|
FLUSH_PENDING_TIMEOUT = 5 * HZ,
|
2010-09-03 17:56:17 +08:00
|
|
|
};
|
|
|
|
|
2018-11-01 02:43:24 +08:00
|
|
|
static void blk_kick_flush(struct request_queue *q,
|
2022-07-15 02:06:32 +08:00
|
|
|
struct blk_flush_queue *fq, blk_opf_t flags);
|
2010-09-03 17:56:16 +08:00
|
|
|
|
2021-11-24 02:53:05 +08:00
|
|
|
static inline struct blk_flush_queue *
|
|
|
|
blk_get_flush_queue(struct request_queue *q, struct blk_mq_ctx *ctx)
|
|
|
|
{
|
|
|
|
return blk_mq_map_queue(q, REQ_OP_FLUSH, ctx)->fq;
|
|
|
|
}
|
|
|
|
|
2011-01-25 19:43:54 +08:00
|
|
|
static unsigned int blk_flush_cur_seq(struct request *rq)
|
2010-09-03 17:56:17 +08:00
|
|
|
{
|
2011-01-25 19:43:54 +08:00
|
|
|
return 1 << ffz(rq->flush.seq);
|
|
|
|
}
|
2010-09-03 17:56:17 +08:00
|
|
|
|
2011-01-25 19:43:54 +08:00
|
|
|
static void blk_flush_restore_request(struct request *rq)
|
|
|
|
{
|
2010-09-03 17:56:17 +08:00
|
|
|
/*
|
2011-01-25 19:43:54 +08:00
|
|
|
* After flush data completion, @rq->bio is %NULL but we need to
|
|
|
|
* complete the bio again. @rq->biotail is guaranteed to equal the
|
|
|
|
* original @rq->bio. Restore it.
|
2010-09-03 17:56:17 +08:00
|
|
|
*/
|
2011-01-25 19:43:54 +08:00
|
|
|
rq->bio = rq->biotail;
|
2024-05-01 19:09:01 +08:00
|
|
|
if (rq->bio)
|
|
|
|
rq->__sector = rq->bio->bi_iter.bi_sector;
|
2011-01-25 19:43:54 +08:00
|
|
|
|
|
|
|
/* make @rq a normal request */
|
2016-10-20 21:12:13 +08:00
|
|
|
rq->rq_flags &= ~RQF_FLUSH_SEQ;
|
block: fix flush machinery for stacking drivers with differring flush flags
Commit ae1b1539622fb46e51b4d13b3f9e5f4c713f86ae, block: reimplement
FLUSH/FUA to support merge, introduced a performance regression when
running any sort of fsyncing workload using dm-multipath and certain
storage (in our case, an HP EVA). The test I ran was fs_mark, and it
dropped from ~800 files/sec on ext4 to ~100 files/sec. It turns out
that dm-multipath always advertised flush+fua support, and passed
commands on down the stack, where those flags used to get stripped off.
The above commit changed that behavior:
static inline struct request *__elv_next_request(struct request_queue *q)
{
struct request *rq;
while (1) {
- while (!list_empty(&q->queue_head)) {
+ if (!list_empty(&q->queue_head)) {
rq = list_entry_rq(q->queue_head.next);
- if (!(rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) ||
- (rq->cmd_flags & REQ_FLUSH_SEQ))
- return rq;
- rq = blk_do_flush(q, rq);
- if (rq)
- return rq;
+ return rq;
}
Note that previously, a command would come in here, have
REQ_FLUSH|REQ_FUA set, and then get handed off to blk_do_flush:
struct request *blk_do_flush(struct request_queue *q, struct request *rq)
{
unsigned int fflags = q->flush_flags; /* may change, cache it */
bool has_flush = fflags & REQ_FLUSH, has_fua = fflags & REQ_FUA;
bool do_preflush = has_flush && (rq->cmd_flags & REQ_FLUSH);
bool do_postflush = has_flush && !has_fua && (rq->cmd_flags &
REQ_FUA);
unsigned skip = 0;
...
if (blk_rq_sectors(rq) && !do_preflush && !do_postflush) {
rq->cmd_flags &= ~REQ_FLUSH;
if (!has_fua)
rq->cmd_flags &= ~REQ_FUA;
return rq;
}
So, the flush machinery was bypassed in such cases (q->flush_flags == 0
&& rq->cmd_flags & (REQ_FLUSH|REQ_FUA)).
Now, however, we don't get into the flush machinery at all. Instead,
__elv_next_request just hands a request with flush and fua bits set to
the scsi_request_fn, even if the underlying request_queue does not
support flush or fua.
The agreed upon approach is to fix the flush machinery to allow
stacking. While this isn't used in practice (since there is only one
request-based dm target, and that target will now reflect the flush
flags of the underlying device), it does future-proof the solution, and
make it function as designed.
In order to make this work, I had to add a field to the struct request,
inside the flush structure (to store the original req->end_io). Shaohua
had suggested overloading the union with rb_node and completion_data,
but the completion data is used by device mapper and can also be used by
other drivers. So, I didn't see a way around the additional field.
I tested this patch on an HP EVA with both ext4 and xfs, and it recovers
the lost performance. Comments and other testers, as always, are
appreciated.
Cheers,
Jeff
Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
2011-08-16 03:37:25 +08:00
|
|
|
rq->end_io = rq->flush.saved_end_io;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:26 +08:00
|
|
|
static void blk_account_io_flush(struct request *rq)
|
|
|
|
{
|
2021-11-26 20:18:00 +08:00
|
|
|
struct block_device *part = rq->q->disk->part0;
|
2019-11-21 18:40:26 +08:00
|
|
|
|
|
|
|
part_stat_lock();
|
|
|
|
part_stat_inc(part, ios[STAT_FLUSH]);
|
|
|
|
part_stat_add(part, nsecs[STAT_FLUSH],
|
2024-01-16 05:45:07 +08:00
|
|
|
blk_time_get_ns() - rq->start_time_ns);
|
2019-11-21 18:40:26 +08:00
|
|
|
part_stat_unlock();
|
|
|
|
}
|
|
|
|
|
2011-01-25 19:43:54 +08:00
|
|
|
/**
|
|
|
|
* blk_flush_complete_seq - complete flush sequence
|
2017-08-25 02:09:25 +08:00
|
|
|
* @rq: PREFLUSH/FUA request being sequenced
|
2014-09-25 23:23:45 +08:00
|
|
|
* @fq: flush queue
|
2011-01-25 19:43:54 +08:00
|
|
|
* @seq: sequences to complete (mask of %REQ_FSEQ_*, can be zero)
|
|
|
|
* @error: whether an error occurred
|
|
|
|
*
|
|
|
|
* @rq just completed @seq part of its flush sequence, record the
|
|
|
|
* completion and trigger the next step.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
2018-11-15 00:02:10 +08:00
|
|
|
* spin_lock_irq(fq->mq_flush_lock)
|
2011-01-25 19:43:54 +08:00
|
|
|
*/
|
2018-11-01 02:43:24 +08:00
|
|
|
static void blk_flush_complete_seq(struct request *rq,
|
2014-09-25 23:23:45 +08:00
|
|
|
struct blk_flush_queue *fq,
|
2017-06-03 15:38:04 +08:00
|
|
|
unsigned int seq, blk_status_t error)
|
2008-01-29 21:53:40 +08:00
|
|
|
{
|
2011-01-25 19:43:54 +08:00
|
|
|
struct request_queue *q = rq->q;
|
2014-09-25 23:23:43 +08:00
|
|
|
struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
|
2022-07-15 02:06:32 +08:00
|
|
|
blk_opf_t cmd_flags;
|
2011-01-25 19:43:54 +08:00
|
|
|
|
|
|
|
BUG_ON(rq->flush.seq & seq);
|
|
|
|
rq->flush.seq |= seq;
|
2018-06-09 20:37:14 +08:00
|
|
|
cmd_flags = rq->cmd_flags;
|
2011-01-25 19:43:54 +08:00
|
|
|
|
|
|
|
if (likely(!error))
|
|
|
|
seq = blk_flush_cur_seq(rq);
|
|
|
|
else
|
|
|
|
seq = REQ_FSEQ_DONE;
|
|
|
|
|
|
|
|
switch (seq) {
|
|
|
|
case REQ_FSEQ_PREFLUSH:
|
|
|
|
case REQ_FSEQ_POSTFLUSH:
|
|
|
|
/* queue for flush */
|
|
|
|
if (list_empty(pending))
|
2014-09-25 23:23:43 +08:00
|
|
|
fq->flush_pending_since = jiffies;
|
2024-06-08 22:31:15 +08:00
|
|
|
list_add_tail(&rq->queuelist, pending);
|
2011-01-25 19:43:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REQ_FSEQ_DATA:
|
2023-07-17 12:00:57 +08:00
|
|
|
fq->flush_data_in_flight++;
|
2023-05-19 12:40:50 +08:00
|
|
|
spin_lock(&q->requeue_lock);
|
2023-07-17 12:00:58 +08:00
|
|
|
list_move(&rq->queuelist, &q->requeue_list);
|
2023-05-19 12:40:50 +08:00
|
|
|
spin_unlock(&q->requeue_lock);
|
2023-04-13 14:40:53 +08:00
|
|
|
blk_mq_kick_requeue_list(q);
|
2011-01-25 19:43:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case REQ_FSEQ_DONE:
|
|
|
|
/*
|
2019-11-21 18:40:26 +08:00
|
|
|
* @rq was previously adjusted by blk_insert_flush() for
|
2011-01-25 19:43:54 +08:00
|
|
|
* flush sequencing and may already have gone through the
|
|
|
|
* flush data request completion path. Restore @rq for
|
|
|
|
* normal completion and end it.
|
|
|
|
*/
|
2023-07-17 12:00:58 +08:00
|
|
|
list_del_init(&rq->queuelist);
|
2011-01-25 19:43:54 +08:00
|
|
|
blk_flush_restore_request(rq);
|
2018-10-14 05:44:12 +08:00
|
|
|
blk_mq_end_request(rq, error);
|
2011-01-25 19:43:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2018-11-01 02:43:24 +08:00
|
|
|
blk_kick_flush(q, fq, cmd_flags);
|
2008-01-29 21:53:40 +08:00
|
|
|
}
|
|
|
|
|
2022-09-22 05:19:54 +08:00
|
|
|
static enum rq_end_io_ret flush_end_io(struct request *flush_rq,
|
|
|
|
blk_status_t error)
|
2008-01-29 21:53:40 +08:00
|
|
|
{
|
2011-01-25 19:43:54 +08:00
|
|
|
struct request_queue *q = flush_rq->q;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
struct list_head *running;
|
2011-01-25 19:43:54 +08:00
|
|
|
struct request *rq, *n;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
unsigned long flags = 0;
|
2014-09-25 23:23:46 +08:00
|
|
|
struct blk_flush_queue *fq = blk_get_flush_queue(q, flush_rq->mq_ctx);
|
2011-01-25 19:43:54 +08:00
|
|
|
|
2018-10-14 05:44:12 +08:00
|
|
|
/* release the tag's ownership to the req cloned from */
|
|
|
|
spin_lock_irqsave(&fq->mq_flush_lock, flags);
|
block: fix null pointer dereference in blk_mq_rq_timed_out()
We got a null pointer deference BUG_ON in blk_mq_rq_timed_out()
as following:
[ 108.825472] BUG: kernel NULL pointer dereference, address: 0000000000000040
[ 108.827059] PGD 0 P4D 0
[ 108.827313] Oops: 0000 [#1] SMP PTI
[ 108.827657] CPU: 6 PID: 198 Comm: kworker/6:1H Not tainted 5.3.0-rc8+ #431
[ 108.829503] Workqueue: kblockd blk_mq_timeout_work
[ 108.829913] RIP: 0010:blk_mq_check_expired+0x258/0x330
[ 108.838191] Call Trace:
[ 108.838406] bt_iter+0x74/0x80
[ 108.838665] blk_mq_queue_tag_busy_iter+0x204/0x450
[ 108.839074] ? __switch_to_asm+0x34/0x70
[ 108.839405] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.839823] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.840273] ? syscall_return_via_sysret+0xf/0x7f
[ 108.840732] blk_mq_timeout_work+0x74/0x200
[ 108.841151] process_one_work+0x297/0x680
[ 108.841550] worker_thread+0x29c/0x6f0
[ 108.841926] ? rescuer_thread+0x580/0x580
[ 108.842344] kthread+0x16a/0x1a0
[ 108.842666] ? kthread_flush_work+0x170/0x170
[ 108.843100] ret_from_fork+0x35/0x40
The bug is caused by the race between timeout handle and completion for
flush request.
When timeout handle function blk_mq_rq_timed_out() try to read
'req->q->mq_ops', the 'req' have completed and reinitiated by next
flush request, which would call blk_rq_init() to clear 'req' as 0.
After commit 12f5b93145 ("blk-mq: Remove generation seqeunce"),
normal requests lifetime are protected by refcount. Until 'rq->ref'
drop to zero, the request can really be free. Thus, these requests
cannot been reused before timeout handle finish.
However, flush request has defined .end_io and rq->end_io() is still
called even if 'rq->ref' doesn't drop to zero. After that, the 'flush_rq'
can be reused by the next flush request handle, resulting in null
pointer deference BUG ON.
We fix this problem by covering flush request with 'rq->ref'.
If the refcount is not zero, flush_end_io() return and wait the
last holder recall it. To record the request status, we add a new
entry 'rq_status', which will be used in flush_end_io().
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Keith Busch <keith.busch@intel.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: stable@vger.kernel.org # v4.18+
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Bob Liu <bob.liu@oracle.com>
Signed-off-by: Yufen Yu <yuyufen@huawei.com>
-------
v2:
- move rq_status from struct request to struct blk_flush_queue
v3:
- remove unnecessary '{}' pair.
v4:
- let spinlock to protect 'fq->rq_status'
v5:
- move rq_status after flush_running_idx member of struct blk_flush_queue
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-09-27 16:19:55 +08:00
|
|
|
|
2021-10-15 04:39:59 +08:00
|
|
|
if (!req_ref_put_and_test(flush_rq)) {
|
block: fix null pointer dereference in blk_mq_rq_timed_out()
We got a null pointer deference BUG_ON in blk_mq_rq_timed_out()
as following:
[ 108.825472] BUG: kernel NULL pointer dereference, address: 0000000000000040
[ 108.827059] PGD 0 P4D 0
[ 108.827313] Oops: 0000 [#1] SMP PTI
[ 108.827657] CPU: 6 PID: 198 Comm: kworker/6:1H Not tainted 5.3.0-rc8+ #431
[ 108.829503] Workqueue: kblockd blk_mq_timeout_work
[ 108.829913] RIP: 0010:blk_mq_check_expired+0x258/0x330
[ 108.838191] Call Trace:
[ 108.838406] bt_iter+0x74/0x80
[ 108.838665] blk_mq_queue_tag_busy_iter+0x204/0x450
[ 108.839074] ? __switch_to_asm+0x34/0x70
[ 108.839405] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.839823] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.840273] ? syscall_return_via_sysret+0xf/0x7f
[ 108.840732] blk_mq_timeout_work+0x74/0x200
[ 108.841151] process_one_work+0x297/0x680
[ 108.841550] worker_thread+0x29c/0x6f0
[ 108.841926] ? rescuer_thread+0x580/0x580
[ 108.842344] kthread+0x16a/0x1a0
[ 108.842666] ? kthread_flush_work+0x170/0x170
[ 108.843100] ret_from_fork+0x35/0x40
The bug is caused by the race between timeout handle and completion for
flush request.
When timeout handle function blk_mq_rq_timed_out() try to read
'req->q->mq_ops', the 'req' have completed and reinitiated by next
flush request, which would call blk_rq_init() to clear 'req' as 0.
After commit 12f5b93145 ("blk-mq: Remove generation seqeunce"),
normal requests lifetime are protected by refcount. Until 'rq->ref'
drop to zero, the request can really be free. Thus, these requests
cannot been reused before timeout handle finish.
However, flush request has defined .end_io and rq->end_io() is still
called even if 'rq->ref' doesn't drop to zero. After that, the 'flush_rq'
can be reused by the next flush request handle, resulting in null
pointer deference BUG ON.
We fix this problem by covering flush request with 'rq->ref'.
If the refcount is not zero, flush_end_io() return and wait the
last holder recall it. To record the request status, we add a new
entry 'rq_status', which will be used in flush_end_io().
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Keith Busch <keith.busch@intel.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: stable@vger.kernel.org # v4.18+
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Bob Liu <bob.liu@oracle.com>
Signed-off-by: Yufen Yu <yuyufen@huawei.com>
-------
v2:
- move rq_status from struct request to struct blk_flush_queue
v3:
- remove unnecessary '{}' pair.
v4:
- let spinlock to protect 'fq->rq_status'
v5:
- move rq_status after flush_running_idx member of struct blk_flush_queue
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-09-27 16:19:55 +08:00
|
|
|
fq->rq_status = error;
|
|
|
|
spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
|
2022-09-22 05:19:54 +08:00
|
|
|
return RQ_END_IO_NONE;
|
block: fix null pointer dereference in blk_mq_rq_timed_out()
We got a null pointer deference BUG_ON in blk_mq_rq_timed_out()
as following:
[ 108.825472] BUG: kernel NULL pointer dereference, address: 0000000000000040
[ 108.827059] PGD 0 P4D 0
[ 108.827313] Oops: 0000 [#1] SMP PTI
[ 108.827657] CPU: 6 PID: 198 Comm: kworker/6:1H Not tainted 5.3.0-rc8+ #431
[ 108.829503] Workqueue: kblockd blk_mq_timeout_work
[ 108.829913] RIP: 0010:blk_mq_check_expired+0x258/0x330
[ 108.838191] Call Trace:
[ 108.838406] bt_iter+0x74/0x80
[ 108.838665] blk_mq_queue_tag_busy_iter+0x204/0x450
[ 108.839074] ? __switch_to_asm+0x34/0x70
[ 108.839405] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.839823] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.840273] ? syscall_return_via_sysret+0xf/0x7f
[ 108.840732] blk_mq_timeout_work+0x74/0x200
[ 108.841151] process_one_work+0x297/0x680
[ 108.841550] worker_thread+0x29c/0x6f0
[ 108.841926] ? rescuer_thread+0x580/0x580
[ 108.842344] kthread+0x16a/0x1a0
[ 108.842666] ? kthread_flush_work+0x170/0x170
[ 108.843100] ret_from_fork+0x35/0x40
The bug is caused by the race between timeout handle and completion for
flush request.
When timeout handle function blk_mq_rq_timed_out() try to read
'req->q->mq_ops', the 'req' have completed and reinitiated by next
flush request, which would call blk_rq_init() to clear 'req' as 0.
After commit 12f5b93145 ("blk-mq: Remove generation seqeunce"),
normal requests lifetime are protected by refcount. Until 'rq->ref'
drop to zero, the request can really be free. Thus, these requests
cannot been reused before timeout handle finish.
However, flush request has defined .end_io and rq->end_io() is still
called even if 'rq->ref' doesn't drop to zero. After that, the 'flush_rq'
can be reused by the next flush request handle, resulting in null
pointer deference BUG ON.
We fix this problem by covering flush request with 'rq->ref'.
If the refcount is not zero, flush_end_io() return and wait the
last holder recall it. To record the request status, we add a new
entry 'rq_status', which will be used in flush_end_io().
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Keith Busch <keith.busch@intel.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: stable@vger.kernel.org # v4.18+
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Bob Liu <bob.liu@oracle.com>
Signed-off-by: Yufen Yu <yuyufen@huawei.com>
-------
v2:
- move rq_status from struct request to struct blk_flush_queue
v3:
- remove unnecessary '{}' pair.
v4:
- let spinlock to protect 'fq->rq_status'
v5:
- move rq_status after flush_running_idx member of struct blk_flush_queue
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-09-27 16:19:55 +08:00
|
|
|
}
|
|
|
|
|
2021-05-11 23:22:33 +08:00
|
|
|
blk_account_io_flush(flush_rq);
|
2020-11-13 21:44:48 +08:00
|
|
|
/*
|
|
|
|
* Flush request has to be marked as IDLE when it is really ended
|
|
|
|
* because its .end_io() is called from timeout code path too for
|
|
|
|
* avoiding use-after-free.
|
|
|
|
*/
|
|
|
|
WRITE_ONCE(flush_rq->state, MQ_RQ_IDLE);
|
block: Fix fsync always failed if once failed
We do test with inject error fault base on v4.19, after test some time we found
sync /dev/sda always failed.
[root@localhost] sync /dev/sda
sync: error syncing '/dev/sda': Input/output error
scsi log as follows:
[19069.812296] sd 0:0:0:0: [sda] tag#64 Send: scmd 0x00000000d03a0b6b
[19069.812302] sd 0:0:0:0: [sda] tag#64 CDB: Synchronize Cache(10) 35 00 00 00 00 00 00 00 00 00
[19069.812533] sd 0:0:0:0: [sda] tag#64 Done: SUCCESS Result: hostbyte=DID_OK driverbyte=DRIVER_OK
[19069.812536] sd 0:0:0:0: [sda] tag#64 CDB: Synchronize Cache(10) 35 00 00 00 00 00 00 00 00 00
[19069.812539] sd 0:0:0:0: [sda] tag#64 scsi host busy 1 failed 0
[19069.812542] sd 0:0:0:0: Notifying upper driver of completion (result 0)
[19069.812546] sd 0:0:0:0: [sda] tag#64 sd_done: completed 0 of 0 bytes
[19069.812549] sd 0:0:0:0: [sda] tag#64 0 sectors total, 0 bytes done.
[19069.812564] print_req_error: I/O error, dev sda, sector 0
ftrace log as follows:
rep-306069 [007] .... 19654.923315: block_bio_queue: 8,0 FWS 0 + 0 [rep]
rep-306069 [007] .... 19654.923333: block_getrq: 8,0 FWS 0 + 0 [rep]
kworker/7:1H-250 [007] .... 19654.923352: block_rq_issue: 8,0 FF 0 () 0 + 0 [kworker/7:1H]
<idle>-0 [007] ..s. 19654.923562: block_rq_complete: 8,0 FF () 18446744073709551615 + 0 [0]
<idle>-0 [007] d.s. 19654.923576: block_rq_complete: 8,0 WS () 0 + 0 [-5]
As 8d6996630c03 introduce 'fq->rq_status', this data only update when 'flush_rq'
reference count isn't zero. If flush request once failed and record error code
in 'fq->rq_status'. If there is no chance to update 'fq->rq_status',then do fsync
will always failed.
To address this issue reset 'fq->rq_status' after return error code to upper layer.
Fixes: 8d6996630c03("block: fix null pointer dereference in blk_mq_rq_timed_out()")
Signed-off-by: Ye Bin <yebin10@huawei.com>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20211129012659.1553733-1-yebin10@huawei.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2021-11-29 09:26:59 +08:00
|
|
|
if (fq->rq_status != BLK_STS_OK) {
|
block: fix null pointer dereference in blk_mq_rq_timed_out()
We got a null pointer deference BUG_ON in blk_mq_rq_timed_out()
as following:
[ 108.825472] BUG: kernel NULL pointer dereference, address: 0000000000000040
[ 108.827059] PGD 0 P4D 0
[ 108.827313] Oops: 0000 [#1] SMP PTI
[ 108.827657] CPU: 6 PID: 198 Comm: kworker/6:1H Not tainted 5.3.0-rc8+ #431
[ 108.829503] Workqueue: kblockd blk_mq_timeout_work
[ 108.829913] RIP: 0010:blk_mq_check_expired+0x258/0x330
[ 108.838191] Call Trace:
[ 108.838406] bt_iter+0x74/0x80
[ 108.838665] blk_mq_queue_tag_busy_iter+0x204/0x450
[ 108.839074] ? __switch_to_asm+0x34/0x70
[ 108.839405] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.839823] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.840273] ? syscall_return_via_sysret+0xf/0x7f
[ 108.840732] blk_mq_timeout_work+0x74/0x200
[ 108.841151] process_one_work+0x297/0x680
[ 108.841550] worker_thread+0x29c/0x6f0
[ 108.841926] ? rescuer_thread+0x580/0x580
[ 108.842344] kthread+0x16a/0x1a0
[ 108.842666] ? kthread_flush_work+0x170/0x170
[ 108.843100] ret_from_fork+0x35/0x40
The bug is caused by the race between timeout handle and completion for
flush request.
When timeout handle function blk_mq_rq_timed_out() try to read
'req->q->mq_ops', the 'req' have completed and reinitiated by next
flush request, which would call blk_rq_init() to clear 'req' as 0.
After commit 12f5b93145 ("blk-mq: Remove generation seqeunce"),
normal requests lifetime are protected by refcount. Until 'rq->ref'
drop to zero, the request can really be free. Thus, these requests
cannot been reused before timeout handle finish.
However, flush request has defined .end_io and rq->end_io() is still
called even if 'rq->ref' doesn't drop to zero. After that, the 'flush_rq'
can be reused by the next flush request handle, resulting in null
pointer deference BUG ON.
We fix this problem by covering flush request with 'rq->ref'.
If the refcount is not zero, flush_end_io() return and wait the
last holder recall it. To record the request status, we add a new
entry 'rq_status', which will be used in flush_end_io().
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Keith Busch <keith.busch@intel.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: stable@vger.kernel.org # v4.18+
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Bob Liu <bob.liu@oracle.com>
Signed-off-by: Yufen Yu <yuyufen@huawei.com>
-------
v2:
- move rq_status from struct request to struct blk_flush_queue
v3:
- remove unnecessary '{}' pair.
v4:
- let spinlock to protect 'fq->rq_status'
v5:
- move rq_status after flush_running_idx member of struct blk_flush_queue
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-09-27 16:19:55 +08:00
|
|
|
error = fq->rq_status;
|
block: Fix fsync always failed if once failed
We do test with inject error fault base on v4.19, after test some time we found
sync /dev/sda always failed.
[root@localhost] sync /dev/sda
sync: error syncing '/dev/sda': Input/output error
scsi log as follows:
[19069.812296] sd 0:0:0:0: [sda] tag#64 Send: scmd 0x00000000d03a0b6b
[19069.812302] sd 0:0:0:0: [sda] tag#64 CDB: Synchronize Cache(10) 35 00 00 00 00 00 00 00 00 00
[19069.812533] sd 0:0:0:0: [sda] tag#64 Done: SUCCESS Result: hostbyte=DID_OK driverbyte=DRIVER_OK
[19069.812536] sd 0:0:0:0: [sda] tag#64 CDB: Synchronize Cache(10) 35 00 00 00 00 00 00 00 00 00
[19069.812539] sd 0:0:0:0: [sda] tag#64 scsi host busy 1 failed 0
[19069.812542] sd 0:0:0:0: Notifying upper driver of completion (result 0)
[19069.812546] sd 0:0:0:0: [sda] tag#64 sd_done: completed 0 of 0 bytes
[19069.812549] sd 0:0:0:0: [sda] tag#64 0 sectors total, 0 bytes done.
[19069.812564] print_req_error: I/O error, dev sda, sector 0
ftrace log as follows:
rep-306069 [007] .... 19654.923315: block_bio_queue: 8,0 FWS 0 + 0 [rep]
rep-306069 [007] .... 19654.923333: block_getrq: 8,0 FWS 0 + 0 [rep]
kworker/7:1H-250 [007] .... 19654.923352: block_rq_issue: 8,0 FF 0 () 0 + 0 [kworker/7:1H]
<idle>-0 [007] ..s. 19654.923562: block_rq_complete: 8,0 FF () 18446744073709551615 + 0 [0]
<idle>-0 [007] d.s. 19654.923576: block_rq_complete: 8,0 WS () 0 + 0 [-5]
As 8d6996630c03 introduce 'fq->rq_status', this data only update when 'flush_rq'
reference count isn't zero. If flush request once failed and record error code
in 'fq->rq_status'. If there is no chance to update 'fq->rq_status',then do fsync
will always failed.
To address this issue reset 'fq->rq_status' after return error code to upper layer.
Fixes: 8d6996630c03("block: fix null pointer dereference in blk_mq_rq_timed_out()")
Signed-off-by: Ye Bin <yebin10@huawei.com>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20211129012659.1553733-1-yebin10@huawei.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2021-11-29 09:26:59 +08:00
|
|
|
fq->rq_status = BLK_STS_OK;
|
|
|
|
}
|
block: fix null pointer dereference in blk_mq_rq_timed_out()
We got a null pointer deference BUG_ON in blk_mq_rq_timed_out()
as following:
[ 108.825472] BUG: kernel NULL pointer dereference, address: 0000000000000040
[ 108.827059] PGD 0 P4D 0
[ 108.827313] Oops: 0000 [#1] SMP PTI
[ 108.827657] CPU: 6 PID: 198 Comm: kworker/6:1H Not tainted 5.3.0-rc8+ #431
[ 108.829503] Workqueue: kblockd blk_mq_timeout_work
[ 108.829913] RIP: 0010:blk_mq_check_expired+0x258/0x330
[ 108.838191] Call Trace:
[ 108.838406] bt_iter+0x74/0x80
[ 108.838665] blk_mq_queue_tag_busy_iter+0x204/0x450
[ 108.839074] ? __switch_to_asm+0x34/0x70
[ 108.839405] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.839823] ? blk_mq_stop_hw_queue+0x40/0x40
[ 108.840273] ? syscall_return_via_sysret+0xf/0x7f
[ 108.840732] blk_mq_timeout_work+0x74/0x200
[ 108.841151] process_one_work+0x297/0x680
[ 108.841550] worker_thread+0x29c/0x6f0
[ 108.841926] ? rescuer_thread+0x580/0x580
[ 108.842344] kthread+0x16a/0x1a0
[ 108.842666] ? kthread_flush_work+0x170/0x170
[ 108.843100] ret_from_fork+0x35/0x40
The bug is caused by the race between timeout handle and completion for
flush request.
When timeout handle function blk_mq_rq_timed_out() try to read
'req->q->mq_ops', the 'req' have completed and reinitiated by next
flush request, which would call blk_rq_init() to clear 'req' as 0.
After commit 12f5b93145 ("blk-mq: Remove generation seqeunce"),
normal requests lifetime are protected by refcount. Until 'rq->ref'
drop to zero, the request can really be free. Thus, these requests
cannot been reused before timeout handle finish.
However, flush request has defined .end_io and rq->end_io() is still
called even if 'rq->ref' doesn't drop to zero. After that, the 'flush_rq'
can be reused by the next flush request handle, resulting in null
pointer deference BUG ON.
We fix this problem by covering flush request with 'rq->ref'.
If the refcount is not zero, flush_end_io() return and wait the
last holder recall it. To record the request status, we add a new
entry 'rq_status', which will be used in flush_end_io().
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Keith Busch <keith.busch@intel.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: stable@vger.kernel.org # v4.18+
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Bob Liu <bob.liu@oracle.com>
Signed-off-by: Yufen Yu <yuyufen@huawei.com>
-------
v2:
- move rq_status from struct request to struct blk_flush_queue
v3:
- remove unnecessary '{}' pair.
v4:
- let spinlock to protect 'fq->rq_status'
v5:
- move rq_status after flush_running_idx member of struct blk_flush_queue
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-09-27 16:19:55 +08:00
|
|
|
|
2020-07-02 12:58:32 +08:00
|
|
|
if (!q->elevator) {
|
2020-07-06 22:41:11 +08:00
|
|
|
flush_rq->tag = BLK_MQ_NO_TAG;
|
2020-07-02 12:58:32 +08:00
|
|
|
} else {
|
|
|
|
blk_mq_put_driver_tag(flush_rq);
|
2020-07-06 22:41:11 +08:00
|
|
|
flush_rq->internal_tag = BLK_MQ_NO_TAG;
|
2020-07-02 12:58:32 +08:00
|
|
|
}
|
2014-02-11 00:29:00 +08:00
|
|
|
|
2014-09-25 23:23:43 +08:00
|
|
|
running = &fq->flush_queue[fq->flush_running_idx];
|
|
|
|
BUG_ON(fq->flush_pending_idx == fq->flush_running_idx);
|
2011-01-25 19:43:54 +08:00
|
|
|
|
|
|
|
/* account completion of the flush request */
|
2014-09-25 23:23:43 +08:00
|
|
|
fq->flush_running_idx ^= 1;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
|
2011-01-25 19:43:54 +08:00
|
|
|
/* and push the waiting requests to the next stage */
|
2023-07-17 12:00:58 +08:00
|
|
|
list_for_each_entry_safe(rq, n, running, queuelist) {
|
2011-01-25 19:43:54 +08:00
|
|
|
unsigned int seq = blk_flush_cur_seq(rq);
|
|
|
|
|
|
|
|
BUG_ON(seq != REQ_FSEQ_PREFLUSH && seq != REQ_FSEQ_POSTFLUSH);
|
2024-06-08 22:31:15 +08:00
|
|
|
list_del_init(&rq->queuelist);
|
2018-11-01 02:43:24 +08:00
|
|
|
blk_flush_complete_seq(rq, fq, seq, error);
|
2011-01-25 19:43:54 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 05:44:12 +08:00
|
|
|
spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
|
2022-09-22 05:19:54 +08:00
|
|
|
return RQ_END_IO_NONE;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
}
|
|
|
|
|
2021-08-18 09:09:25 +08:00
|
|
|
bool is_flush_rq(struct request *rq)
|
|
|
|
{
|
|
|
|
return rq->end_io == flush_end_io;
|
|
|
|
}
|
|
|
|
|
2011-01-25 19:43:54 +08:00
|
|
|
/**
|
|
|
|
* blk_kick_flush - consider issuing flush request
|
|
|
|
* @q: request_queue being kicked
|
2014-09-25 23:23:45 +08:00
|
|
|
* @fq: flush queue
|
2018-06-06 22:21:40 +08:00
|
|
|
* @flags: cmd_flags of the original request
|
2011-01-25 19:43:54 +08:00
|
|
|
*
|
|
|
|
* Flush related states of @q have changed, consider issuing flush request.
|
|
|
|
* Please read the comment at the top of this file for more info.
|
|
|
|
*
|
|
|
|
* CONTEXT:
|
2018-11-15 00:02:10 +08:00
|
|
|
* spin_lock_irq(fq->mq_flush_lock)
|
2011-01-25 19:43:54 +08:00
|
|
|
*
|
|
|
|
*/
|
2018-11-01 02:43:24 +08:00
|
|
|
static void blk_kick_flush(struct request_queue *q, struct blk_flush_queue *fq,
|
2022-07-15 02:06:32 +08:00
|
|
|
blk_opf_t flags)
|
2008-01-29 21:53:40 +08:00
|
|
|
{
|
2014-09-25 23:23:43 +08:00
|
|
|
struct list_head *pending = &fq->flush_queue[fq->flush_pending_idx];
|
2011-01-25 19:43:54 +08:00
|
|
|
struct request *first_rq =
|
2023-07-17 12:00:58 +08:00
|
|
|
list_first_entry(pending, struct request, queuelist);
|
2014-09-25 23:23:43 +08:00
|
|
|
struct request *flush_rq = fq->flush_rq;
|
2011-01-25 19:43:54 +08:00
|
|
|
|
|
|
|
/* C1 described at the top of this file */
|
2014-09-25 23:23:43 +08:00
|
|
|
if (fq->flush_pending_idx != fq->flush_running_idx || list_empty(pending))
|
2018-11-01 02:43:24 +08:00
|
|
|
return;
|
2011-01-25 19:43:54 +08:00
|
|
|
|
2020-07-16 14:52:01 +08:00
|
|
|
/* C2 and C3 */
|
2023-07-17 12:00:57 +08:00
|
|
|
if (fq->flush_data_in_flight &&
|
2011-01-25 19:43:54 +08:00
|
|
|
time_before(jiffies,
|
2014-09-25 23:23:43 +08:00
|
|
|
fq->flush_pending_since + FLUSH_PENDING_TIMEOUT))
|
2018-11-01 02:43:24 +08:00
|
|
|
return;
|
2011-01-25 19:43:54 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Issue flush and toggle pending_idx. This makes pending_idx
|
|
|
|
* different from running_idx, which means flush is in flight.
|
|
|
|
*/
|
2014-09-25 23:23:43 +08:00
|
|
|
fq->flush_pending_idx ^= 1;
|
2014-02-11 00:29:00 +08:00
|
|
|
|
2014-09-25 23:23:42 +08:00
|
|
|
blk_rq_init(q, flush_rq);
|
2014-09-25 23:23:47 +08:00
|
|
|
|
|
|
|
/*
|
2017-11-02 23:24:38 +08:00
|
|
|
* In case of none scheduler, borrow tag from the first request
|
|
|
|
* since they can't be in flight at the same time. And acquire
|
|
|
|
* the tag's ownership for flush req.
|
|
|
|
*
|
|
|
|
* In case of IO scheduler, flush rq need to borrow scheduler tag
|
|
|
|
* just for cheating put/get driver tag.
|
2014-09-25 23:23:47 +08:00
|
|
|
*/
|
2018-10-14 05:44:12 +08:00
|
|
|
flush_rq->mq_ctx = first_rq->mq_ctx;
|
2018-10-30 05:06:13 +08:00
|
|
|
flush_rq->mq_hctx = first_rq->mq_hctx;
|
2018-10-14 05:44:12 +08:00
|
|
|
|
2023-09-13 23:16:13 +08:00
|
|
|
if (!q->elevator)
|
2018-10-14 05:44:12 +08:00
|
|
|
flush_rq->tag = first_rq->tag;
|
2023-09-13 23:16:13 +08:00
|
|
|
else
|
2018-10-14 05:44:12 +08:00
|
|
|
flush_rq->internal_tag = first_rq->internal_tag;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
|
2016-11-01 21:40:10 +08:00
|
|
|
flush_rq->cmd_flags = REQ_OP_FLUSH | REQ_PREFLUSH;
|
2018-06-06 22:21:40 +08:00
|
|
|
flush_rq->cmd_flags |= (flags & REQ_DRV) | (flags & REQ_FAILFAST_MASK);
|
2016-10-20 21:12:13 +08:00
|
|
|
flush_rq->rq_flags |= RQF_FLUSH_SEQ;
|
2014-09-25 23:23:42 +08:00
|
|
|
flush_rq->end_io = flush_end_io;
|
blk-mq: fix kernel panic during iterating over flush request
For fixing use-after-free during iterating over requests, we grabbed
request's refcount before calling ->fn in commit 2e315dc07df0 ("blk-mq:
grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter").
Turns out this way may cause kernel panic when iterating over one flush
request:
1) old flush request's tag is just released, and this tag is reused by
one new request, but ->rqs[] isn't updated yet
2) the flush request can be re-used for submitting one new flush command,
so blk_rq_init() is called at the same time
3) meantime blk_mq_queue_tag_busy_iter() is called, and old flush request
is retrieved from ->rqs[tag]; when blk_mq_put_rq_ref() is called,
flush_rq->end_io may not be updated yet, so NULL pointer dereference
is triggered in blk_mq_put_rq_ref().
Fix the issue by calling refcount_set(&flush_rq->ref, 1) after
flush_rq->end_io is set. So far the only other caller of blk_rq_init() is
scsi_ioctl_reset() in which the request doesn't enter block IO stack and
the request reference count isn't used, so the change is safe.
Fixes: 2e315dc07df0 ("blk-mq: grab rq->refcount before calling ->fn in blk_mq_tagset_busy_iter")
Reported-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
Tested-by: "Blank-Burian, Markus, Dr." <blankburian@uni-muenster.de>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: John Garry <john.garry@huawei.com>
Link: https://lore.kernel.org/r/20210811142624.618598-1-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2021-08-11 22:26:24 +08:00
|
|
|
/*
|
|
|
|
* Order WRITE ->end_io and WRITE rq->ref, and its pair is the one
|
|
|
|
* implied in refcount_inc_not_zero() called from
|
|
|
|
* blk_mq_find_and_get_req(), which orders WRITE/READ flush_rq->ref
|
|
|
|
* and READ flush_rq->end_io
|
|
|
|
*/
|
|
|
|
smp_wmb();
|
2021-10-15 04:39:59 +08:00
|
|
|
req_ref_set(flush_rq, 1);
|
2011-01-25 19:43:54 +08:00
|
|
|
|
2023-05-19 12:40:50 +08:00
|
|
|
spin_lock(&q->requeue_lock);
|
|
|
|
list_add_tail(&flush_rq->queuelist, &q->flush_list);
|
|
|
|
spin_unlock(&q->requeue_lock);
|
|
|
|
|
2023-04-13 14:40:53 +08:00
|
|
|
blk_mq_kick_requeue_list(q);
|
2008-01-29 21:53:40 +08:00
|
|
|
}
|
|
|
|
|
2022-09-22 05:19:54 +08:00
|
|
|
static enum rq_end_io_ret mq_flush_data_end_io(struct request *rq,
|
|
|
|
blk_status_t error)
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
{
|
|
|
|
struct request_queue *q = rq->q;
|
2018-10-30 05:06:13 +08:00
|
|
|
struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
|
2014-09-25 23:23:46 +08:00
|
|
|
struct blk_mq_ctx *ctx = rq->mq_ctx;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
unsigned long flags;
|
2014-09-25 23:23:46 +08:00
|
|
|
struct blk_flush_queue *fq = blk_get_flush_queue(q, ctx);
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
|
2020-07-02 12:58:32 +08:00
|
|
|
if (q->elevator) {
|
|
|
|
WARN_ON(rq->tag < 0);
|
|
|
|
blk_mq_put_driver_tag(rq);
|
|
|
|
}
|
|
|
|
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
/*
|
|
|
|
* After populating an empty queue, kick it to avoid stall. Read
|
|
|
|
* the comment in flush_end_io().
|
|
|
|
*/
|
2014-09-25 23:23:43 +08:00
|
|
|
spin_lock_irqsave(&fq->mq_flush_lock, flags);
|
2023-07-17 12:00:57 +08:00
|
|
|
fq->flush_data_in_flight--;
|
2023-07-17 12:00:58 +08:00
|
|
|
/*
|
|
|
|
* May have been corrupted by rq->rq_next reuse, we need to
|
|
|
|
* re-initialize rq->queuelist before reusing it here.
|
|
|
|
*/
|
|
|
|
INIT_LIST_HEAD(&rq->queuelist);
|
2017-01-17 21:03:22 +08:00
|
|
|
blk_flush_complete_seq(rq, fq, REQ_FSEQ_DATA, error);
|
2014-09-25 23:23:43 +08:00
|
|
|
spin_unlock_irqrestore(&fq->mq_flush_lock, flags);
|
2017-01-17 21:03:22 +08:00
|
|
|
|
2019-01-30 17:01:56 +08:00
|
|
|
blk_mq_sched_restart(hctx);
|
2022-09-22 05:19:54 +08:00
|
|
|
return RQ_END_IO_NONE;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
}
|
|
|
|
|
2023-05-19 12:40:44 +08:00
|
|
|
static void blk_rq_init_flush(struct request *rq)
|
|
|
|
{
|
|
|
|
rq->flush.seq = 0;
|
|
|
|
rq->rq_flags |= RQF_FLUSH_SEQ;
|
|
|
|
rq->flush.saved_end_io = rq->end_io; /* Usually NULL */
|
|
|
|
rq->end_io = mq_flush_data_end_io;
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:40:46 +08:00
|
|
|
/*
|
|
|
|
* Insert a PREFLUSH/FUA request into the flush state machine.
|
|
|
|
* Returns true if the request has been consumed by the flush state machine,
|
|
|
|
* or false if the caller should continue to process it.
|
2011-01-25 19:43:54 +08:00
|
|
|
*/
|
2023-05-19 12:40:46 +08:00
|
|
|
bool blk_insert_flush(struct request *rq)
|
2008-01-29 21:53:40 +08:00
|
|
|
{
|
2011-01-25 19:43:54 +08:00
|
|
|
struct request_queue *q = rq->q;
|
2014-09-25 23:23:46 +08:00
|
|
|
struct blk_flush_queue *fq = blk_get_flush_queue(q, rq->mq_ctx);
|
2024-06-17 14:04:40 +08:00
|
|
|
bool supports_fua = q->limits.features & BLK_FEAT_FUA;
|
2024-06-17 14:04:39 +08:00
|
|
|
unsigned int policy = 0;
|
2008-01-29 21:53:40 +08:00
|
|
|
|
2023-05-19 12:40:45 +08:00
|
|
|
/* FLUSH/FUA request must never be merged */
|
|
|
|
WARN_ON_ONCE(rq->bio != rq->biotail);
|
|
|
|
|
2024-06-17 14:04:39 +08:00
|
|
|
if (blk_rq_sectors(rq))
|
|
|
|
policy |= REQ_FSEQ_DATA;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check which flushes we need to sequence for this operation.
|
|
|
|
*/
|
2024-06-17 14:04:40 +08:00
|
|
|
if (blk_queue_write_cache(q)) {
|
2024-06-17 14:04:39 +08:00
|
|
|
if (rq->cmd_flags & REQ_PREFLUSH)
|
|
|
|
policy |= REQ_FSEQ_PREFLUSH;
|
2024-06-17 14:04:40 +08:00
|
|
|
if ((rq->cmd_flags & REQ_FUA) && !supports_fua)
|
2024-06-17 14:04:39 +08:00
|
|
|
policy |= REQ_FSEQ_POSTFLUSH;
|
|
|
|
}
|
|
|
|
|
2011-01-25 19:43:54 +08:00
|
|
|
/*
|
|
|
|
* @policy now records what operations need to be done. Adjust
|
2016-06-06 03:32:25 +08:00
|
|
|
* REQ_PREFLUSH and FUA for the driver.
|
2011-01-25 19:43:54 +08:00
|
|
|
*/
|
2016-06-06 03:32:25 +08:00
|
|
|
rq->cmd_flags &= ~REQ_PREFLUSH;
|
2024-06-17 14:04:40 +08:00
|
|
|
if (!supports_fua)
|
2011-01-25 19:43:54 +08:00
|
|
|
rq->cmd_flags &= ~REQ_FUA;
|
|
|
|
|
2016-11-09 10:39:28 +08:00
|
|
|
/*
|
|
|
|
* REQ_PREFLUSH|REQ_FUA implies REQ_SYNC, so if we clear any
|
|
|
|
* of those flags, we have to set REQ_SYNC to avoid skewing
|
|
|
|
* the request accounting.
|
|
|
|
*/
|
|
|
|
rq->cmd_flags |= REQ_SYNC;
|
|
|
|
|
2023-05-19 12:40:45 +08:00
|
|
|
switch (policy) {
|
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* An empty flush handed down from a stacking driver may
|
|
|
|
* translate into nothing if the underlying device does not
|
|
|
|
* advertise a write-back cache. In this case, simply
|
|
|
|
* complete the request.
|
|
|
|
*/
|
2018-10-14 05:44:12 +08:00
|
|
|
blk_mq_end_request(rq, 0);
|
2023-05-19 12:40:46 +08:00
|
|
|
return true;
|
2023-05-19 12:40:45 +08:00
|
|
|
case REQ_FSEQ_DATA:
|
|
|
|
/*
|
|
|
|
* If there's data, but no flush is necessary, the request can
|
|
|
|
* be processed directly without going through flush machinery.
|
|
|
|
* Queue for normal execution.
|
|
|
|
*/
|
2023-05-19 12:40:46 +08:00
|
|
|
return false;
|
2023-05-19 12:40:48 +08:00
|
|
|
case REQ_FSEQ_DATA | REQ_FSEQ_POSTFLUSH:
|
|
|
|
/*
|
|
|
|
* Initialize the flush fields and completion handler to trigger
|
|
|
|
* the post flush, and then just pass the command on.
|
|
|
|
*/
|
|
|
|
blk_rq_init_flush(rq);
|
2023-07-17 12:00:56 +08:00
|
|
|
rq->flush.seq |= REQ_FSEQ_PREFLUSH;
|
2023-05-19 12:40:48 +08:00
|
|
|
spin_lock_irq(&fq->mq_flush_lock);
|
2023-07-17 12:00:57 +08:00
|
|
|
fq->flush_data_in_flight++;
|
2023-05-19 12:40:48 +08:00
|
|
|
spin_unlock_irq(&fq->mq_flush_lock);
|
|
|
|
return false;
|
2023-05-19 12:40:45 +08:00
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* Mark the request as part of a flush sequence and submit it
|
|
|
|
* for further processing to the flush state machine.
|
|
|
|
*/
|
|
|
|
blk_rq_init_flush(rq);
|
|
|
|
spin_lock_irq(&fq->mq_flush_lock);
|
|
|
|
blk_flush_complete_seq(rq, fq, REQ_FSEQ_ACTIONS & ~policy, 0);
|
|
|
|
spin_unlock_irq(&fq->mq_flush_lock);
|
2023-05-19 12:40:46 +08:00
|
|
|
return true;
|
2021-11-18 23:30:41 +08:00
|
|
|
}
|
2008-01-29 21:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* blkdev_issue_flush - queue a flush
|
|
|
|
* @bdev: blockdev to issue flush for
|
|
|
|
*
|
|
|
|
* Description:
|
2020-05-13 20:36:00 +08:00
|
|
|
* Issue a flush for the block device in question.
|
2008-01-29 21:53:40 +08:00
|
|
|
*/
|
2021-01-26 22:52:35 +08:00
|
|
|
int blkdev_issue_flush(struct block_device *bdev)
|
2008-01-29 21:53:40 +08:00
|
|
|
{
|
2021-01-26 22:52:35 +08:00
|
|
|
struct bio bio;
|
2008-01-29 21:53:40 +08:00
|
|
|
|
2022-01-24 17:11:06 +08:00
|
|
|
bio_init(&bio, bdev, NULL, 0, REQ_OP_WRITE | REQ_PREFLUSH);
|
2021-01-26 22:52:35 +08:00
|
|
|
return submit_bio_wait(&bio);
|
2008-01-29 21:53:40 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(blkdev_issue_flush);
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
|
2020-03-10 05:41:37 +08:00
|
|
|
struct blk_flush_queue *blk_alloc_flush_queue(int node, int cmd_size,
|
|
|
|
gfp_t flags)
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
{
|
2014-09-25 23:23:43 +08:00
|
|
|
struct blk_flush_queue *fq;
|
|
|
|
int rq_sz = sizeof(struct request);
|
2014-09-25 23:23:39 +08:00
|
|
|
|
2018-10-12 18:07:26 +08:00
|
|
|
fq = kzalloc_node(sizeof(*fq), flags, node);
|
2014-09-25 23:23:43 +08:00
|
|
|
if (!fq)
|
|
|
|
goto fail;
|
2014-09-25 23:23:39 +08:00
|
|
|
|
2018-10-14 05:44:12 +08:00
|
|
|
spin_lock_init(&fq->mq_flush_lock);
|
2014-09-25 23:23:43 +08:00
|
|
|
|
2017-01-28 00:51:45 +08:00
|
|
|
rq_sz = round_up(rq_sz + cmd_size, cache_line_size());
|
2018-10-12 18:07:26 +08:00
|
|
|
fq->flush_rq = kzalloc_node(rq_sz, flags, node);
|
2014-09-25 23:23:43 +08:00
|
|
|
if (!fq->flush_rq)
|
|
|
|
goto fail_rq;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&fq->flush_queue[0]);
|
|
|
|
INIT_LIST_HEAD(&fq->flush_queue[1]);
|
|
|
|
|
|
|
|
return fq;
|
|
|
|
|
|
|
|
fail_rq:
|
|
|
|
kfree(fq);
|
|
|
|
fail:
|
|
|
|
return NULL;
|
blk-mq: new multi-queue block IO queueing mechanism
Linux currently has two models for block devices:
- The classic request_fn based approach, where drivers use struct
request units for IO. The block layer provides various helper
functionalities to let drivers share code, things like tag
management, timeout handling, queueing, etc.
- The "stacked" approach, where a driver squeezes in between the
block layer and IO submitter. Since this bypasses the IO stack,
driver generally have to manage everything themselves.
With drivers being written for new high IOPS devices, the classic
request_fn based driver doesn't work well enough. The design dates
back to when both SMP and high IOPS was rare. It has problems with
scaling to bigger machines, and runs into scaling issues even on
smaller machines when you have IOPS in the hundreds of thousands
per device.
The stacked approach is then most often selected as the model
for the driver. But this means that everybody has to re-invent
everything, and along with that we get all the problems again
that the shared approach solved.
This commit introduces blk-mq, block multi queue support. The
design is centered around per-cpu queues for queueing IO, which
then funnel down into x number of hardware submission queues.
We might have a 1:1 mapping between the two, or it might be
an N:M mapping. That all depends on what the hardware supports.
blk-mq provides various helper functions, which include:
- Scalable support for request tagging. Most devices need to
be able to uniquely identify a request both in the driver and
to the hardware. The tagging uses per-cpu caches for freed
tags, to enable cache hot reuse.
- Timeout handling without tracking request on a per-device
basis. Basically the driver should be able to get a notification,
if a request happens to fail.
- Optional support for non 1:1 mappings between issue and
submission queues. blk-mq can redirect IO completions to the
desired location.
- Support for per-request payloads. Drivers almost always need
to associate a request structure with some driver private
command structure. Drivers can tell blk-mq this at init time,
and then any request handed to the driver will have the
required size of memory associated with it.
- Support for merging of IO, and plugging. The stacked model
gets neither of these. Even for high IOPS devices, merging
sequential IO reduces per-command overhead and thus
increases bandwidth.
For now, this is provided as a potential 3rd queueing model, with
the hope being that, as it matures, it can replace both the classic
and stacked model. That would get us back to having just 1 real
model for block devices, leaving the stacked approach to dm/md
devices (as it was originally intended).
Contributions in this patch from the following people:
Shaohua Li <shli@fusionio.com>
Alexander Gordeev <agordeev@redhat.com>
Christoph Hellwig <hch@infradead.org>
Mike Christie <michaelc@cs.wisc.edu>
Matias Bjorling <m@bjorling.me>
Jeff Moyer <jmoyer@redhat.com>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-24 16:20:05 +08:00
|
|
|
}
|
2014-09-25 23:23:40 +08:00
|
|
|
|
2014-09-25 23:23:44 +08:00
|
|
|
void blk_free_flush_queue(struct blk_flush_queue *fq)
|
2014-09-25 23:23:40 +08:00
|
|
|
{
|
2014-09-25 23:23:43 +08:00
|
|
|
/* bio based request queue hasn't flush queue */
|
|
|
|
if (!fq)
|
|
|
|
return;
|
2014-09-25 23:23:41 +08:00
|
|
|
|
2014-09-25 23:23:43 +08:00
|
|
|
kfree(fq->flush_rq);
|
|
|
|
kfree(fq);
|
|
|
|
}
|
2020-12-03 09:26:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow driver to set its own lock class to fq->mq_flush_lock for
|
|
|
|
* avoiding lockdep complaint.
|
|
|
|
*
|
|
|
|
* flush_end_io() may be called recursively from some driver, such as
|
|
|
|
* nvme-loop, so lockdep may complain 'possible recursive locking' because
|
|
|
|
* all 'struct blk_flush_queue' instance share same mq_flush_lock lock class
|
|
|
|
* key. We need to assign different lock class for these driver's
|
|
|
|
* fq->mq_flush_lock for avoiding the lockdep warning.
|
|
|
|
*
|
|
|
|
* Use dynamically allocated lock class key for each 'blk_flush_queue'
|
|
|
|
* instance is over-kill, and more worse it introduces horrible boot delay
|
|
|
|
* issue because synchronize_rcu() is implied in lockdep_unregister_key which
|
|
|
|
* is called for each hctx release. SCSI probing may synchronously create and
|
|
|
|
* destroy lots of MQ request_queues for non-existent devices, and some robot
|
|
|
|
* test kernel always enable lockdep option. It is observed that more than half
|
|
|
|
* an hour is taken during SCSI MQ probe with per-fq lock class.
|
|
|
|
*/
|
|
|
|
void blk_mq_hctx_set_fq_lock_class(struct blk_mq_hw_ctx *hctx,
|
|
|
|
struct lock_class_key *key)
|
|
|
|
{
|
|
|
|
lockdep_set_class(&hctx->fq->mq_flush_lock, key);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(blk_mq_hctx_set_fq_lock_class);
|