git: 70aba9512b53 - stable/13 - kqueue: replace kq_ncallouts loop with atomic_fetchadd

Mateusz Guzik mjg at FreeBSD.org
Mon Jun 7 00:49:52 UTC 2021


The branch stable/13 has been updated by mjg:

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

commit 70aba9512b534a4b1a6601b8cbba1988da5b3718
Author:     Mateusz Guzik <mjg at FreeBSD.org>
AuthorDate: 2021-06-02 15:14:58 +0000
Commit:     Mateusz Guzik <mjg at FreeBSD.org>
CommitDate: 2021-06-07 00:49:46 +0000

    kqueue: replace kq_ncallouts loop with atomic_fetchadd
    
    (cherry picked from commit c9f8dcda856c50325190326a618dc251311bc43a)
---
 sys/kern/kern_event.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c
index 6b8ab11cb2dc..c277ac085d62 100644
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -206,7 +206,7 @@ static struct filterops user_filtops = {
 };
 
 static uma_zone_t	knote_zone;
-static unsigned int	kq_ncallouts = 0;
+static unsigned int __exclusive_cache_line	kq_ncallouts;
 static unsigned int 	kq_calloutmax = 4 * 1024;
 SYSCTL_UINT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
     &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
@@ -813,18 +813,16 @@ filt_timerattach(struct knote *kn)
 {
 	struct kq_timer_cb_data *kc;
 	sbintime_t to;
-	unsigned int ncallouts;
 	int error;
 
 	error = filt_timervalidate(kn, &to);
 	if (error != 0)
 		return (error);
 
-	do {
-		ncallouts = kq_ncallouts;
-		if (ncallouts >= kq_calloutmax)
-			return (ENOMEM);
-	} while (!atomic_cmpset_int(&kq_ncallouts, ncallouts, ncallouts + 1));
+	if (atomic_fetchadd_int(&kq_ncallouts, 1) + 1 > kq_calloutmax) {
+		atomic_subtract_int(&kq_ncallouts, 1);
+		return (ENOMEM);
+	}
 
 	if ((kn->kn_sfflags & NOTE_ABSTIME) == 0)
 		kn->kn_flags |= EV_CLEAR;	/* automatically set */


More information about the dev-commits-src-all mailing list