A small folio API enhancement for 5.17
Subsystems (4)
Matthew Wilcox (Oracle)
maintainercover A small folio API enhancement for 5.17
It turns out I want to use folio_put_refs() in two completely different places (which would likely be in separate pull requests) for 5.18, and there's a useful place to use it in 5.17, so I'm planning on sending Linus a pull request for this pair of patches next week. The reviews on patch 1 come from the folio GUP reviews over the last week, but I doubt I've shown patch 2 to anybody before.
Matthew Wilcox (Oracle) (2):
mm: Add folio_put_refs()
filemap: Use folio_put_refs() in filemap_free_folio()
include/linux/mm.h | 20 ++++++++++++++++++++ mm/filemap.c | 10 ++++------ 2 files changed, 24 insertions(+), 6 deletions(-)
Signature · 1 line
2.33.0
Matthew Wilcox (Oracle)
maintainerPATCH 1/2 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.
--- include/linux/mm.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
include/linux/mm.h +20 -0
@@ -1244,6 +1244,26 @@ static inline void folio_put(struct folio *folio)__put_page (&folio->page); } +/** +* folio_put_refs - Reduce the reference count on a folio. +* @folio: The folio. +* @refs: The amount to subtract from the folio's reference count. +* +* If the folio's reference count reaches zero, the memory will be +* released back to the page allocator and may be used by another +* allocation immediately. Do not access the memory or the struct folio +* after calling folio_put_refs() unless you can be sure that these weren't +* the last references. +* +* Context: May be called in process or interrupt context, but not in NMI +* context. May be called while holding a spinlock. +*/ +static inline void folio_put_refs (struct folio *folio,int refs) +{ +if (folio_ref_sub_and_test (folio, refs)) +__put_page (&folio->page); +} +static inline void put_page (struct page *page) {struct folio *folio =page_folio (page);
Signature · 1 line
2.33.0
Matthew Wilcox (Oracle)
maintainerPATCH 2/2 filemap: Use folio_put_refs() in filemap_free_folio()
In reply to Matthew Wilcox (Oracle)This shrinks filemap_free_folio() by 55 bytes in my .config; 24 bytes from removing the VM_BUG_ON_FOLIO() and 31 bytes from unifying the small/large folio paths.
We could just use folio_ref_sub() here since the caller should hold a reference (as the VM_BUG_ON_FOLIO() was asserting), but that's fragile.
--- mm/filemap.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
mm/filemap.c +4 -6
@@ -231,17 +231,15 @@ void __filemap_remove_folio(struct folio *folio, void *shadow)void filemap_free_folio (struct address_space *mapping,struct folio *folio) {void (*freepage)(struct page *); +int refs =1 ; freepage = mapping->a_ops->freepage;if (freepage)freepage (&folio->page); -if (folio_test_large (folio) && !folio_test_hugetlb (folio)) { -folio_ref_sub (folio,folio_nr_pages (folio)); -VM_BUG_ON_FOLIO (folio_ref_count (folio) <=0 , folio); - }else { -folio_put (folio); - } +if (folio_test_large (folio) && !folio_test_hugetlb (folio)) + refs =folio_nr_pages (folio); +folio_put_refs (folio, refs); }/**
Signature · 1 line
2.33.0