[PATCH RFC] timekeeping: don't use seqcount loop in ktime_mono_to_any on 64-bit arch

Jeff Layton posted 1 patch 2 months, 2 weeks ago
kernel/time/timekeeping.c | 10 ++++++++++
1 file changed, 10 insertions(+)
[PATCH RFC] timekeeping: don't use seqcount loop in ktime_mono_to_any on 64-bit arch
Posted by Jeff Layton 2 months, 2 weeks ago
ktime_mono_to_any only fetches the offset inside the loop. This is a
single word on 64-bit arch, and seqcount_read_begin implies a full SMP
barrier. While we do want to use the latest offset value available, a
full seqcount loop is overkill on 64-bit, where there is no possibility
of torn reads. Just do a READ_ONCE for that and don't bother with the
seqcount.

Cc: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 kernel/time/timekeeping.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 5391e4167d60..0693f44e064e 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -924,6 +924,15 @@ EXPORT_SYMBOL_GPL(ktime_get_coarse_with_offset);
  * @tmono:	time to convert.
  * @offs:	which offset to use
  */
+#if BITS_PER_LONG == 64
+ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
+{
+	ktime_t *offset = offsets[offs];
+
+	return ktime_add(tmono, READ_ONCE(*offset));
+}
+EXPORT_SYMBOL_GPL(ktime_mono_to_any);
+#else /* BITS_PER_LONG == 64 */
 ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
 {
 	ktime_t *offset = offsets[offs];
@@ -938,6 +947,7 @@ ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
 	return tconv;
 }
 EXPORT_SYMBOL_GPL(ktime_mono_to_any);
+#endif /* BITS_PER_LONG == 64 */
 
 /**
  * ktime_get_raw - Returns the raw monotonic time in ktime_t format

---
base-commit: 79d4f9eb4a3c4c68736d500025423e96e212b13d
change-id: 20240910-mgtime-731eace7cca5

Best regards,
-- 
Jeff Layton <jlayton@kernel.org>
Re: [PATCH RFC] timekeeping: don't use seqcount loop in ktime_mono_to_any on 64-bit arch
Posted by Thomas Gleixner 2 months, 2 weeks ago
On Tue, Sep 10 2024 at 07:17, Jeff Layton wrote:

Please describe functions with foo() and not foo. Also please refrain
from using abbreviations. The 'arch' above is not really useful.

64-bit systems perhaps?

> ktime_mono_to_any only fetches the offset inside the loop. This is a
> single word on 64-bit arch, and seqcount_read_begin implies a full SMP
> barrier. While we do want to use the latest offset value available, a

We do nothing.

> full seqcount loop is overkill on 64-bit, where there is no possibility
> of torn reads. Just do a READ_ONCE for that and don't bother with the
> seqcount.

don't bother is not really a technical term.

https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#changelog

> +#if BITS_PER_LONG == 64
> +ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
> +{
> +	ktime_t *offset = offsets[offs];
> +
> +	return ktime_add(tmono, READ_ONCE(*offset));

Where is the corresponing WRITE_ONCE()?

> +}
> +EXPORT_SYMBOL_GPL(ktime_mono_to_any);
> +#else /* BITS_PER_LONG == 64 */
>  EXPORT_SYMBOL_GPL(ktime_mono_to_any);
> +#endif /* BITS_PER_LONG == 64 */

Why do we need this export twice?

Thanks,

        tglx
Re: [PATCH RFC] timekeeping: don't use seqcount loop in ktime_mono_to_any on 64-bit arch
Posted by Jeff Layton 2 months, 2 weeks ago
On Tue, 2024-09-10 at 13:58 +0200, Thomas Gleixner wrote:
> On Tue, Sep 10 2024 at 07:17, Jeff Layton wrote:
> 
> Please describe functions with foo() and not foo. Also please refrain
> from using abbreviations. The 'arch' above is not really useful.
> 
> 64-bit systems perhaps?
> 

Thanks, will fix.

> > ktime_mono_to_any only fetches the offset inside the loop. This is a
> > single word on 64-bit arch, and seqcount_read_begin implies a full SMP
> > barrier. While we do want to use the latest offset value available, a
> 
> We do nothing.
> 

I assume you mean "we do not". Fair enough. I misinterpreted the
purpose of the seqcount loop then.

> > full seqcount loop is overkill on 64-bit, where there is no possibility
> > of torn reads. Just do a READ_ONCE for that and don't bother with the
> > seqcount.
> 
> don't bother is not really a technical term.
> 
> https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#changelog
> 

Thanks, fixed.

> > +#if BITS_PER_LONG == 64
> > +ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
> > +{
> > +	ktime_t *offset = offsets[offs];
> > +
> > +	return ktime_add(tmono, READ_ONCE(*offset));
> 
> Where is the corresponing WRITE_ONCE()?
> 

I'll just make it do a simple fetch without READ_ONCE.

> > +}
> > +EXPORT_SYMBOL_GPL(ktime_mono_to_any);
> > +#else /* BITS_PER_LONG == 64 */
> >  EXPORT_SYMBOL_GPL(ktime_mono_to_any);
> > +#endif /* BITS_PER_LONG == 64 */
> 
> Why do we need this export twice?
> 

We don't (obviously). I wasn't sure whether it was preferred to put it
inside the #ifdef.

Now that I look though, the resulting helper is very simple. Would it
be better to make this a static inline instead of an exported symbol?

----------------8<----------------
ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
{
        ktime_t *offset = offsets[offs];

        return ktime_add(tmono, *offset);
}
----------------8<----------------

-- 
Jeff Layton <jlayton@kernel.org>
Re: [PATCH RFC] timekeeping: don't use seqcount loop in ktime_mono_to_any on 64-bit arch
Posted by Thomas Gleixner 2 months, 2 weeks ago
On Tue, Sep 10 2024 at 08:32, Jeff Layton wrote:
> On Tue, 2024-09-10 at 13:58 +0200, Thomas Gleixner wrote:
>> > +#if BITS_PER_LONG == 64
>> > +ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
>> > +{
>> > +	ktime_t *offset = offsets[offs];
>> > +
>> > +	return ktime_add(tmono, READ_ONCE(*offset));
>> 
>> Where is the corresponing WRITE_ONCE()?
>> 
> I'll just make it do a simple fetch without READ_ONCE.

Which will make KCSAN complain ...

So yes, READ_ONCE() is the correct thing todo, but then we want to have
the counterpart at the write sides.

Thanks,

        tglx