Sat Feb 12 15:51:29 2022 UTC ()
Add inline functions to manipulate the klists that link up knotes
via kn_selnext:

- klist_init()
- klist_fini()
- klist_insert()
- klist_remove()

These provide some API insulation from the implementation details of these
lists (but not completely; see vn_knote_attach() and vn_knote_detach()).
Currently just a wrapper around SLIST(9).

This will make it significantly easier to switch kn_selnext linkage
to a different kind of list.


(thorpej)
diff -r1.7 -r1.8 src/sys/arch/arm/xscale/pxa2x0_apm.c
diff -r1.13 -r1.14 src/sys/arch/mips/ralink/ralink_gpio.c
diff -r1.14 -r1.15 src/sys/dev/pci/xmm7360.c
diff -r1.1 -r1.2 src/sys/external/bsd/drm2/linux/linux_sync_file.c
diff -r1.139 -r1.140 src/sys/kern/kern_event.c
diff -r1.262 -r1.263 src/sys/kern/kern_proc.c
diff -r1.400 -r1.401 src/sys/kern/kern_sig.c
diff -r1.57 -r1.58 src/sys/kern/sys_select.c
diff -r1.53 -r1.54 src/sys/kern/vfs_init.c
diff -r1.554 -r1.555 src/sys/kern/vfs_syscalls.c
diff -r1.129 -r1.130 src/sys/kern/vfs_vnode.c
diff -r1.51 -r1.52 src/sys/sys/event.h
diff -r1.237 -r1.238 src/sys/sys/mount.h

