All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] A small folio API enhancement for 5.17
@ 2022-01-13 22:08 Matthew Wilcox (Oracle)
  2022-01-13 22:08 ` [PATCH 1/2] mm: Add folio_put_refs() Matthew Wilcox (Oracle)
  2022-01-13 22:08 ` [PATCH 2/2] filemap: Use folio_put_refs() in filemap_free_folio() Matthew Wilcox (Oracle)
  0 siblings, 2 replies; 3+ messages in thread
From: Matthew Wilcox (Oracle) @ 2022-01-13 22:08 UTC (permalink / raw)
  To: linux-kernel, linux-mm, linux-fsdevel; +Cc: Matthew Wilcox (Oracle)

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(-)

-- 
2.33.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] mm: Add folio_put_refs()
  2022-01-13 22:08 [PATCH 0/2] A small folio API enhancement for 5.17 Matthew Wilcox (Oracle)
@ 2022-01-13 22:08 ` Matthew Wilcox (Oracle)
  2022-01-13 22:08 ` [PATCH 2/2] filemap: Use folio_put_refs() in filemap_free_folio() Matthew Wilcox (Oracle)
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Wilcox (Oracle) @ 2022-01-13 22:08 UTC (permalink / raw)
  To: linux-kernel, linux-mm, linux-fsdevel
  Cc: Matthew Wilcox (Oracle),
	Christoph Hellwig, John Hubbard, Jason Gunthorpe,
	William Kucharski

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>
---
 include/linux/mm.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index c768a7c81b0b..cb98f75b245e 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -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);
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] filemap: Use folio_put_refs() in filemap_free_folio()
  2022-01-13 22:08 [PATCH 0/2] A small folio API enhancement for 5.17 Matthew Wilcox (Oracle)
  2022-01-13 22:08 ` [PATCH 1/2] mm: Add folio_put_refs() Matthew Wilcox (Oracle)
@ 2022-01-13 22:08 ` Matthew Wilcox (Oracle)
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Wilcox (Oracle) @ 2022-01-13 22:08 UTC (permalink / raw)
  To: linux-kernel, linux-mm, linux-fsdevel; +Cc: 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.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 mm/filemap.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 2fd9b2f24025..afc8f5ca85ac 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -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);
 }
 
 /**
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-13 22:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-13 22:08 [PATCH 0/2] A small folio API enhancement for 5.17 Matthew Wilcox (Oracle)
2022-01-13 22:08 ` [PATCH 1/2] mm: Add folio_put_refs() Matthew Wilcox (Oracle)
2022-01-13 22:08 ` [PATCH 2/2] filemap: Use folio_put_refs() in filemap_free_folio() Matthew Wilcox (Oracle)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.