Skip to contentSkip to conversation

net: ena: reset the device before freeing admin queues on restore error

PATCH New Yuho Choi 3 participants 3 messages Nov 30 – Dec 1

Subsystems (3)
lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 net: ena: reset the device before freeing admin queues on restore error

The ena_restore_device() error path frees the admin queues before it resets the device, so the device can still write AENQ events, such as the keep-alive, into freed memory. Reset the device first, as ena_destroy_device() does.

Fixes d7703ddbd7c9 ("net: ena: fix rare bug when failed restart/resume is followed by driver removal")

---
I used an LLM to help write this patch and its changelog.

Compile-tested only: QEMU has no ENA device, and KASAN cannot see writes made by the device.

 drivers/net/ethernet/amazon/ena/ena_netdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/amazon/ena/ena_netdev.c +1 -1
index 560b20a5861c..5ef9c0967bb4 100644
@@ -3370,8 +3370,8 @@ int ena_restore_device(struct ena_adapter *adapter)
 err_device_destroy:
 	ena_com_abort_admin_commands(ena_dev);
 	ena_com_wait_for_abort_completion(ena_dev);
-	ena_com_admin_destroy(ena_dev);
 	ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
+	ena_com_admin_destroy(ena_dev);
 	ena_phc_destroy(adapter);
 	ena_com_mmio_reg_read_request_destroy(ena_dev);
 err:

base-commit: fad0d57b12f6979bc0127f0a4f704c6d7177ca78

Signature · 1 line
2.56.1
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#2 lore raw
In reply to Yuho Choi
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 (2) · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The patch is titled "reset the device before freeing admin queues on restore error", but one restore failure still frees the…

Pre-existing issues:
- [Medium] In the err_device_destroy path of ena_restore_device() (ena_netdev.c:3373-3374), the return value of ena_com_dev_reset() is…
- [Medium] Each ena_destroy_device() -> ena_restore_device() cycle leaks the 4 KiB coherent DMA host_info buffer.

--

net: ena: reset the device before freeing admin queues on restore error

This patch moves the ena_com_dev_reset() call in the err_device_destroy path of ena_restore_device() so that it runs before ena_com_admin_destroy(). The goal is to stop the device from writing AENQ events into admin queue memory that has already been freed. This matches the order used in ena_destroy_device().

drivers/net/ethernet/amazon/ena/ena_netdev.c int ena_restore_device(struct ena_adapter *adapter) · line 3374

Quoted 11 lines
drivers/net/ethernet/amazon/ena/ena_netdev.c +1 -1
index 560b20a5861c8..5ef9c0967bb4a 100644
@@ -3370,8 +3370,8 @@ int ena_restore_device(struct ena_adapter *adapter)
 err_device_destroy:
 	ena_com_abort_admin_commands(ena_dev);
 	ena_com_wait_for_abort_completion(ena_dev);
-	ena_com_admin_destroy(ena_dev);
 	ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
+	ena_com_admin_destroy(ena_dev);

[Severity: Medium]
This is a pre-existing issue, not one introduced by this patch. The return value of ena_com_dev_reset() is ignored here. The AQ, ACQ and AENQ rings are freed whether or not the reset actually happened.

ena_com_dev_reset() can return before it writes the reset bit:

drivers/net/ethernet/amazon/ena/ena_com.c:ena_com_dev_reset() {
    ...
	if (unlikely((stat == ENA_MMIO_READ_TIMEOUT) || (cap == ENA_MMIO_READ_TIMEOUT))) {
		netdev_err(ena_dev->net_device, "Reg read32 timeout occurred\n");
		return -ETIME;
	}

	if ((stat & ENA_REGS_DEV_STS_READY_MASK) == 0) {
		...
		return -EINVAL;
	}
    ...
	if (timeout == 0) {
		...
		return -EINVAL;
	}
    ...
}

It can also fail if the reset indication never turns on or never turns off.

In those cases the device still has the old ring base addresses programmed. There is no fallback here, such as pci_clear_master().

Could the device then DMA into the freed coherent rings? This error path can be reached when ena_up() fails because of admin timeouts. That is also when the device is most likely to be unhealthy.

ena_destroy_device() follows the same pattern. It stores rc from ena_com_dev_reset() but frees the rings anyway.

