From patchwork Thu Sep 26 01:34:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13812677 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 48F96CCFA10 for ; Thu, 26 Sep 2024 01:35:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 74C4A6B00AE; Wed, 25 Sep 2024 21:35:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6FB5F6B00B4; Wed, 25 Sep 2024 21:35:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 574986B00B5; Wed, 25 Sep 2024 21:35:20 -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 373166B00AE for ; Wed, 25 Sep 2024 21:35:20 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DB5188123D for ; Thu, 26 Sep 2024 01:35:19 +0000 (UTC) X-FDA: 82605171558.11.2D10521 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 1428810000F for ; Thu, 26 Sep 2024 01:35:17 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZaDTARha; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3Vbr0ZgoKCNoFPDKQCDPKJCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3Vbr0ZgoKCNoFPDKQCDPKJCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--jthoughton.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727314420; a=rsa-sha256; cv=none; b=SAFzrMS0sfT5NAOnVdv4zM5+zRk8rHkLHd3hxTrR54UpluDKcDUeow0snLf2Jtcz+OFQTN 8nSRDHOwgZFOsCAVwBn2jTs6HvvxMQyBZ7EEgMT/dfI+qfidyuAwYsNjZ0Rxfj7jHcDO+D QVNXIlD8wZ/RchKIvBD2IoMJ4SbN2ro= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZaDTARha; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3Vbr0ZgoKCNoFPDKQCDPKJCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3Vbr0ZgoKCNoFPDKQCDPKJCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--jthoughton.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727314420; 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=nrPGFdAf3DB4vj7nij04fMkTLi5+MW1J/YyJNP+NgJQ=; b=W1ia44GI9QcQRZRe9pjmS/gSg7CKPYEkO9nuL0mgTYMzMEAHa7WqvWYbELsKvY8g8b3G8w IzNklfOrp7erSIqXYFMcTQhgaNgFHM0L7zt4Cxv1kmgoGzBL1ylojJC52VoB9198uLcHib VLyQMqK7odUwMs1ff3zO+qZETSIY78M= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e25cae769abso816232276.0 for ; Wed, 25 Sep 2024 18:35:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727314517; x=1727919317; 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=nrPGFdAf3DB4vj7nij04fMkTLi5+MW1J/YyJNP+NgJQ=; b=ZaDTARhaWJUiJxLTJW4Di3DOGr9TDlMXL12ZxCzIq+l899I6V6SaImQ0//lxSjkbAU JDSKwSh+2odQ32LPSGHQNw8aOnOYxcHon01ek8PZFTZLfuRAbaWwIah1YOk5dS1vA1HF AphYty54RPYcacvO6L+FH33MaGdTY6H7m5yIosc0ifL1MrDHa1PxIll8BfsjmY9AlgTy lvOq/ZcRxREOUzRHK2mZMPVpCy5s9D6WLZz/jn2fmzoNA5jD+FixwVoIWRDTwM1Qlfr4 zfhQFQJnN4X/yvnLjScH5RfbqwUyB/vrumXoyZ80N2LcSIeGpejGV6nuEBkwaL+pPmEv ++EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727314517; x=1727919317; 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=nrPGFdAf3DB4vj7nij04fMkTLi5+MW1J/YyJNP+NgJQ=; b=QTOjeA5pj1HzPod3HspG0NF40ohzWpTL8CdkOSf8j0iEV5GtKysrH6cqH8Z5rJ7/Pc bdUf85T8lziP8eDsbMqsM0mJzXMjkQFcZzr5q/XGqzMcQEGagaS3uxa5v/XuuxqFbv8S /tXxKTKXRPzYN9qpGeOvUO7GgUjtiH0OlP+0Etehx6wsBNDacO2WZeACcJcnI6GsHzDy j0GG07/ruN15lycFWCzm203zzJgF7UOmrNAAHF2ZWbjz0N0ghslPX7II9ks+4qgp0umh 3jB5u9DRomURifbo+sY55N/2OzLcCpg31O8MV80SBCsPmW2UEk3Z6jHfC590Dn+0Ww5k GClQ== X-Forwarded-Encrypted: i=1; AJvYcCU6VetQ46ZtXmsdeJH2Bo9ztmK2JqQZnrHzcO3JEGUftT2c8mufO/CbCv2cSbkpuwOwaSQtdoAIiQ==@kvack.org X-Gm-Message-State: AOJu0YxKkXSpD2WbiYuftilqhofYW7o9vNFhRKq53tYT0e4DJTuXm7DW 0MdiiJvGCcT/fqEsD5Hxdtb7RvzJPogrRA2ALo5dZQbgM4kVbHubq+fTLWORpfwo9SHl6l/hUb6 cYmZ0ok13F5bFe1qbSg== X-Google-Smtp-Source: AGHT+IHBkpgMYqE9bFhZXKzj+UxHtQzg97Wx4pqBE3O5Wvje89pjGl0kpLMaEu3zaYYGi2UruxZxZDhUO7JaGYM6 X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:13d:fb22:ac12:a84b]) (user=jthoughton job=sendgmr) by 2002:a25:3546:0:b0:e1b:10df:78ea with SMTP id 3f1490d57ef6-e24d7df1a54mr3612276.4.1727314517106; Wed, 25 Sep 2024 18:35:17 -0700 (PDT) Date: Thu, 26 Sep 2024 01:34:50 +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-3-jthoughton@google.com> Subject: [PATCH v7 02/18] KVM: Add lockless memslot walk to KVM 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-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 1428810000F X-Stat-Signature: jkjmpyz13rjmsnnajeugsfbujxtm9d9z X-Rspam-User: X-HE-Tag: 1727314517-347596 X-HE-Meta: U2FsdGVkX19m+7rFO8rAudDJ3N9wG4VzlnYKJ7f2pN8ZmAL0tyDgBr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0ngdFHqmYHFPLs7f3WDvHNEG/Sl1Ji4jNmuVevmdnSpxFtx7P5Wxqi5+2b5XSh8i0IIkCTKs8R0Pd0F11E3dOraKFJOthEt5raAAHK4Nu133p+gtOVNLfroO4GqLa5oikFpzLEYBpbZU2g1VDe3LCvmKaCH0PqsMTDN3KNpveiR/BtwIfQxrtfERfqRZy6oMFQOPx5ha5LWJb447a4Yx++AJRlpaQ2g/8C4Qjay1rpr22cby3cCI/KbloivCLCuDwLTv3HFJgOh02JEFnL/p1u5de2LpZT3HSwiggnD4FkIR8UFNAlduWl+f+XIdlrhyFbYnBt8/Du8y3D0ygCzXYqlZdPo/yuwxkMG/vXpmQJ/e9AUFN7Jfily0IVdW9+R4jT5IITO4I1Ssr9M2umZ/OTALGGp9GKe27weiyJMVLkhKB 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: Provide flexibility to the architecture to synchronize as optimally as they can instead of always taking the MMU lock for writing. Architectures that do their own locking must select CONFIG_KVM_MMU_NOTIFIER_YOUNG_LOCKLESS. The immediate application is to allow architectures to implement the test/clear_young MMU notifiers more cheaply. Suggested-by: Yu Zhao Signed-off-by: James Houghton --- include/linux/kvm_host.h | 1 + virt/kvm/Kconfig | 3 +++ virt/kvm/kvm_main.c | 28 +++++++++++++++++++++------- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index b23c6d48392f..98a987e88578 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -266,6 +266,7 @@ struct kvm_gfn_range { gfn_t end; union kvm_mmu_notifier_arg arg; bool may_block; + bool lockless; }; bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range); diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index fd6a3010afa8..58d896b2f4ed 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -100,6 +100,9 @@ config KVM_GENERIC_MMU_NOTIFIER select MMU_NOTIFIER bool +config KVM_MMU_NOTIFIER_YOUNG_LOCKLESS + bool + config KVM_GENERIC_MEMORY_ATTRIBUTES depends on KVM_GENERIC_MMU_NOTIFIER bool diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 090e79e4304f..7d5b35cfc1ed 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -555,6 +555,7 @@ struct kvm_mmu_notifier_range { on_lock_fn_t on_lock; bool flush_on_ret; bool may_block; + bool lockless; }; /* @@ -609,6 +610,10 @@ static __always_inline kvm_mn_ret_t kvm_handle_hva_range(struct kvm *kvm, IS_KVM_NULL_FN(range->handler))) return r; + /* on_lock will never be called for lockless walks */ + if (WARN_ON_ONCE(range->lockless && !IS_KVM_NULL_FN(range->on_lock))) + return r; + idx = srcu_read_lock(&kvm->srcu); for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { @@ -640,15 +645,18 @@ static __always_inline kvm_mn_ret_t kvm_handle_hva_range(struct kvm *kvm, gfn_range.start = hva_to_gfn_memslot(hva_start, slot); gfn_range.end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, slot); gfn_range.slot = slot; + gfn_range.lockless = range->lockless; if (!r.found_memslot) { r.found_memslot = true; - KVM_MMU_LOCK(kvm); - if (!IS_KVM_NULL_FN(range->on_lock)) - range->on_lock(kvm); - - if (IS_KVM_NULL_FN(range->handler)) - goto mmu_unlock; + if (!range->lockless) { + KVM_MMU_LOCK(kvm); + if (!IS_KVM_NULL_FN(range->on_lock)) + range->on_lock(kvm); + + if (IS_KVM_NULL_FN(range->handler)) + goto mmu_unlock; + } } r.ret |= range->handler(kvm, &gfn_range); } @@ -658,7 +666,7 @@ static __always_inline kvm_mn_ret_t kvm_handle_hva_range(struct kvm *kvm, kvm_flush_remote_tlbs(kvm); mmu_unlock: - if (r.found_memslot) + if (r.found_memslot && !range->lockless) KVM_MMU_UNLOCK(kvm); srcu_read_unlock(&kvm->srcu, idx); @@ -834,6 +842,8 @@ static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, .on_lock = (void *)kvm_null_fn, .flush_on_ret = true, .may_block = false, + .lockless = + IS_ENABLED(CONFIG_KVM_MMU_NOTIFIER_YOUNG_LOCKLESS), }; trace_kvm_age_hva(start, end); @@ -854,6 +864,8 @@ static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn, .on_lock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = false, + .lockless = + IS_ENABLED(CONFIG_KVM_MMU_NOTIFIER_YOUNG_LOCKLESS), }; trace_kvm_age_hva(start, end); @@ -886,6 +898,8 @@ static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn, .on_lock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = false, + .lockless = + IS_ENABLED(CONFIG_KVM_MMU_NOTIFIER_YOUNG_LOCKLESS), }; trace_kvm_test_age_hva(address);