Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178397767
D62518.1796664004.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None
D62518.1796664004.diff
View Options
diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -2095,6 +2095,12 @@
/*
* Scan, update kn_data (if not ONESHOT), and copyout triggered events.
* We treat KN_MARKER knotes as if they are in flux.
+ *
+ * With peek, specified by k_ops == NULL, the triggered events are
+ * only counted against maxevents, and stay queued. The knotes
+ * without events are still discarded, which leaves kq_count non-zero
+ * only if there are events to report. In peek mode, the function
+ * returns with the kqueue locked.
*/
static int
kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
@@ -2111,6 +2117,10 @@
error = 0;
haskqglobal = 0;
+ /* Peek must not take the early exits below, they return unlocked. */
+ MPASS(k_ops != NULL || (maxevents > 0 &&
+ (tsp == NULL || timespecvalid_interval(tsp))));
+
if (maxevents == 0)
goto done_nl;
if (maxevents < 0) {
@@ -2208,6 +2218,9 @@
knote_drop(kn, td);
KQ_LOCK(kq);
continue;
+ } else if (k_ops == NULL &&
+ (kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
+ TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
} else if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
kn->kn_status &= ~KN_QUEUED;
kn_enter_flux(kn);
@@ -2241,13 +2254,17 @@
}
touch = (!kn->kn_fop->f_isfd &&
kn->kn_fop->f_touch != NULL);
- if (touch)
- kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
- else
- *kevp = kn->kn_kevent;
+ if (k_ops != NULL) {
+ if (touch)
+ kn->kn_fop->f_touch(kn, kevp,
+ EVENT_PROCESS);
+ else
+ *kevp = kn->kn_kevent;
+ }
KQ_LOCK(kq);
KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
- if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
+ if (k_ops != NULL &&
+ (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) != 0) {
/*
* Manually clear knotes who weren't
* 'touch'ed.
@@ -2269,10 +2286,13 @@
influx = 1;
}
+ count--;
+ if (k_ops == NULL)
+ continue;
+
/* we are returning a copy to the user */
kevp++;
nkev++;
- count--;
if (nkev == KQ_NEVENTS) {
influx = 0;
@@ -2288,12 +2308,15 @@
TAILQ_REMOVE(&kq->kq_head, &marker, kn_tqe);
done:
KQ_OWNED(kq);
- KQ_UNLOCK_FLUX(kq);
+ if (k_ops != NULL) {
+ KQ_UNLOCK_FLUX(kq);
done_nl:
- KQ_NOTOWNED(kq);
+ KQ_NOTOWNED(kq);
+ }
if (nkev != 0)
error = k_ops->k_copyout(k_ops->arg, keva, nkev);
- td->td_retval[0] = maxevents - count;
+ if (k_ops != NULL)
+ td->td_retval[0] = maxevents - count;
return (error);
}
@@ -2345,30 +2368,39 @@
return (ENOTTY);
}
-/*ARGSUSED*/
static int
kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
- struct thread *td)
+ struct thread *td)
{
struct kqueue *kq;
- int revents = 0;
- int error;
+ struct timespec ts;
+ int error, revents;
if ((error = kqueue_acquire(fp, &kq)))
- return POLLERR;
+ return (POLLERR);
- KQ_LOCK(kq);
- if (events & (POLLIN | POLLRDNORM)) {
- if (kq->kq_count) {
- revents |= events & (POLLIN | POLLRDNORM);
- } else {
- selrecord(td, &kq->kq_sel);
- if (SEL_WAITING(&kq->kq_sel))
- kq->kq_state |= KQ_SEL;
- }
+ if ((events & (POLLIN | POLLRDNORM)) == 0) {
+ kqueue_release(kq, 0);
+ return (0);
+ }
+
+ revents = 0;
+
+ /*
+ * Discard the stale knotes, so that kq_count is non-zero
+ * only if kevent(2) would return an event.
+ */
+ timespecclear(&ts);
+ (void)kqueue_scan(kq, 1, NULL, &ts, NULL, td);
+ if (kq->kq_count) {
+ revents |= events & (POLLIN | POLLRDNORM);
+ } else {
+ selrecord(td, &kq->kq_sel);
+ if (SEL_WAITING(&kq->kq_sel))
+ kq->kq_state |= KQ_SEL;
}
kqueue_release(kq, 1);
- KQ_UNLOCK(kq);
+ KQ_UNLOCK_FLUX(kq);
return (revents);
}
diff --git a/tests/sys/kqueue/Makefile b/tests/sys/kqueue/Makefile
--- a/tests/sys/kqueue/Makefile
+++ b/tests/sys/kqueue/Makefile
@@ -4,7 +4,8 @@
BINDIR= ${TESTSDIR}
ATF_TESTS_C+= kqueue_fork \
- kqueue_peek_signal
+ kqueue_peek_signal \
+ kqueue_poll
NETBSD_ATF_TESTS_C= proc1_test
# XXX: fails `ke.fflags & NOTE_TRACKERR` invariant
diff --git a/tests/sys/kqueue/kqueue_poll.c b/tests/sys/kqueue/kqueue_poll.c
new file mode 100644
--- /dev/null
+++ b/tests/sys/kqueue/kqueue_poll.c
@@ -0,0 +1,163 @@
+/*-
+ * Copyright (c) 2026 The FreeBSD Foundation
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ */
+
+/*
+ * poll(2) must not report a kqueue as readable when its queued knotes
+ * have no events that kevent(2) would return.
+ */
+
+#include <sys/types.h>
+#include <sys/event.h>
+
+#include <atf-c.h>
+#include <errno.h>
+#include <poll.h>
+#include <stdbool.h>
+#include <string.h>
+#include <unistd.h>
+
+static void
+kq_change(int kq, uintptr_t ident, short filter, u_short flags, u_int fflags)
+{
+ struct kevent kev;
+
+ EV_SET(&kev, ident, filter, flags, fflags, 0, NULL);
+ ATF_REQUIRE_MSG(kevent(kq, &kev, 1, NULL, 0, NULL) == 0,
+ "kevent: %s", strerror(errno));
+}
+
+static int
+kq_get(int kq, struct kevent *kev)
+{
+ struct timespec ts;
+ int n;
+
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+ n = kevent(kq, NULL, 0, kev, 1, &ts);
+ ATF_REQUIRE_MSG(n != -1, "kevent: %s", strerror(errno));
+ return (n);
+}
+
+static void
+kq_poll(int kq, bool readable)
+{
+ struct pollfd pfd;
+ int n;
+
+ pfd.fd = kq;
+ pfd.events = POLLIN;
+ pfd.revents = 0;
+ n = poll(&pfd, 1, 0);
+ ATF_REQUIRE_MSG(n != -1, "poll: %s", strerror(errno));
+ if (readable) {
+ ATF_REQUIRE_MSG(n == 1 && pfd.revents == POLLIN,
+ "kqueue with an event is not readable, revents %#x",
+ pfd.revents);
+ } else {
+ ATF_REQUIRE_MSG(n == 0,
+ "kqueue without events is readable, revents %#x",
+ pfd.revents);
+ }
+}
+
+ATF_TC_WITHOUT_HEAD(user_clear);
+ATF_TC_BODY(user_clear, tc)
+{
+ struct kevent kev;
+ int kq;
+
+ kq = kqueue();
+ ATF_REQUIRE(kq != -1);
+ kq_change(kq, 1, EVFILT_USER, EV_ADD | EV_CLEAR, 0);
+ kq_change(kq, 1, EVFILT_USER, 0, NOTE_TRIGGER);
+ kq_poll(kq, true);
+
+ /* Reset the triggered event, the knote stays queued. */
+ kq_change(kq, 1, EVFILT_USER, EV_CLEAR, 0);
+ kq_poll(kq, false);
+ ATF_REQUIRE_EQ(0, kq_get(kq, &kev));
+
+ /* poll(2) leaves the event to kevent(2). */
+ kq_change(kq, 1, EVFILT_USER, 0, NOTE_TRIGGER);
+ kq_poll(kq, true);
+ ATF_REQUIRE_EQ(1, kq_get(kq, &kev));
+ ATF_REQUIRE(kev.ident == 1 && kev.filter == EVFILT_USER);
+ kq_poll(kq, false);
+
+ ATF_REQUIRE(close(kq) == 0);
+}
+
+ATF_TC_WITHOUT_HEAD(user_disable);
+ATF_TC_BODY(user_disable, tc)
+{
+ struct kevent kev;
+ int kq;
+
+ kq = kqueue();
+ ATF_REQUIRE(kq != -1);
+ kq_change(kq, 1, EVFILT_USER, EV_ADD, 0);
+ kq_change(kq, 1, EVFILT_USER, 0, NOTE_TRIGGER);
+ kq_poll(kq, true);
+
+ /* Disable the knote with the event, it stays queued. */
+ kq_change(kq, 1, EVFILT_USER, EV_DISABLE, 0);
+ kq_poll(kq, false);
+ ATF_REQUIRE_EQ(0, kq_get(kq, &kev));
+
+ /* The event is reported again after the knote is enabled. */
+ kq_change(kq, 1, EVFILT_USER, EV_ENABLE, 0);
+ kq_poll(kq, true);
+ ATF_REQUIRE_EQ(1, kq_get(kq, &kev));
+ ATF_REQUIRE(kev.ident == 1 && kev.filter == EVFILT_USER);
+
+ ATF_REQUIRE(close(kq) == 0);
+}
+
+ATF_TC_WITHOUT_HEAD(pipe_drained);
+ATF_TC_BODY(pipe_drained, tc)
+{
+ struct kevent kev;
+ int kq, p[2];
+ char c;
+
+ ATF_REQUIRE(pipe(p) == 0);
+ kq = kqueue();
+ ATF_REQUIRE(kq != -1);
+ kq_change(kq, p[0], EVFILT_READ, EV_ADD, 0);
+ kq_poll(kq, false);
+
+ c = 'x';
+ ATF_REQUIRE(write(p[1], &c, 1) == 1);
+ kq_poll(kq, true);
+
+ /* Drain the pipe, the knote stays queued. */
+ ATF_REQUIRE(read(p[0], &c, 1) == 1);
+ kq_poll(kq, false);
+ ATF_REQUIRE_EQ(0, kq_get(kq, &kev));
+
+ ATF_REQUIRE(write(p[1], &c, 1) == 1);
+ kq_poll(kq, true);
+ ATF_REQUIRE_EQ(1, kq_get(kq, &kev));
+ ATF_REQUIRE(kev.ident == (uintptr_t)p[0] && kev.filter == EVFILT_READ);
+ ATF_REQUIRE_EQ(1, kev.data);
+
+ ATF_REQUIRE(close(kq) == 0);
+ ATF_REQUIRE(close(p[0]) == 0);
+ ATF_REQUIRE(close(p[1]) == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, user_clear);
+ ATF_TP_ADD_TC(tp, user_disable);
+ ATF_TP_ADD_TC(tp, pipe_drained);
+
+ return (atf_no_error());
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Dec 7, 5:20 PM (20 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42278209
Default Alt Text
D62518.1796664004.diff (7 KB)
Attached To
Mode
D62518: kqueue_poll(): do not report stale knotes
Attached
Detach File
Event Timeline