From patchwork Tue Aug 23 23:08:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 9296601 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8485B607D0 for ; Tue, 23 Aug 2016 23:08:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5421628CF2 for ; Tue, 23 Aug 2016 23:08:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 48BC528D18; Tue, 23 Aug 2016 23:08:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1298B28CF5 for ; Tue, 23 Aug 2016 23:08:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754457AbcHWXIc (ORCPT ); Tue, 23 Aug 2016 19:08:32 -0400 Received: from mail-pf0-f173.google.com ([209.85.192.173]:36789 "EHLO mail-pf0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932307AbcHWXIa (ORCPT ); Tue, 23 Aug 2016 19:08:30 -0400 Received: by mail-pf0-f173.google.com with SMTP id h186so47874298pfg.3 for ; Tue, 23 Aug 2016 16:08:30 -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=QVnH2KT1YQ4G0XyqTiMkdEXXSzAcPj2GJ+gVA/GoiTA=; b=FcH1p7pL4IUjteCtd7y02nn/ZB3bqaT4tchFdvxlYqYRkoOIRc+wRClI+L/x2+vE8d uHw5gRKnnGIGKtgoo5gJNkzTCiCgpTAwREpU0Z+z7yzbpgzK+Z9uwD1TOAH07uxvgOwQ swU6n6Jik5FDnEn0lJ1ueJaGmp4lO1XjieuzA= 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=QVnH2KT1YQ4G0XyqTiMkdEXXSzAcPj2GJ+gVA/GoiTA=; b=f6YmSEbnrTNzygZ599nC47Xi52LiuXjvqqHGYdyII5iaqJcgIst6bD/64mCl06RlXz gGq2yL5twkojjfiNBrFmX4/2JiSVqEDnSA43pc3RcNbxFNHAB04dqF6kdbJ7361dVf4F DJycmTUlWhquX+chs27epkPzNBHe/tmU2QzNsXc+sLUtdBNp7OTrrLgZiw5QblvgvjZ0 sQ766O3sC2vdmIVoYrCozZ6ndfjShv0WZDXGlMYrUZmmRbxOHPRBKtnjCVZEh8yE1461 dNCV3lr+IKJ71L5YsnI0jYorJNOgeahU2n4gzE0lKECMBD0gGfaJ0GSWo1Pjs17BVO60 fFUw== X-Gm-Message-State: AEkoouvJl54P02kONk/ISiLvJ3CqX38q9EBmVahIIHohK2NfrV8iQ79Bfce1Y1J9iWJwkXov X-Received: by 10.98.74.91 with SMTP id x88mr57917460pfa.79.1471993710111; Tue, 23 Aug 2016 16:08:30 -0700 (PDT) Received: from localhost.localdomain (c-73-67-244-238.hsd1.or.comcast.net. [73.67.244.238]) by smtp.gmail.com with ESMTPSA id i137sm8200194pfe.64.2016.08.23.16.08.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 23 Aug 2016 16:08:29 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Thomas Gleixner , Peter Zijlstra , Ingo Molnar , "Rafael J. Wysocki" , Janek Kozicki , Chen Yu , Xunlei Pang , Zhang Rui , linux-pm@vger.kernel.org, stable Subject: [PATCH 2/2] timekeeping: Cap array access in timekeeping_debug to protect against invalid sleep times Date: Tue, 23 Aug 2016 16:08:22 -0700 Message-Id: <1471993702-29148-3-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1471993702-29148-1-git-send-email-john.stultz@linaro.org> References: <1471993702-29148-1-git-send-email-john.stultz@linaro.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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: 0xffffffff81c9d080 b sleep_time_bin 0xffffffff81c9d100 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: Peter Zijlstra Cc: Ingo Molnar Cc: Rafael J. Wysocki Cc: Janek Kozicki Cc: Chen Yu Cc: Xunlei Pang Cc: Zhang Rui Cc: linux-pm@vger.kernel.org Cc: stable 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(-) 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]++; }