From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796148413; b=g9ubuVwGz4iwA7GWPlm5+tTqfgegg/3ywWaCBpjPM8eWMaKHrCeLDT7AFb00wK5fN/SL m7KPRg20DKUF/aeyKsSDAJLs+6UDM/CcyGUXyzSY0Z+d70bnKZu+XhtI7JH6aoNj8lxSR ODPo/1+HMVMSZXOxoyybvDnw/xeItfrhBhD/MFMzsFuTcToD/0B2hzcYzoUNK2zdRli4H 5U07bkzzACVSFk1NPAr27MJ+yON/DeotpS0diM1x2JN0tFruSsh43oranb2oxhtJ6BH3V KzSPLeiWIdaZUQ1wDnevRKu1YI9FXSDXzkpRFCnNVjwdmHXfeMyboqscy9/RS9kf2CQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796148413; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=UvRQQoe13OTzWbIUBS5N/T6Vdg/Mc9ggBt+KmYjAGjs=; b=ghQ2P2iCXpZ9RsjNpvCVvP8bOvvClWqzrHKfoMMjrDyO53WnMYBXSG3U1OhTZ2VWMO+I ADp8LXJwgl5jz4MkwEdyallwBtP4gmkmDtQxHZ43o/M4v28FbSEaWr7ZSjT/bX+OqabuH B3k+U+diKp99k223QGmPCu9SpMsDZWU5RfXeDudvvJd25P5eqQCJjCfcnBMK0nxt53r7o WcRomPWy4lh2iPS7HCbbl++5bFtgaA5av3xPXEqfFFpd4eQA2cX8OTQb+TsIGa/O8IoXD a7PmXORp3M1ggnv2SqoXfuVQzttRt5pYKreIt1yCluUBXIn3kIsy5Btkq+RHJNs5Edw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=vDZ1uumS; arc=none smtp.remote-ip=74.125.82.182 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49E16606A31A for ; Tue, 1 Dec 2026 18:06:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 49E16606A31A 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.a=rsa-sha256 header.s=20260707 header.b=vDZ1uumS Received: by mail-dy1-f182.google.com with SMTP id 5a478bee46e88-38644aedba1so20375650eec.0 for ; Tue, 01 Dec 2026 10:06:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796148412; x=1796753212; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=UvRQQoe13OTzWbIUBS5N/T6Vdg/Mc9ggBt+KmYjAGjs=; b=vDZ1uumSgJmF6BNTYcCzuoVRvNQOThNwleKpUYjTQXt7oFzG2Wo35mtpfbYfKdLylD 2KchAkb7RDfGzOKuiMjZN03l9+dF+qV1TTCBnZysIJMvXafkf9FIIsmvJ8ZJmKi1A+uc 4XS0wH7yS5oRPoeEXicy5nr6Z+N18f/ULRxJw3fMUj9wguZpl836CIx7oVbYw+icMgEv l3v9/Rl4ja3jfbQUBfD9M25SqSdA9s0Id3zgN59j9zK6ywBPgLgT45UZxGx43jtWCL01 E0bboaAptZ6vKeCY28wdKHJsh9jmt2wWvDq4TtqgZKDk2uCeFdjcyhBstnZj0s1AIo8e rZTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796148412; x=1796753212; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=UvRQQoe13OTzWbIUBS5N/T6Vdg/Mc9ggBt+KmYjAGjs=; b=FfgWTIx0XIU2cQx8c+3HCTddOy+rV9lFq4ZwcpB1KN6MHy+2D+I6Qktr1H+MgQ8SiG rFktg5FtY16VhUkoD6kjL/R3IyRdmvkxPN4uXi5DbVeCbNx/Ed9nz7tOdzoyNlXqdIkI ii+KshfEb3rWD9HaIw2Bw71G0zZnl4x3QtyUMmAJBFjxHYZSeTv+nqgPNHH0CI4sC+nx n5iPQ8uztlyzasqRnnSQE8Fb6+QTdbRrF/S9Er3AXTN6zT23GIH4G2wGw5qGV5DaBJkf zkUFBht0s8Jv3+crw81iL8RaQgtgLuxXXYAMxiFLvDNq1U9cpPbqppSBW9/MG+zme/MX bNtA== X-Forwarded-Encrypted: i=1; AL+hErFu39+lV+2oFbkhHRHuJ2NXOH+gWb8y6lTFKTXrenoU6gkbuC8w+zaTrpNHR6cSWuCzxAV//0JQHHZ/gGE=@vger.kernel.org X-Gm-Message-State: APuU4Z8g1835O3Fchib3Ju5NG6ra4xxNFcjcB+Y03nFdiR1AlgIB1are avHSfOdX2Rn2ekoINFZ6hvwcCB9DvXJKsds0ojVCkenhRxzFbMzsdokE X-Gm-Gg: AZFdTZdOtKx46lCagZzY/z6mhJ3Mo1Q/QEDop1MVyp6v7cd/43cP6izn2koJC6vK3MD a8SjGrSI6+qUxRUmQelvVIzZeI8pAW4DZtVxNJZFqSGPMQXhiQglGN1RQcv8Zhs/yJNoKmFthGM JEV0kwP+c1U2x3RTrUDXlM9CF1QeQZPXXcYf2km0Z5rN+EoRdB6YgNE4qwXaNgUn1ISIZcy4QGC pCVOqaovMYqfP+sjCTcPEwfAi/69VrR8fYYcMVEaGMMMn2TrfSnfz+wfFhdAxbHOKh1iXxWtwJZ a4uDhuhbGBStTQF++uE7aJmuzkDIHUJmM08n7rNZvotH3+bCEvaDsNMqfpGF9DuqmoFuepCSS0Y oDsMXEoBJLWxZyVrGjxWZox0dZPTI7gQml0xOVkAY3pXF65fGrNqYyNRbAttDqGmY5B0El6tBcM sX1KQgfjoyU/KMiuk1Mo5KjcTYl6BaPmbuX1CzFQUMuoI8Z+zmLOWkqzJzjm+NqBhbZNhPUf3Pk qUfAF4B2aOXsSJasTco8hhlSDt+K3wuEyx/7wMF0o8fKREv5eMJXX2V1tEljY11vQREQwD67oTW Ftvey921BZoAbqcjmVSot8REGzFQMeI= X-Received: by 2002:a05:7301:4718:b0:369:1401:c5c2 with SMTP id 5a478bee46e88-3857025806bmr38320049eec.26.1796148412146; Tue, 01 Dec 2026 10:06:52 -0800 (PST) Received: from ubuntu24.lan ([2a10:8744:8aa6:f72a:73a9:555f:b311:3e8]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-38566cd46c5sm139810520eec.11.2026.12.01.10.06.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 10:06:51 -0800 (PST) From: Yiyang Chen To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Frederic Weisbecker , Thomas Gleixner Cc: Yiyang Chen , Dietmar Eggemann , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev Subject: [PATCH] sched/cputime: Account for softirq preemption on PREEMPT_RT Date: Wed, 2 Dec 2026 02:06:42 +0800 Message-ID: <61e7928eb0fd6d946528fd3d067e6a0446fd2b78.1796148059.git.cyyzero16@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit IRQ time accounting uses a per-CPU timestamp and assumes that a task serving a softirq cannot be scheduled out. On PREEMPT_RT, softirq execution is preemptible, so another task can run on the CPU without an intervening irqtime update. If the softirq task resumes without an intervening hardirq or softirq, time spent in the other task is charged to CPUTIME_SOFTIRQ. Conversely, if a hardirq arrives while the other task runs, IRQ entry sees that the interrupted task is not serving a softirq and advances the timestamp without charging CPUTIME_SOFTIRQ, losing the prior softirq time. Account the elapsed softirq time on entry to __schedule(), before update_rq_clock() reads the IRQ time total. Advance the timestamp before local interrupts are re-enabled when resuming a task serving a softirq. Do this also when the scheduler picks the same task, so that time spent selecting it is excluded from softirq time. Fixes: 8b1c04acad08 ("softirq: Make softirq control and processing RT aware") Signed-off-by: Yiyang Chen --- include/linux/vtime.h | 17 +++++++++++++++++ kernel/sched/core.c | 3 +++ 2 files changed, 20 insertions(+) diff --git a/include/linux/vtime.h b/include/linux/vtime.h index 82825e775499..d8fe56bc9af6 100644 --- a/include/linux/vtime.h +++ b/include/linux/vtime.h @@ -176,4 +176,21 @@ static inline void account_hardirq_exit(struct task_struct *tsk) irqtime_account_irq(tsk, 0); } +#ifdef CONFIG_PREEMPT_RT +static inline void account_softirq_sched_out(void) +{ + if (unlikely(in_serving_softirq())) + account_softirq_exit(current); +} + +static inline void account_softirq_sched_in(void) +{ + if (unlikely(in_serving_softirq())) + account_softirq_enter(current); +} +#else +static inline void account_softirq_sched_out(void) { } +static inline void account_softirq_sched_in(void) { } +#endif + #endif /* _LINUX_KERNEL_VTIME_H */ diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 5ed660f099a5..13cf84e7a83d 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -5430,6 +5430,7 @@ static struct rq *finish_task_switch(struct task_struct *prev) perf_event_task_sched_in(prev, current); finish_task(prev); tick_nohz_task_switch(); + account_softirq_sched_in(); finish_lock_switch(rq); finish_arch_post_lock_switch(); kcov_finish_switch(current); @@ -7235,6 +7236,7 @@ static void __sched notrace __schedule(int sched_mode) klp_sched_try_switch(prev); local_irq_disable(); + account_softirq_sched_out(); rcu_note_context_switch(preempt); migrate_disable_switch(rq, prev); @@ -7386,6 +7388,7 @@ static void __sched notrace __schedule(int sched_mode) rq = context_switch(rq, prev, next, &rf); } else { rq_unpin_lock(rq, &rf); + account_softirq_sched_in(); __balance_callbacks(rq, NULL); hrtick_schedule_exit(rq); raw_spin_rq_unlock_irq(rq); -- 2.43.0