drivers/net/ethernet/amazon/ena/ena_netdev.c int ena_restore_device(struct ena_adapter *adapter) · line 3376

Quoted 2 lines
drivers/net/ethernet/amazon/ena/ena_netdev.c +0 -0
 	ena_phc_destroy(adapter);
 	ena_com_mmio_reg_read_request_destroy(ena_dev);

[Severity: Medium]
This isn't a bug introduced by this patch, but does each ena_destroy_device() -> ena_restore_device() cycle leak the 4 KiB host_info DMA buffer?

Every call to ena_device_init() goes through ena_config_host_info(), which calls ena_com_allocate_host_info(). That function overwrites the previous pointer without checking it:

drivers/net/ethernet/amazon/ena/ena_com.c:ena_com_allocate_host_info() {
    ...
	host_attr->host_info = dma_alloc_coherent(ena_dev->dmadev, SZ_4K,
						  &host_attr->host_info_dma_addr, GFP_KERNEL);
    ...
}

Neither ena_destroy_device() nor this err_device_destroy path calls ena_com_delete_host_info(), so the old buffer is orphaned at the next restore.

The only callers of ena_com_delete_host_info() are:

- the ena_config_host_info() error path
- err_admin_init in ena_device_init()
- err_device_destroy in ena_probe()
- __ena_shutoff()

__ena_shutoff() only frees the most recent buffer.

The cycle can be triggered by any of these:

- the reset task (ena_fw_reset_device())
- suspend/resume
- devlink reload
- the XDP setup failure path
- toggling the large LLQ header through the ethtool ring parameters

drivers/net/ethernet/amazon/ena/ena_netdev.c int ena_restore_device(struct ena_adapter *adapter) · line 3377

Quoted 1 line
drivers/net/ethernet/amazon/ena/ena_netdev.c +0 -0
 err:

[Severity: Medium]
When ena_device_init() fails, does ena_restore_device() still free the admin queues on a device that was never reset?

On that failure ena_restore_device() jumps straight to err:

	rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx, &wd_state);
	if (rc) {
		dev_err(&pdev->dev, "Can not initialize device\n");
		goto err;
	}

The err label only clears flags.

Inside ena_device_init(), the only ena_com_dev_reset() runs before ena_com_admin_init(), which writes the AQ, ACQ and AENQ base addresses into the device. After that, ena_com_get_dev_attr_feat(), ena_com_set_aenq_config(), ena_set_queues_placement_policy() and ena_calc_io_queue_size() can each fail and jump to err_admin_init:

drivers/net/ethernet/amazon/ena/ena_netdev.c:ena_device_init() {
    ...
err_admin_init:
	ena_com_abort_admin_commands(ena_dev);
	ena_com_wait_for_abort_completion(ena_dev);
	ena_com_delete_host_info(ena_dev);
	ena_com_admin_destroy(ena_dev);
    ...
}

That path frees the rings without resetting the device first. The kernel-doc for ena_com_admin_destroy() in ena_com.h says:

* @note: Before calling this method, the caller must validate that the device
* won't send any additional admin completions/aenq.
* To achieve that, a FLR is recommended.

One way to get there is an admin command in polling mode that times out. Examples are set_host_attributes from ena_config_host_info() or one of the get_feature commands. On timeout, ena_com_wait_and_process_admin_cq_polling() releases the comp_ctx. outstanding_cmds then drops to 0, so ena_com_wait_for_abort_completion() returns immediately.

Could the device then post the late completion into the freed ACQ, or read the freed host_info buffer?

The device is not reset later on this path either. A subsequent ena_destroy_device() returns early because ENA_FLAG_DEVICE_RUNNING is clear.

The AENQ keep-alive case from the commit message should not apply here, because ena_com_admin_aenq_enable() has not run yet. The concern is late admin completions.

ena_probe() has the same gap at its err_device_destroy label. It is reached from earlier probe failures and through err_worker_destroy. Neither route passes through the ena_com_dev_reset() in err_free_msix.

Would it make sense to also call ena_com_dev_reset() before ena_com_admin_destroy() in err_admin_init?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130000647.1158646-2-oss.patchbox%40gmail.com