mm: Add folio_put_refs()

This is like folio_put(), but puts N references at once instead of
just one. It's like put_page_refs(), but does one atomic operation
instead of two, and is available to more than just gup.c.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: John Hubbard <jhubbard@nvidia.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: William Kucharski <william.kucharski@oracle.com>

+20
+20
include/linux/mm.h
··· 1244 1244 __put_page(&folio->page); 1245 1245 } 1246 1246 1247 + /** 1248 + * folio_put_refs - Reduce the reference count on a folio. 1249 + * @folio: The folio. 1250 + * @refs: The amount to subtract from the folio's reference count. 1251 + * 1252 + * If the folio's reference count reaches zero, the memory will be 1253 + * released back to the page allocator and may be used by another 1254 + * allocation immediately. Do not access the memory or the struct folio 1255 + * after calling folio_put_refs() unless you can be sure that these weren't 1256 + * the last references. 1257 + * 1258 + * Context: May be called in process or interrupt context, but not in NMI 1259 + * context. May be called while holding a spinlock. 1260 + */ 1261 + static inline void folio_put_refs(struct folio *folio, int refs) 1262 + { 1263 + if (folio_ref_sub_and_test(folio, refs)) 1264 + __put_page(&folio->page); 1265 + } 1266 + 1247 1267 static inline void put_page(struct page *page) 1248 1268 { 1249 1269 struct folio *folio = page_folio(page);