From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 21D0EEDEBF1 for ; Tue, 3 Mar 2026 21:15:56 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 657ED10E8E9; Tue, 3 Mar 2026 21:15:55 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="ClSPKbLW"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 011AF10E8CF for ; Tue, 3 Mar 2026 21:15:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572553; 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=62xgaFtJ6odD5MPfFUAjLdRekJQ/qnRaVs4d4H+RycE=; b=ClSPKbLWZNGN7BuHje3qvLZ7qVruJgZQlQ6Dzt90mRjvMLJOgyuwOe+iLQmRQtehu3M1Eu aSQSrjDKm+qViaKWCQQupOBQPImtlkLD1m9E9esi1+5kppZbDOfiVFmiuqnQNeh/b92YUz pxiThj/9P3Lw7yb/ihg7WMAwOf58mYs= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-600-4RiN34FYNmSmUAFUOXqqOQ-1; Tue, 03 Mar 2026 16:15:52 -0500 X-MC-Unique: 4RiN34FYNmSmUAFUOXqqOQ-1 X-Mimecast-MFC-AGG-ID: 4RiN34FYNmSmUAFUOXqqOQ_1772572552 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-506b4bdde91so469749671cf.2 for ; Tue, 03 Mar 2026 13:15:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572552; x=1773177352; 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=62xgaFtJ6odD5MPfFUAjLdRekJQ/qnRaVs4d4H+RycE=; b=qJVt+I0ROjy89DQR8n2l+LDtHOCDIRLy4JwUG02K1hB61NnbcNIUPmMGbJOfaaubLc rS9LVezabz9ILbxf0BhnmK3vdc4pD8FUM7TGx4osHW58TC+3nu+WQ2yT+dviGCGV42B3 3e9P9gE6JjGutsV/UoV9ggR0WihRHHwFW62m3UjDsvXcDqs0xlegkj5eN1eSuEmP5cHR VElfETiuy1BcaZWcg+VLKgiusxsEK2OV6rFiiprDTe7DfQ+oVUCz9DOFbcDOttMx5Sps v8ws1cXgK6XFflPv7p1Sp7rDulqTmpJ598dq5QK21MmrDic7utP9fzXUvzEqAk0V4n0x naJQ== X-Forwarded-Encrypted: i=1; AJvYcCUhPUftdKaHxw7TLDnJnVzEtm+s/PpnNwP1y05Q1Z3otdyyBh1kCeCpoaLQuG22YOnVifK/Rq8h7BU=@lists.freedesktop.org X-Gm-Message-State: AOJu0YzYpUTfDobwnNuPn5ZH0boloKqcYLuqo9sb3DmCQqkbiTIfiN1B 22hg78vHElofOtRgP5Mc1Wes9PkijHt0KwHTgga6Ak+AC+KLjEPsjnjdQT1a2CD8BUMKSGzdThH Twzmn44XEtslJiRJeh3XLK2TDhPbAeYL2ztqY///aSH0zK8Sq+L9hg3Dalzt5GpNT+Cf9pMvGUk pVdB0gg4vbHgw8SwpzohdiNnLgyhMKA+646R7ihxXVxncS+8G+G2HWRZCe X-Gm-Gg: ATEYQzx0TwwhyIyUb0c3Mmkt3Gio/N0TocQYMfoigCIhI/FincwVYpgAAnq+Wl8ZuCf 2UDhyNQSgjEkI9jeEB211/vFBKSFu425R2gmYojOX8I1uVqs9XND44oxKEerQm0II5OxJmiDzYr GHYUInKWYbXfVwNI6Ggaxgq96ZL9SVUH4Dvmp4se1VkMwq2010C0RpDEs3tdQD941RirK3gyNHL n/pwgEJvVIoBZZjVWeZYLd3snnNLyjXUJUZ3RmXaHPKM/WTAgVLAQVZwQenbbiSu+yFplSF6sch sW6YW44+LfCQcSHroL6KJRb1RawU5F3m/QZ2Gr2mkVD/MEV0L7/cidD8feQVEXn9cQanx80tFEr qPmWHHxa/LACM0+whCwk49YSBWA== X-Received: by 2002:ac8:7d53:0:b0:503:2ea0:ef2a with SMTP id d75a77b69052e-507528a83e7mr213704011cf.18.1772572551617; Tue, 03 Mar 2026 13:15:51 -0800 (PST) X-Received: by 2002:ac8:7d53:0:b0:503:2ea0:ef2a with SMTP id d75a77b69052e-507528a83e7mr213703051cf.18.1772572551032; Tue, 03 Mar 2026 13:15:51 -0800 (PST) Received: from [172.16.1.8] ([2607:f2c0:b1e3:9a00:3c7:56c2:f819:96d2]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-5074481c0e5sm156286991cf.0.2026.03.03.13.15.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:50 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:29 -0500 Subject: [PATCH v3 09/10] rust: pci: add physfn(), to return PF device for VF device MIME-Version: 1.0 Message-Id: <20260303-rust-pci-sriov-v3-9-4443c35f0c88@redhat.com> References: <20260303-rust-pci-sriov-v3-0-4443c35f0c88@redhat.com> In-Reply-To: <20260303-rust-pci-sriov-v3-0-4443c35f0c88@redhat.com> To: Danilo Krummrich , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Miguel Ojeda , Alex Gaynor , 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" , Boqun Feng 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 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: jk1HKmigVbTJdM93cY-6MGy1p5Rphj-h9FHtnsMB7Z4_1772572552 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 v3: - Replace SR_IOV -> SR-IOV in description. 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..3b11f73a9f2b69a02fe003b8feadd61864adc8c0 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -525,6 +525,59 @@ pub fn pci_class(&self) -> Class { } } +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 = vf_pdev.physfn()?; + /// let pf_drvdata = 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 pci_dev`. + // `physfn` is a valid pointer to a `struct pci_dev` since `is_virtfn()` is `true`. + let pf_dev = 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_driver`. + let pf_drv = 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 the + // 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 { -- 2.53.0