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 13B88CD4F26 for ; Fri, 19 Jun 2026 08:52:33 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4ghWYv0M97z3bt2; Fri, 19 Jun 2026 18:52:19 +1000 (AEST) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=172.105.4.254 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1781859139; cv=none; b=RazJ5yR7YAB0T8ZrgT3ixRW68h+WS9YslCModRPqfsWt/W91Q5KKMzEVy+XwLQ0zObest2ZK4dPKqBgUli/32O5nXZhaPz0MDfzfyLrPZUBrD/oLzFio+2rUnoi/kQpD1S4igBUo9t+rJc7AyZ/5NvNrzUqiQGZ6uRLxGKOO1eUsz0dPupGUs+VC/+Fwt3yOVdHhhpGuID55NZ3z0eFeWiNdGnfOgkv5XqeYkKuPigsi8Bkp6oUpIxVrw0bzf2ufHPiZwZvjLrH/58kpXb8cO+j3OJT/zyndovcLeFLYQR1auoLF11UtMCx6ewOTFJy6wWl1q30JTEfn1nRSiuM0ig== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1781859139; c=relaxed/relaxed; bh=/rWAb2PQyXpe51MYSLcCHAQKEqqJcKtSCl1xrrAhrYA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gM1e42TTvQLVONhT7NloRtdch6Qhpn6frVIUISGPU/7GwHq6mQ73B4xBNicGDo9uLToRpLXKSBG9vJO9NjO/jSdpGBzovXodeZO7UWFgkgyr2AoTKVjiYMirFfvbkBNa1sFXDCFTbEDIwr4EQU4DD9mdLYDDkNIF8mPEEKKhOikstFxc7rku4WTphCxtBhkL5aKtbXKw1YiH2eWD9F1AxIGVSv22vZodI906fhG1bPzvb1PN+R9KNwGjw7M2eZR6ilNw2YazJ3d0/PCx1LYp6qH4nZQ9PVdi/BC5jYnV3fLulTERbKNp72svOrYqo1GNbSa+ErGtoBWZluP0N9X2hQ== 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=k20260515 header.b=F27UngIs; dkim-atps=neutral; spf=pass (client-ip=172.105.4.254; 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=k20260515 header.b=F27UngIs; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=kernel.org (client-ip=172.105.4.254; helo=tor.source.kernel.org; envelope-from=kwilczynski@kernel.org; receiver=lists.ozlabs.org) Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) (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 4ghWYt2NZXz3bpp for ; Fri, 19 Jun 2026 18:52:18 +1000 (AEST) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 7252A601E2; Fri, 19 Jun 2026 08:52:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC8A31F00A3A; Fri, 19 Jun 2026 08:52:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1781859136; bh=/rWAb2PQyXpe51MYSLcCHAQKEqqJcKtSCl1xrrAhrYA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=F27UngIsTvsIm4LqQq2gAKx/i5q017coZwQ0kmUn31/jjMB06JkYSxzTbrofsdbpK uyf2lMNPANCCr+I9CP8jm5aMdulhdfiWvSZ5xVu1CAJSi3D7m+/ZVab4VAy4QkAm3c KVGw7xr31VDYxfaeLNc+RmKwHR9t1hTk5t5gy34Jibc/IQYvzp2oGo+s4H4f4TCcNw u4tolHV8/JirRcTM01bjWBLwB+FmlnaKgUUZ7SxAhwHYuLNHezpfZ5ZD94Za+miwpB 6/Dcw0ZERikvVbOL7KMph+EY/QB8OP7rdC7YKESPAiR7aCB6B7xITlHBp9QuD2MkZR hY2XMK91StQ3A== From: =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: Bjorn Helgaas , Manivannan Sadhasivam , Lorenzo Pieralisi , Alex Williamson , 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 v8 03/25] PCI/sysfs: Only allow supported resource types in I/O and MMIO helpers Date: Fri, 19 Jun 2026 08:51:38 +0000 Message-ID: <20260619085200.3729431-4-kwilczynski@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260619085200.3729431-1-kwilczynski@kernel.org> References: <20260619085200.3729431-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(). Tested-by: Shivaprasad G Bhat Reviewed-by: Ilpo Järvinen 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 1fbc3daf87cc..2e4e226e78d4 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1082,20 +1082,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); } @@ -1123,6 +1127,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.54.0