Skip to contentSkip to conversation

mm: zswap: avoid unnecessary xarray lookup in zswap_store()

PATCH In review Kefeng Wang 2 participants 4 messages Sep 9 – Sep 10

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: zswap: avoid unnecessary xarray lookup in zswap_store()

zswap_store() falls through to check_old and walks the swap xarray even when zswap is disabled. Add a zswap_never_enabled() early return matching zswap_load(), and reuse zswap_invalidate() whose xa_empty() check skips empty per-area trees to avoid unnecessary xarry lockup.

Cc Chengming Zhou <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Kairui Song <[email protected]>
Cc Nhat Pham <[email protected]>
Cc Yosry Ahmed <[email protected]>
Cc Andrew Morton <[email protected]>
---
 mm/zswap.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
mm/zswap.c +7 -9
index 5d0d8bd72193..fc6c5e0db5e4 100644
@@ -1488,6 +1488,9 @@ bool zswap_store(struct folio *folio)
 	VM_WARN_ON_ONCE(!folio_test_locked(folio));
 	VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
 
+	if (zswap_never_enabled())
+		return false;
+
 	if (!zswap_enabled)
 		goto check_old;
 
@@ -1545,15 +1548,10 @@ bool zswap_store(struct folio *folio)
 	if (!ret) {
 		unsigned type = swp_type(swp);
 		pgoff_t offset = swp_offset(swp);
-		struct zswap_entry *entry;
-		struct xarray *tree;
-
-		for (index = 0; index < nr_pages; ++index) {
-			tree = swap_zswap_tree(swp_entry(type, offset + index));
-			entry = xa_erase(tree, offset + index);
-			if (entry)
-				zswap_entry_free(entry);
-		}
+
+		for (index = 0; index < nr_pages; ++index)
+			zswap_invalidate(swp_entry(type, offset + index));
+
 	}
 
 	return ret;
Signature · 1 line
2.55.0
#1 lore raw
Quoted 4 lines · Kefeng Wang

zswap_store() falls through to check_old and walks the swap xarray even when zswap is disabled. Add a zswap_never_enabled() early return matching zswap_load(), and reuse zswap_invalidate() whose xa_empty() check skips empty per-area trees to avoid unnecessary xarry lockup.

Sorry for the typo, should be "xarray lookup".

Quoted 46 lines
Cc Chengming Zhou <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Kairui Song <[email protected]>
Cc Nhat Pham <[email protected]>
Cc Yosry Ahmed <[email protected]>
Cc Andrew Morton <[email protected]>
---
  mm/zswap.c | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
mm/zswap.c +7 -9
index 5d0d8bd72193..fc6c5e0db5e4 100644
@@ -1488,6 +1488,9 @@ bool zswap_store(struct folio *folio)
  	VM_WARN_ON_ONCE(!folio_test_locked(folio));
  	VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
 
+	if (zswap_never_enabled())
+		return false;
+
  	if (!zswap_enabled)
  		goto check_old;
 
@@ -1545,15 +1548,10 @@ bool zswap_store(struct folio *folio)
  	if (!ret) {
  		unsigned type = swp_type(swp);
  		pgoff_t offset = swp_offset(swp);
-		struct zswap_entry *entry;
-		struct xarray *tree;
-
-		for (index = 0; index < nr_pages; ++index) {
-			tree = swap_zswap_tree(swp_entry(type, offset + index));
-			entry = xa_erase(tree, offset + index);
-			if (entry)
-				zswap_entry_free(entry);
-		}
+
+		for (index = 0; index < nr_pages; ++index)
+			zswap_invalidate(swp_entry(type, offset + index));
+
  	}
 
  	return ret;
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Kefeng Wang
Quoted 5 lines · Kefeng Wang

zswap_store() falls through to check_old and walks the swap xarray even when zswap is disabled. Add a zswap_never_enabled() early return matching zswap_load(), and reuse zswap_invalidate() whose xa_empty() check skips empty per-area trees to avoid unnecessary xarry lockup.

Quoted 7 lines
Cc Chengming Zhou <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Kairui Song <[email protected]>
Cc Nhat Pham <[email protected]>
Cc Yosry Ahmed <[email protected]>
Cc Andrew Morton <[email protected]>

These are really two separate changes. Can you please split them out?

Quoted 9 lines
---
 mm/zswap.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
mm/zswap.c +0 -0
index 5d0d8bd72193..fc6c5e0db5e4 100644

mm/zswap.c bool zswap_store(struct folio *folio) · line 1492

