vm: swap prefetch-1
Subsystems (6)
PATCH 1/1 vm: swap prefetch-1
Thanks to those who tested the swap prefetch code in its first public iteration. Here is a respin of the code which improves its behaviour to be virtually unnoticable, and fix a couple of bugs that showed up.
Changes:
This version is suspend aware
Swap prefetches are limited to SWAP_CLUSTER_MAX (=128kb) per second Prefetching is delayed whenever memory watermark limits are missed, or swap is going in or out. The delay has been reduced to 5 seconds though. Memory pressure while in the actual prefetch code will break out of it The dependence on an new timer interface was abolished
Other cleanups and tweaks
Please test and report back. Assuming no bugs show up in this version, it will be included in the next -ck.
The latest patch is available for download from here:
http://ck.kolivas.org/patches/swap-prefetch/
and is attached to this email
Cheers,
Con
This patch implements swap prefetching when the vm is in an idle state.
Add a list of pages that are added to swap. Then use that list when the vm is idle, as determined by not failing any watermark tests or swapping in/out pages, to prefetch pages in the most recently used order back into swap cache with a low priority kernel thread.
Leaving the pages on swap and in swap cache means that if any vm stress is encountered without using those prefetched pages, the swap cache entries can be dropped and they are already in swap without needing to be rewritten out.
include/linux/fs.h | 2 include/linux/swap.h | 27 +++++ init/Kconfig | 11 ++ mm/Makefile | 1 mm/page_alloc.c | 10 + mm/swap.c | 3 mm/swap_prefetch.c | 260 +++++++++++++++++++++++++++++++++++++++++++++++++++ mm/swap_state.c | 12 ++ mm/vmscan.c | 1 9 files changed, 324 insertions(+), 3 deletions(-)
include/linux/fs.h +2 -0
@@ -340,6 +340,8 @@ struct address_space {struct inode *host;/* owner: inode, block_device */ struct radix_tree_root page_tree;/* radix tree of all pages */ rwlock_t tree_lock;/* and rwlock protecting it */ +struct radix_tree_root swap_tree;/* radix tree of swapped pages */ +struct list_head swapped_pages;/* list of swapped pages */ unsigned int i_mmap_writable;/* count VM_SHARED mappings */ struct prio_tree_root i_mmap;/* tree of private and shared mappings */ struct list_head i_mmap_nonlinear;/*list VM_NONLINEAR mappings */
include/linux/swap.h +27 -0
@@ -206,6 +206,7 @@ extern void free_pages_and_swap_cache(stextern struct page *lookup_swap_cache (swp_entry_t );extern struct page *read_swap_cache_async (swp_entry_t ,struct vm_area_struct *vma,unsigned long addr); +extern int add_to_swap_cache (struct page *page,swp_entry_t entry);/* linux/mm/swapfile.c */ extern long total_swap_pages;extern unsigned int nr_swapfiles;
@@ -247,6 +248,32 @@ static inline void put_swap_token(struct __put_swap_token(mm); } +#ifdef CONFIG_SWAP_PREFETCH +/* mm/swap_prefetch.c */ +extern void prepare_prefetch (void ); +extern void add_to_swapped_list (unsigned long index); +extern void remove_from_swapped_list (unsigned long index); +extern void delay_prefetch (void ); + +#else /* CONFIG_SWAP_PREFETCH */ +static inline void add_to_swapped_list (unsigned long index) +{ +} + +static inline void prepare_prefetch (void ) +{ +} + +static inline void remove_from_swapped_list (unsigned long index) +{ +} + +static inline void delay_prefetch (void ) +{ +} + +#endif /* CONFIG_SWAP_PREFETCH */ +#else /* CONFIG_SWAP */ #define total_swap_pages 0
init/Kconfig +11 -0
@@ -87,6 +87,17 @@ config SWAP used to provide more virtual memory than the actual RAM present in your computer. If unsure say Y. +config SWAP_PREFETCH + bool "Support for prefetching swapped memory (EXPERIMENTAL)" + depends on SWAP && EXPERIMENTAL + default n + ---help--- + This option will allow the kernel to prefetch swapped memory pages + when idle. The pages will be kept on both swap and in swap_cache + thus avoiding the need for further I/O if either ram or swap space + is required. This is desirable on workstations. + Desktop users will most likely want to say Y. + config SYSVIPC bool "System V IPC" depends on MMU
mm/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-y := bootmem.o filemap.o mempool.o prio_tree.o $(mmu-y) obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o thrash.o +obj-$(CONFIG_SWAP_PREFETCH) += swap_prefetch.o obj-$(CONFIG_HUGETLBFS) += hugetlb.o obj-$(CONFIG_NUMA) += mempolicy.o obj-$(CONFIG_SPARSEMEM) += sparse.o
mm/page_alloc.c +8 -2
@@ -745,7 +745,7 @@ int zone_watermark_ok(struct zone *z, in min -= min /4 ;if (free_pages <= min + z->lowmem_reserve[classzone_idx]) -return 0 ; + goto out_failed;for (o =0 ; o < order; o++) {/* At the next order, this order's pages become unavailable */ free_pages -= z->free_area[o].nr_free << o;
@@ -754,9 +754,15 @@ int zone_watermark_ok(struct zone *z, in min >>=1 ;if (free_pages <= min) -return 0 ; + goto out_failed; } +return 1 ; +out_failed: +/* Swap prefetching is delayed if any watermark is low */ +delay_prefetch (); + +return 0 ; } static inline int
mm/swap.c +3 -0
@@ -481,5 +481,8 @@ void __init swap_setup(void)* Right now other parts of the system means that we * _really_ don't want to cluster much more */ + +prepare_prefetch (); +hotcpu_notifier (cpu_swap_callback,0 ); }
mm/swap_prefetch.c +260 -0
260 lines not on this page · raw message
mm/swap_state.c +11 -1
@@ -36,6 +36,8 @@ static struct backing_dev_info swap_backstruct address_space swapper_space = { .page_tree =RADIX_TREE_INIT (GFP_ATOMIC |__GFP_NOWARN), .tree_lock =RW_LOCK_UNLOCKED , + .swap_tree =RADIX_TREE_INIT (GFP_ATOMIC ), + .swapped_pages =LIST_HEAD_INIT (swapper_space.swapped_pages), .a_ops = &swap_aops, .i_mmap_nonlinear =LIST_HEAD_INIT (swapper_space.i_mmap_nonlinear), .backing_dev_info = &swap_backing_dev_info,
@@ -80,6 +82,7 @@ static int __add_to_swap_cache(struct pa error =radix_tree_insert (&swapper_space.page_tree, entry.val, page);if (!error) { +remove_from_swapped_list (entry.val);page_cache_get (page);SetPageLocked (page);SetPageSwapCache (page);
@@ -93,11 +96,12 @@ static int __add_to_swap_cache(struct pareturn error; } -static int add_to_swap_cache (struct page *page,swp_entry_t entry) +int add_to_swap_cache (struct page *page,swp_entry_t entry) {int error;if (!swap_duplicate (entry)) { +remove_from_swapped_list (entry.val);INC_CACHE_INFO (noent_race);return -ENOENT ; }
@@ -148,6 +152,9 @@ int add_to_swap(struct page * page)if (!PageLocked (page))BUG (); +/* Swap prefetching is delayed if we're swapping pages */ +delay_prefetch (); +for (;;) { entry =get_swap_page ();if (!entry.val)
@@ -325,6 +332,9 @@ struct page *read_swap_cache_async(swp_estruct page *found_page, *new_page =NULL ;int err; +/* Swap prefetching is delayed if we're already reading from swap */ +delay_prefetch (); + do { /* * First check the swap cache. Since this is normally
mm/vmscan.c +1 -0
@@ -519,6 +519,7 @@ static int shrink_list(struct list_head#ifdef CONFIG_SWAP if (PageSwapCache (page)) {swp_entry_t swap = { .val = page->private }; +add_to_swapped_list (swap.val);__delete_from_swap_cache (page);write_unlock_irq (&mapping->tree_lock);swap_free (swap);