From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933424AbcGJMYd (ORCPT ); Sun, 10 Jul 2016 08:24:33 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:32898 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933281AbcGJMY2 (ORCPT ); Sun, 10 Jul 2016 08:24:28 -0400 From: Nicolai Stange To: Thomas Gleixner Cc: Ingo Molnar , "H. Peter Anvin" , x86@kernel.org, John Stultz , Borislav Petkov , Paolo Bonzini , Viresh Kumar , Hidehiro Kawai , "Peter Zijlstra (Intel)" , "Christopher S. Hall" , Adrian Hunter , Suresh Siddha , linux-kernel@vger.kernel.org, Nicolai Stange Subject: [PATCH 1/4] arch, x86, tsc deadline clockevent dev: reduce frequency roundoff error Date: Sun, 10 Jul 2016 14:23:42 +0200 Message-Id: <20160710122345.13061-2-nicstange@gmail.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20160710122345.13061-1-nicstange@gmail.com> References: <20160710122345.13061-1-nicstange@gmail.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In setup_APIC_timer(), the registered clockevent device's frequency is calculated by first dividing tsc_khz by TSC_DIVISOR and multiplying it with 1000 afterwards. The multiplication with 1000 is done for converting from kHz to Hz and the division by TSC_DIVISOR is carried out in order to make sure that the final result fits into an u32. However, with the order given in this calculation, the roundoff error introduced by the division gets magnified by a factor of 1000 by the following multiplication. Increase the accuracy by reversing the order of the division and multiplication. In order not to overflow during this calculation, cast temporarily to u64. Signed-off-by: Nicolai Stange --- arch/x86/kernel/apic/apic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/apic/apic.c b/arch/x86/kernel/apic/apic.c index 89a5bce..dce654c 100644 --- a/arch/x86/kernel/apic/apic.c +++ b/arch/x86/kernel/apic/apic.c @@ -563,7 +563,8 @@ static void setup_APIC_timer(void) CLOCK_EVT_FEAT_DUMMY); levt->set_next_event = lapic_next_deadline; clockevents_config_and_register(levt, - (tsc_khz / TSC_DIVISOR) * 1000, + (u32)(((u64)tsc_khz * 1000) / + TSC_DIVISOR), 0xF, ~0UL); } else clockevents_register_device(levt); -- 2.9.0