Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752640AbaAPPmh (ORCPT ); Thu, 16 Jan 2014 10:42:37 -0500 Received: from mail-wg0-f49.google.com ([74.125.82.49]:53143 "EHLO mail-wg0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752557AbaAPPmH (ORCPT ); Thu, 16 Jan 2014 10:42:07 -0500 From: Frederic Weisbecker To: Ingo Molnar Cc: LKML , Kevin Hilman , Thomas Gleixner , Peter Zijlstra , Alex Shi , Steven Rostedt , "Paul E. McKenney" , John Stultz , Frederic Weisbecker Subject: [PATCH 4/4] sched/nohz: Fix overflow error in scheduler_tick_max_deferment() Date: Thu, 16 Jan 2014 16:41:52 +0100 Message-Id: <1389886912-4783-5-git-send-email-fweisbec@gmail.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1389886912-4783-1-git-send-email-fweisbec@gmail.com> References: <1389886912-4783-1-git-send-email-fweisbec@gmail.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kevin Hilman While calculating the scheduler tick max deferment, the delta is converted from microseconds to nanoseconds through a multiplication against NSEC_PER_USEC. But this microseconds operand is an unsigned int, thus the result may likely overflow. The result is cast to u64 but only once the operation is completed, which is too late to avoid overflown result. This is currently not a problem because the scheduler tick max deferment is 1 second. But this may become an issue as we plan to make this value tunable. So lets fix this by casting the usecs value to u64 before multiplying by NSECS_PER_USEC. Also to prevent from this kind of mistake to happen again, move this ad-hoc jiffies -> nsecs conversion to a new helper. Signed-off-by: Kevin Hilman Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Alex Shi Cc: Steven Rostedt Cc: Paul E. McKenney Cc: John Stultz Cc: Kevin Hilman Link: http://lkml.kernel.org/r/1387315388-31676-2-git-send-email-khilman@linaro.org [move ad-hoc conversion to jiffies_to_nsecs helper] Signed-off-by: Frederic Weisbecker --- include/linux/jiffies.h | 6 ++++++ kernel/sched/core.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h index d235e88..1f44466 100644 --- a/include/linux/jiffies.h +++ b/include/linux/jiffies.h @@ -294,6 +294,12 @@ extern unsigned long preset_lpj; */ extern unsigned int jiffies_to_msecs(const unsigned long j); extern unsigned int jiffies_to_usecs(const unsigned long j); + +static inline u64 jiffies_to_nsecs(const unsigned long j) +{ + return (u64)jiffies_to_usecs(j) * NSEC_PER_USEC; +} + extern unsigned long msecs_to_jiffies(const unsigned int m); extern unsigned long usecs_to_jiffies(const unsigned int u); extern unsigned long timespec_to_jiffies(const struct timespec *value); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a88f4a4..61e601f 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2325,7 +2325,7 @@ u64 scheduler_tick_max_deferment(void) if (time_before_eq(next, now)) return 0; - return jiffies_to_usecs(next - now) * NSEC_PER_USEC; + return jiffies_to_nsecs(next - now); } #endif -- 1.8.3.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/