Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp573715lqb; Wed, 17 Apr 2024 05:18:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW4qoeqHHVN4dJu/A0QnJCFATHS90EjMosxwo4diwTJWW2HEm0aiZrUQAxx0DUS8iK7Ml+AhE9OmosgfKmulhTfVgdnPjSMQ26A69ct9A== X-Google-Smtp-Source: AGHT+IFaYDXg2n8Gv944h7bJhSLWMaho8mliSwuCC55GGEJARSZrYASGd6qs44PmT+m9WuO9x6QL X-Received: by 2002:a17:906:3756:b0:a55:3240:ba3b with SMTP id e22-20020a170906375600b00a553240ba3bmr4175368ejc.8.1713356311505; Wed, 17 Apr 2024 05:18:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713356311; cv=pass; d=google.com; s=arc-20160816; b=GUPLHVN1xbOubt4xRqFK9Xw5Y/8ojiZ6hZ1D/yziZuRH+8J33+9arCTimA6bvYijuu 9jjlKXNgSnnOf4rs8ITbv1zn89hxMaXug4/9PKZ8OgT1uMBorstwSNklO7YJ9UlkpXM3 M8cky5dbPilZx9JjrBoMyBO6bdjLE3NspEEaE7tQreUZuieZInsA2FV2dxLgzZeMLKUB tYDf/fxgqtixtx1MgGZ70XXYeOBPy+4OSFOH3YiYa/DSjcYzOLPRym33Lom5Em/vJY5u PTPm8CPMR67orQ0H4MIQnWIFVwAqpKY1aYcqfmA2VFQMRqpIntXHvARXsjxw/LD2p7OD JveA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=Z1xeyxCIrXbRqe02PXMvaFPCYUz5h2TFi+FFPVK4xZ0=; fh=NvY/QBtuwW1cUH4FA6OQjHrT/qC6Fj9mkqdAnrPRqvk=; b=Fg+efGIjhYpJB0F0YbaaqnqGCLU11SVv/cN0268XqbL/58lL03LeijB7/IF49X7zkO IBFcVcw0DlaVo3RbpCqlZ8h5mmte1sZVv3Lpl+JQwVkODLJb01K7X7NzeBXu4IaCFw6l 2xs/40VUgMM0FE6PqRr1j5ZldD4bB6QLDmc8hQwONKMODpTePg9rv2GERKhP+RvnOEVV H/RtXZtTYDenJgB4HV0TfrdzmFeGross9po0fqEql9LntsWP49uMYEaMMLb6ZHxeE4xO +f1UUalZ7wANElQSUGIf/UHkEt+wG+tkPuWYAfErVEomj2I8BnQTxmlq12x50JondU2O 3iRw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NRK9Yy4C; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-148468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-148468-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id du8-20020a17090772c800b00a524e64fb99si4550234ejc.579.2024.04.17.05.18.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 05:18:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-148468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NRK9Yy4C; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-148468-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-148468-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 002BE1F22678 for ; Wed, 17 Apr 2024 12:18:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 69D7D13D533; Wed, 17 Apr 2024 12:18:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NRK9Yy4C" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E061113BC21 for ; Wed, 17 Apr 2024 12:18:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713356298; cv=none; b=a8+X29PSGxOTnAlYTJWqFXdCIbUWPPvEkpCTzYLLUHq9hkDsZteYzQQBqbnqyhwmD00vzg3NXdbdHTYC8iKNOU9j+UflGLnLn1jghic/g5ZWqhzDtBwW/NKRwprTIXeFI47U7znnNgOz6crbF8aKboh0dfeHZlQGWckOD5A/fBg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713356298; c=relaxed/simple; bh=5sPbX2601ThBLr4CINQAAZzaaohjANPkkqRezSuwCDQ=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=rU3VEFKafW/dW3bVA5V/2Zq3FjkhxwZcrituyM5pB87uphD2XsHHns9Det2dr/4KPR5CQIgcOGVRuSl88SLOIx+/mvnCusUl9F1EF0mrOG5kWwSXwuul6u/wNjAEYGI6KL/y3WZsTDR8Q10LF8x6ihrmkH3R58i2+0RAEZLwEFQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=NRK9Yy4C; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713356295; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z1xeyxCIrXbRqe02PXMvaFPCYUz5h2TFi+FFPVK4xZ0=; b=NRK9Yy4Ci8fwG4QRghd4vcJ3SJgY9ipY264hFZ45HqIkXkOnXnj9OaC6R9XXI/N+sY0pJ+ lOAIG25LxbD4DK61vT9A3kTIh6GAEkSc8nKDpVr+RSj068603xJFZKwZqIT9+7srzMrH5t lMH6iegsmZvhTGZLJ70FOAmTa9YlnwQ= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-349-6lHditKaPLSCPvcNYbTqVA-1; Wed, 17 Apr 2024 08:18:14 -0400 X-MC-Unique: 6lHditKaPLSCPvcNYbTqVA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3479b511725so2405527f8f.3 for ; Wed, 17 Apr 2024 05:18:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713356293; x=1713961093; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z1xeyxCIrXbRqe02PXMvaFPCYUz5h2TFi+FFPVK4xZ0=; b=qxilSUkvoqooUAlufY5kcuPrv+Esf0arMAibwHA1/JIwazuDkn1ye1v8JDKzqrEQ/P k4AC1w34Wk+2C2c7Zb4Yl9rVxzqxtd/24BQQH4o2c5fv0tblARj7PkTYWKcg+xpeSW2B 4lNAjRrdCnGkxkoG+GZZeg4Ojdlplk+wYXFCl7Fclclq6WJhbkzy14gJom4TYpttmkPw wHQP7huQ6Qr+dVxJBiZyj9LQRPPROt5F/rmPk7kR3TsVqDo+qm5nxtRw2qbeU4SzSjv+ c5OYYIn/6upXszPJ+QqFMjX8ei1zityKKQuTvFjPkj2v7TOHMJMVaC1Ob7PviLcZL3Sn 5gjg== X-Forwarded-Encrypted: i=1; AJvYcCUN8L/ptc9+A/OqMm02NPUFHfMxA1zJULw+Bl5+Tn9AjQbNa7p7i18iJh+ji7KR93aKHwKXnuwP4rxu7J9tT9knj/1GLLjXeAlxMx2j X-Gm-Message-State: AOJu0YzjXb/HYLYsav6CoYUde4X1WRWDJj+RdHPmWULpv95RIpEUHCqp +Z6Ez/HXALgSJfsNtrPcFM21vsQMYq/UkEsuvZYk0wpJb65kdA9vYSpYdqb/hBdZL1/dt9s475M Q2gLj2VBbuXGaYLS3VyzMmzGVQmOI/kW81tWbOnOgkKwaWr7bLkDPA7j12fwakrFBzqqADoKWFO z0w3kYG4r2a/ubPRm74feYlWSi75H6j0caR5jW X-Received: by 2002:adf:e691:0:b0:349:c5da:f234 with SMTP id r17-20020adfe691000000b00349c5daf234mr1166775wrm.24.1713356293316; Wed, 17 Apr 2024 05:18:13 -0700 (PDT) X-Received: by 2002:adf:e691:0:b0:349:c5da:f234 with SMTP id r17-20020adfe691000000b00349c5daf234mr1166757wrm.24.1713356292989; Wed, 17 Apr 2024 05:18:12 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <7138a3bc00ea8d3cbe0e59df15f8c22027005b59.1712785629.git.isaku.yamahata@intel.com> In-Reply-To: <7138a3bc00ea8d3cbe0e59df15f8c22027005b59.1712785629.git.isaku.yamahata@intel.com> From: Paolo Bonzini Date: Wed, 17 Apr 2024 14:18:01 +0200 Message-ID: Subject: Re: [PATCH v2 06/10] KVM: x86: Implement kvm_arch_vcpu_map_memory() To: isaku.yamahata@intel.com Cc: kvm@vger.kernel.org, isaku.yamahata@gmail.com, linux-kernel@vger.kernel.org, Sean Christopherson , Michael Roth , David Matlack , Federico Parola , Kai Huang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 11, 2024 at 12:08=E2=80=AFAM wrote: > > From: Isaku Yamahata > > Wire KVM_MAP_MEMORY ioctl to kvm_mmu_map_tdp_page() to populate guest > memory. When KVM_CREATE_VCPU creates vCPU, it initializes the x86 > KVM MMU part by kvm_mmu_create() and kvm_init_mmu(). vCPU is ready to > invoke the KVM page fault handler. > > Signed-off-by: Isaku Yamahata > --- > v2: > - Catch up the change of struct kvm_memory_mapping. (Sean) > - Removed mapping level check. Push it down into vendor code. (David, Sea= n) > - Rename goal_level to level. (Sean) > - Drop kvm_arch_pre_vcpu_map_memory(), directly call kvm_mmu_reload(). > (David, Sean) > - Fixed the update of mapping. > --- > arch/x86/kvm/x86.c | 30 ++++++++++++++++++++++++++++++ > 1 file changed, 30 insertions(+) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 2d2619d3eee4..2c765de3531e 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -4713,6 +4713,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, l= ong ext) > case KVM_CAP_VM_DISABLE_NX_HUGE_PAGES: > case KVM_CAP_IRQFD_RESAMPLE: > case KVM_CAP_MEMORY_FAULT_INFO: > + case KVM_CAP_MAP_MEMORY: > r =3D 1; > break; > case KVM_CAP_EXIT_HYPERCALL: > @@ -5867,6 +5868,35 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vc= pu *vcpu, > } > } > > +int kvm_arch_vcpu_map_memory(struct kvm_vcpu *vcpu, > + struct kvm_memory_mapping *mapping) > +{ > + u64 end, error_code =3D 0; > + u8 level =3D PG_LEVEL_4K; > + int r; > + > + /* > + * Shadow paging uses GVA for kvm page fault. The first implemen= tation > + * supports GPA only to avoid confusion. > + */ > + if (!tdp_enabled) > + return -EOPNOTSUPP; > + > + /* reload is optimized for repeated call. */ > + kvm_mmu_reload(vcpu); > + > + r =3D kvm_tdp_map_page(vcpu, mapping->base_address, error_code, &= level); > + if (r) > + return r; > + > + /* mapping->base_address is not necessarily aligned to level-huge= page. */ /* * level can be more than the alignment of mapping->base_address if * the mapping can use a huge page. */ > + end =3D (mapping->base_address & KVM_HPAGE_MASK(level)) + > + KVM_HPAGE_SIZE(level); > + mapping->size -=3D end - mapping->base_address; > + mapping->base_address =3D end; Slightly safer in the case where level is more than the alignment of mapping->base_address: mapped =3D min(mapping->size, end - mapping->base_address); mapping->size -=3D mapped; mapping->base_address +=3D mapped; Paolo > + return r; > +} > + > long kvm_arch_vcpu_ioctl(struct file *filp, > unsigned int ioctl, unsigned long arg) > { > -- > 2.43.2 >