mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hui Su <sh_def@163.com>
To: Ingo Molnar <mingo@redhat.com>, Peter Zijlstra <peterz@infradead.org>
Cc: 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>,
	Mike Galbraith <efault@gmx.de>,
	linux-kernel@vger.kernel.org, Hui Su <sh_def@163.com>
Subject: [PATCH v2] sched/autogroup: Make nice rate-limit updates atomic
Date: Mon, 12 Oct 2026 22:31:55 +0900	[thread overview]
Message-ID: <20261012133155.86480-1-sh_def@163.com> (raw)

proc_sched_autogroup_set_nice() uses a function-static timestamp to
rate-limit the expensive sched_group_set_shares() operation.

The timestamp check and update are not atomic. Concurrent unprivileged
writers can therefore observe the same expired timestamp and proceed to
sched_group_set_shares(), weakening the intended throttling of this
expensive path.

Use try_cmpxchg() so that only one unprivileged caller can claim an
expired rate-limit window. A competing unprivileged caller which loses
the race returns -EAGAIN. Use xchg() for privileged callers so that
they retain their existing rate-limit bypass behavior while updating
the timestamp atomically.

Fixes: 5091faa449ee ("sched: Add 'autogroup' scheduling feature: automated per session task groups")
Suggested-by: K Prateek Nayak <kprateek.nayak@amd.com>
Signed-off-by: Hui Su <sh_def@163.com>
---
Testing:
- x86_64 KVM functional rate-limit oracle admitted one of eight
  synchronized unprivileged writers and returned -EAGAIN to the other
  seven.
- x86_64 KVM privileged and mixed-contention tests completed all
  CAP_SYS_ADMIN writes without -EAGAIN or errors; competing
  unprivileged writers were rate-limited as expected.
- x86_64 KVM KCSAN forced-overlap test admitted one writer and returned
  -EAGAIN to the other, with no proc_sched_autogroup_set_nice report.

Changes in v2:
- Replace the dedicated spinlock with try_cmpxchg() for unprivileged
  callers, as suggested by Prateek.
- Use xchg() for CAP_SYS_ADMIN callers to preserve their existing
  rate-limit bypass behavior.

v1: https://lore.kernel.org/lkml/20261008064440.240528-1-sh_def@163.com/
---
 kernel/sched/autogroup.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/kernel/sched/autogroup.c b/kernel/sched/autogroup.c
index e380cf9372bb..76fa3f287208 100644
--- a/kernel/sched/autogroup.c
+++ b/kernel/sched/autogroup.c
@@ -234,7 +234,7 @@ int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
 {
 	static unsigned long next = INITIAL_JIFFIES;
 	struct autogroup *ag;
-	unsigned long shares;
+	unsigned long shares, old;
 	int err, idx;
 
 	if (nice < MIN_NICE || nice > MAX_NICE)
@@ -247,11 +247,15 @@ int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
 	if (nice < 0 && !can_nice(current, nice))
 		return -EPERM;
 
-	/* This is a heavy operation, taking global locks.. */
-	if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
-		return -EAGAIN;
+	/* Rate-limit this heavy operation, which takes global locks. */
+	if (capable(CAP_SYS_ADMIN)) {
+		xchg(&next, HZ / 10 + jiffies);
+	} else {
+		old = READ_ONCE(next);
+		if (time_before(jiffies, old) || !try_cmpxchg(&next, &old, HZ / 10 + jiffies))
+			return -EAGAIN;
+	}
 
-	next = HZ / 10 + jiffies;
 	ag = autogroup_task_get(p);
 
 	idx = array_index_nospec(nice + 20, 40);

base-commit: 6f8319e3e9a44dd537d17f41565a8453c560a581


                 reply	other threads:[~2026-10-12 13:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261012133155.86480-1-sh_def@163.com \
    --to=sh_def@163.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=efault@gmx.de \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.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

all inboxes | Powered by JetHome®