From patchwork Sat Feb 3 00:09:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13543612 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 B23908C1D for ; Sat, 3 Feb 2024 00:09:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918973; cv=none; b=BCRwm1tOpCiCuoVqXb//9Rbifd6WHrZycOXKup75BjrTrydz6TSAod1+ZK4nl9LErm9dZawFNi/m5eyxSWiK3i2Lki8+FZ659iyAffeZqQ5HG9/CJxEx4xW1TPv2tVLdNeilwFCD67a+A5rV1yE4WGk8CwuTgtAkQM2BWTs53cM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918973; c=relaxed/simple; bh=eEsuDAyYhqh0PRIkmb2mwUAqWJ2Pol0Ha6iX400H8fc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DzKOSTIUy5qTwO7bUYFfYXLbC7lLz/skpTIbSAa3v+439+NIR8pO0/dM+zlrkX/5ESXGkmrjZLBlM/uAEAX1C5+rAb5Hk6KmLEdPHED3Z892KVPs2/AdYobkQElFFPeQKh7LzSagHWfXHH84yjX1PCasGE82v71iIP99RXlBg1Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xBbbsGRc; arc=none smtp.client-ip=209.85.128.201 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xBbbsGRc" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60411f95c44so35212607b3.1 for ; Fri, 02 Feb 2024 16:09:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918971; x=1707523771; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=5qit/ax01cjsi8jn+OF5XBDYnQC7hsp7dQaMdNEMAzo=; b=xBbbsGRczX9e6fFUaFftX9mbr+YTAnGcrCF0wxijp5Pg0y8c3hL4zKqkKl2xF/++5D Qim+Kmq+PowZDSCqlfAv9A+iXYl/+3ohyQgHXTg/y9sYDBo6CB3PeKihRh2PuFzG/vWM oYN9q1jqfaxZELkUhzKGnfj8+FDEV4lSRKD1GErcojnu3+G42PzdXbHdqk5pY88gZ8ri qtbJNHCkoIzt1l3duR7NYpH9GYgFAPXIlYmODppjNjbFZ5IozElMJgX9jl5+u+XtxyP9 9ysVF4a5cUaYz3xO0pwbtqUDVs0vbIW0AqlkqtN7KdYYWowJqEdkRiO3cSDrDR9iVb4K jEpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918971; x=1707523771; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5qit/ax01cjsi8jn+OF5XBDYnQC7hsp7dQaMdNEMAzo=; b=TIT2YZ67yEgnfOY/yfz7/1YPV3S5M0x6RJYYnu3vZPbwpCnNDhIEBOCxmP+gGTd8ri Vfc7ZhjqT882aFSYDrO37VIuqyOXtd4W2jGH5iy7yuGH0093sWAi/RsFTiXnhWIM2iJb NEOffMaTVlM7s+vUMXI8hzDptw03AZjJnKPSjJDwY2crgqaGl8WhnANgAHfgCPDdNwh9 1oE6XK/zZ4CKeelV+HMFRNv9TINnxvNjwjAUYcUm1tgeR2nU9oLfDnpurjAC/zw+Ngwj D4bTjVQiXHcqtbtrZIuD7vDHaVjEnOpx99EHGNC+aUQ0cVRiPtYI0MV0CTmdKqf5AjWa QDOA== X-Gm-Message-State: AOJu0YwJAz2a/MlLNhicinvwPoXCJLyGNnJwj4qsi88JKdIhqSTP8/rP HLh6+5ZWIJZGo3C6+pEmrFxZDPVv+NlYuofL+GsY8qbve7NNL49cD4jJrbhBVpspPC4gKPx5bbd Udg== X-Google-Smtp-Source: AGHT+IEn7SwzHm7K9kCm5lO4ksgfDS3a7ro7MEBhevB3F0wiSwy11y/YKaE4P41Zv85Ql7R3gAuHdPatt3E= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:4cd5:0:b0:602:d83f:bf36 with SMTP id z204-20020a814cd5000000b00602d83fbf36mr1585025ywa.0.1706918970863; Fri, 02 Feb 2024 16:09:30 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:11 -0800 In-Reply-To: <20240203000917.376631-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-6-seanjc@google.com> Subject: [PATCH v8 05/10] KVM: selftests: Add support for protected vm_vaddr_* allocations From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda From: Michael Roth Test programs may wish to allocate shared vaddrs for things like sharing memory with the guest. Since protected vms will have their memory encrypted by default an interface is needed to explicitly request shared pages. Implement this by splitting the common code out from vm_vaddr_alloc() and introducing a new vm_vaddr_alloc_shared(). Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerly Tng cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Peter Gonda Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 3 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 26 +++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index a82149305349..cb3159af6db3 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -590,6 +590,9 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_mi vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, enum kvm_mem_region_type type); +vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, + vm_vaddr_t vaddr_min, + enum kvm_mem_region_type type); vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages); vm_vaddr_t __vm_vaddr_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index ea677aa019ef..e7f4f84f2e68 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1431,15 +1431,17 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, return pgidx_start * vm->page_size; } -vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, - enum kvm_mem_region_type type) +static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, + vm_vaddr_t vaddr_min, + enum kvm_mem_region_type type, + bool protected) { uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0); virt_pgd_alloc(vm); - vm_paddr_t paddr = vm_phy_pages_alloc(vm, pages, - KVM_UTIL_MIN_PFN * vm->page_size, - vm->memslots[type]); + vm_paddr_t paddr = __vm_phy_pages_alloc(vm, pages, + KVM_UTIL_MIN_PFN * vm->page_size, + vm->memslots[type], protected); /* * Find an unused range of virtual page addresses of at least @@ -1459,6 +1461,20 @@ vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, return vaddr_start; } +vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, + enum kvm_mem_region_type type) +{ + return ____vm_vaddr_alloc(vm, sz, vaddr_min, type, + vm_arch_has_protected_memory(vm)); +} + +vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, + vm_vaddr_t vaddr_min, + enum kvm_mem_region_type type) +{ + return ____vm_vaddr_alloc(vm, sz, vaddr_min, type, false); +} + /* * VM Virtual Address Allocate *