@@ -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 */
381430static 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 */
390455static 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
408470static inline int filemap_nr_thps (struct address_space * mapping )
0 commit comments