From patchwork Wed Jul 4 06:21:30 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: john stultz X-Patchwork-Id: 9812 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id A264223E23 for ; Wed, 4 Jul 2012 06:22:18 +0000 (UTC) Received: from mail-yx0-f180.google.com (mail-yx0-f180.google.com [209.85.213.180]) by fiordland.canonical.com (Postfix) with ESMTP id 56450A187CA for ; Wed, 4 Jul 2012 06:22:18 +0000 (UTC) Received: by yenq6 with SMTP id q6so6634098yen.11 for ; Tue, 03 Jul 2012 23:22:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf:from:to:cc :subject:date:message-id:x-mailer:in-reply-to:references :x-content-scanned:x-cbid:x-gm-message-state; bh=0V15xVcmZ2u5o5VfvM59EBlbqu4DJlOupc91zLLKGG4=; b=pHBQvOygSnvPDAhFM2OynAwKm9vK2N2SRxj/7vEhB+JF6Bi4+1VutvdzxN6/jJI4+P 0XDiFvNyet6G2J9h5u0/aEPVY/9yl0yBegitRzi3KnjGW29eEdCwSZK6yk+TipJ1RRfM V8TedVOMKh1aJVZE+ZJ9w+CeU7yIfd6eDZbWd3tZ84PO4GCXUXEBRD03DRxpZoPAjy9x bj/X80+V6a0CZFjIpIMTaE6/vesevatlvpGCz+aXj5003xk42cLCF51zX0R1XvxVa8/s qgexHf2Y/Al9RTEnkf4oeEcxqzLD8/I7E7wa3L5AdzJ4qlJ7zZYoXx0/WayddSvc7Ovn Efew== Received: by 10.50.40.193 with SMTP id z1mr12651445igk.0.1341382937531; Tue, 03 Jul 2012 23:22:17 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.24.148 with SMTP id v20csp49792ibb; Tue, 3 Jul 2012 23:22:17 -0700 (PDT) Received: by 10.68.130.67 with SMTP id oc3mr5368848pbb.18.1341382936904; Tue, 03 Jul 2012 23:22:16 -0700 (PDT) Received: from e3.ny.us.ibm.com (e3.ny.us.ibm.com. [32.97.182.143]) by mx.google.com with ESMTPS id ue4si31073116pbc.213.2012.07.03.23.22.16 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 03 Jul 2012 23:22:16 -0700 (PDT) Received-SPF: pass (google.com: domain of johnstul@us.ibm.com designates 32.97.182.143 as permitted sender) client-ip=32.97.182.143; Authentication-Results: mx.google.com; spf=pass (google.com: domain of johnstul@us.ibm.com designates 32.97.182.143 as permitted sender) smtp.mail=johnstul@us.ibm.com Received: from /spool/local by e3.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 4 Jul 2012 02:22:14 -0400 Received: from d01dlp01.pok.ibm.com (9.56.224.56) by e3.ny.us.ibm.com (192.168.1.103) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 4 Jul 2012 02:21:56 -0400 Received: from d01relay03.pok.ibm.com (d01relay03.pok.ibm.com [9.56.227.235]) by d01dlp01.pok.ibm.com (Postfix) with ESMTP id 9A32338C806A for ; Wed, 4 Jul 2012 02:21:55 -0400 (EDT) Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by d01relay03.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q646LtIS261334 for ; Wed, 4 Jul 2012 02:21:55 -0400 Received: from d01av01.pok.ibm.com (loopback [127.0.0.1]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q64Bqmfr019363 for ; Wed, 4 Jul 2012 07:52:48 -0400 Received: from kernel.stglabs.ibm.com (kernel.stglabs.ibm.com [9.114.214.19]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q64BqmM5019347; Wed, 4 Jul 2012 07:52:48 -0400 From: John Stultz To: Linux Kernel Cc: John Stultz , Prarit Bhargava , stable@vger.kernel.org, Thomas Gleixner , linux@openhuawei.org Subject: [PATCH 3/3] [RFC] hrtimer: Update hrtimer base offsets each hrtimer_interrupt Date: Wed, 4 Jul 2012 02:21:30 -0400 Message-Id: <1341382890-42324-4-git-send-email-johnstul@us.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1341382890-42324-1-git-send-email-johnstul@us.ibm.com> References: <1341382890-42324-1-git-send-email-johnstul@us.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12070406-8974-0000-0000-00000AD7406A X-Gm-Message-State: ALoCoQmygZoDKLcjzLVANYRWWB3dQK+rm2KxLLtm9vRvIZzkLC50ZqMP1r0RMZKntHevYpGNAoKm This patch introduces a new funciton which captures the CLOCK_MONOTONIC time, along with the CLOCK_REALTIME and CLOCK_BOOTTIME offsets at the same moment. This new function is then used in place of ktime_get() when hrtimer_interrupt() is expiring timers. This ensures that any changes to realtime or boottime offsets are noticed and stored into the per-cpu hrtimer base structures, prior to doing any hrtimer expiration. This should ensure that timers are not expired early if the offsets changes under us. This is useful in the case where clock_was_set() is called from atomic context and have to schedule the hrtimer base offset update via a timer, as it provides extra robustness in the face of any possible timer delay. CC: Prarit Bhargava CC: stable@vger.kernel.org CC: Thomas Gleixner CC: linux@openhuawei.org Signed-off-by: John Stultz --- include/linux/hrtimer.h | 3 +++ kernel/hrtimer.c | 14 +++++++++++--- kernel/time/timekeeping.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h index fd0dc30..f6b2a74 100644 --- a/include/linux/hrtimer.h +++ b/include/linux/hrtimer.h @@ -320,6 +320,9 @@ extern ktime_t ktime_get(void); extern ktime_t ktime_get_real(void); extern ktime_t ktime_get_boottime(void); extern ktime_t ktime_get_monotonic_offset(void); +extern void ktime_get_and_real_and_sleep_offset(ktime_t *monotonic, + ktime_t *real_offset, + ktime_t *sleep_offset); DECLARE_PER_CPU(struct tick_device, tick_cpu_device); diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c index d730678..56600c4 100644 --- a/kernel/hrtimer.c +++ b/kernel/hrtimer.c @@ -1258,18 +1258,26 @@ static void __run_hrtimer(struct hrtimer *timer, ktime_t *now) void hrtimer_interrupt(struct clock_event_device *dev) { struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); - ktime_t expires_next, now, entry_time, delta; + ktime_t expires_next, now, entry_time, delta, real_offset, sleep_offset; int i, retries = 0; BUG_ON(!cpu_base->hres_active); cpu_base->nr_events++; dev->next_event.tv64 = KTIME_MAX; - entry_time = now = ktime_get(); + + ktime_get_and_real_and_sleep_offset(&now, &real_offset, &sleep_offset); + + entry_time = now; retry: expires_next.tv64 = KTIME_MAX; raw_spin_lock(&cpu_base->lock); + + /* Update base offsets, to avoid early wakeups */ + cpu_base->clock_base[HRTIMER_BASE_REALTIME].offset = real_offset; + cpu_base->clock_base[HRTIMER_BASE_BOOTTIME].offset = sleep_offset; + /* * We set expires_next to KTIME_MAX here with cpu_base->lock * held to prevent that a timer is enqueued in our queue via @@ -1346,7 +1354,7 @@ retry: * interrupt routine. We give it 3 attempts to avoid * overreacting on some spurious event. */ - now = ktime_get(); + ktime_get_and_real_and_sleep_offset(&now, &real_offset, &sleep_offset); cpu_base->nr_retries++; if (++retries < 3) goto retry; diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index cc2991d..b3404cf 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1251,6 +1251,40 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim, } /** + * ktime_get_and_real_and_sleep_offset() - hrtimer helper, gets monotonic ktime, + * realtime offset, and sleep offsets. + */ +void ktime_get_and_real_and_sleep_offset(ktime_t *monotonic, + ktime_t *real_offset, + ktime_t *sleep_offset) +{ + unsigned long seq; + struct timespec wtom, sleep; + u64 secs, nsecs; + + do { + seq = read_seqbegin(&timekeeper.lock); + + secs = timekeeper.xtime.tv_sec + + timekeeper.wall_to_monotonic.tv_sec; + nsecs = timekeeper.xtime.tv_nsec + + timekeeper.wall_to_monotonic.tv_nsec; + nsecs += timekeeping_get_ns(); + /* If arch requires, add in gettimeoffset() */ + nsecs += arch_gettimeoffset(); + + wtom = timekeeper.wall_to_monotonic; + sleep = timekeeper.total_sleep_time; + } while (read_seqretry(&timekeeper.lock, seq)); + + *monotonic = ktime_add_ns(ktime_set(secs, 0), nsecs); + set_normalized_timespec(&wtom, -wtom.tv_sec, -wtom.tv_nsec); + *real_offset = timespec_to_ktime(wtom); + *sleep_offset = timespec_to_ktime(sleep); +} + + +/** * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format */ ktime_t ktime_get_monotonic_offset(void)