From nobody Fri Apr 17 10:29:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B47C5C433FE for ; Thu, 17 Nov 2022 15:08:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240233AbiKQPIm (ORCPT ); Thu, 17 Nov 2022 10:08:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240123AbiKQPIB (ORCPT ); Thu, 17 Nov 2022 10:08:01 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CA1C74CD3; Thu, 17 Nov 2022 07:08:00 -0800 (PST) Date: Thu, 17 Nov 2022 15:07:57 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668697678; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gNMZbiEYQH9X4IQCzLRwhDqmPwiMGrE/kmQ0eUwzr5Q=; b=ZTHUUbWZlxpfHhDzESjRF5g4LMT8jhwxTgSjrqSpiVooUC6hTykT7mXg/PaCtsHFBaEhJp E7wgysyMlMYVKvSpvwq5SfC9yeMaCD6QFl9/frhjJluKnOtVU6/LYTTbUszRavi2Kj7OeT 1iie5SlTde10skY+w2XNi5j0Uj8zRggQe7rqd13rsRPn7g2Cb5q7PR/UBs3j7G96AlbwvO +7FXkt3zeEg3KnyKxBX5XaJMRPip0GKCyjQ1LyI09uInxRaHs7gdb1VrIP/F0R31JEYRqq cBZMdJ5IkCu0eHQR8p4y/mr12doJN6lJKePafugf7Fv8uV229BPxaeR3C/j2ug== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668697678; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gNMZbiEYQH9X4IQCzLRwhDqmPwiMGrE/kmQ0eUwzr5Q=; b=WZf+X5rALrAG7TWFYECZr+LLv59kFf/1c2UhjNIPYgMJRxwdI+cxuK3TtQenCQO9H4J6Ge 9LjlLWpzdsl+EPBQ== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: irq/core] PCI/MSI: Sanitize MSI-X checks Cc: Thomas Gleixner , Jason Gunthorpe , Bjorn Helgaas , x86@kernel.org, linux-kernel@vger.kernel.org, maz@kernel.org In-Reply-To: <20221111122015.516946468@linutronix.de> References: <20221111122015.516946468@linutronix.de> MIME-Version: 1.0 Message-ID: <166869767748.4906.3113945352157433278.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the irq/core branch of tip: Commit-ID: bab65e48cb064d67b488efb6888c06fd977a8245 Gitweb: https://git.kernel.org/tip/bab65e48cb064d67b488efb6888c06fd9= 77a8245 Author: Thomas Gleixner AuthorDate: Fri, 11 Nov 2022 14:55:07 +01:00 Committer: Thomas Gleixner CommitterDate: Thu, 17 Nov 2022 15:15:22 +01:00 PCI/MSI: Sanitize MSI-X checks There is no point in doing the same sanity checks over and over in a loop during MSI-X enablement. Put them in front of the loop and return early when they fail. Signed-off-by: Thomas Gleixner Reviewed-by: Jason Gunthorpe Acked-by: Bjorn Helgaas Link: https://lore.kernel.org/r/20221111122015.516946468@linutronix.de --- drivers/pci/msi/msi.c | 65 +++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/drivers/pci/msi/msi.c b/drivers/pci/msi/msi.c index 380e651..794ec97 100644 --- a/drivers/pci/msi/msi.c +++ b/drivers/pci/msi/msi.c @@ -721,47 +721,31 @@ out_disable: return ret; } =20 -static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entri= es, - int nvec, struct irq_affinity *affd, int flags) +static bool pci_msix_validate_entries(struct msix_entry *entries, int nvec= , int hwsize) { - int nr_entries; int i, j; =20 - if (!pci_msi_supported(dev, nvec) || dev->current_state !=3D PCI_D0) - return -EINVAL; + if (!entries) + return true; =20 - nr_entries =3D pci_msix_vec_count(dev); - if (nr_entries < 0) - return nr_entries; - if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL)) - return nr_entries; + for (i =3D 0; i < nvec; i++) { + /* Entry within hardware limit? */ + if (entries[i].entry >=3D hwsize) + return false; =20 - if (entries) { - /* Check for any invalid entries */ - for (i =3D 0; i < nvec; i++) { - if (entries[i].entry >=3D nr_entries) - return -EINVAL; /* invalid entry */ - for (j =3D i + 1; j < nvec; j++) { - if (entries[i].entry =3D=3D entries[j].entry) - return -EINVAL; /* duplicate entry */ - } + /* Check for duplicate entries */ + for (j =3D i + 1; j < nvec; j++) { + if (entries[i].entry =3D=3D entries[j].entry) + return false; } } - - /* Check whether driver already requested for MSI IRQ */ - if (dev->msi_enabled) { - pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n"); - return -EINVAL; - } - return msix_capability_init(dev, entries, nvec, affd); + return true; } =20 -int __pci_enable_msix_range(struct pci_dev *dev, - struct msix_entry *entries, int minvec, - int maxvec, struct irq_affinity *affd, - int flags) +int __pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entrie= s, int minvec, + int maxvec, struct irq_affinity *affd, int flags) { - int rc, nvec =3D maxvec; + int hwsize, rc, nvec =3D maxvec; =20 if (maxvec < minvec) return -ERANGE; @@ -774,6 +758,23 @@ int __pci_enable_msix_range(struct pci_dev *dev, if (WARN_ON_ONCE(dev->msix_enabled)) return -EINVAL; =20 + if (!pci_msi_supported(dev, nvec) || dev->current_state !=3D PCI_D0) + return -EINVAL; + + hwsize =3D pci_msix_vec_count(dev); + if (hwsize < 0) + return hwsize; + + if (!pci_msix_validate_entries(entries, nvec, hwsize)) + return -EINVAL; + + /* PCI_IRQ_VIRTUAL is a horrible hack! */ + if (nvec > hwsize && !(flags & PCI_IRQ_VIRTUAL)) + nvec =3D hwsize; + + if (nvec < minvec) + return -ENOSPC; + rc =3D pci_setup_msi_context(dev); if (rc) return rc; @@ -785,7 +786,7 @@ int __pci_enable_msix_range(struct pci_dev *dev, return -ENOSPC; } =20 - rc =3D __pci_enable_msix(dev, entries, nvec, affd, flags); + rc =3D msix_capability_init(dev, entries, nvec, affd); if (rc =3D=3D 0) return nvec;