From nobody Sun Feb 8 03:33:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1576503397; cv=none; d=zohomail.com; s=zohoarc; b=Tko1eNYGcIhoLp96Z31w8V+/z4+m8DjRmMERpeTyzlsT/86jjbd7Y+Yo4QvHuqeTSCWV+pCd5ob9Y8dIf4xzF8HqbrpeDkjfxSAmDFv9cPu5TLg300A2PTkcYVjKrniosowWU+/MQVi7/9RkZWg4khrMRY6sPI9/V2LvnZrzrHI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1576503397; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bUjPFdWQrrwxueoVHK5TSKazSywhuE9d6JMAKwbQljY=; b=ZBcVO2jzDe5sKIlk0nIFTHgQNOPpfhdUmqRSh2AOPC+/BNWN9G9wOqJXYTO6KxPhfvKDOWmIAlAC8/qpStOUpjJWQ2nilycggmG8dqlcN/ib0a6VOf5wKYJsAe2xXHcnuVGCaUZ9WtWYTx5cWJGdM2cMT3l9cSfhFqiPiGwWsQI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 157650339744253.047392465071425; Mon, 16 Dec 2019 05:36:37 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-123-yyDzSTZSOFmpMai2SnKHXw-1; Mon, 16 Dec 2019 08:36:34 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BD81A104ED3D; Mon, 16 Dec 2019 13:36:28 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8D6AA5C1D6; Mon, 16 Dec 2019 13:36:28 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4BD98ADAAB; Mon, 16 Dec 2019 13:36:28 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xBGDaQP5007558 for ; Mon, 16 Dec 2019 08:36:27 -0500 Received: by smtp.corp.redhat.com (Postfix) id C81542063FF5; Mon, 16 Dec 2019 13:36:26 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C33D92026D68 for ; Mon, 16 Dec 2019 13:36:26 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A73AE85A333 for ; Mon, 16 Dec 2019 13:36:26 +0000 (UTC) Received: from mail-qv1-f66.google.com (mail-qv1-f66.google.com [209.85.219.66]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-219-hWcZIPk9OH2KiPXhZ_5k6w-1; Mon, 16 Dec 2019 08:36:23 -0500 Received: by mail-qv1-f66.google.com with SMTP id x1so1386376qvr.8; Mon, 16 Dec 2019 05:36:22 -0800 (PST) Received: from rekt.ibmuc.com ([2804:431:c7c6:2f19:a73a:b6c3:c24a:90f3]) by smtp.gmail.com with ESMTPSA id r12sm2414002qkm.94.2019.12.16.05.36.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2019 05:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576503396; h=from:from:sender:sender: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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=bUjPFdWQrrwxueoVHK5TSKazSywhuE9d6JMAKwbQljY=; b=MjCeujhKZCZkfH4UEtAMc6cC7fw+JqiPWg9WtgAVCwJntPWv4ScgNSovbLLgvMrxmZjdSV I2rGIju2762RRcoTM7ZNdiguqqfmiAYLe7UccfSm8Z2ZCAW/6yDwMFtdAaBPeS7/18kbeY idvSgUAMiFJrYutTMh9Hu376fbQIxeQ= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=tigpZ7TRbDUY1H9PSts/IrB7bb+O+VLwwyKhCEzkx2U=; b=Uf0aRV60E9aLTeJvc+04VoiDrHO2smV++5TwiZC7P0gtD1hXqtwfHllqprkMttlgCJ sy0VLP8lJsCrSFNKeG+vDFmx9hPKlXn8YhyXNMfvT+bp3kDzifSz7BOOKawCwSLxygE5 3kczdLOs0Os0JPjmI4NuFzNYzDXd4DPwoFjexvxnHEm93EfnsOk/QXjIB3R6c+hbuClH GMmOseiQaRCfJ+HiQKCVONtBPpmZTt4roDNYzDJAP5chPuDZaw8eV2Hm2s1perjR+KWI Lc2OEuAzLFnii7Mf5pQJjlKndZ9zJaQwCewyCctH0OukEQeaUm6rgYIpVRknJnNbRd02 DycQ== X-Gm-Message-State: APjAAAXlcyDVHes3m4WRTBSC0bLc8gGXO0sIqN3MRS1EJMPsYg4nNA40 oi0T+oDf5RVSuVYYlTCDOlqrVjIX X-Google-Smtp-Source: APXvYqyVp4XjKpYv6yWSuM9PhA8YpDrLIcF+ck36jPJppqusY5C12dUMe7s4IQxbFBjiSoW1JFUWIw== X-Received: by 2002:ad4:4c42:: with SMTP id cs2mr26637448qvb.198.1576503382130; Mon, 16 Dec 2019 05:36:22 -0800 (PST) From: Daniel Henrique Barboza To: libvir-list@redhat.com Date: Mon, 16 Dec 2019 10:36:05 -0300 Message-Id: <20191216133607.3055202-4-danielhb413@gmail.com> In-Reply-To: <20191216133607.3055202-1-danielhb413@gmail.com> References: <20191216133607.3055202-1-danielhb413@gmail.com> MIME-Version: 1.0 X-MC-Unique: hWcZIPk9OH2KiPXhZ_5k6w-1 X-MC-Unique: yyDzSTZSOFmpMai2SnKHXw-1 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id xBGDaQP5007558 X-loop: libvir-list@redhat.com Cc: Daniel Henrique Barboza , laine@laine.org, darkstego@gmail.com, crobinso@redhat.com Subject: [libvirt] [PATCH v4 3/5] virhostdev.c: check all IOMMU devs in virHostdevPreparePCIDevices X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" virHostdevPreparePCIDevices verifies if a PCI hostdev "A" is in use by another domain by checking the 'activePCIHostdevs' list. It also verifies if other domains are using any other hostdev that belongs to the same IOMMU of "A". This is not enough to cover all the cases. Suppose a PCI hostdev "B" that shares the IOMMU with "A". We are currently able to abort guest launch if "B" is being used by another domain, but if "B" is not being used by any domain, we'll proceed guest execution. What happens then is: if "A" is being used in managed mode, the guest will fail to launch because Libvirt didn't detach "B" from the host. If "A" is being used in un-managed mode, the guest might fail or succeed to launch depending on whether both "A" and "B" were detached manually prior to guest start. Libvirt is now able to deal with these scenarios in a homogeneous fashion, providing the same behavior for both managed and un-managed mode while allowing parcial assignment for both cases as well. This patch changes virHostdevPreparePCIDevices to check all the PCI hostdevs of the domain against all the PCI hostdevs of the IOMMU, failing to launch if the domain does not contain all the PCI hostdevs declared, in both managed and un-managed cases. After this patch, any existing domains that were using PCI hostdevs with un-managed mode, and were getting away with partial assignment without declaring all the IOMMU PCI hostdevs in the domain XML, will be forced to do so. The missing PCI hostdevs can be declared with "
" to keep them invisible to the guest, making the guest oblivious of this change. This is an annoyance for such domains, but the payoff is more consistency of behavior between managed and un-managed mode and more clarity on the domain XML, forcing the admin to declare all PCI hostdevs of the IOMMU and knowing exactly what will be detached from the host. This is an example of this newly added error condition when the domain does not declare all the PCI hostdevs of the same IOMMU: $ sudo ./run tools/virsh start multipci-coldplug-partial-fail error: Failed to start domain multipci-coldplug-partial-fail error: Requested operation is not valid: All devices of the same IOMMU grou= p 1 of the PCI device 0001:09:00.0 must belong to domain multipci-coldplug-partial= -fail $ Signed-off-by: Daniel Henrique Barboza --- src/util/virhostdev.c | 64 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c index 39e6b8f49f..2e7885112f 100644 --- a/src/util/virhostdev.c +++ b/src/util/virhostdev.c @@ -56,6 +56,13 @@ struct virHostdevIsPCINodeDeviceUsedData { bool usesVFIO; }; =20 +struct virHostdevIsAllIOMMUGroupUsedData { + virPCIDeviceListPtr pcidevs; + const char *domainName; + const char *deviceName; + int iommuGroup; +}; + /* This module makes heavy use of bookkeeping lists contained inside a * virHostdevManager instance to keep track of the devices' status. To make * it easy to spot potential ownership errors when moving devices from one @@ -111,6 +118,27 @@ static int virHostdevIsPCINodeDeviceUsed(virPCIDeviceA= ddressPtr devAddr, void *o return 0; } =20 +static int virHostdevIsAllIOMMUGroupUsed(virPCIDeviceAddressPtr devAddr, v= oid *opaque) +{ + struct virHostdevIsAllIOMMUGroupUsedData *helperData =3D opaque; + virPCIDevicePtr actual; + + actual =3D virPCIDeviceListFindByIDs(helperData->pcidevs, + devAddr->domain, devAddr->bus, + devAddr->slot, devAddr->function); + if (actual) { + return 0; + } else { + virReportError(VIR_ERR_OPERATION_INVALID, + _("All devices of the same IOMMU group %d of " + "the PCI device %s must belong to domain %s"), + helperData->iommuGroup, + helperData->deviceName, + helperData->domainName); + return -1; + } +} + static int virHostdevManagerOnceInit(void) { if (!VIR_CLASS_NEW(virHostdevManager, virClassForObject())) @@ -724,9 +752,10 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr, unsigned int flags) { g_autoptr(virPCIDeviceList) pcidevs =3D NULL; + g_autofree unsigned int *searchedIOMMUs =3D NULL; int last_processed_hostdev_vf =3D -1; - size_t i; - int ret =3D -1; + size_t i, j; + int ret =3D -1, nSearchedIOMMUs =3D 0; virPCIDeviceAddressPtr devAddr =3D NULL; =20 if (!nhostdevs) @@ -735,6 +764,8 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr, if (!(pcidevs =3D virHostdevGetPCIHostDeviceList(hostdevs, nhostdevs))) return -1; =20 + searchedIOMMUs =3D g_new0(unsigned int, virPCIDeviceListCount(pcidevs)= ); + virObjectLock(mgr->activePCIHostdevs); virObjectLock(mgr->inactivePCIHostdevs); =20 @@ -778,13 +809,32 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr, goto cleanup; =20 /* VFIO devices belonging to same IOMMU group can't be - * shared across guests. Check if that's the case. */ + * shared across guests, and all of the must belong to the + * same domain. If a device isn't going to be assigned + * (flag unassigned is true) the guest will not use it, but + * the actual device must be detached together from the host + * anyway. */ if (usesVFIO) { - data.usesVFIO =3D true; - if (virPCIDeviceAddressIOMMUGroupIterate(devAddr, - virHostdevIsPCINodeDe= viceUsed, - &data) < 0) + int devIOMMUGroup =3D virPCIDeviceAddressGetIOMMUGroupNum(devA= ddr); + struct virHostdevIsAllIOMMUGroupUsedData helper =3D { + pcidevs, dom_name, virPCIDeviceGetName(pci), devIOMMUGroup= }; + bool alreadySearched =3D false; + + for (j =3D 0; j < nSearchedIOMMUs; j++) { + if (devIOMMUGroup =3D=3D searchedIOMMUs[j]) { + alreadySearched =3D true; + break; + } + } + + if (alreadySearched) + continue; + + if (virPCIDeviceAddressIOMMUGroupIterate( + devAddr, virHostdevIsAllIOMMUGroupUsed, &helper) < 0) goto cleanup; + + searchedIOMMUs[nSearchedIOMMUs++] =3D devIOMMUGroup; } } =20 --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list