Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (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 85EF3382365 for ; Tue, 1 Sep 2026 08:10:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.70 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788250213; cv=none; b=Cu48rCpJHpFuwBXm1itpx1RN9DGTCc0DlXyzZMUs+Cphs/ECzGCQCuUWxDRGk8EPdf/DU61cF4ENFZZ0vSCfA1ErfxTQUTSlOkHjtvXI5src6pYFpPfUnTXk7aI9c1Lly8pYxrK6qSPQRZji9xI5Zf0xdSf6BF/uN3K1jeAZSPs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788250213; c=relaxed/simple; bh=KdegU/1PX6Mo+wT2krxtrKfkkaxgeCUIaEdZddwioqM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kSFGdoFLqih3FlslfLxSIuXtJTtNrf+LEi/yxtbSXJ4wDcuVaDEHYQs80nq7Qa/+XRcJxvI2rhN+68II55JNOhEgB2zaRSfkWHDNzGlR9WKiaY6PsrRRz6MprVS+nj3nox+UQKubPAI7hkNMLCsFuLixb8wTBF7SfOnanUlkfWo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=j7V1j0bz; arc=none smtp.client-ip=209.85.128.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j7V1j0bz" Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-49cd83ca361so7915655e9.1 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.linux.dev; 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=j7V1j0bzwhyaUcrad8gxR9ezWPZado8pnIB2FADSRwdVDEaUyI2vGKA7xPdDUiYiGm olruLDTug6LTS6Gal4DzoW99rFkgMOqCNOcZIpVHo4l3aHcYpeYaqcb9HvkUG8NG+C8k ITdGV/rijPlUfVw1sihpef3AKBn4omCk3qmAfPpZ0a7VCmCeAfk5wnNMD8hgCCwNXckn 1wdPAX7oa4Z4Winbb9Q+ZNVyL+CU3P3WMgkGpMQo/xvTrCVL/Q1wDTDUUe9DI86O2UqO RUaTOuDEChHJ93j6CtjhNQwLeAWR/RzPzMKKtWwC3e+GYq+IE5N9es+TeAmfs4684OW2 t95A== 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=mdrzxltNO7OPPceZfwDQMo8Ba44ll3CCbz8Zr2zsNOXEwhc26ftvUw0nBl10tGa5oO pXVArEhKX5V0CZg0WoalMZTuFql0rlBjdkhNT+KMOjs3YbWp4PmVoiWGeu8P2mGJCP9H bu5dA7Y71FXm11FA+7Il0fsOoiVLbcGRo8F8dR6R8vZI7rKXNym0rVbv3j3yQqUFBiFi lcpsAfESqd3U6zQ1U5D9Nwp28l7RFRTFcrX8AQn3fuHn/oY3MSjryRnKbqq5a9a16zw/ 1UP+Xr9hsAPPwsebgbFXRCMCCBEz+/63VZ4XN40qMwCMARAgO0IhAggSAHmTo74YqbRC Fhbg== X-Forwarded-Encrypted: i=1; AHgh+Rr7gfAVTgVKdOYM+6I0J+VxakwUZQs7V+g051rpO4kx+jnzUYcyBYOVBBFrJp+yCN15cFCck/I=@lists.linux.dev X-Gm-Message-State: AFuF++mJXj72ZEk4GhEeOWqzKVDu4W7t4R17UmKEoWTRZv6SlgXILqb+ TY+XPjtARIGX9xXvLadLOsC6BTXjyNjYp1yyLPjo1SDHhYsbEHNbgeixi6GnKBi38NS5T6yOcY+ vbgqCoEZaHnRqx2RYUYDXjA== 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> Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: 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" 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