cvs diff -r1.7 -r1.8 src/sys/arch/arm/xscale/pxa2x0_apm.c (expand / switch to context diff)
--- src/sys/arch/arm/xscale/pxa2x0_apm.c 2021/09/26 16:36:18 1.7
+++ src/sys/arch/arm/xscale/pxa2x0_apm.c 2022/02/12 15:51:28 1.8
@@ -1,4 +1,4 @@
-/*	$NetBSD: pxa2x0_apm.c,v 1.7 2021/09/26 16:36:18 thorpej Exp $	*/
+/*	$NetBSD: pxa2x0_apm.c,v 1.8 2022/02/12 15:51:28 thorpej Exp $	*/
 /*	$OpenBSD: pxa2x0_apm.c,v 1.28 2007/03/29 18:42:38 uwe Exp $	*/
 
 /*-
@@ -601,7 +601,7 @@
 	struct pxa2x0_apm_softc *sc =
 	    (struct pxa2x0_apm_softc *)kn->kn_hook;
 
-	SLIST_REMOVE(&sc->sc_note, kn, knote, kn_selnext);
+	klist_remove(&sc->sc_note, kn);
 }
 
 int
@@ -633,7 +633,7 @@
 	}
 
 	kn->kn_hook = (caddr_t)sc;
-	SLIST_INSERT_HEAD(&sc->sc_note, kn, kn_selnext);
+	klist_insert(&sc->sc_note, kn);
 
 	return (0);
 }
@@ -651,6 +651,7 @@
 	}
 
 	lockinit(&sc->sc_lock, PWAIT, "apmlk", 0, 0);
+	klist_init(&sc->sc_note);
 
 	kthread_create_deferred(apm_thread_create, sc);
 

cvs diff -r1.13 -r1.14 src/sys/arch/mips/ralink/ralink_gpio.c (expand / switch to context diff)
--- src/sys/arch/mips/ralink/ralink_gpio.c 2021/09/26 01:16:07 1.13
+++ src/sys/arch/mips/ralink/ralink_gpio.c 2022/02/12 15:51:29 1.14

cvs diff -r1.14 -r1.15 src/sys/dev/pci/xmm7360.c (expand / switch to context diff)
--- src/sys/dev/pci/xmm7360.c 2022/02/12 03:24:36 1.14
+++ src/sys/dev/pci/xmm7360.c 2022/02/12 15:51:29 1.15
@@ -1,4 +1,4 @@
-/*	$NetBSD: xmm7360.c,v 1.14 2022/02/12 03:24:36 riastradh Exp $	*/
+/*	$NetBSD: xmm7360.c,v 1.15 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*
  * Device driver for Intel XMM7360 LTE modems, eg. Fibocom L850-GL.
@@ -75,7 +75,7 @@
 #include "opt_gateway.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xmm7360.c,v 1.14 2022/02/12 03:24:36 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xmm7360.c,v 1.15 2022/02/12 15:51:29 thorpej Exp $");
 #endif
 
 #include <sys/param.h>
@@ -258,10 +258,6 @@
 #define if_hardmtu			if_mtu
 #define IF_OUTPUT_CONST			const
 #define si_note				sel_klist
-#define klist_insert(klist, kn)		\
-		SLIST_INSERT_HEAD(klist, kn, kn_selnext)
-#define klist_remove(klist, kn)		\
-		SLIST_REMOVE(klist, kn, knote, kn_selnext)
 #define XMM_KQ_ISFD_INITIALIZER		.f_flags = FILTEROP_ISFD
 #define tty_lock()			mutex_spin_enter(&tty_lock)
 #define tty_unlock()			mutex_spin_exit(&tty_lock)

cvs diff -r1.1 -r1.2 src/sys/external/bsd/drm2/linux/linux_sync_file.c (expand / switch to context diff)
--- src/sys/external/bsd/drm2/linux/linux_sync_file.c 2021/12/19 10:45:50 1.1
+++ src/sys/external/bsd/drm2/linux/linux_sync_file.c 2022/02/12 15:51:29 1.2
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_sync_file.c,v 1.1 2021/12/19 10:45:50 riastradh Exp $	*/
+/*	$NetBSD: linux_sync_file.c,v 1.2 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_sync_file.c,v 1.1 2021/12/19 10:45:50 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_sync_file.c,v 1.2 2022/02/12 15:51:29 thorpej Exp $");
 
 #include <sys/event.h>
 #include <sys/fcntl.h>
@@ -136,7 +136,7 @@
 		kn->kn_fop = &sync_file_filtops;
 		kn->kn_hook = sf;
 		mutex_enter(&sf->sf_lock);
-		SLIST_INSERT_HEAD(&sf->sf_selq.sel_klist, kn, kn_selnext);
+		klist_insert(&sf->sf_selq.sel_klist, kn);
 		mutex_exit(&sf->sf_lock);
 		return 0;
 	default:
@@ -150,7 +150,7 @@
 	struct sync_file *sf = kn->kn_hook;
 
 	mutex_enter(&sf->sf_lock);
-	SLIST_REMOVE(&sf->sf_selq.sel_klist, kn, knote, kn_selnext);
+	klist_remove(&sf->sf_selq.sel_klist, kn);
 	mutex_exit(&sf->sf_lock);
 }
 

cvs diff -r1.139 -r1.140 src/sys/kern/kern_event.c (expand / switch to context diff)
--- src/sys/kern/kern_event.c 2022/01/01 10:54:21 1.139
+++ src/sys/kern/kern_event.c 2022/02/12 15:51:29 1.140
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.139 2022/01/01 10:54:21 msaitoh Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.140 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2021 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 #endif /* _KERNEL_OPT */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.139 2022/01/01 10:54:21 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.140 2022/02/12 15:51:29 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -790,7 +790,7 @@
 	 */
 	kn->kn_sfflags &= ~NOTE_CHILD;
 
-	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
+	klist_insert(&p->p_klist, kn);
     	mutex_exit(p->p_lock);
 
 	return 0;
@@ -828,7 +828,7 @@
 			goto again;
 		}
 		kn->kn_status |= KN_DETACHED;
