git: 14fc33ea3a05 - main - LinuxKPI: Fix resource leak on pci_iounmap-ing of PCI BAR

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Sun, 21 Jul 2024 13:14:58 UTC
The branch main has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=14fc33ea3a0571ca70e609b6f9e67db39f7c6140

commit 14fc33ea3a0571ca70e609b6f9e67db39f7c6140
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2024-07-21 13:10:44 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2024-07-21 13:10:44 +0000

    LinuxKPI: Fix resource leak on pci_iounmap-ing of PCI BAR
    
    If the resource was allocated with want_iomap_res flag set.
    
    Sponsored by:   Serenity CyberSecurity, LLC
    MFC after:      1 week
    Reviewed by:    manu, bz
    Differential Revision:  https://reviews.freebsd.org/D45905
---
 sys/compat/linuxkpi/common/src/linux_pci.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c
index 3287a8a15ff5..3376d93053dc 100644
--- a/sys/compat/linuxkpi/common/src/linux_pci.c
+++ b/sys/compat/linuxkpi/common/src/linux_pci.c
@@ -780,12 +780,18 @@ void
 linuxkpi_pci_iounmap(struct pci_dev *pdev, void *res)
 {
 	struct pci_mmio_region *mmio, *p;
+	bus_space_handle_t bh = (bus_space_handle_t)res;
 
 	TAILQ_FOREACH_SAFE(mmio, &pdev->mmio, next, p) {
-		if ((bus_space_handle_t)res < rman_get_bushandle(mmio->res) ||
-		    (bus_space_handle_t)res >= rman_get_bushandle(mmio->res) +
-					       rman_get_size(mmio->res))
-			continue;
+		if (pdev->want_iomap_res) {
+			if (res != mmio->res)
+				continue;
+		} else {
+			if (bh <  rman_get_bushandle(mmio->res) ||
+			    bh >= rman_get_bushandle(mmio->res) +
+				  rman_get_size(mmio->res))
+				continue;
+		}
 		bus_release_resource(pdev->dev.bsddev,
 		    mmio->type, mmio->rid, mmio->res);
 		TAILQ_REMOVE(&pdev->mmio, mmio, next);