From patchwork Sat Nov 19 04:53:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 83057 Delivered-To: patches@linaro.org Received: by 10.140.97.165 with SMTP id m34csp442110qge; Fri, 18 Nov 2016 20:53:41 -0800 (PST) X-Received: by 10.99.1.132 with SMTP id 126mr6848987pgb.129.1479531221064; Fri, 18 Nov 2016 20:53:41 -0800 (PST) Return-Path: Received: from mail-pf0-x22e.google.com (mail-pf0-x22e.google.com. [2607:f8b0:400e:c00::22e]) by mx.google.com with ESMTPS id j62si11361180pgc.184.2016.11.18.20.53.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Nov 2016 20:53:40 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22e as permitted sender) client-ip=2607:f8b0:400e:c00::22e; 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::22e as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-pf0-x22e.google.com with SMTP id 189so57358845pfz.3 for ; Fri, 18 Nov 2016 20:53:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=exNOK3dYS4lc5XSlqfdJso5n5Ji0JwuuyRGWVyLouvU=; b=FqRKQO9jqQRw6rgig7kgUSTzmH9tbQa1fxAaXMVKrl9sCcNZtmKwljXBDXFBWYzRyh ys71tNLsNQ44vK23PUSRDn8WbY9mYNk66dvIkW7VaSrq5y9ptODdUFbvL0E5srDW9zlo veWFZTJznDu4MVvECXefB0WUOODx/msY204tk= 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; bh=exNOK3dYS4lc5XSlqfdJso5n5Ji0JwuuyRGWVyLouvU=; b=Hp4kZve1Rc/OZilBXj5c7tZBiM9ad2lT+MmKH3CCjb9k736AauOvZEnicWKEAqALn9 0tuf355U01nDM9xj0nYs6kmfLGIVyVBuksoiv6yLQjwkpaB02tpBck9eVSS+huVoUyqZ 6V0akN+nq3NB4KTa0fjSC83e/kruVYaNY/h9TmdUThD8Mndg8pW56C5av2XhYnXDvYq/ INAA8qy2XmDculIujdrnlnIWjjMKVxPhTuosBnwJvldlYXRIO27Tq2YImK9Y72o+xR00 xFWkqI3eJ6+Wd/cQvk6ImOGihf53yg37Za708lqqHEC7OoxR/8mX6cTTYnDdA2uZSIuO BbVw== X-Gm-Message-State: AKaTC038VN48awQjhf9xuxD4QEqciC921FR1mAmv04jK5sXzK+NcsOqgGiQexgCrbRmF4MyrjLk= X-Received: by 10.98.79.25 with SMTP id d25mr4147788pfb.42.1479531220643; Fri, 18 Nov 2016 20:53:40 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:1002:83f0:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id y29sm21394452pfd.63.2016.11.18.20.53.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 18 Nov 2016 20:53:40 -0800 (PST) From: John Stultz To: lkml Cc: Liav Rehana , Chris Metcalf , Thomas Gleixner , Richard Cochran , Ingo Molnar , Prarit Bhargava , Laurent Vivier , David Gibson , "Christopher S . Hall" , stable@vger.kernel.org (4.6+), John Stultz Subject: [PATCH] timekeeping: Change type of nsec variable to unsigned in its calculation. Date: Fri, 18 Nov 2016 20:53:36 -0800 Message-Id: <1479531216-25361-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 From: Liav Rehana During the calculation of the nsec variable in the inline function timekeeping_delta_to_ns, it may undergo a sign extension if its msb is set just before the shift. The sign extension may, in some cases, gain it a value near the maximum value of the 64-bit range. This is bad when it is later used in a division function, such as __iter_div_u64_rem, where the amount of loops it will go through to calculate the division will be too large. One can encounter such a problem, for example, when trying to connect through ftp from an outside host to the operation system. When the OS is too overloaded, delta will get a high enough value for the msb of the sum delta * tkr->mult + tkr->xtime_nsec to be set, and so after the shift the nsec variable will gain a value similar to 0xffffffffff000000. Using a variable with such a value in the inline function __iter_div_u64_rem will take too long, making the ftp connection attempt seem to get stuck. The following commit fixes that chance of sign extension, while maintaining the type of the nsec variable as signed for other functions that use this variable, for possible legit negative time intervals. Cc: Chris Metcalf Cc: Thomas Gleixner Cc: Richard Cochran Cc: Ingo Molnar Cc: Prarit Bhargava Cc: Laurent Vivier Cc: David Gibson Cc: "Christopher S . Hall" Cc: stable@vger.kernel.org (4.6+) Fixes: 6bd58f09e1d8 ("time: Add cycles to nanoseconds translation") Also-Reported-by: Chris Metcalf Signed-off-by: Liav Rehana Signed-off-by: John Stultz --- Thomas/Ingo: This is for tip:timers/urgent. kernel/time/timekeeping.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) -- 2.7.4 diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 37dec7e..46e312e 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -299,10 +299,10 @@ u32 (*arch_gettimeoffset)(void) = default_arch_gettimeoffset; static inline u32 arch_gettimeoffset(void) { return 0; } #endif -static inline s64 timekeeping_delta_to_ns(struct tk_read_base *tkr, +static inline u64 timekeeping_delta_to_ns(struct tk_read_base *tkr, cycle_t delta) { - s64 nsec; + u64 nsec; nsec = delta * tkr->mult + tkr->xtime_nsec; nsec >>= tkr->shift;