From nobody Tue Feb 10 07:40:11 2026 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=libvir-list-bounces@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=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1616777365; cv=none; d=zohomail.com; s=zohoarc; b=i6Gu9al1co7YQ70FNZq64g5xOrl1LutwN3zvM5AukJHQl90hhF69C4pttBxtrq+FlmwFPTPyWL800wpUxkTMgiFt2mGTG8DiryoeD4L67ueGJfAnX1ypbAtT+5AGMX4NNijgD/daPWf3JnxA2D8TOY/fujg+uYVnBNrCxJeHtiw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1616777365; 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=oC09Eskm6srJv6p9/XzJ/xmFQWTWEvVrrEJVEgedaRM=; b=mQpUVjCakyrI1dipWEhoqPG5w/vaKQTUCtZeoSZ0QNgu7ZR6RVdyTWKcFFXiVH+qjt//XJzlV/NNYldEry7CViv9vkDMkQ69fOedQRZ6VJz+1HK/qGRWC5JkwMXRBmdR6ZuDugoJhxZTFK/0g5WIJZuuD+QLvo1eFqRZSZjuXa8= 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=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: 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 161677736544340.21759542135783; Fri, 26 Mar 2021 09:49:25 -0700 (PDT) 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-435-ML8G23hLOVKGzU_Cp51gVA-1; Fri, 26 Mar 2021 12:49:21 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id B3C5484B9BC; Fri, 26 Mar 2021 16:49:14 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8CA5A607D5; Fri, 26 Mar 2021 16:49:14 +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 4B1CF180214F; Fri, 26 Mar 2021 16:49:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 12QGmgOQ003109 for ; Fri, 26 Mar 2021 12:48:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id DF3DC5D72E; Fri, 26 Mar 2021 16:48:42 +0000 (UTC) Received: from himantopus.redhat.com (ovpn-113-51.phx2.redhat.com [10.3.113.51]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A80455D6DC; Fri, 26 Mar 2021 16:48:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616777364; 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=oC09Eskm6srJv6p9/XzJ/xmFQWTWEvVrrEJVEgedaRM=; b=Y1VZEF915zSW7N6urJkI1Y2w3LSoAkzybENQ9mMPKoGaUQ/o6PNr61UFxEZQxJMJMeygz5 FUM1QwZHSMxBoeHQkPQdRRMviYU20VPZD6W+tz8evEglDABgfLXJ6dFNrScy3cF37gd2uP KKN9YyhqIKsr3xY/hYfSQGFHS+N37CE= X-MC-Unique: ML8G23hLOVKGzU_Cp51gVA-1 From: Jonathon Jongsma To: libvir-list@redhat.com Subject: [libvirt PATCH v6 27/30] nodedev: fix hang when destroying an mdev in use Date: Fri, 26 Mar 2021 11:48:23 -0500 Message-Id: <20210326164826.1720062-28-jjongsma@redhat.com> In-Reply-To: <20210326164826.1720062-1-jjongsma@redhat.com> References: <20210326164826.1720062-1-jjongsma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: eskultet@redhat.com 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.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Calling `mdevctl stop` for a mediated device that is in use by an active domain will block until that vm exits (or the vm closes the device). Since the nodedev driver cannot query the hypervisor driver to see whether any active domains are using the device, we resort to a workaround that relies on the fact that a vfio group can only be opened by one user at a time. If we get an EBUSY error when attempting to open the group file, we assume the device is in use and refuse to try to destroy that device. Signed-off-by: Jonathon Jongsma Reviewed-by: Erik Skultety --- src/node_device/node_device_driver.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index b1b3217afb..31322e3afb 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -1177,8 +1177,27 @@ nodeDeviceDestroy(virNodeDevicePtr device) =20 ret =3D 0; } else if (nodeDeviceHasCapability(def, VIR_NODE_DEV_CAP_MDEV)) { + /* If this mediated device is in use by a vm, attempting to stop it + * will block until the vm closes the device. The nodedev driver + * cannot query the hypervisor driver to determine whether the dev= ice + * is in use by any active domains, since that would introduce cir= cular + * dependencies between daemons and add a risk of deadlocks. So we= need + * to resort to a workaround. vfio only allows the group for a de= vice + * to be opened by one user at a time. So if we get EBUSY when ope= ning + * the group, we infer that the device is in use and therefore we + * shouldn't try to remove the device. */ + g_autofree char *vfiogroup =3D + virMediatedDeviceGetIOMMUGroupDev(def->caps->data.mdev.uuid); + VIR_AUTOCLOSE fd =3D open(vfiogroup, O_RDONLY); g_autofree char *errmsg =3D NULL; =20 + if (fd < 0 && errno =3D=3D EBUSY) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to destroy '%s': device in use"), + def->name); + goto cleanup; + } + if (virMdevctlStop(def, &errmsg) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to destroy '%s': %s"), def->name, --=20 2.26.3