From patchwork Fri Jan 17 16:29:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943552 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 9B943C02183 for ; Fri, 17 Jan 2025 16:30:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EFD806B0096; Fri, 17 Jan 2025 11:30:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EAC186B0098; Fri, 17 Jan 2025 11:30:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD98F6B009A; Fri, 17 Jan 2025 11:30:18 -0500 (EST) 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 9E2066B0096 for ; Fri, 17 Jan 2025 11:30:18 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 33A4DB054B for ; Fri, 17 Jan 2025 16:30:18 +0000 (UTC) X-FDA: 83017481316.27.1926264 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf02.hostedemail.com (Postfix) with ESMTP id CAAEC8001C for ; Fri, 17 Jan 2025 16:30:15 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IWP8tNzK; spf=pass (imf02.hostedemail.com: domain of 3loWKZwUKCA88pqqpv33v0t.r310x29C-11zAprz.36v@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3loWKZwUKCA88pqqpv33v0t.r310x29C-11zAprz.36v@flex--tabba.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=1737131415; h=from:from:sender: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=LVZ6/wJnW1NLmDdlnZeeMFjWuJzNbaR38/28aWmB+Ok=; b=vQioO/SOTzvqQt+t3i3jlwacOaOGijiUlpXnrmj5ueYUMIWafONu+0nUlPXocd/LAarihY SnleKltg9HqfqKY3CTr+EtmsfmKwzJNsw9D6cbehFs8AcQL/63nAWcj3aRIheYv8tkVfH+ 4LtYblQxzMsz+tItjabeoj39yfzSNnw= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IWP8tNzK; spf=pass (imf02.hostedemail.com: domain of 3loWKZwUKCA88pqqpv33v0t.r310x29C-11zAprz.36v@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3loWKZwUKCA88pqqpv33v0t.r310x29C-11zAprz.36v@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131415; a=rsa-sha256; cv=none; b=K+7xBq+czMbUtycYYOuvSU2hFGL6hh1BNPQoVZVA5WDCr0WJllgrC18wYEDjSRKAhQfHxi 85lSvjgfzknMWyR5SnkPwSaCt252nSRFZZj5UdzIp+qKeiqcDbJqiIxs0m0L2Hn/hLg+tH WGNYYQWz7RME0qISzlKsHn4oJSvQBSg= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43673af80a6so17314115e9.1 for ; Fri, 17 Jan 2025 08:30:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131414; x=1737736214; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LVZ6/wJnW1NLmDdlnZeeMFjWuJzNbaR38/28aWmB+Ok=; b=IWP8tNzKGfYoeNl9LBoutrAshrOj19x5/t6r4WKGQU9Hh8lBW6wY8zNC4CJHpnh+Uh oryuajmN0ZsIPwcwbg02z355mbQWdZ8cRQoJMMBjpHW83vdLEgiDTaQZuNVFKbowfwF0 UFTGMTpFJiX3S5+4eb8uPM2LV01kOaLw2ROpDtJw+VtwC4o8gdsvMZ3QoJ2Lk3uSvEdn gRxwt3CkjdlB0C80xCOzMGS9QBI2F+IgeB/65vcpFNQ+xEODeoqxCfwWPKO4gYxCoC+d 3wegkLqalMZhC9C4mM4PPdX6fEPEyC3+oWgioMOa/O0IYkhkVOJkPZBfK21mOkll9Db6 PAHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131414; x=1737736214; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LVZ6/wJnW1NLmDdlnZeeMFjWuJzNbaR38/28aWmB+Ok=; b=n4mPTa09Qe4NxxYE8+IaD2BSbJLOBDqOvf3WjvJ7dhhMA/ifAt8YyaJ17Wy2sVN5dN 794iqUQ2vieRYnYIYXUeA5AQU6+7M0np6/nvFtWBZvyy8b1wFSGRyscHkwds4sVzPfV7 /54RYyy3Gq+cyLrEOR0Vwkj754SfTeemzIixwIhGBlgQqmHHlojRdNr0VEJTFEL7r7si Qfs40cJmDhQQNoVTqg5hXT+wlwp2LZRuGrXmrtnx6YPhZl4OC9LgtjTHX4T1TGeA5VM6 hDNcwIpCKrKF8iPJrtcySR2IKJnnr4CWOJTjaqw5GHO1Z84936wRwvMqaNZYieASoQ9A 0Gdg== X-Forwarded-Encrypted: i=1; AJvYcCUL3Q3m8CkmQcL+ucuRJ5Us3mt6XzwmtMRe5IdbMP9n9DKWAlYoNWex0Vcvb1T+pZs+uKQ7GMXEhA==@kvack.org X-Gm-Message-State: AOJu0YxjCRb9QRgdup25Pa5Rbed4kx+Y2s1xcnskw+oN1isa2npJvBbk +FN/mxHvBy3NJ4thm3ofrg7QxWjhoXGJVPNiNjCD4muWj8RHDGFibGDeSaNTmYbevS9GQQVK5w= = X-Google-Smtp-Source: AGHT+IHCaOLf3AxGrXp3UT2DCvE92fOOg4PnB/FoRBbpczstO0JHX8UoSu03mcJ4e4/3vk20WdvjqLoW7Q== X-Received: from wmgg21.prod.google.com ([2002:a05:600d:15:b0:434:f271:522e]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5027:b0:434:f5c0:3288 with SMTP id 5b1f17b1804b1-43891430ed1mr33942345e9.29.1737131414416; Fri, 17 Jan 2025 08:30:14 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:51 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-6-tabba@google.com> Subject: [RFC PATCH v5 05/15] KVM: guest_memfd: Folio mappability states and functions that manage their transition From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: CAAEC8001C X-Stat-Signature: nks1iqgp84t1wz7emapsdw6yw19opqn9 X-Rspam-User: X-HE-Tag: 1737131415-362474 X-HE-Meta: U2FsdGVkX19LU9ZB5h5Dk5nkOvSUxi1gHJB8/SQN0VpzLpRL7QIHx0vxMj1phoPU7oxyes3+FqNmIh7FFshzBbmpXJDpA+YAQNjEYDaYTJ0Y9IFO5l6juTQDiRuB8HwRuX3reBcNXr0iQjuLWHRKw7259kmQR93jtr5PpSAQ8JXG9vnTQnP2MANtg7Ii+8DeDj+EFcBNcjDq1ylDshhVBRX+iu3T9jPiwwfppvnyrN86cnI9v8vfpGkf58HeBeiQNF/QxY7k01EKzvsBRq/einEtlcBTjO0dER3Ubfv5u+Iq7o23FcijYDKNHA2gfHAWNYkl3O8ZwCPikfd9DSN0zHLwHaxrv5UYdrL+Yb0QbDwJdwB1PHjInU7WlB0bNSpAq+NPDpfobNdhmvb4OnN7NSCKu6VQKhvZuAfcsluT6vpZwg2NwkF6B8DzzHudgaPVxpdgJo0PcIggzCQMCLQdbW+LEvcQSKfklQpAljTZJp6B7er+ksRXf75eZghmRf46VF8q+wyjr/LFEyMiXnkPkVRw/KUL2W1OI491n804cwanP+pOm7wDkSd03ezuVAsxHEiAe2BS618i4kHKoVOWldNzXP3mw0YAFSAT+KXqi+H5VECJcWDNjBRkIbA6N2BdmeuX5lInD52BFE1vS2WRRT20QGcuV785xUf8bbSmYcnWkKf6FicgT4zoKiJygZmFiKdzPb8H5Gczxd2n7AuuSLO4CyUTuGI0p/tBQmb0GvYmztMLQ1GuhcCWAVscrbAvnwDhQ/CLPCb/qYhV2G4lRrm8RNpiIghtvnBBCgNM+Pv1NuzJErBvwKo2C3o9J2CdsS59iH+BXC6FjMTCtjhMx2ogntiiXzIbpJNmsUPc8SWDqiiqjFjX0BYilnSlntLYrbIeN3Q4uupYQS6hVidm/X7TePHANRwarCwY1akIgDmuefrhFa/NJENxEQQpwlhTBswWTyosEiFpr4rPZ4Y VfvcIflO ffvlW8bOKFpvCqKzuqrU+201MnaRBe2PVv0tm0A+Kq44gAaY86iNh0uv6Vvhmx2egW2Y2d3FTyaUAB71M59uF3ANCJ2juWKZkGHavIPslDP5AnMuFSh2TCaBaotOXW/YARk4xEB7xxpBkxabXqVT237Sgrq+IVKA2IXNXqr2p/GdhQHcVC5yqY/rw3L4v9NU9u7MuYikA4jlN7UQiw0nIWpFJBSv6sIiY1/tI6McJtg7A7+GxxALtOjqSpfwaSJGRf/xKVIK6w0zCQUa77EM3B75SIrv+iu0Eoac750xH9dg0n55Ndga7m+PWodS3eya7xx33WxelWAZqnWgbbXpCd9KvylPbhqraLCpsJMWIREthilW17AqD9TuU+iPJK0F/YF2doHiO3b1nIchiAQgVYxmJoIicnu+RCWyxtbPlOWZ3H4AyWV/l5EU59DH8Ch0ZN8hQgSFUcUBxQ+MTukjXnO9DMlAPH78ruf4Urq4kzF4tt/eWI0jBgZgOXc04UCzErHdwWUpeTY/ynEKi2ydimcl0HoIGG/iqX+SH08citGk20U6fbR3E/jE7Je7pFIVAvK1Xd3oBTHUG1E8UCMzJNqVCknayNFWpliBdO5FTmyNcse+ZPLR2q3bvEiwiX7R0rFcGzKKnZGVevjbLZUjaNUibzw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: To allow restricted mapping of guest_memfd folios by the host, guest_memfd needs to track whether they can be mapped and by who, since the mapping will only be allowed under conditions where it safe to access these folios. These conditions depend on the folios being explicitly shared with the host, or not yet exposed to the guest (e.g., at initialization). This patch introduces states that determine whether the host and the guest can fault in the folios as well as the functions that manage transitioning between those states. Signed-off-by: Fuad Tabba --- include/linux/kvm_host.h | 53 ++++++++++++++ virt/kvm/guest_memfd.c | 153 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 92 +++++++++++++++++++++++ 3 files changed, 298 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index cda3ed4c3c27..84aa7908a5dd 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2564,4 +2564,57 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range); #endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE +bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end); +int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end); +int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end); +int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, gfn_t start, + gfn_t end); +int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start, + gfn_t end); +bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn); +bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, gfn_t gfn); +#else +static inline bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end) +{ + WARN_ON_ONCE(1); + return false; +} +static inline int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, + gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, + gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, + gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, + gfn_t gfn) +{ + WARN_ON_ONCE(1); + return false; +} +static inline bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, + gfn_t gfn) +{ + WARN_ON_ONCE(1); + return false; +} +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + #endif diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 0a7b6cf8bd8f..d1c192927cf7 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -375,6 +375,159 @@ static void kvm_gmem_init_mount(void) kvm_gmem_mnt->mnt_flags |= MNT_NOEXEC; } +#ifdef CONFIG_KVM_GMEM_MAPPABLE +/* + * An enum of the valid states that describe who can map a folio. + * Bit 0: if set guest cannot map the page + * Bit 1: if set host cannot map the page + */ +enum folio_mappability { + KVM_GMEM_ALL_MAPPABLE = 0b00, /* Mappable by host and guest. */ + KVM_GMEM_GUEST_MAPPABLE = 0b10, /* Mappable only by guest. */ + KVM_GMEM_NONE_MAPPABLE = 0b11, /* Not mappable, transient state. */ +}; + +/* + * Marks the range [start, end) as mappable by both the host and the guest. + * Usually called when guest shares memory with the host. + */ +static int gmem_set_mappable(struct inode *inode, pgoff_t start, pgoff_t end) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + void *xval = xa_mk_value(KVM_GMEM_ALL_MAPPABLE); + pgoff_t i; + int r = 0; + + filemap_invalidate_lock(inode->i_mapping); + for (i = start; i < end; i++) { + r = xa_err(xa_store(mappable_offsets, i, xval, GFP_KERNEL)); + if (r) + break; + } + filemap_invalidate_unlock(inode->i_mapping); + + return r; +} + +/* + * Marks the range [start, end) as not mappable by the host. If the host doesn't + * have any references to a particular folio, then that folio is marked as + * mappable by the guest. + * + * However, if the host still has references to the folio, then the folio is + * marked and not mappable by anyone. Marking it is not mappable allows it to + * drain all references from the host, and to ensure that the hypervisor does + * not transition the folio to private, since the host still might access it. + * + * Usually called when guest unshares memory with the host. + */ +static int gmem_clear_mappable(struct inode *inode, pgoff_t start, pgoff_t end) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + void *xval_guest = xa_mk_value(KVM_GMEM_GUEST_MAPPABLE); + void *xval_none = xa_mk_value(KVM_GMEM_NONE_MAPPABLE); + pgoff_t i; + int r = 0; + + filemap_invalidate_lock(inode->i_mapping); + for (i = start; i < end; i++) { + struct folio *folio; + int refcount = 0; + + folio = filemap_lock_folio(inode->i_mapping, i); + if (!IS_ERR(folio)) { + refcount = folio_ref_count(folio); + } else { + r = PTR_ERR(folio); + if (WARN_ON_ONCE(r != -ENOENT)) + break; + + folio = NULL; + } + + /* +1 references are expected because of filemap_lock_folio(). */ + if (folio && refcount > folio_nr_pages(folio) + 1) { + /* + * Outstanding references, the folio cannot be faulted + * in by anyone until they're dropped. + */ + r = xa_err(xa_store(mappable_offsets, i, xval_none, GFP_KERNEL)); + } else { + /* + * No outstanding references. Transition the folio to + * guest mappable immediately. + */ + r = xa_err(xa_store(mappable_offsets, i, xval_guest, GFP_KERNEL)); + } + + if (folio) { + folio_unlock(folio); + folio_put(folio); + } + + if (WARN_ON_ONCE(r)) + break; + } + filemap_invalidate_unlock(inode->i_mapping); + + return r; +} + +static bool gmem_is_mappable(struct inode *inode, pgoff_t pgoff) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + unsigned long r; + + r = xa_to_value(xa_load(mappable_offsets, pgoff)); + + return (r == KVM_GMEM_ALL_MAPPABLE); +} + +static bool gmem_is_guest_mappable(struct inode *inode, pgoff_t pgoff) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + unsigned long r; + + r = xa_to_value(xa_load(mappable_offsets, pgoff)); + + return (r == KVM_GMEM_ALL_MAPPABLE || r == KVM_GMEM_GUEST_MAPPABLE); +} + +int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + struct inode *inode = file_inode(slot->gmem.file); + pgoff_t start_off = slot->gmem.pgoff + start - slot->base_gfn; + pgoff_t end_off = start_off + end - start; + + return gmem_set_mappable(inode, start_off, end_off); +} + +int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + struct inode *inode = file_inode(slot->gmem.file); + pgoff_t start_off = slot->gmem.pgoff + start - slot->base_gfn; + pgoff_t end_off = start_off + end - start; + + return gmem_clear_mappable(inode, start_off, end_off); +} + +bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn) +{ + struct inode *inode = file_inode(slot->gmem.file); + unsigned long pgoff = slot->gmem.pgoff + gfn - slot->base_gfn; + + return gmem_is_mappable(inode, pgoff); +} + +bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, gfn_t gfn) +{ + struct inode *inode = file_inode(slot->gmem.file); + unsigned long pgoff = slot->gmem.pgoff + gfn - slot->base_gfn; + + return gmem_is_guest_mappable(inode, pgoff); +} +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + static struct file_operations kvm_gmem_fops = { .open = generic_file_open, .release = kvm_gmem_release, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de2c11dae231..fffff01cebe7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3094,6 +3094,98 @@ static int next_segment(unsigned long len, int offset) return len; } +#ifdef CONFIG_KVM_GMEM_MAPPABLE +bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct kvm_memslot_iter iter; + bool r = true; + + mutex_lock(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end, i; + + if (!kvm_slot_can_be_private(memslot)) + continue; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(gfn_start >= gfn_end)) + continue; + + for (i = gfn_start; i < gfn_end; i++) { + r = kvm_slot_gmem_is_mappable(memslot, i); + if (r) + goto out; + } + } +out: + mutex_unlock(&kvm->slots_lock); + + return r; +} + +int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct kvm_memslot_iter iter; + int r = 0; + + mutex_lock(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end; + + if (!kvm_slot_can_be_private(memslot)) + continue; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(start >= end)) + continue; + + r = kvm_slot_gmem_set_mappable(memslot, gfn_start, gfn_end); + if (WARN_ON_ONCE(r)) + break; + } + + mutex_unlock(&kvm->slots_lock); + + return r; +} + +int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct kvm_memslot_iter iter; + int r = 0; + + mutex_lock(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end; + + if (!kvm_slot_can_be_private(memslot)) + continue; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(start >= end)) + continue; + + r = kvm_slot_gmem_clear_mappable(memslot, gfn_start, gfn_end); + if (WARN_ON_ONCE(r)) + break; + } + + mutex_unlock(&kvm->slots_lock); + + return r; +} + +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + /* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */ static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, void *data, int offset, int len)