From nobody Sun May 12 22:35:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1627070330; cv=none; d=zohomail.com; s=zohoarc; b=Y2BXH6Yqt1wigGU3fzY/te6U86WcKheqazRWdwN9T2DkLWSKOZUacxDMHNvghPQuIXbQMXEIUcSivsGp5vVM7++VZH6yFvutaQibyKnLYLhyw1LabBu4wwm9bd8rCw6E/QM8FkPVhJiYPIIb8G+YzHVXffN1GzZQEyme/tCLMKE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627070330; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:MIME-Version:Message-ID:Subject:To; bh=M21HEs2+wKWVDBeRSP0S+SbBwVj4R2HaqJi7xPG/Bxc=; b=c2ybB2cQXXzeAmeqZdZeGHgZt5gQ52IYcVFWUm5HpA5Ysae175s+WLsXjUwplTay3Byir4zoeIZz8NJNKMF+oDdEaVQHee2/RS5pp5Q93u8PIXWSWovg0IoEZBrhV91rtYD5tAm8e4PVT8YB/cANk5LfJ0ILcq0lIgmginLYubo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1627070330329636.2051806864431; Fri, 23 Jul 2021 12:58:50 -0700 (PDT) Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-161-vR8HrWuCO-2tynI1nleYIw-1; Fri, 23 Jul 2021 15:58:46 -0400 Received: by mail-wm1-f70.google.com with SMTP id 25-20020a05600c0219b029024ebb12928cso499052wmi.3 for ; Fri, 23 Jul 2021 12:58:46 -0700 (PDT) Return-Path: Return-Path: Received: from x1w.. (122.red-83-42-66.dynamicip.rima-tde.net. [83.42.66.122]) by smtp.gmail.com with ESMTPSA id u2sm27627257wmc.42.2021.07.23.12.58.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jul 2021 12:58:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1627070329; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=M21HEs2+wKWVDBeRSP0S+SbBwVj4R2HaqJi7xPG/Bxc=; b=Fb7H91MKi0Q3rpq2bfB7APYSB91ueotTZ99iESBMYiVjAfccvDiyxJZpGweM92zeX9FT4W M3uTh+qx6V63NCOjap1bcn3ePK4rsAQFdXGi/uWg+JriEx+bcz0y2OEFY9SerShdbaIv8P vEnV7U5ZqokJpW3ni5YqbMspC/bRigo= X-MC-Unique: vR8HrWuCO-2tynI1nleYIw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=M21HEs2+wKWVDBeRSP0S+SbBwVj4R2HaqJi7xPG/Bxc=; b=DWa9HYqJ/vR5SCl8HfRJ25j6vUwtcxmKaexs/TRehLtwFZuFM5kmhLK84YX3BJTB7w Tc2RxUV0qVAvTnbXxRWGJ+uIkmiwHpAyEvN+eHjQstIT77HodPRLLcDEBCySnymrDfM8 DRutqLH0bk+XAWBcFPnE1AqlCkSfjrVB4nNMWqflOeMWGBGSF2d/VNiuq2gSQpG/nRSP +7CgkPlBDRxPOkWTApHi8A0TyHZx3TiEjUHxSRMr9+O/IPZScRdNONIc+05c0xpDydCP WYla60Xy/7pD8UU2B4vEEmtwbYzm9seWgY4enF57I7+Gxxctj3E6bB8T0fmhPqE/rYZd k78Q== X-Gm-Message-State: AOAM530sh4sUn/WMP37jdb7nW6AXNx9+eviEVHsTwKURPnb6GFc8a/dk ae/hjxI3PRm5YF8eWPAPLNvdJga96tRwd/DhdDdeSOjrAuIWtHwQBzFig6yYvLv5824/16+65YC SMINekEiYsbB0hA== X-Received: by 2002:a5d:608c:: with SMTP id w12mr7071475wrt.53.1627070325419; Fri, 23 Jul 2021 12:58:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3T0yCH6T3c7QoLEFmdnbC8cj4lvLKKlLT9H+qCAu4ZsJKY8qcJuX6viE7D2lpjwA3TvMLGg== X-Received: by 2002:a5d:608c:: with SMTP id w12mr7071456wrt.53.1627070325264; Fri, 23 Jul 2021 12:58:45 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Alex Williamson , Maxim Levitsky , Max Reitz , Fam Zheng , Kevin Wolf , Stefan Hajnoczi , Eric Auger , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , qemu-stable@nongnu.org, =?UTF-8?q?Michal=20Pr=C3=ADvozn=C3=ADk?= Subject: [PATCH-for-6.1 v3] block/nvme: Fix VFIO_MAP_DMA failed: No space left on device Date: Fri, 23 Jul 2021 21:58:43 +0200 Message-Id: <20210723195843.1032825-1-philmd@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1627070332144100001 When the NVMe block driver was introduced (see commit bdd6a90a9e5, January 2018), Linux VFIO_IOMMU_MAP_DMA ioctl was only returning -ENOMEM in case of error. The driver was correctly handling the error path to recycle its volatile IOVA mappings. To fix CVE-2019-3882, Linux commit 492855939bdb ("vfio/type1: Limit DMA mappings per container", April 2019) added the -ENOSPC error to signal the user exhausted the DMA mappings available for a container. The block driver started to mis-behave: qemu-system-x86_64: VFIO_MAP_DMA failed: No space left on device (qemu) (qemu) info status VM status: paused (io-error) (qemu) c VFIO_MAP_DMA failed: No space left on device (qemu) c VFIO_MAP_DMA failed: No space left on device (The VM is not resumable from here, hence stuck.) Fix by handling the new -ENOSPC error (when DMA mappings are exhausted) without any distinction to the current -ENOMEM error, so we don't change the behavior on old kernels where the CVE-2019-3882 fix is not present. An easy way to reproduce this bug is to restrict the DMA mapping limit (65535 by default) when loading the VFIO IOMMU module: # modprobe vfio_iommu_type1 dma_entry_limit=3D666 Cc: qemu-stable@nongnu.org Cc: Fam Zheng Cc: Maxim Levitsky Cc: Alex Williamson Reported-by: Michal Pr=C3=ADvozn=C3=ADk Fixes: bdd6a90a9e5 ("block: Add VFIO based NVMe driver") Buglink: https://bugs.launchpad.net/qemu/+bug/1863333 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/65 Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- v3: Reworded (Fam) v2: KISS checking both errors undistinguishedly (Maxim) --- block/nvme.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/block/nvme.c b/block/nvme.c index 2b5421e7aa6..e8dbbc23177 100644 --- a/block/nvme.c +++ b/block/nvme.c @@ -1030,7 +1030,29 @@ try_map: r =3D qemu_vfio_dma_map(s->vfio, qiov->iov[i].iov_base, len, true, &iova); + if (r =3D=3D -ENOSPC) { + /* + * In addition to the -ENOMEM error, the VFIO_IOMMU_MAP_DMA + * ioctl returns -ENOSPC to signal the user exhausted the DMA + * mappings available for a container since Linux kernel commit + * 492855939bdb ("vfio/type1: Limit DMA mappings per container= ", + * April 2019, see CVE-2019-3882). + * + * This block driver already handles this error path by checki= ng + * for the -ENOMEM error, so we directly replace -ENOSPC by + * -ENOMEM. Beside, -ENOSPC has a specific meaning for blockdev + * coroutines: it triggers BLOCKDEV_ON_ERROR_ENOSPC and + * BLOCK_ERROR_ACTION_STOP which stops the VM, asking the oper= ator + * to add more storage to the blockdev. Not something we can do + * easily with an IOMMU :) + */ + r =3D -ENOMEM; + } if (r =3D=3D -ENOMEM && retry) { + /* + * We exhausted the DMA mappings available for our container: + * recycle the volatile IOVA mappings. + */ retry =3D false; trace_nvme_dma_flush_queue_wait(s); if (s->dma_map_count) { --=20 2.31.1