From nobody Mon Feb 9 22:18:57 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4363033065B for ; Thu, 5 Feb 2026 21:00:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770325243; cv=none; b=adzFnuhU9h3CY3rnXHQ/JiGCujCMwU87CSQkhl1wMjJeUnwg8/q4mMskz6+u7DymcUIlKNcm99ns1pip/mlBHNK/8cXUMibvQXBZji+wS/Gyo/Vui9mt1tathDbE3djhwFlQcF5n7bIMj3hbwwMYqKZmJl7tukZWKi/ORXdfxek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770325243; c=relaxed/simple; bh=KLbaRgHVZXPc1lQS39X/po8u/6fvm7lynlb8kx1LgCw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nH/UxE17fI/FYtOsHAgxLBIJg6gf6wYIyPk7HvwMJFVM57bH0uqOImGJi2GAWbM81STb+k7VFqyWfEcxD5aHWO+lB5q9/K1RcjQ181FNbUfWOuTh/cuzHvEBs39CVRfkcYGs8NGJqnIvPDR3qIidzChBDyick4+c89dRL0f0lLY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FoAjm2F0; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=M/IJ5Nn2; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FoAjm2F0"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="M/IJ5Nn2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770325242; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=swhoxBv4zwo4dFRpNRtMjzIqauuYaXTIl/FyLukXQWY=; b=FoAjm2F0+C0lIDsMprrIgJ8Jwa8OkDVMqK0lixuZ+YVQeY0fqll3qYfI6Hk5ebcu3rnBsF NFLpw5V4lpvPfuyE4d0qBhv4K2f3aISa/9jPrHf5TEa6407DKGuTbMG+G1zG9JTGRdDJM4 HThLzERRi9FNIsfArESOH+OgH4LN/BU= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-318-EJkHhi5ANg2iX9v0ukDuTA-1; Thu, 05 Feb 2026 16:00:41 -0500 X-MC-Unique: EJkHhi5ANg2iX9v0ukDuTA-1 X-Mimecast-MFC-AGG-ID: EJkHhi5ANg2iX9v0ukDuTA_1770325240 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-8c538971a16so350705185a.1 for ; Thu, 05 Feb 2026 13:00:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1770325240; x=1770930040; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=swhoxBv4zwo4dFRpNRtMjzIqauuYaXTIl/FyLukXQWY=; b=M/IJ5Nn2e1zalLsMQcV9xn3IR5HsYWg+UoIwOxvJw3JOyFy3zy58Fgpz3X9QVp5T4e v8MBIZbu2lZO/4xgsxmBakehdR+/fq/ciGNRZC17eebxAwU03NF5hoFqJWF/V8ZZU7TO MkGnSAkS+dytExI1EnUzeX7yI8rGPu67zK12ZeGsW7eLMhU8uEvgFu1jaqNlPuAVVPvN KOtZl37pFa2ox0zYI9N3CFOG2Sd323BokctBP/4wiIZ8swqYjRbCFHZ6ydK2btG24clA X2ubOs89UvivxzAPnAPiQxI31xgOS8UwpX/UlWgNZEE+86ga5VPfOJMp1709UWe471Hs +0Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770325240; x=1770930040; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=swhoxBv4zwo4dFRpNRtMjzIqauuYaXTIl/FyLukXQWY=; b=Z/sMf3QxyVQJUnb8edYgs+ODk9nZMxvkam89M52uhNfcZW3mBk4U4CpIqdxczgx2OG nLvUpVkInsO4fS4UV2KR9/F9HSyjSxDyBN0+Ujig0LmnhHyP+kCYavh7nANM7Ixff2To tFEF5Q9k7yu9eNCedgE+EnrRGu9XsBylPgKWwz+eP06l4EsXGnmZqY88nNIOgMIr83Wz oNXNR2kzw6pLj8FHxCv+F5HMN8OV80g+FGGPUmf/qxRgznu08rLnzcw2fyXL0VHaixji lMrn2Os2C4xdtgHut5yN17UWUQGBwIgUFHap6ZhG18PInmOITeDYq4rdPPnoIqYdH+JI U6pQ== X-Forwarded-Encrypted: i=1; AJvYcCX07g+wBlcqT6c9hQXIbugDt2OYaxXfDIy+52Qh01D/kjIO7rYmrUf8iQnsxG/nxucB3cbB1iwEo7H7WNU=@vger.kernel.org X-Gm-Message-State: AOJu0YzzSkYEnlGJxXiMkyPgnVoiCU8sWNUVNC4QMNaBunUW5/DUicl0 HCfwVG6j878tCOKZIhQ16F39Ask4kVmuNcj0eERyxwkqB87Ox6ozzH5BTZrAaDFaTwqrb8H0ZVO 7fMRs0AER7ub9UYp6waw8D6afNZj9otI70FjkCpqYKX+uZ6FjCXGHbCQZa4StJcASnYVastXCwA == X-Gm-Gg: AZuq6aIZampO2jE8yb96B1o5ZzZdTeftDzRZDgPJNr23Ak1i3nmVSfu8hdR1iCEDPJ0 oCXKSMuusBakwn178nkVaMqTmDSiRVb6sRj85pzf9BPNn3QQTjvbFrWHrFUdNeQF2o1Y+oDP2aF 80Myl5YnDWBDYK015TyF87pd93eqn9P1c5/fYuWbGvoMAw76/P1LLHTRzv8VxZ/DlDnZXTCGkhu lZ0fS3kKPRU+3Z++pDLoCele8MqStRSG4Yttfe5biTL0tOvyntEBChHVGCI2mM4NNsqYbe4CNNY VrqydYdhFfn2+dTiO4oBkydge49t0ZCj0OjDCYGuj8a98u2WTokWEfdvGfjT9RDi1Oi795MFA7z X+nYWD5Hx5KkNYc0= X-Received: by 2002:a05:620a:3953:b0:8b2:eae0:bbf4 with SMTP id af79cd13be357-8caf0487af1mr60625785a.19.1770325239526; Thu, 05 Feb 2026 13:00:39 -0800 (PST) X-Received: by 2002:a05:620a:3953:b0:8b2:eae0:bbf4 with SMTP id af79cd13be357-8caf0487af1mr60617885a.19.1770325238937; Thu, 05 Feb 2026 13:00:38 -0800 (PST) Received: from [172.16.1.8] ([2607:f2c0:b010:9000:4c85:f148:4c91:943a]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8caf9fdf692sm17571285a.44.2026.02.05.13.00.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Feb 2026 13:00:38 -0800 (PST) From: Peter Colberg Date: Thu, 05 Feb 2026 15:59:56 -0500 Subject: [PATCH v2 09/10] rust: pci: add physfn(), to return PF device for VF device 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="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260205-rust-pci-sriov-v2-9-ef9400c7767b@redhat.com> References: <20260205-rust-pci-sriov-v2-0-ef9400c7767b@redhat.com> In-Reply-To: <20260205-rust-pci-sriov-v2-0-ef9400c7767b@redhat.com> To: Danilo Krummrich , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Abdiel Janulgue , Daniel Almeida , Robin Murphy , Greg Kroah-Hartman , Dave Ertman , Ira Weiny , Leon Romanovsky , David Airlie , Simona Vetter , Jonathan Corbet , Xu Yilun , Tom Rix , Moritz Fischer , "Rafael J. Wysocki" Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot , Alistair Popple , Joel Fernandes , John Hubbard , Zhi Wang , nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-fpga@vger.kernel.org, driver-core@lists.linux.dev, Peter Colberg , Jason Gunthorpe X-Mailer: b4 0.14.2 Add a method to return the Physical Function (PF) device for a Virtual Function (VF) device in the bound device context. Unlike for a PCI driver written in C, guarantee that when a VF device is bound to a driver, the underlying PF device is bound to a driver, too, by always setting the flag managed_sriov in the pci_driver structure. In case SR-IOV has been enabled by a C driver that has not set the flag managed_sriov in pci_driver, return an error from physfn(). This change depends on commit a995fe1a3aa7 ("rust: driver: drop device private data post unbind") to also uphold the safety guarantee in case a (broken) PF driver re-enables SR-IOV in its unbind() callback. That commit extends the lifetime of the device private data beyond the remove_callback() wrapper. In particular, that commit ensures that the device private data for the PF device is still alive until after the function pci_iov_remove() is called and forcibly re-disables SR_IOV, which means the data can be safely accessed by VF drivers until then. Suggested-by: Danilo Krummrich Signed-off-by: Peter Colberg --- Changes in v2: - Uphold safety guarantee when PF driver is written in C. - Let physfn() return error if driver flag managed_sriov is unset. --- rust/kernel/pci.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++= +++ 1 file changed, 53 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 581930d0afe98ccc29d729e4d9aab75b4144e46c..3b11f73a9f2b69a02fe003b8fea= dd61864adc8c0 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -525,6 +525,59 @@ pub fn pci_class(&self) -> Class { } } =20 +impl Device { + /// Returns the Physical Function (PF) device for a Virtual Function (= VF) device. + /// + /// # Examples + /// + /// The following example illustrates how to obtain the private driver= data of the PF device, + /// where `vf_pdev` is the VF device of reference type `&Device`= or `&Device`. + /// + /// ``` + /// # use kernel::{device::Core, pci}; + /// /// A PCI driver that binds to both the PF and its VF devices. + /// struct MyDriver; + /// + /// impl MyDriver { + /// fn connect(vf_pdev: &pci::Device) -> Result { + /// let pf_pdev =3D vf_pdev.physfn()?; + /// let pf_drvdata =3D pf_pdev.as_ref().drvdata::()?; + /// Ok(()) + /// } + /// } + /// ``` + #[cfg(CONFIG_PCI_IOV)] + pub fn physfn(&self) -> Result<&Device> { + if !self.is_virtfn() { + return Err(EINVAL); + } + + // SAFETY: `self.as_raw()` returns a valid pointer to a `struct pc= i_dev`. + // `physfn` is a valid pointer to a `struct pci_dev` since `is_vir= tfn()` is `true`. + let pf_dev =3D unsafe { (*self.as_raw()).__bindgen_anon_1.physfn }; + + // SAFETY: `pf_dev` is a valid pointer to a `struct pci_dev`. + // `driver` is either NULL or a valid pointer to a `struct pci_dri= ver`. + let pf_drv =3D unsafe { (*pf_dev).driver }; + if pf_drv.is_null() { + return Err(EINVAL); + } + + // SAFETY: `pf_drv` is a valid pointer to a `struct pci_driver`. + if !unsafe { (*pf_drv).managed_sriov } { + return Err(EINVAL); + } + + // SAFETY: `physfn` may be cast to a `Device` since= the + // driver flag `managed_sriov` forces SR-IOV to be disabled when t= he + // PF driver is unbound, i.e., all VF devices are destroyed. This + // guarantees that the underlying PF device is bound to a driver + // when the VF device is bound to a driver, which is the case since + // `Device::physfn()` requires a `&Device` reference. + Ok(unsafe { &*pf_dev.cast() }) + } +} + impl Device { /// Enable memory resources for this device. pub fn enable_device_mem(&self) -> Result { --=20 2.52.0