From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795983732; b=YnpDC/I77B6+GsfiDp0izgcaLn03p6bAbTf3DML1c0uz/0At8a+xWU6LxCbmz5zHq0pC KK+ygPSbh5xPvuVmfLUz+jMWL60aMLvnA371mnAN1avEtXrk0Y0XHW1KSTwzcPItSuzGA 42g/Tk3tYJj0y73ZPXPqvNYcCQwnKKpwVignaASPR/g05wuPw7QjQvAMHLynDekZKGQHd 9rx+BDyNGP3SG/XcQCSLPsRoTasJmLTKnvZ0JeFqA+FH56ueOiB4ocTvHwWSKhDRM19P2 I19i47csPB4p9MeQmpZxWLC2xbka/ss2w0NwRJcCsOQ9EetSRKdn+OLlpO9iM9GnEBg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795983732; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=+I087uAHFfr3vIi2QzJD6zDcj5fneQUT1cINxigykdY=; b=oOBqYAH0leBtj2vmz/jC4JTooSQW5Knx7VPZlR7fxx6LyYarV9Vlbm6jzHUKrDRrJ6J3 wOUo1vlLRjyl14YJfZzzfPa1RoUDEm54DslPmHeHNvJHz3wzaIqH2CJS2K5B4ymOI7v6h yKSyiR8ssVqnFxLnq11k+TmtXd9diAPgCj9iLAzsryuDPuGrmOssNiK3zIUBftYgqAg9A pvK+d9ZP5niHgXQA0WrO6N0St+FIkNu8ck6M2r3YZIKv0HgCgUdEIhimGci0+HIw8ty6m QpTkodBNznd2agDBXZAz6VYBOdaZvSxGZVXSNwl22XOV9EIWcj/ZmlMHInd/p2/t+hQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="i/D836YK"; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72FDA606A32E for ; Sun, 29 Nov 2026 20:22:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 72FDA606A32E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=i/D836YK Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-3846d5f54c1so10664790eec.1 for ; Sun, 29 Nov 2026 12:22:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795983731; x=1796588531; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=+I087uAHFfr3vIi2QzJD6zDcj5fneQUT1cINxigykdY=; b=i/D836YK+My36AwWuVO4T4YKPVufCE2zxFWputrursTLeWLiu70ldIQAIn1+w/5MYX b75d7+Y6JsSuFLyec+Ulkr8wHWrMlnSDZhnv4hPFu+sx4BLZB1a4zCc0FCXPbMNU9/bP yduqy9pLeIQhzRoZPy3yRSo/Epy2/Oe8xtDewwD0wjatfhypnHFtk77kNqzwB36/XXLo t5ZzjXtwjaMIz7gXK/Sa004xbALBtkTiD1e2BGmUR5ZeSFd6WK7qr9vOvOl820SsPxJe tkebsyScrmnn4FlC5j3CKEY/P7o9isgsP8QMNL16Lpcyc73E+/QEKR/PueB5SOItievl yjlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795983731; x=1796588531; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+I087uAHFfr3vIi2QzJD6zDcj5fneQUT1cINxigykdY=; b=DiiQw9k4pv+7DqvwqTwLSF+MBWZlxQ37NSCqoc58RrRx+i//I49aXtvjw1Ll8v8S2h BjX6jbHrgu4TMSI1viHNlqhseYGfDsMBSkRVYomq/CwubME7upplCCq5Pjm/n7z7pGSP C645cnrOmSEsePG1igL2t7JTKLJ6POTkw2wY+otcEluUCpN9QqRUvaNJi2Tpe62XHNZG t5edejJS6v+KuBeOrSMvVLcMfcxmmUTmU4BnqS1Ecl/dEhNhfowK7BxpN9dm3VsLEtPZ Aecz6jPC/YGIw4fkxPbpeawvTa8AYpYJP3aWSP7ayhHwrtohF0qAoN35YpQ7PRUqbYCY V9Qg== X-Forwarded-Encrypted: i=1; AL+hErHQDBUNz++PlivzJPKnqIW3niFcOkP/wjAEDo+0OcTu5XsjrwcqM/Pv10lpqJEhD0mBnwKH7dxXDeSR@vger.kernel.org X-Gm-Message-State: APuU4Z+gVQHi0sCH8zLRAPCqdHtYqi3w69QpSrR9kEQf1RzmKjtZ19qW xKgtd3m03oHb67TCmMt5m98+k6gHgA/IdCvKmhvjT1YLsBxAIv8CCwbv7MYi/ieEwGChRIaUPI6 nV+4NyA== X-Received: from dybgk2.prod.google.com ([2002:a05:7301:182:b0:385:e49a:dd09]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:6486:b0:378:1191:43ca with SMTP id 5a478bee46e88-3863f1355b2mr25913017eec.18.1795983731013; Sun, 29 Nov 2026 12:22:11 -0800 (PST) Date: Sun, 29 Nov 2026 12:22:03 -0800 In-Reply-To: <20261129202203.1183277-1-surenb@google.com> X-Mailing-List: linux-s390@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261129202203.1183277-1-surenb@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261129202203.1183277-3-surenb@google.com> Subject: [RFC PATCH 2/2] mm: split VM_FAULT_RETRY_LOCK_MM out of VM_FAULT_RETRY From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: david@kernel.org, willy@infradead.org, ljs@kernel.org, liam@infradead.org, vbabka@kernel.org, rppt@kernel.org, jannh@google.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, nico.pache@linux.dev, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, usama.arif@linux.dev, kas@kernel.org, muchun.song@linux.dev, osalvador@suse.de, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, linux@armlinux.org.uk, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, chenhuacai@kernel.org, kernel@xen0n.name, maddy@linux.ibm.com, mpe@ellerman.id.au, npiggin@gmail.com, chleroy@kernel.org, ritesh.list@gmail.com, sshegde@linux.ibm.com, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, agordeev@linux.ibm.com, gerald.schaefer@linux.ibm.com, corbet@lwn.net, skhan@linuxfoundation.org, rdunlap@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-doc@vger.kernel.org, Suren Baghdasaryan Content-Type: text/plain; charset="UTF-8" VM_FAULT_RETRY is returned for two different reasons. It tells the caller that the fault handler had to drop its lock, usually to wait for something, so the fault has to be handled again. With per-VMA locks it is also returned when the fault can not be handled under the per-VMA lock at all and has to be handled under mmap_lock. The caller can not tell these two cases apart. Introduce VM_FAULT_RETRY_LOCK_MM for the second case and return it where the fault handler falls back from the per-VMA lock to mmap_lock. Those cases are: - vmf_can_call_fault(), when the VMA has ->fault but no ->map_pages; - __vmf_anon_prepare(), when the VMA has no anon_vma yet and mmap_lock can not be taken without blocking; - do_swap_page() and do_huge_pmd_device_private(), because migrate_to_ram() is not ready to operate under the per-VMA lock. VM_FAULT_RETRY keeps being returned in all the other cases. vmf_anon_prepare() and hugetlb release the per-VMA lock based on the result of __vmf_anon_prepare(), so make them check the new code. should_retry_mm_fault() accepts both codes, so page fault handlers keep retrying under mmap_lock in both cases. VM_FAULT_RETRY_LOCK_MM is never returned for a fault that is handled under mmap_lock, so the checks done after such a fault are left unchanged. No functional change intended. Signed-off-by: Suren Baghdasaryan --- Documentation/filesystems/porting.rst | 4 ++-- include/linux/mm_types.h | 13 +++++++++++-- mm/huge_memory.c | 2 +- mm/hugetlb.c | 9 +++++---- mm/internal.h | 2 +- mm/memory.c | 18 ++++++++++-------- 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst index e41466904501..94db82f2195f 100644 --- a/Documentation/filesystems/porting.rst +++ b/Documentation/filesystems/porting.rst @@ -944,8 +944,8 @@ Calling convention for ->huge_fault has changed. It now takes a page order instead of an enum page_entry_size, and it may be called without the mmap_lock held. All in-tree users have been audited and do not seem to depend on the mmap_lock being held, but out of tree users should verify -for themselves. If they do need it, they can return VM_FAULT_RETRY to -be called with the mmap_lock held. +for themselves. If they do need it, they can drop the per-VMA lock and +return VM_FAULT_RETRY_LOCK_MM to be called with the mmap_lock held. --- diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 7a80c904eab9..b46a9dbfe2c8 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1711,6 +1711,8 @@ typedef __bitwise unsigned int vm_fault_t; * fsync() to complete (for synchronous page faults * in DAX) * @VM_FAULT_COMPLETED: ->fault completed, meanwhile mmap lock released + * @VM_FAULT_RETRY_LOCK_MM: fault can not be handled under per-VMA lock, + * must retry under mmap_lock * @VM_FAULT_HINDEX_MASK: mask HINDEX value * */ @@ -1728,6 +1730,7 @@ enum vm_fault_reason { VM_FAULT_DONE_COW = (__force vm_fault_t)0x001000, VM_FAULT_NEEDDSYNC = (__force vm_fault_t)0x002000, VM_FAULT_COMPLETED = (__force vm_fault_t)0x004000, + VM_FAULT_RETRY_LOCK_MM = (__force vm_fault_t)0x008000, VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000, }; @@ -1743,12 +1746,17 @@ enum vm_fault_reason { * should_retry_mm_fault - Check whether a page fault has to be retried. * @fault: The fault result returned by handle_mm_fault(). * + * A fault handled under the per-VMA lock can fail with either + * VM_FAULT_RETRY_LOCK_MM, when it has to be handled under mmap_lock, or with + * VM_FAULT_RETRY for all the other reasons to retry. A fault handled under + * mmap_lock can only fail with the latter. + * * Return: true if the fault was not handled and the caller has to retry it, * false otherwise. */ static inline bool should_retry_mm_fault(vm_fault_t fault) { - return !!(fault & VM_FAULT_RETRY); + return !!(fault & (VM_FAULT_RETRY | VM_FAULT_RETRY_LOCK_MM)); } #define VM_FAULT_RESULT_TRACE \ @@ -1764,7 +1772,8 @@ static inline bool should_retry_mm_fault(vm_fault_t fault) { (__force u32)VM_FAULT_FALLBACK, "FALLBACK" }, \ { (__force u32)VM_FAULT_DONE_COW, "DONE_COW" }, \ { (__force u32)VM_FAULT_NEEDDSYNC, "NEEDDSYNC" }, \ - { (__force u32)VM_FAULT_COMPLETED, "COMPLETED" } + { (__force u32)VM_FAULT_COMPLETED, "COMPLETED" }, \ + { (__force u32)VM_FAULT_RETRY_LOCK_MM, "RETRY_LOCK_MM" } struct vm_special_mapping { const char *name; /* The name, e.g. "[vdso]". */ diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4fc888ddfb69..23a5dfe05824 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1479,7 +1479,7 @@ vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf) if (vmf->flags & FAULT_FLAG_VMA_LOCK) { vma_end_read(vma); - return VM_FAULT_RETRY; + return VM_FAULT_RETRY_LOCK_MM; } ptl = pmd_lock(vma->vm_mm, vmf->pmd); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 4386425b5ce5..4f3eea0b1e67 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6130,9 +6130,9 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, /* * We must check to release the per-VMA lock. __vmf_anon_prepare() is - * the only way ret can be set to VM_FAULT_RETRY. + * the only way ret can be set to VM_FAULT_RETRY_LOCK_MM. */ - if (unlikely(ret & VM_FAULT_RETRY)) + if (unlikely(ret & VM_FAULT_RETRY_LOCK_MM)) vma_end_read(vma); mutex_unlock(&hugetlb_fault_mutex_table[hash]); @@ -6391,9 +6391,10 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, /* * We must check to release the per-VMA lock. __vmf_anon_prepare() in - * hugetlb_wp() is the only way ret can be set to VM_FAULT_RETRY. + * hugetlb_wp() is the only way ret can be set to + * VM_FAULT_RETRY_LOCK_MM. */ - if (unlikely(ret & VM_FAULT_RETRY)) + if (unlikely(ret & VM_FAULT_RETRY_LOCK_MM)) vma_end_read(vma); mutex_unlock(&hugetlb_fault_mutex_table[hash]); diff --git a/mm/internal.h b/mm/internal.h index f865d7b0c94b..a65192d8358d 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -574,7 +574,7 @@ static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) { vm_fault_t ret = __vmf_anon_prepare(vmf); - if (unlikely(ret & VM_FAULT_RETRY)) + if (unlikely(ret & VM_FAULT_RETRY_LOCK_MM)) vma_end_read(vmf->vma); return ret; } diff --git a/mm/memory.c b/mm/memory.c index e0f1aeed713c..682d1167eb49 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4021,7 +4021,7 @@ static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf) if (vma->vm_ops->map_pages || !(vmf->flags & FAULT_FLAG_VMA_LOCK)) return 0; vma_end_read(vma); - return VM_FAULT_RETRY; + return VM_FAULT_RETRY_LOCK_MM; } /** @@ -4037,7 +4037,8 @@ static inline vm_fault_t vmf_can_call_fault(const struct vm_fault *vmf) * do with only the per-VMA lock held for this VMA. * * Return: 0 if fault handling can proceed. Any other value should be - * returned to the caller. + * returned to the caller. VM_FAULT_RETRY_LOCK_MM is returned with the + * per-VMA lock still held if the fault has to be retried under mmap_lock. */ vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf) { @@ -4048,7 +4049,7 @@ vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf) return 0; if (vmf->flags & FAULT_FLAG_VMA_LOCK) { if (!mmap_read_trylock(vma->vm_mm)) - return VM_FAULT_RETRY; + return VM_FAULT_RETRY_LOCK_MM; } if (__anon_vma_prepare(vma)) ret = VM_FAULT_OOM; @@ -5046,7 +5047,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) * under VMA lock. */ vma_end_read(vma); - ret = VM_FAULT_RETRY; + ret = VM_FAULT_RETRY_LOCK_MM; goto out; } @@ -6744,9 +6745,9 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) /* * On entry, we hold either the VMA lock or the mmap_lock - * (see FAULT_FLAG_VMA_LOCK). If VM_FAULT_RETRY is set in - * the result, the lock is not held on exit. See filemap_fault() - * and __folio_lock_or_retry(). + * (see FAULT_FLAG_VMA_LOCK). If VM_FAULT_RETRY or + * VM_FAULT_RETRY_LOCK_MM is set in the result, the lock is not held + * on exit. See filemap_fault() and __folio_lock_or_retry(). */ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, unsigned long address, unsigned int flags) @@ -6966,7 +6967,8 @@ static vm_fault_t sanitize_fault_flags(struct vm_area_struct *vma, } /* * Per-VMA locks can't be used with FAULT_FLAG_RETRY_NOWAIT because of - * the assumption that lock is dropped on VM_FAULT_RETRY. + * the assumption that lock is dropped on VM_FAULT_RETRY and + * VM_FAULT_RETRY_LOCK_MM. */ if (WARN_ON_ONCE((*flags & (FAULT_FLAG_VMA_LOCK | FAULT_FLAG_RETRY_NOWAIT)) == -- 2.56.1.839.g22a8290db9-goog