From owner-dev-commits-src-main@freebsd.org Mon May 3 16:51:03 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id CF40463726C; Mon, 3 May 2021 16:51:03 +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 4FYprl5Z5kz3Fkx; Mon, 3 May 2021 16:51:03 +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 B107F219A8; Mon, 3 May 2021 16:51:03 +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 143Gp3kx065157; Mon, 3 May 2021 16:51:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 143Gp31q065156; Mon, 3 May 2021 16:51:03 GMT (envelope-from git) Date: Mon, 3 May 2021 16:51:03 GMT Message-Id: <202105031651.143Gp31q065156@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 7cb40543e964 - main - filt_timerexpire: do not iterate over the interval MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7cb40543e96451092d5bc6bb3d96ebee364327e0 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 May 2021 16:51:03 -0000 The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=7cb40543e96451092d5bc6bb3d96ebee364327e0 commit 7cb40543e96451092d5bc6bb3d96ebee364327e0 Author: Konstantin Belousov AuthorDate: 2021-04-28 16:28:49 +0000 Commit: Konstantin Belousov CommitDate: 2021-05-03 16:49:54 +0000 filt_timerexpire: do not iterate over the interval User-supplied data might make this loop too time-consuming. Divide directly, and handle both the possibility that we were woken up earlier, and arithmetic overflows/underflows from the calculation. Reported and tested by: pho (previous version) Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D30069 --- sys/kern/kern_event.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c index 33a6cdcda486..1067e7f128b7 100644 --- a/sys/kern/kern_event.c +++ b/sys/kern/kern_event.c @@ -718,6 +718,7 @@ filt_timerexpire_l(struct knote *kn, bool proc_locked) { struct kq_timer_cb_data *kc; struct proc *p; + uint64_t delta; sbintime_t now; kc = kn->kn_ptr.p_v; @@ -728,9 +729,17 @@ filt_timerexpire_l(struct knote *kn, bool proc_locked) return; } - for (now = sbinuptime(); kc->next <= now; kc->next += kc->to) - kn->kn_data++; - KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */ + now = sbinuptime(); + if (now >= kc->next) { + delta = (now - kc->next) / kc->to; + if (delta == 0) + delta = 1; + kn->kn_data += delta; + kc->next += (delta + 1) * kc->to; + if (now >= kc->next) /* overflow */ + kc->next = now + kc->to; + KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */ + } /* * Initial check for stopped kc->p is racy. It is fine to