From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F573C433F5 for ; Thu, 21 Oct 2021 15:48:47 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 46EF460D42 for ; Thu, 21 Oct 2021 15:48:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 46EF460D42 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=HHSrosfYPvDfOnjqC8waInsP1sPvi7n2XFUz0Kx+tS8=; b=JGhzlZVSQKKFjF U82SezE8zOIk35FRtNTG5G4ac9zZpWAn0tDQ8iIPS5tXA6aq+pfcemCwVXdy9RK6onRXil9l1nsDj 7pRMt+FzGR9rZB+iD+bvVS1K9KBIKTnTyd8p6qLfyurDhs/iZCNQZvCAtviCz9lrL/acwKb3ZQYqr 5FT+irgc9RunlV0IO0SUiMT3oxE94bY6yNYF1uyiMLc5UzXSjbmQSzsUNbAtMnHG++kFT0O/+tNLd apXtuqU1iJsZ+8A+sbaZxhj7xFeuHMu2nh9mgaqe7JREycJ89e/2ntCBngKMKJAhQ4bmkOlBVRwov oUqZydWVswuquZ2xdDUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdaI5-008DRK-J4; Thu, 21 Oct 2021 15:47:37 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdaI2-008DPH-BZ for linux-arm-kernel@lists.infradead.org; Thu, 21 Oct 2021 15:47:35 +0000 Received: by mail-pl1-x631.google.com with SMTP id s1so682951plg.12 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=dJACllL6jcPbMgZEMtp/w0IuxnjKCL+3UDkhZKwber+rjmq/7yGl1KUfeIYLvl85+G AgnlnngOlBaa9Vq/Ru2guSwG7I+8VlBHoPNLuJW6/c9LusEQi8qAPWj8xn2Pfc/fI1v1 qItgo9mCHofDFdQkkF86vtplLn/pZk4mfuG0IVsxK500/eqVo1/zWwTvQVnLWywGeKjK eaFj5mRGdnKo3eGGdZ/tcD+42Yx8Fqb4daqSQORZq0gxnqKGMzW0LeMKLb+8lhcJAxYi lBLHA2Iq0Tz8AoQF0jF6jHGpmitiLdhVZeGNl62b0hQGQR9DHstr5WUMnrKe1dpu3Lt+ klhg== X-Gm-Message-State: AOAM532rmCAbA6M1pbOlBtnJAG2//OlOAA1OOQQ09OBwDC667kijAH+m PZB1OepXSpPiM6/zXzhul6ta+Q== X-Google-Smtp-Source: ABdhPJzWRRaUhrJPndRABVKVdgJ1OpmaLHbcaLDagTz8RC8SHv0taaYpAJEqvR/gJDrn5/MKGviahw== 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-Disposition: inline In-Reply-To: <53962765-53b9-dfdc-a5b2-a3133a924c12@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211021_084734_443597_AE65BF49 X-CRM114-Status: GOOD ( 33.36 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel