Skip to content

Commit ab95d23

Browse files
Panky-codesbrauner
authored andcommitted
filemap: allocate mapping_min_order folios in the page cache
filemap_create_folio() and do_read_cache_folio() were always allocating folio of order 0. __filemap_get_folio was trying to allocate higher order folios when fgp_flags had higher order hint set but it will default to order 0 folio if higher order memory allocation fails. Supporting mapping_min_order implies that we guarantee each folio in the page cache has at least an order of mapping_min_order. When adding new folios to the page cache we must also ensure the index used is aligned to the mapping_min_order as the page cache requires the index to be aligned to the order of the folio. Co-developed-by: Luis Chamberlain <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]> Signed-off-by: Pankaj Raghav <[email protected]> Link: https://lore.kernel.org/r/[email protected] Tested-by: David Howells <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Reviewed-by: Matthew Wilcox (Oracle) <[email protected]> Reviewed-by: Daniel Gomez <[email protected]> Reviewed-by: Dave Chinner <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 84429b6 commit ab95d23

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

include/linux/pagemap.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,26 @@ mapping_min_folio_order(const struct address_space *mapping)
448448
return (mapping->flags & AS_FOLIO_ORDER_MIN_MASK) >> AS_FOLIO_ORDER_MIN;
449449
}
450450

451+
static inline unsigned long
452+
mapping_min_folio_nrpages(struct address_space *mapping)
453+
{
454+
return 1UL << mapping_min_folio_order(mapping);
455+
}
456+
457+
/**
458+
* mapping_align_index() - Align index for this mapping.
459+
* @mapping: The address_space.
460+
*
461+
* The index of a folio must be naturally aligned. If you are adding a
462+
* new folio to the page cache and need to know what index to give it,
463+
* call this function.
464+
*/
465+
static inline pgoff_t mapping_align_index(struct address_space *mapping,
466+
pgoff_t index)
467+
{
468+
return round_down(index, mapping_min_folio_nrpages(mapping));
469+
}
470+
451471
/*
452472
* Large folio support currently depends on THP. These dependencies are
453473
* being worked on but are not yet fixed.

mm/filemap.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,8 @@ noinline int __filemap_add_folio(struct address_space *mapping,
859859

860860
VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
861861
VM_BUG_ON_FOLIO(folio_test_swapbacked(folio), folio);
862+
VM_BUG_ON_FOLIO(folio_order(folio) < mapping_min_folio_order(mapping),
863+
folio);
862864
mapping_set_update(&xas, mapping);
863865

864866
VM_BUG_ON_FOLIO(index & (folio_nr_pages(folio) - 1), folio);
@@ -1919,8 +1921,10 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
19191921
folio_wait_stable(folio);
19201922
no_page:
19211923
if (!folio && (fgp_flags & FGP_CREAT)) {
1922-
unsigned order = FGF_GET_ORDER(fgp_flags);
1924+
unsigned int min_order = mapping_min_folio_order(mapping);
1925+
unsigned int order = max(min_order, FGF_GET_ORDER(fgp_flags));
19231926
int err;
1927+
index = mapping_align_index(mapping, index);
19241928

19251929
if ((fgp_flags & FGP_WRITE) && mapping_can_writeback(mapping))
19261930
gfp |= __GFP_WRITE;
@@ -1943,7 +1947,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
19431947
gfp_t alloc_gfp = gfp;
19441948

19451949
err = -ENOMEM;
1946-
if (order > 0)
1950+
if (order > min_order)
19471951
alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
19481952
folio = filemap_alloc_folio(alloc_gfp, order);
19491953
if (!folio)
@@ -1958,7 +1962,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
19581962
break;
19591963
folio_put(folio);
19601964
folio = NULL;
1961-
} while (order-- > 0);
1965+
} while (order-- > min_order);
19621966

19631967
if (err == -EEXIST)
19641968
goto repeat;
@@ -2447,13 +2451,15 @@ static int filemap_update_page(struct kiocb *iocb,
24472451
}
24482452

24492453
static int filemap_create_folio(struct file *file,
2450-
struct address_space *mapping, pgoff_t index,
2454+
struct address_space *mapping, loff_t pos,
24512455
struct folio_batch *fbatch)
24522456
{
24532457
struct folio *folio;
24542458
int error;
2459+
unsigned int min_order = mapping_min_folio_order(mapping);
2460+
pgoff_t index;
24552461

2456-
folio = filemap_alloc_folio(mapping_gfp_mask(mapping), 0);
2462+
folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order);
24572463
if (!folio)
24582464
return -ENOMEM;
24592465

@@ -2471,6 +2477,7 @@ static int filemap_create_folio(struct file *file,
24712477
* well to keep locking rules simple.
24722478
*/
24732479
filemap_invalidate_lock_shared(mapping);
2480+
index = (pos >> (PAGE_SHIFT + min_order)) << min_order;
24742481
error = filemap_add_folio(mapping, folio, index,
24752482
mapping_gfp_constraint(mapping, GFP_KERNEL));
24762483
if (error == -EEXIST)
@@ -2531,8 +2538,7 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,
25312538
if (!folio_batch_count(fbatch)) {
25322539
if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
25332540
return -EAGAIN;
2534-
err = filemap_create_folio(filp, mapping,
2535-
iocb->ki_pos >> PAGE_SHIFT, fbatch);
2541+
err = filemap_create_folio(filp, mapping, iocb->ki_pos, fbatch);
25362542
if (err == AOP_TRUNCATED_PAGE)
25372543
goto retry;
25382544
return err;
@@ -3748,9 +3754,11 @@ static struct folio *do_read_cache_folio(struct address_space *mapping,
37483754
repeat:
37493755
folio = filemap_get_folio(mapping, index);
37503756
if (IS_ERR(folio)) {
3751-
folio = filemap_alloc_folio(gfp, 0);
3757+
folio = filemap_alloc_folio(gfp,
3758+
mapping_min_folio_order(mapping));
37523759
if (!folio)
37533760
return ERR_PTR(-ENOMEM);
3761+
index = mapping_align_index(mapping, index);
37543762
err = filemap_add_folio(mapping, folio, index, gfp);
37553763
if (unlikely(err)) {
37563764
folio_put(folio);

0 commit comments

Comments
 (0)