mm: prototype: rid swapoff of quadratic complexity
Subsystems (3)
PATCH v2 1/1 mm: prototype: rid swapoff of quadratic complexity
This patch was initially posted by Kelley([email protected]). Reposting the patch with all review comments addressed and with minor modifications and optimizations. Tests were rerun and commit message updated with new results.
The function try_to_unuse() is of quadratic complexity, with a lot of wasted effort. It unuses swap entries one by one, potentially iterating over all the page tables for all the processes in the system for each one.
This new proposed implementation of try_to_unuse simplifies its complexity to linear. It iterates over the system's mms once, unusing all the affected entries as it walks each set of page tables. It also makes similar changes to shmem_unuse.
Improvement
swapoff was called on a swap partition containing about 6G of data, in a VM(8cpu, 16G RAM), and calls to unuse_pte_range() were counted.
Present implementation....about 1200M calls(8min, avg 80% cpu util). Prototype.................about 8.0K calls(3min, avg 5% cpu util).
Details
In shmem_unuse(), iterate over the shmem_swaplist and, for each shmem_inode_info that contains a swap entry, pass it to shmem_unuse_inode(), along with the swap type. In shmem_unuse_inode(), iterate over its associated radix tree, and store the index of each exceptional entry in an array for passing to shmem_getpage_gfp() outside of the RCU critical section.
In try_to_unuse(), instead of iterating over the entries in the type and unusing them one by one, perhaps walking all the page tables for all the processes for each one, iterate over the mmlist, making one pass. Pass each mm to unuse_mm() to begin its page table walk, and during the walk, unuse all the ptes that have backing store in the swap type received by try_to_unuse(). After the walk, check the type for orphaned swap entries with find_next_to_unuse(), and remove them from the swap cache. If find_next_to_unuse() starts over at the beginning of the type, repeat the check of the shmem_swaplist and the walk a maximum of three times.
Change unuse_mm() and the intervening walk functions down to unuse_pte_range() to take the type as a parameter, and to iterate over their entire range, calling the next function down on every iteration. In unuse_pte_range(), make a swap entry from each pte in the range using the passed in type. If it has backing store in the type, call swapin_readahead() to retrieve the page and pass it to unuse_pte().
Pass the count of pages_to_unuse down the page table walks in try_to_unuse(), and return from the walk when the desired number of pages has been swapped back in.
Changes in v2
- Updated patch to use Xarray instead of radix tree
--- include/linux/shmem_fs.h | 2 +- mm/shmem.c | 208 ++++++++------------ mm/swapfile.c | 413 +++++++++++++++------------------------ 3 files changed, 242 insertions(+), 381 deletions(-)
include/linux/shmem_fs.h +1 -1
@@ -72,7 +72,7 @@ extern void shmem_unlock_mapping(struct address_space *mapping);extern struct page *shmem_read_mapping_page_gfp (struct address_space *mapping,pgoff_t index,gfp_t gfp_mask);extern void shmem_truncate_range (struct inode *inode,loff_t start,loff_t end); -extern int shmem_unuse (swp_entry_t entry,struct page *page); +extern int shmem_unuse (unsigned int type);extern unsigned long shmem_swap_usage (struct vm_area_struct *vma); extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
mm/shmem.c +83 -125
249 lines not on this page · raw message
mm/swapfile.c +158 -255
533 lines not on this page · raw message
Signature · 1 line
2.17.1
mm/shmem.c static void shmem_evict_inode(struct inode *inode) · line 1106
Quoted 10 lines · Vineeth Remanan Pillai
mm/shmem.c +9
-0+ do { + XA_STATE(xas, &mapping->i_pages, start); + int i; + int entries = 0; + struct page *page; + pgoff_t indices[PAGEVEC_SIZE]; + unsigned long end = start + PAGEVEC_SIZE; + rcu_read_lock(); + xas_for_each(&xas, page, end) {
I think this is a mistake. You should probably specify ULONG_MAX for the end. Otherwise if there are no swap entries in the first 60kB of the file, you'll just exit. That does mean you'll need to check 'entries' for hitting PAGEVEC_SIZE.
Quoted 28 lines
mm/shmem.c +25
-0+if (xas_retry (&xas, page)) +continue ; +if (!xa_is_value (page)) +continue ; + indices[entries++] = xas.xa_index; + +if (need_resched ()) { +xas_pause (&xas); +cond_resched_rcu (); + } } +rcu_read_unlock (); + +if (entries ==0 ) +break ; + +for (i =0 ; i < entries; i++) { +int err =0 ; + + err =shmem_getpage (inode, indices[i], + &page,SGP_CACHE ); +if (err ==0 ) { +unlock_page (page); +put_page (page); + } +if (err == -ENOMEM )
mm/shmem.c static void shmem_evict_inode(struct inode *inode) · line 1144
Quoted 10 lines
mm/shmem.c +8
-0+ goto out; + else + error = err; + } + start = xas.xa_index; + }while (true); + +out:return error; }
This seems terribly complicated. You run through i_pages, record the indices of the swap entries, then go back and look them up again by calling shmem_getpage() which calls the incredibly complex 300 line shmem_getpage_gfp().
Can we refactor shmem_getpage_gfp() to skip some of the checks which aren't necessary when called from this path, and turn this into a nice simple xas_for_each() loop which works one entry at a time?
Quoted 36 lines
/*
- * Search through swapped inodes to find and replace swap by page.
+ * Read all the shared memory data that resides in the swap
+ * device 'type' back into memory, so the swap device can be
+ * unused.
*/mm/shmem.c +12
-12-int shmem_unuse (swp_entry_t swap,struct page *page) +int shmem_unuse (unsigned int type) { -struct list_head *this, *next;struct shmem_inode_info *info; -struct mem_cgroup *memcg; +struct inode *inode; +struct inode *prev_inode =NULL ; +struct list_head *p; +struct list_head *next;int error =0 ; -/* -* There's a faint possibility that swap page was replaced before -* caller locked it: caller will come back later with the right page. -*/ -if (unlikely (!PageSwapCache (page) ||page_private (page) != swap.val)) - goto out; +if (list_empty (&shmem_swaplist)) +return 0 ; + +mutex_lock (&shmem_swaplist_mutex); + p = &shmem_swaplist;/* -* Charge page using GFP_KERNEL while we can wait, before taking -* the shmem_swaplist_mutex which might hold up shmem_writepage(). -* Charged back to the user (not to caller) when swap account is used. +* The extra refcount on the inode is necessary to safely dereference +* p->next after re-acquiring the lock. New shmem inodes with swap
mm/shmem.c static void shmem_evict_inode(struct inode *inode) · line 1172
Quoted 10 lines
mm/shmem.c +3
-6+* get added to the end of the list and we will scan them all. */ - error =mem_cgroup_try_charge_delay (page, current->mm,GFP_KERNEL , - &memcg, false); -if (error) - goto out; -/* No memory allocation: swap entry occupies the slot for the page */ - error = -EAGAIN ; +list_for_each_safe (p, next, &shmem_swaplist) { + info = list_entry(p, struct shmem_inode_info, swaplist);
This could use list_for_each_entry_safe(), right?
Hi Mathew,
Thanks for your response!
Quoted 9 lines · Matthew Wilcox
Quoted 8 lines · Vineeth Remanan Pillai
mm/shmem.c +7
-0+ do { + XA_STATE(xas, &mapping->i_pages, start); + int i; + int entries = 0; + struct page *page; + pgoff_t indices[PAGEVEC_SIZE]; + unsigned long end = start + PAGEVEC_SIZE;
Quoted 6 lines
mm/shmem.c static void shmem_evict_inode(struct inode *inode) · line 1106
Quoted 2 lines
mm/shmem.c +2
-0+rcu_read_lock (); +xas_for_each (&xas, page, end) {I think this is a mistake. You should probably specify
ULONG_MAXfor the end. Otherwise if there are no swap entries in the first 60kB of the file, you'll just exit. That does mean you'll need to check 'entries' for hittingPAGEVEC_SIZE.
Thanks for pointing this out. I shall fix this in the next version.
Quoted 8 lines
This seems terribly complicated. You run through
i_pages, record the indices of the swap entries, then go back and look them up again by callingshmem_getpage()which calls the incredibly complex 300 lineshmem_getpage_gfp().Can we refactor
shmem_getpage_gfp()to skip some of the checks which aren't necessary when called from this path, and turn this into a nice simplexas_for_each()loop which works one entry at a time?
I shall investigate this and make this simpler as you suggested.
Quoted 3 lines
mm/shmem.c static void shmem_evict_inode(struct inode *inode) · line 1172
Quoted 2 lines
mm/shmem.c +2
-0+list_for_each_safe (p, next, &shmem_swaplist) { + info = list_entry(p, struct shmem_inode_info, swaplist);This could use
list_for_each_entry_safe(), right?
Yes, you are right. Will fix.
Thanks,
Vineeth
Quoted 1 line
Hi Matthew,
Quoted 10 lines
Quoted 8 lines
This seems terribly complicated. You run through
i_pages, record the indices of the swap entries, then go back and look them up again by callingshmem_getpage()which calls the incredibly complex 300 lineshmem_getpage_gfp().Can we refactor
shmem_getpage_gfp()to skip some of the checks which aren't necessary when called from this path, and turn this into a nice simplexas_for_each()loop which works one entry at a time?I shall investigate this and make this simpler as you suggested.
I have looked into this deeper. I think it would be very difficult to consolidate the whole logic into a single xas_for_each() loop because, we do disk io and might sleep. I have refactored the code such that it much more readable now and I am using the same format used by find_get_entries.
Will send out the next revision later today.
Thanks,
Vineeth