mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: Rodolfo Giometti <giometti@enneenne.com>,
	Richard Cochran	 <richardcochran@gmail.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>,
	 Paolo Abeni <pabeni@redhat.com>,
	John Stultz <jstultz@google.com>,
	Thomas Gleixner <tglx@kernel.org>,
	 Stephen Boyd <sboyd@kernel.org>,
	Miroslav Lichvar <mlichvar@redhat.com>,
	linux-kernel@vger.kernel.org, 	netdev@vger.kernel.org,
	Alexander Gordeev <agordeev@linux.ibm.com>
Subject: Re: [PATCH v4 3/4] pps: Always use ktime_get_snapshot_id() for pps_get_ts()
Date: Sat, 26 Sep 2026 21:38:05 +0100	[thread overview]
Message-ID: <d1af17dff1d6f181d74edd3b6358398469d61917.camel@infradead.org> (raw)
In-Reply-To: <920a2d70-c1ec-452b-8fc4-aebf1f6af412@enneenne.com>

[-- Attachment #1: Type: text/plain, Size: 4167 bytes --]

On Tue, 2026-09-01 at 17:35 +0200, Rodolfo Giometti wrote:
> 
> Separately: are we sure that calling ktime_get_snapshot_id() does not
> introduce much larger delays than ktime_get_real_ts64()? pps_get_ts()
> runs in hard IRQ, and in pps-gpio it is the first statement of the
> handler. Whatever it costs sits between the edge and the timestamp,
> and that is the one thing PPS has to keep short.
> 
> I would like to see that measured on something other than an x86 VM
> with a TSC. A small 32-bit ARM board is what I worry about.

Not 32-bit but I had a Banana Pi R64 lying around (Cortex A53, 12.5MHz
arch counter) and I bought it a GPS hat.

So yes, the specific code path you're looking at *does* get slightly
longer (50ns to the counter read instead of 30ns). Some of which is
easily reclaimable with some optimisations...

Firstly, why in $DEITY's name does the Arm kernel not set
CONFIG_ARCH_WANTS_CLOCKSOURCE_READ_INLINE? Setting that and using it
gains us 7-8ns back in both paths. I can get another 2-3ns back by
optimising the CLOCK_REALTIME path through ktime_get_snapshot_id() and
only hitting the case statement for other clock IDs:

                                    ┌───────────────┬─────────────────┐
                                    │  ktime_get_   │   ktime_get_    │
                                    │  real_ts64()  │  snapshot_id()  │
────────────────────────────────────┼───────────────┼─────────────────┤
 stock                              │    30.2 ns    │     49.9 ns     │
 + arm64 inlined clocksource read   │    23.4 ns    │     49.9 ns     │
 + inlined in get_snapshot_id() too │    23.2 ns    │     41.2 ns     │
 + CLOCK_REALTIME dispatch bias     │    23.4 ns    │     38.8 ns     │
────────────────────────────────────┴───────────────┴─────────────────┘

So I'll probably do those things because I've seen them now.

However, they are *entirely* in the noise, as there's about 600 ns of
hardware and 2-4 *microseconds* of software latency before we even get
there.

I was pondering an IRQF_HWTIMESTAMP which would sample the arch-defined
inline clocksource as early as possible and stash it in the pt_regs, so
I knocked up a proof of concept which just did that unconditionally in
kernel_entry in arch/arm64/kernel/entry.S. That's what gives me the
software latency I cited (~2.1µs from that stamp to the GPIO handler). 

To measure the hardware latency I removed the hat and looped the PPS
pin back from an output GPIO, then read the counter the instruction
before the MMIO write to trigger a rising edge. That gave me 0.6µs to
the counter read in entry.S.

 pin edge → exception entry stamp:   min 0.40µs  med 0.64µs  p90 0.72µs
 entry stamp → pps handler:          min 2.08µs  med 2.16µs  p90 2.56µs
 pin edge → pps handler (sum):       min 2.72µs  med 2.80µs  p90 3.12µs

The latency is higher from cold with an actual PPS signal, as opposed
to the warm soak test.

So no, I really don't care about the tiny cost of calling
ktime_get_snapshot_id() to get a more accurate timestamp; at least that
cost is *constant* unlike the sawtoothing ntp_error that it eliminates
from the readings. (Which is actually larger than it should be; I have
more fixes for ntp_error.)

I'm still interested in the IRQF_HWTIMESTAMP thing. As that sample
would be taken outside the tkd->seq count we'd need to use something
like get_system_device_crosststamp() to reliably interpret it, but I'm
going to have to implement that *anyway*. I want to add support for a
hardware module which *latches* the counter value on the pulse, instead
of waiting for the CPU to wake up and do so for itself. One of those
TimeCard devices with PCIe PTM could do that, for example.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 6179 bytes --]

  parent reply	other threads:[~2026-09-26 20:38 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-29 20:56 [PATCH v4 0/4] Add ntp_error to clock snapshot, enable NTP_PPS on tickless kernel David Woodhouse
2026-08-29 20:56 ` [PATCH v4 1/4] timekeeping: Apply extrapolated ntp_error to clock snapshots David Woodhouse
2026-08-29 20:57 ` [PATCH v4 2/4] pps: Drop the !NO_HZ_COMMON dependency from NTP_PPS David Woodhouse
2026-09-01 15:35   ` Rodolfo Giometti
2026-09-02  0:13     ` David Woodhouse
2026-09-28 13:37     ` David Woodhouse
2026-09-28 16:41       ` Rodolfo Giometti
2026-09-28 19:28         ` David Woodhouse
2026-09-29  6:33           ` Rodolfo Giometti
2026-09-29  9:32             ` David Woodhouse
2026-09-29 11:48               ` Rodolfo Giometti
2026-09-29 12:02                 ` David Woodhouse
2026-09-30  1:28                 ` David Woodhouse
2026-09-30 12:57                   ` Rodolfo Giometti
2026-09-30 10:37                 ` David Woodhouse
2026-09-30 12:57                   ` Rodolfo Giometti
2026-09-30 14:05                     ` David Woodhouse
2026-09-30 18:24                       ` David Woodhouse
2026-10-01  8:20                         ` Rodolfo Giometti
2026-10-01  9:08                           ` David Woodhouse
2026-08-29 20:57 ` [PATCH v4 3/4] pps: Always use ktime_get_snapshot_id() for pps_get_ts() David Woodhouse
2026-09-01 15:35   ` Rodolfo Giometti
2026-09-01 23:56     ` David Woodhouse
2026-09-26 20:38     ` David Woodhouse [this message]
2026-09-28  7:58       ` Rodolfo Giometti
2026-09-28 12:59         ` David Woodhouse
2026-09-28 16:41           ` Rodolfo Giometti
2026-10-01 13:14         ` Miroslav Lichvar
2026-10-01 15:38           ` David Woodhouse
2026-10-02  7:04           ` Rodolfo Giometti
2026-10-02  9:07             ` David Woodhouse
2026-10-02 12:29               ` David Woodhouse
2026-10-02 13:44                 ` David Woodhouse
2026-10-03 11:29                   ` David Woodhouse
2026-10-05  9:16                   ` Miroslav Lichvar
2026-08-29 20:57 ` [PATCH v4 4/4] [DO NOT MERGE] ptp: ptp_vmclock: Add simulated 1PPS support David Woodhouse
2026-09-01 15:35 ` [PATCH v4 0/4] Add ntp_error to clock snapshot, enable NTP_PPS on tickless kernel Rodolfo Giometti
2026-09-01 23:37   ` David Woodhouse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d1af17dff1d6f181d74edd3b6358398469d61917.camel@infradead.org \
    --to=dwmw2@infradead.org \
    --cc=agordeev@linux.ibm.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=giometti@enneenne.com \
    --cc=jstultz@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mlichvar@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=richardcochran@gmail.com \
    --cc=sboyd@kernel.org \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®