From patchwork Tue Jul 18 23:44:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13317882 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 35906EB64DC for ; Tue, 18 Jul 2023 23:49:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14D9B8D002B; Tue, 18 Jul 2023 19:49:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1241F8D0012; Tue, 18 Jul 2023 19:49:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDF5C8D002B; Tue, 18 Jul 2023 19:48:59 -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 DFF298D0012 for ; Tue, 18 Jul 2023 19:48:59 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B327FA04F5 for ; Tue, 18 Jul 2023 23:48:59 +0000 (UTC) X-FDA: 81026375598.11.3CB7874 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf26.hostedemail.com (Postfix) with ESMTP id D519B140009 for ; Tue, 18 Jul 2023 23:48:57 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=vxUR1tuj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 36CS3ZAYKCDspbXkgZdlldib.Zljifkru-jjhsXZh.lod@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=36CS3ZAYKCDspbXkgZdlldib.Zljifkru-jjhsXZh.lod@flex--seanjc.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689724137; 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=/pt3Q2tpoxQxXn1tLyyE1UR18jbz9QzeXCmKj/ev2NM=; b=F6M83UGRGbmMLIKkVUhXTf7f1tCSpkwBqOT4NfjIDCRzYqf1wJy28pmfUWuKl2Fs9VCqwU Bw+L+SKOUurjMfAGfvdN/6CMBlPFjBiLIbwBPEtRJ6RbKPx7oXn5WbfOBf+/9TdYiXbaS3 J5wAWaA0qCRHaEOlONAadW+GUz5Toi8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=vxUR1tuj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 36CS3ZAYKCDspbXkgZdlldib.Zljifkru-jjhsXZh.lod@flex--seanjc.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=36CS3ZAYKCDspbXkgZdlldib.Zljifkru-jjhsXZh.lod@flex--seanjc.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689724137; a=rsa-sha256; cv=none; b=7G30T+2BDfVjdk0/EMpQ7Os+cTwYRzElBFxd56H57IQ7yfVJrKwBrzYAxBC+4pjN4d075E /VREhRwVrX2sV60AieTj5UVRHefBVw8oErU/oieB0AjlaAIqZI3zNhCnH+RqO8ZGJKdCJ2 JYSCfh64gluclmVgd3+f7hBgautak7U= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1b8b2a2e720so32165645ad.3 for ; Tue, 18 Jul 2023 16:48:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689724137; x=1692316137; 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=/pt3Q2tpoxQxXn1tLyyE1UR18jbz9QzeXCmKj/ev2NM=; b=vxUR1tuj/vcGsaOe3o3Dg3+dBjjNSQ1vNUqJnA915YLta3qnEHtgJJmhnuBEvMSvuO PeQ54yO76QkRq4Poh7gfw5EibjHULF6szcbTO++R7xTLcTVHQgaEtb3O2GkE8TOOgrPj j+skKU6AKAL/CHeOI2fWZhtH2+GBhR8GFXcSgy+S791M68OMvBR8fw1sghKrAfPeG39D aPhvXxFMYa/vphJO2xprgh5gp8hbsuB9tPxNTIVm7fk+E06HBzO4G4hjTZ+UFpjlD7u9 AiVs2GVbO1nSDvgFw3EWZlgv0q++Q9CJ07oUOhmTcF8UoY6sdZ1aY/fD8Yp9QaPi1uiy 5yig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689724137; x=1692316137; 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=/pt3Q2tpoxQxXn1tLyyE1UR18jbz9QzeXCmKj/ev2NM=; b=X0gzvsdEcrAb+Brtm+mkqky0TEwTyeXUpdWFFjwnBnjGy1fTJPxfcfabCCa3w9IGiI yCDVgL5ahSFqhkpgJVGWOl+5zaYyqrOdRQBqdzE2nR4Za0StIJujZAP492Ojq7rvwl7c 2VUAQhoPM6iFVo3TSj2fDeFA5vSq1UlzlXN5n9daRdZLr9R9lHMasI2kuy7pZmrekSll Z1IFBxYYSeQgcBGPjnohGpgWJKZQumc1j92k4sNKKOVLfn4zvz5snznr/Ro9VD+Sbdis pIATxp7V+K3xbRQx+ddIC8T89bW5jFbcq8SjnabaUTMxQ06DAoghAIIuAPZFd1849UQK EeGA== X-Gm-Message-State: ABy/qLblJhM8opX42sSYUkpNnl2Cl3JNEnPwXvNAC+5VfyVmykyrggOY 8/EgfkkK76SViMUwx7oZ+txkm8BVP1g= X-Google-Smtp-Source: APBJJlFIKaKNLayUXkm9YIdNhY8NWw+bGGk4UkSeGDFPqrXqT1vlGljtzz/Lr2RncRF8zUbyrReIDSYCDDk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ce88:b0:1ab:18eb:17c8 with SMTP id f8-20020a170902ce8800b001ab18eb17c8mr7987plg.2.1689724136690; Tue, 18 Jul 2023 16:48:56 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 18 Jul 2023 16:44:56 -0700 In-Reply-To: <20230718234512.1690985-1-seanjc@google.com> Mime-Version: 1.0 References: <20230718234512.1690985-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230718234512.1690985-14-seanjc@google.com> Subject: [RFC PATCH v11 13/29] 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 , Yu Zhang , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , Vlastimil Babka , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" X-Rspamd-Queue-Id: D519B140009 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: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z1C8jco2WgJqF1kn7lAojBjy1QraJJkmUM5IKevnHb64dsbaHJCkOBTDFSLPwxqT6hQdm6cmjrOcgFnrYTQgyN9PMAXJ2DmTkSTVBQjll2+HCZ1q3HXx/Zqwj2+yemeak4VBacZ7ExiRvkF3g9DuuCCOhsC4dm8xYhMzB0bu+6kLPLQ8RJhg5JhLn6TmEB8eLa/HNXXb4aPE6szhPP9LlaN25E6dpsdQyxVZ98hK4QvoFBkIlAlMl/519T+bS7N33GtNJ5W4WuVVRhMFiPfuzL1S6Un5uCgjhDl6X4oRSByGExcC2/Y6W3nl1yIhP0m6gP4OIH7WOmG9tcHQZrvZvQg3TkLlMbPiirUi4LsfvCmEaJ1bWpg2Flpw/txh9ov594/B90PNbiAoyTFdxkydQnVQGshXHG9f7bjAuo3ACJtLHUryxwpXCk30pmDsLQXKhzo4OhFV1zoNdhxTg0DgbZja9zg5zHrZmuPUoL7jfsX6Pw+rvdZIYVo8QsnyGmvtzw/kZoLu8ub7LVglGVH/pflWaEROHQJiijmoITccCQCLGobZ0xXWi259Tsr+KutZrICWJXCjFn1kiuJioqbxlpiCcRefZTx47G2AnYs5UGjpf+SLY6fFJwRB53hzalHC6MoZ9xtoisDVDGOd9wwMymIrq5sNMmApnC5fDpJFtFtaR8MIC5PRgNsJm69pxiMC2ZjUklvBXCznLvXZFJ1Dgl/j3LK5qOw2jlRpuqLpGNKYCL/lBwilFC96r97dZx1vkVEisIuXk4I9/F6m2KOxmp 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 --- include/uapi/linux/kvm.h | 2 ++ virt/kvm/guest_mem.c | 52 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 9b344fc98598..17b12ee8b70e 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2290,6 +2290,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 1b705fd63fa8..384671a55b41 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 (!(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 (!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 @@ -332,7 +365,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)); @@ -355,6 +389,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_unevictable(inode->i_mapping); mapping_set_unmovable(inode->i_mapping); @@ -404,6 +439,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; } @@ -413,6 +454,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;