From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 5E13D189F3F for ; Fri, 17 Jan 2025 15:50:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737129021; cv=none; b=lpEgSaKxL5fEh7EJR8P2+vwfKt1ZIy91A1IhLW9U89ayH6LQL875Q26lg1naN2Mn3eEIJskeKx031FZ3AuiWy/k60ldiVwAuZ9Tp2Ks5cRTmfVJ1ghbvVug8QocvTKANJZ3SnMMxsNThqEQgGSxMQ27AWdKLNCoNBitflRxrSfE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737129021; c=relaxed/simple; bh=dclbQA2kv91cOorJaTivwzz7mnR/zYBL6itMyl+l6F4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=cGUEPhTvXqS0kjGsTRagVBtI84gk5zHdFPofdPBigS/oZDi+5K8KVJh3xJJjyVbYp28jG1QKTQ2nmsJpSji7JQryK96QyEzhLuJ0JALC/wsOWBIJBQLp1CLeVpyiux+IUw9ae65ZKTOyCIvOV4IPUPMHODw+MVqsZqoZjqs2pYc= 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=dz0iJhY+; arc=none smtp.client-ip=209.85.221.48 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="dz0iJhY+" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-385e06af753so1192846f8f.2 for ; Fri, 17 Jan 2025 07:50:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1737129018; x=1737733818; 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=cp/GIvJPAqb8LwPhNDqvZSb8wpAapnmNF5gAZrNXtXA=; b=dz0iJhY+SaJlzRgU+0IhBXaj+VQYmRdzLCvXev0LiDG27OwqdidS7M0rD8LStIevj0 fsg+sOrWPYN99qdlTivbqFlR0IuliHlLFM50BcssVwR/dgYwzNgr1Vv6KgjmKRYevW4y yAstQ3Z6Ll9ihWrueHOssIVTrh1iQ4lD4IAS3BFIcsXmgtQpTdUsTcCIPFvQr42e3eRB Xy8UmCy7GqhOEL92ZolIQGpk+gjZyHDINdV98J5qpXlzWj3By88cNmKVjt8nOIpGvY26 kGaDyD4WGG9SZ/0IKEWImPTW6J8BgGee+frNdcIwfzY6Ws9yv762Jfyt63LjDcuRU3xb 7S6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737129018; x=1737733818; 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=cp/GIvJPAqb8LwPhNDqvZSb8wpAapnmNF5gAZrNXtXA=; b=OgyAVmGlkU8HfIFxdEipl4obRDIVkfZOmSu5N4SVcuekwfO99dQdXf9aHCLe8T3OYx R9YdcVEelfhRbil9F78Isy1sWAHZbpduLzmc6n+8DfCTRWrGQQO4qOh9Em19Z6ApD9OX Y4QNdkUwOxGuHgwhQ2KaT2bhF987f3fxP44xr/HBovK+qm+50oQp5Kp6SXp6KNM4nEBV 3JwwGxEgOyBkvZfA1BO0+tZQkzQecFmnmfPV7rKW3iQJ40N6i0vekR4D/tA9hAk0WJZA qM23hN/AUcpJ6n6dlYwPNOYVXdfdqIdFTbfVZwQPkCPk+Xo+A/q4uKpIa1Kpp3Mltwzj lfdQ== X-Forwarded-Encrypted: i=1; AJvYcCX8KotXB+R8rBIqYAJE+CPUOnH0ANn45OUBldsIkl2Wzsu1gLRw1r4vHKFelOYr3Yt26MBMq98MxRdKAYs=@vger.kernel.org X-Gm-Message-State: AOJu0YysJ/qs0mZVGxYAcAwM+j8fW5+QXNLqz/CMZWsgkTQeyVsbk8Jz 1tD8J9jKd18g3PV77V0TPOcKKTD63cMx10puqBjI2wh0ab0Tx1N1o3e5bt6PH0s= X-Gm-Gg: ASbGnctu9QPbAbcErBxNXp5fCC+Lcf1+57cOuLdW5L4rsVDb44JNtepfTL8jgNxOch1 il+L4dgYkPvtBb66hu99cfz/DtD0GMfEwkN1znIBFAxihuwi3PCaNKGAxgKKqtpRgapawJhVy/Y R/JNB4lSvZTG6nmB76wwtebqNOeiyNOmzQa9cQrw3/MYB6+U6W7MHnApxfnYi2oeX7MnCjW2Kkn AX90H7Q/439RXOcwHTJ7LjNRpKkyl8/YcPuEYcMfhFV0jdJ/UY8ceFNFQsxZ/SLDc/a04+J9sjJ 6ARSRuhishrywzjmCLg6yHrk/g== X-Google-Smtp-Source: AGHT+IG2hMkiEwGdkGXBgxT0aXmQV7q70JaFEOKvaBWwhUvfyCsA51xrLRzqSMURNo7XJP0BjSwJJw== X-Received: by 2002:a5d:620f:0:b0:38b:d765:7027 with SMTP id ffacd0b85a97d-38bf57a2675mr2269669f8f.41.1737129017614; Fri, 17 Jan 2025 07:50:17 -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 ffacd0b85a97d-38bf322b51bsm2860493f8f.60.2025.01.17.07.50.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Jan 2025 07:50:16 -0800 (PST) Message-ID: <9fdcd0cf-334e-46f9-97c1-305f213d1ad3@rivosinc.com> Date: Fri, 17 Jan 2025 16:50:16 +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 3/6] RISC-V: KVM: add SBI extension init()/deinit() functions 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-4-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:42, Samuel Holland wrote: > Hi Clément, > > On 2025-01-06 9:48 AM, Clément Léger wrote: >> The FWFT SBI extension will need to dynamically allocate memory and do >> init time specific initialization. Add an init/deinit callbacks that >> allows to do so. >> >> Signed-off-by: Clément Léger >> --- >> arch/riscv/include/asm/kvm_vcpu_sbi.h | 9 ++++++++ >> arch/riscv/kvm/vcpu.c | 2 ++ >> arch/riscv/kvm/vcpu_sbi.c | 31 ++++++++++++++++++++++++++- >> 3 files changed, 41 insertions(+), 1 deletion(-) >> >> diff --git a/arch/riscv/include/asm/kvm_vcpu_sbi.h b/arch/riscv/include/asm/kvm_vcpu_sbi.h >> index b96705258cf9..8c465ce90e73 100644 >> --- a/arch/riscv/include/asm/kvm_vcpu_sbi.h >> +++ b/arch/riscv/include/asm/kvm_vcpu_sbi.h >> @@ -49,6 +49,14 @@ struct kvm_vcpu_sbi_extension { >> >> /* Extension specific probe function */ >> unsigned long (*probe)(struct kvm_vcpu *vcpu); >> + >> + /* >> + * Init/deinit function called once during VCPU init/destroy. These >> + * might be use if the SBI extensions need to allocate or do specific >> + * init time only configuration. >> + */ >> + int (*init)(struct kvm_vcpu *vcpu); >> + void (*deinit)(struct kvm_vcpu *vcpu); >> }; >> >> void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run); >> @@ -69,6 +77,7 @@ const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext( >> bool riscv_vcpu_supports_sbi_ext(struct kvm_vcpu *vcpu, int idx); >> int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run); >> void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu); >> +void kvm_riscv_vcpu_sbi_deinit(struct kvm_vcpu *vcpu); >> >> int kvm_riscv_vcpu_get_reg_sbi_sta(struct kvm_vcpu *vcpu, unsigned long reg_num, >> unsigned long *reg_val); >> diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c >> index e048dcc6e65e..3420a4a62c94 100644 >> --- a/arch/riscv/kvm/vcpu.c >> +++ b/arch/riscv/kvm/vcpu.c >> @@ -180,6 +180,8 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) >> >> void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) >> { >> + kvm_riscv_vcpu_sbi_deinit(vcpu); >> + >> /* Cleanup VCPU AIA context */ >> kvm_riscv_vcpu_aia_deinit(vcpu); >> >> diff --git a/arch/riscv/kvm/vcpu_sbi.c b/arch/riscv/kvm/vcpu_sbi.c >> index 6e704ed86a83..d2dbb0762072 100644 >> --- a/arch/riscv/kvm/vcpu_sbi.c >> +++ b/arch/riscv/kvm/vcpu_sbi.c >> @@ -486,7 +486,7 @@ void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu) >> struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context; >> const struct kvm_riscv_sbi_extension_entry *entry; >> const struct kvm_vcpu_sbi_extension *ext; >> - int idx, i; >> + int idx, i, ret; >> >> for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) { >> entry = &sbi_ext[i]; >> @@ -501,8 +501,37 @@ void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu) >> continue; >> } >> >> + if (ext->init) { >> + ret = ext->init(vcpu); >> + if (ret) >> + scontext->ext_status[idx] = >> + KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE; >> + } >> + >> scontext->ext_status[idx] = ext->default_disabled ? >> KVM_RISCV_SBI_EXT_STATUS_DISABLED : >> KVM_RISCV_SBI_EXT_STATUS_ENABLED; > > This will overwrite the KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE set above. Oh yes indeed, I should add a "continue" under the if above. > >> } >> } >> + >> +void kvm_riscv_vcpu_sbi_deinit(struct kvm_vcpu *vcpu) >> +{ >> + struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context; >> + const struct kvm_riscv_sbi_extension_entry *entry; >> + const struct kvm_vcpu_sbi_extension *ext; >> + int idx, i; >> + >> + for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) { >> + entry = &sbi_ext[i]; >> + ext = entry->ext_ptr; >> + idx = entry->ext_idx; >> + >> + if (idx < 0 || idx >= ARRAY_SIZE(scontext->ext_status)) >> + continue; >> + >> + if (scontext->ext_status[idx] != KVM_RISCV_SBI_EXT_STATUS_ENABLED || !ext->deinit) > > Given that an extension can be enabled/disabled after initialization, this > should only skip deinit if the status is UNAVAILABLE. yeah, make sense as well, Thanks for the review, Clément > > Regards, > Samuel > >> + continue; >> + >> + ext->deinit(vcpu); >> + } >> +} >