From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 B61B62C17A1 for ; Tue, 23 Dec 2025 22:29:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766528967; cv=none; b=RCRhC07MjFlgZfr46OZTN+ZZrfrC0Q5Bi53Z+pInIO5w0DZzWyKT4IMIm5PcKPbNv5d1quZLzZXMzDT9qK3G0c8FtfNVZAY9ktK/WFhVdolpkubUd8unrzqdP35NC7i6IO8ccGcb3I9rx5EB6uZYP8mSjrg9AAVatHhBwLxU+Kg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766528967; c=relaxed/simple; bh=OkyDThQbjU98Y48PTqWT0v4KhKIhS5MRAQxWjXA7sZ4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=O3ZwyG14zbkogGOCjYT1Ygaz2YmybaXrybd6j36/SAlttK9zbOAROEu7GoROcUYNbadUe257U0RxPptjW7UpEWFefWjqwoXSPxB71LaKmHvClo6QnR9Li8eaMIh7gPbRwFSQR4G6+B5aDpShIXPppznl7gqws+Eqoe12jgb6iY4= 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=ChYbKd4r; arc=none smtp.client-ip=209.85.210.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="ChYbKd4r" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7b82c2c2ca2so9975481b3a.1 for ; Tue, 23 Dec 2025 14:29:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1766528965; x=1767133765; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PIzYm1357PLBlwJSpJ9Q9Pnkpgmtjnd2X3PzBGc4u0E=; b=ChYbKd4rGlVos7N+6eXe4rdct6ZaUxDWBV0Oz3aseRQEHbx/0biUCLmsUu4WQqwXCp /NNH4U/6oxNYMnaERsNh7OSlAlzLB9hm7CKPf6SySdb00teMmGMfN/e5mDhMVpl6ZFgf k1UAv7e+dqbbC4ZWMoSew54Q9RhXPSLuwYNZwqvCY25djgOwUVHsJOY6u4C5ZbjkGHDw VEckazEpbmC0IO/1qhmcp6i4CjYCkx/7HsEdUFVmSrLFH1l9t4eD7yjfJHg9Tqe9EUef HywkgIRjinVHtVMNq0wSSwKC4rcdUC13V0OhoHLHnBv5rpskKLP4pvzvuzmYUfUT0DQX mLGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766528965; x=1767133765; h=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; bh=PIzYm1357PLBlwJSpJ9Q9Pnkpgmtjnd2X3PzBGc4u0E=; b=Cu+oaiFE0iRgGR1dJ9D53N5RINFAhP3WpMAimTM59WRscoYgD5Cop/droCvLn+UXfu MlQOb0i3H4fOqkQtYGDa5Qw+0ypbcX0FsQ3yLji/xGSTMkErzniYO4O5wAHzT2zTLW13 YQfQLkOq8SXvz3uAXti95dracuJooDg25hqqk5wqE9aZKUkcCYyCJiWbSplS7/IBZIy/ Ov/BSCo6sFczVcFwfiumuEvbwIZJ5g4xw6KnTTvUIKeQjhu5WtCQ1MNfggSIMhiewXP/ fj/iYd6MlyhP0I1qMXo9jVM66hdBz4u9bH7jhAK4mqFEO73JKSqiQxUkZ7qLHNGvOM7E rY7w== X-Forwarded-Encrypted: i=1; AJvYcCVFOvRSVBt9/sV2A3j9FO4LrCUNzMYb50NndESOm24Yo8025kDT6nro0V5htiMlkKtQXdRfiJhhJDVbJII=@vger.kernel.org X-Gm-Message-State: AOJu0YwE9RvMhApL3aYmWIyqI458J+UABorBmLhX7ltdV+YRgtGmNxO/ cdR1FQshZhos5QI+HzFhG8Bh7g3iKxZdfKIAgXvoiwAyQymSBbB0SLyaY0+bhAwFKyAbhbxvqnz Ytb+nQA== X-Google-Smtp-Source: AGHT+IEhDeR7Xm0mAYb+x0wfXfdsI2qgS00NPGQ3TSa3ZXT1lYGjl3a1TULge+FN3tNTpCcrJy4FLsEJhhk= X-Received: from pgbdq13.prod.google.com ([2002:a05:6a02:f8d:b0:bc7:4ee3:3185]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:a103:b0:35d:5d40:6d79 with SMTP id adf61e73a8af0-376a88c7411mr15665035637.12.1766528965091; Tue, 23 Dec 2025 14:29:25 -0800 (PST) Date: Tue, 23 Dec 2025 14:29:23 -0800 In-Reply-To: <20251127013440.3324671-7-yosry.ahmed@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251127013440.3324671-1-yosry.ahmed@linux.dev> <20251127013440.3324671-7-yosry.ahmed@linux.dev> Message-ID: Subject: Re: [PATCH v3 06/16] KVM: selftests: Introduce struct kvm_mmu From: Sean Christopherson To: Yosry Ahmed Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" On Thu, Nov 27, 2025, Yosry Ahmed wrote: > In preparation for generalizing the virt mapping functions to work with > TDP page tables, introduce struct kvm_mmu. This struct currently only > holds the root GPA and number of page table levels. Parameterize virt > mapping functions by the kvm_mmu, and use the root GPA and page table > levels instead of hardcoding vm->pgd and vm->pgtable_levels. > > There's a subtle change here, instead of checking that the parent > pointer is the address of the vm->pgd, check if the value pointed at by > the parent pointer is the root GPA (i.e. the value of vm->pgd in this > case). No change in behavior expected. > > Opportunistically, switch the ordering of the checks in the assertion in > virt_get_pte(), as it makes more sense to check if the parent PTE is the > root (in which case, not a PTE) before checking the present flag. > > vm->arch.mmu is dynamically allocated to avoid a circular dependency > chain if kvm_util_arch.h includes processor.h for the struct definition: > kvm_util_arch.h -> processor.h -> kvm_util.h -> kvm_util_arch.h > > No functional change intended. > > Suggested-by: Sean Christopherson > Signed-off-by: Yosry Ahmed > --- > .../selftests/kvm/include/x86/kvm_util_arch.h | 4 ++ > .../selftests/kvm/include/x86/processor.h | 8 ++- > .../testing/selftests/kvm/lib/x86/processor.c | 61 +++++++++++++------ > 3 files changed, 53 insertions(+), 20 deletions(-) > > diff --git a/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h b/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h > index 972bb1c4ab4c..d8808fa33faa 100644 > --- a/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h > +++ b/tools/testing/selftests/kvm/include/x86/kvm_util_arch.h > @@ -10,6 +10,8 @@ > > extern bool is_forced_emulation_enabled; > > +struct kvm_mmu; > + > struct kvm_vm_arch { > vm_vaddr_t gdt; > vm_vaddr_t tss; > @@ -19,6 +21,8 @@ struct kvm_vm_arch { > uint64_t s_bit; > int sev_fd; > bool is_pt_protected; > + > + struct kvm_mmu *mmu; No, put kvm_mmu in common code and create kvm_vm.mmu. This makes the "mmu" object a weird copy of state that's already in kvm_vm (pgd, pgd_created, and pgtable_levels), and more importantly makes it _way_ to easy to botch the x86 MMU code (speaking from first hand experience), e.g. due to grabbing vm->pgtable_levels instead of the mmu's version. I don't see an easy way to _completely_ guard against goofs like that, but it's easy-ish to audit code the code for instance of "vm->mmu.", and adding a common kvm_mmu avoids the weird duplicate code.