From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97E813DD520 for ; Mon, 13 Apr 2026 15:46:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776095161; cv=none; b=n08W/o85dO0dclDnXPKjzUp0b783y1i68XqeSFqS3JQSdmIEzjMJdhrXIQcvPWDEahpbFCGz27LZCpxxtEKhv15+BDjPZxc4q2R/Br3A//CEUBKswFu1p/zTSzfrnL8XnJccFTvd98rP+qTlW0TJ9Y506V4jKKqKsm9TYuvAwYc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776095161; c=relaxed/simple; bh=4CemZu+suVUtzFuZOxj3sutsvIitsmDpqggC93wWMTw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XtUY23wGuXipgNzMun8BjU9Fct4aCJVgDGqjKB1U90XffXYlaajgWGd8iyMD7XLEt8730WySNcG0PmEFr52F8SJ0guqg9PjZcxAYyJGCLXaNMonYKzMYIIk6tfb/rNAsRgjLWtY5KBFfi0YirZL1zjGnwn7Chg+GMt+f3yuqjDg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jOLZKnZ6; arc=none smtp.client-ip=209.85.215.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jOLZKnZ6" Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-c70e27e2b74so1693671a12.0 for ; Mon, 13 Apr 2026 08:46:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776095160; x=1776699960; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+JusnDR0Yh9TDaRsWoQKtZaA9CPLjEJexzqPRXw9RNc=; b=jOLZKnZ6lAysEHloeIMR0dvx5BrUykpwrigoAoKIcSwehxZPhir9Z8loaQHzDOKDH6 pBr5S+GJQJRbz1rGjio4HLogRjxdHCgBK42YdCG9nrbAc+5yLno0NATxSTOhVpvc57vZ hZv/lxhYbBF4P8LV2WdRj+625g1LsQqILQ5DvJ4Je4oaPTq7S9rZBM/OUpiWvW8HN+Vp s23HBtaZ9Jqo2UHtArjjaKBh2FugIXWOqwx3xN9kdw5vrRM00Cw1imYolNVL+lBW1/NF wvyzH5GwEppwMR5UGZiSgsW5M6cC0b+S/x+Zg+DVIO+GikBlaYVmHnM8GLb3gsHHJ4bS JHSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776095160; x=1776699960; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=+JusnDR0Yh9TDaRsWoQKtZaA9CPLjEJexzqPRXw9RNc=; b=O+ImWVhStyD+oGb+SMD8LPHQ+XHlWOCIlwbl86nyn6dRXBHDBEeJv9VDplRTid9RH5 zgibWirJaJNXXBDZAYFlnEqXIztkUlf1rULcpJ+T2phvBv12wHRv1uaFMdcTkxWpIaym MeEwIuIVYZEKP0xHtcowihalNcqVzYPzjciHG3goaXpUtAUOL0gIvMVBrUK7vtGyy9hX Fl83VX5asP72IlJXfHu8o1b/wiPALu/MvxXrEIOc1o95szFB2YZKq/KyApeb+DgSqxjQ 1Y5nB6o8KAO3DEI07wQwTEJJoSufihe/Ci5CPPdtvjX2J/TmfzKFLVkc1qzkIHH4wQgI 411w== X-Gm-Message-State: AOJu0YzWS+3HzxEKzlLalhgPpdbLn5703EXa3lCQ210zd3kBkW6ukeQz 5g71JJmbBiIHt6BeF0t/u1YG+xr5M18iUTLlKfGR1BQnf1PJJv/NgGnr X-Gm-Gg: AeBDieuhmGXDojMmYTMsDkOruOB4Y+qg7p4/6a7nnMP7fIKefPXhnBIkG09T5w/PJRs WzhDQN1ebScBt7Z532bFb69FU6MamNlZdbjkrRBRlKZt3vINMK5rQXIMjeV3sNtMT2fabfNs28k S8PjsIBFUUEo7RfV+pOxYlGXofj+7f7ey1hkez0x16oSbjwyrEcVYXKuGRWWalEIC1Ir+7hhfH/ f2yGdG7fzJ0faDeBtCb05fnggTFF8CbUnFvVwquCWJ4d6/QaJhML3f2gDL66oheE8NzsicZOd6B WTR99BpLE1KsqW2wZxivrWOu/cl8Qwws/6AgrOgi6OHU4/DcQY41+A92hQ4hYsYG3hMQJt0CQD6 CTpSkLZ4u4CReVAYWf3QnT57nnSwKNRiECGPSIw1rnCGyaHX769lJBsF/zKxeGgRVNsljNqJepo JutABnfmpTSiqt8N1BF//AnA== X-Received: by 2002:a05:6a20:7486:b0:398:b346:b13 with SMTP id adf61e73a8af0-39fe3d0dfafmr15810712637.16.1776095159705; Mon, 13 Apr 2026 08:45:59 -0700 (PDT) Received: from ubuntu24.lan ([2602:ffe4:1:2113:9dfd:1ff:3726:3839]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c793488d824sm6318233a12.16.2026.04.13.08.45.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Apr 2026 08:45:59 -0700 (PDT) From: Yiyang Chen To: Balbir Singh , Yang Yang , Wang Yaxin Cc: linux-kernel@vger.kernel.org, Oleg Nesterov , "Dr . Thomas Orgis" , Andrew Morton , stable@vger.kernel.org, Yiyang Chen Subject: [PATCH v3 1/2] taskstats: retain dead thread stats in TGID queries Date: Mon, 13 Apr 2026 23:45:44 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit fill_stats_for_tgid() builds TGID stats from two sources: the cached aggregate in signal->stats and a scan of the live threads in the group. However, fill_tgid_exit() only accumulates delay accounting into signal->stats. This means that once a thread exits, TGID queries lose the fields that fill_stats_for_tgid() adds for live threads. This gap was introduced incrementally by two earlier changes that extended fill_stats_for_tgid() but did not make the corresponding update to fill_tgid_exit(): - commit 8c733420bdd5 ("taskstats: add e/u/stime for TGID command") added ac_etime, ac_utime, and ac_stime to the TGID query path. - commit b663a79c1915 ("taskstats: add context-switch counters") added nvcsw and nivcsw to the TGID query path. As a result, those fields were accounted for live threads in TGID queries, but were dropped from the cached TGID aggregate after thread exit. The final TGID exit notification emitted when group_dead is true also copies that cached aggregate, so it loses the same fields. Factor the per-task TGID accumulation into tgid_stats_add_task() and use it in both fill_stats_for_tgid() and fill_tgid_exit(). This keeps the cached aggregate used for dead threads aligned with the live-thread accumulation used by TGID queries. Fixes: 8c733420bdd5 ("taskstats: add e/u/stime for TGID command") Fixes: b663a79c1915 ("taskstats: add context-switch counters") Cc: stable@vger.kernel.org Acked-by: Balbir Singh Signed-off-by: Yiyang Chen diff --git a/kernel/taskstats.c b/kernel/taskstats.c index 0cd680ccc7e5..f572f27a5828 100644 --- a/kernel/taskstats.c +++ b/kernel/taskstats.c @@ -210,13 +210,39 @@ static int fill_stats_for_pid(pid_t pid, struct taskstats *stats) return 0; } +static void tgid_stats_add_task(struct taskstats *stats, + struct task_struct *tsk, u64 now_ns) +{ + u64 delta, utime, stime; + + /* + * Each accounting subsystem calls its functions here to + * accumulate its per-task stats for tsk, into the per-tgid structure + * + * per-task-foo(stats, tsk); + */ + delayacct_add_tsk(stats, tsk); + + /* calculate task elapsed time in nsec */ + delta = now_ns - tsk->start_time; + /* Convert to micro seconds */ + do_div(delta, NSEC_PER_USEC); + stats->ac_etime += delta; + + task_cputime(tsk, &utime, &stime); + stats->ac_utime += div_u64(utime, NSEC_PER_USEC); + stats->ac_stime += div_u64(stime, NSEC_PER_USEC); + + stats->nvcsw += tsk->nvcsw; + stats->nivcsw += tsk->nivcsw; +} + static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) { struct task_struct *tsk, *first; unsigned long flags; int rc = -ESRCH; - u64 delta, utime, stime; - u64 start_time; + u64 now_ns; /* * Add additional stats from live tasks except zombie thread group @@ -233,30 +259,12 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) else memset(stats, 0, sizeof(*stats)); - start_time = ktime_get_ns(); + now_ns = ktime_get_ns(); for_each_thread(first, tsk) { if (tsk->exit_state) continue; - /* - * Accounting subsystem can call its functions here to - * fill in relevant parts of struct taskstsats as follows - * - * per-task-foo(stats, tsk); - */ - delayacct_add_tsk(stats, tsk); - - /* calculate task elapsed time in nsec */ - delta = start_time - tsk->start_time; - /* Convert to micro seconds */ - do_div(delta, NSEC_PER_USEC); - stats->ac_etime += delta; - task_cputime(tsk, &utime, &stime); - stats->ac_utime += div_u64(utime, NSEC_PER_USEC); - stats->ac_stime += div_u64(stime, NSEC_PER_USEC); - - stats->nvcsw += tsk->nvcsw; - stats->nivcsw += tsk->nivcsw; + tgid_stats_add_task(stats, tsk, now_ns); } unlock_task_sighand(first, &flags); @@ -275,18 +283,14 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) static void fill_tgid_exit(struct task_struct *tsk) { unsigned long flags; + u64 now_ns; spin_lock_irqsave(&tsk->sighand->siglock, flags); if (!tsk->signal->stats) goto ret; - /* - * Each accounting subsystem calls its functions here to - * accumalate its per-task stats for tsk, into the per-tgid structure - * - * per-task-foo(tsk->signal->stats, tsk); - */ - delayacct_add_tsk(tsk->signal->stats, tsk); + now_ns = ktime_get_ns(); + tgid_stats_add_task(tsk->signal->stats, tsk, now_ns); ret: spin_unlock_irqrestore(&tsk->sighand->siglock, flags); return; -- 2.43.0