Skip to contentSkip to conversation

swap: add a simple random read swapin detection

RFC Superseded Shaohua Li 3 participants 3 messages Aug 22, 2012 – Aug 23, 2012

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Shaohua Li

author
#0 lore raw

RFC 1/1 swap: add a simple random read swapin detection

The swapin readahead does a blind readahead regardless if the swapin is sequential. This is ok for harddisk and random read, because read big size has no penality in harddisk, and if the readahead pages are garbage, they can be reclaimed fastly. But for SSD, big size read is more expensive than small size read. If readahead pages are garbage, such readahead only has overhead.

This patch addes a simple random read detection like what file mmap readahead does. If random read is detected, swapin readahead will be skipped. This improves a lot for a swap workload with random IO in a fast SSD.

---
 include/linux/mm_types.h |    1 +
 mm/memory.c              |    3 ++-
 mm/swap_state.c          |    9 +++++++++
 3 files changed, 12 insertions(+), 1 deletion(-)
mm/swap_state.c +9 -0
Index: linux/mm/swap_state.c
===================================================================
@@ -351,6 +351,7 @@ struct page *read_swap_cache_async(swp_e
 	return found_page;
 }
 
+#define SWAPRA_MISS  (100)
 /**
  * swapin_readahead - swap in pages in hope we need them soon
  * @entry: swap entry of this memory
@@ -379,6 +380,13 @@ struct page *swapin_readahead(swp_entry_
 	unsigned long mask = (1UL << page_cluster) - 1;
 	struct blk_plug plug;
 
+	if (vma) {
+		if (atomic_read(&vma->swapra_miss) < SWAPRA_MISS * 10)
+			atomic_inc(&vma->swapra_miss);
+		if (atomic_read(&vma->swapra_miss) > SWAPRA_MISS)
+			goto skip;
+	}
+
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;
 	end_offset = offset | mask;
@@ -397,5 +405,6 @@ struct page *swapin_readahead(swp_entry_
 	blk_finish_plug(&plug);
 
 	lru_add_drain();	/* Push any new pages onto the LRU now */
+skip:
 	return read_swap_cache_async(entry, gfp_mask, vma, addr);
 }
include/linux/mm_types.h +1 -0
Index: linux/include/linux/mm_types.h
===================================================================
@@ -279,6 +279,7 @@ struct vm_area_struct {
 #ifdef CONFIG_NUMA
 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
 #endif
+	atomic_t swapra_miss;
 };
 
 struct core_thread {
mm/memory.c +2 -1
Index: linux/mm/memory.c
===================================================================
@@ -2953,7 +2953,8 @@ static int do_swap_page(struct mm_struct
 		ret = VM_FAULT_HWPOISON;
 		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
 		goto out_release;
-	}
+	} else if (!(flags & FAULT_FLAG_TRIED))
+		atomic_dec_if_positive(&vma->swapra_miss);
 
 	locked = lock_page_or_retry(page, mm, flags);
 

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

#1 lore raw
Quoted 18 lines · Shaohua Li
mm/swap_state.c +7 -0
+#define SWAPRA_MISS  (100)
  /**
   * swapin_readahead - swap in pages in hope we need them soon
   * @entry: swap entry of this memory
@@ -379,6 +380,13 @@ struct page *swapin_readahead(swp_entry_
  	unsigned long mask = (1UL << page_cluster) - 1;
  	struct blk_plug plug;
 
+	if (vma) {
+		if (atomic_read(&vma->swapra_miss) < SWAPRA_MISS * 10)
+			atomic_inc(&vma->swapra_miss);
+		if (atomic_read(&vma->swapra_miss) > SWAPRA_MISS)
+			goto skip;
+	}
 

mm/memory.c static int do_swap_page(struct mm_struct · line 2957

Quoted 6 lines
mm/memory.c +2 -1
@@ -2953,7 +2953,8 @@ static int do_swap_page(struct mm_struct
  		ret = VM_FAULT_HWPOISON;
  		delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  		goto out_release;
-	}
+	} else if (!(flags & FAULT_FLAG_TRIED))
+		atomic_dec_if_positive(&vma->swapra_miss);

The approach makes sense when viewed together with the changelog, but I fear it will be non-obvious to anyone who just looks at the code later in time.

Please hide these increments and decrements behind some simple accessor functions, eg:

swap_cache_hit()
swap_cache_miss()
swap_cache_skip_readahead()

These small functions can then be placed together (maybe in swap.c?) and get a good comment documenting exactly what they are supposed to do.

As an aside, how well do these patches work?

What kind of performance changes have you seen, both on SSDs and hard disks?

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

#2 lore raw
In reply to Shaohua Li

Hi Shaohua,

Quoted 55 lines · Shaohua Li

The swapin readahead does a blind readahead regardless if the swapin is sequential. This is ok for harddisk and random read, because read big size has no penality in harddisk, and if the readahead pages are garbage, they can be reclaimed fastly. But for SSD, big size read is more expensive than small size read. If readahead pages are garbage, such readahead only has overhead.

This patch addes a simple random read detection like what file mmap readahead does. If random read is detected, swapin readahead will be skipped. This improves a lot for a swap workload with random IO in a fast SSD.

---
 include/linux/mm_types.h |    1 +
 mm/memory.c              |    3 ++-
 mm/swap_state.c          |    9 +++++++++
 3 files changed, 12 insertions(+), 1 deletion(-)

Index: linux/mm/swap_state.c
===================================================================

mm/swap_state.c +9 -0
@@ -351,6 +351,7 @@ struct page *read_swap_cache_async(swp_e
 	return found_page;
 }
 
+#define SWAPRA_MISS  (100)
 /**
  * swapin_readahead - swap in pages in hope we need them soon
  * @entry: swap entry of this memory
@@ -379,6 +380,13 @@ struct page *swapin_readahead(swp_entry_
 	unsigned long mask = (1UL << page_cluster) - 1;
 	struct blk_plug plug;
 
+	if (vma) {
+		if (atomic_read(&vma->swapra_miss) < SWAPRA_MISS * 10)
+			atomic_inc(&vma->swapra_miss);
+		if (atomic_read(&vma->swapra_miss) > SWAPRA_MISS)
+			goto skip;
+	}
+
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;
 	end_offset = offset | mask;
@@ -397,5 +405,6 @@ struct page *swapin_readahead(swp_entry_
 	blk_finish_plug(&plug);
 
 	lru_add_drain();	/* Push any new pages onto the LRU now */
+skip:
 	return read_swap_cache_async(entry, gfp_mask, vma, addr);
 }

Index: linux/include/linux/mm_types.h
===================================================================

include/linux/mm_types.h struct vm_area_struct { · line 282

Quoted 4 lines
include/linux/mm_types.h +1 -0
@@ -279,6 +279,7 @@ struct vm_area_struct {
 #ifdef CONFIG_NUMA
 	struct mempolicy *vm_policy;	/* NUMA policy for the VMA */
 #endif
+	atomic_t swapra_miss;
#ifdef CONFIG_SWAP
	atomic_t swapra_miss;

#endif

Many embedded devices don't have swap.

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