Index | Thread | Search

From:
David Gwynne <david@gwynne.id.au>
Subject:
futex(2): make it easier to follow futexes around slpques
To:
tech@openbsd.org
Cc:
mpi@openbsd.org
Date:
Thu, 3 Jul 2025 13:30:09 +1000

Download raw body.

Thread
  • David Gwynne:

    futex(2): make it easier to follow futexes around slpques

have futexes store which slpqueue they're on so it's easier to check if
we have the right one in futex_unwait if futex_requeue moved it.

this is just an optimisation.

Index: sys_futex.c
===================================================================
RCS file: /cvs/src/sys/kern/sys_futex.c,v
diff -u -p -r1.25 sys_futex.c
--- sys_futex.c	5 Jun 2025 08:44:00 -0000	1.25
+++ sys_futex.c	3 Jul 2025 03:13:06 -0000
@@ -65,7 +65,11 @@
  * to the futexes on the list until it clears ft_proc.
  */
 
+struct futex_slpque;
+
 struct futex {
+	struct futex_slpque * volatile
+				 ft_fsq;	/* [f] current futex_slpque */
 	TAILQ_ENTRY(futex)	 ft_entry;	/* [f] entry on futex_slpque */
 
 	struct process		*ft_ps;		/* [I] for private futexes */
@@ -223,7 +227,7 @@ futex_unwait(struct futex_slpque *ofsq, 
 
 	for (;;) {
 		rw_enter_write(&ofsq->fsq_lock);
-		fsq = futex_get_slpque(f);
+		fsq = f->ft_fsq;
 		if (ofsq == fsq)
 			break;
 
@@ -275,6 +279,7 @@ futex_wait(struct proc *p, uint32_t *uad
 	fsq = futex_get_slpque(&f);
 
 	/* Mark futex as waiting. */
+	f.ft_fsq = fsq;
 	f.ft_proc = p;
 	rw_enter_write(&fsq->fsq_lock);
 	/* Make the waiting futex visible to wake/requeue */
@@ -415,6 +420,7 @@ futex_requeue(struct proc *p, uint32_t *
 				continue;
 
 			TAILQ_REMOVE(&ofsq->fsq_list, f, ft_entry);
+			f->ft_fsq = nfsq;
 			f->ft_ps = nkey.ft_ps;
 			f->ft_obj = nkey.ft_obj;
 			f->ft_amap = nkey.ft_amap;