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, From patchwork Fri Oct 27 18:21:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438929 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 32417C27C48 for ; Fri, 27 Oct 2023 18:22:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F21F80009; Fri, 27 Oct 2023 14:22:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 95239900002; Fri, 27 Oct 2023 14:22:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77CDA80009; Fri, 27 Oct 2023 14:22:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 600F8900002 for ; Fri, 27 Oct 2023 14:22:32 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3A20D1609C0 for ; Fri, 27 Oct 2023 18:22:32 +0000 (UTC) X-FDA: 81392061744.01.1DFB724 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf09.hostedemail.com (Postfix) with ESMTP id 7910F140005 for ; Fri, 27 Oct 2023 18:22:30 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Hg1SIt2q; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 35f87ZQYKCAYykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=35f87ZQYKCAYykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430950; 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=Fn/2IBJw8IgyOIqoe5CTcghLJbfyq3WPHSyzyGJhcug=; b=Mdo9JMqSn4s3PUKoY9RLy1v7eve218utJkuEi+ufwic4xqUoXtV1XlSUCxdoDXKI9wItRM 7y1HwLo/OohwExTCuz4jm+WGKov244zY9sDHXkyjHOcLwG9Lz7oqjqaKFFFKzFe0LFzqf+ ZyqevW+RVYtVZdVaYGH5e8TUI4ZszBE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Hg1SIt2q; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 35f87ZQYKCAYykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=35f87ZQYKCAYykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430950; a=rsa-sha256; cv=none; b=DVH5zzHIrHtsHWsfEoxOHV28YdUdnEc5QXyfjFopviqLQ79vktMIpcSC8tjE6778Jp3+53 slxehIftqDFf5ABRkg/XqiLO84DD7bKL0ydpcZX0WrKSwN3LBWXLL1gfRzupHDhBHgn+GO FxFBdxBuGif7mbo17Jh5h+rbVP86HGo= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d81e9981ff4so1668505276.3 for ; Fri, 27 Oct 2023 11:22:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430949; x=1699035749; 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=Fn/2IBJw8IgyOIqoe5CTcghLJbfyq3WPHSyzyGJhcug=; b=Hg1SIt2qnkPj02Q5lxiZc/tmhOoM3qD5puaVTGciDl6zlSaKrRqYOQxUa5FI59cP27 8l/WILeq2E8v7ADb5cERxwReP7yL/R/i1efyjpnDaeNvYHbzatL3sPFUZtnnDclQ0KmI uK6SEafZfQ5xZPO+8Pis9dQ/8cPBAurmq9WZXrYFw3BEDXpaN9SmDgANpMctfOQYsSP5 8ZeASuYT0Syz86Z1Mbpi1XHFyevlu5IYax8VMZqMZYciJOhZ/gyYk0xGzOiJX0srgN74 0YhovBjmRA6gq2X/SvBJPR5/EN78Ik1/tP0vD3j3nkVnNAbcMPh5SCzFaPAkXI+HtOtI g8yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430949; x=1699035749; 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=Fn/2IBJw8IgyOIqoe5CTcghLJbfyq3WPHSyzyGJhcug=; b=kHwE9n5Ey95kyGgIgxszFQGjQQvT4Do3Fx11gvxeQ4O4H8h52GmL4tyR4zq5DksHL7 ANfijFQMyyJHneo+2+Cx4KAX0SJGAu54v++GoeHtd3D77qQGbfx3PdWeKsJzi31awJpL mVgOun/p+mylMjwX+PjO6YvW1xA3cJVMO4ji4ZJX6+aOwMnbxGD2CEux88qYkxbuLoV8 p48giGTYSlZ6LNSxIp7+bo5dH43wXbQtZ2oXm9tu0Psj+cS/Di3GU3orhFRlr7h2znK9 zEqPB7Agn3VIQW4Bkp4k7cpjqXt1oXrEbSc/+oP/Blu51XV1jahB+l07WC9Qhke1mhpV QxFw== X-Gm-Message-State: AOJu0Yy8dgd73RZaRKC0A3/ngc8ujlpgcxV3zYGHzz4OFAchWo+lJ/4Y JtX2w8wFNQB1cExWDbzJt5h7xY65Atw= X-Google-Smtp-Source: AGHT+IE/2A67BLmFvNwLQGxAeqfnMv4JVTEXFJ3uXVKssW9yJHFYWQUNgIG+FyGEOnQbWZWT52otjVeFu0A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1788:b0:da0:c9a5:b529 with SMTP id ca8-20020a056902178800b00da0c9a5b529mr57775ybb.12.1698430949628; Fri, 27 Oct 2023 11:22:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:44 -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-3-seanjc@google.com> Subject: [PATCH v13 02/35] KVM: Assert that mmu_invalidate_in_progress *never* goes negative 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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7910F140005 X-Stat-Signature: sef6gfqqisaj48urrnq1i9m57x7otuxx X-Rspam-User: X-HE-Tag: 1698430950-307720 X-HE-Meta: U2FsdGVkX1+H6G1uv+loof34O9jCHvVl5dVIicbNwes99Ax4+S9r+qBkCTSlNuteD2oCK4d8/KIXtBj2XjUGx6CgvAGcKvoGy/06kHjOMZUo2AY7Qe3nWL4/YVtk9WQmHnmG3Uqs12S6Ckokl/fPyoOV9nQRXoySj6+Nkl8AKwAo/OKKuNWw7I3E+aOf6Y4gJMIZG32icB3q7gzuguEInNKqS5r3wv5nXMA3OKAwty5IUfkRKLR9/LB4APmk0go8p0UOzNey6igqoCMgaNxYF+m9+TtWMPcq6BooBxC9NEHACYyvSh7X6mq3f+pLY5g6EDP5D/zaSQkIUupfoiPSNPVcGpy/7NF0ZTMsasEZj+QZj8Mze9LOVFYUwPdOIWg9PEPmpjejcV7bSf9z7HrY3CvSBRbBZfviq4WosyfPg+a3C5ZwjnJpKA/bXgO0UtHtU8OwSH35PMuIYiBvz3RNEQOkKGF48PPZDDIjUzv8jBnjwpo/Frmmr0h83XziAGe6K3/TBTC/YBtgZleKnjMVd9lTdj1naJgf4oRArsFSawzEA5E6N89kdwFBEgeBTfgidQhv/YwZ8ox8yI5xFN2pzjyGXMLGxRkca6y32x3SrrtGyNC7vZM+eT1UeVqIcFJTZGmVjMrQPKS67A/fzEZI+Ku97D5Q4QtnZqewpXkEUJS7NCbA3lIljkRUMoO8E8Dpaew7SoeoEkru0zIR530Xd6jUIKxYn7sBUblcB8Sm9/8QE83nUhk3pZFRG0OlzXps9B9ap671u1mSmyunns9BgZnCA7wm2XUcECjWBdzAHEFVjHwJdFAHRx5xxVU5pM5Tu7Yrxy8fp/dkjSmFXwYWjUE9xqa6VcYQQ29gMk0NBM9TUqbo8ILSZ7pkRXoK8nqgDk+6Bc9dnEjtjTsIunX4TRQzDUDUtUdGndMCl0aG86AymLqhyk2Zx0q5+rW8v4qj6wPH7gQ5IX88Q0vb5H4 bRzixHpt TSypGis66zZSMFsCZb8veR1W+tpjtmwiSN10T5L2Kk7qARs6oudWNqj9+nVZVxuu4OPkSnLsxH09tzRq20vnA2gtbhs6rCY0L+1ziZPAik6zYE1tn9b6h9i8hWlfB4ITm+kMhEd0e74tzsPsa4G9Sup6K2dJgWUdjEAAMwN73XczKc/Y8QzjJXq5v6wRli6MqjxgI3M+di6B7G9oHlScKrotQmAh0lNYF/UeXzMk5khVHUewioxv7sKMTYEvwwFKzReOypWz7CyW6Kt0O/tDhbJ7TajKitaEhv5ivBlJmiW+AzWXpiHDOKgDjELw3BsrB/UTvB9mhQGr2+3qHafcfVAeEbFjiaQkH7eobnO1Ec5dyAr8Fu8+S4CznqWPgxMoWniOIN/JeOKNnk4sXoWWAd+G9b0pD92+L19qcvT6ftZHezahoxSUFDgWfloUW5gWagRmqAjkzViV/ww/LT8FT0oq0yyN7ykmxqJPLF+8XhqSWIevfjmtEfJgzFfSfsJCVH7p5cQlbZJkra0m2cmlGv2YWEvoWWCacZ0CaAwUpuzx+gfVQs5ZVN52QnMQOjQcrB3Koonw1Edurk8vCTKk32w8p1rXiEOp0pSjAE7Gku4nTWcLo7DpyKDZ3Ezf5j6VXD+Ncdk4HKSZq8yiQ3PKrCKMUAcBiAlmEFLcuk5I+5LLPFThhzVwBWsxjIQwmB1g6Sd0NVa7lXcya6mgu56oUlAiQfZR3K8qFesTtP4/lB8oczU9HJi23wi8Cl0Zlx2mIhX9ut+UUCg0VFM9EPa/WpkLFdOzNje/jN77q8JjLq6gjgaIOayGRSlFNtA== 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: Move the assertion on the in-progress invalidation count from the primary MMU's notifier path to KVM's common notification path, i.e. assert that the count doesn't go negative even when the invalidation is coming from KVM itself. Opportunistically convert the assertion to a KVM_BUG_ON(), i.e. kill only the affected VM, not the entire kernel. A corrupted count is fatal to the VM, e.g. the non-zero (negative) count will cause mmu_invalidate_retry() to block any and all attempts to install new mappings. But it's far from guaranteed that an end() without a start() is fatal or even problematic to anything other than the target VM, e.g. the underlying bug could simply be a duplicate call to end(). And it's much more likely that a missed invalidation, i.e. a potential use-after-free, would manifest as no notification whatsoever, not an end() without a start(). Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- virt/kvm/kvm_main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0524933856d4..5a97e6c7d9c2 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -833,6 +833,7 @@ void kvm_mmu_invalidate_end(struct kvm *kvm, unsigned long start, * in conjunction with the smp_rmb in mmu_invalidate_retry(). */ kvm->mmu_invalidate_in_progress--; + KVM_BUG_ON(kvm->mmu_invalidate_in_progress < 0, kvm); } static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, @@ -863,8 +864,6 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, */ if (wake) rcuwait_wake_up(&kvm->mn_memslots_update_rcuwait); - - BUG_ON(kvm->mmu_invalidate_in_progress < 0); } static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn, From patchwork Fri Oct 27 18:21:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438930 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 5CC3AC25B6F for ; Fri, 27 Oct 2023 18:22:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0EF18000A; Fri, 27 Oct 2023 14:22:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9664900002; Fri, 27 Oct 2023 14:22:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BC1FF8000A; Fri, 27 Oct 2023 14:22:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A4220900002 for ; Fri, 27 Oct 2023 14:22:34 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7D22D140B02 for ; Fri, 27 Oct 2023 18:22:34 +0000 (UTC) X-FDA: 81392061828.12.3253DCE Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf25.hostedemail.com (Postfix) with ESMTP id 9704EA0004 for ; Fri, 27 Oct 2023 18:22:32 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=YHXXoRVw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 35_87ZQYKCAg0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=35_87ZQYKCAg0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430952; 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=0ecI9u+NU7x+hIBqOXm0c4fKiWu/1/+75d/SeCcbF3U=; b=3kZM7tZhYpUF1NFPtdXiOAftKOKA77/5WFNjNgsfWvbWPkAPHgEVuf7SVs6mDYtT+BZgAR iAxG9kD34F/1p1GG+TMZ7VZlbJjkwT1JFAyt6yUSbaqdPzBJyx3kLgtDNVGoBVAtHCjF26 phrDaffM2NEgwNgdR4P0WAQ37zsWWj8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=YHXXoRVw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 35_87ZQYKCAg0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=35_87ZQYKCAg0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430952; a=rsa-sha256; cv=none; b=ileP1ubFkWifESprH6UYIzT0qgrX8ooUwxVRGY1y69Wf2Rbcd+OBiqZ3djc1T27Efz7k9s 3z2TKUEfonoebizbOxKMkUzJzSqcwWIVgZF0H/WLe78YQK26Ng+NFsagCGoQVYlw44fTKH sh16YhipKzSegoTmnj9D9Rzk/g7o2qA= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5a7b53a48e5so20845747b3.1 for ; Fri, 27 Oct 2023 11:22:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430951; x=1699035751; 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=0ecI9u+NU7x+hIBqOXm0c4fKiWu/1/+75d/SeCcbF3U=; b=YHXXoRVw2ttKo+YISB5qiv1Ls0vwOs/wlUw3bcBwlmqup5LgOFwayyvWcdML/fTx8P CLik0E8gtyjd6bkgqKsavz40dvlsoS2pEjDJODy69XcslBHhQNHDoQuwOjygAjoTylv5 yLtWJsUhyDmWo6Ml0SU5WIdxJXoWGPQDDKES5ZPlOPJ0fpVvbhYKctpuSWRMGhGsQEWn 5KIF8+kxkxzl6tqzf5Q+8rBCsEWTZxet9Oztnv1VLfvW4nDGNrcIdXgaQZPzGGiKvuEK TG4vo9/mNxaEqpgcTU9rnMYZSOJ4rlwecXRdoMz0U1vB7pjQ3uEpY9F+fjrLp4N0Rtrj 2BNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430951; x=1699035751; 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=0ecI9u+NU7x+hIBqOXm0c4fKiWu/1/+75d/SeCcbF3U=; b=o3rr7j3heTGyRmafwGPywz7Arx35RrhJTp28STLklzxngdarewrmcZP0mAXsEFma2z Fvr2M2gLDD5Xkxs7piilNDpppMMPfGSc+Ze2Ov4eH7WWwJ6eW1qrDnldm+snLUVUOwnP +B82wA6wPfXKuVnFkDQ0D/59Jyx4sXob0orl5aautB9vjGAtbDNyJjFCHLma/ZKi5YKJ zxDfzFnClOSx57sXBOe2Yq39C2yMgiZyOiLrFwlm8SQgsGJu6VdpjzEvAnejgVX+kGDx PcF5UdFgDwGk5Gfrxl+xF4uzrWBrb9nP0RwTzXF9sKI8VIQahsZ2EFVKnVV6CKpSLleV 2s8A== X-Gm-Message-State: AOJu0YyYqxKM8DHiC4zn559cETSgsjEIABH63sv+f/VZttA3xdHg+E44 hHwhurop1cml1Yk2JlQk+s5Tvr3LDxw= X-Google-Smtp-Source: AGHT+IEVX0PyerXEbWK1UOBgKc7BZmLynzYtP989xU7q9rVAiqIPcpbzc0WQAlmV6uCIwfdxpJ3o/vng1sw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:4858:0:b0:5a7:be3f:159f with SMTP id v85-20020a814858000000b005a7be3f159fmr70939ywa.5.1698430951577; Fri, 27 Oct 2023 11:22:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:45 -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-4-seanjc@google.com> Subject: [PATCH v13 03/35] KVM: Use gfn instead of hva for mmu_notifier_retry 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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 9704EA0004 X-Stat-Signature: amnx5goqbohzqruru5m4ks5b5g1e5boq X-Rspam-User: X-HE-Tag: 1698430952-657721 X-HE-Meta: U2FsdGVkX1+sD3U4OSZzxMjFOAvcLeu2NzU6GwlsC4Lmmofk2XBP9phtKAkfYcWUG06txeBIMrjxPj/AqhYNkvuknKtT28ENW1Vyi5VcsXKEF53QnF0hqMW6zh9OlrnLcyBFARQhcg61Ux12e7ZDZxHPxxKwsTIoVkeYkNhHrZ4q2ssJqrGMOvHJ2tkWRQiiVBu/uDLwmmAzzkLPfEsM3KmSwpUdHDaCcr/7HPKb+YGbVMG+GIct3rNX7zzp90fXzRz1SlJWzP4peoEBNiCU4GSiKBgq/ehGIdESUWth69Jli0Uk0fRcDN6LaOZEHsReBbKCxSuwDul3NrYuEQK7x2eYGJaAet+JT5xMixSw6IEtGNjmFH8pwEfFOQEtobExQikGLJUABjxBLnr8fMdtNiDYfhfVC6qcUOpBzvkVgKr33ob3uXtBctDa9p1sRKFwrnTSd/g3ZwFLWwgyST0utgoqvvKMzEjx+p3kLhFmuC2nNGXyUiybwhd7YkmdM2PQCtJVFIH5//jk45FheOyUVIWZRyzb3GRXLbLla/w6Os/Bwouzgd4NZhqE7FCCWdM5se7CXU7mDrjnaYKmc0Ui+26o4ifARJtsK1sWITynz6RftFsMARFb9cJh+IIWMeFd04V8Y6Dii8yIrpV9L6rzGRRhbzJ//NavBRBGEhkgjDPA4y/03CmthVJw5KZSf1hp0147U5JReFAIZkv4vWb3jQVHzkXmHBFkMpAoxHpdq5xMqlhB2w3wH2y0ygBSwfLJcMgVPitBa7wWvFCgw973pwYvDQsNhRn3Io1LQpexzwhxRxRMBdQKGcMlP1q1LG5sQBbhnNaYfjbY8qKsBMIoAcx1iUr6RFadgdLiKwTNOKrPFEnOw191EqKpb53y28aSjG+UX5YsskXN/UXLftUDrfeTQB1o/9BFLAf1CgUnrgC2TVlpMPxY09QybGKCs6xpXG7aMRb+4z31KyrMiLv LP7EtCpF LXiFKdNg+/ETIEXFFbXSSrlik30Y4VDOaA9b5MVIBN6+lT/1QyMw+YPuFx9k4WTFKgF/Wl58pD6a1zvGag0tnQkf4ylKKPcYiASnh8+1bliZacbPv3F2MfO6i4HAC99X2Pv6uoaKBVScZ1N2u3ftoiWZDW+TLCtajWUgxVUSq05Y0NqU2Hg77WCmq2opGmnyw8tHZVbo6l+nd1As4smlXueHQS2hCJD6jRsvGukuVjbM9ZhzJh1vIp5PXQqRGQ5sqt8PZkcf9oyYHfavYiResfNEQ3F16wLuJvxu9G0ZoXviPDtIlQGJgUQw+m5lrW7UVNm+pDs+ggPThuG//leaVEk0DIY1zQeGr1lIyHr6dcOsAyeevv5SY2Gx+KBD4AZp/FgLIXodO1CBEZ2gppKxq8GXi3/5XFX9EzVvsaAQ29rYC+eKvdifIIkXnH9MigSlRtDRXhdwXFe2ITNtoLAMy3g2DrRDuJ9FW4DsOcpN+7N3drd7Ir40KtYur6PJQUA+ttwsFeHAABs82gepyRGmFRc+pw30snogS8sn6r+lEn2SO0uUb/8LqUUK2jxq5OnyQXehAetAboC5TTgrYxrSdV52EJxoCkqFRGro5GJrgPiPKiUfxoQqDWykJzQGF5L2IhTeLI1hsWzcIiGN104O6DJhl7dNvTug/Pl8t1fyhFqtlBc2D01N+/gZsFTQMXxJ/LafRviDhrbjC0hqhwHdmGfhtpyM6fWoPdtUkLi5xAh2mFAjIwRcdVRuQqVADBBKOPr+8J9DkCtdjvbcIJdHWnYWGXKYTmWC79uO+0baO9AIkq2HUpWSq9uUN7gCZU0+uXWmyEqV9UWohA3C28yA607Mjgg== 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: From: Chao Peng Currently in mmu_notifier invalidate path, hva range is recorded and then checked against by mmu_notifier_retry_hva() in the page fault handling path. However, for the to be introduced private memory, a page fault may not have a hva associated, checking gfn(gpa) makes more sense. For existing hva based shared memory, gfn is expected to also work. The only downside is when aliasing multiple gfns to a single hva, the current algorithm of checking multiple ranges could result in a much larger range being rejected. Such aliasing should be uncommon, so the impact is expected small. Suggested-by: Sean Christopherson Cc: Xu Yilun Signed-off-by: Chao Peng Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba [sean: convert vmx_set_apic_access_page_addr() to gfn-based API] Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 10 ++++++---- arch/x86/kvm/vmx/vmx.c | 11 +++++------ include/linux/kvm_host.h | 33 ++++++++++++++++++++------------ virt/kvm/kvm_main.c | 41 +++++++++++++++++++++++++++++++--------- 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index f7901cb4d2fa..d33657d61d80 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3056,7 +3056,7 @@ static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep) * * There are several ways to safely use this helper: * - * - Check mmu_invalidate_retry_hva() after grabbing the mapping level, before + * - Check mmu_invalidate_retry_gfn() after grabbing the mapping level, before * consuming it. In this case, mmu_lock doesn't need to be held during the * lookup, but it does need to be held while checking the MMU notifier. * @@ -4358,7 +4358,7 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu, return true; return fault->slot && - mmu_invalidate_retry_hva(vcpu->kvm, fault->mmu_seq, fault->hva); + mmu_invalidate_retry_gfn(vcpu->kvm, fault->mmu_seq, fault->gfn); } static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) @@ -6245,7 +6245,9 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) write_lock(&kvm->mmu_lock); - kvm_mmu_invalidate_begin(kvm, 0, -1ul); + kvm_mmu_invalidate_begin(kvm); + + kvm_mmu_invalidate_range_add(kvm, gfn_start, gfn_end); flush = kvm_rmap_zap_gfn_range(kvm, gfn_start, gfn_end); @@ -6255,7 +6257,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) if (flush) kvm_flush_remote_tlbs_range(kvm, gfn_start, gfn_end - gfn_start); - kvm_mmu_invalidate_end(kvm, 0, -1ul); + kvm_mmu_invalidate_end(kvm); write_unlock(&kvm->mmu_lock); } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 72e3943f3693..6e502ba93141 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6757,10 +6757,10 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) return; /* - * Grab the memslot so that the hva lookup for the mmu_notifier retry - * is guaranteed to use the same memslot as the pfn lookup, i.e. rely - * on the pfn lookup's validation of the memslot to ensure a valid hva - * is used for the retry check. + * Explicitly grab the memslot using KVM's internal slot ID to ensure + * KVM doesn't unintentionally grab a userspace memslot. It _should_ + * be impossible for userspace to create a memslot for the APIC when + * APICv is enabled, but paranoia won't hurt in this case. */ slot = id_to_memslot(slots, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT); if (!slot || slot->flags & KVM_MEMSLOT_INVALID) @@ -6785,8 +6785,7 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) return; read_lock(&vcpu->kvm->mmu_lock); - if (mmu_invalidate_retry_hva(kvm, mmu_seq, - gfn_to_hva_memslot(slot, gfn))) { + if (mmu_invalidate_retry_gfn(kvm, mmu_seq, gfn)) { kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); read_unlock(&vcpu->kvm->mmu_lock); goto out; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index fb6c6109fdca..11d091688346 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -787,8 +787,8 @@ struct kvm { struct mmu_notifier mmu_notifier; unsigned long mmu_invalidate_seq; long mmu_invalidate_in_progress; - unsigned long mmu_invalidate_range_start; - unsigned long mmu_invalidate_range_end; + gfn_t mmu_invalidate_range_start; + gfn_t mmu_invalidate_range_end; #endif struct list_head devices; u64 manual_dirty_log_protect; @@ -1392,10 +1392,9 @@ void kvm_mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc); void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc); #endif -void kvm_mmu_invalidate_begin(struct kvm *kvm, unsigned long start, - unsigned long end); -void kvm_mmu_invalidate_end(struct kvm *kvm, unsigned long start, - unsigned long end); +void kvm_mmu_invalidate_begin(struct kvm *kvm); +void kvm_mmu_invalidate_range_add(struct kvm *kvm, gfn_t start, gfn_t end); +void kvm_mmu_invalidate_end(struct kvm *kvm); long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg); @@ -1970,9 +1969,9 @@ static inline int mmu_invalidate_retry(struct kvm *kvm, unsigned long mmu_seq) return 0; } -static inline int mmu_invalidate_retry_hva(struct kvm *kvm, +static inline int mmu_invalidate_retry_gfn(struct kvm *kvm, unsigned long mmu_seq, - unsigned long hva) + gfn_t gfn) { lockdep_assert_held(&kvm->mmu_lock); /* @@ -1981,10 +1980,20 @@ static inline int mmu_invalidate_retry_hva(struct kvm *kvm, * that might be being invalidated. Note that it may include some false * positives, due to shortcuts when handing concurrent invalidations. */ - if (unlikely(kvm->mmu_invalidate_in_progress) && - hva >= kvm->mmu_invalidate_range_start && - hva < kvm->mmu_invalidate_range_end) - return 1; + if (unlikely(kvm->mmu_invalidate_in_progress)) { + /* + * Dropping mmu_lock after bumping mmu_invalidate_in_progress + * but before updating the range is a KVM bug. + */ + if (WARN_ON_ONCE(kvm->mmu_invalidate_range_start == INVALID_GPA || + kvm->mmu_invalidate_range_end == INVALID_GPA)) + return 1; + + if (gfn >= kvm->mmu_invalidate_range_start && + gfn < kvm->mmu_invalidate_range_end) + return 1; + } + if (kvm->mmu_invalidate_seq != mmu_seq) return 1; return 0; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 5a97e6c7d9c2..1a577a25de47 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -543,9 +543,7 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) 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_lock_fn_t)(struct kvm *kvm); typedef void (*on_unlock_fn_t)(struct kvm *kvm); struct kvm_mmu_notifier_range { @@ -637,7 +635,8 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, locked = true; KVM_MMU_LOCK(kvm); if (!IS_KVM_NULL_FN(range->on_lock)) - range->on_lock(kvm, range->start, range->end); + range->on_lock(kvm); + if (IS_KVM_NULL_FN(range->handler)) break; } @@ -742,16 +741,29 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, kvm_handle_hva_range(mn, address, address + 1, arg, kvm_change_spte_gfn); } -void kvm_mmu_invalidate_begin(struct kvm *kvm, unsigned long start, - unsigned long end) +void kvm_mmu_invalidate_begin(struct kvm *kvm) { + lockdep_assert_held_write(&kvm->mmu_lock); /* * The count increase must become visible at unlock time as no * spte can be established without taking the mmu_lock and * count is also read inside the mmu_lock critical section. */ kvm->mmu_invalidate_in_progress++; + if (likely(kvm->mmu_invalidate_in_progress == 1)) { + kvm->mmu_invalidate_range_start = INVALID_GPA; + kvm->mmu_invalidate_range_end = INVALID_GPA; + } +} + +void kvm_mmu_invalidate_range_add(struct kvm *kvm, gfn_t start, gfn_t end) +{ + lockdep_assert_held_write(&kvm->mmu_lock); + + WARN_ON_ONCE(!kvm->mmu_invalidate_in_progress); + + if (likely(kvm->mmu_invalidate_range_start == INVALID_GPA)) { kvm->mmu_invalidate_range_start = start; kvm->mmu_invalidate_range_end = end; } else { @@ -771,6 +783,12 @@ void kvm_mmu_invalidate_begin(struct kvm *kvm, unsigned long start, } } +static bool kvm_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) +{ + kvm_mmu_invalidate_range_add(kvm, range->start, range->end); + return kvm_unmap_gfn_range(kvm, range); +} + static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) { @@ -778,7 +796,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct kvm_mmu_notifier_range hva_range = { .start = range->start, .end = range->end, - .handler = kvm_unmap_gfn_range, + .handler = kvm_mmu_unmap_gfn_range, .on_lock = kvm_mmu_invalidate_begin, .on_unlock = kvm_arch_guest_memory_reclaimed, .flush_on_ret = true, @@ -817,8 +835,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, return 0; } -void kvm_mmu_invalidate_end(struct kvm *kvm, unsigned long start, - unsigned long end) +void kvm_mmu_invalidate_end(struct kvm *kvm) { /* * This sequence increase will notify the kvm page fault that @@ -834,6 +851,12 @@ void kvm_mmu_invalidate_end(struct kvm *kvm, unsigned long start, */ kvm->mmu_invalidate_in_progress--; KVM_BUG_ON(kvm->mmu_invalidate_in_progress < 0, kvm); + + /* + * Assert that at least one range was added between start() and end(). + * Not adding a range isn't fatal, but it is a KVM bug. + */ + WARN_ON_ONCE(kvm->mmu_invalidate_range_start == INVALID_GPA); } static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, From patchwork Fri Oct 27 18:21:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438931 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 24FA3C25B70 for ; Fri, 27 Oct 2023 18:22:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE8A68000B; Fri, 27 Oct 2023 14:22:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9784900002; Fri, 27 Oct 2023 14:22:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B74898000B; Fri, 27 Oct 2023 14:22:36 -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 9F058900002 for ; Fri, 27 Oct 2023 14:22:36 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 78904A09D1 for ; Fri, 27 Oct 2023 18:22:36 +0000 (UTC) X-FDA: 81392061912.23.7BC16AD Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 9796F100013 for ; Fri, 27 Oct 2023 18:22:34 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=w5+VIBQE; spf=pass (imf14.hostedemail.com: domain of 36f87ZQYKCAo2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=36f87ZQYKCAo2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@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=1698430954; 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=QjG+iTAl+MysAoCDX0shLrWGCG7e9aCGlo9+qs2tVnE=; b=z8/ipqdNABnooU1oXL557bhvG85dvcw7IO+eWqPcbIyp/tKqvxWXTZnRqPQ73K7FB9LDiD ci4e4Qlf9/07bW1kmMzWnenOSu8oPs4WhE+W765GfW51J+SGGr/VWKnw6SgnE3y77nW/iw 41iV8dtXoOmKGp356nSX2aS80/0b1sU= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=w5+VIBQE; spf=pass (imf14.hostedemail.com: domain of 36f87ZQYKCAo2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=36f87ZQYKCAo2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430954; a=rsa-sha256; cv=none; b=q3cf7C07bDIhZls3pNCdnqSXH66C0vOHGnKTuhdHhyyspjJFV/qGIVlc46AGn84S4hbm1+ RLfz89zkSX5MPm1t11jIE9pLURc/VOTT+eq8FV4ZdPHbMYcbY+0mdMorvbdsFywteZQ8Ub UCt5u/AJH4U4nKYryUjGBzKftAwheL0= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5af9ad9341fso10343367b3.2 for ; Fri, 27 Oct 2023 11:22:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430953; x=1699035753; 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=QjG+iTAl+MysAoCDX0shLrWGCG7e9aCGlo9+qs2tVnE=; b=w5+VIBQE9Tx1WO0HRye876yZc9hKO+piTxbvaOVlYmrB/Kz0GuwWN5FwR3He10TnCa PqKjXU2KvxK8zUEO1i2DACiOoABF4AyM9Uv/kn6k0D3rKdYnuIk198p3bVJr15KSTzEI rj6YH5UPB6xRfFtKt5jGlNdwft/qzuWVJH3nd8DIf8JnuNNhmki7i74ize11q2B1dH9H X00wP+bN42ljEoy37DhcGsbYQPalG10tuol+XO4Djw9Q6jIYT3K9EcrTP12G3V+pJQ0j ltS5l6+Az4V7n3r5Ugncc3zKhYFJYoJkFpK6oqhYzpBeA2fidc+Zmj3UumMcv2omYnnM Zbgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430953; x=1699035753; 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=QjG+iTAl+MysAoCDX0shLrWGCG7e9aCGlo9+qs2tVnE=; b=d+FnabI4ApbTBGl65r1TYcCf6R3A0nBGePL5xIfO25I73k7nGOBEF1yf/E2k0j6Ylc 12NYQvahMvRX1So4IwtrVoSI909ZXmlH+tmSnoR5xSBwof58LwP0hfhdQN3lxH5gyyB1 hgzWuLd7GG+Rx+ixqFTcCdbrmMsFeRQh0v8pP6f4EsJ2XDzQRjDsqKGARq7YscXchKH3 09R+X3EzR7xilXv4Tl1zSOcKuZHZUS5XnkOOCWtLWJnECJ4lIVoWgQ7getcnJ6Q7YQ08 BRDgc1tev43oCgZqlnCPR+6dRUY7Gd/1ksgqUKLvd1bOJtS813KZysEt3C98dj1cuOnL GlkQ== X-Gm-Message-State: AOJu0Yzt3K+e7wApL9DiZImCT8FquktD4mhaaRAnxERmfRmSPdwmPrgb 2oqOFOV/87gkM/MIbdmGSt8PazrLXno= X-Google-Smtp-Source: AGHT+IFjdgmBdyxBQ0zr6Phdzdc3NhfRHFKE/+Wea1IZiVOT7T4IBQHOPkTpo2hU6PU86u42a7hH74NJYwo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:da0:567d:f819 with SMTP id v3-20020a056902108300b00da0567df819mr78680ybu.10.1698430953673; Fri, 27 Oct 2023 11:22:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:46 -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-5-seanjc@google.com> Subject: [PATCH v13 04/35] KVM: WARN if there are dangling MMU invalidations at VM destruction 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-Rspamd-Queue-Id: 9796F100013 X-Rspam-User: X-Stat-Signature: rgo833qnknqwc6gu98d7byu7rm6yd39o X-Rspamd-Server: rspam01 X-HE-Tag: 1698430954-719981 X-HE-Meta: U2FsdGVkX19+pmmzoWm0A2SnrOGzC7DrU6I5oZyGA3jo6KcUO8oBl+13ej2DSNe1026JMLIwfV2sQt5N9ebws1xtSfXj6f6zHzPNnx8XjOCjvGXbhDHFwTo+5kXtVLfOD6G2GVYWdmlRNE9vXORF/EKfE28ijG66Dhg5rRSrji5eAcKcH5NS2QDfS9CVM07cc6K2hhbUHjTR4cFc1bpaR0FwlbB5jJd5XCCYrEJ1plVJYQt+S9x9sT3VMHqXeWOnPfYhWm9B8sBJSAeQAuT8mruUh67F7TxrNTMW472V9vtzXyu3CjSkOn/XfOoajFNxSDUFQrhciQTB5tjpfSzWFS+yg0Mg4kJ2oyVL7tvGjzMS8JX9Vn+GRGzaPU8OYeCxdBqe3lyh3r0oILLpN+XTGgFog8xlrlyrokQ9pbJjBoSpSadiKkAwd30kr+XeOZrBquUooj9aviaRAu3g8gWofVU+exlWERNnBKuAMxX1hq1bRdMXEtXgzpVydZbjHEbADU7Xtvc4ar17Zotd2TP+N9t0JCrKpOqoelEt58wlbUNMZpMydJuKvnEFpiPU2A44T3BKo0QdyMgMHTBmAvmCkeookhOMx/ctyx7FNSN7Dpwdm+xxfsT/i/HVhghLW5V4IJhUgbZBKbgRlzB9rMIM/xu3Razg/zvVn9pM2x8x1wHvATf0WE2r/88LzECEpT4OyO20KpUfBo7UL0KYxlznz4dD7LOrM37yB46fUd0yM+ySGlUPIIKUqNrRguHscs16WXNzmOrVInyBGiXXFzpFV3eLxKrlplJXsMDHsb6nRU2fuE2PRU2XAzGuHECCFAe2Dp8TDGr+kF06ScZn3ot/JlSG2tPdUFpQywAgcoVaqI2BDBeY8/vtz66T954wlCIfI7cUEdWt4t1rKwDMyAMcv/Vpr/g+7fXMJHzJnUAS2WWtaeMPpeDTWL1EKNSjCRDTzmlvsMIOm4ad+TgzpAD k41dkiiP m5CFwHcsauAemF5fh4bJ305vQ7/VJ9mNNf6SSYSjIRANdtiA6CPCSmMDgUWXtR5V/9vVaUl/cg1Hit6hRNmHgged3t+EDbb5nZcCnOpr1G+lbiBU3UPOCBke/jpQybZiiGP2MrGWCA1C76A8QXGy0YgxWndAIjvybLaLJajZG8PoLlLvgQgpBP80csC6sezFfgoFl/aXI/e9ld9NfAMzLwX++g0AKOkZmced8em55v1ul8HeM5njXnmdHO2MiWxlXSe/ZL/chq6/fSChEYJmHJerDlFv6l64HyxBteHGs3q1A+CqXG23MvdMaT4KbU6st4ZPrqrsxj/rgljMNg7UU9chmHAQYfSstDJEBpCdOQQkelKvo+5edQlD1bdXrJ8WYU5zsk6kAkAuhqUYp7T+4lQ2Z+AJ89BnbBWD+CMf4ishHQkEo2ldRo6nBOjU4OgmLrE9sph0NYNFX2W37xp6UU5PXmy4WwdS5rzQfA2Bc9Lax45BdfzalXSZmw/zhuWkMzpzxNrgrG64nBwz+fA2ixz3ypc5daF13fzplBgu8+6q4zg3/mvdLhP74bJuBaYlFZPZIC/6v8Y9p3qb/7TCvbNZMyZZIyFL3850bSGA4H/emy5pJjVb0nl4tSy21QxHGp/oPJPXP71HsVPf2OqF9h5XiqjWMQKnf85fsxonb30Mv7uWsTtDKWY9WJZSHFbbD6e9HgZzAKMvS70MX4tCRJIzLc3ph6W1YIXZp1ov4oqDnYFOCvH2aQoP9u1PAsUFGElI/MSILFmsKmGL8vPU56TawfJzQfq6NIw2uBHa2OX/1zH6C1oS6+SFk9TTcKILcwsIR7coIFkkusQPVU9IzcK86u9lGwIT2ZRl0 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: Add an assertion that there are no in-progress MMU invalidations when a VM is being destroyed, with the exception of the scenario where KVM unregisters its MMU notifier between an .invalidate_range_start() call and the corresponding .invalidate_range_end(). KVM can't detect unpaired calls from the mmu_notifier due to the above exception waiver, but the assertion can detect KVM bugs, e.g. such as the bug that *almost* escaped initial guest_memfd development. Link: https://lore.kernel.org/all/e397d30c-c6af-e68f-d18e-b4e3739c5389@linux.intel.com Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- virt/kvm/kvm_main.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1a577a25de47..4dba682586ee 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1356,9 +1356,16 @@ static void kvm_destroy_vm(struct kvm *kvm) * No threads can be waiting in kvm_swap_active_memslots() as the * last reference on KVM has been dropped, but freeing * memslots would deadlock without this manual intervention. + * + * If the count isn't unbalanced, i.e. KVM did NOT unregister its MMU + * notifier between a start() and end(), then there shouldn't be any + * in-progress invalidations. */ WARN_ON(rcuwait_active(&kvm->mn_memslots_update_rcuwait)); - kvm->mn_active_invalidate_count = 0; + if (kvm->mn_active_invalidate_count) + kvm->mn_active_invalidate_count = 0; + else + WARN_ON(kvm->mmu_invalidate_in_progress); #else kvm_flush_shadow_all(kvm); #endif From patchwork Fri Oct 27 18:21:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438932 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 C2983C25B47 for ; Fri, 27 Oct 2023 18:22:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 18BE0900002; Fri, 27 Oct 2023 14:22:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C7238000C; Fri, 27 Oct 2023 14:22:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E5C2D900005; Fri, 27 Oct 2023 14:22:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C82D1900002 for ; Fri, 27 Oct 2023 14:22:38 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 90B6FA09DB for ; Fri, 27 Oct 2023 18:22:38 +0000 (UTC) X-FDA: 81392061996.05.1FB50C7 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf18.hostedemail.com (Postfix) with ESMTP id C3F951C001C for ; Fri, 27 Oct 2023 18:22:36 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mj4pTWxs; spf=pass (imf18.hostedemail.com: domain of 36_87ZQYKCAw4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=36_87ZQYKCAw4qmzvos00sxq.o0yxuz69-yyw7mow.03s@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=1698430956; 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=JgU06SXMv6nm3bYLetCIh2Atkw0AeiFtLr3TWBcGDvg=; b=D/56PpGG17rz7Mu+UjAGAzYfeRtFavgFO/81v6y2hEN4H5589bt8zm7BU9+pgU0GvhWNNr FQkQHzuOpjOQwudNTTQslp0FJgibiPCMCUj0eA+u8BMnRWQcAF0lkFuaq9EgZ+awZWq03C N6XnWV+Qq/PenlUQB3lK95BVW3BmnrM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mj4pTWxs; spf=pass (imf18.hostedemail.com: domain of 36_87ZQYKCAw4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=36_87ZQYKCAw4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430956; a=rsa-sha256; cv=none; b=ep8NiKFz+SV+0OPURdS4fkl5Va9BbGXYvaTIuiyR1hOkkVyCQVwpVkK5v2Uq+V42ddV1S3 RgL3YcONa98P1B+aDZiw18RSkhIuwbMddRWg+DGIqRVTrIlzolJ+vgrfKypfp6Jn2ASAgH Pl3XrjzRDScl1nnz2s5FZpPJ0gsezUc= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1c9da175faaso20616475ad.1 for ; Fri, 27 Oct 2023 11:22:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430955; x=1699035755; 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=JgU06SXMv6nm3bYLetCIh2Atkw0AeiFtLr3TWBcGDvg=; b=mj4pTWxsnJfBn3XSIdKCJWLBadKNzHpQ3r0oSrTv5GTQTjJPYZLwhqtSj+pZY/42Hp uvsPr6MKoLa+O7DFx0QZuzUPGO4VPB9DiL87RrHOQtKzYwrJ/+szFh5Qlkwv795JN16S N6wc5a+HrKxuyb6AwsGivhSg0fRa8AVn6Vy0RMd4lGjGB7I6pwXnpCqo1G+u0O05FOif G/UeitQq7oStTvnSAPIavmP3t81Jev/umQq3eNG8IyI2HpEtfpggB6qH8PcYifMJ/wCz pgMcdJ6Gp0O7Lli/ObBUEKbV1MZ5vAuOVWXO6uHn3CQyUg5y0EjnCe/ApMGcZUhf9C14 PTWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430955; x=1699035755; 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=JgU06SXMv6nm3bYLetCIh2Atkw0AeiFtLr3TWBcGDvg=; b=nc/WO3DXQCe25qY+tiAswMGb1rxIOncb4HbaXWPGmZgz8PXnwadqBkARSNVs5sRy0Q +Q38gHmN41HFNgAG6rfXVlZFospnL4IyoNLk0iblq7tmDJ7y9ZzjDQ+PyPhkCFGeb+GE gfhWrLPAQVo8s5HFpVN//Q2X/pghTyUT98KJjsuHTjb+lw0HFSV+r6h/5hVqag8PNFCg omycbaLnH/W0c1KojmYkOke9uPqO3qoStkzQ7UNwq/kwdaYj01P9UmPp79AFupGot7/3 b3PFNnjcTs8gsvl3La1wu0EBqRERNATUVzbdMKfpvM1FGbLgTzxDzg385NERU4dhDngE OOCw== X-Gm-Message-State: AOJu0Yy18qqG3Kws3RFGeBn+6Uxo69f4iPf8TO1er+kTkAx35J7eOBCj Fkfxh3X+v+OD75vnMXnMmLeXuIbYFrI= X-Google-Smtp-Source: AGHT+IGtvUvxtvF0vLnhcz0wuwQy/3Gylfgr5sP9pToido+ZyPcnNi9qskOerQDKma1HuMTn89KXKiciq2s= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c144:b0:1cc:281a:8463 with SMTP id 4-20020a170902c14400b001cc281a8463mr32525plj.7.1698430955693; Fri, 27 Oct 2023 11:22:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:47 -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-6-seanjc@google.com> Subject: [PATCH v13 05/35] KVM: PPC: Drop dead code related to KVM_ARCH_WANT_MMU_NOTIFIER 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-Rspamd-Queue-Id: C3F951C001C X-Rspam-User: X-Stat-Signature: rpocj1u5kzydn5irf1h797ss77xo5j8a X-Rspamd-Server: rspam01 X-HE-Tag: 1698430956-199235 X-HE-Meta: U2FsdGVkX18NWZR4vjXrtprbM/MqnSl+LyYQ+2hEIT6DKi2JZgLEMrwMy50ORucTHgDLDG4ktgdxjCz5RmKiLrzi0Si/hsN9vWy3SRsFAi0/I5ivvIiP35Fqin8JwHfIHUFxgDfKcug8xKNSDLr5c23QsdAC8WhxmoBqByxxebTXOzLnyflKEyirNtLk1v0IRPWpZSiDkSQTT1GYsiZ+1RywaYmtqCTQhaJd7WaBEILxGk9RaQyLojOxiMu7p1BK9ojcNLUq3LR0O++/gc8uRjPayOTSfhwAKNXQygTrnk7ABeShDYixQoFkO6cBe70A99gbB+JMt9jVA6xBnpFFYe3ol3tXaobVdaBd+maEfAAVi30bdrVdm7OuacIOxCba/chF1+gnW8ji/kOHHPYuLitHzHkr5BKG8kjYjukrqXC/FSJKIC1xfb0g6mWxvD2ytDQvyUMjmoUzeYSwlAV8ctgBINYEidCT1pVnyoYAbeycFfcITQVQ0cEDo67cw28Ndl7URS8o5HzvbYMLmAAF6iF+C/DLuE98QLaZrTJoYytwlDmGywfp6c15wDX+Fr6wggFCryB8uflnY7wwbxvRdJi1Ox4xiXwXcJrRvPGjqm2hZEWXls3O4lPc44QSW4vfmDfMYiMWhoOGbEOMevsfNpoTqKekjQ7hFOH/mVD14yfDpz/wVJQ2S1bdOUD5sFU8myDF1HxEntlB/auz5ebvWcf3Ejeb8NConmeskyZorp5imXMJTW/6XyATrub6YILxuacyqk9XV8hJPIlclSNaVQ2A6ZgcXswBAQmRgp3diwB32xOhmKkuKjz8jd6VRSxD1RXB/8JqHrKmIpM7gBDHgI/ggGvVvAg11w4ZkqUeuakQMfgZHK4ei7SEl+yXpUsQoD2ToGtXIAGJZ2pYLNXHmsB6vSyC8hCvrmdWtLgOzsw03G2mtonxSAVgCQGDuPdppGokjJq79TYLiPYcLCq fUMrQU63 p/qNiYtZGlna/qZ1FmBKLq4WLgoFfOsclHsE4/Q/2NWL0Ndha5a2+LBsN99ZZ1mIAf8zfdunMACnnTyMqVeWubxBc4uJ1/2SqsOeFE9p2TLRfamWdivTlZ9bBILxLfwTHXVzzNijbqcEBeiJMRKsfTEfJqktCxPzH/bk4DEtdowKWGbHywAXmyR3jo0Az43b9tpGLjv2rz4X+CDuIupCGXGQhODRvYoOCPSLAvAQehtLbGkSG78x+NQKZBiiHHlkabqE0bZRK/f7gRsFaKgp/abjTcq1M9xH6ykepcl0MtIdD3DpEDgpKZmYXTZgyyON35I/SkxG55uY1yMmixjW8n//g5S0NjTUffTqk2T3SlB/HpM81YB4kujiSudtjY5ExXH+qkWLozg+2tE8eB3oul51n/MBv5LZ0eRn2TkzXUUloiU9Gw4EgYSwdgv/cfKgaxHB035Gt0l6LdZlorYJGIn1FtcN0kCuatPlQIRUFyNiytG6Kr+ZB1Uu9Lal5UiDUm93OTN+n3RLtlP10yYly7z/K77JnBqT5b9iPav0MvY1poXj6rkVWBmAIqXWHLkfVpVemF/31OonFxWhUj4V4hB3WfjmrmqX5g7sckQLv1JQwevOYzj+4Cdzopm2kDQ3qIkNveFWkd1K55oUPpdMvz+4bZknuNSU4hEIcWdHxlGf95fkNa78nZfRKItMz41F5jkSYPS4fRi8zIQJX6+0qlzCW3MZRpPDz2ZxOLtTiVTVMYF6qW83JrePmmSccO68cisrOZ677wNeaWykm9+EwjH4EOemkgVS7BZCjxkeznj4VImq8EgShEyyrQtVVMZWUg7yg 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: Assert that both KVM_ARCH_WANT_MMU_NOTIFIER and CONFIG_MMU_NOTIFIER are defined when KVM is enabled, and return '1' unconditionally for the CONFIG_KVM_BOOK3S_HV_POSSIBLE=n path. All flavors of PPC support for KVM select MMU_NOTIFIER, and KVM_ARCH_WANT_MMU_NOTIFIER is unconditionally defined by arch/powerpc/include/asm/kvm_host.h. Effectively dropping use of KVM_ARCH_WANT_MMU_NOTIFIER will simplify a future cleanup to turn KVM_ARCH_WANT_MMU_NOTIFIER into a Kconfig, i.e. will allow combining all of the #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) checks into a single #ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER without having to worry about PPC's "bare" usage of KVM_ARCH_WANT_MMU_NOTIFIER. Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba --- arch/powerpc/kvm/powerpc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 7197c8256668..b0a512ede764 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -632,12 +632,13 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) break; #endif case KVM_CAP_SYNC_MMU: +#if !defined(CONFIG_MMU_NOTIFIER) || !defined(KVM_ARCH_WANT_MMU_NOTIFIER) + BUILD_BUG(); +#endif #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE r = hv_enabled; -#elif defined(KVM_ARCH_WANT_MMU_NOTIFIER) - r = 1; #else - r = 0; + r = 1; #endif break; #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE From patchwork Fri Oct 27 18:21:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438933 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 5600BC25B48 for ; Fri, 27 Oct 2023 18:22:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D98CD8000D; Fri, 27 Oct 2023 14:22:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D23BD8000C; Fri, 27 Oct 2023 14:22:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4CD48000D; Fri, 27 Oct 2023 14:22:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9D0428000C for ; Fri, 27 Oct 2023 14:22:40 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 78671B5F1D for ; Fri, 27 Oct 2023 18:22:40 +0000 (UTC) X-FDA: 81392062080.19.B53CF6C Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf18.hostedemail.com (Postfix) with ESMTP id 84E471C0011 for ; Fri, 27 Oct 2023 18:22:38 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=grrMPnbo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 37f87ZQYKCA46so1xqu22uzs.q20zw18B-00y9oqy.25u@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=37f87ZQYKCA46so1xqu22uzs.q20zw18B-00y9oqy.25u@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430958; 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=srqr8yeDZlFrvxNrVECKE5xxuDvV2cb3J0BOKhehVs4=; b=zD2bU1ev4p/JeJO9ci2BOpZqdoOsts9Gi9C1CEqLSwH+pznEFkm0zqIwkc9KUk5T2pdCjO YOyC8I63sWu/SpuLlfmM0B48lORPDfMsh8+bKIYdJq8hSpkhf0jDFY/ox/n7Myr9HG7Zc5 DV8wJAzZDLoVQ5Xq+YpYTVqGGW/8RE0= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=grrMPnbo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 37f87ZQYKCA46so1xqu22uzs.q20zw18B-00y9oqy.25u@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=37f87ZQYKCA46so1xqu22uzs.q20zw18B-00y9oqy.25u@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430958; a=rsa-sha256; cv=none; b=zHOil4nJGjlTn0lF3Y3sDsdopWK+pTbzCwCSqWpV6dYGhOx6TgJXQo3NSo4ns9VbSxpzly qfMw1OwEFOUIJn2bK4zxjDLVmuqJ2vNqEvS4jwe4GFVJlhvFx8azMGAwfvFRuw6ywVzBHS uCVTFxD3CnWxv3e0Ko9XdN8zhyUnOzE= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1caaaa873efso23349735ad.3 for ; Fri, 27 Oct 2023 11:22:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430957; x=1699035757; 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=srqr8yeDZlFrvxNrVECKE5xxuDvV2cb3J0BOKhehVs4=; b=grrMPnbo3nildZ36x6XR8dn2TV44a56Pyy9vPXcIszH33K/KWlKs0WePTH+//j60ZZ b03VLcO0iJUhnT7MSrGQrCKCIne4DVVJpnGJ5wQymDsQ3GjI3hiYGV6m61cQRKdlFnAT hlPfM0TYc8xEg8I55EFImQoKL7xnLoBcu4Zmf1cA5uTj5XEFZJMicE47Xru91LzcDtJG 1nR7PzFyeBqONLcwymZP3skG49x6lrYT35sA3GGV43D4zX1j2OkS2hQSaLfVLdyfJ0o0 wVKoCV0SFlpPf94esJhVasojXzx/zlIj/qoe3LTAxrwBZh6HFlgPbaSl03zGSw/uMk3O 8Mgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430957; x=1699035757; 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=srqr8yeDZlFrvxNrVECKE5xxuDvV2cb3J0BOKhehVs4=; b=cpkBKdmWvemH6w0lh76iunukx6Zjj/pMFUJU42jZC+3eXHmkuj7BaqFe4c1oLWhQKB xyqy/riMU3qKMJ2RYGZW4VzWtSFB5DkxJmzbNFp0Xr40i5y8Dx3W2Ye31FMEx8rOzYiq 0fmuuyo0YiVo9z7aZBHhqSWAzHtasrPTPLIYQ3NHgflniasltFkLBKZt2wETJ89x+BrI SOZPa8ekD8bpyXNIVhSyGb61hZQHw6tTIz8IEha1WhiOQeYl/W4YIxYIFC3925A5AKnv ANBD7lY5x5fpV1iroHdv+xSb0vWRXUGt3N907nuXPLRDLCixu1o8Qj1MBF70y1+NW0Rl lSjg== X-Gm-Message-State: AOJu0YxSbno+4nqWZfMmNvHwQP12jwjm7Aev44AXfr6IU0sqvIRcyQ30 STrQRp7Mcorhd35/CPiRJwjSTzJLiHo= X-Google-Smtp-Source: AGHT+IEgJpE7SXSukSyCmrL3MrLSyvY8980C5XITIQh/pYBdmOorR/VvmfyRxJnTS7i/xC9oBoneiM7iOr4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:446:b0:1ca:2620:78ad with SMTP id iw6-20020a170903044600b001ca262078admr60815plb.8.1698430957451; Fri, 27 Oct 2023 11:22:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:48 -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-7-seanjc@google.com> Subject: [PATCH v13 06/35] KVM: PPC: Return '1' unconditionally for KVM_CAP_SYNC_MMU 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 84E471C0011 X-Stat-Signature: mxuyrsfwdtdggj5qfhrnemuscmmry4u3 X-HE-Tag: 1698430958-768877 X-HE-Meta: U2FsdGVkX18HUxIEdSZUBDZzrRD8BndaniKXkaIvfKUsAR68CW07Qd1hzOziNVwOPpMJ5yXltwRWidnheM6vSkYZB2tb5u8NkdGc92GOmlc2w2iyceUlv24d1cchzNMSnDwG3tFyAuQVEyHiUP+ay7Q+Ox8ajSFniOjFNLr9uwTOhgEbGk7RA44BBQa5t6Xv8ROPmlJkbpTMryFh0WXho1Wh6j4riXKPAGkgP0PRXRk62pXKBC6FshEFBbsrTIldqhxt/9ixIbt1mSF58vfFZnKWpYbYhfRmj9nbfzfiuEgZUu+HERZNOcL+8NWOQ5mxt7xI13fhyigGVVzU3MFXUuG3RkbNS44VYlmqyrKGuMw+nCDlotY8YO4R1cQOUoDa2IEQBOUkg8jTRB6V1l7JlvJs6jvlEq1qM+U0cCSbfgx5z9lJKMd2hn2+QdXSFHBMf8OeXuHdjmXMwQIUeAqwzBOpvpm/sOhJlL7OK4qlPDPcWdKqwr0MGaDX16OY322+6hHXJ97rBQzTOlF4xpRL/WZ+qWhl63+X203ikgqk9ZNkB049FxFBtkLjV16PJUcV+hVS8h7+0t7p4iMO7u+ZwOEotOQRI2hj4gv8i8Y1w2KoITO168ReyLAwj8eOaBF3rIdL6DIfGs8lsonPVl5Ml8xHviU8tnaQDPSycf09K0y2QNXO4TpuqfXxTwGQGzKfeH9nmy4AMgRDA20DB9tqrFujzSN1fRrGi1bHwdeDzG8sRUB1Awc5q4kXtI8HsS9Kd/ecN+UySXZNIMQ0RtD5137AUE+S9NfThEIO4W4bKRV7UgXurwrw8Isvs520YhinFyj6yZmzxpe2b9pI6i3xQqopyI9UgC+LliQJ39eHcVoSBPibPhNjkq3B+XbqPNLwlzxhOegizkADWrSbg9/5d7uxx0087LLRbYRLxfE8TjfRl6TwPL+p/QoitYG3sbLVofec9URue7hdNejAsDC nu4zlfGb 5mRkaKe1EW31Wie8GvxvkxIOQiS7OBReGdfcUHyjipUW8DxetJXI2iPmzGsoSTeccATMP6JIgShuTjvg3z2uNVKl1ISe0JQW413XbzkoToeKsdvAwaoZ2y8evnM6p2T6m7sHK33/BYiHxNJPxS4+j2V/uMP4IsuR8PjqHDyY/gbvGVOGdUTig/ZbcM+0TWcATKfaL33G1ppBXnWb5RJu72Qr6l6YqziuukrsVvCdplzD9ojdywPwwJfUiODwn+v+Vlnv0wdqkAQ7PJ5ML1M84jQmhtrjHq9TQi9IZWbxWYDJkgpFuUmpZ/mpSCwn6JkpJt8Mt/KPsiuOHCWsyxNiCSLRbIa5+H+Pqd68PscHwPzz+o3DbzmfIEiXeQjhvVS9z884N/tpJzBJ2uNsfOJkBJIA2A8fEDTpRwgcvJjYvGeA/MVkYOS5ex7dtPGggZr9GChlLHzlr1bBXQDvaUVQ1hA7MTBqWqmM/0yS6WG7GztIIktkWkQA6NqHlPu4BUZkWfu/KC9Do5CF8wuAqhGQs0ej/c9FOKtSI7kcn5XoGUoj/pD0/e5hnufjHrWWYgNX6/oMmxOGQSc2qCDezWYpEY6sGco4FtAAVvGr1xK7CWfk7tOHCwWfxu3ympuj/sYAxTQLwMZrg40JtiXgl6XdnbgmUl60zC1Dqh6A28X2btZnaZmsQfbbGM56lmWi+J/Au95FRNhsEaFZmO/LAIb0O8fx6DoMhS0Dyrfcce9ngn9SbGT3AqC2DRqirhDWH9PPgl33pfOtgZE8Px78SAsIEe62RJQMn4WFOtrlgM4euQ8I3sTNRt24VQvHaHHIqbkq4ZczmtwFWcH4LJ2IOOvj0V5ERJQ== 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: Advertise that KVM's MMU is synchronized with the primary MMU for all flavors of PPC KVM support, i.e. advertise that the MMU is synchronized when CONFIG_KVM_BOOK3S_HV_POSSIBLE=y but the VM is not using hypervisor mode (a.k.a. PR VMs). PR VMs, via kvm_unmap_gfn_range_pr(), do the right thing for mmu_notifier invalidation events, and more tellingly, KVM returns '1' for KVM_CAP_SYNC_MMU when CONFIG_KVM_BOOK3S_HV_POSSIBLE=n and CONFIG_KVM_BOOK3S_PR_POSSIBLE=y, i.e. KVM already advertises a synchronized MMU for PR VMs, just not when CONFIG_KVM_BOOK3S_HV_POSSIBLE=y. Suggested-by: Paolo Bonzini Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/powerpc.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index b0a512ede764..8d3ec483bc2b 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -635,11 +635,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) #if !defined(CONFIG_MMU_NOTIFIER) || !defined(KVM_ARCH_WANT_MMU_NOTIFIER) BUILD_BUG(); #endif -#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE - r = hv_enabled; -#else r = 1; -#endif break; #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE case KVM_CAP_PPC_HTAB_FD: From patchwork Fri Oct 27 18:21:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438934 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 9F5AEC25B6F for ; Fri, 27 Oct 2023 18:22:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BEB368000E; Fri, 27 Oct 2023 14:22:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B75068000C; Fri, 27 Oct 2023 14:22:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97B498000E; Fri, 27 Oct 2023 14:22:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 782588000C for ; Fri, 27 Oct 2023 14:22:42 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4824CC0965 for ; Fri, 27 Oct 2023 18:22:42 +0000 (UTC) X-FDA: 81392062164.29.F2CA07D Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf03.hostedemail.com (Postfix) with ESMTP id 71B7220007 for ; Fri, 27 Oct 2023 18:22:40 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rws9A0lU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 37_87ZQYKCBA8uq3zsw44w1u.s421y3AD-220Bqs0.47w@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=37_87ZQYKCBA8uq3zsw44w1u.s421y3AD-220Bqs0.47w@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430960; 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=+AXFJbz5WZr/NYjDBv52nUkuFXaO3S1StF6qZQtTbwQ=; b=CjiyAJvcf8ih8v/XtsgwbWcOqtUP8IWZi+qXEtPDgYJBpj/rUneyrBbDI8T6gEfpyQ+z5U 9IC4iK3PHLdx+neHTRgEJhF4UVJAIaCKtJh+zPuWvjenWfD/OXVOymkRml0BiQoxZwbS8l xxf3g4YH0y+rObVvdJlYpM1Ptr2MLXk= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rws9A0lU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 37_87ZQYKCBA8uq3zsw44w1u.s421y3AD-220Bqs0.47w@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=37_87ZQYKCBA8uq3zsw44w1u.s421y3AD-220Bqs0.47w@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430960; a=rsa-sha256; cv=none; b=MLnPEQlHzjaagLS7RCucGXCnsbasMbluYPh3pv18iCUtQJqnp3J/YZ/f2Heznir2+WbJL8 9asXzLI6+dOZiWr5od9MRVeXCKUSnC5O9BIOct4dsye0jAcBpwnphZZI7pSfzoNttb5Ro+ IvnIfq7wAUix8uhks6jLcESZyj1LY0Q= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1cc281f1214so4815205ad.2 for ; Fri, 27 Oct 2023 11:22:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430959; x=1699035759; 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=+AXFJbz5WZr/NYjDBv52nUkuFXaO3S1StF6qZQtTbwQ=; b=rws9A0lUEzBnxecgk5QMwVx9xkF5eb1khsKlvWFD+fNiSTvwBY9tbeOiYQgoMIh1wG Fh89EtyfpVA8shNHfJQgtdi0sqW0H9QyFLquWUkSQcDOL8LlbqFD5wahMkF/0XvE5HlL 31IaqlNLDgvTOReUZ2DOHUxZxSnHsmXunlwHQpkK5v35EfiKr2BNkMN69AMNBt7ljuX7 v/4hl0ygJa9FB1YkB7j4G1rQfajd4qrPEv7Jbbv9MIuG4H4ATMiyAwR8oSU12yu0nqd8 E6URMnAg72gtgxPdVLL9yTy6YS8kjTqMdgQMhfvDoqaFk6hyc08KqBsigxgTBtPMel9g WmBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430959; x=1699035759; 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=+AXFJbz5WZr/NYjDBv52nUkuFXaO3S1StF6qZQtTbwQ=; b=bXlwnRYk9/9cr7rY8sEkNB+YUmU9Ysg2UY9ImMskd6Uzlm5XhtzJbGVmzBhTOucDv3 0rf20P4RuHvNjjKbuIdnc0/2WOxEWOQY8gAWZCIc9CBE+/NGRNHBghmV3osTObDVgXDy csddbOdGvrAe/doSKeM/ppo88ULFVMOx5DpSMIPXqavGBfR1GIhp4XU1LbxUxz/6kNhs U3CScv9Lsg4UoA+Rl0aUejc8vbfjGdI5rjuPU36WUj8Ask40hg+rJtTEPnt3FDp/UIrz BAQx+5xMMjtN2IjfDDLWwHQvnsob8j13J2GooXHx/M8OumE5mh4o7AcmAdxmeoJlHcpn ygBw== X-Gm-Message-State: AOJu0YzUj5oxA8VEJ7d3OLKqKyq/L1KmpYNKSdVje6GXEaUr6/ZjJRDf OJpxd1Mh0YNPVVeYFuEEOmm4xZ16VeQ= X-Google-Smtp-Source: AGHT+IEo9repSBGZOxXVda3T/byTyJ1q5UTkkyqwmlpHdlfrn0lBxj3W/oLyvthWFNznN6u5r+uymjEP5Uc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c255:b0:1c3:29c4:c4e8 with SMTP id 21-20020a170902c25500b001c329c4c4e8mr57345plg.4.1698430959411; Fri, 27 Oct 2023 11:22:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:49 -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-8-seanjc@google.com> Subject: [PATCH v13 07/35] KVM: Convert KVM_ARCH_WANT_MMU_NOTIFIER to CONFIG_KVM_GENERIC_MMU_NOTIFIER 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-Rspamd-Queue-Id: 71B7220007 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 8ys3x5wz3eg5iob48pxmcg8afra43tfu X-HE-Tag: 1698430960-45809 X-HE-Meta: U2FsdGVkX19JMbNaAu6D2OHkv9E4Qj0jBsT+0bCxXe3bwOE0y5UHVAARMrlFtyxXr24Kj6rfrVW8RKlr63+1tbp+nGcxl1O9wOrvyj9j3nPUEw7lA8W+c07/nmsQsU4wOhq3s8YWzrRgXL3adHYoU2t9EcG/d3xwKZqDyqBVszy5C2BYIBBez8abeeV9qjKlpikYLG7mUYAB3Hqg4Q5K+MvzBpQjrAsHzOja0yToCjpyr8EckGouNsMzuQPwNHOju0cYjdopZntr6JEIxN7g4ovVw9HAJxhqtCAFGjTTDNg+NqTVzXHtx9q7fZUc8jrhJZB2VXgY2Wg37f4WhN6f1wSZTd/YXDPBBGpT4+x8G46IUodB6xXYaBmHLbmv34NdjJ2lnnMmtes2ao8pNMp75qORffV9kCa/s/IZ0l04n88Cxv50WkBlAhGg5hDwhC+HdwfWq5ZWtOSy+LueDuQh5zSanowzq0pF9UyaK1yBiNgzTEvwbzTeTeca7LjDKNXy9JszUqQvSWH/kv1mqi9fzXeopCUsYhSMKS41J4fk8wpja2XY4L9DTXKS1d7ghdKAc7OMI7F3dDlOBbQ/jmEPWjblR6YiFFYTqffRvX37Z8wenBV7elEjygZV/VwcQ8A+7DRMKjNuUmC4jNCNXLu6yIqjHl3416gpF+9wyRoNyf0HlpG55lT86XkTfbpwA6hQOZntLAs4Fq/1DjAwlOG3kB3yQcg1BV0vhWlkAgT1Oed9+SsyvegvjM9uW3LJ1ERbUtL5mzzYsee2aibUg2Tavo7gIS254SPPIdIiaXmhnBmQVTFRkCOM+xhoMXZLXoNm2aN7Y8GeOYWrJ4Y9qpQN4r4VBBHd7aFuYo4t21fkXK6Ggb4HcZfi77WKRnFqE60CndjxprwOY7fqlUZ2aoHwiiy9UJvQ5E+X69Bx6/nYOnKF4orX4UOBqQWwU4IcyclLTB7m+VbcO7RbYeVM2p4 PbUUdTK8 CPKPRvDyWTa8H74tr5ErjiW+/OByv0ZRSsJRHHD/1MPMqF+BokiJBZSHctluaaShIBrn2L47hMPgQl/VKtMaBtU/YM0MJUA/UkS2hB6WIf4Olz1SnE7cALb6Pbfv/lGXH0MEWoHEykJyBhLW7kNkBvknhILUBVP3/VrwGxPiI5x5PyVmzvrfsd1VJWpAjP0cdRGCRae/me52fs7AqLDESAqhrNheCiNi46iqZSiitKneukRbB1Jc6BKIfckuH4QL+wGziIZmHekWoyVOYr0Myu4zRep7f9hfSSMcbeO9QadKN7zP1JjU1gp/jsMsaL7N+ytV3DXbgfQqZKXP/8UJhbLXuuYx7KA6AjcMzDI3O/g5lghvanUX9h4Y9EUuYYZ3+ORrWFC2OIZYHTopbgF9W2hBCZY01+XUyPdhb0XjfaoqVXA5Q6+tvCXLY1OEOsaE+Ihmie7qkH6uji3KZ0+d0cM9sLe0MEh8ExBoQOwAMkpxDGdSAKjIqBXRBPRubvhGj3Sxw8taKuqxZo/5QYJhvY4Zd1RCqUIj9ByfR/Ip2ugf7AnsEuqWumdjBqfzQYp9xJjB+dy9htIqbYy/nojMEsoF+Kj+UtaPZ7+fNPgNFxjviiJ/tM7OPowEgqbO7XmcgzqkOAi0wqY/Z0fCPj6FAm+0ZSyAAJHIoQD9F/ipvZAkFLhRyQcIJAXHBcJ45YNdfk6IQVgc5PLgk8oODtfGPOzBKbDPulv1SuK7b6VTMjo2KudQbFy84edZa0rIf6e4rLTBIKRP4s/MN7Yyjvjy07sxQihGE1aDLq5PPzE54HoAZXmFuldXydpqx/BUPRsm2z6Atgrf31BgP2GzX+bbO1JSJJVo0nV0IbE8XFFln9uDhyU3vTqbMy0lkb3sO6KCOrNnYTaorRmKiSkV1GKFz88eRLA== 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: Convert KVM_ARCH_WANT_MMU_NOTIFIER into a Kconfig and select it where appropriate to effectively maintain existing behavior. Using a proper Kconfig will simplify building more functionality on top of KVM's mmu_notifier infrastructure. Add a forward declaration of kvm_gfn_range to kvm_types.h so that including arch/powerpc/include/asm/kvm_ppc.h's with CONFIG_KVM=n doesn't generate warnings due to kvm_gfn_range being undeclared. PPC defines hooks for PR vs. HV without guarding them via #ifdeffery, e.g. bool (*unmap_gfn_range)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*test_age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*set_spte_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); Alternatively, PPC could forward declare kvm_gfn_range, but there's no good reason not to define it in common KVM. Acked-by: Anup Patel Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- arch/arm64/include/asm/kvm_host.h | 2 -- arch/arm64/kvm/Kconfig | 2 +- arch/mips/include/asm/kvm_host.h | 2 -- arch/mips/kvm/Kconfig | 2 +- arch/powerpc/include/asm/kvm_host.h | 2 -- arch/powerpc/kvm/Kconfig | 8 ++++---- arch/powerpc/kvm/powerpc.c | 4 +--- arch/riscv/include/asm/kvm_host.h | 2 -- arch/riscv/kvm/Kconfig | 2 +- arch/x86/include/asm/kvm_host.h | 2 -- arch/x86/kvm/Kconfig | 2 +- include/linux/kvm_host.h | 6 +++--- include/linux/kvm_types.h | 1 + virt/kvm/Kconfig | 4 ++++ virt/kvm/kvm_main.c | 10 +++++----- 15 files changed, 22 insertions(+), 29 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index af06ccb7ee34..9e046b64847a 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -921,8 +921,6 @@ int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, struct kvm_vcpu_events *events); -#define KVM_ARCH_WANT_MMU_NOTIFIER - void kvm_arm_halt_guest(struct kvm *kvm); void kvm_arm_resume_guest(struct kvm *kvm); diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 83c1e09be42e..1a777715199f 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -22,7 +22,7 @@ menuconfig KVM bool "Kernel-based Virtual Machine (KVM) support" depends on HAVE_KVM select KVM_GENERIC_HARDWARE_ENABLING - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select PREEMPT_NOTIFIERS select HAVE_KVM_CPU_RELAX_INTERCEPT select KVM_MMIO diff --git a/arch/mips/include/asm/kvm_host.h b/arch/mips/include/asm/kvm_host.h index 54a85f1d4f2c..179f320cc231 100644 --- a/arch/mips/include/asm/kvm_host.h +++ b/arch/mips/include/asm/kvm_host.h @@ -810,8 +810,6 @@ int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn); pgd_t *kvm_pgd_alloc(void); void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu); -#define KVM_ARCH_WANT_MMU_NOTIFIER - /* Emulation */ enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause); int kvm_get_badinstr(u32 *opc, struct kvm_vcpu *vcpu, u32 *out); diff --git a/arch/mips/kvm/Kconfig b/arch/mips/kvm/Kconfig index a8cdba75f98d..c04987d2ed2e 100644 --- a/arch/mips/kvm/Kconfig +++ b/arch/mips/kvm/Kconfig @@ -25,7 +25,7 @@ config KVM select HAVE_KVM_EVENTFD select HAVE_KVM_VCPU_ASYNC_IOCTL select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select INTERVAL_TREE select KVM_GENERIC_HARDWARE_ENABLING help diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h index 14ee0dece853..4b5c3f2acf78 100644 --- a/arch/powerpc/include/asm/kvm_host.h +++ b/arch/powerpc/include/asm/kvm_host.h @@ -62,8 +62,6 @@ #include -#define KVM_ARCH_WANT_MMU_NOTIFIER - #define HPTEG_CACHE_NUM (1 << 15) #define HPTEG_HASH_BITS_PTE 13 #define HPTEG_HASH_BITS_PTE_LONG 12 diff --git a/arch/powerpc/kvm/Kconfig b/arch/powerpc/kvm/Kconfig index 902611954200..b33358ee6424 100644 --- a/arch/powerpc/kvm/Kconfig +++ b/arch/powerpc/kvm/Kconfig @@ -42,7 +42,7 @@ config KVM_BOOK3S_64_HANDLER config KVM_BOOK3S_PR_POSSIBLE bool select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER config KVM_BOOK3S_HV_POSSIBLE bool @@ -85,7 +85,7 @@ config KVM_BOOK3S_64_HV tristate "KVM for POWER7 and later using hypervisor mode in host" depends on KVM_BOOK3S_64 && PPC_POWERNV select KVM_BOOK3S_HV_POSSIBLE - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select CMA help Support running unmodified book3s_64 guest kernels in @@ -194,7 +194,7 @@ config KVM_E500V2 depends on !CONTEXT_TRACKING_USER select KVM select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER help Support running unmodified E500 guest kernels in virtual machines on E500v2 host processors. @@ -211,7 +211,7 @@ config KVM_E500MC select KVM select KVM_MMIO select KVM_BOOKE_HV - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER help Support running unmodified E500MC/E5500/E6500 guest kernels in virtual machines on E500MC/E5500/E6500 host processors. diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 8d3ec483bc2b..aac75c98a956 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -632,9 +632,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) break; #endif case KVM_CAP_SYNC_MMU: -#if !defined(CONFIG_MMU_NOTIFIER) || !defined(KVM_ARCH_WANT_MMU_NOTIFIER) - BUILD_BUG(); -#endif + BUILD_BUG_ON(!IS_ENABLED(CONFIG_KVM_GENERIC_MMU_NOTIFIER)); r = 1; break; #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 1ebf20dfbaa6..66ee9ff483e9 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -249,8 +249,6 @@ struct kvm_vcpu_arch { static inline void kvm_arch_sync_events(struct kvm *kvm) {} static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {} -#define KVM_ARCH_WANT_MMU_NOTIFIER - #define KVM_RISCV_GSTAGE_TLB_MIN_ORDER 12 void kvm_riscv_local_hfence_gvma_vmid_gpa(unsigned long vmid, diff --git a/arch/riscv/kvm/Kconfig b/arch/riscv/kvm/Kconfig index dfc237d7875b..ae2e05f050ec 100644 --- a/arch/riscv/kvm/Kconfig +++ b/arch/riscv/kvm/Kconfig @@ -30,7 +30,7 @@ config KVM select KVM_GENERIC_HARDWARE_ENABLING select KVM_MMIO select KVM_XFER_TO_GUEST_WORK - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select PREEMPT_NOTIFIERS help Support hosting virtualized guest machines. diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 70d139406bc8..31e84668014e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2129,8 +2129,6 @@ enum { # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, 0) #endif -#define KVM_ARCH_WANT_MMU_NOTIFIER - int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v); int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu); int kvm_cpu_has_extint(struct kvm_vcpu *v); diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index ed90f148140d..091b74599c22 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -24,7 +24,7 @@ config KVM depends on HIGH_RES_TIMERS depends on X86_LOCAL_APIC select PREEMPT_NOTIFIERS - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select HAVE_KVM_IRQCHIP select HAVE_KVM_PFNCACHE select HAVE_KVM_IRQFD diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 11d091688346..5faba69403ac 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -253,7 +253,7 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); #endif -#ifdef KVM_ARCH_WANT_MMU_NOTIFIER +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER union kvm_mmu_notifier_arg { pte_t pte; }; @@ -783,7 +783,7 @@ struct kvm { struct hlist_head irq_ack_notifier_list; #endif -#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER struct mmu_notifier mmu_notifier; unsigned long mmu_invalidate_seq; long mmu_invalidate_in_progress; @@ -1946,7 +1946,7 @@ extern const struct _kvm_stats_desc kvm_vm_stats_desc[]; extern const struct kvm_stats_header kvm_vcpu_stats_header; extern const struct _kvm_stats_desc kvm_vcpu_stats_desc[]; -#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER static inline int mmu_invalidate_retry(struct kvm *kvm, unsigned long mmu_seq) { if (unlikely(kvm->mmu_invalidate_in_progress)) diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index 6f4737d5046a..9d1f7835d8c1 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -6,6 +6,7 @@ struct kvm; struct kvm_async_pf; struct kvm_device_ops; +struct kvm_gfn_range; struct kvm_interrupt; struct kvm_irq_routing_table; struct kvm_memory_slot; diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 484d0873061c..ecae2914c97e 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -92,3 +92,7 @@ config HAVE_KVM_PM_NOTIFIER config KVM_GENERIC_HARDWARE_ENABLING bool + +config KVM_GENERIC_MMU_NOTIFIER + select MMU_NOTIFIER + bool diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4dba682586ee..6e708017064d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -535,7 +535,7 @@ void kvm_destroy_vcpus(struct kvm *kvm) } EXPORT_SYMBOL_GPL(kvm_destroy_vcpus); -#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) { return container_of(mn, struct kvm, mmu_notifier); @@ -960,14 +960,14 @@ static int kvm_init_mmu_notifier(struct kvm *kvm) return mmu_notifier_register(&kvm->mmu_notifier, current->mm); } -#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */ +#else /* !CONFIG_KVM_GENERIC_MMU_NOTIFIER */ static int kvm_init_mmu_notifier(struct kvm *kvm) { return 0; } -#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ +#endif /* CONFIG_KVM_GENERIC_MMU_NOTIFIER */ #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER static int kvm_pm_notifier_call(struct notifier_block *bl, @@ -1287,7 +1287,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) out_err_no_debugfs: kvm_coalesced_mmio_free(kvm); out_no_coalesced_mmio: -#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER if (kvm->mmu_notifier.ops) mmu_notifier_unregister(&kvm->mmu_notifier, current->mm); #endif @@ -1347,7 +1347,7 @@ static void kvm_destroy_vm(struct kvm *kvm) kvm->buses[i] = NULL; } kvm_coalesced_mmio_free(kvm); -#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); /* * At this point, pending calls to invalidate_range_start() From patchwork Fri Oct 27 18:21:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438935 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 3B5A0C25B47 for ; Fri, 27 Oct 2023 18:22:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8D5C8000F; Fri, 27 Oct 2023 14:22:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 99F398000C; Fri, 27 Oct 2023 14:22:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A64D8000F; Fri, 27 Oct 2023 14:22:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5FB1B8000C for ; Fri, 27 Oct 2023 14:22:44 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3E9E1B591C for ; Fri, 27 Oct 2023 18:22:44 +0000 (UTC) X-FDA: 81392062248.12.138E6B7 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf22.hostedemail.com (Postfix) with ESMTP id 62791C0007 for ; Fri, 27 Oct 2023 18:22:42 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="k/uwxAUa"; spf=pass (imf22.hostedemail.com: domain of 38f87ZQYKCBIAws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=38f87ZQYKCBIAws51uy66y3w.u64305CF-442Dsu2.69y@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=1698430962; 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=7QvlKvGCf16Xr+uDo8e13B+5xMJ1JYvNFtX+dCaVrEc=; b=leYP+qA94ooU0yjb+9P7xRwvHfvxAVEawIVJlHNloOhXZ2yn+/JLZcFREofReW97i2w7bz wxHWTmSHbTzgunDtfY0aRMyhrFV3eSg6/+daZVCw/H1/UAV8LHJ9lRLSKWIDwXaQWjmJZx ksOMuglkgtkm2WbaaTI4jyAP5qYgvtg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430962; a=rsa-sha256; cv=none; b=Q1GxulZ5eSQFhLb5DPFAs18BnHJhSslVMiaQbYFjdyhuB8+sXkfoXUsGKj+a6UYL8RH1X4 M/791eOzlQmuZcLbtq/4spAiB6CV0YSlhycBc1y30lv2ruCgQ92PMUAP8JjHP7zr2ytwwN NgUZnthmNpqKJflSisuAj7fLN/+WKHo= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="k/uwxAUa"; spf=pass (imf22.hostedemail.com: domain of 38f87ZQYKCBIAws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=38f87ZQYKCBIAws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d9a5a3f2d4fso1710463276.3 for ; Fri, 27 Oct 2023 11:22:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430961; x=1699035761; 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=7QvlKvGCf16Xr+uDo8e13B+5xMJ1JYvNFtX+dCaVrEc=; b=k/uwxAUa7uk1XdVQgDy7CTGKwNqHCeSXejZVjuu/tjbNsckc9PWh57AhR/SPXXTPJf lFCpIOvf2kGvOAKfiAJNng8e2TP44j0VGkvz+n0zlA0/KE3k0uhx6uMTUUHlDwEaImnv i49fZAkElCmI4Bc5fOYoFxWVX9cZBY2nmbjCm7I1HsHdYUxxB8sMsdC3XKEjIdBR/UCU rmpk+HlR87W+jOSKG4VVMN0+ZR5r7o6WB5d8lXgaYngdrb99tt64CNMj/0G2nMCRCn0N rlZ90ZFQLqStpUzVzMSR4lhOv/VQesvIeSeiPTOZdEnRGtM0YjC1mskKOHbMcDL73Dag 7ZkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430961; x=1699035761; 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=7QvlKvGCf16Xr+uDo8e13B+5xMJ1JYvNFtX+dCaVrEc=; b=aLpcldUhKN6oKWDmOUU4Tn1V/Jtv93oIVucWkibRdFUt5fxDs6CGS/H9Zhk21SLOVF o63xqDs5XzpUjCS4OqTYY26LV/ttklwSct9wz3xbSBMK4rahbDdcGCR8fgYD8rsbR7CV 8nptDHlcjvVGdhOVFxyOcJ6Y/1iz1G45jKFQMT9kkDDcXbnpAYSb34tzIU/I3YNvnmzJ wojeZFoKmsSg4LoOFIqYl8y2yP1BIamGN1vTGuTwCHJPeIgEwKgAs8X5WreAgw7Xu5VW jpPa2YqgUJLSBU7lKccMEkOzcb88vJv7lOWHTBBE4cpHK+v2xJmCWxKvBoAXabe0Qp3J +QTQ== X-Gm-Message-State: AOJu0YyXPEKUF0873gV8Jwm1lAG03vnHHLX61YLdCfN18srSyvc3y0JD N1hzQsMv9Kf4P+RjBzMKH6zP/NSzUsg= X-Google-Smtp-Source: AGHT+IEGteVk64WoocowFgc++46Ydn1QrcnOyupSkq+rT/SBtiB9CXAW8K00cqUCj/t8cf8Aji8XyxJUQTQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:abe2:0:b0:da0:48e1:5f46 with SMTP id v89-20020a25abe2000000b00da048e15f46mr66725ybi.9.1698430961446; Fri, 27 Oct 2023 11:22:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:50 -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-9-seanjc@google.com> Subject: [PATCH v13 08/35] KVM: Introduce KVM_SET_USER_MEMORY_REGION2 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: jexgifax89gzzy6yuqh5k8nzamgowigy X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 62791C0007 X-Rspam-User: X-HE-Tag: 1698430962-891779 X-HE-Meta: U2FsdGVkX19mw/Xd9zl6G6Zz5SO6D2IABLDyO0Zzy3l1GRac2q9+xLmpCfX0kG8ZVghIayUSpZwOxtYdvk9A1ZcoiogpCgCBpNKWuJ+FA3hiAVPiGZcBMin21ec747zHDXCoPb/or4EI4jZ8WhHYEObInimnZm/P77mTIlIMWRISSIAfh0RoQE7P1MMgX+BfX94vNylf1g9N9k4+y7/w0I265G+2KGRn1aXRzROAphiKYH09DO5MLWojF9xmnMiTyzz9ifTRN8fn2Cibo8Jeg1jlcm1ZizkjK00kfIgLd4NI/6PB66/6Chzcy1FpCXWX7WfqWyOqtEylrNO8wT8UPkZWI5hpco/UJSZnzNSczXGTg0OlDN9lX8nqGNDmgQSVg7atLCkVcO3jjJhIsizeVBvYdA4Ydj1MiPrEeVf9ndwEE0R0JNg2q8JucW/oykDmU6LEVPg1Qj5mFDCH0ypCl6jtn4ZqNwn8SMndhDt9qtGtkT2M3x7qmhLKoSFvx60GHkhQZK6VfdpCytj3DqIYU5Eu5yh573lWosR+GVDwq/+EQXe8OOhGGMNhlTGqq64X00sy5hQg2dESlJwW7rhu2yC2YXqz1fc8CHYNmqG/hjT3VQ9iobBYhcg8YRIxCg06Es3DMFATQFSssohw+meVZb1KlNR1XhBgqfwDzDHQuW+6BfcJOgfO8h9yAF941p5OBj+IvIWS45LgjGd39VbY+NC6+6wTUBAYC1nD5QqwqOQpwftTtJxmh8IeSjy6R3Ywd9FVCSKfIHfz1edUn2sIUjABoC50wI8P3PHnIJcFLRCEB2a7QAsohuuJ2As7KVWWrvNHoFtntv4tqRqywwku4A1/hUWg37tySFXQTDRTTSstyJfk9UnTTCdvrAiYL9X+Qv1SbsIKX+QohtE6ZGjP9E+LkAtEwpL0raIteeE3w3EeYHJxiJkHZ3jHsIzkGwsYK/zeP/M5Z0cse5BtP2s MzT4UGRG V94gm1EFCeO+6dUIDOLCI45cdmV1r9H3Y8F8ozynuLrfqEf3hF0YjVeq4Cic986IDD3n3NDOU1arUIa6UTdP3fzGfiyqSzYxeOwKxvFlhtMiRAkQrUPLN4l64bWZtITI59Ys7utT+o8H3t9gE+kYrRvwA3x6EA5SD/x94bghx7HFy40Cetw3Z1FAiIPiRuE1i8fO0hdUJnJRw4zME2mDGrPyEnXyg6pjYEcZlnmyeHA4U5uCozZQZg3KHnd8+KVPCaF4FfFESu75AuMJ4ykGTYiBNp8sr76gA7NEhBkacxGJ3yZixgJ1zKPRmqgYqvweP6hAr129LY2oDu451di5l00mZDk0WBscNQ9QZci04Ye/1FqY+Y9SSNOyg+5e62BsFXNEO/4nYr6D1ad+7jW4eN2x+rmF+fTlBF6UBYDdJiMWzYDfLfvD22FdaJGeBgfsvv60Is8hWM2Fd7UnBxWv9GXuJT/zzHtVVX4kkmWuAYuMiyMqZGDh/j+Yq0MJWBK/BYD/u9HDzFYP7SVfkXejPnRAlMbYEk+q5N8w6qtexw5JEZe9LRO3j6wLd3Nd5/ST5ph5FJ5qKqeb9Alj/NAeEG/yfpD3rgUrezQfT1lJmpOzkKnSJBM3N8iAe37Y+9k1x3ntKR1XDZ2siPD3qzWn4nOsEtPxl3qx1S62UrweKJmMkvX9EOOgNj+t5Wpi/kBw2Z9qehnMZ7n0CGvyfirYI8szqfPfbnx2Wt2Bo8T3a3VTRJk3AI1ZzmM0newPVtXGRW5IfnqRIO6+QXx30VOt2LGuTclPSdcTnUtS+rz4/F3iGUFJqEQKmJ5usyi8J2yf5+H3u9v79EPhHViLcxvAOeBV4MrX42clGJgfEMoD/fHslSNRSg+EIMvFkZezDHVSjv7EKc4cYI3O3q/ozPaJEyEe8+8UtJLPoiwyXFBHymxNtj7I= 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: Introduce a "version 2" of KVM_SET_USER_MEMORY_REGION so that additional information can be supplied without setting userspace up to fail. The padding in the new kvm_userspace_memory_region2 structure will be used to pass a file descriptor in addition to the userspace_addr, i.e. allow userspace to point at a file descriptor and map memory into a guest that is NOT mapped into host userspace. Alternatively, KVM could simply add "struct kvm_userspace_memory_region2" without a new ioctl(), but as Paolo pointed out, adding a new ioctl() makes detection of bad flags a bit more robust, e.g. if the new fd field is guarded only by a flag and not a new ioctl(), then a userspace bug (setting a "bad" flag) would generate out-of-bounds access instead of an -EINVAL error. Cc: Jarkko Sakkinen Reviewed-by: Paolo Bonzini Reviewed-by: Xiaoyao Li Signed-off-by: Sean Christopherson Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 21 +++++++++++++++++++ arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 4 ++-- include/uapi/linux/kvm.h | 13 ++++++++++++ virt/kvm/kvm_main.c | 38 +++++++++++++++++++++++++++------- 5 files changed, 67 insertions(+), 11 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 21a7578142a1..ace984acc125 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6070,6 +6070,27 @@ writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG interface. No error will be returned, but the resulting offset will not be applied. +4.139 KVM_SET_USER_MEMORY_REGION2 +--------------------------------- + +:Capability: KVM_CAP_USER_MEMORY2 +:Architectures: all +:Type: vm ioctl +:Parameters: struct kvm_userspace_memory_region2 (in) +:Returns: 0 on success, -1 on error + +:: + + struct kvm_userspace_memory_region2 { + __u32 slot; + __u32 flags; + __u64 guest_phys_addr; + __u64 memory_size; /* bytes */ + __u64 userspace_addr; /* start of the userspace allocated memory */ + }; + +See KVM_SET_USER_MEMORY_REGION. + 5. The kvm_run structure ======================== diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 41cce5031126..6409914428ca 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12455,7 +12455,7 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, } for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { - struct kvm_userspace_memory_region m; + struct kvm_userspace_memory_region2 m; m.slot = id | (i << 16); m.flags = 0; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 5faba69403ac..4e741ff27af3 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1146,9 +1146,9 @@ enum kvm_mr_change { }; int kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region *mem); + const struct kvm_userspace_memory_region2 *mem); int __kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region *mem); + const struct kvm_userspace_memory_region2 *mem); void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot); void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen); int kvm_arch_prepare_memory_region(struct kvm *kvm, diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 13065dd96132..bd1abe067f28 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -95,6 +95,16 @@ struct kvm_userspace_memory_region { __u64 userspace_addr; /* start of the userspace allocated memory */ }; +/* for KVM_SET_USER_MEMORY_REGION2 */ +struct kvm_userspace_memory_region2 { + __u32 slot; + __u32 flags; + __u64 guest_phys_addr; + __u64 memory_size; + __u64 userspace_addr; + __u64 pad[16]; +}; + /* * The bit 0 ~ bit 15 of kvm_userspace_memory_region::flags are visible for * userspace, other bits are reserved for kvm internal use which are defined @@ -1192,6 +1202,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_COUNTER_OFFSET 227 #define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228 #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 +#define KVM_CAP_USER_MEMORY2 230 #ifdef KVM_CAP_IRQ_ROUTING @@ -1473,6 +1484,8 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region) #define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47) #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_USER_MEMORY_REGION2 _IOW(KVMIO, 0x49, \ + struct kvm_userspace_memory_region2) /* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6e708017064d..3f5b7c2c5327 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1578,7 +1578,7 @@ static void kvm_replace_memslot(struct kvm *kvm, } } -static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem) +static int check_memory_region_flags(const struct kvm_userspace_memory_region2 *mem) { u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; @@ -1980,7 +1980,7 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id, * Must be called holding kvm->slots_lock for write. */ int __kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region *mem) + const struct kvm_userspace_memory_region2 *mem) { struct kvm_memory_slot *old, *new; struct kvm_memslots *slots; @@ -2084,7 +2084,7 @@ int __kvm_set_memory_region(struct kvm *kvm, EXPORT_SYMBOL_GPL(__kvm_set_memory_region); int kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region *mem) + const struct kvm_userspace_memory_region2 *mem) { int r; @@ -2096,7 +2096,7 @@ int kvm_set_memory_region(struct kvm *kvm, EXPORT_SYMBOL_GPL(kvm_set_memory_region); static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, - struct kvm_userspace_memory_region *mem) + struct kvm_userspace_memory_region2 *mem) { if ((u16)mem->slot >= KVM_USER_MEM_SLOTS) return -EINVAL; @@ -4566,6 +4566,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) { switch (arg) { case KVM_CAP_USER_MEMORY: + case KVM_CAP_USER_MEMORY2: case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS: case KVM_CAP_INTERNAL_ERROR_DATA: @@ -4821,6 +4822,14 @@ static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm) return fd; } +#define SANITY_CHECK_MEM_REGION_FIELD(field) \ +do { \ + BUILD_BUG_ON(offsetof(struct kvm_userspace_memory_region, field) != \ + offsetof(struct kvm_userspace_memory_region2, field)); \ + BUILD_BUG_ON(sizeof_field(struct kvm_userspace_memory_region, field) != \ + sizeof_field(struct kvm_userspace_memory_region2, field)); \ +} while (0) + static long kvm_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { @@ -4843,15 +4852,28 @@ static long kvm_vm_ioctl(struct file *filp, r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap); break; } + case KVM_SET_USER_MEMORY_REGION2: case KVM_SET_USER_MEMORY_REGION: { - struct kvm_userspace_memory_region kvm_userspace_mem; + struct kvm_userspace_memory_region2 mem; + unsigned long size; + + if (ioctl == KVM_SET_USER_MEMORY_REGION) + size = sizeof(struct kvm_userspace_memory_region); + else + size = sizeof(struct kvm_userspace_memory_region2); + + /* Ensure the common parts of the two structs are identical. */ + SANITY_CHECK_MEM_REGION_FIELD(slot); + SANITY_CHECK_MEM_REGION_FIELD(flags); + SANITY_CHECK_MEM_REGION_FIELD(guest_phys_addr); + SANITY_CHECK_MEM_REGION_FIELD(memory_size); + SANITY_CHECK_MEM_REGION_FIELD(userspace_addr); r = -EFAULT; - if (copy_from_user(&kvm_userspace_mem, argp, - sizeof(kvm_userspace_mem))) + if (copy_from_user(&mem, argp, size)) goto out; - r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem); + r = kvm_vm_ioctl_set_memory_region(kvm, &mem); break; } case KVM_GET_DIRTY_LOG: { From patchwork Fri Oct 27 18:21:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438936 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 3F9D4C25B70 for ; Fri, 27 Oct 2023 18:22:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7824180010; Fri, 27 Oct 2023 14:22:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 70AE68000C; Fri, 27 Oct 2023 14:22:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C02080010; Fri, 27 Oct 2023 14:22:46 -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 34BA48000C for ; Fri, 27 Oct 2023 14:22:46 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 12D801CBB26 for ; Fri, 27 Oct 2023 18:22:46 +0000 (UTC) X-FDA: 81392062332.01.AB0D342 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf08.hostedemail.com (Postfix) with ESMTP id 31E3A16000F for ; Fri, 27 Oct 2023 18:22:44 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tF5mWc1B; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 38_87ZQYKCBQCyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=38_87ZQYKCBQCyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430964; a=rsa-sha256; cv=none; b=xuYlQ1+P4ernGWI05+W8VZqxS8ALoKOQVb0EWGz/o0q9MZeJs7lUPxNgVovs55SxCvpzlN RvGG7tTUubyzpFbIYB/MlBmlV/xLkhkrolJ/vaU+Ttrraksaw5GbawpoiD4ytZqKJyy/JR nKbVkoJ99Q/ArdkV/M94/QJCQa3YCxM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tF5mWc1B; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 38_87ZQYKCBQCyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=38_87ZQYKCBQCyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430964; 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=dSzaJ5MjV13E6q0DS9u4ZW54i690mbtysQRlTl8m4E8=; b=FkCsD9hAKFSA81XiWhrrHt8Xit+z7VcWM72AiBqAVoyoyx+lGt7GZWw0tOQ8zi8P7bNqR3 HuBEQKobUBe9oF92W63Nq05MkYyZYCMNIY12u+le5YvwF9ISgmf1oiNlB7yCffmoyFTsZw jTbsIgpuDr7R/qFzKE1+LGWjmzkwM6E= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-da04fb79246so1711643276.2 for ; Fri, 27 Oct 2023 11:22:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430963; x=1699035763; 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=dSzaJ5MjV13E6q0DS9u4ZW54i690mbtysQRlTl8m4E8=; b=tF5mWc1BrqSA65DExzkoKKazt3+0/c5XCPvDj38zKVNfBXJ7yfniKrZIl1Y7LG6ioa xCOwyDQR37kmTa+nKePr5up9tg83e/tpuPbKLguUDqf1i6Q25Z2Hf9EDUhvQ1EKzRwXD XQhkqEYBjkBHOiweuumfcUhLqWVk+4BgnJWLkbqb2JwRfKocS+p1SIdQBBIMqOowUC/1 IWcsWumZtgRjSjB0p55VH+d48RJHU9Eyu46uBqGtqqsCMkzal7jXDZNap2ob03C0gzHd og2yOHk/6T7LlvZxMbFZjaABG8Ve28nWorzGHBr3SR/qGeFic6Cp/rszwmpOjrSA4i9i inXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430963; x=1699035763; 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=dSzaJ5MjV13E6q0DS9u4ZW54i690mbtysQRlTl8m4E8=; b=QWz9AbeFOECNN9oKzIM9z9eV2hSWpDBYp5S6V/paLSCEois7F2UM5Sic/KQOPSSa3T RB0Uf2MaZ/7fWYDhO7hAgRSPDCId+t+LNwE5Krsq78tpqNlrLKCJSmz1DVfXJuzZ78CC n0C/Ks/W+xKs/PCITwXgSBVa6q7Ccv+idSlrhjv7nxKI2hSro5/4O78xr0FTLsY7u7J6 KndT9YDWbxCgt1BJnBmBOIrA79yZ0wtew0pCJ7pbTVUaWS6RQ32GfKJYzZ8ZMXmppynM C2eFrcrvtZsHlfdGZkyA2iSu1M+aQRNfbY5MCOk/wupzfyepqh+UNGEeCfwR5O/QvbwJ KAaA== X-Gm-Message-State: AOJu0YzmIdbLou2iXQ+EcfpWmklL3WtbAfJdA8qT6P263TkTKx6faRe/ hIS8C9t+FxEaQoMyzDSW5n1VVhvO4NQ= X-Google-Smtp-Source: AGHT+IE2yfyx6OwAXQ6PbUozSNKQr4HAB6CvBECxReNk+TNP1aa3zG1osU5Glk09o10tbDLcLiI8MNAMXYU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1746:b0:d9a:59cb:8bed with SMTP id bz6-20020a056902174600b00d9a59cb8bedmr61033ybb.5.1698430963421; Fri, 27 Oct 2023 11:22:43 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:51 -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-10-seanjc@google.com> Subject: [PATCH v13 09/35] KVM: Add KVM_EXIT_MEMORY_FAULT exit to report faults to userspace 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 31E3A16000F X-Stat-Signature: 399xnbdhurszndrypo7axqonnum9ntcq X-HE-Tag: 1698430964-393207 X-HE-Meta: U2FsdGVkX1+BX+kNkMDkEKcfbpXH1jdj1Pr3E84TNMFGp/IOyyQKZQMDMDObFQ/lM9TYIhPt8dhjRGDL+E1igkZR/Xf35nCTEQoLgeKmRPzKxnoXyiXP7/j3SAZexKOZ3qNjy1eJeZtXJ1caMtsB2hDF8wOKOCfF0Omcdp5DNj+utsb/fkWjLAP4JXjtLEDjedthA3XzLEfZdLAAj7wHPjGQeRrCnXBzw03A9AmbXOfsOoWuQ85xlhrtdrv6EZXd+xyTubryU2DXHETdkj5tZVA70n8urwmg8dWrKwuMWcL4Jti4p/GbXFMgp56dIQ2ZTrEoN4lrOpoQLO4eMvqHy7of+ohDXWh+DmFFx1FSWWeO//uJZyLnOUSfo+Cormnls2/hx4QY3lqcof0F3gC2eUaxoQtB9LfZRaUThAuWu7DjkqGQgqe0m532thWr7eK/A3l9EAVg2QdL00Um8D1AxSPDpjopLqfPRuCZzkRXMEC70CZvc0NNERefC0ONeb9Lx2n5WPggIZpLZhwH99rIeFjvc6LY5tjls/Xopyyva0K0lFGBMX3lWotaw1mCAwtOtPX+FCBcXZuAVmPKFtNZc5vyyI9L8HTCXEl0CPDZwg5SZpLafTT/gJ1f9nsTLfwbtiD4wm0OpUH8DjtpwBUCqXNQr7WhFIshZGPc7sXqZu4Ezp73JIDIh/NwLn1U6GbfEZDRoWOSftRJgtd+Dmgs7U6u5CD3wUsuq+WnWQVhJTWOE9h9KJfF7RYUz/dhmwJOznH9Vv6THgQlTrbDG0vd7d6wDiPhrg8YL9KykgTu3ACON0p8x1v6ix1upf/0r/wg7uVooMmsLOaliv5bnC5MuKAJGINiN6fTI9xHG0H9443PyNtHpqfQp9WBasLwOYszsvWaLkPVNNW71zxWF/2upF3qmkMvX4/meWq+9NUbOgRlwBDRXUPI7rnZxDa5dNElk9JGJ7xAqirPLpdzJfR QdfohwID ly/vP6P6AZcwI5BdD1USPk33ZBkhHpIImSU+BcW/LSNsz3ye8l6YYHGFPFawclZZ+Ovk3I/mWPwBOpRswfTNm2n4GS3fG61VbdYZFQAxUGA7jIecveAyGt3qyWVXONn6uYQT6+gFK1lAp2RU6z363mQb9V1Y1rlTaWLCy29DfmuEjJoWLobBkx5//oiKqK4nHBFzMZcqlrQbCIU4cexbwhtO8eYBqksQPkOnpihmX2eOM8ewiG6G5f/f3eDGf8CK/ADNydEsJ9bjZOtFdAyGaaveFZHetcyVJqUhH3SkA9n6I6W0uKeOpa6R4qfvGkUSkdf3rRFWbIyblMfHetMrql6sK97Sx+hDyMjm3lwgYG5NR+xSauAoFpJ94ei4ZZ2evkaE1uqM7FCiIKj2X24M91wFMBG8b3n4b6f+jEy4ivAI1lqvoL0wgZvWFotau8vGCfsWsZsNG5jA/lchgOliAEN+B0Ia/SLzCSt1af7zyDS/c3EifCvEBVC7IZplkOPYI7m2DnlCx64Yhn3ZBU5B2uvyA8HpfVYpStuGVPCIMoSdVZL1La3wuJbI6s/pEUxu8RImoCCRmgikVeLBvyXJDNia7Drbs6w/rTcj8kOpTvNg1XNUoJVyfXCuQ5rSdCrHrV22Y1Yuwar2gDewDqdfhf5ge5Xlccyt/4o8v9UUd49PCMn4porlQ4AGW6A9k2FY1dnYw2CPEs6H5lD9uHvRjV2C3urz6NgR3pL/gRFsLpO2TRHYjN0odjG99i9cjBGuJ5owMyEvJr5L4FcTkmUyCC0AlPYLIxUp0QqP1Mutpqo6nNY1k4JBu8gM8UgloOTBnbIMp4MPL/lV7PjjXyQnGvAxjLjjvu8otvRPfz1YH2eDIyjHJLQyUWaJtxboJ1k3IHbwXTTJVQOjXinCEzafMUKb2UDM/r1AhYv/Bd1X4IqQHUiM= 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: From: Chao Peng Add a new KVM exit type to allow userspace to handle memory faults that KVM cannot resolve, but that userspace *may* be able to handle (without terminating the guest). KVM will initially use KVM_EXIT_MEMORY_FAULT to report implicit conversions between private and shared memory. With guest private memory, there will be two kind of memory conversions: - explicit conversion: happens when the guest explicitly calls into KVM to map a range (as private or shared) - implicit conversion: happens when the guest attempts to access a gfn that is configured in the "wrong" state (private vs. shared) On x86 (first architecture to support guest private memory), explicit conversions will be reported via KVM_EXIT_HYPERCALL+KVM_HC_MAP_GPA_RANGE, but reporting KVM_EXIT_HYPERCALL for implicit conversions is undesriable as there is (obviously) no hypercall, and there is no guarantee that the guest actually intends to convert between private and shared, i.e. what KVM thinks is an implicit conversion "request" could actually be the result of a guest code bug. KVM_EXIT_MEMORY_FAULT will be used to report memory faults that appear to be implicit conversions. Note! To allow for future possibilities where KVM reports KVM_EXIT_MEMORY_FAULT and fills run->memory_fault on _any_ unresolved fault, KVM returns "-EFAULT" (-1 with errno == EFAULT from userspace's perspective), not '0'! Due to historical baggage within KVM, exiting to userspace with '0' from deep callstacks, e.g. in emulation paths, is infeasible as doing so would require a near-complete overhaul of KVM, whereas KVM already propagates -errno return codes to userspace even when the -errno originated in a low level helper. Report the gpa+size instead of a single gfn even though the initial usage is expected to always report single pages. It's entirely possible, likely even, that KVM will someday support sub-page granularity faults, e.g. Intel's sub-page protection feature allows for additional protections at 128-byte granularity. Link: https://lore.kernel.org/all/20230908222905.1321305-5-amoorthy@google.com Link: https://lore.kernel.org/all/ZQ3AmLO2SYv3DszH@google.com Cc: Anish Moorthy Cc: David Matlack Suggested-by: Sean Christopherson Co-developed-by: Yu Zhang Signed-off-by: Yu Zhang Signed-off-by: Chao Peng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini --- Documentation/virt/kvm/api.rst | 41 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 11 +++++++++ include/uapi/linux/kvm.h | 8 +++++++ 4 files changed, 61 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index ace984acc125..860216536810 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6723,6 +6723,26 @@ array field represents return values. The userspace should update the return values of SBI call before resuming the VCPU. For more details on RISC-V SBI spec refer, https://github.com/riscv/riscv-sbi-doc. +:: + + /* KVM_EXIT_MEMORY_FAULT */ + struct { + __u64 flags; + __u64 gpa; + __u64 size; + } memory; + +KVM_EXIT_MEMORY_FAULT indicates the vCPU has encountered a memory fault that +could not be resolved by KVM. The 'gpa' and 'size' (in bytes) describe the +guest physical address range [gpa, gpa + size) of the fault. The 'flags' field +describes properties of the faulting access that are likely pertinent. +Currently, no flags are defined. + +Note! KVM_EXIT_MEMORY_FAULT is unique among all KVM exit reasons in that it +accompanies a return code of '-1', not '0'! errno will always be set to EFAULT +or EHWPOISON when KVM exits with KVM_EXIT_MEMORY_FAULT, userspace should assume +kvm_run.exit_reason is stale/undefined for all other error numbers. + :: /* KVM_EXIT_NOTIFY */ @@ -7757,6 +7777,27 @@ This capability is aimed to mitigate the threat that malicious VMs can cause CPU stuck (due to event windows don't open up) and make the CPU unavailable to host or other VMs. +7.34 KVM_CAP_MEMORY_FAULT_INFO +------------------------------ + +:Architectures: x86 +:Returns: Informational only, -EINVAL on direct KVM_ENABLE_CAP. + +The presence of this capability indicates that KVM_RUN will fill +kvm_run.memory_fault if KVM cannot resolve a guest page fault VM-Exit, e.g. if +there is a valid memslot but no backing VMA for the corresponding host virtual +address. + +The information in kvm_run.memory_fault is valid if and only if KVM_RUN returns +an error with errno=EFAULT or errno=EHWPOISON *and* kvm_run.exit_reason is set +to KVM_EXIT_MEMORY_FAULT. + +Note: Userspaces which attempt to resolve memory faults so that they can retry +KVM_RUN are encouraged to guard against repeatedly receiving the same +error/annotated fault. + +See KVM_EXIT_MEMORY_FAULT for more information. + 8. Other capabilities. ====================== diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6409914428ca..ee3cd8c3c0ef 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4518,6 +4518,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_ENABLE_CAP: case KVM_CAP_VM_DISABLE_NX_HUGE_PAGES: case KVM_CAP_IRQFD_RESAMPLE: + case KVM_CAP_MEMORY_FAULT_INFO: r = 1; break; case KVM_CAP_EXIT_HYPERCALL: diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4e741ff27af3..96aa930536b1 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2327,4 +2327,15 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr) /* Max number of entries allowed for each kvm dirty ring */ #define KVM_DIRTY_RING_MAX_ENTRIES 65536 +static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, + gpa_t gpa, gpa_t size) +{ + vcpu->run->exit_reason = KVM_EXIT_MEMORY_FAULT; + vcpu->run->memory_fault.gpa = gpa; + vcpu->run->memory_fault.size = size; + + /* Flags are not (yet) defined or communicated to userspace. */ + vcpu->run->memory_fault.flags = 0; +} + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index bd1abe067f28..7ae9987b48dd 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -274,6 +274,7 @@ struct kvm_xen_exit { #define KVM_EXIT_RISCV_SBI 35 #define KVM_EXIT_RISCV_CSR 36 #define KVM_EXIT_NOTIFY 37 +#define KVM_EXIT_MEMORY_FAULT 38 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -520,6 +521,12 @@ struct kvm_run { #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) __u32 flags; } notify; + /* KVM_EXIT_MEMORY_FAULT */ + struct { + __u64 flags; + __u64 gpa; + __u64 size; + } memory_fault; /* Fix the size of the union. */ char padding[256]; }; @@ -1203,6 +1210,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228 #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 #define KVM_CAP_USER_MEMORY2 230 +#define KVM_CAP_MEMORY_FAULT_INFO 231 #ifdef KVM_CAP_IRQ_ROUTING From patchwork Fri Oct 27 18:21:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438937 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 5591AC25B6F for ; Fri, 27 Oct 2023 18:22:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6715680011; Fri, 27 Oct 2023 14:22:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5CFA68000C; Fri, 27 Oct 2023 14:22:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4269580011; Fri, 27 Oct 2023 14:22:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1B6288000C for ; Fri, 27 Oct 2023 14:22:48 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E81A0A0749 for ; Fri, 27 Oct 2023 18:22:47 +0000 (UTC) X-FDA: 81392062374.11.1FCBCBB Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf10.hostedemail.com (Postfix) with ESMTP id 209F6C0004 for ; Fri, 27 Oct 2023 18:22:45 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LbCICIAr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 39f87ZQYKCBYE0w95y2AA270.yA8749GJ-886Hwy6.AD2@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=39f87ZQYKCBYE0w95y2AA270.yA8749GJ-886Hwy6.AD2@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430966; 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=yZ4F+cCxLEA4PAApSP/v5UU01C7c86XBvQlNJK8gQkw=; b=5cbFWANCvckOHHc1aPsqflioHUSPf65k8iISZ0QD+zeid5zohQ1AevRfFNqsiYLZkp+KP4 EqBir+dsvPZ8Ea0IE5PMS72pwRrhO6pCw3S9cPMK9oO/J8akNyo0+/ZZdqllYQYZnK6+Xx Q6MY5XF30ywwWhlqjiHCuWZYtGLdEmQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LbCICIAr; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 39f87ZQYKCBYE0w95y2AA270.yA8749GJ-886Hwy6.AD2@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=39f87ZQYKCBYE0w95y2AA270.yA8749GJ-886Hwy6.AD2@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430966; a=rsa-sha256; cv=none; b=kj0tF4MSZuUxWjUCxtGep6XrCobqkSzXJsT7oCdLH9WRofkwqs9dk/ObE9RtYHkXf71jTO yGJKmdiTrC/Do7SG+UloRpZPHOYoYUiATkSlZ7X2co2akJAkkmhb3OzemUWJ6P5HSfpFe6 Q/qHw9Ny6pG/Ra+Lbjg+wxcqeh1dCeE= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5af9b0850fdso9978457b3.1 for ; Fri, 27 Oct 2023 11:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430965; x=1699035765; 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=yZ4F+cCxLEA4PAApSP/v5UU01C7c86XBvQlNJK8gQkw=; b=LbCICIArbW1Pb1ffDv3sW5RhuulUO2t5vGWoUvhMlOdAWyqPq+yCb4DKZdfUYfkNH4 Ta2Yfc92yt1jQjRzQYH8pNCcqFF5pmrkO7mXP9ASg5RUSPZp7CgdpqsnJB7CfQmHZUIx 5FO1XtO929Zsk797tF0yXNEh5cHUh2Dstls83nuYG7SL2hOJF4e0j8HgaMBcyaSA6XSa uqu9mTgl0i6/VUZpFUa60Ym/BABr8d6B6Ni4IxTzblU+JZCrtb8qAdS8pWGNBeHJovuj Zw5SMb6W1P90FHf7D/3Psla3gVzuZhuhG6DsnGRtkE7k6IRLmOdVkYZMMyrMtFlZNq5O 2dvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430965; x=1699035765; 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=yZ4F+cCxLEA4PAApSP/v5UU01C7c86XBvQlNJK8gQkw=; b=mLqhVVLf2LoK1VOr7bxf/PnCCL2tS+ra9JeSNYxbzPjSA/kHkkVzsC9t0Ni2DB9/z+ BYxgmLrif2ansHIwLF9Y54l0w5TfGVV7y961NgfRcBc1LaUUiCZZ16YbDSj4LTlqrLmL ARlWRxGdZvsFjCITIFv+NFz6NYeSFyUKhvXGuscTw8bHZb9HEYPjhO9LRfXX200WmZ9v BpdNpaJD90ZrObRjOdiwTXUT2Sj5MMflYE8a4vJjZ9k6+gd0k5YsUYWXjTPElZpnWX2d VbgmHGd/XIVU18d8eqWDKdjoIjcPMe+dmzShyM1yzTFyrIr9+uNXdgIHP7oXduMj/epk rV6g== X-Gm-Message-State: AOJu0YwjGRgtMmt3fdrYuu/c80LQQoPvmXayROUG5N5WuFVix9BzNPRk wMIFz1MyBPEr+rp1b/U1eldAdgex9Po= X-Google-Smtp-Source: AGHT+IGwuIwT1CUNj+EGBgkfDLIp57neyP6oJZbU4jqENzEgZhephR9TVXqJjgCFT4zzJuNYuq9lZ329bBo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:a14c:0:b0:59b:e97e:f7e2 with SMTP id y73-20020a81a14c000000b0059be97ef7e2mr73613ywg.4.1698430965278; Fri, 27 Oct 2023 11:22:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:52 -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-11-seanjc@google.com> Subject: [PATCH v13 10/35] KVM: Add a dedicated mmu_notifier flag for reclaiming freed memory 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-Rspamd-Queue-Id: 209F6C0004 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 39qq1cjg41atjhdhnx7nan95s78r9sw6 X-HE-Tag: 1698430965-486796 X-HE-Meta: U2FsdGVkX1+jSlUolXeBBQCT9Zps4aEVWcRJlu1/5sI/M5n5guT7J4rPCNvVuqybqLovpgykBYoIlEd0LyuMkh7gCbPpjwKEPDH6K0E+XqyqMZjeYwIoKpawcBUy5PjdDrxS0CX1NwrKZ4hmdx0MNS0dlOpDE6cm/fvq53FMQsSgQbUMlmTB/mHe2VLWz+8e8VOVHSnP+jl498iw8nqiNv42frCD9FhmBNWnL9nuCkHxnmb02BTYU57ECVc1TFRCET0fqJfxWKNtCLWb2uuyp6qMcdV1VRlzrtp9SM2oyLzb/i/NsdYrtuZ3p6jtTO/W5mjoeVFGxUEM4NZHJiC77nfHbbOjSWipDg4yYevdWYePeSL++Xikm6YU7drIKfjj5aGGH/O/5/wNsdSAgoBo4qot4j7ObqVLj5IyeosiTHlVbod/ZqtNWEK08MBjSzRlSCPPXBSZvMWDq+1wYtVO2uP61NBSgDDTR+lFvgpXZwV53XundpXjYB2OkRV+u6FfwLs7PeW0ejtymIVyv8w6HlCjV/Xjit6nNxL+zL2YJ3VTywpb6Lz7Zga4KRY1WrZ2s/juXqMQ20D/FunwjekUO4nPsgFnRHWNeFaCPcHHN7kqrK3ZmzPXkIKOXttgyxbdmMc90SVnKO3q5ZA/o8rhpNOD5Ip18udWi9Zt0v0Y52O1iH4lWadZ7QwbkxGbS6Qj6lZ7J3ZXxs8ApsngQ8NUDwBv2o3INXy05MlweOsjxaDa4q///pdpdBbblDyrKpTakebZIfDgPP/AEKCnOZQPQ3JNf/wiYXOD+omVy0Nu8MkE3GburgstOPB/QMBgh1kq+aRmeBocSrGdtvgd97USmQBohX0gr13D41TqKn2WcUMAyprOl3sOMmCnDuv8+dDg5FMxSJ2Z6GSpBgxzWKnRegrUWMJ6m1bS63cnt2J3HZ0urD1GqawbgvUByC22JISyPJF2FUFqg2GEtEnZnmU euXTDCge yAq11vLXNoT0fdwJ/QnrePPP2xnGqUQRZ0QlKm+TeRyFQl/6eD0Y0qTY1Fi/I27PJscjgtu6T0jAH3/tFfIKrMPDrecac6vYhp7iY396klzp258UujpKUwUKEVMdrOspCSVIv7f/Yw4GDNfKmP9mu/acqozjAlFOQOcVj8DjYP5QsjMqBQFhjxw+BJIWLRbuRXcQrFLz5Jtq2lsn1YUNrnIMjdjRqqoB6WN5z1jvRlgNGddEJoOTy7GPOlYmq/lDmvnpm8zzX6fW2Q36lqJ5lKZTFzAJQTi5uhVU/bJeHndTlE/CiN16w93aEiI+mtPiUu5nTyx5EbJ/D+EMbJUmPsSNlZlAmxldyVo7LtYf81KHCH+A3EVKNytiPbBdTpW67ox6fL5lKU8HHsKT7kGi7Lk4IcXMJfkRr6lrrMXxEnja/D8xxTIgjwjwCNOL/t8VYJUCzY7xjzytkRRvzI076pec09xrd8s9FmEVLHqnDIQxHFc0vP+8ZqTnXbbQefJQ8dylgvsj0rf84330Vjg+pThzuQhwG4G0SKhhiO/rZJmVjNNtCed6lYgS3PGRPPPaAV9DpnXLQnKerr/2RWaRXq0LizBoYA4DCLpaZrR0/jACNFt4eAqHmVkDOzhnAngJfyuk8yLb6GPkHPCQvCD4IP5My77aYDl4vR+8hU3tRbbG8zB94xe058FJ9DK2b0o0A2WK+XBRuKrMq2woz1kBOy0/gGsQwZMWcjbfw/ISzFK5kTKuGNj00zWd752DgN4yvH8e2/c7CexbkiZAh7WvezZbJiwEynod9XKCE8FIRyFiKQJeWhhfZNRQUqA== 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: Handle AMD SEV's kvm_arch_guest_memory_reclaimed() hook by having __kvm_handle_hva_range() return whether or not an overlapping memslot was found, i.e. mmu_lock was acquired. Using the .on_unlock() hook works, but kvm_arch_guest_memory_reclaimed() needs to run after dropping mmu_lock, which makes .on_lock() and .on_unlock() asymmetrical. Use a small struct to return the tuple of the notifier-specific return, plus whether or not overlap was found. Because the iteration helpers are __always_inlined, practically speaking, the struct will never actually be returned from a function call (not to mention the size of the struct will be two bytes in practice). Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- virt/kvm/kvm_main.c | 53 +++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 3f5b7c2c5327..2bc04c8ae1f4 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -561,6 +561,19 @@ struct kvm_mmu_notifier_range { bool may_block; }; +/* + * The inner-most helper returns a tuple containing the return value from the + * arch- and action-specific handler, plus a flag indicating whether or not at + * least one memslot was found, i.e. if the handler found guest memory. + * + * Note, most notifiers are averse to booleans, so even though KVM tracks the + * return from arch code as a bool, outer helpers will cast it to an int. :-( + */ +typedef struct kvm_mmu_notifier_return { + bool ret; + bool found_memslot; +} kvm_mn_ret_t; + /* * Use a dedicated stub instead of NULL to indicate that there is no callback * function/handler. The compiler technically can't guarantee that a real @@ -582,22 +595,25 @@ static const union kvm_mmu_notifier_arg KVM_MMU_NOTIFIER_NO_ARG; node; \ node = interval_tree_iter_next(node, start, last)) \ -static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, - const struct kvm_mmu_notifier_range *range) +static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, + const struct kvm_mmu_notifier_range *range) { - bool ret = false, locked = false; + struct kvm_mmu_notifier_return r = { + .ret = false, + .found_memslot = false, + }; struct kvm_gfn_range gfn_range; struct kvm_memory_slot *slot; struct kvm_memslots *slots; int i, idx; if (WARN_ON_ONCE(range->end <= range->start)) - return 0; + return r; /* A null handler is allowed if and only if on_lock() is provided. */ if (WARN_ON_ONCE(IS_KVM_NULL_FN(range->on_lock) && IS_KVM_NULL_FN(range->handler))) - return 0; + return r; idx = srcu_read_lock(&kvm->srcu); @@ -631,8 +647,8 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, gfn_range.end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, slot); gfn_range.slot = slot; - if (!locked) { - locked = true; + if (!r.found_memslot) { + r.found_memslot = true; KVM_MMU_LOCK(kvm); if (!IS_KVM_NULL_FN(range->on_lock)) range->on_lock(kvm); @@ -640,14 +656,14 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, if (IS_KVM_NULL_FN(range->handler)) break; } - ret |= range->handler(kvm, &gfn_range); + r.ret |= range->handler(kvm, &gfn_range); } } - if (range->flush_on_ret && ret) + if (range->flush_on_ret && r.ret) kvm_flush_remote_tlbs(kvm); - if (locked) { + if (r.found_memslot) { KVM_MMU_UNLOCK(kvm); if (!IS_KVM_NULL_FN(range->on_unlock)) range->on_unlock(kvm); @@ -655,8 +671,7 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, srcu_read_unlock(&kvm->srcu, idx); - /* The notifiers are averse to booleans. :-( */ - return (int)ret; + return r; } static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, @@ -677,7 +692,7 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, .may_block = false, }; - return __kvm_handle_hva_range(kvm, &range); + return __kvm_handle_hva_range(kvm, &range).ret; } static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn, @@ -696,7 +711,7 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn .may_block = false, }; - return __kvm_handle_hva_range(kvm, &range); + return __kvm_handle_hva_range(kvm, &range).ret; } static bool kvm_change_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) @@ -798,7 +813,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, .end = range->end, .handler = kvm_mmu_unmap_gfn_range, .on_lock = kvm_mmu_invalidate_begin, - .on_unlock = kvm_arch_guest_memory_reclaimed, + .on_unlock = (void *)kvm_null_fn, .flush_on_ret = true, .may_block = mmu_notifier_range_blockable(range), }; @@ -830,7 +845,13 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, gfn_to_pfn_cache_invalidate_start(kvm, range->start, range->end, hva_range.may_block); - __kvm_handle_hva_range(kvm, &hva_range); + /* + * If one or more memslots were found and thus zapped, notify arch code + * that guest memory has been reclaimed. This needs to be done *after* + * dropping mmu_lock, as x86's reclaim path is slooooow. + */ + if (__kvm_handle_hva_range(kvm, &hva_range).found_memslot) + kvm_arch_guest_memory_reclaimed(kvm); return 0; } From patchwork Fri Oct 27 18:21:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438938 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 1300CC25B47 for ; Fri, 27 Oct 2023 18:22:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B77A980012; Fri, 27 Oct 2023 14:22:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B003E8000C; Fri, 27 Oct 2023 14:22:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9531980012; Fri, 27 Oct 2023 14:22:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7DE918000C for ; Fri, 27 Oct 2023 14:22:50 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5A55E409B0 for ; Fri, 27 Oct 2023 18:22:50 +0000 (UTC) X-FDA: 81392062500.16.2AC5188 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 72B67180003 for ; Fri, 27 Oct 2023 18:22:48 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=eUkEKsba; spf=pass (imf16.hostedemail.com: domain of 39_87ZQYKCBgG2yB704CC492.0CA96BIL-AA8Jy08.CF4@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=39_87ZQYKCBgG2yB704CC492.0CA96BIL-AA8Jy08.CF4@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=1698430968; 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=fLrd3AGy66HdihfPmI58EyyG3FCGELBv0TZMEZAIG4Y=; b=CQUawqdnjxxW4TxZnJalhoVPqEC1shBouFSNDQCm6Hvd3wjQZMWzuYdu2HZXHjIp+XsyNx VsUhObrYKx69j/evqprb92+62QAbjxSP1deeA6cMuMU7U691W4PtDuGJm4jG+bd44wPGCn msjRiWqzoFRqENlrOuzsNbeeRRZXYik= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=eUkEKsba; spf=pass (imf16.hostedemail.com: domain of 39_87ZQYKCBgG2yB704CC492.0CA96BIL-AA8Jy08.CF4@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=39_87ZQYKCBgG2yB704CC492.0CA96BIL-AA8Jy08.CF4@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430968; a=rsa-sha256; cv=none; b=u3e0px++tpw1CdFYIiXdsvEyjp5Wh/ynK7pQzlK/wV43axgiOvDNzFIdcLw4UytSjoZ3xt GbxyrHXCI8nf7YtOSS4m4tQSYxkyb+3I332iubBpH2Ue6Mk9n1+QmG3EluxdDTt2Xqkyl9 1h6Jec9UuMwQsORQkINkpedU5YDbwv4= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1cc1ddb34ccso9088505ad.1 for ; Fri, 27 Oct 2023 11:22:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430967; x=1699035767; 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=fLrd3AGy66HdihfPmI58EyyG3FCGELBv0TZMEZAIG4Y=; b=eUkEKsbasHZ/A0sQ/eE5StIAJEN0AVSIomqOWPnUcQVU2f40XQUZrCGBhvM2MFcGTW ikEAlnvz02zgPF0ICtJOWEX/SdHtVtb8dogs3sU02vlax0aXqRNeyx4xSdM2N/nOBQ6H mehrtzo7v8jl+qFZ9OfezJ0ku1npJ0WMBIBJaCXuLIhzniKHlQpJOp40iujiFk9uGZYB puQVZwX2O04UaPKqRwSbrm4HGHkRI+yA+5y8We39vxWcKC3oDfB8iaRovnzrMtLz7Kxa ja6TdE+wPJIIiiqrGnZufrm+5WzZQM22YWc/J7ETJsNUW7M2S34do88QQ34GSizP1YlO DdBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430967; x=1699035767; 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=fLrd3AGy66HdihfPmI58EyyG3FCGELBv0TZMEZAIG4Y=; b=h5uFVxcmOYXXhSxg9WZp769Krxt/K0/jWTtGXV0DAr2uxBv1f7ulTTchrB9X6rnZUJ ebBhFfXju0ZV944pLE/g+X/kwYy+8/NG62k2irVf9LatsdLkaU7r0f46BSaeDvpsEVW7 NZ4XrMCUyRwzc/9E5vQyDPIxLp3JD/6cCZhMAjpTPnOX7+THkQYVNV7gFj0jGUss04uP P1MsZ/dDvsQfsZLCVNYvTzhWQmJ2GeuSV7haQITDMtl6gKhA/Ybwfugmm17F36+yToOD AXVP9ABYA1MNVpGp8UxQWzdstyurKYA2qV81pa5gY1OZ28iMOrRiGTJdRUb+6QIMVu+M rquw== X-Gm-Message-State: AOJu0Yzk3Nx85D7UHDpwULhWHw1NzUoxJP0FxJ7kugmfevmriugt4iB8 l8vzVwoH2UOjNFW2DO4f/gCkoKfsAiM= X-Google-Smtp-Source: AGHT+IFw7CMiVxkdfLmPKgztX5f1RnQlL4BTlZlVKLxMz6aRVtHUrQY578kvMdkjnRTl6BBw2VS25sQYIRo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ee14:b0:1ca:b952:f5fa with SMTP id z20-20020a170902ee1400b001cab952f5famr63161plb.5.1698430967325; Fri, 27 Oct 2023 11:22:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:53 -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-12-seanjc@google.com> Subject: [PATCH v13 11/35] KVM: Drop .on_unlock() mmu_notifier hook 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-Rspamd-Queue-Id: 72B67180003 X-Rspam-User: X-Stat-Signature: qkn5dpy5hq6p5in9wwu81dgnan63pmbb X-Rspamd-Server: rspam01 X-HE-Tag: 1698430968-539171 X-HE-Meta: U2FsdGVkX1/wODEntmttDuwmr/xIBPE/F/HB0765CD2Ztj1FGa3/b5qfEhqdp3I/WZYzogO8jvUVd1HLa0UjK16ovoJNPQX30TdzLDSIBfwytllwLPZ8Ot0Ok8SNHB/oTvKBqRCjVOk89MUdhEp5OcHAmCfJxLQg3Oh4PO3uQqDKnoqUvC+giQVhpzmGzvHWPiykZ5WqZZIEyQNyepubpLfIpniJwcSKfB2eUiLyRnBYyts72VxKaWDWFRbPMFg/DKXW+3Z0zJ7pGLJYmEpqP6TEFGSY0/6NeuK/gqu24Vzj/gWlC1tsSCiRHuU83iC9lC7Ut97HsWOSUcv2+p/2RoPwcy1CyCBIpcuBc44auBigNyd/GR5IcVzE0FpTFTxQ0cMChI1hVgYf1bkDEcDYF9xcRRSK8Y2oewAuufBlSeETzPw89wTSb35E40Y6GwCl9RRiX+WxZ42j9dN1tNbQEGkSc0mqrrsAaLpOpnxnDd4wKBDSH947/PKTiLVVqsjTRJSPXKURAkbfmLWMu4GHA1YCx/B5S2KcT2KhGHjJ0muemt4EaN+dwczUPk7kXuRvMbwkaCvI+LovxwYLPf3PbNIsoiDasAFajL9Jh2UCMjC3i97YzW8RgIcmhsz0lwqnCal0FM2XyQIuje96FGMejyIt5H+YASqXRLck//IQ8o45kj2Pt86hLbL5UurEgz/MZMYRIwiPIulNfhITqbaik7erUvpflrPmr98lXRXvxCAgi61ugwMGSQleBivFmHFPkM0grnW5CiVle8MVol1MROox3EQGKFS/V6hC4VcGZhcIIKcxHH3b2e+gz26ZdtNySkeTIm4OESM1yKApyGv36njIM1oy2PBh0yXoHXjIA+KNW7N+G5WsVuopGy9d3bTZaa3iIZBvhNdTl0+VQN3dKfta2gyCuLC3C4XYBrFMshRmKbB1gAQo5H1sL2kMuG6D243jDLAfLhVMK3cjU+0 v91jZwBy ub88rUIvBNhZ0Hfj2EBBHGwd7OlHsbRpbHmMKB/1x5EWo2Gozi+CsaXDhYoUQHc/Nl84eXeh9us6whPLWNn4Db1ICn+l/98eYtaZC9LnKIni1GDMSXDgn7c4gkJIEinsEUdcmyDp6TfBSzMump6mWimUa00mYpJ4d6BwedG8Ot4QUPk3tVQFAo0W5Sbu3x0MlbOorWjVCPKUclpMKJNOw626ozBKOK80/w0mpHqu+Op45Wu0zVr5WFrsoRkzEJCIkR1FvhX1pD4i6RmKKZra2pNWR4M07PDSUGX2T8T1lJlJkr6/Fr0X0DbmMYTtiszesr3G62jNK43/u36dFKGdrpOntQ6JsClVRSPyNDhJCjUkRClsyydXcANjvzrf4whJXW2OX1Ak3rSKZq+JwP29Z+DK2/ZBNaylBUbr7yGd7YixSJDQyccBCYUM4HWt+H7PCiqIdtFGycmSj9Z8PNigWMWSWyhlOscFIspoZY/bP8FgwhrWvx5sUyTJbNg5tYYCpZ8d+R7rl2sMIac6yWc2wY5+RFIcMt7A78QgASZNbkUVDOtXVYJucw0w2naApLGvdSRK6gLdQJk3W6o5z/C3Qd0kl2g757KNx3b5UBQantRTWLAJWUl7nJsqV0zUwmAJHIQgEVUss1u/81I84EVmfyAUGiYhFXxnuJ04syRQoY99Zq6+mCXTbkUdMiRyvzHE6X1YEs7hn5ha1tof4UTJChn3cB2aga8mGm7l1pjX/JlNnCKnDd+Xz5lZDTwU8jucKi8P5JexJIBF2fXHD3W3W0VFU2iBS9E24oXIns3BUsiTHDU0tyA7xjfGQWD7G3MBKaZl1M3qGXs2JG9pcUMgPF+/VQhv3Jr8N1cUPpAe3XRTIE/s+so8H3w2gJTvYrNy7FuVFlioc36B6PlQ= 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: Drop the .on_unlock() mmu_notifer hook now that it's no longer used for notifying arch code that memory has been reclaimed. Adding .on_unlock() and invoking it *after* dropping mmu_lock was a terrible idea, as doing so resulted in .on_lock() and .on_unlock() having divergent and asymmetric behavior, and set future developers up for failure, i.e. all but asked for bugs where KVM relied on using .on_unlock() to try to run a callback while holding mmu_lock. Opportunistically add a lockdep assertion in kvm_mmu_invalidate_end() to guard against future bugs of this nature. Reported-by: Isaku Yamahata Link: https://lore.kernel.org/all/20230802203119.GB2021422@ls.amr.corp.intel.com Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- virt/kvm/kvm_main.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 2bc04c8ae1f4..cb9376833c18 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -544,7 +544,6 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) typedef bool (*gfn_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range); typedef void (*on_lock_fn_t)(struct kvm *kvm); -typedef void (*on_unlock_fn_t)(struct kvm *kvm); struct kvm_mmu_notifier_range { /* @@ -556,7 +555,6 @@ struct kvm_mmu_notifier_range { union kvm_mmu_notifier_arg arg; gfn_handler_t handler; on_lock_fn_t on_lock; - on_unlock_fn_t on_unlock; bool flush_on_ret; bool may_block; }; @@ -663,11 +661,8 @@ 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) KVM_MMU_UNLOCK(kvm); - if (!IS_KVM_NULL_FN(range->on_unlock)) - range->on_unlock(kvm); - } srcu_read_unlock(&kvm->srcu, idx); @@ -687,7 +682,6 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, .arg = arg, .handler = handler, .on_lock = (void *)kvm_null_fn, - .on_unlock = (void *)kvm_null_fn, .flush_on_ret = true, .may_block = false, }; @@ -706,7 +700,6 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn .end = end, .handler = handler, .on_lock = (void *)kvm_null_fn, - .on_unlock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = false, }; @@ -813,7 +806,6 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, .end = range->end, .handler = kvm_mmu_unmap_gfn_range, .on_lock = kvm_mmu_invalidate_begin, - .on_unlock = (void *)kvm_null_fn, .flush_on_ret = true, .may_block = mmu_notifier_range_blockable(range), }; @@ -858,6 +850,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, void kvm_mmu_invalidate_end(struct kvm *kvm) { + lockdep_assert_held_write(&kvm->mmu_lock); + /* * This sequence increase will notify the kvm page fault that * the page that is going to be mapped in the spte could have @@ -889,7 +883,6 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, .end = range->end, .handler = (void *)kvm_null_fn, .on_lock = kvm_mmu_invalidate_end, - .on_unlock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = mmu_notifier_range_blockable(range), }; From patchwork Fri Oct 27 18:21:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438939 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 50A00C25B70 for ; Fri, 27 Oct 2023 18:22:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 97BA780014; Fri, 27 Oct 2023 14:22:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9061B8000C; Fri, 27 Oct 2023 14:22:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70A3080014; Fri, 27 Oct 2023 14:22:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 50E208000C for ; Fri, 27 Oct 2023 14:22:52 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E7AE3809E5 for ; Fri, 27 Oct 2023 18:22:51 +0000 (UTC) X-FDA: 81392062542.02.3303C99 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf29.hostedemail.com (Postfix) with ESMTP id 261A3120002 for ; Fri, 27 Oct 2023 18:22:49 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=H3PwEBhf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3-f87ZQYKCBoI40D926EE6B4.2ECB8DKN-CCAL02A.EH6@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3-f87ZQYKCBoI40D926EE6B4.2ECB8DKN-CCAL02A.EH6@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430970; 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=MfwjFnr1wNHEBMs743caMc0qlRTo5HMEnN08vNH/OXQ=; b=D+DMM/m6/QZbMb2AwWa83pJRPXy3BC/HkFx0jJMg2tGHYePc3wF6Hq9dgMcEP6s5Ixwy03 b6ZGvtG916f5rcvBu1PYmVdnuujNpJEiCCyVPBgnFwdYoo1zUD+e/q133eSEfVxgh0Tmho nsSe9I78pQnM0NiD3e45VoazK6X1m04= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=H3PwEBhf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3-f87ZQYKCBoI40D926EE6B4.2ECB8DKN-CCAL02A.EH6@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3-f87ZQYKCBoI40D926EE6B4.2ECB8DKN-CCAL02A.EH6@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430970; a=rsa-sha256; cv=none; b=te3g6djuJbRcHNbxRAo7+tIfGO4pcMwf2ctRZvzEB62qwL7j74U6XWYnGGW3S2iHdIoZ+V jbm+zL4Y4IwODLxADumRQ0brtY1YaC2dzyto8xOosuyl5S6tvyKTgpEVkHNUrLugmOrdsq tm1nELlJrIsiTzJ/PzMR9l+RWSjtGNs= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5a7b9e83b70so22724417b3.0 for ; Fri, 27 Oct 2023 11:22:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430969; x=1699035769; 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=MfwjFnr1wNHEBMs743caMc0qlRTo5HMEnN08vNH/OXQ=; b=H3PwEBhfr0C275u3UgFNg/ZSi6zPgcf5TwGvrYzQ/jY8mPmMpig5D/L2ErLBeDjq8F A7rMQMASx9QdD1GtPc3rQGKy8y7VN3UTjPjC2mfEtz/H1iaT+TROlT/PWGJa2ZgO5ag1 8wiiuiXdmKreeraEdDpjTG6v8gSr0imorgwscCf//xk3jBnj90DNts5RfAR5/BPGiBGI wXVtoqeyibM3sxW1upBNwkWb2ZNfOE0pGVWh7/5SaDHSJEPMhpEqbODQPq5MuRBy5Dyu nZffDxJoSn35FdbCp3gTsSjj5snGfu6P+8X84RWDIMv4rxQ+lVRCX0FUnhx3LBmXh8Ss R8Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430969; x=1699035769; 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=MfwjFnr1wNHEBMs743caMc0qlRTo5HMEnN08vNH/OXQ=; b=g9y4/JgnZueQvjI4dUeFwA9NomF3+SjiQ4EcjhSdFN6BopLDutSjV5qpAUwbuumd61 vw32GKsq68xH0DKmbTNvbotVbzBo0+dbqS2zBACPeSRat0+DYkZdUnmHicNgdY5uwiIB VALUxouD3xttNjPk81uEFYZRn6hVP/4EXYHhukZ88OAmOf9P+JyXY2w7Nb45JKso4j5J ow/PhM2krKD9QsqPEEkFxh2B0D6q0d0fDlUjDtBMfWOBJfLp6cJxx4J/OwtntZI5ZQvk YJGMyN0HFZDqMCd4tgiEWOycqIrlE929Tbsn1Hkbd2fS+KVBMD4xSYcx4XhFz6ahVbuA IxwA== X-Gm-Message-State: AOJu0YypodrstE4+mgPj77qOFLJ7sQ0rkZ/jdnFdZqgfOKrUoNb9xHnE l2f4P+PZtQOY3HtGSc80IdGBnMTLV8k= X-Google-Smtp-Source: AGHT+IHtw6A7NuD1zDZw/j7xBoNpID5PTLHCLxD7YeudY/tlgoiYGQdJN9iuOebDV/xY0oQ2kV5qeLUK06Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:ca0f:0:b0:59b:e663:23b0 with SMTP id m15-20020a0dca0f000000b0059be66323b0mr102868ywd.4.1698430969394; Fri, 27 Oct 2023 11:22:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:54 -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-13-seanjc@google.com> Subject: [PATCH v13 12/35] KVM: Prepare for handling only shared mappings in mmu_notifier events 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-Rspam-User: X-Stat-Signature: xfpxu9xp8eb5xhbt4rk6cn71mowcd1h6 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 261A3120002 X-HE-Tag: 1698430969-203767 X-HE-Meta: U2FsdGVkX18ffyntTm0goyCjP2jVcj+ggm18ao0iWAqgfQBmToWIkpkVjPYSWWpr0+d2dXhv+vN/lVghRGepDTx8o6pkDvzreErqgrh1h281iLZa8D4eeDvLbShULNh/2pYCaWEd9hVf9UsA6cXvPlXMaL3ipPjgh47MiukNozCPsd8MBiGDO0bq5kcN6BsM5B4foooDVL/m11ciujYqXs5YCOpfH9yH91A/6T6CYFxVYaOs/NlvtcAExBCmCIM6phqejv7aFAbohTRu9CGkDWXp7pal9/PuXrq52RIA4vOgM0r2hTrQhRTezJlfYmsYNtPuXDWmHpdJAnhmSKj48hWKrP0sonkuJMYFIGWZfVjb/TmSdi4Hq/eZgzPGMQUM0MIat/nqkBGRZ/i8BVwTJZ7TgKYNNSDt2YrrGVxr8rXfHuyHrq2GisuVXAB6GNW1IwS0fOeBksPFSbfxe0QkVcTp2yjg9lR+uqbQ0Xb0uPuTwDPHTOteGzgGcoTiXE66SjnJZn6heoLCNlwxB17i95hAHlQcafWqWglcD3Zy7hhPuJ2bGpJb2KFKMuD8JRE8w+AiPlI12QFL+ilBms/bju63Q0j7gJW9vg/+f+1WhmjYummXU2fTvlniczV1s4L9RFZBkfm1781qB9KDxmBGw/dXsn3Mg651zcg7YSOmU2VUZug1wrGCvMZ/BIizmqIrdIgThcrecjWq2xbVBu0zcqw4403h1YIKwXjRnxjUnBRJQSQ25ROBHZ4vM4TlVLp4O+Qv1corHjTDP7bL8t7RrNCQpb0cvfiWkf75ON+Ot0KDBF4XhbeKQeRdmvNaxL2UypbOcGzT48OUJ2ibgdQ01sBjTpRbgF8gnvQeE9IFtA0VC89pOGGbibGMUQtRIj7QKkPtQaOenCSx89/ms4p+fcIaoYU/ByyiCNHnASp33qusdqROKZ4cuhjr7TFahfFxeDrTgtnr0WScexvT1/8 b5al1nfV nFvyl/XOrAfbVlCsDz3g2eUAPFKKnzNy9VA5lwiMS67UEUoVww14yvgOFS3i4Iw4n1h36E/iHnWRKeX3ouvi8wgv+OJuWT4tSxhHhPnpWYfVCj6ohFrhAtrKASYTvLqElCsglBUhQUkKux2eVRAtgBPN/wqHEoc1xdS5jm1GphK0shYN+m2oCL6xrQ4ajVVayq410tamc9TqKAMssAiLAC3LcnU0Gzq96M5zIx90/g4qAMqAvHiRa7lWSofnCDQWzW399x7n2t8SCRALckKe3k36hocj9s84G1XKWCg1+a2yFiPl/+zChVseRGlSkvxJ85C9yoSCTe+MsH5F1HgPM43kLRl35dtT031/hrtWY/5RqK3fIYfL3RXiWJtttZo7QQsTzdgGhiU7DTVWYmbEYbwOv/A8dTTgXQ4JSf3nH7wQv/p93v50sH5SaO0qYxqAQFvJSG7k4Z2xc2W1pqqXx3pstD/ErHZqh9+56zMPA7Nihr3pCxJSyRaFMnoz6xkvXbqo5lzAsotSZTbA1bA+QOTRAh/jaQRfIxDANWoGGgjbJB0YGYy5RObZ/pH6sbX7hI6WWRHLpE1p/yBtwufEATJM/OrQlSklpdmywMVcXashKEJzDpTZr390zK+pizflFwOwji5rNOwwiCy1wyYyQZqtZ0Y4/QjJQ6T+T3PpQVOX4HXnS+Ozxk5a6zZnyXfWL3clLQmRS7ldGYV+FompcDq9Ggy6uBA+R78ibgiiXQX1AEEkxbcw94ZihWK9J7rdnXm941OC47Lib5FcOVxYVOHXVVeG/KQ4QoBmVtJVHN/7A9ZR75y+89PMbh6RIudPCMTo9pppCkiZwWFpqNz7i7I7+dUkwJ6not0J9Wz7eCMVYR3yvhpTuLlbwYA== 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: Add flags to "struct kvm_gfn_range" to let notifier events target only shared and only private mappings, and write up the existing mmu_notifier events to be shared-only (private memory is never associated with a userspace virtual address, i.e. can't be reached via mmu_notifiers). Add two flags so that KVM can handle the three possibilities (shared, private, and shared+private) without needing something like a tri-state enum. Link: https://lore.kernel.org/all/ZJX0hk+KpQP0KUyB@google.com Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 96aa930536b1..89c1a991a3b8 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -263,6 +263,8 @@ struct kvm_gfn_range { gfn_t start; gfn_t end; union kvm_mmu_notifier_arg arg; + bool only_private; + bool only_shared; bool may_block; }; bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index cb9376833c18..302ccb87b4c1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -635,6 +635,13 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, * the second or later invocation of the handler). */ gfn_range.arg = range->arg; + + /* + * HVA-based notifications aren't relevant to private + * mappings as they don't have a userspace mapping. + */ + gfn_range.only_private = false; + gfn_range.only_shared = true; gfn_range.may_block = range->may_block; /* From patchwork Fri Oct 27 18:21:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438940 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 9F898C27C47 for ; Fri, 27 Oct 2023 18:23:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4CCC80015; Fri, 27 Oct 2023 14:22:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD3C78000C; Fri, 27 Oct 2023 14:22:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A173780015; Fri, 27 Oct 2023 14:22:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 875FD8000C for ; Fri, 27 Oct 2023 14:22:54 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6036B1CB290 for ; Fri, 27 Oct 2023 18:22:54 +0000 (UTC) X-FDA: 81392062668.08.FA2A288 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf20.hostedemail.com (Postfix) with ESMTP id 5DCC71C0011 for ; Fri, 27 Oct 2023 18:22:52 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=kXp5Atak; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3-_87ZQYKCBwK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3-_87ZQYKCBwK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430972; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PHuKmbJK++WVHRTr2ixGhZnz3PjglwNcwDhJuJ6xsm0=; b=b3td16VUvnJ2hI3CHuGtl8x3SsTGqeedzC6CKMlzHQKn4UqXa29FitGBdvMkzlWhAZvwL3 t1badf4AidkmWPORH91mJgZg/L/RYUEND560T3jAoJqcSylRycw7QYo8nqMo8oP1BQ+yuf et6kXT9rqKiNtVbN8YSD5UxG1307B4U= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=kXp5Atak; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3-_87ZQYKCBwK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3-_87ZQYKCBwK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430972; a=rsa-sha256; cv=none; b=yuOI51R1Ul5aQcyDYVmQg3aB1IXDZnxIQxjInzGLtWSPOpTlqsPZQxh4ZCJ5c28BPmG7Z9 j4GseIW3DDwwRXcsnjJZXaD8ExJ/eaXsPLdHrGKNHLnrTLP3anCgL84LIw1cqzKPDOAn8c iGNyEXgNKgCcEcXZ30uuhXiRLJ7LGQ8= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5a7bbe0a453so21184367b3.0 for ; Fri, 27 Oct 2023 11:22:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430971; x=1699035771; darn=kvack.org; h=content-transfer-encoding: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=PHuKmbJK++WVHRTr2ixGhZnz3PjglwNcwDhJuJ6xsm0=; b=kXp5AtakBMDdqWMJqlkWXn2FbwujSbkAo2BT5jjt/vpaciOTrvbtJjDeRF7Tz9Kwg1 gByVoW4OzxyDi1DjqnrLukmo+u3A1tRRzBtnCHDWEgN3Z4ZDhqcEm2AURYgDhA5RVnJX sQLawZXL86of97TeIR3z4znfjvgGH2z1Ywmqg/5s6P08Z5vyvN7ZQCJAIvhlD1WV/gON QifPRmnazGV5f5AmhCXLhmJLmXo97fypjukSoTXgLv7ftbRrIOCRsMvheiVkaXJtlU0U BAsvYblHNnE341sPJHOE9TmLOK8D3my07yjyGCTAD/k5tJu3W0Ed6W9IvMry4y+ug0sB Tvyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430971; x=1699035771; h=content-transfer-encoding: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=PHuKmbJK++WVHRTr2ixGhZnz3PjglwNcwDhJuJ6xsm0=; b=S1URrOQ/fVunb86fPK3LvRsGCm+1z5Jo2qyJnyMEuPxOQCRZMMVTMlxDc0kZkb5t5K /D/wG1t34zi2onBfBe0OUzMYxu1eY8Ww5PByf60aQtl/+isK8FhkL20PjQtzhpZ46OAf OUJ0OWrixZug3wjb9Bu87esXrhrTFHZyllIqICr1paw6S30hEq21RbB4PfVEUT4vUz+q Hz7+klwtr3yF3GQU9lqT0V927b2PM1TJhcJFGCXOxUhoszsMtQ/1ugfWJUZmao8rMhoX y4nNXtNBnHEi1p1JQef7BaZ1yt4PqQAx6bREVP/djBg8btqkRvs+WpzuPVRpJdTiMXWJ nmng== X-Gm-Message-State: AOJu0YzTvzEjc98CTwcjcNIKUpFN3YrVqvEijDbh+y2miOgeTR9au1AU tlzaxSSwhzr20iZ/lb5yvdSocFPBrAE= X-Google-Smtp-Source: AGHT+IHeAi8c5J1JHmkq3P4PpsdNdjX6Wq3XK5NOfAhf9NwJS3eapFdTXpLG2igv2I31VgMM7Gn14gWqO18= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:ca97:0:b0:5a1:d4a5:7dff with SMTP id m145-20020a0dca97000000b005a1d4a57dffmr73743ywd.6.1698430971510; Fri, 27 Oct 2023 11:22:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:55 -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-14-seanjc@google.com> Subject: [PATCH v13 13/35] KVM: Introduce per-page memory attributes 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-Rspamd-Queue-Id: 5DCC71C0011 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: f9g8m9m6gss9s9c4o7jzejgmpbtkumqu X-HE-Tag: 1698430972-508768 X-HE-Meta: U2FsdGVkX1/44VEGCrB28XVMrQ/XprIUt1kDSXYOVlGQMLediJ+oDZrTTxBnJtEFKsg6f1IB8l3iPtCangJVZ+jRrNKcf0RwdsfYp6wLLGDre2DAh3sOJzrC0nhmRc90y50GONppCbGw5ddhewiC69MswPHumwdnbcn35zi+nk/aagrkrAl7oETrPXK5th177Cj4wFcydGvy4laZp8zMwWf/5NvEuBnegX27po3y0IJ66h/6Po9bjbNL6iK96CEM+Dw8GRraTGNbvSTeuqG/az1vaHHIR2h70ZAYyQzcf0tYCUzBhPXIkD5a4VcNx5fRkDVMKWrply0PThSDmtw/dkM3hxiz46T3DB/AXmVRVNowbkaptBXmLPkBZHcOBJ41JVdy8ZKzPRQABiWQ2TDBy6TxaRRW1xD9dEAKtgvqZVI1qHrm6R2E+eGUoJiIH/ToYi+pwg61EAxg9gFzOt/heSIQfNofUo6vAleBd7LRap69kMcmZz1g+2Yvo+Dgv/+0uA5eE2ZaF4dkTefOYRSwKdoNlLJD9clWOorr4mhj258qO+kCyvhiRlDlUYib9LYhQZUAhnS+62Pds7GhQZmcHq6YPra3z9wjq5T7n9CGvvNfwllP1bOEfMkT8AOZw/yQw2eRfSd3dJxAV4teDsjcvC8j0Wj4uSisHVXdO0c5DoP7daQC+rwqS6N4y8tVwnnlPmE1yAcZ1sD3cD7xM+EzlbwEPcm4AgdPSSdmGFewQqRbH1fUMc6aqs2fZMBIgrttkgQuYHmu2pnYR2NfWn+5htqM5/bmADHN+lRvRmR53+RroiL7xwNoI0PwU8FnSNZ+uyYqbF5rqT9ibiAFeUyG8YWY9WHNxpKfpnqhY8lFIW/b5sxJnVmFzEW3FKbQBXVjtPnMku8x+Ox2ZIujbK2+eXQCcyd+08zObM4/c6WUlWlGbcdY8jfDfzXqWhDoVsYdZlPSpJDZek2xeDSkpfO 2u0/ZA0m SvswblHExLR1pURj0aIBaEPo5wIm2/JVI405Xd+QVq3mdgy94alx32zFfufBiKHI6GeoiSQkTtgyk6ja0kNhTYY+gFoGRMF63SkGmCRNqbJ6e3jLPi8Aj1ySa6FwlFc10aehornvQkhcEfPSX62Kr5B6c/Rn4nvxe5EaOIbutCueDR+89OiKGVPUQa68MlZWjGszmJuR9BCWW3V9i4Q0vCeNJDdMmSShNFBLBHrMf4ZSwzJRNKJigsaQio7a5VxvMJUoM1e7/ch39o2nhA2BhID/adsJ3/UCF4mnQVVLgtlPT5o9GsFbk2VMlQK8DU7rxd/FGw4h2b5nZGWZ6OurpjtdLTuC7nl7x2Dkgmr+EpiH84Tay8Sbfp0G4xk60uHlGMr8t8UJtxHI7Xs/iODK/HiNCRhDp1jhSAGVYQoViXO3/kpxw4HBQ1BTYo7Cwsor0mQksWaaPmYWbN8rxR4hb5PcJyO7lF4OEgvBFItDa+kiUP+IP3ElmRKL21Vz8Q1fber3KOS/kyyrw7y1/xBszRypjkLOg3CRN77fbYswNH3qxdNuuPHzCuDbkSIQ6GD294L9og06+ZDFbbdqHtEE9F0c8L+L+kGnlrjdHrJ2DBMeKL0QmVgBVnzn66rI05OW9bAhHdQOae5f0mT+TwtyeV2sTG6+2HpLO2vwu7bcfeS/OqntpachfQAJAIO/tYpcY7EXvG75JrBw9rY5x3tdg+uKqy8IHi+MG1CJ9WT07piNYlOcnGu0RVhnFDuMXGgdxbiyVGd4mHxiYd9HzBEmTBQLPelXQoZDQzC0xbdzSSUW9FEfjBfZrwbp1o8agvWx8t/YxmiJVaRF7o7X10Sr30DXX57mYLWa2q1s+f9zxYp3J4mAheCZGhpkmNbh8w5r7wU01q5iTU/CUQJbZr0V7izbbJGNAipPrfKiF 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: From: Chao Peng In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots. Introduce two ioctls (advertised by KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes. - KVM_SET_MEMORY_ATTRIBUTES to set the per-page memory attributes to a guest memory range. - KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES to return the KVM supported memory attributes. Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation. Use bit 3 for the PRIVATE attribute so that KVM can use bits 0-2 for RWX attributes/protections in the future, e.g. to give userspace fine-grained control over read, write, and execute protections for guest memory. Provide arch hooks for handling attribute changes before and after common code sets the new attributes, e.g. x86 will use the "pre" hook to zap all relevant mappings, and the "post" hook to track whether or not hugepages can be used to map the range. To simplify the implementation wrap the entire sequence with kvm_mmu_invalidate_{begin,end}() even though the operation isn't strictly guaranteed to be an invalidation. For the initial use case, x86 *will* always invalidate memory, and preventing arch code from creating new mappings while the attributes are in flux makes it much easier to reason about the correctness of consuming attributes. It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed. Suggested-by: Sean Christopherson Link: https://lore.kernel.org/all/Y2WB48kD0J4VGynX@google.com Cc: Fuad Tabba Cc: Xu Yilun Cc: Mickaël Salaün Signed-off-by: Chao Peng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst | 36 +++++ include/linux/kvm_host.h | 18 +++ include/uapi/linux/kvm.h | 13 ++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 233 +++++++++++++++++++++++++++++++++ 5 files changed, 304 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 860216536810..e2252c748fd6 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6091,6 +6091,42 @@ applied. See KVM_SET_USER_MEMORY_REGION. +4.140 KVM_SET_MEMORY_ATTRIBUTES +------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in) +:Returns: 0 on success, <0 on error + +KVM_SET_MEMORY_ATTRIBUTES allows userspace to set memory attributes for a range +of guest physical memory. + +:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + }; + + #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + +The address and size must be page aligned. The supported attributes can be +retrieved via ioctl(KVM_CHECK_EXTENSION) on KVM_CAP_MEMORY_ATTRIBUTES. If +executed on a VM, KVM_CAP_MEMORY_ATTRIBUTES precisely returns the attributes +supported by that VM. If executed at system scope, KVM_CAP_MEMORY_ATTRIBUTES +returns all attributes supported by KVM. The only attribute defined at this +time is KVM_MEMORY_ATTRIBUTE_PRIVATE, which marks the associated gfn as being +guest private memory. + +Note, there is no "get" API. Userspace is responsible for explicitly tracking +the state of a gfn/page as needed. + +The "flags" field is reserved for future extensions and must be '0'. + 5. The kvm_run structure ======================== diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 89c1a991a3b8..df573229651b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -256,6 +256,7 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); #ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER union kvm_mmu_notifier_arg { pte_t pte; + unsigned long attributes; }; struct kvm_gfn_range { @@ -808,6 +809,9 @@ struct kvm { #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; +#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + struct xarray mem_attr_array; #endif char stats_id[KVM_STATS_NAME_SIZE]; }; @@ -2340,4 +2344,18 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, vcpu->run->memory_fault.flags = 0; } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn) +{ + return xa_to_value(xa_load(&kvm->mem_attr_array, gfn)); +} + +bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, + unsigned long attrs); +bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range); +bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range); +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 7ae9987b48dd..547837feaa28 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1211,6 +1211,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 #define KVM_CAP_USER_MEMORY2 230 #define KVM_CAP_MEMORY_FAULT_INFO 231 +#define KVM_CAP_MEMORY_ATTRIBUTES 232 #ifdef KVM_CAP_IRQ_ROUTING @@ -2277,4 +2278,16 @@ struct kvm_s390_zpci_op { /* flags for kvm_s390_zpci_op->u.reg_aen.flags */ #define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0) +/* Available with KVM_CAP_MEMORY_ATTRIBUTES */ +#define KVM_SET_MEMORY_ATTRIBUTES _IOW(KVMIO, 0xd2, struct kvm_memory_attributes) + +struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; +}; + +#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index ecae2914c97e..5bd7fcaf9089 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -96,3 +96,7 @@ config KVM_GENERIC_HARDWARE_ENABLING config KVM_GENERIC_MMU_NOTIFIER select MMU_NOTIFIER bool + +config KVM_GENERIC_MEMORY_ATTRIBUTES + select KVM_GENERIC_MMU_NOTIFIER + bool diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 302ccb87b4c1..78a0b09ef2a5 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1218,6 +1218,9 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) spin_lock_init(&kvm->mn_invalidate_lock); rcuwait_init(&kvm->mn_memslots_update_rcuwait); xa_init(&kvm->vcpu_array); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + xa_init(&kvm->mem_attr_array); +#endif INIT_LIST_HEAD(&kvm->gpc_list); spin_lock_init(&kvm->gpc_lock); @@ -1398,6 +1401,9 @@ static void kvm_destroy_vm(struct kvm *kvm) } cleanup_srcu_struct(&kvm->irq_srcu); cleanup_srcu_struct(&kvm->srcu); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + xa_destroy(&kvm->mem_attr_array); +#endif kvm_arch_free_vm(kvm); preempt_notifier_dec(); hardware_disable_all(); @@ -2396,6 +2402,210 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, } #endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +/* + * Returns true if _all_ gfns in the range [@start, @end) have attributes + * matching @attrs. + */ +bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, + unsigned long attrs) +{ + XA_STATE(xas, &kvm->mem_attr_array, start); + unsigned long index; + bool has_attrs; + void *entry; + + rcu_read_lock(); + + if (!attrs) { + has_attrs = !xas_find(&xas, end - 1); + goto out; + } + + has_attrs = true; + for (index = start; index < end; index++) { + do { + entry = xas_next(&xas); + } while (xas_retry(&xas, entry)); + + if (xas.xa_index != index || xa_to_value(entry) != attrs) { + has_attrs = false; + break; + } + } + +out: + rcu_read_unlock(); + return has_attrs; +} + +static u64 kvm_supported_mem_attributes(struct kvm *kvm) +{ + if (!kvm) + return KVM_MEMORY_ATTRIBUTE_PRIVATE; + + return 0; +} + +static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, + struct kvm_mmu_notifier_range *range) +{ + struct kvm_gfn_range gfn_range; + struct kvm_memory_slot *slot; + struct kvm_memslots *slots; + struct kvm_memslot_iter iter; + bool found_memslot = false; + bool ret = false; + int i; + + gfn_range.arg = range->arg; + gfn_range.may_block = range->may_block; + + /* + * If/when KVM supports more attributes beyond private .vs shared, this + * _could_ set only_{private,shared} appropriately if the entire target + * range already has the desired private vs. shared state (it's unclear + * if that is a net win). For now, KVM reaches this point if and only + * if the private flag is being toggled, i.e. all mappings are in play. + */ + gfn_range.only_private = false; + gfn_range.only_shared = false; + + for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + slots = __kvm_memslots(kvm, i); + + kvm_for_each_memslot_in_gfn_range(&iter, slots, range->start, range->end) { + slot = iter.slot; + gfn_range.slot = slot; + + gfn_range.start = max(range->start, slot->base_gfn); + gfn_range.end = min(range->end, slot->base_gfn + slot->npages); + if (gfn_range.start >= gfn_range.end) + continue; + + if (!found_memslot) { + found_memslot = true; + KVM_MMU_LOCK(kvm); + if (!IS_KVM_NULL_FN(range->on_lock)) + range->on_lock(kvm); + } + + ret |= range->handler(kvm, &gfn_range); + } + } + + if (range->flush_on_ret && ret) + kvm_flush_remote_tlbs(kvm); + + if (found_memslot) + KVM_MMU_UNLOCK(kvm); +} + +static bool kvm_pre_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range) +{ + /* + * Unconditionally add the range to the invalidation set, regardless of + * whether or not the arch callback actually needs to zap SPTEs. E.g. + * if KVM supports RWX attributes in the future and the attributes are + * going from R=>RW, zapping isn't strictly necessary. Unconditionally + * adding the range allows KVM to require that MMU invalidations add at + * least one range between begin() and end(), e.g. allows KVM to detect + * bugs where the add() is missed. Rexlaing the rule *might* be safe, + * but it's not obvious that allowing new mappings while the attributes + * are in flux is desirable or worth the complexity. + */ + kvm_mmu_invalidate_range_add(kvm, range->start, range->end); + + return kvm_arch_pre_set_memory_attributes(kvm, range); +} + +/* Set @attributes for the gfn range [@start, @end). */ +static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, + unsigned long attributes) +{ + struct kvm_mmu_notifier_range pre_set_range = { + .start = start, + .end = end, + .handler = kvm_pre_set_memory_attributes, + .on_lock = kvm_mmu_invalidate_begin, + .flush_on_ret = true, + .may_block = true, + }; + struct kvm_mmu_notifier_range post_set_range = { + .start = start, + .end = end, + .arg.attributes = attributes, + .handler = kvm_arch_post_set_memory_attributes, + .on_lock = kvm_mmu_invalidate_end, + .may_block = true, + }; + unsigned long i; + void *entry; + int r = 0; + + entry = attributes ? xa_mk_value(attributes) : NULL; + + mutex_lock(&kvm->slots_lock); + + /* Nothing to do if the entire range as the desired attributes. */ + if (kvm_range_has_memory_attributes(kvm, start, end, attributes)) + goto out_unlock; + + /* + * Reserve memory ahead of time to avoid having to deal with failures + * partway through setting the new attributes. + */ + for (i = start; i < end; i++) { + r = xa_reserve(&kvm->mem_attr_array, i, GFP_KERNEL_ACCOUNT); + if (r) + goto out_unlock; + } + + kvm_handle_gfn_range(kvm, &pre_set_range); + + for (i = start; i < end; i++) { + r = xa_err(xa_store(&kvm->mem_attr_array, i, entry, + GFP_KERNEL_ACCOUNT)); + KVM_BUG_ON(r, kvm); + } + + kvm_handle_gfn_range(kvm, &post_set_range); + +out_unlock: + mutex_unlock(&kvm->slots_lock); + + return r; +} +static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, + struct kvm_memory_attributes *attrs) +{ + gfn_t start, end; + + /* flags is currently not used. */ + if (attrs->flags) + return -EINVAL; + if (attrs->attributes & ~kvm_supported_mem_attributes(kvm)) + return -EINVAL; + if (attrs->size == 0 || attrs->address + attrs->size < attrs->address) + return -EINVAL; + if (!PAGE_ALIGNED(attrs->address) || !PAGE_ALIGNED(attrs->size)) + return -EINVAL; + + start = attrs->address >> PAGE_SHIFT; + end = (attrs->address + attrs->size) >> PAGE_SHIFT; + + /* + * xarray tracks data using "unsigned long", and as a result so does + * KVM. For simplicity, supports generic attributes only on 64-bit + * architectures. + */ + BUILD_BUG_ON(sizeof(attrs->attributes) != sizeof(unsigned long)); + + return kvm_vm_set_mem_attributes(kvm, start, end, attrs->attributes); +} +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ + struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) { return __gfn_to_memslot(kvm_memslots(kvm), gfn); @@ -4640,6 +4850,17 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) case KVM_CAP_BINARY_STATS_FD: case KVM_CAP_SYSTEM_EVENT_DATA: return 1; +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + case KVM_CAP_MEMORY_ATTRIBUTES: + u64 attrs = kvm_supported_mem_attributes(kvm); + + r = -EFAULT; + if (copy_to_user(argp, &attrs, sizeof(attrs))) + goto out; + r = 0; + break; + } +#endif default: break; } @@ -5022,6 +5243,18 @@ static long kvm_vm_ioctl(struct file *filp, break; } #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + case KVM_SET_MEMORY_ATTRIBUTES: { + struct kvm_memory_attributes attrs; + + r = -EFAULT; + if (copy_from_user(&attrs, argp, sizeof(attrs))) + goto out; + + r = kvm_vm_ioctl_set_mem_attributes(kvm, &attrs); + break; + } +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ case KVM_CREATE_DEVICE: { struct kvm_create_device cd; From patchwork Fri Oct 27 18:21:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438941 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 65D4EC25B70 for ; Fri, 27 Oct 2023 18:23:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9CD180016; Fri, 27 Oct 2023 14:22:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D25418000C; Fri, 27 Oct 2023 14:22:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9E4680016; Fri, 27 Oct 2023 14:22:56 -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 9FE0E8000C for ; Fri, 27 Oct 2023 14:22:56 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 65782B61E3 for ; Fri, 27 Oct 2023 18:22:56 +0000 (UTC) X-FDA: 81392062752.18.91AA3E2 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 76FB010000C for ; Fri, 27 Oct 2023 18:22:54 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HfqSisbO; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3_f87ZQYKCB4M84HD6AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3_f87ZQYKCB4M84HD6AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430974; 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=KesXVMWlLgDxABh+fu6pK0xRQzS+y87PSmDv/paodW4=; b=M5Jt1O9HINsDdcxfa9PmnwQL22obitryPknGAKoCHyY4ex3Jwo9+8NjG/SYVFJuk5hS3vB HvJUxmrQHW+rgDKbZoENABCKOynpt8jk9r5w390mr+WJOa8EQuZmjZ39ZeK9lhTok7R5TU kKysYpJSzs4q4nhQ7Gzn/g4N/9bxkio= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HfqSisbO; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3_f87ZQYKCB4M84HD6AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3_f87ZQYKCB4M84HD6AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430974; a=rsa-sha256; cv=none; b=Yb1Hi0vq1xUEwEYIVgUgBhOJrlXcBLX6lIYkZZFESGG5SfwcBjDSNtCHheZqx4rd6Tm2J9 v27HWT8ibNMzlIpdlpH9XfWJHRGSjfk3IpQq40sFha1BCM8qWA3fOCjgtU4kWP1HzcPVvi 7boofWRa38+JJ58N27yst+Zu9yhTAAQ= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d8486b5e780so1772917276.0 for ; Fri, 27 Oct 2023 11:22:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430973; x=1699035773; 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=KesXVMWlLgDxABh+fu6pK0xRQzS+y87PSmDv/paodW4=; b=HfqSisbOav1qnaXt+cqAB0kBmWUjm/Is/vDyxkeKw1hFVQydFFphrArB3QGl+C/sK+ ncJj96dHVofWaj6SJ1IT38MG0Ux8gaMw/PI4nEROrwahk2L2/8neXZdzpQ2BqRsbt6Pp rEsiOy4bzdB8UlDxvclmTLiTq0md0vSdVlpW2/wbmXyuibjS7F/wzcZjTVotmieVFg82 j5YAfC8S34hCJMWKYmNoxlhSJo/gQMP/GJkIl+iRw7QOBChI/zxRPSSVtCO5e8u6lyvI x/22ZYMiFCujTpMS33OTMKF0+VOue76rLBxC1h9ckuRpioyWO9YALKx7Bc9PvRr+1tbl WYEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430973; x=1699035773; 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=KesXVMWlLgDxABh+fu6pK0xRQzS+y87PSmDv/paodW4=; b=WNzCudU2iqecL1AXZFlEL403Xekp6FsvHNXJfvplWz2+5+r8YcA8G8R8RxisuW1xAv O5ty0l090/RmaYs8WfjmPFMZBeRi4ZuDNOoVY1vct7AdSZnkLn7Wmmg/UysiuIVrsA2P LtunQbTtA402RK0K16fNUNb85yhafpXW9emqDB0/MYhaQdIsBRkamivcpxnwZ0yTgqZ8 Pf9UT4ilzVX6udomIZlRojanXLbnHmvXfconIti6PWg3CmCt9Rde+odvaUBERjImzSV9 F74Sgbu6cLMSKw713TJCIdDXHdVSe+wnN4xwOP68+mBHi7D5yvIUdjzpGsufEa/Bb1ho 8AoA== X-Gm-Message-State: AOJu0YwoPJhWu5w92fZJyOr4Wbo3pklJzCIUPpStP+uUMbgBEvena8Uz OrDbDewkbgU/yd7sxhn2Rib4qhaT0yg= X-Google-Smtp-Source: AGHT+IEnuAFhzspZaQ7IFc3ileY3RuCJW0iKBNsx2McP6lY6HFfWXzfm0mDQuIa0sPNZvKStqElvm4PIJQ8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:da0:567d:f819 with SMTP id v3-20020a056902108300b00da0567df819mr78694ybu.10.1698430973571; Fri, 27 Oct 2023 11:22:53 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:56 -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-15-seanjc@google.com> Subject: [PATCH v13 14/35] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 76FB010000C X-Stat-Signature: psxt1rbejctamgy8oe38d8r5yxxq7ikj X-HE-Tag: 1698430974-432362 X-HE-Meta: U2FsdGVkX18QU0KMdc+0vLHQSEa3umyX2FjwEZaPb2Cc77YqNE+nSvpdvS1Khh5qM+G1jvhpj8e55YbJx1BWx2z1J8C1+VoBZyR73V+TOvtvGdgXiptg1NgbjmIaXs5Gd9XtfGc7sm2Uxr0uhpIdAhnHEaNlOHoHM7sViNe6qD59KmGsnBv13czf7cslQSRqQjdp8y7hzV+34+wJx5rCI5mqcmlSJOJZsWGmPFehSntrhYn6eOFxejTaGXlDa/R5Go3j6v48MYyHfh1eKsbW1u1pfLs6NEy5n8tJbX7u5vSr4E41ZYVykCrQqXeZmXbLEOO5QAnF6NbaDaxshUtOnJU5xO+YqmRO5opLb30ZTOMwV62ahQvx/+tcLmCqFxyzyf7/JUIoMyhTpI2UeoySA+f+ALTBTPwfcwn30ijwa4fB/QAf9HESDfecj4fZ7N6eGPQKAfZ3OHYqXYh/BEblpyKU6VkLsxfCyxqM+laEG7B4DDrwabXaSASYLPoGreSKTkqV2RC+TKraLkJCNiQ5RCcfl5NO+lcb/1bPORONw23nPg+bjPHtt1lBH8jifW1gIi0U+2+hwcwQZHqRGVdgsq5my/f5xGeEOj76zFccughiI6LT1r/B9TEy4qXu7WDtvjOkZLXFtw+DkVCsWP01/DU8EFvNEwzI7d5eA+LMu198tt2doRxln5juWrau7U0LNVk+Ny9l5pU2Ve2KeAlez/1EbvY3BmBFnN0QXk/Y+m70PrasHwseTQLrkBJcJCkIr7oW0VhXSBvqSMh8EudxZmC1TA67Xv69TmRPOYe0mS7Wg6DH8Z3knb/aZS5bsaJYJ9pCaGHIn8Fb8ta4oHhtOKa2SeIT1sODIi+4JmYNjU/NJ3HCMwx5MWJWSKaIb2a3/Iy0QdW0Vhh3Ox+tiTz/l2AY3cp+L/dgi8uvzyVtxIJsdzY+sT4Ekt/K58xqCPTEeAmnBICNs3BopvdMGoo D6Ri6dsJ 01UGgLc+sGYdMOYkNrp1dNLgO9JFcmx8spgd3zJSGt1JxObpJeVHkdifaXMaYyllRXP4WYLkr6CcsLTSwTUJ/r+rlZPd+fIcbArktZvTOXsK+DU2u7V/enJ9kk/KQ0loBpG9C+NM6Q7GB3aiahM7y31hbzQ72SDZYtYfM99cs43HP7NBEmTX2e/4Oi2P8YWwJ3Ta3EwtIHvv6XC004Zf/ZKxU1p4sk/qRfPQ2MQA8jDvA5QrpT0xgk5nqIaU5n2PCUzEyrBxY9Jk6Z8EsbjNFYv1HdfJRbcJjRNfo5bOMpMwS8YDQV/a25GMI2RWdT4K0hZEE5f9/+cbgo4M64y8CFZ5lq/TINfvMmA/PoM0Rd4NO3f49HnvuGFTuMaxhwX7OeCurCT3DB05F/DQDuLqLE7QF6Y++M8K6N6DYGqcUJ35X+p5s77GzQgSrUHvoSRWH1Z262PCxKTed2CinKijQIZvuCVjLUWekBDlgmSwVsnLJW+vLNP71X9p1U/+EVBVgv0SIdySFYm9uHBN7M4vSp0SyHOVMjMn9ozby970fKWECEKW0ZB88YRPIevPAuUjr0DqwdjJzclX0VCfxU/TgsGexvVwsZf9Va37esPNNm1nkWVtGYNj1wrk4ESZHnXHSokrpVYBsEp7bAFKMHwdzf7ZCHaKKJ2lxPZ22xAQEcLHmtq3g7oGuJVaC8U6IXGwawn6HvyhHVrXSnxDWkglTcTS1+Sd9yGU+fLHgAz20mf98HUqw2PqNrd+KJxd5MODbcOMfUwAR2MhO9IUlOZ7BQ8UCyizEAIqhr0czCH0F+BTp4Nry4YxsX080HoZ6BbwQ0aWIPHZRn4HOfic4NIS8NgrcQ27M/2VyMvIU0XwlP++TuqUDIh9pE1dDgg== 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: Add an "unmovable" flag for mappings that cannot be migrated under any circumstance. KVM will use the flag for its upcoming GUEST_MEMFD support, which will not support compaction/migration, at least not in the foreseeable future. Test AS_UNMOVABLE under folio lock as already done for the async compaction/dirty folio case, as the mapping can be removed by truncation while compaction is running. To avoid having to lock every folio with a mapping, assume/require that unmovable mappings are also unevictable, and have mapping_set_unmovable() also set AS_UNEVICTABLE. Cc: Matthew Wilcox Co-developed-by: Vlastimil Babka Signed-off-by: Vlastimil Babka Signed-off-by: Sean Christopherson --- include/linux/pagemap.h | 19 +++++++++++++++++- mm/compaction.c | 43 +++++++++++++++++++++++++++++------------ mm/migrate.c | 2 ++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 351c3b7f93a1..82c9bf506b79 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -203,7 +203,8 @@ enum mapping_flags { /* writeback related tags are not used */ AS_NO_WRITEBACK_TAGS = 5, AS_LARGE_FOLIO_SUPPORT = 6, - AS_RELEASE_ALWAYS, /* Call ->release_folio(), even if no private data */ + AS_RELEASE_ALWAYS = 7, /* Call ->release_folio(), even if no private data */ + AS_UNMOVABLE = 8, /* The mapping cannot be moved, ever */ }; /** @@ -289,6 +290,22 @@ static inline void mapping_clear_release_always(struct address_space *mapping) clear_bit(AS_RELEASE_ALWAYS, &mapping->flags); } +static inline void mapping_set_unmovable(struct address_space *mapping) +{ + /* + * It's expected unmovable mappings are also unevictable. Compaction + * migrate scanner (isolate_migratepages_block()) relies on this to + * reduce page locking. + */ + set_bit(AS_UNEVICTABLE, &mapping->flags); + set_bit(AS_UNMOVABLE, &mapping->flags); +} + +static inline bool mapping_unmovable(struct address_space *mapping) +{ + return test_bit(AS_UNMOVABLE, &mapping->flags); +} + static inline gfp_t mapping_gfp_mask(struct address_space * mapping) { return mapping->gfp_mask; diff --git a/mm/compaction.c b/mm/compaction.c index 38c8d216c6a3..12b828aed7c8 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -883,6 +883,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, /* Time to isolate some pages for migration */ for (; low_pfn < end_pfn; low_pfn++) { + bool is_dirty, is_unevictable; if (skip_on_failure && low_pfn >= next_skip_pfn) { /* @@ -1080,8 +1081,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, if (!folio_test_lru(folio)) goto isolate_fail_put; + is_unevictable = folio_test_unevictable(folio); + /* Compaction might skip unevictable pages but CMA takes them */ - if (!(mode & ISOLATE_UNEVICTABLE) && folio_test_unevictable(folio)) + if (!(mode & ISOLATE_UNEVICTABLE) && is_unevictable) goto isolate_fail_put; /* @@ -1093,26 +1096,42 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, if ((mode & ISOLATE_ASYNC_MIGRATE) && folio_test_writeback(folio)) goto isolate_fail_put; - if ((mode & ISOLATE_ASYNC_MIGRATE) && folio_test_dirty(folio)) { - bool migrate_dirty; + is_dirty = folio_test_dirty(folio); + + if (((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) || + (mapping && is_unevictable)) { + bool migrate_dirty = true; + bool is_unmovable; /* * Only folios without mappings or that have - * a ->migrate_folio callback are possible to - * migrate without blocking. However, we may - * be racing with truncation, which can free - * the mapping. Truncation holds the folio lock - * until after the folio is removed from the page - * cache so holding it ourselves is sufficient. + * a ->migrate_folio callback are possible to migrate + * without blocking. + * + * Folios from unmovable mappings are not migratable. + * + * However, we can be racing with truncation, which can + * free the mapping that we need to check. Truncation + * holds the folio lock until after the folio is removed + * from the page so holding it ourselves is sufficient. + * + * To avoid locking the folio just to check unmovable, + * assume every unmovable folio is also unevictable, + * which is a cheaper test. If our assumption goes + * wrong, it's not a correctness bug, just potentially + * wasted cycles. */ if (!folio_trylock(folio)) goto isolate_fail_put; mapping = folio_mapping(folio); - migrate_dirty = !mapping || - mapping->a_ops->migrate_folio; + if ((mode & ISOLATE_ASYNC_MIGRATE) && is_dirty) { + migrate_dirty = !mapping || + mapping->a_ops->migrate_folio; + } + is_unmovable = mapping && mapping_unmovable(mapping); folio_unlock(folio); - if (!migrate_dirty) + if (!migrate_dirty || is_unmovable) goto isolate_fail_put; } diff --git a/mm/migrate.c b/mm/migrate.c index 2053b54556ca..ed874e43ecd7 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -956,6 +956,8 @@ static int move_to_new_folio(struct folio *dst, struct folio *src, if (!mapping) rc = migrate_folio(mapping, dst, src, mode); + else if (mapping_unmovable(mapping)) + rc = -EOPNOTSUPP; else if (mapping->a_ops->migrate_folio) /* * Most folios have a mapping and most filesystems From patchwork Fri Oct 27 18:21:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438942 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 4E0F3C25B6F for ; Fri, 27 Oct 2023 18:23:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E87AC80017; Fri, 27 Oct 2023 14:22:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0F628000C; Fri, 27 Oct 2023 14:22:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C14A780017; Fri, 27 Oct 2023 14:22:58 -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 A74A48000C for ; Fri, 27 Oct 2023 14:22:58 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 87346409A8 for ; Fri, 27 Oct 2023 18:22:58 +0000 (UTC) X-FDA: 81392062836.01.FB0F58F Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf01.hostedemail.com (Postfix) with ESMTP id B584A40009 for ; Fri, 27 Oct 2023 18:22:56 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=YIUAHct2; spf=pass (imf01.hostedemail.com: domain of 3__87ZQYKCCAOA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--seanjc.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3__87ZQYKCCAOA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430976; a=rsa-sha256; cv=none; b=zCKOhTtkohEUhwVNTmzGZfUpaLYcUK6UOyvPIy9CCzXPNPb/JByLqYdRPxm3TNBZgefo20 QW1hnicHrPFslYvFuB1BBYQ1hLKMeGh/q0o9Sevbngrin4XAL7kVuLUa/54zzY8Q7WX9NP Noh/u7x9bFRD/khz3TQVRt0dc70kIg0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=YIUAHct2; spf=pass (imf01.hostedemail.com: domain of 3__87ZQYKCCAOA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--seanjc.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3__87ZQYKCCAOA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@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=1698430976; 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=pX02ozc7lSRzYa356XIJ+CMNCgv5fZ3NEfG1CClMU1M=; b=gb7TVHEbDf8jz1JI/D8bwh6rDregc2DfExsx9+ijruQtpSsvc3nTjxAWADaNlwAuFpLlse PR88pp+XsgJkrsTLa/B0jN8FG5JspkMkNMEwTSJSJnmkxyliiyKQDDM83hwFi96TSrHGfl jKF4ItsK+CdmO2KDjImhJIaPUL4zMso= Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5a08e5c7debso2037587a12.2 for ; Fri, 27 Oct 2023 11:22:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430975; x=1699035775; 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=pX02ozc7lSRzYa356XIJ+CMNCgv5fZ3NEfG1CClMU1M=; b=YIUAHct2soTsAC+Ts4w0wWcMItSL61G/cqpZu34ATIDXeN6KIOjEZCOX4PT4qX0V9T dNMe2kio8hxufDsL8dCZAuXUvpGUV7lQ0in0VssNBQUXxPonSuwmIP32uXRiRW2FwDxS q2OV9K9nejJnnOfztAxCBtnEdKIcRXhd15qzFNRUiuY24xlP7U+5Okod8M7akjjaDhJx h4jdSDgsjlPKbJ1WzraBs5V3OGerAw4FEpIm/ZvoWIkCRW7u2MYd7X9FgR3KHbrhlamB s3wlcECgGMAC+UWSJPpBehxu0iVvhiffS0gSDOsuL+UfjFLNPCdMsDMOCO9wlV8w924a kAtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430975; x=1699035775; 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=pX02ozc7lSRzYa356XIJ+CMNCgv5fZ3NEfG1CClMU1M=; b=accILOhGXbW42vKGXOm3h3GMkuox2509GpaJInVg787MsgRzXV8oBmafAt2K3XkRPu pmc5rLqxEqdqKPIaK2jJ9X80ZCzTyBUs51O8c0MxsGO4Iwd7Iw4/yUJeY/qQKKoTa3Jn 3QTFTwYclvKALNxiCXsHHfcw/8izFMvVfrpE1HG/CSdLNPzQfK/KqVJA5pbsmWpItRj4 Qto+pp2r+CoDIASJD4sRlLJIRqBHhZkYOCJGqa3HQQUXQQV8cZ+L5IZCEbkiB/xl/OIH 8rjjtk5WFqv7TyEnVTihmh+WoXVXDz9EgnWke0p9a7AZ4iskP/gSyPe6N0WSsa9QXgI4 ZgkQ== X-Gm-Message-State: AOJu0YygTy0ehdSJPUpuGVxlDDeuCgm8aZTlbUZ26J/KPpiE229/tO+Q k7LKd3pZQ6dq4S9pxq+IH3a5xHEYv/8= X-Google-Smtp-Source: AGHT+IEZ2DkPV4b8zDYsItSE8S6PNMt34EGJVQ4YpOaZs5hBhRUHv0wBQpomiC7JG41zMwbv7hAHGw5rsZI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f7d6:b0:1cc:23d2:bb94 with SMTP id h22-20020a170902f7d600b001cc23d2bb94mr38516plw.1.1698430975510; Fri, 27 Oct 2023 11:22:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:57 -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-16-seanjc@google.com> Subject: [PATCH v13 15/35] fs: Export anon_inode_getfile_secure() for use by KVM 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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: B584A40009 X-Stat-Signature: 99yciz4b5ic8c384p9woo3cgdkkmbe89 X-Rspam-User: X-HE-Tag: 1698430976-418276 X-HE-Meta: U2FsdGVkX19XeT9Ph+OLbcV3EwVOlc2QK9Ry//Ivf8nGSEpKzOC1spodyW+P+yRMdeWjeFoVxHEC/kVNSgoy/v5MOjd9wjbnNNyuknanb9q/heKo06u0rm63NIolPO/Fgwhq5u+lmIXGQt8lBSRM4/rKkohk44ZIBD6+TylZYES8nbQS8sThRdGZDLzGZNIiceUX722eJk+EMPlf0cJqZcD1glbuUZeFB6ZoYljsnqZ8sopD6j9i3m0lVNoLf51ez6A7F5SlODWmDKbg830aJdg1U4heYXxE7zGhSrcZKlKWwKNNjXtPIzy6sFRQGBzQqq7a7KYE2VPHABIXt4+QUMsqsDri1iog/IhiedRExwEVcL0yBTDDGTIzdCI/YZc9QwneMmRzJzrzRcbyu04nBZ2qajWnFibVxDX7GJzWFI/bTdzZE1uIdvYMqdSG3G+uWQmbotPs0rmdSqdD7QlDm2wREY1utOEsgIKupH6KgLzGroqjsOnPPF1C+Gx66niHzNWcR3dRGX2KTyGoj6OO/FLr5GVa7aK3umfBQpdmYZHdZWL6ryZ0Mr/analfIh6jUknbeJ4FEFcw2KRp3qZlHi/ReY0NMWUqcCPrRsc71d0sl4qEVGAdUp/JQSfZFge/yxpViHaaFO2yCyS1QLIFcdXHbaOL0rFlWGQG1x+ogw+rb6HeUWGimnUjgCLJfYQ00lnKWkdrNYqRFUGTgs8w31LITQcbiI/sn7Q++ePE5M14T6koXtLOHhye0iYKvijy+3QIfcbgZbcDIhzOQLNRRONSqs9jo+R2IQQllOoz9exz/wMvTmyUYtRzEYXwGplEr9kNWI0PntJEjbSQ2FWOQN/PX6SkkrWS9Wau+3Kc0dnS0PwGrXLgnagNKu7vgDh/4SoEjKrjKIfPqXFsMeSP+mqQCXTyPasC8W+VJqiLtlOawLTXEqtqZy2cvEpbAR+1ob+83d0rMODi++dEN35 VZT8aLWe WjX5Tln3heEBuMRYvwi06srnUFeISLVOo3IE90xdcdc1+cHh2zqonVo6pILnnQ4kv8Iq7R3wMr3vGFc4mLNg8/2boYzpvryRoq7c12+Tim6UN1B4heO2hNbXc60TxWubnwB9/FGFxcrn3DNbDGzT8pif+qd+KMJY9EVFwXY1UEjSF2AzjsDUKY06wJ5fQB1LO6ZLV8t1FdjSBXEyQe+P6Pf+Tf14/abyDv316sfhN806ts48drjUux0Tnhn80z33trnsgDkV38aqnC5Yfup/WrT/6C/hApllmKBJ9BkkemAXPeGe95bH/VNKItRvtt8qCM10imy2/rHvJFqdAUZxWesr8TPb1KYgnb6HlM4x/yA6mOdfMvlqSEom2KWy3UlF9P0BcaOZv0lGpclILhVGDFAYUmyFB5lCjmyBpP5Q7OTbDSDGfhMl3WKwA9wZOnsdFL/mghEo8KmPaxWQU6F8VkJ6G+Rtehpdc40m5g2vGsEkgd4J7A4oddk1365Hedd6Ap4q9Gxo8QS1s9cKRG1eJeKVOSXU5y2/X/UmbQGzXsUp4FYAc1nLm8q4bKdF7nR76mQlUq4cOQQy0mhcQc219uJ+I+JZBSwI9ofLLuYz+bh3GfgwqZC7ASCq+JhJNoc3J3MtjfF/IXdLWPExzaNzj+kMnUqZsROkS9EZaB/2tD/sryV62gTAd9sVDdCkYZzx3IyI3B81Cz/Iz+DlFTVdKAs+FWFsCY8k3hdfZSDmpF9eF4cEBgvSxFifSdvLJW9PrTgLK2PisWT3PfADXtGch0JeQz11xFbSnE/mFXgEoIgKBeAXDtpZhhWZFrA== 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: Export anon_inode_getfile_secure() so that it can be used by KVM to create and manage file-based guest memory without need a fullblow filesystem. The "standard" anon_inode_getfd() doesn't work for KVM's use case as KVM needs a unique inode for each file, e.g. to be able to independently manage the size and lifecycle of a given file. Note, KVM doesn't need a "secure" version, just unique inodes, i.e. ignore the name. Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini --- fs/anon_inodes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/anon_inodes.c b/fs/anon_inodes.c index 24192a7667ed..4190336180ee 100644 --- a/fs/anon_inodes.c +++ b/fs/anon_inodes.c @@ -176,6 +176,7 @@ struct file *anon_inode_getfile_secure(const char *name, return __anon_inode_getfile(name, fops, priv, flags, context_inode, true); } +EXPORT_SYMBOL_GPL(anon_inode_getfile_secure); static int __anon_inode_getfd(const char *name, const struct file_operations *fops, From patchwork Fri Oct 27 18:21:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438943 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 09AF1C25B6F for ; Fri, 27 Oct 2023 18:23:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 90CAE80019; Fri, 27 Oct 2023 14:23:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F5008000C; Fri, 27 Oct 2023 14:23:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5824880019; Fri, 27 Oct 2023 14:23:02 -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 392EA8000C for ; Fri, 27 Oct 2023 14:23:02 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0A1DA160E5B for ; Fri, 27 Oct 2023 18:23:02 +0000 (UTC) X-FDA: 81392063004.28.69A2624 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf12.hostedemail.com (Postfix) with ESMTP id 98AFF40025 for ; Fri, 27 Oct 2023 18:22:58 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tXoZsUHp; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3AQA8ZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3AQA8ZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430978; a=rsa-sha256; cv=none; b=ogVx1X4M9ShDyulwOqdVg8Tlsh79B4FfA8cfrygbTjbyOvWrwiKRc1IPeRXWUS3OfczkSX Hxy7K6NyLxzwurs/yM41LpA9/0ohB1S3KWlAUQkxCmPM0MHceFPWTYgOa5wrZH1bDCcHtu +skhw8vhGPfuPoVqx6QVcdAUhnEWCZw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tXoZsUHp; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3AQA8ZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3AQA8ZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430978; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UYJQgiZVR4iLUgC6P0Z91HbpOn98YamOrMc7NG6h1kc=; b=G8VaJuoJslJ8Jy67lQKFrCxJVCOI0g9Nrhzo2JLqiknDoNZqQ71aPLUMtXeUwo64IMscZc aEgBK5l7NJxTYrYoW6gjI/ra//SoeMkz6cngQE7nkzJjQvutwR6ou+uTeInp6sP6pXFU0t j3nfn/2fgUnyMwDTgPI6czdhXiK1X4Q= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c9f973d319so23492645ad.0 for ; Fri, 27 Oct 2023 11:22:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430977; x=1699035777; darn=kvack.org; h=content-transfer-encoding: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=UYJQgiZVR4iLUgC6P0Z91HbpOn98YamOrMc7NG6h1kc=; b=tXoZsUHp2589ZbOanZcU+v2rRF+KKHkIxD3KHr8x5Kkfyiqh9yPT49A2TRnr6L0/G3 tNpurrSPX+2iJZSyT+MnXhgTVfMehLSkKPlKfyc3Y7ZkkCY72UCNzSQX9u2pvgTL+GQQ SCuHIitqdLBYFIMCUO2PT93X2ean93grwDBWhJZbT/dYgEkSoWaZn7VItBiq9YazOMIw C+wMXoMuHRQuvCN3tV2NDG8xzCAdScNcuY7T3qDuD8sjoebg8vV7l9aE2Frx9EDZTNv0 IP88WFWmn3l0du4nOY/jJgXiUSzCAfJF1g4LFkQBmXhzb28ojRF4L22hguYuhpNiy9D1 LAeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430977; x=1699035777; h=content-transfer-encoding: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=UYJQgiZVR4iLUgC6P0Z91HbpOn98YamOrMc7NG6h1kc=; b=wqqDfJgi9hbB36hCnZFxf3+yqIvTG1OW3yGfBEBpN9D28hiSXqfCy/Zv9V4oA/WcfW J3myJVjxo1kiZsV66z6G8n2Ucx2ua1ti8PQpd99Cxm2NqCCR3UB9qg00q+CfYX70ebpq 4oAUZI7mRNJ4a2YPzErn3NVJBQicl8y9KhPddaLlEFSTBf6RMMMppzlu+uDqg6khNSwR zCPl0yd8J+gaSSs1ngOpuDY1KJhm7YeXcFUrlctsgH/l6tn6qJNMWse3Zr+GFCzj5e7L C+ug9u0dlzrEZjL+4Ty91xawRZ2kdqSSBHO/i+se9rO3//gUAxv9MUtY3biHCMCbOG0K 8cBg== X-Gm-Message-State: AOJu0YyToWc0Twbx0gYlhM5JvqcB6jTtuUVJkAbQmJkTZ8EMvn0Ayy99 CiULDJNqe5+/MHweS2ldxEYIqgZrRug= X-Google-Smtp-Source: AGHT+IFQTLCtveekXD1rlOFkw+wx7na7JcLPvjDu5+U2W4dimd59+gDznHjkZmFjwgKKy1fzp+yLv/HHt2A= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:7b8f:b0:1c9:f356:b7d5 with SMTP id w15-20020a1709027b8f00b001c9f356b7d5mr60393pll.7.1698430977569; Fri, 27 Oct 2023 11:22:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:58 -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-17-seanjc@google.com> Subject: [PATCH v13 16/35] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 98AFF40025 X-Stat-Signature: cx66bo6jwjs8p8mfohwmtiq9nqaxtib6 X-HE-Tag: 1698430978-467470 X-HE-Meta: U2FsdGVkX18IY6df37LiCwaHnpP12ntap6ZBmlG3Gbv8GPNBoquAsG4z24GkWnZD+FaalpAb+OCQWxXBb77SrykBCl/DxAvO/67ksWyVm4fncjLc+q68JRghOcvXi1X8A8Ey4D5LnScH0VSvINsXTteWaUWcLxxMC+5A/vki0jS96712krh11q1p23+LvnrqXQVhYlwdcVjD+iORfeXd43Ju3iTU/I4iZ9Ioqar36r+rVnfGFLPWixJWdInLgzbrfNy8+JpdJAef6i1rukuNkuoY70SRnj8bbmq/KifeEOJLyw/t09bDXq3YPLIkotzDMhiYC4rlHJRG3mJ36gPpR7c79eh5GWdKrpxINocpR47xSoeI1kmhxSla8+L+cWpUBAycFOYyNjA+F28MbQXE8X5l/7tZmHYsYiQjO1GJjab8y2Z/9jgGFvx4CQbssFx4/FRNzNQQTVCHotU+oBO8j/PKSMhhRK4N3qNYzDTsYEUL+NaXyD55/f6aZKGNcHt0iPa+3iKEyQB6VIbmFqSCq0dd6wgsVxzY3mWe8EnCDNU9YzYKnM564N/ORt6brJ9W5EOpgvG3QI7GKMIzw69NPB/3FNCfVJzV2k4+2YKoEoQ0Ni3lGP+WH2z07HRUC5KREWSYX2aWwaAbWiN/cnqVEwl5b3X7BymBnNgG0v2QDl2yI4qflE/9+2mDKR0htwWcwDN4xfrP7gdDFKNtAPg3ZGKwR5IRFoCyjyvkUXISQDiIyloBVsidl6txrHLuvA8VFrCd5nSYcjGnrzzeOI3/6kx8cRqezyTIe0Dgq1F9GzHnSZ0bUqSHWhZj32svw3kpF++8ETVn7QsIm1c3iwQ4EotUjgXwBAItsX0TaJ/QDh+tv6fL1h0gcapkCPlRp84elNy1pBZ/vAMOrNCcH3HX8wP8viRHzf7mZJ+UjiXZ3KEN8e55Xkjyw7q8Z7mVELKHCMhbO67K+2rLkOlyjOD bfT88hh/ EbYOf0/4lmtimMeFpbsSyxWQM23dRMZsQCfHrV9bXKsMl/juYx2IlSy3lw8rFQ28rTglca2fvw2iVfJPLX/xHdKARMJSeFUWdeD5d+SZYbXAIHPNkhOrwe3vGrD6dV55mlFQcz57cbcXWdklsQGjUBTZztW9iV3y1AICR7OAV3U+T0OjDLb+X5JIbkpS++2gCcE4gCvQsP7DDQ3Gco9oTy3ZMosOsDE8+40M2p68+Qz+O2sYaxhp3wHP4vHMam+Z0+ifbuMTBbIsI1GZYkuRqHHdArHKr8YXRgSgMx66csj7dtHKWRykOKauub4fNpL/nsm8Euu+bZUL8Ly3d4XsCKgYmKhTOh27TZiezmAk4TZgnU/29JKXN3B9z2l0pypVobx/bF+8SSRqnlB682sH7W87ufYraLMgpShcAbkSP9AA+N8qIlsRP6TSQxXt2PPpzsaz+qC3b3TjQO0rDc5ONZ5mmdLyPBQ1Xavp/3ZqAHVNlBnW7x3hAzOXnOBEPZEDCvloQ18tZ+K5CV9Y1s8Oee77hwZ8ihqCeuXzytQS8e0s9j/5VB/gR4aLzatf/AoszOyqhIQ99RCJtZ5CrpgyGDZtbwEvr2lcbDTYeZLKrXuMtYzPrRpzIIX9yyYnpwmaQeO9A6idLgzFWRkP0ZCWDhRGZ3onf34Q2slQrgEN176UWqiBOyMWoVW+VELPe0TIZpwAVLUAA+hVjf2RDVig5eIj2WXfd0rIp25rIX4NUOBnM5ovBMbrRdXtfHn6+4F/i6kYATFZuolMyrxqpQHzCQeoo+QbEZ5q7xqUUdYLRflMJfchaGYFthYXNmLeiExc+YU2mSgD7L1srngf6cfri/Mss9HqlExfDyUoAJ1UoAuO1V4Ye+NdMmwz0V18P9FbPbUvnhe92/+Ul+0JBQc5dn9/iMfaQlwcf3FGTITssFbWzBj0feZnYWMRLxYPZFCcXaaIh0ryh1NckQzFLjHxweLXh0n1k ftYSgXQP eOdCGM45bz6morivn90iQ9F9OZwl6XftMYaOE5dThRQfA1CvkRRfObs/HpIXJuuJ5Q4ZaObSFEU= 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: Introduce an ioctl(), KVM_CREATE_GUEST_MEMFD, to allow creating file-based memory that is tied to a specific KVM virtual machine and whose primary purpose is to serve guest memory. A guest-first memory subsystem allows for optimizations and enhancements that are kludgy or outright infeasible to implement/support in a generic memory subsystem. With guest_memfd, guest protections and mapping sizes are fully decoupled from host userspace mappings. E.g. KVM currently doesn't support mapping memory as writable in the guest without it also being writable in host userspace, as KVM's ABI uses VMA protections to define the allow guest protection. Userspace can fudge this by establishing two mappings, a writable mapping for the guest and readable one for itself, but that’s suboptimal on multiple fronts. Similarly, KVM currently requires the guest mapping size to be a strict subset of the host userspace mapping size, e.g. KVM doesn’t support creating a 1GiB guest mapping unless userspace also has a 1GiB guest mapping. Decoupling the mappings sizes would allow userspace to precisely map only what is needed without impacting guest performance, e.g. to harden against unintentional accesses to guest memory. Decoupling guest and userspace mappings may also allow for a cleaner alternative to high-granularity mappings for HugeTLB, which has reached a bit of an impasse and is unlikely to ever be merged. A guest-first memory subsystem also provides clearer line of sight to things like a dedicated memory pool (for slice-of-hardware VMs) and elimination of "struct page" (for offload setups where userspace _never_ needs to mmap() guest memory). More immediately, being able to map memory into KVM guests without mapping said memory into the host is critical for Confidential VMs (CoCo VMs), the initial use case for guest_memfd. While AMD's SEV and Intel's TDX prevent untrusted software from reading guest private data by encrypting guest memory with a key that isn't usable by the untrusted host, projects such as Protected KVM (pKVM) provide confidentiality and integrity *without* relying on memory encryption. And with SEV-SNP and TDX, accessing guest private memory can be fatal to the host, i.e. KVM must be prevent host userspace from accessing guest memory irrespective of hardware behavior. Attempt #1 to support CoCo VMs was to add a VMA flag to mark memory as being mappable only by KVM (or a similarly enlightened kernel subsystem). That approach was abandoned largely due to it needing to play games with PROT_NONE to prevent userspace from accessing guest memory. Attempt #2 to was to usurp PG_hwpoison to prevent the host from mapping guest private memory into userspace, but that approach failed to meet several requirements for software-based CoCo VMs, e.g. pKVM, as the kernel wouldn't easily be able to enforce a 1:1 page:guest association, let alone a 1:1 pfn:gfn mapping. And using PG_hwpoison does not work for memory that isn't backed by 'struct page', e.g. if devices gain support for exposing encrypted memory regions to guests. Attempt #3 was to extend the memfd() syscall and wrap shmem to provide dedicated file-based guest memory. That approach made it as far as v10 before feedback from Hugh Dickins and Christian Brauner (and others) led to it demise. Hugh's objection was that piggybacking shmem made no sense for KVM's use case as KVM didn't actually *want* the features provided by shmem. I.e. KVM was using memfd() and shmem to avoid having to manage memory directly, not because memfd() and shmem were the optimal solution, e.g. things like read/write/mmap in shmem were dead weight. Christian pointed out flaws with implementing a partial overlay (wrapping only _some_ of shmem), e.g. poking at inode_operations or super_operations would show shmem stuff, but address_space_operations and file_operations would show KVM's overlay. Paraphrashing heavily, Christian suggested KVM stop being lazy and create a proper API. Link: https://lore.kernel.org/all/20201020061859.18385-1-kirill.shutemov@linux.intel.com Link: https://lore.kernel.org/all/20210416154106.23721-1-kirill.shutemov@linux.intel.com Link: https://lore.kernel.org/all/20210824005248.200037-1-seanjc@google.com Link: https://lore.kernel.org/all/20211111141352.26311-1-chao.p.peng@linux.intel.com Link: https://lore.kernel.org/all/20221202061347.1070246-1-chao.p.peng@linux.intel.com Link: https://lore.kernel.org/all/ff5c5b97-acdf-9745-ebe5-c6609dd6322e@google.com Link: https://lore.kernel.org/all/20230418-anfallen-irdisch-6993a61be10b@brauner Link: https://lore.kernel.org/all/ZEM5Zq8oo+xnApW9@google.com Link: https://lore.kernel.org/linux-mm/20230306191944.GA15773@monkey Link: https://lore.kernel.org/linux-mm/ZII1p8ZHlHaQ3dDl@casper.infradead.org Cc: Fuad Tabba Cc: Vishal Annapurve Cc: Ackerley Tng Cc: Jarkko Sakkinen Cc: Maciej Szmigiero Cc: Vlastimil Babka Cc: David Hildenbrand Cc: Quentin Perret Cc: Michael Roth Cc: Wang Cc: Liam Merwick Cc: Isaku Yamahata Co-developed-by: Kirill A. Shutemov Signed-off-by: Kirill A. Shutemov Co-developed-by: Yu Zhang Signed-off-by: Yu Zhang Co-developed-by: Chao Peng Signed-off-by: Chao Peng Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Co-developed-by: Isaku Yamahata Signed-off-by: Isaku Yamahata Co-developed-by: Paolo Bonzini Signed-off-by: Paolo Bonzini Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst | 69 ++++- include/linux/kvm_host.h | 48 +++ include/uapi/linux/kvm.h | 15 +- virt/kvm/Kconfig | 4 + virt/kvm/Makefile.kvm | 1 + virt/kvm/guest_memfd.c | 548 +++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 68 +++- virt/kvm/kvm_mm.h | 26 ++ 8 files changed, 764 insertions(+), 15 deletions(-) create mode 100644 virt/kvm/guest_memfd.c diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e2252c748fd6..e82c69d5e755 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6079,6 +6079,15 @@ applied. :Parameters: struct kvm_userspace_memory_region2 (in) :Returns: 0 on success, -1 on error +KVM_SET_USER_MEMORY_REGION2 is an extension to KVM_SET_USER_MEMORY_REGION that +allows mapping guest_memfd memory into a guest. All fields shared with +KVM_SET_USER_MEMORY_REGION identically. Userspace can set KVM_MEM_PRIVATE in +flags to have KVM bind the memory region to a given guest_memfd range of +[guest_memfd_offset, guest_memfd_offset + memory_size]. The target guest_memfd +must point at a file created via KVM_CREATE_GUEST_MEMFD on the current VM, and +the target range must not be bound to any other memory region. All standard +bounds checks apply (use common sense). + :: struct kvm_userspace_memory_region2 { @@ -6087,9 +6096,24 @@ applied. __u64 guest_phys_addr; __u64 memory_size; /* bytes */ __u64 userspace_addr; /* start of the userspace allocated memory */ + __u64 guest_memfd_offset; + __u32 guest_memfd; + __u32 pad1; + __u64 pad2[14]; }; -See KVM_SET_USER_MEMORY_REGION. +A KVM_MEM_PRIVATE region _must_ have a valid guest_memfd (private memory) and +userspace_addr (shared memory). However, "valid" for userspace_addr simply +means that the address itself must be a legal userspace address. The backing +mapping for userspace_addr is not required to be valid/populated at the time of +KVM_SET_USER_MEMORY_REGION2, e.g. shared memory can be lazily mapped/allocated +on-demand. + +When mapping a gfn into the guest, KVM selects shared vs. private, i.e consumes +userspace_addr vs. guest_memfd, based on the gfn's KVM_MEMORY_ATTRIBUTE_PRIVATE +state. At VM creation time, all memory is shared, i.e. the PRIVATE attribute +is '0' for all gfns. Userspace can control whether memory is shared/private by +toggling KVM_MEMORY_ATTRIBUTE_PRIVATE via KVM_SET_MEMORY_ATTRIBUTES as needed. 4.140 KVM_SET_MEMORY_ATTRIBUTES ------------------------------- @@ -6127,6 +6151,49 @@ the state of a gfn/page as needed. The "flags" field is reserved for future extensions and must be '0'. +4.141 KVM_CREATE_GUEST_MEMFD +---------------------------- + +:Capability: KVM_CAP_GUEST_MEMFD +:Architectures: none +:Type: vm ioctl +:Parameters: struct struct kvm_create_guest_memfd(in) +:Returns: 0 on success, <0 on error + +KVM_CREATE_GUEST_MEMFD creates an anonymous file and returns a file descriptor +that refers to it. guest_memfd files are roughly analogous to files created +via memfd_create(), e.g. guest_memfd files live in RAM, have volatile storage, +and are automatically released when the last reference is dropped. Unlike +"regular" memfd_create() files, guest_memfd files are bound to their owning +virtual machine (see below), cannot be mapped, read, or written by userspace, +and cannot be resized (guest_memfd files do however support PUNCH_HOLE). + +:: + + struct kvm_create_guest_memfd { + __u64 size; + __u64 flags; + __u64 reserved[6]; + }; + +Conceptually, the inode backing a guest_memfd file represents physical memory, +i.e. is coupled to the virtual machine as a thing, not to a "struct kvm". The +file itself, which is bound to a "struct kvm", is that instance's view of the +underlying memory, e.g. effectively provides the translation of guest addresses +to host memory. This allows for use cases where multiple KVM structures are +used to manage a single virtual machine, e.g. when performing intrahost +migration of a virtual machine. + +KVM currently only supports mapping guest_memfd via KVM_SET_USER_MEMORY_REGION2, +and more specifically via the guest_memfd and guest_memfd_offset fields in +"struct kvm_userspace_memory_region2", where guest_memfd_offset is the offset +into the guest_memfd instance. For a given guest_memfd file, there can be at +most one mapping per page, i.e. binding multiple memory regions to a single +guest_memfd range is not allowed (any number of memory regions can be bound to +a single guest_memfd file, but the bound ranges must not overlap). + +See KVM_SET_USER_MEMORY_REGION2 for additional details. + 5. The kvm_run structure ======================== diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index df573229651b..7de93858054d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -591,8 +591,20 @@ struct kvm_memory_slot { u32 flags; short id; u16 as_id; + +#ifdef CONFIG_KVM_PRIVATE_MEM + struct { + struct file __rcu *file; + pgoff_t pgoff; + } gmem; +#endif }; +static inline bool kvm_slot_can_be_private(const struct kvm_memory_slot *slot) +{ + return slot && (slot->flags & KVM_MEM_PRIVATE); +} + static inline bool kvm_slot_dirty_track_enabled(const struct kvm_memory_slot *slot) { return slot->flags & KVM_MEM_LOG_DIRTY_PAGES; @@ -687,6 +699,17 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) } #endif +/* + * Arch code must define kvm_arch_has_private_mem if support for private memory + * is enabled. + */ +#if !defined(kvm_arch_has_private_mem) && !IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) +static inline bool kvm_arch_has_private_mem(struct kvm *kvm) +{ + return false; +} +#endif + struct kvm_memslots { u64 generation; atomic_long_t last_used_slot; @@ -1401,6 +1424,7 @@ void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc); void kvm_mmu_invalidate_begin(struct kvm *kvm); void kvm_mmu_invalidate_range_add(struct kvm *kvm, gfn_t start, gfn_t end); void kvm_mmu_invalidate_end(struct kvm *kvm); +bool kvm_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range); long kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg); @@ -2356,6 +2380,30 @@ bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, struct kvm_gfn_range *range); + +static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) +{ + return IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && + kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE; +} +#else +static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) +{ + return false; +} #endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +#ifdef CONFIG_KVM_PRIVATE_MEM +int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order); +#else +static inline int kvm_gmem_get_pfn(struct kvm *kvm, + struct kvm_memory_slot *slot, gfn_t gfn, + kvm_pfn_t *pfn, int *max_order) +{ + KVM_BUG_ON(1, kvm); + return -EIO; +} +#endif /* CONFIG_KVM_PRIVATE_MEM */ + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 547837feaa28..25caee8d1a80 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -102,7 +102,10 @@ struct kvm_userspace_memory_region2 { __u64 guest_phys_addr; __u64 memory_size; __u64 userspace_addr; - __u64 pad[16]; + __u64 guest_memfd_offset; + __u32 guest_memfd; + __u32 pad1; + __u64 pad2[14]; }; /* @@ -112,6 +115,7 @@ struct kvm_userspace_memory_region2 { */ #define KVM_MEM_LOG_DIRTY_PAGES (1UL << 0) #define KVM_MEM_READONLY (1UL << 1) +#define KVM_MEM_PRIVATE (1UL << 2) /* for KVM_IRQ_LINE */ struct kvm_irq_level { @@ -1212,6 +1216,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_USER_MEMORY2 230 #define KVM_CAP_MEMORY_FAULT_INFO 231 #define KVM_CAP_MEMORY_ATTRIBUTES 232 +#define KVM_CAP_GUEST_MEMFD 233 #ifdef KVM_CAP_IRQ_ROUTING @@ -2290,4 +2295,12 @@ struct kvm_memory_attributes { #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) + +struct kvm_create_guest_memfd { + __u64 size; + __u64 flags; + __u64 reserved[6]; +}; + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 5bd7fcaf9089..08afef022db9 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -100,3 +100,7 @@ config KVM_GENERIC_MMU_NOTIFIER config KVM_GENERIC_MEMORY_ATTRIBUTES select KVM_GENERIC_MMU_NOTIFIER bool + +config KVM_PRIVATE_MEM + select XARRAY_MULTI + bool diff --git a/virt/kvm/Makefile.kvm b/virt/kvm/Makefile.kvm index 2c27d5d0c367..724c89af78af 100644 --- a/virt/kvm/Makefile.kvm +++ b/virt/kvm/Makefile.kvm @@ -12,3 +12,4 @@ kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o kvm-$(CONFIG_HAVE_KVM_PFNCACHE) += $(KVM)/pfncache.o +kvm-$(CONFIG_KVM_PRIVATE_MEM) += $(KVM)/guest_memfd.o diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c new file mode 100644 index 000000000000..98a12da80214 --- /dev/null +++ b/virt/kvm/guest_memfd.c @@ -0,0 +1,548 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include + +#include "kvm_mm.h" + +struct kvm_gmem { + struct kvm *kvm; + struct xarray bindings; + struct list_head entry; +}; + +static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index) +{ + struct folio *folio; + + /* TODO: Support huge pages. */ + folio = filemap_grab_folio(inode->i_mapping, index); + if (IS_ERR_OR_NULL(folio)) + return NULL; + + /* + * Use the up-to-date flag to track whether or not the memory has been + * zeroed before being handed off to the guest. There is no backing + * storage for the memory, so the folio will remain up-to-date until + * it's removed. + * + * TODO: Skip clearing pages when trusted firmware will do it when + * assigning memory to the guest. + */ + if (!folio_test_uptodate(folio)) { + unsigned long nr_pages = folio_nr_pages(folio); + unsigned long i; + + for (i = 0; i < nr_pages; i++) + clear_highpage(folio_page(folio, i)); + + folio_mark_uptodate(folio); + } + + /* + * Ignore accessed, referenced, and dirty flags. The memory is + * unevictable and there is no storage to write back to. + */ + return folio; +} + +static void kvm_gmem_invalidate_begin(struct kvm_gmem *gmem, pgoff_t start, + pgoff_t end) +{ + bool flush = false, found_memslot = false; + struct kvm_memory_slot *slot; + struct kvm *kvm = gmem->kvm; + unsigned long index; + + xa_for_each_range(&gmem->bindings, index, slot, start, end - 1) { + pgoff_t pgoff = slot->gmem.pgoff; + + struct kvm_gfn_range gfn_range = { + .start = slot->base_gfn + max(pgoff, start) - pgoff, + .end = slot->base_gfn + min(pgoff + slot->npages, end) - pgoff, + .slot = slot, + .may_block = true, + }; + + if (!found_memslot) { + found_memslot = true; + + KVM_MMU_LOCK(kvm); + kvm_mmu_invalidate_begin(kvm); + } + + flush |= kvm_mmu_unmap_gfn_range(kvm, &gfn_range); + } + + if (flush) + kvm_flush_remote_tlbs(kvm); + + if (found_memslot) + KVM_MMU_UNLOCK(kvm); +} + +static void kvm_gmem_invalidate_end(struct kvm_gmem *gmem, pgoff_t start, + pgoff_t end) +{ + struct kvm *kvm = gmem->kvm; + + if (xa_find(&gmem->bindings, &start, end - 1, XA_PRESENT)) { + KVM_MMU_LOCK(kvm); + kvm_mmu_invalidate_end(kvm); + KVM_MMU_UNLOCK(kvm); + } +} + +static long kvm_gmem_punch_hole(struct inode *inode, loff_t offset, loff_t len) +{ + struct list_head *gmem_list = &inode->i_mapping->private_list; + pgoff_t start = offset >> PAGE_SHIFT; + pgoff_t end = (offset + len) >> PAGE_SHIFT; + struct kvm_gmem *gmem; + + /* + * Bindings must stable across invalidation to ensure the start+end + * are balanced. + */ + filemap_invalidate_lock(inode->i_mapping); + + list_for_each_entry(gmem, gmem_list, entry) + kvm_gmem_invalidate_begin(gmem, start, end); + + truncate_inode_pages_range(inode->i_mapping, offset, offset + len - 1); + + list_for_each_entry(gmem, gmem_list, entry) + kvm_gmem_invalidate_end(gmem, start, end); + + filemap_invalidate_unlock(inode->i_mapping); + + return 0; +} + +static long kvm_gmem_allocate(struct inode *inode, loff_t offset, loff_t len) +{ + struct address_space *mapping = inode->i_mapping; + pgoff_t start, index, end; + int r; + + /* Dedicated guest is immutable by default. */ + if (offset + len > i_size_read(inode)) + return -EINVAL; + + filemap_invalidate_lock_shared(mapping); + + start = offset >> PAGE_SHIFT; + end = (offset + len) >> PAGE_SHIFT; + + r = 0; + for (index = start; index < end; ) { + struct folio *folio; + + if (signal_pending(current)) { + r = -EINTR; + break; + } + + folio = kvm_gmem_get_folio(inode, index); + if (!folio) { + r = -ENOMEM; + break; + } + + index = folio_next_index(folio); + + folio_unlock(folio); + folio_put(folio); + + /* 64-bit only, wrapping the index should be impossible. */ + if (WARN_ON_ONCE(!index)) + break; + + cond_resched(); + } + + filemap_invalidate_unlock_shared(mapping); + + return r; +} + +static long kvm_gmem_fallocate(struct file *file, int mode, loff_t offset, + loff_t len) +{ + int ret; + + if (!(mode & FALLOC_FL_KEEP_SIZE)) + return -EOPNOTSUPP; + + if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) + return -EOPNOTSUPP; + + if (!PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len)) + return -EINVAL; + + if (mode & FALLOC_FL_PUNCH_HOLE) + ret = kvm_gmem_punch_hole(file_inode(file), offset, len); + else + ret = kvm_gmem_allocate(file_inode(file), offset, len); + + if (!ret) + file_modified(file); + return ret; +} + +static int kvm_gmem_release(struct inode *inode, struct file *file) +{ + struct kvm_gmem *gmem = file->private_data; + struct kvm_memory_slot *slot; + struct kvm *kvm = gmem->kvm; + unsigned long index; + + /* + * Prevent concurrent attempts to *unbind* a memslot. This is the last + * reference to the file and thus no new bindings can be created, but + * dereferencing the slot for existing bindings needs to be protected + * against memslot updates, specifically so that unbind doesn't race + * and free the memslot (kvm_gmem_get_file() will return NULL). + */ + mutex_lock(&kvm->slots_lock); + + filemap_invalidate_lock(inode->i_mapping); + + xa_for_each(&gmem->bindings, index, slot) + rcu_assign_pointer(slot->gmem.file, NULL); + + synchronize_rcu(); + + /* + * All in-flight operations are gone and new bindings can be created. + * Zap all SPTEs pointed at by this file. Do not free the backing + * memory, as its lifetime is associated with the inode, not the file. + */ + kvm_gmem_invalidate_begin(gmem, 0, -1ul); + kvm_gmem_invalidate_end(gmem, 0, -1ul); + + list_del(&gmem->entry); + + filemap_invalidate_unlock(inode->i_mapping); + + mutex_unlock(&kvm->slots_lock); + + xa_destroy(&gmem->bindings); + kfree(gmem); + + kvm_put_kvm(kvm); + + return 0; +} + +static struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot) +{ + struct file *file; + + rcu_read_lock(); + + file = rcu_dereference(slot->gmem.file); + if (file && !get_file_rcu(file)) + file = NULL; + + rcu_read_unlock(); + + return file; +} + +static struct file_operations kvm_gmem_fops = { + .open = generic_file_open, + .release = kvm_gmem_release, + .fallocate = kvm_gmem_fallocate, +}; + +void kvm_gmem_init(struct module *module) +{ + kvm_gmem_fops.owner = module; +} + +static int kvm_gmem_migrate_folio(struct address_space *mapping, + struct folio *dst, struct folio *src, + enum migrate_mode mode) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} + +static int kvm_gmem_error_page(struct address_space *mapping, struct page *page) +{ + struct list_head *gmem_list = &mapping->private_list; + struct kvm_gmem *gmem; + pgoff_t start, end; + + filemap_invalidate_lock_shared(mapping); + + start = page->index; + end = start + thp_nr_pages(page); + + list_for_each_entry(gmem, gmem_list, entry) + kvm_gmem_invalidate_begin(gmem, start, end); + + /* + * Do not truncate the range, what action is taken in response to the + * error is userspace's decision (assuming the architecture supports + * gracefully handling memory errors). If/when the guest attempts to + * access a poisoned page, kvm_gmem_get_pfn() will return -EHWPOISON, + * at which point KVM can either terminate the VM or propagate the + * error to userspace. + */ + + list_for_each_entry(gmem, gmem_list, entry) + kvm_gmem_invalidate_end(gmem, start, end); + + filemap_invalidate_unlock_shared(mapping); + + return MF_DELAYED; +} + +static const struct address_space_operations kvm_gmem_aops = { + .dirty_folio = noop_dirty_folio, +#ifdef CONFIG_MIGRATION + .migrate_folio = kvm_gmem_migrate_folio, +#endif + .error_remove_page = kvm_gmem_error_page, +}; + +static int kvm_gmem_getattr(struct mnt_idmap *idmap, const struct path *path, + struct kstat *stat, u32 request_mask, + unsigned int query_flags) +{ + struct inode *inode = path->dentry->d_inode; + + /* TODO */ + generic_fillattr(idmap, request_mask, inode, stat); + return 0; +} + +static int kvm_gmem_setattr(struct mnt_idmap *idmap, struct dentry *dentry, + struct iattr *attr) +{ + /* TODO */ + return -EINVAL; +} +static const struct inode_operations kvm_gmem_iops = { + .getattr = kvm_gmem_getattr, + .setattr = kvm_gmem_setattr, +}; + +static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) +{ + const char *anon_name = "[kvm-gmem]"; + struct kvm_gmem *gmem; + struct inode *inode; + struct file *file; + int fd, err; + + fd = get_unused_fd_flags(0); + if (fd < 0) + return fd; + + gmem = kzalloc(sizeof(*gmem), GFP_KERNEL); + if (!gmem) { + err = -ENOMEM; + goto err_fd; + } + + /* + * Use the so called "secure" variant, which creates a unique inode + * instead of reusing a single inode. Each guest_memfd instance needs + * its own inode to track the size, flags, etc. + */ + file = anon_inode_getfile_secure(anon_name, &kvm_gmem_fops, gmem, + O_RDWR, NULL); + if (IS_ERR(file)) { + err = PTR_ERR(file); + goto err_gmem; + } + + file->f_flags |= O_LARGEFILE; + + inode = file->f_inode; + WARN_ON(file->f_mapping != inode->i_mapping); + + inode->i_private = (void *)(unsigned long)flags; + inode->i_op = &kvm_gmem_iops; + inode->i_mapping->a_ops = &kvm_gmem_aops; + inode->i_mode |= S_IFREG; + inode->i_size = size; + mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); + mapping_set_unmovable(inode->i_mapping); + /* Unmovable mappings are supposed to be marked unevictable as well. */ + WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); + + kvm_get_kvm(kvm); + gmem->kvm = kvm; + xa_init(&gmem->bindings); + list_add(&gmem->entry, &inode->i_mapping->private_list); + + fd_install(fd, file); + return fd; + +err_gmem: + kfree(gmem); +err_fd: + put_unused_fd(fd); + return err; +} + +int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) +{ + loff_t size = args->size; + u64 flags = args->flags; + u64 valid_flags = 0; + + if (flags & ~valid_flags) + return -EINVAL; + + if (size < 0 || !PAGE_ALIGNED(size)) + return -EINVAL; + + return __kvm_gmem_create(kvm, size, flags); +} + +int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, + unsigned int fd, loff_t offset) +{ + loff_t size = slot->npages << PAGE_SHIFT; + unsigned long start, end; + struct kvm_gmem *gmem; + struct inode *inode; + struct file *file; + + BUILD_BUG_ON(sizeof(gfn_t) != sizeof(slot->gmem.pgoff)); + + file = fget(fd); + if (!file) + return -EBADF; + + if (file->f_op != &kvm_gmem_fops) + goto err; + + gmem = file->private_data; + if (gmem->kvm != kvm) + goto err; + + inode = file_inode(file); + + if (offset < 0 || !PAGE_ALIGNED(offset)) + return -EINVAL; + + if (offset + size > i_size_read(inode)) + goto err; + + filemap_invalidate_lock(inode->i_mapping); + + start = offset >> PAGE_SHIFT; + end = start + slot->npages; + + if (!xa_empty(&gmem->bindings) && + xa_find(&gmem->bindings, &start, end - 1, XA_PRESENT)) { + filemap_invalidate_unlock(inode->i_mapping); + goto err; + } + + /* + * No synchronize_rcu() needed, any in-flight readers are guaranteed to + * be see either a NULL file or this new file, no need for them to go + * away. + */ + rcu_assign_pointer(slot->gmem.file, file); + slot->gmem.pgoff = start; + + xa_store_range(&gmem->bindings, start, end - 1, slot, GFP_KERNEL); + filemap_invalidate_unlock(inode->i_mapping); + + /* + * Drop the reference to the file, even on success. The file pins KVM, + * not the other way 'round. Active bindings are invalidated if the + * file is closed before memslots are destroyed. + */ + fput(file); + return 0; + +err: + fput(file); + return -EINVAL; +} + +void kvm_gmem_unbind(struct kvm_memory_slot *slot) +{ + unsigned long start = slot->gmem.pgoff; + unsigned long end = start + slot->npages; + struct kvm_gmem *gmem; + struct file *file; + + /* + * Nothing to do if the underlying file was already closed (or is being + * closed right now), kvm_gmem_release() invalidates all bindings. + */ + file = kvm_gmem_get_file(slot); + if (!file) + return; + + gmem = file->private_data; + + filemap_invalidate_lock(file->f_mapping); + xa_store_range(&gmem->bindings, start, end - 1, NULL, GFP_KERNEL); + rcu_assign_pointer(slot->gmem.file, NULL); + synchronize_rcu(); + filemap_invalidate_unlock(file->f_mapping); + + fput(file); +} + +int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +{ + pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; + struct kvm_gmem *gmem; + struct folio *folio; + struct page *page; + struct file *file; + int r; + + file = kvm_gmem_get_file(slot); + if (!file) + return -EFAULT; + + gmem = file->private_data; + + if (WARN_ON_ONCE(xa_load(&gmem->bindings, index) != slot)) { + r = -EIO; + goto out_fput; + } + + folio = kvm_gmem_get_folio(file_inode(file), index); + if (!folio) { + r = -ENOMEM; + goto out_fput; + } + + if (folio_test_hwpoison(folio)) { + r = -EHWPOISON; + goto out_unlock; + } + + page = folio_file_page(folio, index); + + *pfn = page_to_pfn(page); + if (max_order) + *max_order = 0; + + r = 0; + +out_unlock: + folio_unlock(folio); +out_fput: + fput(file); + + return r; +} +EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 78a0b09ef2a5..5d1a2f1b4e94 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -798,7 +798,7 @@ void kvm_mmu_invalidate_range_add(struct kvm *kvm, gfn_t start, gfn_t end) } } -static bool kvm_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) +bool kvm_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range) { kvm_mmu_invalidate_range_add(kvm, range->start, range->end); return kvm_unmap_gfn_range(kvm, range); @@ -1034,6 +1034,9 @@ static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) /* This does not remove the slot from struct kvm_memslots data structures */ static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) { + if (slot->flags & KVM_MEM_PRIVATE) + kvm_gmem_unbind(slot); + kvm_destroy_dirty_bitmap(slot); kvm_arch_free_memslot(kvm, slot); @@ -1605,10 +1608,18 @@ static void kvm_replace_memslot(struct kvm *kvm, } } -static int check_memory_region_flags(const struct kvm_userspace_memory_region2 *mem) +static int check_memory_region_flags(struct kvm *kvm, + const struct kvm_userspace_memory_region2 *mem) { u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; + if (kvm_arch_has_private_mem(kvm)) + valid_flags |= KVM_MEM_PRIVATE; + + /* Dirty logging private memory is not currently supported. */ + if (mem->flags & KVM_MEM_PRIVATE) + valid_flags &= ~KVM_MEM_LOG_DIRTY_PAGES; + #ifdef __KVM_HAVE_READONLY_MEM valid_flags |= KVM_MEM_READONLY; #endif @@ -2017,7 +2028,7 @@ int __kvm_set_memory_region(struct kvm *kvm, int as_id, id; int r; - r = check_memory_region_flags(mem); + r = check_memory_region_flags(kvm, mem); if (r) return r; @@ -2036,6 +2047,10 @@ int __kvm_set_memory_region(struct kvm *kvm, !access_ok((void __user *)(unsigned long)mem->userspace_addr, mem->memory_size)) return -EINVAL; + if (mem->flags & KVM_MEM_PRIVATE && + (mem->guest_memfd_offset & (PAGE_SIZE - 1) || + mem->guest_memfd_offset + mem->memory_size < mem->guest_memfd_offset)) + return -EINVAL; if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM) return -EINVAL; if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) @@ -2074,6 +2089,9 @@ int __kvm_set_memory_region(struct kvm *kvm, if ((kvm->nr_memslot_pages + npages) < kvm->nr_memslot_pages) return -EINVAL; } else { /* Modify an existing slot. */ + /* Private memslots are immutable, they can only be deleted. */ + if (mem->flags & KVM_MEM_PRIVATE) + return -EINVAL; if ((mem->userspace_addr != old->userspace_addr) || (npages != old->npages) || ((mem->flags ^ old->flags) & KVM_MEM_READONLY)) @@ -2102,10 +2120,23 @@ int __kvm_set_memory_region(struct kvm *kvm, new->npages = npages; new->flags = mem->flags; new->userspace_addr = mem->userspace_addr; + if (mem->flags & KVM_MEM_PRIVATE) { + r = kvm_gmem_bind(kvm, new, mem->guest_memfd, mem->guest_memfd_offset); + if (r) + goto out; + } r = kvm_set_memslot(kvm, old, new, change); if (r) - kfree(new); + goto out_unbind; + + return 0; + +out_unbind: + if (mem->flags & KVM_MEM_PRIVATE) + kvm_gmem_unbind(new); +out: + kfree(new); return r; } EXPORT_SYMBOL_GPL(__kvm_set_memory_region); @@ -2441,7 +2472,7 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, static u64 kvm_supported_mem_attributes(struct kvm *kvm) { - if (!kvm) + if (!kvm || kvm_arch_has_private_mem(kvm)) return KVM_MEMORY_ATTRIBUTE_PRIVATE; return 0; @@ -4852,14 +4883,11 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) return 1; #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES case KVM_CAP_MEMORY_ATTRIBUTES: - u64 attrs = kvm_supported_mem_attributes(kvm); - - r = -EFAULT; - if (copy_to_user(argp, &attrs, sizeof(attrs))) - goto out; - r = 0; - break; - } + return kvm_supported_mem_attributes(kvm); +#endif +#ifdef CONFIG_KVM_PRIVATE_MEM + case KVM_CAP_GUEST_MEMFD: + return !kvm || kvm_arch_has_private_mem(kvm); #endif default: break; @@ -5282,6 +5310,18 @@ static long kvm_vm_ioctl(struct file *filp, case KVM_GET_STATS_FD: r = kvm_vm_ioctl_get_stats_fd(kvm); break; +#ifdef CONFIG_KVM_PRIVATE_MEM + case KVM_CREATE_GUEST_MEMFD: { + struct kvm_create_guest_memfd guest_memfd; + + r = -EFAULT; + if (copy_from_user(&guest_memfd, argp, sizeof(guest_memfd))) + goto out; + + r = kvm_gmem_create(kvm, &guest_memfd); + break; + } +#endif default: r = kvm_arch_vm_ioctl(filp, ioctl, arg); } @@ -6414,6 +6454,8 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) if (WARN_ON_ONCE(r)) goto err_vfio; + kvm_gmem_init(module); + /* * Registration _must_ be the very last thing done, as this exposes * /dev/kvm to userspace, i.e. all infrastructure must be setup! diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index 180f1a09e6ba..ecefc7ec51af 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -37,4 +37,30 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, } #endif /* HAVE_KVM_PFNCACHE */ +#ifdef CONFIG_KVM_PRIVATE_MEM +void kvm_gmem_init(struct module *module); +int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args); +int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, + unsigned int fd, loff_t offset); +void kvm_gmem_unbind(struct kvm_memory_slot *slot); +#else +static inline void kvm_gmem_init(struct module *module) +{ + +} + +static inline int kvm_gmem_bind(struct kvm *kvm, + struct kvm_memory_slot *slot, + unsigned int fd, loff_t offset) +{ + WARN_ON_ONCE(1); + return -EIO; +} + +static inline void kvm_gmem_unbind(struct kvm_memory_slot *slot) +{ + WARN_ON_ONCE(1); +} +#endif /* CONFIG_KVM_PRIVATE_MEM */ + #endif /* __KVM_MM_H__ */ From patchwork Fri Oct 27 18:21: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: 13438944 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 14F99C25B47 for ; Fri, 27 Oct 2023 18:23:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3E708000C; Fri, 27 Oct 2023 14:23:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ABFE78001A; Fri, 27 Oct 2023 14:23:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 905EC80018; Fri, 27 Oct 2023 14:23:02 -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 52DE180018 for ; Fri, 27 Oct 2023 14:23:02 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2888712094B for ; Fri, 27 Oct 2023 18:23:02 +0000 (UTC) X-FDA: 81392063004.27.BB71127 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf01.hostedemail.com (Postfix) with ESMTP id 4F57440006 for ; Fri, 27 Oct 2023 18:23:00 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Xdvmt+aA; spf=pass (imf01.hostedemail.com: domain of 3AwA8ZQYKCCQSEANJCGOOGLE.COMLINUX-MMKVACK.ORG@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3AwA8ZQYKCCQSEANJCGOOGLE.COMLINUX-MMKVACK.ORG@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430980; a=rsa-sha256; cv=none; b=O2apP34ZaHqSMPC5LVNXjJ5xLRIueg/qR6dRxzrp1x2voMGaRag+pkGB1YkMYTgqkIwnV7 3TuStOJZHb+sOwqtE5D1uxOz1Jr7hh2hmAwG5lYoDJ3fmPKeTs/PKKCJgttL9+B21al7+J o0VpwYxoydmyZhxAWumwO99jdl97Xos= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Xdvmt+aA; spf=pass (imf01.hostedemail.com: domain of 3AwA8ZQYKCCQSEANJCGOOGLE.COMLINUX-MMKVACK.ORG@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3AwA8ZQYKCCQSEANJCGOOGLE.COMLINUX-MMKVACK.ORG@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=1698430980; 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=MVLj5FN4ghSPcx7W6m0P6aKU6EoKDZB5qQvPMI5sLXI=; b=E6r6Vj12QH+ILAKdEEUubkhIT9ZUsixg39WkUJrt1YD0UPWmt6IpgrCki95xA+n88sb/Ew HdYAJSIqHA+TpODkPoQAhmixguyhU5XFzeyFZt1dTbuE+ebK806McgTBTADJQlhwnGlzC4 KIy9CGy7GVL6EZdKKnRmKNuOG/Mc5Tw= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1caaaa873efso23352625ad.3 for ; Fri, 27 Oct 2023 11:23:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430979; x=1699035779; 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=MVLj5FN4ghSPcx7W6m0P6aKU6EoKDZB5qQvPMI5sLXI=; b=Xdvmt+aANwcgboM/XFSO9UEiEb/fm/2idy/YalbwLomYR/za/8L2ON7z/ba9Qe2ywl daw+GoHNOiRmpZHTopHGVrsf6QyANq8WHM+p8l4DOaeUiIZeLknaMwZqddQZ/P/kwMP5 Pu6gBCywHggJD4B+k63v0ab3kBQYGRBbftEkIj8SDMMSBxJpDYdZ19BhXhsmVoQ18vdg vxIuoQ8nwMYgUGGvG86A1xB8XJldj9yLHgKOtFEaU78Ah+JFMwIVk4an5sfOvxg58W4Z mZXmbnmgTZM8kwRkQ5uLHTiDnjG8fVPQpCycZ1xIBK32r5h35e5xqh6k+uRGaNd65CZp iNyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430979; x=1699035779; 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=MVLj5FN4ghSPcx7W6m0P6aKU6EoKDZB5qQvPMI5sLXI=; b=e3NVqGc92VNkxttk305FevC1OwL6N7UNbD2xF6FbEMw/W87YLfKnsc1cgzkPzWat49 27D4DbejkhC9KdkrftNlrkeJGueuG1kFw8YMeJHi7xVfmDNxkPHdQt1VWKfKvcdtAP4v ceZstOV7foM5DR4WmhXVypUGLXnhpJkVB6tmPgW6zSSIP+uzBcYRXYgUtvMNZN1S+Ndv wKCC0idOL2ORuqSAQZJGhiC1FEEm9Xw/kBK2o6V2iKLln1VXpfxJ4cuVtxjwAXxFxFKI GkqALnniPoqVYVU2dt0u8YEgnf4VugCphVW8/w+uLJ+WPxl2wYppTOwvDmX1z4xnT8Tg KpUw== X-Gm-Message-State: AOJu0YwrLJE1czCswQOqS419/Ljaqy7pw0eFWyVDmaIq03HNcwebM0wj phFu7hrV8TbYdCmbUencrFQI3T51jEI= X-Google-Smtp-Source: AGHT+IEKkHZ5OGZS170UfQmbvrYiPXoVQsZfMjyX59FOqET9I4K7HFcSw9HcrHh9Pco2N8IBe4E5ffgBki8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:7b8f:b0:1c9:f356:b7d5 with SMTP id w15-20020a1709027b8f00b001c9f356b7d5mr60396pll.7.1698430979282; Fri, 27 Oct 2023 11:22:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:21:59 -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-18-seanjc@google.com> Subject: [PATCH v13 17/35] KVM: Add transparent hugepage support for dedicated guest memory 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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 4F57440006 X-Stat-Signature: n3tkcjtd43ta4stzes1feuuooyhq56k7 X-Rspam-User: X-HE-Tag: 1698430980-866086 X-HE-Meta: U2FsdGVkX18XPHHRbFFR/SmPB1wnSms0RDgbcJRQ6LSmwpF3ZYW5+kbLvPxfoyhyB4aPPQbNY1t1LzO9YHT/C/cjX5c7oNA2lFIpflCb4ScA8B+sGjvmKaVjE17ji+WPvbLma4pqDiwIsidL41eRYdyiG7xlxKNmbClv9vjtOYzaCXK8z7W9uCnZiZrbVokSJcvu4yd8cD81TDxH2wVGwFsyqhl/aN9JHGevIaR8NU8PzVtAiB3Xr5LFigLc5xTDOs9QZFIMcsryrUNSduAKHPECr7kd2txXVCBBlYXgq4i9VfDRhpwaVriI8ZYixCi8y1HCV0H28M22oyuNgYf3KJoT0tcF6+1ECdVmPfOkMhAumnvQgY9aTmTCbw+X6kQllJAIYRE7g+srpDYYJdov3ztNIXS4HIm799vSQoHyYMC4C9ybeZF/lQaQQwqd6L/MjOVOVmVEwdR5TQue/K5O6l+82BiftLmWbT/PXfMER67s3LnlIx9P/DuribZTZ996sRymIhcysBohfW5DoTTiICtxvNiurpS1EyMmozT+9xuTYpg1iqld3zDsRyYoQAX0ClzcBkP2fxoWzTG7KS1uy5eltnSNMKWJ2uidGntDPSdgamJyu3TeEHS8DMTXu6MsldEZcS+wRl6kIXD86PfYGrl3Kom+Xx/eWeVEeesLsJRrdZbmCzY3I3zgVnOPGaNDewUrPf6bsWefeFug9N0RlgswFoPPJTEY9D7UyFxgT8KVswPGX4uamLRlFNtqAU1jQDicUB27scupYT+TxbG2ZNkm5CSLZhjrZcwnOKg0in0Mq310mibJBANcwTFQ7NdbVAOJjvXz82NigFWycFnwgbtp8oZ3k+sUfUTMRfmwBLH1kJD/vRKJ3P7eki5Ly3tEWdh7SxvX4XZRrtUtAxE8xe2tfZ43u05rVMSXRJK50dE3GcROIva8UPwGNSYT7jo7/mAhSVUyI21W1eerVAN eGP2X4Bw jfzbKf/lBcH96WvlSrp8VMMIeFr0G06YfR/0zycLAGTUBCTrpOs1hBS5qy2XAPLIxkus9VzfPg4fbP3jT49LldvkK1aeE7875y3xuP3PCk7TnJQcPF7S4+OYFlq7dFMf0zDfvZiZzzumS/KS5VWwhBL97cAq2d6XmOmVFWa1zhbLVGk8/zHCIrrXSSXJxpeCEnhKFeYvx5KREODMTgvN8ql78ulFJS47Y20zxMEm/wT1jorTUt3KWmp7L2g7Wqo08n5phJoj4KgkR8R4HPay9yMxzs6MGT8CDBc9vkVPc6sDvzCiw3fnoacMHs7cIZt26vR57yC4ujcpkYlU+fEwyD7ilJeTB16+K4gf3ruvib+0OLXit5r/CGRVyeU6rZUZLlzMxbpVWxd+RM2HrTnTsXNt5fmPiywh5mydEU70vJkAV+SQkdi/jOqEzGLQHyok062JGDN229twMf/LvXJ1UmC0OzcLsUVrFLDEerOhDBfJL2fRwSxVRtkHv4EihraMVqcXYlFOE5Mz36CuJlzfdgKjYGq1wIB5DpYDaXKnlTSl6QLrl0VDQ+lRZXaZOnvQGMtTv8fq8NJivJXisSiXcazvi9OzvAnSp0fTYx0vCIjWzvgnYRMbA34encHYjvbmzmhq4XL+XQalVb8uvZRwe+FOKjSHnn8FxYLS5f9wlng9Ld6HFkFHC9qFXMT7eqMEwu3OCQuLI85e/UtXitHgLJz6tYIPR4d2RZaVfPFFNXFo+z0Cd7gBXflemcqZPdgMiQSS4qCCoA6Bhj8E= 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: Extended guest_memfd to allow backing guest memory with transparent hugepages. Require userspace to opt-in via a flag even though there's no known/anticipated use case for forcing small pages as THP is optional, i.e. to avoid ending up in a situation where userspace is unaware that KVM can't provide hugepages. For simplicity, require the guest_memfd size to be a multiple of the hugepage size, e.g. so that KVM doesn't need to do bounds checking when deciding whether or not to allocate a huge folio. When reporting the max order when KVM gets a pfn from guest_memfd, force order-0 pages if the hugepage is not fully contained by the memslot binding, e.g. if userspace requested hugepages but punches a hole in the memslot bindings in order to emulate x86's VGA hole. Signed-off-by: Sean Christopherson Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst | 7 ++++ include/uapi/linux/kvm.h | 2 + virt/kvm/guest_memfd.c | 73 ++++++++++++++++++++++++++++++---- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e82c69d5e755..7f00c310c24a 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6176,6 +6176,8 @@ and cannot be resized (guest_memfd files do however support PUNCH_HOLE). __u64 reserved[6]; }; + #define KVM_GUEST_MEMFD_ALLOW_HUGEPAGE (1ULL << 0) + Conceptually, the inode backing a guest_memfd file represents physical memory, i.e. is coupled to the virtual machine as a thing, not to a "struct kvm". The file itself, which is bound to a "struct kvm", is that instance's view of the @@ -6192,6 +6194,11 @@ most one mapping per page, i.e. binding multiple memory regions to a single guest_memfd range is not allowed (any number of memory regions can be bound to a single guest_memfd file, but the bound ranges must not overlap). +If KVM_GUEST_MEMFD_ALLOW_HUGEPAGE is set in flags, KVM will attempt to allocate +and map hugepages for the guest_memfd file. This is currently best effort. If +KVM_GUEST_MEMFD_ALLOW_HUGEPAGE is set, the size must be aligned to the maximum +transparent hugepage size supported by the kernel + See KVM_SET_USER_MEMORY_REGION2 for additional details. 5. The kvm_run structure diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 25caee8d1a80..33d542de0a61 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2303,4 +2303,6 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_GUEST_MEMFD_ALLOW_HUGEPAGE (1ULL << 0) + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 98a12da80214..94bc478c26f3 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -13,14 +13,47 @@ struct kvm_gmem { struct list_head entry; }; +static struct folio *kvm_gmem_get_huge_folio(struct inode *inode, pgoff_t index) +{ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + unsigned long huge_index = round_down(index, HPAGE_PMD_NR); + unsigned long flags = (unsigned long)inode->i_private; + struct address_space *mapping = inode->i_mapping; + gfp_t gfp = mapping_gfp_mask(mapping); + struct folio *folio; + + if (!(flags & KVM_GUEST_MEMFD_ALLOW_HUGEPAGE)) + return NULL; + + if (filemap_range_has_page(mapping, huge_index << PAGE_SHIFT, + (huge_index + HPAGE_PMD_NR - 1) << PAGE_SHIFT)) + return NULL; + + folio = filemap_alloc_folio(gfp, HPAGE_PMD_ORDER); + if (!folio) + return NULL; + + if (filemap_add_folio(mapping, folio, huge_index, gfp)) { + folio_put(folio); + return NULL; + } + + return folio; +#else + return NULL; +#endif +} + static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index) { struct folio *folio; - /* TODO: Support huge pages. */ - folio = filemap_grab_folio(inode->i_mapping, index); - if (IS_ERR_OR_NULL(folio)) - return NULL; + folio = kvm_gmem_get_huge_folio(inode, index); + if (!folio) { + folio = filemap_grab_folio(inode->i_mapping, index); + if (IS_ERR_OR_NULL(folio)) + return NULL; + } /* * Use the up-to-date flag to track whether or not the memory has been @@ -373,6 +406,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) inode->i_mode |= S_IFREG; inode->i_size = size; mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); + mapping_set_large_folios(inode->i_mapping); mapping_set_unmovable(inode->i_mapping); /* Unmovable mappings are supposed to be marked unevictable as well. */ WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); @@ -398,12 +432,21 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) u64 flags = args->flags; u64 valid_flags = 0; + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) + valid_flags |= KVM_GUEST_MEMFD_ALLOW_HUGEPAGE; + if (flags & ~valid_flags) return -EINVAL; if (size < 0 || !PAGE_ALIGNED(size)) return -EINVAL; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if ((flags & KVM_GUEST_MEMFD_ALLOW_HUGEPAGE) && + !IS_ALIGNED(size, HPAGE_PMD_SIZE)) + return -EINVAL; +#endif + return __kvm_gmem_create(kvm, size, flags); } @@ -501,7 +544,7 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *max_order) { - pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; + pgoff_t index, huge_index; struct kvm_gmem *gmem; struct folio *folio; struct page *page; @@ -514,6 +557,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gmem = file->private_data; + index = gfn - slot->base_gfn + slot->gmem.pgoff; if (WARN_ON_ONCE(xa_load(&gmem->bindings, index) != slot)) { r = -EIO; goto out_fput; @@ -533,9 +577,24 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, page = folio_file_page(folio, index); *pfn = page_to_pfn(page); - if (max_order) + if (!max_order) + goto success; + + *max_order = compound_order(compound_head(page)); + if (!*max_order) + goto success; + + /* + * The folio can be mapped with a hugepage if and only if the folio is + * fully contained by the range the memslot is bound to. Note, the + * caller is responsible for handling gfn alignment, this only deals + * with the file binding. + */ + huge_index = ALIGN(index, 1ull << *max_order); + if (huge_index < ALIGN(slot->gmem.pgoff, 1ull << *max_order) || + huge_index + (1ull << *max_order) > slot->gmem.pgoff + slot->npages) *max_order = 0; - +success: r = 0; out_unlock: From patchwork Fri Oct 27 18:22:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438945 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 EB68FC25B48 for ; Fri, 27 Oct 2023 18:23:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 733628001A; Fri, 27 Oct 2023 14:23:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BB8980018; Fri, 27 Oct 2023 14:23:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BEC18001A; Fri, 27 Oct 2023 14:23:04 -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 33AC980018 for ; Fri, 27 Oct 2023 14:23:04 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0763B809E5 for ; Fri, 27 Oct 2023 18:23:04 +0000 (UTC) X-FDA: 81392063088.10.8E15EA8 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf20.hostedemail.com (Postfix) with ESMTP id 36D531C000C for ; Fri, 27 Oct 2023 18:23:02 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=pWglAb9v; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3BQA8ZQYKCCYUGCPLEIQQING.EQONKPWZ-OOMXCEM.QTI@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3BQA8ZQYKCCYUGCPLEIQQING.EQONKPWZ-OOMXCEM.QTI@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430982; 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=QVHGnajj4HOCkrhgrdEXP5z3IZ3KkliWY6Z9SNVyio0=; b=elspQxk8ExZY6D3dmGT07zqfYDcOZvto8LrT0RWy4qYoLJrRiKM0cGDWUBWltP2bfDWkNg 7hMkBRsdl0AI8BmA5wxeJHq1/LXaI7eDSg+dv4Ik3naUDaNh7z6pv6n3P7nz9phmRTttoJ +PvShi5HXEmHZsVauI02u4aJU3V0FX8= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=pWglAb9v; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3BQA8ZQYKCCYUGCPLEIQQING.EQONKPWZ-OOMXCEM.QTI@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3BQA8ZQYKCCYUGCPLEIQQING.EQONKPWZ-OOMXCEM.QTI@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430982; a=rsa-sha256; cv=none; b=qT+1nEw1pjTdgJ0YsTzaAOBquVV/267QwlR6qD3w/NgB9f66kchIWihCH706SrawxaKtgf SJ2O/+Znm9aiLRkkf9uRTgvqr0iBV7HYEWFAph5YDSCbd7kU88f9aQJ1PJuqnKTHXqxEeC 2qfDisEJLxdGlXg/pbj/RmGtOH126LE= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5b064442464so1204107b3.3 for ; Fri, 27 Oct 2023 11:23:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430981; x=1699035781; 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=QVHGnajj4HOCkrhgrdEXP5z3IZ3KkliWY6Z9SNVyio0=; b=pWglAb9vGQYzzswSn8lfXA3nA95pDi3Fhvub2HX/5GGZaCSgj8qqJHqVzJEgdPbKJS atMCnhmYMRv3+AZhhAjOmfoHJu1RRnHnbLd6jDIMf2eJXawe90SybLUs1tJd6pB8Q77O vY0TMdrjFd+tLmKLYIfqO4K8aZQhNkT8OqUB26Po6Mrvv+RRADO3KXqwrfyjWVv559ph eQLdqgSCLRKZif0A39EoPZwrYyXwQNP0Di/T1MQt9FSEQ+UbDaJLEGPmj7LSfHshmVbz fpFTwXF0S7n11qBX178LemR7a9EDwftwkffUjyppNwcJQ3wjRkN1I/boFouxL0vBEl+C WR+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430981; x=1699035781; 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=QVHGnajj4HOCkrhgrdEXP5z3IZ3KkliWY6Z9SNVyio0=; b=OKo8TY3Ksej+xR2dZHZMtHr/tSbPgNaXF5CxvxdRzWIjbVN/lkCj93UiJl/OXL1beA NTtd4Qx0gRZ/WcN751h2HMPFFZBbXhL1xyA9y7rJBN0TmwPzC5gQxSzx7AV4Kxa+9IRK Y2IJgNBx3kWCOGxCsbUUrEGKMFpXo5OXWKeflZC2jOipQ6tuQxQZDDnofviMvnhU39x5 RavXBeK6KmPxjIjfqPa4fAU55wKpE6L5g/WxUqd3y/P9ZXTlxdPyPvV9HKCzVU0fa1fK UiEr3NPGl566rJ/Rhe/G9cplrE/qt0wHgLACm3MMPkdtE16AHYTjkT4eB3yHTD/hXzTW A1jg== X-Gm-Message-State: AOJu0Yw61YLVKi6h3MrxSS5bBZJtzDwb7BAa18taAnoTeP5kFT3nu+Wr qLCZirE11OIT9N7+A4DPqCJBWdeYMaA= X-Google-Smtp-Source: AGHT+IGhdcD7DYxvJo53MxSFMcue6wHqMJ/dJKXwjBlyW/fIhxTWfdSD8wIFRBeoqz4UflCFK1Su3DgQ67Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:da0:567d:f819 with SMTP id v3-20020a056902108300b00da0567df819mr78702ybu.10.1698430981246; Fri, 27 Oct 2023 11:23:01 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:00 -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-19-seanjc@google.com> Subject: [PATCH v13 18/35] KVM: x86: "Reset" vcpu->run->exit_reason early in KVM_RUN 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-Rspamd-Queue-Id: 36D531C000C X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 7onzu5mg7h3d9qo4krhb93k8orfgdweq X-HE-Tag: 1698430982-240589 X-HE-Meta: U2FsdGVkX18mouc2QiQgw2c0EcqNjef8eKqMRAt+7K7C+USyBjTj2D2aYrMcFGNThqVLXS5SF7Xqr23iNkyx7PDMJL1XRx1VrMwH2Xd3CELVei9YU7/9sRuwTB63WhkUolHeqdBQ2ST1GET7S6KrLHOKJ57gA2FmmghPf8fRQtgnQLZeW19JbC6rt9cjiDW0x55/wMLWFDiAmOGBO6E3LljK5StsSiI0IpzkYEfJ41iioOqMTgvxP9/Gar3ESZfflMcF7hHKPWPhpjJp78CoGHyWv4jltjOYEaVcMWxDBJ48FaGEaJWg3Oj3l8JaRdv+pQyarJZsRco6Qe6fVOWlI+xBqFnjLVCVonMR/9IZu+565Hxw1YMPOGKbDZ1wZklh4DrxIDbb9yw0kYBkCUn07Pv9yrlMNsNde0iWcJ14JYJd5AA4ofm8Ajw+2h5MyGCzRR7V2yGblGlq5QodzDPFK9ANVElLoLpxN9C4TdxfMhcOt+BooGd4/3qgH+qX4lliD5kdS1rD8w2/2xb7r2C+OSdnQ+H6bNiey2fWRP7W+DKkCTApu0zP6lUg7PEL0hgMTyCUYb3imn+uk/EMZ3SiP3EFheNx1nbu9wZdzWUfPeqDgrSOG3RsF0HT/qOSnnjCINo3nRcjuFBVIZNCaK3xi8U+3uvExJeKRPLfJDQ6aWJWDSB4c/w2vvoqTM6n4AyDlsg6+XWYl9oHb2WIneZSCCQ4fEmxKPBcDrUo4v/PpGERGRuF451zru6Yewb8bYL8qCfr5HiT5QaYGrXlzMXv3JcaC4l09ThU9T7/MjVKJWvvy3Ic0uybtZBTXQTL5U65rvfXgiEG+PPu5d6XeHnhmDuXkq0IE026DVSA4qert+eyl9ClWegbTgfEheUdXF+IKXOoOD14/N/5pqUjOP9fjd4fB0kuQ+MrgK6KGFX6f92qm7lZ6bTBHEyDIzLpQ6PC8dN5qkMrLRv67c6Ivd3 Qh5xZtJd DXEZyk+C6yN6L1EPaNW4QVzD4TUoCYqdmor8PKDi1m9LtKuJiUUdXuVhaJJxT1S29tOakw7EcNC0zePtfGzOnguDaCgsfV4inXV/I0rRcK3YcdJxJbWVjg+fDPWSZCGA8iVsVUWm4cQp1G1IUqA8BQVhmtswuKFPsCvxqxk2S+5/F9HZJHB7bN2SvAF6dlQc9MOwzdHiTJ/eUibsdsLB98Kkx79UrgEkyyqH0fVebAYxdrNQYortNOmh14SaJl6B97C9OUcV/GWXYmaV5gDRHSSI8SlKRrhEEb/dSfrkYQn7Ws8HCiOrrNEUZv2Y+X89tIAqQTJHtXoCoq75kjUFGZNhjgescEVmfCZ662Jx7WZ0F26ElYJ6+Ie/Ev2SBXKR1+6r9RWTlEeXJHeG/EUJAwor8KJOYkSqDAW2yZKQqnJA7JBg1FfYcrwRucVd/7iJKawpmkxMaSJhiPISaK8LNyqWwiMnYUMWLUG2SL5cmM6WaL9boabDjUs0HKhfHVZ5yU7WGYBT0rZYM/HyGZ39ooiLyWfRvIVHeudsdog9IVR0Fj/NGwieudo9Lm5fbmfqROrty21LU+FXwrYnobRGMheac2FXMNHe4SgRqJnRw8xzzK3Nl0kmXKqjGybXF6SkLyUAaFWkhXuah2miYcUl+lz4GPYGD783cxwiszYCq5XE6W7AYuae7ihEDXVBTmwua+yh42Ead1A2JQ+fOuVsHNBiO7KM+vVnB8Kotq1VYCtu+pmTHWuv2vFhAB44knqPDQU6pMP1UIhLcFwDO/fbl7zsIK23buI+Ml9kljbXWuyAxPT+0+u06cVu6AFP1EVLJpjI1aqjA+E6aMSB3ipVMKnU36c+wInrn0Fr88xuVuUswyJUOWEE9dS/4BQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Initialize run->exit_reason to KVM_EXIT_UNKNOWN early in KVM_RUN to reduce the probability of exiting to userspace with a stale run->exit_reason that *appears* to be valid. To support fd-based guest memory (guest memory without a corresponding userspace virtual address), KVM will exit to userspace for various memory related errors, which userspace *may* be able to resolve, instead of using e.g. BUS_MCEERR_AR. And in the more distant future, KVM will also likely utilize the same functionality to let userspace "intercept" and handle memory faults when the userspace mapping is missing, i.e. when fast gup() fails. Because many of KVM's internal APIs related to guest memory use '0' to indicate "success, continue on" and not "exit to userspace", reporting memory faults/errors to userspace will set run->exit_reason and corresponding fields in the run structure fields in conjunction with a a non-zero, negative return code, e.g. -EFAULT or -EHWPOISON. And because KVM already returns -EFAULT in many paths, there's a relatively high probability that KVM could return -EFAULT without setting run->exit_reason, in which case reporting KVM_EXIT_UNKNOWN is much better than reporting whatever exit reason happened to be in the run structure. Note, KVM must wait until after run->immediate_exit is serviced to sanitize run->exit_reason as KVM's ABI is that run->exit_reason is preserved across KVM_RUN when run->immediate_exit is true. Link: https://lore.kernel.org/all/20230908222905.1321305-1-amoorthy@google.com Link: https://lore.kernel.org/all/ZFFbwOXZ5uI%2Fgdaf@google.com Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ee3cd8c3c0ef..f41dbb1465a0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10963,6 +10963,7 @@ static int vcpu_run(struct kvm_vcpu *vcpu) { int r; + vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; vcpu->arch.l1tf_flush_l1d = true; for (;;) { From patchwork Fri Oct 27 18:22:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438946 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 6E46DC25B72 for ; Fri, 27 Oct 2023 18:23:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D010F8001B; Fri, 27 Oct 2023 14:23:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C464C80018; Fri, 27 Oct 2023 14:23:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8ADC8001B; Fri, 27 Oct 2023 14:23:06 -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 9174C80018 for ; Fri, 27 Oct 2023 14:23:06 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6609880CCB for ; Fri, 27 Oct 2023 18:23:06 +0000 (UTC) X-FDA: 81392063172.11.E607DC0 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf07.hostedemail.com (Postfix) with ESMTP id 834D240004 for ; Fri, 27 Oct 2023 18:23:04 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Fw6JjYLR; spf=pass (imf07.hostedemail.com: domain of 3BwA8ZQYKCCgWIERNGKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3BwA8ZQYKCCgWIERNGKSSKPI.GSQPMRYb-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=1698430984; 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=X28f2VSZ9uWGkcVQfgyyaL29edWIecAA+BgvPUM1/nQ=; b=jovBoPbK5sfx5GhSqlpbNX+VbQ8DsRhkq24MZYDNGJx2M1f+FJQ7vSs01BFj83M5d2Jiwq MvJJ2DSQfXk5kgheLV7X6Ra57acszuArmZ7QPuKdkB/0muNohejvPbqQiUjKhJK/rsioTl hhNJeY6BOdyk805/8GUg0q6Nr3sC6D4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430984; a=rsa-sha256; cv=none; b=Ys8tBMM4tWqV0O4iJ5qdWN3VUGTZxeRs3+JT+KbYHKiBPqk/azqQ93u0C6d0bBoTSoWEVZ 38BuxgYH1VEcmbPF9h0bmRtjM6bpAeYlPYWZtAoFz/it+uFFnREL2IDrndPFKVWgS4BRpN +menQpfHN2ipc3QTEHj9zmL27nkvhhw= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Fw6JjYLR; spf=pass (imf07.hostedemail.com: domain of 3BwA8ZQYKCCgWIERNGKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3BwA8ZQYKCCgWIERNGKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1cc1682607eso13654045ad.1 for ; Fri, 27 Oct 2023 11:23:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430983; x=1699035783; 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=X28f2VSZ9uWGkcVQfgyyaL29edWIecAA+BgvPUM1/nQ=; b=Fw6JjYLRaSFED4GHDozvd0QXdIk4pxhVXjdiWKbWFWuTrUUodcHOHV2tT9Dthxrie0 M0lKU7+PpnfFIU0cfE8kZ7F18WezAsAHNPcKhDyBQsm7scn2slZZGT0E7JgwLfS8gSk+ Ym7r2ztpT9dd+2fx+dricFB8mOqQGxMN94h5nREtgN3uuCyXIU+V6dDwBn2dR6SXTIp4 L4krbhYpew1BBQHAbKNGYrdEBH4W2bSUi8J6vCig69CC4hjQtz7uamUg9uHSfDE15twE 9fCGdpNDt2AozLnPE1KFnvraL6/h++VD0En8twjGkmEZi/5Fio8f0ej2rGtjDCLHXvEl 9f7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430983; x=1699035783; 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=X28f2VSZ9uWGkcVQfgyyaL29edWIecAA+BgvPUM1/nQ=; b=h22JIVS1XAaDWwG7v/HF/zNi/K0KSJFTKceLukbSmWaTbSrKG2V9WOfZJPvwOI8dJO HzllEEinObjfdaZ0p32Xy4a79RAqkckMQO+PzqeKTwiiCaYTAUCaTYkgNwQk1FvxJWCG 7Qlmbl6hesmZoRM2gRyKq60ptDk+yXrSkEfWbLIFTWOPjjwXEL8xK5BhaYYRdH3sj51S 7FfvV5BAiwYokFWv4eYWjdpFJZSzzfsChXZACWB7OmWXLRsdOrX4K+Raa0GcUqqzuwuy dFWbasBq+KkUCVfuSzijsXpE81ghTl8To4tv1XEtYqe+H/qNiJ8stcpWefHby5lR5as0 pDZg== X-Gm-Message-State: AOJu0Yx8wfTCkvR78mmx6alGjwnKzIOYrdcNb+C8a+xpEBaiZjZtlcQE HqWQIxeKHE+OATVLGcR1CexKICxX9cA= X-Google-Smtp-Source: AGHT+IERP39eT1EKIW0o0w4FIDOku618XMvV1N7KQCMi55hnYoVKlxflcru5UfA66foRhtHASvLd2ndsc5U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d4cc:b0:1cc:1900:28d7 with SMTP id o12-20020a170902d4cc00b001cc190028d7mr82404plg.12.1698430983359; Fri, 27 Oct 2023 11:23:03 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:01 -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-20-seanjc@google.com> Subject: [PATCH v13 19/35] KVM: x86: Disallow hugepages when memory attributes are mixed 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: n3dbkifgty5gum9tig4ztm51hxxf4ubn X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 834D240004 X-Rspam-User: X-HE-Tag: 1698430984-11663 X-HE-Meta: U2FsdGVkX19KVDxi0D4IN1cgSLGrGW11/MCa7TMbwrVdlSy0zvcl29ISoPvPqufLh2z4ULkHv4ZARJEG8EqwRIDE7DI+AYfwDTyvSmS57KHQoEns0wEJ0vDdVY87e0bc0Quen8q0bBt8jBvwDY3l9qtwbWz+ZDckjrHvuyi3ljqcIN+4sZbq5CeX7WI8uj2XnvpP2hs8tykcRQ5OgDfW1EyTozzusamb7dZPjx2CUEhC1n/DOpucax98rws89jSuYqkSGcDDlnhnIdQAZ0iDjl/YbmtFLJ28gZYoMC2y44888l9CftHSymGtaWgwcZ+FgWtRwGx2OgOgIh77TniIbZir4qKjzMrFscduKq7CDm6Q+qXtzFUGV9C5StxZrxTj+xMUciN9athXkgQYP9Okn2xVdpFyL+SsS5aIbHvyer5NM8fJvxS+qe8ost6qVBaVlkmmPaWoPIp69cJnAaTxKJp9riLHtC+okTAAxEV9LNCRSpHQk/absyURiWRSTs41xkrzepNBcIWSTIzq/9RMsUAy6k6ftcEeLcORQE8q3X8b+6uIYBjbVYiPLGnuwjQ0tyBchSgMaF8PWciQV65kQyKytjy6eVn04CMfez9uqGOTNDmzryjiCWa9nac/KOTQ/gGW1Fjopq1xc5HfKhjL8yHMzQGrpwJVIoyMGgnjUYbYHHtIQF4gDBOfrcKONakrG6GtsOpWye6oNvZfSYVW8h0gvQP/ZhMlMRUcckUBQUa2iixRhROWFMzrPs9kJbwe8jQaJjylfz4YuHx3UP5bBr8n1UW77SIEhRc6cdkXy5KZXB8Q14XvS7AADwLiEtyicCnNqOTH6avewem6mw6mODQGeV0+Nn7Gj5qrlSfOO29yMTRRLBKEHBk3/gOJYK1T4Jwmn78mPZS700/HJjQnljk3hGeoG4Xdlk3vLLwowNebyxFmja9LSqxuaMqb4ivzGj6d26iJbnHU7lU/F1a wSKjIUwM B1gh+tcz3RH6tMbANGBJi7O+cEDdUkW40v8SjxAspEloanwjia/xlI8mQJGopzjGCqeGW+zw1IIGoHPUVawl3F3aGKiFDPNdL1EsoGKmLgHLFNHPOGq23QWaUq0oDd1ffB16hgGTSFCtv61K2wKwsIPXXNSuV7DxUag9PLqBuluLkJ1pHu9wqmcvfovQADOf1dg0w0NcTpusOd/eHIK3lxJhvPeaz2Z7bHcZxeoB98i5Pjs8QsAEL9HW1/6mJJqvNozSv08rv6jnquNAqLkzhzoZ5MoxxbfAd/uMGX+dNpjyGczXO1Dd6FsPMnEC3lFB3c46MixCfiRvpwBQiCzhbv+ywkQPeaTyLlLvW/w0oIB7si0F1uXGGXDgnHnNxoWi3yQ6DgUPv7UzcWGL2vqFlOKtgRHoEO1RcypMywAO1sS4dUVioGbPoPfb5r0zc3YijWWkZwVUdzFwplVV6SpOcg3oCGmvMtB/2FdWLGLsMeL3Fha9ZwJwP1PFENPkDtxdYC7qSeukpxvsYPNnB1/8z2NMICh21pxUAIvvK5tsjLu1U1BrWOOtNPnk0Xwa4Xv/Sm1XJjB8Nhgii96Jih221lDLeIhn2zqFR9rwSZOvf3gv+AAgKkDKUWLAWkGw7rO8qTQnkHUBotTqzCzxALFOwjPccJcNT1JPeEOafgbuVwiocbPtkDTe1EADvUl9o0bpvf3sFrICnKy1o0VhAkt2deMzs6BE4xjvyBwwI/fR+CqnHGxqREsE8OSKmNAcB5zKu1BCZniEbSRd6pQ38xD5Ne2CDQ4U+o5CG739Dux9qf8MGBy4= 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: From: Chao Peng Disallow creating hugepages with mixed memory attributes, e.g. shared versus private, as mapping a hugepage in this case would allow the guest to access memory with the wrong attributes, e.g. overlaying private memory with a shared hugepage. Tracking whether or not attributes are mixed via the existing disallow_lpage field, but use the most significant bit in 'disallow_lpage' to indicate a hugepage has mixed attributes instead using the normal refcounting. Whether or not attributes are mixed is binary; either they are or they aren't. Attempting to squeeze that info into the refcount is unnecessarily complex as it would require knowing the previous state of the mixed count when updating attributes. Using a flag means KVM just needs to ensure the current status is reflected in the memslots. Signed-off-by: Chao Peng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/mmu/mmu.c | 154 +++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 4 + 3 files changed, 159 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 31e84668014e..8d60e4745e8b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1836,6 +1836,9 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu); void kvm_mmu_init_vm(struct kvm *kvm); void kvm_mmu_uninit_vm(struct kvm *kvm); +void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm, + struct kvm_memory_slot *slot); + void kvm_mmu_after_set_cpuid(struct kvm_vcpu *vcpu); void kvm_mmu_reset_context(struct kvm_vcpu *vcpu); void kvm_mmu_slot_remove_write_access(struct kvm *kvm, diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index d33657d61d80..4167d557c577 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -795,16 +795,26 @@ static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn, return &slot->arch.lpage_info[level - 2][idx]; } +/* + * The most significant bit in disallow_lpage tracks whether or not memory + * attributes are mixed, i.e. not identical for all gfns at the current level. + * The lower order bits are used to refcount other cases where a hugepage is + * disallowed, e.g. if KVM has shadow a page table at the gfn. + */ +#define KVM_LPAGE_MIXED_FLAG BIT(31) + static void update_gfn_disallow_lpage_count(const struct kvm_memory_slot *slot, gfn_t gfn, int count) { struct kvm_lpage_info *linfo; - int i; + int old, i; for (i = PG_LEVEL_2M; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) { linfo = lpage_info_slot(gfn, slot, i); + + old = linfo->disallow_lpage; linfo->disallow_lpage += count; - WARN_ON_ONCE(linfo->disallow_lpage < 0); + WARN_ON_ONCE((old ^ linfo->disallow_lpage) & KVM_LPAGE_MIXED_FLAG); } } @@ -7161,3 +7171,143 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm) if (kvm->arch.nx_huge_page_recovery_thread) kthread_stop(kvm->arch.nx_huge_page_recovery_thread); } + +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static bool hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn, + int level) +{ + return lpage_info_slot(gfn, slot, level)->disallow_lpage & KVM_LPAGE_MIXED_FLAG; +} + +static void hugepage_clear_mixed(struct kvm_memory_slot *slot, gfn_t gfn, + int level) +{ + lpage_info_slot(gfn, slot, level)->disallow_lpage &= ~KVM_LPAGE_MIXED_FLAG; +} + +static void hugepage_set_mixed(struct kvm_memory_slot *slot, gfn_t gfn, + int level) +{ + lpage_info_slot(gfn, slot, level)->disallow_lpage |= KVM_LPAGE_MIXED_FLAG; +} + +static bool hugepage_has_attrs(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, int level, unsigned long attrs) +{ + const unsigned long start = gfn; + const unsigned long end = start + KVM_PAGES_PER_HPAGE(level); + + if (level == PG_LEVEL_2M) + return kvm_range_has_memory_attributes(kvm, start, end, attrs); + + for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) { + if (hugepage_test_mixed(slot, gfn, level - 1) || + attrs != kvm_get_memory_attributes(kvm, gfn)) + return false; + } + return true; +} + +bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range) +{ + unsigned long attrs = range->arg.attributes; + struct kvm_memory_slot *slot = range->slot; + int level; + + lockdep_assert_held_write(&kvm->mmu_lock); + lockdep_assert_held(&kvm->slots_lock); + + /* + * Calculate which ranges can be mapped with hugepages even if the slot + * can't map memory PRIVATE. KVM mustn't create a SHARED hugepage over + * a range that has PRIVATE GFNs, and conversely converting a range to + * SHARED may now allow hugepages. + */ + if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm))) + return false; + + /* + * The sequence matters here: upper levels consume the result of lower + * level's scanning. + */ + for (level = PG_LEVEL_2M; level <= KVM_MAX_HUGEPAGE_LEVEL; level++) { + gfn_t nr_pages = KVM_PAGES_PER_HPAGE(level); + gfn_t gfn = gfn_round_for_level(range->start, level); + + /* Process the head page if it straddles the range. */ + if (gfn != range->start || gfn + nr_pages > range->end) { + /* + * Skip mixed tracking if the aligned gfn isn't covered + * by the memslot, KVM can't use a hugepage due to the + * misaligned address regardless of memory attributes. + */ + if (gfn >= slot->base_gfn) { + if (hugepage_has_attrs(kvm, slot, gfn, level, attrs)) + hugepage_clear_mixed(slot, gfn, level); + else + hugepage_set_mixed(slot, gfn, level); + } + gfn += nr_pages; + } + + /* + * Pages entirely covered by the range are guaranteed to have + * only the attributes which were just set. + */ + for ( ; gfn + nr_pages <= range->end; gfn += nr_pages) + hugepage_clear_mixed(slot, gfn, level); + + /* + * Process the last tail page if it straddles the range and is + * contained by the memslot. Like the head page, KVM can't + * create a hugepage if the slot size is misaligned. + */ + if (gfn < range->end && + (gfn + nr_pages) <= (slot->base_gfn + slot->npages)) { + if (hugepage_has_attrs(kvm, slot, gfn, level, attrs)) + hugepage_clear_mixed(slot, gfn, level); + else + hugepage_set_mixed(slot, gfn, level); + } + } + return false; +} + +void kvm_mmu_init_memslot_memory_attributes(struct kvm *kvm, + struct kvm_memory_slot *slot) +{ + int level; + + if (!kvm_arch_has_private_mem(kvm)) + return; + + for (level = PG_LEVEL_2M; level <= KVM_MAX_HUGEPAGE_LEVEL; level++) { + /* + * Don't bother tracking mixed attributes for pages that can't + * be huge due to alignment, i.e. process only pages that are + * entirely contained by the memslot. + */ + gfn_t end = gfn_round_for_level(slot->base_gfn + slot->npages, level); + gfn_t start = gfn_round_for_level(slot->base_gfn, level); + gfn_t nr_pages = KVM_PAGES_PER_HPAGE(level); + gfn_t gfn; + + if (start < slot->base_gfn) + start += nr_pages; + + /* + * Unlike setting attributes, every potential hugepage needs to + * be manually checked as the attributes may already be mixed. + */ + for (gfn = start; gfn < end; gfn += nr_pages) { + unsigned long attrs = kvm_get_memory_attributes(kvm, gfn); + + if (hugepage_has_attrs(kvm, slot, gfn, level, attrs)) + hugepage_clear_mixed(slot, gfn, level); + else + hugepage_set_mixed(slot, gfn, level); + } + } +} +#endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f41dbb1465a0..824b58b44382 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12607,6 +12607,10 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm, } } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + kvm_mmu_init_memslot_memory_attributes(kvm, slot); +#endif + if (kvm_page_track_create_memslot(kvm, slot, npages)) goto out_free; From patchwork Fri Oct 27 18:22:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438947 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 2750EC25B47 for ; Fri, 27 Oct 2023 18:23:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92F6E8001C; Fri, 27 Oct 2023 14:23:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B7EA80018; Fri, 27 Oct 2023 14:23:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6BC4C8001C; Fri, 27 Oct 2023 14:23:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 51F3380018 for ; Fri, 27 Oct 2023 14:23:08 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2BE95A073F for ; Fri, 27 Oct 2023 18:23:08 +0000 (UTC) X-FDA: 81392063256.29.2122E2E Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf16.hostedemail.com (Postfix) with ESMTP id 504E4180005 for ; Fri, 27 Oct 2023 18:23:06 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=DSat3St6; spf=pass (imf16.hostedemail.com: domain of 3CQA8ZQYKCCoYKGTPIMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3CQA8ZQYKCCoYKGTPIMUUMRK.IUSROTad-SSQbGIQ.UXM@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=1698430986; 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=Xj30xbFl07atOtCQSMtQg8dB6rp8Wt3n2Y10jzFlxa0=; b=nD+xKodkf/1beSgURbygpt8ib/ndAhrIPBO36WsuDqJc+aCM2DAL/RpdrD1+9S819egCr4 4oNiuy887o+KeXbXK44MW4W7rRio7FnV8+2SCH0F3Rgq7pzXZNduBgYm4k9o+UQ2R0OEWa C51E47r4TE2y+ljJcQsiP7Ceb9Ok/Zo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430986; a=rsa-sha256; cv=none; b=7lID/HUualetf7GHpTgPbzEahWdBzQRuT5vxByG96RD0M8tdXqKOIwQ82rEAICcVWhLJtc wzuNTG1ExnuBllGzAubApQG9+5/2Zzx5lubQNoONHO5ZDGxWJ0AtRqhar8JkIDJkWy4mqA he29ohLjT7432xI63kYecfBzJyLDtlw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=DSat3St6; spf=pass (imf16.hostedemail.com: domain of 3CQA8ZQYKCCoYKGTPIMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3CQA8ZQYKCCoYKGTPIMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-da1aa98ec19so660386276.2 for ; Fri, 27 Oct 2023 11:23:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430985; x=1699035785; 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=Xj30xbFl07atOtCQSMtQg8dB6rp8Wt3n2Y10jzFlxa0=; b=DSat3St6quZB3GtrKG/ef/BfRoXyRWhOF1EvPhjVi5IOAtn6t/Ey4EO9Kp//st7Mxn CJDn9totVVGzZyjNAcOMAsqvXTbPexUIMa+2Igq9MbVcwd7aS4YSw6ot5jsRc1LeTVbE Y75fG9/GVfinNUleZQcxrovuTiuhmDgcMtZrRgwUxGxHrYnRVigeLGFhB6Tv71tTNmC0 T3yTIFwr8ARSSGyDdvHaKg4YG5Q+qnAKQxeH8Jq/54QPGuF1nbIbyjHsKJCfn+2eIF7/ BBd/5WW6n3hl1JB8WVnWNVcWU0Mt4IrjfsQ3IXWxJ6l9MYOW7MN7nk8MRpeneUtkZ+JB Nx7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430985; x=1699035785; 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=Xj30xbFl07atOtCQSMtQg8dB6rp8Wt3n2Y10jzFlxa0=; b=PVl4I9NFFulU/tDqfYyEabR8L2+YmVqGqnl0Ogp7+Oarn7QQaV7Xwl3LFI2pzOfPLQ NuzMlQ7VVzEW6SqYbgwRwt7umK1G1zv3G8HIC6FwfD0zGa7N1L8hZjVbsArb3A9Mn8SU pW52kGbue0tzOVE33k87An8w44V7J70NZ8n2fukbI4EXbcRzgVSh5Xp+WiGYP2pmftxP RSIMMOG3SD8+nOV6GdRXWiOyIaHDvl/ph6BpghnjQIQ6WBZpsrTzxuFDEd6+bPLuMQ7F 7WVu5HWl+xKOUmOMd5crjRdhgDjhDpfQUT4GQmIvwhJhvJ1ciUfZERGqsC63mfLPhBMz jpgw== X-Gm-Message-State: AOJu0Yxl7diERUYcp+kqyoXapEPVQQF64uYo86yYZ0m6ysi14VZaOqKs LlIeZqktUxLqCICYQUQduttTXAdRZcs= X-Google-Smtp-Source: AGHT+IHJyYgXOSmXHw6Q/ZGgKMIZrlqldOTH4kOV2g/1tUJbmTSTwCBf3zyHkDcXb7wW1+WsIfgoTREwpdY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:5f4c:0:b0:da0:3e20:658d with SMTP id h12-20020a255f4c000000b00da03e20658dmr63345ybm.10.1698430985424; Fri, 27 Oct 2023 11:23:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:02 -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-21-seanjc@google.com> Subject: [PATCH v13 20/35] KVM: x86/mmu: Handle page fault for private memory 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-Rspamd-Queue-Id: 504E4180005 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: zdndj4daspsawjitysjuo7zojjdnh7pu X-HE-Tag: 1698430986-140595 X-HE-Meta: U2FsdGVkX1/HXERDyam2fQRdrXmVtfwYORYRnNddvKv8bjPzTN+AsUBqhqqQ2Dx/FZ5XwVGv6V/1CS0BhWupkwMUhUN+CkT1RLn6SisaH6Myjy+WJPhLL4EY7cDcjhp/JTg2tRuRiemi9QT2mnTVTpl66mg2db/BpgO7GTOsCgUkjewQNUkdvhIY7yCtHpQWUzCe1WVAA/3qqSbJ4Sf1Tp/jrT+adqdu/S2nWWy/AUFWl7kKEo3coHxJlLXuWiZ0CMeG5UR6/ZE/kfs8liP0XfUbvBgKXZLoa/pkHW8KdLL1ubmdq2MIwhBDaqNSm0PQzRw6rbHUlKyQGxHn8XOPpeGxGZrw+eRTODGnghO+EaB1eWZBjalqHaGZDr5FTnvOAeAwfowYg/MeEjPV5MHQUbugLKkjY0zDy1KI5n4PX9vuDsxMn4QBAVUfgdP9IqtmEbp1ah2pCf1hIP69+cJ7n58Ye3J7BXzsy4ca8tkwGULRrxdiLF8l4I+INGbS4+wNiC2aXUWnR6FvTDLFTNhVWF1fS5KUT2F9wVHi+WO8t7DQTQXuAy4gE/Fwm8HYfsMZ/ietTavrmNxjVSwod7s+umPtrOvOJcZA6nTRBfArri4PjQS+qma+tlLXE50hSV5ynnMfxJnv9JP99bNWhQJkqtYKZnhSZulqztLeRPlJDH5JCj5ghb/e3F1iVk/GdtQrVjIcQr0sqB+WuGpEZWv2s+tkZyHmRB7qsueW9nStH9axLE9Df7Mm5iuYcqakidoh6UKxmahUd6vpuAq8p0zSgQkDibmznateEv8M5mDnoxXx/79hyXKRv3tQLlsdiptip5BjZdbl3hbIXnrWXxA1cgyBqzIzUmmeulTRuA8bjhtlPUwCf9gmVl3VRIXhSdEPLHrruUVjXNQv0MTSOW92WRnqo2UwEhEjixNcZjwsP/aWCORnP7wWCKVw5c1Qxzp5lmujOfwKxjPjVoJsxiW 4DobCq8q ot5U9C1lzMJXaO/+J4OUQm+z7bSFD+HeJOgrirBZVzTHwpz1eLKmjZcM2oHpCIMTVoF3wSvpTzmA1QSftUWbiXK9GoXKq+OurcvQPMOpuA8OwvRoAVzEEWP7G/zY67sa5FC3XZ2Lv94Whm4I5G0N/9QELfjsOfmTA32pm3JpmzO1BgEjET4kMX/EbhfZBCHcOGNhUdf6eH4RBE2PB1SUqyLyFvXPlIg/OXy/kisn3E0gViw1jJgh2RO2pH5fJxkOtduiovJZ5r6BzxkokEpa7nXsYNX2uLNXcOa1zbmzrpBF1IRunNPyStl94Gusn0mRhvY17E9QSf2ECN/Pyla4kvCQDACvCnS8A9gHtVEEZIGmtnUGNLT/w1V3X4bRH1BZtYCRMDdSlb93DQpQrxy3xWyKd/fcsJslxDjIFeH6uGFV6w7nVldk+00z+pYSrbyu3vfu1XlLXWXSYxZsRkGcBkX+h7aENLN0ggplgH3eSD/9Uz5yGpKlsJ9P0C7d+qZuC0rdrMQK3tkpVOn+YzYD+Sd/tleiLgonVPBj8bh2FybQAHmphQdz2Se2BdN1M6DNuTmsFa2mX1fXoG6BbgKVyscOaDR+yx2RHiCGOWHLF1KhJPpA4uJ/W5qzON1rwTKCZZzRodm0M7neao9S0EIUzqX3sp0i9GneQlKE8+FzGZSJfbqqKus6jsFO+TBJ643dMlLv1ImHrkbtURxQvN5CR/JkiEKnYJaz7JaIFlDgb10TgsJZvgBUI0XorLkYIshzIo2jjkldCH2LIFvKfsphM3V5hLtU05/CBtQuL7mM6nOKLPCyVhwX2j1xozOD1PTa7yObd 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: From: Chao Peng Add support for resolving page faults on guest private memory for VMs that differentiate between "shared" and "private" memory. For such VMs, KVM_MEM_PRIVATE memslots can include both fd-based private memory and hva-based shared memory, and KVM needs to map in the "correct" variant, i.e. KVM needs to map the gfn shared/private as appropriate based on the current state of the gfn's KVM_MEMORY_ATTRIBUTE_PRIVATE flag. For AMD's SEV-SNP and Intel's TDX, the guest effectively gets to request shared vs. private via a bit in the guest page tables, i.e. what the guest wants may conflict with the current memory attributes. To support such "implicit" conversion requests, exit to user with KVM_EXIT_MEMORY_FAULT to forward the request to userspace. Add a new flag for memory faults, KVM_MEMORY_EXIT_FLAG_PRIVATE, to communicate whether the guest wants to map memory as shared vs. private. Like KVM_MEMORY_ATTRIBUTE_PRIVATE, use bit 3 for flagging private memory so that KVM can use bits 0-2 for capturing RWX behavior if/when userspace needs such information, e.g. a likely user of KVM_EXIT_MEMORY_FAULT is to exit on missing mappings when handling guest page fault VM-Exits. In that case, userspace will want to know RWX information in order to correctly/precisely resolve the fault. Note, private memory *must* be backed by guest_memfd, i.e. shared mappings always come from the host userspace page tables, and private mappings always come from a guest_memfd instance. Co-developed-by: Yu Zhang Signed-off-by: Yu Zhang Signed-off-by: Chao Peng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 8 ++- arch/x86/kvm/mmu/mmu.c | 101 ++++++++++++++++++++++++++++++-- arch/x86/kvm/mmu/mmu_internal.h | 1 + include/linux/kvm_host.h | 8 ++- include/uapi/linux/kvm.h | 1 + 5 files changed, 110 insertions(+), 9 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 7f00c310c24a..38dc1fda4f45 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6837,6 +6837,7 @@ spec refer, https://github.com/riscv/riscv-sbi-doc. /* KVM_EXIT_MEMORY_FAULT */ struct { + #define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) __u64 flags; __u64 gpa; __u64 size; @@ -6845,8 +6846,11 @@ spec refer, https://github.com/riscv/riscv-sbi-doc. KVM_EXIT_MEMORY_FAULT indicates the vCPU has encountered a memory fault that could not be resolved by KVM. The 'gpa' and 'size' (in bytes) describe the guest physical address range [gpa, gpa + size) of the fault. The 'flags' field -describes properties of the faulting access that are likely pertinent. -Currently, no flags are defined. +describes properties of the faulting access that are likely pertinent: + + - KVM_MEMORY_EXIT_FLAG_PRIVATE - When set, indicates the memory fault occurred + on a private memory access. When clear, indicates the fault occurred on a + shared access. Note! KVM_EXIT_MEMORY_FAULT is unique among all KVM exit reasons in that it accompanies a return code of '-1', not '0'! errno will always be set to EFAULT diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 4167d557c577..c4e758f0aebb 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3147,9 +3147,9 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, return level; } -int kvm_mmu_max_mapping_level(struct kvm *kvm, - const struct kvm_memory_slot *slot, gfn_t gfn, - int max_level) +static int __kvm_mmu_max_mapping_level(struct kvm *kvm, + const struct kvm_memory_slot *slot, + gfn_t gfn, int max_level, bool is_private) { struct kvm_lpage_info *linfo; int host_level; @@ -3161,6 +3161,9 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm, break; } + if (is_private) + return max_level; + if (max_level == PG_LEVEL_4K) return PG_LEVEL_4K; @@ -3168,6 +3171,16 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm, return min(host_level, max_level); } +int kvm_mmu_max_mapping_level(struct kvm *kvm, + const struct kvm_memory_slot *slot, gfn_t gfn, + int max_level) +{ + bool is_private = kvm_slot_can_be_private(slot) && + kvm_mem_is_private(kvm, gfn); + + return __kvm_mmu_max_mapping_level(kvm, slot, gfn, max_level, is_private); +} + void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { struct kvm_memory_slot *slot = fault->slot; @@ -3188,8 +3201,9 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault * Enforce the iTLB multihit workaround after capturing the requested * level, which will be used to do precise, accurate accounting. */ - fault->req_level = kvm_mmu_max_mapping_level(vcpu->kvm, slot, - fault->gfn, fault->max_level); + fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot, + fault->gfn, fault->max_level, + fault->is_private); if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed) return; @@ -4261,6 +4275,55 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true, NULL); } +static inline u8 kvm_max_level_for_order(int order) +{ + BUILD_BUG_ON(KVM_MAX_HUGEPAGE_LEVEL > PG_LEVEL_1G); + + KVM_MMU_WARN_ON(order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G) && + order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M) && + order != KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K)); + + if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_1G)) + return PG_LEVEL_1G; + + if (order >= KVM_HPAGE_GFN_SHIFT(PG_LEVEL_2M)) + return PG_LEVEL_2M; + + return PG_LEVEL_4K; +} + +static void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) +{ + kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT, + PAGE_SIZE, fault->write, fault->exec, + fault->is_private); +} + +static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault) +{ + int max_order, r; + + if (!kvm_slot_can_be_private(fault->slot)) { + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); + return -EFAULT; + } + + r = kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, &fault->pfn, + &max_order); + if (r) { + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); + return r; + } + + fault->max_level = min(kvm_max_level_for_order(max_order), + fault->max_level); + fault->map_writable = !(fault->slot->flags & KVM_MEM_READONLY); + + return RET_PF_CONTINUE; +} + static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) { struct kvm_memory_slot *slot = fault->slot; @@ -4293,6 +4356,14 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault return RET_PF_EMULATE; } + if (fault->is_private != kvm_mem_is_private(vcpu->kvm, fault->gfn)) { + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); + return -EFAULT; + } + + if (fault->is_private) + return kvm_faultin_pfn_private(vcpu, fault); + async = false; fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, false, &async, fault->write, &fault->map_writable, @@ -7173,6 +7244,26 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm) } #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range) +{ + /* + * Zap SPTEs even if the slot can't be mapped PRIVATE. KVM x86 only + * supports KVM_MEMORY_ATTRIBUTE_PRIVATE, and so it *seems* like KVM + * can simply ignore such slots. But if userspace is making memory + * PRIVATE, then KVM must prevent the guest from accessing the memory + * as shared. And if userspace is making memory SHARED and this point + * is reached, then at least one page within the range was previously + * PRIVATE, i.e. the slot's possible hugepage ranges are changing. + * Zapping SPTEs in this case ensures KVM will reassess whether or not + * a hugepage can be used for affected ranges. + */ + if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm))) + return false; + + return kvm_unmap_gfn_range(kvm, range); +} + static bool hugepage_test_mixed(struct kvm_memory_slot *slot, gfn_t gfn, int level) { diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index decc1f153669..86c7cb692786 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -201,6 +201,7 @@ struct kvm_page_fault { /* Derived from mmu and global state. */ const bool is_tdp; + const bool is_private; const bool nx_huge_page_workaround_enabled; /* diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7de93858054d..e3223cafd7db 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2358,14 +2358,18 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr) #define KVM_DIRTY_RING_MAX_ENTRIES 65536 static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, - gpa_t gpa, gpa_t size) + gpa_t gpa, gpa_t size, + bool is_write, bool is_exec, + bool is_private) { vcpu->run->exit_reason = KVM_EXIT_MEMORY_FAULT; vcpu->run->memory_fault.gpa = gpa; vcpu->run->memory_fault.size = size; - /* Flags are not (yet) defined or communicated to userspace. */ + /* RWX flags are not (yet) defined or communicated to userspace. */ vcpu->run->memory_fault.flags = 0; + if (is_private) + vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE; } #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 33d542de0a61..29e9eb51dec9 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -527,6 +527,7 @@ struct kvm_run { } notify; /* KVM_EXIT_MEMORY_FAULT */ struct { +#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) __u64 flags; __u64 gpa; __u64 size; From patchwork Fri Oct 27 18:22:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438948 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 F3F23C25B70 for ; Fri, 27 Oct 2023 18:23:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A40518001D; Fri, 27 Oct 2023 14:23:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C74F80018; Fri, 27 Oct 2023 14:23:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CA4D8001D; Fri, 27 Oct 2023 14:23:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 63EB480018 for ; Fri, 27 Oct 2023 14:23:10 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3EC0040779 for ; Fri, 27 Oct 2023 18:23:10 +0000 (UTC) X-FDA: 81392063340.08.7397D48 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf22.hostedemail.com (Postfix) with ESMTP id 74DA6C0016 for ; Fri, 27 Oct 2023 18:23:08 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=n48X3M1J; spf=pass (imf22.hostedemail.com: domain of 3CwA8ZQYKCCwaMIVRKOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3CwA8ZQYKCCwaMIVRKOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430988; a=rsa-sha256; cv=none; b=Ib1wat25RZHo6XbFdhNmISIjc4HSVWoRfPhcRPy2ETESgSjk7y3krQJVD9E3w0uvJPehc8 qqCBpNGeqCbSr521FFb9zjc3REx8orGAzevqV/R/g0UIcQt+Qx+tJ1nkjH4wGkNdOGO02z sq4ft+B5HnjCbrQijuECKXqn+O9fgro= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=n48X3M1J; spf=pass (imf22.hostedemail.com: domain of 3CwA8ZQYKCCwaMIVRKOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3CwA8ZQYKCCwaMIVRKOWWOTM.KWUTQVcf-UUSdIKS.WZO@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=1698430988; 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=o8gdvYQ3/1yqqCN7M6+cUJLwJKILjEFSh0R8bjlKI1I=; b=RDijblpQgxHuXfznSmEdcmAlLykAeb7GwRRQz2OUaCT29twv3q+VyZoA2tvUI6DGZor98z m7+sGfsFguqPwgW2fIsQO1VQvmdirag4OPAPEX7K49fnCaRt+o/kVnrJ+FDNxSMVgqoLQr U9z2bAU4CgPl4IVkqJajNz1wQm3FLA0= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c9da175faaso20619725ad.1 for ; Fri, 27 Oct 2023 11:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430987; x=1699035787; 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=o8gdvYQ3/1yqqCN7M6+cUJLwJKILjEFSh0R8bjlKI1I=; b=n48X3M1JNrWhfG58vEMEzAQr+6pnUd/6VMWQCIeshgyCNR2qTr3/JIs11zWxObsBYA 9Bg55i/iOUtn71Dpwg/AVgzMWJpDHdPrlUOmMPzPPXStdq3jI0+fjzOPU709USIOe5vX ukURhSph8l0Gb5KXvoqqmR1vfsCttycOPcCTCRPRMvEpgfrhK++wNxsOp2QGfk1+Jr2Z hbs70wF+Bb6g4HIJLXsa1U4wgqyKj0rpj0PTmF3WLggVoeZOq9+WWcX0MWu8Ncb9jW2V 2eR8HZ28gdDXd98yc15OjhmMYVjPVg+U9PdKri01kLkSP0k+GXXloLPR6+PGKJTICAzH M0+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430987; x=1699035787; 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=o8gdvYQ3/1yqqCN7M6+cUJLwJKILjEFSh0R8bjlKI1I=; b=vpO5lBhPtfmNGOzYy6xGxxWPQ0iWTU80ume61avV042qac9N6pSP8e+cRINwtspzVc TZIU55b3NvWxYVI20xyPE4aIPZ+xU+QI8pcqq/UIDGk0oOn68jfznZAv4gAH5YTM6jsY dGCBVvq/lRapayydNO2chWxu6gLlB/KoXwi/FdXYRhoGgm6/s47IE/Kge1stz1VGweFl 8DucVdRkS0+v7+KZopf9ruYYWSaWb9sK8QOgHnyx/K9A0iehf7c6/Ip4bnIcwODECjqF 7Y96foPZYSBrJPILHK1ZMSJLAzjydsj0n06idEcxki0GNMBWsbixQU0Ttrmg/6/HHQ41 GGnA== X-Gm-Message-State: AOJu0YxSJvtLzIcV69sAVman3i/cHkPLZtR6quak/6vKO/2TnaVc5CI+ ItmZlcFjk57Tk9s6npb9GiraSiwFZsM= X-Google-Smtp-Source: AGHT+IGxPKYh61YDaE4Ab16I5616FjNw5G/WLIOxqZ93NjDEP07jn0icTkqJBzjVXPbdr7XzvwkO91qMX/M= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c144:b0:1cc:281a:8463 with SMTP id 4-20020a170902c14400b001cc281a8463mr32544plj.7.1698430987435; Fri, 27 Oct 2023 11:23:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:03 -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-22-seanjc@google.com> Subject: [PATCH v13 21/35] KVM: Drop superfluous __KVM_VCPU_MULTIPLE_ADDRESS_SPACE macro 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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 74DA6C0016 X-Stat-Signature: mjfckgynftoh6qbdfkqr6zg7seoid8mj X-Rspam-User: X-HE-Tag: 1698430988-502375 X-HE-Meta: U2FsdGVkX1/BHCsxpspBkFlY8c0yfzXHDG9y9Q6iJfmBhF9GXDIgiShjUCre27arOSoCNgqVzvhm0jGNFugcKZT1aKkESWqS2XTUeqq2Zkl30E6gE7HQZCBQsgR7Ance7h3aG7a3qu1y5PpDTZ4Ourm4GMnCnO29C5DgnxtOFfiwRRa/36u5Ns+J9VoihHpuwoAC/d8J0abdIrwEb3XsSwzbZHkSgC9tedXQt49Ljfmkv0R0Gqp3ZI2QNgfCCSiheBurVi0sqsiNMKmaZvTUcr0Fr3i83J/GdYy4RQNYmhggPaVhUkRFNfv/MUQF+RuZr0LPh5DEoQQMdFofpo2Bl4wAyNlwzJHac4lZ3+lHC68VebMUAfVL0IqDO0zVnV4q1be22u+s40ZFKtCu8oAQ/jTabM/373VcBbnfgYh3BfuoHfm8bpVyGIUjzRyn1CuEkgHMdr8IUG9cD66PijN8+GNLZLePwILaGRp+rJUeSDxMjTKEf8jeGpEsA6o5osVp2fbSzUhuIk0YTzviOi9VzUR9w8YfQs+6JJC38hkvnKKflNV1t+bKGSNclrIxDt/o6Q6KFINDlWw43iyteinqDM1RCGjNmURwmi+l3N7JF/Je5YcGD2Rl6fCQu/ibjxrqznQrOIqCmkZSjmc9I1gAjze1tWbP6oun7kZhD5/w7hCQ7YY293txT++P4WAC7BgFPuCxHHpn+c7MYT4oxFq6pzmKJyd+MzIAmH8nOJqaaZUR7odCgMDFrFgN0kwwtNBASC849F0nhDucSlJpjK+NvvRC/bn9TlOCFD/7tloGa9iWwRybGj7aHvCFVrxQPn/ZzgYHjkw5XASfQ31MhqiyRHXOgUpTja7S0nGfXFPAkzKT0j3IJkU6VcpHVWzSc0ujW3D1AS6dqGRS19VP5Zys3trrmyHBy+m83V0AX6I3SRJmQvHU5XlbWLdhhITL2jozR9IS+3vGhAHYZVEudZi z5QUBmbk BPgl9ZxmbUBqVFY441ZuDIn68q9nPXQDKT6ur4Qoh0OE+ZKPpeG8nSGjqJxge82Zj9Vgp1lyu8HYT4OUR2cG4AQCniF9566dc2DFjS9MJXfiBN/eeqNuPW34aoxlzw6BXVuN1qKgfq2irGITwnCN+ytBw9QYixxF7KbFTcawMTj0lJ1s5HFGE9j4/uu4kjSUS6vYs4LWIO2oRLgyvpabp3IwC1FU21LqdGejTLIssPdqjipXmADX593s63VcZI/QVZnfOr1STxEKko1soiGGHsXuU6bxytPhPdjRaVR3A9XjNQFB4IsvyFvnxFVIgi5Sn64xL5b4KNXMZqldzN9ygS9lqzBXIXueVOORuFcnNtLwlUJfdF2NadM2QxZ6p6d4aeCgrHMEv8XagKFKEaU+n+tvN47ogLe+3/thXhgxOFrWAwjU5Bg1sWzGr4r0bN8S04MKuUGnIQCnPzFrrL0ldRFjfjmKc2N2Bk+ZnkJqZaiUxFfasSDneQPC9yt6n/oF79vAyjx9FNIXoyQr7CXoFK80/EKkgD4v2mvbxep1MV6FefPG7zsYLsikW5tTZ1LnqBzwv3EE9HTLkyFzIJMmDQ1HbLjWcc119P21B43RHHDaV/kLd26evsbTJY1oJUkaMmFQJ5jJyirWSv4BqMS8bCBGf+gRZFrdKF7JukbwgXbom5vf9AJglqDWq9PNllFLmMLET7xNixiRIhBrURd+GabjxKq0I1i1ZCH1oT5nqo5yrse+XnNGkC9FzykaKxoTWcnUhkS0klL8TJpyv+86JsvmBwVfndVKZY9MlTP6ZKtTiYCR2ezSvdkJWW6b/1I/fSg9JR5CCYVeMJLQ= 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: Drop __KVM_VCPU_MULTIPLE_ADDRESS_SPACE and instead check the value of KVM_ADDRESS_SPACE_NUM. No functional change intended. Reviewed-by: Paolo Bonzini Signed-off-by: Sean Christopherson Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- arch/x86/include/asm/kvm_host.h | 1 - include/linux/kvm_host.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 8d60e4745e8b..6702f795c862 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2124,7 +2124,6 @@ enum { #define HF_SMM_MASK (1 << 1) #define HF_SMM_INSIDE_NMI_MASK (1 << 2) -# define __KVM_VCPU_MULTIPLE_ADDRESS_SPACE # define KVM_ADDRESS_SPACE_NUM 2 # define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0) # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index e3223cafd7db..c3cfe08b1300 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -692,7 +692,7 @@ bool kvm_arch_irqchip_in_kernel(struct kvm *kvm); #define KVM_MEM_SLOTS_NUM SHRT_MAX #define KVM_USER_MEM_SLOTS (KVM_MEM_SLOTS_NUM - KVM_INTERNAL_MEM_SLOTS) -#ifndef __KVM_VCPU_MULTIPLE_ADDRESS_SPACE +#if KVM_ADDRESS_SPACE_NUM == 1 static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) { return 0; From patchwork Fri Oct 27 18:22:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438949 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 7FBFAC25B47 for ; Fri, 27 Oct 2023 18:23:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BCAF98001E; Fri, 27 Oct 2023 14:23:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B2D1180018; Fri, 27 Oct 2023 14:23:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9561B8001E; Fri, 27 Oct 2023 14:23:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7611780018 for ; Fri, 27 Oct 2023 14:23:12 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 58D3EA026F for ; Fri, 27 Oct 2023 18:23:12 +0000 (UTC) X-FDA: 81392063424.13.5005E4A Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 6F496180020 for ; Fri, 27 Oct 2023 18:23:10 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2G+sWKmh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3DQA8ZQYKCC4cOKXTMQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3DQA8ZQYKCC4cOKXTMQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430990; a=rsa-sha256; cv=none; b=GWHhw3bSZrKdNqwrGlWR2h6Zm6AH1LUxf4tbZ63qvXu+sgayxSLczlOC4owh0JRK3H5DJc vR9Uaw8pAwA+fQOPykTNtp9MjqAJzRcXELnOKeG8YXyBDRpI/akvyhk3o2FV+NfVIl7KCi 2UWQIfryvVvv7Te7HUU2B7FaWDtcEwE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2G+sWKmh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3DQA8ZQYKCC4cOKXTMQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3DQA8ZQYKCC4cOKXTMQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430990; 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=0auIV1DMPWYBI3KY46bLeGa1maTu7OZj6FOKCV8dRqg=; b=6mVsfwuS+F6FMOyxMt5rkT+FgwAXhv4aXeclprgvWVFZtsLYXaizJB0vDs0SYaJ0Lo0Cyj kqO9C0nYRRp62xeOIEJGZB+2yhCzufJnQGyuwa/BJpitdeI9QbmYZ67SwL7l4bwBqmHc05 RgHZgE4ST47mC2cvOt4At6eXkrbvvPA= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1cc281f1214so4821805ad.2 for ; Fri, 27 Oct 2023 11:23:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430989; x=1699035789; 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=0auIV1DMPWYBI3KY46bLeGa1maTu7OZj6FOKCV8dRqg=; b=2G+sWKmh9BUdOHWD3SwsIOD4KJodwGkFgC4qmsX5iFt6YwnI8gIZ/oH+ZyfRfYFdnH WGDe8ubtlIPRDVxDQpR3LUL7KdRo2e6x4Juoh/NVeZ1WpjXUE7cokABwZREffgq70Ons thaec+QSQkdFW4UhmlwE05gAtj4xTqaBt8B9Jzj8JwxkHJcrFfDhGcMo3gaEqJwgZEyG tLVwZpiDf+6blxWbbf/xNjO4H3fz6dv0YQBhUG3YS6eI6WWjLLeBzYaejIQAI+wEsRO7 2wHyI85jPO+NcJbXDp1PsaGjhbplA6sxSz+4PdxK5wrMWi+YpUNiKzjZtlKnQOuQLOZ3 Yl5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430989; x=1699035789; 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=0auIV1DMPWYBI3KY46bLeGa1maTu7OZj6FOKCV8dRqg=; b=OxAARk7MwJmGZK++2nNu16NrdfZ/3FmeWkLWcQE1ipJV9i9hY2ChcMEAzwHM3P9h5u nzpyaZ8u2UPS4RvnclWo3yYfxagDPM/Zgo2tD5y1qIyH1q2HyhQ+JC+Z/dVkxlem/ST+ cpZNSRjmi5olNPhr1cbN/QA0vwfZyENlYjTIlbYpuRI67pHk2SpIHXB98UBukAabdA/e nLiYHBhMisKof/MF7zDwAWKV749WxYk2vBqvcTxlbY2+LqOUq6A+HN/DgvSEOlLYeA/y 75d5pl6x7SD8c5+nLMiZQ+26c6VKwKQtwgUh3UXtuOtNiClIdYlnrSOCQG/hciyQcnmv 3jHg== X-Gm-Message-State: AOJu0YwlGpeg+xvL20AX1VVV85WrhaUIB9IhwTEn5O33hz2n5vdZVYvU 6l8QY+KV6QDp7SFeiiRC/5lZsIQtDIg= X-Google-Smtp-Source: AGHT+IFXrC3xkKZRDGXkVrFPzvpR3eGICNasom3Cu9rgQoclWToIjhDbekkK9eVZvTfAymJIx68sMWa+w9w= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ee05:b0:1c9:d358:b3db with SMTP id z5-20020a170902ee0500b001c9d358b3dbmr54518plb.11.1698430989341; Fri, 27 Oct 2023 11:23:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:04 -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-23-seanjc@google.com> Subject: [PATCH v13 22/35] KVM: Allow arch code to track number of memslot address spaces per VM 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 6F496180020 X-Stat-Signature: 4d8rzwuq1dits5o3zcp5df4rqf7co1wz X-HE-Tag: 1698430990-307995 X-HE-Meta: U2FsdGVkX194C5zMZdDZxDAmnxZzaB/N12qbIFlUE1HvIYZLpeqZXVsX+czxNhd0uIFI+sTncJ2rR9XnQ+LMgxLmVeK3vpy+Gziu1antaXeNf+/H0AxF5qU3AJ//0XkUDIzsmf0U/jSiUghzcE7tVqZmqZGCw4WTUrtlB2TDbSgnWzoGXd/bYqID3P2Jh4DCBzDAe31da91GeySmMe0bHtwuQ10d2kJrj0tuYPV84/7jOFaj54tLt6uNi+bILc3lbTi4QUSTLhCNntkTXmTitVKzd1tsCwm+1wcm6Mma8bpflUy6Wfi7qX1uUvfwhGJnNKZgMHE8CxtROMxGJkD1A6t0LbhWjWMZK+Re2Nse11RCpPs+s6DASsube0uELxHG0lLa19SmIv9EyxsxifEXEF+2A3NFFMh1v4Y5LX3JAkBF2uR56+gWeTxNxf6pzCFG3qIBXuxTiBmuYWhXf/40rPb+4m0A1GFU1L9NDKkj6jyLSmQll+5vhs4roFpXY7eel2u6C83Jozt/taJq87vDB4ogoa5/rrLfB86bZUyTGJ1uQ6Ov5dEHMT8qhNdgqR+918H76la+eNF3dvcS6Ue4vmqlrLzZvljQwhT+xPNoktcW5qsaJAC5C3KSJR6H9wpNeIxZ8KZVjYog9ZjPkMKoz9Nv74W9B/d3z1h7LlbBE+TBOxdCGXdA51kJyU/b7Urr7OdEHOAMCTYmbOOC2Y0rRg994LcISP7MUBKGdrR7AAcQ8szIu2mF/8oV8JqCiO3t35/tawCePAZ1prFYnV+idWrgvhzttFdQ/abYtDuVfsF363xN5GshxN1ZVNrM0ee/Wnc2zYNZIlSaL5pFUSyFx/qpZGhjDO+a7x2J8zWLLT//gAN0Ly3j1H833o1PJ9P+qtLgkFbhiyPn1dFOq92EDW3x9ox4NlOa7EYeLiZRDSUvVk/foXYh9DiVKqBwQB80smq4/r3UAaRvP48JvMz E/jfcJ37 iXz0ixyn55UOmXk9swhOm/6PDFsSs3yCd1pGCW8u1z++PU+ULVODntxw90seH6LyxjBLWmzFUlqu2fHN2YPADfaIcgNc5tN+OwmUKemxFUUktn8ViS9cuQ9vhg/nwf0YHJ88kuL67KPGvZvvLB9K+qIROP0UjgIJs5M0WxLtn3UruDJGhHIzT3M69ft7Ghbp9LArb9cxkidHT0re/hjCTeZrURsH3Qd2yp7cGDHjYsZFB6zagrHldTJdyVYQ9pW0qhNnGikHhkCSvZlbQttqsULcEdpME78J+1PiHv4h2E3tevYMGCdfqcq2t4asBfCV4BWzIx6b1eYc5rAThLPtqsEOb1CM+kh3beu5aSgnTE759YCR1kWxj6VCsIhwmjKDU4Glr8UIq8bU4dexgFxFZhnlGH9EbtRSM4MoQ+vqoe66K6i4uFF78+rxK5H6OVmRHiM5zJy8GYATNipExslctnG+Vzn9sokATWe6gOTVpZ4AYyD6DbocLE9upPpZAm87oZfDNoX7LjKl4Sdz2L0ttkF5dnEt19opEyWnIUsERpCLCYgKSWg5XagYM5GxqA1eU3/r8n75T+MCGVthJd5bxlQjA/GIm78++8SgHgD1z8esJ7Lr98pOrS8uJYAofZoVLLz3jbufmtOxjH7hwdWFKVrhs7USoehHjy1L+X5So/8LuGUybHC7t8uYpzLmrC02jVEkNPwbsRpeGrA5xnGIHZyUe29tvyT4GpM1XOQ/y/Fl05E665tR6LHGDzc4IgCYC0FHRqo6aqYu86m0= 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: Let x86 track the number of address spaces on a per-VM basis so that KVM can disallow SMM memslots for confidential VMs. Confidentials VMs are fundamentally incompatible with emulating SMM, which as the name suggests requires being able to read and write guest memory and register state. Disallowing SMM will simplify support for guest private memory, as KVM will not need to worry about tracking memory attributes for multiple address spaces (SMM is the only "non-default" address space across all architectures). Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- arch/powerpc/kvm/book3s_hv.c | 2 +- arch/x86/include/asm/kvm_host.h | 8 +++++++- arch/x86/kvm/debugfs.c | 2 +- arch/x86/kvm/mmu/mmu.c | 6 +++--- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 17 +++++++++++------ virt/kvm/dirty_ring.c | 2 +- virt/kvm/kvm_main.c | 26 ++++++++++++++------------ 8 files changed, 39 insertions(+), 26 deletions(-) diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c index 130bafdb1430..9b0eaa17275a 100644 --- a/arch/powerpc/kvm/book3s_hv.c +++ b/arch/powerpc/kvm/book3s_hv.c @@ -6084,7 +6084,7 @@ static int kvmhv_svm_off(struct kvm *kvm) } srcu_idx = srcu_read_lock(&kvm->srcu); - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { struct kvm_memory_slot *memslot; struct kvm_memslots *slots = __kvm_memslots(kvm, i); int bkt; diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 6702f795c862..f9e8d5642069 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2124,9 +2124,15 @@ enum { #define HF_SMM_MASK (1 << 1) #define HF_SMM_INSIDE_NMI_MASK (1 << 2) -# define KVM_ADDRESS_SPACE_NUM 2 +# define KVM_MAX_NR_ADDRESS_SPACES 2 # define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0) # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm) + +static inline int kvm_arch_nr_memslot_as_ids(struct kvm *kvm) +{ + return KVM_MAX_NR_ADDRESS_SPACES; +} + #else # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, 0) #endif diff --git a/arch/x86/kvm/debugfs.c b/arch/x86/kvm/debugfs.c index ee8c4c3496ed..42026b3f3ff3 100644 --- a/arch/x86/kvm/debugfs.c +++ b/arch/x86/kvm/debugfs.c @@ -111,7 +111,7 @@ static int kvm_mmu_rmaps_stat_show(struct seq_file *m, void *v) mutex_lock(&kvm->slots_lock); write_lock(&kvm->mmu_lock); - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { int bkt; slots = __kvm_memslots(kvm, i); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c4e758f0aebb..baeba8fc1c38 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3755,7 +3755,7 @@ static int mmu_first_shadow_root_alloc(struct kvm *kvm) kvm_page_track_write_tracking_enabled(kvm)) goto out_success; - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { slots = __kvm_memslots(kvm, i); kvm_for_each_memslot(slot, bkt, slots) { /* @@ -6294,7 +6294,7 @@ static bool kvm_rmap_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_e if (!kvm_memslots_have_rmaps(kvm)) return flush; - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { slots = __kvm_memslots(kvm, i); kvm_for_each_memslot_in_gfn_range(&iter, slots, gfn_start, gfn_end) { @@ -6791,7 +6791,7 @@ void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen) * modifier prior to checking for a wrap of the MMIO generation so * that a wrap in any address space is detected. */ - gen &= ~((u64)KVM_ADDRESS_SPACE_NUM - 1); + gen &= ~((u64)kvm_arch_nr_memslot_as_ids(kvm) - 1); /* * The very rare case: if the MMIO generation number has wrapped, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 824b58b44382..c4d17727b199 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12456,7 +12456,7 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, hva = slot->userspace_addr; } - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { struct kvm_userspace_memory_region2 m; m.slot = id | (i << 16); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index c3cfe08b1300..687589ce9f63 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -80,8 +80,8 @@ /* Two fragments for cross MMIO pages. */ #define KVM_MAX_MMIO_FRAGMENTS 2 -#ifndef KVM_ADDRESS_SPACE_NUM -#define KVM_ADDRESS_SPACE_NUM 1 +#ifndef KVM_MAX_NR_ADDRESS_SPACES +#define KVM_MAX_NR_ADDRESS_SPACES 1 #endif /* @@ -692,7 +692,12 @@ bool kvm_arch_irqchip_in_kernel(struct kvm *kvm); #define KVM_MEM_SLOTS_NUM SHRT_MAX #define KVM_USER_MEM_SLOTS (KVM_MEM_SLOTS_NUM - KVM_INTERNAL_MEM_SLOTS) -#if KVM_ADDRESS_SPACE_NUM == 1 +#if KVM_MAX_NR_ADDRESS_SPACES == 1 +static inline int kvm_arch_nr_memslot_as_ids(struct kvm *kvm) +{ + return KVM_MAX_NR_ADDRESS_SPACES; +} + static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) { return 0; @@ -747,9 +752,9 @@ struct kvm { struct mm_struct *mm; /* userspace tied to this vm */ unsigned long nr_memslot_pages; /* The two memslot sets - active and inactive (per address space) */ - struct kvm_memslots __memslots[KVM_ADDRESS_SPACE_NUM][2]; + struct kvm_memslots __memslots[KVM_MAX_NR_ADDRESS_SPACES][2]; /* The current active memslot set for each address space */ - struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM]; + struct kvm_memslots __rcu *memslots[KVM_MAX_NR_ADDRESS_SPACES]; struct xarray vcpu_array; /* * Protected by slots_lock, but can be read outside if an @@ -1018,7 +1023,7 @@ void kvm_put_kvm_no_destroy(struct kvm *kvm); static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id) { - as_id = array_index_nospec(as_id, KVM_ADDRESS_SPACE_NUM); + as_id = array_index_nospec(as_id, KVM_MAX_NR_ADDRESS_SPACES); return srcu_dereference_check(kvm->memslots[as_id], &kvm->srcu, lockdep_is_held(&kvm->slots_lock) || !refcount_read(&kvm->users_count)); diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c index c1cd7dfe4a90..86d267db87bb 100644 --- a/virt/kvm/dirty_ring.c +++ b/virt/kvm/dirty_ring.c @@ -58,7 +58,7 @@ static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot, u64 offset, u64 mask) as_id = slot >> 16; id = (u16)slot; - if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) + if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS) return; memslot = id_to_memslot(__kvm_memslots(kvm, as_id), id); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 5d1a2f1b4e94..23633984142f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -615,7 +615,7 @@ static __always_inline kvm_mn_ret_t __kvm_handle_hva_range(struct kvm *kvm, idx = srcu_read_lock(&kvm->srcu); - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { struct interval_tree_node *node; slots = __kvm_memslots(kvm, i); @@ -1248,7 +1248,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) goto out_err_no_irq_srcu; refcount_set(&kvm->users_count, 1); - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { for (j = 0; j < 2; j++) { slots = &kvm->__memslots[i][j]; @@ -1398,7 +1398,7 @@ static void kvm_destroy_vm(struct kvm *kvm) #endif kvm_arch_destroy_vm(kvm); kvm_destroy_devices(kvm); - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { kvm_free_memslots(kvm, &kvm->__memslots[i][0]); kvm_free_memslots(kvm, &kvm->__memslots[i][1]); } @@ -1681,7 +1681,7 @@ static void kvm_swap_active_memslots(struct kvm *kvm, int as_id) * space 0 will use generations 0, 2, 4, ... while address space 1 will * use generations 1, 3, 5, ... */ - gen += KVM_ADDRESS_SPACE_NUM; + gen += kvm_arch_nr_memslot_as_ids(kvm); kvm_arch_memslots_updated(kvm, gen); @@ -2051,7 +2051,7 @@ int __kvm_set_memory_region(struct kvm *kvm, (mem->guest_memfd_offset & (PAGE_SIZE - 1) || mem->guest_memfd_offset + mem->memory_size < mem->guest_memfd_offset)) return -EINVAL; - if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM) + if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_MEM_SLOTS_NUM) return -EINVAL; if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) return -EINVAL; @@ -2187,7 +2187,7 @@ int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log, as_id = log->slot >> 16; id = (u16)log->slot; - if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) + if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS) return -EINVAL; slots = __kvm_memslots(kvm, as_id); @@ -2249,7 +2249,7 @@ static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log) as_id = log->slot >> 16; id = (u16)log->slot; - if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) + if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS) return -EINVAL; slots = __kvm_memslots(kvm, as_id); @@ -2361,7 +2361,7 @@ static int kvm_clear_dirty_log_protect(struct kvm *kvm, as_id = log->slot >> 16; id = (u16)log->slot; - if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS) + if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS) return -EINVAL; if (log->first_page & 63) @@ -2502,7 +2502,7 @@ static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, gfn_range.only_private = false; gfn_range.only_shared = false; - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { slots = __kvm_memslots(kvm, i); kvm_for_each_memslot_in_gfn_range(&iter, slots, range->start, range->end) { @@ -4857,9 +4857,11 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) case KVM_CAP_IRQ_ROUTING: return KVM_MAX_IRQ_ROUTES; #endif -#if KVM_ADDRESS_SPACE_NUM > 1 +#if KVM_MAX_NR_ADDRESS_SPACES > 1 case KVM_CAP_MULTI_ADDRESS_SPACE: - return KVM_ADDRESS_SPACE_NUM; + if (kvm) + return kvm_arch_nr_memslot_as_ids(kvm); + return KVM_MAX_NR_ADDRESS_SPACES; #endif case KVM_CAP_NR_MEMSLOTS: return KVM_USER_MEM_SLOTS; @@ -4967,7 +4969,7 @@ bool kvm_are_all_memslots_empty(struct kvm *kvm) lockdep_assert_held(&kvm->slots_lock); - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { if (!kvm_memslots_empty(__kvm_memslots(kvm, i))) return false; } From patchwork Fri Oct 27 18:22:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438950 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 1E391C27C46 for ; Fri, 27 Oct 2023 18:23:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ADD7C8001F; Fri, 27 Oct 2023 14:23:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A632380018; Fri, 27 Oct 2023 14:23:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8686D8001F; Fri, 27 Oct 2023 14:23:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6D02180018 for ; Fri, 27 Oct 2023 14:23:14 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 42DBD140DAD for ; Fri, 27 Oct 2023 18:23:14 +0000 (UTC) X-FDA: 81392063508.28.99AEB28 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf16.hostedemail.com (Postfix) with ESMTP id 56F0B18001C for ; Fri, 27 Oct 2023 18:23:12 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=4IzqyXtQ; spf=pass (imf16.hostedemail.com: domain of 3DwA8ZQYKCDAeQMZVOSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--seanjc.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3DwA8ZQYKCDAeQMZVOSaaSXQ.OaYXUZgj-YYWhMOW.adS@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=1698430992; 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=vy9wKTn0fkDEcqGdy+Uw8qnM4ToO/Gfdy3c1eQgyq4E=; b=D0nic5GfO3WrGK31cwFIli4hIgmi0aIhyzc1fRxOQCuih0MG1ovUcVrukHACAEAiomR5ha clvmelojj/JPQ/KiE/aS6Py+uU9vg15JLz0IILuu8evgldgglnMD944Dy5R7XFc9+Mkx4B 15Vn9sKuMbHQ0LMRtXpSp01PfsSbqhw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=4IzqyXtQ; spf=pass (imf16.hostedemail.com: domain of 3DwA8ZQYKCDAeQMZVOSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--seanjc.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3DwA8ZQYKCDAeQMZVOSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430992; a=rsa-sha256; cv=none; b=5E8SJ9CM9wx0LWHjXNxK/udIXGAiI1knCI4G7ZrrKfGzRmebnNb962m6gmcQTEUGXRqmvc dLDxAgc3alBhe0by55/ZXzWbF2ndvEyfR905DHVD26O7y30dG/BnMvTbuiEHZ/PowSmVbD +KmI+pT30KjsVJb+4KPSVIV9OWjLQJ0= Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-5aaae6f46e1so2074834a12.3 for ; Fri, 27 Oct 2023 11:23:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430991; x=1699035791; 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=vy9wKTn0fkDEcqGdy+Uw8qnM4ToO/Gfdy3c1eQgyq4E=; b=4IzqyXtQsNkZyHR55Wa2NG7ZmYJjDey3YfPuIOSiYTmde8x4WkH6ibd2W8SgHZq1Fg uFgKBnN1C3MDWPYvqEoJ/LzqKi750Ej/wIDBuMo5veMjltYEah/6w4MSL1K67P9pUqS3 K12UfnYYSoU2YiIpt5+3I84rNd6Be4kFLjdFwCIUOXgfjsqSJ+VQ5o1CREONBP0a1vxm YEAzcQvW+VrYhac6sC/pyO9Z2v/FJI/l8hfTBoGx3MdgNhN5lQUKMgpYyDweXjhdw/tl HW+uun1E9ogB4zp8nme/mtNY9Q9zPE8H8gj+pZ22n/wFCW8gpv5aU60VySOQe0RnrMpC mxWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430991; x=1699035791; 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=vy9wKTn0fkDEcqGdy+Uw8qnM4ToO/Gfdy3c1eQgyq4E=; b=Hh1c0qQ6jAt1HEUpj4AopR0YcIh44jjScI0harV2z9ywi8rAzvGL4Bvv6th2UsRVb+ Tt9CsQKBPVWusnB1qr7Q3HPivGMb0xXWod400wFXClyXksF0G4ssHjtwV5F2YOhrrJh5 PSV5qHjMjuSu6V1Rc66N0TLgH6C+mvR4EBmO2Qv5ByksBs02X8WRsvaA+iMJmp1fI978 5n1op59b2oq1regIeXBKMWtS5IwJRF8+EpoWvVEu0BeJSmH9T0hkvJTUfjb4n4D7iZPn l6Vf3C18El2QZgvHXX5hbeXrH8ti7LXQVzsuOxAu8/AbrTumVsrtYO0OWWmm+0YTQMs3 IdkA== X-Gm-Message-State: AOJu0Yzh9k1lHUu3dZACo5bXbqQONepecdzBUtXLZiQ64JFg3/68O13v er5imOmcK60dTYWyamZy7Z43XPV7bJY= X-Google-Smtp-Source: AGHT+IHJ99M17cdlIrIz+y3E5dgMN6oBoPPflGRQ0snggMyySInUDtQaTlwdekdlvCWpXPVtetZE0SP3KDw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d4cc:b0:1cc:1900:28d7 with SMTP id o12-20020a170902d4cc00b001cc190028d7mr82411plg.12.1698430991116; Fri, 27 Oct 2023 11:23:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:05 -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-24-seanjc@google.com> Subject: [PATCH v13 23/35] KVM: x86: Add support for "protected VMs" that can utilize private memory 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-Rspamd-Queue-Id: 56F0B18001C X-Rspam-User: X-Stat-Signature: ocmxistmyntxo3hhoiokw3cb3r7fgpdh X-Rspamd-Server: rspam01 X-HE-Tag: 1698430992-992886 X-HE-Meta: U2FsdGVkX196kufEub5FAVGJVVCZ3ejxGj2EQnE0D7t775uMfP7i5zZxSkVypLa1RV1SO+7nCvqxH75kOUk9CJ2FmSe37gySAuw23uvXtWUtg9b0GqHi5GcP9xKqHgpTxh5qymqy0fGmmTdCy16iWCDTfsMV9wM96zhMny6PxKl2FbtcThrMalYL1nSDRl1HkhVizTyKHX8GiPkcWnERxHBPdF1q6+N6tRivp9fCDuzbZqRsgUhMt6c/cC2OqdPMlxP6+eF4GMYpnGmQsC0zl1cklQaTFlVBuFHzn3vnjS08FERGQWzuJ7jfP8KJ68OeFLPOMV7POLz2/CnUob8hjw0/pb/VCYzCo4ykze4aVCdOb/+fBntmvClO+2LDAYhG1baFju/34eD7jsDl1G801FHF7fVTLmQ1OQfWEe169/4FjgiR++5DmtwclINXtPNI8CTZWhXZzENCNgugr6iRMDCyPpNMaewpApfETSsQmyWVbdmS5AGGB+HvoAOb6KJ6i+WDG2PGWtoGM7BpJgJZFUd76JgTRBfKUBBMDn1ERZxICDBVZ40hkuI6oO0NIWr9viBa7YVui32qDTrjKRNDpYF2oVhvY7lbfNfVylpRTzobH/BZt3WT6T/GXutyBK9y7yf6fHIaS//GwPmFz1rNPuxlEYDCikHwk9oQUROiahqmageMDfhIr1VmfkEk6c3St4FQflenyI0T3N/tKYp1LmxzwgfRI8KuGYJ0qbrLkAjA7eDXUxdHJGFSvqYLisTuwE8DoAPWl6ObPXaWYEpHsTvqCDCIgoWyE9ul9gb92YrLvHCyo6Z0necCegsoutdmpXbaR498pElR1F66XABxvop/lX67jfvBasgBADTZ4CISfRIFJFau5SVFwGLsth4z0g6DhRZpcT5Y3xBuVlYfdgnNfOl6iD3c4mzpKSO80dk5rzX23FCVXbN2exsu8+GHqbn0cip9ErAbpSoVrd8 0mfYT5mr BA86NAjaNsekbrcJYzmSCmj0qNI4+z1WwRUwHomLFrCcD/IseSQ2Z9w97TQ1GTF5H3EKF0HDbKLIItw/ghLvzXjSw5H6n8qbIyXb+qHJ+IBFNOHpC6QADkjThS5lqHYt4ipFYerYcQYiPvzpObJBt9FQdTYHG5Mg4vsQmKP4GucOF/mhL9XCnjwrBILNnOa3MOzEa/+/sHytkfun1T/HXAQ6kln/R6zaX9yO9oMwnMijhtlUFWOMVglp3d55LhwuGHj8ZYKnl+ELl5PisEv+l55mAivmVtlcsVM1cExuAlxOlZosaKrwQ8ct5HNMOolRBjKoLr3W56ghxVPK9VLooyw/ORIa/QcGZH10/bx2r/3M+aCqoRrDQ/0VRTiYkjmqsyGfk88QP/GCinGlM3jMIoiwxuY+4yckNQdumQF/PGa7g5ZDkdh06f1m6YCL0PyxQkcHYD6UdfocGAdG6EO26pYha2+MhNrek2bcspS+iVf1rmQcLeY/+Z6Jj+75MdsQTTg65yALeZt1e8Ajen0d+xlBT0wYS7oKJJHFFJUemz8x62aaFKkCS4m3Ar4G64aA64L5HXW2quLfVx4e+tyCmwxY15OBGbaEpCP8DhaAuoz1kwKOv07ge5u1UHYFbialvsGO6tT/dwrDDCeBAuwboznVjz3p9mMsDP1UCmRBfZ4rDgwnbcC0twi/mmHKbTJeVO3CM3KJL0CMSsAlInHStCUqm5Ytl/IIaWl7h66mdbVJol/CdsbPbliSKeepYK6C2kiYj 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: Add a new x86 VM type, KVM_X86_SW_PROTECTED_VM, to serve as a development and testing vehicle for Confidential (CoCo) VMs, and potentially to even become a "real" product in the distant future, e.g. a la pKVM. The private memory support in KVM x86 is aimed at AMD's SEV-SNP and Intel's TDX, but those technologies are extremely complex (understatement), difficult to debug, don't support running as nested guests, and require hardware that's isn't universally accessible. I.e. relying SEV-SNP or TDX for maintaining guest private memory isn't a realistic option. At the very least, KVM_X86_SW_PROTECTED_VM will enable a variety of selftests for guest_memfd and private memory support without requiring unique hardware. Signed-off-by: Sean Christopherson Reviewed-by: Paolo Bonzini Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 32 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 15 +++++++++------ arch/x86/include/uapi/asm/kvm.h | 3 +++ arch/x86/kvm/Kconfig | 12 ++++++++++++ arch/x86/kvm/mmu/mmu_internal.h | 1 + arch/x86/kvm/x86.c | 16 +++++++++++++++- include/uapi/linux/kvm.h | 1 + virt/kvm/Kconfig | 5 +++++ 8 files changed, 78 insertions(+), 7 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 38dc1fda4f45..00029436ac5b 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -147,10 +147,29 @@ described as 'basic' will be available. The new VM has no virtual cpus and no memory. You probably want to use 0 as machine type. +X86: +^^^^ + +Supported X86 VM types can be queried via KVM_CAP_VM_TYPES. + +S390: +^^^^^ + In order to create user controlled virtual machines on S390, check KVM_CAP_S390_UCONTROL and use the flag KVM_VM_S390_UCONTROL as privileged user (CAP_SYS_ADMIN). +MIPS: +^^^^^ + +To use hardware assisted virtualization on MIPS (VZ ASE) rather than +the default trap & emulate implementation (which changes the virtual +memory layout to fit in user mode), check KVM_CAP_MIPS_VZ and use the +flag KVM_VM_MIPS_VZ. + +ARM64: +^^^^^^ + On arm64, the physical address size for a VM (IPA Size limit) is limited to 40bits by default. The limit can be configured if the host supports the extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use @@ -8650,6 +8669,19 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a 64-bit bitmap (each bit describing a block size). The default value is 0, to disable the eager page splitting. +8.41 KVM_CAP_VM_TYPES +--------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: system ioctl + +This capability returns a bitmap of support VM types. The 1-setting of bit @n +means the VM type with value @n is supported. Possible values of @n are:: + + #define KVM_X86_DEFAULT_VM 0 + #define KVM_X86_SW_PROTECTED_VM 1 + 9. Known KVM API problems ========================= diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f9e8d5642069..dff10051e9b6 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1244,6 +1244,7 @@ enum kvm_apicv_inhibit { }; struct kvm_arch { + unsigned long vm_type; unsigned long n_used_mmu_pages; unsigned long n_requested_mmu_pages; unsigned long n_max_mmu_pages; @@ -2077,6 +2078,12 @@ void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd); void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, int tdp_max_root_level, int tdp_huge_page_level); +#ifdef CONFIG_KVM_PRIVATE_MEM +#define kvm_arch_has_private_mem(kvm) ((kvm)->arch.vm_type != KVM_X86_DEFAULT_VM) +#else +#define kvm_arch_has_private_mem(kvm) false +#endif + static inline u16 kvm_read_ldt(void) { u16 ldt; @@ -2125,14 +2132,10 @@ enum { #define HF_SMM_INSIDE_NMI_MASK (1 << 2) # define KVM_MAX_NR_ADDRESS_SPACES 2 +/* SMM is currently unsupported for guests with private memory. */ +# define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_has_private_mem(kvm) ? 1 : 2) # define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0) # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm) - -static inline int kvm_arch_nr_memslot_as_ids(struct kvm *kvm) -{ - return KVM_MAX_NR_ADDRESS_SPACES; -} - #else # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, 0) #endif diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 1a6a1f987949..a448d0964fc0 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -562,4 +562,7 @@ struct kvm_pmu_event_filter { /* x86-specific KVM_EXIT_HYPERCALL flags. */ #define KVM_EXIT_HYPERCALL_LONG_MODE BIT(0) +#define KVM_X86_DEFAULT_VM 0 +#define KVM_X86_SW_PROTECTED_VM 1 + #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 091b74599c22..8452ed0228cb 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -77,6 +77,18 @@ config KVM_WERROR If in doubt, say "N". +config KVM_SW_PROTECTED_VM + bool "Enable support for KVM software-protected VMs" + depends on EXPERT + depends on X86_64 + select KVM_GENERIC_PRIVATE_MEM + help + Enable support for KVM software-protected VMs. Currently "protected" + means the VM can be backed with memory provided by + KVM_CREATE_GUEST_MEMFD. + + If unsure, say "N". + config KVM_INTEL tristate "KVM for Intel (and compatible) processors support" depends on KVM && IA32_FEAT_CTL diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index 86c7cb692786..b66a7d47e0e4 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -297,6 +297,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, + .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), }; int r; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c4d17727b199..e3eb608b6692 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4441,6 +4441,13 @@ static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu, return 0; } +static bool kvm_is_vm_type_supported(unsigned long type) +{ + return type == KVM_X86_DEFAULT_VM || + (type == KVM_X86_SW_PROTECTED_VM && + IS_ENABLED(CONFIG_KVM_SW_PROTECTED_VM) && tdp_enabled); +} + int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) { int r = 0; @@ -4632,6 +4639,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_X86_NOTIFY_VMEXIT: r = kvm_caps.has_notify_vmexit; break; + case KVM_CAP_VM_TYPES: + r = BIT(KVM_X86_DEFAULT_VM); + if (kvm_is_vm_type_supported(KVM_X86_SW_PROTECTED_VM)) + r |= BIT(KVM_X86_SW_PROTECTED_VM); + break; default: break; } @@ -12314,9 +12326,11 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) int ret; unsigned long flags; - if (type) + if (!kvm_is_vm_type_supported(type)) return -EINVAL; + kvm->arch.vm_type = type; + ret = kvm_page_track_init(kvm); if (ret) goto out; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 29e9eb51dec9..5b5820d19e71 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1218,6 +1218,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_MEMORY_FAULT_INFO 231 #define KVM_CAP_MEMORY_ATTRIBUTES 232 #define KVM_CAP_GUEST_MEMFD 233 +#define KVM_CAP_VM_TYPES 234 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 08afef022db9..2c964586aa14 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -104,3 +104,8 @@ config KVM_GENERIC_MEMORY_ATTRIBUTES config KVM_PRIVATE_MEM select XARRAY_MULTI bool + +config KVM_GENERIC_PRIVATE_MEM + select KVM_GENERIC_MEMORY_ATTRIBUTES + select KVM_PRIVATE_MEM + bool From patchwork Fri Oct 27 18:22:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438951 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 C5038C25B72 for ; Fri, 27 Oct 2023 18:23:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66F8980020; Fri, 27 Oct 2023 14:23:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5FA3D80018; Fri, 27 Oct 2023 14:23:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 423F480020; Fri, 27 Oct 2023 14:23:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2BA3680018 for ; Fri, 27 Oct 2023 14:23:16 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0AA5740D0D for ; Fri, 27 Oct 2023 18:23:16 +0000 (UTC) X-FDA: 81392063592.12.54C54ED Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf12.hostedemail.com (Postfix) with ESMTP id 4068840002 for ; Fri, 27 Oct 2023 18:23:14 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cjNv74Az; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3EQA8ZQYKCDIgSObXQUccUZS.QcaZWbil-aaYjOQY.cfU@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3EQA8ZQYKCDIgSObXQUccUZS.QcaZWbil-aaYjOQY.cfU@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430994; 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=+S4AiipPfUUKpJ2ruaZ4ElxK7cYS7w8+XYANZ6GAwNM=; b=CE9174exbTgXcfocc1KCc6ZCgsDmdVJuqH9dGZiwQ6pEjh9qbCn/bnSeiMrdPvFnXEnBFv k6MNwvG/DghBdV87euw8ZoO6lexjK4h0UQr6Rn3H/74fwbRbYcCewVXXshgCciBRsFaSJI 3EpubL1PpbNDRJCbn7r4ttPNdqOK/UU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cjNv74Az; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3EQA8ZQYKCDIgSObXQUccUZS.QcaZWbil-aaYjOQY.cfU@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3EQA8ZQYKCDIgSObXQUccUZS.QcaZWbil-aaYjOQY.cfU@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430994; a=rsa-sha256; cv=none; b=a9vgE33a4Nz4iryWW74YJtaL4O0JDhoOr0aM2Nh4MK0HwarBV1Aludo37ks7lDyW/sNHRk dPpd2EVeGc+nos3/A8q07j45MVS1rd9r6h5GEGm2+yLqCokMCp+VMItaDc18CYJ3/i3OTd BSviiqIUYhqOi/RLKLGL/ft3uzNfuBg= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1cc252cbde2so6362855ad.0 for ; Fri, 27 Oct 2023 11:23:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430993; x=1699035793; 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=+S4AiipPfUUKpJ2ruaZ4ElxK7cYS7w8+XYANZ6GAwNM=; b=cjNv74AzlsjIK+bN7tsQStNwM08cYWP3QgQqX/tmckPZCiDcWAG8Gw5/R0FE3fS83a gbdmkoRg9yKdAjFFkTwnbEAK4ZdVcJljQ/eVgCY1jYUoNuH9su7RD4YhTliWdsK0DQZe aJlYJ+dfZcyL1wNjUKpwHPuvXOMgLX/0tlp7u9BECQnb18d9fvZz8+3+hUw+2cFRSd6W pECmqRBPe2vYLnBu6KFT+xONhclH10VcZ43T5yIzHaaZzc/0+91S2NYLo8zSt6GStJ2x x4RAeZ3c9Gdrub2wepAJa6xvgLGc4Fx8UPRl76cuJUw2JNxMeE85Uf1bZLdFNvDm05lK Mkxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430993; x=1699035793; 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=+S4AiipPfUUKpJ2ruaZ4ElxK7cYS7w8+XYANZ6GAwNM=; b=feGtJDzroMGxX1Bg+yrCAvHQkyBJYzy0fHXyP8ghLuuvkoAolZkCVoTiOINsvhOHNu U5QJP0LE9QlS9rZMt9zxedTBcYkUoQ6WAy8dPceDWy/O8oAHbG/kBbqxP8/vz8GC8X91 PX4gbVZMrx5UjtABCs67q3pwQv21PZ2ybQQJu68lUwjjvLVwVvW92BhpJHO2vtB/F/Ts PrG58ZO90POCNd7D2EvG5lkhHY5WY3wkJjozFZFu5i8cz1b9nN+ZySY1ZSPdZHp9uCl3 ZekIvIByneGaPhgokmJMYtJXGi3lEbmNCO4NIGGLfCKW8ms6lDpcPwi3sYQfp+dgll1G 8Spg== X-Gm-Message-State: AOJu0Yz2XNpY0jGKNvWpksLkFkO4ZH5R0rxf2qOrs8vB39StOrXX742f bluQt4F/RfF3q0mSIx+5jpO4TL5PHPk= X-Google-Smtp-Source: AGHT+IEqIkKuv5DX25uHuP2fYVc0fUSbsifhQGdV1yLQ889bX1DKci/qdjsCIFlDcD5dVWlFtax8M8nrsME= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:4296:b0:1c9:e3ea:750d with SMTP id ju22-20020a170903429600b001c9e3ea750dmr58852plb.11.1698430993223; Fri, 27 Oct 2023 11:23:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:06 -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-25-seanjc@google.com> Subject: [PATCH v13 24/35] KVM: selftests: Drop unused kvm_userspace_memory_region_find() helper 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-Rspamd-Queue-Id: 4068840002 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: wty9z9rrncc4mba6xf34m3iq1a6puzkz X-HE-Tag: 1698430994-653585 X-HE-Meta: U2FsdGVkX1901uqWLGow4m1OuMIWNoNgZRLydKlxK9tu3+3wSQ4CczzSzKnT6vg/X/K16JFEF0shd6WeCgR5QrCqlCgNr3rnu6ULH+Y8RvEguZYBWcpDjG8Y7KSn+d9y4c5iu7MHSjUD1PVlgvwnqSqYy9xO8UTqJcPlLQ8OMHQI9KWKef7Ih+5WCq6Daq4Nf2Cuxe0bOE8GDvwgvGtr/PQ1akqvjRwnhXMeITXH/MfTeAnSWhZf+qdd3zCox44Qm4iQIpyO4V/gCqRfEDcIX4ChqVIGaEDPHnFTZetQJEhe2UfGKmlOXFOqbB2VnxK6rXCZmMW2t1CR8rZKBG/3O/xu7BKOK5HVfJdgbkIwJLNSM4L4VvZO5tIHD9TGF60VR1XW7VEtNusplPDEaeoTD539nOAVzGIgc1347Zy9ROblEQc7bB9isR6lveL9l+bknZazSRGV01as6ZJZVc3+T2qYluqFemW/iZmHQPty1ta+VnnbwrNOgcSIBrpb43kSiL9iDmUfx6Xf70d8FgD3uAIvhRo7XxpLmuPkDikBSUoyQ4rPztZc8zirkh+I07TXtHw8R2aJBB8V56lM6nOswhgeVxaM0IJi/9GUoTIqJ7MK+mNidAJdYfJ95xv5INQyaJb0PxNgMPsk/7ePoA0dvFhgfQ2igojfIeSqJAM4ahS3M2LvOHMRhUI4o/cDuDSe/xt5OQmzEBISBDuYewLjDBFGqNrjvVViAUT3P0LUHcr9HJeFHu3TZ+Hb003Qz6B/Rr1aVQwAxbBND/pJD010YEooOUO8lC9SxDjhCTdwaCVSK7f+QASDj2fJTP67DdlTWn/uFeXcpuUxYKLorxyCblilvjeBVii7wkAZosqj/mYItkQtEKBn/FlSeB3AiOxkwZqsrr8cV6GZVDxufwbkmkTOcreCgABMkLgIfRTVhAsPubY2bdYib3EQ+t0bkL8vnqhdHWDmN7WPG/oGUzu XxKljNx2 VMAdhvGU+ybJQyuDnZUJdYsazTBsEPWGcjGI1VL8O7g75ZCYRYIlqptBM0Q1MhonoQ0SBZVmjwusSY3UJcTBiWEVredQOcJ7FRvzDv6nYAo86Hx6nSuErnEhb7uuykBODx3dgjQ8GJsLbs5yeDkky+GYcetv96vSkFe7MGCIjoggdkxymW22zeKqT5ZSrB4j1mgUrMY+Mu1MavcuwwvSRITsll4ezZ3sOQo3IKLFjrc2z4frJHj+GNIBjsOvYqMUTzqKCpDK+7xY/Z/GxUxlPpQPjXhukqPi3MuafehOVFwlSXS4W7wImHY6IfcQ1QaEqdUgXy7mkF/2I9FwHQgRF/sPvoqTAr6XsW8G2d5kpDGwrK2sEn8HJWhvxiTarlS7YPMoxIY03bQ+Jin1kWnLKE9YFUc0itHaXRlOi3P6WrKimtZdNnh0l/oq0xR6IrJIa6fhevyHyKeMyhmhZx1ZjjrxsbugFu489zgRxqHN3Tg3a/XhGYi7+n1QP0UkF2bQkP9Ko99ejxfKJTaZiGcVxXnTo0ktkUzFs86dpfLpbZMkWzjzfMAvMPt1koZVivHPAiBduVLt/0Wadi7aAufF3Mt/cU4yyQUx9q9TTHnyTHz3yPlMsP+h+BrJIHQdk9QlpGhULFb4F5DCUVjlH3alN+MLb+4S7ZGPb+/KN8Z7GVO5gSk7OmohvaM2SI05edtKBQotG4xcZ7kywHq+5EhiaWetcuMT8GAdp9mYYHchuFbBDzKcFZ/osCe3Z2nE+9QUQuz70WWoUVSRPuaXQ13OrPhiIWOaUQ9f1TkxtLJX6BC/vNHqGHSx6a/+YyA== 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: Drop kvm_userspace_memory_region_find(), it's unused and a terrible API (probably why it's unused). If anything outside of kvm_util.c needs to get at the memslot, userspace_mem_region_find() can be exposed to give others full access to all memory region/slot information. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 4 --- tools/testing/selftests/kvm/lib/kvm_util.c | 29 ------------------- 2 files changed, 33 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index a18db6a7b3cf..967eaaeacd75 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -776,10 +776,6 @@ vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages) return n; } -struct kvm_userspace_memory_region * -kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start, - uint64_t end); - #define sync_global_to_guest(vm, g) ({ \ typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ memcpy(_p, &(g), sizeof(g)); \ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 7a8af1821f5d..f09295d56c23 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -590,35 +590,6 @@ userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end) return NULL; } -/* - * KVM Userspace Memory Region Find - * - * Input Args: - * vm - Virtual Machine - * start - Starting VM physical address - * end - Ending VM physical address, inclusive. - * - * Output Args: None - * - * Return: - * Pointer to overlapping region, NULL if no such region. - * - * Public interface to userspace_mem_region_find. Allows tests to look up - * the memslot datastructure for a given range of guest physical memory. - */ -struct kvm_userspace_memory_region * -kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start, - uint64_t end) -{ - struct userspace_mem_region *region; - - region = userspace_mem_region_find(vm, start, end); - if (!region) - return NULL; - - return ®ion->region; -} - __weak void vcpu_arch_free(struct kvm_vcpu *vcpu) { From patchwork Fri Oct 27 18:22:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438952 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 49CEEC25B48 for ; Fri, 27 Oct 2023 18:23:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4035580021; Fri, 27 Oct 2023 14:23:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3861280018; Fri, 27 Oct 2023 14:23:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B50180021; Fri, 27 Oct 2023 14:23:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id F243280018 for ; Fri, 27 Oct 2023 14:23:17 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CF075B5F1D for ; Fri, 27 Oct 2023 18:23:17 +0000 (UTC) X-FDA: 81392063634.26.1E3DA05 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf17.hostedemail.com (Postfix) with ESMTP id EE21F40008 for ; Fri, 27 Oct 2023 18:23:15 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="P3Y/qcOr"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3EgA8ZQYKCDMhTPcYRVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3EgA8ZQYKCDMhTPcYRVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430996; 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=ZNZtzA/linIp+2GMXA1XUg0X+u1GFwKPof82zA2ctug=; b=iDdYodT/SkUiA5WYGEs16mQwDkSjZqwEr3xtdV2qc+bb7yZieeoLzTE8DzNuUVaBlDNzIz BbCCHJA7mZjCWU1ZlHXLFpJA8AHldNgw5LHauAGNV1VSF2UP46SbevMkNZjciCUPiBkwyw SfqIDwVl0eAesGMGVLAPxdLiaCFrYvo= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="P3Y/qcOr"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3EgA8ZQYKCDMhTPcYRVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3EgA8ZQYKCDMhTPcYRVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430996; a=rsa-sha256; cv=none; b=gvKZu1Xx7eHnhL2POt2jAKjgC/GcB/4iYYzaSRNzTnpnHuN+iao04c7owssT74VNMEUmVV KNzi96zreO+tKk8M4zzd5t/UHmaJlCQwzE9ANINKmBMu+L+fFfXOZ34GkVh6FJEIOoNjti 5tSZ8lL80umzkmOFg6foVvH1FfjZo0g= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1caaaa873efso23355005ad.3 for ; Fri, 27 Oct 2023 11:23:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430995; x=1699035795; 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=ZNZtzA/linIp+2GMXA1XUg0X+u1GFwKPof82zA2ctug=; b=P3Y/qcOrCnAHUHSCKRl/1rWbeqnC9TOWLQjIcNvWdmiudX43Epv595unqj5ydHVmUB IpaBHNoYuTz0tWJBoMYPoeAIuYM0LX1tUmMDOscD0m0GZdnv6ArlazrttxpneGmB7uTQ Zb7XdtO/btoNMBtUJh/lXS6QRIYlwOKVbP9tibeFecys/w+1PQ34tv6TQ+3wn0bCpXyW RNZ27kc3aLxJSSibYVR+kK73ka3TrEQ6D8xhVqfvwpWCyingIAptJTsHd35NJhQzgQhW e/4jYFZmsLjysJlFKAzrclvOlFlRFvj2LDAHGvfHZ7/PN7eVstNqoY2fTYsABZGZE+Kg OApg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430995; x=1699035795; 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=ZNZtzA/linIp+2GMXA1XUg0X+u1GFwKPof82zA2ctug=; b=cGas6ZtDLG0i1tnTmLQjD1wOIlgUh57HOciBhZVTpMGrQiL3Y3xquykrVnwDv65bML y1VZS2E8VJjZPapMuOAj4IcCR9MjyxzgfA0UsnT8mpCZp50S9dpEfNoZUhma8R7B2xxT 9mA7dveX5jJ75gaHbi9PGO9T6vUoMmmxp2QIpGtiDHtXnRhPYnbb0NsgA5R4+CVn04oa 8KeUQZFuJErA5h0BrBZPKwaIGJ1f4a554FmoP7ZmigyTMYH42+VhKfnljmG4CAeNUkbD tmObTpkTuaYDMeyFgWIcsPtoRt23EIXVBBsQ6fdgLholHH7tjC0fJjhnBSWwI7QqSO+u CFVg== X-Gm-Message-State: AOJu0YyVbewT8K4tzLWrLSvyCNhLSqD5Sdr97VG+d32bhjD1EgT3ACQH Z0/qBI7hSBQP4S53WlVqpfrOJ/VHPuw= X-Google-Smtp-Source: AGHT+IFgEK+vNYv/vNHgA3JkDytYkMCwP8RspbM7NAIhoWJ8dWWtLUmat77zU+/1XadLWoCAT7TkthpzXqU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f809:b0:1cc:2a6f:ab91 with SMTP id ix9-20020a170902f80900b001cc2a6fab91mr26127plb.0.1698430994917; Fri, 27 Oct 2023 11:23:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:07 -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-26-seanjc@google.com> Subject: [PATCH v13 25/35] KVM: selftests: Convert lib's mem regions to KVM_SET_USER_MEMORY_REGION2 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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: EE21F40008 X-Stat-Signature: mrbd5n5ddeqyk4369chc7xmafybsb943 X-Rspam-User: X-HE-Tag: 1698430995-418831 X-HE-Meta: U2FsdGVkX1/MGzcANfSgpKygxat1Tk3FK7tS5Syf9eCoCMoYqrRpyhY9IRshwepz1EZpTQ/tdm9ZDofWFhetCO57SaiNg50yWPElA1TluNmEDQM9Npm8nbfzbdrPIzSCpqQxGFHSWf4oB85y7vpHwt4mkLYt1VuY0cpTgPih+YLxJ+ctcMbJgVKg+iot+d0Kj8gAjwl5RlMwDDYgTy4rZzG6mbhFuAYPKoO8oe1sB8t0PUz9Sb+whb26BC/u5OMskflOpuwZBaI2AHgtZjZXcfT/U+G6oxMWxh2gSP0C61885V754xscOPYSoDZc6hxeD9qQ+rhB0P0xqeL/f4IzQi920ygXqORqbn3fzVAQUcy9aMR/N1Pg/WpDzI1ggEtjr0tqAxUwxnwvQep+E7FeluhkzJj09k3SR6IixRh/sNOQ6Eo+zQ3x2JDN3q8Fqzn/72uag4eOcPszlw/5LN4L1Vlm7CQ2E8cg2Y3P4AGrKtvlkgbkaKEupME5tOZOVYUZsjJ0foCzZlOVwwqY/gWVoRfgvRNxA0c4xhrk2/AE6aCmfPzdaiuuNeBHL4KlJRiKJOKFMHj3VkFNGifM+A0Pd5KrEBWu+jpeel18ntDbtIA/lld8m2gGRyJVLG/ihhcmPIOExe0Vis9LQ1P/ieQWPn7Vjl7/WRtpSMXlYKgWg483xqMGLwtOtu9QYFIE+82XreigmrfCqXzTCf0xMBYR90ROsO7fwftVaNMZeOeRxCEKOb15GsNIFs9T1p2UNRlGNITML9q7xXCLicgy3vGB2dDGk9dhsYOsSziR5B5QQUQelpUtYElTylc1H8fydj5ZR3GbBYzcB/oPAFJfP3E09HPZmqSe6UT+xwwS5AKQGmiXxhfHmGI9rbrpODWZCCDPIAxE20CdTd9NNFxRghQ9XX03geefNZLtKDwUOJcfbzKOS3tgeHhSwutXf32bBf0R4YofR6ItvvAdgsn/DF1 6OO+zOjD ejLZAFpy6MC61zRrjPywqlulP+d+S3IC7MA5KcObTWYlbW+0OXF8/WQCa1cFSIp1Jv7Qm9hd7JaSZyvKlPYwgPz6CvQOHMwnPoadbBN3K02e0GdWOZc/XvOrbG6uLQxHKvRzO4oIFDbgW4/Fh0+2i8b0z2zD36atpUgR2HawO6z3CYHpsImUqQExf9klVdCQpOrQ0RCi7ie48MNdndMeW8NdCJI7MNETLoh38QVHRXnjMRHOFgDiMtXGqpjV695utKOMqcTGyV9d493XXC/O04kkLO++/4yL3LgkteDO3kRs8BZ6MYhvWtuLVtnZY2l6roAe5KB7ZFrSD6SMmAFrJWe1RlwPPcl3Vfe9J0kysZZvRNkzElfZ49RWUSQ0P4sc2WLE6YWZl3Pk0SqG7kJPUSN9f63fw7uqDfPf/hh1o/fYA+FPAza+eXXlWGkADXsLAQEc0o3G/XFLXW4i0L2QlJnUXdupFtixGSvjZaZ3jn1sn7TsAEutKS+GCgahIT590qrpwk0HvpNGcU9ceH4z2uj8tl5l8Rnup3VyOL5JYK+HDZg2MCzezNM9zwW5HxnQCW0H9l4xBMshhLIqwSSTVbf0FC2835FkSqrk18tdjAgcW6IxMf1uuQWK/AeauBspHtO/symO1BIsZAFuf9evQewnve3hCVRHhRpB43kNQxANdVhcdCTSgwi/vgLV3csCZ8hwY1nXtuCtaWeuSqQS+o4SzbHp8M6pdBo62WL2enbcukQePUstJPyMxDxUeRHi0TYq9+67mbbPAVBTZ1doOAa4pl4PnLuto/urHe6tiObr9txSRAIrkjIydBd9aRgNT++Wj 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: Use KVM_SET_USER_MEMORY_REGION2 throughout KVM's selftests library so that support for guest private memory can be added without needing an entirely separate set of helpers. Note, this obviously makes selftests backwards-incompatible with older KVM versions from this point forward. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 967eaaeacd75..9f144841c2ee 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -44,7 +44,7 @@ typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */ struct userspace_mem_region { - struct kvm_userspace_memory_region region; + struct kvm_userspace_memory_region2 region; struct sparsebit *unused_phy_pages; int fd; off_t offset; diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index f09295d56c23..3676b37bea38 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -453,8 +453,9 @@ void kvm_vm_restart(struct kvm_vm *vmp) vm_create_irqchip(vmp); hash_for_each(vmp->regions.slot_hash, ctr, region, slot_node) { - int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region); - TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n" + int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION2, ®ion->region); + + TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i\n" " slot: %u flags: 0x%x\n" " guest_phys_addr: 0x%llx size: 0x%llx", @@ -657,7 +658,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm, } region->region.memory_size = 0; - vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); + vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); sparsebit_free(®ion->unused_phy_pages); ret = munmap(region->mmap_start, region->mmap_size); @@ -1014,8 +1015,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, region->region.guest_phys_addr = guest_paddr; region->region.memory_size = npages * vm->page_size; region->region.userspace_addr = (uintptr_t) region->host_mem; - ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); - TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n" + ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); + TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i\n" " slot: %u flags: 0x%x\n" " guest_phys_addr: 0x%lx size: 0x%lx", @@ -1097,9 +1098,9 @@ void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags) region->region.flags = flags; - ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); + ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); - TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n" + TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i slot: %u flags: 0x%x", ret, errno, slot, flags); } @@ -1127,9 +1128,9 @@ void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa) region->region.guest_phys_addr = new_gpa; - ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); + ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); - TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION failed\n" + TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION2 failed\n" "ret: %i errno: %i slot: %u new_gpa: 0x%lx", ret, errno, slot, new_gpa); } From patchwork Fri Oct 27 18:22:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438953 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 01FC0C25B48 for ; Fri, 27 Oct 2023 18:23:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 361FB80022; Fri, 27 Oct 2023 14:23:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E9CC80018; Fri, 27 Oct 2023 14:23:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 079C380022; Fri, 27 Oct 2023 14:23:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E3D1780018 for ; Fri, 27 Oct 2023 14:23:19 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B2664120988 for ; Fri, 27 Oct 2023 18:23:19 +0000 (UTC) X-FDA: 81392063718.10.F650385 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf07.hostedemail.com (Postfix) with ESMTP id D13BA4000C for ; Fri, 27 Oct 2023 18:23:17 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Ts/Q51XK"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3FAA8ZQYKCDUjVReaTXffXcV.TfdcZelo-ddbmRTb.fiX@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3FAA8ZQYKCDUjVReaTXffXcV.TfdcZelo-ddbmRTb.fiX@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698430997; 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=RLaDQqSQ3lzZ0t/3jaQVA6mY285XfPpfvSiTXdbQLio=; b=8Fcpgl6A7CWHJNTy2g+sBxd64y62yNiy3alMxldq/IfCeB3M8bW7o0rXTya1+iic1gvL4Z 1OWtFRG4RSNV5EOksW/m/b2PH/fgVR1xzIofcFQxY1ZYOrTd1u/7trNqo+tgkyC9iPvH6s qU/St5nSn4pVqIHxeEXXntnc1oQHvtM= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Ts/Q51XK"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3FAA8ZQYKCDUjVReaTXffXcV.TfdcZelo-ddbmRTb.fiX@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3FAA8ZQYKCDUjVReaTXffXcV.TfdcZelo-ddbmRTb.fiX@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698430997; a=rsa-sha256; cv=none; b=OF6Ww0lVaVzO0A7RnWOqhOMfoirAP5gPft8SbW4sCyVdlReKPPlhDzpWQSSavCuWMxZOZA FP32M88zuruscWL8iFje6se1fqb6LuCqXnzEu+bFbmfcNPHvFWNaX781sJ3K+7sPD8ivG2 23bOlWCpw2xoLH7M7kFgcTKPDmQf6+U= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-da0737dcb26so1813515276.3 for ; Fri, 27 Oct 2023 11:23:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698430997; x=1699035797; 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=RLaDQqSQ3lzZ0t/3jaQVA6mY285XfPpfvSiTXdbQLio=; b=Ts/Q51XKnFdog0bgtAjBfypmfjkZGYwcSDSZ3iWm2ew/N7ojWlwcisgFrjYDLQdkgu Rxt7JiyXhbXFDg/3dFdd/mENdS1G7DSdmyPkj+bkYbbfSsZVGq029w+bz2PVPPy/I95Y 8vWSY3hUiMAlTsj8ZEJ2G6T0O7Oa9xgZlcODK1aNgJdZ438wxb5g3s3xGHwC9JhV++92 juSBIdG9juteyEkTD4soChflDvEayiKNaCOMtC2ngvDnquVSbr/ufOsWXnToE+b/JVep bqOx5V9QS2okrro1ZGyTyQNkNsI4pxUbMoWRsafYhaLne7VqbFkEcl5oFlWuH+iN+uWi ftKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698430997; x=1699035797; 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=RLaDQqSQ3lzZ0t/3jaQVA6mY285XfPpfvSiTXdbQLio=; b=VSGjubjjsvfqlqrfsOJPZkjwDKm9nvCrrC90zOyjYU54fvU/+vrHCix0ZbSIwLX/Gv 0W+IbVVosVIL+8MRsUmXNlOSJLMdJRMBfTfOyalz+0BePboZc0nLUbiqcih/Qk7jOxlQ Jvj4oUA+OauuaKToUmmdVKRDKlAkIP54uoLHFvmCSc/6t9uWBI2KnLJPDRsW6cGZy6L8 vnlNeWLCFj/z7i5/r195p6NxNupCfkc31a4Bh7uWmfXdn4N23upWLWrXJY/0NMcNu6db sOqdpNmVnK0UvaQJGFnYyyV8F2Oiw7nIeod9vfaRYDSBYgxcCfFfEQDhGIRZoZP52yQu sTdg== X-Gm-Message-State: AOJu0YwYISZ4oO1irZqK6apMS6FL0gRWtaHzi/c7ZCL0Kf/NcVfwPWK1 B0aXgJjRAYZ1uTNLlq3YYGD4l/DJXLs= X-Google-Smtp-Source: AGHT+IFVwKsM7vhHHbindB36SAJ//dsM5jy6/DD1JTv8kubZidgUJZpq/2shomqVAZOPrnOCRBDGbJqIEWs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1782:b0:d81:582b:4661 with SMTP id ca2-20020a056902178200b00d81582b4661mr65203ybb.8.1698430996901; Fri, 27 Oct 2023 11:23:16 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:08 -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-27-seanjc@google.com> Subject: [PATCH v13 26/35] KVM: selftests: Add support for creating private memslots 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-Rspamd-Queue-Id: D13BA4000C X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: p7hx6ixwjze69mrzpgccwzjzx48zit46 X-HE-Tag: 1698430997-344453 X-HE-Meta: U2FsdGVkX18wcU0u6nUwpHmDQC1FTVc0F0OjQjRRNK6AuCb+dQ4SCyxNIkt/cMio9jfx8pZm29UvKfmeQjw6/3l1lN7OVR8XNk2FPi1/kFA8Do3MSGwEAwt0szwc30onS0xxUytfd6Goq/x7KnRyUBMrvevi6geT5WHhRECTWDdT1iVoIBzLpVsMsaP1w6Tkg8FAPcaQyI1mXVVZ6Ym/S+RZ2NZPJPcDJ2wmjGj/H75+J7Mt/VkHuZ4ZzVVW+DeMqwgu93lFPawcJ6+b7Iue0X8td/sgnRKXqM9cRSBBlLh3wwpb9SmX6ZS1c9ctlUUgBDAY1FkyP+rrMsiGhKgeJcSDRJNdUxE8QDS7KBz2QB1slePsnFBB6CwGYAyuGLEg6yUpPobKF7Xpre35hfLZWDeFA4xDlh9axq4ynaIu1BJfdCjytqsAyk4l/rp9QW49HCX5IbhygnCSPTbVcOIidXvqzmb5h/K0L6uyP9HH0H2q/lXYJpGYwLNjHLXpSi91wuRbjqs757E3dY1oY/A5LNe8opyVepmwpfg2JOuwN8nDrgtTnb5LYlcZ6YcRrS/9PUVl9GsIgOYeLsLiIffyaTcQmZqwKaNxZM0gHFpZK/DEryZabQWolyUDulqvr+sGN0/G82oirOYUhDCNu55cAb4a8D0FWK5YV6vhuAxPg3Dr4eLDlOAHxDYjVm5gIhF6PjxppfnSv8+2t8JH8fB5q0eAeoFcK5lQ1PsfqZ34iQfWQ4KKfXEb24n0vy4nkzChEAlTClGi4leWb7Zd+IgWMR8K8maUToai3aDsposjYdLCPFUqSw5z54cJ1ncoBHgzPQWVX0nj4XNuehXRgxe0UYmzU6SlZl5TMF5/A7DksaTZR8wpZfLxTTzIG+xNsxrii/F7zZIW9aIatv4rHtaXEGKbQrm8n7qIry16WYZ/kR3Td6WzsRJj4X6NoH05gn3hwioEbASFwiljiowzz23 7DWMWNBq NxlRJsDOqbMYVXGssiIXHQJq2c++TfiAyNO8mvdSQJy40cW4h3UZ+oqderTgJwTU6y+GkvwKwCGa7jwYGzmAzWi+8MCoCvVkvSLVYLU7hXJD4IbI+Vn4xM0PpS7ZAoBeIB5rwomo1WXXuXtHmTaltM17+OLgTetnyYTdRfEsrcwJSqlfrMCzYO1Mxnf4M9YbNNQtwV2v0nk08aw84Krd6MnLPCqP4Fpa31tQQLQ5DiCizbutSaOXdHpwb15n2vmsaYwUyEHXoZarPPO87PE2h1GrdIHupqjImeEXtCttopEYUlKwi7G6CCrpzLb7caef14Lxvt/7eqFYT8R0Z0gH84TeaYieXsoBpuNlkHdhbTn3PjYNpGcwOTgBbQk+iqGNxycrmRUFR3b8XV3O05uerl+vGF5+KlkCDyY+KnptlEb7IxK3049Oz1Y7iuf9FeQ+vD9Ll0ckSmFyniBsJ6dyCDd0oyCqRgO0MoC4M9fhzDBuCihZYQV13vnyUM90YnYTzSdsQ0zo4KA0GCTL5PYTZmzsf89vN1aOPXzzFZW9Picx1Gi0tvtH4hqjcFNmWIj0FyKY+Hy4v/cvFX1rY3UimZU79VFhkCqHbtI+xOh4R1XkfembWFdqgwrm0KybJL2pKA+Pk2r9Ac5/2hJeKHE5Kv3JrcnYAqlRyzjKmZeH8kWNAjYnptS03yRbZ88udWCIvbkxp8KJsbC3nRNrzMaer6AdLxLpZ1zeyc3c7sRb18yCgzlk+078/F9H32lFeFuP0ruwhihN7ra9PGi8= 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: Add support for creating "private" memslots via KVM_CREATE_GUEST_MEMFD and KVM_SET_USER_MEMORY_REGION2. Make vm_userspace_mem_region_add() a wrapper to its effective replacement, vm_mem_add(), so that private memslots are fully opt-in, i.e. don't require update all tests that add memory regions. Pivot on the KVM_MEM_PRIVATE flag instead of the validity of the "gmem" file descriptor so that simple tests can let vm_mem_add() do the heavy lifting of creating the guest memfd, but also allow the caller to pass in an explicit fd+offset so that fancier tests can do things like back multiple memslots with a single file. If the caller passes in a fd, dup() the fd so that (a) __vm_mem_region_delete() can close the fd associated with the memory region without needing yet another flag, and (b) so that the caller can safely close its copy of the fd without having to first destroy memslots. Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 23 +++++ .../testing/selftests/kvm/include/test_util.h | 5 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 85 ++++++++++++------- 3 files changed, 82 insertions(+), 31 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 9f144841c2ee..9f861182c02a 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -431,6 +431,26 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) void vm_create_irqchip(struct kvm_vm *vm); +static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, + uint64_t flags) +{ + struct kvm_create_guest_memfd guest_memfd = { + .size = size, + .flags = flags, + }; + + return __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &guest_memfd); +} + +static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, + uint64_t flags) +{ + int fd = __vm_create_guest_memfd(vm, size, flags); + + TEST_ASSERT(fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_GUEST_MEMFD, fd)); + return fd; +} + void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, uint64_t gpa, uint64_t size, void *hva); int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, @@ -439,6 +459,9 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags); +void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, + uint64_t guest_paddr, uint32_t slot, uint64_t npages, + uint32_t flags, int guest_memfd_fd, uint64_t guest_memfd_offset); void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h index 7e614adc6cf4..7257f2243ab9 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -142,6 +142,11 @@ static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t) return vm_mem_backing_src_alias(t)->flag & MAP_SHARED; } +static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t) +{ + return t != VM_MEM_SRC_ANONYMOUS && t != VM_MEM_SRC_SHMEM; +} + /* Aligns x up to the next multiple of size. Size must be a power of 2. */ static inline uint64_t align_up(uint64_t x, uint64_t size) { diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 3676b37bea38..45050f54701a 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -669,6 +669,8 @@ static void __vm_mem_region_delete(struct kvm_vm *vm, TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret)); close(region->fd); } + if (region->region.guest_memfd >= 0) + close(region->region.guest_memfd); free(region); } @@ -870,36 +872,15 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, errno, strerror(errno)); } -/* - * VM Userspace Memory Region Add - * - * Input Args: - * vm - Virtual Machine - * src_type - Storage source for this region. - * NULL to use anonymous memory. - * guest_paddr - Starting guest physical address - * slot - KVM region slot - * npages - Number of physical pages - * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES) - * - * Output Args: None - * - * Return: None - * - * Allocates a memory area of the number of pages specified by npages - * and maps it to the VM specified by vm, at a starting physical address - * given by guest_paddr. The region is created with a KVM region slot - * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM. The - * region is created with the flags given by flags. - */ -void vm_userspace_mem_region_add(struct kvm_vm *vm, - enum vm_mem_backing_src_type src_type, - uint64_t guest_paddr, uint32_t slot, uint64_t npages, - uint32_t flags) +/* FIXME: This thing needs to be ripped apart and rewritten. */ +void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, + uint64_t guest_paddr, uint32_t slot, uint64_t npages, + uint32_t flags, int guest_memfd, uint64_t guest_memfd_offset) { int ret; struct userspace_mem_region *region; size_t backing_src_pagesz = get_backing_src_pagesz(src_type); + size_t mem_size = npages * vm->page_size; size_t alignment; TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages, @@ -952,7 +933,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, /* Allocate and initialize new mem region structure. */ region = calloc(1, sizeof(*region)); TEST_ASSERT(region != NULL, "Insufficient Memory"); - region->mmap_size = npages * vm->page_size; + region->mmap_size = mem_size; #ifdef __s390x__ /* On s390x, the host address must be aligned to 1M (due to PGSTEs) */ @@ -999,14 +980,47 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, /* As needed perform madvise */ if ((src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) && thp_configured()) { - ret = madvise(region->host_mem, npages * vm->page_size, + ret = madvise(region->host_mem, mem_size, src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE); TEST_ASSERT(ret == 0, "madvise failed, addr: %p length: 0x%lx src_type: %s", - region->host_mem, npages * vm->page_size, + region->host_mem, mem_size, vm_mem_backing_src_alias(src_type)->name); } region->backing_src_type = src_type; + + if (flags & KVM_MEM_PRIVATE) { + if (guest_memfd < 0) { + uint32_t guest_memfd_flags = 0; + + /* + * Allow hugepages for the guest memfd backing if the + * "normal" backing is allowed/required to be huge. + */ + if (src_type != VM_MEM_SRC_ANONYMOUS && + src_type != VM_MEM_SRC_SHMEM) + guest_memfd_flags |= KVM_GUEST_MEMFD_ALLOW_HUGEPAGE; + + TEST_ASSERT(!guest_memfd_offset, + "Offset must be zero when creating new guest_memfd"); + guest_memfd = vm_create_guest_memfd(vm, mem_size, guest_memfd_flags); + } else { + /* + * Install a unique fd for each memslot so that the fd + * can be closed when the region is deleted without + * needing to track if the fd is owned by the framework + * or by the caller. + */ + guest_memfd = dup(guest_memfd); + TEST_ASSERT(guest_memfd >= 0, __KVM_SYSCALL_ERROR("dup()", guest_memfd)); + } + + region->region.guest_memfd = guest_memfd; + region->region.guest_memfd_offset = guest_memfd_offset; + } else { + region->region.guest_memfd = -1; + } + region->unused_phy_pages = sparsebit_alloc(); sparsebit_set_num(region->unused_phy_pages, guest_paddr >> vm->page_shift, npages); @@ -1019,9 +1033,10 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i\n" " slot: %u flags: 0x%x\n" - " guest_phys_addr: 0x%lx size: 0x%lx", + " guest_phys_addr: 0x%lx size: 0x%lx guest_memfd: %d\n", ret, errno, slot, flags, - guest_paddr, (uint64_t) region->region.memory_size); + guest_paddr, (uint64_t) region->region.memory_size, + region->region.guest_memfd); /* Add to quick lookup data structures */ vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region); @@ -1042,6 +1057,14 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, } } +void vm_userspace_mem_region_add(struct kvm_vm *vm, + enum vm_mem_backing_src_type src_type, + uint64_t guest_paddr, uint32_t slot, + uint64_t npages, uint32_t flags) +{ + vm_mem_add(vm, src_type, guest_paddr, slot, npages, flags, -1, 0); +} + /* * Memslot to region * From patchwork Fri Oct 27 18:22:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13439033 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 9C601C25B47 for ; Fri, 27 Oct 2023 18:29:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 33CAF8002B; Fri, 27 Oct 2023 14:29:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2ECBE80018; Fri, 27 Oct 2023 14:29:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B44F8002B; Fri, 27 Oct 2023 14:29:35 -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 0CE8B80018 for ; Fri, 27 Oct 2023 14:29:35 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DA35A1A092B for ; Fri, 27 Oct 2023 18:29:34 +0000 (UTC) X-FDA: 81392079468.20.5C0ABF0 Received: from mail-il1-f201.google.com (mail-il1-f201.google.com [209.85.166.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 1C0B340025 for ; Fri, 27 Oct 2023 18:29:32 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="YKK//JI5"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3FgA8ZQYKCDclXTgcVZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--seanjc.bounces.google.com designates 209.85.166.201 as permitted sender) smtp.mailfrom=3FgA8ZQYKCDclXTgcVZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698431373; 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=yVf4MoQ+pgFottQ0gtqF7KbCgtXqLOOMhwiDUmO9tI4=; b=oi8cZsYPcqE7Dvuop6Slx4tXmuWMh6CzC1dYoja7WiZ9GdUQEYrj1YKIsayET6+zRNizMr twlDG/kNLNZJpPcD1iCSPpmVgM1x2nVDYYRwPPtTkp32Yrz3FG9LbEpTeakNtmhDjU5v5Y f6FV6sMgWxpG2Mq7+s3EQoUd3yRhKsc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="YKK//JI5"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3FgA8ZQYKCDclXTgcVZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--seanjc.bounces.google.com designates 209.85.166.201 as permitted sender) smtp.mailfrom=3FgA8ZQYKCDclXTgcVZhhZeX.Vhfebgnq-ffdoTVd.hkZ@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431373; a=rsa-sha256; cv=none; b=n2LHAnIn3Ns5haZmo2sLxJdVzkYYgghfKXoTFvRDCW8p9KGOiGEDhH3mnsBlXicQPaHRWK 1SSkNA9/4JzXPUXVGg3A8FL37bV93vowO7ySPNhbXgjBWj0Yc7bTLKQQWEa1qVQBHjIM6S 17e4KRTr6tthcr3giOmMMzrVZboU/dY= Received: by mail-il1-f201.google.com with SMTP id e9e14a558f8ab-3574c545ad2so21299755ab.3 for ; Fri, 27 Oct 2023 11:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431372; x=1699036172; 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=yVf4MoQ+pgFottQ0gtqF7KbCgtXqLOOMhwiDUmO9tI4=; b=YKK//JI5LP2toIf6VFKMzOYEsybIF7rZQ/tikhT4/gwaDGLP8lEJI7JMTPL2SZ3Jf9 qhc6yS+gjcESfKGOz9FMcw76mbYlJPUso3ToyfeWvEQlhyg3rpsLe+loHvoxHJ7dUHgu /Qkl8r/KvYgeCsM3xDneKuSeUvSvs9iK75BQ5auIk57NJXxzrkAy4m430t4gWdRO8WET y4/WjYVLoQnvvC1LcDX188Q0/LKb/RpaMTj0pq2YaQ5k4d2AG/1xI6GsdbhtrdSWminx MmYby5rbOYxowxLdi909BWySJUnb/wBmCw5tn8ZcTXJTA/CT23y/vPcncQWFGervesbe aA9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431372; x=1699036172; 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=yVf4MoQ+pgFottQ0gtqF7KbCgtXqLOOMhwiDUmO9tI4=; b=LnOlPW+mczYAdtP/ByYTdWtnCCS2CCmgJspDEFhorPuDIV6B+cH9OutOimojyofpEt /pHVNdSxkt1uVBOVMtV5q6W1AE6+okoE9xyGTIVEWiY3vvTqH71MhPz7005zsN5JTNMO +Ne+NICRPSGzbaG0/3Bj3Nnm0zNDVFoeljG+FvOGeyLYugGYKn+rj8IrPEKR7FtuQCIr XKHgQZXLgJGvS7D++J6RBhyH3wlPC17MzJAvW+mrBs3fJVwU6We0kArNz8walwuUHMlE s/gSGniQtzFcR5buDvT0DVkdovzj2g+fFxBYOZlCJo2E4Zmt3zqTb+p/hocuol1L/Sd9 LAsw== X-Gm-Message-State: AOJu0YwtqL9HTxXoTFgzIqlBTWzcgDAz4EZkXM9w2hu95kxrqCkHXtEJ 6UGq5appcp27AneZItviVcqT/Dbi16Y= X-Google-Smtp-Source: AGHT+IG9XwjMpXerybQ74hZWKPyWlQIkLL60UP7vz/JRGdDaUmVGZC9suFSomOjVdAtgwjkxyK2jTqS148U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:2fce:0:b0:d9a:f3dc:7d19 with SMTP id v197-20020a252fce000000b00d9af3dc7d19mr60503ybv.11.1698430998960; Fri, 27 Oct 2023 11:23:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:09 -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-28-seanjc@google.com> Subject: [PATCH v13 27/35] KVM: selftests: Add helpers to convert guest memory b/w private and shared 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-Rspamd-Queue-Id: 1C0B340025 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6bxnriazjmiym9sr9gpe3c68c6bfxgyk X-HE-Tag: 1698431372-499442 X-HE-Meta: U2FsdGVkX1+gZSbfnkF8H0WTuBIzrskvEesomiPrgZIvbJdnu+Ldf8QgfHTupbUtJZ/g0WZeUd5DFhBqDUXa0En1ekD0ff2ti/rlx66T0zRTLA+uk0ZMDE6SlzcwKqsc25WelEsA5rF04Wd+nZP2n2PhofC6nivKRyycRSMPO+Enw60g3GQMRweXn+dGgdGsqW/1eyZxT5ftL87IAeGHaUvPma/uw0ISSQ0m6+7vfOBWM3UD1Ch6KPs8CsTlMaMiChxj1Dx7mYbDdrXEYRvs7jOJvYe0+IubfHy2Wi1LAL2QNexop1S6V8UiH6MeE2EteMBsD04njpbWSrSr4WpBW71SWRd/4OgF3UPdGj4jpOM4m0h3E7vM/W1CH/v4p7dVresoK6cFPBrUbEq538ybsSuwSRqJ+2ARH01+go0nolemoudupIyRtxz0R7FcoHJ5ltPLhDXBfBhcYFhGfTxqDsjt5Dc4cqPpP1stMBnXkaaGN2omK6yYUg2GdBh3HPOx9Cr97Y2RKYlpQHchzqH3p97+9s3MHXcpas4gPuqZkpRv2CPWOJK8Su3Fubv+3T0h+BOpmUgz9Mn+UbdDOWMH3QHU+r7xZf5jWV75OFEfZgH6lWmTV5M9/zCTbdrlt7sqLfY1jTdU55QPFmcVqndLJWQT+ANnsItV2U7wrcMO31u4zCefUxMpfN3FqLEqkNsv49HNtsc2Oop6JtF1EU/sNs4cakv3vOlvmEp+ryogB+kU55ZSSZL3hxHEKGo1tKuH0B8IaqrIJWKJuIBz6TP/ptRcBYc6HTg+dZ+zK0iZwbSSY4TlSvnFBNeHUYUww7z2A5pHxvfjT6boEsNGwzzYD8wuYaQifxar870DIBxuRQ/4Thkw+KzRBtrF591S8S63gWHSXWudoQLURT8gtkFXVd3UZc4WlHYvNx1vJx8yLLhEv9U+0q9LdqSlGiFsNj3kA8EcHJqmykFxaeaZSPB 8phULkVv ywDkf5UkKE4VO6EFQT/+olCECjEB5a90L1wnkNmw5Lk4b1D/G7w5EfOtfP62cBSWh4NjPsxOtNxjsVsioo6Vfq56OXvPp03F9qOePHlzVZ97szFHHE3B3lt0VjH/YGYkaN0wC1nC6653C9P9SQ3UeiDUQ/2xv+C2DKEPfMU2ki31Phfqz4MJ4y0RpMaakEUalwp1EEy9fBCp0l1+wGmcBp46nzAs48xB0kLHnoMr+xBsK9lsWxy2NNEdhZ6f/8RoJj9kRaqk8SIY2ysyUj3wQOvmGGOAB2ooCZDsucle76+cDYKG7Um7mSp+wzwvz6UMEsUs+y6GZ3LtfdOFy5eABirWZx0gRIJJiVAAj3eSVM/ixxUHKmmx5pPKnO9FF0YtOqPR2bTHUfeZnC9LNDCChTk7mgqdDeVG2Me8EROD3XZUqbzo9yZLeC/94NPsF6Vy3mgrEXUHqtPBxcCZYt4uFv7qdmp94VeJDytujKO6lKZCpOQhYWVzAXnjG4H0GBL5UmrP/cDC+d/GA69KIcMySwOyfLygvJaL1uFr72JLLOOPXbz0iruh0o9ulM1vR2ywvpyVuOJ9PY8iNChtmnXjijYe9LblcjbwoVk5Dpu4Bsk1GCVb388l6cg3Z1h8eQhAeJAkSwbcjobhtR2ryQVVJ08U5qclWxbDhNL+/tpIE7HbQq67VM2L5V9MANK+QBlieuuXMJxfhD9W52TxIBMzvrx+KIBPX6cVouQLgO3X18E3fcmLpqa2O0E8DspfbQAlP2inF8c/v0TTddh3JFvwq3/ZJsxBYfPgytWeygsXCiPpfRA/GGi2Owr9/nQ== 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: From: Vishal Annapurve Add helpers to convert memory between private and shared via KVM's memory attributes, as well as helpers to free/allocate guest_memfd memory via fallocate(). Userspace, i.e. tests, is NOT required to do fallocate() when converting memory, as the attributes are the single source of true. Provide allocate() helpers so that tests can mimic a userspace that frees private memory on conversion, e.g. to prioritize memory usage over performance. Signed-off-by: Vishal Annapurve Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Reviewed-by: Fuad Tabba Tested-by: Fuad Tabba --- .../selftests/kvm/include/kvm_util_base.h | 48 +++++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 28 +++++++++++ 2 files changed, 76 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 9f861182c02a..1441fca6c273 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -333,6 +333,54 @@ static inline void vm_enable_cap(struct kvm_vm *vm, uint32_t cap, uint64_t arg0) vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap); } +static inline void vm_set_memory_attributes(struct kvm_vm *vm, uint64_t gpa, + uint64_t size, uint64_t attributes) +{ + struct kvm_memory_attributes attr = { + .attributes = attributes, + .address = gpa, + .size = size, + .flags = 0, + }; + + /* + * KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These flows + * need significant enhancements to support multiple attributes. + */ + TEST_ASSERT(!attributes || attributes == KVM_MEMORY_ATTRIBUTE_PRIVATE, + "Update me to support multiple attributes!"); + + vm_ioctl(vm, KVM_SET_MEMORY_ATTRIBUTES, &attr); +} + + +static inline void vm_mem_set_private(struct kvm_vm *vm, uint64_t gpa, + uint64_t size) +{ + vm_set_memory_attributes(vm, gpa, size, KVM_MEMORY_ATTRIBUTE_PRIVATE); +} + +static inline void vm_mem_set_shared(struct kvm_vm *vm, uint64_t gpa, + uint64_t size) +{ + vm_set_memory_attributes(vm, gpa, size, 0); +} + +void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t gpa, uint64_t size, + bool punch_hole); + +static inline void vm_guest_mem_punch_hole(struct kvm_vm *vm, uint64_t gpa, + uint64_t size) +{ + vm_guest_mem_fallocate(vm, gpa, size, true); +} + +static inline void vm_guest_mem_allocate(struct kvm_vm *vm, uint64_t gpa, + uint64_t size) +{ + vm_guest_mem_fallocate(vm, gpa, size, false); +} + void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size); const char *vm_guest_mode_string(uint32_t i); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 45050f54701a..a140aee8d0f5 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1176,6 +1176,34 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot) __vm_mem_region_delete(vm, memslot2region(vm, slot), true); } +void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t size, + bool punch_hole) +{ + const int mode = FALLOC_FL_KEEP_SIZE | (punch_hole ? FALLOC_FL_PUNCH_HOLE : 0); + struct userspace_mem_region *region; + uint64_t end = base + size; + uint64_t gpa, len; + off_t fd_offset; + int ret; + + for (gpa = base; gpa < end; gpa += len) { + uint64_t offset; + + region = userspace_mem_region_find(vm, gpa, gpa); + TEST_ASSERT(region && region->region.flags & KVM_MEM_PRIVATE, + "Private memory region not found for GPA 0x%lx", gpa); + + offset = (gpa - region->region.guest_phys_addr); + fd_offset = region->region.guest_memfd_offset + offset; + len = min_t(uint64_t, end - gpa, region->region.memory_size - offset); + + ret = fallocate(region->region.guest_memfd, mode, fd_offset, len); + TEST_ASSERT(!ret, "fallocate() failed to %s at %lx (len = %lu), fd = %d, mode = %x, offset = %lx\n", + punch_hole ? "punch hole" : "allocate", gpa, len, + region->region.guest_memfd, mode, fd_offset); + } +} + /* Returns the size of a vCPU's kvm_run structure. */ static int vcpu_mmap_sz(void) { From patchwork Fri Oct 27 18:22:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438954 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 A8ABEC25B47 for ; Fri, 27 Oct 2023 18:23:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2EBA080023; Fri, 27 Oct 2023 14:23:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 250DC80018; Fri, 27 Oct 2023 14:23:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0795980023; Fri, 27 Oct 2023 14:23:23 -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 E0B0280018 for ; Fri, 27 Oct 2023 14:23:23 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BA3DCC0429 for ; Fri, 27 Oct 2023 18:23:23 +0000 (UTC) X-FDA: 81392063886.25.43D0308 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf12.hostedemail.com (Postfix) with ESMTP id E2BB64001D for ; Fri, 27 Oct 2023 18:23:21 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LgEqtRhf; spf=pass (imf12.hostedemail.com: domain of 3GQA8ZQYKCDooaWjfYckkcha.Ykihejqt-iigrWYg.knc@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3GQA8ZQYKCDooaWjfYckkcha.Ykihejqt-iigrWYg.knc@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=1698431001; 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=Xz8kt0PqFUxEdWsVGTpKhvh/0bAOqOlDGpUeOhSMn4Y=; b=2dli0WSbw3Ki6j+qe2tT4SujtWczRu+NtymM3uFADrnKok1zAiuLNLAh7ouzyoES5D6zGD 8kALH+XHCp8bWjwoRe36d+GNZK5ACsB+zIBeGtIhjXE2tiHCxmqBUFQjzLMK68/W80hfQk z5LmTzrp6Zsk7ECkJm48mztFhivbdo8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431001; a=rsa-sha256; cv=none; b=lJ8WYkV/29CkZkJ7Fm21rBof9oesY6ZfA3FvVoYNjJg4iUhkLBMqisslqg0RJlaQEyzrPG hAqU+D7vh8eVFKUecRilRGua4GKqPmsMr2tTxyfxlkOFWsr/Q8AkDFK51rsnAWiUvlNe44 jm/idadopSrXGhO+dJOYWkS8t+R0euw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LgEqtRhf; spf=pass (imf12.hostedemail.com: domain of 3GQA8ZQYKCDooaWjfYckkcha.Ykihejqt-iigrWYg.knc@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3GQA8ZQYKCDooaWjfYckkcha.Ykihejqt-iigrWYg.knc@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5afe220cadeso6523367b3.3 for ; Fri, 27 Oct 2023 11:23:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431001; x=1699035801; 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=Xz8kt0PqFUxEdWsVGTpKhvh/0bAOqOlDGpUeOhSMn4Y=; b=LgEqtRhfFh2v8AezhEyjkB34j8mVVLpr2M7g1DxLSSfZ7fskSvzy7Ekb1s3yO4/SEz NoQi+TO0fJddYitlVLx5Ol3Xb7nSa93TbF+iUG3GJc+fFDCVW/RANI1uQzKy4ax7T4FQ B5JFQV+kG+40s9/VR3Q/aU8i0MQuZYb5mx9CSOE5qRmMMlD6ppvBAmqG+v9nK25Fn76C 63yZnTwfLsRdJd0HrHVEWqSADuxOftrcpS9s0HqAzts21RRa8wzi7g773uB/YfjnuZea ti4dmFnwBYUEvzmVG0FvedoWR66UEhU1H4mnZnU1oAFEPG7tszDDMbFxANTsElTxYOPC nPhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431001; x=1699035801; 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=Xz8kt0PqFUxEdWsVGTpKhvh/0bAOqOlDGpUeOhSMn4Y=; b=lY6vpq1M7LZqDW4ZBZKNaGAziATrUcExIXP57q5PH6LasXq5Oy7aaKlQ4iuSVV69Nb RH5nv8eiw8sH/9cIw0h6BaiW8ovy02mEMZYmgAVMRSqG0vXq+gTGEQ552WrBGna1Lrro tp5Dr3QMPP/b1DcvrKQ6chRZirjNn9AOTT1XQB1TSEaOqPoLP9ShY8+al9yv+my1cOQM BTVEX8IwjcnJ+W/Ky1R6YjnmTW5aspkEddkDi1kV2t62i86LPXfa0CwJnXqtvBMFv/mM 7qVuyv+01g2jiQEeuXPQzq/uxjonzLxZekk0YLgBdZvhfaoVJgGEXLRGX0j9T0qxCiqZ IYEw== X-Gm-Message-State: AOJu0YzTJ5pLrul6CL53DX1YHDTJAJgymeEjM4fh7Ov7l4WyLkKftL5x c+ahZlAKj/1jAHz/oeKePp45NMy2cMw= X-Google-Smtp-Source: AGHT+IElp9q5EmMXm2ScJmYLrQOoQHqoyljn7k2kapIAuiZd/nyrTEs2J8MXUI1aCi13+9HebXd80A1xuUo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:e653:0:b0:5a7:be10:461d with SMTP id p80-20020a0de653000000b005a7be10461dmr68754ywe.2.1698431001067; Fri, 27 Oct 2023 11:23:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:10 -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-29-seanjc@google.com> Subject: [PATCH v13 28/35] KVM: selftests: Add helpers to do KVM_HC_MAP_GPA_RANGE hypercalls (x86) 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-Rspamd-Queue-Id: E2BB64001D X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: nmcgbii85x5dfwzqxfdxfqq7t5bpum5h X-HE-Tag: 1698431001-600096 X-HE-Meta: U2FsdGVkX18OcrEZ+K33yZgMXnUaCKs3yY9z0nIx25O8qJyeJHrbrUGlYfBghjQXmWEX7ibFmI/jLmxt/TvaokI2kabH68l28R1N6Ex4uUSUU8DVR01hTQYL2s4YGDlYkQsaWiQ9Ry+6QNxXtKDUCe6f4I34c7vAx8DSWX73VdN2JMuDD4YSWlWFI1umcxQPUk4ydIMmizcyyh6WbH82gn223NTk0PjL9GaUBkBwWAEK+4iqd0wsWTa2x1WnRNYp8Behwu9hy4hBbx04QGq/O+jC0ydgJqybsQGIVPpMUm6NloN+7pRzzq90Se7fnn6aBlJylgfTJvvvQvq5YPakq7lUUhZ4gexVb0EjPpd7VtzibAd+Dxjwp4OM99OfO3KPg2gyQyAz6qLFk7Fogb75KxB/n2YraJRmqMOUtDW8fULlVlLxN9rCeHSD++5N2f4D0jIV79tCUC1konGmAnabf5VlUiVpTAqW5Hfcty/wop3jewjOefoQVTjxpHrVqkiHfXLIIYXF+J1fVpgJaliCUyrrgAcBTFAUvyhY261GeBC8MGazcl2A5bzdXwc1EBqZJRifvN78f2IaGqDrIeppl++do8g92PMMdgPglH4wfNA+MtSVJtX6Al7zSBdUHTDtBhM4RLkjkBGrSUo+2VN6zGjGgZrG9kg3AolNOGNXAg68GrR+A8gMMaLCzTz4BR5bS++IkfbrvRfa0CaZRxyNRUado5Z2UbALh+yvpNYn+cUUcfpxghs5Jzy0fL4g5EjL891dlLnPMfAMRil1Ig4xuUbG6z+8EhyoDK6on6pCWeyeouSl4evJ5OQeZuHFCbpmdJEg9MbkVUk9ZhmKo53y9E4FtVs7XJEuV86rh5demg5qXK6eYr17p+ljSXzrXHNQ/lHEsQk5H9X0VtRPsfZjzsjXStQ1hqaFhAF3Y3Nz8Zfq++GF7vWOaFK/j3UoAKdMKC/2TznZSE3uX3krDVo iDBPDl61 p2WZF55g+7z+KKKDhed4fBN6Q4j+Ne7T5yTrequbWLFMTNP+PgVurUAITiwLb+ys0s6xGTKO1pfhBUL6JJJ+rScz3vwcPY1RyNVEEhF/66ANLfEwCd20UAQkaZtuHu6c8G+5bqVAUQBjLbQjQoqSQhQUapaaHI9GbbVbLJGHGsqOYnJldcw1Y7+MOr/6Csts7i++mlmUoy2RhmXL20AzvDUk/+372aJkVFUacO3vwfXNkNCKI9106AigRmOChXFeaSphY4E+9IhrMVLV8dCjSEdSHCioSeUcUTdxjdEVjnQzMMSrtvri691vPOYnuiqZb6m/br+GvNNDV12V7tRaGTfqH6BbrU+gtMqv6iqwg6uLj9vS3jE8CZ1pZJyffoGzqpxqQ9MNVV6cVezuz5HHtuuFRdG5EbugK5y3BLk1OS5bcl1G4v0Iu4mwRehtLfNzRdi9Ya9CbBKkAdZDSejpyeii/Tal8yMPdeiyHhsQv2QHyG/nQt1ZCH/mmNCQJnFU8gJwT/5XWdM5can7mPUavoRmGIwwzUfWeCL5tUoH4b0Qed6FROEFbhRbpU8PvlgLFFqa3Is6sDQfzfeg4y+4jLOZs87YJKR+qxU+YGawyoBsO7I0luWIHbiojF2j41jQhV6nBMGxD6u9hIv1nBHyBgt5YtV8by1ghMSAamf8OehGvXKQY8HsVRmdBj0FZFOuh6OIQhqXPgXwPS2scMefcQOrxUmX9CbCTOj6fxK/5Y5GuOrjC7Y26ufcDRTJcOWISzlwwqcH2D7jc6xkopUnAAqkgDwUpx2juAmTYY/DVY5WegwAR/63Nue+mIAdbdjEwOQwlfCKf2y/PLndSfLh+BYv/Xiw+ZHByl88B 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: From: Vishal Annapurve Add helpers for x86 guests to invoke the KVM_HC_MAP_GPA_RANGE hypercall, which KVM will forward to userspace and thus can be used by tests to coordinate private<=>shared conversions between host userspace code and guest code. Signed-off-by: Vishal Annapurve [sean: drop shared/private helpers (let tests specify flags)] Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 25bc61dac5fb..a84863503fcb 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -15,6 +15,7 @@ #include #include +#include #include #include "../kvm_util.h" @@ -1194,6 +1195,20 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t __xen_hypercall(uint64_t nr, uint64_t a0, void *a1); void xen_hypercall(uint64_t nr, uint64_t a0, void *a1); +static inline uint64_t __kvm_hypercall_map_gpa_range(uint64_t gpa, + uint64_t size, uint64_t flags) +{ + return kvm_hypercall(KVM_HC_MAP_GPA_RANGE, gpa, size >> PAGE_SHIFT, flags, 0); +} + +static inline void kvm_hypercall_map_gpa_range(uint64_t gpa, uint64_t size, + uint64_t flags) +{ + uint64_t ret = __kvm_hypercall_map_gpa_range(gpa, size, flags); + + GUEST_ASSERT(!ret); +} + void __vm_xsave_require_permission(uint64_t xfeature, const char *name); #define vm_xsave_require_permission(xfeature) \ From patchwork Fri Oct 27 18:22:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438955 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 18A84C25B6F for ; Fri, 27 Oct 2023 18:23:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D12180024; Fri, 27 Oct 2023 14:23:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 755EE80018; Fri, 27 Oct 2023 14:23:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55C9980024; Fri, 27 Oct 2023 14:23:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 3C76D80018 for ; Fri, 27 Oct 2023 14:23:26 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1B7CF1607BD for ; Fri, 27 Oct 2023 18:23:26 +0000 (UTC) X-FDA: 81392064012.04.9CDDFB0 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf27.hostedemail.com (Postfix) with ESMTP id 3CC8540002 for ; Fri, 27 Oct 2023 18:23:23 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IUrI9B25; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3GwA8ZQYKCDwqcYlhaemmejc.amkjglsv-kkitYai.mpe@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3GwA8ZQYKCDwqcYlhaemmejc.amkjglsv-kkitYai.mpe@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698431004; 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=jQSVyWKl4uOIPgy0IGoz6RV6+SA994mJdxIjj30CSy8=; b=pxi+s+vPWxlvLK9c3XdKneSm7iUSMZyNTjiYRDBEd6YtIA5WGfEC6Ry8h7K7mTRo4foxJ+ NzoS8um87zr4kCBBu44Za5VBsPrG5jBzl5jccWU3JE5UybjKwINP2PoamHpvtG5crJz4mS eXUaLEkDy+a3ajdocOhWd2iZ+k/GPQk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IUrI9B25; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3GwA8ZQYKCDwqcYlhaemmejc.amkjglsv-kkitYai.mpe@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3GwA8ZQYKCDwqcYlhaemmejc.amkjglsv-kkitYai.mpe@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431004; a=rsa-sha256; cv=none; b=bRwchP8cAicnVyDqss9psC3+3UAkGz3Q2vU9r6Vvh0rWisXhCEGjxUf0t+kY+OyQ8M1gMI Orc6pFkRgZ4OdIyx7/e3InuN22EUI3WAPanHBJf6M4o7+ZGezIYrsrMDeE+HfIoRhzWF3Q LT6h5QnQJWjQAAFpFaw9oA9wH5OgJrg= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1cc1ddb34ccso9092275ad.1 for ; Fri, 27 Oct 2023 11:23:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431003; x=1699035803; 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=jQSVyWKl4uOIPgy0IGoz6RV6+SA994mJdxIjj30CSy8=; b=IUrI9B25DzcuQct9UsZr89LW9++vHW4QIvP9ng4TAcs6nXM91izAUql2oJCLGmHgFs S0xK/KrkgKTBCCLCYC7fWctszbJ7qcvlvuMYW600Zq/V2+9KdI020xv8fortKeMATtRp Wn+kWG2Us0kCWM8GamWioV89j2LMFDyvatBvLpM1OFVLoHCRAwSGc3gwwuayXG1qLW+d nIXUnzpY7DVHqxFwcU36DSl2cDOzxFV+9SlpDsvgV7QxA5f62Z7+MfLxUAKtg7Cj7omY nEy2VDN5DwUkX+NI95h9QRmkOU3d0txeOGsagdYdAfvUyZgFI9DextcP09JvXZK25P9u Pqsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431003; x=1699035803; 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=jQSVyWKl4uOIPgy0IGoz6RV6+SA994mJdxIjj30CSy8=; b=QouNL+VJSPs/hP8HRZuOKHTa9TGgi6ZC2XN29n+DvcjjdbURW3LfQPdryr0CsiPGM6 6WIBIS2bMAWpio1M/buKCRecTC/as/3H+EWI3aPeulMfSujMmx+k3v5XG7qBYqLj/FzK pBsO04yDBkbcloQC6oWruAEbMB9rAL3o+5uBbYC9Y9BNDy0j3aVZvE0RZDtyYXllfzhf LzDJdEdgS0bIAozKFPbog7U5aFTFjoaTlKW0loksN/8jmHYjVh76J5tscg3Ztn0495N1 Fpc0MAuyXTjvAw8tHy+cGJQ3gF8WqNRj9HQK9MOirXeO+9fl87w8asAGDWBnp2/dvPE8 41Cw== X-Gm-Message-State: AOJu0YyQtADj5VF11zoA+1kweexYFQMu35BOHsf65v6BBcUKjf0Sz7Pa iPqg67dfYSJ92t6ZpP6CTffzcR+xapg= X-Google-Smtp-Source: AGHT+IEPiReV3cx4w9TLV/sbeNO6eERW1W7R7I/92aij3c451hoquFxemYBSOe8iuKYscw30iX2x6gNTTBQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:18d:b0:1cc:2183:b380 with SMTP id z13-20020a170903018d00b001cc2183b380mr55116plg.8.1698431003083; Fri, 27 Oct 2023 11:23:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:11 -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-30-seanjc@google.com> Subject: [PATCH v13 29/35] KVM: selftests: Introduce VM "shape" to allow tests to specify the VM type 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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 3CC8540002 X-Stat-Signature: r7yei4auqxch8imuthz4dz7bbw1bns5c X-Rspam-User: X-HE-Tag: 1698431003-376831 X-HE-Meta: U2FsdGVkX1+sxp3b2/NfbcA0ULjRMTcbFKjlwxRmg12SsOYZf0YIYXHA3g0V4Tm9suu0FUqCwq8lhoKC9Sn/f0mlYxe2kHVsbaFsB5m/nZMjUtCVtIhVEqbPq144+mh5CMCzC2NONIwbquCGCxL5dz/07olhfm1KH1+JhX5LwAMPSprXYHfOlzMywk7Y7pnlYx3pNrqq629D7Y/UkoVQrRwvB4Og4NHJGhoSlEF8HLgLxgKTAUHfBAoUEqhcoH3tZqJF2bsTNN0KFk/TC0oun9MHzzUi4FcSfH/ZVKQ8Z52mIVskGEgJDNJB4cTkn1TVrxoGL4EycAUPBhXitNXpognhsz9M0fYwPHuDDIKksR7taouSw5TSpc9KQSI8voWrRnNNCxLyrv3+ptHGLyMuUSND7MhHwMQHXVTyvXi43gRaw+mqxqCFuYUiLXoCOppmfCXbbpMnBWjkjp1P1WVysjLdnPOUX4lH8bL/iNLpO/0XBJ8bKDAaOVukO3tyvgCKOqGDprIyC43c08Lrk2WOZUy7HbJ5iHDBcHn5L6jsUqYDiTPa8EL9dhoaseKsczBgetGscq+bkTOYe5dJDBXqbdQppxqag3OG6RHDbqE/SbLCsQkMGZDZ818sH6XgNmJdi/ODLsRQStuGcZtuZ+7FgdMVg44rkl7Q9JaVz3b4IwxrnieZfzfISKltPZjVa8yliTQkoPq0UzPFjQzlatMJ0mQTx4/ggXyC9fmE9vxgo8X4PzI+fDRV+iwrYA5qUBwEDRhagVeoRpTeRJ1rTmlGWHFdkoKvflhZ7OojLJjSy+HLMK3z0H1RkXiz6xPcWO1bznL6u7dB2pXQoxh7OpvJjK9Z5hEyNmv35ltK33MsZyElf/gyxcimKRQWmLCJ1E/EshsecjJrrQvbezfT7dxcwlSnLwZ4VFjf72bUEIzinyvmdfuTF4lWWxdm/O717ByRrGX1myTPZid3FgOYSOs Xpwnd0rX /Pf6SYvC6tp0cnpKg0PLqyULF2WDQmczQDMtO6TRnIy+LWQ1KsqvPOZ0BXi+BXfhRSgWWsp2L0ivy8NKSrct6bViGIRT53oEsj5dLDREbbYGkfULroU6Sgpe3YdKz0PRy1dt7rvVkqeM1KYW2FWRotACtWy+H4znTVyN83zToQxu2Ri6jkXn7yy1Uo/bIT2XXbGxLpliGIdctngBuCsvZJfpjFdU1amNTO8iQIrn84ZXcjQ+AmryxVkcnI0iW8qgGfX4nZHRIisK0s7vPt4+gDzt3BmLroWaXWh0zj9aGvci7llwPI58avKEbNt8zK4jru0ehmCMDQOXDY8WdQ6Kkcn0kRoEhCIZ9I6lI1oXLIvH19wLxWHJDnmHTN6XRyqOhkfq40FBBAqYijXZ9q3kW0B5CvpdVGVwWu67rccBDbYg9pOXhWmY38ak/Y1ryBPC5hWIJEa2t7ftBZ9uRRf/+0q4C9mTFEWS6f5+bsF4mu7XBtV5eIjqqMvzWMj5QHY8xFK/7DE6SSx9QNliCK0/4je2vhPwuNcjlCQPhsFA2SdaPus69R+YxTeoJxYZTj++09+vHxTFKUXST0qcZUM18gshl95kTT0DRnaDayriMazal9IxGkM+jH2Yafvtz5s8UsqKq4eDBlq8gLBGOZZ2RVb0jo20VzTpjKp4yJk3bslDgWNqDGTC45FWscuCcRnU2diLBzeMtrG1wDTfN8kD4IZ89XtxgaJMYfek7fp3NFQzZtiPQmw0Tb1wyczdS84q4Vw1AribbxYGZdDs= 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: Add a "vm_shape" structure to encapsulate the selftests-defined "mode", along with the KVM-defined "type" for use when creating a new VM. "mode" tracks physical and virtual address properties, as well as the preferred backing memory type, while "type" corresponds to the VM type. Taking the VM type will allow adding tests for KVM_CREATE_GUEST_MEMFD, a.k.a. guest private memory, without needing an entirely separate set of helpers. Guest private memory is effectively usable only by confidential VM types, and it's expected that x86 will double down and require unique VM types for TDX and SNP guests. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- .../selftests/kvm/include/kvm_util_base.h | 54 +++++++++++++++---- .../selftests/kvm/kvm_page_table_test.c | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 43 +++++++-------- tools/testing/selftests/kvm/lib/memstress.c | 3 +- .../kvm/x86_64/ucna_injection_test.c | 2 +- 6 files changed, 72 insertions(+), 34 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c index 936f3a8d1b83..6cbecf499767 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -699,7 +699,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, struct kvm_vcpu **vcpu, pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode)); - vm = __vm_create(mode, 1, extra_mem_pages); + vm = __vm_create(VM_SHAPE(mode), 1, extra_mem_pages); log_mode_create_vm_done(vm); *vcpu = vm_vcpu_add(vm, 0, guest_code); diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 1441fca6c273..157508c071f3 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -188,6 +188,23 @@ enum vm_guest_mode { NUM_VM_MODES, }; +struct vm_shape { + enum vm_guest_mode mode; + unsigned int type; +}; + +#define VM_TYPE_DEFAULT 0 + +#define VM_SHAPE(__mode) \ +({ \ + struct vm_shape shape = { \ + .mode = (__mode), \ + .type = VM_TYPE_DEFAULT \ + }; \ + \ + shape; \ +}) + #if defined(__aarch64__) extern enum vm_guest_mode vm_mode_default; @@ -220,6 +237,8 @@ extern enum vm_guest_mode vm_mode_default; #endif +#define VM_SHAPE_DEFAULT VM_SHAPE(VM_MODE_DEFAULT) + #define MIN_PAGE_SIZE (1U << MIN_PAGE_SHIFT) #define PTES_PER_MIN_PAGE ptes_per_page(MIN_PAGE_SIZE) @@ -784,21 +803,21 @@ vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm); * __vm_create() does NOT create vCPUs, @nr_runnable_vcpus is used purely to * calculate the amount of memory needed for per-vCPU data, e.g. stacks. */ -struct kvm_vm *____vm_create(enum vm_guest_mode mode); -struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus, +struct kvm_vm *____vm_create(struct vm_shape shape); +struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus, uint64_t nr_extra_pages); static inline struct kvm_vm *vm_create_barebones(void) { - return ____vm_create(VM_MODE_DEFAULT); + return ____vm_create(VM_SHAPE_DEFAULT); } static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus) { - return __vm_create(VM_MODE_DEFAULT, nr_runnable_vcpus, 0); + return __vm_create(VM_SHAPE_DEFAULT, nr_runnable_vcpus, 0); } -struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, +struct kvm_vm *__vm_create_with_vcpus(struct vm_shape shape, uint32_t nr_vcpus, uint64_t extra_mem_pages, void *guest_code, struct kvm_vcpu *vcpus[]); @@ -806,17 +825,27 @@ static inline struct kvm_vm *vm_create_with_vcpus(uint32_t nr_vcpus, void *guest_code, struct kvm_vcpu *vcpus[]) { - return __vm_create_with_vcpus(VM_MODE_DEFAULT, nr_vcpus, 0, + return __vm_create_with_vcpus(VM_SHAPE_DEFAULT, nr_vcpus, 0, guest_code, vcpus); } + +struct kvm_vm *__vm_create_shape_with_one_vcpu(struct vm_shape shape, + struct kvm_vcpu **vcpu, + uint64_t extra_mem_pages, + void *guest_code); + /* * Create a VM with a single vCPU with reasonable defaults and @extra_mem_pages * additional pages of guest memory. Returns the VM and vCPU (via out param). */ -struct kvm_vm *__vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, - uint64_t extra_mem_pages, - void *guest_code); +static inline struct kvm_vm *__vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, + uint64_t extra_mem_pages, + void *guest_code) +{ + return __vm_create_shape_with_one_vcpu(VM_SHAPE_DEFAULT, vcpu, + extra_mem_pages, guest_code); +} static inline struct kvm_vm *vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, void *guest_code) @@ -824,6 +853,13 @@ static inline struct kvm_vm *vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, return __vm_create_with_one_vcpu(vcpu, 0, guest_code); } +static inline struct kvm_vm *vm_create_shape_with_one_vcpu(struct vm_shape shape, + struct kvm_vcpu **vcpu, + void *guest_code) +{ + return __vm_create_shape_with_one_vcpu(shape, vcpu, 0, guest_code); +} + struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm); void kvm_pin_this_task_to_pcpu(uint32_t pcpu); diff --git a/tools/testing/selftests/kvm/kvm_page_table_test.c b/tools/testing/selftests/kvm/kvm_page_table_test.c index 69f26d80c821..e37dc9c21888 100644 --- a/tools/testing/selftests/kvm/kvm_page_table_test.c +++ b/tools/testing/selftests/kvm/kvm_page_table_test.c @@ -254,7 +254,7 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg) /* Create a VM with enough guest pages */ guest_num_pages = test_mem_size / guest_page_size; - vm = __vm_create_with_vcpus(mode, nr_vcpus, guest_num_pages, + vm = __vm_create_with_vcpus(VM_SHAPE(mode), nr_vcpus, guest_num_pages, guest_code, test_args.vcpus); /* Align down GPA of the testing memslot */ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index a140aee8d0f5..52b131e3aca5 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -209,7 +209,7 @@ __weak void vm_vaddr_populate_bitmap(struct kvm_vm *vm) (1ULL << (vm->va_bits - 1)) >> vm->page_shift); } -struct kvm_vm *____vm_create(enum vm_guest_mode mode) +struct kvm_vm *____vm_create(struct vm_shape shape) { struct kvm_vm *vm; @@ -221,13 +221,13 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode) vm->regions.hva_tree = RB_ROOT; hash_init(vm->regions.slot_hash); - vm->mode = mode; - vm->type = 0; + vm->mode = shape.mode; + vm->type = shape.type; - vm->pa_bits = vm_guest_mode_params[mode].pa_bits; - vm->va_bits = vm_guest_mode_params[mode].va_bits; - vm->page_size = vm_guest_mode_params[mode].page_size; - vm->page_shift = vm_guest_mode_params[mode].page_shift; + vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits; + vm->va_bits = vm_guest_mode_params[vm->mode].va_bits; + vm->page_size = vm_guest_mode_params[vm->mode].page_size; + vm->page_shift = vm_guest_mode_params[vm->mode].page_shift; /* Setup mode specific traits. */ switch (vm->mode) { @@ -265,7 +265,7 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode) /* * Ignore KVM support for 5-level paging (vm->va_bits == 57), * it doesn't take effect unless a CR4.LA57 is set, which it - * isn't for this VM_MODE. + * isn't for this mode (48-bit virtual address space). */ TEST_ASSERT(vm->va_bits == 48 || vm->va_bits == 57, "Linear address width (%d bits) not supported", @@ -285,10 +285,11 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode) vm->pgtable_levels = 5; break; default: - TEST_FAIL("Unknown guest mode, mode: 0x%x", mode); + TEST_FAIL("Unknown guest mode: 0x%x", vm->mode); } #ifdef __aarch64__ + TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types"); if (vm->pa_bits != 40) vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits); #endif @@ -347,19 +348,19 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode, return vm_adjust_num_guest_pages(mode, nr_pages); } -struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus, +struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus, uint64_t nr_extra_pages) { - uint64_t nr_pages = vm_nr_pages_required(mode, nr_runnable_vcpus, + uint64_t nr_pages = vm_nr_pages_required(shape.mode, nr_runnable_vcpus, nr_extra_pages); struct userspace_mem_region *slot0; struct kvm_vm *vm; int i; - pr_debug("%s: mode='%s' pages='%ld'\n", __func__, - vm_guest_mode_string(mode), nr_pages); + pr_debug("%s: mode='%s' type='%d', pages='%ld'\n", __func__, + vm_guest_mode_string(shape.mode), shape.type, nr_pages); - vm = ____vm_create(mode); + vm = ____vm_create(shape); vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, 0); for (i = 0; i < NR_MEM_REGIONS; i++) @@ -400,7 +401,7 @@ struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus, * extra_mem_pages is only used to calculate the maximum page table size, * no real memory allocation for non-slot0 memory in this function. */ -struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus, +struct kvm_vm *__vm_create_with_vcpus(struct vm_shape shape, uint32_t nr_vcpus, uint64_t extra_mem_pages, void *guest_code, struct kvm_vcpu *vcpus[]) { @@ -409,7 +410,7 @@ struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus TEST_ASSERT(!nr_vcpus || vcpus, "Must provide vCPU array"); - vm = __vm_create(mode, nr_vcpus, extra_mem_pages); + vm = __vm_create(shape, nr_vcpus, extra_mem_pages); for (i = 0; i < nr_vcpus; ++i) vcpus[i] = vm_vcpu_add(vm, i, guest_code); @@ -417,15 +418,15 @@ struct kvm_vm *__vm_create_with_vcpus(enum vm_guest_mode mode, uint32_t nr_vcpus return vm; } -struct kvm_vm *__vm_create_with_one_vcpu(struct kvm_vcpu **vcpu, - uint64_t extra_mem_pages, - void *guest_code) +struct kvm_vm *__vm_create_shape_with_one_vcpu(struct vm_shape shape, + struct kvm_vcpu **vcpu, + uint64_t extra_mem_pages, + void *guest_code) { struct kvm_vcpu *vcpus[1]; struct kvm_vm *vm; - vm = __vm_create_with_vcpus(VM_MODE_DEFAULT, 1, extra_mem_pages, - guest_code, vcpus); + vm = __vm_create_with_vcpus(shape, 1, extra_mem_pages, guest_code, vcpus); *vcpu = vcpus[0]; return vm; diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/selftests/kvm/lib/memstress.c index df457452d146..d05487e5a371 100644 --- a/tools/testing/selftests/kvm/lib/memstress.c +++ b/tools/testing/selftests/kvm/lib/memstress.c @@ -168,7 +168,8 @@ struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus, * The memory is also added to memslot 0, but that's a benign side * effect as KVM allows aliasing HVAs in meslots. */ - vm = __vm_create_with_vcpus(mode, nr_vcpus, slot0_pages + guest_num_pages, + vm = __vm_create_with_vcpus(VM_SHAPE(mode), nr_vcpus, + slot0_pages + guest_num_pages, memstress_guest_code, vcpus); args->vm = vm; diff --git a/tools/testing/selftests/kvm/x86_64/ucna_injection_test.c b/tools/testing/selftests/kvm/x86_64/ucna_injection_test.c index 85f34ca7e49e..0ed32ec903d0 100644 --- a/tools/testing/selftests/kvm/x86_64/ucna_injection_test.c +++ b/tools/testing/selftests/kvm/x86_64/ucna_injection_test.c @@ -271,7 +271,7 @@ int main(int argc, char *argv[]) kvm_check_cap(KVM_CAP_MCE); - vm = __vm_create(VM_MODE_DEFAULT, 3, 0); + vm = __vm_create(VM_SHAPE_DEFAULT, 3, 0); kvm_ioctl(vm->kvm_fd, KVM_X86_GET_MCE_CAP_SUPPORTED, &supported_mcg_caps); From patchwork Fri Oct 27 18:22:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438956 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 7A7FAC25B48 for ; Fri, 27 Oct 2023 18:23:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0CC2480025; Fri, 27 Oct 2023 14:23:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0533280018; Fri, 27 Oct 2023 14:23:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4B3080025; Fri, 27 Oct 2023 14:23:27 -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 B727580018 for ; Fri, 27 Oct 2023 14:23:27 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 943B740779 for ; Fri, 27 Oct 2023 18:23:27 +0000 (UTC) X-FDA: 81392064054.21.942A4F8 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf13.hostedemail.com (Postfix) with ESMTP id C99DD2000D for ; Fri, 27 Oct 2023 18:23:25 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tHB79qhs; spf=pass (imf13.hostedemail.com: domain of 3HAA8ZQYKCD0rdZmibfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3HAA8ZQYKCD0rdZmibfnnfkd.bnlkhmtw-lljuZbj.nqf@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=1698431005; 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=zYKW06Y9tS7nzBZmp5ooZeVm0pRKPQ996s+RZNHFHDA=; b=18d57D7utMx6vNt1cno2luKo2WebAvzkmkQypqzYf/Cq/Rm4grvQwbWKLA3EdAXeF/6OJH 1tvz5xwC05LeMEC/7fXcup5btZitdv0FdyZVx5U5/nsyavN2QOp+Z2+/nQbCsMnWRSRN2i mP/JgjDS3bkY/OR6WBXDx24v8kBxrsk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431005; a=rsa-sha256; cv=none; b=x4jgEpcGAz2gRw7s3rGK/iOctT8GZno0hs8Z4hTP7d2pdHQ7TdZefUdJn6g3PR1eWMSja8 szVYddcK9MdWVEXzZEjswr51k4+Hg6M+onXN27feefom07VtkJCxe7mGjOX8dieSkSPROZ YQNy7U5UruUGCDjZsbjC3E71Ql1ohwI= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tHB79qhs; spf=pass (imf13.hostedemail.com: domain of 3HAA8ZQYKCD0rdZmibfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3HAA8ZQYKCD0rdZmibfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-59b5a586da6so22567597b3.1 for ; Fri, 27 Oct 2023 11:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431005; x=1699035805; 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=zYKW06Y9tS7nzBZmp5ooZeVm0pRKPQ996s+RZNHFHDA=; b=tHB79qhsbnOlFMSSOUSiINdtCh+XGxJajb8oGSNVMkt1NX47WZiX5MWHwDP5L6UMkQ zLui5oG5kvadnjwylug5pcrZuHJxEfC47Bs8ZWtyC/JCkIW0LMYfgDL2jy2u5EH8B6DV MN1UhWKVisdLPtIQT8TXhbNxOJ243cizsVc6g6ipu1BN0VUYCyKM1EYgdwJiDnVzNPXu xSdsXqoX0MqQvVLN4oeD+5We7FRcRFx9FeUzJvJJ5kWTTsyUxr/Vji+xKFv8es6cnvo1 AtPIB+tq6A/9/X2FxuuqXHbYVdnSfEgP2bVvXxWj+ptKcWmfvuq18TgEWJ3rgw3YNjcw F9Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431005; x=1699035805; 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=zYKW06Y9tS7nzBZmp5ooZeVm0pRKPQ996s+RZNHFHDA=; b=vBTbu4mlIXs+6jFzTd7XbCo2Qi4eKSqxWyOYzuetB58UYML1NgCnphLxuecFhX2wRY DDcywJxbF6Pj0ABR7c3uqYGwUommfzOWEBQzxZ2nyrPxd8NGkleIGp65IIGPcHh9HoNd pbltwnLqqKuBUmE+oq45A5DR3Xn1y11H5muw03fN4kEHbsQHZnAbnvTcBiq5kCjtOHgR 0tMpI3k6tlUtN3ld6pVONyFflAku0tDyD8qf/eMzBBMBrdNzW98Gj7F4zAZHhbcvzsGp prEXTrsU41JefRMlOjOaBnpmyCgM24sBkvP9SzaImXxmJZwv/uf6I2smoHeAeTeZnJ8y dJZw== X-Gm-Message-State: AOJu0YxR8nFWm59+CxAlVwfe4FCVLOY1NS8LqHztr/1sssEtityw1EsD 98oBcl8fYMd2VhUnszapZYsKGRPsIo8= X-Google-Smtp-Source: AGHT+IErWdjWWNGtxZOL2jGOuLZz5+Zkhfhc0XQVVHxJSZZLCXWd4MbZY0rGLoG0iR2uIHcgyGOCoD0Mx1Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:ca0f:0:b0:59b:e663:23b0 with SMTP id m15-20020a0dca0f000000b0059be66323b0mr102891ywd.4.1698431004987; Fri, 27 Oct 2023 11:23:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:12 -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-31-seanjc@google.com> Subject: [PATCH v13 30/35] KVM: selftests: Add GUEST_SYNC[1-6] macros for synchronizing more data 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-Rspamd-Queue-Id: C99DD2000D X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: bzresrrn31weeuzoxz56c8kse8489qfh X-HE-Tag: 1698431005-989966 X-HE-Meta: U2FsdGVkX1/Ov2L935bGzrNk/SbnFVaVl5Mq4DEDQisqA70EWqYmjFe7ORxEdHGGfSkw2fEu7Yev5AEUQMva8rMpbGuhS68CKU14HaU4TyIwZWhXA+U+cNIefCE6wo+77YEI7Dw9P36bculihDG/A1ZhRD+DLrnOsnp3S6YMgzQbdEHaurNAZQtydHOUyO0V+DncnwN/ZP2kSGTXcdHmyoWHh5LdTzp9689m3WgNBRN2orvZsjrD7/VjFLs5qdqQ+sBnwDyrTLuJE2mcG2srZu09Vx7AG4bfXW05hybuDVbZ9XoztcoFRLuQAJ7soOognzzH/R4s7ROuVR9mW2q4Sfl1he0z7WLLeW2UvfN40qH4LugxUVg85Dx02CvRd0fm0stKSUtUusc+yt7PvfJi1wAAcztY4lBAXdviYd8rzmZfn7e/gSsV9DQ+pZP3ptVt91CIDa2rXefrRT0bk7SRms2wKs+aIUMtNgvZDDtO7yronhXEPp3u/L8xpUbvKTuK4jyKKbxv6KmAd3BUN/ZmWNnbgqVwuT39ZaoFADzzeVD5gmXYB4dW5HIcqSsEGP2Q3DxNR//av/jU/PYYPuFHppQ4yp10AGfxByTupcVDVhn/NDtyfQLnWXOlC/sMryGqwsQ/xDZijzIp0c1ZMfPLIPCtU4KfYUmn3kNywZQOscsBmkK+qNjQ7W44cY/b241tOHDSACMEIZMYZIG1ZjWOFj6vu7EskmigqGCzHoilX+xtHXgvV7WsBhKEr9zOIpXpbA7GUS/YD1FL3du/7ncwMYpVds++KXLVGyznQuCLtdPjeErCfjX1Gh2KwA2Fbj2+dptk0B+j9CfEz0PedTIKr0PO1RnuMhPn6l9zh83uklkTgQrvkMRVpQj7Zzi9BUbIhGaV5QQKJsIajINBxHmtSvCg3d13/3LasWdRUiqy65C2wCrIVUzCAiHvjgy+29chsqJMiij0TuN4+3cvM4a Waf/VBEN 5Kmep7kU7agiW5efkkPeVT/F7pwkxVdZXjYk0Wephb0VO7vsVS5Jd3bZtLTaZvtwqwTF8kNYsnA6crIO2l5NlyDNd+Pjmv0FmS/rkMntDmWRhzNCw7ffUgoQ0xZk3gyeX+XasowRE7ZTjOU3nTYfh/xfnptocZtJ3FUHla7YuyxYFh8PaY6HFfLeC+yVLCRgWW2Z7Pp/Te/HvJyCf6yguoCoN4iRrwIaKGpg2VBhWMTyVc5rUqWc0wKv0WYa8/N+HWby7BBKkwlegj1ikWl9ye2zP5TE8nqVdo7+Qg0ikOMHy7FgfrWBA0IhHARS++RBfnQHZkuFVsfw+g0xSeU9z7Ohs9w8JZMbLOXmtg3jlvdJc7Usd+XVYpnggtHQqJS9w7dqHZ2EU8q8g1gdx6rNkvaw3Bv6ILNBjzisvMcCgnNTeZIkwJ7Vf9jR/d2kklpnsVAGhpBdXd2dIilfz6dSz2x9O3T01+tFirk9ReOkz7mTIPHKHEcAOlghiZCda0y3OoKdDRg1VedrDFRt/In7cSCbkZXu0mNtoY4BnB1dFOR+w+C2wJ5foPoSx+z3QeOr3OxNXJhITSKHhCwbowoQKYCese6Ys+riAZxoxUN0jSlK3ueS09d1+eN9VL67WnixhSk060y2ydLJN19hzN52MBZYQW2hzixmofnLtUMbMehjgoqgUvqgUWC9s32XqeZGFg2kV0ZRcR+3Y6HIDzJqfs4wuYkCPRsh1gVa4PPrBx8h3dB0Bxs35dEaC+y8nwOUHHX75qPEk5Acfm67hHKCRiWhaNpKOlFOYEuHWIYi56uX0FOxyoDIutAIfFT2VTMAh+IRW X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add GUEST_SYNC[1-6]() so that tests can pass the maximum amount of information supported via ucall(), without needing to resort to shared memory. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/include/ucall_common.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tools/testing/selftests/kvm/include/ucall_common.h b/tools/testing/selftests/kvm/include/ucall_common.h index ce33d306c2cb..0fb472a5a058 100644 --- a/tools/testing/selftests/kvm/include/ucall_common.h +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -52,6 +52,17 @@ int ucall_nr_pages_required(uint64_t page_size); #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4) #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage) +#define GUEST_SYNC1(arg0) ucall(UCALL_SYNC, 1, arg0) +#define GUEST_SYNC2(arg0, arg1) ucall(UCALL_SYNC, 2, arg0, arg1) +#define GUEST_SYNC3(arg0, arg1, arg2) \ + ucall(UCALL_SYNC, 3, arg0, arg1, arg2) +#define GUEST_SYNC4(arg0, arg1, arg2, arg3) \ + ucall(UCALL_SYNC, 4, arg0, arg1, arg2, arg3) +#define GUEST_SYNC5(arg0, arg1, arg2, arg3, arg4) \ + ucall(UCALL_SYNC, 5, arg0, arg1, arg2, arg3, arg4) +#define GUEST_SYNC6(arg0, arg1, arg2, arg3, arg4, arg5) \ + ucall(UCALL_SYNC, 6, arg0, arg1, arg2, arg3, arg4, arg5) + #define GUEST_PRINTF(_fmt, _args...) ucall_fmt(UCALL_PRINTF, _fmt, ##_args) #define GUEST_DONE() ucall(UCALL_DONE, 0) From patchwork Fri Oct 27 18:22:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438957 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 DE4FCC25B6F for ; Fri, 27 Oct 2023 18:23:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B00880026; Fri, 27 Oct 2023 14:23:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3389580018; Fri, 27 Oct 2023 14:23:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1652080026; Fri, 27 Oct 2023 14:23: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 E8DA380018 for ; Fri, 27 Oct 2023 14:23:29 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CAFBE1CB192 for ; Fri, 27 Oct 2023 18:23:29 +0000 (UTC) X-FDA: 81392064138.30.22E84C1 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf20.hostedemail.com (Postfix) with ESMTP id E83AE1C000C for ; Fri, 27 Oct 2023 18:23:27 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O2dPXxAO; spf=pass (imf20.hostedemail.com: domain of 3HgA8ZQYKCD8tfbokdhpphmf.dpnmjovy-nnlwbdl.psh@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3HgA8ZQYKCD8tfbokdhpphmf.dpnmjovy-nnlwbdl.psh@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431008; a=rsa-sha256; cv=none; b=ysmv7UxCQZWqjLi00dSJjvgqxyJDmMdqlCge3E8EktvQiWteMKLLEtVa3T6QGnt/TE/d+d 4m0N9NJlBYAwVqgVNFSoTpn+Sh0rolxUAzsKqA/BFXHjJXTAsWSdnwq/s3XDktghWIHOaB L708O+CMtJ+HdO8EODIBuTxo4hGlJpg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O2dPXxAO; spf=pass (imf20.hostedemail.com: domain of 3HgA8ZQYKCD8tfbokdhpphmf.dpnmjovy-nnlwbdl.psh@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3HgA8ZQYKCD8tfbokdhpphmf.dpnmjovy-nnlwbdl.psh@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=1698431008; 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=T7FIEFWMTHcKy9+/ipKnFwfl/ivaNmc30McFuHgIoSI=; b=Th3Br5WxrtwLBX7Tk6NKYSPgVpsSs/shDpDegaBuiD0AWqE1B0ooVpkqzT9tn+4QEPeVOY sgeLqvByYm/r0uD/yGFq/+M7vCZOdO8NwkL70ETVzQ2IP8W2wf1uYUTGfLaX8K7SRWJ9Ys JVO0EPpkDhW1vbh5YgA6sk2rskaW1Ic= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5af9ad9341fso10352587b3.2 for ; Fri, 27 Oct 2023 11:23:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431007; x=1699035807; 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=T7FIEFWMTHcKy9+/ipKnFwfl/ivaNmc30McFuHgIoSI=; b=O2dPXxAO3sdW+l0AXyVMkJ4QCF7nPe8z8x5Ppb0FL1l2lA0Mlytq9HJIC8uwSjGsON mq/k0aV1xuNjk0PF9tRpQ/DdEGJxa2aXS/TwkiNUptcV+VEuLSOieBQ3u9CAECAKPw34 IDyreMUt4ngab+AVixSUn7pBZqylebJewLbCLdhGjHaOYimO4FfXo8cCaKFsPXSEm4Mb m9Em3ccK9nIQWhFPH/Nfnw9zsO6oR8CU8iZswX5e+3Owpl/Y1XvPL5nwHc9oMTwRUGN3 Lec8uIGhS2143Crcs1oTMa1r/CKuEbEBRaUcYniMuFggChFfmoz8ikr1iVD3j1SrLFGN DLBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431007; x=1699035807; 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=T7FIEFWMTHcKy9+/ipKnFwfl/ivaNmc30McFuHgIoSI=; b=RuFQ0MskIlDhpuALi+j0QxG+ZGNl2d77gBdGfvZW4HtP0KU4xjr1/phN0R2+TVG+Re 3/wHOWc/F96gQZ1ctItQ9JL+24aqNulq50xYpQLdb26ZDG1HUSJdpYpj+7dYmPqwuqgw 0yL+nFw3W6bXvTR4XIv3E+U9athcYU3YXiqlrylCu5eiEoyaw+afo4Qc0DO946qxutXa DCfXiHnKF4IW4JcvW2RsYUdttDgFnUQBaMD4zdxcsB/dIOGu4yHBiRY20f1NJH8rb/fp XoxDeuUxhjzLBpKX6G7Ek/LqzKMMyqyMBNMBm+smQh8S44sw0xZjdHeEzQSvwfYUtsG7 Zq3A== X-Gm-Message-State: AOJu0Yy+RdB3UNQnNSXzpRGQwKaSaYI/qz62N4RqDR1OxJIM51Ze2hRi PHS8jm9aGSiex3UYHyO64IhIVxSJ010= X-Google-Smtp-Source: AGHT+IFeBZl1mAZMVx0Zc8JpU9yhzOGEjS2s1PC6Kf7tqZmKBU40PadqAyuoyQRAwzaLgXgWe5XnLK2Knrg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:da0:567d:f819 with SMTP id v3-20020a056902108300b00da0567df819mr78721ybu.10.1698431006925; Fri, 27 Oct 2023 11:23:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:13 -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-32-seanjc@google.com> Subject: [PATCH v13 31/35] KVM: selftests: Add x86-only selftest for private memory conversions 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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E83AE1C000C X-Stat-Signature: a75po31wpmsz45khb4dxaykwr49xxu1m X-Rspam-User: X-HE-Tag: 1698431007-475848 X-HE-Meta: U2FsdGVkX1+tlRKWfvpwAUv4lI/862+6UzuRFMBDn3N99kOvbOzwxtBBmgjcbLrxpqjmfNyyhdUly5oE9iJ4bnKaVjr3fVWdtzMOfCymvhsUUHtmekumd0risEHpW5L/vMBX0SZPVCZKjjX1YnP+xQf7vpGrpRt7fwAvBPwSDw2Sgsd+55Au51qMtn3tyaZEM9ggoJZK1MNkPcXvJ7LMdk/45127NMb9So9sy0+eArPfxBDI5hBIjmKJvUPZprBI45TSjQrNfCxH0xBzIC+AcfmVlLORUZkajseJd7VjPBKv9OaO0oezRdseRDfTGdUbEy0GwJRQ5zHSYo2yLEe5m+XPe3m+0jXTscAU3CIBRLV++l8lbSZPCvq4GZBaq+CIwbXaeTCpwWOgTcDwmXdt/vrlGioGf33MF5INbk8315ZeEqLlMh0STGEZHlSeWejzu9V4F+WermX90TWbz/0wFI1gHTatk5TmqHb0Z2FPbYN6tjHTdDUT3rglFM/ym6hmzstRMy9k9l6CpIjqpoUCny04hk6f1BHeuZaGJlthkUjhuT2jMuSxXU1OGXXI7Y3wVZNWidcRKyDKLxib5iaJN2pMEFqibWyTAmpMOTjswXQDkOMvl1d7tvh8q5aStiKheo93MEi5dYV6qc8z6NG9wWcSjPSLCFFteVUKri79O/wKSP+S3I3IFWNqDv2b08IyO3OMrxtMetxjXVj/mxw6GGGhhk9qESYxMSYRZ15Xnb/tRQUZD4TJgE7XIGmaLfqPrN7Z/43VXT4E1yDKDfprtJb4jndM++p3ZZ0/HHhaPVjt7k+7aaiMwXeFEOgkjshpI6EGWhZ7l90FHSclnMqso7r9f7KzarKCBauK96ah9nCEvgopMknRFGsodLelZ/jpx3dUE64yfGmvWuzkI/UWRMF3RKLBcC2IX8KemOScS8k7EDFxbgC+Hm8S6IOObvDPUQqCBorYZ4+6xbxkj0K LfzmReBt ki3TDSpaim04QPXqWijCEG4mjq23IlJw/1sv+WNRIRFiQzUpokN4kCdS5goQs4Pj8vLd4n1ltXdeLmzK1mBLYg2FHZb2pYyK0EErR5/CnPn5PIceCI/sx4kf/hMbSvqX6W4Na8O9FK9BreN1PS7y3eDsZsnKDhUgeUyi3c6JqdaE39exwj8nelSgHuR9oCs22tJQ8mYJwOTLnd/EN/1e1+XehB1OwoVruC0XfNgnZ1fpZYk8Ynyz0Ju9KiiMv3tOsCpe49P0o9tJll2WGtq4UfpdHno3paBZYPOhPhLwGQ5vGNL1K64divWBI6oiydILyB3LXVBZYr+aueQmUGsUrZOKPBypcFszw2/IBwbPs1qrrDVln/gKvUtYF73zFG8okjyBkwCQiMIL6KnzHL3KumqsvaUiZXZ0rLPJw4AP7iuBdKn1L8zVfjm2hswFLk3LuEEVJ9mEpz00BTVnEsqL2RrkYCYriuTp17RxRst3+SLbdQX+d6JC+FiBY6i7aStQEy2iYWwwpmIZKCQeFJxBrkEziqTwGr/XdZYSyinz/XV/BZukY/D+2+BBvfk47FWBBG4eQLOO0WfE4bom6HJ91RE95G6vry6dEbjhV497tbpwe3/NtXygn4+lByNfupeHiorzv0wlOf554KeiZD2NLN+IJteGmC6Y5zH6RPxdUN5nQ9r2Eo+N3dcKaxVz/ZQslzvMuC1juaEbBv+5vLEtuVP4L2RHCMBhUm24uNxdopWBVTm8RyhxJy7c8j4AdQzR/ODhnrb6sE2zgF60= 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: From: Vishal Annapurve Add a selftest to exercise implicit/explicit conversion functionality within KVM and verify: - Shared memory is visible to host userspace - Private memory is not visible to host userspace - Host userspace and guest can communicate over shared memory - Data in shared backing is preserved across conversions (test's host userspace doesn't free the data) - Private memory is bound to the lifetime of the VM Ideally, KVM's selftests infrastructure would be reworked to allow backing a single region of guest memory with multiple memslots for _all_ backing types and shapes, i.e. ideally the code for using a single backing fd across multiple memslots would work for "regular" memory as well. But sadly, support for KVM_CREATE_GUEST_MEMFD has languished for far too long, and overhauling selftests' memslots infrastructure would likely open a can of worms, i.e. delay things even further. In addition to the more obvious tests, verify that PUNCH_HOLE actually frees memory. Directly verifying that KVM frees memory is impractical, if it's even possible, so instead indirectly verify memory is freed by asserting that the guest reads zeroes after a PUNCH_HOLE. E.g. if KVM zaps SPTEs but doesn't actually punch a hole in the inode, the subsequent read will still see the previous value. And obviously punching a hole shouldn't cause explosions. Let the user specify the number of memslots in the private mem conversion test, i.e. don't require the number of memslots to be '1' or "nr_vcpus". Creating more memslots than vCPUs is particularly interesting, e.g. it can result in a single KVM_SET_MEMORY_ATTRIBUTES spanning multiple memslots. To keep the math reasonable, align each vCPU's chunk to at least 2MiB (the size is 2MiB+4KiB), and require the total size to be cleanly divisible by the number of memslots. The goal is to be able to validate that KVM plays nice with multiple memslots, being able to create a truly arbitrary number of memslots doesn't add meaningful value, i.e. isn't worth the cost. Intentionally don't take a requirement on KVM_CAP_GUEST_MEMFD, KVM_CAP_MEMORY_FAULT_INFO, KVM_MEMORY_ATTRIBUTE_PRIVATE, etc., as it's a KVM bug to advertise KVM_X86_SW_PROTECTED_VM without its prerequisites. Signed-off-by: Vishal Annapurve Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_conversions_test.c | 487 ++++++++++++++++++ 2 files changed, 488 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index a3bb36fb3cfc..b709a52d5cdb 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -81,6 +81,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/monitor_mwait_test TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c new file mode 100644 index 000000000000..be311944e90a --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -0,0 +1,487 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022, Google LLC. + */ +#define _GNU_SOURCE /* for program_invocation_short_name */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +#define BASE_DATA_SLOT 10 +#define BASE_DATA_GPA ((uint64_t)(1ull << 32)) +#define PER_CPU_DATA_SIZE ((uint64_t)(SZ_2M + PAGE_SIZE)) + +/* Horrific macro so that the line info is captured accurately :-( */ +#define memcmp_g(gpa, pattern, size) \ +do { \ + uint8_t *mem = (uint8_t *)gpa; \ + size_t i; \ + \ + for (i = 0; i < size; i++) \ + __GUEST_ASSERT(mem[i] == pattern, \ + "Guest expected 0x%x at offset %lu (gpa 0x%llx), got 0x%x", \ + pattern, i, gpa + i, mem[i]); \ +} while (0) + +static void memcmp_h(uint8_t *mem, uint64_t gpa, uint8_t pattern, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++) + TEST_ASSERT(mem[i] == pattern, + "Host expected 0x%x at gpa 0x%lx, got 0x%x", + pattern, gpa + i, mem[i]); +} + +/* + * Run memory conversion tests with explicit conversion: + * Execute KVM hypercall to map/unmap gpa range which will cause userspace exit + * to back/unback private memory. Subsequent accesses by guest to the gpa range + * will not cause exit to userspace. + * + * Test memory conversion scenarios with following steps: + * 1) Access private memory using private access and verify that memory contents + * are not visible to userspace. + * 2) Convert memory to shared using explicit conversions and ensure that + * userspace is able to access the shared regions. + * 3) Convert memory back to private using explicit conversions and ensure that + * userspace is again not able to access converted private regions. + */ + +#define GUEST_STAGE(o, s) { .offset = o, .size = s } + +enum ucall_syncs { + SYNC_SHARED, + SYNC_PRIVATE, +}; + +static void guest_sync_shared(uint64_t gpa, uint64_t size, + uint8_t current_pattern, uint8_t new_pattern) +{ + GUEST_SYNC5(SYNC_SHARED, gpa, size, current_pattern, new_pattern); +} + +static void guest_sync_private(uint64_t gpa, uint64_t size, uint8_t pattern) +{ + GUEST_SYNC4(SYNC_PRIVATE, gpa, size, pattern); +} + +/* Arbitrary values, KVM doesn't care about the attribute flags. */ +#define MAP_GPA_SET_ATTRIBUTES BIT(0) +#define MAP_GPA_SHARED BIT(1) +#define MAP_GPA_DO_FALLOCATE BIT(2) + +static void guest_map_mem(uint64_t gpa, uint64_t size, bool map_shared, + bool do_fallocate) +{ + uint64_t flags = MAP_GPA_SET_ATTRIBUTES; + + if (map_shared) + flags |= MAP_GPA_SHARED; + if (do_fallocate) + flags |= MAP_GPA_DO_FALLOCATE; + kvm_hypercall_map_gpa_range(gpa, size, flags); +} + +static void guest_map_shared(uint64_t gpa, uint64_t size, bool do_fallocate) +{ + guest_map_mem(gpa, size, true, do_fallocate); +} + +static void guest_map_private(uint64_t gpa, uint64_t size, bool do_fallocate) +{ + guest_map_mem(gpa, size, false, do_fallocate); +} + +struct { + uint64_t offset; + uint64_t size; +} static const test_ranges[] = { + GUEST_STAGE(0, PAGE_SIZE), + GUEST_STAGE(0, SZ_2M), + GUEST_STAGE(PAGE_SIZE, PAGE_SIZE), + GUEST_STAGE(PAGE_SIZE, SZ_2M), + GUEST_STAGE(SZ_2M, PAGE_SIZE), +}; + +static void guest_test_explicit_conversion(uint64_t base_gpa, bool do_fallocate) +{ + const uint8_t def_p = 0xaa; + const uint8_t init_p = 0xcc; + uint64_t j; + int i; + + /* Memory should be shared by default. */ + memset((void *)base_gpa, def_p, PER_CPU_DATA_SIZE); + memcmp_g(base_gpa, def_p, PER_CPU_DATA_SIZE); + guest_sync_shared(base_gpa, PER_CPU_DATA_SIZE, def_p, init_p); + + memcmp_g(base_gpa, init_p, PER_CPU_DATA_SIZE); + + for (i = 0; i < ARRAY_SIZE(test_ranges); i++) { + uint64_t gpa = base_gpa + test_ranges[i].offset; + uint64_t size = test_ranges[i].size; + uint8_t p1 = 0x11; + uint8_t p2 = 0x22; + uint8_t p3 = 0x33; + uint8_t p4 = 0x44; + + /* + * Set the test region to pattern one to differentiate it from + * the data range as a whole (contains the initial pattern). + */ + memset((void *)gpa, p1, size); + + /* + * Convert to private, set and verify the private data, and + * then verify that the rest of the data (map shared) still + * holds the initial pattern, and that the host always sees the + * shared memory (initial pattern). Unlike shared memory, + * punching a hole in private memory is destructive, i.e. + * previous values aren't guaranteed to be preserved. + */ + guest_map_private(gpa, size, do_fallocate); + + if (size > PAGE_SIZE) { + memset((void *)gpa, p2, PAGE_SIZE); + goto skip; + } + + memset((void *)gpa, p2, size); + guest_sync_private(gpa, size, p1); + + /* + * Verify that the private memory was set to pattern two, and + * that shared memory still holds the initial pattern. + */ + memcmp_g(gpa, p2, size); + if (gpa > base_gpa) + memcmp_g(base_gpa, init_p, gpa - base_gpa); + if (gpa + size < base_gpa + PER_CPU_DATA_SIZE) + memcmp_g(gpa + size, init_p, + (base_gpa + PER_CPU_DATA_SIZE) - (gpa + size)); + + /* + * Convert odd-number page frames back to shared to verify KVM + * also correctly handles holes in private ranges. + */ + for (j = 0; j < size; j += PAGE_SIZE) { + if ((j >> PAGE_SHIFT) & 1) { + guest_map_shared(gpa + j, PAGE_SIZE, do_fallocate); + guest_sync_shared(gpa + j, PAGE_SIZE, p1, p3); + + memcmp_g(gpa + j, p3, PAGE_SIZE); + } else { + guest_sync_private(gpa + j, PAGE_SIZE, p1); + } + } + +skip: + /* + * Convert the entire region back to shared, explicitly write + * pattern three to fill in the even-number frames before + * asking the host to verify (and write pattern four). + */ + guest_map_shared(gpa, size, do_fallocate); + memset((void *)gpa, p3, size); + guest_sync_shared(gpa, size, p3, p4); + memcmp_g(gpa, p4, size); + + /* Reset the shared memory back to the initial pattern. */ + memset((void *)gpa, init_p, size); + + /* + * Free (via PUNCH_HOLE) *all* private memory so that the next + * iteration starts from a clean slate, e.g. with respect to + * whether or not there are pages/folios in guest_mem. + */ + guest_map_shared(base_gpa, PER_CPU_DATA_SIZE, true); + } +} + +static void guest_punch_hole(uint64_t gpa, uint64_t size) +{ + /* "Mapping" memory shared via fallocate() is done via PUNCH_HOLE. */ + uint64_t flags = MAP_GPA_SHARED | MAP_GPA_DO_FALLOCATE; + + kvm_hypercall_map_gpa_range(gpa, size, flags); +} + +/* + * Test that PUNCH_HOLE actually frees memory by punching holes without doing a + * proper conversion. Freeing (PUNCH_HOLE) should zap SPTEs, and reallocating + * (subsequent fault) should zero memory. + */ +static void guest_test_punch_hole(uint64_t base_gpa, bool precise) +{ + const uint8_t init_p = 0xcc; + int i; + + /* + * Convert the entire range to private, this testcase is all about + * punching holes in guest_memfd, i.e. shared mappings aren't needed. + */ + guest_map_private(base_gpa, PER_CPU_DATA_SIZE, false); + + for (i = 0; i < ARRAY_SIZE(test_ranges); i++) { + uint64_t gpa = base_gpa + test_ranges[i].offset; + uint64_t size = test_ranges[i].size; + + /* + * Free all memory before each iteration, even for the !precise + * case where the memory will be faulted back in. Freeing and + * reallocating should obviously work, and freeing all memory + * minimizes the probability of cross-testcase influence. + */ + guest_punch_hole(base_gpa, PER_CPU_DATA_SIZE); + + /* Fault-in and initialize memory, and verify the pattern. */ + if (precise) { + memset((void *)gpa, init_p, size); + memcmp_g(gpa, init_p, size); + } else { + memset((void *)base_gpa, init_p, PER_CPU_DATA_SIZE); + memcmp_g(base_gpa, init_p, PER_CPU_DATA_SIZE); + } + + /* + * Punch a hole at the target range and verify that reads from + * the guest succeed and return zeroes. + */ + guest_punch_hole(gpa, size); + memcmp_g(gpa, 0, size); + } +} + +static void guest_code(uint64_t base_gpa) +{ + /* + * Run the conversion test twice, with and without doing fallocate() on + * the guest_memfd backing when converting between shared and private. + */ + guest_test_explicit_conversion(base_gpa, false); + guest_test_explicit_conversion(base_gpa, true); + + /* + * Run the PUNCH_HOLE test twice too, once with the entire guest_memfd + * faulted in, once with only the target range faulted in. + */ + guest_test_punch_hole(base_gpa, false); + guest_test_punch_hole(base_gpa, true); + GUEST_DONE(); +} + +static void handle_exit_hypercall(struct kvm_vcpu *vcpu) +{ + struct kvm_run *run = vcpu->run; + uint64_t gpa = run->hypercall.args[0]; + uint64_t size = run->hypercall.args[1] * PAGE_SIZE; + bool set_attributes = run->hypercall.args[2] & MAP_GPA_SET_ATTRIBUTES; + bool map_shared = run->hypercall.args[2] & MAP_GPA_SHARED; + bool do_fallocate = run->hypercall.args[2] & MAP_GPA_DO_FALLOCATE; + struct kvm_vm *vm = vcpu->vm; + + TEST_ASSERT(run->hypercall.nr == KVM_HC_MAP_GPA_RANGE, + "Wanted MAP_GPA_RANGE (%u), got '%llu'", + KVM_HC_MAP_GPA_RANGE, run->hypercall.nr); + + if (do_fallocate) + vm_guest_mem_fallocate(vm, gpa, size, map_shared); + + if (set_attributes) + vm_set_memory_attributes(vm, gpa, size, + map_shared ? 0 : KVM_MEMORY_ATTRIBUTE_PRIVATE); + run->hypercall.ret = 0; +} + +static bool run_vcpus; + +static void *__test_mem_conversions(void *__vcpu) +{ + struct kvm_vcpu *vcpu = __vcpu; + struct kvm_run *run = vcpu->run; + struct kvm_vm *vm = vcpu->vm; + struct ucall uc; + + while (!READ_ONCE(run_vcpus)) + ; + + for ( ;; ) { + vcpu_run(vcpu); + + if (run->exit_reason == KVM_EXIT_HYPERCALL) { + handle_exit_hypercall(vcpu); + continue; + } + + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, + "Wanted KVM_EXIT_IO, got exit reason: %u (%s)", + run->exit_reason, exit_reason_str(run->exit_reason)); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + case UCALL_SYNC: { + uint64_t gpa = uc.args[1]; + size_t size = uc.args[2]; + size_t i; + + TEST_ASSERT(uc.args[0] == SYNC_SHARED || + uc.args[0] == SYNC_PRIVATE, + "Unknown sync command '%ld'", uc.args[0]); + + for (i = 0; i < size; i += vm->page_size) { + size_t nr_bytes = min_t(size_t, vm->page_size, size - i); + uint8_t *hva = addr_gpa2hva(vm, gpa + i); + + /* In all cases, the host should observe the shared data. */ + memcmp_h(hva, gpa + i, uc.args[3], nr_bytes); + + /* For shared, write the new pattern to guest memory. */ + if (uc.args[0] == SYNC_SHARED) + memset(hva, uc.args[4], nr_bytes); + } + break; + } + case UCALL_DONE: + return NULL; + default: + TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd); + } + } +} + +static void test_mem_conversions(enum vm_mem_backing_src_type src_type, uint32_t nr_vcpus, + uint32_t nr_memslots) +{ + /* + * Allocate enough memory so that each vCPU's chunk of memory can be + * naturally aligned with respect to the size of the backing store. + */ + const size_t alignment = max_t(size_t, SZ_2M, get_backing_src_pagesz(src_type)); + const size_t per_cpu_size = align_up(PER_CPU_DATA_SIZE, alignment); + const size_t memfd_size = per_cpu_size * nr_vcpus; + const size_t slot_size = memfd_size / nr_memslots; + struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; + pthread_t threads[KVM_MAX_VCPUS]; + uint64_t memfd_flags; + struct kvm_vm *vm; + int memfd, i, r; + + const struct vm_shape shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_SW_PROTECTED_VM, + }; + + TEST_ASSERT(slot_size * nr_memslots == memfd_size, + "The memfd size (0x%lx) needs to be cleanly divisible by the number of memslots (%u)", + memfd_size, nr_memslots); + vm = __vm_create_with_vcpus(shape, nr_vcpus, 0, guest_code, vcpus); + + vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); + + if (backing_src_can_be_huge(src_type)) + memfd_flags = KVM_GUEST_MEMFD_ALLOW_HUGEPAGE; + else + memfd_flags = 0; + memfd = vm_create_guest_memfd(vm, memfd_size, memfd_flags); + + for (i = 0; i < nr_memslots; i++) + vm_mem_add(vm, src_type, BASE_DATA_GPA + slot_size * i, + BASE_DATA_SLOT + i, slot_size / vm->page_size, + KVM_MEM_PRIVATE, memfd, slot_size * i); + + for (i = 0; i < nr_vcpus; i++) { + uint64_t gpa = BASE_DATA_GPA + i * per_cpu_size; + + vcpu_args_set(vcpus[i], 1, gpa); + + /* + * Map only what is needed so that an out-of-bounds access + * results #PF => SHUTDOWN instead of data corruption. + */ + virt_map(vm, gpa, gpa, PER_CPU_DATA_SIZE / vm->page_size); + + pthread_create(&threads[i], NULL, __test_mem_conversions, vcpus[i]); + } + + WRITE_ONCE(run_vcpus, true); + + for (i = 0; i < nr_vcpus; i++) + pthread_join(threads[i], NULL); + + kvm_vm_free(vm); + + /* + * Allocate and free memory from the guest_memfd after closing the VM + * fd. The guest_memfd is gifted a reference to its owning VM, i.e. + * should prevent the VM from being fully destroyed until the last + * reference to the guest_memfd is also put. + */ + r = fallocate(memfd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, memfd_size); + TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("fallocate()", r)); + + r = fallocate(memfd, FALLOC_FL_KEEP_SIZE, 0, memfd_size); + TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("fallocate()", r)); +} + +static void usage(const char *cmd) +{ + puts(""); + printf("usage: %s [-h] [-m nr_memslots] [-s mem_type] [-n nr_vcpus]\n", cmd); + puts(""); + backing_src_help("-s"); + puts(""); + puts(" -n: specify the number of vcpus (default: 1)"); + puts(""); + puts(" -m: specify the number of memslots (default: 1)"); + puts(""); +} + +int main(int argc, char *argv[]) +{ + enum vm_mem_backing_src_type src_type = DEFAULT_VM_MEM_SRC; + uint32_t nr_memslots = 1; + uint32_t nr_vcpus = 1; + int opt; + + TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); + + while ((opt = getopt(argc, argv, "hm:s:n:")) != -1) { + switch (opt) { + case 's': + src_type = parse_backing_src_type(optarg); + break; + case 'n': + nr_vcpus = atoi_positive("nr_vcpus", optarg); + break; + case 'm': + nr_memslots = atoi_positive("nr_memslots", optarg); + break; + case 'h': + default: + usage(argv[0]); + exit(0); + } + } + + test_mem_conversions(src_type, nr_vcpus, nr_memslots); + + return 0; +} From patchwork Fri Oct 27 18:22:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438958 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 77B77C25B48 for ; Fri, 27 Oct 2023 18:23:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C8C180027; Fri, 27 Oct 2023 14:23:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 252A580018; Fri, 27 Oct 2023 14:23:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8F2A80027; Fri, 27 Oct 2023 14:23:31 -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 D303E80018 for ; Fri, 27 Oct 2023 14:23:31 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A7281B60AA for ; Fri, 27 Oct 2023 18:23:31 +0000 (UTC) X-FDA: 81392064222.08.AC741E4 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf15.hostedemail.com (Postfix) with ESMTP id DE60FA0006 for ; Fri, 27 Oct 2023 18:23:29 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=aJXekSVK; spf=pass (imf15.hostedemail.com: domain of 3IQA8ZQYKCEIwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3IQA8ZQYKCEIwierngksskpi.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=1698431009; 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=rHCSEdVT91A5ySy8HWpIrikDyrYgztwmDmnD7EhGGWM=; b=wX6uUKSWsOq9Dg8cF3XZIVHFSYoZCTZTKq6518Qoa3DXtyiAVaeoP5b8FEXNPeGgpK+yAL no1ohSNztRj5FpG49rK1Um+ISaTWyz3/HQdQAPUXBrQjfzEXsWM1/nP34nDD2FANV+60PJ aNO2UwFIbTSAo6+pIglTxgEcTfl7z0M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431009; a=rsa-sha256; cv=none; b=jFex/q3ydMY3ZWvkbEXwEObd/cK5EVBXc6N3ODZxWTkwYxpCNzFJv7PwDllhpC+orZ9aWr BsAFGqrPa997lzXcgXCGWdtL1/0yotcqRE3B1AVY5DIx61+w9FDrP72ow+dQ0hkGq+DDAq pZBZ9rXX9Y7S/mECqjExTaKn6HQ6zok= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=aJXekSVK; spf=pass (imf15.hostedemail.com: domain of 3IQA8ZQYKCEIwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3IQA8ZQYKCEIwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d9bc6447193so1955419276.3 for ; Fri, 27 Oct 2023 11:23:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431009; x=1699035809; 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=rHCSEdVT91A5ySy8HWpIrikDyrYgztwmDmnD7EhGGWM=; b=aJXekSVKXAVk3OqEJyEQ+QV5MktM2tPRi3Wusg3mGJ0XGyDdd3ykOT7r1O1i35TUeS VzI8E+zzvuB9xjQ/mN8r8huKHHC80FLc+XUs5lLRczgcQEJ4V11JADsbxlg+hGXUKZR0 ax8XBMmVvdonPRac6n7lJAEch+2M+tSiAO2TVp719n18hze6VKHXxvJV8CYqD12LmDm/ MDq03IoQE3nBXt99Ll8gox4UQmvPot29wmaqKwc4UqvKt1D1E46dwqcJRYBu2pp335Fq ZNCpKeh9gMU5wVc7+8LNFB4MJxyj8ElNTkLjnkHFSFJz39KGkrR6XscDiqlysmy15arA /GRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431009; x=1699035809; 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=rHCSEdVT91A5ySy8HWpIrikDyrYgztwmDmnD7EhGGWM=; b=Dn2q2eG9dsfxdJr991tsl2C8E+5R+RT2e9tmTPItsLEEZWoTxM7jyXT/0QUzo8JOxj Qox2SgNti8lx9065yBgjc902twQnaQjJNy3XVpOBnWlpFmnNoLQ7zoWkHoDPRItkYR/i MQ1/lVHu/TB8Y0/wxF+js6aU5Rzd+YAOe6QBlvi6+dnIU65WWNiGqSENVPR56WKBqkxI YE+May0aD5lSOMuJmO27zB2cQvCbDtNqUJ8suiTpq+bj9gSALNHBqYjObQ16y0JXGq1H +JkT68MO7Bbux57MDumExZjnPdFrtyBWNlp0esJ5fhbVCTTJqaJrLiwcLh41q45tGQDZ rwEw== X-Gm-Message-State: AOJu0YwSo1IpSTIJwKIuFQFGbeP/bjKS7PFKtu38MK+tELgixNs8hi6n l+e/OxoBVUCtjlTK12PYnUAX5bh3KE8= X-Google-Smtp-Source: AGHT+IGBlPG7wn0887XfDb8cENXXLGU58G9yJP5jBeCkKIucbC9oYDwWXkdT4FgaevehYCQITd+4fshLciA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:565:b0:da0:c979:fd70 with SMTP id a5-20020a056902056500b00da0c979fd70mr69913ybt.9.1698431009012; Fri, 27 Oct 2023 11:23:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:14 -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-33-seanjc@google.com> Subject: [PATCH v13 32/35] KVM: selftests: Add KVM_SET_USER_MEMORY_REGION2 helper 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: ox4jknmp11mk54napiwoke17whpdodqw X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: DE60FA0006 X-Rspam-User: X-HE-Tag: 1698431009-123713 X-HE-Meta: U2FsdGVkX18EVvfR9XUlrR2jS9yvPmVXFAj3YrFrCg7cKSeY7hyIYwAYCCS8rTN06hFDnbcG2+je5BpPRdkCqavLw2U6lOBUTcCs3XN6v8psYlIhc2D6YO32klpUNi+Do5LhljHQ6BxNeLgAmMGhAfSVomIFdH3u/Jg5lvAD+FWOc8+ojmGLNVi6w2dk25f9tqpSizykGFMhUZfPmmcbtibHzy4Wwe44PWTcfZWijLEkwXa0QwNSDO//+RtoqahcrJRUTeJe5hnb1QZD+1gN4GOrcS1wB9bA+fEpAEG52+jYx0F1DGO7xG/5khK7KPip0ZAJacPGASUWCPDqY3+bDnQSXpCTk/Ufo4fdJXbsDSjSe5rkuIWYllFdXoA4kKWEB+gESb1cVEYex0NBsQ/zrKzhp9VI0kZSGz6GUGuYQsQHhKxiU/9snblYwXyLG5UI2pWJ8PFzAEMMQi895DxzWho/8k7ZUvxe955PLco+daRdUw31t53xMD6AHZn+inrMJe7q+p8qQx35oZuuYody3CiSQl4RsLZzvI6qjurmjh+yPZ1/lH8FvrDawP2nliKvwH39ga0WdYRfyfuALilcGZO0a0Kt/HlQSQNAKWjW3gEF2Kx7l6sZo9+b82KwoRfXKl0QjdIpAcqlRzA8NuzoVEyKqWkCpj2aWoFlNcx3U7FjSvQ7bNVhzKJxOJEqZcXmg94fEJ21O5CuufLcLWcB1GsQZ+ueLtDM76hzFQ/Oo4K7WL1MXeh8QtBLkQLCBxYMVqqQnK9u7ZMsFwAkTItJRczz6tGceOPtGAyww2PHd2/vY9TqBG8w4uaTQglKjrjOJ5eScoUwUULQ5gLZ8BJX40KqHyUc5FXQx8MY3lz17UYKCSz1DrRJ95kQySoLetQKfUgmJRhEvq2kzwMG9pXI++VIWDjuUueuZ7iixABGN7A7ePoMvjZX8Umc5/mYlT8IzDJSpWldcCBr7i5cyiN 91lKXnCg 4Z4LsUswkjNiuRqFVrfqBBBakruT/QL270gg7iv7TY79QFT17RFblYgSxnzFAE6VQrZ9OlIiaIuuzIoP6H9EaRX/TAUB6mhSI8m6Fber3K0JjM/iQidZV9WkCtyEsVvgOyRNPHyUDH23ixlFcvP77vSAw3bdywuKczDUTzLOi+p22JkitM3zOjMWnB+zxaHaFSBPgh7z1LUOXv6M57SLgcsPcDPFCoCqe/3+GILdxCAweH9UAp/sfnCNaNgmkF6zqrkDumX7DWMipESa+ENT8ClQvWaRv4wwNp/dA0WwK4nOyQAMhyWA8mLW/I2ndf1wl3rd5wclXfgRN6/n5+BD90pwBaNPEc/8MGTdcTVUoQQR5zm0DjmEfiImqJ1s3IW37DXrIe+MQpMYR3JZVHwTPInLHlJiVFj6pavEF3AANULWH7ekprUuimRhaWVmGu/JAcGJAhHUcGkjg0Oo8uiWQN8b0aPhv+54hiBY7vyNNDUoLxi3hsB00OJf2nZdCvehj1R3++5PxSUOBhSzcTyTu7IC1k2t5DE84VuFz4vGrA5MEiqKGXbEvppu7tIdZyQr3e+XxTZQkKdsCbavpK57p0n4YQ+cI6hg84bK6OJOoB0mIAuq4RF+LzEBxFa0GA314jTPtjUGz49wdp5Jl1CdturNsbdJd0u4Kmz9YEDPE+JJt7dpBs8QDnkxlPfVXiqsddI7xX/Dvw4fO7Xc8U/nJL61In+/lN4sDmB3Ob1i925/+rRbJvNPqD2psfOih2nTbHQ+6SPYza78SVHEzEoloTxxxj8EJfqtZvmSyOyr782a705VmEGKj7nr2BYNAhy60RbCz+9wgA2WbpJTjJgcKl6bwiA== 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: From: Chao Peng Add helpers to invoke KVM_SET_USER_MEMORY_REGION2 directly so that tests can validate of features that are unique to "version 2" of "set user memory region", e.g. do negative testing on gmem_fd and gmem_offset. Provide a raw version as well as an assert-success version to reduce the amount of boilerplate code need for basic usage. Signed-off-by: Chao Peng Signed-off-by: Ackerley Tng Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 7 +++++ tools/testing/selftests/kvm/lib/kvm_util.c | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 157508c071f3..8ec122f5fcc8 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -522,6 +522,13 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, uint64_t gpa, uint64_t size, void *hva); int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, uint64_t gpa, uint64_t size, void *hva); +void vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags, + uint64_t gpa, uint64_t size, void *hva, + uint32_t guest_memfd, uint64_t guest_memfd_offset); +int __vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags, + uint64_t gpa, uint64_t size, void *hva, + uint32_t guest_memfd, uint64_t guest_memfd_offset); + void vm_userspace_mem_region_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 52b131e3aca5..1620452c1cf7 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -873,6 +873,35 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, errno, strerror(errno)); } +int __vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags, + uint64_t gpa, uint64_t size, void *hva, + uint32_t guest_memfd, uint64_t guest_memfd_offset) +{ + struct kvm_userspace_memory_region2 region = { + .slot = slot, + .flags = flags, + .guest_phys_addr = gpa, + .memory_size = size, + .userspace_addr = (uintptr_t)hva, + .guest_memfd = guest_memfd, + .guest_memfd_offset = guest_memfd_offset, + }; + + return ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION2, ®ion); +} + +void vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags, + uint64_t gpa, uint64_t size, void *hva, + uint32_t guest_memfd, uint64_t guest_memfd_offset) +{ + int ret = __vm_set_user_memory_region2(vm, slot, flags, gpa, size, hva, + guest_memfd, guest_memfd_offset); + + TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION2 failed, errno = %d (%s)", + errno, strerror(errno)); +} + + /* FIXME: This thing needs to be ripped apart and rewritten. */ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, From patchwork Fri Oct 27 18:22:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438959 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 0A8A2C25B6F for ; Fri, 27 Oct 2023 18:23:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75F8A80028; Fri, 27 Oct 2023 14:23:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 66BF480018; Fri, 27 Oct 2023 14:23:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4225A80028; Fri, 27 Oct 2023 14:23:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 2D5DE80018 for ; Fri, 27 Oct 2023 14:23:34 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0B4E1160E65 for ; Fri, 27 Oct 2023 18:23:34 +0000 (UTC) X-FDA: 81392064348.14.910F66E Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf14.hostedemail.com (Postfix) with ESMTP id 3854A100002 for ; Fri, 27 Oct 2023 18:23:32 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=EgdFmSeP; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3IwA8ZQYKCEQykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3IwA8ZQYKCEQykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698431012; 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=sbbTeqIjFnESn/3c//KYIg/yBP25ARIIaxIo9NdR+Rs=; b=PC4L1RMgOXqgudvm7/MSEXBvJxjcIBeeZHfyWrLKTdh5JjjftPRP3WOWZ1Lub58Wwj+7on 9Tuelx9nisejn4OEd9uUBBWdkpyJdaeQNL5dII0ShyemmKwLmDMXjYRQZKrz2aLsSsJ6zE sety5wFV2vsHDzdgYUWumod8xj1vspA= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=EgdFmSeP; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3IwA8ZQYKCEQykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3IwA8ZQYKCEQykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431012; a=rsa-sha256; cv=none; b=CCh/j53o+aDWNEKIwKvzGaqvMzuL87hTD7/OCVn6nin+BSjlhYEYUYhd2+Yl7bhQAzgvAY vZIZ+s3FOY2sYZiBuReQ/0h6gjJhHqya4X3XOzLiUEWI0uI79gg5+DEewsFLuSNk/xHAuD GkvTGiFrWC62kxN8bfO04BgQ9tdVmZg= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59b5a586da6so22568757b3.1 for ; Fri, 27 Oct 2023 11:23:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431011; x=1699035811; 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=sbbTeqIjFnESn/3c//KYIg/yBP25ARIIaxIo9NdR+Rs=; b=EgdFmSePKxM3utYfZ6JbYNr+coZJIsS0zwgA1xo4B0kSF18OLfiL+kayxpbXw91SaK px07zOcqWDwGK/W5bbNe+1XdUVR78Gu3hBtmjxujarX1kUZGP9cAUrs6giVr7q48Gpdc CMumzEs5ATaZhDUz0Zu0qe56I0USUcKh9k5W8LqxsQ/YsUx/GoXommuf/OzVcA5nS1vr iu83gH2aiO53wMYqugLMrbiJ9b2ej6a4Orhs4hYuU83zLKpxjsQBpI6i5Ue4JhSLMN7p A7XU5y8YUw0R4J5SEJK2tP+gaWTPIp+oVCt5dNVstoLo+oVW8h2udTIv8rxl1XEvWhXx orEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431011; x=1699035811; 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=sbbTeqIjFnESn/3c//KYIg/yBP25ARIIaxIo9NdR+Rs=; b=ER4O9l9LdU4Kwgglq89smbz09ksy3qybdYH4pGrE6U5+o0ifbiQszh4DdU6qsc1d/a M53+qIf4ahvMkH0ZwNmZadeWvH1e35HtqtvEzVPaSKB4cdCaTuYH2ade2Mu2u65/VqzP r2luFgWr6j1xJN5e8kDdGFwj3y5bwxN1LWKMVhwchyUPf4JEyMqpqea36PwEO95nnya/ Q2wtl/8eVhVAFFq3bKVnN09fFMo5+/caf+tKtRMy+kiyEvfXIZrj9F4ONYLnmCVrVM91 uAUCk/rym8pkYu2oqepFMDMOvPWRQ8+dupopOczZJSb7a3T31XXpDJzVrItJoHVeIfZJ yw6g== X-Gm-Message-State: AOJu0Yx+BmbDPqP8kv4m0dg3Svfs2ylgtgINUTIR84/pjWNhw8KTDThs 0xmtICsfRWsoNxfF549F+co8C7+MY1o= X-Google-Smtp-Source: AGHT+IHBWIjPxzRJgKiud97ZCtykjvc4+klLDW+Sn0Y068ZaDLScSJEVBpBXvLWKn1zDVFylTXJJKJ/wubw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:ca0f:0:b0:59b:e663:23b0 with SMTP id m15-20020a0dca0f000000b0059be66323b0mr102894ywd.4.1698431011148; Fri, 27 Oct 2023 11:23:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:15 -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-34-seanjc@google.com> Subject: [PATCH v13 33/35] KVM: selftests: Expand set_memory_region_test to validate guest_memfd() 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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 3854A100002 X-Stat-Signature: q9nj1hq741s5jh98spscszi7dyo5s4w8 X-Rspam-User: X-HE-Tag: 1698431012-372209 X-HE-Meta: U2FsdGVkX1+OCAbauSfl/Jy8I9XwS0dt9vIT2pE6l8SMZ6yw+AUj4HrfZidz9iUrtt6eZhV6ErSSe2ChH+Yo092u3q3hr3uJVEC52vGjQ7P8n6J/HO6jzk/X7uYzlSHNEeiz7PzG3Rp2BFDn1PHyK6gcqwSH1TgqecHANc6VEWoB7zKizzVNCeERvsfWT81+57OJiFk+ErZ309zvrZddbnbMCNgmalVGLLF1GbD6+AV45Sh3iL+2QGLeq5mqvQcbMNIUIVmY/Jf+XJFs74WRQOBv+WwbLNOtW306/fkZb6A/J7Zzt2rwsVN5q2R8adMehgYoby4Sf1DaHDYA/lvQP9/NtvcDtzgTrFbE71HdVYGXqmKj+oDf+TqIXOpFRX7e/LyZ7CpKTxJUE0PMT2lwH9qiQfdBu+PXFZtprbYMQBwlMlYT8jpLpj2MSb+UndPAHeU+9zizY23/qwhdXG2uxG4XsSOunfs2c0nZ0X+8FiT979cEdbZujWnuZERl6sSNuxYqjGp4QfaV87ikhwD/LGLjlnXSdij5SAC1gOmLS3r00SYOnQBfur0zmiJWFIrSZ9rH0KGUI1aikZd2E8pbEhJQ846FCHBRrKHxaHYiweAdJAUttvcXTOE1DYoTlIww46iAwTUGTv/Z9YED1fSxLRLZAwDFtld1nmZaFFfSMCx2Xq1KkRnGenpgh83wlUbsQbisksvqvk2PNu2Phxt1PhRJqDfo4MnUFCGSMgmGp0gWTW2NKBnq6z54LQ+6uB/xBYfxYpswPJnnOorEfkljP1XmgH9drS/wif4Al//TYmwb5g8qo5TL6ZpZfMiM2Q3NzR2IPrpDWeW2zAtsXNFErkFzJCc1+A3VpdFtr2pNyCfBTAI+oi/UH6Tj5F+UzuYuY4mWRMaO63qulmMweplZjXsNvzFhV16UpigqpvIgiL/9dLK2eoIjjHf1/LCMDzrS5r4k7KfVW6o9EJAGy68 fYmpAULF 8YKGORDtFT0da8LOcQjyimEXyojHC9SEvjdiwxuBtDqfGGp0qbcdS6WMzUjba2TXp4TtbrKf6tMtWnFJVlpr1LQAWQqXco8RCZKnD/jiCL4neRgs21xuCbYwryzHKsuMkUXVEhvPD/FoivmrwW4EmqD0BbPCFueu/wWKykYiyZRnJ7ytnhSRKRdiKcdTOI65whj0s2fmhOg3KZtLlUdf2QAKU8F+B88l2cP89k8wkMQtjSWdt4obellblCBXUkEx0tvbX9fIyqTFpbwnLAkilTPWPmpKMUNyWQv6RLb/OS8eeYWwgfW2vSYrylXl7MH7J5Blnw+77EtTjZNaFYWvweFXuj+r/FhANvMgRmWpKpkF/6FDKOEmqJC6MoSvp+2m+ZXQQO5EODdAEJI4KbeuPg8vV0SWS6pnhxDSo0rzVTQY+6t555Y15J0JCl6dsbvCCIavJcMgloPk10VmtbXoTtJ7h9VG+mWH70OtRcx9Q8WArD8h8aQdxrtvY8F7EUdoKJSn6qHX2B+sa6sWjRL47HeyD/zDbWw7V6NEmiZkorHi4iNMrOYoYTFq+U151I9+kmrUg00g2u/tHn8q/iME4dLyhBH75b5LI7CQFs2ovViRiu+QAsNovJ9VX81bBlYQkFHG07oAg+H9+fKBvZgNoVuDBeSLFEOrqOdzFjWSvCbC9I7+SFkfshbV+6aXy1NZW7K+Wu4bgjbOA7Ppt2EtHmP7y7pGOCIqjueym+szBSCm2p758YBP6Fxw/UleXG9pGMl/G1keD6M5s23N0607BvhqiTAT3k2Ws2z4FSqvxfDsES/8En/20tcRdONew6HfY5DWzh4LWjfkE0pU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000007, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chao Peng Expand set_memory_region_test to exercise various positive and negative testcases for private memory. - Non-guest_memfd() file descriptor for private memory - guest_memfd() from different VM - Overlapping bindings - Unaligned bindings Signed-off-by: Chao Peng Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng [sean: trim the testcases to remove duplicate coverage] Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 10 ++ .../selftests/kvm/set_memory_region_test.c | 100 ++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 8ec122f5fcc8..e4d2cd9218b2 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -819,6 +819,16 @@ static inline struct kvm_vm *vm_create_barebones(void) return ____vm_create(VM_SHAPE_DEFAULT); } +static inline struct kvm_vm *vm_create_barebones_protected_vm(void) +{ + const struct vm_shape shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_SW_PROTECTED_VM, + }; + + return ____vm_create(shape); +} + static inline struct kvm_vm *vm_create(uint32_t nr_runnable_vcpus) { return __vm_create(VM_SHAPE_DEFAULT, nr_runnable_vcpus, 0); diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index b32960189f5f..ca83e3307a98 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -385,6 +385,98 @@ static void test_add_max_memory_regions(void) kvm_vm_free(vm); } + +static void test_invalid_guest_memfd(struct kvm_vm *vm, int memfd, + size_t offset, const char *msg) +{ + int r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE, + 0, memfd, offset); + TEST_ASSERT(r == -1 && errno == EINVAL, "%s", msg); +} + +static void test_add_private_memory_region(void) +{ + struct kvm_vm *vm, *vm2; + int memfd, i; + + pr_info("Testing ADD of KVM_MEM_PRIVATE memory regions\n"); + + vm = vm_create_barebones_protected_vm(); + + test_invalid_guest_memfd(vm, vm->kvm_fd, 0, "KVM fd should fail"); + test_invalid_guest_memfd(vm, vm->fd, 0, "VM's fd should fail"); + + memfd = kvm_memfd_alloc(MEM_REGION_SIZE, false); + test_invalid_guest_memfd(vm, memfd, 0, "Regular memfd() should fail"); + close(memfd); + + vm2 = vm_create_barebones_protected_vm(); + memfd = vm_create_guest_memfd(vm2, MEM_REGION_SIZE, 0); + test_invalid_guest_memfd(vm, memfd, 0, "Other VM's guest_memfd() should fail"); + + vm_set_user_memory_region2(vm2, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0); + close(memfd); + kvm_vm_free(vm2); + + memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE, 0); + for (i = 1; i < PAGE_SIZE; i++) + test_invalid_guest_memfd(vm, memfd, i, "Unaligned offset should fail"); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE, 0, memfd, 0); + close(memfd); + + kvm_vm_free(vm); +} + +static void test_add_overlapping_private_memory_regions(void) +{ + struct kvm_vm *vm; + int memfd; + int r; + + pr_info("Testing ADD of overlapping KVM_MEM_PRIVATE memory regions\n"); + + vm = vm_create_barebones_protected_vm(); + + memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE * 4, 0); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, MEM_REGION_SIZE * 2, 0, memfd, 0); + + vm_set_user_memory_region2(vm, MEM_REGION_SLOT + 1, KVM_MEM_PRIVATE, + MEM_REGION_GPA * 2, MEM_REGION_SIZE * 2, + 0, memfd, MEM_REGION_SIZE * 2); + + /* + * Delete the first memslot, and then attempt to recreate it except + * with a "bad" offset that results in overlap in the guest_memfd(). + */ + vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA, 0, NULL, -1, 0); + + /* Overlap the front half of the other slot. */ + r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA * 2 - MEM_REGION_SIZE, + MEM_REGION_SIZE * 2, + 0, memfd, 0); + TEST_ASSERT(r == -1 && errno == EEXIST, "%s", + "Overlapping guest_memfd() bindings should fail with EEXIST"); + + /* And now the back half of the other slot. */ + r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_PRIVATE, + MEM_REGION_GPA * 2 + MEM_REGION_SIZE, + MEM_REGION_SIZE * 2, + 0, memfd, 0); + TEST_ASSERT(r == -1 && errno == EEXIST, "%s", + "Overlapping guest_memfd() bindings should fail with EEXIST"); + + close(memfd); + kvm_vm_free(vm); +} + int main(int argc, char *argv[]) { #ifdef __x86_64__ @@ -401,6 +493,14 @@ int main(int argc, char *argv[]) test_add_max_memory_regions(); + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD) && + (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) { + test_add_private_memory_region(); + test_add_overlapping_private_memory_regions(); + } else { + pr_info("Skipping tests for KVM_MEM_PRIVATE memory regions\n"); + } + #ifdef __x86_64__ if (argc > 1) loops = atoi_positive("Number of iterations", argv[1]); From patchwork Fri Oct 27 18:22:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438960 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 8B243C25B70 for ; Fri, 27 Oct 2023 18:23:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E02B80029; Fri, 27 Oct 2023 14:23:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 891F280018; Fri, 27 Oct 2023 14:23:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 696D780029; Fri, 27 Oct 2023 14:23:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 51F8880018 for ; Fri, 27 Oct 2023 14:23:36 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2F617A0749 for ; Fri, 27 Oct 2023 18:23:36 +0000 (UTC) X-FDA: 81392064432.22.587B084 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 619BC100019 for ; Fri, 27 Oct 2023 18:23:34 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3okaymEj; spf=pass (imf14.hostedemail.com: domain of 3JQA8ZQYKCEY0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3JQA8ZQYKCEY0mivrkowwotm.kwutqv25-uus3iks.wzo@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=1698431014; 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=Gf3qZUfKxr1l5xs4a4PCSuBn/K/kT1FM/O66ERGfDAo=; b=hAt6x1Gs5HrWikK6d/jnQDoqjNYRLBH9wHWMTQihHCuvBq8p+1TjsH/HMfwxv+/3mTWDmm 7wsoaBKAW3pxffskSkb+lc9Q6ZRMICm/GwmctS/k1FpePOb81C8GyyY/lxCPMkmu+y0weT PHiRnzzNON6bRFHeNGZh10Z8XJJ4Taw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431014; a=rsa-sha256; cv=none; b=S7w1rz+M41NZsnSYq0bTPFhu8Mqz/PrN/QWdmqXPvAfV6wcz9Zkp3sd1GrJkSNqGAAwEkn EchLMIDDCV3KEyUqF72STA/8YkteaGVa4HdTsAZ9aEGCpUFgjnb2dUvTOMv6VP8ko/0CRX VD1oGMNxXlNf4dptGEO38iQx56oQ+k0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3okaymEj; spf=pass (imf14.hostedemail.com: domain of 3JQA8ZQYKCEY0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3JQA8ZQYKCEY0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-da0631f977bso1909293276.2 for ; Fri, 27 Oct 2023 11:23:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431013; x=1699035813; 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=Gf3qZUfKxr1l5xs4a4PCSuBn/K/kT1FM/O66ERGfDAo=; b=3okaymEjoP8D37VMkv9pIDuv05UiXNOAsngo5I3SlcP9++HIIIcSMWIyp9EJe6ITlC YdcmQIfb3MzP+25r9m+JAIpmkIa6ZSNe7MO0tT0/qD7MvosS8eb1DiJfdC8yACiggKXc P/xfdji1S5K6xTJVOTgszDxYHj/jW2uHKCDrJaAxKKHnnm6l5rWh0wKIn8v1/mcO2bpo VjAEuumgPLiQCF+J7OTfebUagfQf71iKyAgmivS9bu5Hsks3+j6/0KTQeOBN05QO7NkM X/r7ik4auYpo6qrWCb4KMHEK3gG4jLg+PDxCBcfadE5kUGaanUPtcqJz10WTEDqltPQA hNog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431013; x=1699035813; 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=Gf3qZUfKxr1l5xs4a4PCSuBn/K/kT1FM/O66ERGfDAo=; b=nOHDOSP3nCPRBtcYOM8vbCWwnL4Veh7chA8/47DD7RvjZgD7SIPzNPk7E+rBqHGTqz UUmZmeCZbWBxgqfgcix3GR8pYEy8jFnluTI0Lb5G2Wo4qK0iwJBb0SXfbp1mL1bUMd0v FznDP5qC9BoMNfAddcUL8W0kVwQRqqEOiSiew/SPsAjIwfvteODCrXZi9JqW/8W0XiE3 GFZPhm44FbDWu2+iXaQ66nZmaKEqrAPt3MXEND9JGysAvZMexroNQeYyQWkFFDnHK9HP JqRnmZ9L1EX8wo2/1/aozrnTEFTw6oF295K4EP7SchT7kfovzYgw/iL0vaYNTycP6w0b NWNQ== X-Gm-Message-State: AOJu0Yx/3RC4ZGI0YMbdoTDvOBl566WsdF4ZCNvcQ0+yJqF84Ghyqt9D 0ZrXlTimmfmlQ5O2p9VJ4T0T9e6mvrw= X-Google-Smtp-Source: AGHT+IFvLwrndU5RN7JLgZaUSfmAls7MFUJfuK8Cz4IhpSWC48U7qmoJK4uE/MrHKdWeSCjIUJubQh1tXXM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:aa47:0:b0:d9a:d272:ee58 with SMTP id s65-20020a25aa47000000b00d9ad272ee58mr68414ybi.9.1698431013458; Fri, 27 Oct 2023 11:23:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:16 -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-35-seanjc@google.com> Subject: [PATCH v13 34/35] KVM: selftests: Add basic selftest for guest_memfd() 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-Rspamd-Queue-Id: 619BC100019 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: edbb4x3exgph4hrxfhki3wrrzky9t5ty X-HE-Tag: 1698431014-304913 X-HE-Meta: U2FsdGVkX1/LcYR6Uw11QRgPW5r7t1jWf50dXYoZ3Lk7keTxnljkIc8t/cz6jXMLUrKWyUDFk+Fypmvs5Imqz5WkzPpzeBJgB1h1ySFti7SnbCUhaAmbrpddAqLSzKhoWmMcQgCSASGwCbydrJmXSljThmRSsB758iMSs5rr2oQ+scf8SjDsHEfYFPC4oNCVvEplcAfAKUNajbk8mCWMMYebO/BP6uVRkd7+vuuXeGxwe0tv8UTUsiyiQ9btutBuypRwi7lFiriCrHhh9qTHx/eV07jJnXPehhj9ceS8QAYsV37g8L6Ff8NLbpX9cRKIrwR/xqndG4NWk0ynbzfYdV76lRY9ZCu66/enH4g+8NQSy4KPh76ml5aSRU/+UuEJUjdXBaFM49a/mCqpzjvdScxIuyJ1QRj363j/zyPJHS+r5Mnr5VqwOTetbXc2bqANLJfwLOmu6+Vj71F1sLmWPk1FdAWTMjy5T5js5fSDdqPzeQVNzXrRIX2b/a81wG7QZJpy4jn9FK7SMN+TCK5UsSQCL7MhRj41gCaqzP7PKx1e2r3TKj3y4zZVYIOh70lfx9fLsb34A0XeU6k5EJKfuMoyeJ+BAgBEM8axx5Vpql1rgYTCxgMJM4DyhwGhCltMc30FPSdUVq7qhZo8iz45ih9gWWryqAMsV4UT6nTLx+4gx5wuaxH4+eJpIU4O98T5nb/sZ6t6HN+M+klecuUvd1KfCnz0NDz9I6sbR/tsI+SjhBB0pX5aRSF12lqUS2reTrLTvX+n0oPBb8OoHIqTGRGcAN6Rgq6cfsN/xuIcZ0ZhZOKkj3AgH8wmoV+4rywNqRlUvvUtV5veB5724l/8e3Uh/vdZ19NkfZrCyGPjQTmtqGqh+Iv71XIyybKho34jerncfe5m738991fJ0vmKDdHGxs3zLLn0mjCxzZt998l9XaizbNjecI12xjKtR8CLah59CXCwwZFmsJuApPh RYZSuMko Ez8QArlK526Y29XN7Ye6JpW98gkNNq5jQdyqjr/+q9dlx+R84tR3kucGQYyaX+qkSMzHdjJeQAij0whDVGVUZTqQP6pOYBju2qDPs4UVYuEG7zWA+X06Z4rPDizWo0BlE7cJ0QfW7kFLSFFPfuLqcNlQdhh8IGtBDu2T0QcpqCQJs6c6wtsCI70iy9hPfQx4oc5cYbVtJL0IKYCb9rSgccIsVrPwNoIsTJ7N+9f1LlJibdzF8NeQgUG4cIvHeggz3J6jhRMQALbPXPqyDVRzrv1AoMGQv0OoNxmk9P8ZBmKrM0lC93rLZ6TUeg9a539koLjc8FcUx/TPH4DL6vW/+f90xhfApbFvTdVwNRx/ID7Khget3AIUqT23lfjEeoSUrp+tiTO6+toLeX/y6UCY9fvu0s6YuVUs3O8Kzn7csPzWioXy9hvnIvOz6H6g2GvH9DeLqyD6zbAqLeXr5dJv5dWgFQXL6zYYW0ieDaszCOno95A/Pg1A1as4iB0TNdDFQNz18DqqNoDp/DEfERP+5RODq6+UMPcoZNKi5ShpqfI9ZOmsjShXWkzS3f/2RullBJjEwgpUAkJmR8+/jPU2cs+K/zqBwyx2e4gSmKKFirp3Tx4j2NdkxP8ShyX5XMbgFeWCAYM0qjCdIjT/YXuP2LUKoulqGrEAncbSeWnwXPuS06XCGqrheVyQTXGD1EDZFn2EOE71lpjIvmJ9cBSeEGo9UG3b5iIR9o17S4iQR+q64Ks6Y550WYoZASayiWbIrWuiciA4PmF/CGg71s1CUHyaoCk0UHX6hl3w/qrNF7dXN2YogPNSqqN1Deu/FBzbWsrT15MoyoviunMnyK1nZHS8uHpch6oMO07AHv1JDieC0GN8= 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: From: Chao Peng Add a selftest to verify the basic functionality of guest_memfd(): + file descriptor created with the guest_memfd() ioctl does not allow read/write/mmap operations + file size and block size as returned from fstat are as expected + fallocate on the fd checks that offset/length on fallocate(FALLOC_FL_PUNCH_HOLE) should be page aligned + invalid inputs (misaligned size, invalid flags) are rejected + file size and inode are unique (the innocuous-sounding anon_inode_getfile() backs all files with a single inode...) Signed-off-by: Chao Peng Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Co-developed-by: Paolo Bonzini Signed-off-by: Paolo Bonzini Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 221 ++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 tools/testing/selftests/kvm/guest_memfd_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index b709a52d5cdb..2b1ef809d73a 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -124,6 +124,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += dirty_log_perf_test +TEST_GEN_PROGS_x86_64 += guest_memfd_test TEST_GEN_PROGS_x86_64 += guest_print_test TEST_GEN_PROGS_x86_64 += hardware_disable_test TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c new file mode 100644 index 000000000000..c15de9852316 --- /dev/null +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -0,0 +1,221 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright Intel Corporation, 2023 + * + * Author: Chao Peng + */ + +#define _GNU_SOURCE +#include "test_util.h" +#include "kvm_util_base.h" +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +static void test_file_read_write(int fd) +{ + char buf[64]; + + TEST_ASSERT(read(fd, buf, sizeof(buf)) < 0, + "read on a guest_mem fd should fail"); + TEST_ASSERT(write(fd, buf, sizeof(buf)) < 0, + "write on a guest_mem fd should fail"); + TEST_ASSERT(pread(fd, buf, sizeof(buf), 0) < 0, + "pread on a guest_mem fd should fail"); + TEST_ASSERT(pwrite(fd, buf, sizeof(buf), 0) < 0, + "pwrite on a guest_mem fd should fail"); +} + +static void test_mmap(int fd, size_t page_size) +{ + char *mem; + + mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT_EQ(mem, MAP_FAILED); +} + +static void test_file_size(int fd, size_t page_size, size_t total_size) +{ + struct stat sb; + int ret; + + ret = fstat(fd, &sb); + TEST_ASSERT(!ret, "fstat should succeed"); + TEST_ASSERT_EQ(sb.st_size, total_size); + TEST_ASSERT_EQ(sb.st_blksize, page_size); +} + +static void test_fallocate(int fd, size_t page_size, size_t total_size) +{ + int ret; + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, total_size); + TEST_ASSERT(!ret, "fallocate with aligned offset and size should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + page_size - 1, page_size); + TEST_ASSERT(ret, "fallocate with unaligned offset should fail"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size, page_size); + TEST_ASSERT(ret, "fallocate beginning at total_size should fail"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, total_size + page_size, page_size); + TEST_ASSERT(ret, "fallocate beginning after total_size should fail"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + total_size, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) at total_size should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + total_size + page_size, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) after total_size should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + page_size, page_size - 1); + TEST_ASSERT(ret, "fallocate with unaligned size should fail"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + page_size, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) with aligned offset and size should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, page_size, page_size); + TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed"); +} + +static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size) +{ + struct { + off_t offset; + off_t len; + } testcases[] = { + {0, 1}, + {0, page_size - 1}, + {0, page_size + 1}, + + {1, 1}, + {1, page_size - 1}, + {1, page_size}, + {1, page_size + 1}, + + {page_size, 1}, + {page_size, page_size - 1}, + {page_size, page_size + 1}, + }; + int ret, i; + + for (i = 0; i < ARRAY_SIZE(testcases); i++) { + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + testcases[i].offset, testcases[i].len); + TEST_ASSERT(ret == -1 && errno == EINVAL, + "PUNCH_HOLE with !PAGE_SIZE offset (%lx) and/or length (%lx) should fail", + testcases[i].offset, testcases[i].len); + } +} + +static void test_create_guest_memfd_invalid(struct kvm_vm *vm) +{ + uint64_t valid_flags = 0; + size_t page_size = getpagesize(); + uint64_t flag; + size_t size; + int fd; + + for (size = 1; size < page_size; size++) { + fd = __vm_create_guest_memfd(vm, size, 0); + TEST_ASSERT(fd == -1 && errno == EINVAL, + "guest_memfd() with non-page-aligned page size '0x%lx' should fail with EINVAL", + size); + } + + if (thp_configured()) { + for (size = page_size * 2; size < get_trans_hugepagesz(); size += page_size) { + fd = __vm_create_guest_memfd(vm, size, KVM_GUEST_MEMFD_ALLOW_HUGEPAGE); + TEST_ASSERT(fd == -1 && errno == EINVAL, + "guest_memfd() with non-hugepage-aligned page size '0x%lx' should fail with EINVAL", + size); + } + + valid_flags = KVM_GUEST_MEMFD_ALLOW_HUGEPAGE; + } + + for (flag = 1; flag; flag <<= 1) { + uint64_t bit; + + if (flag & valid_flags) + continue; + + fd = __vm_create_guest_memfd(vm, page_size, flag); + TEST_ASSERT(fd == -1 && errno == EINVAL, + "guest_memfd() with flag '0x%lx' should fail with EINVAL", + flag); + + for_each_set_bit(bit, &valid_flags, 64) { + fd = __vm_create_guest_memfd(vm, page_size, flag | BIT_ULL(bit)); + TEST_ASSERT(fd == -1 && errno == EINVAL, + "guest_memfd() with flags '0x%llx' should fail with EINVAL", + flag | BIT_ULL(bit)); + } + } +} + +static void test_create_guest_memfd_multiple(struct kvm_vm *vm) +{ + int fd1, fd2, ret; + struct stat st1, st2; + + fd1 = __vm_create_guest_memfd(vm, 4096, 0); + TEST_ASSERT(fd1 != -1, "memfd creation should succeed"); + + ret = fstat(fd1, &st1); + TEST_ASSERT(ret != -1, "memfd fstat should succeed"); + TEST_ASSERT(st1.st_size == 4096, "memfd st_size should match requested size"); + + fd2 = __vm_create_guest_memfd(vm, 8192, 0); + TEST_ASSERT(fd2 != -1, "memfd creation should succeed"); + + ret = fstat(fd2, &st2); + TEST_ASSERT(ret != -1, "memfd fstat should succeed"); + TEST_ASSERT(st2.st_size == 8192, "second memfd st_size should match requested size"); + + ret = fstat(fd1, &st1); + TEST_ASSERT(ret != -1, "memfd fstat should succeed"); + TEST_ASSERT(st1.st_size == 4096, "first memfd st_size should still match requested size"); + TEST_ASSERT(st1.st_ino != st2.st_ino, "different memfd should have different inode numbers"); +} + +int main(int argc, char *argv[]) +{ + size_t page_size; + size_t total_size; + int fd; + struct kvm_vm *vm; + + TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); + + page_size = getpagesize(); + total_size = page_size * 4; + + vm = vm_create_barebones(); + + test_create_guest_memfd_invalid(vm); + test_create_guest_memfd_multiple(vm); + + fd = vm_create_guest_memfd(vm, total_size, 0); + + test_file_read_write(fd); + test_mmap(fd, page_size); + test_file_size(fd, page_size, total_size); + test_fallocate(fd, page_size, total_size); + test_invalid_punch_hole(fd, page_size, total_size); + + close(fd); +} From patchwork Fri Oct 27 18:22:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13438961 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 1BCA0C25B47 for ; Fri, 27 Oct 2023 18:23:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70D758002A; Fri, 27 Oct 2023 14:23:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6961D80018; Fri, 27 Oct 2023 14:23:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44BC08002A; Fri, 27 Oct 2023 14:23:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2D27C80018 for ; Fri, 27 Oct 2023 14:23:38 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0E56240779 for ; Fri, 27 Oct 2023 18:23:38 +0000 (UTC) X-FDA: 81392064516.02.522D8DF Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf12.hostedemail.com (Postfix) with ESMTP id 293EB4001D for ; Fri, 27 Oct 2023 18:23:35 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wcxg8mMk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3JwA8ZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3JwA8ZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698431016; 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=srZUoBPWZiKpBpFDSV5l36Sp6ql4zJFMGAxv8lh5kbY=; b=F4j/CN7drUWzUPy3DItfuqq1xDY5w8fxUY0Og0zvvwruBKngluG0qyScMy/WeWFYI3i+1b uB8nj3x9Qf0u9G0P1zI07ZVJH5/tzJRNgMmYpGiFsRC/rcdaEisHNvxF7LRiDb3p6+yWeE lpDKFLtj/VAiHWUkTpyQkqMpwGfGRVE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wcxg8mMk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf12.hostedemail.com: domain of 3JwA8ZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3JwA8ZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698431016; a=rsa-sha256; cv=none; b=Dfx4F8ldLCkP/mTcwL62aeeE76Jb/z9V8Bfn0HjK0TCitJ9QY8M/MORJlZi/0asmRUKVxz Avo3VzvLScbXgSPBD7r8CyLmhnHO6IMS8gEV7BKY2Hzh/lOrY/lQfylP/RFBuQ3UYWV+Yr T1QTj5Y8NOhdHKWZ0xE/6ozYD/l8aqI= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c9bc9e6a89so21621945ad.0 for ; Fri, 27 Oct 2023 11:23:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698431015; x=1699035815; 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=srZUoBPWZiKpBpFDSV5l36Sp6ql4zJFMGAxv8lh5kbY=; b=wcxg8mMkahgGd5AEGJZUNJ8OwuJis/ASg1M5AGFlf2uzxLHItMYZDsZ7sz7LylT6Hb sg9F7IWKjZyMG62bPsIgFnFKoNFPomgCfPG1ZU9JpnLaps1sPBPBkljCECVajLNiaybq 0Q6V/tDenEgzZ6Vs/cMtc5DRFXozLIYM4UF0OByzhDJoZCfhjiKdCwKAxeu5l2TmGDZ4 J73EyjzYJLcSc8Er10ovVTVH+ih1e1hZbKPgQuTqfy6s8NhZ46/EXRvBtjHOApev3wNC 38eLz/qsY3e5BAMoGDzlMhwGncZfpsrrHdlbT7VUvhOsc/Skfi2aC8YuH4DmBgVohnA3 KsNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698431015; x=1699035815; 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=srZUoBPWZiKpBpFDSV5l36Sp6ql4zJFMGAxv8lh5kbY=; b=xMWI1BWjsMhoGN3I1iEUv6cF9RoZXIV9tXu/Tl/ytxGuCAaW/XVOXGuO6X9sXFms0o 1CPTs/i8w2NFv6+xq8dv3WAMI8QAltDMkLf5S77ZKKvyI/5Uk5tKRKS7YBbZvF7UXqVv vjTDS78hMTtPBzu8U4ZS0jor9BFmED2Rnl31p76fO1R5vImTgYd847lvhZeJc4iT6whi V+mfqmR8G+q025Xq4NlsFFolc+vwHv8La9I7c+HFs1nu+baOMDiHKA4iIeaxN+RES0VW cpR19UnUFfjQAKkO++m8zoSaXKOArtDcCdR7i6A871kyQWSV9/F+z7lX2xicXbMza5Fv 8PDw== X-Gm-Message-State: AOJu0Yxd2JbJWJp+8lW0FIPrp/17RWA6qjslk1qcS5fuhBUVXoXi6sxd zDfDVxIHdHIj/a0XfIX9mN8j3ssfw9Y= X-Google-Smtp-Source: AGHT+IGsD3GBqEIwxpRuMZy6ZOrqAgR6Um5/oe2tLnBOXdqBPG31+kuimftnouCNE0Glk06NnY7NvSmeibY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:442:b0:1cc:1e05:e0e7 with SMTP id iw2-20020a170903044200b001cc1e05e0e7mr49290plb.2.1698431015053; Fri, 27 Oct 2023 11:23:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 27 Oct 2023 11:22:17 -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-36-seanjc@google.com> Subject: [PATCH v13 35/35] KVM: selftests: Test KVM exit behavior for private memory/access 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-Rspam-User: X-Stat-Signature: giqs8i3njbbqi68bw5cncma6tg311kmb X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 293EB4001D X-HE-Tag: 1698431015-212508 X-HE-Meta: U2FsdGVkX1/Th+wX+tUwvf6h9kDjs7SAwbTBAqkISady2r7lw//Rp9mit4IhNf3iRfGstWNpV7Mk8MuOVKRcCL0CRmc+irjeoHkN9xZ6ueh2I+3PZAx+W8AgTd3+SoVZCBWTXGEvjsSfz88cuuci18gY3XuCx4lH0IMwyWfNu6MhYUe9SKjIqi4UpaILulePg9WT+tZ+Zmbn445xyVs3hFC7zV05JaYjrfy9VGE2LTHH/ge28hE9gKRbHiUjeKXa0qTXdM+k6ohI0tFWYnwFgQDDRRe/xSslCwJJjiVerA3OYzLM60o9WIGRaUy/Zj9+YRoXShi8fynIePysubD8S9dxJA2NKjJBQiAml/DY36SxT8OplqUVovBOz+djEKYBhj7k9vQz4+3crgPne3ybJH8joU7WzTRSOTRn9Y0tdCeZt2QM/KgHVlI8mjnaVoNsumXpMiIy39DHh8ptkFdLRHilpeQC9ZjBRcKwEhWf0B7LDBOCfIPr/8JOMk6r1KA5CM5zfB1SA8YClGEHgGF+9yBBPNB5Zd6zdPfSfSYHHN2cwRupF8/anGuiCLQYt4JWtx4i2i5GxLIB49wbYNVHZY34xKJz20frTB6PeAL8/1LTI9CVO0Quohpz/sNCAUPxJdfdFvXZocnzphfY4jvkMa89uOsrfAancq+SZFLpHln18zCqULhIhkLF4sPvg6KMUYTpRtjFeKmB5+DtKoPP2V5Etw/Y8Iml1f0acLnHPpN6UE5N1COweQnlMPGY3N/AnBYqQbfty+ZZApEwWdrNkYAznu4AthuVM0XUFm6PqTPyoZXM0DscAgPsLrVF/VVakx8PWjmwxmDYhQMtRQKzd7umNEydHmkZU21PD6ohWK95OSUo4QxxLWkd6KZFV9FFYU4Soxfu/Gj8zchnTVft2nn20vtMF5srAZ+k8ygqMBswa9czHUQSbbTUbI6YUXcVfHkoiPt08hMeuWYvKWi 0SpZBTLg bmPHKVIIqUtsnPqciQUj7H1XCcUSMsZ3h0DdioeqAWAR9gtcJ6VywQZ9NHTwIRyRntVojLIeT2ovmA7K0W/S4FGg1Ftby9d7IEcRCVz0MHbcHuNj54wD5V/902pjySRMbaSS5+SbLfS8ETMkELLUissaBc7iQHPrMG/Jk7tPpQIdnNPpMBRx1PBIcaAYowgSNt4b8H9797Xdctq/4Zg6jcprLnoiIusX01Xe9O51IONXHTxfq5hlMgE3lHbN+RGP2ntGzO6dpTkkwuGpSFgKYHTNcvHRHMymVzGIsEq0io/JUsK3fTq8JWdWq5LbFEy+O4WhbSW4H7UKJua8Q9o/FgIgTG2CR5ffjJeJLZl3a+OzQSHYd47ybpzvo0fEUWiI+QGiOGSEaDYr9GBgbfHcX5vcwLfYcY7yLJAaSVseykU+dl2THp9evfbfwb4UVBSm0UPSiPCz6eHY2SlZDBOSgYNabkXqgvBtLb7kkH9nLVX+7tTv6T3Zszj17GK2uK6+Hi3PwrJouphXv36pehbJZrKhWTPRhR/LIccc/b2Sh8a4D38nGf8YKV48tKtbOr5z8uPNu7xo62nDdfsJ1LMuygTC3R4UV5U/9qX1b1OIKxPMJ6zEIl0seOzmI0op8zs5gr1ap8Xr7WBAyI2y7t2d7baQUkop+9vcRT3lUzAlcvtydSRLayNuTw698iCg6RoyS5tXFScaX2nM3KqOFtRnZx+PCY+oS03Cih2t2K7OU836L0hD0sncJuKez03eqlOTMkE/Kz7kCu8Bc/b8= 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: From: Ackerley Tng "Testing private access when memslot gets deleted" tests the behavior of KVM when a private memslot gets deleted while the VM is using the private memslot. When KVM looks up the deleted (slot = NULL) memslot, KVM should exit to userspace with KVM_EXIT_MEMORY_FAULT. In the second test, upon a private access to non-private memslot, KVM should also exit to userspace with KVM_EXIT_MEMORY_FAULT. Intentionally don't take a requirement on KVM_CAP_GUEST_MEMFD, KVM_CAP_MEMORY_FAULT_INFO, KVM_MEMORY_ATTRIBUTE_PRIVATE, etc., as it's a KVM bug to advertise KVM_X86_SW_PROTECTED_VM without its prerequisites. Signed-off-by: Ackerley Tng [sean: call out the similarities with set_memory_region_test] Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_kvm_exits_test.c | 120 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 2b1ef809d73a..f7fdd8244547 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -82,6 +82,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c new file mode 100644 index 000000000000..7f7ca4475745 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c @@ -0,0 +1,120 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2022, Google LLC. + */ +#include +#include +#include + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +/* Arbitrarily selected to avoid overlaps with anything else */ +#define EXITS_TEST_GVA 0xc0000000 +#define EXITS_TEST_GPA EXITS_TEST_GVA +#define EXITS_TEST_NPAGES 1 +#define EXITS_TEST_SIZE (EXITS_TEST_NPAGES * PAGE_SIZE) +#define EXITS_TEST_SLOT 10 + +static uint64_t guest_repeatedly_read(void) +{ + volatile uint64_t value; + + while (true) + value = *((uint64_t *) EXITS_TEST_GVA); + + return value; +} + +static uint32_t run_vcpu_get_exit_reason(struct kvm_vcpu *vcpu) +{ + int r; + + r = _vcpu_run(vcpu); + if (r) { + TEST_ASSERT(errno == EFAULT, KVM_IOCTL_ERROR(KVM_RUN, r)); + TEST_ASSERT_EQ(vcpu->run->exit_reason, KVM_EXIT_MEMORY_FAULT); + } + return vcpu->run->exit_reason; +} + +const struct vm_shape protected_vm_shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_SW_PROTECTED_VM, +}; + +static void test_private_access_memslot_deleted(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + pthread_t vm_thread; + void *thread_return; + uint32_t exit_reason; + + vm = vm_create_shape_with_one_vcpu(protected_vm_shape, &vcpu, + guest_repeatedly_read); + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + EXITS_TEST_GPA, EXITS_TEST_SLOT, + EXITS_TEST_NPAGES, + KVM_MEM_PRIVATE); + + virt_map(vm, EXITS_TEST_GVA, EXITS_TEST_GPA, EXITS_TEST_NPAGES); + + /* Request to access page privately */ + vm_mem_set_private(vm, EXITS_TEST_GPA, EXITS_TEST_SIZE); + + pthread_create(&vm_thread, NULL, + (void *(*)(void *))run_vcpu_get_exit_reason, + (void *)vcpu); + + vm_mem_region_delete(vm, EXITS_TEST_SLOT); + + pthread_join(vm_thread, &thread_return); + exit_reason = (uint32_t)(uint64_t)thread_return; + + TEST_ASSERT_EQ(exit_reason, KVM_EXIT_MEMORY_FAULT); + TEST_ASSERT_EQ(vcpu->run->memory_fault.flags, KVM_MEMORY_EXIT_FLAG_PRIVATE); + TEST_ASSERT_EQ(vcpu->run->memory_fault.gpa, EXITS_TEST_GPA); + TEST_ASSERT_EQ(vcpu->run->memory_fault.size, EXITS_TEST_SIZE); + + kvm_vm_free(vm); +} + +static void test_private_access_memslot_not_private(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + uint32_t exit_reason; + + vm = vm_create_shape_with_one_vcpu(protected_vm_shape, &vcpu, + guest_repeatedly_read); + + /* Add a non-private memslot (flags = 0) */ + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + EXITS_TEST_GPA, EXITS_TEST_SLOT, + EXITS_TEST_NPAGES, 0); + + virt_map(vm, EXITS_TEST_GVA, EXITS_TEST_GPA, EXITS_TEST_NPAGES); + + /* Request to access page privately */ + vm_mem_set_private(vm, EXITS_TEST_GPA, EXITS_TEST_SIZE); + + exit_reason = run_vcpu_get_exit_reason(vcpu); + + TEST_ASSERT_EQ(exit_reason, KVM_EXIT_MEMORY_FAULT); + TEST_ASSERT_EQ(vcpu->run->memory_fault.flags, KVM_MEMORY_EXIT_FLAG_PRIVATE); + TEST_ASSERT_EQ(vcpu->run->memory_fault.gpa, EXITS_TEST_GPA); + TEST_ASSERT_EQ(vcpu->run->memory_fault.size, EXITS_TEST_SIZE); + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); + + test_private_access_memslot_deleted(); + test_private_access_memslot_not_private(); +}