mm: Optimise put_pages_list()
PATCH v2 1/1 mm: Optimise put_pages_list()
Instead of calling put_page() one page at a time, pop pages off the list if their refcount was too high and pass the remainder to put_unref_page_list(). This should be a speed improvement, but I have no measurements to support that. Current callers do not care about performance, but I hope to add some which do.
---
v2:
- Handle compound pages (Mel)
- Comment why we don't need to handle PageLRU
- Added call to __ClearPageWaiters(), matching that in release_pages()
mm/swap.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
mm/swap.c +16 -7
@@ -134,18 +134,27 @@ EXPORT_SYMBOL(__put_page);* put_pages_list() - release a list of pages * @pages: list of pages threaded on page->lru * -* Release a list of pages which are strung together on page.lru. Currently -* used by read_cache_pages() and related error recovery code. +* Release a list of pages which are strung together on page.lru. */ void put_pages_list (struct list_head *pages) { -while (!list_empty (pages)) { -struct page *victim; +struct page *page, *next; - victim =lru_to_page (pages); -list_del (&victim->lru); -put_page (victim); +list_for_each_entry_safe (page, next, pages, lru) { +if (!put_page_testzero (page)) { +list_del (&page->lru); +continue ; + } +if (PageHead (page)) { +list_del (&page->lru); +__put_compound_page (page); +continue ; + } +/* Cannot be PageLRU because it's passed to us using the lru */ +__ClearPageWaiters (page); } + +free_unref_page_list (pages); }EXPORT_SYMBOL (put_pages_list);
Signature · 1 line
2.32.0
Andrew Morton
maintainerQuoted 5 lines · On Thu, 7 Oct 2021 20:21:37 +0100 "Matthew Wilcox (Oracle)"…
Instead of calling
put_page()one page at a time, pop pages off the list if their refcount was too high and pass the remainder toput_unref_page_list(). This should be a speed improvement, but I have no measurements to support that. Current callers do not care about performance, but I hope to add some which do.
Don't you think it would actually be slower to take an additional pass across the list? If the list is long enough to cause cache thrashing. Maybe it's faster for small lists.
Quoted 11 lines · Andrew Morton
Quoted 5 lines · On Thu, 7 Oct 2021 20:21:37 +0100 "Matthew Wilcox (Oracle)"…
Instead of calling
put_page()one page at a time, pop pages off the list if their refcount was too high and pass the remainder toput_unref_page_list(). This should be a speed improvement, but I have no measurements to support that. Current callers do not care about performance, but I hope to add some which do.Don't you think it would actually be slower to take an additional pass across the list? If the list is long enough to cause cache thrashing. Maybe it's faster for small lists.
My first response is an appeal to authority -- release_pages() does this same thing. Only it takes an array, constructs a list and passes that to put_unref_page_list(). So if that's slower (and lists _are_ slower than arrays), we should have a put_unref_page_array().
Second, we can follow through the code paths and reason about it.
Before:
while (!list_empty(pages)) {
put_page(victim);
page = compound_head(page);
if (put_page_testzero(page))
__put_page(page);
__put_single_page(page)
__page_cache_release(page);
mem_cgroup_uncharge(page);
<---
free_unref_page(page, 0);
free_unref_page_prepare()
local_lock_irqsave(&pagesets.lock, flags);
free_unref_page_commit(page, pfn, migratetype, order);
local_unlock_irqrestore(&pagesets.lock, flags);After:
free_unref_page_list(pages);
list_for_each_entry_safe(page, next, list, lru) {
if (!free_unref_page_prepare(page, pfn, 0)) {
}
local_lock_irqsave(&pagesets.lock, flags);
list_for_each_entry_safe(page, next, list, lru) {
free_unref_page_commit()
}
local_unlock_irqrestore(&pagesets.lock, flags);So the major win here is that we disable/enable interrupts once per batch rather than once per page.
Andrew Morton
maintainerQuoted 9 lines · On Thu, 7 Oct 2021 21:55:21 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Thu, 7 Oct 2021 20:21:37 +0100 "Matthew Wilcox (Oracle)"…
Instead of calling
put_page()one page at a time, pop pages off the list if their refcount was too high and pass the remainder toput_unref_page_list(). This should be a speed improvement, but I have no measurements to support that. Current callers do not care about performance, but I hope to add some which do.
Quoted 8 lines
Quoted 3 lines
Don't you think it would actually be slower to take an additional pass across the list? If the list is long enough to cause cache thrashing. Maybe it's faster for small lists.
My first response is an appeal to authority --
release_pages()does this same thing. Only it takes an array, constructs a list and passes that toput_unref_page_list(). So if that's slower (and lists _are_ slower than arrays), we should have aput_unref_page_array().
And put_unref_page_list() does two passes across the list!
<quietly sobs>
Here is my beautiful release_pages(), as disrtibuted in linux-2.5.33:
void release_pages(struct page **pages, int nr)
{
int i;
struct pagevec pages_to_free;
struct zone *zone = NULL;
pagevec_init(&pages_to_free);
for (i = 0; i < nr; i++) {
struct page *page = pages[i];
struct zone *pagezone;
if (PageReserved(page) || !put_page_testzero(page))
continue;
pagezone = page_zone(page);
if (pagezone != zone) {
if (zone)
spin_unlock_irq(&zone->lru_lock);
zone = pagezone;
spin_lock_irq(&zone->lru_lock);
}
if (TestClearPageLRU(page))
del_page_from_lru(zone, page);
if (page_count(page) == 0) {
if (!pagevec_add(&pages_to_free, page)) {
spin_unlock_irq(&zone->lru_lock);
pagevec_free(&pages_to_free);
pagevec_init(&pages_to_free);
spin_lock_irq(&zone->lru_lock);
}
}
}
if (zone)
spin_unlock_irq(&zone->lru_lock);
pagevec_free(&pages_to_free);
}I guess the current version is some commentary on the aging process?
Quoted 32 lines
Second, we can follow through the code paths and reason about it.
Before:
while (!list_empty(pages)) { put_page(victim); page = compound_head(page); if (put_page_testzero(page)) __put_page(page); __put_single_page(page) __page_cache_release(page); mem_cgroup_uncharge(page); <--- free_unref_page(page, 0); free_unref_page_prepare() local_lock_irqsave(&pagesets.lock, flags); free_unref_page_commit(page, pfn, migratetype, order); local_unlock_irqrestore(&pagesets.lock, flags);After:
free_unref_page_list(pages); list_for_each_entry_safe(page, next, list, lru) { if (!free_unref_page_prepare(page, pfn, 0)) { } local_lock_irqsave(&pagesets.lock, flags); list_for_each_entry_safe(page, next, list, lru) { free_unref_page_commit() } local_unlock_irqrestore(&pagesets.lock, flags);
Quoted 2 lines
So the major win here is that we disable/enable interrupts once per batch rather than once per page.
Perhaps that's faster if the list is fully cached.
Any feelings for how often release_pages() will be passed a huge enough list for this to occur?
Quoted 11 lines · Andrew Morton
Quoted 4 lines · On Thu, 7 Oct 2021 21:55:21 +0100 Matthew Wilcox <[email protected]> wrote:
My first response is an appeal to authority --
release_pages()does this same thing. Only it takes an array, constructs a list and passes that toput_unref_page_list(). So if that's slower (and lists _are_ slower than arrays), we should have aput_unref_page_array().And
put_unref_page_list()does two passes across the list!<quietly sobs>
Here is my beautiful
release_pages(), as disrtibuted in linux-2.5.33:
I think that looks much better!
Quoted 1 line
I guess the current version is some commentary on the aging process?
I blame the guy who sent cc59850ef940 to Linus back in 2011 ...
I think Anthony was going to look into this and perhaps revert us to a pagevec_free() interface.
Quoted 10 lines · Matthew Wilcox
Quoted 9 lines · Andrew Morton
Quoted 4 lines · On Thu, 7 Oct 2021 21:55:21 +0100 Matthew Wilcox <[email protected]> wrote:
My first response is an appeal to authority --
release_pages()does this same thing. Only it takes an array, constructs a list and passes that toput_unref_page_list(). So if that's slower (and lists _are_ slower than arrays), we should have aput_unref_page_array().And
put_unref_page_list()does two passes across the list!<quietly sobs>
Quoted 8 lines
Quoted 1 line
Here is my beautiful
release_pages(), as disrtibuted in linux-2.5.33:I think that looks much better!
Quoted 1 line
I guess the current version is some commentary on the aging process?
I blame the guy who sent
cc59850ef940to Linus back in 2011 ...I think Anthony was going to look into this and perhaps revert us to a
pagevec_free()interface.
Sorry for not responding sooner. I have some cycles so I'll take a look at this now.
Anthony
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Thu, 7 Oct 2021 21:55:21 +0100 Matthew Wilcox <[email protected]> wrote:
My first response is an appeal to authority --
release_pages()does this same thing. Only it takes an array, constructs a list and passes that toput_unref_page_list(). So if that's slower (and lists _are_ slower than arrays), we should have aput_unref_page_array().
Quoted 8 lines
Quoted 5 lines
And
put_unref_page_list()does two passes across the list!<quietly sobs>
Here is my beautiful
release_pages(), as disrtibuted in linux-2.5.33:I think that looks much better!
For what is worth, something that was suggested by Jason over the unpin_user_pages* improvements series was to apply the same trick to how we iterate a page array to release_pages(). Purpose would be to batch the page refcount update if you pass N contiguous struct pages.
Joao
Quoted 32 lines · Matthew Wilcox (Oracle)
Instead of calling
put_page()one page at a time, pop pages off the list if their refcount was too high and pass the remainder toput_unref_page_list(). This should be a speed improvement, but I have no measurements to support that. Current callers do not care about performance, but I hope to add some which do.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>---
v2:
- Handle compound pages (Mel)
- Comment why we don't need to handle PageLRU
- Added call to__ClearPageWaiters(), matching that inrelease_pages()mm/swap.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)mm/swap.c +1
-4@@ -134,18 +134,27 @@ EXPORT_SYMBOL(__put_page);* put_pages_list() - release a list of pages * @pages: list of pages threaded on page->lru * -* Release a list of pages which are strung together on page.lru. Currently -* used by read_cache_pages() and related error recovery code. +* Release a list of pages which are strung together on page.lru. */ void put_pages_list (struct list_head *pages) { -while (!list_empty (pages)) { -struct page *victim;
mm/swap.c EXPORT_SYMBOL(__put_page); · line 147
Quoted 10 lines
mm/swap.c +6
-3+struct page *page, *next; - victim =lru_to_page (pages); -list_del (&victim->lru); -put_page (victim); +list_for_each_entry_safe (page, next, pages, lru) { +if (!put_page_testzero (page)) { +list_del (&page->lru); +continue ; + }
I know that compound pages are not currently passed to put_pages_list(),
but I assume the put_page_testzero() should only be done on the head
page similar to release_pages()?
Anthony
Quoted 13 lines
mm/swap.c +9
-0+if (PageHead (page)) { +list_del (&page->lru); +__put_compound_page (page); +continue ; + } +/* Cannot be PageLRU because it's passed to us using the lru */ +__ClearPageWaiters (page); } + +free_unref_page_list (pages); }EXPORT_SYMBOL (put_pages_list);
Quoted 40 lines · Anthony Yznaga
Quoted 38 lines · Matthew Wilcox (Oracle)
Instead of calling
put_page()one page at a time, pop pages off the list if their refcount was too high and pass the remainder toput_unref_page_list(). This should be a speed improvement, but I have no measurements to support that. Current callers do not care about performance, but I hope to add some which do.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>---
v2:
- Handle compound pages (Mel)
- Comment why we don't need to handle PageLRU
- Added call to__ClearPageWaiters(), matching that inrelease_pages()mm/swap.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)mm/swap.c +4
-7@@ -134,18 +134,27 @@ EXPORT_SYMBOL(__put_page);* put_pages_list() - release a list of pages * @pages: list of pages threaded on page->lru * -* Release a list of pages which are strung together on page.lru. Currently -* used by read_cache_pages() and related error recovery code. +* Release a list of pages which are strung together on page.lru. */ void put_pages_list (struct list_head *pages) { -while (!list_empty (pages)) { -struct page *victim; +struct page *page, *next; - victim =lru_to_page (pages); -list_del (&victim->lru); -put_page (victim); +list_for_each_entry_safe (page, next, pages, lru) { +if (!put_page_testzero (page)) {
Quoted 8 lines
mm/swap.c EXPORT_SYMBOL(__put_page); · line 147
Quoted 3 lines
mm/swap.c +3
-0+list_del (&page->lru); +continue ; + }I know that compound pages are not currently passed to
put_pages_list(), but I assume theput_page_testzero()should only be done on the head page similar torelease_pages()?
Fun fact about pages: You can't put a tail page on an LRU list. Why?
struct page {
...
union {
struct { /* Page cache and anonymous pages */
struct list_head lru;...
struct { /* Tail pages of compound page */
unsigned long compound_head; /* Bit zero is set */
so if you try to put a tail page on the LRU list, it becomes no longer a tail page.
Anthony Yznaga
Reviewed-byQuoted 54 lines · Matthew Wilcox
Quoted 38 lines · Anthony Yznaga
Quoted 37 lines · Matthew Wilcox (Oracle)
Instead of calling
put_page()one page at a time, pop pages off the list if their refcount was too high and pass the remainder toput_unref_page_list(). This should be a speed improvement, but I have no measurements to support that. Current callers do not care about performance, but I hope to add some which do.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>---
v2:
- Handle compound pages (Mel)
- Comment why we don't need to handle PageLRU
- Added call to__ClearPageWaiters(), matching that inrelease_pages()mm/swap.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)mm/swap.c +3
-7@@ -134,18 +134,27 @@ EXPORT_SYMBOL(__put_page);* put_pages_list() - release a list of pages * @pages: list of pages threaded on page->lru * -* Release a list of pages which are strung together on page.lru. Currently -* used by read_cache_pages() and related error recovery code. +* Release a list of pages which are strung together on page.lru. */ void put_pages_list (struct list_head *pages) { -while (!list_empty (pages)) { -struct page *victim; +struct page *page, *next; - victim =lru_to_page (pages); -list_del (&victim->lru); -put_page (victim); +list_for_each_entry_safe (page, next, pages, lru) {Quoted 8 lines
mm/swap.c EXPORT_SYMBOL(__put_page); · line 147
Quoted 4 lines
mm/swap.c +4
-0+if (!put_page_testzero (page)) { +list_del (&page->lru); +continue ; + }I know that compound pages are not currently passed to
put_pages_list(), but I assume theput_page_testzero()should only be done on the head page similar torelease_pages()?Fun fact about pages: You can't put a tail page on an LRU list. Why?
struct page {
...union { struct { /* Page cache and anonymous pages */ struct list_head lru;
Quoted 6 lines
...
struct { /* Tail pages of compound page */
unsigned longcompound_head; /* Bit zero is set */so if you try to put a tail page on the LRU list, it becomes no longer a tail page.
Ah, makes sense. Thanks!
In that case here's my r-b:
Anthony