Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp9662785ybl; Fri, 17 Jan 2020 16:26:53 -0800 (PST) X-Google-Smtp-Source: APXvYqz/6jm3bspLRVPESVtKR2rgu4WnbCqf3heq9Uk6qyr/q92s91TiXyYrI3vCkQC0it/YKsLu X-Received: by 2002:aca:75cc:: with SMTP id q195mr5336050oic.178.1579307213299; Fri, 17 Jan 2020 16:26:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579307213; cv=none; d=google.com; s=arc-20160816; b=IQjSzvPXEqhfjdPeh3wxkOfQrgj0+Fvl7ZBRxH2PGHiCt3vQU/fWaPQftxzMgY0Sk/ rPChFYT3vGg+T7p6Mr4PxtEs81jhxaiQ9jxBy4CXR+epWD5aEiSSJOJQJ/FpJ9hDN1Xt iD5RfeGdFekO6XwC6m8Z26CMQ8vBkFgaGiAhnAXu2z8cF2U0vd+LX5u/f421CGabVzpL 8e+ojr938k47hZfp9JV3GZNStp2T+5kq8f1mgoN9GVXj7brLpZY9itBz1yif3Nk42r1M jY5uVrAVDrJqBC77GkGVjHioFPTTD4/o42YECfpFbAIhYBuoO1td//T+gLVf3hO3WJlS moMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=vWxirRR7Wfy2+PcTI0hB09FZtOC8LZoqLVZIg5Rnm1w=; b=Pzi+sl4B6l2WgX2RZyDXRp3IdX/oLAloMKMIY95nGoVK0ASz2kiuLoEfFvXTSvV2hL lGhQp96DR9q11YRJaTATbXpWiqyFIziYOJMpjbEq+nFKcfHGkl4cjfCZ/I2OcaRoEqIQ 8wTYDdDoqACO0B7BqvB2aqM6NLmqS7IWoo6Lx4pamCmszCWpH57h6iItlIvFHGMsuWZU xZ/YETuUDZxCpqu+ZPdMehXUkqaSjMkt7sMwWVurEoRcpNERR6/joCehiZp1LBg+IiEg ahGSPGIoWXQFj/wza7eUCeLBG+Ow8kh4AhM6xUcRI7dxvnYXEfcH+qF+H2RksehXYnCe e1hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=keR5G9ua; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v24si6839083otq.87.2020.01.17.16.26.39; Fri, 17 Jan 2020 16:26:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=keR5G9ua; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730422AbgARAZr (ORCPT + 99 others); Fri, 17 Jan 2020 19:25:47 -0500 Received: from mail-pj1-f68.google.com ([209.85.216.68]:39859 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730358AbgARAZq (ORCPT ); Fri, 17 Jan 2020 19:25:46 -0500 Received: by mail-pj1-f68.google.com with SMTP id e11so4075442pjt.4 for ; Fri, 17 Jan 2020 16:25:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vWxirRR7Wfy2+PcTI0hB09FZtOC8LZoqLVZIg5Rnm1w=; b=keR5G9uaZyDKFwyyJfHkvYRIfOR1MoqFTmzm6I/pcaGvIGLBRhvXtIXXMyq0QV+9ft mG3A66ewUhY6vWPdVRIY6lgW1+993WgUmn1NtpThipmkWt7gNeF4qtTABYABLNHYHsnG w/broOmqKVddFYyItFNPg/8rRgL28SYNRX6U8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vWxirRR7Wfy2+PcTI0hB09FZtOC8LZoqLVZIg5Rnm1w=; b=d7bNaRQee/+BS9tnmJfp/Phzqm13bM7nw9gpJwPrRefQyNjaQOr8nEDslsDIl6ruE8 9bgMEYxbxiZfwzFkAhIF62+AaoiRa43/WeziSHI56UPfSNfujjjXf9Xx8SzXYvuuFTXM subsQ4y031fLbm3nBNty2PGRFdlUN570DMFA3zCCZKEkldZhGJMYh0BEA+3+0lWrjPBR Zqy3i/gl9f156fL++rYv3n912N3qR0qPD+fiK1I3Ny1OSmWJ3GPZ6pMNEjd+DapPabMm SgTw3OVTHTcm/Xoeep1SX/FET8aOfP0/7tSZCmeDxsMr3KHGCqJszcxvAwM9XYkILf20 RR6g== X-Gm-Message-State: APjAAAV8V1P1ruUwBfkVPV/shLC5ByXiBbhWF9g0KQf0dA3aQuDePOVj euk85dM41mzRuIiiI6VeVlPm3Q== X-Received: by 2002:a17:90a:8b82:: with SMTP id z2mr9302343pjn.59.1579307146062; Fri, 17 Jan 2020 16:25:46 -0800 (PST) Received: from evgreen2.mtv.corp.google.com ([2620:15c:202:201:ffda:7716:9afc:1301]) by smtp.gmail.com with ESMTPSA id 199sm32194947pfv.81.2020.01.17.16.25.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 17 Jan 2020 16:25:45 -0800 (PST) From: Evan Green To: Bjorn Helgaas Cc: Evan Green , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] PCI/MSI: Avoid torn updates to MSI pairs Date: Fri, 17 Jan 2020 16:25:30 -0800 Message-Id: <20200117162444.v2.1.I9c7e72144ef639cc135ea33ef332852a6b33730f@changeid> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org __pci_write_msi_msg() updates three registers in the device: address high, address low, and data. On x86 systems, address low contains CPU targeting info, and data contains the vector. The order of writes is address, then data. This is problematic if an interrupt comes in after address has been written, but before data is updated, and both the SMP affinity and target vector are being changed. In this case, the interrupt targets the wrong vector on the new CPU. This case is pretty easy to stumble into using xhci and CPU hotplugging. Create a script that repeatedly targets interrupts at a set of cores and then offlines those cores. Put some stress on USB, and then watch xhci lose an interrupt and die. Avoid this by disabling MSIs during the update. Signed-off-by: Evan Green --- Changes in v2: - Also mask msi-x interrupts during the update - Restore the enable/mask bit to its previous value, rather than unconditionally enabling interrupts Bjorn, I was unsure whether disabling MSIs temporarily is actually an okay thing to do. I considered using the mask bit, but got the impression that not all devices support the mask bit. Let me know if this going to cause problems or there's a better way. I can include the repro script I used to cause mayhem if needed. --- drivers/pci/msi.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/pci/msi.c b/drivers/pci/msi.c index 6b43a5455c7af..bb21a7739fa2c 100644 --- a/drivers/pci/msi.c +++ b/drivers/pci/msi.c @@ -311,6 +311,7 @@ void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg) void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg) { struct pci_dev *dev = msi_desc_to_pci_dev(entry); + u16 msgctl; if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) { /* Don't touch the hardware now */ @@ -320,15 +321,25 @@ void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg) if (!base) goto skip; + pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, + &msgctl); + + pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, + msgctl | PCI_MSIX_FLAGS_MASKALL); + writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR); writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR); writel(msg->data, base + PCI_MSIX_ENTRY_DATA); + pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, + msgctl); + } else { int pos = dev->msi_cap; - u16 msgctl; + u16 enabled; pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl); - msgctl &= ~PCI_MSI_FLAGS_QSIZE; + enabled = msgctl & PCI_MSI_FLAGS_ENABLE; + msgctl &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE); msgctl |= entry->msi_attrib.multiple << 4; pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl); @@ -343,6 +354,9 @@ void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg) pci_write_config_word(dev, pos + PCI_MSI_DATA_32, msg->data); } + + msgctl |= enabled; + pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl); } skip: -- 2.24.1