From patchwork Thu Aug 25 18:40:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Davis X-Patchwork-Id: 74702 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp983308qga; Thu, 25 Aug 2016 11:41:15 -0700 (PDT) X-Received: by 10.28.45.65 with SMTP id t62mr29833736wmt.14.1472150475059; Thu, 25 Aug 2016 11:41:15 -0700 (PDT) Return-Path: Received: from theia.denx.de (theia.denx.de. [85.214.87.163]) by mx.google.com with ESMTP id d14si15099999wjs.119.2016.08.25.11.41.14; Thu, 25 Aug 2016 11:41:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 85.214.87.163 as permitted sender) client-ip=85.214.87.163; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 85.214.87.163 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 3F11C4B77D; Thu, 25 Aug 2016 20:41:13 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5P2BxeUSIqtQ; Thu, 25 Aug 2016 20:41:12 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 396D84B68A; Thu, 25 Aug 2016 20:41:12 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id E86B04B68A for ; Thu, 25 Aug 2016 20:41:08 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7jeUtS-e1fLm for ; Thu, 25 Aug 2016 20:41:08 +0200 (CEST) Received: from comal.ext.ti.com (comal.ext.ti.com [198.47.26.152]) by theia.denx.de (Postfix) with ESMTPS id 099C74B660 for ; Thu, 25 Aug 2016 20:41:07 +0200 (CEST) Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by comal.ext.ti.com (8.13.7/8.13.7) with ESMTP id u7PIf0CK017328; Thu, 25 Aug 2016 13:41:00 -0500 Received: from DFLE73.ent.ti.com (dfle73.ent.ti.com [128.247.5.110]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id u7PIexsj001987; Thu, 25 Aug 2016 13:40:59 -0500 Received: from dflp32.itg.ti.com (10.64.6.15) by DFLE73.ent.ti.com (128.247.5.110) with Microsoft SMTP Server id 14.3.294.0; Thu, 25 Aug 2016 13:40:58 -0500 Received: from legion.dal.design.ti.com (legion.dal.design.ti.com [128.247.22.53]) by dflp32.itg.ti.com (8.14.3/8.13.8) with ESMTP id u7PIew4G032130; Thu, 25 Aug 2016 13:40:58 -0500 Received: from localhost (uda0226330.am.dhcp.ti.com [128.247.83.52]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id u7PIew313685; Thu, 25 Aug 2016 13:40:58 -0500 (CDT) From: "Andrew F. Davis" To: Simon Glass , Lokesh Vutla Date: Thu, 25 Aug 2016 13:40:58 -0500 Message-ID: <20160825184058.2071-1-afd@ti.com> X-Mailer: git-send-email 2.9.3 MIME-Version: 1.0 Cc: u-boot@lists.denx.de Subject: [U-Boot] [PATCH] timer: Fix possible underflow in udelay X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" When the inputed usec is too large we process it in chunks of CONFIG_WD_PERIOD size. Subtracting this from usec until usec is zero. If usec is not an integer multiple of CONFIG_WD_PERIOD it will underflow and the condition will not become false when it should. Fix this logic. Signed-off-by: Andrew F. Davis --- lib/time.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) -- 2.9.3 _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot diff --git a/lib/time.c b/lib/time.c index f37150f..4ec3cb9 100644 --- a/lib/time.c +++ b/lib/time.c @@ -145,14 +145,14 @@ void __weak __udelay(unsigned long usec) void udelay(unsigned long usec) { - ulong kv; + ulong kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; + ulong elapsed = 0; do { WATCHDOG_RESET(); - kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; - __udelay (kv); - usec -= kv; - } while(usec); + __udelay(kv); + elapsed += kv; + } while (elapsed < usec); } void mdelay(unsigned long msec)