Path: csiph.com!news.redatomik.org!aioe.org!bofh.it!news.nic.it!robomod From: Vince Weaver Newsgroups: linux.kernel Subject: Re: perf: race with automatic rdpmc() disabling Date: Tue, 14 Mar 2017 17:50:01 +0100 Message-ID: References: Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maine.edu; s=google; h=from:date:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=yHYsHiOCMQX1k0alqIgVOuv8dwpL8pXLQ0g3lnauc0g=; b=jy5D5c2kbag5c7f82XKOx9Nn4VwtuIUJUZFgh68oSAWEopOS/tPke4aXNudy9eG6Qw 3tT7GD1X+uJ111zDFRWzQbhKRCysB77N1aNugerJDl33ucV2rdYhvAGH1dTme11Ll7af WI2Bp+v/m2A/2+cv0D5EEPodOErnz+QLB/WZY= X-Google-Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=yHYsHiOCMQX1k0alqIgVOuv8dwpL8pXLQ0g3lnauc0g=; b=CQv+iEtNlFnotnALtB4X4uaUnbbbClrduoeXnziTpOKuIKRFJ1chrUw2lDDpW9OfWN R70GBtSePCRHcj31TzQcvrIUWkq2xGbmEQdFqouXMdvOo6/DN7RYEr72X28aDQwKmno9 MxXNZfNCQp90JzzeRPeNpyXVs+cGBgUOjmb2/kXW9e4DSk8zkvVC6kF7/DR2aI+ukmO4 48DHqkq0sFo31u+KjuLjl+pOAXIv+f4v2a8lpLGeBz2zV85YzahJum59Ja6EstR0owS7 /Do12V9gG6DVSd2NMWWzm4Rj/ZNfzPhcB/j1ypGPPml+CQ1aZmKxiBu6rTVu7ljNo9Gr ywiA== X-Gm-Message-State: AFeK/H23Ik7KkfYXutfakKlLK1DNQvaPkuDHFZlaGQChuI8mK1nXcijy/+L/Lrw79voNBRHY X-Received: by 10.36.47.213 with SMTP id j204mr16721864itj.7.1489509955597; Tue, 14 Mar 2017 09:45:55 -0700 (PDT) X-Google-Original-From: Vince Weaver X-X-Sender: vince@macbook-air User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: robomod@news.nic.it List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Approved: robomod@news.nic.it Lines: 41 Organization: linux.* mail to news gateway X-Original-Cc: Andy Lutomirski , Peter Zijlstra , "linux-kernel@vger.kernel.org" , Ingo Molnar , Arnaldo Carvalho de Melo X-Original-Date: Tue, 14 Mar 2017 12:45:52 -0400 (EDT) X-Original-Message-ID: X-Original-References: <20170313165507.GJ3312@twins.programming.kicks-ass.net> X-Original-Sender: linux-kernel-owner@vger.kernel.org Xref: csiph.com linux.kernel:1600685 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