The following diff:

* renames existing functions klist_insert() and klist_remove() to
  klist_insert_locked() and klist_remove_locked(), respectively.
  These functions expect that the caller has acquired the klist lock.

* adds new functions klist_insert() and klist_remove() that acquire the
  klist lock internally.

When the diff is applied, no calls of the new klist_insert() and
klist_remove() should appear. Nothing related to locking is changed yet.
Note that the kernel lock serves as the klist lock by default.

The functions with internal locking will allow some code simplification
when the caller does not need the klist lock for anything else.

In many cases, the f_detach callback only does klist removal. This could
be utilized to compact the code even further.

OK?

Index: arch/arm64/dev/apm.c
===================================================================
RCS file: src/sys/arch/arm64/dev/apm.c,v
retrieving revision 1.5
diff -u -p -r1.5 apm.c
--- arch/arm64/dev/apm.c        29 May 2020 04:42:23 -0000      1.5
+++ arch/arm64/dev/apm.c        23 Dec 2020 16:21:29 -0000
@@ -270,7 +270,7 @@ filt_apmrdetach(struct knote *kn)
 {
        struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
 
-       klist_remove(&sc->sc_note, kn);
+       klist_remove_locked(&sc->sc_note, kn);
 }
 
 int
@@ -302,7 +302,7 @@ apmkqfilter(dev_t dev, struct knote *kn)
        }
 
        kn->kn_hook = (caddr_t)sc;
-       klist_insert(&sc->sc_note, kn);
+       klist_insert_locked(&sc->sc_note, kn);
 
        return (0);
 }
Index: arch/i386/i386/apm.c
===================================================================
RCS file: src/sys/arch/i386/i386/apm.c,v
retrieving revision 1.125
diff -u -p -r1.125 apm.c
--- arch/i386/i386/apm.c        24 Jun 2020 22:03:40 -0000      1.125
+++ arch/i386/i386/apm.c        23 Dec 2020 16:21:29 -0000
@@ -1117,7 +1117,7 @@ filt_apmrdetach(struct knote *kn)
        struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
 
        rw_enter_write(&sc->sc_lock);
-       klist_remove(&sc->sc_note, kn);
+       klist_remove_locked(&sc->sc_note, kn);
        rw_exit_write(&sc->sc_lock);
 }
 
@@ -1151,7 +1151,7 @@ apmkqfilter(dev_t dev, struct knote *kn)
        kn->kn_hook = (caddr_t)sc;
 
        rw_enter_write(&sc->sc_lock);
-       klist_insert(&sc->sc_note, kn);
+       klist_insert_locked(&sc->sc_note, kn);
        rw_exit_write(&sc->sc_lock);
        return (0);
 }
Index: arch/loongson/dev/apm.c
===================================================================
RCS file: src/sys/arch/loongson/dev/apm.c,v
retrieving revision 1.38
diff -u -p -r1.38 apm.c
--- arch/loongson/dev/apm.c     24 Jun 2020 22:03:40 -0000      1.38
+++ arch/loongson/dev/apm.c     23 Dec 2020 16:21:29 -0000
@@ -291,7 +291,7 @@ filt_apmrdetach(struct knote *kn)
 {
        struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
 
-       klist_remove(&sc->sc_note, kn);
+       klist_remove_locked(&sc->sc_note, kn);
 }
 
 int
@@ -323,7 +323,7 @@ apmkqfilter(dev_t dev, struct knote *kn)
        }
 
        kn->kn_hook = (caddr_t)sc;
-       klist_insert(&sc->sc_note, kn);
+       klist_insert_locked(&sc->sc_note, kn);
 
        return (0);
 }
Index: arch/macppc/dev/apm.c
===================================================================
RCS file: src/sys/arch/macppc/dev/apm.c,v
retrieving revision 1.22
diff -u -p -r1.22 apm.c
--- arch/macppc/dev/apm.c       7 Apr 2020 13:27:50 -0000       1.22
+++ arch/macppc/dev/apm.c       23 Dec 2020 16:21:29 -0000
@@ -305,7 +305,7 @@ filt_apmrdetach(struct knote *kn)
 {
        struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
 
-       klist_remove(&sc->sc_note, kn);
+       klist_remove_locked(&sc->sc_note, kn);
 }
 
 int
@@ -337,7 +337,7 @@ apmkqfilter(dev_t dev, struct knote *kn)
        }
 
        kn->kn_hook = (caddr_t)sc;
-       klist_insert(&sc->sc_note, kn);
+       klist_insert_locked(&sc->sc_note, kn);
 
        return (0);
 }
Index: arch/sparc64/dev/vldcp.c
===================================================================
RCS file: src/sys/arch/sparc64/dev/vldcp.c,v
retrieving revision 1.20
diff -u -p -r1.20 vldcp.c
--- arch/sparc64/dev/vldcp.c    23 May 2020 11:29:37 -0000      1.20
+++ arch/sparc64/dev/vldcp.c    23 Dec 2020 16:21:29 -0000
@@ -628,7 +628,7 @@ filt_vldcprdetach(struct knote *kn)
        int s;
 
        s = spltty();
-       klist_remove(&sc->sc_rsel.si_note, kn);
+       klist_remove_locked(&sc->sc_rsel.si_note, kn);
        splx(s);
 }
 
@@ -639,7 +639,7 @@ filt_vldcpwdetach(struct knote *kn)
        int s;
 
        s = spltty();
