Skip to contentSkip to conversation

PCI: endpoint: pci-epf-*ntb: Harden vNTB resource management

PATCH Applied Koichiro Den 3 participants 14 messages Feb 26 – Mar 4

Subsystems (3)

v5 of 6 Applied Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover PCI: endpoint: pci-epf-*ntb: Harden vNTB resource management

The vNTB endpoint function (pci-epf-vntb) can be configured and reconfigured through configfs (link/unlink functions, start/stop the controller, update parameters). In practice, several pitfalls present:
duplicate EPC teardown that leads to oopses, a work item running after resources were torn down, and inability to re-link/restart fundamentally because ntb_dev was embedded and the vPCI bus teardown was incomplete.

This series addresses those issues and hardens resource management of pci-epf-vntb:

- Remove duplicate EPC resource teardown in both pci-epf-vntb and pci-epf-ntb, avoiding crashes on .allow_link failures and during .drop_link.
- Stop the delayed cmd_handler work before clearing BARs/doorbells.
- Manage ntb_dev as a devm-managed allocation and implement .remove() in the vNTB PCI driver. Switch to pci_scan_root_bus().

With these changes, the controller can now be stopped, a function unlinked, configfs settings updated, and the controller re-linked and restarted without rebooting the endpoint, as long as the underlying pci_epc_ops .stop() is non-destructive and .start() restores normal operation.

Patches 1-3 carry Fixes tags and are candidates for stable. Patch 4 is a preparatory one for Patch 5.
Patch 5 is a behavioral improvement that completes lifetime management for relink/restart scenarios.

---
v4->v5 changes:
- Rebased onto the latest pci/endpoint (2026-02-26).
- Dropped [PATCH v4 1/7]; will be reposted separately via the NTB tree.
- Dropped [PATCH v4 2/7], which has been applied in a different form.
- Corrected the subject prefix of [PATCH v4 5/7]:
s/NTB: epf: vntb:/PCI: endpoint: pci-epf-vntb:/.
- Picked up a Reviewed-by tag to [PATCH v4 7/7].
- Resolved a conflict in [PATCH v4 7/7] due to commit

    dc693d606644 ("PCI: endpoint: pci-epf-vntb: Add MSI doorbell support").

v3->v4 changes:
- Added Reviewed-by tag for [PATCH v3 6/6].
- Corrected patch split by moving the blank-line cleanup, based on the feedback from Frank.
(No code changes overall.)
v2->v3 changes:
- Added Reviewed-by tag for [PATCH v2 4/6].
- Split [PATCH v2 6/6] into two, based on the feedback from Frank. (No code changes overall.)
v1->v2 changes:
- Incorporated feedback from Frank.
- Added Reviewed-by tags (except for patches #4 and #6).
- Fixed a typo in patch #5 title.
(No code changes overall.)

v4: https://lore.kernel.org/linux-pci/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Koichiro Den (5):
PCI: endpoint: pci-epf-vntb: Remove duplicate resource teardown
PCI: endpoint: pci-epf-ntb: Remove duplicate resource teardown
PCI: endpoint: pci-epf-vntb: Stop cmd_handler work in
epf_ntb_epc_cleanup
PCI: endpoint: pci-epf-vntb: Switch vpci_scan_bus() to use

    pci_scan_root_bus()

PCI: endpoint: pci-epf-vntb: manage ntb_dev lifetime and fix vpci bus
teardown

 drivers/pci/endpoint/functions/pci-epf-ntb.c  | 56 +-----------
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 88 ++++++++++++-------
 2 files changed, 57 insertions(+), 87 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v5 1/5 PCI: endpoint: pci-epf-vntb: Remove duplicate resource teardown

epf_ntb_epc_destroy() duplicates the teardown that the caller is supposed to perform later. This leads to an oops when .allow_link fails or when .drop_link is performed. The following is an example oops of the former case:

  Unable to handle kernel paging request at virtual address dead000000000108
  [...]
  [dead000000000108] address between user and kernel address ranges
  Internal error: Oops: 0000000096000044 [#1]  SMP
  [...]
  Call trace:
   pci_epc_remove_epf+0x78/0xe0 (P)
   pci_primary_epc_epf_link+0x88/0xa8
   configfs_symlink+0x1f4/0x5a0
   vfs_symlink+0x134/0x1d8
   do_symlinkat+0x88/0x138
   __arm64_sys_symlinkat+0x74/0xe0
  [...]

Remove the helper, and drop pci_epc_put(). EPC device refcounting is tied to the configfs EPC group lifetime, and pci_epc_put() in the .drop_link path is sufficient.

Fixes e35f56bb0330 ("PCI: endpoint: Support NTB transfer between RC and EP")
---
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +1 -18
index 148a3b160812..42c870ee3956 100644
@@ -763,19 +763,6 @@ static void epf_ntb_mw_bar_clear(struct epf_ntb *ntb, int num_mws)
 	}
 }
 
