From nobody Thu Apr 2 19:17:30 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 5529026B2CE for ; Wed, 11 Feb 2026 17:30:53 +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=1770831054; cv=none; b=tCimf5NuFAqD/58z84hDvBrftjHakfXczpfoadf8D9THme6KeGLU4lpjxDDX1JOJ86OLYD9jsJ9pq7ad77s47rAFG130/GVV026clwEhp7g98EabGX+0I7pw+sUk0MpOekJlG5EaT2GzbhVw52H8aUWd3ToW7U96PSI8dM4+wDU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770831054; c=relaxed/simple; bh=2LD/4B+eeK+6RiiQXJtqeYjCyOSAeCtrLQC5AL8sT8Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q/R/Jv5o8np79QYQmkmtqIJIUD1cs+obDZU6/KdM2sC2q/7yzIR65ysqCjoqFqsMiA8o4+QbYLML0HtxPvXnlqtHuoMLWWc0uD+aHKWPVm1JlwZUhJf9qnXFFU5Wzx8D17iEuNl2WGq77jXO1lfJD9gJ8On4uR8d0FvLWxvdtZE= 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=1lBK/ktc; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=onsazupK; 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="1lBK/ktc"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="onsazupK" 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=md7PKEkW8qRnREfNKFlfHXRJ8unV7xpddBLzTx6CkJg=; b=1lBK/ktcYfigwtF7Wm7+ofucP+4Ikm8K636KRYzfkD7zRCgm7vNbGQM9/b6/4wQ7Mbso7k qxCFAcbWVZK61NFDmZj2CSEdUTSM0aCN4lfHsGDzNlrYSEnmVm4zT9HLDgWEPeXENBMkNb z814DHgCNM5rII8jkUlUzfuHndgOdMhU+uIhLT8RaEZ5FKiOFa9jMDCIXGA6h25aFNRh39 kGYFV31crqW4FHf6j4tw6bD/8ZznEtRnnrf37GFA8vJF1+gPGPcS+cZ2jVTvInPxl3QVlx L/9WMiaoGAvVJauOn0qI9Qd5hsKmP3dcQS60UoTNatU9RGdJH8d60vA/2Cy+Aw== 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=md7PKEkW8qRnREfNKFlfHXRJ8unV7xpddBLzTx6CkJg=; b=onsazupK5R9EnobhxSXr+gvZPjH6MVKB8Lv4ovAtHYM13MyO2JRg2kLOx2FHEUqFvs/XKx 9sxXcuc2Wrn9Z0AA== 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 4/5] riscv: Split out compare_unaligned_access() Date: Wed, 11 Feb 2026 18:30:34 +0100 Message-ID: <3695f77279d473eead8ed6210d97c941321cd4f1.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" Scalar misaligned access probe and vector misaligned access probe share very similar code. Split out this similar part from scalar probe into compare_unaligned_access(), which will be reused for vector probe in a follow-up commit. Signed-off-by: Nam Cao --- arch/riscv/kernel/unaligned_access_speed.c | 59 +++++++++++++++------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/arch/riscv/kernel/unaligned_access_speed.c b/arch/riscv/kernel= /unaligned_access_speed.c index c0d39c4b2150..b3ed74b71d3e 100644 --- a/arch/riscv/kernel/unaligned_access_speed.c +++ b/arch/riscv/kernel/unaligned_access_speed.c @@ -63,58 +63,79 @@ measure_cycles(void (*func)(void *dst, const void *src,= size_t len), return cycles; } =20 -#ifdef CONFIG_RISCV_PROBE_UNALIGNED_ACCESS -static int check_unaligned_access(void *param) +/* + * Return: + * 1 if unaligned accesses are fast + * 0 if unaligned accesses are slow + * -1 if check cannot be done + */ +static int __maybe_unused +compare_unaligned_access(void (*word_copy)(void *dst, const void *src, siz= e_t len), + void (*byte_copy)(void *dst, const void *src, size_t len), + void *buf) { int cpu =3D smp_processor_id(); u64 word_cycles; u64 byte_cycles; + void *dst, *src; + bool fast; 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; =20 /* Make an unaligned destination buffer. */ - dst =3D (void *)((unsigned long)page_address(page) | 0x1); + dst =3D (void *)((unsigned long)buf | 0x1); /* Unalign src as well, but differently (off by 1 + 2 =3D 3). */ src =3D dst + (MISALIGNED_BUFFER_SIZE / 2); src +=3D 2; =20 - 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); + word_cycles =3D measure_cycles(word_copy, dst, src, MISALIGNED_COPY_SIZE); + byte_cycles =3D measure_cycles(byte_copy, dst, src, MISALIGNED_COPY_SIZE); =20 /* Don't divide by zero. */ if (!word_cycles || !byte_cycles) { pr_warn("cpu%d: rdtime lacks granularity needed to measure unaligned acc= ess speed\n", cpu); =20 - return 0; + return -1; } =20 - if (word_cycles < byte_cycles) - speed =3D RISCV_HWPROBE_MISALIGNED_SCALAR_FAST; + fast =3D word_cycles < byte_cycles; =20 ratio =3D div_u64((byte_cycles * 100), word_cycles); pr_info("cpu%d: Ratio of byte access time to unaligned word access is %d.= %02d, unaligned accesses are %s\n", cpu, ratio / 100, ratio % 100, - (speed =3D=3D RISCV_HWPROBE_MISALIGNED_SCALAR_FAST) ? "fast" : "slow"); + fast ? "fast" : "slow"); =20 - per_cpu(misaligned_access_speed, cpu) =3D speed; + return fast; +} + +#ifdef CONFIG_RISCV_PROBE_UNALIGNED_ACCESS +static int check_unaligned_access(struct page *page) +{ + void *buf =3D page_address(page); + int cpu =3D smp_processor_id(); + int ret; + + if (per_cpu(misaligned_access_speed, cpu) !=3D RISCV_HWPROBE_MISALIGNED_S= CALAR_UNKNOWN) + return 0; + + ret =3D compare_unaligned_access(__riscv_copy_words_unaligned, + __riscv_copy_bytes_unaligned, buf); + if (ret < 0) + return 0; =20 /* * Set the value of fast_misaligned_access of a CPU. These operations * are atomic to avoid race conditions. */ - if (speed =3D=3D RISCV_HWPROBE_MISALIGNED_SCALAR_FAST) + if (ret) { + per_cpu(misaligned_access_speed, cpu) =3D RISCV_HWPROBE_MISALIGNED_SCALA= R_FAST; cpumask_set_cpu(cpu, &fast_misaligned_access); - else + } else { + per_cpu(misaligned_access_speed, cpu) =3D RISCV_HWPROBE_MISALIGNED_SCALA= R_SLOW; cpumask_clear_cpu(cpu, &fast_misaligned_access); + } =20 return 0; } --=20 2.47.3