Page MenuHomeFreeBSD

kevent: Hold the knlist mutex when invoking f_event(NOTE_FORK)
ClosedPublic

Authored by markj on Nov 14 2025, 5:51 PM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Dec 11, 7:50 AM
Unknown Object (File)
Sun, Dec 7, 1:55 AM
Unknown Object (File)
Thu, Nov 27, 9:52 AM
Unknown Object (File)
Tue, Nov 25, 7:44 PM
Unknown Object (File)
Thu, Nov 20, 6:48 AM
Unknown Object (File)
Thu, Nov 20, 12:46 AM
Unknown Object (File)
Thu, Nov 20, 12:45 AM
Unknown Object (File)
Thu, Nov 20, 12:45 AM
Subscribers

Details

Summary

In general f_event is supposed to be called with the knlist mutex held;
this is one case where it is not. I think it is mostly harmless, but we
might as well obey the formal locking protocol.

PR: 291005

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

markj requested review of this revision.Nov 14 2025, 5:51 PM
sys/kern/kern_event.c
637

Wht do we need the knote list locked for kn_fflags modifications?

sys/kern/kern_event.c
637

Isn't kn->kn_event effectively serialized by the knote list lock? I started reading the case in the kqueue_scan() loop where we handle EV_CLEAR and I don't see how this update is serialized against those stores.

kib added inline comments.
sys/kern/kern_event.c
637

You mean, that practically all modifications of kn_kevent are happen to occur under the kn list lock?

Sometimes the lock used as kn list lock is rw (e.g. for vnodes), and updates to kn_kevent can occur while lock is taken in shared mode. E.g. for vnode reads.

This should not invalidate the change above, since kn_lock() should be always exclusive in such cases, but it is not obvious to me that the statement about kn_kevent serialization is true.

Anyway, please add a comment.

This revision is now accepted and ready to land.Mon, Nov 17, 3:51 PM
sys/kern/kern_event.c
637

Sometimes the lock used as kn list lock is rw (e.g. for vnodes), and updates to kn_kevent can occur while lock is taken in shared mode. E.g. for vnode reads.

Hmm, but the f_event implementations for vnodes (filt_vfsread(), filt_vfswrite(), filt_vfsvnode()) all acquire the vnode interlock internally, so they serialize with each other, and vfs_knllock() acquires the exclusive vnode lock, so I think updates are still serialized. Or am I missing something?

sys/kern/kern_event.c
637

Yes, interlock there helps, using the common part of exclusive vnode lock vs. shared vnode lock + interlock. What I tried to say is that kn list lock probably not should be considered as kn_kevent lock due to this detail, in general.

  • Add a comment.
  • Acquire the kqueue lock earlier if we're activating the knote, otherwise we will pick up and drop the kqueue lock twice in a row.
This revision now requires review to proceed.Mon, Nov 17, 6:56 PM
sys/kern/kern_event.c
632
markj marked an inline comment as done.

Fix the comment.

This revision is now accepted and ready to land.Tue, Nov 18, 4:01 AM