Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp914807lqt; Fri, 7 Jun 2024 02:32:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU/4Sajygvk5BY2K1TTnTjWNcHFxrC5J6RrjLS9ycY7DWFr3Qhn2YJgw5BZeWFJEUnAtE6lMbp25AxmINc1iXwC1nkNVaSzrEdX8s+V3g== X-Google-Smtp-Source: AGHT+IHzgwp5qdbA3w/PEkn4CWL5bt4nxCcyBo39y8X8nlitGP0YLcrHyWsO+GadlU1cKDxkHUrF X-Received: by 2002:a05:6214:4388:b0:6ae:3ea7:8177 with SMTP id 6a1803df08f44-6b059be17cbmr19559606d6.34.1717752724093; Fri, 07 Jun 2024 02:32:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717752724; cv=pass; d=google.com; s=arc-20160816; b=S4otQW7zmfRX2StxoGGcMWQFy1rVOne4aT2/aFa2YthKBx/sHUJKdL9aQB3qovfV1e D04vHR3yiTL3NGiHWxrKPsUNzEYKwxe9UPdJohsOuefVsUm3danygZ1W57mXOlRLfSnL GO3lRtQp+L6S3YPF0KfXm32EwaBX/ZNIUD1akIkVCUC44HgVXpRWxjgfQVre1678HzSo PeOZ7T3aEOd07cWj2Qx3AKlxDKj8dqSd68JUUVB0xELzJiMaKZ2YFa4TsSk/F0mQPC1K ZREoa1aEQ+irmn2MImDc93WvCMTSE2eTf6DAHiRPInZFX4vmL2YGttA9BLUplDAZFd5u KfcA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=48cZggxqt9GoOhwUTSaRN9PwmINE7avK2UYxff+junQ=; fh=3uUEJgnKmSJGcLWMYiuPi6W2rVl6tT7ykXBA4xjdocU=; b=g8F5C2StP48E4ZUtsDjj0+IgDe9EnVH7mrU6i+rxbTnJJcolDXT8I+VkMyRGVwWtVm hfU4C2IZzzy3JWrK3QpgASBf1aD/Gy19KMECknWZuLTVLvU2x3o8Fy6E5d3DxswG+1mk 9gAwPtY8/nsTjoJZKhE+EqcAZ+h1fjfLHOGPxo18c+oqUHlBnLpFb4lL/VOI2pm41Sz/ r9+VPISp3rrNuLfEwJSoa/OI+H1FluzwiKGOcuNdS+rqCZmlxk9/83pdw4HrqYLLCxCo eQwwO17LzUTaTJqSD0e0z4SyVs+8wSfxb7eyexL3vD8+BjvUwDMo5lrEeTcGO8MbzqcK t6pg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KSHx8tdI; 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-205735-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205735-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6a1803df08f44-6b04f632945si26687126d6.52.2024.06.07.02.32.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 02:32:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205735-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KSHx8tdI; 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-205735-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205735-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A81161C22FE3 for ; Fri, 7 Jun 2024 09:32:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 34DC715CD5C; Fri, 7 Jun 2024 09:31:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KSHx8tdI" 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 A588012FF96 for ; Fri, 7 Jun 2024 09:31:39 +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=1717752701; cv=none; b=NpzXl3riU3aoOXr+ESvDkt+NahCxh/lyYFdzI+kV4UK18vFDOli5mS8TfWZo0TWEcZsEWl68nSHQwKgwX9BW+Ysyt4u7rLTXj8iM/YWjfnFap14RyTidRzS5bmB/7d6L/1TKozuAaJNt2RGvDYKEI4wEsOX2+g9fmv6N3qGvW8M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717752701; c=relaxed/simple; bh=AO1+wudjPdTqHb3LdpkB0AnTgP40Y3xa66MaIoTyz60=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=N530oRuB6vClTw32JANhpfc47uVghQ7XnR4SpZ0G4ej+PSsJnmOnHCD5KK/BJ3L6lK/q3JRPJkXo32pt2mILMrDsODUPUta5JGtPMA9daTHESOfI7ai6H3WpbHgK5kwbGwJyUxPfdaqarEUBmNhFHWHtHNYHyd5/RaX6CfYcG/o= 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=KSHx8tdI; 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=1717752698; 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: in-reply-to:in-reply-to:references:references; bh=48cZggxqt9GoOhwUTSaRN9PwmINE7avK2UYxff+junQ=; b=KSHx8tdIl3knv9QuyEFk+V1vIA9Fskk9rdnEeXaizHpczUkglMl220FLJUUhEAn0P8ufn6 oEYYdC5HoXBtvoXa4WZcn/OG21qhWpYLG0U2xPbLREqsHP5Z2xmq6Qif2Htt7L/m6pz5l2 dOdxMOJIdorb+mwQ3UI0buiu6Cl5lEg= Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-281-BsHPWg6SPGew5wHL3fXOyA-1; Fri, 07 Jun 2024 05:31:37 -0400 X-MC-Unique: BsHPWg6SPGew5wHL3fXOyA-1 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-52b88765386so1806679e87.0 for ; Fri, 07 Jun 2024 02:31:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717752696; x=1718357496; h=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=48cZggxqt9GoOhwUTSaRN9PwmINE7avK2UYxff+junQ=; b=uPPUzyKa30PSHlmUt/NoLyLe528DdweaXkRUySHbMsc28no9r34LG9WC3fxxCatHBF hXCAWjbSfQrFWkf3oTmM+mNBeRbz+yn0m6ZnwPkb0PVrZgx96k5hmiO2GJ38wKE07Xae yg5UZvSa8zqibxFVPcTf+PVtXiwp1JEYD5KNXtf8nLkTWNj3jIa3enUCs77WvkYLTdBf huUY9J0aH5z8rhgpBHKSqvd7oghAMvOSMUk1hfb+BQTEcHx3jULm/lMZHulUPECLebP7 R/GceEMJfOFcvWymb1dnkOtVMzMit/QzODKB0PF8c+743FT9I136E1goIDj8GYI8XELj 93Tw== X-Forwarded-Encrypted: i=1; AJvYcCX3dWY6JciHzSJol2CpBsC8+YlAQu6iNuZqpft7OnKrd8aW57kHfat6nbwhRjvgOqstAl0w2T8oDfwHRokxECGQoWgydd5ifmyjpgGv X-Gm-Message-State: AOJu0YyBwai8lO33BUZHwESPRIm8d5RgBDjdgHlFscVC4xGOqmdT9HgU XDOckQfnGEAQjhhprZEOrnAaLOCDYis6cFZxpQbGSMXEK5qdfi4vbJb27Chu2Y6nunXuIfNfSHj UKHuIBFbRdNj+IYlTB8QoezI/qwseG2s7H5GnCFqXL1ndHq0waVdbSotj0CNSPU++D7/arG18OM UzKbLmHNIX8yzK4TRadlkMrO6aFkiyb3oy6Tue X-Received: by 2002:a05:6512:3b12:b0:52b:bc04:cc6b with SMTP id 2adb3069b0e04-52bbc04cd38mr1154033e87.49.1717752696043; Fri, 07 Jun 2024 02:31:36 -0700 (PDT) X-Received: by 2002:a05:6512:3b12:b0:52b:bc04:cc6b with SMTP id 2adb3069b0e04-52bbc04cd38mr1153676e87.49.1717752687013; Fri, 07 Jun 2024 02:31:27 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240530210714.364118-1-rick.p.edgecombe@intel.com> <20240530210714.364118-16-rick.p.edgecombe@intel.com> In-Reply-To: <20240530210714.364118-16-rick.p.edgecombe@intel.com> From: Paolo Bonzini Date: Fri, 7 Jun 2024 11:31:14 +0200 Message-ID: Subject: Re: [PATCH v2 15/15] KVM: x86/tdp_mmu: Add a helper function to walk down the TDP MMU To: Rick Edgecombe Cc: seanjc@google.com, kvm@vger.kernel.org, kai.huang@intel.com, dmatlack@google.com, erdemaktas@google.com, isaku.yamahata@gmail.com, linux-kernel@vger.kernel.org, sagis@google.com, yan.y.zhao@intel.com, Isaku Yamahata Content-Type: text/plain; charset="UTF-8" > -int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, > - int *root_level) > +static int __kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, > + enum kvm_tdp_mmu_root_types root_type) > { > - struct kvm_mmu_page *root = root_to_sp(vcpu->arch.mmu->root.hpa); > + struct kvm_mmu_page *root = tdp_mmu_get_root(vcpu, root_type); I think this function should take the struct kvm_mmu_page * directly. > +{ > + *root_level = vcpu->arch.mmu->root_role.level; > + > + return __kvm_tdp_mmu_get_walk(vcpu, addr, sptes, KVM_DIRECT_ROOTS); Here you pass root_to_sp(vcpu->arch.mmu->root.hpa); > +int kvm_tdp_mmu_get_walk_mirror_pfn(struct kvm_vcpu *vcpu, u64 gpa, > + kvm_pfn_t *pfn) > +{ > + u64 sptes[PT64_ROOT_MAX_LEVEL + 1], spte; > + int leaf; > + > + lockdep_assert_held(&vcpu->kvm->mmu_lock); > + > + rcu_read_lock(); > + leaf = __kvm_tdp_mmu_get_walk(vcpu, gpa, sptes, KVM_MIRROR_ROOTS); and likewise here. You might also consider using a kvm_mmu_root_info for the mirror root, even though the pgd field is not used. Then __kvm_tdp_mmu_get_walk() can take a struct kvm_mmu_root_info * instead. kvm_tdp_mmu_get_walk_mirror_pfn() doesn't belong in this series, but introducing __kvm_tdp_mmu_get_walk() can stay here. Looking at the later patch, which uses kvm_tdp_mmu_get_walk_mirror_pfn(), I think this function is a bit overkill. I'll do a pre-review of the init_mem_region function, especially the usage of kvm_gmem_populate: + slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); + if (!kvm_slot_can_be_private(slot) || !kvm_mem_is_private(kvm, gfn)) { + ret = -EFAULT; + goto out_put_page; + } The slots_lock is taken, so checking kvm_slot_can_be_private is unnecessary. Checking kvm_mem_is_private perhaps should also be done in kvm_gmem_populate() itself. I'll send a patch. + read_lock(&kvm->mmu_lock); + + ret = kvm_tdp_mmu_get_walk_mirror_pfn(vcpu, gpa, &mmu_pfn); + if (ret < 0) + goto out; + if (ret > PG_LEVEL_4K) { + ret = -EINVAL; + goto out; + } + if (mmu_pfn != pfn) { + ret = -EAGAIN; + goto out; + } If you require pre-faulting, you don't need to return mmu_pfn and things would be seriously wrong if the two didn't match, wouldn't they? You are executing with the filemap_invalidate_lock() taken, and therefore cannot race with kvm_gmem_punch_hole(). (Soapbox mode on: this is the advantage of putting the locking/looping logic in common code, kvm_gmem_populate() in this case). Therefore, I think kvm_tdp_mmu_get_walk_mirror_pfn() can be replaced just with int kvm_tdp_mmu_gpa_is_mapped(struct kvm_vcpu *vcpu, u64 gpa) { struct kvm *kvm = vcpu->kvm bool is_direct = !kvm_has_mirrored_tdp(...) || (gpa & kvm->arch.direct_mask); hpa_t root = is_direct ? ... : ...; lockdep_assert_held(&vcpu->kvm->mmu_lock); rcu_read_lock(); leaf = __kvm_tdp_mmu_get_walk(vcpu, gpa, sptes, root_to_sp(root)); rcu_read_unlock(); if (leaf < 0) return false; spte = sptes[leaf]; return is_shadow_present_pte(spte) && is_last_spte(spte, leaf); } EXPORT_SYMBOL_GPL(kvm_tdp_mmu_gpa_is_mapped); + while (region.nr_pages) { + if (signal_pending(current)) { + ret = -EINTR; + break; + } Checking signal_pending() should be done in kvm_gmem_populate() - again, I'll take care of that. The rest of the loop is not necessary - just call kvm_gmem_populate() with the whole range and enjoy. You get a nice API that is consistent with the intended KVM_PREFAULT_MEMORY ioctl, because kvm_gmem_populate() returns the number of pages it has processed and you can use that to massage and copy back the struct kvm_tdx_init_mem_region. + arg = (struct tdx_gmem_post_populate_arg) { + .vcpu = vcpu, + .flags = cmd->flags, + }; + gmem_ret = kvm_gmem_populate(kvm, gpa_to_gfn(region.gpa), + u64_to_user_ptr(region.source_addr), + 1, tdx_gmem_post_populate, &arg); Paolo