git: 449496eb28da - main - ena: Fix leaking ifmedia resources on detach

From: Osama Abboud <osamaabb_at_FreeBSD.org>
Date: Tue, 15 Oct 2024 17:43:22 UTC
The branch main has been updated by osamaabb:

URL: https://cgit.FreeBSD.org/src/commit/?id=449496eb28daec8d5b852fa4be1e337c2957345c

commit 449496eb28daec8d5b852fa4be1e337c2957345c
Author:     Osama Abboud <osamaabb@amazon.com>
AuthorDate: 2024-08-07 06:24:22 +0000
Commit:     Osama Abboud <osamaabb@FreeBSD.org>
CommitDate: 2024-10-15 17:38:32 +0000

    ena: Fix leaking ifmedia resources on detach
    
    ifmedia_add() allocates an ifmedia_entry during ena_attach.
    Current code doesn't release this memory during ena_detach()
    
    This commit calls ifmedia_removeall() to properly free the
    allocated memory during ena_detach().
    
    Also, in case ena_attach fails, we need to detach ifmedia
    which was allocated within ena_setup_ifnet().
    
    This bug was first described in:
    https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278100
    
    Reviewed by: zlei
    Approved by: cperciva (mentor)
    MFC after: 2 weeks
    Sponsored by: Amazon, Inc.
---
 sys/dev/ena/ena.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/sys/dev/ena/ena.c b/sys/dev/ena/ena.c
index 28a4d7842306..e9d4530e9085 100644
--- a/sys/dev/ena/ena.c
+++ b/sys/dev/ena/ena.c
@@ -3981,6 +3981,7 @@ ena_attach(device_t pdev)
 #ifdef DEV_NETMAP
 err_detach:
 	ether_ifdetach(adapter->ifp);
+	ifmedia_removeall(&adapter->media);
 	free(adapter->customer_metrics_array, M_DEVBUF);
 #endif /* DEV_NETMAP */
 err_metrics_buffer_destroy:
@@ -4031,6 +4032,8 @@ ena_detach(device_t pdev)
 
 	ether_ifdetach(adapter->ifp);
 
+	ifmedia_removeall(&adapter->media);
+
 	/* Stop timer service */
 	ENA_LOCK_LOCK();
 	ENA_TIMER_DRAIN(adapter);