git: db775f50e947 - stable/13 - ena: Fix leaking ifmedia resources on detach

From: Osama Abboud <osamaabb_at_FreeBSD.org>
Date: Thu, 31 Oct 2024 16:00:16 UTC
The branch stable/13 has been updated by osamaabb:

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

commit db775f50e94744cfedd1f15e3aa5152ab1fbdcde
Author:     Osama Abboud <osamaabb@amazon.com>
AuthorDate: 2024-08-07 06:24:22 +0000
Commit:     Osama Abboud <osamaabb@FreeBSD.org>
CommitDate: 2024-10-31 14:55:21 +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)
    Sponsored by: Amazon, Inc.
    
    (cherry picked from commit 449496eb28daec8d5b852fa4be1e337c2957345c)
---
 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 1455d29a1f4f..a0d3cf28861e 100644
--- a/sys/dev/ena/ena.c
+++ b/sys/dev/ena/ena.c
@@ -3982,6 +3982,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:
@@ -4032,6 +4033,8 @@ ena_detach(device_t pdev)
 
 	ether_ifdetach(adapter->ifp);
 
+	ifmedia_removeall(&adapter->media);
+
 	/* Stop timer service */
 	ENA_LOCK_LOCK();
 	ENA_TIMER_DRAIN(adapter);