Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp4768016pxk; Wed, 30 Sep 2020 11:08:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2T8meeQeX2Iike0ET/j7EdqbAmWiMpNR+uwIB43h08bB4o3WIC5zkMD4x6qwqkqY3Mq6m X-Received: by 2002:a17:906:3791:: with SMTP id n17mr3999274ejc.216.1601489315452; Wed, 30 Sep 2020 11:08:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601489315; cv=none; d=google.com; s=arc-20160816; b=qNNDv9qJ6m8uSAy6NQpEKBfpF2DRr3C5H25LIQ54F/v4Yp15BNudAgRRKAko0K8C7w p04eECRlzmSiRrNPTxfhTb2GHib3vKDmFJq/IGQOzOFrMVTETDx9qTnu0r3Q/IgxJfXm k3dI7KUz3Pr2Lp5+U8X7gOdfAOi+uryf6Y+IuyGiqoTRRJVi3M0kUBW/d/3fp35FitNW AnCAFIeq2hXsGmZqetPuQahqT7pz1UzwE6ch6w2bppRx59TEVO8Ym5Ch88cgHfHsZYuF 28G3avs8kKYLxDlrrY/ZPOHX4QzUO4WnOTL2SdnEOQHxWCC6SWPBNTNLSUKHRf0XcoD8 p+7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :ironport-sdr:ironport-sdr; bh=VlOwoj6F/H/fWILIvCqN83OdEPq2ogR26RRe24xui2w=; b=X4EVYVvDmIsCLR+ZS+nd6o3+NZSQq9Gw4vMBWB0X68n2qhhrYDisKN4hoQg0hc7nRM GtBfjzxu1MG6eGOQzV153JU80v0jKUMBGtUsFGCMqO1JGbyuuCLdJkLqvbZqQV7yo0tq HcIwJduVHD3yOriJyKiyoInDdc1PcU+UjLZTknTs+fNe8koL/bENxvD9p/WsbeqLgGW9 vJ2F1uapyEOZGbaqQnIQDy5xbYh8el//tNjIAwMjQlEzLp9NWybVBtz/KqHmpd61X3Mi u8zd5DVO0tsFCIu3CjYAfUwlnN/hi1haRJFAbCpWAh5F56hCDfcoX7ZHvpH3T7Vpjpzp wegA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f5si1649009edt.158.2020.09.30.11.08.13; Wed, 30 Sep 2020 11:08:35 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728031AbgI3SEp (ORCPT + 99 others); Wed, 30 Sep 2020 14:04:45 -0400 Received: from mga05.intel.com ([192.55.52.43]:11258 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725355AbgI3SEp (ORCPT ); Wed, 30 Sep 2020 14:04:45 -0400 IronPort-SDR: bkp9gNTt+wCfmFHRdw/POWgftgyu0PUFu2eKLWMo1f5KuypR3/e3wOoGct/FnhCmX5x/pTdLYr H48ft4pt4Ozw== X-IronPort-AV: E=McAfee;i="6000,8403,9760"; a="247234912" X-IronPort-AV: E=Sophos;i="5.77,322,1596524400"; d="scan'208";a="247234912" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2020 11:04:43 -0700 IronPort-SDR: I0dCNHipiIbsdyw6DNVgyHoR71iGEmLRSr5q0cMX2x9jIN2GPBWG79zFGjRpciJ5vB4HfYOTz0 1hWtL3rbKFNQ== X-IronPort-AV: E=Sophos;i="5.77,322,1596524400"; d="scan'208";a="341276374" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.160]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2020 11:04:43 -0700 Date: Wed, 30 Sep 2020 11:04:42 -0700 From: Sean Christopherson To: Ben Gardon Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Cannon Matthews , Paolo Bonzini , Peter Xu , Peter Shier , Peter Feiner , Junaid Shahid , Jim Mattson , Yulei Zhang , Wanpeng Li , Vitaly Kuznetsov , Xiao Guangrong Subject: Re: [PATCH 17/22] kvm: mmu: Support dirty logging for the TDP MMU Message-ID: <20200930180438.GH32672@linux.intel.com> References: <20200925212302.3979661-1-bgardon@google.com> <20200925212302.3979661-18-bgardon@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200925212302.3979661-18-bgardon@google.com> User-Agent: Mutt/1.5.24 (2015-08-30) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 25, 2020 at 02:22:57PM -0700, Ben Gardon wrote: > +/* > + * Remove write access from all the SPTEs mapping GFNs in the memslot. If > + * skip_4k is set, SPTEs that map 4k pages, will not be write-protected. > + * Returns true if an SPTE has been changed and the TLBs need to be flushed. > + */ > +bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm, struct kvm_memory_slot *slot, > + bool skip_4k) > +{ > + struct kvm_mmu_page *root; > + int root_as_id; > + bool spte_set = false; > + > + for_each_tdp_mmu_root(kvm, root) { > + root_as_id = kvm_mmu_page_as_id(root); > + if (root_as_id != slot->as_id) > + continue; This pattern pops up quite a few times, probably worth adding #define for_each_tdp_mmu_root_using_memslot(...) \ for_each_tdp_mmu_root(...) \ if (kvm_mmu_page_as_id(root) != slot->as_id) { } else > + > + /* > + * Take a reference on the root so that it cannot be freed if > + * this thread releases the MMU lock and yields in this loop. > + */ > + get_tdp_mmu_root(kvm, root); > + > + spte_set = wrprot_gfn_range(kvm, root, slot->base_gfn, > + slot->base_gfn + slot->npages, skip_4k) || > + spte_set; > + > + put_tdp_mmu_root(kvm, root); > + } > + > + return spte_set; > +} > + > +/* > + * Clear the dirty status of all the SPTEs mapping GFNs in the memslot. If > + * AD bits are enabled, this will involve clearing the dirty bit on each SPTE. > + * If AD bits are not enabled, this will require clearing the writable bit on > + * each SPTE. Returns true if an SPTE has been changed and the TLBs need to > + * be flushed. > + */ > +static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root, > + gfn_t start, gfn_t end) > +{ > + struct tdp_iter iter; > + u64 new_spte; > + bool spte_set = false; > + int as_id = kvm_mmu_page_as_id(root); > + > + for_each_tdp_pte_root(iter, root, start, end) { > + if (!is_shadow_present_pte(iter.old_spte) || > + !is_last_spte(iter.old_spte, iter.level)) > + continue; Same thing here, extra wrappers would probably be helpful. At least add one for the present case, e.g. #define for_each_present_tdp_pte_using_root() and maybe even #define for_each_leaf_tdp_pte_using_root() since the "!present || !last" pops up 4 or 5 times. > + > + if (spte_ad_need_write_protect(iter.old_spte)) { > + if (is_writable_pte(iter.old_spte)) > + new_spte = iter.old_spte & ~PT_WRITABLE_MASK; > + else > + continue;