git: 09a1893398fd - main - netmap: fix refcount bug in netmap allocator

From: Vincenzo Maffione <vmaffione_at_FreeBSD.org>
Date: Sun, 06 Mar 2022 16:40:03 UTC
The branch main has been updated by vmaffione:

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

commit 09a1893398fdcd1d000dae1cb3fd5239c0bfb360
Author:     Vincenzo Maffione <vmaffione@FreeBSD.org>
AuthorDate: 2022-03-06 16:39:16 +0000
Commit:     Vincenzo Maffione <vmaffione@FreeBSD.org>
CommitDate: 2022-03-06 16:39:16 +0000

    netmap: fix refcount bug in netmap allocator
    
    Symptom: when a single extmem memory region is provided to netmap
    multiple times, for multiple interfaces, the memory region is
    never released by netmap once all the existing file descriptors
    are closed.
    
    Fix the relevant condition in netmap_mem_drop(): release the memory
    when the last user of netmap_adapter is gone, rather then when
    the last user of netmap_mem_d is gone.
    
    MFC after:      2 weeks
---
 sys/dev/netmap/netmap.c      | 10 ++++++----
 sys/dev/netmap/netmap_mem2.c |  2 +-
 sys/net/netmap_user.h        |  2 +-
 3 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/sys/dev/netmap/netmap.c b/sys/dev/netmap/netmap.c
index ed3154a524db..17b6eeaac085 100644
--- a/sys/dev/netmap/netmap.c
+++ b/sys/dev/netmap/netmap.c
@@ -994,10 +994,12 @@ netmap_mem_restore(struct netmap_adapter *na)
 static void
 netmap_mem_drop(struct netmap_adapter *na)
 {
-	/* if the native allocator had been overridden on regif,
-	 * restore it now and drop the temporary one
-	 */
-	if (netmap_mem_deref(na->nm_mem, na)) {
+	netmap_mem_deref(na->nm_mem, na);
+
+	if (na->active_fds <= 0) {
+		/* if the native allocator had been overridden on regif,
+		 * restore it now and drop the temporary one
+		 */
 		netmap_mem_restore(na);
 	}
 }
diff --git a/sys/dev/netmap/netmap_mem2.c b/sys/dev/netmap/netmap_mem2.c
index 2b41af761c04..8575a940961e 100644
--- a/sys/dev/netmap/netmap_mem2.c
+++ b/sys/dev/netmap/netmap_mem2.c
@@ -323,7 +323,7 @@ netmap_mem_get_id(struct netmap_mem_d *nmd)
 
 #ifdef NM_DEBUG_MEM_PUTGET
 #define NM_DBG_REFC(nmd, func, line)	\
-	nm_prinf("%d mem[%d:%d] -> %d", line, (nmd)->nm_id, (nmd)->nm_grp, (nmd)->refcount);
+	nm_prinf("%s:%d mem[%d:%d] -> %d", func, line, (nmd)->nm_id, (nmd)->nm_grp, (nmd)->refcount);
 #else
 #define NM_DBG_REFC(nmd, func, line)
 #endif
diff --git a/sys/net/netmap_user.h b/sys/net/netmap_user.h
index e17d2dcbe357..e8983565ddf0 100644
--- a/sys/net/netmap_user.h
+++ b/sys/net/netmap_user.h
@@ -986,7 +986,7 @@ nm_close(struct nm_desc *d)
 		close(d->fd);
 	}
 
-	bzero(d, sizeof(*d));
+	bzero((char *)d, sizeof(*d));
 	free(d);
 	return 0;
 }