mm/swap: add cache for swap slots allocation
Subsystems (2)
[email protected]
maintainerPATCH 118/142 mm/swap: add cache for swap slots allocation
From: Tim Chen <[email protected]>
Subject: mm/swap: add cache for swap slots allocation
We add per cpu caches for swap slots that can be allocated and freed quickly without the need to touch the swap info lock.
Two separate caches are maintained for swap slots allocated and swap slots returned. This is to allow the swap slots to be returned to the global pool in a batch so they will have a chance to be coaelesced with other slots in a cluster. We do not reuse the slots that are returned right away, as it may increase fragmentation of the slots.
The swap allocation cache is protected by a mutex as we may sleep when searching for empty slots in cache. The swap free cache is protected by a spin lock as we cannot sleep in the free path.
We refill the swap slots cache when we run out of slots, and we disable the swap slots cache and drain the slots if the global number of slots fall below a low watermark threshold. We re-enable the cache agian when the slots available are above a high watermark.
---
include/linux/swap.h | 4 include/linux/swap_slots.h | 28 ++ mm/Makefile | 2 mm/swap_slots.c | 342 +++++++++++++++++++++++++++++++++++ mm/swap_state.c | 1 mm/swapfile.c | 26 +- 6 files changed, 391 insertions(+), 12 deletions(-)
include/linux/swap.h renamed from include/linux/swap.h~mm-swap-add-cache-for-swap-slots-allocation +4 -0
@@ -372,6 +372,7 @@ extern struct page *swapin_readahead(swp/* linux/mm/swapfile.c */ extern atomic_long_t nr_swap_pages;extern long total_swap_pages; +extern bool has_usable_swap (void );/* Swap 50% full? Release swapcache more aggressively.. */ static inline bool vm_swap_full (void )
@@ -410,6 +411,9 @@ struct backing_dev_info;extern int init_swap_address_space (unsigned int type,unsigned long nr_pages);extern void exit_swap_address_space (unsigned int type); +extern int get_swap_slots (int n,swp_entry_t *slots); +extern void swapcache_free_batch (swp_entry_t *entries,int n); +#else /* CONFIG_SWAP */ #define swap_address_space (entry) (NULL)
include/linux/swap_slots.h new file +28 -0
@@ -0,0 +1,28 @@ +#ifndef _LINUX_SWAP_SLOTS_H +#define _LINUX_SWAP_SLOTS_H + +#include <linux/swap.h> +#include <linux/spinlock.h> +#include <linux/mutex.h> + +#define SWAP_SLOTS_CACHE_SIZE SWAP_BATCH +#define THRESHOLD_ACTIVATE_SWAP_SLOTS_CACHE (5*SWAP_SLOTS_CACHE_SIZE) +#define THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE (2*SWAP_SLOTS_CACHE_SIZE) + +struct swap_slots_cache { +bool lock_initialized; +struct mutex alloc_lock;/* protects slots, nr, cur */ +swp_entry_t *slots; +int nr; +int cur; +spinlock_t free_lock;/* protects slots_ret, n_ret */ +swp_entry_t *slots_ret; +int n_ret; +}; + +void disable_swap_slots_cache_lock (void ); +void reenable_swap_slots_cache_unlock (void ); +int enable_swap_slots_cache (void ); +int free_swap_slot (swp_entry_t entry); + +#endif /* _LINUX_SWAP_SLOTS_H */
mm/Makefile renamed from mm/Makefile~mm-swap-add-cache-for-swap-slots-allocation +1 -1
@@ -35,7 +35,7 @@ obj-y := filemap.o mempool.o oom_kill. readahead.o swap.o truncate.o vmscan.o shmem.o \ util.o mmzone.o vmstat.o backing-dev.o \ mm_init.o mmu_context.o percpu.o slab_common.o \ - compaction.o vmacache.o \ + compaction.o vmacache.o swap_slots.o \ interval_tree.o list_lru.o workingset.o \ debug.o $(mmu-y)
mm/swap_slots.c new file +342 -0
342 lines not on this page · raw message
mm/swap_state.c renamed from mm/swap_state.c~mm-swap-add-cache-for-swap-slots-allocation +1 -0
@@ -18,6 +18,7 @@#include <linux/pagevec.h> #include <linux/migrate.h> #include <linux/vmalloc.h> +#include <linux/swap_slots.h> #include <asm/pgtable.h>
mm/swapfile.c renamed from mm/swapfile.c~mm-swap-add-cache-for-swap-slots-allocation +15 -11
@@ -34,6 +34,7 @@#include <linux/frontswap.h> #include <linux/swapfile.h> #include <linux/export.h> +#include <linux/swap_slots.h> #include <asm/pgtable.h> #include <asm/tlbflush.h>
@@ -854,14 +855,6 @@ noswap:return n_ret; } -swp_entry_t get_swap_page (void ) -{ -swp_entry_t entry; - -get_swap_pages (1 , &entry); -return entry; -} -/* The only caller of this function is now suspend routine */ swp_entry_t get_swap_page_of_type (int type) {
@@ -1052,7 +1045,7 @@ void swap_free(swp_entry_t entry) p =_swap_info_get (entry);if (p) {if (!__swap_entry_free (p, entry,1 )) -swapcache_free_entries (&entry,1 ); +free_swap_slot (entry); } }
@@ -1066,7 +1059,7 @@ void swapcache_free(swp_entry_t entry) p =_swap_info_get (entry);if (p) {if (!__swap_entry_free (p, entry,SWAP_HAS_CACHE )) -swapcache_free_entries (&entry,1 ); +free_swap_slot (entry); } }
@@ -1288,7 +1281,7 @@ int free_swap_and_cache(swp_entry_t entr page =NULL ; } }else if (!count) -swapcache_free_entries (&entry,1 ); +free_swap_slot (entry); }if (page) {/*
@@ -2116,6 +2109,17 @@ static void reinsert_swap_info(struct swspin_unlock (&swap_lock); } +bool has_usable_swap (void ) +{ +bool ret = true; + +spin_lock (&swap_lock); +if (plist_head_empty (&swap_active_head)) + ret = false; +spin_unlock (&swap_lock); +return ret; +} + SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) {struct swap_info_struct *p =NULL ;
_