git: 9b8db66402e9 - main - netgraph: provide separate malloc type for nodes that are missing it

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 31 May 2024 16:20:02 UTC
The branch main has been updated by glebius:

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

commit 9b8db66402e94919ab47fc2d69863968e49e305a
Author:     Dmitry Lukhtionov <dmitryluhtionov@gmail.com>
AuthorDate: 2024-05-31 16:19:54 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-05-31 16:19:54 +0000

    netgraph: provide separate malloc type for nodes that are missing it
    
    The kernel option NG_SEPARATE_MALLOC helps to debug memory leaks in
    netgraph(4).  Several nodes were missing the support.
---
 sys/netgraph/ng_car.c     | 10 ++++++++--
 sys/netgraph/ng_nat.c     | 26 ++++++++++++++++----------
 sys/netgraph/ng_pptpgre.c | 20 +++++++++++++-------
 sys/netgraph/ng_tcpmss.c  | 10 ++++++++--
 sys/netgraph/ng_tee.c     | 10 ++++++++--
 5 files changed, 53 insertions(+), 23 deletions(-)

diff --git a/sys/netgraph/ng_car.c b/sys/netgraph/ng_car.c
index a4e8c0dd55d6..e828306bd7d5 100644
--- a/sys/netgraph/ng_car.c
+++ b/sys/netgraph/ng_car.c
@@ -51,6 +51,12 @@
 
 #include "qos.h"
 
+#ifdef NG_SEPARATE_MALLOC
+static MALLOC_DEFINE(M_NETGRAPH_CAR, "netgraph_car", "netgraph car node");
+#else
+#define M_NETGRAPH_CAR M_NETGRAPH
+#endif
+
 #define NG_CAR_QUEUE_SIZE	100	/* Maximum queue size for SHAPE mode */
 #define NG_CAR_QUEUE_MIN_TH	8	/* Minimum RED threshold for SHAPE mode */
 
@@ -189,7 +195,7 @@ ng_car_constructor(node_p node)
 	priv_p priv;
 
 	/* Initialize private descriptor. */
-	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
+	priv = malloc(sizeof(*priv), M_NETGRAPH_CAR, M_WAITOK | M_ZERO);
 
 	NG_NODE_SET_PRIVATE(node, priv);
 	priv->node = node;
@@ -571,7 +577,7 @@ ng_car_shutdown(node_p node)
 	mtx_destroy(&priv->upper.q_mtx);
 	mtx_destroy(&priv->lower.q_mtx);
 	NG_NODE_UNREF(priv->node);
-	free(priv, M_NETGRAPH);
+	free(priv, M_NETGRAPH_CAR);
 	return (0);
 }
 
diff --git a/sys/netgraph/ng_nat.c b/sys/netgraph/ng_nat.c
index d7492b71e07c..e9de0e5b60c8 100644
--- a/sys/netgraph/ng_nat.c
+++ b/sys/netgraph/ng_nat.c
@@ -53,6 +53,12 @@
 #include <netgraph/ng_nat.h>
 #include <netgraph/netgraph.h>
 
+#ifdef NG_SEPARATE_MALLOC
+static MALLOC_DEFINE(M_NETGRAPH_NAT, "netgraph_nat", "netgraph nat node");
+#else
+#define M_NETGRAPH_NAT M_NETGRAPH
+#endif
+
 static ng_constructor_t	ng_nat_constructor;
 static ng_rcvmsg_t	ng_nat_rcvmsg;
 static ng_shutdown_t	ng_nat_shutdown;
@@ -306,7 +312,7 @@ ng_nat_constructor(node_p node)
 	priv_p priv;
 
 	/* Initialize private descriptor. */
-	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
+	priv = malloc(sizeof(*priv), M_NETGRAPH_NAT, M_WAITOK | M_ZERO);
 
 	/* Init aliasing engine. */
 	priv->lib = LibAliasInit(NULL);
@@ -422,7 +428,7 @@ ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
 			}
 
 			if ((entry = malloc(sizeof(struct ng_nat_rdr_lst),
-			    M_NETGRAPH, M_NOWAIT | M_ZERO)) == NULL) {
+			    M_NETGRAPH_NAT, M_NOWAIT | M_ZERO)) == NULL) {
 				error = ENOMEM;
 				break;
 			}
@@ -436,7 +442,7 @@ ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
 
 			if (entry->lnk == NULL) {
 				error = ENOMEM;
-				free(entry, M_NETGRAPH);
+				free(entry, M_NETGRAPH_NAT);
 				break;
 			}
 
