From nobody Sun Feb 8 16:05:23 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1562244300; cv=none; d=zoho.com; s=zohoarc; b=XC8nj4Iv7Q7swSAOr+eL+ynZoOqmrwbfMNFxqhzGMqvJKPJn6YUDq3Lh87owNy3+UltMYJckaD15XwITUmpr3/gcsbXsqCqARln9PktNG2zJhcLnER56es0BbyOjNNqFtrPpT1kmB17HG8PVYRgv615ZJcEMLYTB6Fn5mfrxrLU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562244300; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=bjArXlpbYaCpUvqfV4rSa2A/rK4zDHnUc4+0m6nffOM=; b=aU/M9G2+Auq7G8uDwkLt55iRnTdlxPQ1XNbHVD3WRgqdCesooIPurMiy/qrilrqqoB0aPCmsa7tjGwBgfHxa9wc+QOCLN3SI+qfIy+9M9BioI6mdhBlYa75ScHPtXoa/FwtW4S13QoUM9lQtg9O1bQPL3BLFjBesmKtFNtO5dy8= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562244300160712.5695875259458; Thu, 4 Jul 2019 05:45:00 -0700 (PDT) Received: from localhost ([::1]:45340 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hj16h-0005Ui-6a for importer@patchew.org; Thu, 04 Jul 2019 08:44:59 -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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=3D1647104 Signed-off-by: Maxim Levitsky Reviewed-by: Stefan Hajnoczi Reviewed-by: Eric Blake Reviewed-by: Pankaj Gupta --- 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 =3D NULL; } =20 -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 =3D 0; - short max_sectors =3D 0; - if (bs->sg && ioctl(fd, BLKSECTGET, &max_bytes) =3D=3D 0) { + + if (ioctl(fd, BLKSECTGET, &max_bytes) =3D=3D 0) { return max_bytes; - } else if (!bs->sg && ioctl(fd, BLKSECTGET, &max_sectors) =3D=3D 0) { - return max_sectors << BDRV_SECTOR_BITS; } else { return -errno; } @@ -1055,25 +1053,31 @@ static int hdev_get_max_transfer_length(BlockDriver= State *bs, int fd) #endif } =20 -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 =3D NULL; int ret; - int fd =3D -1; + int sysfd =3D -1; long max_segments; + struct stat st; + + if (fstat(fd, &st)) { + ret =3D -errno; + goto out; + } =20 sysfspath =3D g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments= ", - major(st->st_rdev), minor(st->st_rdev)); - fd =3D open(sysfspath, O_RDONLY); - if (fd =3D=3D -1) { + major(st.st_rdev), minor(st.st_rdev)); + sysfd =3D open(sysfspath, O_RDONLY); + if (sysfd =3D=3D -1) { ret =3D -errno; goto out; } do { - ret =3D read(fd, buf, sizeof(buf) - 1); + ret =3D read(sysfd, buf, sizeof(buf) - 1); } while (ret =3D=3D -1 && errno =3D=3D EINTR); if (ret < 0) { ret =3D -errno; @@ -1090,8 +1094,8 @@ static int hdev_get_max_segments(const struct stat *s= t) } =20 out: - if (fd !=3D -1) { - close(fd); + if (sysfd !=3D -1) { + close(sysfd); } g_free(sysfspath); return ret; @@ -1103,19 +1107,17 @@ out: static void raw_refresh_limits(BlockDriverState *bs, Error **errp) { BDRVRawState *s =3D bs->opaque; - struct stat st; =20 - if (!fstat(s->fd, &st)) { - if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { - int ret =3D hdev_get_max_transfer_length(bs, s->fd); - if (ret > 0 && ret <=3D BDRV_REQUEST_MAX_BYTES) { - bs->bl.max_transfer =3D pow2floor(ret); - } - ret =3D hdev_get_max_segments(&st); - if (ret > 0) { - bs->bl.max_transfer =3D MIN(bs->bl.max_transfer, - ret * getpagesize()); - } + if (bs->sg) { + int ret =3D sg_get_max_transfer_length(s->fd); + + if (ret > 0 && ret <=3D BDRV_REQUEST_MAX_BYTES) { + bs->bl.max_transfer =3D pow2floor(ret); + } + + ret =3D sg_get_max_segments(s->fd); + if (ret > 0) { + bs->bl.max_transfer =3D MIN(bs->bl.max_transfer, ret * getpage= size()); } } =20 --=20 2.17.2