From nobody Sat Apr 27 05:03:31 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.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 63.128.21.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=1610037277; cv=none; d=zohomail.com; s=zohoarc; b=KFQlfyIBAI5IDgY2xfSvzv2Zo5sPjDylI2QLudQf34JR8b2SNOKUrytIEtIDjqrjsG8GTFXFC7mlV3+lzchhdyWWiFZfGSLz8UNxkx4gm1Fh2T6Ak8bmL5FloEncYlRdigjiunwY5AKNOSuR+VGjRaeQJSabGBuFYiBifBUftao= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610037277; 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=q9fG1NpDIO93chK9X0WvSEBvAa8wcpFujX1n88f1bV8=; b=Q8OD7TBRuar+s8UrfOSiBg4SDMXZOQtCJtEgeHtJIxnP3t2fu6X2cu0P8L7zZ2CnINd5PPzHOIVTgjFHJR9lcihMFstoKZ42cDCgTcXCoVqwlQllM6FBezMvzPGXu+bSqRDD6gPx6RkMH9Q8pX05uLeLQROXDj7bHBaDT0nJGfo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.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 [63.128.21.124]) by mx.zohomail.com with SMTPS id 1610037277929942.3625606242668; Thu, 7 Jan 2021 08:34: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-78-j1T9NHsbPLy4_t5rQst2rg-1; Thu, 07 Jan 2021 11:34:34 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 07E4E18C89DC; Thu, 7 Jan 2021 16:34:29 +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 B44DE8416C; Thu, 7 Jan 2021 16:34: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 72C994A7C6; Thu, 7 Jan 2021 16:34:28 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 107GYDPJ017306 for ; Thu, 7 Jan 2021 11:34:13 -0500 Received: by smtp.corp.redhat.com (Postfix) id 8EDF010013BD; Thu, 7 Jan 2021 16:34:13 +0000 (UTC) Received: from nautilus.local (unknown [10.40.192.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id D537A1001E73; Thu, 7 Jan 2021 16:34:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610037276; 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=q9fG1NpDIO93chK9X0WvSEBvAa8wcpFujX1n88f1bV8=; b=QNCoW9AgeQeZVmC3pc2glv0mCCQyvL9sCaCzGnA8a4MQjd8QMEfEezuW0eAOY0KI7dEoDj oy7ivcGlJoSH4ZDaAa+aYdp7FVO9byTlS1f2oniWjFrvRFslOebVmgs6q3o3mZwiBrG9HE 5rpMOFYs48TpjzvJBU7OXV/FVKZTeKk= X-MC-Unique: j1T9NHsbPLy4_t5rQst2rg-1 From: Erik Skultety To: libvir-list@redhat.com Subject: [libvirt PATCH 1/2] hostdev: Update mdev pointer reference after checking device type Date: Thu, 7 Jan 2021 17:34:06 +0100 Message-Id: <6b311456e9716194e1d9f7e1fea6ae243736dfe0.1610037231.git.eskultet@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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.84 on 10.5.11.23 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" We set the pointer to some garbage packed structure data without knowing whether it actually we were handling the type of device we expected to be handling. On its own, this was harmless, because we'd never use the pointer as we'd skip the device if it were not the expected type. However, it's better to make the logic even more explicit - we first check the device and only when we're sure we have the expected type we then update the pointer shortcut. Signed-off-by: Erik Skultety Reviewed-by: J=C3=A1n Tomko --- src/hypervisor/virhostdev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hypervisor/virhostdev.c b/src/hypervisor/virhostdev.c index 120187b07a..aa3fc8738f 100644 --- a/src/hypervisor/virhostdev.c +++ b/src/hypervisor/virhostdev.c @@ -1980,11 +1980,11 @@ virHostdevReAttachMediatedDevices(virHostdevManager= Ptr mgr, virDomainHostdevSubsysMediatedDevPtr mdevsrc; virDomainHostdevDefPtr hostdev =3D hostdevs[i]; =20 - mdevsrc =3D &hostdev->source.subsys.u.mdev; - if (!virHostdevIsMdevDevice(hostdev)) continue; =20 + mdevsrc =3D &hostdev->source.subsys.u.mdev; + if (!(mdev =3D virMediatedDeviceNew(mdevsrc->uuidstr, mdevsrc->model))) continue; --=20 2.29.2 From nobody Sat Apr 27 05:03:31 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=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=1610037282; cv=none; d=zohomail.com; s=zohoarc; b=WXVYVBIfsE2kZA7nqLiO3cn/zUwD8jb3XvDUc1dWm/xPQ5uqbfQCngRWq84DvO6wvmUZnX9w9oKUwfT7DWX8wYZ0GAvY9xbleBTWM2ZH7zWtqXCMXi0aSGenTVc6IAS4c3OpJtnBCxvUNGPunUZnIYXeSjgBMJ/kDBtMp8Cl2cs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1610037282; 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=XiYwXhPvwMsFjnGMgqqohendjkqsRkYWIHTzk3w2A6s=; b=XHoDv2wC0lq9nxTYJmiOvtzAA2eBb6VOlj+sjTVWSSuFqmQsLnzZb1eTvBMrcJxUUcWrUeLuLtogkgjJ+y09RczmTnwFr6saFRdkhNz4GfhHY0iBhOK/LRN87dosCYRVCeUmU+l4og8/W4wucun7WFWGPKs5QJbl7nApRCakHiw= 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 16100372824672.6833488232807667; Thu, 7 Jan 2021 08:34:42 -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-45-G2pPWrdrOnWVQpE0Clzh4g-1; Thu, 07 Jan 2021 11:34:38 -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 8AF4C18C89D9; Thu, 7 Jan 2021 16:34:32 +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 6522374AC4; Thu, 7 Jan 2021 16:34:32 +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 2647F4EA6C; Thu, 7 Jan 2021 16:34:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 107GYEHw017314 for ; Thu, 7 Jan 2021 11:34:14 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9DFB510023AD; Thu, 7 Jan 2021 16:34:14 +0000 (UTC) Received: from nautilus.local (unknown [10.40.192.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id E3FB310023AC; Thu, 7 Jan 2021 16:34:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610037281; 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=XiYwXhPvwMsFjnGMgqqohendjkqsRkYWIHTzk3w2A6s=; b=ZjuCZ3jo/Lgws/UXBCdgwbWNSUFQ3Ybn0mocXqWUs2ZIHLPpsGfOqA8r8fE5LQ0vjw0WIU avRGH1HGm3LkkWZSjK0MS2eIJJTp9vmzMz3SZEFNsyVCr0/ICOmeiHbqj126trVBH73CBT zfurOHikKQGAOGbmSxvdZ6DiG8IU0HI= X-MC-Unique: G2pPWrdrOnWVQpE0Clzh4g-1 From: Erik Skultety To: libvir-list@redhat.com Subject: [libvirt PATCH 2/2] hostdev: mdev: Lookup mdevs by sysfs path rather than mdev struct Date: Thu, 7 Jan 2021 17:34:07 +0100 Message-Id: <29500663d3e354639e71150f1bf50d947796a1e5.1610037231.git.eskultet@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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.16 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" The lookup didn't do anything apart from comparing the sysfs paths anyway since that's what makes each mdev unique. The most ridiculous usage of the old logic was in virHostdevReAttachMediatedDevices where in order to drop an mdev hostdev from the list of active devices we first had to create a new mdev and use it in the lookup call. Why couldn't we have used the hostdev directly? Because the hostdev and mdev structures are incompatible. The way mdevs are currently removed is via a write to a specific sysfs attribute. If you do it while the machine which has the mdev assigned is running, the write call may block (this should return a write error instead and it is likely a bug in the vendor driver) until the device is no longer bound to vfio which is when the QEMU process exits. The interesting part here comes afterwards when we're cleaning up and call virHostdevReAttachMediatedDevices. The domain doesn't exist anymore, so the list of active hostdevs needs to be updated and the respective hostdevs removed from the list, but remember we had to create an mdev object in the memory in order to find it in the list first which will fail because the write to sysfs had already removed the mdev instance from the host system. And so the next time you try to start the same domain you'll get: "Requested operation is not valid: mediated device is in use by driver QEMU, domain " Signed-off-by: Erik Skultety Reviewed-by: J=C3=A1n Tomko --- src/hypervisor/virhostdev.c | 10 ++++------ src/util/virmdev.c | 16 ++++++++-------- src/util/virmdev.h | 4 ++-- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/hypervisor/virhostdev.c b/src/hypervisor/virhostdev.c index aa3fc8738f..ea04382d0d 100644 --- a/src/hypervisor/virhostdev.c +++ b/src/hypervisor/virhostdev.c @@ -1975,7 +1975,7 @@ virHostdevReAttachMediatedDevices(virHostdevManagerPt= r mgr, =20 virObjectLock(mgr->activeMediatedHostdevs); for (i =3D 0; i < nhostdevs; i++) { - g_autoptr(virMediatedDevice) mdev =3D NULL; + g_autofree char * sysfspath =3D NULL; virMediatedDevicePtr tmp; virDomainHostdevSubsysMediatedDevPtr mdevsrc; virDomainHostdevDefPtr hostdev =3D hostdevs[i]; @@ -1984,14 +1984,12 @@ virHostdevReAttachMediatedDevices(virHostdevManager= Ptr mgr, continue; =20 mdevsrc =3D &hostdev->source.subsys.u.mdev; - - if (!(mdev =3D virMediatedDeviceNew(mdevsrc->uuidstr, - mdevsrc->model))) - continue; + sysfspath =3D virMediatedDeviceGetSysfsPath(mdevsrc->uuidstr); =20 /* Remove from the list only mdevs assigned to @drv_name/@dom_name= */ =20 - tmp =3D virMediatedDeviceListFind(mgr->activeMediatedHostdevs, mde= v); + tmp =3D virMediatedDeviceListFind(mgr->activeMediatedHostdevs, + sysfspath); =20 /* skip inactive devices */ if (!tmp) diff --git a/src/util/virmdev.c b/src/util/virmdev.c index b6df353d56..fc27e9e45d 100644 --- a/src/util/virmdev.c +++ b/src/util/virmdev.c @@ -308,7 +308,7 @@ int virMediatedDeviceListAdd(virMediatedDeviceListPtr list, virMediatedDevicePtr *dev) { - if (virMediatedDeviceListFind(list, *dev)) { + if (virMediatedDeviceListFind(list, (*dev)->path)) { virReportError(VIR_ERR_INTERNAL_ERROR, _("device %s is already in use"), (*dev)->path); return -1; @@ -354,7 +354,7 @@ virMediatedDevicePtr virMediatedDeviceListSteal(virMediatedDeviceListPtr list, virMediatedDevicePtr dev) { - int idx =3D virMediatedDeviceListFindIndex(list, dev); + int idx =3D virMediatedDeviceListFindIndex(list, dev->path); =20 return virMediatedDeviceListStealIndex(list, idx); } @@ -370,13 +370,13 @@ virMediatedDeviceListDel(virMediatedDeviceListPtr lis= t, =20 int virMediatedDeviceListFindIndex(virMediatedDeviceListPtr list, - virMediatedDevicePtr dev) + const char *sysfspath) { size_t i; =20 for (i =3D 0; i < list->count; i++) { - virMediatedDevicePtr other =3D list->devs[i]; - if (STREQ(other->path, dev->path)) + virMediatedDevicePtr dev =3D list->devs[i]; + if (STREQ(sysfspath, dev->path)) return i; } return -1; @@ -385,11 +385,11 @@ virMediatedDeviceListFindIndex(virMediatedDeviceListP= tr list, =20 virMediatedDevicePtr virMediatedDeviceListFind(virMediatedDeviceListPtr list, - virMediatedDevicePtr dev) + const char *sysfspath) { int idx; =20 - if ((idx =3D virMediatedDeviceListFindIndex(list, dev)) >=3D 0) + if ((idx =3D virMediatedDeviceListFindIndex(list, sysfspath)) >=3D 0) return list->devs[idx]; else return NULL; @@ -403,7 +403,7 @@ virMediatedDeviceIsUsed(virMediatedDevicePtr dev, const char *drvname, *domname; virMediatedDevicePtr tmp =3D NULL; =20 - if ((tmp =3D virMediatedDeviceListFind(list, dev))) { + if ((tmp =3D virMediatedDeviceListFind(list, dev->path))) { virMediatedDeviceGetUsedBy(tmp, &drvname, &domname); virReportError(VIR_ERR_OPERATION_INVALID, _("mediated device %s is in use by " diff --git a/src/util/virmdev.h b/src/util/virmdev.h index b6563a94fc..e96e5bb8c6 100644 --- a/src/util/virmdev.h +++ b/src/util/virmdev.h @@ -130,11 +130,11 @@ virMediatedDeviceListDel(virMediatedDeviceListPtr lis= t, =20 virMediatedDevicePtr virMediatedDeviceListFind(virMediatedDeviceListPtr list, - virMediatedDevicePtr dev); + const char *sysfspath); =20 int virMediatedDeviceListFindIndex(virMediatedDeviceListPtr list, - virMediatedDevicePtr dev); + const char *sysfspath); =20 int virMediatedDeviceListMarkDevices(virMediatedDeviceListPtr dst, --=20 2.29.2