Skip to content

Commit

Permalink
zvol processing should use struct bio
Browse files Browse the repository at this point in the history
Internally, zvols are files exposed through the block device API. This
is intended to reduce overhead when things require block devices.
However, the ZoL zvol code emulates a traditional block device in that
it has a top half and a bottom half. This is an unnecessary source of
overhead that does not exist on any other OpenZFS platform does this.
This patch removes it. Early users of this patch reported double digit
performance gains in IOPS on zvols in the range of 50% to 80%.

Comments in the code suggest that the current implementation was done to
obtain IO merging from Linux's IO elevator. However, the DMU already
does write merging while arc_read() should implicitly merge read IOs
because only 1 thread is permitted to fetch the buffer into ARC. In
addition, commercial ZFSOnLinux distributions report that regular files
are more performant than zvols under the current implementation, and the
main consumers of zvols are VMs and iSCSI targets, which have their own
elevators to merge IOs.

Some minor refactoring allows us to register zfs_request() as our
->make_request() handler in place of the generic_make_request()
function. This eliminates the layer of code that broke IO requests on
zvols into a top half and a bottom half. This has several benefits:

1. No per zvol spinlocks.
2. No redundant IO elevator processing.
3. Interrupts are disabled only when actually necessary.
4. No redispatching of IOs when all taskq threads are busy.
5. Linux's page out routines will properly block.
6. Many autotools checks become obsolete.

An unfortunate consequence of eliminating the layer that
generic_make_request() is that we no longer calls the instrumentation
hooks for block IO accounting. Those hooks are GPL-exported, so we
cannot call them ourselves and consequently, we lose the ability to do
IO monitoring via iostat.  Since zvols are internally files mapped as
block devices, this should be okay. Anyone who is willing to accept the
performance penalty for the block IO layer's accounting could use the
loop device in between the zvol and its consumer. Alternatively, perf
and ftrace likely could be used. Also, tools like latencytop will still
work. Tools such as latencytop sometimes provide a better view of
performance bottlenecks than the traditional block IO accounting tools
do.

Lastly, if direct reclaim occurs during spacemap loading and swap is on
a zvol, this code will deadlock. That deadlock could already occur with
sync=always on zvols. Given that swap on zvols is not yet production
ready, this is not a blocker.

Signed-off-by: Richard Yao <ryao@gentoo.org>
  • Loading branch information
ryao committed Sep 4, 2015
1 parent 782b2c3 commit 37f9dac
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 218 deletions.
25 changes: 25 additions & 0 deletions config/kernel-bio-rw-barrier.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
dnl #
dnl # Interface for issuing a discard bio:
dnl # 2.6.28-2.6.35: BIO_RW_BARRIER
dnl # 2.6.36-3.x: REQ_BARRIER
dnl #

dnl # Since REQ_BARRIER is a preprocessor definition, there is no need for an
dnl # autotools check for it. Also, REQ_BARRIER existed in the request layer
dnl # until torvalds/linux@7b6d91daee5cac6402186ff224c3af39d79f4a0e unified the
dnl # request layer and bio layer flags, so it would be wrong to assume that
dnl # the APIs are mutually exclusive contrary to the typical case.
AC_DEFUN([ZFS_AC_KERNEL_BIO_RW_BARRIER], [
AC_MSG_CHECKING([whether BIO_RW_BARRIER is defined])
ZFS_LINUX_TRY_COMPILE([
#include <linux/bio.h>
],[
int flags __attribute__ ((unused));
flags = BIO_RW_BARRIER;
],[
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_BIO_RW_BARRIER, 1, [BIO_RW_BARRIER is defined])
],[
AC_MSG_RESULT(no)
])
])
25 changes: 25 additions & 0 deletions config/kernel-bio-rw-discard.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
dnl #
dnl # Interface for issuing a discard bio:
dnl # 2.6.28-2.6.35: BIO_RW_DISCARD
dnl # 2.6.36-3.x: REQ_DISCARD
dnl #