-       klist_remove(&sc->sc_wsel.si_note, kn);
+       klist_remove_locked(&sc->sc_wsel.si_note, kn);
        splx(s);
 }
 
@@ -733,7 +733,7 @@ vldcpkqfilter(dev_t dev, struct knote *k
        kn->kn_hook = sc;
 
        s = spltty();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
 
        return (0);
Index: dev/audio.c
===================================================================
RCS file: src/sys/dev/audio.c,v
retrieving revision 1.191
diff -u -p -r1.191 audio.c
--- dev/audio.c 19 May 2020 06:32:24 -0000      1.191
+++ dev/audio.c 23 Dec 2020 16:21:29 -0000
@@ -2294,7 +2294,7 @@ audiokqfilter(dev_t dev, struct knote *k
        kn->kn_hook = sc;
 
        mtx_enter(&audio_lock);
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        mtx_leave(&audio_lock);
 done:
        device_unref(&sc->dev);
@@ -2307,7 +2307,7 @@ filt_audiordetach(struct knote *kn)
        struct audio_softc *sc = kn->kn_hook;
 
        mtx_enter(&audio_lock);
-       klist_remove(&sc->rec.sel.si_note, kn);
+       klist_remove_locked(&sc->rec.sel.si_note, kn);
        mtx_leave(&audio_lock);
 }
 
@@ -2332,7 +2332,7 @@ filt_audiowdetach(struct knote *kn)
        struct audio_softc *sc = kn->kn_hook;
 
        mtx_enter(&audio_lock);
-       klist_remove(&sc->play.sel.si_note, kn);
+       klist_remove_locked(&sc->play.sel.si_note, kn);
        mtx_leave(&audio_lock);
 }
 
@@ -2357,7 +2357,7 @@ filt_audioctlrdetach(struct knote *kn)
        struct audio_softc *sc = kn->kn_hook;
 
        mtx_enter(&audio_lock);
-       klist_remove(&sc->mix_sel.si_note, kn);
+       klist_remove_locked(&sc->mix_sel.si_note, kn);
        mtx_leave(&audio_lock);
 }
 
Index: dev/hotplug.c
===================================================================
RCS file: src/sys/dev/hotplug.c,v
retrieving revision 1.20
diff -u -p -r1.20 hotplug.c
--- dev/hotplug.c       7 Apr 2020 13:27:51 -0000       1.20
+++ dev/hotplug.c       23 Dec 2020 16:21:29 -0000
@@ -211,7 +211,7 @@ hotplugkqfilter(dev_t dev, struct knote 
        }
 
        s = splbio();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
        return (0);
 }
@@ -222,7 +222,7 @@ filt_hotplugrdetach(struct knote *kn)
        int s;
 
        s = splbio();
-       klist_remove(&hotplug_sel.si_note, kn);
+       klist_remove_locked(&hotplug_sel.si_note, kn);
        splx(s);
 }
 
Index: dev/midi.c
===================================================================
RCS file: src/sys/dev/midi.c,v
retrieving revision 1.47
diff -u -p -r1.47 midi.c
--- dev/midi.c  7 Apr 2020 13:27:51 -0000       1.47
+++ dev/midi.c  23 Dec 2020 16:21:29 -0000
@@ -363,7 +363,7 @@ midikqfilter(dev_t dev, struct knote *kn
        kn->kn_hook = (void *)sc;
 
        mtx_enter(&audio_lock);
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        mtx_leave(&audio_lock);
 done:
        device_unref(&sc->dev);
@@ -376,7 +376,7 @@ filt_midirdetach(struct knote *kn)
        struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
 
        mtx_enter(&audio_lock);
-       klist_remove(&sc->rsel.si_note, kn);
+       klist_remove_locked(&sc->rsel.si_note, kn);
        mtx_leave(&audio_lock);
 }
 
@@ -399,7 +399,7 @@ filt_midiwdetach(struct knote *kn)
        struct midi_softc *sc = (struct midi_softc *)kn->kn_hook;
 
        mtx_enter(&audio_lock);
-       klist_remove(&sc->wsel.si_note, kn);
+       klist_remove_locked(&sc->wsel.si_note, kn);
        mtx_leave(&audio_lock);
 }
 
Index: dev/video.c
===================================================================
RCS file: src/sys/dev/video.c,v
retrieving revision 1.44
diff -u -p -r1.44 video.c
--- dev/video.c 16 May 2020 10:47:22 -0000      1.44
+++ dev/video.c 23 Dec 2020 16:21:29 -0000
@@ -419,7 +419,7 @@ filt_videodetach(struct knote *kn)
        int s;
 
        s = splhigh();
-       klist_remove(&sc->sc_rsel.si_note, kn);
+       klist_remove_locked(&sc->sc_rsel.si_note, kn);
        splx(s);
 }
 
@@ -476,7 +476,7 @@ videokqfilter(dev_t dev, struct knote *k
        }
 
        s = splhigh();
-       klist_insert(&sc->sc_rsel.si_note, kn);
+       klist_insert_locked(&sc->sc_rsel.si_note, kn);
        splx(s);
 
        return (0);
