Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2187018iof; Tue, 7 Jun 2022 22:34:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyk0b2pnb/Zn3yftdg93kRud7AkaGXVRubk39VW/LGVQhONQWC31aQePNgoGmh3VRXFEg2 X-Received: by 2002:a17:902:bb92:b0:153:4eae:c77e with SMTP id m18-20020a170902bb9200b001534eaec77emr31910258pls.93.1654666458272; Tue, 07 Jun 2022 22:34:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654666458; cv=none; d=google.com; s=arc-20160816; b=OTTq2IzMJdc583ViEBKC6js/gJs9fQ87s34WEt+yvlnicvnzpPlqdCBdIKO5pT/oAz +nSFCtnZI17hjU4w7BDBOF5YG6TF0HtUHBVaLq7FDZNuDX/SXmK/Yb8+qXmYEDDLNlwM NxvuehaoQd94Cj4n1w6KZllANeXuqNrFKSe2RAFYcBPjyGYEOk2kYscaEIVbtKb39ciA RL5xaMYLmIDSV/rd5PyiMwPmVf6UCXOm6c9D5+rnPZfm9PispqMjJPV+vuY676wzlcIo 3lo89b+wbGts7TXhkhEUaLGugxk/B9sVRKWaIrHcBqYadK0urwpu2iN5uTB+ruDiYa+O zNyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=K1ciEwBjIZbiRYS6ad/KkCf+kIebAbko707A2ZKot8s=; b=gjAzr5jvwaHw5IaCsAObV6i/UuCeXRCsWmIZQiriYS7HFKYck1l2UGxT3BmcDLFV9t CIXbSZqyy44qZWWtdzSeQssxubevKdOX3nnvd8BKx2eXxRILk/Zk6EFaJFQSh9KNT7x2 7EXXLpyOS1K1zNGmldhadRsYgBHRnkGSr79ZT9twMN+S26aw1924saR2W2e1O6ZKz0uB wqd4xuJgnCpSKkVXrlcjqdGYxtCYI0UCqhPPtYB0VNNfIzlPC9oSAZiQuR99/j9qVbRk DCK4S9ihKi4wrUH2QkBdl4CIVoVIEN67RMPFpxIB2sk4BVhYQMqs2Xy+4aTxK8EB3JkK wkPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UWQ++CF7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id z9-20020a17090acb0900b001df676e8351si24308354pjt.167.2022.06.07.22.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:34:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UWQ++CF7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 592FB2CF2B3; Tue, 7 Jun 2022 22:01:17 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347205AbiFGRaP (ORCPT + 99 others); Tue, 7 Jun 2022 13:30:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346874AbiFGRZc (ORCPT ); Tue, 7 Jun 2022 13:25:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B66771109BC; Tue, 7 Jun 2022 10:23:26 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 62A6960DBA; Tue, 7 Jun 2022 17:23:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74608C385A5; Tue, 7 Jun 2022 17:23:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654622605; bh=PInb+dUPQKGb+cDWT7JHyl16dOt/brmkR8T/Z09YEmQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UWQ++CF7W1riMANsChy5mRre9VHlKB0tHuznqRLclAc36t51TJH4q7Pm48TiZjfRl clP4uqy8I/0uWrIq9JsbzAFYwSt5Znma6u7UmkTI8oHVlOUHw4zwZjIi5WP4bJADmg zvM0nm8/UYnaihc2IhNOAYjF3krsErq1mQAWZOfE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yicong Yang , Bjorn Helgaas , Sasha Levin , Jay Zhou Subject: [PATCH 5.10 112/452] PCI: Avoid pci_dev_lock() AB/BA deadlock with sriov_numvfs_store() Date: Tue, 7 Jun 2022 18:59:29 +0200 Message-Id: <20220607164911.893782175@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607164908.521895282@linuxfoundation.org> References: <20220607164908.521895282@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yicong Yang [ Upstream commit a91ee0e9fca9d7501286cfbced9b30a33e52740a ] The sysfs sriov_numvfs_store() path acquires the device lock before the config space access lock: sriov_numvfs_store device_lock # A (1) acquire device lock sriov_configure vfio_pci_sriov_configure # (for example) vfio_pci_core_sriov_configure pci_disable_sriov sriov_disable pci_cfg_access_lock pci_wait_cfg # B (4) wait for dev->block_cfg_access == 0 Previously, pci_dev_lock() acquired the config space access lock before the device lock: pci_dev_lock pci_cfg_access_lock dev->block_cfg_access = 1 # B (2) set dev->block_cfg_access = 1 device_lock # A (3) wait for device lock Any path that uses pci_dev_lock(), e.g., pci_reset_function(), may deadlock with sriov_numvfs_store() if the operations occur in the sequence (1) (2) (3) (4). Avoid the deadlock by reversing the order in pci_dev_lock() so it acquires the device lock before the config space access lock, the same as the sriov_numvfs_store() path. [bhelgaas: combined and adapted commit log from Jay Zhou's independent subsequent posting: https://lore.kernel.org/r/20220404062539.1710-1-jianjay.zhou@huawei.com] Link: https://lore.kernel.org/linux-pci/1583489997-17156-1-git-send-email-yangyicong@hisilicon.com/ Also-posted-by: Jay Zhou Signed-off-by: Yicong Yang Signed-off-by: Bjorn Helgaas Signed-off-by: Sasha Levin --- drivers/pci/pci.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index cda17c615148..6ebbe06f0b08 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -4975,18 +4975,18 @@ static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe) static void pci_dev_lock(struct pci_dev *dev) { - pci_cfg_access_lock(dev); /* block PM suspend, driver probe, etc. */ device_lock(&dev->dev); + pci_cfg_access_lock(dev); } /* Return 1 on successful lock, 0 on contention */ static int pci_dev_trylock(struct pci_dev *dev) { - if (pci_cfg_access_trylock(dev)) { - if (device_trylock(&dev->dev)) + if (device_trylock(&dev->dev)) { + if (pci_cfg_access_trylock(dev)) return 1; - pci_cfg_access_unlock(dev); + device_unlock(&dev->dev); } return 0; @@ -4994,8 +4994,8 @@ static int pci_dev_trylock(struct pci_dev *dev) static void pci_dev_unlock(struct pci_dev *dev) { - device_unlock(&dev->dev); pci_cfg_access_unlock(dev); + device_unlock(&dev->dev); } static void pci_dev_save_and_disable(struct pci_dev *dev) -- 2.35.1