Commit 5dd74441 authored by Guopeng Zhang's avatar Guopeng Zhang Committed by Tejun Heo
Browse files

cgroup/cpuset: Reserve DL bandwidth only for root-domain moves



cpuset_can_attach() currently adds the bandwidth of all migrating
SCHED_DEADLINE tasks to sum_migrate_dl_bw. If the source and destination
cpuset effective CPU masks do not overlap, the whole sum is then
reserved in the destination root domain.

set_cpus_allowed_dl(), however, subtracts bandwidth from the source
root domain only when the affinity change really moves the task between
root domains. A DL task can move between cpusets that are still in the
same root domain, so including that task in sum_migrate_dl_bw can reserve
destination bandwidth without a matching source-side subtraction.

Share the root-domain move test with set_cpus_allowed_dl(). Keep
nr_migrate_dl_tasks counting all migrating deadline tasks for cpuset DL
task accounting, but add to sum_migrate_dl_bw only for tasks that need a
root-domain bandwidth move. Keep using the destination cpuset effective
CPU mask and leave the broader can_attach()/attach() transaction model
unchanged.

Fixes: 2ef269ef ("cgroup/cpuset: Free DL BW in case can_attach() fails")
Cc: stable@vger.kernel.org # v6.10+
Signed-off-by: default avatarGuopeng Zhang <zhangguopeng@kylinos.cn>
Reviewed-by: default avatarWaiman Long <longman@redhat.com>
Acked-by: default avatarJuri Lelli <juri.lelli@redhat.com>
Tested-by: default avatarJuri Lelli <juri.lelli@redhat.com>
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
parent 4a39eda5
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -33,6 +33,15 @@ struct root_domain;
extern void dl_add_task_root_domain(struct task_struct *p);
extern void dl_clear_root_domain(struct root_domain *rd);
extern void dl_clear_root_domain_cpu(int cpu);
/*
 * Return whether moving DL task @p to @new_mask requires moving DL
 * bandwidth accounting between root domains. This helper is specific to
 * DL bandwidth move accounting semantics and is shared by
 * cpuset_can_attach() and set_cpus_allowed_dl() so both paths use the
 * same source root-domain test.
 */
extern bool dl_task_needs_bw_move(struct task_struct *p,
				  const struct cpumask *new_mask);

extern u64 dl_cookie;
extern bool dl_bw_visited(int cpu, u64 cookie);
+1 −0
Original line number Diff line number Diff line
@@ -167,6 +167,7 @@ struct cpuset {
	 */
	int nr_deadline_tasks;
	int nr_migrate_dl_tasks;
	/* DL bandwidth that needs destination reservation for this attach. */
	u64 sum_migrate_dl_bw;
	/*
	 * CPU used for temporary DL bandwidth allocation during attach;
+18 −15
Original line number Diff line number Diff line
@@ -2993,7 +2993,7 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
	struct cpuset *cs, *oldcs;
	struct task_struct *task;
	bool setsched_check;
	int ret;
	int cpu, ret;

	/* used later by cpuset_attach() */
	cpuset_attach_old_cs = task_cs(cgroup_taskset_first(tset, &css));
@@ -3038,17 +3038,21 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
		}

		if (dl_task(task)) {
			/*
			 * Count all migrating DL tasks for cpuset task accounting.
			 * Only tasks that need a root-domain bandwidth move
			 * contribute to sum_migrate_dl_bw.
			 */
			cs->nr_migrate_dl_tasks++;
			if (dl_task_needs_bw_move(task, cs->effective_cpus))
				cs->sum_migrate_dl_bw += task->dl.dl_bw;
		}
	}

	if (!cs->nr_migrate_dl_tasks)
	if (!cs->sum_migrate_dl_bw)
		goto out_success;

	if (!cpumask_intersects(oldcs->effective_cpus, cs->effective_cpus)) {
		int cpu = cpumask_any_and(cpu_active_mask, cs->effective_cpus);

	cpu = cpumask_any_and(cpu_active_mask, cs->effective_cpus);
	if (unlikely(cpu >= nr_cpu_ids)) {
		ret = -EINVAL;
		goto out_unlock;
@@ -3059,7 +3063,6 @@ static int cpuset_can_attach(struct cgroup_taskset *tset)
		goto out_unlock;

	cs->dl_bw_cpu = cpu;
	}

out_success:
	/*
+10 −3
Original line number Diff line number Diff line
@@ -3107,20 +3107,18 @@ static void task_woken_dl(struct rq *rq, struct task_struct *p)
static void set_cpus_allowed_dl(struct task_struct *p,
				struct affinity_context *ctx)
{
	struct root_domain *src_rd;
	struct rq *rq;

	WARN_ON_ONCE(!dl_task(p));

	rq = task_rq(p);
	src_rd = rq->rd;
	/*
	 * Migrating a SCHED_DEADLINE task between exclusive
	 * cpusets (different root_domains) entails a bandwidth
	 * update. We already made space for us in the destination
	 * domain (see cpuset_can_attach()).
	 */
	if (!cpumask_intersects(src_rd->span, ctx->new_mask)) {
	if (dl_task_needs_bw_move(p, ctx->new_mask)) {
		struct dl_bw *src_dl_b;

		src_dl_b = dl_bw_of(cpu_of(rq));
@@ -3137,6 +3135,15 @@ static void set_cpus_allowed_dl(struct task_struct *p,
	set_cpus_allowed_common(p, ctx);
}

bool dl_task_needs_bw_move(struct task_struct *p,
			   const struct cpumask *new_mask)
{
	if (!dl_task(p))
		return false;

	return !cpumask_intersects(task_rq(p)->rd->span, new_mask);
}

/* Assumes rq->lock is held */
static void rq_online_dl(struct rq *rq)
{