Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3156120pxk; Mon, 7 Sep 2020 05:02:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzcp6ptJlG6eHyDU6LCtNDKF7ZruZrQC7Uh0oJFEkvZN9UJh0xubRHfJFCuTlnbgpx/OHRw X-Received: by 2002:a50:ab59:: with SMTP id t25mr20659357edc.364.1599480164035; Mon, 07 Sep 2020 05:02:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599480164; cv=none; d=google.com; s=arc-20160816; b=lpX3wkDyX51TxzY2gUZbDjqoaxT41UKAcwK4OBxPK4+y/e7fh65OdhFJd+erYG0ljS 2g7HHXf1msNj6DCyixycZ4kClWw3ecyU/TsqOl+/6OeGoW+GI6CnXP6Ypx609x2YA+/F 2PnTzZwZTgzJpfNZnRJjgk4qsuBRsdvAHXSSorXK9RDTZbUO3/XEHtUW4BlFgnTWWk5l Gfu6E+BkXT+/QWCt2UAN2YYu9wAn1fwGgae2tDT8MaS13mW4zQR43B+t43z1BKJvWm6y tyT5qazNP8m7qvFvah9e0HtQDXcvgqG9fGF2zMCLsT7lHlF8zXaKAeepZ1zhnHquY1M1 Np0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=cDoOUs36c6IVroCljBhCP1jfgLsq7CdJ+MLzPwWEUOs=; b=KlQf27+M8LKYL5qhOSShSOsfI4nEJNJWR5AhjXJxTBpwnjJtzxMtb7PUPgoCAnxQm+ bUf+OWmknNXO7rhh0oocC3VPMxxX6Jff19DSSRdB+oocmjV6gvyosC6Ohf7Wr0hlpV5a 52MflTEv1BbwVNcZfL7sA3q2h2XBn5JlGcM6hLQ21UkYgrESyNzSo/PBKGZ9ncR7zdpa qAGc/eyoLLYtJdnbtAp/bcXmpibHrRC9zVGDZuTFGk1rba6pPaCKZMgAfUP4a3yo3bXE UadVf8fwEXN9V9HvpuN/JE++N89oFkOGvXVH7Zv5ibbrd5F0W64vbeTAqTkE0TFJEf5V OwhQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h23si9318045ejd.576.2020.09.07.05.02.21; Mon, 07 Sep 2020 05:02:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729069AbgIGMA5 (ORCPT + 99 others); Mon, 7 Sep 2020 08:00:57 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:10826 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728941AbgIGLYV (ORCPT ); Mon, 7 Sep 2020 07:24:21 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 6CB4A68D8EC7E2DC4BFC; Mon, 7 Sep 2020 19:07:18 +0800 (CST) Received: from localhost.localdomain (10.67.165.24) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.487.0; Mon, 7 Sep 2020 19:07:11 +0800 From: Yicong Yang To: , CC: , Subject: [PATCH] PCI: Fix race condition between block_cfg_access and msi_enabled/msix_enabled Date: Mon, 7 Sep 2020 19:06:05 +0800 Message-ID: <1599476765-47123-1-git-send-email-yangyicong@hisilicon.com> X-Mailer: git-send-email 2.8.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.165.24] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Previously we use bit field for block_cfg_access and msi_enabled/msix_enabled, which is non-atomic and they may race each other as they share the same memory region. A race condition is met between driver bind vs FLR through sysfs: for driver bind side in thread 1: ... device_lock() ... ->probe() pci_alloc_irq_vectors_affinity() __pci_enable_msi_range() msi_capability_init() dev->msi_enabled=1 <---set here request_irq(pci_irq_vector(),...) when echo 1 > reset in thread 2: pci_reset_function() pci_dev_lock() pci_cfg_access_lock() dev->block_cfg_access=1 <---may overwrite msi_enabled bit device_lock() The msi_enabled bit may be overwritten to 0 and will trigger the WARN assert in pci_irq_vector(). A similar issue has been addressed in commit 44bda4b7d26e ("PCI: Fix is_added/is_busmaster race condition"). Move the block_cfg_access to the pci_dev->priv_flags and use atomic bit operations to avoid the race condition. Signed-off-by: Yicong Yang --- drivers/pci/access.c | 20 ++++++++++---------- drivers/pci/pci.h | 11 +++++++++++ include/linux/pci.h | 1 - 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/drivers/pci/access.c b/drivers/pci/access.c index 4693569..5826962 100644 --- a/drivers/pci/access.c +++ b/drivers/pci/access.c @@ -208,9 +208,9 @@ static noinline void pci_wait_cfg(struct pci_dev *dev) { do { raw_spin_unlock_irq(&pci_lock); - wait_event(pci_cfg_wait, !dev->block_cfg_access); + wait_event(pci_cfg_wait, !pci_dev_is_cfg_access_blocked(dev)); raw_spin_lock_irq(&pci_lock); - } while (dev->block_cfg_access); + } while (pci_dev_is_cfg_access_blocked(dev)); } /* Returns 0 on success, negative values indicate error. */ @@ -223,7 +223,7 @@ int pci_user_read_config_##size \ if (PCI_##size##_BAD) \ return -EINVAL; \ raw_spin_lock_irq(&pci_lock); \ - if (unlikely(dev->block_cfg_access)) \ + if (unlikely(pci_dev_is_cfg_access_blocked(dev))) \ pci_wait_cfg(dev); \ ret = dev->bus->ops->read(dev->bus, dev->devfn, \ pos, sizeof(type), &data); \ @@ -242,7 +242,7 @@ int pci_user_write_config_##size \ if (PCI_##size##_BAD) \ return -EINVAL; \ raw_spin_lock_irq(&pci_lock); \ - if (unlikely(dev->block_cfg_access)) \ + if (unlikely(pci_dev_is_cfg_access_blocked(dev))) \ pci_wait_cfg(dev); \ ret = dev->bus->ops->write(dev->bus, dev->devfn, \ pos, sizeof(type), val); \ @@ -271,9 +271,9 @@ void pci_cfg_access_lock(struct pci_dev *dev) might_sleep(); raw_spin_lock_irq(&pci_lock); - if (dev->block_cfg_access) + if (pci_dev_is_cfg_access_blocked(dev)) pci_wait_cfg(dev); - dev->block_cfg_access = 1; + pci_dev_block_cfg_access(dev, true); raw_spin_unlock_irq(&pci_lock); } EXPORT_SYMBOL_GPL(pci_cfg_access_lock); @@ -292,10 +292,10 @@ bool pci_cfg_access_trylock(struct pci_dev *dev) bool locked = true; raw_spin_lock_irqsave(&pci_lock, flags); - if (dev->block_cfg_access) + if (pci_dev_is_cfg_access_blocked(dev)) locked = false; else - dev->block_cfg_access = 1; + pci_dev_block_cfg_access(dev, true); raw_spin_unlock_irqrestore(&pci_lock, flags); return locked; @@ -318,9 +318,9 @@ void pci_cfg_access_unlock(struct pci_dev *dev) * This indicates a problem in the caller, but we don't need * to kill them, unlike a double-block above. */ - WARN_ON(!dev->block_cfg_access); + WARN_ON(!pci_dev_is_cfg_access_blocked(dev)); - dev->block_cfg_access = 0; + pci_dev_block_cfg_access(dev, false); raw_spin_unlock_irqrestore(&pci_lock, flags); wake_up_all(&pci_cfg_wait); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 73740dd..1cf3122 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -410,6 +410,7 @@ static inline bool pci_dev_is_disconnected(const struct pci_dev *dev) /* pci_dev priv_flags */ #define PCI_DEV_ADDED 0 +#define PCI_DEV_BLOCK_CFG_ACCESS 1 /* Config space access blocked */ static inline void pci_dev_assign_added(struct pci_dev *dev, bool added) { @@ -421,6 +422,16 @@ static inline bool pci_dev_is_added(const struct pci_dev *dev) return test_bit(PCI_DEV_ADDED, &dev->priv_flags); } +static inline void pci_dev_block_cfg_access(struct pci_dev *dev, bool block) +{ + assign_bit(PCI_DEV_BLOCK_CFG_ACCESS, &dev->priv_flags, block); +} + +static inline bool pci_dev_is_cfg_access_blocked(struct pci_dev *dev) +{ + return test_bit(PCI_DEV_BLOCK_CFG_ACCESS, &dev->priv_flags); +} + #ifdef CONFIG_PCIEAER #include diff --git a/include/linux/pci.h b/include/linux/pci.h index 8355306..4ffb588 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -406,7 +406,6 @@ struct pci_dev { unsigned int is_busmaster:1; /* Is busmaster */ unsigned int no_msi:1; /* May not use MSI */ unsigned int no_64bit_msi:1; /* May only use 32-bit MSIs */ - unsigned int block_cfg_access:1; /* Config space access blocked */ unsigned int broken_parity_status:1; /* Generates false positive parity */ unsigned int irq_reroute_variant:2; /* Needs IRQ rerouting variant */ unsigned int msi_enabled:1; -- 2.8.1