public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Changwoo Min <multics69@gmail.com>
Cc: tj@kernel.org, void@manifault.com, arighi@nvidia.com,
	mingo@redhat.com, changwoo@igalia.com, kernel-dev@igalia.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v7 2/6] sched_ext: Implement scx_bpf_now()
Date: Wed, 8 Jan 2025 09:50:26 +0100	[thread overview]
Message-ID: <20250108085026.GC23315@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20241230095625.114363-3-changwoo@igalia.com>


> +__bpf_kfunc u64 scx_bpf_now(void)
> +{
> +	struct rq *rq;
> +	u64 clock;
> +
> +	preempt_disable();
> +
> +	rq = this_rq();
> +	if (READ_ONCE(rq->scx.flags) & SCX_RQ_CLK_VALID) {
> +		/*
> +		 * If the rq clock is valid, use the cached rq clock.
> +		 *
> +		 * Note that scx_bpf_now() 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.
> +		 */
> +		clock = READ_ONCE(rq->scx.clock);
> +	} else {
> +		/*
> +		 * Otherwise, return a fresh rq clock.
> +		 *
> +		 * 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.
> +		 */
> +		clock = sched_clock_cpu(cpu_of(rq));
> +	}
> +
> +	preempt_enable();
> +
> +	return clock;
> +}

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


AFAICT it is possible to be used like:


  CPU0					CPU1

  lock(rq1->lock);
  ...
  scx_rq_clock_update(...);		scx_bpf_now();
  ...
  unlock(rq->lock);


Which then enables the following ordering problem:

  CPU0					CPU1

  WRITE_ONCE(rq->scx.clock, clock);	if (rq->scx.flags & VALID)
  WRITE_ONCE(rq->scx.flags, VALID);	  return rq->scx.clock;

Where it then becomes possible to observe VALID before clock is written.

That is, I rather think you need:

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

and:

	if (smp_load_acquire(&rq->scx.flags) & SCX_RQ_CLK_VALID) {
> +		/*
> +		 * If the rq clock is valid, use the cached rq clock.
> +		 *
> +		 * Note that scx_bpf_now() 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.
> +		 */
> +		clock = READ_ONCE(rq->scx.clock);

Such that if you ovbserve VALID, you must then also observe the clock.

  reply	other threads:[~2025-01-08  8:50 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-30  9:56 [PATCH v7 0/6] sched_ext: Support high-performance monotonically non-decreasing clock Changwoo Min
2024-12-30  9:56 ` [PATCH v7 1/6] sched_ext: Relocate scx_enabled() related code Changwoo Min
2024-12-30  9:56 ` [PATCH v7 2/6] sched_ext: Implement scx_bpf_now() Changwoo Min
2025-01-08  8:50   ` Peter Zijlstra [this message]
2025-01-08 16:04     ` Changwoo Min
2024-12-30  9:56 ` [PATCH v7 3/6] sched_ext: Add scx_bpf_now() for BPF scheduler Changwoo Min
2024-12-30  9:56 ` [PATCH v7 4/6] sched_ext: Add time helpers for BPF schedulers Changwoo Min
2024-12-30  9:56 ` [PATCH v7 5/6] sched_ext: Replace bpf_ktime_get_ns() to scx_bpf_now() Changwoo Min
2024-12-30  9:56 ` [PATCH v7 6/6] sched_ext: Use time helpers in BPF schedulers Changwoo Min
2025-01-02 18:54 ` [PATCH v7 0/6] sched_ext: Support high-performance monotonically non-decreasing clock Andrea Righi
2025-01-03 22:16 ` Tejun Heo
2025-01-07 19:46   ` Tejun Heo
2025-01-07 19:53     ` Peter Zijlstra
2025-01-07 19:55       ` Tejun Heo
2025-01-08  8:51         ` Peter Zijlstra

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=20250108085026.GC23315@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=arighi@nvidia.com \
    --cc=changwoo@igalia.com \
    --cc=kernel-dev@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=multics69@gmail.com \
    --cc=tj@kernel.org \
    --cc=void@manifault.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