From patchwork Mon Mar 3 13:30:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 13998835 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 C355FC282CD for ; Mon, 3 Mar 2025 13:30:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 463586B0089; Mon, 3 Mar 2025 08:30:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 411D36B008A; Mon, 3 Mar 2025 08:30:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D9DC6B008C; Mon, 3 Mar 2025 08:30:28 -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 0D5476B0089 for ; Mon, 3 Mar 2025 08:30:28 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CE342B329E for ; Mon, 3 Mar 2025 13:30:27 +0000 (UTC) X-FDA: 83180324094.07.0726765 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf27.hostedemail.com (Postfix) with ESMTP id 4FFC240017 for ; Mon, 3 Mar 2025 13:30:25 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=isTZELwa; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf27.hostedemail.com: domain of "prvs=150a0b0d2=kalyazin@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=150a0b0d2=kalyazin@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1741008625; 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: references:dkim-signature; bh=2L3Lyb/8WcMt5Xzo+0pszQ2qfPvOzityflFvXPF/tvA=; b=UcqxCEjTASkl8DZfC1MP+pXEiLh6q9Pd2VO9eTDiF3F9ahuK+l8jaxIM83YXzzdaEw8+zl j8fyB86fK9e8LcQuZiSWVFolFntPGi1t5yNnlHnnhcvuXy8gj4axPOB1LeHsFuXQFtxsnt Bk0w8efg6pDSiDYSUFaLh1ACPbQsz3w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1741008625; a=rsa-sha256; cv=none; b=0EyCAng6xdMTGOTRqowYWaDnytjcKYrmuyQsbLuxfX67h4hg85guyF2FV5c5eCXZr8Cjse fR5gu0ehel+L8SF+3XvTyMzIdWN5WtZ7RWZZM6/qMmvwtC2qXl66L6AxEnk2hR4DasJ0Fy bL8yS94i1rbDoSdGJ1iaV88baSp/PO0= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=isTZELwa; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf27.hostedemail.com: domain of "prvs=150a0b0d2=kalyazin@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=150a0b0d2=kalyazin@amazon.co.uk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1741008625; x=1772544625; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=2L3Lyb/8WcMt5Xzo+0pszQ2qfPvOzityflFvXPF/tvA=; b=isTZELwagn3Nc1gArl2OqcyoTK3wWK5Vo/p+k/zjrjyLZNsZ8yQ6Pclq My4ePkWBYYPZaLqwfkm+WUJDvc+AkxSCkjILA/Y6mXX5dzNhqif1raU/7 3+L1FcHJ8f8xI2x5/WBh+KTboCp9TaWzS1s3vv81xnVFDk0dmydaWnDqC 8=; X-IronPort-AV: E=Sophos;i="6.13,329,1732579200"; d="scan'208";a="498891611" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Mar 2025 13:30:18 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.7.35:4349] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.53.36:2525] with esmtp (Farcaster) id 30b52795-a314-42f7-b716-d33823c57578; Mon, 3 Mar 2025 13:30:18 +0000 (UTC) X-Farcaster-Flow-ID: 30b52795-a314-42f7-b716-d33823c57578 Received: from EX19D020UWA001.ant.amazon.com (10.13.138.249) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Mon, 3 Mar 2025 13:30:15 +0000 Received: from EX19MTAUWC002.ant.amazon.com (10.250.64.143) by EX19D020UWA001.ant.amazon.com (10.13.138.249) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Mon, 3 Mar 2025 13:30:15 +0000 Received: from email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (10.25.36.210) by mail-relay.amazon.com (10.250.64.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14 via Frontend Transport; Mon, 3 Mar 2025 13:30:15 +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 014DD40434; Mon, 3 Mar 2025 13:30:12 +0000 (UTC) From: Nikita Kalyazin To: , , CC: , , , , , , , , , , , , , , , , Subject: [RFC PATCH 0/5] KVM: guest_memfd: support for uffd missing Date: Mon, 3 Mar 2025 13:30:06 +0000 Message-ID: <20250303133011.44095-1-kalyazin@amazon.com> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 4FFC240017 X-Stat-Signature: j7hq3h1zw893de1qmsbijxhodadyi3m5 X-HE-Tag: 1741008625-980010 X-HE-Meta: U2FsdGVkX19CUn1hgkUn73ShHaJYfsaJrD8zPIpy+gAJdx3WfpTEbWI5urEFWDaK4PKq/d23yFPLaDtvw2BAgOo6u6RgNDULTMhzs8CugO3T0ARCEmG/TG0vL7kDGsoXpUgqw2s9JWGy1a1FG/+N+1Uq/9CQR1+aIdCE7SYL55zKLON2vdwsjg1zLmbd6n5SIzd94u0QW24f314mvwGB3D8D5gZ9KZASYVnxZK6KLAP4iZiluP3HR6MrBVToMQCQENhjiBFiqPlCcVSHkOW8ABcdEeUtGFdBynyDaqiPGUdxvAg85QuyOFPvVDZ2ydE9wJYHL+YedTWGXKJ6z5CAd2badT/kdEPzzAL8ubLuDGYu5mmf7VMNFqdgAhTCXgZ3kEfAZvAAQUrpz6aBW35iUtY6y6ed798UTbmECG6xGodin+D/thyWxetTRHON62wZGBxP1ahRj8133jEfjex8JyLPo+YZAThk3Q2k80Sb/vCpaRoYUBd+l7zBRR91jKrMK0FcW8GQUCsS6U5+nem+p1sU+e2XESD1oJ+L/kHhlabCxAnxpRNtFbUbWVxqYGsFpdlwlvv9ehNJqcfIL/Hq01JvNtbHvMz+lFgqoTDt64SRhh1wTtlNOl8xH2IQNpr1d8OBxQvG7N10/Txthk5xOlTu9qGaE9Q++SbaJbfEyjnUTPfmCbj8ktAlhmMuwQj43DASND3+mxoW5ESXc+HGs0oJFvJB5xA7CTdjwmw5IQiczKBTCCjZBXO4vRCLvcHFxG6O/lQMP8dCt4zpSR5SH+dQwBlBo7x6luTcO1ijkmmpee84I43dyx8Lm6MYMe9Try9TvQnRBBScuamuaheEezO0PQXdKSN5EnFsxREZRJwMiHnvhd3Ug26TzoSwe4ruosWWS7FaqTpRy6wEv28+F7V9gzVKqRlouQnusj7DF0P1+IN2V4lGqYUN7Ag0S6Sfj3oooz/SShImMfODRXe gUmQS+ra H+JiLtMa/fbP31Dj5wWmOX5V2qbJVYe4yO0cRAH+CM/3iFJQs6FerXD3VkekUsrcmpNEUPH/DS28NhIApTGgZxgW4vXEUu3BRZLxCcmIsREH4gUQ6I+sZunWvEdqYvlEyZZaQ93s8kJW0/S74rhVOZTSRhZE0qym0N8oFSfUgC5dc4NV7O3kPPJkQ13XErEjTPVIhrlkpKy7HIcxwRoNDFEonVFxkxXBTEVBbQtSiXh28XjQrfvzyj+FQXDiWK7ekD4RMOILl4WuAGDSyiEQN99zrqwP4ETGAMynOLVXB3labXJLKmmtKKGYTx2XJDW5NjVtjk+TEsUDNtGsj+fcuQdO4KZ8P4rOx4spQ821ZDyjRnv4CreJq4yL8R24NkVe+D3ODlHtBfukAJnWb1pSZ+lakW7jdv6nKAdVSuQXEzt0Bf0PL9LWw0If2l04Haqa3TgdBWoueVd2BOqEuyd9XD8FSZeb3N+nmu+LVkArJMsw3qoN/Rc2orOvUqoqvNT46ac7cAQRlFP6IHsk3EaN0W4pjt0wW5LL6T4iMr4j52/tSdKuFLsr2MHiHW4Xg7Q+EiwC85hdO+7kasxomXXnWO4pdwtonjPXPay1fO2tCjJA/uHpckHDoCWDDzihEU1GNbMX/tu+om8kSy49ord1ouOMSoxOsDf2qeB8huP3PRRY/j1837+VI6DlV3VgDARxpRG1R X-Bogosity: Ham, tests=bogofilter, spamicity=0.013004, 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 series is built on top of the v3 write syscall support [1]. With James's KVM userfault [2], it is possible to handle stage-2 faults in guest_memfd in userspace. However, KVM itself also triggers faults in guest_memfd in some cases, for example: PV interfaces like kvmclock, PV EOI and page table walking code when fetching the MMIO instruction on x86. It was agreed in the guest_memfd upstream call on 23 Jan 2025 [3] that KVM would be accessing those pages via userspace page tables. In order for such faults to be handled in userspace, guest_memfd needs to support userfaultfd. This series proposes a limited support for userfaultfd in guest_memfd: - userfaultfd support is conditional to `CONFIG_KVM_GMEM_SHARED_MEM` (as is fault support in general) - Only `page missing` event is currently supported - Userspace is supposed to respond to the event with the `write` syscall followed by `UFFDIO_CONTINUE` ioctl to unblock the faulting process. Note that we can't use `UFFDIO_COPY` here because userfaulfd code does not know how to prepare guest_memfd pages, eg remove them from direct map [4]. Not included in this series: - Proper interface for userfaultfd to recognise guest_memfd mappings - Proper handling of truncation cases after locking the page Request for comments: - Is it a sensible workflow for guest_memfd to resolve a userfault `page missing` event with `write` syscall + `UFFDIO_CONTINUE`? One of the alternatives is teaching `UFFDIO_COPY` how to deal with guest_memfd pages. - What is a way forward to make userfaultfd code aware of guest_memfd? I saw that Patrick hit a somewhat similar problem in [5] when trying to use direct map manipulation functions in KVM and was pointed by David at Elliot's guestmem library [6] that might include a shim for that. Would the library be the right place to expose required interfaces like `vma_is_gmem`? Nikita [1] https://lore.kernel.org/kvm/20250303130838.28812-1-kalyazin@amazon.com/T/ [2] https://lore.kernel.org/kvm/20250109204929.1106563-1-jthoughton@google.com/T/ [3] https://docs.google.com/document/d/1M6766BzdY1Lhk7LiR5IqVR8B8mG3cr-cxTxOrAosPOk/edit?tab=t.0#heading=h.w1126rgli5e3 [4] https://lore.kernel.org/kvm/20250221160728.1584559-1-roypat@amazon.co.uk/T/ [4] https://lore.kernel.org/kvm/20250221160728.1584559-1-roypat@amazon.co.uk/T/#ma130b29c130dbdc894aa08d8d56c16ec383f36dd [5] https://lore.kernel.org/kvm/20241122-guestmem-library-v5-2-450e92951a15@quicinc.com/T/ Nikita Kalyazin (5): KVM: guest_memfd: add kvm_gmem_vma_is_gmem KVM: guest_memfd: add support for uffd missing mm: userfaultfd: allow to register userfaultfd for guest_memfd mm: userfaultfd: support continue for guest_memfd KVM: selftests: add uffd missing test for guest_memfd include/linux/userfaultfd_k.h | 9 ++ mm/userfaultfd.c | 23 ++++- .../testing/selftests/kvm/guest_memfd_test.c | 88 +++++++++++++++++++ virt/kvm/guest_memfd.c | 17 +++- virt/kvm/kvm_mm.h | 1 + 5 files changed, 136 insertions(+), 2 deletions(-) base-commit: 592e7531753dc4b711f96cd1daf808fd493d3223