From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 250D4189BAF for ; Fri, 17 Jan 2025 15:09:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737126574; cv=none; b=qesnKltLTYlFAXoEF3YtZuxAfrVWZVsjr/gHY3CT2uiASWhTWCGIitS/RSpbWpxErehUPpgHP3TAfyqHH/vFrg3TWCKlJvk6qBQKJZn42qGNPtVmAT7m4+sDhiZRYmqrpJVXIAKfiI/mMG5E33UWaRIBpNFOGx7stx8aWOuTtC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737126574; c=relaxed/simple; bh=inUeq6IPy0tOSN5tNPx8YmCl9sfH7a15OZf6QpDe5o4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=iuh2qt+rCyKpIb0R4OspFbfwQzYzfHzUr1Vq/EXOPISezOg0iHPq7VTkgwGM3J6JHBEhDZ2ZVKGtgbMtMzjhvvxTIlnsmBH/f8B39ZLmGfqVpf7iaycCZNoYpq7Qp8r0p4GEhgXRFP9VXYp8Tirrg4c1wrCIldDx51oCxjA14xU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=ByhHdQcw; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="ByhHdQcw" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-43621d27adeso14850665e9.2 for ; Fri, 17 Jan 2025 07:09:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1737126569; x=1737731369; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=eeBHQLs9jVU95ezPlxDDqu8VwoV3ld6Y4fMnADed+lM=; b=ByhHdQcwJR1Vk3GUQdletUr7cy6cUX2v1kuKEabsUBAhWa8Adt5quePWXL3FUBKgJ8 rTfT+UQBWR09Bl9dprBTeG+3HlF0YOWoGPs0tOAqCpjMGma2Rpz1zMgOU17uqI4xoJT0 BPTsDnu6lRWpNl0XYcY0UtQcXIMG33oGmlBM/iPk8s/TZB8FogyOmN470Uo08YFSCzFp CDu5xeEzOpTcPGa9B2lkkM9WO4Z93MwZYaaY82+7G2tqq/GT6vqDB+MMMiuUg99vbrrq m7MGafPFlNeGhuboBF8Yrmz5ZK+CdTQnZdXPPVdaxWZWrNSp8fs6aRSB4l8zeUVjqe97 1vHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737126569; x=1737731369; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eeBHQLs9jVU95ezPlxDDqu8VwoV3ld6Y4fMnADed+lM=; b=Z/wXRuNan3TRrnhtyx8gkjEBg37R3FCr9Wolu6/5EStOa17d39SGHHfKLpXJ5ncwQ/ 6KY6bPGwaswz/qSaEcQ5Q/CsOk7qMpHRgL7kIYmYUDIkhDFl50r5CPiBotwJG5FahMyB GkjAE5sepqKdcKt9Tdd71Ch1sHWaqWT1lIsCMeeqym3b5MM7l4Pb3XfNwcO6ewdUsM3n /o4SQHqYxuKxDUfkUAH6t+DSeVaWbv6jVomQfL+7Tbgg9ki2HyY2Pd5p05EiDqOIqgeU chEhfRfQutDLSLggWQlEhfEApAHPPCqXPGfcRYZz+OJTalvGbbSOnGxJWTAHwKS34lPD NGmw== X-Forwarded-Encrypted: i=1; AJvYcCWVjEGaneMmS/EsC/t7MjexuV8S1U0++grm990exdJjNPUu4KhSwCl34crb098YbXh82koy3ITqTnPSqYU=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+PjCPC7DBLEhRxTpyIOzaSSQW2svcCPtRh+GDJKtrARY0iw3s AsxycuPngrjxg6QHkk0inzmy2gQ76pcNXsBB453FAYnQ4EAN+NCB2qM1NiJlPzk= X-Gm-Gg: ASbGnctMZ0MfhG3x2KVPham0vtLPKOTEuEpiDNabeiyveNYTQh0SclU7zHBVkzIPpxr 2o1FW2hfEvXdl0i0pEJPuaErYQTPLZ8MtNdrN9hfcREswvos+nKWmT1ldVLiQTbLekDkeBrZds9 XCYx3oXs0bNUb7myN7tj48NgeCMAvAw/gsKOUl4UP5k8bws5venD+3zTAIimwdpqDer0gJum0fb ctsCqVZyprwa3wxA9FufOI29S2lCALoqMd0edKOROqNu5N3jCVem+DOG11T1E349sTfiqzQAmJe EDCvaeOUg1KqFSFXvept/kgo1Q== X-Google-Smtp-Source: AGHT+IG1uZGUInjJmwhJbH8Ho3BFf5w1Dx39oNxBLgMYa7175SzgdXR9MqOpaXnkZP4l9hYELj4CQA== X-Received: by 2002:a05:600c:5101:b0:434:f925:f5c9 with SMTP id 5b1f17b1804b1-438913c85e0mr30286255e9.6.1737126568985; Fri, 17 Jan 2025 07:09:28 -0800 (PST) Received: from ?IPV6:2a01:e0a:e17:9700:16d2:7456:6634:9626? ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-437c7499bbasm101555505e9.3.2025.01.17.07.09.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Jan 2025 07:09:28 -0800 (PST) Message-ID: <24f9b863-9b88-4a58-a7a0-b562f6be56ad@rivosinc.com> Date: Fri, 17 Jan 2025 16:09:27 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/6] riscv: request misaligned exception delegation from SBI To: Samuel Holland Cc: Paul Walmsley , Palmer Dabbelt , Anup Patel , Atish Patra , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org References: <20250106154847.1100344-1-cleger@rivosinc.com> <20250106154847.1100344-3-cleger@rivosinc.com> Content-Language: en-US From: =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 11/01/2025 00:35, Samuel Holland wrote: > Hi Clément, > > On 2025-01-06 9:48 AM, Clément Léger wrote: >> Now that the kernel can handle misaligned accesses in S-mode, request >> misaligned access exception delegation from SBI. This uses the FWFT SBI >> extension defined in SBI version 3.0. >> >> Signed-off-by: Clément Léger >> --- >> arch/riscv/include/asm/cpufeature.h | 1 + >> arch/riscv/kernel/traps_misaligned.c | 59 ++++++++++++++++++++++ >> arch/riscv/kernel/unaligned_access_speed.c | 2 + >> 3 files changed, 62 insertions(+) >> >> diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h >> index 4bd054c54c21..cd406fe37df8 100644 >> --- a/arch/riscv/include/asm/cpufeature.h >> +++ b/arch/riscv/include/asm/cpufeature.h >> @@ -62,6 +62,7 @@ void __init riscv_user_isa_enable(void); >> _RISCV_ISA_EXT_DATA(_name, _id, _sub_exts, ARRAY_SIZE(_sub_exts), _validate) >> >> bool check_unaligned_access_emulated_all_cpus(void); >> +void unaligned_access_init(void); >> #if defined(CONFIG_RISCV_SCALAR_MISALIGNED) >> void check_unaligned_access_emulated(struct work_struct *work __always_unused); >> void unaligned_emulation_finish(void); >> diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c >> index 7cc108aed74e..4aca600527e9 100644 >> --- a/arch/riscv/kernel/traps_misaligned.c >> +++ b/arch/riscv/kernel/traps_misaligned.c >> @@ -16,6 +16,7 @@ >> #include >> #include >> #include >> +#include >> #include >> >> #define INSN_MATCH_LB 0x3 >> @@ -689,3 +690,61 @@ bool check_unaligned_access_emulated_all_cpus(void) >> return false; >> } >> #endif >> + >> +#ifdef CONFIG_RISCV_SBI >> + >> +struct misaligned_deleg_req { >> + bool enable; >> + int error; >> +}; >> + >> +static void >> +cpu_unaligned_sbi_request_delegation(void *arg) >> +{ >> + struct misaligned_deleg_req *req = arg; >> + struct sbiret ret; >> + >> + ret = sbi_ecall(SBI_EXT_FWFT, SBI_EXT_FWFT_SET, >> + SBI_FWFT_MISALIGNED_EXC_DELEG, req->enable, 0, 0, 0, 0); >> + if (ret.error) >> + req->error = 1; >> +} >> + >> +static void unaligned_sbi_request_delegation(void) >> +{ >> + struct misaligned_deleg_req req = {true, 0}; >> + >> + on_each_cpu(cpu_unaligned_sbi_request_delegation, &req, 1); >> + if (!req.error) { >> + pr_info("SBI misaligned access exception delegation ok\n"); >> + /* >> + * Note that we don't have to take any specific action here, if >> + * the delegation is successful, then >> + * check_unaligned_access_emulated() will verify that indeed the >> + * platform traps on misaligned accesses. >> + */ >> + return; >> + } >> + >> + /* >> + * If at least delegation request failed on one hart, revert misaligned >> + * delegation for all harts, if we don't do that, we'll panic at >> + * misaligned delegation check time (see >> + * check_unaligned_access_emulated()). >> + */ >> + req.enable = false; >> + req.error = 0; >> + on_each_cpu(cpu_unaligned_sbi_request_delegation, &req, 1); >> + if (req.error) >> + panic("Failed to disable misaligned delegation for all CPUs\n"); > > This logic doesn't handle the case where the delegation request failed on every > CPU, so there's nothing to revert. This causes a panic in a KVM guest inside > qemu-softmmu (the host kernel detects MISALIGNED_SCALAR_FAST, so > unaligned_ctl_available() returns false, and all FWFT calls fail). Hi Samuel, Indeed, that's a problem and the revert is not really clean since it is called on all CPUs (even tha rone that failed). I'll modify that to use cpumasks and thus cleanly revert the misaligned delegation on CPUs that actually succeeded only. Thanks, Clément > > Regards, > Samuel > >> + >> +} >> + >> +void unaligned_access_init(void) >> +{ >> + if (sbi_probe_extension(SBI_EXT_FWFT) > 0) >> + unaligned_sbi_request_delegation(); >> +} >> +#else >> +void unaligned_access_init(void) {} >> +#endif >> diff --git a/arch/riscv/kernel/unaligned_access_speed.c b/arch/riscv/kernel/unaligned_access_speed.c >> index 91f189cf1611..1e3166100837 100644 >> --- a/arch/riscv/kernel/unaligned_access_speed.c >> +++ b/arch/riscv/kernel/unaligned_access_speed.c >> @@ -403,6 +403,8 @@ static int check_unaligned_access_all_cpus(void) >> { >> bool all_cpus_emulated, all_cpus_vec_unsupported; >> >> + unaligned_access_init(); >> + >> all_cpus_emulated = check_unaligned_access_emulated_all_cpus(); >> all_cpus_vec_unsupported = check_vector_unaligned_access_emulated_all_cpus(); >> >