From mboxrd@z Thu Jan 1 00:00:00 1970 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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 730BDF36BA7 for ; Sat, 11 Apr 2026 08:02:21 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4ft5jq2LV0z2ynH; Sat, 11 Apr 2026 18:02:07 +1000 (AEST) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2600:3c04:e001:324:0:1991:8:25" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1775894527; cv=none; b=FKx9zPr7lX/tNsv/lkBVhSOVBuWI3JyMliQQCA4oPlZzxSoItP8tlv/VWzapUKTKTx6RvMWhVK87D1tIdjctvOS6OyO0Qdlebcd0Vkw37wlRCYX1hmEhELNjOyD7P044gkymfrXT2U+LeK8z1DS+vvDXxc0jGw1DLONsDRNDNQ9UTyRMTTj7ntX7H7lRz/12unDsxXTy91qljtf9c5MPe1V9wgm1P4tGpPvgsjxS2KLmKuxWKSgKgVR8tXIXxQl2PRB2/7t1PRwmikxEiyO1xQQY9tcSGn/CL4nUwdLw2umbdb8yyZWYXiHrPZkVMnjcTno+XU3cVNf1xbDpfcVbnw== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1775894527; c=relaxed/relaxed; bh=NdYwR9n+VwP0zMHvz0KdeCGbGqkgxQ7qttBog55x26g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ilct5tiNXfkGlFi/NsslJapgQhEy00rMxIiMdzYDC8SBUNUJuJ9K6aTe/3OswSbzeOd4qNwDTsa3jDYySF6v8x/P4aPgT/mPoZlUTehij7lCNKqB+RWUQAm6ME4NYaJWakDzYikeNVz9tU5JVPUX9+x4OGvCFh0GLUlb4ERQw9HK2WjNUB/6ts3mI/Wc7GZS+NcHxvHAns8J3NKoB6frHzAuuvgr3lmPfaRU58NZuVJq/4xkh9uNJmMBpCzFMqLDQJUZAPeTZXktJtX17ayRr4U8Xk6U86rfT3SUH64bgpzQi8rVQIiL4jGS2hUlWo+KdXXlKYqhWZSiJYD4HaLcfg== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=f0dNidQj; dkim-atps=neutral; spf=pass (client-ip=2600:3c04:e001:324:0:1991:8:25; helo=tor.source.kernel.org; envelope-from=kwilczynski@kernel.org; receiver=lists.ozlabs.org) smtp.mailfrom=kernel.org Authentication-Results: lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=f0dNidQj; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=kernel.org (client-ip=2600:3c04:e001:324:0:1991:8:25; helo=tor.source.kernel.org; envelope-from=kwilczynski@kernel.org; receiver=lists.ozlabs.org) Received: from tor.source.kernel.org (tor.source.kernel.org [IPv6:2600:3c04:e001:324:0:1991:8:25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4ft5jp3ksLz2ypW for ; Sat, 11 Apr 2026 18:02:06 +1000 (AEST) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 5E1C860145; Sat, 11 Apr 2026 08:02:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E1B4FC2BCAF; Sat, 11 Apr 2026 08:02:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775894524; bh=gvdpAvTWcmWTQ2Jvb4VCkVuJk3Ar+NokzIsq3HF2vFE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f0dNidQjV44Ue+vVjSEEnDjBkc8VTjDJSr8gWP4mgEne0S/0mHRvxG+2fvJNOfhvZ 17zqSgAj/9/8IGOyLsWO+8iTgzv7h8SXYYM4g4ooOK1h3j0pEkGaEOGMWcYb1RcEgo 321TfKyRs3FhrpvZfYpMb/bj/P63M/59GcLd7stTFG9jraLs2GPnYcx/hL5ZTV+fBU PW11SyYxDQGBKyq0NkQVGVQpgKnHYtwNWpRdSc1G/CWEn3IGQ4DCEWPAseyH9u2xA2 OuZikA0WNSZDuh9+Qyftvu+6gVplPZSkCtlvX8bDIc1D41c5d07iaP20X+cIuYn5Eg Eoi5/mo7ROo7A== From: =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: Bjorn Helgaas , Manivannan Sadhasivam , Lorenzo Pieralisi , Magnus Lindholm , Matt Turner , Richard Henderson , Christophe Leroy , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , Dexuan Cui , =?UTF-8?q?Krzysztof=20Ha=C5=82asa?= , Lukas Wunner , "Oliver O'Halloran" , Saurabh Singh Sengar , Shuan He , Srivatsa Bhat , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , linux-pci@vger.kernel.org, linux-alpha@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 03/24] PCI/sysfs: Only allow supported resource types in I/O and MMIO helpers Date: Sat, 11 Apr 2026 08:01:27 +0000 Message-ID: <20260411080148.471335-4-kwilczynski@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260411080148.471335-1-kwilczynski@kernel.org> References: <20260411080148.471335-1-kwilczynski@kernel.org> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, when the sysfs attributes for PCI resources are added dynamically, the resource access callbacks are only set when the underlying BAR type matches, using .read and .write for IORESOURCE_IO, and .mmap for IORESOURCE_MEM or IORESOURCE_IO with arch_can_pci_mmap_io() support. As such, when the callback is not set, the operation inherently fails. After the conversion to static attributes, visibility callbacks will control which resource files appear for each BAR, but the callbacks themselves will always be set. Thus, add a type check to pci_resource_io() and pci_mmap_resource() to return -EIO for an unsupported resource type. Use the new pci_resource_is_io() and pci_resource_is_mem() helpers for the type checks, replacing the open-coded bitwise flag tests and also drop the local struct resource pointer in pci_mmap_resource(). Signed-off-by: Krzysztof WilczyƄski --- drivers/pci/pci-sysfs.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index ad3c17f86c7f..6783c6168445 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1108,20 +1108,24 @@ static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *a struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); int bar = (unsigned long)attr->private; enum pci_mmap_state mmap_type; - struct resource *res = pci_resource_n(pdev, bar); int ret; ret = security_locked_down(LOCKDOWN_PCI_ACCESS); if (ret) return ret; - if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start)) + if (!pci_resource_is_mem(pdev, bar) && + !(pci_resource_is_io(pdev, bar) && arch_can_pci_mmap_io())) + return -EIO; + + if (pci_resource_is_mem(pdev, bar) && + iomem_is_exclusive(pci_resource_start(pdev, bar))) return -EINVAL; if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) return -EINVAL; - mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; + mmap_type = pci_resource_is_mem(pdev, bar) ? pci_mmap_mem : pci_mmap_io; return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine); } @@ -1149,6 +1153,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, int bar = (unsigned long)attr->private; unsigned long port = off; + if (!pci_resource_is_io(pdev, bar)) + return -EIO; + port += pci_resource_start(pdev, bar); if (port > pci_resource_end(pdev, bar)) -- 2.53.0