Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp330674pxf; Thu, 8 Apr 2021 04:10:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwZ27gaFi89Y0C3c4ZBMPPcLMyiglX6pn4po4ZJUujDPkeOmMVHNkJghSxk/8Gk70PL8r1S X-Received: by 2002:a17:906:2504:: with SMTP id i4mr9657460ejb.115.1617880205118; Thu, 08 Apr 2021 04:10:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617880205; cv=none; d=google.com; s=arc-20160816; b=yfVFkQIJJungBPUVcQwNYY91ceKvpZj4kO1gDCahar3dkJCxS3bRSrCOohQ26IXT3o 1hJ3a6B1c9yYQMV7tKTMoRVfheSllg3Sd+DH0k4yKcrVOKX5Dk7qZm5fg7/rUBD1myCy XwamyGpetHLMxL7umlUAUh9mmRiXcQ1qbKexElWTAWjd7ZarCw5aJqdQDxXgXu1J8Jvy wxOUUcjKF69+uoYFZvoM7bQRMSVE1Mk8CJGxr3wGddQTklman/lNz08ZcqfdimIK/nCB OJGvd6W/YquDEbtzz/wXV/0QVWMClE1RbgMA95qJBHq/sKN+djLrynNJTQ1rESy8XaMa GFnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=0lQ+C2SrlCZ95gPfzCRomXEliMjquqP8n9e8yEcEZlk=; b=uMlF/fOb9my5KdTxSKwFNjKsNeIMABnsgpPFO8Bba2RDjq7MzAWZnBC8k8lpZLIlC9 jQDyQ88mrO6q5hVMD9X1RHnBW3q2xJSnp+ysH5WNN9z4qjikXoyhSnxx8JWxPGECjwiT M78waAxibF1nMCSPGDUjkB+tajr5v8LuQs+Sx0nKOMXoLTMcPyWqzaAlbiAcIYtpzb2u THlHiz19Y5k4aA0Tgr4d532hy7vb5FGJd+kguYpMLdIwWk3YY7e9LAuylwjsN4zpfFQ3 Gn29/oJ7Mkbpn9TgGtl7n/MlDKX+vS7f9UwLpNiZjPRbDo6+QzkGhb6OaQ0sJebYIkZ7 rBjg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ba26si12686982edb.40.2021.04.08.04.09.42; Thu, 08 Apr 2021 04:10:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230434AbhDHLIm (ORCPT + 99 others); Thu, 8 Apr 2021 07:08:42 -0400 Received: from foss.arm.com ([217.140.110.172]:44076 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229803AbhDHLIl (ORCPT ); Thu, 8 Apr 2021 07:08:41 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3010F106F; Thu, 8 Apr 2021 04:08:30 -0700 (PDT) Received: from C02TD0UTHF1T.local (unknown [10.57.24.62]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3B5303F73D; Thu, 8 Apr 2021 04:08:26 -0700 (PDT) Date: Thu, 8 Apr 2021 12:08:15 +0100 From: Mark Rutland To: Will Deacon Cc: Rob Herring , Catalin Marinas , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Jiri Olsa , Ian Rogers , Alexander Shishkin , Honnappa Nagarahalli , Zachary.Leaf@arm.com, Raphael Gault , Jonathan Cameron , Namhyung Kim , Itaru Kitayama , linux-arm-kernel , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v6 02/10] arm64: perf: Enable PMU counter direct access for perf event Message-ID: <20210408110800.GA32792@C02TD0UTHF1T.local> References: <20210311000837.3630499-1-robh@kernel.org> <20210311000837.3630499-3-robh@kernel.org> <20210330153125.GC6567@willie-the-truck> <20210331160059.GD7815@willie-the-truck> <20210407124437.GA15622@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210407124437.GA15622@willie-the-truck> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 07, 2021 at 01:44:37PM +0100, Will Deacon wrote: > [Moving Mark to To: since I'd like his view on this] > > On Thu, Apr 01, 2021 at 02:45:21PM -0500, Rob Herring wrote: > > On Wed, Mar 31, 2021 at 11:01 AM Will Deacon wrote: > > > > > > On Tue, Mar 30, 2021 at 12:09:38PM -0500, Rob Herring wrote: > > > > On Tue, Mar 30, 2021 at 10:31 AM Will Deacon wrote: > > > > > > > > > > On Wed, Mar 10, 2021 at 05:08:29PM -0700, Rob Herring wrote: > > > > > > From: Raphael Gault > > > > > > +static void armv8pmu_event_unmapped(struct perf_event *event, struct mm_struct *mm) > > > > > > +{ > > > > > > + struct arm_pmu *armpmu = to_arm_pmu(event->pmu); > > > > > > + > > > > > > + if (!(event->hw.flags & ARMPMU_EL0_RD_CNTR)) > > > > > > + return; > > > > > > + > > > > > > + if (atomic_dec_and_test(&mm->context.pmu_direct_access)) > > > > > > + on_each_cpu_mask(&armpmu->supported_cpus, refresh_pmuserenr, mm, 1); > > > > > > > > > > Given that the pmu_direct_access field is global per-mm, won't this go > > > > > wrong if multiple PMUs are opened by the same process but only a subset > > > > > are exposed to EL0? Perhaps pmu_direct_access should be treated as a mask > > > > > rather than a counter, so that we can 'and' it with the supported_cpus for > > > > > the PMU we're dealing with. > > > > > > > > It needs to be a count to support multiple events on the same PMU. If > > > > the event is not enabled for EL0, then we'd exit out on the > > > > ARMPMU_EL0_RD_CNTR check. So I think we're fine. > > > > > > I'm still not convinced; pmu_direct_access is shared between PMUs, so > > > testing the result of atomic_dec_and_test() just doesn't make sense to > > > me, as another PMU could be playing with the count. > > > > How is that a problem? Let's make a concrete example: > > > > map PMU1:event2 -> pmu_direct_access = 1 -> enable access > > map PMU2:event3 -> pmu_direct_access = 2 > > map PMU1:event4 -> pmu_direct_access = 3 > > unmap PMU2:event3 -> pmu_direct_access = 2 > > unmap PMU1:event2 -> pmu_direct_access = 1 > > unmap PMU1:event4 -> pmu_direct_access = 0 -> disable access > > > > The only issue I can see is PMU2 remains enabled for user access until > > the last unmap. But we're sharing the mm, so who cares? Also, in this > > scenario it is the user's problem to pin themselves to cores sharing a > > PMU. If the user doesn't do that, they get to keep the pieces. > > I guess I'm just worried about exposing the counters to userspace after > the PMU driver (or perf core?) thinks that they're no longer exposed in > case we leak other events. IMO that's not practically different from the single-PMU case (i.e. multi-PMU isn't material, either we have a concern with leaking or we don't); more on that below. While it looks odd to place this on the mm, I don't think it's the end of the world. > However, I'm not sure how this is supposed to work normally: what > happens if e.g. a privileged user has a per-cpu counter for a kernel > event while a task has a counter with direct access -- can that task > read the kernel event out of the PMU registers from userspace? Yes -- userspace could go read any counters even though it isn't supposed to, and could potentially infer information from those. It won't have access to the config registers or kernel data structures, so it isn't guaranteed to know what the even is or when it is context-switched/reprogrammed/etc. If we believe that's a problem, then it's difficult to do anything robust other than denying userspace access entirely, since disabling userspace access while in use would surprise applications, and denying privileged events would need some global state that we consult at event creation time (in addition to being an inversion of privilege). IIRC there was some fuss about this a while back on x86; I'll go dig and see what I can find, unless Peter has a memory... Thanks, Mark.