Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp558662lqm; Wed, 1 May 2024 08:47:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXzMjmun8Bu9iisYXgo7Ub9Bb6TMH8PX+5WswOk6okrUv6qQrGOEIvnVqmZlNG+W4ritkTpeq6As36iMEb2C0YMvShRpmelVKbTBDmQ+A== X-Google-Smtp-Source: AGHT+IGLGM7fFQcKEA30rzBjbId4jrqe7NJgD5puc6xZFvEFstiseBMTZlaoFqPCGcTGLI1Nd9Xg X-Received: by 2002:a05:6a21:1a8:b0:1a7:5fbf:3774 with SMTP id le40-20020a056a2101a800b001a75fbf3774mr4610363pzb.55.1714578462110; Wed, 01 May 2024 08:47:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714578462; cv=pass; d=google.com; s=arc-20160816; b=HyzWGtb9nlaI9KgwLnYJLtubriWothv6i0vRpERqj4ve0oP8oxD2LFdZIIAJBplELT SXvwEQ3tju/2jM6sV4YnxxTANOmVGVvVwVn8OScHVT6AYnIyV+VYb0xotklAGuZnPwAM 9qDLgoCN2igDtHzT18KNgfdWxLuuoO0PjWHCVLj+5Ei8QIlxv9Firxa9rzIa4VoTCldE gzBVAST5DFLEpUvRUrvblEeSMKKLt0FPYtLyGbYowTINwhFLwIqhvsOy5YOdRyKjH2b5 TRH5vIU7SgSz2wxvrcXJsR1MKuvV3r19hgpH3yFEcrmaf4SbdBCdFIX+AdW3kFk4uRC+ dLHw== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=4dLEKN++7ifAXkJMPqJFQi5wKgHUDf+sEKTjaUCLyWI=; fh=c3o/xurI+EadO89FV7gKGcRkB9bMKwZnMFk36erK83c=; b=zQmlJMHrMjiRyk8dD6l78XtoEhoWN2fd4C6J5phQ14Mu4lVS14s0uXxlHb5rnpx+KO w3pQ0WQ/NPvIFkS6BzeqOl6jl3E44uo5LsRW4AHpHiASXZfklIcHhBHjQyiCxPMX+6lM rwdp+8Dr5nMHOtv5T5P9KvxaiYfKyWmVPD5KRcpLpclSgtmRQ3b+hqG2X10K5+uu6g1b oKnU2hIRXyxFxNHc4j0HXqkrpYSCvyrXKFYqr/MwWZNHeWhEV1KAp8GlxpYxDjPHihLf lv3BsJ//NXATLz78h3Ro10j2I+bLZpLmaxS6rPhWkCvc3gpewCBzbtiECm6lWREZqPb7 2QoA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=th6c1+mm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-165504-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165504-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id c22-20020a056a000ad600b006f3f649d77esi7656533pfl.153.2024.05.01.08.47.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 08:47:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165504-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=th6c1+mm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-165504-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165504-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 3CDE5B22F25 for ; Wed, 1 May 2024 15:43:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0561F13048E; Wed, 1 May 2024 15:43:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="th6c1+mm" 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 2748312FF78; Wed, 1 May 2024 15:43:23 +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=1714578204; cv=none; b=FEKb1HAUfk2fXpnyox1NHPoOvVbvRZg31JDL5RLrmsri3VKhj7z/UT3VaaoPtqH+RGm7eNPVdgotBjWH2JCcLcrcFvW0AznyI/XEZ6XE8bVxrTAQjJK5lA95JzN59ZTEgC9aU6U75/8nfDjWbtdYGeQ6z5HktY4JtKj7782IuKk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714578204; c=relaxed/simple; bh=KALlXTVUTaG7KIY4xfSeGKWZAwSJ5/dytvx09z4PAuA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=RNahXCpZnbDLqOh90aD5nZEn8GAZkbKVwVVY1nsH6vTBYOHQoC4yLZJQypfyD9dulGJa8Y7MMS7inW3OO78ROfwwQhW+7ui+VTyjb7JuVGkGR6nOwRpmIf2+knCjVunFyk+ueD0qH2V/Bh/cCT+GoFxIQKi2SdXtNLgaKPEtCw4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=th6c1+mm; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A5EFC072AA; Wed, 1 May 2024 15:43:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714578203; bh=KALlXTVUTaG7KIY4xfSeGKWZAwSJ5/dytvx09z4PAuA=; h=From:To:Cc:Subject:Date:From; b=th6c1+mm5tEOQDCQZKB6/0pXmDxMTXaBzvR1lpXEFQJPkq9GJSEKcwTW2jyNHYjr7 TfGDpwsO2mcdGdRbuwZigx9Qvocz20fn2W+ubLTx30PXku5RilOYMB2Pm2QSC6NIK8 W/P0Cd93k5yfwdAzOyNsRifq291WpbE2ZPbL7m+Fp44F+Q4gcMs+qhHNtAzUdnSsN+ WBTDTW0CerAW/OQ1YH4uUi43tiRvR/8jNPJ8S3A6q84o/5GXHwi6/sliWmj/b1HgT2 TlLIyU76GEGF6bQNrV9tHqkhDRtg6xiA7qsYh2XZynGE1eOyxQe0xEgGqwOzMhFrR2 +y4cw2FaaZ90A== 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] arm64: implement raw_smp_processor_id() using thread_info Date: Wed, 1 May 2024 15:42:36 +0000 Message-Id: <20240501154236.10236-1-puranjay@kernel.org> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ARM64 defines THREAD_INFO_IN_TASK which means the cpu id can be found from current_thread_info()->cpu. Implement raw_smp_processor_id() using the above. 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%) | +---------------------------------------------------+ This improvement is in this very specific microbenchmark but it proves the point. The percpu variable cpu_number is left as it is because it is used in set_smp_ipi_range() [1] https://github.com/puranjaymohan/linux/commit/77d3fdd Signed-off-by: Puranjay Mohan --- arch/arm64/include/asm/smp.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/arch/arm64/include/asm/smp.h b/arch/arm64/include/asm/smp.h index efb13112b408..88fd2ab805ec 100644 --- a/arch/arm64/include/asm/smp.h +++ b/arch/arm64/include/asm/smp.h @@ -34,13 +34,9 @@ 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. + * This relies on THREAD_INFO_IN_TASK, but arm64 defines that unconditionally. */ -#define raw_smp_processor_id() (*raw_cpu_ptr(&cpu_number)) +#define raw_smp_processor_id() (current_thread_info()->cpu) /* * Logical CPU mapping. -- 2.40.1