From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 A044337C931 for ; Wed, 25 Mar 2026 20:55:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774472114; cv=none; b=oOvKgMnHe0Q3OB2a5WIu0+8QNApm51aHreoUngD+4l3gALAg4VLN4IfU9KesKBxO90I04ZcYwa755S6VOZ18eYaeQ5iTJko1dRpO6b3ZGhiMRKSoQHC1H2rKCQMPtHAovr3viiZN4qiBAkGwCCfwx6mE4erkI+BNYWdZEx8VUBQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774472114; c=relaxed/simple; bh=+jxPirHWm4GxVwEslObRk9vkViIlq49DC7znSdbetR0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=hv9xZpLSI9eocH6x6/CZDhEqSvWAbOdyGqaZtQaUE08p22hzbWNvW7tohk5+AsQsdllS2f50VhhKfAY+iftos6QDeWE77u62OScNVu/7yPN/5Tt1g1UJkdUprl3PKod+yCxLP2w/CXqDUp7d7rtHy/PZ27wx340VpU+dZFIjQsc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LuSa1z7V; arc=none smtp.client-ip=209.85.214.173 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=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LuSa1z7V" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2b04c9e3eb7so27865ad.0 for ; Wed, 25 Mar 2026 13:55:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774472111; x=1775076911; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=jhMIGpIQFcuKvxAcvdtwqtuFZewnUju9lpglG9yLN80=; b=LuSa1z7VTCnAH2IFy41qkzK7393d9kXGJkKPDl+pkhq7t14OA2L9dt/ZwHEpwt1L71 Cw4CubTrtJkmYFxq0vyfBuHj307VIiXVQpTCNGBMIZNnXn/JTOrBUCsP5TTEpVKjDcP/ zBCz1+wysqbfEAH2KyS7/AL5x5XyofoONtZGvAqQ8Kt56WxPa5jYCv26NT1qASr2VxoR +ecnqGhCjJOjqFfzDusewpUKZqA5F7+EluVsqE76YhNerClv/pvh2JuhVHxR+AydHYY9 exxnKQYpIlgSdm5qrM4Sd1ykJLRxZDkKJGSixMvPcyJszy0vad4QM7xsPC8/5UhGBLyo Dz4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774472111; x=1775076911; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jhMIGpIQFcuKvxAcvdtwqtuFZewnUju9lpglG9yLN80=; b=r+sx5ngslmYy8gK7/BBXk6MmPalJ+W85cFen5qgJhhzf2wpFJiN4q7vi1YW+gVLtu2 7vNXQ5sfIXwZ+5FvrEKlOO4u+zp//naujDdX8NGqJowKC0t9nhAsg8dPqbXlsnDY31eB X+XM1qaEdoKdpguRvoJGS0uG57yP2afRBcHU9qC2g6JAeJE2nqefp9qlpGYsRImeigVw 3WkXspslQdHwamF+FiQg/8i90mlZa0GDWjmSJ3o35UhCrp9AUManXQqcjWFgQdMthICc Q3r7eY6qCosHBioaWKbriRWjA28X4WFqfYZql7mTi2XVG9IvHt9gaTwVycPlGKVh5XNe gRQQ== X-Forwarded-Encrypted: i=1; AJvYcCXJHlv9XWgMEcFpXXGncMrJ3lzl1JSApQwYeko8KqfdTHNuLvd4OUDGBQz8/tTp9/+Da/QQlea9Qqx/14s=@vger.kernel.org X-Gm-Message-State: AOJu0Yxp5GGbrL6IjlFvmLjjX1wIjSYEpaJeVqW6U7n/mYqFTFCf5dVy SBTx/CL8E7M/rdNw6fSzvaELfWL+pcDx1gnK/DmHjQsc/3jyDX6gXDnExT99xlHyYT6bESHWlvE ZLSiUcDpu X-Gm-Gg: ATEYQzzgHNgRRkGOlZpnPDwZkW0vsB7tmzaiSN/igYp/l+ohsxzSRqgmo2NHTHllt3U 3NVyab3nFvme/OK89AkLa5AOlrWYWR+XGLfpfLeIM5FHAR3auCEH7G42XH9LE0gPqO50qARLF6J Uh5bcumgH+XGux2UHHKuWH3u7oao0QI1tOZIjnJ9AB6DhJwFP5M+BH4tJpgqNbxAYxr6lA9qdas quO77K2MJI/jTcxZhAUlLu6OQqajCFsYfaFHE26NwhgC2CrH/zjyiNYmtzPJeWYwOgqrC6khrBG W3sVKGpuxGANP/6mJ5eW+JPmTOCo/wcSDhmS6xMXd8LDz5lnRGQTAnlPrER5l54UhYubHxwT7fK SbL1ruoZS8VbBkvFitP8aMjENWmsh+wXB26+tVEkwNM0lOMS3v4H+/kdbnGtHG1Bh/ZOuaBnJn5 nrUu4Z9bp/aHOMHHr0e3ztXewJhvTKcV9vj3KO+qMy2MZwT6SauSlXo3PN+A== X-Received: by 2002:a17:903:22d2:b0:2b0:5683:1cf with SMTP id d9443c01a7336-2b0bfa3715cmr503835ad.13.1774472110165; Wed, 25 Mar 2026 13:55:10 -0700 (PDT) Received: from google.com (10.129.124.34.bc.googleusercontent.com. [34.124.129.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b0bc917b5fsm7349805ad.84.2026.03.25.13.55.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Mar 2026 13:55:09 -0700 (PDT) Date: Wed, 25 Mar 2026 20:55:00 +0000 From: Pranjal Shrivastava To: Samiullah Khawaja Cc: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Jason Gunthorpe , Robin Murphy , Kevin Tian , Alex Williamson , Shuah Khan , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Pratyush Yadav , Pasha Tatashin , David Matlack , Andrew Morton , Chris Li , Vipin Sharma , YiFei Zhu Subject: Re: [PATCH 13/14] vfio/pci: Preserve the iommufd state of the vfio cdev Message-ID: References: <20260203220948.2176157-1-skhawaja@google.com> <20260203220948.2176157-14-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260203220948.2176157-14-skhawaja@google.com> On Tue, Feb 03, 2026 at 10:09:47PM +0000, Samiullah Khawaja wrote: > If the vfio cdev is attached to an iommufd, preserve the state of the > attached iommufd also. Basically preserve the iommu state of the device > and also the attached domain. The token returned by the preservation API > will be used to restore/rebind to the iommufd state after liveupdate. > > Signed-off-by: Samiullah Khawaja > --- > drivers/vfio/pci/vfio_pci_liveupdate.c | 28 +++++++++++++++++++++++++- > include/linux/kho/abi/vfio_pci.h | 10 +++++++++ > 2 files changed, 37 insertions(+), 1 deletion(-) > > diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio_pci_liveupdate.c > index c52d6bdb455f..af6fbfb7a65c 100644 > --- a/drivers/vfio/pci/vfio_pci_liveupdate.c > +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c > @@ -15,6 +15,7 @@ > #include > #include > #include > +#include > > #include "vfio_pci_priv.h" > > @@ -39,6 +40,7 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) > struct vfio_pci_core_device_ser *ser; > struct vfio_pci_core_device *vdev; > struct pci_dev *pdev; > + u64 token = 0; > > vdev = container_of(device, struct vfio_pci_core_device, vdev); > pdev = vdev->pdev; > @@ -49,15 +51,32 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) > if (vfio_pci_is_intel_display(pdev)) > return -EINVAL; > > +#if CONFIG_IOMMU_LIVEUPDATE Did we mean to use #ifdef or #if > + /* If iommufd is attached, preserve the underlying domain */ > + if (device->iommufd_attached) { > + int err = iommufd_device_preserve(args->session, > + device->iommufd_device, > + &token); > + if (err < 0) > + return err; > + } > +#endif > + > ser = kho_alloc_preserve(sizeof(*ser)); > - if (IS_ERR(ser)) > + if (IS_ERR(ser)) { > + if (device->iommufd_attached) > + iommufd_device_unpreserve(args->session, > + device->iommufd_device, token); Few minor things here, we've protected the preserve call above with a #ifdef but not the unpreserve here in the clean up path. Also, it looks like in the previous patch we wrap both of these functions in #ifdefs and define their #else static inline-d versions too? > + > return PTR_ERR(ser); > + } > > pci_liveupdate_outgoing_preserve(pdev); > > ser->bdf = pci_dev_id(pdev); > ser->domain = pci_domain_nr(pdev->bus); > ser->reset_works = vdev->reset_works; > + ser->iommufd_ser.token = token; > > args->serialized_data = virt_to_phys(ser); > return 0; > @@ -66,6 +85,13 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) > static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args) > { > struct vfio_device *device = vfio_device_from_file(args->file); > + struct vfio_pci_core_device_ser *ser; > + > + ser = phys_to_virt(args->serialized_data); > + if (device->iommufd_attached) > + iommufd_device_unpreserve(args->session, > + device->iommufd_device, > + ser->iommufd_ser.token); > > pci_liveupdate_outgoing_unpreserve(to_pci_dev(device->dev)); > kho_unpreserve_free(phys_to_virt(args->serialized_data)); > diff --git a/include/linux/kho/abi/vfio_pci.h b/include/linux/kho/abi/vfio_pci.h > index 6c3d3c6dfc09..d01bd58711c2 100644 > --- a/include/linux/kho/abi/vfio_pci.h > +++ b/include/linux/kho/abi/vfio_pci.h > @@ -28,6 +28,15 @@ > > #define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1" > > +/** > + * struct vfio_iommufd_ser - Serialized state of the attached iommufd. > + * > + * @token: The token of the bound iommufd state. > + */ > +struct vfio_iommufd_ser { > + u32 token; > +} __packed; > + > /** > * struct vfio_pci_core_device_ser - Serialized state of a single VFIO PCI > * device. > @@ -40,6 +49,7 @@ struct vfio_pci_core_device_ser { > u16 bdf; > u16 domain; > u8 reset_works; > + struct vfio_iommufd_ser iommufd_ser; > } __packed; > Another struct alignment geekery: when we update vfio_iommufd_ser.token to __u64 (as pointed out by other comments), putting it right after `reset_works` forces an unaligned 8-byte access at offset 5. Ideally, we should add explicit padding (e.g. __u8 padding[3]) before iommufd_ser to ensure natural alignment. Because __packed prevents the compiler from automatically aligning fields, placing a 64-bit integer at an odd offset forces the CPU to perform an unaligned memory read. While some archs handle this with a performance penalty, other archs could outright fault on unaligned 64-bit accesses. Explicit padding guarantees safe, portable access across all archs while maintaining the strict memory layout required by the KHO ABI. Thanks, Praan