@@ -481,7 +487,7 @@ ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
 			}
 
 			if ((entry = malloc(sizeof(struct ng_nat_rdr_lst),
-			    M_NETGRAPH, M_NOWAIT | M_ZERO)) == NULL) {
+			    M_NETGRAPH_NAT, M_NOWAIT | M_ZERO)) == NULL) {
 				error = ENOMEM;
 				break;
 			}
@@ -492,7 +498,7 @@ ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
 
 			if (entry->lnk == NULL) {
 				error = ENOMEM;
-				free(entry, M_NETGRAPH);
+				free(entry, M_NETGRAPH_NAT);
 				break;
 			}
 
@@ -533,7 +539,7 @@ ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
 			}
 
 			if ((entry = malloc(sizeof(struct ng_nat_rdr_lst),
-			    M_NETGRAPH, M_NOWAIT | M_ZERO)) == NULL) {
+			    M_NETGRAPH_NAT, M_NOWAIT | M_ZERO)) == NULL) {
 				error = ENOMEM;
 				break;
 			}
@@ -545,7 +551,7 @@ ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
 
 			if (entry->lnk == NULL) {
 				error = ENOMEM;
-				free(entry, M_NETGRAPH);
+				free(entry, M_NETGRAPH_NAT);
 				break;
 			}
 
@@ -611,7 +617,7 @@ ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
 			/* Delete entry from our internal list. */
 			priv->rdrcount--;
 			STAILQ_REMOVE(&priv->redirhead, entry, ng_nat_rdr_lst, entries);
-			free(entry, M_NETGRAPH);
+			free(entry, M_NETGRAPH_NAT);
 		    }
 			break;
 		case NGM_NAT_ADD_SERVER:
@@ -912,12 +918,12 @@ ng_nat_shutdown(node_p node)
 	while (!STAILQ_EMPTY(&priv->redirhead)) {
 		struct ng_nat_rdr_lst *entry = STAILQ_FIRST(&priv->redirhead);
 		STAILQ_REMOVE_HEAD(&priv->redirhead, entries);
-		free(entry, M_NETGRAPH);
+		free(entry, M_NETGRAPH_NAT);
 	}
 
 	/* Final free. */
 	LibAliasUninit(priv->lib);
-	free(priv, M_NETGRAPH);
+	free(priv, M_NETGRAPH_NAT);
 
 	return (0);
 }
diff --git a/sys/netgraph/ng_pptpgre.c b/sys/netgraph/ng_pptpgre.c
index 8bd866c2881a..ae55a8dae5fd 100644
--- a/sys/netgraph/ng_pptpgre.c
+++ b/sys/netgraph/ng_pptpgre.c
@@ -73,6 +73,12 @@
 #include <netgraph/ng_parse.h>
 #include <netgraph/ng_pptpgre.h>
 
+#ifdef NG_SEPARATE_MALLOC
+static MALLOC_DEFINE(M_NETGRAPH_PPTP, "netgraph_pptp", "netgraph pptpgre node");
+#else
+#define M_NETGRAPH_PPTP M_NETGRAPH
+#endif
+
 /* GRE packet format, as used by PPTP */
 struct greheader {
 #if BYTE_ORDER == LITTLE_ENDIAN
@@ -320,7 +326,7 @@ ng_pptpgre_constructor(node_p node)
 	int i;
 
 	/* Allocate private structure */
-	priv = malloc(sizeof(*priv), M_NETGRAPH, M_WAITOK | M_ZERO);
+	priv = malloc(sizeof(*priv), M_NETGRAPH_PPTP, M_WAITOK | M_ZERO);
 
 	NG_NODE_SET_PRIVATE(node, priv);
 
@@ -380,7 +386,7 @@ ng_pptpgre_newhook(node_p node, hook_p hook, const char *name)
 		if (hex[i] != '\0')
 			return (EINVAL);
 
-		hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
+		hpriv = malloc(sizeof(*hpriv), M_NETGRAPH_PPTP, M_NOWAIT | M_ZERO);
 		if (hpriv == NULL)
 			return (ENOMEM);
 
@@ -542,7 +548,7 @@ ng_pptpgre_disconnect(hook_p hook)
 
 		LIST_REMOVE(hpriv, sessions);
 		mtx_destroy(&hpriv->mtx);
-		free(hpriv, M_NETGRAPH);
+		free(hpriv, M_NETGRAPH_PPTP);
 	}
 
 	/* Go away if no longer connected to anything */
@@ -566,7 +572,7 @@ ng_pptpgre_shutdown(node_p node)
 	LIST_REMOVE(&priv->uppersess, sessions);
 	mtx_destroy(&priv->uppersess.mtx);
 
