linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
To: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: "linux-mm@kvack.org" <linux-mm@kvack.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"nishimura@mxp.nes.nec.co.jp" <nishimura@mxp.nes.nec.co.jp>,
	"balbir@linux.vnet.ibm.com" <balbir@linux.vnet.ibm.com>,
	"hugh.dickins@tiscali.co.uk" <hugh.dickins@tiscali.co.uk>,
	"hannes@cmpxchg.org" <hannes@cmpxchg.org>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>
Subject: [PATCH 3/4] reuse unused swap entry if necessary
Date: Thu, 28 May 2009 14:20:47 +0900	[thread overview]
Message-ID: <20090528142047.3069543b.kamezawa.hiroyu@jp.fujitsu.com> (raw)
In-Reply-To: <20090528135455.0c83bedc.kamezawa.hiroyu@jp.fujitsu.com>

From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>

Now, we can know a swap entry is just used as SwapCache via swap_map,
without looking up swap cache.

Then, we have a chance to reuse swap-cache-only swap entries in
get_swap_pages().

This patch tries to free swap-cache-only swap entries if swap is
not enough.
Note: We hit following path when swap_cluster code cannot find
a free cluster. Then, vm_swap_full() is not only condition to allow
the kernel to reclaim unused swap.

Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
---
 mm/swapfile.c |   39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

Index: new-trial-swapcount2/mm/swapfile.c
===================================================================
--- new-trial-swapcount2.orig/mm/swapfile.c
+++ new-trial-swapcount2/mm/swapfile.c
@@ -73,6 +73,25 @@ static inline unsigned short make_swap_c
 	return ret;
 }
 
+static int
+try_to_reuse_swap(struct swap_info_struct *si, unsigned long offset)
+{
+	int type = si - swap_info;
+	swp_entry_t entry = swp_entry(type, offset);
+	struct page *page;
+	int ret = 0;
+
+	page = find_get_page(&swapper_space, entry.val);
+	if (!page)
+		return 0;
+	if (trylock_page(page)) {
+		ret = try_to_free_swap(page);
+		unlock_page(page);
+	}
+	page_cache_release(page);
+	return ret;
+}
+
 /*
  * We need this because the bdev->unplug_fn can sleep and we cannot
  * hold swap_lock while calling the unplug_fn. And swap_lock
@@ -294,6 +313,18 @@ checks:
 		goto no_page;
 	if (offset > si->highest_bit)
 		scan_base = offset = si->lowest_bit;
+
+	/* reuse swap entry of cache-only swap if not busy. */
+	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+		int ret;
+		spin_unlock(&swap_lock);
+		ret = try_to_reuse_swap(si, offset);
+		spin_lock(&swap_lock);
+		if (ret)
+			goto checks; /* we released swap_lock. retry. */
+		goto scan; /* In some racy case */
+	}
+
 	if (si->swap_map[offset])
 		goto scan;
 
@@ -375,6 +406,10 @@ scan:
 			spin_lock(&swap_lock);
 			goto checks;
 		}
+		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+			spin_lock(&swap_lock);
+			goto checks;
+		}
 		if (unlikely(--latency_ration < 0)) {
 			cond_resched();
 			latency_ration = LATENCY_LIMIT;
@@ -386,6 +421,10 @@ scan:
 			spin_lock(&swap_lock);
 			goto checks;
 		}
+		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
+			spin_lock(&swap_lock);
+			goto checks;
+		}
 		if (unlikely(--latency_ration < 0)) {
 			cond_resched();
 			latency_ration = LATENCY_LIMIT;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  parent reply	other threads:[~2009-05-28  5:22 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-28  4:54 [PATCH 0/4] memcg fix swap accounting (28/May) KAMEZAWA Hiroyuki
2009-05-28  5:10 ` [PATCH 1/4] add swap cache interface for swap reference KAMEZAWA Hiroyuki
2009-05-29  4:21   ` Daisuke Nishimura
2009-05-29  5:08     ` KAMEZAWA Hiroyuki
2009-05-29  5:37       ` [PATCH 1/4] add swap cache interface for swap reference v2 (updated) KAMEZAWA Hiroyuki
2009-05-29  6:05         ` Daisuke Nishimura
2009-05-29  6:53           ` KAMEZAWA Hiroyuki
2009-05-30  5:21         ` Balbir Singh
2009-05-28  5:19 ` [PATCH 2/4] modify swap_map and add SWAP_HAS_CACHE flag KAMEZAWA Hiroyuki
2009-05-30  6:10   ` Balbir Singh
2009-05-30 11:16     ` KAMEZAWA Hiroyuki
2009-05-30 11:35       ` Balbir Singh
2009-06-01  7:04   ` Daisuke Nishimura
2009-05-28  5:20 ` KAMEZAWA Hiroyuki [this message]
2009-05-29 21:55   ` [PATCH 3/4] reuse unused swap entry if necessary Andrew Morton
2009-05-30 11:11     ` KAMEZAWA Hiroyuki
2009-05-30  6:40   ` Balbir Singh
2009-05-28  5:21 ` [PATCH 4/4] memcg: fix swap accounting KAMEZAWA Hiroyuki
2009-05-30  7:20   ` Balbir Singh
2009-06-02  3:04 [PATCH 0/4] memcg fix swap accounting (2/Jun) KAMEZAWA Hiroyuki
2009-06-02  3:12 ` [PATCH 3/4] reuse unused swap entry if necessary KAMEZAWA Hiroyuki
2009-06-04  5:57   ` Daisuke Nishimura
2009-06-04  6:59     ` KAMEZAWA Hiroyuki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090528142047.3069543b.kamezawa.hiroyu@jp.fujitsu.com \
    --to=kamezawa.hiroyu@jp.fujitsu.com \
    --cc=akpm@linux-foundation.org \
    --cc=balbir@linux.vnet.ibm.com \
    --cc=hannes@cmpxchg.org \
    --cc=hugh.dickins@tiscali.co.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nishimura@mxp.nes.nec.co.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox