From patchwork Thu Sep 14 01:54:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384008 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 C891FEE0213 for ; Thu, 14 Sep 2023 01:55:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 441AB6B0278; Wed, 13 Sep 2023 21:55:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F2C16B0279; Wed, 13 Sep 2023 21:55:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B9FE6B027A; Wed, 13 Sep 2023 21:55:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1B8EF6B0278 for ; Wed, 13 Sep 2023 21:55:41 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E2C59120EF6 for ; Thu, 14 Sep 2023 01:55:40 +0000 (UTC) X-FDA: 81233536440.10.414CD53 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf02.hostedemail.com (Postfix) with ESMTP id 213C980002 for ; Thu, 14 Sep 2023 01:55:38 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jQts7R+7; spf=pass (imf02.hostedemail.com: domain of 3GWgCZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3GWgCZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.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=1694656539; h=from:from:sender:reply-to: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=/cDQ8+RWss5y1dl7//cmz9qd0NB/VeUV6+/tNlKTQHI=; b=Nu2NDrJVe9sfBZRbTd3Bo/AOoZMcJtUa+mFPTcQuLInukF37omRa4hjTnM0oGPCfXlGlM3 h+S0zTT8zpVQvBKmThuufzMyvgAN8mhE2sezqTfEUkhaTkacXN27TBXEyjc9P+2EnrMTBH brvJT84YPg8x8CHSGX98cfjMZLEvR6I= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jQts7R+7; spf=pass (imf02.hostedemail.com: domain of 3GWgCZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3GWgCZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656539; a=rsa-sha256; cv=none; b=IvcvHKYZf/Foy22sj32U3oZ6/4AgqXRsNqX4+i1y2kWhPTVwHRwDVVx2sF3BBRlykJ8t7k pXBaVDPq+P5U6EzXlazf6DjMOUUX930jFD4v8+pcuM0EyWDpTUMIT+44GniqtL04zTH6KF Kj4GmL5KNz2r7S45AXEqMHtBBhhuHu8= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1bf681d3d04so3893365ad.2 for ; Wed, 13 Sep 2023 18:55:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656538; x=1695261338; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=/cDQ8+RWss5y1dl7//cmz9qd0NB/VeUV6+/tNlKTQHI=; b=jQts7R+747ZpbBI421UvcWO7a9PWV2qqWSwoCYpuOjD4d1PNVO7eb8l8W1g12Mb4mi 9I2h/ltak6WgWRwvNsU7mladmAsJW4mLX1vaPGXsYpv7D7fCBWO/2+H439jS4xhcGee9 VQTC2p0qJ4hsr/3SnG2zcjU/n56JvoHPQiuSZzrf01Xzl5UdUwYCRNlo8c8E7T7/xVki AiJaQ7kKaT0/avrs4KBmVYDdzUf1xKb4H+4fx181tCT8jvcu3CIU9LEnsvycmY0fbD8z GtRL9HgHSiVqaM5d2SzP14KF9Ff3jZMkFMsj4h4pUXNqR/OC2i9MZ9K6ObYTxaZRaWRV zaMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656538; x=1695261338; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/cDQ8+RWss5y1dl7//cmz9qd0NB/VeUV6+/tNlKTQHI=; b=m5a7I8DyKyxuGIfesvYARAS+V5sA4HNA/u3pNQLJGWqsFBqFiW2QB5WiWzy0JxNcWj O0fOzdmrLeli2GqBbnOx6N5KeZ6OjRa/33oC0zpWpvXmFCraQLXZR/nfRqvmgM67pxxH fdJUgHPXotaDSwqBBHrVUW6nvSuSy4cYKSuRdOvjiggEPG9RcuLgsprHAZr95R0vuiDk dg8IHbMnBivDM6t9mjBACzXtDRyddPh81isyICLWkFtwvnaI4p9rOMLkQFc1XnEHbEon mrRL8PY783I+BsNLCg/Z/QRdYgvXnomcVeSnU5NdxWIAyrd1p+QaaNQkENBcT16Xums+ yIGw== X-Gm-Message-State: AOJu0YzT/Cx1jqqCNsLuHJZbPTjWEiz1Qz6LxBt83PsKGxPUXuQn/Qzc bJJVNXbJ0fhY4XlVEeDTnrHEFDArDgA= X-Google-Smtp-Source: AGHT+IE0PXMQEm/IFGrVOf6ppWkIN82E9wFoqnKGFhjVJyi0Xqws7PTmRSBCpdZciUBlWguOQ7crfgDDDDg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e88f:b0:1b8:8c7:31e6 with SMTP id w15-20020a170902e88f00b001b808c731e6mr201908plg.1.1694656537960; Wed, 13 Sep 2023 18:55:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:54:59 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-2-seanjc@google.com> Subject: [RFC PATCH v12 01/33] KVM: Tweak kvm_hva_range and hva_handler_t to allow reusing for gfn ranges From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 213C980002 X-Rspam-User: X-Stat-Signature: gzqcd41ozb9crfoworx4wy7hjqnt3cj4 X-Rspamd-Server: rspam01 X-HE-Tag: 1694656538-148150 X-HE-Meta: U2FsdGVkX19tes8r/tpYZtAGEehcbYCf8i75Hmy6CJt+35CAVtTes3EBoOWGR0aYEV++m5UjbpsRHD5xhfIPTB5mwRCgMRu4LzFBZK+/vhtYe5V2Lj6UQqOXv04/MiCM2tSofDcEtmmj5Mk8jzFFWP8+CVIOOSpVFICl6oOr/i08suhJkFC9sHE7hjQbdKzzrZMX/w16GqPfoCUc89rpBY4bPvZo1M7sTmNA4YUTDqG8ssb7v58l9PVRCKizZ18bOmXxlU3JfikZSyCMQCNNs1KqUJJKB97wK563B9dgCVkZzoyifRGtMmDIuzIU+85VxZG2GEce+G/JHoA4XyQ4oSMeWCjTkQoWLDy+Z4nk0iZzN4wxNBlz+JhMdkgcPuoq0gvW837QjEwwaJpifZifF+cnf13OZlxPkF9A2hF4FO40pQNRyWVp2/iTokpKzXyTu5zWUd1XSAaywCJTKLEfA/ITvbO2YA7XtBVzsyZYKMoZdRLmJ4bQQxoDBmsDd+Gd5u/OXVc1Pj9LjcTWD19giCfi3wQxeNN+BTSmyrEFb9znQFwZhiPO+X2CRuCSAUOnzUWolLe+Bmudj1qB/hnDTQHkEi28j/r/KjUOWtlkJweLJgvu7WgCyGB92banATdwoYy8rp1sHxOl/bxpzcO2lGlyG7CwAQ+C3KXluFVcG8oFVz6+mnGLPx7SQIMCFedAtDvn95sfHpPeiWgd1qR377WBZdGWsio9T//xcWmDp5R0hexmJmjzIu86mDK7pgrwLlkDR0gNicgVIkxSoS1iVOjVwCDHUOFvZXjjZJp6t0ErkjyH5++upRSkj7gf17ORLL+WxZ7vmU6Ig7KbmKv5YOM16us3GOVLscbMRmT2t3/+I14mfGgwy+3FVIWxzo6Gj33YzCYUbhCh7P1+82svjDB63MOxqqFGit/2fpI6bShcmXg1jHkg9GqdiqyBsqbXtsmGb9DfsSypLtH1Rbz 3eCgPSLi FEoG/J3fQ/K2qdPx45NoW3+it1g5WmjPYo35yl2XP789tXKxvJKNG0Chc4aIkglPte0NaoZIU9S05QdUoZ7NQAIk2PwTPiqo0kgNSsgrrBEwWO9rXwt7u7/Cq7I9w01X/teZKZrngn31xojdUAZ18tnVV3Uo3TgYp5WNvVHitMs+eBPMyTHy7hmfITrtMrH+L4Rux/Bqc6rL3X2ndh2N4fLEoU1k3BbPmvMznwp5wNcipA2FXaV+61wONq5hen1/QjgVfi7xTL0lFNUQY2U9AfLhMfM6lZGgJMj8JPN92dPB1XQWgUWNYwGhegf3idrkmGiplbK97pdPErjMIYmxrElCM/35Sz1TfMQ4RlXsFG+cOk7ge3JtJd1RSjzWIQMc3q2AeZuD6stms7cV+QSNwnbC4mnqTWI1URAyNkIQEMZaomI029YIemEJGQZN9Aqh+RMY96JhBD7CNLT0M2duJZc64SGKklwSx4yqvEJ7EQzpPVqDw/Yua0ihMtMB9GxiI71gLLBjWkETj3RtPbEuwifKTs5xxtYQhKjQeveTXDYvZ5oxnhXoszhznCTDcN6Kd/kMLXGev3kcTgE/rugVzqydDb3OqM8t+d32YS0FRAOMcN0aErf7SEYFRE89TsGxsyqbFI9sAFKKBpuXvr/csdZvekgav0USYBM3L3gk3LcBIDeVnbfXnuY5EkAWDP+FvpuxRbEYy6NLDPHZMndMx6qJrAXTydUATuV8q5TDPwFkw/MOH7Bgg6IccuaXmBWt2lX9YEbBsvAoK+C9Em57bnoSMd8OODGRrzVuSA6gelstYd2oQjre2wpzsKZihwdzIAhE+0/VnqwMZV5hHb8nK8qGcoA== 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: Rework and rename "struct kvm_hva_range" into "kvm_mmu_notifier_range" so that the structure can be used to handle notifications that operate on gfn context, i.e. that aren't tied to a host virtual address. Practically speaking, this is a nop for 64-bit kernels as the only meaningful change is to store start+end as u64s instead of unsigned longs. Reviewed-by: Paolo Bonzini Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- virt/kvm/kvm_main.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 486800a7024b..0524933856d4 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -541,18 +541,22 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) return container_of(mn, struct kvm, mmu_notifier); } -typedef bool (*hva_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range); +typedef bool (*gfn_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range); typedef void (*on_lock_fn_t)(struct kvm *kvm, unsigned long start, unsigned long end); typedef void (*on_unlock_fn_t)(struct kvm *kvm); -struct kvm_hva_range { - unsigned long start; - unsigned long end; +struct kvm_mmu_notifier_range { + /* + * 64-bit addresses, as KVM notifiers can operate on host virtual + * addresses (unsigned long) and guest physical addresses (64-bit). + */ + u64 start; + u64 end; union kvm_mmu_notifier_arg arg; - hva_handler_t handler; + gfn_handler_t handler; on_lock_fn_t on_lock; on_unlock_fn_t on_unlock; bool flush_on_ret; @@ -581,7 +585,7 @@ static const union kvm_mmu_notifier_arg KVM_MMU_NOTIFIER_NO_ARG; node = interval_tree_iter_next(node, start, last)) \ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, - const struct kvm_hva_range *range) + const struct kvm_mmu_notifier_range *range) { bool ret = false, locked = false; struct kvm_gfn_range gfn_range; @@ -608,9 +612,9 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, unsigned long hva_start, hva_end; slot = container_of(node, struct kvm_memory_slot, hva_node[slots->node_idx]); - hva_start = max(range->start, slot->userspace_addr); - hva_end = min(range->end, slot->userspace_addr + - (slot->npages << PAGE_SHIFT)); + hva_start = max_t(unsigned long, range->start, slot->userspace_addr); + hva_end = min_t(unsigned long, range->end, + slot->userspace_addr + (slot->npages << PAGE_SHIFT)); /* * To optimize for the likely case where the address @@ -660,10 +664,10 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start, unsigned long end, union kvm_mmu_notifier_arg arg, - hva_handler_t handler) + gfn_handler_t handler) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range range = { + const struct kvm_mmu_notifier_range range = { .start = start, .end = end, .arg = arg, @@ -680,10 +684,10 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn, unsigned long start, unsigned long end, - hva_handler_t handler) + gfn_handler_t handler) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range range = { + const struct kvm_mmu_notifier_range range = { .start = start, .end = end, .handler = handler, @@ -771,7 +775,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range hva_range = { + const struct kvm_mmu_notifier_range hva_range = { .start = range->start, .end = range->end, .handler = kvm_unmap_gfn_range, @@ -835,7 +839,7 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, const struct mmu_notifier_range *range) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range hva_range = { + const struct kvm_mmu_notifier_range hva_range = { .start = range->start, .end = range->end, .handler = (void *)kvm_null_fn,