Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5394981rwd; Sun, 18 Jun 2023 10:31:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6em/q7dNqxPxaio00qL6x+aHmIXOQ5a5yO7terD6K7rp9/NSZT4zPdy7oNLyg7bTQqTibx X-Received: by 2002:a17:902:8ec8:b0:1b3:c578:c3e9 with SMTP id x8-20020a1709028ec800b001b3c578c3e9mr3874598plo.62.1687109503706; Sun, 18 Jun 2023 10:31:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687109503; cv=none; d=google.com; s=arc-20160816; b=sGGkErXPPtVFXP5ukl81L5UbPgkcUwUwOeR40pGDOALHl9aU0Xbe6bedhiJnrlM4Nf 8VX0q0xq6TSKSsZJPSVqs1pJeQQJ77C3havfkicwYXyDwuv6XBneXVWV5FI1CFojf0wo EpdSqkJNmYV5FXtSVZg83D5evEhcP7yKPX/hpbh+vA8Y9x3UBaz+2dFWEUg0FHWFyXMR j6Q5vNsN9HaD2QOp5F0fYcW+H1EnvFqAvNKqx/G7LI78wkXIx1fiQpUWuDykidXyjaIJ 6YScFSAXmgx1jVuKd6a3CUGgz/xOmCLer5Y03W15dI5TOCTtcVAr/LXjlRLvBjFmcQ5A BtQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=pOrL8BFRHGPFKNt50HWOdSNYJN70HOwoymgdsxH1iyQ=; b=yzeGpP9ZitBiMYEdnB5BYx948foq1fmkBu+mN+Qs0JJvtgvYHZdnJpefCfFk90Xopf qF2rba9ABU6UYBMMye2wyTFWIJy4Qql7SwWfhS0yeMmxJRfEv2CXjGy1drq444Rwtyk2 RkH0zShBTNUV+IseCFXe/SSMePMDGLtIH9s249iMTSj/SxlZchQX320ywDIMmMzc0i/O EAs0UawDVn1ZPdg3gFaMskDxHkXF3astpFSLFKcDB04wntnvu/YwriFxk5NP3kT9zHkS fAjZ/tis8S5F6gEeir1nhz0Ur03FZ4ygDn32yOaAyAcDThNmdqixy0l0GMcQ8SekaSa5 Zt0Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j5-20020a170902da8500b001ae3b512697si20554294plx.113.2023.06.18.10.31.25; Sun, 18 Jun 2023 10:31:43 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229492AbjFRRDF (ORCPT + 99 others); Sun, 18 Jun 2023 13:03:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229458AbjFRRDE (ORCPT ); Sun, 18 Jun 2023 13:03:04 -0400 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24CC0B7 for ; Sun, 18 Jun 2023 10:02:58 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R121e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045192;MF=cruzzhao@linux.alibaba.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---0VlMn5WT_1687107761; Received: from rt2b04371.sqa.tbc.tbsite.net(mailfrom:CruzZhao@linux.alibaba.com fp:SMTPD_---0VlMn5WT_1687107761) by smtp.aliyun-inc.com; Mon, 19 Jun 2023 01:02:54 +0800 From: Cruz Zhao To: mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, vschneid@redhat.com, paulmck@kernel.org, frederic@kernel.org, nsaenzju@redhat.com Cc: linux-kernel@vger.kernel.org Subject: [PATCH] sched/core: fix inaccurate idle stat in /proc/stat when force idle Date: Mon, 19 Jun 2023 01:02:40 +0800 Message-Id: <1687107760-111340-1-git-send-email-CruzZhao@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL 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 When force idle, rq->curr == rq->idle but rq->nr_running > 0, results that idle_cpu() returns 0. In function tick_irq_exit(), if idle_cpu() is 0, tick_nohz_irq_exit() will not be called, and ts->idle_active will not become 1, which became 0 in tick_nohz_irq_enter(). ts->idle_sleeptime won't update in function update_ts_time_stats(), if ts->idle_active is 0, which should be 1. And this bug will result that ts->idle_sleeptime is less than the actual value, and finally will result that the idle time in /proc/stat is less than the actual value. To fix this problem, we introduce sched_core_idle_cpu(), which returns true when force idle, and we change the judgment criteria from idle_cpu() to sched_core_idle_cpu() in function tick_irq_exit(). Signed-off-by: Cruz Zhao --- include/linux/sched.h | 2 ++ kernel/sched/core.c | 13 +++++++++++++ kernel/softirq.c | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index b09a83bfad8b..73e61c0f10a7 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2430,9 +2430,11 @@ extern void sched_core_free(struct task_struct *tsk); extern void sched_core_fork(struct task_struct *p); extern int sched_core_share_pid(unsigned int cmd, pid_t pid, enum pid_type type, unsigned long uaddr); +extern int sched_core_idle_cpu(int cpu); #else static inline void sched_core_free(struct task_struct *tsk) { } static inline void sched_core_fork(struct task_struct *p) { } +static inline int sched_core_idle_cpu(int cpu) { return idle_cpu(cpu); } #endif extern void sched_set_stop_task(int cpu, struct task_struct *stop); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 71c1a0f232b4..c80088956987 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -7286,6 +7286,19 @@ struct task_struct *idle_task(int cpu) return cpu_rq(cpu)->idle; } +#ifdef CONFIG_SCHED_CORE +int sched_core_idle_cpu(int cpu) +{ + struct rq *rq = cpu_rq(cpu); + + if (sched_core_enabled(rq) && rq->curr == rq->idle) + return 1; + + return idle_cpu(cpu); +} + +#endif + #ifdef CONFIG_SMP /* * This function computes an effective utilization for the given CPU, to be diff --git a/kernel/softirq.c b/kernel/softirq.c index c8a6913c067d..98b98991ce45 100644 --- a/kernel/softirq.c +++ b/kernel/softirq.c @@ -630,7 +630,7 @@ static inline void tick_irq_exit(void) int cpu = smp_processor_id(); /* Make sure that timer wheel updates are propagated */ - if ((idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) { + if ((sched_core_idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) { if (!in_hardirq()) tick_nohz_irq_exit(); } -- 2.27.0