From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 CE0113148D2 for ; Fri, 23 Jan 2026 00:27:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769128072; cv=none; b=kztxcJs2u2B3ZPgUcJHP+jYBh9lcNK0pIbFzg/P4w5tT50OJIZmz6YIa3YoctHwnfvIBfb2bRmiJUfYJUoRaZpHhefNa/VGKThTKiTmnCFJTDm0aD+ULuKoH05R5z0ZIWENW2ykMT225wrjD40Mtv5QIPltZHkX+ToE7FofddXM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769128072; c=relaxed/simple; bh=CX/Mi64QO8J8pI3rVoDB4cSZ0vo5OgmB89Hew8LDtWc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Tuic+MbKiDmwiGZh+g9giWYNgeynKm05AJ2NAkLmjJ1yJ5tEXWkTsXaotQehRXQjFWz+Zk3TRcis6uVFRhdbzrWLD07i9+pVTYiJ0u3svd7UHBUhmVd1e7Hb894ovSmF5wyzuHjIUIvZU/+4uRo2WhJRPg2kPMJC1MaqLtdtRkU= 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=FT0gGuZ3; arc=none smtp.client-ip=209.85.216.73 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="FT0gGuZ3" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34ea5074935so1394413a91.0 for ; Thu, 22 Jan 2026 16:27:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769128064; x=1769732864; 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=OVtIIEz/2p/DaoNzout/fwLsF254wkNGmXV6Fgcz1ww=; b=FT0gGuZ3R1xfXG9o8Xv7Hcbc9pR91LVqy12TcUzs6EDJ5//gNSK4Xgcg0caYNMM80+ 5FXp4T6kQlVZTVqkXl2Zxf8+bWuIjcgqC4isTXh2vobFPjUerEdxGYj58LPnFWh2jT63 ORS6RlUWLfPUo0A5kVAiJsPU21BqAuhC5QSxgOWIs5LmTGNPVrlUCKGCZuLVzRG4HCMG AwyhATGCV6LRsMIUqYuUIK21bWC4xGOELEfa74BKpjsegJuZU4Hq/dLrrLoq5lVej2ta z4r+j4ViL6/yHFgpDk5pmnm3J4CBtnnsJ7LmM1Vtf4BCJ6poVfSfCRN/LpTyNdzxImp0 wYLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769128064; x=1769732864; 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=OVtIIEz/2p/DaoNzout/fwLsF254wkNGmXV6Fgcz1ww=; b=h2iHB8lxClYAHqQLZSBl6DoMp2cKGFip6ekTIKPW6h6E1HLsCKGNIytlaaHe/7/zRB V7CexID90Lam8hhuXjdUf9Li/k8uLG597czL5ekuwLEu15g5K6U03pexhPNU4K4I8QMc 4A1QwXxpicqgbtjQDjGdQxYkApVIYqCpYf3wk9qUS//uxu7C/fYn6D3xF8vceL0nIQUM mUnELt0WLIIHSA92hoWyLHgYxVpMsRK1F3UrPJheIMNk9aJqtyfbd4Wci2fcNM6PrEeQ SNWyA7zeYaxwXu9I8ialsswy0L+JlcfpZ7FlXPapX7e3pu5LFFOAl9qIARtMFHDxUJNI GXJQ== X-Forwarded-Encrypted: i=1; AJvYcCV6EeAY/ajA/BCMVcNhcRa/err/i8tMZ6AwjaTaIT1z8kXMGDP5ASBsMFLSOcGaxgpBA2E3klqxGkxwpNE=@vger.kernel.org X-Gm-Message-State: AOJu0YwxFt2OHr30GKdKgrMtpErUz8PIAprskHO3C0uEBlLpcU/IqO09 2ydmiNc0r1gIsyiZJHctsPoGknDDWfkfgntrY5SJBNI+K+fD7pE+tx29YNwPCUQN7kknmWWhLrI UgMVZ/g== X-Received: from pjbss8.prod.google.com ([2002:a17:90b:2ec8:b0:34c:2ca6:ff3e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:1602:b0:38b:e430:156f with SMTP id adf61e73a8af0-38e6f714396mr1115096637.20.1769128064100; Thu, 22 Jan 2026 16:27:44 -0800 (PST) Date: Thu, 22 Jan 2026 16:27:42 -0800 In-Reply-To: <20260120234115.546590-1-someguy@effective-light.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260120234115.546590-1-someguy@effective-light.com> Message-ID: Subject: Re: [PATCH] KVM: x86/mmu: move reused pages to the top of active_mmu_pages From: Sean Christopherson To: Hamza Mahfooz Cc: kvm@vger.kernel.org, Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" On Tue, Jan 20, 2026, Hamza Mahfooz wrote: > Move reused shadow pages to the head of active_mmu_pages in > __kvm_mmu_get_shadow_page(). This will allow us to move towards more of > a LRU approximation eviction strategy instead of just straight FIFO. Does this actually have a (positive) impact on real-world workloads? It seems like an obvious improvment, but there's enough subtlely around active_mmu_pages that I don't want to make any changes without a strong benefit. Specifically, kvm_zap_obsolete_pages() has a hard dependency on the list being FIFO. We _might_ be ok if we make sure to filter out obsolete pages, but only because of KVM's behavior of (a) only allowing two memslot generations at any given time and (b) zapping all shadow pages from the old/obsolete generation prior to kvm_zap_obsolete_pages() exiting. But it most definitely makes me nervous. diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 3911ac9bddfd..929085d46dd7 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2327,6 +2327,16 @@ static struct kvm_mmu_page *kvm_mmu_find_shadow_page(struct kvm *kvm, if (collisions > kvm->stat.max_mmu_page_hash_collisions) kvm->stat.max_mmu_page_hash_collisions = collisions; + + /* + * If a shadow page was found, move it to the head of the active pages + * as a rudimentary form of LRU-reclaim (KVM reclaims shadow pages from + * tail=>head if the VM hits the limit on the number of MMU pages). + * */ + if (sp && !WARN_ON_ONCE(is_obsolete_sp(kvm, sp)) && + !list_is_head(&sp->link, &kvm->arch.active_mmu_pages)) + list_move(&sp->link, &kvm->arch.active_mmu_pages); + return sp; } > Signed-off-by: Hamza Mahfooz > --- > arch/x86/kvm/mmu/mmu.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 02c450686b4a..2fe04e01863d 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -2395,7 +2395,8 @@ static struct kvm_mmu_page *__kvm_mmu_get_shadow_page(struct kvm *kvm, > if (!sp) { > created = true; > sp = kvm_mmu_alloc_shadow_page(kvm, caches, gfn, sp_list, role); > - } > + } else if (!list_is_head(&sp->link, &kvm->arch.active_mmu_pages)) > + list_move(&sp->link, &kvm->arch.active_mmu_pages); As alluded to above, I think I'd prefer to put this in kvm_mmu_find_shadow_page()? Largely a moot point, but it seems like we'd want to move a page to the head of the list if we look it up for any reason.