Skip to content

Commit 84429b6

Browse files
Matthew Wilcox (Oracle)brauner
authored andcommitted
fs: Allow fine-grained control of folio sizes
We need filesystems to be able to communicate acceptable folio sizes to the pagecache for a variety of uses (e.g. large block sizes). Support a range of folio sizes between order-0 and order-31. Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Co-developed-by: Pankaj Raghav <[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: Daniel Gomez <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 8400291 commit 84429b6

File tree

3 files changed

+80
-20
lines changed

3 files changed

+80
-20
lines changed

include/linux/pagemap.h

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,21 @@ enum mapping_flags {
204204
AS_EXITING = 4, /* final truncate in progress */
205205
/* writeback related tags are not used */
206206
AS_NO_WRITEBACK_TAGS = 5,
207-
AS_LARGE_FOLIO_SUPPORT = 6,
208-
AS_RELEASE_ALWAYS, /* Call ->release_folio(), even if no private data */
209-
AS_STABLE_WRITES, /* must wait for writeback before modifying
207+
AS_RELEASE_ALWAYS = 6, /* Call ->release_folio(), even if no private data */
208+
AS_STABLE_WRITES = 7, /* must wait for writeback before modifying
210209
folio contents */
211-
AS_INACCESSIBLE, /* Do not attempt direct R/W access to the mapping,
212-
including to move the mapping */
210+
AS_INACCESSIBLE = 8, /* Do not attempt direct R/W access to the mapping */
211+
/* Bits 16-25 are used for FOLIO_ORDER */
212+
AS_FOLIO_ORDER_BITS = 5,
213+
AS_FOLIO_ORDER_MIN = 16,
214+
AS_FOLIO_ORDER_MAX = AS_FOLIO_ORDER_MIN + AS_FOLIO_ORDER_BITS,
213215
};
214216

217+
#define AS_FOLIO_ORDER_BITS_MASK ((1u << AS_FOLIO_ORDER_BITS) - 1)
218+
#define AS_FOLIO_ORDER_MIN_MASK (AS_FOLIO_ORDER_BITS_MASK << AS_FOLIO_ORDER_MIN)
219+
#define AS_FOLIO_ORDER_MAX_MASK (AS_FOLIO_ORDER_BITS_MASK << AS_FOLIO_ORDER_MAX)
220+
#define AS_FOLIO_ORDER_MASK (AS_FOLIO_ORDER_MIN_MASK | AS_FOLIO_ORDER_MAX_MASK)
221+
215222
/**
216223
* mapping_set_error - record a writeback error in the address_space
217224
* @mapping: the mapping in which an error should be set
@@ -367,9 +374,51 @@ static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
367374
#define MAX_XAS_ORDER (XA_CHUNK_SHIFT * 2 - 1)
368375
#define MAX_PAGECACHE_ORDER min(MAX_XAS_ORDER, PREFERRED_MAX_PAGECACHE_ORDER)
369376

377+
/*
378+
* mapping_set_folio_order_range() - Set the orders supported by a file.
379+
* @mapping: The address space of the file.
380+
* @min: Minimum folio order (between 0-MAX_PAGECACHE_ORDER inclusive).
381+
* @max: Maximum folio order (between @min-MAX_PAGECACHE_ORDER inclusive).
382+
*
383+
* The filesystem should call this function in its inode constructor to
384+
* indicate which base size (min) and maximum size (max) of folio the VFS
385+
* can use to cache the contents of the file. This should only be used
386+
* if the filesystem needs special handling of folio sizes (ie there is
387+
* something the core cannot know).
388+
* Do not tune it based on, eg, i_size.
389+
*
390+
* Context: This should not be called while the inode is active as it
391+
* is non-atomic.
392+
*/
393+
static inline void mapping_set_folio_order_range(struct address_space *mapping,
394+
unsigned int min,
395+
unsigned int max)
396+
{
397+
if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
398+
return;
399+
400+
if (min > MAX_PAGECACHE_ORDER)
401+
min = MAX_PAGECACHE_ORDER;
402+
403+
if (max > MAX_PAGECACHE_ORDER)
404+
max = MAX_PAGECACHE_ORDER;
405+
406+
if (max < min)
407+
max = min;
408+
409+
mapping->flags = (mapping->flags & ~AS_FOLIO_ORDER_MASK) |
410+
(min << AS_FOLIO_ORDER_MIN) | (max << AS_FOLIO_ORDER_MAX);
411+
}
412+
413+
static inline void mapping_set_folio_min_order(struct address_space *mapping,
414+
unsigned int min)
415+
{
416+
mapping_set_folio_order_range(mapping, min, MAX_PAGECACHE_ORDER);
417+
}
418+
370419
/**
371420
* mapping_set_large_folios() - Indicate the file supports large folios.
372-
* @mapping: The file.
421+
* @mapping: The address space of the file.
373422
*
374423
* The filesystem should call this function in its inode constructor to
375424
* indicate that the VFS can use large folios to cache the contents of
@@ -380,7 +429,23 @@ static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
380429
*/
381430
static inline void mapping_set_large_folios(struct address_space *mapping)
382431
{
383-
__set_bit(AS_LARGE_FOLIO_SUPPORT, &mapping->flags);
432+
mapping_set_folio_order_range(mapping, 0, MAX_PAGECACHE_ORDER);
433+
}
434+
435+
static inline unsigned int
436+
mapping_max_folio_order(const struct address_space *mapping)
437+
{
438+
if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
439+
return 0;
440+
return (mapping->flags & AS_FOLIO_ORDER_MAX_MASK) >> AS_FOLIO_ORDER_MAX;
441+
}
442+
443+
static inline unsigned int
444+
mapping_min_folio_order(const struct address_space *mapping)
445+
{
446+
if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
447+
return 0;
448+
return (mapping->flags & AS_FOLIO_ORDER_MIN_MASK) >> AS_FOLIO_ORDER_MIN;
384449
}
385450

386451
/*
@@ -389,20 +454,17 @@ static inline void mapping_set_large_folios(struct address_space *mapping)
389454
*/
390455
static inline bool mapping_large_folio_support(struct address_space *mapping)
391456
{
392-
/* AS_LARGE_FOLIO_SUPPORT is only reasonable for pagecache folios */
457+
/* AS_FOLIO_ORDER is only reasonable for pagecache folios */
393458
VM_WARN_ONCE((unsigned long)mapping & PAGE_MAPPING_ANON,
394459
"Anonymous mapping always supports large folio");
395460

396-
return IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
397-
test_bit(AS_LARGE_FOLIO_SUPPORT, &mapping->flags);
461+
return mapping_max_folio_order(mapping) > 0;
398462
}
399463

400464
/* Return the maximum folio size for this pagecache mapping, in bytes. */
401-
static inline size_t mapping_max_folio_size(struct address_space *mapping)
465+
static inline size_t mapping_max_folio_size(const struct address_space *mapping)
402466
{
403-
if (mapping_large_folio_support(mapping))
404-
return PAGE_SIZE << MAX_PAGECACHE_ORDER;
405-
return PAGE_SIZE;
467+
return PAGE_SIZE << mapping_max_folio_order(mapping);
406468
}
407469

408470
static inline int filemap_nr_thps(struct address_space *mapping)

mm/filemap.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,10 +1933,8 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
19331933
if (WARN_ON_ONCE(!(fgp_flags & (FGP_LOCK | FGP_FOR_MMAP))))
19341934
fgp_flags |= FGP_LOCK;
19351935

1936-
if (!mapping_large_folio_support(mapping))
1937-
order = 0;
1938-
if (order > MAX_PAGECACHE_ORDER)
1939-
order = MAX_PAGECACHE_ORDER;
1936+
if (order > mapping_max_folio_order(mapping))
1937+
order = mapping_max_folio_order(mapping);
19401938
/* If we're not aligned, allocate a smaller folio */
19411939
if (index & ((1UL << order) - 1))
19421940
order = __ffs(index);

mm/readahead.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,10 @@ void page_cache_ra_order(struct readahead_control *ractl,
449449

450450
limit = min(limit, index + ra->size - 1);
451451

452-
if (new_order < MAX_PAGECACHE_ORDER)
452+
if (new_order < mapping_max_folio_order(mapping))
453453
new_order += 2;
454454

455-
new_order = min_t(unsigned int, MAX_PAGECACHE_ORDER, new_order);
455+
new_order = min(mapping_max_folio_order(mapping), new_order);
456456
new_order = min_t(unsigned int, new_order, ilog2(ra->size));
457457

458458
/* See comment in page_cache_ra_unbounded() */

0 commit comments

Comments
 (0)