Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3806596pxk; Tue, 29 Sep 2020 06:48:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwb4OuEsracOJ4o1XXsm/xyi6lq7Pz2LtIfK5zx10meYLStJmp2itw8CWNchpeUfhP2fN4Y X-Received: by 2002:a05:6402:16c9:: with SMTP id r9mr3276041edx.27.1601387323473; Tue, 29 Sep 2020 06:48:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601387323; cv=none; d=google.com; s=arc-20160816; b=yv+1lgP6WZWOhqJ4X4SUW09yOAkLL2lMiGbvZWdkRHy5z+wKJOkJye4VvLL75czfT0 NmBJYqM8kRmmIScPsYF64ZIsO8QLsYzfrZe/snW+DCoPjjDKu4XX2TnCYwrZAvZwM9pf v1KNpD5tQIVEPUQlpjtUru5RmD5shQ2CzshfZVisq9wpm5VJxvjrB2IWl2mHQFleym9L J83d9RXaWgoM3NyDvKIQmZTspawhxoEkN0cBkQXXKNbFg3mAUSwyMQK20dtY3ZUm7Tw9 qpO1yucHNchkI0CWMkWvBq/yNwFJmfsP+g+1GXQgNicgQPP6gbNhwDQGCmLOtBRpgX0u RVCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=856C2B8iCwEtBC3mCyDL0tsK7AYhEBuuSXVVGhg5UEo=; b=YpiC9Bi/ivYmM2zKFrqOgc0ojBNG9nXUgSrXsdlZoNZgCNLKjpkhsQ9xkOmZRKNPIM KSmKxoREGP9DdakrazuRlcxxOF/MPgHNiKMEDA1G/5BtvoIWFN8MgK5/ZtWOYnWMPbyM GdlMn2z/rwG39lfbqxH3XuoUVXYyxn8QBUVsvYSev/Cus/CkAWgHuefnyoz+uEK5G73f MBdU4PXqzqfcDspQrFBdNe+QOQN9mRAMGJI3OuFAmBkEuncwJTnClhT9MB5ho1q9OOcW 5cqhli1uwVwZN1WoVcn+iPufvJgEDOQw2kxKKU+wI716S2zyYN/lNF78FhMYz4/5lpTW ZTGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=RTD2HHxY; 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 lr3si3063535ejb.744.2020.09.29.06.48.20; Tue, 29 Sep 2020 06:48:43 -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; dkim=pass header.i=@kernel.org header.s=default header.b=RTD2HHxY; 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 S1730387AbgI2NrF (ORCPT + 99 others); Tue, 29 Sep 2020 09:47:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:34894 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729858AbgI2Nq7 (ORCPT ); Tue, 29 Sep 2020 09:46:59 -0400 Received: from mail-oo1-f47.google.com (mail-oo1-f47.google.com [209.85.161.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 58C8C21924 for ; Tue, 29 Sep 2020 13:46:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601387218; bh=g8XcAgcDdCLuUnEGsc4x5tUH7M2XpVkS8jU3ISBpim8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=RTD2HHxYM7e/CJXp+98tgz6dnMbrFfMhcTmBottNdz3wL156TAmFU90+XU+XevIb+ fhVOtCeRJwwZ49CBogToraL7lVha3Lj8AhQf/hwcg1gqVFffKB/njPh8n3gEQtrYkL 8tAQ9AGASJ8/YdQBPQfi4syXGJExBvIpmiyjuWoM= Received: by mail-oo1-f47.google.com with SMTP id g26so1277517ooa.9 for ; Tue, 29 Sep 2020 06:46:58 -0700 (PDT) X-Gm-Message-State: AOAM533a1ABtou1b1AQxXy0DV+ChR0JpVWa9h3maHwAyAadsir74kBcd Rjj4wekP1En5KcJ5nH2ffBn2RElXXNCEmd7NrQ== X-Received: by 2002:a4a:d306:: with SMTP id g6mr4688913oos.25.1601387217467; Tue, 29 Sep 2020 06:46:57 -0700 (PDT) MIME-Version: 1.0 References: <20200911215118.2887710-1-robh@kernel.org> <20200911215118.2887710-2-robh@kernel.org> <20200928182601.GA11974@willie-the-truck> In-Reply-To: <20200928182601.GA11974@willie-the-truck> From: Rob Herring Date: Tue, 29 Sep 2020 08:46:46 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 01/10] arm64: pmu: Add hook to handle pmu-related undefined instructions To: Will Deacon Cc: Catalin Marinas , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Jiri Olsa , "linux-kernel@vger.kernel.org" , linux-arm-kernel , Alexander Shishkin , Namhyung Kim , Raphael Gault , Mark Rutland , Jonathan Cameron , Ian Rogers , Honnappa Nagarahalli Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 28, 2020 at 1:26 PM Will Deacon wrote: > > On Fri, Sep 11, 2020 at 03:51:09PM -0600, Rob Herring wrote: > > From: Raphael Gault > > > > This patch introduces a protection for the userspace processes which are > > trying to access the registers from the pmu registers on a big.LITTLE > > environment. It introduces a hook to handle undefined instructions. > > > > The goal here is to prevent the process to be interrupted by a signal > > when the error is caused by the task being scheduled while accessing > > a counter, causing the counter access to be invalid. As we are not able > > to know efficiently the number of counters available physically on both > > pmu in that context we consider that any faulting access to a counter > > which is architecturally correct should not cause a SIGILL signal if > > the permissions are set accordingly. > > > > This commit also modifies the mask of the mrs_hook declared in > > arch/arm64/kernel/cpufeatures.c which emulates only feature register > > access. This is necessary because this hook's mask was too large and > > thus masking any mrs instruction, even if not related to the emulated > > registers which made the pmu emulation inefficient. > > > > Signed-off-by: Raphael Gault > > Signed-off-by: Rob Herring > > --- > > v2: > > - Fix warning for set but unused sys_reg > > --- > > arch/arm64/kernel/cpufeature.c | 4 +-- > > arch/arm64/kernel/perf_event.c | 54 ++++++++++++++++++++++++++++++++++ > > 2 files changed, 56 insertions(+), 2 deletions(-) > > > > diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c > > index a389b999482e..00bf53ffd9b0 100644 > > --- a/arch/arm64/kernel/cpufeature.c > > +++ b/arch/arm64/kernel/cpufeature.c > > @@ -2811,8 +2811,8 @@ static int emulate_mrs(struct pt_regs *regs, u32 insn) > > } > > > > static struct undef_hook mrs_hook = { > > - .instr_mask = 0xfff00000, > > - .instr_val = 0xd5300000, > > + .instr_mask = 0xffff0000, > > + .instr_val = 0xd5380000, > > .pstate_mask = PSR_AA32_MODE_MASK, > > .pstate_val = PSR_MODE_EL0t, > > .fn = emulate_mrs, > > diff --git a/arch/arm64/kernel/perf_event.c b/arch/arm64/kernel/perf_event.c > > index 462f9a9cc44b..70538ae684da 100644 > > --- a/arch/arm64/kernel/perf_event.c > > +++ b/arch/arm64/kernel/perf_event.c > > @@ -8,9 +8,11 @@ > > * This code is based heavily on the ARMv7 perf event code. > > */ > > > > +#include > > #include > > #include > > #include > > +#include > > #include > > > > #include > > @@ -1016,6 +1018,58 @@ static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu) > > return probe.present ? 0 : -ENODEV; > > } > > > > +static int emulate_pmu(struct pt_regs *regs, u32 insn) > > +{ > > + u32 rt; > > + u32 pmuserenr; > > + > > + rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn); > > + pmuserenr = read_sysreg(pmuserenr_el0); > > + > > + if ((pmuserenr & (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR)) != > > + (ARMV8_PMU_USERENR_ER|ARMV8_PMU_USERENR_CR)) > > + return -EINVAL; > > + > > + > > + /* > > + * Userspace is expected to only use this in the context of the scheme > > + * described in the struct perf_event_mmap_page comments. > > + * > > + * Given that context, we can only get here if we got migrated between > > + * getting the register index and doing the MSR read. This in turn > > + * implies we'll fail the sequence and retry, so any value returned is > > + * 'good', all we need is to be non-fatal. > > + * > > + * The choice of the value 0 is comming from the fact that when > > + * accessing a register which is not counting events but is accessible, > > + * we get 0. > > + */ > > + pt_regs_write_reg(regs, rt, 0); > > Hmm... this feels pretty fragile since, although we may expect userspace only > to trigger this in the context of the specific perf use-case, we don't have > a way to detect that, so the ABI we're exposing is that EL0 accesses to > non-existent counters will return 0. I don't really think that's something > we want to commit to. > > When restartable sequences were added to the kernel, one of the proposed > use-cases was to allow PMU access on big/little systems, because the > sequence will abort on preemption. Taking that approach removes the need > for this emulation hook entirely. Is that something we can rely on instead > of this emulation hook? So back to the RFC version[1]!? That would mean pulling librseq into the kernel based on the prior discussion. It doesn't look like that has happened yet. Why not just drop the undef hook? For heterogeneous systems, we require userspace to pin itself to cores for a specific PMU. See patch 9. If userspace fails to do that, then it gets to keep the pieces. Rob [1] https://lore.kernel.org/linux-arm-kernel/20190625134117.r3gysn7mvzzdrytj@willie-the-truck/