From patchwork Fri Apr 4 15:43:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 14038635 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 2185EC36010 for ; Fri, 4 Apr 2025 15:44:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A8336B0027; Fri, 4 Apr 2025 11:44:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4554A6B0028; Fri, 4 Apr 2025 11:44:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 343EA6B0029; Fri, 4 Apr 2025 11:44:21 -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 13A7C6B0027 for ; Fri, 4 Apr 2025 11:44:21 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C9574804A2 for ; Fri, 4 Apr 2025 15:44:21 +0000 (UTC) X-FDA: 83296783122.28.BA241D4 Received: from smtp-fw-9105.amazon.com (smtp-fw-9105.amazon.com [207.171.188.204]) by imf16.hostedemail.com (Postfix) with ESMTP id A012D18000A for ; Fri, 4 Apr 2025 15:44:19 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=KRg8Xetb; spf=pass (imf16.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 207.171.188.204 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743781459; 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=4KrCtE/sXZ3BNNe21aeAaGtv43DEVIENoUwdd2sqOFQ=; b=Gy+AW9bNKeE//p5+msE4Vgw6evyak6TVvhBrSLM+BJF6Pj/74K6BpRGxnN3Vot2wLyt6Cu 651CDAa73RbzDUSa7BwJ82mSar170uZo/PCmKsMQ9bX4KPBfBvr1UmJH6Ku+K1BAGUWSQp Vrhaz/GtPztRs//TY1utdoiXQ5Ka6Ms= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743781459; a=rsa-sha256; cv=none; b=MUkzBum/RCVPaLWKmdh59yomd+5Ypz+MzqPt5AnlkTsUQeqg7CyRxLtCCyW858P4T8Zu/a 5ZYOU+FdvEiiA+MS2Nm5Mb6L2AAoEOm96UOLxKXzG5dbXzYwqT4V/Cc43EaIRsESPlDOma cZc2rUQxpRTUg7pA0GQUfa+4XXRNX34= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=KRg8Xetb; spf=pass (imf16.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 207.171.188.204 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1743781460; x=1775317460; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4KrCtE/sXZ3BNNe21aeAaGtv43DEVIENoUwdd2sqOFQ=; b=KRg8Xetb3PGol4iMwQf4x2wZ9jMGFS9oKiW7kglCvPXlctg4HUcOFIo8 XtMJ+RuTWmCuhdmZobx0Uh/DXWB8njX+hNpvJLOKJSDPb9QoYgKEWUJQg hjVkTN+ShOYVf3bNpsTmAE2bd+PmyGXaGJ9RJyYdomRyBhpRRQfzECjoB o=; X-IronPort-AV: E=Sophos;i="6.15,188,1739836800"; d="scan'208";a="7643503" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-9105.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2025 15:44:14 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.38.20:31324] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.62.147:2525] with esmtp (Farcaster) id ea24aab9-5b16-4f64-955a-2db6935ecc80; Fri, 4 Apr 2025 15:44:12 +0000 (UTC) X-Farcaster-Flow-ID: ea24aab9-5b16-4f64-955a-2db6935ecc80 Received: from EX19D020UWA004.ant.amazon.com (10.13.138.231) by EX19MTAUWA001.ant.amazon.com (10.250.64.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:44:08 +0000 Received: from EX19MTAUEA001.ant.amazon.com (10.252.134.203) by EX19D020UWA004.ant.amazon.com (10.13.138.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:44:07 +0000 Received: from email-imr-corp-prod-iad-all-1b-85daddd1.us-east-1.amazon.com (10.43.8.2) by mail-relay.amazon.com (10.252.134.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14 via Frontend Transport; Fri, 4 Apr 2025 15:44:07 +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-iad-all-1b-85daddd1.us-east-1.amazon.com (Postfix) with ESMTPS id A7CCB41FBE; Fri, 4 Apr 2025 15:44:05 +0000 (UTC) From: Nikita Kalyazin To: , , , , , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v3 1/6] mm: userfaultfd: generic continue for non hugetlbfs Date: Fri, 4 Apr 2025 15:43:47 +0000 Message-ID: <20250404154352.23078-2-kalyazin@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250404154352.23078-1-kalyazin@amazon.com> References: <20250404154352.23078-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: A012D18000A X-Stat-Signature: fkfkyx1tpb1ikywjrsw4n4ppuhcx84ho X-HE-Tag: 1743781459-589148 X-HE-Meta: U2FsdGVkX192qZqQUyQ/GSaRuKFJReWS4GNDA+EoHotIMJx47JIXvO1wfIroP7VsW8GknHzq88EiOEQSk75ah7x9FNsjEW8gX15VgVeygo/LhwyFoa47WZkFc2nu6WWDcJmR6jYi/4TE/0dmCpt89tSPiSd1Ja/iaqqTCxw/X+o+YGM0UnkgyJGq2wBqb0IHK/m/NDkST+bL0lJH/KShB5A8IGDry0BLDejcBP+gPHJkfFkTv0h7ImhQm5z/QFEzmhih2yosoujYDFO5ONNnZECV7KO8x/T3gDyqA/Vm9s5DX+d/ryQ+2i2Z0ouBxW/juhlDNm9uqW7DDHmZfOBA+Rdokv4esMrzzy/icNDcB0GaTjdyZEHSbKHrGYLOCb7BJC38Yre4W/BpObx/GX7EcV6yJp1L9ETENbj9yrgvaVfq+22zkmsNTw/ek9s/44tY6trg9/hrr9KjYMW+dyCSV7mDDCjtAxeTnDs8YpUdibZNbAB0ZG/sjOnirMOtPdWwS6RN8WuzNZZ1mDXTQG2THyjO00QV6aylplXcmUKaxN6L+HfeUFYrR42jTl67PFqgpXdqdgmXHrbvhSKsJVJp9mXZAp8P0zzQlzAJYQEn65mjYWwWrgDIslxIcdETKP45YKvZP+73m3eWGQODlX0sjFstW84tbAb4CaZeGM+O2BPGMa5pbxmlUrAvwTvRkE/r/cd2R+5dc6Ohgw4JCyVMXNieLeVItbauyyfQzs3kPCJx4xNwLLC0DXC9OfodQR0KPX+2IFoE4iOPJddTSQzmkWlbeaayayti5wzNgxwpwbOiyvw1ygYuiYLp3P7G+LOkqi/D+Xg1pbvt+610/DeemtabsG2xukudVZAoja9NQAF7iUaXbT0CMauH5m/s5gz3rU6o+p2VMzitYZeMZXbVRild1sO4EOefNZcMjBNkbwq9LDRuF84T26ZrjCR1gK2NymUpWApZXWbkxrpbiKf +q5KaoUM dGtqdyMVuWkMmodt2HhiEhgCcG5IDNFHTFJRd5UtEBv0Hpc3fESx1fC4b9y8Gq1ZNOMQl92jt1Ip8pFEr3UEVzIZZ5+FV0yZTy5B1X4rDglHTcOoYJHX3C5iTX8fThta7Uiyrn3D5UhYgKZYlcBs69p52uH/nxyQsPfCoWNye2Xb2HGLlxmfp6mxHZdW/x0/p7SoPEwKD/1cFtkLpG2H0PUh2oGJ/jmZ/VbN1BlJ99ihbvCqyHYXzoekd4N+bUr5SERRrfg52VnW4jNTVaDlvn9tYEUcaXbC6tUtUbsQXJmx7XDRKgFWJY/R/Iyt0AaQLJKL44xuw/e+aSplGC0eCWeiPB06luXLClUoC1j0jhnyzW5ynQwXY0WZrfUgadjW4AUw4VmGGvqO9NOXntRpF0USQrGSMBC28y6uDdkMphyapfY1rELuMRohTgYiDW+uuq0ImltfiR/otR19fLp0H3cVU9AqK7pndIY4LgwS9hFJzWDF4iJVUPm3IyxfYkOXpsdyJkcYsX/pOAdtx8XgD+lz10Q== 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: Remove shmem-specific code from UFFDIO_CONTINUE implementation for non-huge pages by calling vm_ops->fault(). A new VMF flag, FAULT_FLAG_USERFAULT_CONTINUE, is introduced to avoid recursive call to handle_userfault(). Suggested-by: James Houghton Signed-off-by: Nikita Kalyazin --- include/linux/mm_types.h | 4 ++++ mm/hugetlb.c | 2 +- mm/shmem.c | 9 ++++++--- mm/userfaultfd.c | 37 +++++++++++++++++++++++++++---------- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 0234f14f2aa6..2f26ee9742bf 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1429,6 +1429,9 @@ enum tlb_flush_reason { * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. * We should only access orig_pte if this flag set. * @FAULT_FLAG_VMA_LOCK: The fault is handled under VMA lock. + * @FAULT_FLAG_USERFAULT_CONTINUE: The fault handler must not call userfaultfd + * minor handler as it is being called by the + * userfaultfd code itself. * * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify * whether we would allow page faults to retry by specifying these two @@ -1467,6 +1470,7 @@ enum fault_flag { FAULT_FLAG_UNSHARE = 1 << 10, FAULT_FLAG_ORIG_PTE_VALID = 1 << 11, FAULT_FLAG_VMA_LOCK = 1 << 12, + FAULT_FLAG_USERFAULT_CONTINUE = 1 << 13, }; typedef unsigned int __bitwise zap_flags_t; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 97930d44d460..c004cfdcd4e2 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6228,7 +6228,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, } /* Check for page in userfault range. */ - if (userfaultfd_minor(vma)) { + if (userfaultfd_minor(vma) && !(vmf->flags & FAULT_FLAG_USERFAULT_CONTINUE)) { folio_unlock(folio); folio_put(folio); /* See comment in userfaultfd_missing() block above */ diff --git a/mm/shmem.c b/mm/shmem.c index 1ede0800e846..b4159303fe59 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2467,7 +2467,8 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, fault_mm = vma ? vma->vm_mm : NULL; folio = filemap_get_entry(inode->i_mapping, index); - if (folio && vma && userfaultfd_minor(vma)) { + if (folio && vma && userfaultfd_minor(vma) && + !(vmf->flags & FAULT_FLAG_USERFAULT_CONTINUE)) { if (!xa_is_value(folio)) folio_put(folio); *fault_type = handle_userfault(vmf, VM_UFFD_MINOR); @@ -2727,6 +2728,8 @@ static vm_fault_t shmem_falloc_wait(struct vm_fault *vmf, struct inode *inode) static vm_fault_t shmem_fault(struct vm_fault *vmf) { struct inode *inode = file_inode(vmf->vma->vm_file); + enum sgp_type sgp = vmf->flags & FAULT_FLAG_USERFAULT_CONTINUE ? + SGP_NOALLOC : SGP_CACHE; gfp_t gfp = mapping_gfp_mask(inode->i_mapping); struct folio *folio = NULL; vm_fault_t ret = 0; @@ -2743,8 +2746,8 @@ static vm_fault_t shmem_fault(struct vm_fault *vmf) } WARN_ON_ONCE(vmf->page != NULL); - err = shmem_get_folio_gfp(inode, vmf->pgoff, 0, &folio, SGP_CACHE, - gfp, vmf, &ret); + err = shmem_get_folio_gfp(inode, vmf->pgoff, 0, &folio, sgp, gfp, vmf, + &ret); if (err) return vmf_error(err); if (folio) { diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index d06453fa8aba..4b3dbc7dac64 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -380,30 +380,47 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, return ret; } -/* Handles UFFDIO_CONTINUE for all shmem VMAs (shared or private). */ +/* Handles UFFDIO_CONTINUE for all VMAs */ static int mfill_atomic_pte_continue(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr, uffd_flags_t flags) { - struct inode *inode = file_inode(dst_vma->vm_file); - pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); struct folio *folio; struct page *page; int ret; + struct vm_fault vmf = { + .vma = dst_vma, + .address = dst_addr, + .flags = FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE | + FAULT_FLAG_USERFAULT_CONTINUE, + .pte = NULL, + .page = NULL, + .pgoff = linear_page_index(dst_vma, dst_addr), + }; + + if (!dst_vma->vm_ops || !dst_vma->vm_ops->fault) + return -EINVAL; - ret = shmem_get_folio(inode, pgoff, 0, &folio, SGP_NOALLOC); - /* Our caller expects us to return -EFAULT if we failed to find folio */ - if (ret == -ENOENT) +retry: + ret = dst_vma->vm_ops->fault(&vmf); + if (ret & VM_FAULT_ERROR) { ret = -EFAULT; - if (ret) goto out; - if (!folio) { - ret = -EFAULT; + } + + if (ret & VM_FAULT_NOPAGE) { + ret = -EAGAIN; goto out; } - page = folio_file_page(folio, pgoff); + if (ret & VM_FAULT_RETRY) + goto retry; + + page = vmf.page; + folio = page_folio(page); + BUG_ON(!folio); + if (PageHWPoison(page)) { ret = -EIO; goto out_release; From patchwork Fri Apr 4 15:43:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 14038636 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 5DADCC36014 for ; Fri, 4 Apr 2025 15:44:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF7476B0029; Fri, 4 Apr 2025 11:44:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C7FBE6B002A; Fri, 4 Apr 2025 11:44:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B1FF36B002B; Fri, 4 Apr 2025 11:44:28 -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 8EEB36B0029 for ; Fri, 4 Apr 2025 11:44:28 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2B4E31204C1 for ; Fri, 4 Apr 2025 15:44:29 +0000 (UTC) X-FDA: 83296783458.25.316A39E Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) by imf26.hostedemail.com (Postfix) with ESMTP id 56F64140005 for ; Fri, 4 Apr 2025 15:44:27 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=QVFG3Qax; spf=pass (imf26.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 52.119.213.156 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743781467; 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=dh/0PI2ns4oZ37OX+8TMvz834BKQ/4DyQ5yTHrbPiLw=; b=LoiuatC/mzmgJMkZrEn9da95HbwQXhYxJj4uLRuXuHAyqaV6GMsa7+Jp+Ic0OKzwUrYk4P y0KMWmnGDsG6L2QMfQhPRE2nWAnEK0onxTHCvWsJwURNMmfVP445Sl+7kyatdVJqnbuqj9 it+msbzYLz+C5ZzmMe8A7hVmOLwUrBU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743781467; a=rsa-sha256; cv=none; b=lICnknFPRHAm6fp5JZEmb8Ol3ApMvSC2mFb8HQE2O7mRJwy7APDBjIj+JU6bTQLhocfqbx QFk7zyzm/7JZrXeHstt4pI5AQscewp/T7OmtxTQSHi/2UETQ692UibfkEZaBsJte4xm5Lw Bfj5wPUlZ4IDTy7mYHND+8M1aOaCLuw= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=QVFG3Qax; spf=pass (imf26.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 52.119.213.156 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1743781468; x=1775317468; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dh/0PI2ns4oZ37OX+8TMvz834BKQ/4DyQ5yTHrbPiLw=; b=QVFG3QaxmCrGVKvb0SR++lXOwXXPCHhFmq3GdDqLDT7kVWJ+TY4sLZiq qe8FQqxwhaC5SZw/a2Q3u9T9pMfyUBiiInAqmyMELzjiqyRF+FuqaLUw4 eOqe81x9vX3RA9cxHshwBq0sDsaF57pwIheXdCIYYtZvYICfJpVjt9C7w 4=; X-IronPort-AV: E=Sophos;i="6.15,188,1739836800"; d="scan'208";a="732940908" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2025 15:44:23 +0000 Received: from EX19MTAUWC001.ant.amazon.com [10.0.21.151:37011] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.25.120:2525] with esmtp (Farcaster) id 08c94e81-d4d9-4237-8218-603f770ff779; Fri, 4 Apr 2025 15:44:21 +0000 (UTC) X-Farcaster-Flow-ID: 08c94e81-d4d9-4237-8218-603f770ff779 Received: from EX19D003UWB003.ant.amazon.com (10.13.138.116) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:44:21 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D003UWB003.ant.amazon.com (10.13.138.116) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:44:21 +0000 Received: from email-imr-corp-prod-iad-1box-1a-6851662a.us-east-1.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.1544.14 via Frontend Transport; Fri, 4 Apr 2025 15:44:21 +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-iad-1box-1a-6851662a.us-east-1.amazon.com (Postfix) with ESMTPS id 709954032D; Fri, 4 Apr 2025 15:44:18 +0000 (UTC) From: Nikita Kalyazin To: , , , , , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v3 2/6] mm: provide can_userfault vma operation Date: Fri, 4 Apr 2025 15:43:48 +0000 Message-ID: <20250404154352.23078-3-kalyazin@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250404154352.23078-1-kalyazin@amazon.com> References: <20250404154352.23078-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 56F64140005 X-Stat-Signature: wwkf5wumr8eetrwj9jcdag99dco3uyun X-HE-Tag: 1743781467-325967 X-HE-Meta: U2FsdGVkX1+ksBQsCHrIiwdpj/bk9zResHwjBRZ3vjzpRRBmCKQbOq5b5YCJfxRhNjcPUaByX73mAeQtDyml86QpqOyZxV3BVAVfue5MXoYZiC+S5Bu7CkwGET9rbgh2jFWvPF69+Q/a7bJWy3/1ZQ9IZSelGgCmlKCitbJrb/iVudJI8cJPXfmI9DOMp1qQvusmeTHtrmxAg4/mKB65gHo3q9hOlnBdTFd51KYoHIziDDPEZv1Qez7q58X3Rq+DbHu43fX/Y227ftGKd+jY6o1Z8cOyWrgmKWC4aJ6OF1tQ1rPZlL0I7BQ2gFw3RyLJR3f5UnbOlH8aNJl9LIIlTVYPlz00yBRltGkH86pXnVP9lkpuFW2zXujEALEP8bAvHJ2PQHgbxC3IL/qNEMQc9QEZdAPpd7EtA2BxUjsUb9ChGUcTWzF6n+cpacFPVIAO3bP3bfpq0dBdKuxhXwG7yLoCkItGXjmWbyx5fz/K3wmGo/TqkLmX4SbCoVqNOZhSItxo3scsyjmbQ1+P4pWwoYbigRuQ8YzkxkU4IHEC5uC7D/cICLVdui/e8jqRQMayR116VpiD0hQrKvKgYlxOH9nJwlPGcE1c2ttG3Iuw56FPOpaONXGuJgwUdNgFjJ/mHxrTk0XzjeoCjuxvdBeXUpr7QokiqNKl3cjFJrCuqoTACmyzS1OJSMpxxYITVtXYp1k8ptYPAhHub/7HkHIyptNpTWmA6nLBf9in0JXrKH/BWk+bi0D3dPG1pYQAVFuMl6bDkMrbs3R7lTI0+tMts3pR65SWHfpArtrHC7mR73bCQ0viW0k8wpRaMALz0sP5wENsAC7EDJp7z3fde088p5Xx8jI5DHdpU8Axprqtcy/8hWZYXy2giN8Irr+qDKRC9BrBnoD78IoFxsRcRZBRJ2vh3Cyj9YYyZrLoQ6lYZL7fjI7Z50z2HPThrtiW98L5Fdq0IzMtBcpgNkf+Znu V+oRTXA5 us6pTys7ZiE+VNLEN6DbNgyUyxj1DrQZaNv60mHpCkXGZPt5hAg1dNdV0P7PWTZimy5YHZ3qZ4JWX7/FtwJWP51Sh+PxtgWDVzXYVIkBNxoBADOCSidsffy0xFFj4vfYwYNokk3SD7s7EhX8AuJeS3yCHPfAZ55xOKnNRhQO+Vu8ou0nIKEihj8DMZBu2jn4oZjEm1lEb4DOYLEXz8iqhRbPm7A9wUr4AAmiANZFjDxxoh7tcA5MtVLgTmHifijqCa8nLbjB3bTuL5cI= 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 new operation allows to decouple the userfaulfd code from dependencies to VMA types, specifically, shmem and hugetlb. The vm_flags bitmap argument is processed with "any" logic, meaning if the VMA type supports any of the flags set, it returns true. This is to avoid multiple calls when checking for __VM_UFFD_FLAGS. Signed-off-by: Nikita Kalyazin --- include/linux/mm.h | 5 +++++ mm/hugetlb.c | 7 +++++++ mm/shmem.c | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8483e09aeb2c..488d721d8542 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -680,6 +680,11 @@ struct vm_operations_struct { */ struct page *(*find_special_page)(struct vm_area_struct *vma, unsigned long addr); + /* + * True if the VMA supports userfault at least for one of the vm_flags + */ + bool (*can_userfault)(struct vm_area_struct *vma, + unsigned long vm_flags); }; #ifdef CONFIG_NUMA_BALANCING diff --git a/mm/hugetlb.c b/mm/hugetlb.c index c004cfdcd4e2..f3901c11e1fd 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5143,6 +5143,12 @@ static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma) return huge_page_size(hstate_vma(vma)); } +static bool hugetlb_vm_op_can_userfault(struct vm_area_struct *vma, + unsigned long vm_flags) +{ + return true; +} + /* * We cannot handle pagefaults against hugetlb pages at all. They cause * handle_mm_fault() to try to instantiate regular-sized pages in the @@ -5168,6 +5174,7 @@ const struct vm_operations_struct hugetlb_vm_ops = { .close = hugetlb_vm_op_close, .may_split = hugetlb_vm_op_split, .pagesize = hugetlb_vm_op_pagesize, + .can_userfault = hugetlb_vm_op_can_userfault, }; static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, diff --git a/mm/shmem.c b/mm/shmem.c index b4159303fe59..0b9e19abd1e9 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2891,6 +2891,12 @@ static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); } +static bool shmem_can_userfault(struct vm_area_struct *vma, + unsigned long vm_flags) +{ + return true; +} + static struct mempolicy *shmem_get_pgoff_policy(struct shmem_inode_info *info, pgoff_t index, unsigned int order, pgoff_t *ilx) { @@ -5309,6 +5315,7 @@ static const struct vm_operations_struct shmem_vm_ops = { .set_policy = shmem_set_policy, .get_policy = shmem_get_policy, #endif + .can_userfault = shmem_can_userfault, }; static const struct vm_operations_struct shmem_anon_vm_ops = { @@ -5318,6 +5325,7 @@ static const struct vm_operations_struct shmem_anon_vm_ops = { .set_policy = shmem_set_policy, .get_policy = shmem_get_policy, #endif + .can_userfault = shmem_can_userfault, }; int shmem_init_fs_context(struct fs_context *fc) From patchwork Fri Apr 4 15:43:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 14038637 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 36C24C36014 for ; Fri, 4 Apr 2025 15:44:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C0BB280006; Fri, 4 Apr 2025 11:44:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 97128280001; Fri, 4 Apr 2025 11:44:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 81D90280006; Fri, 4 Apr 2025 11:44:43 -0400 (EDT) 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 5EB3C280001 for ; Fri, 4 Apr 2025 11:44:43 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E97EEB762E for ; Fri, 4 Apr 2025 15:44:43 +0000 (UTC) X-FDA: 83296784046.18.5EB8F4A Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf29.hostedemail.com (Postfix) with ESMTP id D276712000B for ; Fri, 4 Apr 2025 15:44:41 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=bgpufSt6; spf=pass (imf29.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743781482; 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=wZsvbmbu/0jS7xgl288uAUsGSomu15ez6E6onuR11lQ=; b=XR52BgVCHmpRZOJOSXGuLrXD9qxQWKQFLrmwk6DiablkCjGxeCvNsNylaCsNnT/NjmbDwC +4s0yL/oAqq7XV/Y4/XSNjRYKck6YCSL5VlZUGCvi07txhWFmQ+wz8RUkDCp1WXw5wghwR LjLvJ0E388YqVicgqRBAyhrTTijzO8s= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=bgpufSt6; spf=pass (imf29.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743781482; a=rsa-sha256; cv=none; b=vtc1t5MtTBS0BxZ939u7kZcKir7gSotk1Q72UnEb/qfteLR+TuiMHYn614F1GWlCP8vdWG FdUGTXROtKYCqeqRR0+V2kkW3pPmLLbORifYCgQqyKserVWsMqQhJ5jUVJq3OBXEYENotw D7mFRMY3bzZ69R57MVAqLZYtRDRM8uE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1743781482; x=1775317482; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wZsvbmbu/0jS7xgl288uAUsGSomu15ez6E6onuR11lQ=; b=bgpufSt6q6igmfaTxmFvHLNn8F7JwjipWO4L0STPrbBk79RBHZwYxY4R TWMW9JlKxgv11gQbfeW5Q0SZ3yOvCtRqOe1meuikUwKBaHjsDWS6/Y5Sp ySxhoJPBmj20j8MgUM7nSOsJqJ42EFvNKMMgVcdrdtoX/ww3OvgTYLSGt 8=; X-IronPort-AV: E=Sophos;i="6.15,188,1739836800"; d="scan'208";a="508886507" 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; 04 Apr 2025 15:44:36 +0000 Received: from EX19MTAUWA001.ant.amazon.com [10.0.38.20:55309] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.51.140:2525] with esmtp (Farcaster) id 2332ab5f-8f03-4ed5-a407-5ac2d770b39b; Fri, 4 Apr 2025 15:44:35 +0000 (UTC) X-Farcaster-Flow-ID: 2332ab5f-8f03-4ed5-a407-5ac2d770b39b Received: from EX19D020UWC003.ant.amazon.com (10.13.138.187) by EX19MTAUWA001.ant.amazon.com (10.250.64.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:44:34 +0000 Received: from EX19MTAUWC002.ant.amazon.com (10.250.64.143) by EX19D020UWC003.ant.amazon.com (10.13.138.187) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:44:33 +0000 Received: from email-imr-corp-prod-iad-all-1b-3ae3de11.us-east-1.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; Fri, 4 Apr 2025 15:44:33 +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-iad-all-1b-3ae3de11.us-east-1.amazon.com (Postfix) with ESMTPS id 3EB8EA02DE; Fri, 4 Apr 2025 15:44:31 +0000 (UTC) From: Nikita Kalyazin To: , , , , , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v3 3/6] mm: userfaultfd: use can_userfault vma operation Date: Fri, 4 Apr 2025 15:43:49 +0000 Message-ID: <20250404154352.23078-4-kalyazin@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250404154352.23078-1-kalyazin@amazon.com> References: <20250404154352.23078-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D276712000B X-Stat-Signature: bfoss97bwpj9j63quscm9wooqbx7icb9 X-HE-Tag: 1743781481-268373 X-HE-Meta: U2FsdGVkX19cwDxTUFZayEZpspdu+gLtgulL7XYfe8fCx5ZuDPe24jGbmU+o2um2J4FstQ0+X8lAhpjMISra3YUamxxhvtTGcjhL8y7vwjfvcK+djVsdEPaivHSf0A225rtYKt/HACGRpfmQc+uKK8MbrEPMNWeQhjMq5vAb+qe7rqAyc8F3vrkKCVDcoh9imySSFbyTBjT3hNjJxLuR2CQxbOOC5j4CeZkh2JxbpYeHGvxg+zKgr7A57/GL9U2a4LXyGF+wJJMJG3BXA/Vzm5h0KoTytipaKhuftvau7+kpeZLP2+8c7U7x/C7DwhVHYJgPenFmOT8Q6jwqnsRGyMDtXqdImobVa40I9wCuWq13vLRulHe/Yae2u7FyxfVgudry7TcTtfJ2Yr/Vej7rUQynLtYNVtQtF6uBDFbY0stYRVAoDNJ26kheSiczgQHg1t5JvxmP00tZLSz6CLqwKRC4NmQQNjIJELPW23XZzuTHIljEoejxfWTgJkUt8bWwhcinemG534BaRzEKFPoDGZU1rMM+tagsX3D0lQjyME0ySF4oDecF2D9vuf5MAYH1sYDuFiy+0acqqD218r5R+pcBut1uCJYq+vg8VE98wGHd/rKdOGPc4ovP0XRUY4R+ogqYOMcB+TwUmSNGMh4uQC1zGIiszeAVeCdvpMEBgnvIzNbWA3L0GAObfd0+WH/X7ixCcYL5b6g78H247O1XarTfZknuWGJ3l76smUUM8G+lvsW9TWzDE2Pqx2+fCV6gwgZPh7i+k3MHAC0ayI43GADrAzqBcxhQiQ/tOUxv60Sz3VdDNul16AOKgfGQVslY5SLwdHp5fXLCeE623DUrp1Iyfg4RBg1amcQilQ/d2SaoBd8aVj/o5HFv8V6Nkl9V4tGaJ3iy3HkelNEDl2jMEOy/9nIeTTU6CyDO+pyzS8kLnpwtVXOuLp4OwqnlBrVWxnsqxKF/T+ej72EuK62 9ffHW4NU 1Kga6g6M/qV7Ts29/9UWSNpdEKr8Mv3r7l8wM1XR4rfi+wJRg07b4v9JhM/027FYV4P+qKBdXOYxC74Kr7ti0XZE6guhtbWbtEM//FvILMKNVaFTCDi/em8Ec48M1hoo7Uocui8b0UvK0h2aEmnn2fMW+tSuGLcj/8d/iXellhwjg3XX46yqWv171iSCcEhcom1wtZ7Cr5/gMQmiqWqCkyEyTwf/Sj2uUEWnEzYW8UVTcaUKskxEFQwcvR+perQjhhjtIM4KVY6QmqcNTT882CGV7XS63oGr1ianILmWWRoygRawSiOTGCDKki2+bjQtLSoNKL0G4n4pxWuVytSc3ByDZvqEvp2iYcYEY2qqb1i08Zef/bDiDA2d7WahpFDkfFiOYm5s6UUxlLaEV61rd92P+ieU5qIDqljaGh5aZJTxZ+OpEzzTw9Ywt4BTNwb5X1tk5bBQhtP8bmfjhf+Ds9ywmsQplAMlPUWPMEYStN0IxoAvtIVNN+qfYhvzAesmfNK1/mRW6kDbbV9bIZ1toziHqQeDZAIG95RXCPy3eG/KvWLc= 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: Signed-off-by: Nikita Kalyazin --- include/linux/userfaultfd_k.h | 13 ++++++------- mm/userfaultfd.c | 10 +++++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 75342022d144..64551e8a55fb 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -221,8 +221,8 @@ static inline bool vma_can_userfault(struct vm_area_struct *vma, if (vm_flags & VM_DROPPABLE) return false; - if ((vm_flags & VM_UFFD_MINOR) && - (!is_vm_hugetlb_page(vma) && !vma_is_shmem(vma))) + if (!vma->vm_ops->can_userfault || + !vma->vm_ops->can_userfault(vma, VM_UFFD_MINOR)) return false; /* @@ -235,16 +235,15 @@ static inline bool vma_can_userfault(struct vm_area_struct *vma, #ifndef CONFIG_PTE_MARKER_UFFD_WP /* * If user requested uffd-wp but not enabled pte markers for - * uffd-wp, then shmem & hugetlbfs are not supported but only - * anonymous. + * uffd-wp, then only anonymous is supported. */ if ((vm_flags & VM_UFFD_WP) && !vma_is_anonymous(vma)) return false; #endif - /* By default, allow any of anon|shmem|hugetlb */ - return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) || - vma_is_shmem(vma); + return vma_is_anonymous(vma) || + (vma->vm_ops->can_userfault && + vma->vm_ops->can_userfault(vma, vm_flags)); } static inline bool vma_has_uffd_without_event_remap(struct vm_area_struct *vma) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 4b3dbc7dac64..0aa82c968e16 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -728,6 +728,7 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, unsigned long src_addr, dst_addr; long copied; struct folio *folio; + bool can_userfault; /* * Sanitize the command parameters: @@ -787,10 +788,13 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx, return mfill_atomic_hugetlb(ctx, dst_vma, dst_start, src_start, len, flags); - if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) + can_userfault = dst_vma->vm_ops->can_userfault && + dst_vma->vm_ops->can_userfault(dst_vma, __VM_UFFD_FLAGS); + + if (!vma_is_anonymous(dst_vma) && !can_userfault) goto out_unlock; - if (!vma_is_shmem(dst_vma) && - uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) + + if (!can_userfault && uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE)) goto out_unlock; while (src_addr < src_start + len) { From patchwork Fri Apr 4 15:43:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 14038638 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 A0112C36010 for ; Fri, 4 Apr 2025 15:44:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12DE8280007; Fri, 4 Apr 2025 11:44:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 08CE9280001; Fri, 4 Apr 2025 11:44:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E716A280007; Fri, 4 Apr 2025 11:44:54 -0400 (EDT) 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 C1CD5280001 for ; Fri, 4 Apr 2025 11:44:54 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 825E3140474 for ; Fri, 4 Apr 2025 15:44:55 +0000 (UTC) X-FDA: 83296784550.19.83CED47 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by imf03.hostedemail.com (Postfix) with ESMTP id 934E520009 for ; Fri, 4 Apr 2025 15:44:53 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="c/dNBaBL"; spf=pass (imf03.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 72.21.196.25 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743781493; 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=Hs0vi6KJkVKWhslhiRZ4duAi0DhiXeUL0EGHDvOz7fs=; b=rzf5SUBAZ2DmVvK6/D4lHkvdQhX6DvA3WC8unRkBmpbaE75ptTo/eJ+saAwRuR5cs984Ss 2IWW/EAli8q9jSIyHg8gjrKI0HcE8whUO4Zk+ev/f7IY0Fe6qtH4iABGLN3US1jfukHzlu Reb9I4VDMFfFgpq8Ttwin8xMyxC5LXU= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="c/dNBaBL"; spf=pass (imf03.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 72.21.196.25 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743781493; a=rsa-sha256; cv=none; b=YPE9xdDx05TH9aaMXL23vpLMw2s8GAJNIr+k/UOQA4DabLmrvM9W470O/0czJZfZfD1Gng XSW/wazL8FdbWnyuPFy2VOrIcng1XVNw/APZCFozuFBSOVBMBh2dM3SAeDtB6n5S4W/Dqk 6v7Jw45AR1G24jK9+zJKKkwfGcJEIV8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1743781494; x=1775317494; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Hs0vi6KJkVKWhslhiRZ4duAi0DhiXeUL0EGHDvOz7fs=; b=c/dNBaBLMi3PY/Qxhqidhrr0S7bhxqlEPoSl0iFs0PVmRj7RRmF8ugWE 2TcJ0IQorEiVAovonhDOTrsr/MuDeTXjwK8sc857X6tphBtgvVYg7Kw1m gXeFSehWv99Icj+knic8Vs64eSmSoAaeuoS+3ExO8XsJGXe72gFt9ABM4 8=; X-IronPort-AV: E=Sophos;i="6.15,188,1739836800"; d="scan'208";a="480424509" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2025 15:44:50 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:10114] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.51.140:2525] with esmtp (Farcaster) id 7694f073-a459-43df-b4a1-c487f31edac7; Fri, 4 Apr 2025 15:44:48 +0000 (UTC) X-Farcaster-Flow-ID: 7694f073-a459-43df-b4a1-c487f31edac7 Received: from EX19D020UWC003.ant.amazon.com (10.13.138.187) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:44:48 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D020UWC003.ant.amazon.com (10.13.138.187) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:44:47 +0000 Received: from email-imr-corp-prod-pdx-all-2b-5ec155c2.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.1544.14 via Frontend Transport; Fri, 4 Apr 2025 15:44: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-2b-5ec155c2.us-west-2.amazon.com (Postfix) with ESMTPS id 68987402AD; Fri, 4 Apr 2025 15:44:44 +0000 (UTC) From: Nikita Kalyazin To: , , , , , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v3 4/6] KVM: guest_memfd: add support for userfaultfd minor Date: Fri, 4 Apr 2025 15:43:50 +0000 Message-ID: <20250404154352.23078-5-kalyazin@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250404154352.23078-1-kalyazin@amazon.com> References: <20250404154352.23078-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 934E520009 X-Rspamd-Server: rspam05 X-Rspam-User: X-Stat-Signature: i8fpmt8ukw69p93k63azmipjz8zb44q9 X-HE-Tag: 1743781493-570208 X-HE-Meta: U2FsdGVkX18qiEoa14DpuCxnc9CJf/bfCcvjxtMfeUEeskDkr6xejmEBnqoKKmZXJ/4ShdHSr/lRUVtoElKvvieZtnZVqyCHnC+fQ5ac83wPvKd24m7GlrELsqj+TvSizEGPkxS+AhdPil164KAzzM3DJK6W7CCZbMJf8SSegwxDpjJSa8myibVHpPmDbze/Gjw2TC+FX3P8SInp65V28ohZDhqCyubQJsoYS07SxLNN/G22xTkH3PGGeMrBPMMdobVSo7/Wwl47ucd20kSUSbn020k9VhUbs851hKtgswDRyl+fnWLNK6G/k46eYGuyvDIs6YE5x5kivIeeiGhe4LvfUtPSxNfyKqvIlQ+WqOZy+a7k2lH5KMe7+U8aLmwk9QTMD2sndGNQUOpGTf/hDoJnEWQy+AMe+CNXKukR1pY3UJRidmGN8Me4UhqvvRFmb07vLtcLAUcEEerWXhPYUQgshemp/GLUhHqIZVGnleAGNlqPA3XT9vZ/OIgMaHeFFtI6C9nBwMnpj9bDXe6fjEUNXA6A7Q1RRBGylaQIuZCj7I1tULx2UGOKOTuTEeYckniG50bFgR9+6p82htU+f9ouhudYgW2BzQeCtRFOin91HjWtoa4MG3TEguxWywreRkfD1dj+SeziURh3+gM2XeSJY6mS6+4xaD/mAHYEMiKDllpnMDFK6wWkcbh5DlefmdFNEh5HEBNPqYSr0GeO/5C8VcmRckmzpw1jWfvM4wtqiOenXl31ycudz8HwII4qtl6Y4aAcyxH/Iw0q61y3SSPYsR1Dk7Bq0MzTzDlA97frLYBWRkysW37Oge5gBi51vGO1ZW8IvdKXZgFcBKrbPyovgL2u5kUCoYbLdNfB6WCHDQdpzNQGE0nI+9h9UMHjPfgpYrNCanxYj6E8aIKyYCu09L9ySXYzpNT8GEn9qRMaL1qN0SFV43SBkhVm8En5hDdb0rlVherETHOG5cn bjwXqPYL eOPasElfIroSwpRPqQGcAHkg8McP/8snoBigBs4FVMh5lXgQBbnKVOZTPLyRdaWhN8+B3VsfbqPjyOFOIJOVSDEpYUI/F1MwBXUXZrmVjn3UyaqQniCcxVOtIcz3A/Bplghi8lD150b++WVx3veMWUNeyK1pqkKCWULOIJ+vVyIn0gLzdHygQX1RjHkDt5Vq/MdrJPoUFaA3460aejzN4addaSySdMSD/2BemQkjVMyjl5FPzWFmgmmm3xGnbyLN5rcc9q6+ig6XfILZg2KXEL2lZ5lf9zK/HbMb7Q1BIMXynR9HPye+fD3SrCjGUMbyL/JgReflNO+kpKnVlHS1oYggfsW8z2D+zLTYpec/5fhxcdbQ= 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: Add support for sending a pagefault event if userfaultfd is registered. Only page minor event is currently supported. Signed-off-by: Nikita Kalyazin --- virt/kvm/guest_memfd.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index fbf89e643add..096d89e7282d 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -4,6 +4,9 @@ #include #include #include +#ifdef CONFIG_KVM_PRIVATE_MEM +#include +#endif /* CONFIG_KVM_PRIVATE_MEM */ #include "kvm_mm.h" @@ -380,6 +383,13 @@ static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) kvm_gmem_mark_prepared(folio); } + if (userfaultfd_minor(vmf->vma) && + !(vmf->flags & FAULT_FLAG_USERFAULT_CONTINUE)) { + folio_unlock(folio); + filemap_invalidate_unlock_shared(inode->i_mapping); + return handle_userfault(vmf, VM_UFFD_MINOR); + } + vmf->page = folio_file_page(folio, vmf->pgoff); out_folio: From patchwork Fri Apr 4 15:43:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 14038639 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 CE3F5C36010 for ; Fri, 4 Apr 2025 15:45:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 33174280008; Fri, 4 Apr 2025 11:45:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E0D3280001; Fri, 4 Apr 2025 11:45:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A802280008; Fri, 4 Apr 2025 11:45:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id ED3AA280001 for ; Fri, 4 Apr 2025 11:45:13 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C26DE1C7771 for ; Fri, 4 Apr 2025 15:45:14 +0000 (UTC) X-FDA: 83296785348.10.A9C1B5C Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by imf23.hostedemail.com (Postfix) with ESMTP id A208B14000B for ; Fri, 4 Apr 2025 15:45:12 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=j4vPo7SB; spf=pass (imf23.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 99.78.197.218 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743781512; 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=GO+fOmyXj9EkRr/gRISxH91V61Pvwt+WgpsvJXpM3FY=; b=BiO1UBkScidDIuICNOxw9jJwiLK6BYxmEp/kNd+i4cnfrmW7XlEyfRB7jZIWfU9vZDvxI5 13+ehfnVM3FyC9fjaL5yQV4szNgnodHt6n1ztW4p2dvSAtKpaNuzTU5AjY4JTW/qqYFYrw MMP6gbOTFrMQNKPVImE4jFEdW7UwRjc= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=j4vPo7SB; spf=pass (imf23.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 99.78.197.218 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743781512; a=rsa-sha256; cv=none; b=aYPbiCVrlnMRGIvjFSw9/PauwTPvxJWpFx9gm4hu5xYvkvGrr/eYjAAl5u3BL2l6W//Six GkNw51eqcLKx7Hjf2bofLzxfkh4Is32XLd7SisVde6jDH3sEU+AE/3HxeLgT/NzjEVjIyv zsLlURNJTFW50g/pQGRQppa8MCA6lL8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1743781513; x=1775317513; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GO+fOmyXj9EkRr/gRISxH91V61Pvwt+WgpsvJXpM3FY=; b=j4vPo7SBcQdtKistktRuW0yuk779DtzCEgHu2rqeFUdE2shyTaQS7Prf XMrgG/nj5WcdeHZtSYmmB5Un1oUmfYRi3iyxCT9XwVpZmTGqUK99NoNMj nvX6/hztObcVHtXs3NL+d0zU9PRjpiELZ0LHSlx9ALjkAvXSoDsTWjpsT k=; X-IronPort-AV: E=Sophos;i="6.15,188,1739836800"; d="scan'208";a="393099257" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2025 15:45:08 +0000 Received: from EX19MTAUWB001.ant.amazon.com [10.0.7.35:38032] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.51.140:2525] with esmtp (Farcaster) id fdc68b7e-a4fc-4834-afdf-ba6792da542c; Fri, 4 Apr 2025 15:45:07 +0000 (UTC) X-Farcaster-Flow-ID: fdc68b7e-a4fc-4834-afdf-ba6792da542c Received: from EX19D003UWB002.ant.amazon.com (10.13.138.11) 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.1544.14; Fri, 4 Apr 2025 15:45:01 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D003UWB002.ant.amazon.com (10.13.138.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:45:00 +0000 Received: from email-imr-corp-prod-iad-all-1b-3ae3de11.us-east-1.amazon.com (10.43.8.6) by mail-relay.amazon.com (10.252.135.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14 via Frontend Transport; Fri, 4 Apr 2025 15:45:00 +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-iad-all-1b-3ae3de11.us-east-1.amazon.com (Postfix) with ESMTPS id A0B33A0242; Fri, 4 Apr 2025 15:44:58 +0000 (UTC) From: Nikita Kalyazin To: , , , , , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v3 5/6] mm: userfaultfd: add UFFD_FEATURE_MINOR_GUEST_MEMFD Date: Fri, 4 Apr 2025 15:43:51 +0000 Message-ID: <20250404154352.23078-6-kalyazin@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250404154352.23078-1-kalyazin@amazon.com> References: <20250404154352.23078-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A208B14000B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: czxtibe8aw6txkqcunihhuhea9ff3pp3 X-HE-Tag: 1743781512-169445 X-HE-Meta: U2FsdGVkX18vRmUZlh0e491X1kunwNMrX3C0/dh3i8NusVctrVGP8Ywe7We7ylGw4tnwOVVRypIirI62SXPoGJIALYfh3qUdAKTr9tV78i31nHaGkfVY7W8xokErBubxP8Q7CjaG4Tik+3ZPYlOxtLoYYZx35ZDRCRsffIHc6jGK1T/u28AbVK0W8RY3D1vXcHZhNORyhIDBZqh4TZ+NVRT659uUveNO5DPdCkwFinCK4DUxjnE+okE19h130B/eTumlYjpmPRT+71BXW5cpKOxbUSxqIQld0g7x1Nmik7L9taRzI87Z8OmWilbFPkGTh+lXCkqwhX+jq/r+0JIq+XqTNIiEXh0rSV5E2dcPw4wa/iQK1i2XoENLpfSD+yxY3zucvimG0eL2NgJgSrtfDjk6++nVsRciEAJxMcjrTo4f8H68SCMuOs2X1vimCk9rqrAbQMgvxfhoYLh0k6IX6WvWikRbXIOEqFwh8QrqhV1wcllG4Ou/KXw9H8sntoLVQIeODKPy9/arwRmNu26i96SBcZXOaLtVxjKcYbFPCl8khzBQ9/w3bINAPTlkqKWHKxnwwNepDKTise/Rym43NcIwqBY+lmwOWYl2Bn1DVkzIL+BtfTGBsptOcxS+NUXPW6OSz/yJCF5jA//VqrvjkxEbEexbT1P6jqQ4pf6HX+Z0UvfQUf5Je27rB9C9YMjt/bOcRvm4Yr2/ps7bnw7it4YRojdXwxSc8YXqbHgwQx1N/N4mAEf2Nc4G4ITdxYI8IEmgumSI2PyBAURWYfUKXvff01VG5fViqqn1mK20ZKBeeO1AMKjtV9MliUKsNpgHttrBu46TuFE2/yyyRg28qox82Ze6J+oLLTBxyt4vOywmDFDdL2U23onHzw+APVr0vLfrtaqZCETZkyDy4QMTBaBqjWufzEWQ87QGwzvRKs8q4btWPg4BT3l0/d484mjk2s1y0zrpGeGy6r5FOMI UsEnRzYZ X/Lx3ZuMsY2YFyxvBuCI29rXAdvGEl2qH5CAHgOk9Nnocyz+bCjnH6yvOoqbmitv+gLK3Wa2ndOKjh11SFJEVTaAowGb+HVq0AI5q3E1VSHHUDQUyAoqbPtptWmF0YquKPW3g9CdXwwfEPdxKlx/mTA3iXGAckFnj9YjtTqPHKyx3yF8A8+U2uM7Q5pL7L6y220YBwV8IDYXyoHHuA07L+6pXaYlVjAnB2VkxVeFpuVHwd16sBLvHAkzEBnVIrxUssCAnfjSNub2pQJsPjo4GuEv0tWOUPKglbH9K7AzkzOsEsKcHt0PvQBp44CSWKn6lEvUxspSa7rh30an4+Plv9r0Ypg05har76erKSZcPGlb611x4+0zYBcxkEU3ZJJdar3nqGt6Gc1bn7JUMqeZpAKDZ5UtOmsZv3clty8LQdFCdftlQmPUAxQxwohKK9CzrrbHEIyG3RnMtFhNA+rGuuJH/DNYHdE+JofQ4NoD4UE7lTMI6P/RX6OX9DCXdv1WNOfzo1OWWz8wb0UiQGp9dLBjkUv21j4sRW4QCuYIiAyX9Zzo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000097, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Signed-off-by: Nikita Kalyazin --- fs/userfaultfd.c | 3 ++- include/uapi/linux/userfaultfd.h | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 97c4d71115d8..32152bfa462a 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1954,7 +1954,8 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, uffdio_api.features = UFFD_API_FEATURES; #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR uffdio_api.features &= - ~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM); + ~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM | + UFFD_FEATURE_MINOR_GUEST_MEMFD); #endif #ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP; diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h index 2841e4ea8f2c..ed688797eba7 100644 --- a/include/uapi/linux/userfaultfd.h +++ b/include/uapi/linux/userfaultfd.h @@ -42,7 +42,8 @@ UFFD_FEATURE_WP_UNPOPULATED | \ UFFD_FEATURE_POISON | \ UFFD_FEATURE_WP_ASYNC | \ - UFFD_FEATURE_MOVE) + UFFD_FEATURE_MOVE | \ + UFFD_FEATURE_MINOR_GUEST_MEMFD) #define UFFD_API_IOCTLS \ ((__u64)1 << _UFFDIO_REGISTER | \ (__u64)1 << _UFFDIO_UNREGISTER | \ @@ -230,6 +231,10 @@ struct uffdio_api { * * UFFD_FEATURE_MOVE indicates that the kernel supports moving an * existing page contents from userspace. + * + * UFFD_FEATURE_MINOR_GUEST_MEMFD indicates the same support as + * UFFD_FEATURE_MINOR_HUGETLBFS, but for guest_memfd-backed pages + * instead. */ #define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0) #define UFFD_FEATURE_EVENT_FORK (1<<1) @@ -248,6 +253,7 @@ struct uffdio_api { #define UFFD_FEATURE_POISON (1<<14) #define UFFD_FEATURE_WP_ASYNC (1<<15) #define UFFD_FEATURE_MOVE (1<<16) +#define UFFD_FEATURE_MINOR_GUEST_MEMFD (1<<17) __u64 features; __u64 ioctls; From patchwork Fri Apr 4 15:43:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Kalyazin X-Patchwork-Id: 14038640 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 98571C36010 for ; Fri, 4 Apr 2025 15:45:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D3F55280004; Fri, 4 Apr 2025 11:45:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEEB8280001; Fri, 4 Apr 2025 11:45:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB52C280004; Fri, 4 Apr 2025 11:45:27 -0400 (EDT) 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 97D7C280001 for ; Fri, 4 Apr 2025 11:45:27 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 88C72B757C for ; Fri, 4 Apr 2025 15:45:28 +0000 (UTC) X-FDA: 83296785936.19.7FC96C8 Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by imf09.hostedemail.com (Postfix) with ESMTP id 5AEF1140012 for ; Fri, 4 Apr 2025 15:45:26 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=VyJgAqww; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf09.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 99.78.197.217 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743781526; 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=yc6GH0/dKmQ7WSv9wb3vUThoZ6MNVvGsszSJT/iNJmY=; b=vhJpNw3OsQuNhTnK6Q1u1Bf0fnpaVtrMrnZaBp2Hna2MNvOsdzkS8dvVZn/MjscMZDtb/M vZFxqLLnr3OBkh0tdvP66GvkqbWAvOcIhk6elxxwk0zGhx8qk+PtONhCYAhNxbNFYF8kAT lYA1zgT3B+zTeIpfLplgLtNWJ8Wf4nc= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=VyJgAqww; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf09.hostedemail.com: domain of "prvs=182d669d3=kalyazin@amazon.co.uk" designates 99.78.197.217 as permitted sender) smtp.mailfrom="prvs=182d669d3=kalyazin@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743781526; a=rsa-sha256; cv=none; b=2ZrLojkF7Rw8ZrsPCJtACRSm3cJxkCBNm7WwmwDh6g7V3Jh+mm1Q5+NzpqI5HNXa4CQb6C cTSSUfD05eeH6M07l8WCFacWEgHQDY4RJOmIyWW50t14WtdZ6KNWzxeN5TM0+5kQxM0cI8 Xx375mU19kA7fdG18zCuVHe1LhhiK5k= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1743781527; x=1775317527; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yc6GH0/dKmQ7WSv9wb3vUThoZ6MNVvGsszSJT/iNJmY=; b=VyJgAqwwvChJ8r34jPBAd4W8rL5T0Dw6TrU9bZQlr1uRDtN7cF66W9PB ZMRuI94AZu9CrVUM1Gj/4kjpa3pO4p+46IjNo6aWnsZ5WGmWBYJ3++VrL skb19LD2kY6LmHppZKJn5fsdI9jXfFyphi28KlPzZAl4NlUXYON1JO8U0 4=; X-IronPort-AV: E=Sophos;i="6.15,188,1739836800"; d="scan'208";a="37863645" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2025 15:45:22 +0000 Received: from EX19MTAEUB002.ant.amazon.com [10.0.43.254:25779] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.18.64:2525] with esmtp (Farcaster) id bc1466b6-dabf-4c34-ba00-853571b00927; Fri, 4 Apr 2025 15:45:20 +0000 (UTC) X-Farcaster-Flow-ID: bc1466b6-dabf-4c34-ba00-853571b00927 Received: from EX19D030EUB002.ant.amazon.com (10.252.61.16) by EX19MTAEUB002.ant.amazon.com (10.252.51.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:45:16 +0000 Received: from EX19MTAUEA002.ant.amazon.com (10.252.134.9) by EX19D030EUB002.ant.amazon.com (10.252.61.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14; Fri, 4 Apr 2025 15:45:16 +0000 Received: from email-imr-corp-prod-pdx-all-2b-dbd438cc.us-west-2.amazon.com (10.43.8.2) by mail-relay.amazon.com (10.252.134.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1544.14 via Frontend Transport; Fri, 4 Apr 2025 15:45: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-2b-dbd438cc.us-west-2.amazon.com (Postfix) with ESMTPS id CCF7BA047C; Fri, 4 Apr 2025 15:45:11 +0000 (UTC) From: Nikita Kalyazin To: , , , , , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v3 6/6] KVM: selftests: test userfaultfd minor for guest_memfd Date: Fri, 4 Apr 2025 15:43:52 +0000 Message-ID: <20250404154352.23078-7-kalyazin@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250404154352.23078-1-kalyazin@amazon.com> References: <20250404154352.23078-1-kalyazin@amazon.com> MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 5AEF1140012 X-Stat-Signature: arryd59nkxf5hsgfdcbiyyp3fp4e6mqy X-Rspam-User: X-HE-Tag: 1743781526-771801 X-HE-Meta: U2FsdGVkX1/Xu2NPExf3pb8wbwEI6lHvgfd9wu0YIil4YXQDrU/qWsYyhJI+czJRa4ghquZyxkmaw2zJp1xSxdV8vneqt6/QaIcsuBNlAvAEzF6PEqx3yc3vl1Fa4KWo4Zy6q3vYl6EBE+NzujVWNtIpOXTCuG3Nyhz7O2X4RC69VUQBBOf2b8RQ4IQzYIkjYTLSzsis894o5NvFF5IMVEpDx/ovDNn4YxGJYOS1M2dh5+KpwsB+X1rPu2hSPfrDXBx+WKNs8C6sFbaM9ICCKHTnQmfLzRw6UlKpOyjNMT5JsqLfKFm/PxyQldD2w3DX8VGzau5G0MgNBZNIpkZw1ly7CLEjcnvFfWA3MLlVH0lnQb+KfJ2sTiQ0Nt3YtVkJJopRNT2nDTfh9jhc1gKz+pk0bzu7LaY947DpLxaTYyD7fW3/EwPuxV6XaUBpBNJCdXCg15kWmr5DoVI5DmPuHxjADtQZdT3WG+RZbXq1h8Qvir2Vy+10lfYKkN0Ra2AuoS7g8j3DFRs9mlGpJ7m/H7A9p7BAiChKTRInI/QR+izn+MK3uE70P4BHa6+dNwHcforOVlcei4Uy2ijcknMtWNIpYo9GkAVsz//3B1rOMY9rK1Y/EOSQYCztBmqpsZmaCjrXhSt2dpBYyIBsRsLJoLIHfHr2Q/Dyq5RL2VtMTWe9/Q+YSzSQfB8spDlkOZVVGbtjBkW/P0wvo6GTJgxMVjzAmNPPA31oECAuYqeRX6aO37C6Y15RrVi6hyho1jUg8eZTv8pLtkwV/qMdbzarLezgsKjnL7aKGu7Q+BEWWKdtLnKMeXjlSMZemurRdq0yJ8TH+ppUXTGLRrqNMHzeCtBoMiOH7LxhoecTbFzF/hVEP34TiumdOaBUN629gSG/gUoHPWH8TMxkKPHm/XHYEOgNN8trf9T/9DEO+D7J4qwJrXcT4CrPfK2VcbFKJbi0FN2GxmeRoqbMOWUZxc9 VLcdyZ5Z PfWI3MQ/Xv9CJF5Jg/XDnCYkc0HgOYbcw903MjoRFwQTWMnpDqULtvUGXTLS5lSyy/BEpUpHghU4PFQVZPicheDoZwSDkIq+ZAlmH2OACCb0z2Cg4Zp+bmuEQmG4tJQnc9JeeY4I/7Ewbwv3KBaZHeAaAEIh0lk1ylB3qJiI6+wmsvt2dUMYeqdvqdKyFfsFlJ76R60GRCs8SqAZswS2oPW72y7co1IpAc1jiV6w/L66Ef+ve2zF78Y8qarrt472Rfeq3t/oXiI67otgLjUGYf83+lyHS9Ay+kQVAlxxz9ioaPdf4+k99fHdgOmebfmVFhRfpzjVl/ijcsrRYXJDkkos1zmsOw4IYuO9QpOyILE2IoP46h01pfXZxMfkzktB4on1lmdRfKP8FGy55g7tygPUGtniLWdhAuV7GVRPaw7LYA2PJ2wNwquY6TZ69pLu1SJYcXSkNmEmUPoDzh/RnX0YczjDE0xQvkBt+XTUjzcxkj6tT0HRLqjSB+y5NiQROjRER8WaJwVC9/qRMeCcr3EobwCwyu9RIvndcaKRZQkxXcGk= 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 test demonstrates that a minor userfaultfd event in guest_memfd can be resolved via a memcpy followed by a UFFDIO_CONTINUE ioctl. Signed-off-by: Nikita Kalyazin --- .../testing/selftests/kvm/guest_memfd_test.c | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index 38c501e49e0e..9a06c2486218 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -10,12 +10,16 @@ #include #include #include +#include #include #include +#include #include #include #include +#include +#include #include "kvm_util.h" #include "test_util.h" @@ -206,6 +210,98 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) close(fd1); } +struct fault_args { + char *addr; + volatile char value; +}; + +static void *fault_thread_fn(void *arg) +{ + struct fault_args *args = arg; + + /* Trigger page fault */ + args->value = *args->addr; + return NULL; +} + +static void test_uffd_minor(int fd, size_t page_size, size_t total_size) +{ + struct uffdio_register uffd_reg; + struct uffdio_continue uffd_cont; + struct uffd_msg msg; + struct fault_args args; + pthread_t fault_thread; + void *mem, *mem_nofault, *buf = NULL; + int uffd, ret; + off_t offset = page_size; + void *fault_addr; + + ret = posix_memalign(&buf, page_size, total_size); + TEST_ASSERT_EQ(ret, 0); + + uffd = syscall(__NR_userfaultfd, O_CLOEXEC); + TEST_ASSERT(uffd != -1, "userfaultfd creation should succeed"); + + struct uffdio_api uffdio_api = { + .api = UFFD_API, + .features = UFFD_FEATURE_MINOR_GUEST_MEMFD, + }; + ret = ioctl(uffd, UFFDIO_API, &uffdio_api); + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_API) should succeed"); + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem != MAP_FAILED, "mmap should succeed"); + + mem_nofault = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem_nofault != MAP_FAILED, "mmap should succeed"); + + uffd_reg.range.start = (unsigned long)mem; + uffd_reg.range.len = total_size; + uffd_reg.mode = UFFDIO_REGISTER_MODE_MINOR; + ret = ioctl(uffd, UFFDIO_REGISTER, &uffd_reg); + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_REGISTER) should succeed"); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, + offset, page_size); + TEST_ASSERT(!ret, "fallocate(PUNCH_HOLE) should succeed"); + + fault_addr = mem + offset; + args.addr = fault_addr; + + ret = pthread_create(&fault_thread, NULL, fault_thread_fn, &args); + TEST_ASSERT(ret == 0, "pthread_create should succeed"); + + ret = read(uffd, &msg, sizeof(msg)); + TEST_ASSERT(ret != -1, "read from userfaultfd should succeed"); + TEST_ASSERT(msg.event == UFFD_EVENT_PAGEFAULT, "event type should be pagefault"); + TEST_ASSERT((void *)(msg.arg.pagefault.address & ~(page_size - 1)) == fault_addr, + "pagefault should occur at expected address"); + + memcpy(mem_nofault + offset, buf + offset, page_size); + + uffd_cont.range.start = (unsigned long)fault_addr; + uffd_cont.range.len = page_size; + uffd_cont.mode = 0; + ret = ioctl(uffd, UFFDIO_CONTINUE, &uffd_cont); + TEST_ASSERT(ret != -1, "ioctl(UFFDIO_CONTINUE) should succeed"); + + TEST_ASSERT(args.value == *(char *)(mem_nofault + offset), + "memory should contain the value that was copied"); + TEST_ASSERT(args.value == *(char *)(mem + offset), + "no further fault is expected"); + + ret = pthread_join(fault_thread, NULL); + TEST_ASSERT(ret == 0, "pthread_join should succeed"); + + ret = munmap(mem_nofault, total_size); + TEST_ASSERT(!ret, "munmap should succeed"); + + ret = munmap(mem, total_size); + TEST_ASSERT(!ret, "munmap should succeed"); + free(buf); + close(uffd); +} + unsigned long get_shared_type(void) { #ifdef __x86_64__ @@ -244,6 +340,9 @@ void test_vm_type(unsigned long type, bool is_shared) test_fallocate(fd, page_size, total_size); test_invalid_punch_hole(fd, page_size, total_size); + if (is_shared) + test_uffd_minor(fd, page_size, total_size); + close(fd); kvm_vm_release(vm); }