diff mbox series

[RFC,09/14] swap: implement locking out swapoff using virtual swap slot

Message ID 20250407234223.1059191-10-nphamcs@gmail.com
State New
Headers show
Series Virtual Swap Space | expand

Commit Message

Nhat Pham April 7, 2025, 11:42 p.m. UTC
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@gmail.com>
---
 include/linux/swap.h | 24 +++++++++++++++++++++++-
 mm/vswap.c           | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 1 deletion(-)
diff mbox series

Patch

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