Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1107265lqm; Thu, 2 May 2024 05:43:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV7LwdpQOgn3gV8Oigguc3ASboJtsM2F4JUnXf5F6oRu3S5cLciCzibjyt4Bcoiu1xEW+U0c8TW7ZlLq8ImRHay5pdQhLltX6LpyokClw== X-Google-Smtp-Source: AGHT+IG0SKsMamkjryOkOVLG9ak5unXzU/KinFHdx9ESG9MdX/YA9NSj0wz6IUVTXM5lZbYMuGP1 X-Received: by 2002:a05:6a20:3217:b0:1ac:e379:542b with SMTP id hl23-20020a056a20321700b001ace379542bmr4824610pzc.3.1714653813946; Thu, 02 May 2024 05:43:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714653813; cv=pass; d=google.com; s=arc-20160816; b=TskE1ijZF7AgrzqAL6guygTRpcMmAx1KzU0x1SXEPyn6sTr0J7bLyng4/ws2YAFOTS 2XPa/c5Ba4Rrgq+HPKnS/wdgWiPXWEVDKpUXa/Q0bGpGWmplG3wZJ63mVaGVnEAxi/rn SQmuhhdrD2/cOGrbD1y0SrGMkS7MpFzlomtiqb4eNxvitCnYQMx713OO3NRiFKPcr5y7 PFdWDLJNooFp3dnglsnv/2RTvXyca898wFYAp5GYYAWsiOnc1tATDWE4/U4TPLcm0vIo cuFhNMPc/Q4H/9l8dkC4lM5u19EzUXVqsJHdXwcyJl7rY02ohi2Qzgqt93NugWtfpQDW TSig== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=CCmZAnL5qXSgRYOVej248xhb33d5oIq/c5KsvLbJzXg=; fh=c3o/xurI+EadO89FV7gKGcRkB9bMKwZnMFk36erK83c=; b=QyHC723wCSh7CIBuWO8ojmwwGXGOWii9MyEwngk+mHJFtmhyEFAemUIGAU4x3WMBoT E6lrQKcdsz668mbfPo08D8oBVlAurn96uP0tLMldy/UKd93repVlYlwfcUBCKtSXoz7j 9DPI88Hhr2iNH8sLYSLo6w3nd6vms9l+oooFUBO7GMyagZ+JGRyI2/SYU14S/x+Ay4EV OxBSUqAVSI7h2NXPeRa65TOqIHaDn5h4Fdnw3QaHwQ/XIUIfdSHgY/iJjcQhW31gjkgh tvMywFOYzk8eklq4COPUJawXbClgffVtKD4hVADlqlMtn5YlpL/pB3f3LADlmhgEVWf6 Koxg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q0oA7urD; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-166460-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166460-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e14-20020a63500e000000b005cdacf9065fsi969601pgb.720.2024.05.02.05.43.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 05:43:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-166460-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Q0oA7urD; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-166460-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166460-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A4C5828358D for ; Thu, 2 May 2024 12:35:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 71FAC7E792; Thu, 2 May 2024 12:35:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Q0oA7urD" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84CCA1EA8D; Thu, 2 May 2024 12:35:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714653332; cv=none; b=XRYkzOAhsQd61saFEilv8qc5AoEpTxWGene+/mgQ9gR8/yuKwYcJx8PKk/sn+Me1h9u9UBFTGLMgAtrUka3WXJy7jRqoCRu82Yjy+ZfFUeMJezpiywAZnI0TAh3V3HKkSyRMkcASCSBeik6FpEfY5PxZDtROPsgg8sxyLpSxQ9Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714653332; c=relaxed/simple; bh=1iCPxTYI/pWQqAi8vVdqGnub1dSEJs9IuaXYhwyCwuE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=u+HywJWOKlpTUXVRaTANxroG/wGVju5BHpvXW7BxNZQ/40rBiwNLi4FXtFFITNQzzQ3IMhc3Nj3ds7joMA1Bsy3/9HCruzSW8D38dM/kSH46juFk6gjcv8AVDcRRwoTeTCVFbbjjWW8zzgybRpe+p4zxzqgc0vOH8sky39xn1Lg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Q0oA7urD; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7696C4AF1A; Thu, 2 May 2024 12:35:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714653332; bh=1iCPxTYI/pWQqAi8vVdqGnub1dSEJs9IuaXYhwyCwuE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q0oA7urDCD13K1L0cCU8HeM+7YcjiTjuyh+c7i/+Td/1fhozuvdP/0d9ryRRMIlGh Fxnwe7dUXuAZSkCRGPojYpvJtAqj2goIyyNIoKAg5s7N7LegK+Q0ig0yTzXidmoOMA u9VPCndHJ4wPVDdV6WP+64NtQTNgV05kLXr94d+XZQ//2xD5N9B9z1wpz8fJLgYfSt oZTyFYstifkOarSaNm57faFrAHZnmIDyCXYJxAf02Ht6H1FxJvk8HrfhVCjKo9bhlD wILsHliajdkrMhQnycBmDqOyEFW7wMGKLAYbztTTZd8hoaFAOFEr1KLQMqc98ebK8I /8vnC+E3XaceQ== From: Puranjay Mohan To: Catalin Marinas , Will Deacon , Sumit Garg , Stephen Boyd , Douglas Anderson , "Peter Zijlstra (Intel)" , Thomas Gleixner , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: puranjay12@gmail.com Subject: [PATCH v2 2/2] arm64: implement raw_smp_processor_id() using thread_info Date: Thu, 2 May 2024 12:34:49 +0000 Message-Id: <20240502123449.2690-2-puranjay@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240502123449.2690-1-puranjay@kernel.org> References: <20240502123449.2690-1-puranjay@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Historically, arm64 implemented raw_smp_processor_id() as a read of current_thread_info()->cpu. This changed when arm64 moved thread_info into task struct, as at the time CONFIG_THREAD_INFO_IN_TASK made core code use thread_struct::cpu for the cpu number, and due to header dependencies prevented using this in raw_smp_processor_id(). As a workaround, we moved to using a percpu variable in commit: commit 57c82954e77f ("arm64: make cpu number a percpu variable") Since then, thread_info::cpu was reintroduced, and core code was made to use this in commits: commit 001430c1910d ("arm64: add CPU field to struct thread_info") commit bcf9033e5449 ("sched: move CPU field back into thread_info if THREAD_INFO_IN_TASK=y") Consequently it is possible to use current_thread_info()->cpu again. This decreases the number of emitted instructions like in the following example: Dump of assembler code for function bpf_get_smp_processor_id: 0xffff8000802cd608 <+0>: nop 0xffff8000802cd60c <+4>: nop 0xffff8000802cd610 <+8>: adrp x0, 0xffff800082138000 0xffff8000802cd614 <+12>: mrs x1, tpidr_el1 0xffff8000802cd618 <+16>: add x0, x0, #0x8 0xffff8000802cd61c <+20>: ldrsw x0, [x0, x1] 0xffff8000802cd620 <+24>: ret After this patch: Dump of assembler code for function bpf_get_smp_processor_id: 0xffff8000802c9130 <+0>: nop 0xffff8000802c9134 <+4>: nop 0xffff8000802c9138 <+8>: mrs x0, sp_el0 0xffff8000802c913c <+12>: ldr w0, [x0, #24] 0xffff8000802c9140 <+16>: ret A microbenchmark[1] was built to measure the performance improvement provided by this change. It calls the following function given number of times and finds the runtime overhead: static noinline int get_cpu_id(void) { return smp_processor_id(); } Run the benchmark like: modprobe smp_processor_id nr_function_calls=1000000000 +--------------------------+------------------------+ | | Number of Calls | Time taken | +--------+-----------------+------------------------+ | Before | 1000000000 | 1602888401ns | +--------+-----------------+------------------------+ | After | 1000000000 | 1206212658ns | +--------+-----------------+------------------------+ | Difference (decrease) | 396675743ns (24.74%) | +---------------------------------------------------+ Remove the percpu variable cpu_number as it is used only in set_smp_ipi_range() as a dummy variable to be passed to ipi_handler(). Use irq_stat in place of cpu_number here. [1] https://github.com/puranjaymohan/linux/commit/77d3fdd Signed-off-by: Puranjay Mohan --- Changes in v1 -> v2: v1: https://lore.kernel.org/all/20240501154236.10236-1-puranjay@kernel.org/ - Remove the percpu variable cpu_number - Add more information to the commit message. --- arch/arm64/include/asm/smp.h | 13 +------------ arch/arm64/kernel/smp.c | 9 ++------- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/arch/arm64/include/asm/smp.h b/arch/arm64/include/asm/smp.h index efb13112b408..2510eec026f7 100644 --- a/arch/arm64/include/asm/smp.h +++ b/arch/arm64/include/asm/smp.h @@ -25,22 +25,11 @@ #ifndef __ASSEMBLY__ -#include - #include #include #include -DECLARE_PER_CPU_READ_MOSTLY(int, cpu_number); - -/* - * We don't use this_cpu_read(cpu_number) as that has implicit writes to - * preempt_count, and associated (compiler) barriers, that we'd like to avoid - * the expense of. If we're preemptible, the value can be stale at use anyway. - * And we can't use this_cpu_ptr() either, as that winds up recursing back - * here under CONFIG_DEBUG_PREEMPT=y. - */ -#define raw_smp_processor_id() (*raw_cpu_ptr(&cpu_number)) +#define raw_smp_processor_id() (current_thread_info()->cpu) /* * Logical CPU mapping. diff --git a/arch/arm64/kernel/smp.c b/arch/arm64/kernel/smp.c index 4ced34f62dab..98d4e352c3d0 100644 --- a/arch/arm64/kernel/smp.c +++ b/arch/arm64/kernel/smp.c @@ -55,9 +55,6 @@ #include -DEFINE_PER_CPU_READ_MOSTLY(int, cpu_number); -EXPORT_PER_CPU_SYMBOL(cpu_number); - /* * as from 2.5, kernels no longer have an init_tasks structure * so we need some other way of telling a new secondary core @@ -742,8 +739,6 @@ void __init smp_prepare_cpus(unsigned int max_cpus) */ for_each_possible_cpu(cpu) { - per_cpu(cpu_number, cpu) = cpu; - if (cpu == smp_processor_id()) continue; @@ -1021,12 +1016,12 @@ void __init set_smp_ipi_range(int ipi_base, int n) if (ipi_should_be_nmi(i)) { err = request_percpu_nmi(ipi_base + i, ipi_handler, - "IPI", &cpu_number); + "IPI", &irq_stat); WARN(err, "Could not request IPI %d as NMI, err=%d\n", i, err); } else { err = request_percpu_irq(ipi_base + i, ipi_handler, - "IPI", &cpu_number); + "IPI", &irq_stat); WARN(err, "Could not request IPI %d as IRQ, err=%d\n", i, err); } -- 2.40.1