lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Mon, 29 Aug 2016 15:02:13 +0200
From:   Peter Zijlstra <peterz@...radead.org>
To:     Jiri Olsa <jolsa@...hat.com>
Cc:     Vegard Nossum <vegard.nossum@...il.com>,
        Thomas Gleixner <tglx@...utronix.de>,
        Stephane Eranian <eranian@...gle.com>,
        Vince Weaver <vincent.weaver@...ne.edu>,
        Ingo Molnar <mingo@...nel.org>,
        David Carrillo-Cisneros <davidcc@...gle.com>,
        "H. Peter Anvin" <hpa@...or.com>, Kan Liang <kan.liang@...el.com>,
        Arnaldo Carvalho de Melo <acme@...hat.com>,
        Paul Turner <pjt@...gle.com>,
        Linus Torvalds <torvalds@...ux-foundation.org>,
        LKML <linux-kernel@...r.kernel.org>,
        Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
        linux-tip-commits@...r.kernel.org
Subject: Re: [tip:perf/core] perf/core: Check return value of the
 perf_event_read() IPI

On Mon, Aug 29, 2016 at 12:03:09PM +0200, Peter Zijlstra wrote:
> @@ -1802,8 +1802,18 @@ event_sched_out(struct perf_event *event,
>  
>  	event->tstamp_stopped = tstamp;
>  	event->pmu->del(event, 0);
> -	event->oncpu = -1;
> -	event->state = PERF_EVENT_STATE_INACTIVE;
> +
> +	WRITE_ONCE(event->state, PERF_EVENT_STATE_INACTIVE);
> +	/*
> +	 * pmu::del() will have updated the event count. Now mark it inactive,
> +	 * but take care to clear ->oncpu after the INACTIVE store, such that
> +	 * while ->state == ACTIVE, ->oncpu must be valid.
> +	 *
> +	 * See event_sched_in(), perf_event_restart() and perf_event_read().
> +	 */
> +	smp_wmb();
> +	WRITE_ONCE(event->oncpu, -1);
> +
>  	if (event->pending_disable) {
>  		event->pending_disable = 0;
>  		event->state = PERF_EVENT_STATE_OFF;
> @@ -2015,8 +2025,10 @@ event_sched_in(struct perf_event *event,
>  
>  	WRITE_ONCE(event->oncpu, smp_processor_id());
>  	/*
> -	 * Order event::oncpu write to happen before the ACTIVE state
> -	 * is visible.
> +	 * Order event::oncpu write to happen before the ACTIVE state is
> +	 * visible, such that when we observe ACTIVE, oncpu must be correct.
> +	 *
> +	 * Matches the smp_rmb() in perf_event_restart().
>  	 */
>  	smp_wmb();
>  	WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);

Urgh.. that cannot work either, because now perf_event_read() can race
against event_sched_in(). Since that's no longer crossed.

> @@ -3561,28 +3576,36 @@ u64 perf_event_read_local(struct perf_event *event)
>  
>  static int perf_event_read(struct perf_event *event, bool group)
>  {
> -	int ret = 0, cpu_to_read, local_cpu;
> +	int ret = 0, cpu_to_read, local_cpu, state;
> +
> +	local_cpu = get_cpu(); /* disable preemption to hold off hotplut */
> +	cpu_to_read = READ_ONCE(event->oncpu);
> +	/*
> +	 * Matches smp_wmb() from event_sched_out(), ->oncpu must be valid
> +	 * IFF we observe ACTIVE.
> +	 */
> +	smp_rmb();
> +	state = READ_ONCE(event->state);

The best I can come up with is something like:


	do {
		state = READ_ONCE(event->state);
		if (state != ACTIVE)
			break;
		smp_rmb();
		cpu = READ_ONCE(event->cpu);
		smp_rmb();
	} while (READ_ONCE(event->state) != state);


And I suppose perf_event_restart() should do the same thing... Let me
ponder this a wee bit more.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