From patchwork Tue Sep 10 23:43:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13799499 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 020F4EE01F2 for ; Tue, 10 Sep 2024 23:45:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD2228D00E5; Tue, 10 Sep 2024 19:45:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C5BA78D00E2; Tue, 10 Sep 2024 19:45:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAE388D00E5; Tue, 10 Sep 2024 19:45:21 -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 875378D00E2 for ; Tue, 10 Sep 2024 19:45:21 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3FC5880897 for ; Tue, 10 Sep 2024 23:45:21 +0000 (UTC) X-FDA: 82550462442.09.15FB169 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf09.hostedemail.com (Postfix) with ESMTP id 760EA140013 for ; Tue, 10 Sep 2024 23:45:19 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=CXtTXfxR; spf=pass (imf09.hostedemail.com: domain of 3DtrgZgsKCIMhjrlysl50unnvvnsl.jvtspu14-ttr2hjr.vyn@flex--ackerleytng.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3DtrgZgsKCIMhjrlysl50unnvvnsl.jvtspu14-ttr2hjr.vyn@flex--ackerleytng.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=1726011817; 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=tJq0MqYSe34io4UmOdJbczSi4Mtmt5uxlF7QPZDIEdU=; b=3jd+3XQOKQuioAAxNs1t0CRFDmnEAVlo4TVjwdYfhcnpJ9WyxvUrET+LaU0YP95dkPY9K2 TpDZLvBmUcNIg16UbyvgFt2U0qw1TBQOalX29Q+hbyAagSo7biN4+xVx/AgANDWxGXYG3v sAjazLDKAPcrpEGLybVSLcWNWnj7Vvo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726011817; a=rsa-sha256; cv=none; b=tzVdFR6BZpsw64zdSX7u7pkmBn0r8RdugGIL3a6l4BKZqvhlv3wazzSYOBV76tlOCZ6gZL X9f2ebC5HpFXx+7IHGHZSjQhoCr75SXV9ac6JolQeDdc14S87ml2MuC6eD5QTBuYkDmmBW MN42iVoBh15YBJybLRZ1A1qbLmbv17w= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=CXtTXfxR; spf=pass (imf09.hostedemail.com: domain of 3DtrgZgsKCIMhjrlysl50unnvvnsl.jvtspu14-ttr2hjr.vyn@flex--ackerleytng.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3DtrgZgsKCIMhjrlysl50unnvvnsl.jvtspu14-ttr2hjr.vyn@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6d5235d1bcaso181004727b3.2 for ; Tue, 10 Sep 2024 16:45:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726011918; x=1726616718; 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=tJq0MqYSe34io4UmOdJbczSi4Mtmt5uxlF7QPZDIEdU=; b=CXtTXfxRxiSxNWy/souNmVzJKUgI5X2QHQI3p/7IqrTkzK4FuMSg0q7oCSk4pRlsMZ TtLpSPDOuIiOoR9hPrxUNcAzrJwo1CFu+ORI17WEKVJSxWHB7dLyS5kzzfV6MyBjilLR jnBLlngDKcmWUX3+uIDxbQa2iA8wFYGY2e6ULVip5HaaDUg5UDYWBLkuU/UZhMsCQQuB hrnQl4ZlvkLuH89qWnvlhNJfKfRA8PgFpGqzxukQcHOyGRA9WYhW26drCBdJ3HxB2H5P 8LiSmwfWu6kJCcBZLxpgiSWz6F6qE/zNj4vVdpaFHrTvFp9sloC/Hbk8Th/ZxD0p8XIc S5jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726011918; x=1726616718; 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=tJq0MqYSe34io4UmOdJbczSi4Mtmt5uxlF7QPZDIEdU=; b=n8R8W8evJ+qRo594EZF6NUXRmsa8gbsywTr0maPTGLX9w4WUNG+nwHrE1tADWISvCO /OkVcz+NFNdJy3ORx6Cpal2cVagZyu8CIKyO9gUFVcccTcBBeZgLCwrv3EG8n6ZEhwkB 6MZEuGDdhqWHLIi2CcfmT+BN2vw9cgktzNR7vBmJv6dUdKD13Fkv/G7cDEdIu1P+T/UM 0fGnz8o0xt5mvT9edQobiKRLEwR/DJmM+ygrawoeIWLHR1U5wI+IHjJ5nWKCIOZnXe3x zaES6SRBNtxnK683e9Zh2BVk0KaKqTGeu0yFLpfuOtAvewInjWAHRa6zBA4YyeifgyCX VYOA== X-Forwarded-Encrypted: i=1; AJvYcCWB8aOON4lXyEiktlkq72C7OV41yldMoMi1UQGStBnl5Z0pLabzMAGrZQIwWNJe8dN8UW4XTgzkPA==@kvack.org X-Gm-Message-State: AOJu0YwOozw6UESOnfP9KNzH7Y0rI3Ws9prlFsOo976MCtVfWFRJIWQ6 xMz7wvAIsCbzcV+cOSyuEVQDEEnGXWg9FU9snLGnlUn0sAQpFloL2Vn30Q9NrPpQV8Eh/l+HK4+ oguIAuojfFi0M7v6h7DEwVQ== X-Google-Smtp-Source: AGHT+IGSLUm8oBiaMD+q3wklbXJeQqiWR8FYu/x3IcQqY2JhzZy9eAY0OzQzZWTZkFFReNAsglf+owucD34Qou3lfw== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a05:690c:38c:b0:6af:623c:7694 with SMTP id 00721157ae682-6db44a5d200mr10772047b3.0.1726011918533; Tue, 10 Sep 2024 16:45:18 -0700 (PDT) Date: Tue, 10 Sep 2024 23:43:57 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: Subject: [RFC PATCH 26/39] KVM: guest_memfd: Track faultability within a struct kvm_gmem_private From: Ackerley Tng To: tabba@google.com, quic_eberman@quicinc.com, roypat@amazon.co.uk, jgg@nvidia.com, peterx@redhat.com, david@redhat.com, rientjes@google.com, fvdl@google.com, jthoughton@google.com, seanjc@google.com, pbonzini@redhat.com, zhiquan1.li@intel.com, fan.du@intel.com, jun.miao@intel.com, isaku.yamahata@intel.com, muchun.song@linux.dev, mike.kravetz@oracle.com Cc: erdemaktas@google.com, vannapurve@google.com, ackerleytng@google.com, qperret@google.com, jhubbard@nvidia.com, willy@infradead.org, shuah@kernel.org, brauner@kernel.org, bfoster@redhat.com, kent.overstreet@linux.dev, pvorel@suse.cz, rppt@kernel.org, richard.weiyang@gmail.com, anup@brainfault.org, haibo1.xu@intel.com, ajones@ventanamicro.com, vkuznets@redhat.com, maciej.wieczor-retman@intel.com, pgonda@google.com, oliver.upton@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-fsdevel@kvack.org X-Rspamd-Queue-Id: 760EA140013 X-Stat-Signature: ewokzxdrajiu75qjdctk4k94qpqqbkme X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1726011919-526864 X-HE-Meta: U2FsdGVkX19Fb4ZeI3eC/hrAR5wXCbLdFW+C0Ln74CWy7uSrGPlfeFgRMFF8JkKAzdxSyCwFXK5wtgS1R9j4a4pcAuUUXXkk6e2ESMM/LLAzTC+7/OxvVKawgXrGn5yDG3jQma+3rIxsILCfj9h52dvPT3HtWLBdJ8emjZTwHsFsxcGcFkJ1HHWjd0BNS1ZCS/luYoRq4O0Nqy9lrVDMYWYvu00FCEyyvTFuwsmBwQOSz1kaJFn4CxfXnlRO9fO6WWmUQ4khK5WwGIE3G8GaP1oP8EKAnw9Tl5UPPoN/gUfAMrcdNeSZ/TC9kv+JKkP10yJF6n9eUfK/aznt0xTINWbLiVr/f4NSl4d5UD9Y9F/NS2s4oLGcdkEq1BZtq0kx96/isS71HtL5zS+ffKcx0++qV88DegCIzRs3F8jGZaUqw9BKFzN5FW5bTp1E/ImtzIEBlMKCWYBYE+SdIg0JhIpx3bWofsxKtpfyo3MK6w4BTGWUAP/dsf93vlplz2itYFOAdwCzpYtz3hHeyGcy4kzm0K9CFkqIl2bQQ5vCuBIDcWVVCRfbs4u4LLAvubG/lYHT7frvJBvZ6NVSu/c5zmmfSNh+nQWJQwueSlD4f9+IZAB8IMHNMVbT7+JWN6DlJ0HXbOVhUwL6kRf51qvtvFjwD44A7AotwNd8SiUr8kXATnRxIaH+KoObFxq/um1M+dpRtypy4joKE7bH9TYtgaguIw7+g1j2DqB2zuU2fWYy8lhHTrh/T+apWzbnZ7BtWN3nflZiPI9cl6OUITm8MVrupgXUMPJoDu1GtLTnDsmq7GwDAREptAMgXmfdA2MvGam0dMx4aIUIUJ4Cmw8SZKRwe5kouGS9exQK/CsDx3t8sChRkyxRen9U20Ul3pU4g4OaH9O/+U7fuARYI5TaKP+Aw88U+7iU1JavZQ1I8HUX+iry9iVRRim3BXbq4mAwcAsCNTWARmOhKabZNky ppTv7ANd 0iH4iGMbqk7T6+NagexW2slfbBbwKkCAUc3noRx8viBKDlshRH/7XHdmQ2NK2+12TMK541hPLddMH4JtSdUxEW1bA7EIY8bjNd404fmdVNpzkPMyU/ktAnFLGy+qDbqwjZ505COEsOC+uxlnbmI3yFdxeQx+jI1GnMCswcvgUMSk6+lP4Qm/mEWg29ccwIevnBGVNUUIpMKE0NHWIBoMVcgTg02Wlv/WNZH+aYJ2EMoIsFHopr7/Ydpaisssd/SDNt1RiOm94F/qABx/VCjBu/5f2XUnfHOgQCKubhPry514UgjWTktIrezTUni0Ly3+CnSqRozHVytA3hl674yxshUv8c1yZXeeRTCgEBPHRtPLgXBllPN8in09sJX0JkCRSQyt1N/BdFHlM3bcxCL2+CsxSkeEvjpz/XWFH81EOysdEyuc4JZ69LBn3/RLFtiHZYpKMkEnRvdyacKPtqIRNE/kLBzsgmrD7gAOjBUG8GAMFADfipCvKTkWrwWHbpQ2V2Fp7uQco9j7pjN7/NxMJVLgnIClt2Xwz7k0JDGY/wqQ9l4sgxUuM38aK1RpYffevjpeFeUGrt9iB2Tuln+pBQFDlVo+X3sE9K54qoXSecYafZMs/tWlr7wSNnbilkwr5ryDwZvuDlPAKFm7irE4kR4uh1FRXfaLwgFbI 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: The faultability xarray is stored on the inode since faultability is a property of the guest_memfd's memory contents. In this RFC, presence of an entry in the xarray indicates faultable, but this could be flipped so that presence indicates unfaultable. For flexibility, a special value "FAULT" is used instead of a simple boolean. However, at some stages of a VM's lifecycle there could be more private pages, and at other stages there could be more shared pages. This is likely to be replaced by a better data structure in a future revision to better support ranges. Also store struct kvm_gmem_hugetlb in struct kvm_gmem_hugetlb as a pointer. inode->i_mapping->i_private_data. Co-developed-by: Fuad Tabba Signed-off-by: Fuad Tabba Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Co-developed-by: Vishal Annapurve Signed-off-by: Vishal Annapurve --- virt/kvm/guest_memfd.c | 105 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 11 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 8151df2c03e5..b603518f7b62 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -26,11 +26,21 @@ struct kvm_gmem_hugetlb { struct hugepage_subpool *spool; }; -static struct kvm_gmem_hugetlb *kvm_gmem_hgmem(struct inode *inode) +struct kvm_gmem_inode_private { + struct xarray faultability; + struct kvm_gmem_hugetlb *hgmem; +}; + +static struct kvm_gmem_inode_private *kvm_gmem_private(struct inode *inode) { return inode->i_mapping->i_private_data; } +static struct kvm_gmem_hugetlb *kvm_gmem_hgmem(struct inode *inode) +{ + return kvm_gmem_private(inode)->hgmem; +} + static bool is_kvm_gmem_hugetlb(struct inode *inode) { u64 flags = (u64)inode->i_private; @@ -38,6 +48,57 @@ static bool is_kvm_gmem_hugetlb(struct inode *inode) return flags & KVM_GUEST_MEMFD_HUGETLB; } +#define KVM_GMEM_FAULTABILITY_VALUE 0x4641554c54 /* FAULT */ + +/** + * Set faultability of given range of inode indices [@start, @end) to + * @faultable. Return 0 if attributes were successfully updated or negative + * errno on error. + */ +static int kvm_gmem_set_faultable(struct inode *inode, pgoff_t start, pgoff_t end, + bool faultable) +{ + struct xarray *faultability; + void *val; + pgoff_t i; + + /* + * The expectation is that fewer pages are faultable, hence save memory + * entries are created for faultable pages as opposed to creating + * entries for non-faultable pages. + */ + val = faultable ? xa_mk_value(KVM_GMEM_FAULTABILITY_VALUE) : NULL; + faultability = &kvm_gmem_private(inode)->faultability; + + /* + * TODO replace this with something else (maybe interval + * tree?). store_range doesn't quite do what we expect if overlapping + * ranges are specified: if we store_range(5, 10, val) and then + * store_range(7, 12, NULL), the entire range [5, 12] will be NULL. For + * now, use the slower xa_store() to store individual entries on indices + * to avoid this. + */ + for (i = start; i < end; i++) { + int r; + + r = xa_err(xa_store(faultability, i, val, GFP_KERNEL_ACCOUNT)); + if (r) + return r; + } + + return 0; +} + +/** + * Return true if the page at @index is allowed to be faulted in. + */ +static bool kvm_gmem_is_faultable(struct inode *inode, pgoff_t index) +{ + struct xarray *faultability = &kvm_gmem_private(inode)->faultability; + + return xa_to_value(xa_load(faultability, index)) == KVM_GMEM_FAULTABILITY_VALUE; +} + /** * folio_file_pfn - like folio_file_page, but return a pfn. * @folio: The folio which contains this index. @@ -895,11 +956,21 @@ static void kvm_gmem_hugetlb_teardown(struct inode *inode) static void kvm_gmem_evict_inode(struct inode *inode) { + struct kvm_gmem_inode_private *private = kvm_gmem_private(inode); + + /* + * .evict_inode can be called before faultability is set up if there are + * issues during inode creation. + */ + if (private) + xa_destroy(&private->faultability); + if (is_kvm_gmem_hugetlb(inode)) kvm_gmem_hugetlb_teardown(inode); else truncate_inode_pages_final(inode->i_mapping); + kfree(private); clear_inode(inode); } @@ -1028,7 +1099,9 @@ static const struct inode_operations kvm_gmem_iops = { .setattr = kvm_gmem_setattr, }; -static int kvm_gmem_hugetlb_setup(struct inode *inode, loff_t size, u64 flags) +static int kvm_gmem_hugetlb_setup(struct inode *inode, + struct kvm_gmem_inode_private *private, + loff_t size, u64 flags) { struct kvm_gmem_hugetlb *hgmem; struct hugepage_subpool *spool; @@ -1036,6 +1109,10 @@ static int kvm_gmem_hugetlb_setup(struct inode *inode, loff_t size, u64 flags) struct hstate *h; long hpages; + hgmem = kzalloc(sizeof(*hgmem), GFP_KERNEL); + if (!hgmem) + return -ENOMEM; + page_size_log = (flags >> KVM_GUEST_MEMFD_HUGE_SHIFT) & KVM_GUEST_MEMFD_HUGE_MASK; h = hstate_sizelog(page_size_log); @@ -1046,21 +1123,16 @@ static int kvm_gmem_hugetlb_setup(struct inode *inode, loff_t size, u64 flags) if (!spool) goto err; - hgmem = kzalloc(sizeof(*hgmem), GFP_KERNEL); - if (!hgmem) - goto err_subpool; - inode->i_blkbits = huge_page_shift(h); hgmem->h = h; hgmem->spool = spool; - inode->i_mapping->i_private_data = hgmem; + private->hgmem = hgmem; return 0; -err_subpool: - kfree(spool); err: + kfree(hgmem); return -ENOMEM; } @@ -1068,6 +1140,7 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, loff_t size, u64 flags) { const struct qstr qname = QSTR_INIT(name, strlen(name)); + struct kvm_gmem_inode_private *private; struct inode *inode; int err; @@ -1079,12 +1152,20 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, if (err) goto out; + err = -ENOMEM; + private = kzalloc(sizeof(*private), GFP_KERNEL); + if (!private) + goto out; + if (flags & KVM_GUEST_MEMFD_HUGETLB) { - err = kvm_gmem_hugetlb_setup(inode, size, flags); + err = kvm_gmem_hugetlb_setup(inode, private, size, flags); if (err) - goto out; + goto free_private; } + xa_init(&private->faultability); + inode->i_mapping->i_private_data = private; + inode->i_private = (void *)(unsigned long)flags; inode->i_op = &kvm_gmem_iops; inode->i_mapping->a_ops = &kvm_gmem_aops; @@ -1097,6 +1178,8 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, return inode; +free_private: + kfree(private); out: iput(inode);