From patchwork Wed Aug 17 19:18:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 74121 Delivered-To: patches@linaro.org Received: by 10.140.29.52 with SMTP id a49csp2657727qga; Wed, 17 Aug 2016 12:18:58 -0700 (PDT) X-Received: by 10.98.89.210 with SMTP id k79mr3364912pfj.43.1471461538738; Wed, 17 Aug 2016 12:18:58 -0700 (PDT) Return-Path: Received: from mail-pf0-x234.google.com (mail-pf0-x234.google.com. [2607:f8b0:400e:c00::234]) by mx.google.com with ESMTPS id 4si518921pav.105.2016.08.17.12.18.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Aug 2016 12:18:58 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::234 as permitted sender) client-ip=2607:f8b0:400e:c00::234; 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::234 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-pf0-x234.google.com with SMTP id x72so40088220pfd.2 for ; Wed, 17 Aug 2016 12:18:58 -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:in-reply-to:references; bh=BRaLnal3XXKytWpTc5KziIfXq4y3HRUFOkU5WmllOG0=; b=IhpvkqY3n8bNGU+75kNYvZtPA7rmJsHTYtbDZ0cdB1cb97Tne2X8X47+p6VgQLWR8X vRYmV/HQYxk1ackocX4BP7Xn9GyPJNEmjGfOBSyWwND46RnfrjkbZuTc3jUPumRAyMxM DJNl+Aywo6DCqKb+vjC37Ek5jIAiu0aV/CYD4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=BRaLnal3XXKytWpTc5KziIfXq4y3HRUFOkU5WmllOG0=; b=ArcieBXJs0qpvDY4hx2CfIMdVZDrT0xNAIdQmr8uQiv2qzElmhVikJHOjBbLkV2LP/ lbQxLEgakQcYp+bFcoGWrEW2Kncf7RTkL70GFLOQ3KhF+6H7FL7s0ZuXqtzcz5Z18/z3 nG2an1adt57+mCGrN/qYgXf0/WoK20Fnwnef8tp4whM2t0Mh3bOWJ7TlLKg38ZnuuWys lq4zWZwzHiIayTa9fhyuNXtfekdoPSfsum7mFKVpcNOWXMNA+I1Hkyx8AK1E5GmaUU4d WgdIMmNR/WZnrtVtfgYX17DK9CGHIp4N6PmpwwUxDYwnDXcmk6WZ7Ib4YJBeif83rnRN HsMw== X-Gm-Message-State: AEkoouvGAaqz0Yu41IlcdKnlfn8mJJORBOZI7BmRiHmQNq0wGHCapiDLhoE2OVvb5bG3jkEtrUc= X-Received: by 10.98.70.8 with SMTP id t8mr8837363pfa.36.1471461538139; Wed, 17 Aug 2016 12:18:58 -0700 (PDT) Return-Path: Received: from localhost.localdomain (c-73-67-244-238.hsd1.or.comcast.net. [73.67.244.238]) by smtp.gmail.com with ESMTPSA id g5sm49072337pfg.0.2016.08.17.12.18.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 17 Aug 2016 12:18:57 -0700 (PDT) From: John Stultz To: Chen Yu Cc: John Stultz , Thomas Gleixner , "Rafael J. Wysocki" , Janek Kozicki , Xunlei Pang , Zhang Rui , linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org Subject: [PATCH] timekeeping: Cap array access in timekeeping_debug to protect against invalid sleep times Date: Wed, 17 Aug 2016 12:18:50 -0700 Message-Id: <1471461530-19575-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1469892465-15853-1-git-send-email-yu.c.chen@intel.com> References: <1469892465-15853-1-git-send-email-yu.c.chen@intel.com> It was reported that hibernation could fail on the 2nd attempt, where the system hangs at hibernate() -> syscore_resume() -> i8237A_resume() -> claim_dma_lock(), because the lock has already been taken. However there is actually no other process would like to grab this lock on that problematic platform. Further investigation showed that the problem is triggered by setting /sys/power/pm_trace to 1 before the 1st hibernation. Since once pm_trace is enabled, the rtc becomes unmeaningful after suspend, and meanwhile some BIOSes would like to adjust the 'invalid' tsc(e.g, smaller than 1970) to the release date of that motherboard during POST stage, thus after resumed, it may seem that the system had a significant long sleep time might due to meaningless tsc or RTC delta. Then in timekeeping_resume -> tk_debug_account_sleep_time, if the bit31 of the sleep time happened to be set to 1, the fls returns 32 and then we add 1 to sleep_time_bin[32], which caused a memory overwritten. As depicted by System.map: ffffffff81c9d080 b sleep_time_bin ffffffff81c9d100 B dma_spin_lock the dma_spin_lock.val is set to 1, which caused this problem. This patch adds a sanity check in tk_debug_account_sleep_time() to ensure we don't index past the sleep_time_bin array. Cc: Thomas Gleixner Cc: Rafael J. Wysocki Cc: Janek Kozicki Cc: Chen Yu Cc: Xunlei Pang Cc: Zhang Rui Cc: linux-kernel@vger.kernel.org Cc: linux-pm@vger.kernel.org Reported-by: Janek Kozicki Reported-by: Chen Yu [jstultz: Problem diagnosed and original patch by Chen Yu, I've solved the issue slightly differently, but borrowed his excelent explanation of of the issue here.] Signed-off-by: John Stultz --- kernel/time/timekeeping_debug.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) -- 1.9.1 diff --git a/kernel/time/timekeeping_debug.c b/kernel/time/timekeeping_debug.c index f6bd652..107310a6 100644 --- a/kernel/time/timekeeping_debug.c +++ b/kernel/time/timekeeping_debug.c @@ -23,7 +23,9 @@ #include "timekeeping_internal.h" -static unsigned int sleep_time_bin[32] = {0}; +#define NUM_BINS 32 + +static unsigned int sleep_time_bin[NUM_BINS] = {0}; static int tk_debug_show_sleep_time(struct seq_file *s, void *data) { @@ -69,6 +71,9 @@ late_initcall(tk_debug_sleep_time_init); void tk_debug_account_sleep_time(struct timespec64 *t) { - sleep_time_bin[fls(t->tv_sec)]++; + /* Cap bin index so we don't overflow the array */ + int bin = min(fls(t->tv_sec), NUM_BINS-1); + + sleep_time_bin[bin]++; }