From nobody Sat Apr 11 09:24:34 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABF5CC00140 for ; Fri, 12 Aug 2022 10:03:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237062AbiHLKD5 (ORCPT ); Fri, 12 Aug 2022 06:03:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231637AbiHLKDz (ORCPT ); Fri, 12 Aug 2022 06:03:55 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D9A3AA4E3; Fri, 12 Aug 2022 03:03:53 -0700 (PDT) Received: from canpemm500009.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4M3zg318qszXdVQ; Fri, 12 Aug 2022 17:59:43 +0800 (CST) Received: from huawei.com (10.67.174.191) by canpemm500009.china.huawei.com (7.192.105.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 12 Aug 2022 18:03:50 +0800 From: Li Hua To: , , , , , , , , , CC: , Subject: [PATCH -next] sched/cputime: Fix the bug of reading time backward from /proc/stat Date: Sat, 13 Aug 2022 08:01:02 +0800 Message-ID: <20220813000102.42051-1-hucool.lihua@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.67.174.191] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500009.china.huawei.com (7.192.105.203) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The problem that the statistical time goes backward, the value read first i= s 319, and the value read again is 318. As follows=EF=BC=9A first=EF=BC=9A cat /proc/stat | grep cpu1 cpu1 319 0 496 41665 0 0 0 0 0 0 then=EF=BC=9A cat /proc/stat | grep cpu1 cpu1 318 0 497 41674 0 0 0 0 0 0 Time goes back, which is counterintuitive. After debug this, The problem is caused by the implementation of kcpustat_c= pu_fetch_vtime. As follows=EF=BC=9A CPU0 = CPU1 First: show_stat(): ->kcpustat_cpu_fetch() ->kcpustat_cpu_fetch_vtime() ->cpustat[CPUTIME_USER] =3D kcpustat_cpu(cpu) + vtime->utime + = delta; rq->curr is in user mod ---> When CPU1 rq->curr running on userspace, need add utime a= nd delta = ---> rq->curr->vtime->utime is less than 1 tick Then: show_stat(): ->kcpustat_cpu_fetch() ->kcpustat_cpu_fetch_vtime() ->cpustat[CPUTIME_USER] =3D kcpustat_cpu(cpu); = rq->curr is in kernel mod ---> When CPU1 rq->curr running on kernel space, just got kcpus= tat Fixes: 74722bb223d0 ("sched/vtime: Bring up complete kcpustat accessor") Signed-off-by: Li Hua --- kernel/sched/core.c | 1 + kernel/sched/cputime.c | 33 ++++++++++++++++++++++++++++++++- kernel/sched/sched.h | 6 ++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 189999007f32..c542b61cab54 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -9753,6 +9753,7 @@ void __init sched_init(void) =20 rq->core_cookie =3D 0UL; #endif + cputime_cpu_init(i); } =20 set_load_weight(&init_task, false); diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c index 95fc77853743..ba3bcb40795e 100644 --- a/kernel/sched/cputime.c +++ b/kernel/sched/cputime.c @@ -1060,6 +1060,19 @@ static int kcpustat_cpu_fetch_vtime(struct kernel_cp= ustat *dst, return 0; } =20 +/* + * Stores the time of the last acquisition, which is used to handle the ca= se of + * time backwards. + */ +static DEFINE_PER_CPU(struct kernel_cpustat, cpustat_prev); +static DEFINE_PER_CPU(raw_spinlock_t, cpustat_prev_lock); + +void cputime_cpu_init(int cpu) +{ + raw_spin_lock_init(per_cpu_ptr(&cpustat_prev_lock, cpu)); +} + + void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu) { const struct kernel_cpustat *src =3D &kcpustat_cpu(cpu); @@ -1087,8 +1100,26 @@ void kcpustat_cpu_fetch(struct kernel_cpustat *dst, = int cpu) err =3D kcpustat_cpu_fetch_vtime(dst, src, curr, cpu); rcu_read_unlock(); =20 - if (!err) + if (!err) { + int i; + int map[5] =3D {CPUTIME_USER, CPUTIME_SYSTEM, CPUTIME_NICE, + CPUTIME_GUEST, CPUTIME_GUEST_NICE}; + struct kernel_cpustat *prev =3D &per_cpu(cpustat_prev, cpu); + raw_spinlock_t *cpustat_lock =3D &per_cpu(cpustat_prev_lock, cpu); + u64 *dst_stat =3D dst->cpustat; + u64 *prev_stat =3D prev->cpustat; + + raw_spin_lock(cpustat_lock); + for (i =3D 0; i < 5; i++) { + int idx =3D map[i]; + + if (dst_stat[idx] < prev_stat[idx]) + dst_stat[idx] =3D prev_stat[idx]; + } + *prev =3D *dst; + raw_spin_unlock(cpustat_lock); return; + } =20 cpu_relax(); } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index a6f071b2acac..cbe09795a394 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3156,4 +3156,10 @@ extern int sched_dynamic_mode(const char *str); extern void sched_dynamic_update(int mode); #endif =20 +#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN +extern void cputime_cpu_init(int cpu); +#else +static inline void cputime_cpu_init(int cpu) {} +#endif + #endif /* _KERNEL_SCHED_SCHED_H */ --=20 2.17.1