linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Marc Zyngier <maz@kernel.org>,
	Bjorn Helgaas <bhelgaas@google.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: [PATCH 4.19 36/84] PCI/MSI: Mask all unused MSI-X entries
Date: Tue, 24 Aug 2021 13:02:02 -0400	[thread overview]
Message-ID: <20210824170250.710392-37-sashal@kernel.org> (raw)
In-Reply-To: <20210824170250.710392-1-sashal@kernel.org>

From: Thomas Gleixner <tglx@linutronix.de>

commit 7d5ec3d3612396dc6d4b76366d20ab9fc06f399f upstream.

When MSI-X is enabled the ordering of calls is:

  msix_map_region();
  msix_setup_entries();
  pci_msi_setup_msi_irqs();
  msix_program_entries();

This has a few interesting issues:

 1) msix_setup_entries() allocates the MSI descriptors and initializes them
    except for the msi_desc:masked member which is left zero initialized.

 2) pci_msi_setup_msi_irqs() allocates the interrupt descriptors and sets
    up the MSI interrupts which ends up in pci_write_msi_msg() unless the
    interrupt chip provides its own irq_write_msi_msg() function.

 3) msix_program_entries() does not do what the name suggests. It solely
    updates the entries array (if not NULL) and initializes the masked
    member for each MSI descriptor by reading the hardware state and then
    masks the entry.

Obviously this has some issues:

 1) The uninitialized masked member of msi_desc prevents the enforcement
    of masking the entry in pci_write_msi_msg() depending on the cached
    masked bit. Aside of that half initialized data is a NONO in general

 2) msix_program_entries() only ensures that the actually allocated entries
    are masked. This is wrong as experimentation with crash testing and
    crash kernel kexec has shown.

    This limited testing unearthed that when the production kernel had more
    entries in use and unmasked when it crashed and the crash kernel
    allocated a smaller amount of entries, then a full scan of all entries
    found unmasked entries which were in use in the production kernel.

    This is obviously a device or emulation issue as the device reset
    should mask all MSI-X table entries, but obviously that's just part
    of the paper specification.

Cure this by:

 1) Masking all table entries in hardware
 2) Initializing msi_desc::masked in msix_setup_entries()
 3) Removing the mask dance in msix_program_entries()
 4) Renaming msix_program_entries() to msix_update_entries() to
    reflect the purpose of that function.

As the masking of unused entries has never been done the Fixes tag refers
to a commit in:
   git://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git

