Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1884012lqe; Tue, 9 Apr 2024 03:29:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUzBhxsDwnDV5814b0xmrxBg8gEuNOWtn3NycDm2JRDiGNElml/RTLuqCCSavjGm5VgQCEgqRZaNOHCVYSNwP2pqy2haaONK0bZvA7ULA== X-Google-Smtp-Source: AGHT+IGwAAkWYUpp8n4e7TQ56lbWmEk9MQDlf6cXg5G8CmsYVhv4WYeVNvu94kHpNzx0LrhBigNf X-Received: by 2002:a05:6214:29ee:b0:699:406d:9030 with SMTP id jv14-20020a05621429ee00b00699406d9030mr12950727qvb.38.1712658563303; Tue, 09 Apr 2024 03:29:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712658563; cv=pass; d=google.com; s=arc-20160816; b=z6jKatfgfNQh9/BosuEKDjFzLJiym17Ae0m+WeCi/6Z6glq6iFc+J189dWzCM5VHjx cY13qHAfejqitTuiwxTwxIGL0F0IFDAER/hRO5JFj4Al+ZQIfLcVEVV5M0bdwJ0p2Ozg oS3/GcA39/dB+AtG3K0c0frWxfokgyeBb/5o7kurcrAUD7ag3TcI32SOMgTCrBtpbO+x xcRHT4/BRNanAGaBt4q5RkWaytYMdhnuKKsKgPTt+suOBFAbavQr4eUeUd0QL6IQ2ifM I4zx4CnULQvweSorW/igVS6zVaKghgH2wm6aEf/DK4xbzC9OKlQs8PMA+ToPIysPIzWQ Vy7g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :message-id:date:references:in-reply-to:subject:cc:to:dkim-signature :dkim-signature:from; bh=EF6ovFLdWNLhvSGuwEQg4yxIeTGiTX0nsnyNSwoTsr0=; fh=B/d2Xzaypxlti1E9w+Rp+Hx/am0YI71N07FjIadMQps=; b=u02kDRuA+izg3ztn8v4P+zuZxwiBl77qpdPBrm3q8voHvz3tjap+ZPg4x0/MxRnImb L1dqXMF13FbprzMfuRvkreMdRlaDt5gjj/plfqjqj1tdHm0fyxhwQmmLLobGxMhC2Q9f DNlDX9lDJ74uF9gPExQosB1PONQ+/5ct+NF0+6akrSVHA+MzEhi6E1n5EpmX+NHKMNt8 su2Bf1SkRMGigcVolk4AzX8paZ679TJTzhz9u2xPHaKCdG1SW5ZCMbFBCkkqXj0K5BXK B6NZkzz+Qkr/Da8hjfQMvvvTvfYNAVp4vnXpXwEHe1l0lQL40Q2yMznsnnKQpUULGxvt 3N6g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=lYZJJGS1; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-136674-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136674-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id jh23-20020a0562141fd700b00696b0422475si9728152qvb.156.2024.04.09.03.29.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Apr 2024 03:29:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-136674-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=lYZJJGS1; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-136674-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136674-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0ACBF1C20D3E for ; Tue, 9 Apr 2024 10:29:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77A1081AB4; Tue, 9 Apr 2024 10:29:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lYZJJGS1"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0pcujErO" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 871F180629 for ; Tue, 9 Apr 2024 10:29:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712658556; cv=none; b=M5YBwqpv/SVf31v+VU6khHTF00eggkbZ9HWPqQO1OwQXfoSjrPjCdz8UEkstrg8ylt9xPL0uyY2sjQCEXsKsKk1qcwV3zKWP/egRVNgHNCzBgVMAqGNr8GV1A3VElFbE7DciF0VevR0wursIlcn+i+NeyZUNsoX5v4j72/WA/8E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712658556; c=relaxed/simple; bh=XNnyP8qVi1HgN7u6NP7Q7yseLfpjzGBeNAOYFNuri88=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=RZzhbBisiyO+IiR8jQc2nFJN22vw4mn/jnQki47I4ILeY0W5njXmUusjaw+o+kCU9AVAip+RMoOissGq83EJGBsgvph4DLy/PQye80AvV7OHSOhqdkm4VAk71mmQ64E7t3yo+I7aUEq79VSsh3dONLeAqnhryuPecbUOQHA3Q+U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lYZJJGS1; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0pcujErO; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712658552; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=EF6ovFLdWNLhvSGuwEQg4yxIeTGiTX0nsnyNSwoTsr0=; b=lYZJJGS1T08kmtgYpWSzPd3Ox4L8SjKZOeTLqC4dEC0cQJRdfztnNIOhWhxc4wW7X+g4H7 +iPdkT5l3MdTExFEjZREIrunsdd0R9L135tv4I+eZ79Wxs7yXHNwTLegUDWefjN49wctZI wtfz12cfp71zTWVC7tMsokckBTfNGn42SJp2Q2S6mzNTu94rTfO5P+oEBLyJJ5CpvEAnfy aiudqhaOglPtSmPR+dHj4++m5/aHeMK7H+l9V3KCrPEhgicbkOvqKH5Fd6zsjXt5DqkKvj rZ9sobL+RXRwaglGNRrv71dWI0Ttu4bLKWcaCfwMLD9g0CCpkpFSnCXXNwGtMg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712658552; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=EF6ovFLdWNLhvSGuwEQg4yxIeTGiTX0nsnyNSwoTsr0=; b=0pcujErOj0KtWGpK/4UyeHbcdqpP0+9z5skGuaH7NrgnggkFNteBp3Z2wrETC/NgE+lN3+ l2cL83BopLDVphCQ== To: Sean Anderson , Mirsad Todorovac , linux-kernel@vger.kernel.org Cc: Frederic Weisbecker , Ingo Molnar Subject: timekeeping: Use READ/WRITE_ONCE() for tick_do_timer_cpu In-Reply-To: References: <40ac9903-469a-b1ea-4339-8b06f855450e@alu.unizg.hr> <3993c468-fdac-2ff2-c3ee-9784c098694c@alu.unizg.hr> <83b3c900-d4eb-457f-99e6-cb21e0eae4cb@seco.com> <87a5n4rfhu.ffs@tglx> Date: Tue, 09 Apr 2024 12:29:12 +0200 Message-ID: <87cyqy7rt3.ffs@tglx> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain tick_do_timer_cpu is used lockless to check which CPU needs to take care of the per tick timekeeping duty. This is done to avoid a thundering herd problem on jiffies_lock. The read and writes are not annotated so KCSAN complains about data races: BUG: KCSAN: data-race in tick_nohz_idle_stop_tick / tick_nohz_next_event write to 0xffffffff8a2bda30 of 4 bytes by task 0 on cpu 26: tick_nohz_idle_stop_tick+0x3b1/0x4a0 do_idle+0x1e3/0x250 read to 0xffffffff8a2bda30 of 4 bytes by task 0 on cpu 16: tick_nohz_next_event+0xe7/0x1e0 tick_nohz_get_sleep_length+0xa7/0xe0 menu_select+0x82/0xb90 cpuidle_select+0x44/0x60 do_idle+0x1c2/0x250 value changed: 0x0000001a -> 0xffffffff Annotate them with READ/WRITE_ONCE() to document the intentional data race. Reported-by: Mirsad Todorovac Signed-off-by: Thomas Gleixner Tested-by: Sean Anderson --- kernel/time/tick-common.c | 17 +++++++++-------- kernel/time/tick-sched.c | 36 ++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 22 deletions(-) --- a/kernel/time/tick-common.c +++ b/kernel/time/tick-common.c @@ -7,6 +7,7 @@ * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner */ +#include #include #include #include @@ -84,7 +85,7 @@ int tick_is_oneshot_available(void) */ static void tick_periodic(int cpu) { - if (tick_do_timer_cpu == cpu) { + if (READ_ONCE(tick_do_timer_cpu) == cpu) { raw_spin_lock(&jiffies_lock); write_seqcount_begin(&jiffies_seq); @@ -215,8 +216,8 @@ static void tick_setup_device(struct tic * If no cpu took the do_timer update, assign it to * this cpu: */ - if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) { - tick_do_timer_cpu = cpu; + if (READ_ONCE(tick_do_timer_cpu) == TICK_DO_TIMER_BOOT) { + WRITE_ONCE(tick_do_timer_cpu, cpu); tick_next_period = ktime_get(); #ifdef CONFIG_NO_HZ_FULL /* @@ -232,7 +233,7 @@ static void tick_setup_device(struct tic !tick_nohz_full_cpu(cpu)) { tick_take_do_timer_from_boot(); tick_do_timer_boot_cpu = -1; - WARN_ON(tick_do_timer_cpu != cpu); + WARN_ON(READ_ON_ONCE(tick_do_timer_cpu) != cpu); #endif } @@ -406,10 +407,10 @@ void tick_assert_timekeeping_handover(vo int tick_cpu_dying(unsigned int dying_cpu) { /* - * If the current CPU is the timekeeper, it's the only one that - * can safely hand over its duty. Also all online CPUs are in - * stop machine, guaranteed not to be idle, therefore it's safe - * to pick any online successor. + * If the current CPU is the timekeeper, it's the only one that can + * safely hand over its duty. Also all online CPUs are in stop + * machine, guaranteed not to be idle, therefore there is no + * concurrency and it's safe to pick any online successor. */ if (tick_do_timer_cpu == dying_cpu) tick_do_timer_cpu = cpumask_first(cpu_online_mask); --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -8,6 +8,7 @@ * * Started by: Thomas Gleixner and Ingo Molnar */ +#include #include #include #include @@ -204,7 +205,7 @@ static inline void tick_sched_flag_clear static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now) { - int cpu = smp_processor_id(); + int tick_cpu, cpu = smp_processor_id(); /* * Check if the do_timer duty was dropped. We don't care about @@ -216,16 +217,18 @@ static void tick_sched_do_timer(struct t * If nohz_full is enabled, this should not happen because the * 'tick_do_timer_cpu' CPU never relinquishes. */ - if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && - unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)) { + tick_cpu = READ_ONCE(tick_do_timer_cpu); + + if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && unlikely(tick_cpu == TICK_DO_TIMER_NONE)) { #ifdef CONFIG_NO_HZ_FULL WARN_ON_ONCE(tick_nohz_full_running); #endif - tick_do_timer_cpu = cpu; + WRITE_ONCE(tick_do_timer_cpu, cpu); + tick_cpu = cpu; } /* Check if jiffies need an update */ - if (tick_do_timer_cpu == cpu) + if (tick_cpu == cpu) tick_do_update_jiffies64(now); /* @@ -610,7 +613,7 @@ bool tick_nohz_cpu_hotpluggable(unsigned * timers, workqueues, timekeeping, ...) on behalf of full dynticks * CPUs. It must remain online when nohz full is enabled. */ - if (tick_nohz_full_running && tick_do_timer_cpu == cpu) + if (tick_nohz_full_running && READ_ONCE(tick_do_timer_cpu) == cpu) return false; return true; } @@ -891,6 +894,7 @@ static ktime_t tick_nohz_next_event(stru { u64 basemono, next_tick, delta, expires; unsigned long basejiff; + int tick_cpu; basemono = get_jiffies_update(&basejiff); ts->last_jiffies = basejiff; @@ -947,9 +951,9 @@ static ktime_t tick_nohz_next_event(stru * Otherwise we can sleep as long as we want. */ delta = timekeeping_max_deferment(); - if (cpu != tick_do_timer_cpu && - (tick_do_timer_cpu != TICK_DO_TIMER_NONE || - !tick_sched_flag_test(ts, TS_FLAG_DO_TIMER_LAST))) + tick_cpu = READ_ONCE(tick_do_timer_cpu); + if (tick_cpu != cpu && + (tick_cpu != TICK_DO_TIMER_NONE || !tick_sched_flag_test(ts, TS_FLAG_DO_TIMER_LAST))) delta = KTIME_MAX; /* Calculate the next expiry time */ @@ -970,6 +974,7 @@ static void tick_nohz_stop_tick(struct t unsigned long basejiff = ts->last_jiffies; u64 basemono = ts->timer_expires_base; bool timer_idle = tick_sched_flag_test(ts, TS_FLAG_STOPPED); + int tick_cpu; u64 expires; /* Make sure we won't be trying to stop it twice in a row. */ @@ -1007,10 +1012,11 @@ static void tick_nohz_stop_tick(struct t * do_timer() never gets invoked. Keep track of the fact that it * was the one which had the do_timer() duty last. */ - if (cpu == tick_do_timer_cpu) { - tick_do_timer_cpu = TICK_DO_TIMER_NONE; + tick_cpu = READ_ONCE(tick_do_timer_cpu); + if (tick_cpu == cpu) { + WRITE_ONCE(tick_do_timer_cpu, TICK_DO_TIMER_NONE); tick_sched_flag_set(ts, TS_FLAG_DO_TIMER_LAST); - } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) { + } else if (tick_cpu != TICK_DO_TIMER_NONE) { tick_sched_flag_clear(ts, TS_FLAG_DO_TIMER_LAST); } @@ -1173,15 +1179,17 @@ static bool can_stop_idle_tick(int cpu, return false; if (tick_nohz_full_enabled()) { + int tick_cpu = READ_ONCE(tick_do_timer_cpu); + /* * Keep the tick alive to guarantee timekeeping progression * if there are full dynticks CPUs around */ - if (tick_do_timer_cpu == cpu) + if (tick_cpu == cpu) return false; /* Should not happen for nohz-full */ - if (WARN_ON_ONCE(tick_do_timer_cpu == TICK_DO_TIMER_NONE)) + if (WARN_ON_ONCE(tick_cpu == TICK_DO_TIMER_NONE)) return false; }