<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
From: Hugh Dickins &lt;hugh@veritas.com&gt;

There are several comments that swap's extent_list.prev points to the lowest
extent: that's not so, it's extent_list.next which points to it, as you'd
expect.  And a couple of loops in add_swap_extent which go all the way through
the list, when they should just add to the other end.

Fix those up, and let map_swap_page search the list forwards: profiles shows
it to be twice as quick that way - because prefetch works better on how the
structs are typically kmalloc'ed?  or because usually more is written to than
read from swap, and swap is allocated ascendingly?

Signed-off-by: Hugh Dickins &lt;hugh@veritas.com&gt;
Signed-off-by: Andrew Morton &lt;akpm@osdl.org&gt;
---

 include/linux/swap.h |    2 --
 mm/swapfile.c        |   27 +++++++++------------------
 2 files changed, 9 insertions(+), 20 deletions(-)

diff -puN include/linux/swap.h~swap-swap-extent-list-is-ordered include/linux/swap.h
--- devel/include/linux/swap.h~swap-swap-extent-list-is-ordered	2005-08-06 15:34:27.000000000 -0700
+++ devel-akpm/include/linux/swap.h	2005-08-06 15:34:27.000000000 -0700
@@ -116,8 +116,6 @@ enum {
 
 /*
  * The in-memory structure used to track swap areas.
- * extent_list.prev points at the lowest-index extent.  That list is
- * sorted.
  */
 struct swap_info_struct {
 	unsigned int flags;
diff -puN mm/swapfile.c~swap-swap-extent-list-is-ordered mm/swapfile.c
--- devel/mm/swapfile.c~swap-swap-extent-list-is-ordered	2005-08-06 15:34:27.000000000 -0700
+++ devel-akpm/mm/swapfile.c	2005-08-06 15:34:27.000000000 -0700
@@ -832,9 +832,9 @@ sector_t map_swap_page(struct swap_info_
 				offset &lt; (se-&gt;start_page + se-&gt;nr_pages)) {
 			return se-&gt;start_block + (offset - se-&gt;start_page);
 		}
-		lh = se-&gt;list.prev;
+		lh = se-&gt;list.next;
 		if (lh == &amp;sis-&gt;extent_list)
-			lh = lh-&gt;prev;
+			lh = lh-&gt;next;
 		se = list_entry(lh, struct swap_extent, list);
 		sis-&gt;curr_swap_extent = se;
 		BUG_ON(se == start_se);		/* It *must* be present */
@@ -859,10 +859,9 @@ static void destroy_swap_extents(struct 
 
 /*
  * Add a block range (and the corresponding page range) into this swapdev's
- * extent list.  The extent list is kept sorted in block order.
+ * extent list.  The extent list is kept sorted in page order.
  *
- * This function rather assumes that it is called in ascending sector_t order.
- * It doesn't look for extent coalescing opportunities.
+ * This function rather assumes that it is called in ascending page order.
  */
 static int
 add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
@@ -872,16 +871,15 @@ add_swap_extent(struct swap_info_struct 
 	struct swap_extent *new_se;
 	struct list_head *lh;
 
-	lh = sis-&gt;extent_list.next;	/* The highest-addressed block */
-	while (lh != &amp;sis-&gt;extent_list) {
+	lh = sis-&gt;extent_list.prev;	/* The highest page extent */
+	if (lh != &amp;sis-&gt;extent_list) {
 		se = list_entry(lh, struct swap_extent, list);
-		if (se-&gt;start_block + se-&gt;nr_pages == start_block &amp;&amp;
-		    se-&gt;start_page  + se-&gt;nr_pages == start_page) {
+		BUG_ON(se-&gt;start_page + se-&gt;nr_pages != start_page);
+		if (se-&gt;start_block + se-&gt;nr_pages == start_block) {
 			/* Merge it */
 			se-&gt;nr_pages += nr_pages;
 			return 0;
 		}
-		lh = lh-&gt;next;
 	}
 
 	/*
@@ -894,14 +892,7 @@ add_swap_extent(struct swap_info_struct 
 	new_se-&gt;nr_pages = nr_pages;
 	new_se-&gt;start_block = start_block;
 
-	lh = sis-&gt;extent_list.prev;	/* The lowest block */
-	while (lh != &amp;sis-&gt;extent_list) {
-		se = list_entry(lh, struct swap_extent, list);
-		if (se-&gt;start_block &gt; start_block)
-			break;
-		lh = lh-&gt;prev;
-	}
-	list_add_tail(&amp;new_se-&gt;list, lh);
+	list_add_tail(&amp;new_se-&gt;list, &amp;sis-&gt;extent_list);
 	sis-&gt;nr_extents++;
 	return 0;
 }
_
</pre></body></html>