-/**
- * epf_ntb_epc_destroy() - Cleanup NTB EPC interface
- * @ntb: NTB device that facilitates communication between HOST and VHOST
- *
- * Wrapper for epf_ntb_epc_destroy_interface() to cleanup all the NTB interfaces
- */
-static void epf_ntb_epc_destroy(struct epf_ntb *ntb)
-{
-	pci_epc_remove_epf(ntb->epf->epc, ntb->epf, 0);
-	pci_epc_put(ntb->epf->epc);
-}
-
-
 /**
  * epf_ntb_is_bar_used() - Check if a bar is used in the ntb configuration
  * @ntb: NTB device that facilitates communication between HOST and VHOST
@@ -1529,7 +1516,7 @@ static int epf_ntb_bind(struct pci_epf *epf)
 	ret = epf_ntb_init_epc_bar(ntb);
 	if (ret) {
 		dev_err(dev, "Failed to create NTB EPC\n");
-		goto err_bar_init;
+		return ret;
 	}
 
 	ret = epf_ntb_config_spad_bar_alloc(ntb);
@@ -1569,9 +1556,6 @@ static int epf_ntb_bind(struct pci_epf *epf)
 err_bar_alloc:
 	epf_ntb_config_spad_bar_free(ntb);
 
-err_bar_init:
-	epf_ntb_epc_destroy(ntb);
-
 	return ret;
 }
 
@@ -1587,7 +1571,6 @@ static void epf_ntb_unbind(struct pci_epf *epf)
 
 	epf_ntb_epc_cleanup(ntb);
 	epf_ntb_config_spad_bar_free(ntb);
-	epf_ntb_epc_destroy(ntb);
 
 	pci_unregister_driver(&vntb_pci_driver);
 }
Signature · 1 line
2.51.0
#2 lore raw

PATCH v5 2/5 PCI: endpoint: pci-epf-ntb: Remove duplicate resource teardown

In reply to Koichiro Den

epf_ntb_epc_destroy() duplicates the teardown that the caller is supposed to do later. This leads to an oops when .allow_link fails or when .drop_link is performed. Remove the helper.

Also drop pci_epc_put(). EPC device refcounting is tied to configfs EPC group lifetime, and pci_epc_put() in the .drop_link path is sufficient.

Fixes 8b821cf76150 ("PCI: endpoint: Add EP function driver to provide NTB functionality")
---
 drivers/pci/endpoint/functions/pci-epf-ntb.c | 56 +-------------------
 1 file changed, 2 insertions(+), 54 deletions(-)
drivers/pci/endpoint/functions/pci-epf-ntb.c +2 -54
index a3a588e522e7..2bdcc35b652c 100644
@@ -1494,47 +1494,6 @@ static int epf_ntb_db_mw_bar_init(struct epf_ntb *ntb,
 	return ret;
 }
 
-/**
- * epf_ntb_epc_destroy_interface() - Cleanup NTB EPC interface
- * @ntb: NTB device that facilitates communication between HOST1 and HOST2
- * @type: PRIMARY interface or SECONDARY interface
- *
- * Unbind NTB function device from EPC and relinquish reference to pci_epc
- * for each of the interface.
- */
-static void epf_ntb_epc_destroy_interface(struct epf_ntb *ntb,
-					  enum pci_epc_interface_type type)
-{
-	struct epf_ntb_epc *ntb_epc;
-	struct pci_epc *epc;
-	struct pci_epf *epf;
-
-	if (type < 0)
-		return;
-
-	epf = ntb->epf;
-	ntb_epc = ntb->epc[type];
-	if (!ntb_epc)
-		return;
-	epc = ntb_epc->epc;
-	pci_epc_remove_epf(epc, epf, type);
-	pci_epc_put(epc);
-}
-
-/**
- * epf_ntb_epc_destroy() - Cleanup NTB EPC interface
- * @ntb: NTB device that facilitates communication between HOST1 and HOST2
- *
- * Wrapper for epf_ntb_epc_destroy_interface() to cleanup all the NTB interfaces
- */
-static void epf_ntb_epc_destroy(struct epf_ntb *ntb)
-{
-	enum pci_epc_interface_type type;
-
-	for (type = PRIMARY_INTERFACE; type <= SECONDARY_INTERFACE; type++)
-		epf_ntb_epc_destroy_interface(ntb, type);
-}
-
 /**
  * epf_ntb_epc_create_interface() - Create and initialize NTB EPC interface
  * @ntb: NTB device that facilitates communication between HOST1 and HOST2
@@ -1614,15 +1573,8 @@ static int epf_ntb_epc_create(struct epf_ntb *ntb)
 
 	ret = epf_ntb_epc_create_interface(ntb, epf->sec_epc,
 					   SECONDARY_INTERFACE);
-	if (ret) {
+	if (ret)
 		dev_err(dev, "SECONDARY intf: Fail to create NTB EPC\n");
-		goto err_epc_create;
-	}
-
-	return 0;
-
-err_epc_create:
-	epf_ntb_epc_destroy_interface(ntb, PRIMARY_INTERFACE);
 
 	return ret;
 }
@@ -1887,7 +1839,7 @@ static int epf_ntb_bind(struct pci_epf *epf)
 	ret = epf_ntb_init_epc_bar(ntb);
 	if (ret) {
 		dev_err(dev, "Failed to create NTB EPC\n");
-		goto err_bar_init;
+		return ret;
 	}
 
 	ret = epf_ntb_config_spad_bar_alloc_interface(ntb);
@@ -1909,9 +1861,6 @@ static int epf_ntb_bind(struct pci_epf *epf)
 err_bar_alloc:
 	epf_ntb_config_spad_bar_free(ntb);
 
-err_bar_init:
-	epf_ntb_epc_destroy(ntb);
-
 	return ret;
 }
 
@@ -1927,7 +1876,6 @@ static void epf_ntb_unbind(struct pci_epf *epf)
 
 	epf_ntb_epc_cleanup(ntb);
 	epf_ntb_config_spad_bar_free(ntb);
-	epf_ntb_epc_destroy(ntb);
 }
 
 #define EPF_NTB_R(_name)						\
Signature · 1 line
2.51.0
#3 lore raw

PATCH v5 3/5 PCI: endpoint: pci-epf-vntb: Stop cmd_handler work in epf_ntb_epc_cleanup

In reply to Koichiro Den

Disable the delayed work before clearing BAR mappings and doorbells to avoid running the handler after resources have been torn down.

  Unable to handle kernel paging request at virtual address ffff800083f46004
  [...]
  Internal error: Oops: 0000000096000007 [#1]  SMP
  [...]
  Call trace:
   epf_ntb_cmd_handler+0x54/0x200 [pci_epf_vntb] (P)
   process_one_work+0x154/0x3b0
   worker_thread+0x2c8/0x400
   kthread+0x148/0x210
   ret_from_fork+0x10/0x20
Fixes e35f56bb0330 ("PCI: endpoint: Support NTB transfer between RC and EP")
---
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 1 +
 1 file changed, 1 insertion(+)
drivers/pci/endpoint/functions/pci-epf-vntb.c +1 -0
index 42c870ee3956..805353528967 100644
@@ -942,6 +942,7 @@ static int epf_ntb_epc_init(struct epf_ntb *ntb)
  */
 static void epf_ntb_epc_cleanup(struct epf_ntb *ntb)
 {
+	disable_delayed_work_sync(&ntb->cmd_handler);
 	epf_ntb_mw_bar_clear(ntb, ntb->num_mws);
 	epf_ntb_db_bar_clear(ntb);
 	epf_ntb_config_sspad_bar_clear(ntb);
Signature · 1 line
2.51.0
#4 lore raw

PATCH v5 4/5 PCI: endpoint: pci-epf-vntb: Switch vpci_scan_bus() to use pci_scan_root_bus()

In reply to Koichiro Den

vpci_scan_bus() currently uses pci_scan_bus(), which creates a root bus without a parent struct device. In a subsequent change we want to tear down the virtual PCI root bus using pci_remove_root_bus(). For that to work correctly, the root bus must be associated with a parent device, similar to what the removed pci_scan_bus_parented() helper used to do.

Switch vpci_scan_bus() to use pci_scan_root_bus() and pass &ndev->epf->epc->dev as the parent. Build the resource list in the same way as pci_scan_bus(), so the behavior is unchanged except that the virtual root bus now has a proper parent device. This avoids crashes in the pci_epf_unbind() -> epf_ntb_unbind() -> pci_remove_root_bus() -> pci_bus_release_domain_nr() path once we start removing the root bus in a follow-up patch.

---
Changes since v4:
- Correct the subject prefix
s/NTB: epf: vntb:/PCI: endpoint: pci-epf-vntb:/

 drivers/pci/endpoint/functions/pci-epf-vntb.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +13 -2
index 805353528967..f353e9a57194 100644
@@ -1220,8 +1220,19 @@ static int vpci_scan_bus(void *sysdata)
 {
 	struct pci_bus *vpci_bus;
 	struct epf_ntb *ndev = sysdata;
-
-	vpci_bus = pci_scan_bus(ndev->vbus_number, &vpci_ops, sysdata);
+	LIST_HEAD(resources);
+	static struct resource busn_res = {
+		.start = 0,
+		.end = 255,
+		.flags = IORESOURCE_BUS,
+	};
+
+	pci_add_resource(&resources, &ioport_resource);
+	pci_add_resource(&resources, &iomem_resource);
+	pci_add_resource(&resources, &busn_res);
+
+	vpci_bus = pci_scan_root_bus(&ndev->epf->epc->dev, ndev->vbus_number,
+				     &vpci_ops, sysdata, &resources);
 	if (!vpci_bus) {
 		pr_err("create pci bus failed\n");
 		return -EINVAL;
Signature · 1 line
2.51.0
#5 lore raw

Re: [PATCH v5 4/5] PCI: endpoint: pci-epf-vntb: Switch vpci_scan_bus() to use pci_scan_root_bus()

Quoted 37 lines · Koichiro Den

vpci_scan_bus() currently uses pci_scan_bus(), which creates a root bus without a parent struct device. In a subsequent change we want to tear down the virtual PCI root bus using pci_remove_root_bus(). For that to work correctly, the root bus must be associated with a parent device, similar to what the removed pci_scan_bus_parented() helper used to do.

Switch vpci_scan_bus() to use pci_scan_root_bus() and pass &ndev->epf->epc->dev as the parent. Build the resource list in the same way as pci_scan_bus(), so the behavior is unchanged except that the virtual root bus now has a proper parent device. This avoids crashes in the pci_epf_unbind() -> epf_ntb_unbind() -> pci_remove_root_bus() -> pci_bus_release_domain_nr() path once we start removing the root bus in a follow-up patch.

---
Changes since v4:
- Correct the subject prefix
s/NTB: epf: vntb:/PCI: endpoint: pci-epf-vntb:/

 drivers/pci/endpoint/functions/pci-epf-vntb.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +3 -2
index 805353528967..f353e9a57194 100644
@@ -1220,8 +1220,19 @@ static int vpci_scan_bus(void *sysdata)
 {
 	struct pci_bus *vpci_bus;
 	struct epf_ntb *ndev = sysdata;
-
-	vpci_bus = pci_scan_bus(ndev->vbus_number, &vpci_ops, sysdata);
+	LIST_HEAD(resources);
+	static struct resource busn_res = {
+		.start = 0,

drivers/pci/endpoint/functions/pci-epf-vntb.c static int vpci_scan_bus(void *sysdata) · line 1235

Quoted 10 lines
drivers/pci/endpoint/functions/pci-epf-vntb.c +10 -0
+		.end = 255,
+		.flags = IORESOURCE_BUS,
+	};
+
+	pci_add_resource(&resources, &ioport_resource);
+	pci_add_resource(&resources, &iomem_resource);
+	pci_add_resource(&resources, &busn_res);
+
+	vpci_bus = pci_scan_root_bus(&ndev->epf->epc->dev, ndev->vbus_number,
+				     &vpci_ops, sysdata, &resources);

Don't you need pci_lock_rescan_remove()/pci_unlock_rescan_remove()?

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#6 lore raw

Re: [PATCH v5 4/5] PCI: endpoint: pci-epf-vntb: Switch vpci_scan_bus() to use pci_scan_root_bus()

Quoted 42 lines · Manivannan Sadhasivam
Quoted 41 lines · Koichiro Den

vpci_scan_bus() currently uses pci_scan_bus(), which creates a root bus without a parent struct device. In a subsequent change we want to tear down the virtual PCI root bus using pci_remove_root_bus(). For that to work correctly, the root bus must be associated with a parent device, similar to what the removed pci_scan_bus_parented() helper used to do.

Switch vpci_scan_bus() to use pci_scan_root_bus() and pass &ndev->epf->epc->dev as the parent. Build the resource list in the same way as pci_scan_bus(), so the behavior is unchanged except that the virtual root bus now has a proper parent device. This avoids crashes in the pci_epf_unbind() -> epf_ntb_unbind() -> pci_remove_root_bus() -> pci_bus_release_domain_nr() path once we start removing the root bus in a follow-up patch.

---
Changes since v4:
- Correct the subject prefix
s/NTB: epf: vntb:/PCI: endpoint: pci-epf-vntb:/

 drivers/pci/endpoint/functions/pci-epf-vntb.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +7 -2
index 805353528967..f353e9a57194 100644
@@ -1220,8 +1220,19 @@ static int vpci_scan_bus(void *sysdata)
 {
 	struct pci_bus *vpci_bus;
 	struct epf_ntb *ndev = sysdata;
-
-	vpci_bus = pci_scan_bus(ndev->vbus_number, &vpci_ops, sysdata);
+	LIST_HEAD(resources);
+	static struct resource busn_res = {
+		.start = 0,
+		.end = 255,
+		.flags = IORESOURCE_BUS,
+	};
+
Quoted 8 lines

drivers/pci/endpoint/functions/pci-epf-vntb.c static int vpci_scan_bus(void *sysdata) · line 1235

Quoted 6 lines
drivers/pci/endpoint/functions/pci-epf-vntb.c +6 -0
+	pci_add_resource(&resources, &ioport_resource);
+	pci_add_resource(&resources, &iomem_resource);
+	pci_add_resource(&resources, &busn_res);
+
+	vpci_bus = pci_scan_root_bus(&ndev->epf->epc->dev, ndev->vbus_number,
+				     &vpci_ops, sysdata, &resources);

Don't you need pci_lock_rescan_remove()/pci_unlock_rescan_remove()?

Yes, we do need them. Thanks for pointing this out.

This locking was already missing before this series (when vpci_scan_bus() used pci_scan_bus()), so I'll add a separate fix patch between Patch 3 and Patch 4, likely with:

Fixes e35f56bb0330 ("PCI: endpoint: Support NTB transfer between RC and EP").

---8<---

drivers/pci/endpoint/functions/pci-epf-vntb.c +5 -0
index 805353528967..469b6e2b8d45 100644
@@ -1221,14 +1221,19 @@ static int vpci_scan_bus(void *sysdata)
        struct pci_bus *vpci_bus;
        struct epf_ntb *ndev = sysdata;
 
+       pci_lock_rescan_remove();
+
        vpci_bus = pci_scan_bus(ndev->vbus_number, &vpci_ops, sysdata);
        if (!vpci_bus) {
                pr_err("create pci bus failed\n");
+               pci_unlock_rescan_remove();
                return -EINVAL;
        }
 
        pci_bus_add_devices(vpci_bus);
 
+       pci_unlock_rescan_remove();
+
        return 0;
 }
 

---8<---

Also, while respinning, I'll fix the error path in the pci_scan_root_bus() version to free the temporary resource list when pci_scan_root_bus() fails, since it doesn't free it on behalf of the caller. I noticed this while revisiting the patch.

Best regards,
Koichiro

Quoted 5 lines

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#7 lore raw

PATCH v5 5/5 PCI: endpoint: pci-epf-vntb: manage ntb_dev lifetime and fix vpci bus teardown

In reply to Koichiro Den

Currently ntb_dev is embedded in epf_ntb, while configfs allows starting or stopping controller and linking or unlinking functions as you want. In fact, re-linking and re-starting is not possible with the embedded design and leads to oopses.

Allocate ntb_dev with devm and add a .remove callback to the pci driver that calls ntb_unregister_device(). This allows a fresh device to be created on the next .bind call.

With these changes, the controller can now be stopped, a function unlinked, configfs settings updated, and the controller re-linked and restarted without rebooting the endpoint, as long as the underlying pci_epc_ops .stop() operation is non-destructive, and .start() can restore normal operations.

---
Changes since v4:
- Adjusted context due to commit:

    dc693d606644 ("PCI: endpoint: pci-epf-vntb: Add MSI doorbell support")
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 53 ++++++++++++++-----
 1 file changed, 40 insertions(+), 13 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +40 -13
index f353e9a57194..41a2f42e8a39 100644
@@ -120,7 +120,7 @@ struct epf_ntb_ctrl {
 } __packed;
 
 struct epf_ntb {
-	struct ntb_dev ntb;
+	struct ntb_dev *ntb;
 	struct pci_epf *epf;
 	struct config_group group;
 
@@ -147,10 +147,16 @@ struct epf_ntb {
 	void __iomem *vpci_mw_addr[MAX_MW];
 
 	struct delayed_work cmd_handler;
+
+	struct pci_bus *vpci_bus;
 };
 
 #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group)
-#define ntb_ndev(__ntb) container_of(__ntb, struct epf_ntb, ntb)
+
+static inline struct epf_ntb *ntb_ndev(struct ntb_dev *ntb)
+{
+	return (struct epf_ntb *)ntb->pdev->sysdata;
+}
 
 static struct pci_epf_header epf_ntb_header = {
 	.vendorid	= PCI_ANY_ID,
@@ -176,7 +182,7 @@ static int epf_ntb_link_up(struct epf_ntb *ntb, bool link_up)
 	else
 		ntb->reg->link_status &= ~LINK_STATUS_UP;
 
-	ntb_link_event(&ntb->ntb);
+	ntb_link_event(ntb->ntb);
 	return 0;
 }
 
@@ -264,7 +270,7 @@ static void epf_ntb_cmd_handler(struct work_struct *work)
 	for (i = 1; i < ntb->db_count && !ntb->msi_doorbell; i++) {
 		if (ntb->epf_db[i]) {
 			atomic64_or(1 << (i - 1), &ntb->db);
-			ntb_db_event(&ntb->ntb, i);
+			ntb_db_event(ntb->ntb, i);
 			ntb->epf_db[i] = 0;
 		}
 	}
@@ -333,7 +339,7 @@ static irqreturn_t epf_ntb_doorbell_handler(int irq, void *data)
 	for (i = 1; i < ntb->db_count; i++)
 		if (irq == ntb->epf->db_msg[i].virq) {
 			atomic64_or(1 << (i - 1), &ntb->db);
-			ntb_db_event(&ntb->ntb, i);
+			ntb_db_event(ntb->ntb, i);
 		}
 
 	return IRQ_HANDLED;
@@ -1237,6 +1243,7 @@ static int vpci_scan_bus(void *sysdata)
 		pr_err("create pci bus failed\n");
 		return -EINVAL;
 	}
+	ndev->vpci_bus = vpci_bus;
 
 	pci_bus_add_devices(vpci_bus);
 
@@ -1281,7 +1288,7 @@ static int vntb_epf_mw_set_trans(struct ntb_dev *ndev, int pidx, int idx,
 	int ret;
 	struct device *dev;
 
-	dev = &ntb->ntb.dev;
+	dev = &ntb->ntb->dev;
 	barno = ntb->epf_ntb_bar[BAR_MW1 + idx];
 	epf_bar = &ntb->epf->bar[barno];
 	epf_bar->phys_addr = addr;
@@ -1381,7 +1388,7 @@ static int vntb_epf_peer_db_set(struct ntb_dev *ndev, u64 db_bits)
 	ret = pci_epc_raise_irq(ntb->epf->epc, func_no, vfunc_no,
 				PCI_IRQ_MSI, interrupt_num + 1);
 	if (ret)
-		dev_err(&ntb->ntb.dev, "Failed to raise IRQ\n");
+		dev_err(&ntb->ntb->dev, "Failed to raise IRQ\n");
 
 	return ret;
 }
@@ -1468,9 +1475,12 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
 	struct device *dev = &pdev->dev;
 
-	ndev->ntb.pdev = pdev;
-	ndev->ntb.topo = NTB_TOPO_NONE;
-	ndev->ntb.ops =  &vntb_epf_ops;
+	ndev->ntb = devm_kzalloc(dev, sizeof(*ndev->ntb), GFP_KERNEL);
+	if (!ndev->ntb)
+		return -ENOMEM;
+	ndev->ntb->pdev = pdev;
+	ndev->ntb->topo = NTB_TOPO_NONE;
+	ndev->ntb->ops = &vntb_epf_ops;
 
 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
 	if (ret) {
@@ -1478,7 +1488,7 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
-	ret = ntb_register_device(&ndev->ntb);
+	ret = ntb_register_device(ndev->ntb);
 	if (ret) {
 		dev_err(dev, "Failed to register NTB device\n");
 		return ret;
@@ -1488,6 +1498,17 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	return 0;
 }
 
+static void pci_vntb_remove(struct pci_dev *pdev)
+{
+	struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
+
+	if (!ndev || !ndev->ntb)
+		return;
+
+	ntb_unregister_device(ndev->ntb);
+	ndev->ntb = NULL;
+}
+
 static struct pci_device_id pci_vntb_table[] = {
 	{
 		PCI_DEVICE(0xffff, 0xffff),
@@ -1499,6 +1520,7 @@ static struct pci_driver vntb_pci_driver = {
 	.name           = "pci-vntb",
 	.id_table       = pci_vntb_table,
 	.probe          = pci_vntb_probe,
+	.remove         = pci_vntb_remove,
 };
 
 /* ============ PCIe EPF Driver Bind ====================*/
@@ -1581,10 +1603,15 @@ static void epf_ntb_unbind(struct pci_epf *epf)
 {
 	struct epf_ntb *ntb = epf_get_drvdata(epf);
 
+	pci_unregister_driver(&vntb_pci_driver);
+
+	pci_lock_rescan_remove();
+	pci_stop_root_bus(ntb->vpci_bus);
+	pci_remove_root_bus(ntb->vpci_bus);
+	pci_unlock_rescan_remove();
+
 	epf_ntb_epc_cleanup(ntb);
 	epf_ntb_config_spad_bar_free(ntb);
-
-	pci_unregister_driver(&vntb_pci_driver);
 }
 
 // EPF driver probe
Signature · 1 line
2.51.0
#8 lore raw

Re: [PATCH v5 5/5] PCI: endpoint: pci-epf-vntb: manage ntb_dev lifetime and fix vpci bus teardown

Quoted 40 lines · Koichiro Den

Currently ntb_dev is embedded in epf_ntb, while configfs allows starting or stopping controller and linking or unlinking functions as you want. In fact, re-linking and re-starting is not possible with the embedded design and leads to oopses.

Allocate ntb_dev with devm and add a .remove callback to the pci driver that calls ntb_unregister_device(). This allows a fresh device to be created on the next .bind call.

With these changes, the controller can now be stopped, a function unlinked, configfs settings updated, and the controller re-linked and restarted without rebooting the endpoint, as long as the underlying pci_epc_ops .stop() operation is non-destructive, and .start() can restore normal operations.

---
Changes since v4:
- Adjusted context due to commit:

    dc693d606644 ("PCI: endpoint: pci-epf-vntb: Add MSI doorbell support")
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 53 ++++++++++++++-----
 1 file changed, 40 insertions(+), 13 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +1 -1
index f353e9a57194..41a2f42e8a39 100644
@@ -120,7 +120,7 @@ struct epf_ntb_ctrl {
 } __packed;
 
 struct epf_ntb {
-	struct ntb_dev ntb;
+	struct ntb_dev *ntb;
 	struct pci_epf *epf;
 	struct config_group group;
 
@@ -147,10 +147,16 @@ struct epf_ntb {
 	void __iomem *vpci_mw_addr[MAX_MW];

drivers/pci/endpoint/functions/pci-epf-vntb.c struct epf_ntb { · line 156

Quoted 10 lines
drivers/pci/endpoint/functions/pci-epf-vntb.c +4 -1
 
 	struct delayed_work cmd_handler;
+
+	struct pci_bus *vpci_bus;
 };
 
 #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group)
-#define ntb_ndev(__ntb) container_of(__ntb, struct epf_ntb, ntb)
+
+static inline struct epf_ntb *ntb_ndev(struct ntb_dev *ntb)

Nit: No need of 'inline' keyword in a .c file.

- Mani

Quoted 131 lines
drivers/pci/endpoint/functions/pci-epf-vntb.c +35 -11
+{
+	return (struct epf_ntb *)ntb->pdev->sysdata;
+}
 
 static struct pci_epf_header epf_ntb_header = {
 	.vendorid	= PCI_ANY_ID,
@@ -176,7 +182,7 @@ static int epf_ntb_link_up(struct epf_ntb *ntb, bool link_up)
 	else
 		ntb->reg->link_status &= ~LINK_STATUS_UP;
 
-	ntb_link_event(&ntb->ntb);
+	ntb_link_event(ntb->ntb);
 	return 0;
 }
 
@@ -264,7 +270,7 @@ static void epf_ntb_cmd_handler(struct work_struct *work)
 	for (i = 1; i < ntb->db_count && !ntb->msi_doorbell; i++) {
 		if (ntb->epf_db[i]) {
 			atomic64_or(1 << (i - 1), &ntb->db);
-			ntb_db_event(&ntb->ntb, i);
+			ntb_db_event(ntb->ntb, i);
 			ntb->epf_db[i] = 0;
 		}
 	}
@@ -333,7 +339,7 @@ static irqreturn_t epf_ntb_doorbell_handler(int irq, void *data)
 	for (i = 1; i < ntb->db_count; i++)
 		if (irq == ntb->epf->db_msg[i].virq) {
 			atomic64_or(1 << (i - 1), &ntb->db);
-			ntb_db_event(&ntb->ntb, i);
+			ntb_db_event(ntb->ntb, i);
 		}
 
 	return IRQ_HANDLED;
@@ -1237,6 +1243,7 @@ static int vpci_scan_bus(void *sysdata)
 		pr_err("create pci bus failed\n");
 		return -EINVAL;
 	}
+	ndev->vpci_bus = vpci_bus;
 
 	pci_bus_add_devices(vpci_bus);
 
@@ -1281,7 +1288,7 @@ static int vntb_epf_mw_set_trans(struct ntb_dev *ndev, int pidx, int idx,
 	int ret;
 	struct device *dev;
 
-	dev = &ntb->ntb.dev;
+	dev = &ntb->ntb->dev;
 	barno = ntb->epf_ntb_bar[BAR_MW1 + idx];
 	epf_bar = &ntb->epf->bar[barno];
 	epf_bar->phys_addr = addr;
@@ -1381,7 +1388,7 @@ static int vntb_epf_peer_db_set(struct ntb_dev *ndev, u64 db_bits)
 	ret = pci_epc_raise_irq(ntb->epf->epc, func_no, vfunc_no,
 				PCI_IRQ_MSI, interrupt_num + 1);
 	if (ret)
-		dev_err(&ntb->ntb.dev, "Failed to raise IRQ\n");
+		dev_err(&ntb->ntb->dev, "Failed to raise IRQ\n");
 
 	return ret;
 }
@@ -1468,9 +1475,12 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
 	struct device *dev = &pdev->dev;
 
-	ndev->ntb.pdev = pdev;
-	ndev->ntb.topo = NTB_TOPO_NONE;
-	ndev->ntb.ops =  &vntb_epf_ops;
+	ndev->ntb = devm_kzalloc(dev, sizeof(*ndev->ntb), GFP_KERNEL);
+	if (!ndev->ntb)
+		return -ENOMEM;
+	ndev->ntb->pdev = pdev;
+	ndev->ntb->topo = NTB_TOPO_NONE;
+	ndev->ntb->ops = &vntb_epf_ops;
 
 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
 	if (ret) {
@@ -1478,7 +1488,7 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
-	ret = ntb_register_device(&ndev->ntb);
+	ret = ntb_register_device(ndev->ntb);
 	if (ret) {
 		dev_err(dev, "Failed to register NTB device\n");
 		return ret;
@@ -1488,6 +1498,17 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	return 0;
 }
 
+static void pci_vntb_remove(struct pci_dev *pdev)
+{
+	struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
+
+	if (!ndev || !ndev->ntb)
+		return;
+
+	ntb_unregister_device(ndev->ntb);
+	ndev->ntb = NULL;
+}
+
 static struct pci_device_id pci_vntb_table[] = {
 	{
 		PCI_DEVICE(0xffff, 0xffff),
@@ -1499,6 +1520,7 @@ static struct pci_driver vntb_pci_driver = {
 	.name           = "pci-vntb",
 	.id_table       = pci_vntb_table,
 	.probe          = pci_vntb_probe,
+	.remove         = pci_vntb_remove,
 };
 
 /* ============ PCIe EPF Driver Bind ====================*/
@@ -1581,10 +1603,15 @@ static void epf_ntb_unbind(struct pci_epf *epf)
 {
 	struct epf_ntb *ntb = epf_get_drvdata(epf);
 
+	pci_unregister_driver(&vntb_pci_driver);
+
+	pci_lock_rescan_remove();
+	pci_stop_root_bus(ntb->vpci_bus);
+	pci_remove_root_bus(ntb->vpci_bus);
+	pci_unlock_rescan_remove();
+
 	epf_ntb_epc_cleanup(ntb);
 	epf_ntb_config_spad_bar_free(ntb);
-
-	pci_unregister_driver(&vntb_pci_driver);
 }
 
 // EPF driver probe
Signature · 1 line
2.51.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#9 lore raw

Re: [PATCH v5 5/5] PCI: endpoint: pci-epf-vntb: manage ntb_dev lifetime and fix vpci bus teardown

Quoted 47 lines · Manivannan Sadhasivam
Quoted 46 lines · Koichiro Den

Currently ntb_dev is embedded in epf_ntb, while configfs allows starting or stopping controller and linking or unlinking functions as you want. In fact, re-linking and re-starting is not possible with the embedded design and leads to oopses.

Allocate ntb_dev with devm and add a .remove callback to the pci driver that calls ntb_unregister_device(). This allows a fresh device to be created on the next .bind call.

With these changes, the controller can now be stopped, a function unlinked, configfs settings updated, and the controller re-linked and restarted without rebooting the endpoint, as long as the underlying pci_epc_ops .stop() operation is non-destructive, and .start() can restore normal operations.

---
Changes since v4:
- Adjusted context due to commit:

    dc693d606644 ("PCI: endpoint: pci-epf-vntb: Add MSI doorbell support")
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 53 ++++++++++++++-----
 1 file changed, 40 insertions(+), 13 deletions(-)
drivers/pci/endpoint/functions/pci-epf-vntb.c +3 -1
index f353e9a57194..41a2f42e8a39 100644
@@ -120,7 +120,7 @@ struct epf_ntb_ctrl {
 } __packed;
 
 struct epf_ntb {
-	struct ntb_dev ntb;
+	struct ntb_dev *ntb;
 	struct pci_epf *epf;
 	struct config_group group;
 
@@ -147,10 +147,16 @@ struct epf_ntb {
 	void __iomem *vpci_mw_addr[MAX_MW];
 
 	struct delayed_work cmd_handler;
+
+	struct pci_bus *vpci_bus;
 };
 
Quoted 6 lines

drivers/pci/endpoint/functions/pci-epf-vntb.c struct epf_ntb { · line 156

Quoted 4 lines
drivers/pci/endpoint/functions/pci-epf-vntb.c +2 -1
 #define to_epf_ntb(epf_group) container_of((epf_group), struct epf_ntb, group)
-#define ntb_ndev(__ntb) container_of(__ntb, struct epf_ntb, ntb)
+
+static inline struct epf_ntb *ntb_ndev(struct ntb_dev *ntb)

Nit: No need of 'inline' keyword in a .c file.

Thanks for catching this. I'll drop the 'inline' in v6.

While revisiting Patch 5/5 carefully, I also noticed a lifetime issue around cmd_handler (and doorbell IRQ handlers).

1. Race window between pci_unregister_driver() and work/IRQ teardown

With this patch, the following window exists:

-> epf_ntb_unbind()

      -> pci_unregister_driver()
         ...
         -> pci_vntb_remove()
            -> ntb_unregister_device()
            -> ndev->ntb = NULL ----------------------------- (A)
      -> epf_ntb_epc_cleanup()
         -> disable_delayed_work_sync(&ntb->cmd_handler) ---- (B)
         -> epf_ntb_db_bar_clear() (free_irq, etc.) --------- (C)

If cmd_handler runs between (A) and (B) (or if epf_ntb_doorbell_handler() runs between (A) and (C)), they may call ntb_{db,link}_event(ndev->ntb, ...) with ndev->ntb == NULL, leading to a NULL deref. Also, since the doorbell IRQs are requested during bind, the IRQ handler can run even before pci_vntb_probe() ever sets up ndev->ntb.

In current mainline, cmd_handler is initially scheduled in epf_ntb_epc_init(), and the doorbell IRQs are also requested during bind, i.e. both are set up before pci_register_driver() and ndev->ntb becomes valid.

I think the cleanest solution is to tie cmd_handler and the "enabled" state of the doorbell IRQs to the lifetime of struct ntb_dev:

- Move the initial scheduling (queue_work(..., &ndev->cmd_handler.work)) to pci_vntb_probe(), after ntb_register_device() succeeds, and stop it in pci_vntb_remove() using disable_delayed_work_sync() before ntb_unregister_device().

- Keep request_irq()/free_irq() in the existing bind/unbind paths, but gate

     delivery in vPCI probe/remove: keep the doorbell IRQs disabled until
     pci_vntb_probe() succeeds (enable_irq() after ndev->ntb becomes valid), and
     disable_irq() them in pci_vntb_remove() before clearing ndev->ntb /
     ntb_unregister_device().

That way, the unsafe windows I wrote above disappear, and the lifetime of struct ntb_dev and its associated resources are managed symmetrically across bind/unbind and probe/remove paths.

2. Stale ndev->ntb on probe failure

pci_vntb_probe() assigns ndev->ntb before ntb_register_device(). If probe fails after devm_kzalloc(), ndev->ntb may still hold a stale pointer. I'll fix this by avoiding publishing ndev->ntb until ntb_register_device() succeeds (or clear it on the error path).

I'll include these fixes into v6.

Best regards,
Koichiro

Quoted 137 lines

- Mani

Quoted 131 lines
drivers/pci/endpoint/functions/pci-epf-vntb.c +35 -11
+{
+	return (struct epf_ntb *)ntb->pdev->sysdata;
+}
 
 static struct pci_epf_header epf_ntb_header = {
 	.vendorid	= PCI_ANY_ID,
@@ -176,7 +182,7 @@ static int epf_ntb_link_up(struct epf_ntb *ntb, bool link_up)
 	else
 		ntb->reg->link_status &= ~LINK_STATUS_UP;
 
-	ntb_link_event(&ntb->ntb);
+	ntb_link_event(ntb->ntb);
 	return 0;
 }
 
@@ -264,7 +270,7 @@ static void epf_ntb_cmd_handler(struct work_struct *work)
 	for (i = 1; i < ntb->db_count && !ntb->msi_doorbell; i++) {
 		if (ntb->epf_db[i]) {
 			atomic64_or(1 << (i - 1), &ntb->db);
-			ntb_db_event(&ntb->ntb, i);
+			ntb_db_event(ntb->ntb, i);
 			ntb->epf_db[i] = 0;
 		}
 	}
@@ -333,7 +339,7 @@ static irqreturn_t epf_ntb_doorbell_handler(int irq, void *data)
 	for (i = 1; i < ntb->db_count; i++)
 		if (irq == ntb->epf->db_msg[i].virq) {
 			atomic64_or(1 << (i - 1), &ntb->db);
-			ntb_db_event(&ntb->ntb, i);
+			ntb_db_event(ntb->ntb, i);
 		}
 
 	return IRQ_HANDLED;
@@ -1237,6 +1243,7 @@ static int vpci_scan_bus(void *sysdata)
 		pr_err("create pci bus failed\n");
 		return -EINVAL;
 	}
+	ndev->vpci_bus = vpci_bus;
 
 	pci_bus_add_devices(vpci_bus);
 
@@ -1281,7 +1288,7 @@ static int vntb_epf_mw_set_trans(struct ntb_dev *ndev, int pidx, int idx,
 	int ret;
 	struct device *dev;
 
-	dev = &ntb->ntb.dev;
+	dev = &ntb->ntb->dev;
 	barno = ntb->epf_ntb_bar[BAR_MW1 + idx];
 	epf_bar = &ntb->epf->bar[barno];
 	epf_bar->phys_addr = addr;
@@ -1381,7 +1388,7 @@ static int vntb_epf_peer_db_set(struct ntb_dev *ndev, u64 db_bits)
 	ret = pci_epc_raise_irq(ntb->epf->epc, func_no, vfunc_no,
 				PCI_IRQ_MSI, interrupt_num + 1);
 	if (ret)
-		dev_err(&ntb->ntb.dev, "Failed to raise IRQ\n");
+		dev_err(&ntb->ntb->dev, "Failed to raise IRQ\n");
 
 	return ret;
 }
@@ -1468,9 +1475,12 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
 	struct device *dev = &pdev->dev;
 
-	ndev->ntb.pdev = pdev;
-	ndev->ntb.topo = NTB_TOPO_NONE;
-	ndev->ntb.ops =  &vntb_epf_ops;
+	ndev->ntb = devm_kzalloc(dev, sizeof(*ndev->ntb), GFP_KERNEL);
+	if (!ndev->ntb)
+		return -ENOMEM;
+	ndev->ntb->pdev = pdev;
+	ndev->ntb->topo = NTB_TOPO_NONE;
+	ndev->ntb->ops = &vntb_epf_ops;
 
 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
 	if (ret) {
@@ -1478,7 +1488,7 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		return ret;
 	}
 
-	ret = ntb_register_device(&ndev->ntb);
+	ret = ntb_register_device(ndev->ntb);
 	if (ret) {
 		dev_err(dev, "Failed to register NTB device\n");
 		return ret;
@@ -1488,6 +1498,17 @@ static int pci_vntb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	return 0;
 }
 
+static void pci_vntb_remove(struct pci_dev *pdev)
+{
+	struct epf_ntb *ndev = (struct epf_ntb *)pdev->sysdata;
+
+	if (!ndev || !ndev->ntb)
+		return;
+
+	ntb_unregister_device(ndev->ntb);
+	ndev->ntb = NULL;
+}
+
 static struct pci_device_id pci_vntb_table[] = {
 	{
 		PCI_DEVICE(0xffff, 0xffff),
@@ -1499,6 +1520,7 @@ static struct pci_driver vntb_pci_driver = {
 	.name           = "pci-vntb",
 	.id_table       = pci_vntb_table,
 	.probe          = pci_vntb_probe,
+	.remove         = pci_vntb_remove,
 };
 
 /* ============ PCIe EPF Driver Bind ====================*/
@@ -1581,10 +1603,15 @@ static void epf_ntb_unbind(struct pci_epf *epf)
 {
 	struct epf_ntb *ntb = epf_get_drvdata(epf);
 
+	pci_unregister_driver(&vntb_pci_driver);
+
+	pci_lock_rescan_remove();
+	pci_stop_root_bus(ntb->vpci_bus);
+	pci_remove_root_bus(ntb->vpci_bus);
+	pci_unlock_rescan_remove();
+
 	epf_ntb_epc_cleanup(ntb);
 	epf_ntb_config_spad_bar_free(ntb);
-
-	pci_unregister_driver(&vntb_pci_driver);
 }
 
 // EPF driver probe
Signature · 1 line
2.51.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#10 lore raw
In reply to Koichiro Den
Quoted 23 lines · Koichiro Den

The vNTB endpoint function (pci-epf-vntb) can be configured and reconfigured through configfs (link/unlink functions, start/stop the controller, update parameters). In practice, several pitfalls present:
duplicate EPC teardown that leads to oopses, a work item running after resources were torn down, and inability to re-link/restart fundamentally because ntb_dev was embedded and the vPCI bus teardown was incomplete.

This series addresses those issues and hardens resource management of pci-epf-vntb:

- Remove duplicate EPC resource teardown in both pci-epf-vntb and pci-epf-ntb, avoiding crashes on .allow_link failures and during .drop_link.
- Stop the delayed cmd_handler work before clearing BARs/doorbells.
- Manage ntb_dev as a devm-managed allocation and implement .remove() in the vNTB PCI driver. Switch to pci_scan_root_bus().

With these changes, the controller can now be stopped, a function unlinked, configfs settings updated, and the controller re-linked and restarted without rebooting the endpoint, as long as the underlying pci_epc_ops .stop() is non-destructive and .start() restores normal operation.

Quoted 4 lines

Patches 1-3 carry Fixes tags and are candidates for stable. Patch 4 is a preparatory one for Patch 5.
Patch 5 is a behavioral improvement that completes lifetime management for relink/restart scenarios.

While I'm updating Patch 4 and 5 to address feedback from Mani, as well as the concern I mentioned at [1], I noticed that if [2] gets merged before this series, another issue may arrise. With [2], the DB IRQ may become a shared IRQ, in which case the unbind/remove race would require additional care.

Mani, if it's ok, could you take Patch 1-3?

- If so, I'll spin the rest (Patch 4-5) into a separate patch series starting from v6, with some additional commits.

It turns out that Patch 4-5 are a bigger change than I initially thought. Even though Patch 1-3 were originally written as preparatory fixes, they can be applied independently at any time.

The code in Patches 1-3 has also been unchanged since v1 (submitted last October).

[1] https://lore.kernel.org/linux-pci/mipdls67csyyrugf4rjx3qqtbxes4sjjtluy3psecnadcgcs7k@rn42d3m6ggsf/
[2] [PATCH v10 0/7] PCI: endpoint: pci-ep-msi: Add embedded doorbell fallback

    https://lore.kernel.org/linux-pci/[email protected]/

Best regards,
Koichiro

Quoted 50 lines

---
v4->v5 changes:
- Rebased onto the latest pci/endpoint (2026-02-26).
- Dropped [PATCH v4 1/7]; will be reposted separately via the NTB tree.
- Dropped [PATCH v4 2/7], which has been applied in a different form.
- Corrected the subject prefix of [PATCH v4 5/7]:
s/NTB: epf: vntb:/PCI: endpoint: pci-epf-vntb:/.
- Picked up a Reviewed-by tag to [PATCH v4 7/7].
- Resolved a conflict in [PATCH v4 7/7] due to commit

    dc693d606644 ("PCI: endpoint: pci-epf-vntb: Add MSI doorbell support").

v3->v4 changes:
- Added Reviewed-by tag for [PATCH v3 6/6].
- Corrected patch split by moving the blank-line cleanup, based on the feedback from Frank.
(No code changes overall.)
v2->v3 changes:
- Added Reviewed-by tag for [PATCH v2 4/6].
- Split [PATCH v2 6/6] into two, based on the feedback from Frank. (No code changes overall.)
v1->v2 changes:
- Incorporated feedback from Frank.
- Added Reviewed-by tags (except for patches #4 and #6).
- Fixed a typo in patch #5 title.
(No code changes overall.)

v4: https://lore.kernel.org/linux-pci/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Koichiro Den (5):
PCI: endpoint: pci-epf-vntb: Remove duplicate resource teardown
PCI: endpoint: pci-epf-ntb: Remove duplicate resource teardown
PCI: endpoint: pci-epf-vntb: Stop cmd_handler work in
epf_ntb_epc_cleanup
PCI: endpoint: pci-epf-vntb: Switch vpci_scan_bus() to use

    pci_scan_root_bus()

PCI: endpoint: pci-epf-vntb: manage ntb_dev lifetime and fix vpci bus
teardown

 drivers/pci/endpoint/functions/pci-epf-ntb.c  | 56 +-----------
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 88 ++++++++++++-------
 2 files changed, 57 insertions(+), 87 deletions(-)
Signature · 1 line
2.51.0
#11 lore raw
Quoted 35 lines · Koichiro Den
Quoted 23 lines · Koichiro Den

The vNTB endpoint function (pci-epf-vntb) can be configured and reconfigured through configfs (link/unlink functions, start/stop the controller, update parameters). In practice, several pitfalls present:
duplicate EPC teardown that leads to oopses, a work item running after resources were torn down, and inability to re-link/restart fundamentally because ntb_dev was embedded and the vPCI bus teardown was incomplete.

This series addresses those issues and hardens resource management of pci-epf-vntb:

- Remove duplicate EPC resource teardown in both pci-epf-vntb and pci-epf-ntb, avoiding crashes on .allow_link failures and during .drop_link.
- Stop the delayed cmd_handler work before clearing BARs/doorbells.
- Manage ntb_dev as a devm-managed allocation and implement .remove() in the vNTB PCI driver. Switch to pci_scan_root_bus().

With these changes, the controller can now be stopped, a function unlinked, configfs settings updated, and the controller re-linked and restarted without rebooting the endpoint, as long as the underlying pci_epc_ops .stop() is non-destructive and .start() restores normal operation.

Quoted 4 lines

Patches 1-3 carry Fixes tags and are candidates for stable. Patch 4 is a preparatory one for Patch 5.
Patch 5 is a behavioral improvement that completes lifetime management for relink/restart scenarios.

While I'm updating Patch 4 and 5 to address feedback from Mani, as well as the concern I mentioned at [1], I noticed that if [2] gets merged before this series, another issue may arrise. With [2], the DB IRQ may become a shared IRQ, in which case the unbind/remove race would require additional care.

Quoted 5 lines

Mani, if it's ok, could you take Patch 1-3?

- If so, I'll spin the rest (Patch 4-5) into a separate patch series starting from v6, with some additional commits.

Sounds OK to me.

- Mani

Quoted 65 lines

It turns out that Patch 4-5 are a bigger change than I initially thought. Even though Patch 1-3 were originally written as preparatory fixes, they can be applied independently at any time.

The code in Patches 1-3 has also been unchanged since v1 (submitted last October).

[1] https://lore.kernel.org/linux-pci/mipdls67csyyrugf4rjx3qqtbxes4sjjtluy3psecnadcgcs7k@rn42d3m6ggsf/
[2] [PATCH v10 0/7] PCI: endpoint: pci-ep-msi: Add embedded doorbell fallback

    https://lore.kernel.org/linux-pci/[email protected]/

Best regards,
Koichiro

Quoted 50 lines

---
v4->v5 changes:
- Rebased onto the latest pci/endpoint (2026-02-26).
- Dropped [PATCH v4 1/7]; will be reposted separately via the NTB tree.
- Dropped [PATCH v4 2/7], which has been applied in a different form.
- Corrected the subject prefix of [PATCH v4 5/7]:
s/NTB: epf: vntb:/PCI: endpoint: pci-epf-vntb:/.
- Picked up a Reviewed-by tag to [PATCH v4 7/7].
- Resolved a conflict in [PATCH v4 7/7] due to commit

    dc693d606644 ("PCI: endpoint: pci-epf-vntb: Add MSI doorbell support").

v3->v4 changes:
- Added Reviewed-by tag for [PATCH v3 6/6].
- Corrected patch split by moving the blank-line cleanup, based on the feedback from Frank.
(No code changes overall.)
v2->v3 changes:
- Added Reviewed-by tag for [PATCH v2 4/6].
- Split [PATCH v2 6/6] into two, based on the feedback from Frank. (No code changes overall.)
v1->v2 changes:
- Incorporated feedback from Frank.
- Added Reviewed-by tags (except for patches #4 and #6).
- Fixed a typo in patch #5 title.
(No code changes overall.)

v4: https://lore.kernel.org/linux-pci/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Koichiro Den (5):
PCI: endpoint: pci-epf-vntb: Remove duplicate resource teardown
PCI: endpoint: pci-epf-ntb: Remove duplicate resource teardown
PCI: endpoint: pci-epf-vntb: Stop cmd_handler work in
epf_ntb_epc_cleanup
PCI: endpoint: pci-epf-vntb: Switch vpci_scan_bus() to use

    pci_scan_root_bus()

PCI: endpoint: pci-epf-vntb: manage ntb_dev lifetime and fix vpci bus
teardown

 drivers/pci/endpoint/functions/pci-epf-ntb.c  | 56 +-----------
 drivers/pci/endpoint/functions/pci-epf-vntb.c | 88 ++++++++++++-------
 2 files changed, 57 insertions(+), 87 deletions(-)
Signature · 1 line
2.51.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#12 lore raw

Re: (subset) [PATCH v5 0/5] PCI: endpoint: pci-epf-*ntb: Harden vNTB resource management

In reply to Koichiro Den
Quoted 8 lines · Koichiro Den

The vNTB endpoint function (pci-epf-vntb) can be configured and reconfigured through configfs (link/unlink functions, start/stop the controller, update parameters). In practice, several pitfalls present:
duplicate EPC teardown that leads to oopses, a work item running after resources were torn down, and inability to re-link/restart fundamentally because ntb_dev was embedded and the vPCI bus teardown was incomplete.

[...]

Applied, thanks!

[1/5] PCI: endpoint: pci-epf-vntb: Remove duplicate resource teardown

      commit: 0da63230d3ec1ec5fcc443a2314233e95bfece54
[2/5] PCI: endpoint: pci-epf-ntb: Remove duplicate resource teardown
      commit: 3446beddba450c8d6f9aca2f028712ac527fead3
[3/5] PCI: endpoint: pci-epf-vntb: Stop cmd_handler work in epf_ntb_epc_cleanup
      commit: d799984233a50abd2667a7d17a9a710a3f10ebe2

Best regards,

Signature · 1 line
Manivannan Sadhasivam <[email protected]>
#13 lore raw

Re: (subset) [PATCH v5 0/5] PCI: endpoint: pci-epf-*ntb: Harden vNTB resource management

Quoted 11 lines · Manivannan Sadhasivam
Quoted 8 lines · Koichiro Den

The vNTB endpoint function (pci-epf-vntb) can be configured and reconfigured through configfs (link/unlink functions, start/stop the controller, update parameters). In practice, several pitfalls present:
duplicate EPC teardown that leads to oopses, a work item running after resources were torn down, and inability to re-link/restart fundamentally because ntb_dev was embedded and the vPCI bus teardown was incomplete.

[...]

Quoted 8 lines

Applied, thanks!

[1/5] PCI: endpoint: pci-epf-vntb: Remove duplicate resource teardown

      commit: 0da63230d3ec1ec5fcc443a2314233e95bfece54
[2/5] PCI: endpoint: pci-epf-ntb: Remove duplicate resource teardown
      commit: 3446beddba450c8d6f9aca2f028712ac527fead3
[3/5] PCI: endpoint: pci-epf-vntb: Stop cmd_handler work in epf_ntb_epc_cleanup
      commit: d799984233a50abd2667a7d17a9a710a3f10ebe2

Thanks for taking the subset, Mani.

I'll prepare the remaining patches as a separate series after taking another careful look.

Best regards,
Koichiro

Quoted 5 lines

Best regards,

Signature · 1 line
Manivannan Sadhasivam <[email protected]>