From patchwork Thu Mar 11 23:16:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Gardon X-Patchwork-Id: 12133237 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44E02C4332D for ; Thu, 11 Mar 2021 23:18:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F24F64F9A for ; Thu, 11 Mar 2021 23:18:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231395AbhCKXRo (ORCPT ); Thu, 11 Mar 2021 18:17:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230406AbhCKXR0 (ORCPT ); Thu, 11 Mar 2021 18:17:26 -0500 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07351C061574 for ; Thu, 11 Mar 2021 15:17:26 -0800 (PST) Received: by mail-qv1-xf4a.google.com with SMTP id u17so16348051qvq.23 for ; Thu, 11 Mar 2021 15:17:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=nVhAIt5VREXOLgbcbWfBMhCi9SRRPCjtnqZMSRmixNI=; b=qaP2XqovDlAUs0DzgGGp+lLIkzUk6IF44/j1LRwLkgKp8GwCLjAEdyBwoB1a7rHiVk V13xfo+YEeBoRTlpuxTGpryM4Cno45siFKYVCjytxck5FD+TV8sMfryv+0DM4fhB+0G/ vDtmCATkTSlit0JfLKCruP7W+D8Ce2t71vDcY60Lyc5AWNzMuQbxoKVP+SXxVPbOoY6X 9ZGcMY2uQmQOC0hO+S0Cx5U5VoNRguUMwHh5eE97otEo+D0D+JSpf+I/KNU7Ov4kqY88 THsDkutN4A72Y54hKXLVxLOYFlFbBJgTE9sMLZT/HpS6MOsH3wx8HbuaYdRvVMsFdD8Y UyPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=nVhAIt5VREXOLgbcbWfBMhCi9SRRPCjtnqZMSRmixNI=; b=Gqw7ebNb6pnppN44GJQ55wwgLdKRagTuEPQUAH62fg+cG1VmfnJK+CdN3mUX7rQI0/ K50OxeeoPRjKjf7MFD2+5QLuvu+eZJgRcML7L2dwwl4qt32P+HN1nWV2YIzgb5C8Jn6z zP5QTtSOOcjXOtyu4ulB7ZifrQ/HDgxkzBLRXQk5JFAfP/TXrycSz7mVCaYup912zWAw TgPIDVNkRPsYn3S7g2m9fPwUc5CesfR8IWujpShhyLKqzh4j+KCV5iF3B3hMY2wU+hhW LhvSzSMneMixZKOU3GbS87NFkcVP24g+dYGyW5jEDmz6elZPd1ejySl1nbvZvjw0l7mM 778Q== X-Gm-Message-State: AOAM533hwF+yrx+Zv/jI0xh+1Qhsx5LKpV6fbYz0Z6WJyoQlNY1ElDwX psO4MpkfWdV7BwK9PrRvlsYSjHgnja6L X-Google-Smtp-Source: ABdhPJyud+Anzi0dLD+vZhAklD+994rvg7BLyxQt7Qx2vCqzkZRj/TkyJHEIwzonvah7XILrc+z2D4y4UrxL X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:b4d4:7253:76fa:9c42]) (user=bgardon job=sendgmr) by 2002:a0c:b59f:: with SMTP id g31mr9990587qve.28.1615504645150; Thu, 11 Mar 2021 15:17:25 -0800 (PST) Date: Thu, 11 Mar 2021 15:16:55 -0800 In-Reply-To: <20210311231658.1243953-1-bgardon@google.com> Message-Id: <20210311231658.1243953-2-bgardon@google.com> Mime-Version: 1.0 References: <20210311231658.1243953-1-bgardon@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH 1/4] KVM: x86/mmu: Fix RCU usage in handle_removed_tdp_mmu_page From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Peter Shier , Jim Mattson , Ben Gardon Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The pt passed into handle_removed_tdp_mmu_page does not need RCU protection, as it is not at any risk of being freed by another thread at that point. However, the implicit cast from tdp_sptep_t to u64 * dropped the __rcu annotation without a proper rcu_derefrence. Fix this by passing the pt as a tdp_ptep_t and then rcu_dereferencing it in the function. Suggested-by: Sean Christopherson Reported-by: kernel test robot Signed-off-by: Ben Gardon Reviewed-by: Sean Christopherson --- arch/x86/kvm/mmu/tdp_mmu.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index c926c6b899a1..5387ac040f66 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -301,11 +301,16 @@ static void tdp_mmu_unlink_page(struct kvm *kvm, struct kvm_mmu_page *sp, * * Given a page table that has been removed from the TDP paging structure, * iterates through the page table to clear SPTEs and free child page tables. + * + * Note that pt is passed in as a tdp_ptep_t, but it does not need RCU + * protection. Since this thread removed it from the paging structure, + * this thread will be responsible for ensuring the page is freed. Hence the + * early rcu_dereferences in the function. */ -static void handle_removed_tdp_mmu_page(struct kvm *kvm, u64 *pt, +static void handle_removed_tdp_mmu_page(struct kvm *kvm, tdp_ptep_t pt, bool shared) { - struct kvm_mmu_page *sp = sptep_to_sp(pt); + struct kvm_mmu_page *sp = sptep_to_sp(rcu_dereference(pt)); int level = sp->role.level; gfn_t base_gfn = sp->gfn; u64 old_child_spte; @@ -318,7 +323,7 @@ static void handle_removed_tdp_mmu_page(struct kvm *kvm, u64 *pt, tdp_mmu_unlink_page(kvm, sp, shared); for (i = 0; i < PT64_ENT_PER_PAGE; i++) { - sptep = pt + i; + sptep = rcu_dereference(pt) + i; gfn = base_gfn + (i * KVM_PAGES_PER_HPAGE(level - 1)); if (shared) { From patchwork Thu Mar 11 23:16:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Gardon X-Patchwork-Id: 12133239 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6834DC4332B for ; Thu, 11 Mar 2021 23:18:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A7A464FB3 for ; Thu, 11 Mar 2021 23:18:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231433AbhCKXRq (ORCPT ); Thu, 11 Mar 2021 18:17:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229488AbhCKXRh (ORCPT ); Thu, 11 Mar 2021 18:17:37 -0500 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1546FC061574 for ; Thu, 11 Mar 2021 15:17:37 -0800 (PST) Received: by mail-qk1-x74a.google.com with SMTP id v136so16889601qkb.9 for ; Thu, 11 Mar 2021 15:17:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=WfbyYdb68xASx0rik3LLH3bJ+u86HlW/YnL+1UN7bRg=; b=UFnnkD1qKxAJQAGTGtaWpjjsSj9Gg+i2QLcuMFbcbeO7zkkEc+TsL42Ah7MvWH0ctv PFpLu/Nyywbasudcngum2Bk9NphF7J8H51jiABuNCyO9rn//W1sAXrXs0gyjiSwZPOMu GugG5vfrsxqaBW8K5coQDu1VytV20y/wMnShJS5mrIR8R5GUHQzPT6K5PBCBGPze5f3E /BLfCf48nUy5Y+I0vdOFRTkilK92ZpU9J97qDHxUAYugpe3UlNCiXPyB3AOt3b813mWL sKUJBaU+iJbljvOcQSyJKeYZycaYcGO0c8m4VyWtInrQ/CafLAEoLTAOTX1UYku2ravM NiFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=WfbyYdb68xASx0rik3LLH3bJ+u86HlW/YnL+1UN7bRg=; b=MxoSlradaGcJm5wwTqb1JdDSMIJuRwhttnudfwfRg6vg25KUZJXzTjGK1k1g3nsjyi kUHr3p2ycUouaQVCysP83/w2SQF+kHFRDgwF7vgAXemntcpXVAo3avWHqWhgmnk1VOPW YXAKjzYYliGJB56k5kvlnoj6thv5GD2L4a4/hj5CIcBduDm+qU4Jloem6GTEtaYAE0hd WA1hsN4kIork6heNYh9yx0eJ20vUbcyJyz4ssGOu1l5AKCu675jHMWEL+9wu3fBGQFR8 sei2SW4qAFv70R/lzWf/2p0ZbBKA2uax/ysIx4no/PcsruKyW1I0Rl4QcoRMX9XHC4gK BJ7A== X-Gm-Message-State: AOAM530ZrrWyIg9W0hEudSLidN+fOhaxy5k9QBAKW8iEp6dQouAsVNCy J9o5Vy0gV6JP7DcYdXVpQ4iKHSVCIb8K X-Google-Smtp-Source: ABdhPJxrh5lL/RyL27y+kf6kMDDXgW/8luozm42iisMnrZ9+99ldYCymOS7CF1DHuBrFCbDXG0qLHJdKMQTZ X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:b4d4:7253:76fa:9c42]) (user=bgardon job=sendgmr) by 2002:a0c:ed45:: with SMTP id v5mr9922106qvq.13.1615504656303; Thu, 11 Mar 2021 15:17:36 -0800 (PST) Date: Thu, 11 Mar 2021 15:16:56 -0800 In-Reply-To: <20210311231658.1243953-1-bgardon@google.com> Message-Id: <20210311231658.1243953-3-bgardon@google.com> Mime-Version: 1.0 References: <20210311231658.1243953-1-bgardon@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH 2/4] KVM: x86/mmu: Fix RCU usage for tdp_iter_root_pt From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Peter Shier , Jim Mattson , Ben Gardon Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The root page table in the TDP MMU paging structure is not protected with RCU, but rather by the root_count in the associated SP. As a result it is safe for tdp_iter_root_pt to simply return a u64 *. This sidesteps the complexities assoicated with propagating the __rcu annotation around. Reported-by: kernel test robot Signed-off-by: Ben Gardon Reported-by: kernel test robot Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/tdp_iter.c | 10 ++++++++-- arch/x86/kvm/mmu/tdp_iter.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c index e5f148106e20..8e2c053533b6 100644 --- a/arch/x86/kvm/mmu/tdp_iter.c +++ b/arch/x86/kvm/mmu/tdp_iter.c @@ -159,8 +159,14 @@ void tdp_iter_next(struct tdp_iter *iter) iter->valid = false; } -tdp_ptep_t tdp_iter_root_pt(struct tdp_iter *iter) +u64 *tdp_iter_root_pt(struct tdp_iter *iter) { - return iter->pt_path[iter->root_level - 1]; + /* + * Though it is stored in an array of tdp_ptep_t for convenience, + * the root PT is not actually protected by RCU, but by the root + * count on the associated struct kvm_mmu_page. As a result it's + * safe to rcu_dereference and return the value here. + */ + return rcu_dereference(iter->pt_path[iter->root_level - 1]); } diff --git a/arch/x86/kvm/mmu/tdp_iter.h b/arch/x86/kvm/mmu/tdp_iter.h index 4cc177d75c4a..5a47c57810ab 100644 --- a/arch/x86/kvm/mmu/tdp_iter.h +++ b/arch/x86/kvm/mmu/tdp_iter.h @@ -62,6 +62,6 @@ tdp_ptep_t spte_to_child_pt(u64 pte, int level); void tdp_iter_start(struct tdp_iter *iter, u64 *root_pt, int root_level, int min_level, gfn_t next_last_level_gfn); void tdp_iter_next(struct tdp_iter *iter); -tdp_ptep_t tdp_iter_root_pt(struct tdp_iter *iter); +u64 *tdp_iter_root_pt(struct tdp_iter *iter); #endif /* __KVM_X86_MMU_TDP_ITER_H */ diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 5387ac040f66..6c8824bcc2f2 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -558,7 +558,7 @@ static inline void __tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter, u64 new_spte, bool record_acc_track, bool record_dirty_log) { - tdp_ptep_t root_pt = tdp_iter_root_pt(iter); + u64 *root_pt = tdp_iter_root_pt(iter); struct kvm_mmu_page *root = sptep_to_sp(root_pt); int as_id = kvm_mmu_page_as_id(root); @@ -653,7 +653,7 @@ static inline bool tdp_mmu_iter_cond_resched(struct kvm *kvm, WARN_ON(iter->gfn > iter->next_last_level_gfn); - tdp_iter_start(iter, iter->pt_path[iter->root_level - 1], + tdp_iter_start(iter, tdp_iter_root_pt(iter), iter->root_level, iter->min_level, iter->next_last_level_gfn); From patchwork Thu Mar 11 23:16:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Gardon X-Patchwork-Id: 12133241 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8ABEEC433E0 for ; Thu, 11 Mar 2021 23:18:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5580C64F9A for ; Thu, 11 Mar 2021 23:18:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231354AbhCKXSS (ORCPT ); Thu, 11 Mar 2021 18:18:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231473AbhCKXRv (ORCPT ); Thu, 11 Mar 2021 18:17:51 -0500 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D31BC061574 for ; Thu, 11 Mar 2021 15:17:51 -0800 (PST) Received: by mail-qv1-xf4a.google.com with SMTP id da16so16334477qvb.2 for ; Thu, 11 Mar 2021 15:17:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=AxoBB0NhGnhe+D+L5sVMK+GdUudXG/INmmMF914oWWE=; b=tV0hnmCWulMFsIisLAiMtfKadjwWmccSr25Mj2NNio4ERe4PSU7RdV9axRNPcqXREO QzDbeeCe+XS9y5iQkXOb8Gp4u2dvVW8xryU2q5HRS/f9q4WqtoXX1pGPlcTZceHizTZA BB4PjRjUasTTCwpj1xSVPdNgufMeo1Jv7PJgbqwYsLeZw2g4mjRl7US3Ed/sV6cGyuxy WUHpPDYbwmlsO190YBidzsaiPhVptf91n+xklkvn7FUeQePqIvdzooRAU30rfMYpgFoV tko8SL2UPfXOSrW+hLZxzen1dT24Yg9dT+fbMZeSA2JyzrbjBG6wcGTbCMYZNacq+PdE Uj4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=AxoBB0NhGnhe+D+L5sVMK+GdUudXG/INmmMF914oWWE=; b=SgBVX9Fzkvhr+PMdXtrqwWnpceP8YaJJ1Fbk+4Yhe0I+dcflPjQlUYi6Ee6q7TvWnl FZ9ITZfTtvnYI+50Tb9pojhubUJOm/r4/aRjGsAnEsvlDKyh5h04yK0mjUxBn8BwMtOK PJ5RS4KJpT9rQ/SZ5pfFNMJ5E2xyDwcoh2mUKWOechnMbEbXtg1YmupQSQ9FOyTUPFCp js3egeuB+HxiyO8yoHnGdUkOewOPQ2XWWA/5Y0G/fJfDNbt26catb2Vc7GnKLu9d7f9e 60nvvjbaqEFoeld8tNKA4KWyrrbnSR7R6RV94YbYI7kJ/14oxcQzc/gqhvWwLHVjpYBJ PLLw== X-Gm-Message-State: AOAM533cv5jcBBRGkCitRyQurDDlaGZMBhnkmHpkM+kdzwJ2RF+69w+B FWG0F9mjaTicP1e4kZ8S5YbmMt4RkzRB X-Google-Smtp-Source: ABdhPJxKfRDBF4vpCW1d/v+gDvm9EvkTeBEqeDYcfg+fH48GtcYfPkfKEHXH5oumo6ocbbv0gsB5JiCzK02v X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:b4d4:7253:76fa:9c42]) (user=bgardon job=sendgmr) by 2002:a0c:e385:: with SMTP id a5mr9900907qvl.12.1615504670412; Thu, 11 Mar 2021 15:17:50 -0800 (PST) Date: Thu, 11 Mar 2021 15:16:57 -0800 In-Reply-To: <20210311231658.1243953-1-bgardon@google.com> Message-Id: <20210311231658.1243953-4-bgardon@google.com> Mime-Version: 1.0 References: <20210311231658.1243953-1-bgardon@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH 3/4] KVM: x86/mmu: Fix RCU usage when atomically zapping SPTEs From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Peter Shier , Jim Mattson , Ben Gardon Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Fix a missing rcu_dereference in tdp_mmu_zap_spte_atomic. Reported-by: kernel test robot Signed-off-by: Ben Gardon Reviewed-by: Sean Christopherson --- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 6c8824bcc2f2..a8fdccf4fd06 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -532,7 +532,7 @@ static inline bool tdp_mmu_zap_spte_atomic(struct kvm *kvm, * here since the SPTE is going from non-present * to non-present. */ - WRITE_ONCE(*iter->sptep, 0); + WRITE_ONCE(*rcu_dereference(iter->sptep), 0); return true; } From patchwork Thu Mar 11 23:16:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Gardon X-Patchwork-Id: 12133243 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D77D4C43381 for ; Thu, 11 Mar 2021 23:18:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9044064FA6 for ; Thu, 11 Mar 2021 23:18:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231402AbhCKXST (ORCPT ); Thu, 11 Mar 2021 18:18:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229574AbhCKXR6 (ORCPT ); Thu, 11 Mar 2021 18:17:58 -0500 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B3D7C061574 for ; Thu, 11 Mar 2021 15:17:58 -0800 (PST) Received: by mail-qk1-x74a.google.com with SMTP id k68so16872995qke.2 for ; Thu, 11 Mar 2021 15:17:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=X0XCjVCvhvbH3OrIFQ9hEGpj4aufRJMFYEYVKUCG4/0=; b=jaohzeDkuOwcE3EF4dC2nlQr6YB7AOxQAW6yv0QPXuwHIEKT31GPNUY5QzqbfzSZLU qsM0eGg6zfWB4hCf3pLWWiRql57bj9a5jh5/xPjgXvSukVffonIEw+/b2dBAEx4Vtl9+ IMY8J3d6Ae1HZEPe8KZ6g4vVZ/5JzA6chnBx3dbN73EofcqiR8Yiyam6ottkgPT6BzyJ 7UzDtuOP4doOoXaEZcmT+UQBtTq+CRsdokizdZ+BXELdpV9f9nBFr11CDEx0BYJUjVdu Nx7h1A0LM5+M6A+wifAIljzIYvfBQU0TFxhZg8eygX9TlHu6t9IeOMcHeqr/qoNq3UQa zeDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=X0XCjVCvhvbH3OrIFQ9hEGpj4aufRJMFYEYVKUCG4/0=; b=TTUhoynRDgAGowtZspXePkQANmitCyKul0FuxZBAU/tHf6rHszcem394xCEbyfc7vc eF591I37LyjxP1yYPJKoezMHHCv6cmMSo9znTwvAr3j9FJw2MwmuEqWe3ERcko5AHIXx YxKFuKTMpx57htsab9RAt4+igcjG3y8VlWndq2oN1u/b/UaQ1/Nj6LAgOwV1IBGjWCtB PVKyfk2S5Ot5qHpIZed1MxVpncy23sB5kkS1q/OLz5EYblXPQpgzkv/Oz276H6pxKLfc o74ur4rQ4e307Hkvk/IFzT0rfvyeOo+pkqJsNyYIX4/FQ5SulYB4YNvo7UAgKNRpNbU0 /KWA== X-Gm-Message-State: AOAM531OHSqPgr67aco2ePlUDdfpY4U6Z0pK9KaWBxievDkOpO7Y+nfF gwtvJMoGdZhmL9rKdxIzwoTFMpc2diWh X-Google-Smtp-Source: ABdhPJxVeaYlfCoROC02zZsOcBw7+wpEjU4aMS5KaUJBvId7WfS0464ppBBqk4LJBqITGOksKsxW/67djgGx X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:b4d4:7253:76fa:9c42]) (user=bgardon job=sendgmr) by 2002:ad4:5c87:: with SMTP id o7mr9772773qvh.31.1615504677316; Thu, 11 Mar 2021 15:17:57 -0800 (PST) Date: Thu, 11 Mar 2021 15:16:58 -0800 In-Reply-To: <20210311231658.1243953-1-bgardon@google.com> Message-Id: <20210311231658.1243953-5-bgardon@google.com> Mime-Version: 1.0 References: <20210311231658.1243953-1-bgardon@google.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH 4/4] KVM: x86/mmu: Factor out tdp_iter_return_to_root From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Peter Shier , Jim Mattson , Ben Gardon Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org In tdp_mmu_iter_cond_resched there is a call to tdp_iter_start which causes the iterator to continue its walk over the paging structure from the root. This is needed after a yield as paging structure could have been freed in the interim. The tdp_iter_start call is not very clear and something of a hack. It requires exposing tdp_iter fields not used elsewhere in tdp_mmu.c and the effect is not obvious from the function name. Factor a more aptly named function out of tdp_iter_start and call it from tdp_mmu_iter_cond_resched and tdp_iter_start. No functional change intended. Signed-off-by: Ben Gardon --- arch/x86/kvm/mmu/tdp_iter.c | 24 +++++++++++++++++------- arch/x86/kvm/mmu/tdp_iter.h | 1 + arch/x86/kvm/mmu/tdp_mmu.c | 4 +--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_iter.c b/arch/x86/kvm/mmu/tdp_iter.c index 8e2c053533b6..bbf53b98cc65 100644 --- a/arch/x86/kvm/mmu/tdp_iter.c +++ b/arch/x86/kvm/mmu/tdp_iter.c @@ -20,6 +20,21 @@ static gfn_t round_gfn_for_level(gfn_t gfn, int level) return gfn & -KVM_PAGES_PER_HPAGE(level); } +/* + * Return the TDP iterator to the root PT and allow it to continue its + * traversal over the paging structure from there. + */ +void tdp_iter_return_to_root(struct tdp_iter *iter) +{ + iter->yielded_gfn = iter->next_last_level_gfn; + iter->level = iter->root_level; + + iter->gfn = round_gfn_for_level(iter->next_last_level_gfn, iter->level); + tdp_iter_refresh_sptep(iter); + + iter->valid = true; +} + /* * Sets a TDP iterator to walk a pre-order traversal of the paging structure * rooted at root_pt, starting with the walk to translate next_last_level_gfn. @@ -31,16 +46,11 @@ void tdp_iter_start(struct tdp_iter *iter, u64 *root_pt, int root_level, WARN_ON(root_level > PT64_ROOT_MAX_LEVEL); iter->next_last_level_gfn = next_last_level_gfn; - iter->yielded_gfn = iter->next_last_level_gfn; iter->root_level = root_level; iter->min_level = min_level; - iter->level = root_level; - iter->pt_path[iter->level - 1] = (tdp_ptep_t)root_pt; + iter->pt_path[iter->root_level - 1] = (tdp_ptep_t)root_pt; - iter->gfn = round_gfn_for_level(iter->next_last_level_gfn, iter->level); - tdp_iter_refresh_sptep(iter); - - iter->valid = true; + tdp_iter_return_to_root(iter); } /* diff --git a/arch/x86/kvm/mmu/tdp_iter.h b/arch/x86/kvm/mmu/tdp_iter.h index 5a47c57810ab..2ecc48e78526 100644 --- a/arch/x86/kvm/mmu/tdp_iter.h +++ b/arch/x86/kvm/mmu/tdp_iter.h @@ -63,5 +63,6 @@ void tdp_iter_start(struct tdp_iter *iter, u64 *root_pt, int root_level, int min_level, gfn_t next_last_level_gfn); void tdp_iter_next(struct tdp_iter *iter); u64 *tdp_iter_root_pt(struct tdp_iter *iter); +void tdp_iter_return_to_root(struct tdp_iter *iter); #endif /* __KVM_X86_MMU_TDP_ITER_H */ diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index a8fdccf4fd06..941e9d11c7ed 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -653,9 +653,7 @@ static inline bool tdp_mmu_iter_cond_resched(struct kvm *kvm, WARN_ON(iter->gfn > iter->next_last_level_gfn); - tdp_iter_start(iter, tdp_iter_root_pt(iter), - iter->root_level, iter->min_level, - iter->next_last_level_gfn); + tdp_iter_return_to_root(iter); return true; }