-		SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
+		klist_remove(&p->p_klist, kn);
 		mutex_exit(p->p_lock);
 	}
 	mutex_spin_exit(&kq->kq_lock);
@@ -1019,7 +1019,7 @@
 		error = EACCES;
 		goto out;
 	}
-	SLIST_INSERT_HEAD(&p2->p_klist, kntrack, kn_selnext);
+	klist_insert(&p2->p_klist, kntrack);
 	mutex_exit(p2->p_lock);
 
 	KASSERT(fdp->fd_knhashmask != 0);

cvs diff -r1.262 -r1.263 src/sys/kern/kern_proc.c (expand / switch to context diff)
--- src/sys/kern/kern_proc.c 2020/12/24 12:14:50 1.262
+++ src/sys/kern/kern_proc.c 2022/02/12 15:51:29 1.263
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_proc.c,v 1.262 2020/12/24 12:14:50 nia Exp $	*/
+/*	$NetBSD: kern_proc.c,v 1.263 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.262 2020/12/24 12:14:50 nia Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.263 2022/02/12 15:51:29 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kstack.h"
@@ -354,7 +354,17 @@
 static int
 proc_ctor(void *arg __unused, void *obj, int flags __unused)
 {
-	memset(obj, 0, sizeof(struct proc));
+	struct proc *p = obj;
+
+	memset(p, 0, sizeof(*p));
+	klist_init(&p->p_klist);
+
+	/*
+	 * There is no need for a proc_dtor() to do a klist_fini(),
+	 * since knote_proc_exit() ensures that p->p_klist is empty
+	 * when a process exits.
+	 */
+
 	return 0;
 }
 

cvs diff -r1.400 -r1.401 src/sys/kern/kern_sig.c (expand / switch to context diff)
--- src/sys/kern/kern_sig.c 2021/10/27 04:45:42 1.400
+++ src/sys/kern/kern_sig.c 2022/02/12 15:51:29 1.401
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.400 2021/10/27 04:45:42 thorpej Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.401 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.400 2021/10/27 04:45:42 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.401 2022/02/12 15:51:29 thorpej Exp $");
 
 #include "opt_execfmt.h"
 #include "opt_ptrace.h"
@@ -2660,7 +2660,7 @@
 	kn->kn_flags |= EV_CLEAR;	/* automatically set */
 
 	mutex_enter(p->p_lock);
-	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
+	klist_insert(&p->p_klist, kn);
 	mutex_exit(p->p_lock);
 
 	return 0;
@@ -2672,7 +2672,7 @@
 	struct proc *p = kn->kn_obj;
 
 	mutex_enter(p->p_lock);
-	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
+	klist_remove(&p->p_klist, kn);
 	mutex_exit(p->p_lock);
 }
 

