From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752472AbcHQP0T (ORCPT ); Wed, 17 Aug 2016 11:26:19 -0400 Received: from merlin.infradead.org ([205.233.59.134]:43590 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750764AbcHQP0S (ORCPT ); Wed, 17 Aug 2016 11:26:18 -0400 Date: Wed, 17 Aug 2016 17:26:16 +0200 From: Peter Zijlstra To: Vince Weaver Cc: linux-kernel@vger.kernel.org, Ingo Molnar , Arnaldo Carvalho de Melo Subject: Re: perf: rdpmc and PERF_EVENT_IOC_RESET Message-ID: <20160817152616.GT30192@twins.programming.kicks-ass.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23.1 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 17, 2016 at 12:53:01AM -0400, Vince Weaver wrote: > Hello > > so using rdpmc() and the mmap page to do fast perf_event reads seems to > interact poorly with the PERF_EVENT_IOC_RESET ioctl. Hurm.. I never considered using RESET with rdpmc(). The typical usage of rdpmc() I considered is something like: u64 start = rdpmc(); /* code goes here */ delta = rdpmc() - start; That said; I don't object to fixing the interaction if it doesn't have downsides. > From what I can tell, on reset event->count is set to zero, but > event->hw.prev_count is not, so the userpg->offset field ends up negative > and weird things happen. _perf_event_reset() perf_event_read() /* updates prev_count */ local64_set(count, 0); after that, like you say: userpg->offset = perf_event_count() - prev_count, which does indeed end up negative. But the rdpmc user function should: u64 mmap_read_self(void *addr) { struct perf_event_mmap_page *pc = addr; u32 seq, idx, time_mult = 0, time_shift = 0, width = 0; u64 count, cyc = 0, time_offset = 0, enabled, running, delta; s64 pmc = 0; do { seq = pc->lock; barrier(); /* enabled/running muck */ idx = pc->index; count = pc->offset; if (pc->cap_user_rdpmc && idx) { width = pc->pmc_width; pmc = rdpmc(idx - 1); } barrier(); } while (pc->lock != seq); if (idx) { pmc <<= 64 - width; pmc >>= 64 - width; /* shift right signed */ count += pmc; } /* more enabled/running muck */ return count; } Which sign-extends the RDPMC result and adds it to userpg->offset, resulting in a positive number again. So I think it should work..