From patchwork Tue Nov 26 09:09:53 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Gordeev X-Patchwork-Id: 3241211 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CC12F9F3B8 for ; Tue, 26 Nov 2013 20:51:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A699A20445 for ; Tue, 26 Nov 2013 20:51:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 88F0C2041A for ; Tue, 26 Nov 2013 20:51:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753067Ab3KZUvE (ORCPT ); Tue, 26 Nov 2013 15:51:04 -0500 Received: from 6.77.broadband6.iol.cz ([88.101.77.6]:52468 "EHLO dhcp-26-207.brq.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752492Ab3KZUvD (ORCPT ); Tue, 26 Nov 2013 15:51:03 -0500 Received: from dhcp-26-207.brq.redhat.com (localhost [127.0.0.1]) by dhcp-26-207.brq.redhat.com (8.14.5/8.14.5) with ESMTP id rAQ9AXCT025349; Tue, 26 Nov 2013 10:10:33 +0100 Received: (from agordeev@localhost) by dhcp-26-207.brq.redhat.com (8.14.5/8.14.5/Submit) id rAQ9AWhB025348; Tue, 26 Nov 2013 10:10:32 +0100 From: Alexander Gordeev To: linux-kernel@vger.kernel.org Cc: Alexander Gordeev , Bjorn Helgaas , Michael Ellerman , Benjamin Herrenschmidt , Tejun Heo , Ben Hutchings , David Laight , Mark Lord , "H. Peter Anvin" , linux-pci@vger.kernel.org Subject: [PATCH v3 04/11] PCI/MSI/pSeries: Make quota traversing and requesting race-safe Date: Tue, 26 Nov 2013 10:09:53 +0100 Message-Id: <97749db3e9562eb05fb3ee22a0be0ed7a93a0615.1385399393.git.agordeev@redhat.com> X-Mailer: git-send-email 1.7.7.6 In-Reply-To: References: Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The current MSI quota handling is not race-safe and might lead to incoherent number of MSIs allocated between the firmware and Linux MSI data structures. I.e. if the following chain is called from concurrently loading drivers: rtas_setup_msi_irqs() -> msi_quota_for_device() -> traverse_pci_devices() a driver might get a stalled value of MSI limit for its device or possibly even crash. This update introduces "rtas_quota_mutex" and serializes all accesses to msi_quota_for_device() function. As result, no driver could eat into other device's MSI limit. Signed-off-by: Alexander Gordeev --- arch/powerpc/platforms/pseries/msi.c | 24 ++++++++++++++++++++++-- 1 files changed, 22 insertions(+), 2 deletions(-) diff --git a/arch/powerpc/platforms/pseries/msi.c b/arch/powerpc/platforms/pseries/msi.c index 009ec73..0e1d288 100644 --- a/arch/powerpc/platforms/pseries/msi.c +++ b/arch/powerpc/platforms/pseries/msi.c @@ -26,6 +26,8 @@ static int query_token, change_token; #define RTAS_CHANGE_MSIX_FN 4 #define RTAS_CHANGE_32MSI_FN 5 +static DEFINE_MUTEX(rtas_quota_mutex); + /* RTAS Helpers */ static int rtas_change_msi(struct pci_dn *pdn, u32 func, u32 num_irqs) @@ -345,7 +347,9 @@ static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type) if (rc) return rc; + mutex_lock(&rtas_quota_mutex); quota = msi_quota_for_device(pdev, nvec); + mutex_unlock(&rtas_quota_mutex); if (quota && quota < nvec) return quota; @@ -399,6 +403,7 @@ static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec_in, int type) struct msi_msg msg; int nvec = nvec_in; int use_32bit_msi_hack = 0; + int quota; pdn = pci_get_pdn(pdev); if (!pdn) @@ -407,13 +412,21 @@ static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec_in, int type) if (type == PCI_CAP_ID_MSIX && check_msix_entries(pdev)) return -EINVAL; + mutex_lock(&rtas_quota_mutex); + + quota = msi_quota_for_device(pdev, nvec); + if (quota && quota < nvec) { + mutex_unlock(&rtas_quota_mutex); + return quota; + } + /* * Firmware currently refuse any non power of two allocation * so we round up if the quota will allow it. */ if (type == PCI_CAP_ID_MSIX) { int m = roundup_pow_of_two(nvec); - int quota = msi_quota_for_device(pdev, m); + quota = msi_quota_for_device(pdev, m); if (quota >= m) nvec = m; @@ -433,8 +446,11 @@ again: * We only want to run the 32 bit MSI hack below if * the max bus speed is Gen2 speed */ - if (pdev->bus->max_bus_speed != PCIE_SPEED_5_0GT) + if (pdev->bus->max_bus_speed != + PCIE_SPEED_5_0GT) { + mutex_unlock(&rtas_quota_mutex); return rc; + } use_32bit_msi_hack = 1; } @@ -459,6 +475,7 @@ again: nvec = nvec_in; goto again; } + mutex_unlock(&rtas_quota_mutex); pr_debug("rtas_msi: rtas_change_msi() failed\n"); return rc; } @@ -467,6 +484,7 @@ again: list_for_each_entry(entry, &pdev->msi_list, list) { hwirq = rtas_query_irq_number(pdn, i++); if (hwirq < 0) { + mutex_unlock(&rtas_quota_mutex); pr_debug("rtas_msi: error (%d) getting hwirq\n", hwirq); return hwirq; } @@ -474,6 +492,7 @@ again: virq = irq_create_mapping(NULL, hwirq); if (virq == NO_IRQ) { + mutex_unlock(&rtas_quota_mutex); pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq); return -ENOSPC; } @@ -486,6 +505,7 @@ again: entry->msg = msg; } + mutex_unlock(&rtas_quota_mutex); return 0; }