From patchwork Thu Oct 21 13:45:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Yan X-Patchwork-Id: 12575129 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 7A87AC433F5 for ; Thu, 21 Oct 2021 13:47:33 +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 41AF0610A1 for ; Thu, 21 Oct 2021 13:47:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 41AF0610A1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.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:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=aGDgMe2IJgpxxe4d1Z629XLFvvgBZbhoB6SzGtpWrJk=; b=Mh6IxDkfNW7JsQ Mz3Pzfpf9RzSNTMXLEOmSO8sFDRLjOfc0pavLZBp7EVhshvU9JAElJ8GCpN1CBNn5MTdOr6xdncFo IU8ThBdDSRZYKWlFCl0hvb/hb/E3aTlWFaZRwaQgYX3eSsuT4w6cu/uQ1mz0R9upxQvYlO/a+P5BQ FRv5rc+MvUhYlaugOOWUYRvCFqlVDy/OPDq9mep3S3xdh6aZpWiiQqtnKhq/UgQz/OoD1YpjsNRfA R633vX06y4rFOHHuXSIzJm3VUtmETNy8RVU1kaoonT8Xinl6Q/e5IClq6luqbG4lLstpdjXv1QXIY R1rNDi3nWMtCaiLuoxVA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdYON-007gbD-1A; Thu, 21 Oct 2021 13:45:59 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mdYOA-007gW8-7m for linux-arm-kernel@lists.infradead.org; Thu, 21 Oct 2021 13:45:47 +0000 Received: by mail-pj1-x102d.google.com with SMTP id u6-20020a17090a3fc600b001a00250584aso3165028pjm.4 for ; Thu, 21 Oct 2021 06:45:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QDxUA5M+5Tc9pgMaEnmtMzJohvZssOB/8meny4PzmyY=; b=FIUQGFZ7VcUHQw/Lbr3XDxLdHWunjI79HlNGqEknHtJaH3F/YRy6LUuGzshoBeXDYr MZlbyyYmOFBMUpa6fAEa+ahnfh1ILUpy0O2mHlcbfdbBLpbQZjDU85f0ecq/OVTqNFpo 18auKJ2PqREqW0jRZ0e9PU0KxPS4QbGaLpDy60P5CZmPgMgI1h3zVgkzB24Yr4EXITPW ngdail0s4XsavPsVm2VlUP3T2DModYPjhY7eKoXJzRQF8Owjo/EOl2dqmyLm+cDUvCwd UjHACR1cuXhbezr2fIn87GzGepfNOhcb5Iw3rxpTvAJDxB65AIgLhK7HiWIFcwtslPLI VuNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QDxUA5M+5Tc9pgMaEnmtMzJohvZssOB/8meny4PzmyY=; b=ysn/0NtUXWzDvDkkjk0W5aatGwpFMbeAEpkGY1+qZZDzfQlh5ZgH1dRg2hGrhqi99q 4wBZXETL7R4ewJQXwKo6rfY55VxOKMxst97kWdW5Uf6tmrLhx32QN60P9QfX0RGU9e/s Yz/crwZ2YhVh+nJ8qajzZQz8RlgBu98J+XL5aX8BOsHQDU3ZuH0TkK8gnQblturd3ZEn TxCPPc2Zb9/2mWwLzprzMVljJ9Ku1sdWUbeYvyKdra3d/qE85WlrAHx6TcrP3gFGbcrl UE6G+FM+sw3UH42wFDfqafdWyDBbg7ZJH+4BpWHtO/wx4JIlaDfUj7KLqNKHrDi6xoG/ lXbg== X-Gm-Message-State: AOAM530C5+CuJVvwCIN3kXyVgr0t0idKl0fC+D9OBL6imtEhZULYgJ+Z YVGUuJqT0Am1EniRm176ldkqFA== X-Google-Smtp-Source: ABdhPJyRF8CtK/W4CjNGZjAYfQW6WxJaeE1/vureHfym+WBfENBF4Q2Cej51bPyc1R3TWXC/9XwKhQ== X-Received: by 2002:a17:902:7001:b0:13d:d5b7:d06e with SMTP id y1-20020a170902700100b0013dd5b7d06emr5272202plk.61.1634823945071; Thu, 21 Oct 2021 06:45:45 -0700 (PDT) Received: from localhost ([103.127.241.129]) by smtp.gmail.com with ESMTPSA id b19sm5480396pgs.33.2021.10.21.06.45.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 06:45:44 -0700 (PDT) From: Leo Yan To: Catalin Marinas , Will Deacon , Mark Rutland , Kees Cook , Ard Biesheuvel , Sami Tolvanen , Nicholas Piggin , James Morse , Marc Zyngier , Joey Gouly , Peter Collingbourne , Vincenzo Frascino , "Peter Zijlstra (Intel)" , Stephane Eranian , James Clark , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [RFCv1 1/4] arm64: Use static key for tracing PID in CONTEXTIDR Date: Thu, 21 Oct 2021 21:45:27 +0800 Message-Id: <20211021134530.206216-2-leo.yan@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211021134530.206216-1-leo.yan@linaro.org> References: <20211021134530.206216-1-leo.yan@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211021_064546_310591_9FDF419F X-CRM114-Status: GOOD ( 16.69 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 Reviewed-by: Kees Cook --- 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);