Index: dev/vscsi.c
===================================================================
RCS file: src/sys/dev/vscsi.c,v
retrieving revision 1.57
diff -u -p -r1.57 vscsi.c
--- dev/vscsi.c 22 Sep 2020 19:32:52 -0000      1.57
+++ dev/vscsi.c 23 Dec 2020 16:21:29 -0000
@@ -575,7 +575,7 @@ vscsikqfilter(dev_t dev, struct knote *k
        kn->kn_hook = sc;
 
        mtx_enter(&sc->sc_sel_mtx);
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        mtx_leave(&sc->sc_sel_mtx);
 
        /* device ref is given to the knote in the klist */
@@ -590,7 +590,7 @@ filt_vscsidetach(struct knote *kn)
        struct klist *klist = &sc->sc_sel.si_note;
 
        mtx_enter(&sc->sc_sel_mtx);
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
        mtx_leave(&sc->sc_sel_mtx);
 
        device_unref(&sc->sc_dev);
Index: dev/acpi/acpi.c
===================================================================
RCS file: src/sys/dev/acpi/acpi.c,v
retrieving revision 1.393
diff -u -p -r1.393 acpi.c
--- dev/acpi/acpi.c     17 Dec 2020 17:57:19 -0000      1.393
+++ dev/acpi/acpi.c     23 Dec 2020 16:21:29 -0000
@@ -3672,7 +3672,7 @@ acpi_filtdetach(struct knote *kn)
        int s;
 
        s = splbio();
-       klist_remove(sc->sc_note, kn);
+       klist_remove_locked(sc->sc_note, kn);
        splx(s);
 }
 
@@ -3706,7 +3706,7 @@ acpikqfilter(dev_t dev, struct knote *kn
        kn->kn_hook = sc;
 
        s = splbio();
-       klist_insert(sc->sc_note, kn);
+       klist_insert_locked(sc->sc_note, kn);
        splx(s);
 
        return (0);
Index: dev/pci/drm/drm_drv.c
===================================================================
RCS file: src/sys/dev/pci/drm/drm_drv.c,v
retrieving revision 1.178
diff -u -p -r1.178 drm_drv.c
--- dev/pci/drm/drm_drv.c       14 Nov 2020 23:08:47 -0000      1.178
+++ dev/pci/drm/drm_drv.c       23 Dec 2020 16:21:29 -0000
@@ -1657,7 +1657,7 @@ filt_drmdetach(struct knote *kn)
        int s;
 
        s = spltty();
-       klist_remove(&dev->note, kn);
+       klist_remove_locked(&dev->note, kn);
        splx(s);
 }
 
@@ -1676,7 +1676,7 @@ filt_drmreaddetach(struct knote *kn)
        int s;
 
        s = spltty();
-       klist_remove(&file_priv->rsel.si_note, kn);
+       klist_remove_locked(&file_priv->rsel.si_note, kn);
        splx(s);
 }
 
