[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20250731121506.6423-1-lirongqing@baidu.com>
Date: Thu, 31 Jul 2025 20:15:06 +0800
From: lirongqing <lirongqing@...du.com>
To: <mingo@...hat.com>, <peterz@...radead.org>, <juri.lelli@...hat.com>,
<vincent.guittot@...aro.org>, <dietmar.eggemann@....com>,
<rostedt@...dmis.org>, <bsegall@...gle.com>, <mgorman@...e.de>,
<vschneid@...hat.com>, <linux-kernel@...r.kernel.org>
CC: Li RongQing <lirongqing@...du.com>
Subject: [PATCH] sched/cputime: Simplify seq retry logic in thread_group_cputime()
From: Li RongQing <lirongqing@...du.com>
The new logic maintains equivalent functionality but streamlines the code:
1. First iteration: seq becomes 2 (even -> lockless path)
2. Contended iterations: seq becomes odd -> locked path
Eliminates redundant state tracking variables
Signed-off-by: Li RongQing <lirongqing@...du.com>
---
kernel/sched/cputime.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index 7097de2..7eeff8d 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -315,7 +315,7 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
struct signal_struct *sig = tsk->signal;
u64 utime, stime;
struct task_struct *t;
- unsigned int seq, nextseq;
+ unsigned int seq = 1;
unsigned long flags;
/*
@@ -330,10 +330,8 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
(void) task_sched_runtime(current);
rcu_read_lock();
- /* Attempt a lockless read on the first round. */
- nextseq = 0;
do {
- seq = nextseq;
+ seq++; /* 2 on the 1st/lockless path, otherwise odd */
flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
times->utime = sig->utime;
times->stime = sig->stime;
@@ -345,8 +343,6 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
times->stime += stime;
times->sum_exec_runtime += read_sum_exec_runtime(t);
}
- /* If lockless access failed, take the lock. */
- nextseq = 1;
} while (need_seqretry(&sig->stats_lock, seq));
done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
rcu_read_unlock();
--
2.9.4
Powered by blists - more mailing lists