From nobody Sun Oct 5 18:21:48 2025 Received: from baidu.com (mx22.baidu.com [220.181.50.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44110C120 for ; Thu, 31 Jul 2025 12:15:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.181.50.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753964156; cv=none; b=brZysY4EGnwU1nFWFCmdmIcqtGWkBlTKOp7DUMJGD6zTE/sh8nxA+qPw/GKS44vE0qRGXi/ljFsLBXryULDbK7stu0dvc4g0B9LgueghBi+MLE/K4MmqyJE9hVVyYtzmTn1FVsfJ3q65wh4IiuVH1qYucd7pW2zhycGgxrpkrFk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753964156; c=relaxed/simple; bh=/AlRXjyWbfEosjXA+W7RlO/tBXADFQ5LZY68630rcVs=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=TmcQNnEji/UJu+F8ALCJHtNL3NB8JzXQGHcPEARe6zBECP3pRGtukvwV0z39fTojLYtyAV1oKMSMIzGvnxD5ygsaeq18V9jSwn0HYWiUXYprp+ucikNET37hfABvGgHLhzrbwpuAu7X3oVLryUR4PhiFn0lgjtaAAuM16mj5X3c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=baidu.com; spf=pass smtp.mailfrom=baidu.com; arc=none smtp.client-ip=220.181.50.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=baidu.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baidu.com From: lirongqing To: , , , , , , , , , CC: Li RongQing Subject: [PATCH] sched/cputime: Simplify seq retry logic in thread_group_cputime() Date: Thu, 31 Jul 2025 20:15:06 +0800 Message-ID: <20250731121506.6423-1-lirongqing@baidu.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: bjhj-exc8.internal.baidu.com (172.31.3.18) To bjkjy-exc3.internal.baidu.com (172.31.50.47) X-FEAS-Client-IP: 172.31.50.47 X-FE-Policy-ID: 52:10:53:SYSTEM Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Li RongQing 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 --- 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, stru= ct task_cputime *times) struct signal_struct *sig =3D tsk->signal; u64 utime, stime; struct task_struct *t; - unsigned int seq, nextseq; + unsigned int seq =3D 1; unsigned long flags; =20 /* @@ -330,10 +330,8 @@ void thread_group_cputime(struct task_struct *tsk, str= uct task_cputime *times) (void) task_sched_runtime(current); =20 rcu_read_lock(); - /* Attempt a lockless read on the first round. */ - nextseq =3D 0; do { - seq =3D nextseq; + seq++; /* 2 on the 1st/lockless path, otherwise odd */ flags =3D read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq); times->utime =3D sig->utime; times->stime =3D sig->stime; @@ -345,8 +343,6 @@ void thread_group_cputime(struct task_struct *tsk, stru= ct task_cputime *times) times->stime +=3D stime; times->sum_exec_runtime +=3D read_sum_exec_runtime(t); } - /* If lockless access failed, take the lock. */ - nextseq =3D 1; } while (need_seqretry(&sig->stats_lock, seq)); done_seqretry_irqrestore(&sig->stats_lock, seq, flags); rcu_read_unlock(); --=20 2.9.4