From patchwork Thu Sep 26 01:34:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13812684 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D554CCCF9EB for ; Thu, 26 Sep 2024 01:35:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B3726B0095; Wed, 25 Sep 2024 21:35:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 639D46B009C; Wed, 25 Sep 2024 21:35:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 443366B00C1; Wed, 25 Sep 2024 21:35:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 132DE6B0095 for ; Wed, 25 Sep 2024 21:35:29 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B7D48160720 for ; Thu, 26 Sep 2024 01:35:28 +0000 (UTC) X-FDA: 82605171936.10.765BB3B Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf01.hostedemail.com (Postfix) with ESMTP id E900C4000B for ; Thu, 26 Sep 2024 01:35:26 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=V9zIoD3q; spf=pass (imf01.hostedemail.com: domain of 3Xbr0ZgoKCOINXLSYKLXSRKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--jthoughton.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Xbr0ZgoKCOINXLSYKLXSRKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727314490; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MfYThc4u3zoEwhsYqTZzHZoRr9LypPNnm95GFpy1spA=; b=t4+BArPj7jURF9UinzPJMZ2K1hGNCqG8jV1amuBY65kJbDSpolmZJnOt/MuSkwu6xHUNmR H1kjLf3niFoHGNaNUSbVMrUGhN/tMORgWWI2xiNXahCPuY/pZUT/uZqL07VWmJCl1E5j9D xbNHHzSUVIQDAIKodRiIXPdDiFJIDpI= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=V9zIoD3q; spf=pass (imf01.hostedemail.com: domain of 3Xbr0ZgoKCOINXLSYKLXSRKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--jthoughton.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Xbr0ZgoKCOINXLSYKLXSRKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727314490; a=rsa-sha256; cv=none; b=rR4d17JVo+H9OSSGXQtAAP8Zd9ghgwrI8QjAa6Pet15Bkg4TXf1imy7ZB5yxU6oYoMNZZS BCt5gS/hgXlpFytoYt9wZEhiz1SwH+QHPgbv9UGPT9KaUOOByV+90k8n0bEKkUyq44eVMx 7laSPWXjuw9J2T4sw0ecB39bAhOce+0= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e230808388so6923337b3.0 for ; Wed, 25 Sep 2024 18:35:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727314526; x=1727919326; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MfYThc4u3zoEwhsYqTZzHZoRr9LypPNnm95GFpy1spA=; b=V9zIoD3qVfJ2VypVVbqwyLriwuz2JItn1BApSC2FV5YQeTqvQLI60oaiID3/6bkX3W R/c/Im2FOyEXJ1lfw+LvLKi2RX6bVlOOVltXO0QvrFA9+wUSu0V5hcYtAaDiwfkIQpkE zYtog2fGAcnB1Gx+28c7CRLO6xODt7ds3ojneTiyS/DxeFuxaWRr7ln6q9PVPOOgLpi+ 2Zs/NMzrTVK73wzP5dJCZOOPyhMm0SCoo7Q22+js3+ExBQWnAi8hOXH8LrLS5/0ie+lj 1QjjQcgST6vWDQ2bCjbL2fPuGRw3/r1nmZxfhIHcDWgTxJ+RP4setF5nXcV95rAQ2WPs g9Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727314526; x=1727919326; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MfYThc4u3zoEwhsYqTZzHZoRr9LypPNnm95GFpy1spA=; b=n0cX3YTikvV8ngU77vWVMAlHIdty12nuvUYwNuwOMgyUdrxiTCm3b/97KKYj9hmA4N 28KTmLxw++XWmzNibQczxkki2iWuZ7xCqvRnHPa6nUu0PuuwS9NVLadKQvgRho9PEk1j xVxM1qG5iF1j9Z1Sv28GtHQBl+QC4HxjAvzGI4Cua2ZfACUAp3RY1FVRC9RGS1zvhqrQ /qkF6Q2LAgBRRCwFjCj51GyE3L+5s47itRbJN3oZkyAOlOjyZf0nk0eHtDDMubS1HUOg WIhYsYGdzgRX225Siu12+sK6vztLDBPhm/0xo7qfZ/OKqhYSVY+YWQm4Y9J0+KlWARG6 eLEw== X-Forwarded-Encrypted: i=1; AJvYcCWMIFKgk5Vssj3W48VAgpDrc9id4j/hd4L83zoTdW3+UbuzIq3h9vI8th6fSDksf7QMfWWRr6mwbA==@kvack.org X-Gm-Message-State: AOJu0YyA8Ub/+BJ2oWkJ6PHiaYvhNX9d60BA3P4mrTMGH5YO4jSRTIRC G2/6/BksIJs54QDkcLe/dJRolc8IUos9lm0nvpJ6wsnqSYzB4ayu2Afeb8kq6lf0sbMOH90804r CU9qgTGhuw3i0Kno01w== X-Google-Smtp-Source: AGHT+IFc2BTb/n/6/Pa4YiRnYJohGJI1GOQLPmGDGx2avUwnGUEvHFpkm5PUE1m++wXUG9sRJDYpg95gpRMXrHPb X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:13d:fb22:ac12:a84b]) (user=jthoughton job=sendgmr) by 2002:a05:690c:5086:b0:6e2:1b8c:39bf with SMTP id 00721157ae682-6e21d835b06mr289447b3.2.1727314525948; Wed, 25 Sep 2024 18:35:25 -0700 (PDT) Date: Thu, 26 Sep 2024 01:34:57 +0000 In-Reply-To: <20240926013506.860253-1-jthoughton@google.com> Mime-Version: 1.0 References: <20240926013506.860253-1-jthoughton@google.com> X-Mailer: git-send-email 2.46.0.792.g87dc391469-goog Message-ID: <20240926013506.860253-10-jthoughton@google.com> Subject: [PATCH v7 09/18] KVM: x86/mmu: Add support for lockless walks of rmap SPTEs From: James Houghton To: Sean Christopherson , Paolo Bonzini Cc: Andrew Morton , David Matlack , David Rientjes , James Houghton , Jason Gunthorpe , Jonathan Corbet , Marc Zyngier , Oliver Upton , Wei Xu , Yu Zhao , Axel Rasmussen , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org X-Rspam-User: X-Stat-Signature: pnxunjxwm6hdan8ojbdm8i5wq4xrzhqj X-Rspamd-Queue-Id: E900C4000B X-Rspamd-Server: rspam11 X-HE-Tag: 1727314526-454407 X-HE-Meta: U2FsdGVkX18+YhkBwKvQJU5cVMbL97XFsTh6ieWjLJWC43BJNVrSrWwj8/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MIKVTsMG7b4r3GBjBnHOJLXAKnxHnZG+s3K4+37aWDZrBmPHzbq7C34G7jzww7pecBwfTHvAPa3hOPTr2v6N6AXc1PXUV2PiKcfZ4ZM5SWZWAycHssR2qfEHreGtOKO0J+vq46hfJtSUo1kIJzJfdwtgQVN+ov4f6J0rHkr05YkXNRpM9h/Vcu9/toPQ7aUcW+VHj8o1675UqYOZ5VBavz0W50TwxePl75Nuf+7FRwGf2rb2+On5m2MD66Vk6DddkAgKFhF6CHXq8EM24vCnZMP6hb/QZEIqBSrWhEYRqszbtnV4LmjxEoAm0hMuEZaK0i6lsXOFctaGkfqZQBEA6PlkA4LX3QMMJlCwet59yOUrYDCH2ZgNQuGaCvmo+PEKQ7dewh5eUEQ/L1JdFGySOuU1b46K5f3PBdayHBDtP2aVVod+jxdymk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Sean Christopherson Add a lockless version of for_each_rmap_spte(), which is pretty much the same as the normal version, except that it doesn't BUG() the host if a non-present SPTE is encountered. When mmu_lock is held, it should be impossible for a different task to zap a SPTE, _and_ zapped SPTEs must be removed from their rmap chain prior to dropping mmu_lock. Thus, the normal walker BUG()s if a non-present SPTE is encountered as something is wildly broken. When walking rmaps without holding mmu_lock, the SPTEs pointed at by the rmap chain can be zapped/dropped, and so a lockless walk can observe a non-present SPTE if it runs concurrently with a different operation that is zapping SPTEs. Signed-off-by: Sean Christopherson [jthoughton: Added lockdep assertion for kvm_rmap_lock, synchronization fixup] Signed-off-by: James Houghton --- arch/x86/kvm/mmu/mmu.c | 75 +++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 79676798ba77..72c682fa207a 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -932,7 +932,7 @@ static struct kvm_memory_slot *gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu */ #define KVM_RMAP_LOCKED BIT(1) -static unsigned long kvm_rmap_lock(struct kvm_rmap_head *rmap_head) +static unsigned long __kvm_rmap_lock(struct kvm_rmap_head *rmap_head) { unsigned long old_val, new_val; @@ -976,14 +976,25 @@ static unsigned long kvm_rmap_lock(struct kvm_rmap_head *rmap_head) */ } while (!atomic_long_try_cmpxchg_acquire(&rmap_head->val, &old_val, new_val)); - /* Return the old value, i.e. _without_ the LOCKED bit set. */ + /* + * Return the old value, i.e. _without_ the LOCKED bit set. It's + * impossible for the return value to be 0 (see above), i.e. the read- + * only unlock flow can't get a false positive and fail to unlock. + */ return old_val; } +static unsigned long kvm_rmap_lock(struct kvm *kvm, + struct kvm_rmap_head *rmap_head) +{ + lockdep_assert_held_write(&kvm->mmu_lock); + return __kvm_rmap_lock(rmap_head); +} + static void kvm_rmap_unlock(struct kvm_rmap_head *rmap_head, unsigned long new_val) { - WARN_ON_ONCE(new_val & KVM_RMAP_LOCKED); + KVM_MMU_WARN_ON(new_val & KVM_RMAP_LOCKED); /* * Ensure that all accesses to the rmap have completed * before we actually unlock the rmap. @@ -1023,14 +1034,14 @@ static void kvm_rmap_unlock_readonly(struct kvm_rmap_head *rmap_head, /* * Returns the number of pointers in the rmap chain, not counting the new one. */ -static int pte_list_add(struct kvm_mmu_memory_cache *cache, u64 *spte, - struct kvm_rmap_head *rmap_head) +static int pte_list_add(struct kvm *kvm, struct kvm_mmu_memory_cache *cache, + u64 *spte, struct kvm_rmap_head *rmap_head) { unsigned long old_val, new_val; struct pte_list_desc *desc; int count = 0; - old_val = kvm_rmap_lock(rmap_head); + old_val = kvm_rmap_lock(kvm, rmap_head); if (!old_val) { new_val = (unsigned long)spte; @@ -1110,7 +1121,7 @@ static void pte_list_remove(struct kvm *kvm, u64 *spte, unsigned long rmap_val; int i; - rmap_val = kvm_rmap_lock(rmap_head); + rmap_val = kvm_rmap_lock(kvm, rmap_head); if (KVM_BUG_ON_DATA_CORRUPTION(!rmap_val, kvm)) goto out; @@ -1154,7 +1165,7 @@ static bool kvm_zap_all_rmap_sptes(struct kvm *kvm, unsigned long rmap_val; int i; - rmap_val = kvm_rmap_lock(rmap_head); + rmap_val = kvm_rmap_lock(kvm, rmap_head); if (!rmap_val) return false; @@ -1246,23 +1257,18 @@ static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head, struct rmap_iterator *iter) { unsigned long rmap_val = kvm_rmap_get(rmap_head); - u64 *sptep; if (!rmap_val) return NULL; if (!(rmap_val & KVM_RMAP_MANY)) { iter->desc = NULL; - sptep = (u64 *)rmap_val; - goto out; + return (u64 *)rmap_val; } iter->desc = (struct pte_list_desc *)(rmap_val & ~KVM_RMAP_MANY); iter->pos = 0; - sptep = iter->desc->sptes[iter->pos]; -out: - BUG_ON(!is_shadow_present_pte(*sptep)); - return sptep; + return iter->desc->sptes[iter->pos]; } /* @@ -1272,14 +1278,11 @@ static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head, */ static u64 *rmap_get_next(struct rmap_iterator *iter) { - u64 *sptep; - if (iter->desc) { if (iter->pos < PTE_LIST_EXT - 1) { ++iter->pos; - sptep = iter->desc->sptes[iter->pos]; - if (sptep) - goto out; + if (iter->desc->sptes[iter->pos]) + return iter->desc->sptes[iter->pos]; } iter->desc = iter->desc->more; @@ -1287,20 +1290,24 @@ static u64 *rmap_get_next(struct rmap_iterator *iter) if (iter->desc) { iter->pos = 0; /* desc->sptes[0] cannot be NULL */ - sptep = iter->desc->sptes[iter->pos]; - goto out; + return iter->desc->sptes[iter->pos]; } } return NULL; -out: - BUG_ON(!is_shadow_present_pte(*sptep)); - return sptep; } -#define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \ - for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \ - _spte_; _spte_ = rmap_get_next(_iter_)) +#define __for_each_rmap_spte(_rmap_head_, _iter_, _sptep_) \ + for (_sptep_ = rmap_get_first(_rmap_head_, _iter_); \ + _sptep_; _sptep_ = rmap_get_next(_iter_)) + +#define for_each_rmap_spte(_rmap_head_, _iter_, _sptep_) \ + __for_each_rmap_spte(_rmap_head_, _iter_, _sptep_) \ + if (!WARN_ON_ONCE(!is_shadow_present_pte(*(_sptep_)))) \ + +#define for_each_rmap_spte_lockless(_rmap_head_, _iter_, _sptep_, _spte_) \ + __for_each_rmap_spte(_rmap_head_, _iter_, _sptep_) \ + if (is_shadow_present_pte(_spte_ = mmu_spte_get_lockless(sptep))) static void drop_spte(struct kvm *kvm, u64 *sptep) { @@ -1396,11 +1403,12 @@ static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head, struct rmap_iterator iter; bool flush = false; - for_each_rmap_spte(rmap_head, &iter, sptep) + for_each_rmap_spte(rmap_head, &iter, sptep) { if (spte_ad_need_write_protect(*sptep)) flush |= spte_wrprot_for_clear_dirty(sptep); else flush |= spte_clear_dirty(sptep); + } return flush; } @@ -1710,7 +1718,7 @@ static void __rmap_add(struct kvm *kvm, kvm_update_page_stats(kvm, sp->role.level, 1); rmap_head = gfn_to_rmap(gfn, sp->role.level, slot); - rmap_count = pte_list_add(cache, spte, rmap_head); + rmap_count = pte_list_add(kvm, cache, spte, rmap_head); if (rmap_count > kvm->stat.max_mmu_rmap_size) kvm->stat.max_mmu_rmap_size = rmap_count; @@ -1859,13 +1867,14 @@ static unsigned kvm_page_table_hashfn(gfn_t gfn) return hash_64(gfn, KVM_MMU_HASH_SHIFT); } -static void mmu_page_add_parent_pte(struct kvm_mmu_memory_cache *cache, +static void mmu_page_add_parent_pte(struct kvm *kvm, + struct kvm_mmu_memory_cache *cache, struct kvm_mmu_page *sp, u64 *parent_pte) { if (!parent_pte) return; - pte_list_add(cache, parent_pte, &sp->parent_ptes); + pte_list_add(kvm, cache, parent_pte, &sp->parent_ptes); } static void mmu_page_remove_parent_pte(struct kvm *kvm, struct kvm_mmu_page *sp, @@ -2555,7 +2564,7 @@ static void __link_shadow_page(struct kvm *kvm, mmu_spte_set(sptep, spte); - mmu_page_add_parent_pte(cache, sp, sptep); + mmu_page_add_parent_pte(kvm, cache, sp, sptep); /* * The non-direct sub-pagetable must be updated before linking. For