cvs diff -r1.57 -r1.58 src/sys/kern/sys_select.c (expand / switch to context diff)
--- src/sys/kern/sys_select.c 2021/12/10 20:36:04 1.57
+++ src/sys/kern/sys_select.c 2022/02/12 15:51:29 1.58
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_select.c,v 1.57 2021/12/10 20:36:04 andvar Exp $	*/
+/*	$NetBSD: sys_select.c,v 1.58 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2008, 2009, 2010, 2019, 2020 The NetBSD Foundation, Inc.
@@ -84,7 +84,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.57 2021/12/10 20:36:04 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.58 2022/02/12 15:51:29 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -675,7 +675,7 @@
 void
 selrecord_knote(struct selinfo *sip, struct knote *kn)
 {
-	SLIST_INSERT_HEAD(&sip->sel_klist, kn, kn_selnext);
+	klist_insert(&sip->sel_klist, kn);
 }
 
 /*
@@ -689,8 +689,7 @@
 bool
 selremove_knote(struct selinfo *sip, struct knote *kn)
 {
-	SLIST_REMOVE(&sip->sel_klist, kn, knote, kn_selnext);
-	return SLIST_EMPTY(&sip->sel_klist);
+	return klist_remove(&sip->sel_klist, kn);
 }
 
 /*
@@ -914,6 +913,7 @@
 {
 
 	memset(sip, 0, sizeof(*sip));
+	klist_init(&sip->sel_klist);
 }
 
 /*
@@ -932,6 +932,8 @@
 	selcluster_t *sc;
 	kmutex_t *lock;
 	lwp_t *l;
+
+	klist_fini(&sip->sel_klist);
 
 	if (sip->sel_lwp == NULL)
 		return;

cvs diff -r1.53 -r1.54 src/sys/kern/vfs_init.c (expand / switch to context diff)
--- src/sys/kern/vfs_init.c 2021/09/26 21:29:38 1.53
+++ src/sys/kern/vfs_init.c 2022/02/12 15:51:29 1.54
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_init.c,v 1.53 2021/09/26 21:29:38 thorpej Exp $	*/
+/*	$NetBSD: vfs_init.c,v 1.54 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_init.c,v 1.53 2021/09/26 21:29:38 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_init.c,v 1.54 2022/02/12 15:51:29 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/mount.h>
@@ -456,8 +456,10 @@
 	 */
 	module_init_class(MODULE_CLASS_VFS);
 
-	extern kmutex_t fs_klist_lock;
-	mutex_init(&fs_klist_lock, MUTEX_DEFAULT, IPL_NONE);
+	/*
+	 * Initialize EVFILT_FS for kqueue.
+	 */
+	vfs_evfilt_fs_init();
 }
 
 /*

cvs diff -r1.554 -r1.555 src/sys/kern/vfs_syscalls.c (expand / switch to context diff)
--- src/sys/kern/vfs_syscalls.c 2021/11/07 13:47:50 1.554
+++ src/sys/kern/vfs_syscalls.c 2022/02/12 15:51:29 1.555
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_syscalls.c,v 1.554 2021/11/07 13:47:50 christos Exp $	*/
+/*	$NetBSD: vfs_syscalls.c,v 1.555 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2019, 2020 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.554 2021/11/07 13:47:50 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.555 2022/02/12 15:51:29 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_fileassoc.h"
@@ -168,18 +168,25 @@
 /*
  * Filter event method for EVFILT_FS.
  */
-static struct klist fs_klist = SLIST_HEAD_INITIALIZER(&fs_klist);
-kmutex_t fs_klist_lock;
+static struct klist fs_klist;
+static kmutex_t fs_klist_lock;
 
 CTASSERT((NOTE_SUBMIT & VQ_MOUNT) == 0);
 CTASSERT((NOTE_SUBMIT & VQ_UNMOUNT) == 0);
 
