git: cf9b68575453 - main - Revert "ipfilter: Avoid allocating a new ipf token when not needed"

From: Cy Schubert <cy_at_FreeBSD.org>
Date: Mon, 07 Jul 2025 23:04:16 UTC
The branch main has been updated by cy:

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

commit cf9b6857545371ab5becc6a785c62bc844cb2f94
Author:     Cy Schubert <cy@FreeBSD.org>
AuthorDate: 2025-07-07 16:50:27 +0000
Commit:     Cy Schubert <cy@FreeBSD.org>
CommitDate: 2025-07-07 23:03:00 +0000

    Revert "ipfilter: Avoid allocating a new ipf token when not needed"
    
    malloc() outside of a write lock will reduce potential contention.
    
    MFC after:      3 days
    
    This reverts commit 7f5e3b9fa3d159b7f061b4d01a767cbe5d0527f3.
---
 sys/netpfil/ipfilter/netinet/fil.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/sys/netpfil/ipfilter/netinet/fil.c b/sys/netpfil/ipfilter/netinet/fil.c
index 2a75190a3ec7..2a4feb17dbd3 100644
--- a/sys/netpfil/ipfilter/netinet/fil.c
+++ b/sys/netpfil/ipfilter/netinet/fil.c
@@ -7455,6 +7455,10 @@ ipf_token_find(ipf_main_softc_t *softc, int type, int uid, void *ptr)
 {
 	ipftoken_t *it, *new;
 
+	KMALLOC(new, ipftoken_t *);
+	if (new != NULL)
+		bzero((char *)new, sizeof(*new));
+
 	WRITE_ENTER(&softc->ipf_tokens);
 	for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) {
 		if ((ptr == it->ipt_ctx) && (type == it->ipt_type) &&
@@ -7463,10 +7467,6 @@ ipf_token_find(ipf_main_softc_t *softc, int type, int uid, void *ptr)
 	}
 
 	if (it == NULL) {
-		KMALLOC(new, ipftoken_t *);
-		if (new != NULL)
-			bzero((char *)new, sizeof(*new));
-
 		it = new;
 		new = NULL;
 		if (it == NULL) {
@@ -7478,6 +7478,11 @@ ipf_token_find(ipf_main_softc_t *softc, int type, int uid, void *ptr)
 		it->ipt_type = type;
 		it->ipt_ref = 1;
 	} else {
+		if (new != NULL) {
+			KFREE(new);
+			new = NULL;
+		}
+
 		if (it->ipt_complete > 0)
 			it = NULL;
 		else