Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp754074pxb; Thu, 21 Oct 2021 08:52:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwG3lJIWrALV8+ETkNAovMIjiqn9MI5Fx6E9HQ08xUzSUDukI1lzGSoytvqFJ6QVP+ETks9 X-Received: by 2002:a17:906:84e:: with SMTP id f14mr8261052ejd.105.1634831554637; Thu, 21 Oct 2021 08:52:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634831554; cv=none; d=google.com; s=arc-20160816; b=bTTknPoJKSQLMOzk70ZCUJrtgW2br5EGFXQLtDX9WJ5r69i2mQxha1f5MB41Ka5pwX dfl8zwX7IbPePRZjU+ZbeopH2p4K23hwC9wklnpVA38baPFFR/J314LWKVvo2pAGE73C F/D73LPQLCHghd4E5yi/QfkmJ07T4nQhuvESt1e4OoxAntthX9Ns3eA6ITnxQmmReCo7 qRDoA+GAKXA2JjtupzBedb8rDMNsh+NUipV57ookHy9MiHCVf45bOyev+10KkmKyvxNn hfl5TheSMV7WRkcN1BzpRK2QvCcToVXKc+HaJLIi0k3rre77IqELoUZQdnx13KjP08SM lacg== 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:dkim-signature; bh=LjkqvoWTymfCy+YcaZh0ZqmF+EOwMe7EIAue/ES2zZI=; b=O0Hyb3QcdQTQg7TYI+rn/p1RHHALhK3pJ3DsHPEdjJGTrciY/6qpjW9rbUZLZV5R8p 2EBqyHBeWruxJzgLZ853oKZIzCepQysfVAfDei2miWQa8RLX9RE4tRy2ujTF7LijvZSK jfP/Fff2E0jR/dHiUfQYoH5zfo56AN1lNe+kGtVwSVEi52TYh5F3yzyWL25iZoIgqy11 XRbvfRNVhrWVaMGWPggU+jXSEWsSngvYOxjUb5J+3/iEidmsHBsjkXczPW6nztD+UywM k9kldcsFvOWETWyAZ9fKoXd5LJCgtZnP6MdRdo85Uxw9nkKX/ab3mAIVTY8Qe/+Kd69V 2F+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CKOZQwON; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t6si9513125edd.621.2021.10.21.08.52.09; Thu, 21 Oct 2021 08:52:34 -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=@chromium.org header.s=google header.b=CKOZQwON; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231509AbhJUPtu (ORCPT + 99 others); Thu, 21 Oct 2021 11:49:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230187AbhJUPts (ORCPT ); Thu, 21 Oct 2021 11:49:48 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B95CBC061764 for ; Thu, 21 Oct 2021 08:47:31 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id na16-20020a17090b4c1000b0019f5bb661f9so883520pjb.0 for ; Thu, 21 Oct 2021 08:47:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=LjkqvoWTymfCy+YcaZh0ZqmF+EOwMe7EIAue/ES2zZI=; b=CKOZQwON6vxm5fh94b90jypEEwVdTo/XS3/hpKg8aWp4Q3bRT2RoOivhdvZiLyvWSj VwB2cy4/Zon7lOfSIpRV0SCkDz6vRE5NSNBnXBJaSvYyzQ0wRePr5maWpgkVcyz54dzs yan3eA4lYTfpE1UJs594hRujOZzkgWhWDs0ng= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=LjkqvoWTymfCy+YcaZh0ZqmF+EOwMe7EIAue/ES2zZI=; b=lSl+37kFiMF3Kt4+STUMaHWtNOZI5/Ilks8n9YERvyYB28Bc0L9HHPRbT1uGj6C7Jt PkhhRUAmBBXOwDcr04KRiNatmEkx23dnr6/AlOkp9opblhiJYwN4buKlsNgu29JxZhhd C0ggcrh+R0itPuAbcc/wYJ9/+y1wJn8DiOEW+cFhO3oT8Z+H7MfIvpbII3CNsOK8OjLN KhHWHG8q0Lo2V7Idhlmfyx9rgKQMZptQ2NKp2rK31qn4TcnjKZk8kMYnxZnnYEvlNyyG ccvHqctGBAICL/QMF323JylOjY/cb8CrRMVVehAeN+yORzfgBu1hd+1EdQZG+2bA07Ko 4N/A== X-Gm-Message-State: AOAM530w/6K9WS94yTV0YKoE78e61F5ShIaEBCbAZHOkmt3NjqHZEDV0 kL8XDPV/iLEJG23vjJx1MuZ8BHf5JBU= X-Received: by 2002:a17:90b:3ec6:: with SMTP id rm6mr7568949pjb.27.1634831251225; Thu, 21 Oct 2021 08:47:31 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n22sm6640941pfo.15.2021.10.21.08.47.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 08:47:30 -0700 (PDT) Date: Thu, 21 Oct 2021 08:47:30 -0700 From: Kees Cook To: James Clark Cc: Leo Yan , Catalin Marinas , Will Deacon , Mark Rutland , Ard Biesheuvel , Sami Tolvanen , Nicholas Piggin , James Morse , Marc Zyngier , Joey Gouly , Peter Collingbourne , Vincenzo Frascino , "Peter Zijlstra (Intel)" , Stephane Eranian , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [RFCv1 1/4] arm64: Use static key for tracing PID in CONTEXTIDR Message-ID: <202110210846.8A7B9F684@keescook> References: <20211021134530.206216-1-leo.yan@linaro.org> <20211021134530.206216-2-leo.yan@linaro.org> <53962765-53b9-dfdc-a5b2-a3133a924c12@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <53962765-53b9-dfdc-a5b2-a3133a924c12@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 21, 2021 at 03:33:01PM +0100, James Clark wrote: > > > On 21/10/2021 14:45, Leo Yan wrote: > > The kernel provides CONFIG_PID_IN_CONTEXTIDR for tracing PID into system > > register CONTEXTIDR; we need to statically enable this configuration > > when build kernel image to use this feature. > > > > On the other hand, hardware tracing modules (e.g. Arm CoreSight, SPE, > > etc) rely on this feature to provide context info in their tracing data. > > If kernel has not enabled configuration CONFIG_PID_IN_CONTEXTIDR, then > > tracing modules have no chance to capture PID related info. > > > > This patch introduces static key for tracing PID in CONTEXTIDR, it > > provides a possibility for device driver to dynamically enable and > > disable tracing PID into CONTEXTIDR as needed. > > > > As the first step, the kernel increases the static key if > > CONFIG_PID_IN_CONTEXTIDR is enabled when booting kernel, in this case > > kernel will always trace PID into CONTEXTIDR at the runtime. This means > > before and after applying this patch, the semantics for > > CONFIG_PID_IN_CONTEXTIDR are consistent. > > > > Signed-off-by: Leo Yan > > --- > > arch/arm64/include/asm/mmu_context.h | 4 +++- > > arch/arm64/kernel/process.c | 11 +++++++++++ > > 2 files changed, 14 insertions(+), 1 deletion(-) > > > > diff --git a/arch/arm64/include/asm/mmu_context.h b/arch/arm64/include/asm/mmu_context.h > > index f4ba93d4ffeb..e1f33616f83a 100644 > > --- a/arch/arm64/include/asm/mmu_context.h > > +++ b/arch/arm64/include/asm/mmu_context.h > > @@ -26,9 +26,11 @@ > > > > extern bool rodata_full; > > > > +DECLARE_STATIC_KEY_FALSE(contextidr_in_use); > > + > > static inline void contextidr_thread_switch(struct task_struct *next) > > { > > - if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR)) > > + if (!static_branch_unlikely(&contextidr_in_use)) > > return; > > > > write_sysreg(task_pid_nr(next), contextidr_el1); > > diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c > > index 40adb8cdbf5a..d744c0c7e4c4 100644 > > --- a/arch/arm64/kernel/process.c > > +++ b/arch/arm64/kernel/process.c > > @@ -61,6 +61,9 @@ unsigned long __stack_chk_guard __ro_after_init; > > EXPORT_SYMBOL(__stack_chk_guard); > > #endif > > > > +DEFINE_STATIC_KEY_FALSE(contextidr_in_use); > > +EXPORT_SYMBOL_GPL(contextidr_in_use); > > + > > /* > > * Function pointers to optional machine specific functions > > */ > > @@ -721,3 +724,11 @@ int arch_elf_adjust_prot(int prot, const struct arch_elf_state *state, > > return prot; > > } > > #endif > > + > > +static int __init contextidr_init(void) > > +{ > > + if (IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR)) > > + static_branch_inc(&contextidr_in_use); > > + return 0; > > +} > > +early_initcall(contextidr_init); > > Hi Leo, > > Can you skip this early_initcall() part if you do something like this: > > DECLARE_STATIC_KEY_MAYBE(CONFIG_PID_IN_CONTEXTIDR, contextidr_in_use) > > It seems like there is a way to conditionally initialise it to true. I was going to suggest the same thing. :) With this change, it looks good: Reviewed-by: Kees Cook -- Kees Cook