Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp5506049pxb; Wed, 19 Jan 2022 21:22:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJwiZ29V3en6JPUogLZaQo+a9yb4rgAxl6aeyszdqdpE8+k1z5zm1b229cmkaDQ/uYgdccDf X-Received: by 2002:a17:902:8d91:b0:14a:486a:ac7 with SMTP id v17-20020a1709028d9100b0014a486a0ac7mr35840891plo.101.1642656164375; Wed, 19 Jan 2022 21:22:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642656164; cv=none; d=google.com; s=arc-20160816; b=fSR78ONguf3I7EptjtGzmmZGsNWnylG1l4tul5enS5JF9V6qezUhzWRNnh8Fy0+Bp5 gij7lfQlcsIfpjaA6Ss/d4LAmQcqwRtrUre3GYocKW6oiJfPrUO/oR4q66DtewcQsK9j r99R5N5B71ud60pW3fZ+ir7yuGRwNmX3x6VTVZQNs6Csmh+Oy4wvrNoYYQrp3etruCm9 wAxNeeNzqMieACHpqrlbAB9J+MMoSWs16yZdWIMKJa4+DKqF0xFHcmvccYsT2vx8GBxr Yp0nbH1QJLhB7eGeIwH2pIB0u7ULkrwSO2lyg69CXD9xfeLEDSJX0jU+lzrZH+BxXble bU7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=4LYDLFul0cO7iDMSOVKaaja59ofyWzsVuEsoTj2Z64k=; b=Kx7Uiw9T0n3ho0bJnAfs3EdMlM0ViHj60siZrqae79hFJHYk/l10UPdhYsOgSgUvCw bwTUkiXzr2Qt67ZmI6Idy+uyH0vJHc5+0LMnIM67pmHVIM5YZiWhDKiGqJK/HUe1xzPM e9fsdAcGJl1ZDbvduCJX6wjDBeUWxBtUgY43xGpz2hhXL4F70P+EbwHCX6CkP4eAhBIJ TswZz1RwNfAeftTNqCBBUnphXz5wzwwoDpXMETyWlnwAnrqRfxQEoQZGluWGMJVEn3hJ J1nB+OJwvQEpBh2jou5xbZFXM5+RRC9e+cD/XZwVCL4AgWeJUn00+IPrlp0e/RkPVa32 on2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hg9Z5PRj; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g10si2071512pgc.517.2022.01.19.21.22.21; Wed, 19 Jan 2022 21:22:44 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hg9Z5PRj; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236480AbiARKHK (ORCPT + 99 others); Tue, 18 Jan 2022 05:07:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236434AbiARKHK (ORCPT ); Tue, 18 Jan 2022 05:07:10 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A75D4C061574 for ; Tue, 18 Jan 2022 02:07:09 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 68AA8B812A5 for ; Tue, 18 Jan 2022 10:07:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5931C00446; Tue, 18 Jan 2022 10:07:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642500427; bh=LtGI7ReI1VAbLJR+tqn3W9qAVWXu25LmWPuXFerZXhI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=hg9Z5PRj0PLyaAfZcZ0nd+RtrzHE3w1vkMa0by9aVLjd6SwARimg+IUMbRee82B2N np7ybr4YgzC1p2EpKJY1Wr3ODdXmJHnXLr8BAL6lU3+pJCjSeXGReJ4xztXjn7s060 spfUotA4H38aFS/1tTrJ4B7JYUbbfI52ofw5hiCrGYTpCWJ2Mn+cHtdkbqBSoWzAvr 55qljGPlUGFYdWByjOKRoQ7E49jEyjnvUu61E7QPnAw+YcQLh4JtLic25I2/o4BXre Db84KAfITczL+QsH6aiqcN0S4B+BWGloJZaqOpmbxbOmNox3SihDkK3EV7XUSfitfQ 29KDcQrXVSk9A== Date: Tue, 18 Jan 2022 10:07:02 +0000 From: Will Deacon To: German Gomez Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, mark.rutland@arm.com, james.clark@arm.com, leo.yan@linaro.org Subject: Re: [RFC PATCH 1/2] perf: arm_spe: Fix consistency of PMSCR register bit CX Message-ID: <20220118100702.GB16547@willie-the-truck> References: <20220117124432.3119132-1-german.gomez@arm.com> <20220117124432.3119132-2-german.gomez@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220117124432.3119132-2-german.gomez@arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 17, 2022 at 12:44:31PM +0000, German Gomez wrote: > The arm_spe_pmu driver will enable the CX bit of the PMSCR register in > order to add CONTEXT packets into the traces if the owner of the perf > event runs with capabilities CAP_PERFMON or CAP_SYS_ADMIN. > > The value of the bit is computed in the arm_spe_event_to_pmscr function > [1], and the check for capabilities happens in [2] in the pmu init > callback. This suggests that the value of the CX bit should remain > consistent for the duration of the perf session. > > However, the function arm_spe_event_to_pmscr may be called later during > the start callback [3] when the "current" process is not the owner of > the perf session, so the CX bit is currently not consistent. Consider > the following example: > > 1. Run a process in the background with capability CAP_SYS_ADMIN in CPU0. > > $ taskset --cpu-list 0 sudo dd if=/dev/random of=/dev/null & > [3] 3806 > > 2. Begin a perf session _without_ capabilities (we shouldn't see CONTEXT packets). > > $ perf record -e arm_spe_0// -C0 -- sleep 1 > $ perf report -D | grep CONTEXT > . 0000000e: 65 df 0e 00 00 CONTEXT 0xedf el2 > . 0000004e: 65 df 0e 00 00 CONTEXT 0xedf el2 > . 0000008e: 65 df 0e 00 00 CONTEXT 0xedf el2 > [...] > > As can be seen, the traces begin showing CONTEXT packets when the pid is > 0xedf (3807). So to be clear: we shouldn't be reporting these packets because 'perf' doesn't have the right capabilities, but we evaluate that in the context of 'dd' (running as root) and so incorrectly grant the permission. Correct? > This happens because the pmu start callback is run when > the current process is not the owner of the perf session, so the CX > register bit is set. This doesn't really seem SPE-specific to me -- the perf_allow_*() helpers also operate implicitly on the current task. How do other PMU drivers avoid falling into this trap? > One way to fix this is by caching the value of the CX bit during the > initialization of the PMU event, so that it remains consistent for the > duration of the session. It doesn't feel right to stash this in 'struct arm_spe_pmu' during event initialisation -- wouldn't that allow perf to continue creating new events with CX set, even if the paranoid sysctl was changed dynamically? Instead, I think it would be better if the capabilities were stash in the event itself somehow at initialisation time. Will