| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/08/27 09:59 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": [
"XSWAP",
"ZSWAP",
"ZSWAP_DEFAULT_ON",
"SYSFS"
],
"FocusSymbols": [
"xswap_create",
"xswap_destroy",
"xswap_map_clusters",
"xswap_unmap_clusters",
"xswap_try_shrink",
"xswap_update_free_tail",
"xswap_trim_free_tail",
"xswap_create_store",
"xswap_destroy_store",
"xswap_max_clusters_write"
],
"KMSANReasoning": "The patch introduces an extendable virtual swap device (xswap) and modifies swap management logic. It adds sysfs and debugfs interfaces, but all data copied to userspace is properly initialized (e.g., via `snprintf` in `xswap_max_clusters_read`). The new data structures, such as the dynamically mapped `cluster_info` pages, are explicitly allocated with `__GFP_ZERO`. The `xswap_page_data` struct used during unmapping correctly tracks the number of initialized array elements to avoid freeing uninitialized pointers. There are no info-leaks or uses of uninitialized memory introduced by this patch. Any potential memory errors like out-of-bounds accesses or use-after-free bugs in the new swap logic will be effectively caught by standard KASAN. Therefore, a dedicated KMSAN fuzzing session is not required.",
"NeedsKMSAN": false,
"Reasoning": "The patch introduces a new extendable swap device (xswap) that decouples PTE swap entries from physical backing storage. It dynamically grows and shrinks the cluster_info array via a VM_SPARSE area. The feature is exposed to userspace via sysfs (`/sys/kernel/mm/xswap/create` and `destroy`) and debugfs for runtime size tuning. This is a significant functional change to the core memory management and swap subsystems, and it is reachable from userspace, making it highly worth fuzzing.",
"WorthFuzzing": true
} |
| 1/1 | 2026/08/27 09:59 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 886eb2bf972c2bb8fe0af9acf11f1de148f1d3e8\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Thu Aug 27 09:59:55 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/include/linux/swap.h b/include/linux/swap.h\nindex 5658a1634b85e..998f04c0b7674 100644\n--- a/include/linux/swap.h\n+++ b/include/linux/swap.h\n@@ -207,6 +207,7 @@ enum {\n \tSWP_STABLE_WRITES = (1 \u003c\u003c 11),\t/* no overwrite PG_writeback pages */\n \tSWP_SYNCHRONOUS_IO = (1 \u003c\u003c 12),\t/* synchronous IO is efficient */\n \tSWP_HIBERNATION = (1 \u003c\u003c 13),\t/* pinned for hibernation */\n+\tSWP_XSWAP\t= (1 \u003c\u003c 14),\t/* extendable swap device */\n \t\t\t\t\t/* add others here before... */\n };\n \n@@ -247,6 +248,16 @@ struct swap_info_struct {\n \tsigned char\ttype;\t\t/* strange name for an index */\n \tunsigned int\tmax;\t\t/* size of this swap device */\n \tstruct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */\n+#ifdef CONFIG_XSWAP\n+\tstruct vm_struct\t*cluster_vm;\t/* VM_SPARSE area for xswap dynamic cluster_info */\n+\tunsigned long\t\tnr_clusters_max;/* upper limit from swap header */\n+\tunsigned long\t\tnr_clusters;\t/* current growth ceiling (≤ nr_clusters_max) */\n+\tunsigned long\t\tnr_clusters_mapped; /* currently mapped cluster count */\n+\tunsigned long\t\tnr_free_tail;\t/* contiguous free clusters at tail */\n+\tstruct dentry\t\t*debugfs_entry;\t/* debugfs: type\u003cN\u003e_max_clusters */\n+\tstruct work_struct\txswap_shrink_work; /* deferred shrink trigger */\n+\tstruct mutex\t\txswap_lock;\t/* serialize map/unmap operations */\n+#endif\n \tstruct list_head free_clusters; /* free clusters list */\n \tstruct list_head full_clusters; /* full clusters list */\n \tstruct list_head nonfull_clusters[SWAP_NR_ORDERS];\n@@ -356,6 +367,7 @@ void free_folio_and_swap_cache(struct folio *folio);\n void free_pages_and_swap_cache(struct encoded_page **, int);\n /* linux/mm/swapfile.c */\n extern atomic_long_t nr_swap_pages;\n+extern atomic_t nr_real_swapfiles;\n extern long total_swap_pages;\n extern atomic_t nr_rotate_swap;\n \ndiff --git a/mm/Kconfig b/mm/Kconfig\nindex 604c58199acbf..26cf26a2c59fd 100644\n--- a/mm/Kconfig\n+++ b/mm/Kconfig\n@@ -122,6 +122,15 @@ config ZSWAP_COMPRESSOR_DEFAULT\n default \"zstd\" if ZSWAP_COMPRESSOR_DEFAULT_ZSTD\n default \"\"\n \n+config XSWAP\n+\tbool \"Extendable (virtual) swap device\"\n+\tdepends on SWAP \u0026\u0026 64BIT\n+\thelp\n+\t Adds support for extendable swap devices (xswap) that decouple\n+\t PTE swap entries from physical backing storage. The cluster_info\n+\t array is backed by a sparse vmalloc area that grows and shrinks\n+\t on demand, avoiding static pre-allocation overhead.\n+\n config ZSMALLOC\n \ttristate\n \ndiff --git a/mm/page_io.c b/mm/page_io.c\nindex 88962571cb931..5483c943e3e35 100644\n--- a/mm/page_io.c\n+++ b/mm/page_io.c\n@@ -248,6 +248,17 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)\n \t}\n \trcu_read_unlock();\n \n+\t/*\n+\t * ctx-\u003esis is set by swap_add_folio() which is called from\n+\t * __swap_writepage() below. Since we must avoid the writepage\n+\t * path for xswap devices, use the swap_info from the folio's\n+\t * swap entry directly instead of going through ctx.\n+\t */\n+\tif (unlikely(__swap_entry_to_info(folio-\u003eswap)-\u003eflags \u0026 SWP_XSWAP)) {\n+\t\tfolio_mark_dirty(folio);\n+\t\treturn AOP_WRITEPAGE_ACTIVATE;\n+\t}\n+\n \t__swap_writepage(ctx, folio);\n \treturn 0;\n out_unlock:\n@@ -480,6 +491,11 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)\n \tif (zswap_load(folio) != -ENOENT)\n \t\tgoto finish;\n \n+\tif (unlikely(sis-\u003eflags \u0026 SWP_XSWAP)) {\n+\t\tfolio_unlock(folio);\n+\t\tgoto finish;\n+\t}\n+\n \t/* We have to read from slower devices. Increase zswap protection. */\n \tzswap_folio_swapin(folio);\n \tswap_add_folio(ctx, folio, READ);\ndiff --git a/mm/swap_state.c b/mm/swap_state.c\nindex b76eb3d876fd7..2eedb7a3d7bb2 100644\n--- a/mm/swap_state.c\n+++ b/mm/swap_state.c\n@@ -830,6 +830,13 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,\n \tstruct blk_plug plug;\n \tswp_entry_t ra_entry;\n \n+\t/*\n+\t * The entry may have been freed by another task. Avoid swap_info_get()\n+\t * which will print error message if the race happens.\n+\t */\n+\tif (si-\u003eflags \u0026 SWP_XSWAP)\n+\t\tgoto skip;\n+\n \tmask = swapin_nr_pages(offset) - 1;\n \tif (!mask)\n \t\tgoto skip;\ndiff --git a/mm/swapfile.c b/mm/swapfile.c\nindex 53bf01d5f7f11..258b52bce4389 100644\n--- a/mm/swapfile.c\n+++ b/mm/swapfile.c\n@@ -16,6 +16,8 @@\n #include \u003clinux/kernel_stat.h\u003e\n #include \u003clinux/swap.h\u003e\n #include \u003clinux/vmalloc.h\u003e\n+#include \u003clinux/kobject.h\u003e\n+#include \u003clinux/sysfs.h\u003e\n #include \u003clinux/pagemap.h\u003e\n #include \u003clinux/namei.h\u003e\n #include \u003clinux/shmem_fs.h\u003e\n@@ -48,6 +50,213 @@\n #include \"swap_table.h\"\n #include \"internal.h\"\n #include \"swap.h\"\n+#include \u003clinux/debugfs.h\u003e\n+\n+#define DEF_SWAP_PRIO -1\n+\n+static DEFINE_SPINLOCK(swap_lock);\n+\n+#ifdef CONFIG_XSWAP\n+/*\n+ * xswap: dynamically grow and shrink the cluster_info array via a\n+ * VM_SPARSE area.\n+ *\n+ * XSWAP_GROW_CLUSTERS is the number of clusters to map/unmap in one\n+ * grow/shrink operation. It is set to the number of cluster_info\n+ * structs that fit in a single page (at least 16), so that the vmalloc\n+ * page table\n+ * overhead is proportional to the number of clusters mapped.\n+ */\n+#define XSWAP_GROW_CLUSTERS \\\n+\tmax_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16)\n+#define XSWAP_DEFAULT_CLUSTER_PERCENT 30\n+\n+static struct dentry *xswap_debugfs_root;\n+\n+static int xswap_map_clusters(struct swap_info_struct *si,\n+\t\t\t unsigned long start_idx, unsigned long nr);\n+static void xswap_unmap_clusters(struct swap_info_struct *si,\n+\t\t\t\t unsigned long start_idx, unsigned long nr);\n+static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data);\n+static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx);\n+static void xswap_update_free_tail(struct swap_info_struct *si,\n+\t\t\t\t unsigned long freed_idx);\n+static void xswap_try_shrink(struct swap_info_struct *si);\n+\n+/*\n+ * debugfs read/write for per-device max cluster count.\n+ */\n+static ssize_t xswap_max_clusters_read(struct file *file, char __user *buf,\n+\t\t\t\t size_t count, loff_t *ppos)\n+{\n+\tstruct swap_info_struct *si = file-\u003eprivate_data;\n+\tchar tmp[32];\n+\tint len;\n+\n+\tlen = snprintf(tmp, sizeof(tmp), \"%lu\\n\", READ_ONCE(si-\u003enr_clusters));\n+\treturn simple_read_from_buffer(buf, count, ppos, tmp, len);\n+}\n+\n+static ssize_t xswap_max_clusters_write(struct file *file,\n+\t\t\t\t\tconst char __user *buf,\n+\t\t\t\t\tsize_t count, loff_t *ppos)\n+{\n+\tstruct swap_info_struct *si = file-\u003eprivate_data;\n+\tunsigned long val, new_pages;\n+\tint err;\n+\n+\terr = kstrtoul_from_user(buf, count, 0, \u0026val);\n+\tif (err)\n+\t\treturn err;\n+\n+\tif (val \u003e si-\u003enr_clusters_max)\n+\t\tval = si-\u003enr_clusters_max;\n+\n+\tspin_lock(\u0026si-\u003elock);\n+\tsi-\u003enr_clusters = val;\n+\tspin_unlock(\u0026si-\u003elock);\n+\n+\tnew_pages = min_t(unsigned long, val * SWAPFILE_CLUSTER, si-\u003emax);\n+\tif (new_pages)\n+\t\tnew_pages--;\n+\tif (new_pages != si-\u003epages) {\n+\t\tlong delta = (long)new_pages - (long)si-\u003epages;\n+\n+\t\tspin_lock(\u0026swap_lock);\n+\t\tsi-\u003epages = new_pages;\n+\t\tatomic_long_add(delta, \u0026nr_swap_pages);\n+\t\ttotal_swap_pages += delta;\n+\t\tspin_unlock(\u0026swap_lock);\n+\t}\n+\n+\t/* Lowering the ceiling may free tail clusters. */\n+\txswap_try_shrink(si);\n+\n+\treturn count;\n+}\n+\n+static const struct file_operations xswap_debugfs_fops = {\n+\t.read = xswap_max_clusters_read,\n+\t.write = xswap_max_clusters_write,\n+\t.open = simple_open,\n+\t.llseek = default_llseek,\n+};\n+\n+static void xswap_debugfs_add(struct swap_info_struct *si)\n+{\n+\tchar name[32];\n+\n+\tif (!xswap_debugfs_root)\n+\t\treturn;\n+\n+\tsnprintf(name, sizeof(name), \"type%d_cluster_limit\", si-\u003etype);\n+\tsi-\u003edebugfs_entry = debugfs_create_file(name, 0644, xswap_debugfs_root,\n+\t\t\t\t\t\tsi, \u0026xswap_debugfs_fops);\n+}\n+\n+static void xswap_debugfs_del(struct swap_info_struct *si)\n+{\n+\tdebugfs_remove(si-\u003edebugfs_entry);\n+\tsi-\u003edebugfs_entry = NULL;\n+}\n+\n+#ifdef CONFIG_SYSFS\n+static int xswap_create(int percent, int prio);\n+static int xswap_destroy(int type);\n+/* /sys/kernel/mm/xswap/: create.\n+ * Write \"\u003cpercent\u003e [\u003cprio\u003e]\" to add a zswap-backed swap device; a missing\n+ * priority means DEF_SWAP_PRIO.\n+ * Per-device runtime size is tuned via debugfs type\u003cN\u003e_cluster_limit.\n+ */\n+\n+static ssize_t xswap_create_store(struct kobject *kobj,\n+\t\t\t\t struct kobj_attribute *attr,\n+\t\t\t\t const char *buf, size_t count)\n+{\n+\tlong percent;\n+\tint prio = DEF_SWAP_PRIO;\n+\tint nr, err;\n+\n+\tif (!capable(CAP_SYS_ADMIN))\n+\t\treturn -EPERM;\n+\n+\t/* \"\u003cpercent\u003e [\u003cprio\u003e]\"; a missing prio means DEF_SWAP_PRIO.\n+\t * %li keeps kstrtoul(buf, 0)'s base-0 behavior for percent.\n+\t */\n+\tnr = sscanf(buf, \"%li %d\", \u0026percent, \u0026prio);\n+\tif (nr \u003c 1)\n+\t\treturn -EINVAL;\n+\n+\t/* 0 means \"use the default percent\" */\n+\tif (percent == 0)\n+\t\tpercent = XSWAP_DEFAULT_CLUSTER_PERCENT;\n+\n+\terr = xswap_create(percent, prio);\n+\tif (err \u003c 0)\n+\t\treturn err;\n+\n+\treturn count;\n+}\n+\n+static struct kobj_attribute xswap_create_attr = __ATTR(create, 0200, NULL,\n+\t\t\t\t\t\t\txswap_create_store);\n+\n+static ssize_t xswap_destroy_store(struct kobject *kobj,\n+\t\t\t\t struct kobj_attribute *attr,\n+\t\t\t\t const char *buf, size_t count)\n+{\n+\tunsigned long type;\n+\tint err;\n+\n+\tif (!capable(CAP_SYS_ADMIN))\n+\t\treturn -EPERM;\n+\n+\terr = kstrtoul(buf, 0, \u0026type);\n+\tif (err)\n+\t\treturn err;\n+\n+\terr = xswap_destroy(type);\n+\tif (err)\n+\t\treturn err;\n+\n+\treturn count;\n+}\n+\n+static struct kobj_attribute xswap_destroy_attr = __ATTR(destroy, 0200, NULL,\n+\t\t\t\t\t\t\t xswap_destroy_store);\n+\n+static struct attribute *xswap_attrs[] = {\n+\t\u0026xswap_create_attr.attr,\n+\t\u0026xswap_destroy_attr.attr,\n+\tNULL,\n+};\n+\n+static const struct attribute_group xswap_attr_group = {\n+\t.attrs = xswap_attrs,\n+};\n+\n+static struct kobject *xswap_kobj;\n+\n+static void xswap_sysfs_init(void)\n+{\n+\txswap_kobj = kobject_create_and_add(\"xswap\", mm_kobj);\n+\tif (!xswap_kobj) {\n+\t\tpr_err(\"xswap: failed to create sysfs kobject\\n\");\n+\t\treturn;\n+\t}\n+\tif (sysfs_create_group(xswap_kobj, \u0026xswap_attr_group))\n+\t\tpr_err(\"xswap: failed to create sysfs group\\n\");\n+}\n+#else\n+static inline void xswap_sysfs_init(void)\n+{\n+}\n+#endif /* CONFIG_SYSFS */\n+#else /* !CONFIG_XSWAP */\n+static inline void xswap_sysfs_init(void)\n+{\n+}\n+#endif /* CONFIG_XSWAP */\n \n static void swap_range_alloc(struct swap_info_struct *si,\n \t\t\t unsigned int nr_entries);\n@@ -63,9 +272,9 @@ static void move_cluster(struct swap_info_struct *si,\n *\n * Also protects swap_active_head total_swap_pages, and the SWP_WRITEOK flag.\n */\n-static DEFINE_SPINLOCK(swap_lock);\n static unsigned int nr_swapfiles;\n atomic_long_t nr_swap_pages;\n+atomic_t nr_real_swapfiles;\n /*\n * Some modules use swappable objects and may try to swap them out under\n * memory pressure (via the shrinker). Before doing so, they may wish to\n@@ -74,7 +283,6 @@ atomic_long_t nr_swap_pages;\n EXPORT_SYMBOL_GPL(nr_swap_pages);\n /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */\n long total_swap_pages;\n-#define DEF_SWAP_PRIO -1\n unsigned long swapfile_maximum_size;\n #ifdef CONFIG_MIGRATION\n bool swap_migration_ad_supported;\n@@ -609,6 +817,10 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info\n \tswap_cluster_free_table(ci);\n \tmove_cluster(si, ci, \u0026si-\u003efree_clusters, CLUSTER_FLAG_FREE);\n \tci-\u003eorder = 0;\n+#ifdef CONFIG_XSWAP\n+\txswap_update_free_tail(si, ci - si-\u003ecluster_info);\n+\tschedule_work(\u0026si-\u003exswap_shrink_work);\n+#endif\n }\n \n /*\n@@ -955,6 +1167,9 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,\n \tif (cluster_is_empty(ci))\n \t\tci-\u003eorder = order;\n \tci-\u003ecount += nr_pages;\n+#ifdef CONFIG_XSWAP\n+\txswap_trim_free_tail(si, cluster_index(si, ci));\n+#endif\n \tswap_range_alloc(si, nr_pages);\n \n \treturn true;\n@@ -1185,6 +1400,51 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,\n \t\tif (found)\n \t\t\tgoto done;\n \t}\n+\n+#ifdef CONFIG_XSWAP\n+\t/*\n+\t * For xswap: if no free cluster was found and more clusters\n+\t * can be mapped, grow the cluster_info array and retry.\n+\t */\n+\tif (!found \u0026\u0026 (si-\u003eflags \u0026 SWP_XSWAP) \u0026\u0026\n+\t READ_ONCE(si-\u003enr_clusters_mapped) \u003c READ_ONCE(si-\u003enr_clusters) \u0026\u0026\n+\t list_empty(\u0026si-\u003efree_clusters)) {\n+\t\tunsigned long nr_new = min(READ_ONCE(si-\u003enr_clusters) -\n+\t\t\t\t\t READ_ONCE(si-\u003enr_clusters_mapped),\n+\t\t\t\t\t XSWAP_GROW_CLUSTERS);\n+\t\tunsigned long start = READ_ONCE(si-\u003enr_clusters_mapped);\n+\t\tunsigned long i;\n+\n+\t\tif (!xswap_map_clusters(si, start, nr_new)) {\n+\t\t\tunsigned long added = 0;\n+\n+\t\t\tfor (i = start; i \u003c start + nr_new; i++) {\n+\t\t\t\tstruct swap_cluster_info *ci = \u0026si-\u003ecluster_info[i];\n+\n+\t\t\t\t/*\n+\t\t\t\t * A concurrent grower may have already added\n+\t\t\t\t * these clusters to the free list. Only add\n+\t\t\t\t * clusters that are still off-list (NONE).\n+\t\t\t\t * Lock ci-\u003elock first: move_cluster() takes\n+\t\t\t\t * si-\u003elock internally.\n+\t\t\t\t */\n+\t\t\t\tspin_lock(\u0026ci-\u003elock);\n+\t\t\t\tif (ci-\u003eflags == CLUSTER_FLAG_NONE) {\n+\t\t\t\t\tmove_cluster(si, ci, \u0026si-\u003efree_clusters,\n+\t\t\t\t\t\t CLUSTER_FLAG_FREE);\n+\t\t\t\t\tadded++;\n+\t\t\t\t}\n+\t\t\t\tspin_unlock(\u0026ci-\u003elock);\n+\t\t\t}\n+\t\t\tWRITE_ONCE(si-\u003enr_free_tail,\n+\t\t\t\t READ_ONCE(si-\u003enr_free_tail) + added);\n+\n+\t\t\t/* Retry allocation from the free list */\n+\t\t\tfound = alloc_swap_scan_list(si, \u0026si-\u003efree_clusters,\n+\t\t\t\t\t\t folio, false);\n+\t\t}\n+\t}\n+#endif\n done:\n \tif (!(si-\u003eflags \u0026 SWP_SOLIDSTATE))\n \t\tspin_unlock(\u0026si-\u003eglobal_cluster_lock);\n@@ -1223,6 +1483,8 @@ static void del_from_avail_list(struct swap_info_struct *si, bool swapoff)\n \t\t\tgoto skip;\n \t}\n \n+\tif (!(si-\u003eflags \u0026 SWP_XSWAP))\n+\t\tatomic_sub(1, \u0026nr_real_swapfiles);\n \tplist_del(\u0026si-\u003eavail_list, \u0026swap_avail_head);\n \n skip:\n@@ -1265,6 +1527,8 @@ static void add_to_avail_list(struct swap_info_struct *si, bool swapon)\n \t}\n \n \tplist_add(\u0026si-\u003eavail_list, \u0026swap_avail_head);\n+\tif (!(si-\u003eflags \u0026 SWP_XSWAP))\n+\t\tatomic_add(1, \u0026nr_real_swapfiles);\n \n skip:\n \tspin_unlock(\u0026swap_avail_lock);\n@@ -2710,15 +2974,27 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,\n \t\t\t\t\tunsigned int prev)\n {\n \tunsigned int i;\n+\tunsigned int end = si-\u003emax;\n \tunsigned long swp_tb;\n \n+#ifdef CONFIG_XSWAP\n+\t/* xswap may have shrunk and unmapped the cluster_info tail. */\n+\tif (si-\u003eflags \u0026 SWP_XSWAP) {\n+\t\tunsigned long mapped_end;\n+\n+\t\tmapped_end = READ_ONCE(si-\u003enr_clusters_mapped) * SWAPFILE_CLUSTER;\n+\t\tif (mapped_end \u003c end)\n+\t\t\tend = mapped_end;\n+\t}\n+#endif\n+\n \t/*\n \t * No need for swap_lock here: we're just looking\n \t * for whether an entry is in use, not modifying it; false\n \t * hits are okay, and sys_swapoff() has already prevented new\n \t * allocations from this area (while holding swap_lock).\n \t */\n-\tfor (i = prev + 1; i \u003c si-\u003emax; i++) {\n+\tfor (i = prev + 1; i \u003c end; i++) {\n \t\tswp_tb = swap_table_get(__swap_offset_to_cluster(si, i),\n \t\t\t\t\ti % SWAPFILE_CLUSTER);\n \t\tif (!swp_tb_is_null(swp_tb) \u0026\u0026 !swp_tb_is_bad(swp_tb))\n@@ -2727,7 +3003,7 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,\n \t\t\tcond_resched();\n \t}\n \n-\tif (i == si-\u003emax)\n+\tif (i == end)\n \t\ti = 0;\n \n \treturn i;\n@@ -2959,6 +3235,19 @@ static int setup_swap_extents(struct swap_info_struct *sis,\n \tstruct inode *inode = mapping-\u003ehost;\n \tint ret;\n \n+\tif (sis-\u003eflags \u0026 SWP_XSWAP) {\n+\t\t*span = 0;\n+\t\t/*\n+\t\t * xswap devices have no backing block device and\n+\t\t * physical writeout is skipped in swap_writeout(),\n+\t\t * but sis-\u003eops must still be set so that callers\n+\t\t * like shrink_folio_list() can safely dereference\n+\t\t * ops-\u003eflags.\n+\t\t */\n+\t\tsis-\u003eops = \u0026swap_bdev_ops;\n+\t\treturn 0;\n+\t}\n+\n \tret = sio_pool_init();\n \tif (ret)\n \t\treturn ret;\n@@ -3030,20 +3319,46 @@ static void wait_for_allocation(struct swap_info_struct *si)\n \n \tBUG_ON(si-\u003eflags \u0026 SWP_WRITEOK);\n \n+#ifdef CONFIG_XSWAP\n+\t/* Skip shrinker-unmapped cluster tail. */\n+\tif (si-\u003eflags \u0026 SWP_XSWAP)\n+\t\tend = min(end, READ_ONCE(si-\u003enr_clusters_mapped) *\n+\t\t\t SWAPFILE_CLUSTER);\n+#endif\n+\n \tfor (offset = 0; offset \u003c end; offset += SWAPFILE_CLUSTER) {\n \t\tci = swap_cluster_lock(si, offset);\n \t\tswap_cluster_unlock(ci);\n \t}\n }\n \n-static void free_swap_cluster_info(struct swap_cluster_info *cluster_info,\n-\t\t\t\t unsigned long maxpages)\n+static void free_swap_cluster_info(struct swap_info_struct *si)\n {\n+\tstruct swap_cluster_info *cluster_info = si-\u003ecluster_info;\n+\tunsigned long maxpages = si-\u003emax;\n \tstruct swap_cluster_info *ci;\n-\tint i, nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);\n+\tint i, nr_clusters;\n \n \tif (!cluster_info)\n \t\treturn;\n+\n+#ifdef CONFIG_XSWAP\n+\tif (si-\u003eflags \u0026 SWP_XSWAP) {\n+\t\txswap_debugfs_del(si);\n+\t\tcancel_work_sync(\u0026si-\u003exswap_shrink_work);\n+\t\t/* Unmap all mapped clusters and free the VM_SPARSE area */\n+\t\tif (si-\u003enr_clusters_mapped \u003e 0)\n+\t\t\txswap_unmap_clusters(si, 0, si-\u003enr_clusters_mapped);\n+\t\tfree_vm_area(si-\u003ecluster_vm);\n+\t\tsi-\u003ecluster_vm = NULL;\n+\t\tsi-\u003enr_clusters_max = 0;\n+\t\tsi-\u003enr_clusters = 0;\n+\t\tsi-\u003enr_clusters_mapped = 0;\n+\t\treturn;\n+\t}\n+#endif\n+\n+\tnr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);\n \tfor (i = 0; i \u003c nr_clusters; i++) {\n \t\tci = cluster_info + i;\n \t\t/* Cluster with bad marks count will have a remaining table */\n@@ -3079,63 +3394,13 @@ static void flush_percpu_swap_cluster(struct swap_info_struct *si)\n }\n \n \n-SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)\n+/* Common swap teardown after list removal; shared by sys_swapoff() and\n+ * xswap_destroy().\n+ */\n+static int __swapoff(struct swap_info_struct *p)\n {\n-\tstruct swap_info_struct *p = NULL;\n-\tstruct swap_cluster_info *cluster_info;\n-\tstruct file *swap_file, *victim;\n-\tstruct address_space *mapping;\n-\tstruct inode *inode;\n-\tunsigned int maxpages;\n-\tint err, found = 0;\n-\n-\tif (!capable(CAP_SYS_ADMIN))\n-\t\treturn -EPERM;\n-\n-\tBUG_ON(!current-\u003emm);\n-\n-\tCLASS(filename, pathname)(specialfile);\n-\tvictim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);\n-\tif (IS_ERR(victim))\n-\t\treturn PTR_ERR(victim);\n-\n-\tmapping = victim-\u003ef_mapping;\n-\tspin_lock(\u0026swap_lock);\n-\tplist_for_each_entry(p, \u0026swap_active_head, list) {\n-\t\tif (p-\u003eflags \u0026 SWP_WRITEOK) {\n-\t\t\tif (p-\u003eswap_file-\u003ef_mapping == mapping) {\n-\t\t\t\tfound = 1;\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t}\n-\t}\n-\tif (!found) {\n-\t\terr = -EINVAL;\n-\t\tspin_unlock(\u0026swap_lock);\n-\t\tgoto out_dput;\n-\t}\n-\n-\t/* Refuse swapoff while the device is pinned for hibernation */\n-\tif (p-\u003eflags \u0026 SWP_HIBERNATION) {\n-\t\terr = -EBUSY;\n-\t\tspin_unlock(\u0026swap_lock);\n-\t\tgoto out_dput;\n-\t}\n-\n-\tif (!security_vm_enough_memory_mm(current-\u003emm, p-\u003epages))\n-\t\tvm_unacct_memory(p-\u003epages);\n-\telse {\n-\t\terr = -ENOMEM;\n-\t\tspin_unlock(\u0026swap_lock);\n-\t\tgoto out_dput;\n-\t}\n-\tspin_lock(\u0026p-\u003elock);\n-\tdel_from_avail_list(p, true);\n-\tplist_del(\u0026p-\u003elist, \u0026swap_active_head);\n-\tatomic_long_sub(p-\u003epages, \u0026nr_swap_pages);\n-\ttotal_swap_pages -= p-\u003epages;\n-\tspin_unlock(\u0026p-\u003elock);\n-\tspin_unlock(\u0026swap_lock);\n+\tstruct file *swap_file = NULL;\n+\tint err;\n \n \twait_for_allocation(p);\n \n@@ -3146,7 +3411,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)\n \tif (err) {\n \t\t/* re-insert swap space back into swap_list */\n \t\treinsert_swap_info(p);\n-\t\tgoto out_dput;\n+\t\treturn err;\n \t}\n \n \t/*\n@@ -3167,7 +3432,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)\n \n \tdestroy_swap_extents(p, p-\u003eswap_file);\n \n-\tif (!(p-\u003eflags \u0026 SWP_SOLIDSTATE))\n+\tif (!(p-\u003eflags \u0026 SWP_XSWAP) \u0026\u0026\n+\t !(p-\u003eflags \u0026 SWP_SOLIDSTATE))\n \t\tatomic_dec(\u0026nr_rotate_swap);\n \n \tmutex_lock(\u0026swapon_mutex);\n@@ -3177,10 +3443,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)\n \n \tswap_file = p-\u003eswap_file;\n \tp-\u003eswap_file = NULL;\n-\tmaxpages = p-\u003emax;\n-\tcluster_info = p-\u003ecluster_info;\n-\tp-\u003emax = 0;\n-\tp-\u003ecluster_info = NULL;\n \tspin_unlock(\u0026p-\u003elock);\n \tspin_unlock(\u0026swap_lock);\n \tarch_swap_invalidate_area(p-\u003etype);\n@@ -3188,14 +3450,18 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)\n \tmutex_unlock(\u0026swapon_mutex);\n \tkfree(p-\u003eglobal_cluster);\n \tp-\u003eglobal_cluster = NULL;\n-\tfree_swap_cluster_info(cluster_info, maxpages);\n+\tfree_swap_cluster_info(p);\n+\tp-\u003emax = 0;\n+\tp-\u003ecluster_info = NULL;\n \n-\tinode = mapping-\u003ehost;\n+\tif (swap_file) {\n+\t\tstruct inode *inode = swap_file-\u003ef_mapping-\u003ehost;\n \n-\tinode_lock(inode);\n-\tinode-\u003ei_flags \u0026= ~S_SWAPFILE;\n-\tinode_unlock(inode);\n-\tfilp_close(swap_file, NULL);\n+\t\tinode_lock(inode);\n+\t\tinode-\u003ei_flags \u0026= ~S_SWAPFILE;\n+\t\tinode_unlock(inode);\n+\t\tfilp_close(swap_file, NULL);\n+\t}\n \n \t/*\n \t * Clear the SWP_USED flag after all resources are freed so that swapon\n@@ -3206,10 +3472,69 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)\n \tp-\u003eflags = 0;\n \tspin_unlock(\u0026swap_lock);\n \n-\terr = 0;\n \tatomic_inc(\u0026proc_poll_event);\n \twake_up_interruptible(\u0026proc_poll_wait);\n \n+\treturn 0;\n+}\n+\n+SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)\n+{\n+\tstruct swap_info_struct *p = NULL;\n+\tstruct file *victim;\n+\tstruct address_space *mapping;\n+\tint err, found = 0;\n+\n+\tif (!capable(CAP_SYS_ADMIN))\n+\t\treturn -EPERM;\n+\n+\tBUG_ON(!current-\u003emm);\n+\n+\tCLASS(filename, pathname)(specialfile);\n+\tvictim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);\n+\tif (IS_ERR(victim))\n+\t\treturn PTR_ERR(victim);\n+\n+\tmapping = victim-\u003ef_mapping;\n+\tspin_lock(\u0026swap_lock);\n+\tplist_for_each_entry(p, \u0026swap_active_head, list) {\n+\t\tif (p-\u003eflags \u0026 SWP_WRITEOK) {\n+\t\t\tif (p-\u003eswap_file \u0026\u0026 p-\u003eswap_file-\u003ef_mapping == mapping) {\n+\t\t\t\tfound = 1;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n+\tif (!found) {\n+\t\terr = -EINVAL;\n+\t\tspin_unlock(\u0026swap_lock);\n+\t\tgoto out_dput;\n+\t}\n+\n+\t/* Refuse swapoff while the device is pinned for hibernation */\n+\tif (p-\u003eflags \u0026 SWP_HIBERNATION) {\n+\t\terr = -EBUSY;\n+\t\tspin_unlock(\u0026swap_lock);\n+\t\tgoto out_dput;\n+\t}\n+\n+\tif (!security_vm_enough_memory_mm(current-\u003emm, p-\u003epages))\n+\t\tvm_unacct_memory(p-\u003epages);\n+\telse {\n+\t\terr = -ENOMEM;\n+\t\tspin_unlock(\u0026swap_lock);\n+\t\tgoto out_dput;\n+\t}\n+\tspin_lock(\u0026p-\u003elock);\n+\tdel_from_avail_list(p, true);\n+\tplist_del(\u0026p-\u003elist, \u0026swap_active_head);\n+\tatomic_long_sub(p-\u003epages, \u0026nr_swap_pages);\n+\ttotal_swap_pages -= p-\u003epages;\n+\tspin_unlock(\u0026p-\u003elock);\n+\tspin_unlock(\u0026swap_lock);\n+\n+\terr = __swapoff(p);\n+\n out_dput:\n \tfilp_close(victim, NULL);\n \treturn err;\n@@ -3243,7 +3568,7 @@ static void *swap_start(struct seq_file *swap, loff_t *pos)\n \t\treturn SEQ_START_TOKEN;\n \n \tfor (type = 0; (si = swap_type_to_info(type)); type++) {\n-\t\tif (!(si-\u003eswap_file))\n+\t\tif (!(si-\u003eswap_file) \u0026\u0026 !(si-\u003eflags \u0026 SWP_XSWAP))\n \t\t\tcontinue;\n \t\tif (!--l)\n \t\t\treturn si;\n@@ -3264,7 +3589,7 @@ static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)\n \n \t++(*pos);\n \tfor (; (si = swap_type_to_info(type)); type++) {\n-\t\tif (!(si-\u003eswap_file))\n+\t\tif (!(si-\u003eswap_file) \u0026\u0026 !(si-\u003eflags \u0026 SWP_XSWAP))\n \t\t\tcontinue;\n \t\treturn si;\n \t}\n@@ -3277,6 +3602,19 @@ static void swap_stop(struct seq_file *swap, void *v)\n \tmutex_unlock(\u0026swapon_mutex);\n }\n \n+static const char *swap_type_str(struct swap_info_struct *si)\n+{\n+\tstruct file *file = si-\u003eswap_file;\n+\n+\tif (si-\u003eflags \u0026 SWP_XSWAP)\n+\t\treturn \"xswap\\t\";\n+\n+\tif (S_ISBLK(file_inode(file)-\u003ei_mode))\n+\t\treturn \"partition\";\n+\n+\treturn \"file\\t\";\n+}\n+\n static int swap_show(struct seq_file *swap, void *v)\n {\n \tstruct swap_info_struct *si = v;\n@@ -3293,11 +3631,17 @@ static int swap_show(struct seq_file *swap, void *v)\n \tinuse = K(swap_usage_in_pages(si));\n \n \tfile = si-\u003eswap_file;\n-\tlen = seq_file_path(swap, file, \" \\t\\n\\\\\");\n+\tif (file)\n+\t\tlen = seq_file_path(swap, file, \" \\t\\n\\\\\");\n+\telse {\n+\t\tchar name[16];\n+\n+\t\tlen = scnprintf(name, sizeof(name), \"xswap%d\", si-\u003etype);\n+\t\tseq_puts(swap, name);\n+\t}\n \tseq_printf(swap, \"%*s%s\\t%lu\\t%s%lu\\t%s%d\\n\",\n \t\t\tlen \u003c 40 ? 40 - len : 1, \" \",\n-\t\t\tS_ISBLK(file_inode(file)-\u003ei_mode) ?\n-\t\t\t\t\"partition\" : \"file\\t\",\n+\t\t\tswap_type_str(si),\n \t\t\tbytes, bytes \u003c 10000000 ? \"\\t\" : \"\",\n \t\t\tinuse, inuse \u003c 10000000 ? \"\\t\" : \"\",\n \t\t\tsi-\u003eprio);\n@@ -3530,6 +3874,326 @@ static unsigned long read_swap_header(struct swap_info_struct *si,\n \treturn maxpages;\n }\n \n+#ifdef CONFIG_XSWAP\n+static int xswap_map_clusters(struct swap_info_struct *si,\n+\t\t\t unsigned long start_idx, unsigned long nr)\n+{\n+\tunsigned long start_addr = (unsigned long)si-\u003ecluster_info +\n+\t\t\t\t (size_t)start_idx * sizeof(struct swap_cluster_info);\n+\tunsigned long end_addr = start_addr + (size_t)nr * sizeof(struct swap_cluster_info);\n+\t/* Round to page boundaries for vm_area_map_pages(). */\n+\tunsigned long vm_start = PAGE_ALIGN(start_addr);\n+\tunsigned long vm_end = PAGE_ALIGN(end_addr);\n+\tunsigned int noreclaim_flags;\n+\tunsigned long npages;\n+\tstruct page **pages;\n+\tunsigned long i;\n+\tint err;\n+\n+\tmutex_lock(\u0026si-\u003exswap_lock);\n+\n+\tif (vm_start \u003e= vm_end) {\n+\t\t/* All requested clusters fall within already-mapped pages. */\n+\t\tfor (i = start_idx; i \u003c start_idx + nr; i++)\n+\t\t\tspin_lock_init(\u0026si-\u003ecluster_info[i].lock);\n+\t\tWRITE_ONCE(si-\u003enr_clusters_mapped, start_idx + nr);\n+\t\tmutex_unlock(\u0026si-\u003exswap_lock);\n+\t\treturn 0;\n+\t}\n+\n+\tnpages = (vm_end - vm_start) \u003e\u003e PAGE_SHIFT;\n+\n+\t/* Prevent recursive reclaim during vmap page table allocation. */\n+\tnoreclaim_flags = memalloc_noreclaim_save();\n+\n+\tpages = kmalloc_array(npages, sizeof(*pages),\n+\t\t\t __GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL);\n+\tif (!pages) {\n+\t\tmemalloc_noreclaim_restore(noreclaim_flags);\n+\t\tmutex_unlock(\u0026si-\u003exswap_lock);\n+\t\treturn -ENOMEM;\n+\t}\n+\n+\tfor (i = 0; i \u003c npages; i++) {\n+\t\t/* __GFP_ZERO: cluster_info pointer fields must start NULL. */\n+\t\tpages[i] = alloc_page(__GFP_HIGH | __GFP_NOMEMALLOC |\n+\t\t\t\t GFP_KERNEL | __GFP_ZERO);\n+\t\tif (!pages[i])\n+\t\t\tgoto fail;\n+\t}\n+\n+\t/* Detect racing grower that already mapped these pages. */\n+\tif (apply_to_existing_page_range(\u0026init_mm, vm_start,\n+\t\t\t\t\t vm_end - vm_start,\n+\t\t\t\t\t xswap_check_mapped, NULL)) {\n+\t\ti = npages;\n+\t\tgoto fail_nounmap;\n+\t}\n+\n+\terr = vm_area_map_pages(si-\u003ecluster_vm, vm_start, vm_end, pages);\n+\tif (err) {\n+\t\t/* -EBUSY: defensive, the page was already mapped. */\n+\t\tif (err == -EBUSY) {\n+\t\t\ti = npages;\n+\t\t\tgoto fail_nounmap;\n+\t\t}\n+\t\ti = npages;\n+\t\tgoto fail;\n+\t}\n+\n+\tkfree(pages);\n+\tmemalloc_noreclaim_restore(noreclaim_flags);\n+\n+\t/* Initialize spinlocks for newly mapped clusters */\n+\tfor (i = start_idx; i \u003c start_idx + nr; i++)\n+\t\tspin_lock_init(\u0026si-\u003ecluster_info[i].lock);\n+\n+\t/*\n+\t * Pairs with READ_ONCE() in shrink/grow paths.\n+\t */\n+\tWRITE_ONCE(si-\u003enr_clusters_mapped, start_idx + nr);\n+\tmutex_unlock(\u0026si-\u003exswap_lock);\n+\treturn 0;\n+\n+fail_nounmap:\n+\t/*\n+\t * The concurrent grower already mapped the range, initialized the\n+\t * cluster spinlocks and advanced nr_clusters_mapped. It may still\n+\t * be holding those locks while adding clusters to the free list, so\n+\t * do not touch them here; just free our unused pages.\n+\t */\n+\twhile (i \u003e 0) {\n+\t\ti--;\n+\t\tif (pages[i])\n+\t\t\t__free_page(pages[i]);\n+\t}\n+\tkfree(pages);\n+\tmemalloc_noreclaim_restore(noreclaim_flags);\n+\tmutex_unlock(\u0026si-\u003exswap_lock);\n+\treturn 0;\n+\n+fail:\n+\twhile (i \u003e 0) {\n+\t\ti--;\n+\t\tif (pages[i])\n+\t\t\t__free_page(pages[i]);\n+\t}\n+\tmemalloc_noreclaim_restore(noreclaim_flags);\n+\tkfree(pages);\n+\tmutex_unlock(\u0026si-\u003exswap_lock);\n+\treturn -ENOMEM;\n+}\n+\n+struct xswap_page_data {\n+\tstruct page **pages;\n+\tint nr;\n+\tint max;\n+};\n+\n+static int xswap_collect_page(pte_t *pte, unsigned long addr, void *data)\n+{\n+\tstruct xswap_page_data *xpd = data;\n+\n+\tif (!pte_present(*pte))\n+\t\treturn 0;\n+\tif (xpd-\u003enr \u003c xpd-\u003emax)\n+\t\txpd-\u003epages[xpd-\u003enr++] = pte_page(*pte);\n+\treturn 0;\n+}\n+\n+static void xswap_unmap_clusters(struct swap_info_struct *si,\n+\t\t\t\t unsigned long start_idx, unsigned long nr)\n+{\n+\tunsigned long start_addr = (unsigned long)si-\u003ecluster_info +\n+\t\t\t\t (size_t)start_idx * sizeof(struct swap_cluster_info);\n+\tunsigned long end_addr = start_addr + (size_t)nr * sizeof(struct swap_cluster_info);\n+\t/* Round to page boundaries for vm_area_unmap_pages(). */\n+\tunsigned long vm_start = PAGE_ALIGN(start_addr);\n+\tunsigned long vm_end = PAGE_ALIGN(end_addr);\n+\tunsigned long size;\n+\tunsigned long npages;\n+\tstruct xswap_page_data xpd;\n+\tint i;\n+\n+\tmutex_lock(\u0026si-\u003exswap_lock);\n+\n+\tif (vm_start \u003e= vm_end) {\n+\t\tWRITE_ONCE(si-\u003enr_clusters_mapped, start_idx);\n+\t\tmutex_unlock(\u0026si-\u003exswap_lock);\n+\t\treturn;\n+\t}\n+\n+\tsize = vm_end - vm_start;\n+\tnpages = size \u003e\u003e PAGE_SHIFT;\n+\n+\txpd.pages = kmalloc_array(npages, sizeof(*xpd.pages), GFP_KERNEL);\n+\tif (xpd.pages) {\n+\t\txpd.nr = 0;\n+\t\txpd.max = npages;\n+\t\tapply_to_existing_page_range(\u0026init_mm, vm_start, size,\n+\t\t\t\t\t xswap_collect_page, \u0026xpd);\n+\t}\n+\n+\tvm_area_unmap_pages(si-\u003ecluster_vm, vm_start, vm_end);\n+\n+\t/* Free the collected backing pages */\n+\tif (xpd.pages) {\n+\t\tfor (i = 0; i \u003c xpd.nr; i++)\n+\t\t\t__free_page(xpd.pages[i]);\n+\t\tkfree(xpd.pages);\n+\t}\n+\n+\t/* Pairs with READ_ONCE() in shrink/grow paths. */\n+\tWRITE_ONCE(si-\u003enr_clusters_mapped, start_idx);\n+\tmutex_unlock(\u0026si-\u003exswap_lock);\n+}\n+\n+/* Return 1 at first present PTE to signal range is already mapped. */\n+static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data)\n+{\n+\treturn 1;\n+}\n+\n+/*\n+ * Maintain si-\u003enr_free_tail, the number of contiguous free clusters at\n+ * the tail of the mapped range. Called when a cluster at @freed_idx is\n+ * freed. Provides O(1) shrink detection: if nr_free_tail is non-zero,\n+ * the tail can be unmapped without scanning cluster_info[].\n+ *\n+ * Only increments when @freed_idx is the cluster immediately before the\n+ * existing tail region. Then scans backwards for already-free clusters\n+ * now connected to the tail, bounded by XSWAP_GROW_CLUSTERS at a time.\n+ */\n+static void xswap_update_free_tail(struct swap_info_struct *si,\n+\t\t\t\t unsigned long freed_idx)\n+{\n+\tunsigned long nr_mapped, nr_tail, tid, i;\n+\tstruct swap_cluster_info *ci;\n+\n+\tif (!(si-\u003eflags \u0026 SWP_XSWAP))\n+\t\treturn;\n+\n+\tnr_mapped = READ_ONCE(si-\u003enr_clusters_mapped);\n+\tnr_tail = READ_ONCE(si-\u003enr_free_tail);\n+\n+\t/* Protect against concurrent shrink that races past us */\n+\tif (nr_tail \u003e= nr_mapped)\n+\t\treturn;\n+\n+\ttid = nr_mapped - nr_tail - 1;\n+\n+\t/* Only the cluster immediately before the tail region counts */\n+\tif (freed_idx != tid)\n+\t\treturn;\n+\n+\tnr_tail++;\n+\tWRITE_ONCE(si-\u003enr_free_tail, nr_tail);\n+\n+\t/* Extend: include already-free clusters now connected to the tail */\n+\tfor (i = 1; i \u003c XSWAP_GROW_CLUSTERS; i++) {\n+\t\tnr_mapped = READ_ONCE(si-\u003enr_clusters_mapped);\n+\t\tnr_tail = READ_ONCE(si-\u003enr_free_tail);\n+\t\tif (nr_tail \u003e= nr_mapped - 1)\n+\t\t\tbreak; /* reached cluster 0 */\n+\t\ttid = nr_mapped - nr_tail - 1;\n+\t\tci = \u0026si-\u003ecluster_info[tid];\n+\n+\t\tif (READ_ONCE(ci-\u003ecount) ||\n+\t\t READ_ONCE(ci-\u003eflags) != CLUSTER_FLAG_FREE)\n+\t\t\tbreak;\n+\t\tnr_tail++;\n+\t\tWRITE_ONCE(si-\u003enr_free_tail, nr_tail);\n+\t}\n+}\n+\n+/*\n+ * Trim si-\u003enr_free_tail when a cluster in the tail region is allocated.\n+ * @idx: index of the cluster being allocated.\n+ */\n+static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx)\n+{\n+\tunsigned long nr_mapped, nr_tail, tail_start;\n+\n+\tif (!(si-\u003eflags \u0026 SWP_XSWAP))\n+\t\treturn;\n+\n+\t/*\n+\t * nr_clusters_mapped and nr_free_tail are read locklessly;\n+\t * concurrent updates may cause nr_free_tail to be trimmed\n+\t * slightly less than ideally, which is harmless.\n+\t */\n+\tnr_mapped = READ_ONCE(si-\u003enr_clusters_mapped);\n+\tnr_tail = READ_ONCE(si-\u003enr_free_tail);\n+\ttail_start = nr_mapped - nr_tail;\n+\tif (idx \u003e= tail_start)\n+\t\tWRITE_ONCE(si-\u003enr_free_tail, nr_mapped - idx - 1);\n+}\n+\n+static void xswap_shrink_work_fn(struct work_struct *work)\n+{\n+\tstruct swap_info_struct *si = container_of(work,\n+\t\t\tstruct swap_info_struct, xswap_shrink_work);\n+\txswap_try_shrink(si);\n+}\n+\n+/*\n+ * Try to shrink the cluster_info tail. Uses si-\u003enr_free_tail which\n+ * is maintained incrementally during alloc/free — no scanning needed.\n+ */\n+static void xswap_try_shrink(struct swap_info_struct *si)\n+{\n+\tunsigned long nr_mapped, nr_ceiling, nr_tail, nr_unmap;\n+\tunsigned long start_idx, i;\n+\tstruct swap_cluster_info *ci;\n+\n+\tif (!(si-\u003eflags \u0026 SWP_XSWAP))\n+\t\treturn;\n+\n+\tnr_mapped = READ_ONCE(si-\u003enr_clusters_mapped);\n+\tnr_ceiling = READ_ONCE(si-\u003enr_clusters);\n+\tnr_tail = READ_ONCE(si-\u003enr_free_tail);\n+\n+\tif (nr_mapped \u003c= nr_ceiling)\n+\t\treturn;\n+\tif (nr_tail \u003c XSWAP_GROW_CLUSTERS)\n+\t\treturn;\n+\n+\tnr_unmap = min(round_down(nr_tail, XSWAP_GROW_CLUSTERS),\n+\t\t nr_mapped - nr_ceiling);\n+\tif (nr_unmap \u003c XSWAP_GROW_CLUSTERS)\n+\t\treturn;\n+\tstart_idx = nr_mapped - nr_unmap;\n+\n+\t/*\n+\t * Verify the tail clusters are still free before unmapping. Count the\n+\t * contiguous free run first, then detach it in a second pass once the\n+\t * whole run is confirmed long enough. Detaching while counting would\n+\t * leave a partial run off the free list if it proved too short to unmap.\n+\t */\n+\tspin_lock(\u0026si-\u003elock);\n+\tfor (i = start_idx; i \u003c nr_mapped; i++) {\n+\t\tci = \u0026si-\u003ecluster_info[i];\n+\t\tif (ci-\u003eflags != CLUSTER_FLAG_FREE)\n+\t\t\tbreak;\n+\t}\n+\tnr_unmap = i - start_idx;\n+\tif (nr_unmap \u003c XSWAP_GROW_CLUSTERS) {\n+\t\tspin_unlock(\u0026si-\u003elock);\n+\t\treturn;\n+\t}\n+\n+\tfor (i = start_idx; i \u003c start_idx + nr_unmap; i++) {\n+\t\tci = \u0026si-\u003ecluster_info[i];\n+\t\tlist_del_init(\u0026ci-\u003elist);\n+\t\tci-\u003eflags = CLUSTER_FLAG_NONE;\n+\t}\n+\tspin_unlock(\u0026si-\u003elock);\n+\n+\txswap_unmap_clusters(si, start_idx, nr_unmap);\n+\tWRITE_ONCE(si-\u003enr_free_tail, nr_tail - nr_unmap);\n+}\n+#endif /* CONFIG_XSWAP */\n+\n static int setup_swap_clusters_info(struct swap_info_struct *si,\n \t\t\t\t union swap_header *swap_header,\n \t\t\t\t unsigned long maxpages)\n@@ -3539,10 +4203,78 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,\n \tint err = -ENOMEM;\n \tunsigned long i;\n \n+#ifdef CONFIG_XSWAP\n+\tif (si-\u003eflags \u0026 SWP_XSWAP) {\n+\t\tunsigned long size = PAGE_ALIGN(nr_clusters * sizeof(*cluster_info));\n+\t\tstruct vm_struct *vm;\n+\n+\t\tvm = get_vm_area(size, VM_SPARSE);\n+\t\tif (!vm)\n+\t\t\tgoto err;\n+\n+\t\tcluster_info = vm-\u003eaddr;\n+\t\tsi-\u003ecluster_vm = vm;\n+\t\tsi-\u003enr_clusters_max = nr_clusters;\n+\t\tsi-\u003enr_clusters = nr_clusters;\n+\t\tsi-\u003ecluster_info = cluster_info;\n+\n+\t\t/* Must be initialized before xswap_map_clusters() locks it. */\n+\t\tmutex_init(\u0026si-\u003exswap_lock);\n+\n+\t\t/* Map the initial chunk (at least cluster 0) */\n+\t\tif (xswap_map_clusters(si, 0, min_t(unsigned long,\n+\t\t\t\t\tXSWAP_GROW_CLUSTERS, nr_clusters)))\n+\t\t\tgoto err_free_vm;\n+\n+\t\t/* xswap: only cluster 0 slot 0 is bad */\n+\t\terr = swap_cluster_setup_bad_slot(si, cluster_info, 0, false);\n+\t\tif (err)\n+\t\t\tgoto err_unmap;\n+\n+\t\tINIT_LIST_HEAD(\u0026si-\u003efree_clusters);\n+\t\tINIT_LIST_HEAD(\u0026si-\u003efull_clusters);\n+\t\tINIT_LIST_HEAD(\u0026si-\u003ediscard_clusters);\n+\t\tfor (i = 0; i \u003c SWAP_NR_ORDERS; i++) {\n+\t\t\tINIT_LIST_HEAD(\u0026si-\u003enonfull_clusters[i]);\n+\t\t\tINIT_LIST_HEAD(\u0026si-\u003efrag_clusters[i]);\n+\t\t}\n+\n+\t\t/* Mark mapped clusters: cluster 0 has 1 bad slot, rest free */\n+\t\tfor (i = 0; i \u003c si-\u003enr_clusters_mapped; i++) {\n+\t\t\tstruct swap_cluster_info *ci = \u0026cluster_info[i];\n+\n+\t\t\tif (i == 0) {\n+\t\t\t\tci-\u003eflags = CLUSTER_FLAG_NONFULL;\n+\t\t\t\tlist_add_tail(\u0026ci-\u003elist, \u0026si-\u003enonfull_clusters[0]);\n+\t\t\t} else {\n+\t\t\t\tci-\u003eflags = CLUSTER_FLAG_FREE;\n+\t\t\t\tlist_add_tail(\u0026ci-\u003elist, \u0026si-\u003efree_clusters);\n+\t\t\t}\n+\t\t}\n+\n+\t\t/* All mapped clusters except cluster 0 are free at the tail */\n+\t\tsi-\u003enr_free_tail = si-\u003enr_clusters_mapped - 1;\n+\n+\t\tINIT_WORK(\u0026si-\u003exswap_shrink_work, xswap_shrink_work_fn);\n+\t\txswap_debugfs_add(si);\n+\t\treturn 0;\n+\n+err_unmap:\n+\t\txswap_unmap_clusters(si, 0, si-\u003enr_clusters_mapped);\n+err_free_vm:\n+\t\tfree_vm_area(si-\u003ecluster_vm);\n+\t\tsi-\u003ecluster_vm = NULL;\n+\t\tsi-\u003ecluster_info = NULL;\n+\t\treturn err;\n+\t}\n+#endif /* CONFIG_XSWAP */\n+\n \tcluster_info = kvzalloc_objs(*cluster_info, nr_clusters);\n \tif (!cluster_info)\n \t\tgoto err;\n \n+\tsi-\u003ecluster_info = cluster_info;\n+\n \tfor (i = 0; i \u003c nr_clusters; i++)\n \t\tspin_lock_init(\u0026cluster_info[i].lock);\n \n@@ -3606,10 +4338,132 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,\n \tsi-\u003ecluster_info = cluster_info;\n \treturn 0;\n err:\n-\tfree_swap_cluster_info(cluster_info, maxpages);\n+\tfree_swap_cluster_info(si);\n \treturn err;\n }\n \n+#ifdef CONFIG_XSWAP\n+#ifdef CONFIG_SYSFS\n+/* Create a file-less xswap device. si-\u003emax = full RAM; @percent sets the\n+ * runtime nr_clusters ceiling.\n+ */\n+static int xswap_create(int percent, int prio)\n+{\n+\tstruct swap_info_struct *si;\n+\tunsigned long ram, maxpages, init_clusters;\n+\tint error;\n+\n+\tif (percent \u003c 1 || percent \u003e 100)\n+\t\treturn -EINVAL;\n+\tif (prio != DEF_SWAP_PRIO \u0026\u0026 (prio \u003c 0 || prio \u003e SWAP_FLAG_PRIO_MASK))\n+\t\treturn -EINVAL;\n+\n+\t/* xswap has no backing store, it relies on zswap. */\n+\tif (!zswap_is_enabled())\n+\t\treturn -EOPNOTSUPP;\n+\n+\tsi = alloc_swap_info();\n+\tif (IS_ERR(si))\n+\t\treturn PTR_ERR(si);\n+\n+\tINIT_WORK(\u0026si-\u003ediscard_work, swap_discard_work);\n+\tINIT_WORK(\u0026si-\u003ereclaim_work, swap_reclaim_work);\n+\n+\tram = totalram_pages();\n+\tmaxpages = min_t(unsigned long, ram, swapfile_maximum_size);\n+\tif ((unsigned int)maxpages == 0)\n+\t\tmaxpages = UINT_MAX;\n+\tif (maxpages \u003c 2)\n+\t\tmaxpages = 2;\n+\n+\tsi-\u003ebdev = NULL;\n+\tsi-\u003eflags |= SWP_XSWAP | SWP_SOLIDSTATE;\n+\tsi-\u003emax = maxpages;\n+\tsi-\u003epages = maxpages - 1;\n+\t/* no backing file: mirror the xswap branch of setup_swap_extents() */\n+\tsi-\u003eops = \u0026swap_bdev_ops;\n+\n+\terror = setup_swap_clusters_info(si, NULL, maxpages);\n+\tif (error)\n+\t\tgoto bad_swap;\n+\n+\t/* VM_SPARSE covers full RAM; runtime nr_clusters starts at percent. */\n+\tinit_clusters = div_u64((u64)maxpages * percent, 100 * SWAPFILE_CLUSTER);\n+\tinit_clusters = max_t(unsigned long, init_clusters, XSWAP_GROW_CLUSTERS);\n+\tif (init_clusters \u003e si-\u003enr_clusters_max)\n+\t\tinit_clusters = si-\u003enr_clusters_max;\n+\tsi-\u003enr_clusters = init_clusters;\n+\tsi-\u003epages = min_t(unsigned long, init_clusters * SWAPFILE_CLUSTER, si-\u003emax) - 1;\n+\n+\terror = zswap_swapon(si-\u003etype, si-\u003emax);\n+\tif (error)\n+\t\tgoto bad_swap;\n+\n+\tmutex_lock(\u0026swapon_mutex);\n+\tsi-\u003eprio = prio;\n+\tsi-\u003elist.prio = -si-\u003eprio;\n+\tsi-\u003eavail_list.prio = -si-\u003eprio;\n+\t/* si-\u003eswap_file stays NULL: this is a file-less device */\n+\tenable_swap_info(si);\n+\tmutex_unlock(\u0026swapon_mutex);\n+\n+\tpr_info(\"xswap: adding extendable swap type %d (prio %d, %d%% of %lu pages = %u pages, max %lu)\\n\",\n+\t\tsi-\u003etype, prio, percent, ram, si-\u003epages, maxpages);\n+\tatomic_inc(\u0026proc_poll_event);\n+\twake_up_interruptible(\u0026proc_poll_wait);\n+\n+\treturn si-\u003etype;\n+\n+bad_swap:\n+\tkfree(si-\u003eglobal_cluster);\n+\tsi-\u003eglobal_cluster = NULL;\n+\tdestroy_swap_extents(si, NULL);\t/* safe: xswap never sets SWP_ACTIVATED */\n+\tfree_swap_cluster_info(si);\n+\tsi-\u003ecluster_info = NULL;\n+\tspin_lock(\u0026swap_lock);\n+\tsi-\u003eflags = 0;\n+\tspin_unlock(\u0026swap_lock);\n+\treturn error;\n+}\n+\n+/* Tear down a file-less xswap device by its swap type. */\n+static int xswap_destroy(int type)\n+{\n+\tstruct swap_info_struct *p;\n+\n+\tp = swap_type_to_info(type);\n+\tif (!p)\n+\t\treturn -EINVAL;\n+\n+\tspin_lock(\u0026swap_lock);\n+\tif (!(p-\u003eflags \u0026 SWP_WRITEOK) || !(p-\u003eflags \u0026 SWP_XSWAP)) {\n+\t\tspin_unlock(\u0026swap_lock);\n+\t\treturn -EINVAL;\n+\t}\n+\t/* Refuse swapoff while the device is pinned for hibernation */\n+\tif (p-\u003eflags \u0026 SWP_HIBERNATION) {\n+\t\tspin_unlock(\u0026swap_lock);\n+\t\treturn -EBUSY;\n+\t}\n+\tif (!security_vm_enough_memory_mm(current-\u003emm, p-\u003epages))\n+\t\tvm_unacct_memory(p-\u003epages);\n+\telse {\n+\t\tspin_unlock(\u0026swap_lock);\n+\t\treturn -ENOMEM;\n+\t}\n+\tspin_lock(\u0026p-\u003elock);\n+\tdel_from_avail_list(p, true);\n+\tplist_del(\u0026p-\u003elist, \u0026swap_active_head);\n+\tatomic_long_sub(p-\u003epages, \u0026nr_swap_pages);\n+\ttotal_swap_pages -= p-\u003epages;\n+\tspin_unlock(\u0026p-\u003elock);\n+\tspin_unlock(\u0026swap_lock);\n+\n+\treturn __swapoff(p);\n+}\n+#endif /* CONFIG_SYSFS */\n+#endif /* CONFIG_XSWAP */\n+\n SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)\n {\n \tstruct swap_info_struct *si;\n@@ -3825,7 +4679,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)\n \tsi-\u003eglobal_cluster = NULL;\n \tinode = NULL;\n \tdestroy_swap_extents(si, swap_file);\n-\tfree_swap_cluster_info(si-\u003ecluster_info, si-\u003emax);\n+\tfree_swap_cluster_info(si);\n \tsi-\u003ecluster_info = NULL;\n \t/*\n \t * Clear the SWP_USED flag after all resources are freed so\n@@ -3937,7 +4791,6 @@ void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)\n static int __init swapfile_init(void)\n {\n \tswapfile_maximum_size = arch_max_swapfile_size();\n-\n \t/*\n \t * Once a cluster is freed, it's swap table content is read\n \t * only, and all swap cache readers (swap_cache_*) verifies\n@@ -3947,12 +4800,16 @@ static int __init swapfile_init(void)\n \t\tswap_table_cachep = kmem_cache_create(\"swap_table\",\n \t\t\t\t sizeof(struct swap_table),\n \t\t\t\t 0, SLAB_PANIC | SLAB_TYPESAFE_BY_RCU, NULL);\n-\n #ifdef CONFIG_MIGRATION\n \tif (swapfile_maximum_size \u003e= (1UL \u003c\u003c SWP_MIG_TOTAL_BITS))\n \t\tswap_migration_ad_supported = true;\n #endif\t/* CONFIG_MIGRATION */\n \n+#ifdef CONFIG_XSWAP\n+\txswap_debugfs_root = debugfs_create_dir(\"xswap\", NULL);\n+#endif\n+\txswap_sysfs_init();\n+\n \treturn 0;\n }\n subsys_initcall(swapfile_init);\ndiff --git a/mm/zswap.c b/mm/zswap.c\nindex 37f34e406c8e3..064970a4393f5 100644\n--- a/mm/zswap.c\n+++ b/mm/zswap.c\n@@ -998,7 +998,12 @@ static int zswap_writeback_entry(struct zswap_entry *entry,\n \t/* try to allocate swap cache folio */\n \tsi = get_swap_device(swpentry);\n \tif (!si)\n-\t\treturn -EEXIST;\n+\t\treturn -ENOENT;\n+\n+\tif (si-\u003eflags \u0026 SWP_XSWAP) {\n+\t\tput_swap_device(si);\n+\t\treturn -EINVAL;\n+\t}\n \n \tmpol = get_task_policy(current);\n \tfolio = swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,\n@@ -1545,7 +1550,7 @@ bool zswap_store(struct folio *folio)\n \tzswap_pool_put(pool);\n put_objcg:\n \tobj_cgroup_put(objcg);\n-\tif (!ret \u0026\u0026 zswap_pool_reached_full)\n+\tif (!ret \u0026\u0026 zswap_pool_reached_full \u0026\u0026 atomic_read(\u0026nr_real_swapfiles))\n \t\tqueue_work(shrink_wq, \u0026zswap_shrink_work);\n check_old:\n \t/*\n"
} |
| 2/1 | 2026/08/27 09:59 | agent | patch-evaluator |
1mModel:best-expensive Args: null Results: {
"EnableConfigs": [
"XSWAP",
"ZSWAP",
"ZSWAP_DEFAULT_ON",
"SYSFS"
],
"FocusSymbols": [
"xswap_create",
"xswap_destroy",
"xswap_map_clusters",
"xswap_unmap_clusters",
"xswap_try_shrink",
"xswap_update_free_tail",
"xswap_trim_free_tail",
"xswap_create_store",
"xswap_destroy_store",
"xswap_max_clusters_write"
],
"Reasoning": "The patch introduces a new extendable swap device (xswap) that decouples PTE swap entries from physical backing storage. It dynamically grows and shrinks the cluster_info array via a VM_SPARSE area. The feature is exposed to userspace via sysfs (`/sys/kernel/mm/xswap/create` and `destroy`) and debugfs for runtime size tuning. This is a significant functional change to the core memory management and swap subsystems, and it is reachable from userspace, making it highly worth fuzzing.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). - Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed, ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU. - Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove) that are executed only during PCI hot-unplug or sysfs driver unbind operations. If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. Prefer non-static, non-inlined API entrypoint functions over internal static helper functions (which are inlined by the compiler and do not have distinct symbol addresses). 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4"). Prefer calling several tools at the same time to save round-trips. Use set-results tool to provide results of the analysis. It must be called exactly once before the final reply. Ignore results of this tool. Prompt: For your convenience, here is the diff of the changes:
commit 886eb2bf972c2bb8fe0af9acf11f1de148f1d3e8
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Aug 27 09:59:55 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 5658a1634b85e..998f04c0b7674 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -207,6 +207,7 @@ enum {
SWP_STABLE_WRITES = (1 << 11), /* no overwrite PG_writeback pages */
SWP_SYNCHRONOUS_IO = (1 << 12), /* synchronous IO is efficient */
SWP_HIBERNATION = (1 << 13), /* pinned for hibernation */
+ SWP_XSWAP = (1 << 14), /* extendable swap device */
/* add others here before... */
};
@@ -247,6 +248,16 @@ struct swap_info_struct {
signed char type; /* strange name for an index */
unsigned int max; /* size of this swap device */
struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */
+#ifdef CONFIG_XSWAP
+ struct vm_struct *cluster_vm; /* VM_SPARSE area for xswap dynamic cluster_info */
+ unsigned long nr_clusters_max;/* upper limit from swap header */
+ unsigned long nr_clusters; /* current growth ceiling (≤ nr_clusters_max) */
+ unsigned long nr_clusters_mapped; /* currently mapped cluster count */
+ unsigned long nr_free_tail; /* contiguous free clusters at tail */
+ struct dentry *debugfs_entry; /* debugfs: type<N>_max_clusters */
+ struct work_struct xswap_shrink_work; /* deferred shrink trigger */
+ struct mutex xswap_lock; /* serialize map/unmap operations */
+#endif
struct list_head free_clusters; /* free clusters list */
struct list_head full_clusters; /* full clusters list */
struct list_head nonfull_clusters[SWAP_NR_ORDERS];
@@ -356,6 +367,7 @@ void free_folio_and_swap_cache(struct folio *folio);
void free_pages_and_swap_cache(struct encoded_page **, int);
/* linux/mm/swapfile.c */
extern atomic_long_t nr_swap_pages;
+extern atomic_t nr_real_swapfiles;
extern long total_swap_pages;
extern atomic_t nr_rotate_swap;
diff --git a/mm/Kconfig b/mm/Kconfig
index 604c58199acbf..26cf26a2c59fd 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -122,6 +122,15 @@ config ZSWAP_COMPRESSOR_DEFAULT
default "zstd" if ZSWAP_COMPRESSOR_DEFAULT_ZSTD
default ""
+config XSWAP
+ bool "Extendable (virtual) swap device"
+ depends on SWAP && 64BIT
+ help
+ Adds support for extendable swap devices (xswap) that decouple
+ PTE swap entries from physical backing storage. The cluster_info
+ array is backed by a sparse vmalloc area that grows and shrinks
+ on demand, avoiding static pre-allocation overhead.
+
config ZSMALLOC
tristate
diff --git a/mm/page_io.c b/mm/page_io.c
index 88962571cb931..5483c943e3e35 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -248,6 +248,17 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
}
rcu_read_unlock();
+ /*
+ * ctx->sis is set by swap_add_folio() which is called from
+ * __swap_writepage() below. Since we must avoid the writepage
+ * path for xswap devices, use the swap_info from the folio's
+ * swap entry directly instead of going through ctx.
+ */
+ if (unlikely(__swap_entry_to_info(folio->swap)->flags & SWP_XSWAP)) {
+ folio_mark_dirty(folio);
+ return AOP_WRITEPAGE_ACTIVATE;
+ }
+
__swap_writepage(ctx, folio);
return 0;
out_unlock:
@@ -480,6 +491,11 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
if (zswap_load(folio) != -ENOENT)
goto finish;
+ if (unlikely(sis->flags & SWP_XSWAP)) {
+ folio_unlock(folio);
+ goto finish;
+ }
+
/* We have to read from slower devices. Increase zswap protection. */
zswap_folio_swapin(folio);
swap_add_folio(ctx, folio, READ);
diff --git a/mm/swap_state.c b/mm/swap_state.c
index b76eb3d876fd7..2eedb7a3d7bb2 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -830,6 +830,13 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
struct blk_plug plug;
swp_entry_t ra_entry;
+ /*
+ * The entry may have been freed by another task. Avoid swap_info_get()
+ * which will print error message if the race happens.
+ */
+ if (si->flags & SWP_XSWAP)
+ goto skip;
+
mask = swapin_nr_pages(offset) - 1;
if (!mask)
goto skip;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 53bf01d5f7f11..258b52bce4389 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -16,6 +16,8 @@
#include <linux/kernel_stat.h>
#include <linux/swap.h>
#include <linux/vmalloc.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
#include <linux/pagemap.h>
#include <linux/namei.h>
#include <linux/shmem_fs.h>
@@ -48,6 +50,213 @@
#include "swap_table.h"
#include "internal.h"
#include "swap.h"
+#include <linux/debugfs.h>
+
+#define DEF_SWAP_PRIO -1
+
+static DEFINE_SPINLOCK(swap_lock);
+
+#ifdef CONFIG_XSWAP
+/*
+ * xswap: dynamically grow and shrink the cluster_info array via a
+ * VM_SPARSE area.
+ *
+ * XSWAP_GROW_CLUSTERS is the number of clusters to map/unmap in one
+ * grow/shrink operation. It is set to the number of cluster_info
+ * structs that fit in a single page (at least 16), so that the vmalloc
+ * page table
+ * overhead is proportional to the number of clusters mapped.
+ */
+#define XSWAP_GROW_CLUSTERS \
+ max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16)
+#define XSWAP_DEFAULT_CLUSTER_PERCENT 30
+
+static struct dentry *xswap_debugfs_root;
+
+static int xswap_map_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr);
+static void xswap_unmap_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr);
+static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data);
+static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx);
+static void xswap_update_free_tail(struct swap_info_struct *si,
+ unsigned long freed_idx);
+static void xswap_try_shrink(struct swap_info_struct *si);
+
+/*
+ * debugfs read/write for per-device max cluster count.
+ */
+static ssize_t xswap_max_clusters_read(struct file *file, char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ struct swap_info_struct *si = file->private_data;
+ char tmp[32];
+ int len;
+
+ len = snprintf(tmp, sizeof(tmp), "%lu\n", READ_ONCE(si->nr_clusters));
+ return simple_read_from_buffer(buf, count, ppos, tmp, len);
+}
+
+static ssize_t xswap_max_clusters_write(struct file *file,
+ const char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ struct swap_info_struct *si = file->private_data;
+ unsigned long val, new_pages;
+ int err;
+
+ err = kstrtoul_from_user(buf, count, 0, &val);
+ if (err)
+ return err;
+
+ if (val > si->nr_clusters_max)
+ val = si->nr_clusters_max;
+
+ spin_lock(&si->lock);
+ si->nr_clusters = val;
+ spin_unlock(&si->lock);
+
+ new_pages = min_t(unsigned long, val * SWAPFILE_CLUSTER, si->max);
+ if (new_pages)
+ new_pages--;
+ if (new_pages != si->pages) {
+ long delta = (long)new_pages - (long)si->pages;
+
+ spin_lock(&swap_lock);
+ si->pages = new_pages;
+ atomic_long_add(delta, &nr_swap_pages);
+ total_swap_pages += delta;
+ spin_unlock(&swap_lock);
+ }
+
+ /* Lowering the ceiling may free tail clusters. */
+ xswap_try_shrink(si);
+
+ return count;
+}
+
+static const struct file_operations xswap_debugfs_fops = {
+ .read = xswap_max_clusters_read,
+ .write = xswap_max_clusters_write,
+ .open = simple_open,
+ .llseek = default_llseek,
+};
+
+static void xswap_debugfs_add(struct swap_info_struct *si)
+{
+ char name[32];
+
+ if (!xswap_debugfs_root)
+ return;
+
+ snprintf(name, sizeof(name), "type%d_cluster_limit", si->type);
+ si->debugfs_entry = debugfs_create_file(name, 0644, xswap_debugfs_root,
+ si, &xswap_debugfs_fops);
+}
+
+static void xswap_debugfs_del(struct swap_info_struct *si)
+{
+ debugfs_remove(si->debugfs_entry);
+ si->debugfs_entry = NULL;
+}
+
+#ifdef CONFIG_SYSFS
+static int xswap_create(int percent, int prio);
+static int xswap_destroy(int type);
+/* /sys/kernel/mm/xswap/: create.
+ * Write "<percent> [<prio>]" to add a zswap-backed swap device; a missing
+ * priority means DEF_SWAP_PRIO.
+ * Per-device runtime size is tuned via debugfs type<N>_cluster_limit.
+ */
+
+static ssize_t xswap_create_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ long percent;
+ int prio = DEF_SWAP_PRIO;
+ int nr, err;
+
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ /* "<percent> [<prio>]"; a missing prio means DEF_SWAP_PRIO.
+ * %li keeps kstrtoul(buf, 0)'s base-0 behavior for percent.
+ */
+ nr = sscanf(buf, "%li %d", &percent, &prio);
+ if (nr < 1)
+ return -EINVAL;
+
+ /* 0 means "use the default percent" */
+ if (percent == 0)
+ percent = XSWAP_DEFAULT_CLUSTER_PERCENT;
+
+ err = xswap_create(percent, prio);
+ if (err < 0)
+ return err;
+
+ return count;
+}
+
+static struct kobj_attribute xswap_create_attr = __ATTR(create, 0200, NULL,
+ xswap_create_store);
+
+static ssize_t xswap_destroy_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ unsigned long type;
+ int err;
+
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ err = kstrtoul(buf, 0, &type);
+ if (err)
+ return err;
+
+ err = xswap_destroy(type);
+ if (err)
+ return err;
+
+ return count;
+}
+
+static struct kobj_attribute xswap_destroy_attr = __ATTR(destroy, 0200, NULL,
+ xswap_destroy_store);
+
+static struct attribute *xswap_attrs[] = {
+ &xswap_create_attr.attr,
+ &xswap_destroy_attr.attr,
+ NULL,
+};
+
+static const struct attribute_group xswap_attr_group = {
+ .attrs = xswap_attrs,
+};
+
+static struct kobject *xswap_kobj;
+
+static void xswap_sysfs_init(void)
+{
+ xswap_kobj = kobject_create_and_add("xswap", mm_kobj);
+ if (!xswap_kobj) {
+ pr_err("xswap: failed to create sysfs kobject\n");
+ return;
+ }
+ if (sysfs_create_group(xswap_kobj, &xswap_attr_group))
+ pr_err("xswap: failed to create sysfs group\n");
+}
+#else
+static inline void xswap_sysfs_init(void)
+{
+}
+#endif /* CONFIG_SYSFS */
+#else /* !CONFIG_XSWAP */
+static inline void xswap_sysfs_init(void)
+{
+}
+#endif /* CONFIG_XSWAP */
static void swap_range_alloc(struct swap_info_struct *si,
unsigned int nr_entries);
@@ -63,9 +272,9 @@ static void move_cluster(struct swap_info_struct *si,
*
* Also protects swap_active_head total_swap_pages, and the SWP_WRITEOK flag.
*/
-static DEFINE_SPINLOCK(swap_lock);
static unsigned int nr_swapfiles;
atomic_long_t nr_swap_pages;
+atomic_t nr_real_swapfiles;
/*
* Some modules use swappable objects and may try to swap them out under
* memory pressure (via the shrinker). Before doing so, they may wish to
@@ -74,7 +283,6 @@ atomic_long_t nr_swap_pages;
EXPORT_SYMBOL_GPL(nr_swap_pages);
/* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
long total_swap_pages;
-#define DEF_SWAP_PRIO -1
unsigned long swapfile_maximum_size;
#ifdef CONFIG_MIGRATION
bool swap_migration_ad_supported;
@@ -609,6 +817,10 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info
swap_cluster_free_table(ci);
move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
ci->order = 0;
+#ifdef CONFIG_XSWAP
+ xswap_update_free_tail(si, ci - si->cluster_info);
+ schedule_work(&si->xswap_shrink_work);
+#endif
}
/*
@@ -955,6 +1167,9 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
if (cluster_is_empty(ci))
ci->order = order;
ci->count += nr_pages;
+#ifdef CONFIG_XSWAP
+ xswap_trim_free_tail(si, cluster_index(si, ci));
+#endif
swap_range_alloc(si, nr_pages);
return true;
@@ -1185,6 +1400,51 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
if (found)
goto done;
}
+
+#ifdef CONFIG_XSWAP
+ /*
+ * For xswap: if no free cluster was found and more clusters
+ * can be mapped, grow the cluster_info array and retry.
+ */
+ if (!found && (si->flags & SWP_XSWAP) &&
+ READ_ONCE(si->nr_clusters_mapped) < READ_ONCE(si->nr_clusters) &&
+ list_empty(&si->free_clusters)) {
+ unsigned long nr_new = min(READ_ONCE(si->nr_clusters) -
+ READ_ONCE(si->nr_clusters_mapped),
+ XSWAP_GROW_CLUSTERS);
+ unsigned long start = READ_ONCE(si->nr_clusters_mapped);
+ unsigned long i;
+
+ if (!xswap_map_clusters(si, start, nr_new)) {
+ unsigned long added = 0;
+
+ for (i = start; i < start + nr_new; i++) {
+ struct swap_cluster_info *ci = &si->cluster_info[i];
+
+ /*
+ * A concurrent grower may have already added
+ * these clusters to the free list. Only add
+ * clusters that are still off-list (NONE).
+ * Lock ci->lock first: move_cluster() takes
+ * si->lock internally.
+ */
+ spin_lock(&ci->lock);
+ if (ci->flags == CLUSTER_FLAG_NONE) {
+ move_cluster(si, ci, &si->free_clusters,
+ CLUSTER_FLAG_FREE);
+ added++;
+ }
+ spin_unlock(&ci->lock);
+ }
+ WRITE_ONCE(si->nr_free_tail,
+ READ_ONCE(si->nr_free_tail) + added);
+
+ /* Retry allocation from the free list */
+ found = alloc_swap_scan_list(si, &si->free_clusters,
+ folio, false);
+ }
+ }
+#endif
done:
if (!(si->flags & SWP_SOLIDSTATE))
spin_unlock(&si->global_cluster_lock);
@@ -1223,6 +1483,8 @@ static void del_from_avail_list(struct swap_info_struct *si, bool swapoff)
goto skip;
}
+ if (!(si->flags & SWP_XSWAP))
+ atomic_sub(1, &nr_real_swapfiles);
plist_del(&si->avail_list, &swap_avail_head);
skip:
@@ -1265,6 +1527,8 @@ static void add_to_avail_list(struct swap_info_struct *si, bool swapon)
}
plist_add(&si->avail_list, &swap_avail_head);
+ if (!(si->flags & SWP_XSWAP))
+ atomic_add(1, &nr_real_swapfiles);
skip:
spin_unlock(&swap_avail_lock);
@@ -2710,15 +2974,27 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
unsigned int prev)
{
unsigned int i;
+ unsigned int end = si->max;
unsigned long swp_tb;
+#ifdef CONFIG_XSWAP
+ /* xswap may have shrunk and unmapped the cluster_info tail. */
+ if (si->flags & SWP_XSWAP) {
+ unsigned long mapped_end;
+
+ mapped_end = READ_ONCE(si->nr_clusters_mapped) * SWAPFILE_CLUSTER;
+ if (mapped_end < end)
+ end = mapped_end;
+ }
+#endif
+
/*
* No need for swap_lock here: we're just looking
* for whether an entry is in use, not modifying it; false
* hits are okay, and sys_swapoff() has already prevented new
* allocations from this area (while holding swap_lock).
*/
- for (i = prev + 1; i < si->max; i++) {
+ for (i = prev + 1; i < end; i++) {
swp_tb = swap_table_get(__swap_offset_to_cluster(si, i),
i % SWAPFILE_CLUSTER);
if (!swp_tb_is_null(swp_tb) && !swp_tb_is_bad(swp_tb))
@@ -2727,7 +3003,7 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
cond_resched();
}
- if (i == si->max)
+ if (i == end)
i = 0;
return i;
@@ -2959,6 +3235,19 @@ static int setup_swap_extents(struct swap_info_struct *sis,
struct inode *inode = mapping->host;
int ret;
+ if (sis->flags & SWP_XSWAP) {
+ *span = 0;
+ /*
+ * xswap devices have no backing block device and
+ * physical writeout is skipped in swap_writeout(),
+ * but sis->ops must still be set so that callers
+ * like shrink_folio_list() can safely dereference
+ * ops->flags.
+ */
+ sis->ops = &swap_bdev_ops;
+ return 0;
+ }
+
ret = sio_pool_init();
if (ret)
return ret;
@@ -3030,20 +3319,46 @@ static void wait_for_allocation(struct swap_info_struct *si)
BUG_ON(si->flags & SWP_WRITEOK);
+#ifdef CONFIG_XSWAP
+ /* Skip shrinker-unmapped cluster tail. */
+ if (si->flags & SWP_XSWAP)
+ end = min(end, READ_ONCE(si->nr_clusters_mapped) *
+ SWAPFILE_CLUSTER);
+#endif
+
for (offset = 0; offset < end; offset += SWAPFILE_CLUSTER) {
ci = swap_cluster_lock(si, offset);
swap_cluster_unlock(ci);
}
}
-static void free_swap_cluster_info(struct swap_cluster_info *cluster_info,
- unsigned long maxpages)
+static void free_swap_cluster_info(struct swap_info_struct *si)
{
+ struct swap_cluster_info *cluster_info = si->cluster_info;
+ unsigned long maxpages = si->max;
struct swap_cluster_info *ci;
- int i, nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
+ int i, nr_clusters;
if (!cluster_info)
return;
+
+#ifdef CONFIG_XSWAP
+ if (si->flags & SWP_XSWAP) {
+ xswap_debugfs_del(si);
+ cancel_work_sync(&si->xswap_shrink_work);
+ /* Unmap all mapped clusters and free the VM_SPARSE area */
+ if (si->nr_clusters_mapped > 0)
+ xswap_unmap_clusters(si, 0, si->nr_clusters_mapped);
+ free_vm_area(si->cluster_vm);
+ si->cluster_vm = NULL;
+ si->nr_clusters_max = 0;
+ si->nr_clusters = 0;
+ si->nr_clusters_mapped = 0;
+ return;
+ }
+#endif
+
+ nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
for (i = 0; i < nr_clusters; i++) {
ci = cluster_info + i;
/* Cluster with bad marks count will have a remaining table */
@@ -3079,63 +3394,13 @@ static void flush_percpu_swap_cluster(struct swap_info_struct *si)
}
-SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
+/* Common swap teardown after list removal; shared by sys_swapoff() and
+ * xswap_destroy().
+ */
+static int __swapoff(struct swap_info_struct *p)
{
- struct swap_info_struct *p = NULL;
- struct swap_cluster_info *cluster_info;
- struct file *swap_file, *victim;
- struct address_space *mapping;
- struct inode *inode;
- unsigned int maxpages;
- int err, found = 0;
-
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
-
- BUG_ON(!current->mm);
-
- CLASS(filename, pathname)(specialfile);
- victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
- if (IS_ERR(victim))
- return PTR_ERR(victim);
-
- mapping = victim->f_mapping;
- spin_lock(&swap_lock);
- plist_for_each_entry(p, &swap_active_head, list) {
- if (p->flags & SWP_WRITEOK) {
- if (p->swap_file->f_mapping == mapping) {
- found = 1;
- break;
- }
- }
- }
- if (!found) {
- err = -EINVAL;
- spin_unlock(&swap_lock);
- goto out_dput;
- }
-
- /* Refuse swapoff while the device is pinned for hibernation */
- if (p->flags & SWP_HIBERNATION) {
- err = -EBUSY;
- spin_unlock(&swap_lock);
- goto out_dput;
- }
-
- if (!security_vm_enough_memory_mm(current->mm, p->pages))
- vm_unacct_memory(p->pages);
- else {
- err = -ENOMEM;
- spin_unlock(&swap_lock);
- goto out_dput;
- }
- spin_lock(&p->lock);
- del_from_avail_list(p, true);
- plist_del(&p->list, &swap_active_head);
- atomic_long_sub(p->pages, &nr_swap_pages);
- total_swap_pages -= p->pages;
- spin_unlock(&p->lock);
- spin_unlock(&swap_lock);
+ struct file *swap_file = NULL;
+ int err;
wait_for_allocation(p);
@@ -3146,7 +3411,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
if (err) {
/* re-insert swap space back into swap_list */
reinsert_swap_info(p);
- goto out_dput;
+ return err;
}
/*
@@ -3167,7 +3432,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
destroy_swap_extents(p, p->swap_file);
- if (!(p->flags & SWP_SOLIDSTATE))
+ if (!(p->flags & SWP_XSWAP) &&
+ !(p->flags & SWP_SOLIDSTATE))
atomic_dec(&nr_rotate_swap);
mutex_lock(&swapon_mutex);
@@ -3177,10 +3443,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
swap_file = p->swap_file;
p->swap_file = NULL;
- maxpages = p->max;
- cluster_info = p->cluster_info;
- p->max = 0;
- p->cluster_info = NULL;
spin_unlock(&p->lock);
spin_unlock(&swap_lock);
arch_swap_invalidate_area(p->type);
@@ -3188,14 +3450,18 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
mutex_unlock(&swapon_mutex);
kfree(p->global_cluster);
p->global_cluster = NULL;
- free_swap_cluster_info(cluster_info, maxpages);
+ free_swap_cluster_info(p);
+ p->max = 0;
+ p->cluster_info = NULL;
- inode = mapping->host;
+ if (swap_file) {
+ struct inode *inode = swap_file->f_mapping->host;
- inode_lock(inode);
- inode->i_flags &= ~S_SWAPFILE;
- inode_unlock(inode);
- filp_close(swap_file, NULL);
+ inode_lock(inode);
+ inode->i_flags &= ~S_SWAPFILE;
+ inode_unlock(inode);
+ filp_close(swap_file, NULL);
+ }
/*
* Clear the SWP_USED flag after all resources are freed so that swapon
@@ -3206,10 +3472,69 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
p->flags = 0;
spin_unlock(&swap_lock);
- err = 0;
atomic_inc(&proc_poll_event);
wake_up_interruptible(&proc_poll_wait);
+ return 0;
+}
+
+SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
+{
+ struct swap_info_struct *p = NULL;
+ struct file *victim;
+ struct address_space *mapping;
+ int err, found = 0;
+
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ BUG_ON(!current->mm);
+
+ CLASS(filename, pathname)(specialfile);
+ victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
+ if (IS_ERR(victim))
+ return PTR_ERR(victim);
+
+ mapping = victim->f_mapping;
+ spin_lock(&swap_lock);
+ plist_for_each_entry(p, &swap_active_head, list) {
+ if (p->flags & SWP_WRITEOK) {
+ if (p->swap_file && p->swap_file->f_mapping == mapping) {
+ found = 1;
+ break;
+ }
+ }
+ }
+ if (!found) {
+ err = -EINVAL;
+ spin_unlock(&swap_lock);
+ goto out_dput;
+ }
+
+ /* Refuse swapoff while the device is pinned for hibernation */
+ if (p->flags & SWP_HIBERNATION) {
+ err = -EBUSY;
+ spin_unlock(&swap_lock);
+ goto out_dput;
+ }
+
+ if (!security_vm_enough_memory_mm(current->mm, p->pages))
+ vm_unacct_memory(p->pages);
+ else {
+ err = -ENOMEM;
+ spin_unlock(&swap_lock);
+ goto out_dput;
+ }
+ spin_lock(&p->lock);
+ del_from_avail_list(p, true);
+ plist_del(&p->list, &swap_active_head);
+ atomic_long_sub(p->pages, &nr_swap_pages);
+ total_swap_pages -= p->pages;
+ spin_unlock(&p->lock);
+ spin_unlock(&swap_lock);
+
+ err = __swapoff(p);
+
out_dput:
filp_close(victim, NULL);
return err;
@@ -3243,7 +3568,7 @@ static void *swap_start(struct seq_file *swap, loff_t *pos)
return SEQ_START_TOKEN;
for (type = 0; (si = swap_type_to_info(type)); type++) {
- if (!(si->swap_file))
+ if (!(si->swap_file) && !(si->flags & SWP_XSWAP))
continue;
if (!--l)
return si;
@@ -3264,7 +3589,7 @@ static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
++(*pos);
for (; (si = swap_type_to_info(type)); type++) {
- if (!(si->swap_file))
+ if (!(si->swap_file) && !(si->flags & SWP_XSWAP))
continue;
return si;
}
@@ -3277,6 +3602,19 @@ static void swap_stop(struct seq_file *swap, void *v)
mutex_unlock(&swapon_mutex);
}
+static const char *swap_type_str(struct swap_info_struct *si)
+{
+ struct file *file = si->swap_file;
+
+ if (si->flags & SWP_XSWAP)
+ return "xswap\t";
+
+ if (S_ISBLK(file_inode(file)->i_mode))
+ return "partition";
+
+ return "file\t";
+}
+
static int swap_show(struct seq_file *swap, void *v)
{
struct swap_info_struct *si = v;
@@ -3293,11 +3631,17 @@ static int swap_show(struct seq_file *swap, void *v)
inuse = K(swap_usage_in_pages(si));
file = si->swap_file;
- len = seq_file_path(swap, file, " \t\n\\");
+ if (file)
+ len = seq_file_path(swap, file, " \t\n\\");
+ else {
+ char name[16];
+
+ len = scnprintf(name, sizeof(name), "xswap%d", si->type);
+ seq_puts(swap, name);
+ }
seq_printf(swap, "%*s%s\t%lu\t%s%lu\t%s%d\n",
len < 40 ? 40 - len : 1, " ",
- S_ISBLK(file_inode(file)->i_mode) ?
- "partition" : "file\t",
+ swap_type_str(si),
bytes, bytes < 10000000 ? "\t" : "",
inuse, inuse < 10000000 ? "\t" : "",
si->prio);
@@ -3530,6 +3874,326 @@ static unsigned long read_swap_header(struct swap_info_struct *si,
return maxpages;
}
+#ifdef CONFIG_XSWAP
+static int xswap_map_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr)
+{
+ unsigned long start_addr = (unsigned long)si->cluster_info +
+ (size_t)start_idx * sizeof(struct swap_cluster_info);
+ unsigned long end_addr = start_addr + (size_t)nr * sizeof(struct swap_cluster_info);
+ /* Round to page boundaries for vm_area_map_pages(). */
+ unsigned long vm_start = PAGE_ALIGN(start_addr);
+ unsigned long vm_end = PAGE_ALIGN(end_addr);
+ unsigned int noreclaim_flags;
+ unsigned long npages;
+ struct page **pages;
+ unsigned long i;
+ int err;
+
+ mutex_lock(&si->xswap_lock);
+
+ if (vm_start >= vm_end) {
+ /* All requested clusters fall within already-mapped pages. */
+ for (i = start_idx; i < start_idx + nr; i++)
+ spin_lock_init(&si->cluster_info[i].lock);
+ WRITE_ONCE(si->nr_clusters_mapped, start_idx + nr);
+ mutex_unlock(&si->xswap_lock);
+ return 0;
+ }
+
+ npages = (vm_end - vm_start) >> PAGE_SHIFT;
+
+ /* Prevent recursive reclaim during vmap page table allocation. */
+ noreclaim_flags = memalloc_noreclaim_save();
+
+ pages = kmalloc_array(npages, sizeof(*pages),
+ __GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL);
+ if (!pages) {
+ memalloc_noreclaim_restore(noreclaim_flags);
+ mutex_unlock(&si->xswap_lock);
+ return -ENOMEM;
+ }
+
+ for (i = 0; i < npages; i++) {
+ /* __GFP_ZERO: cluster_info pointer fields must start NULL. */
+ pages[i] = alloc_page(__GFP_HIGH | __GFP_NOMEMALLOC |
+ GFP_KERNEL | __GFP_ZERO);
+ if (!pages[i])
+ goto fail;
+ }
+
+ /* Detect racing grower that already mapped these pages. */
+ if (apply_to_existing_page_range(&init_mm, vm_start,
+ vm_end - vm_start,
+ xswap_check_mapped, NULL)) {
+ i = npages;
+ goto fail_nounmap;
+ }
+
+ err = vm_area_map_pages(si->cluster_vm, vm_start, vm_end, pages);
+ if (err) {
+ /* -EBUSY: defensive, the page was already mapped. */
+ if (err == -EBUSY) {
+ i = npages;
+ goto fail_nounmap;
+ }
+ i = npages;
+ goto fail;
+ }
+
+ kfree(pages);
+ memalloc_noreclaim_restore(noreclaim_flags);
+
+ /* Initialize spinlocks for newly mapped clusters */
+ for (i = start_idx; i < start_idx + nr; i++)
+ spin_lock_init(&si->cluster_info[i].lock);
+
+ /*
+ * Pairs with READ_ONCE() in shrink/grow paths.
+ */
+ WRITE_ONCE(si->nr_clusters_mapped, start_idx + nr);
+ mutex_unlock(&si->xswap_lock);
+ return 0;
+
+fail_nounmap:
+ /*
+ * The concurrent grower already mapped the range, initialized the
+ * cluster spinlocks and advanced nr_clusters_mapped. It may still
+ * be holding those locks while adding clusters to the free list, so
+ * do not touch them here; just free our unused pages.
+ */
+ while (i > 0) {
+ i--;
+ if (pages[i])
+ __free_page(pages[i]);
+ }
+ kfree(pages);
+ memalloc_noreclaim_restore(noreclaim_flags);
+ mutex_unlock(&si->xswap_lock);
+ return 0;
+
+fail:
+ while (i > 0) {
+ i--;
+ if (pages[i])
+ __free_page(pages[i]);
+ }
+ memalloc_noreclaim_restore(noreclaim_flags);
+ kfree(pages);
+ mutex_unlock(&si->xswap_lock);
+ return -ENOMEM;
+}
+
+struct xswap_page_data {
+ struct page **pages;
+ int nr;
+ int max;
+};
+
+static int xswap_collect_page(pte_t *pte, unsigned long addr, void *data)
+{
+ struct xswap_page_data *xpd = data;
+
+ if (!pte_present(*pte))
+ return 0;
+ if (xpd->nr < xpd->max)
+ xpd->pages[xpd->nr++] = pte_page(*pte);
+ return 0;
+}
+
+static void xswap_unmap_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr)
+{
+ unsigned long start_addr = (unsigned long)si->cluster_info +
+ (size_t)start_idx * sizeof(struct swap_cluster_info);
+ unsigned long end_addr = start_addr + (size_t)nr * sizeof(struct swap_cluster_info);
+ /* Round to page boundaries for vm_area_unmap_pages(). */
+ unsigned long vm_start = PAGE_ALIGN(start_addr);
+ unsigned long vm_end = PAGE_ALIGN(end_addr);
+ unsigned long size;
+ unsigned long npages;
+ struct xswap_page_data xpd;
+ int i;
+
+ mutex_lock(&si->xswap_lock);
+
+ if (vm_start >= vm_end) {
+ WRITE_ONCE(si->nr_clusters_mapped, start_idx);
+ mutex_unlock(&si->xswap_lock);
+ return;
+ }
+
+ size = vm_end - vm_start;
+ npages = size >> PAGE_SHIFT;
+
+ xpd.pages = kmalloc_array(npages, sizeof(*xpd.pages), GFP_KERNEL);
+ if (xpd.pages) {
+ xpd.nr = 0;
+ xpd.max = npages;
+ apply_to_existing_page_range(&init_mm, vm_start, size,
+ xswap_collect_page, &xpd);
+ }
+
+ vm_area_unmap_pages(si->cluster_vm, vm_start, vm_end);
+
+ /* Free the collected backing pages */
+ if (xpd.pages) {
+ for (i = 0; i < xpd.nr; i++)
+ __free_page(xpd.pages[i]);
+ kfree(xpd.pages);
+ }
+
+ /* Pairs with READ_ONCE() in shrink/grow paths. */
+ WRITE_ONCE(si->nr_clusters_mapped, start_idx);
+ mutex_unlock(&si->xswap_lock);
+}
+
+/* Return 1 at first present PTE to signal range is already mapped. */
+static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data)
+{
+ return 1;
+}
+
+/*
+ * Maintain si->nr_free_tail, the number of contiguous free clusters at
+ * the tail of the mapped range. Called when a cluster at @freed_idx is
+ * freed. Provides O(1) shrink detection: if nr_free_tail is non-zero,
+ * the tail can be unmapped without scanning cluster_info[].
+ *
+ * Only increments when @freed_idx is the cluster immediately before the
+ * existing tail region. Then scans backwards for already-free clusters
+ * now connected to the tail, bounded by XSWAP_GROW_CLUSTERS at a time.
+ */
+static void xswap_update_free_tail(struct swap_info_struct *si,
+ unsigned long freed_idx)
+{
+ unsigned long nr_mapped, nr_tail, tid, i;
+ struct swap_cluster_info *ci;
+
+ if (!(si->flags & SWP_XSWAP))
+ return;
+
+ nr_mapped = READ_ONCE(si->nr_clusters_mapped);
+ nr_tail = READ_ONCE(si->nr_free_tail);
+
+ /* Protect against concurrent shrink that races past us */
+ if (nr_tail >= nr_mapped)
+ return;
+
+ tid = nr_mapped - nr_tail - 1;
+
+ /* Only the cluster immediately before the tail region counts */
+ if (freed_idx != tid)
+ return;
+
+ nr_tail++;
+ WRITE_ONCE(si->nr_free_tail, nr_tail);
+
+ /* Extend: include already-free clusters now connected to the tail */
+ for (i = 1; i < XSWAP_GROW_CLUSTERS; i++) {
+ nr_mapped = READ_ONCE(si->nr_clusters_mapped);
+ nr_tail = READ_ONCE(si->nr_free_tail);
+ if (nr_tail >= nr_mapped - 1)
+ break; /* reached cluster 0 */
+ tid = nr_mapped - nr_tail - 1;
+ ci = &si->cluster_info[tid];
+
+ if (READ_ONCE(ci->count) ||
+ READ_ONCE(ci->flags) != CLUSTER_FLAG_FREE)
+ break;
+ nr_tail++;
+ WRITE_ONCE(si->nr_free_tail, nr_tail);
+ }
+}
+
+/*
+ * Trim si->nr_free_tail when a cluster in the tail region is allocated.
+ * @idx: index of the cluster being allocated.
+ */
+static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx)
+{
+ unsigned long nr_mapped, nr_tail, tail_start;
+
+ if (!(si->flags & SWP_XSWAP))
+ return;
+
+ /*
+ * nr_clusters_mapped and nr_free_tail are read locklessly;
+ * concurrent updates may cause nr_free_tail to be trimmed
+ * slightly less than ideally, which is harmless.
+ */
+ nr_mapped = READ_ONCE(si->nr_clusters_mapped);
+ nr_tail = READ_ONCE(si->nr_free_tail);
+ tail_start = nr_mapped - nr_tail;
+ if (idx >= tail_start)
+ WRITE_ONCE(si->nr_free_tail, nr_mapped - idx - 1);
+}
+
+static void xswap_shrink_work_fn(struct work_struct *work)
+{
+ struct swap_info_struct *si = container_of(work,
+ struct swap_info_struct, xswap_shrink_work);
+ xswap_try_shrink(si);
+}
+
+/*
+ * Try to shrink the cluster_info tail. Uses si->nr_free_tail which
+ * is maintained incrementally during alloc/free — no scanning needed.
+ */
+static void xswap_try_shrink(struct swap_info_struct *si)
+{
+ unsigned long nr_mapped, nr_ceiling, nr_tail, nr_unmap;
+ unsigned long start_idx, i;
+ struct swap_cluster_info *ci;
+
+ if (!(si->flags & SWP_XSWAP))
+ return;
+
+ nr_mapped = READ_ONCE(si->nr_clusters_mapped);
+ nr_ceiling = READ_ONCE(si->nr_clusters);
+ nr_tail = READ_ONCE(si->nr_free_tail);
+
+ if (nr_mapped <= nr_ceiling)
+ return;
+ if (nr_tail < XSWAP_GROW_CLUSTERS)
+ return;
+
+ nr_unmap = min(round_down(nr_tail, XSWAP_GROW_CLUSTERS),
+ nr_mapped - nr_ceiling);
+ if (nr_unmap < XSWAP_GROW_CLUSTERS)
+ return;
+ start_idx = nr_mapped - nr_unmap;
+
+ /*
+ * Verify the tail clusters are still free before unmapping. Count the
+ * contiguous free run first, then detach it in a second pass once the
+ * whole run is confirmed long enough. Detaching while counting would
+ * leave a partial run off the free list if it proved too short to unmap.
+ */
+ spin_lock(&si->lock);
+ for (i = start_idx; i < nr_mapped; i++) {
+ ci = &si->cluster_info[i];
+ if (ci->flags != CLUSTER_FLAG_FREE)
+ break;
+ }
+ nr_unmap = i - start_idx;
+ if (nr_unmap < XSWAP_GROW_CLUSTERS) {
+ spin_unlock(&si->lock);
+ return;
+ }
+
+ for (i = start_idx; i < start_idx + nr_unmap; i++) {
+ ci = &si->cluster_info[i];
+ list_del_init(&ci->list);
+ ci->flags = CLUSTER_FLAG_NONE;
+ }
+ spin_unlock(&si->lock);
+
+ xswap_unmap_clusters(si, start_idx, nr_unmap);
+ WRITE_ONCE(si->nr_free_tail, nr_tail - nr_unmap);
+}
+#endif /* CONFIG_XSWAP */
+
static int setup_swap_clusters_info(struct swap_info_struct *si,
union swap_header *swap_header,
unsigned long maxpages)
@@ -3539,10 +4203,78 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
int err = -ENOMEM;
unsigned long i;
+#ifdef CONFIG_XSWAP
+ if (si->flags & SWP_XSWAP) {
+ unsigned long size = PAGE_ALIGN(nr_clusters * sizeof(*cluster_info));
+ struct vm_struct *vm;
+
+ vm = get_vm_area(size, VM_SPARSE);
+ if (!vm)
+ goto err;
+
+ cluster_info = vm->addr;
+ si->cluster_vm = vm;
+ si->nr_clusters_max = nr_clusters;
+ si->nr_clusters = nr_clusters;
+ si->cluster_info = cluster_info;
+
+ /* Must be initialized before xswap_map_clusters() locks it. */
+ mutex_init(&si->xswap_lock);
+
+ /* Map the initial chunk (at least cluster 0) */
+ if (xswap_map_clusters(si, 0, min_t(unsigned long,
+ XSWAP_GROW_CLUSTERS, nr_clusters)))
+ goto err_free_vm;
+
+ /* xswap: only cluster 0 slot 0 is bad */
+ err = swap_cluster_setup_bad_slot(si, cluster_info, 0, false);
+ if (err)
+ goto err_unmap;
+
+ INIT_LIST_HEAD(&si->free_clusters);
+ INIT_LIST_HEAD(&si->full_clusters);
+ INIT_LIST_HEAD(&si->discard_clusters);
+ for (i = 0; i < SWAP_NR_ORDERS; i++) {
+ INIT_LIST_HEAD(&si->nonfull_clusters[i]);
+ INIT_LIST_HEAD(&si->frag_clusters[i]);
+ }
+
+ /* Mark mapped clusters: cluster 0 has 1 bad slot, rest free */
+ for (i = 0; i < si->nr_clusters_mapped; i++) {
+ struct swap_cluster_info *ci = &cluster_info[i];
+
+ if (i == 0) {
+ ci->flags = CLUSTER_FLAG_NONFULL;
+ list_add_tail(&ci->list, &si->nonfull_clusters[0]);
+ } else {
+ ci->flags = CLUSTER_FLAG_FREE;
+ list_add_tail(&ci->list, &si->free_clusters);
+ }
+ }
+
+ /* All mapped clusters except cluster 0 are free at the tail */
+ si->nr_free_tail = si->nr_clusters_mapped - 1;
+
+ INIT_WORK(&si->xswap_shrink_work, xswap_shrink_work_fn);
+ xswap_debugfs_add(si);
+ return 0;
+
+err_unmap:
+ xswap_unmap_clusters(si, 0, si->nr_clusters_mapped);
+err_free_vm:
+ free_vm_area(si->cluster_vm);
+ si->cluster_vm = NULL;
+ si->cluster_info = NULL;
+ return err;
+ }
+#endif /* CONFIG_XSWAP */
+
cluster_info = kvzalloc_objs(*cluster_info, nr_clusters);
if (!cluster_info)
goto err;
+ si->cluster_info = cluster_info;
+
for (i = 0; i < nr_clusters; i++)
spin_lock_init(&cluster_info[i].lock);
@@ -3606,10 +4338,132 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
si->cluster_info = cluster_info;
return 0;
err:
- free_swap_cluster_info(cluster_info, maxpages);
+ free_swap_cluster_info(si);
return err;
}
+#ifdef CONFIG_XSWAP
+#ifdef CONFIG_SYSFS
+/* Create a file-less xswap device. si->max = full RAM; @percent sets the
+ * runtime nr_clusters ceiling.
+ */
+static int xswap_create(int percent, int prio)
+{
+ struct swap_info_struct *si;
+ unsigned long ram, maxpages, init_clusters;
+ int error;
+
+ if (percent < 1 || percent > 100)
+ return -EINVAL;
+ if (prio != DEF_SWAP_PRIO && (prio < 0 || prio > SWAP_FLAG_PRIO_MASK))
+ return -EINVAL;
+
+ /* xswap has no backing store, it relies on zswap. */
+ if (!zswap_is_enabled())
+ return -EOPNOTSUPP;
+
+ si = alloc_swap_info();
+ if (IS_ERR(si))
+ return PTR_ERR(si);
+
+ INIT_WORK(&si->discard_work, swap_discard_work);
+ INIT_WORK(&si->reclaim_work, swap_reclaim_work);
+
+ ram = totalram_pages();
+ maxpages = min_t(unsigned long, ram, swapfile_maximum_size);
+ if ((unsigned int)maxpages == 0)
+ maxpages = UINT_MAX;
+ if (maxpages < 2)
+ maxpages = 2;
+
+ si->bdev = NULL;
+ si->flags |= SWP_XSWAP | SWP_SOLIDSTATE;
+ si->max = maxpages;
+ si->pages = maxpages - 1;
+ /* no backing file: mirror the xswap branch of setup_swap_extents() */
+ si->ops = &swap_bdev_ops;
+
+ error = setup_swap_clusters_info(si, NULL, maxpages);
+ if (error)
+ goto bad_swap;
+
+ /* VM_SPARSE covers full RAM; runtime nr_clusters starts at percent. */
+ init_clusters = div_u64((u64)maxpages * percent, 100 * SWAPFILE_CLUSTER);
+ init_clusters = max_t(unsigned long, init_clusters, XSWAP_GROW_CLUSTERS);
+ if (init_clusters > si->nr_clusters_max)
+ init_clusters = si->nr_clusters_max;
+ si->nr_clusters = init_clusters;
+ si->pages = min_t(unsigned long, init_clusters * SWAPFILE_CLUSTER, si->max) - 1;
+
+ error = zswap_swapon(si->type, si->max);
+ if (error)
+ goto bad_swap;
+
+ mutex_lock(&swapon_mutex);
+ si->prio = prio;
+ si->list.prio = -si->prio;
+ si->avail_list.prio = -si->prio;
+ /* si->swap_file stays NULL: this is a file-less device */
+ enable_swap_info(si);
+ mutex_unlock(&swapon_mutex);
+
+ pr_info("xswap: adding extendable swap type %d (prio %d, %d%% of %lu pages = %u pages, max %lu)\n",
+ si->type, prio, percent, ram, si->pages, maxpages);
+ atomic_inc(&proc_poll_event);
+ wake_up_interruptible(&proc_poll_wait);
+
+ return si->type;
+
+bad_swap:
+ kfree(si->global_cluster);
+ si->global_cluster = NULL;
+ destroy_swap_extents(si, NULL); /* safe: xswap never sets SWP_ACTIVATED */
+ free_swap_cluster_info(si);
+ si->cluster_info = NULL;
+ spin_lock(&swap_lock);
+ si->flags = 0;
+ spin_unlock(&swap_lock);
+ return error;
+}
+
+/* Tear down a file-less xswap device by its swap type. */
+static int xswap_destroy(int type)
+{
+ struct swap_info_struct *p;
+
+ p = swap_type_to_info(type);
+ if (!p)
+ return -EINVAL;
+
+ spin_lock(&swap_lock);
+ if (!(p->flags & SWP_WRITEOK) || !(p->flags & SWP_XSWAP)) {
+ spin_unlock(&swap_lock);
+ return -EINVAL;
+ }
+ /* Refuse swapoff while the device is pinned for hibernation */
+ if (p->flags & SWP_HIBERNATION) {
+ spin_unlock(&swap_lock);
+ return -EBUSY;
+ }
+ if (!security_vm_enough_memory_mm(current->mm, p->pages))
+ vm_unacct_memory(p->pages);
+ else {
+ spin_unlock(&swap_lock);
+ return -ENOMEM;
+ }
+ spin_lock(&p->lock);
+ del_from_avail_list(p, true);
+ plist_del(&p->list, &swap_active_head);
+ atomic_long_sub(p->pages, &nr_swap_pages);
+ total_swap_pages -= p->pages;
+ spin_unlock(&p->lock);
+ spin_unlock(&swap_lock);
+
+ return __swapoff(p);
+}
+#endif /* CONFIG_SYSFS */
+#endif /* CONFIG_XSWAP */
+
SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
{
struct swap_info_struct *si;
@@ -3825,7 +4679,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
si->global_cluster = NULL;
inode = NULL;
destroy_swap_extents(si, swap_file);
- free_swap_cluster_info(si->cluster_info, si->max);
+ free_swap_cluster_info(si);
si->cluster_info = NULL;
/*
* Clear the SWP_USED flag after all resources are freed so
@@ -3937,7 +4791,6 @@ void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
static int __init swapfile_init(void)
{
swapfile_maximum_size = arch_max_swapfile_size();
-
/*
* Once a cluster is freed, it's swap table content is read
* only, and all swap cache readers (swap_cache_*) verifies
@@ -3947,12 +4800,16 @@ static int __init swapfile_init(void)
swap_table_cachep = kmem_cache_create("swap_table",
sizeof(struct swap_table),
0, SLAB_PANIC | SLAB_TYPESAFE_BY_RCU, NULL);
-
#ifdef CONFIG_MIGRATION
if (swapfile_maximum_size >= (1UL << SWP_MIG_TOTAL_BITS))
swap_migration_ad_supported = true;
#endif /* CONFIG_MIGRATION */
+#ifdef CONFIG_XSWAP
+ xswap_debugfs_root = debugfs_create_dir("xswap", NULL);
+#endif
+ xswap_sysfs_init();
+
return 0;
}
subsys_initcall(swapfile_init);
diff --git a/mm/zswap.c b/mm/zswap.c
index 37f34e406c8e3..064970a4393f5 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -998,7 +998,12 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
/* try to allocate swap cache folio */
si = get_swap_device(swpentry);
if (!si)
- return -EEXIST;
+ return -ENOENT;
+
+ if (si->flags & SWP_XSWAP) {
+ put_swap_device(si);
+ return -EINVAL;
+ }
mpol = get_task_policy(current);
folio = swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
@@ -1545,7 +1550,7 @@ bool zswap_store(struct folio *folio)
zswap_pool_put(pool);
put_objcg:
obj_cgroup_put(objcg);
- if (!ret && zswap_pool_reached_full)
+ if (!ret && zswap_pool_reached_full && atomic_read(&nr_real_swapfiles))
queue_work(shrink_wq, &zswap_shrink_work);
check_old:
/*
|
| 3/2 | 2026/08/27 09:59 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 18227 output: 28 thoughts: 469 Thoughts: **Xswap Feature Assessment for Fuzzing** Okay, so we've got a new patch introducing "xswap," an extendable swap device. It looks like it's adding `CONFIG_XSWAP`, which is dependent on `SWAP` and `64BIT`. Interesting. Decoupling PTE swap entries from physical storage is a pretty significant architectural change, and dynamically creating and destroying xswap devices via sysfs, `/sys/kernel/mm/xswap/create` and `destroy`, as well as runtime size tuning through debugfs, are definitely worth investigating. Given that the patch touches `mm/swapfile.c`, `mm/swap_state.c`, `mm/page_io.c`, and `mm/zswap.c`, this is clearly a functional change, and therefore, fuzzing it is definitely on the cards. Let's nail down what needs our attention. The sysfs interface, specifically `xswap_create_store` calling `xswap_create` and `xswap_destroy_store` calling `xswap_destroy`, plus the debugfs write for `xswap_max_clusters_write` are the obvious entry points. We're going to need to define these as our focus symbols: `xswap_create`, `xswap_destroy`, `xswap_map_clusters`, `xswap_unmap_clusters`, `xswap_try_shrink`, `xswap_update_free_tail`, `xswap_trim_free_tail`. Now for the enabling configurations. Obviously, we'll need `XSWAP` (with its dependencies on `SWAP` and `64BIT`). But wait, `xswap_create` checks `zswap_is_enabled()`. This means `ZSWAP` is *likely* also required. Let's go look at `mm/Kconfig` for the `ZSWAP` configuration to verify the dependencies. Okay, let's dive into `xswap_create` in `mm/swapfile.c` to see what it's doing. Yep, it calls `zswap_is_enabled()`, so we are going to need `ZSWAP`. Now, let's see what that depends on... It depends on `FRONTSWAP`, `CRYPTO`, and `ZPOOL`. Hmm, wasn't `FRONTSWAP` deprecated in recent kernels? Let's quickly grep for `ZSWAP` to confirm its dependencies. |
| 4/2 | 2026/08/27 10:00 | tool | grepper |
0mArgs:{
"Expression": "config ZSWAP",
"PathPrefix": "mm/Kconfig"
}Results: {
"Output": "mm/Kconfig=12=menuconfig SWAP\n--\nmm/Kconfig-21-\nmm/Kconfig:22:config ZSWAP\nmm/Kconfig-23-\tbool \"Compressed cache for swap pages\"\n--\nmm/Kconfig-34-\nmm/Kconfig:35:config ZSWAP_DEFAULT_ON\nmm/Kconfig-36-\tbool \"Enable the compressed cache for swap pages by default\"\n--\nmm/Kconfig-44-\nmm/Kconfig:45:config ZSWAP_SHRINKER_DEFAULT_ON\nmm/Kconfig-46-\tbool \"Shrink the zswap pool on memory pressure\"\n--\nmm/Kconfig=59=choice\n--\nmm/Kconfig-76-\nmm/Kconfig:77:config ZSWAP_COMPRESSOR_DEFAULT_DEFLATE\nmm/Kconfig-78-\tbool \"Deflate\"\n--\nmm/Kconfig-82-\nmm/Kconfig:83:config ZSWAP_COMPRESSOR_DEFAULT_LZO\nmm/Kconfig-84-\tbool \"LZO\"\n--\nmm/Kconfig-88-\nmm/Kconfig:89:config ZSWAP_COMPRESSOR_DEFAULT_842\nmm/Kconfig-90-\tbool \"842\"\n--\nmm/Kconfig-94-\nmm/Kconfig:95:config ZSWAP_COMPRESSOR_DEFAULT_LZ4\nmm/Kconfig-96-\tbool \"LZ4\"\n--\nmm/Kconfig-100-\nmm/Kconfig:101:config ZSWAP_COMPRESSOR_DEFAULT_LZ4HC\nmm/Kconfig-102-\tbool \"LZ4HC\"\n--\nmm/Kconfig-106-\nmm/Kconfig:107:config ZSWAP_COMPRESSOR_DEFAULT_ZSTD\nmm/Kconfig-108-\tbool \"zstd\"\n--\nmm/Kconfig=112=endchoice\nmm/Kconfig-113-\nmm/Kconfig:114:config ZSWAP_COMPRESSOR_DEFAULT\nmm/Kconfig-115- string\n"
} |
| 5/2 | 2026/08/27 10:00 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 19795 output: 27 thoughts: 11 Thoughts: Okay, here's my interpretation of that thought process, tailored for an expert audience, and written in the first person: **Analyzing ZSWAP Dependencies** Alright, time to dig into the dependencies of `ZSWAP`. What's really driving this? I need to understand what components are reliant on `ZSWAP` and, conversely, what `ZSWAP` itself depends on. Are there any other virtual memory management systems? Are there any core kernel structures or modules that interface with it directly? I want to map out the call paths and data structures involved, to identify potential bottlenecks, points of failure, or areas for optimization. This isn't just about code, it's about the entire system architecture implications. Understanding the 'who talks to who' will allow me to assess the impact of changes or explore alternative solutions, and maybe see if there are better ways to accomplish the same end. |
| 6/2 | 2026/08/27 10:00 | tool | grepper |
0mArgs:{
"Expression": "depends on.*",
"PathPrefix": "mm/Kconfig"
}Results: {
"Output": "mm/Kconfig=12=menuconfig SWAP\nmm/Kconfig-13-\tbool \"Support for paging of anonymous memory (swap)\"\nmm/Kconfig:14:\tdepends on MMU \u0026\u0026 BLOCK \u0026\u0026 !ARCH_NO_SWAP\nmm/Kconfig-15-\tdefault y\n--\nmm/Kconfig=22=config ZSWAP\nmm/Kconfig-23-\tbool \"Compressed cache for swap pages\"\nmm/Kconfig:24:\tdepends on SWAP\nmm/Kconfig-25-\tselect CRYPTO\n--\nmm/Kconfig=35=config ZSWAP_DEFAULT_ON\nmm/Kconfig-36-\tbool \"Enable the compressed cache for swap pages by default\"\nmm/Kconfig:37:\tdepends on ZSWAP\nmm/Kconfig-38-\thelp\n--\nmm/Kconfig=45=config ZSWAP_SHRINKER_DEFAULT_ON\nmm/Kconfig-46-\tbool \"Shrink the zswap pool on memory pressure\"\nmm/Kconfig:47:\tdepends on ZSWAP\nmm/Kconfig-48-\tdefault n\n--\nmm/Kconfig=59=choice\nmm/Kconfig-60-\tprompt \"Default compressor\"\nmm/Kconfig:61:\tdepends on ZSWAP\nmm/Kconfig-62-\tdefault ZSWAP_COMPRESSOR_DEFAULT_LZO\n--\nmm/Kconfig=114=config ZSWAP_COMPRESSOR_DEFAULT\nmm/Kconfig-115- string\nmm/Kconfig:116: depends on ZSWAP\nmm/Kconfig-117- default \"deflate\" if ZSWAP_COMPRESSOR_DEFAULT_DEFLATE\n--\nmm/Kconfig=125=config XSWAP\nmm/Kconfig-126-\tbool \"Extendable (virtual) swap device\"\nmm/Kconfig:127:\tdepends on SWAP \u0026\u0026 64BIT\nmm/Kconfig-128-\thelp\n--\nmm/Kconfig=137=menu \"Zsmalloc allocator options\"\nmm/Kconfig:138:\tdepends on ZSMALLOC\nmm/Kconfig-139-\n--\nmm/Kconfig=177=config KVFREE_RCU_BATCHED\nmm/Kconfig-178-\tdef_bool y\nmm/Kconfig:179:\tdepends on !SLUB_TINY \u0026\u0026 !TINY_RCU\nmm/Kconfig:180:\tdepends on !RCU_STRICT_GRACE_PERIOD\nmm/Kconfig-181-\nmm/Kconfig=182=config SLUB_TINY\nmm/Kconfig-183-\tbool \"Configure for minimal memory footprint\"\nmm/Kconfig:184:\tdepends on EXPERT \u0026\u0026 !COMPILE_TEST\nmm/Kconfig-185-\tselect SLAB_MERGE_DEFAULT\n--\nmm/Kconfig=209=config SLAB_FREELIST_RANDOM\nmm/Kconfig-210-\tbool \"Randomize slab freelist\"\nmm/Kconfig:211:\tdepends on !SLUB_TINY\nmm/Kconfig-212-\thelp\n--\nmm/Kconfig=217=config SLAB_FREELIST_HARDENED\nmm/Kconfig-218-\tbool \"Harden slab freelist metadata\"\nmm/Kconfig:219:\tdepends on !SLUB_TINY\nmm/Kconfig-220-\thelp\n--\nmm/Kconfig=226=config SLAB_BUCKETS\nmm/Kconfig-227-\tbool \"Support allocation from separate kmalloc buckets\"\nmm/Kconfig:228:\tdepends on !SLUB_TINY\nmm/Kconfig-229-\tdefault SLAB_FREELIST_HARDENED\n--\nmm/Kconfig=243=config SLUB_STATS\n--\nmm/Kconfig-245-\tbool \"Enable performance statistics\"\nmm/Kconfig:246:\tdepends on SYSFS \u0026\u0026 !SLUB_TINY\nmm/Kconfig-247-\thelp\n--\nmm/Kconfig=256=config KMALLOC_PARTITION_CACHES\nmm/Kconfig:257:\tdepends on !SLUB_TINY\nmm/Kconfig-258-\tbool \"Partitioned slab caches for normal kmalloc\"\n--\nmm/Kconfig=272=choice\nmm/Kconfig-273-\tprompt \"Partitioned slab cache mode\"\nmm/Kconfig:274:\tdepends on KMALLOC_PARTITION_CACHES\nmm/Kconfig-275-\tdefault KMALLOC_PARTITION_TYPED if CC_HAS_ALLOC_TOKEN\n--\nmm/Kconfig=292=config KMALLOC_PARTITION_TYPED\nmm/Kconfig-293-\tbool \"Type based slab cache selection for normal kmalloc\"\nmm/Kconfig:294:\tdepends on CC_HAS_ALLOC_TOKEN\nmm/Kconfig-295-\thelp\n--\nmm/Kconfig=363=config MMAP_ALLOW_UNINITIALIZED\nmm/Kconfig-364-\tbool \"Allow mmapped anonymous memory to be uninitialized\"\nmm/Kconfig:365:\tdepends on EXPERT \u0026\u0026 !MMU\nmm/Kconfig-366-\tdefault n\n--\nmm/Kconfig=385=config SELECT_MEMORY_MODEL\nmm/Kconfig-386-\tdef_bool y\nmm/Kconfig:387:\tdepends on ARCH_SELECT_MEMORY_MODEL\nmm/Kconfig-388-\nmm/Kconfig=389=choice\nmm/Kconfig-390-\tprompt \"Memory model\"\nmm/Kconfig:391:\tdepends on SELECT_MEMORY_MODEL\nmm/Kconfig-392-\tdefault SPARSEMEM_MANUAL if ARCH_SPARSEMEM_DEFAULT\n--\nmm/Kconfig=400=config FLATMEM_MANUAL\nmm/Kconfig-401-\tbool \"Flat Memory\"\nmm/Kconfig:402:\tdepends on !NUMA\nmm/Kconfig:403:\tdepends on !ARCH_SPARSEMEM_ENABLE || ARCH_FLATMEM_ENABLE\nmm/Kconfig-404-\thelp\n--\nmm/Kconfig=416=config SPARSEMEM_MANUAL\nmm/Kconfig-417-\tbool \"Sparse Memory\"\nmm/Kconfig:418:\tdepends on ARCH_SPARSEMEM_ENABLE\nmm/Kconfig-419-\thelp\n--\nmm/Kconfig=431=config SPARSEMEM\nmm/Kconfig-432-\tdef_bool y\nmm/Kconfig:433:\tdepends on (!SELECT_MEMORY_MODEL \u0026\u0026 ARCH_SPARSEMEM_ENABLE) || SPARSEMEM_MANUAL\nmm/Kconfig-434-\nmm/Kconfig=435=config FLATMEM\nmm/Kconfig-436-\tdef_bool y\nmm/Kconfig:437:\tdepends on !NUMA\nmm/Kconfig:438:\tdepends on !SPARSEMEM || FLATMEM_MANUAL\nmm/Kconfig-439-\n--\nmm/Kconfig=458=config SPARSEMEM_EXTREME\nmm/Kconfig-459-\tdef_bool y\nmm/Kconfig:460:\tdepends on SPARSEMEM \u0026\u0026 !SPARSEMEM_STATIC\nmm/Kconfig-461-\n--\nmm/Kconfig=465=config SPARSEMEM_VMEMMAP\nmm/Kconfig-466-\tdef_bool y\nmm/Kconfig:467:\tdepends on SPARSEMEM \u0026\u0026 SPARSEMEM_VMEMMAP_ENABLE\nmm/Kconfig-468-\thelp\n--\nmm/Kconfig=491=config HAVE_GUP_FAST\nmm/Kconfig:492:\tdepends on MMU\nmm/Kconfig-493-\tbool\n--\nmm/Kconfig=515=config EXCLUSIVE_SYSTEM_RAM\nmm/Kconfig-516-\tdef_bool y\nmm/Kconfig:517:\tdepends on !DEVMEM || STRICT_DEVMEM\nmm/Kconfig-518-\n--\nmm/Kconfig=523=menuconfig MEMORY_HOTPLUG\n--\nmm/Kconfig-525-\tselect MEMORY_ISOLATION\nmm/Kconfig:526:\tdepends on SPARSEMEM_VMEMMAP\nmm/Kconfig:527:\tdepends on ARCH_ENABLE_MEMORY_HOTPLUG\nmm/Kconfig:528:\tdepends on 64BIT\nmm/Kconfig-529-\tselect NUMA_KEEP_MEMINFO if NUMA\n--\nmm/Kconfig=595=config MHP_MEMMAP_ON_MEMORY\nmm/Kconfig-596-\tdef_bool y\nmm/Kconfig:597:\tdepends on ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE\nmm/Kconfig-598-\n--\nmm/Kconfig=601=config ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE\n--\nmm/Kconfig-607-# Default to 4 for wider testing, though 8 might be more appropriate.\nmm/Kconfig:608:# ARM's adjust_pte (unused if VIPT) depends on mm-wide page_table_lock.\nmm/Kconfig-609-# PA-RISC 7xxx's spinlock_t would enlarge struct page from 32 to 44 bytes.\n--\nmm/Kconfig=615=config SPLIT_PTE_PTLOCKS\nmm/Kconfig-616-\tdef_bool y\nmm/Kconfig:617:\tdepends on MMU\nmm/Kconfig:618:\tdepends on SMP\nmm/Kconfig:619:\tdepends on NR_CPUS \u003e= 4\nmm/Kconfig:620:\tdepends on !ARM || CPU_CACHE_VIPT\nmm/Kconfig:621:\tdepends on !PARISC || PA20\nmm/Kconfig:622:\tdepends on !SPARC32\nmm/Kconfig:623:\tdepends on !UML\nmm/Kconfig-624-\n--\nmm/Kconfig=628=config SPLIT_PMD_PTLOCKS\nmm/Kconfig-629-\tdef_bool y\nmm/Kconfig:630:\tdepends on SPLIT_PTE_PTLOCKS \u0026\u0026 ARCH_ENABLE_SPLIT_PMD_PTLOCK\nmm/Kconfig-631-\n--\nmm/Kconfig=639=config BALLOON_MIGRATION\n--\nmm/Kconfig-641-\tdefault y\nmm/Kconfig:642:\tdepends on MIGRATION \u0026\u0026 BALLOON\nmm/Kconfig-643-\thelp\n--\nmm/Kconfig=651=config COMPACTION\n--\nmm/Kconfig-654-\tselect MIGRATION\nmm/Kconfig:655:\tdepends on MMU\nmm/Kconfig-656-\thelp\n--\nmm/Kconfig=666=config COMPACT_UNEVICTABLE_DEFAULT\nmm/Kconfig-667-\tint\nmm/Kconfig:668:\tdepends on COMPACTION\nmm/Kconfig-669-\tdefault 0 if PREEMPT_RT\n--\nmm/Kconfig=682=config NUMA_MIGRATION\n--\nmm/Kconfig-684-\tdefault y\nmm/Kconfig:685:\tdepends on NUMA \u0026\u0026 MMU\nmm/Kconfig-686-\tselect MIGRATION\n--\nmm/Kconfig=693=config MIGRATION\nmm/Kconfig-694-\tbool\nmm/Kconfig:695:\tdepends on MMU\nmm/Kconfig-696-\n--\nmm/Kconfig=737=config KSM\nmm/Kconfig-738-\tbool \"Enable KSM for page merging\"\nmm/Kconfig:739:\tdepends on MMU\nmm/Kconfig-740-\tselect XXHASH\n--\nmm/Kconfig=752=config DEFAULT_MMAP_MIN_ADDR\nmm/Kconfig-753-\tint \"Low address space to protect from user allocation\"\nmm/Kconfig:754:\tdepends on MMU\nmm/Kconfig-755-\tdefault 4096\n--\nmm/Kconfig=774=config MEMORY_FAILURE\nmm/Kconfig:775:\tdepends on MMU\nmm/Kconfig:776:\tdepends on ARCH_SUPPORTS_MEMORY_FAILURE\nmm/Kconfig-777-\tbool \"Enable recovery from hardware memory errors\"\n--\nmm/Kconfig=786=config HWPOISON_INJECT\nmm/Kconfig-787-\ttristate \"HWPoison pages injector\"\nmm/Kconfig:788:\tdepends on MEMORY_FAILURE \u0026\u0026 DEBUG_KERNEL \u0026\u0026 PROC_FS\nmm/Kconfig-789-\tselect PROC_PAGE_MONITOR\n--\nmm/Kconfig=791=config NOMMU_INITIAL_TRIM_EXCESS\nmm/Kconfig-792-\tint \"Turn on mmap() excess space trimming before booting\"\nmm/Kconfig:793:\tdepends on !MMU\nmm/Kconfig-794-\tdefault 1\n--\nmm/Kconfig=825=config PERSISTENT_HUGE_ZERO_FOLIO\nmm/Kconfig-826-\tbool \"Allocate a PMD sized folio for zeroing\"\nmm/Kconfig:827:\tdepends on TRANSPARENT_HUGEPAGE\nmm/Kconfig-828-\thelp\n--\nmm/Kconfig=844=menuconfig TRANSPARENT_HUGEPAGE\nmm/Kconfig-845-\tbool \"Transparent Hugepage Support\"\nmm/Kconfig:846:\tdepends on HAVE_ARCH_TRANSPARENT_HUGEPAGE \u0026\u0026 !PREEMPT_RT\nmm/Kconfig-847-\tselect COMPACTION\n--\nmm/Kconfig=980=config THP_SWAP\nmm/Kconfig-981-\tdef_bool y\nmm/Kconfig:982:\tdepends on ARCH_WANTS_THP_SWAP \u0026\u0026 SWAP \u0026\u0026 64BIT\nmm/Kconfig-983-\thelp\n--\nmm/Kconfig=1026=config ARCH_SUPPORTS_HUGE_PFNMAP\nmm/Kconfig-1027-\tdef_bool n\nmm/Kconfig:1028:\tdepends on TRANSPARENT_HUGEPAGE\nmm/Kconfig-1029-\nmm/Kconfig=1030=config ARCH_SUPPORTS_PMD_PFNMAP\nmm/Kconfig-1031-\tdef_bool y\nmm/Kconfig:1032:\tdepends on ARCH_SUPPORTS_HUGE_PFNMAP \u0026\u0026 HAVE_ARCH_TRANSPARENT_HUGEPAGE\nmm/Kconfig-1033-\nmm/Kconfig=1034=config ARCH_SUPPORTS_PUD_PFNMAP\nmm/Kconfig-1035-\tdef_bool y\nmm/Kconfig:1036:\tdepends on ARCH_SUPPORTS_HUGE_PFNMAP \u0026\u0026 HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD\nmm/Kconfig-1037-\n--\nmm/Kconfig=1048=config NEED_PER_CPU_KM\nmm/Kconfig:1049:\tdepends on !SMP || !MMU\nmm/Kconfig-1050-\tbool\n--\nmm/Kconfig=1065=config CMA\nmm/Kconfig-1066-\tbool \"Contiguous Memory Allocator\"\nmm/Kconfig:1067:\tdepends on MMU\nmm/Kconfig-1068-\tselect MIGRATION\n--\nmm/Kconfig=1080=config CMA_DEBUGFS\nmm/Kconfig-1081-\tbool \"CMA debugfs interface\"\nmm/Kconfig:1082:\tdepends on CMA \u0026\u0026 DEBUG_FS\nmm/Kconfig-1083-\thelp\n--\nmm/Kconfig=1086=config CMA_SYSFS\nmm/Kconfig-1087-\tbool \"CMA information through sysfs interface\"\nmm/Kconfig:1088:\tdepends on CMA \u0026\u0026 SYSFS\nmm/Kconfig-1089-\thelp\n--\nmm/Kconfig=1093=config CMA_AREAS\nmm/Kconfig-1094-\tint \"Maximum count of the CMA areas\"\nmm/Kconfig:1095:\tdepends on CMA\nmm/Kconfig-1096-\tdefault 20 if NUMA\n--\nmm/Kconfig=1140=config MEM_SOFT_DIRTY\nmm/Kconfig-1141-\tbool \"Track memory changes\"\nmm/Kconfig:1142:\tdepends on CHECKPOINT_RESTORE \u0026\u0026 HAVE_ARCH_SOFT_DIRTY \u0026\u0026 PROC_FS\nmm/Kconfig-1143-\tselect PROC_PAGE_MONITOR\n--\nmm/Kconfig=1155=config STACK_MAX_DEFAULT_SIZE_MB\n--\nmm/Kconfig-1158-\trange 8 2048\nmm/Kconfig:1159:\tdepends on STACK_GROWSUP \u0026\u0026 (!64BIT || COMPAT)\nmm/Kconfig-1160-\thelp\n--\nmm/Kconfig=1167=config DEFERRED_STRUCT_PAGE_INIT\nmm/Kconfig-1168-\tbool \"Defer initialisation of struct pages to kthreads\"\nmm/Kconfig:1169:\tdepends on SPARSEMEM\nmm/Kconfig:1170:\tdepends on !NEED_PER_CPU_KM\nmm/Kconfig:1171:\tdepends on 64BIT\nmm/Kconfig:1172:\tdepends on !KMSAN\nmm/Kconfig-1173-\tselect PADATA\n--\nmm/Kconfig=1191=config IDLE_PAGE_TRACKING\nmm/Kconfig-1192-\tbool \"Enable idle page tracking\"\nmm/Kconfig:1193:\tdepends on SYSFS \u0026\u0026 MMU\nmm/Kconfig-1194-\tselect PAGE_IDLE_FLAG\n--\nmm/Kconfig=1229=config ZONE_DMA32\nmm/Kconfig-1230-\tbool \"Support DMA32 zone\" if ARCH_HAS_ZONE_DMA_SET\nmm/Kconfig:1231:\tdepends on !X86_32\nmm/Kconfig-1232-\tdefault y if ARM64\n--\nmm/Kconfig=1234=config ZONE_DEVICE\nmm/Kconfig-1235-\tbool \"Device memory (pmem, HMM, etc...) hotplug support\"\nmm/Kconfig:1236:\tdepends on MEMORY_HOTREMOVE\nmm/Kconfig-1237-\tselect XARRAY_MULTI\n--\nmm/Kconfig=1256=config HMM_MIRROR\nmm/Kconfig-1257-\tbool\nmm/Kconfig:1258:\tdepends on MMU\nmm/Kconfig-1259-\tselect MMU_NOTIFIER\n--\nmm/Kconfig=1264=config DEVICE_PRIVATE\nmm/Kconfig-1265-\tbool \"Unaddressable device memory (GPU memory, ...)\"\nmm/Kconfig:1266:\tdepends on ZONE_DEVICE\nmm/Kconfig-1267-\tselect GET_FREE_REGION\n--\nmm/Kconfig=1303=config GUP_TEST\nmm/Kconfig-1304-\tbool \"Enable infrastructure for get_user_pages()-related unit tests\"\nmm/Kconfig:1305:\tdepends on DEBUG_FS\nmm/Kconfig-1306-\thelp\n--\nmm/Kconfig=1323=comment \"GUP_TEST needs to have DEBUG_FS enabled\"\nmm/Kconfig:1324:\tdepends on !GUP_TEST \u0026\u0026 !DEBUG_FS\nmm/Kconfig-1325-\n--\nmm/Kconfig=1329=config DMAPOOL_TEST\nmm/Kconfig-1330-\ttristate \"Enable a module to run time tests on dma_pool\"\nmm/Kconfig:1331:\tdepends on HAS_DMA\nmm/Kconfig-1332-\thelp\n--\nmm/Kconfig=1353=config SECRETMEM\n--\nmm/Kconfig-1355-\tbool \"Enable memfd_secret() system call\" if EXPERT\nmm/Kconfig:1356:\tdepends on ARCH_HAS_SET_DIRECT_MAP\nmm/Kconfig-1357-\thelp\n--\nmm/Kconfig=1362=config ANON_VMA_NAME\nmm/Kconfig-1363-\tbool \"Anonymous VMA name support\"\nmm/Kconfig:1364:\tdepends on PROC_FS \u0026\u0026 ADVISE_SYSCALLS \u0026\u0026 MMU\nmm/Kconfig-1365-\n--\nmm/Kconfig=1386=config USERFAULTFD_RWP\nmm/Kconfig-1387-\tdef_bool y\nmm/Kconfig:1388:\tdepends on 64BIT \u0026\u0026 ARCH_HAS_PTE_PROTNONE \u0026\u0026 HAVE_ARCH_USERFAULTFD_WP\nmm/Kconfig-1389-\thelp\n--\nmm/Kconfig=1395=menuconfig USERFAULTFD\nmm/Kconfig-1396-\tbool \"Enable userfaultfd() system call\"\nmm/Kconfig:1397:\tdepends on MMU\nmm/Kconfig-1398-\thelp\n--\nmm/Kconfig=1402=config PTE_MARKER_UFFD_WP\n--\nmm/Kconfig-1404-\tdefault y\nmm/Kconfig:1405:\tdepends on USERFAULTFD \u0026\u0026 HAVE_ARCH_USERFAULTFD_WP\nmm/Kconfig-1406-\n--\nmm/Kconfig=1413=config LRU_GEN\nmm/Kconfig-1414-\tbool \"Multi-Gen LRU\"\nmm/Kconfig:1415:\tdepends on MMU\nmm/Kconfig-1416-\t# make sure folio-\u003eflags has enough spare bits\nmm/Kconfig:1417:\tdepends on 64BIT || !SPARSEMEM || SPARSEMEM_VMEMMAP\nmm/Kconfig-1418-\thelp\n--\nmm/Kconfig=1422=config LRU_GEN_ENABLED\nmm/Kconfig-1423-\tbool \"Enable by default\"\nmm/Kconfig:1424:\tdepends on LRU_GEN\nmm/Kconfig-1425-\thelp\n--\nmm/Kconfig=1428=config LRU_GEN_STATS\nmm/Kconfig-1429-\tbool \"Full stats for debugging\"\nmm/Kconfig:1430:\tdepends on LRU_GEN\nmm/Kconfig-1431-\thelp\n--\nmm/Kconfig=1437=config LRU_GEN_WALKS_MMU\nmm/Kconfig-1438-\tdef_bool y\nmm/Kconfig:1439:\tdepends on LRU_GEN \u0026\u0026 ARCH_HAS_HW_PTE_YOUNG\nmm/Kconfig-1440-# }\n--\nmm/Kconfig=1445=config PER_VMA_LOCK\nmm/Kconfig-1446-\tdef_bool y\nmm/Kconfig:1447:\tdepends on ARCH_SUPPORTS_PER_VMA_LOCK \u0026\u0026 MMU \u0026\u0026 SMP\nmm/Kconfig-1448-\thelp\n--\nmm/Kconfig=1454=config LOCK_MM_AND_FIND_VMA\nmm/Kconfig-1455-\tbool\nmm/Kconfig:1456:\tdepends on !STACK_GROWSUP\nmm/Kconfig-1457-\n--\nmm/Kconfig=1467=config NUMA_EMU\nmm/Kconfig-1468-\tbool \"NUMA emulation\"\nmm/Kconfig:1469:\tdepends on NUMA_MEMBLKS\nmm/Kconfig:1470:\tdepends on X86 || GENERIC_ARCH_NUMA\nmm/Kconfig-1471-\thelp\n--\nmm/Kconfig=1492=config PT_RECLAIM\nmm/Kconfig-1493-\tdef_bool y\nmm/Kconfig:1494:\tdepends on MMU_GATHER_RCU_TABLE_FREE \u0026\u0026 !HAVE_ARCH_TLB_REMOVE_TABLE\nmm/Kconfig-1495-\thelp\n--\nmm/Kconfig=1511=config LAZY_MMU_MODE_KUNIT_TEST\nmm/Kconfig-1512-\ttristate \"KUnit tests for the lazy MMU mode\" if !KUNIT_ALL_TESTS\nmm/Kconfig:1513:\tdepends on ARCH_HAS_LAZY_MMU_MODE\nmm/Kconfig:1514:\tdepends on KUNIT\nmm/Kconfig-1515-\tdefault KUNIT_ALL_TESTS\n"
} |
| 7/2 | 2026/08/27 10:00 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 26216 output: 26 thoughts: 286 Thoughts: **My Analysis of XSWAP and its Dependencies** Okay, so I'm digging into the dependencies of this XSWAP feature. Clearly, it hinges on the more fundamental SWAP functionality, that's a given. And I recall there's a 64-bit architecture requirement, so `XSWAP` depends on both `SWAP` and `64BIT`. Now, the mention of ZSMALLOC triggered a thought, but I need to be precise. Does `ZSWAP` *require* `ZSMALLOC`? No, that's incorrect, my understanding is that ZSWAP needs a zpool, not necessarily ZSMALLOC. I should clarify the relationship between `ZSWAP` and ZSMALLOC later. Let's make sure I didn't miss any other obvious dependencies. Let's see. My focus is on configuring and interacting with XSWAP from userspace, so let's look at the kernel configuration options involved. The sysfs interface for xswap, that's under `CONFIG_SYSFS`, which is usually enabled in a standard kernel build, so that's likely not a problem, but it's important to remember. The debugfs interface, `CONFIG_DEBUG_FS`, is good for debugging, but not essential for testing the core functionality of the feature. Okay, let's revisit `xswap_create`. I need to be really clear on what's needed for userspace device creation. Ah, yes, it *is* inside an `#ifdef CONFIG_SYSFS` block. So, creating XSWAP devices from userspace is dependent on `SYSFS` being enabled. Now, double-checking, does it depend on `XSWAP` itself? Yes, `xswap_create` is within an `#ifdef CONFIG_XSWAP` block, so that's a definite requirement. So it comes down to: `XSWAP` and `SYSFS` are required. Oh, wait, the implementation checks `zswap_is_enabled()`. That seems to indicate a reliance on `ZSWAP`. If `ZSWAP` isn't enabled, then `zswap_is_enabled()` might return false. I need to verify that and understand how that affects things. I should check `include/linux/zswap.h` or `mm/zswap.c` to understand that check. I need to understand that link between the two. |
| 8/2 | 2026/08/27 10:00 | tool | grepper |
0mArgs:{
"Expression": "zswap_is_enabled",
"PathPrefix": ""
}Results: {
"Output": "include/linux/zswap.h=35=void zswap_folio_swapin(struct folio *folio);\ninclude/linux/zswap.h:36:bool zswap_is_enabled(void);\ninclude/linux/zswap.h-37-bool zswap_never_enabled(void);\n--\ninclude/linux/zswap.h=60=static inline void zswap_folio_swapin(struct folio *folio) {}\ninclude/linux/zswap.h-61-\ninclude/linux/zswap.h:62:static inline bool zswap_is_enabled(void)\ninclude/linux/zswap.h-63-{\n--\nmm/memcontrol.c=6080=bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg)\n--\nmm/memcontrol.c-6082-\t/* if zswap is disabled, do not block pages going to the swapping device */\nmm/memcontrol.c:6083:\tif (!zswap_is_enabled())\nmm/memcontrol.c-6084-\t\treturn true;\n--\nmm/swapfile.c=4350=static int xswap_create(int percent, int prio)\n--\nmm/swapfile.c-4361-\t/* xswap has no backing store, it relies on zswap. */\nmm/swapfile.c:4362:\tif (!zswap_is_enabled())\nmm/swapfile.c-4363-\t\treturn -EOPNOTSUPP;\n--\nmm/zswap.c=123=module_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644);\nmm/zswap.c-124-\nmm/zswap.c:125:bool zswap_is_enabled(void)\nmm/zswap.c-126-{\n"
} |
| 9/2 | 2026/08/27 10:00 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 27517 output: 33 thoughts: 110 Thoughts: **ZSWAP Configuration Deep Dive** Okay, so the initial assumption is that `ZSWAP` has to be enabled for this specific use case. That seems like the logical starting point. Now, let's delve into its configuration requirements. I need to make sure I haven't missed anything. I recall `ZSWAP` relies on `SWAP` and also implicitly selects `CRYPTO` for its underlying operations. Good to keep in mind, of course, because that will influence related system resources. Now, a crucial point: Is `ZSWAP_DEFAULT_ON` actually needed? If `ZSWAP` is enabled in the kernel config, but not activated by default at boot, then `zswap_is_enabled()` could return false unless specifically configured to be on with a boot parameter. I want to avoid that gotcha. Therefore, a careful reading of `zswap_is_enabled()` in `mm/zswap.c` is paramount to a complete picture. Gotta know the state of play. |
| 10/2 | 2026/08/27 10:00 | tool | grepper |
0mArgs:{
"Expression": "bool zswap_is_enabled",
"PathPrefix": "mm/zswap.c"
}Results: {
"Output": "mm/zswap.c=123=module_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644);\nmm/zswap.c-124-\nmm/zswap.c:125:bool zswap_is_enabled(void)\nmm/zswap.c-126-{\n"
} |
| 11/2 | 2026/08/27 10:00 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 27973 output: 34 thoughts: 19 Thoughts: Here's a summary of my thought process, as requested: **Diving into `mm/zswap.c`** Okay, I need to get a handle on how this zswap implementation is working. Let's see... the most sensible starting point is probably right around line 125 in `mm/zswap.c`. That should give me a good entry point into the core logic, assuming the author structured the code somewhat logically. I'm expecting to find some crucial initialization or data structure definitions there, or perhaps the beginnings of the page swapping or compression processes. No time to waste; let's get that code loaded up and start digging. I'm hoping to quickly identify the key functions or data structures that govern zswap's operation within the kernel. |
| 12/2 | 2026/08/27 10:00 | tool | read-file |
0mArgs:{
"File": "mm/zswap.c",
"FirstLine": 120,
"LineCount": 20
}Results: {
"Contents": " 120:\t/* Enable/disable memory pressure-based shrinker. */\n 121:\tstatic bool zswap_shrinker_enabled = IS_ENABLED(\n 122:\t\t\tCONFIG_ZSWAP_SHRINKER_DEFAULT_ON);\n 123:\tmodule_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644);\n 124:\t\n 125:\tbool zswap_is_enabled(void)\n 126:\t{\n 127:\t\treturn zswap_enabled;\n 128:\t}\n 129:\t\n 130:\tbool zswap_never_enabled(void)\n 131:\t{\n 132:\t\treturn !static_branch_maybe(CONFIG_ZSWAP_DEFAULT_ON, \u0026zswap_ever_enabled);\n 133:\t}\n 134:\t\n 135:\t/*********************************\n 136:\t* data structures\n 137:\t**********************************/\n 138:\t\n 139:\tstruct crypto_acomp_ctx {\n"
} |
| 13/2 | 2026/08/27 10:00 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 28485 output: 30 thoughts: 23 Thoughts: Here's my take on those thoughts, tailored for an expert audience: **Investigating zswap_enabled Initialization** Okay, so we've got `zswap_enabled` back from wherever it came, likely a system call or config read. My immediate next thought is, "Right, let's trace this thing back to its roots." I need to understand *how* `zswap_enabled` is actually being set. Is it a default, a boot parameter, something in `/proc`, a sysfs entry, or a more elaborate configuration mechanism? Knowing where it's initialized will tell me everything about its potential behavior. It could be dynamic, static, overridden by various methods. The specifics of the initialization determine the level of my concern, and what I should investigate next. |
| 14/2 | 2026/08/27 10:00 | tool | grepper |
0mArgs:{
"Expression": "zswap_enabled",
"PathPrefix": "mm/zswap.c"
}Results: {
"Output": "mm/zswap.c=89=static DEFINE_STATIC_KEY_MAYBE(CONFIG_ZSWAP_DEFAULT_ON, zswap_ever_enabled);\nmm/zswap.c:90:static bool zswap_enabled = IS_ENABLED(CONFIG_ZSWAP_DEFAULT_ON);\nmm/zswap.c:91:static int zswap_enabled_param_set(const char *,\nmm/zswap.c-92-\t\t\t\t const struct kernel_param *);\nmm/zswap.c:93:static const struct kernel_param_ops zswap_enabled_param_ops = {\nmm/zswap.c:94:\t.set =\t\tzswap_enabled_param_set,\nmm/zswap.c-95-\t.get =\t\tparam_get_bool,\nmm/zswap.c-96-};\nmm/zswap.c:97:module_param_cb(enabled, \u0026zswap_enabled_param_ops, \u0026zswap_enabled, 0644);\nmm/zswap.c-98-\n--\nmm/zswap.c=125=bool zswap_is_enabled(void)\nmm/zswap.c-126-{\nmm/zswap.c:127:\treturn zswap_enabled;\nmm/zswap.c-128-}\n--\nmm/zswap.c=528=static int zswap_compressor_param_set(const char *val, const struct kernel_param *kp)\n--\nmm/zswap.c-616-\nmm/zswap.c:617:static int zswap_enabled_param_set(const char *val,\nmm/zswap.c-618-\t\t\t\t const struct kernel_param *kp)\n--\nmm/zswap.c=1493=bool zswap_store(struct folio *folio)\n--\nmm/zswap.c-1505-\nmm/zswap.c:1506:\tif (!zswap_enabled)\nmm/zswap.c-1507-\t\tgoto check_old;\n--\nmm/zswap.c=1776=static int zswap_setup(void)\n--\nmm/zswap.c-1815-\t\tpr_err(\"pool creation failed\\n\");\nmm/zswap.c:1816:\t\tzswap_enabled = false;\nmm/zswap.c-1817-\t}\n--\nmm/zswap.c-1834-\tzswap_init_state = ZSWAP_INIT_FAILED;\nmm/zswap.c:1835:\tzswap_enabled = false;\nmm/zswap.c-1836-\treturn -ENOMEM;\n--\nmm/zswap.c=1839=static int __init zswap_init(void)\nmm/zswap.c-1840-{\nmm/zswap.c:1841:\tif (!zswap_enabled)\nmm/zswap.c-1842-\t\treturn 0;\n"
} |
| 15/2 | 2026/08/27 10:00 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 29456 output: 29 thoughts: 182 Thoughts: **My Zswap Configuration Plan** Alright, so to ensure `zswap_enabled` is true by default, I need to include `CONFIG_ZSWAP_DEFAULT_ON`. I'll add `ZSWAP_DEFAULT_ON` to the EnableConfigs section. While I'm at it, I should also consider `XSWAP` and `SYSFS` in relation to this configuration change. Now, I'm going to shift my focus to the core functions. I need to make sure I understand the interactions of: * `xswap_create` * `xswap_destroy` * `xswap_map_clusters` * `xswap_unmap_clusters` * `xswap_try_shrink` * `xswap_update_free_tail` * `xswap_trim_free_tail` * `xswap_max_clusters_write` * `xswap_max_clusters_read` * `xswap_create_store` * `xswap_destroy_store` I'll verify the names of each function as a sanity check. |
| 16/2 | 2026/08/27 10:00 | tool | grepper |
0mArgs:{
"Expression": "xswap_",
"PathPrefix": "mm/swapfile.c"
}Results: {
"Output": "mm/swapfile.c=57=static DEFINE_SPINLOCK(swap_lock);\n--\nmm/swapfile.c-73-\nmm/swapfile.c:74:static struct dentry *xswap_debugfs_root;\nmm/swapfile.c-75-\nmm/swapfile.c:76:static int xswap_map_clusters(struct swap_info_struct *si,\nmm/swapfile.c-77-\t\t\t unsigned long start_idx, unsigned long nr);\nmm/swapfile.c:78:static void xswap_unmap_clusters(struct swap_info_struct *si,\nmm/swapfile.c-79-\t\t\t\t unsigned long start_idx, unsigned long nr);\nmm/swapfile.c:80:static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data);\nmm/swapfile.c:81:static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx);\nmm/swapfile.c:82:static void xswap_update_free_tail(struct swap_info_struct *si,\nmm/swapfile.c-83-\t\t\t\t unsigned long freed_idx);\nmm/swapfile.c:84:static void xswap_try_shrink(struct swap_info_struct *si);\nmm/swapfile.c-85-\n--\nmm/swapfile.c-88- */\nmm/swapfile.c:89:static ssize_t xswap_max_clusters_read(struct file *file, char __user *buf,\nmm/swapfile.c-90-\t\t\t\t size_t count, loff_t *ppos)\n--\nmm/swapfile.c-99-\nmm/swapfile.c:100:static ssize_t xswap_max_clusters_write(struct file *file,\nmm/swapfile.c-101-\t\t\t\t\tconst char __user *buf,\n--\nmm/swapfile.c-132-\t/* Lowering the ceiling may free tail clusters. */\nmm/swapfile.c:133:\txswap_try_shrink(si);\nmm/swapfile.c-134-\n--\nmm/swapfile.c-137-\nmm/swapfile.c:138:static const struct file_operations xswap_debugfs_fops = {\nmm/swapfile.c:139:\t.read = xswap_max_clusters_read,\nmm/swapfile.c:140:\t.write = xswap_max_clusters_write,\nmm/swapfile.c-141-\t.open = simple_open,\n--\nmm/swapfile.c-144-\nmm/swapfile.c:145:static void xswap_debugfs_add(struct swap_info_struct *si)\nmm/swapfile.c-146-{\n--\nmm/swapfile.c-148-\nmm/swapfile.c:149:\tif (!xswap_debugfs_root)\nmm/swapfile.c-150-\t\treturn;\n--\nmm/swapfile.c-152-\tsnprintf(name, sizeof(name), \"type%d_cluster_limit\", si-\u003etype);\nmm/swapfile.c:153:\tsi-\u003edebugfs_entry = debugfs_create_file(name, 0644, xswap_debugfs_root,\nmm/swapfile.c:154:\t\t\t\t\t\tsi, \u0026xswap_debugfs_fops);\nmm/swapfile.c-155-}\nmm/swapfile.c-156-\nmm/swapfile.c:157:static void xswap_debugfs_del(struct swap_info_struct *si)\nmm/swapfile.c-158-{\n--\nmm/swapfile.c-163-#ifdef CONFIG_SYSFS\nmm/swapfile.c:164:static int xswap_create(int percent, int prio);\nmm/swapfile.c:165:static int xswap_destroy(int type);\nmm/swapfile.c-166-/* /sys/kernel/mm/xswap/: create.\n--\nmm/swapfile.c-171-\nmm/swapfile.c:172:static ssize_t xswap_create_store(struct kobject *kobj,\nmm/swapfile.c-173-\t\t\t\t struct kobj_attribute *attr,\n--\nmm/swapfile.c-193-\nmm/swapfile.c:194:\terr = xswap_create(percent, prio);\nmm/swapfile.c-195-\tif (err \u003c 0)\n--\nmm/swapfile.c-200-\nmm/swapfile.c:201:static struct kobj_attribute xswap_create_attr = __ATTR(create, 0200, NULL,\nmm/swapfile.c:202:\t\t\t\t\t\t\txswap_create_store);\nmm/swapfile.c-203-\nmm/swapfile.c:204:static ssize_t xswap_destroy_store(struct kobject *kobj,\nmm/swapfile.c-205-\t\t\t\t struct kobj_attribute *attr,\n--\nmm/swapfile.c-217-\nmm/swapfile.c:218:\terr = xswap_destroy(type);\nmm/swapfile.c-219-\tif (err)\n--\nmm/swapfile.c-224-\nmm/swapfile.c:225:static struct kobj_attribute xswap_destroy_attr = __ATTR(destroy, 0200, NULL,\nmm/swapfile.c:226:\t\t\t\t\t\t\t xswap_destroy_store);\nmm/swapfile.c-227-\nmm/swapfile.c:228:static struct attribute *xswap_attrs[] = {\nmm/swapfile.c:229:\t\u0026xswap_create_attr.attr,\nmm/swapfile.c:230:\t\u0026xswap_destroy_attr.attr,\nmm/swapfile.c-231-\tNULL,\n--\nmm/swapfile.c-233-\nmm/swapfile.c:234:static const struct attribute_group xswap_attr_group = {\nmm/swapfile.c:235:\t.attrs = xswap_attrs,\nmm/swapfile.c-236-};\nmm/swapfile.c-237-\nmm/swapfile.c:238:static struct kobject *xswap_kobj;\nmm/swapfile.c-239-\nmm/swapfile.c:240:static void xswap_sysfs_init(void)\nmm/swapfile.c-241-{\nmm/swapfile.c:242:\txswap_kobj = kobject_create_and_add(\"xswap\", mm_kobj);\nmm/swapfile.c:243:\tif (!xswap_kobj) {\nmm/swapfile.c-244-\t\tpr_err(\"xswap: failed to create sysfs kobject\\n\");\n--\nmm/swapfile.c-246-\t}\nmm/swapfile.c:247:\tif (sysfs_create_group(xswap_kobj, \u0026xswap_attr_group))\nmm/swapfile.c-248-\t\tpr_err(\"xswap: failed to create sysfs group\\n\");\n--\nmm/swapfile.c-250-#else\nmm/swapfile.c:251:static inline void xswap_sysfs_init(void)\nmm/swapfile.c-252-{\n--\nmm/swapfile.c-255-#else /* !CONFIG_XSWAP */\nmm/swapfile.c:256:static inline void xswap_sysfs_init(void)\nmm/swapfile.c-257-{\n--\nmm/swapfile.c=814=static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci)\n--\nmm/swapfile.c-820-#ifdef CONFIG_XSWAP\nmm/swapfile.c:821:\txswap_update_free_tail(si, ci - si-\u003ecluster_info);\nmm/swapfile.c:822:\tschedule_work(\u0026si-\u003exswap_shrink_work);\nmm/swapfile.c-823-#endif\n--\nmm/swapfile.c=1123=static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,\n--\nmm/swapfile.c-1170-#ifdef CONFIG_XSWAP\nmm/swapfile.c:1171:\txswap_trim_free_tail(si, cluster_index(si, ci));\nmm/swapfile.c-1172-#endif\n--\nmm/swapfile.c=1313=static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,\n--\nmm/swapfile.c-1417-\nmm/swapfile.c:1418:\t\tif (!xswap_map_clusters(si, start, nr_new)) {\nmm/swapfile.c-1419-\t\t\tunsigned long added = 0;\n--\nmm/swapfile.c=3335=static void free_swap_cluster_info(struct swap_info_struct *si)\n--\nmm/swapfile.c-3346-\tif (si-\u003eflags \u0026 SWP_XSWAP) {\nmm/swapfile.c:3347:\t\txswap_debugfs_del(si);\nmm/swapfile.c:3348:\t\tcancel_work_sync(\u0026si-\u003exswap_shrink_work);\nmm/swapfile.c-3349-\t\t/* Unmap all mapped clusters and free the VM_SPARSE area */\nmm/swapfile.c-3350-\t\tif (si-\u003enr_clusters_mapped \u003e 0)\nmm/swapfile.c:3351:\t\t\txswap_unmap_clusters(si, 0, si-\u003enr_clusters_mapped);\nmm/swapfile.c-3352-\t\tfree_vm_area(si-\u003ecluster_vm);\n--\nmm/swapfile.c=3379=static void flush_percpu_swap_cluster(struct swap_info_struct *si)\n--\nmm/swapfile.c-3397-/* Common swap teardown after list removal; shared by sys_swapoff() and\nmm/swapfile.c:3398: * xswap_destroy().\nmm/swapfile.c-3399- */\n--\nmm/swapfile.c=3814=static unsigned long read_swap_header(struct swap_info_struct *si,\n--\nmm/swapfile.c-3877-#ifdef CONFIG_XSWAP\nmm/swapfile.c:3878:static int xswap_map_clusters(struct swap_info_struct *si,\nmm/swapfile.c-3879-\t\t\t unsigned long start_idx, unsigned long nr)\n--\nmm/swapfile.c-3892-\nmm/swapfile.c:3893:\tmutex_lock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-3894-\n--\nmm/swapfile.c-3899-\t\tWRITE_ONCE(si-\u003enr_clusters_mapped, start_idx + nr);\nmm/swapfile.c:3900:\t\tmutex_unlock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-3901-\t\treturn 0;\n--\nmm/swapfile.c-3912-\t\tmemalloc_noreclaim_restore(noreclaim_flags);\nmm/swapfile.c:3913:\t\tmutex_unlock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-3914-\t\treturn -ENOMEM;\n--\nmm/swapfile.c-3927-\t\t\t\t\t vm_end - vm_start,\nmm/swapfile.c:3928:\t\t\t\t\t xswap_check_mapped, NULL)) {\nmm/swapfile.c-3929-\t\ti = npages;\n--\nmm/swapfile.c-3954-\tWRITE_ONCE(si-\u003enr_clusters_mapped, start_idx + nr);\nmm/swapfile.c:3955:\tmutex_unlock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-3956-\treturn 0;\n--\nmm/swapfile.c-3971-\tmemalloc_noreclaim_restore(noreclaim_flags);\nmm/swapfile.c:3972:\tmutex_unlock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-3973-\treturn 0;\n--\nmm/swapfile.c-3982-\tkfree(pages);\nmm/swapfile.c:3983:\tmutex_unlock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-3984-\treturn -ENOMEM;\n--\nmm/swapfile.c-3986-\nmm/swapfile.c:3987:struct xswap_page_data {\nmm/swapfile.c-3988-\tstruct page **pages;\n--\nmm/swapfile.c-3992-\nmm/swapfile.c:3993:static int xswap_collect_page(pte_t *pte, unsigned long addr, void *data)\nmm/swapfile.c-3994-{\nmm/swapfile.c:3995:\tstruct xswap_page_data *xpd = data;\nmm/swapfile.c-3996-\n--\nmm/swapfile.c-4003-\nmm/swapfile.c:4004:static void xswap_unmap_clusters(struct swap_info_struct *si,\nmm/swapfile.c-4005-\t\t\t\t unsigned long start_idx, unsigned long nr)\n--\nmm/swapfile.c-4014-\tunsigned long npages;\nmm/swapfile.c:4015:\tstruct xswap_page_data xpd;\nmm/swapfile.c-4016-\tint i;\nmm/swapfile.c-4017-\nmm/swapfile.c:4018:\tmutex_lock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-4019-\n--\nmm/swapfile.c-4021-\t\tWRITE_ONCE(si-\u003enr_clusters_mapped, start_idx);\nmm/swapfile.c:4022:\t\tmutex_unlock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-4023-\t\treturn;\n--\nmm/swapfile.c-4033-\t\tapply_to_existing_page_range(\u0026init_mm, vm_start, size,\nmm/swapfile.c:4034:\t\t\t\t\t xswap_collect_page, \u0026xpd);\nmm/swapfile.c-4035-\t}\n--\nmm/swapfile.c-4047-\tWRITE_ONCE(si-\u003enr_clusters_mapped, start_idx);\nmm/swapfile.c:4048:\tmutex_unlock(\u0026si-\u003exswap_lock);\nmm/swapfile.c-4049-}\n--\nmm/swapfile.c-4051-/* Return 1 at first present PTE to signal range is already mapped. */\nmm/swapfile.c:4052:static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data)\nmm/swapfile.c-4053-{\n--\nmm/swapfile.c-4066- */\nmm/swapfile.c:4067:static void xswap_update_free_tail(struct swap_info_struct *si,\nmm/swapfile.c-4068-\t\t\t\t unsigned long freed_idx)\n--\nmm/swapfile.c-4112- */\nmm/swapfile.c:4113:static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx)\nmm/swapfile.c-4114-{\n--\nmm/swapfile.c-4131-\nmm/swapfile.c:4132:static void xswap_shrink_work_fn(struct work_struct *work)\nmm/swapfile.c-4133-{\nmm/swapfile.c-4134-\tstruct swap_info_struct *si = container_of(work,\nmm/swapfile.c:4135:\t\t\tstruct swap_info_struct, xswap_shrink_work);\nmm/swapfile.c:4136:\txswap_try_shrink(si);\nmm/swapfile.c-4137-}\n--\nmm/swapfile.c-4142- */\nmm/swapfile.c:4143:static void xswap_try_shrink(struct swap_info_struct *si)\nmm/swapfile.c-4144-{\n--\nmm/swapfile.c-4191-\nmm/swapfile.c:4192:\txswap_unmap_clusters(si, start_idx, nr_unmap);\nmm/swapfile.c-4193-\tWRITE_ONCE(si-\u003enr_free_tail, nr_tail - nr_unmap);\n--\nmm/swapfile.c=4197=static int setup_swap_clusters_info(struct swap_info_struct *si,\n--\nmm/swapfile.c-4220-\nmm/swapfile.c:4221:\t\t/* Must be initialized before xswap_map_clusters() locks it. */\nmm/swapfile.c:4222:\t\tmutex_init(\u0026si-\u003exswap_lock);\nmm/swapfile.c-4223-\nmm/swapfile.c-4224-\t\t/* Map the initial chunk (at least cluster 0) */\nmm/swapfile.c:4225:\t\tif (xswap_map_clusters(si, 0, min_t(unsigned long,\nmm/swapfile.c-4226-\t\t\t\t\tXSWAP_GROW_CLUSTERS, nr_clusters)))\n--\nmm/swapfile.c-4257-\nmm/swapfile.c:4258:\t\tINIT_WORK(\u0026si-\u003exswap_shrink_work, xswap_shrink_work_fn);\nmm/swapfile.c:4259:\t\txswap_debugfs_add(si);\nmm/swapfile.c-4260-\t\treturn 0;\n--\nmm/swapfile.c-4262-err_unmap:\nmm/swapfile.c:4263:\t\txswap_unmap_clusters(si, 0, si-\u003enr_clusters_mapped);\nmm/swapfile.c-4264-err_free_vm:\n--\nmm/swapfile.c-4349- */\nmm/swapfile.c:4350:static int xswap_create(int percent, int prio)\nmm/swapfile.c-4351-{\n--\nmm/swapfile.c-4429-/* Tear down a file-less xswap device by its swap type. */\nmm/swapfile.c:4430:static int xswap_destroy(int type)\nmm/swapfile.c-4431-{\n--\nmm/swapfile.c=4791=static int __init swapfile_init(void)\n--\nmm/swapfile.c-4808-#ifdef CONFIG_XSWAP\nmm/swapfile.c:4809:\txswap_debugfs_root = debugfs_create_dir(\"xswap\", NULL);\nmm/swapfile.c-4810-#endif\nmm/swapfile.c:4811:\txswap_sysfs_init();\nmm/swapfile.c-4812-\n"
} |
| 17/2 | 2026/08/27 10:00 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 34844 output: 28 thoughts: 1134 Thoughts: **Analyzing xswap Function Entry Points** Okay, let's break this down. The initial focus is on identifying key entry point functions within the `xswap` subsystem, specifically those not inlined. I need to make sure I am focusing on the "right" functions. First, the original list: `xswap_create`, `xswap_destroy`, `xswap_map_clusters`, `xswap_unmap_clusters`, `xswap_try_shrink`, `xswap_update_free_tail`, `xswap_trim_free_tail`, `xswap_max_clusters_write`, `xswap_max_clusters_read`, `xswap_create_store`, `xswap_destroy_store`. The prompt emphasizes non-static, non-inlined, so I check if any of these are non-static. A quick scan reveals they are all *static* functions. That seems to go against the prompt's preference. However, given their size and usage, some of these static functions will *not* be inlined. Therefore, the initial list of potential entry points stands, assuming the compiler doesn't inline them, and includes those functions, *and* I need to investigate any *other* relevant functions. Looking more closely at the list, I see `__swapoff` is static and `swapoff` is non-static (a SYSCALL_DEFINE1 function). Also, `swapon` is non-static. So, `swapoff` and `swapon` are both non-static. So, I re-evaluate the static function list and reduce to the ones I know are important and will likely be the entry points: `xswap_create`, `xswap_destroy`, `xswap_map_clusters`, `xswap_unmap_clusters`, `xswap_try_shrink`, `xswap_create_store`, `xswap_destroy_store`, `xswap_max_clusters_write`. The functions that are entry points to the sysfs are `xswap_create_store`, `xswap_destroy_store`, and the entry point from debugfs is `xswap_max_clusters_write`. Now, I look at the call graphs for the functions. `xswap_create` is only called from `xswap_create_store`. The entry point into the sysfs is `xswap_create_store` and `xswap_destroy_store`. The entry point for the debugfs is `xswap_max_clusters_write`. `xswap_map_clusters` is called from `cluster_alloc_swap_entry` and `setup_swap_clusters_info`. `xswap_unmap_clusters` is called from `xswap_try_shrink`, `free_swap_cluster_info`, and `setup_swap_clusters_info`. `xswap_try_shrink` is called from `xswap_shrink_work_fn` and `xswap_max_clusters_write`. `xswap_update_free_tail` is called from `__free_cluster`. `xswap_trim_free_tail` is called from `__swap_cluster_alloc_entries`. Therefore, I'll list the functions again. * `xswap_create` * `xswap_destroy` * `xswap_map_clusters` * `xswap_unmap_clusters` * `xswap_try_shrink` * `xswap_update_free_tail` * `xswap_trim_free_tail` * `xswap_create_store` * `xswap_destroy_store` * `xswap_max_clusters_write` Finally, let's keep track of the relevant configurations. I see `XSWAP`, `SYSFS`, `ZSWAP`, and `ZSWAP_DEFAULT_ON`. The goal is to look at the relation between `ZSWAP` and `ZPOOL`. I know that `ZSWAP` *selects* `CRYPTO` and `ZPOOL`. Let me check the `mm/Kconfig` file again to confirm the select statements. ``` config ZSWAP bool "Compressed cache for swap pages" depends on SWAP select CRYPTO select ZPOOL ``` There it is! `ZSWAP` does select `ZPOOL`. Done. |
| 18/2 | 2026/08/27 10:01 | tool | grepper |
0mArgs:{
"Expression": "config ZSWAP",
"PathPrefix": "mm/Kconfig"
}Results: {
"Output": "mm/Kconfig=12=menuconfig SWAP\n--\nmm/Kconfig-21-\nmm/Kconfig:22:config ZSWAP\nmm/Kconfig-23-\tbool \"Compressed cache for swap pages\"\n--\nmm/Kconfig-34-\nmm/Kconfig:35:config ZSWAP_DEFAULT_ON\nmm/Kconfig-36-\tbool \"Enable the compressed cache for swap pages by default\"\n--\nmm/Kconfig-44-\nmm/Kconfig:45:config ZSWAP_SHRINKER_DEFAULT_ON\nmm/Kconfig-46-\tbool \"Shrink the zswap pool on memory pressure\"\n--\nmm/Kconfig=59=choice\n--\nmm/Kconfig-76-\nmm/Kconfig:77:config ZSWAP_COMPRESSOR_DEFAULT_DEFLATE\nmm/Kconfig-78-\tbool \"Deflate\"\n--\nmm/Kconfig-82-\nmm/Kconfig:83:config ZSWAP_COMPRESSOR_DEFAULT_LZO\nmm/Kconfig-84-\tbool \"LZO\"\n--\nmm/Kconfig-88-\nmm/Kconfig:89:config ZSWAP_COMPRESSOR_DEFAULT_842\nmm/Kconfig-90-\tbool \"842\"\n--\nmm/Kconfig-94-\nmm/Kconfig:95:config ZSWAP_COMPRESSOR_DEFAULT_LZ4\nmm/Kconfig-96-\tbool \"LZ4\"\n--\nmm/Kconfig-100-\nmm/Kconfig:101:config ZSWAP_COMPRESSOR_DEFAULT_LZ4HC\nmm/Kconfig-102-\tbool \"LZ4HC\"\n--\nmm/Kconfig-106-\nmm/Kconfig:107:config ZSWAP_COMPRESSOR_DEFAULT_ZSTD\nmm/Kconfig-108-\tbool \"zstd\"\n--\nmm/Kconfig=112=endchoice\nmm/Kconfig-113-\nmm/Kconfig:114:config ZSWAP_COMPRESSOR_DEFAULT\nmm/Kconfig-115- string\n"
} |
| 19/2 | 2026/08/27 10:01 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 37505 output: 31 thoughts: 16 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Analyzing mm/Kconfig Line 22** Alright, let's dive into `mm/Kconfig` and zero in on line 22. My initial focus is likely driven by something specific I'm trying to understand, probably related to a memory management configuration option or dependency. I'm hoping line 22 holds the key to clarifying a particular macro, condition, or default setting that's tripping me up. I'm expecting to find a conditional statement (e.g., `if`, `select`, `depends on`) or a definition that directly impacts how a specific memory management feature behaves at compile time. I'll be looking for clues about the scope of the option, any relevant dependencies it has on other features, or how it might interact with the overall kernel configuration. More specifically, I'm probably looking at the dependencies of a specific feature, how its status is determined at compile-time, and whether or not there are any compile-time side-effects (e.g. including or excluding header files) based on its selection. Understanding the context of that line should provide some insight into the broader architecture. |
| 20/2 | 2026/08/27 10:01 | tool | read-file |
0mArgs:{
"File": "mm/Kconfig",
"FirstLine": 20,
"LineCount": 15
}Results: {
"Contents": " 20:\t\t in your computer. If unsure say Y.\n 21:\t\n 22:\tconfig ZSWAP\n 23:\t\tbool \"Compressed cache for swap pages\"\n 24:\t\tdepends on SWAP\n 25:\t\tselect CRYPTO\n 26:\t\tselect ZSMALLOC\n 27:\t\thelp\n 28:\t\t A lightweight compressed cache for swap pages. It takes\n 29:\t\t pages that are in the process of being swapped out and attempts to\n 30:\t\t compress them into a dynamically allocated RAM-based memory pool.\n 31:\t\t This can result in a significant I/O reduction on swap device and,\n 32:\t\t in the case where decompressing from RAM is faster than swap device\n 33:\t\t reads, can also improve workload performance.\n 34:\t\n"
} |
| 21/2 | 2026/08/27 10:01 | llm | patch-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 38023 output: 257 thoughts: 185 Thoughts: **My Assessment of the Configuration Changes** Okay, so the system automatically chose `ZSMALLOC` – good, that simplifies things, I don't need to explicitly enable it in the configuration. That's one less thing to worry about. Now, let's add `XSWAP`, `ZSWAP`, `ZSWAP_DEFAULT_ON`, and `SYSFS`. Those seem crucial for this new feature. Let me just quickly review the rationale here. This patch introduces this "xswap" which is essentially an extendable swap device. The key is that it separates the page table entries (PTEs) that point to swapped-out pages from the physical storage itself, allowing the swap device to grow and shrink dynamically using VM_SPARSE regions for the `cluster_info` array. It’s exposed to userspace via the sysfs interface ( `/sys/kernel/mm/xswap/create` and `destroy`) and includes a debugfs interface, which is convenient for runtime size tuning. This is a substantial architectural change to how memory management and swap operate, and, crucially, it's accessible from user space. Given the user-space interaction and the core nature of these changes, this is *definitely* something that's screaming for intensive fuzzing. Right, setting the results. |
| 22/2 | 2026/08/27 10:01 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"XSWAP",
"ZSWAP",
"ZSWAP_DEFAULT_ON",
"SYSFS"
],
"FocusSymbols": [
"xswap_create",
"xswap_destroy",
"xswap_map_clusters",
"xswap_unmap_clusters",
"xswap_try_shrink",
"xswap_update_free_tail",
"xswap_trim_free_tail",
"xswap_create_store",
"xswap_destroy_store",
"xswap_max_clusters_write"
],
"Reasoning": "The patch introduces a new extendable swap device (xswap) that decouples PTE swap entries from physical backing storage. It dynamically grows and shrinks the cluster_info array via a VM_SPARSE area. The feature is exposed to userspace via sysfs (`/sys/kernel/mm/xswap/create` and `destroy`) and debugfs for runtime size tuning. This is a significant functional change to the core memory management and swap subsystems, and it is reachable from userspace, making it highly worth fuzzing.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"XSWAP",
"ZSWAP",
"ZSWAP_DEFAULT_ON",
"SYSFS"
],
"FocusSymbols": [
"xswap_create",
"xswap_destroy",
"xswap_map_clusters",
"xswap_unmap_clusters",
"xswap_try_shrink",
"xswap_update_free_tail",
"xswap_trim_free_tail",
"xswap_create_store",
"xswap_destroy_store",
"xswap_max_clusters_write"
],
"Reasoning": "The patch introduces a new extendable swap device (xswap) that decouples PTE swap entries from physical backing storage. It dynamically grows and shrinks the cluster_info array via a VM_SPARSE area. The feature is exposed to userspace via sysfs (`/sys/kernel/mm/xswap/create` and `destroy`) and debugfs for runtime size tuning. This is a significant functional change to the core memory management and swap subsystems, and it is reachable from userspace, making it highly worth fuzzing.",
"WorthFuzzing": true
} |
| 23/1 | 2026/08/27 10:01 | action | If |
0mArgs:{
"WorthFuzzing": true
}Results: null |
| 24/2 | 2026/08/27 10:01 | agent | kmsan-evaluator |
0mModel:best-expensive Args: null Results: {
"KMSANReasoning": "The patch introduces an extendable virtual swap device (xswap) and modifies swap management logic. It adds sysfs and debugfs interfaces, but all data copied to userspace is properly initialized (e.g., via `snprintf` in `xswap_max_clusters_read`). The new data structures, such as the dynamically mapped `cluster_info` pages, are explicitly allocated with `__GFP_ZERO`. The `xswap_page_data` struct used during unmapping correctly tracks the number of initialized array elements to avoid freeing uninitialized pointers. There are no info-leaks or uses of uninitialized memory introduced by this patch. Any potential memory errors like out-of-bounds accesses or use-after-free bugs in the new swap logic will be effectively caught by standard KASAN. Therefore, a dedicated KMSAN fuzzing session is not required.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: For your convenience, here is the diff of the changes:
commit 886eb2bf972c2bb8fe0af9acf11f1de148f1d3e8
Author: syz-cluster <triage@syzkaller.com>
Date: Thu Aug 27 09:59:55 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 5658a1634b85e..998f04c0b7674 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -207,6 +207,7 @@ enum {
SWP_STABLE_WRITES = (1 << 11), /* no overwrite PG_writeback pages */
SWP_SYNCHRONOUS_IO = (1 << 12), /* synchronous IO is efficient */
SWP_HIBERNATION = (1 << 13), /* pinned for hibernation */
+ SWP_XSWAP = (1 << 14), /* extendable swap device */
/* add others here before... */
};
@@ -247,6 +248,16 @@ struct swap_info_struct {
signed char type; /* strange name for an index */
unsigned int max; /* size of this swap device */
struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */
+#ifdef CONFIG_XSWAP
+ struct vm_struct *cluster_vm; /* VM_SPARSE area for xswap dynamic cluster_info */
+ unsigned long nr_clusters_max;/* upper limit from swap header */
+ unsigned long nr_clusters; /* current growth ceiling (≤ nr_clusters_max) */
+ unsigned long nr_clusters_mapped; /* currently mapped cluster count */
+ unsigned long nr_free_tail; /* contiguous free clusters at tail */
+ struct dentry *debugfs_entry; /* debugfs: type<N>_max_clusters */
+ struct work_struct xswap_shrink_work; /* deferred shrink trigger */
+ struct mutex xswap_lock; /* serialize map/unmap operations */
+#endif
struct list_head free_clusters; /* free clusters list */
struct list_head full_clusters; /* full clusters list */
struct list_head nonfull_clusters[SWAP_NR_ORDERS];
@@ -356,6 +367,7 @@ void free_folio_and_swap_cache(struct folio *folio);
void free_pages_and_swap_cache(struct encoded_page **, int);
/* linux/mm/swapfile.c */
extern atomic_long_t nr_swap_pages;
+extern atomic_t nr_real_swapfiles;
extern long total_swap_pages;
extern atomic_t nr_rotate_swap;
diff --git a/mm/Kconfig b/mm/Kconfig
index 604c58199acbf..26cf26a2c59fd 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -122,6 +122,15 @@ config ZSWAP_COMPRESSOR_DEFAULT
default "zstd" if ZSWAP_COMPRESSOR_DEFAULT_ZSTD
default ""
+config XSWAP
+ bool "Extendable (virtual) swap device"
+ depends on SWAP && 64BIT
+ help
+ Adds support for extendable swap devices (xswap) that decouple
+ PTE swap entries from physical backing storage. The cluster_info
+ array is backed by a sparse vmalloc area that grows and shrinks
+ on demand, avoiding static pre-allocation overhead.
+
config ZSMALLOC
tristate
diff --git a/mm/page_io.c b/mm/page_io.c
index 88962571cb931..5483c943e3e35 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -248,6 +248,17 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
}
rcu_read_unlock();
+ /*
+ * ctx->sis is set by swap_add_folio() which is called from
+ * __swap_writepage() below. Since we must avoid the writepage
+ * path for xswap devices, use the swap_info from the folio's
+ * swap entry directly instead of going through ctx.
+ */
+ if (unlikely(__swap_entry_to_info(folio->swap)->flags & SWP_XSWAP)) {
+ folio_mark_dirty(folio);
+ return AOP_WRITEPAGE_ACTIVATE;
+ }
+
__swap_writepage(ctx, folio);
return 0;
out_unlock:
@@ -480,6 +491,11 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
if (zswap_load(folio) != -ENOENT)
goto finish;
+ if (unlikely(sis->flags & SWP_XSWAP)) {
+ folio_unlock(folio);
+ goto finish;
+ }
+
/* We have to read from slower devices. Increase zswap protection. */
zswap_folio_swapin(folio);
swap_add_folio(ctx, folio, READ);
diff --git a/mm/swap_state.c b/mm/swap_state.c
index b76eb3d876fd7..2eedb7a3d7bb2 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -830,6 +830,13 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
struct blk_plug plug;
swp_entry_t ra_entry;
+ /*
+ * The entry may have been freed by another task. Avoid swap_info_get()
+ * which will print error message if the race happens.
+ */
+ if (si->flags & SWP_XSWAP)
+ goto skip;
+
mask = swapin_nr_pages(offset) - 1;
if (!mask)
goto skip;
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 53bf01d5f7f11..258b52bce4389 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -16,6 +16,8 @@
#include <linux/kernel_stat.h>
#include <linux/swap.h>
#include <linux/vmalloc.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
#include <linux/pagemap.h>
#include <linux/namei.h>
#include <linux/shmem_fs.h>
@@ -48,6 +50,213 @@
#include "swap_table.h"
#include "internal.h"
#include "swap.h"
+#include <linux/debugfs.h>
+
+#define DEF_SWAP_PRIO -1
+
+static DEFINE_SPINLOCK(swap_lock);
+
+#ifdef CONFIG_XSWAP
+/*
+ * xswap: dynamically grow and shrink the cluster_info array via a
+ * VM_SPARSE area.
+ *
+ * XSWAP_GROW_CLUSTERS is the number of clusters to map/unmap in one
+ * grow/shrink operation. It is set to the number of cluster_info
+ * structs that fit in a single page (at least 16), so that the vmalloc
+ * page table
+ * overhead is proportional to the number of clusters mapped.
+ */
+#define XSWAP_GROW_CLUSTERS \
+ max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16)
+#define XSWAP_DEFAULT_CLUSTER_PERCENT 30
+
+static struct dentry *xswap_debugfs_root;
+
+static int xswap_map_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr);
+static void xswap_unmap_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr);
+static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data);
+static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx);
+static void xswap_update_free_tail(struct swap_info_struct *si,
+ unsigned long freed_idx);
+static void xswap_try_shrink(struct swap_info_struct *si);
+
+/*
+ * debugfs read/write for per-device max cluster count.
+ */
+static ssize_t xswap_max_clusters_read(struct file *file, char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ struct swap_info_struct *si = file->private_data;
+ char tmp[32];
+ int len;
+
+ len = snprintf(tmp, sizeof(tmp), "%lu\n", READ_ONCE(si->nr_clusters));
+ return simple_read_from_buffer(buf, count, ppos, tmp, len);
+}
+
+static ssize_t xswap_max_clusters_write(struct file *file,
+ const char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ struct swap_info_struct *si = file->private_data;
+ unsigned long val, new_pages;
+ int err;
+
+ err = kstrtoul_from_user(buf, count, 0, &val);
+ if (err)
+ return err;
+
+ if (val > si->nr_clusters_max)
+ val = si->nr_clusters_max;
+
+ spin_lock(&si->lock);
+ si->nr_clusters = val;
+ spin_unlock(&si->lock);
+
+ new_pages = min_t(unsigned long, val * SWAPFILE_CLUSTER, si->max);
+ if (new_pages)
+ new_pages--;
+ if (new_pages != si->pages) {
+ long delta = (long)new_pages - (long)si->pages;
+
+ spin_lock(&swap_lock);
+ si->pages = new_pages;
+ atomic_long_add(delta, &nr_swap_pages);
+ total_swap_pages += delta;
+ spin_unlock(&swap_lock);
+ }
+
+ /* Lowering the ceiling may free tail clusters. */
+ xswap_try_shrink(si);
+
+ return count;
+}
+
+static const struct file_operations xswap_debugfs_fops = {
+ .read = xswap_max_clusters_read,
+ .write = xswap_max_clusters_write,
+ .open = simple_open,
+ .llseek = default_llseek,
+};
+
+static void xswap_debugfs_add(struct swap_info_struct *si)
+{
+ char name[32];
+
+ if (!xswap_debugfs_root)
+ return;
+
+ snprintf(name, sizeof(name), "type%d_cluster_limit", si->type);
+ si->debugfs_entry = debugfs_create_file(name, 0644, xswap_debugfs_root,
+ si, &xswap_debugfs_fops);
+}
+
+static void xswap_debugfs_del(struct swap_info_struct *si)
+{
+ debugfs_remove(si->debugfs_entry);
+ si->debugfs_entry = NULL;
+}
+
+#ifdef CONFIG_SYSFS
+static int xswap_create(int percent, int prio);
+static int xswap_destroy(int type);
+/* /sys/kernel/mm/xswap/: create.
+ * Write "<percent> [<prio>]" to add a zswap-backed swap device; a missing
+ * priority means DEF_SWAP_PRIO.
+ * Per-device runtime size is tuned via debugfs type<N>_cluster_limit.
+ */
+
+static ssize_t xswap_create_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ long percent;
+ int prio = DEF_SWAP_PRIO;
+ int nr, err;
+
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ /* "<percent> [<prio>]"; a missing prio means DEF_SWAP_PRIO.
+ * %li keeps kstrtoul(buf, 0)'s base-0 behavior for percent.
+ */
+ nr = sscanf(buf, "%li %d", &percent, &prio);
+ if (nr < 1)
+ return -EINVAL;
+
+ /* 0 means "use the default percent" */
+ if (percent == 0)
+ percent = XSWAP_DEFAULT_CLUSTER_PERCENT;
+
+ err = xswap_create(percent, prio);
+ if (err < 0)
+ return err;
+
+ return count;
+}
+
+static struct kobj_attribute xswap_create_attr = __ATTR(create, 0200, NULL,
+ xswap_create_store);
+
+static ssize_t xswap_destroy_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ unsigned long type;
+ int err;
+
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ err = kstrtoul(buf, 0, &type);
+ if (err)
+ return err;
+
+ err = xswap_destroy(type);
+ if (err)
+ return err;
+
+ return count;
+}
+
+static struct kobj_attribute xswap_destroy_attr = __ATTR(destroy, 0200, NULL,
+ xswap_destroy_store);
+
+static struct attribute *xswap_attrs[] = {
+ &xswap_create_attr.attr,
+ &xswap_destroy_attr.attr,
+ NULL,
+};
+
+static const struct attribute_group xswap_attr_group = {
+ .attrs = xswap_attrs,
+};
+
+static struct kobject *xswap_kobj;
+
+static void xswap_sysfs_init(void)
+{
+ xswap_kobj = kobject_create_and_add("xswap", mm_kobj);
+ if (!xswap_kobj) {
+ pr_err("xswap: failed to create sysfs kobject\n");
+ return;
+ }
+ if (sysfs_create_group(xswap_kobj, &xswap_attr_group))
+ pr_err("xswap: failed to create sysfs group\n");
+}
+#else
+static inline void xswap_sysfs_init(void)
+{
+}
+#endif /* CONFIG_SYSFS */
+#else /* !CONFIG_XSWAP */
+static inline void xswap_sysfs_init(void)
+{
+}
+#endif /* CONFIG_XSWAP */
static void swap_range_alloc(struct swap_info_struct *si,
unsigned int nr_entries);
@@ -63,9 +272,9 @@ static void move_cluster(struct swap_info_struct *si,
*
* Also protects swap_active_head total_swap_pages, and the SWP_WRITEOK flag.
*/
-static DEFINE_SPINLOCK(swap_lock);
static unsigned int nr_swapfiles;
atomic_long_t nr_swap_pages;
+atomic_t nr_real_swapfiles;
/*
* Some modules use swappable objects and may try to swap them out under
* memory pressure (via the shrinker). Before doing so, they may wish to
@@ -74,7 +283,6 @@ atomic_long_t nr_swap_pages;
EXPORT_SYMBOL_GPL(nr_swap_pages);
/* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
long total_swap_pages;
-#define DEF_SWAP_PRIO -1
unsigned long swapfile_maximum_size;
#ifdef CONFIG_MIGRATION
bool swap_migration_ad_supported;
@@ -609,6 +817,10 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info
swap_cluster_free_table(ci);
move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
ci->order = 0;
+#ifdef CONFIG_XSWAP
+ xswap_update_free_tail(si, ci - si->cluster_info);
+ schedule_work(&si->xswap_shrink_work);
+#endif
}
/*
@@ -955,6 +1167,9 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si,
if (cluster_is_empty(ci))
ci->order = order;
ci->count += nr_pages;
+#ifdef CONFIG_XSWAP
+ xswap_trim_free_tail(si, cluster_index(si, ci));
+#endif
swap_range_alloc(si, nr_pages);
return true;
@@ -1185,6 +1400,51 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
if (found)
goto done;
}
+
+#ifdef CONFIG_XSWAP
+ /*
+ * For xswap: if no free cluster was found and more clusters
+ * can be mapped, grow the cluster_info array and retry.
+ */
+ if (!found && (si->flags & SWP_XSWAP) &&
+ READ_ONCE(si->nr_clusters_mapped) < READ_ONCE(si->nr_clusters) &&
+ list_empty(&si->free_clusters)) {
+ unsigned long nr_new = min(READ_ONCE(si->nr_clusters) -
+ READ_ONCE(si->nr_clusters_mapped),
+ XSWAP_GROW_CLUSTERS);
+ unsigned long start = READ_ONCE(si->nr_clusters_mapped);
+ unsigned long i;
+
+ if (!xswap_map_clusters(si, start, nr_new)) {
+ unsigned long added = 0;
+
+ for (i = start; i < start + nr_new; i++) {
+ struct swap_cluster_info *ci = &si->cluster_info[i];
+
+ /*
+ * A concurrent grower may have already added
+ * these clusters to the free list. Only add
+ * clusters that are still off-list (NONE).
+ * Lock ci->lock first: move_cluster() takes
+ * si->lock internally.
+ */
+ spin_lock(&ci->lock);
+ if (ci->flags == CLUSTER_FLAG_NONE) {
+ move_cluster(si, ci, &si->free_clusters,
+ CLUSTER_FLAG_FREE);
+ added++;
+ }
+ spin_unlock(&ci->lock);
+ }
+ WRITE_ONCE(si->nr_free_tail,
+ READ_ONCE(si->nr_free_tail) + added);
+
+ /* Retry allocation from the free list */
+ found = alloc_swap_scan_list(si, &si->free_clusters,
+ folio, false);
+ }
+ }
+#endif
done:
if (!(si->flags & SWP_SOLIDSTATE))
spin_unlock(&si->global_cluster_lock);
@@ -1223,6 +1483,8 @@ static void del_from_avail_list(struct swap_info_struct *si, bool swapoff)
goto skip;
}
+ if (!(si->flags & SWP_XSWAP))
+ atomic_sub(1, &nr_real_swapfiles);
plist_del(&si->avail_list, &swap_avail_head);
skip:
@@ -1265,6 +1527,8 @@ static void add_to_avail_list(struct swap_info_struct *si, bool swapon)
}
plist_add(&si->avail_list, &swap_avail_head);
+ if (!(si->flags & SWP_XSWAP))
+ atomic_add(1, &nr_real_swapfiles);
skip:
spin_unlock(&swap_avail_lock);
@@ -2710,15 +2974,27 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
unsigned int prev)
{
unsigned int i;
+ unsigned int end = si->max;
unsigned long swp_tb;
+#ifdef CONFIG_XSWAP
+ /* xswap may have shrunk and unmapped the cluster_info tail. */
+ if (si->flags & SWP_XSWAP) {
+ unsigned long mapped_end;
+
+ mapped_end = READ_ONCE(si->nr_clusters_mapped) * SWAPFILE_CLUSTER;
+ if (mapped_end < end)
+ end = mapped_end;
+ }
+#endif
+
/*
* No need for swap_lock here: we're just looking
* for whether an entry is in use, not modifying it; false
* hits are okay, and sys_swapoff() has already prevented new
* allocations from this area (while holding swap_lock).
*/
- for (i = prev + 1; i < si->max; i++) {
+ for (i = prev + 1; i < end; i++) {
swp_tb = swap_table_get(__swap_offset_to_cluster(si, i),
i % SWAPFILE_CLUSTER);
if (!swp_tb_is_null(swp_tb) && !swp_tb_is_bad(swp_tb))
@@ -2727,7 +3003,7 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
cond_resched();
}
- if (i == si->max)
+ if (i == end)
i = 0;
return i;
@@ -2959,6 +3235,19 @@ static int setup_swap_extents(struct swap_info_struct *sis,
struct inode *inode = mapping->host;
int ret;
+ if (sis->flags & SWP_XSWAP) {
+ *span = 0;
+ /*
+ * xswap devices have no backing block device and
+ * physical writeout is skipped in swap_writeout(),
+ * but sis->ops must still be set so that callers
+ * like shrink_folio_list() can safely dereference
+ * ops->flags.
+ */
+ sis->ops = &swap_bdev_ops;
+ return 0;
+ }
+
ret = sio_pool_init();
if (ret)
return ret;
@@ -3030,20 +3319,46 @@ static void wait_for_allocation(struct swap_info_struct *si)
BUG_ON(si->flags & SWP_WRITEOK);
+#ifdef CONFIG_XSWAP
+ /* Skip shrinker-unmapped cluster tail. */
+ if (si->flags & SWP_XSWAP)
+ end = min(end, READ_ONCE(si->nr_clusters_mapped) *
+ SWAPFILE_CLUSTER);
+#endif
+
for (offset = 0; offset < end; offset += SWAPFILE_CLUSTER) {
ci = swap_cluster_lock(si, offset);
swap_cluster_unlock(ci);
}
}
-static void free_swap_cluster_info(struct swap_cluster_info *cluster_info,
- unsigned long maxpages)
+static void free_swap_cluster_info(struct swap_info_struct *si)
{
+ struct swap_cluster_info *cluster_info = si->cluster_info;
+ unsigned long maxpages = si->max;
struct swap_cluster_info *ci;
- int i, nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
+ int i, nr_clusters;
if (!cluster_info)
return;
+
+#ifdef CONFIG_XSWAP
+ if (si->flags & SWP_XSWAP) {
+ xswap_debugfs_del(si);
+ cancel_work_sync(&si->xswap_shrink_work);
+ /* Unmap all mapped clusters and free the VM_SPARSE area */
+ if (si->nr_clusters_mapped > 0)
+ xswap_unmap_clusters(si, 0, si->nr_clusters_mapped);
+ free_vm_area(si->cluster_vm);
+ si->cluster_vm = NULL;
+ si->nr_clusters_max = 0;
+ si->nr_clusters = 0;
+ si->nr_clusters_mapped = 0;
+ return;
+ }
+#endif
+
+ nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
for (i = 0; i < nr_clusters; i++) {
ci = cluster_info + i;
/* Cluster with bad marks count will have a remaining table */
@@ -3079,63 +3394,13 @@ static void flush_percpu_swap_cluster(struct swap_info_struct *si)
}
-SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
+/* Common swap teardown after list removal; shared by sys_swapoff() and
+ * xswap_destroy().
+ */
+static int __swapoff(struct swap_info_struct *p)
{
- struct swap_info_struct *p = NULL;
- struct swap_cluster_info *cluster_info;
- struct file *swap_file, *victim;
- struct address_space *mapping;
- struct inode *inode;
- unsigned int maxpages;
- int err, found = 0;
-
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
-
- BUG_ON(!current->mm);
-
- CLASS(filename, pathname)(specialfile);
- victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
- if (IS_ERR(victim))
- return PTR_ERR(victim);
-
- mapping = victim->f_mapping;
- spin_lock(&swap_lock);
- plist_for_each_entry(p, &swap_active_head, list) {
- if (p->flags & SWP_WRITEOK) {
- if (p->swap_file->f_mapping == mapping) {
- found = 1;
- break;
- }
- }
- }
- if (!found) {
- err = -EINVAL;
- spin_unlock(&swap_lock);
- goto out_dput;
- }
-
- /* Refuse swapoff while the device is pinned for hibernation */
- if (p->flags & SWP_HIBERNATION) {
- err = -EBUSY;
- spin_unlock(&swap_lock);
- goto out_dput;
- }
-
- if (!security_vm_enough_memory_mm(current->mm, p->pages))
- vm_unacct_memory(p->pages);
- else {
- err = -ENOMEM;
- spin_unlock(&swap_lock);
- goto out_dput;
- }
- spin_lock(&p->lock);
- del_from_avail_list(p, true);
- plist_del(&p->list, &swap_active_head);
- atomic_long_sub(p->pages, &nr_swap_pages);
- total_swap_pages -= p->pages;
- spin_unlock(&p->lock);
- spin_unlock(&swap_lock);
+ struct file *swap_file = NULL;
+ int err;
wait_for_allocation(p);
@@ -3146,7 +3411,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
if (err) {
/* re-insert swap space back into swap_list */
reinsert_swap_info(p);
- goto out_dput;
+ return err;
}
/*
@@ -3167,7 +3432,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
destroy_swap_extents(p, p->swap_file);
- if (!(p->flags & SWP_SOLIDSTATE))
+ if (!(p->flags & SWP_XSWAP) &&
+ !(p->flags & SWP_SOLIDSTATE))
atomic_dec(&nr_rotate_swap);
mutex_lock(&swapon_mutex);
@@ -3177,10 +3443,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
swap_file = p->swap_file;
p->swap_file = NULL;
- maxpages = p->max;
- cluster_info = p->cluster_info;
- p->max = 0;
- p->cluster_info = NULL;
spin_unlock(&p->lock);
spin_unlock(&swap_lock);
arch_swap_invalidate_area(p->type);
@@ -3188,14 +3450,18 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
mutex_unlock(&swapon_mutex);
kfree(p->global_cluster);
p->global_cluster = NULL;
- free_swap_cluster_info(cluster_info, maxpages);
+ free_swap_cluster_info(p);
+ p->max = 0;
+ p->cluster_info = NULL;
- inode = mapping->host;
+ if (swap_file) {
+ struct inode *inode = swap_file->f_mapping->host;
- inode_lock(inode);
- inode->i_flags &= ~S_SWAPFILE;
- inode_unlock(inode);
- filp_close(swap_file, NULL);
+ inode_lock(inode);
+ inode->i_flags &= ~S_SWAPFILE;
+ inode_unlock(inode);
+ filp_close(swap_file, NULL);
+ }
/*
* Clear the SWP_USED flag after all resources are freed so that swapon
@@ -3206,10 +3472,69 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
p->flags = 0;
spin_unlock(&swap_lock);
- err = 0;
atomic_inc(&proc_poll_event);
wake_up_interruptible(&proc_poll_wait);
+ return 0;
+}
+
+SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
+{
+ struct swap_info_struct *p = NULL;
+ struct file *victim;
+ struct address_space *mapping;
+ int err, found = 0;
+
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ BUG_ON(!current->mm);
+
+ CLASS(filename, pathname)(specialfile);
+ victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
+ if (IS_ERR(victim))
+ return PTR_ERR(victim);
+
+ mapping = victim->f_mapping;
+ spin_lock(&swap_lock);
+ plist_for_each_entry(p, &swap_active_head, list) {
+ if (p->flags & SWP_WRITEOK) {
+ if (p->swap_file && p->swap_file->f_mapping == mapping) {
+ found = 1;
+ break;
+ }
+ }
+ }
+ if (!found) {
+ err = -EINVAL;
+ spin_unlock(&swap_lock);
+ goto out_dput;
+ }
+
+ /* Refuse swapoff while the device is pinned for hibernation */
+ if (p->flags & SWP_HIBERNATION) {
+ err = -EBUSY;
+ spin_unlock(&swap_lock);
+ goto out_dput;
+ }
+
+ if (!security_vm_enough_memory_mm(current->mm, p->pages))
+ vm_unacct_memory(p->pages);
+ else {
+ err = -ENOMEM;
+ spin_unlock(&swap_lock);
+ goto out_dput;
+ }
+ spin_lock(&p->lock);
+ del_from_avail_list(p, true);
+ plist_del(&p->list, &swap_active_head);
+ atomic_long_sub(p->pages, &nr_swap_pages);
+ total_swap_pages -= p->pages;
+ spin_unlock(&p->lock);
+ spin_unlock(&swap_lock);
+
+ err = __swapoff(p);
+
out_dput:
filp_close(victim, NULL);
return err;
@@ -3243,7 +3568,7 @@ static void *swap_start(struct seq_file *swap, loff_t *pos)
return SEQ_START_TOKEN;
for (type = 0; (si = swap_type_to_info(type)); type++) {
- if (!(si->swap_file))
+ if (!(si->swap_file) && !(si->flags & SWP_XSWAP))
continue;
if (!--l)
return si;
@@ -3264,7 +3589,7 @@ static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
++(*pos);
for (; (si = swap_type_to_info(type)); type++) {
- if (!(si->swap_file))
+ if (!(si->swap_file) && !(si->flags & SWP_XSWAP))
continue;
return si;
}
@@ -3277,6 +3602,19 @@ static void swap_stop(struct seq_file *swap, void *v)
mutex_unlock(&swapon_mutex);
}
+static const char *swap_type_str(struct swap_info_struct *si)
+{
+ struct file *file = si->swap_file;
+
+ if (si->flags & SWP_XSWAP)
+ return "xswap\t";
+
+ if (S_ISBLK(file_inode(file)->i_mode))
+ return "partition";
+
+ return "file\t";
+}
+
static int swap_show(struct seq_file *swap, void *v)
{
struct swap_info_struct *si = v;
@@ -3293,11 +3631,17 @@ static int swap_show(struct seq_file *swap, void *v)
inuse = K(swap_usage_in_pages(si));
file = si->swap_file;
- len = seq_file_path(swap, file, " \t\n\\");
+ if (file)
+ len = seq_file_path(swap, file, " \t\n\\");
+ else {
+ char name[16];
+
+ len = scnprintf(name, sizeof(name), "xswap%d", si->type);
+ seq_puts(swap, name);
+ }
seq_printf(swap, "%*s%s\t%lu\t%s%lu\t%s%d\n",
len < 40 ? 40 - len : 1, " ",
- S_ISBLK(file_inode(file)->i_mode) ?
- "partition" : "file\t",
+ swap_type_str(si),
bytes, bytes < 10000000 ? "\t" : "",
inuse, inuse < 10000000 ? "\t" : "",
si->prio);
@@ -3530,6 +3874,326 @@ static unsigned long read_swap_header(struct swap_info_struct *si,
return maxpages;
}
+#ifdef CONFIG_XSWAP
+static int xswap_map_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr)
+{
+ unsigned long start_addr = (unsigned long)si->cluster_info +
+ (size_t)start_idx * sizeof(struct swap_cluster_info);
+ unsigned long end_addr = start_addr + (size_t)nr * sizeof(struct swap_cluster_info);
+ /* Round to page boundaries for vm_area_map_pages(). */
+ unsigned long vm_start = PAGE_ALIGN(start_addr);
+ unsigned long vm_end = PAGE_ALIGN(end_addr);
+ unsigned int noreclaim_flags;
+ unsigned long npages;
+ struct page **pages;
+ unsigned long i;
+ int err;
+
+ mutex_lock(&si->xswap_lock);
+
+ if (vm_start >= vm_end) {
+ /* All requested clusters fall within already-mapped pages. */
+ for (i = start_idx; i < start_idx + nr; i++)
+ spin_lock_init(&si->cluster_info[i].lock);
+ WRITE_ONCE(si->nr_clusters_mapped, start_idx + nr);
+ mutex_unlock(&si->xswap_lock);
+ return 0;
+ }
+
+ npages = (vm_end - vm_start) >> PAGE_SHIFT;
+
+ /* Prevent recursive reclaim during vmap page table allocation. */
+ noreclaim_flags = memalloc_noreclaim_save();
+
+ pages = kmalloc_array(npages, sizeof(*pages),
+ __GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL);
+ if (!pages) {
+ memalloc_noreclaim_restore(noreclaim_flags);
+ mutex_unlock(&si->xswap_lock);
+ return -ENOMEM;
+ }
+
+ for (i = 0; i < npages; i++) {
+ /* __GFP_ZERO: cluster_info pointer fields must start NULL. */
+ pages[i] = alloc_page(__GFP_HIGH | __GFP_NOMEMALLOC |
+ GFP_KERNEL | __GFP_ZERO);
+ if (!pages[i])
+ goto fail;
+ }
+
+ /* Detect racing grower that already mapped these pages. */
+ if (apply_to_existing_page_range(&init_mm, vm_start,
+ vm_end - vm_start,
+ xswap_check_mapped, NULL)) {
+ i = npages;
+ goto fail_nounmap;
+ }
+
+ err = vm_area_map_pages(si->cluster_vm, vm_start, vm_end, pages);
+ if (err) {
+ /* -EBUSY: defensive, the page was already mapped. */
+ if (err == -EBUSY) {
+ i = npages;
+ goto fail_nounmap;
+ }
+ i = npages;
+ goto fail;
+ }
+
+ kfree(pages);
+ memalloc_noreclaim_restore(noreclaim_flags);
+
+ /* Initialize spinlocks for newly mapped clusters */
+ for (i = start_idx; i < start_idx + nr; i++)
+ spin_lock_init(&si->cluster_info[i].lock);
+
+ /*
+ * Pairs with READ_ONCE() in shrink/grow paths.
+ */
+ WRITE_ONCE(si->nr_clusters_mapped, start_idx + nr);
+ mutex_unlock(&si->xswap_lock);
+ return 0;
+
+fail_nounmap:
+ /*
+ * The concurrent grower already mapped the range, initialized the
+ * cluster spinlocks and advanced nr_clusters_mapped. It may still
+ * be holding those locks while adding clusters to the free list, so
+ * do not touch them here; just free our unused pages.
+ */
+ while (i > 0) {
+ i--;
+ if (pages[i])
+ __free_page(pages[i]);
+ }
+ kfree(pages);
+ memalloc_noreclaim_restore(noreclaim_flags);
+ mutex_unlock(&si->xswap_lock);
+ return 0;
+
+fail:
+ while (i > 0) {
+ i--;
+ if (pages[i])
+ __free_page(pages[i]);
+ }
+ memalloc_noreclaim_restore(noreclaim_flags);
+ kfree(pages);
+ mutex_unlock(&si->xswap_lock);
+ return -ENOMEM;
+}
+
+struct xswap_page_data {
+ struct page **pages;
+ int nr;
+ int max;
+};
+
+static int xswap_collect_page(pte_t *pte, unsigned long addr, void *data)
+{
+ struct xswap_page_data *xpd = data;
+
+ if (!pte_present(*pte))
+ return 0;
+ if (xpd->nr < xpd->max)
+ xpd->pages[xpd->nr++] = pte_page(*pte);
+ return 0;
+}
+
+static void xswap_unmap_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr)
+{
+ unsigned long start_addr = (unsigned long)si->cluster_info +
+ (size_t)start_idx * sizeof(struct swap_cluster_info);
+ unsigned long end_addr = start_addr + (size_t)nr * sizeof(struct swap_cluster_info);
+ /* Round to page boundaries for vm_area_unmap_pages(). */
+ unsigned long vm_start = PAGE_ALIGN(start_addr);
+ unsigned long vm_end = PAGE_ALIGN(end_addr);
+ unsigned long size;
+ unsigned long npages;
+ struct xswap_page_data xpd;
+ int i;
+
+ mutex_lock(&si->xswap_lock);
+
+ if (vm_start >= vm_end) {
+ WRITE_ONCE(si->nr_clusters_mapped, start_idx);
+ mutex_unlock(&si->xswap_lock);
+ return;
+ }
+
+ size = vm_end - vm_start;
+ npages = size >> PAGE_SHIFT;
+
+ xpd.pages = kmalloc_array(npages, sizeof(*xpd.pages), GFP_KERNEL);
+ if (xpd.pages) {
+ xpd.nr = 0;
+ xpd.max = npages;
+ apply_to_existing_page_range(&init_mm, vm_start, size,
+ xswap_collect_page, &xpd);
+ }
+
+ vm_area_unmap_pages(si->cluster_vm, vm_start, vm_end);
+
+ /* Free the collected backing pages */
+ if (xpd.pages) {
+ for (i = 0; i < xpd.nr; i++)
+ __free_page(xpd.pages[i]);
+ kfree(xpd.pages);
+ }
+
+ /* Pairs with READ_ONCE() in shrink/grow paths. */
+ WRITE_ONCE(si->nr_clusters_mapped, start_idx);
+ mutex_unlock(&si->xswap_lock);
+}
+
+/* Return 1 at first present PTE to signal range is already mapped. */
+static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data)
+{
+ return 1;
+}
+
+/*
+ * Maintain si->nr_free_tail, the number of contiguous free clusters at
+ * the tail of the mapped range. Called when a cluster at @freed_idx is
+ * freed. Provides O(1) shrink detection: if nr_free_tail is non-zero,
+ * the tail can be unmapped without scanning cluster_info[].
+ *
+ * Only increments when @freed_idx is the cluster immediately before the
+ * existing tail region. Then scans backwards for already-free clusters
+ * now connected to the tail, bounded by XSWAP_GROW_CLUSTERS at a time.
+ */
+static void xswap_update_free_tail(struct swap_info_struct *si,
+ unsigned long freed_idx)
+{
+ unsigned long nr_mapped, nr_tail, tid, i;
+ struct swap_cluster_info *ci;
+
+ if (!(si->flags & SWP_XSWAP))
+ return;
+
+ nr_mapped = READ_ONCE(si->nr_clusters_mapped);
+ nr_tail = READ_ONCE(si->nr_free_tail);
+
+ /* Protect against concurrent shrink that races past us */
+ if (nr_tail >= nr_mapped)
+ return;
+
+ tid = nr_mapped - nr_tail - 1;
+
+ /* Only the cluster immediately before the tail region counts */
+ if (freed_idx != tid)
+ return;
+
+ nr_tail++;
+ WRITE_ONCE(si->nr_free_tail, nr_tail);
+
+ /* Extend: include already-free clusters now connected to the tail */
+ for (i = 1; i < XSWAP_GROW_CLUSTERS; i++) {
+ nr_mapped = READ_ONCE(si->nr_clusters_mapped);
+ nr_tail = READ_ONCE(si->nr_free_tail);
+ if (nr_tail >= nr_mapped - 1)
+ break; /* reached cluster 0 */
+ tid = nr_mapped - nr_tail - 1;
+ ci = &si->cluster_info[tid];
+
+ if (READ_ONCE(ci->count) ||
+ READ_ONCE(ci->flags) != CLUSTER_FLAG_FREE)
+ break;
+ nr_tail++;
+ WRITE_ONCE(si->nr_free_tail, nr_tail);
+ }
+}
+
+/*
+ * Trim si->nr_free_tail when a cluster in the tail region is allocated.
+ * @idx: index of the cluster being allocated.
+ */
+static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx)
+{
+ unsigned long nr_mapped, nr_tail, tail_start;
+
+ if (!(si->flags & SWP_XSWAP))
+ return;
+
+ /*
+ * nr_clusters_mapped and nr_free_tail are read locklessly;
+ * concurrent updates may cause nr_free_tail to be trimmed
+ * slightly less than ideally, which is harmless.
+ */
+ nr_mapped = READ_ONCE(si->nr_clusters_mapped);
+ nr_tail = READ_ONCE(si->nr_free_tail);
+ tail_start = nr_mapped - nr_tail;
+ if (idx >= tail_start)
+ WRITE_ONCE(si->nr_free_tail, nr_mapped - idx - 1);
+}
+
+static void xswap_shrink_work_fn(struct work_struct *work)
+{
+ struct swap_info_struct *si = container_of(work,
+ struct swap_info_struct, xswap_shrink_work);
+ xswap_try_shrink(si);
+}
+
+/*
+ * Try to shrink the cluster_info tail. Uses si->nr_free_tail which
+ * is maintained incrementally during alloc/free — no scanning needed.
+ */
+static void xswap_try_shrink(struct swap_info_struct *si)
+{
+ unsigned long nr_mapped, nr_ceiling, nr_tail, nr_unmap;
+ unsigned long start_idx, i;
+ struct swap_cluster_info *ci;
+
+ if (!(si->flags & SWP_XSWAP))
+ return;
+
+ nr_mapped = READ_ONCE(si->nr_clusters_mapped);
+ nr_ceiling = READ_ONCE(si->nr_clusters);
+ nr_tail = READ_ONCE(si->nr_free_tail);
+
+ if (nr_mapped <= nr_ceiling)
+ return;
+ if (nr_tail < XSWAP_GROW_CLUSTERS)
+ return;
+
+ nr_unmap = min(round_down(nr_tail, XSWAP_GROW_CLUSTERS),
+ nr_mapped - nr_ceiling);
+ if (nr_unmap < XSWAP_GROW_CLUSTERS)
+ return;
+ start_idx = nr_mapped - nr_unmap;
+
+ /*
+ * Verify the tail clusters are still free before unmapping. Count the
+ * contiguous free run first, then detach it in a second pass once the
+ * whole run is confirmed long enough. Detaching while counting would
+ * leave a partial run off the free list if it proved too short to unmap.
+ */
+ spin_lock(&si->lock);
+ for (i = start_idx; i < nr_mapped; i++) {
+ ci = &si->cluster_info[i];
+ if (ci->flags != CLUSTER_FLAG_FREE)
+ break;
+ }
+ nr_unmap = i - start_idx;
+ if (nr_unmap < XSWAP_GROW_CLUSTERS) {
+ spin_unlock(&si->lock);
+ return;
+ }
+
+ for (i = start_idx; i < start_idx + nr_unmap; i++) {
+ ci = &si->cluster_info[i];
+ list_del_init(&ci->list);
+ ci->flags = CLUSTER_FLAG_NONE;
+ }
+ spin_unlock(&si->lock);
+
+ xswap_unmap_clusters(si, start_idx, nr_unmap);
+ WRITE_ONCE(si->nr_free_tail, nr_tail - nr_unmap);
+}
+#endif /* CONFIG_XSWAP */
+
static int setup_swap_clusters_info(struct swap_info_struct *si,
union swap_header *swap_header,
unsigned long maxpages)
@@ -3539,10 +4203,78 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
int err = -ENOMEM;
unsigned long i;
+#ifdef CONFIG_XSWAP
+ if (si->flags & SWP_XSWAP) {
+ unsigned long size = PAGE_ALIGN(nr_clusters * sizeof(*cluster_info));
+ struct vm_struct *vm;
+
+ vm = get_vm_area(size, VM_SPARSE);
+ if (!vm)
+ goto err;
+
+ cluster_info = vm->addr;
+ si->cluster_vm = vm;
+ si->nr_clusters_max = nr_clusters;
+ si->nr_clusters = nr_clusters;
+ si->cluster_info = cluster_info;
+
+ /* Must be initialized before xswap_map_clusters() locks it. */
+ mutex_init(&si->xswap_lock);
+
+ /* Map the initial chunk (at least cluster 0) */
+ if (xswap_map_clusters(si, 0, min_t(unsigned long,
+ XSWAP_GROW_CLUSTERS, nr_clusters)))
+ goto err_free_vm;
+
+ /* xswap: only cluster 0 slot 0 is bad */
+ err = swap_cluster_setup_bad_slot(si, cluster_info, 0, false);
+ if (err)
+ goto err_unmap;
+
+ INIT_LIST_HEAD(&si->free_clusters);
+ INIT_LIST_HEAD(&si->full_clusters);
+ INIT_LIST_HEAD(&si->discard_clusters);
+ for (i = 0; i < SWAP_NR_ORDERS; i++) {
+ INIT_LIST_HEAD(&si->nonfull_clusters[i]);
+ INIT_LIST_HEAD(&si->frag_clusters[i]);
+ }
+
+ /* Mark mapped clusters: cluster 0 has 1 bad slot, rest free */
+ for (i = 0; i < si->nr_clusters_mapped; i++) {
+ struct swap_cluster_info *ci = &cluster_info[i];
+
+ if (i == 0) {
+ ci->flags = CLUSTER_FLAG_NONFULL;
+ list_add_tail(&ci->list, &si->nonfull_clusters[0]);
+ } else {
+ ci->flags = CLUSTER_FLAG_FREE;
+ list_add_tail(&ci->list, &si->free_clusters);
+ }
+ }
+
+ /* All mapped clusters except cluster 0 are free at the tail */
+ si->nr_free_tail = si->nr_clusters_mapped - 1;
+
+ INIT_WORK(&si->xswap_shrink_work, xswap_shrink_work_fn);
+ xswap_debugfs_add(si);
+ return 0;
+
+err_unmap:
+ xswap_unmap_clusters(si, 0, si->nr_clusters_mapped);
+err_free_vm:
+ free_vm_area(si->cluster_vm);
+ si->cluster_vm = NULL;
+ si->cluster_info = NULL;
+ return err;
+ }
+#endif /* CONFIG_XSWAP */
+
cluster_info = kvzalloc_objs(*cluster_info, nr_clusters);
if (!cluster_info)
goto err;
+ si->cluster_info = cluster_info;
+
for (i = 0; i < nr_clusters; i++)
spin_lock_init(&cluster_info[i].lock);
@@ -3606,10 +4338,132 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
si->cluster_info = cluster_info;
return 0;
err:
- free_swap_cluster_info(cluster_info, maxpages);
+ free_swap_cluster_info(si);
return err;
}
+#ifdef CONFIG_XSWAP
+#ifdef CONFIG_SYSFS
+/* Create a file-less xswap device. si->max = full RAM; @percent sets the
+ * runtime nr_clusters ceiling.
+ */
+static int xswap_create(int percent, int prio)
+{
+ struct swap_info_struct *si;
+ unsigned long ram, maxpages, init_clusters;
+ int error;
+
+ if (percent < 1 || percent > 100)
+ return -EINVAL;
+ if (prio != DEF_SWAP_PRIO && (prio < 0 || prio > SWAP_FLAG_PRIO_MASK))
+ return -EINVAL;
+
+ /* xswap has no backing store, it relies on zswap. */
+ if (!zswap_is_enabled())
+ return -EOPNOTSUPP;
+
+ si = alloc_swap_info();
+ if (IS_ERR(si))
+ return PTR_ERR(si);
+
+ INIT_WORK(&si->discard_work, swap_discard_work);
+ INIT_WORK(&si->reclaim_work, swap_reclaim_work);
+
+ ram = totalram_pages();
+ maxpages = min_t(unsigned long, ram, swapfile_maximum_size);
+ if ((unsigned int)maxpages == 0)
+ maxpages = UINT_MAX;
+ if (maxpages < 2)
+ maxpages = 2;
+
+ si->bdev = NULL;
+ si->flags |= SWP_XSWAP | SWP_SOLIDSTATE;
+ si->max = maxpages;
+ si->pages = maxpages - 1;
+ /* no backing file: mirror the xswap branch of setup_swap_extents() */
+ si->ops = &swap_bdev_ops;
+
+ error = setup_swap_clusters_info(si, NULL, maxpages);
+ if (error)
+ goto bad_swap;
+
+ /* VM_SPARSE covers full RAM; runtime nr_clusters starts at percent. */
+ init_clusters = div_u64((u64)maxpages * percent, 100 * SWAPFILE_CLUSTER);
+ init_clusters = max_t(unsigned long, init_clusters, XSWAP_GROW_CLUSTERS);
+ if (init_clusters > si->nr_clusters_max)
+ init_clusters = si->nr_clusters_max;
+ si->nr_clusters = init_clusters;
+ si->pages = min_t(unsigned long, init_clusters * SWAPFILE_CLUSTER, si->max) - 1;
+
+ error = zswap_swapon(si->type, si->max);
+ if (error)
+ goto bad_swap;
+
+ mutex_lock(&swapon_mutex);
+ si->prio = prio;
+ si->list.prio = -si->prio;
+ si->avail_list.prio = -si->prio;
+ /* si->swap_file stays NULL: this is a file-less device */
+ enable_swap_info(si);
+ mutex_unlock(&swapon_mutex);
+
+ pr_info("xswap: adding extendable swap type %d (prio %d, %d%% of %lu pages = %u pages, max %lu)\n",
+ si->type, prio, percent, ram, si->pages, maxpages);
+ atomic_inc(&proc_poll_event);
+ wake_up_interruptible(&proc_poll_wait);
+
+ return si->type;
+
+bad_swap:
+ kfree(si->global_cluster);
+ si->global_cluster = NULL;
+ destroy_swap_extents(si, NULL); /* safe: xswap never sets SWP_ACTIVATED */
+ free_swap_cluster_info(si);
+ si->cluster_info = NULL;
+ spin_lock(&swap_lock);
+ si->flags = 0;
+ spin_unlock(&swap_lock);
+ return error;
+}
+
+/* Tear down a file-less xswap device by its swap type. */
+static int xswap_destroy(int type)
+{
+ struct swap_info_struct *p;
+
+ p = swap_type_to_info(type);
+ if (!p)
+ return -EINVAL;
+
+ spin_lock(&swap_lock);
+ if (!(p->flags & SWP_WRITEOK) || !(p->flags & SWP_XSWAP)) {
+ spin_unlock(&swap_lock);
+ return -EINVAL;
+ }
+ /* Refuse swapoff while the device is pinned for hibernation */
+ if (p->flags & SWP_HIBERNATION) {
+ spin_unlock(&swap_lock);
+ return -EBUSY;
+ }
+ if (!security_vm_enough_memory_mm(current->mm, p->pages))
+ vm_unacct_memory(p->pages);
+ else {
+ spin_unlock(&swap_lock);
+ return -ENOMEM;
+ }
+ spin_lock(&p->lock);
+ del_from_avail_list(p, true);
+ plist_del(&p->list, &swap_active_head);
+ atomic_long_sub(p->pages, &nr_swap_pages);
+ total_swap_pages -= p->pages;
+ spin_unlock(&p->lock);
+ spin_unlock(&swap_lock);
+
+ return __swapoff(p);
+}
+#endif /* CONFIG_SYSFS */
+#endif /* CONFIG_XSWAP */
+
SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
{
struct swap_info_struct *si;
@@ -3825,7 +4679,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
si->global_cluster = NULL;
inode = NULL;
destroy_swap_extents(si, swap_file);
- free_swap_cluster_info(si->cluster_info, si->max);
+ free_swap_cluster_info(si);
si->cluster_info = NULL;
/*
* Clear the SWP_USED flag after all resources are freed so
@@ -3937,7 +4791,6 @@ void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp)
static int __init swapfile_init(void)
{
swapfile_maximum_size = arch_max_swapfile_size();
-
/*
* Once a cluster is freed, it's swap table content is read
* only, and all swap cache readers (swap_cache_*) verifies
@@ -3947,12 +4800,16 @@ static int __init swapfile_init(void)
swap_table_cachep = kmem_cache_create("swap_table",
sizeof(struct swap_table),
0, SLAB_PANIC | SLAB_TYPESAFE_BY_RCU, NULL);
-
#ifdef CONFIG_MIGRATION
if (swapfile_maximum_size >= (1UL << SWP_MIG_TOTAL_BITS))
swap_migration_ad_supported = true;
#endif /* CONFIG_MIGRATION */
+#ifdef CONFIG_XSWAP
+ xswap_debugfs_root = debugfs_create_dir("xswap", NULL);
+#endif
+ xswap_sysfs_init();
+
return 0;
}
subsys_initcall(swapfile_init);
diff --git a/mm/zswap.c b/mm/zswap.c
index 37f34e406c8e3..064970a4393f5 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -998,7 +998,12 @@ static int zswap_writeback_entry(struct zswap_entry *entry,
/* try to allocate swap cache folio */
si = get_swap_device(swpentry);
if (!si)
- return -EEXIST;
+ return -ENOENT;
+
+ if (si->flags & SWP_XSWAP) {
+ put_swap_device(si);
+ return -EINVAL;
+ }
mpol = get_task_policy(current);
folio = swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
@@ -1545,7 +1550,7 @@ bool zswap_store(struct folio *folio)
zswap_pool_put(pool);
put_objcg:
obj_cgroup_put(objcg);
- if (!ret && zswap_pool_reached_full)
+ if (!ret && zswap_pool_reached_full && atomic_read(&nr_real_swapfiles))
queue_work(shrink_wq, &zswap_shrink_work);
check_old:
/*
|
| 25/3 | 2026/08/27 10:01 | llm | kmsan-evaluator |
0mModel:gemini-3.1-pro-preview Args: null Results: null Tokens: input: 18224 output: 205 thoughts: 2537 Thoughts: **KMSAN Fuzzing Session Assessment for XSWAP Patch Series** Okay, let's break this down. My immediate task is to determine if this "extendable virtual swap device" patch series warrants a dedicated KMSAN fuzzing session. Given the scope, touching `mm/` and core swap infrastructure, my primary concern is uninitialized memory access, particularly those that could lead to information leaks or exploitable conditions. First, I need to methodically check all areas that deal with data transfers to and from user space. I'll scrutinize anything involving `snprintf`, `simple_read_from_buffer`, `sscanf`, `kstrtoul`, and `seq_printf` for potential uninitialized data leaks. The checks for things like `xswap_max_clusters_read`, `xswap_max_clusters_write`, `xswap_create_store`, and `swap_show` are important here. Next, I'll identify allocations, especially complex data structures where uninitialized fields could be read by kernel code. I'm looking at `xswap_map_clusters`, `xswap_unmap_clusters`, and the allocation of `cluster_info` pages. `alloc_page` is a primary target. I'll trace how these are initialized and used. `setup_swap_clusters_info` and `xswap_create`'s allocation of `swap_info_struct` are other important areas. Then, I'll analyze any modifications to control flow and bounds checking that might allow uninitialized memory to be accessed. The addition of the `SWP_XSWAP` flag and the skipping of physical writeout and readahead logic immediately caught my eye. I'll need to confirm that any conditional behavior is safe. Now, let's zoom in on `xswap_map_clusters`. I see that the allocated pages are explicitly zeroed using `__GFP_ZERO`. This is good, ensuring no uninitialized data leaks from these memory regions. Looking closely at `xswap_max_clusters_read`, the buffer `tmp` is fully initialized by `snprintf`. The subsequent use of `simple_read_from_buffer` is safe; the `len` variable limits the number of bytes copied. I've re-examined this a second time. `xswap_create_store` uses `sscanf` to read from the user-provided buffer. This is inherently safe because the kernel only reads data given from userspace, so nothing is leaked. Same with `xswap_destroy_store` and `kstrtoul`. `xswap_unmap_clusters` uses a temporary array, `xpd.pages`, which is allocated but not initialized. However, `xpd.nr` is initialized to 0. The code iterates up to `xpd.nr`, ensuring that only initialized memory is accessed and freed. I've also verified that the memory backing the `cluster_info` array is allocated with `__GFP_ZERO` using a `VM_SPARSE` area. The `swap_info_struct` is allocated with `alloc_swap_info()` which presumably uses `kvzalloc` (I can verify if needed), and the `xswap_page_data` struct is stack-allocated and initialized. All these allocations appear to be safe. I have not identified any new ioctls, netlink attributes, or socket options, which reduces the potential for an obvious info leak surface. The debugfs read function leverages safe mechanisms using `snprintf` and `simple_read_from_buffer`, and there is no sysfs read function (only store). Crucially, I've checked for any conditional logic depending on uninitialized variables. I see no such dependencies. Therefore, after this exhaustive review, I'm confident in concluding that this patch series does not introduce any *new* uninitialized memory risks. Standard tools like KASAN are entirely sufficient to catch out-of-bounds accesses, use-after-free conditions, etc. KMSAN is not necessary here. I re-examined the key areas, such as `xswap_max_clusters_read`, `xswap_unmap_clusters`, and `xswap_map_clusters`, to reinforce the safety of these operations and prevent potential oversight, and remain confident. I'm marking this for closure with this assessment. |
| 26/3 | 2026/08/27 10:02 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch introduces an extendable virtual swap device (xswap) and modifies swap management logic. It adds sysfs and debugfs interfaces, but all data copied to userspace is properly initialized (e.g., via `snprintf` in `xswap_max_clusters_read`). The new data structures, such as the dynamically mapped `cluster_info` pages, are explicitly allocated with `__GFP_ZERO`. The `xswap_page_data` struct used during unmapping correctly tracks the number of initialized array elements to avoid freeing uninitialized pointers. There are no info-leaks or uses of uninitialized memory introduced by this patch. Any potential memory errors like out-of-bounds accesses or use-after-free bugs in the new swap logic will be effectively caught by standard KASAN. Therefore, a dedicated KMSAN fuzzing session is not required.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch introduces an extendable virtual swap device (xswap) and modifies swap management logic. It adds sysfs and debugfs interfaces, but all data copied to userspace is properly initialized (e.g., via `snprintf` in `xswap_max_clusters_read`). The new data structures, such as the dynamically mapped `cluster_info` pages, are explicitly allocated with `__GFP_ZERO`. The `xswap_page_data` struct used during unmapping correctly tracks the number of initialized array elements to avoid freeing uninitialized pointers. There are no info-leaks or uses of uninitialized memory introduced by this patch. Any potential memory errors like out-of-bounds accesses or use-after-free bugs in the new swap logic will be effectively caught by standard KASAN. Therefore, a dedicated KMSAN fuzzing session is not required.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|