Diff below is the second part of the refactoring [0] that got committed
then reverted 6 months ago.

The idea of this refactoring is to move the accounting and markers used
when collecting events to a context (struct kqueue_scan_state) such that
the collecting routine (kqueue_scan()) can be called multiple times.

Please test this especially with X and report back.

[0] https://marc.info/?l=openbsd-tech&m=158739418817156&w=2

Index: kern/kern_event.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_event.c,v
retrieving revision 1.143
diff -u -p -r1.143 kern_event.c
--- kern/kern_event.c   11 Oct 2020 07:11:59 -0000      1.143
+++ kern/kern_event.c   19 Oct 2020 08:47:00 -0000
@@ -905,7 +905,6 @@ kqueue_scan(struct kqueue_scan_state *sc
 
        nkev = 0;
        kevp = kev;
-
        count = maxevents;
        if (count == 0)
                goto done;
@@ -921,7 +920,8 @@ retry:
 
        s = splhigh();
        if (kq->kq_count == 0) {
-               if (tsp != NULL && !timespecisset(tsp)) {
+               if ((tsp != NULL && !timespecisset(tsp)) ||
+                   scan->kqs_nevent != 0) {
                        splx(s);
                        error = 0;
                        goto done;
@@ -937,18 +937,32 @@ retry:
                goto done;
        }
 
-       TAILQ_INSERT_TAIL(&kq->kq_head, &scan->kqs_end, kn_tqe);
+       /*
+        * Put the end marker in the queue to limit the scan to the events
+        * that are currently active.  This prevents events from being
+        * recollected if they reactivate during scan.
+        *
+        * If a partial scan has been performed already but no events have
+        * been collected, reposition the end marker to make any new events
+        * reachable.
+        */
+       if (!scan->kqs_queued) {
+               TAILQ_INSERT_TAIL(&kq->kq_head, &scan->kqs_end, kn_tqe);
+               scan->kqs_queued = 1;
+       } else if (scan->kqs_nevent == 0) {
+               TAILQ_REMOVE(&kq->kq_head, &scan->kqs_end, kn_tqe);
+               TAILQ_INSERT_TAIL(&kq->kq_head, &scan->kqs_end, kn_tqe);
+       }
+
        TAILQ_INSERT_HEAD(&kq->kq_head, &scan->kqs_start, kn_tqe);
        while (count) {
                kn = TAILQ_NEXT(&scan->kqs_start, kn_tqe);
                if (kn->kn_filter == EVFILT_MARKER) {
                        if (kn == &scan->kqs_end) {
-                               TAILQ_REMOVE(&kq->kq_head, &scan->kqs_end,
-                                   kn_tqe);
                                TAILQ_REMOVE(&kq->kq_head, &scan->kqs_start,
                                    kn_tqe);
                                splx(s);
-                               if (count == maxevents)
+                               if (scan->kqs_nevent == 0)
                                        goto retry;
                                goto done;
                        }
@@ -984,6 +998,9 @@ retry:
                *kevp = kn->kn_kevent;
                kevp++;
                nkev++;
+               count--;
+               scan->kqs_nevent++;
+
                if (kn->kn_flags & EV_ONESHOT) {
                        splx(s);
                        kn->kn_fop->f_detach(kn);
@@ -1009,7 +1026,6 @@ retry:
                        knote_release(kn);
                }
                kqueue_check(kq);
-               count--;
                if (nkev == KQ_NEVENTS) {
                        splx(s);
 #ifdef KTRACE
@@ -1026,7 +1042,6 @@ retry:
                                break;
                }
        }
-       TAILQ_REMOVE(&kq->kq_head, &scan->kqs_end, kn_tqe);
        TAILQ_REMOVE(&kq->kq_head, &scan->kqs_start, kn_tqe);
        splx(s);
 done:
@@ -1059,15 +1074,21 @@ void
 kqueue_scan_finish(struct kqueue_scan_state *scan)
 {
        struct kqueue *kq = scan->kqs_kq;
+       int s;
 
        KASSERT(scan->kqs_start.kn_filter == EVFILT_MARKER);
        KASSERT(scan->kqs_start.kn_status == KN_PROCESSING);
        KASSERT(scan->kqs_end.kn_filter == EVFILT_MARKER);
        KASSERT(scan->kqs_end.kn_status == KN_PROCESSING);
 
+       if (scan->kqs_queued) {
+               scan->kqs_queued = 0;
+               s = splhigh();
+               TAILQ_REMOVE(&kq->kq_head, &scan->kqs_end, kn_tqe);
+               splx(s);
+       }
        KQRELE(kq);
 }
-
 
 /*
  * XXX
Index: sys/event.h
===================================================================
RCS file: /cvs/src/sys/sys/event.h,v
retrieving revision 1.46
diff -u -p -r1.46 event.h
--- sys/event.h 11 Oct 2020 07:11:59 -0000      1.46
+++ sys/event.h 19 Oct 2020 08:39:51 -0000
@@ -204,6 +204,9 @@ struct kqueue_scan_state {
        struct kqueue   *kqs_kq;                /* kqueue of this scan */
        struct knote     kqs_start;             /* start marker */
        struct knote     kqs_end;               /* end marker */
+       int              kqs_nevent;            /* number of events collected */
+       int              kqs_queued;            /* if set, end marker is
+                                                * in queue */
 };
 
 struct proc;
@@ -223,6 +226,7 @@ extern int  kqueue_scan(struct kqueue_sca
                    struct timespec *, struct kevent *, struct proc *, int *);
 extern void    kqueue_scan_setup(struct kqueue_scan_state *, struct kqueue *);
 extern void    kqueue_scan_finish(struct kqueue_scan_state *);
+extern void    kqueue_purge(struct proc *, struct kqueue *);
 extern int     filt_seltrue(struct knote *kn, long hint);
 extern int     seltrue_kqfilter(dev_t, struct knote *);
 extern void    klist_insert(struct klist *, struct knote *);

Reply via email to