-	free(priv, M_NETGRAPH);
+	free(priv, M_NETGRAPH_PPTP);
 
 	/* Decrement ref count */
 	NG_NODE_UNREF(node);
@@ -731,7 +737,7 @@ ng_pptpgre_sendq(const hpriv_p hpriv, roqh *q, const struct ng_pptpgre_roq *st)
 		NGI_GET_M(np->item, m);
 		NG_FWD_NEW_DATA(error, np->item, hpriv->hook, m);
 		if (np != st)
-			free(np, M_NETGRAPH);
+			free(np, M_NETGRAPH_PPTP);
 	}
 	return (error);
 }
@@ -967,7 +973,7 @@ badAck:
 	hpriv->recvSeq = np->seq;
 
 enqueue:
-	np = malloc(sizeof(*np), M_NETGRAPH, M_NOWAIT | M_ZERO);
+	np = malloc(sizeof(*np), M_NETGRAPH_PPTP, M_NOWAIT | M_ZERO);
 	if (np == NULL) {
 		priv->stats.memoryFailures++;
 		/*
@@ -1263,7 +1269,7 @@ ng_pptpgre_reset(hpriv_p hpriv)
 		np = SLIST_FIRST(&hpriv->roq);
 		SLIST_REMOVE_HEAD(&hpriv->roq, next);
 		NG_FREE_ITEM(np->item);
-		free(np, M_NETGRAPH);
+		free(np, M_NETGRAPH_PPTP);
 	}
 	hpriv->roq_len = 0;
 }
diff --git a/sys/netgraph/ng_tcpmss.c b/sys/netgraph/ng_tcpmss.c
index 3c5b4051ed92..02fa097369ad 100644
--- a/sys/netgraph/ng_tcpmss.c
+++ b/sys/netgraph/ng_tcpmss.c
@@ -63,6 +63,12 @@
 #include <netgraph/ng_parse.h>
 #include <netgraph/ng_tcpmss.h>
 
+#ifdef NG_SEPARATE_MALLOC
+static MALLOC_DEFINE(M_NETGRAPH_TCPMSS, "netgraph_tcpmss", "netgraph tcpmss node");
+#else
+#define M_NETGRAPH_TCPMSS M_NETGRAPH
+#endif
+
 /* Per hook info. */
 typedef struct {
 	hook_p				outHook;
@@ -159,7 +165,7 @@ ng_tcpmss_newhook(node_p node, hook_p hook, const char *name)
 {
 	hpriv_p priv;
 
-	priv = malloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
+	priv = malloc(sizeof(*priv), M_NETGRAPH_TCPMSS, M_NOWAIT | M_ZERO);
 	if (priv == NULL)
 		return (ENOMEM);
 
@@ -371,7 +377,7 @@ ng_tcpmss_disconnect(hook_p hook)
 			priv->outHook = NULL;
 	}
 
-	free(NG_HOOK_PRIVATE(hook), M_NETGRAPH);
+	free(NG_HOOK_PRIVATE(hook), M_NETGRAPH_TCPMSS);
 
 	if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
 		ng_rmnode_self(NG_HOOK_NODE(hook));
diff --git a/sys/netgraph/ng_tee.c b/sys/netgraph/ng_tee.c
index 391355cd756a..b2cceda29a40 100644
--- a/sys/netgraph/ng_tee.c
+++ b/sys/netgraph/ng_tee.c
@@ -60,6 +60,12 @@
 #include <netgraph/ng_parse.h>
 #include <netgraph/ng_tee.h>
 
+#ifdef NG_SEPARATE_MALLOC
+static MALLOC_DEFINE(M_NETGRAPH_TEE, "netgraph_tee", "netgraph tee node");
+#else
+#define M_NETGRAPH_TEE M_NETGRAPH
+#endif
+
 /* Per hook info */
 struct hookinfo {
 	hook_p			hook;
@@ -151,7 +157,7 @@ ng_tee_constructor(node_p node)
 {
 	sc_p privdata;
 
-	privdata = malloc(sizeof(*privdata), M_NETGRAPH, M_WAITOK | M_ZERO);
+	privdata = malloc(sizeof(*privdata), M_NETGRAPH_TEE, M_WAITOK | M_ZERO);
 
 	NG_NODE_SET_PRIVATE(node, privdata);
 	return (0);
@@ -352,7 +358,7 @@ ng_tee_shutdown(node_p node)
 	const sc_p privdata = NG_NODE_PRIVATE(node);
 
 	NG_NODE_SET_PRIVATE(node, NULL);
-	free(privdata, M_NETGRAPH);
+	free(privdata, M_NETGRAPH_TEE);
 	NG_NODE_UNREF(node);
 	return (0);
 }