From nobody Sat Feb 7 21:11:08 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 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=1563903374; cv=none; d=zoho.com; s=zohoarc; b=EDhuE85dUlIwVs3pfESejEOf7BiB9bpu9cv5AH10iFSg1EG9U7M1ohbbVBLshAYrXKwaEb3OFdqIu8TjMsyXr5g6z1zKRjZwliY6LWVug9zwMLgqwGOVkr7CspaYWQTXQ8wT1uQ9KMfq3Ql1rCQQQk4ZutIPkwaQCXgNEAuxnis= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563903374; 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:ARC-Authentication-Results; bh=gCD6ocaIzOFLUbJyBb3zJq7qEK6nfKUuhIE1EVJQ/QQ=; b=P+0YbwNnO75nzQyXte55Jvo5aZ8Q9lSmRWDjWPSZJbWPJCID+QX7G3yAmk97bbldgtiyau6VuWl7HuL8y6fGsatGRvGyU69TZK5d6Kzxsa08igzGoiIoKI0GlaZyZh5irPfahkEBxftQsyplANnyZXuzynAi23IyRc79Enw7sSQ= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1563903374926753.0269761081461; Tue, 23 Jul 2019 10:36:14 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BEDCB30A7C67; Tue, 23 Jul 2019 17:36:13 +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 9471E5DA2E; Tue, 23 Jul 2019 17:36:13 +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 59DC819734; Tue, 23 Jul 2019 17:36:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x6NHZuVL008619 for ; Tue, 23 Jul 2019 13:35:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5FD2760BF7; Tue, 23 Jul 2019 17:35:56 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 56FE260BEC; Tue, 23 Jul 2019 17:35:54 +0000 (UTC) Received: from mail-vs1-f66.google.com (mail-vs1-f66.google.com [209.85.217.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 47A1D8553A; Tue, 23 Jul 2019 17:35:53 +0000 (UTC) Received: by mail-vs1-f66.google.com with SMTP id a186so27772355vsd.7; Tue, 23 Jul 2019 10:35:53 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c6:30ed:e8ca:caf:6199:5dc7]) by smtp.gmail.com with ESMTPSA id u5sm42910619uah.0.2019.07.23.10.35.51 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 23 Jul 2019 10:35:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=E8+X1JQbJltAn54zdDoo4ifjwVYzS8XCAIrFLF6m7Q4=; b=F2C+jZYZ0cwa4sVP7v/GDGest5iWf9u12IJJ05IPZJIhHmHr2+WT7nO7D7uu1eVQc4 BhN3USSQZTJRWbLgB4XeYx0XImMiYT5p2l1FQnX9WfUr/jUlozZeK0Dp+6teLTN3L5Gd tKUyyhQDZP5PvKq/Ic0Opkmj6xph1VsgMR234+y1MgWUMAnJ+ogdsyCdw9NNo4A+RtS/ u5c3vNBONhdEUGv6sEkZ5IwzTyk9ml5f5fKiunuwzaAg89vpgM+Mg03jzy+IfmBGP4b9 twIw6+Qi2l0XqRh1naHo0eebZcqybKq/mR/EyVDTeXlPtIl2oOwjY+BFFqFScI843YJa IzQw== 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=E8+X1JQbJltAn54zdDoo4ifjwVYzS8XCAIrFLF6m7Q4=; b=cp6bz0CanVhf/nsxOqMZvTMTuBdVI9DJ2s4ULcO4mMyrlf3ZzBii2HH4TfuSzyUGBW LegeeD3Uw2WYo0umukNxxa/UbNCqIREAUfJgENVJF6YBsG6UDB+38Q4ZsAPCfPkHhWax 3nSxeM5MJx6mK8Wt5ARofa7hELTotiMPeuNPpGUmz+B5n8Y5kPTns7LHAHj1Kjn69Xyz /fE7j92wS64GvU+eQ+vYdlccguP8ulOhytZnDg9KKKpp0rdy6eU8WNlY6c2g1ZO40hwJ hWaeH/D0m8bmGgtAjXSBAaaCIleUGzqSrj58sm+qknLztiBc+OHUYUd2mpo03S+MKkZl LILA== X-Gm-Message-State: APjAAAVHoW6x40vWlqvx74wZpRe4o+MdaUU6zA3+KRW/R01qP44xkBmG l64lbtv862GnCQbGiY60791w1+WBP60= X-Google-Smtp-Source: APXvYqyhZ894JPogGywD/RUB9kGoItB/zA57aAQIXyT7G1QbErFgZ0zOVaEqrU2KsZlzT9Jkz0SNKA== X-Received: by 2002:a67:68d4:: with SMTP id d203mr51760915vsc.28.1563903352410; Tue, 23 Jul 2019 10:35:52 -0700 (PDT) From: Daniel Henrique Barboza To: libvir-list@redhat.com Date: Tue, 23 Jul 2019 14:35:40 -0300 Message-Id: <20190723173541.26501-3-danielhb413@gmail.com> In-Reply-To: <20190723173541.26501-1-danielhb413@gmail.com> References: <20190723173541.26501-1-danielhb413@gmail.com> MIME-Version: 1.0 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 23 Jul 2019 17:35:53 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 23 Jul 2019 17:35:53 +0000 (UTC) for IP:'209.85.217.66' DOMAIN:'mail-vs1-f66.google.com' HELO:'mail-vs1-f66.google.com' FROM:'danielhb413@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.151 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS) 209.85.217.66 mail-vs1-f66.google.com 209.85.217.66 mail-vs1-f66.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: mprivozn@redhat.com, Daniel Henrique Barboza Subject: [libvirt] [PATCH v2 2/3] virhostdev: remove virHostdevReattachPCIDevice 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: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Tue, 23 Jul 2019 17:36:14 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" virHostdevReattachPCIDevice() is a static that simply does a wait loop with virPCIDeviceWaitForCleanup() before calling virPCIDeviceReattach(). This loop traces back to commit d1e5676c0d, aiming to solve a race condition between Libvirt returning the device back to the host and QEMU trying to access it in the meantime, which resulted in QEMU exiting on error and killing the guest. This happens because device_del is asynchronous, returning OK even if the guest didn't release the device. Commit 01abc8a1b8 moved this code to qemu_hostdev.c, 82e8dd4cf8 added the pci-stub conditional for the loop, 899b261127 moved the code to virhostdev.c where it stood until now. The intent of this wait loop is still valid: device_del is still not bullet proof into preventing the conditions that commit d1e5676c0d aimed to fix, especially when considering all the architectures we must support. However, this loop is executed only in virHostdevReattachPCIDevice(), leaving every other virPCIDeviceReattach() call prone to that error. Let's move the wait loop code to virPCIDeviceReattach(). This will: - make every reattach call safe from this race condition with the pci-stub; - allow for a bit of code cleanup (virHostdevReattachPCIDevice() can be erased, and virHostdevReAttachPCIDevices() can use virPCIDeviceReattach() directly); - make it easier to understand the overall reattach mechanisms in Libvirt, without the risk of a newcomer wondering why reattach is done slightly different in some instances. Signed-off-by: Daniel Henrique Barboza --- src/util/virhostdev.c | 40 ++++++++++------------------------------ src/util/virpci.c | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c index 4dd24a8f65..31d075a11a 100644 --- a/src/util/virhostdev.c +++ b/src/util/virhostdev.c @@ -927,33 +927,6 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr, return ret; } =20 -/* - * Pre-condition: inactivePCIHostdevs & activePCIHostdevs - * are locked - */ -static void -virHostdevReattachPCIDevice(virHostdevManagerPtr mgr, - virPCIDevicePtr actual) -{ - /* Wait for device cleanup if it is qemu/kvm */ - if (virPCIDeviceGetStubDriver(actual) =3D=3D VIR_PCI_STUB_DRIVER_KVM) { - int retries =3D 100; - while (virPCIDeviceWaitForCleanup(actual, "kvm_assigned_device") - && retries) { - usleep(100*1000); - retries--; - } - } - - VIR_DEBUG("Reattaching PCI device %s", virPCIDeviceGetName(actual)); - if (virPCIDeviceReattach(actual, mgr->activePCIHostdevs, - mgr->inactivePCIHostdevs) < 0) { - VIR_ERROR(_("Failed to re-attach PCI device: %s"), - virGetLastErrorMessage()); - virResetLastError(); - } -} - /* @oldStateDir: * For upgrade purpose: see virHostdevRestoreNetConfig */ @@ -1072,12 +1045,19 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr m= gr, virPCIDevicePtr actual; =20 /* We need to look up the actual device because that's what - * virHostdevReattachPCIDevice() expects as its argument */ + * virPCIDeviceReattach() expects as its argument */ if (!(actual =3D virPCIDeviceListFind(mgr->inactivePCIHostdevs, pc= i))) continue; =20 - if (virPCIDeviceGetManaged(actual)) - virHostdevReattachPCIDevice(mgr, actual); + if (virPCIDeviceGetManaged(actual)) { + if (virPCIDeviceReattach(actual, + mgr->activePCIHostdevs, + mgr->inactivePCIHostdevs) < 0) { + VIR_ERROR(_("Failed to re-attach PCI device: %s"), + virGetLastErrorMessage()); + virResetLastError(); + } + } else VIR_DEBUG("Not reattaching unmanaged PCI device %s", virPCIDeviceGetName(actual)); diff --git a/src/util/virpci.c b/src/util/virpci.c index 6dc0a2711c..05232888c3 100644 --- a/src/util/virpci.c +++ b/src/util/virpci.c @@ -1509,6 +1509,10 @@ virPCIDeviceDetach(virPCIDevicePtr dev, return 0; } =20 +/* + * Pre-condition: inactivePCIHostdevs & activePCIHostdevs + * are locked + */ int virPCIDeviceReattach(virPCIDevicePtr dev, virPCIDeviceListPtr activeDevs, @@ -1520,6 +1524,16 @@ virPCIDeviceReattach(virPCIDevicePtr dev, return -1; } =20 + /* Wait for device cleanup if it is qemu/kvm */ + if (virPCIDeviceGetStubDriver(dev) =3D=3D VIR_PCI_STUB_DRIVER_KVM) { + int retries =3D 100; + while (virPCIDeviceWaitForCleanup(dev, "kvm_assigned_device") + && retries) { + usleep(100*1000); + retries--; + } + } + if (virPCIDeviceUnbindFromStub(dev) < 0) return -1; =20 --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list