From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CFAFDC61DD6 for ; Tue, 1 Sep 2026 08:10:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=L15v7UAbfzgcUH2YusfvRM5sHf1SXeOAjXSMkhubbSs=; b=WdcAYYkhUuE1B1wOf0+LGtpzJX LLzmLCNp5zZCTPPS+BPjgRaTHEHWNyAPmeb+CneYVABR7dHCuq5KFHX3x5phgpTTNiwNpcuWSyYr6 xZUzar9KUKIWGZ9Cp1lWX+nn5Qjg0StUg24FWGbXyjec8sdYWY7xHbrnhBvbBqrKZU7mDHb8xOusL vBukKxZK63AJ5hBR1+Ii5EXwVq9UaryFd1OxDz5HpBOEpEqHAnmYavpRfdd2To+oUbq5xvx5TE/ws cfzoIwOIhm1X1Zhbd/fmvwpdCPIV7RPcHlQWhH1FHIfRVmA7/hTi1ndfPfVl75ykRnAxtEjuweKBk Elljq4wA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1JZO-0000000BDCQ-21Gd; Tue, 01 Sep 2026 08:10:14 +0000 Received: from mail-wm1-x346.google.com ([2a00:1450:4864:20::346]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1JZM-0000000BD3Z-0R9T for linux-arm-kernel@lists.infradead.org; Tue, 01 Sep 2026 08:10:13 +0000 Received: by mail-wm1-x346.google.com with SMTP id 5b1f17b1804b1-4994d67d0e3so2894675e9.2 for ; Tue, 01 Sep 2026 01:10:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1788250210; x=1788855010; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=L15v7UAbfzgcUH2YusfvRM5sHf1SXeOAjXSMkhubbSs=; b=rHMCU7LPcWK4bnsrt+sizAYOelQi8AyfOplRspy4EqNXs/bbxcLaQ4pm0y1cECAWly Eq3lgSY2W4ew1POD1w+7dfxsTg4MRWJI4yD2XYQvkPIUPUj6nh+diWw9UhidgdC5ONLc ubeiZ42udog5f1xyclN+aXgnRJ4zQpb6HJlW/KQk9Ic/1O5zYXegWaAcWho4LbVVQXMJ EBErI0lfc3a329G9WTOZUUiHFZWqq5itIRwWlUC1i58sgwNFWJCA7LGDiOwIW2Z1ZTw/ iC70qPuF4p3kehYDsgfitl1WuBs46XgnDxCVvmkv46bAl9UgEC41rsp0ZmtkPz/RLsq5 QczA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788250210; x=1788855010; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=L15v7UAbfzgcUH2YusfvRM5sHf1SXeOAjXSMkhubbSs=; b=sTdPbiVJN/yn+eSMJ8cJsWgd/mOnYh+RSZKxQRUOw8A32Slls070HoCDb+C3Cy+etM nKmnDGcssrtc8aULJjsmXBKLyRtpVZKpbFmoo7cl6faOiVftaXgMD9EcBpintCYQlAo5 SfBwsjR6F+cgaFYBMMr/AYOslxbSGnh56UcMEBFBhFNC1+s2cqchmyQv3ZYqa3h6rRLb ZfZoaeI5KZXVVxMUqkiEztId7E5t735ovqBql8EWpdxxzgAC1+x7XFlioywTK3ZcEs0E V2SOct83BlXEUMjfBSU+k/XSpSfp2WrP2TXyzvSgQD6Wf2HgbriWeYMJOEQKWQ7JyTSI u46Q== X-Forwarded-Encrypted: i=1; AHgh+RqmnnVyU2kV4le/qzILOcJRQI6sXxoiYVI/7ulpJRdCFM6PZyKhVmdcIXChqzM0TLh5dFwev65m//zhxnt44bbb@lists.infradead.org X-Gm-Message-State: AFuF++kITSK2KBfX3X90V9Xoihx5ILUtNA0Z6aqe1JpefRuYpzM2h3dr GI/Lh5x6GKzbCV0G4VsdfFCGqDefDFQs1yD8RU2sZYUUxjw/CCKjamAUtFAd/Yc9o0qQvcfGfmT kI8LdrixZQ1FLQ9670eMZMQ== X-Received: from wmdd10.prod.google.com ([2002:a05:600c:a20a:b0:49c:ca44:63de]) (user=vdonnefort job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:c08a:b0:49c:dcf8:5216 with SMTP id 5b1f17b1804b1-49cdcf85238mr69043505e9.6.1788250209691; Tue, 01 Sep 2026 01:10:09 -0700 (PDT) Date: Tue, 1 Sep 2026 09:09:41 +0100 In-Reply-To: <20260901080941.997769-1-vdonnefort@google.com> Mime-Version: 1.0 References: <20260901080941.997769-1-vdonnefort@google.com> X-Mailer: git-send-email 2.55.0.897.gb25b4bd76c-goog Message-ID: <20260901080941.997769-19-vdonnefort@google.com> Subject: [PATCH v5 18/18] KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator From: Vincent Donnefort To: maz@kernel.org, oupton@kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org Cc: joey.gouly@arm.com, seiden@linux.ibm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, kernel-team@android.com, fuad.tabba@linux.dev, qperret@google.com, Vincent Donnefort Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260901_011012_235190_59631DFE X-CRM114-Status: GOOD ( 19.86 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In protected mode, transition the allocation of the simple_ring_buffer structures from the host to the hypervisor using the new pKVM heap allocator. Previously, the host allocated and donated a contiguous backing memory for these structures. In pKVM the hypervisor can now allocate them dynamically. Use the pkvm_call_hyp_req() wrapper in the host to invoke __tracing_load, which automatically handles any top-up requests if the hypervisor runs out of heap memory during allocation. Tested-by: Fuad Tabba Reviewed-by: Fuad Tabba Signed-off-by: Vincent Donnefort diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index fc3a624b0c4c..59314883c094 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -934,7 +934,7 @@ static void handle___tracing_load(struct kvm_cpu_context *host_ctxt) DECLARE_REG(unsigned long, desc_hva, host_ctxt, 1); DECLARE_REG(size_t, desc_size, host_ctxt, 2); - cpu_reg(host_ctxt, 1) = __tracing_load(desc_hva, desc_size); + errno_to_smccc(__tracing_load(desc_hva, desc_size), host_ctxt); } static void handle___tracing_unload(struct kvm_cpu_context *host_ctxt) diff --git a/arch/arm64/kvm/hyp/nvhe/trace.c b/arch/arm64/kvm/hyp/nvhe/trace.c index e34f9133e5d9..45322dec6201 100644 --- a/arch/arm64/kvm/hyp/nvhe/trace.c +++ b/arch/arm64/kvm/hyp/nvhe/trace.c @@ -4,6 +4,7 @@ * Author: Vincent Donnefort */ +#include #include #include #include @@ -62,18 +63,34 @@ static void __release_host_mem(void *start, u64 size) WARN_ON(__pkvm_hyp_donate_host(hyp_virt_to_pfn(start), size >> PAGE_SHIFT)); } -static int hyp_trace_buffer_load_bpage_backing(struct hyp_trace_buffer *trace_buffer, - struct hyp_trace_desc *desc) +static int hyp_trace_buffer_alloc_bpages(struct hyp_trace_buffer *trace_buffer, + struct hyp_trace_desc *desc) { - void *start = (void *)kern_hyp_va(desc->bpages_backing_start); - size_t size = desc->bpages_backing_size; + void *start; + size_t size; int ret; - ret = __admit_host_mem(start, size); - if (ret) - return ret; + if (is_protected_kvm_enabled()) { + struct ring_buffer_desc *rb_desc; + int cpu; - memset(start, 0, size); + size = 0; + for_each_ring_buffer_desc(rb_desc, cpu, &desc->trace_buffer_desc) + size += rb_desc->nr_page_va * sizeof(struct simple_buffer_page); + + start = hyp_alloc(size); + if (!start) + return hyp_alloc_errno(); + } else { + start = (void *)kern_hyp_va(desc->bpages_backing_start); + size = desc->bpages_backing_size; + + ret = __admit_host_mem(start, size); + if (ret) + return ret; + + memset(start, 0, size); + } trace_buffer->bpages_backing_start = start; trace_buffer->bpages_backing_size = size; @@ -81,7 +98,7 @@ static int hyp_trace_buffer_load_bpage_backing(struct hyp_trace_buffer *trace_bu return 0; } -static void hyp_trace_buffer_unload_bpage_backing(struct hyp_trace_buffer *trace_buffer) +static void hyp_trace_buffer_free_bpages(struct hyp_trace_buffer *trace_buffer) { void *start = trace_buffer->bpages_backing_start; size_t size = trace_buffer->bpages_backing_size; @@ -89,9 +106,12 @@ static void hyp_trace_buffer_unload_bpage_backing(struct hyp_trace_buffer *trace if (!size) return; - memset(start, 0, size); - - __release_host_mem(start, size); + if (is_protected_kvm_enabled()) { + hyp_free(start); + } else { + memset(start, 0, size); + __release_host_mem(start, size); + } trace_buffer->bpages_backing_start = 0; trace_buffer->bpages_backing_size = 0; @@ -128,7 +148,7 @@ static void hyp_trace_buffer_unload(struct hyp_trace_buffer *trace_buffer) simple_ring_buffer_unload_mm(per_cpu_ptr(trace_buffer->simple_rbs, cpu), __unpin_shared_page); - hyp_trace_buffer_unload_bpage_backing(trace_buffer); + hyp_trace_buffer_free_bpages(trace_buffer); } static int hyp_trace_buffer_load(struct hyp_trace_buffer *trace_buffer, @@ -143,7 +163,7 @@ static int hyp_trace_buffer_load(struct hyp_trace_buffer *trace_buffer, if (hyp_trace_buffer_loaded(trace_buffer)) return -EINVAL; - ret = hyp_trace_buffer_load_bpage_backing(trace_buffer, desc); + ret = hyp_trace_buffer_alloc_bpages(trace_buffer, desc); if (ret) return ret; @@ -168,18 +188,16 @@ static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz { struct ring_buffer_desc *rb_desc; unsigned int cpu; - size_t nr_bpages; void *desc_end; if (!is_protected_kvm_enabled()) return true; /* - * Both desc_size and bpages_backing_size are untrusted host-provided - * values. We rely on __pkvm_host_donate_hyp() to enforce their validity. + * desc_size is an untrusted host-provided value. We rely on + * __pkvm_host_donate_hyp() to enforce its validity. */ desc_end = (void *)desc + desc_size; - nr_bpages = desc->bpages_backing_size / sizeof(struct simple_buffer_page); if (desc->trace_buffer_desc.nr_cpus != hyp_nr_cpus) return false; @@ -197,17 +215,8 @@ static bool hyp_trace_desc_is_valid(struct hyp_trace_desc *desc, size_t desc_siz if ((void *)rb_desc + struct_size(rb_desc, page_va, rb_desc->nr_page_va) > desc_end) return false; - /* Overflow bpages backing memory? */ - if (nr_bpages < rb_desc->nr_page_va) - return false; - - if (cpu >= hyp_nr_cpus) - return false; - if (cpu != rb_desc->cpu) return false; - - nr_bpages -= rb_desc->nr_page_va; } return true; diff --git a/arch/arm64/kvm/hyp_trace.c b/arch/arm64/kvm/hyp_trace.c index 9644c424819b..a72ad1cc429d 100644 --- a/arch/arm64/kvm/hyp_trace.c +++ b/arch/arm64/kvm/hyp_trace.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "hyp_trace.h" @@ -151,11 +152,18 @@ static void __unshare_page(unsigned long va) static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_buffer, size_t size) { - int nr_bpages = (PAGE_ALIGN(size) / PAGE_SIZE) + 1; size_t backing_size; + int nr_bpages; void *start; int ret; + if (is_protected_kvm_enabled()) { + trace_buffer->desc->bpages_backing_start = 0; + trace_buffer->desc->bpages_backing_size = 0; + return 0; + } + + nr_bpages = (PAGE_ALIGN(size) / PAGE_SIZE) + 1; backing_size = PAGE_ALIGN(sizeof(struct simple_buffer_page) * nr_bpages * num_possible_cpus()); @@ -177,6 +185,9 @@ static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_ static void hyp_trace_buffer_free_bpages_backing(struct hyp_trace_buffer *trace_buffer) { + if (!trace_buffer->desc->bpages_backing_start) + return; + free_pages_exact((void *)trace_buffer->desc->bpages_backing_start, trace_buffer->desc->bpages_backing_size); } @@ -269,7 +280,7 @@ static struct trace_buffer_desc *hyp_trace_load(unsigned long size, void *priv) if (ret) goto err_free_buffer; - ret = kvm_call_hyp_nvhe(__tracing_load, (unsigned long)desc, desc_size); + ret = pkvm_call_hyp_req(__tracing_load, (unsigned long)desc, desc_size); if (ret) goto err_unload_pages; -- 2.55.0.897.gb25b4bd76c-goog