From: Luka Bai <lukafocus@icloud.com>
To: linux-mm@kvack.org
Cc: "Johannes Weiner" <hannes@cmpxchg.org>,
"Suren Baghdasaryan" <surenb@google.com>,
"Peter Ziljstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>,
"Juri Lelli" <juri.lelli@redhat.com>,
"Vincent Guittot" <vincent.guittot@linaro.org>,
"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Ben Segall" <bsegall@google.com>, "Mel Gorman" <mgorman@suse.de>,
"Valentin Schneider" <vschneid@redhat.com>,
"K Prateek Nayak" <kprateek.nayak@amd.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"David Hildenbrand" <david@kernel.org>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Mike Rapoport" <rppt@kernel.org>,
"Michal Hocko" <mhocko@suse.com>, "Kees Cook" <kees@kernel.org>,
"Tejun Heo" <tj@kernel.org>, "Michal Koutný" <mkoutny@suse.com>,
linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
"Luka Bai" <lukabai@tencent.com>
Subject: [PATCH 1/6] psi: move curr_in_memstall out of psi_group_change
Date: Tue, 12 May 2026 14:19:57 +0800 [thread overview]
Message-ID: <20260512-psi_impr-v1-1-2b7f10fdfad5@tencent.com> (raw)
In-Reply-To: <20260512-psi_impr-v1-0-2b7f10fdfad5@tencent.com>
From: Luka Bai <lukabai@tencent.com>
Variable curr_in_memstall is currently judged by accessing the
in_memstall of cpu_curr(cpu), which contains multiple times of
memory accessing. And it is now located in psi_group_change()
that will be called for each parent cgroup and it is redundant
sometimes since its value will not change for all these parent
cgroups.
So we move the variable outside for two reasons:
1. We save the extra calling for each parent cgroup so we avoid
these possible uncessary cacheline stall.
2. For function like psi_task_switch, we don't need to call the
cpu_curr(cpu) to get the task that is currently running in
the cpu runqueue. Under that context, "next" is absolutely the
running task so we can save some costly calling.
Signed-off-by: Luka Bai <lukabai@tencent.com>
---
kernel/sched/psi.c | 23 ++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)
diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index d9c9d9480a45..27097cb0dc79 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -795,7 +795,7 @@ static void record_times(struct psi_group_cpu *groupc, u64 now)
static void psi_group_change(struct psi_group *group, int cpu,
unsigned int clear, unsigned int set,
- u64 now, bool wake_clock)
+ u64 now, bool wake_clock, bool curr_in_memstall)
{
struct psi_group_cpu *groupc;
unsigned int t, m;
@@ -868,7 +868,7 @@ static void psi_group_change(struct psi_group *group, int cpu,
* task in a cgroup is in_memstall, the corresponding groupc
* on that cpu is in PSI_MEM_FULL state.
*/
- if (unlikely((state_mask & PSI_ONCPU) && cpu_curr(cpu)->in_memstall))
+ if (unlikely((state_mask & PSI_ONCPU) && curr_in_memstall))
state_mask |= (1 << PSI_MEM_FULL);
record_times(groupc, now);
@@ -910,6 +910,7 @@ void psi_task_change(struct task_struct *task, int clear, int set)
{
int cpu = task_cpu(task);
u64 now;
+ bool curr_in_memstall;
if (!task->pid)
return;
@@ -917,9 +918,11 @@ void psi_task_change(struct task_struct *task, int clear, int set)
psi_flags_change(task, clear, set);
psi_write_begin(cpu);
+ curr_in_memstall = cpu_curr(cpu)->in_memstall;
now = cpu_clock(cpu);
for_each_group(group, task_psi_group(task))
- psi_group_change(group, cpu, clear, set, now, true);
+ psi_group_change(group, cpu, clear, set, now, true,
+ curr_in_memstall);
psi_write_end(cpu);
}
@@ -929,11 +932,13 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
struct psi_group *common = NULL;
int cpu = task_cpu(prev);
u64 now;
+ bool curr_in_memstall = false;
psi_write_begin(cpu);
now = cpu_clock(cpu);
if (next->pid) {
+ curr_in_memstall = next->in_memstall;
psi_flags_change(next, 0, TSK_ONCPU);
/*
* Set TSK_ONCPU on @next's cgroups. If @next shares any
@@ -947,7 +952,8 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
common = group;
break;
}
- psi_group_change(group, cpu, 0, TSK_ONCPU, now, true);
+ psi_group_change(group, cpu, 0, TSK_ONCPU, now, true,
+ curr_in_memstall);
}
}
@@ -984,7 +990,8 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
for_each_group(group, task_psi_group(prev)) {
if (group == common)
break;
- psi_group_change(group, cpu, clear, set, now, wake_clock);
+ psi_group_change(group, cpu, clear, set, now, wake_clock,
+ curr_in_memstall);
}
/*
@@ -996,7 +1003,8 @@ void psi_task_switch(struct task_struct *prev, struct task_struct *next,
if ((prev->psi_flags ^ next->psi_flags) & ~TSK_ONCPU) {
clear &= ~TSK_ONCPU;
for_each_group(group, common)
- psi_group_change(group, cpu, clear, set, now, wake_clock);
+ psi_group_change(group, cpu, clear, set, now, wake_clock,
+ curr_in_memstall);
}
}
psi_write_end(cpu);
@@ -1236,7 +1244,8 @@ void psi_cgroup_restart(struct psi_group *group)
psi_write_begin(cpu);
now = cpu_clock(cpu);
- psi_group_change(group, cpu, 0, 0, now, true);
+ psi_group_change(group, cpu, 0, 0, now, true,
+ cpu_curr(cpu)->in_memstall);
psi_write_end(cpu);
}
}
--
2.52.0
next prev parent reply other threads:[~2026-05-12 6:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-12 6:19 [PATCH 0/6] psi: slightly improve performance of psi Luka Bai
2026-05-12 6:19 ` Luka Bai [this message]
2026-05-12 6:19 ` [PATCH 2/6] psi: reorganize the psi members for cacheline benifits Luka Bai
2026-05-12 6:19 ` [PATCH 3/6] psi: use prefetch to preread the parent groupc Luka Bai
2026-05-12 6:20 ` [PATCH 4/6] psi: do not call record_times when the state is not changed Luka Bai
2026-05-12 6:20 ` [PATCH 5/6] psi: add psi group for the root cgroup Luka Bai
2026-05-12 6:20 ` [PATCH 6/6] psi: remove psi_bug and moves checking of NR_RUNNING ahead Luka Bai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260512-psi_impr-v1-1-2b7f10fdfad5@tencent.com \
--to=lukafocus@icloud.com \
--cc=akpm@linux-foundation.org \
--cc=bsegall@google.com \
--cc=cgroups@vger.kernel.org \
--cc=david@kernel.org \
--cc=dietmar.eggemann@arm.com \
--cc=hannes@cmpxchg.org \
--cc=juri.lelli@redhat.com \
--cc=kees@kernel.org \
--cc=kprateek.nayak@amd.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=lukabai@tencent.com \
--cc=mgorman@suse.de \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=mkoutny@suse.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=surenb@google.com \
--cc=tj@kernel.org \
--cc=vbabka@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox