public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Martin Kaistra <martin.kaistra@linutronix.de>,
	Florian Fainelli <f.fainelli@gmail.com>,
	Andrew Lunn <andrew@lunn.ch>,
	Vivien Didelot <vivien.didelot@gmail.com>
Cc: martin.kaistra@linutronix.de,
	Richard Cochran <richardcochran@gmail.com>,
	Vladimir Oltean <olteanv@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	John Stultz <john.stultz@linaro.org>,
	Stephen Boyd <sboyd@kernel.org>,
	Russell King <linux@armlinux.org.uk>,
	Marc Kleine-Budde <mkl@pengutronix.de>,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH v2 3/7] timecounter: allow for non-power of two overflow
Date: Wed, 24 Nov 2021 21:55:44 +0100	[thread overview]
Message-ID: <874k81l1db.ffs@tglx> (raw)
In-Reply-To: <20211109095013.27829-4-martin.kaistra@linutronix.de>

Martin,

On Tue, Nov 09 2021 at 10:50, Martin Kaistra wrote:
>   *			see CYCLECOUNTER_MASK() helper macro
>   * @mult:		cycle to nanosecond multiplier
>   * @shift:		cycle to nanosecond divisor (power of two)
> + * @overflow_point:	non-power of two overflow point (optional),
> + *			smaller than mask
>   */
>  struct cyclecounter {
>  	u64 (*read)(const struct cyclecounter *cc);
>  	u64 mask;
>  	u32 mult;
>  	u32 shift;
> +	u64 overflow_point;
>  };
>  
>  /**
> diff --git a/kernel/time/timecounter.c b/kernel/time/timecounter.c
> index e6285288d765..afd2910a9724 100644
> --- a/kernel/time/timecounter.c
> +++ b/kernel/time/timecounter.c
> @@ -39,6 +39,9 @@ static u64 timecounter_read_delta(struct timecounter *tc)
>  	/* calculate the delta since the last timecounter_read_delta(): */
>  	cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
>  
> +	if (tc->cc->overflow_point && (cycle_now - tc->cycle_last) > tc->cc->mask)
> +		cycle_delta -= tc->cc->mask - tc->cc->overflow_point;

TBH, this took me more than one twisted braincell to grok.

With support for clocks which do not wrap at power of 2 boundaries we
already lose the unconditional fast path no matter what. So what's the
point of having two conditions and doing this convoluted math here?

In timecounter_init():

   	tc->ovfl = cc->ovfl ? cc->ovfl : cc->mask + 1;

which makes it a common path in timecounter_read_delta():

  	cycle_delta = cycle_now - tc->cycle_last;
        if ((s64)cycle_delta) < 0)
        	cycle_delta += tc->ovfl;

which produces way better binary code.

The conditional does not really matter for the timecounter use cases as
that calculation is noise compared to the actual cc->read() access.

Aside of that the same problem exists in timecounter_cyc2time()...

After that we probably should do a treewide sweep to get rid of cc->mask
to avoid confusion and subtle to understand errors when some code uses
cc->mask instead of cc->ovfl.

Thanks,

        tglx

  reply	other threads:[~2021-11-24 20:55 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-09  9:50 [PATCH v2 0/7] Add PTP support for BCM53128 switch Martin Kaistra
2021-11-09  9:50 ` [PATCH v2 1/7] net: dsa: b53: Add BroadSync HD register definitions Martin Kaistra
2021-11-09 10:10   ` Kurt Kanzenbach
2021-11-09 18:04   ` Florian Fainelli
2021-11-10  8:19     ` Martin Kaistra
2021-11-09  9:50 ` [PATCH v2 2/7] net: dsa: b53: Move struct b53_device to include/linux/dsa/b53.h Martin Kaistra
2021-11-09 18:05   ` Florian Fainelli
2021-11-09 18:11     ` Florian Fainelli
2021-11-09 18:15       ` Vladimir Oltean
2021-11-09 18:20         ` Florian Fainelli
2021-11-09 18:49           ` Vladimir Oltean
2021-11-09  9:50 ` [PATCH v2 3/7] timecounter: allow for non-power of two overflow Martin Kaistra
2021-11-24 20:55   ` Thomas Gleixner [this message]
2021-11-09  9:50 ` [PATCH v2 4/7] net: dsa: b53: Add PHC clock support Martin Kaistra
2021-11-09 18:08   ` Florian Fainelli
2021-11-09  9:50 ` [PATCH v2 5/7] net: dsa: b53: Add logic for RX timestamping Martin Kaistra
2021-11-09 18:07   ` Florian Fainelli
2021-11-10  8:43     ` Martin Kaistra
2021-11-09  9:50 ` [PATCH v2 6/7] net: dsa: b53: Add logic for TX timestamping Martin Kaistra
2021-11-09 11:12   ` Vladimir Oltean
2021-11-10  7:14     ` Kurt Kanzenbach
2021-11-10 13:05       ` Vladimir Oltean
2021-11-10 13:30         ` Vladimir Oltean
2021-11-10 13:47         ` Kurt Kanzenbach
2021-11-10 14:00           ` Vladimir Oltean
2021-11-10 15:08         ` Richard Cochran
2021-11-10 15:23           ` Vladimir Oltean
2021-11-10 12:57   ` Vladimir Oltean
2021-11-09  9:50 ` [PATCH v2 7/7] net: dsa: b53: Expose PTP timestamping ioctls to userspace Martin Kaistra
2021-11-09 10:39 ` [PATCH v2 0/7] Add PTP support for BCM53128 switch Vladimir Oltean
2021-11-09 11:13   ` Martin Kaistra
2021-11-09 18:13     ` Florian Fainelli

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=874k81l1db.ffs@tglx \
    --to=tglx@linutronix.de \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=f.fainelli@gmail.com \
    --cc=john.stultz@linaro.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=martin.kaistra@linutronix.de \
    --cc=mkl@pengutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=richardcochran@gmail.com \
    --cc=sboyd@kernel.org \
    --cc=vivien.didelot@gmail.com \
    /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