dnl # Since REQ_DISCARD is a preprocessor definition, there is no need for an
dnl # autotools check for it. Also, REQ_DISCARD existed in the request layer
dnl # until torvalds/linux@7b6d91daee5cac6402186ff224c3af39d79f4a0e unified the
dnl # request layer and bio layer flags, so it would be wrong to assume that
dnl # the APIs are mutually exclusive contrary to the typical case.
AC_DEFUN([ZFS_AC_KERNEL_BIO_RW_DISCARD], [
AC_MSG_CHECKING([whether BIO_RW_DISCARD is defined])
ZFS_LINUX_TRY_COMPILE([
#include <linux/bio.h>
],[
int flags __attribute__ ((unused));
flags = BIO_RW_DISCARD;
],[
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_BIO_RW_DISCARD, 1, [BIO_RW_DISCARD is defined])
],[
AC_MSG_RESULT(no)
])
])
33 changes: 33 additions & 0 deletions config/kernel-current_bio_tail.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
dnl #
dnl # 2.6.34 API change
dnl # current->bio_tail and current->bio_list were struct bio pointers prior to
dnl # Linux 2.6.34. They were refactored into a struct bio_list pointer called
dnl # current->bio_list in Linux 2.6.34.
dnl #
AC_DEFUN([ZFS_AC_KERNEL_CURRENT_BIO_TAIL], [
AC_MSG_CHECKING([whether current->bio_tail exists])
ZFS_LINUX_TRY_COMPILE([
#include <linux/sched.h>
],[
current->bio_tail = (struct bio **) NULL;
],[
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_CURRENT_BIO_TAIL, 1,
[current->bio_tail exists])
],[
AC_MSG_RESULT(no)
AC_MSG_CHECKING([whether current->bio_list exists])
ZFS_LINUX_TRY_COMPILE([
#include <linux/sched.h>
],[
current->bio_list = (struct bio_list *) NULL;
],[
AC_MSG_RESULT(yes)
AC_DEFINE(HAVE_CURRENT_BIO_LIST, 1,
[current->bio_list exists])
],[
AC_MSG_ERROR(no - Please file a bug report at
https://github.com/zfsonlinux/zfs/issues/new)
])
])
])
43 changes: 43 additions & 0 deletions config/kernel-mk-request-fn.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
dnl #
dnl # Linux 3.2 API Change
dnl # make_request_fn returns void instead of int.
dnl #
AC_DEFUN([ZFS_AC_KERNEL_MAKE_REQUEST_FN], [
AC_MSG_CHECKING([whether make_request_fn() returns int])
ZFS_LINUX_TRY_COMPILE([
#include <linux/blkdev.h>
int make_request(struct request_queue *q, struct bio *bio)
{
return (0);
}
],[
blk_queue_make_request(NULL, &make_request);
],[
AC_MSG_RESULT(yes)
AC_DEFINE(MAKE_REQUEST_FN_RET, int,
[make_request_fn() returns int])
AC_DEFINE(HAVE_MAKE_REQUEST_FN_RET_INT, 1,
[Noting that make_request_fn() returns int])
],[
AC_MSG_RESULT(no)
AC_MSG_CHECKING([whether make_request_fn() returns void])
ZFS_LINUX_TRY_COMPILE([
#include <linux/blkdev.h>
void make_request(struct request_queue *q, struct bio *bio)
{
return;
}
],[
blk_queue_make_request(NULL, &make_request);
],[
AC_MSG_RESULT(yes)
AC_DEFINE(MAKE_REQUEST_FN_RET, void,
[make_request_fn() returns void])
],[
AC_MSG_ERROR(no - Please file a bug report at
https://github.com/zfsonlinux/zfs/issues/new)
])
])
])
4 changes: 4 additions & 0 deletions config/kernel.m4
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ AC_DEFUN([ZFS_AC_CONFIG_KERNEL], [
ZFS_AC_TEST_MODULE
ZFS_AC_KERNEL_CONFIG
ZFS_AC_KERNEL_DECLARE_EVENT_CLASS
ZFS_AC_KERNEL_CURRENT_BIO_TAIL
ZFS_AC_KERNEL_BDEV_BLOCK_DEVICE_OPERATIONS
ZFS_AC_KERNEL_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
ZFS_AC_KERNEL_TYPE_FMODE_T
Expand All @@ -22,6 +23,8 @@ AC_DEFUN([ZFS_AC_CONFIG_KERNEL], [
ZFS_AC_KERNEL_BIO_FAILFAST_DTD
ZFS_AC_KERNEL_REQ_FAILFAST_MASK
ZFS_AC_KERNEL_BIO_END_IO_T_ARGS
ZFS_AC_KERNEL_BIO_RW_BARRIER
ZFS_AC_KERNEL_BIO_RW_DISCARD
ZFS_AC_KERNEL_BIO_RW_SYNC
ZFS_AC_KERNEL_BIO_RW_SYNCIO
ZFS_AC_KERNEL_REQ_SYNC
Expand Down Expand Up @@ -100,6 +103,7 @@ AC_DEFUN([ZFS_AC_CONFIG_KERNEL], [
ZFS_AC_KERNEL_VFS_RW_ITERATE
ZFS_AC_KERNEL_KMAP_ATOMIC_ARGS
ZFS_AC_KERNEL_FOLLOW_DOWN_ONE
ZFS_AC_KERNEL_MAKE_REQUEST_FN
AS_IF([test "$LINUX_OBJ" != "$LINUX"], [
KERNELMAKE_PARAMS="$KERNELMAKE_PARAMS O=$LINUX_OBJ"
Expand Down
21 changes: 20 additions & 1 deletion include/linux/blkdev_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,16 @@ struct req_iterator {
#define BIO_BI_SECTOR(bio) (bio)->bi_iter.bi_sector
#define BIO_BI_SIZE(bio) (bio)->bi_iter.bi_size
#define BIO_BI_IDX(bio) (bio)->bi_iter.bi_idx
#define bio_for_each_segment4(bv, bvp, b, i) \
bio_for_each_segment((bv), (b), (i))
typedef struct bvec_iter bvec_iterator_t;
#else
#define BIO_BI_SECTOR(bio) (bio)->bi_sector
#define BIO_BI_SIZE(bio) (bio)->bi_size
#define BIO_BI_IDX(bio) (bio)->bi_idx
#define bio_for_each_segment4(bv, bvp, b, i) \
bio_for_each_segment((bvp), (b), (i))
typedef int bvec_iterator_t;
#endif

/*
Expand Down Expand Up @@ -457,17 +463,30 @@ bio_set_flags_failfast(struct block_device *bdev, int *flags)
#define VDEV_REQ_FUA REQ_FUA
#else
#define VDEV_WRITE_FLUSH_FUA WRITE_BARRIER
#ifdef HAVE_BIO_RW_BARRIER
#define VDEV_REQ_FLUSH (1 << BIO_RW_BARRIER)
#define VDEV_REQ_FUA (1 << BIO_RW_BARRIER)
#else
#define VDEV_REQ_FLUSH REQ_HARDBARRIER
#define VDEV_REQ_FUA REQ_FUA
#endif
#endif

/*
* 2.6.32 API change
* Use the normal I/O patch for discards.
*/
#ifdef REQ_DISCARD
#ifdef QUEUE_FLAG_DISCARD
#ifdef HAVE_BIO_RW_DISCARD
#define VDEV_REQ_DISCARD (1 << BIO_RW_DISCARD)
#else
#define VDEV_REQ_DISCARD REQ_DISCARD
#endif
#else
#error "Allowing the build will cause discard requests to become writes "
"potentially triggering the DMU_MAX_ACCESS assertion. Please file a "
"an issue report at: https://github.com/zfsonlinux/zfs/issues/new"
#endif

/*
* 2.6.33 API change
Expand Down
4 changes: 2 additions & 2 deletions include/sys/dmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ void dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
dmu_tx_t *tx);
#ifdef _KERNEL
#include <linux/blkdev_compat.h>
int dmu_read_req(objset_t *os, uint64_t object, struct request *req);
int dmu_write_req(objset_t *os, uint64_t object, struct request *req,
int dmu_read_bio(objset_t *os, uint64_t object, struct bio *bio);
int dmu_write_bio(objset_t *os, uint64_t object, struct bio *bio,
dmu_tx_t *tx);
int dmu_read_uio(objset_t *os, uint64_t object, struct uio *uio, uint64_t size);
int dmu_read_uio_dbuf(dmu_buf_t *zdb, struct uio *uio, uint64_t size);
Expand Down
11 changes: 0 additions & 11 deletions man/man5/zfs-module-parameters.5
Original file line number Diff line number Diff line change
Expand Up @@ -1591,17 +1591,6 @@ Max number of blocks to discard at once
Default value: \fB16,384\fR.
.RE

.sp
.ne 2
.na
\fBzvol_threads\fR (uint)
.ad
.RS 12n
Max number of threads to handle zvol I/O requests
.sp
Default value: \fB32\fR.
.RE

.SH ZFS I/O SCHEDULER
ZFS issues I/O operations to leaf vdevs to satisfy and complete I/Os.
The I/O scheduler determines when and in what order those operations are
Expand Down
57 changes: 29 additions & 28 deletions module/zfs/dmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,15 +1049,16 @@ xuio_stat_wbuf_nocopy()
* return value is the number of bytes successfully copied to arg_buf.
*/
static int
dmu_req_copy(void *arg_buf, int size, struct request *req, size_t req_offset)
dmu_bio_copy(void *arg_buf, int size, struct bio *bio, size_t bio_offset)
{
struct bio_vec bv, *bvp;
struct req_iterator iter;
struct bio_vec bv, *bvp = &bv;
bvec_iterator_t iter;
char *bv_buf;
int tocpy, bv_len, bv_offset;
int offset = 0;

rq_for_each_segment4(bv, bvp, req, iter) {
bio_for_each_segment4(bv, bvp, bio, iter) {

/*
* Fully consumed the passed arg_buf. We use goto here because
* rq_for_each_segment is a double loop
Expand All @@ -1066,23 +1067,23 @@ dmu_req_copy(void *arg_buf, int size, struct request *req, size_t req_offset)
if (size == offset)
goto out;

/* Skip already copied bv */
if (req_offset >= bv.bv_len) {
req_offset -= bv.bv_len;
/* Skip already copied bvp */
if (bio_offset >= bvp->bv_len) {
bio_offset -= bvp->bv_len;
continue;
}

bv_len = bv.bv_len - req_offset;
bv_offset = bv.bv_offset + req_offset;
req_offset = 0;
bv_len = bvp->bv_len - bio_offset;
bv_offset = bvp->bv_offset + bio_offset;
bio_offset = 0;

tocpy = MIN(bv_len, size - offset);
ASSERT3S(tocpy, >=, 0);

bv_buf = page_address(bv.bv_page) + bv_offset;
bv_buf = page_address(bvp->bv_page) + bv_offset;
ASSERT3P(bv_buf, !=, NULL);

if (rq_data_dir(req) == WRITE)
if (bio_data_dir(bio) == WRITE)
memcpy(arg_buf + offset, bv_buf, tocpy);
else
memcpy(bv_buf, arg_buf + offset, tocpy);
Expand All @@ -1094,13 +1095,13 @@ dmu_req_copy(void *arg_buf, int size, struct request *req, size_t req_offset)
}

int
dmu_read_req(objset_t *os, uint64_t object, struct request *req)
dmu_read_bio(objset_t *os, uint64_t object, struct bio *bio)
{
uint64_t size = blk_rq_bytes(req);
uint64_t offset = blk_rq_pos(req) << 9;
uint64_t offset = BIO_BI_SECTOR(bio) << 9;
uint64_t size = BIO_BI_SIZE(bio);
dmu_buf_t **dbp;
int numbufs, i, err;
size_t req_offset;
size_t bio_offset;

/*
* NB: we could do this block-at-a-time, but it's nice
Expand All @@ -1111,7 +1112,7 @@ dmu_read_req(objset_t *os, uint64_t object, struct request *req)
if (err)
return (err);

req_offset = 0;
bio_offset = 0;
for (i = 0; i < numbufs; i++) {
uint64_t tocpy;
int64_t bufoff;
Expand All @@ -1125,8 +1126,8 @@ dmu_read_req(objset_t *os, uint64_t object, struct request *req)
if (tocpy == 0)
break;

didcpy = dmu_req_copy(db->db_data + bufoff, tocpy, req,
req_offset);
didcpy = dmu_bio_copy(db->db_data + bufoff, tocpy, bio,
bio_offset);

if (didcpy < tocpy)
err = EIO;
Expand All @@ -1136,7 +1137,7 @@ dmu_read_req(objset_t *os, uint64_t object, struct request *req)

size -= tocpy;
offset += didcpy;
req_offset += didcpy;
bio_offset += didcpy;
err = 0;
}
dmu_buf_rele_array(dbp, numbufs, FTAG);
Expand All @@ -1145,13 +1146,13 @@ dmu_read_req(objset_t *os, uint64_t object, struct request *req)
}

int
dmu_write_req(objset_t *os, uint64_t object, struct request *req, dmu_tx_t *tx)
dmu_write_bio(objset_t *os, uint64_t object, struct bio *bio, dmu_tx_t *tx)
{
uint64_t size = blk_rq_bytes(req);
uint64_t offset = blk_rq_pos(req) << 9;
uint64_t offset = BIO_BI_SECTOR(bio) << 9;
uint64_t size = BIO_BI_SIZE(bio);
dmu_buf_t **dbp;
int numbufs, i, err;
size_t req_offset;
size_t bio_offset;

if (size == 0)
return (0);
Expand All @@ -1161,7 +1162,7 @@ dmu_write_req(objset_t *os, uint64_t object, struct request *req, dmu_tx_t *tx)
if (err)
return (err);

req_offset = 0;
bio_offset = 0;
for (i = 0; i < numbufs; i++) {
uint64_t tocpy;
int64_t bufoff;
Expand All @@ -1182,8 +1183,8 @@ dmu_write_req(objset_t *os, uint64_t object, struct request *req, dmu_tx_t *tx)
else
dmu_buf_will_dirty(db, tx);

didcpy = dmu_req_copy(db->db_data + bufoff, tocpy, req,
req_offset);
didcpy = dmu_bio_copy(db->db_data + bufoff, tocpy, bio,
bio_offset);

if (tocpy == db->db_size)
dmu_buf_fill_done(db, tx);
Expand All @@ -1196,7 +1197,7 @@ dmu_write_req(objset_t *os, uint64_t object, struct request *req, dmu_tx_t *tx)

size -= tocpy;
offset += didcpy;
req_offset += didcpy;
bio_offset += didcpy;
err = 0;
}

Expand Down
Loading

0 comments on commit 37f9dac

Please sign in to comment.