[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240408121439.GA252652@bytedance>
Date: Mon, 8 Apr 2024 20:14:39 +0800
From: Zhaoyu Liu <liuzhaoyu.zackary@...edance.com>
To: akpm@...ux-foundation.org, ryncsn@...il.com, nphamcs@...il.com
Cc: ying.huang@...el.com, songmuchun@...edance.com, david@...hat.com,
chrisl@...nel.org, guo.ziliang@....com.cn, yosryahmed@...gle.com,
linux-kernel@...r.kernel.org, linux-mm@...ck.org
Subject: [PATCH v2] mm: swap: prejudgement swap_has_cache to avoid page
allocation
Based on qemu arm64 - latest kernel + 100M memory + 1024M swapfile.
Create 1G anon mmap and set it to shared, and has two processes
randomly access the shared memory. When they are racing on swap cache,
on average, each "alloc_pages_mpol + swapcache_prepare + folio_put"
took about 1475 us.
So skip page allocation if SWAP_HAS_CACHE was set, just
schedule_timeout_uninterruptible and continue to acquire page
via filemap_get_folio() from swap cache, to speedup
__read_swap_cache_async.
Signed-off-by: Zhaoyu Liu <liuzhaoyu.zackary@...edance.com>
---
Changes in v2:
- Fix the patch format and rebase to latest linux-next.
---
include/linux/swap.h | 6 ++++++
mm/swap_state.c | 10 ++++++++++
mm/swapfile.c | 15 +++++++++++++++
3 files changed, 31 insertions(+)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 11c53692f65f..a374070e05a7 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -492,6 +492,7 @@ extern sector_t swapdev_block(int, pgoff_t);
extern int __swap_count(swp_entry_t entry);
extern int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry);
extern int swp_swapcount(swp_entry_t entry);
+extern bool swap_has_cache(struct swap_info_struct *si, swp_entry_t entry);
struct swap_info_struct *swp_swap_info(swp_entry_t entry);
struct backing_dev_info;
extern int init_swap_address_space(unsigned int type, unsigned long nr_pages);
@@ -583,6 +584,11 @@ static inline int swp_swapcount(swp_entry_t entry)
return 0;
}
+static inline bool swap_has_cache(struct swap_info_struct *si, swp_entry_t entry)
+{
+ return false;
+}
+
static inline swp_entry_t folio_alloc_swap(struct folio *folio)
{
swp_entry_t entry;
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 642c30d8376c..f117fbf18b59 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -462,6 +462,15 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
if (!swap_swapcount(si, entry) && swap_slot_cache_enabled)
goto fail_put_swap;
+ /*
+ * Skipping page allocation if SWAP_HAS_CACHE was set,
+ * just schedule_timeout_uninterruptible and continue to
+ * acquire page via filemap_get_folio() from swap cache,
+ * to speedup __read_swap_cache_async.
+ */
+ if (swap_has_cache(si, entry))
+ goto skip_alloc;
+
/*
* Get a new folio to read into from swap. Allocate it now,
* before marking swap_map SWAP_HAS_CACHE, when -EEXIST will
@@ -483,6 +492,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,
if (err != -EEXIST)
goto fail_put_swap;
+skip_alloc:
/*
* Protect against a recursive call to __read_swap_cache_async()
* on the same entry waiting forever here because SWAP_HAS_CACHE
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 3ee8957a46e6..b016ebc43b0d 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1511,6 +1511,21 @@ int swp_swapcount(swp_entry_t entry)
return count;
}
+/*
+ * Verify that a swap entry has been tagged with SWAP_HAS_CACHE
+ */
+bool swap_has_cache(struct swap_info_struct *si, swp_entry_t entry)
+{
+ pgoff_t offset = swp_offset(entry);
+ struct swap_cluster_info *ci;
+ bool has_cache;
+
+ ci = lock_cluster_or_swap_info(si, offset);
+ has_cache = !!(si->swap_map[offset] & SWAP_HAS_CACHE);
+ unlock_cluster_or_swap_info(si, ci);
+ return has_cache;
+}
+
static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,
swp_entry_t entry,
unsigned int nr_pages)
--
2.25.1
Powered by blists - more mailing lists