From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 ECCEB35BDCB for ; Thu, 29 Jan 2026 21:25:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721942; cv=none; b=bdc71H9ILr2ESmDbs+4y8yzWzlXgc9XXRsY2Hm4BAEZHsQTJFNn2yqKEXIAzPpymXn+lWulDEhR+t3VLWS5TdRkD0sW4QMV6cPDZ6ajSsCcy3jz3Zb2juXMtg3MjUnahC+FXLIDEfhJeB+ng1J1cCj3dBuTGpBncuGOWOYGETbY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721942; c=relaxed/simple; bh=pGh11QoBfatNNPcD3UbRdXhxdT0MMITx2WqEtn8eFss=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rLOB5n/9onikf2jEtHD4m6B1FVUusa5RUCke0UOMC7SJngoW2xqvBCJNcGfBQ7r4kMnkLZBF7aG2nAcuyE/L1+YngfB2k2IX6pUzJf63Mhym4WN6rKLYgpbXwWuC23z1VYnbu28H6n7s01+9hAiDbcTwTHeVFpNLKiqikpgSuJk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YgClsBSQ; arc=none smtp.client-ip=209.85.216.74 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YgClsBSQ" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34abd303b4aso2994468a91.1 for ; Thu, 29 Jan 2026 13:25:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721939; x=1770326739; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iXsfpp7fYjSPi1nIw2ScrDnrjqTlWGi7ZArDWIRjDgk=; b=YgClsBSQkemOJVm7JMV99ev6GYocZTi+wpSdSxrIQAl42Bql7Vr4oGTqoEtm0Hz9vX 24FOanK/YWsgGM16o/756RCi/TOKqOzj6j+/XBtzZ6F5BPmRulnQtrHlb2JCAgnW64xI wNDMI/jZXzuZhvw5/SCsFa8VhoGxUbJKTZNI+6kQ30ZrMLTCUyccCV5JxCzWHXbKh3JW Q2FnBxQj9+z5NNXKrQFB9QPKJiGtxr5vgr5VXI9hJjIvAQXV7wmRW9xO9nrwSm3foyaO u23daigvanFsWEXNf1FeMaLkRRSNf4xQ68YAVvzmWfN9o6GimsOztC2hSdEGDi2TCgLu 3L6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721939; x=1770326739; h=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; bh=iXsfpp7fYjSPi1nIw2ScrDnrjqTlWGi7ZArDWIRjDgk=; b=QkeOjMZpg0xelpRpyzzsHjJP22DZAAn1rnYvIYlhn12oQ8e1oP4wXJiyv7EJQoaJL3 ha+JxvHQfjos1/+zcmvNNZk4DDtopS91CsjnuYChlS2LO/0LsL+LRNX5cflHPV88lHCW yWDDDl6c0E3PGR0qy09MbLVJKGuzfzVLCvFsh8+vw0zJcE/enwwtRjZsXAEDg+bXkjxA JYmH4zuEy7M9oFdo54nFoA0sJbg5Xa1FmX9PT7cQRyVMV4OQfJ/rn+mXM4deBAEHygZ9 h5+6AnarPhyg8usUfLLBzUUo/zClwNyA5KhkrvFIzFPNqKTo/wgapH8YdSsFykCzmX+0 QLAQ== X-Forwarded-Encrypted: i=1; AJvYcCWq93aPHC3qK7UJmWfLIPhrIvTRhydRpNe4CWgrsuPy2P20r/EedYuGwg8/+MH14oCAnxAfyjuC97vX+2w=@vger.kernel.org X-Gm-Message-State: AOJu0YyBgd8sgoMxpXjtiBsFcgPlD1A9fQOEEDK/YmUfM7563hopMjKr aCZ16QxuYddU6DGbNmAQPCNvGfOIOcTT2SwVRR1tDfxxoS8pVnSFTY8CyelkjQE97WpcsjDWKzu 1yaRMaKfLHorEWA== X-Received: from pjyu17.prod.google.com ([2002:a17:90a:e011:b0:353:454:939c]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:17c9:b0:353:356c:6840 with SMTP id 98e67ed59e1d1-3543b31688fmr768863a91.14.1769721939368; Thu, 29 Jan 2026 13:25:39 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:48 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-2-dmatlack@google.com> Subject: [PATCH v2 01/22] liveupdate: Export symbols needed by modules From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Export liveupdate_enabled(), 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. Signed-off-by: David Matlack --- kernel/liveupdate/luo_core.c | 1 + kernel/liveupdate/luo_file.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c index dda7bb57d421..59d7793d9444 100644 --- a/kernel/liveupdate/luo_core.c +++ b/kernel/liveupdate/luo_core.c @@ -255,6 +255,7 @@ bool liveupdate_enabled(void) { return luo_global.enabled; } +EXPORT_SYMBOL_GPL(liveupdate_enabled); =20 /** * DOC: LUO ioctl Interface diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c index 35d2a8b1a0df..32759e846bc9 100644 --- a/kernel/liveupdate/luo_file.c +++ b/kernel/liveupdate/luo_file.c @@ -872,6 +872,7 @@ int liveupdate_register_file_handler(struct liveupdate_= file_handler *fh) luo_session_resume(); return err; } +EXPORT_SYMBOL_GPL(liveupdate_register_file_handler); =20 /** * liveupdate_unregister_file_handler - Unregister a liveupdate file handl= er @@ -917,3 +918,4 @@ int liveupdate_unregister_file_handler(struct liveupdat= e_file_handler *fh) liveupdate_test_register(fh); return err; } +EXPORT_SYMBOL_GPL(liveupdate_unregister_file_handler); --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 8BD0E35CBD1 for ; Thu, 29 Jan 2026 21:25:41 +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=1769721943; cv=none; b=gFklA3QWvLgCCOWANuXj3pMNGGdUJWYJMeioL+4UmkG+MG6qcvCdBx6teMLIin0KkRcSn4jKZwariFVv4cyp1264SW0nvLGG01kWdr4VxHz2glZwHR90NDfmkVIViuAOsLdPsX4JL104cu3kJgAM2pLpqy0zgoCEYCJQg8LbQWM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721943; c=relaxed/simple; bh=aa5qSxMMVDUDlKQtjTotswW5ZPNNWuohp9tvJB5cNUU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=D8+smzgYrpTTxR8I5+mAaNuZ+WOmNJjNNCuTBYJDdFGJJNgEfherkcfAXMEppi8FTXeNLDqD5n21tUzblphMxN0lQyRiZ29IAnD6QJ9UDi3MAMIDOaJYFlqvlHhi35K/1k4IpfRX0yBKmklveuiInlTzhwB7BnrlXrY26rKZDtg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3TvWTVNF; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3TvWTVNF" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34c93f0849dso1505997a91.1 for ; Thu, 29 Jan 2026 13:25:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721941; x=1770326741; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FgSWc2a8f762WhttrAnnu8Gpn5PakSPoQMEwyGUxMj4=; b=3TvWTVNFMZSWsn9Vsp9r1HhqjChTu4BqptHdvrpmUTFibjjg2wVMW2gE56UxkZLIdn dF1GvnuYGAuSj0EArvmtq/c6hEGS04CZNWJfck7yvIqNvMd+RE7dip5k5eEPWXrsDx/f nO3+Pr/zfn8QSVZER3MOrCe/RYGkB79CsgXtomwDu506EhYT2I+sn/hN4fZj4DRvOshV iqI7L6MbdK/hTQOWWAZLtqR07N5VtMa4r7SRCJTJ4flHxw/JXOwfSgQjRx/KK0QQU+z/ XDspk6pyWV1lgPekufRSKZKlEqLxwN4m34QuW7b9m7Yq/OArDd8yKRN5Ha1FrNpz/iWL mhBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721941; x=1770326741; h=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; bh=FgSWc2a8f762WhttrAnnu8Gpn5PakSPoQMEwyGUxMj4=; b=Dl0TUoxVxuRfJWrRrel2fk7ON+evP2Z8L1hvXPWhCdgpAjPv6C5ygNb+mvPdqOjIM4 J3ESdcvFiyQR0KcTK+PsdZT8FDopjA9/Js5ykDrvQ4w7CmSAF3FZvw6AEsyolBjNFyIg OznbotJtP2N+i4m04mOpvNVWPiYpZYOUTm5Xd1XgcBTiBQrwmwA/K8/984hvAa3Rq2vz VTq2qQEZA0vDV5clf7w1NsNCBRoA2JnMNb3qCMkt6aa8D9m0RuvqnHudnxaPj/inZetD B7Bhx8VELcGvXoVAz+p0vSIUzcA+OxBV5e04S3iynXL5kPlXDOhzj0hahFlN+3xYa0SY GoPg== X-Forwarded-Encrypted: i=1; AJvYcCXq8UqRa/nGXEn+xeKb99bRvJiUC9PsTpw4mjxQXm3bGahcuc5NCu+fmBFJXeKrilUwQplpwAG4+1X/GAA=@vger.kernel.org X-Gm-Message-State: AOJu0YzZ5rKLof/wS3EqMOWCq1q/Yq2rnhLbyWP1QPcqAE3l5sYukZ57 odJRfU6e2pG/gejQYQOgg3BCiahXjUexm7GGxMlM8pE9/Vmx1NvFOcdX+XiF3dpxOqHNRd9nXIQ wWrgu/tyAJx8bLQ== X-Received: from pjbha8.prod.google.com ([2002:a17:90a:f3c8:b0:34c:34ab:8fd9]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4a82:b0:34f:6312:f225 with SMTP id 98e67ed59e1d1-35429b0007emr3496453a91.14.1769721940932; Thu, 29 Jan 2026 13:25:40 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:49 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-3-dmatlack@google.com> Subject: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel). Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs. Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices. pci_liveupdate_register_fh(driver_fh) pci_liveupdate_unregister_fh(driver_fh) Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs: pci_liveupdate_outgoing_preserve(pci_dev) pci_liveupdate_outgoing_unpreserve(pci_dev) After a Live Update, the PCI subsystem fetches its FLB global data from the previous kernel from the Live Update Orchestrator (LUO) during device initialization to determine which devices were preserved. Drivers can check if a device was preserved before userspace retrieves the file for it via pci_dev->liveupdate_incoming. Once a driver has finished restoring an incoming preserved device, it can notify the PCI subsystem with the following call, which clears pci_dev->liveupdate_incoming. pci_liveupdate_incoming_finish(pci_dev) This API will be used in subsequent commits by the vfio-pci driver to preserve VFIO devices across Live Update and by the PCI subsystem. Signed-off-by: David Matlack --- drivers/pci/Makefile | 1 + drivers/pci/liveupdate.c | 212 ++++++++++++++++++++++++++++++++++++ drivers/pci/probe.c | 2 + include/linux/kho/abi/pci.h | 55 ++++++++++ include/linux/pci.h | 47 ++++++++ 5 files changed, 317 insertions(+) create mode 100644 drivers/pci/liveupdate.c create mode 100644 include/linux/kho/abi/pci.h diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile index 8c259a9a8796..a32f7658b9e5 100644 --- a/drivers/pci/Makefile +++ b/drivers/pci/Makefile @@ -16,6 +16,7 @@ obj-$(CONFIG_PROC_FS) +=3D proc.o obj-$(CONFIG_SYSFS) +=3D pci-sysfs.o slot.o obj-$(CONFIG_ACPI) +=3D pci-acpi.o obj-$(CONFIG_GENERIC_PCI_IOMAP) +=3D iomap.o +obj-$(CONFIG_LIVEUPDATE) +=3D liveupdate.o endif =20 obj-$(CONFIG_OF) +=3D of.o diff --git a/drivers/pci/liveupdate.c b/drivers/pci/liveupdate.c new file mode 100644 index 000000000000..182cfc793b80 --- /dev/null +++ b/drivers/pci/liveupdate.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * David Matlack + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static DEFINE_MUTEX(pci_flb_outgoing_lock); + +static int pci_flb_preserve(struct liveupdate_flb_op_args *args) +{ + struct pci_dev *dev =3D NULL; + int max_nr_devices =3D 0; + struct pci_ser *ser; + unsigned long size; + + for_each_pci_dev(dev) + max_nr_devices++; + + size =3D struct_size_t(struct pci_ser, devices, max_nr_devices); + + ser =3D kho_alloc_preserve(size); + if (IS_ERR(ser)) + return PTR_ERR(ser); + + ser->max_nr_devices =3D max_nr_devices; + + args->obj =3D ser; + args->data =3D virt_to_phys(ser); + return 0; +} + +static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args) +{ + struct pci_ser *ser =3D args->obj; + + WARN_ON_ONCE(ser->nr_devices); + kho_unpreserve_free(ser); +} + +static int pci_flb_retrieve(struct liveupdate_flb_op_args *args) +{ + args->obj =3D phys_to_virt(args->data); + return 0; +} + +static void pci_flb_finish(struct liveupdate_flb_op_args *args) +{ + kho_restore_free(args->obj); +} + +static struct liveupdate_flb_ops pci_liveupdate_flb_ops =3D { + .preserve =3D pci_flb_preserve, + .unpreserve =3D pci_flb_unpreserve, + .retrieve =3D pci_flb_retrieve, + .finish =3D pci_flb_finish, + .owner =3D THIS_MODULE, +}; + +static struct liveupdate_flb pci_liveupdate_flb =3D { + .ops =3D &pci_liveupdate_flb_ops, + .compatible =3D PCI_LUO_FLB_COMPATIBLE, +}; + +#define INIT_PCI_DEV_SER(_dev) { \ + .domain =3D pci_domain_nr((_dev)->bus), \ + .bdf =3D pci_dev_id(_dev), \ +} + +static int pci_dev_ser_cmp(const void *__a, const void *__b) +{ + const struct pci_dev_ser *a =3D __a, *b =3D __b; + + return cmp_int(a->domain << 16 | a->bdf, b->domain << 16 | b->bdf); +} + +static struct pci_dev_ser *pci_ser_find(struct pci_ser *ser, + struct pci_dev *dev) +{ + const struct pci_dev_ser key =3D INIT_PCI_DEV_SER(dev); + + return bsearch(&key, ser->devices, ser->nr_devices, + sizeof(key), pci_dev_ser_cmp); +} + +static int pci_ser_delete(struct pci_ser *ser, struct pci_dev *dev) +{ + struct pci_dev_ser *dev_ser; + int i; + + dev_ser =3D pci_ser_find(ser, dev); + if (!dev_ser) + return -ENOENT; + + for (i =3D dev_ser - ser->devices; i < ser->nr_devices - 1; i++) + ser->devices[i] =3D ser->devices[i + 1]; + + ser->nr_devices--; + return 0; +} + +int pci_liveupdate_outgoing_preserve(struct pci_dev *dev) +{ + struct pci_dev_ser new =3D INIT_PCI_DEV_SER(dev); + struct pci_ser *ser; + int i, ret; + + /* Preserving VFs is not supported yet. */ + if (dev->is_virtfn) + return -EINVAL; + + guard(mutex)(&pci_flb_outgoing_lock); + + if (dev->liveupdate_outgoing) + return -EBUSY; + + ret =3D liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser); + if (ret) + return ret; + + if (ser->nr_devices =3D=3D ser->max_nr_devices) + return -E2BIG; + + for (i =3D ser->nr_devices; i > 0; i--) { + struct pci_dev_ser *prev =3D &ser->devices[i - 1]; + int cmp =3D pci_dev_ser_cmp(&new, prev); + + if (WARN_ON_ONCE(!cmp)) + return -EBUSY; + + if (cmp > 0) + break; + + ser->devices[i] =3D *prev; + } + + ser->devices[i] =3D new; + ser->nr_devices++; + dev->liveupdate_outgoing =3D true; + return 0; +} +EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_preserve); + +void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev) +{ + struct pci_ser *ser; + int ret; + + guard(mutex)(&pci_flb_outgoing_lock); + + ret =3D liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser); + if (WARN_ON_ONCE(ret)) + return; + + WARN_ON_ONCE(pci_ser_delete(ser, dev)); + dev->liveupdate_outgoing =3D false; +} +EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_unpreserve); + +u32 pci_liveupdate_incoming_nr_devices(void) +{ + struct pci_ser *ser; + int ret; + + ret =3D liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser); + if (ret) + return 0; + + return ser->nr_devices; +} +EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_nr_devices); + +void pci_liveupdate_setup_device(struct pci_dev *dev) +{ + struct pci_ser *ser; + int ret; + + ret =3D liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser); + if (ret) + return; + + dev->liveupdate_incoming =3D !!pci_ser_find(ser, dev); +} +EXPORT_SYMBOL_GPL(pci_liveupdate_setup_device); + +void pci_liveupdate_incoming_finish(struct pci_dev *dev) +{ + dev->liveupdate_incoming =3D false; +} +EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_finish); + +int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh) +{ + return liveupdate_register_flb(fh, &pci_liveupdate_flb); +} +EXPORT_SYMBOL_GPL(pci_liveupdate_register_fh); + +int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh) +{ + return liveupdate_unregister_flb(fh, &pci_liveupdate_flb); +} +EXPORT_SYMBOL_GPL(pci_liveupdate_unregister_fh); diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index 37329095e5fe..af6356c5a156 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -2060,6 +2060,8 @@ int pci_setup_device(struct pci_dev *dev) if (pci_early_dump) early_dump_pci_device(dev); =20 + pci_liveupdate_setup_device(dev); + /* Need to have dev->class ready */ dev->cfg_size =3D pci_cfg_space_size(dev); =20 diff --git a/include/linux/kho/abi/pci.h b/include/linux/kho/abi/pci.h new file mode 100644 index 000000000000..6577767f8da6 --- /dev/null +++ b/include/linux/kho/abi/pci.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (c) 2025, Google LLC. + * David Matlack + */ + +#ifndef _LINUX_KHO_ABI_PCI_H +#define _LINUX_KHO_ABI_PCI_H + +#include +#include + +/** + * DOC: PCI File-Lifecycle Bound (FLB) Live Update ABI + * + * This header defines the ABI for preserving core PCI state across kexec = using + * Live Update File-Lifecycle Bound (FLB) data. + * + * 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 PCI_LUO_FLB_COMPATIBLE string. + */ + +#define PCI_LUO_FLB_COMPATIBLE "pci-v1" + +/** + * struct pci_dev_ser - Serialized state about a single PCI device. + * + * @domain: The device's PCI domain number (segment). + * @bdf: The device's PCI bus, device, and function number. + */ +struct pci_dev_ser { + u16 domain; + u16 bdf; +} __packed; + +/** + * struct pci_ser - PCI Subsystem Live Update State + * + * This struct tracks state about all devices that are being preserved acr= oss + * a Live Update for the next kernel. + * + * @max_nr_devices: The length of the devices[] flexible array. + * @nr_devices: The number of devices that were preserved. + * @devices: Flexible array of pci_dev_ser structs for each device. Guaran= teed + * to be sorted ascending by domain and bdf. + */ +struct pci_ser { + u64 max_nr_devices; + u64 nr_devices; + struct pci_dev_ser devices[]; +} __packed; + +#endif /* _LINUX_KHO_ABI_PCI_H */ diff --git a/include/linux/pci.h b/include/linux/pci.h index 7e36936bb37a..9ead6d84aef6 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -40,6 +40,7 @@ #include #include #include +#include =20 #include =20 @@ -582,6 +583,10 @@ struct pci_dev { u8 tph_mode; /* TPH mode */ u8 tph_req_type; /* TPH requester type */ #endif +#ifdef CONFIG_LIVEUPDATE + unsigned int liveupdate_incoming:1; /* Preserved by previous kernel */ + unsigned int liveupdate_outgoing:1; /* Preserved for next kernel */ +#endif }; =20 static inline struct pci_dev *pci_physfn(struct pci_dev *dev) @@ -2854,4 +2859,46 @@ void pci_uevent_ers(struct pci_dev *pdev, enum pci_= ers_result err_type); WARN_ONCE(condition, "%s %s: " fmt, \ dev_driver_string(&(pdev)->dev), pci_name(pdev), ##arg) =20 +#ifdef CONFIG_LIVEUPDATE +int pci_liveupdate_outgoing_preserve(struct pci_dev *dev); +void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev); +void pci_liveupdate_setup_device(struct pci_dev *dev); +u32 pci_liveupdate_incoming_nr_devices(void); +void pci_liveupdate_incoming_finish(struct pci_dev *dev); +int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh); +int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh); +#else /* !CONFIG_LIVEUPDATE */ +static inline int pci_liveupdate_outgoing_preserve(struct pci_dev *dev) +{ + return -EOPNOTSUPP; +} + +static inline void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev) +{ +} + +static inline void pci_liveupdate_setup_device(struct pci_dev *dev) +{ +} + +static inline u32 pci_liveupdate_incoming_nr_devices(void) +{ + return 0; +} + +static inline void pci_liveupdate_incoming_finish(struct pci_dev *dev) +{ +} + +static inline int pci_liveupdate_register_fh(struct liveupdate_file_handle= r *fh) +{ + return -EOPNOTSUPP; +} + +static inline int pci_liveupdate_unregister_fh(struct liveupdate_file_hand= ler *fh) +{ + return -EOPNOTSUPP; +} +#endif /* !CONFIG_LIVEUPDATE */ + #endif /* LINUX_PCI_H */ --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 5232335D5EA for ; Thu, 29 Jan 2026 21:25:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721945; cv=none; b=MgCWo5p5WSTkkryNuXJl56ubTlSFRzpjzDejejnOVkC4fBi8Xs4elW4aYVNG+kJxCBvRHh+z21huZqtOtlyIR/K+OCFDQPAB0wDjycQm+zxinIfbVXB3Gvj4ZHnLXDsp1KF4q9v/IX4FFDrEQGRyh4DDzP33g8yOUVukep+3vWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721945; c=relaxed/simple; bh=WQ9zgAmCTBZ4bwjQYNH8OonhSwck2yad34xCkVdAyYE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NVugHnz9iOsWAymwBnKuKaZWkZcenvQfnk4lZsE1iPnKN0gDAzOKIoAgcU5eYX4IZWLaZXaYkJUnPgwxm57SUpJOcM8MljoazCfdiDHwF2Qp1ogsJEEl+0HipGokRJvs6IZOxVfU2M6EFRsxqHux3QsE6jPwkyLYWQD5x8/szBY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=XjVaCgTF; arc=none smtp.client-ip=209.85.216.74 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XjVaCgTF" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-352de7a89e1so1182240a91.1 for ; Thu, 29 Jan 2026 13:25:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721943; x=1770326743; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=u1Z3QHTjPxpYvShlt2cO6c78554A0ZIR6ozbVe3TjI8=; b=XjVaCgTFQyi6ghx7LAtflhE0rRuGae3zRnMF+wJ64bOcpbaClnEJCHeeFPuNMx0bhj Chg5w3+Rx1+NgQiC/X3EpKjOM5AHlPxMXucXPInqNLq5W2iNCZO09+LE2qwLNDBEXIPE k6KPrSjv+Nicpr3qeTwLiE4/7n7Eq4kAhYE1RGrPbyQj1PFa5a168R4x8Ag9H2obFNnC MioljlYyKnMLyNKbtxL/nZduWIAaIE13aEIZ/x9ZtOBBqDhg6avJOIdW/06RH84x6bAT mspO0qAoOXdpPYicKB0pO4XSn/yd4pHPLC5KY91JNeLGhYPPPelvXvLyDFk+A+VUIMw2 d43g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721943; x=1770326743; h=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; bh=u1Z3QHTjPxpYvShlt2cO6c78554A0ZIR6ozbVe3TjI8=; b=eKu0V4ba8RSrTHSXD3Ez6X0qDuPvy51l6LQPJ72Hs9BUzjDXyPLlqlNo4sPT9bgpDu ZQWe3S8zSASPZ+Y8ILuFUPlOuxP7EagW9sXj3NLPsTRD8cx5aBAMQXvKfq/pFfeXmO9V daa+QOCfbjmeaunsAvvBnNwW+Z2r+8YtfkBGhGv/gWKWh+ni3PeE7YMFr/HI1D2Y5DlQ xlqam1Aql/cfSAVQk3T2q+X05yziEIpUnkuy0xz7FuQsQFWLwRT38IsxY/o7Qeokoi/y HKNsOI3Cpt31TlSgJ0p1c+DBpCzDvsjGsw3oOBYgp6gedt5rV7hG4kqU/9NxoBKWErmV 6NVg== X-Forwarded-Encrypted: i=1; AJvYcCVv8bzGSRwFPHuYKA1iLx+Zwg3XSEUVzSY55tJc8V6jeZYl3C5LikOBigmRnoDIItR9OqaPJhysUYER25A=@vger.kernel.org X-Gm-Message-State: AOJu0YzMqzyX1zQc5LxQwxatKVVHmy+e0o7GyC1zygi+IwyNE0V2yv5n QiLk5xmlc4AJuxGrZQ5rK62YG5Lq2wGLC6oTZVnh/+RerrBrqd4GB0QPJDxN17gIiTacsIcya8u B0/TOBWADOhcUhg== X-Received: from pjblb16.prod.google.com ([2002:a17:90b:4a50:b0:352:b92b:ef8]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:28c5:b0:343:684c:f8a0 with SMTP id 98e67ed59e1d1-3543b3ae12dmr762757a91.23.1769721942669; Thu, 29 Jan 2026 13:25:42 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:50 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-4-dmatlack@google.com> Subject: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Inherit bus numbers from the previous kernel during a Live Update when one or more PCI devices are being preserved. This is necessary so that preserved devices can DMA through the IOMMU during a Live Update (changing bus numbers would break IOMMU translation). Signed-off-by: David Matlack --- drivers/pci/probe.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index af6356c5a156..ca6e5f79debb 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -1351,6 +1351,20 @@ static bool pci_ea_fixed_busnrs(struct pci_dev *dev,= u8 *sec, u8 *sub) return true; } =20 +static bool pci_assign_all_busses(void) +{ + /* + * During a Live Update where devices are preserved by the previous + * kernel, inherit all bus numbers assigned by the previous kernel. Bus + * numbers must remain stable for preserved devices so that they can + * perform DMA during the Live Update uninterrupted. + */ + if (pci_liveupdate_incoming_nr_devices()) + return false; + + return pcibios_assign_all_busses(); +} + /* * pci_scan_bridge_extend() - Scan buses behind a bridge * @bus: Parent bus the bridge is on @@ -1378,6 +1392,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus= , struct pci_dev *dev, int max, unsigned int available_buses, int pass) { + bool assign_all_busses =3D pci_assign_all_busses(); struct pci_bus *child; int is_cardbus =3D (dev->hdr_type =3D=3D PCI_HEADER_TYPE_CARDBUS); u32 buses, i, j =3D 0; @@ -1424,7 +1439,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus= , struct pci_dev *dev, pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT); =20 - if ((secondary || subordinate) && !pcibios_assign_all_busses() && + if ((secondary || subordinate) && !assign_all_busses && !is_cardbus && !broken) { unsigned int cmax, buses; =20 @@ -1467,7 +1482,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus= , struct pci_dev *dev, * do in the second pass. */ if (!pass) { - if (pcibios_assign_all_busses() || broken || is_cardbus) + if (assign_all_busses || broken || is_cardbus) =20 /* * Temporarily disable forwarding of the @@ -1542,7 +1557,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus= , struct pci_dev *dev, max+i+1)) break; while (parent->parent) { - if ((!pcibios_assign_all_busses()) && + if (!assign_all_busses && (parent->busn_res.end > max) && (parent->busn_res.end <=3D max+i)) { j =3D 1; --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 D3F5835CBCD for ; Thu, 29 Jan 2026 21:25:44 +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=1769721946; cv=none; b=CqMS21n1YHOxZPXodLDwBBYUQVoJCOW33qy0kM1Ckm4sYNSWN9iU/V/y8yJouzZnhhxx31QLoeTB2fKuh1DhNctpOxYmtvUkCZpqKhf2IMjlDczJlaThghwiqupgqUqqcICVFLPscTRZbmy1Vi3JKooErx3Ab3AFQZIBnV1zrdo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721946; c=relaxed/simple; bh=gIPoK/blCm65riWNC9nZMXYVtHTirbCm9IWwozauu7g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pXyESI3DFt0+qtA65F+qSj2lY7kMenk4yO8eh9PaH9Rhowp7YCujeFlEOAEafcbfunvXum2NvctwaZk4dn7qcpkm7KbSqA9kZ0RiXl47ISuX0HLO7UThEDQavMWUSZcgQ2ytdy7TICLJIQTJQV1aSEIgCxeQ4jegTHQiLta85a8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SKeL3oCc; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SKeL3oCc" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a0bae9acd4so10812905ad.3 for ; Thu, 29 Jan 2026 13:25:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721944; x=1770326744; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2DumsOMxgbS0OIrY6jatWasSBK4J7y0u52rkK5iohgQ=; b=SKeL3oCcS3IiB61sYeJWkChzrZzJJDq/CISPenmYSjYZMH5XEGfUultWr4Aa/pAUum l4EKHtWjaigdMcCSSHMmQSLu1CoijEU/0Ni1rdG/iHkYjWiV05WNjD3AnFcUMTQ56mXd tR2jfrrIGOblh3qE5YF14bqqOywvMxIkDA48ywMSwMOoHo3ozD3RvGweJULJcyeN8yYx JwThM/hRcDCLkoRYbtO5GBjwKvME6guWADfqlL2rCXFUK7xvzG+bPaYukIBjO7koOTK4 lxw+ELvUk/TK7/rh94UeNua2UNEDGqBwc+Kd0Iu7M9t9IuJGTfsf08tte5DYktBCILe4 RzYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721944; x=1770326744; h=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; bh=2DumsOMxgbS0OIrY6jatWasSBK4J7y0u52rkK5iohgQ=; b=gOcRKjmADgv7TQWoPpCU7leuNHcuwamxvHHUSFISIYTGDV8tE6TQ5O1qLOLGSnaPlI ocZ8Kh5V5laSBWi54z50lxG5akYgMy992s6OB9bahFzG7dWjjxOYrwQLMff/Aep88hDp x6GY9FxrLIOpMt4v7TLyn5OEH/M5LiqtYW7bEX0jFtq88an67hBDAvH22oYp7xzc4yOb 37JGvMNmSvqnK3QHt6c9IZnyCqZ49+njL40L6xURZCjNVHsOoOQ04rV8qES8Adl1+FHV kD6L1mhPN6EU0M39UduLOXW0c7vzbZok7MRve0H76mkcmUD2JO4eeYB3Zu/vE7+2yzvP mwDw== X-Forwarded-Encrypted: i=1; AJvYcCVBmETQOnxBZmFvlN3N54a/NfWTRcXaa8I93x8UA4iJSr4c84ggPAyApj67mAfhclo2bCMpDhMLXfQIvXU=@vger.kernel.org X-Gm-Message-State: AOJu0YxVxyEDwKE03sd9lQU00iynGTjehyxSk2tK2MeT/cJh/wbA4cmr iFK9FT1lsPBEarrPpHi2BvTMhtwCzFSI1d8s/1RJN+N/MxKx26YJGW9EI5u5mcZbGQb8i1+iFga eNx+s4mbpV7sh4w== X-Received: from plxd5.prod.google.com ([2002:a17:902:ef05:b0:2a7:5199:9c34]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:41c1:b0:2a0:d074:e9f4 with SMTP id d9443c01a7336-2a8d9a55422mr4900435ad.59.1769721944173; Thu, 29 Jan 2026 13:25:44 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:51 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-5-dmatlack@google.com> Subject: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma 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). 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. Signed-off-by: Vipin Sharma Co-developed-by: David Matlack Signed-off-by: David Matlack --- MAINTAINERS | 1 + drivers/vfio/pci/Makefile | 1 + drivers/vfio/pci/vfio_pci.c | 9 +++- drivers/vfio/pci/vfio_pci_liveupdate.c | 69 ++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_priv.h | 14 ++++++ include/linux/kho/abi/vfio_pci.h | 28 +++++++++++ 6 files changed, 121 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 a671e3d4e8be..7d6cdecedb05 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -27520,6 +27520,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/Makefile b/drivers/vfio/pci/Makefile index e0a0757dd1d2..23305ebc418b 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_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 0c771064c0b8..19e88322af2c 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -258,6 +258,10 @@ static int __init vfio_pci_init(void) int ret; bool is_disable_vga =3D true; =20 + ret =3D vfio_pci_liveupdate_init(); + if (ret) + return ret; + #ifdef CONFIG_VFIO_PCI_VGA is_disable_vga =3D disable_vga; #endif @@ -266,8 +270,10 @@ static int __init vfio_pci_init(void) =20 /* Register and scan for devices */ ret =3D pci_register_driver(&vfio_pci_driver); - if (ret) + if (ret) { + vfio_pci_liveupdate_cleanup(); return ret; + } =20 vfio_pci_fill_ids(); =20 @@ -281,6 +287,7 @@ module_init(vfio_pci_init); 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..b84e63c0357b --- /dev/null +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Vipin Sharma + * David Matlack + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#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) +{ + if (!liveupdate_enabled()) + return 0; + + return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh); +} + +void vfio_pci_liveupdate_cleanup(void) +{ + if (!liveupdate_enabled()) + return; + + 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 27ac280f00b9..68966ec64e51 100644 --- a/drivers/vfio/pci/vfio_pci_priv.h +++ b/drivers/vfio/pci/vfio_pci_priv.h @@ -133,4 +133,18 @@ static inline void vfio_pci_dma_buf_move(struct vfio_p= ci_core_device *vdev, } #endif =20 +#ifdef CONFIG_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_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..37a845eed972 --- /dev/null +++ b/include/linux/kho/abi/vfio_pci.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (c) 2025, 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 + * + * This header defines the ABI for preserving the state of a VFIO PCI devi= ce + * files across a kexec 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.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 80CAA35EDCC for ; Thu, 29 Jan 2026 21:25:46 +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=1769721948; cv=none; b=Fe8BLkV0uCBNUanmz4fZ89c/gFo8aKMNXdiFmUJDLnArOJadgTxcmQEaP2AJUBpUWHoSumI0mpEqaa5k9gjWCx7yUAJuEXu8of7wbcAhwPxnJZG4crAuE0eOnBsUWI9m5fGehnO3B3uOD3mCII67AIOpylj7BIGKNM7wN8j2U4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721948; c=relaxed/simple; bh=Sb2eXynkhOU6bn58XiptwWXFvaWJzPQjxY1+zr9Zx98=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VRy0VCETFr1z9i3y1JK+l/5bUtNTGX+fmJsqUi5QRXMgCeIHL+71eWE5xL5VeN8xvSJzgtzD5T/iGgDMeVZlh/uaf9nfM7GzG3qf6pbBhaXHr63OB+m9Btr18KfjWzFw8gJMPuK7E/Ja9X3fR3hFGs6mvnyGIHLhpDCNj+ZOgWA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qO4OzVQh; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qO4OzVQh" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2a871c32cdbso13762185ad.2 for ; Thu, 29 Jan 2026 13:25:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721946; x=1770326746; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+aO6tJGKcRtg/TIeiT23MNEgzxzjjKFBFfPz6SwCjTg=; b=qO4OzVQhQfN0D1ZFFnIVV7pulBuImku5J/8aGEuS+lPU7xxX23foxvNLA9bLMNkKJ1 rtpkxgK2sZnaJUNIhwTvPWU/gLYb9ChbkUWDvOctyb+M+Khq/Sqybg37IFDkXm20d4CI LkvE+r7rqsicDrBYpI3opKzIjWOirQNsufO/BA+ilGodsRGr25LwMHUi/f2nuVF6noTa Bs9IWzDg+0oNNCSu5kBo32913WYQtxwIIFLgqJySYktdOMdZMgl83cwSnVcSlqQO/or5 OQMMxfCN6pAN2/JxzoMUtcx7ptiv84gOFtx+9J3tbjiRI07h+0QOJJybFOftwj3ay1NZ zJew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721946; x=1770326746; h=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; bh=+aO6tJGKcRtg/TIeiT23MNEgzxzjjKFBFfPz6SwCjTg=; b=j0exM1iqkRphs5mCpVM1Q4wUquaFzpxkntq6wXZmKGDsTtQzwtv7V4zuONnPpmFWHO zu4Ar2/7CmBYXWX/eIFNvJ2yX1uIgxwVSHGaBWEya7Np5C16PJ/VT0Sud6NjRwc1GEWS POhxFF+q9Qxe8O36gnyuNpjb+mIL06G12CNDE5SZAX+gIhjfbm8yUirp0Niod/DvyrQ6 0XZbt0TXNfg86kBcWLAXhCoVvCvIGMX3UqhCWv8BIlnbyvdgcAXV/vu2TaeohcuX83LY 0Qt5drqKZWTRzNIgyRAOxDBieZzGERnQXDmj8tMrqbUyG/lPINGDAeGVH1+tmwsq7C1K fXZQ== X-Forwarded-Encrypted: i=1; AJvYcCVOkOxdXdZZbK4+PU0ak3+JANfO4yigGY/CNfTE1lznNO+X88evwmlzQebOAZCRr7ByI+yfoN6g2KLSeMo=@vger.kernel.org X-Gm-Message-State: AOJu0YxlRhbhHCgTT5dXoKeUEmbPkNzWUcBGbJPWnhz3AXR4iMYj9FcT Y3h8hmmYSM3sa3ihz+EQW34ksBc8iOJ+VMkm/TNGB4v2TIphS/75r/gA8/7L51ImeCOiUOQU3Fr mWx965E2jGB7itA== X-Received: from plrf4.prod.google.com ([2002:a17:902:ab84:b0:29d:5afa:2de]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c952:b0:295:3584:1bbd with SMTP id d9443c01a7336-2a8d8176d76mr6975245ad.41.1769721945885; Thu, 29 Jan 2026 13:25:45 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:52 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-6-dmatlack@google.com> Subject: [PATCH v2 05/22] vfio/pci: Preserve vfio-pci device files across Live Update From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma 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. Live Update support is scoped only to cdev files (i.e. not VFIO_GROUP_GET_DEVICE_FD files). 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 the device's PCI segment number and BDF, so that the device can be uniquely identified after the Live Update. Require that userspace disables interrupts on the device prior to freeze() so that the device does not send any interrupts until new interrupt handlers have been set up by the next kernel. Reset the device and restore its state in the freeze() callback. This ensures the device can be received by the next kernel in a consistent state. Eventually this will be dropped and the device can be preserved across in a running state, but that requires further work in VFIO and the core PCI layer. Note that LUO holds a reference to this file when it is preserved. So VFIO is guaranteed that vfio_df_device_last_close() will not be called on this device no matter what userspace does. Signed-off-by: Vipin Sharma Co-developed-by: David Matlack Signed-off-by: David Matlack --- drivers/vfio/pci/vfio_pci.c | 2 +- drivers/vfio/pci/vfio_pci_liveupdate.c | 84 +++++++++++++++++++++++++- drivers/vfio/pci/vfio_pci_priv.h | 2 + drivers/vfio/vfio.h | 13 ---- drivers/vfio/vfio_main.c | 10 +-- include/linux/kho/abi/vfio_pci.h | 15 +++++ include/linux/vfio.h | 28 +++++++++ 7 files changed, 129 insertions(+), 25 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 19e88322af2c..0260afb9492d 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_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c index b84e63c0357b..f01de98f1b75 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -8,25 +8,104 @@ =20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#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_file *df =3D to_vfio_device_file(file); + + if (!df) + return false; + + /* Live Update support is limited to cdev files. */ + if (df->group) + return false; + + return df->device->ops =3D=3D &vfio_pci_ops; } =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; + + vdev =3D container_of(device, struct vfio_pci_core_device, vdev); + pdev =3D vdev->pdev; + + if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) + return -EINVAL; + + if (vfio_pci_is_intel_display(pdev)) + return -EINVAL; + + ser =3D kho_alloc_preserve(sizeof(*ser)); + if (IS_ERR(ser)) + return PTR_ERR(ser); + + 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; } =20 static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args = *args) { + 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; + int ret; + + vdev =3D container_of(device, struct vfio_pci_core_device, vdev); + pdev =3D vdev->pdev; + + guard(mutex)(&device->dev_set->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; + } + + pci_dev_lock(pdev); + + ret =3D pci_load_saved_state(pdev, vdev->pci_saved_state); + if (ret) + goto out; + + /* + * Reset the device and restore it back to its original state before + * handing it to the next kernel. + * + * Eventually both of these should be dropped and the device should be + * kept running with its current state across the Live Update. + */ + if (vdev->reset_works) + ret =3D __pci_reset_function_locked(pdev); + + pci_restore_state(pdev); + +out: + pci_dev_unlock(pdev); + return ret; } =20 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *ar= gs) @@ -42,6 +121,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 68966ec64e51..d3da79b7b03c 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; diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 50128da18bca..6b89edbbf174 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -16,17 +16,6 @@ struct iommufd_ctx; struct iommu_group; struct vfio_container; =20 -struct vfio_device_file { - struct vfio_device *device; - struct vfio_group *group; - - u8 access_granted; - u32 devid; /* only valid when iommufd is valid */ - spinlock_t kvm_ref_lock; /* protect kvm field */ - struct kvm *kvm; - struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock= */ -}; - void vfio_device_put_registration(struct vfio_device *device); bool vfio_device_try_get_registration(struct vfio_device *device); int vfio_df_open(struct vfio_device_file *df); @@ -34,8 +23,6 @@ void vfio_df_close(struct vfio_device_file *df); struct vfio_device_file * vfio_allocate_device_file(struct vfio_device *device); =20 -extern const struct file_operations vfio_device_fops; - #ifdef CONFIG_VFIO_NOIOMMU extern bool vfio_noiommu __read_mostly; #else diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index f7df90c423b4..276f615f0c28 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1436,15 +1436,7 @@ const struct file_operations vfio_device_fops =3D { .show_fdinfo =3D vfio_device_show_fdinfo, #endif }; - -static struct vfio_device *vfio_device_from_file(struct file *file) -{ - struct vfio_device_file *df =3D file->private_data; - - if (file->f_op !=3D &vfio_device_fops) - return NULL; - return df->device; -} +EXPORT_SYMBOL_GPL(vfio_device_fops); =20 /** * vfio_file_is_valid - True if the file is valid vfio file diff --git a/include/linux/kho/abi/vfio_pci.h b/include/linux/kho/abi/vfio_= pci.h index 37a845eed972..9bf58a2f3820 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. + * + * @bdf: The device's PCI bus, device, and function number. + * @domain: The device's PCI domain number (segment). + */ +struct vfio_pci_core_device_ser { + u16 bdf; + u16 domain; +} __packed; + #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index e90859956514..9aa1587fea19 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -81,6 +81,34 @@ struct vfio_device { #endif }; =20 +struct vfio_device_file { + struct vfio_device *device; + struct vfio_group *group; + + u8 access_granted; + u32 devid; /* only valid when iommufd is valid */ + spinlock_t kvm_ref_lock; /* protect kvm field */ + struct kvm *kvm; + struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock= */ +}; + +extern const struct file_operations vfio_device_fops; + +static inline struct vfio_device_file *to_vfio_device_file(struct file *fi= le) +{ + if (file->f_op !=3D &vfio_device_fops) + return NULL; + + return file->private_data; +} + +static inline struct vfio_device *vfio_device_from_file(struct file *file) +{ + struct vfio_device_file *df =3D to_vfio_device_file(file); + + return df ? df->device : NULL; +} + /** * struct vfio_device_ops - VFIO bus driver device callbacks * --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 2604F3612C0 for ; Thu, 29 Jan 2026 21:25:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721950; cv=none; b=aW6e5UnV0088VdRL0+A0koXRkz8tnhwWdi7ijLNhvErdRr0Y2nZqG1VGfLKoHQPdAfCnsGmPRMLe9gmyTRFAoQxvNo4HPiNXvfAoi5Z684MT/f+fWbIi8gAfNTT3QC8C2kEd//EUQy+KiJ9RX34A0jmcKL0IjsvAts2r7iSrToM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721950; c=relaxed/simple; bh=SRC6WyUfpos4EfjDAe8KcuM0pgKR7jThm7ZjQ5NtQv0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OTo5Mj0Pwze5Tmxw2v+awvhcyzUEx++O/j4TPn50dZXWE4VSARt2LOB7dpNgbXmeSmpeGv+o+ngoYc+1V3vcfMQXkmUhiIvvvEtuf77sJn4kPbU1bxPphesxTH0x6jVIWq4SFLMabrrtgMSjT8KXkEEntjA5mVKGT+IFec1t44g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=KxeVlgC6; arc=none smtp.client-ip=209.85.216.74 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="KxeVlgC6" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34c2f670a06so1573331a91.3 for ; Thu, 29 Jan 2026 13:25:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721947; x=1770326747; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TvBnrq4VVaLn6O5AYPXHhriXtNBsa9110uSNH/MSfDg=; b=KxeVlgC6MlgLSZpDVBSKKqgoqQy9CkkT9ocCoxG7UNFMaBwKvBbHnCtQs6T2E8ChJ0 pt0NB8PmPoEbMGDzcRZ4433x5FqaWtYKtDmJGY1ZxZNhbyldGltYcZbUXWBBBXf0O9I7 xRlLUqEyaqyVV1w09g+VVcIRNjQSGAzeq6BTqL9wQ9r0557s7DYgESOGrkMFJsV/Ugqy JgY57agfZ4yxM/ve2ma+RAQ2cv9z9Hzw3QbovOTjiUWN3RiGM2t7aMNCr9VDbLl+IJFM p8+QaUiy+lJproWxDVlXdVpr7xRPS6eaqlN6fArXVXtQMi8AGh5yhzQogSUJVw7F9zOC kY1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721947; x=1770326747; h=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; bh=TvBnrq4VVaLn6O5AYPXHhriXtNBsa9110uSNH/MSfDg=; b=MP3IE3qCRclfGFzeleW56SEF3WqzDd9ftUo49orB+7vLLmUWcRSQIUE1E9hNpaoqJT eV6r+6DQP1hO/TOVUH9lzzSoZPpYNtaivECxfDQGEd2c7AkwI9wa9E/7lnW4QX2WboFl j2GBnWLXwMa14gXMY8i92ylYjjuSpIB2StGkOE35SUjWX8Pd+GbGnjhjuCzsP0qLWY+p /6xavyxz0LEWaRAAxQ8zylKay3ArTA1AKA9K4zMX0X4vaChAWJfE6S7MBJVBOW0EVrn7 8O6hWoK6p7GBg5seYuzsiL3xSQXpeMTPOWaIWQeoN72kcUSm+1u9YdlZLq0sA+xnp5pE 1P6Q== X-Forwarded-Encrypted: i=1; AJvYcCV9T5kvYaVM1afPn2hvXukTamnJMMDNmG/3YJwz+4uT9WVgdxERw9VCXsqCKMKa0LI5Zm+T/mO2xl9PAl0=@vger.kernel.org X-Gm-Message-State: AOJu0Yyd41gsvClWOAbrTmOdodfaEATMEgIFZHStig7fr/xqZjKqs3lE 2HeiqwNfp6JrYEZvDuURfYg32nw79mNOfPkSn/O3BX4/5JHiOXcMmnsvWbmX+2UiCdtg8UuaFlq BwNGMTdjCJ3fp4w== X-Received: from pjbcp23.prod.google.com ([2002:a17:90a:fb97:b0:352:e9d7:ffd2]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d4d:b0:34c:9cec:dd83 with SMTP id 98e67ed59e1d1-3543b3d6641mr750618a91.27.1769721947474; Thu, 29 Jan 2026 13:25:47 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:53 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-7-dmatlack@google.com> Subject: [PATCH v2 06/22] vfio/pci: Retrieve preserved device files after Live Update From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma Enable userspace to retrieve preserved VFIO device files from VFIO after a Live Update by implementing the retrieve() and finish() file handler callbacks. 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. For now the retrieved file is functionally equivalent a opening the corresponding VFIO cdev file. Subsequent commits will leverage the preserved state associated with the retrieved file to preserve bits of the device across Live Update. Signed-off-by: Vipin Sharma Co-developed-by: David Matlack Signed-off-by: David Matlack --- drivers/vfio/device_cdev.c | 21 ++++++--- drivers/vfio/pci/vfio_pci_liveupdate.c | 60 +++++++++++++++++++++++++- drivers/vfio/vfio_main.c | 13 ++++++ include/linux/vfio.h | 12 ++++++ 4 files changed, 98 insertions(+), 8 deletions(-) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index 8ceca24ac136..935f84a35875 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) +int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *= filep) { - struct vfio_device *device =3D container_of(inode->i_cdev, - struct vfio_device, cdev); struct vfio_device_file *df; int ret; =20 @@ -52,6 +46,19 @@ int vfio_device_fops_cdev_open(struct inode *inode, stru= ct file *filep) vfio_device_put_registration(device); return ret; } +EXPORT_SYMBOL_GPL(__vfio_device_fops_cdev_open); + +/* + * 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) +{ + struct vfio_device *device =3D container_of(inode->i_cdev, + struct vfio_device, cdev); + + return __vfio_device_fops_cdev_open(device, filep); +} =20 static void vfio_df_get_kvm_safe(struct vfio_device_file *df) { diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c index f01de98f1b75..7f4117181fd0 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -8,6 +8,8 @@ =20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include +#include #include #include #include @@ -108,13 +110,68 @@ static int vfio_pci_liveupdate_freeze(struct liveupda= te_file_op_args *args) return ret; } =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; + + ser =3D phys_to_virt(args->serialized_data); + + device =3D vfio_find_device(ser, match_device); + if (!device) + return -ENODEV; + + /* + * 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). + */ + 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 out; + } + + ret =3D __vfio_device_fops_cdev_open(device, file); + if (ret) { + fput(file); + goto out; + } + + args->file =3D file; + +out: + /* Drop the reference from vfio_find_device() */ + put_device(&device->device); + + return ret; +} + +static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args = *args) +{ + return args->retrieved; } =20 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *arg= s) { + kho_restore_free(phys_to_virt(args->serialized_data)); } =20 static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops =3D { @@ -123,6 +180,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, }; diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 276f615f0c28..89c5feef75d5 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -1758,6 +1759,18 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr= _t iova, void *data, } EXPORT_SYMBOL(vfio_dma_rw); =20 +struct vfio_device *vfio_find_device(const void *data, device_match_t matc= h) +{ + struct device *device; + + device =3D class_find_device(vfio.device_class, NULL, data, match); + if (!device) + return NULL; + + 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 9aa1587fea19..dc592dc00f89 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -419,4 +419,16 @@ 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) +int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *= filep); +#else +static inline int __vfio_device_fops_cdev_open(struct vfio_device *device, + struct file *filep) +{ + return -EOPNOTSUPP; +} +#endif /* IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) */ + +struct vfio_device *vfio_find_device(const void *data, device_match_t matc= h); + #endif /* VFIO_H */ --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 90B1836212C for ; Thu, 29 Jan 2026 21:25:49 +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=1769721953; cv=none; b=GT4sh/psOgKdw3TuKCvoYwIzGfQV4YQOJKOSESTDT0zA+yGyOUYqUMFPZO6qw/GMzcZoNx1c6jfxrdvo0aXFS6WrZVNUHU6AEb5gGf95QZrVs5EIEIelZP3r4Oz0EE4rGbWy7+H/faVgjmVL88odHhylPMOUouuYDA1kBmvbBOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721953; c=relaxed/simple; bh=s2EF+0wW+b5BmyDvXoLeRzuJ8QMd8hJHZ8kLYdzRY0Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=t7XFO5j5JgHlBbft9iscylLYXWRWC13QDcb7F7QSDyd0RVhwJXF1sxPnsVMNlL+U6Ff6Bxo4zyiWjq8W+8r7TO8a5LAMqOoJIB1HmeEJg/ulRG3S4jJ74rqTSw7A3KkTVX98RklgMk2yD+Uhzv9cUR0V2+0gYIJxS0iuHN4wy/g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=MjNid7Wr; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MjNid7Wr" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2a0a0bad5dfso30902945ad.0 for ; Thu, 29 Jan 2026 13:25:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721949; x=1770326749; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=AVUDzL5NEoLRiR0kq4rtjTLHEzT2c4a+QSQgWmkEpoo=; b=MjNid7WrOyEJT1IuYZdcgcGfdihVHcDFOUlYLCAAuNkFKHRth/gQaAA5frJ1KHYwur kM5Z22z3Y2+cE4N4p8JS27nWJCBRsTo15KFDUdzKxCnr6PYbR/4o9j8xOK8jCAyIoh4A KE1IC+P4h9JuZCG5aOio+d3OVeBq448eX7STBsLxM839FCDNyTuzboldeBZWoY/cx3EN N/RyJkYmSyCdlyGvL3fffoA8tswrFrLYVO9wRG1b1ygGrn4YWuT4DmmqPFuE/rKfHO9c kc2UW9GgqiJvZcyH9VAK1bNVrAQnAM+CshOPTrugIaT00cEpaW/uHFdi20QfcdmEl8bA Ovrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721949; x=1770326749; h=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; bh=AVUDzL5NEoLRiR0kq4rtjTLHEzT2c4a+QSQgWmkEpoo=; b=AWxMiGiAwsblCH09Ye1GMq/pA8Jn2p3VqZtdBrSYXwP5KdVT9s3TreY4QRnkgctDj0 M/HbzTBqzoh/ZXr7uOP/ma6Vaz1DHYd+Q6reWLP47XNxX0oyyfOeWZ9x+sqH6Ks9uJxc OstlTKUBwL/ADJBqpSqDu8F3uoamEeAuw6D27pxkQo4xT4m33Jrf8HPjikXGpRv3kHFS itBrIFm+ssFEWzVEQVYo2tEgWvcNNoH+OcASNHnj0JIcbD6HOgjcE7jPA9EPrkcnXXZG pgyR7JVJGrLADoStZWFr/coRVPdaEGzYn0IuovUBrLnoPzDolG8rAzXRmKHH5SdhI335 D9SA== X-Forwarded-Encrypted: i=1; AJvYcCUOUwbTsktmZAbH18C+41ihouDePZcRX1UsAlHOg89PV4aBLr9j+5728NmeLhxyRYjziRmSveaNuzR6SA0=@vger.kernel.org X-Gm-Message-State: AOJu0YxMQY+yA4OReNxrXl66T3ITmpilUvfavUnrzuwMJo5GVhiPEdFL 33OSV+bnqvmOkg0ZZRhfPwKjYaBT+MavfnhuixA3lswcySFfbXlCzqPNEABQ9Ofd/zhg5kmbkvR PfBlyJkUpnD1yiw== X-Received: from pllm10.prod.google.com ([2002:a17:902:768a:b0:2a0:a0e0:a9c3]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:124c:b0:2a1:3ade:c351 with SMTP id d9443c01a7336-2a8d959a4bamr5657375ad.2.1769721949051; Thu, 29 Jan 2026 13:25:49 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:54 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-8-dmatlack@google.com> Subject: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Notify the PCI subsystem about devices vfio-pci is preserving across Live Update by registering the vfio-pci liveupdate file handler with the PCI subsystem's FLB handler. Notably this will ensure that devices preserved through vfio-pci will have their PCI bus numbers preserved across Live Update, allowing VFIO to use BDF as a key to identify the device across the Live Update and (in the future) allow the device to continue DMA operations across the Live Update. This also enables VFIO to detect that a device was preserved before userspace first retrieves the file from it, which will be used in subsequent commits. Signed-off-by: David Matlack --- drivers/vfio/pci/vfio_pci_liveupdate.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c index 7f4117181fd0..ad915352303f 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -53,6 +53,8 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate= _file_op_args *args) if (IS_ERR(ser)) return PTR_ERR(ser); =20 + pci_liveupdate_outgoing_preserve(pdev); + ser->bdf =3D pci_dev_id(pdev); ser->domain =3D pci_domain_nr(pdev->bus); =20 @@ -62,6 +64,9 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate= _file_op_args *args) =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_outgoing_unpreserve(to_pci_dev(device->dev)); kho_unpreserve_free(phys_to_virt(args->serialized_data)); } =20 @@ -171,6 +176,9 @@ static bool vfio_pci_liveupdate_can_finish(struct liveu= pdate_file_op_args *args) =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_incoming_finish(to_pci_dev(device->dev)); kho_restore_free(phys_to_virt(args->serialized_data)); } =20 @@ -192,10 +200,24 @@ static struct liveupdate_file_handler vfio_pci_liveup= date_fh =3D { =20 int __init vfio_pci_liveupdate_init(void) { + int ret; + if (!liveupdate_enabled()) return 0; =20 - return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh); + ret =3D liveupdate_register_file_handler(&vfio_pci_liveupdate_fh); + if (ret) + return ret; + + ret =3D pci_liveupdate_register_fh(&vfio_pci_liveupdate_fh); + if (ret) + goto error; + + return 0; + +error: + liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh); + return ret; } =20 void vfio_pci_liveupdate_cleanup(void) @@ -203,5 +225,6 @@ void vfio_pci_liveupdate_cleanup(void) if (!liveupdate_enabled()) return; =20 + WARN_ON_ONCE(pci_liveupdate_unregister_fh(&vfio_pci_liveupdate_fh)); liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh); } --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 53730361DDF for ; Thu, 29 Jan 2026 21:25:51 +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=1769721953; cv=none; b=B2Z6Ji5Z6sf2eAljwo5SO8HQFF8xgbv5XkfcjJGcRPtDqahu6XOYO7F8tqikassGyRNEY0uuon/YjtiFJkJtdjkp+HB5sMpbsIL0kx06cU7G6Co6JQs9rwdNQ9uqguIQyPR39hnTkf1lsTxDke4Z1B05S/FZj4qEklQfyFRSD70= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721953; c=relaxed/simple; bh=PH5iGzvJjXTHpagZy3PRNHKNaI45nyOejtuOEQ4Vpv0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jWgs9Zjbd8Us/WXLmMYNvBMUgZJog4hALyhMXBiCIhTXfqEwUlySclrdpFnmrRGP9Ufz3qcvy1AzVVQGxv3VjNV/qa6G4r5H1Q49aHyO8Rf1Apj0CfGFx+kaJc4aq6khAI2cv8aPxtmc++pEI+/1U+aTeeYPnUYPa6Sv0mwxn24= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=C8e2Ortj; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="C8e2Ortj" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a871c32cdbso13762805ad.2 for ; Thu, 29 Jan 2026 13:25:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721951; x=1770326751; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3umikqL/So8idv3VwvFYaIe6+4TeFw4VgnmENinGbaA=; b=C8e2OrtjW0pK6NU4+4pA7apan1abQ64W9AU73v+5MLSgoxU3PFLcQ35UnxyjSkvxW/ jHGFZ0Sly19gY9qnHYAz8wYk5VaqCdHZM2Cics4NzJGADMN6HvBtPJacvT1eGdo2suwt ZRhpTHioWcJ/pWkH0ykH2wBZy2qfsg2XW+OMkV0yYJMVLWQKMlkq16WIsS6txca3VotJ /nsOLnywSf0t5LPsRcB/2fTdZdo82Z7dYxby8mo6zDWAS6vlF4PYmOhwl6or4V/1gc9D hPaf+DOmaNpeQvVe1RnN7NwGGMG7zJ7jnVbWavgGDvg2WFb4KxJJzZQJx5nEe3d+Yclm xQTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721951; x=1770326751; h=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; bh=3umikqL/So8idv3VwvFYaIe6+4TeFw4VgnmENinGbaA=; b=BiGtDurIMH+fYrYnS/6Y0BDHFOv1Eh/Hbwi/YWMOBgkh1y4FPGKjYa7jaJD1EA9kkA 6J/wn90C448eghXFCOzs0+BLnQIhH4Kfrh/xfmdI4xgTuyY2Gry+ErhAcmLqUQmGFMVz FvtJ7qTzgSZVLbp0Wb8RQLBb/S5FrpREEN06tPvaoaiG8p7Wt8n2lIyYXLvzVe4lGbId MhT6V9LIRu7msdvJynNJgwZV5ERYwtgjldD0HYgpKLyIS27RskUPlJAFO/2b1mH3sW+L WLt7EoRrFFq4R/ka+BaRvIvDwKgHZxj25/UPnwi1uCWIx7ATK3JI9Y70WPCX9ke1/CjF hYpg== X-Forwarded-Encrypted: i=1; AJvYcCUFiQpZ4ECQjlWj0ZpsA8PgBsTP0GMdiI6gl2rjoPq8aa9bceisKGvxDateB+eZtud6GcpKCTXAc3BMxY0=@vger.kernel.org X-Gm-Message-State: AOJu0YyzyE7auVPdfW8z1V9d2diGu3RHW4IEKguzsCGJGNwtn5uSIrbt xaU2tC2L+AOXeEyaY0GLT9zrrakPnzYXc+PmpXuIZvfhBSm8H5/zhrhFwzpPNJpjDZZ9gsErQLs 1lyqP4O3TnFbxCg== X-Received: from plrf5.prod.google.com ([2002:a17:902:ab85:b0:2a7:6c0c:5916]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c952:b0:295:3584:1bbd with SMTP id d9443c01a7336-2a8d8176d76mr6976645ad.41.1769721950767; Thu, 29 Jan 2026 13:25:50 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:55 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-9-dmatlack@google.com> Subject: [PATCH v2 08/22] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: David Matlack --- drivers/vfio/device_cdev.c | 4 ++++ drivers/vfio/group.c | 9 +++++++++ include/linux/vfio.h | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index 935f84a35875..355447e2add3 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -57,6 +57,10 @@ int vfio_device_fops_cdev_open(struct inode *inode, stru= ct file *filep) struct vfio_device *device =3D container_of(inode->i_cdev, struct vfio_device, cdev); =20 + /* Device file must be retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD */ + if (vfio_liveupdate_incoming_is_preserved(device)) + return -EBUSY; + return __vfio_device_fops_cdev_open(device, filep); } =20 diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index d47ffada6912..63fc4d656215 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -311,6 +311,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/include/linux/vfio.h b/include/linux/vfio.h index dc592dc00f89..0921847b18b5 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -16,6 +16,7 @@ #include #include #include +#include =20 struct kvm; struct iommufd_ctx; @@ -431,4 +432,21 @@ static inline int __vfio_device_fops_cdev_open(struct = vfio_device *device, =20 struct vfio_device *vfio_find_device(const void *data, device_match_t matc= h); =20 +#ifdef CONFIG_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 to_pci_dev(d)->liveupdate_incoming; + + return false; +} +#else +static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_devic= e *device) +{ + return false; +} +#endif + #endif /* VFIO_H */ --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 663EB35E536 for ; Thu, 29 Jan 2026 21:25:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721955; cv=none; b=rY3m9dnI2nfFKrtAyxZG6tKHLfhCJn2OAJAYCphXg457/EzsAFkuHXWoSOL6K7tMy7uVq5/BeQXZx7P0cnBU41ni9m3XNJlBkBGoTVaGNcgESE6yEF377PjlMEYS8FtN+xj6pmfOJXlO3g4Qgv8Pp/BzFMTQoGDDsgkT2kEs1HI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721955; c=relaxed/simple; bh=6IdtaI14wSLFnlm3AePOxVdQnZ5Ce/BaKfnD8oLedO0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=O3CC1ujZhzEmH6gCdxXXcYDUmzA7DK/Ey7ZTpiG/XmnWb80k0IIwp/cuJD627rymBKgy4+1gE/FvOtSL6XQWfISg9avdlawEODEoMuXmXYIjc33rjZG1v06Ylj4Zzpzf6JZCUB7LWccs4Id5B2P7phyw1A6D+y1XDyCarT5+CyI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2b2iQaws; arc=none smtp.client-ip=209.85.216.74 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2b2iQaws" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34c93f0849dso1506142a91.1 for ; Thu, 29 Jan 2026 13:25:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721952; x=1770326752; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cEITHmxK+p0fg2PKlGTWLUl48Yr7XTXbjGIRUwhwM+A=; b=2b2iQawsuPGFdxNaG5DH0hw0rX+Sqvp6lcSMbvGtXOPbhnGelfqgN3xGH8EIEeaqLM MWu+vEK8CeOtWWmjKilHqHyrBwyaXmEe46T9N+cw5YAXzBwF9T8blgw1yE+JxCy/Jxco Pxo2hJ+Jxf/qwb9gO6AoYjGm1Z+iX+in76+iq2cuwkei8ZGaV3jevtB9ODT4pQuJ7re/ 6TFO29PI7rslUwKTqHnG1A524axdk9oAhdD5huZ2N8TOU+8yayQPfX/9fezyK9B5AkGU t3EE3LQcNqDBMJ5LYwz8P5dllqo0HzvspUHB/e/CXHBRon9adexg1bP5Oi1YIHovGq3u CbIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721952; x=1770326752; h=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; bh=cEITHmxK+p0fg2PKlGTWLUl48Yr7XTXbjGIRUwhwM+A=; b=f8XEVYmH7GYaiNoMpCgY9Q6evDTJrmhNpyEiKL1nXfwQqFgbOiuXsnRZqD/Nir1aBC dXEEeI3gN3OEjOD6dGddlF9c6k6wkqGHlSQdsdIYx+9HbxI2oCABZra3+K8hWrgh+bVq Du/+yX8Cz8/6Brow4guYZgqtIUhki2i+d0Xv5EGNK6aKhp2O9SyHQnlKbNYq6pzUFOr7 W9WksY1JK5W9WKsVQCN8uUorNTp5IIH3R3Bh41Mvg9a8RW3Fz0B/paK+VJHAORgV/VDB 5H6vbQ7iCEVRNrVv0LQuGN5yzsfqC/LA5dLhQGqbEw43H/ltTAcWCkR1+og2fDkKvol0 D2AQ== X-Forwarded-Encrypted: i=1; AJvYcCXuHv9pkPQiGkOk4kjKn0NhwKah6HLe9hUx3egRoAiq+rXCMnjWT+T4oCgKg+1PoGyCeOA5WkmNrpmwRL4=@vger.kernel.org X-Gm-Message-State: AOJu0YzhypcFa1n/jVBnkXKTb+D+2G09DQKOiwJ4BcKWnKoY6JSPzGzF 5Q388o3KafSRpQOvmnjVeBnZe86RGTf3Bij1JvT+czzxnFzliZXdinqSxd9yDDjwRbWn5jTHWLB OqtDZoBWZejfknw== X-Received: from pjk11.prod.google.com ([2002:a17:90b:558b:b0:34c:489a:f4c9]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3c10:b0:353:2972:74a4 with SMTP id 98e67ed59e1d1-35429af8f7amr3444629a91.13.1769721952367; Thu, 29 Jan 2026 13:25:52 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:56 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-10-dmatlack@google.com> Subject: [PATCH v2 09/22] vfio/pci: Store incoming Live Update state in struct vfio_pci_core_device From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Stash a pointer to a device's incoming Live Updated state in struct vfio_pci_core_device. This will enable subsequent commits to use the preserved state when initializing the device. To enable VFIO to safely access this pointer during device enablement, require that the device is fully enabled before returning true from can_finish(). This is synchronized by vfio_pci_core.c setting vdev->liveupdate_incoming_state to NULL under dev_set lock once it's done using it. Signed-off-by: David Matlack --- drivers/vfio/pci/vfio_pci_core.c | 2 +- drivers/vfio/pci/vfio_pci_liveupdate.c | 17 ++++++++++++++++- include/linux/vfio_pci_core.h | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 3a11e6f450f7..b01b94d81e28 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -569,7 +569,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *v= dev) if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev)) vdev->has_vga =3D true; =20 - + vdev->liveupdate_incoming_state =3D NULL; return 0; =20 out_free_zdev: diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c index ad915352303f..1ad7379c70c4 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -131,6 +131,7 @@ static int match_device(struct device *dev, const void = *arg) static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *ar= gs) { struct vfio_pci_core_device_ser *ser; + struct vfio_pci_core_device *vdev; struct vfio_device *device; struct file *file; int ret; @@ -160,6 +161,9 @@ static int vfio_pci_liveupdate_retrieve(struct liveupda= te_file_op_args *args) goto out; } =20 + vdev =3D container_of(device, struct vfio_pci_core_device, vdev); + vdev->liveupdate_incoming_state =3D ser; + args->file =3D file; =20 out: @@ -171,7 +175,18 @@ static int vfio_pci_liveupdate_retrieve(struct liveupd= ate_file_op_args *args) =20 static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args = *args) { - return args->retrieved; + struct vfio_pci_core_device *vdev; + struct vfio_device *device; + + if (!args->retrieved) + return false; + + device =3D vfio_device_from_file(args->file); + vdev =3D container_of(device, struct vfio_pci_core_device, vdev); + + /* Check that vdev->liveupdate_incoming_state is no longer in use. */ + guard(mutex)(&device->dev_set->lock); + return !vdev->liveupdate_incoming_state; } =20 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *arg= s) diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 1ac86896875c..350c30f84a13 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -143,6 +143,7 @@ struct vfio_pci_core_device { struct notifier_block nb; struct rw_semaphore memory_lock; struct list_head dmabufs; + struct vfio_pci_core_device_ser *liveupdate_incoming_state; }; =20 enum vfio_pci_io_width { --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 D1EC5364038 for ; Thu, 29 Jan 2026 21:25:54 +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=1769721956; cv=none; b=m5d+DXJ9nJgipNl8iyNC6DdZ+YCCr5d9fzzACuW4hX00ZaXXyDMkxGeXhiSjUmY6DXHdt5dumQnGnd+05lk/UDcUfyDNmPEH3NwzpEBSciluxRd3zXMYVIP3H8owu3m3G8UhkpTVMnS+rrfORknww8TFk9TQkv6O8kYAJDncO/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721956; c=relaxed/simple; bh=a+0/tE04p22lp6sOOwH4YlMRI/tEgdEnUCc2v9hrZNg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EGI9lnZa+9Ssgc31y/Lf4j0cMIutMM+vfGEeT7Py4nbcY9VOOk1oE9z2NHX01MOsmV1lM7Rr9cxyJqZTPrZ6WmVVfiKBKUwIozy15dms1p4wwq6d0IZcQUAFhxQ4GzRAhLCa9lo8LOdmNcUWhwNuWprGTHSyaHNFDf6ORIL0EIQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=V+wI6RtB; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="V+wI6RtB" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a351686c17so11574075ad.0 for ; Thu, 29 Jan 2026 13:25:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721954; x=1770326754; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Ha2krivTi79qOGY9TNrpoF2Kn7NOugjkcaAbsESD6eI=; b=V+wI6RtBEKGX/Fwog7ZpkPux3Grm+DRByFarIYW9X9UZUWZw08uoezcY3k3//4eVLK Ii0d4yLzLzwB1oR/oTfl+qsqVyBgtpR+9jYzC7XAAxZendDZJdtq/Z5CwAhzCg8TkpBr 7oQLtRpXEM1CiXVXJxM6IvwOuX/DXkKmhyC71s/JLwRVdFFIOVk+bFTW14kDQyQhZenT +js8UIHWg8KBT9LbpO1KtROQF2CI6T47CC2G85sUIye9WsmbF7FiFfSrj9DEAKbbceR7 J6T2AH3sczRVsTrjPNYx/l5ECouQE1pHCyimQ49l4hsuIxhoTyBcwWmpSF8WthM2Y/YH Q+Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721954; x=1770326754; h=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; bh=Ha2krivTi79qOGY9TNrpoF2Kn7NOugjkcaAbsESD6eI=; b=pSM9mYJx1pN6x279/9JDDgSQzMH5+TtuLUU4Yb3IIEo6/C21pUwXPpMIDJGbbgTD3L e/F8zGPk9XHOGc+4WtxYAneUMGj1Nhv7aAUl3+rfkhmmsvG2luXyfRMMGxLtXgGQHiP8 5GfDD1TLqKI5310gc19N7XIDfh0h0BdroEmtSoCEimRUe8FX6aNrrJTffX4qP64jPT2E ohgjJ9tkmMqNExgP2rD2MleWo6leq4b1sAM/TDgcf+omHWoP5SXa7BoI4NkE2qfKjK19 EnG9s/t7xGGI+LIAA+KvFrqm/X0GWIHWKxZMBIin6Sqj37w93v0851+aVXCQQ12sDNOu 6QZw== X-Forwarded-Encrypted: i=1; AJvYcCUcaoZv7tZ7nToKedMsN3WVGrHbyx89DUlSLbCDqkOQIhO5IUmm/lnsCkK5lLiBagPl4okSO/U4IuFyafE=@vger.kernel.org X-Gm-Message-State: AOJu0Yxy73cSMEFayOTUuLjGpQtMKg0gZDkY58ONnoHzKKzZU9aUg0o5 WZy10cMBemwoip5mIs/7Mn1BUwMOAJ56fmk3N8bmwKHv+m5DFmAfrljmiAHcjXrZ5YPXQSCrWt9 oQJATZoSOJIjidA== X-Received: from plba14.prod.google.com ([2002:a17:903:100e:b0:29f:1fb1:be7e]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d4c9:b0:2a7:99c9:1086 with SMTP id d9443c01a7336-2a8d9a56c95mr4558805ad.47.1769721953965; Thu, 29 Jan 2026 13:25:53 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:57 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-11-dmatlack@google.com> Subject: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma Do not reset the device when a Live Update preserved vfio-pci device is retrieved and first enabled. vfio_pci_liveupdate_freeze() guarantees the device is reset prior to Live Update, so there's no reason to reset it again after Live Update. Since VFIO normally uses the initial reset to detect if the device supports function resets, pass that from the previous kernel via struct vfio_pci_core_dev_ser. Signed-off-by: Vipin Sharma Signed-off-by: David Matlack --- drivers/vfio/pci/vfio_pci_core.c | 22 +++++++++++++++++----- drivers/vfio/pci/vfio_pci_liveupdate.c | 1 + include/linux/kho/abi/vfio_pci.h | 2 ++ include/linux/vfio_pci_core.h | 1 + 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index b01b94d81e28..c9f73f597797 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -515,12 +515,24 @@ int vfio_pci_core_enable(struct vfio_pci_core_device = *vdev) if (ret) goto out_power; =20 - /* If reset fails because of the device lock, fail this path entirely */ - ret =3D pci_try_reset_function(pdev); - if (ret =3D=3D -EAGAIN) - goto out_disable_device; + if (vdev->liveupdate_incoming_state) { + /* + * This device was preserved by the previous kernel across a + * Live Update, so it does not need to be reset. + */ + vdev->reset_works =3D vdev->liveupdate_incoming_state->reset_works; + } else { + /* + * If reset fails because of the device lock, fail this path + * entirely. + */ + ret =3D pci_try_reset_function(pdev); + if (ret =3D=3D -EAGAIN) + goto out_disable_device; + + vdev->reset_works =3D !ret; + } =20 - vdev->reset_works =3D !ret; pci_save_state(pdev); vdev->pci_saved_state =3D pci_store_saved_state(pdev); if (!vdev->pci_saved_state) diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio= _pci_liveupdate.c index 1ad7379c70c4..c52d6bdb455f 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -57,6 +57,7 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate= _file_op_args *args) =20 ser->bdf =3D pci_dev_id(pdev); ser->domain =3D pci_domain_nr(pdev->bus); + ser->reset_works =3D vdev->reset_works; =20 args->serialized_data =3D virt_to_phys(ser); return 0; diff --git a/include/linux/kho/abi/vfio_pci.h b/include/linux/kho/abi/vfio_= pci.h index 9bf58a2f3820..6c3d3c6dfc09 100644 --- a/include/linux/kho/abi/vfio_pci.h +++ b/include/linux/kho/abi/vfio_pci.h @@ -34,10 +34,12 @@ * * @bdf: The device's PCI bus, device, and function number. * @domain: The device's PCI domain number (segment). + * @reset_works: Non-zero if the device supports function resets. */ struct vfio_pci_core_device_ser { u16 bdf; u16 domain; + u8 reset_works; } __packed; =20 #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */ diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 350c30f84a13..95835298e29e 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -16,6 +16,7 @@ #include #include #include +#include =20 #ifndef VFIO_PCI_CORE_H #define VFIO_PCI_CORE_H --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 C131835F8B7 for ; Thu, 29 Jan 2026 21:25:56 +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=1769721958; cv=none; b=Sh6q7txgI6JkftT29Osu+evlU9vs5BlVZjr3o5/T3f5Cdli2YrNgVhbA4qR8xc8UvaoLd0vghEiKkbaRcmaK1JB7blRAMsDEJ9ZGV9W8yMdxVpzgLyaxqPg9SfJREaGbDDwmn5Hlb6HP9/FIOV8Tm/T5+yOLhHkp4wtX8oNDqdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721958; c=relaxed/simple; bh=Fptw60PqRkYzWIuTBN2mK+X1wys6jPz6VcpFsR3yJlM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=e5Q5kihq9VKZ4Mgv2I2eDYrZyaEGo5oIC9pCO8QET9gRTLjmG/eKH1fPy5F1OFsAVY1WHdRt3CCkwLrOIlkdkTANYXYIAN7GyaKEiiTP72tKx5bfVi6CSDz+OOwptTXJ918yQxR+NXJW9avnqeVh+yejbImDqhuNm9ftTDUa2LY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aP8+0eiA; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aP8+0eiA" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34c704d5d15so2642608a91.1 for ; Thu, 29 Jan 2026 13:25:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721956; x=1770326756; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ibCvH5eZEobsLpnry66xHV5xBXd91tAWSqhhlaOXScs=; b=aP8+0eiArsIVTvtj7o4LSZBOiYFcpm8isKZLT2N+x+NokzE1S91UrUx2UMIGAt30gw 5hDPtgzFsLczo/KKP7qTLwA2Ywm/oA/qL2DPBHoiRyFayDjk8RtnzfjRkaJQvgN+5upp YgxKRwWUSrtL6Ap+r/ohSX5tDHFa3n8CzRb8Oe4e+ab+S3Nd60Sfowe+tboV5/BPWn93 0/rruoBRES59bxrvygKvX62Lm31AJAa2z7Ucftv9W1vSlKuIDI04oix8WwVjHMOAeYqe XqOTUV++UcionZ9h9ekwVJ5IrqdlvwKDp7lCKnb64VYdBfnLJKKa7oIxufYJId0Uqkao fg6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721956; x=1770326756; h=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; bh=ibCvH5eZEobsLpnry66xHV5xBXd91tAWSqhhlaOXScs=; b=pnfs8arngxJW6DE9tHZAU1G3z+JZjWcN77HA7ydaNNvyRZkiDPVCwQgb8uXqwnTUg7 jg2rWreD1fwFQ18pEXeX7FsXDXh/AXOumXGWu2ga8tYP+aMbe0qrUlSprdWjW08+ztef Gi3q7SwDVuUzZWu+fMR2QF6xrocWx8tmBiToBmsPZIZsqxGx/wOtVQjN/LyZ2JAMU+4Z 8aRAOUw+4RkxhOXr58LMimC6uyonv+BbWufPAtPyhESX0EKbgDpZjd1kgcvBz7epdKIn iL3AZxZ+hfJk2E3yK+nAZx4QbOGJR/VMn0Sh9fMW54Nkki9HKLzJYTza6KRej4sjF8g7 1TXg== X-Forwarded-Encrypted: i=1; AJvYcCUs6mJxjhJfuZ/xjmfI3L0f3nQHPKamGXLJ/CpRX/2lvxNrtKPMCAhI9/68Vh7EuXbndqxGpy94uDxF0yQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyroMJzyfEGGEyEJ5v/U/NfmRh+R7vsai6Fpsm4Pr8FTFU4KMLr xu7fRdpIgoatcIyshLYO6YCMq8Z6ojEelHdN+51ZBlewm3RCAdHgRlKxwBX/mYHwL/T2WGR9y1o mD3wUgcjFFXuLzQ== X-Received: from pjbpv18.prod.google.com ([2002:a17:90b:3c92:b0:33b:ba24:b207]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3ccd:b0:352:bd7c:ddbd with SMTP id 98e67ed59e1d1-3543b38af32mr802532a91.23.1769721955711; Thu, 29 Jan 2026 13:25:55 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:58 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-12-dmatlack@google.com> Subject: [PATCH v2 11/22] docs: liveupdate: Document VFIO device file preservation From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add documentation for preserving VFIO device files across a Live Update, as well as some generic file preservation documentation. This documentation will be extended in the future as new types of files are supported and new dependency/ordering requirements are added. Signed-off-by: David Matlack --- Documentation/userspace-api/liveupdate.rst | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/Documentation/userspace-api/liveupdate.rst b/Documentation/use= rspace-api/liveupdate.rst index 41c0473e4f16..dbf1e4aeddd7 100644 --- a/Documentation/userspace-api/liveupdate.rst +++ b/Documentation/userspace-api/liveupdate.rst @@ -14,6 +14,150 @@ ioctl uAPI =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D .. kernel-doc:: include/uapi/linux/liveupdate.h =20 +File Preservation +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Files can be preserved across Live Update in sessions. Since only one proc= ess +can open /dev/liveupdate, sessions must be created by a centralized process +(e.g. "luod") and then passed via UDS to lower privilege processes (e.g. V= MMs) +for them to preserve their own files. + +luod:: + + luo_fd =3D open("/dev/liveupdate", ...); + + ... + + // Create a new session with the given name. + struct liveupdate_ioctl_create_session arg =3D { + .size =3D sizeof(arg), + .name =3D SESSION_NAME, + }; + ioctl(luo_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &arg); + + // Send session_fd to the VMM over UDS. + send_session_fd(..., arg.fd); + +VMM:: + + // Receive the newly created session from luod over UDS + session_fd =3D create_session(SESSION_NAME); + + ... + + // Preserve a file with a unique token value in the session. + struct liveupdate_session_preserve_fd arg =3D { + .size =3D sizeof(arg), + .fd =3D fd, + .token =3D TOKEN, + } + ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg); + +Files can be unpreserved with the LIVEUPDATE_SESSION_UNPRESERVE_FD ioctl. = They +are also unpreserved once the last reference to the session is dropped. To +carry preserved files across a Live Update, references must be kept on the +session files through the reboot(LINUX_REBOOT_CMD_KEXEC) syscall. + +While a file is preserved in a session, the kernel holds an extra reference +to it to prevent it from being destroyed. + +Only the following types of files support LIVEUPDATE_SESSION_PRESERVE_FD. = More +types of files are expected to be added in the future. + + - memfd + - VFIO character device files (vfio-pci only) + +File Retrieval +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Files that are preserved in a session retrieved after +reboot(LINUX_REBOOT_CMD_KEXEC). + +luod:: + + luo_fd =3D open("/dev/liveupdate", ...); + + ... + + struct liveupdate_ioctl_retrieve_session arg =3D { + .size =3D sizeof(arg), + .name =3D SESSION_NAME, + }; + ioctl(luo_fd, LIVEUPDATE_IOCTL_RETRIEVE_SESSION, &arg); + + // Send session_fd to VMM over UDS. + send_session_fd(..., arg.fd); + +VMM:: + + // Receive the retrieved session from luod over UDS + session_fd =3D retrieve_session(SESSION_NAME); + + ... + + // Retrieve the file associated with the token from the session. + struct liveupdate_session_retrieve_fd arg =3D { + .size =3D sizeof(arg), + .token =3D TOKEN, + }; + ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg); + + ... + + ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, ...); + +A session can only be finished once all of the files within it have been +retrieved, and are fully restored from the kernel's perspective. The exact +requirements will vary by file type. + +VFIO Character Device (cdev) Files +=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 + +The kernel supports preserving VFIO character device files across Live Upd= ate +within a session:: + + device_fd =3D open("/dev/vfio/devices/X"); + + ... + + ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, { ..., device_fd, ...}= ); + +Attempting to preserve files acquired via VFIO_GROUP_GET_DEVICE_FD will fa= il. + +Since the kernel holds an extra reference to files preserved in sessions, = there +is no way for the underlying PCI device to be unbound from vfio-pci while = it +is being preserved. + +When a VFIO device file is preserved in a session, interrupts must be disa= bled +on the device prior to reboot(LINUX_REBOOT_CMD_KEXEC), or the kexec will f= ail. + +Preserved VFIO device files can be retrieved after a Live Update 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, ...); + +Prior to LIVEUPDATE_SESSION_FINISH, preserved devices must be retrieved fr= om +the session and bound to an iommufd. Attempting to open the device through +its character device (/dev/vfio/devices/X) or VFIO_GROUP_GET_DEVICE_FD will +fail with -EBUSY. + +The eventual goal of these support is to preserve devices running uninterr= upted +across a Live Update. However there are many steps still needed to achieve= this +(see Future Work below). So for now, VFIO will reset and restore the device +back into an idle state during reboot(LINUX_REBOOT_CMD_KEXEC). + +Future work: + + - Preservation of iommufd files + - Preservation of IOMMU driver state + - Preservation of PCI state (BAR resources, device state, bridge state, .= ..) + - Preservation of vfio-pci driver state + See Also =3D=3D=3D=3D=3D=3D=3D=3D =20 --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 35C72365A1A for ; Thu, 29 Jan 2026 21:25:57 +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=1769721960; cv=none; b=oVAwcPF3dCK65XAocc6kINMGSOtlWr0MFk2yzY01X5m0B+t78BYruKRG+9syyvQDkhTrNU2ZZGeiVZJY7XjVBha5Afcsytxn1Xn4FB+Hfsw2O3FxQ13jjvG7llSIU8xHOiGH/6y93GWSoKqPQ4yBb6Uoye7SrcCfE1JZ47C4nKU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721960; c=relaxed/simple; bh=iPn4pDoAWI4g0pJVI1rNQDofz3/2KTtO9Jp6Bq5j67U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bY2gTmeZEb/mQJ/0GVNOZRI13eT/8EpxiNAYHQILzDZe2NoyBPP1nVJz/a2QB1Khw40z30GPz4rEL1yVMmnEygLOMfvd/9ebfPOVXxvkkoaXPUTF89SzXg5YF/aiDw+MEBwD9d8cY60hjMzB7LE4ndwKFZwr7pdnX7BEGPzHqgY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uVO572HN; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uVO572HN" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a0e952f153so35451925ad.0 for ; Thu, 29 Jan 2026 13:25:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721957; x=1770326757; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Hp3td6uMAaTZjQV6oFbLUGjv1pGfeS9Cq/BNEG9/dt4=; b=uVO572HNSVbXcjicVqxDYowjIz5A9b59NGNJ+BCwmCK0ahnq3H1IveFLIpPu11cvP7 GUKLuRDJUc+OLeV90GvSJNsEi8TAYJIAiekIsYt792Yy5IvT/XVjbV+gEpoDR8f5Sgew 850JkkjK80jrKuP0jOp2Nc0Kc1Hef6GMe/CY3/1wfypAwSNJ5r6ZuLlNzCcZw1WVgqby 4rMY7zUXrzdyONw7+UXRaj9kbxVAo9owhDydPIo1hC3Yl2kOodmLAEf4I8LNbMwWQAIZ v+EefPJ/DFr+iPMaPkweKnngU6MiIY+FmXIfW2/3y8BICRLbeyBvBkz7HNmoC4KnqTzR QT2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721957; x=1770326757; h=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; bh=Hp3td6uMAaTZjQV6oFbLUGjv1pGfeS9Cq/BNEG9/dt4=; b=EO0m5V3i3kLGAVlFD7Q1BHEi25Vv8RkmosjbQj2mP+ReQyCkDgPaMVIuJO+5vp5CCB 4f1z9/lPJrjJu9mNRLcQdUEfrorR/VHLHCf4jtGakr0lUdEEYmLHGFofei576I1yuJ6J NE65lf4Ej8ImvYGsOEi6USc7zucjQqZEz4k4L8V3qtWXoM3C2ka4TOXOlk8v9pTID44p uCuuIMC+DQ9B36xtSCOOkoqTG0eJCk+AJ19GQJAWMavNq0x42LVgmPyUBjbWTp74jiMv H5LtwYDwusSuGRJAAYK6J5Ajj+uwHGtdYdn69hI3bWAsNib80nr4KHzw0S0tPrPHFNBe w7aw== X-Forwarded-Encrypted: i=1; AJvYcCX5at5UhBSBVamMqzeRwVBE7NDCCTCVqfb3DxuRo/mAqT4SE6a/o2f0TJfiwQsX2mFBbGXchZt6sSE8tR0=@vger.kernel.org X-Gm-Message-State: AOJu0YxXwKtC2PQF1sLvIpEa/NgWHPtHJHEhzmYmV3RErO2VBp/5PeZv mScOj8nqXa7Pf2NPnwSNsCCw4iCYDO9IDd/s0Mdn1ykoRmZaVvvLp6etzmfXKVmyZWZ+KKNsfAn izgqa+vw8Df+bkw== X-Received: from plgb8.prod.google.com ([2002:a17:902:d508:b0:295:50ce:4dd]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:388e:b0:295:596f:8507 with SMTP id d9443c01a7336-2a8d89464b3mr6954275ad.0.1769721957393; Thu, 29 Jan 2026 13:25:57 -0800 (PST) Date: Thu, 29 Jan 2026 21:24:59 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-13-dmatlack@google.com> Subject: [PATCH v2 12/22] selftests/liveupdate: Move luo_test_utils.* into a reusable library From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma Move luo_test_utils.[ch] into a lib/ directory and pull the rules to build them out into a separate make script. This will enable these utilities to be also built by and used within other selftests (such as VFIO) in subsequent commits. No functional change intended. Signed-off-by: Vipin Sharma Co-developed-by: David Matlack Signed-off-by: David Matlack --- tools/testing/selftests/liveupdate/.gitignore | 1 + tools/testing/selftests/liveupdate/Makefile | 14 ++++--------- .../include/libliveupdate.h} | 8 ++++---- .../selftests/liveupdate/lib/libliveupdate.mk | 20 +++++++++++++++++++ .../{luo_test_utils.c =3D> lib/liveupdate.c} | 2 +- .../selftests/liveupdate/luo_kexec_simple.c | 2 +- .../selftests/liveupdate/luo_multi_session.c | 2 +- 7 files changed, 32 insertions(+), 17 deletions(-) rename tools/testing/selftests/liveupdate/{luo_test_utils.h =3D> lib/inclu= de/libliveupdate.h} (87%) create mode 100644 tools/testing/selftests/liveupdate/lib/libliveupdate.mk rename tools/testing/selftests/liveupdate/{luo_test_utils.c =3D> lib/liveu= pdate.c} (99%) diff --git a/tools/testing/selftests/liveupdate/.gitignore b/tools/testing/= selftests/liveupdate/.gitignore index 661827083ab6..18a0c7036cf3 100644 --- a/tools/testing/selftests/liveupdate/.gitignore +++ b/tools/testing/selftests/liveupdate/.gitignore @@ -3,6 +3,7 @@ !/**/ !*.c !*.h +!*.mk !*.sh !.gitignore !config diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/se= lftests/liveupdate/Makefile index 080754787ede..a060cc21f27f 100644 --- a/tools/testing/selftests/liveupdate/Makefile +++ b/tools/testing/selftests/liveupdate/Makefile @@ -1,7 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only =20 -LIB_C +=3D luo_test_utils.c - TEST_GEN_PROGS +=3D liveupdate =20 TEST_GEN_PROGS_EXTENDED +=3D luo_kexec_simple @@ -10,25 +8,21 @@ TEST_GEN_PROGS_EXTENDED +=3D luo_multi_session TEST_FILES +=3D do_kexec.sh =20 include ../lib.mk +include lib/libliveupdate.mk =20 CFLAGS +=3D $(KHDR_INCLUDES) CFLAGS +=3D -Wall -O2 -Wno-unused-function CFLAGS +=3D -MD =20 -LIB_O :=3D $(patsubst %.c, $(OUTPUT)/%.o, $(LIB_C)) TEST_O :=3D $(patsubst %, %.o, $(TEST_GEN_PROGS)) TEST_O +=3D $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED)) =20 -TEST_DEP_FILES :=3D $(patsubst %.o, %.d, $(LIB_O)) +TEST_DEP_FILES :=3D $(patsubst %.o, %.d, $(LIBLIVEUPDATE_O)) TEST_DEP_FILES +=3D $(patsubst %.o, %.d, $(TEST_O)) -include $(TEST_DEP_FILES) =20 -$(LIB_O): $(OUTPUT)/%.o: %.c - $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ - -$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: %.o $(LIB_O) - $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIB_O) $(LDLIB= S) -o $@ +$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: %.o $(LIBLIVEUP= DATE_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_= O) $(LDLIBS) -o $@ =20 -EXTRA_CLEAN +=3D $(LIB_O) EXTRA_CLEAN +=3D $(TEST_O) EXTRA_CLEAN +=3D $(TEST_DEP_FILES) diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.h b/tools/te= sting/selftests/liveupdate/lib/include/libliveupdate.h similarity index 87% rename from tools/testing/selftests/liveupdate/luo_test_utils.h rename to tools/testing/selftests/liveupdate/lib/include/libliveupdate.h index 90099bf49577..4390a2737930 100644 --- a/tools/testing/selftests/liveupdate/luo_test_utils.h +++ b/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h @@ -7,13 +7,13 @@ * Utility functions for LUO kselftests. */ =20 -#ifndef LUO_TEST_UTILS_H -#define LUO_TEST_UTILS_H +#ifndef SELFTESTS_LIVEUPDATE_LIB_LIVEUPDATE_H +#define SELFTESTS_LIVEUPDATE_LIB_LIVEUPDATE_H =20 #include #include #include -#include "../kselftest.h" +#include "../../../kselftest.h" =20 #define LUO_DEVICE "/dev/liveupdate" =20 @@ -41,4 +41,4 @@ typedef void (*luo_test_stage2_fn)(int luo_fd, int state_= session_fd); int luo_test(int argc, char *argv[], const char *state_session_name, luo_test_stage1_fn stage1, luo_test_stage2_fn stage2); =20 -#endif /* LUO_TEST_UTILS_H */ +#endif /* SELFTESTS_LIVEUPDATE_LIB_LIVEUPDATE_H */ diff --git a/tools/testing/selftests/liveupdate/lib/libliveupdate.mk b/tool= s/testing/selftests/liveupdate/lib/libliveupdate.mk new file mode 100644 index 000000000000..fffd95b085b6 --- /dev/null +++ b/tools/testing/selftests/liveupdate/lib/libliveupdate.mk @@ -0,0 +1,20 @@ +include $(top_srcdir)/scripts/subarch.include +ARCH ?=3D $(SUBARCH) + +LIBLIVEUPDATE_SRCDIR :=3D $(selfdir)/liveupdate/lib + +LIBLIVEUPDATE_C :=3D liveupdate.c + +LIBLIVEUPDATE_OUTPUT :=3D $(OUTPUT)/libliveupdate + +LIBLIVEUPDATE_O :=3D $(patsubst %.c, $(LIBLIVEUPDATE_OUTPUT)/%.o, $(LIBLIV= EUPDATE_C)) + +LIBLIVEUPDATE_O_DIRS :=3D $(shell dirname $(LIBLIVEUPDATE_O) | uniq) +$(shell mkdir -p $(LIBLIVEUPDATE_O_DIRS)) + +CFLAGS +=3D -I$(LIBLIVEUPDATE_SRCDIR)/include + +$(LIBLIVEUPDATE_O): $(LIBLIVEUPDATE_OUTPUT)/%.o : $(LIBLIVEUPDATE_SRCDIR)/= %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + +EXTRA_CLEAN +=3D $(LIBLIVEUPDATE_OUTPUT) diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c b/tools/te= sting/selftests/liveupdate/lib/liveupdate.c similarity index 99% rename from tools/testing/selftests/liveupdate/luo_test_utils.c rename to tools/testing/selftests/liveupdate/lib/liveupdate.c index 3c8721c505df..60121873f685 100644 --- a/tools/testing/selftests/liveupdate/luo_test_utils.c +++ b/tools/testing/selftests/liveupdate/lib/liveupdate.c @@ -21,7 +21,7 @@ #include #include =20 -#include "luo_test_utils.h" +#include =20 int luo_open_device(void) { diff --git a/tools/testing/selftests/liveupdate/luo_kexec_simple.c b/tools/= testing/selftests/liveupdate/luo_kexec_simple.c index d7ac1f3dc4cb..786ac93b9ae3 100644 --- a/tools/testing/selftests/liveupdate/luo_kexec_simple.c +++ b/tools/testing/selftests/liveupdate/luo_kexec_simple.c @@ -8,7 +8,7 @@ * across a single kexec reboot. */ =20 -#include "luo_test_utils.h" +#include =20 #define TEST_SESSION_NAME "test-session" #define TEST_MEMFD_TOKEN 0x1A diff --git a/tools/testing/selftests/liveupdate/luo_multi_session.c b/tools= /testing/selftests/liveupdate/luo_multi_session.c index 0ee2d795beef..aac24a5f5ce3 100644 --- a/tools/testing/selftests/liveupdate/luo_multi_session.c +++ b/tools/testing/selftests/liveupdate/luo_multi_session.c @@ -9,7 +9,7 @@ * files. */ =20 -#include "luo_test_utils.h" +#include =20 #define SESSION_EMPTY_1 "multi-test-empty-1" #define SESSION_EMPTY_2 "multi-test-empty-2" --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 DDE6135FF7D for ; Thu, 29 Jan 2026 21:25:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721961; cv=none; b=RHWv5u8zVSgkc20PMiW3NiD8ans/k93D31quILttKUnBQhr3pxLWEYd79HRr/qkpqL9Cq56BWNvYfz+TjmMd6ajaTiQwThkZx3OeLtwkd1OlMvRxWgRd8HOlgUjP0BoOsCA07dHxjsouffUksMLxU5T3XEELEZeadbbTeglY4h0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721961; c=relaxed/simple; bh=FPm4HNnNzdGsASTEmXYmmsAPSB4aul9GWDReAyxeCCY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=R1h8MWs0ZkMFOKY3mVm67BiqhwGv+At9oNanuA0N6V5ZUeqi3VjRpwHCg8vwTXzpQGPUB54+YZMnn7S3+zXtEhqtMWgRzh2o2wQeXBpaCJ0jcnbjGCC7IZFT8/LKtY+oO3I9tOFSeCij/yEPAYzxucaOrMVf0YXxnlIbgJ5j08E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jeUpRM1U; arc=none smtp.client-ip=209.85.215.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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jeUpRM1U" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c634b862fcfso895216a12.2 for ; Thu, 29 Jan 2026 13:25:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721959; x=1770326759; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=idHpW3SDccKJdv6+7GcWw8jBEpKmDKEMPgP0EwwfxqU=; b=jeUpRM1UNmTsBxYdioDrYyaaBwCFGiaoczwyCBJZM67st2YYKUK208OKLs7RYsr+D1 BJBExVhnR5YILu3UlQ3Ik9nU/V3io0yTjFysavSShZS6Zi83Moo6S/iiOg9okaftHKTG /OCM7VqwrAJ1caIGChnC1gcg4HOBFTLLhphWpuY3BACjk3dqIz/7O6mFsy/A6PPjLunC ObFwNqzIUX+EtQiMLStK0f3Zvp+ugXw9XHCOG10iSBdzrs/04Mf0Pl994mDUK0jvpWBt kA1Dh67dlTNA3ioHsoQdQlnsjWivaZ3NzH7oihtDOml5t+98mMjV8eXnd9FeNtdw6B7p xneA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721959; x=1770326759; h=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; bh=idHpW3SDccKJdv6+7GcWw8jBEpKmDKEMPgP0EwwfxqU=; b=ahsTzD1fJXy8uLXQc47qxw+ErDcpRcNCNUP1+BmThZN5MittnhQpiDexCNO2Rwtddf omNOmxYPSKCzrUk+cv1usFo8+mgQVaRH0YbP0kwRsZ6/eeA3jvmO+lqyuwK26onJEW3l 720yxyHCb1aiHrC69RaBaALxCkC39IugT3E7E0tFIo0uote2vP3n3oC4CcByntXV7Oe4 c1AuDe47YUCpAFFQ4/rfwmR1m9yfjqUss3cDllxrIbr2/gr/hzwpzwPqgidcqk2jpo0I lqm7FVhOYuAvas/iVbhF+zwtRd0l8ejxUt+UMta+oVZ5Gs8GWFRsj8CJRwJ1h38osNF7 /3Cg== X-Forwarded-Encrypted: i=1; AJvYcCUW4l/5zqYxS1JhWa/t85DtU4PdjcXSbGM2ifhf7U0jP3MBIO1UNdiEAU86+msygm6kzAcZAYrtvkBaXtE=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3ywnpUq9aUfUV1AvjpsDh9Cg6Ni6ZxcsQpXxYnUzsaXfxYSpz Wl1DluckxhbgZl3CbMVaAGFc+qKzv0YfOw9F5oOQjm6aoJ5dfpb1uCJfEqImPkxz90Mu7Euv4a6 kLOys2fN9Q53UkA== X-Received: from pgdj29.prod.google.com ([2002:a05:6a02:521d:b0:c5e:d16c:917e]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3392:b0:366:581e:1a11 with SMTP id adf61e73a8af0-392e0148145mr472290637.57.1769721958852; Thu, 29 Jan 2026 13:25:58 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:00 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-14-dmatlack@google.com> Subject: [PATCH v2 13/22] selftests/liveupdate: Add helpers to preserve/retrieve FDs From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma Add helper functions to preserve and retrieve file descriptors from an LUO session. These will be used be used in subsequent commits to preserve FDs other than memfd. No functional change intended. Signed-off-by: Vipin Sharma Co-developed-by: David Matlack Signed-off-by: David Matlack --- .../liveupdate/lib/include/libliveupdate.h | 3 ++ .../selftests/liveupdate/lib/liveupdate.c | 41 +++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h= b/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h index 4390a2737930..4c93d043d2b3 100644 --- a/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h +++ b/tools/testing/selftests/liveupdate/lib/include/libliveupdate.h @@ -26,6 +26,9 @@ int luo_create_session(int luo_fd, const char *name); int luo_retrieve_session(int luo_fd, const char *name); int luo_session_finish(int session_fd); =20 +int luo_session_preserve_fd(int session_fd, int fd, int token); +int luo_session_retrieve_fd(int session_fd, int token); + int create_and_preserve_memfd(int session_fd, int token, const char *data); int restore_and_verify_memfd(int session_fd, int token, const char *expect= ed_data); =20 diff --git a/tools/testing/selftests/liveupdate/lib/liveupdate.c b/tools/te= sting/selftests/liveupdate/lib/liveupdate.c index 60121873f685..9bf4f16ca0a4 100644 --- a/tools/testing/selftests/liveupdate/lib/liveupdate.c +++ b/tools/testing/selftests/liveupdate/lib/liveupdate.c @@ -54,9 +54,35 @@ int luo_retrieve_session(int luo_fd, const char *name) return arg.fd; } =20 +int luo_session_preserve_fd(int session_fd, int fd, int token) +{ + struct liveupdate_session_preserve_fd arg =3D { + .size =3D sizeof(arg), + .fd =3D fd, + .token =3D token, + }; + + if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg)) + return -errno; + + return 0; +} + +int luo_session_retrieve_fd(int session_fd, int token) +{ + struct liveupdate_session_retrieve_fd arg =3D { + .size =3D sizeof(arg), + .token =3D token, + }; + + if (ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg)) + return -errno; + + return arg.fd; +} + int create_and_preserve_memfd(int session_fd, int token, const char *data) { - struct liveupdate_session_preserve_fd arg =3D { .size =3D sizeof(arg) }; long page_size =3D sysconf(_SC_PAGE_SIZE); void *map =3D MAP_FAILED; int mfd =3D -1, ret =3D -1; @@ -75,9 +101,8 @@ int create_and_preserve_memfd(int session_fd, int token,= const char *data) snprintf(map, page_size, "%s", data); munmap(map, page_size); =20 - arg.fd =3D mfd; - arg.token =3D token; - if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg) < 0) + ret =3D luo_session_preserve_fd(session_fd, mfd, token); + if (ret) goto out; =20 ret =3D 0; @@ -92,15 +117,13 @@ int create_and_preserve_memfd(int session_fd, int toke= n, const char *data) int restore_and_verify_memfd(int session_fd, int token, const char *expected_data) { - struct liveupdate_session_retrieve_fd arg =3D { .size =3D sizeof(arg) }; long page_size =3D sysconf(_SC_PAGE_SIZE); void *map =3D MAP_FAILED; int mfd =3D -1, ret =3D -1; =20 - arg.token =3D token; - if (ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg) < 0) - return -errno; - mfd =3D arg.fd; + mfd =3D luo_session_retrieve_fd(session_fd, token); + if (mfd < 0) + return mfd; =20 map =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, mfd, 0); if (map =3D=3D MAP_FAILED) --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 8340F3612FF for ; Thu, 29 Jan 2026 21:26:01 +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=1769721967; cv=none; b=bsuTwUMFOumnVhDWEfzzNz+ZJcxl8LQh3CbPLPc6NEWvuHqaR//DYXT9Tvu6GgIpeTJOs3+TaO5c09wh0XU4V/LLJXf2KUQKgcuvt7EwRyBSvQwf7jhwVKuF59tSWe3FhtvfZPKxu5znT/FVd4EksrVi538NGwg98RVcYAUW1Ss= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721967; c=relaxed/simple; bh=Jis9QmSRTC6AxZZguKb1BxVkXKYzE5EgFNnbHekO2IA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=shZYVglnxqDK0VSHGn4Grz7mz8tl8lKU9cdjD+3hwUsqd6yRSrqJtUCiZFp3sdaM1ApFzX9aPSbyaUF06HKlNROskVsVRPS2i9jahPeB2dvZ52HGrQ4dVekUt4vjCZcPa03ZtEJZJomwJgNBdpeKaoMXgNy6eKtbe6KFyXfpu00= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oqTfR/f3; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oqTfR/f3" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-29f1f69eec6so15835635ad.1 for ; Thu, 29 Jan 2026 13:26:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721961; x=1770326761; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dy0UJfAzqC2Uv0TXXgbyaGP8I7SQ7WbjV2I2sUTFK/o=; b=oqTfR/f3sqqiJ4FsX88tf5lJRqyrnV9wLcLhcRoljtXnLfCrOFKWavxNkiKmIH6Lf/ obTwP9dc1q9yI06Uco2+Eio3WlraRyysYY2O1uiNFNel7ZR8K2xJkeTcWT6n1paa9OUt bwLmvpaxrIo4e7NtHZrSikfRWtxgW2mSE2an0DyCG61PBkn4A3SJsMaLPSAjagp70Ce1 NZys+z5B4PRKdU4v+k+MjDrJzXE/10hNDhraM2qxClOrwvsY76Ye99wMUCNaZSLLKDTk Kz2D44wxl29hiOchwksRPp9xMSXXOvAS6xY4a/qelLonzm6D3VybaFCyDqQCUzD0p+xA c9Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721961; x=1770326761; h=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; bh=dy0UJfAzqC2Uv0TXXgbyaGP8I7SQ7WbjV2I2sUTFK/o=; b=Wh2pxVRSG5fcQxniP2J+lwbORL8Y9Ov8zMhTaR3k1kicD3T+96XHNJ+1AMDGbKBJf9 fMXbb/CYuEXmg6htb3rzZH3dz00bM5W2SVC9WxsJKcFtFhNdzWLE8GPNG4h3E3jXTJGV yzfddz0/Uj+HiY0beWrhCsUEHdGqZuNaXPOGhBlZ7iTFMEwYPTJP7Cl8CrILfbtzSr1p w5iBuirYcyKk8hCzABr2KTaZwDr/JkeRm9q51edQ/uKxBMxHR9j7WKdm5wOV3wwxf5c+ RLGwm8yd5bWGpJA04FawTN0G6Me4VoJXwC6u/Ll97Zral81RNXlTisabjWyIViqA89Fa rqwQ== X-Forwarded-Encrypted: i=1; AJvYcCUI0Q4wF6M5efIuy2Ev7Z9be8Xb1GpqREeQ/SXBbe3LFGjArgdWVutQz79P36lL+LHGKiWKAYu192d14jE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0C7Ynsyj2HLNfyo28UxXlRUM0FoVEDrUd9BpMuGwk0jSVVwQ/ MEsHG9Tuyae6R4Vv4cTSFiFNRux41asyx3uCom/TEBnnKBZlIudwUqHLSccZBQCz5b9IkdCqiPk CkoDc9keHEux3lg== X-Received: from plrp10.prod.google.com ([2002:a17:902:b08a:b0:2a0:8ca0:1e55]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:290d:b0:2a0:dabc:1388 with SMTP id d9443c01a7336-2a8d990adfcmr3786725ad.28.1769721960616; Thu, 29 Jan 2026 13:26:00 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:01 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-15-dmatlack@google.com> Subject: [PATCH v2 14/22] vfio: selftests: Build liveupdate library in VFIO selftests From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma Import and build liveupdate selftest library in VFIO selftests. It allows to use liveupdate ioctls in VFIO selftests Signed-off-by: Vipin Sharma Signed-off-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 3c796ca99a50..1e50998529fd 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -12,6 +12,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 @@ -19,11 +20,15 @@ CFLAGS +=3D $(EXTRA_CFLAGS) =20 LDFLAGS +=3D -pthread =20 -$(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) - $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ +LIBS_O :=3D $(LIBVFIO_O) +LIBS_O +=3D $(LIBLIVEUPDATE_O) + +$(TEST_GEN_PROGS): %: %.o $(LIBS_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBS_O) $(LDLI= BS) -o $@ =20 TEST_GEN_PROGS_O =3D $(patsubst %, %.o, $(TEST_GEN_PROGS)) -TEST_DEP_FILES =3D $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O) $(LIBVFIO_O)) +TEST_DEP_FILES :=3D $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O)) +TEST_DEP_FILES +=3D $(patsubst %.o, %.d, $(LIBS_O)) -include $(TEST_DEP_FILES) =20 EXTRA_CLEAN +=3D $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES) --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 97308366826 for ; Thu, 29 Jan 2026 21:26:03 +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=1769721968; cv=none; b=fsuf+R+fEoYpPxYrXWCVQrAaTsGKAr4LIOQwOwCniepB6Ia3EKEfgg7PrpjA//1QBQEVib1vuwAAb+L/C1nZ4wr/k95NeMjKaM+sRfIYZjuWyR61rg7j+BUnFEhSWD+Z61UCEy7qTmgQCpxYvCR3T88WXKWrcQtOCTO34sHfAdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721968; c=relaxed/simple; bh=Cb9Y8KWzYaZZbnWLogkDgrwZwr7o6CbuJ0QS3Pv10oU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZvBUEbNWmg8S6iXKQ0MexuJoGjfYo3FGp8/x4/v6hl/nRz4LPxEbpm1OoD29VCvkB2KEyJ26KU0KY6c+djNlz4f/vfFChAAepKi+WQGZeyUEOPt73YelHdabNROt3VPyMyyHIT3hR2fe45hVM9NHNS4x9erg3XZU1h9UmwG+p9c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qlo9Thx/; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qlo9Thx/" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a377e15716so35010425ad.3 for ; Thu, 29 Jan 2026 13:26:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721963; x=1770326763; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BqEhbyqURPyKNgE6Ne2XGuBboHLh1jY5l9yG8cIgxTg=; b=qlo9Thx/qUtFYUjR0DRugLS6QCrbGk5U06fts1AWz8wt5TC5cAgDGcawCCt2DLOE2W gNijW6XfVYFUXEdOqlExgu7MxNAayZ60sMdzjbFpAIatXqO7sNwdUb+dc86rVsJejpNL Bw/pl6J5liIYsbPCjrGFjlZVQUZp9HZ1xAaGv49KXOhAnN4UnGSsLGb6Q6x3fSG4aE0Y T8oIL0jtc0aUTVgDtsS7qZZuXCA2iANYhQPII3RXmJtf7l4+1kMKnXe18ZUKu7qo9Eqe V175GSQtTKMKbsYbzP6hH/5SW+RauqUtq6JFmKRQC82vgoD0sDqQxaIKzwctxgVL0n0s b5Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721963; x=1770326763; h=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; bh=BqEhbyqURPyKNgE6Ne2XGuBboHLh1jY5l9yG8cIgxTg=; b=Paje0boYHMxvYFX7reCJB7K0EnPd/wActOP/ttDfV2jjRXMVDqJqGxnOuOKorAHjMV Y4WpQDOFR8JJUEIIpv41q28wAJXCaaDo1MDW+FerPGGYZlb1TAI4KJrR+Og+sw8XTIzq gyWK+smCCEhIxqTttOmJK+tdpUOPETCi9O/mWLwUyJD+V/qQFaEhogWL6e1xNBdgB8HI 7IqwzbQuAhedQDYjVTx2s7rKllWviio6EBN/cilK6ZlaYN5B3GEpu00ckEY74rW5HZYE Py7LAnJpX1FEufjuD41s5FPQY8A1gDUpIuMLdeiEfLYG9CO53WFPmGA1gJ8fSuSUuSPI oHnQ== X-Forwarded-Encrypted: i=1; AJvYcCWLnnS3KgMrtYz1dwKjcgI4ZM8kohWO47EX3FSZIa+G89VsvsBYnZ4AxEzQz16dd6reVaAMyYH3ZoVhjXQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1uNTlpwxTJsfM8RlOMHa/3FrrOMnvMH9TFE8O2icrlkeSDpMs /PG3y7q9IqZMJrvteSKu+XQ8g7EDgkZFY622XD3WZIvMCocQUDdISpX5UgWirGPMU05Ma5USaAY RzGBzVa8vYrl/2g== X-Received: from plot3.prod.google.com ([2002:a17:902:8c83:b0:2a7:78b9:f962]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:bd03:b0:2a7:a9b8:ebb0 with SMTP id d9443c01a7336-2a8d96a7ff3mr3875585ad.19.1769721962280; Thu, 29 Jan 2026 13:26:02 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:02 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-16-dmatlack@google.com> Subject: [PATCH v2 15/22] vfio: selftests: Add Makefile support for TEST_GEN_PROGS_EXTENDED From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- tools/testing/selftests/vfio/Makefile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 1e50998529fd..f9c040094d4a 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -23,12 +23,15 @@ LDFLAGS +=3D -pthread LIBS_O :=3D $(LIBVFIO_O) LIBS_O +=3D $(LIBLIVEUPDATE_O) =20 -$(TEST_GEN_PROGS): %: %.o $(LIBS_O) +$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): %: %.o $(LIBS_O) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBS_O) $(LDLI= BS) -o $@ =20 -TEST_GEN_PROGS_O =3D $(patsubst %, %.o, $(TEST_GEN_PROGS)) -TEST_DEP_FILES :=3D $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O)) +TESTS_O :=3D $(patsubst %, %.o, $(TEST_GEN_PROGS)) +TESTS_O +=3D $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED)) + +TEST_DEP_FILES :=3D $(patsubst %.o, %.d, $(TESTS_O)) TEST_DEP_FILES +=3D $(patsubst %.o, %.d, $(LIBS_O)) -include $(TEST_DEP_FILES) =20 -EXTRA_CLEAN +=3D $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES) +EXTRA_CLEAN +=3D $(TESTS_O) +EXTRA_CLEAN +=3D $(TEST_DEP_FILES) --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 826A335F8B7 for ; Thu, 29 Jan 2026 21:26:04 +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=1769721966; cv=none; b=QY69D6BLgmTEvdOWBisHM3CoCK18y1gghXUA+CN95e1gfcs3v46ZzKwPOlAMGs6pOk1uRQ3uy2pBeXhsV8AMUmnr12v//RMeMnWcgmso+w9QlFxike550MithRp+C83SP/lSY1gI8kTCef03ZTIeO1JQiOTrosIpWhA8L9+6i/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721966; c=relaxed/simple; bh=vXiU4Ip82P/IqvKktUHvsDLiorMl5x2Nv3JVfbu29NU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LT0ZpPtaBE8wC4EoDjBvSp7Iik36jwqwLeogAHr9IdtpOdB4E+Kz9kK0j7ds1L4YHM4ZdHe+7sHrjWIjc2R+wvRaEEnfTkMV9oz08JxhvUGnz39Cn+oRn/LJJ2CIcSOpy/Ln9MSkEfc+xi9R4f1gZJ1w7GATRa+gBy5oGiSNT/8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=m9IHXB9C; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="m9IHXB9C" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34c48a76e75so1221010a91.1 for ; Thu, 29 Jan 2026 13:26:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721964; x=1770326764; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FUlLWwYhjyjIuuZGYT6Vav103VXsQgg/WHiCQN706aw=; b=m9IHXB9Cp6Jpi/nyTdRxHE4/6DN9hBJh1C0uJSuyvJueP+8OMKZWh0SGIrQhiyqDfl Sz+ZsTHN0And5FHO2urqkhKIGez5ZeKvLPrlsFmwjcaSWDxL0yTcBpaAXE/N/sXV8xhE GRIpC+Le3zB4DCpTMbt8X1b6Xjk9U9xEzjPC9ZRHvk8vemfabwetCV277An8UxAlHGPA o3xx0/3YQMU43654akvq35FFB8LpHO/8FYUbHA4W6nQzAezBqb7Gf0ZzUdXhv62No2U8 mSNEuOcWbNfrO8n1SS/AemlBqWKpoIp4C3JYlS0gUUyc5DHPnwY9fW+BAR2H2HJRfIGv k2wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721964; x=1770326764; h=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; bh=FUlLWwYhjyjIuuZGYT6Vav103VXsQgg/WHiCQN706aw=; b=GQI4tskWQOQB2YWlp53zeXhgHlyBvJVQSoWLF4pDSSGLG5zfgAaxI/sKg/jLR7gcVh DVjC9RsDWW7UhZ+6K53AjxBL+mKNGvoZq0oKmssKRG7Femo7MP/PL7BUgVou++GFKmSK JX76Rx+dGubJK0e/b6M3VvuQDwjbFCy92ooDyFrgbUZr8xiTM9Hp7pAoqKI3cSmXGLOU O19Ha445rnj+nyRQqHpeE/EVvNEl3D/5n9gk4dvQijRadXNC2XYVkl1slVBQ2oj0I+Yh 78+gOR1kGJ9PMCLLwBMNRvIY3xzRhQaC1bK2H/GBCDWl+ew3I/Ij+FihjbavpmhbLMfI cFEg== X-Forwarded-Encrypted: i=1; AJvYcCVzYVZSZyMVtwpbJZt8ms8HwE8t6+6md0+0k1bz8+4fNvPXyHjbgIBi9eIh7V3O0vpqIqQ/gcng8gRGTZM=@vger.kernel.org X-Gm-Message-State: AOJu0YxKzQ0HKuL6AC+CFOONpsa9+GEB1+2CwjyPOydwOc0+mu8uFZ6A u3YmduPrw973Rn56PsOwoiPJjC7w00swgzT9HFi/kR4ETmMpxOdakPIWAtTPd88M54QGeht65JW NA0ZfqwdQfO1Sew== X-Received: from pjps20.prod.google.com ([2002:a17:90a:a114:b0:352:ca2d:ce63]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d64e:b0:341:2b78:61b8 with SMTP id 98e67ed59e1d1-3543b3d65f8mr671636a91.20.1769721963975; Thu, 29 Jan 2026 13:26:03 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:03 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-17-dmatlack@google.com> Subject: [PATCH v2 16/22] vfio: selftests: Add vfio_pci_liveupdate_uapi_test From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a selftest to exercise preserving a 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 --- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_pci_liveupdate_uapi_test.c | 93 +++++++++++++++++++ 2 files changed, 94 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 f9c040094d4a..666310872217 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -4,6 +4,7 @@ TEST_GEN_PROGS +=3D vfio_iommufd_setup_test 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_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..3b4276b2532c --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c @@ -0,0 +1,93 @@ +// 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_GE(self->luo_fd, 0); + + self->session_fd =3D luo_create_session(self->luo_fd, "session"); + ASSERT_GE(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) +{ + vfio_pci_device_cleanup(self->device); + iommu_cleanup(self->iommu); + close(self->session_fd); + 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) + return; + + 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) + return; + + 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) { + printf("open(%s) failed: %s, skipping\n", LUO_DEVICE, strerror(errno)); + return KSFT_SKIP; + } + close(fd); + + device_bdf =3D vfio_selftests_get_bdf(&argc, argv); + return test_harness_run(argc, argv); +} --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 935E9366DCB for ; Thu, 29 Jan 2026 21:26:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721968; cv=none; b=aQQwpMraDEjABjcryLpBNnVL60cl6Wf/2ZtoMFV4mB/k2Z0tVSqjHn3zyRJCCT+0/2axK0TBaxQmndoFhDwjIYJxJjlOc1RGkTOJrlHDPPo8RW8ZNtlZ2goMtU5+vJanEdYDNkQgVS2Q+PABe/lIFLS4nXZcyc0FhyVkAIGzHEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721968; c=relaxed/simple; bh=PAHReE1IBOjJ0GFM21TbxazaUD0k808+5LV95g4EPBk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cCCznFxEtkFbegdhRuWDyMEALtcs1bfXPOq3lcfFSbKJQeXutQ2tcTx8o42AI1f8fRiClgl5vKQhA8HD8gHL1yHYBiN6HfgjlhsUHlSnE8CISUqAME8tGIYq5SnFQad4DZD33+p+aZ1hRS4L3fSj9Ow0N7+KfAyLDCB2Miu1ofc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uolrwHUK; arc=none smtp.client-ip=209.85.215.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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uolrwHUK" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c503d6be76fso2852092a12.0 for ; Thu, 29 Jan 2026 13:26:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721966; x=1770326766; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lEyO5iRGiN+9INh5ffm6tFBNJ0ZoJ+9Z1mvOjrL+acQ=; b=uolrwHUK+ADR1fqMMEb7X3dZUBZuYgaFaErvFKNbRPZulQfQ1BRIW+G8SMHgPhtDv8 SFpDkB9ZiaRkBqiPbzplTtnkWTuxlT/qqAMaCrkF9y5rYVTjOXbLETArjnTJIUtofL+F yKF9J28YHUWFklDs1q70eeAN7n4bzZcj7BtQa51xsNtHBV1wwUxQXMYglPBclTastCLc RW9A94PdSRu4ONAS1cGJdATVlH5M+E2N5oHpT0SRCxAasPJ4r6IYky9zgg9fdUCTWsUc pS0LvR/e6kvriGtEck1sgynOqFh+xpmP4vPOGYCpFrf+/K15WQTZH05yPJyJCvOES9J2 8/FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721966; x=1770326766; h=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; bh=lEyO5iRGiN+9INh5ffm6tFBNJ0ZoJ+9Z1mvOjrL+acQ=; b=JxCrQMKYg6e3uvtd0GaKNVQ2TCTJBNGeq374GQpg4GXM1lcdZNba6efsRvFjZVIROV cqLTJ0Jqx95UI6a11F1TUK1sBDy3ISuo2uDkIYjlVwJxK142RgztcDwAYMrLOZc1V1t3 +c0SGuOwiQ0CYVGYQqycw/dyM3SQuUr4Bf5RBTBDoVoVZ25qu5oMA0Zr3A7hVG/7xJOh Xn1p+r7/I6+6R9K6cCXyR39dzeq2uH8ypHED9bqkn7TesTEX8sc1yDUGqflSp6YPo9hC UOSFvgFmySy4Go7+zEymkFEG3Yj32ZoovRrIHjztfuv/Cs7lcJaE4MNBjOBbW5MWa36q zuAg== X-Forwarded-Encrypted: i=1; AJvYcCVw+aCqZb6Mrbbhnkx7u5pyJaIglU2gxXY2v8LAiHjA4EqSs5+zMym4IsabV0bwbbPSdym/TM7DXjF3+ro=@vger.kernel.org X-Gm-Message-State: AOJu0YzaQDMfh54PGoBhEaw5Q90a+1KPmImtV8Kd3GsJDXAO54tg+0bq RrlLjoduqP4pPBxuNESaVQjV/hvxxSv+eEtMPzzztOz/wBhncgLISSwuwVbN+pJFQ2Lcf7Q5C6o Q2QRrMuoq0yoMmw== X-Received: from pgbdo6.prod.google.com ([2002:a05:6a02:e86:b0:c08:9db4:d5cb]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6f89:b0:35c:e441:e6d2 with SMTP id adf61e73a8af0-392dfff5a08mr409686637.7.1769721965922; Thu, 29 Jan 2026 13:26:05 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:04 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-18-dmatlack@google.com> Subject: [PATCH v2 17/22] vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma 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 to write selftests where VFIO device FD is preserved using liveupdate and retrieved later using liveupdate ioctl after kexec. Signed-off-by: Vipin Sharma Co-developed-by: David Matlack Signed-off-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 3 ++ .../selftests/vfio/lib/vfio_pci_device.c | 33 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 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 2858885a89bb..896dfde88118 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 @@ -38,6 +38,9 @@ struct vfio_pci_device { #define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_= ARGS__) #define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf,= ##__VA_ARGS__) =20 +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 fac4c0ecadef..08bb582eaa8f 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -318,19 +318,27 @@ static void vfio_device_attach_iommufd_pt(int device_= fd, u32 pt_id) ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args); } =20 -static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const c= har *bdf) +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, int device_fd) { - const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); + const char *cdev_path; =20 - device->fd =3D open(cdev_path, O_RDWR); - VFIO_ASSERT_GE(device->fd, 0); - free((void *)cdev_path); + if (device_fd >=3D 0) { + device->fd =3D device_fd; + } else { + cdev_path =3D vfio_pci_get_cdev_path(bdf); + device->fd =3D open(cdev_path, O_RDWR); + VFIO_ASSERT_GE(device->fd, 0); + free((void *)cdev_path); + } =20 vfio_device_bind_iommufd(device->fd, device->iommu->iommufd); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =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 @@ -341,10 +349,12 @@ struct vfio_pci_device *vfio_pci_device_init(const ch= ar *bdf, struct iommu *iomm device->iommu =3D iommu; device->bdf =3D bdf; =20 - if (iommu->mode->container_path) + if (iommu->mode->container_path) { + VFIO_ASSERT_EQ(device_fd, -1); vfio_pci_container_setup(device, bdf); - else - vfio_pci_iommufd_setup(device, bdf); + } else { + vfio_pci_iommufd_setup(device, bdf, device_fd); + } =20 vfio_pci_device_setup(device); vfio_pci_driver_probe(device); @@ -352,6 +362,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.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 34551368282 for ; Thu, 29 Jan 2026 21:26:10 +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=1769721977; cv=none; b=q56Sf3COlDAVq2xzwOYW4EpT5qH1TtzGjw/y3PeK4h7zVit60p7pg3QQf2LqFo0EEgCyuMAzLSTXsupfEbc3yIqTMiYa0fry7sb50ImgGp5rDHaRYPwJ4aWWP4X8yF4EQbSbELnK5S1lPkf+fq8erUe1krNP8tz6EPRGGlwMY2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721977; c=relaxed/simple; bh=PuQG/fB45n7aUAZcDzwF1eani2iX652Um8BM/GdHrKU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G/UuYtxhO+e29fpfpdSSWHBRV9rboFfEZGZEByQXgRMd03pe3YXzrCkF7Jp1ZgRiJ+tWxr1rkbtBRFMPBmNZodpJoo6NDXEU9lNANb5kgf+Lkf0+pPg1UY7TuhFm55k6MNGmU/8t4pyawi1Qow5SnpMV9S2hmmP7Az8Wfde5CZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YZ16ggfg; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YZ16ggfg" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2a863be8508so17026035ad.2 for ; Thu, 29 Jan 2026 13:26:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721968; x=1770326768; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JG18MDRMH4VUqggxYa+ZNtdyrKhzyTrjCDPr+CW7RY4=; b=YZ16ggfgThMakvaPkiOwKSqLjIUwlXsrv8pgb1dsBaIOedEBTeBjltvRX1/yqNjM0u 94PCo03HyzNhKd44zCNmWyv5jQY03/ruvL0b4U/Dtwxk1Ck33gdIIXralwPwZAJo5hyS cRrJASefFpbCGG+uKT5IV/83eBDcfT56ZHiUVVy+LtVlKZFDGlddxW4Fm3fiWWrxgiNX rX5AmbN6eFTWHFdUZ1OSXofIXK0ji9K/BMP3mBVKzBmXkagvO2k+0RcFFAsJGxUGF8Bg G9KMhERvbDQVz85QKtJPVkUZ6xnszjnKNcp6iwtdRk4Zh98Dy0WGLyciGtbUFu5GVYFM rpWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721968; x=1770326768; h=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; bh=JG18MDRMH4VUqggxYa+ZNtdyrKhzyTrjCDPr+CW7RY4=; b=vVVX9xNR6PcP4wKgJFiDWcpXOK+2WTaUPNwLN7yClpkh9H50kC8p4RmRYKmwmwoHth 0fAjxKFWmr/H4HdGKJSxIEMJsjgtC4u+jRGRh+K7nM4Q1zinmlIJPIg+bgjLwEkRMr78 qzMPxJVVMr+vC6DBbgp1DF9YESDLllEtmmNGOsZ9yFPG293NnvwenPD7WIW3s252DqwN qNQy9Ca9nK9/y0L8P+sHWVXx6M6k8Q2J0/UVcBxAHc8XKJy7zAmyEqOeOcP5pTg59bat kCBNHqFab7MlItQ1+ERGmCLz+WYDxNB+xh+eIVNvIT9AvH7ZkEH3utO11VSlEyeeAU3r rh3w== X-Forwarded-Encrypted: i=1; AJvYcCWZbyzF+lNNjJxUDnZR5K6vEYMLaDfiBQGu3/Rm2ZALbkWVq0Dcyx/nfR1Ek3G/e8apX/YhGT32OoF468Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yx0P5QsfmIbnYiwT+FMOkg6FKBeTiJ1Shl1683QYgHFPjJIWPeK pO7yE+5qpptNxj9dSkEBBVfoMiLxWdyc8GUq3ezu0+FS8FHCQiv4BSvM/E81I/ehdiidLlMD/XM LjfjUqs1WJvooYA== X-Received: from pldv20.prod.google.com ([2002:a17:902:ca94:b0:29f:2b44:973b]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1c1:b0:2a7:90a5:2c95 with SMTP id d9443c01a7336-2a8d819ac05mr6111855ad.51.1769721967422; Thu, 29 Jan 2026 13:26:07 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:05 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-19-dmatlack@google.com> Subject: [PATCH v2 18/22] vfio: selftests: Add vfio_pci_liveupdate_kexec_test From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma 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. Signed-off-by: Vipin Sharma Co-developed-by: David Matlack Signed-off-by: David Matlack --- 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 666310872217..55d685f6e540 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -6,6 +6,10 @@ TEST_GEN_PROGS +=3D vfio_pci_device_init_perf_test TEST_GEN_PROGS +=3D vfio_pci_driver_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.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 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 A397C367F5D for ; Thu, 29 Jan 2026 21:26:09 +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=1769721971; cv=none; b=uo+rR/1UI9/Um8WYdNcDAFPQthVJDeYhba+dRppo4nF6iJUI5IMRxZtTKHnpUtR2E3f86c6GAuskOy4cQK1KlsOUUBqodHuKSl9PQgUhEBQ/AgrkBboJyhZtsVyOblZGbq/dKnbH9diRrin4YU8VEp1WWho+m3k10niJG2jd088= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721971; c=relaxed/simple; bh=rzLzfQ5aFdVRSJeSbIlzRD0ZQoe/xL5btWLwt6zemMY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JGRXuhsmQL76bIlG6yexmOOXckcLEgJW72bYbH8Q8CPq65ZTetXcVe08k9MertdSO7n3qbhI1uuBIyUEfUHMK8S2dPJK4wJvnfjuaTskigmuBdNoYDdT5Bx6d1CK14KvfaVR42AsSuHKbMCxZJfOdZrjHtdb6GlFyO9aCUInzQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vsHoIDiV; 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vsHoIDiV" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34c704d5d15so2643030a91.1 for ; Thu, 29 Jan 2026 13:26:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721969; x=1770326769; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xZYm6U95PKyVs4z00Hg/ZV7BBbgjTCKK+U9M7txSstE=; b=vsHoIDiV6OWKfXOkJEefD3nmxTq9d2bBOte3nbIHo+29FIKL60dkUKiWNqXiDl9nDv awlgm3pxEarSdD7EE4VW3Gmf4p8/DaaSugzyDq+2X7+s14Zgrw6pEfDtS+iTNjyCDp9p dWqPR02UGc/eaSUxhVq6J257dprQyiO5GeZujJcVnnILUHOYgsMMWGFuJj2fFZ/8PiQA xUfoNirro9duFMPPjTKZZK/EF5kbF+GRW8c5f6aruQtRtEPiWR1/kTpt64owru6EqXmY CsMJ0G18oEja26QXnLXNJcapsZN0Rz7Ll7zFk2rp8JtBIRap8A+iGWQCjuMEygvPYuIr 4V6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721969; x=1770326769; h=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; bh=xZYm6U95PKyVs4z00Hg/ZV7BBbgjTCKK+U9M7txSstE=; b=oqMDefyuWXP8Z3Jj4t+qlwTAczKSbrOSXXl8doEX055r+GtGIk3gf17LgHUvrvFMni Ol5AXlMBWR6TSxPkqyQqKdlEclSTAQVajinrimo9WSwx6KcJYlKH72++aQ2W8Gtk4y9x 0V1AGPkjf1dKoNZrD589K+iLR00kjzmhl/cSXmfKg6yHNvoDPsDp376qb8fGLKo7Q5my fUS4f88Q0h+P9VVCOuxFCSHur1EArfDaXXM0Yj+lmptLsMaVKVN+MX8cl5xhr6nl+ZDn UkjK7qm4UREJM9TdGO67LNj4mP7HU5vcqE9wdcaCmPO3wS1s48EGy1/CM9b9mvmvNPSN X8CQ== X-Forwarded-Encrypted: i=1; AJvYcCVR5Eeod5qb/A3f5cHhtO5rhgTl98Q6/PQGFJw1C3lKJuPD3bgy8q0K+Ehg4TV1y3MMTMxMKwA5vyF/9Vc=@vger.kernel.org X-Gm-Message-State: AOJu0YwCkK4butlUcQ9BZf2oXSCi5RD52SAkVYUTzdhB9qphR2mNzEEF 5IdfVAOw4gOuZDqa3KEb3HICX3tBmXTNyPkh9aPPYTZQ0J59B6RHWhfRVakZPP6mimqPV0Ogtmh fkccIDCQ8ZAjebw== X-Received: from pjpy19.prod.google.com ([2002:a17:90a:a413:b0:34c:9f0b:fd7]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3ccd:b0:352:bd7c:ddbd with SMTP id 98e67ed59e1d1-3543b38af32mr803010a91.23.1769721969001; Thu, 29 Jan 2026 13:26:09 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:06 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-20-dmatlack@google.com> Subject: [PATCH v2 19/22] vfio: selftests: Expose iommu_modes to tests From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expose the list of iommu_modes to enable tests that want to iterate through all possible iommu modes. Signed-off-by: David Matlack --- 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 5c9b9dc6d993..a03ff2281f11 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 58b7fb7430d4..add35dbc83f8 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 "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 "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.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 33D9C36827B for ; Thu, 29 Jan 2026 21:26:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721973; cv=none; b=nJCcmRsJYTUB84QM+ma5a74fwWC5j6fUfUG0hnvPprvWpY7ZfCDhqqc5zOduV4dN9AjxH3+eNBD4BHvXaXGULMUJ9fEFGyJDFLoRL838QhFQIyza/qe4NoiAyf3K2a3Xy9GgFyTWGjDf00a+vN9o4mzNAkStCWfqvfrGu6yQ4rE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721973; c=relaxed/simple; bh=7CV7PbEQub3WXnBX9axHLCIJCl0ibvYACGkaoVQCNpM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u9RKm2tObb4ch4u8Dxnb78qj1xw+ywgxX82zib+jlx31cLfwcZGGT9IfQD7spkOvsvqGfKIzEucn9QADXEAMjMkn2K5ukA0OPrjPN5SfpiIATA5sXKCo/W6rk22b3EKuFCDN1GElygISuQL+q/d5s/NuU081Yq8fm3uy9aS8La4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=a6bUtr4L; arc=none smtp.client-ip=209.85.216.74 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="a6bUtr4L" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34c5d203988so2580398a91.3 for ; Thu, 29 Jan 2026 13:26:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721971; x=1770326771; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jY83qjLDePi9A6Rw0FeAVcPg0HBWAQ4uS6Od5NUhNZI=; b=a6bUtr4LQLPVmTGTCdRqa2YItvKeOVNo3zDOTZUcwzqinJ5M3ErVlUlq66OvNKqmIU yajYRrvJSBhTwM3RGdpPKol4NzGeD48zOJocDjKOgEm6f3e4ivXZpJ9bD+2zVUWkOlHW JdfFsnmNgwOxB7urfWp8YB088+qzMdyBYIZ6xql7W/FydX+aH4G2tk2MUlC4dUGbJppW DEUG85PrHmaTDcqxrE7RetIiXnhC8Wf6prS0DUecOPKbWMNrSxHVVPtsPG6gL5j8/wUM zsfeLCIdegKU7JkL8N1YL3TrciFSDqdtPUXSmjVuMDcYE4UZ6ruUB4TZ47s7fCjlGLv+ vF0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721971; x=1770326771; h=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; bh=jY83qjLDePi9A6Rw0FeAVcPg0HBWAQ4uS6Od5NUhNZI=; b=lTeYGd7sPAtgeaOhNimoL44aHObcogLPK+HGcIfFHBQCpljsQ/gnxcBhW/hGeaqf3U ArocchTYPK8SznG9ZiixmMKbi88sir7A41y7YoHK3GbBcCuCOI2fHs6ReFmr1s2rab94 gwgfWPq0SqhjrQOYbITIYj15/f4V3FX+mXbyBssrlmwVdq9SIUKTvHNN4Qf2lvu3b/Wk kT229ZmodGCRyEn+XFBgMK+874qSzCfrrb8ks2PHRJ6CAJzL+5sgyVQOOm5+djDM9v6f kEWlecwggCfJUF+OB7bTQgNsvECn4J6Og2KcKhaJCu2MxKy7/Flk80ta69qnuO2ELRQp HJgQ== X-Forwarded-Encrypted: i=1; AJvYcCXlth3FOj+ApvcaaXwPEpTy/qOD0Io3+JrdZN/tsTOCmEoUau+gT7NM28fu5wKy14HwZDPuonnW1MZThEw=@vger.kernel.org X-Gm-Message-State: AOJu0YyvSnDlGPPGP/M3wpLADgNNTSo4zJ0wdLQTai9ugIQnR1BWBQVo rdLEvbhWp7+A8dR0PUzUeOBl2mWF4dzjhu31k8Lkw+dYu9+tMXm3ry99of/ygtpUC6k+fmpyWky M6GDv59Vwl4DkxA== X-Received: from pjvf5.prod.google.com ([2002:a17:90a:da85:b0:352:d19a:6739]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:510b:b0:340:a1a8:eb87 with SMTP id 98e67ed59e1d1-3543b3ba97dmr804461a91.35.1769721970642; Thu, 29 Jan 2026 13:26:10 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:07 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-21-dmatlack@google.com> Subject: [PATCH v2 20/22] vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expose a few low-level helper routings for setting up vfio_pci_device structs. These routines will be used in a subsequent commit to assert that VFIO_GROUP_GET_DEVICE_FD fails under certain conditions. Signed-off-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 5 +++ .../selftests/vfio/lib/vfio_pci_device.c | 33 +++++++++++++------ 2 files changed, 28 insertions(+), 10 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 896dfde88118..2389c7698335 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 @@ -125,4 +125,9 @@ static inline bool vfio_pci_device_match(struct vfio_pc= i_device *device, =20 const char *vfio_pci_get_cdev_path(const char *bdf); =20 +/* Low-level routines for setting up a struct vfio_pci_device */ +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu); +void vfio_pci_group_setup(struct vfio_pci_device *device); +void vfio_pci_iommu_setup(struct vfio_pci_device *device); + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 08bb582eaa8f..76597c84e956 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -198,7 +198,7 @@ static unsigned int vfio_pci_get_group_from_dev(const c= har *bdf) return group; } =20 -static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) +void vfio_pci_group_setup(struct vfio_pci_device *device) { struct vfio_group_status group_status =3D { .argsz =3D sizeof(group_status), @@ -206,7 +206,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf char group_path[32]; int group; =20 - group =3D vfio_pci_get_group_from_dev(bdf); + group =3D vfio_pci_get_group_from_dev(device->bdf); snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); =20 device->group_fd =3D open(group_path, O_RDWR); @@ -218,14 +218,12 @@ static void vfio_pci_group_setup(struct vfio_pci_devi= ce *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 -static void vfio_pci_container_setup(struct vfio_pci_device *device, const= char *bdf) +void vfio_pci_iommu_setup(struct vfio_pci_device *device) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; int ret; =20 - vfio_pci_group_setup(device, bdf); - ret =3D ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type); VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); =20 @@ -235,8 +233,14 @@ static void vfio_pci_container_setup(struct vfio_pci_d= evice *device, const char * because the IOMMU type is already set. */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); +} =20 - device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); +static void vfio_pci_container_setup(struct vfio_pci_device *device) +{ + vfio_pci_group_setup(device); + vfio_pci_iommu_setup(device); + + device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, device->= bdf); VFIO_ASSERT_GE(device->fd, 0); } =20 @@ -336,9 +340,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_devi= ce *device, vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 -struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, - struct iommu *iommu, - int device_fd) +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu) { struct vfio_pci_device *device; =20 @@ -349,9 +351,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const c= har *bdf, device->iommu =3D iommu; device->bdf =3D bdf; =20 + return device; +} + +struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, + struct iommu *iommu, + int device_fd) +{ + struct vfio_pci_device *device; + + device =3D vfio_pci_device_alloc(bdf, iommu); + if (iommu->mode->container_path) { VFIO_ASSERT_EQ(device_fd, -1); - vfio_pci_container_setup(device, bdf); + vfio_pci_container_setup(device); } else { vfio_pci_iommufd_setup(device, bdf, device_fd); } --=20 2.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 F325A2F5A28 for ; Thu, 29 Jan 2026 21:26:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721974; cv=none; b=uWP8gB9HnYugIv5VYMEQxrRkNwrco9PBmE0YugCP/TB0QClz71k8IADmDPMcIROuvxKH1Lyv2VOgV82EkDiY+4rNguhPUfpUUbUTBNbE8XZ0OPvJ6Rkh4xeTuXEmFljxy3y8xYP2P2sEYf2NTuhuhcJw7k4xbFQPp2/dURwFQ1s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721974; c=relaxed/simple; bh=21+Dgo5HtQc5jZ/S02DFtrwF10l6Ks7onfvVXCrr6ps=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DCYb4lJaZoR5Bg3TSfCCf12uuTwT17fB5oI/gFaVxEr6KUNhF9TSrzmtZ1j7mqkfSP0cbr7ZZQcAkWBPuinXEidscyAjMP1Uuly9ODcoESXZJgWfgJ671d/MMpsbgWzSRadJS+C1Gqf9MDWiNSKyAD16IagFIyXKkXJldsLf88Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GQDE3hyd; arc=none smtp.client-ip=209.85.216.74 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GQDE3hyd" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34e5a9de94bso2990003a91.0 for ; Thu, 29 Jan 2026 13:26:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721972; x=1770326772; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=uE4aM7+RnwFNy7PeqMvvBM9KOovOI9GoqBkQFV8UcGE=; b=GQDE3hydIMvo1ssbOIUQLKON6i040qShJriKTVXg6+97RyIxfNS8rGxbGf8YKrUqhX gcZsi0XXzp27Y7nHPPX0M8N1D0G8/Yeg5cPuywVWGn4a2YYIUHO+psnoluMpFvjmoLiD 0Tpdo2HYZM0OkoamJuSKL7XwRBkKsBW/Mt8ro4jRQJukT+NWpJNks8n4j0GSTfJksbwn qJbt6+blTo/ErbM8x+f2Ptoo1a4E/jZj+BAji4AkUqNnMX+ZuruHHEnjmffV4BDjnCxc EKFxJqJiy5cELpJy7Z5uuEg26vvN58CFcMgpf6icwSwwcUQlyfMdOcxh2Em4rMuSkk2t Fp0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721972; x=1770326772; h=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; bh=uE4aM7+RnwFNy7PeqMvvBM9KOovOI9GoqBkQFV8UcGE=; b=PLKU0d07FMD2Fc+CaSqA0zKV0S8DC134iISGhjae1zNUYoCrbuhPfYoFr4/psjnZd4 //Bkf2LIJQ/61lnlEUAuyDIHb3AyC1NpmQna7rkSDn2+wqdkOXqT2qhfRwgV0LXd1HjJ RdvQAyBmqQIsRfSvMNEdNd0Az8YjsBvL7FheoWKAlEt8tShh9M/ZFBzHRfbopWDVvthZ dDsv+d41VnSUIudk98bxN6zr7hscrwX1ZDEvLCnMO+mvM22zpgeav2doUtFGPN7rOW0H urF0KVsUSfXFqo9mPaWO5Bvx6mAT8rjXml9GLdoAi9+KA6TmU5WOEODbXz2+b5oDhg5u rIZQ== X-Forwarded-Encrypted: i=1; AJvYcCWU+D477HH4xOtwpwV/BnoNbEa0oxrnE1lqSsMDqMPseBCycc1cWyXKzyNi+oF9vNlODrIEZKM5ce8yZoc=@vger.kernel.org X-Gm-Message-State: AOJu0YwbreSNnCgQWupgbl630LPWusRUJGJKZ+QdaQ8Y/kiqC1QVYK6X OPGv/FKmqDkB808bF/iUAQKzp8UKqC7/cjhKErw44EpB8YCIjZmJZJPxdXG3VJKehK8Es2iyxT7 VdjKGMJTsESLgXg== X-Received: from pjbok5.prod.google.com ([2002:a17:90b:1d45:b0:34e:795d:fe31]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:388c:b0:34a:8c77:d386 with SMTP id 98e67ed59e1d1-3543b2fbdccmr728738a91.9.1769721972187; Thu, 29 Jan 2026 13:26:12 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:08 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-22-dmatlack@google.com> Subject: [PATCH v2 21/22] vfio: selftests: Verify that opening VFIO device fails during Live Update From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 preserve across a Live Update, the file must be retrieved from /dev/liveupdate, not from VFIO directly. Signed-off-by: David Matlack --- .../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..65c48196e44e 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); + vfio_pci_iommu_setup(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.53.0.rc1.225.gd81095ad13-goog From nobody Sat Feb 7 12:19:28 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 93B2936682F for ; Thu, 29 Jan 2026 21:26:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721976; cv=none; b=ikrlISJKF64IXatToMQ9p6KPfm2oAT0LCZuMjYF9mYK/UldiD75YBwUM4wZS3zJ0xGStefGEl3MLCh6dy04vnLX6LfTuMTmv5lCgnxd1vvxr7c8wLKpoOZoW4OiuXQGUzcWH60HVuEyzSPv1kSXguRwMgYdNy0sVwVxgYFyOp+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769721976; c=relaxed/simple; bh=4vdFi/BbAfJQTKROKUar1N5nU267nS3mfoR9iF/kNJs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CEiCWP870DlSt5esI0lSGBvpEV71LJk8SJBBJ8nHphQnADAqfinE6rvK/NjHJEQujlNYeW4oE11Ir+Yv1YmrVOy+HiCWDBqwETbL4dgJ2Vxbf1ioBLARvj4E7nhBxh1c+9uCW2kt3ewNstYBNXAi+r6ruYe+XYFejd3bYa5beVc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wJuGZjYU; arc=none smtp.client-ip=209.85.216.74 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wJuGZjYU" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34ec823527eso2332137a91.2 for ; Thu, 29 Jan 2026 13:26:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769721974; x=1770326774; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=J/muCk34u9aT1IY2gVvKiXcNAAUfYmiU08gi1alRQns=; b=wJuGZjYUDpntIpqNm67JSx7AXQVZvedOBAi30DCcDwz3H6VxkXHMpLoyIsw9a1l/Fj v/Jtj7S2xqL0ILKsYytxRfmRO32KIBlzBaOsjKEuDV+BzfmvHMCgq42TAd0ij3aSRW8A UR4l+mkOjW+xqw6H7XM2zrPs/fRO6rtpzs8bTEHsBHYlAwD8UKvK3tDPVXHEgVQRL9Wa Fg/xts26AMgJxMBOXpTaP+/Hq4j5Dkm9uUm6LkRIoTUqKr3i9ye5Qf0w2IqE2eIBZIFs 0D4h05TsLATZJOEYOmZ7vVtqIygO7xWgGJA76OzZk/8+bK9orV8F8cobQSeRX3FU3SdI cQnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769721974; x=1770326774; h=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; bh=J/muCk34u9aT1IY2gVvKiXcNAAUfYmiU08gi1alRQns=; b=G3P3ksD9j9/XUSijBCIFgkgo9Y7SqUBxE/vuOe2lj+w6zGr6tgNPPS/zBoiV8tTqtT aQu6SgocPOpzYnev80033gMbLDWP7GO4fcwrN0yxJWRVRbQ8coz/oE57ST7lxpAnLbrR kEe7qjc/hc44jXeBt2D51kxk9MUdcPwylTKWvlhSCMXRVAL+nOdJDiA01rkuvZ1eqZY6 CSImiOdERgvPlaHdt0BLSI0WifRRbo2GlCUQxbZMerKPUYe1mUW9fC+T/jQkoMUKen5S aioaJZJngS3GmhY53+8wL1Ls7WdrM2EfDjTLK/K5C1Hlcy9vw9XKFoDjAX9Ir8eDjcFJ Ky5A== X-Forwarded-Encrypted: i=1; AJvYcCWWPvzDZ40Qb2+pIs7+y8y5iZgMaXKmjgG+3bYgyxqQvTOrjN+h8xPSrY3Erl280lc+ofgbvbzxkEaTRtk=@vger.kernel.org X-Gm-Message-State: AOJu0YwLp56RqyyIjUrH30HgZ/RQPYpnQBVi1A124b5+bTcHjcvXtYxp yOMla0xtNtRAnWvWcgaGzYPcrgnUl0uYPVsTb4+cNeH+cC2RenLyy8xLp1fQYJpUM9/rqs9vQW4 Z9WBpgFYq6SgawQ== X-Received: from pjbso3.prod.google.com ([2002:a17:90b:1f83:b0:33b:c211:1fa9]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d60d:b0:32e:38b0:15f4 with SMTP id 98e67ed59e1d1-3543b2dc1c9mr828054a91.7.1769721973868; Thu, 29 Jan 2026 13:26:13 -0800 (PST) Date: Thu, 29 Jan 2026 21:25:09 +0000 In-Reply-To: <20260129212510.967611-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260129212510.967611-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: <20260129212510.967611-23-dmatlack@google.com> Subject: [PATCH v2 22/22] vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Alistair Popple , Andrew Morton , Ankit Agrawal , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Lukas Wunner , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , "=?UTF-8?q?Thomas=20Hellstr=C3=B6m?=" , Tomita Moeko , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 during 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 --- .../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 65c48196e44e..36bddfbb88ed 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_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.53.0.rc1.225.gd81095ad13-goog