From nobody Tue Dec 2 02:29:06 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 BD76E329381 for ; Wed, 19 Nov 2025 22:19:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763590774; cv=none; b=eNFbOsYvl4qF8bb/UV9351aBILFhDxzgT8lI16NEayfPxcocCIBAr3G2s1XjDZQk83F1pacU9gEYQV8JPdY3yGYeDcujBfv5KVs4fuRQxPy9KE021YQIJhgYjZSy2NlNYLUpxX6zCmNG0d8C6jCvMx1IMtzzo20xeWIugWoKJ08= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763590774; c=relaxed/simple; bh=isBVQ57+Brz1VfxrUYneQSmfzbDfRyfWhKEYUrQf6Oc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ko6mJpy3LWaiENz0UhabG6uvsADKJEAfLqA+AoybfpqTaFXWAJt+ET6okEAcqWxz+Rq11NfNpeqeEJKGlQlrxlPOCyu1ViM0snwxs30R84+dTC9DR2Gncd7Q5tC8Q4OQPzNGJjIhJ77+jf1nMZFlRbgpjg6w+eOWFXQBGa1WtSQ= 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=MMZRZqtt; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=QMdubPD1; arc=none smtp.client-ip=170.10.129.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="MMZRZqtt"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="QMdubPD1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763590769; 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=8TaHEw4qfrN2YhxZsjXLb3t5RR9TWTSPbbKUt1wn06E=; b=MMZRZqttV2x7tO1K5D0HNiykivfH1TcuervJ8m5GZr+PiuWbmFNvCD0rRUVMWzxW92TlMe HwQVEV8Ld6cNFNu7PzAEgCyBsTDUQjcLYYdRRPGtZrbwkHuwOB1yNQ+21RYelCupHCpxRJ +xvs3FfC9MV8axB6lwgk4bIY+gKoKBw= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-161-3LydTOKqOPGdzF8-aw51nQ-1; Wed, 19 Nov 2025 17:19:28 -0500 X-MC-Unique: 3LydTOKqOPGdzF8-aw51nQ-1 X-Mimecast-MFC-AGG-ID: 3LydTOKqOPGdzF8-aw51nQ_1763590767 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-8804b9afe30so9030216d6.0 for ; Wed, 19 Nov 2025 14:19:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1763590767; x=1764195567; 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=8TaHEw4qfrN2YhxZsjXLb3t5RR9TWTSPbbKUt1wn06E=; b=QMdubPD1MAzVcmSYvJ3WKZpqlDeoC2bXnJZ7pYxToyCByT+VZ67873aZoEtVLwTtzw wbMnOOHyP25l1tWQrIiZOrqpr2u/OHe+u7FoMoL/UvCOat9FMqzki3Qe6iHFlFhFa+ar XwxHNgryTyqWhPt1DhCkL/KApNLRaspcmGk8QZ0qK4bXSsNEvueIbWqaaAax55YZUTUc ENfYehJLBrc6uT+1+GVOG8GMlhNj5w3sxMIonAt4JIXmru3FvJ/2yKINh0QnE/nw3DWp vt8RTYdbWggosDKvxz5PhYyKZoTUeuFnnMzYCqY1FfvzR0Ekg8/jV1KNzETjMo9yVf8B GMyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763590767; x=1764195567; 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=8TaHEw4qfrN2YhxZsjXLb3t5RR9TWTSPbbKUt1wn06E=; b=sVICYTg2zU068axw7Lpyvyn/FZtXElzEO8NxSi2znLzZnoPhSqDfiMevsHihv3RWpp TlQBMjuXjm8Ve/kuB0ipl5Yt1jeiHDpz8r9LSFNokbtQeQgu6ZldWMAP5JVSIf7gsIN4 nK01zEWx43OeHy/Rx5usxHHZw3hzBxH76c8tAHbrkOgXQ6Uejh+cRnM5Ud1ylpvovLLL SPS6GQ3JN8nN4zN/mgI9412s7GEPsNcNzifMjDtU7Vpdq144pRAqWg95dbslKodbhZpt GSaGvJ2RIWcg64Sbz/XyeNnlqQSM7Vjmlz6O7zH8Rwll4hAxkv2zz23L2RzL53jgHwzD uhLA== X-Forwarded-Encrypted: i=1; AJvYcCUA1/xc+NPaoGONC/BKkJEy6A/pmySjqx3W/MsoTEYc/mbSB+XEjTprpMUPOGgZdyIB7apgRkENnXRbQX0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4EfHQtUXFEqlFZ9UEZNLT1cv0ouYLWEfOQy4I2iz/lqNPq6Ho SEm7mQRBTmzo+K0WkBhh/lEH3phX7MuYQ363B4V3UGOec8fEYtPBV3e/9seZqxvBOkHgyrwcaRm w/bfktQp/OH8G4Gefs67zK0eDaL+Oal5BRdIpC9WJyokfhmAQ2iKnlpZU++XVP+nvCQ== X-Gm-Gg: ASbGnctIe/2ayIya2GBL1yvHCfxEeFzWZOtkEILKAzdKmjhMQ7WzHKsTAnZouNzaOCO MyAFVIzE2M2ASR/hwNYsuAWuoSibvnQzuFP3yzhWu+yjjdOXx58wpj/zhNKCvFO5CvhfIGE0lGR 11LONo4tgj5JAIlYYTGcGndVltORPaxTdzfObSwbQYzY1EtAnz9VuQB2R27LSHSp2WFb8ME/gXX r7N+yT60CXPi0ROEz5G++Unjp/E+DE3DA2UXfcl5K2vDWhjkS6WvAn9qJACelWNlUGR70T6h3fy XYXolyR6YJZxbl5TFSbKpEsgKtJ8vVnw+B4V3IQlUKTpvBoFoDsnblQg4/wNQGlSiaZIPg7MSMU QTwcnQbm4h7jisg== X-Received: by 2002:a05:6214:33c4:b0:880:5bff:74d6 with SMTP id 6a1803df08f44-8846e16c331mr12082806d6.51.1763590766620; Wed, 19 Nov 2025 14:19:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IG56WlPFxLZ+xUxoXWMrJpIJfnEVBJYcG8NscQK+qr9mchmkFNoMy2xH4aNIky2oxUFrfMmpw== X-Received: by 2002:a05:6214:33c4:b0:880:5bff:74d6 with SMTP id 6a1803df08f44-8846e16c331mr12082416d6.51.1763590766172; Wed, 19 Nov 2025 14:19:26 -0800 (PST) Received: from [172.16.1.8] ([2607:f2c0:b141:ac00:ca1:dc8c:d6d0:7e87]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8846e447304sm4426866d6.4.2025.11.19.14.19.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Nov 2025 14:19:25 -0800 (PST) From: Peter Colberg Date: Wed, 19 Nov 2025 17:19:11 -0500 Subject: [PATCH 7/8] 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: <20251119-rust-pci-sriov-v1-7-883a94599a97@redhat.com> References: <20251119-rust-pci-sriov-v1-0-883a94599a97@redhat.com> In-Reply-To: <20251119-rust-pci-sriov-v1-0-883a94599a97@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 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 , 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. When a device with enabled VFs is unbound from a driver, invoke the sriov_configure() callback to disable SR-IOV before the unbind() callback. To ensure the guarantee is upheld, call disable_sriov() to remove all VF devices if the driver has not done so already. Suggested-by: Danilo Krummrich Signed-off-by: Peter Colberg --- rust/kernel/pci.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index f9054c52a3bdff2c42273366a4058d943ee5fd76..d6cc5d7e7cd7a3b6e451c0ff5ae= a044b89c6774a 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -120,6 +120,19 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci= _dev) { // and stored a `Pin>`. let data =3D unsafe { pdev.as_ref().drvdata_obtain::() }; =20 + // Always disable SR-IOV before `unbind()` to guarantee that when = a VF device is bound + // to a driver, the underlying PF device is bound to a driver, too. + #[cfg(CONFIG_PCI_IOV)] + { + // First, allow the driver to gracefully disable SR-IOV by its= elf. + if T::HAS_SRIOV_CONFIGURE && pdev.num_vf() !=3D 0 { + let _ =3D T::sriov_configure(pdev, 0); + } + // Then, forcibly disable SR-IOV if the driver has not done so= already, + // to uphold the guarantee with regard to driver binding descr= ibed above. + pdev.disable_sriov(); + } + T::unbind(pdev, data.as_ref()); } =20 @@ -332,6 +345,11 @@ fn unbind(dev: &Device, this: Pin<&Self>= ) { /// [`Device`] by writing the number of Virtual Functions (VF), `nr_vi= rtfn` or zero to the /// sysfs file `sriov_numvfs` for this device. Implementing this callb= ack is optional. /// + /// Further, and unlike for a PCI driver written in C, when a PF devic= e with enabled VFs is + /// unbound from its bound [`Driver`], the `sriov_configure()` callbac= k is invoked to disable + /// SR-IOV before the `unbind()` callback. This guarantees that when a= VF device is bound to a + /// driver, the underlying PF device is bound to a driver, too. + /// /// Upon success, this callback must return the number of VFs that wer= e enabled, or zero if /// SR-IOV was disabled. /// @@ -500,6 +518,35 @@ 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`. + /// + /// ``` + /// let pf_pdev =3D vf_pdev.physfn()?; + /// let pf_drvdata =3D pf_pdev.as_ref().drvdata::()?; + /// ``` + #[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 self.is= _virtfn() is `true`. + // + // `physfn` may be cast to a `Device` since `pci::D= river::remove()` calls + // `disable_sriov()` to remove all VF devices, which guarantees th= at the underlying + // PF device is always bound to a driver when the VF device is bou= nd to a driver. + Ok(unsafe { &*(*self.as_raw()).__bindgen_anon_1.physfn.cast() }) + } +} + impl Device { /// Enable memory resources for this device. pub fn enable_device_mem(&self) -> Result { --=20 2.51.1