Re: [PATCH v6 2/6] sched_ext: Implement scx_bpf_now_ns()

From: Tejun Heo
Date: Tue Dec 24 2024 - 16:48:06 EST


Hello,

On Fri, Dec 20, 2024 at 03:20:21PM +0900, Changwoo Min wrote:
...
> +__bpf_kfunc u64 scx_bpf_now_ns(void)

Given that the default time unit is ns for the scheduler, the _ns suffix
can probably go.

> +{
> + struct rq *rq;
> + u64 clock;
> +
> + preempt_disable();
> +
> + /*
> + * If the rq clock is valid, use the cached rq clock.
> + * Otherwise, return a fresh rq glock.
> + *
> + * Note that scx_bpf_now_ns() is re-entrant between a process
> + * context and an interrupt context (e.g., timer interrupt).
> + * However, we don't need to consider the race between them
> + * because such race is not observable from a caller.
> + */
> + rq = this_rq();
> + clock = READ_ONCE(rq->scx.clock);
> +
> + if (!(READ_ONCE(rq->scx.flags) & SCX_RQ_CLK_VALID)) {
> + clock = sched_clock_cpu(cpu_of(rq));
> +
> + /*
> + * The rq clock is updated outside of the rq lock.
> + * In this case, keep the updated rq clock invalid so the next
> + * kfunc call outside the rq lock gets a fresh rq clock.
> + */
> + scx_rq_clock_update(rq, clock, false);

Hmm... what does this update do?

...
> +static inline void scx_rq_clock_update(struct rq *rq, u64 clock, bool valid)
> +{
> + if (!scx_enabled())
> + return;
> + WRITE_ONCE(rq->scx.clock, clock);
> + if (valid)
> + WRITE_ONCE(rq->scx.flags, rq->scx.flags | SCX_RQ_CLK_VALID);
> +}

Isn't rq->scx.clock used iff VALID is set? If so, why does !VALID read need
to update rq->scx.clock?

Thanks.

--
tejun