From: Ingo Molnar <mingo@kernel.org>
To: John Stultz <john.stultz@linaro.org>
Cc: lkml <linux-kernel@vger.kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
Miroslav Lichvar <mlichvar@redhat.com>,
Richard Cochran <richardcochran@gmail.com>,
Prarit Bhargava <prarit@redhat.com>,
Stephen Boyd <stephen.boyd@linaro.org>,
Daniel Mentz <danielmentz@google.com>
Subject: Re: [RFC][PATCH 1/4] time: Fix clock->read(clock) race around clocksource changes
Date: Sat, 27 May 2017 09:31:39 +0200 [thread overview]
Message-ID: <20170527073138.btn3qta2af6b2wzu@gmail.com> (raw)
In-Reply-To: <1495856035-6622-2-git-send-email-john.stultz@linaro.org>
* John Stultz <john.stultz@linaro.org> wrote:
> In some testing on arm64 platforms, I was seeing null ptr
> crashes in the kselftest/timers clocksource-switch test.
>
> This was happening in a read function like:
> u64 clocksource_mmio_readl_down(struct clocksource *c)
> {
> return ~(u64)readl_relaxed(to_mmio_clksrc(c)->reg) & c->mask;
> }
>
> Where the callers enter the seqlock, and then call something
> like:
> cycle_now = tkr->read(tkr->clock);
>
> The problem seeming to be that since the read and clock
> references are happening separately, its possible the
> clocksource change happens in between and we end up calling the
> old read with the new clocksource, (or vice-versa) which causes
> the to_mmio_clksrc() in the read function to run off into space.
Maybe:
s/clock/->clock pointer
s/read/old ->read() function
for increased clarity - because both are ambiguous in an English sentence.
>
> This patch tries to address the issue by providing a helper
> function that atomically reads the clock value and then calls
> the clock->read(clock) call so that we always call the read
> funciton with the appropriate clocksource and don't accidentally
> mix them.
s/call/function
because 'call' is used both as a verb and as noun here, making for a difficult
read.
>
> The one exception where this helper isn't necessary is for the
> fast-timekepers which use their own locking and update logic
> to the tkr structures.
>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@kernel.org>
> Cc: Miroslav Lichvar <mlichvar@redhat.com>
> Cc: Richard Cochran <richardcochran@gmail.com>
> Cc: Prarit Bhargava <prarit@redhat.com>
> Cc: Stephen Boyd <stephen.boyd@linaro.org>
> Cc: Daniel Mentz <danielmentz@google.com>
> Signed-off-by: John Stultz <john.stultz@linaro.org>
> ---
> kernel/time/timekeeping.c | 40 +++++++++++++++++++++++++++++-----------
> 1 file changed, 29 insertions(+), 11 deletions(-)
>
> diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
> index 9652bc5..abc1968 100644
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -118,6 +118,26 @@ static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
> tk->offs_boot = ktime_add(tk->offs_boot, delta);
> }
>
> +/*
> + * tk_clock_read - atomic clocksource read() helper
> + *
> + * This helper is necessary to use in the read paths as, while the seqlock
> + * ensures we don't return a bad value while structures are updated, it
> + * doesn't protect from potential crashes. There is the possibility that
> + * the tkr's clocksource may change between the read reference, and the
> + * clock refernce passed to the read function. This can cause crashes if
> + * the wrong clocksource is passed to the wrong read function.
> + * This isn't necessary to use when holding the timekeeper_lock or doing
> + * a read of the fast-timekeeper tkrs (which is protected by its own locking
> + * and update logic).
typo:
s/refernce/reference
I'd also do:
s/as/because
because while 'as' is not wrong, it's somewhat harder to parse on first reading.
Other than that:
Acked-by: Ingo Molnar <mingo@kernel.org>
Thanks,
Ingo
next prev parent reply other threads:[~2017-05-27 7:31 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-27 3:33 [RFC][PATCH 0/4] Fixes for two recently found timekeeping bugs John Stultz
2017-05-27 3:33 ` [RFC][PATCH 1/4] time: Fix clock->read(clock) race around clocksource changes John Stultz
2017-05-27 7:31 ` Ingo Molnar [this message]
2017-05-27 3:33 ` [RFC][PATCH 2/4] time: Fix CLOCK_MONOTONIC_RAW sub-nanosecond accounting John Stultz
2017-05-27 7:36 ` Ingo Molnar
2017-05-30 18:42 ` Daniel Mentz
2017-05-27 3:33 ` [RFC][PATCH 3/4] arm64: vdso: Fix nsec handling for CLOCK_MONOTONIC_RAW John Stultz
2017-05-30 9:38 ` Will Deacon
2017-05-27 3:33 ` [RFC][PATCH 4/4] time: Clean up CLOCK_MONOTONIC_RAW time handling John Stultz
2017-08-25 13:40 ` Chris Wilson
2017-08-25 18:55 ` John Stultz
2017-08-25 21:16 ` John Stultz
2017-08-25 22:57 ` [RFC][PATCH] time: Fix ktime_get_raw() issues caused by incorrect base accumulation John Stultz
2017-08-26 10:20 ` Chris Wilson
2017-08-26 14:10 ` [tip:timers/urgent] time: Fix ktime_get_raw() " tip-bot for John Stultz
2017-05-27 7:38 ` [RFC][PATCH 0/4] Fixes for two recently found timekeeping bugs Ingo Molnar
2017-05-27 16:16 ` John Stultz
2017-05-28 8:54 ` Ingo Molnar
[not found] ` <CAE2F3rBuOJqLs5Cu7A9wEruZj1Vmnpy6qAYW=U9FVAOEP73pdg@mail.gmail.com>
2017-05-31 0:11 ` Daniel Mentz
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=20170527073138.btn3qta2af6b2wzu@gmail.com \
--to=mingo@kernel.org \
--cc=danielmentz@google.com \
--cc=john.stultz@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mlichvar@redhat.com \
--cc=prarit@redhat.com \
--cc=richardcochran@gmail.com \
--cc=stephen.boyd@linaro.org \
--cc=tglx@linutronix.de \
/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
Powered by JetHome