[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241211093256.GY35539@noisy.programming.kicks-ass.net>
Date: Wed, 11 Dec 2024 10:32:56 +0100
From: Peter Zijlstra <peterz@...radead.org>
To: Changwoo Min <multics69@...il.com>
Cc: tj@...nel.org, void@...ifault.com, mingo@...hat.com,
changwoo@...lia.com, kernel-dev@...lia.com,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v4 4/6] sched_ext: Implement scx_bpf_now_ns()
On Mon, Dec 09, 2024 at 03:15:29PM +0900, Changwoo Min wrote:
> +__bpf_kfunc u64 scx_bpf_now_ns(void)
> +{
> + struct rq *rq;
> + u64 clock;
> +
> + preempt_disable();
> +
> + /*
> + * If the rq clock is valid, use the cached rq clock
> + * whenever the clock does not go backward.
> + */
> + rq = this_rq();
> + clock = rq->scx.clock;
> +
> + if (!(rq->scx.flags & SCX_RQ_CLK_VALID) ||
> + (rq->scx.prev_clock >= clock)) {
As TJ said, it's best to consider that the clock can wrap.
> + /*
> + * If the rq clock is invalid or goes backward,
> + * start a new rq clock period with a fresh sched_clock_cpu().
> + *
> + * The cached rq clock can go backward because there is a
> + * race with a timer interrupt. Suppose that a timer interrupt
> + * occurred while running scx_bpf_now_ns() *after* reading the
> + * rq clock and *before* comparing the if condition. The timer
> + * interrupt will eventually call a BPF scheduler's ops.tick(),
> + * and the BPF scheduler can call scx_bpf_now_ns(). Since the
> + * scheduler core updates the rq clock before calling
> + * ops.tick(), the scx_bpf_now_ns() call will get the fresh
> + * clock. After handling the timer interrupt, the interrupted
> + * scx_bpf_now_ns() will be resumed, so the if condition will
> + * be compared. In this case, the clock, which was read before
> + * the timer interrupt, will be the same as rq->scx.prev_clock.
> + * When such a case is detected, start a new rq clock period
> + * with a fresh sched_clock_cpu().
This has a wall-of-text problem; use paragraphs?
> + */
> + clock = sched_clock_cpu(cpu_of(rq));
> + scx_rq_clock_update(rq, clock);
Doesn't this set the VALID bit again? How is using this outside of
RQ-lock and setting VALID a good idea?
> + }
> +
> + preempt_enable();
> +
> + return clock;
> +}
> +
> __bpf_kfunc_end_defs();
>
> BTF_KFUNCS_START(scx_kfunc_ids_any)
> @@ -7632,6 +7704,7 @@ BTF_ID_FLAGS(func, scx_bpf_cpu_rq)
> #ifdef CONFIG_CGROUP_SCHED
> BTF_ID_FLAGS(func, scx_bpf_task_cgroup, KF_RCU | KF_ACQUIRE)
> #endif
> +BTF_ID_FLAGS(func, scx_bpf_now_ns)
> BTF_KFUNCS_END(scx_kfunc_ids_any)
>
> static const struct btf_kfunc_id_set scx_kfunc_set_any = {
> --
> 2.47.1
>
Powered by blists - more mailing lists