mm: zswap: misc optimizations
Subsystems (3)
v2 cover mm: zswap: misc optimizations
This series cleans up duplication in zswap_invalidate() and adds fast paths in zswap_store() which now skips redundant xarray lookups, while swap_range_free() skips needless zswap_invalidate() calls.
v2:
- split patches and refactor zswap_invalidate(), suggested by Johannes Weiner
- add patch4 to skip zswap_invalidate() in swap_range_free()
Kefeng Wang (4):
mm: zswap: pass type and offset to zswap_invalidate() directly
mm: zswap: reuse zswap_invalidate() in zswap_store() check_old path
mm: zswap: avoid unnecessary xarray lookup in zswap_store()
mm: zswap: skip zswap_invalidate() in swap_range_free() when zswap is
unused
include/linux/zswap.h | 4 ++-- mm/swapfile.c | 6 ++++-- mm/zswap.c | 29 +++++++++++++++-------------- 3 files changed, 21 insertions(+), 18 deletions(-)
Signature · 1 line
2.55.0
PATCH v2 1/4 mm: zswap: pass type and offset to zswap_invalidate() directly
zswap_invalidate() takes a swp_entry_t only to unpack it right back into type and offset. Both callers already have those values, so pass them directly and drop the swp_entry()/swp_type()/swp_offset() round-trip.
--- include/linux/zswap.h | 4 ++-- mm/swapfile.c | 2 +- mm/zswap.c | 14 +++++++++----- 3 files changed, 12 insertions(+), 8 deletions(-)
include/linux/zswap.h +2 -2
@@ -27,7 +27,7 @@ struct zswap_lruvec_state {unsigned long zswap_total_pages (void );bool zswap_store (struct folio *folio);int zswap_load (struct folio *folio); -void zswap_invalidate (swp_entry_t swp); +void zswap_invalidate (int type,pgoff_t offset);int zswap_swapon (int type,unsigned long nr_pages);void zswap_swapoff (int type);void zswap_memcg_offline_cleanup (struct mem_cgroup *memcg);
@@ -49,7 +49,7 @@ static inline int zswap_load(struct folio *folio)return -ENOENT ; } -static inline void zswap_invalidate (swp_entry_t swp) {} +static inline void zswap_invalidate (int type,pgoff_t offset) {}static inline int zswap_swapon (int type,unsigned long nr_pages) {return 0 ;
mm/swapfile.c +1 -1
@@ -1319,7 +1319,7 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,unsigned int i;for (i =0 ; i < nr_entries; i++) -zswap_invalidate (swp_entry (si->type, offset + i)); +zswap_invalidate (si->type, offset + i);if (si->flags &SWP_BLKDEV ) swap_slot_free_notify =
mm/zswap.c +9 -5
@@ -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) \
@@ -1659,10 +1664,9 @@ int zswap_load(struct folio *folio)return 0 ; } -void zswap_invalidate (swp_entry_t swp) +void zswap_invalidate (int type,pgoff_t offset) { -pgoff_t offset =swp_offset (swp); -struct xarray *tree =swap_zswap_tree (swp); +struct xarray *tree =zswap_tree (type, offset);struct zswap_entry *entry;if (xa_empty (tree))
Signature · 1 line
2.55.0
Re: [PATCH v2 1/4] mm: zswap: pass type and offset to zswap_invalidate() directly
Quoted 6 lines · Kefeng Wang
zswap_invalidate()takes aswp_entry_tonly to unpack it right back into type and offset. Both callers already have those values, so pass them directly and drop the swp_entry()/swp_type()/swp_offset() round-trip.
Quoted 1 line
Signed-off-by Kefeng Wang <[email protected]>
Quoted 80 lines
--- include/linux/zswap.h | 4 ++-- mm/swapfile.c | 2 +- mm/zswap.c | 14 +++++++++----- 3 files changed, 12 insertions(+), 8 deletions(-)include/linux/zswap.h +2
-2@@ -27,7 +27,7 @@ struct zswap_lruvec_state {unsigned long zswap_total_pages (void );bool zswap_store (struct folio *folio);int zswap_load (struct folio *folio); -void zswap_invalidate (swp_entry_t swp); +void zswap_invalidate (int type,pgoff_t offset);int zswap_swapon (int type,unsigned long nr_pages);void zswap_swapoff (int type);void zswap_memcg_offline_cleanup (struct mem_cgroup *memcg);@@ -49,7 +49,7 @@ static inline int zswap_load(struct folio *folio)return -ENOENT ; } -static inline void zswap_invalidate (swp_entry_t swp) {} +static inline void zswap_invalidate (int type,pgoff_t offset) {}static inline int zswap_swapon (int type,unsigned long nr_pages) {return 0 ;mm/swapfile.c +1
-1@@ -1319,7 +1319,7 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,unsigned int i;for (i =0 ; i < nr_entries; i++) -zswap_invalidate (swp_entry (si->type, offset + i)); +zswap_invalidate (si->type, offset + i);if (si->flags &SWP_BLKDEV ) swap_slot_free_notify =mm/zswap.c +9
-6@@ -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) \@@ -1659,10 +1664,9 @@ int zswap_load(struct folio *folio)return 0 ; } -void zswap_invalidate (swp_entry_t swp) +void zswap_invalidate (int type,pgoff_t offset) { -pgoff_t offset =swp_offset (swp); -struct xarray *tree =swap_zswap_tree (swp); +struct xarray *tree =zswap_tree (type, offset);struct zswap_entry *entry;if (xa_empty (tree)) --2.55.0
Re: [PATCH v2 1/4] mm: zswap: pass type and offset to zswap_invalidate() directly
In reply to Kefeng WangQuoted 7 lines · Kefeng Wang
zswap_invalidate()takes aswp_entry_tonly to unpack it right back into type and offset. Both callers already have those values, so pass them directly and drop the swp_entry()/swp_type()/swp_offset() round-trip.Signed-off-by Kefeng Wang <[email protected]>
Re: [PATCH v2 1/4] mm: zswap: pass type and offset to zswap_invalidate() directly
In reply to Kefeng WangQuoted 6 lines · Kefeng Wang
zswap_invalidate()takes aswp_entry_tonly to unpack it right back into type and offset. Both callers already have those values, so pass them directly and drop the swp_entry()/swp_type()/swp_offset() round-trip.Signed-off-by Kefeng Wang <[email protected]>
Noice.
PATCH v2 2/4 mm: zswap: reuse zswap_invalidate() in zswap_store() check_old path
In reply to Kefeng WangThe check_old path in zswap_store() open-codes the same per-slot xarray lookup and entry free that zswap_invalidate() already does. Reuse zswap_invalidate() whose xa_empty() check skips empty per-area trees to avoid unnecessary xarray lookup and code duplication.
--- mm/zswap.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
mm/zswap.c +3 -9
@@ -1549,15 +1549,9 @@ 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 (type, offset + index); }return ret;
Signature · 1 line
2.55.0
Re: [PATCH v2 2/4] mm: zswap: reuse zswap_invalidate() in zswap_store() check_old path
Quoted 7 lines · Kefeng Wang
The
check_oldpath inzswap_store()open-codes the same per-slot xarray lookup and entry free thatzswap_invalidate()already does. Reusezswap_invalidate()whosexa_empty()check skips empty per-area trees to avoid unnecessary xarray lookup and code duplication.Signed-off-by Kefeng Wang <[email protected]>
Nice.
Quoted 31 lines
--- mm/zswap.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)mm/zswap.c +3
-10@@ -1549,15 +1549,9 @@ 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 (type, offset + index); }return ret; --2.55.0
Re: [PATCH v2 2/4] mm: zswap: reuse zswap_invalidate() in zswap_store() check_old path
In reply to Kefeng WangQuoted 6 lines · Kefeng Wang
The
check_oldpath inzswap_store()open-codes the same per-slot xarray lookup and entry free thatzswap_invalidate()already does. Reusezswap_invalidate()whosexa_empty()check skips empty per-area trees to avoid unnecessary xarray lookup and code duplication.Signed-off-by Kefeng Wang <[email protected]>
PATCH v2 3/4 mm: zswap: avoid unnecessary xarray lookup in zswap_store()
In reply to Kefeng Wangzswap_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() to avoid unnecessary xarray lookup.
--- mm/zswap.c | 3 +++ 1 file changed, 3 insertions(+)
mm/zswap.c +3 -0
@@ -1492,6 +1492,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;
Signature · 1 line
2.55.0
Re: [PATCH v2 3/4] mm: zswap: avoid unnecessary xarray lookup in zswap_store()
Quoted 7 lines · Kefeng Wang
zswap_store()falls through tocheck_oldand walks the swap xarray even when zswap is disabled. Add azswap_never_enabled()early return matchingzswap_load()to avoid unnecessary xarray lookup.Reviewed-by Johannes Weiner <[email protected]>Signed-off-by Kefeng Wang <[email protected]>
Quoted 22 lines
--- mm/zswap.c | 3 +++ 1 file changed, 3 insertions(+)mm/zswap.c +3
-1@@ -1492,6 +1492,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; --2.55.0
PATCH v2 4/4 mm: zswap: skip zswap_invalidate() in swap_range_free() when zswap is unused
In reply to Kefeng Wangswap_range_free() calls zswap_invalidate() for every slot being freed, even when zswap has never been enabled. Guard the loop with zswap_never_enabled() to skip it.
--- mm/swapfile.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/swapfile.c +4 -2
@@ -1318,8 +1318,10 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,void (*swap_slot_free_notify)(struct block_device *,unsigned long );unsigned int i; -for (i =0 ; i < nr_entries; i++) -zswap_invalidate (si->type, offset + i); +if (!zswap_never_enabled ()) { +for (i =0 ; i < nr_entries; i++) +zswap_invalidate (si->type, offset + i); + }if (si->flags &SWP_BLKDEV ) swap_slot_free_notify =
Signature · 1 line
2.55.0
Yosry Ahmed
maintainerRe: [PATCH v2 4/4] mm: zswap: skip zswap_invalidate() in swap_range_free() when zswap is unused
Quoted 15 lines · Kefeng Wang
swap_range_free()callszswap_invalidate()for every slot being freed, even when zswap has never been enabled. Guard the loop withzswap_never_enabled()to skip it.Signed-off-by Kefeng Wang <[email protected]>--- mm/swapfile.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/swapfile.c +0
-0
mm/swapfile.c static void swap_range_free(struct swap_info_struct *si, unsigned long offset, · line 1324
Quoted 9 lines
mm/swapfile.c +4
-2@@ -1318,8 +1318,10 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,void (*swap_slot_free_notify)(struct block_device *,unsigned long );unsigned int i; -for (i =0 ; i < nr_entries; i++) -zswap_invalidate (si->type, offset + i); +if (!zswap_never_enabled ()) { +for (i =0 ; i < nr_entries; i++) +zswap_invalidate (si->type, offset + i); + }
What if we add the check in zswap_invalidate()? I understand we'd avoid the loop here, which is nice, but I wonder if it's actually a measurable difference.
If we keep it in zswap_invalidate(), we can probably also skip patch 3?
Quoted 7 lines
mm/swapfile.c +0
-1if (si->flags &SWP_BLKDEV ) swap_slot_free_notify = --2.55.0
Re: [PATCH v2 4/4] mm: zswap: skip zswap_invalidate() in swap_range_free() when zswap is unused
Quoted 19 lines · Yosry Ahmed
Quoted 18 lines · Kefeng Wang
swap_range_free()callszswap_invalidate()for every slot being freed, even when zswap has never been enabled. Guard the loop withzswap_never_enabled()to skip it.Signed-off-by Kefeng Wang <[email protected]>--- mm/swapfile.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/swapfile.c +0
-0@@ -1318,8 +1318,10 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,void (*swap_slot_free_notify)(struct block_device *,unsigned long );unsigned int i;
Quoted 11 lines
mm/swapfile.c static void swap_range_free(struct swap_info_struct *si, unsigned long offset, · line 1324
Quoted 6 lines
mm/swapfile.c +4
-2-for (i =0 ; i < nr_entries; i++) -zswap_invalidate (si->type, offset + i); +if (!zswap_never_enabled ()) { +for (i =0 ; i < nr_entries; i++) +zswap_invalidate (si->type, offset + i); + }What if we add the check in
zswap_invalidate()? I understand we'd avoid the loop here, which is nice, but I wonder if it's actually a measurable difference.
Skipping useless instructions is always a good thing.
Quoted 1 line
If we keep it in
zswap_invalidate(), we can probably also skip patch 3?
Maybe add nr_entries to zswap_invalidate() and check zswap_never_enabled() in it.
include/linux/zswap.h +2 -2
@@ -27,7 +27,7 @@ struct zswap_lruvec_state {unsigned long zswap_total_pages (void );bool zswap_store (struct folio *folio);int zswap_load (struct folio *folio); -void zswap_invalidate (int type,pgoff_t offset); +void zswap_invalidate (int type,pgoff_t offset,unsigned int nr_entries);int zswap_swapon (int type,unsigned long nr_pages);void zswap_swapoff (int type);void zswap_memcg_offline_cleanup (struct mem_cgroup *memcg);
@@ -49,7 +49,7 @@ static inline int zswap_load(struct folio *folio)return -ENOENT ; } -static inline void zswap_invalidate (int type,pgoff_t offset) {} +static inline void zswap_invalidate (int type,pgoff_t offset,unsigned int nr_entries) {}static inline int zswap_swapon (int type,unsigned long nr_pages) {return 0 ;
mm/swapfile.c +0 -0
@@ -1316,12 +1316,8 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, {unsigned long end = offset + nr_entries -1 ; void (*swap_slot_free_notify)(struct block_device *, unsigned
long);
- unsigned int i;
- if (!zswap_never_enabled()) {
- for (i = 0; i < nr_entries; i++)
- zswap_invalidate(si->type, offset + i);
- }
+ zswap_invalidate(si->type, offset, nr_entries);
if (si->flags & SWP_BLKDEV)
swap_slot_free_notify =mm/zswap.c +16 -13
@@ -1549,13 +1549,8 @@ bool zswap_store(struct folio *folio)* offsets corresponding to each page of the folio. Otherwise, * writeback could overwrite the new data in the swapfile. */ -if (!ret) { -unsigned type =swp_type (swp); -pgoff_t offset =swp_offset (swp); - -for (index =0 ; index < nr_pages; ++index) -zswap_invalidate (type, offset + index); - } +if (!ret) +zswap_invalidate (swp_type (swp),swp_offset (swp), nr_pages);return ret; }
@@ -1661,17 +1656,25 @@ int zswap_load(struct folio *folio)return 0 ; } -void zswap_invalidate (int type,pgoff_t offset) +void zswap_invalidate (int type,pgoff_t offset,int nr_entries) { -struct xarray *tree =zswap_tree (type, offset);struct zswap_entry *entry; +struct xarray *tree; +int i; -if (xa_empty (tree)) +if (!zswap_never_enabled ())return ; - entry =xa_erase (tree, offset); -if (entry) -zswap_entry_free (entry); +for (i =0 ; i < nr_entries; ++i) { + tree =zswap_tree (type, offset + i); + +if (xa_empty (tree)) +return ; + + entry =xa_erase (tree, offset + i); +if (entry) +zswap_entry_free (entry); + } }
If no objections, I will refresh all the patches.
Yosry Ahmed
maintainerRe: [PATCH v2 4/4] mm: zswap: skip zswap_invalidate() in swap_range_free() when zswap is unused
Quoted 136 lines · Kefeng Wang
Quoted 19 lines · Yosry Ahmed
Quoted 18 lines · Kefeng Wang
swap_range_free()callszswap_invalidate()for every slot being freed, even when zswap has never been enabled. Guard the loop withzswap_never_enabled()to skip it.Signed-off-by Kefeng Wang <[email protected]>--- mm/swapfile.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/swapfile.c +0
-0@@ -1318,8 +1318,10 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,void (*swap_slot_free_notify)(struct block_device *,unsigned long );unsigned int i;Quoted 11 lines
mm/swapfile.c static void swap_range_free(struct swap_info_struct *si, unsigned long offset, · line 1324
Quoted 6 lines
mm/swapfile.c +4
-2-for (i =0 ; i < nr_entries; i++) -zswap_invalidate (si->type, offset + i); +if (!zswap_never_enabled ()) { +for (i =0 ; i < nr_entries; i++) +zswap_invalidate (si->type, offset + i); + }What if we add the check in
zswap_invalidate()? I understand we'd avoid the loop here, which is nice, but I wonder if it's actually a measurable difference.Skipping useless instructions is always a good thing.
Quoted 1 line
If we keep it in
zswap_invalidate(), we can probably also skip patch 3?Maybe add
nr_entriestozswap_invalidate()and checkzswap_never_enabled()in it.include/linux/zswap.h +2
-2@@ -27,7 +27,7 @@ struct zswap_lruvec_state {unsigned long zswap_total_pages (void );bool zswap_store (struct folio *folio);int zswap_load (struct folio *folio); -void zswap_invalidate (int type,pgoff_t offset); +void zswap_invalidate (int type,pgoff_t offset,unsigned int nr_entries);int zswap_swapon (int type,unsigned long nr_pages);void zswap_swapoff (int type);void zswap_memcg_offline_cleanup (struct mem_cgroup *memcg);@@ -49,7 +49,7 @@ static inline int zswap_load(struct folio *folio)return -ENOENT ; } -static inline void zswap_invalidate (int type,pgoff_t offset) {} +static inline void zswap_invalidate (int type,pgoff_t offset,unsigned int nr_entries) {}static inline int zswap_swapon (int type,unsigned long nr_pages) {return 0;mm/swapfile.c +1
-5@@ -1316,12 +1316,8 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, { unsigned long end = offset + nr_entries - 1;void (*swap_slot_free_notify)(struct block_device *,unsigned long ); -unsigned int i; -if (!zswap_never_enabled ()) { -for (i =0 ; i < nr_entries; i++) -zswap_invalidate (si->type, offset + i); - } +zswap_invalidate (si->type, offset, nr_entries);if (si->flags & SWP_BLKDEV) swap_slot_free_notify =mm/zswap.c +16
-13@@ -1549,13 +1549,8 @@ bool zswap_store(struct folio *folio)* offsets corresponding to each page of the folio. Otherwise, * writeback could overwrite the new data in the swapfile. */ -if (!ret) { -unsigned type =swp_type (swp); -pgoff_t offset =swp_offset (swp); - -for (index =0 ; index < nr_pages; ++index) -zswap_invalidate (type, offset + index); - } +if (!ret) +zswap_invalidate (swp_type (swp),swp_offset (swp), nr_pages);return ret; }@@ -1661,17 +1656,25 @@ int zswap_load(struct folio *folio)return 0 ; } -void zswap_invalidate (int type,pgoff_t offset) +void zswap_invalidate (int type,pgoff_t offset,int nr_entries) { -struct xarray *tree =zswap_tree (type, offset);struct zswap_entry *entry; +struct xarray *tree; +int i; -if (xa_empty (tree)) +if (!zswap_never_enabled ())return ; - entry =xa_erase (tree, offset); -if (entry) -zswap_entry_free (entry); +for (i =0 ; i < nr_entries; ++i) { + tree =zswap_tree (type, offset + i); + +if (xa_empty (tree)) +return ; + + entry =xa_erase (tree, offset + i); +if (entry) +zswap_entry_free (entry); + } }
Quoted 1 line
If no objections, I will refresh all the patches.
Yeah I think that's a good idea, even for the zswap_store() path.