Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752109AbdCNQp7 (ORCPT ); Tue, 14 Mar 2017 12:45:59 -0400 Received: from mail-it0-f67.google.com ([209.85.214.67]:35446 "EHLO mail-it0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750797AbdCNQp5 (ORCPT ); Tue, 14 Mar 2017 12:45:57 -0400 From: Vince Weaver X-Google-Original-From: Vince Weaver Date: Tue, 14 Mar 2017 12:45:52 -0400 (EDT) X-X-Sender: vince@macbook-air To: Andy Lutomirski cc: Andy Lutomirski , Peter Zijlstra , "linux-kernel@vger.kernel.org" , Ingo Molnar , Arnaldo Carvalho de Melo Subject: Re: perf: race with automatic rdpmc() disabling In-Reply-To: Message-ID: References: <20170313165507.GJ3312@twins.programming.kicks-ass.net> User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1549 Lines: 41 On Tue, 14 Mar 2017, Andy Lutomirski wrote: > On Mon, Mar 13, 2017 at 2:05 PM, Andy Lutomirski wrote: > > On Mon, Mar 13, 2017 at 9:55 AM, Peter Zijlstra wrote: > >> On Mon, Mar 13, 2017 at 09:44:02AM -0700, Andy Lutomirski wrote: > >>> static void x86_pmu_event_mapped(struct perf_event *event) > >>> { > >>> if (!(event->hw.flags & PERF_X86_EVENT_RDPMC_ALLOWED)) > >>> return; > >>> > >>> if (atomic_inc_return(¤t->mm->context.perf_rdpmc_allowed) == 1) > >>> > >>> <-- thread 1 stalls here > >>> > >>> on_each_cpu_mask(mm_cpumask(current->mm), refresh_pce, NULL, 1); > >>> } > >>> > >>> Suppose you start with perf_rdpmc_allowed == 0. Thread 1 runs > >>> x86_pmu_event_mapped and gets preempted (or just runs slowly) where I > >>> marked. Then thread 2 runs the whole function, does *not* update CR4, > >>> returns to userspace, and GPFs. > >>> > >>> The big hammer solution is to stick a per-mm mutex around it. Let me > >>> ponder whether a smaller hammer is available. > >> > >> Reminds me a bit of what we ended up with in kernel/jump_label.c:static_key_slow_inc(). > >> > >> > > > > One thing I don't get: isn't mmap_sem held for write the whole time? > > mmap_sem is indeed held, so my theory is wrong. I can reproduce it, > but I don't see the bug yet... It could still be a PAPI bug, as I'm having absolutely no luck trying to come up with a plain perf_event reproducer. Let me dig through the PAPI code again and make sure I'm not missing something. Vince