From nobody Mon Feb 9 20:10:39 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=1612374007; cv=none; d=zohomail.com; s=zohoarc; b=CRfgqKS62GfX5YP4HXGe7R3GVKF3FdshVk8Y1Ps9ztasYA/ytvdb4H2PB+7jiOkcEJfhHsJ+5I7MHWFHejShJW2HLWxSQO2b3qr0megcrEwkLN0ItlMUvU6rHHidWXs2vMjA4fBRBzWoJ8TIW/McUNgeLqBGBD7j+zof+8GmpQU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612374007; 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=tzWbrie3/IjmVIklRZQkMw9OxbTnGfQ17rLJB7MvM94=; b=Jg+FQqUzB4cK8ZqhIs2iwLPKQ0XcLe+oB0hD25fb0ymyX2bt+vqK/Ayo9Ri9CSfLPkL3hw+aBflino5gAMafZx+vA1gWSFNPW76VOcPQ+s5bHKXrJEf9O9yynpywJvgcoLjWVl2w9X79uc/NBPajC1PfEg6yBg7Fm1oLvYs3SQk= 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 1612374007466362.97445603688993; Wed, 3 Feb 2021 09:40:07 -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-527-OFq7xfzHODK3-pH-QPujIA-1; Wed, 03 Feb 2021 12:39:33 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9DED5100C63B; Wed, 3 Feb 2021 17:39:25 +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 7D2BC60BFA; Wed, 3 Feb 2021 17:39:25 +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 4A9BA1809C92; Wed, 3 Feb 2021 17:39:25 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 113HdHZd024309 for ; Wed, 3 Feb 2021 12:39:17 -0500 Received: by smtp.corp.redhat.com (Postfix) id 4ED515B4A1; Wed, 3 Feb 2021 17:39:17 +0000 (UTC) Received: from himantopus.redhat.com (ovpn-113-7.phx2.redhat.com [10.3.113.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 093DA722CA; Wed, 3 Feb 2021 17:39:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612374006; 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=tzWbrie3/IjmVIklRZQkMw9OxbTnGfQ17rLJB7MvM94=; b=fyd5sTvx+Xix53AEzZD9MrN3BwBlRxuAHcvfAxAEWKxPf4wYE2sgNMljFsFOhikXPrZHA9 g5f9SINFgzYHVjmEjiHZ0UYyLQAWhwmefxrjhpCkTF1x8ok5MMzWgDsLsrVuBFZA4D61ip /CktZaZzp8/HNOrMi27Q+t7+rBy742U= X-MC-Unique: OFq7xfzHODK3-pH-QPujIA-1 From: Jonathon Jongsma To: libvir-list@redhat.com Subject: [libvirt PATCH v4 09/25] nodedev: add mdevctl devices to node device list Date: Wed, 3 Feb 2021 11:38:53 -0600 Message-Id: <20210203173909.1273000-10-jjongsma@redhat.com> In-Reply-To: <20210203173909.1273000-1-jjongsma@redhat.com> References: <20210203173909.1273000-1-jjongsma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 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.12 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" At startup, query devices that are defined by 'mdevctl' and add them to the node device list. This adds a complication: we now have two potential sources of information for a node device: - udev for all devices and for activated mediated devices - mdevctl for persistent mediated devices Unfortunately, neither backend returns full information for a mediated device. For example, if a persistent mediated device in the list (with information provided from mdevctl) is 'started', that same device will now be detected by udev. If we simply overwrite the existing device definition with the new one provided by the udev backend, we will lose extra information that was provided by mdevctl (e.g. attributes, etc). To avoid this, make sure to copy the extra information into the new device definition. Signed-off-by: Jonathon Jongsma --- src/node_device/node_device_driver.c | 164 +++++++++++++++++++++++++++ src/node_device/node_device_driver.h | 6 + src/node_device/node_device_udev.c | 31 ++++- 3 files changed, 196 insertions(+), 5 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index 2778e41f97..fd57dcacc1 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -1156,3 +1156,167 @@ nodeDeviceGenerateName(virNodeDeviceDefPtr def, *(def->name + i) =3D '_'; } } + + +static int +virMdevctlListDefined(virNodeDeviceDefPtr **devs) +{ + int status; + g_autofree char *output =3D NULL; + g_autoptr(virCommand) cmd =3D nodeDeviceGetMdevctlListCommand(true, &o= utput); + + if (virCommandRun(cmd, &status) < 0) + return -1; + + if (!output) + return -1; + + return nodeDeviceParseMdevctlJSON(output, devs); +} + + +typedef struct _virMdevctlForEachData virMdevctlForEachData; +struct _virMdevctlForEachData { + int ndefs; + virNodeDeviceDefPtr *defs; +}; + + +int +nodeDeviceUpdateMediatedDevices(void) +{ + g_autofree virNodeDeviceDefPtr *defs =3D NULL; + int ndefs; + GList * tofree =3D NULL; + size_t i; + + if ((ndefs =3D virMdevctlListDefined(&defs)) < 0) { + virReportSystemError(errno, "%s", + _("failed to query mdevs from mdevctl")); + return -1; + } + + for (i =3D 0; i < ndefs; i++) { + virNodeDeviceObjPtr obj; + virObjectEventPtr event; + virNodeDeviceDefPtr def =3D defs[i]; + bool was_defined =3D false; + + def->driver =3D g_strdup("vfio_mdev"); + + if ((obj =3D virNodeDeviceObjListFindByName(driver->devs, def->nam= e))) { + bool changed; + virNodeDeviceDefPtr olddef =3D virNodeDeviceObjGetDef(obj); + + was_defined =3D virNodeDeviceObjIsPersistent(obj); + /* Active devices contain some additional information (e.g. sy= sfs + * path) that is not provided by mdevctl, so re-use the existi= ng + * definition and copy over new mdev data */ + changed =3D nodeDeviceDefCopyFromMdevctl(olddef, def); + + /* Re-use the existing definition (after merging new data from + * mdevctl), so def needs to be freed at the end of the functi= on */ + tofree =3D g_list_prepend(tofree, def); + + if (was_defined && !changed) { + /* if this device was already defined and the definition + * hasn't changed, there's nothing to do for this device */ + virNodeDeviceObjEndAPI(&obj); + continue; + } + } else { + if (!(obj =3D virNodeDeviceObjListAssignDef(driver->devs, def)= )) { + virNodeDeviceDefFree(def); + goto cleanup; + } + } + + /* all devices returned by virMdevctlListDefined() are persistent = */ + virNodeDeviceObjSetPersistent(obj, true); + + if (!was_defined) + event =3D virNodeDeviceEventLifecycleNew(def->name, + VIR_NODE_DEVICE_EVENT_D= EFINED, + 0); + else + event =3D virNodeDeviceEventUpdateNew(def->name); + + virNodeDeviceObjEndAPI(&obj); + virObjectEventStateQueue(driver->nodeDeviceEventState, event); + } + + cleanup: + g_list_free_full(tofree, (GDestroyNotify)virNodeDeviceDefFree); + + return 0; +} + + +/* returns true if any attributes were copied, else returns false */ +static bool +virMediatedDeviceAttrsCopy(virNodeDevCapMdevPtr dst, + virNodeDevCapMdevPtr src) +{ + bool ret =3D false; + size_t i; + + if (src->nattributes !=3D dst->nattributes) { + ret =3D true; + for (i =3D 0; i < dst->nattributes; i++) + virMediatedDeviceAttrFree(dst->attributes[i]); + g_free(dst->attributes); + + dst->nattributes =3D src->nattributes; + dst->attributes =3D g_new0(virMediatedDeviceAttrPtr, + src->nattributes); + for (i =3D 0; i < dst->nattributes; i++) + dst->attributes[i] =3D virMediatedDeviceAttrNew(); + } + + for (i =3D 0; i < src->nattributes; i++) { + if (STRNEQ_NULLABLE(src->attributes[i]->name, + dst->attributes[i]->name)) { + ret =3D true; + dst->attributes[i]->name =3D + g_strdup(src->attributes[i]->name); + } + if (STRNEQ_NULLABLE(src->attributes[i]->value, + dst->attributes[i]->value)) { + ret =3D true; + dst->attributes[i]->value =3D + g_strdup(src->attributes[i]->value); + } + } + + return ret; +} + + +/* A mediated device definitions from mdevctl contains additional info tha= t is + * not available from udev. Transfer this data to the new definition. + * Returns true if anything was copied, else returns false */ +bool +nodeDeviceDefCopyFromMdevctl(virNodeDeviceDefPtr dst, + virNodeDeviceDefPtr src) +{ + bool ret =3D false; + virNodeDevCapMdevPtr srcmdev =3D &src->caps->data.mdev; + virNodeDevCapMdevPtr dstmdev =3D &dst->caps->data.mdev; + + if (STRNEQ_NULLABLE(dstmdev->type, srcmdev->type)) { + ret =3D true; + g_free(dstmdev->type); + dstmdev->type =3D g_strdup(srcmdev->type); + } + + if (STRNEQ_NULLABLE(dstmdev->uuid, srcmdev->uuid)) { + ret =3D true; + g_free(dstmdev->uuid); + dstmdev->uuid =3D g_strdup(srcmdev->uuid); + } + + if (virMediatedDeviceAttrsCopy(dstmdev, srcmdev)) + ret =3D true; + + return ret; +} diff --git a/src/node_device/node_device_driver.h b/src/node_device/node_de= vice_driver.h index 80ac7c5320..d8837e4ef8 100644 --- a/src/node_device/node_device_driver.h +++ b/src/node_device/node_device_driver.h @@ -126,8 +126,14 @@ int nodeDeviceParseMdevctlJSON(const char *jsonstring, virNodeDeviceDefPtr **devs); =20 +int +nodeDeviceUpdateMediatedDevices(void); + void nodeDeviceGenerateName(virNodeDeviceDefPtr def, const char *subsystem, const char *sysname, const char *s); + +bool nodeDeviceDefCopyFromMdevctl(virNodeDeviceDefPtr dst, + virNodeDeviceDefPtr src); diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_devi= ce_udev.c index 5d1a192411..038941ec51 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -1416,9 +1416,17 @@ udevRemoveOneDeviceSysPath(const char *path) VIR_NODE_DEVICE_EVENT_DELETED, 0); =20 - VIR_DEBUG("Removing device '%s' with sysfs path '%s'", - def->name, path); - virNodeDeviceObjListRemove(driver->devs, obj); + /* If the device is a mediated device that has been 'stopped', it may = still + * be defined by mdevctl and can therefore be started again. Don't dro= p it + * from the list of node devices */ + if (virNodeDeviceObjIsPersistent(obj)) { + VIR_FREE(def->sysfs_path); + virNodeDeviceObjSetActive(obj, false); + } else { + VIR_DEBUG("Removing device '%s' with sysfs path '%s'", + def->name, path); + virNodeDeviceObjListRemove(driver->devs, obj); + } virNodeDeviceObjEndAPI(&obj); =20 virObjectEventStateQueue(driver->nodeDeviceEventState, event); @@ -1476,7 +1484,6 @@ udevSetParent(struct udev_device *device, return 0; } =20 - static int udevAddOneDevice(struct udev_device *device) { @@ -1486,6 +1493,7 @@ udevAddOneDevice(struct udev_device *device) virObjectEventPtr event =3D NULL; bool new_device =3D true; int ret =3D -1; + bool was_persistent =3D false; =20 def =3D g_new0(virNodeDeviceDef, 1); =20 @@ -1509,14 +1517,23 @@ udevAddOneDevice(struct udev_device *device) goto cleanup; =20 if ((obj =3D virNodeDeviceObjListFindByName(driver->devs, def->name)))= { + objdef =3D virNodeDeviceObjGetDef(obj); + + if (def->caps->data.type =3D=3D VIR_NODE_DEV_CAP_MDEV) + nodeDeviceDefCopyFromMdevctl(def, objdef); + was_persistent =3D virNodeDeviceObjIsPersistent(obj); + /* If the device was defined by mdevctl and was never instantiated= , it + * won't have a sysfs path. We need to emit a CREATED event... */ + new_device =3D (objdef->sysfs_path =3D=3D NULL); + virNodeDeviceObjEndAPI(&obj); - new_device =3D false; } =20 /* If this is a device change, the old definition will be freed * and the current definition will take its place. */ if (!(obj =3D virNodeDeviceObjListAssignDef(driver->devs, def))) goto cleanup; + virNodeDeviceObjSetPersistent(obj, was_persistent); objdef =3D virNodeDeviceObjGetDef(obj); =20 if (new_device) @@ -1935,6 +1952,10 @@ nodeStateInitializeEnumerate(void *opaque) /* Populate with known devices */ if (udevEnumerateDevices(udev) !=3D 0) goto error; + /* Load persistent mdevs (which might not be activated yet) and additi= onal + * information about active mediated devices from mdevctl */ + if (nodeDeviceUpdateMediatedDevices() !=3D 0) + goto error; =20 nodeDeviceLock(); driver->initialized =3D true; --=20 2.26.2