From nobody Wed Oct 06 07:07:29 2021 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id EA26D12D703B; Wed, 6 Oct 2021 07:07:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HPQWQ3C7zz3jcT; Wed, 6 Oct 2021 07:07:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 16B7E1BC51; Wed, 6 Oct 2021 07:07:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 19677TMg025191; Wed, 6 Oct 2021 07:07:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 19677TIX025190; Wed, 6 Oct 2021 07:07:29 GMT (envelope-from git) Date: Wed, 6 Oct 2021 07:07:29 GMT Message-Id: <202110060707.19677TIX025190@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: f9765c9c1acb - stable/12 - kqueue: don't arbitrarily restrict long-past values for NOTE_ABSTIME List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: f9765c9c1acbd42c074841c96c0e84d32ad1b1a1 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=f9765c9c1acbd42c074841c96c0e84d32ad1b1a1 commit f9765c9c1acbd42c074841c96c0e84d32ad1b1a1 Author: Kyle Evans AuthorDate: 2021-09-29 19:55:59 +0000 Commit: Kyle Evans CommitDate: 2021-10-06 07:07:07 +0000 kqueue: don't arbitrarily restrict long-past values for NOTE_ABSTIME NOTE_ABSTIME values are converted to values relative to boottime in filt_timervalidate(), and negative values are currently rejected. We don't reject times in the past in general, so clamp this up to 0 as needed such that the timer fires immediately rather than imposing what looks like an arbitrary restriction. Another possible scenario is that the system clock had to be adjusted by ~minutes or ~hours and we have less than that in terms of uptime, making a reasonable short-timeout suddenly invalid. Firing it is still a valid choice in this scenario so that applications can at least expect a consistent behavior. (cherry picked from commit 9c999a259f00b35f0467acd351fea9157ed7e1e4) (cherry picked from commit 2f4dbe279f6b5eb87ec493d96f6943ffdb603ba0) --- sys/kern/kern_event.c | 12 +++- tests/sys/kqueue/libkqueue/timer.c | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index af611281ee68..d6f4516a265c 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -721,13 +721,13 @@ filt_timervalidate(struct knote *kn, sbintime_t *to) return (EINVAL); *to = timer2sbintime(kn->kn_sdata, kn->kn_sfflags); + if (*to < 0) + return (EINVAL); if ((kn->kn_sfflags & NOTE_ABSTIME) != 0) { getboottimebin(&bt); sbt = bttosbt(bt); - *to -= sbt; + *to = MAX(0, *to - sbt); } - if (*to < 0) - return (EINVAL); return (0); } @@ -739,9 +739,15 @@ filt_timerattach(struct knote *kn) unsigned int ncallouts; int error; + to = -1; error = filt_timervalidate(kn, &to); if (error != 0) return (error); + KASSERT(to > 0 || (kn->kn_flags & EV_ONESHOT) != 0 || + (kn->kn_sfflags & NOTE_ABSTIME) != 0, + ("%s: periodic timer has a calculated zero timeout", __func__)); + KASSERT(to >= 0, + ("%s: timer has a calculated negative timeout", __func__)); do { ncallouts = kq_ncallouts; diff --git a/tests/sys/kqueue/libkqueue/timer.c b/tests/sys/kqueue/libkqueue/timer.c index 1861030d697a..81ab364b796c 100644 --- a/tests/sys/kqueue/libkqueue/timer.c +++ b/tests/sys/kqueue/libkqueue/timer.c @@ -247,6 +247,117 @@ test_abstime(void) success(); } +static void +test_abstime_epoch(void) +{ + const char *test_id = "kevent(EVFILT_TIMER (EPOCH), NOTE_ABSTIME)"; + struct kevent kev; + + test_begin(test_id); + + test_no_kevents(); + + EV_SET(&kev, vnode_fd, EVFILT_TIMER, EV_ADD, NOTE_ABSTIME, 0, + NULL); + if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) + err(1, "%s", test_id); + + /* Retrieve the event */ + kev.flags = EV_ADD; + kev.data = 1; + kev.fflags = 0; + kevent_cmp(&kev, kevent_get(kqfd)); + + /* Delete the event */ + kev.flags = EV_DELETE; + if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) + err(1, "%s", test_id); + + success(); +} + +static void +test_abstime_preboot(void) +{ + const char *test_id = "kevent(EVFILT_TIMER (PREBOOT), EV_ONESHOT, NOTE_ABSTIME)"; + struct kevent kev; + struct timespec btp; + uint64_t end, start, stop; + + test_begin(test_id); + + test_no_kevents(); + + /* + * We'll expire it at just before system boot (roughly) with the hope that + * we'll get an ~immediate expiration, just as we do for any value specified + * between system boot and now. + */ + start = now(); + if (clock_gettime(CLOCK_BOOTTIME, &btp) != 0) + err(1, "%s", test_id); + + end = start - SEC_TO_US(btp.tv_sec + 1); + EV_SET(&kev, vnode_fd, EVFILT_TIMER, EV_ADD | EV_ONESHOT, + NOTE_ABSTIME | NOTE_USECONDS, end, NULL); + if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) + err(1, "%s", test_id); + + /* Retrieve the event */ + kev.flags = EV_ADD | EV_ONESHOT; + kev.data = 1; + kev.fflags = 0; + kevent_cmp(&kev, kevent_get(kqfd)); + + stop = now(); + if (stop < end) + err(1, "too early %jd %jd", (intmax_t)stop, (intmax_t)end); + /* Check if the event occurs again */ + sleep(3); + test_no_kevents(); + + success(); +} + +static void +test_abstime_postboot(void) +{ + const char *test_id = "kevent(EVFILT_TIMER (POSTBOOT), EV_ONESHOT, NOTE_ABSTIME)"; + struct kevent kev; + uint64_t end, start, stop; + const int timeout_sec = 1; + + test_begin(test_id); + + test_no_kevents(); + + /* + * Set a timer for 1 second ago, it should fire immediately rather than + * being rejected. + */ + start = now(); + end = start - SEC_TO_US(timeout_sec); + EV_SET(&kev, vnode_fd, EVFILT_TIMER, EV_ADD | EV_ONESHOT, + NOTE_ABSTIME | NOTE_USECONDS, end, NULL); + if (kevent(kqfd, &kev, 1, NULL, 0, NULL) < 0) + err(1, "%s", test_id); + + /* Retrieve the event */ + kev.flags = EV_ADD | EV_ONESHOT; + kev.data = 1; + kev.fflags = 0; + kevent_cmp(&kev, kevent_get(kqfd)); + + stop = now(); + if (stop < end) + err(1, "too early %jd %jd", (intmax_t)stop, (intmax_t)end); + /* Check if the event occurs again */ + sleep(3); + test_no_kevents(); + + success(); +} + static void test_update(void) { @@ -517,6 +628,9 @@ test_evfilt_timer() test_oneshot(); test_periodic(); test_abstime(); + test_abstime_epoch(); + test_abstime_preboot(); + test_abstime_postboot(); test_update(); test_update_equal(); test_update_expired();