From nobody Tue May 7 14:12:58 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) 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=1555502114; cv=none; d=zoho.com; s=zohoarc; b=d4eKw88vbjKEDODn08KhUr235M5pGpkMiXoc2e429ptYSGkPj+aqjSsDpJ17IWtxWy5P1CFgxs0kiXvtrJJkiNjN4TFY5L5H0sQZB9PMsQUc+yYMR+UkXXV0tCSWdawaV1fGw3Qg0VF7iuF8x3h1h+SRCD5/B9wnuokGioI0w5w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1555502114; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=aTonvoy5+A1ucHgbV/tPRpJa7ib7WD0Z9gD7xjGRDSc=; b=j6k+3ETar8Q2M+9RBJdwvah2wX5IDFFyug3zQ4Xbuq8CgYkz+OfRwwHYsMFfXrKf9itr/xvvPUg0UrJgkr3WenJ1XMvc4hH47aM5mGr2bF0uWnpNyYucajhtUBT784PmTikFl9MelYDvcddzbLAaL2YvB5CFiaQcjY0kKVZ3o7w= ARC-Authentication-Results: i=1; mx.zoho.com; spf=temperror (zoho.com: Error in retrieving data from DNS) 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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1555502114363271.94132428976957; Wed, 17 Apr 2019 04:55:14 -0700 (PDT) Received: from localhost ([127.0.0.1]:51130 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGj9W-0006XK-RW for importer@patchew.org; Wed, 17 Apr 2019 07:54:58 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39192) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGj8S-00066V-UT for qemu-devel@nongnu.org; Wed, 17 Apr 2019 07:53:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hGj8R-0003Rx-SZ for qemu-devel@nongnu.org; Wed, 17 Apr 2019 07:53:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54548) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hGj8P-0003PL-Ln; Wed, 17 Apr 2019 07:53:49 -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 7EA854E93D; Wed, 17 Apr 2019 11:53:48 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-44.ams2.redhat.com [10.36.112.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id B77F7608C1; Wed, 17 Apr 2019 11:53:44 +0000 (UTC) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 17 Apr 2019 13:53:43 +0200 Message-Id: <20190417115343.30527-1-pbonzini@redhat.com> MIME-Version: 1.0 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]); Wed, 17 Apr 2019 11:53:48 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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] scsi-generic: prevent guest from exceeding SG_IO limits X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Hajnoczi , qemu-stable@nongnu.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Linux places a limit of UIO_MAXIOV pages on SG_IO ioctls (and if the limit is exceeded, a confusing ENOMEM error is returned[1]). Prevent the guest from exceeding these limits, by capping the maximum transfer length to that value in the block limits VPD page. [1] Oh well, at least it was easier to follow the kernel source knowing it had to end as ENOMEM... Cc: qemu-stable@nongnu.org Cc: qemu-block@nongnu.org Cc: Stefan Hajnoczi Signed-off-by: Paolo Bonzini Reviewed-by: Stefan Hajnoczi --- hw/scsi/scsi-generic.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/hw/scsi/scsi-generic.c b/hw/scsi/scsi-generic.c index d82b462be4..4134d8389b 100644 --- a/hw/scsi/scsi-generic.c +++ b/hw/scsi/scsi-generic.c @@ -131,6 +131,17 @@ static int execute_command(BlockBackend *blk, return 0; } =20 +/* + * Linux places a hard limit on SG_IO transfers equal to UIO_MAXIOV + * pages, which we need to factor in the block limits we return. + */ +static uint32_t sg_max_transfer(SCSIDevice *s) +{ + uint32_t max_transfer =3D blk_get_max_transfer(s->conf.blk); + max_transfer =3D MIN_NON_ZERO(max_transfer, UIO_MAXIOV * qemu_real_hos= t_page_size); + return max_transfer / s->blocksize; +} + static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s) { uint8_t page, page_idx; @@ -162,10 +173,8 @@ static void scsi_handle_inquiry_reply(SCSIGenericReq *= r, SCSIDevice *s) if (s->type =3D=3D TYPE_DISK && (r->req.cmd.buf[1] & 0x01)) { page =3D r->req.cmd.buf[2]; if (page =3D=3D 0xb0) { - uint32_t max_transfer =3D - blk_get_max_transfer(s->conf.blk) / s->blocksize; + uint32_t max_transfer =3D sg_max_transfer(s); =20 - assert(max_transfer); stl_be_p(&r->buf[8], max_transfer); /* Also take care of the opt xfer len. */ stl_be_p(&r->buf[12], @@ -206,7 +215,7 @@ static int scsi_generic_emulate_block_limits(SCSIGeneri= cReq *r, SCSIDevice *s) uint8_t buf[64]; =20 SCSIBlockLimits bl =3D { - .max_io_sectors =3D blk_get_max_transfer(s->conf.blk) / s->blocksi= ze + .max_io_sectors =3D sg_max_transfer(s) }; =20 memset(r->buf, 0, r->buflen); --=20 2.21.0