From patchwork Thu Sep 14 01:54:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384008 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C891FEE0213 for ; Thu, 14 Sep 2023 01:55:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 441AB6B0278; Wed, 13 Sep 2023 21:55:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F2C16B0279; Wed, 13 Sep 2023 21:55:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B9FE6B027A; Wed, 13 Sep 2023 21:55:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1B8EF6B0278 for ; Wed, 13 Sep 2023 21:55:41 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E2C59120EF6 for ; Thu, 14 Sep 2023 01:55:40 +0000 (UTC) X-FDA: 81233536440.10.414CD53 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf02.hostedemail.com (Postfix) with ESMTP id 213C980002 for ; Thu, 14 Sep 2023 01:55:38 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jQts7R+7; spf=pass (imf02.hostedemail.com: domain of 3GWgCZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3GWgCZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656539; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/cDQ8+RWss5y1dl7//cmz9qd0NB/VeUV6+/tNlKTQHI=; b=Nu2NDrJVe9sfBZRbTd3Bo/AOoZMcJtUa+mFPTcQuLInukF37omRa4hjTnM0oGPCfXlGlM3 h+S0zTT8zpVQvBKmThuufzMyvgAN8mhE2sezqTfEUkhaTkacXN27TBXEyjc9P+2EnrMTBH brvJT84YPg8x8CHSGX98cfjMZLEvR6I= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jQts7R+7; spf=pass (imf02.hostedemail.com: domain of 3GWgCZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3GWgCZQYKCCIQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656539; a=rsa-sha256; cv=none; b=IvcvHKYZf/Foy22sj32U3oZ6/4AgqXRsNqX4+i1y2kWhPTVwHRwDVVx2sF3BBRlykJ8t7k pXBaVDPq+P5U6EzXlazf6DjMOUUX930jFD4v8+pcuM0EyWDpTUMIT+44GniqtL04zTH6KF Kj4GmL5KNz2r7S45AXEqMHtBBhhuHu8= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1bf681d3d04so3893365ad.2 for ; Wed, 13 Sep 2023 18:55:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656538; x=1695261338; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=/cDQ8+RWss5y1dl7//cmz9qd0NB/VeUV6+/tNlKTQHI=; b=jQts7R+747ZpbBI421UvcWO7a9PWV2qqWSwoCYpuOjD4d1PNVO7eb8l8W1g12Mb4mi 9I2h/ltak6WgWRwvNsU7mladmAsJW4mLX1vaPGXsYpv7D7fCBWO/2+H439jS4xhcGee9 VQTC2p0qJ4hsr/3SnG2zcjU/n56JvoHPQiuSZzrf01Xzl5UdUwYCRNlo8c8E7T7/xVki AiJaQ7kKaT0/avrs4KBmVYDdzUf1xKb4H+4fx181tCT8jvcu3CIU9LEnsvycmY0fbD8z GtRL9HgHSiVqaM5d2SzP14KF9Ff3jZMkFMsj4h4pUXNqR/OC2i9MZ9K6ObYTxaZRaWRV zaMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656538; x=1695261338; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/cDQ8+RWss5y1dl7//cmz9qd0NB/VeUV6+/tNlKTQHI=; b=m5a7I8DyKyxuGIfesvYARAS+V5sA4HNA/u3pNQLJGWqsFBqFiW2QB5WiWzy0JxNcWj O0fOzdmrLeli2GqBbnOx6N5KeZ6OjRa/33oC0zpWpvXmFCraQLXZR/nfRqvmgM67pxxH fdJUgHPXotaDSwqBBHrVUW6nvSuSy4cYKSuRdOvjiggEPG9RcuLgsprHAZr95R0vuiDk dg8IHbMnBivDM6t9mjBACzXtDRyddPh81isyICLWkFtwvnaI4p9rOMLkQFc1XnEHbEon mrRL8PY783I+BsNLCg/Z/QRdYgvXnomcVeSnU5NdxWIAyrd1p+QaaNQkENBcT16Xums+ yIGw== X-Gm-Message-State: AOJu0YzT/Cx1jqqCNsLuHJZbPTjWEiz1Qz6LxBt83PsKGxPUXuQn/Qzc bJJVNXbJ0fhY4XlVEeDTnrHEFDArDgA= X-Google-Smtp-Source: AGHT+IE0PXMQEm/IFGrVOf6ppWkIN82E9wFoqnKGFhjVJyi0Xqws7PTmRSBCpdZciUBlWguOQ7crfgDDDDg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e88f:b0:1b8:8c7:31e6 with SMTP id w15-20020a170902e88f00b001b808c731e6mr201908plg.1.1694656537960; Wed, 13 Sep 2023 18:55:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:54:59 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-2-seanjc@google.com> Subject: [RFC PATCH v12 01/33] KVM: Tweak kvm_hva_range and hva_handler_t to allow reusing for gfn ranges From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Sean Christopherson , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 213C980002 X-Rspam-User: X-Stat-Signature: gzqcd41ozb9crfoworx4wy7hjqnt3cj4 X-Rspamd-Server: rspam01 X-HE-Tag: 1694656538-148150 X-HE-Meta: U2FsdGVkX19tes8r/tpYZtAGEehcbYCf8i75Hmy6CJt+35CAVtTes3EBoOWGR0aYEV++m5UjbpsRHD5xhfIPTB5mwRCgMRu4LzFBZK+/vhtYe5V2Lj6UQqOXv04/MiCM2tSofDcEtmmj5Mk8jzFFWP8+CVIOOSpVFICl6oOr/i08suhJkFC9sHE7hjQbdKzzrZMX/w16GqPfoCUc89rpBY4bPvZo1M7sTmNA4YUTDqG8ssb7v58l9PVRCKizZ18bOmXxlU3JfikZSyCMQCNNs1KqUJJKB97wK563B9dgCVkZzoyifRGtMmDIuzIU+85VxZG2GEce+G/JHoA4XyQ4oSMeWCjTkQoWLDy+Z4nk0iZzN4wxNBlz+JhMdkgcPuoq0gvW837QjEwwaJpifZifF+cnf13OZlxPkF9A2hF4FO40pQNRyWVp2/iTokpKzXyTu5zWUd1XSAaywCJTKLEfA/ITvbO2YA7XtBVzsyZYKMoZdRLmJ4bQQxoDBmsDd+Gd5u/OXVc1Pj9LjcTWD19giCfi3wQxeNN+BTSmyrEFb9znQFwZhiPO+X2CRuCSAUOnzUWolLe+Bmudj1qB/hnDTQHkEi28j/r/KjUOWtlkJweLJgvu7WgCyGB92banATdwoYy8rp1sHxOl/bxpzcO2lGlyG7CwAQ+C3KXluFVcG8oFVz6+mnGLPx7SQIMCFedAtDvn95sfHpPeiWgd1qR377WBZdGWsio9T//xcWmDp5R0hexmJmjzIu86mDK7pgrwLlkDR0gNicgVIkxSoS1iVOjVwCDHUOFvZXjjZJp6t0ErkjyH5++upRSkj7gf17ORLL+WxZ7vmU6Ig7KbmKv5YOM16us3GOVLscbMRmT2t3/+I14mfGgwy+3FVIWxzo6Gj33YzCYUbhCh7P1+82svjDB63MOxqqFGit/2fpI6bShcmXg1jHkg9GqdiqyBsqbXtsmGb9DfsSypLtH1Rbz 3eCgPSLi FEoG/J3fQ/K2qdPx45NoW3+it1g5WmjPYo35yl2XP789tXKxvJKNG0Chc4aIkglPte0NaoZIU9S05QdUoZ7NQAIk2PwTPiqo0kgNSsgrrBEwWO9rXwt7u7/Cq7I9w01X/teZKZrngn31xojdUAZ18tnVV3Uo3TgYp5WNvVHitMs+eBPMyTHy7hmfITrtMrH+L4Rux/Bqc6rL3X2ndh2N4fLEoU1k3BbPmvMznwp5wNcipA2FXaV+61wONq5hen1/QjgVfi7xTL0lFNUQY2U9AfLhMfM6lZGgJMj8JPN92dPB1XQWgUWNYwGhegf3idrkmGiplbK97pdPErjMIYmxrElCM/35Sz1TfMQ4RlXsFG+cOk7ge3JtJd1RSjzWIQMc3q2AeZuD6stms7cV+QSNwnbC4mnqTWI1URAyNkIQEMZaomI029YIemEJGQZN9Aqh+RMY96JhBD7CNLT0M2duJZc64SGKklwSx4yqvEJ7EQzpPVqDw/Yua0ihMtMB9GxiI71gLLBjWkETj3RtPbEuwifKTs5xxtYQhKjQeveTXDYvZ5oxnhXoszhznCTDcN6Kd/kMLXGev3kcTgE/rugVzqydDb3OqM8t+d32YS0FRAOMcN0aErf7SEYFRE89TsGxsyqbFI9sAFKKBpuXvr/csdZvekgav0USYBM3L3gk3LcBIDeVnbfXnuY5EkAWDP+FvpuxRbEYy6NLDPHZMndMx6qJrAXTydUATuV8q5TDPwFkw/MOH7Bgg6IccuaXmBWt2lX9YEbBsvAoK+C9Em57bnoSMd8OODGRrzVuSA6gelstYd2oQjre2wpzsKZihwdzIAhE+0/VnqwMZV5hHb8nK8qGcoA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Rework and rename "struct kvm_hva_range" into "kvm_mmu_notifier_range" so that the structure can be used to handle notifications that operate on gfn context, i.e. that aren't tied to a host virtual address. Practically speaking, this is a nop for 64-bit kernels as the only meaningful change is to store start+end as u64s instead of unsigned longs. Reviewed-by: Paolo Bonzini Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- virt/kvm/kvm_main.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 486800a7024b..0524933856d4 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -541,18 +541,22 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) return container_of(mn, struct kvm, mmu_notifier); } -typedef bool (*hva_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range); +typedef bool (*gfn_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range); typedef void (*on_lock_fn_t)(struct kvm *kvm, unsigned long start, unsigned long end); typedef void (*on_unlock_fn_t)(struct kvm *kvm); -struct kvm_hva_range { - unsigned long start; - unsigned long end; +struct kvm_mmu_notifier_range { + /* + * 64-bit addresses, as KVM notifiers can operate on host virtual + * addresses (unsigned long) and guest physical addresses (64-bit). + */ + u64 start; + u64 end; union kvm_mmu_notifier_arg arg; - hva_handler_t handler; + gfn_handler_t handler; on_lock_fn_t on_lock; on_unlock_fn_t on_unlock; bool flush_on_ret; @@ -581,7 +585,7 @@ static const union kvm_mmu_notifier_arg KVM_MMU_NOTIFIER_NO_ARG; node = interval_tree_iter_next(node, start, last)) \ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, - const struct kvm_hva_range *range) + const struct kvm_mmu_notifier_range *range) { bool ret = false, locked = false; struct kvm_gfn_range gfn_range; @@ -608,9 +612,9 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, unsigned long hva_start, hva_end; slot = container_of(node, struct kvm_memory_slot, hva_node[slots->node_idx]); - hva_start = max(range->start, slot->userspace_addr); - hva_end = min(range->end, slot->userspace_addr + - (slot->npages << PAGE_SHIFT)); + hva_start = max_t(unsigned long, range->start, slot->userspace_addr); + hva_end = min_t(unsigned long, range->end, + slot->userspace_addr + (slot->npages << PAGE_SHIFT)); /* * To optimize for the likely case where the address @@ -660,10 +664,10 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, unsigned long start, unsigned long end, union kvm_mmu_notifier_arg arg, - hva_handler_t handler) + gfn_handler_t handler) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range range = { + const struct kvm_mmu_notifier_range range = { .start = start, .end = end, .arg = arg, @@ -680,10 +684,10 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn, unsigned long start, unsigned long end, - hva_handler_t handler) + gfn_handler_t handler) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range range = { + const struct kvm_mmu_notifier_range range = { .start = start, .end = end, .handler = handler, @@ -771,7 +775,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range hva_range = { + const struct kvm_mmu_notifier_range hva_range = { .start = range->start, .end = range->end, .handler = kvm_unmap_gfn_range, @@ -835,7 +839,7 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, const struct mmu_notifier_range *range) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range hva_range = { + const struct kvm_mmu_notifier_range hva_range = { .start = range->start, .end = range->end, .handler = (void *)kvm_null_fn, From patchwork Thu Sep 14 01:55: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: 13384009 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 EBF30EE0212 for ; Thu, 14 Sep 2023 01:55:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C57C6B027A; Wed, 13 Sep 2023 21:55:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 727136B027B; Wed, 13 Sep 2023 21:55:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C8B96B027F; Wed, 13 Sep 2023 21:55:43 -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 4E4C96B027A for ; Wed, 13 Sep 2023 21:55:43 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1B55240E78 for ; Thu, 14 Sep 2023 01:55:43 +0000 (UTC) X-FDA: 81233536566.28.BF1E48F Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf03.hostedemail.com (Postfix) with ESMTP id 42D4620008 for ; Thu, 14 Sep 2023 01:55:41 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HXIy2lnu; spf=pass (imf03.hostedemail.com: domain of 3G2gCZQYKCCQSEANJCGOOGLE.COMLINUX-MMKVACK.ORG@flex--seanjc.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3G2gCZQYKCCQSEANJCGOOGLE.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=1694656541; 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=/PWByveWahNDDpwQn3dTY8oCHEhRVqmq8s8F8KhgOhw=; b=CbYZKJQWwzG4ETHuBbIGHPdi/mlkaIJZfEwgDLfkL+v3bX/22Fc/VMdlTmL+BpHqdNHyMo CQERlcSEbMt8kIVI6TVB7HXiB89QhmPbtiuZMF3ZSaPlqT+XJDziNKkrqqFU6O1cf3cKTx y+/hIcQW42GBkgUSp4WDBg6HYpIVUDw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656541; a=rsa-sha256; cv=none; b=StJI37m7Klkzs4FEYIgUpgqdmNd7+yt+6UgpWruq3vBx7/OStt62E8E0rLcLQpCZExs6QQ xIcD/FJfyqB2eg6GCtIh0XQN3xkm0k1+v6tuEqi6WEwVXNKYtWu32t1noz0NXwnvecfMmB um0qylEVzSAZAsGUzRTFzkw6f8VshWM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HXIy2lnu; spf=pass (imf03.hostedemail.com: domain of 3G2gCZQYKCCQSEANJCGOOGLE.COMLINUX-MMKVACK.ORG@flex--seanjc.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3G2gCZQYKCCQSEANJCGOOGLE.COMLINUX-MMKVACK.ORG@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-57787e29037so342127a12.2 for ; Wed, 13 Sep 2023 18:55:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656540; x=1695261340; 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=/PWByveWahNDDpwQn3dTY8oCHEhRVqmq8s8F8KhgOhw=; b=HXIy2lnut6XyQkW5Xo2VBegD9RFsEeJ0REXF8hUqooDK90DM8GJA3y8b+ZYzy2x3x4 CIQoWuUYZxIZltSaX+g2tO0Qsygr/X0uKhLxYjeLYWABye4uw3ol/Q5BwdY+pYGCyNj/ ESBcB99Uto0G6RT8rO95ZsTDzNm4cAtBznzuOe8Gu+l38+WiualtZWx1CqvCFedAVZph 0pfnEkfRVzyHnSdBbT9Aje4uXQNjQi0nQJhewMyLSwIQPLz5Nty+739cDeoCVZTvg48j lFFjET0am6z3JNYMBYDRmySpQRoouvOKtqg62gwUmyFn4mknVO0665ZXUa2a0vfyQ3Wl VGcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656540; x=1695261340; 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=/PWByveWahNDDpwQn3dTY8oCHEhRVqmq8s8F8KhgOhw=; b=B6pFbFKH+PV+7Uqa9rYKe8wQ5bNo+aE4vJLJzGtbq40JzDERSGj8WNROjZ7ySthx5j B2C4yodO3WnHWuJlPvkSWjTGUUG3CEMgoAQg4A2v1ubS2iibZnd4msa9ft7FH1zNR+p5 OzIEbYLW96dKV35jFi8HbTNeLCxmYXAwAk8sYlWjTjWTeN1ckjh1oLhJMsScTcn3A1G+ OsKMY80W/7ftYJsis5cmUJkHmeAlE/Pg0FpZ/1fNNhPYOW366VswAHyOat5gU83sTaxX QwpiWlHFx5doNSDZwgbmxvYGVoKlkYZN7kVeXJA2stHj+PQZGwubpYldcR9onqfT6zhc gNsQ== X-Gm-Message-State: AOJu0YxxIYy68sa+EE68npPuHsIrN3Drh5U630iUZGq291TXlH5KFpnn 8xUcy2dOXScPa+/0bp7oJDTNN39Bpi8= X-Google-Smtp-Source: AGHT+IFH7Y8a1vBNnSACzhOpKycbEt9pew6IzwhiL/gw/jXJMO2Ad5b3/QDP/rYw9JwfwFTkI8vz1sHeU4s= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:230b:b0:1bf:cc5:7b53 with SMTP id d11-20020a170903230b00b001bf0cc57b53mr198908plh.1.1694656539979; Wed, 13 Sep 2023 18:55:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:00 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-3-seanjc@google.com> Subject: [RFC PATCH v12 02/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 42D4620008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: fgb1paom711qkrczq4c8f4epuep6395n X-HE-Tag: 1694656541-542858 X-HE-Meta: U2FsdGVkX1+UzyW78UPflj2wgYmCXhFD0B9H8WGSFiOFY09MQeUsGXEi0MihDYwvbBO0Kve5XsWrn7XkdiqW+jl36QTZWWjHrUmUAB+FGLwKy9eu59sOBtkE490xDXBmQRpmw0taNY/tQ6y2G0CBDOIZ/6YwpucF879FZc3PiYBLymZthFPi8FRV86l4UHXn62KAU1du9ks8O7+VZp2zSt3K54pAPiNQ8ymP5+4oSt6lFvzzTIe7LEaDVHCgrl2saoBnG/KkRyCgwwMokPDcpybzVBTB+H5y2Z6YvMP7t5hob7511Qb09bm1jizYXrRXn6Xsai0UArbLbKFAEnZJKdyTXmWWmz8lQBx0THoa3RkZosXRoeEcSJgR2wUIvqBLzHcfLCWyiZt6EMkCVMaBUt8xWomqSn6JTDUIRTUH7gSQCT6PoJm9qJcFMtg62SvgQoxGrd1WqG4OhLM8cupGBuiG3ZOUo0UsSaBt9pU+NKuvVG6IYr7I3BU3bHuaf6X1OlidgW7os0XVpv/Fxx5Dbnh0ZhdWVoZ6sSqt8OESm1DS/doLamH+ZIbF33pos4jx3j8LmGUOtCeorGglNiwUzl2abGw/EdBoeOW2bXBMjsr28JEvl6CeKJRsKPHN5SRQMxtUIK0XQuanCGN0TQpwZa/PHYxw240Oz2QmjEsaHZ4ZwU18ns5wK+O4vvuVuCKpbd/ZNDDAljzxYZcamwu4rfwTlenEgY0yZl6uQ8//VYEPwEwGoWTNGV1qaQJhox/xOg+Rk+8IDW+WvS0GJiXSH6MEzTvJ9BND9F4Zx5IeFJOEKkr8gQgBsFbpFffoc5S6K/E87X8/Uoho4wfKZ3bUOBJ22QbXAzyKU7NCU8d2MKvBdCH8cqY+sJMem9G6WHgdYYpY4S3a/YYKK26a69F9Xo6r8CjFWI4EA2gmveXpg2L1/rgyHGGUcU/u8G90qgHdQt5KCujPtYuWzey0v4b BDRA+Ad+ TtVSCmDb5CvocUPaOzV5EuaH6Xku6jACK1fYZfEG9y4AeiO/0YZyudJu4xE0leey4v09zufhApGKmYfa/eVYOP11+65eqnUEFTwHLf5uDOYvtgaJR2jyCTcaEfnoudzhXAZz8TR7ndoCM95MJ28Y3AsLPHazPS7HNw9jL8a3sCIDgwAp2IpvB+E322gQ6tK0LkBT81bcq+Vn2aYOgGcApYUR0lRS88oXc4Yi7lpeG8WLXCFVzEAVYQm8CdnCmQeppnArYF4XC37UkA5ud4WUKL+quYdSJAK2FmaE/NhIeCqhHP/eExtVICiil7OAcgNBhkZxjr9jEs11Z2lRYDN4jnWt3pLDq8YH/LyDqD7DXvL2N28iy1pAnJEuxTGXY5X3rc8IeiXPILA5KFMZ5FAZxR2A0GuIvbBluUgPma0XjvDtlixhBuDtTE7DctgOV1x8VhwCMoYdcAAnhIqV/g4y7WVpb8CZbkZyFG26tFumsKn9E7KnygJK0MbzgN4jNrVK9TEye9fP0tJczs8ozYNJakXX7vIuKip6UA8HchPCwEXd7GuVl11wB0moWk5sTajA/eOrXQ5MolUGskTPC8OcISPciNF4NeDKB//g1KjOGbuKPjzdhFkilxdc6PyieItTTSHdaVK4SBMeRj5qEsL7N/pxSURXduj8G5kYlDh3dZ2bqF2zCa5dM+EQ+2r/QzoPaCMuQmKJUV9dtfNrL0QHDYqTFV5dmivPzrF6PwJa8/aN867NL8GORoJzmZQ1+2kqfEmLQxtNSCl+WqdCrHR0HmISGTs/rPQ1wtKr9p3MNIiO3+cFIQpgC99gY0hEQeOQcO5yRsbZmDOCiFr3u/pe4lZz/Ow== 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: 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 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 --- 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 | 40 +++++++++++++++++++++++++++++++--------- 4 files changed, 63 insertions(+), 31 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e1d011c67cc6..0f0231d2b74f 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) @@ -6253,7 +6253,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); @@ -6266,7 +6268,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 0524933856d4..4fad3b01dc1f 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,15 +741,26 @@ 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; +} + +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_in_progress == 1)) { kvm->mmu_invalidate_range_start = start; kvm->mmu_invalidate_range_end = end; @@ -771,6 +781,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 +794,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 +833,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 @@ -833,6 +848,13 @@ 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--; + + /* + * Assert that at least one range must be added between start() and + * end(). Not adding a range isn't fatal, but it is a KVM bug. + */ + WARN_ON_ONCE(kvm->mmu_invalidate_in_progress && + kvm->mmu_invalidate_range_start == INVALID_GPA); } static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, From patchwork Thu Sep 14 01:55: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: 13384010 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 0953BEE0213 for ; Thu, 14 Sep 2023 01:55:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3CEFB6B027F; Wed, 13 Sep 2023 21:55:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3576D6B0288; Wed, 13 Sep 2023 21:55:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A9D96B0289; Wed, 13 Sep 2023 21:55:46 -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 04E4B6B027F for ; Wed, 13 Sep 2023 21:55:46 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D29F1A0F4D for ; Thu, 14 Sep 2023 01:55:45 +0000 (UTC) X-FDA: 81233536650.18.9042FFE Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf02.hostedemail.com (Postfix) with ESMTP id 19C1F80005 for ; Thu, 14 Sep 2023 01:55:42 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Zdac4Ao6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3HmgCZQYKCCcVHDQMFJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3HmgCZQYKCCcVHDQMFJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656543; 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=ipMZPiP5QxzJYr8+L2XHaFOQ2agfNk5lTTsPLIZe0Eo=; b=XR3l/nKfoXKrkf/WVaS9ozSom0YE3ah1kqX6NoN59OMasj0W2679sMnWBJZtvTTu7Wvp84 V1TtQ0OEQz0CXAh96csNGU5bvMgMBKFAI/VR5yjFsvC1FN41HdnQiehXy8Z35nj8jBqazj Y1r1DxwZkgHb0bZZT1cYoCFek0tWySc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Zdac4Ao6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3HmgCZQYKCCcVHDQMFJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3HmgCZQYKCCcVHDQMFJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656543; a=rsa-sha256; cv=none; b=OfcVP7PUR7+c7grxMJ10nwlBCLjV6jFeMxa9mFWDVQPWCAEjHTKZE4KSGqFgXw4dYupJyu kIK2ci+IZWe4rsZ1MBEOrfYpC1RRa1kDAgGLbjm6A+GroJYFKCk3mt9N5pwV2fzOrMHWqv f6x7WE4FHfOmFnyNG1ag9d7oYYpSedE= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-58cb845f2f2so6171017b3.1 for ; Wed, 13 Sep 2023 18:55:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656542; x=1695261342; 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=ipMZPiP5QxzJYr8+L2XHaFOQ2agfNk5lTTsPLIZe0Eo=; b=Zdac4Ao6elJ2FlJgia3JQZ1h822RopV2q4fEJ7UgeL9Be9Vlz0kq47fUeuBwRaMEJ6 DoG3ZB0vlBLRvCNV+M3s64dRL9Fo8H9fC+KJz1Olcf98Z2cWT7j995Z7yTjuBgxfPKq8 yXYTkdnqvVtA6zml4VCWmuLD+hyKuCEgfPQH6B+RVZ5/85G3vVdZzwVYs5mUI8KhB0P3 T63aD39oRB89i/X6jr4afD4E/fl6B1It+gZaevfAUPlg868+mVwsUws+HZCED2wUurJL XE1Qe7fYukvZ6FNrfBx68KxuIfzyQsW1mZNgnpm6swHkLknoypXc9piLc3BbpBqfoMqc iH/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656542; x=1695261342; 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=ipMZPiP5QxzJYr8+L2XHaFOQ2agfNk5lTTsPLIZe0Eo=; b=BPFXVwrDEgD0ejJeehFfhT1xLgHYcuoWXvh+RRY5RPHVB+2pojHIfeiQCRAetUZYXe rYPPjbhudWdvnPq9fB2A2Qcx5LauO9aNkw+VAkPs9faZ+BZK/Ge7NR7/7WEqJgRW6POp Xs4KWGDrxLktg6OOQUyxmPYGy3pAxBv+8t4q7MS1f69aJmOiyMoBLywJV0cjIBBmPiiu fPCizT1yTPhDxt1tcnNs03Z2T4jg/QgibusRHPOydlrZu4p6rkXGjxCpF4dYr+AxIRi4 SuIS0Psgt+srSbk6y9RvSyuM4CcR+giBCGxnc/JjCZbnBsPMhVfmBoONsJb+6i2b8OU/ ItEw== X-Gm-Message-State: AOJu0Yyp5i92/uaqa7I04B7My8y2oHjIrWYjOIm9y7eNonwzTw1HVREg 1NkqELnZ5Vt3bI2jgBu4RWrxpybM/wE= X-Google-Smtp-Source: AGHT+IHZ9maMkZbk/mDeS4QG3cCYdpuky8EXqvBvcYXtjkjg3bX1mPxWKIK7RZclMdxYQ1SdoMO1l2xcItw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:b0a:b0:59b:ebe0:9fd6 with SMTP id cj10-20020a05690c0b0a00b0059bebe09fd6mr14465ywb.6.1694656542028; Wed, 13 Sep 2023 18:55:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:01 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-4-seanjc@google.com> Subject: [RFC PATCH v12 03/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspam-User: X-Stat-Signature: p1agnaais8ba7xfjfbjpo7iugxf6trib X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 19C1F80005 X-HE-Tag: 1694656542-707866 X-HE-Meta: U2FsdGVkX1+icok/XfPDQgi/p8ezWpTBkhkRajvG2i47eGjp0LZjuXmjFctBdNJxpBofUoTwsdRJ5aoC9AC3VJuU0zPtCjuMVHS/p62IGYKg23C9yygL+J+pXCuNut0dpVTpleuMrQpCbMqPnKjZEqxHt2tqELUmLRgtvh3144pLGf75R7Hr5+wKdFYkoPRZIs3Yf1RBGmeRay6xSaElLkYiMY3M5h359yMEX7waEnPPwoG/ifLHYZuGK1NGq0TS8Zs9z4MlCyx34Y+3MGLSL/b3NYsk293yXCMKENSS7m0UvhkJfCqmbeocPD2eqdEwvVaLkxTQ12BCSpXqEYIQYNu0mkPjSxhGrR+RmZ5sw6dRfaYwd/cIISpstg9nSwA8Xf2EZ3xwNSVy6C2D4Tvtr7WMdCzsswuJEuUAXimF6ZZiX3MNsZSmFaHI+z2TcSnEYEhjyH30ynMuxPgDhkB8Xn0pVRVbV87xEATnCTGA4eFcMfFrH0mmjmRKscl5SrBLBvf1ihwBQMA5rVynSulcJ/sov0orOEHimkGsbbs54BgqdtuBfxLvcGg4ShlFFNvI5F2nUbznBtnYNoLThLWuPELxA8FyW1Jas5lGZYr8DTJrUjuyQeT9Vme9AD9Rn/NY0IDU4a3rxct2XWmh98SeSLMNgUvh/Xewbq3bTUnuDd5FvrBZ1xzScW2r6ewvKGTF0fIIVTagqCzP3VtM9w21dF/tjprkYYBqbiOP5ZABuR5/W1aS7v2RfDGPer1izguC/5QKqZsdKRos7UUkWtxbY/SxCsn0UH7l1BZB8SPf0iJ6VN/iAgeTFQAe2SkCyAjSoMY9uWT7c+Fm2t1L6wnnZGkkBhiJu4pFW/2xl/0HjrWXl5XsSNO5RIWYeSSNJ4Ohci/CoABN/wWHVnr8i2n+xyeR4ftxfbfr1H/BR5qcmIbw2m/WTJ9pbq6eEts1z1Z3IKPJDDyLTu/LUoM6/Ou eHfhhzHW qI52/aJ7UCHHhMnB1ooyFLnxhkv73zdhjP72HSuyqFdy27ZcCQ79LFxwTBkiFaWwIoeslczBCMfanoTMBcXDBcIHRwMIrUmbTorftieTRXODlD1NMD4ETGk3XWhiwLjfzVdavXb+JVb930Fgy+4PkpYTHjpXKH9FY/SXCsyr2q66EbR99bC9+R+lxqk8nBkCNr50akP0tyy7cxPQIAr2Cxt656J7XPG1xvX1tx2Cq79HjMaQqSDYbDJhqnNTTsKhGsqUhm2NcEB/QDh2OmV3/amUE9H+ugSRde2QYNtx5zUPKUGT0GUFBDwt8Jl+3piDr2rWgLgNsFZtGclKPc2eTg9VGBw5ka/EHqm5IgPW+7bBIykRSq9jVRWP+Fi77m+PkmHc6a/WU5SAQQGx5PwnKxg+yC+Z16JJp6aPEOSkWrQW1Fo/V5NdpHTl3s4qi1U6oWBf3Noi42pzB7x/GPFaTyaLnC6BUGxrJfBT45JE0D65VXR8QNAbKR+XYdvm+oSKcCaCCG+hEFfkfXDd+y/tvli2UbCqpeO5MSZjaNU+HEZexaYWVjn637m2IrcTWWsB4pVmo6WQHDd8jqHaghAi6+F83IZZKcZHCZDghEgoNyQRHHI+4ev+1tdLbyYgRrS1RCjJAEeAQfMPBEv105LsuHaxG4yBFnzM8Ei0NrfxYxCCgu6UcFLOMUDhgsdWAzPHTN3vdUahSECxYZOfK3nBEIkkj1xXmzeZNoveUhrUclwe6eJAtiUD461UgXH6xF3INBxeu0cvtj7rUieLJ0AVJ130Jfa1XKGYLNfTfpKCWBxSNbfs6EGmbhGLoJw== 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: 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 --- 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 Thu Sep 14 01:55: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: 13384011 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 EAC28EE021A for ; Thu, 14 Sep 2023 01:55:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37F576B0288; Wed, 13 Sep 2023 21:55:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32E986B0289; Wed, 13 Sep 2023 21:55:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 181726B028F; Wed, 13 Sep 2023 21:55:48 -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 0462A6B0288 for ; Wed, 13 Sep 2023 21:55:48 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A5E9B160F12 for ; Thu, 14 Sep 2023 01:55:47 +0000 (UTC) X-FDA: 81233536734.01.F6C6851 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf19.hostedemail.com (Postfix) with ESMTP id D6B751A0017 for ; Thu, 14 Sep 2023 01:55:44 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=oscbF+Sd; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 3H2gCZQYKCCgWIERNGKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3H2gCZQYKCCgWIERNGKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656544; 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=5olfKKfAgOBEIWorNeGA6oMvbpwHV/k+KeYKQRUdq04=; b=etpS4a9RpxJTM8bozbEnKCCtDmjuQNvQ6ebj1tmNEtFhzZKbOOplVfFgWwMyQU7gm4MIyd E1dxzQPqOw1tqDQJlvWHeb/pTDwTCv1ITiWpeEHBgPYoQdNtfnUa8rYVGy8VWHpeNUHfK4 Y/rjOvqtPHYxMkhbg8CLBb32YmIILPE= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=oscbF+Sd; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 3H2gCZQYKCCgWIERNGKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3H2gCZQYKCCgWIERNGKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656544; a=rsa-sha256; cv=none; b=0n6ZVmnil6x0Y5BC0o6zVzIkqaS7lyNkxBrlprm2jzMBktiHOIEdmrOu4mLbr0N+xcbDPR CjN+9ganUyBzN/8gi30P/BcNZtEjI5U/nhMsGpUVTU1DwCWb3mxqm4G8EemUpOtXhl6GP4 714R1Cjzm1oekYsEbVip9ZLlPcq7qHo= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d7e8e12df78so539518276.3 for ; Wed, 13 Sep 2023 18:55:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656544; x=1695261344; 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=5olfKKfAgOBEIWorNeGA6oMvbpwHV/k+KeYKQRUdq04=; b=oscbF+Sdn29udgOEuwGekl8Q8vhPzdmb2OYzzsBdGlf6X9nb9P365vKagSKKt1Z7ut g93IAyHOOadFiBobNbiu1LhVt4ov0tRoM4/GCEg6Duu5Cl0wRbOdIKnj1SRK+Jz0nBRE NRU6LBszAeJ7U7bGuXv96ypARmalsqoLjYJA0WAhGAFR8hZFCGDPTJ81SxC1Hvh6KuMt ElMv0Y7fXqRmBR23WhTO06TmT2Nfd47Lsbv6Ks6OGqSn9WCeUoJxhe8YbO28ecIJNUW+ 9XLy1atrWeVUhywOeAJPdU0Xe7vAj78X0ZFKjTZYXIY15TdGBONmMIqrc2kis/cvGdMN hb6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656544; x=1695261344; 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=5olfKKfAgOBEIWorNeGA6oMvbpwHV/k+KeYKQRUdq04=; b=xNzIvl0BgRMEgc1dnMABJyk7INdfk+Q2oFRqk5ODVY1kthW0uhbIpv4xf5ROvNE7sZ LTNfCvev+OgZGA7xOT+F2JDy882q/EfilQvMecic0/EhHMHqNfZ/VyCdWn/T0K/ANshZ BFqTm94eXCQJ9KXS6bTSfsr76bhCIm7KkE+kGkj4GPMqEzh3M5qagBbGIqSm/dnqBG8b w7f4xdTjwzN6PT8Gp97jeRqfTmVBGtBQtiTYBsF5CfxYqoT60temQlnXR6mKkczAlxBl dYNkeQuxCYHexnIfML4ERnEKPICpMPY336ximJAY/jFdeKSmKGzeXWShAJUt36D+T0vg KA/Q== X-Gm-Message-State: AOJu0YxlzQXW/QgsSHkcrrvAnLTflRrMS2IA2QUSBf3J1N/Tt75MoFcM 7jCs3fBuLoXBDqpWH15YQHzZafyGDHg= X-Google-Smtp-Source: AGHT+IFAWDIKY3pID0CrDZgtjj2bXptPteu0k42w51y6Ps+oH46knUj2OnzhEQTEbd0IbAu5MzXei5K4JBI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:684:b0:d81:8e4d:b681 with SMTP id i4-20020a056902068400b00d818e4db681mr15248ybt.12.1694656543919; Wed, 13 Sep 2023 18:55:43 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:02 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-5-seanjc@google.com> Subject: [RFC PATCH v12 04/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: D6B751A0017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: pnfys4x9fys99rq8q54tjmio9bqbutyo X-HE-Tag: 1694656544-75175 X-HE-Meta: U2FsdGVkX1/Na/YeaKpCG0nriJ19Cti7KjW/4hay/+fx0ACGG+bgfpzsg3RJYO89bH7fbw5eYfU7WKsAWErFUGxhiuMKHX+iffNq2X9Wi0ilgwpwtR8oDq+zayGAyh5wYqoYcgxCLd+YrzCGUrLikVE+fI60Y67yKVTYMQgrveoCTLb5IeqeH433gl6WG2L2/mspdcO0jUsEAhVycqEor5xYh1YZVc0HttvXEIFem840JUqic9w7cwwTdZMUUDtihQYZFHZKqCdQMQ5oXMLEKcvVsF/COUnOyDFmYfmwHyHAB+yFdxEHHwLtySpqJ7NtzZB/8k9TfqAuAc2fHn8AWkqYD3o57LwkyJB/n7YpjA0kcLV7XuJV+/vzWftxRXQy2QA0qnwOl1Iu5A656gLvM3427jhgot2rNNNZcbWX7S32vfWtkjkzEk+58ZEf+FbLE8VfY2OzcTEA7hcb/jBNE5IMROdKWEuHwZb0H9lX6DFQ7i3LkM6LvpPHyy3CK0wa/yZKO3jsoNDbzObRh16Nsedvl3DRa8jAzSKsHXHuLnXtq7EI1HNeO3fgvAzI7QrFI20Ufyyf00REccEGa9bqoBKLa3pdTAcNIkFgQCyQ0szCANmhMnDObMC5YTLxOR0XJNEWhifelIXEY5Qu2QHO7Qfazy9mZ/vvQjKHt4HIcwlHjOsE6yRSM6rUlP9fp8bXe8fjOE3RKYJVfsqgi04+oFSCfwuW3dpRy0VR0prBvQrAZhNM3bX1rB5+dsDLR/wu6et0FmVXcfhUVvUpADgInEa9/Cc+aJ262ejuxuPf86RbLMIPQopLGVQsvXgzH6Dovn+F/K/sUt8A4DeV4hOHjWkd3o6RmXqO1rD9SvVyOkAJjMjWdTw78qkMXsP7SoCaFo/HMqRF91zJVhS4oMSE9lqPCdciZR7cRAgrIfewFuIpiyOrEgY8Ue/R+Y5srBshKhlpLjdvLkr27D3yuHb sNVAF0CP LSBEQxuVhr1E4NpWcJrxTZRW78aUeVXMFAJ70bSxPh5Ksm8ZpakAAO71iU9bLl8p1MaPktA1tEdt43ZgIg0xjelcsD+eaClqfsEmAqRCPSjyH2JSdtQv7Licieq5jbZChU1k9P1/5X5HMJi1ZA+XtrJbSiBu9duaPnUjgRH4ArtGqN2jyZz5FSgnfYSNCSS+W7eF2WdJlli4iUc8pKf+Xp8n3PrEeQEhGWcnIE8soGtkDG2s0OszTdS7wsa+6Y0Zw8mOMtvs61FFGTqQwVEk1qn5sOP0c7WyTuAIIucxMOIX2/xHrsdZZlZ91MpwEP08XYhr/6Y1UQCul+JmyJtsUSoCwxm/ZaMxX/afC0DR0bmqPDMja7MwLqEBXsWpmP6wNIj8IbTKs8VcfRPqoLzGcV2/+FG9Ence7vzFI+/bg16vSvED5jN7KMvguQNX7l9dUym5iyGbM/KKRHzEVeY/ofyAnQk4m72uU3GN3bQMEMMLl7kBeLNWZkaLp1kXhgwLb4I8ZhnJd4p4UI1NyRe2H/ijOKH3Z6y+V2t7tUDfRH+2lUJyVzY+8tDZXo2xVXmVgdkuf1DHae07PO8Yx75jaAjM/rjd56xUwrNVn0XwgpGyHiHR5U4UJmXmIxhUMpSB18TjMik9b3emvcNxagU0NCrfoKAy43qjv08rNjc+vQuICE8F0sWfXx6P7TRDVkkLbUhgztZZ4Ax/lftavo7MKYTbYhBuL7m9HUJwIZqWrLtRYzxwAmoqdIeeL0/januBWmsZmCaAUFXYpXDzdyrip2YJGqO57I2eD2dr+2zlO+zWQo3hflR4ju5MP9romODVuCJfSphoNVKkwNHfHGESlXADz5g== 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: 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 Thu Sep 14 01:55: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: 13384012 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 BED28EE021B for ; Thu, 14 Sep 2023 01:55:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E7DBE6B0294; Wed, 13 Sep 2023 21:55:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E2C006B028F; Wed, 13 Sep 2023 21:55:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C56286B0293; Wed, 13 Sep 2023 21:55:48 -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 AF3586B0289 for ; Wed, 13 Sep 2023 21:55:48 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 824DDC0F37 for ; Thu, 14 Sep 2023 01:55:48 +0000 (UTC) X-FDA: 81233536776.20.750B6FE Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf30.hostedemail.com (Postfix) with ESMTP id B739980010 for ; Thu, 14 Sep 2023 01:55:46 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=b0Lpv6VS; spf=pass (imf30.hostedemail.com: domain of 3IWgCZQYKCCoYKGTPIMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3IWgCZQYKCCoYKGTPIMUUMRK.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=1694656546; 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=oEzTBc+DjRhLKf7NebwS7BLBkXRA7c8m0o570Q7bCVE=; b=2yE7wwENJGdzqhczH+remawwvi44rglOkZN7/Nd2ysihsm4Ybnh2XnOIXsV9l01Q0QdsXo n9nSSZSNnHy5ZVbcLWpR5ggKOQMwfTWjyrNRQmWwD5/QniS7fdH07k0iSPzp92QQwHQafv W50AKrT/bTvUD7+AxksimpXkWUd74/k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656546; a=rsa-sha256; cv=none; b=Y9MLiCPNOAzWHbh59n7YqBVM0ZumQNc4aMe+VNiVNZxOC7gTnVhU4j/naC0YQHlM4ywdlT xQk35AsbK0Tt68J6XF8ypQWJ3XSEsBSoXtMdhWcg6LHRNCORCLU9evYsmkFbCc9NcEWSio BfXuZOc39EB84sJqHavirqx6YBZjW20= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=b0Lpv6VS; spf=pass (imf30.hostedemail.com: domain of 3IWgCZQYKCCoYKGTPIMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3IWgCZQYKCCoYKGTPIMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c31359832dso3883215ad.3 for ; Wed, 13 Sep 2023 18:55:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656545; x=1695261345; 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=oEzTBc+DjRhLKf7NebwS7BLBkXRA7c8m0o570Q7bCVE=; b=b0Lpv6VSvGoKtVm5ceBUWOf4B6x2S6OsW4DR21iiUk6CxTDyVdbAwkUDvGgdWXrBLk lNdEsIa2larSrJ68wEKFJwqjoG8230Ow6q6JxerdjeqoOItIxeDhuIvFFJX63lI1IKmc Qm2o2mR35WrbDzxHjitmNAs8HhFxj0VE8NQ+dDJtO2xNnaqWz2gOxihbmuPab4LGTqM9 JfO5P6qSDhon9WNprQB6Uz4od5Wj0OdZvk1NMRPWPSaUYlYtHTAIscWrAdoKsKs6ItxA n8Uz/DAgsp1dZ7SujqWh7PB+/sTDHZ1PXCDm5lJHIXPSFQ1v+v58rs5JNmvUpRkx7Wyb 9ncg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656545; x=1695261345; 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=oEzTBc+DjRhLKf7NebwS7BLBkXRA7c8m0o570Q7bCVE=; b=NyFPZZ6BVtbk3+BY1GPnSWrmMeFuAxGDDXW/W6/fAMz8VomBQ5N5xFivKBblsdVeMg SRRjPKwJAjIbtmyM3IKz/aFWqNjUCiFY4XBFsGhJoTEHFt6xahvFuT1Kh8GQvpsxKb4M Bnk1HMReCgL/tmLSzKDFD+ajnlbAfNMQuFtrlHrfWGOacYQNfVScIVYmN1WcwaUo2B82 RVKA3oukvUKovFBVK7JKZ4XJdehgxgNdRZLwtbUd3WNNCDPm22eh2bAjZCXmecCIhYnM yvUSnYm/hsWhLx5e9YfzmXL65zFdqxr/sXYa2uv8EIViPLSCyO3jMWoeC6RLxuqYgEvn JM8w== X-Gm-Message-State: AOJu0YwpJwZuY1vNLtG+RtX4+LI2BVH7LjLsxL3/m0+JTcTnBvg5L0sw yq52o6utj6ztG67ZR7h1Dj3j9AXJxao= X-Google-Smtp-Source: AGHT+IHntm7+Cf6jIX24jK1AXJaXRvd807TiTvEX0TOOWnFxE7V55dOmRO8z+0ko8RJbayizJ7xaJWYuYwQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c701:b0:1bf:4e9d:8fc9 with SMTP id p1-20020a170902c70100b001bf4e9d8fc9mr148342plp.11.1694656545655; Wed, 13 Sep 2023 18:55:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:03 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-6-seanjc@google.com> Subject: [RFC PATCH v12 05/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Stat-Signature: zxsxt3fm6zdcmpp5qty4sicgzrcmw4eq X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: B739980010 X-Rspam-User: X-HE-Tag: 1694656546-676702 X-HE-Meta: U2FsdGVkX1/OfgyuIt8wtoKHEbYCIX7VFZZEPbuUnyIZtnRq/XuQOiQWgj1Dc4rvW7pia2NE9JCK51oLq3i6Iea4t/pvunvajCBx5AL4HDgYciPpfbLvRdYpmV0XA3wGBTfCHgQzaOiJpTwVSSkYMBosZEIDMpIrDZKdoZXh7xMgTVckmeAMl/6a+73EIeWHk6tAWMojnK6C+/yxbjMTqw4+t/iwHGUdXKhv1HLSFLxF9Q1AIRgmnzTj3DaVO2k5GG9CZbmDeLhe52vtAD8UvAO+OFssUvhUimsl1mQ0AP+/NeyJVgrEjntHQYWo0eah6gcms4O2pWIko22JqoEh/Va/KqaIWAZ2UBWP1a502i/TWodueRRAO+5rC708brp1d8y8dk204hUgL6G6GywZUhDCtgxGvwIWMozys6M6rZypD0kt/dYyLtcWIuug6Iv+bjpts85vBXag80iRcf7emGa0zQEcX9qc539SF2PS3Hid+O0q05aRHT229uIJQf7nIdwS11vOWJQN6ThnJE4pE3GCyhYFDv04pWR5X+yo8JUZIHDZBWYpFwHX+J0KcjwuMmmLObhyBTnZSM7TDPp0cXWxIP5QRvZoNN4TOMxKrXTLMAPx1fcncYnz/y77Oo11BqYuF5jGJW8+0p36nyDd9mfzRPQS8Tws5zzt0HsKHqvoRWSvSuOfuS1NrkPaHwNir1M24qNHanrcgrRgaOCO+WJQspxSmfbZ4iTGNbofDXYy7C2/pj5uB/7T27DFibBr03YAnl6V9Wgrp/t/JKWQxXYzyr67+NXBUXufxovhdjrm6AfiUkiRvj73LhCjT74CofXDXO0hZ3Di9ygcq1MBLjTO4RJzfaAukUqcunkSEJSzIR+QPGlA28a0gILQ8xhgqCqhKaVHR5COPITFw8Pw2q/BKmU0E1YUnaP2GInobAfNVH9B/oEBO5JxsEsAZ6+rGdpDwqUgmvHB8UgwfSO WpMDD8zQ saN6Za2QFih/WBXaBimSbiAQJgZZ83LiJYEiDUhS+8i6Q+HQqxhJKtNR6Pmk4gSGRZ/QTn3OpR5cjnByrgu9BAM80Deo/4zWEL+8EE3CGlXUm0k53u8KgcJsTNPVBnb1ErfBgEdbxhK6vTAe1bNQxDXiIjbdDPFN7DbizHjEKAUmp3Q3jao4F+9wInxFvOCpoU/gZIRxm1XH6ZLslthwZflSKr5ESzI1r6C5IcjgKiQR5Kic+d+GyxSRKGiXoI8zWzx+moHwC7B805AQb4gzZCoELEVdrIyg1OP84KLUefEqqZtfHzwa1vQLT+ryo1g5dHNZ5kTvbsamxhYSqb0KIvQaBBvNZnZpQB2RrJlMGi98a9nAhPV7pcX0Qyu9l9RxsPhHyLsOgHAqCXV7bRTwvCwOnp6pPgQLXSW7lKuA0RNo0wTUjYfoBNLyT0/wnzd8t0muoIzOibtqY91RR6LYFCvL5Q7VIRHgeHfEyIW/O7Kkx9H3nnU4HaaC6KgZonLNtdC0NyUjdpNAEUfGsXImpNhJzH2NlCMGIGfyI4NVTo1vsCjbaT3Sx64bzWRzDuG/O8RCiurNYHoqVjrizMmJ1fMxVJKgsxOntJ0RbKDi4rsDMvQydtEB62FyOb2aK+3RNZrDn/JekERkxQFXNjkNaIRtOrJ3r50Ki63IXMtIkROHz3v/gjuwkB6qW1m+HgqjUkBChgSxi125tGn/X35ywusLXSBUADOqC7flT8RfZclEOklXkRBudz6zERYWfh79CHon1JXrG0xUJ81oQKeNfZzAvo+TqUSTvULniR34Cp3sen7qfmb51Ho9s+QDeHnp6zey5hZu+3sMc3Oj6tFoXr76C+P01Nio9RLyH 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: 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. Signed-off-by: Sean Christopherson Acked-by: Anup Patel --- 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 1a4def36d5bb..3a2b53483524 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2131,8 +2131,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 4fad3b01dc1f..8d21757cd5e9 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 Thu Sep 14 01:55: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: 13384013 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 61A1EEE0216 for ; Thu, 14 Sep 2023 01:55:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 88C216B0289; Wed, 13 Sep 2023 21:55:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8387B6B028F; Wed, 13 Sep 2023 21:55:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 68E476B0297; Wed, 13 Sep 2023 21:55:51 -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 4FA766B0289 for ; Wed, 13 Sep 2023 21:55:51 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 25238120EF6 for ; Thu, 14 Sep 2023 01:55:51 +0000 (UTC) X-FDA: 81233536902.02.5432784 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf21.hostedemail.com (Postfix) with ESMTP id 568281C0002 for ; Thu, 14 Sep 2023 01:55:49 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AAa40jtb; spf=pass (imf21.hostedemail.com: domain of 3JGgCZQYKCC0bNJWSLPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3JGgCZQYKCC0bNJWSLPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656549; a=rsa-sha256; cv=none; b=dM5d5ohBL8ZTFcs7rzEqB4NhlD2XESA4doDvjHuVn6+xRF+qtUXuzya4UvjGxCPUi40hpS Bkgt1dlfLPUR+skJF/Wm0CsiEdC7hyEX4nmve/dwukk105XJF2jZbLez2Lk0BokHXZ/G5w TdX9+ew1SPTnthqnl2U9AqLh2XOaI3o= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=AAa40jtb; spf=pass (imf21.hostedemail.com: domain of 3JGgCZQYKCC0bNJWSLPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3JGgCZQYKCC0bNJWSLPXXPUN.LXVURWdg-VVTeJLT.XaP@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=1694656549; 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=BhxGep0GRUEwAUAzFvnYRN6yWfiwAK/IRlQcI8pBhpQ=; b=lQAvpHPUMufXjOQeYxCWnD5M10fNTIwOOCKSlXWdKNx+q1S8LVMINn8QIL2Bs5/fdjzZZ0 ZHFyc78CUF/sfRF9oOFOzfXLl3JvBdX6godfvMf+cSbLnXuYs1MtFmFVKDvK8V6xXhcIt5 gXtBRR619waryysIfc8Gi5D8LgkpkXc= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59b56dab74bso6570407b3.2 for ; Wed, 13 Sep 2023 18:55:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656548; x=1695261348; 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=BhxGep0GRUEwAUAzFvnYRN6yWfiwAK/IRlQcI8pBhpQ=; b=AAa40jtb0aSHjCvdffJkQL0wAExm3+HyzUq7UOcxEItWrPJfF3uZ0JpBSi/NPvO7kA ScMrD5bC331uFeIXrbuzS1SLYu/4LMU6zlk4fPAK4NNDsihmHgTZLXF4iKEvvnxvLZBY 7WzJn8npVvbTzqhJaTVnlPbVK9Uh1kaaIQak+f3KUKgeALwiiYaqohgG9s882XRmrjCm kA1UxXfXJW+L++A/7G4RE7KifKh66SukGcCQwULuFoBfiZuwBbHj8YN5bRPFYoyH7xkd g40+6GDWJC6aphU2k1MN9igwidFDlhLKe/rbMsk8bvz8SlrWR2uUG+M+nyWulWjs8jOj Rm/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656548; x=1695261348; 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=BhxGep0GRUEwAUAzFvnYRN6yWfiwAK/IRlQcI8pBhpQ=; b=pEbF/VaSXFYv4eQVOgV/3gRiNBpWPnPdEOYNmtROjBdzpkkCCGvL7FoRWFdV1WYzlj zKuThYvR+bO/ZHqDlFEQnhB/yjLlojkIQdyUakjdcpRe/H4ttN7uNv37Vj5cQPq+4noo YEOcyKLwRUprdJMuXmBtt1MQ/JxZDH6n7eCqK5NiujHNGESdtXfAkLk8Qfl7JECx32Yb pEHwXGrTdYp/zOSVYckeGIHWXZa26z0WIhCzbqivyGPxncrLCpqbn+YcL099/2YjZjxY Xt1ah8Vdna5lvZDAvQ22HlNCbtzJuy3pXe4HkivDTIQ3OW9zkrc5iaWpZ8uA7jow3k/C x7vg== X-Gm-Message-State: AOJu0YwiWGtUSleKtbD5ViBQqqAk56/SsFFZhvOmuhlpTqyL8AU4As/K O/37U0Aa/9zyvS/JYWXX6ImeZzs/7RA= X-Google-Smtp-Source: AGHT+IFIu1rjOq9FW+JbDj5fY4BKSa/epsGGWccSSMIHRlmCarPy2ezJLxiJZod49VfvzwgQV9RD5huQPI8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:4207:0:b0:d81:536d:2978 with SMTP id p7-20020a254207000000b00d81536d2978mr74175yba.2.1694656548405; Wed, 13 Sep 2023 18:55:48 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:04 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-7-seanjc@google.com> Subject: [RFC PATCH v12 06/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 568281C0002 X-Stat-Signature: yanac1ashiuasc4obonzeotah4bksdqp X-Rspam-User: X-HE-Tag: 1694656549-885707 X-HE-Meta: U2FsdGVkX1/9zjuLB7WC91g7XCsQvl1kxj55S4zhqzd1hvFz+sPafJ9MmlYK26w/pOkUG8WsQkGoy84Mmcq1WsDk0anzc7U3W+ko9yMmhYPR3RkBjo8buGSFdm1yDdNgtxbf7rpeO+GosqyTMn+909LqNHGaodLBu8bIlo9XwPU1LlfdJYfZVkZmgWRQrTtudifnjAmziLGWA2vyoVlE/2OpvftIYo5edQov/NoL8qsOKDwWebDX1cNDAPEaBvr0t8TiPu2f+wM0GTm2UiIb/Mib2ofyLF0K/3vnTMzGELohTImpK8QrzWdOJrwiuEXG1nUrMxqzlREc6tY7SBEaFeYH0RVzzTc9+vNl0EK5cNvQUcvy1sh20jluZQXUWKcfGnS0vK3bqpGjT9tmNeIwGy5gcGjIegZM0JXsrTMJLL98U2mRpFRdxZFV4BRZUeFBrEhz4Fv6IyAmVwE5t1cIbqlWsTX2psy2tWwS/tqzimS8a4TJLxH7kTXT2xIHmGI+M39g6X8AX9VFpMcLlHdgVlFFZeZefQMOJKbZOF+5hWIe/kBbEpxDLJjrKcF1p2BWUmeL6dSAHyqIeInNh5Ykk20rCpYE4oV/KqRPRZwlX4PW5ljyWokMEU5GZRKg2jD+eNm05sOkIpcRPoMyzxgjv1TMXxfygajU+PJ9yM0LF/Th5kA7lOO21FB/6D97IjOODpwVQz/CzmfLDLLDly7oiq1FunNkUslm6dJkOAg/5pRfwfG5JAwEoiWc6tnNwMpOErZAYwAUb9oUnC8T8sd9tIq8PoewFNnuCu++LZYjIBZm9vNejOaOWjcsPIp4QONW5qj2YelJzBIyqyskeErHtWXS1idZekxrDfER72W5PEkEX6x9e+C42DUNEydH4NTH+GoLE7r+XWFN/j2n4qOM63T56u8MIBnvBUEqj98FIiunpu8e8LRwGB7YR7MQTpRDc2XAUD8Ep+Maqz4tv7b uZPL0UFZ pZkStgSDmJ9PHbU7vYi6pWczR1WcVlTqO5lzHdF2fCARx2+4dzg2yawSWRTyGeDnhfiuu+AskPImHE6+AS/Tp8nuuJX4PWGTghgakf4FUCCNTje7R0Aol1ybTNBMZtpJieb1yXd/2Ey4FtHsuY4uhkek8qgn+mek6BX0gY8gqEqcKHwGE185OyZNxwKTWClQZ90mLO9jl1OoQmyeL7U2EfQ8o21WfaKkSx+0eRzMbNKVgUpQulUiJ6cFXFkM91n4ypQaHlYn1afVHZ5bPJ3KIHYE2jGPfXsIv79vm3BtMwWJ+g/M1sWx67FJwj/Jzx1ShLCfGGbWi/7kIXFyFVeQhPkWvAUpex1moUsOiW/UuEaeWF/9K6VCVHDaev+rHbO+1PUKqu/Qvg1Hkf3SFlzwx9kuSZqDT38FEDrbb2iO53DEIAN4vUWi3Ao/jtU0kc75cwZE5eUSIOz5Ns7m5Wu14J5kUOCf2Lkigw24Ya7ha2CdWF+gJxAk06DNwp8WP35O+/qyHboBGP+uB9UN+focbvhkAr10OqXLH3lrEnsvEDbNeoFJeeLwB6dSh58n5+1XHamMPIc5in03laSMZPLlbgt1GEW2+X09ExC3O8XtyPjWqN38JqHMSwOyo6749jPRYuWKMBFW97SH8KOl6jeaa56YhxbUh8edx3u5NnENXErK1XiHO+BueF5mWOM9eXVbfYXK8E/ArjhlDoF3vURRKDEststUReoUoSvM9w+IG3Ycg7GmIAiQPsL+B05u5JNykl7sujSr/CPVwQjkvGW3eLHW4Jij4aKxl3DoopaZjVNpBlVMVm39PZm04JPuZSZAD0GUdKbHxV2Ycuz9iYvCT9+iUQQKxDQb90zseSO9/V2K3ChoILrCGgjUYvEiLDmR28M2jQIiKJdn4RL4= 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: 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 Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 4 ++-- include/uapi/linux/kvm.h | 13 +++++++++++++ virt/kvm/kvm_main.c | 38 ++++++++++++++++++++++++++++++-------- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6c9c81e82e65..8356907079e1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12447,7 +12447,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 8d21757cd5e9..7c0e38752526 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1571,7 +1571,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; @@ -1973,7 +1973,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; @@ -2077,7 +2077,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; @@ -2089,7 +2089,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; @@ -4559,6 +4559,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: @@ -4814,6 +4815,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) { @@ -4836,15 +4845,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 Thu Sep 14 01:55: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: 13384014 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 48B68EE0203 for ; Thu, 14 Sep 2023 01:55:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 951046B028F; Wed, 13 Sep 2023 21:55:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FDF96B0297; Wed, 13 Sep 2023 21:55:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7779E6B0299; Wed, 13 Sep 2023 21:55:53 -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 601F56B028F for ; Wed, 13 Sep 2023 21:55:53 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3697DB4209 for ; Thu, 14 Sep 2023 01:55:53 +0000 (UTC) X-FDA: 81233536986.22.4106033 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 544F814000A for ; Thu, 14 Sep 2023 01:55:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=F+eEFcb9; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3JmgCZQYKCC8dPLYUNRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--seanjc.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3JmgCZQYKCC8dPLYUNRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656551; 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=o3ncWp87Xrx57ugLKytKkEWMncW9euHhZSqjHZ5fZmU=; b=uTo1XvlKLCaqmdDsJCmLfVAFNMe1ppG9VEdIgPBzVUwVZh2oHTtV72h/i2upN6vWtI7fr/ N8bHHvUoIEZNQS16rAm9QQLfREfDW3sxBWzDBfzlPg60KCIeBoFaBUGAzLL1yh/t0D4Tst LbsVtUXv+4c5OdAdPu8mvmGxYw2JxfQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=F+eEFcb9; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3JmgCZQYKCC8dPLYUNRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--seanjc.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3JmgCZQYKCC8dPLYUNRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656551; a=rsa-sha256; cv=none; b=kOL3S9ctLW0efn05/tsBV5CqbKNhHna5qe76qqGLD0olRuNwMbi5LMmIyy/waE0uUM7v4b QVr0as92RuTu7eQseehLSbsjHTbMQqvvsoGJHmGOt8cJc0JV6jy1icnaSt93WHBZJpm+Yw GApdNhUlLP91RmP0QdvRbXJI8P20LCc= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-26f6ed09f59so387953a91.1 for ; Wed, 13 Sep 2023 18:55:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656550; x=1695261350; 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=o3ncWp87Xrx57ugLKytKkEWMncW9euHhZSqjHZ5fZmU=; b=F+eEFcb9PIqfNv6ff2L48CzHszpRS+uaHc9s/hlNGutx1BjJN/rbT/T8JWdkT8B0W8 TzpKZAI5AVz5m7aVTJaJuFdyJMB5zrXKdwgOGWU98ZrDQfOFwLZhuVuF3KLcTtabomdq TOwSge/SonhJ4rNGQQVSRiDkVGVHeC6sOx3oUtpfmXTJyf3fKoYH+az5hFUond8NuZI+ fWIo8wp6LUuS2UOJHEpsu2nNisp12SL4aNOHOvYjFTmW5oLjFNXHkC12xcvCR+w67Dxa v9hhZwm2B0sFSTxbqBakBJfzgk9oxKXWpP628vXcwE/DwGN6acBWNNhbpufA4NzYlzm3 QasA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656550; x=1695261350; 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=o3ncWp87Xrx57ugLKytKkEWMncW9euHhZSqjHZ5fZmU=; b=p1B06CV7+hbMfkMnuHPjCsT0HplcaoIJuvlvba/rjTRWyC6ilX1fEEVgeN3jtjwVrM eld0kibhRPIX1pHnEqfU2Rh5LWXfUHek+VQGhBpQxjZcd0QzCLOCw/2oOOxQBYpE8mlR ZCwAE8I30LkAXUEpl8fXKACwg6R55qHJngMmTw3PvgLyPZR4lNfpg1zePs7h/a6yLOMp 6Q3nR+Q2nsfHzpLQSrnu1eeN9wNAfJ5zBW5GznAFu5uJVdYghjGpukZ5VNxRWwueuEha fzQJUOjoVr6VAsAmjVmQXBh2AWb13a6acUovmjDvy/mWA1Z1NKZvO1Tvrh7Z2XlTExL9 u3+g== X-Gm-Message-State: AOJu0YxrM8UuvqVC/a4mCxIacWQQ1FoqStBHSjzM/6Zn3cIHxghLZQ/5 OkVd5+iHnyJB0miDBKz0RlDGPQzX9vA= X-Google-Smtp-Source: AGHT+IGMCwYjc3KV33cqzPB4diNP6tSuJVwUoEl1ZPejd5zU17fdiva4810hnuR1hhtSq7kLalb8uulnWEI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:6bca:b0:1b8:80c9:a98e with SMTP id m10-20020a1709026bca00b001b880c9a98emr149786plt.13.1694656550053; Wed, 13 Sep 2023 18:55:50 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:05 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-8-seanjc@google.com> Subject: [RFC PATCH v12 07/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 544F814000A X-Stat-Signature: mzamneiz6kckziagdwnde85uppob7cix X-Rspam-User: X-HE-Tag: 1694656551-348228 X-HE-Meta: U2FsdGVkX1+JV1uxPzid0iuE682Xvxgb36DTy98qnqSHqdszcCLw93q7tRZV8eGjUkUnPcw0+Lrif7a5KVFpLKKPGITg5OpipFVauXjWSIxdA+Wp/BDgLOEqCocl6Rtqw9NzSGeQl/QURgyZz/odBLO76aFQqGHA7oLM5I7pg1yhhveu+e0/wAOdX5yaEbWu6zqDneXufRw78jA505rPsFoyMUCcHfR263emJaYhQIe/SGd6MmWy+dbllIUh6NHonjOu2gLF24GbKrrSx9GHPUKO9R+di9YPOjhTVugiWDMutHEMD7sehXRMKg8+4VJTBjo4J74yWu13d7jjqdW+OzBAD+BtCz3eBYE14EhiWsB7nAEP4hsr1UplOW2BU/9FRD31i6pnmlroUfx9yGcPSeKnCbNr9HwDYtihvA/mcAb1uUoW7/ONOy7OtvwxmaRX0LoOwb+e79BEgsBJXMfExKYd0Bc2a9cBSrWE8Hvwh6VQAM54ZCSXkQLQ188LjsSVHv4rdBBLjyyZEVfeMdcEbEVnk4XwEJHGgA2QIX4habgzjmtg9QrRU2n0p7cYQnp0bzlhPFJ0qU6ZaTAOX8lGAN5v6wOjJZMrUy6TQtlul9pi+7MPn0fKst+6MD+BeOkS/HIkhcfgIHZj0MeGASHrk3OVAhM8i7WlKwGtUIlkASw0/9WzCqbBI6NmIR4Ydq1gI3cYrbD6sLJREjyCw/Yhgin9UH8NIuzDgL0jN9Oaw+jR1TTCP4hbVb8DcjEYyYOq9yBbvHtLRRMICC15Gi2sK9JFYS8n4TxTYeGEF3XiKx+pMl/S9OC6Di1QXgb2xqp/pi1fXgJaLnLhVhGYIVXiP28iGsOak+I+BKcIDaE+M/XKfFwxdQ20NZUj85WjyhF38o2C9JLwRlS1aVyxGwp+JzhWjC4ObR6p305HHIsUHggV7XfxEW6xO+8CWjyoVmTaPoq+rWy0p0d6s+FloOK 1skF2TJm hvUSTvTdkJh39m2ghnCqTb/S76XSFSFBxKxS2sw6eCyf8+vaPEImUo8VMVxHWZTFL+b3Nc42DruVT5KK24/GGWXVheuf6vaVRqRVHJjIGM2HOtxhwLn33a6vNegmyR2vDp65jUaDCjTMBWgslpWAjjvlsRWv5AQMGpbh+ThTb/IOjMXoYuvW6LGBXEqyoBhntu6/63bQRTxm3c74E4N5HjNIH5/6v4tpP1IdQj/lngkJU7DMRFc/QE0Kge+yDz/PkWyYDuZ05w+r3ykj9101zFpcizL1SAiNOT9OFprKEpP7DqW64oEviBcM+axmkGNvmdk35t0EArZlmgR/CLFtWxdjBcP2XnjA9LwKdCnkN8ThII4OevEowAmNY10vmiv8a46Cy+eGdxxSaSJEzupf7np39zscc/8/UUKLd04o/ncrO96OKGEfKssdhJUYXw8CG8hmL0OrJ7kbcyvAGkBu8lffosUZCpq/0YsKjQsKBtcGoB/Z2JC3GJ5aP8ijkUp5HYe0AWIUc49UXWmv75sBZ8S4e848ZBQD/xLozavxxQue1Wb6Yd31T92O73tVkEo11o8NMJs0n2DbySYEjNkanICgR/3g5Avkn3r7npQXd3pdmuq5z0Cq9IElQJHcdQL9BTrNu5cqVGhBOy80eC9HsTryZaqNpVo+zgB3VKhaVijcJPqxY51KY3xHStYB4J33OE5aLXHv2g5CY+GwtRM3WLrArJ2IYSG+pi2+cZ2YR0fP7EvyNNXdxUGn2ZypxZSVokXeT39IdpcyUakG2z4HvZ6S888KfpNNA7JJnzS+Tbu4W2EelcoDAj9NT+qLWtydmg9ZmWeMEDhqWzz6NoZU2H4Lj2bEJmgzierzbwb5GslKqU77ugFfXPbKXjJM3PUQ6ZJTEb1H/owjWTzuwfgJcEul9KoDMic9GcjeG 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: 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. Place "struct memory_fault" in a second anonymous union so that filling memory_fault doesn't clobber state from other yet-to-be-fulfilled exits, and to provide additional information if KVM does NOT ultimately exit to userspace with KVM_EXIT_MEMORY_FAULT, e.g. if KVM suppresses (or worse, loses) the exit, as KVM often suppresses exits for memory failures that occur when accessing paravirt data structures. The initial usage for private memory will be all-or-nothing, but other features such as the proposed "userfault on missing mappings" support will use KVM_EXIT_MEMORY_FAULT for potentially _all_ guest memory accesses, i.e. will run afoul of KVM's various quirks. 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. 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. Link: https://lore.kernel.org/all/20230908222905.1321305-5-amoorthy@google.com Cc: Anish Moorthy 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 --- Documentation/virt/kvm/api.rst | 24 ++++++++++++++++++++++++ include/linux/kvm_host.h | 15 +++++++++++++++ include/uapi/linux/kvm.h | 24 ++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 21a7578142a1..e28a13439a95 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6702,6 +6702,30 @@ 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 { + #define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) + __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: + + - 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 +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 */ diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4e741ff27af3..d8c6ce6c8211 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2327,4 +2327,19 @@ 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, + 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; + + /* 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; +} + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index bd1abe067f28..d2d913acf0df 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. */ @@ -541,6 +542,29 @@ struct kvm_run { struct kvm_sync_regs regs; char padding[SYNC_REGS_SIZE_BYTES]; } s; + + /* + * This second exit union holds structs for exit types which may be + * triggered after KVM has already initiated a different exit, or which + * may be ultimately dropped by KVM. + * + * For example, because of limitations in KVM's uAPI, KVM x86 can + * generate a memory fault exit an MMIO exit is initiated (exit_reason + * and kvm_run.mmio are filled). And conversely, KVM often disables + * paravirt features if a memory fault occurs when accessing paravirt + * data instead of reporting the error to userspace. + */ + union { + /* KVM_EXIT_MEMORY_FAULT */ + struct { +#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) + __u64 flags; + __u64 gpa; + __u64 size; + } memory_fault; + /* Fix the size of the union. */ + char padding2[256]; + }; }; /* for KVM_REGISTER_COALESCED_MMIO / KVM_UNREGISTER_COALESCED_MMIO */ From patchwork Thu Sep 14 01:55: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: 13384015 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 B730EEE0212 for ; Thu, 14 Sep 2023 01:56:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09A086B0299; Wed, 13 Sep 2023 21:55:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 04ECF6B029D; Wed, 13 Sep 2023 21:55:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDFD46B02A0; Wed, 13 Sep 2023 21:55:54 -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 CB6636B0299 for ; Wed, 13 Sep 2023 21:55:54 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A7F8D40E86 for ; Thu, 14 Sep 2023 01:55:54 +0000 (UTC) X-FDA: 81233537028.17.724F8F3 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf09.hostedemail.com (Postfix) with ESMTP id DE8BE140003 for ; Thu, 14 Sep 2023 01:55:52 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0JbeIQyc; spf=pass (imf09.hostedemail.com: domain of 3KGgCZQYKCDEfRNaWPTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3KGgCZQYKCDEfRNaWPTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656552; a=rsa-sha256; cv=none; b=7zcNNRM0XEqgT3FWwgnHETvDjxoMcQ60x3q+HAQRu1FztlLxGaV/+LfJHSxUytYl43OtRR DJo4wtTfaNKYByegXCwaxoriNd9w3QmKvX1pPWBRwaohJD0e1+WtiOHWd9VVhG21BI9+XM N6Cky9ljwKkmz2RxGFkjxfCvq84f+BQ= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0JbeIQyc; spf=pass (imf09.hostedemail.com: domain of 3KGgCZQYKCDEfRNaWPTbbTYR.PbZYVahk-ZZXiNPX.beT@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3KGgCZQYKCDEfRNaWPTbbTYR.PbZYVahk-ZZXiNPX.beT@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=1694656552; 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=c0gbqnW9KUovCg+coU7ldlYhYVFXBRbmaH2jYlXJSe8=; b=0B7wVgW0HV9chiIy4/5Eidms4YCUylXnTiU9hARoJqEJx6INWz6UgEtSVhSPmGsEWAoxEf yFyMJCrZtyDrhbb3JwS2qN9C8tgnh6M1WcZ4yFOv38mnCp9A3x5otmvXgatjDxa4FNnvIU a0YbFZwlw1hDUtUhM6vub8S3bYDPZiw= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59b56dab74bso6570877b3.2 for ; Wed, 13 Sep 2023 18:55:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656552; x=1695261352; 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=c0gbqnW9KUovCg+coU7ldlYhYVFXBRbmaH2jYlXJSe8=; b=0JbeIQycHs7BZ2aCSIcNnvfrqMKRnixwKzzFQdOfw0tlcEtJS3gTqFDrmeiI4daCfG Zi03vQ+WaDZDOBb9k9kmUdaXiFli0QkmfHTRYYlhkjB/+9Hcc2Wifx+yd+OawzIieLEZ m6g3dryGNLHGKL6e57O0+70PG4pgYlCrR7Dba21c8f7BK9itG870O5b1ojujcAs1qgOE H2l3ZQiBhMtUu5agu9TVkkMhx6rF8Fkeb8YFu3lx6twwWzE0wmxkRbGVXywWX6vaVKsQ JBHTyemjHw7K1cSgCpMWTYOU4FBLIhMVi5/N7byl1i4rqL18aLtj4aAPaZeNebKB+R3G bPPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656552; x=1695261352; 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=c0gbqnW9KUovCg+coU7ldlYhYVFXBRbmaH2jYlXJSe8=; b=YmQ7/6XGcQO9CMlLfFIxk5oCdmJKJ7mTaquAqfTbZedPn146kGZd8ySXquQg7CCWnW QyaTRRJ9dumUanXPeKrK6SjER+h2YADjE50T3IwDL1KGD35Gr3jbSdB3T43BC/t0aBH2 oXo4WJrmW8lGUzN00v9ST01+i3+ym2/1Jcu8hZg+0pg+Qqfu87zyjud+4WoUpoCcAzz1 I63FAs4h7nrJ+4oS6dK8oXxpVe4bQSo6BqIl6c431OCnatLXTF75EeeyZDChtDvVJEAV EApm/TWFgcPqfkt0RUF0a8M2CaIR1mdYjWRTtbliCda2LHpJEvtyoaUGRlZl3K4dCzB3 Pp3A== X-Gm-Message-State: AOJu0Yxv9hb6wqA6V1ut40Rc8XCzIEUTr9a+bFM8+yeAKnfyw/b7/7ze n+0WphV59Z85W0WneBeWDiquZCRLKWk= X-Google-Smtp-Source: AGHT+IG9pkaa+NfBfBN3tv0PMD61sScfAedWpdRl9BNgXYxxGW/3XiB249I/ScBV6f/2zKuHHJhj18tIK2Y= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:83d0:0:b0:d78:3c2e:b186 with SMTP id v16-20020a2583d0000000b00d783c2eb186mr79839ybm.5.1694656552108; Wed, 13 Sep 2023 18:55:52 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:06 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-9-seanjc@google.com> Subject: [RFC PATCH v12 08/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: DE8BE140003 X-Stat-Signature: dewzyf7fopfa8bkdfnhb61hrtn3ukn1j X-Rspam-User: X-HE-Tag: 1694656552-427043 X-HE-Meta: U2FsdGVkX19xJ+VOgsvFOR+Dqz4X2GdLdFGHE3yMOawC30YnO6lXynS6upbzYSDKrMjqXFNLHjuZ90GYnhk5fCYREedwDHimjdlun1J34XDHYsijG5s+5uuvmjJqqCnmNpY8j9rYduuhVuGAsQ1GRQAEWaHzwlCWS2kb9PSl2XlStUmzWrEkeuOKXoJimJT8EGVoSA+Df3VOeDpWIWpUqhhEZTYTr+39yurYjikUrlQtZhZiyuvi5Yb3FiUafr+1Yp1MMhvE3OmyboPqJ4FxH6qAtVHOINeeWCfkehuKHA+IfElr7Kyeedl0ImZUA+MQfGo2ORnc+8oZntg5UGEdMUIPGQINzt6dEWpYzMaMdJ6WAL4BGDDQ7xVgrhnNe/nDTHis+02ypt0o7zez6VQTS9lSlWFkQX6Ruuz5DI6ClbM236vjZO0uXuW0vaW1uP9TQDLiE8852722H4E7js1P1WawKjZyNEMOJjg2OrLfJPBetaMr2UgPjVa8YALPQSJclcisnRKAJsZ18ZQsm6Ww9T5fHzQRroHZYLHGzx047nntI9LJaXomLskkP4KsN6p/TcfZvpLyOplAuCxutlY70a5CevjpXdNZmDXoAK0+frsOiTMD9C/v+FtvCRxr7jKK70Ewyc1PKvfkLEZ+Ft5c4OCTfh+lzh8JERdU1HOXvc/9hN7YpiK3dhBntqPUO6AzIBd92HrIaru/YHKKjqgB+STqpz7yU71IOfft+yH0dy/FohmTAa/9ugGdDvyayG3PZDDx+t7vw8ZBuVPPPm+n8/2Bygb7RRvO8fPbAQxH3Zv9qARBbw8NJfovKT+9CJLVqQcQ8z5Kqm3Dlt4iu+riLKRX1ubK2KTtVJATQh1E6RP1kAydQm+9XTNnMdorQHrSfLCgaFYIko+Vowkc81fsFENhFOhL6qE0AUXTqyb99NzDMfjH/w8z5uEk2bdURaGQ84cDXrkZpzkAReKNH8i 8wLWXmBg mDNosOtmatwfDTskUFHd1FaXuO2WYvIgAD4+yzf047s6+B5qy4n7YHQCebQ46uc8CFVLOZzbMyZKRamf/2GhmAO1GCHTuda2WDfa5tOp2eVN8DHhRo5EugnakCzues9cwJ+QH6NFLXyW3MaxgMq97cCcCYElFBNi2ZLvvXnaVLavD+6YX+/cFE6H7+jXtjN8zw0S1on4xwjvTgOSWmG5B7CnpP3i1Scw0gfCIZ6oPUsCO0FwpRiC3Wlm2nml2FjCkFWYESZAntVPThCOu25hbRXNXnnch6SICLGb2hvB6PRzRy3MDuMM/3AtqgHhRwC6cgcDuspt4K7nyM2Hl0I7mYzmJEXsWmUWmJvtH7XCZwuUrnRMvkbc+3TmEa3NEPlW8Uhog7vMSINoFMMCP/7hB+J+p+/pWtVj6WZ5HOp8Hz7zjx1WBygxCg2eGXlr/K1AZCauEsWACNfOVikXUC3Xvufa80+h1WDwrIznp9Piwjp62SilNba/AnrcRJMV8SXSD7AqZRJDph3Z6XisypBy7U6Cui5wSl4msnuFT+9tTUbuYkYvbCtTShTkxgBpkUARegJ26MpObN7SChQOMFsLE6ZhcNW+KaxvSTYd3IibJ+xDNa+kXHpCw/1DdFBP6VYDvmI1mNmeDElfoJmqLMoEzwN+7zybocsbjo5/euk4ynBv5xENuJqIud3p3rFbemMzsXmLODArTfjO+eVWsU7Aoe7SCnCsx3snjG1/dT0OO1zmQVt+H+PcZJ0+5MjEViUWuhMq2MDbA7m/5v+IaSprzZL8ZyCY6dPGo6i9RwkgvIyWHGQHI+pcLX6z2DA== 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: 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 --- 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 7c0e38752526..76d01de7838f 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) @@ -796,7 +811,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), }; @@ -828,7 +843,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 Thu Sep 14 01:55: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: 13384016 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 47254EE0213 for ; Thu, 14 Sep 2023 01:56:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 704FA6B02A0; Wed, 13 Sep 2023 21:55:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B4F06B02A4; Wed, 13 Sep 2023 21:55:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B6AC6B02A3; Wed, 13 Sep 2023 21:55:57 -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 352936B029D for ; Wed, 13 Sep 2023 21:55:57 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 11A8280EC1 for ; Thu, 14 Sep 2023 01:55:57 +0000 (UTC) X-FDA: 81233537154.29.154AB1E Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 494CD100018 for ; Thu, 14 Sep 2023 01:55:55 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=JG67F76E; spf=pass (imf14.hostedemail.com: domain of 3KmgCZQYKCDMhTPcYRVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3KmgCZQYKCDMhTPcYRVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656555; a=rsa-sha256; cv=none; b=v0rc+I94ff+HJ/u4MaDap0tKCdJ88a8l8iCCBxQljuaQEOh0jASa29Tm+U0zLtE6QppbXV XQxp7MejNZCbrvQv8QNdJ2+gid9dpfCkGmKn3anXqsGz8O3HfHuwbHISMlIH0+qgdxtCPC 0TLfeQHBQf3CvegQnv9lQfj/Ahh8PqI= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=JG67F76E; spf=pass (imf14.hostedemail.com: domain of 3KmgCZQYKCDMhTPcYRVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3KmgCZQYKCDMhTPcYRVddVaT.RdbaXcjm-bbZkPRZ.dgV@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=1694656555; 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=snuesVO7Pgb0vyCed2d5Ea1z3YHTBEL1EKVJI8LHRYQ=; b=a3v57WnLSwgtwMZ+mNfHNtnrWqAsY9Ra+A1+IVm9tP/ub5EUuPEXIvniWBW80onotYLYPF UXahFw4pzZ8vF4l525fCQ3aQP8mfhJKB4QT9Gbot9z7X/1XZDN2TTvQuizbmICI0dntezn 1rBie57HnA75jxNAuuYgkGrNFiH60Qk= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c08a15fcf4so3864675ad.3 for ; Wed, 13 Sep 2023 18:55:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656554; x=1695261354; 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=snuesVO7Pgb0vyCed2d5Ea1z3YHTBEL1EKVJI8LHRYQ=; b=JG67F76Eoc0w+9Ln2a6HnvV//GejqbLJPVtGZo5Qi/79mntK83e3PIUZEvNMAFn3H5 h0b/eWj5JXrXY6Q6LCjDX47DqQxyFmyGJUxj6bEq0By6sW1UIsua8h6dekx5fMJjzC6J 6eajylvgf/LiIj3sRTYB6BCKB7iyEEeW/O255Un3LfqjApc3AdVU5hWV5Pun35SXN/6B 3wBePewOwBh7KtN6Abf2jFQoWDeTx3vXWClYIm1nB8A0mzFFO/LvOk6Lpw0ha5FC3msx PsvLmkbLfKIy3HivPMAQdlstVy8t5xBJ3qxQ1rCijpC0Lhyix6reS4aemHzUEBKmFJvs te0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656554; x=1695261354; 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=snuesVO7Pgb0vyCed2d5Ea1z3YHTBEL1EKVJI8LHRYQ=; b=HW32/4HTqMjwiiITdpC6C6pqSobYX4SPcOIXLdZtMdKv9Gsk8GnQIcq2bOXKrVLPWj us0WoJGFOwCQagUqpL2i7tmD4/aTSXNYGe3Xt5YE4jIPIAqhkolSZe2O1f2X63vi1Ff3 toTM+HoDH8P5DoDP8C3I20V+No01IeM2yMrSGySLL3rjH3AYnbn7S4GwSiQQxEYr/Fga lzantRi5HVuShj8Rjqy6aESxtb3xU9CveLvMZ30D5jt/5CCeZFh5RyIsgc12l3Cssgzl aWaaBEKDvx/v+s9U6iY1JkUU6jETIb7911cwQMBGeDuUVUhZYvW0zkrriomHeRotFTdn Un9w== X-Gm-Message-State: AOJu0YxYHmmf3q2jPq/Zum/h0H48Z+pJbC2k537wsbGrwearFler2o0d WRT73ItjXG9b7yNiz+HL3N+doqiGkQ4= X-Google-Smtp-Source: AGHT+IGilm0IehJXdYbDJdwfV4Q368JwTuC1tx+vxoEF5ebf3gbFa0uVyGZgeqLtkzJroDu6r4LDVxWZ5mo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:6bca:b0:1b8:80c9:a98e with SMTP id m10-20020a1709026bca00b001b880c9a98emr149790plt.13.1694656554156; Wed, 13 Sep 2023 18:55:54 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:07 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-10-seanjc@google.com> Subject: [RFC PATCH v12 09/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 494CD100018 X-Stat-Signature: uo8t9b13kef4fqb3zyjnh8xcrc8s94pa X-Rspam-User: X-HE-Tag: 1694656555-200715 X-HE-Meta: U2FsdGVkX1+2OAwN4E+SIqtu8nrgjpJGogA3uM50p8s3oKb+29VBZMXgPREsUMxp6eGEIfgfsGyXuYbRIGNBVoU+S1UK7qsHPsChW445t8fUtWkmte756f6DbV3Agfd3gXj5kovEUTtF7x6O4vFBqx4eE6dEmbT/nZJCCY1FcMyrwLAoDe0F7AxbYOZAJKzZ5V4aMBlcG4nsy5Aa4uPNxc/sP18d7Q3UHxQ6H50SQm9RuWwoFmERmE0cPrXWyUiQrMGCSk5RU5Rsx2L3aDbeDsZzfJR6gOAo5ikYymorbXjE1fSU4FFQIv2bfYg7QM9fVJiqqRBiby8ZGEIwbzRSK2aWlbjkSoZnjXaX3iH+TKtZVWv719llBzkZXrxoc2ZfOXa/rBAXwpMySXoPpDaf181LxGFBWhPhixCsed/Bz1B67NcgB8SiGV+WbaXEZdC/mAoe3TAUsGLlk9p9s1M2lLLengU+XM7Td9OkOPt0rV7zIQ2yO76OCIeEWOUCXKUYtUostH8kaM8jzKsmORJhoROCYnhfIvn9LWf09O7GCkzRfLCylrItWFDecYq1z8FivshxidJ4sD6GzLfm52zARWaFKmb1aTLiMGjdzh9M+DWmBRcZKpKSYBJj7WS3CTmUJvgWPSgzHXBcZtJITtgN4gAaWZKtqwneJdCXo82teg6uzOJkuqIOPLbtMv8PiRYiVeQdpI6XWazCJVp5LbbuEhmvGw9sguknJnCktRNtsXch67hTNoCWku+zWnhyJS7O3WA6n+yD7e0CvaIVSKwq6o7F56XmE1vqeCVIkssF2+TI5vwRgIxFfkexc6SNm6Wom1j73IMFHJbwe81u4YQegS0F5L9va8mnARu3m0KMRC/8nzX5GPXfyHJ962H8hDY2Mule32vml5GBjREH7f3zxGDM9yb7i1xw8wJkraDf0d2zzYL15Q2ib0NaPMavCmdPdzr5RFU1I7I9COAH//2 1cWZ0VVe d0mQ8AjpQ82tg2u9FrKlKa4HyJwunn0eCYYQ4twk2e+dqsFllKWn3fEwlnGs5paYtWXtf4oHkV1iGcKqg548CMEzf8ggy8Zc3uBCUWhZ6k/SfDTBlkfMiDay046ufdMTwZOklDdtDTcevEI368BDIuSkfpy+TiOz5rdJU0MKw5YnFEqQzT1nJ1u4LLr01hFDsVACS5okYugPEvOSD3XlgRC3+V+ufguOz70bY3dub9Gg+JFtsuVKyxQEBYPkQ11r1qiZdkB1G9VB01NGXYwG58bO1cyMPnBcvQ0XlA+4ZOrZqcSt1o6Rw5n0WFMVbg9q2T2xpx3VOyqqTSAouUIp76r+Ci5dFHLs4vV5PbwwMiWR07loGRQKQVebxw0TrBZiHwj7k01LpknXxU9r3icV7Mng8upqs6bwgw/+0f0CnwIXMhhmfVaLgflFwsgC8aDbldhiX5c/b2FnkdsIndf/huJlz4H6cknzwlJostItSzyQNEVOSdHn5b03TN/TwMD6dsFDFUw8EhjcSXTRRaslPcu4J7TY66cfqWGKWP5rryaUHoFzgxQUBdM2LBjyAXi38i/Lbh4imhBA0lpKBeevmWxmpBUrwyf5T1s5NM6GJxtBCU4UFqcQNZTzQnNfJZ58LoQ/R7ADJkoNQ9evAvx8AW29TWdLbAO0fbgw/w83QYbnWC0HEElI9FpUioS+D4A9wymbp7+5XmvNebaw8UyP8DO45L9GiGLvrmuqGGrhLzGrMbkIXd1ozha6EN31As7cNL/+o+8iDjET039eaZIZ1y1sBlZ1WIuGElZWDdv1sSwOwuXEuYvsCPYUb5cEp+H2Ymt/q3FvFUyFcRSNlp0smy4CugdS96FTm1hUaEcAvFSd1szh+pZDUeS+EiMKcg3+IDGHuuTbGqUbfxeEJ1v/CIyrC5w== 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: 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 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 --- 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 76d01de7838f..174de2789657 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, }; @@ -811,7 +804,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), }; @@ -856,6 +848,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 @@ -887,7 +881,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 Thu Sep 14 01:55: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: 13384017 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 F25A5EE0216 for ; Thu, 14 Sep 2023 01:56:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E7D2A6B02A3; Wed, 13 Sep 2023 21:55:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E2C506B02A4; Wed, 13 Sep 2023 21:55:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA5A56B02A5; Wed, 13 Sep 2023 21:55:58 -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 B556F6B02A3 for ; Wed, 13 Sep 2023 21:55:58 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8F5E01A0F0A for ; Thu, 14 Sep 2023 01:55:58 +0000 (UTC) X-FDA: 81233537196.23.81E75C9 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf18.hostedemail.com (Postfix) with ESMTP id CD1D31C0015 for ; Thu, 14 Sep 2023 01:55:56 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LUc1CCoz; spf=pass (imf18.hostedemail.com: domain of 3K2gCZQYKCDQiUQdZSWeeWbU.SecbYdkn-ccalQSa.ehW@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3K2gCZQYKCDQiUQdZSWeeWbU.SecbYdkn-ccalQSa.ehW@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=1694656556; 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=3EzYMZ/N2FtmFuyogL5CZw7tscjDCoW1YPbolbcS4Zk=; b=StiiKae7AMwCGXhTli9R2/+ZwrnQT6zKMxVfBLqOsX16RfIzEsybT2I7tGvS3WTWrYcB1H 9E9bZpGuzTIfOE1zMcLCf/pPE/Ob3xyO29rp8ZiielLOlbLq+Uh13ykVosR0nd0H/03XKT 6bvrFlEC6klb4xcfr7hY6/dSGk3NItw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656556; a=rsa-sha256; cv=none; b=J0TlNrjrgJfUehHX+erngBbhLf5M4K/wPvSx0H7hpWcdgO5m4WiH6bUxvfeP51l8HE4qMn q7zMu8EIQH6OlHfPVxmYkpg9PblpOFPRdRM+/Rj8t/swdYi9gY8p/xg/GJUyIKVsPigKgK rjoRD/wHKBebQ0GEF0b9vOqPJvujMTc= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LUc1CCoz; spf=pass (imf18.hostedemail.com: domain of 3K2gCZQYKCDQiUQdZSWeeWbU.SecbYdkn-ccalQSa.ehW@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3K2gCZQYKCDQiUQdZSWeeWbU.SecbYdkn-ccalQSa.ehW@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d814634fe4bso612173276.1 for ; Wed, 13 Sep 2023 18:55:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656556; x=1695261356; 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=3EzYMZ/N2FtmFuyogL5CZw7tscjDCoW1YPbolbcS4Zk=; b=LUc1CCozucD/pnFuw+erekfjzBXzoX0c3TiJGD5ekqAKZ8YWwskk9mCMt5X0KtUczK ofFp87I3CYITPgtBorLW2tC+nxV2QvtNv1DaTKMk9uMXfK5Ck2+JzmzuE0quiSHjWlQj WMfqidxaZh7s/OmeGuQmBHsJI82HDTWRAhVTzIko+hl9D46MagHSJJRG6FcuooAhogUN Ygie1Ug93VwYZwlvWzlzE3bsA7P/IGlLrIhQ6iPqg2inAqeB35FJTBVLxrQyw2GIJa4S e9gwv7ewrRgLwwTkblAK3BBTongdG+qmDCIMDhHUC4Ipce0siyAb9+7TwVB6+XP1X5gF wffw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656556; x=1695261356; 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=3EzYMZ/N2FtmFuyogL5CZw7tscjDCoW1YPbolbcS4Zk=; b=rFNkfM3E3T6vVGXR0Wmw/WOCQ2+0aSZnU9LyOrV685ZMZFiF63slBAh/rBn68iZXeT grLdW+jpBY9dg5utNX2dfQihg3C/IgbhfLfpEEHAJrNlhs8mdGJT5ekPbWetSPtxG7o4 J4MyXrt8/JnGsQiq3BvmF/zw+pnHn6CMDYcJegag38KNQcBTEt3WIV4yjWjiK/F+JnnV wtcTZ4/nvR7IkXyAQbw5R/zBQ+D5Ua5v4LOWREkHQwTz9DUgmZZ9rfMsMXOBC0S2ETfD UkaD/tF7riH0WmlcL8vMirqwfsfCKrrcbdiNC9tyY8vCZGmNl5AOFr5TTZ3WXcJV4X9N X2kw== X-Gm-Message-State: AOJu0YxDK2380NhO0+XrLEwZ654f8cFpMxVtoMmYURdrNq1sPieKl8QL mrcFr5Vp9U1H1ZdGCXMqpiCQ7m9noYc= X-Google-Smtp-Source: AGHT+IG4OY/vWo0x9F6MnIXrirdXq2cFaaYhe2gVdWc98bocPGMoKEXe9xVAJF1f8jZjRLg8FYPwelhBszM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:ad87:0:b0:d80:eb4:9ca with SMTP id z7-20020a25ad87000000b00d800eb409camr101075ybi.0.1694656555915; Wed, 13 Sep 2023 18:55:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:08 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-11-seanjc@google.com> Subject: [RFC PATCH v12 10/33] KVM: Set the stage 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: CD1D31C0015 X-Rspam-User: X-Stat-Signature: z4i5bnaoaf93qymn8p1ijactohtda9xx X-Rspamd-Server: rspam03 X-HE-Tag: 1694656556-134652 X-HE-Meta: U2FsdGVkX19wAo/sQa/H9N+dl4E0WrS5Q1/WJ0B8RmsU0+U7n+2O34PlxbYm49NQ8ShcT+pR++ykPfsb9iVUBMjNcK3NpQ4V2UwqiR4Nm5Sl/mLr1YycuDplLrTxMLJv6yvaFkwe10oBQhuNFCTJiCj+aoJ9wDesp5NNYwkT9vcEiIE1HM+7WFs3c25CZmokilwA8VNq/3sXro8qyl5Z39egU0qfoKyejXH0Px2IAGDez2JhvmwSwejMmVGc5fUmbBcm9XmlIWUwmWyt8vZIie/rGGobPON69coCsOk1kJ4K+OFhbVG3gwI1VD50uP3V9DiqQuKmDaHMPXzBTZkphNRjdfw5alnnq6bFwfGgb1g0513WU48yFCA2HjWeIcI4kHhIQV43EuKy5SxMmbX9wDDCUUKGMCM3uQDX2JalF6UmgyDDujnFrGx4VVISnSxT1e/8v5ADEwAV3W8mITJC2b4rOXCDAsPeijvKmigSMDX5FbbsHPTZkmTHuJXLpZEg3V7fG5jw189ncyEB7ie3MRSMB3wHGQLaNZrhZbYhzPioBVeJPSbuyBIwXAgE5cdY+E1iaJZVSYP6JN4MM54hGziMqDRS/L9kguscBRd4AypJaj6Wjx0YYa2nHV52n9KIWGOi8scu8j6RxoARTv8t5lpsHossXMWjFY+LjzoGPZNOLyj+30bmlCJhF7m/GIhcWjXaIxuZjQa79d7SXtObzcdnv04uj9KQm/XLxWs0vGRleeuGvy2JhcZNhU1qmtA/LVnwZxQXSJuFaEVqD7WoTsY6HfHJDHv6PfVQedgFt90NNPN8k76vn4iNWEJQBAH2p8t/RQijoX3C1xoOVVt38+DfV39q+l963pwmJGhfxoeTsPldYgom5GZZJAMo+q7bEj9eTW7iB/FkNS5Ql4/3/vRjYux8wJllaJpZrW0/rdoXOM7SGl4IpBeVj675HHbYUe3fjNgTmSp0y05oUED zTXUritB wnoE3FRFE/1vdVrgrcme+JnijgaUfDONy28sSDbEgck57DnbaNMo4UNur5n/FAx09QNfMMTCtxkja4GRspiihA1PgDbyqpcZT1VOnd/GbqHVdyl5IyZdsHmIubY7IT2ePaS5QQt73BBdZSmOF5jyGAiyOHqCa3r7SNs2W3WvW39aG3d4u9uqJwa8T4IjEZS2BpH7MKENYJedkxHOj9zqH1Bz8T8yLCFBC6oPB+RSqUpK9BPPmTPh+Iy0xgnNwkkY0AsMHwKbxj4z6xv07Mo8ttYQPvzR6Kh8PMQx/i9/YlQivRJn2g1mHBvGDvLpkEH7G79vZr+UYagj59gSONPtFkGKaAyG22VD9Xs9QgDHxM4GM+UE76NYmuTy3KlS4weSUVGTXJBAjV9rPYEuAGeAjA74Vvz5jbqbxK9/GepowdKWcm7aMPEDPCOBGjp0Rn2cNYB5LpXY+SetpxWL/Eft9JZ0o37Rs3LE6LUsSEEGjKf6U4jsqdOOoF0aZJ5OdfpoqjkaQvwbN+5IRMwP8QrT1pnbmsIt+at2afdf3sr6Y1yROy0+UXpma0aXIgEMrnyeB0kTYo/DRgu6Y3i6QrfsF8Ayr1hGkT+QGJ27LMDDxxxMTXzrfSaH9+qyzVwPJYp5JHoOmlC9Pjawv3QIkki/RCH1jUOgZEQFAz7Ki7ZOMb3GLb0D40jVLAZNJByQE9ALhgmZfRSpcYGYt2OkrZCiX2oFkvWRcUw+A/2AtBfDtZ47kg+BkzjVAWw5Zvrk7N+8s0unGXoWweWyxKUqiEW1WjftPtvkRChRSvyxss58Y8JfB33nXZy7bIPIQPU5rnj4An2uWKYsEF586oJDrIi/RSOA1HgjkEaLU/Yr5zXNxv3cFI6wDX/X9OldYkg== 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: 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 d8c6ce6c8211..b5373cee2b08 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 174de2789657..a41f8658dfe0 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 Thu Sep 14 01:55: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: 13384018 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 957FCEE0219 for ; Thu, 14 Sep 2023 01:56:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41DD96B02A5; Wed, 13 Sep 2023 21:56:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CD166B02A6; Wed, 13 Sep 2023 21:56:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D4B16B02A7; Wed, 13 Sep 2023 21:56:01 -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 058466B02A5 for ; Wed, 13 Sep 2023 21:56:01 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CF60EC0F32 for ; Thu, 14 Sep 2023 01:56:00 +0000 (UTC) X-FDA: 81233537280.10.8E0171B Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf07.hostedemail.com (Postfix) with ESMTP id DEFAE4000C for ; Thu, 14 Sep 2023 01:55:58 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=GOHwSFMt; spf=pass (imf07.hostedemail.com: domain of 3LWgCZQYKCDYkWSfbUYggYdW.Ugedafmp-eecnSUc.gjY@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3LWgCZQYKCDYkWSfbUYggYdW.Ugedafmp-eecnSUc.gjY@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=1694656559; 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=yIE8nMi3JnNDmDb0GBSv5tarA9FMsQImAxg8v1w+0oc=; b=aGAs2hGtr49EsyDlGMLi1PH6yjJCseLJ35K2tx4lHJqioQDibi7oAWYhH//mmb8PC272fy m2UynjHIgRN4A/k4Ex9hy9GOfbYQtxmM+yPp1ed82/AjVIaS822NAKGzRRb6poYvKaU9Fx ACk7BzMJ8b/vxWWjoD7KPaF97fW7f/k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656559; a=rsa-sha256; cv=none; b=1h+VW8YJ7cTFwHpQtRcIMAgSh/PLpRtR4CZC81SNvIh1Q1TuzLSdXvDiQG5GWKGsQZUX/c h/9vzcixcwUEuXoOv8TUCrTnHwsV1s6u/tHm5t36+pIyI1/6VbhcmUfE1vuVCi4eJ9EYJm fDGYPyks0u40fgChP8SoDG2IctBg4qM= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=GOHwSFMt; spf=pass (imf07.hostedemail.com: domain of 3LWgCZQYKCDYkWSfbUYggYdW.Ugedafmp-eecnSUc.gjY@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3LWgCZQYKCDYkWSfbUYggYdW.Ugedafmp-eecnSUc.gjY@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1bf681d3d04so3895115ad.2 for ; Wed, 13 Sep 2023 18:55:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656558; x=1695261358; 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=yIE8nMi3JnNDmDb0GBSv5tarA9FMsQImAxg8v1w+0oc=; b=GOHwSFMtB63bq75mNW6dwWWoJDozgjjDYAfHF3E0ViU6PFcfWYO5NMy7NeWLrOw95M C1iq7hZ2DTa3YkOPYybVEuhV/I4yyeAuSQNRFuRxMNg0GzbKKk6cTGe0nopMLBwP8HnB P+bldjmvYJS8e+y0KuzWhhlINBWz/vmtYf5x5sk/t3CSfbNyuOCIQzezg1z6NTh5A1hl WRbNNM4t8VKcjCMAg6CwU5fnqhOgmm1d1/Cy37F9QWW2zdP24xAoi2CAON3ifIG8bq6A AASFkNfFfJWzymr5BIrPXqvly2IVVTA22NL+b909eSAd6wbIhHR8n/FRE2Ttv7lA7UIt ilRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656558; x=1695261358; 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=yIE8nMi3JnNDmDb0GBSv5tarA9FMsQImAxg8v1w+0oc=; b=ewUutAk8RkrnzmZbdimI+amk9v4s1vt0FGFP3QXn1zCLQpGNmJ6O66AWXQOzHLSb6+ oecO959HZ01Q/4S3ZiFh2Ueiv3TbbTQHB1myP1cVkiqfhse2pLubWspPzNyeJ6sJJrGI eZHdy2AKiD/wHLipUJ3aVbn1jtIbzHqKlJPmrPCNuacR01LpU5fnqJJ1yF1EzAqM4o/+ XZtk8oZbOI4Z9nqFdCRXUtXoQf8QsKrjauE/9le8AuhqtlwFdNo+xAKZX0Z3RHcuJfYE XuMMY1ym/GnWzwOTIzqPVU/4Id8KoxVvYUGiBadYeJrsbohzl4cFlEwEb6A3ehfbfVyD sA2g== X-Gm-Message-State: AOJu0YzkyU6Dc+jUp+52rNoTBkHoMkUajm7F6Jqq8Eedl7+gTFBYdBUX DovNwaTJqkXKcpQAfV3ePilKQCoT4Go= X-Google-Smtp-Source: AGHT+IF443gBTAAUA2YuGDTpz8JAOdzRRFMiPageKRcJ4kGF2KTWVEAjNOGI96kb6BUMaL6QDKcvpM+ucQQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:fb83:b0:1c0:ac09:4032 with SMTP id lg3-20020a170902fb8300b001c0ac094032mr144133plb.9.1694656557750; Wed, 13 Sep 2023 18:55:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:09 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-12-seanjc@google.com> Subject: [RFC PATCH v12 11/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: DEFAE4000C X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: aprqy3fdtjaekihu5yar6jo6m8ka74jf X-HE-Tag: 1694656558-545163 X-HE-Meta: U2FsdGVkX19zrPOpT1oEDWR/APYSJ+sGZ3c7eVQjtEsywXObvvxBkagMC9Hr9ERSthW9sovErPhbubZ1E+kfdpeAmUG0vtJjcvAU5yajPF330Nxyl6YFwf+rEzOcOLJ7NDq47gRMp5OxxO6I//yBf+DOph41oep3l4ZEI33NvnK1TG3CJSpjLEPcPcXc+iw+XYZqtltGy1mRTiCRHloOaBnHwTfPpLwqurVwRf0bu8IAsOBGloYP5PTHXvaVQXr6wLwBYaqZlxjxbmOMakVtSs0XwXdVQUNBBRUyR3SbdSc2QVBnoqlQxL/Z4w34LhYEe36Jnxx+ysF0zYiW3qy3Ot72XovCsHD8vPzxDfRgjgD4dqGu09clbZTQKGrUNY79SZQPFeNLJEC37xTLZb9n+D2yC23NYZktCgHURj46tXBlAlzefy9aRguCLQ3huqwwBrezeqXxyYGIDON2oUB0k1uDskjNW9I0nSXerU7qA6ldUbwiTj5EJT7vH6hRmnQcTGtYc1oOhNHzi3TRvIJo+dDvAhqFQdWDbXMn9te7LN6i9Eg/ssVcnP2hRlA55MrIDY8YUpSXDDDJb5TWj5AiGeLEC9ytQkP5mmmzmadlUjJ34lwV4OuuKCNcAL+xbVXzshE4rms0SvxgZGwYT5n0juq/nm7h3JzplHFO5sxefvZPX0nIXSVfdWMJcu7Djixd3LaV2uO7DA+h+cQrF1subQksI/b2sBPHDT2Bf1D9XhNL0q2dFIkxmlabAJSvwE4f5wqLRVNB8gFOeWQc9LU24PhbJK45g7QlgwDZIwPaPzUS428Ucq3B6ks4yOpLUoznqiH2gEjVdRkY+zRSli0OxmsDlcSQL0xq64cRdnVrvj8k+lr/SY2NaceytogqqpCG2Bdn+jOk3WT9RRo7+/3wZTJof7+pCLsEp9/z47yn2w38yMjULLZhPfYlF+lIvGd0yoQ8gzYyk1fwTpo5QK1 D56LNMJi PROnr0XcKTshhhfSc+UKAUNXsRvts8zAGXaSfxp9PyUuC5quwjdtaF5c2cHPsfzMI6/QQSkrye28uwa7X6Lmo6AlCoPX1yvJ94EMtoGzqs0fe/hbUq9Hrqx0txcxsmfgk9encMacDURivbLODoCdDqDVgLR+G/VnjVKyDdriH5pR5JHPMe1i0VFJuUMYDpjvnQCGDya7WMwaziVHWxB+B0nQPenKSi6Xc1ycIotCOyPsqE/Jbn9A3A3QIXwK262Zxb9BOurwhlHxdn9FC9vXIbCJtiTSEHtV3G5GpUB7Cb8nJ/RUGio9dMQ9gxZLYs+cqJwShfIqk4yNYDkT/pfDPRfEim2bMzvTefIOE1j4BKnOuxtAuA7eZ4fRKZgHpO4W7ufaeyxR6ZEyDztVSJj8HpkWI9drx/W+d6AV6hEQFKny8CauoqKJHz4N5lvoOE1YYKgenB/BZlYAAonEKq6kqCR++1KzvqFiTaD5brGpHyz65NNmKl3m48xqYLCj4WP6Xmmw6vXHYBCyHC34YW1/dUwzwjzFHlz2+RxptCtjw+tNNrtfxrDHYPk4nQbRYiQaPytdH0EKWVi3kBVe6Fguh3hsFrqhhPs+SIKjZGbmqi5aEoHlxrg8iW7k1q1eeufunlidx1uPoxLuvO2Zx8BYVKYYu5kh6FOdPYN+7HFXGnh1/ZpidrNaEF+AcYic09PeuZPzSpFlKg1pB1dAz+rAs9SHvWAq06zgK6/8gUnQtvbSUf0eIaka8X+NwpSIA4EbgnfxEUhO8LDj7/kCuxhP0jLzEm5fw9OdB2SZZCx+NKx78dFAOTJI06ItB3bnDY5SMw+rPWvynEzX0kR4pA5HzeH7Efw== 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: 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. Because setting memory attributes is roughly analogous to mprotect() on memory that is mapped into the guest, zap existing mappings prior to updating the memory attributes. Opportunistically provide an arch hook for the post-set path (needed to complete invalidation anyways) in anticipation of x86 needing the hook to update metadata related to determining whether or not a given gfn can be backed with various sizes of hugepages. 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 Signed-off-by: Chao Peng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst | 60 ++++++++++ include/linux/kvm_host.h | 18 +++ include/uapi/linux/kvm.h | 14 +++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 212 +++++++++++++++++++++++++++++++++ 5 files changed, 308 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e28a13439a95..c44ef5295a12 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6070,6 +6070,56 @@ 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_GET_SUPPORTED_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: u64 memory attributes bitmask(out) +:Returns: 0 on success, <0 on error + +Returns supported memory attributes bitmask. Supported memory attributes will +have the corresponding bits set in u64 memory attributes bitmask. + +The following memory attributes are defined:: + + #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + +4.140 KVM_SET_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in/out) +:Returns: 0 on success, <0 on error + +Sets memory attributes for pages in a guest memory range. Parameters are +specified via the following structure:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + }; + +The user sets the per-page memory attributes to a guest memory range indicated +by address/size, and in return KVM adjusts address and size to reflect the +actual pages of the memory range have been successfully set to the attributes. +If the call returns 0, "address" is updated to the last successful address + 1 +and "size" is updated to the remaining address size that has not been set +successfully. The user should check the return value as well as the size to +decide if the operation succeeded for the whole range or not. The user may want +to retry the operation with the returned address/size if the previous range was +partially successful. + +Both address and size should be page aligned and the supported attributes can be +retrieved with KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES. + +The "flags" field may be used for future extensions and should be set to 0s. + 5. The kvm_run structure ======================== @@ -8498,6 +8548,16 @@ 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_MEMORY_ATTRIBUTES +------------------------------ + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm + +This capability indicates KVM supports per-page memory attributes and ioctls +KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. + 9. Known KVM API problems ========================= diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index b5373cee2b08..9b695391b11c 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]; }; @@ -2344,4 +2348,18 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE; } +#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 d2d913acf0df..f8642ff2eb9d 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1227,6 +1227,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_ATTRIBUTES 231 #ifdef KVM_CAP_IRQ_ROUTING @@ -2293,4 +2294,17 @@ 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_GET_SUPPORTED_MEMORY_ATTRIBUTES _IOR(KVMIO, 0xd2, __u64) +#define KVM_SET_MEMORY_ATTRIBUTES _IOW(KVMIO, 0xd3, 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 a41f8658dfe0..2726938b684b 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); @@ -1391,6 +1394,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(); @@ -2389,6 +2395,188 @@ 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); + 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) +{ + 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); +} + +/* 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_arch_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); @@ -4587,6 +4775,9 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) #ifdef CONFIG_HAVE_KVM_MSI case KVM_CAP_SIGNAL_MSI: #endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + case KVM_CAP_MEMORY_ATTRIBUTES: +#endif #ifdef CONFIG_HAVE_KVM_IRQFD case KVM_CAP_IRQFD: #endif @@ -5015,6 +5206,27 @@ static long kvm_vm_ioctl(struct file *filp, break; } #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + case KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES: { + u64 attrs = kvm_supported_mem_attributes(kvm); + + r = -EFAULT; + if (copy_to_user(argp, &attrs, sizeof(attrs))) + goto out; + r = 0; + break; + } + 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 Thu Sep 14 01:55: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: 13384019 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 48293EE0217 for ; Thu, 14 Sep 2023 01:56:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09A606B02A7; Wed, 13 Sep 2023 21:56:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 049F56B02A8; Wed, 13 Sep 2023 21:56:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE2AA6B02A9; Wed, 13 Sep 2023 21:56:03 -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 C7E1B6B02A7 for ; Wed, 13 Sep 2023 21:56:03 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 998891CAC09 for ; Thu, 14 Sep 2023 01:56:03 +0000 (UTC) X-FDA: 81233537406.28.507A1EF Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf08.hostedemail.com (Postfix) with ESMTP id D25F9160009 for ; Thu, 14 Sep 2023 01:56:01 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="XRL+G/79"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3L2gCZQYKCDgmYUhdWaiiafY.Wigfchor-ggepUWe.ila@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3L2gCZQYKCDgmYUhdWaiiafY.Wigfchor-ggepUWe.ila@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656561; 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=IroGtA6z4X06VM4XXyW5Hwunrfyb2UP8Ai0nAjfYz+0=; b=rTm1irVeXNGolUPAYCC+pvPzzDCxRmBZLW9MpM4i/mX+GFCxB5bahcJ7gimueo16RO4TQS DJZf712dar4kXCNtKwKNdk3GTfQ89jkpak/K+ef4hP3wmAX1zElLkTPh2kdR+If9zCoJvX kJ4CR2jgr7zIQco8g0uaqKuKcID1FzI= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="XRL+G/79"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3L2gCZQYKCDgmYUhdWaiiafY.Wigfchor-ggepUWe.ila@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3L2gCZQYKCDgmYUhdWaiiafY.Wigfchor-ggepUWe.ila@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656561; a=rsa-sha256; cv=none; b=VeaToaPbxe/nuppDkThTvXPEbeNAK/8+ljBPlrwVwX/zUdOTjTvolWK40SjvmuH0/ZUssH bz9ZdiueROdOrLUxFwUvLVWTH0A0RGqeESHhRKe0cPRTEJNBfeL2Sws/tyQuevmyBc5vey heaHQiREfzCD+vQCRJzlzVLWKtK0LhU= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1c395534687so3972665ad.1 for ; Wed, 13 Sep 2023 18:56:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656561; x=1695261361; 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=IroGtA6z4X06VM4XXyW5Hwunrfyb2UP8Ai0nAjfYz+0=; b=XRL+G/79REw88nYDBRz4tdzFkOvD0YGeYXOcpL3HWZ/xF9pqy/o+QoaKgxVcukItLH zJ5aIDCaPez46hTj1kiA4yoqdxsQhOq6eiA+hu8VXAEHzkZBqY46QfBZ5ZhpG8rrg1Z/ F3mc/q/lBQm3wcDvBkKn8YbJObyl5Sla5YEwSAuNhM4Efhq0e/cZGKOyyCPJX7zIxw/O M5Jq4k5U2Ytag0hNCO29BkygCilI2Mtb61TmSUFkbA1GWWMIrdeqiFmo5VsANxpLb3SF PRlzZtZYVCEPRQAUdsQLAx1IDDd4zntg53H5SgOuJ1tdtJRvhsQp1F03ukt+0wqvWTvs G4Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656561; x=1695261361; 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=IroGtA6z4X06VM4XXyW5Hwunrfyb2UP8Ai0nAjfYz+0=; b=genyYPQvPIRxvG71mwQG7yXYs50xa1+0jA9U1425s+PBtSyGX8iXm8MErGRvZNexVh YsAamTtPhLoOO+gTQOvV5jpqPGnLnkSKaw+g1w7RW+w7HfOU7PUN+3bsXxfHWAZIeo/Q WTkp+sDcc6zgSzmsY3arS9JAE1ajTLh7iquuei/dYLVDR0Z4lBGkN2GD3m4x/tTG/HrA pYzmS62gHnuVQ8SOZNUGt+s3bP4f9zCYoz43tQ0EKZmpfxKPFyJZniQ6IlrbF9+aRrOB MD/Y29NfzY1Q1Ymx6rG4WIi4Gq5i1GHhMM7p2So8zp0kXrO9kQdKYlsY7iSPPp2q+kGw 1Byg== X-Gm-Message-State: AOJu0YxtIu11GBTx/l5okyZfnieXDaesW5IEJYjwDVUWxTEBlQqJdCQX nEWCN4zRdZzuaJTOFSvHtfDqTFaKH1M= X-Google-Smtp-Source: AGHT+IGFyRd+BYegO6/D6beA7XY/sIg6slB9OxyqJyx+o15XvIYq7E/XS8tz54WBztwLFVG+Qn77mgnd2p8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:fb83:b0:1c0:ac09:4032 with SMTP id lg3-20020a170902fb8300b001c0ac094032mr144135plb.9.1694656559883; Wed, 13 Sep 2023 18:55:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:10 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-13-seanjc@google.com> Subject: [RFC PATCH v12 12/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D25F9160009 X-Stat-Signature: yxwpanfzeucpngfcajmxk9ppspsyhgpw X-Rspam-User: X-HE-Tag: 1694656561-270472 X-HE-Meta: U2FsdGVkX1/yWqgrPCTvAL0S4BNygkO9XmSjPVcgYSYzvqCD7pomuuHS6afez+dc95tpiRrk40eNYrELXwkuhHLf9tThWPKpbJg9LryssXaKhKnBIR3tErAtwWp5M3PlkkhnWG/nNDX8pSKNLwnLZ9A1LJVlVPp6uJW3uJplqlQ/zRARPWY1AeCNmDYq9po7d12yLAKMEV9va0QT/hY+ILPedbM4cm8YN4gMjUGZ8QNuVnxma83NCwwhJ7wQrnw+NY3gXh2JjMbgeZ+3LlBkjZMqPyZiuXgAVzgGmK5pHWS7Y/pA+A/N9p58vCdLx6YXYY1mQcmaLrglpfMz5c9kbGGLCTbEt0uK0Q+uF8HSQnkLKLuxdAOAlW8FsSZeH7rJz11xmTS6aRVMg7ncgQn/oxMUDMORmragIacoMNoz1dG1aM2AIcZ7MlNcXDS26A8QJ+X/xu2L7UGPp8/Kt2UFZyF8r0NhdKsoOvsYtvvcycM+KZ5UdQ3KkSTl2CkqoFGOljZbWgQjo/X8sfxqhkinU4u1ke3mqr4aIjRODkstYgY/rS7nbiGlCFRTd6nWV3Io1KWaEoPn6JyZqD/h0iGjjm7ju1PTfvBlDmOHtJrCljVLZzHZaZdb72aswyqysWVz5216nOV96//7wvjRH1CXHds320S3ChFFkMl8+sHp7A3DsjKu9GeVioOLSJpnpv2LRMEK/WEhDEXF/+cK/r5zdGHCGWmjBoc/pGGFRcjsVO3TGRZwlR9WOaA+e+jAnrce/4/zKBPkkBEqX9v1gxrTOtQp7ERkgTajv3cZxYHRf79GJOZK4hFEyknXF+i5a0o0M9uHApEr3vu/q1gtt0xuhq93MynnX1dvcY3vi2zHMIegptGhqQwB84ZKxnpcaPUU+ns8Lvf5qJIiSftwrHsYelqUvSzskg5Gp3nWMFKUxaIGBJS+8N7Fb0+vvkjbdBeDZO45dSZHAFQiLa/nGE6 buUKdkE5 hsLOOtKReM8fSISak+EWNn3vlZW1N88Yl7aAl6lYkBP6DmQDoCp25Esp921wpces/GIKa7prEeIVteUSKGQnA8lQTQ64mbjyr4h6DTD58v6etD0HoYOEYhcdhOj5J6kHr0k0hJO72QWLeeXgrm9JFnCNge0XuXt8y3n+j2byvsQU8WvCN+EdXzVFKCGopmDqVjS/NsQeQ/jh0qErwGf8ctAGCJlonWNgjy0BHldKEfQjIOSMjj1/JZ8lhLI7y/0HPsRTSaD1ndnkGfw306OjqJ+3CkpYwYWqsE8FGmT45F5U6S/76c1BXwdt7jKrHKB/JB+ICsTqfJkxuM4G/ulcH2bk5bIuX2OA0/JYQz2jGAfRrN1bS+tttEw0Up/CD7724OCLBRTGTugQ8VVFMK5A8mrGpEWfZUcAqwaQrwD/jv0RwkOdYbY9exUMI9wesa/UH9Q2JrzUEjfAznkmGiMRULJYTDJy6iYr19fubOMmIUSJflRKA3hkQ7cNFS430Za2HVOFIyQCy253IoIBkQbcLlpEwVJm848OO95cqUxsAEA+jrNExG6T0tomuB0ER57cAJGYEpR1ojXz00Nlt/YMDAK3SR1hDrLw0827xCAkeJ3xby+aA/t3pGum4iWztX6ScrBFpT77wPl9Ser6YDGsbSNj2sfE8nFPA8PozrMJPbZDw5HvAL2td07sukibWkzKUhyLk+ef7dlXSvYmfozJrAwK/2cGP9KefGwPtHGGjvObF0BqfoP54KfJZlhCjZBqG7JzuWVz9QY6VDYZUAjcm06NDWdp7uUPv0H94toR1S9XiLFlmlCoJY+q1za4F8Ba5J+WFVyCqnoC1efbXb1IrYDOHQGb2KNaga4fUvEx2DIep8S+Yu9IP5YtDwOoSgRfR4nGp 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: 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 b7fa020003f3..3d25c145098d 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -953,6 +953,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 Thu Sep 14 01:55: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: 13384020 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 DAC4AEE0212 for ; Thu, 14 Sep 2023 01:56:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EFC466B02A9; Wed, 13 Sep 2023 21:56:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0AC76B02AA; Wed, 13 Sep 2023 21:56:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5AA46B02AB; Wed, 13 Sep 2023 21:56:05 -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 ABDB16B02A9 for ; Wed, 13 Sep 2023 21:56:05 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7F71480ECE for ; Thu, 14 Sep 2023 01:56:05 +0000 (UTC) X-FDA: 81233537490.19.9BD5CB7 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf30.hostedemail.com (Postfix) with ESMTP id B88628000A for ; Thu, 14 Sep 2023 01:56:03 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=S9MTxO38; spf=pass (imf30.hostedemail.com: domain of 3MmgCZQYKCDspbXkgZdlldib.Zljifkru-jjhsXZh.lod@flex--seanjc.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3MmgCZQYKCDspbXkgZdlldib.Zljifkru-jjhsXZh.lod@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=1694656563; 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=pjDK4myqRW/1aonXWMm2h3oZGIvtROhN9F/LidQar5g=; b=iIkQeF35Vc39YGYK/gOQHLLUea0QFfnjT6AI6M4tYeOavkI4X3x62mvez3ETCd7k/3nrsB BnWtNbWQ8QWNHI25ZfOjgdB2wPumG+q+2LV3n6lAbS/qdFpBR/mqlgp2bFJVdV2M8wJfx1 C0XK9tqT5VFXbHLpXjeISvXLtVAXu3Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656563; a=rsa-sha256; cv=none; b=iIoZqMN0vGS//WmqGtajUQLb490onYQbzfntm51aC0Q5LsOO9FEbPobZG8uw0IG07xExh7 atGja1zVQRQaAEyB7oQ24DH9b42Fnnqv4pZioO+ml3yZ2UfdVxHhy0mZowPnmyK7Ofp+BR 6RiHNZTovyxxStLshOgiSOdYMSDmCMk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=S9MTxO38; spf=pass (imf30.hostedemail.com: domain of 3MmgCZQYKCDspbXkgZdlldib.Zljifkru-jjhsXZh.lod@flex--seanjc.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3MmgCZQYKCDspbXkgZdlldib.Zljifkru-jjhsXZh.lod@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-26f591c1a2cso411597a91.3 for ; Wed, 13 Sep 2023 18:56:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656563; x=1695261363; 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=pjDK4myqRW/1aonXWMm2h3oZGIvtROhN9F/LidQar5g=; b=S9MTxO38bVxtM0g+uDtdYdiw4h23MM2zsQQuiUkjIyaIRxzJmmRVYHDMQQQZpN+WHa v/YzQmdMOJE4UdHDjp3kpBDaWHkRqv65ee6gElJl5eEXlVGR1ub/Eo+XrKJaqv5rOJjZ 8BJZtYPjimA/aYIgh9t3TDKzDdEmYyiO6qZtDKZkskSHwGXnxLREsAVExIIqcFHcumg3 R2jRIdE57297MhTH+ulgS/ZoRlmlMDlK5TitwPfw0DWiJkWc2kwEuKgD/gjLSRp9yxrk wUqoYySFIYyhVrBkyYIeoNtSOMKvHqvnoHobzWEjOEApeLWsyFuVGaUhuzgf21ca6fMk oWWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656563; x=1695261363; 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=pjDK4myqRW/1aonXWMm2h3oZGIvtROhN9F/LidQar5g=; b=qc0uH/rSdriONO38CCDDK2zExljsyd9oUVIQ+oMOo1QY8wN8+ZwOTrmDVzTrrAm6d1 vDKxAuKyqlgUWQNzZjQaVLfbR3gL58niFdyXTDc+q0V4caH2jXmOda02WdkbYTHvxij9 oqAgnUJcfnJZoKCY7iwNSgNfFHoJb+nvjBa7N39fkAQ+5Gqtv3ONZDFzJN6SsISGx2HT kdaKS8tW2hLyAo3t4tY+1BHhKvDPFTm9VIjykWIeDysHq6hIJu7FWd7WhXEsnzkWyCfx E6KAGqA8UUBZrypyiYfQWKCn5XriwkqmUbVf6J/p9I9Q9qHM/h+nyjO9M27UNjmxzH6N Z3+A== X-Gm-Message-State: AOJu0YzzDOCKYXjP4gvgopVIPYYpZLEGb//S5wgjVq9BgHYxYfKl5RGR NminfgGsHPFPP5RA1s62MxWI2RrM8VQ= X-Google-Smtp-Source: AGHT+IGkqKKLF+YNvEneEdPalVclX+vMSg6CmJX8h6r/u0gfxt18eBEohuQohG5dATROkCqosWZykFZT8R8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:fa8b:b0:1ae:6895:cb96 with SMTP id lc11-20020a170902fa8b00b001ae6895cb96mr149990plb.5.1694656562630; Wed, 13 Sep 2023 18:56:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:11 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-14-seanjc@google.com> Subject: [RFC PATCH v12 13/33] security: Export security_inode_init_security_anon() 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: B88628000A X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: fexsfdhmq1szphnmnsya69hz77fyfi5d X-HE-Tag: 1694656563-891936 X-HE-Meta: U2FsdGVkX1+dpRQihikAUPT0OxXEHeUqr6j1U6q/r/EeAdRb8Np0QypL6SoxYdflddvhCNR+0DZdtcHFnKXq0IbwLLEYkLANLa68KFL4ZdBJzqF4HneQ2ssEYFopbah6T2NAGiLeB/92VGmsg+WwLBRBbX1l3mABYzBgAp39YlJakzbJQjjz/sNLLcVECfIR7ttEry4FT+0DKdjLN5rYeAD+FKus2T0iYjwbOy9jw3spU+PTW6mD6L2XWx++MBk+TNE9naGy3pUqGuvRW1G0+In0bHHqPaGyIoPOIlthCl89FD5cfbVkhcxmxGPNdC1Nfd2+h0hRJX+JRPWWbGWx9HVTNyHG9dG1x4zOm0ri+ctPMrcrJQB15biQ8inUp/7aQVgjRLI+RXtHI3r/NhOPUe0bDk4b/UsSJtkQ74XuSXY3x+qN7WCQAxhfuDc+9Ti7xf2x5aoAGpnzTE4klnDo2lhrpfglFzYDld9ZdiEstY1SvrIrfrFP8jFTHpxNt7xAwU18lphATAe97QZyJLY18VofL3GHdaA8GxXLmo3rivDCegcvYT7Yzl2O+36zXUjWxKFZVB1+Q+M2JBqkKDHeJ8Aw7vmT2TY4+8t82s45Utq5I7jLi2GJuWNp7xwW8quZ6b8H5d50Yhxgtseph3MkVSRzcZNqb4AgdN7Rdf9Yc20y+9t0lWNF/W+muXLFkUm4nIMgMgBbBQI4ATe8UFhMHmjm2m9f3Bal/zvQJTf4QbxlRkBxba96XTzW43/O1/59Sg7uYIMqYrfKR++Cnahe+konIQpCaHLxUAzX0eYVFyb9LKHyqT14B9zsts1vD3aOmURCxuJZ5yZ8c20O5Umffd29llyn6wvC100K34kLLTNms6d07nl6u7L7Jl2H+m0dheoYVl8zMtmtPftvIr8T4+nG9/vfj8//cz1q4Rpzhh+KBp2xYjNWxrcPV2z7lpRFVKjqEn+UC0Hy92EomnO 8HDy4IV+ a4obT9NBcAgoKLIlcW0asc5X83gzBfGJbs83C5BsV39epKphl1q9+lRZMKLu4Q8ZniCrxzu3j4WzAN3bklEwI9+r3NNsMSv5qJmcnDXnRv+zD+9yUGzdlcy6FiYkjjJ3yV4SzU1LaZOm01OKQhXj1dL1yqPqVxrSULA9nn3xNWGEZmNiNha7OO232fSt9UIMjnikwLM2zZRgPPa3mSEzkwjTfsnL3W8/P4pZ4MIJDn/O9WBS72Hrm+kT7/K559tYTI7tVldKX8OkH8U+Tbd6aRtl6mO4L2DUvdQzMq0yiJ8U+JZysw9goMd60TWxZcAWdctGLBZBegssp+TwzjQJ1oQ24CyLJAdzoIaT2pRT73Yf3Q0sKsgytJNCdKW9BmYUwXLvCfpAwrGgIRX+eqksp7iwgREiABE6JbJXgoSIzeNcWahNq0rVgcjGHSiKHMNvvb/wsAwI53CBAfrXzw86tMqDxuTNDIjjzQEoGBjg5Pcy2sH0JQzJPzWlDrLh4bKqD5NLpo1MKpacRBv+73CaTzHAV0OhWFJL8J4CmLTlAY/4QYJgDTziI+3c+AlSpKU/jvKTYVlr2sgT6cR9ECDuRvVBosUVZRrUhhN1OqN2s1Li6E0MIg8VkaEx+UCuZPbk9dVVlxPk0pGzwdj7Nvmxf8tNIVZ0+5DAMfhyT/wJ0hZaPUDw1E4ugtnEeGnUy/yXX6f7S+TSBvsMN7CoYZB68jvV+Q3ntjQzJ2MK7y0+0zn6C8dqk5WUtU1BW+ZsGXy12hSHT0jHRaX1kTlJOO0Ax4gKzmQkgislIfuG4iitj9VbKzANcmMJb1wQRr6+2aFhzlyvqfLZV1DbkaiiH88Rc7kZrlb78do1wJzmbSdZK3/QH5S/m32SboDgaGXgks51lUN+mtiBm2iLSvTEsi6kc0bF+kw== 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: TODO: Throw this away, assuming KVM drops its dedicated file system. Acked-by: Paul Moore Signed-off-by: Sean Christopherson --- security/security.c | 1 + 1 file changed, 1 insertion(+) diff --git a/security/security.c b/security/security.c index 23b129d482a7..0024156f867a 100644 --- a/security/security.c +++ b/security/security.c @@ -1693,6 +1693,7 @@ int security_inode_init_security_anon(struct inode *inode, return call_int_hook(inode_init_security_anon, 0, inode, name, context_inode); } +EXPORT_SYMBOL_GPL(security_inode_init_security_anon); #ifdef CONFIG_SECURITY_PATH /** From patchwork Thu Sep 14 01:55: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: 13384021 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 6270DEE021B for ; Thu, 14 Sep 2023 01:56:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE9656B02AB; Wed, 13 Sep 2023 21:56:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6DB76B02AC; Wed, 13 Sep 2023 21:56:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94BBD6B02AD; Wed, 13 Sep 2023 21:56:07 -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 7B7FF6B02AB for ; Wed, 13 Sep 2023 21:56:07 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4BA54120F28 for ; Thu, 14 Sep 2023 01:56:07 +0000 (UTC) X-FDA: 81233537574.07.7B884E7 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 6B33720020 for ; Thu, 14 Sep 2023 01:56:05 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3+bmh2wt; spf=pass (imf13.hostedemail.com: domain of 3NGgCZQYKCD0rdZmibfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3NGgCZQYKCD0rdZmibfnnfkd.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=1694656565; 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=0xZuxSvjNqdLSp6ZC4j5yUjHcPQeLv93P/PrP34/Z2I=; b=ozJNXeIoxj4yso/yOGO5gzvDDbrwNBq6w7GHLVvGDIPcHNxBXjqKY8SFd9ekUNLe7czrTx r2iEvpBbOl5WcoKDXSM39OZ6wGRTjm9dKUsasGViRHJB+8R/9B9PmWBb8KxicH4NXGtHEn f6iPhSsp+QvCcp4BGdZwZGAeE6tPAAQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656565; a=rsa-sha256; cv=none; b=de/LuVzY0z9WA9cDVciMGiunHd3vWol1QcbGXt9oE/RsrH21WY1wRcIcbtQZyyS7VnHL3x C1Q9UAaOJ0+4iOSF1tzr0UBQBxVpN8pOqMM91+ORvnV93B2Zl8FFOXeg5jONggaUhby1Tj 6sKnSPSZBim/CLyOkK9sQSN/tB0YItA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3+bmh2wt; spf=pass (imf13.hostedemail.com: domain of 3NGgCZQYKCD0rdZmibfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3NGgCZQYKCD0rdZmibfnnfkd.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-594e8207103so6713377b3.2 for ; Wed, 13 Sep 2023 18:56:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656564; x=1695261364; 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=0xZuxSvjNqdLSp6ZC4j5yUjHcPQeLv93P/PrP34/Z2I=; b=3+bmh2wt/uzC96aoUqDzzkbLJl9aQqfnayq8NI89dEw7EkV0f0HpHBXnKix9aVDGdI SoGRlWmEJVEBd4qYV743wicCLqoghTYlM3prFGJX8pVDtSYgZpKg5Y2lrmA7ztQiCTG0 0sqYHt3KzVohGckJ1wse3DaSYZzdi1wELH8js8CTcUKRFtUlxtl/fSQpeN+PQIL6glhs EmRnAeIEWPAJhISME/nXlCHGPsjrzM5QPLiFdK6y2NLj1ob9dYsdNqg1x8Ey0QjyL6y8 +R0PUtuR2gx82syqoIW5ilmgltqvQssWlvqZtQu6YavNuR7She2PCILMalqHcrxw5cGD AroA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656564; x=1695261364; 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=0xZuxSvjNqdLSp6ZC4j5yUjHcPQeLv93P/PrP34/Z2I=; b=wV0ujS9jtIm8Jf4Iq6NJPR0GGCWb47ZZqfH/ulp40JQd0EjV/Z5egL0lHAN1bEv0GW rH3xgHQjKc7GX28O64kovMKnf6L9BYQ2UlBdj/5RICvv7qNIPgWydT8AkRw70nywgIdP HKezmHitavcknZmYV3n0QBhjPU732zMYnv095KTvkqIXOEYg3CyGKGMhw+MqIXxikeoL hZACEql/VGMEQC6X4d7psNE/h9Goik1tHxyxkKUIM0qwN7S0vHoSn+55CRFNmBiOYZNp YBegU08tGIoEzmm74ZTJUenS06bhNwPXwm4vEEg6b8SNv2jg39HTTGxI7ha3oO+MW8Su /Nlg== X-Gm-Message-State: AOJu0YzSFIeBnI+CpzaUJVvPdkxEwDJ+ZPUUu2p8DWyEQnmxFsXoYpQD iu+2WaTD77o+M2/LOJJ2Em2QEos6IOM= X-Google-Smtp-Source: AGHT+IFUOIryDOCU+/5hEerl5iZde5CL20+1Yxt8GDISIc9Tx5mp6HRjdT4pBIqwkt3T5QOm51dkBscWUdY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1816:b0:d0e:e780:81b3 with SMTP id cf22-20020a056902181600b00d0ee78081b3mr102709ybb.2.1694656564588; Wed, 13 Sep 2023 18:56:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:12 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-15-seanjc@google.com> Subject: [RFC PATCH v12 14/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 6B33720020 X-Rspam-User: X-Stat-Signature: nuskozitfmgkdd1kokcaqy8x1qj8fopo X-Rspamd-Server: rspam03 X-HE-Tag: 1694656565-967766 X-HE-Meta: U2FsdGVkX19yO7t6lZF2CMF3ep/CWnRRl6MlQqSQAhL8xpO2POwOrco+elypXgzyLsHKpWvBgSDq/KlwsEQimO61221yasWkpd8Z33kB04rnzaKTSeGPiIC2j+lwjZ62OHakTBv9sYfEQRiLyBxPunvPdpuWEW47daB75YdGrtIijjr7CiUk78qFCP6VezJXWCwo8iLfcLlmr8tdgLEyCS+jcnys9xyZbbsk7Jm3QU7bdTqMinyUPLh4s0oL91xTyyqYM/uBEsROn3rKV7LU/4Ty8ywPft9/hxBOQsY5qp4B7eIMzTVsU1TL+Rtf7INZBIHIlHPbxbayVTpBolIzNcAS52qacbpTykAKRXg2MctsPn3bXQjsPRIiivhjqoZfcn2qGMkRY6wFI/10XiENHEU6Os5mCK5L8KAyM70oh1D1AXtCAuPe40vF3CgICb22lhUZhWLknhsvIJ2lP+hcDro9issT8qQQrU3diUE8bHPi5usQKw60y698S0LsdBIi8iszmZR65Bd06c8pvlovPRtYTS14+8PArIUF9IuhRHmmMZRV6wOR3CIzOj3APwMwlyaXs3gwwuUGYrCpNxuDKx5aE6TJRM7+gFdCuWf22GESpfEXGgijNWUCeQz6WB/06LPfZ4dVX1tW/jFUDqSaqHuoKkwvENJyjf/8+T5Qf0m1kKYu67j+/Tt3HEWPXH8hRVlyaTXKOs7s/rBJsPUc/HXKWWNjpqFn6+3RRwOIYB6BeBGlzmpEp0XCjZnjzoI3a1eWZl49url1n9NGNgT8/HAyki8dhEiCdfmGuT1SldfrsRnHnuPwzX0PbuDlleI7dBcLeNEOeqTIcI8M08n9ab16a+Jc7pFFgg0xx4Rl77bazr42DLhR0Jm586McxFCVYzfVQ9okqgA8aYjpvKkTstcM6d+PTsiG/wMAN7aeb1y6Qtipgyzc50oJHBuv4kyBc3Lfy1nKr0Q72DtD8Fy nbc3Hdrh /kP30HHQ3s+9ZVucIMFOwB4+S0Own2ddEvlh6KIJQilOoQBIaZwJhUHFV3QY8Eqcy/H03bkOWyq2bGlmVfj2auTHr3teUWDRT8VOn+wdX70zl0ipyULOOeHGReWy32yHAhRl3of98dP8osKm7ZAvPGKRrGzIkYliUMhd2jXhh7iw2KF+c7dzE7odj/7fvzFa4nOEa8QfPwQazAw018SEfnLoV6ogw2KC1pZoDZW+Cx+7UuRyF8nJ0bCW0epSzMn/0sWDa2aLRXsv719pDiIeksePbtZFmqlNWjZvjD7Y/EscstW2/FQdpyqrhZ7MnUk68/ypbY+JZF7L3FdfazRTimhAalVjaMIqmepxYtAzGu3aaHRZ3yrLWex1NNtrqbJlL7SbZWcc92dVtMLlrb0yuw4k1hCASeK9pJI0fCnuMtQAQED29dHZJLJq1MUFpccLTDhBjDX5bWgYCR5Y8cE+ntXLa2CZ8vkOAqsgZoJ0ujrgo4LRZvdDkFOYzjKiV9PSCdsBrmwrj18nJ3PXUkM74KAPO8wY3+DqtVj+ikI9oOD3T/L7fKxGQh2Sdh4jH7dD33xMJmoy0kGaO3wP0CHWW2olicjH5mqxMT4ihE1SsSJ1/bmcK8qf0byJKrHd3ZZu9FZCi4ZoRnAP5ixCSdX4fFHnOHrD6cuda1aYiEBPk5VOd5JWKUtXCSz0PegvwFFEOnTJ7NPknO40vGydnSGQY9fQPDa/iS8u27I6uU15n0j/b0PGApe8zIzfHuUd/Dffuw/sEwXZS0jHVjVjtTRQ/dFXyfyR25XXS53MffADfI3kfKrI+yQW5bYuHRMOB5orY6Yi54eiKTETPUBmTjg9ASAthcrTD57LoM4eU7azW8ESuBKmxzaKzCDlt03um2GebMkJhgEs/YJNXbBdXuAxUM5Xo7T1lREsbRQ5oAfqzbQSYERnQbhX4FhYJNUvF8UbUIrZxom67M3nArNCEbCyBCYlGtvx4 z1lRMYhe fpB6CZEvLGtrgn5BhGj9UQ== 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: TODO 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 Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 48 +++ include/uapi/linux/kvm.h | 15 +- include/uapi/linux/magic.h | 1 + virt/kvm/Kconfig | 4 + virt/kvm/Makefile.kvm | 1 + virt/kvm/guest_mem.c | 593 +++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 61 +++- virt/kvm/kvm_mm.h | 38 +++ 8 files changed, 756 insertions(+), 5 deletions(-) create mode 100644 virt/kvm/guest_mem.c diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 9b695391b11c..18d8f02a99a3 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); @@ -2360,6 +2384,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 f8642ff2eb9d..b6f90a273e2e 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 gmem_offset; + __u32 gmem_fd; + __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 { @@ -1228,6 +1232,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 #define KVM_CAP_USER_MEMORY2 230 #define KVM_CAP_MEMORY_ATTRIBUTES 231 +#define KVM_CAP_GUEST_MEMFD 232 #ifdef KVM_CAP_IRQ_ROUTING @@ -2307,4 +2312,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/include/uapi/linux/magic.h b/include/uapi/linux/magic.h index 6325d1d0e90f..afe9c376c9a5 100644 --- a/include/uapi/linux/magic.h +++ b/include/uapi/linux/magic.h @@ -101,5 +101,6 @@ #define DMA_BUF_MAGIC 0x444d4142 /* "DMAB" */ #define DEVMEM_MAGIC 0x454d444d /* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d /* "SECM" */ +#define KVM_GUEST_MEMORY_MAGIC 0x474d454d /* "GMEM" */ #endif /* __LINUX_MAGIC_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..a5a61bbe7f4c 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_mem.o diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c new file mode 100644 index 000000000000..0dd3f836cf9c --- /dev/null +++ b/virt/kvm/guest_mem.c @@ -0,0 +1,593 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include + +#include + +#include "kvm_mm.h" + +static struct vfsmount *kvm_gmem_mnt; + +struct kvm_gmem { + struct kvm *kvm; + struct xarray bindings; + struct list_head entry; +}; + +static struct folio *kvm_gmem_get_folio(struct file *file, pgoff_t index) +{ + struct folio *folio; + + /* TODO: Support huge pages. */ + folio = filemap_grab_folio(file->f_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) +{ + struct kvm_memory_slot *slot; + struct kvm *kvm = gmem->kvm; + unsigned long index; + bool flush = false; + + KVM_MMU_LOCK(kvm); + + kvm_mmu_invalidate_begin(kvm); + + 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, + }; + + flush |= kvm_mmu_unmap_gfn_range(kvm, &gfn_range); + } + + if (flush) + kvm_flush_remote_tlbs(kvm); + + 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; + + KVM_MMU_LOCK(kvm); + if (xa_find(&gmem->bindings, &start, end - 1, XA_PRESENT)) + 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); + 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 const struct file_operations kvm_gmem_fops = { + .open = generic_file_open, + .release = kvm_gmem_release, + .fallocate = kvm_gmem_fallocate, +}; + +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, struct vfsmount *mnt) +{ + const char *anon_name = "[kvm-gmem]"; + const struct qstr qname = QSTR_INIT(anon_name, strlen(anon_name)); + struct kvm_gmem *gmem; + struct inode *inode; + struct file *file; + int fd, err; + + inode = alloc_anon_inode(mnt->mnt_sb); + if (IS_ERR(inode)) + return PTR_ERR(inode); + + err = security_inode_init_security_anon(inode, &qname, NULL); + if (err) + goto err_inode; + + 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)); + + fd = get_unused_fd_flags(0); + if (fd < 0) { + err = fd; + goto err_inode; + } + + file = alloc_file_pseudo(inode, mnt, "kvm-gmem", O_RDWR, &kvm_gmem_fops); + if (IS_ERR(file)) { + err = PTR_ERR(file); + goto err_fd; + } + + file->f_flags |= O_LARGEFILE; + file->f_mapping = inode->i_mapping; + + gmem = kzalloc(sizeof(*gmem), GFP_KERNEL); + if (!gmem) { + err = -ENOMEM; + goto err_file; + } + + kvm_get_kvm(kvm); + gmem->kvm = kvm; + xa_init(&gmem->bindings); + + file->private_data = gmem; + + list_add(&gmem->entry, &inode->i_mapping->private_list); + + fd_install(fd, file); + return fd; + +err_file: + fput(file); +err_fd: + put_unused_fd(fd); +err_inode: + iput(inode); + return err; +} + +static bool kvm_gmem_is_valid_size(loff_t size, u64 flags) +{ + if (size < 0 || !PAGE_ALIGNED(size)) + return false; + + return true; +} + +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 (!kvm_gmem_is_valid_size(size, flags)) + return -EINVAL; + + return __kvm_gmem_create(kvm, size, flags, kvm_gmem_mnt); +} + +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, flags; + 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); + flags = (unsigned long)inode->i_private; + + /* + * For simplicity, require the offset into the file and the size of the + * memslot to be aligned to the largest possible page size used to back + * the file (same as the size of the file itself). + */ + if (!kvm_gmem_is_valid_size(offset, flags) || + !kvm_gmem_is_valid_size(size, flags)) + goto err; + + 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 = compound_order(compound_head(page)); + r = 0; + +out_unlock: + folio_unlock(folio); +out_fput: + fput(file); + + return r; +} +EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); + +static int kvm_gmem_init_fs_context(struct fs_context *fc) +{ + if (!init_pseudo(fc, KVM_GUEST_MEMORY_MAGIC)) + return -ENOMEM; + + return 0; +} + +static struct file_system_type kvm_gmem_fs = { + .name = "kvm_guest_memory", + .init_fs_context = kvm_gmem_init_fs_context, + .kill_sb = kill_anon_super, +}; + +int kvm_gmem_init(void) +{ + kvm_gmem_mnt = kern_mount(&kvm_gmem_fs); + if (IS_ERR(kvm_gmem_mnt)) + return PTR_ERR(kvm_gmem_mnt); + + /* For giggles. Userspace can never map this anyways. */ + kvm_gmem_mnt->mnt_flags |= MNT_NOEXEC; + + return 0; +} + +void kvm_gmem_exit(void) +{ + kern_unmount(kvm_gmem_mnt); + kvm_gmem_mnt = NULL; +} diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 2726938b684b..68a6119e09e4 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -796,7 +796,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); @@ -1598,10 +1601,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 @@ -2010,7 +2021,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; @@ -2029,6 +2040,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->gmem_offset & (PAGE_SIZE - 1) || + mem->gmem_offset + mem->memory_size < mem->gmem_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) @@ -2067,6 +2082,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)) @@ -2095,10 +2113,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->gmem_fd, mem->gmem_offset); + if (r) + goto out; + } r = kvm_set_memslot(kvm, old, new, change); if (r) - kfree(new); + goto out_restricted; + + return 0; + +out_restricted: + if (mem->flags & KVM_MEM_PRIVATE) + kvm_gmem_unbind(new); +out: + kfree(new); return r; } EXPORT_SYMBOL_GPL(__kvm_set_memory_region); @@ -2434,6 +2465,8 @@ 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_arch_has_private_mem(kvm)) + return KVM_MEMORY_ATTRIBUTE_PRIVATE; return 0; } @@ -4824,6 +4857,10 @@ 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_PRIVATE_MEM + case KVM_CAP_GUEST_MEMFD: + return !kvm || kvm_arch_has_private_mem(kvm); +#endif default: break; } @@ -5254,6 +5291,16 @@ static long kvm_vm_ioctl(struct file *filp, case KVM_GET_STATS_FD: r = kvm_vm_ioctl_get_stats_fd(kvm); break; + 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; + } default: r = kvm_arch_vm_ioctl(filp, ioctl, arg); } @@ -6375,6 +6422,10 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) if (r) goto err_async_pf; + r = kvm_gmem_init(); + if (r) + goto err_gmem; + kvm_chardev_ops.owner = module; kvm_preempt_ops.sched_in = kvm_sched_in; @@ -6401,6 +6452,8 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) err_register: kvm_vfio_ops_exit(); err_vfio: + kvm_gmem_exit(); +err_gmem: kvm_async_pf_deinit(); err_async_pf: kvm_irqfd_exit(); diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index 180f1a09e6ba..798f20d612bb 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -37,4 +37,42 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, } #endif /* HAVE_KVM_PFNCACHE */ +#ifdef CONFIG_KVM_PRIVATE_MEM +int kvm_gmem_init(void); +void kvm_gmem_exit(void); +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 int kvm_gmem_init(void) +{ + return 0; +} + +static inline void kvm_gmem_exit(void) +{ + +} + +static inline int kvm_gmem_create(struct kvm *kvm, + struct kvm_create_guest_memfd *args) +{ + return -EOPNOTSUPP; +} + +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 Thu Sep 14 01:55: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: 13384022 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 3D9C4CA5506 for ; Thu, 14 Sep 2023 01:56:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 336536B02AC; Wed, 13 Sep 2023 21:56:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C5DA6B02AD; Wed, 13 Sep 2023 21:56:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10F496B02AE; Wed, 13 Sep 2023 21:56:09 -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 E8FBE6B02AC for ; Wed, 13 Sep 2023 21:56:08 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BD450C0E62 for ; Thu, 14 Sep 2023 01:56:08 +0000 (UTC) X-FDA: 81233537616.26.F222144 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf14.hostedemail.com (Postfix) with ESMTP id F11B610000A for ; Thu, 14 Sep 2023 01:56:06 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jSirx3pP; spf=pass (imf14.hostedemail.com: domain of 3NmgCZQYKCD8tfbokdhpphmf.dpnmjovy-nnlwbdl.psh@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3NmgCZQYKCD8tfbokdhpphmf.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=1694656567; 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=tmZ4Pf+Lkz5wRFy1K0dmYcmTbZlsWWC8lS02Q/mSk0M=; b=zEXyC98FcYfwY1hMTxB9EADM8qlPxHbIiq69g1MSoBukzVCYN3fkU8/jbZ0nSaaYWnOZ0J +NVpHNWSWVzoO04ZVDczGnXQ9KGwh9VCHaEIBiU6vYs6TCaR0bHKojhzdPdziFw4gXOvzJ eEvJWO6iHEN5ufgnzA/AeycmJJ+jJak= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656567; a=rsa-sha256; cv=none; b=Y5rdi3A3pS7nvm+trfrUktrW8oWkQtTw8eVmaSa1dS02ln7WTF4GITDWZDiNm/DXBQDYLG QXCsdRAl/VeQ9yNPXdIpbr798zBrh25VoCwxBa+x0MAI5zfvExE3M1zaKp5nI3ttDkAlbn LBjyk95wmEx0mfQU6BH+TLEZIpkBKA0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jSirx3pP; spf=pass (imf14.hostedemail.com: domain of 3NmgCZQYKCD8tfbokdhpphmf.dpnmjovy-nnlwbdl.psh@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3NmgCZQYKCD8tfbokdhpphmf.dpnmjovy-nnlwbdl.psh@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d80256afb63so3105051276.0 for ; Wed, 13 Sep 2023 18:56:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656566; x=1695261366; 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=tmZ4Pf+Lkz5wRFy1K0dmYcmTbZlsWWC8lS02Q/mSk0M=; b=jSirx3pP1FtwE97w8elw2Zuq685ZEwpUM2FGOzcwC7T5XKN5Rpq8P7P3/Tm40C3okC AmbDfkIOqJG4SG2NZ8IwRTwL/it8cttwil7RW7sPl5K1VfQ6vs+WBc+G0+ANJStzfxnX 4GfyW79/lU6z5TcCXfmu3r3wcJPwF2ufv8o9YuPua2R/jyX5gD8wj3+AOJw/suSJ85x8 Z7cNG1iZ3VOkT5zFS6xYXZ22ee2fFE7XjZQHFbJLz7FY7uo+kUP+1dcVbQTZXrkDCRIV qnvDlkainnZ/VP49iI0VlfX8hZgaCSBlgqgUSeCdC0p4cj5crg14dY7oyVWTRkbCl7tH 4ISw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656566; x=1695261366; 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=tmZ4Pf+Lkz5wRFy1K0dmYcmTbZlsWWC8lS02Q/mSk0M=; b=QUNeWaxxwUm4OqTCDbf/kP2c8JrkeNubUuaq5jMDRISMm6Gwux+8vopBTnrwi0CHb1 wPlLupeL/bcfx62txCk28ynSzeLqndDGAZcGMpa457CXCjNm+P2ixF6DKZCpEPb5McRE BnH3+L+DUYSCgMtoy3wSM96dFWScrs7yjtxXOj4mtIU1SwQlC8RvbJNSCgJ9HvV0XMd5 aVIj5ycXwwd0kEwBT+FIW9vzY7T80e+bOSORUuQVh2qXY9H6SyDTEUZ7w5aI9kzObR4j A+mduDrtlrR0z1kNmP7B7JohkjMmNp0jz9LD2KGybcmBSVGZ/F096gCnSvXu4SM45Lfw fOaA== X-Gm-Message-State: AOJu0YxI3By6GwIa5chmcdZ8EdG3dvBq8gmiawlU5kejpvJG2Xcya146 Jp5o/fxA3prgiCaVR4QyLWk6guFj+A4= X-Google-Smtp-Source: AGHT+IEewm62VrPkwfcYWKCbnDgTMcslgQzN36+Ag8qFabvQ71jteqZnAdWKs/X98zA5hLrpiwS+nbPNXc8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:ab47:0:b0:d7e:78db:d264 with SMTP id u65-20020a25ab47000000b00d7e78dbd264mr13061ybi.5.1694656566137; Wed, 13 Sep 2023 18:56:06 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:13 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-16-seanjc@google.com> Subject: [RFC PATCH v12 15/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: F11B610000A X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 8st356j5463dbqq7n6d4h7ckm7jwyzfq X-HE-Tag: 1694656566-858912 X-HE-Meta: U2FsdGVkX18vSMgEoxMQ599ze2aYdCxTS1Ingu0wLSvjFalipncEWwuLNVz/234MVXMQ0nJvlUSEx5qlFx3lvdizacwcgmjRNllZWZxmV04On1zBHbamjtXoM/zbbFmb/fIqReASS5bJOm2UNtEr21m3y1/+iXzBj91lmb8ky1EmQRnkymWXeomCaZAshesXUXzsQbTZMipWuTsNkuRXFJn17diHsBgqYeJbj+j4Bq2Z2YuIxdYvO6hftCkOA8A6wm340xsnaFJQkGeigXPkRGUDZ1sOYlsFmAq2m8tqsG3Ghv2dY7o7AGS7jVt+LKHAelCQNHnmLEa3+2a9Bu2ammTm31hf22oh7OHpaGlZfzjl4LF0Lfn5myEwyji2UtQj4NILN89DwZmV7hvYHNIPw3IVlO+j7zLd7wPxagfL7PdHX1FndVpmdrTROvSNIa13SKvnQolHXzAXQY41iiHtSNgUE3KDuMWMyt8cw2sVNvL9BagJiy0suBxWM5/anqGlzd5gLnddqxIF6a5P9mBVoHCrLP6Vj87sgSRhLOn9j/LmblCvBo7O4S1cT24aTqJBOl2wtL2c1kWpxW8WykcN/O0uE6R0aTYl0IzNyjxUK7iLdS9LS7FOFDybnh8I3u/LK6xwgAfIJ/YBXNm83xRUTcn86SSW3UpOPHc/EqOHy2k96p73w3Dld1KKWBZJlDU4fR92YpRYFZiT/+XvPReC3wcwohmbD04c1N0Aet78/eBgmz3f+0FsFwa3J9mxGgYs0eAWGgi6xK10wOTDvq+PHIUTl4pOKk4GuVPuDTpcdG3pG1QaY5pgPGcju8rVkAYsfxdmcaIkQ2VZLd36OFTAIh2Ub8Yl2n3i/6kc3ENyTjf1t+7GjxnO5L9wCpxx22mrFzsqIp+RzxKhJciU1A2pWsmvatUlVe0RIfQHYeqf/RobPFmmtb0lv/GScmrPi6EQiGthErZgX+s1FhwyI8k 9elxkUzC pZeH26Y/WLAV2oiDscORN/yQFJxn8T2Vk4Vsb80yJaiWBss9+lnfLRAMuwpUEp2/2f4zKt7vFK+r4bFjm1kN512sVbv+RjQ7OpMd9DqUGw32pW5x9fT3K1fuWTPwB21t25snaEz1CNQDbLUSpJThuexRA08zcMBj0wZ3UFrsl0+85a3Rw3b5fNxHH2BBt+kFK+R5LhrxiAkWV1KeLiR3hA0ku+bawukqcUV4NlwgBwltoUQcEBXy2ibxDluGbM4e01tzjkw9x76jBouYvMWcXc/tY0/qGtRdVHqBSGdjP9BmpFvU983r9coPP5JvXem9puX08MwpskI9JiNuflDL5hp2AISXE8hggW+vR0/5KPn7t+qMlr/pD2tU+gR+fw0vNubUNN3p7Ph+wFSpu3Eepxs2/l7T1g/zP+W7exYlRiCK49exsc/cfGNkI5DJobGf5XtHPKz7wmxVYG9uBGUwsO4LIvniY19mDx1FmyGyq5LLouSR+hZrg7JzdP8SLdxg2VsHCmAHRTjYxS40uDTMG158eJbUlO++iNULdcdnhDtUWEUtgYgpz9kOl44yZ0BpEIHKyuU6VtEtQ0bS6oMAnMikbazONwgv7IMy2gYkiKwhZjlYNJqh9mgW+pPuzhwhCMB6HjICxskt02/+qufr72BjJB16mRkTfLZifGeSNJoinpmW1htbDe9mJ57FDflrSWnJOLf9I/qB7ENQ3iA2QHL8ClQfYUUX0dU4/Ml9LRLd37bNFjSV3EYht4QA3AiKJxxkWr6BFjiFRAoVmQXy10YqbH9K5m7OhW6akhO2uy4S5fbQ= 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: TODO: writeme Signed-off-by: Sean Christopherson --- include/uapi/linux/kvm.h | 2 ++ virt/kvm/guest_mem.c | 54 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index b6f90a273e2e..2df18796fd8e 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2314,6 +2314,8 @@ struct kvm_memory_attributes { #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) +#define KVM_GUEST_MEMFD_ALLOW_HUGEPAGE (1ULL << 0) + struct kvm_create_guest_memfd { __u64 size; __u64 flags; diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index 0dd3f836cf9c..a819367434e9 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -17,15 +17,48 @@ struct kvm_gmem { struct list_head entry; }; -static struct folio *kvm_gmem_get_folio(struct file *file, pgoff_t index) +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; - /* TODO: Support huge pages. */ - folio = filemap_grab_folio(file->f_mapping, index); - if (IS_ERR_OR_NULL(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; + + 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 * zeroed before being handed off to the guest. There is no backing @@ -323,7 +356,8 @@ static const struct inode_operations kvm_gmem_iops = { .setattr = kvm_gmem_setattr, }; -static int __kvm_gmem_create(struct kvm *kvm, loff_t size, struct vfsmount *mnt) +static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags, + struct vfsmount *mnt) { const char *anon_name = "[kvm-gmem]"; const struct qstr qname = QSTR_INIT(anon_name, strlen(anon_name)); @@ -346,6 +380,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, struct vfsmount *mnt) 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)); @@ -396,6 +431,12 @@ static bool kvm_gmem_is_valid_size(loff_t size, u64 flags) if (size < 0 || !PAGE_ALIGNED(size)) return false; +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if ((flags & KVM_GUEST_MEMFD_ALLOW_HUGEPAGE) && + !IS_ALIGNED(size, HPAGE_PMD_SIZE)) + return false; +#endif + return true; } @@ -405,6 +446,9 @@ 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; From patchwork Thu Sep 14 01:55: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: 13384023 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 37EC0EE0203 for ; Thu, 14 Sep 2023 01:56:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 074A06B02AE; Wed, 13 Sep 2023 21:56:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F41056B02B1; Wed, 13 Sep 2023 21:56:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6B896B02AF; Wed, 13 Sep 2023 21:56: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 C187A6B02AD for ; Wed, 13 Sep 2023 21:56:10 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A156F40C99 for ; Thu, 14 Sep 2023 01:56:10 +0000 (UTC) X-FDA: 81233537700.14.71DCAED Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf02.hostedemail.com (Postfix) with ESMTP id DAA4980007 for ; Thu, 14 Sep 2023 01:56:08 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=bwUni2QH; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3N2gCZQYKCEAugcpleiqqing.eqonkpwz-oomxcem.qti@flex--seanjc.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3N2gCZQYKCEAugcpleiqqing.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=1694656568; 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=JU2E1RY5jYghlRVsn3OuL2a/W8MVmnBu3lS2NKbXZTA=; b=B8Y/snSiDmvXKRkGUT475WF7OOKgJwIMe4Ss8CbXedABve57Hlq9oclmZPqlfkJ7ddlQXU 2B3BEtp66SAB/cnVs/ZssqL35uc5tXDNGvtbLNSDwvZ5cyCwDb0n0dNQAUzny2Pg10jm97 lWsrcdCzKiEK4IYgS9zVmZ2Y4ah9VNE= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=bwUni2QH; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3N2gCZQYKCEAugcpleiqqing.eqonkpwz-oomxcem.qti@flex--seanjc.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3N2gCZQYKCEAugcpleiqqing.eqonkpwz-oomxcem.qti@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656568; a=rsa-sha256; cv=none; b=X7ngsLQEx83/f5tS2Zfyb/GcdkWts5KE0m9lICoGhar2ot8kb36mDL0f5zNwpvr8m7FnT7 hvqfFsGEUR3PeDyibiXR3ihObNyuH/LCaXkCEVsnFuKViL0EyXIQBdRcA1kg7+k4ulkfXK qRiRlVI+AiIfVvRcaAkOWc2uM2XTxmQ= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-273fa168866so433203a91.0 for ; Wed, 13 Sep 2023 18:56:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656568; x=1695261368; 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=JU2E1RY5jYghlRVsn3OuL2a/W8MVmnBu3lS2NKbXZTA=; b=bwUni2QHbrALVQDofU3OaNr0lvVY6JjK4YYPK+k3GUA9QVMM0pM3NYJoUHFk6XBcwS cpy1nxWQp3+N9POo28Xzmy+JNxAumAa0fpYR0s9rU66Em96H6XVHWvJJKFFJOVoUZJ3Z ROoo2R11/tXQvxXFapSIg6kaxFUB03a6c/2Wl5T0eDCMMf4xQ5K/GOerayREK5bmmdYH 3c9LT8S19CDdS+ljSAnJU1P63eq8o7HAEfrnZaZ8saXc/Fnmms41gbBeNr8El/TCAIUL FC35pDeHgf2VLkkZrX4FxnM+LuhpLI+HnmPR4MDUP3zSZ00fXpj4WDtK5hRTq6K1JYSu 9eYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656568; x=1695261368; 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=JU2E1RY5jYghlRVsn3OuL2a/W8MVmnBu3lS2NKbXZTA=; b=VvUvYWKIMojbl4QuZYoq884f3rlvJZZpv0UHsGuvSUfKo7Aw4uzWxisOBHrmsH4p3G woqC97ns2Pucw4NpiK6aQRxfTzGCWKA729hIpqpWvkto1iMBZ/KHy1sjtND6weS8Ppld qHKUIm7rIykmb7HSCXYR5xH+nBKX6zLAq2HE5TKA2ONEQqvPl9RrzC/+BGShuN+q2f20 y+1K5fvXiwU9gwdGR8QYI69ui8sYvsd01XsrhkyJQ+guqb/Et8bKrjbXpYWM35urO/Sw 3khGtflDtnN2laqXinqARH7bDntaGEQlITfzt6MrJFYR/nA9k8hHZgbEN5IN0Z9VwYDb Nbiw== X-Gm-Message-State: AOJu0YyBepI8J5/SPf+GBd3/7HF4o2IwRDTsHgCUQI5O4QdpV7E9W55V rxMGA4GfrStUIIevbQ+Fck2KpSv9OcA= X-Google-Smtp-Source: AGHT+IHMQPQ3k0d/UtHxD4b9hamrYXMHgCSNyIGKdggb9SAJfpQFP9N68H8VlXpWCI6NE2xrZ7HMeRXjSd4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:da8e:b0:1c0:e87e:52b9 with SMTP id j14-20020a170902da8e00b001c0e87e52b9mr204006plx.12.1694656567699; Wed, 13 Sep 2023 18:56:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:14 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-17-seanjc@google.com> Subject: [RFC PATCH v12 16/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: DAA4980007 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 33yzcwjmua8rofbuq5goq1xx73fb8pxn X-HE-Tag: 1694656568-853677 X-HE-Meta: U2FsdGVkX1/SxNqHgWfWkkvX0CPeJK5PeNZ5lDxQdlTEHXqHfp7OKo7kJJsdwkcOWj6/GEln9glR/dEjIN4mKwnxRTidI6WlMH0gOw9zRzBxZa2WVIZpIYTGXGA/D1bhJ6rB4vVrN8BnIZlwPumiHZd2UEg3GGTkmf2NbulAjFVik/nd0FLnMdcZm15I9zld3Lk8xmPnybRnb4MKpRQCJD2BTMH7wte49fvWwK8ZMmt4NTDK6uRs1g2U8Bmwak9BtfH9ndS2H57NcwGTOJw5Pj/FuOnHgRAU7cPvu4Eh2wrOS11wmomcaalxNLEOBFrRtaczTjNhfU8Kmsn99F270ad7Qfwyg0nHmw5YWV5BmEdE0i5POSIe6jvWxS6G6xfEdn2/ZNHxi2YZyB8nWcu7jjTwgikXidSnDKC3np7wlbZ00FyAm6JUSrYcM1uzPkVcTUAvoblyjqY3Mq5ThECAVGu/sK5SQE/ASOTm3638Ycpfu8AKqQoQLWimi1VdwmF7NyMZO9JFxBoqFFsAuBLDbFFo4SKZf+StLZVNb282EVEI39PdIG87ZuQevDh0S7QWjY9AmlAhHznCjJ8E7R56xOJDfV1VlJKJIWVzf07PtplrltKm00OWisIzbbY1F0/DemwvNtR2CFr9hfsBtpI0L+ga97O62rMyJmO7jMayixofMhMik1ICgdqDNzJr0NzQQ5w8fQ+uAGZbgnj9vFnTNaImJt2NkmTfuv5FwUAy1ss8ARFREE/Kv1GigMUmuYw/kjj9Euf5gfVBa1XgCi4BlHXIt+E2SUd0J36mLxYrImFpk4tAiyVS9qphMH+xPR4YpTbhwI5jIPYmfS0xWTVxLeFRj4EJEji9bpE8cBPZiiCrvZJAzur+f/QFqFOIBKmV/frxdqesqeBlYPZccjvmtG/svUptWhay2F6TZLTaQFCokIbFAXcw5+S8pPNuvG/pypmA4WyvT6/kiooJhna RunEM0B9 50l2ztHfn7fIJIm8UArufrI6t3x16a06Q3kYZd4Yif3N6AJbNKsEO3NpncdPXAEkAoqh2pw09iSt/lJ5tCgbReGKj1+9OZFUSOvWi2jxdM5S2KvyYggInXuMaQzGSuC3AyHSOQNybS7kHCGtElXaIZ2B6h5WBnu/w9kRB2cC3lSfDCwO4/8tpBdUAA0GL5xU6IdiJ5/U++1xVJYBoC6jbC53qBvDCSNCR4FKYc7pB5AOcOLqK2zHjef0wu8iVo9LBrfxArwWhykXp9FXHYj9BhdHTopbYOq1vf9A0p2cH1cMeRBZ/jOstZjAWUhld1MWsRZxlrFMzvRESN857dPL0pVkMvrrp99e/rtvlyknWyZ0uoaD3hjgtnjql6wyN9qkEiwUmEyVqC++GIc8m/2/gw9bBvLfO1XYAJ+RQ6YoUruB//Bt5vEvVs4gc3GMv/c9aafBOHRv0bXDrxPP+nV3L2Y/BXVt8XBeQj3TMEEwQW1po/qqzYmKonRJ38psJvYLcmXbpV6TsbezJeWMpPMtFD3hxgQIpudQ4C1AQCrjpLFKFQITvBonHbtm+O/i5H2ShK0qaN/rMSNxtz5UngOLR0df+oP7JS3vLQqFotYUYoWAGZRb9Beg/eYr+SogH5ts+Y6xMtVWAlBq6lEH/XHtFTCRxgAtdD/ovKKhUetjdt3ecz/r8B5Zljgs116TTVdULg2MUTTUzts1Xmfxrp3GUaep6FS6MBj+Ystkm0BHlZbyopSLt3H0/W2TFRiTn9z5uGEU3WSXDUxn7h3B0cK1nd9oYYC278w0NJ3mLITg+NnCi3xyoXvLBZmTrMNWdU+68X0Lmtkx1/cGdM/E3nN9hOOOR8NIsss116RuwUw+arkvE++6qPJFAiy3WKA== 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: 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 --- 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 8356907079e1..8d21b7b09bb5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10951,6 +10951,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 Thu Sep 14 01:55: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: 13384024 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 BB0A3EE0213 for ; Thu, 14 Sep 2023 01:56:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BC4856B02AF; Wed, 13 Sep 2023 21:56:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6EDA6B02B1; Wed, 13 Sep 2023 21:56:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E79D6B02B2; Wed, 13 Sep 2023 21:56:12 -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 8BED16B02AF for ; Wed, 13 Sep 2023 21:56:12 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 691F71CAC09 for ; Thu, 14 Sep 2023 01:56:12 +0000 (UTC) X-FDA: 81233537784.01.140E4EC Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf10.hostedemail.com (Postfix) with ESMTP id 9B026C000A for ; Thu, 14 Sep 2023 01:56:10 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="SYxsf/oG"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3OWgCZQYKCEIwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3OWgCZQYKCEIwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656570; 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=hXg+c13z5FpxpMh0dTDS8MDb5ldwgANdA63niVsOR7o=; b=MCqdvsRAQossvSjKXFhbzneEjOjqrhYa+Ph9AtvRBopR37xzWhCLmcevkPcLwKhM0zWk+O Zn0P6ru7Bep6eHRmEDpkIHmtQ01dbfBRSMlDPNzGLVGZm6GkiFHtYX6avSLetwHdmAeqp8 yVuKa8rwBr8wzBRSLC3pBd6800JXVwA= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="SYxsf/oG"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3OWgCZQYKCEIwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3OWgCZQYKCEIwierngksskpi.gsqpmry1-qqozego.svk@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656570; a=rsa-sha256; cv=none; b=1yCVFmExtHCYmtVW7bsda36Q9LeTwXYKn5HbALt4qeB1QzcxkyvgBuT8GS3AMFjv8I7607 13iIrsCHuG4eVTZMk8SoP+XWTErxt2PeINcPuDieIdikojf8O0fERaXfKvXwijJnyUH5qx m7fKe48BeTwJDcuJeg7Q9JDL5A6Qq7s= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-594e5e2e608so6332947b3.2 for ; Wed, 13 Sep 2023 18:56:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656570; x=1695261370; 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=hXg+c13z5FpxpMh0dTDS8MDb5ldwgANdA63niVsOR7o=; b=SYxsf/oGak78MZ32BMpM4wGhqTaJipF+Z6cpincYO37dzUQz5/0ZqjHLx369/Ty7W5 Ln0Efofv58SQC6Aw8Eb/MGCdZKMiTYT8CvBhzqrV8O/3P6VfwardnR47JdlS2+9EHptw WPcen5yOQ5gvHGrijRVJvaE1OLbGKZRrdv+OQKEog4Nt72DfEyvYF1V0d4ZZXkhX4QH5 DsKN1F1SANijoEOttMV/ZxjB90eARYCiShS2bmKOKyUm98TiSC+p8BWBlOseW3OYFOit XJcAuLLok9xI+DNsxTD+79zvr9WNaPW5sHlM1w4mv1h3TJfDwCpHVq6YspJ3GsqAHcJa ij5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656570; x=1695261370; 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=hXg+c13z5FpxpMh0dTDS8MDb5ldwgANdA63niVsOR7o=; b=SBexgDtXGXl8ZZPeIouRksp7i6p+peigMhOTMIcj9Qbh/4q4R82K4NcVl+yldWZgKk uO5fwkO1S5eUMO8od+y5pErduMPz9EX1oRkSf6/KnQG447gIibQ1/XEGVaKH2AYVXrKJ Ty6wBiXd8CKPh/trmaSvuKiGYZ0YuEtpiCr3Bb/s4u/g8pcTlVQe5oTzCD3tnEhDrfJR ZRZGpOSeynmcUn71KPWdg1TGRKsqyZFwdVW/xg3OVCj9QUmbLnonKACplvUOozOHJzHo zz7pM2eRoFLziv0prnjDZDHgITur4OdVd2wqMRVAkQlCjzJgccNjXcw4B/108JTWLqg1 +xfA== X-Gm-Message-State: AOJu0YzebY06FQcUYfhuR8ex9ptfLiadBIUBFhfaYB6USl9+AVqVIAki wgoxdxzSyeoHraVAAp8fwAymvGl6r3c= X-Google-Smtp-Source: AGHT+IHPiLkfoLFlIeGbzzjp2cxBfgGGv6hvIzEJvebBRPzJegHp6YgbGy/UCdWVr8MsxooaabenVz16PQA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:72a:b0:59b:b8bf:5973 with SMTP id bt10-20020a05690c072a00b0059bb8bf5973mr115588ywb.0.1694656569799; Wed, 13 Sep 2023 18:56:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:15 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-18-seanjc@google.com> Subject: [RFC PATCH v12 17/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 9B026C000A X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: ki6tpaqmqpdqya1ifuwh64esxkrxceg4 X-HE-Tag: 1694656570-762711 X-HE-Meta: U2FsdGVkX1/LzmAzPzLtcYEYc0kHurf6hmslShlX5bLTK64mb87CqIG0kEHVBMd7XPvdzzXY0QWS8x7GJQ0I8L5Ox5YiFq68tzATe3mSMd9mPZFe3XLnSJ1800xdPmH9ZtPi1/1wcToNDdb8665aCG7w6O9/+/Nf3B3dkiPLxOS7LImQpu0Omxbpk2ihxIc9GpnIzHviQlZGnd4ru52TQ2LoWml47UXDUVPXAxVilK+oHJqZLE1gf8qXevb1NdeYTiogdZAifWv1tQoymr99Vfa2raumMH1zPlb/Wcjti/B1O20GtN6VyNuD0j4kdpNqAqGeYsh/tYaDCladVTX7QinXQrfOqoQanZq07JQtRFyqxsWFIOixrZZ/b0Sd+yZkyl/H21qWrIyjIHmQabdyzFaGGckslPYh5OPzopOyMXgQHvCbIaCkf37GwEYRvj/Exka/1764Vs48wnFEVA84EDIad9+Ykavbmbivx91S82N0IJiCNscx0n3X9eirURl6TbEwTsarD/TkymgTYjKypQQy/LseV12W5e98bst0rfZ6J5WLG7igcooCKuItSKSCQ/o9i+Whc1AhrhdXtT0GFZzPE0Tl9yhyxtXUN8RActE70PwPWfAaSIe6395Yyqszl3LxlDA/nQziGTeG5j9nX1XBFZyMWXE5HlJvF9/aoPtzwyYNkpoxe0VkozqMDbSwy6GAZli27CNX43YNbgzl1GnDQk3cQpHvFsL+3KqLxiXF58y6ZagCdy2HDFdK8QUBFmqk+beqq2wr6mFNV5rijeFTI3YOpzNcA8GlDOGHnq/cPcj1bzxcbyafFzLdaiSXyjd1slxaxm2e1q+e+IvgwY5jsTUfmZkxwP9nxO7zABxQpNXWYCNyky941QZRWGqyCqJ8flhOD2x83dsE9kavwxCTpYd9M7pMFUa+opqpinIRYpuUgwpOdNXjQ9d3wKWOtPnuMYQfKMnEiWfsBFD AfpVPcZ3 HseBGADKKh1K70IQa/dq3WOTz6mN8SmxQydx3arsagDPt062C/q/r81upuCjBmnc9j3BMc0BBiM26kihM09CuwiqKc1gz8BkJBbsx8qV3yQWELtmorSzwZBRrjPQaBCr8nsgok/CZuBfj9fDLLLx/1u+Kl+FEsE6zDaDC+Bg6CNjWlLWjH6UbbVZbC+R8op66bWxdsKh9pzPVLrrr+wx+ti5oXCX7Lc9IRt4pD2zsQkgZ08TEFzn4Hvtm7Iw3A5etDaLYSL5WQ5M48KNAZuq4XbbNXDH/s/yvj1+xHMKFH/kBlKH3QhtQ7bgT9lRKW05TWXTG+e9cQKYAZH+ShFFFnjpO37VIqJEchjOWVLMRCGW5zP3FTQEE7imv8mXXRh39Dq2Igecd/MSA/KcaB6q1kq2i9O4OZihHm8SYVxqJWL1X+nsT0z2viSDImkcJTBzMk57BlkCdLaHs++aHLIhX2HCvkat58oPj2bAQc5hGA5LgcRa+QwPLFBHr05AQvZcoB+2JyyfiOKYWDCcjAvByoTBtob1atQk1obpzivrw0so9JCE5AxfinANRGcGAfl2dT5Xgd9l+OkjYP1OA2H4ti8PXpNqJqklhNJ0l8ZH9TjUe2hxh4d84GCHBebUIDcY9Vq70fwqko7bXVL2YMYeYQTNCWSffaUW+mgAifLB+qe/ijIfV/kIuEBc4B/D/r5wAqclK3AIYA3F9ZbwHgsiqJhFQSE9e7+sBUA2QPMhDJ/PDGqzEBETw75rwOY556aiqwM34XhZQm3fDIVOEUmvzEdBEWn/IBc0ANj5sHIWxBOIto0s= 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: 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 | 152 +++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 4 + 3 files changed, 157 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 3a2b53483524..91a28ddf7cfd 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1838,6 +1838,9 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu); int 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 0f0231d2b74f..a079f36a8bf5 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); } } @@ -7172,3 +7182,141 @@ 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); + + /* + * KVM x86 currently only supports KVM_MEMORY_ATTRIBUTE_PRIVATE, skip + * the slot if the slot will never consume the PRIVATE attribute. + */ + if (!kvm_slot_can_be_private(slot)) + 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_slot_can_be_private(slot)) + 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 8d21b7b09bb5..ac36a5b7b5a3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12598,6 +12598,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 Thu Sep 14 01:55: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: 13384025 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 988A4EE021C for ; Thu, 14 Sep 2023 01:56:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A13D56B02B2; Wed, 13 Sep 2023 21:56:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9C0646B02B3; Wed, 13 Sep 2023 21:56:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7EF6B6B02B4; Wed, 13 Sep 2023 21:56:14 -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 69C2D6B02B2 for ; Wed, 13 Sep 2023 21:56:14 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 418691CABF9 for ; Thu, 14 Sep 2023 01:56:14 +0000 (UTC) X-FDA: 81233537868.16.D53BB35 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf25.hostedemail.com (Postfix) with ESMTP id 68179A0003 for ; Thu, 14 Sep 2023 01:56:12 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jvd73Hw5; spf=pass (imf25.hostedemail.com: domain of 3O2gCZQYKCEQykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3O2gCZQYKCEQykgtpimuumrk.iusrot03-ssq1giq.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=1694656572; 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=tHJ1oIsuZhhB0EvSSZcC1bEv2oeM3xd3VRRUpH1LT1s=; b=4YDVVlNymw02+ahfrZ/QhAFwqYs06NiPaGs1NhUjjJ5Kzm37J4qMc23QwKCHATf0wxbD9F nhAA4SFpx5HCwowpCn6iOS/HF7GA6GJSO48+IgVzNVXaDsol0xrH5mIREOE6H1z38ukgDA GKAomP4bGVb9LHs8ix8Ed9mEE7F4RvQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656572; a=rsa-sha256; cv=none; b=5HVQKraDTqOT3+bP9zrz9VGcKYksFRScTHdbtBCt1o2pqb84Mtrd+P3QV0I+8cvHGmVt8M ZYdtljxBodluBA+2Y3Pkv2sBoFEdBpGC+LVbCIjf8YUoI9LnbElU1CQhFSe0gbc3a7NFOI 5iQBhnlS9rHNrCcbqIFoZRaxOyodPPc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jvd73Hw5; spf=pass (imf25.hostedemail.com: domain of 3O2gCZQYKCEQykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3O2gCZQYKCEQykgtpimuumrk.iusrot03-ssq1giq.uxm@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d81646fcf3eso627830276.0 for ; Wed, 13 Sep 2023 18:56:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656571; x=1695261371; 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=tHJ1oIsuZhhB0EvSSZcC1bEv2oeM3xd3VRRUpH1LT1s=; b=jvd73Hw5osWwylukpEP/Yl7cTwQg883+g6HBhnxuWvR7Y3psVobifQedPgrPXLJBLp zJO0CoD/0wLEeNHozNag2fQu8I+UOP27cjFAfiCQPKpPGDQ7QqQ9Wo0EfUliINmkUDSp c2TOumOqDHsp1rQZXcHM5J1BSFRp/RVE3bFOMIIQrF/fwQmc6uaKt6hKBU/n0mkMQdO5 eXKJzjxCxBnZmyi+l0aXP+k8GB7I1s6Y++7i3yTDkte+VjhHe4iM2p7NC4QBhU02Y1L1 Md7spNJwI7nMzyk27WpHFl4XhMNBxEYNK4G/BXT1Wn06jmJIDFyZJlvPwUM8+zS0ANKM LFAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656571; x=1695261371; 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=tHJ1oIsuZhhB0EvSSZcC1bEv2oeM3xd3VRRUpH1LT1s=; b=MxX6oGM1oQsnSg+aWXZVE/RMHtPLJd2lKQfZrVxYeiFl/VyBvA9JAexVmvLOAELDoH vdZyHWlHQDtAPF0SkcGdhI+iynMC9W36uDqRsl59DgRUGzORBo/KbiZkVfBJLm9/v852 0tkaYzKFHQQPuxXrcUsZvE0ds2XDgyJoyqBZ3y8Yeo3VENEvk1laG7DK3sb4cp5qqv0v nKi0kzBbRvmkssynFyIMBzINhMVYc15c5Mz8H43cqlLgnBIB8AUBI014K7f+gobaM+pF Uy56A70JBVgtxx+od3gUOaYP74AslxVGzEKAaa3PhDha6LPinxZP+4FwSEe18DvM3tzB KFMg== X-Gm-Message-State: AOJu0YyKR0Z6Cr9P4IOrAvuAqaJm3PF3LrgQ8IH5EBh8WcKxbYqQ+u2T sb9Js+1bchk5iqOZ6n9aa1/H0P4f+7M= X-Google-Smtp-Source: AGHT+IFyAB8K8OP9oG3Uk6mt7LdYos/kG/8NwWox73fG712J3+qVdxKDIkOEp8/f3m964K84JR/iRTKItFY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:aa83:0:b0:d77:984e:c770 with SMTP id t3-20020a25aa83000000b00d77984ec770mr96102ybi.5.1694656571602; Wed, 13 Sep 2023 18:56:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:16 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-19-seanjc@google.com> Subject: [RFC PATCH v12 18/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 68179A0003 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: djef865nmrpfg3edahbyb3wpuoc65h9b X-HE-Tag: 1694656572-653034 X-HE-Meta: U2FsdGVkX1/x4cifERsSC8TL5do4O3JYF1+IufkGkEO48SzZGgtCbaB67zNYeIQO9OK9JmLyZLCLsaQJOF1hn9Q/sRl7HOmv8gV98sIlPW8G7b8LqWHEukvlzNCJBSeRSlJW0kjWf+syNd+SxQ4VIthNuz2hXcjwGbyxOhMlDv8CpUdVf0FMy1v7KULE3TR0BWEV1ptUWeT6pYQVFYa2NdhVIDlI4xObXtHxVoI+Un3M5AiqR0W7t2ly/F1G5FGblS0fTGP0inOQ8xTmcs5GAykhky/P7ruzdIq92eBkUl6vKdR3F83qDV/m7PcdVX8Mtczx5JNi8cEX+2vtcZxgPY+2Ulybsw5h/Q8Lrxtl9FZaipuOu4oxgc+gGdFbaKbJcPzfeQ4dqKZ0ZqzpL13Wj+vUrch30epREvIKNsqOlDgFMvwQff4H07tlvnxz/690ieN8WznIVLHUHxeWFsysAoWlYU4+DBKFeSk9obGlV4WPJvGB6daRjSPeO6WjIBS7j6/MB0IKSNZMnwlH7qjJivMUmB66jjNN/XNjtFXHuD+1qMse7vIlPdJueBvzA5AwB4wTupD4k+P5PWKC1/2lBJxFvnob6JAX1ixXoSG6Pw9fP/AHQmExsleMz79BcFRt99Lwjtnh/+Gr85Ksv9qyZB0tqkPnhUPI3n9Z7G2Ke8gZ3iJ5hSJME5vmP98QERo1VeOHf+KIHMDOqUoC+NreVkT2YV8rmpYg5s4MJlyWu9192QihEXd3B8nDqYdY4RsTt8R+JGv48K2rTMphhkcEFc7e3yX3sRtq5MxrgfrxvjXg+ly5er/0oTeuhSvTK/qJgCJ8g9s/vFye5itM2e1kgp/ngiV9p8uQhB7bUa87aXMo7qlbY0HLSCBF5d/mB/S2tCACld0O0NyUAxWiwOoEG4CAtHr5mSvlx8OR+JHARjoSD7Ti1mLyn2/atMNW8N0Ls/oL1LT923qdyEUvg3p vW2Y7xEX OgfG7gPnysuCkVemTEfnYq9+HxJke9OWBZ1TbOnurTeJUQ5F2cDGNG6bWr0dHYkJmpQx7CPujlSaEcggDgoDKUgf3NAbVgsFK9BT7c8GusbZ843TGUiYrplnB6XiPmlxTZTDuGwr46viLYM6TEa30Twiz9DocsAVzCR7LPpwdokec3mhaxoo2yHp9EotiqJrcYTUGXvA+bK/DhuXLQI0Nl+FniChLgyvvLqE5FhviPPNB7CF7GMN32/flNPS4hd5aFlGmDyRMbcCrIw4wis9OXzvJA6rUvIZ0LPBaY6G0XzWerVq4bPpuJ3mkoOdK79jBK70any1nhPQE79onNXsOmDvvTbUJ/DT1ieJuZhhhi+gOd51Wt3G9x98X2fFmSax4DW3kbpapF/X/5x3E6WJgiPPkJVwj9mcZoePFliFricvZu6VjBpPKg/+YNiZlNyqiT51Q4b/Zuq6BBv+Nfnebm5fKi82pfvBZLAHTnEcbopHBncI9vgNOmIaYN4Se1AjcD3JiVucKJ8SRvxw7wtt0dE77ZEjpkiCQ4JjwnYUyDrLAgDO2w9aHwDNULdfbPYNpk7EAk3HHSkVSkh3T+c91RAVYQQfMJJEMn7uXEj4HvfsAeawAxWmaRl2jS7Ae0Gmj7qQkEmzB41cwWx2EDTfM5zFsKNDoZMXQVkiMSYaoy6DoFv8qgr39eLx+87WaVaY41qtZblVc2qv206xCmfc7plAZay+GstF6yy0fagxylQvNorrGspShZUcjbVaM+lSuojcc8KaUEoN4KaL1PE1SO9NJhlRBIBP2G6OJGOBeqxz7Ul4= 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: From: Chao Peng A KVM_MEM_PRIVATE memslot can include both fd-based private memory and hva-based shared memory. Architecture code (like TDX code) can tell whether the on-going fault is private or not. This patch adds a 'is_private' field to kvm_page_fault to indicate this and architecture code is expected to set it. To handle page fault for such memslot, the handling logic is different depending on whether the fault is private or shared. KVM checks if 'is_private' matches the host's view of the page (maintained in mem_attr_array). - For a successful match, private pfn is obtained with restrictedmem_get_page() and shared pfn is obtained with existing get_user_pages(). - For a failed match, KVM causes a KVM_EXIT_MEMORY_FAULT exit to userspace. Userspace then can convert memory between private/shared in host's view and retry the fault. 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 --- arch/x86/kvm/mmu/mmu.c | 94 +++++++++++++++++++++++++++++++-- arch/x86/kvm/mmu/mmu_internal.h | 1 + 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index a079f36a8bf5..9b48d8d0300b 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, @@ -7184,6 +7255,19 @@ 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) +{ + /* + * KVM x86 currently only supports KVM_MEMORY_ATTRIBUTE_PRIVATE, skip + * the slot if the slot will never consume the PRIVATE attribute. + */ + if (!kvm_slot_can_be_private(range->slot)) + return false; + + return kvm_mmu_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 b102014e2c60..4efbf43b4b18 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -202,6 +202,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; /* From patchwork Thu Sep 14 01:55: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: 13384026 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 44A55EE0216 for ; Thu, 14 Sep 2023 01:56:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5FA496B02B4; Wed, 13 Sep 2023 21:56:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A7696B02B5; Wed, 13 Sep 2023 21:56:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4272A6B02B6; Wed, 13 Sep 2023 21:56:16 -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 2C72E6B02B4 for ; Wed, 13 Sep 2023 21:56:16 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0B757B4293 for ; Thu, 14 Sep 2023 01:56:16 +0000 (UTC) X-FDA: 81233537952.01.BE087F5 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf25.hostedemail.com (Postfix) with ESMTP id 3F331A001E for ; Thu, 14 Sep 2023 01:56:14 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=CBCXzbnE; spf=pass (imf25.hostedemail.com: domain of 3PWgCZQYKCEY0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3PWgCZQYKCEY0mivrkowwotm.kwutqv25-uus3iks.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=1694656574; a=rsa-sha256; cv=none; b=SXm8iosmvShAxwuc7x/AGyy0NLq4dj9W3j+G9fDmE6RKjzy4YME2LM5dfX0ErIUfU3PcCn fEHb1fIEa1QNE413mvbhjKg03GNLwcbkKcZLwampEJadDnLAMsgpDu7NfjMgD5X5sfNmPO D6TY2Fz9KPCh4voJ5fxS5/hvRgFemhU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=CBCXzbnE; spf=pass (imf25.hostedemail.com: domain of 3PWgCZQYKCEY0mivrkowwotm.kwutqv25-uus3iks.wzo@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3PWgCZQYKCEY0mivrkowwotm.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=1694656574; 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=TEdWNzKShragyniQ9J+Ve2p49++UXy1fA45JB0RVd2M=; b=uKSmNjQ88nkyG1bPf8sZh4v763ZoXWm0UA0WTHN9GesLSxz35s69Ziud6jnO65J3IkULcr bHDUy3uPaXKSl5eEkhjS7us6NW/+eFXMj1EBNq+2MYe2OH2FEQJIPJX2KeemQ+oiVX2lRi j52Ozt66eRbdZQzbVy0KarRvB1cAWwg= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-58fb8933e18so6410687b3.3 for ; Wed, 13 Sep 2023 18:56:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656573; x=1695261373; 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=TEdWNzKShragyniQ9J+Ve2p49++UXy1fA45JB0RVd2M=; b=CBCXzbnEiqVOkx0xvSWNruAE/w1qPrcS/OiRFLCkUo2kS06xrRPuHlCo0VAMIiT+A4 rZxqILwMiNIW/aQC18QcMiJDIStKnElryesd/90vL4KlieRlfvNTnc3OxF+uTcF60Grx XLr0de9TiWO/BkMViJjFPQo41H0/WCYeOY2/9yA3kfdsHEvJ+t+pzxYxqgq7hCwfgvKW uEIFK8S9+d8yrvo6R3oeFeE2XPj86T4xMJc8Ods0rzYlyVqt8nf745vD33/sJgmQxa8D Tvx+4miZpXy7xljzABd/aE8bCn5vTJ6A6T452d0a4YbrtUNWlEN3N1gZtEWfnxspdcdv MLFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656573; x=1695261373; 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=TEdWNzKShragyniQ9J+Ve2p49++UXy1fA45JB0RVd2M=; b=RpK0Ce2OiWD47wWzpATwfKztevoyBvtfG64ZVsT9qhI5gtFMG1AwOEcmD1341M8qho VasVmvJdNvwSqFRLHjXfODxoCHptNqsX5XuHxIA5E0u0oUO6VLqDnk4XbYRmSFwmeNXs 82eZ73MCuqzjH5VhTDrdTmntg1HIWLk4aJyIHs9lrVJ3SeVTn4NPjtRLM7wCidKxdQIl rnAXq4d77OSDjiO4J2nK7WYNxwbLGgdbMg4KMoRhEqvdaP1atqjZCT/1oiI4Wp0boYxR nP4mIVO+ZwGhRqs4Sy47b0YKNg98tOH4OuQ+XvXf4cCHwCboo7aYXgtd+Ojz60EryCwy 9Qjw== X-Gm-Message-State: AOJu0YyTFoL/yQh2qkGG7EG9ijrOLnUIXazcu0EXAf58JvJ5R+vLxL+k tO8Tzcc+hp30ayq0gZk94l370cSzNZU= X-Google-Smtp-Source: AGHT+IGYm//r9vLVsRhCBexYLsW+kyUM6dthNH5+IPZo7SQZB/dk4at291uiQmKPgon5iad9USRf5MgW9tA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:400c:0:b0:589:a855:7af with SMTP id l12-20020a81400c000000b00589a85507afmr107415ywn.7.1694656573483; Wed, 13 Sep 2023 18:56:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:17 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-20-seanjc@google.com> Subject: [RFC PATCH v12 19/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 3F331A001E X-Stat-Signature: ahk5gd8cjqx6ywjje8w4jm44sr3bg4hx X-Rspam-User: X-HE-Tag: 1694656574-443864 X-HE-Meta: U2FsdGVkX1/OSu0bS5bHnsqIcEIOoKlaSuXJTSFfK7PgNPtMhQCVpuZ1sYdCSnYk89b/HNzr3bbXdKMNI6IDW05pDZcJWcw0OZyxV7icH1+1jgkexCiVI07dzps3f5WZTQh6bp/mfWP2/VbHCXT+FO6xv2ohCYzof39+A6H1V6PspvOMcAzmm8GwsjYdD/F+/NsAy1ulQm5xdi3phFF8VGU5lZ3A28FrKDWF5SU0znBvj9Uk/0z7hShh4FIugIa/1Rm/+P7VS9PxoqjRPWSUt5ITcRCsNQSzQ5NUE4w2+iNQzy7dXfiXYWDg13yMmgaiscN7H7MtF7tlMFS+POyOQry/m9tGUxNfbpMopB5CffMi0wN132en3Hi7ydheGjx7WDIS4NA3Q5NEEypzXTPhvMuoWfvV3IUCBXeNpBAFViFLl6lgihRbw84VyjleO5a4FaM9RRPLO7P0s+FGVCV8AjiL5JA+gcXz0M7GzIhRNVeopZYLzC2Izz+DIGjKvJE/B61CLfFDTmG/gVL2JBroPWvIbGSIPWSm2/mVYJ+hzweKChQZfSOuWG3mQUZo4icW+u0Y55P4MK0nGU+flsTO6PVwM/NgcVAQWpcwwchpw9Bc0NeRiLhDWUfB4SvWYK+o6BBP9yvHvsMRNaYTNVmnJqRWo2hwPgMAbLiDsPdNUVL0WHbfdFix79Ev9XBfbglIMYwYa5B8sEiKCo+El7hqsHsHCnZnNmuZZrzB0os+ZSgevvKsfKFkjJcExssS9Y0suHJRKBPpXyB5WsfEBRfOMc0fOzBamwDkQbs5UXIJSwpa3CdiGEJBctNu1hBbWJ7EeIa/1KWWh8Lk9HdlkwQEyaWMduIIWIAl5Ve/zXiLaT+aIwQF4SaQM956IajID7Lr5RQM90SNm8qzaF223lTGLukYcD/VG8M44ayiFNevgYdkZJsXBCbhp1vU8qVYDzjeeaz6vcfuvLt8xsnZQTR fU/2dRX9 dFu2/Va9oJsGyKPbNq2Z2RyGKIBsDJ01lxpQg1sGVpgVweF4oorZGy7IWKglm1xmvQsoKPSiA1I8F+8obsA4R7ntJ3e1719w7f6qqadiyiQC+Qt5IrYd3nGwuJJCrvG57O+frMLKuFA4VmDmbbLRY4SBYTDe/g6wi6wrPX3SZHuRv13mPWFMlKeZGVB2EGcm8rCN+GIVw06Kyb1ns4UhcDxblM/uzbB9DN9mql8ZMiF+K92XWJFc6kWrKo97lEUx9gGOiYjcHsD4qLuZXm2mi71PUT1V8dgBn6+ci1AJgpRZIYUmXeK0Uhl1gyueyLpra4L+ECYGIHYt6Wf0sioZ9c1xm+PQvPg/bR70YIsoAX02DjNAcKsQ370LQk0XOXBDP4FdhVeVqMD3XisPTuTW0A0ZkvJ6iWQeDPbNX7YwiH9q2brlTA4AP/jSuF6iqKcU6XsNqK3FbceXAOLugaduzo5eXUC+cIpARFDavlaNGdEwAt6c9pzyw3n4KOoseVmIrGGGekAc9SECvTfcHd1uHbTts5B806kx7MjQG9lWe/1ERmWtnYHlhkUBnN3J89aUgTRczMHEChaUVRmrTxAiVAll4HtGNFSXIY83s5ObcLJ1eMX6uDIrtELIs8/hOPYfW9HGggKMinHXkocSoMnd+7xG42Vqq2YfLkOPrXp2vw73fgrT+DYnUq+7l/QoxGTSchXStVL2uiNbb+aJLeveSlTpoyKmA4n4SK/RYUzfF3OtzP1dCCJtwJAgwAx54Tb6e36lUr6zjFU0kFG4cbuoE6HdsGnj6HrPMn04LOJJynlS0hMyivq/j4Q0SgBmO4CgzBzmyiyvxHtIQDpcCPt7wM8J3vQ== 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: 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 --- 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 91a28ddf7cfd..78d641056ec5 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2126,7 +2126,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 18d8f02a99a3..aea1b4306129 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 Thu Sep 14 01:55:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384027 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 361C7EE0203 for ; Thu, 14 Sep 2023 01:56:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15DA98D0002; Wed, 13 Sep 2023 21:56:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1118E8D0001; Wed, 13 Sep 2023 21:56:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA4BA8D0002; Wed, 13 Sep 2023 21:56:18 -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 CB8368D0001 for ; Wed, 13 Sep 2023 21:56:18 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A93471CABF7 for ; Thu, 14 Sep 2023 01:56:18 +0000 (UTC) X-FDA: 81233538036.13.E3E3C56 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf30.hostedemail.com (Postfix) with ESMTP id DE81080005 for ; Thu, 14 Sep 2023 01:56:16 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FeCKlhlG; spf=pass (imf30.hostedemail.com: domain of 3P2gCZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3P2gCZQYKCEg2okxtmqyyqvo.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=1694656577; 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=kuaopqTQwIJoODQjwL8UCc8nafSHAqskK1PNRD1zt6Q=; b=cfLGYnbbOCIbFo39kWrtqUsrM+kBCXoJemD8dwt5lmPwMvRLa76Ahztiejlb2ixmlgfsGQ jT2ZGVqQV68VJu8lQeFHv4F0/sXfxqamfpYEQA+oj2udQEia2SiP4WTu+HIZY2Dk5ktM2q 1FmmJnNHMVsZGMFqEo1YSAkSlYrgrOA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656577; a=rsa-sha256; cv=none; b=SM3sBj40fMLMW0eLLGZx/YL/dMMrp+yNu7nicXXOpNN9K+dLiYBL9cLgpbmb8J8rAtR4Cy tvzwCX4W/I42EbhlLFx67RqYq6fHY7FYdUkUdS+YmZLXC9veaCPSmrETqktd9VinlYDRP/ dkqUg4hpvh0j2oaJGpT9RdGth44zFWs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FeCKlhlG; spf=pass (imf30.hostedemail.com: domain of 3P2gCZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3P2gCZQYKCEg2okxtmqyyqvo.mywvsx47-wwu5kmu.y1q@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-55c7bb27977so360295a12.0 for ; Wed, 13 Sep 2023 18:56:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656576; x=1695261376; 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=kuaopqTQwIJoODQjwL8UCc8nafSHAqskK1PNRD1zt6Q=; b=FeCKlhlGdsa+WHvpisI34IcMnNHfLIPPB1c9k8sxhKL8/r0YmVQQOr7Lh2xytp3Uri f/kzg0MclVi5uR30VkJFBbpCLzLLLMcC/aGXRtA+1nwTWaowBT4YN3QGm/BnAqvNNJ3W bT5XxE3tDZjdxcwL8ZkUagmXdhfmXUNA+UHOx282hYoGDq/q5nBf2MJDM7L8uFAPH+hV jBisMH24/q/9+x1/tOj2LVLSkHaFdaGCk2iDpPN6+JoWiZGzMq19UJhEtVgObyQsuP8R 5tPRWjG7R43Phn1zGELCQJVvHewaga3b5dl2MZjtC/wrTH8aziylACFwby4yUp4XrWHX gO7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656576; x=1695261376; 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=kuaopqTQwIJoODQjwL8UCc8nafSHAqskK1PNRD1zt6Q=; b=dD5sq/OhDIGnwzy33F8ioSGD4/vSrVAa86UdJ6YGzi3WiAk9F5XStirHGKiSvATK8L MyhZUbRTr7f+0u6GIR90Pb6CE/1MVLPlUS2kKgQom41yLjD9zeUQo+gTUBgGAdrjSMRz oEjBsJi0iqy9ZrJPZJIa+oRg9TdwIQvu1amVthGFt9iJUEyK3Ns5oeWRkcXS2BDe2Gwd 4rIEr3t6VyIFtvhCHU7rEtvej8LYXufweV8ynQb/mFC+LiHP4znv2HNhOLHQTQwqK3+/ b6IQFRJ78IflyegDVjl9drL6V1mWwLXxwUUHlFMbvhxiFNTEw5AaqhMgoNX9Cz9FR4cl kSrA== X-Gm-Message-State: AOJu0YwdwmiN5FW6u8Cm+ajC4ihhci9ICcMaf1iOdwgwHmCTm6PBcZSw UR1uj8BHLpBxPi5L1ixbhFqkjI9Dbh4= X-Google-Smtp-Source: AGHT+IFIYZjFKMw6WHtNJEOGL2Eon5EsR/54GlB+Thrn8G9XTPh/MDdknE8YBd4objuzaOAQYplftVof7WQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ec83:b0:1c3:77cd:6520 with SMTP id x3-20020a170902ec8300b001c377cd6520mr179995plg.11.1694656575520; Wed, 13 Sep 2023 18:56:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:18 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-21-seanjc@google.com> Subject: [RFC PATCH v12 20/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: DE81080005 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: zzjr9i9axyy659injcnsu57tjw44783o X-HE-Tag: 1694656576-982781 X-HE-Meta: U2FsdGVkX190+50P5BuS+LCLBCIYGpL2qSfH3g0sCornqkKGoGgCU+0Mnl1ymhZ+I/wB0R+d3w9TBUaBqWFXzznaPe4TIGYxluA+jscVXt07cm86GbnfbflEpWFIPKSjb8hqGF6N79ST0hFZaylfwXO9tkJW2AtnlWYJkHRTBDZwRy0F9+AjZKw5qoI9cLatToI5eOc4fMA42IhLHD5aLIWja1hB5wzslJz45VM0K/qj7xnfcFuAsyz68twh09cA+vXYXe2nlZ6k8pdCSloHImpSWn9J3Tsxy0/CrygLlAaEdjc+tHll+kEFF5r6s2MBYMBayyBdD28+WHxhfjN5K739JyxK74m0wTsovRNOpw+qyc1Lfr24PdmU+3FqjgVKVotKPOn5f+tMCnJFerCTksR15/U80AH2FQlV7lR0jhkp75xgAjjPhvxKUDbzPzVe74QHtUHh+PwTv8hQv/Lw6rqC3MPixso2YACCquJbY9jxest5aOqDUl2/EgsgZ/V8p+xYHQYDm35RaPkDQwbX0bFKY2GvVN+ukAQjgmFQA/LmVAwlRtaYQeemD5WkFKPIajQ4vucMGX4CQl4FOo3x53sLSCN1v+8uEHWtGZqcVfUjor4jILU2NpSAB6dEkfLXlO9UrQJDuRV7X8NgbIYagGAXKWyPryrqL99vyJZenEc/jRFVAkFDjsy1lpKO0aiDu1lTQKODVolpPicDr/lqnc55SO6uc4iYbl3Cb4qhk0vb69+tvHijOfDYkC/NI1slkY4TcK02DlZTHnTfx2HAtgQ6BCsxZP+fz3tgQdS5CUNakwD40OUVKJzQUIHXl0RIkn4XoHOlgemuNZlciq/7had2w8PC85bK1HLlBRlGCMIGxeO9fUZd6ONiL0PFBTKe3e3yDdk81gyY7Ohk9yJ/NXZwHqKM768D/hAEWmWdS8ZkGl0ChQIdhPe6qIxKjztMEq1MCssWLcRp7Ysew9o fojYVHHe ezE2CeB9oEq3VuCGgLoSgmoGMfD9cN11dmyUonqpJD1cixQEiSDLzyz1o1TGydFfgjRlGjHmKMfL8mOQeDoJTODsXxoQFz+V0nHCxKoUpeEdG4vromlxT29MtyTl0VpevO4bW8hz5BjN1LO1eDOFQjZfA3F4HdE7HtVqBErIuj595f/oxNvl4cioHh55rTFFgNjzkcyMYFYeUcNyeEiuBJ4lorclohvINzCcZzvHW6hNbJKGl2pnceiNeSmQbPbBd4tpvhRWyaLTHHI7xj2NrUk0j+yLXpAH9ey8FO/SerNr78JbBK6J/kQVAEwUHXSNyFM0V1sVbuqnm074OWeyTY8eshGD4nSykATfociXUm3vwplmlsZzK9+qYhe1KQNMGL27XCt3D9AX6QTlhrze13HdD4ES0df8OvKyKl6bs5ciXx1TVAj4rQT64uPNl0xsoMjUAoqKWr55kh82Zedq/uUFyOYeXaxoCg6LFngjOfjjzhMFqIQmIYjC7DI2ZiaNAMKEB9v5EpEsUvXYIGUJ5uWnuslHhZM8v4mXfg0uDNp7EFhwoWToUM6CXRpKeUQ3RmOnKI1oqbwe35vIMTRJwpZ697ZW2Fpjm/71fAEO/LxM0cEgxgXsXJJXzvHYjVNvmTtZMiibRMKv0IRlLci9w+5NHqaaPnnWMYChfaF3hFj+eyeEI8KlF+FNMFolaJyEUClzAVAGtx3WBAj8UvtrfjCgYaWoE6XXx92QZJvPUF+ooKkxl01RFCfE7//msZ+m6a/3QunM5FYHy7h8= 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: 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 --- 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 | 8 ++++---- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 17 +++++++++++------ virt/kvm/dirty_ring.c | 2 +- virt/kvm/kvm_main.c | 26 ++++++++++++++------------ 9 files changed, 41 insertions(+), 28 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 78d641056ec5..44d67a97304e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2126,9 +2126,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 9b48d8d0300b..269d4dc47c98 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) { /* @@ -6301,7 +6301,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) { @@ -6341,7 +6341,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) flush = kvm_rmap_zap_gfn_range(kvm, gfn_start, gfn_end); if (tdp_mmu_enabled) { - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) flush = kvm_tdp_mmu_zap_leafs(kvm, i, gfn_start, gfn_end, true, flush); } @@ -6802,7 +6802,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/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 6c63f2d1675f..ca7ec39f17d3 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -905,7 +905,7 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm) * is being destroyed or the userspace VMM has exited. In both cases, * KVM_RUN is unreachable, i.e. no vCPUs will ever service the request. */ - for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { for_each_tdp_mmu_root_yield_safe(kvm, root, i) tdp_mmu_zap_root(kvm, root, false); } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ac36a5b7b5a3..f1da61236670 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12447,7 +12447,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 aea1b4306129..8c5c017ab4e9 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 68a6119e09e4..a83dfef1316e 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]; @@ -1391,7 +1391,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]); } @@ -1674,7 +1674,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); @@ -2044,7 +2044,7 @@ int __kvm_set_memory_region(struct kvm *kvm, (mem->gmem_offset & (PAGE_SIZE - 1) || mem->gmem_offset + mem->memory_size < mem->gmem_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; @@ -2180,7 +2180,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); @@ -2242,7 +2242,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); @@ -2354,7 +2354,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) @@ -2494,7 +2494,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) { @@ -4833,9 +4833,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; @@ -4939,7 +4941,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 Thu Sep 14 01:55:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384028 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 2EF41EE0212 for ; Thu, 14 Sep 2023 01:56:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 751808D0003; Wed, 13 Sep 2023 21:56:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 666D48D0001; Wed, 13 Sep 2023 21:56:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B8918D0003; Wed, 13 Sep 2023 21:56:20 -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 32D4E8D0001 for ; Wed, 13 Sep 2023 21:56:20 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 14EAC1CAC01 for ; Thu, 14 Sep 2023 01:56:20 +0000 (UTC) X-FDA: 81233538120.06.DAEC3A2 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf02.hostedemail.com (Postfix) with ESMTP id 4E3CB80007 for ; Thu, 14 Sep 2023 01:56:18 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fNl3rtW3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3QWgCZQYKCEo4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3QWgCZQYKCEo4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656578; 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=Jl+0PkL6fF6lt3zrM7PMQo1yyUvP9Wp8yN6Ux9vmUNY=; b=6hcFM34JzURnsNJOipQf+dGsXK8tOhFKWQ4AThib1DDI1h5N70yOJph/iFXQs4BKTxugt6 OtpX+nI+dlg7FUeH1uglbElYPCYPLSd8RzZISZYS8I4ESE1Gq3O2DLfZMfuK3QqNy4/hlI +zILttvPWeBczm50nCp3nKEGNP8lQyo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fNl3rtW3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3QWgCZQYKCEo4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3QWgCZQYKCEo4qmzvos00sxq.o0yxuz69-yyw7mow.03s@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656578; a=rsa-sha256; cv=none; b=3cOdMK46tWOmHb44cHlNiDaVv1BgTajQkfLu2am4NZNOm7Exzppk4Za4PLR2uUwB7Ji2ic ie0WOnzQ0omr+Eje5dbUeq0tN9mAjMn5z6QrGI6U/5MILJE9UnvxUi5v0Zd5vnJetGgFtU lj1PTf6X399L7oIhtxkyQCkJGpiDzPU= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59bcabc69easo6890057b3.1 for ; Wed, 13 Sep 2023 18:56:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656577; x=1695261377; 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=Jl+0PkL6fF6lt3zrM7PMQo1yyUvP9Wp8yN6Ux9vmUNY=; b=fNl3rtW3D62J/GfyGnG2BGJvlDueFNptaOI19rUF+70S/0Rt05tsHppFm2ko2L/mEW Vtox+Ppxj3aYbFheOpNBP1i9i9sy18HyizA4zrbsmDOPfke+BVMZiLxW6KPPRfqsOrYm /byLYicmXjC6ozk1S7qQB8gpvutgBPsLtF2pjOUd889u17rXUlQePSdkG6EVbplDHoxB JkpuNN4uiDdd7Nfi8J7m3P1NwzWsd7pQqslRfafLx3stFZydluedrIcqL6bcLaLQXXyQ 9YUWRJLU8l+xWA5eP/eS6MHvA6XYIGu11uORTz/RtlSPseWvIcjkJm9GqTNxgc4/Br2k p12A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656577; x=1695261377; 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=Jl+0PkL6fF6lt3zrM7PMQo1yyUvP9Wp8yN6Ux9vmUNY=; b=t5X4IdGnNJPpLIxCbYbh1XtT/Hr+vpGa+1kLQzLD6vnTBwacOJfGiqoxzF2/+uFaRH uYs3/X3/aArE8g8N9eQsFmwxSBi4EZm5M0kFNcR7wLMJiscMg/4eBQpJRuAFCCBsnhlc Gv8+loM69+jQFuHI+/3DCq8uprt6Ze1+a5nVI/m+60eVW7vkq7nsZiMxRZRPmbYA+3xd ABjBdNlGwp5gGs6/Mt+ja3lz3AlQOtAldMRDRR4nWHQbgjlxUF6SPO8zGmWW3MH4Fyyz gq3DQDQotSG5iAx7qpYiH0HAoFl1MfnHyeu8ccI1fFZgcXoXux9Wz/mkdjuOH9Zqs3FX Sk3w== X-Gm-Message-State: AOJu0YwGDoQMYqR2l9x94iLkl0lxtGtvGcpnOdSpv5tYit2On9PYai9K x5Yw3AofHS2U+ZJXpL7f2k3RrajrndA= X-Google-Smtp-Source: AGHT+IGMW5Fpgp6gfJWk0eHuGZ3TAZOaiXnaa6CntAf7ojvb+izdwYFJ9WwJ3V4CWPMkeT0TBFGRR2456Wo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:ed5:b0:59b:eace:d46f with SMTP id cs21-20020a05690c0ed500b0059beaced46fmr23980ywb.8.1694656577511; Wed, 13 Sep 2023 18:56:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:19 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-22-seanjc@google.com> Subject: [RFC PATCH v12 21/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspam-User: X-Stat-Signature: wbyadgkm3e4mw7dd5ysoq8oznqemxzie X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4E3CB80007 X-HE-Tag: 1694656578-975859 X-HE-Meta: U2FsdGVkX1/CvwMG+GMy8VXGVPfN6ogtu3pSjHKmSwN9Avwy5ecIhN2AqPcRkRJyZVCyExFd33hHkTr0PLPZ+BIF7riOTQ3qJ0xcG3rVRE/H9oLUURpbPsscCzdc0/0KNjAIwFK3PZoi38Rr2geyywSbeRkUrM0VPpgPyE+l9/s4xWNYzO2nw/QbrhdsMX4d+X42wZI5gk2VVOqogP+sXNegwfOeD3Jy54OR6vmyfvDnBhnHyxNfYStO6pBpLH7tD75XXgCpV/NaRezODU6yGBIxqZwMEfa3Fv/gS53RG3wGBa09ozjvpVINCs7/wAaEK6bBSQQGJu1EFkABZwnf+Ub1rMJzQASACTW5vJoh2xmof5car/EZ03uKI013tCjxzYx0spk6Myt/eawaSNPjMWo8Njhg3c2tU9NtqoUwE9QQC6OzhC9CpYDMg83+Z1nOu/aOmvht/vGpWxuIqPdQnD+N5/3xrgWtYf9TBxInHWup+S19JdrwfW8qkJLICfAx3nPWCOFXBec/SXsXjo8vz653FvA/jACa/7U6kqZQYRsnkfkj0PyIjZlj3fFFglt1hRIBYWTogQP5icEpdjIiJ0zoxNRgbvxSMwKr3ty797UH3EurS8EteLGkD8SEr5MtGx4W7Ru7t88n4NtwM3eRy1Y/sFQeQhXqQi6+66o/pQTBhZfMUSpzM0TXDu+ysr7E76YAZlzc5nyGtDQW+H7vjy6DfJVQTLTtB+LPCn3RXUKLg5y3XZ/jeoFL2c9DO684VevAWQaMmPjyyvkYnSsJNLAXZT1pREg5eNf4AV2GzGOx1PDJxGb4O76E5T7dKS8zCT4RJxLGIiPdWiHa3m0jPP/vOy8FnIh1szVYKvK2P0hzip/w/yA0PgOLhEu3Lq54vVnvhlg7uCsxlGJlzJU6/u5jg3ekaRs6N3WojfhKTJG7t2slUwvynoWkxQwAXkxxcZyixRhM6DkLxk1BdUB O3FThnjc 0Kg5lCOCdMrZoSI1fIO2c9tQiX+X+1LfqbxqAtsMyqSRLw4U5KPRPU4pJAz7+MKgwIYlLVVfK4dQAoOpQnH3EzLaUERRAcVcyBWALjkHUAZDhUB02L+OyNgLhkVbcvqNzBKUjJZpZmgXPjTD6UrEdyuP/oL2nhMtVXqZkNZ0aw8cvTCH0wV9NoGqdwly6U0wrNatHSbz/R+U1mgqvSy1ExQ8O0UQZ2vAVuPgBDtBwIsuwRdrZ4mPkt1OM7oluLsrerxvMXojbaiZNFzozy4HWStnYAhpgii7hnu7jm8dvYaxl0ieTrq/a36wzhgGHcGevvysZ+kS+4xbkhTQRKPdF2oAOBDRPcR6GNRwnlv5+2c+qOWN1KSKt/ILd45AWF9Lz7pKyXHFD6iT8S6bC4U0byrkAC6L12iagX5oD5Uj8gAjza9UgIxnHrlRDvapypPih6y/kCNG1F72PbqDaisrDdR5EXPWuH77NGLvSoLaocgUlD0j7jZH2cjYb2KyxYPJHj+2jpMYAZmpp3KCsNaZpEuPe5UNhEXYJ+brJm4182OlpaewaSe9rOhgyBpQ1sO+B3bh8v7O2GGeh1OZMk1fYP2pPg/6AusP2lKbCc6ZejVwl/nsG/S0VCLG1v2YwMi4SVw8q/NFV1fYIlVeXYfJDmAqrr16/apEFoUshE4etcF9bPBFXeQR4YKYmSLST3EcgZt+CZMmb1J9ZAXqsxwCPVrSswO+pYhbwSx24KbFlnL/mEDyHAAVOHquOdoSZiR4qPv37 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: Signed-off-by: Sean Christopherson --- 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 c44ef5295a12..5e08f2a157ef 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 @@ -8558,6 +8577,19 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a This capability indicates KVM supports per-page memory attributes and ioctls KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. +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 44d67a97304e..95018cc653f5 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1245,6 +1245,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; @@ -2079,6 +2080,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; @@ -2127,14 +2134,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 4efbf43b4b18..71ba4f833dc1 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -298,6 +298,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 f1da61236670..767236b4d771 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; @@ -4631,6 +4638,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; } @@ -12302,9 +12314,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 2df18796fd8e..65fc983af840 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1233,6 +1233,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_USER_MEMORY2 230 #define KVM_CAP_MEMORY_ATTRIBUTES 231 #define KVM_CAP_GUEST_MEMFD 232 +#define KVM_CAP_VM_TYPES 233 #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 Thu Sep 14 01:55:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384029 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 48F5AEE0203 for ; Thu, 14 Sep 2023 01:56:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F8EA8D0005; Wed, 13 Sep 2023 21:56:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A68A8D0001; Wed, 13 Sep 2023 21:56:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1ABD08D0005; Wed, 13 Sep 2023 21:56:22 -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 06EA78D0001 for ; Wed, 13 Sep 2023 21:56:22 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D6A22120F28 for ; Thu, 14 Sep 2023 01:56:21 +0000 (UTC) X-FDA: 81233538162.26.2F17E13 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf19.hostedemail.com (Postfix) with ESMTP id 269D71A0008 for ; Thu, 14 Sep 2023 01:56:19 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=zlo8Z5FV; spf=pass (imf19.hostedemail.com: domain of 3Q2gCZQYKCEw6so1xqu22uzs.q20zw18B-00y9oqy.25u@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Q2gCZQYKCEw6so1xqu22uzs.q20zw18B-00y9oqy.25u@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=1694656580; 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=m3Tc/K++HKyC1JKw6ryTn8bhcHxW+fZqtY22WqzjI8E=; b=E1v7+n4U+RwIDxCJi2mKaCZZZDy3EqdOtHk53V1hGTgIWQjIIm/06Cj74VsEkducgJIOmZ PgW/e7O3xhN+CV6Mf11dG7KpxZ5IqkAeg3u/nBuM3DZ4ZGGGOEm+lfP7vT3bOnziNs0EYs 9pqacp00+FklUL1WxdiuATvUxwx434Q= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656580; a=rsa-sha256; cv=none; b=8VFpUzFWyrCXaZNybWfFt4FkOaRG8b8Q4tQzkwE4jqaUS3ZHFt6PlakMIhozG0kyShUNd3 jn9Bw/c0CcLw44zQNt0BRNp775S5S+hTaMkBmEdJKhl466ckwlsWx8Cm7g20IxenOyCxU+ x1UyQCLNfaE8eu5BRGFDJ+s1ZnBJCeA= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=zlo8Z5FV; spf=pass (imf19.hostedemail.com: domain of 3Q2gCZQYKCEw6so1xqu22uzs.q20zw18B-00y9oqy.25u@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Q2gCZQYKCEw6so1xqu22uzs.q20zw18B-00y9oqy.25u@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-59b6a51f360so6489847b3.1 for ; Wed, 13 Sep 2023 18:56:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656579; x=1695261379; 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=m3Tc/K++HKyC1JKw6ryTn8bhcHxW+fZqtY22WqzjI8E=; b=zlo8Z5FV8fMMyklMB8EJbgG8ul4qRTs42F21ZfMI8Hqjd1o4605znGzAbNxek0Getn iWdPH91zlTTnKi3nsUXKZ8/0qerTN/7YcQIEBTxLUQoK7Ldw2/NjTLAvOg4P/dxhDo7g q+7HBaN2VCwjudvYF4+yf5twDabK7lq7lufpHV4c1xAKkn1zebuPNeJoC/O8uQWcupjJ Y+F2hiBpsPuQA6lz2WvNTxSdD76HSbY73GvOQWZBtRzb+hyBao9m14BmCB4WosEBlW0U g1MNUGewKuZsF5X69fN5BZMlXUdIRUQ++Td0UfiJnG1a6UMuR4I3jr7H4FD/jICV2r7e CS5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656579; x=1695261379; 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=m3Tc/K++HKyC1JKw6ryTn8bhcHxW+fZqtY22WqzjI8E=; b=SKyFFf6wk0QxInwhPnMo/Q0yegAnp2ZVARM2WOUOPsM3h1W6qth7ml/RI0TRTrLple 3Puj8HkI8f2bysQSUr0mBjsin7tZnFZv45xAt80YcX8i0n5M+K8TkKDlRieLhmtXPrnk w2nt1VzQd6X32EEEsXgHJuCN1NsUhQHZH+It2/Xt7JHSRrd6No1fAf26UuZ1W3vluxy2 I2E8pM5qIFk7P9lHRS2pNFlkUIxg23HhfWX6Zph7w/eXCqjwUpzH1Tb8oAe6ak5bc6x8 srTj8o0CEB+ft/jacA+aQS7XWWuInGPT7EiplrqUtSNtfDo4sUmbVpt8viZt7s2qvbg7 LzLQ== X-Gm-Message-State: AOJu0YxlNbhPEqWWIjPJaKnnoXQ4DEIL+QGcr7fkgVsxDdm//PXQLKos cdVJt3drndmVJEBriOWUllC3iMitbS8= X-Google-Smtp-Source: AGHT+IFDxQWOII5kHYcZMuAOGuYKUEYlYg+M0lPpcGlC7jaodEdrBYecqjufioLD9d6AjHIxw5JvKtydhZY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:ae59:0:b0:59b:e669:c940 with SMTP id g25-20020a81ae59000000b0059be669c940mr41958ywk.5.1694656579341; Wed, 13 Sep 2023 18:56:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:20 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-23-seanjc@google.com> Subject: [RFC PATCH v12 22/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 269D71A0008 X-Rspam-User: X-Stat-Signature: 39yrp4jpxknzux7hxpz7e1wksbqn9dwk X-Rspamd-Server: rspam03 X-HE-Tag: 1694656579-153162 X-HE-Meta: U2FsdGVkX1/yA4IJibwWxgujahwS/aJ8XUeILmnKYiABPXK1O9EFr9cp82a0L6pwb3abDRwJOXPSGmjg5Jbvz13i9zCy3UgMkmNkaJFr0MTlocx9Nvg4dzkmP5BiApvvCMO9d6GNMJVP4NaO4ht0pJeHs5rQ8358/2nk3Vd469ERA5vZMGpDFbtn09+623fzGRc/7PaQvqo2HxGn83agT0FEdIYSPr74bKGUrG2jtPN5shU7/quppb2MM5kpunewaikjlhkzSYiVvVe0enLMI4y/06MW2zNQpnnyjx0ik3LyX5xMbr+dh3DfMpmImUba29fY7IjU/4/iAATqVqGgTq935kcSMgGxaG46slglZAegUOP0Dt3IKVesE7x3jA2rGvvvTNnDOYRc8w2/SaVBOfM6Jax+jWgKKAQWEivFCc/IP6t6ESn/1JsO3rkfMnEMPSbkv9Ked5OQCDV1TfXJ+4tw40j3hDHNpJnpQkBRYatK8FQD4LYvABQTnWNfq2mraAYm5olL8RbqpnSJFmTtXHp+AKGq4G1/okl4Jxg+T7rLMA+ynDGA3bZ+coALFUiQE0xKNXjL4MwlmhqNLDntS5Llzx7bKJqb9ecsM2j3LLnCJHjA/sNNnltNZnTmZxTSVu+eZhlL24JK+f1Vv7CcPw1LgTA8FSx0DIY3r/6kuoL66Gr9lAg9ZjUH4wTmJTm+VkrgmyR2XY8Dl0PAvRkhmgDWOVJxcTDiV2Z+Q9GyDTGiK6VE1uiXxY1JMVxN98qhBqaFT6QJAAmoaXHinU6t/4EwoOqW90HZ8dVAVOlewElDpdHAGpMAxd4OJNNZZQlm5+SrhykVH+EAoVAkcBlCP+BhIEEOFoIE0ZAgaN9BU34YIGXk8j3m7OIpnmNRtOFfrlucLVim6fTZ6D+9NbTgrHV8IryJGeZ9Ust/EZudx1CJ7Z/kXQCUiWCBJKdYYdmPDEOSyQ1/EeF9HZgsRQG Xx76WjJd 1Xhog8Ea84o46rDd6untKdcmb7gxVoeh+KGDSqX1qRdyCvYlW6dSY6+JKd99SqNbVT8yVnRTyVL6aWE2DV5AHWWWoZeK3D3yY8jk6H8BUxuQqYeXNIMzseoHpJT7tp4rqiPpHCQgNuM3FV7qD25HnhSYJnkduj5AU7iTBIS6DuApGLjHzJpjZC3818K13uAI9ggguBxXQuWvDHZBcFj8Fm0Kmtz8OOxn4Fc6yrwJpWC9o4EqCrMi4StYbQsX9xkBtM5Iceu2VckOCX4WMfWYpAz4Dh5yXGqiG15vuhebrVIp8HXqXbFTJj373kawlHZ7NfRibTjq8dRqFt3UGt9jgM0YctTk8TBEKd+RW7XWp+jzEMtm8pZXe0FNuLk/I+1SqJ3NY6FsAbr6ZrkHgzhjs7AnQwT4Hu337I5J7njcMhp7SlMET2eFPLKMB66H20jP2Zt2GUeIFwK7iTNVlfpysfnV+ClVGaYb4qEdWdhSBMXxmT5+7vxCWOFaWgN+AjhG/DaQ4kmfCXthSTctGiQ/HIyyYlCpkkXx2RUJjj4yXDWcmagcwPPNu17kh4LlRDNubKVBn05s6xM7fgWncP/MtVKd6nAlt2h1MrxhBjq8QnneSzXanp0xaqdfyNgdDjxtjbDICdUveajg+bAeQad4xK6N2PsfpkwJDhhEikPmlXaADVyb//KuD08sge/uS7CDctAluEUzIRVoOAnujOCdgq8GtPaJLQ2dgbmguCJeH5dttti6JISi9O7NMP4nNzk+Wrpj/mKjpCQTB/P5eVRjfAo4EpQfzktI+pMKTAJPCi+0HyBggDlU/I1b5ED0LyRSZ9KPR 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: 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 Thu Sep 14 01:55:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384030 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 4338BEE0212 for ; Thu, 14 Sep 2023 01:56:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CA378D0006; Wed, 13 Sep 2023 21:56:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7769D8D0001; Wed, 13 Sep 2023 21:56:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A8718D0006; Wed, 13 Sep 2023 21:56:24 -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 474FD8D0001 for ; Wed, 13 Sep 2023 21:56:24 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 25A55B429E for ; Thu, 14 Sep 2023 01:56:24 +0000 (UTC) X-FDA: 81233538288.12.C59D261 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf18.hostedemail.com (Postfix) with ESMTP id 50E561C0015 for ; Thu, 14 Sep 2023 01:56:22 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="SD/+qdO5"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 3RWgCZQYKCE48uq3zsw44w1u.s421y3AD-220Bqs0.47w@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3RWgCZQYKCE48uq3zsw44w1u.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=1694656582; 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=f+Cn7aLXsoRGXB5YrbWUVJE/B3mEfo3u49dlyQ3c6p4=; b=Y/I/mmZHqkv3MhalJtvbuToWvM+9iQX327WAKpLcuC4UQKlxdhPdSFBbpP1dF2B+xufzrd +/w1+NkXy4LROZLzdWoEsccuZIJAYz8KmiYDo1QKf0+jOawMNgFhSa/poW418AjxgsfdNr 2Ck5wVKMIty9WK7KK4UZxg2znCRnimM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="SD/+qdO5"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf18.hostedemail.com: domain of 3RWgCZQYKCE48uq3zsw44w1u.s421y3AD-220Bqs0.47w@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3RWgCZQYKCE48uq3zsw44w1u.s421y3AD-220Bqs0.47w@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656582; a=rsa-sha256; cv=none; b=JhTAiqM6nW49wox/1kp4JjkI/RL+06n4tuuf2q8bB4WFb7k+HIpVCGyaWg+ibZFQX1t3Gm fCFZafo+cKafBZ9XlkcsFOtX1zGLiqrTfFl1+GzqQGp0rCm3iRWWPvxWqIVFjpQkCCleF6 EGfD5rKE+3q2cpUcSCu9QuS78D/lVok= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59b56dab74bso6575637b3.2 for ; Wed, 13 Sep 2023 18:56:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656581; x=1695261381; 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=f+Cn7aLXsoRGXB5YrbWUVJE/B3mEfo3u49dlyQ3c6p4=; b=SD/+qdO5NLWcVazui8ugoI5oCj5IBSEat/oYRXZhr19bZrOVkuGyNhf7G8x9faDoRh lMdd646k3VYUvYSdU0DpC+05Vn/00cEmSU3hOrjEMMLWNG91nxZiA99pzNGQ5cHfk0C8 B06EkTFvufBzyTQeTaJxs/mYaxuaKyFScjgLhMuZIWcrS3QqVe3lK2P68Xu8N9jz+r6o t3Z6kkvD9jHN5VQfhuJfeY5dNJbTBGxvSzcevXIuNO8XiztmxcClYfYDW5pG2+pr2pXX X7yuNzljaNDifBFE9H+h7935xlj4LSC7LTT0WvPl8sA/IvxPo9xNF5wNPQxaZCbmybrd roZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656581; x=1695261381; 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=f+Cn7aLXsoRGXB5YrbWUVJE/B3mEfo3u49dlyQ3c6p4=; b=CfPWVz/EeQi71i1MsBUsJFv2RFzfy4seBvzp5zmuLzBSZu9VeuTNzYjZOn3gho0FEV r2nKG106bweyIV1z9TtgDV9d1wclpAr4gvNYC+Yb26qaw16RIwkg1UCPHvi0THbWe1Md w1zM2Hk7e38YXZ15f7Hb+C2exDNv7/HD5kQ3buM1e1cdg93E+OTZRhLt7+K9PbGktmsi gY98JC3oPp1g+RGZZCw4c8hvpUBN04dHh0CM2GxlY072Qw3j1XtAz7NBhOxXu0+sqVJv GCRwm7vhJnQ9Hy/pl3ZcDkGx/mq3RZNVDhw4bVX2ANDYZz4aTezrCgBpJIG6s1vigr75 +bBg== X-Gm-Message-State: AOJu0YzTgDmOYs0KsycxWTdgDbHN+KCDfiyRUCSzJvPO3SJ0OjlLgXpD hE3ax0svbihZh+tj9yYM1USg7LXdTdk= X-Google-Smtp-Source: AGHT+IGqSdh70dZcwo/Z1+tDjuCpmZBS7POD57w/4oolbNcAVORh1dC9oB0tdW5CJRUNXoVwQDptkL+JHt0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:2513:b0:59b:eea4:a5a6 with SMTP id dt19-20020a05690c251300b0059beea4a5a6mr5435ywb.0.1694656581441; Wed, 13 Sep 2023 18:56:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:21 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-24-seanjc@google.com> Subject: [RFC PATCH v12 23/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 50E561C0015 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 66go7k1yge9xjs5pwoxjknozwrsfrsyh X-HE-Tag: 1694656582-82713 X-HE-Meta: U2FsdGVkX1+RXg51zhCD2amcpUrglhqhrPnNIBVlbI6a3cg54yR3SvPNsw1IVW/wsECYa9LQXJxNN4TVva2Qra5fUOuOwrlGfqSaug19OnA5WwGYyMH2bRc4+z8cub32b2mtFAkY5BA8doU9Xk7h+NONQljZ1LeK017EJiX0LVbUMwT/KTD0VY+P0EPO9W+GFhCjES6NMK0LN2Yj7NBTG0l+YzRp69QtN2VWyTGolJgAY1oOQkpO57W1hvtn893B2lmRm2n+NW7PIolGapuZrEK3e7co2yr/iAMtkDX4ar35OWNR6h+gFbEt9kdtfRW2Qu1xMiw5aaP81BtTDIgByebWfTsWo8sKwR3S63CciSdxEE29TDiLOI4FDYneQQ+vduRG8pJgybZD/OPe54GQcZ4aKC1sQTSQn7p1wXgZapj5UgJbOm0GjWBPKTWA8/Uhl1Sl6BlB9xN7RJp2TlcGkRcV9+z2AQj3wDA2FQgm0bm/yu3p39QEvvTJoik6e5YJ+Kbf+dG5yIceLNjn2jOgp9IKjtIIDh7XiSBv8+G8r9xbZB21jYmzp443rrZGksv5DvOgJAyvUnGYXieHlVK8hT5732M0G7pWP80su1Dm6fewUNUcdsnCRf5NEz4+KjeQ2+NL17V+xbAV+i5JoqCD9NRGIHOtfKHvyAtrtI/GWC2ok2M7Pj/NeFyMvcjDrkTF/Ttr8AxDXFWhHu7f4fNqx3S4xjfEF9mZkSdigqGCXHRdAA7Eyvlz4HpOzoaNFo9BSVGysXtmGhlHVBdq8oPicOcagaEFKds2DUiIc7smTF/9USSzjxW2XyAsg2Jx6YW0hBqcGIj6jyDyzgqWstWSjvkQaAba4UKY1ub9z8S+GjFYhUiBIcb2k9CbVvrIvwzYg+M+cx7yND0tKn91T71XUhCLKVQXPBrRXJ9RDC+Z2izAP6G8E2N1x83AP+Mf+KI8tMciI1UZX0SqXlar1vR nwk/9+Eg DIQ4IrP6X6K/WMXjr1SLw1PwsyeAajTFUuvX9FbxJvnx8iSpsE1KgF+f2e/7EQmaRL2T/3HAIJ2ePGyveXnoXpnNAmVXbW973MFjkq6ISM68QrMTvwk31DGrw6H5xl+wBBHtuaw6G5VLd/8m3rohNf3rWoPrJlpSDpXUmIKmlO1td+JKo2QQgHjnxGmxLRjXDFkW/JFm4Gq2qPIiG+K/EX/8IkrKLYRk/XucFJSsAeaaAssf5Y53jDs+sK+g5cEbTGeOsZldur+5+fJHMGBggg/6NY0lYiK1YxfgiUTB1/Epth+GBxAA6q1XH6K+kwvYJ4BWmeK7w1X7tux12Ig/qZ55IqV0eJNgvNQnsNtbZaHIrEiB9v3G0Rb8Fkih378l/VP0s3VpstSCvwiErsm7IlTf2SF9jzIj3GrkyouVIvPXhFZmAXRATIHLWXaeDl0aIdkqVfAbDkFr//a2QRCq792cbdRyDAzJ8LuSbTH6PbnrkolYfObcASR0GWPTaTR3zFHIr6oHicxyBJvaB3QYw5XW/lJYWyYnS5XAe2osdu6ejpzNAiSw5v1NdqUiKSmwNy8w3Wy0FHyTFHrGXc0M6dwu8KChH8oVW56ZurnokxwBDd1H8cFfOaXU8RjEX5JrrxrsRqhwM/eGGoX0qCkDgRbOmx5/vyJH538EPOlCvemSLOXbdBJdQSrdaGl04gvAL0R58cMU5/aC3SXaRZmwN2EIgjRz4qLQMeX1NNi4+hDllFqMtxIM2lDW8DB+n3O8gRJnKt+owaeY5qzXnmw+PuD3TdPWZ1ahneuf1a36lb7M41atP0PiKTrLZUQ== 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: Use KVM_SET_USER_MEMORY_REGION2 throughough KVM's selftests library so that support for guest private memory can be added without needing an entirely separate set of helpers. 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 Thu Sep 14 01:55:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384031 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 2419DEE0203 for ; Thu, 14 Sep 2023 01:56:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6CAA8D0007; Wed, 13 Sep 2023 21:56:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D1C888D0001; Wed, 13 Sep 2023 21:56:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6DD08D0007; Wed, 13 Sep 2023 21:56:26 -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 A20178D0001 for ; Wed, 13 Sep 2023 21:56:26 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 82FA4A0F5E for ; Thu, 14 Sep 2023 01:56:26 +0000 (UTC) X-FDA: 81233538372.29.8894692 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf07.hostedemail.com (Postfix) with ESMTP id B19BB40009 for ; Thu, 14 Sep 2023 01:56:24 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=K9v5fled; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3R2gCZQYKCFAAws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3R2gCZQYKCFAAws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656584; 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=OxMDE800j4aiJ8tPuJinoyPTmv5S2+AxjnM5iapuS4E=; b=godiPeKXpo62XnNxZ4f9dKZIK/z4mpmTo9THCyvHnQbcd5mCu84jvgdWc3pBTpvDYsKFqr qfnsw7st0Ze379nt3J6Tmdfdmcm3mSEQgroltvtEBv2E7fQWr3O97cG/xiotk387TQTWPt 6WNWlizlc1kf4/9S9AHRH2+WNX8DE+s= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=K9v5fled; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3R2gCZQYKCFAAws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3R2gCZQYKCFAAws51uy66y3w.u64305CF-442Dsu2.69y@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656584; a=rsa-sha256; cv=none; b=wPaZ/L3ZjmCj/aEUX98R+JCNWok/BdcGTnDC6FUTXV7S8RSdHOnnXsi6NQ2s24GINuHvap QZ8zVRRibjUojrWjJvWsOQ/c7v42ZYTrn43yAPKDhDDe1hDIODoI11DMvw+U8jSOSP1Pe8 H4Sl+focU3LOUNUn+mU0vczYDCUYVXE= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-26d5970cbdbso499541a91.0 for ; Wed, 13 Sep 2023 18:56:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656584; x=1695261384; 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=OxMDE800j4aiJ8tPuJinoyPTmv5S2+AxjnM5iapuS4E=; b=K9v5fled+UZYGIQhq3ph1XNIhq7eFYpeRv7/SvW+q9HTHkvpmF297HJrQ6AZcWE7ZS AtH8xXhuQhhp8djMaBDotnvxh+0PHU4AkVR3lxv0xNVeptpLC59FtRDt3Q6lNgc12qHZ P33YxEWZo0bJRjf3H7DBbbyywMnvbpKrUXlzFn73KDdtDqYlx/96fpU69tVPdoBKZzCR t8rQ+cVu4PE8Q0Clupl9uSMVmY2cJT2RcMTHiSmo+/JT4spNSh6WmJf9rJDsifi2VQ8a Nd7eC1MHQ4wlcoOFggfNDg2CtLquz5Zmeo1WYI4vdbMi3Cs8FQ8wiKFuZ/Q/MEksKleg xDgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656584; x=1695261384; 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=OxMDE800j4aiJ8tPuJinoyPTmv5S2+AxjnM5iapuS4E=; b=Z09VudM6Pw4rU6bUCwO+dNlSudJrzox3dJ4xabIpAh5FBoljsE+INvOUSx7snCUu67 fM3UuQjIqJQyXXwSSypu9oGmDexZfU63DD2i602Z3ygjeBA3URihY79QiAInWsdH2cLE at1WRoeYWVawmYgF0BxqmyoO5bx0xEEJw2CclR8f+GnB3kk3Z3to8f7RgnMzStnPrCDu djHpYMyO+5HYSqOeBlQjgZvpp/RRSLuEX+iU35XpvFlLbKli7491LaUNySX0znZvaL4K EBlrwgbFOJQyQ01l1aJ4nZyX3Vo21SP+7l/coXgbMRGfgo4i89b6ix5U+rrP9ZvxmrYR WbCQ== X-Gm-Message-State: AOJu0Yzxaq6dU8jJ4Mlwfk0S1ocagzfTX9nv9WZOTU2sevFZgrCuA4KL e2ezN2XYaK8PHo6xFOj4L9JxzSu4B6g= X-Google-Smtp-Source: AGHT+IHrJ+akemSkjusv3tsEKJ81jQid3VaDO1GbVbcTPU4KobK5t8Z1M2fJ9kpEYdnobHHBOeyWBEN2WZU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d50b:b0:1c3:29c4:c4e8 with SMTP id b11-20020a170902d50b00b001c329c4c4e8mr224068plg.4.1694656583660; Wed, 13 Sep 2023 18:56:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:22 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-25-seanjc@google.com> Subject: [RFC PATCH v12 24/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: B19BB40009 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 84fwbsre6mzdijrf55wfosdh8ohrm6kk X-HE-Tag: 1694656584-390577 X-HE-Meta: U2FsdGVkX19vfHGlmEseRjrZMcENi25fvJlDZpSJXxHqtyElwd32E60GpCQCrknXPPGNn7jW34+kuGqYpwA2VdeqRbWQ5JHq3r+1RIr47B4HT1LIhfNm7gqhedMgyqYHboH3lBHe8yYDk6iiwP5qm7+o15tnqfreL5nS4zUo2oJiQ4rxCdTPdLBEQFeFMQaJRM538tnmX8onio/VoaudzcQJFNOg8ZNoWxeiHd8gcsk7sUsdjLv8qD9OLJQnRYYABgpPp9h5pB6fExieWcLgONs6TSoMRiJZgZRZz2KXjXPXpL4AE+ls5e+JDfEon4zA/5kzncUM0Ah1UWK9Lx+GSx+APxoMONWBsBM5BSKeTN7Aj1HZgkPWH9B5PIDdQIN3Te8RGjZ82oDmVzNrR22v3Rvg2RiVZtbFY8falmekdoHppBgxJUpKVUdWDYdJSNr4k8bNUKS/dwtfMq5AugE9GgnNNoM13ykmGDwWl3y0NAqKzpIZSILznLIqFQjrS38PL6ibraeu9POHS2v5L5i5++S47ppGZZmjAWxjj9Q3WiJ8ZwKbo2XsIcM6cIV1QKJm455AyEfQtuhYm7tM+shB9xQ5yERsfqN4L3z1R5iGNbknps6YjCvZBZ4z8Q0XLl9CjHXPQuTv0B/vGCmvf84q48OyHSDkF4x4Ky/+WD2c3OE4HhOTN7oGEZwLF8sdRq+hEHwufMeOR9zXpmX0h42xMLYE6N8oZ/hdSqj0ARleZwuVBTEmytFiIOo3fSmYLWZ7rI89OSdvB7s6VjRUhUPbk/tsja5te8HEngGPVy0HT1I2cXofNLaunwx1ljFt/sObkUSc2+VPTI7KQrR8i8B8VXv2WXeaRx1EpmXEYzCL3nAXT1RTukWJGHYa+X556+qpdepGI+dP8XYLIUOko63IfAHbbp+gxrGtuKPn4+uQhqt6HmgSCjojGf0wR8t26mlPSb5J25tScm05f0MdaO3 gBfIaIlk jhJBPqp9c2Z8hHXkiDAuO9LtksjO5UL+WOiNQsYVucTKkfiGqPA+TtAyYzXu5GCftkMqGaipARdukZ5FWv8W1CqGOgGVNX0ca0c9yDd1OT0uZkNDTTaVj+yrmGiwlabcdYmoxTGKCxOTNb0YoBbe6p5JjcI9SlNheGpOFKzrRS41P3wkrxfbWfAjy6JhDtuwfDPkBEniQ1SwEfYQ3IrD2Gyb+fMpPrFpekPGW/cqUoJu5qJ2VyajqJlQyMJo5jJoZsMBIxaFTKh4Br9I9IvO+WsoIz41TqD7rQTCwHI+KYV38cKP87JLzuzbT/6erpaYgCLFAXecQSVE7RXTyQdF51mkcFds7oQShH8+X57Xub5cAZ1HyU1eP+onYha7KYSROh8ES+HX/Hcm+UwkpF0bazZ8cI05dOgR47kO7ivHzohto+KL/MMXDFCHStGZstUXvd53FV10DZj7HRgPOiDIqQ3dt52/6uobBVK1gJjBg0Mn+1QfAYlibhRWduwOD1KZUNNmQnmqRln1hFLyjhTJ5fxGs/y7IGauchJroZbisBbZ5p6/jA8LXZPfBgTFF8nnBHp2VnskwMUPprRI1w3dAknQNnrhlmvEnUZf47cyGFRLAFAd82mPorM4Oh9YAk4B1wNCnL6ov+9Y9kzTsdIA+bxHXYsbilo4ttM3J1iAxTs3Qh3jbbRSvoRTbKu2zDtaPLBkU7EFk+NVWRTuUaqDniv6hAq2HZrWm1hwWwhc85ydpzpShrt3+IoU92i0hV5d3dLrJ 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: 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..47ea25f9dc97 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 gmem = { + .size = size, + .flags = flags, + }; + + return __vm_ioctl(vm, KVM_CREATE_GUEST_MEMFD, &gmem); +} + +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 gmem_fd, uint64_t gmem_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..127f44c6c83c 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.gmem_fd >= 0) + close(region->region.gmem_fd); 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 gmem_fd, uint64_t gmem_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 (gmem_fd < 0) { + uint32_t gmem_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) + gmem_flags |= KVM_GUEST_MEMFD_ALLOW_HUGEPAGE; + + TEST_ASSERT(!gmem_offset, + "Offset must be zero when creating new guest_memfd"); + gmem_fd = vm_create_guest_memfd(vm, mem_size, gmem_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. + */ + gmem_fd = dup(gmem_fd); + TEST_ASSERT(gmem_fd >= 0, __KVM_SYSCALL_ERROR("dup()", gmem_fd)); + } + + region->region.gmem_fd = gmem_fd; + region->region.gmem_offset = gmem_offset; + } else { + region->region.gmem_fd = -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.gmem_fd); /* 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 Thu Sep 14 01:55:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384032 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 68953EE0216 for ; Thu, 14 Sep 2023 01:56:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 974F28D0008; Wed, 13 Sep 2023 21:56:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FD0B8D0001; Wed, 13 Sep 2023 21:56:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 776F38D0008; Wed, 13 Sep 2023 21:56:29 -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 612808D0001 for ; Wed, 13 Sep 2023 21:56:29 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 38B26A0F68 for ; Thu, 14 Sep 2023 01:56:29 +0000 (UTC) X-FDA: 81233538498.16.EB5ECC5 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 6921F10000F for ; Thu, 14 Sep 2023 01:56:27 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3X6aX5Rp; spf=pass (imf14.hostedemail.com: domain of 3SWgCZQYKCFICyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3SWgCZQYKCFICyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656587; a=rsa-sha256; cv=none; b=SqMD+DMw0qRwBViguO3eas3kGFsQO71UWM1nYixhew8cSn7W3zyJ1cYe/4HKpH4jfpTZoa NGPZ4ktneUdofqBTLbrsmbUNiDQ9aMzIBmavvtP6KxnAhTLPp6NDj5UcpSRe3aGA76igT4 b0QoeonGzP6QJmRp0erE8hPehSHLcbQ= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3X6aX5Rp; spf=pass (imf14.hostedemail.com: domain of 3SWgCZQYKCFICyu73w08805y.w86527EH-664Fuw4.8B0@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3SWgCZQYKCFICyu73w08805y.w86527EH-664Fuw4.8B0@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=1694656587; 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=yQPra1dQiB3SthC1x0t7SGlDkIN4vOXtCvjVK4W5apc=; b=c3lOEsnsiR5MeuR6iK2svQjC8KA5f69TKEux77dAx7MWtYd9VIKMzcNOlq/laJnhPCEwr7 Y1I5zFZaPUygdtH/Fud7GCtGX9tKcLCB2h9RubVDFrsrDTyCK/wLPzpMNg0eOf20w9x/lF 8SlQ03bTcEOcR53Gbcsefof7hwLfHM8= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c31359832dso3886945ad.3 for ; Wed, 13 Sep 2023 18:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656586; x=1695261386; 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=yQPra1dQiB3SthC1x0t7SGlDkIN4vOXtCvjVK4W5apc=; b=3X6aX5Rp8IHnakrOo7UZRNJ81RAwrIfX4kNwtG2m9Ubw9orEr8da3+gw+azoMJfHaE +0wMpDQTCLmuaaGYvtGcxgwmBSm8p2nQuT9HM25exPoT3dDG9/tAPSF3R6ujP39Ynkgo 0Tu9jJHiJVDmjEKazC4Iv56ziXvoVUXmF6VQKFvsk52FPj+z7HcfSYDKoMQ5+ZvmSYkr kB6HlndLMgzNqodfGjkPYwQaP9NvIZM8m4WlAvyLm2gHfojtkPKh9Ml0WEp27KEAqmOi n4HYZ6xxLSLS3pRahSM6ZdLi78vRRox5I6uZ358UMpykfO+v56k2+S5uh4GL6cJdUZKK gy6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656586; x=1695261386; 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=yQPra1dQiB3SthC1x0t7SGlDkIN4vOXtCvjVK4W5apc=; b=iO3IczdtMxBhIYAmKS6e26cWYhhPw/QwSBfxC3hHHGekhKANGMujCHCr557gEnqSJU F+YXBGJ3kAVL+vQBNwKuNvJjRJ291jfZ10UQPv96gSJalHFpniOLlxQG91ZUbeg8IMel 2A2YBXDIqecW4SiQSYPte9M+fLDAUzfwl9ZMeco9h5QwoZxjIDhTdj2X/6ObJmgrMme8 18XQdM79yKXmzV7nv6c9FX488WCDf+Hlh+YtIUSvueUTFAs/wmOQChW9huqzM5Dsx2Zv JjlsTK92EmltwZYsxK6u25qYl9fdb4mjQEhJPtS4jKcXbYb957txqIvnECsGthTp+PHm +1HA== X-Gm-Message-State: AOJu0YzQWhj2JYb6AXQerbrx+MwmYJpqUqGrQcVopjl/eTHrviVInexL lrj7bmjg1+ovas2mL3uXgmhu7EraT3I= X-Google-Smtp-Source: AGHT+IFAHhW5BZGO1uppfpot1pEr0aAhGs0ZmH0MKLDsA+afIocjRRcxvAD33MR5tv+EpN9UJvITdXcpd9o= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:da8e:b0:1c0:e87e:52b9 with SMTP id j14-20020a170902da8e00b001c0e87e52b9mr204011plx.12.1694656585587; Wed, 13 Sep 2023 18:56:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:23 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-26-seanjc@google.com> Subject: [RFC PATCH v12 25/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 6921F10000F X-Stat-Signature: fdk37qopmskpaqmhrcz3jmhbhk7ajyqn X-Rspam-User: X-HE-Tag: 1694656587-940833 X-HE-Meta: U2FsdGVkX1/aqoXj6V81PDkHub7Vi2rjz4s4d4crucLkUC38SCqQkeEiawadqFzlD/KJi0dBotygGH/G7nXVOtRORw6VRY+t7kIoLkJ9c/Gy1xdjDGOY37xzLN22Lhk0WlqJlbiuT+8TkN5GlKmt804QQTLSyDO2AExGEWGDR22Z/1L09INoB3UwLrAMYzPMmISP3VwqgtGfgWGvKLvhuU2Jj5V3lcaTf+25k3Z25zxvkf3PdagsJ7osLBgrlD1bsAp3VRVGW/uVx0hYkp5k7+MZFZtdSnupqWXoR1Umr6UZOlg2VVaMsGXz6hJkquwivg2CN8sIMfr9ePu+iTA7PVAt3i89zlKdY4ZpRHIaY9Z601rDNe7xEf3c4D51Mhp4kNqQ1y6GhWQ+6yuhTTKs07kg3piv79JQc0mjeq2t/RxQ2E3yXLyVEiBofmTsFt1klXZFYjAdzxyhgEnmB3aoWP5a5rnXp1kyxczgTz4wTSIlEq1Ti0eF+J1oZh7n8frMpgPAuPyR9fPFvIGZXSf1Yxq/MwIbrftJmKOUA9vF/5jyST0jPiYdJIw8mVNkgTlqluI1cwrt45L4Yr7kB2W7BpEJkGa/OlpyxtpF6OwwLgFwrywGm9ZyKoITRC4yfjjLOKjMTH9HTA72rj5jONfn4bCKsq9ZZlkmr0XxwUuz5DIjGIX29hCRxTqV9uxJlY5Y4CnmjR2ndx5g/GZq71p1weEZTZpoTVZXIEdxMsQJrysw7aT8eq36A1rG0Yppw5/VBkstvmNrkLj/IE5wgwhUQ4Km0AuN3/qRHNAUeN3m2avLMMt9srkBtlLHcrqveLCfOwijLi0N/rTEG5IDcVTZSG5fakHNIE8HNTQRrWpZcPKAsouVQhYHMvDzKCxAD8MRIEgE/m7awYFGH2alEh6KKAJm3Aa+Lj6o4rXJeSYf/P6OjRRQVZ8K23FBxC9r9QLzWRyPN/BeesESz0FtdLM Iw584Rjm /97C+0hCdeu83QRUHRi/71raob+7qiwmE0RD/AFrMt4LAXhi08Er2czs9qCGTR+gUxwaVehFzseqVA3vk/sjsLIVNLKxk7QyV3arY+7UqDURmAqD77CLu7ozCeh8PhuXFPidiIW5o60nJAmZiCdkzjB//V+wTsVegkK+iK+9HdHMLpPnMJ5WZ0mquoV8WRTesbPCaasX2OcaVVYWH7v9YEpnWuJYf9nIju4rBPhQurTP9aPW78jUTMTqbc1K6U+nRpPMt6KVYLdYF1ke63dhoHhXFbX0Q+QCVpIoP6E/Y+mPVMOOARYsrfN5kwq0Q1ufcEf8D35GTuPvsRvLe9EzDEeGF4yarMQiqr4Fl3qZi8k8OxBU03YYruf1ndbaNe+dE4oynBpaYPmVjv57yDilerA43c8MUQum4eepR02nLJmi+23c9oxoCHmW9sUd5XnLXZlYWrclQ65NVsuVoJQzihiplp5299yff5ObIituqON1bx5QxRQkjG/TVT83Q2T9VfIpnzwlAjk+WTN764W0y1vL2KbfMCe5TKZ83z0BdTJUu5ICbQFkt9U8sagHRTMYASNBf4tDO65pNcG14F/V2yXhAju57exs3nNEVOjLSwHSPuWKD0SsFbNt5AeImdJAd1xR0hUSh6GDughRRiPKETU5+nMYLt3k9JK53xfYed5RRN0X8iziXFrdpW9AJoufDshnbIwFJZIz2CoH0zVIoCWsIp7mv7ZZQjc+vqblq4VfcnFQzgRViz+7zEN6MGm+FwHKilGAZ1I8ibmg= 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: 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. The fallocate() helpers are provided 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 --- .../selftests/kvm/include/kvm_util_base.h | 48 +++++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 26 ++++++++++ 2 files changed, 74 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 47ea25f9dc97..a0315503ac3e 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 127f44c6c83c..bf2bd5c39a96 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1176,6 +1176,32 @@ 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 gpa, uint64_t size, + bool punch_hole) +{ + struct userspace_mem_region *region; + uint64_t end = gpa + size - 1; + off_t fd_offset; + int mode, ret; + + 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); + + TEST_ASSERT(region == userspace_mem_region_find(vm, end, end), + "fallocate() for guest_memfd must act on a single memslot"); + + fd_offset = region->region.gmem_offset + + (gpa - region->region.guest_phys_addr); + + mode = FALLOC_FL_KEEP_SIZE | (punch_hole ? FALLOC_FL_PUNCH_HOLE : 0); + + ret = fallocate(region->region.gmem_fd, mode, fd_offset, size); + TEST_ASSERT(!ret, "fallocate() failed to %s at %lx[%lu], fd = %d, mode = %x, offset = %lx\n", + punch_hole ? "punch hole" : "allocate", gpa, size, + region->region.gmem_fd, mode, fd_offset); +} + /* Returns the size of a vCPU's kvm_run structure. */ static int vcpu_mmap_sz(void) { From patchwork Thu Sep 14 01:55:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384033 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 4FC7BEE0219 for ; Thu, 14 Sep 2023 01:56:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D1A18D0009; Wed, 13 Sep 2023 21:56:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3826F8D0001; Wed, 13 Sep 2023 21:56:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1AA9C8D0009; Wed, 13 Sep 2023 21:56:31 -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 0719A8D0001 for ; Wed, 13 Sep 2023 21:56:31 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D72ED80ED4 for ; Thu, 14 Sep 2023 01:56:30 +0000 (UTC) X-FDA: 81233538540.01.CD77419 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf05.hostedemail.com (Postfix) with ESMTP id 1846E100011 for ; Thu, 14 Sep 2023 01:56:28 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Yv/9OMEL"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3TGgCZQYKCFUF1xA6z3BB381.zB985AHK-997Ixz7.BE3@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3TGgCZQYKCFUF1xA6z3BB381.zB985AHK-997Ixz7.BE3@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656589; a=rsa-sha256; cv=none; b=mMt7Gv+b0kzNfd8H6WN4elA/mCBf36y3tV0TSEzp95BlJCEXvOR1qxpKkHn0CFuAf9RmPf ceNhnwO7AQfd+W7ea1RCsDXvsjOY9r/XtalT4Hd3yrfO+Lk9I6FkSK2I+exy9yzLeXGp7u av+DRjCeM9BOUhNBI/RxPgC1GjeOmsw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Yv/9OMEL"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3TGgCZQYKCFUF1xA6z3BB381.zB985AHK-997Ixz7.BE3@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3TGgCZQYKCFUF1xA6z3BB381.zB985AHK-997Ixz7.BE3@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656589; 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=UjBddbZXLqwDqs/dRSKLKY4HwUhf6Ai3WxLtYL+KxyQ=; b=TstT270K/VRbTxnWzxf7Y+uSClzvVNSIcAv4aqcs6kiGhipJMUikPYcbqpTLPc3IWQtrEl sZJ1nT5l2ZnAWsxSbpFv1zszGdw47LzRpYeNGQqnlgomppi3FdNpOl0QPi3VRqPkayEMMB V+ydlNWcZdfQk0HFdUc6M4Z1D6x10Ls= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-594e1154756so6572687b3.2 for ; Wed, 13 Sep 2023 18:56:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656588; x=1695261388; 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=UjBddbZXLqwDqs/dRSKLKY4HwUhf6Ai3WxLtYL+KxyQ=; b=Yv/9OMELYO2oatGSAK9VA6gJW2WeBOSQEC1maVexDT1vwiBiAIXS6L59vmsjBVwc2/ Q5e/ho0lWMvl335nV0i0r0Kgm9O3005kb2kHM6ETKsEpwL8bWcrtAtQ2o4r15ey++JPx CvclDNMxoVFBxqLWHBWd8ZdSdkFlPBtS46N6HSRBZwzwoS+nVB0DD8q08V2raPUDOxYo 8j7NvcfVNAau3kqT3ayrj9lFbsgOlMoz3jFHq59MhmTp4WguRUc0I18A0B5wxB+rpc2v FrTzTZpt+C9we1qDLWFjpeAEqGraTqgQRD4dk+DgLCH9utJQISyc4UFv7dFMnVraPvFn 3dUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656588; x=1695261388; 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=UjBddbZXLqwDqs/dRSKLKY4HwUhf6Ai3WxLtYL+KxyQ=; b=hLl13VwGGJm0jhM8GQGfg7VwCW/Kmr4YILXzDrCzkyK2zxuWTHqYA00auXQpD+SnT2 kExj6H2r53g8dNy79JN/354uyRyy622mnmr5kb6ZfJXXVLRZrUyJbc7kKRb9q6iYnsb3 Zkd6QvlRrlopCmGGbsUzOdQGQP3ZoEUNlu318eEy9PFIThmeUWBasBg9b3rVIWM9pe9R xYKddogjk1Qr6XTG9FWqrgqxz0hbYgu8byI9EyhBPg+0hSPeMzcrulDYG1yLEqCdXeNA DK4W8ZWF2/Q2rgxVOUxSM+dyiNo2/Zap8s4WHL2XKFuFO9hXjMbq69mm5J6qpfCZ/nuV phlQ== X-Gm-Message-State: AOJu0YwBqhwgeMXsnmpSYOEY9OCHCVneeWhcdYaZi5s/2gJhYoui/X37 De1Ibi/THdPgi3n5RktCo+G6vzCNaOQ= X-Google-Smtp-Source: AGHT+IH0gGeX6zOzLjc3uszP5L1cVBy/yaTsK9vfF/9VEoisKrc6F33BJumlmtJCwJ85BnFPZfgq0s7SPdg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:2e05:b0:59b:e1db:5633 with SMTP id et5-20020a05690c2e0500b0059be1db5633mr53066ywb.1.1694656588217; Wed, 13 Sep 2023 18:56:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:24 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-27-seanjc@google.com> Subject: [RFC PATCH v12 26/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1846E100011 X-Stat-Signature: as3e7oiqi1xyn4p5473z6g3jeamp4pd4 X-HE-Tag: 1694656588-425051 X-HE-Meta: U2FsdGVkX1+MXz8xXeqe16MbP1NhrvwmYjt/ruaJaeDkGGYHPC/8NoDwVROSNdXY4l6mGxjv7cqlChcurk4TINiyWjsNrjWcj4Hq1yMZekK6driEl6zBPf2ddDhriXRzukD6GphrejeeE3P3MKSl32zsTC22NIrGHFBtzQEvlAoCCEK+l0bDoyHhFYWJ/vvOcA5glTIarpNT1MempUxNWSIMCdPsbFJkXKG7aGH4a9QsVCGHLqkhv5yktXYgofwVIxSV1/rTKEGCTjqumvUSF7k8I51uaadlZKguCsY9P22XVC+/jpVXKkUB6E6V+njcVPydnmnCRTDJ6utkb9p94iWHi5dUZpUP3QB/c2LxPFL+6h3SHc/qhBa0FbZR63eGD50RgC+6hNJua29oHiLADXol3XMlovh2GUKPWCMXx1gllG/tqMm2zYabBLcj8PEI6CO1mM1WGMHQm6C3K3b9vTg0LzHXerDNvxGSZJp8PpjDQRtppODl4374OYIhQwZK+MmNcvn9NKJLCJXH9rgCaoeOeRZjOzVCuPAhUZNBeSxg9uzKZhQWy3Z9towACs/h5uSCFiFodiox0WAYC+wEMVUl/oO3W2Nln28fJdt6/uuQmzrqqUxBTX0wkVZs+cSe31LpfPmiWZcM+SrA3+LPIdt1sQdQU56HATE0EyHKVZHwRvTL55IekC+cfMIW0gQOyYi+pUdqaLKi7mxR79lRpxIILyjHLfAsgmwuJACAZ4UQGPGvuWlxRPz4Ohe7NSXZShBGhC5qebjqek4DC6HeQg5xHaqQyA5le/4HPR0vavzS0hSBX783a77W4tMuTOSJtoi9c6Mpf5ZOnZxq/AgZdOtxGAupx5QP7zOBL3uoafXSGBP1xBxO5N0zrKSjQg3UA2DZa8zriHCO9xiSDacudkYuIOBn1GdVhUVFcSAnbkZinFSGlo+k3YExb2/hHz1WIU6XSq/JBjfUx/lAH8M hkF4K5GE //uJ1SZgRfRDWh7c+MMJ3zNo+5bDkQ6eg42q9I/05MS7QzbLG7GNCAUL91StNmH1Gy2p6KR0xSBPZwEpt9hUu7mQ3t+4QoBgrQVYQlE7OqGfAQvRke+Ede09rBWs/lwhqYkgp7pvBILCsKsr/AEEehquIQBz3Cz+azq16Egwmtpor6cqJ6GtCNKI28H9+k6JhgqC8e2L5KPm/R0/vjGSrKsHTQvREnnpEp1Am3W+B2SpBiWsSDF6CD6+g6c4ZJUR+tGVpphenHJNxtbZHXsw2oBUC/+ZiiEKcP/pbAK/mgeoCDbFPNIyCHClyIr0WHLMDkmzFcvoQuk8e0Ks6gkgT3VlCsCOM52CwvTTJYWbRTHGoMdIKB9V9SXEOaPRqAWUlOJbhDMI9DSfdT914nSxXYuivvnHVlYXnH4oZ5adjAjIJEskfdLkwp006c03LS8sFE37anUA35uTf2uJMEmp5DUNc1ENP763eu9yOgHis8xoI5bOYH/6whTymDM0vjvXxyxW+FqwonR5NbmKFAXTaqUZc/MGAwGgFmTH823Mgx3k7kktOSuXn8sfVnUu2h0Lup7UqhLBL2ttuMbmQ8LMeJvj0an/neqD1SaSx5LQPeinURLWdQEXWJj+aF5IztF0KvPzM4MWOzZZlQt38oz+/zFGp0xYeyqA81PedmIudjwx7uwvJFpLi3MeIo0dURzFJ/u0JUOujL2dihE+j0eQlQRVvPf09VvNNv1qOKrUO9KxtO8PWc37dJx4InOPA5uPpeSLkL6UQwgat9GpPy+5g6W2BMq0nnkjmHtm2eF1+qSC6WSo4Gr1oNx5/JJe8YhvtNUdHrlUc9O0K/YirEBUEgdcNVwD2rGPvyqCl 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: 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 4fd042112526..1911c12d5bad 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" @@ -1171,6 +1172,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 Thu Sep 14 01:55:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384034 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 10BD8EE0212 for ; Thu, 14 Sep 2023 01:56:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6DD228D000A; Wed, 13 Sep 2023 21:56:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 68F308D0001; Wed, 13 Sep 2023 21:56:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E23F8D000A; Wed, 13 Sep 2023 21:56:33 -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 392168D0001 for ; Wed, 13 Sep 2023 21:56:33 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 12959120F29 for ; Thu, 14 Sep 2023 01:56:33 +0000 (UTC) X-FDA: 81233538666.17.CDF1BD4 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf13.hostedemail.com (Postfix) with ESMTP id 3290120020 for ; Thu, 14 Sep 2023 01:56:31 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vjl+XAQq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3TmgCZQYKCFcH3zC815DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3TmgCZQYKCFcH3zC815DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656591; 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=1D+mcCC4ruo3JVlv7Hlb74B5VrYwifsCPpHX73yGnr0=; b=zhh0KKCLyMe5QNSc9GLDwpFXzQqKYUujKvFZRHjc+uuE//kkWycGkCmFXvmzGWIjdegjfs ri7upW5MnEzv8CWMb+xwKvMctaPTnhOm9AO67Jjr1r1BGZTezCQYmv2QysX1tzFYXIJqIm 7hYQYysdYFmAF8+aWCLIUvij901VrMs= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vjl+XAQq; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3TmgCZQYKCFcH3zC815DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3TmgCZQYKCFcH3zC815DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656591; a=rsa-sha256; cv=none; b=t8/5bbKSaPlaPDmHdlFOIT4fEh6/uC/xGiRiQF4pWieFd7LbykzPfC7ByQlsoj9RmFe4Mc MUrpIah61WJtQvvKpEC/LuKLFPNVc6PnATg/eDOUwavGXkXlatl2MOljaGXctbbY5FM8uo /4RETvZW1cMRgTjIe93oFhttw+wMqzo= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d81646fcf3eso628247276.0 for ; Wed, 13 Sep 2023 18:56:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656590; x=1695261390; 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=1D+mcCC4ruo3JVlv7Hlb74B5VrYwifsCPpHX73yGnr0=; b=vjl+XAQqALI60C5MkM53ePtpwPDbjTqJTSZ8NYHgUGSeRFLDol7rE7MqgkNPNacjtr +oUc+pADZoHsfSu6R+webyqeK9z0noaSsrsio2p7LAzj6lV1PuMG7d7RJzKorym9L7MA lccA2y/9qhmF9sKv42D2rKc0VRE82iud+pmUcCwmsKOwz33xMFCtNzZXmlvBeKXsSfmW DpbinCzphZTrBWnO273+n+CLbLkhuiDp9UP+HLnmHL98CJSgMhWJQd7JRIxNXkFpl4x0 JZDCiYpQEofcu9wpfCTx+nkDZ/JGjrrwyogpSv99N8fCEu5ZUH++zd77EZ3mRP5Rq30h 6kbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656590; x=1695261390; 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=1D+mcCC4ruo3JVlv7Hlb74B5VrYwifsCPpHX73yGnr0=; b=lGImOT5/N/hgFzDuG1dBcEQTIP2Ftb1llMW87bOv5nHt7bNux8hm1yLfp+5ptDv4mV LT9BWak7oO8+5b/5A7BtdXixbqPwmDql76bb4e31+8NCvv+/fS69iDattFzoiDV4dBiz eFjdBgimK9tjrb8HI4JuF9aFUUwhB1HDBpsyjGdZ+XXSRnmZwGsgvJzmajAEejXzs25a JesFfIDTNt+wPFjSnJ8MB/5R7/BLC+DgdDzyd9KAL70a8ZxeLyLnQFwdvik+PYL9TNwV OzXAueocJSiOUJii2/MYGQqzuA2mv7Ks3CNLIeA8WpBPr4omNN/FAn0yA6kl/50AX9mK RYvA== X-Gm-Message-State: AOJu0YyO9vIPEyOi+6/XzRZIBxdRsGWpMzhhhvd8HvIjp/Zo4Ckkcoxi pvoGUcP6zDs5s3miVQ2VbJanB/YHQoo= X-Google-Smtp-Source: AGHT+IFoTDeavyD2giEESqlhSPBk+MO+O3rBZm4uq7G0VqTMxwWtU4QKtOyPXHglJsay30cXObwzZigXcjo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:c9:b0:d35:bf85:5aa0 with SMTP id i9-20020a05690200c900b00d35bf855aa0mr115311ybs.4.1694656590375; Wed, 13 Sep 2023 18:56:30 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:25 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-28-seanjc@google.com> Subject: [RFC PATCH v12 27/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 3290120020 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 7so1xcqzx981ea7178ububb6j93ds3ph X-HE-Tag: 1694656591-877852 X-HE-Meta: U2FsdGVkX1+F586YekLGyVKIHZHQcEhaHgEPkdtGWOD0mpFUrzmAl8Us/zGtjifvEv3SmC95VVh86FRJbVgxV1xVTXpQJ3Cf2BQ4sbJorfs07g7iZV0cUk4zqlIf0n2KhN1fMpDNv/1bxWF27WdG4vallhQrf2vbtZsuDKJOmoKO/B8d6DhTm8Gp5XLC6AdBo0EYVT4ZLIrTh7zXUeo3g/O6ulMn7wxciLyeZ7wd53Da7r2f3xlK+t96K/JrXmYVpb7OH/4PBZIq/+0WETb3JW9v60ytt3hXvrPP1rdyaEo1LeUGcAzYcd5fdZI8DXVKhdDNArU6hNfJ2vdgqYXYrdO8LekWa4WQ7do1KntKvOin6R/3f0D4QJjFJEoKwSsVoMCp2jMyiYPmytAhE7grZaavnlfvczHMDVTY5gCaRkRq6pXQ7pCwCvkvkPDIAt/En0MoicyVQxfRh+AplGpU1HANHS1TomR4hs/1+EsHKfHRUjcUm3ovHXtv2QEpeFx48xCISJalcdcvm6/13gbkg/4IwqwvqMnZ8S6LBNMZr/WIQzm5W0fFGY9PAZTYGICjRPNGFlQ/k2zDB+P7At/hG1oEdZ3Zjo24RRjj6u4PKc4NpmbxXX/hltKVC7aOsIWm6kVFclIcYSPVyR+OMR9s3BTms32KHympYnJVunYOcziAt65nxDjHSYeSgzvK7JVeVE1q5XRSPhGtXxZRbCLLa1LGxoCYUkxaNFSgLz91zLr6/x4qxx9CAlc4aC3PUVh4yB+NRZqj+hbjzdf+0ao/nZidI8cQ2kXqt3po+uJ+GS5XPGiL5oxGeJD6ykniyFTomKpet2/Uxq5QycEoChIW+X/f56tirOe9jkshs+IxkW0hbKfGq8KKFnUE+yLl7z+1SSQAyYrQSgol15oeVTi1LvZeCqkRQIbCacHClP+wGdIH6gefZbhyYdvm9i2TlvyKoKkc+deMQ2kz8vG9a5S Xq5dOLMP coUop/uiPUbr2YsbJSU5rCLO4lXpxdQcNHNky8dXJFtv5epjkfk2L+Gt9LeQcyTzQZAtz/Evewx6rBY+dT1Fe8IYsnr3GMWCYU4DS/cKlgRiPCQZOe/CKC7bpxQzR1aDBJQAzDFsbhQ9Wgyp250oJn43H20erwNYeT9mCoiL7CtlGb7VJ+3s4qMlvuJo5T0mDvkj6G0zXf4bh107ZC55imyBoJdSmkc54HdCUCZ0lRT1OzLMSauLyJ9C2IuUG1n4+yDcTsfsRT46MIFdqL0aJadddIpLRzQ/79Q9IDmmxYR8aokONUgxoHvOlPAfeQ4fTD13EYeErVP68a/SRRcGjo5Ilo6RHrnGEYdIuFfDYLWiGOHOKOU5YFaBca+fyQfEqg6mGUcGMnY9uaJrckd85VEnjt1oLao7kUcumKbON2B0hFERJtT6JfaHGMgZ1Q/SSU9MmMDS5pkXjTnrGvOcVuzRvX6XgSTfyP/0Y44UnEWBdijqouxkOkxEThH5EpZZyKAZL2FXdzM8C8+QSA4UYcDJ5JnSDY7VtoF6+sZ7+NavvBj2en29Pv2UqfZDfaziX+0WMtGhhhjNQhQBuAIqkhwej00ZrJrWn0+/BbyEYi8jEMUdGu6ArGJ53kGTKmWfWOEPb7yjg0e7A/7G5nSh09PZi76Buzt5+fs1MTjnaJ6unARagdjAM0GBg5D09OAIzh6Vb55aX8VoZwDnAtdJ9g0ILIKq0+DuMgQaw+lH+o7GbLBEoSqByg82qNTb4gbIoqmVE 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: 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 a0315503ac3e..b608fbb832d5 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 bf2bd5c39a96..68afea10b469 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 Thu Sep 14 01:55:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384035 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 C8237EE0203 for ; Thu, 14 Sep 2023 01:56:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 46EC28D000B; Wed, 13 Sep 2023 21:56:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 41D068D0001; Wed, 13 Sep 2023 21:56:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A0DA8D000B; Wed, 13 Sep 2023 21:56: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 144DA8D0001 for ; Wed, 13 Sep 2023 21:56:35 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DA6CE1A0F07 for ; Thu, 14 Sep 2023 01:56:34 +0000 (UTC) X-FDA: 81233538708.07.4214C65 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 2145640009 for ; Thu, 14 Sep 2023 01:56:32 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rhgeZWBU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3T2gCZQYKCFgI40D926EE6B4.2ECB8DKN-CCAL02A.EH6@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3T2gCZQYKCFgI40D926EE6B4.2ECB8DKN-CCAL02A.EH6@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656593; a=rsa-sha256; cv=none; b=bmfVuyQI0cZk38265uJXuVsEm8R+Wahb12HBL7AF2C/bll25xM2N8qyuNiTQW5SFL0ANG+ OKO5nhEtJH6QeEKDWa2QdYRCl3eC/lHZWLxKOV1B51+x/fyD5aS829g7DDwl4ZxXHY2EU5 Kw5qPAW9afxB+GF21xBpPcWmK9iPBIw= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rhgeZWBU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3T2gCZQYKCFgI40D926EE6B4.2ECB8DKN-CCAL02A.EH6@flex--seanjc.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3T2gCZQYKCFgI40D926EE6B4.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=1694656593; 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=Vjv0zC4RrtxbIkJLIAO5e3EhKuQZW28HjcF6ypERxIg=; b=3xUk/k5zOmH3KJG/s8chm+CsIJNWNbmB4pBid0zEeSneleKOryO/BtgeyyX9T+0UYHzsr2 9xHMW2c79WPEX6TrqOZ0FgrY5xWYPwVcbEtIni90awBf1ZPhFcl2T/YIUailaSnKYINETy XFhdFOue9coxrHDTY0KXkIR7ffvpht0= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1c1fe87fc29so4092685ad.0 for ; Wed, 13 Sep 2023 18:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656592; x=1695261392; 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=Vjv0zC4RrtxbIkJLIAO5e3EhKuQZW28HjcF6ypERxIg=; b=rhgeZWBUUg0VlFtn1xzNiRVmc/OglmRO5WWEDzX+VE/y6ZMfYYnMgSExoQTXRzXT/4 BBJb2wXVTP6ubx/mRNOrJiVF2/GntwjE6sc8gG0SecDfyhS9hmvCw/bmBy1ZIS93s7kt Qpz5qYhpSscwApesIhD2h5l/nelZxgp+6V74jAgFnlXUFDoUTKzfNiuyZ/81B0yTNBe9 71iLwd2/HYsaVCwhDqWic2MbbK+PmKdc57Ng7JRWZpuGtma5tkaUjUU4L3tlVYwhnUdx Fm3Fe/miZ+lyTh0YI/EeTh2oRD8Vu/Gqr2SMCJjUiz4mUjzOwbBUSh9+5O2ybhHwfq0p N9iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656592; x=1695261392; 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=Vjv0zC4RrtxbIkJLIAO5e3EhKuQZW28HjcF6ypERxIg=; b=BTO6CRSGtxFrChO7S7cOVBpZZuHfUvuZ0T2CnzVzgKj88b5jIbk6e4F3N31eJeKmMo d/FyNdbYjiGC7kHJFGtEJQ96Gd5CQ2EtVNLTo1/PSuPDsSCPnpp38AINSU/GCLuhIrIT jjo37OWoiVd6NAA8KU7r9ydAHAE6jG9k60vt5CSLb/g9kzstwfpfloBZZ5CqPP4jG73/ HIDkUFExow300gbisGXj6tHSGUNH+T+5jMqGKTx7c8O54g2hYGVtDrLFLwrMzJVJt8Lb 5J104wqjKNtPfdpB/gppilne4Uh0prPD8GKtx30tTtYnZoC2oum3kqIMx1utXInrvTQ9 zBAA== X-Gm-Message-State: AOJu0YxZ/eG5pyqEThr9Wb4gxwljRcUXOLTOqfyaUo+e/VJCCDplfe35 k48X0GF0PHD3SbmUyQCm0Rtkw1hxEE8= X-Google-Smtp-Source: AGHT+IH29SvaM4IJ2/NXJnaPAeFMS+fjtgLQL7Rq48UqpO83PYLOZng2qV6FMiS0c4sL4jzOiYmTHdNxdV0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e551:b0:1c3:a4f2:7c85 with SMTP id n17-20020a170902e55100b001c3a4f27c85mr212223plf.6.1694656591946; Wed, 13 Sep 2023 18:56:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:26 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-29-seanjc@google.com> Subject: [RFC PATCH v12 28/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2145640009 X-Stat-Signature: t54gygsahq56t8wbd88etdooxpzc183b X-HE-Tag: 1694656592-934804 X-HE-Meta: U2FsdGVkX1+PvfaYKKvIWu0BrKQEBFsho1tt0SV03ukha+0x0LpMZsfuOJtFgYqb4hoGldG1lafqJc/8OgMTrTesxwBXVNkJnacZYRJ3YP7SdFsBqRAyzxbBjmP8dHUQaiP9w2wQ3eDmTFO6bC4u2TqnffOwkiDolw1aqlvxfXIj55egc7BvC4Lbn1DeZPvK9fS8fVJ0GA05o2ub1U3ps9LQdr/LaZWs7nZsRlQlutTTyzEO/NBal5JR4MXg+lFr1couLLD5xOx8URzFjP+tBxpqixjERWyerQFtTlK4XZdWpT0Vnmwg07KabvPSo94vaM9bPsBgcpbDlGoVpSnOTiz2Uz2GzqL9l+3mMdd6kozLO4Xfgs9HvV4yovYyVDtAUBTeb/+vatuYbElnOMn+2zaXCSQussyKr0m4bInhNggZPOEYgHqaaC8X0mePHwA4eUf8Lr4dnT6wBPED12FzuV3S32tnJ0UqGgbAdcTX361kAv0XnFm0+bBegmKWUc8TCYoQQ1MleHQHpmh5tteu5zHfTH+HguY4N9g9VXWMdpg0xoBuQDLiKXHhVxrfLXpVer/Quj6kwZIjlBZgVaJT0ZYyg7BUwnfqagTLb/raqivF8cj8pqCDv9vg7GVDTulwtGHszjfBQ6emNy1ylQKi7JjhV2T+xU71h4s+MMzDhDLe3YRe9TAz3fYGVUsQn7GhKmE9jPI9qvod6IXn0z2ZYFuQPqmizQ7l8GJHOof3IU8YCIrAgUpvKlTYt6jnhgzRdxnaqEdTLWeBT5MfjqqrOfp0sbxJ83F3du7Wiw53U8kU1w7beSQdcVzfVELHjDnwIPct+ehsAPdhAZDWF8U+fVMBd9biDs0ME4ZzK/3wnubXsUGIZCFsFY33SCHmapwj9TAFBzAsr3P0KzSUerRd2cE9GP6quh4z4E0vGv7+W6U61D9yF/dR0JX7NBAzhGtIqgg+DMWxtu5+rRbT/W0 O5apUKq+ xj7lNHjH/sczCCRcUg2/Z9CV0XBPfSf6AHeg85QYNFqD2G5DklPlU2hZrP1zHx8+CqeDQtIZX8sx/ezo6RNUdq2PhOhZY/C00250G82n9h4NAxMCht9/rdoBYwpcQ8ffiHs0KVUlHQWgc+etEJsDNWbGpfQpAz1mIYM53cU4kQ0dJlCI26eIrMSlra8weZUVUkTPCAhYOs/CLNBMkNEGlx5soneVLdGX1e/VL/cGP9R+Vgm6SZPCOiswP0dGyoJ4g7dpLqOz713ofaRKp0vXJgVWdq8XjoAIEKa4PO62Zc7vj+P9NkyXXisKd3M1VnniOx8pWFC/ff7TxMTh9ZYa/GFakyXV7kcb9+6SwyjIMgL41QQTiNcZpd3sxUoTOQRd0zqN/Y7k6X7GGhB7N86v4VOQG8zOVhJBo6YKudUm5hFdhkSWZj9bg+cthVr3Is70geXfxx0468tYfXpNH+yBHPGuocplPIEvtt2fy/xrfjKndX7mxbcsnQndXUW+/rqr3fxXeyXk2nqmE53L9S4CJfzEEpNDXWiYsp+BfVR4GzPV9hKmaxz1kXu48NJXUfKFbC3IojkvGwuGyX45wR4rZjyKat4dSLEcm7835dK8qLOi97JGPLEp645MvIJnSkCmyoPTMkWgNvNTbDD3raZqt70YNwsQvu0BQGViJyvQsE2oL2fpxaO9hRst1AhC8x1qenTix0hzUNPu01/0l1NVi5qjCAuw2ttY2LbAmXlisYMuT8RLjrIK//l5eKEVq/5M8fh5VHRj1DmMWdAZEbKUzQ5xhawZME17whp5bHMsDyezXqtZmBgz2wBp3cK0QFryKURWR 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: 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 112bc1da732a..7cf40aba7add 100644 --- a/tools/testing/selftests/kvm/include/ucall_common.h +++ b/tools/testing/selftests/kvm/include/ucall_common.h @@ -54,6 +54,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 Thu Sep 14 01:55:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384036 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 4C3C8EE0216 for ; Thu, 14 Sep 2023 01:56:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6192D8D000C; Wed, 13 Sep 2023 21:56:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C7A68D0001; Wed, 13 Sep 2023 21:56:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3CE708D000C; Wed, 13 Sep 2023 21:56:37 -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 1F6618D0001 for ; Wed, 13 Sep 2023 21:56:37 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EA0EDA0F2E for ; Thu, 14 Sep 2023 01:56:36 +0000 (UTC) X-FDA: 81233538792.26.C0B328E Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf03.hostedemail.com (Postfix) with ESMTP id 28F4A20008 for ; Thu, 14 Sep 2023 01:56:34 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="4hNI5/1s"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3UWgCZQYKCFoK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3UWgCZQYKCFoK62FB48GG8D6.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=1694656595; 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=6d59dJ9WRF4CIf/96OLgpi5904fNPKOKnB6AeL+fWuI=; b=3GVAFl9dMFHJOoLwqKeK8B4H87p6Pwb3BCqeg1C53YWaxOgk4g57HqIHw8TW5tUoWLNo0o kTCm51K4K6tPF5qI6VQR6N0vB11VAbf3Hmi0keXswNBDm3vRc09N7NhOBNEsdzjTKPZTvr 5iA0+5/VKHDZpWl/Q6lGeWmsKlE7xK0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="4hNI5/1s"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3UWgCZQYKCFoK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3UWgCZQYKCFoK62FB48GG8D6.4GEDAFMP-EECN24C.GJ8@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656595; a=rsa-sha256; cv=none; b=I08NedMT8rDYjHuy/9LSzpOrmw95jvv8QchgbJqs4VN1xplcUemhwWXmKtU+BmjInOZZEv 9rK9i3wuhhGSY8MDanuDB4Y49ZA0qBaOMpnpGj7ohFSz5eopkU3NKgNzUcqCAZGbbLBTmO Vyxr1UBj2twI/WSmR/yVxmOpxhb40xQ= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1c0cfc2b995so3900505ad.2 for ; Wed, 13 Sep 2023 18:56:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656594; x=1695261394; 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=6d59dJ9WRF4CIf/96OLgpi5904fNPKOKnB6AeL+fWuI=; b=4hNI5/1s6n9SoDQd4/MI8zTMh02GKVgY2HTNNcwC9yipMZ5q+Zvvl7IGVBb8Ju2PH0 +POBegji7d9kaiXKbKNx4rLM8kHi2pRFGjr7k1GrcnJMsxoYeDoz/el0TOYT0qJtba8l My8G75xgSz9PxyQLgpf3ZOL23muZqgaLCdqqvUxphx17w3tyPmcvoskroEhHYJpFDa6r ZBliDJixMNqXcCh+LnlKr4+N/x6XIjRJQuD5wwJCZl/y9roVVmgOo6oV+BVc7xZCK3RT BRfydpd/KQ1+0cE7Vg9jiJJUhojBuxZgahum4MEFJTeieyNJ/tC22Eq0rABarBZoQdSK W9Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656594; x=1695261394; 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=6d59dJ9WRF4CIf/96OLgpi5904fNPKOKnB6AeL+fWuI=; b=iKrxoNOXf1SUyacItn4jGgjDaOPruV1w0436Ozk5gL+3HzVbzJRRhbkF8uUJD0bx37 wGU5B2X7zProwxR0DaAYzp9Aa0tSYNVtsdwxk5+Ld5w0+foRDvnzyBM/M+ksseyAh/fA sWylHQ9Twf6WpvlBKea4AU9ZSLfUI1EpdOEQGq09AO/HAYWpDCwlU24wB8X5f6dqXcR/ ZXWsJ0rNftioXhx4oESzmVyTEPU6/nLjNy0l88qjKttW2bFxa31vcmKyVk7yCE6/X8Dd O/2ykg8ueUVp/OYV1LJ0msRis5wUotbWOpbi3FQFWouJUmOtYbBo/49B6WkZpPwxcJCM avVw== X-Gm-Message-State: AOJu0YzYSNe1iS/9S0sME1hPPkHDTLPBWzc4fWmVbbWKHgdQtMmk3RCm h41mwJHsfL5bDKDAEADH8Zp9nAHb6F4= X-Google-Smtp-Source: AGHT+IFzfhVkixKoPQtGcLMCaK6G8PArrix4K5c3tLhejBjfYUO+G8+9tOF2VAFi/BYmIBrSELG28dy7Zc8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c701:b0:1bf:4e9d:8fc9 with SMTP id p1-20020a170902c70100b001bf4e9d8fc9mr148367plp.11.1694656593996; Wed, 13 Sep 2023 18:56:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:27 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-30-seanjc@google.com> Subject: [RFC PATCH v12 29/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: 28F4A20008 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: qw5sxdspu3u8wmyconb9j9h7rbmoeaty X-HE-Tag: 1694656594-532996 X-HE-Meta: U2FsdGVkX19kGB39pS0X7eXgm/GVv2wY93nDqxhvqdw29INiOzpQpj3IQpk/XmdBW1+37Y/1POZvOZuca/0HI16oE7TIBN+aDIB5jubrGBzsoAeTbiPYSWNYP/7dPiKG2qqIlZF36QopC0hjgpjnSJuUbFkBT2iUeP2zrKKo+jlXKFTqnYQXdEF1P46XeYtTqOe6ToxIIJdZI9TovRD+rBcS3gJjFtbF0XKTCak7bRpPKd9wLTTtN2nlDjHZAOiaeFhCnPZiZZXhrpjva1GjUybgnNzAHMPeRNTohBvneC9GjZpeJ9WXq1+Ozs8LyFP8hOyWlAs0Fm9ioozy+ymF4kTAnG6vSLx/O/tDPiXIgaTcsdJDkKt7aFX7K6YBVer+E8EIfTHHBPT+SUbCx0lHrFX9GU1fwUA2h9AbzSef1EUjr/DXSqjED8UDS8ez32rXBqxh50RhdG1s5F7U6JUuntuY6t2REZUY1DH2BCkrLT3QfEz/2HvR2WDuGHlgBOcd3Pbdv9nMAGjEBShgXbktGFhVhSVmDnYfnOlrCEIhvIa1uVzzAjtgzZwmKh5r+of6+/Ou5IyWkYArxXxa8qokTCQWfDaOZiGwZMvw7m1qSkSZvqW7kXqFMv8kNxk0VqPru/Ef3tNiwwwp4hyvV12+tLzuBw88hoqLYySIZItF/tczM99ODCDhZnI7YmIYTFy/GE7mqCtRYmOTuzX86gMoglG/XDiGARdOdDea9lXUINVzaR0JB/8qVkgevF6g0PeRJNdClBCrASQFlnQJiulY8i+vStDgs47eVlIVNttErNBAuDbeJ3MNtA7UmGaPHoP2RsVexbsp6LuiUGjwfMEFbRVVoIDfNEd0LoJezME3c7JmpFBCSvGKxbFMLU3iXMoSXInJc0jH7yLJ9nYg0yesdMwh9II7y5zvNZFehXqKhUXUNrDfxZwSmecpApud8AuvaW1Rb1CVjE9fCZF8Hg7 2xI3p36n uI1J61D6ZBdJvOwR3YfXpbCWi+VKln1pmmiH39AhRC3CwKblwCy4vlFFhc0XiC9TrqwWreXdsDQJjliBiyp+t2woI3UM3hQTYZ2FP89K2HJG4VAHmoWqxA9LZxuirBSoIzaihnyu85snlj38/hpl931ejBYigO8+IHmQZhf0dqR6wVbqSELO4lfPpMCDa1UivdV0oJ+oDU13irdiiZxHVNZqeFUjNogskMWc/LaGFbJZEbV+k7f7ooZ0s2JKXfUI2gYRssSNJ36WLkngj+xWyhHuktLssmFAZ4eb+/WhgOr6XZsLXiIXL7t06zEgJ2xrAqmCCIDHJQ9wAar/AcgcYx97x1+zmiHksmVo/8PMLMV073OUfTJkwIGYcjotuQWy7cuPDsmGL091bjRHe96q/P95YF8nPjzuJvVjKAyBaYlUf0iHnEYRY+hLgNZORqF8tHqyOxVHkz3MheUTqNdTiiqLIgdnr7YFuuIBPzV3c9SCwPjEGR+paLXnwtCebrzyOQG/zuGwx+7HTgV9J4gWhqvDsi4RuRv53QYmgW0yMxMUCkBQOivVQpLw/vk64Eg9taILijIQ31sMif/+Nh2dU4cAYx/ra43x2Dtu10lCDHSutDhrwGAF4+je2bVjXRvPpb0YINmzV8l9sZocHvyjgXYo+jAJ3rlgeqhvkA95XJTlwkpinPL7BebxmnvARriRswREUk6F+OGv0EStI5IZKT/mub8aVyfkuMuVLSnW8woCUuEWPjrTB6WMUVPOJ+u3rTL5K 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: 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. 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 | 410 ++++++++++++++++++ 2 files changed, 411 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..50541246d6fd --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -0,0 +1,410 @@ +// 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, \ + "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, uint8_t pattern, size_t size) +{ + size_t i; + + for (i = 0; i < size; i++) + TEST_ASSERT(mem[i] == pattern, + "Expected 0x%x at offset %lu, got 0x%x", + pattern, 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_SHARED BIT(0) +#define MAP_GPA_DO_FALLOCATE BIT(1) + +static void guest_map_mem(uint64_t gpa, uint64_t size, bool map_shared, + bool do_fallocate) +{ + uint64_t flags = 0; + + 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); +} + +static void guest_run_test(uint64_t base_gpa, bool do_fallocate) +{ + struct { + uint64_t offset; + uint64_t size; + uint8_t pattern; + } stages[] = { + 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), + }; + const uint8_t init_p = 0xcc; + uint64_t j; + int i; + + /* Memory should be shared by default. */ + memset((void *)base_gpa, ~init_p, PER_CPU_DATA_SIZE); + guest_sync_shared(base_gpa, PER_CPU_DATA_SIZE, (uint8_t)~init_p, init_p); + memcmp_g(base_gpa, init_p, PER_CPU_DATA_SIZE); + + for (i = 0; i < ARRAY_SIZE(stages); i++) { + uint64_t gpa = base_gpa + stages[i].offset; + uint64_t size = stages[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_code(uint64_t base_gpa) +{ + /* + * Run everything twice, with and without doing fallocate() on the + * guest_memfd backing when converting between shared and private. + */ + guest_run_test(base_gpa, false); + guest_run_test(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 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); + + 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: { + uint8_t *hva = addr_gpa2hva(vm, uc.args[1]); + uint64_t size = uc.args[2]; + + TEST_ASSERT(uc.args[0] == SYNC_SHARED || + uc.args[0] == SYNC_PRIVATE, + "Unknown sync command '%ld'", uc.args[0]); + + /* In all cases, the host should observe the shared data. */ + memcmp_h(hva, uc.args[3], size); + + /* For shared, write the new pattern to guest memory. */ + if (uc.args[0] == SYNC_SHARED) + memset(hva, uc.args[4], size); + 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 size = align_up(PER_CPU_DATA_SIZE, get_backing_src_pagesz(src_type)); + const size_t memfd_size = size * nr_vcpus; + struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; + pthread_t threads[KVM_MAX_VCPUS]; + uint64_t gmem_flags; + struct kvm_vm *vm; + int memfd, i, r; + + const struct vm_shape shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_SW_PROTECTED_VM, + }; + + 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)) + gmem_flags = KVM_GUEST_MEMFD_ALLOW_HUGEPAGE; + else + gmem_flags = 0; + memfd = vm_create_guest_memfd(vm, memfd_size, gmem_flags); + + for (i = 0; i < nr_memslots; i++) + vm_mem_add(vm, src_type, BASE_DATA_GPA + size * i, + BASE_DATA_SLOT + i, size / vm->page_size, + KVM_MEM_PRIVATE, memfd, size * i); + + for (i = 0; i < nr_vcpus; i++) { + uint64_t gpa = BASE_DATA_GPA + i * size; + + vcpu_args_set(vcpus[i], 1, gpa); + + virt_map(vm, gpa, gpa, 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] [-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: use multiple memslots (default: 1)"); + puts(""); +} + +int main(int argc, char *argv[]) +{ + enum vm_mem_backing_src_type src_type = DEFAULT_VM_MEM_SRC; + bool use_multiple_memslots = false; + uint32_t nr_vcpus = 1; + uint32_t nr_memslots; + int opt; + + TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); + TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXIT_HYPERCALL)); + TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); + + while ((opt = getopt(argc, argv, "hms: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': + use_multiple_memslots = true; + break; + case 'h': + default: + usage(argv[0]); + exit(0); + } + } + + nr_memslots = use_multiple_memslots ? nr_vcpus : 1; + + test_mem_conversions(src_type, nr_vcpus, nr_memslots); + + return 0; +} From patchwork Thu Sep 14 01:55:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384037 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 C25EDEE0219 for ; Thu, 14 Sep 2023 01:57:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 076F58D000D; Wed, 13 Sep 2023 21:56:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 025C18D0001; Wed, 13 Sep 2023 21:56:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D959D8D000D; Wed, 13 Sep 2023 21:56:38 -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 C27558D0001 for ; Wed, 13 Sep 2023 21:56:38 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9BAF7B42AA for ; Thu, 14 Sep 2023 01:56:38 +0000 (UTC) X-FDA: 81233538876.18.8A4E85A Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf13.hostedemail.com (Postfix) with ESMTP id D0D0420023 for ; Thu, 14 Sep 2023 01:56:36 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="WeKc41/R"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3VGgCZQYKCF0N95IE7BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VGgCZQYKCF0N95IE7BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656596; 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=SX/tmvtGQjfAKzRzM0R+P9Auz7l4k5LFyR/Je4f49mI=; b=U8aXR0yKGqWGgkpfNHKiNsCDcP80Qbo2wk094OXs88c3YaSSSgFMwwOb1cL54pwAH9FaqU hlqg/3I52QrX7nbgTRYQtAos80jp2reskOczY5l55TB95wlyM7yG+DR56lCt9nIOCEti+i FsPkYAhyL9aP5R/hlEfJ95lbRMddEQs= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="WeKc41/R"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 3VGgCZQYKCF0N95IE7BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VGgCZQYKCF0N95IE7BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656596; a=rsa-sha256; cv=none; b=DQ5vC6XcnG8kIaBvPqDk9Spi9dNE5Vqdk7VCuhzDcDOlROkVchVadceYDc1rplFU0zlyzG nmXKYdtr+pgwtL+JF4fLIbCWvoBIMnpTJNvYmmRSFwhND0JhOVJ3Py2dkQqR8swWL4J0QB eaVFBl8YLW5Y+sVpkpNk1vxqhNLx4c8= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59b59e1ac70so6842807b3.1 for ; Wed, 13 Sep 2023 18:56:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656596; x=1695261396; 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=SX/tmvtGQjfAKzRzM0R+P9Auz7l4k5LFyR/Je4f49mI=; b=WeKc41/RNsGzwSx5ao0iNVplGJPtfwUvl3f4LC9eHdxCUpQBHHWxejQkfGIlWN0l5z 0d/rbTEn1Gnr6T+2W4A7+3hLC766b/J9R6H/dp0x6b65zTvrFsMUuaFgEyKL2MgaumhQ MuGCFZf7UcR0P2TWnKl68b1RREWy+ZlF9RiShip8QcuxAJwMvEiJwujjTjBfMsw20w51 i2d3nUK4OvLPcJ4YHP/aeWjlt3phxhJPAu8uMetMc4xiZBwW3UZbBMem4jdZTwMn9l2u y8mZcEcdQ1f1I2Mi5Wak9W6zcVU+Ch2eqU4CvbQqXIZ36znG6rtAU0tlmYHeUpAherc3 xYQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656596; x=1695261396; 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=SX/tmvtGQjfAKzRzM0R+P9Auz7l4k5LFyR/Je4f49mI=; b=oTMcEBTS69du7xhf349ZrJXRotfizxi4sjHygt1MKJcNnDVQ+w+MpOLAzLIuDKwPG+ wG2SzQxj1FQ+j8GeHUNECKeW5/6vktU+e0ZnpK4NBAx78eB87Qdh+G/vBNl1AOgcx5cw FlA8DPD0IPftCABHJrKrMeiEle6osrT7vE3bp90xu1pi6E0Id8+W0+wALb/gXt4CTHCK px8Wfu5PWfLwHnEcWOUK3hFz6+8A78q2RuyYm1dofrIyMdr3pa3LIGFyN76qKgdUirAI AoNvFZoECSz3/iq6CRBE2mllWQhwfql7HqkZf2QlWJEX5tr8o/xz1hh/hPS2MLA0WRGA J0GA== X-Gm-Message-State: AOJu0YwrOzCOyWVKzPTemDzQBSLALiNYY90uaHSHU0Ey48q5vtq6fkPv 5lEmrSrIcaFrz2OGa3/2QuHwiWcXfAo= X-Google-Smtp-Source: AGHT+IHERRdQQcWN7mkCNgIgj9nlxuSe44izkSe/zW2QZGFwDrBBBoEw/nSNmj2b/8hHFzwPazIBLLDdPVQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:905:b0:d64:f7ec:6d5d with SMTP id bu5-20020a056902090500b00d64f7ec6d5dmr96462ybb.10.1694656596047; Wed, 13 Sep 2023 18:56:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:28 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-31-seanjc@google.com> Subject: [RFC PATCH v12 30/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D0D0420023 X-Stat-Signature: g8qu4ydyu3tj1tna3uszdetcc4ydqws9 X-HE-Tag: 1694656596-420293 X-HE-Meta: U2FsdGVkX19E94BQMU10H6RyplYdubFCdiTrM6T9qbGlPEbfaEH7Mn6TbfKb/iYHdxN78Pu3HQ4hq7aAKOHa2EIEX+bIRw7zTzfXtU2EPhDOcZ930+22Q0eAEsgn+4GFCajToRSm1djLEP7Qpv+evB4A1y7ccO/wmkzMIsm6a8OoyBM/c+U1mx/hgPcPOVl4VtQlVSnobG5RFQ6AQ5r+9Y4Wa+cQwqxmC9q6NBhhlSUcH1MjxieQukyAOyPUG/D1C8rFkwfouIwWUqnJb/KBmJmBrSGv73qMjYXn15QWSNtOQC45UFZpqj1B2OtmTMuf9tokNC3KX5btNICrjRBx89gxVzYvv4zdj8NXEjJdVdSRXwaAVsizoL37MB6mIhg1Gps131Y60noaR7icdOEa2tJg/FUNFqDbh7dnlaUJOGNLxqkBv5P0IYp/NbXMrps6wHrrELaJO6fjwKGb9h/YHM4AwpDd0ApouwH+MIIclznwYlhradiPXDmp4OVAZGWZCgUkMqtvEpu0W7PSqr4uBNuBCux0Fc+QfPv58lfnJE2eRAEeOmX1QUWHHEmjqydZiY8ankbPus2YjdLj3iFCJNWtkhxfAi5rm5/zWiasPHx2HFQc0hngzvUNuQSWKoGOLNaNtLmIssfvQ9KnGTNknKc9INHz6mV5Lgx6diiSSF0aYazXDEJhn4fZPjz9nQOXlSwfZ6L4BEVY+mPcyob5jj7CnF8SbPdd12DfcOeaJWr3XoaLj5UYs3Yz79xdJinC+6cM3F5DAn70tBXkSaV9kjb1Sxyf5WULSkWkd2LuR6rzLEQoZo/0+8kyjQ+aAj34HlbJS96w/Hvw1sP7Q9RU+Iybyt+UZZIfPY8G1VQNt9sPR1NEjCQ5rdpZ9MLzjmFwi5cbfXi2jJVK5/KjiU8QLmaXZ0Xe3BQH8Dadso3TVqQ+UPlznhxuEQkrzgiZ9SR6mRO/dx4LOjEB7YF6JjH NH8pqMHQ SS02bAOOvAZ4yhvKcxw9nd6xBZW2XETXFgn1h2jbEwKBfVjOBfprWeyon/+4pQj6Qyq0WK4T3wOVeo7xGu4aDesiBvQCHgHtD6fMqHCLxa/jdqhREpwi07bmLTQQX+uBHYCWPo4y8N9ngoLr0gfw+c2fw8Z7lO4sf+TfzDVvOmG4Iuk4Dh0u7X3s1llz/Wb1DsRx6IlLABrCgd+91EocZLBTzKgTj3l4KwIIW8Jc0ZQfKlC2zKN4q55FcL9Nky2B5jOlXtsXnYnBmwGoo/0cXHtb5G6n7zPlhaOtTw+eLA9X19wPojHYXr7NHf5MbznpJL7Ib7ta5vFQNVSfKI8tTSyhuCHaDjUs8RdiM7vjpFpx73SvFzffkFy3FYUZ69TwpUjRTQcrnFbUe7KmcCa7V4wqk4L8TdI2DNtZzvrRYs4z52OCrcKcj1R2wKMjwpY1wFJen+WzwasnpA3+xQhzbfWdZiG5TQidjPIbzeADTl8Z6ZlfpusYgenU9caE3v8SvJaBCAhfVlQG5O+Tg1pneRVLmwe9eyB/RzHiLGc0NAjqkKiPKC8MzQ0kvoTmUI0GojyuFy50SSDp9oW4qnSJyCulEQubFEta4DI92dKS+kIcMaR/ye6GZF8qt4wacft0/GesvFtlUd8ifWTvUVG0wp5hGlPiqeS7fyGuW+W6dDnfUs7A9m6z0RisYC1MA4TDS/htIi7LumzKGVCteSa6xzd/oz345x6773STcF07jUG6UKbKZEuzK8AhCsbhAsFrkejKFLhPdxNi3xS79gsZ0mDTu2BWwk/nyjH+dY1a5/zLNi2rbv1jhRMhCBdkmZeM3kWZWLNAk0hPuB6tJ1YfoXrPE0Q== 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: 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 --- .../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 b608fbb832d5..edc0f380acc0 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 gmem_fd, uint64_t gmem_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 gmem_fd, uint64_t gmem_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 68afea10b469..8fc70c021c1c 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 gmem_fd, uint64_t gmem_offset) +{ + struct kvm_userspace_memory_region2 region = { + .slot = slot, + .flags = flags, + .guest_phys_addr = gpa, + .memory_size = size, + .userspace_addr = (uintptr_t)hva, + .gmem_fd = gmem_fd, + .gmem_offset = gmem_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 gmem_fd, uint64_t gmem_offset) +{ + int ret = __vm_set_user_memory_region2(vm, slot, flags, gpa, size, hva, + gmem_fd, gmem_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 Thu Sep 14 01:55:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384038 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 84FBBEE0203 for ; Thu, 14 Sep 2023 01:57:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA8F88D000E; Wed, 13 Sep 2023 21:56:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E58FE8D0001; Wed, 13 Sep 2023 21:56:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CCE4E8D000E; Wed, 13 Sep 2023 21:56:40 -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 B8D098D0001 for ; Wed, 13 Sep 2023 21:56:40 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 97E4C1CABF9 for ; Thu, 14 Sep 2023 01:56:40 +0000 (UTC) X-FDA: 81233538960.10.235C4AD Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf26.hostedemail.com (Postfix) with ESMTP id C214F14000E for ; Thu, 14 Sep 2023 01:56:38 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jeSNNMeO; spf=pass (imf26.hostedemail.com: domain of 3VWgCZQYKCF4OA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VWgCZQYKCF4OA6JF8CKKCHA.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=1694656598; 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=mhn7+u5bXSh3DxSOzG9/ewBd6nzzrjcseQEqhhMfZJw=; b=bIkje3ah0kLP5JlzcEuwCfC/1FzlT+FtEy+S+2437bBFHaG7BoT+0zA4GXvwF1BgwzowdD +9Mdiu635PKwy3bjCn/5ig82D2ljbeL+4vPRQEga0JkjnaVtykP+0Vk3yp4iWNmgxREwFc E1V45zoqhwGQwrLu88GPJm4LW54x4ww= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656598; a=rsa-sha256; cv=none; b=UjR8dIrqp+Dz6oo43boxeBXBsUF5hjz43kJvKH4a2zrHmeSKMLalKZJA6mRDQ3WJKfvuoR UN/ocTJR0kNHL0k6KWTOrTwcNnXErpD0QxvjeLiOkSIyUox6+OG0VA0QCCLzVdKR60UkeP VL9p7swwxm7tJYGcW/e7nqQL5X94/oQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jeSNNMeO; spf=pass (imf26.hostedemail.com: domain of 3VWgCZQYKCF4OA6JF8CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3VWgCZQYKCF4OA6JF8CKKCHA.8KIHEJQT-IIGR68G.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-59bee08c13aso2140047b3.0 for ; Wed, 13 Sep 2023 18:56:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656598; x=1695261398; 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=mhn7+u5bXSh3DxSOzG9/ewBd6nzzrjcseQEqhhMfZJw=; b=jeSNNMeOwPDEI439ZkaD8Veh+Zzf3ivDDCOkpxBAoIDHbv4+al/Nr4qA9oLMXXK3mC D1BO+rQTupV8q284zWFblIeQNUzdFSmhbRp+kr3HDRXF+SWZIAZuWvGVDHdXbOKz9kBL 3Vo/77BA/tZOZ73I2m/6FWG+QzOZRMxHeeYtqx7mnRIYgoog3ta8N9Yyww5tfVYNKqW5 kdbsVn4MOrCtzcN6SCWbMqCGCHEorSofL0cyWtDNq3EkAJ2t7oxmc/aD3l9eROCSX/EW HbwCptTrXIXdxgYZtJneTbBK16wg4ZUFQ0XBY7xDxgIhfBImeUn+OCKU6HQgREYXxFsu pXPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656598; x=1695261398; 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=mhn7+u5bXSh3DxSOzG9/ewBd6nzzrjcseQEqhhMfZJw=; b=jn3J4WcZ8LBtDalFLlJ6HfJUpz8baNLm1+kgFBcK51OztDzAMp6OlVTdHE5YzMr+1l vwOmS4xWXYCoosqnhkyhl2Dmaf6TCw9/MdZSVdIVwEIrPBC39hqd+LH2Wr7IeAhDCfKa l39W2MRaNb0Q71t1ccQicYQ1QGztgGP6Pm5HhMLrEtap/p/z1qrqcEHshiVClFfKA+Uq B8VsI8v4GSHi4zyuz8e9GLbR9OZwykNmE292hmHAK/dgfQoSxuySKr7yy/q6ElcyflzS bHcCvP4B0HOr2ojhj1riq6cBWXOZ/LypZKawOERDNpP23qmjowNXSrh220NXMbCmZNey dErA== X-Gm-Message-State: AOJu0YwL/Xpvgq4dzL8mWF1Bxjbqp6swLTk/f6Qg0dc/2d4EUGdlk0n8 4lh4Rq81J3k97BIqWIv9yieMsVN9QNI= X-Google-Smtp-Source: AGHT+IECmRUJsED8Us3qWlJnugvslNzA7dXsGOQfEOYZbUPHi7DQFFf33qpMB4ILXjFkyWuOYhA2SvRB6XQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:e809:0:b0:59b:ebe0:9fcd with SMTP id a9-20020a81e809000000b0059bebe09fcdmr13568ywm.7.1694656597965; Wed, 13 Sep 2023 18:56:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:29 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-32-seanjc@google.com> Subject: [RFC PATCH v12 31/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: C214F14000E X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: uq6boyydeorkhswbxtu4oug93w95p9ma X-HE-Tag: 1694656598-814801 X-HE-Meta: U2FsdGVkX19q4dwtUyPn8i8WQTpPdKLp1zEGzH5C4YXhAiafQLcXslbvsWsaamxny60g+xKpZdjzQQH3l3uobmB1KDMA3I2ZQ83dYUEo4tz8TY+nWf7NjsK33AF7rHZbkO0llsySLOroprFtHeDvEerVfr7iLK3sN4ZqljilyFx2HTRTKOfmSfPNQlUYaDPoS+d8FQZ2cC8YNP4kQQqd3VVQAEYqP7MCqCqzS5ycqpEdUfgKvpEFN4TJFublksF+FaDcWJ/vWE3E3Y5Xk45kUs9xFuCcN20FO1zHDhNuxuzQx3n+fzbqQNg1QwpR2InEkH7YRqn/ja8B1ZSxGRPeapYmZsdnfn9R6EtugPF+HAckRtuNrw0Druj/4sOOj4cT6Oom14aJfcV0VBD05/kTERJfp3LmuijiUUytbVBDr1/Vog8/pT9l9xBdHgEzI6gSUc8Vz/HKo3rCeGEtxZlyoLzd9jqjYL6iv5Wy6hfaBD5q0f0G4igfRKJIHBVxDjZPOEQ7ZsBdimGl/gPLa3DI6NJW5HUn3I9qS13JGeNDB7YD+r5dzvFwJXs3WDK3XSuuY4Iu66/NkRnqv/hGiFEb9/k+39GT0Y4tMigZoeJQDxBCKBLmdmCaxvZod9rjzFW+bhG/DtkqT3Ewvwkd81wX6Q81sORUm2SpE1SBUld5dPt8M8KwG7wX00fwB4gMTr0bPxm5fuSen3CIJyWzaADN3Mzve8LIjFRgkOLott9oT1KEs5n31cTRpgXh3CooxPtGE40lzX3ecIijrDO4kwEg1jGXVeqgnCPb74z0PZ2PAi7f4u7DTPsGhOUI/Z2XvoPtYTVKTvVR6JxkDh0jwiqRLt7Z1j8xhw3WEC0qZQjrwgWHKJLLNomrLdI9wIP4bQCrRYWR8KroY7FYhS+PnHToaBMh4htHAKQan+tQyu0Y3ZttNUNpGD6MOjAxK3GDf4262i3Oh1MBVHLbd5/lDuX PnfI2Ic5 xZ4ZnPpJ9CfvkCLIkcT8OcjvEXVNJECyDmMak2kkcKRjVUhnBTelWF8RpxGrlaOt6L2PG/fBLrQ2l9zJ4DX/+94FJXcxNDuXdW02j1bnMzXKjki+jF81D8XMDxAr8JDEFf2V58Sukq3EIy2UrErRjceqXpNU/mHjQD9oQ14H/x/ynoncrQzgLby7a408ul5+87K7VaV8eBctAuMrr5XcdWs63N1+9ybTb0LHmx3Yt7yRRx3k+mIQQrG6M9P7cqw5JvVCNSkzc0viGZGyVJbEHdwfJF5ZfwgXa1FSBpl13D4sITwAfA9Q6xJg03tTqyVrNc/CNNktqvvIp6JV7YCGIGA4aurlGITPu2Soz4u68rdworD2UQf7n5AxKtONouVtrxR6Uz7GsBRLw6r0K7vuooFjC7M9p32nkY05c/GuChQw3gnOq9f+xerI1ObX5sHhJS+/M56/dtiOvpehCiJOv00f5XSO/D7xKWRZR2YvtMxr66LbZQocJdhhlX+bbe/Hukc4r1a4WkgPAHijMjgav79wiAIk+KqfjU3/GwXfUMV4f9EhnQ4cNVzKkduLo6Dh8MiEBdICqxoEKQMi1C+nbji5vkrRtpiPC0fbpPAvem4ezxtMBeK7LXyMjlbuk6eNJGnGrGrHnVvOPeHKD0WRQNPVDvyF3Z9eEc51vpzYNQ9DGCh27G9yIJhWjNaYdybulyKDt39bUAki+DS6s0ahELJuse2a5ffu8pVVqjQqAoG5oUg4E7Yq7HzzhwICRYr7RMezJp7cuhuQC9fViDvr6mPYm9SNZyJ36GNZ+OYbxVxmFcG9/ngflyQa1dql0ZeszG6dWGZfD8QqTh3bBg2qrNCmMrw== 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: 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 edc0f380acc0..ac9356108df6 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 Thu Sep 14 01:55:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384039 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 43442EE021A for ; Thu, 14 Sep 2023 01:57:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6EB38D000F; Wed, 13 Sep 2023 21:56:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D200E8D0001; Wed, 13 Sep 2023 21:56:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6FB78D000F; Wed, 13 Sep 2023 21:56:42 -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 A0A0B8D0001 for ; Wed, 13 Sep 2023 21:56:42 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7E5301C988A for ; Thu, 14 Sep 2023 01:56:42 +0000 (UTC) X-FDA: 81233539044.21.1BDCE84 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf02.hostedemail.com (Postfix) with ESMTP id AB27880007 for ; Thu, 14 Sep 2023 01:56:40 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O8afp114; spf=pass (imf02.hostedemail.com: domain of 3V2gCZQYKCGAQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3V2gCZQYKCGAQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656600; a=rsa-sha256; cv=none; b=0aPn3eqqyi7kTh5uy60oM/RQOh+5aaTI1tItkGaxhNGkS2hT14g0U5iiRC6KC+GtQbZ/my M106m9d3mMbUDYfkhFq2WCiM331Z0oUtDKrgwjPrq8kIyFrBKcJWTiwm10UGqv6KIKXS+W CfOSSOmD07P/emOssFkSkgXObyIgMao= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O8afp114; spf=pass (imf02.hostedemail.com: domain of 3V2gCZQYKCGAQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3V2gCZQYKCGAQC8LHAEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694656600; 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=i91JvGzfQg7A4MbSmUGyo4+8+JqUux5ifD59BoUsadA=; b=HEb/Grp4ABCkC1pOexN5SsOJgPppuSrdDkDibVkDt0P4UZARBfvYZzwpd8uIB4PhX/sVSR R6Gh0Rwt/WbC/ZCmXGr3fpubwmmcrF2Krv67D/ltU0pgxy5e2M39tlGC88WKoqGRI3WWfl 6/ajBxQoVRKd/3tYuiF5ftLGJMzOf98= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-59beb3a8291so3976617b3.1 for ; Wed, 13 Sep 2023 18:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656600; x=1695261400; 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=i91JvGzfQg7A4MbSmUGyo4+8+JqUux5ifD59BoUsadA=; b=O8afp114x8/7Z3hV+O6wMLCkihDQpUbqrxbYmpskXN2SW0V4flc1/B2gEB+SCCWhmK aK2qE6JZh/L2RmYBvoSRt8RzUDSJeK/ISsUch2JJT7D0Pr0lUbfp5RF6AZ6DSeSx4L3X 0CfpWa3kogr66TbpVu53T9n6s1tvrqbn5lys5T6zznrJ9/ahk4/ZZpfvor+R2iXN3Zw+ ZwiQHQXall7nLwrI4cYvX786Tt8/vvT7Z7CJydnTQqm4VMKnUeymFQRmn/mrwgYHfVLM knriV/gZb017L0RJSkCpgB/5xjKS+11/XknXm7T5ePGWf5KspI17VHaR9h3KHUNsqwz2 /IWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656600; x=1695261400; 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=i91JvGzfQg7A4MbSmUGyo4+8+JqUux5ifD59BoUsadA=; b=EstnEpfPeVl48c7z1DPTWlSI9U+Ko65QvpDLt2+pWRAjI/MOYH2AosXViPN0vyiVDs oyuIcj8Z7fQlJjkxlENyBxllmkZZSCdfv0/+Xm8oyWdA0Y9yak7MkMfJjRZSl+4UojWQ n03brAkbOJyWwy+9Stillu0UwmoRAYS/B9XVJKqZYENm9pfJOyNetlQ/QUaD/nIZ0DuO m3ebxWJ8ksbme8FwyZudNnWMio+Zk+OfSPHYANTV5/C/csUokLYkYEMW3/Pg1v1bHIRK yzhk99xvrOKxhREWs+nnrC7sLIUDr93VJaSQ/ihmAjzUTRwa1/1KI22Ntr8tho2HGnNK VhsQ== X-Gm-Message-State: AOJu0YzDeu45qS2B/UfUsx+Vw0xESXP1948MWi2HcT1vbjfeKQiclOz4 OWcqoRJrJjLGq5EJxn95F6wrTpv+KZU= X-Google-Smtp-Source: AGHT+IEvLzTJgrzr2NLiV8Z9En5+JUJxc1EswgYLaEL3Jr6eWhED9/CQgA0jS1j6Htu+3uQxxbNwCvj4oqA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:400c:0:b0:589:a855:7af with SMTP id l12-20020a81400c000000b00589a85507afmr107432ywn.7.1694656599914; Wed, 13 Sep 2023 18:56:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:30 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-33-seanjc@google.com> Subject: [RFC PATCH v12 32/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: AB27880007 X-Stat-Signature: wwx7m716wq5dhc5gms5yzjsp9uaxz93t X-Rspam-User: X-HE-Tag: 1694656600-537783 X-HE-Meta: U2FsdGVkX1/1UdPVfZptmxc9cAQPj0EjaYXvjTlxQj0VJOOx6tHMepQovlDjyhsuZUybBvg2bySqTfro7yBStoqRTl7hzVym/e+uqci7SMd8QN17jYXvLAldEy8FwdN5DI4EFbt5Pl6p3fWs8PWhKVKMpueC9weOXfgxiQ0QONExUwuJxWbsU/vrtn6DN+kkcpY9ImyCKv4FTesUIX8T+8O7vCvnZVBldTsuUEet7uiozDhaWcMMmOYPIoGIHmpB3IdABANy38PfjvkTQUOfMFrg7urrI28mMSuLf6XguY/hWdGoI4nUoCmCA2UnAnGnf1KHEKyfExOuXZ1/PhEEfnVbsU2+40defsq22QgixZhOdo3vpfp76pcP+gwUeTtOJC2K5hkUDqM0nRxdN86aTbLWJa6EFUJESRA0vyXIj1289Yw1B9rKrn3fYA+UPlBUFBwwWIYdXwS8uVgAZGNZAsPLAcUMGSSqqyLP7+8vh5ZKzHIctFAuPa+TrHAXfEBSPiMiMxGhiWcETZXBuBVhcpuRQRy8vWpU9kE2KlPu3syLFxkoe7USfGVnC/JcjKg+l38UVj34zePDdjITS+BC2P3SPsDz6bRzYkignE7BxjuulWdjtqbjymaCZG0kWBt7rUGwVYnC2Ct7hytivMGF7I8mrkg3ZZPdSz03HdhlpoUMrtVjCsYlAvvBWwz6UTeV5NuMVkLqc/4SPsSazK/RdTeA+xmEKBzFQrjC8SFMRn4ezyazyhpyOLtDbwlkVUo22AlhjWQV0BpUatlrGL2UYyDdSdR6AO32ZOuIdO4Av98Tf9QtstT3/Glacq+2Kd71+xHpKgW5DFP6emUBEFC4WJLJROYl0nPXRpuhdsXI9MBUvrCBbmPYr9UXEXlbb57ptOOhepFs13RsOAFPS+KlKstvWlJgKt2+aH34FNH/CGnpnksTec+xLcl/rre4YgZ8m2lXx0OtRozNoDFzdQO +2M78qXv PUoI0kJC4U0tBrbgRzDowbnXvhZR79hIECE0B3kqF0ZHBK/tbtdJ+xJYUDg+Lk8LvZqp7KniYkJxRNJxGRdHkA/l708WA0S+thilUz95F0dnfll4p9t3NWBd5ybIhf8og98QYX8X5JGtrRL/AZqpPUrZruf5Hmr02thInvbQg9Ai3u4QclXAVYN9/1reUBZJVdx6GPvxXabojtbLxm3ckQohbIpQqk6iuWQ0DC8pRE8JzgO5e5vLpdwB7O32xjPzUOLgNtzGNvO8S4lRSu1uPlAzAeQZM5k0ptFTVoXpioJp4yMvdJn8u5GQ/mPYqiNLA4pCzJhPi7Ve6R+CfCDdQbq56G3nPAG05LrSq8fKLeWOHZR98JcaO+P9IckBSvMYGbHk4oju9PzyLEuULgAufaeegviiv0Jwgd6/tgL4jSv7dRVrE1ZWSLr0AK1lfZ2/ptIlBR0hJDtdppr3vf1tzrm4x2rTu0aDWWkWVy6sy3gX/qV7TektKKKQ0rIe0ceucPJWBU+ty+AlE/dhwekGfVoeHFBIWZD83Fu4uujtVAdCSBJha4QRAkET8GJut6bfgG51uuJHIpxp+U2+j6BsNAJ0snCtnp2veoilqap6pHO4T0/5DYvEEgju+bwpe19Z0hobuxFKB02fVYEHPyGwR8QwUJRLwug5j32MBOXuB8XFz1tBN7fMYUM9tHtaxfrvB9EPfD+SdXs+BNPy17ROaR7hDQWzyrvdzmDne7KhmnXUI+vQmL38TYIVulcQNnymbBVKo/D0O1k2mguYKtWuPDGLH358MUdeYK/7RT3ceL0yACoeieVhaCTGq41LXkYmo+UAkkgGD4LNHprv8DPpx+y+LMg== 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: 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 Signed-off-by: Chao Peng 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 + .../testing/selftests/kvm/guest_memfd_test.c | 165 ++++++++++++++++++ 2 files changed, 166 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..75073645aaa1 --- /dev/null +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -0,0 +1,165 @@ +// 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_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)); + } + } +} + + +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); + + 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); + + close(fd); +} From patchwork Thu Sep 14 01:55:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13384040 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 7A0E9EE0212 for ; Thu, 14 Sep 2023 01:57:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04C5C8D0010; Wed, 13 Sep 2023 21:56:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 008288D0001; Wed, 13 Sep 2023 21:56:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6B138D0010; Wed, 13 Sep 2023 21:56:44 -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 C00D58D0001 for ; Wed, 13 Sep 2023 21:56:44 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 96328140EBC for ; Thu, 14 Sep 2023 01:56:44 +0000 (UTC) X-FDA: 81233539128.12.5E5DBD1 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf19.hostedemail.com (Postfix) with ESMTP id CF4161A0003 for ; Thu, 14 Sep 2023 01:56:42 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=r6dzU3bZ; spf=pass (imf19.hostedemail.com: domain of 3WmgCZQYKCGMTFBOKDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WmgCZQYKCGMTFBOKDHPPHMF.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=1694656602; 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=XfRZ+8VXY/OEYZxaTO5L0PdI0q7LErIUsWDHkpQzw6o=; b=bkEeaq2BQNGgtU+SveEznnmI7Yix2sSrwzOmnEBG1yMTLSfRgvkxsBJVZD5HvitZMYA+vW 3OG90mNt28VSAkKJh6E+S2A5ZnS5/JXDTHsDKEFhJinQuqP1Ts3rFUq20bfFBh7G1qgV4Q Lf43R5TNt5hT8DjsGrlq9FfLBCBql2U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694656602; a=rsa-sha256; cv=none; b=JOAlhltBsTawpWgzGNOU/R0eyIsm34Oy5C9xF14GwUSzy5NM8a9A2XC7N/YcfMmcywQTuk 8t/s7vp+piJOaFqyBlYt4Vsj9ljZBMivqO6XIqt8qLIyTHrS1sEBUlGZCfBBk4SObB96TK UHyCvqO6vdQfeqegXC1LFHhMuSlyx6o= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=r6dzU3bZ; spf=pass (imf19.hostedemail.com: domain of 3WmgCZQYKCGMTFBOKDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--seanjc.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WmgCZQYKCGMTFBOKDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d814a1f7378so2791632276.1 for ; Wed, 13 Sep 2023 18:56:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694656602; x=1695261402; 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=XfRZ+8VXY/OEYZxaTO5L0PdI0q7LErIUsWDHkpQzw6o=; b=r6dzU3bZpAXmV5oOwdBhy4KnPH98+HqnVYcWpE/d0LKl5bdtt6+Db/7NmhFQQCT2c8 QmnobPuWP7/j58Ab1ZiHekQQ4ZvVRWUwY2kPLm+PQR8jF9eBXc9QZYMKe7yUiNNv79PF t4qYCwU1gGLb5tYq/o8D7anvh9qT1jpEPoE4wInDvKPeGOGcfy8hjmHbTmnSXIHh6Cx3 lgt+wuS2ERC7voOWx4CxMmuT26boYbU7skSIQdDZHmy78zy75vEEIZ5KQS8HcU0dy0mL sP863sdT/TRfcZ9K3mLHVefPGFiGsGVBVy2xbg49qqbDP5Infim3idqb4q5T7DUVzPIY ih7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694656602; x=1695261402; 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=XfRZ+8VXY/OEYZxaTO5L0PdI0q7LErIUsWDHkpQzw6o=; b=oORVcd7KGS98k8wIgdNfQx6wdzpmS1nRdw+zbPHsl4OD5U0BImfodO06xIXv5oJzN4 TsZZ0bP+/ZCbL70BTyMecmcpHjazlyN2MimFF7l1ysUNt3iLu105vqWV0vD9ooHYybd4 AAU8qT62SLQb1Ui/OjhlqP0lJLgZrrrTS3TF/CHFaflvONxasjaeWCks0mmXBdr3fyAK 81CH6+aWPJ7FTYc8P2V9wbX+LEP6iUknWortYGx8W2tc9st/HP4w0oLmZrpGHQ+/+kYP PhMYa/XQsZjh5Hmbw07eKJl4d9okC9wvfJKv3MxXOBW5I7nIGXPTElDszLOqTb7MNvWV KT9g== X-Gm-Message-State: AOJu0YxIPA5U54NYlYw4GG9w+h1/iKD410GqrvrBOjfjuiM3eacoXAyB Fn3qrE9PhAoWC0kOlBNYaWcQ73D7dwk= X-Google-Smtp-Source: AGHT+IGRS3O+T9+VVevze+Os2k+DiGR8L+K7fuRVrk/vVySDtGkT7cNTInKtJxs4EW7npy+OCmJw4oxYHnE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:d141:0:b0:d77:fb00:b246 with SMTP id i62-20020a25d141000000b00d77fb00b246mr14108ybg.1.1694656602040; Wed, 13 Sep 2023 18:56:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 13 Sep 2023 18:55:31 -0700 In-Reply-To: <20230914015531.1419405-1-seanjc@google.com> Mime-Version: 1.0 References: <20230914015531.1419405-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914015531.1419405-34-seanjc@google.com> Subject: [RFC PATCH v12 33/33] 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 , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Stat-Signature: z6hpysqffy7s8b9ugq83f81b3twqyjbo X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: CF4161A0003 X-Rspam-User: X-HE-Tag: 1694656602-536103 X-HE-Meta: U2FsdGVkX1+wos18vnygIABHFfG/5AS0O9OWqWemClSgeGxzlePJD2P8jM6zovpYRPvXCzs5zW0sT0IGXzwM54SnYp1AHvGPNHiuPAUgeevflWJjA8DMEsQsq+0ip7xBjbWuz6FDSugZi196D6PiXJ7l47jRO7Qf2fjPfCJEk4sVwxtM0IdJ0xtYf+RTjnS0bmye6MzaiDSWuYMcgoMy+tsJ6BH5WMflaCdurp5xx9FkTgjYOvBdbu0KQNkZvYBHPfFnTGKnvtGCT8nhZqqtzFiR3P4gPHMrjpmxgkCHrnqU03/2qfkrUk2So0I6vhUC5m286RVgPIwPPeSQAnbxqRpIKdesDKsbNe4CZGTXoI+xhSbxzciO+0Vw9hXn39x11boKx3oF6ocnCYHRyxs2ulq8yVOO7oqr2sValSprXBm8NqXH4m6upHyhiPGJnCP0fL/Pg9t8UUZh67nTYW31MytOZdyDau3B0jrY4RH5O0zjyYbPFj39GKC0WapsaiUIGurMko0CCr44RkgjSNO69agQf4C/+Kwx02NetN/HiQrPNv8/ycpPZ6XnzRJeRsMsR/Jjw8hwdbywGD/XOiz8qIEphpsOp71bmha8kchADumZgEADdY9GXW1brSdeVJkheVDHxQDflDvAAgM4439tkV//L4XDowzrqErnQmPG8MOZiZCxSD9gByhWKakR1XY6gy/s+xMa/UBQlht53qoEKaa9R6Wuzf3eNfdC9585SGC4zQAsrAEOzK1w+f3hv8M+OLZPnNC7alTw/7a7Vifm5IKpVcexryat8Q1uCpCucdKjnR5XEiBJVfRvQ+c5ccWfpUGtbUIr4+IB83XrBCxfuLj73bbUNy8GfgLIDSaLAWYeX1X/2WNtaAAVi0I2ZwwQLXdWLhkAbeY+XoeWjC7FrULqgGg0SWu74yGbV8YLLkaY5xsGGag4solDIiwVxQyK0bZIRJ5tvHa4m1xqxbM ytGdu3x3 f7lBcJafH754Kt7bwRwJVCTmg3VHzDFneGxbQ4nlFNPmE8WFhyzD3hBPjLg8G9p/jjSyzK/5gge2xCgrUYQICzy4mbfsbZUPFTxsDFsQRZ0kQ8L5ZhdKWPf9kTjlAIKQkCPawmjNHeFygZZJlkEmhhaG/lmiSBdo2tJ7XemDrzmnyc0LrUmMefNiR8nBib0BO+50WvSZy0puMEisDQlxMOoXb8ICLbmhNYvYsRWSgrMJvVM1rDKwMnSLsaRBGV7cQp82swNbjKPzhb2NkrZVcqNOUL7JVlf6OeJ3A/glGeO5H9OSmPl056DHMYPj0VfGPkpZANJIthkKxoxEQTEoo9NmBCa44c42MrauNRVvQioRPFZ4JPY0xcY3pUA4MjgH5IcSTBCyQp8/XiFahAgzGND0KUAjbwqfbNgsyDeG9aTb1muEiR3JE1gpm6Z74fcfvr9oLkCjz+1PekMlsk2PAwYj4NOftbemTqVW6JkUlUBUU2PPc8EAsMMwJSPYRKaoWozfRliVmvK4tom4CEHQd8ZDSTL+MhSFk84GW8OnFIxw2XJOn3Zwnfxap9dbFCaN6tsZirqZ26pOW830dHC0GA/geoH1KCVbhF4M7qL0pIlgytm3dgUa3tdveivNzAht4m/4FB91+1TEwC6WZJICWW2ebPcsPHOa4ofyXJenpWmP3QSErSyJ3BuZvwB4yS4Nz7AgAMOLN++kfULhIoulS+usPcXOJnTk8EsSSRLWIei+6keeI2OldiNW4ynz7/fsie616vHFD1l5GaAU= 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: 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. sean: These testcases belong in set_memory_region_test.c, they're private variants on existing testscases and aren't as robust, e.g. don't ensure the vCPU is actually running and accessing memory when converting and deleting. Signed-off-by: Ackerley Tng Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_kvm_exits_test.c | 121 ++++++++++++++++++ 2 files changed, 122 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..1a61c51c2390 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c @@ -0,0 +1,121 @@ +// 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_has_cap(KVM_CAP_GUEST_MEMFD)); + 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(); +}