From nobody Thu Apr 2 19:15:32 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 74AA03EBF2C for ; Wed, 11 Feb 2026 17:30:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770831053; cv=none; b=e/wEmAFi+Ga5A/H+AUouy8Er8VLpsKc/rXLSrtUCO33xLSUyJw8cuIeCROstrMinILGxmnfM2kjsEwUFV2tujOFOF9/YePxVibmJyRSKFJ0ddLCJmvXQMtFczxQ+xMjglXu0OP2JlNAGQMGIj03oiAy98RpKLaPyaZ7vJUzmcAY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770831053; c=relaxed/simple; bh=4QUfyU0BrGpA2i3nZ5cJtFi2TkDjb1Ji+IDFW45hrIk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cX82C06j7A+CMXid1uhZr6BCFI4iyvaEEoZBFlWt/OrML9TuzwnudsW6nFVJrdC99XmlGFl1B/ZEYui5kP6ZmhoTbwMu8SY8kCZUrgxgesFajh2kB7BLpygcxJ6YKW/4wwN+DgfY3ubQrqqHsAJaQ6tKp2+lQf7c3so9j4+eGqY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rs7dFTkJ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=eOZVy0Mg; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rs7dFTkJ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="eOZVy0Mg" From: Nam Cao DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1770831051; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BUvkJ8w7tWD428d0BdBWQcoGHqIOYKVajPuycpt1LHo=; b=rs7dFTkJT2aeKl7wyDrHY6j4sJsJOPo3k7zZJtt8ykCO5bPQHcjxArKin4vr5B7H2cCBp0 nF2lLxYzQxi2lkjKBDgqR+FZs1+JiNr0W1/aeWRgljWhFIBqzXNK821kZS1k955yHBjc3J JyyZdTMHm5dF8Olbmjiox+v/q33SfMuwZBFklSJhKiT6zmX1yaRjYqDSDCs67MMU/X/DjW EMsdL7yTLCwAEk5Nht7JFECXFuDLHWsX+FbCju1MNFrCdZkv9C96u6CUEA0VLrGDOChQzE TFPrpVrt5ge4biAfHivFqlBxt6UwukyCIoyU0e0z9XO7UGDP2Cmg789zXnUe9A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1770831051; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BUvkJ8w7tWD428d0BdBWQcoGHqIOYKVajPuycpt1LHo=; b=eOZVy0MgH/eo0R1k1vEpbyAXyjY293OPaiCvVmNW/F/WqzsANEBsUecAxJbJTnhSUABGfX FdXZTbiS0bgUhwDQ== To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Jones , =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Nam Cao Subject: [PATCH 2/5] riscv: Split out measure_cycles() for reuse Date: Wed, 11 Feb 2026 18:30:32 +0100 Message-ID: <50d0598e45acc56c95176e52fbbe56e1f4becc84.1770830596.git.namcao@linutronix.de> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Byte cycle measurement and word cycle measurement of scalar misaligned access are very similar. Split these parts out into a common measure_cycles() function to avoid duplication. This function will also be reused for vector misaligned access probe in a follow-up commit. Signed-off-by: Nam Cao --- arch/riscv/kernel/unaligned_access_speed.c | 69 +++++++++++----------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/arch/riscv/kernel/unaligned_access_speed.c b/arch/riscv/kernel= /unaligned_access_speed.c index 8b744c4a41ea..b964a666a973 100644 --- a/arch/riscv/kernel/unaligned_access_speed.c +++ b/arch/riscv/kernel/unaligned_access_speed.c @@ -31,30 +31,15 @@ static long unaligned_vector_speed_param =3D RISCV_HWPR= OBE_MISALIGNED_VECTOR_UNKNO static cpumask_t fast_misaligned_access; =20 #ifdef CONFIG_RISCV_PROBE_UNALIGNED_ACCESS -static int check_unaligned_access(void *param) +static u64 measure_cycles(void (*func)(void *dst, const void *src, size_t = len), + void *dst, void *src, size_t len) { - int cpu =3D smp_processor_id(); - u64 start_cycles, end_cycles; - u64 word_cycles; - u64 byte_cycles; + u64 start_cycles, end_cycles, cycles =3D -1ULL; u64 start_ns; - int ratio; - struct page *page =3D param; - void *dst; - void *src; - long speed =3D RISCV_HWPROBE_MISALIGNED_SCALAR_SLOW; =20 - if (per_cpu(misaligned_access_speed, cpu) !=3D RISCV_HWPROBE_MISALIGNED_S= CALAR_UNKNOWN) - return 0; - - /* Make an unaligned destination buffer. */ - dst =3D (void *)((unsigned long)page_address(page) | 0x1); - /* Unalign src as well, but differently (off by 1 + 2 =3D 3). */ - src =3D dst + (MISALIGNED_BUFFER_SIZE / 2); - src +=3D 2; - word_cycles =3D -1ULL; /* Do a warmup. */ - __riscv_copy_words_unaligned(dst, src, MISALIGNED_COPY_SIZE); + func(dst, src, len); + preempt_disable(); =20 /* @@ -66,29 +51,41 @@ static int check_unaligned_access(void *param) start_cycles =3D get_cycles64(); /* Ensure the CSR read can't reorder WRT to the copy. */ mb(); - __riscv_copy_words_unaligned(dst, src, MISALIGNED_COPY_SIZE); + func(dst, src, len); /* Ensure the copy ends before the end time is snapped. */ mb(); end_cycles =3D get_cycles64(); - if ((end_cycles - start_cycles) < word_cycles) - word_cycles =3D end_cycles - start_cycles; + if ((end_cycles - start_cycles) < cycles) + cycles =3D end_cycles - start_cycles; } =20 - byte_cycles =3D -1ULL; - __riscv_copy_bytes_unaligned(dst, src, MISALIGNED_COPY_SIZE); + preempt_enable(); =20 - start_ns =3D ktime_get_mono_fast_ns(); - while (ktime_get_mono_fast_ns() < start_ns + MISALIGNED_ACCESS_NS) { - start_cycles =3D get_cycles64(); - mb(); - __riscv_copy_bytes_unaligned(dst, src, MISALIGNED_COPY_SIZE); - mb(); - end_cycles =3D get_cycles64(); - if ((end_cycles - start_cycles) < byte_cycles) - byte_cycles =3D end_cycles - start_cycles; - } + return cycles; +} =20 - preempt_enable(); +static int check_unaligned_access(void *param) +{ + int cpu =3D smp_processor_id(); + u64 word_cycles; + u64 byte_cycles; + int ratio; + struct page *page =3D param; + void *dst; + void *src; + long speed =3D RISCV_HWPROBE_MISALIGNED_SCALAR_SLOW; + + if (per_cpu(misaligned_access_speed, cpu) !=3D RISCV_HWPROBE_MISALIGNED_S= CALAR_UNKNOWN) + return 0; + + /* Make an unaligned destination buffer. */ + dst =3D (void *)((unsigned long)page_address(page) | 0x1); + /* Unalign src as well, but differently (off by 1 + 2 =3D 3). */ + src =3D dst + (MISALIGNED_BUFFER_SIZE / 2); + src +=3D 2; + + word_cycles =3D measure_cycles(__riscv_copy_words_unaligned, dst, src, MI= SALIGNED_COPY_SIZE); + byte_cycles =3D measure_cycles(__riscv_copy_bytes_unaligned, dst, src, MI= SALIGNED_COPY_SIZE); =20 /* Don't divide by zero. */ if (!word_cycles || !byte_cycles) { --=20 2.47.3