From nobody Thu Jul 7 15:25:02 2022 X-Original-To: freebsd-hackers@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 C8A878D711A for ; Thu, 7 Jul 2022 15:25:18 +0000 (UTC) (envelope-from sebastian.huber@embedded-brains.de) Received: from dedi548.your-server.de (dedi548.your-server.de [85.10.215.148]) (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 mx1.freebsd.org (Postfix) with ESMTPS id 4Lf0bK4LT2z3CP2 for ; Thu, 7 Jul 2022 15:25:17 +0000 (UTC) (envelope-from sebastian.huber@embedded-brains.de) Received: from sslproxy03.your-server.de ([88.198.220.132]) by dedi548.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1o9TNN-0008KH-82 for freebsd-hackers@freebsd.org; Thu, 07 Jul 2022 17:25:09 +0200 Received: from [82.100.198.138] (helo=mail.embedded-brains.de) by sslproxy03.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o9TNN-000Mxy-EK for freebsd-hackers@freebsd.org; Thu, 07 Jul 2022 17:25:09 +0200 Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 2241F4801C0 for ; Thu, 7 Jul 2022 17:25:09 +0200 (CEST) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 5WTVeDD0034b for ; Thu, 7 Jul 2022 17:25:08 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id BEC2A4801CA for ; Thu, 7 Jul 2022 17:25:08 +0200 (CEST) X-Virus-Scanned: amavisd-new at zimbra.eb.localhost Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 6UXX3L1vwF_X for ; Thu, 7 Jul 2022 17:25:08 +0200 (CEST) Received: from zimbra.eb.localhost (unknown [192.168.96.242]) by mail.embedded-brains.de (Postfix) with ESMTPSA id 9EDD94801BD for ; Thu, 7 Jul 2022 17:25:08 +0200 (CEST) From: Sebastian Huber To: freebsd-hackers@freebsd.org Subject: [PATCH 2/6] pps: Simplify capture and event processing Date: Thu, 7 Jul 2022 17:25:02 +0200 Message-Id: <20220707152506.55626-3-sebastian.huber@embedded-brains.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220707152506.55626-1-sebastian.huber@embedded-brains.de> References: <20220707152506.55626-1-sebastian.huber@embedded-brains.de> List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Authenticated-Sender: smtp-embedded@poldinet.de X-Virus-Scanned: Clear (ClamAV 0.103.6/26596/Thu Jul 7 09:53:54 2022) X-Rspamd-Queue-Id: 4Lf0bK4LT2z3CP2 X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of sebastian.huber@embedded-brains.de designates 85.10.215.148 as permitted sender) smtp.mailfrom=sebastian.huber@embedded-brains.de X-Spamd-Result: default: False [0.21 / 15.00]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_SHORT(-0.99)[-0.986]; R_MISSING_CHARSET(0.50)[]; R_SPF_ALLOW(-0.20)[+ip4:85.10.215.148]; MIME_GOOD(-0.10)[text/plain]; MLMMJ_DEST(0.00)[freebsd-hackers]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; R_DKIM_NA(0.00)[]; ASN(0.00)[asn:24940, ipnet:85.10.192.0/18, country:DE]; RCVD_TLS_LAST(0.00)[]; DMARC_NA(0.00)[embedded-brains.de]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_COUNT_SEVEN(0.00)[8]; FROM_HAS_DN(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; HAS_X_AS(0.00)[] X-ThisMailContainsUnwantedMimeParts: N Use local variables for the captured timehand and timecounter in pps_even= t(). This fixes a potential issue in the nsec preparation for hardpps(). Here= the timecounter was accessed through the captured timehand after the generati= on was checked. Make a snapshot of the relevent timehand values early in pps_event(). Ch= eck the timehand generation only once during the capture and event processing= . Use atomic_thread_fence_acq() similar to the other readers. --- sys/kern/kern_tc.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index 20520adc0aba..b466fd4399e4 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -1772,14 +1772,14 @@ pps_capture(struct pps_state *pps) #endif tc =3D th->th_counter; pps->capcount =3D tc->tc_get_timecount(tc); - atomic_thread_fence_acq(); - if (pps->capgen !=3D th->th_generation) - pps->capgen =3D 0; } =20 void pps_event(struct pps_state *pps, int event) { + struct timehands *capth; + struct timecounter *captc; + uint64_t capth_scale; struct bintime bt; struct timespec ts, *tsp, *osp; u_int tcount, *pcount; @@ -1798,9 +1798,17 @@ pps_event(struct pps_state *pps, int event) /* Nothing to do if not currently set to capture this event type. */ if ((event & pps->ppsparam.mode) =3D=3D 0) return; + + /* Make a snapshot of the captured timehand */ + capth =3D pps->capth; + captc =3D capth->th_counter; + capth_scale =3D capth->th_scale; + tcount =3D capth->th_offset_count; + bt =3D capth->th_bintime; + /* If the timecounter was wound up underneath us, bail out. */ - if (pps->capgen =3D=3D 0 || pps->capgen !=3D - atomic_load_acq_int(&pps->capth->th_generation)) + atomic_thread_fence_acq(); + if (pps->capgen =3D=3D 0 || pps->capgen !=3D capth->th_generation) return; =20 /* Things would be easier with arrays. */ @@ -1838,25 +1846,19 @@ pps_event(struct pps_state *pps, int event) * If the timecounter changed, we cannot compare the count values, so * we have to drop the rest of the PPS-stuff until the next event. */ - if (pps->ppstc !=3D pps->capth->th_counter) { - pps->ppstc =3D pps->capth->th_counter; + if (__predict_false(pps->ppstc !=3D captc)) { + pps->ppstc =3D captc; *pcount =3D pps->capcount; pps->ppscount[2] =3D pps->capcount; return; } =20 /* Convert the count to a timespec. */ - tcount =3D pps->capcount - pps->capth->th_offset_count; - tcount &=3D pps->capth->th_counter->tc_counter_mask; - bt =3D pps->capth->th_bintime; - bintime_addx(&bt, pps->capth->th_scale * tcount); + tcount =3D pps->capcount - tcount; + tcount &=3D captc->tc_counter_mask; + bintime_addx(&bt, capth_scale * tcount); bintime2timespec(&bt, &ts); =20 - /* If the timecounter was wound up underneath us, bail out. */ - atomic_thread_fence_acq(); - if (pps->capgen !=3D pps->capth->th_generation) - return; - *pcount =3D pps->capcount; (*pseq)++; *tsp =3D ts; @@ -1890,9 +1892,9 @@ pps_event(struct pps_state *pps, int event) */ tcount =3D pps->capcount - pps->ppscount[2]; pps->ppscount[2] =3D pps->capcount; - tcount &=3D pps->capth->th_counter->tc_counter_mask; + tcount &=3D captc->tc_counter_mask; scale =3D (uint64_t)1 << 63; - scale /=3D pps->capth->th_counter->tc_frequency; + scale /=3D captc->tc_frequency; scale *=3D 2; bt.sec =3D 0; bt.frac =3D 0; --=20 2.35.3