[RFC v3 12/15] mm, swap: add debugfs knob for xswap per-device cluster limit
Baoquan He <[email protected]>
| Newsgroups | org.kvack.linux-mm,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <[email protected]> |
Add a per-device debugfs file for runtime adjustment of xswap cluster limit: /sys/kernel/debug/xswap/type<N>_cluster_limit Reading shows the current ceiling (in clusters); writing sets it (clamped to [0, nr_clusters_max]). Setting below nr_clusters_mapped triggers an immediate shrink check via xswap_try_shrink(). The debugfs entry is created on device creation and removed on destruction. Signed-off-by: Baoquan He <[email protected]> --- include/linux/swap.h | 1 + mm/swapfile.c | 90 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 93d7771f2427..4d8ca920d2f9 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -254,6 +254,7 @@ struct swap_info_struct { 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 mutex xswap_lock; /* serialize map/unmap operations */ #endif struct list_head free_clusters; /* free clusters list */ diff --git a/mm/swapfile.c b/mm/swapfile.c index be56114a6691..2755a549582c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -50,6 +50,9 @@ #include "swap_table.h" #include "internal.h" #include "swap.h" +#include <linux/debugfs.h> + +static DEFINE_SPINLOCK(swap_lock); #ifdef CONFIG_XSWAP /* @@ -66,6 +69,8 @@ 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, @@ -76,6 +81,83 @@ 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); /* /sys/kernel/mm/xswap/: create. @@ -156,7 +238,6 @@ 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; @@ -3223,6 +3304,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si) #ifdef CONFIG_XSWAP if (si->flags & SWP_XSWAP) { + xswap_debugfs_del(si); /* 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); @@ -4109,6 +4191,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, /* All mapped clusters except cluster 0 are free at the tail */ si->nr_free_tail = si->nr_clusters_mapped - 1; mutex_init(&si->xswap_lock); + xswap_debugfs_add(si); return 0; err_unmap: @@ -4236,8 +4319,8 @@ static int xswap_create(int percent) /* 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) - init_clusters = si->nr_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; @@ -4615,6 +4698,7 @@ static int __init swapfile_init(void) swap_migration_ad_supported = true; #endif /* CONFIG_MIGRATION */ + xswap_debugfs_root = debugfs_create_dir("xswap", NULL); xswap_sysfs_init(); return 0; -- 2.54.0