1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * include/linux/pagevec.h 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * In many places it is efficient to batch an operation up against multiple 6*1e0877d5SMatthew Wilcox (Oracle) * folios. A folio_batch is a container which is used for that. 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 978854014SDavid Howells #ifndef _LINUX_PAGEVEC_H 1078854014SDavid Howells #define _LINUX_PAGEVEC_H 1178854014SDavid Howells 12*1e0877d5SMatthew Wilcox (Oracle) #include <linux/types.h> 1310bbd235SMatthew Wilcox 14*1e0877d5SMatthew Wilcox (Oracle) /* 15 pointers + header align the folio_batch structure to a power of two */ 15146500e9SMatthew Wilcox #define PAGEVEC_SIZE 15 161da177e4SLinus Torvalds 1710331795SMatthew Wilcox (Oracle) struct folio; 181da177e4SLinus Torvalds 1910331795SMatthew Wilcox (Oracle) /** 2010331795SMatthew Wilcox (Oracle) * struct folio_batch - A collection of folios. 2110331795SMatthew Wilcox (Oracle) * 2210331795SMatthew Wilcox (Oracle) * The folio_batch is used to amortise the cost of retrieving and 2310331795SMatthew Wilcox (Oracle) * operating on a set of folios. The order of folios in the batch may be 2410331795SMatthew Wilcox (Oracle) * significant (eg delete_from_page_cache_batch()). Some users of the 2510331795SMatthew Wilcox (Oracle) * folio_batch store "exceptional" entries in it which can be removed 2610331795SMatthew Wilcox (Oracle) * by calling folio_batch_remove_exceptionals(). 2710331795SMatthew Wilcox (Oracle) */ 2810331795SMatthew Wilcox (Oracle) struct folio_batch { 2910331795SMatthew Wilcox (Oracle) unsigned char nr; 3010331795SMatthew Wilcox (Oracle) bool percpu_pvec_drained; 3110331795SMatthew Wilcox (Oracle) struct folio *folios[PAGEVEC_SIZE]; 3210331795SMatthew Wilcox (Oracle) }; 3310331795SMatthew Wilcox (Oracle) 3410331795SMatthew Wilcox (Oracle) /** 3510331795SMatthew Wilcox (Oracle) * folio_batch_init() - Initialise a batch of folios 3610331795SMatthew Wilcox (Oracle) * @fbatch: The folio batch. 3710331795SMatthew Wilcox (Oracle) * 3810331795SMatthew Wilcox (Oracle) * A freshly initialised folio_batch contains zero folios. 3910331795SMatthew Wilcox (Oracle) */ folio_batch_init(struct folio_batch * fbatch)4010331795SMatthew Wilcox (Oracle)static inline void folio_batch_init(struct folio_batch *fbatch) 4110331795SMatthew Wilcox (Oracle) { 4210331795SMatthew Wilcox (Oracle) fbatch->nr = 0; 436840f909SMatthew Wilcox (Oracle) fbatch->percpu_pvec_drained = false; 4410331795SMatthew Wilcox (Oracle) } 4510331795SMatthew Wilcox (Oracle) folio_batch_reinit(struct folio_batch * fbatch)4681156128SLorenzo Stoakesstatic inline void folio_batch_reinit(struct folio_batch *fbatch) 4781156128SLorenzo Stoakes { 4881156128SLorenzo Stoakes fbatch->nr = 0; 4981156128SLorenzo Stoakes } 5081156128SLorenzo Stoakes folio_batch_count(struct folio_batch * fbatch)5110331795SMatthew Wilcox (Oracle)static inline unsigned int folio_batch_count(struct folio_batch *fbatch) 5210331795SMatthew Wilcox (Oracle) { 5310331795SMatthew Wilcox (Oracle) return fbatch->nr; 5410331795SMatthew Wilcox (Oracle) } 5510331795SMatthew Wilcox (Oracle) folio_batch_space(struct folio_batch * fbatch)56ce064428SMatthew Wilcox (Oracle)static inline unsigned int folio_batch_space(struct folio_batch *fbatch) 5710331795SMatthew Wilcox (Oracle) { 5810331795SMatthew Wilcox (Oracle) return PAGEVEC_SIZE - fbatch->nr; 5910331795SMatthew Wilcox (Oracle) } 6010331795SMatthew Wilcox (Oracle) 6110331795SMatthew Wilcox (Oracle) /** 6210331795SMatthew Wilcox (Oracle) * folio_batch_add() - Add a folio to a batch. 6310331795SMatthew Wilcox (Oracle) * @fbatch: The folio batch. 6410331795SMatthew Wilcox (Oracle) * @folio: The folio to add. 6510331795SMatthew Wilcox (Oracle) * 6610331795SMatthew Wilcox (Oracle) * The folio is added to the end of the batch. 6710331795SMatthew Wilcox (Oracle) * The batch must have previously been initialised using folio_batch_init(). 6810331795SMatthew Wilcox (Oracle) * 6910331795SMatthew Wilcox (Oracle) * Return: The number of slots still available. 7010331795SMatthew Wilcox (Oracle) */ folio_batch_add(struct folio_batch * fbatch,struct folio * folio)7110331795SMatthew Wilcox (Oracle)static inline unsigned folio_batch_add(struct folio_batch *fbatch, 7210331795SMatthew Wilcox (Oracle) struct folio *folio) 7310331795SMatthew Wilcox (Oracle) { 7410331795SMatthew Wilcox (Oracle) fbatch->folios[fbatch->nr++] = folio; 75ce064428SMatthew Wilcox (Oracle) return folio_batch_space(fbatch); 7610331795SMatthew Wilcox (Oracle) } 7710331795SMatthew Wilcox (Oracle) 78*1e0877d5SMatthew Wilcox (Oracle) void __folio_batch_release(struct folio_batch *pvec); 79982a7194SMatthew Wilcox (Oracle) folio_batch_release(struct folio_batch * fbatch)8010331795SMatthew Wilcox (Oracle)static inline void folio_batch_release(struct folio_batch *fbatch) 8110331795SMatthew Wilcox (Oracle) { 82982a7194SMatthew Wilcox (Oracle) if (folio_batch_count(fbatch)) 83982a7194SMatthew Wilcox (Oracle) __folio_batch_release(fbatch); 8410331795SMatthew Wilcox (Oracle) } 8510331795SMatthew Wilcox (Oracle) 861613fac9SMatthew Wilcox (Oracle) void folio_batch_remove_exceptionals(struct folio_batch *fbatch); 8778854014SDavid Howells #endif /* _LINUX_PAGEVEC_H */ 88