@@ -1731,7 +1731,7 @@ drmkqfilter(dev_t kdev, struct knote *kn
                kn->kn_hook = file_priv;
 
                s = spltty();
-               klist_insert(&file_priv->rsel.si_note, kn);
+               klist_insert_locked(&file_priv->rsel.si_note, kn);
                splx(s);
                break;
        case EVFILT_DEVICE:
@@ -1739,7 +1739,7 @@ drmkqfilter(dev_t kdev, struct knote *kn
                kn->kn_hook = dev;
 
                s = spltty();
-               klist_insert(&dev->note, kn);
+               klist_insert_locked(&dev->note, kn);
                splx(s);
                break;
        default:
Index: dev/usb/ugen.c
===================================================================
RCS file: src/sys/dev/usb/ugen.c,v
retrieving revision 1.108
diff -u -p -r1.108 ugen.c
--- dev/usb/ugen.c      29 Sep 2020 09:11:44 -0000      1.108
+++ dev/usb/ugen.c      23 Dec 2020 16:21:30 -0000
@@ -1304,7 +1304,7 @@ filt_ugenrdetach(struct knote *kn)
        int s;
 
        s = splusb();
-       klist_remove(&sce->rsel.si_note, kn);
+       klist_remove_locked(&sce->rsel.si_note, kn);
        splx(s);
 }
 
@@ -1413,7 +1413,7 @@ ugenkqfilter(dev_t dev, struct knote *kn
        kn->kn_hook = (void *)sce;
 
        s = splusb();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
 
        return (0);
Index: dev/usb/uhid.c
===================================================================
RCS file: src/sys/dev/usb/uhid.c,v
retrieving revision 1.80
diff -u -p -r1.80 uhid.c
--- dev/usb/uhid.c      13 May 2020 08:13:42 -0000      1.80
+++ dev/usb/uhid.c      23 Dec 2020 16:21:30 -0000
@@ -447,7 +447,7 @@ filt_uhidrdetach(struct knote *kn)
        int s;
 
        s = splusb();
-       klist_remove(&sc->sc_rsel.si_note, kn);
+       klist_remove_locked(&sc->sc_rsel.si_note, kn);
        splx(s);
 }
 
@@ -496,7 +496,7 @@ uhidkqfilter(dev_t dev, struct knote *kn
        kn->kn_hook = (void *)sc;
 
        s = splusb();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
 
        return (0);
Index: dev/wscons/wsevent.c
===================================================================
RCS file: src/sys/dev/wscons/wsevent.c,v
retrieving revision 1.24
diff -u -p -r1.24 wsevent.c
--- dev/wscons/wsevent.c        7 Apr 2020 13:27:51 -0000       1.24
+++ dev/wscons/wsevent.c        23 Dec 2020 16:21:30 -0000
@@ -236,7 +236,7 @@ wsevent_kqfilter(struct wseventvar *ev, 
        kn->kn_hook = ev;
 
        s = splwsevent();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
 
        return (0);
@@ -250,7 +250,7 @@ filt_wseventdetach(struct knote *kn)
        int s;
 
        s = splwsevent();
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
        splx(s);
 }
 
Index: isofs/cd9660/cd9660_vnops.c
===================================================================
RCS file: src/sys/isofs/cd9660/cd9660_vnops.c,v
retrieving revision 1.85
diff -u -p -r1.85 cd9660_vnops.c
--- isofs/cd9660/cd9660_vnops.c 11 Jun 2020 09:18:43 -0000      1.85
+++ isofs/cd9660/cd9660_vnops.c 23 Dec 2020 16:21:30 -0000
@@ -1002,7 +1002,7 @@ cd9660_kqfilter(void *v)
 
        kn->kn_hook = (caddr_t)vp;
 
-       klist_insert(&vp->v_selectinfo.si_note, kn);
+       klist_insert_locked(&vp->v_selectinfo.si_note, kn);
 
        return (0);
 }
@@ -1012,7 +1012,7 @@ filt_cd9660detach(struct knote *kn)
 {
        struct vnode *vp = (struct vnode *)kn->kn_hook;
 
-       klist_remove(&vp->v_selectinfo.si_note, kn);
+       klist_remove_locked(&vp->v_selectinfo.si_note, kn);
 }
 
 int
Index: kern/kern_event.c
===================================================================
RCS file: src/sys/kern/kern_event.c,v
retrieving revision 1.155
diff -u -p -r1.155 kern_event.c
--- kern/kern_event.c   23 Dec 2020 13:59:09 -0000      1.155
+++ kern/kern_event.c   23 Dec 2020 16:21:30 -0000
@@ -228,7 +228,7 @@ kqueue_kqfilter(struct file *fp, struct 
                return (EINVAL);
 
        kn->kn_fop = &kqread_filtops;
-       klist_insert(&kq->kq_sel.si_note, kn);
+       klist_insert_locked(&kq->kq_sel.si_note, kn);
        return (0);
 }
 
@@ -237,7 +237,7 @@ filt_kqdetach(struct knote *kn)
 {
        struct kqueue *kq = kn->kn_fp->f_data;
 
-       klist_remove(&kq->kq_sel.si_note, kn);
+       klist_remove_locked(&kq->kq_sel.si_note, kn);
 }
 
 int
@@ -283,7 +283,7 @@ filt_procattach(struct knote *kn)
        }
 
        s = splhigh();
-       klist_insert(&pr->ps_klist, kn);
+       klist_insert_locked(&pr->ps_klist, kn);
        splx(s);
 
        return (0);
@@ -307,7 +307,7 @@ filt_procdetach(struct knote *kn)
                return;
 
        s = splhigh();
-       klist_remove(&pr->ps_klist, kn);
+       klist_remove_locked(&pr->ps_klist, kn);
        splx(s);
 }
 
@@ -339,7 +339,7 @@ filt_proc(struct knote *kn, long hint)
                kn->kn_status |= KN_DETACHED;
                kn->kn_flags |= (EV_EOF | EV_ONESHOT);
                kn->kn_data = W_EXITCODE(pr->ps_xexit, pr->ps_xsig);
-               klist_remove(&pr->ps_klist, kn);
+               klist_remove_locked(&pr->ps_klist, kn);
                splx(s);
                return (1);
        }
