From patchwork Tue Apr 18 14:53:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 84906 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2912710vqo; Tue, 18 Apr 2023 07:54:50 -0700 (PDT) X-Google-Smtp-Source: AKy350a749jApTMnX4+V6lVNtK2nXXPPk72DN923VAdfggrTkRRgj4nYUDM1AZHZAq+qjDSCvvxp X-Received: by 2002:a05:6a20:394a:b0:ef:fd71:3041 with SMTP id r10-20020a056a20394a00b000effd713041mr6843777pzg.54.1681829690513; Tue, 18 Apr 2023 07:54:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681829690; cv=none; d=google.com; s=arc-20160816; b=stEpJehTctYw3lr9KctrtSAjrTN2Eu1RW6OHmOpKsHXXFM76XFY428H2bmkj4S0rVY 11BRA7OehSn+KBVKg8ya8rl0ZzeIQgPV3jPyhzfB0Vk6Jy3hMIjQBPFVgy2fNn4ClbLt LhH4uU+VsE2/aOOd96Qoy8OzDjS9Pw0yg1pKnSHUqAl7q4ntpG7B8gdTpQYPEo9EZ1sd zOVbeLzKSxfzD1cCBqVruon9rZvO1+7IKJS7RHXokkC4oRLVioQO2H2qQ6/azqr0HYLF uJ7d+/lBzdNnt6b/A4l80+7Jdy1r3f4PGB1FMtMPhgW+ixRWpl7fcvF9n895QV27OUzY Rjsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=85JUodLV7EWbfM40Ibp7CaeedStOFvRsNIpu1aewYc8=; b=g0o1RzWO0TIauenLK8xzfA/IAU0wBcznZAOdM7+Vu4LpAYTWSuXrg/SVYd2BQvD/Eg PLov//d8acai43cxQ6nAwg1b0T4MYXmg3XR2/Mgw0a1A5GBAqUFKUGAbhqD59bS/pbQq SZx8mnR7dbi4N8LhnsT2OKOQ3U0DEEJCi9yKeo0OjEzdEpOpCbilfDK8QGmyKUuHcpKr 47F7PtS7ufuS42W9mBJGqF6KExz6W1gJ1mA7ZwpATaGCbVRrkHvcTAD0CjS7Hacvp+ex NYfoMSkLVHzW8w9ZMwOomjOIHQC3FFG3hJSKKTnjGG54Pcd338Essxu9BnaiU0qg0Ger nUJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=I6icHYbz; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t14-20020a6549ce000000b0051453dfd139si1548621pgs.613.2023.04.18.07.54.35; Tue, 18 Apr 2023 07:54:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=I6icHYbz; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232139AbjDROxv (ORCPT + 99 others); Tue, 18 Apr 2023 10:53:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231492AbjDROxg (ORCPT ); Tue, 18 Apr 2023 10:53:36 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB9E7AD2A; Tue, 18 Apr 2023 07:53:34 -0700 (PDT) Date: Tue, 18 Apr 2023 14:53:29 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1681829610; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=85JUodLV7EWbfM40Ibp7CaeedStOFvRsNIpu1aewYc8=; b=I6icHYbzw2mps60pQHWVw8tMzY4WON367lA/CRcH0QySPd+rrEVIed/4wd7Yc1tsBWzRxL xKCMr9U/Zl5jRMBXwPt+0IpQ508Gue3JQJUDTCNR8ysPOl40bmVVr6CDWBGq8jQaJp1L3/ I1MoOBK2YLX/ptTo1NB4S2WfL0X+ZX13NSCNvvp5qEa3RP/vDdOyrdYDHYbpoxILB/TVKa UfSRDWEwpCuXoci9euzX0c1R9Q7crrENG39dk5hlEvQAqE0dPdbidkaXjCu8lWhsQSunKt 1bVujyqJ30KJNk3as8NAPspKRc6/93wF5lA6Je4F0q0Sk8ew6N5bimjDA23M4Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1681829610; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=85JUodLV7EWbfM40Ibp7CaeedStOFvRsNIpu1aewYc8=; b=EL6BD7hTdtHDDXqVWIt5OcTVTDYsvmgYWYYGKoN/sVXsEaFr309AGyLA28k0cN9JMgWOrQ ojYe89l3gYH+59Cg== From: "tip-bot2 for Frederic Weisbecker" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] timers/nohz: Protect idle/iowait sleep time under seqcount Cc: Yu Liao , Frederic Weisbecker , Thomas Gleixner , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230222144649.624380-4-frederic@kernel.org> References: <20230222144649.624380-4-frederic@kernel.org> MIME-Version: 1.0 Message-ID: <168182960967.404.10362810597852537883.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758543243476744223?= X-GMAIL-MSGID: =?utf-8?q?1763526249939346944?= The following commit has been merged into the timers/core branch of tip: Commit-ID: 620a30fa0bd14878891b22bf2261e6ed4587c2b4 Gitweb: https://git.kernel.org/tip/620a30fa0bd14878891b22bf2261e6ed4587c2b4 Author: Frederic Weisbecker AuthorDate: Wed, 22 Feb 2023 15:46:44 +01:00 Committer: Thomas Gleixner CommitterDate: Tue, 18 Apr 2023 16:35:12 +02:00 timers/nohz: Protect idle/iowait sleep time under seqcount Reading idle/IO sleep time (eg: from /proc/stat) can race with idle exit updates because the state machine handling the stats is not atomic and requires a coherent read batch. As a result reading the sleep time may report irrelevant or backward values. Fix this with protecting the simple state machine within a seqcount. This is expected to be cheap enough not to add measurable performance impact on the idle path. Note this only fixes reader VS writer condition partitially. A race remains that involves remote updates of the CPU iowait task counter. It can hardly be fixed. Reported-by: Yu Liao Signed-off-by: Frederic Weisbecker Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Link: https://lore.kernel.org/r/20230222144649.624380-4-frederic@kernel.org --- kernel/time/tick-sched.c | 22 ++++++++++++++++------ kernel/time/tick-sched.h | 1 + 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index 9058b9e..90d9b7b 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -646,6 +646,7 @@ static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now) delta = ktime_sub(now, ts->idle_entrytime); + write_seqcount_begin(&ts->idle_sleeptime_seq); if (nr_iowait_cpu(smp_processor_id()) > 0) ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta); else @@ -653,14 +654,18 @@ static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now) ts->idle_entrytime = now; ts->idle_active = 0; + write_seqcount_end(&ts->idle_sleeptime_seq); sched_clock_idle_wakeup_event(); } static void tick_nohz_start_idle(struct tick_sched *ts) { + write_seqcount_begin(&ts->idle_sleeptime_seq); ts->idle_entrytime = ktime_get(); ts->idle_active = 1; + write_seqcount_end(&ts->idle_sleeptime_seq); + sched_clock_idle_sleep_event(); } @@ -668,6 +673,7 @@ static u64 get_cpu_sleep_time_us(struct tick_sched *ts, ktime_t *sleeptime, bool compute_delta, u64 *last_update_time) { ktime_t now, idle; + unsigned int seq; if (!tick_nohz_active) return -1; @@ -676,13 +682,17 @@ static u64 get_cpu_sleep_time_us(struct tick_sched *ts, ktime_t *sleeptime, if (last_update_time) *last_update_time = ktime_to_us(now); - if (ts->idle_active && compute_delta) { - ktime_t delta = ktime_sub(now, ts->idle_entrytime); + do { + seq = read_seqcount_begin(&ts->idle_sleeptime_seq); - idle = ktime_add(*sleeptime, delta); - } else { - idle = *sleeptime; - } + if (ts->idle_active && compute_delta) { + ktime_t delta = ktime_sub(now, ts->idle_entrytime); + + idle = ktime_add(*sleeptime, delta); + } else { + idle = *sleeptime; + } + } while (read_seqcount_retry(&ts->idle_sleeptime_seq, seq)); return ktime_to_us(idle); diff --git a/kernel/time/tick-sched.h b/kernel/time/tick-sched.h index c666325..5ed5a9d 100644 --- a/kernel/time/tick-sched.h +++ b/kernel/time/tick-sched.h @@ -75,6 +75,7 @@ struct tick_sched { ktime_t idle_waketime; /* Idle entry */ + seqcount_t idle_sleeptime_seq; ktime_t idle_entrytime; /* Tick stop */