Fixes: f036d4ea5fa7 ("[PATCH] ia32 Message Signalled Interrupt support")
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Marc Zyngier <maz@kernel.org>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20210729222542.403833459@linutronix.de
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/pci/msi.c | 40 ++++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/drivers/pci/msi.c b/drivers/pci/msi.c
index a9cbc301a8a6..d13b8b608891 100644
--- a/drivers/pci/msi.c
+++ b/drivers/pci/msi.c
@@ -675,6 +675,7 @@ static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
 {
 	struct cpumask *curmsk, *masks = NULL;
 	struct msi_desc *entry;
+	void __iomem *addr;
 	int ret, i;
 
 	if (affd)
@@ -694,6 +695,7 @@ static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
 
 		entry->msi_attrib.is_msix	= 1;
 		entry->msi_attrib.is_64		= 1;
+
 		if (entries)
 			entry->msi_attrib.entry_nr = entries[i].entry;
 		else
@@ -701,6 +703,10 @@ static int msix_setup_entries(struct pci_dev *dev, void __iomem *base,
 		entry->msi_attrib.default_irq	= dev->irq;
 		entry->mask_base		= base;
 
+		addr = pci_msix_desc_addr(entry);
+		if (addr)
+			entry->masked = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
+
 		list_add_tail(&entry->list, dev_to_msi_list(&dev->dev));
 		if (masks)
 			curmsk++;
@@ -711,21 +717,27 @@ out:
 	return ret;
 }
 
-static void msix_program_entries(struct pci_dev *dev,
-				 struct msix_entry *entries)
+static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
 {
 	struct msi_desc *entry;
-	int i = 0;
 
 	for_each_pci_msi_entry(entry, dev) {
-		if (entries)
-			entries[i++].vector = entry->irq;
-		entry->masked = readl(pci_msix_desc_addr(entry) +
-				PCI_MSIX_ENTRY_VECTOR_CTRL);
-		msix_mask_irq(entry, 1);
+		if (entries) {
+			entries->vector = entry->irq;
+			entries++;
+		}
 	}
 }
 
+static void msix_mask_all(void __iomem *base, int tsize)
+{
+	u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
+	int i;
+
+	for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
+		writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
+}
+
 /**
  * msix_capability_init - configure device's MSI-X capability
  * @dev: pointer to the pci_dev data structure of MSI-X device function
@@ -740,9 +752,9 @@ static void msix_program_entries(struct pci_dev *dev,
 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
 				int nvec, const struct irq_affinity *affd)
 {
-	int ret;
-	u16 control;
 	void __iomem *base;
+	int ret, tsize;
+	u16 control;
 
 	/*
 	 * Some devices require MSI-X to be enabled before the MSI-X
@@ -754,12 +766,16 @@ static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
 
 	pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
 	/* Request & Map MSI-X table region */
-	base = msix_map_region(dev, msix_table_size(control));
+	tsize = msix_table_size(control);
+	base = msix_map_region(dev, tsize);
 	if (!base) {
 		ret = -ENOMEM;
 		goto out_disable;
 	}
 
+	/* Ensure that all table entries are masked. */
+	msix_mask_all(base, tsize);
+
 	ret = msix_setup_entries(dev, base, entries, nvec, affd);
 	if (ret)
 		goto out_disable;
@@ -773,7 +789,7 @@ static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
 	if (ret)
 		goto out_free;
 
-	msix_program_entries(dev, entries);
+	msix_update_entries(dev, entries);
 
 	ret = populate_msi_sysfs(dev);
 	if (ret)
-- 
2.30.2


  parent reply	other threads:[~2021-08-24 17:25 UTC|newest]

Thread overview: 91+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-24 17:01 [PATCH 4.19 00/84] 4.19.205-rc1 review Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 01/84] iio: humidity: hdc100x: Add margin to the conversion time Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 02/84] iio: adc: Fix incorrect exit of for-loop Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 03/84] ASoC: intel: atom: Fix reference to PCM buffer address Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 04/84] i2c: dev: zero out array used for i2c reads from userspace Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 05/84] ACPI: NFIT: Fix support for virtual SPA ranges Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 06/84] ieee802154: hwsim: fix GPF in hwsim_set_edge_lqi Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 07/84] ieee802154: hwsim: fix GPF in hwsim_new_edge_nl Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 08/84] ASoC: cs42l42: Correct definition of ADC Volume control Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 09/84] ASoC: cs42l42: Don't allow SND_SOC_DAIFMT_LEFT_J Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 10/84] ASoC: cs42l42: Fix inversion of ADC Notch Switch control Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 11/84] ASoC: cs42l42: Remove duplicate control for WNF filter frequency Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 12/84] ASoC: cs42l42: Fix LRCLK frame start edge Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 13/84] net: dsa: mt7530: add the missing RxUnicast MIB counter Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 14/84] ppp: Fix generating ifname when empty IFLA_IFNAME is specified Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 15/84] psample: Add a fwd declaration for skbuff Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 16/84] net: Fix memory leak in ieee802154_raw_deliver Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 17/84] net: igmp: fix data-race in igmp_ifc_timer_expire() Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 18/84] net: dsa: lan9303: fix broken backpressure in .port_fdb_dump Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 19/84] net: bridge: fix memleak in br_add_if() Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 20/84] tcp_bbr: fix u32 wrap bug in round logic if bbr_init() called after 2B packets Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 21/84] net: igmp: increase size of mr_ifc_count Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 22/84] xen/events: Fix race in set_evtchn_to_irq Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 23/84] vsock/virtio: avoid potential deadlock when vsock device remove Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 24/84] powerpc/kprobes: Fix kprobe Oops happens in booke Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 25/84] x86/tools: Fix objdump version check again Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 26/84] genirq: Provide IRQCHIP_AFFINITY_PRE_STARTUP Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 27/84] x86/msi: Force affinity setup before startup Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 28/84] x86/ioapic: " Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 29/84] x86/resctrl: Fix default monitoring groups reporting Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 30/84] genirq/msi: Ensure deactivation on teardown Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 31/84] PCI/MSI: Enable and mask MSI-X early Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 32/84] PCI/MSI: Do not set invalid bits in MSI mask Sasha Levin
2021-08-24 17:01 ` [PATCH 4.19 33/84] PCI/MSI: Correct misleading comments Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 34/84] PCI/MSI: Use msi_mask_irq() in pci_msi_shutdown() Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 35/84] PCI/MSI: Protect msi_desc::masked for multi-MSI Sasha Levin
2021-08-24 17:02 ` Sasha Levin [this message]
2021-08-24 17:02 ` [PATCH 4.19 37/84] PCI/MSI: Enforce that MSI-X table entry is masked for update Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 38/84] PCI/MSI: Enforce MSI[X] entry updates to be visible Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 39/84] vmlinux.lds.h: Handle clang's module.{c,d}tor sections Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 40/84] iommu/vt-d: Fix agaw for a supported 48 bit guest address width Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 41/84] mac80211: drop data frames without key on encrypted links Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 42/84] KVM: nSVM: always intercept VMLOAD/VMSAVE when nested (CVE-2021-3656) Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 43/84] KVM: nSVM: avoid picking up unsupported bits from L2 in int_ctl (CVE-2021-3653) Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 44/84] x86/fpu: Make init_fpstate correct with optimized XSAVE Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 45/84] ath: Use safer key clearing with key cache entries Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 46/84] ath9k: Clear key cache explicitly on disabling hardware Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 47/84] ath: Export ath_hw_keysetmac() Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 48/84] ath: Modify ath_key_delete() to not need full key entry Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 49/84] ath9k: Postpone key cache entry deletion for TXQ frames reference it Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 50/84] dmaengine: xilinx_dma: Fix read-after-free bug when terminating transfers Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 51/84] dmaengine: usb-dmac: Fix PM reference leak in usb_dmac_probe() Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 52/84] ARM: dts: am43x-epos-evm: Reduce i2c0 bus speed for tps65218 Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 53/84] dmaengine: of-dma: router_xlate to return -EPROBE_DEFER if controller is not yet available Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 54/84] scsi: megaraid_mm: Fix end of loop tests for list_for_each_entry() Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 55/84] scsi: scsi_dh_rdac: Avoid crash during rdac_bus_attach() Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 56/84] scsi: core: Avoid printing an error if target_alloc() returns -ENXIO Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 57/84] ARM: dts: nomadik: Fix up interrupt controller node names Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 58/84] net: usb: lan78xx: don't modify phy_device state concurrently Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 59/84] Bluetooth: hidp: use correct wait queue when removing ctrl_wait Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 60/84] cpufreq: armada-37xx: forbid cpufreq for 1.2 GHz variant Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 61/84] dccp: add do-while-0 stubs for dccp_pr_debug macros Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 62/84] vhost: Fix the calculation in vhost_overflow() Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 63/84] bnxt: don't lock the tx queue from napi poll Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 64/84] bnxt: disable napi before canceling DIM Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 65/84] net: 6pack: fix slab-out-of-bounds in decode_data Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 66/84] ptp_pch: Restore dependency on PCI Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 67/84] net: qlcnic: add missed unlock in qlcnic_83xx_flash_read32 Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 68/84] net: mdio-mux: Don't ignore memory allocation errors Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 69/84] net: mdio-mux: Handle -EPROBE_DEFER correctly Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 70/84] mmc: dw_mmc: Fix hang on data CRC error Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 71/84] ALSA: hda - fix the 'Capture Switch' value change notifications Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 72/84] tracing / histogram: Fix NULL pointer dereference on strcmp() on NULL event name Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 73/84] slimbus: messaging: start transaction ids from 1 instead of zero Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 74/84] slimbus: messaging: check for valid transaction id Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 75/84] slimbus: ngd: reset dma setup during runtime pm Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 76/84] ipack: tpci200: fix many double free issues in tpci200_pci_probe Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 77/84] ipack: tpci200: fix memory leak in the tpci200_register Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 78/84] btrfs: prevent rename2 from exchanging a subvol with a directory from different parents Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 79/84] PCI: Increase D3 delay for AMD Renoir/Cezanne XHCI Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 80/84] ASoC: intel: atom: Fix breakage for PCM buffer address setup Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 81/84] locks: print a warning when mount fails due to lack of "mand" support Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 82/84] fs: warn about impending deprecation of mandatory locks Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 83/84] netfilter: nft_exthdr: fix endianness of tcp option cast Sasha Levin
2021-08-24 17:02 ` [PATCH 4.19 84/84] Linux 4.19.205-rc1 Sasha Levin
2021-08-25  7:34 ` [PATCH 4.19 00/84] 4.19.205-rc1 review Pavel Machek
2021-08-25 12:57 ` Sudip Mukherjee
2021-08-25 20:25 ` Guenter Roeck
2021-08-25 21:01 ` Daniel Díaz
2021-08-25 22:36 ` Shuah Khan
2021-08-26  1:01 ` Samuel Zou

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=20210824170250.710392-37-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=bhelgaas@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /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).