Quoted 5 lines
mm/zswap.c +2 -0
@@ -1488,6 +1488,9 @@ bool zswap_store(struct folio *folio)
 	VM_WARN_ON_ONCE(!folio_test_locked(folio));
 	VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
 
+	if (zswap_never_enabled())
+		return false;
Quoted 6 lines
mm/zswap.c +0 -2
@@ -1545,15 +1548,10 @@ bool zswap_store(struct folio *folio)
 	if (!ret) {
 		unsigned type = swp_type(swp);
 		pgoff_t offset = swp_offset(swp);
-		struct zswap_entry *entry;
-		struct xarray *tree;

mm/zswap.c bool zswap_store(struct folio *folio) · line 1553

Quoted 10 lines
mm/zswap.c +3 -7
-
-		for (index = 0; index < nr_pages; ++index) {
-			tree = swap_zswap_tree(swp_entry(type, offset + index));
-			entry = xa_erase(tree, offset + index);
-			if (entry)
-				zswap_entry_free(entry);
-		}
+
+		for (index = 0; index < nr_pages; ++index)
+			zswap_invalidate(swp_entry(type, offset + index));

That dance through a swp_entry_t was already kind of awful. This would be a good opportunity to refactor things to avoid that:

mm/zswap.c +23 -18
index e6ec3295bdb0..df18fdaae703 100644
@@ -228,10 +228,15 @@ static bool zswap_has_pool;
 /* One swap address space for each 64M swap space */
 #define ZSWAP_ADDRESS_SPACE_SHIFT 14
 #define ZSWAP_ADDRESS_SPACE_PAGES (1 << ZSWAP_ADDRESS_SPACE_SHIFT)
+
+static inline struct xarray *zswap_tree(int type, pgoff_t offset)
+{
+	return &zswap_trees[type][offset >> ZSWAP_ADDRESS_SPACE_SHIFT];
+}
+
 static inline struct xarray *swap_zswap_tree(swp_entry_t swp)
 {
-	return &zswap_trees[swp_type(swp)][swp_offset(swp)
-		>> ZSWAP_ADDRESS_SPACE_SHIFT];
+	return zswap_tree(swp_type(swp), swp_offset(swp));
 }
 
 #define zswap_pool_debug(msg, p)			\
@@ -729,6 +734,19 @@ static void zswap_entry_free(struct zswap_entry *entry)
 	atomic_long_dec(&zswap_stored_pages);
 }
 
+static void __zswap_invalidate(int type, pgoff_t offset)
+{
+	struct xarray *tree = zswap_tree(type, offset);
+	struct zswap_entry *entry;
+
+	if (xa_empty(tree))
+		return;
+
+	entry = xa_erase(tree, offset);
+	if (entry)
+		zswap_entry_free(entry);
+}
+
 /*********************************
 * compressed storage functions
 **********************************/
@@ -1554,12 +1572,8 @@ bool zswap_store(struct folio *folio)
 		struct zswap_entry *entry;
 		struct xarray *tree;
 
-		for (index = 0; index < nr_pages; ++index) {
-			tree = swap_zswap_tree(swp_entry(type, offset + index));
-			entry = xa_erase(tree, offset + index);
-			if (entry)
-				zswap_entry_free(entry);
-		}
+		for (index = 0; index < nr_pages; ++index)
+			__zswap_invalidate(type, offset + index);
 	}
 
 	return ret;
@@ -1647,16 +1661,7 @@ int zswap_load(struct folio *folio)
 
 void zswap_invalidate(swp_entry_t swp)
 {
-	pgoff_t offset = swp_offset(swp);
-	struct xarray *tree = swap_zswap_tree(swp);
-	struct zswap_entry *entry;
-
-	if (xa_empty(tree))
-		return;
-
-	entry = xa_erase(tree, offset);
-	if (entry)
-		zswap_entry_free(entry);
+	__zswap_invalidate(swp_type(swp), swp_offset(swp));
 }
 
 int zswap_swapon(int type, unsigned long nr_pages)
#3 lore raw
Quoted 14 lines · Johannes Weiner
Quoted 5 lines · Kefeng Wang

zswap_store() falls through to check_old and walks the swap xarray even when zswap is disabled. Add a zswap_never_enabled() early return matching zswap_load(), and reuse zswap_invalidate() whose xa_empty() check skips empty per-area trees to avoid unnecessary xarry lockup.

Quoted 7 lines
Cc Chengming Zhou <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Kairui Song <[email protected]>
Cc Nhat Pham <[email protected]>
Cc Yosry Ahmed <[email protected]>
Cc Andrew Morton <[email protected]>
Quoted 1 line