@@ -1575,6 +1575,16 @@ klist_free(struct klist *klist)
 void
 klist_insert(struct klist *klist, struct knote *kn)
 {
+       int ls;
+
+       ls = klist_lock(klist);
+       SLIST_INSERT_HEAD(&klist->kl_list, kn, kn_selnext);
+       klist_unlock(klist, ls);
+}
+
+void
+klist_insert_locked(struct klist *klist, struct knote *kn)
+{
        KLIST_ASSERT_LOCKED(klist);
 
        SLIST_INSERT_HEAD(&klist->kl_list, kn, kn_selnext);
@@ -1583,6 +1593,16 @@ klist_insert(struct klist *klist, struct
 void
 klist_remove(struct klist *klist, struct knote *kn)
 {
+       int ls;
+
+       ls = klist_lock(klist);
+       SLIST_REMOVE(&klist->kl_list, kn, knote, kn_selnext);
+       klist_unlock(klist, ls);
+}
+
+void
+klist_remove_locked(struct klist *klist, struct knote *kn)
+{
        KLIST_ASSERT_LOCKED(klist);
 
        SLIST_REMOVE(&klist->kl_list, kn, knote, kn_selnext);
Index: kern/kern_sig.c
===================================================================
RCS file: src/sys/kern/kern_sig.c,v
retrieving revision 1.268
diff -u -p -r1.268 kern_sig.c
--- kern/kern_sig.c     7 Dec 2020 16:55:28 -0000       1.268
+++ kern/kern_sig.c     23 Dec 2020 16:21:30 -0000
@@ -1854,7 +1854,7 @@ filt_sigattach(struct knote *kn)
        kn->kn_flags |= EV_CLEAR;               /* automatically set */
 
        s = splhigh();
-       klist_insert(&pr->ps_klist, kn);
+       klist_insert_locked(&pr->ps_klist, kn);
        splx(s);
 
        return (0);
@@ -1867,7 +1867,7 @@ filt_sigdetach(struct knote *kn)
        int s;
 
        s = splhigh();
-       klist_remove(&pr->ps_klist, kn);
+       klist_remove_locked(&pr->ps_klist, kn);
        splx(s);
 }
 
Index: kern/subr_log.c
===================================================================
RCS file: src/sys/kern/subr_log.c,v
retrieving revision 1.69
diff -u -p -r1.69 subr_log.c
--- kern/subr_log.c     25 Oct 2020 10:55:42 -0000      1.69
+++ kern/subr_log.c     23 Dec 2020 16:21:30 -0000
@@ -319,7 +319,7 @@ logkqfilter(dev_t dev, struct knote *kn)
        kn->kn_hook = (void *)msgbufp;
 
        s = splhigh();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
 
        return (0);
@@ -331,7 +331,7 @@ filt_logrdetach(struct knote *kn)
        int s;
 
        s = splhigh();
-       klist_remove(&logsoftc.sc_selp.si_note, kn);
+       klist_remove_locked(&logsoftc.sc_selp.si_note, kn);
        splx(s);
 }
 
Index: kern/sys_pipe.c
===================================================================
RCS file: src/sys/kern/sys_pipe.c,v
retrieving revision 1.124
diff -u -p -r1.124 sys_pipe.c
--- kern/sys_pipe.c     11 Dec 2020 14:17:35 -0000      1.124
+++ kern/sys_pipe.c     23 Dec 2020 16:21:30 -0000
@@ -887,7 +887,7 @@ pipe_kqfilter(struct file *fp, struct kn
        case EVFILT_READ:
                kn->kn_fop = &pipe_rfiltops;
                kn->kn_hook = rpipe;
-               klist_insert(&rpipe->pipe_sel.si_note, kn);
+               klist_insert_locked(&rpipe->pipe_sel.si_note, kn);
                break;
        case EVFILT_WRITE:
                if (wpipe == NULL) {
@@ -897,7 +897,7 @@ pipe_kqfilter(struct file *fp, struct kn
                }
                kn->kn_fop = &pipe_wfiltops;
                kn->kn_hook = wpipe;
-               klist_insert(&wpipe->pipe_sel.si_note, kn);
+               klist_insert_locked(&wpipe->pipe_sel.si_note, kn);
                break;
        default:
                error = EINVAL;
@@ -914,7 +914,7 @@ filt_pipedetach(struct knote *kn)
        struct pipe *cpipe = kn->kn_hook;
 
        rw_enter_write(cpipe->pipe_lock);
-       klist_remove(&cpipe->pipe_sel.si_note, kn);
+       klist_remove_locked(&cpipe->pipe_sel.si_note, kn);
        rw_exit_write(cpipe->pipe_lock);
 }
 
Index: kern/tty.c
===================================================================
RCS file: src/sys/kern/tty.c,v
retrieving revision 1.165
diff -u -p -r1.165 tty.c
--- kern/tty.c  7 Dec 2020 16:55:29 -0000       1.165
+++ kern/tty.c  23 Dec 2020 16:21:30 -0000
@@ -1133,7 +1133,7 @@ ttkqfilter(dev_t dev, struct knote *kn)
        kn->kn_hook = tp;
 
        s = spltty();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
 
        return (0);
@@ -1146,7 +1146,7 @@ filt_ttyrdetach(struct knote *kn)
        int s;
 
        s = spltty();
-       klist_remove(&tp->t_rsel.si_note, kn);
+       klist_remove_locked(&tp->t_rsel.si_note, kn);
        splx(s);
 }
 
@@ -1175,7 +1175,7 @@ filt_ttywdetach(struct knote *kn)
        int s;
 
        s = spltty();
-       klist_remove(&tp->t_wsel.si_note, kn);
+       klist_remove_locked(&tp->t_wsel.si_note, kn);
        splx(s);
 }
 
Index: kern/tty_pty.c
===================================================================
RCS file: src/sys/kern/tty_pty.c,v
retrieving revision 1.104
diff -u -p -r1.104 tty_pty.c
--- kern/tty_pty.c      9 Sep 2020 16:29:14 -0000       1.104
+++ kern/tty_pty.c      23 Dec 2020 16:21:30 -0000
@@ -657,7 +657,7 @@ filt_ptcrdetach(struct knote *kn)
        int s;
 
        s = spltty();
-       klist_remove(&pti->pt_selr.si_note, kn);
+       klist_remove_locked(&pti->pt_selr.si_note, kn);
        splx(s);
 }
 
@@ -705,7 +705,7 @@ filt_ptcwdetach(struct knote *kn)
        int s;
 
        s = spltty();
-       klist_remove(&pti->pt_selw.si_note, kn);
+       klist_remove_locked(&pti->pt_selw.si_note, kn);
        splx(s);
 }
 
@@ -779,7 +779,7 @@ ptckqfilter(dev_t dev, struct knote *kn)
        kn->kn_hook = (caddr_t)pti;
 
        s = spltty();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
 
        return (0);
