Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp6156637rwl; Mon, 9 Jan 2023 05:03:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXvbEDZdxR491p9o3oJcn4S70uOhzvLx2iJA+I5U256S20uYxV7MNBC9TSEvWcMDbxeUof8I X-Received: by 2002:aa7:d393:0:b0:490:47c3:3d7f with SMTP id x19-20020aa7d393000000b0049047c33d7fmr14066166edq.2.1673269411840; Mon, 09 Jan 2023 05:03:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673269411; cv=none; d=google.com; s=arc-20160816; b=XE8FOtME+2ETmHZgQEsCvytAO39F37QpG6d9FpF6dn9HB7X0qPXkmnz6JL9kvvd69n AKq0aMRAclt0+tgo34xh3FejciIJNr6lTXu8JJa7wq2DQKx9SZUyggi9UtZ54FCb/rRk 0cOGf7mnlIzJGxXnCsH0BxRbAc+/zU9Nyt2pNL4yLGa+XJOa7ZsqvQVxzecPpGshd3kh 3DPzBk9ALDqkBUyxyC3ooF4cKRzgmG9SGlUHRAco7g5NF7N+Ips9OUAcjYU1duzQV5LY gxe5v5f2RIzJzi7dxURo/tUcz95amsnB9H3MV0NxX8jJOJzS+TGZoBJeD2XPZNBdOOt0 lHKA== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=uoWLYWZtTN5ZtWcVNElvXtV3FLHQfyD/J4yUz9n0OU4=; b=NwyLcCkwnOQmGOTwlIfimT1TB5G72jxCNikuHQOJeYeYrljgwnHD5X0i1RmaF+8KNs pY1aBOPQK8+AfmhaXqeJpdp3s14mNHom0CO1oACDJb4X3lGOSmW2enpbVsN6XPK+dqRx xEmVV0/9wo9hltfDVTwSPYd8qZ1aVfnxGhHp6Fmd2qBPrBqUJ1pL8r2X2Q67ySa/dduo YzcIwRrO2ziiGcLMjQMR2vMIp+j5xMPQG5mnttO9IkrY0CAt7oZ7aQHbp7OmltKdtFAZ Biu+JuM0fDIPVXuUfkbg8WbJ5VXu2Map/Tnzdo/tFanUj/Muj916i0uDLb0TNrie57iG pEzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=RQkP46LB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p24-20020a50cd98000000b00490c0e5c369si9023917edi.86.2023.01.09.05.03.18; Mon, 09 Jan 2023 05:03:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=RQkP46LB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234919AbjAIMlg (ORCPT + 54 others); Mon, 9 Jan 2023 07:41:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229865AbjAIMl2 (ORCPT ); Mon, 9 Jan 2023 07:41:28 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58B8215F13; Mon, 9 Jan 2023 04:41:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=uoWLYWZtTN5ZtWcVNElvXtV3FLHQfyD/J4yUz9n0OU4=; b=RQkP46LBZU9Ak6iH97viki6JVO qWgje3MyLedr138OMO67cuuHxYhiG03oEEn4np304QhQjzvqdx8kYieVIQRS0IOioLlimzXpPBXtN 7+n0WJyUiA93kiggn8pkB9Of1AaAtnmxjHxR6U5WlSPOqMZg8AQ+5zEXxYZV7R9MoCGUUbUDQ9wjZ u5nR3QcrcInd4ySpKw+RwQOx31pNUiXT5iFV9s7bAC5DBhd6p6x7tAb/QIXSEDZHwtUy18S30dxBU wIIKISIcKuQsjAeWmpyd70lYmdrT23D5lNWEa6IH6rmP2pm69Hs8/kbOZpechHv8Onsl1YkckXwYB kBkSwfTw==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1pErSy-002H2w-Kf; Mon, 09 Jan 2023 12:41:28 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 8ECE2300193; Mon, 9 Jan 2023 13:41:15 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 66397201ABB81; Mon, 9 Jan 2023 13:41:15 +0100 (CET) Date: Mon, 9 Jan 2023 13:41:15 +0100 From: Peter Zijlstra To: Atish Patra Cc: linux-perf-users@vger.kernel.org, "linux-kernel@vger.kernel.org List" , Mark Rutland , Arnaldo Carvalho de Melo , Alexander Shishkin , Will Deacon , Stephane Eranian , Andi Kleen , Palmer Dabbelt , Beeman Strong , Atish Patra , Kan Liang , Anup Patel Subject: Re: Expected rdpmc behavior during context swtich and a RISC-V conundrum Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 05, 2023 at 11:59:24AM -0800, Atish Patra wrote: > Hi All, > There was a recent uabi update[1] for RISC-V that allows the users to > read cycle and instruction count without any checks. > We tried to restrict that behavior to address security concerns > earlier but it resulted in breakage for some user space > applications[2]. > Thus, previous behavior was restored where a user on RISC-V platforms > can directly read cycle or instruction count[3]. > > Comparison with other ISAs w.r.t user space access of counters: > ARM64 > -- Enabled/Disabled via (/proc/sys/kernel/perf_user_access) > -- Only for task bound events configured via perf. > > X86 > --- rdpmc instruction > --- Enable/Disable via “/sys/devices/cpu/rdpmc” > -- Before v4.0 > -- any process (even without active perf event) rdpmc > After v4.0 > -- Default behavior changed to support only active events in a > process’s context. > -- Configured through perf similar to ARM64 > -- Continue to maintain backward compatibility for unrestricted access > by writing 2 to “/sys/devices/cpu/rdpmc” > > IMO, RISC-V should only enable user space access through perf similar > to ARM64 and x86 (post v4.0). > However, we do have to support the legacy behavior to avoid > application breakage. > As per my understanding a direct user space access can lead to the > following problems: > > 1) There is no context switch support, so counts from other contexts are exposed > 2) If a perf user is allocated one of these counters, the counter > value will be written > > Looking at the x86 code as it continues to allow the above behavior, > rdpmc_always_available_key is enabled in the above case. However, > during the context switch (cr4_update_pce_mm) > only dirty counters are cleared. It only prevents leakage from perf > task to rdpmc task. > > How does the context switch of counters work for users who enable > unrestricted access by writing 2 to “/sys/devices/cpu/rdpmc” ? > Otherwise, rdpmc users likely get noise from other applications. Is > that expected ? > This can be a security concern also where a rogue rdpmc user > application can monitor other critical applications to initiate side > channel attack. > > Am I missing something? Please correct my understanding of the x86 > implementation if it is wrong. So on x86 we have RDTSC and RDPMC instructions. RDTSC reads the Time-Stamp-Counter which is a globally synchronized monotonic increasing counter at some 'random' rate (idealized, don't ask). This thing is used for time-keeping etc.. And then there's RDPMC which (optionally) allows reading the PMU counters which are normally disabled and all 0. Even if RDPMC is unconditionally allowed from userspace (the 2 option you refer to) userspace will only be able to read these 0s unless someone also programs the PMU. Linux only supports a single means of doing so: perf (some people use /dev/msr to poke directly to the MSRs but they get to keep all pieces). RDPMC is only useful if you read counters you own on yourself -- IOW selfmonitoring, using the interface outlined in uapi/linux/perf_events.h near struct perf_event_mmap_page. Any other usage -- you get to keep the pieces. Can you observe random other counters, yes, unavoidably so. The sysfs control you mention was instituted to restrict this somewhat. If the RISC-V counters are fundamentally the PMU counters that need to be reset to trigger events, then you've managed to paint yourself into a tight spot :/ Either you must dis-allow userspace access to these things (and break them) or limit the PMU usage -- both options suck. Now, I'm thinking that esp. something like instruction count is not synchronized between cores (seems fundamentally impossible) and can only be reasonably be consumed (and compared) when strictly affine to a particular CPU, you can argue that applications doing this without also strictly managing their affinity mask are broken anyway and therefore your breakage is not in fact a breaking them -- you can't break something that's already broken. Anyway, given RISC-V being a very young platform, I would try really *really* *REALLY* hard to stomp on these applications and get them to change in order to reclaim the PMU usage.