git: b163dcab2786 - main - pf: hoist the unlinked rules lock out of the mass rule removal loop

From: Mateusz Guzik <mjg_at_FreeBSD.org>
Date: Thu, 10 Mar 2022 17:21:12 UTC
The branch main has been updated by mjg:

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

commit b163dcab27862f90e0108a73185945456cafd2f3
Author:     Mateusz Guzik <mjg@FreeBSD.org>
AuthorDate: 2022-02-28 10:39:02 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2022-03-10 17:20:41 +0000

    pf: hoist the unlinked rules lock out of the mass rule removal loop
    
    Reviewed by:    kp
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/netpfil/pf/pf_ioctl.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/sys/netpfil/pf/pf_ioctl.c b/sys/netpfil/pf/pf_ioctl.c
index ef7092c0ed1b..68307c9e7bb3 100644
--- a/sys/netpfil/pf/pf_ioctl.c
+++ b/sys/netpfil/pf/pf_ioctl.c
@@ -474,16 +474,26 @@ pf_empty_kpool(struct pf_kpalist *poola)
 }
 
 static void
-pf_unlink_rule(struct pf_krulequeue *rulequeue, struct pf_krule *rule)
+pf_unlink_rule_locked(struct pf_krulequeue *rulequeue, struct pf_krule *rule)
 {
 
 	PF_RULES_WASSERT();
+	PF_UNLNKDRULES_ASSERT();
 
 	TAILQ_REMOVE(rulequeue, rule, entries);
 
-	PF_UNLNKDRULES_LOCK();
 	rule->rule_ref |= PFRULE_REFS;
 	TAILQ_INSERT_TAIL(&V_pf_unlinked_rules, rule, entries);
+}
+
+static void
+pf_unlink_rule(struct pf_krulequeue *rulequeue, struct pf_krule *rule)
+{
+
+	PF_RULES_WASSERT();
+
+	PF_UNLNKDRULES_LOCK();
+	pf_unlink_rule_locked(rulequeue, rule);
 	PF_UNLNKDRULES_UNLOCK();
 }
 
@@ -1338,8 +1348,10 @@ pf_commit_rules(u_int32_t ticket, int rs_num, char *anchor)
 	pf_calc_skip_steps(rs->rules[rs_num].active.ptr);
 
 	/* Purge the old rule list. */
+	PF_UNLNKDRULES_LOCK();
 	while ((rule = TAILQ_FIRST(old_rules)) != NULL)
-		pf_unlink_rule(old_rules, rule);
+		pf_unlink_rule_locked(old_rules, rule);
+	PF_UNLNKDRULES_UNLOCK();
 	if (rs->rules[rs_num].inactive.ptr_array)
 		free(rs->rules[rs_num].inactive.ptr_array, M_TEMP);
 	rs->rules[rs_num].inactive.ptr_array = NULL;