diff mbox series

mm/zswap.c: avoid partial LRU inversion by evicting pages earlier

Message ID 43912f1a-c607-ff55-50c7-3285cfb5e7c0@mail.ee (mailing list archive)
State New
Headers show
Series mm/zswap.c: avoid partial LRU inversion by evicting pages earlier | expand

Commit Message

Ruben Bremer May 11, 2021, 12:54 p.m. UTC
Zswap can evict stored pages on an LRU basis when its pool is full. 
However, new incoming pages will then simultaneously also be rejected 
and thus sent directly to swap, thereby effectively partially inversing 
LRU caching behavior.
In order to avoid this LRU inversion, this patch allows zswap to begin 
evicting pages earlier when its pool is only 90 % full but still accept 
new incoming pages. There may be more elegant solutions.

---
diff mbox series

Patch

--- a/mm/zswap.c~mm-zswap-avoid-lru-inversion-by-earlier-eviction
+++ a/mm/zswap.c
@@ -228,6 +228,12 @@  static const struct zpool_ops zswap_zpoo
  	.evict = zswap_writeback_entry
  };

+static bool zswap_is_almost_full(void)
+{
+	return totalram_pages() * zswap_max_pool_percent / 111 <
+			DIV_ROUND_UP(zswap_pool_total_size, PAGE_SIZE);
+}
+
  static bool zswap_is_full(void)
  {
  	return totalram_pages() * zswap_max_pool_percent / 100 <
@@ -1109,20 +1115,24 @@  static int zswap_frontswap_store(unsigne
  	}

  	/* reclaim space if needed */
-	if (zswap_is_full()) {
+	if (zswap_is_almost_full()) {
  		struct zswap_pool *pool;

-		zswap_pool_limit_hit++;
-		zswap_pool_reached_full = true;
  		pool = zswap_pool_last_get();
-		if (pool)
+		if (pool) {
  			queue_work(shrink_wq, &pool->shrink_work);
-		ret = -ENOMEM;
-		goto reject;
+		}
+
+		if (zswap_is_full()) {
+			zswap_pool_limit_hit++;
+			zswap_pool_reached_full = true;
+			ret = -ENOMEM;
+			goto reject;
+		}
  	}

  	if (zswap_pool_reached_full) {
-	       if (!zswap_can_accept()) {
+		if (!zswap_can_accept()) {
  			ret = -ENOMEM;
  			goto reject;
  		} else