From nobody Wed Nov 27 00:29:23 2024 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33170189BB8 for ; Tue, 15 Oct 2024 06:22:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728973379; cv=none; b=iOEPWKyJoX1r//lPB5RAUzZjpt9bsHebti8opoo27Ko45hQDv4adfvgm7FDBKdLznvFes34fG5Vlt0A1kb7vMB6iTADQuVR/SQ4G/XgG5cBTHpc/aCk3d8KCuGY7TCzrgOqzhdleeckfZ4/qYp1VV28anZVNxEw5RLplAP2RH9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728973379; c=relaxed/simple; bh=WhJWhAbq0UVdVTbBtMSmvTKOy1yBEbfuRj6REU2MJ5A=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=TJf4e0Qay1N59B0xLPQJnLVl0gTd6zY+X6O/8nXK7UViVkd+x1xx/O+XSdT3ZvnfBfeCKQTlVhb4zIndGRa9EsJvj4B76M0dsVmQTqpG9G92m3/QdhvG+gqTbYUbiywpQyjIfmh9gDAPp+NjyNKqdDoTMJaYUQNN38glf4uVveU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=dJPBkla0; arc=none smtp.client-ip=91.218.175.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="dJPBkla0" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1728973374; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=xV2Yn9Bz9oRhnkXtJemv6T7ztjEHIIDMEfp++wcVRdY=; b=dJPBkla0CzIQt1/oCLTl+pyBwsupuDADTe3AMaNaRjSbkYP42m30f4ofdFd5LRYo9NRlmI AiJiV7ef9EpEWCpe55/OYaVyye+0s06QQXtww2oGxuAR5qko658/weME0PXIKGBiuJnJLz FpJN01awZym+VFUgSI0s7Tvjo4WOxIU= From: Andrea Righi To: Tejun Heo , David Vernet , Peter Zijlstra , Ingo Molnar , Juri Lelli , Vincent Guittot Cc: Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , linux-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH v3] sched_ext: Trigger ops.update_idle() from pick_task_idle() Date: Tue, 15 Oct 2024 08:22:50 +0200 Message-ID: <20241015062250.55350-1-andrea.righi@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" With the consolidation of put_prev_task/set_next_task(), see commit 436f3eed5c69 ("sched: Combine the last put_prev_task() and the first set_next_task()"), we are now skipping the transition between these two functions when the previous and the next tasks are the same. As a result, ops.update_idle() is now called only once when the CPU transitions to the idle class. If the CPU stays active (e.g., through a call to scx_bpf_kick_cpu()), ops.update_idle() will not be triggered again since the task remains unchanged (rq->idle). While this behavior seems generally correct, it can cause issues in certain sched_ext scenarios. For example, a BPF scheduler might use logic like the following to keep the CPU active under specific conditions: void BPF_STRUCT_OPS(sched_update_idle, s32 cpu, bool idle) { if (!idle) return; if (condition) scx_bpf_kick_cpu(cpu, 0); } A call to scx_bpf_kick_cpu() wakes up the CPU, so in theory, ops.update_idle() should be triggered again until the condition becomes false. However, this doesn't happen, and scx_bpf_kick_cpu() doesn't produce the expected effect. In practice, this change badly impacts performance in user-space schedulers that rely on ops.update_idle() to activate user-space components. For instance, in the case of scx_rustland, performance drops significantly (e.g., gaming benchmarks fall from ~60fps to ~10fps). To address this, trigger ops.update_idle() from pick_task_idle() rather than set_next_task_idle(). This restores the correct behavior of ops.update_idle() and it allows to fix the performance regression in scx_rustland. Fixes: 7c65ae81ea86 ("sched_ext: Don't call put_prev_task_scx() before pick= ing the next task") Signed-off-by: Andrea Righi --- kernel/sched/idle.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) ChangeLog v2 -> v3: - add a comment to clarify why we need to update the scx idle state in pick_task() ChangeLog v1 -> v2: - move the logic from put_prev_set_next_task() to scx_update_idle() diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index d2f096bb274c..d336a05a6006 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -459,13 +459,26 @@ static void put_prev_task_idle(struct rq *rq, struct = task_struct *prev, struct t static void set_next_task_idle(struct rq *rq, struct task_struct *next, bo= ol first) { update_idle_core(rq); - scx_update_idle(rq, true); schedstat_inc(rq->sched_goidle); next->se.exec_start =3D rq_clock_task(rq); } =20 struct task_struct *pick_task_idle(struct rq *rq) { + /* + * When switching from a non-idle to the idle class, .set_next_task() + * is called only once during the transition. + * + * However, the CPU may remain active for multiple rounds (e.g., by + * calling scx_bpf_kick_cpu() from the ops.update_idle() callback). + * + * In such cases, we need to keep updating the scx idle state to + * properly re-trigger the ops.update_idle() callback. + * + * Updating the state in .pick_task(), instead of .set_next_task(), + * ensures correct handling of scx idle state transitions. + */ + scx_update_idle(rq, true); return rq->idle; } =20 --=20 2.47.0