From patchwork Fri Nov 29 12:39:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 13888633 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 8986CD6EC04 for ; Fri, 29 Nov 2024 12:39:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 023AA6B008C; Fri, 29 Nov 2024 07:39:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F15886B0092; Fri, 29 Nov 2024 07:39:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDD666B0093; Fri, 29 Nov 2024 07:39:54 -0500 (EST) 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 BD2CA6B008C for ; Fri, 29 Nov 2024 07:39:54 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 37AA240EA8 for ; Fri, 29 Nov 2024 12:39:54 +0000 (UTC) X-FDA: 82839089424.29.C9F28B3 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by imf02.hostedemail.com (Postfix) with ESMTP id C30A58000E for ; Fri, 29 Nov 2024 12:39:36 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=mJVBKR0v; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf02.hostedemail.com: domain of "prvs=0560a7136=kalyazin@amazon.co.uk" designates 52.95.49.90 as permitted sender) smtp.mailfrom="prvs=0560a7136=kalyazin@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732883984; a=rsa-sha256; cv=none; b=1F0LhuHMcztvjhreEge0QBwWSL4P40xvUq7QNvugjc32oGb/Ck1eYYCpWb+amQliP9xaCF fL2NmDF3SR82Q3xRyT/ur2mnbNUevCpxQ3D0i6aTKDCWLP0H6zHZ1osGFABzik6EwUlEOU FP7ZfM3WEKMy+79JJytvcCsZRSAF5wU= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=mJVBKR0v; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf02.hostedemail.com: domain of "prvs=0560a7136=kalyazin@amazon.co.uk" designates 52.95.49.90 as permitted sender) smtp.mailfrom="prvs=0560a7136=kalyazin@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732883984; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=pruxSJBO+jg2vTrKWukJWri3CAYz3t7ISP8fRBBPjl8=; b=4Ouuf9cEvKQVtI2+jkwNZUBnwNe8+gOxHNHIEw3+FQPnrUXuAl8HTTx3k0tYSdpvfCm8Xj 05Qgu3h65K5T3TGYZMtDklaJIJh3vGZql3qN4Q4H6tVNb2A4qi/rK73K8sNLgAfE/8SRSk hUHuQ0DHXFG+ZB/dtTt9x/YhyH8YW7s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1732883993; x=1764419993; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pruxSJBO+jg2vTrKWukJWri3CAYz3t7ISP8fRBBPjl8=; b=mJVBKR0vws3zANPD2VVYcF7J+adO0l7dNiq+SC26Uawswttn5bBO5k7+ sN5qTFGeNR1a8eyEIslFhETd6RSK1zKT643MMvfvELI8hEvXOTSZw8eFF FUYLmrpSHGOvO1yi/kc9hgWmiidXXZyaSsiRqh4u3W33NsfpjnNMJt3Ww 4=; X-IronPort-AV: E=Sophos;i="6.12,195,1728950400"; d="scan'208";a="452145714" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.124.125.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2024 12:39:49 +0000 Received: from EX19MTAUWB001.ant.amazon.com [10.0.7.35:13724] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.32.230:2525] with esmtp (Farcaster) id cf81e0c5-4a2e-4985-891e-674da5678ac5; Fri, 29 Nov 2024 12:39:48 +0000 (UTC) X-Farcaster-Flow-ID: cf81e0c5-4a2e-4985-891e-674da5678ac5 Received: from EX19D003UWB001.ant.amazon.com (10.13.138.92) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Fri, 29 Nov 2024 12:39:47 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D003UWB001.ant.amazon.com (10.13.138.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.35; Fri, 29 Nov 2024 12:39:47 +0000 Received: from email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (10.25.36.214) by mail-relay.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Fri, 29 Nov 2024 12:39:47 +0000 Received: from dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com (dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com [172.19.103.116]) by email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (Postfix) with ESMTPS id B78B0406EB; Fri, 29 Nov 2024 12:39:44 +0000 (UTC) From: Nikita Kalyazin To: , , , , CC: , , , , , , , , , , , , , Subject: [RFC PATCH v2 1/2] KVM: guest_memfd: add generic population via write Date: Fri, 29 Nov 2024 12:39:28 +0000 Message-ID: <20241129123929.64790-2-kalyazin@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20241129123929.64790-1-kalyazin@amazon.com> References: <20241129123929.64790-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C30A58000E X-Stat-Signature: xu4sio7bsjryrfm5p3d5mfqykyawjt6p X-Rspam-User: X-HE-Tag: 1732883976-615477 X-HE-Meta: U2FsdGVkX18+q+p3kx76fy06ZxmsE9UgE3xKitHKwDuxFFqlTwAHLaEj4+Am5DhPMcmelyDYOvdv7JwhKZzt4hsZ6MJ0sNU8OOKFC+lO/IqQTmjhGWdjpjw6sXY3lO+nLp6xs87ptPHH+ftJl+JDAIlE/0+llHigo15/Y6JSOMhHycA0Q6m0MOOL5RWVA+fv6DaIHDWqQRqNUi8ZGjdjWmDxVlgCOdKb1GOjO9OgkvHwKtkRoBe7M1l6eUtcVOZi4xS2IPBLNgydbopaP5n9k1u03Le2AAYipjRQ9a1gX7MOxmk6LXoa6Cwc2BR9vW0YbT6C3Xs0E5Fm+JaHIPHXEpt26PNYYk04AChLkAWyjwNKLkU+wWrd9tMgWC0Im9jFz9f7JxqC7q6vQGOqVnyOazOwva3y4oVPTGv8iC93h1Al10S7HojNpkeCF0yhuRTxVoQS3TtuEE0JM3gOQw3I1IWTbE4jBwzNAv+Z0bc+C9wbxUlUBtBCYFSOdHuF/cGB7Sf50dLuQiSlNnoxfb4byHmeiKpt1s/FZ+O5+Xw/hvZarqIsImlYURU8KaW/VevBZG1fsqEuWdXWzUN05hEFa1TO1pu+/vay9HxDJXq1Sr77umKpulqB9Tr0AvojdVrufliWmetMIACWT82Gn0+tnw5+f33WkT0I2l3hkAUgwbLMNfeFClvJ6SbWpZul8kuZl/JfK2z0yx7kZpkCAn6Ebyuey70SA4WolEUGQwfZXmKcQJLi+C4ZcsOTQ5LbGbzaKA4DDBVGMfalezg+I5gWabhqO0MICHPsBJau8IE2V9g/fYvVRCauvUNXqAyPwMSZ3dL7aAe0iWBGoi2Ov8r6tD3bTvGJdR7qvqO1dyzMhPXI/9vPVWXQOyxQyblNtgEAJC+JSi8S8YS1FsFFXyUFCCMCQ9GrDUBf0NxpHOnYS1gYfjeUKL5RhlcL4qVyZ5CjrWyPy63r89yylBmcVKm wH9DU9BF palOMJwLOPBb/JbyU3vymcsO7rk5naF/xQ1YwYxzic33wA2YTRQr2OIPCycW/Ya00RYzhhG9sP1Zv5RKKTAAVgBy6RUd7cX3mSjCMlW+V8yBIuvs2BQ5i57xfjxDZ91YfVlNbKgen/ah188QfHieRukS6X2/9EsKxnAgkoN+NtEzkFo/JKteWk4rhF8HEl++IUqR2FhSjyBkK5zpNRNvFU2QY0CwULFar+DnryQf3HuG8dWPPyO3M2MGztY8ClMZfIcT6aUpPbKqoknEAJOpBHtHqtmga5z+qRNxOuaNOdM67+RVTV7mEb2GVyy+7BbtkeFdNavGiIS++MKFYG/QccDrSs02pFTav80m7hFds6JvrINDRcSRIeiQYB+gG56TsWfTa6nBVzjtpLK/8L3OkzvN11D/aHY2BGuqSyw/BaXsurdQfur5kpW3ywxV9xBQ7MnMku7y/Wkk20Q5mxDkKUW9SSr5JADiGmRpegWs6rEISe36aX48MtNXUwZS6wqIpOW0qUgi1h3wl0Kf4jGUeXMsWyoR+P2llyAIj 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: write syscall populates guest_memfd with user-supplied data in a generic way, ie no vendor-specific preparation is performed. This is supposed to be used in non-CoCo setups where guest memory is not hardware-encrypted. The following behaviour is implemented: - only page-aligned count and offset are allowed - if the memory is already allocated, the call will successfully populate it - if the memory is not allocated, the call will both allocate and populate - if the memory is already populated, the call will not repopulate it Signed-off-by: Nikita Kalyazin --- virt/kvm/guest_memfd.c | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 47a9f68f7b24..e80566ef56e9 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -102,6 +102,80 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index) return filemap_grab_folio(inode->i_mapping, index); } +#if defined(CONFIG_KVM_GENERIC_PRIVATE_MEM) && !defined(CONFIG_KVM_AMD_SEV) +static ssize_t kvm_kmem_gmem_write(struct file *file, const char __user *buf, + size_t count, loff_t *offset) +{ + pgoff_t start, end, index; + ssize_t ret = 0; + + if (!PAGE_ALIGNED(*offset) || !PAGE_ALIGNED(count)) + return -EINVAL; + + if (*offset + count > i_size_read(file_inode(file))) + return -EINVAL; + + if (!buf) + return -EINVAL; + + start = *offset >> PAGE_SHIFT; + end = (*offset + count) >> PAGE_SHIFT; + + filemap_invalidate_lock(file->f_mapping); + + for (index = start; index < end; ) { + struct folio *folio; + void *vaddr; + pgoff_t buf_offset = (index - start) << PAGE_SHIFT; + + if (signal_pending(current)) { + ret = -EINTR; + goto out; + } + + folio = kvm_gmem_get_folio(file_inode(file), index); + if (IS_ERR(folio)) { + ret = -EFAULT; + goto out; + } + + if (folio_test_hwpoison(folio)) { + folio_unlock(folio); + folio_put(folio); + ret = -EFAULT; + goto out; + } + + if (folio_test_uptodate(folio)) { + folio_unlock(folio); + folio_put(folio); + ret = -ENOSPC; + goto out; + } + + folio_unlock(folio); + + vaddr = kmap_local_folio(folio, 0); + ret = copy_from_user(vaddr, buf + buf_offset, PAGE_SIZE); + if (ret) + ret = -EINVAL; + kunmap_local(vaddr); + + kvm_gmem_mark_prepared(folio); + folio_put(folio); + + index = folio_next_index(folio); + *offset += PAGE_SIZE; + } + +out: + filemap_invalidate_unlock(file->f_mapping); + + return ret && start == (*offset >> PAGE_SHIFT) ? + ret : *offset - (start << PAGE_SHIFT); +} +#endif + static void kvm_gmem_invalidate_begin(struct kvm_gmem *gmem, pgoff_t start, pgoff_t end) { @@ -308,6 +382,10 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) } static struct file_operations kvm_gmem_fops = { +#if defined(CONFIG_KVM_GENERIC_PRIVATE_MEM) && !defined(CONFIG_KVM_AMD_SEV) + .llseek = default_llseek, + .write = kvm_kmem_gmem_write, +#endif .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate, @@ -423,6 +501,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) } file->f_flags |= O_LARGEFILE; + file->f_mode |= FMODE_LSEEK | FMODE_PWRITE; inode = file->f_inode; WARN_ON(file->f_mapping != inode->i_mapping); From patchwork Fri Nov 29 12:39:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 13888634 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 8739CD6EC04 for ; Fri, 29 Nov 2024 12:40:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 245B96B0093; Fri, 29 Nov 2024 07:40:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21E236B0095; Fri, 29 Nov 2024 07:40:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E5836B0096; Fri, 29 Nov 2024 07:40:09 -0500 (EST) 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 E4A3C6B0093 for ; Fri, 29 Nov 2024 07:40:08 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9C09940D10 for ; Fri, 29 Nov 2024 12:40:08 +0000 (UTC) X-FDA: 82839089718.03.762385C Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) by imf27.hostedemail.com (Postfix) with ESMTP id 1E9D840020 for ; Fri, 29 Nov 2024 12:39:54 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=UDblTZWb; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf27.hostedemail.com: domain of "prvs=0560a7136=kalyazin@amazon.co.uk" designates 52.95.48.154 as permitted sender) smtp.mailfrom="prvs=0560a7136=kalyazin@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732884002; a=rsa-sha256; cv=none; b=BoTUSnDnBqesH9Q6dt5l9eyckrNXhVPO9ZLWTu/gfn0ALQ3J1BXzIjtkHMkKnpNYCiZzok dNIZl0HfrOGjmdI6cKhQaQ1uTliA+EqvAZT4A3WEmopg95fGh9RN7SZxMP2092MPF36AF+ vqKRknHv7B1CyGtQ2Y+CQNEczrS/sP8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=UDblTZWb; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf27.hostedemail.com: domain of "prvs=0560a7136=kalyazin@amazon.co.uk" designates 52.95.48.154 as permitted sender) smtp.mailfrom="prvs=0560a7136=kalyazin@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732884002; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5pUJY8dTjFf+Uw5/C4D+44Y0A859zY36PuFsL3IHnPw=; b=O91N0xu82cDh8oDwDSQj2Ra4KOfRGSyM9NcKcA3pbfagjlEix+ExlyMPZNadIbaAUqv8vO U4oUT8s1MWXIfpWQdCMcZQrL46Ayenf/ec7emM5Wt49ZpEapqDLvu6HGAHIFxzmgypDoj1 Ih3NYzzWtAQQbc3iSnZs/QHzMNrNxCk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1732884006; x=1764420006; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5pUJY8dTjFf+Uw5/C4D+44Y0A859zY36PuFsL3IHnPw=; b=UDblTZWbqV7zdRKHXI/fiZ9aqO2grdRLTCL2oAeOm0iAZIaSJ1aQIXQ2 5RIbAkvxLpBmiuM8VTtawfmjHSWvcZZIY5tcgoFGFZaY7QTBEH+hTXZ9q tNm2t6HqP6f0j543dR7Bbvj0B66w3InGWR5ndhcyRXGKIdvgNO5jGh96g 4=; X-IronPort-AV: E=Sophos;i="6.12,195,1728950400"; d="scan'208";a="441805293" Received: from iad6-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.124.125.2]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2024 12:40:04 +0000 Received: from EX19MTAEUB002.ant.amazon.com [10.0.43.254:12475] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.23.253:2525] with esmtp (Farcaster) id 4a00d189-7d5b-47bc-ae41-4835ef36fc4b; Fri, 29 Nov 2024 12:40:03 +0000 (UTC) X-Farcaster-Flow-ID: 4a00d189-7d5b-47bc-ae41-4835ef36fc4b Received: from EX19D004EUC001.ant.amazon.com (10.252.51.190) by EX19MTAEUB002.ant.amazon.com (10.252.51.59) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Fri, 29 Nov 2024 12:40:02 +0000 Received: from EX19MTAUEB002.ant.amazon.com (10.252.135.47) by EX19D004EUC001.ant.amazon.com (10.252.51.190) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Fri, 29 Nov 2024 12:40:01 +0000 Received: from email-imr-corp-prod-pdx-1box-2b-8c2c6aed.us-west-2.amazon.com (10.124.125.2) by mail-relay.amazon.com (10.252.135.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Fri, 29 Nov 2024 12:40:01 +0000 Received: from dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com (dev-dsk-kalyazin-1a-a12e27e2.eu-west-1.amazon.com [172.19.103.116]) by email-imr-corp-prod-pdx-1box-2b-8c2c6aed.us-west-2.amazon.com (Postfix) with ESMTPS id 6C72CA0379; Fri, 29 Nov 2024 12:39:58 +0000 (UTC) From: Nikita Kalyazin To: , , , , CC: , , , , , , , , , , , , , Subject: [RFC PATCH v2 2/2] KVM: selftests: update guest_memfd write tests Date: Fri, 29 Nov 2024 12:39:29 +0000 Message-ID: <20241129123929.64790-3-kalyazin@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20241129123929.64790-1-kalyazin@amazon.com> References: <20241129123929.64790-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1E9D840020 X-Stat-Signature: z8uxjgxgap7n7gfypj3p8rxaawjkwgi5 X-HE-Tag: 1732883994-451495 X-HE-Meta: U2FsdGVkX1/1G378ZJpdB9Spue65GjEXQY8vFSEu0/yUBz/ISMvmAXJ1buIcR7y+WZRUNwU5C98EGeJa03tkpYsGQo4bavYxrZryRh3SnKdF08taWnk3PplLVE5QU5aN/O1PcptVrkoDr2U9aWDJWgVSeAIPrrmF2A7HmP5NVys50vXlQ0HJbtkJXjYf1/sETnLUQURj++qc08pb8OWLiBtKgom3aJgl/EElz4w+GmWR/d3qwm39e01oURBVCwquNe7j0ZRPi4ILcp4+iKVXCwp/hz+98H69cqY3lV5UjwW59Ju3y3MZp3mBuffsLhrDaJysz4dMCgaVldmmDp2nydCjcFJ8LaiRuzicNcx4EltD6qIrGaOzC4VkdHbA9lv3Cq7vlf7vi9eGSfheSXzYMUfnN0iaB/GKK5tBKJtrrDUyNzcSTNK5NzykggURVn4upQ/Ie5XLJPliilVtvL7YUiSX0PpdiPyxTOyVKH0ZJh1d6Sdup62SkyZFrseseev8EYRZXJrIwqZOIVaPVjljlrX2jmhMi0dfB8F3UwPgv20xM3C9SpmzTI+UZMSM6S78Cu5rUNgZfGilAcLiP8dz0PxJb/BgYIfOhyeKFqEKyWwYhWLuVXDrrZBr7NQ3dX6WtUSXst/nJ4zMimTBmuzEt6JDR8pEXUBgMxmi1P7YGMjUvui+69iGNp3tO6IrU4Z0i/T4F2yv3k0RQcjcytbZIT5APl4f24zx4QC7jDfJI01mlfU4Oej/gJYz54ybTGF/KD7XitKddWmtjZAk0hkRgi90K49ND9+QzXvReunmWSL2Ba8ium5664TqKZgLfqBxeniTMRN4KY0Ytc0wHxp5LyvGJHgyG0i5f2dWg1MHQ1hwZvXOkGmQrP6F6AKrI9GG8j0tBr/viYHe4HtiXXIxl2o+t2cCnMHBKjObJPY0gy21ENiRMkmpa0GcZNiqAet7LV0YABTewX6cQGq6eZ/ aGktROtg 6Zqj5wZyup0Z2VCqE7Cur7VN4apKUy/zNN8wkZGCUlEXBRfXZafhfemhdw5nE7ew5OTJXPbyxedEtkKec1FFhmlmKnO0mOXk/mAKbzVgjPzSWIyE/krOhuDnJp/9ghhyjM7bsetFF3UBQzgNAmKprL0N23huoYWaO+tISClWusjL0V044gS7y+bv1JZ73z9isbNifkv8cUax8HO8K0YcmWaSv2oA26CgEDltrR0jaAg/n+m+0kBrlsknunaeAw2cUaxKT5ZGbqSc8Fa057aXv6IZGa5+N4pLaqNpNj0BxIWGM+Nelegb2ozP7ZAYw2t7KroYrhIMVjp8cRwFEkH2AtrVeEXY7P4d9vWqbg3fDT/B1nG3f8G8f4TDgPCtD3nRRLsMOvro1jvoos1J6mfXcNX9VNYOMrVkdMLkf1oUebuTDg8pLMR6XwJWVMjQDg5jfOv80++dosZgJhhs4+1ryJMMXKz1OcYZ8EgUVCF9/jYye3PxoQKKJMF/Z1j2eXWQCYTElp1I8XpZ8B6OsCfbcmgCdTR6+GU/2NWsBY+eaYJWK/0A= 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: This is to reflect that the write syscall is now implemented for guest_memfd. Signed-off-by: Nikita Kalyazin --- .../testing/selftests/kvm/guest_memfd_test.c | 85 +++++++++++++++++-- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index ce687f8d248f..e10d0f51da93 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -20,18 +20,90 @@ #include "kvm_util.h" #include "test_util.h" -static void test_file_read_write(int fd) +static void test_file_read(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_file_write(int fd, size_t total_size) +{ + size_t page_size = getpagesize(); + void *buf = NULL; + int ret; + + ret = posix_memalign(&buf, page_size, total_size); + TEST_ASSERT_EQ(ret, 0); + + /* Check arguments correctness checks work as expected */ + + ret = pwrite(fd, buf, page_size - 1, 0); + TEST_ASSERT(ret == -1, "write unaligned count on a guest_mem fd should fail"); + TEST_ASSERT_EQ(errno, EINVAL); + + ret = pwrite(fd, buf, page_size, 1); + TEST_ASSERT(ret == -1, "write unaligned offset on a guest_mem fd should fail"); + TEST_ASSERT_EQ(errno, EINVAL); + + ret = pwrite(fd, buf, page_size, total_size); + TEST_ASSERT(ret == -1, "writing past the file size on a guest_mem fd should fail"); + TEST_ASSERT_EQ(errno, EINVAL); + + ret = pwrite(fd, NULL, page_size, 0); + TEST_ASSERT(ret == -1, "supplying a NULL buffer when writing a guest_mem fd should fail"); + TEST_ASSERT_EQ(errno, EINVAL); + + /* Check double population is not allowed */ + + ret = pwrite(fd, buf, page_size, 0); + TEST_ASSERT(ret == page_size, "page-aligned write on a guest_mem fd should succeed"); + + ret = pwrite(fd, buf, page_size, 0); + TEST_ASSERT(ret == -1, "write on already populated guest_mem fd should fail"); + TEST_ASSERT_EQ(errno, ENOSPC); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed"); + + /* Check population is allowed again after punching a hole */ + + ret = pwrite(fd, buf, page_size, 0); + TEST_ASSERT(ret == page_size, "page-aligned write on a punched guest_mem fd should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed"); + + /* Check population of already allocated memory is allowed */ + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, page_size); + TEST_ASSERT(!ret, "fallocate with aligned offset and size should succeed"); + + ret = pwrite(fd, buf, page_size, 0); + TEST_ASSERT(ret == page_size, "write on a preallocated guest_mem fd should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed"); + + /* Check population works until an already populated page is encountered */ + + ret = pwrite(fd, buf, total_size, 0); + TEST_ASSERT(ret == total_size, "page-aligned write on a guest_mem fd should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed"); + + ret = pwrite(fd, buf, total_size, 0); + TEST_ASSERT(ret == page_size, "write on a guest_mem fd should not overwrite data"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, total_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed"); + + + free(buf); } static void test_mmap(int fd, size_t page_size) @@ -189,7 +261,8 @@ int main(int argc, char *argv[]) fd = vm_create_guest_memfd(vm, total_size, 0); - test_file_read_write(fd); + test_file_read(fd); + test_file_write(fd, total_size); test_mmap(fd, page_size); test_file_size(fd, page_size, total_size); test_fallocate(fd, page_size, total_size);