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;