From nobody Mon Dec 1 23:06:45 2025 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 7B30E340277 for ; Wed, 26 Nov 2025 19:36:24 +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=1764185786; cv=none; b=UKn6te0Wt5/R6K8TKpmjVjV6/3HmByydeOEWXnQvvPPDo577B3sXtxx+aIe6jA9bTKMfNvT+EtfmqlZsbJF619dFWJyOyCfxsrMZvxOI4jE8wI7Ucq9NT4IVCK/b+HlzyPdNk9goWLB3hCOK4bjA4CUh3INlLSK3pFXFxzzshMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764185786; c=relaxed/simple; bh=mCOKJ+2oazOul2zSOoTvvQYPlws6VrgIzEWD2SHP4h0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tGx5lZJsLJvzazhxkTo74hlHn6sZrF1FDPU400MKNJjfEg4DwEwz5EV57TyAyIAs4mP1JltGcIBQq5wQK4a0BzsmjIbprr1+kRtJK2lZ9Gm++Nq8sfgyWXqrknWZbHPT2razud/NckpauLX9PyWU1OldUa9GbuKbDnUxlyKCxEU= 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=leAra91s; 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="leAra91s" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34374bfbcccso78830a91.0 for ; Wed, 26 Nov 2025 11:36:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764185784; x=1764790584; 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=34dEI7XqQFY3PUssu08ewRf+hu3ICrVVHhgvFs2tITk=; b=leAra91seKzVgaVuc1yowBpyV16MZVEQ/3jryUfGEdiEb4Q3Ko2JxP/oPV93go6eOV rGzf0xj00ibva5m04HYWQMNtHdlfUI0xHJjaVKm7XiyOwnvsZJUgUAggVq3l6rkJOw1g 2zApUa/IIbAXWmSO9ccvS4EjN+D7sAxQDfq0pnyCf7kTfwzqdf2lrqu8DtPNujROkOve ARdeSCLlfpDROJmZ1Jw8Dcfk/xWjHx4es34lalz3/aGgqBWKLZC7F8D3x38RRxI2Yyho 4UPf5jeZHgg5JGUuf9j6ZGX/ZK40tbuih14vHz+uBcmy9R2nmAbajhlomhXH3gyfSlWJ LBiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764185784; x=1764790584; 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=34dEI7XqQFY3PUssu08ewRf+hu3ICrVVHhgvFs2tITk=; b=ZhHpMMdJ1a25vDQoY1SRIDP/myDK0B4Fr2orgGvLuV6uSGCTRQ7O+uWYbCY/a0ijEL b5eNf3MxPf0qLCcjD9lRzxBEqE11MqEZki1HFtRA6WbRrNGU2m6NZyntbHmz8d2+zpc4 l8aRLkcAkXYOO0PD2ZNx4ZXxNI/yuQhI7Q1OQ3ZJDXrDWXIc6J51HH+xz0P9Ov9/OE5t hkYMemYbANmhMruOVPUI8lTmvPV1s1Xk8OVg+vfIJIXTnOt1cVWZLojPTdk5WFmPuEW+ //Z8doExhaIeLp4ccEZf5vg3iR8WBiJZcjT7r6B0lyOe0ok0zbHOTIdi6nr7XSy0+LqL yHSg== X-Forwarded-Encrypted: i=1; AJvYcCWx0nHAJJGS+s4ri3Hd4qppSrXh4HV87zvX0+MDbtqe10kUCI3NnFWxZ42hkRTEPtxCxx81QDDHVGZp04s=@vger.kernel.org X-Gm-Message-State: AOJu0Yz11Boy7/1VB2ol7lUGviHF89LZ862rKIf8K6NEy8etpIZh0XYT vAZSNUD3mgAwNthrHNMBxXS0P+BXZZf+Q0EzSSM1lvOG2tIAdjR0qbKa6H1wS9fk7NHeQK/v20O JgBGrZkTOWBcOBQ== X-Google-Smtp-Source: AGHT+IEqIu3OlacY0Bm+LqAlpJoB8yetidaH8WntE+ttbQ3ZI60SDomuw3uasEM3kvlaRlv8+XaIAZZGkGEx9w== X-Received: from pfbfb24.prod.google.com ([2002:a05:6a00:2d98:b0:781:e8d:dd2b]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6d96:b0:35e:6c3:c8d0 with SMTP id adf61e73a8af0-3637e0c5c31mr8915052637.54.1764185783558; Wed, 26 Nov 2025 11:36:23 -0800 (PST) Date: Wed, 26 Nov 2025 19:35:55 +0000 In-Reply-To: <20251126193608.2678510-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: <20251126193608.2678510-1-dmatlack@google.com> X-Mailer: git-send-email 2.52.0.487.g5c8c507ade-goog Message-ID: <20251126193608.2678510-9-dmatlack@google.com> Subject: [PATCH 08/21] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alex Mastro , Alistair Popple , Andrew Morton , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Josh Hilke , Kevin Tian , kvm@vger.kernel.org, Leon Romanovsky , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-pci@vger.kernel.org, Lukas Wunner , Mike Rapoport , Parav Pandit , Pasha Tatashin , Philipp Stanner , Pratyush Yadav , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , Tomita Moeko , Vipin Sharma , William Tu , Yi Liu , Yunxiang Li , 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 | 11 +++++++++++ 3 files changed, 24 insertions(+) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index 0a6e972f322b..f1e54dcc04e7 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 c376a6279de0..93dbbf37ec4c 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -313,6 +313,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)) { + ret =3D -EBUSY; + goto err_put_device; + } + fdno =3D get_unused_fd_flags(O_CLOEXEC); if (fdno < 0) { ret =3D fdno; diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 4e400a7219ea..040c2b2ee074 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; @@ -425,4 +426,14 @@ 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 +static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_devic= e *device) +{ + struct device *d =3D device->dev; + + if (dev_is_pci(d)) + return pci_liveupdate_incoming_is_preserved(to_pci_dev(d)); + + return false; +} + #endif /* VFIO_H */ --=20 2.52.0.487.g5c8c507ade-goog