linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Frederic Weisbecker <frederic@kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Jacek Anaszewski <jacek.anaszewski@gmail.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	"Rafael J . Wysocki" <rjw@rjwysocki.net>,
	Rik van Riel <riel@surriel.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Yauheni Kaliuta <yauheni.kaliuta@redhat.com>,
	Ingo Molnar <mingo@kernel.org>,
	Viresh Kumar <viresh.kumar@linaro.org>,
	Pavel Machek <pavel@ucw.cz>
Subject: Re: [PATCH 11/14] sched/kcpustat: Introduce vtime-aware kcpustat accessor for CPUTIME_SYSTEM
Date: Thu, 24 Oct 2019 13:50:34 +0200	[thread overview]
Message-ID: <20191024115034.GA4114@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <20191016025700.31277-12-frederic@kernel.org>

On Wed, Oct 16, 2019 at 04:56:57AM +0200, Frederic Weisbecker wrote:

> +static int kcpustat_field_vtime(u64 *cpustat,
> +				struct vtime *vtime,
> +				enum cpu_usage_stat usage,
> +				int cpu, u64 *val)
> +{
> +	unsigned int seq;
> +	int err;
> +
> +	do {
> +		seq = read_seqcount_begin(&vtime->seqcount);
> +
> +		/*
> +		 * We raced against context switch, fetch the
> +		 * kcpustat task again.
> +		 */
> +		if (vtime->cpu != cpu && vtime->cpu != -1) {
> +			err = -EAGAIN;
> +			continue;

Did that want to be break?

> +		}
> +
> +		/*
> +		 * Two possible things here:
> +		 * 1) We are seeing the scheduling out task (prev) or any past one.
> +		 * 2) We are seeing the scheduling in task (next) but it hasn't
> +		 *    passed though vtime_task_switch() yet so the pending
> +		 *    cputime of the prev task may not be flushed yet.
> +		 *
> +		 * Case 1) is ok but 2) is not. So wait for a safe VTIME state.
> +		 */
> +		if (vtime->state == VTIME_INACTIVE) {
> +			err = -EAGAIN;
> +			continue;

Idem.

If so, you can do return -EAGAIN here, and return 0 at the end and get
rid of err.

Also, if you're spin-waiting here, there should probably be a
cpu_relax() before the return -EAGAIN.

And in case that is so, you probably want the rcu_read_lock() section
below _inside_ the do{}while loop, such that the RCU section doesn't
cover the entire spin-wait.

> +		}
> +
> +		err = 0;
> +
> +		*val = cpustat[usage];
> +
> +		if (vtime->state == VTIME_SYS)
> +			*val += vtime->stime + vtime_delta(vtime);
> +
> +	} while (read_seqcount_retry(&vtime->seqcount, seq));
> +
> +	return err;
> +}
> +
> +u64 kcpustat_field(struct kernel_cpustat *kcpustat,
> +		   enum cpu_usage_stat usage, int cpu)
> +{
> +	u64 val;
> +	int err;
> +	u64 *cpustat = kcpustat->cpustat;
> +
> +	if (!vtime_accounting_enabled_cpu(cpu))
> +		return cpustat[usage];
> +
> +	/* Only support sys vtime for now */
> +	if (usage != CPUTIME_SYSTEM)
> +		return cpustat[usage];
> +
> +	rcu_read_lock();
> +
> +	do {
> +		struct rq *rq = cpu_rq(cpu);
> +		struct task_struct *curr;
> +		struct vtime *vtime;
> +
> +		curr = rcu_dereference(rq->curr);

This is indeed safe now (relies on commit

  5311a98fef7d ("tasks, sched/core: RCUify the assignment of rq->curr")

)

> +		if (WARN_ON_ONCE(!curr)) {
> +			val = cpustat[usage];
> +			break;
> +		}
> +
> +		vtime = &curr->vtime;
> +		err = kcpustat_field_vtime(cpustat, vtime, usage, cpu, &val);
> +	} while (err == -EAGAIN);
> +
> +	rcu_read_unlock();
> +
> +	return val;
> +}
> +
>  #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
> -- 
> 2.23.0
> 

  reply	other threads:[~2019-10-24 11:51 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-16  2:56 [PATCH 00/14] sched/nohz: Make kcpustat's CPUTIME_SYSTEM vtime aware v2 (Partially fix kcpustat on nohz_full) Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 01/14] sched/vtime: Record CPU under seqcount for kcpustat needs Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 02/14] sched/cputime: Add vtime idle task state Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 03/14] sched/cputime: Add vtime guest " Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 04/14] context_tracking: Remove context_tracking_active() Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 05/14] context_tracking: s/context_tracking_is_enabled/context_tracking_enabled() Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] context_tracking: Rename context_tracking_is_enabled() => context_tracking_enabled() tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 06/14] context_tracking: Rename context_tracking_is_cpu_enabled() to context_tracking_enabled_this_cpu() Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 07/14] context_tracking: Introduce context_tracking_enabled_cpu() Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 08/14] sched/vtime: Rename vtime_accounting_cpu_enabled() to vtime_accounting_enabled_this_cpu() Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 09/14] sched/vtime: Introduce vtime_accounting_enabled_cpu() Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 10/14] context_tracking: Check static key on context_tracking_enabled_*cpu() Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 11/14] sched/kcpustat: Introduce vtime-aware kcpustat accessor for CPUTIME_SYSTEM Frederic Weisbecker
2019-10-24 11:50   ` Peter Zijlstra [this message]
2019-10-25  1:25     ` Frederic Weisbecker
2019-10-25  2:03   ` [PATCH 11/14 v2] " Frederic Weisbecker
2019-10-29  9:52     ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 12/14] procfs: Use vtime aware kcpustat accessor to fetch CPUTIME_SYSTEM Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:56 ` [PATCH 13/14] cpufreq: " Frederic Weisbecker
2019-10-16  3:40   ` Viresh Kumar
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-16  2:57 ` [PATCH 14/14] leds: " Frederic Weisbecker
2019-10-29  9:52   ` [tip: sched/core] " tip-bot2 for Frederic Weisbecker
2019-10-24  0:45 ` [GIT PULL] sched/nohz: Make kcpustat's CPUTIME_SYSTEM vtime aware Frederic Weisbecker

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=20191024115034.GA4114@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=frederic@kernel.org \
    --cc=jacek.anaszewski@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=pavel@ucw.cz \
    --cc=riel@surriel.com \
    --cc=rjw@rjwysocki.net \
    --cc=tglx@linutronix.de \
    --cc=viresh.kumar@linaro.org \
    --cc=wanpengli@tencent.com \
    --cc=yauheni.kaliuta@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;
as well as URLs for NNTP newsgroup(s).