From nobody Tue Nov 5 10:48:31 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B04DBC25B48 for ; Fri, 27 Oct 2023 18:22:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231594AbjJ0SWh (ORCPT ); Fri, 27 Oct 2023 14:22:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47692 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232301AbjJ0SWb (ORCPT ); Fri, 27 Oct 2023 14:22:31 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DBFB1A1 for ; Fri, 27 Oct 2023 11:22:28 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2800ef7d1ddso675541a91.3 for ; Fri, 27 Oct 2023 11:22:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430947; x=1699035747; darn=vger.kernel.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=ySJgJVuGTMb9d8TKULAih8JyrrSd4YVeUb9Jc+6kES0=; b=s0jU3Rgpw7Sefpwq7Aoxxr4+RVgqsmAczpwZnarGUs2RS98QnByA3C5i80GMz3YEhY qS12VS7IWyeVBLqP9Zdvj6K1vZ+UU4LHH/sxr+jOHxQfwMj0OSmYJSwNa1BH601Rtpj1 riFTpK8dqTlZY0liKk4y7VyhLvbhwrePGU8EclNb93exDl//evBoAzVdKQetLr8+j7BZ uSlfO+A9jXtDULNLK/XgmIT5zz+u7iSzKIzKEwi73cdyE9FsQj2SbIll4EPvpvGFMq3q kHOrLt/1QQAzqW6fwLU4b6nHJnpPaACtmwLFqbRQ4nc3Il3U4UYbke0V5OCgZgqxrnUU r/rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430947; x=1699035747; 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=ySJgJVuGTMb9d8TKULAih8JyrrSd4YVeUb9Jc+6kES0=; b=erjRkZy2laqIruFynxQ7PlkCVoiM1gRt5MclO9KGSS96iJCaplWmsRcVQ9fzRjE0Wl dHj+Y7uuhJ/bRbuduC1PwlgjpkAoRTNi4+oRgS7ECoNbDi9qSwaaZL0cN8Zz8pvgESht NL1tQsCFrgGM+TLZFeX9Gu6au7njI0WVFNOcm0YymNTwmji6dFxY9QMJa62kB7JEYhOQ vWsBHQRkXF54zlcUQP98hQxROh1UkzYKLHyzBmVx9oALhTdCHeirM/+Opc1vrz/go1k7 YMyl2rTEAB5n4qEjY9VzO2lp3hk6Sv59LfMwFcEnzYLrrZhc1beEkyKKhtZSnO324qMU wvsQ== X-Gm-Message-State: AOJu0YxDtGsuxtgojrketkc/2POH2h6AYy25yo+9TUu14p/v6H5d4KVY w3miiHCJ0rukkaQZc8I/uOHlK+YOml8= X-Google-Smtp-Source: AGHT+IEjsuFrlNkUUm601K8PUP+r8R6hCJNbQUJrGpc/pHCXTUzh4iBnaMaTICwCQxjFnojaM1raJDMMRNs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ac8e:b0:1cc:30cf:eae6 with SMTP id h14-20020a170902ac8e00b001cc30cfeae6mr955plr.10.1698430947671; Fri, 27 Oct 2023 11:22:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:43 -0700 In-Reply-To: <20231027182217.3615211-1-seanjc@google.com> Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231027182217.3615211-2-seanjc@google.com> Subject: [PATCH v13 01/35] 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 , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton 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-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , "=?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?=" , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Rename the handler typedef too (arguably it should always have been gfn_handler_t). 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 Reviewed-by: Xiaoyao Li Signed-off-by: Sean Christopherson --- 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); } =20 -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= ); =20 typedef void (*on_lock_fn_t)(struct kvm *kvm, unsigned long start, unsigned long end); =20 typedef void (*on_unlock_fn_t)(struct kvm *kvm); =20 -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_NOTIFIE= R_NO_ARG; node =3D interval_tree_iter_next(node, start, last)) \ =20 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 =3D false, locked =3D false; struct kvm_gfn_range gfn_range; @@ -608,9 +612,9 @@ static __always_inline int __kvm_handle_hva_range(struc= t kvm *kvm, unsigned long hva_start, hva_end; =20 slot =3D container_of(node, struct kvm_memory_slot, hva_node[slots->nod= e_idx]); - hva_start =3D max(range->start, slot->userspace_addr); - hva_end =3D min(range->end, slot->userspace_addr + - (slot->npages << PAGE_SHIFT)); + hva_start =3D max_t(unsigned long, range->start, slot->userspace_addr); + hva_end =3D min_t(unsigned long, range->end, + slot->userspace_addr + (slot->npages << PAGE_SHIFT)); =20 /* * To optimize for the likely case where the address @@ -660,10 +664,10 @@ static __always_inline int kvm_handle_hva_range(struc= t 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 =3D mmu_notifier_to_kvm(mn); - const struct kvm_hva_range range =3D { + const struct kvm_mmu_notifier_range range =3D { .start =3D start, .end =3D end, .arg =3D arg, @@ -680,10 +684,10 @@ static __always_inline int kvm_handle_hva_range(struc= t mmu_notifier *mn, static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifi= er *mn, unsigned long start, unsigned long end, - hva_handler_t handler) + gfn_handler_t handler) { struct kvm *kvm =3D mmu_notifier_to_kvm(mn); - const struct kvm_hva_range range =3D { + const struct kvm_mmu_notifier_range range =3D { .start =3D start, .end =3D end, .handler =3D handler, @@ -771,7 +775,7 @@ static int kvm_mmu_notifier_invalidate_range_start(stru= ct mmu_notifier *mn, const struct mmu_notifier_range *range) { struct kvm *kvm =3D mmu_notifier_to_kvm(mn); - const struct kvm_hva_range hva_range =3D { + const struct kvm_mmu_notifier_range hva_range =3D { .start =3D range->start, .end =3D range->end, .handler =3D kvm_unmap_gfn_range, @@ -835,7 +839,7 @@ static void kvm_mmu_notifier_invalidate_range_end(struc= t mmu_notifier *mn, const struct mmu_notifier_range *range) { struct kvm *kvm =3D mmu_notifier_to_kvm(mn); - const struct kvm_hva_range hva_range =3D { + const struct kvm_mmu_notifier_range hva_range =3D { .start =3D range->start, .end =3D range->end, .handler =3D (void *)kvm_null_fn, --=20 2.42.0.820.g83a721a137-goog