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: 196106 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769924dyc; Fri, 2 Feb 2024 16:12:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHWgOXmmX9z5A1swlkHCMGMo4OhrJecuKnnlMv+mNzWDYo8XbRXAAQ7QXK9/NzzmBjh/pvL X-Received: by 2002:aa7:d890:0:b0:560:799:f802 with SMTP id u16-20020aa7d890000000b005600799f802mr713754edq.21.1706919121828; Fri, 02 Feb 2024 16:12:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919121; cv=pass; d=google.com; s=arc-20160816; b=ZZcD+jUg1WAMIre9pYEZ9YTuqhjVglOnCbfYhDjSHbccDT62cK7UefR8mkx1s7SgPU kpZr4a/sOtogYPV0nNEq/ldUn+oRmSkyqKkaDvAfVCpvaTi5t/A63pPTCQWOlbl9su72 RXi3DPUPmqXKlh7iCMfCAfLdqTWa1aaN6/XrUsf5qRAgVqMyBtEGE+GOSktCm5hQJCWV v6x8P1fC0IwPoU0QGcfh5TVjoNhSi1j9H1lwnZystyTPbDhQiqtpFNTl1m6zpf+vAKj9 rpp0a8li9vLvulKu1r8K+fYYSTv4aTDLpnASvQ4meHA98Le4hCO2lb5gHsGl8o6MtrHm zIOQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=5qit/ax01cjsi8jn+OF5XBDYnQC7hsp7dQaMdNEMAzo=; fh=77zCzlRNJ7R9RcV+rk/vZ+5U93O2r/1eOCRCthCDPVM=; b=dx72eSWwNlQSpvObPYQiuR3MFZyN23aYOHTJ32ih+fSVwgQUnLcfN+935qj76ydYgW WB3NLTEA+OShWjIVO8zaMFRq8iNQsfrUr9PLQ/4HWJtQXEKxHQFyYQJSBlDTuqHSB+um 4t6kG7/xijn4/1AmzRoCI1smflWPOOSMhX6lmClS1TwQlE0bpAj5uZKZgtRjQu/SpaY5 Ov8u9JjOQBdCwQ4jmM5FRiU587cL1rRhTV5X+6qQWvpME3UjCV3KHCyXs0vGwYhvpYZ1 FMTvnLOrPdOfPN4TH4Y+jGyZ05DSFXEq1pljlpvxbalQ7xSj5R5SD6E6S15UMwuivKVr wnng==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xBbbsGRc; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCXNfu5Pq3Ozjsr5vuX3Er2ochihw7Qt2cjjbo3G8Me3xZkEnc1mvbNpnE6vZxHYiR528XDRrBdxYI35eJrbsyX9SjJgzw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a8-20020a509b48000000b0055fc426b511si1267329edj.641.2024.02.02.16.12.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:12:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xBbbsGRc; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 3E2D31F2262A for ; Sat, 3 Feb 2024 00:12:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B6FB11738; Sat, 3 Feb 2024 00:09:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xBbbsGRc" 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 CDD628F72 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6041e5b4367so38749347b3.3 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=FZsZJACWwYeiaSYRfvbwIcW2LdYaq9aQA7l6cMminLAL/lRYD2SWKapPhwrfVLIXIw yj3GLK+gUWIC/SIU0ECuBkoUzee3gOL7QotNWmSJYzhbyC4P4sOkj2KET3W/QyAGGuAg EWdbPQHUJ2rgcR+5EfSxg/o0+CjYvlKgZweCCH55I2Yg68BDIFqLlJgCSbzATVZm1262 /zrQ6Q0QWfg+dSZdzRYbjuiQpZZNcBw21Y4NrKp1GZEYnn7T5vQEDZgGrrVyi9iXsp4m crrV+TmsQ+vD+zuCTOrT53equijy7QbSJDL5SYVSbZIikZW71HMVLLEIq8TE5D8r88X/ l+RA== X-Gm-Message-State: AOJu0YxpCKN3SF/VZS8NQu0ncMzRqOv9UvlnoMmJFzJ+aFXcqm0n4P+O 1Y0F/euo28k3QkImBFGt592tjzDRgXexEp9ABVgBU3mzaqlU4ra6WzmzKleE/nFJk8CD3brMUhl aCQ== 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: linux-kernel@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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834424988780166 X-GMAIL-MSGID: 1789834424988780166 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 *