From patchwork Wed Nov 15 12:51:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Gonzalez X-Patchwork-Id: 10059341 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 2CC956019D for ; Wed, 15 Nov 2017 12:52:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1D4EE29F68 for ; Wed, 15 Nov 2017 12:52:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 108D629F72; Wed, 15 Nov 2017 12:52:44 +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=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7514929F68 for ; Wed, 15 Nov 2017 12:52:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date: Message-ID:References:To:From:Subject:Reply-To:Content-ID:Content-Description :Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=epP296ZPl2R+Gc7FAX6NBFJzW+wet3Y96BBHHO6qkwU=; b=B2JXJ5LKG9cFqF eK39/kq9WU5JCn6yh8xwEWPnET/5LehD6Y+fPyWjy10NqaK1lrUP6qq+zRFrxMkWrrlnF3Vax9EuO 4OEDFlrAl5Ec2eHMhBrjNK0dbFU/wq288M3zI+TUYHrVkhR1uoovTL97giJjHP3PgQVPE/MZ/2GH8 FnQMDfBcY74kU9xLQLiWWPC/vwc0xA2kN098ollr1xFDgOiK3G+sLG8XKRSCATTiositB2wEKQAFR D8dbOtfrUCiai0D3O+1ND7C/Oamf+A3x3zhxyUcMH6JEshxy6zUbBbwbUqK8Dhs2T19IgnLDUO9d3 jXplptekSO7maPe8wycg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eExB6-0006Ny-68; Wed, 15 Nov 2017 12:52:28 +0000 Received: from us-smtp-delivery-107.mimecast.com ([63.128.21.107]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eExB2-0006MC-U9 for linux-arm-kernel@lists.infradead.org; Wed, 15 Nov 2017 12:52:26 +0000 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 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: X-Originating-IP: [172.27.0.114] X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171115_045225_050291_24245E1B X-CRM114-Status: GOOD ( 15.19 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Alan Cox , Mason , Thibaud Cornic , Russell King , Jonathan Austin , Arnd Bergmann , Nicolas Pitre , Peter Zijlstra , Stephen Boyd , Michael Turquette , Kevin Hilman , Will Deacon , LKML , Steven Rostedt , Douglas Anderson , John Stultz , Boris Brezillon , Thomas Gleixner , Ingo Molnar , Linux ARM Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: Douglas Anderson --- 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 */