Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3153813pxy; Sun, 25 Apr 2021 15:48:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz6TKM7fJ4PBeo12oOSqIcplByf1Cyf8vZalPtxlJV1mUtASARIz+6JNKMrTUiB8BKHsSAK X-Received: by 2002:a17:906:a1c5:: with SMTP id bx5mr16196004ejb.166.1619390907710; Sun, 25 Apr 2021 15:48:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619390907; cv=none; d=google.com; s=arc-20160816; b=KkpqVNcjIwq7w0qy/OGWEGr29paywTO4oJJMeIuPTJsjsMHoXRGC5BtXwjc12gAMgh 5DWv/iUJ5OEy/IRqmy1mZ/1VPVljyFK53QtI2rYv5yq27Tq48jGfw+CbyA4/bBcaRlzn CGcRhS19P+aDs1dhe2PD93YDhdxeZhEbgwa98q+n9A+zLSCIIMdbuXuFmSBQ7LOwbRd1 EA8m9z/Nwc4Oyr5c05M8xTCd2/W/gYbvTjzI1jqwhj1c1l4FLZdwTrboqPVmiiR0+Blk mZC+fo1sX037/x2PqzffJuQ7nKEFtJXRPUKRUy8qVUmpBWsmaXknuaRmE6lPKjIGXgfE 0jUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0aV1UsBiS9dlM9jh/ikdkP4nnGd+RTCILiBu9eVlQxE=; b=LN2az2+FxMsTo6aRHZDNlUhfYeQynFMYfoetiazoKK5AGdgvUwhtw+NEGX4dmPvOUu nSwNG4vHs9lMc/RiRIptl2gmJYkbeBCUiqnNdxn14UHkfSQJYNHR1xY6Kg++asiu4z65 POo2yowEr4hqJqLOpbIuIbgAVHqrthV97c3iEIHn5WOHyO0GSM98nUO5JRFRMQmVAyJk HXk1p6ZNOfZ2Mms85wobbkqnlSJfn4XDqsmOkFW2/5f2ZEdhCkha9Ez2CS4+pMJZGx4p L+lEKhDDAmqwiXak4UENsqrvT9DG0kqVVM2OJlEpuJB4BDXqkCV0Paq4G+os1gy2mel7 r/lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Mm+LaYSQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bo20si11994710edb.530.2021.04.25.15.48.04; Sun, 25 Apr 2021 15:48:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Mm+LaYSQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231480AbhDYWry (ORCPT + 99 others); Sun, 25 Apr 2021 18:47:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:50740 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231331AbhDYWrv (ORCPT ); Sun, 25 Apr 2021 18:47:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 188866115C; Sun, 25 Apr 2021 22:47:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1619390831; bh=z5xwv/1zUYSWulLKV12dIGhC1CSCvNDXFx0BmZkCIPA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mm+LaYSQsnWeTAEuvzcizdS61hR+BDmxp5+F+Qu2EVOaU4dSKIOYECD4gXumiIvyC 96IaW6tVt+1/Ogsdlk2DobomoTHjlPefxIpeiQCWKEByJVPN8VbgiVIWSsw9UkxQBu LBxojEZA2fvyTe2His2N3AJUWDa8sDOAgDTruXuHaX9dStAof1fV2qAyUx1dXpAOs5 uKeksocm3noMJ/VFFzIYF1uALiD00XpFx2HKyAGU5dXErVHWpkREpdduNagKLoLGXE KzN95XDq/x1E6gFWLX3ZZ6rBY9BC8XKrV64wiyRTDccv5hphYm7Jrsg+Icjr9Xfz8/ SjVUAereBHTwQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id D79F35C012B; Sun, 25 Apr 2021 15:47:10 -0700 (PDT) From: "Paul E. McKenney" To: tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, john.stultz@linaro.org, sboyd@kernel.org, corbet@lwn.net, Mark.Rutland@arm.com, maz@kernel.org, kernel-team@fb.com, neeraju@codeaurora.org, ak@linux.intel.com, feng.tang@intel.com, zhengjun.xing@intel.com, "Paul E. McKenney" Subject: [PATCH v10 clocksource 5/7] clocksource: Limit number of CPUs checked for clock synchronization Date: Sun, 25 Apr 2021 15:47:06 -0700 Message-Id: <20210425224709.1312655-5-paulmck@kernel.org> X-Mailer: git-send-email 2.31.1.189.g2e36527f23 In-Reply-To: <20210425224540.GA1312438@paulmck-ThinkPad-P17-Gen-1> References: <20210425224540.GA1312438@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, if skew is detected on a clock marked CLOCK_SOURCE_VERIFY_PERCPU, that clock is checked on all CPUs. This is thorough, but might not be what you want on a system with a few tens of CPUs, let alone a few hundred of them. Therefore, by default check only up to eight randomly chosen CPUs. Also provide a new clocksource.verify_n_cpus kernel boot parameter. A value of -1 says to check all of the CPUs, and a non-negative value says to randomly select that number of CPUs, without concern about selecting the same CPU multiple times. However, make use of a cpumask so that a given CPU will be checked at most once. Suggested-by: Thomas Gleixner # For verify_n_cpus=1. Cc: John Stultz Cc: Thomas Gleixner Cc: Stephen Boyd Cc: Jonathan Corbet Cc: Mark Rutland Cc: Marc Zyngier Cc: Andi Kleen Signed-off-by: Paul E. McKenney --- .../admin-guide/kernel-parameters.txt | 10 +++ kernel/time/clocksource.c | 74 ++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 03a8c88a6bb9..6e5b5f19fc56 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -621,6 +621,16 @@ unstable. Defaults to three retries, that is, four attempts to read the clock under test. + clocksource.verify_n_cpus= [KNL] + Limit the number of CPUs checked for clocksources + marked with CLOCK_SOURCE_VERIFY_PERCPU that + are marked unstable due to excessive skew. + A negative value says to check all CPUs, while + zero says not to check any. Values larger than + nr_cpu_ids are silently truncated to nr_cpu_ids. + The actual CPUs are chosen randomly, with + no replacement if the same CPU is chosen twice. + clearcpuid=BITNUM[,BITNUM...] [X86] Disable CPUID feature X for the kernel. See arch/x86/include/asm/cpufeatures.h for the valid bit diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c index 584433448226..f71f375df544 100644 --- a/kernel/time/clocksource.c +++ b/kernel/time/clocksource.c @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include "tick-internal.h" #include "timekeeping_internal.h" @@ -200,6 +202,8 @@ static int inject_delay_shift_percpu = -1; module_param(inject_delay_shift_percpu, int, 0644); static ulong max_read_retries = 3; module_param(max_read_retries, ulong, 0644); +static int verify_n_cpus = 8; +module_param(verify_n_cpus, int, 0644); static void clocksource_watchdog_inject_delay(void) { @@ -250,6 +254,55 @@ static bool cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow) static u64 csnow_mid; static cpumask_t cpus_ahead; static cpumask_t cpus_behind; +static cpumask_t cpus_chosen; + +static void clocksource_verify_choose_cpus(void) +{ + int cpu, i, n = verify_n_cpus; + + if (n < 0) { + /* Check all of the CPUs. */ + cpumask_copy(&cpus_chosen, cpu_online_mask); + cpumask_clear_cpu(smp_processor_id(), &cpus_chosen); + return; + } + + /* If no checking desired, or no other CPU to check, leave. */ + cpumask_clear(&cpus_chosen); + if (n == 0 || num_online_cpus() <= 1) + return; + + /* Make sure to select at least one CPU other than the current CPU. */ + cpu = cpumask_next(-1, cpu_online_mask); + if (cpu == smp_processor_id()) + cpu = cpumask_next(cpu, cpu_online_mask); + if (WARN_ON_ONCE(cpu >= nr_cpu_ids)) + return; + cpumask_set_cpu(cpu, &cpus_chosen); + + /* Force a sane value for the boot parameter. */ + if (n > nr_cpu_ids) + n = nr_cpu_ids; + + /* + * Randomly select the specified number of CPUs. If the same + * CPU is selected multiple times, that CPU is checked only once, + * and no replacement CPU is selected. This gracefully handles + * situations where verify_n_cpus is greater than the number of + * CPUs that are currently online. + */ + for (i = 1; i < n; i++) { + cpu = prandom_u32() % nr_cpu_ids; + cpu = cpumask_next(cpu - 1, cpu_online_mask); + if (cpu >= nr_cpu_ids) + cpu = cpumask_next(-1, cpu_online_mask); + if (!WARN_ON_ONCE(cpu >= nr_cpu_ids)) + cpumask_set_cpu(cpu, &cpus_chosen); + } + + /* Don't verify ourselves. */ + cpumask_clear_cpu(smp_processor_id(), &cpus_chosen); +} static void clocksource_verify_one_cpu(void *csin) { @@ -271,12 +324,22 @@ static void clocksource_verify_percpu(struct clocksource *cs) int cpu, testcpu; s64 delta; + if (verify_n_cpus == 0) + return; cpumask_clear(&cpus_ahead); cpumask_clear(&cpus_behind); + get_online_cpus(); preempt_disable(); + clocksource_verify_choose_cpus(); + if (cpumask_weight(&cpus_chosen) == 0) { + preempt_enable(); + put_online_cpus(); + pr_warn("Not enough CPUs to check clocksource '%s'.\n", cs->name); + return; + } testcpu = smp_processor_id(); - pr_warn("Checking clocksource %s synchronization from CPU %d.\n", cs->name, testcpu); - for_each_online_cpu(cpu) { + pr_warn("Checking clocksource %s synchronization from CPU %d to CPUs %*pbl.\n", cs->name, testcpu, cpumask_pr_args(&cpus_chosen)); + for_each_cpu(cpu, &cpus_chosen) { if (cpu == testcpu) continue; csnow_begin = cs->read(cs); @@ -296,6 +359,7 @@ static void clocksource_verify_percpu(struct clocksource *cs) cs_nsec_min = cs_nsec; } preempt_enable(); + put_online_cpus(); if (!cpumask_empty(&cpus_ahead)) pr_warn(" CPUs %*pbl ahead of CPU %d for clocksource %s.\n", cpumask_pr_args(&cpus_ahead), testcpu, cs->name); @@ -366,6 +430,12 @@ static void clocksource_watchdog(struct timer_list *unused) watchdog->name, wdnow, wdlast, watchdog->mask); pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n", cs->name, csnow, cslast, cs->mask); + if (curr_clocksource == cs) + pr_warn(" '%s' is current clocksource.\n", cs->name); + else if (curr_clocksource) + pr_warn(" '%s' (not '%s') is current clocksource.\n", curr_clocksource->name, cs->name); + else + pr_warn(" No current clocksource.\n"); __clocksource_unstable(cs); continue; } -- 2.31.1.189.g2e36527f23