From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB4BDC433FE for ; Fri, 4 Mar 2022 16:11:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240541AbiCDQL7 (ORCPT ); Fri, 4 Mar 2022 11:11:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230244AbiCDQL4 (ORCPT ); Fri, 4 Mar 2022 11:11:56 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 218151C887E for ; Fri, 4 Mar 2022 08:11:09 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id z12-20020a17090ad78c00b001bf022b69d6so7236559pju.2 for ; Fri, 04 Mar 2022 08:11:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=DIyLQIOrxe8b/7rD2cOd+7Pkicu9JYWiuSVB/t6mS0M=; b=dDP+V5lR4moJsIzApLZrQyQafzXYPyN5cFQk1PR0KXVqOyl6GkoTkx9Tu267sjPLtY SoQtcHLqo43NwjxPKpt4kXrPFVzu4M7Hml7fm023r5NE0PaS6CjUnvLjhSGx69fiegpX nR3NEdbrIkkvDuJLKB1HOelnskUrjqSGg4R5Zk/32PI1JwQFvJYghYw7AXpznxawTnhb vRSrMdMud/K11sxaWuzemLThw8BUyyvz2Hvd1jdFAfw3LprM+pYjTDAc/FYX0EJTVijH uMwSg6dpDmGI2C7BEUipYpyTXEjy0QbMfrxbLhzwgSKsTFA5/KW8mvGm3wV4mkOy7WSE TcQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=DIyLQIOrxe8b/7rD2cOd+7Pkicu9JYWiuSVB/t6mS0M=; b=L2TMUUDh75/KcvHlhg0G6oyDLi1tOvg6ZOWcsNxZZ0fz8+BBI/2Fmxfxdbu5nJUyJ4 Z8duz/+bj1EpxLXDE5r5bT0k8XN46d1PEbUNs78dJIUywnMocnuHAY49MJoNw4ASZgrS rfMUP2x1MqMHfKG9biytW52KTHO6YL0JterTYsHL6XtvI8VuQ6cDynls2S7nf5pAj1nO LiuEResN8Fw8jdTG/HDjcjgYaVBdRz99N5wpKvL2z4m/xswRYw7xzBaON4eVFaoBhUjp /f1la1c9qlHJj1/9Uts8lrqyi42CAIF71TKpbatnOfLvDMNh+/RJnKna1h2vhPgbwVyK ruLg== X-Gm-Message-State: AOAM530AJUvtob0OcdSFeYeiniTP+UE+6PqXfnkcqlSGTS3UjmISppJ3 wHEM6L1myU4gP35GR92teWUV3g== X-Google-Smtp-Source: ABdhPJwEwj6BCMzRENnDa2/rQYeINmokB/hH/kKvN307KtluG9RtP7D7bw0BbBJs2sgqDakanier9A== X-Received: by 2002:a17:902:cf0d:b0:14f:e424:3579 with SMTP id i13-20020a170902cf0d00b0014fe4243579mr41950027plg.74.1646410268274; Fri, 04 Mar 2022 08:11:08 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id h13-20020a056a00230d00b004f427ffd485sm6726838pfh.143.2022.03.04.08.11.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 08:11:07 -0800 (PST) Date: Fri, 4 Mar 2022 16:11:04 +0000 From: Sean Christopherson To: Mingwei Zhang Cc: Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , David Hildenbrand , David Matlack , Ben Gardon Subject: Re: [PATCH v4 18/30] KVM: x86/mmu: Zap only TDP MMU leafs in kvm_zap_gfn_range() Message-ID: References: <20220303193842.370645-1-pbonzini@redhat.com> <20220303193842.370645-19-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 04, 2022, Mingwei Zhang wrote: > On Thu, Mar 03, 2022, Paolo Bonzini wrote: > > diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c > > index f3939ce4a115..c71debdbc732 100644 > > --- a/arch/x86/kvm/mmu/tdp_mmu.c > > +++ b/arch/x86/kvm/mmu/tdp_mmu.c > > @@ -834,10 +834,8 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp) > > } > > > > /* > > - * Tears down the mappings for the range of gfns, [start, end), and frees the > > - * non-root pages mapping GFNs strictly within that range. Returns true if > > - * SPTEs have been cleared and a TLB flush is needed before releasing the > > - * MMU lock. > > + * Zap leafs SPTEs for the range of gfns, [start, end). Returns true if SPTEs > > + * have been cleared and a TLB flush is needed before releasing the MMU lock. > > I think the original code does not _over_ zapping. But the new version > does. No, the new version doesn't overzap. > Will that have some side effects? In particular, if the range is > within a huge page (or HugeTLB page of various sizes), then we choose to > zap it even if it is more than the range. The old version did that too. KVM _must_ zap a hugepage that overlaps the range, otherwise the guest would be able to access memory that has been freed/moved. If the operation has unmapped a subset of a hugepage, KVM needs to zap and rebuild the portions that are still valid using smaller pages. > Regardless of side effect, I think we probably should mention that in > the comments? > > - /* > > - * If this is a non-last-level SPTE that covers a larger range > > - * than should be zapped, continue, and zap the mappings at a > > - * lower level, except when zapping all SPTEs. > > - */ > > - if (!zap_all && > > - (iter.gfn < start || > > - iter.gfn + KVM_PAGES_PER_HPAGE(iter.level) > end) && > > + if (!is_shadow_present_pte(iter.old_spte) || > > !is_last_spte(iter.old_spte, iter.level)) It's hard to see in the diff, but the key is the "!is_last_spte()" check. The check before was skipping non-leaf, a.k.a. shadow pages, if they weren't in the range. The new version _always_ skips shadow pages. Hugepages will always return true for is_last_spte() and will never be skipped.