Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp4285905pxv; Mon, 19 Jul 2021 23:13:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwIhu8fuLOB5Iv1OkVN5WLDjJJdGVCDMA8r7JcdWDbU4+VS2nw+HZJ4539eNGL+Icr8KHvB X-Received: by 2002:a17:906:c097:: with SMTP id f23mr29694606ejz.194.1626761616309; Mon, 19 Jul 2021 23:13:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626761616; cv=none; d=google.com; s=arc-20160816; b=auhQx/CFkGXv+BMKWGllLQ3/W7lfobPv1uT7RrweWdVfWtZud7C0FvOWAAF2fbjB85 H1S6+VcphP9uIvbs1+giDxQ6l7/Zldhy7wvb+8UYyI0aKoEBLBFiwLc0pDbLrGZE2w2N s1zbnUpg9j3DLdb3PZCC/FXlHMdz3f/3ixAA24XbH1s6+pbE2fDDH9lboasWrJBNfS/a HCEq8DtzaJLz3nvs850PAk4GbNlJgQ8ja02nx2Gvf8obL9FrnKpBYd/173jWDWk2eYWA F9M9KJKiLJeau8qKEzslXGMpCLUgpzWbLlbvi3aFJp+1xc1qW87KdEbKKLZBtwdAlmAG fs4g== 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:to:from; bh=xPW3uOhpSAtIz8elmnDf4nMTacusSc0StuEgOtdrmYI=; b=jdpVjlzX8FDqpmbSu5UBJBXE9Trwr4p9aq3r5IrbY5lquaSkExJHcn58iaREFBqHrI x3du4E7H3fyBQYA/ooZ4EQ0Hf7TvLKWuPtIu3qoYbl+B5+J4A2urIl7kOVe+B/suEDi3 K/06gnakdkMEycp7E8OLQNVL1vla20LgZxUTlnE+Hg6V0AGNwIygtWIe4Zfrir2Ui/2G vVdzzoQThdHcGdo8JpPR+BLu0BFegRP4yi1ZTnfg1qfSGQCTOAjFBNIHrmhteBGqudaj LiXvjftqnuEYSpFgsYYkF8DVODmzat+F2b+oihYbwIBROZuxRbjAwAe8/VzjPxJBGR8d Mg7A== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h3si14949271ejb.234.2021.07.19.23.13.11; Mon, 19 Jul 2021 23:13:36 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232637AbhGTFaj (ORCPT + 99 others); Tue, 20 Jul 2021 01:30:39 -0400 Received: from mx425.baidu.com ([119.249.100.233]:52617 "EHLO mx423.baidu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230414AbhGTFa1 (ORCPT ); Tue, 20 Jul 2021 01:30:27 -0400 X-Greylist: delayed 382 seconds by postgrey-1.27 at vger.kernel.org; Tue, 20 Jul 2021 01:30:27 EDT Received: from unknown.domain.tld (bjhw-sys-rpm015653cc5.bjhw.baidu.com [10.227.53.39]) by mx423.baidu.com (Postfix) with ESMTP id 5B2C716E01287; Tue, 20 Jul 2021 14:04:41 +0800 (CST) From: Li RongQing 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.co, linux-kernel@vger.kernel.org, songmuchun@bytedance.com Subject: [PATCH][RFC] sched/cpuacct: Fix cpuacct charge Date: Tue, 20 Jul 2021 14:04:41 +0800 Message-Id: <20210720060441.22550-1-lirongqing@baidu.com> X-Mailer: git-send-email 2.9.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org get_irq_regs only work for current running cpu, but the task, whose cpuacct will be charged, maybe run different cpu, like Cpu 2 wake up a kernel thread to CPU 3, cause CPU 3 task are charged with the following stack cpuacct_charge+0xd8/0x100 update_curr+0xe1/0x1e0 enqueue_entity+0x144/0x6e0 enqueue_task_fair+0x93/0x900 ttwu_do_activate+0x4b/0x90 try_to_wake_up+0x20b/0x530 ? update_dl_rq_load_avg+0x10f/0x210 swake_up_locked.part.1+0x13/0x40 swake_up_one+0x27/0x40 rcu_process_callbacks+0x484/0x4f0 ? run_rebalance_domains_bt+0x5a/0x180 __do_softirq+0xe3/0x308 irq_exit+0xf0/0x100 smp_apic_timer_interrupt+0x74/0x160 apic_timer_interrupt+0xf/0x20 RIP: 0033:0x456947 so define a get_irq_regs_cpu which returns the required cpu irq registers BUT it should be not safe, and do not know what it should be like in MIPS? Fixes: dbe9337109c2 "(sched/cpuacct: Fix charge cpuacct.usage_sys)" Co-developed-by: Zhao Jie Signed-off-by: Zhao Jie Signed-off-by: Li RongQing --- include/asm-generic/irq_regs.h | 5 +++++ kernel/sched/cpuacct.c | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/asm-generic/irq_regs.h b/include/asm-generic/irq_regs.h index 2e7c6e8..93e2579 100644 --- a/include/asm-generic/irq_regs.h +++ b/include/asm-generic/irq_regs.h @@ -21,6 +21,11 @@ static inline struct pt_regs *get_irq_regs(void) return __this_cpu_read(__irq_regs); } +static inline struct pt_regs *get_irq_regs_cpu(int cpu) +{ + return per_cpu(__irq_regs, cpu); +} + static inline struct pt_regs *set_irq_regs(struct pt_regs *new_regs) { struct pt_regs *old_regs; diff --git a/kernel/sched/cpuacct.c b/kernel/sched/cpuacct.c index 893eece..8b96058 100644 --- a/kernel/sched/cpuacct.c +++ b/kernel/sched/cpuacct.c @@ -340,7 +340,8 @@ void cpuacct_charge(struct task_struct *tsk, u64 cputime) { struct cpuacct *ca; int index = CPUACCT_STAT_SYSTEM; - struct pt_regs *regs = get_irq_regs() ? : task_pt_regs(tsk); + int cpu = task_cpu(tsk); + struct pt_regs *regs = get_irq_regs_cpu(cpu) ? : task_pt_regs(tsk); if (regs && user_mode(regs)) index = CPUACCT_STAT_USER; -- 2.9.4