Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753843AbdCMQqf (ORCPT ); Mon, 13 Mar 2017 12:46:35 -0400 Received: from mail.kernel.org ([198.145.29.136]:35728 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754113AbdCMQoa (ORCPT ); Mon, 13 Mar 2017 12:44:30 -0400 MIME-Version: 1.0 In-Reply-To: References: From: Andy Lutomirski Date: Mon, 13 Mar 2017 09:44:02 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: perf: race with automatic rdpmc() disabling To: Vince Weaver Cc: "linux-kernel@vger.kernel.org" , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2035 Lines: 53 On Mon, Mar 13, 2017 at 6:58 AM, Vince Weaver wrote: > Hello > > I've been trying to track this issue down for a few days and haven't been > able to isolate it. So maybe someone who understands low-level perf mmap > reference counting can help here. > > As you might recall, 7911d3f7af14a614617e38245fedf98a724e46a9 > introduced automatic disabling of userspace rdpmc when no perf_events > were running. > > I've run into a problem with PAPI when using rdpmc. If you have PAPI > measuring events in multiple pthread threads, sometimes (but not always) > the program will GPF because CR4/rdpmc gets turned off while events are > still active. > > I've been trying to put together a reproducible test case but haven't been > able to manage. I have a PAPI test that will show the problem about > 50% of the time but I can't seem to isolate the problem. > > Any ideas? > > If you really want to try to reproduce it, get the current git version of > PAPI > git clone https://bitbucket.org/icl/papi.git > edit src/components/perf_event/perf_event.c > so that #define PERF_USE_RDPMC 1 > in src run ./configure , make > then run the ./ctests/zero_pthreads test a few times. It will GPF and I'm > relatively (though not entirely) sure it's not a PAPI issue. > The problem does go away if you set /sys/devices/cpu/rdpmc to 2 Hmm 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.