From patchwork Tue May 23 00:53:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 100344 Delivered-To: patches@linaro.org Received: by 10.140.96.100 with SMTP id j91csp92613qge; Mon, 22 May 2017 17:53:47 -0700 (PDT) X-Received: by 10.84.216.77 with SMTP id f13mr32075796plj.130.1495500827556; Mon, 22 May 2017 17:53:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1495500827; cv=none; d=google.com; s=arc-20160816; b=WI0wVpUulcmARuCqUGFE9zpAhq5c7JaogH3RDJ8sU2rDKOF4+alCBURxNDUvDtBXQ8 kYBhIAfPoiVS3LoHhXSUzuMeaQg8erUgx5HLrhbhtFU6Zq9E2ff5XDsgBe667iiT0H7b AZT6yyP4GAjzc+NRZz/U7h7RJWTgoV6SRdcaJJiMePdc4y3XVOhy35Sa+yAbpFmmh3oR n73mjKdrKQOd6zzy5k8iproGH/DPmYzQ9N/p5a/VA7oYIdrps9egiW0PNxsfhx7ocR/G QO2c4HuA8CyaS63So6/Fik+8cX1Pk1i3muKDDeMybImOkZ+LNhyNo9ewJo6IAa7UAko6 vSMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=dJTvAJENVXhpNyY8ETJGQANwPvGnZOzgfs3pGXd9dyA=; b=yyuSx1oHwW9ZZPc55eqQkKB3YybYmGfCAghNF8dDtNWOhIQU/q49CbAznrS5n26BMN NsL1VFDg/qJn5qpcMagCqPX+ZUxBAbc8nzFIL2FhZrPiRCeCdIZbJQ/OszP1ziyAsRSy p6d87GxfJ4V7FpIzScdGHzE5Ne1Q5iNEBNcOfnnJci3b3vC+TNtA7Yyu75oUWv/62HQ9 pdS/psTuKn8CXZlKxGQKOjMZOdYnkqnkONzlSNg45CgbPbrLmCYzVVB/X16WU7QMtwJ9 8rKtdiKBJ5liAmuZg91klpUKcXD7Qbcj2ezg2stzB99gsZVmEykgEY7wv/cvkiFHHH2c f+QQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22e as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-pf0-x22e.google.com (mail-pf0-x22e.google.com. [2607:f8b0:400e:c00::22e]) by mx.google.com with ESMTPS id g5si18756593plm.37.2017.05.22.17.53.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 May 2017 17:53:47 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22e as permitted sender) client-ip=2607:f8b0:400e:c00::22e; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22e as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by mail-pf0-x22e.google.com with SMTP id m17so97561077pfg.3 for ; Mon, 22 May 2017 17:53:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=dJTvAJENVXhpNyY8ETJGQANwPvGnZOzgfs3pGXd9dyA=; b=GlPK1plt3d8+Jju2A2VP2CNeZ2tM/Kf0/wyaEU6GoxUEbiuSo0LY/aRsAdU5AvspGr aATFVyxmkegmYo+g1jwP3K9rpakOztd4l3UI2nr4KAdEj+/93p4PciEDh5ykh8k/PiSI znGMjmx2w2bYpLCU8wRGmE7JBPkHZzmWHzuSg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=dJTvAJENVXhpNyY8ETJGQANwPvGnZOzgfs3pGXd9dyA=; b=E9AhexrSfmpJ+PuIQIcQn/vAMrcLAMbwTiS45kUGZ5N8tKIMDNkU2MjsU8UyCHX0Bi T1UFZjcMYM7C850kU8XE+X2o8WiTY3Sb4h1FBYIDSV1kB0UuSi/I7h9Vmv6tZs3OPkS1 IUUGV9JdCHaVCNUGe5FTcfQGa9BTteuDXbGE2mVRn2bjEpdaSsqG8Qs3VbHbiLTq/mLB XIGnk2BstKsFe+NCUEGd26VIdFjfaAidSJNnDc83dRxnNGQtMvmje8LXiM8IFIbetbYX Y75+t9pg2YR+ra1m+hHlwjFX2X/3z6XBCZJm7ZFeVDY7ARSCx8IHWnu99Pp7LOKrtsh3 vPcA== X-Gm-Message-State: AODbwcDcZdh/lC7ZU4EVaokCS1zjpzeKtSEqpXw1pvevdT7SEgMo+1nV c162sFRthGCCycNfaVE= X-Received: by 10.99.95.79 with SMTP id t76mr28501366pgb.82.1495500827123; Mon, 22 May 2017 17:53:47 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:1002:83f0:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id b129sm14426733pfg.102.2017.05.22.17.53.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 22 May 2017 17:53:46 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Thomas Gleixner , Ingo Molnar , Miroslav Lichvar , Richard Cochran , Prarit Bhargava , Stephen Boyd Subject: [RFC][PATCH] time: Fix clock->read(clock) race around clocksource changes Date: Mon, 22 May 2017 17:53:39 -0700 Message-Id: <1495500819-31970-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 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. 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. 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 Cc: Ingo Molnar Cc: Miroslav Lichvar Cc: Richard Cochran Cc: Prarit Bhargava Cc: Stephen Boyd Signed-off-by: John Stultz --- kernel/time/timekeeping.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) -- 2.7.4 diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 8c1523d..501a173 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). + */ +static inline u64 tk_clock_read(struct tk_read_base *tkr) +{ + struct clocksource *clock = READ_ONCE(tkr->clock); + + return clock->read(clock); +} + #ifdef CONFIG_DEBUG_TIMEKEEPING #define WARNING_FREQ (HZ*300) /* 5 minute rate-limiting */ @@ -175,7 +195,7 @@ static inline u64 timekeeping_get_delta(struct tk_read_base *tkr) */ do { seq = read_seqcount_begin(&tk_core.seq); - now = tkr->read(tkr->clock); + now = tk_clock_read(tkr); last = tkr->cycle_last; mask = tkr->mask; max = tkr->clock->max_cycles; @@ -209,7 +229,7 @@ static inline u64 timekeeping_get_delta(struct tk_read_base *tkr) u64 cycle_now, delta; /* read clocksource */ - cycle_now = tkr->read(tkr->clock); + cycle_now = tk_clock_read(tkr); /* calculate the delta since the last update_wall_time */ delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask); @@ -240,7 +260,7 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) tk->tkr_mono.clock = clock; tk->tkr_mono.read = clock->read; tk->tkr_mono.mask = clock->mask; - tk->tkr_mono.cycle_last = tk->tkr_mono.read(clock); + tk->tkr_mono.cycle_last = tk_clock_read(&tk->tkr_mono); tk->tkr_raw.clock = clock; tk->tkr_raw.read = clock->read; @@ -477,7 +497,7 @@ static void halt_fast_timekeeper(struct timekeeper *tk) struct tk_read_base *tkr = &tk->tkr_mono; memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy)); - cycles_at_suspend = tkr->read(tkr->clock); + cycles_at_suspend = tk_clock_read(tkr); tkr_dummy.read = dummy_clock_read; update_fast_timekeeper(&tkr_dummy, &tk_fast_mono); @@ -650,11 +670,10 @@ static void timekeeping_update(struct timekeeper *tk, unsigned int action) */ static void timekeeping_forward_now(struct timekeeper *tk) { - struct clocksource *clock = tk->tkr_mono.clock; u64 cycle_now, delta; u64 nsec; - cycle_now = tk->tkr_mono.read(clock); + cycle_now = tk_clock_read(&tk->tkr_mono); delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last, tk->tkr_mono.mask); tk->tkr_mono.cycle_last = cycle_now; tk->tkr_raw.cycle_last = cycle_now; @@ -930,8 +949,7 @@ void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot) do { seq = read_seqcount_begin(&tk_core.seq); - - now = tk->tkr_mono.read(tk->tkr_mono.clock); + now = tk_clock_read(&tk->tkr_mono); systime_snapshot->cs_was_changed_seq = tk->cs_was_changed_seq; systime_snapshot->clock_was_set_seq = tk->clock_was_set_seq; base_real = ktime_add(tk->tkr_mono.base, @@ -1109,7 +1127,7 @@ int get_device_system_crosststamp(int (*get_time_fn) * Check whether the system counter value provided by the * device driver is on the current timekeeping interval. */ - now = tk->tkr_mono.read(tk->tkr_mono.clock); + now = tk_clock_read(&tk->tkr_mono); interval_start = tk->tkr_mono.cycle_last; if (!cycle_between(interval_start, cycles, now)) { clock_was_set_seq = tk->clock_was_set_seq; @@ -1630,7 +1648,7 @@ void timekeeping_resume(void) * The less preferred source will only be tried if there is no better * usable source. The rtc part is handled separately in rtc core code. */ - cycle_now = tk->tkr_mono.read(clock); + cycle_now = tk_clock_read(&tk->tkr_mono); if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) && cycle_now > tk->tkr_mono.cycle_last) { u64 nsec, cyc_delta; @@ -2031,7 +2049,7 @@ void update_wall_time(void) #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET offset = real_tk->cycle_interval; #else - offset = clocksource_delta(tk->tkr_mono.read(tk->tkr_mono.clock), + offset = clocksource_delta(tk_clock_read(&tk->tkr_mono), tk->tkr_mono.cycle_last, tk->tkr_mono.mask); #endif