Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3654921pxb; Mon, 24 Jan 2022 14:33:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJzdR7npWPFGT2J9OiSHS21CPCFX3/ToNw/Wv0GGpTK930zvspBlOspqLXWdewkPIG+QMlWu X-Received: by 2002:a05:6a00:174f:b0:4c2:3cc8:d7c2 with SMTP id j15-20020a056a00174f00b004c23cc8d7c2mr15507900pfc.81.1643063472098; Mon, 24 Jan 2022 14:31:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643063472; cv=none; d=google.com; s=arc-20160816; b=RSju+ERr6Pih1DN8pPbeuBXzgOjuyHGwVOaRvOqDdbquNvYJYFU5rXkPkfYN9yiNTy L5VD6KD7hTVwgxF53gy1gY4kcqudmEASem3HXCoSWe7aEieb3AM6iIVFjuZ1hZIIV0Z0 G6yXJKsjueCQWPk24auqJdcD0vcLjJO/k2cSirRheoFA81gsJMZWpzWB9sCAPV1g+5RI u9LBWd+q41EDqKRsmxUBs1NiNlqoJcxSc1bgtEbif8W/Vv/K5GG0PlMoiGmK1lHG8x8J kt+1IE9DYsiC4aSG7yVHy8NJD2Ox7rn8brVGzwmhbbbtGTpwVgmr1kDjkG4C8buSoIUm hP6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=59hZAReh9ZjOa9bXubYkpGzvjGctFmdmRm55wNlF8g4=; b=RPnNpINeBhI+au6D8EWkRS7BapbVP19A1FZ6ctBQc1UOUsFnB16usI6k51BMQUYm6g p7bKO6I67RtSmg6YwjO9kiOroiCIrIlKL6R5WuayJ6XYOcXUzGhhgRInsYC8Csg5/lI1 8IzlgAZyk/b9Is2hNhOUe6xUZcH4tLy1m9eqYm1IOdwARdOCPmKV9k9guLh3tSJePT4j lI/Lf/6DUMwfUynWnTQGf9tGZLJJzhN6Lv24U5Wc3kXaKW8y7FhsfSMOnJRC5c8gF+Ui CUWT5tQ8adHjOQlLaCHVYmKD1guSfuP7ZKxPvU0+S+LNhrkRMp8Mzn62O/b/fQgA6eyU Y3HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ucgCgEE+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i186si6691177pge.299.2022.01.24.14.30.59; Mon, 24 Jan 2022 14:31:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ucgCgEE+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1586597AbiAXW1A (ORCPT + 99 others); Mon, 24 Jan 2022 17:27:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1390792AbiAXVM6 (ORCPT ); Mon, 24 Jan 2022 16:12:58 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD58FC02980C; Mon, 24 Jan 2022 12:10:01 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 84A03B81218; Mon, 24 Jan 2022 20:10:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A742EC340E7; Mon, 24 Jan 2022 20:09:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643055000; bh=fdEnQR3ESgo3jqbn1qe9UEwBUMsHG31tWLdWi7m5dR8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ucgCgEE+JWo22aQAIGa6Jg1JZEFycUGVZq/2shhpyjFxHQSufX9tICoPMpcV22pOP 5hhR3iVpednjHABZ8qvcg5t6QZMXuKF2Rx9//z+BOPotVsoC0nMh/ZwpCzfo8sTbxp o9cmdpYf5Ur8BJvSCv44gnrIjIPVnaBVu0DdVTD8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Matlack , Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.15 001/846] KVM: x86/mmu: Fix write-protection of PTs mapped by the TDP MMU Date: Mon, 24 Jan 2022 19:31:58 +0100 Message-Id: <20220124184100.925469129@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@linuxfoundation.org> User-Agent: quilt/0.66 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Matlack commit 7c8a4742c4abe205ec9daf416c9d42fd6b406e8e upstream. When the TDP MMU is write-protection GFNs for page table protection (as opposed to for dirty logging, or due to the HVA not being writable), it checks if the SPTE is already write-protected and if so skips modifying the SPTE and the TLB flush. This behavior is incorrect because it fails to check if the SPTE is write-protected for page table protection, i.e. fails to check that MMU-writable is '0'. If the SPTE was write-protected for dirty logging but not page table protection, the SPTE could locklessly be made writable, and vCPUs could still be running with writable mappings cached in their TLB. Fix this by only skipping setting the SPTE if the SPTE is already write-protected *and* MMU-writable is already clear. Technically, checking only MMU-writable would suffice; a SPTE cannot be writable without MMU-writable being set. But check both to be paranoid and because it arguably yields more readable code. Fixes: 46044f72c382 ("kvm: x86/mmu: Support write protection for nesting in tdp MMU") Cc: stable@vger.kernel.org Signed-off-by: David Matlack Message-Id: <20220113233020.3986005-2-dmatlack@google.com> Reviewed-by: Sean Christopherson Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1493,12 +1493,12 @@ static bool write_protect_gfn(struct kvm !is_last_spte(iter.old_spte, iter.level)) continue; - if (!is_writable_pte(iter.old_spte)) - break; - new_spte = iter.old_spte & ~(PT_WRITABLE_MASK | shadow_mmu_writable_mask); + if (new_spte == iter.old_spte) + break; + tdp_mmu_set_spte(kvm, &iter, new_spte); spte_set = true; }