Index: kern/uipc_socket.c
===================================================================
RCS file: src/sys/kern/uipc_socket.c,v
retrieving revision 1.251
diff -u -p -r1.251 uipc_socket.c
--- kern/uipc_socket.c  12 Dec 2020 11:48:54 -0000      1.251
+++ kern/uipc_socket.c  23 Dec 2020 16:21:30 -0000
@@ -2040,7 +2040,7 @@ soo_kqfilter(struct file *fp, struct kno
                return (EINVAL);
        }
 
-       klist_insert(&sb->sb_sel.si_note, kn);
+       klist_insert_locked(&sb->sb_sel.si_note, kn);
        sb->sb_flagsintr |= SB_KNOTE;
 
        return (0);
@@ -2053,7 +2053,7 @@ filt_sordetach(struct knote *kn)
 
        KERNEL_ASSERT_LOCKED();
 
-       klist_remove(&so->so_rcv.sb_sel.si_note, kn);
+       klist_remove_locked(&so->so_rcv.sb_sel.si_note, kn);
        if (klist_empty(&so->so_rcv.sb_sel.si_note))
                so->so_rcv.sb_flagsintr &= ~SB_KNOTE;
 }
@@ -2106,7 +2106,7 @@ filt_sowdetach(struct knote *kn)
 
        KERNEL_ASSERT_LOCKED();
 
-       klist_remove(&so->so_snd.sb_sel.si_note, kn);
+       klist_remove_locked(&so->so_snd.sb_sel.si_note, kn);
        if (klist_empty(&so->so_snd.sb_sel.si_note))
                so->so_snd.sb_flagsintr &= ~SB_KNOTE;
 }
Index: miscfs/fifofs/fifo_vnops.c
===================================================================
RCS file: src/sys/miscfs/fifofs/fifo_vnops.c,v
retrieving revision 1.77
diff -u -p -r1.77 fifo_vnops.c
--- miscfs/fifofs/fifo_vnops.c  15 Jun 2020 15:29:40 -0000      1.77
+++ miscfs/fifofs/fifo_vnops.c  23 Dec 2020 16:21:30 -0000
@@ -532,7 +532,7 @@ fifo_kqfilter(void *v)
 
        ap->a_kn->kn_hook = so;
 
-       klist_insert(&sb->sb_sel.si_note, ap->a_kn);
+       klist_insert_locked(&sb->sb_sel.si_note, ap->a_kn);
        sb->sb_flagsintr |= SB_KNOTE;
 
        return (0);
@@ -543,7 +543,7 @@ filt_fifordetach(struct knote *kn)
 {
        struct socket *so = (struct socket *)kn->kn_hook;
 
-       klist_remove(&so->so_rcv.sb_sel.si_note, kn);
+       klist_remove_locked(&so->so_rcv.sb_sel.si_note, kn);
        if (klist_empty(&so->so_rcv.sb_sel.si_note))
                so->so_rcv.sb_flagsintr &= ~SB_KNOTE;
 }
@@ -579,7 +579,7 @@ filt_fifowdetach(struct knote *kn)
 {
        struct socket *so = (struct socket *)kn->kn_hook;
 
-       klist_remove(&so->so_snd.sb_sel.si_note, kn);
+       klist_remove_locked(&so->so_snd.sb_sel.si_note, kn);
        if (klist_empty(&so->so_snd.sb_sel.si_note))
                so->so_snd.sb_flagsintr &= ~SB_KNOTE;
 }
Index: miscfs/fuse/fuse_device.c
===================================================================
RCS file: src/sys/miscfs/fuse/fuse_device.c,v
retrieving revision 1.34
diff -u -p -r1.34 fuse_device.c
--- miscfs/fuse/fuse_device.c   13 May 2020 08:13:42 -0000      1.34
+++ miscfs/fuse/fuse_device.c   23 Dec 2020 16:21:30 -0000
@@ -555,7 +555,7 @@ fusekqfilter(dev_t dev, struct knote *kn
 
        kn->kn_hook = fd;
 
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
 
        return (0);
 }
@@ -566,7 +566,7 @@ filt_fuse_rdetach(struct knote *kn)
        struct fuse_d *fd = kn->kn_hook;
        struct klist *klist = &fd->fd_rsel.si_note;
 
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
 }
 
 int
Index: miscfs/fuse/fuse_vnops.c
===================================================================
RCS file: src/sys/miscfs/fuse/fuse_vnops.c,v
retrieving revision 1.61
diff -u -p -r1.61 fuse_vnops.c
--- miscfs/fuse/fuse_vnops.c    11 Jun 2020 09:18:43 -0000      1.61
+++ miscfs/fuse/fuse_vnops.c    23 Dec 2020 16:21:30 -0000
@@ -154,7 +154,7 @@ fusefs_kqfilter(void *v)
 
        kn->kn_hook = (caddr_t)vp;
 
-       klist_insert(&vp->v_selectinfo.si_note, kn);
+       klist_insert_locked(&vp->v_selectinfo.si_note, kn);
 
        return (0);
 }
@@ -164,7 +164,7 @@ filt_fusefsdetach(struct knote *kn)
 {
        struct vnode *vp = (struct vnode *)kn->kn_hook;
 
-       klist_remove(&vp->v_selectinfo.si_note, kn);
+       klist_remove_locked(&vp->v_selectinfo.si_note, kn);
 }
 
 int
Index: msdosfs/msdosfs_vnops.c
===================================================================
RCS file: src/sys/msdosfs/msdosfs_vnops.c,v
retrieving revision 1.134
diff -u -p -r1.134 msdosfs_vnops.c
--- msdosfs/msdosfs_vnops.c     11 Jun 2020 09:18:43 -0000      1.134
+++ msdosfs/msdosfs_vnops.c     23 Dec 2020 16:21:30 -0000
@@ -1980,7 +1980,7 @@ msdosfs_kqfilter(void *v)
 
        kn->kn_hook = (caddr_t)vp;
 
-       klist_insert(&vp->v_selectinfo.si_note, kn);
+       klist_insert_locked(&vp->v_selectinfo.si_note, kn);
 
        return (0);
 }
@@ -1990,7 +1990,7 @@ filt_msdosfsdetach(struct knote *kn)
 {
        struct vnode *vp = (struct vnode *)kn->kn_hook;
 
-       klist_remove(&vp->v_selectinfo.si_note, kn);
+       klist_remove_locked(&vp->v_selectinfo.si_note, kn);
 }
 
 int
Index: net/bpf.c
===================================================================
RCS file: src/sys/net/bpf.c,v
retrieving revision 1.197
diff -u -p -r1.197 bpf.c
--- net/bpf.c   12 Dec 2020 11:49:02 -0000      1.197
+++ net/bpf.c   23 Dec 2020 16:21:30 -0000
@@ -1195,7 +1195,7 @@ bpfkqfilter(dev_t dev, struct knote *kn)
 
        bpf_get(d);
        kn->kn_hook = d;
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
 
        mtx_enter(&d->bd_mtx);
        if (d->bd_rnonblock == 0 && d->bd_rdStart == 0)
@@ -1212,7 +1212,7 @@ filt_bpfrdetach(struct knote *kn)
 
        KERNEL_ASSERT_LOCKED();
 
-       klist_remove(&d->bd_sel.si_note, kn);
+       klist_remove_locked(&d->bd_sel.si_note, kn);
        bpf_put(d);
 }
 
Index: net/if_pppx.c
===================================================================
RCS file: src/sys/net/if_pppx.c,v
retrieving revision 1.105
diff -u -p -r1.105 if_pppx.c
--- net/if_pppx.c       20 Sep 2020 12:27:40 -0000      1.105
+++ net/if_pppx.c       23 Dec 2020 16:21:30 -0000
@@ -492,7 +492,7 @@ pppxkqfilter(dev_t dev, struct knote *kn
        kn->kn_hook = (caddr_t)pxd;
 
        mtx_enter(mtx);
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        mtx_leave(mtx);
 
        return (0);
@@ -505,7 +505,7 @@ filt_pppx_rdetach(struct knote *kn)
        struct klist *klist = &pxd->pxd_rsel.si_note;
 
        mtx_enter(&pxd->pxd_rsel_mtx);
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
        mtx_leave(&pxd->pxd_rsel_mtx);
 }
 
@@ -526,7 +526,7 @@ filt_pppx_wdetach(struct knote *kn)
        struct klist *klist = &pxd->pxd_wsel.si_note;
 
        mtx_enter(&pxd->pxd_wsel_mtx);
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
        mtx_leave(&pxd->pxd_wsel_mtx);
 }
 
@@ -1259,7 +1259,7 @@ pppackqfilter(dev_t dev, struct knote *k
        kn->kn_hook = sc;
 
        mtx_enter(mtx);
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        mtx_leave(mtx);
 
        return (0);
@@ -1272,7 +1272,7 @@ filt_pppac_rdetach(struct knote *kn)
        struct klist *klist = &sc->sc_rsel.si_note;
 
        mtx_enter(&sc->sc_rsel_mtx);
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
        mtx_leave(&sc->sc_rsel_mtx);
 }
 
@@ -1293,7 +1293,7 @@ filt_pppac_wdetach(struct knote *kn)
        struct klist *klist = &sc->sc_wsel.si_note;
 
        mtx_enter(&sc->sc_wsel_mtx);
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
        mtx_leave(&sc->sc_wsel_mtx);
 }
 
Index: net/if_tun.c
===================================================================
RCS file: src/sys/net/if_tun.c,v
retrieving revision 1.227
diff -u -p -r1.227 if_tun.c
--- net/if_tun.c        4 Oct 2020 06:59:16 -0000       1.227
+++ net/if_tun.c        23 Dec 2020 16:21:30 -0000
@@ -995,7 +995,7 @@ tun_dev_kqfilter(dev_t dev, struct knote
        kn->kn_hook = (caddr_t)sc; /* XXX give the sc_ref to the hook? */
 
        s = splhigh();
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
        splx(s);
 
 put:
@@ -1010,7 +1010,7 @@ filt_tunrdetach(struct knote *kn)
        struct tun_softc        *sc = kn->kn_hook;
 
        s = splhigh();
-       klist_remove(&sc->sc_rsel.si_note, kn);
+       klist_remove_locked(&sc->sc_rsel.si_note, kn);
        splx(s);
 }
 
@@ -1032,7 +1032,7 @@ filt_tunwdetach(struct knote *kn)
        struct tun_softc        *sc = kn->kn_hook;
 
        s = splhigh();
-       klist_remove(&sc->sc_wsel.si_note, kn);
+       klist_remove_locked(&sc->sc_wsel.si_note, kn);
        splx(s);
 }
 