+void
+vfs_evfilt_fs_init(void)
+{
+	klist_init(&fs_klist);
+	mutex_init(&fs_klist_lock, MUTEX_DEFAULT, IPL_NONE);
+}
+
 static int
 filt_fsattach(struct knote *kn)
 {
 	mutex_enter(&fs_klist_lock);
 	kn->kn_flags |= EV_CLEAR;
-	SLIST_INSERT_HEAD(&fs_klist, kn, kn_selnext);
+	klist_insert(&fs_klist, kn);
 	mutex_exit(&fs_klist_lock);
 
 	return 0;
@@ -189,7 +196,7 @@
 filt_fsdetach(struct knote *kn)
 {
 	mutex_enter(&fs_klist_lock);
-	SLIST_REMOVE(&fs_klist, kn, knote, kn_selnext);
+	klist_remove(&fs_klist, kn);
 	mutex_exit(&fs_klist_lock);
 }
 

cvs diff -r1.129 -r1.130 src/sys/kern/vfs_vnode.c (expand / switch to context diff)
--- src/sys/kern/vfs_vnode.c 2022/02/08 08:57:11 1.129
+++ src/sys/kern/vfs_vnode.c 2022/02/12 15:51:29 1.130
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_vnode.c,v 1.129 2022/02/08 08:57:11 hannken Exp $	*/
+/*	$NetBSD: vfs_vnode.c,v 1.130 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 1997-2011, 2019, 2020 The NetBSD Foundation, Inc.
@@ -148,7 +148,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.129 2022/02/08 08:57:11 hannken Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.130 2022/02/12 15:51:29 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pax.h"
@@ -444,6 +444,7 @@
 	vp->v_mount = mp;
 	vp->v_type = VBAD;
 	vp->v_interlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
+	klist_init(&vp->v_klist);
 	vip->vi_state = VS_MARKER;
 
 	return vp;
@@ -461,6 +462,7 @@
 	KASSERT(vip->vi_state == VS_MARKER);
 	mutex_obj_free(vp->v_interlock);
 	uvm_obj_destroy(&vp->v_uobj, true);
+	klist_fini(&vp->v_klist);
 	pool_cache_put(vcache_pool, vip);
 }
 
@@ -1307,6 +1309,7 @@
 	vp->v_interlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
 
 	uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 1);
+	klist_init(&vp->v_klist);
 	cv_init(&vp->v_cv, "vnode");
 	cache_vnode_init(vp);
 
@@ -1368,6 +1371,7 @@
 	mutex_obj_free(vp->v_interlock);
 	rw_destroy(&vip->vi_lock);
 	uvm_obj_destroy(&vp->v_uobj, true);
+	klist_fini(&vp->v_klist);
 	cv_destroy(&vp->v_cv);
 	cache_vnode_fini(vp);
 	pool_cache_put(vcache_pool, vip);

cvs diff -r1.51 -r1.52 src/sys/sys/event.h (expand / switch to context diff)
--- src/sys/sys/event.h 2021/10/23 01:28:33 1.51
+++ src/sys/sys/event.h 2022/02/12 15:51:29 1.52
@@ -1,4 +1,4 @@
-/*	$NetBSD: event.h,v 1.51 2021/10/23 01:28:33 thorpej Exp $	*/
+/*	$NetBSD: event.h,v 1.52 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
@@ -333,6 +333,31 @@
 
 int	filt_seltrue(struct knote *, long);
 extern const struct filterops seltrue_filtops;
+
+static inline void
+klist_init(struct klist *list)
+{
+	SLIST_INIT(list);
+}
+
+static inline void
+klist_fini(struct klist *list)
+{
+	/* Nothing, for now. */
+}
+
+static inline void
+klist_insert(struct klist *list, struct knote *kn)
+{
+	SLIST_INSERT_HEAD(list, kn, kn_selnext);
+}
+
+static inline bool
+klist_remove(struct klist *list, struct knote *kn)
+{
+	SLIST_REMOVE(list, kn, knote, kn_selnext);
+	return SLIST_EMPTY(list);
+}
 
 #else 	/* !_KERNEL */
 

cvs diff -r1.237 -r1.238 src/sys/sys/mount.h (expand / switch to context diff)
--- src/sys/sys/mount.h 2021/02/04 21:07:06 1.237
+++ src/sys/sys/mount.h 2022/02/12 15:51:29 1.238
@@ -1,4 +1,4 @@
-/*	$NetBSD: mount.h,v 1.237 2021/02/04 21:07:06 jdolecek Exp $	*/
+/*	$NetBSD: mount.h,v 1.238 2022/02/12 15:51:29 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1989, 1991, 1993
@@ -450,6 +450,7 @@
 int	do_sys_mount(struct lwp *, const char *, enum uio_seg, const char *,
 	    int, void *, enum uio_seg, size_t, register_t *);
 void	vfsinit(void);
+void	vfs_evfilt_fs_init(void);
 void	vfs_opv_init(const struct vnodeopv_desc * const *);
 void	vfs_opv_free(const struct vnodeopv_desc * const *);
 #ifdef DEBUG