From patchwork Tue Jun 11 00:21:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13692705 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1CAD0C27C5F for ; Tue, 11 Jun 2024 00:22:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=p6JLKlvqN8Oqtg35g+gKM+WF/WNv7SFa6eFogaU/d54=; b=Gv+jAipvuYnGavV3f+ia5lOjHo 1j694AxpAS+8N6eroOM9aPOqvCrX6Kyr5g1AYh24SPz0G8dmAYzXXAC/u6wPKb+uzyL/D40QGyDuz dhoG+g0Dqg+l5EaFe0mf4Pgbk6D8tM5+Hg0oCO0TM9hIsA+uv+ROXhhChL2kZCg6e7BNUw/dPyDop SDmsCoHfoVVX9HFiP1bYCtAr3Dle6MHMoBSJ7Vc6uemQG+GMHho8DdmQd32WCLQCit6vT19mtreIg Zyt6dfH1G2wKPFDlxaAEz6Xs+qUeNQSDWmmdBiLqgHv3iCRBsuu5ypbtyFvGwntExofBkm8MBFEwa 9TMOIdgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGpH5-00000006vnU-3oJY; Tue, 11 Jun 2024 00:22:08 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGpH1-00000006vkV-22PT for linux-arm-kernel@lists.infradead.org; Tue, 11 Jun 2024 00:22:04 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-df7a6530373so9383599276.0 for ; Mon, 10 Jun 2024 17:22:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718065320; x=1718670120; darn=lists.infradead.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=UYg8xBfSHgODLvTlV3mxh03y8s3TyTlow0j+6oXRZyE=; b=K7MnOoMDTXR0J85RneVEgeqfLtTbr0lhki0fOhN9mxqp9rKhj9eT+QjbAv5kWByPjT 6hzK42PX7xtfSFX7NfBrvrarUquI+5KtbD7uFrGxI6P89JB64GpjSNsuKiL5g28mYjfz 3lo26X8SE+8m7QI70AEp8cbUdFR+RUQoqo94I7h4CeV9rEpSXP05DOVOXzKbDzpLvocA 0UolU+GrS3oYZOyC/1a960R45DKQv3uVBlXQ4laSb3Qd2kye5BABG5NjzWx136sj4ufx qORu6A3QXcg2xvnG0dRoB3taJ+yQo8oM470YHqBAdJTTWXapkSvN83Vvzd4P5J2P9/Zp VGJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718065320; x=1718670120; 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=UYg8xBfSHgODLvTlV3mxh03y8s3TyTlow0j+6oXRZyE=; b=kM0x74pEvLs6jaBNGGuLrN4RmKunBBChmMJllKBBmWbnSQ5c2mPm9E0Kfg7DV28Ojl KJhc79s+Hq+vrR8I6jJ36ytYXo5JsmiPOh8TGParUyxBSRyRPUSr1ot/g3fptPWY7rPl EdU5Lrgm/l+r28NO1giaHdIzyl7uRkaKBPhD35B7km2zBdJb7LHX7YqEiGa6yNTIFN2w axzUreubeM4TG1chfMBtiIIjpfhOaXF5m+QbrL/KC3Mn2T315hoYMLwH4vwlBR9aI71d ocFNkKIy/CCAA4l+psuL4+QA53hw6dkfQJAEJOC95FXu8+STsg0Vuz9jGy3FxByzskJv MRpQ== X-Forwarded-Encrypted: i=1; AJvYcCXMg5iwrR52kgTke50NtN+CP0IuPt0TlyYm9K9w5evnTFFP8JUmGfVfkyTa7IOZloBNXcHUsXkJUYkoSDq2mEF/syu6Mqhdnewqt7rCr21LeFoi7Z0= X-Gm-Message-State: AOJu0Yy3+YkDQBS/SlOtFMBz07+LbrB/+tc6vu6FHSUK/DjCCZxftui0 gniL/qgpMHVneD55GvEBPwrHksxarqvggGamI/UJH946SbDor8AAOaaD76A3snnGAj9rEI4NJqD vLhdvM2ScgMbsQofj6g== X-Google-Smtp-Source: AGHT+IEGsmdXMyxgJ1Cj/3hdc6/ErG88swxZnK/rdFuCwoqmhzkdtuuICPRcxc2axyqsks7ulwYmlzACcQfHnSLC X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:6902:1003:b0:dd9:20c1:85b6 with SMTP id 3f1490d57ef6-dfd9fcb617bmr288182276.2.1718065319751; Mon, 10 Jun 2024 17:21:59 -0700 (PDT) Date: Tue, 11 Jun 2024 00:21:37 +0000 In-Reply-To: <20240611002145.2078921-1-jthoughton@google.com> Mime-Version: 1.0 References: <20240611002145.2078921-1-jthoughton@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240611002145.2078921-2-jthoughton@google.com> Subject: [PATCH v5 1/9] KVM: Add lockless memslot walk to KVM From: James Houghton To: Andrew Morton , Paolo Bonzini Cc: Ankit Agrawal , Axel Rasmussen , Catalin Marinas , David Matlack , David Rientjes , James Houghton , James Morse , Jonathan Corbet , Marc Zyngier , Oliver Upton , Raghavendra Rao Ananta , Ryan Roberts , Sean Christopherson , Shaoqin Huang , Suzuki K Poulose , Wei Xu , Will Deacon , Yu Zhao , Zenghui Yu , kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240610_172203_555512_2D24439E X-CRM114-Status: GOOD ( 16.14 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 | 26 +++++++++++++++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 692c01e41a18..4d7c3e8632e6 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 29b73eedfe74..0404857c1702 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -97,6 +97,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 14841acb8b95..d8fa0d617f12 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -558,6 +558,7 @@ struct kvm_mmu_notifier_range { on_lock_fn_t on_lock; bool flush_on_ret; bool may_block; + bool lockless; }; /* @@ -612,6 +613,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++) { @@ -643,15 +648,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)) - break; + 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)) + break; + } } r.ret |= range->handler(kvm, &gfn_range); } @@ -660,7 +668,7 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, if (range->flush_on_ret && r.ret) kvm_flush_remote_tlbs(kvm); - if (r.found_memslot) + if (r.found_memslot && !range->lockless) KVM_MMU_UNLOCK(kvm); srcu_read_unlock(&kvm->srcu, idx); @@ -681,6 +689,8 @@ static __always_inline int kvm_handle_hva_range(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), }; return __kvm_handle_hva_range(kvm, &range).ret; @@ -699,6 +709,8 @@ static __always_inline int kvm_handle_hva_range_no_flush(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), }; return __kvm_handle_hva_range(kvm, &range).ret;