From nobody Sun Oct 11 13:51:31 2026 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4j2hpW4hFDz6vrK7 for ; Sun, 11 Oct 2026 13:51:31 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4j2hpW3qrfz4pM6 for ; Sun, 11 Oct 2026 13:51:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791726691; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ezRqYnqVMU9DDK5OsjeKQp9PdePiGERpabB+plCY3Y4=; b=bc9ajRq3F8+rErsF/STxbF8y8r/9RPoeb2mXv1BPRpbs78RFnd7OAMf969U0XJlJam7mG5 d5E4KF4kAHGv0gXFh5zndFS8yuLh4OagGe6255ENBMxujp/qEw1HTAjrnPewpfp0dyZeho aowxkleuqSCxokXIeMhCPHrGKbgffX01rJMQQaUPUwWI6734RYj3G1qApR5w0rDIEbNbtW k+rhihPUtMP0FP5FDNshB8ra7BkKdVoabHBDp4cpQHN0DHRR/5/f7gu8O8pCNZvrRI4rvr dFhuhtwZu4scwrOiSq1L/TlWDCVq0nHAmsIcqOls/yXSKENINyTDjBvV3jf2AQ== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1791726691; b=x/kGZ6RaBWmQfsJI0XSSxRZn9ANvLMUHVeYk5llmonu4VUFMm5Lx3atY8L4k2n05n+iDTV tquR8JWtlspMSgfYUVHUDpO+3oHYhojkSoLJPqPaNauKPcN7OBYfHx8HQ+sdg1APR7k0b+ 2Tph4i5vWB+iWtA9nlyBSJC+tuNQ1n+4xFT08FBmAvtdG12vyD7CIuEl3WT1grZLLN7XNz FQ0pC0leJdVeoVLLJ8Z8g/9WvSjfG82clnVs4qI+4E4Gb1OdM/J3f6dGPdkQKGEnlCMIU/ mgkxZc5U1ql1waMgnODVpSpS5/QrSkSU+P9oYMkwNXqCf0AFGQ/VuPw21mQgHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791726691; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ezRqYnqVMU9DDK5OsjeKQp9PdePiGERpabB+plCY3Y4=; b=mSor6de8zeJyJPhByqa6KqadjoYj3DJZLEGANCf2TB9CLpV0T5mu8bkVor+/Rt1BbRg2ZK gwl7PMiUmFLf1sFczvh4N8QYaN3xbAJU869VbwxPL+A9Y/1CPKk+vB8w07JJMdD6q4/YwN GdBbFn3cQt7MGP7S9T53+XB5mMCNvEfshemou4OOtCQomrci6vveHPcaIdkrDIRyNQ3Oqf +cHNLZ+7QI/BOYCHKZZ6b+EqyLTEYhBpiXWP42d4hb6vf1AVs1RfuZBzHY8Z6vB2YkmE/9 jSka67WtfEdmXvn2LhXf4ewl5W61/VJDcaO/glsqdCjMs6fBf17UHaqbhnKPYw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4j2hpW2txtzkyP for ; Sun, 11 Oct 2026 13:51:31 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 37185 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sun, 11 Oct 2026 13:51:31 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Bjoern A. Zeeb Subject: git: 9d82546ac2de - main - LinuxKPI: pci: cleanup lkpinew_pci_dev_release() List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: Commit messages for the main branch of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 9d82546ac2def568ff2f55c63cd77c8d8ff959e2 Auto-Submitted: auto-generated Date: Sun, 11 Oct 2026 13:51:31 +0000 Message-Id: <6acb9463.37185.19f89ec9@gitrepo.freebsd.org> The branch main has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=9d82546ac2def568ff2f55c63cd77c8d8ff959e2 commit 9d82546ac2def568ff2f55c63cd77c8d8ff959e2 Author: Bjoern A. Zeeb AuthorDate: 2026-06-03 03:39:09 +0000 Commit: Bjoern A. Zeeb CommitDate: 2026-10-11 13:22:23 +0000 LinuxKPI: pci: cleanup lkpinew_pci_dev_release() Factor out some cleanup routines from the device detach path so we can re-use them here too. Also clarifying that lkpinew_pci_dev_release() should only run from the (*release) callback function means that we adjust our other calls to what we had already envisioned in comments, to go through the official pci_dev_put() path. Compared to the device detach path, we have little chance to know if we held the last reference and if the device is really gone or not but in this case a later release would be fine (if it happens). In contrast to the device detach path we cannot do the hack of setting the .release callback to NULL and check upon return from pci_dev_put() as in this case the pdev is freed. Sponsored by: The FreeBSD Foundation MFC after: 3 days Reviewed by: dumbbell (before rebase for ac9c07a3e908 instead of D57430) Differential Revision: https://reviews.freebsd.org/D57431 --- sys/compat/linuxkpi/common/src/linux_pci.c | 114 +++++++++++++++++------------ 1 file changed, 67 insertions(+), 47 deletions(-) diff --git a/sys/compat/linuxkpi/common/src/linux_pci.c b/sys/compat/linuxkpi/common/src/linux_pci.c index fbfde94ca6fd..2c442b3d8f77 100644 --- a/sys/compat/linuxkpi/common/src/linux_pci.c +++ b/sys/compat/linuxkpi/common/src/linux_pci.c @@ -31,10 +31,10 @@ /* * We have two ways to create a pci_dev (pdev): * (1) coming from the device_attach DEVMETHOD, and - * (2) the other from manual creation via lkpinew_pci_dev(). + * (2) from manual creation via lkpinew_pci_dev(), in which case + * we have no driver (pdev->pdrv). * * Only devices from (1) end up on our LinuxKPI global pci_devices list. - * All others are "place fillers" -- XXX if only "place filler" was always true. */ #include @@ -109,7 +109,6 @@ static int linux_backlight_get_status(device_t dev, struct backlight_props *prop static int linux_backlight_update_status(device_t dev, struct backlight_props *props); static int linux_backlight_get_info(device_t dev, struct backlight_info *info); static void lkpi_pcim_iomap_table_release(struct device *, void *); -static void lkpinew_pci_dev_release(struct device *); static device_method_t pci_methods[] = { DEVMETHOD(device_probe, linux_pci_probe), @@ -346,31 +345,14 @@ lkpi_pci_get_device(uint32_t vendor, uint32_t device, struct pci_dev *odev) return (found); } +/* + * Shared function releasing the bus resources which can be built independent + * of the way we allocate/initialization the pdev [pci_upstream_bridge(), + * pcie_find_root_port()]. + */ static void -lkpi_pci_dev_release(struct device *dev) +lkpi_pci_dev_bus_release(struct pci_dev *pdev) { - struct pci_dev *pdev; - - /* - * Before anything else, we have to free all the dynamic - * resource which are on the devres list. - * Otherwise we risk that supporting infrastructure - * is gone and we panic 'randomly'. - */ - lkpi_devres_release_free_list(dev); - - /* - * Now undo linux_pci_attach_device() in reverse-ish - * order. - */ - pdev = to_pci_dev(dev); - - /* - * pdrv->remove happens before pci_put_dev() in - * linux_pci_detach_device(), which means the driver should have - * cleaned up before we get here; see irqents and mmio below. - */ - /* Clear the hierarchy recursively to root. */ if (pdev->bus->self != pdev) { pci_dev_put(pdev->bus->self); @@ -378,20 +360,19 @@ lkpi_pci_dev_release(struct device *dev) } if (pdev->root != NULL) { - lkpinew_pci_dev_release(&pdev->root->dev); /* pci_dev_put(pdev->root); ? */ + pci_dev_put(pdev->root); pdev->root = NULL; } +} - spin_lock(&pci_lock); - list_del(&pdev->links); - spin_unlock(&pci_lock); - - linux_pdev_dma_uninit(pdev); +/* Release resources acquired by lkpifill_pci_dev(). */ +static void +lkpi_pci_dev_fill_release(struct device *dev) +{ + struct pci_dev *pdev; - /* irq? */ + pdev = to_pci_dev(dev); - /* Undo lkpifill_pci_dev(). */ - /* devres is gone already; went at the very top. */ if (!list_empty_careful(&pdev->dev.irqents)) { dev_warn(&pdev->dev, "%s: driver did not clean up; " "leaking IRQs\n", __func__); @@ -418,6 +399,47 @@ lkpi_pci_dev_release(struct device *dev) free(pdev->bus, M_DEVBUF); kfree(pdev->path_name); +} + +static void +lkpi_pci_dev_release(struct device *dev) +{ + struct pci_dev *pdev; + + /* + * Before anything else, we have to free all the dynamic + * resource which are on the devres list. + * Otherwise we risk that supporting infrastructure + * is gone and we panic 'randomly'. + */ + lkpi_devres_release_free_list(dev); + + /* + * Now undo linux_pci_attach_device() in reverse-ish + * order. + */ + pdev = to_pci_dev(dev); + + /* + * pdrv->remove happens before pci_put_dev() in + * linux_pci_detach_device(), which means the driver should have + * cleaned up before we get here; see irqents and mmio below. + */ + + /* Clear the hierarchy recursively to root. */ + lkpi_pci_dev_bus_release(pdev); + + spin_lock(&pci_lock); + list_del(&pdev->links); + spin_unlock(&pci_lock); + + linux_pdev_dma_uninit(pdev); + + /* irq? */ + + /* Undo lkpifill_pci_dev(). */ + /* devres is gone already; went at the very top. */ + lkpi_pci_dev_fill_release(dev); /* * Lastly, apply an internal hack in order to signal @@ -487,24 +509,22 @@ lkpifill_pci_dev(device_t dev, struct pci_dev *pdev) return (0); } +/* + * This is the .release function called when the kobj reference drops to 0. + * There should be no direct calls of this. + */ static void lkpinew_pci_dev_release(struct device *dev) { struct pci_dev *pdev; - int i; pdev = to_pci_dev(dev); - if (pdev->root != NULL) - pci_dev_put(pdev->root); - if (pdev->bus->self != pdev && pdev->bus->self != NULL) - pci_dev_put(pdev->bus->self); - free(pdev->bus, M_DEVBUF); - if (pdev->msi_desc != NULL) { - for (i = pci_msi_count(pdev->dev.bsddev) - 1; i >= 0; i--) - free(pdev->msi_desc[i], M_DEVBUF); - free(pdev->msi_desc, M_DEVBUF); - } - kfree(pdev->path_name); + + /* The root/bus structure could be built up w/o us having asked for it. */ + lkpi_pci_dev_bus_release(pdev); + /* Cleanup the lkpifill_pci_dev() resources. */ + lkpi_pci_dev_fill_release(dev); + free(pdev, M_DEVBUF); }