Received: by 10.223.164.202 with SMTP id h10csp585642wrb; Wed, 15 Nov 2017 04:55:25 -0800 (PST) X-Google-Smtp-Source: AGs4zMbHaMrpx6fPM5pf0hasbV1P+25BFSCaFl+uJqFisP0L7JLovPEtVfD4NPvtKOQEAZ0UaSkn X-Received: by 10.99.146.76 with SMTP id s12mr15959852pgn.354.1510750525760; Wed, 15 Nov 2017 04:55:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510750525; cv=none; d=google.com; s=arc-20160816; b=M7hIC8qk4rgzSuhP1AYefVJmPQ46XpDYiV9V11zwkC1zG1mA/dls3f3WWqmuMY/rZx nG+lsJPX9Tf8+wVQ45GbU4OH8dKb7NXaNBBkzPsUPpX8s+KOFdpN7Ygc9o1d+hxMV4Al vjSlEFMoMSKdN9GVWlOw9x1aRBGdMZKIlxbDf9zyviKyr3W9ClLbXkqcdNV4vHbfuPeO +idD5D3JThghAmBjU9Lzv2fUu+yrCusYx7QvlECa0jjgJ7Z/mFvUwhTrE3llGOIpLs/I KbE0RAQIg/cc8C+NZP+k/kEB9ZltEPA947k91k3iYkj+86DU/H1onKe01qeJG5p8OToV SQvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:references:cc:to:from :subject:arc-authentication-results; bh=/g4bGVgQNfNpNmce8jJs4sP2Kp815XMcj4cl1XxWLhk=; b=GRpw6Ho54t95/yozXCDtZsJjVtvv2VZxHUJix9qo/iF/UFBWdBuUFDWSbTDIHz7t37 Q88Dc2dQbVlR7Rqe1uhfQf3A62kdmItNfupi37NFgIkvHc+QtaqDjuLU76otS8FgFxeU 0EqnZz50EMFZVso6sWPAOzQP++y5LlHIJwM2OAettamUTePHCaTZd2jOHwV+3gmw6EKg 8FvJ94bjXCwLCyTWLq9kbmYcNGv93ZGtA9E5l0dANuNG82Ep48wYvBxY01GiAy7kndoK 2CcET2769mzOJSwSN0LpN7RfHm5HLqwudFogWLoJp9RiEVCNi8/HqSlUs17EnovXMV9h GlxQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f2si188202pgr.306.2017.11.15.04.55.12; Wed, 15 Nov 2017 04:55:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757598AbdKOMxM (ORCPT + 90 others); Wed, 15 Nov 2017 07:53:12 -0500 Received: from us-smtp-delivery-107.mimecast.com ([216.205.24.107]:47505 "EHLO us-smtp-delivery-107.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754719AbdKOMxG (ORCPT ); Wed, 15 Nov 2017 07:53:06 -0500 Received: from CPH-EX1.SDESIGNS.COM (195-215-56-170-static.dk.customer.tdc.net [195.215.56.170]) (Using TLS) by us-smtp-1.mimecast.com with ESMTP id us-mta-212-HG4ow0_bMOyqzWccDrfvXw-1; Wed, 15 Nov 2017 07:51:59 -0500 X-MC-Unique: HG4ow0_bMOyqzWccDrfvXw-1 Received: from [172.27.0.114] (172.27.0.114) by CPH-EX1.sdesigns.com (192.168.10.36) with Microsoft SMTP Server (TLS) id 14.3.294.0; Wed, 15 Nov 2017 13:51:55 +0100 Subject: Re: [RFC] Improving udelay/ndelay on platforms where that is possible From: Marc Gonzalez To: Linus Torvalds CC: Alan Cox , LKML , Linux ARM , Steven Rostedt , Ingo Molnar , Thomas Gleixner , Peter Zijlstra , John Stultz , Douglas Anderson , Nicolas Pitre , Mark Rutland , Will Deacon , Jonathan Austin , Arnd Bergmann , Kevin Hilman , Russell King , Michael Turquette , Stephen Boyd , Boris Brezillon , Thibaud Cornic , Mason References: <20171101175325.2557ce85@alans-desktop> <4b707ce0-6067-ab36-e167-1acf348d26bf@free.fr> Message-ID: <11393e07-b042-180c-3bcd-484bf51eada6@sigmadesigns.com> Date: Wed, 15 Nov 2017 13:51:54 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0 SeaMonkey/2.49.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [172.27.0.114] Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 01/11/2017 20:38, Marc Gonzalez wrote: > OK, I'll just send my patch, and then crawl back under my rock. Linus, As promised, the patch is provided below. And as promised, I will no longer bring this up on LKML. FWIW, I have checked that the computed value matches the expected value for all HZ and delay_us, and for a few clock frequencies, using the following program: $ cat delays.c #include #define MEGA 1000000u typedef unsigned int uint; typedef unsigned long long u64; #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) static const uint HZ_tab[] = { 100, 250, 300, 1000 }; static void check_cycle_count(uint freq, uint HZ, uint delay_us) { uint UDELAY_MULT = (2147 * HZ) + (483648 * HZ / MEGA); uint lpj = DIV_ROUND_UP(freq, HZ); uint computed = ((u64)lpj * delay_us * UDELAY_MULT >> 31) + 1; uint expected = DIV_ROUND_UP((u64)delay_us * freq, MEGA); if (computed != expected) printf("freq=%u HZ=%u delay_us=%u comp=%u exp=%u\n", freq, HZ, delay_us, computed, expected); } int main(void) { uint idx, delay_us, freq; for (freq = 3*MEGA; freq <= 100*MEGA; freq += 3*MEGA) for (idx = 0; idx < sizeof HZ_tab / sizeof *HZ_tab; ++idx) for (delay_us = 1; delay_us <= 2000; ++delay_us) check_cycle_count(freq, HZ_tab[idx], delay_us); return 0; } -- >8 -- Subject: [PATCH] ARM: Tweak clock-based udelay implementation In 9f8197980d87a ("delay: Add explanation of udelay() inaccuracy") Russell pointed out that loop-based delays may return early. On the arm platform, delays may be either loop-based or clock-based. This patch tweaks the clock-based implementation so that udelay(N) is guaranteed to spin at least N microseconds. Signed-off-by: Marc Gonzalez --- arch/arm/lib/delay.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/arm/lib/delay.c b/arch/arm/lib/delay.c index 2cef11884857..0a25712077ec 100644 --- a/arch/arm/lib/delay.c +++ b/arch/arm/lib/delay.c @@ -58,15 +58,15 @@ static void __timer_delay(unsigned long cycles) { cycles_t start = get_cycles(); - while ((get_cycles() - start) < cycles) + while ((get_cycles() - start) <= cycles) cpu_relax(); } static void __timer_const_udelay(unsigned long xloops) { - unsigned long long loops = xloops; - loops *= arm_delay_ops.ticks_per_jiffy; - __timer_delay(loops >> UDELAY_SHIFT); + u64 tmp = (u64)xloops * arm_delay_ops.ticks_per_jiffy; + unsigned long cycles = tmp >> UDELAY_SHIFT; + __timer_delay(cycles + 1); /* Round up in 1 instruction */ } static void __timer_udelay(unsigned long usecs) @@ -92,7 +92,7 @@ void __init register_current_timer_delay(const struct delay_timer *timer) if (!delay_calibrated && (!delay_res || (res < delay_res))) { pr_info("Switching to timer-based delay loop, resolution %lluns\n", res); delay_timer = timer; - lpj_fine = timer->freq / HZ; + lpj_fine = DIV_ROUND_UP(timer->freq, HZ); delay_res = res; /* cpufreq may scale loops_per_jiffy, so keep a private copy */ -- 2.15.0 From 1582971425976549486@xxx Thu Nov 02 16:13:12 +0000 2017 X-GM-THRID: 1582790467810046578 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread