Speed up page cache truncation
Subsystems (4)
Jan Kara
maintainerv2 cover Speed up page cache truncation
Hello,
when rebasing our enterprise distro to a newer kernel (from 4.4 to 4.12) we have noticed a regression in bonnie++ benchmark when deleting files. Eventually we have tracked this down to a fact that page cache truncation got slower by about 10%. There were both gains and losses in the above interval of kernels but we have been able to identify that commit 83929372f629 "filemap:
prepare find and delete operations for huge pages" caused about 10% regression on its own.
After some investigation it didn't seem easily possible to fix the regression while maintaining the THP in page cache functionality so we've decided to optimize the page cache truncation path instead to make up for the change. This series is a result of that effort.
Patch 1 is an easy speedup of cancel_dirty_page(). Patches 2-6 refactor page cache truncation code so that it is easier to batch radix tree operations. Patch 7 implements batching of deletes from the radix tree which more than makes up for the original regression.
Andrew, can you please consider merging these patches? Thanks!
Changes since v1:
* Added acks and reviewed-by tags
Honza
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerPATCH v2 1/7 mm: Speedup cancel_dirty_page() for clean pages
cancel_dirty_page() does quite some work even for clean pages (fetching of mapping, locking of memcg, atomic bit op on page flags) so it accounts for ~2.5% of cost of truncation of a clean page. That is not much but still dumb for something we don't need at all. Check whether a page is actually dirty and avoid any work if not.
--- include/linux/mm.h | 8 +++++++- mm/page-writeback.c | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-)
include/linux/mm.h +7 -1
@@ -1430,7 +1430,13 @@ void account_page_cleaned(struct page *page, struct address_space *mapping, struct bdi_writeback *wb);int set_page_dirty (struct page *page);int set_page_dirty_lock (struct page *page); -void cancel_dirty_page (struct page *page); +void __cancel_dirty_page (struct page *page); +static inline void cancel_dirty_page (struct page *page) +{ +/* Avoid atomic ops, locking, etc. when not actually needed. */ +if (PageDirty (page)) +__cancel_dirty_page (page); +}int clear_page_dirty_for_io (struct page *page);int get_cmdline (struct task_struct *task,char *buffer,int buflen);
mm/page-writeback.c +2 -2
@@ -2623,7 +2623,7 @@ EXPORT_SYMBOL(set_page_dirty_lock);* page without actually doing it through the VM. Can you say "ext3 is * horribly ugly"? Thought you could. */ -void cancel_dirty_page (struct page *page) +void __cancel_dirty_page (struct page *page) {struct address_space *mapping =page_mapping (page);
@@ -2644,7 +2644,7 @@ void cancel_dirty_page(struct page *page)ClearPageDirty (page); } } -EXPORT_SYMBOL (cancel_dirty_page); +EXPORT_SYMBOL (__cancel_dirty_page);/* * Clear a page's dirty flag, while caring for dirty memory accounting.
Signature · 7 lines
2.12.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerPATCH v2 2/7 mm: Refactor truncate_complete_page()
In reply to Jan KaraMove call of delete_from_page_cache() and page->mapping check out of truncate_complete_page() into the single caller - truncate_inode_page(). Also move page_mapped() check into truncate_complete_page(). That way it will be easier to batch operations. Also rename truncate_complete_page() to truncate_cleanup_page().
--- mm/truncate.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-)
mm/truncate.c +16 -14
@@ -134,11 +134,17 @@ void do_invalidatepage(struct page *page, unsigned int offset,* its lock, b) when a concurrent invalidate_mapping_pages got there first and * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space. */ -static int -truncate_complete_page (struct address_space *mapping,struct page *page) +static void +truncate_cleanup_page (struct address_space *mapping,struct page *page) { -if (page->mapping != mapping) -return -EIO ; +if (page_mapped (page)) { +loff_t holelen; + + holelen =PageTransHuge (page) ?HPAGE_PMD_SIZE :PAGE_SIZE ; +unmap_mapping_range (mapping, + (loff_t )page->index <<PAGE_SHIFT , + holelen,0 ); + }if (page_has_private (page))do_invalidatepage (page,0 ,PAGE_SIZE );
@@ -150,8 +156,6 @@ truncate_complete_page(struct address_space *mapping, struct page *page)*/ cancel_dirty_page (page);ClearPageMappedToDisk (page); -delete_from_page_cache (page); -return 0 ; }/*
@@ -180,16 +184,14 @@ invalidate_complete_page(struct address_space *mapping, struct page *page)int truncate_inode_page (struct address_space *mapping,struct page *page) { -loff_t holelen;VM_BUG_ON_PAGE (PageTail (page), page); - holelen =PageTransHuge (page) ?HPAGE_PMD_SIZE :PAGE_SIZE ; -if (page_mapped (page)) { -unmap_mapping_range (mapping, - (loff_t )page->index <<PAGE_SHIFT , - holelen,0 ); - } -return truncate_complete_page (mapping, page); +if (page->mapping != mapping) +return -EIO ; + +truncate_cleanup_page (mapping, page); +delete_from_page_cache (page); +return 0 ; }/*
Signature · 7 lines
2.12.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerPATCH v2 3/7 mm: Factor out page cache page freeing into a separate function
In reply to Jan KaraFactor out page freeing from delete_from_page_cache() into a separate function. We will need to call the same when batching pagecache deletion operations.
invalidate_complete_page2() and replace_page_cache_page() might want to call this function as well however they currently don't seem to handle THPs so it's unnecessary for them to take the hit of checking whether a page is THP or not.
--- mm/filemap.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)
mm/filemap.c +18 -13
@@ -254,6 +254,23 @@ void __delete_from_page_cache(struct page *page, void *shadow)account_page_cleaned (page, mapping,inode_to_wb (mapping->host)); } +static void page_cache_free_page (struct address_space *mapping, +struct page *page) +{ +void (*freepage)(struct page *); + + freepage = mapping->a_ops->freepage; +if (freepage) +freepage (page); + +if (PageTransHuge (page) && !PageHuge (page)) { +page_ref_sub (page,HPAGE_PMD_NR ); +VM_BUG_ON_PAGE (page_count (page) <=0 , page); + }else { +put_page (page); + } +} +/** * delete_from_page_cache - delete page from page cache * @page: the page which the kernel is trying to remove from page cache
@@ -266,25 +283,13 @@ void delete_from_page_cache(struct page *page) {struct address_space *mapping =page_mapping (page);unsigned long flags; -void (*freepage)(struct page *);BUG_ON (!PageLocked (page)); - - freepage = mapping->a_ops->freepage; -spin_lock_irqsave (&mapping->tree_lock, flags);__delete_from_page_cache (page,NULL );spin_unlock_irqrestore (&mapping->tree_lock, flags); -if (freepage) -freepage (page); - -if (PageTransHuge (page) && !PageHuge (page)) { -page_ref_sub (page,HPAGE_PMD_NR ); -VM_BUG_ON_PAGE (page_count (page) <=0 , page); - }else { -put_page (page); - } +page_cache_free_page (mapping, page); }EXPORT_SYMBOL (delete_from_page_cache);
Signature · 7 lines
2.12.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerPATCH v2 4/7 mm: Move accounting updates before page_cache_tree_delete()
In reply to Jan KaraMove updates of various counters before page_cache_tree_delete() call. It will be easier to batch things this way and there is no difference whether the counters get updated before or after removal from the radix tree.
--- mm/filemap.c | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-)
mm/filemap.c +25 -24
@@ -224,34 +224,35 @@ void __delete_from_page_cache(struct page *page, void *shadow) } } -page_cache_tree_delete (mapping, page, shadow); - - page->mapping =NULL ; -/* Leave page->index set: truncation lookup relies upon it */ -/* hugetlb pages do not participate in page cache accounting. */ -if (PageHuge (page)) -return ; +if (!PageHuge (page)) { +__mod_node_page_state (page_pgdat (page),NR_FILE_PAGES , -nr); +if (PageSwapBacked (page)) { +__mod_node_page_state (page_pgdat (page),NR_SHMEM , -nr); +if (PageTransHuge (page)) +__dec_node_page_state (page,NR_SHMEM_THPS ); + }else { +VM_BUG_ON_PAGE (PageTransHuge (page), page); + } -__mod_node_page_state (page_pgdat (page),NR_FILE_PAGES , -nr); -if (PageSwapBacked (page)) { -__mod_node_page_state (page_pgdat (page),NR_SHMEM , -nr); -if (PageTransHuge (page)) -__dec_node_page_state (page,NR_SHMEM_THPS ); - }else { -VM_BUG_ON_PAGE (PageTransHuge (page), page); +/* +* At this point page must be either written or cleaned by +* truncate. Dirty page here signals a bug and loss of +* unwritten data. +* +* This fixes dirty accounting after removing the page entirely +* but leaves PageDirty set: it has no effect for truncated +* page and anyway will be cleared before returning page into +* buddy allocator. +*/ +if (WARN_ON_ONCE (PageDirty (page))) +account_page_cleaned (page, mapping, +inode_to_wb (mapping->host)); } +page_cache_tree_delete (mapping, page, shadow); -/* -* At this point page must be either written or cleaned by truncate. -* Dirty page here signals a bug and loss of unwritten data. -* -* This fixes dirty accounting after removing the page entirely but -* leaves PageDirty set: it has no effect for truncated page and -* anyway will be cleared before returning page into buddy allocator. -*/ -if (WARN_ON_ONCE (PageDirty (page))) -account_page_cleaned (page, mapping,inode_to_wb (mapping->host)); + page->mapping =NULL ; +/* Leave page->index set: truncation lookup relies upon it */ } static void page_cache_free_page(struct address_space *mapping,
Signature · 7 lines
2.12.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerPATCH v2 5/7 mm: Move clearing of page->mapping to page_cache_tree_delete()
In reply to Jan KaraClearing of page->mapping makes sense in page_cache_tree_delete() as well and it will help us with batching things this way.
--- mm/filemap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/filemap.c +3 -3
@@ -165,6 +165,9 @@ static void page_cache_tree_delete(struct address_space *mapping, workingset_update_node, mapping); } + page->mapping =NULL ; +/* Leave page->index set: truncation lookup relies upon it */ +if (shadow) { mapping->nrexceptional += nr;/*
@@ -250,9 +253,6 @@ void __delete_from_page_cache(struct page *page, void *shadow) inode_to_wb(mapping->host)); }page_cache_tree_delete (mapping, page, shadow); - - page->mapping =NULL ; -/* Leave page->index set: truncation lookup relies upon it */ } static void page_cache_free_page(struct address_space *mapping,
Signature · 7 lines
2.12.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerPATCH v2 6/7 mm: Factor out checks and accounting from __delete_from_page_cache()
In reply to Jan KaraMove checks and accounting updates from __delete_from_page_cache() into a separate function. We will reuse it when batching page cache truncation operations.
--- mm/filemap.c | 72 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 31 deletions(-)
mm/filemap.c +41 -31
@@ -181,17 +181,11 @@ static void page_cache_tree_delete(struct address_space *mapping, mapping->nrpages -= nr; } -/* -* Delete a page from the page cache and free it. Caller has to make -* sure the page is locked and that nobody else uses it - or that usage -* is safe. The caller must hold the mapping's tree_lock. -*/ -void __delete_from_page_cache (struct page *page,void *shadow) +static void unaccount_page_cache_page (struct address_space *mapping, +struct page *page) { -struct address_space *mapping = page->mapping; -int nr =hpage_nr_pages (page); +int nr; -trace_mm_filemap_delete_from_page_cache (page);/* * if we're uptodate, flush out into the cleancache, otherwise * invalidate any existing cleancache entries. We can't leave
@@ -228,30 +222,46 @@ void __delete_from_page_cache(struct page *page, void *shadow) }/* hugetlb pages do not participate in page cache accounting. */ -if (!PageHuge (page)) { -__mod_node_page_state (page_pgdat (page),NR_FILE_PAGES , -nr); -if (PageSwapBacked (page)) { -__mod_node_page_state (page_pgdat (page),NR_SHMEM , -nr); -if (PageTransHuge (page)) -__dec_node_page_state (page,NR_SHMEM_THPS ); - }else { -VM_BUG_ON_PAGE (PageTransHuge (page), page); - } +if (PageHuge (page)) +return ; -/* -* At this point page must be either written or cleaned by -* truncate. Dirty page here signals a bug and loss of -* unwritten data. -* -* This fixes dirty accounting after removing the page entirely -* but leaves PageDirty set: it has no effect for truncated -* page and anyway will be cleared before returning page into -* buddy allocator. -*/ -if (WARN_ON_ONCE (PageDirty (page))) -account_page_cleaned (page, mapping, -inode_to_wb (mapping->host)); + nr =hpage_nr_pages (page); + +__mod_node_page_state (page_pgdat (page),NR_FILE_PAGES , -nr); +if (PageSwapBacked (page)) { +__mod_node_page_state (page_pgdat (page),NR_SHMEM , -nr); +if (PageTransHuge (page)) +__dec_node_page_state (page,NR_SHMEM_THPS ); + }else { +VM_BUG_ON_PAGE (PageTransHuge (page), page); } + +/* +* At this point page must be either written or cleaned by +* truncate. Dirty page here signals a bug and loss of +* unwritten data. +* +* This fixes dirty accounting after removing the page entirely +* but leaves PageDirty set: it has no effect for truncated +* page and anyway will be cleared before returning page into +* buddy allocator. +*/ +if (WARN_ON_ONCE (PageDirty (page))) +account_page_cleaned (page, mapping,inode_to_wb (mapping->host)); +} + +/* +* Delete a page from the page cache and free it. Caller has to make +* sure the page is locked and that nobody else uses it - or that usage +* is safe. The caller must hold the mapping's tree_lock. +*/ +void __delete_from_page_cache (struct page *page,void *shadow) +{ +struct address_space *mapping = page->mapping; + +trace_mm_filemap_delete_from_page_cache (page); + +unaccount_page_cache_page (mapping, page);page_cache_tree_delete (mapping, page, shadow); }
Signature · 7 lines
2.12.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerPATCH v2 7/7 mm: Batch radix tree operations when truncating pages
In reply to Jan KaraCurrently we remove pages from the radix tree one by one. To speed up page cache truncation, lock several pages at once and free them in one go. This allows us to batch radix tree operations in a more efficient way and also save round-trips on mapping->tree_lock. As a result we gain about 20% speed improvement in page cache truncation.
Data from a simple benchmark timing 10000 truncates of 1024 pages (on ext4 on ramdisk but the filesystem is barely visible in the profiles). The range shows 1% and 95% percentiles of the measured times:
4.14-rc2 4.14-rc2 + batched truncation
248-256 209-219
249-258 209-217
248-255 211-239
248-255 209-217
247-256 210-218
--- include/linux/pagemap.h | 2 ++ mm/filemap.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ mm/truncate.c | 20 ++++++++++-- 3 files changed, 104 insertions(+), 2 deletions(-)
include/linux/pagemap.h +2 -0
@@ -623,6 +623,8 @@ int add_to_page_cache_lru(struct page *page, struct address_space *mapping,extern void delete_from_page_cache (struct page *page);extern void __delete_from_page_cache (struct page *page,void *shadow);int replace_page_cache_page (struct page *old,struct page *new,gfp_t gfp_mask); +void delete_from_page_cache_batch (struct address_space *mapping,int count, +struct page **pages);/* * Like add_to_page_cache_locked, but used to add newly allocated pages:
mm/filemap.c +84 -0
90 lines not on this page · raw message
mm/truncate.c +18 -2
@@ -294,6 +294,14 @@ void truncate_inode_pages_range(struct address_space *mapping,while (index < end &&pagevec_lookup_entries (&pvec, mapping, index,min (end - index, (pgoff_t )PAGEVEC_SIZE ), indices)) { +/* +* Pagevec array has exceptional entries and we may also fail +* to lock some pages. So we store pages that can be deleted +* in an extra array. +*/ +struct page *pages[PAGEVEC_SIZE ]; +int batch_count =0 ; +for (i =0 ; i <pagevec_count (&pvec); i++) {struct page *page = pvec.pages[i];
@@ -315,9 +323,17 @@ void truncate_inode_pages_range(struct address_space *mapping,unlock_page (page);continue ; } -truncate_inode_page (mapping, page); -unlock_page (page); +if (page->mapping != mapping) { +unlock_page (page); +continue ; + } + pages[batch_count++] = page; } +for (i =0 ; i < batch_count; i++) +truncate_cleanup_page (mapping, pages[i]); +delete_from_page_cache_batch (mapping, batch_count, pages); +for (i =0 ; i < batch_count; i++) +unlock_page (pages[i]);pagevec_remove_exceptionals (&pvec);pagevec_release (&pvec);cond_resched ();
Signature · 7 lines
2.12.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>