Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp5236658ybl; Tue, 27 Aug 2019 01:18:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqwzmUOE+dT3j9h5JKC+vgGCgpwcwAo1sS0Vke+6XmCWScS9XbnCccGXZykLftEmONIv3P4E X-Received: by 2002:aa7:8c03:: with SMTP id c3mr24502256pfd.139.1566893911716; Tue, 27 Aug 2019 01:18:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566893911; cv=none; d=google.com; s=arc-20160816; b=SDX1GdE/d4F54OeabROMBMdpDTBFvgTDFCr1EL5uA5s1KkRWL8+Cq7dCxm2EgyOwSI WTMULFQLSbg5A/NSLE7VNMQ9oZ1GTWy9gAvOLMqiH4PVBnRZ4Y1lLfoQnOYI3JLeihdS Rd1Iu9XeA8qFcOQ1gNyH36dEntAz3iKALqW1VJnzl+yUdaIjZA+LzjJwCP9rKut4oF5L usxoVpR98hwC9ra9R1cYIsn4t8mFGvtejotQsWar++MjuCdm4K9Br2NkOthHY3KtH7/R DA2VctqPnnY5tsBs+0kJyTRAL+XiSl6TP1ZgpLqhHUV7gEQrHzEdYuTp2VJii93yTbRl rg6g== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=y8JBzR9dg+uYJXFMK8L20dII9Zmfb1JQ+m1hK23ibKM=; b=gFGsqyMslSBoOh7k40g8V8SkjfN/ZRH0T8vEVftU4Wx1KSpPDCf9BiAL0FPC3PKueV lvO5YqMw4qk8s7KxnxxrFQVIUGR1xQePqEENT9EmP8mgkdN6oxhirPYhIhPS/TTzZriw UTqFlfMk9ehnYJSWYkKOna1NQNvGCCwhc0JeIvP63ECBWh8c30vIyMQmdfPMD6cl2vib 5MFWWyZxuYHlvsBK8mQnATZgcq6b/Myg41e+BcK1skNtieOZCF3gwLr8zLy/ekRbC9Wy 4/Bh3mSG/nhJb5u1qVER8YEZGgYwcRW9a5PeARfBiMXCUoLVBKrSA7GtLIHLTlNPhigq P/Og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="CTw/jpw0"; 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 y186si11166477pgd.440.2019.08.27.01.18.15; Tue, 27 Aug 2019 01:18:31 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b="CTw/jpw0"; 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 S1729244AbfH0Hxh (ORCPT + 99 others); Tue, 27 Aug 2019 03:53:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:45032 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729804AbfH0Hxc (ORCPT ); Tue, 27 Aug 2019 03:53:32 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C093A20828; Tue, 27 Aug 2019 07:53:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566892411; bh=GvdTTFn8xWYYNjP/s73R7HmCpkFkg2yW7FrKc12F+4w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CTw/jpw0lVdHPjti9FsvfuynE3CPKcpSHuBD1l9qJtUTiExRKVdHWrJ2OOzWKlWJD avL0egjKyPjQBAROrR1yvOeN2QM8LJVLpXfXMzc71vnRmtYOs6bDPK/L0MIWqiq4vs vdjc2XZORVru+BM81LQJc8d6RkRzxAbBskPN5gXw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Drake , Jiri Slaby , Thomas Gleixner Subject: [PATCH 4.14 45/62] x86/apic: Handle missing global clockevent gracefully Date: Tue, 27 Aug 2019 09:50:50 +0200 Message-Id: <20190827072703.162640443@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190827072659.803647352@linuxfoundation.org> References: <20190827072659.803647352@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Gleixner commit f897e60a12f0b9146357780d317879bce2a877dc upstream. Some newer machines do not advertise legacy timers. The kernel can handle that situation if the TSC and the CPU frequency are enumerated by CPUID or MSRs and the CPU supports TSC deadline timer. If the CPU does not support TSC deadline timer the local APIC timer frequency has to be known as well. Some Ryzens machines do not advertize legacy timers, but there is no reliable way to determine the bus frequency which feeds the local APIC timer when the machine allows overclocking of that frequency. As there is no legacy timer the local APIC timer calibration crashes due to a NULL pointer dereference when accessing the not installed global clock event device. Switch the calibration loop to a non interrupt based one, which polls either TSC (if frequency is known) or jiffies. The latter requires a global clockevent. As the machines which do not have a global clockevent installed have a known TSC frequency this is a non issue. For older machines where TSC frequency is not known, there is no known case where the legacy timers do not exist as that would have been reported long ago. Reported-by: Daniel Drake Reported-by: Jiri Slaby Signed-off-by: Thomas Gleixner Tested-by: Daniel Drake Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/alpine.DEB.2.21.1908091443030.21433@nanos.tec.linutronix.de Link: http://bugzilla.opensuse.org/show_bug.cgi?id=1142926#c12 Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/apic/apic.c | 68 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 15 deletions(-) --- a/arch/x86/kernel/apic/apic.c +++ b/arch/x86/kernel/apic/apic.c @@ -723,7 +723,7 @@ static __initdata unsigned long lapic_ca static __initdata unsigned long lapic_cal_j1, lapic_cal_j2; /* - * Temporary interrupt handler. + * Temporary interrupt handler and polled calibration function. */ static void __init lapic_cal_handler(struct clock_event_device *dev) { @@ -807,7 +807,8 @@ calibrate_by_pmtimer(long deltapm, long static int __init calibrate_APIC_clock(void) { struct clock_event_device *levt = this_cpu_ptr(&lapic_events); - void (*real_handler)(struct clock_event_device *dev); + u64 tsc_perj = 0, tsc_start = 0; + unsigned long jif_start; unsigned long deltaj; long delta, deltatsc; int pm_referenced = 0; @@ -838,29 +839,65 @@ static int __init calibrate_APIC_clock(v apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n" "calibrating APIC timer ...\n"); + /* + * There are platforms w/o global clockevent devices. Instead of + * making the calibration conditional on that, use a polling based + * approach everywhere. + */ local_irq_disable(); - /* Replace the global interrupt handler */ - real_handler = global_clock_event->event_handler; - global_clock_event->event_handler = lapic_cal_handler; - /* * Setup the APIC counter to maximum. There is no way the lapic * can underflow in the 100ms detection time frame */ __setup_APIC_LVTT(0xffffffff, 0, 0); - /* Let the interrupts run */ + /* + * Methods to terminate the calibration loop: + * 1) Global clockevent if available (jiffies) + * 2) TSC if available and frequency is known + */ + jif_start = READ_ONCE(jiffies); + + if (tsc_khz) { + tsc_start = rdtsc(); + tsc_perj = div_u64((u64)tsc_khz * 1000, HZ); + } + + /* + * Enable interrupts so the tick can fire, if a global + * clockevent device is available + */ local_irq_enable(); - while (lapic_cal_loops <= LAPIC_CAL_LOOPS) - cpu_relax(); + while (lapic_cal_loops <= LAPIC_CAL_LOOPS) { + /* Wait for a tick to elapse */ + while (1) { + if (tsc_khz) { + u64 tsc_now = rdtsc(); + if ((tsc_now - tsc_start) >= tsc_perj) { + tsc_start += tsc_perj; + break; + } + } else { + unsigned long jif_now = READ_ONCE(jiffies); + + if (time_after(jif_now, jif_start)) { + jif_start = jif_now; + break; + } + } + cpu_relax(); + } + + /* Invoke the calibration routine */ + local_irq_disable(); + lapic_cal_handler(NULL); + local_irq_enable(); + } local_irq_disable(); - /* Restore the real event handler */ - global_clock_event->event_handler = real_handler; - /* Build delta t1-t2 as apic timer counts down */ delta = lapic_cal_t1 - lapic_cal_t2; apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta); @@ -912,10 +949,11 @@ static int __init calibrate_APIC_clock(v levt->features &= ~CLOCK_EVT_FEAT_DUMMY; /* - * PM timer calibration failed or not turned on - * so lets try APIC timer based calibration + * PM timer calibration failed or not turned on so lets try APIC + * timer based calibration, if a global clockevent device is + * available. */ - if (!pm_referenced) { + if (!pm_referenced && global_clock_event) { apic_printk(APIC_VERBOSE, "... verify APIC timer\n"); /*