public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: torvalds@linux-foundation.org, mingo@redhat.com,
	peterz@infradead.org, juri.lelli@redhat.com,
	vincent.guittot@linaro.org, dietmar.eggemann@arm.com,
	rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de,
	bristot@redhat.com, vschneid@redhat.com, ast@kernel.org,
	daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org,
	joshdon@google.com, brho@google.com, pjt@google.com,
	derkling@google.com, haoluo@google.com, dvernet@meta.com,
	dschatzberg@meta.com, dskarlat@cs.cmu.edu, riel@surriel.com
Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	kernel-team@meta.com, Tejun Heo <tj@kernel.org>
Subject: [PATCH 21/34] sched_ext: Add task state tracking operations
Date: Mon, 10 Jul 2023 15:13:39 -1000	[thread overview]
Message-ID: <20230711011412.100319-22-tj@kernel.org> (raw)
In-Reply-To: <20230711011412.100319-1-tj@kernel.org>

Being able to track the task runnable and running state transitions are
useful for a variety of purposes including latency tracking and load factor
calculation.

Currently, BPF schedulers don't have a good way of tracking these
transitions. Becoming runnable can be determined from ops.enqueue() but
becoming quiescent can only be inferred from the lack of subsequent enqueue.
Also, as the local dsq can have multiple tasks and some events are handled
in the sched_ext core, it's difficult to determine when a given task starts
and stops executing.

This patch adds sched_ext_ops.runnable(), .running(), .stopping() and
.quiescent() operations to track the task runnable and running state
transitions. They're mostly self explanatory; however, we want to ensure
that running <-> stopping transitions are always contained within runnable
<-> quiescent transitions which is a bit different from how the scheduler
core behaves. This adds a bit of complication. See the comment in
dequeue_task_scx().

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: David Vernet <dvernet@meta.com>
Acked-by: Josh Don <joshdon@google.com>
Acked-by: Hao Luo <haoluo@google.com>
Acked-by: Barret Rhoden <brho@google.com>
---
 include/linux/sched/ext.h | 65 +++++++++++++++++++++++++++++++++++++++
 kernel/sched/ext.c        | 31 +++++++++++++++++++
 2 files changed, 96 insertions(+)

diff --git a/include/linux/sched/ext.h b/include/linux/sched/ext.h
index 5c2df7ccc0a6..5cc37bc30352 100644
--- a/include/linux/sched/ext.h
+++ b/include/linux/sched/ext.h
@@ -192,6 +192,71 @@ struct sched_ext_ops {
 	 */
 	void (*dispatch)(s32 cpu, struct task_struct *prev);
 
+	/**
+	 * runnable - A task is becoming runnable on its associated CPU
+	 * @p: task becoming runnable
+	 * @enq_flags: %SCX_ENQ_*
+	 *
+	 * This and the following three functions can be used to track a task's
+	 * execution state transitions. A task becomes ->runnable() on a CPU,
+	 * and then goes through one or more ->running() and ->stopping() pairs
+	 * as it runs on the CPU, and eventually becomes ->quiescent() when it's
+	 * done running on the CPU.
+	 *
+	 * @p is becoming runnable on the CPU because it's
+	 *
+	 * - waking up (%SCX_ENQ_WAKEUP)
+	 * - being moved from another CPU
+	 * - being restored after temporarily taken off the queue for an
+	 *   attribute change.
+	 *
+	 * This and ->enqueue() are related but not coupled. This operation
+	 * notifies @p's state transition and may not be followed by ->enqueue()
+	 * e.g. when @p is being dispatched to a remote CPU. Likewise, a task
+	 * may be ->enqueue()'d without being preceded by this operation e.g.
+	 * after exhausting its slice.
+	 */
+	void (*runnable)(struct task_struct *p, u64 enq_flags);
+
+	/**
+	 * running - A task is starting to run on its associated CPU
+	 * @p: task starting to run
+	 *
+	 * See ->runnable() for explanation on the task state notifiers.
+	 */
+	void (*running)(struct task_struct *p);
+
+	/**
+	 * stopping - A task is stopping execution
+	 * @p: task stopping to run
+	 * @runnable: is task @p still runnable?
+	 *
+	 * See ->runnable() for explanation on the task state notifiers. If
+	 * !@runnable, ->quiescent() will be invoked after this operation
+	 * returns.
+	 */
+	void (*stopping)(struct task_struct *p, bool runnable);
+
+	/**
+	 * quiescent - A task is becoming not runnable on its associated CPU
+	 * @p: task becoming not runnable
+	 * @deq_flags: %SCX_DEQ_*
+	 *
+	 * See ->runnable() for explanation on the task state notifiers.
+	 *
+	 * @p is becoming quiescent on the CPU because it's
+	 *
+	 * - sleeping (%SCX_DEQ_SLEEP)
+	 * - being moved to another CPU
+	 * - being temporarily taken off the queue for an attribute change
+	 *   (%SCX_DEQ_SAVE)
+	 *
+	 * This and ->dequeue() are related but not coupled. This operation
+	 * notifies @p's state transition and may not be preceded by ->dequeue()
+	 * e.g. when @p is being dispatched to a remote CPU.
+	 */
+	void (*quiescent)(struct task_struct *p, u64 deq_flags);
+
 	/**
 	 * yield - Yield CPU
 	 * @from: yielding task
diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 48e27d59e621..2951200cd81a 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -775,6 +775,9 @@ static void enqueue_task_scx(struct rq *rq, struct task_struct *p, int enq_flags
 	rq->scx.nr_running++;
 	add_nr_running(rq, 1);
 
+	if (SCX_HAS_OP(runnable))
+		SCX_CALL_OP(SCX_KF_REST, runnable, p, enq_flags);
+
 	do_enqueue_task(rq, p, enq_flags, sticky_cpu);
 }
 
@@ -835,6 +838,26 @@ static void dequeue_task_scx(struct rq *rq, struct task_struct *p, int deq_flags
 
 	ops_dequeue(p, deq_flags);
 
+	/*
+	 * A currently running task which is going off @rq first gets dequeued
+	 * and then stops running. As we want running <-> stopping transitions
+	 * to be contained within runnable <-> quiescent transitions, trigger
+	 * ->stopping() early here instead of in put_prev_task_scx().
+	 *
+	 * @p may go through multiple stopping <-> running transitions between
+	 * here and put_prev_task_scx() if task attribute changes occur while
+	 * balance_scx() leaves @rq unlocked. However, they don't contain any
+	 * information meaningful to the BPF scheduler and can be suppressed by
+	 * skipping the callbacks if the task is !QUEUED.
+	 */
+	if (SCX_HAS_OP(stopping) && task_current(rq, p)) {
+		update_curr_scx(rq);
+		SCX_CALL_OP(SCX_KF_REST, stopping, p, false);
+	}
+
+	if (SCX_HAS_OP(quiescent))
+		SCX_CALL_OP(SCX_KF_REST, quiescent, p, deq_flags);
+
 	if (deq_flags & SCX_DEQ_SLEEP)
 		p->scx.flags |= SCX_TASK_DEQD_FOR_SLEEP;
 	else
@@ -1371,6 +1394,10 @@ static void set_next_task_scx(struct rq *rq, struct task_struct *p, bool first)
 
 	p->se.exec_start = rq_clock_task(rq);
 
+	/* see dequeue_task_scx() on why we skip when !QUEUED */
+	if (SCX_HAS_OP(running) && (p->scx.flags & SCX_TASK_QUEUED))
+		SCX_CALL_OP(SCX_KF_REST, running, p);
+
 	watchdog_unwatch_task(p, true);
 }
 
@@ -1409,6 +1436,10 @@ static void put_prev_task_scx(struct rq *rq, struct task_struct *p)
 
 	update_curr_scx(rq);
 
