diff --git a/mm/filemap.c b/mm/filemap.c index a1b2ca5b933c..053aa6a5e580 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1956,6 +1956,21 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index) return folio; } +/* + * A folio of the mapping's minimum order is the smallest unit the page cache + * can use for this mapping, so there is no smaller order to fall back to and + * a failed allocation is reported to userspace as -ENOMEM. The page allocator + * gives up on orders above PAGE_ALLOC_COSTLY_ORDER after a single round of + * reclaim and compaction, which happens easily on a fragmented system that + * has plenty of reclaimable memory. Ask it to try harder in that case. + */ +static gfp_t filemap_min_order_gfp(gfp_t gfp, unsigned int min_order) +{ + if (min_order > PAGE_ALLOC_COSTLY_ORDER && !(gfp & __GFP_NORETRY)) + gfp |= __GFP_RETRY_MAYFAIL; + return gfp; +} + /** * __filemap_get_folio_mpol - Find and get a reference to a folio. * @mapping: The address_space to search. @@ -2044,6 +2059,9 @@ struct folio *__filemap_get_folio_mpol(struct address_space *mapping, err = -ENOMEM; if (order > min_order) alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN; + else + alloc_gfp = filemap_min_order_gfp(alloc_gfp, + min_order); folio = filemap_alloc_folio(alloc_gfp, order, policy); if (!folio) continue; @@ -2652,11 +2670,13 @@ static int filemap_create_folio(struct kiocb *iocb, struct folio_batch *fbatch) int error; unsigned int min_order = mapping_min_folio_order(mapping); pgoff_t index; + gfp_t gfp; if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ)) return -EAGAIN; - folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order, NULL); + gfp = filemap_min_order_gfp(mapping_gfp_mask(mapping), min_order); + folio = filemap_alloc_folio(gfp, min_order, NULL); if (!folio) return -ENOMEM; if (iocb->ki_flags & IOCB_DONTCACHE) @@ -4149,7 +4169,10 @@ static struct folio *do_read_cache_folio(struct address_space *mapping, repeat: folio = filemap_get_folio(mapping, index); if (IS_ERR(folio)) { - folio = filemap_alloc_folio(gfp, mapping_min_folio_order(mapping), NULL); + unsigned int min_order = mapping_min_folio_order(mapping); + + folio = filemap_alloc_folio(filemap_min_order_gfp(gfp, min_order), + min_order, NULL); if (!folio) return ERR_PTR(-ENOMEM); index = mapping_align_index(mapping, index);