Skip to contentSkip to conversation

mm: prototype: rid swapoff of quadratic complexity

RFC Superseded Kelley Nielsen 2 participants 2 messages Nov 12, 2014 – Dec 1, 2014

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

RFC v6 1/2 mm: prototype: rid swapoff of quadratic complexity

Changes since v5:
* Make multiple orphan checking passes at the end of try_to_unuse
* Remove pages from swapcache if we hold the only reference in
* unuse_pte_range

Quoted 1 line

8----------------------------------------------------------8<

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

Time took by swapoff on a swap partition containing about 240M of data, with about 1.1G free memory and about 520M swap available. Swap partition was on a laptop with a hard disk drive (not SSD).

Present implementation....about 13.8s
Prototype.................about 5.5s

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.

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 then pass this page to unuse_pte.

TODO

* Handle count of unused pages for frontswap.

---
 include/linux/shmem_fs.h |   2 +-
 mm/shmem.c               | 190 +++++++++++------------
 mm/swapfile.c            | 394 +++++++++++++++++------------------------------
 3 files changed, 230 insertions(+), 356 deletions(-)
include/linux/shmem_fs.h +1 -1
index 50777b5..f0cd6c9 100644
@@ -58,7 +58,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);
 
 static inline struct page *shmem_read_mapping_page(
 				struct address_space *mapping, pgoff_t index)
mm/shmem.c +87 -103

233 lines not on this page · raw message

mm/swapfile.c +142 -252

497 lines not on this page · raw message

Signature · 7 lines
1.8.3.2

--
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>
maintainer
#1 lore raw
Quoted 4 lines · Kelley Nielsen

Changes since v5:
* Make multiple orphan checking passes at the end of try_to_unuse
* Remove pages from swapcache if we hold the only reference in
* unuse_pte_range

I'm very glad you're back on this, Kelley, thank you.

I gave it a try this weekend, and at first I was very pleased with the results - up until make died with an unexplained SIGSEGV, but that was only after several hours (an unexplained SIGSEGV usually means a swap error of some kind in my testing).

At the time I put that down to bringing KSM in, and I wasn't expecting KSM to be right yet anyway. But after looking at the code, I was a little disappointed to find the MAX_RETRIES 3 stuff: and there is a particular bug there which was probably responsible for my SIGSEGV - when try_to_unuse() has done MAX_RETRIES and gives up, it returns 0 pretending success: no, please make that, say, -EBUSY.

When I made that change, and at the same time switched from testing swap on SSD to swap on HDD, it didn't go so well: the eighth swapoff (under concurrent swapping load) then admitted that it had failed.

Even -EBUSY is not really right there: I do like your improvements, and want to get them in, but it is a regression to fail under load like that. And I imagine that the bigger the swap area, or the slower the disk, or the heavier the load, then the more likely a failure is in that case.

It's good that you're now removing from swapcache when you can in unuse_pte_range(), but that's just not enough: I'm pretty sure it does need the rmap walk that I suggested back in April (and I'm hopeful that with that, we could then get rid of those retries - hopeful but far from certain). So that a page can be taken out of swap in one go, with no danger of being repeatedly refaulted in behind you.

I'm not sure where you want to take it from here. I'd be happy for this to go into mmotm (with the changes Rik proposed for 2/2, and the -EBUSY at MAX_RETRIES), so that it gets wider exposure (few people will in practice be disadvantaged by that -EBUSY):
so long as one of us commits to fixing up the regression before it goes to Linus.

I wonder what your time situation is: I wonder if you just haven't found time to add the rmap walk, or are not confident in doing so.

To be honest, I'd rather like to do that bit myself: apart from anything else, it is the only way in which I will properly get to grips with your changes - I'm a coder not a reviewer. But there is no chance whatever that I'll get to do it before Christmas, and whether I find time to do it then rather depends on what else comes up that may demand higher priority.

And, I don't mean to pressure you with more than you have time for, but if you do have time, please remember to remove the CONFIG_SHMEM CONFIG_SWAP stuff from lib/radix-tree.c and radix-tree.h (Cc Johannes Weiner <[email protected]> and Konstantin Khlebnikov <[email protected]>, they will give a cheer), and rename all those "unuse"s to "swapoff"s (Cc Oleg Nesterov <[email protected]> and Pavel Machek <[email protected]>) to be clearer, and to avoid confusion with the other unuse_mm().

Of course, anyone can do those cleanups, if you don't have the time:
but you have earned the right, so I'd rather see them in your name.

By the way, I'll Cc you in a few moments on a swapoff fix I tracked down last week: nothing to do with your changes or my testing of them, but something I found in testing an unrelated patchset.

Hugh

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