From patchwork Fri Oct 27 18:21:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438928 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 8D7A5C25B48 for ; Fri, 27 Oct 2023 18:22:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 115E7900004; Fri, 27 Oct 2023 14:22:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 04FE9900002; Fri, 27 Oct 2023 14:22:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE4C5900004; Fri, 27 Oct 2023 14:22:30 -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 C9CAB900002 for ; Fri, 27 Oct 2023 14:22:30 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9B02F14078A for ; Fri, 27 Oct 2023 18:22:30 +0000 (UTC) X-FDA: 81392061660.15.871724D Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf24.hostedemail.com (Postfix) with ESMTP id C7EF218001F for ; Fri, 27 Oct 2023 18:22:28 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=itZH9v8d; spf=pass (imf24.hostedemail.com: domain of 34_87ZQYKCAQwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=34_87ZQYKCAQwierngksskpi.gsqpmry1-qqozego.svk@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=1698430948; 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=ySJgJVuGTMb9d8TKULAih8JyrrSd4YVeUb9Jc+6kES0=; b=UTiHwLwo4vParhOQQAPY6eXRWczjDOnoVoiYmToDM4FTmn4WPpYaquNehKcUe6r2x7LCnX lfR4m4b2z4KAwS2XAi08N9LPdG+msxR5fiCEikhqDJ+VkvpfNZQltWP6kKAVvt/L0fJWLH fY2SNooW7Te0aP5xP83lx3ttqGjkCIg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430948; a=rsa-sha256; cv=none; b=FzWv53Uil+aeYP7p5duwMSHL0ZV0iLWzYbthI1MVXoqn9J0mYl5Wc2nL+53iTiUBswCbLg 0vxX7S2wYfS+5bryMfaCWNKtb77uK3eal3MFMPbIcVAxDkVIygaco90MraCZPzjKvgiHmh aQFZpw1ddcIYkVvTc1GvKi3hmWCdBOY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=itZH9v8d; spf=pass (imf24.hostedemail.com: domain of 34_87ZQYKCAQwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=34_87ZQYKCAQwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1cc23f2226bso7015935ad.2 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=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=ySJgJVuGTMb9d8TKULAih8JyrrSd4YVeUb9Jc+6kES0=; b=itZH9v8dDsHV3011UDiy+2/07jw582ASQcLraOKqoBNMqnlfAFH7+gNOzwSQVRA3tk cxWivXlZJWahwQyeE8mqa2lBcbQwCIW8bmY+Dzf/VsUhtgnqQYAPZ8mEKoo3Co+2GoRN BFwm8tIWc8WgmT0QJCbsk5+kyLuEfheW1hgNmsHZ7hyozW/9+9Rc3UUyMZFK/frFDx6b 3nKZKHZFSD/YNjkwyD+juM3/CqA2Hm7Aq92o/pGyGBzWfyso+jxcDeyZ+m3oG4SfjnxG JpLuq0ySgYF0y37yBrFKqZlRnnOnwBfN7CfWFRYfN42l5Mvk+tZ/HW9TSZaXYD2vdV6S zCUw== 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=QH4uPSpTvKnr7PyGO1Zr6sxjM8jWzbBQCXUlxXwVEfOTYV3fcdUl6ogKXisbwQNOSi 6FjQjyLLLWaQU9NNuHSkAwyskWUR3f1SSccquW1TquC9wsThfQ4GW/UVsi17zbdU5m4b qVSirXYeBi1Oo/pTZPERWiC4SpFPLc0PoatoINoNpjeLA89WYQquM2ZlrYgOYjyhggp+ KY1eBS9iOWyaf0VE0pHov59G6ihd0CgQQuWxU+PMqkPgOynTAXhglFfkyumeb1PcT/05 7MFhZaHYvMXg9KY4hxeVKoVZrBLf3CRR95UccqaSuiVGVOZvDPcSqoeHYJjuYY3YLo+m n/KA== X-Gm-Message-State: AOJu0Yw9AJYjZ+3j0fKsbxQ5R8rSecnbDrTvmoxo5CpQBkFXxtbHiCtJ S/bfmvy2my0ITUOheNmvYK/ocFT0qxs= 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_Sala?= =?utf-8?q?=C3=BCn?= " , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Stat-Signature: itkd1nc1afc68mes96ppz9gigergodfa X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: C7EF218001F X-Rspam-User: X-HE-Tag: 1698430948-135704 X-HE-Meta: U2FsdGVkX1+I6N37VAsM/gLQJEuTjc/748kxfEucn0GnQoR+74nEHIuLgG0y7YOpM+A0QDstIfsusLFVWrTpXBpHCTQlH1DQOZh4Re2Pcx3ozTvUuEKoaAJ684mz/+tWpLamAa7Nn6e43qx1C69QbtgDOQdTdrCmde/NPIexPimLvgikLiCHqmlnhmSqwRunqYNdi1gu20sRhrbVOup7YV67qUnnHgsgok/gy1ZOW+OnOpgWweXKRHhnX4CS5uUhZj+Sc2b4nI4i4QASujEQXEnvhGkD4pXyp+++VWCyhveFFCCMUoWQk+uF6yYIsEi03pQCtkzXeiBrHBLvluFDPcPMjd11k+91URav978WTN6OM2MMSaP61jYKmv44we/bBYTD99QVVlWOoJmVZKUqhgQQiNbabrhN/lSG8Qn0d1O77j3gxFVXUMO4lrEkih6WsKM5Hjxf1yeLmsbECTAryGayz16cOqeuuMsCZKy7rQTLeCdf1+V7Kh/Jov6sIS+eO5CwTpyb3bn82SmmBec8xSOM1gv19gzzN8JeAvC+e1AauslTkv4zOHbxBJpULG/GQ3Y2svw9Lv76MpYARSzn1vFcGBUhJdCYOl5E1DUL3fCCOKdAYAVylHVpzRTTwbh920arNWLv78GVgAJNqLm4ShUEqOJGZ1jmmSc1nq8as8FUpXuvG/0YvssbJPoAA4aQaUa2ZipD2q3jhQRgi4krZ3q7gQRxJ62Vji4EZH/chv26urmgUlNUkCD5YMEYy3LdCdG2vIrUPwOXf1ABmzlEuCdu689Ik7gWRnpkIzO5otSib+c+vIuUO13CwkCK+PdZJPXUbhmM2mW9UgFaebQKCW6/QnzIP2fOkSuIZwxqQJuNmF09eBej5TaDkAxySXe1pTYn1Cfy2DeIzKgVh1XI5QN4JlIVaiDVvGbyjR4LrVQ6Q7camu1inwHrIrXVWqo+5ij8uPQntaeDICHFbmO mDGv9W8R TXU7iZIScDtql0QCcbF1VWx9Gge7nsLajxbV112X7prqdTPDUoFxr4JZLgpStJjqnZ5knHuJEe2Kl5Q3CrPpoc+IYA8z2JQj8wB6s9MvY360cH0a3VFGxJDl0PCRoeJLo6E+cZAyCFiPO3n7HjPTSWAlGXrvhhTKQkjTdvyUtrRts1bjiL+Cgp1dPjaSO5dn2RDk59Dw9bsskzQAouicAhZSZhl/O42hI1kP1+eljpPC7FJqZ9rWvTiMls8v1SQeClcc/3IolUvIee6uNpKsTV+Rf3dHLwnQLOsXF86wPZecxr83vx6ls5btWk36706xtdzujr4DBYOwCTOvuI+oxLSb/m9B6Ksa8hqtWDRkzM9kT50lpxJp9EWjeEKOqFaJsRhqzCcBzpfIhwBV78ry7HbYJn8n8oS0d0Bqg5k81AfEQzx2B1/XKJHlBDayF/bCY9hLwoG2+1BPLy8Ft8x0E8yvoeHWw6++MBX/j2ZowUuc4nLGw+01AN9aWUBojTrmp7yGlkyaCvDqERvmJrigTic0q/f2teQuS29VJHm6TT/L/+oM6NK8IDoQn1LDWV5hHbL3nzEQHlfVui8WCRpFK1CfPZ6jn3LPMhjPQa7B0UQBOaFNjfQTXcW17CM21PgVcszW1cOwaZxY/fhiFabrGEYZiIRx56K2ZEv8UssYG/H6rEXxq9CW30GkS/wWdYTi1yTImG4PCr66mE3XGCYoPuUNqe2N7KKxtNW7jAbasbPOACG4lElNh8DJYxB+bxl17/rJKwP8bNh7eCqxH2VuaSIC/NPMBi6IGjW8mRdOq839N8atMhFm7zNo8/9CuGz4Mnai2TW4cV+/GRTyjKBgiNyq3+M2FmioRjWbAJ9wg/RSB/sY= 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: 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 Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- 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,