[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250407234223.1059191-10-nphamcs@gmail.com>
Date: Mon, 7 Apr 2025 16:42:10 -0700
From: Nhat Pham <nphamcs@...il.com>
To: linux-mm@...ck.org
Cc: akpm@...ux-foundation.org,
hannes@...xchg.org,
hughd@...gle.com,
yosry.ahmed@...ux.dev,
mhocko@...nel.org,
roman.gushchin@...ux.dev,
shakeel.butt@...ux.dev,
muchun.song@...ux.dev,
len.brown@...el.com,
chengming.zhou@...ux.dev,
kasong@...cent.com,
chrisl@...nel.org,
huang.ying.caritas@...il.com,
ryan.roberts@....com,
viro@...iv.linux.org.uk,
baohua@...nel.org,
osalvador@...e.de,
lorenzo.stoakes@...cle.com,
christophe.leroy@...roup.eu,
pavel@...nel.org,
kernel-team@...a.com,
linux-kernel@...r.kernel.org,
cgroups@...r.kernel.org,
linux-pm@...r.kernel.org
Subject: [RFC PATCH 09/14] swap: implement locking out swapoff using virtual swap slot
In the old design, we obtain a reference to the swap device to maintain
the validity of the device's metadata struct (i.e swap_info_struct), as
well as the swap entry itself, before various operations.
In the new virtual swap space design, however, this is no longer
necessary - we can simply acquire a reference to the virtual swap slot
itself to ensure it remains valid.
Furthermore, once we decouple virtual swap slots from their backing,
obtaining a reference to the backing swap device itself is not
sufficient or even possible anyway, as the backing of a virtual swap
slot can change under it.
Signed-off-by: Nhat Pham <nphamcs@...il.com>
---
include/linux/swap.h | 24 +++++++++++++++++++++++-
mm/vswap.c | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+), 1 deletion(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 1d8679bd57f3..7f6200f1db33 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -730,11 +730,33 @@ int vswap_init(void);
void vswap_exit(void);
swp_slot_t swp_entry_to_swp_slot(swp_entry_t entry);
swp_entry_t swp_slot_to_swp_entry(swp_slot_t slot);
+bool vswap_tryget(swp_entry_t entry);
+void vswap_put(swp_entry_t entry);
bool folio_swapped(struct folio *folio);
bool vswap_swapcache_only(swp_entry_t entry, int nr);
int non_swapcache_batch(swp_entry_t entry, int nr);
bool swap_free_nr_any_cache_only(swp_entry_t entry, int nr);
void put_swap_folio(struct folio *folio, swp_entry_t entry);
+
+static inline bool trylock_swapoff(swp_entry_t entry,
+ struct swap_info_struct **si)
+{
+ if (!vswap_tryget(entry))
+ return false;
+
+ /*
+ * No need to hold a reference to the swap device. The virtual swap slot pins
+ * the physical swap slot, which in turns pin the swap device.
+ */
+ *si = swap_slot_swap_info(swp_entry_to_swp_slot(entry));
+ return true;
+}
+
+static inline void unlock_swapoff(swp_entry_t entry,
+ struct swap_info_struct *si)
+{
+ vswap_put(entry);
+}
#else
static inline int vswap_init(void)
{
@@ -773,7 +795,6 @@ static inline void put_swap_folio(struct folio *folio, swp_entry_t entry)
{
swap_slot_put_folio(swp_entry_to_swp_slot(entry), folio);
}
-#endif
static inline bool trylock_swapoff(swp_entry_t entry,
struct swap_info_struct **si)
@@ -789,6 +810,7 @@ static inline void unlock_swapoff(swp_entry_t entry,
{
swap_slot_put_swap_info(si);
}
+#endif
#if defined(CONFIG_SWAP) && !defined(CONFIG_VIRTUAL_SWAP)
int add_swap_count_continuation(swp_entry_t, gfp_t);
diff --git a/mm/vswap.c b/mm/vswap.c
index 1b8cf894390c..8a518ebd20e4 100644
--- a/mm/vswap.c
+++ b/mm/vswap.c
@@ -425,6 +425,42 @@ swp_entry_t swp_slot_to_swp_entry(swp_slot_t slot)
return ret;
}
+/**
+ * vswap_tryget - try to obtain an ephemeral reference to a virtual swap slot.
+ *
+ * @entry: the virtual swap slot.
+ *
+ * Return: true if the reference was obtained.
+ */
+bool vswap_tryget(swp_entry_t entry)
+{
+ struct swp_desc *desc;
+ bool ret;
+
+ rcu_read_lock();
+ desc = xa_load(&vswap_map, entry.val);
+ if (!desc) {
+ rcu_read_unlock();
+ return false;
+ }
+
+ ret = kref_get_unless_zero(&desc->refcnt);
+ rcu_read_unlock();
+ return ret;
+}
+
+/**
+ * vswap_put - release an ephemeral reference to the virtual swap slot.
+ *
+ * @entry: the virtual swap slot.
+ */
+void vswap_put(swp_entry_t entry)
+{
+ struct swp_desc *desc = xa_load(&vswap_map, entry.val);
+
+ kref_put(&desc->refcnt, vswap_ref_release);
+}
+
/**
* swap_free_nr_any_cache_only - decrease the swap count of nr contiguous swap
* entries by 1 (when the swap entries are removed
--
2.47.1
Powered by blists - more mailing lists