From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796297973; b=Ivvu0/vrPLMQYMdzy7D6ODx6F4F+bZegeARXeQgC38C4Lc6HbU167lxQgW00ddOZSI02 EU+ExS9Ya53/babiPBWHZV3TaPrsDJJ9GAlvMZOvvi0niwQEhH1A3KphyBy6bYbyh7nik 0iUohGwtnytsA1I0/+kkr4Z4NR6C4dUBT6iKGqNn0BLRerSGXBP7Z9YDQBQBF0EhGbDwu J9Vf7jTGvYuUcQBnmH+1P8saXswDJTsPbnNMsd4rqMbOwgkgMyUvXqDWVS+0g0zvAJQ/h Xche8yjkhG0M+rPPiyijMyoAzzP1+mjqp9YZM3Ih0EU6I0KsY2OMyTI3sN52Q9uOv4Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796297973; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=yUvQk6qwJT1wFH5Jz8tMczsZi0wMmp6Hj4kHCTahm40=; b=O+rJpetYzwAPdnJAOcNsGso0EPJ6hYEohYVIgdGZpwKjnaqbrFwjaBwOYDXi8YfF3Cxn 3JyZTa9WPIPRDGZXb7fkEu2Sp+7CNcsT30QjibRXZf9C0ykHDyM4W/DEZvD8gfkK2DYUI 7myMZ7E/M5VAbLblPLrejo47rCD9owuZ27A9GIdAM6JE48jDiWA9QklP2bNd28Zn7CRCi YRbim157TvOJtGLIMasMY+z38cEuBl8Z1k+mJNcjOgbq4h3WTWVggOPSF9aKNC+b479D4 8qoGlxKHIxMIMXAcZ+kf0VtUGxz42uy0PCZGhmlK8MaczkW1Rm3dQjDOtViu3l0oSsA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=H+aZlFIg; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B39CF606A333; Thu, 3 Dec 2026 11:39:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=H+aZlFIg Received: by smtp.kernel.org (Postfix) with ESMTPSA id 751031F00898; Thu, 3 Dec 2026 11:39:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796297973; bh=yUvQk6qwJT1wFH5Jz8tMczsZi0wMmp6Hj4kHCTahm40=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=H+aZlFIgFbGuk6DYREm9rH8YJRWxT6KvDnyTdj0Bu/TABAfvTO2H4Hl0MgA6D7FG8 uU3PedyHkM0OWN1aWh7ThhknYefyO+lb0BX8TQudmT+pr6smXWZstfpIPprLu7/06t DNhPwQfSPzAg2sUCC51dWBYkRH1MVWQ/dqqWV3JIr20C36P+7ekBVZCkW7c6KZdi9C ZBaYl+ey9tWvEzRl6UuCdjTcFnAmdI5IC7yhzarGcCd59RJLeMEg4IPlSoa5Ub7pq3 jbSoNSGAwlG6Ys1iAuK7xPUbJeQGYO65li2u0jCrvdWHQimz1JVDfqROdKoaASAMxH h+OZrUL8uR6ZA== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:30 +0100 Subject: [PATCH v2 07/35] mm/hugetlb: always mark new page cache folios migratable X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-7-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4096; i=david@kernel.org; h=from:subject:message-id; bh=5YsqrIJExRkIYUACTunrH/pCGkN9bLOfPb6x2tkfXEg=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ6q25U8vTuXwOJj++5Gq0IPe+hdZPrvnuN7+aqaUe atCx/JvRykLgxgHg6yYIktQ8H2Dly/X1Va78THDzGFlAhnCwMUpABNpv83wP+SvzUZh3famCVkW qdrmO6N+r39ynuXWNccTCa9iElz3nWP4pxul+VtyIfeJBGMWlTlVD8ND+fae1yh6xeSTLSF2seU bCwA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A hugetlb_no_page() and hugetlb_mfill_atomic_pte() only set hugetlb_migratable for a new folio once they installed the PTE. If they back out after adding the new folio to the page cache, for example because the PTE changed or is no longer empty, the folio will stay in the page cache without the migratable flag set. Later faults will find it as an existing folio and never reset the flag. Such a folio can never be isolated for migration, which breaks memory offlining, alloc_contig_range(), longterm pinning and memory failure handling. Worse, if the folio is not longterm pinnable, because it resides in ZONE_MOVABLE or CMA, memfd_pin_folios() and the longterm GUP path retry forever on -EAGAIN. memfd_pin_folios() cannot even be killed, which will be fixed independently. Let's fix it by setting hugetlb folios migratable whenever (1) a new anon folio gets mapped; or (2) a new folio was added to the page cache, like hugetlbfs_fallocate() already does. This has low urgency. Fixes: cb6acd01e2e4 ("hugetlbfs: fix races and page leaks during migration") Cc: stable@vger.kernel.org Reviewed-by: Muchun Song (ByteDance) Acked-by: Oscar Salvador (SUSE) Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f64c3293fd109..662cb87fe740a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5972,12 +5972,11 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, { const bool pagecache_folio = vmf->vma->vm_flags & VM_MAYSHARE; u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff); - bool new_folio, new_anon_folio = false; + bool folio_locked = true, new_anon_folio = false; struct vm_area_struct *vma = vmf->vma; struct mm_struct *mm = vma->vm_mm; struct hstate *h = hstate_vma(vma); vm_fault_t ret = VM_FAULT_SIGBUS; - bool folio_locked = true; struct folio *folio; pte_t new_pte; @@ -5997,7 +5996,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, * Use page lock to guard against racing truncation * before we get page_table_lock. */ - new_folio = false; folio = filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff); if (IS_ERR(folio)) { if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false)) @@ -6067,7 +6065,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, } folio_zero_user(folio, vmf->real_address); __folio_mark_uptodate(folio); - new_folio = true; if (pagecache_folio) { int err = hugetlb_add_to_page_cache(folio, mapping, @@ -6088,6 +6085,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, goto out; } filemap_invalidate_unlock_shared(mapping); + folio_set_hugetlb_migratable(folio); } else { new_anon_folio = true; folio_lock(folio); @@ -6170,11 +6168,10 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping, } /* - * Only set hugetlb_migratable in newly allocated pages. Existing pages - * found in the pagecache may not have hugetlb_migratable if they have - * been isolated for migration. + * We must set hugetlb folios migratable whenever (1) a new anon folio + * gets mapped; or (2) a new folio was added to the pagecache. */ - if (new_folio) + if (new_anon_folio) folio_set_hugetlb_migratable(folio); if (folio_locked) @@ -6668,6 +6665,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, if (ret) goto out_release_nounlock; folio_in_pagecache = true; + folio_set_hugetlb_migratable(folio); } ptl = huge_pte_lock(h, dst_mm, dst_pte); @@ -6716,7 +6714,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, /* No need to invalidate - it was non-present before */ update_mmu_cache(dst_vma, dst_addr, dst_pte); - if (!is_continue) + if (!folio_in_pagecache) folio_set_hugetlb_migratable(folio); if (vm_shared || is_continue) folio_unlock(folio); -- 2.56.1