{"id":2225993,"url":"http://patchwork.ozlabs.org/api/1.2/patches/2225993/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-pci/patch/1776821127-234830-2-git-send-email-shawn.lin@rock-chips.com/","project":{"id":28,"url":"http://patchwork.ozlabs.org/api/1.2/projects/28/?format=json","name":"Linux PCI development","link_name":"linux-pci","list_id":"linux-pci.vger.kernel.org","list_email":"linux-pci@vger.kernel.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<1776821127-234830-2-git-send-email-shawn.lin@rock-chips.com>","list_archive_url":null,"date":"2026-04-22T01:25:25","name":"[v3,1/3] PCI/MSI: Add Devres managed IRQ vectors allocation","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"822b46683106973aca5d631a874d67dfa9778396","submitter":{"id":66993,"url":"http://patchwork.ozlabs.org/api/1.2/people/66993/?format=json","name":"Shawn Lin","email":"shawn.lin@rock-chips.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/linux-pci/patch/1776821127-234830-2-git-send-email-shawn.lin@rock-chips.com/mbox/","series":[{"id":500907,"url":"http://patchwork.ozlabs.org/api/1.2/series/500907/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-pci/list/?series=500907","date":"2026-04-22T01:25:24","name":"Add Devres managed IRQ vectors allocation","version":3,"mbox":"http://patchwork.ozlabs.org/series/500907/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2225993/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2225993/checks/","tags":{},"related":[],"headers":{"Return-Path":"\n <linux-pci+bounces-52891-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-pci@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=rock-chips.com header.i=@rock-chips.com\n header.a=rsa-sha256 header.s=default header.b=QKxEZYXx;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c0a:e001:db::12fc:5321; helo=sea.lore.kernel.org;\n envelope-from=linux-pci+bounces-52891-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (1024-bit key) header.d=rock-chips.com header.i=@rock-chips.com\n header.b=\"QKxEZYXx\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=220.197.31.107","smtp.subspace.kernel.org;\n dmarc=pass (p=none dis=none) header.from=rock-chips.com","smtp.subspace.kernel.org;\n spf=pass smtp.mailfrom=rock-chips.com"],"Received":["from sea.lore.kernel.org (sea.lore.kernel.org\n [IPv6:2600:3c0a:e001:db::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g0hWm3LLqz1y2d\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 22 Apr 2026 11:31:16 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id 1E2213014942\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 22 Apr 2026 01:31:14 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id 06874201113;\n\tWed, 22 Apr 2026 01:31:13 +0000 (UTC)","from mail-m19731107.qiye.163.com (mail-m19731107.qiye.163.com\n [220.197.31.107])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id EB6331E0B9C\n\tfor <linux-pci@vger.kernel.org>; Wed, 22 Apr 2026 01:31:09 +0000 (UTC)","from localhost.localdomain (unknown [61.154.14.86])\n\tby smtp.qiye.163.com (Hmail) with ESMTP id 3ba07bb34;\n\tWed, 22 Apr 2026 09:25:48 +0800 (GMT+08:00)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1776821472; cv=none;\n b=OGgDnS6ePWsi+uitDVONcrmNy5MfH474uBplQBuwSSsyZERag10If1gIkkilxWLh/9Xp+e3b/ID1fhv80bsxXon1TKPYxyUwjH6TuxlLL8Ac8RoLJQvFLdRAsVX6q8MwJjGOoCJWnqmWmZvuHhutvs2YQ4Gnv6vzBquTiSd/6SQ=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1776821472; c=relaxed/simple;\n\tbh=mgOROuN7KOw/IJn45SaXnkxOyF2I9YikrzO2sZIeVhc=;\n\th=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References;\n b=umtWqEbxHI4AQoIwRXy+fPOub+YPRGrmPFuMJdX6A+i5k2tUeELF3I9ZwV9BUokbaAY47RwPGHzQF0enq3Rf9A3W4sIXGkpygrkcGmticj3G4XSJZoJo8H7cGi4+7ISQJ8AYfplWVgZsQ1KIlOsV+7NtP42xAqM+kuonqeYnsts=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dmarc=pass (p=none dis=none) header.from=rock-chips.com;\n spf=pass smtp.mailfrom=rock-chips.com;\n dkim=pass (1024-bit key) header.d=rock-chips.com header.i=@rock-chips.com\n header.b=QKxEZYXx; arc=none smtp.client-ip=220.197.31.107","From":"Shawn Lin <shawn.lin@rock-chips.com>","To":"Bjorn Helgaas <bhelgaas@google.com>","Cc":"Nirmal Patel <nirmal.patel@linux.intel.com>,\n\tJonathan Derrick <jonathan.derrick@linux.dev>,\n\tKurt Schwemmer <kurt.schwemmer@microsemi.com>,\n\tLogan Gunthorpe <logang@deltatee.com>,\n\tPhilipp Stanner <phasta@kernel.org>,\n\tlinux-pci@vger.kernel.org,\n\tShawn Lin <shawn.lin@rock-chips.com>","Subject":"[PATCH v3 1/3] PCI/MSI: Add Devres managed IRQ vectors allocation","Date":"Wed, 22 Apr 2026 09:25:25 +0800","Message-Id":"<1776821127-234830-2-git-send-email-shawn.lin@rock-chips.com>","X-Mailer":"git-send-email 2.7.4","In-Reply-To":"<1776821127-234830-1-git-send-email-shawn.lin@rock-chips.com>","References":"<1776821127-234830-1-git-send-email-shawn.lin@rock-chips.com>","X-HM-Tid":"0a9db2cb1a0309cckunm4d4f8bca15def1","X-HM-MType":"1","X-HM-Spam-Status":"e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly\n\ttZV1koWUFITzdXWS1ZQUlXWQ8JGhUIEh9ZQVlDSk1PVksZTB1ISExJGEwYHlYVFAkWGhdVEwETFh\n\toSFyQUDg9ZV1kYEgtZQVlNSlVKTk9VSk9VQ01ZV1kWGg8SFR0UWUFZT0tIVUpLSU9PT0hVSktLVU\n\tpCS0tZBg++","DKIM-Signature":"a=rsa-sha256;\n\tb=QKxEZYXxt4Fp30R+T3roc63r1fbAbRfy1tF/S/dJ0Iyp6KszXaXKvWUsNXRfMVEYuRXxWJmTKOU3qfgq8XcJLlesuVr1ohcH1ypQXzSdQSaY8vy0pXStXPDz9UmJwGYADuM9K92vo2PkOhOCpS2GpyRQrRiHFpymrrRbBO7dWWM=;\n c=relaxed/relaxed; s=default; d=rock-chips.com; v=1;\n\tbh=HxWUyPlDcbq4Pzt6slUkVSSbA8U7i/j7+dvZ/xOYPJg=;\n\th=date:mime-version:subject:message-id:from;","Precedence":"bulk","X-Mailing-List":"linux-pci@vger.kernel.org","List-Id":"<linux-pci.vger.kernel.org>","List-Subscribe":"<mailto:linux-pci+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-pci+unsubscribe@vger.kernel.org>"},"content":"The PCI/MSI subsystem suffers from a long-standing design issue where\nthe implicit, automatic management of IRQ vectors by the devres\nframework conflicts with explicit driver cleanup, leading to ambiguous\nownership and potential resource management bugs.\n\nHistorically, pcim_enable_device() not only manages standard PCI\nresources (BARs) via devres but also implicitly sets the is_msi_managed\nflag if calling pci_alloc_irq_vectors*(). This registers pcim_msi_release()\nas a cleanup action, creating a hybrid ownership model.\n\nThis ambiguity causes problems when drivers follow a common but\nhazardous pattern:\n1. Using pcim_enable_device() (which implicitly marks IRQs as managed)\n2. Explicitly calling pci_alloc_irq_vectors() for IRQ allocation\n3. Also calling pci_free_irq_vectors() in error paths or remove routines\n\nIn this scenario, the devres framework may attempt to free the IRQ\nvectors a second time upon device release, leading to double-free\nissues. Analysis of the tree shows this hazardous pattern exists\nin multiple drivers, while 35 other drivers correctly rely solely\non the implicit cleanup.\n\nTo resolve this ambiguity, introduce explicit managed APIs for IRQ\nvector allocation:\n- pcim_alloc_irq_vectors()\n- pcim_alloc_irq_vectors_affinity()\n\nThese functions are the devres-managed counterparts to\npci_alloc_irq_vectors[_affinity](). Drivers that wish to have\ndevres-managed IRQ vectors should use these new APIs instead.\n\nThe long-term goal is to convert all drivers which use pcim_enable_device()\nas well as pci_alloc_irq_vectors[_affinity]() to use these managed\nfunctions, and eventually remove the problematic hybrid management\npattern from pcim_enable_device() and pcim_setup_msi_release() entirely.\nThis patch lays the foundation by introducing the APIs.\n\nSuggested-by: Philipp Stanner <phasta@kernel.org>\nSigned-off-by: Shawn Lin <shawn.lin@rock-chips.com>\n\n---\n\nChanges in v3:\n- Rework the commit message and function doc (Philipp)\n- Remove setting is_msi_managed flag from new APIs (Philipp)\n\nChanges in v2:\n- Rebase\n- Introduce patches only for PCI subsystem to convert the API\n\n drivers/pci/msi/api.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++\n include/linux/pci.h   | 22 ++++++++++++++++++++++\n 2 files changed, 73 insertions(+)","diff":"diff --git a/drivers/pci/msi/api.c b/drivers/pci/msi/api.c\nindex c18559b..53b3966 100644\n--- a/drivers/pci/msi/api.c\n+++ b/drivers/pci/msi/api.c\n@@ -297,6 +297,57 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,\n EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity);\n \n /**\n+ * pcim_alloc_irq_vectors - devres managed pci_alloc_irq_vectors()\n+ * @dev: the PCI device to operate on\n+ * @min_vecs: minimum number of vectors required (must be >= 1)\n+ * @max_vecs: maximum (desired) number of vectors\n+ * @flags: flags for this allocation, see pci_alloc_irq_vectors()\n+ *\n+ * This is a device resource managed version of pci_alloc_irq_vectors().\n+ * Interrupt vectors are automatically freed on driver detach by the\n+ * devres. Drivers do not need to explicitly call pci_free_irq_vectors().\n+ *\n+ * Returns number of vectors allocated (which might be smaller than\n+ * @max_vecs) on success, or a negative error code on failure.\n+ */\n+int pcim_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,\n+\t\t\t   unsigned int max_vecs, unsigned int flags)\n+{\n+\treturn pcim_alloc_irq_vectors_affinity(dev, min_vecs, max_vecs,\n+\t\t\t\t\t       flags, NULL);\n+}\n+EXPORT_SYMBOL(pcim_alloc_irq_vectors);\n+\n+/**\n+ * pcim_alloc_irq_vectors_affinity - devres managed pci_alloc_irq_vectors_affinity()\n+ * @dev: the PCI device to operate on\n+ * @min_vecs: minimum number of vectors required (must be >= 1)\n+ * @max_vecs: maximum (desired) number of vectors\n+ * @flags: flags for this allocation, see pci_alloc_irq_vectors()\n+ * @affd: optional description of the affinity requirements\n+ *\n+ * This is a device resource managed version of pci_alloc_irq_vectors_affinity().\n+ * Interrupt vectors are automatically freed on driver detach by the devres\n+ * machinery. Drivers do not need to explicitly call pci_free_irq_vectors().\n+ *\n+ * The function sets dev->is_msi_managed to true before allocating\n+ * vectors. If allocation fails and is_msi_managed was not already set\n+ * before calling this function, the flag is restored to false. This\n+ * ensures proper state cleanup on failure.\n+ *\n+ * Returns number of vectors allocated (which might be smaller than\n+ * @max_vecs) on success, or a negative error code on failure.\n+ */\n+int pcim_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,\n+\t\t\t\t   unsigned int max_vecs, unsigned int flags,\n+\t\t\t\t   struct irq_affinity *affd)\n+{\n+\treturn pci_alloc_irq_vectors_affinity(dev, min_vecs, max_vecs,\n+\t\t\t\t\t      flags, affd);\n+}\n+EXPORT_SYMBOL(pcim_alloc_irq_vectors_affinity);\n+\n+/**\n  * pci_irq_vector() - Get Linux IRQ number of a device interrupt vector\n  * @dev: the PCI device to operate on\n  * @nr:  device-relative interrupt vector index (0-based); has different\ndiff --git a/include/linux/pci.h b/include/linux/pci.h\nindex 2c44545..3716c67 100644\n--- a/include/linux/pci.h\n+++ b/include/linux/pci.h\n@@ -1770,6 +1770,12 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,\n \t\t\t\t   unsigned int max_vecs, unsigned int flags,\n \t\t\t\t   struct irq_affinity *affd);\n \n+int pcim_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,\n+\t\t\t  unsigned int max_vecs, unsigned int flags);\n+int pcim_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,\n+\t\t\t\t   unsigned int max_vecs, unsigned int flags,\n+\t\t\t\t   struct irq_affinity *affd);\n+\n bool pci_msix_can_alloc_dyn(struct pci_dev *dev);\n struct msi_map pci_msix_alloc_irq_at(struct pci_dev *dev, unsigned int index,\n \t\t\t\t     const struct irq_affinity_desc *affdesc);\n@@ -1812,6 +1818,22 @@ pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,\n \t\t\t\t\t      flags, NULL);\n }\n \n+static inline int\n+pcim_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,\n+\t\t\t       unsigned int max_vecs, unsigned int flags,\n+\t\t\t       struct irq_affinity *aff_desc)\n+{\n+\treturn pci_alloc_irq_vectors_affinity(dev, min_vecs, max_vecs,\n+\t\t\t\t\t      flags, aff_desc);\n+}\n+static inline int\n+pcim_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,\n+\t\t      unsigned int max_vecs, unsigned int flags)\n+{\n+\treturn pcim_alloc_irq_vectors_affinity(dev, min_vecs, max_vecs,\n+\t\t\t\t\t      flags, NULL);\n+}\n+\n static inline bool pci_msix_can_alloc_dyn(struct pci_dev *dev)\n { return false; }\n static inline struct msi_map pci_msix_alloc_irq_at(struct pci_dev *dev, unsigned int index,\n","prefixes":["v3","1/3"]}