From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 60C5180067 for ; Thu, 26 Nov 2026 14:31:50 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AQEUbFb019642; Thu, 26 Nov 2026 17:30:37 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AQEUbFb019642 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="d/ZUk14/" Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AQEUQrq019590 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Thu, 26 Nov 2026 17:30:27 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AQEUQrq019590 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-c50c9af44d6so681582866b.1 for ; Thu, 26 Nov 2026 06:30:26 -0800 (PST) X-Gm-Message-State: APuU4Z/y2XJYx8Na5t1HGAGgn+Rof+FEGHfN6gtzAw/t2gQ8VEnbCiCn SwPGBfOnQOOJQyp1eCrlj0SUZKT6hxeP4nKeA1KCmPpG0dJZdjkwQWfYWsPQwcEA38oz9qYjUgT NJz/B+Z97a5M5ZQDnjl5DY7ixNPYxAbXMxKI+U+n1/srylA7Fw94YoA== X-Gm-Gg: AZFdTZev/Dj+rrTXo70m0DgcAtt5aEbyCw8tUTE31GTq+aX/iL7NK5igXIyk7iHC9Ry 8nVJP+KLzZ13LPCou5K9SstMDnGi8cri0Oq0EBI4d0KnyFlzeKHpQltzpd9fcWec9ckKbrxHrqC ZJwzeYB2xkE7IL9YpswYLr/aBTMGQ8xSw9xwULZYViRfQxyw7obqbK39KQR3DWZelRVdtmIY5Fp Y/A8qfVHrUNbouf9XEqb0LBhL6aSi3KxZLW3JmMAjlP8VGC/dSRJQLrMZxhy3N3xh19vseQmM1u +UEn/ht5iSLSHPucu7eBLLHEs0W7G8nwYu618IaoWyrHXvGsgvzfkH9JUswIU2wu0pW4RtEZXHS b1smULMDA0kQks6Eb X-Received: by 2002:a17:907:3e86:b0:c36:777f:824c with SMTP id a640c23a62f3a-c51315851d7mr1963994666b.4.1795703426644; Thu, 26 Nov 2026 06:30:26 -0800 (PST) X-Received: by 2002:a17:907:3e86:b0:c36:777f:824c with SMTP id a640c23a62f3a-c51315851d7mr1963991966b.4.1795703426104; Thu, 26 Nov 2026 06:30:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795703426; cv=none; d=google.com; s=arc-20260327; b=py5aNPxr4gfehTMSHhrpEm5yTnxDYp72z7X68h1S4bUeaUVDJ277aGuFcWC6UgUvbu Uib8GhCgskzczSDP9E3+kZ06AnZQlYBS+r5Pi8RgL/DeT8LN5kqFzB++Yx89pV3SZwlH EpERHCXD5uktbpfCpZ8iQQvM5b5L0B1QrFYAc4J4k+veOEzQ3xplBmUykw+63R1CV1KP GZZRJia2dpphbrQ0BA0zC2XeRAICsEZQK5+0QKZ9N/hFClvz+ccbrXRXl/FKwMpgEJNq QhDCi5W5+G2WT/kMCl4GCku2JP/IBwzWJe3jKBBmEMcChyncQXDpzLIXzg2JNTArZS7M SdKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:dkim-signature; bh=cfjqP3/Lrd6AbLawpSaOYlnDKg5MFL+BbDG//ygTJpQ=; fh=Qsi53PdKzYUvWZSiHi9J8uTgTupZPnYodD0rm/IZBSM=; b=j99UmaZlYhQowPIIcuGbe0zzNX0lgEHsn2GHw7SMeMja2Lxmmo0+efMYQepiCHIHie wi2sYGBul5d4PmV5hwCGbTd/3IrPjuQk9qz18kabRfER4tNhObDE6DkVNZhRmCleAFSD 5kZkNOCFAeH2pQeSvcPUp9+RI6VR8mDsePxgbO7iL2nPiv9N4TqX71KRU1AFw56DV1Bs 8CVXxyd08/9edveULpH3wJ/HhhvwaxLXV1PsH9Hqf3rH7VQg6j+/VxuXkFZ9fzuuvI5p BYhaqY+dK9oT6qK6ismGvaSRMz6ng2DBFN672SDLgJudgIa8H8nDZ+kQ5jxjnKFrcZHo UU3Q==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b="d/ZUk14/"; spf=pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=khorenko@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-6bb3375af2csi4551492a12.56.2026.11.26.06.30.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Nov 2026 06:30:26 -0800 (PST) Received-SPF: pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b="d/ZUk14/"; spf=pass (google.com: domain of khorenko@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=khorenko@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Message-Id:Content-Type:MIME-Version:Subject:Date :From; bh=cfjqP3/Lrd6AbLawpSaOYlnDKg5MFL+BbDG//ygTJpQ=; b=d/ZUk14/zhK10l29neS yp5T7QKGNULCWDBSrbtXo47AA4FhIWiNjZsuh4IEAqYiimTTUtRqQCGrY4Z0nZK52TcIvH5cmoaLJ YD7CqQJVgz8HWSd7lqAE0IW8KIG80P8zPswdtLNI5LExwY0pk6dMlP6/O+P1/qS2k9Xmvp1c3mrCZ mvWMCpNUpSzqrUcc8vLyEs9pR+1pMTbP1WKRggI9WJi3rrDjvXnz3mC+uLPUw3HRxQTbSsR70opCK LWcT4VnOIPKaC7GclEolmvHvrM+8zZ3tx6eH5YPs1c+MSH7037OC/H2FVWiUViDxrntDIizhb0ybO Pl0ijenN6XqDkdQ==; Received: from ch-demo-asa.virtuozzo.com ([130.117.225.8] helo=f0.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1xWaUG-00GkQd-2a; Thu, 26 Nov 2026 15:30:23 +0100 From: Konstantin Khorenko Date: Thu, 26 Nov 2026 15:30:17 +0100 MIME-Version: 1.0 Message-Id: <20261126-khorenko-vstor-157133-klp-and-kvm-fixes-v1-4-0ec8eaadf1bc@virtuozzo.com> References: <20261126-khorenko-vstor-157133-klp-and-kvm-fixes-v1-0-0ec8eaadf1bc@virtuozzo.com> In-Reply-To: <20261126-khorenko-vstor-157133-klp-and-kvm-fixes-v1-0-0ec8eaadf1bc@virtuozzo.com> To: Pavel Tikhomirov X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3892; i=khorenko@virtuozzo.com; h=from:subject:message-id; bh=bwcKnYaQ41cnrHEG9UhLspxKnz0JVC3MlarmHJc91/w=; b=owEBbQKS/ZANAwAKAVGWCkf5YVwqAcsmYgBrCEJ8FcvrpWb9fou54jOVJ84QYYWhSxj00tK7r 7ENa/THj6KJAjMEAAEKAB0WIQRWD23IPcXT0GO9CCxRlgpH+WFcKgUCawhCfAAKCRBRlgpH+WFc Kg1/D/4uxp64Vdr84U74I+YoR53JOyMoJO/JXtK1H1CqjTLco+qHIXWDlgdWpwMjHqHW7W8OpjZ ZKOS4VnrpvSJ5S3JvQXLE2x1GlblFsF5YY7g4bhTLf8ZaLTdaogzTPlae2uVcfjkgle77+Z/cyT Y2NCbOq8RAM14+RgEFUGRFHLnFWdgewEb9DjKyxHWUUNJNyK0vgsU+gxX1xuXEepNk77DJoqKYg GjySjGcsjtwUJGATXE4aoQucW2XWJtfU/YttU1DBdpEtDlBQSbd0vWER6S090w5uWv0KbZsPN7y hm3Ud1hprdPahpvB/w3cfRnSVW2MRQcC0sxreGO9fHf6UM7DjgsPMiHNROGrgQUo4DG5+4KxpQS 08/5X4hpPCvucrF9k1XiTj/niwBOkZHgM+mJysiaUqpj015wEV/eompZZJi22P0+78kjm1CqmO4 /UEpYCyU3S5DwnDm6UPJvXsI23P6U2e7IqOhq7DnuPpjwrwMHrN8tGAF8ntq4korNZ2QfZh4YzW xYPxW8qy3i4L4zrNMZ/a7TJ7qafDSEHwxBAtvHc5u/fA4LxX10DxpIgwToYQzY/qsWEmCveO0bT mEVp7SIM5puqkq4ewR7VGWzhaDxGwxwTxjITaTwCDlov7+qucjh3+EsRjv0hBYgdqApnUcMXnZ8 uVyujnFvGS4lpAg== X-Developer-Key: i=khorenko@virtuozzo.com; a=openpgp; fpr=560F6DC83DC5D3D063BD082C51960A47F9615C2A X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.1 04/14] ms/KVM: x86/mmu: Cap number of parent PTEs per SP at 1000 (the arbitrary rmap limit) X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org From: Sean Christopherson [ Upstream commit e4558c3d7198ed9a0adc3304681f561f4e4d80e1 ] When potentially reusing a shadow page, zap the shadow page if its number of parent PTEs exceeds KVM's (mostly) arbitrary rmap recycling threshold. Letting the guest create a (mostly) unbounded number of parent PTEs allows the guest to coerce KVM into creating lists containing millions of PTEs, effectively bounded only by the maximum number of shadow pages. Because PTE lists are simple singly linked lists, removal is O(n). Even worse, if a list is constructed just so, teardown can be O(n^2), e.g. if SPTEs are built such that zapping child SPTEs always zaps the last entry in the list. To cap things off, all paths that walk PTE lists do so while holding mmu_lock, without conditionally rescheduling. Using a doubly linked list would not address the issue, as removal would remain O(n) since KVM still wouldn't know where in the list to remove from. Removal could be made O(1) by storing a per-SPTE pointer, but that would largely defeat the purpose of reusing SPs, and would penalize well-behaved guests, especially for nested TDP where aliasing page tables is uncommon. So, cap the maximum number of parent PTEs at the rmap limit. In practice, well-behaved guests will likely never run afoul of the limit. E.g. when running Linux as L1 with TDP, the number of parent PTEs for a single SP tops out at under 200 (though undoubtedly obscure workloads can push it higher). Deliberately use a "greater than or equal" as opposed to rmap's strictly "greater than", as the rmap code checks the threshold immediately *after* inserting into the list, i.e. the limit is 1000 for rmaps, KVM just very briefly blows past it. Apply the check to both indirect and direct SPs, because while a fully direct MMU naturally limits the number of parent PTEs to '1', direct SPs in indirect MMUs have no such constraint. E.g. if the guest maps a huge PTE into many locations in its page tables, and KVM is backing the hugepage with a direct SP, i.e. with a series of smaller pages, then KVM could end up with a direct SP with many parents. Note, shadow_mmu_try_split_huge_pages() always restarts the rmap walk or bails entirely if it gets to the point where it tries to grab a SP, i.e. there is *no* pre-existing bug that would be tripped by zapping a SP and thus rmaps when getting a SP. Cc: Jim Mattson Cc: James Houghton Cc: Yosry Ahmed Cc: David Woodhouse Cc: stable@vger.kernel.org Reported-by: Stefan Teodorescu Signed-off-by: Sean Christopherson Message-ID: <20261027171518.1432557-1-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin (cherry picked from commit edf87e07376054eedcd5306e0794a26e3004d97b) https://virtuozzo.atlassian.net/browse/VSTOR-157287 Feature: fix ms/kvm Signed-off-by: Konstantin Khorenko --- arch/x86/kvm/mmu/mmu.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 66bda34aeb7f6..d7527f2180e7a 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2270,6 +2270,16 @@ static struct kvm_mmu_page *kvm_mmu_find_shadow_page(struct kvm *kvm, continue; } + /* + * Note! Unlike fully direct MMUs, direct SPs in indirect MMUs + * can have an arbitrary number of parents, e.g if the guest is + * mapping a huge PTE at many points in its page tables. + */ + if (pte_list_count(&sp->parent_ptes) >= RMAP_RECYCLE_THRESHOLD) { + kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); + break; + } + /* unsync and write-flooding only apply to indirect SPs. */ if (sp->role.direct) goto out; -- 2.43.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel