From patchwork Thu Jul 4 12:43:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 1127527 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45fd2s6FW5z9sBp for ; Thu, 4 Jul 2019 22:44:21 +1000 (AEST) Received: from localhost ([::1]:45332 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hj163-0004hX-VE for incoming@patchwork.ozlabs.org; Thu, 04 Jul 2019 08:44:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42306) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hj15l-0004dd-WE for qemu-devel@nongnu.org; Thu, 04 Jul 2019 08:44:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hj15k-0002fr-Mw for qemu-devel@nongnu.org; Thu, 04 Jul 2019 08:44:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44852) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hj15h-0002bJ-T4; Thu, 04 Jul 2019 08:43:58 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9298B2F8BFE; Thu, 4 Jul 2019 12:43:51 +0000 (UTC) Received: from maximlenovopc.usersys.redhat.com (dhcp-4-67.tlv.redhat.com [10.35.4.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id D2FBA6B8CC; Thu, 4 Jul 2019 12:43:49 +0000 (UTC) From: Maxim Levitsky To: qemu-devel@nongnu.org Date: Thu, 4 Jul 2019 15:43:42 +0300 Message-Id: <20190704124342.7753-2-mlevitsk@redhat.com> In-Reply-To: <20190704124342.7753-1-mlevitsk@redhat.com> References: <20190704124342.7753-1-mlevitsk@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 04 Jul 2019 12:43:51 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 1/1] raw-posix.c - use max transfer length / max segement count only for SCSI passthrough X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, Maxim Levitsky , John Ferlan , Max Reitz Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Regular kernel block devices (/dev/sda*, /dev/nvme*, etc) don't have max segment size/max segment count hardware requirements exposed to the userspace, but rather the kernel block layer takes care to split the incoming requests that violate these requirements. Allowing the kernel to do the splitting allows qemu to avoid various overheads that arise otherwise from this. This is especially visible in nbd server, exposing as a raw file, a mostly empty qcow2 image over the net. In this case most of the reads by the remote user won't even hit the underlying kernel block device, and therefore most of the overhead will be in the nbd traffic which increases significantly with lower max transfer size. In addition to that even for local block device access the peformance improves a bit due to less traffic between qemu and the kernel when large transfer sizes are used (e.g for image conversion) More info can be found at: https://bugzilla.redhat.com/show_bug.cgi?id=1647104 Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi Reviewed-by: Eric Blake --- block/file-posix.c | 54 ++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index ab05b51a66..4479cc7ab4 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1038,15 +1038,13 @@ static void raw_reopen_abort(BDRVReopenState *state) s->reopen_state = NULL; } -static int hdev_get_max_transfer_length(BlockDriverState *bs, int fd) +static int sg_get_max_transfer_length(int fd) { #ifdef BLKSECTGET int max_bytes = 0; - short max_sectors = 0; - if (bs->sg && ioctl(fd, BLKSECTGET, &max_bytes) == 0) { + + if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) { return max_bytes; - } else if (!bs->sg && ioctl(fd, BLKSECTGET, &max_sectors) == 0) { - return max_sectors << BDRV_SECTOR_BITS; } else { return -errno; } @@ -1055,25 +1053,31 @@ static int hdev_get_max_transfer_length(BlockDriverState *bs, int fd) #endif } -static int hdev_get_max_segments(const struct stat *st) +static int sg_get_max_segments(int fd) { #ifdef CONFIG_LINUX char buf[32]; const char *end; - char *sysfspath; + char *sysfspath = NULL; int ret; - int fd = -1; + int sysfd = -1; long max_segments; + struct stat st; + + if (fstat(fd, &st)) { + ret = -errno; + goto out; + } sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments", - major(st->st_rdev), minor(st->st_rdev)); - fd = open(sysfspath, O_RDONLY); - if (fd == -1) { + major(st.st_rdev), minor(st.st_rdev)); + sysfd = open(sysfspath, O_RDONLY); + if (sysfd == -1) { ret = -errno; goto out; } do { - ret = read(fd, buf, sizeof(buf) - 1); + ret = read(sysfd, buf, sizeof(buf) - 1); } while (ret == -1 && errno == EINTR); if (ret < 0) { ret = -errno; @@ -1090,8 +1094,8 @@ static int hdev_get_max_segments(const struct stat *st) } out: - if (fd != -1) { - close(fd); + if (sysfd != -1) { + close(sysfd); } g_free(sysfspath); return ret; @@ -1103,19 +1107,17 @@ out: static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s = bs->opaque; - struct stat st; - if (!fstat(s->fd, &st)) { - if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { - int ret = hdev_get_max_transfer_length(bs, s->fd); - if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { - bs->bl.max_transfer = pow2floor(ret); - } - ret = hdev_get_max_segments(&st); - if (ret > 0) { - bs->bl.max_transfer = MIN(bs->bl.max_transfer, - ret * getpagesize()); - } + if (bs->sg) { + int ret = sg_get_max_transfer_length(s->fd); + + if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) { + bs->bl.max_transfer = pow2floor(ret); + } + + ret = sg_get_max_segments(s->fd); + if (ret > 0) { + bs->bl.max_transfer = MIN(bs->bl.max_transfer, ret * getpagesize()); } }