From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: linux-pci@vger.kernel.org, "Bjorn Helgaas" <bhelgaas@google.com>,
"Lorenzo Pieralisi" <lorenzo.pieralisi@arm.com>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Wilczyński" <kw@linux.com>,
"Emmanuel Grumbach" <emmanuel.grumbach@intel.com>,
"Rafael J . Wysocki" <rafael@kernel.org>,
"Heiner Kallweit" <hkallweit1@gmail.com>,
"Lukas Wunner" <lukas@wunner.de>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Rafael J. Wysocki" <rjw@rjwysocki.net>,
"Jesse Barnes" <jbarnes@virtuousgeek.org>,
"Yijing Wang" <wangyijing@huawei.com>,
"Jiang Liu" <jiang.liu@huawei.com>,
"Shaohua Li" <shaohua.li@intel.com>,
"Greg Kroah-Hartman" <gregkh@suse.de>,
"Doug Ledford" <dledford@redhat.com>,
"Mike Marciniszyn" <mike.marciniszyn@intel.com>,
"Dean Luick" <dean.luick@intel.com>,
"Ashutosh Dixit" <ashutosh.dixit@intel.com>,
"Ira Weiny" <ira.weiny@intel.com>,
"Ricky Wu" <ricky_wu@realtek.com>,
"Rui Feng" <rui_feng@realsil.com.cn>,
"Lee Jones" <lee@kernel.org>,
"Micky Ching" <micky_ching@realsil.com.cn>,
"Wei WANG" <wei_wang@realsil.com.cn>,
"Samuel Ortiz" <sameo@linux.intel.com>,
"Daniel Bristot de Oliveira" <bristot@redhat.com>,
"Michael Chan" <mchan@broadcom.com>,
"Matt Carlson" <mcarlson@broadcom.com>,
"David S. Miller" <davem@davemloft.net>,
"Francois Romieu" <romieu@fr.zoreil.com>,
"Sven Peter" <sven@svenpeter.dev>,
"Luiz Augusto von Dentz" <luiz.von.dentz@intel.com>,
"Bruce Allan" <bruce.w.allan@intel.com>,
"Jeff Kirsher" <jeffrey.t.kirsher@intel.com>,
"Auke Kok" <auke-jan.h.kok@intel.com>,
"Jeff Garzik" <jeff@garzik.org>,
"Vasanthakumar Thiagarajan" <vasanth@atheros.com>,
"John W. Linville" <linville@tuxdriver.com>,
"Stanislaw Gruszka" <sgruszka@redhat.com>,
"Kalle Valo" <kvalo@kernel.org>, "Felix Fietkau" <nbd@nbd.name>,
"Lorenzo Bianconi" <lorenzo@kernel.org>,
linux-kernel@vger.kernel.org
Cc: "Dean Luick" <dean.luick@cornelisnetworks.com>,
"Jonas Dreßler" <verdre@v0yd.nl>,
"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
stable@vger.kernel.org
Subject: [PATCH v5 01/11] PCI: Add locking to RMW PCI Express Capability Register accessors
Date: Mon, 17 Jul 2023 15:04:53 +0300 [thread overview]
Message-ID: <20230717120503.15276-2-ilpo.jarvinen@linux.intel.com> (raw)
In-Reply-To: <20230717120503.15276-1-ilpo.jarvinen@linux.intel.com>
Many places in the kernel write the Link Control and Root Control PCI
Express Capability Registers without proper concurrency control and
this could result in losing the changes one of the writers intended to
make.
Add pcie_cap_lock spinlock into the struct pci_dev and use it to
protect bit changes made in the RMW capability accessors. Protect only
a selected set of registers by differentiating the RMW accessor
internally to locked/unlocked variants using a wrapper which has the
same signature as pcie_capability_clear_and_set_word(). As the
Capability Register (pos) given to the wrapper is always a constant,
the compiler should be able to simplify all the dead-code away.
So far only the Link Control Register (ASPM, hotplug, link retraining,
various drivers) and the Root Control Register (AER & PME) seem to
require RMW locking.
Fixes: c7f486567c1d ("PCI PM: PCIe PME root port service driver")
Fixes: f12eb72a268b ("PCI/ASPM: Use PCI Express Capability accessors")
Fixes: 7d715a6c1ae5 ("PCI: add PCI Express ASPM support")
Fixes: affa48de8417 ("staging/rdma/hfi1: Add support for enabling/disabling PCIe ASPM")
Fixes: 849a9366cba9 ("misc: rtsx: Add support new chip rts5228 mmc: rtsx: Add support MMC_CAP2_NO_MMC")
Fixes: 3d1e7aa80d1c ("misc: rtsx: Use pcie_capability_clear_and_set_word() for PCI_EXP_LNKCTL")
Fixes: c0e5f4e73a71 ("misc: rtsx: Add support for RTS5261")
Fixes: 3df4fce739e2 ("misc: rtsx: separate aspm mode into MODE_REG and MODE_CFG")
Fixes: 121e9c6b5c4c ("misc: rtsx: modify and fix init_hw function")
Fixes: 19f3bd548f27 ("mfd: rtsx: Remove LCTLR defination")
Fixes: 773ccdfd9cc6 ("mfd: rtsx: Read vendor setting from config space")
Fixes: 8275b77a1513 ("mfd: rts5249: Add support for RTS5250S power saving")
Fixes: 5da4e04ae480 ("misc: rtsx: Add support for RTS5260")
Fixes: 0f49bfbd0f2e ("tg3: Use PCI Express Capability accessors")
Fixes: 5e7dfd0fb94a ("tg3: Prevent corruption at 10 / 100Mbps w CLKREQ")
Fixes: b726e493e8dc ("r8169: sync existing 8168 device hardware start sequences with vendor driver")
Fixes: e6de30d63eb1 ("r8169: more 8168dp support.")
Fixes: 8a06127602de ("Bluetooth: hci_bcm4377: Add new driver for BCM4377 PCIe boards")
Fixes: 6f461f6c7c96 ("e1000e: enable/disable ASPM L0s and L1 and ERT according to hardware errata")
Fixes: 1eae4eb2a1c7 ("e1000e: Disable L1 ASPM power savings for 82573 mobile variants")
Fixes: 8060e169e02f ("ath9k: Enable extended synch for AR9485 to fix L0s recovery issue")
Fixes: 69ce674bfa69 ("ath9k: do btcoex ASPM disabling at initialization time")
Fixes: f37f05503575 ("mt76: mt76x2e: disable pcie_aspm by default")
Suggested-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Reviewed-by: Rafael J. Wysocki <rafael@kernel.org>
Cc: stable@vger.kernel.org
---
drivers/pci/access.c | 20 +++++++++++++++++---
drivers/pci/probe.c | 1 +
include/linux/pci.h | 34 ++++++++++++++++++++++++++++++++--
3 files changed, 50 insertions(+), 5 deletions(-)
diff --git a/drivers/pci/access.c b/drivers/pci/access.c
index 3c230ca3de58..0b2e90d2f04f 100644
--- a/drivers/pci/access.c
+++ b/drivers/pci/access.c
@@ -497,8 +497,8 @@ int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
}
EXPORT_SYMBOL(pcie_capability_write_dword);
-int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
- u16 clear, u16 set)
+int pcie_capability_clear_and_set_word_unlocked(struct pci_dev *dev, int pos,
+ u16 clear, u16 set)
{
int ret;
u16 val;
@@ -512,7 +512,21 @@ int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
return ret;
}
-EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
+EXPORT_SYMBOL(pcie_capability_clear_and_set_word_unlocked);
+
+int pcie_capability_clear_and_set_word_locked(struct pci_dev *dev, int pos,
+ u16 clear, u16 set)
+{
+ unsigned long flags;
+ int ret;
+
+ spin_lock_irqsave(&dev->pcie_cap_lock, flags);
+ ret = pcie_capability_clear_and_set_word_unlocked(dev, pos, clear, set);
+ spin_unlock_irqrestore(&dev->pcie_cap_lock, flags);
+
+ return ret;
+}
+EXPORT_SYMBOL(pcie_capability_clear_and_set_word_locked);
int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
u32 clear, u32 set)
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 8bac3ce02609..f1587fb0ba71 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -2324,6 +2324,7 @@ struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
.end = -1,
};
+ spin_lock_init(&dev->pcie_cap_lock);
#ifdef CONFIG_PCI_MSI
raw_spin_lock_init(&dev->msi_lock);
#endif
diff --git a/include/linux/pci.h b/include/linux/pci.h
index c69a2cc1f412..7ee498cd1f37 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -467,6 +467,7 @@ struct pci_dev {
pci_dev_flags_t dev_flags;
atomic_t enable_cnt; /* pci_enable_device has been called */
+ spinlock_t pcie_cap_lock; /* Protects RMW ops in capability accessors */
u32 saved_config_space[16]; /* Config space saved at suspend time */
struct hlist_head saved_cap_space;
int rom_attr_enabled; /* Display of ROM attribute enabled? */
@@ -1217,11 +1218,40 @@ int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val);
int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val);
int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val);
int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val);
-int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
- u16 clear, u16 set);
+int pcie_capability_clear_and_set_word_unlocked(struct pci_dev *dev, int pos,
+ u16 clear, u16 set);
+int pcie_capability_clear_and_set_word_locked(struct pci_dev *dev, int pos,
+ u16 clear, u16 set);
int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
u32 clear, u32 set);
+/**
+ * pcie_capability_clear_and_set_word - RMW accessor for PCI Express Capability Registers
+ * @dev: PCI device structure of the PCI Express device
+ * @pos: PCI Express Capability Register
+ * @clear: Clear bitmask
+ * @set: Set bitmask
+ *
+ * Perform a Read-Modify-Write (RMW) operation using @clear and @set
+ * bitmasks on PCI Express Capability Register at @pos. Certain PCI Express
+ * Capability Registers are accessed concurrently in RMW fashion, hence
+ * require locking which is handled transparently to the caller.
+ */
+static inline int pcie_capability_clear_and_set_word(struct pci_dev *dev,
+ int pos,
+ u16 clear, u16 set)
+{
+ switch (pos) {
+ case PCI_EXP_LNKCTL:
+ case PCI_EXP_RTCTL:
+ return pcie_capability_clear_and_set_word_locked(dev, pos,
+ clear, set);
+ default:
+ return pcie_capability_clear_and_set_word_unlocked(dev, pos,
+ clear, set);
+ }
+}
+
static inline int pcie_capability_set_word(struct pci_dev *dev, int pos,
u16 set)
{
--
2.30.2
next prev parent reply other threads:[~2023-07-17 12:05 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-17 12:04 [PATCH v5 00/11] PCI: Improve PCIe Capability RMW concurrency control Ilpo Järvinen
2023-07-17 12:04 ` Ilpo Järvinen [this message]
2023-07-17 12:04 ` [PATCH v5 02/11] PCI: Make link retraining use RMW accessors for changing LNKCTL Ilpo Järvinen
2023-07-17 12:04 ` [PATCH v5 03/11] PCI: pciehp: Use " Ilpo Järvinen
2023-07-17 12:04 ` [PATCH v5 04/11] PCI/ASPM: " Ilpo Järvinen
2023-07-17 12:04 ` [PATCH v5 05/11] drm/amdgpu: " Ilpo Järvinen
2023-07-20 21:55 ` Bjorn Helgaas
2023-07-21 8:07 ` Ilpo Järvinen
2023-07-21 14:52 ` Alex Deucher
2023-08-03 14:12 ` Ilpo Järvinen
2023-07-17 12:04 ` [PATCH v5 06/11] drm/radeon: " Ilpo Järvinen
2023-08-18 16:12 ` Deucher, Alexander
2023-08-21 9:57 ` Ilpo Järvinen
2023-08-21 19:12 ` Bjorn Helgaas
2023-07-17 12:04 ` [PATCH v5 07/11] net/mlx5: " Ilpo Järvinen
2023-07-17 12:05 ` [PATCH v5 08/11] wifi: ath11k: " Ilpo Järvinen
2023-07-17 12:05 ` [PATCH v5 09/11] wifi: ath12k: " Ilpo Järvinen
2023-07-17 12:05 ` [PATCH v5 10/11] wifi: ath10k: " Ilpo Järvinen
2023-07-17 12:05 ` [PATCH v5 11/11] PCI: Document the Capability accessor RMW improvements Ilpo Järvinen
2023-08-10 16:17 ` [PATCH v5 00/11] PCI: Improve PCIe Capability RMW concurrency control Bjorn Helgaas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230717120503.15276-2-ilpo.jarvinen@linux.intel.com \
--to=ilpo.jarvinen@linux.intel.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=ashutosh.dixit@intel.com \
--cc=auke-jan.h.kok@intel.com \
--cc=bhelgaas@google.com \
--cc=bristot@redhat.com \
--cc=bruce.w.allan@intel.com \
--cc=davem@davemloft.net \
--cc=dean.luick@cornelisnetworks.com \
--cc=dean.luick@intel.com \
--cc=dledford@redhat.com \
--cc=emmanuel.grumbach@intel.com \
--cc=gregkh@suse.de \
--cc=hkallweit1@gmail.com \
--cc=ira.weiny@intel.com \
--cc=jbarnes@virtuousgeek.org \
--cc=jeff@garzik.org \
--cc=jeffrey.t.kirsher@intel.com \
--cc=jiang.liu@huawei.com \
--cc=kvalo@kernel.org \
--cc=kw@linux.com \
--cc=lee@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linville@tuxdriver.com \
--cc=lorenzo.pieralisi@arm.com \
--cc=lorenzo@kernel.org \
--cc=luiz.von.dentz@intel.com \
--cc=lukas@wunner.de \
--cc=mcarlson@broadcom.com \
--cc=mchan@broadcom.com \
--cc=micky_ching@realsil.com.cn \
--cc=mike.marciniszyn@intel.com \
--cc=nbd@nbd.name \
--cc=rafael@kernel.org \
--cc=ricky_wu@realtek.com \
--cc=rjw@rjwysocki.net \
--cc=robh@kernel.org \
--cc=romieu@fr.zoreil.com \
--cc=rui_feng@realsil.com.cn \
--cc=sameo@linux.intel.com \
--cc=sgruszka@redhat.com \
--cc=shaohua.li@intel.com \
--cc=stable@vger.kernel.org \
--cc=sven@svenpeter.dev \
--cc=vasanth@atheros.com \
--cc=verdre@v0yd.nl \
--cc=wangyijing@huawei.com \
--cc=wei_wang@realsil.com.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).