These are really two separate changes. Can you please split them out?

Sure.

Quoted 13 lines
Quoted 13 lines
---
  mm/zswap.c | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
mm/zswap.c +0 -0
index 5d0d8bd72193..fc6c5e0db5e4 100644
@@ -1488,6 +1488,9 @@ bool zswap_store(struct folio *folio)
  	VM_WARN_ON_ONCE(!folio_test_locked(folio));
  	VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
 
Quoted 5 lines

mm/zswap.c bool zswap_store(struct folio *folio) · line 1492

Quoted 2 lines
mm/zswap.c +2 -0
+	if (zswap_never_enabled())
+		return false;

Thanks.

Quoted 11 lines
Quoted 11 lines
mm/zswap.c +0 -7
@@ -1545,15 +1548,10 @@ bool zswap_store(struct folio *folio)
  	if (!ret) {
  		unsigned type = swp_type(swp);
  		pgoff_t offset = swp_offset(swp);
-		struct zswap_entry *entry;
-		struct xarray *tree;
-
-		for (index = 0; index < nr_pages; ++index) {
-			tree = swap_zswap_tree(swp_entry(type, offset + index));
-			entry = xa_erase(tree, offset + index);
-			if (entry)
Quoted 8 lines

mm/zswap.c bool zswap_store(struct folio *folio)

Quoted 5 lines
mm/zswap.c +3 -2
-				zswap_entry_free(entry);
-		}
+
+		for (index = 0; index < nr_pages; ++index)
+			zswap_invalidate(swp_entry(type, offset + index));

That dance through a swp_entry_t was already kind of awful. This would be a good opportunity to refactor things to avoid that:

It looks better, thanks for your review. will update.

Quoted 76 lines
mm/zswap.c +23 -18
index e6ec3295bdb0..df18fdaae703 100644
@@ -228,10 +228,15 @@ static bool zswap_has_pool;
  /* One swap address space for each 64M swap space */
  #define ZSWAP_ADDRESS_SPACE_SHIFT 14
  #define ZSWAP_ADDRESS_SPACE_PAGES (1 << ZSWAP_ADDRESS_SPACE_SHIFT)
+
+static inline struct xarray *zswap_tree(int type, pgoff_t offset)
+{
+	return &zswap_trees[type][offset >> ZSWAP_ADDRESS_SPACE_SHIFT];
+}
+
  static inline struct xarray *swap_zswap_tree(swp_entry_t swp)
  {
-	return &zswap_trees[swp_type(swp)][swp_offset(swp)
-		>> ZSWAP_ADDRESS_SPACE_SHIFT];
+	return zswap_tree(swp_type(swp), swp_offset(swp));
  }
 
  #define zswap_pool_debug(msg, p)			\
@@ -729,6 +734,19 @@ static void zswap_entry_free(struct zswap_entry *entry)
  	atomic_long_dec(&zswap_stored_pages);
  }
 
+static void __zswap_invalidate(int type, pgoff_t offset)
+{
+	struct xarray *tree = zswap_tree(type, offset);
+	struct zswap_entry *entry;
+
+	if (xa_empty(tree))
+		return;
+
+	entry = xa_erase(tree, offset);
+	if (entry)
+		zswap_entry_free(entry);
+}
+
  /*********************************
  * compressed storage functions
  **********************************/
@@ -1554,12 +1572,8 @@ bool zswap_store(struct folio *folio)
  		struct zswap_entry *entry;
  		struct xarray *tree;
 
-		for (index = 0; index < nr_pages; ++index) {
-			tree = swap_zswap_tree(swp_entry(type, offset + index));
-			entry = xa_erase(tree, offset + index);
-			if (entry)
-				zswap_entry_free(entry);
-		}
+		for (index = 0; index < nr_pages; ++index)
+			__zswap_invalidate(type, offset + index);
  	}
 
  	return ret;
@@ -1647,16 +1661,7 @@ int zswap_load(struct folio *folio)
 
  void zswap_invalidate(swp_entry_t swp)
  {
-	pgoff_t offset = swp_offset(swp);
-	struct xarray *tree = swap_zswap_tree(swp);
-	struct zswap_entry *entry;
-
-	if (xa_empty(tree))
-		return;
-
-	entry = xa_erase(tree, offset);
-	if (entry)
-		zswap_entry_free(entry);
+	__zswap_invalidate(swp_type(swp), swp_offset(swp));
  }
 
  int zswap_swapon(int type, unsigned long nr_pages)