git: bbe62559c78f - main - rlimit: line up with other clean up in thread_reap_domain

From: Mateusz Guzik <mjg_at_FreeBSD.org>
Date: Sat, 20 May 2023 12:48:28 UTC
The branch main has been updated by mjg:

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

commit bbe62559c78f22e2774e12a058981ee8e46afbee
Author:     Mateusz Guzik <mjg@FreeBSD.org>
AuthorDate: 2022-08-19 19:37:33 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2023-05-20 12:46:46 +0000

    rlimit: line up with other clean up in thread_reap_domain
    
    NFC
---
 sys/kern/kern_resource.c | 27 +++++++++++++++++++++++++++
 sys/kern/kern_thread.c   | 24 +++++++++---------------
 sys/sys/resourcevar.h    | 22 ++++++++++++++++++++++
 3 files changed, 58 insertions(+), 15 deletions(-)

diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c
index be81f97847de..b1ce9526d4cc 100644
--- a/sys/kern/kern_resource.c
+++ b/sys/kern/kern_resource.c
@@ -1276,6 +1276,33 @@ lim_freen(struct plimit *limp, int n)
 		free((void *)limp, M_PLIMIT);
 }
 
+void
+limbatch_add(struct limbatch *lb, struct thread *td)
+{
+	struct plimit *limp;
+
+	MPASS(td->td_limit != NULL);
+	limp = td->td_limit;
+
+	if (lb->limp != limp) {
+		if (lb->count != 0) {
+			lim_freen(lb->limp, lb->count);
+			lb->count = 0;
+		}
+		lb->limp = limp;
+	}
+
+	lb->count++;
+}
+
+void
+limbatch_final(struct limbatch *lb)
+{
+
+	MPASS(lb->count != 0);
+	lim_freen(lb->limp, lb->count);
+}
+
 /*
  * Make a copy of the plimit structure.
  * We share these structures copy-on-write after fork.
diff --git a/sys/kern/kern_thread.c b/sys/kern/kern_thread.c
index b62bfafa58be..585531d3ab3a 100644
--- a/sys/kern/kern_thread.c
+++ b/sys/kern/kern_thread.c
@@ -588,9 +588,8 @@ thread_reap_domain(struct thread_domain_data *tdd)
 	struct thread *itd, *ntd;
 	struct tidbatch tidbatch;
 	struct credbatch credbatch;
+	struct limbatch limbatch;
 	int tdcount;
-	struct plimit *lim;
-	int limcount;
 
 	/*
 	 * Reading upfront is pessimal if followed by concurrent atomic_swap,
@@ -612,42 +611,37 @@ thread_reap_domain(struct thread_domain_data *tdd)
 
 	tidbatch_prep(&tidbatch);
 	credbatch_prep(&credbatch);
+	limbatch_prep(&limbatch);
 	tdcount = 0;
-	lim = NULL;
-	limcount = 0;
 
 	while (itd != NULL) {
 		ntd = itd->td_zombie;
 		EVENTHANDLER_DIRECT_INVOKE(thread_dtor, itd);
+
 		tidbatch_add(&tidbatch, itd);
 		credbatch_add(&credbatch, itd);
-		MPASS(itd->td_limit != NULL);
-		if (lim != itd->td_limit) {
-			if (limcount != 0) {
-				lim_freen(lim, limcount);
-				limcount = 0;
-			}
-		}
-		lim = itd->td_limit;
-		limcount++;
+		limbatch_add(&limbatch, itd);
+
 		thread_free_batched(itd);
+
 		tidbatch_process(&tidbatch);
 		credbatch_process(&credbatch);
+		limbatch_process(&limbatch);
 		tdcount++;
 		if (tdcount == 32) {
 			thread_count_sub(tdcount);
 			tdcount = 0;
 		}
+
 		itd = ntd;
 	}
 
 	tidbatch_final(&tidbatch);
 	credbatch_final(&credbatch);
+	limbatch_final(&limbatch);
 	if (tdcount != 0) {
 		thread_count_sub(tdcount);
 	}
-	MPASS(limcount != 0);
-	lim_freen(lim, limcount);
 }
 
 /*
diff --git a/sys/sys/resourcevar.h b/sys/sys/resourcevar.h
index bd57211c75f0..8b88c9321e70 100644
--- a/sys/sys/resourcevar.h
+++ b/sys/sys/resourcevar.h
@@ -82,6 +82,28 @@ struct plimit {
 	int	pl_refcnt;		/* number of references */
 };
 
+struct limbatch {
+	struct plimit *limp;
+	int count;
+};
+
+static inline void
+limbatch_prep(struct limbatch *lb)
+{
+        lb->limp = NULL;
+        lb->count = 0;
+}
+
+void    limbatch_add(struct limbatch *lb, struct thread *td);
+
+static inline void
+limbatch_process(struct limbatch *lb __unused)
+{
+
+}
+
+void    limbatch_final(struct limbatch *lb);
+
 struct racct;
 
 /*-