From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6709749550D for ; Tue, 14 Jul 2026 15:15:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042115; cv=none; b=rSSnxxHtBJvHJb0t+IZNX9qoK4yDKfJDg2YZrp/wYkanvWqln4JtbfUYfhVwkiVPaV6+PGATpSrdOygEvC5aEFcg6ZPWKXtu/JhHxuq3fyWfKdHYAxXUZBOHMBV8DlpQfomQvXDyrcbnR4/oo8O1tiCEIo3vAATUkgJ1GlAKmZc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042115; c=relaxed/simple; bh=zNaX3o4Q+PMjJkfE7ZeWse4rtkQ12e9AdpLMHhDvtQA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=p+ft3Es/zPMlsPoPiZyNzeOfgOvRuEdfBQG77Cb8HFzYmeM/NPX4kBqqG1D5lk9xJav3yQczqUIfDg85XZVljM+ieqbR8IO6T6OYOp42lQ74gK4E+/SFawNBCMB1U5yxi6xAxgpxyNUuX2qamh5Wvq/mg/QEk4zEP+CCbvJTPvU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RtvF1jnw; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RtvF1jnw" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2cc88e22f92so91142875ad.1 for ; Tue, 14 Jul 2026 08:15:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042111; x=1784646911; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=OET28Pw7Tm9U8SptXyUJqzCGyMYvQ/PVMzqx3Tt4yN8=; b=RtvF1jnwUVPbVVyENKs2UnEkfbOjjQybzx1a17V0LwoMW+w9/3kNSPfNWYB9gUiliU 5xAFqgv5VypxX8zoPIeCD641En74yU2bL1vII4AxP6A3friE1qQbono9GBHvKXsZSeig H3FmsBOjiNkzdyCruVpa8ycYSOOwwHWK85UhxVk0+WNElhZ7/W80m+4BvPwz1bEZ+Nk5 Uq/TMm8gSxOwpuy2xZMNAMIuFw7PLfE/4GKjskKjxX62YRa9kKhMkMxd9QYk4YshIwFR zgkaddaBDpio0947CJBcBmWwxaItwD/9zuC30mqExlVY4U7DSqLsm2zC29Xr3guBX8nm lCNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042111; x=1784646911; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=OET28Pw7Tm9U8SptXyUJqzCGyMYvQ/PVMzqx3Tt4yN8=; b=Vi2PE4bVBBxuftdsF1+i5pHQsBf8IQTwpSXoF1bk6Vlm3InzX23yANA19nWjupm1hA O37akcuQZNJGn8XUx66X4zjsxB1RyWPHzg7WvrqL29c5ajAo7orV3KV1IZhsCMruvgOd GNqKPxR1NrROsMBhvtm7J+hl3kK/hXSIvvt32mYwtk064eKhBCR2hUMOSdPo8dFKcy/8 bgbfTaXhhZy11qSppC+fAyNcHXVmkrCn8mPvuvMi5EqtcbgiBqy5pVxP+RNa5FpUCoaA nNxh2WskH8jBDwQZzq5E9xZklW5uVMPZH9B1ioerhbXsLmAWa6vfVxsffuJiFl9EjN6v pL5Q== X-Forwarded-Encrypted: i=1; AHgh+RpaxlV33z+qQtXOTiF4OPpuvIjJaPvQIgfHiY/AHkVoAd7wFfRvx42lW7K7iySvXB+ydHWmQz33yFVCFxQ=@vger.kernel.org X-Gm-Message-State: AOJu0YylhwIjcwzzTfs3OGrYGc+yWEMqX7xuovDP/idlOyiGV1aNkFgI +NaqesqyM5svzL6v4tBl4en12jNTX7t4xZzuDZB1phYdYBqAhZvXLftC4hb5VTnnVomy0f0KiWF p8wL+TuZlvg== X-Received: from pldu11.prod.google.com ([2002:a17:903:108b:b0:2c7:e06e:86f6]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:b85:b0:2c9:ae0b:61e3 with SMTP id d9443c01a7336-2ce9e9a6ed8mr116458815ad.2.1784042110624; Tue, 14 Jul 2026 08:15:10 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:46 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-2-vipinsh@google.com> Subject: [PATCH v5 01/20] vfio/pci: Factor out the reset logic in VFIO PCI device close path From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" vfio_pci_core_disable() is a very long function doing multiple things like exiting runtime power management, setting device to D0 power, clearing bus master, clearing irqs, freeing eventfds, releasing device regions, unmapping PCI bars, and performing device/bus resets. The upcoming VFIO Liveupdate feature will also need to utilize same reset logic. Extract device reset code to a new helper function, vfio_pci_core_try_reset(), and update vfio_pci_core_disable() to use this new function. No functional change intended. Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- drivers/vfio/pci/vfio_pci_core.c | 100 ++++++++++++++++--------------- 1 file changed, 53 insertions(+), 47 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index a28f1e99362c..3c8b990ee92f 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -623,9 +623,60 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *= vdev) } EXPORT_SYMBOL_GPL(vfio_pci_core_enable); =20 -void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) +static void vfio_pci_core_try_reset(struct vfio_pci_core_device *vdev) { + struct pci_dev *pdev =3D vdev->pdev; struct pci_dev *bridge; + + vdev->needs_reset =3D true; + + /* + * If we have saved state, restore it. If we can reset the device, + * even better. Resetting with current state seems better than + * nothing, but saving and restoring current state without reset + * is just busy work. + */ + if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) { + pci_info(pdev, "%s: Couldn't reload saved state\n", __func__); + + if (!vdev->reset_works) + return; + + pci_save_state(pdev); + } + + /* + * Disable INTx and MSI, presumably to avoid spurious interrupts + * during reset. Stolen from pci_reset_function() + */ + pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); + + /* + * Try to get the locks ourselves to prevent a deadlock. The + * success of this is dependent on being able to lock the device, + * which is not always possible. + * We can not use the "try" reset interface here, which will + * overwrite the previously restored configuration information. + */ + if (vdev->reset_works) { + bridge =3D pci_upstream_bridge(pdev); + if (bridge && !pci_dev_trylock(bridge)) + goto out_restore_state; + if (pci_dev_trylock(pdev)) { + if (!__pci_reset_function_locked(pdev)) + vdev->needs_reset =3D false; + pci_dev_unlock(pdev); + } + if (bridge) + pci_dev_unlock(bridge); + } + +out_restore_state: + pci_restore_state(pdev); +} + +void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) +{ struct pci_dev *pdev =3D vdev->pdev; struct vfio_pci_dummy_resource *dummy_res, *tmp; struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; @@ -700,54 +751,9 @@ void vfio_pci_core_disable(struct vfio_pci_core_device= *vdev) kfree(dummy_res); } =20 - vdev->needs_reset =3D true; - vfio_pci_zdev_close_device(vdev); =20 - /* - * If we have saved state, restore it. If we can reset the device, - * even better. Resetting with current state seems better than - * nothing, but saving and restoring current state without reset - * is just busy work. - */ - if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) { - pci_info(pdev, "%s: Couldn't reload saved state\n", __func__); - - if (!vdev->reset_works) - goto out; - - pci_save_state(pdev); - } - - /* - * Disable INTx and MSI, presumably to avoid spurious interrupts - * during reset. Stolen from pci_reset_function() - */ - pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); - - /* - * Try to get the locks ourselves to prevent a deadlock. The - * success of this is dependent on being able to lock the device, - * which is not always possible. - * We can not use the "try" reset interface here, which will - * overwrite the previously restored configuration information. - */ - if (vdev->reset_works) { - bridge =3D pci_upstream_bridge(pdev); - if (bridge && !pci_dev_trylock(bridge)) - goto out_restore_state; - if (pci_dev_trylock(pdev)) { - if (!__pci_reset_function_locked(pdev)) - vdev->needs_reset =3D false; - pci_dev_unlock(pdev); - } - if (bridge) - pci_dev_unlock(bridge); - } - -out_restore_state: - pci_restore_state(pdev); -out: + vfio_pci_core_try_reset(vdev); pci_disable_device(pdev); =20 vfio_pci_dev_set_try_reset(vdev->vdev.dev_set); --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A2E2233938 for ; Tue, 14 Jul 2026 15:15:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042116; cv=none; b=D29UfWUF7cSQurE9OPhecGQ6Cl6UCUEh2xp+ht/bSLE9rGRI9zzINaIQ/mLLT703JUBwZQYAkcGk6UiJy2Xr3gnxzEmHdWEwp3b1YFPvcMB6FcwU1u4Cgy7vjluDEqGk95U2WIwX5aZzfyF4gz3L9ARcN2/Rdpdwztth1xzKhJ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042116; c=relaxed/simple; bh=uKsEEI4F1niy9EceQWdXOHKPYFI0RfY+kBMdOL+Yuj0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NMdZoybyKBipwGh7E/3uaqY+YWY3tX47MEGE9K+WY6N3wa+yXJjOVwYSPcOzmHaUKK3VbSjK6Bgowm0nqyAwBOliI8iAOsxhrFPhd9Gmu3Da4A/2Ieb4s3ZySAZwKPkcqPDe/YKOckkzylgOhEcOXpNUslRrwbd6uuzoUaJ714Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=S/tgKfnt; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="S/tgKfnt" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-8484b57b98bso8660743b3a.0 for ; Tue, 14 Jul 2026 08:15:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042112; x=1784646912; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=S1UFdHONMaUW3i8xUAtShYLn0EA7imz4HjKdEgIKF0Q=; b=S/tgKfntKJGsHupLkgX4KDgVgp3BZnxow2AJi8vFPRSYHrx5HBe6WxVtthoTfFxsb4 219tKk1LOdYrue0WgvcRqYEkzpCs5N7X9Df4ngRAz1XJ/tt6PfrhVNW7guLGCucDb3Kb C+5LeDKj+KvY6C3D1CZstCu1E1eUFYOMsU40IzRNSAqt7agXaSWDYrl1OmHlpdfe/rcu aQ2C1Ek5rKiqAHvniYo/aQCJ2npm+iA3yBEvzavtT39wUgNFLger/mqOBmXdscX72eyO LLC5D0QyTI1vv5aaQRSR3boV9VDbAkPqEpmNrBW7KmLAWdeHx9uwup/N+/jGeHcUQIp7 rArw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042112; x=1784646912; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=S1UFdHONMaUW3i8xUAtShYLn0EA7imz4HjKdEgIKF0Q=; b=BRWBB4wRL9WxHxj7SYyH8MhEqCeUjDU9k1d9lrJzFW3sXjPb3C3pjZ1lUyvkaOgctE p4/UNotXqWrzUCuyCH9zaUcIyzltHC26HFMiPx8cNTrWEYnMB1sMUwYkga2QYrLIJmu0 +CEe7yep4vzrHk7ZnECrfWyFuwMco2y/lIRJaWqhUhqU/fyvyTh0lNORBaJJer24uOuj GPKW7/lBWmYp2voiX/LiPvUEPEQjENpdHiCSl0dDzZ6sg12amOhC067I9exG7yDnMW7v pbf3XhNzAwZi14YTp8jR5VcPxJZKZMECIaycI9D/q4wGksuc1AJuzu+pICEcepO19gzf Yt6g== X-Forwarded-Encrypted: i=1; AHgh+RqYmyhV0phlGIUyeFMgvS2AI3aX5DdpO8gtHkfO8LUiTuFg1ATntJGE2QtFj989cjtKnMR8Y1nLDsJaH1U=@vger.kernel.org X-Gm-Message-State: AOJu0YxHLa46VmvX/YndEGluSEyuSBNUSBYqkD2d3wRbRjfNIHU8bEFY +QTLmZ2mXKSm3BwoNjWPqawAFg3/pMnEvSFl4As8tx+jXVG3ZUPpSLaGi7KgIRQtsjiJURTYqqJ pScjTL2Budg== X-Received: from pfbdi5.prod.google.com ([2002:a05:6a00:4805:b0:848:4690:d658]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e93:b0:848:19f4:3b52 with SMTP id d2e1a72fcca58-84a516062d4mr3676620b3a.38.1784042111640; Tue, 14 Jul 2026 08:15:11 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:47 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-3-vipinsh@google.com> Subject: [PATCH v5 02/20] vfio: Export various helpers from VFIO From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The upcoming VFIO live update feature needs to access various helper functions for its logic which are not exported by default. Export these functions and variable for other modules to use them. Remove static from their declaration and add them in include/linux/vfio.h and drivers/vfio/pci/vfio_pci_priv.h header files. No functional change intended. Signed-off-by: Vipin Sharma --- drivers/vfio/pci/vfio_pci.c | 2 +- drivers/vfio/pci/vfio_pci_core.c | 6 ++++-- drivers/vfio/pci/vfio_pci_priv.h | 5 +++++ drivers/vfio/vfio_main.c | 3 ++- include/linux/vfio.h | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 0c771064c0b8..1fb4b276afe7 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -125,7 +125,7 @@ static int vfio_pci_open_device(struct vfio_device *cor= e_vdev) return 0; } =20 -static const struct vfio_device_ops vfio_pci_ops =3D { +const struct vfio_device_ops vfio_pci_ops =3D { .name =3D "vfio-pci", .init =3D vfio_pci_core_init_dev, .release =3D vfio_pci_core_release_dev, diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 3c8b990ee92f..f31e59e5badb 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -623,7 +623,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *v= dev) } EXPORT_SYMBOL_GPL(vfio_pci_core_enable); =20 -static void vfio_pci_core_try_reset(struct vfio_pci_core_device *vdev) +void vfio_pci_core_try_reset(struct vfio_pci_core_device *vdev) { struct pci_dev *pdev =3D vdev->pdev; struct pci_dev *bridge; @@ -674,6 +674,7 @@ static void vfio_pci_core_try_reset(struct vfio_pci_cor= e_device *vdev) out_restore_state: pci_restore_state(pdev); } +EXPORT_SYMBOL_GPL(vfio_pci_core_try_reset); =20 void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) { @@ -1657,7 +1658,7 @@ ssize_t vfio_pci_core_write(struct vfio_device *core_= vdev, const char __user *bu } EXPORT_SYMBOL_GPL(vfio_pci_core_write); =20 -static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev) +void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev) { struct vfio_device *core_vdev =3D &vdev->vdev; loff_t start =3D VFIO_PCI_INDEX_TO_OFFSET(VFIO_PCI_BAR0_REGION_INDEX); @@ -1666,6 +1667,7 @@ static void vfio_pci_zap_bars(struct vfio_pci_core_de= vice *vdev) =20 unmap_mapping_range(core_vdev->inode->i_mapping, start, len, true); } +EXPORT_SYMBOL_GPL(vfio_pci_zap_bars); =20 void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device *= vdev) { diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_p= riv.h index fca9d0dfac90..85bc1666cb0e 100644 --- a/drivers/vfio/pci/vfio_pci_priv.h +++ b/drivers/vfio/pci/vfio_pci_priv.h @@ -11,6 +11,8 @@ /* Cap maximum number of ioeventfds per device (arbitrary) */ #define VFIO_PCI_IOEVENTFD_MAX 1000 =20 +extern const struct vfio_device_ops vfio_pci_ops; + struct vfio_pci_ioeventfd { struct list_head next; struct vfio_pci_core_device *vdev; @@ -73,6 +75,9 @@ u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_core_= device *vdev); void vfio_pci_memory_unlock_and_restore(struct vfio_pci_core_device *vdev, u16 cmd); =20 +void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev); +void vfio_pci_core_try_reset(struct vfio_pci_core_device *vdev); + #ifdef CONFIG_VFIO_PCI_IGD bool vfio_pci_is_intel_display(struct pci_dev *pdev); int vfio_pci_igd_init(struct vfio_pci_core_device *vdev); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 5e0422014523..2437af031169 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1467,7 +1467,7 @@ const struct file_operations vfio_device_fops =3D { #endif }; =20 -static struct vfio_device *vfio_device_from_file(struct file *file) +struct vfio_device *vfio_device_from_file(struct file *file) { struct vfio_device_file *df =3D file->private_data; =20 @@ -1475,6 +1475,7 @@ static struct vfio_device *vfio_device_from_file(stru= ct file *file) return NULL; return df->device; } +EXPORT_SYMBOL_GPL(vfio_device_from_file); =20 /** * vfio_file_is_valid - True if the file is valid vfio file diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 31b826efba00..cf0d941d0410 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -378,6 +378,7 @@ static inline bool vfio_file_has_dev(struct file *file,= struct vfio_device *devi bool vfio_file_is_valid(struct file *file); bool vfio_file_enforced_coherent(struct file *file); void vfio_file_set_kvm(struct file *file, struct kvm *kvm); +struct vfio_device *vfio_device_from_file(struct file *file); =20 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) =20 --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9109F495526 for ; Tue, 14 Jul 2026 15:15:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042116; cv=none; b=hkHiD3RZAXWPKWGQkrxgpbNn1c8zGttSZPDt8H6wBE8HzgpmM4tDTS/zQPGo9QEA0Le+Dm2uLHRFjFhbSCmXIpq/Q+EQOfu+LYVXF7liRVT1MU0+3FLYcDlEWLQJFnNmzK3D/yrOfz4tVyco6jaTg/3PB9yEClvSeZfTJmRG7rU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042116; c=relaxed/simple; bh=vk3lJYsQ6kv5efXhe5IXY+0n8uW9hKBTtNoO26/F0xo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eTOShJ7ceyjzMoACu5dWs2MwwsrIyyD2OLbPhmU7/bxek81T6Gz4onljXWa0iXKhGk+wo7XtOrjLcKc3S7eC7i7imQnyBxpfTwQ7jePeZY+sSvVoEKoUJQOuW71CJm+FCG950TcR5QOdHOyyDJBYAEEyP+Ow61/iMj7UsulHFSE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cXAbhwxC; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cXAbhwxC" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-848551b54f5so8620391b3a.0 for ; Tue, 14 Jul 2026 08:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042113; x=1784646913; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NOi6e266Y3mSv+Tn1Q4zn00EKJBjh0V4SkK/1ApFkGE=; b=cXAbhwxC1l71o8RZqwj5fOoLRKiP76rP/kX3zsAGquOeEtEeDjawKJOMAq4iIODr3T 5TTJ1enzec5g2l+jcPRAXMR58yC1nc4b/pcY1Gyyjpo4W/mJbN+bT4z/XVKC0ibUsT43 rVp0o8dmKSCO6Ui+DcfCxmpvWBgicJW4uDsKvtif2Oz87dEgBJNTnn9P3UJZGhccMgBX KSn25OE039XDsViwXN7TINoskS3oOxShSMs5mi4BsG47T+wzCH7cVI5vgjJ3psEfVZbI xCP6c6yURk88vIRB7v14D7S3dsIpE9+S50MyHiebrUxWNUXAIqHgc+XcN18+ibRDN2mI ilfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042113; x=1784646913; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NOi6e266Y3mSv+Tn1Q4zn00EKJBjh0V4SkK/1ApFkGE=; b=cpnOiqPrl00zsi0PVZx7SMDUAbo1jGDCFU+XMROZGvVbr3/g3obHmndXVihYW25U3k 9Ohge2Wik2ifJchFsXoCopvW8lpkOLxOLDcTjcA7DeBljuLo+xfqb9galJlReqAuykI9 Abre616bBgE7ZkMsOXZh/ouIgpkb+9hbzI9dgNWsdUtG2nBMy/CDIuZQf594arb41IqX 1Bkl0sJvWbcOZ7jWmrP8mJTm7B5p6VGMvNbYvGntQ6Aejpg4kNnk+L+VYWA6o6HqwK5+ E+sEU7zsUj62K78kBr2uAq8kyFvwu+NzARB8eOcjC4FyMBBQX8Q0Z7utvR6La2+hICoJ 403g== X-Forwarded-Encrypted: i=1; AHgh+RqHtfspAWGr9PGMNcd0wtdds3MGoiMCvtfvd3QBdw50DT05ieMXxz4BpD0xWj3GtOkDAjg6oGVTWAWVTK0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw/OHMv8Z6LPdQ6R1hrHOAKc1kwZTOlM8l9vV+rYNsmOXfIjdQP 9HJL5zYHNfXoi62mQGI14uYJk7efWzlOSPsG9ICaYzvOVwQsMEMJat81ZYP2awGwVfQvdQ3f6Jp ayF9qF4OzMQ== X-Received: from pfee10.prod.google.com ([2002:a05:6a00:bc8a:b0:847:a829:37d1]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a27:b0:848:2f7a:2e5e with SMTP id d2e1a72fcca58-8488976671amr13012571b3a.77.1784042112537; Tue, 14 Jul 2026 08:15:12 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:48 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-4-vipinsh@google.com> Subject: [PATCH v5 03/20] vfio/pci: Export vfio_pci_dma_buf_move for vfio-pci module From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" VFIO Live update during freeze needs to reset the device before handing over to next kernel. Export DMA revoke function which VFIO Live Update will call in its freeze before reset so that P2P DMA doesn't error out or cause some other issue. Signed-off-by: Vipin Sharma --- drivers/vfio/pci/vfio_pci_dmabuf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci= _dmabuf.c index c16f460c01d6..2c05fb5a171a 100644 --- a/drivers/vfio/pci/vfio_pci_dmabuf.c +++ b/drivers/vfio/pci/vfio_pci_dmabuf.c @@ -376,6 +376,7 @@ void vfio_pci_dma_buf_move(struct vfio_pci_core_device = *vdev, bool revoked) fput(priv->dmabuf->file); } } +EXPORT_SYMBOL_GPL(vfio_pci_dma_buf_move); =20 void vfio_pci_dma_buf_cleanup(struct vfio_pci_core_device *vdev) { --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1362A34CFCF for ; Tue, 14 Jul 2026 15:15:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042118; cv=none; b=sArqfrgseyAamb3VmZsHr4W3QmpswzEXV7T/0+u/NiLp50ZJEsmWgyj90MU6xqs7K8cfWzgI/4sidOWUlwWHe19z90isJHAw5mdaScvzF7v4YinuAxDtEEsQIKbsBchytxkdeycwu+KmmRUMAnbZfag7fLxxezgNBQ0VU4jvMms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042118; c=relaxed/simple; bh=YVc+pKa6Xq4074+to6tFg828NsW4PA06kR2BHhEiYWA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HiA/OoqCkVS3TSADikvnaD/9vo0elAoQZ4BSNOcoK3XFs1xVQGgXKs6excSzwbM4WTCr5P7yZociQ+EC1JeT+F4fzdGAXXp8iFcGSrCAC0fP0vPdTl5RFUzFnKN9rskStjdhkSoM9HT86MlEb2XmWUyu8S7p6kSvTukh0Y5jeHs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=K4t2rlcF; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="K4t2rlcF" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-8482b95574dso5639114b3a.1 for ; Tue, 14 Jul 2026 08:15:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042114; x=1784646914; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NsilKFLygw8IS3OsjN731JV3UeNVYFWUky8KZoNrr2s=; b=K4t2rlcF3KYL3tUet10Vhgi5pW8myoazkA0EXJwCQmfspyzYTzKSmHJzDNfaeGroes tCbeqGVNsJ4dd2Nuqx1ApzTwwtw6PBDgkeUJHHCPpOrBA16NrC62Bb5R+lz7CWci9J9d Q2i0o5rpsCpIIESANYDmqbPiDeVjtUrFvNBgg53wmeiXgvBh+F3EJ2pYzi/o99rUxsTO CbMjHdpR/rpEf7P16STgHd+xuHWlJeLL8vmStl5ViYP0Jcg4bP6sNvqE15/eZFDr/Ip4 NDs41oKa1UOYLzn/JRWcSyf5z6WrtPHzPiFIxdb+LZc0N0pkXt8R6wdR8h5IKMWOhP0X nkNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042114; x=1784646914; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NsilKFLygw8IS3OsjN731JV3UeNVYFWUky8KZoNrr2s=; b=dFn5zw2HApIJpiwGY6AIB6GQ/VVnpS5OytrCV0CC+NuoI7qzWASIYXhiTLjWubXoWL mbjSAVD7bdfDdtus48yk3q+HWsDHNr7NU0OUOxd3rZHB7OttIzIo354yOQrJFjuXT5Ry xtmzcAN01vItwnBxI6xmEGCqlLaAbncsNmbpLy0GVOukl93tJ9iBv5j4u0ABzvKmrdYN fHDaee0MIo1uzNOsOqG1jp+iduvTVHmptVZwbEYYijxQ6Cyrwwdt9Fmpb/6TjwXCg0lX Zpw1d5DzyyIdLz5EdrDwLB+0BvE8u2xtIucPNS0iEggFUhgs9FZvzGGcR5ZhMpKdSOmy /QBQ== X-Forwarded-Encrypted: i=1; AHgh+RpiKIySvL14z26FnsTiA1hgvNttT56rvNWmWePPXL+u3ITSL2u2JWh1ttqKSR/yMO6WMWqQHVmzMwxy5HA=@vger.kernel.org X-Gm-Message-State: AOJu0YyQXf0ZcqzBpgT1j2XsMeQ8hrUXyiID23E0IZ+mQ7hVKKjbTs/W pwScIsqFER/hevR049x7RAbeEl2B0cp+Oh55SIXUCf4nhB8vghmB0S5652Kq03z4X6aZTEBfD82 iS8jWbVJMgQ== X-Received: from pfff9.prod.google.com ([2002:a05:6a00:bd09:b0:848:487e:d23c]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:992:b0:848:65f3:23c1 with SMTP id d2e1a72fcca58-848897d7661mr12194779b3a.69.1784042113533; Tue, 14 Jul 2026 08:15:13 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:49 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-5-vipinsh@google.com> Subject: [PATCH v5 04/20] liveupdate: Export symbols needed by modules From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Export liveupdate_register_file_handler() and liveupdate_unregister_file_handler(). All of these will be used by vfio-pci in a subsequent commit, which can be built as a module. Reviewed-by: Samiullah Khawaja Reviewed-by: Pranjal Shrivastava Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- kernel/liveupdate/luo_file.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c index c39f96961a85..dd4e26ac34f0 100644 --- a/kernel/liveupdate/luo_file.c +++ b/kernel/liveupdate/luo_file.c @@ -909,6 +909,7 @@ int liveupdate_register_file_handler(struct liveupdate_= file_handler *fh) up_write(&luo_register_rwlock); return err; } +EXPORT_SYMBOL_GPL(liveupdate_register_file_handler); =20 /** * liveupdate_unregister_file_handler - Unregister a liveupdate file handl= er @@ -926,3 +927,4 @@ void liveupdate_unregister_file_handler(struct liveupda= te_file_handler *fh) luo_flb_unregister_all(fh); list_del(&ACCESS_PRIVATE(fh, list)); } +EXPORT_SYMBOL_GPL(liveupdate_unregister_file_handler); --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3E04495530 for ; Tue, 14 Jul 2026 15:15:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042118; cv=none; b=pyXkFlNTJj6AjHv7F73kMcAM3HzY3eyXETAAlN1Vp7XhSTFSfdNldb7b5GlPhWx/QziRAvtjCTfpED0EYcOH1Ei6DnXc+Wf3sUfz9iU6r9l5SBJV/R5b9am3vGH1AhgLIUNS452PSjHg6qXKT9tBhJg2HcD06vVUDW84eV4KMdo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042118; c=relaxed/simple; bh=CA6HRFl1VzcHVdZ+8FD2TBs4Cp5+Gp7/yga7JFoATG0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G9lLf+JZ3y9B5UrAHrbxx4SQN6q4uY5qiwot/JXSD67Juuu3nxOG36cgOSQWtwmDbbmQYZ98b8c0zAW8Xh62CDPgbiku3qlFYhxhKCkhHQCxIjVXllSYGBMOGIfSVaHfTqsa1PZhIbHUL5qSxwQp3Gsu9VOBb7O84pctucomJ/I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sLO1mFtg; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sLO1mFtg" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-84885a4fcabso1484887b3a.3 for ; Tue, 14 Jul 2026 08:15:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042115; x=1784646915; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=cbe+Pbv5TCQdXk1eG0Iq+EuxqrVwsKywg3T3pq48EEY=; b=sLO1mFtgn14IfUtBS7RJRq3tWuGYgZ4CzllUakdX+CWHHrVohLRBEGkrZPBSBYxo87 KdUhXMAPkkA6i/ZP30DRZogbOCAMBz7myhftxnIOykgoKXVLZt4I70llwKxOa12VTCMb xswe6VUnF6+U4iJV2KjJiJEvBq0hkaZnKZIvJIqLV/YmDCdZRg6c45b7Vt90oDh4gWU8 d2jLf+Vj7SwKk6v+Vo5gISqn4phaSKs7aCiHuE9whn9c4l3kVI13pNRf1X7M01F9M7kJ 0SKhqeZfg8m/eXRlB5KBiY3GW8+cYcRYh4LgCeNQHJ8Id4wpxOXs1vYsbMmlmRIYT7Iw JgfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042115; x=1784646915; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cbe+Pbv5TCQdXk1eG0Iq+EuxqrVwsKywg3T3pq48EEY=; b=D1PLSeXjnBOwsrBe2xHM1sTk1uuVjmJYuQt3/Tdw7jedJuQ2DVMPb7cMjiONRURaR6 fcU3ew/tjnU42LVIW/audzw3nrfmIysbnbjjJE00keLCrXMv8CcKcAgCtY8ewtJYgLrZ 3Trph1B0ZFKtNHtj8E95QqHSlixr1ceIVKesvuAzqEuXkqTgXriTH3eSJIaTpohEVdIs JhmE5jbMeo4oIRybnuIZDkcIFNZmKvwZCM/OyLk8nb1d6gy7HObz5yJ9yMc08XR0gKO3 4Rw26D2xPUtzL/CRHs5KtrP7IsXuXEL52eKHxiPMAH2ppiR3/O86iD1ZU32mWMKZEPzK B65A== X-Forwarded-Encrypted: i=1; AHgh+Rpd8CpvTdzyRkwn2mW/EmEeMquvAlAn0Gkf85zeNo30tYOMp97KOQAHKFeaxowHS85pLCdU531CLR9S+cQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyH54D/hvP59b7KbRtppxI/uYUK4ppsPwVTVe22jvNF8broOBl8 3UPsiXh6OJ6UeOhuOw6ju6zNh6hsOLUjbCOCiEtwrju7rSU6q2DUVG1A+I7lWnMb54jknZnMOY3 M411eiCaq8w== X-Received: from pgj187.prod.google.com ([2002:a63:9c4:0:b0:c92:460e:4f54]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:728c:b0:39f:a8fb:3354 with SMTP id adf61e73a8af0-3c1108b5a4bmr12826055637.17.1784042114667; Tue, 14 Jul 2026 08:15:14 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:50 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-6-vipinsh@google.com> Subject: [PATCH v5 05/20] vfio/pci: Register a file handler with Live Update Orchestrator From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Register a live update file handler for vfio-pci device files. Add stub implementations of all required callbacks so that registration does not fail (i.e. to avoid breaking git-bisect). Register it with both live update framework and PCI subsystem. This file handler will be extended in subsequent commits to enable a device bound to vfio-pci to run without interruption while the host is going through a kexec Live Update. Put this support behind a new Kconfig VFIO_PCI_LIVEUPDATE that is marked experimental and default-disabled until more of the device preservation support has landed in the kernel. Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- MAINTAINERS | 1 + drivers/vfio/pci/Kconfig | 12 ++++ drivers/vfio/pci/Makefile | 1 + drivers/vfio/pci/vfio_pci.c | 11 +++- drivers/vfio/pci/vfio_pci_liveupdate.c | 80 ++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_priv.h | 14 +++++ include/linux/kho/abi/vfio_pci.h | 28 +++++++++ 7 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 drivers/vfio/pci/vfio_pci_liveupdate.c create mode 100644 include/linux/kho/abi/vfio_pci.h diff --git a/MAINTAINERS b/MAINTAINERS index 347c435ca404..77a35742de0a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -28329,6 +28329,7 @@ F: Documentation/ABI/testing/debugfs-vfio F: Documentation/ABI/testing/sysfs-devices-vfio-dev F: Documentation/driver-api/vfio.rst F: drivers/vfio/ +F: include/linux/kho/abi/vfio_pci.h F: include/linux/vfio.h F: include/linux/vfio_pci_core.h F: include/uapi/linux/vfio.h diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig index 296bf01e185e..c12d614fc6c4 100644 --- a/drivers/vfio/pci/Kconfig +++ b/drivers/vfio/pci/Kconfig @@ -42,6 +42,18 @@ config VFIO_PCI_IGD and LPC bridge config space. =20 To enable Intel IGD assignment through vfio-pci, say Y. + +config VFIO_PCI_LIVEUPDATE + bool "VFIO PCI support for Live Update (EXPERIMENTAL)" + depends on PCI_LIVEUPDATE + help + Support for preserving devices bound to vfio-pci across a Live + Update. This option should only be enabled by developers working on + implementing this support. Once enough support has landed in the + kernel, this option will no longer be marked EXPERIMENTAL. + + If you don't know what to do here, say N. + endif =20 config VFIO_PCI_ZDEV_KVM diff --git a/drivers/vfio/pci/Makefile b/drivers/vfio/pci/Makefile index 6138f1bf241d..bf296ab1c967 100644 --- a/drivers/vfio/pci/Makefile +++ b/drivers/vfio/pci/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_VFIO_PCI_CORE) +=3D vfio-pci-core.o =20 vfio-pci-y :=3D vfio_pci.o vfio-pci-$(CONFIG_VFIO_PCI_IGD) +=3D vfio_pci_igd.o +vfio-pci-$(CONFIG_VFIO_PCI_LIVEUPDATE) +=3D vfio_pci_liveupdate.o obj-$(CONFIG_VFIO_PCI) +=3D vfio-pci.o =20 obj-$(CONFIG_MLX5_VFIO_PCI) +=3D mlx5/ diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 1fb4b276afe7..439d14d79d8f 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -264,10 +264,14 @@ static int __init vfio_pci_init(void) =20 vfio_pci_core_set_params(nointxmask, is_disable_vga, disable_idle_d3); =20 + ret =3D vfio_pci_liveupdate_init(); + if (ret) + return ret; + /* Register and scan for devices */ ret =3D pci_register_driver(&vfio_pci_driver); if (ret) - return ret; + goto err_liveupdate_cleanup; =20 vfio_pci_fill_ids(); =20 @@ -275,12 +279,17 @@ static int __init vfio_pci_init(void) pr_warn("device denylist disabled.\n"); =20 return 0; + +err_liveupdate_cleanup: + vfio_pci_liveupdate_cleanup(); + return ret; } module_init(vfio_pci_init); =20 static void __exit vfio_pci_cleanup(void) { pci_unregister_driver(&vfio_pci_driver); + vfio_pci_liveupdate_cleanup(); } module_exit(vfio_pci_cleanup); =20 diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c new file mode 100644 index 000000000000..803528c9f1b2 --- /dev/null +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2026, Google LLC. + * Vipin Sharma + * David Matlack + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include + +#include "vfio_pci_priv.h" + +static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handle= r *handler, + struct file *file) +{ + return false; +} + +static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *ar= gs) +{ + return -EOPNOTSUPP; +} + +static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args = *args) +{ +} + +static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *ar= gs) +{ + return -EOPNOTSUPP; +} + +static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *arg= s) +{ +} + +static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops =3D { + .can_preserve =3D vfio_pci_liveupdate_can_preserve, + .preserve =3D vfio_pci_liveupdate_preserve, + .unpreserve =3D vfio_pci_liveupdate_unpreserve, + .retrieve =3D vfio_pci_liveupdate_retrieve, + .finish =3D vfio_pci_liveupdate_finish, + .owner =3D THIS_MODULE, +}; + +static struct liveupdate_file_handler vfio_pci_liveupdate_fh =3D { + .ops =3D &vfio_pci_liveupdate_file_ops, + .compatible =3D VFIO_PCI_LUO_FH_COMPATIBLE, +}; + +int __init vfio_pci_liveupdate_init(void) +{ + int ret; + + ret =3D liveupdate_register_file_handler(&vfio_pci_liveupdate_fh); + if (ret) + goto err_return; + + ret =3D pci_liveupdate_register_flb(&vfio_pci_liveupdate_fh); + if (ret) + goto err_unregister; + + return 0; + +err_unregister: + liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh); +err_return: + return (ret =3D=3D -EOPNOTSUPP) ? 0 : ret; +} + +void vfio_pci_liveupdate_cleanup(void) +{ + pci_liveupdate_unregister_flb(&vfio_pci_liveupdate_fh); + liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh); +} diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_p= riv.h index 85bc1666cb0e..5d2ad128523d 100644 --- a/drivers/vfio/pci/vfio_pci_priv.h +++ b/drivers/vfio/pci/vfio_pci_priv.h @@ -142,4 +142,18 @@ static inline void vfio_pci_dma_buf_move(struct vfio_p= ci_core_device *vdev, } #endif =20 +#ifdef CONFIG_VFIO_PCI_LIVEUPDATE +int __init vfio_pci_liveupdate_init(void); +void vfio_pci_liveupdate_cleanup(void); +#else +static inline int vfio_pci_liveupdate_init(void) +{ + return 0; +} + +static inline void vfio_pci_liveupdate_cleanup(void) +{ +} +#endif /* CONFIG_VFIO_PCI_LIVEUPDATE */ + #endif diff --git a/include/linux/kho/abi/vfio_pci.h b/include/linux/kho/abi/vfio_= pci.h new file mode 100644 index 000000000000..67e8199eec59 --- /dev/null +++ b/include/linux/kho/abi/vfio_pci.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (c) 2026, Google LLC. + * Vipin Sharma + * David Matlack + */ + +#ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H +#define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H + +/** + * DOC: VFIO PCI Live Update ABI + * + * VFIO uses the ABI defined below for preserving device files across a ke= xec + * reboot using LUO. + * + * Device metadata is serialized into memory which is then handed to the n= ext + * kernel via KHO. + * + * This interface is a contract. Any modification to any of the serializat= ion + * structs defined here constitutes a breaking change. Such changes require + * incrementing the version number in the VFIO_PCI_LUO_FH_COMPATIBLE strin= g. + */ + +#define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1" + +#endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */ --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BED0F4963B1 for ; Tue, 14 Jul 2026 15:15:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042120; cv=none; b=qIQmjX4pY2lSAoZXy4il7Du8yVeGXfg8WeXl35QwpPgDBwnkG/6XO9V0gbhhV84DYyTvm1qjeHnkebaScNwlyD7VIG9yDm7EPabJoUtrbuSNFhtajqr1iWle8qaEaWQUp4Z6YNQvat/oYSL296LHV7GBJkgPyiD0fHmIcfQkR7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042120; c=relaxed/simple; bh=zy4ZwnAN3Pcnh+KFhjXVW8yh5YMMs9zNxVJMDSeOlIA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MDbCtJtGmZo/2iFLLKOFZwgfbilFPcje8dv9fgPiCMQZq0/jH69GuyRk5G9a3a0QX6TAqj6u0zghFx60QFuVxKOukN4t1G1MvkKGuuipjHEwl6hD4hUBBkbzb4y0uCuFBLsKvjo5qzfagHSYVju0/GLi7jh4jLvIZ3ZucLxvlYM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YC65IeZZ; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YC65IeZZ" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-848860def2cso4614468b3a.2 for ; Tue, 14 Jul 2026 08:15:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042116; x=1784646916; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=H3w9lxehSFy6bEWUAEqWnKleB5eNdNwWh1m/SMhRNo4=; b=YC65IeZZLU2psEKwIAllptQko4aEP1s2USCAJbqj4ErA6MfHP9jULAn7IhB663OBxZ OeLrEQ2ummr5JV3JIZWC3ZHxoBN34lPdenqsEbfYgCjTuDjcDsXSeBYh9isvpWHAAfOE /hEz2A1jgTmsdRHpZ47FeQTaOvJv/Y/syiDwxUezX21qdbFraLkHyDEOVnVFu5fouJiK AlCe7Z6nbEleJ7DMwYvUFbtZ4BXbX1q7km1ISEP0IJr3tZHnh9q5HX4uIo0xfMHSL3Yo z3dN3Y6SDGrRHcqTItOQvnkdUD69mnRtdff3JAUN5ApCMeNDTgTn2hb3TjG5NpTVLG1+ qFqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042116; x=1784646916; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=H3w9lxehSFy6bEWUAEqWnKleB5eNdNwWh1m/SMhRNo4=; b=DS7oyDzHGZDu+RPaB2SkC++6q2tzt2v5MOPrgohbng3ztjLsVrhnF9H1now5A1wyRL Gm8y4lkh9HA1LO/gJtFPxB2kwyfdxEx7x78PBM8WMWDQrUNogGMk6m/T+B8RBbemLksX slIWIIcvFFjMxlEJGKJXGfTQJ+jf0CSqT1ZeX6eZOov+atDda1id5xn5VybDrbf61u1z LbnQ1TAuDAMnM4KQOF7+JG2jlJaSLZxUVVCx2YWxxgRaM97asVKhNaBdNURBN7UTqTnI pTmYTsj0NiUA+vq9Rr9+L16HT7KZhLxcdR2Zr6DKo4Tod97CAr2yekWATPSgJjTH6xxn FqaA== X-Forwarded-Encrypted: i=1; AHgh+Rq9XKG31GtfF0UjDQ1Ful1nhx/6MAOM0/CbgqNYfP6OfIdv0aS6vy7j2bynhY2+smHSyK3wgILBA9pkj1c=@vger.kernel.org X-Gm-Message-State: AOJu0Yzb3gKoJTLb6P5sGe5jOuuulOLP6JqNKxnNXU4SJqLOcfq5K0sZ surJ26Oa0kbdNsfW43hjk8oibTDCpBRVpFoPNs3f9ZA/r6PYjDe3dP+60qL4M1JIJFZQiJxvYrm QA0TFPL5+Ag== X-Received: from pfez10.prod.google.com ([2002:aa7:888a:0:b0:848:492a:5b41]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:17a6:b0:847:980d:71f8 with SMTP id d2e1a72fcca58-848896349ddmr12405772b3a.15.1784042115712; Tue, 14 Jul 2026 08:15:15 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:51 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-7-vipinsh@google.com> Subject: [PATCH v5 06/20] vfio/pci: Preserve vfio-pci device files across Live Update From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement the Live Update file handler callbacks to preserve a vfio-pci device across a Live Update. Subsequent commits will enable userspace to then retrieve this file after the Live Update. State about each device is serialized into a new ABI struct, vfio_pci_core_device_ser. The contents of this struct are preserved across the Live Update to the next kernel using a combination of Kexec-Handover (KHO) to preserve the page(s) holding the struct, and the Live Update Orchestrator (LUO) to preserve the physical address of the struct. For now, the only contents of struct vfio_pci_core_device_ser are the device's PCI segment number and BDF, ensuring the device can be uniquely identified after the Live Update. Key design notes and constraints: * Device state before kexec: Userspace is required to disable interrupts on the device and ensure it is in the D0 power state. This ensures the device does not send any interrupts until new handlers are set up by the next kernel. * Device reset during freeze: The device is reset and its state is restored in the freeze() callback, ensuring the next kernel receives it in a consistent state. Eventually, this will be dropped so the device can be preserved in a running state, but that requires further work in VFIO and the core PCI layer. * FD usability after freeze: Once the device is frozen, the VFIO device FD becomes unusable (i.e., ioctls and memory accesses will return errors) because the device may have already been reset. If the freeze operation fails, userspace must close the FD and reinitialize the device. * Lifecycle: LUO holds a reference to this file while it is preserved. Therefore, VFIO is guaranteed that vfio_df_device_last_close() will not be called on this device until userspace has unpreserved the file via LUO. Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- drivers/vfio/pci/Kconfig | 2 +- drivers/vfio/pci/vfio_pci_config.c | 3 + drivers/vfio/pci/vfio_pci_core.c | 15 ++ drivers/vfio/pci/vfio_pci_liveupdate.c | 198 ++++++++++++++++++++++++- drivers/vfio/pci/vfio_pci_priv.h | 9 ++ include/linux/kho/abi/vfio_pci.h | 15 ++ include/linux/vfio_pci_core.h | 3 + 7 files changed, 242 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig index c12d614fc6c4..d97c809599fa 100644 --- a/drivers/vfio/pci/Kconfig +++ b/drivers/vfio/pci/Kconfig @@ -45,7 +45,7 @@ config VFIO_PCI_IGD =20 config VFIO_PCI_LIVEUPDATE bool "VFIO PCI support for Live Update (EXPERIMENTAL)" - depends on PCI_LIVEUPDATE + depends on PCI_LIVEUPDATE && VFIO_DEVICE_CDEV help Support for preserving devices bound to vfio-pci across a Live Update. This option should only be enabled by developers working on diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci= _config.c index a10ed733f0e3..a51805515d30 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -406,6 +406,9 @@ bool __vfio_pci_memory_enabled(struct vfio_pci_core_dev= ice *vdev) struct pci_dev *pdev =3D vdev->pdev; u16 cmd =3D le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]); =20 + if (vfio_pci_dev_is_frozen(vdev)) + return false; + /* * Memory region cannot be accessed if device power state is D3. * diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index f31e59e5badb..c312b0eaf7d0 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1087,6 +1087,9 @@ int vfio_pci_ioctl_get_region_info(struct vfio_device= *core_vdev, struct pci_dev *pdev =3D vdev->pdev; int i, ret; =20 + if (vfio_pci_dev_is_frozen(vdev)) + return -EIO; + switch (info->index) { case VFIO_PCI_CONFIG_REGION_INDEX: info->offset =3D VFIO_PCI_INDEX_TO_OFFSET(info->index); @@ -1513,6 +1516,9 @@ long vfio_pci_core_ioctl(struct vfio_device *core_vde= v, unsigned int cmd, container_of(core_vdev, struct vfio_pci_core_device, vdev); void __user *uarg =3D (void __user *)arg; =20 + if (vfio_pci_dev_is_frozen(vdev)) + return -EIO; + switch (cmd) { case VFIO_DEVICE_GET_INFO: return vfio_pci_ioctl_get_info(vdev, uarg); @@ -1567,6 +1573,9 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *d= evice, u32 flags, struct vfio_pci_core_device *vdev =3D container_of(device, struct vfio_pci_core_device, vdev); =20 + if (vfio_pci_dev_is_frozen(vdev)) + return -EIO; + switch (flags & VFIO_DEVICE_FEATURE_MASK) { case VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY: return vfio_pci_core_pm_entry(vdev, flags, arg, argsz); @@ -1591,6 +1600,9 @@ static ssize_t vfio_pci_rw(struct vfio_pci_core_devic= e *vdev, char __user *buf, unsigned int index =3D VFIO_PCI_OFFSET_TO_INDEX(*ppos); int ret; =20 + if (vfio_pci_dev_is_frozen(vdev)) + return -EIO; + if (index >=3D VFIO_PCI_NUM_REGIONS + vdev->num_regions) return -EINVAL; =20 @@ -1775,6 +1787,9 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev,= struct vm_area_struct *vma u64 phys_len, req_len, pgoff, req_start; void __iomem *bar_io; =20 + if (vfio_pci_dev_is_frozen(vdev)) + return -EIO; + index =3D vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); =20 if (index >=3D VFIO_PCI_NUM_REGIONS + vdev->num_regions) diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c index 803528c9f1b2..e6eb7f6b53d7 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -6,28 +6,221 @@ * David Matlack */ =20 +/** + * DOC: VFIO PCI Preservation via LUO + * + * VFIO PCI devices can be preserved over a kexec using the Live Update + * Orchestrator (LUO) file preservation. This allows userspace (such as a = VMM) + * to transfer an in-use device to the next kernel. + * + * .. note:: + * The support for preserving VFIO PCI devices is currently *partial* a= nd + * should be considered *experimental*. It should only be used by devel= opers + * working on expanding the support for the time being. + * + * To avoid accidental usage while the support is still experimental, t= his + * support is hidden behind a default-disable config option + * ``CONFIG_VFIO_PCI_LIVEUPDATE``. Once the kernel support has stabiliz= ed and + * become complete, this option will be enabled by default when + * ``CONFIG_VFIO_PCI`` and ``CONFIG_PCI_LIVEUPDATE`` are enabled. + * + * Usage Example + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + * + * VFIO PCI devices can be preserved across a kexec by preserving the file + * associated with the device in a LUO session:: + * + * device_fd =3D open("/dev/vfio/devices/vfioX"); + * ... + * ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, { ..., device_fd, .= ..}); + * + * .. note:: + * LUO will hold an extra reference to the device file for as long as i= t is + * preserved, so there is no way for the file to be destroyed or the de= vice + * to be unbound from the vfio-pci driver while it is preserved. + * + * Retrieving the file after kexec is not yet supported. + * + * Restrictions + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + * + * The kernel imposes the following restrictions when preserving VFIO devi= ces: + * + * * The device must be bound to the ``vfio-pci`` driver. + * + * * ``CONFIG_VFIO_PCI_ZDEV_KVM`` must not be enabled. This may be relaxe= d in + * the future. + * + * * The device must not be an Intel display device. This may be relaxed = in + * the future. + * + * * The device file descriptor must be obtained by opening the VFIO char= acter + * device (``/dev/vfio/devices/vfioX``) and not via + * ``VFIO_GROUP_GET_DEVICE_FD``. + * + * * The device must meet following conditions prior to kexec without whi= ch the + * ``reboot(LINUX_REBOOT_CMD_KEXEC)`` syscall (to initiate the kexec) w= ill + * fail. + * + * - The device must have interrupt disabled. + * - The device must be in D0 state. + * + * In addition, the device must meet all of the restrictions imposed by the + * core PCI layer documented at :doc:`/PCI/liveupdate`. + * + * Preservation Behavior + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + * + * The eventual goal of this support is to avoid disrupting the workload, = state, + * or configuration of each preserved device during a Live Update. This wo= uld + * include allowing the device to perform DMA to preserved memory buffers = and + * perform P2P DMA to other preserved devices. However, there are many pie= ces + * that still need to land in the kernel. + * + * For now, VFIO only preserves the following state for devices: + * + * * The PCI Segment, Bus, Device, and Function numbers of the device. The + * kernel guarantees the these will not change across a kexec when a de= vice + * is preserved. + * + * Since the kernel is not yet prepared to preserve all parts of the devic= e and + * its dependencies (such as DMA mappings), VFIO currently resets and rest= ores + * preserved devices back into an idle state during kexec, before handing = off + * control to the next kernel. This will be relaxed in future versions of = the + * kernel once it is safe to allow the device to keep running across kexec. + */ + #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 #include +#include #include #include #include +#include =20 #include "vfio_pci_priv.h" =20 static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handle= r *handler, struct file *file) { - return false; + struct vfio_device *device =3D vfio_device_from_file(file); + struct vfio_pci_core_device *vdev; + struct pci_dev *pdev; + + if (!device) + return false; + + /* Live Update support is limited to cdev files. */ + if (!vfio_device_cdev_opened(device)) + return false; + + if (device->ops !=3D &vfio_pci_ops) + return false; + + vdev =3D container_of(device, struct vfio_pci_core_device, vdev); + pdev =3D vdev->pdev; + + /* + * Don't support specialized vfio-pci devices for now since they haven't + * been tested. + */ + if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM) || vfio_pci_is_intel_display(pde= v)) + return false; + + /* Only non-VFs are supported for now. */ + if (pdev->is_virtfn) + return false; + + return true; } =20 static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *ar= gs) { - return -EOPNOTSUPP; + struct vfio_device *device =3D vfio_device_from_file(args->file); + struct vfio_pci_core_device_ser *ser; + struct vfio_pci_core_device *vdev; + struct pci_dev *pdev; + int ret; + + vdev =3D container_of(device, struct vfio_pci_core_device, vdev); + pdev =3D vdev->pdev; + + ret =3D pci_liveupdate_preserve(pdev); + if (ret) + return ret; + + ser =3D kho_alloc_preserve(sizeof(*ser)); + if (IS_ERR(ser)) { + ret =3D PTR_ERR(ser); + goto err_unpreserve; + } + + ser->bdf =3D pci_dev_id(pdev); + ser->domain =3D pci_domain_nr(pdev->bus); + + args->serialized_data =3D virt_to_phys(ser); + return 0; + +err_unpreserve: + pci_liveupdate_unpreserve(pdev); + return ret; } =20 static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args = *args) { + struct vfio_device *device =3D vfio_device_from_file(args->file); + + pci_liveupdate_unpreserve(to_pci_dev(device->dev)); + kho_unpreserve_free(phys_to_virt(args->serialized_data)); +} + +static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args) +{ + struct vfio_device *device =3D vfio_device_from_file(args->file); + struct vfio_pci_core_device *vdev; + struct pci_dev *pdev; + + vdev =3D container_of(device, struct vfio_pci_core_device, vdev); + pdev =3D vdev->pdev; + + guard(mutex)(&device->dev_set->lock); + guard(mutex)(&vdev->igate); + guard(rwsem_write)(&vdev->memory_lock); + + /* + * Userspace must disable interrupts on the device prior to freeze so + * that the device does not send any interrupts until new interrupt + * handlers have been established by the next kernel. + */ + if (vdev->irq_type !=3D VFIO_PCI_NUM_IRQS) { + pci_err(pdev, "Freeze failed! Interrupts are still enabled.\n"); + return -EINVAL; + } + + if (pdev->current_state !=3D PCI_D0) { + pci_err(pdev, "Freeze failed! Device not in D0 state.\n"); + return -EINVAL; + } + + /* + * Reset is a temporary measure to provide kernel after kexec a clean + * device while VFIO live update work is under development and not + * fully supported. It will go away once continuous DMA support is + * added to device preservation. + */ + vfio_pci_zap_bars(vdev); + vfio_pci_dma_buf_move(vdev, true); + vfio_pci_core_try_reset(vdev); + pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); + /* + * Userspace cannot use the FD correctly now irrespective of liveupdate + * freeze failing or succeeding. They will have to reinitialize the VFIO + * device to continue using it as reset might have loaded default PCI + * state. Disable ioctl, read, write and mmap access to the device. + */ + smp_store_release(&vdev->liveupdate_frozen, true); + return 0; } =20 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *ar= gs) @@ -43,6 +236,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupd= ate_file_ops =3D { .can_preserve =3D vfio_pci_liveupdate_can_preserve, .preserve =3D vfio_pci_liveupdate_preserve, .unpreserve =3D vfio_pci_liveupdate_unpreserve, + .freeze =3D vfio_pci_liveupdate_freeze, .retrieve =3D vfio_pci_liveupdate_retrieve, .finish =3D vfio_pci_liveupdate_finish, .owner =3D THIS_MODULE, diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_p= riv.h index 5d2ad128523d..4b65753e8936 100644 --- a/drivers/vfio/pci/vfio_pci_priv.h +++ b/drivers/vfio/pci/vfio_pci_priv.h @@ -145,6 +145,11 @@ static inline void vfio_pci_dma_buf_move(struct vfio_p= ci_core_device *vdev, #ifdef CONFIG_VFIO_PCI_LIVEUPDATE int __init vfio_pci_liveupdate_init(void); void vfio_pci_liveupdate_cleanup(void); +static inline bool vfio_pci_dev_is_frozen(struct vfio_pci_core_device *vde= v) +{ + /* Written in VFIO PCI Liveupdate during freeze() event */ + return smp_load_acquire(&vdev->liveupdate_frozen); +} #else static inline int vfio_pci_liveupdate_init(void) { @@ -154,6 +159,10 @@ static inline int vfio_pci_liveupdate_init(void) static inline void vfio_pci_liveupdate_cleanup(void) { } +static inline bool vfio_pci_dev_is_frozen(struct vfio_pci_core_device *vde= v) +{ + return false; +} #endif /* CONFIG_VFIO_PCI_LIVEUPDATE */ =20 #endif diff --git a/include/linux/kho/abi/vfio_pci.h b/include/linux/kho/abi/vfio_= pci.h index 67e8199eec59..229a13ac1e52 100644 --- a/include/linux/kho/abi/vfio_pci.h +++ b/include/linux/kho/abi/vfio_pci.h @@ -9,6 +9,9 @@ #ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H #define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H =20 +#include +#include + /** * DOC: VFIO PCI Live Update ABI * @@ -25,4 +28,16 @@ =20 #define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1" =20 +/** + * struct vfio_pci_core_device_ser - Serialized state of a single VFIO PCI + * device. + * + * @domain: The device's PCI domain number (segment). + * @bdf: The device's PCI bus, device, and function number. + */ +struct vfio_pci_core_device_ser { + u32 domain; + u16 bdf; +} __packed; + #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */ diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 5fc6ce4dd786..adc5d05456bd 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -143,6 +143,9 @@ struct vfio_pci_core_device { struct notifier_block nb; struct rw_semaphore memory_lock; struct list_head dmabufs; +#ifdef CONFIG_VFIO_PCI_LIVEUPDATE + bool liveupdate_frozen; +#endif }; =20 enum vfio_pci_io_width { --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA4BF4963D8 for ; Tue, 14 Jul 2026 15:15:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042121; cv=none; b=JaTTV69P+zbXEzGPEVsEIz5SJrkF2E2M379R5OneRsNFmvjH+hkBeXDzoW1IcvsrxtJo9ll7iY7sXLOc45WkGP3Anmw65VDK/Vx06SLZLnIH5CLLfxAkGzs2kcBrG80mSLu+9UNJ0oBtVZNr/vDjiwysBOXNNuNVTqYdhdeTzLc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042121; c=relaxed/simple; bh=aKGWYiSzEZ4niXjLldUisRuvryDVWAvVanTamVNf5F8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QZIoAfFyFPYH8jP8/EO4lV+rUlWrKBinuKZPCYmNTXbOyyevYvT4I6iKuSQNUsu3F2YU3F1SlgjwWmAhV6ML44LfDK6V/TvrKyw+TzhWP9Dlq+AfHH6fTGURrf4tqe6TNXopKohVCgRixsW3z/hPElor2hHWZPjpZraz+UFLBT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=kLJM4HRA; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kLJM4HRA" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-84870e7f498so5237262b3a.3 for ; Tue, 14 Jul 2026 08:15:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042117; x=1784646917; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=/JxusoxEn+ehjTRVvbJo8Z2+RthtKj17Kn8IH4fWGXA=; b=kLJM4HRAEfedtLhhHWD0BQCFBly2bVG1r1HtPoJB5UqPJUkZjxtXQPt+DPgPBexBsR Fl9ungd9E63hblkBowIewt1qFRYxcH1Z9qo2wFKjhNy76Gf/Ya8fXTI4nGP0OO6QNkUr Kz1qLIRQNAmf2JzAnGIjJUG5DvKkMLnzB9xAmsnih5nI17dGh0cXnxvVKlAUvmCpQ5IO mlle5DpQyauOLmoybs8JaAH8Zf2nKONStAPaOZF1EEEZjakColhv2wY+toChaRV0axVB 7fQ10m4ESEWqPtKY65ZjsTGCHZtsOPBXprL2+W27CSD+jKm4ex2bEVgHgobGIm7SXRjC DMOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042117; x=1784646917; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/JxusoxEn+ehjTRVvbJo8Z2+RthtKj17Kn8IH4fWGXA=; b=n+GOLPUzKdkYtXlLY2Svd2TNfKP/BBOrqLXicSw53JJszqpelPcMiffFDsKYKfCO/a 0HAoeffMOwUMbrwSRTvnBNt8Y3/EhpScKX7fhZbAk6nBk5ns7F6ZQvZdCF1/MBjP64CD 3ZadB0So5fp1y3/z8Rsj352MwzBcAjyFlbgivRLJ4nuQE7hXVSai/dCd8WEAsUFj0Kai 5XC8Cf6idNm6fHCGUp+xib2JkCuTXdjpprkkoTVB+JjtNHpdg6u1JMFG48TpaqSElt1p NUHYSN5ZmATnrUhq0XruOVGdcHoIy3gNKUTdlh2TjwilDte3EYc75RSUSaK6VaKQpYjn LxpA== X-Forwarded-Encrypted: i=1; AHgh+Rq/tOC8MXXgEIDB3sirCN1XUDKZgwsccoGMyAGg9BT27LAujV/kI2+StKtvVH3uVseDTruGosuZ9FQySiA=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2mIgn5cF1/4VF7VtbeqsJ5M1IbhFX3ZAabi/jlmnnTzKyiM1H hTED3saiUBvw96jUTzocQsglaLemQQF3wQe/sK0NuH0Rkn1KOgJxuf2ithMDMVBcQM/pxYMMOUr A3GGMhIgjAw== X-Received: from pfbcr14.prod.google.com ([2002:a05:6a00:f0e:b0:84a:2de2:315f]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:94f5:b0:82f:2b0:2809 with SMTP id d2e1a72fcca58-84889637281mr13764500b3a.1.1784042116875; Tue, 14 Jul 2026 08:15:16 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:52 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-8-vipinsh@google.com> Subject: [PATCH v5 07/20] vfio: Introduce vfio_find_device() helper From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce vfio_find_device() to discover a registered vfio_device from vfio_device_class matching user-provided criteria. To ensure safe lifecycle handling, vfio_find_device() acquires a registration reference (device->refcount) via vfio_device_try_get_registration() rather than relying solely on device.kref taken in class_find_device(). Holding only kref allows vendor modules to unload while external callers hold device pointers, leading to a kernel panic on release. Callers must release the device via vfio_device_put_registration(). Additionally, move refcount_set() prior to vfio_device_add() in __vfio_register_dev() so device->refcount is initialized before the device is published to class lookups, avoiding registration data races. On vfio_device_add() failure, the error path drops the initial registration reference and waits for completion to drain any reference taken by a concurrent lookup before returning the error to the driver. Signed-off-by: Vipin Sharma --- drivers/vfio/vfio_main.c | 64 +++++++++++++++++++++++++++++++++++++--- include/linux/vfio.h | 2 ++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 2437af031169..e91fd83dc58d 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -359,17 +360,20 @@ static int __vfio_register_dev(struct vfio_device *de= vice, goto err_out; } =20 - ret =3D vfio_device_add(device); - if (ret) - goto err_out; - /* Refcounting can't start until the driver calls register */ refcount_set(&device->refcount, 1); =20 + ret =3D vfio_device_add(device); + if (ret) + goto err_add; + vfio_device_group_register(device); vfio_device_debugfs_init(device); =20 return 0; +err_add: + vfio_device_put_registration(device); + wait_for_completion(&device->comp); err_out: vfio_device_remove_group(device); return ret; @@ -1797,6 +1801,58 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr= _t iova, void *data, } EXPORT_SYMBOL(vfio_dma_rw); =20 +struct vfio_device_match_data { + const void *data; + device_match_t match; +}; + +static int vfio_device_match_fn(struct device *dev, const void *data) +{ + struct vfio_device *vdev =3D container_of(dev, struct vfio_device, device= ); + const struct vfio_device_match_data *mdata =3D data; + + if (mdata->match(dev, mdata->data)) { + if (vfio_device_try_get_registration(vdev)) + return 1; + } + return 0; +} + +/** + * vfio_find_device - Find a registered VFIO device matching search criter= ia + * @data: Context data passed to the @match function + * @match: Device match callback function + * + * Finds a registered VFIO device in the vfio_device_class matching the se= arch + * criteria specified by @match. + * + * On success, this function acquires a registration reference on the matc= hed + * device via vfio_device_try_get_registration(). This pins the device and + * prevents its vendor driver module from unloading or unbinding while the + * reference is held. + * + * Return: Pointer to the matching vfio_device on success, or NULL if no + * matching device is found. The caller MUST release the acquired registra= tion + * reference using vfio_device_put_registration() when finished with the d= evice. + */ +struct vfio_device *vfio_find_device(const void *data, device_match_t matc= h) +{ + struct vfio_device_match_data mdata =3D { .data =3D data, .match =3D matc= h }; + struct device *device; + + device =3D class_find_device(&vfio_device_class, NULL, &mdata, vfio_devic= e_match_fn); + if (!device) + return NULL; + + /* + * Drop standard kref reference. Caller holds reference from + * vfio_device_try_get_registration(). + */ + put_device(device); + return container_of(device, struct vfio_device, device); +} +EXPORT_SYMBOL_GPL(vfio_find_device); + /* * Module/class support */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index cf0d941d0410..42c3a9ae3a85 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -430,4 +430,6 @@ int vfio_virqfd_enable(void *opaque, int (*handler)(voi= d *, void *), void vfio_virqfd_disable(struct virqfd **pvirqfd); void vfio_virqfd_flush_thread(struct virqfd **pvirqfd); =20 +struct vfio_device *vfio_find_device(const void *data, device_match_t matc= h); + #endif /* VFIO_H */ --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4E9E4968FD for ; Tue, 14 Jul 2026 15:15:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042120; cv=none; b=QFaVOKURm8inffr/EhdlvrqnG2Yr6AVvU/Y4oxCC4qZwt+H2fXd6HD7/7XT4437nu0BQXb15pVLAaBCybNrS6i1BhnOnjwT61e/RcAVUQWUs405nK6sqLUbD3WF+Ki2OpUVWXrbwEjKa7W75hnPrRxYU1W9HCBtuPfORrJFIvhs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042120; c=relaxed/simple; bh=D1azvDxApPs5xbXiJtVJUqLP/cxUk13HfBV7O9ih/3A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kUZ44JiVfja0gPtsuaR5xWepgdEMbnvKS1Sv9Axe18EjzTJjytOAmcvr84MWXbvqxrO1tUa+fAZslu/F/x2MJe81lsGl0vJ50XNgPv78NUASENeDggt2pfW7FmDbXgKV093TkXzaz1eXM9TlOrS8nOEVfQq0bF5pvjVbj4Z+qYA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nkAVN8ha; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nkAVN8ha" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-848662cd2a1so1239968b3a.2 for ; Tue, 14 Jul 2026 08:15:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042118; x=1784646918; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=tEd+0Hxb+fZMWIajq41a/8wnr7549OLbqTKKS2EBeVQ=; b=nkAVN8ha9vPSVAEThwr9y96KS7Jl6hScvKVpUMEdQb7bYzh9SK6UqJeijRbfzNq8Cq C+7UjICjP8xXv/ZoMP/WVJlviwDRFGIGQ0vB+2JCwqwH/1yIPI1K4uHEGAIL7yjI/K/N tq9hgX8bIosrjvwonH8xk6FMOygLMnU5Dd5dmD9SRtnJMbSJGVeCLzLds8tQchi4FNI3 27tBOn6nQiqLVFYz3RI4kgxS6SGyYRI7WQuKoa4yqYMQkJswOczV407dvvveQQHf7Gtu 9aBj/RPuIDC+0JztGtF8YkbVEtSz7VtASGtyq0N2ME74QayvECMni3fmB4N08q119YC3 Jp2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042118; x=1784646918; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=tEd+0Hxb+fZMWIajq41a/8wnr7549OLbqTKKS2EBeVQ=; b=nlatK+tDu/HoofhbOjFzicFsQtnI5WeDBqZHEkz2dxYirGmxmQ/NJemBAvc7U01YG6 d/h70K16bqBXLseRzuVjGXRfTkTYkb7y9VWUopG1UZpeZvepSZUvtRjwj/GBjJ7Ew9a2 0FNACdXl5NWJVDHDVUylTlFaV755zKZ1xe4cJV+NjqF7+jufVQquTtKI+9TpEVuQlwWq jSksNRLRbw3cM4CeYfdx9mkJt6HUS32uJleL+EbYncIwxqqT4ZIvYwbdPqeuozqVgNva fi0Uq/CfWTav3e7VNvwfZI0E+1u/dG0mILXS1RdkV9fAusUeAfFI4/iUbkfYy6DSwW5U eXXA== X-Forwarded-Encrypted: i=1; AHgh+RrZkuWkU7+SWPqOrj33HQZ3EiecDbi0G6oRRg9Q9aSR3hez3KQRx6Q4xhEItqlhHePEDrUAxO+a5GrKQmM=@vger.kernel.org X-Gm-Message-State: AOJu0YxnxcT29FzUlyBVHByxmzcMcPT5GJj0OCRVeIfs/+CNjgHO8RYH r6JfphHyFbA4dhr7zwPj/bdFsHscwPKUvW2i8LJqbswiChhNGzpH5qJpV0W5XGrmTlRpkwW7xTX aV64FiW3v9w== X-Received: from pfblh20.prod.google.com ([2002:a05:6a00:7114:b0:847:86de:a817]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d9e:b0:848:7459:5f62 with SMTP id d2e1a72fcca58-848897d2bbcmr12462324b3a.44.1784042117867; Tue, 14 Jul 2026 08:15:17 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:53 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-9-vipinsh@google.com> Subject: [PATCH v5 08/20] vfio: Refactor vfio_device_fops_cdev_open() From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor vfio_device_fops_cdev_open() to use a static helper vfio_device_cdev_open(). This helper will be used in subsequent patches to support opening the device file during Live Update restore. No functional change intended. Signed-off-by: Vipin Sharma --- drivers/vfio/device_cdev.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index 54abf312cf04..2231423fe56d 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -16,14 +16,8 @@ void vfio_init_device_cdev(struct vfio_device *device) device->cdev.owner =3D THIS_MODULE; } =20 -/* - * device access via the fd opened by this function is blocked until - * .open_device() is called successfully during BIND_IOMMUFD. - */ -int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep) +static int vfio_device_cdev_open(struct vfio_device *device, struct file *= file) { - struct vfio_device *device =3D container_of(inode->i_cdev, - struct vfio_device, cdev); struct vfio_device_file *df; int ret; =20 @@ -37,14 +31,14 @@ int vfio_device_fops_cdev_open(struct inode *inode, str= uct file *filep) goto err_put_registration; } =20 - filep->private_data =3D df; + file->private_data =3D df; =20 /* * Use the pseudo fs inode on the device to link all mmaps * to the same address space, allowing us to unmap all vmas * associated to this device using unmap_mapping_range(). */ - filep->f_mapping =3D device->inode->i_mapping; + file->f_mapping =3D device->inode->i_mapping; =20 return 0; =20 @@ -53,6 +47,18 @@ int vfio_device_fops_cdev_open(struct inode *inode, stru= ct file *filep) return ret; } =20 +/* + * device access via the fd opened by this function is blocked until + * .open_device() is called successfully during BIND_IOMMUFD. + */ +int vfio_device_fops_cdev_open(struct inode *inode, struct file *file) +{ + struct vfio_device *device =3D container_of(inode->i_cdev, + struct vfio_device, cdev); + + return vfio_device_cdev_open(device, file); +} + static void vfio_df_get_kvm_safe(struct vfio_device_file *df) { spin_lock(&df->kvm_ref_lock); --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6CAE4A1384 for ; Tue, 14 Jul 2026 15:15:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042122; cv=none; b=t+xgFhLmmsp4/7jTSUXHGhEWSek094Uqj17l9WvJxbHPwys7I7ZMYXekldwTf48t3qIXKSotumkdEmrtoABRwJjr8xmB8qIeq+DCH8eW8Vm+0ZJivetWjUu9KbkdDwGW+Cb2mWkqVfe/UaD1D4hcVGJMX0itZ9TA/QxBgbQdQoQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042122; c=relaxed/simple; bh=KxwauFSG6vbKAkq6gpdomeQJiBPpzZsdR8oSo2q1AAs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JMMcVoK+Dcma99JkUkvSl4HtX92EVD7CN1BXktUyeRAhItbOq89Up4dCy4hPHFi2uoIyZY/C4s/2Tz8c+VJHfuW0eIwNpw6Wjto5ByMESvwlflr3ZaXEUorKZa+a7ChnxTmmS64bnubSLfq1NlIHJ2SlJ9GAcEBE7T2RRItYtgM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=J2CWa0jP; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="J2CWa0jP" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-38dad28e86eso2428940a91.0 for ; Tue, 14 Jul 2026 08:15:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042119; x=1784646919; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fk6o2vphXfQkoL1z/+pIbWzomzkKJfAmoavVaL/E+Fg=; b=J2CWa0jPNcS/n5ijCnmp/29Y0SnnucVQHu1YQrfxNFWU/6AYE6afIWtoBJGunolgZn qZqyR1QXtXWDO+epUBNnsfmyO3Sle4EYwCvgCshErFthwxaY9YmUp8SxoCH4kADHnIXy iv3uc+3KRtgT1+p8UxUyl1Gvb+WG+DIxkLFoc1dTruqvO2VdxN0MRzVD24Ubm6A0lVr5 5o+0iWaJayMbi0nI6sRgCQvzDJZ9GgLeDOfhsHP0WAnphxLjQpJxHGRJSSnQ8ggD8UWI US9Qwe9hqCpD5XPB+HdSOykJ0TkW81btzYkApyXLKoUDK0Lu0Ri6zvOUMYj6KxvZZUw4 fhGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042119; x=1784646919; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fk6o2vphXfQkoL1z/+pIbWzomzkKJfAmoavVaL/E+Fg=; b=RhHpU8fhXmazRA6kpmbZ0KNHZ2+cCdkU9uj++aaHtJB+R3vkwDu5YZCTjjG5X5K+PU VleQIFlBOuw9NDYzcDAYcPObfhkDtn6pUse115okNzYeyrTGfngtmkM5UCYh43LXS2XA 1bDZNmE77MF8giD9ayjMiBrQR/jJ4ALy6eISpYqnvB5iaxFs3hYTWaISGzKDPMdJNZYW QdrNOjc1dxNs9vA8IyZvxqpMwkji8RGwSA0VAcEt58lapqnudxJRZjUlo7SyCg7roSkp KewcX7RRQIOhkp9+/DGG/7r90iGJRPWmUub7P8To3xvOsbbBpBY15HsVD0pANj+k4dGh uvuQ== X-Forwarded-Encrypted: i=1; AHgh+Rq8BJzOU5u4cRnXvsovD3Bs7GlTQ4OiU4hkyVQcrnJwl2z7t/6ER+lctBtDw/b3k48QjvJNC+7d+dejtrw=@vger.kernel.org X-Gm-Message-State: AOJu0YysDoLTwiei1fpvoQkdFixe1wI2AElut+raMz1DURvjhKVDVT35 OS9xcyO3biTGGWfruSEO7iy5gQIck7B9mi3s3mfYi9gaon3Y4eCBVM3Wk7BdzpfCq6sYGxZVRwc u4FY08zqCrQ== X-Received: from pjyu5.prod.google.com ([2002:a17:90a:e005:b0:383:f7b7:fb7]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1dc9:b0:381:528a:808a with SMTP id 98e67ed59e1d1-38dc7735977mr13456012a91.27.1784042118892; Tue, 14 Jul 2026 08:15:18 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:54 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-10-vipinsh@google.com> Subject: [PATCH v5 09/20] vfio: Add API to open cdev device for Live Update restore From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" During Live Update restore, VFIO PCI driver needs to recreate the device file context that was preserved before kexec and return it to the user. Provide an API for VFIO Live Update to get VFIO device file. Use an anonymous inode when creating the file, since the retrieved device file is not opened through any particular cdev inode, and the cdev inode does not matter in practice. Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- drivers/vfio/device_cdev.c | 39 ++++++++++++++++++++++++++++++++++++-- include/linux/vfio.h | 9 +++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index 2231423fe56d..970932f22ecb 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -2,6 +2,7 @@ /* * Copyright (c) 2023 Intel Corporation. */ +#include #include #include =20 @@ -16,9 +17,10 @@ void vfio_init_device_cdev(struct vfio_device *device) device->cdev.owner =3D THIS_MODULE; } =20 -static int vfio_device_cdev_open(struct vfio_device *device, struct file *= file) +static int vfio_device_cdev_open(struct vfio_device *device, struct file *= *filep) { struct vfio_device_file *df; + struct file *file =3D *filep; int ret; =20 /* Paired with the put in vfio_device_fops_release() */ @@ -31,6 +33,24 @@ static int vfio_device_cdev_open(struct vfio_device *dev= ice, struct file *file) goto err_put_registration; } =20 + /* + * Simulate opening the character device using an anonymous inode. The + * returned file has the same properties as a cdev file (e.g. operations + * are blocked until BIND_IOMMUFD is called). + */ + if (!file) { + file =3D anon_inode_getfile_fmode("[vfio-device-liveupdate]", + &vfio_device_fops, NULL, + O_RDWR, FMODE_PREAD | FMODE_PWRITE); + + if (IS_ERR(file)) { + ret =3D PTR_ERR(file); + goto err_free_device_file; + } + + *filep =3D file; + } + file->private_data =3D df; =20 /* @@ -42,11 +62,26 @@ static int vfio_device_cdev_open(struct vfio_device *de= vice, struct file *file) =20 return 0; =20 +err_free_device_file: + kfree(df); err_put_registration: vfio_device_put_registration(device); return ret; } =20 +struct file *vfio_device_liveupdate_cdev_open(struct vfio_device *device) +{ + struct file *file =3D NULL; + int ret; + + ret =3D vfio_device_cdev_open(device, &file); + if (ret) + return ERR_PTR(ret); + + return file; +} +EXPORT_SYMBOL_GPL(vfio_device_liveupdate_cdev_open); + /* * device access via the fd opened by this function is blocked until * .open_device() is called successfully during BIND_IOMMUFD. @@ -56,7 +91,7 @@ int vfio_device_fops_cdev_open(struct inode *inode, struc= t file *file) struct vfio_device *device =3D container_of(inode->i_cdev, struct vfio_device, cdev); =20 - return vfio_device_cdev_open(device, file); + return vfio_device_cdev_open(device, &file); } =20 static void vfio_df_get_kvm_safe(struct vfio_device_file *df) diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 42c3a9ae3a85..b896c64b11da 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -430,6 +430,15 @@ int vfio_virqfd_enable(void *opaque, int (*handler)(vo= id *, void *), void vfio_virqfd_disable(struct virqfd **pvirqfd); void vfio_virqfd_flush_thread(struct virqfd **pvirqfd); =20 +#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) +struct file *vfio_device_liveupdate_cdev_open(struct vfio_device *device); +#else +static inline struct file *vfio_device_liveupdate_cdev_open(struct vfio_de= vice *device) +{ + return ERR_PTR(-EOPNOTSUPP); +} +#endif /* IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) */ + struct vfio_device *vfio_find_device(const void *data, device_match_t matc= h); =20 #endif /* VFIO_H */ --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D10E74A2E01 for ; Tue, 14 Jul 2026 15:15:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042123; cv=none; b=kTqJfEN0ysB96w7M4a76/iJnIm83IYkJ9WPQGOlFbDiifurS0vtFrO+zPl3WBMd8nvz6kS0Zh8GCf1D24LGHTdd8HK0pD9VdjDJKCnRSqyRdpwYVPf9nbMdgFuQhTCObt63mcKoNS0gNZrEpVWID3dGq9LpwkRPy/CUJDsd6dok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042123; c=relaxed/simple; bh=5cybhF/vC+0B3zoCTwUJxRWAum92bZ7tlfrTb6K+VF4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KL4CJOOq6zPc47lH82+uWCrbzycO3GnDPWZZIO7prQcVAfoezm9amwd85rj8ctUVAEMDRnGLdlZ58Db6AA1PjXc51be1+dwhFeThbeeGYeo4WRgLXTP2PsG+mACZrm8IhRcM5G5PmdvHRZRwQdbt9YQUTt3bLMRSZ5mgWXF4s30= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qNW4KZRb; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qNW4KZRb" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-8488ac68185so6844024b3a.2 for ; Tue, 14 Jul 2026 08:15:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042120; x=1784646920; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=2EHEoo7An3D7oI8NS/D99aTdGzqKD9FxsvRgklpMZ9w=; b=qNW4KZRbrG2kB6r0yIfOt3gdhcotMM0JJeeAGkxE91nPczQMU2SR0Nt88xBXgA7O5m 6AU5D0HIle3T4QU1OD0WDFpriWYIL9p4CYlYBNApsPEkNhsVYtxEoXY1/KLTWypKati0 m+YmhLlVHavoWXie74vUdsNcqOugpx5m2qkf9bRtyZ1K5MMmhVDqKc8g5xIJRHDPrrui G3w3SryPzflqgGGSs3Pq3j/nIYFtn8bNxTscO3eJq76yUyfVNR5fXjnVTgbOJu9zY761 A/GbFx16rTUKw4sSux1c62Gl8WOpB/MVKlq8y6/3dFJ44fXhJ0w9m2iiZf6aJQr9RD0D wL3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042120; x=1784646920; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=2EHEoo7An3D7oI8NS/D99aTdGzqKD9FxsvRgklpMZ9w=; b=YQ5RXvRuUNw4PZ6tr4xdJbVM5nCfOCBoufDnSEL7iu79vZxDHaGPn3/1jT3A3Pas9T PGyvSt6mHnuwUz6vqnsmpMjoPtKVLn2zuDNes6RUJNGKxB3doa3y/Wz0+c1j0TzxDhJx 7jK6Ess9jPdufQ4e6dm/A73RnQrIbLZRk04WIXQMcLqRIkGP6sgc49ojHaUdIFjKef5u ayhs55SBBQvEzRPSOpBG+PmzUz4OzVCOlZv677FIstQCrcXG99O3Ap6Jh9i7lgvDQ2VV T/7xFg0kTwDI7pu8WGBy8Bh4nmbm1OMbFiCnCPR2+6oVo/Z6SzygH9HbaZ30RtAkZJ5e anzQ== X-Forwarded-Encrypted: i=1; AHgh+Ro3L8MgxrqFzCVrWkHHPhCyn4WTSwT+nHSBmXrfKEQi3c2ts15r1pXXX0IJFYT6pgF5Vurl/lXGCp0DQVs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9GaWsS9KYFlAEg5ahsqKeljyP3qaFVyH3LfB5q+pTEoXgg9VX iZEF3k3Jt6+cKgh9Mt+HdryNwrUauFH0afeOi7yWq4PueKkPE4IqRZIbFyfD3wyYgBsfzzuqSv+ b0xqEqeUhNQ== X-Received: from pfbdi5.prod.google.com ([2002:a05:6a00:4805:b0:848:4690:d658]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:c081:b0:84a:2d5c:528c with SMTP id d2e1a72fcca58-84a2d5c562cmr6425028b3a.61.1784042119955; Tue, 14 Jul 2026 08:15:19 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:55 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-11-vipinsh@google.com> Subject: [PATCH v5 10/20] vfio/pci: Retrieve preserved device files after Live Update From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Enable userspace to retrieve preserved VFIO device files from VFIO after a Live Update by implementing the retrieve(), can_finish() and finish() file handler callbacks. During retrieve, find the VFIO device using the BDF and domain information from the serialized state, and open it using the new vfio_device_liveupdate_cdev_open() API. Disallow Live Update to finish if userspace hasn't opened the file successfully or any error occurred during retrieve. Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- drivers/vfio/pci/vfio_pci_liveupdate.c | 64 +++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c index e6eb7f6b53d7..4baac80d03f0 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -39,7 +39,13 @@ * preserved, so there is no way for the file to be destroyed or the de= vice * to be unbound from the vfio-pci driver while it is preserved. * - * Retrieving the file after kexec is not yet supported. + * After kexec, the preserved VFIO device file can be retrieved from the s= ession + * just like any other preserved file:: + * + * ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg); + * device_fd =3D arg.fd; + * ... + * ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, ...); * * Restrictions * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D @@ -93,6 +99,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 #include +#include #include #include #include @@ -223,13 +230,65 @@ static int vfio_pci_liveupdate_freeze(struct liveupda= te_file_op_args *args) return 0; } =20 +static int match_device(struct device *dev, const void *arg) +{ + struct vfio_device *device =3D container_of(dev, struct vfio_device, devi= ce); + const struct vfio_pci_core_device_ser *ser =3D arg; + struct pci_dev *pdev; + + pdev =3D dev_is_pci(device->dev) ? to_pci_dev(device->dev) : NULL; + if (!pdev) + return false; + + return ser->bdf =3D=3D pci_dev_id(pdev) && ser->domain =3D=3D pci_domain_= nr(pdev->bus); +} + static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *ar= gs) { - return -EOPNOTSUPP; + struct vfio_pci_core_device_ser *ser; + struct vfio_device *device; + struct file *file; + int ret =3D 0; + + ser =3D phys_to_virt(args->serialized_data); + + device =3D vfio_find_device(ser, match_device); + if (!device) + return -ENODEV; + + file =3D vfio_device_liveupdate_cdev_open(device); + if (IS_ERR(file)) { + ret =3D PTR_ERR(file); + goto out; + } + + args->file =3D file; +out: + /* Drop the reference from vfio_find_device() */ + vfio_device_put_registration(device); + return ret; + +} + + +static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args = *args) +{ + struct vfio_device *device; + + if (args->retrieve_status <=3D 0) + return false; + + device =3D vfio_device_from_file(args->file); + guard(mutex)(&device->dev_set->lock); + return vfio_device_cdev_opened(device); } =20 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *arg= s) { + struct vfio_device *device =3D vfio_device_from_file(args->file); + + pci_liveupdate_finish(to_pci_dev(device->dev)); + kho_restore_free(phys_to_virt(args->serialized_data)); } =20 static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops =3D { @@ -238,6 +297,7 @@ static const struct liveupdate_file_ops vfio_pci_liveup= date_file_ops =3D { .unpreserve =3D vfio_pci_liveupdate_unpreserve, .freeze =3D vfio_pci_liveupdate_freeze, .retrieve =3D vfio_pci_liveupdate_retrieve, + .can_finish =3D vfio_pci_liveupdate_can_finish, .finish =3D vfio_pci_liveupdate_finish, .owner =3D THIS_MODULE, }; --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15ABA4A33EE for ; Tue, 14 Jul 2026 15:15:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042124; cv=none; b=JH6Z91RbRGPR4SU4HlBbP2uDkaNTfFqvjeetZ7xtqMgPGHjVI58zBOoagqgf+g/JnqFTnoqT5plDzosFzsje6Jf9tQj9cAQlPp/J0jA86szzx/us6hSIhBehpO9vOwOlCxZ3o83IQ16MrnRcg5FoG38QreN41SzkbN7AEeVCHpc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042124; c=relaxed/simple; bh=7r/bDrW/3tccMnKG0Sox3jGGc6LraNqsuBFxlk3AlCo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N3ZJBNyOA+ZZDAdkmWigCQqREfTz8cIewcGoXxmbpe3IPMEhlreweKyPrQcJwKOzvmSwMuF082O3ZR9iiv1nhVVihqoP+jq7tw7HZd+rs4CLVz4x4lTbLi6xfngn25N6x4t5lfemrGFc3Yt39o33ky1ucDZ4tBv69V6POj1p3TA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=omkuNMDG; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="omkuNMDG" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-8488ac68185so6844067b3a.2 for ; Tue, 14 Jul 2026 08:15:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042121; x=1784646921; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=DHOmohYTYSD8/2ryEq7eNVyF04S9VyCIkpjOIEDjYJc=; b=omkuNMDG56nX5cYtruAlP06g4Dz3SQcY195KR+vy2exrp2lg0UHZkis1JYBFzK9YSY i0bcAXBPV+WcKYtGo8aPsO4qcrNdj76isdvHHagy4tWg47MlR01B6KOLiUB8cKzCmnyK OX7dwnb789/1O+y9iQjgJOUDXMaOpXDw+a5DUBmDkfNoKZ+GfXs3GBAu1NinlplSqBwX wFhOth0IqjWQcgmSrf2DS6UWl1KODeI0czSDWI+c/bzwBtnNGBLxy3jcr2zbSwgEoFMk wVgghz7Z1rLmYASz0TE0dEUMbG2h+EM2rccIBJKOLwvQO2TEAlNjcN8K84mitYkY/MD1 KvSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042121; x=1784646921; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=DHOmohYTYSD8/2ryEq7eNVyF04S9VyCIkpjOIEDjYJc=; b=ct/eKlfMk1hL2Pg+MS2EOL6iWtzpP0hFWf7HZlxmAKjlTHUG2rp2TQvnOLvOM3X4Na nJ7/lHkq9iiz5dswFRlJ02ug7z5djUmYqTtGMxTWPO6oFe1PZ0HgvfJ4eK3hfjarMVNI QwSkoUhMUpSheAg3ggUcnRK3rwsob9P1DggZK2kf/uSXM8PpoxF3q2IbLn7S1Zkr4SMs KTMMKSZv2Qfc2NbcP7d9tDvUSPQa5CFYGDuVG0YEUdum3zULJwg6hdUfSCS1My3vKso/ biRbfJdIxlXLMu1kX3wox5YL83kPeApQvjrf0fUIISIGnJ/Y5QZlubgcwWDGRelgqyxM npgA== X-Forwarded-Encrypted: i=1; AHgh+RrQMxtyaEEBZoPgcqUiXxZBRcqzR/ZL5GR9//lcZCt600dy2gmKsJ8zh3CsIi80QtQ9+JiUUE9pHmiMoak=@vger.kernel.org X-Gm-Message-State: AOJu0YwfHgn2+2TqWW8fcHR+ISMzB6BmucqWNqHBWFdIWtS3eLGUXEZZ a9jCdb4vp74gNQDFMO0J9VuGkvk9a1F5HfEMpfR6Md7L/OVCbNHaq4ighSGpCjsgsycZkUKXlPN 9NhnFFET6QA== X-Received: from pfux16.prod.google.com ([2002:a05:6a00:bd0:b0:848:8b16:23d6]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d19:b0:848:76af:db37 with SMTP id d2e1a72fcca58-84889611b82mr12946882b3a.7.1784042120956; Tue, 14 Jul 2026 08:15:20 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:56 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-12-vipinsh@google.com> Subject: [PATCH v5 11/20] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Enforce that files for incoming (preserved by previous kernel) VFIO devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD rather than by opening the corresponding VFIO character device or via VFIO_GROUP_GET_DEVICE_FD. Both of these methods would result in VFIO initializing the device without access to the preserved state of the device passed by the previous kernel. Reviewed-by: Pranjal Shrivastava Signed-off-by: David Matlack Co-developed-by: Vipin Sharma Signed-off-by: Vipin Sharma --- drivers/vfio/device_cdev.c | 8 ++++++++ drivers/vfio/group.c | 9 +++++++++ drivers/vfio/pci/vfio_pci_liveupdate.c | 6 ++++++ drivers/vfio/vfio.h | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index 970932f22ecb..0050f0fe456b 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -49,6 +49,14 @@ static int vfio_device_cdev_open(struct vfio_device *dev= ice, struct file **filep } =20 *filep =3D file; + } else if (vfio_liveupdate_incoming_is_preserved(device)) { + /* + * Since it is live update preserved device, it must be + * retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD instead of + * opening /dev/vfio/devices/vfioX. + */ + ret =3D -EBUSY; + goto err_free_device_file; } =20 file->private_data =3D df; diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index b2299e5bc6df..62b4eaabc829 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -316,6 +316,15 @@ static int vfio_group_ioctl_get_device_fd(struct vfio_= group *group, if (IS_ERR(device)) return PTR_ERR(device); =20 + /* + * This device was preserved across a Live Update. Accessing it via + * VFIO_GROUP_GET_DEVICE_FD is not allowed. + */ + if (vfio_liveupdate_incoming_is_preserved(device)) { + vfio_device_put_registration(device); + return -EBUSY; + } + fd =3D FD_ADD(O_CLOEXEC, vfio_device_open_file(device)); if (fd < 0) vfio_device_put_registration(device); diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c index 4baac80d03f0..f0ea37d98696 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -47,6 +47,12 @@ * ... * ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, ...); * + * .. note:: + * After kexec, if a device was preserved by the previous kernel, attem= pting + * to open a new file for the device via its character device + * (``/dev/vfio/devices/X``) or via ``VFIO_GROUP_GET_DEVICE_FD`` will f= ail + * with ``-EBUSY``. + * * Restrictions * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D * diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index e4b72e79b7e3..496180950eb6 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -11,6 +11,7 @@ #include #include #include +#include =20 struct iommufd_ctx; struct iommu_group; @@ -461,4 +462,21 @@ static inline void vfio_device_debugfs_init(struct vfi= o_device *vdev) { } static inline void vfio_device_debugfs_exit(struct vfio_device *vdev) { } #endif /* CONFIG_VFIO_DEBUGFS */ =20 +#ifdef CONFIG_VFIO_PCI_LIVEUPDATE +static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_devic= e *device) +{ + struct device *d =3D device->dev; + + if (dev_is_pci(d)) + return pci_liveupdate_is_incoming(to_pci_dev(d)); + + return false; +} +#else +static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_devic= e *device) +{ + return false; +} +#endif /* CONFIG_VFIO_PCI_LIVEUPDATE */ + #endif --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5A334A1389 for ; Tue, 14 Jul 2026 15:15:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042124; cv=none; b=Cd8KLoLd7RvFB8JBGL6kuL7nLetxd6xXegARB5cMnhx4gcpaPLAupThH7HRaJI2QlA/B3kqrg9EftynO2D492LElMS9cFdGgkzUgMiVwXGQRKPi81JakiHK5T1BsyIkPIbJYzffWXaEiztnn2yfKIXXe0TxUBjEEjN8ExRGgHpY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042124; c=relaxed/simple; bh=CuAm2Qbad2+WXKfFJI+2wHhyeOkWA/h5aLdYHlm9P2Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hxW/LVXG73qeeIB6cW5Hq8qjvAmdhk1MDbmWcORISmASZpLPk5dDx9ovFIC8WMgORhBLWVDtWtP8MKefNHPrziDrN48YUP7HspRVXHqbmgTcb7oStspA1gehwr42+HNU8i5flfS4rP9SyahKyq8NTdD1OqcPa84meGFZrVncQLI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CNoqJHkl; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CNoqJHkl" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-8482b95574dso5639356b3a.1 for ; Tue, 14 Jul 2026 08:15:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042122; x=1784646922; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=14GlidItFhGsteVDeFZQr0b0Q8bnxkhoEthNZJCEcqI=; b=CNoqJHklWgUNa0uJQRa6gWAjbX7l5cQUpLhQUWBZedfLTgw5HSndhK8z9cg+T7fjTn T1VE89VifZbgnIeI9jQTOonfM7E94NhMiD8IIqdOcPMmVesd7KYP0FeA2wUkP9V8kNgZ a4Fz5TjqZB6lw5Gqz5FxlQI/qIanJf92Jfaeg0Kla6TETUaDvZlPKy4FECd5pbIQioE7 bNJmQ8uKIhCx1OItAr7IrdVuA2F9XHfUIkc9uw36HE7SLGYxM7HNZ76DFz5Y7UgH62dR IiYcyLHfKjpnauK2CCCbAkuVlZ18dZk46swv8Lri5G6dUzcHK1TpCGYKIW9a/vQLN1xp 03NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042122; x=1784646922; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=14GlidItFhGsteVDeFZQr0b0Q8bnxkhoEthNZJCEcqI=; b=O4/LKPuPLs8WXnuUsYvxp0vrjONn7nT5Y1hcH25yUaWWnQ/y+GcKuu6fILcRn5AG3y EySJB7Lr4iLEdLDoPRFEdWvIU7WPYBlMxyyQx/InHCu3AVvGXjfVbQG0vkoSFQ2uKIH5 y3EAI9LTAf7Q1urMb0XTGY+HMIy5n8mZus8XhNg5bZmAfS4peb+Ea+t8vnTYbYoeY5cI 4LVyKnBHfyXKw/oWSppnkYuV6rd8CeS95KHkcydILUDt2U07pUIqkqHnacfFyeVkjQcl MNj5OkI0H35Vd6IckcWmaZi4Pr/eyRSpyGIx2qWvGpXFBLuZWCRPKd9YzO6U6wMWCheJ p33w== X-Forwarded-Encrypted: i=1; AHgh+RqUTA66lm8f+FZdhqsM3G1o2XLJUMobm/Ky/rj1ZOTZRRIGDLvGrIw4219msVMhvYIOIOLR/gsNZawl7JA=@vger.kernel.org X-Gm-Message-State: AOJu0YzUCXLHcSlJX4wIEU+2U1pDR4+z1OH/FiFxBvP551KbmFnjCmLU LOCfxwHOObLPvFxDO2TG9mH+8T5W1z+u6i+hrMPmmCc5OGz7yzAycuflCR2ajbgWTB3zegSYfGn WEKpG5v6i5A== X-Received: from pfoa23.prod.google.com ([2002:aa7:8657:0:b0:847:99d5:ef10]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1ca3:b0:845:d286:1fbe with SMTP id d2e1a72fcca58-848897aa414mr11307298b3a.60.1784042122068; Tue, 14 Jul 2026 08:15:22 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:57 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-13-vipinsh@google.com> Subject: [PATCH v5 12/20] docs: liveupdate: Add documentation for VFIO PCI From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add documentation for preserving VFIO device files across a Live Update, generated from the kernel-doc comments in the code. Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- Documentation/core-api/liveupdate.rst | 1 + .../driver-api/vfio_pci_liveupdate.rst | 23 +++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 25 insertions(+) create mode 100644 Documentation/driver-api/vfio_pci_liveupdate.rst diff --git a/Documentation/core-api/liveupdate.rst b/Documentation/core-api= /liveupdate.rst index 2bce2644eba2..20f1d2e20508 100644 --- a/Documentation/core-api/liveupdate.rst +++ b/Documentation/core-api/liveupdate.rst @@ -38,6 +38,7 @@ The following types of file descriptors can be preserved :maxdepth: 1 =20 ../mm/memfd_preservation + ../driver-api/vfio_pci_liveupdate =20 Public API =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D diff --git a/Documentation/driver-api/vfio_pci_liveupdate.rst b/Documentati= on/driver-api/vfio_pci_liveupdate.rst new file mode 100644 index 000000000000..1098b84e5ecd --- /dev/null +++ b/Documentation/driver-api/vfio_pci_liveupdate.rst @@ -0,0 +1,23 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +VFIO PCI Device Preservation via LUO +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. kernel-doc:: drivers/vfio/pci/vfio_pci_liveupdate.c + :doc: VFIO PCI Preservation via LUO + +VFIO PCI Preservation ABI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + +.. kernel-doc:: include/linux/kho/abi/vfio_pci.h + :doc: VFIO PCI Live Update ABI + +.. kernel-doc:: include/linux/kho/abi/vfio_pci.h + :internal: + +See Also +=3D=3D=3D=3D=3D=3D=3D=3D + +- :doc:`/core-api/liveupdate` +- :doc:`/core-api/kho/index` diff --git a/MAINTAINERS b/MAINTAINERS index 77a35742de0a..43b3165d1c68 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -28328,6 +28328,7 @@ T: git https://github.com/awilliam/linux-vfio.git F: Documentation/ABI/testing/debugfs-vfio F: Documentation/ABI/testing/sysfs-devices-vfio-dev F: Documentation/driver-api/vfio.rst +F: Documentation/driver-api/vfio_pci_liveupdate.rst F: drivers/vfio/ F: include/linux/kho/abi/vfio_pci.h F: include/linux/vfio.h --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC634495515 for ; Tue, 14 Jul 2026 15:15:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042126; cv=none; b=Gs/0XDpqb0D9uYysgkzRM1VZl8CoCDF26oXgcBusmhdpCXfod/zhIsZ/uQPpOlNRSjth/PgDawf83haaL8udWRC59vcRwofV1Rb09zjXuNGaMw32sKt9SLSxotNp02xIz2VtO9Q4DmlkGtqNtC2FFNZj+vLjczxYMG3Ukv34zKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042126; c=relaxed/simple; bh=ikkIKALeVfseIqEqO/ki0c8rJ+D/mTjIlQS+Bh7TNH8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fVxBP7BNp4DWq3VcDnGobTnY9rwFc3ZlWeINn+zuivIotH7B0mhHL0rlUvJ4r3KnuWSyHK8G7hDxFTQPGV8eJC3S2BlnrFHpfYw/4X9Cat9xenaouxgMY1uEsTvo5LfD1hzF9w0rMieGbtYR/KdTErq1zkQt/NF3F42pO/y34Ec= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Saua0PpR; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Saua0PpR" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-8484b57b98bso8661111b3a.0 for ; Tue, 14 Jul 2026 08:15:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042123; x=1784646923; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=6euMhf7mogWwsfwAzliK6IQ37N0mgMRUyNxCkXOGnkg=; b=Saua0PpR+1WYgK5nic0uAf4ibCeyitepgMldrbhBt/QtD2vnwVJkePQf9zltqpv/4N kLOGC3oPS0zYBJqHKeW5hKdQfgyEo7g1wu9SXR0erxCzK7kq2ovXNbUZtmThYc0geFAN FvlEuxvfyHnfGP3MIzLPkDOEGB1X8c/XskYncNGB55TH2d2tzviTY1+6gtcapTPWKDwZ jEIk+gedgykX+vPlR/ymnkKhi/xv8eCzNrr4ApDkxAt0HYthyyE2AmVOiLuB7Q4oRNeM 7/SLGMQPm2dRUXb2ECiwosQSoXC4S3mBUNC//pJrk0M9gkj/D84hmJMFQ7Yi10/xCsDx OldA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042123; x=1784646923; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6euMhf7mogWwsfwAzliK6IQ37N0mgMRUyNxCkXOGnkg=; b=Zb2xVIHf+GWMdktDgd+aQhr5Yi+sSCNv6Ip0RkuYg9GhU4z40OBWYsGXtxzkspaQ/W oX/K6Sd/EGHfxCNQJi+z5RmZrBO/st7selIxJzsHC5/Npkvres0ZEsR/gbo6DOtLA5tl OtxaO//ioPV58o4FqB8sW44WlVbWljV+6kYoMjyEo9mT17NY14zd+iMlHTLVddGKFnSH 6+yCwFTj42lBs8nZFL/rH+0NkNV+KT9uxZgjiFXmDjdHcw7vsACrNfR6RCvEYCw35IxF Cqy+kSc/vnZvlqMPULkjhr2/F0CvbxOj6yHZESab/XoWCYdSdgCvWNeA/IPfoCHeP4eZ W5og== X-Forwarded-Encrypted: i=1; AHgh+Rr48ABG+IcpcPlE2BtAXmr+X8//mz7vERM9nhxWjpx2F1YWmfHZ4oyoDaPlJD3eM7RjHuyLr8DfLfCmWhU=@vger.kernel.org X-Gm-Message-State: AOJu0Yy9Omtux7gInMX7ton6JPb9/HXTOqARW3/KhY5RsG5x9iaDpR7L slyFIma3Qj3ARTiGBHSA6lAePn51rC8YIhEW6KI9ib2ALlrEw9EHef+Aw2bncjae76rfppy8eb3 toVpv01GPGA== X-Received: from pful22.prod.google.com ([2002:a05:6a00:1416:b0:847:7fb7:7370]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e87:b0:848:6dd5:f69 with SMTP id d2e1a72fcca58-84a516062dfmr4085012b3a.42.1784042123043; Tue, 14 Jul 2026 08:15:23 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:58 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-14-vipinsh@google.com> Subject: [PATCH v5 13/20] vfio: selftests: Build liveupdate library in VFIO selftests From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Import and build liveupdate selftest library in VFIO selftests. It allows using liveupdate ioctls in VFIO selftests Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- tools/testing/selftests/vfio/Makefile | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index e6e8cb52ab03..4c8a30f950c4 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -21,6 +21,7 @@ TEST_FILES +=3D scripts/setup.sh =20 include ../lib.mk include lib/libvfio.mk +include ../liveupdate/lib/libliveupdate.mk =20 CFLAGS +=3D -I$(top_srcdir)/tools/include CFLAGS +=3D -MD @@ -31,14 +32,17 @@ LDFLAGS +=3D -pthread =20 LDLIBS +=3D -luuid =20 -$(TEST_GEN_PROGS): $(OUTPUT)/%: $(OUTPUT)/%.o $(LIBVFIO_O) - $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ +LIBS_O :=3D $(LIBVFIO_O) +LIBS_O +=3D $(LIBLIVEUPDATE_O) + +$(TEST_GEN_PROGS): $(OUTPUT)/%: $(OUTPUT)/%.o $(LIBS_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBS_O) $(LDLIBS) -o $@ =20 TEST_GEN_PROGS_O =3D $(patsubst %, %.o, $(TEST_GEN_PROGS)) $(TEST_GEN_PROGS_O): $(OUTPUT)/%.o: %.c $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ =20 -TEST_DEP_FILES =3D $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O) $(LIBVFIO_O)) +TEST_DEP_FILES =3D $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O) $(LIBS_O)) -include $(TEST_DEP_FILES) =20 EXTRA_CLEAN +=3D $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES) --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CA7949250A for ; Tue, 14 Jul 2026 15:15:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042127; cv=none; b=BUYSjem1AQ79H8V8Fp/Hco+tdfEiYmjCjia3R3rJPU4nTcEzPzjVQxv4E7xc3rqjGy1pAgwyl/Zzu3K+TFYsXqL9Ome3XxzvlHzuQzVwLuu6hnqMwrtoa9KuZ8ScMeCi8lTUPhW1PDR+dNNVoydtxvAy+EUomAM6a61vofiAo1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042127; c=relaxed/simple; bh=23AOW+ibIYHlba5Hxvem4H4THwWt1BCwNsb6ZEVk/Zs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Oas7Vap0K2oiK1/B2XgBvhbYI5XDNAbpqnbm6WixKIHdes6Se+ZlEWSeKCwbwbadEkjsggEbhedm5OjU+USkvIHKvdbzIVEJ3xVCMj4NURzY+JLZ++xetVamkrYsy21EU5mZsCR46U4kXb9ppEeJiN5l34+odSNCFtDR1SotlSI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gKDJVSiw; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gKDJVSiw" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-8485b7e18b4so7807996b3a.1 for ; Tue, 14 Jul 2026 08:15:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042124; x=1784646924; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=gKj/zyluhYMMwtYKfEtevhbjKc1OLf8e8hVjMJ10va8=; b=gKDJVSiwckvNFlqZqnXWEemG3VuCHtNqgnc2Or1Rrhdx4WZJALFFsNDP/w4+GN4YdX 66cNRj3Ivt1B6lzQHGbowlZTxWGUQ8iuPlRP1W8o9LaQ3eifb2x0SAMKQNc/8++FKA3F cXAcepvzpqF26KpdVbJJWrXM2gDqqDu3e9uF4hI5NueF/bs0ceRijxcEA1swsungekVS 028IwB32vzDSY+7aN9zGukSQCaI56Endr1BBm9HbrIa0/ySWjbVeNMLL4lCprUa8ovVI L2F9R9b8Gt5OQ+00Yp2r8yQceJnnbrYkblAw1r69nKEx2C8S/Xd1FYuvgQ2VqxRbs/kA rmIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042124; x=1784646924; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=gKj/zyluhYMMwtYKfEtevhbjKc1OLf8e8hVjMJ10va8=; b=GNuS7G2MAd1WRxPwjZG+nY/I3c4/XfLr5Gn09ildZckf3+4cGDBUSA4Mqr8Bq5+Z19 pvg6WY9JRStGBox4Y4c+LHYQUUSXDkiooy1hlBig/YVVg4esFhf+8JowUHhPbnqGIWru bqGAjd9xGTOY0zkacviAx8Z3ZOvw7FxnB7iiFteT8hFYRw8bVsS+1OpXWU1EiFoWzs2Q 8J7/brw6sXm5SmZlbqDGzGLfbkCbRAqV/wql31zFLmzkCVkgg06n3Ptp9d6OChlSVVfp eUlW2XhwMVUl1vScLoEtJwn05mEpisNRajJtesaI7OP6ohIF3AouNwnYDRC+SM2Leg/J Ebww== X-Forwarded-Encrypted: i=1; AHgh+Rqo8mxRWg5FsSRoiuZr3nFj7n4W8O27ZJG0eskWi/5WsqbjogVcSQIIpsb2Cb0MMf1RmCu8TE/xVBvxMys=@vger.kernel.org X-Gm-Message-State: AOJu0YzvQBXoqWKXymJtLmfImN0A+xPJudsnmdocxYQAFPDIFzjJjsTo SpVEvKybHXe9Oc8fFbjptGLaO96Q1BtRnkbA9cfv/KRXIVkd+DxqqIPfyfuyK4wywg+czqiOMEl W115c4qrwpg== X-Received: from pfbhx15.prod.google.com ([2002:a05:6a00:898f:b0:845:e683:1287]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:b50:b0:848:6698:2d5a with SMTP id d2e1a72fcca58-84889659202mr12144241b3a.20.1784042124125; Tue, 14 Jul 2026 08:15:24 -0700 (PDT) Date: Tue, 14 Jul 2026 08:14:59 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-15-vipinsh@google.com> Subject: [PATCH v5 14/20] vfio: selftests: Add vfio_pci_liveupdate_uapi_test From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add a selftest to exercise preserving various VFIO files through /dev/liveupdate. Ensure that VFIO cdev device files can be preserved and everything else (group-based device files, group files, and container files) all fail. Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_pci_liveupdate_uapi_test.c | 97 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_t= est.c diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 4c8a30f950c4..dc06fddcf855 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -13,6 +13,7 @@ TEST_GEN_PROGS +=3D vfio_pci_device_test TEST_GEN_PROGS +=3D vfio_pci_device_init_perf_test TEST_GEN_PROGS +=3D vfio_pci_driver_test TEST_GEN_PROGS +=3D vfio_pci_sriov_uapi_test +TEST_GEN_PROGS +=3D vfio_pci_liveupdate_uapi_test =20 TEST_FILES +=3D scripts/cleanup.sh TEST_FILES +=3D scripts/lib.sh diff --git a/tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c b= /tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file mode 100644 index 000000000000..f81c8ab21db3 --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include + +static const char *device_bdf; + +FIXTURE(vfio_pci_liveupdate_uapi_test) { + int luo_fd; + int session_fd; + struct iommu *iommu; + struct vfio_pci_device *device; +}; + +FIXTURE_VARIANT(vfio_pci_liveupdate_uapi_test) { + const char *iommu_mode; +}; + +#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode) \ +FIXTURE_VARIANT_ADD(vfio_pci_liveupdate_uapi_test, _iommu_mode) { \ + .iommu_mode =3D #_iommu_mode, \ +} + +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(); +#undef FIXTURE_VARIANT_ADD_IOMMU_MODE + +FIXTURE_SETUP(vfio_pci_liveupdate_uapi_test) +{ + self->luo_fd =3D luo_open_device(); + ASSERT_GT(self->luo_fd, 0); + + self->session_fd =3D luo_create_session(self->luo_fd, "session"); + ASSERT_GT(self->session_fd, 0); + + self->iommu =3D iommu_init(variant->iommu_mode); + self->device =3D vfio_pci_device_init(device_bdf, self->iommu); +} + +FIXTURE_TEARDOWN(vfio_pci_liveupdate_uapi_test) +{ + if (self->device) + vfio_pci_device_cleanup(self->device); + if (self->iommu) + iommu_cleanup(self->iommu); + if (self->session_fd > 0) + close(self->session_fd); + if (self->luo_fd > 0) + close(self->luo_fd); +} + +TEST_F(vfio_pci_liveupdate_uapi_test, preserve_device) +{ + int ret; + + ret =3D luo_session_preserve_fd(self->session_fd, self->device->fd, 0); + + /* Preservation should only be supported for VFIO cdev files. */ + ASSERT_EQ(ret, self->iommu->iommufd ? 0 : -ENOENT); +} + +TEST_F(vfio_pci_liveupdate_uapi_test, preserve_group_fails) +{ + int ret; + + if (self->iommu->iommufd) + SKIP(return, "iommufd-mode does not have group files"); + + ret =3D luo_session_preserve_fd(self->session_fd, self->device->group_fd,= 0); + ASSERT_EQ(ret, -ENOENT); +} + +TEST_F(vfio_pci_liveupdate_uapi_test, preserve_container_fails) +{ + int ret; + + if (self->iommu->iommufd) + SKIP(return, "iommufd-mode does not have container files"); + + ret =3D luo_session_preserve_fd(self->session_fd, self->iommu->container_= fd, 0); + ASSERT_EQ(ret, -ENOENT); +} + +int main(int argc, char *argv[]) +{ + int fd; + + fd =3D luo_open_device(); + if (fd < 0) + ksft_exit_skip("open(%s) failed: %s, skipping\n", + LUO_DEVICE, strerror(errno)); + + close(fd); + + device_bdf =3D vfio_selftests_get_bdf(&argc, argv); + return test_harness_run(argc, argv); +} --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9FCC2D97AA for ; Tue, 14 Jul 2026 15:15:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042127; cv=none; b=AgpB6HQLqgd+dFe9CBSEdIKKB1Vno29Em6E5sU1gyObGJ64VVlrSjY5a8wge7tL/I4mlUrJiIPepO3lcz9hkipVermVzTihQIqydYd0QgmKu4ZnXIr0AM0j3RnLU0WjOYoc4DmtWPqCKg33dd6hxkI3VGFDoxPJYi+A6g9EboO4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042127; c=relaxed/simple; bh=18is/Qt0S+jmUTzhdeluE3sxLqScHvGF040iAxoJfOk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RICAunaGbIy8NHBotpTjyV4cLPc45DihMia7+FzTNBRBn9q2m8Ldy2crKJ/dtSITqti2mTzGvFb9+HwNxkXAsUODfZ+A+QcG8BW4tQz1ZBw1cpwFgHE3s58TfXyFAsdQp6hOetrjvl5+pb9AVyG4jSghH7wrHArBA7LJK3tRY3A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hTpjetri; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hTpjetri" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-84877b362f6so7350065b3a.2 for ; Tue, 14 Jul 2026 08:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042125; x=1784646925; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=JgKrbgFKZxbxCBaliRqkNgo/i0ql++Rr7PTW+pok5fM=; b=hTpjetrirdlqoEFS/f56wAzOxHd4w/QamnydPc19Y/LJtawKyAhOx4W4TPKTm73c6g Eua067ADISKd0fsw9arYvGfi9pU9Ms/x+lq1pMiAqo/Z9YAjcekHMbY0gmg5y1osaHuE IbO9LCx7dFdg6+HVGFeaYWeEj+4UyQZqBcdauktw+pWJVmBvpdJMGA9lH6amUfhmmJVv 9VGDpPsqqHThH/1ymD/XkugLKbs0E8SjGMnO6cOPrta2jHCtNKunARRdfV15rBCs6TI8 Wo842BcgAiRv8Ch0uxDISIRhQ1TmoakAVDXJlOMQwXxIYCzaRw9IFomg/kd7JNjR8ozX E5yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042125; x=1784646925; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=JgKrbgFKZxbxCBaliRqkNgo/i0ql++Rr7PTW+pok5fM=; b=slPTFJrGO3CGuuK2505ow0CG+9lSN0e9SYUGwymqI4ddkz8YRhfjUtxE5FcdfWkCCX KOPxt1u+vVHXMnH/5u54TZfK+T+F8RGdhWAHihBGO2LK20/1AQWseL/yjJTj2m6pKC6T JXoRZW8OrMN8pIyg1CTPTPmS8OwRt95EleOW1pIfo0BezNWIi6C+We6LYIRavRQ8edGd QVn3RbQPh63fEdL158xxJI/0gvbTYsAQCELVNhkTklZJK2Z0RCCFfzgAa/CAPMyvOEVS 2K1IF38OQnsf8rhSvp28Hdox+iXPuLJA1mtCSEBFtMWAoN/flLsvn8Mt55y4AL7FrmaG uBWg== X-Forwarded-Encrypted: i=1; AHgh+RqhrisvnzMOqQwBJKHbBJpYCpfIpjcVYB/2gCP9KaKNe8pzVcTFy7Dq/OpHjIY/lSh+r8tPAtLIkNuE5m0=@vger.kernel.org X-Gm-Message-State: AOJu0YwC9vydIoAv8oT40T1J38HrZM0oLlzltZi7An701cdfES8y2x0L RrXjFDehK3rWP6Jbq1DVRtnTNuICjDdW+SjCtX9TLXO9ZvQZHiSmMSjPTgkEuUiNOuxniA/Pdcl uTUwOVdqHqw== X-Received: from pfbll1.prod.google.com ([2002:a05:6a00:7281:b0:845:e386:e036]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1f14:b0:845:e9e5:cdec with SMTP id d2e1a72fcca58-84a559bb7dfmr2554072b3a.62.1784042125277; Tue, 14 Jul 2026 08:15:25 -0700 (PDT) Date: Tue, 14 Jul 2026 08:15:00 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-16-vipinsh@google.com> Subject: [PATCH v5 15/20] vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use the given VFIO cdev FD to initialize vfio_pci_device in VFIO selftests. Add the assertion to make sure that passed cdev FD is not used with legacy VFIO APIs. If VFIO cdev FD is provided then do not open the device instead use the FD for any interaction with the device. This API will allow writing selftests where VFIO device FD is preserved using liveupdate and retrieved later using liveupdate ioctl after kexec. Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- .../lib/include/libvfio/vfio_pci_device.h | 3 +++ .../selftests/vfio/lib/vfio_pci_device.c | 25 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_devi= ce.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h index 3eabead717bb..66fc29662daa 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h @@ -40,6 +40,9 @@ struct vfio_pci_device { =20 struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu); void vfio_pci_device_free(struct vfio_pci_device *device); +struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, + struct iommu *iommu, + int device_fd); struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu); void vfio_pci_device_cleanup(struct vfio_pci_device *device); =20 diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 94dc5fcecbeb..a6e0e7c6e18e 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -414,9 +414,13 @@ void vfio_pci_cdev_open(struct vfio_pci_device *device= , const char *bdf) } =20 static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) + const char *bdf, int device_fd, + const char *vf_token) { - vfio_pci_cdev_open(device, bdf); + if (device_fd >=3D 0) + device->fd =3D device_fd; + else + vfio_pci_cdev_open(device, bdf); vfio_device_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } @@ -440,16 +444,20 @@ void vfio_pci_device_free(struct vfio_pci_device *dev= ice) free(device); } =20 -struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) +struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, + struct iommu *iommu, + int device_fd) { struct vfio_pci_device *device; =20 device =3D vfio_pci_device_alloc(bdf, iommu); =20 - if (iommu->mode->container_path) + if (iommu->mode->container_path) { + VFIO_ASSERT_EQ(device_fd, -1); vfio_pci_container_setup(device, bdf, NULL); - else - vfio_pci_iommufd_setup(device, bdf, NULL); + } else { + vfio_pci_iommufd_setup(device, bdf, device_fd, NULL); + } =20 vfio_pci_device_setup(device); vfio_pci_driver_probe(device); @@ -457,6 +465,11 @@ struct vfio_pci_device *vfio_pci_device_init(const cha= r *bdf, struct iommu *iomm return device; } =20 +struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) +{ + return __vfio_pci_device_init(bdf, iommu, /*device_fd=3D*/-1); +} + void vfio_pci_device_cleanup(struct vfio_pci_device *device) { int i; --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4657F4C77D8 for ; Tue, 14 Jul 2026 15:15:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042130; cv=none; b=QlCgX9WvGsDqkXC9ezlOqd8q/X6PTISSsfm/MntBZy6TGS04vZIoSBPb93F35vDd0GD0C4JohZnIo3qmqiHF00fOgb38KrtXKh00K3UFsaqRcBsJe2MeX4GHs8H9t/4QKGekgaCfIfZCE+RrcQD1DvEo+tTg35GrCYgeMkySwxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042130; c=relaxed/simple; bh=D8+CDqnTMa7BMe+HDcJm6O9ksq5NtEkbNPKxr0uj1to=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QOXFknWJy5CVclXJorJlsoDunnpDQ3Y3RKROSakSEBIRul8U7iQk8qU//6ZVQ2lD4R2UX+8eHJh7ks8eS3EAbgj55nNICj6QUa/iAh8CThC8Fw3iOXVdP8vtbMr6HbqtBm3v0nQ0484z9a24EPeWlKrvTilWw+SKR7Lru6nWlNI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Sn6TTR9c; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Sn6TTR9c" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-cb11535e6a1so1034164a12.0 for ; Tue, 14 Jul 2026 08:15:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042126; x=1784646926; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=w4NITAiOyX61WSGfTGRgV5OG/+gstw4nR1bSZsXufP8=; b=Sn6TTR9cwxZ5Ug2u4keqR/9NKnAV88TIe1ZjTJeR7Y1p6KCi+6fU1Hg4h6s7gR0/+c JHo669hyUDbiWenPJIxgTB83PR0lZI/9pIFlbgeEU2ZjR0gUHt7eL6Txii2g0xXXdcJL NXZR142BCT7pssq5sPMxeisGNiMNNWF77w8wlLWfKTTn3UwWd3tLWS39NZM4LHNRbP6a LcVai/YSlqX2KNmXEscr5aF2MmPmjudL19mnNal3QqPsEBXaPAzGav59S3xLB+GHkuBz 4rZWSosNTXx6YY1LsjnsqG5z5uMOTp7oMqINa9BezPwziOj/01Lvviyz5E8CElPLNHDJ s+TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042126; x=1784646926; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=w4NITAiOyX61WSGfTGRgV5OG/+gstw4nR1bSZsXufP8=; b=I5ifEtvnrNzl7us+Sjc8/eBlMIW9ri3TTMq8ynexM2il5Un1sfntXndqKySuo5vBrY ex22Q/HfU/rcMqDwshdD8kxUOghU1ahsk3OsL8I96Z/IWZht+ix/WLZi/vNhBQ0r5swz /g7Wp1Hdmli3UJoFWQ+4aWmi3zjfi9F+rg4b2YH0ofbWKl+61DudvATigCiXmPYc2cNR xVsN5G8xMTqUp5jdzPenjBARhyrJHNgw5RVzwU2rNc00/R70Hm+RfNyXU8b43urSJL75 TR7EmRVPmJiTokWtgHhpgbUOtnjfAtVpOtUF2Hh/0rDB9cW6jI0ciX/JkpRznD8/qLUg 1ROw== X-Forwarded-Encrypted: i=1; AHgh+RpGg2hlwEJJEgZ8pnDfl9Df1S7GKFR+RRnNqOrM6v+UrXl2VwodjS3FeBy4B0dx5Q5SA3K2hLGDxHm58ck=@vger.kernel.org X-Gm-Message-State: AOJu0YyU/AJybMhYQ81sDX68elJie+3zMTYWxIF1AO5f59W4ThS5Vh3j h6YXamehLpgcNV5oKBBKFcD8woJzi/26GCzaLuvGPU1m+0Ke2TDRhl1JoTeZZBwOjb4vtpdjMOK b3nx1Tl0N6g== X-Received: from plbkx14.prod.google.com ([2002:a17:902:f94e:b0:2cc:61be:8bbb]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:db11:b0:2c9:d539:61e7 with SMTP id d9443c01a7336-2ce9ead007bmr140240975ad.19.1784042126213; Tue, 14 Jul 2026 08:15:26 -0700 (PDT) Date: Tue, 14 Jul 2026 08:15:01 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-17-vipinsh@google.com> Subject: [PATCH v5 16/20] vfio: selftests: Add Makefile support for TEST_GEN_PROGS_EXTENDED From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add Makefile support for TEST_GEN_PROGS_EXTENDED targets. These tests are not run by default. TEST_GEN_PROGS_EXTENDED will be used for Live Update selftests in subsequent commits. These selftests must be run manually because they require the user/runner to perform additional actions, such as kexec, during the test. Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- tools/testing/selftests/vfio/Makefile | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index dc06fddcf855..591f1a52c310 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -36,16 +36,18 @@ LDLIBS +=3D -luuid LIBS_O :=3D $(LIBVFIO_O) LIBS_O +=3D $(LIBLIVEUPDATE_O) =20 -$(TEST_GEN_PROGS): $(OUTPUT)/%: $(OUTPUT)/%.o $(LIBS_O) +$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: $(OUTPUT)/%.o $= (LIBS_O) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBS_O) $(LDLIBS) -o $@ =20 -TEST_GEN_PROGS_O =3D $(patsubst %, %.o, $(TEST_GEN_PROGS)) -$(TEST_GEN_PROGS_O): $(OUTPUT)/%.o: %.c +TESTS_O :=3D $(patsubst %, %.o, $(TEST_GEN_PROGS)) +TESTS_O +=3D $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED)) + +$(TESTS_O): $(OUTPUT)/%.o: %.c $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ =20 -TEST_DEP_FILES =3D $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O) $(LIBS_O)) +TEST_DEP_FILES =3D $(patsubst %.o, %.d, $(TESTS_O) $(LIBS_O)) -include $(TEST_DEP_FILES) =20 -EXTRA_CLEAN +=3D $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES) +EXTRA_CLEAN +=3D $(TESTS_O) $(TEST_DEP_FILES) =20 endif --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CF094963BB for ; Tue, 14 Jul 2026 15:15:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042131; cv=none; b=hxtsRAcJAA6hbrkN2A58mSg6Q+4O/3ni4/GwYrj/bs7tsp7MyQjNTwDAUzmj5339DtB1C7zVNgfTdQNRvu9RhiDG+6JCDt0mQzoaSIVeVib2gSLYtjjUXKRtMTdg5YfVQ75Sdckqblqzkyc1FRTVN+hibIhgrfB1YOXfS+Ac3Mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042131; c=relaxed/simple; bh=+ruXTCO9gRYmmLH2Y2rM+2QpabS7Kd0bF79dd5M3NDg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DNgoITuIObEBFiAAz0fHG61nA2yWV7n8nfm8Y174DvxoWFaYdqSfZN0f3vXJDdbdFcLgJmeHKfnSos/dzQOI1IFJYJuLOpdQhAREXwzbie4JFQ0Lf8cQT2dkQo1/fGjq9Nowiq2KPOnhcRlHpZaCVd/qDqCcIx8agrQIJpQ2w7M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ql8ar7Sk; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ql8ar7Sk" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2cce406883eso10086825ad.2 for ; Tue, 14 Jul 2026 08:15:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042128; x=1784646928; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=M0/6OkiDA7soDeG3nK/kM6W+DTMONIYlLqtJZ7GmGRw=; b=Ql8ar7Sk3nnxrxubtlG3kuXL7q92+JVN60sdnMFTnt39qHvECD2br4viqvIYTko1O3 wgja7/919i4Az0J/NPA8+fKLsjiHVWUi8pKRpnMTkVDOIosZltDaTYqe5P6KhPGcd6ef uysCK/hMncMvENtQZgkPNpWYK5ufI5FNRE5ZVMcDRKWMkR2PSeBvplOPg/pYzFbrP+/N KbOk+NoxgGBllQVEwNbuhL6lFCNMvGtOJQ+GNHmp/kVCUuISZ4MAxMaaMMc49Xv2cF0P VuYICq13x83zbhbPM4VcKUnfm+sukSss+TCAHtzwVBWZaekq0dnxWNCIYaIq9Gcfy3U+ +VVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042128; x=1784646928; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=M0/6OkiDA7soDeG3nK/kM6W+DTMONIYlLqtJZ7GmGRw=; b=Vg0351yDIurnnWgA0WHivei6r1V3sSp+0Wu7zYpc4O7r6PuzFIIs37eg3iLVreHzDB IagCYY1RqcHyrhME3pykyAww9p6qd7xY/OeGwQcRijCRv8SispsOwNp+RS89WhmntdnN K4FtlN5w5ikA77+DdU0w7Iwv3onC9+z0TV7APuCYDmbFHgr4vIoBOof8rll7ARN9IVuO bWhroS9KsNGJLg84mzaqKjusHoS2NW116PPEa/Zd0q10CGd90BXl7PnRulmNckAV21oa sgcToa+DwXX3iZGdoQBEQXbmASEV9nUVVXIIBcDznO/xlfCGBftm+rMC2p4dW//aw3yB mkcA== X-Forwarded-Encrypted: i=1; AHgh+RrJc+/LKacqePgEXudYw8M9bxHoOtfnLvl412cTuZh4gmFNXTIvCFeEhuqPcvOEpimvh4YV8TzGT3qOXjM=@vger.kernel.org X-Gm-Message-State: AOJu0YzDvMLURK7C8lBWKQOBqoXuUY60hyVWKNKikkp1EviRjcjr2ilW yFXP0PGVCUH9cmVuVBfrItEJ8MTvgs7xne7X914rFbeJIMAci9tD9m5dRk2WFRZp/jKngUquMXE 8LrP56WmkKg== X-Received: from plei5.prod.google.com ([2002:a17:902:e485:b0:2cb:d8fb:3da3]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1b08:b0:2ca:17a8:cfce with SMTP id d9443c01a7336-2ce9f2852b2mr140988325ad.29.1784042127310; Tue, 14 Jul 2026 08:15:27 -0700 (PDT) Date: Tue, 14 Jul 2026 08:15:02 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-18-vipinsh@google.com> Subject: [PATCH v5 17/20] vfio: selftests: Add vfio_pci_liveupdate_kexec_test From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a selftest to exercise preserving a vfio-pci device across a Live Update. For now the test is extremely simple and just verifies that the device file can be preserved and retrieved. In the future this test will be extended to verify more parts about device preservation as they are implemented. This test is added to TEST_GEN_PROGS_EXTENDED since it must be run manually along with a kexec. To run this test manually: $ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test --stage 1 00= 00:00:04.0 $ kexec ... # NOTE: Exact method will be distro-dependent $ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test --stage 2 00= 00:00:04.0 The second call to setup.sh is necessary because preserved devices are not bound to a driver after Live Update. Such devices must be manually bound by userspace after Live Update via driver_override. This test is considered passing if all commands exit with 0. Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- tools/testing/selftests/vfio/Makefile | 4 + .../vfio/vfio_pci_liveupdate_kexec_test.c | 89 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_= test.c diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 591f1a52c310..a2f0a459e1f4 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -15,6 +15,10 @@ TEST_GEN_PROGS +=3D vfio_pci_driver_test TEST_GEN_PROGS +=3D vfio_pci_sriov_uapi_test TEST_GEN_PROGS +=3D vfio_pci_liveupdate_uapi_test =20 +# This test must be run manually since it requires the user/automation to +# perform a kexec during the test. +TEST_GEN_PROGS_EXTENDED +=3D vfio_pci_liveupdate_kexec_test + TEST_FILES +=3D scripts/cleanup.sh TEST_FILES +=3D scripts/lib.sh TEST_FILES +=3D scripts/run.sh diff --git a/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c = b/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c new file mode 100644 index 000000000000..15b3e3af91d1 --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include + +static const char *device_bdf; + +static char state_session[LIVEUPDATE_SESSION_NAME_LENGTH]; +static char device_session[LIVEUPDATE_SESSION_NAME_LENGTH]; + +enum { + STATE_TOKEN, + DEVICE_TOKEN, +}; + +static void before_kexec(int luo_fd) +{ + struct vfio_pci_device *device; + struct iommu *iommu; + int session_fd; + int ret; + + iommu =3D iommu_init("iommufd"); + device =3D vfio_pci_device_init(device_bdf, iommu); + + create_state_file(luo_fd, state_session, STATE_TOKEN, /*next_stage=3D*/2); + + session_fd =3D luo_create_session(luo_fd, device_session); + VFIO_ASSERT_GE(session_fd, 0); + + printf("Preserving device in session\n"); + ret =3D luo_session_preserve_fd(session_fd, device->fd, DEVICE_TOKEN); + VFIO_ASSERT_EQ(ret, 0); + + close(luo_fd); + daemonize_and_wait(); +} + +static void after_kexec(int luo_fd, int state_session_fd) +{ + struct vfio_pci_device *device; + struct iommu *iommu; + int session_fd; + int device_fd; + int stage; + + restore_and_read_stage(state_session_fd, STATE_TOKEN, &stage); + VFIO_ASSERT_EQ(stage, 2); + + session_fd =3D luo_retrieve_session(luo_fd, device_session); + VFIO_ASSERT_GE(session_fd, 0); + + printf("Finishing the session before retrieving the device (should fail)\= n"); + VFIO_ASSERT_NE(luo_session_finish(session_fd), 0); + + printf("Retrieving the device FD from LUO\n"); + device_fd =3D luo_session_retrieve_fd(session_fd, DEVICE_TOKEN); + VFIO_ASSERT_GE(device_fd, 0); + + printf("Finishing the session before binding to iommufd (should fail)\n"); + VFIO_ASSERT_NE(luo_session_finish(session_fd), 0); + + printf("Binding the device to an iommufd and setting it up\n"); + iommu =3D iommu_init("iommufd"); + + /* + * This will invoke various ioctls on device_fd such as + * VFIO_DEVICE_GET_INFO. So this is a decent sanity test + * that LUO actually handed us back a valid VFIO device + * file and not something else. + */ + device =3D __vfio_pci_device_init(device_bdf, iommu, device_fd); + + printf("Finishing the session\n"); + VFIO_ASSERT_EQ(luo_session_finish(session_fd), 0); + + vfio_pci_device_cleanup(device); + iommu_cleanup(iommu); +} + +int main(int argc, char *argv[]) +{ + device_bdf =3D vfio_selftests_get_bdf(&argc, argv); + + sprintf(device_session, "device-%s", device_bdf); + sprintf(state_session, "state-%s", device_bdf); + + return luo_test(argc, argv, state_session, before_kexec, after_kexec); +} --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B79D4A1384 for ; Tue, 14 Jul 2026 15:15:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042132; cv=none; b=VVf8IDeORqR3P3G5MEgBJKioYr/qUhgwZgpDKT+WUUHsP8dSaamHKnA+nv2T1Uo0nG92okIejVteox4BNxZ9uINDNUCkun/89IZgKPJvuEGu7riM3LRT+RVQUaPRdQ81pNcvFHoN+EDzo1/q3YYHP6RMjenPkAhMW49O249u0hA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042132; c=relaxed/simple; bh=qNh2OB7o9DY+fchan5SBAgPmk4q+K1nnzSrJogXCq6o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fn5nwDRGApylmc438QQH+hPV/d7k2hgZaSnuPxRn1DQuwlTOjq0+cc9QBTWB4lo7M+1Zv+vsrrWgBfur+oPT1d7FXirgCnpQVQWSEUSgUbZxFEq2bqQKRe6jNOZujUdCYF6blfwz6UEFIqO6pzjrwRlKBb9otNeWAchVjb6GyQQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cpkYYYAf; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cpkYYYAf" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-848662cd2a1so1240246b3a.2 for ; Tue, 14 Jul 2026 08:15:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042129; x=1784646929; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=JcpQuk6vjGQSe9CsOI/pxanFsfOSXhryIEYWt4/hbIA=; b=cpkYYYAfmMzDHs0nc3O/FTomvuJ7iQRO9EFunIBdLjddq3HRe6dHFNletndhvkfMub k1+Dat20s9WImy8KxaxOMpYuTgGG4PgbsB84eWmNwQ0QJGM5G+0ABVVGbURsXBemGT6d wWxfsYZqDlW94p5TcS8dpCv0oEInhoVnhdZ17CZ2FUE/G4MCinThX2TpWx5lQRwDFx9A 7zxMltZ3chK/4YQc07tIwv0qH9vIEzQYItdGp1Fv7nbkjwLPZ2FPqLxqX0SL7WJNjDNH l6zsQUsOg0TEOSDitFnBJD8PKzRovgdpPl6jRKbZkzTmHdiY2hle0HAHbbZVYL3gr/7O FaHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042129; x=1784646929; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=JcpQuk6vjGQSe9CsOI/pxanFsfOSXhryIEYWt4/hbIA=; b=X9cbbGTb3DDmtxcr9y4m8ew1FWHRJbl75nXD1DUnFNxogjYFE/Kg3ebtSQqHtIuLoc OVGeZfePxbfl3qi8rtaYWovEG/9Cc95hLH6rx5EGtBOHzFKMtIB6AaD00YNxQOFm95Ty 56dZJb3520+N/t72gnrQtdVqOFEpvjeU7auWClqYuaiFdQURbpEsUohQtHsterMJ8yFV 9DBl3qtLNdziCvf7H7b6d5zd3Fq+hgy5HiyzG/tq8Bco6IHn72Y7k0VRS9p7RftuaFSS SpHZI7I5UbNJ2N3RyCVpPgM9jkLdzOP9pYDiHz5yGOTTyP7LHrjs75Zq5uyFrn0ggg0k LIxA== X-Forwarded-Encrypted: i=1; AHgh+RqpH+r7oPD32Yy7MKrAjPn78/j2HpDqNDeKuTltoTTpnQ56ZwjTG+8V2VJC+mr8lilZjRVaqcK9XncR3Xg=@vger.kernel.org X-Gm-Message-State: AOJu0YzMAF30rRhQBB8Iiurlo4mKvh/IMdgav/52S7dADkWybmFWcplZ ynO0gOL0L8UMtovJHge5t8KPRW8ln7M1zw+Q75+g3/fuXEbkNs22cvFYfoh0ltEzqKRVa4q2OJj KmVyUhRqOOw== X-Received: from pfx27.prod.google.com ([2002:a05:6a00:a45b:b0:848:5415:1869]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:bd8a:b0:848:70e7:f053 with SMTP id d2e1a72fcca58-8488980f75dmr11709317b3a.54.1784042128707; Tue, 14 Jul 2026 08:15:28 -0700 (PDT) Date: Tue, 14 Jul 2026 08:15:03 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-19-vipinsh@google.com> Subject: [PATCH v5 18/20] vfio: selftests: Expose iommu_modes to tests From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Expose the list of iommu_modes to enable tests that want to iterate through all possible iommu modes. Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- tools/testing/selftests/vfio/lib/include/libvfio/iommu.h | 2 ++ tools/testing/selftests/vfio/lib/iommu.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h b/too= ls/testing/selftests/vfio/lib/include/libvfio/iommu.h index e9a3386a4719..4b9cbe262159 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h @@ -15,6 +15,8 @@ struct iommu_mode { unsigned long iommu_type; }; =20 +extern const struct iommu_mode iommu_modes[]; +extern const int nr_iommu_modes; extern const char *default_iommu_mode; =20 struct dma_region { diff --git a/tools/testing/selftests/vfio/lib/iommu.c b/tools/testing/selft= ests/vfio/lib/iommu.c index 035dac069d60..95a494f829d2 100644 --- a/tools/testing/selftests/vfio/lib/iommu.c +++ b/tools/testing/selftests/vfio/lib/iommu.c @@ -23,7 +23,7 @@ const char *default_iommu_mode =3D MODE_IOMMUFD; =20 /* Reminder: Keep in sync with FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(). */ -static const struct iommu_mode iommu_modes[] =3D { +const struct iommu_mode iommu_modes[] =3D { { .name =3D MODE_VFIO_TYPE1_IOMMU, .container_path =3D "/dev/vfio/vfio", @@ -49,6 +49,8 @@ static const struct iommu_mode iommu_modes[] =3D { }, }; =20 +const int nr_iommu_modes =3D ARRAY_SIZE(iommu_modes); + static const struct iommu_mode *lookup_iommu_mode(const char *iommu_mode) { int i; --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C103C495530 for ; Tue, 14 Jul 2026 15:15:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042134; cv=none; b=PHgVDGiiSPIpaISYyl8qfiGcPGDfdDfiyatAoxHkH8YBx3SA63EL9PnpryDE1j2hHxnAjHErQm8pc1rH33uIrMbpvdYjOFHoRBwEBcnH2+V3/Jj8CeM68qXPM9Ac3ib3Sjd4VWJEeHBOmA2AKT75P/EiesIjG99i/jMGFDO62ko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042134; c=relaxed/simple; bh=iM8RSTeJkKuQ7uZ+HeWVjXp4kjNux7fkyVX+jcY2FXU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a4j2tAhKB/ers/hepeDkVASgyVN+0hu5q8GGerhQ+XoxT+osFeRfBqkXI2sfgstIwTSTK/IyN79uIqU3VnqoAYQ+GdN9YDi/lhFaZ2mCcJ6AUlWLfiqhglNILcKJl2ZPWjDpWR4pX5Q2/i+aY4SD74GP9t2HvELywSpP7+cY3ss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Jtu2/c6o; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Jtu2/c6o" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-845317fa7e6so8117433b3a.3 for ; Tue, 14 Jul 2026 08:15:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042130; x=1784646930; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=tk/1w3UkNj58XSSYbOe8ncPxmDwQIUvhVdPzzBUFHPI=; b=Jtu2/c6oFwtUtbK2rAjE6drE9/ydkTrGzccUaezbebLupdGEwpOs2wjVUbYqszn8Gr zYdPxLoTrxZcJDAUNEzLmkyFag0at+pT5cDIz8DcmWX22hMz2p65hUzVPXljUM2gYx8S S9yHKbBAqsFKLHYlOBgLRsKQhJuHdhgCrsItJFefrCHUbVeicFG4TkmnXTXRUa04XV+t V/69Sz4gMxkziZgA4f0Vu+qZm8utK59arESUve6r5pZREnV58LZogOg/DwOvXKkuz/1i Eqw0uaipSDn5y1btBKPTVus77JYyeoboxrvTMz+sy3zZdQuK7snfT1qFJerB9/9JPnO4 dRXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042130; x=1784646930; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=tk/1w3UkNj58XSSYbOe8ncPxmDwQIUvhVdPzzBUFHPI=; b=h0ih5r/DkhQYw2G2ld8re5pnMfj/kYei8Ww25pAewQEbvWqtmh2cmx8ao34ENDOw98 Sd2XeZ8N3oDJd7Kcb7Zc1kqPs8T6LwGIjheXMkR8UjB4VsvGDjpUj5dsciO6oC+nknP5 9BLw/m1ZJSEXo9jvhY513KcwKO0JcvexifNNwRDFDTCgbkQbvkxI0zNnmxOtbKiIOyaP ypmCBBg/0mKLpTNcNDFJxoboXALixb+v4DmkBy5ynK3TN1dGwNZbQz3DwbyRwxxSNFqY tIc8OnBrAFck64g7XIyi8/+baWvLySdRWxYemif/5JcsZpoWxZ65Eoe9C7rgkRvOHLdW HBHA== X-Forwarded-Encrypted: i=1; AHgh+RopfPQQ3XR2D4qqxQ9yO652TliIf51+NGLiE28rlH+bjagSsTD6dxQBzcGyzsW0awRLW3YwN7HL32nH49g=@vger.kernel.org X-Gm-Message-State: AOJu0YwAENodttOZojxpf88S9mpNA0LXd6Aq+mcekrkrnuc08ALCaNkA 9fkG0n3kLJCZ2vz3UUbWtwQAmI2hOTROAjgCm+5ZIrkEFlk4Ds6lLjaXFsGAsIpxCcN0GAtMk7+ 2MKsWtkLUdA== X-Received: from pfet14.prod.google.com ([2002:aa7:938e:0:b0:84a:1163:2ad0]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:c88:b0:847:83bd:6676 with SMTP id d2e1a72fcca58-8488963aa37mr12799379b3a.17.1784042129679; Tue, 14 Jul 2026 08:15:29 -0700 (PDT) Date: Tue, 14 Jul 2026 08:15:04 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-20-vipinsh@google.com> Subject: [PATCH v5 19/20] vfio: selftests: Verify that opening VFIO device fails during Live Update From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Verify that opening a VFIO device through its cdev file and via VFIO_GROUP_GET_DEVICE_FD both fail with -EBUSY if the device was preserved across a Live Update. When a device file is preserved across a Live Update, the file must be retrieved from /dev/liveupdate, not from VFIO directly. Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- .../vfio/vfio_pci_liveupdate_kexec_test.c | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c = b/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c index 15b3e3af91d1..ec3fe8f19502 100644 --- a/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c @@ -36,6 +36,42 @@ static void before_kexec(int luo_fd) daemonize_and_wait(); } =20 +static void check_open_vfio_device_fails(void) +{ + const char *cdev_path =3D vfio_pci_get_cdev_path(device_bdf); + struct vfio_pci_device *device; + struct iommu *iommu; + int ret, i; + + printf("Checking open(%s) fails\n", cdev_path); + ret =3D open(cdev_path, O_RDWR); + VFIO_ASSERT_EQ(ret, -1); + VFIO_ASSERT_EQ(errno, EBUSY); + free((void *)cdev_path); + + for (i =3D 0; i < nr_iommu_modes; i++) { + if (!iommu_modes[i].container_path) + continue; + + iommu =3D iommu_init(iommu_modes[i].name); + + device =3D vfio_pci_device_alloc(device_bdf, iommu); + vfio_pci_group_setup(device, device_bdf); + vfio_container_set_iommu(device); + + printf("Checking ioctl(group_fd, VFIO_GROUP_GET_DEVICE_FD, \"%s\") fails= (%s)\n", + device_bdf, iommu_modes[i].name); + + ret =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, device->bdf); + VFIO_ASSERT_EQ(ret, -1); + VFIO_ASSERT_EQ(errno, EBUSY); + + close(device->group_fd); + free(device); + iommu_cleanup(iommu); + } +} + static void after_kexec(int luo_fd, int state_session_fd) { struct vfio_pci_device *device; @@ -44,6 +80,8 @@ static void after_kexec(int luo_fd, int state_session_fd) int device_fd; int stage; =20 + check_open_vfio_device_fails(); + restore_and_read_stage(state_session_fd, STATE_TOKEN, &stage); VFIO_ASSERT_EQ(stage, 2); =20 --=20 2.55.0.795.g602f6c329a-goog From nobody Sat Jul 25 19:28:42 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34B2F4963C9 for ; Tue, 14 Jul 2026 15:15:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042136; cv=none; b=apd/MGjJU/gKnaB+Ss2bkV5OUPTgKv2M92k+6FzNXNQvUzarOWZQWstm9eZ+cilFIi52rmI1/27xsFiPmvMJkXAjmAMtZm6lMrYvQbaL15gAP0PGrcHU/VUrjOJjwLz7TUSmbzv9Do1kEje/JBoTgBjRyeAeTiGqNpZeAmAaerU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042136; c=relaxed/simple; bh=8CBMauhZZenpn+a2yQ87Rr2EfyqC5lFfzuVr5bte59U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hi1Chxe8cL54ykExqKqgHJ8+y310jNbCNr+PXzQNHxLzOTaQ/E7ZTCfdrxDMWykrEQfuOdfL5P/VxocUmf36ITIVjuqs/V1AFt3Fr33xWk1B4q7CuD9fkwzhVbA9elapStVp2VBO/ECXPcwbHtI+YdW9YdnP0D4UEpF2zGLIOoA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CbeLsf7O; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CbeLsf7O" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2cc8bde6318so82381125ad.3 for ; Tue, 14 Jul 2026 08:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042131; x=1784646931; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=xjEe04qswGeo8Cvf7F1uxuMmAPxZARk/R3E4HFO0u5g=; b=CbeLsf7ODiv/CAWg1Rr/dD33QZT/TGVzG4/d5rqy2hjIksaoqkfjs9in7QEiV4flMZ RvIcM4ZQ06NW5ZyxK+AcRRryMs5MC4jN0kCBTT90OQmhpY+luRhXRfxyJdjO0G/Pshul 7n0HfAM5GKt8OAtuk/DaIWQIHJaWuQvX5dljn9BLw/6HQ3dKWO+GdlRf1XhMkw4bnu8a Qv2dySadfBIrnjObPZ9r5fmGX4UKxrd68Nx9QHxAlWTjrZyJewymgHL2HRJyP0f3j5An q6DaHfZW1SBGtNFTrb3Pmm9Jo+9Bgl45BXyWpvtcc4UIMg+3jYoHzARufkmN+awQ8vmJ JgZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042131; x=1784646931; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xjEe04qswGeo8Cvf7F1uxuMmAPxZARk/R3E4HFO0u5g=; b=dp7vCAQSe0tBCuZLKKeUQfPuSFApt7iv7tiKSA52ZZDJrz0gwuNa/LU7y9A0qbHYHC 0f2wkfSmUcSUthAEjvdlnnhsiYOqYk8VBUza4V/+pHF3CAdNH2npv87kGNJWtacye7Pc KyMSX3n4mt4YgVx93nodjR0jVU8o6Rf75LEP7BkFp3y/T8v/bFPpA3L+EQor/OmMYTdW /cPEK7AL0u+RW+X+yNGnX0djYrFo0324uZbVr+WxP9f5EcqUWnDxydqx5Sxe3QkHR1L3 GhBibMlEY0PV3MU6XO/gAIw/eX9o66KPPmugvvEAxRrqmGlyLrFt5R9pbnaZZcu7NznF T3Iw== X-Forwarded-Encrypted: i=1; AHgh+Ro06CvKb2Yc9Qiis3bdrGPw5RB71Kpxo8z2ne33zJupgwCX377cK9uTQ0zMxb83SIY51eFPIhVUDzOO9HE=@vger.kernel.org X-Gm-Message-State: AOJu0YwQzVEmsbmrL6sP214E+IyHYX300xQy7Wnz5WXUYPUAPBB5qExl 7WhZOJzeFUdrOsj/5vwPlSEZASuaCrgBlvov7khbbuDNYwPPhjidBnuzE+uWscg4e1fvr/2DtPC mpTlqozjygA== X-Received: from plbl21.prod.google.com ([2002:a17:902:eb15:b0:2cc:da2c:8085]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:120c:b0:2ce:d1dc:1e1 with SMTP id d9443c01a7336-2cef1332689mr29905365ad.39.1784042130777; Tue, 14 Jul 2026 08:15:30 -0700 (PDT) Date: Tue, 14 Jul 2026 08:15:05 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-21-vipinsh@google.com> Subject: [PATCH v5 20/20] vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Matlack Add a long-running DMA memcpy operation to vfio_pci_liveupdate_kexec_test so that the device attempts to perform DMAs continuously during the Live Update. At this point iommufd preservation is not supported and bus mastering is not kept enabled on the device across the kexec, so most of these DMAs will be dropped. However this test ensures that the current device preservation support does not lead to system instability or crashes if the device is active. And once iommufd and bus mastering are preserved, this test can be relaxed to check that the DMA operations completed successfully. Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- .../vfio/vfio_pci_liveupdate_kexec_test.c | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c = b/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c index ec3fe8f19502..51b2f24f352c 100644 --- a/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c @@ -1,8 +1,16 @@ // SPDX-License-Identifier: GPL-2.0-only =20 +#include +#include + #include #include =20 +#define MEMCPY_SIZE SZ_1G +#define DRIVER_SIZE SZ_1M +#define MEMFD_SIZE (MEMCPY_SIZE + DRIVER_SIZE) + +static struct dma_region memcpy_region; static const char *device_bdf; =20 static char state_session[LIVEUPDATE_SESSION_NAME_LENGTH]; @@ -11,8 +19,89 @@ static char device_session[LIVEUPDATE_SESSION_NAME_LENGT= H]; enum { STATE_TOKEN, DEVICE_TOKEN, + MEMFD_TOKEN, }; =20 +static void dma_memcpy_one(struct vfio_pci_device *device) +{ + void *src =3D memcpy_region.vaddr, *dst; + u64 size; + + size =3D min_t(u64, memcpy_region.size / 2, device->driver.max_memcpy_siz= e); + dst =3D src + size; + + memset(src, 1, size); + memset(dst, 0, size); + + printf("Kicking off 1 DMA memcpy operations of size 0x%lx...\n", size); + vfio_pci_driver_memcpy(device, + to_iova(device, src), + to_iova(device, dst), + size); + + VFIO_ASSERT_EQ(memcmp(src, dst, size), 0); +} + +static void dma_memcpy_start(struct vfio_pci_device *device) +{ + void *src =3D memcpy_region.vaddr, *dst; + u64 count, size; + + size =3D min_t(u64, memcpy_region.size / 2, device->driver.max_memcpy_siz= e); + dst =3D src + size; + + /* + * Rough Math: If we assume the device will perform memcpy at a rate of + * 30GB/s then 7200GB of transfers will run for about 4 minutes. + */ + count =3D (u64)7200 * SZ_1G / size; + count =3D min_t(u64, count, device->driver.max_memcpy_count); + + memset(src, 1, size / 2); + memset(dst, 0, size / 2); + + printf("Kicking off %lu DMA memcpy operations of size 0x%lx...\n", count,= size); + vfio_pci_driver_memcpy_start(device, + to_iova(device, src), + to_iova(device, dst), + size, count); +} + +static void dma_memfd_map(struct vfio_pci_device *device, int fd) +{ + void *vaddr; + + vaddr =3D mmap(NULL, MEMFD_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, = 0); + VFIO_ASSERT_NE(vaddr, MAP_FAILED); + + memcpy_region.iova =3D SZ_4G; + memcpy_region.size =3D MEMCPY_SIZE; + memcpy_region.vaddr =3D vaddr; + iommu_map(device->iommu, &memcpy_region); + + device->driver.region.iova =3D memcpy_region.iova + memcpy_region.size; + device->driver.region.size =3D DRIVER_SIZE; + device->driver.region.vaddr =3D vaddr + memcpy_region.size; + iommu_map(device->iommu, &device->driver.region); +} + +static void dma_memfd_setup(struct vfio_pci_device *device, int session_fd) +{ + int fd, ret; + + fd =3D memfd_create("dma-buffer", 0); + VFIO_ASSERT_GE(fd, 0); + + ret =3D fallocate(fd, 0, 0, MEMFD_SIZE); + VFIO_ASSERT_EQ(ret, 0); + + printf("Preserving memfd of size 0x%x in session\n", MEMFD_SIZE); + ret =3D luo_session_preserve_fd(session_fd, fd, MEMFD_TOKEN); + VFIO_ASSERT_EQ(ret, 0); + + dma_memfd_map(device, fd); +} + static void before_kexec(int luo_fd) { struct vfio_pci_device *device; @@ -32,6 +121,27 @@ static void before_kexec(int luo_fd) ret =3D luo_session_preserve_fd(session_fd, device->fd, DEVICE_TOKEN); VFIO_ASSERT_EQ(ret, 0); =20 + dma_memfd_setup(device, session_fd); + + /* + * If the device has a selftests driver, kick off a long-running DMA + * operation to exercise the device trying to DMA during a Live Update. + * Since iommufd preservation is not supported yet, these DMAs should be + * dropped. So this is just looking to verify that the system does not + * fall over and crash as a result of a busy device being preserved. + */ + if (device->driver.ops) { + vfio_pci_driver_init(device); + dma_memcpy_start(device); + + /* + * Disable interrupts on the device or freeze() will fail. + * Unfortunately there isn't a way to easily have a test for + * that here since the check happens during shutdown. + */ + vfio_pci_msix_disable(device); + } + close(luo_fd); daemonize_and_wait(); } @@ -78,6 +188,7 @@ static void after_kexec(int luo_fd, int state_session_fd) struct iommu *iommu; int session_fd; int device_fd; + int memfd; int stage; =20 check_open_vfio_device_fails(); @@ -88,6 +199,10 @@ static void after_kexec(int luo_fd, int state_session_f= d) session_fd =3D luo_retrieve_session(luo_fd, device_session); VFIO_ASSERT_GE(session_fd, 0); =20 + printf("Retrieving memfd from LUO\n"); + memfd =3D luo_session_retrieve_fd(session_fd, MEMFD_TOKEN); + VFIO_ASSERT_GE(memfd, 0); + printf("Finishing the session before retrieving the device (should fail)\= n"); VFIO_ASSERT_NE(luo_session_finish(session_fd), 0); =20 @@ -109,9 +224,23 @@ static void after_kexec(int luo_fd, int state_session_= fd) */ device =3D __vfio_pci_device_init(device_bdf, iommu, device_fd); =20 + dma_memfd_map(device, memfd); + printf("Finishing the session\n"); VFIO_ASSERT_EQ(luo_session_finish(session_fd), 0); =20 + /* + * Once iommufd preservation is supported and the device is kept fully + * running across the Live Update, this should wait for the long- + * running DMA memcpy operation kicked off in before_kexec() to + * complete. But for now we expect the device to be reset so just + * trigger a single memcpy to make sure it's still functional. + */ + if (device->driver.ops) { + vfio_pci_driver_init(device); + dma_memcpy_one(device); + } + vfio_pci_device_cleanup(device); iommu_cleanup(iommu); } --=20 2.55.0.795.g602f6c329a-goog