diff mbox series

[RFC,v2,06/18] mm: create scaffolds for the new virtual swap implementation

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

Commit Message

Nhat Pham April 29, 2025, 11:38 p.m. UTC
In prepration for the implementation of swap virtualization, add new
scaffolds for the new code:

1. Add a new mm/vswap.c source file, which currently only holds the
   logic to set up the (for now, empty) vswap debugfs directory. Hook
   this up in the swap setup step in mm/swap_state.c. Add a new
   maintainer entry for the new source file.

2. Add a new config option (CONFIG_VIRTUAL_SWAP). We will only get new
   behavior when the kernel is built with this config option. The entry
   for the config option in mm/Kconfig summarizes the pros and cons of
   the new virtual swap design, which the remainder of the patch series
   will implement.

3. Set up vswap compilation in the Makefile.

Other than the debugfs directory, no behavioral change intended.

Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
 MAINTAINERS          |  7 +++++++
 include/linux/swap.h |  9 +++++++++
 mm/Kconfig           | 25 +++++++++++++++++++++++++
 mm/Makefile          |  1 +
 mm/swap_state.c      |  6 ++++++
 mm/vswap.c           | 35 +++++++++++++++++++++++++++++++++++
 6 files changed, 83 insertions(+)
 create mode 100644 mm/vswap.c
diff mbox series

Patch

diff --git a/MAINTAINERS b/MAINTAINERS
index 00e94bec401e..65108bf2a5f1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25290,6 +25290,13 @@  S:	Maintained
 F:	Documentation/devicetree/bindings/iio/light/vishay,veml6030.yaml
 F:	drivers/iio/light/veml6030.c
 
+VIRTUAL SWAP SPACE
+M:	Nhat Pham <nphamcs@gmail.com>
+M:	Johannes Weiner <hannes@cmpxchg.org>
+L:	linux-mm@kvack.org
+S:	Maintained
+F:	mm/vswap.c
+
 VISHAY VEML6075 UVA AND UVB LIGHT SENSOR DRIVER
 M:	Javier Carrasco <javier.carrasco.cruz@gmail.com>
 S:	Maintained
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 567fd2ebb0d3..328f6aec9313 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -726,6 +726,15 @@  static inline bool mem_cgroup_swap_full(struct folio *folio)
 }
 #endif
 
+#ifdef CONFIG_VIRTUAL_SWAP
+int vswap_init(void);
+#else /* CONFIG_VIRTUAL_SWAP */
+static inline int vswap_init(void)
+{
+	return 0;
+}
+#endif /* CONFIG_VIRTUAL_SWAP */
+
 /**
  * swp_entry_to_swp_slot - look up the physical swap slot corresponding to a
  *                         virtual swap slot.
diff --git a/mm/Kconfig b/mm/Kconfig
index 1b501db06417..2e8eb66c5888 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -22,6 +22,31 @@  menuconfig SWAP
 	  used to provide more virtual memory than the actual RAM present
 	  in your computer.  If unsure say Y.
 
+config VIRTUAL_SWAP
+	bool "Swap space virtualization"
+	depends on SWAP
+	default n
+	help
+		When this is selected, the kernel is built with the new swap
+		design, where each swap entry is associated with a virtual swap
+		slot that is decoupled from a specific physical backing storage
+		location. As a result, swap entries that are:
+
+		1. Zero-filled
+
+		2. Stored in the zswap pool.
+
+		3. Rejected by zswap/zram but cannot be written back to a
+		   backing swap device.
+
+		no longer take up any disk storage (i.e they do not occupy any
+		slot in the backing swap device).
+
+		Swapoff is also more efficient.
+
+		There might be more lock contentions with heavy swap use, since
+		the swap cache is no longer range partitioned.
+
 config ZSWAP
 	bool "Compressed cache for swap pages"
 	depends on SWAP
diff --git a/mm/Makefile b/mm/Makefile
index 850386a67b3e..b7216c714fa1 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -76,6 +76,7 @@  ifdef CONFIG_MMU
 endif
 
 obj-$(CONFIG_SWAP)	+= page_io.o swap_state.o swapfile.o swap_slots.o
+obj-$(CONFIG_VIRTUAL_SWAP)	+= vswap.o
 obj-$(CONFIG_ZSWAP)	+= zswap.o
 obj-$(CONFIG_HAS_DMA)	+= dmapool.o
 obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o
diff --git a/mm/swap_state.c b/mm/swap_state.c
index cbd1532b6b24..1607d23a3d7b 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -930,6 +930,12 @@  static int __init swap_init_sysfs(void)
 	int err;
 	struct kobject *swap_kobj;
 
+	err = vswap_init();
+	if (err) {
+		pr_err("failed to initialize virtual swap space\n");
+		return err;
+	}
+
 	swap_kobj = kobject_create_and_add("swap", mm_kobj);
 	if (!swap_kobj) {
 		pr_err("failed to create swap kobject\n");
diff --git a/mm/vswap.c b/mm/vswap.c
new file mode 100644
index 000000000000..b9c28e819cca
--- /dev/null
+++ b/mm/vswap.c
@@ -0,0 +1,35 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Virtual swap space
+ *
+ * Copyright (C) 2024 Meta Platforms, Inc., Nhat Pham
+ */
+ #include <linux/swap.h>
+
+#ifdef CONFIG_DEBUG_FS
+#include <linux/debugfs.h>
+
+static struct dentry *vswap_debugfs_root;
+
+static int vswap_debug_fs_init(void)
+{
+	if (!debugfs_initialized())
+		return -ENODEV;
+
+	vswap_debugfs_root = debugfs_create_dir("vswap", NULL);
+	return 0;
+}
+#else
+static int vswap_debug_fs_init(void)
+{
+	return 0;
+}
+#endif
+
+int vswap_init(void)
+{
+	if (vswap_debug_fs_init())
+		pr_warn("Failed to initialize vswap debugfs\n");
+
+	return 0;
+}