Index: net/switchctl.c
===================================================================
RCS file: src/sys/net/switchctl.c,v
retrieving revision 1.22
diff -u -p -r1.22 switchctl.c
--- net/switchctl.c     12 Dec 2020 11:49:02 -0000      1.22
+++ net/switchctl.c     23 Dec 2020 16:21:30 -0000
@@ -409,7 +409,7 @@ switchkqfilter(dev_t dev, struct knote *
 
        kn->kn_hook = (caddr_t)sc;
 
-       klist_insert(klist, kn);
+       klist_insert_locked(klist, kn);
 
        return (0);
 }
@@ -420,7 +420,7 @@ filt_switch_rdetach(struct knote *kn)
        struct switch_softc     *sc = (struct switch_softc *)kn->kn_hook;
        struct klist            *klist = &sc->sc_swdev->swdev_rsel.si_note;
 
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
 }
 
 int
@@ -444,7 +444,7 @@ filt_switch_wdetach(struct knote *kn)
        struct switch_softc     *sc = (struct switch_softc *)kn->kn_hook;
        struct klist            *klist = &sc->sc_swdev->swdev_wsel.si_note;
 
-       klist_remove(klist, kn);
+       klist_remove_locked(klist, kn);
 }
 
 int
Index: nfs/nfs_kq.c
===================================================================
RCS file: src/sys/nfs/nfs_kq.c,v
retrieving revision 1.32
diff -u -p -r1.32 nfs_kq.c
--- nfs/nfs_kq.c        11 Jun 2020 09:18:43 -0000      1.32
+++ nfs/nfs_kq.c        23 Dec 2020 16:21:30 -0000
@@ -186,7 +186,7 @@ filt_nfsdetach(struct knote *kn)
 {
        struct vnode *vp = (struct vnode *)kn->kn_hook;
 
-       klist_remove(&vp->v_selectinfo.si_note, kn);
+       klist_remove_locked(&vp->v_selectinfo.si_note, kn);
 
        /* Remove the vnode from watch list */
        if ((kn->kn_flags & __EV_POLL) == 0)
@@ -343,7 +343,7 @@ nfs_kqfilter(void *v)
                        return (error);
        }
 
-       klist_insert(&vp->v_selectinfo.si_note, kn);
+       klist_insert_locked(&vp->v_selectinfo.si_note, kn);
 
        return (0);
 }
Index: sys/event.h
===================================================================
RCS file: src/sys/sys/event.h,v
retrieving revision 1.51
diff -u -p -r1.51 event.h
--- sys/event.h 20 Dec 2020 12:54:05 -0000      1.51
+++ sys/event.h 23 Dec 2020 16:21:30 -0000
@@ -246,7 +246,9 @@ extern void klist_init_mutex(struct klis
 extern void    klist_init_rwlock(struct klist *, struct rwlock *);
 extern void    klist_free(struct klist *);
 extern void    klist_insert(struct klist *, struct knote *);
+extern void    klist_insert_locked(struct klist *, struct knote *);
 extern void    klist_remove(struct klist *, struct knote *);
+extern void    klist_remove_locked(struct klist *, struct knote *);
 extern int     klist_empty(struct klist *);
 extern void    klist_invalidate(struct klist *);
 
Index: tmpfs/tmpfs_vnops.c
===================================================================
RCS file: src/sys/tmpfs/tmpfs_vnops.c,v
retrieving revision 1.44
diff -u -p -r1.44 tmpfs_vnops.c
--- tmpfs/tmpfs_vnops.c 12 Oct 2020 13:08:03 -0000      1.44
+++ tmpfs/tmpfs_vnops.c 23 Dec 2020 16:21:30 -0000
@@ -2634,7 +2634,7 @@ tmpfs_kqfilter(void *v)
 
        kn->kn_hook = (caddr_t)vp;
 
-       klist_insert(&vp->v_selectinfo.si_note, kn);
+       klist_insert_locked(&vp->v_selectinfo.si_note, kn);
 
        return (0);
 }
@@ -2644,7 +2644,7 @@ filt_tmpfsdetach(struct knote *kn)
 {
        struct vnode *vp = (struct vnode *)kn->kn_hook;
 
-       klist_remove(&vp->v_selectinfo.si_note, kn);
+       klist_remove_locked(&vp->v_selectinfo.si_note, kn);
 }
 
 int
Index: ufs/ufs/ufs_vnops.c
===================================================================
RCS file: src/sys/ufs/ufs/ufs_vnops.c,v
retrieving revision 1.152
diff -u -p -r1.152 ufs_vnops.c
--- ufs/ufs/ufs_vnops.c 11 Jun 2020 09:18:43 -0000      1.152
+++ ufs/ufs/ufs_vnops.c 23 Dec 2020 16:21:30 -0000
@@ -1934,7 +1934,7 @@ ufs_kqfilter(void *v)
 
        kn->kn_hook = (caddr_t)vp;
 
-       klist_insert(&vp->v_selectinfo.si_note, kn);
+       klist_insert_locked(&vp->v_selectinfo.si_note, kn);
 
        return (0);
 }
@@ -1944,7 +1944,7 @@ filt_ufsdetach(struct knote *kn)
 {
        struct vnode *vp = (struct vnode *)kn->kn_hook;
 
-       klist_remove(&vp->v_selectinfo.si_note, kn);
+       klist_remove_locked(&vp->v_selectinfo.si_note, kn);
 }
 
 int

Reply via email to