+	/* see dequeue_task_scx() on why we skip when !QUEUED */
+	if (SCX_HAS_OP(stopping) && (p->scx.flags & SCX_TASK_QUEUED))
+		SCX_CALL_OP(SCX_KF_REST, stopping, p, true);
+
 	/*
 	 * If we're being called from put_prev_task_balance(), balance_scx() may
 	 * have decided that @p should keep running.
-- 
2.41.0


  parent reply	other threads:[~2023-07-11  1:16 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-11  1:13 [PATCHSET v4] sched: Implement BPF extensible scheduler class Tejun Heo
2023-07-11  1:13 ` [PATCH 01/34] cgroup: Implement cgroup_show_cftypes() Tejun Heo
2023-07-11  1:13 ` [PATCH 02/34] sched: Restructure sched_class order sanity checks in sched_init() Tejun Heo
2023-07-11  1:13 ` [PATCH 03/34] sched: Allow sched_cgroup_fork() to fail and introduce sched_cancel_fork() Tejun Heo
2023-07-11  1:13 ` [PATCH 04/34] sched: Add sched_class->reweight_task() Tejun Heo
2023-07-11  1:13 ` [PATCH 05/34] sched: Add sched_class->switching_to() and expose check_class_changing/changed() Tejun Heo
2023-07-11  1:13 ` [PATCH 06/34] sched: Factor out cgroup weight conversion functions Tejun Heo
2023-07-11  1:13 ` [PATCH 07/34] sched: Expose css_tg() and __setscheduler_prio() Tejun Heo
2023-07-11  1:13 ` [PATCH 08/34] sched: Enumerate CPU cgroup file types Tejun Heo
2023-07-11  1:13 ` [PATCH 09/34] sched: Add @reason to sched_class->rq_{on|off}line() Tejun Heo
2023-07-11  1:13 ` [PATCH 10/34] sched: Add normal_policy() Tejun Heo
2023-07-11  1:13 ` [PATCH 11/34] sched_ext: Add boilerplate for extensible scheduler class Tejun Heo
2023-07-11  1:13 ` [PATCH 12/34] sched_ext: Implement BPF " Tejun Heo
2023-07-11  9:21   ` Andrea Righi
2023-07-11 21:45     ` Tejun Heo
2023-08-16 11:45   ` Vishal Chourasia
2023-08-16 19:20     ` Tejun Heo
2023-07-11  1:13 ` [PATCH 13/34] sched_ext: Add scx_simple and scx_example_qmap example schedulers Tejun Heo
2023-07-11  1:13 ` [PATCH 14/34] sched_ext: Add sysrq-S which disables the BPF scheduler Tejun Heo
2023-07-11  1:13 ` [PATCH 15/34] sched_ext: Implement runnable task stall watchdog Tejun Heo
2023-07-11  1:13 ` [PATCH 16/34] sched_ext: Allow BPF schedulers to disallow specific tasks from joining SCHED_EXT Tejun Heo
2023-07-11  1:13 ` [PATCH 17/34] sched_ext: Allow BPF schedulers to switch all eligible tasks into sched_ext Tejun Heo
2023-07-11  1:13 ` [PATCH 18/34] sched_ext: Implement scx_bpf_kick_cpu() and task preemption support Tejun Heo
2023-07-11  1:13 ` [PATCH 19/34] sched_ext: Add a central scheduler which makes all scheduling decisions on one CPU Tejun Heo
2023-07-11  1:13 ` [PATCH 20/34] sched_ext: Make watchdog handle ops.dispatch() looping stall Tejun Heo
2023-07-11  1:13 ` Tejun Heo [this message]
2023-07-11  1:13 ` [PATCH 22/34] sched_ext: Implement tickless support Tejun Heo
2023-07-11  1:13 ` [PATCH 23/34] sched_ext: Track tasks that are subjects of the in-flight SCX operation Tejun Heo
2023-07-11  1:13 ` [PATCH 24/34] sched_ext: Add cgroup support Tejun Heo
2023-07-11  1:13 ` [PATCH 25/34] sched_ext: Add a cgroup-based core-scheduling scheduler Tejun Heo
2023-07-11  1:13 ` [PATCH 26/34] sched_ext: Add a cgroup scheduler which uses flattened hierarchy Tejun Heo
2023-07-11  1:13 ` [PATCH 27/34] sched_ext: Implement SCX_KICK_WAIT Tejun Heo
2023-07-13 13:45   ` Andrea Righi
2023-07-13 18:32     ` Linus Torvalds
2023-07-13 19:48       ` Tejun Heo
2023-07-11  1:13 ` [PATCH 28/34] sched_ext: Implement sched_ext_ops.cpu_acquire/release() Tejun Heo
2023-07-11  1:13 ` [PATCH 29/34] sched_ext: Implement sched_ext_ops.cpu_online/offline() Tejun Heo
2023-07-11  1:13 ` [PATCH 30/34] sched_ext: Implement core-sched support Tejun Heo
2023-07-11  1:13 ` [PATCH 31/34] sched_ext: Add vtime-ordered priority queue to dispatch_q's Tejun Heo
2023-07-11  1:13 ` [PATCH 32/34] sched_ext: Documentation: scheduler: Document extensible scheduler class Tejun Heo
2023-07-11  1:13 ` [PATCH 33/34] sched_ext: Add a basic, userland vruntime scheduler Tejun Heo
2023-07-11  1:13 ` [PATCH 34/34] sched_ext: Add a rust userspace hybrid example scheduler Tejun Heo
2023-07-21 18:37 ` [PATCHSET v4] sched: Implement BPF extensible scheduler class Tejun Heo
2023-07-24 15:11   ` Barret Rhoden
2023-07-26  9:17   ` Peter Zijlstra
2023-07-28  0:12     ` Tejun Heo
2023-08-04  0:08       ` Tejun Heo
2023-08-11  1:16       ` Tejun Heo
2023-08-17 12:44       ` Mel Gorman
2023-08-24 21:31         ` Tejun Heo
2023-09-19 17:56           ` Tejun Heo
2023-09-26  9:20             ` Mel Gorman
2023-10-10 22:09               ` Tejun Heo
2023-08-25  0:26   ` Josh Don

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=20230711011412.100319-22-tj@kernel.org \
    --to=tj@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brho@google.com \
    --cc=bristot@redhat.com \
    --cc=bsegall@google.com \
    --cc=daniel@iogearbox.net \
    --cc=derkling@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=dschatzberg@meta.com \
    --cc=dskarlat@cs.cmu.edu \
    --cc=dvernet@meta.com \
    --cc=haoluo@google.com \
    --cc=joshdon@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pjt@google.com \
    --cc=riel@surriel.com \
    --cc=rostedt@goodmis.org \
    --cc=torvalds@linux-foundation.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