From patchwork Mon Dec 17 18:51:54 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 13629 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 7854F23E02 for ; Mon, 17 Dec 2012 18:52:36 +0000 (UTC) Received: from mail-ia0-f180.google.com (mail-ia0-f180.google.com [209.85.210.180]) by fiordland.canonical.com (Postfix) with ESMTP id 932DDA18503 for ; Mon, 17 Dec 2012 18:52:35 +0000 (UTC) Received: by mail-ia0-f180.google.com with SMTP id t4so5625847iag.11 for ; Mon, 17 Dec 2012 10:52:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:x-forwarded-to:x-forwarded-for:delivered-to:x-received :received-spf:from:to:cc:subject:date:message-id:x-mailer :x-content-scanned:x-cbid:x-gm-message-state; bh=spqAIhRAUNN0jh+0J37crvbRWbUsqqGl8bpINaHsD7k=; b=fmQFkU4ldfMmE31ASeGJ612krPNTQeRSiDy0gAs9LXY681bKxRAfmp7gB4EB07w4X0 P9w0ajmHVj3TXOo1KzphQLKVQ4l/hbLuykdNJT67JwVS+J4HezJQMuipDjFuXeAITlDj xMgTCx9LBQCGWapL1nwgS2z3Gn1VCikO2JloJPtM4iUetKmiOdsM5cZOx8wm05v0rU6n MhxHtRoIYH9v2rh3lbYNdhB+6hkDwB2PxX6EnyB5txMB5tYCrKx5WqFO7/Bvog/tXSdn 4vgoT6RR3i83/8OpClZjL/+GNXPKF6Yv4k/jbloqmE6ZTA73g1fQI9sKTRqJ2AC1GEoc GuTQ== X-Received: by 10.50.91.195 with SMTP id cg3mr10127642igb.57.1355770354876; Mon, 17 Dec 2012 10:52:34 -0800 (PST) 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.50.67.148 with SMTP id n20csp92474igt; Mon, 17 Dec 2012 10:52:34 -0800 (PST) X-Received: by 10.50.187.230 with SMTP id fv6mr10096419igc.48.1355770353470; Mon, 17 Dec 2012 10:52:33 -0800 (PST) Received: from e8.ny.us.ibm.com (e8.ny.us.ibm.com. [32.97.182.138]) by mx.google.com with ESMTPS id g8si13910989icx.37.2012.12.17.10.52.32 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 17 Dec 2012 10:52:33 -0800 (PST) Received-SPF: neutral (google.com: 32.97.182.138 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) client-ip=32.97.182.138; Authentication-Results: mx.google.com; spf=neutral (google.com: 32.97.182.138 is neither permitted nor denied by best guess record for domain of john.stultz@linaro.org) smtp.mail=john.stultz@linaro.org Received: from /spool/local by e8.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 17 Dec 2012 13:52:31 -0500 Received: from d01dlp02.pok.ibm.com (9.56.250.167) by e8.ny.us.ibm.com (192.168.1.108) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 17 Dec 2012 13:52:20 -0500 Received: from d01relay04.pok.ibm.com (d01relay04.pok.ibm.com [9.56.227.236]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id CE6E46E8047; Mon, 17 Dec 2012 13:52:18 -0500 (EST) Received: from d03av04.boulder.ibm.com (d03av04.boulder.ibm.com [9.17.195.170]) by d01relay04.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id qBHIqIEf268894; Mon, 17 Dec 2012 13:52:19 -0500 Received: from d03av04.boulder.ibm.com (loopback [127.0.0.1]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id qBHIqB4N012370; Mon, 17 Dec 2012 11:52:11 -0700 Received: from kernel-pok.stglabs.ibm.com (kernel.stglabs.ibm.com [9.114.214.19]) by d03av04.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id qBHIqAu8012269; Mon, 17 Dec 2012 11:52:10 -0700 From: John Stultz To: linux-kernel@vger.kernel.org Cc: Kees Cook , Anton Vorontsov , Thomas Gleixner , John Stultz Subject: [PATCH] time: create __getnstimeofday for WARNless calls Date: Mon, 17 Dec 2012 13:51:54 -0500 Message-Id: <1355770314-9167-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.7.9.5 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12121718-9360-0000-0000-00000E1141D6 X-Gm-Message-State: ALoCoQl/XqplXqGspDhsUCqZkz6atpkjMWA/U8orF9C8hWvODlIbxTgwr8qVf4mUk7Vy8FyvUVyx From: Kees Cook Hey Thomas, Wanted to see if maybe there was still time for this for 3.8? thanks -john The pstore RAM backend can get called during resume, and must be defensive against a suspended time source. Expose getnstimeofday logic that returns an error instead of a WARN. This can be detected and the timestamp can be zeroed out. Reported-by: Doug Anderson Cc: Anton Vorontsov Cc: Thomas Gleixner Signed-off-by: Kees Cook Signed-off-by: John Stultz --- fs/pstore/ram.c | 10 +++++++--- include/linux/time.h | 1 + kernel/time/timekeeping.c | 29 ++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/fs/pstore/ram.c b/fs/pstore/ram.c index 1a4f6da..dacfe78 100644 --- a/fs/pstore/ram.c +++ b/fs/pstore/ram.c @@ -168,12 +168,16 @@ static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type, static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz) { char *hdr; - struct timeval timestamp; + struct timespec timestamp; size_t len; - do_gettimeofday(×tamp); + /* Report zeroed timestamp if called before timekeeping has resumed. */ + if (__getnstimeofday(×tamp)) { + timestamp.tv_sec = 0; + timestamp.tv_nsec = 0; + } hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n", - (long)timestamp.tv_sec, (long)timestamp.tv_usec); + (long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000)); WARN_ON_ONCE(!hdr); len = hdr ? strlen(hdr) : 0; persistent_ram_write(prz, hdr, len); diff --git a/include/linux/time.h b/include/linux/time.h index 4d358e9..0015aea 100644 --- a/include/linux/time.h +++ b/include/linux/time.h @@ -158,6 +158,7 @@ extern int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue); extern unsigned int alarm_setitimer(unsigned int seconds); extern int do_getitimer(int which, struct itimerval *value); +extern int __getnstimeofday(struct timespec *tv); extern void getnstimeofday(struct timespec *tv); extern void getrawmonotonic(struct timespec *ts); extern void getnstime_raw_and_real(struct timespec *ts_raw, diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 4c7de02..dfc7f87 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -214,19 +214,18 @@ static void timekeeping_forward_now(struct timekeeper *tk) } /** - * getnstimeofday - Returns the time of day in a timespec + * __getnstimeofday - Returns the time of day in a timespec. * @ts: pointer to the timespec to be set * - * Returns the time of day in a timespec. + * Updates the time of day in the timespec. + * Returns 0 on success, or -ve when suspended (timespec will be undefined). */ -void getnstimeofday(struct timespec *ts) +int __getnstimeofday(struct timespec *ts) { struct timekeeper *tk = &timekeeper; unsigned long seq; s64 nsecs = 0; - WARN_ON(timekeeping_suspended); - do { seq = read_seqbegin(&tk->lock); @@ -237,6 +236,26 @@ void getnstimeofday(struct timespec *ts) ts->tv_nsec = 0; timespec_add_ns(ts, nsecs); + + /* + * Do not bail out early, in case there were callers still using + * the value, even in the face of the WARN_ON. + */ + if (unlikely(timekeeping_suspended)) + return -EAGAIN; + return 0; +} +EXPORT_SYMBOL(__getnstimeofday); + +/** + * getnstimeofday - Returns the time of day in a timespec. + * @ts: pointer to the timespec to be set + * + * Returns the time of day in a timespec (WARN if suspended). + */ +void getnstimeofday(struct timespec *ts) +{ + WARN_ON(__getnstimeofday(ts)); } EXPORT_SYMBOL(getnstimeofday);