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=1796150902; b=R4MgjbPxTTvGSfnmYyxwnc9GmpcEMy2QMQ815MveIGOsRzWqi1oUrpOfv6lEbC19ouYD xBlbk+Xjd5gz3zt4g/u2mZn/cQ5Trx21ckfMaPJMuGKDco16E+V58DrjgT2i9M+SrQt1L fdkwaCb9K5jnfgSihoVUuIwXNtAAO46gb3Vz/WZSNImg45H7Oqbz9rbxbcnE6x61ALQGD F092OusKgQ9XD3LCbNiCPrHwgcs5zo7UYKfaQ9dKlkvBUu3wNmvuUTFpadRVnoqt8LUrb LNHa+tLefGccnMV041VCnBCwGwgyQRlttR6A3eZJ9ws3bEs8owT+t/QXoUJ489NdPoQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796150902; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=OL0v9pfidz650jDceDVWzCKwohqMee8HG5H4PqscYFs=; b=WBL3XvSTvb4kj454qxiXA/u+LZVJqt1WPImTbP2aVnBMmnoaETf4lHghq5Yr+ipFt6Zf n6mPNu0cdPQXWzzQqbjjEYOUlBmERPe/ILeqxm6jvmzh0AL+KfGUBFn3uqMK/8nU9Z6z1 37eldbokuxTv02G3hvKFFbTX8fqao9/zrCR/EktxJGKvn8HUOLBxnK2WFulwjkCWkitll l7UOdmJQY5JVG4Z092up6mJyJUVhzk4DzLCokQzFYCkJMI/m+4eyzX9hi6vaPQOvnwVdg ETTpKK8y0zuQcaSACqpLLECORTUUuhBQpS0YB7uz6amfsiASVKtTSs32a5qI9Rnnl9A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=H6OE5eWY; 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 872A4606A31A for ; Tue, 1 Dec 2026 18:48:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=H6OE5eWY Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C96F1F00893; Tue, 1 Dec 2026 18:48:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796150902; bh=OL0v9pfidz650jDceDVWzCKwohqMee8HG5H4PqscYFs=; h=Date:To:From:In-Reply-To:References:Subject; b=H6OE5eWYf7AfBxNBoPIgNQneP0jY4mzrzGN1U2T1LGVrIeY/+pvK9QKJhkOuXSq3u KVJK5o59DIagWaiuLEDFT87GMbfvsd8NkPYwHDHnm73Gh4TuSXNl+H8YycrkpYlOAQ NcAXhlPPlbMNfIXaFg+B/ljtmZBBbKRJ1l3GOYlk= Date: Tue, 01 Dec 2026 10:48:21 -0800 To: mm-commits@vger.kernel.org,vbabka@kernel.org,usama.anjum@arm.com, surenb@google.com,skhan@linuxfoundation.org,sfr@canb.auug.org.au, rppt@kernel.org,rientjes@google.com,riel@redhat.com,rdunlap@infradead.org, pfalcato@suse.de,mirq-linux@rere.qmqm.pl,mhocko@suse.com,ljs@kernel.org, liam@infradead.org,jannh@google.com,jaharkes@cs.cmu.edu,hughd@google.com, hannes@cmpxchg.org,david@readahead.eu,david@kernel.org,corbet@lwn.net, avagin@gmail.com,arnd@arndb.de,alexs@kernel.org,mason@kernel.org, akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261127-mlock-madvise-mprotect-ai-fixes-v1-2-e46b2b1eb435@kernel.org> References: <20261127-mlock-madvise-mprotect-ai-fixes-v1-2-e46b2b1eb435@kernel.org> Subject: + mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch added to mm-new branch Message-Id: <20261201184822.1C96F1F00893@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/madvise: don't declare OOM for MADV_WILLNEED swapin has been added to the -mm mm-new branch. Its filename is mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Chris Mason Subject: mm/madvise: don't declare OOM for MADV_WILLNEED swapin Date: Fri, 27 Nov 2026 19:45:41 +0000 MADV_WILLNEED on memory in swap can OOM kill a task when the memcg that the swap entries are charged to is at its limit. The hint is only a prefetch, so it has to give up on a page when memory is short. But swapin_walk_pmd_entry() and shmem_swapin_range() call read_swap_cache_async() with GFP_HIGHUSER_MOVABLE and with the gfp mask of the mapping, so the allocation and the memcg charge can both go as far as the OOM killer. The charge goes to the memcg of the swap entry, which need not be the caller's. The fix used here is to allocate with the gfp mask of readahead, and to flush the block plug when a page fails. madvise_do_behavior() plugs the whole walk, so the swap writes of reclaim can sit in that plug. Their folios stay under writeback, and with no retries the rest of the range can fail as well. The hint can now skip pages when memory is short, whether in a memcg at its limit or on the system as a whole, since neither the allocation nor the memcg charge is retried any longer. This applies in particular where reclaim has to write to swap to make room. Skipped pages stay in swap until a fault reads them. [ljs@kernel.org: commit message tweak] Link: https://patch.msgid.link/20261127-mlock-madvise-mprotect-ai-fixes-v1-2-e46b2b1eb435@kernel.org Fixes: 4c6355b25e8b ("mm: memcontrol: charge swapin pages on instantiation") Signed-off-by: Chris Mason Signed-off-by: Lorenzo Stoakes (ARM) Signed-off-by: Andrew Morton Reviewed-by: Pedro Falcato Acked-by: David Hildenbrand (Arm) Cc: Jan Harkes Cc: David Rheinsberg Cc: Hugh Dickins Cc: Vlastimil Babka Cc: Liam R. Howlett Cc: Jann Horn Cc: Alex Shi Cc: Johannes Weiner Cc: David Rientjes Cc: Rik van Riel Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Michał Mirosław Cc: Arnd Bergmann Cc: Stephen Rothwell Cc: Muhammad Usama Anjum Cc: Andrei Vagin mm/madvise.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) --- a/mm/madvise.c~mm-madvise-dont-declare-oom-for-madv_willneed-swapin +++ a/mm/madvise.c @@ -195,6 +195,8 @@ static int madvise_update_vma(vma_flags_ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start, unsigned long end, struct mm_walk *walk) { + /* Only a prefetch, as in readahead_gfp_mask() */ + const gfp_t gfp = GFP_HIGHUSER_MOVABLE | __GFP_NORETRY | __GFP_NOWARN; struct vm_area_struct *vma = walk->private; struct swap_io_ctx ctx = {}; pte_t *ptep = NULL; @@ -220,10 +222,13 @@ static int swapin_walk_pmd_entry(pmd_t * pte_unmap_unlock(ptep, ptl); ptep = NULL; - folio = read_swap_cache_async(&ctx, entry, GFP_HIGHUSER_MOVABLE, - vma, addr); - if (folio) + folio = read_swap_cache_async(&ctx, entry, gfp, vma, addr); + if (folio) { folio_put(folio); + } else { + /* Reclaim queues its swap writes in our plug */ + blk_flush_plug(current->plug, false); + } } if (ptep) @@ -266,9 +271,13 @@ static void shmem_swapin_range(struct vm rcu_read_unlock(); folio = read_swap_cache_async(&ctx, entry, - mapping_gfp_mask(mapping), vma, addr); - if (folio) + readahead_gfp_mask(mapping), vma, addr); + if (folio) { folio_put(folio); + } else { + /* Reclaim queues its swap writes in our plug */ + blk_flush_plug(current->plug, false); + } rcu_read_lock(); } _ Patches currently in -mm which might be from mason@kernel.org are mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch