From patchwork Mon Feb 7 17:22:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 12737762 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1CA5DC3526C for ; Mon, 7 Feb 2022 17:33:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382915AbiBGRbO (ORCPT ); Mon, 7 Feb 2022 12:31:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346174AbiBGRXb (ORCPT ); Mon, 7 Feb 2022 12:23:31 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam07on2051.outbound.protection.outlook.com [40.107.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BC66C0401D5; Mon, 7 Feb 2022 09:23:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e75H7EOs14+yKQfTfbvRPl1AVtiRC3JIcNh3kirVukzqbyT5Wu7bPEsMWOg1mXerirfwD5/+85iQeF3sCOQztp0yYtK24pXBMCWCWHiPE+ZvVq6Hu1BBwS4DuzXNL1bPgTlexFmwbRGhhf3secHYT42lxsBZcfFDW1GflMQ96SrgJ4bYnsyJc9qBFSdREVzEb0WM/SznHEqmTH5SyY0o/3+cN4V1RDdZFiVbybRwzjM4qyormA8hR5lR2RM4isSAUMRvNfa+OsQ5YHIhJmclaP8zVjNJlsnc3bpzf4ObzfmroRdFNlHpuGrF/V7Mocaes2Sinkwri6NAw7m+i+Db3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=huHD3ovZNc9rzQ0iGUHiVTaCqum+rUNRny5WgGaN18I=; b=l9gXChJriPSYrRPfYJ7fdwMDjdkaNDbInbZIWX2GikUCA75Zv7atsEfkKWrPEeXFUzSUOxcijI0fDaV/VzDHA/OjypjfHIPRtWX7YsqOA6SkDJRh2MmJVt5CAM+ta55rqLc1OkJh+b3paAbzI3YKAln78Al7KsVnL4KPnXHlwjfI6GvIc8FTvjiR1j49u2m8SrJ3GL9HCeNE60YpWz9oxEB9Q8eTS0K16imqNwFvIY58EGTiB1pSLzY1ppO/65FbmQrfkFZmi2EZZfG3EUIJEe7Asgj90tt9s4pa09xMvdSHcw/TkUL2Ej/LOxifW6NgxuL/D6QvVPsCyLCpMYEsIw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.235) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=huHD3ovZNc9rzQ0iGUHiVTaCqum+rUNRny5WgGaN18I=; b=hIaGylfKIpvuzgKsiPqMNA6/Vz37Py54wf7Xkx6DFDCGMk6bL+D9vioJ6jnTH9dKoKtThdMPsUFvqJ9ArY+ENhJleg6bGHV45MsHe3F4d3yqMLYvtmdcbnuS9V3C/gfhxLJHUHrUbMz9tMr9O8L6BJWx9uKeKLM3w9MQGqJPIDUFkElOFjLdoHEFKOoCZ3RQVJl8y2z3oYH3xz5XDkUtDn34kXimyn6cuD6VjdxCzhKIyeYz+MWL4kfMVvvhEEy15UnldoeLUFyH+ATMYPuni7QgI0AiGGQ++/kZIxRQMzf44q/9GSox4HLspvPsw0d1WY1CClEkU2i2tZrQbW6/3Q== Received: from BN9PR03CA0469.namprd03.prod.outlook.com (2603:10b6:408:139::24) by DM6PR12MB4986.namprd12.prod.outlook.com (2603:10b6:5:16f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.17; Mon, 7 Feb 2022 17:23:28 +0000 Received: from BN8NAM11FT068.eop-nam11.prod.protection.outlook.com (2603:10b6:408:139:cafe::f3) by BN9PR03CA0469.outlook.office365.com (2603:10b6:408:139::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.12 via Frontend Transport; Mon, 7 Feb 2022 17:23:27 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.235) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.235 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.235; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.235) by BN8NAM11FT068.mail.protection.outlook.com (10.13.177.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4951.12 via Frontend Transport; Mon, 7 Feb 2022 17:23:27 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.18; Mon, 7 Feb 2022 17:23:23 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.9; Mon, 7 Feb 2022 09:23:22 -0800 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.9 via Frontend Transport; Mon, 7 Feb 2022 09:23:18 -0800 From: Yishai Hadas To: , , , CC: , , , , , , , , , , , Subject: [PATCH V7 mlx5-next 04/15] PCI/IOV: Add pci_iov_get_pf_drvdata() to allow VF reaching the drvdata of a PF Date: Mon, 7 Feb 2022 19:22:05 +0200 Message-ID: <20220207172216.206415-5-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20220207172216.206415-1-yishaih@nvidia.com> References: <20220207172216.206415-1-yishaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4dda248d-b5d5-46c4-ff5b-08d9ea5e8dd0 X-MS-TrafficTypeDiagnostic: DM6PR12MB4986:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: T4Mn7V26FmugjTiTNRuUcSXSIhXjWszlcqw8tSgwzJGQAhjLnaJffK7FGU6OObnoswbWDQw98HBeLHnAHCNmQmEtzecQXrfGOFltTSDCZEjgTELjqbXyQKxD/x5plloTmq41i8feCisdeC3hqFVWZOsYVzQGKMqzZYic1UrIbBUusz4PQBj7hTJ57Q7sa8P/D9971pASIoO70gq9bARGqXqkFCL95ePg8oX5CNPdATuprumGiNBOuzyhbvZDMmbP9zF/Ex0SUU6uhFTCG2QFrQMzqYp8Lq7i/6rVjxCuh5z3iyCwymihUkVCASHSYIua4/pfxFNMbWXj2FC4KkOb04IUhROyr1m4CvkzYXFPi/sxDxvDxNdWNh2pWkdBYE/Y03w7i+qi5NVvwM7IGJYOYhGZDUF/7UXgLTJd7evKMHfH7T12QJtuVn32RIjPsP2OuZXuPjlIxLySouCzZ09Ks/Eo3UkYY5eZWIyg04zQfrseEssbEPT4NYyT0qCTU2zFeOn/iyfLQyUY3pl7I6yE9r1//8ADXUnLIYXM2IuxthkSqCjhA0/KtL28AX3UYxs1tPZp2kARnOJRdl6Ai7/O0Sx5QTphEh+AHpzgMyuXY28umTlB9SzWiAK3NnDRHQhye/++XGuRUTFV17ugR+dHrUpgWaTHzDidRow5CsL1toD1ib9qxd6zAZAP6dV/D8R2UjdWLYs3Lx9+I8md21rrww== X-Forefront-Antispam-Report: CIP:12.22.5.235;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(2616005)(81166007)(2906002)(6666004)(82310400004)(86362001)(40460700003)(5660300002)(7696005)(356005)(8936002)(508600001)(83380400001)(316002)(70586007)(36756003)(54906003)(6636002)(4326008)(8676002)(110136005)(70206006)(36860700001)(26005)(186003)(1076003)(47076005)(426003)(336012)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Feb 2022 17:23:27.5026 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4dda248d-b5d5-46c4-ff5b-08d9ea5e8dd0 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.235];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT068.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4986 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org From: Jason Gunthorpe There are some cases where a SR-IOV VF driver will need to reach into and interact with the PF driver. This requires accessing the drvdata of the PF. Provide a function pci_iov_get_pf_drvdata() to return this PF drvdata in a safe way. Normally accessing a drvdata of a foreign struct device would be done using the device_lock() to protect against device driver probe()/remove() races. However, due to the design of pci_enable_sriov() this will result in a ABBA deadlock on the device_lock as the PF's device_lock is held during PF sriov_configure() while calling pci_enable_sriov() which in turn holds the VF's device_lock while calling VF probe(), and similarly for remove. This means the VF driver can never obtain the PF's device_lock. Instead use the implicit locking created by pci_enable/disable_sriov(). A VF driver can access its PF drvdata only while its own driver is attached, and the PF driver can control access to its own drvdata based on when it calls pci_enable/disable_sriov(). To use this API the PF driver will setup the PF drvdata in the probe() function. pci_enable_sriov() is only called from sriov_configure() which cannot happen until probe() completes, ensuring no VF races with drvdata setup. For removal, the PF driver must call pci_disable_sriov() in its remove function before destroying any of the drvdata. This ensures that all VF drivers are unbound before returning, fencing concurrent access to the drvdata. The introduction of a new function to do this access makes clear the special locking scheme and the documents the requirements on the PF/VF drivers using this. Signed-off-by: Jason Gunthorpe Acked-by: Bjorn Helgaas Signed-off-by: Leon Romanovsky Signed-off-by: Yishai Hadas --- drivers/pci/iov.c | 29 +++++++++++++++++++++++++++++ include/linux/pci.h | 7 +++++++ 2 files changed, 36 insertions(+) diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c index 2e9f3d70803a..28ec952e1221 100644 --- a/drivers/pci/iov.c +++ b/drivers/pci/iov.c @@ -47,6 +47,35 @@ int pci_iov_vf_id(struct pci_dev *dev) } EXPORT_SYMBOL_GPL(pci_iov_vf_id); +/** + * pci_iov_get_pf_drvdata - Return the drvdata of a PF + * @dev - VF pci_dev + * @pf_driver - Device driver required to own the PF + * + * This must be called from a context that ensures that a VF driver is attached. + * The value returned is invalid once the VF driver completes its remove() + * callback. + * + * Locking is achieved by the driver core. A VF driver cannot be probed until + * pci_enable_sriov() is called and pci_disable_sriov() does not return until + * all VF drivers have completed their remove(). + * + * The PF driver must call pci_disable_sriov() before it begins to destroy the + * drvdata. + */ +void *pci_iov_get_pf_drvdata(struct pci_dev *dev, struct pci_driver *pf_driver) +{ + struct pci_dev *pf_dev; + + if (!dev->is_virtfn) + return ERR_PTR(-EINVAL); + pf_dev = dev->physfn; + if (pf_dev->driver != pf_driver) + return ERR_PTR(-EINVAL); + return pci_get_drvdata(pf_dev); +} +EXPORT_SYMBOL_GPL(pci_iov_get_pf_drvdata); + /* * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may * change when NumVFs changes. diff --git a/include/linux/pci.h b/include/linux/pci.h index 3d4ff7b35ad1..60d423d8f0c4 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -2167,6 +2167,7 @@ void __iomem *pci_ioremap_wc_bar(struct pci_dev *pdev, int bar); int pci_iov_virtfn_bus(struct pci_dev *dev, int id); int pci_iov_virtfn_devfn(struct pci_dev *dev, int id); int pci_iov_vf_id(struct pci_dev *dev); +void *pci_iov_get_pf_drvdata(struct pci_dev *dev, struct pci_driver *pf_driver); int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn); void pci_disable_sriov(struct pci_dev *dev); @@ -2200,6 +2201,12 @@ static inline int pci_iov_vf_id(struct pci_dev *dev) return -ENOSYS; } +static inline void *pci_iov_get_pf_drvdata(struct pci_dev *dev, + struct pci_driver *pf_driver) +{ + return ERR_PTR(-EINVAL); +} + static inline int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn) { return -ENODEV; }