From nobody Thu Sep 19 00:18:42 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 AF5C315697A for ; Fri, 26 Jul 2024 23:52:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722037972; cv=none; b=H4ldcffTgNELWqWdCPDULNY9Nhd+ughNTw31f82kDvDewPqrKqiHWLNab1GxlH802O0dm4JENK1z7nWNwgb9tXFfC24By0mapRfSaKkBVfyD9jpIGfQEvDDF8I4qHwEBNIGklfCPItkdsniHYxvNFH2lYvVzBJkjkit4KpgWR6Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722037972; c=relaxed/simple; bh=W8/UyfsPYwl/cL0QQCHocRUppr1lNXJ7XLufW7qP3ig=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gCTAyQ+tfRbEJap94ZkNzesFa3875T4g74JQa2jjYqcAZbC5xVwBZQY0kwOtQrvZjUCRkeL3AQ+V7rEnhH5nxO+e3wEHJjG4e+sD/XiubkCckrIEZZZQ4jSG2plby2XxALYfYle3g9tXDXyttdYvvP+okd8dqEwnIO48xa94URA= 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=sImOLOZ+; arc=none smtp.client-ip=209.85.128.202 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="sImOLOZ+" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-650b621f4cdso5677747b3.1 for ; Fri, 26 Jul 2024 16:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037970; x=1722642770; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=VnEAHRC1zG591I39OowzIjQNXTqQsRDXyh/rUn6sicQ=; b=sImOLOZ+olHeeZqm/vAoWA9dUWSXr5MivwgNPZbhD2jvQLSaHvC4v3kFop25k0C3Do zbpjg7TRqPh/YWv/Q0Go8/X/54/KxDxJsRuGEXibYasNWaaIJX/fgAToM27lqVw1y7e2 63BJDSkH3s3BQ+vKOnEqHxOYmqJ+m3e9V8ikZTZvD42QXfSCwJuXNoUlBDxjTPPXeB4q +sWEoCgQQjleOM4SxthrOIRcz93P+AxvlGvapM4t8H77VHScb5+FY3gag2DcAER/DfBm wy83/sFtYgSLZln1W1ibrAcWZEtAHeLSGChDEy8Dez+zRng4pmqALN2KiRTkqWxGYfLL 8GJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037970; x=1722642770; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VnEAHRC1zG591I39OowzIjQNXTqQsRDXyh/rUn6sicQ=; b=ZLdGkHXsTwUQIRp7s9PdbIx87l7hAP/bgAqr+45/kZNNTCess6RnH71qqCjotW+t+a MptpRh/kvebkYXAp43ocUCvzGqIldGryomc+BH+IiYPt5fwJIQMDuglpf8hnQ14Okb63 bvBxZZU9JW63MxcGgbY3duTWRqGX20r4FeMQIcMsWSfVXguZrVdalzuCQKewWnQW5r8t hAfmEAosm8S+4AkO57zd+ykv5tdWLhmxMWFpf6HsPB1ksJLcCf6hAIgKAV+Q9sBW5AyL /8o2/CcMo29MNOQqHsvf982/JQP0AoHPMKFyTj8fKeDMnngn6bHbuWL1WCDsAR5bf+eI +XNQ== X-Forwarded-Encrypted: i=1; AJvYcCVf8f7SQZQ2el3EWThA5d+hIGudihZ14Hgqphhw0BnCBbFPZEhXyFqBAGApqLCHo+D3dutLutBYmTx+4mKoTrHSF2T6ifkEznMKewZC X-Gm-Message-State: AOJu0YyDV/jPMjUk+owj6IiYqZHCTDtiinfDS0cpATOHkI7wYU12tttj OmUqB70jt/m57WIhhpRSk5qvBRG4/bKUTTPTApvdTIhXluRyboE6IBBIn3Knp5eOFd1X89CcykK 6zg== X-Google-Smtp-Source: AGHT+IFPOoUYsYyJmRjA25F0UYC9yeYKs61mDA/QqS/vgTcAgpqjjzQfUfGqPl0MU2c7vZElDBuLLOtKUts= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:fc9:b0:648:afcb:a7ce with SMTP id 00721157ae682-67a05a9d90bmr269367b3.3.1722037969718; Fri, 26 Jul 2024 16:52:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 26 Jul 2024 16:51:15 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-7-seanjc@google.com> Subject: [PATCH v12 06/84] KVM: x86/mmu: Skip the "try unsync" path iff the old SPTE was a leaf SPTE From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Apply make_spte()'s optimization to skip trying to unsync shadow pages if and only if the old SPTE was a leaf SPTE, as non-leaf SPTEs in direct MMUs are always writable, i.e. could trigger a false positive and incorrectly lead to KVM creating a SPTE without write-protecting or marking shadow pages unsync. This bug only affects the TDP MMU, as the shadow MMU only overwrites a shadow-present SPTE when synchronizing SPTEs (and only 4KiB SPTEs can be unsync). Specifically, mmu_set_spte() drops any non-leaf SPTEs *before* calling make_spte(), whereas the TDP MMU can do a direct replacement of a page table with the leaf SPTE. Opportunistically update the comment to explain why skipping the unsync stuff is safe, as opposed to simply saying "it's someone else's problem". Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/spte.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index d4527965e48c..a3baf0cadbee 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -226,12 +226,20 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_= page *sp, spte |=3D PT_WRITABLE_MASK | shadow_mmu_writable_mask; =20 /* - * Optimization: for pte sync, if spte was writable the hash - * lookup is unnecessary (and expensive). Write protection - * is responsibility of kvm_mmu_get_page / kvm_mmu_sync_roots. - * Same reasoning can be applied to dirty page accounting. + * When overwriting an existing leaf SPTE, and the old SPTE was + * writable, skip trying to unsync shadow pages as any relevant + * shadow pages must already be unsync, i.e. the hash lookup is + * unnecessary (and expensive). + * + * The same reasoning applies to dirty page/folio accounting; + * KVM will mark the folio dirty using the old SPTE, thus + * there's no need to immediately mark the new SPTE as dirty. + * + * Note, both cases rely on KVM not changing PFNs without first + * zapping the old SPTE, which is guaranteed by both the shadow + * MMU and the TDP MMU. */ - if (is_writable_pte(old_spte)) + if (is_last_spte(old_spte, level) && is_writable_pte(old_spte)) goto out; =20 /* --=20 2.46.0.rc1.232.g9752f9e123-goog