* [PATCH v8 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 02/14] mm: xswap support for zswap Baoquan He
` (12 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
VM_SPARSE areas are created with get_vm_area() and filled in by the
caller via vm_area_map_pages(), so the caller need to allocate the pages,
keep the struct page array and free everything by itself.
Add vm_area_grow_pages() and vm_area_shrink_pages() for VM_SPARSE |
VM_ALLOC areas. Pages come from the page allocator and are tracked in
area->pages, so the area is released with a single vfree(). The backed
pages always start at the beginning of the area: growing maps
[nr_pages, end) and shrinking unmaps and frees [end, nr_pages). Callers
serialize the two helpers.
nr_pages keeps its existing meaning of "number of backed pages", as
with vrealloc() and vread_iter(), and is updated under the vmap node's
busy.lock so /proc/vmallocinfo never sees a page that is already gone.
Only areas populated through vm_area_map_pages() stay a memory hole.
vm_area_map_pages() now rejects VM_ALLOC areas, because vfree() owns
their pages, to avoid double frees; free_vm_area() warns for the same
combination but not for plain VM_ALLOC areas.
Suggested-by: Chris Li <chrisl@kernel.org>
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/vmalloc.h | 2 +
mm/vmalloc.c | 183 +++++++++++++++++++++++++++++++++++++---
2 files changed, 172 insertions(+), 13 deletions(-)
diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 034a693777ca..9f40edcddfa8 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -296,6 +296,8 @@ int vm_area_map_pages(struct vm_struct *area, unsigned long start,
unsigned long end, struct page **pages);
void vm_area_unmap_pages(struct vm_struct *area, unsigned long start,
unsigned long end);
+int vm_area_grow_pages(struct vm_struct *area, unsigned long end, gfp_t gfp);
+void vm_area_shrink_pages(struct vm_struct *area, unsigned long end);
void vunmap_range(unsigned long addr, unsigned long end);
static inline void set_vm_flush_reset_perms(void *addr)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index aeb1cd4a5385..dd8d0c30e22f 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -788,6 +788,8 @@ static int check_sparse_vm_area(struct vm_struct *area, unsigned long start,
return -EINVAL;
if (WARN_ON_ONCE(!(area->flags & VM_SPARSE)))
return -EINVAL;
+ if (WARN_ON_ONCE(area->flags & VM_ALLOC))
+ return -EINVAL;
if ((end - start) >> PAGE_SHIFT > totalram_pages())
return -E2BIG;
if (start < (unsigned long)area->addr ||
@@ -4173,6 +4175,148 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
return NULL;
}
+/**
+ * vm_area_grow_pages - back more of a sparse area with pages
+ * @area: area created with %VM_SPARSE | %VM_ALLOC
+ * @end: end of the range to back, %PAGE_SIZE aligned
+ * @gfp: gfp flags for the page allocation
+ *
+ * Allocate and map pages for [@area->nr_pages << PAGE_SHIFT, @end). The
+ * pages are zeroed when %__GFP_ZERO is set. Pages are always backed from
+ * the start of the area, so a grow only extends that range. An @end that
+ * is already backed is a no-op. On failure nothing is mapped and
+ * @area->nr_pages is unchanged.
+ *
+ * The area must be released with vfree(@area->addr). Callers must
+ * serialize calls to this function.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+int vm_area_grow_pages(struct vm_struct *area, unsigned long end, gfp_t gfp)
+{
+ unsigned long start = (unsigned long)area->nr_pages << PAGE_SHIFT;
+ unsigned long addr = (unsigned long)area->addr;
+ unsigned long nr_pages, nr_allocated, nr_end, cap;
+ gfp_t nested_gfp = (gfp & GFP_RECLAIM_MASK) | __GFP_ZERO;
+ struct vmap_node *vn;
+ struct page **pages;
+ unsigned int flags;
+ int ret;
+
+ might_sleep();
+
+ if (WARN_ON_ONCE(!(area->flags & VM_ALLOC) ||
+ !(area->flags & VM_SPARSE)))
+ return -EINVAL;
+ if (WARN_ON_ONCE(!IS_ALIGNED(end, PAGE_SIZE)))
+ return -EINVAL;
+ if (WARN_ON_ONCE(end > get_vm_area_size(area)))
+ return -EINVAL;
+ if (end <= start)
+ return 0;
+ if ((end - start) >> PAGE_SHIFT > totalram_pages())
+ return -E2BIG;
+
+ nr_end = end >> PAGE_SHIFT;
+ cap = roundup_pow_of_two(nr_end);
+ vn = addr_to_node(addr);
+ if (!area->pages ||
+ cap > (area->nr_pages ? roundup_pow_of_two(area->nr_pages) : 0)) {
+ struct page **old = area->pages;
+
+ pages = kvmalloc_array(cap, sizeof(*pages), nested_gfp);
+ if (!pages)
+ return -ENOMEM;
+ if (!old) {
+ area->pages = pages;
+ } else {
+ memcpy(pages, old, area->nr_pages * sizeof(*pages));
+ spin_lock(&vn->busy.lock);
+ area->pages = pages;
+ spin_unlock(&vn->busy.lock);
+ kvfree(old);
+ }
+ }
+
+ if (!(gfp & (GFP_DMA | GFP_DMA32)))
+ gfp |= __GFP_HIGHMEM;
+
+ nr_pages = (end - start) >> PAGE_SHIFT;
+ nr_allocated = vm_area_alloc_pages(vmalloc_gfp_adjust(gfp, 0),
+ NUMA_NO_NODE, 0, nr_pages,
+ area->pages + (start >> PAGE_SHIFT));
+ if (nr_allocated != nr_pages) {
+ vm_area_free_pages(area, start >> PAGE_SHIFT,
+ (start >> PAGE_SHIFT) + nr_allocated);
+ return -ENOMEM;
+ }
+
+ flags = memalloc_apply_gfp_scope(gfp);
+ ret = __vmap_pages_range(addr + start, addr + end, PAGE_KERNEL,
+ area->pages + (start >> PAGE_SHIFT),
+ PAGE_SHIFT, nested_gfp);
+ memalloc_restore_scope(flags);
+ if (ret)
+ goto free_pages;
+
+ kasan_unpoison_vmalloc((void *)(addr + start), end - start,
+ KASAN_VMALLOC_PROT_NORMAL |
+ KASAN_VMALLOC_KEEP_TAG);
+
+ /*
+ * Publish under busy.lock, like clear_vm_uninitialized_flag():
+ * show_busy_info() reads nr_pages and pages[] under that lock.
+ */
+ spin_lock(&vn->busy.lock);
+ WRITE_ONCE(area->nr_pages, end >> PAGE_SHIFT);
+ spin_unlock(&vn->busy.lock);
+ return 0;
+
+free_pages:
+ vunmap_range(addr + start, addr + end);
+ vm_area_free_pages(area, start >> PAGE_SHIFT, end >> PAGE_SHIFT);
+ return -ENOMEM;
+}
+
+/**
+ * vm_area_shrink_pages - release the pages backing the end of a sparse area
+ * @area: area created with %VM_SPARSE | %VM_ALLOC
+ * @end: new end of the backed range, %PAGE_SIZE aligned
+ *
+ * Unmap and free pages for [@end, @area->nr_pages << PAGE_SHIFT). The
+ * released range can be backed again with vm_area_grow_pages(). Callers
+ * must serialize calls to this function.
+ */
+void vm_area_shrink_pages(struct vm_struct *area, unsigned long end)
+{
+ unsigned long start = (unsigned long)area->nr_pages << PAGE_SHIFT;
+ unsigned long addr = (unsigned long)area->addr;
+ struct vmap_node *vn;
+
+ might_sleep();
+
+ if (WARN_ON_ONCE(!(area->flags & VM_ALLOC) ||
+ !(area->flags & VM_SPARSE)))
+ return;
+ if (WARN_ON_ONCE(!IS_ALIGNED(end, PAGE_SIZE)))
+ return;
+ if (end >= start)
+ return;
+
+ /*
+ * Lower nr_pages under busy.lock before the pages go away: readers
+ * walk pages[] up to nr_pages under that lock.
+ */
+ vn = addr_to_node(addr);
+ spin_lock(&vn->busy.lock);
+ WRITE_ONCE(area->nr_pages, end >> PAGE_SHIFT);
+ spin_unlock(&vn->busy.lock);
+
+ kasan_poison_vmalloc((void *)(addr + end), start - end);
+ vunmap_range(addr + end, addr + start);
+ vm_area_free_pages(area, end >> PAGE_SHIFT, start >> PAGE_SHIFT);
+}
+
/*
* See __vmalloc_node_range() for a clear list of supported vmalloc flags.
* This gfp lists all flags currently passed through vmalloc. Currently,
@@ -4951,18 +5095,21 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count)
smp_rmb();
vaddr = (char *) va->va_start;
- if (vm)
+ if (vm) {
/*
- * For VM_ALLOC areas, use nr_pages rather than
- * get_vm_area_size() because vrealloc() may shrink
- * the mapping without updating area->size. Other
- * mapping types (vmap, ioremap) don't set nr_pages.
+ * Use nr_pages for VM_ALLOC areas: vrealloc() may
+ * shrink them without updating area->size, and a
+ * VM_SPARSE area is backed lazily, so its zero
+ * prefix means nothing is mapped.
*/
- size = (vm->flags & VM_ALLOC && vm->nr_pages) ?
- (vm->nr_pages << PAGE_SHIFT) :
- get_vm_area_size(vm);
- else
+ if ((vm->flags & VM_ALLOC) &&
+ (vm->nr_pages || (vm->flags & VM_SPARSE)))
+ size = vm->nr_pages << PAGE_SHIFT;
+ else
+ size = get_vm_area_size(vm);
+ } else {
size = va_size(va);
+ }
if (addr >= vaddr + size)
goto next_va;
@@ -4982,12 +5129,18 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count)
if (n > remains)
n = remains;
- if (flags & VMAP_RAM)
+ if (flags & VMAP_RAM) {
copied = vmap_ram_vread_iter(iter, addr, n, flags);
- else if (!(vm && (vm->flags & (VM_IOREMAP | VM_SPARSE))))
- copied = aligned_vread_iter(iter, addr, n);
- else /* IOREMAP | SPARSE area is treated as memory hole */
+ } else if (vm && (vm->flags & VM_IOREMAP)) {
+ /* Not backed by struct pages. */
+ copied = zero_iter(iter, n);
+ } else if (vm && (vm->flags & VM_SPARSE) &&
+ !(vm->flags & VM_ALLOC)) {
+ /* Populated by the caller; treat as a memory hole. */
copied = zero_iter(iter, n);
+ } else {
+ copied = aligned_vread_iter(iter, addr, n);
+ }
addr += copied;
remains -= copied;
@@ -5106,6 +5259,10 @@ EXPORT_SYMBOL(remap_vmalloc_range);
void free_vm_area(struct vm_struct *area)
{
struct vm_struct *ret;
+
+ /* VM_SPARSE | VM_ALLOC areas own their pages and need vfree(). */
+ WARN_ON_ONCE((area->flags & (VM_ALLOC | VM_SPARSE)) ==
+ (VM_ALLOC | VM_SPARSE));
ret = remove_vm_area(area->addr);
BUG_ON(ret != area);
kfree(area);
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas
2026-12-03 16:43 ` [PATCH v8 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] mm/vmalloc: missing alloc_hooks() instrumentation in
vm_area_grow_pages()
- [Medium] mm: vm_area_grow_pages() infers pages[] capacity from
area->nr_pages
- [Low] mm/vmalloc: unlocked area->pages write in vm_area_grow_pages()
- [Low] mm: vm_area_grow_pages() end parameter unit conflicts with
vmalloc.h APIs
- [Low] mm/vmalloc: vm_area_grow_pages() omits sleepable vfree() context
requirement
- [Low] mm/vmalloc: VM_SPARSE | VM_ALLOC areas keep KASAN_TAG_KERNEL
under SW_TAGS
--
commit 41b3c2f65a0817d28ac0086fc5899b3fa86f007c
Author: Baoquan He <hebaoquan@kylinos.cn>
mm/vmalloc: support vmalloc-backed VM_SPARSE areas
This commit adds vm_area_grow_pages() and vm_area_shrink_pages() to lazily
back and unmap pages from the start of VM_SPARSE | VM_ALLOC areas, and
updates vread_iter(), check_sparse_vm_area(), and free_vm_area() to handle
them.
> diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
> index 034a693777ca0..9f40edcddfa8a 100644
> --- a/include/linux/vmalloc.h
> +++ b/include/linux/vmalloc.h
> @@ -296,6 +296,8 @@ int vm_area_map_pages(struct vm_struct *area, unsigned long start,
> unsigned long end, struct page **pages);
> void vm_area_unmap_pages(struct vm_struct *area, unsigned long start,
> unsigned long end);
> +int vm_area_grow_pages(struct vm_struct *area, unsigned long end, gfp_t gfp);
> +void vm_area_shrink_pages(struct vm_struct *area, unsigned long end);
> void vunmap_range(unsigned long addr, unsigned long end);
[Severity: Low]
Should the end parameter use the same unit as the adjacent vmalloc.h APIs,
or be renamed?
vm_area_map_pages(), vm_area_unmap_pages(), and vunmap_range() all treat
end as a kernel virtual address, whereas vm_area_grow_pages() and
vm_area_shrink_pages() take a byte offset from area->addr. The kernel-doc
@end descriptions do not state that it is an offset, and the commit message
mixes page counts and byte offsets ([nr_pages, end)).
In addition, vm_area_shrink_pages() omits the end > get_vm_area_size(area)
check that vm_area_grow_pages() performs. Renaming end to size (or
new_size), or taking a virtual address end to match vm_area_map_pages(),
would avoid the unit mismatch.
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index aeb1cd4a53850..dd8d0c30e22f0 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
[ ... ]
> @@ -4173,6 +4175,148 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
[ ... ]
> + * The area must be released with vfree(@area->addr). Callers must
> + * serialize calls to this function.
> + *
> + * Return: 0 on success, -errno on failure.
> + */
> +int vm_area_grow_pages(struct vm_struct *area, unsigned long end, gfp_t gfp)
[Severity: Low]
Should this comment note that vfree() must be called from sleepable
context?
Unlike normal vmalloc() areas, a VM_SPARSE | VM_ALLOC area can have
nr_pages == 0 with offset 0 unmapped (immediately after get_vm_area(),
after a failed grow, or after vm_area_shrink_pages(area, 0)). vfree() is
documented as callable from interrupt context, where it delegates to
vfree_atomic() and writes a struct llist_node directly into *addr, which
would page-fault if offset 0 is unmapped.
[ ... ]
> + nr_end = end >> PAGE_SHIFT;
> + cap = roundup_pow_of_two(nr_end);
> + vn = addr_to_node(addr);
> + if (!area->pages ||
> + cap > (area->nr_pages ? roundup_pow_of_two(area->nr_pages) : 0)) {
> + struct page **old = area->pages;
> +
> + pages = kvmalloc_array(cap, sizeof(*pages), nested_gfp);
> + if (!pages)
> + return -ENOMEM;
[Severity: Medium]
Can inferring area->pages capacity from area->nr_pages cause repeated
reallocations?
If kvmalloc_array() expands area->pages across a power-of-two boundary and
the subsequent vm_area_alloc_pages() fails, area->nr_pages is left
unchanged while area->pages already has cap slots. Every retry under
memory pressure then allocates another cap-sized array, copies the
pointers, and frees the old one; once cap * sizeof(*pages) exceeds
PAGE_SIZE, each retry becomes a nested vmalloc()/vfree() cycle. Shrinking
with vm_area_shrink_pages() and growing back across a power-of-two boundary
hits the same churn.
cap is also not clamped to get_vm_area_size(area) >> PAGE_SHIFT, which can
over-allocate area->pages by up to nearly 2x for non-power-of-two areas.
Tracking the allocated capacity in struct vm_struct and clamping cap would
avoid both.
> + if (!old) {
> + area->pages = pages;
> + } else {
> + memcpy(pages, old, area->nr_pages * sizeof(*pages));
> + spin_lock(&vn->busy.lock);
> + area->pages = pages;
> + spin_unlock(&vn->busy.lock);
> + kvfree(old);
> + }
> + }
[Severity: Low]
Does the unlocked write to area->pages race with show_busy_info()?
CPU0 CPU1
vm_area_grow_pages() show_busy_info()
spin_lock(&vn->busy.lock)
area->pages = pages; <-- data race
if (is_vmalloc_addr(v->pages))
spin_unlock(&vn->busy.lock)
A VM_SPARSE | VM_ALLOC area from get_vm_area() is linked into vn->busy.head
without VM_UNINITIALIZED, so /proc/vmallocinfo readers in show_busy_info()
read v->pages under vn->busy.lock even when v->nr_pages is 0. This causes
a KCSAN data race and can transiently misreport the vpages flag. Taking
vn->busy.lock unconditionally around area->pages = pages avoids the race.
[ ... ]
> + nr_pages = (end - start) >> PAGE_SHIFT;
> + nr_allocated = vm_area_alloc_pages(vmalloc_gfp_adjust(gfp, 0),
> + NUMA_NO_NODE, 0, nr_pages,
> + area->pages + (start >> PAGE_SHIFT));
[Severity: Medium]
Is vm_area_grow_pages() missing alloc_hooks() instrumentation?
vm_area_alloc_pages() calls *_noprof page allocator helpers directly, so
without an alloc_hooks() wrapper on vm_area_grow_pages(), current->alloc_tag
is unset. Under CONFIG_MEM_ALLOC_PROFILING, the backing pages are omitted
from /proc/allocinfo and area->pages is billed to vm_area_grow_pages()
instead of its caller; under CONFIG_MEM_ALLOC_PROFILING_DEBUG, the first
grow triggers WARN_ONCE(!tag, "current->alloc_tag not set\n") in
alloc_tag_add_check() and freeing those pages warns in
alloc_tag_sub_check().
Exposing vm_area_grow_pages_noprof() wrapped with alloc_hooks() in
include/linux/vmalloc.h and calling kvmalloc_array_noprof() inside it would
match the other page-allocating vmalloc APIs.
[ ... ]
> + kasan_unpoison_vmalloc((void *)(addr + start), end - start,
> + KASAN_VMALLOC_PROT_NORMAL |
> + KASAN_VMALLOC_KEEP_TAG);
[Severity: Low]
Does passing KASAN_VMALLOC_KEEP_TAG here disable CONFIG_KASAN_SW_TAGS
checks?
__get_vm_area_node() skips kasan_unpoison_vmalloc() when VM_ALLOC is set,
so area->addr still carries the match-all KASAN_TAG_KERNEL tag (0xFF).
Passing KASAN_VMALLOC_KEEP_TAG writes that tag into shadow memory instead
of assigning a random tag, and kasan_check_range() short-circuits all
checks for KASAN_TAG_KERNEL pointers, silently disabling out-of-bounds and
use-after-free detection on these areas.
Assigning a random tag to area->addr at reservation time (without
unpoisoning unbacked pages) and using kasan_reset_tag(area->addr) before
addr_to_node(), __vmap_pages_range(), and vunmap_range() would restore
tag-based checking.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=1
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 02/14] mm: xswap support for zswap
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
2026-12-03 16:43 ` [PATCH v8 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Baoquan He
` (11 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Kunwu Chan, Baoquan He
From: Chris Li <chrisl@kernel.org>
Introduce extendable swap device support - xswap.
An xswap device has no backing storage and no swap data section, so
it wastes no disk space. Creation is via swapon(2) with the name
"xswap", added in a later patch.
Zswap writeback is gated on whether a real (non-xswap) swap device is
active. nr_real_swapfiles counts such devices and is maintained at
swapon/swapoff only, so the gate reflects "a device exists to write
back to" rather than "a device currently has free slots". This keeps
writeback working even when the real swap device is full, and avoids
a double decrement when a full device is swapped off.
An xswap entry is not added to the zswap writeback LRU: with no backing
store there is nothing to write it back to. zswap_lru_del() tolerates
that, since an entry that was never added is on no list.
An incompressible page is stored uncompressed only because it can be
written back later, in LRU order. In this phase an xswap entry has no
writeback target and never reaches the LRU, so such a page would be
pinned in the pool for the lifetime of the entry. Reject it instead,
the same way a writeback-disabled memcg is handled. A later phase gives
xswap a physical backend, and the test becomes "this device has a
writeback target".
A VMA readahead walks the page table and can reach an entry on another
device. Skip an xswap device there: its entries live only in zswap, so
there is no IO to read ahead and a speculative read would take the entry
out of zswap.
An xswap entry is kept out of the zswap writeback LRU but still counts
towards the pool size. With memcg disabled the shrinker walks the root
LRU, finds no entry it can write back and is told -ENOENT without
counting a failure, so the accept-threshold loop would spin. Count that
as a failure; the worker then stops after its retry budget.
Reviewed-by: Kunwu Chan <chentao@kylinos.cn>
Co-developed-by: Baoquan He <hebaoquan@kylinos.cn>
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
Signed-off-by: Chris Li <chrisl@kernel.org>
---
include/linux/swap.h | 2 ++
mm/page_io.c | 13 +++++++++++++
mm/swap_state.c | 24 +++++++++++++++++++-----
mm/swapfile.c | 34 ++++++++++++++++++++++++++++------
mm/zswap.c | 34 +++++++++++++++++++++++++++-------
5 files changed, 89 insertions(+), 18 deletions(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index a3a98dc474c3..9b73a038e542 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -201,6 +201,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... */
};
@@ -378,6 +379,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/page_io.c b/mm/page_io.c
index 12deab296718..54bdeab13641 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -259,6 +259,12 @@ int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
}
rcu_read_unlock();
+ /* No backing store: keep the folio; ctx->sis is not set yet. */
+ if (unlikely(__swap_entry_to_info(folio->swap)->flags & SWP_XSWAP)) {
+ folio_mark_dirty(folio);
+ return AOP_WRITEPAGE_ACTIVATE;
+ }
+
__swap_writeout(ctx, folio);
return 0;
out_unlock:
@@ -516,6 +522,13 @@ void swap_read_folio(struct swap_io_ctx *ctx, struct folio *folio)
if (zswap_load(folio) != -ENOENT)
return;
+ if (unlikely(sis->flags & SWP_XSWAP)) {
+ /* An xswap entry only lives in zswap: zswap_load() must hit. */
+ WARN_ON_ONCE(1);
+ folio_unlock(folio);
+ return;
+ }
+
/* 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 da65d75ceead..90cd99c25dfe 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -899,6 +899,10 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,
struct blk_plug plug;
swp_entry_t ra_entry;
+ /* xswap entries live only in zswap; readahead does not help. */
+ if (si->flags & SWP_XSWAP)
+ goto skip;
+
mask = swapin_nr_pages(offset) - 1;
if (!mask)
goto skip;
@@ -984,6 +988,7 @@ static int swap_vma_ra_win(struct vm_fault *vmf, unsigned long *start,
static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask,
struct mempolicy *mpol, pgoff_t targ_ilx, struct vm_fault *vmf)
{
+ struct swap_info_struct *si = __swap_entry_to_info(targ_entry);
struct swap_io_ctx ctx = {};
struct blk_plug plug;
struct folio *folio;
@@ -992,6 +997,10 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask,
unsigned long start, end, addr;
pgoff_t ilx = targ_ilx;
+ /* xswap entries live only in zswap; readahead does not help. */
+ if (si->flags & SWP_XSWAP)
+ goto skip;
+
win = swap_vma_ra_win(vmf, &start, &end);
if (win == 1)
goto skip;
@@ -1000,7 +1009,7 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask,
blk_start_plug(&plug);
for (addr = start; addr < end; ilx++, addr += PAGE_SIZE) {
- struct swap_info_struct *si = NULL;
+ struct swap_info_struct *entry_si = NULL;
softleaf_t entry;
if (!pte++) {
@@ -1020,14 +1029,19 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask,
* holding a reference to, try to grab a reference, or skip.
*/
if (swp_type(entry) != swp_type(targ_entry)) {
- si = get_swap_device(entry);
- if (IS_ERR_OR_NULL(si))
+ entry_si = get_swap_device(entry);
+ if (IS_ERR_OR_NULL(entry_si))
+ continue;
+ /* An xswap entry lives only in zswap; there is no IO. */
+ if (entry_si->flags & SWP_XSWAP) {
+ put_swap_device(entry_si);
continue;
+ }
}
folio = swap_cache_read_folio(&ctx, entry, gfp_mask, mpol, ilx,
addr != vmf->address);
- if (si)
- put_swap_device(si);
+ if (entry_si)
+ put_swap_device(entry_si);
if (!folio)
continue;
folio_put(folio);
diff --git a/mm/swapfile.c b/mm/swapfile.c
index d59fa8220862..558c4b75bf92 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -66,6 +66,7 @@ static void move_cluster(struct swap_info_struct *si,
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
@@ -775,7 +776,8 @@ static void free_cluster(struct swap_info_struct *si, struct swap_cluster_info *
/*
* If the swap is discardable, prepare discard the cluster
* instead of free it immediately. The cluster will be freed
- * after discard.
+ * after discard. xswap has no bdev and never sets
+ * SWP_PAGE_DISCARD, so it always takes the free path below.
*/
if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
(SWP_WRITEOK | SWP_PAGE_DISCARD)) {
@@ -1277,6 +1279,9 @@ static void del_from_avail_list(struct swap_info_struct *si, bool swapoff)
*/
lockdep_assert_held(&si->lock);
si->flags &= ~SWP_WRITEOK;
+ /* Count active devices, not merely those on the avail list. */
+ if (!(si->flags & SWP_XSWAP))
+ atomic_sub(1, &nr_real_swapfiles);
atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages);
} else {
/*
@@ -1310,6 +1315,9 @@ static void add_to_avail_list(struct swap_info_struct *si, bool swapon)
if (swapon) {
lockdep_assert_held(&si->lock);
si->flags |= SWP_WRITEOK;
+ /* A full device is still active and skips the add below. */
+ if (!(si->flags & SWP_XSWAP))
+ atomic_add(1, &nr_real_swapfiles);
} else {
if (!(READ_ONCE(si->flags) & SWP_WRITEOK))
goto skip;
@@ -1323,10 +1331,11 @@ static void add_to_avail_list(struct swap_info_struct *si, bool swapon)
/*
* When device is full and device is on the plist, only one updater will
* see (inuse_pages == si->pages) and will call del_from_avail_list. If
- * that updater happen to be here, just skip adding.
+ * that updater happen to be here, just skip adding. @val still has
+ * the off-list bit, so compare its counter part.
*/
pages = si->pages;
- if (val == pages) {
+ if ((val & SWAP_USAGE_COUNTER_MASK) == pages) {
/* Just like the cmpxchg in del_from_avail_list */
if (atomic_long_try_cmpxchg(&si->inuse_pages, &pages,
pages | SWAP_USAGE_OFFLIST_BIT))
@@ -3330,7 +3339,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);
@@ -3440,6 +3450,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;
@@ -3459,8 +3482,7 @@ static int swap_show(struct seq_file *swap, void *v)
len = seq_file_path(swap, file, " \t\n\\");
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);
diff --git a/mm/zswap.c b/mm/zswap.c
index 6a66b898ab47..52f350b2c452 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -885,7 +885,8 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
}
static bool zswap_compress(struct folio *folio, long index,
- struct zswap_entry *entry, struct zswap_pool *pool)
+ struct zswap_entry *entry, struct zswap_pool *pool,
+ bool can_writeback)
{
struct zswap_comp_ctx *comp_ctx;
struct scatterlist input, output;
@@ -934,9 +935,13 @@ static bool zswap_compress(struct folio *folio, long index,
* save the content as is without a compression, to keep the LRU order
* of writebacks. If writeback is disabled, reject the page since it
* only adds metadata overhead. swap_writeout() will put the page back
- * to the active LRU list in the case.
+ * to the active LRU list in the case. An xswap entry in this phase
+ * is never put on the LRU, so an incompressible page stored for it
+ * could never be reclaimed: reject it the same way.
*/
if (comp_ret) {
+ if (!can_writeback)
+ goto unlock;
rcu_read_lock();
if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) {
rcu_read_unlock();
@@ -1093,6 +1098,11 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
if (IS_ERR_OR_NULL(si))
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,
NO_INTERLEAVE_INDEX);
@@ -1508,10 +1518,15 @@ static void shrink_worker(struct work_struct *w)
* There are no writeback-candidate pages in the memcg.
* This is not an issue as long as we can find another memcg
* with pages in zswap. Skip this without incrementing attempts
- * and failures.
+ * and failures. With memcg disabled there is no other memcg,
+ * so an empty walk is the last one and has to count.
*/
- if (ret == -ENOENT)
+ if (ret == -ENOENT) {
+ if (mem_cgroup_disabled() &&
+ ++failures == MAX_RECLAIM_RETRIES)
+ break;
goto resched;
+ }
++attempts;
if (ret && ++failures == MAX_RECLAIM_RETRIES)
@@ -1531,6 +1546,7 @@ static bool zswap_store_page(struct folio *folio, long index,
{
swp_entry_t page_swpentry = folio_swap_entry(folio, index);
struct zswap_entry *entry, *old;
+ bool can_writeback;
/* allocate entry */
entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio));
@@ -1539,7 +1555,10 @@ static bool zswap_store_page(struct folio *folio, long index,
return false;
}
- if (!zswap_compress(folio, index, entry, pool))
+ /* In this phase xswap has no device to write the page back to. */
+ can_writeback = !(__swap_entry_to_info(page_swpentry)->flags & SWP_XSWAP);
+
+ if (!zswap_compress(folio, index, entry, pool, can_writeback))
goto compress_failed;
/*
@@ -1598,7 +1617,8 @@ static bool zswap_store_page(struct folio *folio, long index,
entry->objcg = objcg;
entry->referenced = true;
INIT_LIST_HEAD(&entry->lru);
- zswap_lru_add(entry);
+ if (can_writeback)
+ zswap_lru_add(entry);
return true;
@@ -1667,7 +1687,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:
/*
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 02/14] mm: xswap support for zswap
2026-12-03 16:43 ` [PATCH v8 02/14] mm: xswap support for zswap Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Kunwu Chan, Baoquan He
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] mm/zswap: unnecessary zswap_lru_del() locking and LRU alloc
for xswap entries
- [Medium] mm: zswap_store() queues shrink_worker() on empty LRU when
xswap fills pool
- [Low] mm: unmentioned add_to_avail_list() bug fix and /proc/swaps
change
- [Low] mm: zswap_store() calls shrink_memcg() without checking
nr_real_swapfiles
--
commit 9c57f7ef12d862357f940d3c5e67abd20d6102eb
Author: Chris Li <chrisl@kernel.org>
mm: xswap support for zswap
This commit adds core swap and zswap support for extendable swap devices
(SWP_XSWAP) that have no backing storage, skipping writeback, readahead,
and zswap LRU insertion for xswap entries while tracking active non-xswap
devices via nr_real_swapfiles.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index d59fa82208620..558c4b75bf928 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -1323,10 +1331,11 @@ static void add_to_avail_list(struct swap_info_struct *si, bool swapon)
> /*
> * When device is full and device is on the plist, only one updater will
> * see (inuse_pages == si->pages) and will call del_from_avail_list. If
> - * that updater happen to be here, just skip adding.
> + * that updater happen to be here, just skip adding. @val still has
> + * the off-list bit, so compare its counter part.
> */
> pages = si->pages;
> - if (val == pages) {
> + if ((val & SWAP_USAGE_COUNTER_MASK) == pages) {
> /* Just like the cmpxchg in del_from_avail_list */
> if (atomic_long_try_cmpxchg(&si->inuse_pages, &pages,
> pages | SWAP_USAGE_OFFLIST_BIT))
[Severity: Low]
Should this fix in add_to_avail_list() be split into its own commit?
Because atomic_long_fetch_and_relaxed() returns the value before clearing
SWAP_USAGE_OFFLIST_BIT, the old (val == pages) check was always false and
let a full swap device be added back to swap_avail_head. Splitting it out
lets it carry Fixes: b228386cf237 ("mm, swap: clean up plist removal and
adding") for stable backports. Also, the new "xswap" Type column value
added to /proc/swaps by swap_type_str() is not mentioned in the commit
message.
[ ... ]
> diff --git a/mm/zswap.c b/mm/zswap.c
> index 6a66b898ab470..52f350b2c452f 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
[ ... ]
> @@ -1598,7 +1617,8 @@ static bool zswap_store_page(struct folio *folio, long index,
> entry->objcg = objcg;
> entry->referenced = true;
> INIT_LIST_HEAD(&entry->lru);
> - zswap_lru_add(entry);
> + if (can_writeback)
> + zswap_lru_add(entry);
>
> return true;
[Severity: Medium]
Can zswap_entry_free() skip zswap_lru_del() for xswap entries?
zswap_store_page() never adds SWP_XSWAP entries to zswap_list_lru, but
zswap_entry_free() still calls zswap_lru_del() unconditionally on every
xswap swap-in and slot invalidation. list_lru_del() then does the memcg
XArray lookup and takes the shared per-(memcg, node) l->lock spinlock
only to see list_empty(&entry->lru) is true; during slot freeing via
swap_range_free(), that lock is acquired per page while holding ci->lock.
Similarly, zswap_store() unconditionally calls memcg_list_lru_alloc() for
xswap folios that will never be placed on zswap_list_lru. Gating both on
!(__swap_entry_to_info(...)->flags & SWP_XSWAP) avoids the lock contention
and unused per-memcg list_lru allocations.
[ ... ]
> @@ -1667,7 +1687,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:
> /*
[Severity: Medium]
Does gating zswap_shrink_work on nr_real_swapfiles cause shrink_worker() to
spin when xswap fills the zswap pool?
When both xswap and a real swap device are active, xswap entries can fill
the zswap pool to zswap_max_pages() without putting any entries on
zswap_list_lru. Every subsequent rejected zswap_store() sees
zswap_pool_reached_full and nr_real_swapfiles > 0 and re-queues
zswap_shrink_work back-to-back while reclaim keeps retrying xswap folios.
On each wakeup, shrink_worker() runs MAX_RECLAIM_RETRIES full passes over
every online memcg and NUMA node, taking and dropping l->lock on every
allocated empty list_lru_one (or 16 empty root LRU walks when memcg is
disabled). Gating the wakeup on whether zswap_list_lru actually has
entries would avoid this background spin.
[Severity: Low]
Should the synchronous shrink_memcg() call in zswap_store() also check
nr_real_swapfiles?
When no real swap device is active (nr_real_swapfiles == 0) and a cgroup
hits memory.zswap.max, dirty xswap folios already in swapcache still reach
zswap_store() and call shrink_memcg() synchronously. Because zswap_store()
allocated list_lru_one entries for the cgroup, shrink_memcg() starts a
block plug and takes l->lock on every NUMA node even though zswap_list_lru
is empty and no writeback target exists.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=2
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
2026-12-03 16:43 ` [PATCH v8 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas Baoquan He
2026-12-03 16:43 ` [PATCH v8 02/14] mm: xswap support for zswap Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices Baoquan He
` (10 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He, Kunwu Chan
From: Baoquan He <hebaoquan@kylinos.cn>
Change free_swap_cluster_info() to take struct swap_info_struct* instead
of (cluster_info, maxpages). It now extracts the fields from si and
clears si->cluster_info after freeing to avoid a double free on the
swapon() error path.
The new parameter also lets the xswap path access si->flags in the
function.
Reviewed-by: Chris Li <chrisl@kernel.org>
Reviewed-by: Kunwu Chan <chentao@kylinos.cn>
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/swapfile.c | 26 ++++++++++++++------------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 558c4b75bf92..35a079f2bb76 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3208,14 +3208,17 @@ static void wait_for_allocation(struct swap_info_struct *si)
}
}
-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;
+
+ 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 */
@@ -3227,6 +3230,7 @@ static void free_swap_cluster_info(struct swap_cluster_info *cluster_info,
spin_unlock(&ci->lock);
}
kvfree(cluster_info);
+ si->cluster_info = NULL;
}
/*
@@ -3254,11 +3258,9 @@ static void flush_percpu_swap_cluster(struct swap_info_struct *si)
SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
{
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))
@@ -3350,10 +3352,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);
@@ -3361,7 +3359,9 @@ 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;
@@ -3728,6 +3728,8 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
if (!cluster_info)
goto err;
+ si->cluster_info = cluster_info;
+
for (i = 0; i < nr_clusters; i++)
spin_lock_init(&cluster_info[i].lock);
@@ -3791,7 +3793,7 @@ 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;
}
@@ -4013,7 +4015,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
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct
2026-12-03 16:43 ` [PATCH v8 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He, Kunwu Chan
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] mm: redundant cluster_info assignments in swapoff, swapon, and
cluster setup
--
commit f9d22060727a70e6e930f46ace975242c7c96f88
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: refactor free_swap_cluster_info to take swap_info_struct
This commit changes free_swap_cluster_info() to take a swap_info_struct
pointer instead of separate cluster_info and maxpages arguments, and has it
clear si->cluster_info after freeing.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 558c4b75bf928..35a079f2bb767 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -3361,7 +3359,9 @@ 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;
[ ... ]
> @@ -3791,7 +3793,7 @@ 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;
> }
[ ... ]
> @@ -4013,7 +4015,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;
[Severity: Low]
Can these three redundant cluster_info assignments be dropped?
free_swap_cluster_info() now clears si->cluster_info to NULL before
returning, so the trailing p->cluster_info = NULL in swapoff() and
si->cluster_info = NULL in swapon() are no longer needed.
Likewise, setup_swap_clusters_info() now assigns si->cluster_info right
after kvzalloc_objs(), making the second si->cluster_info = cluster_info
before return 0 redundant.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=3
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (2 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Baoquan He
` (9 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
Add CONFIG_XSWAP and the per-device state for the sparse cluster array.
An xswap device does not have to allocate cluster_info for its whole
address space at once. Create the array as a VM_SPARSE | VM_ALLOC area,
which reserves the whole range but lets vmalloc allocate and track the
backing pages, and map only the first chunk at setup. The map helper
extends the mapped range as clusters become visible, and a failed grow
leaves the previous backing in place.
Setup, free and the two cluster_info scans must change together; free
uses vfree(), which drops the mapping and the pages together.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/swap.h | 6 ++
mm/Kconfig | 9 ++
mm/swapfile.c | 203 +++++++++++++++++++++++++++++++++++++++++--
3 files changed, 209 insertions(+), 9 deletions(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 9b73a038e542..3b4fd8e9781c 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -242,6 +242,12 @@ 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; /* array, one entry per cluster */
+#ifdef CONFIG_XSWAP
+ struct vm_struct *cluster_vm; /* VM_SPARSE | VM_ALLOC area */
+ unsigned long nr_clusters_max;/* total clusters in the xswap address space */
+ unsigned long nr_clusters_mapped; /* currently mapped cluster count */
+ 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];
diff --git a/mm/Kconfig b/mm/Kconfig
index 18925ccb4adc..d238a4971b1e 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 && ZSWAP
+ 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 is mapped lazily
+ in chunks, avoiding static pre-allocation overhead.
+
config ZSMALLOC
tristate
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 35a079f2bb76..de2feaa2a628 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -49,6 +49,15 @@
#include "internal.h"
#include "swap.h"
+#ifdef CONFIG_XSWAP
+/*
+ * Clusters mapped per grow: one page worth of cluster_info, so the
+ * vmalloc page table overhead stays proportional to the mapped range.
+ */
+#define XSWAP_GROW_CLUSTERS \
+ max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16)
+#endif
+
static void swap_range_alloc(struct swap_info_struct *si,
unsigned int nr_entries);
static bool folio_swapcache_freeable(struct folio *folio);
@@ -1983,6 +1992,20 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
offset = swp_offset(entry);
if (offset >= si->max)
goto put_out;
+#ifdef CONFIG_XSWAP
+ if (si->flags & SWP_XSWAP) {
+ /* Pairs with the release store in xswap_map_clusters(). */
+ unsigned long mapped = smp_load_acquire(&si->nr_clusters_mapped);
+
+ /*
+ * xswap maps cluster_info lazily. A stale entry can point
+ * past nr_clusters_mapped; reject it before anything
+ * dereferences cluster_info.
+ */
+ if (offset / SWAPFILE_CLUSTER >= mapped)
+ goto put_out;
+ }
+#endif
return si;
bad_nofile:
@@ -2863,10 +2886,27 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
unsigned int prev)
{
struct swap_cluster_info *ci;
- unsigned long i, end;
+ unsigned long i, cluster_end, end;
unsigned int ci_off;
unsigned long swp_tb;
+ end = si->max;
+#ifdef CONFIG_XSWAP
+ /*
+ * An xswap device maps its cluster_info in chunks as it grows, so
+ * the tail past nr_clusters_mapped has nothing behind it.
+ */
+ if (si->flags & SWP_XSWAP) {
+ unsigned long mapped_end;
+
+ /* Pairs with the smp_store_release() in xswap_map_clusters(). */
+ mapped_end = smp_load_acquire(&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
@@ -2874,11 +2914,11 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
* allocations from this area (while holding swap_lock).
*/
i = prev + 1;
- while (i < si->max) {
+ while (i < end) {
ci = __swap_offset_to_cluster(si, i);
- end = min_t(unsigned long,
- ALIGN_DOWN(i, SWAPFILE_CLUSTER) + SWAPFILE_CLUSTER,
- si->max);
+ cluster_end = min_t(unsigned long,
+ ALIGN_DOWN(i, SWAPFILE_CLUSTER) + SWAPFILE_CLUSTER,
+ end);
/*
* An empty cluster has no slot in use, so skip it whole.
@@ -2888,13 +2928,13 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
* enough, unlike in every other cluster_is_empty() caller.
*/
if (!READ_ONCE(ci->count)) {
- i = end;
+ i = cluster_end;
cond_resched();
continue;
}
ci_off = i % SWAPFILE_CLUSTER;
- for (; i < end; ci_off++, i++) {
+ for (; i < cluster_end; ci_off++, i++) {
swp_tb = swap_table_get(ci, ci_off);
if (!swp_tb_is_null(swp_tb) && !swp_tb_is_bad(swp_tb))
return i;
@@ -2904,7 +2944,6 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si,
return 0;
}
-
static int try_to_unuse(unsigned int type)
{
struct mm_struct *prev_mm;
@@ -3202,6 +3241,14 @@ static void wait_for_allocation(struct swap_info_struct *si)
BUG_ON(si->flags & SWP_WRITEOK);
+#ifdef CONFIG_XSWAP
+ if (si->flags & SWP_XSWAP) {
+ /* Pairs with the smp_store_release() in xswap_map_clusters(). */
+ end = min(end, smp_load_acquire(&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);
@@ -3213,11 +3260,38 @@ 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;
+ unsigned long i, nr_clusters;
if (!cluster_info)
return;
+#ifdef CONFIG_XSWAP
+ if (si->flags & SWP_XSWAP) {
+ unsigned long nr_mapped;
+
+ /*
+ * Cluster 0 keeps the bad header slot, so it never empties
+ * and __free_cluster() never frees its table.
+ */
+ /* Pairs with the smp_store_release() in xswap_map_clusters(). */
+ nr_mapped = smp_load_acquire(&si->nr_clusters_mapped);
+ for (i = 0; i < nr_mapped; i++) {
+ ci = &cluster_info[i];
+ spin_lock(&ci->lock);
+ if (cluster_table_is_alloced(ci)) {
+ swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true);
+ swap_cluster_free_table(ci);
+ }
+ spin_unlock(&ci->lock);
+ }
+ vfree(si->cluster_info);
+ si->cluster_vm = NULL;
+ si->cluster_info = NULL;
+ 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;
@@ -3715,6 +3789,54 @@ 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)
+{
+ /* vm_area_grow_pages() takes an offset into the area, not an address. */
+ unsigned long vm_end = PAGE_ALIGN((size_t)(start_idx + nr) *
+ sizeof(struct swap_cluster_info));
+ unsigned int noreclaim_flags;
+ unsigned long i;
+ int err;
+
+ mutex_lock(&si->xswap_lock);
+
+ /* Refuse a stale range: the boundary moved since the caller read it. */
+ if (start_idx != READ_ONCE(si->nr_clusters_mapped)) {
+ mutex_unlock(&si->xswap_lock);
+ return -EAGAIN;
+ }
+ if (start_idx + nr > si->nr_clusters_max) {
+ mutex_unlock(&si->xswap_lock);
+ return -EAGAIN;
+ }
+
+ /* Prevent recursive reclaim during page table allocation. */
+ noreclaim_flags = memalloc_noreclaim_save();
+ /* __GFP_ZERO: cluster_info pointer fields must start NULL. */
+ err = vm_area_grow_pages(si->cluster_vm, vm_end,
+ GFP_KERNEL | __GFP_ZERO |
+ __GFP_HIGH | __GFP_NOMEMALLOC);
+ memalloc_noreclaim_restore(noreclaim_flags);
+ if (err) {
+ mutex_unlock(&si->xswap_lock);
+ return err;
+ }
+
+ for (i = start_idx; i < start_idx + nr; i++)
+ spin_lock_init(&si->cluster_info[i].lock);
+
+ /*
+ * Publish the new mappings and cluster lock initialization before
+ * the count; walkers without xswap_lock use smp_load_acquire().
+ */
+ smp_store_release(&si->nr_clusters_mapped, start_idx + nr);
+ mutex_unlock(&si->xswap_lock);
+ return 0;
+}
+#endif /* CONFIG_XSWAP */
+
static int setup_swap_clusters_info(struct swap_info_struct *si,
union swap_header *swap_header,
unsigned long maxpages)
@@ -3724,6 +3846,69 @@ 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 | VM_ALLOC);
+ if (!vm)
+ goto err;
+
+ cluster_info = vm->addr;
+ si->cluster_vm = vm;
+ si->nr_clusters_max = nr_clusters;
+ si->cluster_info = cluster_info;
+
+ /* Must be initialized before xswap_map_clusters() locks it. */
+ mutex_init(&si->xswap_lock);
+
+ err = xswap_map_clusters(si, 0, min_t(unsigned long,
+ XSWAP_GROW_CLUSTERS, nr_clusters));
+ if (err)
+ 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_free_vm;
+
+ 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]);
+ }
+
+ /*
+ * Cluster 0 holds the header slot, which is marked bad, so it
+ * is not entirely free. si->max is cluster aligned, so no
+ * cluster holds a slot past the end of the device.
+ */
+ for (i = 0; i < si->nr_clusters_mapped; i++) {
+ struct swap_cluster_info *ci = &cluster_info[i];
+
+ if (ci->count) {
+ 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);
+ }
+ }
+
+ return 0;
+
+err_free_vm:
+ vfree(si->cluster_info);
+ si->cluster_vm = NULL;
+ si->cluster_info = NULL;
+ si->nr_clusters_mapped = 0;
+ return err;
+ }
+#endif /* CONFIG_XSWAP */
+
cluster_info = kvzalloc_objs(*cluster_info, nr_clusters);
if (!cluster_info)
goto err;
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices
2026-12-03 16:43 ` [PATCH v8 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: spurious SIGBUS in do_swap_page() for stale xswap entries
- [Low] mm: duplicated xswap cluster setup and teardown in swapfile.c
- [Low] mm: data race on area->pages in vm_area_grow_pages() vs
show_busy_info()
--
commit 870c51b1a2136c162ae15350e3332e819a3a39bd
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: use a VM_SPARSE cluster array for xswap devices
This commit adds CONFIG_XSWAP and backs an xswap device's cluster_info
array with a VM_SPARSE | VM_ALLOC vmalloc area mapped lazily in chunks.
It also updates get_swap_device(), find_next_to_unuse(),
wait_for_allocation(), and free_swap_cluster_info() to bound accesses by
nr_clusters_mapped.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 35a079f2bb767..de2feaa2a6287 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -1983,6 +1992,20 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)
> offset = swp_offset(entry);
> if (offset >= si->max)
> goto put_out;
> +#ifdef CONFIG_XSWAP
> + if (si->flags & SWP_XSWAP) {
> + /* Pairs with the release store in xswap_map_clusters(). */
> + unsigned long mapped = smp_load_acquire(&si->nr_clusters_mapped);
> +
> + /*
> + * xswap maps cluster_info lazily. A stale entry can point
> + * past nr_clusters_mapped; reject it before anything
> + * dereferences cluster_info.
> + */
> + if (offset / SWAPFILE_CLUSTER >= mapped)
> + goto put_out;
> + }
> +#endif
>
> return si;
[Severity: Medium]
Can jumping to put_out here cause a spurious SIGBUS in do_swap_page()?
CPU0 CPU1
do_swap_page()
pte_unmap_same(vmf)
swapoff()
unuse_mm() /* mmap_read_lock */
/* replaces swap PTE */
swapon() /* xswap, reuses slot */
xswap_map_clusters()
get_swap_device(entry)
goto put_out <-- returns ERR_PTR(-EIO)
ret = VM_FAULT_SIGBUS
do_swap_page() drops the page table lock in pte_unmap_same() before
calling get_swap_device(entry) under mmap_read_lock or the per-VMA read
lock. Because unuse_mm() only takes mmap_read_lock, a concurrent
swapoff() followed by swapon() of an xswap device can reuse the same
swap_info[] slot with nr_clusters_mapped initialized to
XSWAP_GROW_CLUSTERS.
When do_swap_page() then passes the stale entry, put_out logs a
"Bad swap offset entry" error and returns ERR_PTR(-EIO), which
do_swap_page() turns directly into VM_FAULT_SIGBUS without re-locking
the page table lock to check whether the PTE changed.
[ ... ]
> @@ -3724,6 +3846,69 @@ 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 | VM_ALLOC);
> + if (!vm)
> + goto err;
> +
> + cluster_info = vm->addr;
> + si->cluster_vm = vm;
> + si->nr_clusters_max = nr_clusters;
> + si->cluster_info = cluster_info;
> +
> + /* Must be initialized before xswap_map_clusters() locks it. */
> + mutex_init(&si->xswap_lock);
> +
> + err = xswap_map_clusters(si, 0, min_t(unsigned long,
> + XSWAP_GROW_CLUSTERS, nr_clusters));
> + if (err)
> + goto err_free_vm;
[Severity: Low]
Is there a data race on area->pages between the initial
xswap_map_clusters() call and /proc/vmallocinfo?
get_vm_area() inserts the vm_struct onto vn->busy.head without
VM_UNINITIALIZED, making it immediately visible to show_busy_info().
On the first grow, vm_area_grow_pages() assigns area->pages without
vn->busy.lock:
mm/vmalloc.c:vm_area_grow_pages() {
...
if (!old) {
area->pages = pages;
} else {
memcpy(pages, old, area->nr_pages * sizeof(*pages));
spin_lock(&vn->busy.lock);
area->pages = pages;
spin_unlock(&vn->busy.lock);
...
}
Concurrently, show_busy_info() reads v->pages under vn->busy.lock in
is_vmalloc_addr(v->pages), which can trigger a KCSAN report or an
inconsistent vpages tag. Taking vn->busy.lock around area->pages = pages
in the !old branch too would close the race.
[ ... ]
> + 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]);
> + }
> +
> + /*
> + * Cluster 0 holds the header slot, which is marked bad, so it
> + * is not entirely free. si->max is cluster aligned, so no
> + * cluster holds a slot past the end of the device.
> + */
> + for (i = 0; i < si->nr_clusters_mapped; i++) {
> + struct swap_cluster_info *ci = &cluster_info[i];
> +
> + if (ci->count) {
> + 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);
> + }
> + }
> +
> + return 0;
> +
> +err_free_vm:
> + vfree(si->cluster_info);
> + si->cluster_vm = NULL;
> + si->cluster_info = NULL;
> + si->nr_clusters_mapped = 0;
> + return err;
> + }
> +#endif /* CONFIG_XSWAP */
[Severity: Low]
Can the xswap path share the cluster list initialization, insertion loop,
and error cleanup with the non-xswap path below?
The list setup and cluster insertion loop here duplicate the ones at the
end of setup_swap_clusters_info(), and err_free_vm duplicates the xswap
teardown in free_swap_cluster_info() while omitting per-cluster table
cleanup, so jumping to the existing err label would also clean up
cluster 0's table on error. The same per-cluster table freeing loop is
also duplicated between the xswap and non-xswap branches in
free_swap_cluster_info().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=4
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (3 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-03 16:43 ` [PATCH v8 06/14] mm, swap: create the xswap device from swapon(2) Baoquan He
` (8 subsequent siblings)
13 siblings, 0 replies; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
si->max and si->pages are unsigned int. With 4 KiB pages, this limits
an xswap device to 16 TB, and xswap may need a larger address space.
Change both fields to unsigned long, together with the offsets and
indexes that carry them.
A real swap device is still limited by swap_header.info.last_page,
which is __u32, so this only widens the xswap address space. The old
overflow clamp in read_swap_header() is no longer needed; the header
check still limits real devices.
count_swap_pages() returns unsigned int and is only used by
hibernation, which never selects an xswap device. If that ever
changes, WARN and return 0 instead of silently truncating the value.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/swap.h | 4 ++--
mm/swapfile.c | 57 +++++++++++++++++++++++---------------------
2 files changed, 32 insertions(+), 29 deletions(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 3b4fd8e9781c..b01f5661b450 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -240,7 +240,7 @@ struct swap_info_struct {
signed short prio; /* swap priority of this type */
struct plist_node list; /* entry in swap_active_head */
signed char type; /* strange name for an index */
- unsigned int max; /* size of this swap device */
+ unsigned long max; /* size of this swap device */
struct swap_cluster_info *cluster_info; /* array, one entry per cluster */
#ifdef CONFIG_XSWAP
struct vm_struct *cluster_vm; /* VM_SPARSE | VM_ALLOC area */
@@ -254,7 +254,7 @@ struct swap_info_struct {
/* list of cluster that contains at least one free slot */
struct list_head frag_clusters[SWAP_NR_ORDERS];
/* list of cluster that are fragmented or contented */
- unsigned int pages; /* total of usable pages of swap */
+ unsigned long pages; /* total of usable pages of swap */
atomic_long_t inuse_pages; /* number of those currently in use */
struct swap_sequential_cluster *global_cluster; /* Use one global cluster for rotating device */
spinlock_t global_cluster_lock; /* Serialize usage of global cluster */
diff --git a/mm/swapfile.c b/mm/swapfile.c
index de2feaa2a628..912cfa68ba01 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -412,10 +412,10 @@ static inline unsigned int cluster_index(struct swap_info_struct *si,
return ci - si->cluster_info;
}
-static inline unsigned int cluster_offset(struct swap_info_struct *si,
- struct swap_cluster_info *ci)
+static inline unsigned long cluster_offset(struct swap_info_struct *si,
+ struct swap_cluster_info *ci)
{
- return cluster_index(si, ci) * SWAPFILE_CLUSTER;
+ return (unsigned long)cluster_index(si, ci) * SWAPFILE_CLUSTER;
}
static void swap_cluster_free_table_folio_rcu_cb(struct rcu_head *head)
@@ -863,7 +863,7 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
/* si->max may got shrunk by swap swap_activate() */
if (offset >= si->max && !mask) {
- pr_debug("Ignoring bad slot %u (max: %u)\n", offset, si->max);
+ pr_debug("Ignoring bad slot %u (max: %lu)\n", offset, si->max);
return 0;
}
/*
@@ -1045,11 +1045,12 @@ static bool swap_prefer_free_cluster(struct swap_info_struct *si)
}
/* Try use a new cluster for current CPU and allocate from it. */
-static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
- struct swap_cluster_info *ci,
- struct folio *folio, unsigned long offset)
+static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si,
+ struct swap_cluster_info *ci,
+ struct folio *folio,
+ unsigned long offset)
{
- unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
+ unsigned long next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
unsigned int order = likely(folio) ? folio_order(folio) : 0;
unsigned long end = start + SWAPFILE_CLUSTER;
@@ -1096,12 +1097,12 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
return found;
}
-static unsigned int alloc_swap_scan_list(struct swap_info_struct *si,
- struct list_head *list,
- struct folio *folio,
- bool scan_all)
+static unsigned long alloc_swap_scan_list(struct swap_info_struct *si,
+ struct list_head *list,
+ struct folio *folio,
+ bool scan_all)
{
- unsigned int found = SWAP_ENTRY_INVALID;
+ unsigned long found = SWAP_ENTRY_INVALID;
do {
struct swap_cluster_info *ci = isolate_lock_cluster(si, list);
@@ -1184,7 +1185,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
{
struct swap_cluster_info *ci;
unsigned int order = likely(folio) ? folio_order(folio) : 0;
- unsigned int offset = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
+ unsigned long offset = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
/*
* Swapfile is not block device so unable
@@ -1454,7 +1455,7 @@ static bool swap_alloc_fast(struct folio *folio)
unsigned int order = folio_order(folio);
struct swap_cluster_info *ci;
struct swap_info_struct *si;
- unsigned int offset;
+ unsigned long offset;
/*
* Once allocated, swap_info_struct will never be completely freed,
@@ -2559,9 +2560,15 @@ unsigned int count_swap_pages(int type, int free)
spin_lock(&sis->lock);
if (sis->flags & SWP_WRITEOK) {
- n = sis->pages;
- if (free)
- n -= swap_usage_in_pages(sis);
+ /* Hibernation only; it never selects an xswap device. */
+ if (unlikely(sis->flags & SWP_XSWAP)) {
+ WARN_ON_ONCE(1);
+ n = 0;
+ } else {
+ n = sis->pages;
+ if (free)
+ n -= swap_usage_in_pages(sis);
+ }
}
spin_unlock(&sis->lock);
}
@@ -2882,8 +2889,8 @@ static int unuse_mm(struct mm_struct *mm, unsigned int type)
* Return 0 if there are no inuse entries after prev till end of
* the map.
*/
-static unsigned int find_next_to_unuse(struct swap_info_struct *si,
- unsigned int prev)
+static unsigned long find_next_to_unuse(struct swap_info_struct *si,
+ unsigned long prev)
{
struct swap_cluster_info *ci;
unsigned long i, cluster_end, end;
@@ -2953,7 +2960,7 @@ static int try_to_unuse(unsigned int type)
struct swap_info_struct *si = swap_info[type];
struct folio *folio;
swp_entry_t entry;
- unsigned int i;
+ unsigned long i;
if (!swap_usage_in_pages(si))
goto success;
@@ -3767,12 +3774,8 @@ static unsigned long read_swap_header(struct swap_info_struct *si,
pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
K(maxpages), K(last_page));
}
- if (maxpages > last_page) {
+ if (maxpages > last_page)
maxpages = last_page + 1;
- /* p->max is an unsigned int: don't overflow it */
- if ((unsigned int)maxpages == 0)
- maxpages = UINT_MAX;
- }
if (!maxpages)
return 0;
@@ -4086,7 +4089,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
goto bad_swap_unlock_inode;
}
if (si->pages != si->max - 1) {
- pr_err("swap:%u != (max:%u - 1)\n", si->pages, si->max);
+ pr_err("swap:%lu != (max:%lu - 1)\n", si->pages, si->max);
error = -EINVAL;
goto bad_swap_unlock_inode;
}
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* [PATCH v8 06/14] mm, swap: create the xswap device from swapon(2)
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (4 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 07/14] mm, swap: refactor swapoff and add xswap_destroy Baoquan He
` (7 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
An xswap device has no backing file, so it has no path for swapon(2) to
open, and it needs a way to be activated. Give swapon(2) a name that
says exactly that:
swapon("xswap", SWAP_FLAG_PREFER | prio);
The name is not a path: swapon(2) opens specialfile the usual way first
and falls back to the device only when that fails with -ENOENT. A file
or directory called "xswap" therefore keeps precedence, and only the
exact string matches, so "./xswap" and absolute paths are files as
usual. This is the same rule swapoff(2) uses. There is no separate
flag and no second way to create it.
The call is still an activation: it takes a priority, it fails with
-EBUSY when the device exists, and /proc/swaps shows it as the fixed
"xswap" next to the real devices. Discard flags are rejected: there is
no storage to discard.
The device is sized at twice RAM, and its cluster array is mapped
lazily, so only the part that is used costs memory.
Hibernation device discovery skips xswap devices: there is no bdev to
store a resume image.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/swap.h | 1 -
mm/swapfile.c | 187 +++++++++++++++++++++++++++++++++++++++----
2 files changed, 172 insertions(+), 16 deletions(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index b01f5661b450..ba1bf7e09893 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -21,7 +21,6 @@
#define SWAP_FLAG_DISCARD 0x10000 /* enable discard for swap */
#define SWAP_FLAG_DISCARD_ONCE 0x20000 /* discard swap area at swapon-time */
#define SWAP_FLAG_DISCARD_PAGES 0x40000 /* discard page-clusters after use */
-
#define SWAP_FLAGS_VALID (SWAP_FLAG_PRIO_MASK | SWAP_FLAG_PREFER | \
SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | \
SWAP_FLAG_DISCARD_PAGES)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 912cfa68ba01..4e598ab8da65 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -48,6 +48,16 @@
#include "swap_table.h"
#include "internal.h"
#include "swap.h"
+#define DEF_SWAP_PRIO -1
+
+/*
+ * Highest priority: xswap must take the page before a real device does.
+ * At equal priority the list is FIFO, so an earlier real device wins.
+ */
+#define DEF_XSWAP_PRIO SWAP_FLAG_PRIO_MASK
+
+/* File-less, so /proc/swaps shows this name; swapoff -a passes it back. */
+#define XSWAP_NAME "xswap"
#ifdef CONFIG_XSWAP
/*
@@ -56,7 +66,18 @@
*/
#define XSWAP_GROW_CLUSTERS \
max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16)
-#endif
+
+static int xswap_create(int prio);
+static int xswap_create_by_name(const char *name, int swap_flags);
+
+/* Serializes create and destroy: at most one xswap device exists. */
+static DEFINE_MUTEX(xswap_ctl_lock);
+#else /* !CONFIG_XSWAP */
+static inline int xswap_create_by_name(const char *name, int swap_flags)
+{
+ return -ENOENT;
+}
+#endif /* CONFIG_XSWAP */
static void swap_range_alloc(struct swap_info_struct *si,
unsigned int nr_entries);
@@ -84,7 +105,6 @@ atomic_t nr_real_swapfiles;
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;
@@ -2334,6 +2354,9 @@ static int __find_hibernation_swap_type(dev_t device, sector_t offset)
if (!(sis->flags & SWP_WRITEOK))
continue;
+ /* xswap has no bdev to match a resume device */
+ if (sis->flags & SWP_XSWAP)
+ continue;
if (device == sis->bdev->bd_dev) {
struct swap_extent *se = first_se(sis);
@@ -2521,6 +2544,8 @@ int find_first_swap(dev_t *device)
if (!(sis->flags & SWP_WRITEOK))
continue;
+ if (sis->flags & SWP_XSWAP)
+ continue;
*device = sis->bdev->bd_dev;
spin_unlock(&swap_lock);
return type;
@@ -3497,7 +3522,8 @@ 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) && (si->flags & SWP_WRITEOK)))
continue;
if (!--l)
return si;
@@ -3518,7 +3544,8 @@ 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) && (si->flags & SWP_WRITEOK)))
continue;
return si;
}
@@ -3560,7 +3587,13 @@ 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 {
+ /* The file-less xswap device; one per system. */
+ len = strlen(XSWAP_NAME);
+ seq_puts(swap, XSWAP_NAME);
+ }
seq_printf(swap, "%*s%s\t%lu\t%s%lu\t%s%d\n",
len < 40 ? 40 - len : 1, " ",
swap_type_str(si),
@@ -3874,7 +3907,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
/* xswap: only cluster 0 slot 0 is bad */
err = swap_cluster_setup_bad_slot(si, cluster_info, 0, false);
if (err)
- goto err_free_vm;
+ goto err_unmap;
INIT_LIST_HEAD(&si->free_clusters);
INIT_LIST_HEAD(&si->full_clusters);
@@ -3903,6 +3936,9 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
return 0;
+err_unmap:
+ /* swap_cluster_setup_bad_slot() allocates cluster 0's table. */
+ swap_cluster_free_table(&si->cluster_info[0]);
err_free_vm:
vfree(si->cluster_info);
si->cluster_vm = NULL;
@@ -3985,6 +4021,117 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
return err;
}
+#ifdef CONFIG_XSWAP
+/*
+ * Create the file-less xswap device, sized at twice RAM. Called from
+ * swapon(2) with SWAP_FLAG_XSWAP; there is at most one device per system.
+ */
+static int xswap_create(int prio)
+{
+ struct swap_info_struct *si;
+ unsigned long ram, maxpages;
+ unsigned int type;
+ int error;
+
+ mutex_lock(&xswap_ctl_lock);
+
+ spin_lock(&swap_lock);
+ for (type = 0; type < nr_swapfiles; type++) {
+ struct swap_info_struct *q = swap_info[type];
+
+ if (q && (q->flags & SWP_XSWAP)) {
+ spin_unlock(&swap_lock);
+ error = -EBUSY;
+ goto out;
+ }
+ }
+ spin_unlock(&swap_lock);
+
+ si = alloc_swap_info();
+ if (IS_ERR(si)) {
+ error = PTR_ERR(si);
+ goto out;
+ }
+
+ 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 * 2, swapfile_maximum_size);
+ /* A cluster index is an unsigned int, so cap the cluster count. */
+ if (maxpages / SWAPFILE_CLUSTER > UINT_MAX)
+ maxpages = (unsigned long)UINT_MAX * SWAPFILE_CLUSTER;
+ /* Cluster-aligned, so no cluster holds a slot past si->max. */
+ if (maxpages > SWAPFILE_CLUSTER)
+ maxpages = rounddown(maxpages, SWAPFILE_CLUSTER);
+ if (maxpages < 2)
+ maxpages = 2;
+
+ si->bdev = NULL;
+ si->swap_file = NULL;
+ si->flags |= SWP_XSWAP | SWP_SOLIDSTATE;
+ si->max = maxpages;
+ si->pages = maxpages - 1;
+ /*
+ * setup_swap_extents() is file-only, so set ops here; only
+ * ops->flags is used, and the IO methods are never called.
+ */
+ si->ops = &swap_bdev_ops;
+
+ error = setup_swap_clusters_info(si, NULL, maxpages);
+ if (error)
+ goto bad_swap;
+
+ 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;
+ enable_swap_info(si);
+ pr_info("xswap: adding extendable swap device %s (prio %d, %lu pages)\n",
+ XSWAP_NAME, prio, si->pages);
+ mutex_unlock(&swapon_mutex);
+ atomic_inc(&proc_poll_event);
+ wake_up_interruptible(&proc_poll_wait);
+
+ error = 0;
+out:
+ mutex_unlock(&xswap_ctl_lock);
+ return error;
+
+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);
+ goto out;
+}
+
+/* A name, not a path: only -ENOENT from opening it gets here. */
+static int xswap_create_by_name(const char *name, int swap_flags)
+{
+ int prio = DEF_XSWAP_PRIO;
+
+ if (strcmp(name, XSWAP_NAME))
+ return -ENOENT;
+
+ if (swap_flags & (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE |
+ SWAP_FLAG_DISCARD_PAGES))
+ return -EINVAL;
+ if (swap_flags & SWAP_FLAG_PREFER)
+ prio = swap_flags & SWAP_FLAG_PRIO_MASK;
+
+ return xswap_create(prio);
+}
+#endif /* CONFIG_XSWAP */
+
SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
{
struct swap_info_struct *si;
@@ -4007,26 +4154,36 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
+ CLASS(filename, name)(specialfile);
+ if (IS_ERR(name))
+ return PTR_ERR(name);
+
+ swap_file = file_open_name(name, O_RDWR | O_LARGEFILE | O_EXCL, 0);
+ if (IS_ERR(swap_file)) {
+ error = PTR_ERR(swap_file);
+ /*
+ * Only -ENOENT is the request name; a file of that name
+ * keeps precedence, as it does for swapoff.
+ */
+ if (error == -ENOENT)
+ return xswap_create_by_name(name->name, swap_flags);
+ return error;
+ }
+
/*
* Allocate or reuse existing !SWP_USED swap_info. The returned
* si will stay in a dying status, so nothing will access its content
* until enable_swap_info resurrects its percpu ref and expose it.
*/
si = alloc_swap_info();
- if (IS_ERR(si))
+ if (IS_ERR(si)) {
+ filp_close(swap_file, NULL);
return PTR_ERR(si);
+ }
INIT_WORK(&si->discard_work, swap_discard_work);
INIT_WORK(&si->reclaim_work, swap_reclaim_work);
- CLASS(filename, name)(specialfile);
- swap_file = file_open_name(name, O_RDWR | O_LARGEFILE | O_EXCL, 0);
- if (IS_ERR(swap_file)) {
- error = PTR_ERR(swap_file);
- swap_file = NULL;
- goto bad_swap;
- }
-
mapping = swap_file->f_mapping;
dentry = swap_file->f_path.dentry;
inode = mapping->host;
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 06/14] mm, swap: create the xswap device from swapon(2)
2026-12-03 16:43 ` [PATCH v8 06/14] mm, swap: create the xswap device from swapon(2) Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm, swap: xswap_create() breaks large-folio swapout by omitting
SWP_BLKDEV
- [Medium] mm: unsynchronized si->flags reads and SWP_WRITEOK gating in
/proc/swaps
- [Medium] mm, swap: xswap_create() omits SWP_SYNCHRONOUS_IO, hurting
packing and swapin
--
commit 3ff5334f3639c75b784e10b181014a401d16ea84
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: create the xswap device from swapon(2)
This commit allows swapon(2) to create a file-less xswap device when
passed the name "xswap" and no matching file exists, exposes it in
/proc/swaps, and skips it during hibernation device discovery.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 912cfa68ba01a..4e598ab8da656 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -3497,7 +3522,8 @@ 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) && (si->flags & SWP_WRITEOK)))
> continue;
> if (!--l)
> return si;
> @@ -3518,7 +3544,8 @@ 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) && (si->flags & SWP_WRITEOK)))
> continue;
> return si;
> }
[Severity: Medium]
Can these unmarked reads of si->flags race with concurrent writers, and
does gating on SWP_WRITEOK hide xswap from /proc/swaps during swapoff?
swap_start() and swap_next() hold only swapon_mutex, which protects
si->swap_file rather than si->flags:
CPU0 (/proc/swaps) CPU1 (swapoff)
swap_start()
mutex_lock(&swapon_mutex)
del_from_avail_list()
spin_lock(&swap_lock)
si->flags &= ~SWP_WRITEOK
spin_unlock(&swap_lock)
si->flags & SWP_WRITEOK <-- data race
Similarly, xswap_create() modifies si->flags without holding swap_lock
after alloc_swap_info() has already published si in swap_info[], racing
with si_swapinfo() (via sysinfo(2)) which reads si->flags under swap_lock.
Checking SWP_WRITEOK here also hides the xswap device from /proc/swaps
as soon as swapoff clears SWP_WRITEOK before try_to_unuse(), instead of
keeping it visible to show unuse progress as established by commit
eca4d01b982d ("mm, swap: protect si->swap_file properly and use as a mount indicator").
[ ... ]
> +static int xswap_create(int prio)
> +{
[ ... ]
> + si->bdev = NULL;
> + si->swap_file = NULL;
> + si->flags |= SWP_XSWAP | SWP_SOLIDSTATE;
[Severity: High]
Does omitting SWP_BLKDEV here disable large-folio swapout system-wide
once xswap is active?
xswap defaults to DEF_XSWAP_PRIO (SWAP_FLAG_PRIO_MASK), placing it ahead
of real swap partitions on swap_avail_head. When shrink_folio_list()
calls folio_alloc_swap() -> swap_alloc_slow() on a large folio,
cluster_alloc_swap_entry() rejects order > 0 because SWP_BLKDEV is unset.
swap_alloc_slow() then returns immediately for large folios without
trying lower-priority devices, so folio_alloc_swap() returns -E2BIG and
shrink_folio_list() splits every THP and mTHP into order-0 pages. Since
SWP_BLKDEV paths dereference si->bdev, should cluster_alloc_swap_entry()
also allow SWP_XSWAP for order > 0 allocations?
[Severity: Medium]
Should SWP_SYNCHRONOUS_IO also be set on the RAM-only xswap device?
Without SWP_SYNCHRONOUS_IO, swap_prefer_free_cluster() returns true, so
cluster_alloc_swap_entry() allocates fresh free clusters and swap tables
before reusing nonfull clusters, inflating the lazy cluster footprint.
On read faults, should_try_to_free_swap() also returns false without
SWP_SYNCHRONOUS_IO, leaving folios pinned in swapcache even after
zswap_load() has deleted the zswap entry and dirtied the folio.
Finally, swap_entry_synchronous() falls back to an extra
zswap_is_present() XArray lookup on anonymous faults (and sends
zero-mapped entries through swapin_readahead()), while
shmem_swapin_folio() falls back to shmem_swapin_cluster() instead of
direct synchronous swap-in.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=6
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 07/14] mm, swap: refactor swapoff and add xswap_destroy
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (5 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 06/14] mm, swap: create the xswap device from swapon(2) Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 08/14] mm, swap: require zswap for xswap devices Baoquan He
` (6 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
Extract __swapoff() from sys_swapoff() so the teardown logic can be
shared, and make it work for file-less devices.
Add xswap_destroy(), reached through the name /proc/swaps prints.
"xswap" is not a path, so the name cannot be opened, and "swapoff -a"
passes it back unchanged.
A real file or directory keeps precedence: only a name that cannot be
opened (-ENOENT) is taken for the device, so a plain file is never
mistaken for it. The name is compared with strcmp() against the one
name the kernel prints, so there is no parsing.
Destroy is serialized with create by xswap_ctl_lock, and it refuses a
device that is pinned for hibernation.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/swapfile.c | 148 +++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 127 insertions(+), 21 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 4e598ab8da65..1f319e365b68 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -69,6 +69,8 @@
static int xswap_create(int prio);
static int xswap_create_by_name(const char *name, int swap_flags);
+static int xswap_destroy(void);
+static int xswap_destroy_by_name(const char *name);
/* Serializes create and destroy: at most one xswap device exists. */
static DEFINE_MUTEX(xswap_ctl_lock);
@@ -77,6 +79,11 @@ static inline int xswap_create_by_name(const char *name, int swap_flags)
{
return -ENOENT;
}
+
+static inline int xswap_destroy_by_name(const char *name)
+{
+ return -ENOENT;
+}
#endif /* CONFIG_XSWAP */
static void swap_range_alloc(struct swap_info_struct *si,
@@ -3360,13 +3367,14 @@ static void flush_percpu_swap_cluster(struct swap_info_struct *si)
}
}
+static int swap_info_remove(struct swap_info_struct *p);
+static int __swapoff(struct swap_info_struct *p);
SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
{
struct swap_info_struct *p = NULL;
- struct file *swap_file, *victim;
+ struct file *victim;
struct address_space *mapping;
- struct inode *inode;
int err, found = 0;
if (!capable(CAP_SYS_ADMIN))
@@ -3375,15 +3383,30 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
BUG_ON(!current->mm);
CLASS(filename, pathname)(specialfile);
+ if (IS_ERR(pathname))
+ return PTR_ERR(pathname);
+
victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
- if (IS_ERR(victim))
- return PTR_ERR(victim);
+ if (IS_ERR(victim)) {
+ /*
+ * /proc/swaps shows a name that cannot be opened; accept it
+ * so swapoff -a works. Only -ENOENT means "no such path",
+ * so a file of that name keeps precedence.
+ */
+ if (PTR_ERR(victim) != -ENOENT)
+ return PTR_ERR(victim);
+
+ err = xswap_destroy_by_name(pathname->name);
+ if (err == -ENOENT)
+ return PTR_ERR(victim);
+ return err;
+ }
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) {
+ if (p->swap_file && p->swap_file->f_mapping == mapping) {
found = 1;
break;
}
@@ -3402,20 +3425,50 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
goto out_dput;
}
- if (!security_vm_enough_memory_mm(current->mm, p->pages))
- vm_unacct_memory(p->pages);
- else {
- err = -ENOMEM;
+ err = swap_info_remove(p);
+ if (err) {
spin_unlock(&swap_lock);
goto out_dput;
}
+ spin_unlock(&swap_lock);
+
+ err = __swapoff(p);
+
+out_dput:
+ filp_close(victim, NULL);
+ return err;
+}
+
+/*
+ * Drop @p from the avail and active lists and undo its accounting. The
+ * caller must hold swap_lock and have checked that @p is WRITEOK and not
+ * pinned for hibernation.
+ *
+ * Returns 0, or -ENOMEM with swap_lock still held.
+ */
+static int swap_info_remove(struct swap_info_struct *p)
+{
+ if (!security_vm_enough_memory_mm(current->mm, p->pages))
+ vm_unacct_memory(p->pages);
+ else
+ 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 0;
+}
+
+/* Common swap teardown after list removal; shared by sys_swapoff() and
+ * xswap_destroy().
+ */
+static int __swapoff(struct swap_info_struct *p)
+{
+ struct file *swap_file = NULL;
+ int err;
wait_for_allocation(p);
@@ -3426,7 +3479,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;
}
/*
@@ -3466,15 +3519,21 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
kfree(p->global_cluster);
p->global_cluster = NULL;
free_swap_cluster_info(p);
+ /*
+ * The device is off swap_active_head and no longer WRITEOK, so no
+ * reader can observe these; clearing them here needs no lock.
+ */
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
@@ -3485,13 +3544,10 @@ 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);
-out_dput:
- filp_close(victim, NULL);
- return err;
+ return 0;
}
#ifdef CONFIG_PROC_FS
@@ -4114,7 +4170,57 @@ static int xswap_create(int prio)
goto out;
}
-/* A name, not a path: only -ENOENT from opening it gets here. */
+/* Tear down the file-less xswap device. */
+static int xswap_destroy(void)
+{
+ struct swap_info_struct *p = NULL;
+ unsigned int type;
+ int err;
+
+ mutex_lock(&xswap_ctl_lock);
+
+ spin_lock(&swap_lock);
+ /* Only a fully enabled device; a half-created one does not count. */
+ for (type = 0; type < nr_swapfiles; type++) {
+ struct swap_info_struct *q = swap_info[type];
+
+ if (q && (q->flags & (SWP_XSWAP | SWP_WRITEOK)) ==
+ (SWP_XSWAP | SWP_WRITEOK)) {
+ p = q;
+ break;
+ }
+ }
+ if (!p) {
+ spin_unlock(&swap_lock);
+ err = -ENOENT;
+ goto out;
+ }
+ /* Refuse swapoff while the device is pinned for hibernation */
+ if (p->flags & SWP_HIBERNATION) {
+ spin_unlock(&swap_lock);
+ err = -EBUSY;
+ goto out;
+ }
+
+ err = swap_info_remove(p);
+ spin_unlock(&swap_lock);
+ if (!err)
+ err = __swapoff(p);
+out:
+ mutex_unlock(&xswap_ctl_lock);
+ return err;
+}
+
+/* /proc/swaps shows XSWAP_NAME; accept it so swapoff -a works. */
+static int xswap_destroy_by_name(const char *name)
+{
+ if (strcmp(name, XSWAP_NAME))
+ return -ENOENT;
+
+ return xswap_destroy();
+}
+
+/* The swapon counterpart of xswap_destroy_by_name(). */
static int xswap_create_by_name(const char *name, int swap_flags)
{
int prio = DEF_XSWAP_PRIO;
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 07/14] mm, swap: refactor swapoff and add xswap_destroy
2026-12-03 16:43 ` [PATCH v8 07/14] mm, swap: refactor swapoff and add xswap_destroy Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: swapoff() open-first relative lookup for xswap enables
CWD shadowing
- [Medium] mm: xswap_destroy() holds uninterruptible xswap_ctl_lock
across __swapoff()
- [Low] mm: unannotated data race on si->flags in /proc/swaps vs
xswap_destroy()
--
commit 238343efb8d379276ae632b33dfaa8a1b5aa260e
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: refactor swapoff and add xswap_destroy
This commit extracts swap_info_remove() and __swapoff() from sys_swapoff()
to support file-less swap devices, and adds xswap_destroy() reached via the
name printed in /proc/swaps.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 4e598ab8da656..1f319e365b68e 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -3375,15 +3383,30 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
> BUG_ON(!current->mm);
>
> CLASS(filename, pathname)(specialfile);
> + if (IS_ERR(pathname))
> + return PTR_ERR(pathname);
> +
> victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
> - if (IS_ERR(victim))
> - return PTR_ERR(victim);
> + if (IS_ERR(victim)) {
> + /*
> + * /proc/swaps shows a name that cannot be opened; accept it
> + * so swapoff -a works. Only -ENOENT means "no such path",
> + * so a file of that name keeps precedence.
> + */
> + if (PTR_ERR(victim) != -ENOENT)
> + return PTR_ERR(victim);
> +
> + err = xswap_destroy_by_name(pathname->name);
> + if (err == -ENOENT)
> + return PTR_ERR(victim);
> + return err;
> + }
[Severity: Medium]
Can resolving "xswap" through file_open_name() first break swapoff -a
and allow working-directory shadowing?
file_open_name() resolves relative names against AT_FDCWD, so any
./xswap file or directory in the working directory makes swapoff -a fail
with -EINVAL or -EISDIR instead of reaching xswap_destroy_by_name().
Userspace then has no unambiguous path to deactivate xswap.
A symlink ./xswap can also redirect root's O_RDWR open to another swap
device or device node, and a dangling symlink returns -ENOENT and falls
through to xswap_destroy() despite a filesystem entry existing.
[ ... ]
> @@ -3485,13 +3544,10 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
> p->flags = 0;
> spin_unlock(&swap_lock);
[Severity: Low]
Is there an unannotated data race on p->flags between xswap teardown
and /proc/swaps readers?
swap_start(), swap_next(), and swap_type_str() hold only swapon_mutex
and read si->flags directly without READ_ONCE() or swap_lock:
CPU0 CPU1
__swapoff() swap_start()
mutex_lock(&swapon_mutex)
...
mutex_unlock(&swapon_mutex)
mutex_lock(&swapon_mutex)
spin_lock(&swap_lock)
p->flags = 0 if (!si->swap_file &&
!((si->flags & SWP_XSWAP) &&
(si->flags & SWP_WRITEOK)))
<-- data race on si->flags
del_from_avail_list() and reinsert_swap_info() also modify si->flags
without swapon_mutex or WRITE_ONCE(). This triggers KCSAN reports and
allows tearing across the two flag checks; annotating these accesses
with READ_ONCE()/WRITE_ONCE() would fix the race.
[ ... ]
> @@ -4114,7 +4170,57 @@ static int xswap_create(int prio)
> goto out;
> }
>
> -/* A name, not a path: only -ENOENT from opening it gets here. */
> +/* Tear down the file-less xswap device. */
> +static int xswap_destroy(void)
> +{
> + struct swap_info_struct *p = NULL;
> + unsigned int type;
> + int err;
> +
> + mutex_lock(&xswap_ctl_lock);
> +
> + spin_lock(&swap_lock);
> + /* Only a fully enabled device; a half-created one does not count. */
> + for (type = 0; type < nr_swapfiles; type++) {
> + struct swap_info_struct *q = swap_info[type];
> +
> + if (q && (q->flags & (SWP_XSWAP | SWP_WRITEOK)) ==
> + (SWP_XSWAP | SWP_WRITEOK)) {
> + p = q;
> + break;
> + }
> + }
> + if (!p) {
> + spin_unlock(&swap_lock);
> + err = -ENOENT;
> + goto out;
> + }
> + /* Refuse swapoff while the device is pinned for hibernation */
> + if (p->flags & SWP_HIBERNATION) {
> + spin_unlock(&swap_lock);
> + err = -EBUSY;
> + goto out;
> + }
> +
> + err = swap_info_remove(p);
> + spin_unlock(&swap_lock);
> + if (!err)
> + err = __swapoff(p);
> +out:
> + mutex_unlock(&xswap_ctl_lock);
> + return err;
> +}
[Severity: Medium]
Can holding xswap_ctl_lock across __swapoff() stall concurrent swapon
and swapoff callers in unkillable D-state?
__swapoff() runs try_to_unuse(), which retries without a cap while
draining swap, followed by synchronize_rcu(), wait_for_completion(),
and flush_work(). Both xswap_create() and xswap_destroy() take
xswap_ctl_lock with mutex_lock(), so a concurrent caller cannot be
killed and can trigger hung-task warnings or panics.
Releasing xswap_ctl_lock before __swapoff() still keeps the single
device invariant: swap_info_remove() already cleared SWP_WRITEOK (so
another xswap_destroy() gets -ENOENT), while SWP_XSWAP stays set until
__swapoff() finishes (so xswap_create() gets -EBUSY).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=7
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 08/14] mm, swap: require zswap for xswap devices
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (6 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 07/14] mm, swap: refactor swapoff and add xswap_destroy Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 09/14] mm, swap: let the command line set the xswap device size Baoquan He
` (5 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
xswap pages live only in zswap. Without zswap, swapout cannot
free the page, but still takes a swap entry. So the device consumes
swap entries without freeing memory. Fail to create a device when
zswap is unavailable.
Acked-by: Chris Li <chrisl@kernel.org>
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/swapfile.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 1f319e365b68..ba899e349e99 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -4089,6 +4089,10 @@ static int xswap_create(int prio)
unsigned int type;
int error;
+ /* xswap has no backing store, it relies on zswap. */
+ if (!zswap_is_enabled())
+ return -EOPNOTSUPP;
+
mutex_lock(&xswap_ctl_lock);
spin_lock(&swap_lock);
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 08/14] mm, swap: require zswap for xswap devices
2026-12-03 16:43 ` [PATCH v8 08/14] mm, swap: require zswap for xswap devices Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: xswap_create() zswap check races and allows runtime zswap
disablement
--
commit 23d5b372a16f8090564baf7fd99d6589959b354c
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: require zswap for xswap devices
This commit checks zswap_is_enabled() in xswap_create() and returns
-EOPNOTSUPP when zswap is not enabled.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 1f319e365b68e..ba899e349e992 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -4089,6 +4089,10 @@ static int xswap_create(int prio)
> unsigned int type;
> int error;
>
> + /* xswap has no backing store, it relies on zswap. */
> + if (!zswap_is_enabled())
> + return -EOPNOTSUPP;
> +
> mutex_lock(&xswap_ctl_lock);
>
> spin_lock(&swap_lock);
[Severity: Medium]
Can zswap still be disabled via /sys/module/zswap/parameters/enabled
either concurrently with xswap_create() or after the xswap device is
active?
zswap_is_enabled() performs a plain unlocked read of zswap_enabled and
shares no lock with zswap_enabled_param_set(), which updates
zswap_enabled under zswap_init_lock and does not check for active xswap
devices:
CPU0 CPU1
xswap_create()
zswap_is_enabled() -> true
zswap_enabled_param_set()
zswap_enabled = false
enable_swap_info(si) <-- active xswap with zswap disabled
Once zswap_enabled is false with an active xswap device,
folio_alloc_swap() keeps allocating xswap slots, zswap_store() rejects
them, and swap_writeout() returns AOP_WRITEPAGE_ACTIVATE, keeping dirty
folios in memory while consuming xswap slots and starving backing swap.
Also, because this check runs before xswap_ctl_lock and the existing
xswap device scan, a duplicate swapon of xswap after zswap is disabled
returns -EOPNOTSUPP instead of -EBUSY.
Should xswap creation and teardown synchronize with
zswap_enabled_param_set() to reject disabling zswap with -EBUSY while an
xswap device is active, or should swap allocation skip xswap devices when
zswap is disabled?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=8
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 09/14] mm, swap: let the command line set the xswap device size
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (7 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 08/14] mm, swap: require zswap for xswap devices Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 10/14] mm, swap: add CONFIG_XSWAP_DEFAULT_MAX Baoquan He
` (4 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
By default, a new xswap device is 200% of RAM, which is twice RAM.
Add xswap.max= to set the size of devices created after it:
xswap.max=300% three times RAM
xswap.max=8G eight GiB
xswap.max=max the largest size the device can address
The default is fine for most users. But a machine with a lot of RAM
and a small working set can get a SwapTotal much larger than it needs.
The size is resolved at device creation, not at parameter parsing,
because a percentage depends on RAM, which is not known yet when the
parameters are parsed.
An explicit size that is too large or overflows is refused; the default
is clamped instead. A size below one cluster is also refused.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
.../admin-guide/kernel-parameters.txt | 7 +
mm/swapfile.c | 135 ++++++++++++++++--
2 files changed, 130 insertions(+), 12 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index dce9b8f3ae98..2b0fc585e0c7 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -8734,3 +8734,10 @@ Kernel parameters
memory, and other data can't be written using
xmon commands.
off xmon is disabled.
+
+ xswap.max= [KNL,SWAP]
+ Maximum size of a new xswap device. The value is a
+ size in bytes with an optional KMGTPE suffix, a
+ percentage of RAM, or "max" for the largest size
+ the device can address.
+ Format: <size>[KMGTPE] | <percent>% | max
diff --git a/mm/swapfile.c b/mm/swapfile.c
index ba899e349e99..6ce5dbb3b103 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -14,6 +14,7 @@
#include <linux/mman.h>
#include <linux/slab.h>
#include <linux/kernel_stat.h>
+#include <linux/overflow.h>
#include <linux/swap.h>
#include <linux/vmalloc.h>
#include <linux/pagemap.h>
@@ -74,6 +75,102 @@ static int xswap_destroy_by_name(const char *name);
/* Serializes create and destroy: at most one xswap device exists. */
static DEFINE_MUTEX(xswap_ctl_lock);
+
+/*
+ * Size of a new device: xswap.max= if given, otherwise the built-in
+ * default. A percentage unless bytes is set; "max" asks for the largest
+ * size the device can address.
+ */
+static unsigned int xswap_size_pct = 200;
+static unsigned long xswap_size_bytes;
+static bool xswap_size_max;
+static bool xswap_size_set; /* xswap.max= was given */
+
+/*
+ * Parse "200%", "2T" or "max". @str is not modified.
+ */
+static int __init xswap_parse_size(const char *str, const char *what)
+{
+ const char *pct;
+ char buf[16];
+ unsigned int pct_val;
+ unsigned long bytes;
+ char *end;
+
+ pct = strchr(str, '%');
+ if (!strcmp(str, "max")) {
+ xswap_size_max = true;
+ xswap_size_pct = 0;
+ xswap_size_bytes = 0;
+ } else if (pct) {
+ size_t len = pct - str;
+
+ if (pct[1] || !len || len >= sizeof(buf))
+ goto bad;
+ memcpy(buf, str, len);
+ buf[len] = '\0';
+ if (kstrtouint(buf, 0, &pct_val) || !pct_val)
+ goto bad;
+ xswap_size_pct = pct_val;
+ xswap_size_bytes = 0;
+ xswap_size_max = false;
+ } else {
+ bytes = memparse(str, &end);
+ if (*end || bytes < PAGE_SIZE)
+ goto bad;
+ xswap_size_bytes = bytes;
+ xswap_size_pct = 0;
+ xswap_size_max = false;
+ }
+
+ xswap_size_set = true;
+ return 0;
+bad:
+ pr_err("xswap: bad %s=%s\n", what, str);
+ return -EINVAL;
+}
+
+static int __init xswap_max_setup(char *str)
+{
+ xswap_parse_size(str, "xswap.max");
+ return 1;
+}
+__setup("xswap.max=", xswap_max_setup);
+
+/*
+ * The most a device can address, whatever xswap.max= asked for. An xswap
+ * slot is still a swap entry, so the swap offset limit applies even though
+ * the data never goes through a physical address.
+ */
+static unsigned long xswap_size_limit(void)
+{
+ unsigned long limit = swapfile_maximum_size;
+
+ /* A cluster index is an unsigned int. */
+ if (limit / SWAPFILE_CLUSTER > UINT_MAX)
+ limit = (unsigned long)UINT_MAX * SWAPFILE_CLUSTER;
+
+ return limit;
+}
+
+static unsigned long xswap_size_pages(void)
+{
+ unsigned long long pages;
+
+ if (xswap_size_max)
+ return xswap_size_limit();
+
+ if (xswap_size_bytes)
+ return xswap_size_bytes >> PAGE_SHIFT;
+
+ /* Let xswap_create() refuse a size that does not fit in the type. */
+ if (check_mul_overflow((unsigned long long)totalram_pages(),
+ (unsigned long long)xswap_size_pct, &pages))
+ return ULONG_MAX;
+
+ return div_u64(pages, 100);
+}
+
#else /* !CONFIG_XSWAP */
static inline int xswap_create_by_name(const char *name, int swap_flags)
{
@@ -4079,13 +4176,15 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
#ifdef CONFIG_XSWAP
/*
- * Create the file-less xswap device, sized at twice RAM. Called from
- * swapon(2) with SWAP_FLAG_XSWAP; there is at most one device per system.
+ * Create the file-less xswap device, sized by xswap.max= or the built-in
+ * default. Called from swapon(2) with SWAP_FLAG_XSWAP; there is at most
+ * one device per system.
*/
static int xswap_create(int prio)
{
struct swap_info_struct *si;
- unsigned long ram, maxpages;
+ unsigned long maxpages;
+ unsigned long limit;
unsigned int type;
int error;
@@ -4093,6 +4192,26 @@ static int xswap_create(int prio)
if (!zswap_is_enabled())
return -EOPNOTSUPP;
+ /*
+ * Refuse an explicitly requested size the device cannot address;
+ * clamp the default size, which nobody asked for.
+ */
+ limit = xswap_size_limit();
+ maxpages = xswap_size_pages();
+ if (xswap_size_set && maxpages > limit) {
+ pr_err("xswap: xswap.max is larger than the device can address\n");
+ return -EINVAL;
+ }
+ maxpages = min(maxpages, limit);
+ /*
+ * Below one cluster, si->max is not cluster-aligned and a slot in
+ * the only cluster would be past the end of the device.
+ */
+ if (maxpages < SWAPFILE_CLUSTER) {
+ pr_err("xswap: the configured size is smaller than one cluster\n");
+ return -EINVAL;
+ }
+
mutex_lock(&xswap_ctl_lock);
spin_lock(&swap_lock);
@@ -4116,16 +4235,8 @@ static int xswap_create(int prio)
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 * 2, swapfile_maximum_size);
- /* A cluster index is an unsigned int, so cap the cluster count. */
- if (maxpages / SWAPFILE_CLUSTER > UINT_MAX)
- maxpages = (unsigned long)UINT_MAX * SWAPFILE_CLUSTER;
/* Cluster-aligned, so no cluster holds a slot past si->max. */
- if (maxpages > SWAPFILE_CLUSTER)
- maxpages = rounddown(maxpages, SWAPFILE_CLUSTER);
- if (maxpages < 2)
- maxpages = 2;
+ maxpages = rounddown(maxpages, SWAPFILE_CLUSTER);
si->bdev = NULL;
si->swap_file = NULL;
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 09/14] mm, swap: let the command line set the xswap device size
2026-12-03 16:43 ` [PATCH v8 09/14] mm, swap: let the command line set the xswap device size Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm, swap: xswap.max=max exceeds kvmalloc INT_MAX limit in
zswap_swapon()
- [Low] mm, swap: xswap_parse_size() parses percentages with base 0
instead of 10
- [Low] mm, swap: sub-PAGE_SIZE xswap.max= falls back to 200% instead of
refusing
--
commit 5bc278987251bcc1f79b53f20a43d0a5be1540cc
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: let the command line set the xswap device size
This commit adds the xswap.max= kernel command-line parameter so users can
configure the size of a new xswap device as a byte size, a percentage of
RAM, or "max". The size is resolved in xswap_create(), refusing explicit
sizes that exceed the device limit or fall below one cluster.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index ba899e349e992..6ce5dbb3b1035 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -74,6 +75,102 @@ static int xswap_destroy_by_name(const char *name);
[ ... ]
> +static int __init xswap_parse_size(const char *str, const char *what)
> +{
> + const char *pct;
> + char buf[16];
> + unsigned int pct_val;
> + unsigned long bytes;
> + char *end;
> +
> + pct = strchr(str, '%');
> + if (!strcmp(str, "max")) {
> + xswap_size_max = true;
> + xswap_size_pct = 0;
> + xswap_size_bytes = 0;
> + } else if (pct) {
> + size_t len = pct - str;
> +
> + if (pct[1] || !len || len >= sizeof(buf))
> + goto bad;
> + memcpy(buf, str, len);
> + buf[len] = '\0';
> + if (kstrtouint(buf, 0, &pct_val) || !pct_val)
> + goto bad;
[Severity: Low]
Should kstrtouint() use base 10 here instead of 0? With base 0, a
leading zero triggers octal parsing, so xswap.max=050% is silently parsed
as 40%, while xswap.max=080% fails as invalid octal and falls back to the
200% default.
> + xswap_size_pct = pct_val;
> + xswap_size_bytes = 0;
> + xswap_size_max = false;
> + } else {
> + bytes = memparse(str, &end);
> + if (*end || bytes < PAGE_SIZE)
> + goto bad;
[Severity: Low]
Does rejecting bytes < PAGE_SIZE here cause sub-page sizes to fall back to
the 200% default instead of being refused as documented?
xswap_max_setup() discards the error from xswap_parse_size(), leaving
xswap_size_pct at 200 and xswap_size_set false. A parameter such as
xswap.max=32K is refused by xswap_create() on a 4 KiB page kernel, but on
a 64 KiB page kernel it logs a parse error and silently creates a
200%-of-RAM device on swapon(2).
[ ... ]
> +static unsigned long xswap_size_limit(void)
> +{
> + unsigned long limit = swapfile_maximum_size;
> +
> + /* A cluster index is an unsigned int. */
> + if (limit / SWAPFILE_CLUSTER > UINT_MAX)
> + limit = (unsigned long)UINT_MAX * SWAPFILE_CLUSTER;
> +
> + return limit;
> +}
> +
> +static unsigned long xswap_size_pages(void)
> +{
> + unsigned long long pages;
> +
> + if (xswap_size_max)
> + return xswap_size_limit();
[Severity: Medium]
Can xswap.max=max exceed kvmalloc's INT_MAX limit in zswap_swapon() and
trigger a WARN_ON_ONCE?
On 64-bit kernels with CONFIG_THP_SWAP=y (SWAPFILE_CLUSTER = 512 on 4 KiB
pages), xswap_size_limit() returns 0xffffffffUL * 512 (~8 PiB).
xswap_create() passes the full si->max to zswap_swapon():
mm/zswap.c:zswap_swapon() {
...
nr = DIV_ROUND_UP(nr_pages, ZSWAP_ADDRESS_SPACE_PAGES);
trees = kvzalloc_objs(*tree, nr);
...
}
With ZSWAP_ADDRESS_SPACE_PAGES = 16384 and sizeof(struct xarray) >= 16,
nr is 2^27, so kvzalloc_objs() requests at least INT_MAX + 1 bytes without
__GFP_NOWARN. __kvmalloc_node_noprof() hits WARN_ON_ONCE and returns
NULL, causing swapon(2) to fail with -ENOMEM (or panic if panic_on_warn).
Even for large sizes below INT_MAX, zswap_swapon() eagerly pins up to
1-2 GiB of unswappable memory and both zswap_swapon() and zswap_swapoff()
iterate over all trees without cond_resched().
In addition, setup_swap_clusters_info() reserves ~192-224+ GiB of vmalloc
space for UINT_MAX clusters, exceeding vmalloc space on 39-bit VA 64-bit
kernels (e.g., arm64 39-bit VA, riscv64 Sv39). Adding petabytes of swap
to total_swap_pages also distorts oom_badness() and vm_commit_limit().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=9
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 10/14] mm, swap: add CONFIG_XSWAP_DEFAULT_MAX
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (8 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 09/14] mm, swap: let the command line set the xswap device size Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-03 16:43 ` [PATCH v8 11/14] mm, swap: add xswap grow trigger on cluster allocation Baoquan He
` (3 subsequent siblings)
13 siblings, 0 replies; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
Add a build-time default for the size of a new xswap device. The value
can be a percentage of RAM, a size in bytes, or "max". "max" means the
largest size the device can address. xswap.max= on the command line
overrides this default.
The build-time default is clamped to the device limit. An explicit
xswap.max= that is too large is refused.
If the build-time default cannot be parsed, or is a byte size below one
cluster, use 200% instead. A percentage default can still be below one
cluster after RAM is known. In that case, device creation fails.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
.../admin-guide/kernel-parameters.txt | 6 ++--
mm/Kconfig | 11 ++++++
mm/swapfile.c | 34 ++++++++++++-------
3 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 2b0fc585e0c7..2a28aaa3cb32 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -8736,8 +8736,10 @@ Kernel parameters
off xmon is disabled.
xswap.max= [KNL,SWAP]
- Maximum size of a new xswap device. The value is a
+ Maximum size of a new xswap device, which is created
+ with swapon(2) and the spec "xswap". The value is a
size in bytes with an optional KMGTPE suffix, a
percentage of RAM, or "max" for the largest size
- the device can address.
+ the device can address. The default is set by
+ CONFIG_XSWAP_DEFAULT_MAX.
Format: <size>[KMGTPE] | <percent>% | max
diff --git a/mm/Kconfig b/mm/Kconfig
index d238a4971b1e..16958240a066 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -131,6 +131,17 @@ config XSWAP
array is backed by a sparse vmalloc area that is mapped lazily
in chunks, avoiding static pre-allocation overhead.
+config XSWAP_DEFAULT_MAX
+ string "Default maximum size of a new xswap device"
+ depends on XSWAP
+ default "200%"
+ help
+ The maximum size of a newly created xswap device. It accepts a
+ percentage of RAM (for example "200%"), a size (for example "2T"),
+ or "max" for the largest size the device can address.
+
+ xswap.max= on the kernel command line overrides this value.
+
config ZSMALLOC
tristate
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 6ce5dbb3b103..074c5114d129 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -77,9 +77,9 @@ static int xswap_destroy_by_name(const char *name);
static DEFINE_MUTEX(xswap_ctl_lock);
/*
- * Size of a new device: xswap.max= if given, otherwise the built-in
- * default. A percentage unless bytes is set; "max" asks for the largest
- * size the device can address.
+ * Size of a new device: xswap.max= if given, otherwise
+ * CONFIG_XSWAP_DEFAULT_MAX. A percentage unless bytes is set; "max" asks
+ * for the largest size the device can address.
*/
static unsigned int xswap_size_pct = 200;
static unsigned long xswap_size_bytes;
@@ -87,9 +87,12 @@ static bool xswap_size_max;
static bool xswap_size_set; /* xswap.max= was given */
/*
- * Parse "200%", "2T" or "max". @str is not modified.
+ * Parse "200%", "2T" or "max". @str is not modified. @cmdline tells an
+ * explicit xswap.max= from the build-time default, which is clamped
+ * instead of refused when it is too large.
*/
-static int __init xswap_parse_size(const char *str, const char *what)
+static int __init xswap_parse_size(const char *str, const char *what,
+ bool cmdline)
{
const char *pct;
char buf[16];
@@ -118,12 +121,16 @@ static int __init xswap_parse_size(const char *str, const char *what)
bytes = memparse(str, &end);
if (*end || bytes < PAGE_SIZE)
goto bad;
+ /* A default below one cluster would fail every create. */
+ if (!cmdline && bytes < (unsigned long)SWAPFILE_CLUSTER * PAGE_SIZE)
+ goto bad;
xswap_size_bytes = bytes;
xswap_size_pct = 0;
xswap_size_max = false;
}
- xswap_size_set = true;
+ if (cmdline)
+ xswap_size_set = true;
return 0;
bad:
pr_err("xswap: bad %s=%s\n", what, str);
@@ -132,7 +139,7 @@ static int __init xswap_parse_size(const char *str, const char *what)
static int __init xswap_max_setup(char *str)
{
- xswap_parse_size(str, "xswap.max");
+ xswap_parse_size(str, "xswap.max", true);
return 1;
}
__setup("xswap.max=", xswap_max_setup);
@@ -4175,11 +4182,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
}
#ifdef CONFIG_XSWAP
-/*
- * Create the file-less xswap device, sized by xswap.max= or the built-in
- * default. Called from swapon(2) with SWAP_FLAG_XSWAP; there is at most
- * one device per system.
- */
+/* Create the file-less xswap device from swapon(2). */
static int xswap_create(int prio)
{
struct swap_info_struct *si;
@@ -4694,6 +4697,13 @@ static int __init swapfile_init(void)
{
swapfile_maximum_size = arch_max_swapfile_size();
+#ifdef CONFIG_XSWAP
+ /* xswap.max= on the command line overrides the build-time default. */
+ if (!xswap_size_set)
+ xswap_parse_size(CONFIG_XSWAP_DEFAULT_MAX,
+ "CONFIG_XSWAP_DEFAULT_MAX", false);
+#endif
+
/*
* Once a cluster is freed, it's swap table content is read
* only, and all swap cache readers (swap_cache_*) verifies
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* [PATCH v8 11/14] mm, swap: add xswap grow trigger on cluster allocation
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (9 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 10/14] mm, swap: add CONFIG_XSWAP_DEFAULT_MAX Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 12/14] mm, swap: add debugfs counters for xswap Baoquan He
` (2 subsequent siblings)
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
Grow the mapped xswap range before it runs out. When usage reaches 85%
of the mapped range, map more clusters from cluster_alloc_swap_entry().
Grow toward 65% usage, but by a bounded step. The target leaves free
space in the mapped range before the next grow at 85%, so one grow is
not immediately followed by another. The bounded step keeps one grow
from mapping an unbounded number of pages while it sleeps, so on a
large device the in-use fraction stays closer to the 85% trigger than
to the 65% target.
A burst of concurrent allocators can take every mapped cluster off the
free list while usage is still below the threshold. When the allocator
cannot find a cluster, it forces one grow chunk and retries, instead of
failing while unmapped clusters remain.
Mapping pages can sleep. Grow before the allocation that needs them,
not after it fails.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/swap.h | 1 +
mm/swapfile.c | 142 ++++++++++++++++++++++++++++++++++++++++---
2 files changed, 136 insertions(+), 7 deletions(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index ba1bf7e09893..55649ad5b145 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -246,6 +246,7 @@ struct swap_info_struct {
unsigned long nr_clusters_max;/* total clusters in the xswap address space */
unsigned long nr_clusters_mapped; /* currently mapped cluster count */
struct mutex xswap_lock; /* serialize map/unmap operations */
+ atomic_long_t xswap_grows; /* grow operations so far */
#endif
struct list_head free_clusters; /* free clusters list */
struct list_head full_clusters; /* full clusters list */
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 074c5114d129..4d6a46346b93 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -68,6 +68,23 @@
#define XSWAP_GROW_CLUSTERS \
max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16)
+/*
+ * Grow thresholds, as a percentage of the mapped range in use. The target
+ * sits below the next grow trigger, so a grow that follows a rise in usage
+ * does not immediately trigger another one. It is a goal, not a bound: a
+ * grow maps at most XSWAP_GROW_CHUNKS chunks, so on a large device the
+ * in-use fraction stays closer to the trigger than to the target.
+ */
+#define XSWAP_GROW_UNTIL 65
+#define XSWAP_GROW_WHEN 85
+#define XSWAP_GROW_CHUNKS 4
+
+static bool xswap_should_grow(struct swap_info_struct *si);
+static unsigned long xswap_grow(struct swap_info_struct *si, bool force);
+static int xswap_map_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr,
+ bool link_free);
+
static int xswap_create(int prio);
static int xswap_create_by_name(const char *name, int swap_flags);
static int xswap_destroy(void);
@@ -1317,6 +1334,9 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
struct swap_cluster_info *ci;
unsigned int order = likely(folio) ? folio_order(folio) : 0;
unsigned long offset = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
+#ifdef CONFIG_XSWAP
+ bool retried = false;
+#endif
/*
* Swapfile is not block device so unable
@@ -1325,6 +1345,13 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
if (order && !(si->flags & SWP_BLKDEV))
return 0;
+#ifdef CONFIG_XSWAP
+ /* Grow before allocating: mapping pages can sleep. */
+ if ((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK) &&
+ xswap_should_grow(si))
+ xswap_grow(si, false);
+#endif
+
if (!(si->flags & SWP_SOLIDSTATE)) {
/* Serialize HDD SWAP allocation for each device. */
spin_lock(&si->global_cluster_lock);
@@ -1397,6 +1424,22 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
if (found)
goto done;
}
+
+#ifdef CONFIG_XSWAP
+ /*
+ * A burst of allocators can hold every mapped cluster while usage is
+ * still below the growth threshold. Grow and rescan all lists: a
+ * cluster published by another grower may already be on a frag list.
+ */
+ if (!found && (si->flags & SWP_XSWAP) &&
+ (READ_ONCE(si->flags) & SWP_WRITEOK) && !retried) {
+ retried = true;
+ if (READ_ONCE(si->nr_clusters_mapped) <
+ READ_ONCE(si->nr_clusters_max))
+ xswap_grow(si, true);
+ goto new_cluster;
+ }
+#endif
done:
if (!(si->flags & SWP_SOLIDSTATE))
spin_unlock(&si->global_cluster_lock);
@@ -3986,8 +4029,39 @@ static unsigned long read_swap_header(struct swap_info_struct *si,
}
#ifdef CONFIG_XSWAP
+/*
+ * Link the new clusters and publish the larger range in one si->lock
+ * section: linking first makes a cluster visible to an allocator only
+ * after it is on the free list. @link_free is false for the first
+ * chunk, whose free list is not initialized yet.
+ */
+static void xswap_publish_clusters(struct swap_info_struct *si,
+ unsigned long start_idx, unsigned long nr,
+ bool link_free)
+{
+ unsigned long i;
+
+ spin_lock(&si->lock);
+ if (link_free) {
+ for (i = start_idx; i < start_idx + nr; i++) {
+ struct swap_cluster_info *ci = &si->cluster_info[i];
+
+ /* A concurrent grower cannot take these: xswap_lock. */
+ if (ci->flags != CLUSTER_FLAG_NONE)
+ continue;
+ list_add_tail(&ci->list, &si->free_clusters);
+ WRITE_ONCE(ci->flags, CLUSTER_FLAG_FREE);
+ ci->order = 0;
+ }
+ }
+ /* Pairs with the smp_load_acquire() in the readers of the range. */
+ smp_store_release(&si->nr_clusters_mapped, start_idx + nr);
+ spin_unlock(&si->lock);
+}
+
static int xswap_map_clusters(struct swap_info_struct *si,
- unsigned long start_idx, unsigned long nr)
+ unsigned long start_idx, unsigned long nr,
+ bool link_free)
{
/* vm_area_grow_pages() takes an offset into the area, not an address. */
unsigned long vm_end = PAGE_ALIGN((size_t)(start_idx + nr) *
@@ -4023,14 +4097,65 @@ static int xswap_map_clusters(struct swap_info_struct *si,
for (i = start_idx; i < start_idx + nr; i++)
spin_lock_init(&si->cluster_info[i].lock);
- /*
- * Publish the new mappings and cluster lock initialization before
- * the count; walkers without xswap_lock use smp_load_acquire().
- */
- smp_store_release(&si->nr_clusters_mapped, start_idx + nr);
+ /* The mappings and the locks are in place; make them visible. */
+ xswap_publish_clusters(si, start_idx, nr, link_free);
mutex_unlock(&si->xswap_lock);
return 0;
}
+static bool xswap_should_grow(struct swap_info_struct *si)
+{
+ unsigned long mapped = READ_ONCE(si->nr_clusters_mapped);
+
+ if (mapped >= READ_ONCE(si->nr_clusters_max))
+ return false;
+
+ return swap_usage_in_pages(si) * 100 >=
+ mapped * SWAPFILE_CLUSTER * XSWAP_GROW_WHEN;
+}
+
+/*
+ * Map more of the cluster_info array, up to GROW_UNTIL in use. The caller
+ * holds percpu_swap_cluster.lock, which this drops while it sleeps.
+ */
+static unsigned long xswap_grow(struct swap_info_struct *si, bool force)
+{
+ unsigned long mapped = READ_ONCE(si->nr_clusters_mapped);
+ unsigned long nr_new, want;
+ int ret;
+
+ lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock);
+
+ want = DIV_ROUND_UP(swap_usage_in_pages(si) * 100,
+ XSWAP_GROW_UNTIL * SWAPFILE_CLUSTER);
+ if (want <= mapped) {
+ if (!force)
+ return 0;
+ nr_new = XSWAP_GROW_CLUSTERS;
+ } else {
+ nr_new = rounddown(want - mapped, XSWAP_GROW_CLUSTERS);
+ }
+ if (!nr_new)
+ nr_new = XSWAP_GROW_CLUSTERS;
+ if (nr_new > XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS)
+ nr_new = XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS;
+ nr_new = min(nr_new, READ_ONCE(si->nr_clusters_max) - mapped);
+ if (!nr_new)
+ return 0;
+
+ /*
+ * Mapping pages can sleep. The lock only guards the per-cpu
+ * cluster cache, which this path does not touch.
+ */
+ local_unlock(&percpu_swap_cluster.lock);
+ ret = xswap_map_clusters(si, mapped, nr_new, true);
+ local_lock(&percpu_swap_cluster.lock);
+ if (ret)
+ return 0;
+
+ atomic_long_inc(&si->xswap_grows);
+ return nr_new;
+}
+
#endif /* CONFIG_XSWAP */
static int setup_swap_clusters_info(struct swap_info_struct *si,
@@ -4060,7 +4185,8 @@ static int setup_swap_clusters_info(struct swap_info_struct *si,
mutex_init(&si->xswap_lock);
err = xswap_map_clusters(si, 0, min_t(unsigned long,
- XSWAP_GROW_CLUSTERS, nr_clusters));
+ XSWAP_GROW_CLUSTERS, nr_clusters),
+ false);
if (err)
goto err_free_vm;
@@ -4252,6 +4378,8 @@ static int xswap_create(int prio)
*/
si->ops = &swap_bdev_ops;
+ atomic_long_set(&si->xswap_grows, 0);
+
error = setup_swap_clusters_info(si, NULL, maxpages);
if (error)
goto bad_swap;
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 11/14] mm, swap: add xswap grow trigger on cluster allocation
2026-12-03 16:43 ` [PATCH v8 11/14] mm, swap: add xswap grow trigger on cluster allocation Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] mm: xswap_grow() requests multiple chunks when force is true
- [Medium] mm: xswap forced grow fails prematurely on concurrent bursts
- [Medium] mm: proactive xswap_grow() stalls concurrent allocators on
xswap_lock
- [Medium] mm: cluster_alloc_swap_entry() rescans lists even when xswap
did not grow
- [Medium] mm: cluster_alloc_swap_entry() grows xswap when free_clusters
is non-empty
--
commit b0a141260a703566349e90f3c0ef3c0714bd6db0
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: add xswap grow trigger on cluster allocation
This patch adds proactive and fallback growth of the mapped xswap
cluster_info range in cluster_alloc_swap_entry(), growing toward 65%
usage in bounded chunks once mapped usage reaches 85% and retrying after
a forced grow when no cluster is found.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 074c5114d129a..4d6a46346b935 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -1325,6 +1345,13 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
> if (order && !(si->flags & SWP_BLKDEV))
> return 0;
>
> +#ifdef CONFIG_XSWAP
> + /* Grow before allocating: mapping pages can sleep. */
> + if ((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK) &&
> + xswap_should_grow(si))
> + xswap_grow(si, false);
> +#endif
[Severity: Medium]
Does this stall concurrent allocators on si->xswap_lock once usage
reaches 85%?
Since si->nr_clusters_mapped is only updated at the end of
xswap_publish_clusters(), other CPUs entering cluster_alloc_swap_entry()
while a grow sleeps in vm_area_grow_pages() also call xswap_grow() and
block on mutex_lock(&si->xswap_lock) in xswap_map_clusters().
Those waiters then wake up serially just to fail the stale start_idx
check with -EAGAIN, instead of allocating from the remaining 15% free
clusters. Using mutex_trylock(&si->xswap_lock) for non-forced grows
would let them proceed while one grow is already in flight.
[ ... ]
> @@ -1397,6 +1424,22 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
> if (found)
> goto done;
> }
> +
> +#ifdef CONFIG_XSWAP
> + /*
> + * A burst of allocators can hold every mapped cluster while usage is
> + * still below the growth threshold. Grow and rescan all lists: a
> + * cluster published by another grower may already be on a frag list.
> + */
> + if (!found && (si->flags & SWP_XSWAP) &&
> + (READ_ONCE(si->flags) & SWP_WRITEOK) && !retried) {
> + retried = true;
> + if (READ_ONCE(si->nr_clusters_mapped) <
> + READ_ONCE(si->nr_clusters_max))
> + xswap_grow(si, true);
> + goto new_cluster;
> + }
> +#endif
[Severity: Medium]
Can a concurrent burst still fail allocation here while unmapped clusters
remain below si->nr_clusters_max?
Because xswap_grow() collapses -EAGAIN from xswap_map_clusters() to 0
and retried caps the fallback to a single attempt, excess threads in a
burst (more than XSWAP_GROW_CLUSTERS) or a thread racing with an
in-flight grow can lose the newly published clusters and return 0:
CPU0 CPU1
cluster_alloc_swap_entry() cluster_alloc_swap_entry()
!found, retried = true !found, retried = true
xswap_grow(si, true) xswap_grow(si, true)
mapped = N mapped = N
xswap_map_clusters(N, ...)
mutex_lock(&si->xswap_lock) mutex_lock(&si->xswap_lock)
publish N + chunk
mutex_unlock(&si->xswap_lock)
start_idx != nr_clusters_mapped
return -EAGAIN (returned as 0)
goto new_cluster
isolate new clusters
goto new_cluster
lists empty, retried == true
return 0 <-- premature -ENOMEM
Once another thread isolates the new clusters (which stay off-list while
swap_cluster_populate() can sleep), CPU1 cannot retry a forced grow and
folio_alloc_swap() fails with -ENOMEM despite unmapped clusters
remaining.
[Severity: Medium]
Should goto new_cluster be skipped when si->nr_clusters_mapped did not
advance?
When nr_clusters_mapped has already reached nr_clusters_max, or when
xswap_grow(si, true) fails with -ENOMEM, this still jumps back to
new_cluster and repeats the entire scan across all cluster lists and
swap_reclaim_full_clusters() on every failed allocation.
Snapshotting nr_clusters_mapped before new_cluster and retrying only if
xswap_grow() succeeded or nr_clusters_mapped increased would avoid the
redundant pass while still catching clusters published by another grower.
[Severity: Medium]
Can this grow xswap even when si->free_clusters is not empty?
If swap_cluster_populate() fails (for example with -ENOMEM in
swap_cluster_alloc_table() or -EAGAIN), it returns the cluster to
si->free_clusters and returns NULL, which makes alloc_swap_scan_list()
break out and return 0 while free clusters remain on the list.
When nonfull and frag lists have no slots, cluster_alloc_swap_entry()
then calls xswap_grow(si, true) without checking list_empty() on
si->free_clusters, mapping additional permanent vmalloc pages for
cluster_info even though free clusters are already available.
[ ... ]
> +static unsigned long xswap_grow(struct swap_info_struct *si, bool force)
> +{
> + unsigned long mapped = READ_ONCE(si->nr_clusters_mapped);
> + unsigned long nr_new, want;
> + int ret;
> +
> + lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock);
> +
> + want = DIV_ROUND_UP(swap_usage_in_pages(si) * 100,
> + XSWAP_GROW_UNTIL * SWAPFILE_CLUSTER);
> + if (want <= mapped) {
> + if (!force)
> + return 0;
> + nr_new = XSWAP_GROW_CLUSTERS;
> + } else {
> + nr_new = rounddown(want - mapped, XSWAP_GROW_CLUSTERS);
> + }
> + if (!nr_new)
> + nr_new = XSWAP_GROW_CLUSTERS;
> + if (nr_new > XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS)
> + nr_new = XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS;
[Severity: Medium]
Should xswap_grow() cap nr_new to a single XSWAP_GROW_CLUSTERS chunk when
force is true?
Here force is only checked in the want <= mapped branch. Whenever usage
exceeds 65% (XSWAP_GROW_UNTIL), want > mapped holds and a forced grow
takes the else branch, requesting up to XSWAP_GROW_CHUNKS (4) chunks
instead of the single chunk described in the commit message.
Because vm_area_grow_pages() is all-or-nothing under
memalloc_noreclaim_save() and __GFP_NOMEMALLOC, if the proactive 4-chunk
grow fails with -ENOMEM under memory pressure, the fallback
xswap_grow(si, true) repeats the exact same 4-chunk request and starves
swap allocation even when a single page is available.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=11
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 12/14] mm, swap: add debugfs counters for xswap
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (10 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 11/14] mm, swap: add xswap grow trigger on cluster allocation Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Baoquan He
2026-12-03 16:43 ` [PATCH v8 14/14] mm, swap: skip the overcommit probe when removing xswap Baoquan He
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
The mapped range and the grow count are not visible from userspace.
This makes it hard to check if the grow thresholds work: we cannot see
how large the mapped range is, how much it grows, or how much free tail
it keeps.
Put this information in debugfs. It is a debugging aid, not a stable
interface, so it does not belong in sysfs or the ABI:
# cat /sys/kernel/debug/xswap/xswap.stats
type 0
clusters_max 2038783
clusters_mapped 255
usage_pages 91456
tail_free 16
grows 3
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
include/linux/swap.h | 1 +
mm/swapfile.c | 69 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 55649ad5b145..7979b56e5224 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -246,6 +246,7 @@ struct swap_info_struct {
unsigned long nr_clusters_max;/* total clusters in the xswap address space */
unsigned long nr_clusters_mapped; /* currently mapped cluster count */
struct mutex xswap_lock; /* serialize map/unmap operations */
+ struct dentry *xswap_debugfs; /* mapped range and counters */
atomic_long_t xswap_grows; /* grow operations so far */
#endif
struct list_head free_clusters; /* free clusters list */
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 4d6a46346b93..b2d24e1a24f0 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -43,12 +43,14 @@
#include <linux/suspend.h>
#include <linux/zswap.h>
#include <linux/plist.h>
+#include <linux/debugfs.h>
#include <asm/tlbflush.h>
#include <linux/leafops.h>
#include "swap_table.h"
#include "internal.h"
#include "swap.h"
+
#define DEF_SWAP_PRIO -1
/*
@@ -81,6 +83,7 @@
static bool xswap_should_grow(struct swap_info_struct *si);
static unsigned long xswap_grow(struct swap_info_struct *si, bool force);
+static void xswap_debugfs_del(struct swap_info_struct *si);
static int xswap_map_clusters(struct swap_info_struct *si,
unsigned long start_idx, unsigned long nr,
bool link_free);
@@ -3470,6 +3473,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si)
}
spin_unlock(&ci->lock);
}
+ xswap_debugfs_del(si);
vfree(si->cluster_info);
si->cluster_vm = NULL;
si->cluster_info = NULL;
@@ -4102,6 +4106,7 @@ static int xswap_map_clusters(struct swap_info_struct *si,
mutex_unlock(&si->xswap_lock);
return 0;
}
+
static bool xswap_should_grow(struct swap_info_struct *si)
{
unsigned long mapped = READ_ONCE(si->nr_clusters_mapped);
@@ -4156,6 +4161,68 @@ static unsigned long xswap_grow(struct swap_info_struct *si, bool force)
return nr_new;
}
+static ssize_t xswap_debugfs_read(struct file *file, char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ struct swap_info_struct *si = file->private_data;
+ struct swap_cluster_info *ci;
+ unsigned long mapped, tail = 0;
+ char tmp[192];
+ int len;
+
+ /*
+ * Count the free clusters at the end of the mapped range. The
+ * xswap_lock keeps the range from moving under the scan.
+ */
+ mutex_lock(&si->xswap_lock);
+ mapped = READ_ONCE(si->nr_clusters_mapped);
+ if (si->cluster_info) {
+ while (mapped - tail > 1) {
+ ci = &si->cluster_info[mapped - tail - 1];
+ if (READ_ONCE(ci->count) ||
+ READ_ONCE(ci->flags) != CLUSTER_FLAG_FREE)
+ break;
+ tail++;
+ }
+ }
+ len = scnprintf(tmp, sizeof(tmp),
+ "type %d\nclusters_max %lu\nclusters_mapped %lu\nusage_pages %lu\ntail_free %lu\ngrows %lu\n",
+ si->type, READ_ONCE(si->nr_clusters_max), mapped,
+ swap_usage_in_pages(si), tail,
+ atomic_long_read(&si->xswap_grows));
+ mutex_unlock(&si->xswap_lock);
+
+ return simple_read_from_buffer(buf, count, ppos, tmp, len);
+}
+
+static const struct file_operations xswap_debugfs_fops = {
+ .read = xswap_debugfs_read,
+ .open = simple_open,
+ .llseek = default_llseek,
+};
+
+static struct dentry *xswap_debugfs_root;
+
+static void xswap_debugfs_add(struct swap_info_struct *si)
+{
+ if (!xswap_debugfs_root)
+ xswap_debugfs_root = debugfs_create_dir("xswap", NULL);
+ if (IS_ERR_OR_NULL(xswap_debugfs_root))
+ return;
+
+ /* One device, so one file: /sys/kernel/debug/xswap/xswap.stats. */
+ si->xswap_debugfs = debugfs_create_file("xswap.stats", 0444,
+ xswap_debugfs_root, si,
+ &xswap_debugfs_fops);
+ if (IS_ERR(si->xswap_debugfs))
+ si->xswap_debugfs = NULL;
+}
+
+static void xswap_debugfs_del(struct swap_info_struct *si)
+{
+ debugfs_remove(si->xswap_debugfs);
+ si->xswap_debugfs = NULL;
+}
#endif /* CONFIG_XSWAP */
static int setup_swap_clusters_info(struct swap_info_struct *si,
@@ -4378,6 +4445,7 @@ static int xswap_create(int prio)
*/
si->ops = &swap_bdev_ops;
+ si->xswap_debugfs = NULL;
atomic_long_set(&si->xswap_grows, 0);
error = setup_swap_clusters_info(si, NULL, maxpages);
@@ -4389,6 +4457,7 @@ static int xswap_create(int prio)
goto bad_swap;
mutex_lock(&swapon_mutex);
+ xswap_debugfs_add(si);
si->prio = prio;
si->list.prio = -si->prio;
si->avail_list.prio = -si->prio;
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 12/14] mm, swap: add debugfs counters for xswap
2026-12-03 16:43 ` [PATCH v8 12/14] mm, swap: add debugfs counters for xswap Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: xswap_debugfs_read() corrupts output on multi-read with
live stats
- [Medium] mm: xswap_debugfs_read() scans under xswap_lock without
cond_resched()
- [Low] mm: xswap_debugfs_add() permanently caches ERR_PTR in
xswap_debugfs_root
--
commit 661117d24147f38fb85952116650fbeb71a65516
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: add debugfs counters for xswap
This commit exposes xswap mapped cluster counts, free tail length, usage,
and grow statistics through /sys/kernel/debug/xswap/xswap.stats.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 4d6a46346b935..b2d24e1a24f08 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -4156,6 +4161,68 @@ static unsigned long xswap_grow(struct swap_info_struct *si, bool force)
> return nr_new;
> }
>
> +static ssize_t xswap_debugfs_read(struct file *file, char __user *buf,
> + size_t count, loff_t *ppos)
> +{
> + struct swap_info_struct *si = file->private_data;
> + struct swap_cluster_info *ci;
> + unsigned long mapped, tail = 0;
> + char tmp[192];
> + int len;
> +
> + /*
> + * Count the free clusters at the end of the mapped range. The
> + * xswap_lock keeps the range from moving under the scan.
> + */
> + mutex_lock(&si->xswap_lock);
> + mapped = READ_ONCE(si->nr_clusters_mapped);
> + if (si->cluster_info) {
> + while (mapped - tail > 1) {
> + ci = &si->cluster_info[mapped - tail - 1];
> + if (READ_ONCE(ci->count) ||
> + READ_ONCE(ci->flags) != CLUSTER_FLAG_FREE)
> + break;
> + tail++;
> + }
> + }
[Severity: Medium]
Could this loop cause scheduling stalls on large xswap devices without a
cond_resched()?
Once nr_clusters_mapped has grown large and trailing clusters are freed,
reading xswap.stats walks millions of vmalloc-backed swap_cluster_info
entries in a tight loop. On CONFIG_PREEMPT_NONE or
CONFIG_PREEMPT_VOLUNTARY kernels this causes non-preemptible stalls.
Holding si->xswap_lock across the scan also blocks swap-out growth in
cluster_alloc_swap_entry() -> xswap_grow() -> xswap_map_clusters().
Calling cond_resched() periodically inside the loop would avoid the
scheduling stall.
> + len = scnprintf(tmp, sizeof(tmp),
> + "type %d\nclusters_max %lu\nclusters_mapped %lu\nusage_pages %lu\ntail_free %lu\ngrows %lu\n",
> + si->type, READ_ONCE(si->nr_clusters_max), mapped,
> + swap_usage_in_pages(si), tail,
> + atomic_long_read(&si->xswap_grows));
> + mutex_unlock(&si->xswap_lock);
> +
> + return simple_read_from_buffer(buf, count, ppos, tmp, len);
> +}
[Severity: Medium]
Can reformatting live counters on every read() corrupt output on the
follow-up EOF read?
Standard readers such as cat issue a second read() at *ppos == len to
check for EOF. If any counter grows in decimal width between the two
calls, len increases, so simple_read_from_buffer() copies trailing bytes
from tmp instead of returning 0 and appends garbled output.
This also re-acquires si->xswap_lock and rescans the tail on every EOF
check. Using DEFINE_SHOW_ATTRIBUTE() would snapshot the stats once per
open and avoid both issues.
[ ... ]
> +static void xswap_debugfs_add(struct swap_info_struct *si)
> +{
> + if (!xswap_debugfs_root)
> + xswap_debugfs_root = debugfs_create_dir("xswap", NULL);
> + if (IS_ERR_OR_NULL(xswap_debugfs_root))
> + return;
[Severity: Low]
Does this permanently cache an ERR_PTR in xswap_debugfs_root if
debugfs_create_dir() fails?
debugfs_create_dir() returns a non-NULL ERR_PTR on failure such as
-ENOMEM. On later swapoff/swapon cycles, !xswap_debugfs_root is false
and IS_ERR_OR_NULL() returns early, leaving xswap.stats unavailable
until reboot.
Resetting xswap_debugfs_root to NULL on error, as done for
si->xswap_debugfs below, would allow subsequent xswap_create() calls to
retry.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=12
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (11 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 12/14] mm, swap: add debugfs counters for xswap Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 14/14] mm, swap: skip the overcommit probe when removing xswap Baoquan He
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
At this point, an xswap slot holds only a zswap-backed page. It has no
storage of its own. If the folio's cgroup cannot take another zswap
page, giving it an xswap slot is wasted: zswap refuses the page and the
slot cannot hold it.
Skip xswap devices when the cgroup cannot zswap, and try a physical swap
device instead.
The check is obj_cgroup_may_zswap(): zswap_store() uses the same check
but first tries to shrink the memcg, while the allocation-time check does
not, so it is stricter.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/swapfile.c | 150 ++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 127 insertions(+), 23 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index b2d24e1a24f0..6c1cfa0d693b 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -228,6 +228,8 @@ static DEFINE_SPINLOCK(swap_lock);
static unsigned int nr_swapfiles;
atomic_long_t nr_swap_pages;
atomic_t nr_real_swapfiles;
+/* Active xswap devices, as in nr_real_swapfiles. */
+static atomic_t nr_xswap_files;
/*
* 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
@@ -1199,7 +1201,7 @@ static bool swap_prefer_free_cluster(struct swap_info_struct *si)
static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si,
struct swap_cluster_info *ci,
struct folio *folio,
- unsigned long offset)
+ unsigned long offset, bool no_cache)
{
unsigned long next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID;
unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
@@ -1240,6 +1242,10 @@ static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si,
relocate_cluster(si, ci);
swap_cluster_unlock(ci);
if (si->flags & SWP_SOLIDSTATE) {
+ if (no_cache &&
+ (this_cpu_read(percpu_swap_cluster.si[order]) != si ||
+ !this_cpu_read(percpu_swap_cluster.offset[order])))
+ return found;
this_cpu_write(percpu_swap_cluster.offset[order], next);
this_cpu_write(percpu_swap_cluster.si[order], si);
} else {
@@ -1251,7 +1257,7 @@ static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si,
static unsigned long alloc_swap_scan_list(struct swap_info_struct *si,
struct list_head *list,
struct folio *folio,
- bool scan_all)
+ bool scan_all, bool no_cache)
{
unsigned long found = SWAP_ENTRY_INVALID;
@@ -1262,7 +1268,8 @@ static unsigned long alloc_swap_scan_list(struct swap_info_struct *si,
if (!ci)
break;
offset = cluster_offset(si, ci);
- found = alloc_swap_scan_cluster(si, ci, folio, offset);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset,
+ no_cache);
if (found)
break;
} while (scan_all);
@@ -1332,7 +1339,8 @@ static void swap_reclaim_work(struct work_struct *work)
* cluster for current CPU too.
*/
static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
- struct folio *folio)
+ struct folio *folio,
+ bool no_cache)
{
struct swap_cluster_info *ci;
unsigned int order = likely(folio) ? folio_order(folio) : 0;
@@ -1367,7 +1375,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
if (cluster_is_usable(ci, order)) {
if (cluster_is_empty(ci))
offset = cluster_offset(si, ci);
- found = alloc_swap_scan_cluster(si, ci, folio, offset);
+ found = alloc_swap_scan_cluster(si, ci, folio, offset,
+ no_cache);
} else {
swap_cluster_unlock(ci);
}
@@ -1376,20 +1385,33 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
}
new_cluster:
+ /*
+ * A no_cache allocation cannot keep a per-CPU cluster, so pack it
+ * into a cluster already in use and leave the free ones to
+ * allocations that can use a whole one.
+ */
+ if (no_cache && order < PMD_ORDER) {
+ found = alloc_swap_scan_list(si, &si->frag_clusters[order],
+ folio, false, no_cache);
+ if (found)
+ goto done;
+ }
+
if (swap_prefer_free_cluster(si)) {
- found = alloc_swap_scan_list(si, &si->free_clusters, folio, false);
+ found = alloc_swap_scan_list(si, &si->free_clusters, folio, false, no_cache);
if (found)
goto done;
}
if (order < PMD_ORDER) {
- found = alloc_swap_scan_list(si, &si->nonfull_clusters[order], folio, true);
+ found = alloc_swap_scan_list(si, &si->nonfull_clusters[order],
+ folio, true, no_cache);
if (found)
goto done;
}
if (!swap_prefer_free_cluster(si)) {
- found = alloc_swap_scan_list(si, &si->free_clusters, folio, false);
+ found = alloc_swap_scan_list(si, &si->free_clusters, folio, false, no_cache);
if (found)
goto done;
}
@@ -1405,7 +1427,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
* failure is not critical. Scanning one cluster still
* keeps the list rotated and reclaimed (for clean swap cache).
*/
- found = alloc_swap_scan_list(si, &si->frag_clusters[order], folio, false);
+ found = alloc_swap_scan_list(si, &si->frag_clusters[order], folio, false, no_cache);
if (found)
goto done;
}
@@ -1419,11 +1441,11 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
* Clusters here have at least one usable slots and can't fail order 0
* allocation, but reclaim may drop si->lock and race with another user.
*/
- found = alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true);
+ found = alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true, no_cache);
if (found)
goto done;
- found = alloc_swap_scan_list(si, &si->nonfull_clusters[o], folio, true);
+ found = alloc_swap_scan_list(si, &si->nonfull_clusters[o], folio, true, no_cache);
if (found)
goto done;
}
@@ -1467,7 +1489,9 @@ static void del_from_avail_list(struct swap_info_struct *si, bool swapoff)
lockdep_assert_held(&si->lock);
si->flags &= ~SWP_WRITEOK;
/* Count active devices, not merely those on the avail list. */
- if (!(si->flags & SWP_XSWAP))
+ if (si->flags & SWP_XSWAP)
+ atomic_sub(1, &nr_xswap_files);
+ else
atomic_sub(1, &nr_real_swapfiles);
atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages);
} else {
@@ -1503,7 +1527,9 @@ static void add_to_avail_list(struct swap_info_struct *si, bool swapon)
lockdep_assert_held(&si->lock);
si->flags |= SWP_WRITEOK;
/* A full device is still active and skips the add below. */
- if (!(si->flags & SWP_XSWAP))
+ if (si->flags & SWP_XSWAP)
+ atomic_add(1, &nr_xswap_files);
+ else
atomic_add(1, &nr_real_swapfiles);
} else {
if (!(READ_ONCE(si->flags) & SWP_WRITEOK))
@@ -1627,7 +1653,7 @@ static bool get_swap_device_info(struct swap_info_struct *si)
* Fast path try to get swap entries with specified order from current
* CPU's swap entry pool (a cluster).
*/
-static bool swap_alloc_fast(struct folio *folio)
+static bool swap_alloc_fast(struct folio *folio, bool may_zswap)
{
unsigned int order = folio_order(folio);
struct swap_cluster_info *ci;
@@ -1643,11 +1669,16 @@ static bool swap_alloc_fast(struct folio *folio)
if (!si || !offset || !get_swap_device_info(si))
return false;
+ if (!may_zswap && (si->flags & SWP_XSWAP)) {
+ put_swap_device(si);
+ return false;
+ }
+
ci = swap_cluster_lock(si, offset);
if (cluster_is_usable(ci, order)) {
if (cluster_is_empty(ci))
offset = cluster_offset(si, ci);
- alloc_swap_scan_cluster(si, ci, folio, offset);
+ alloc_swap_scan_cluster(si, ci, folio, offset, false);
} else {
swap_cluster_unlock(ci);
}
@@ -1657,18 +1688,24 @@ static bool swap_alloc_fast(struct folio *folio)
}
/* Rotate the device and switch to a new cluster */
-static void swap_alloc_slow(struct folio *folio)
+static void swap_alloc_slow(struct folio *folio, bool may_zswap)
{
struct swap_info_struct *si, *next;
spin_lock(&swap_avail_lock);
start_over:
plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
+ /*
+ * Do not rotate a skipped device: the walk follows the
+ * rotation and would spin forever.
+ */
+ if (!may_zswap && (si->flags & SWP_XSWAP))
+ continue;
/* Rotate the device and switch to a new cluster */
plist_requeue(&si->avail_list, &swap_avail_head);
spin_unlock(&swap_avail_lock);
if (get_swap_device_info(si)) {
- cluster_alloc_swap_entry(si, folio);
+ cluster_alloc_swap_entry(si, folio, !may_zswap);
put_swap_device(si);
if (folio_test_swapcache(folio))
return;
@@ -1986,6 +2023,34 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,
return err;
}
+/* Can the folio's cgroup take another zswap page? */
+static bool folio_may_zswap(struct folio *folio)
+{
+ struct obj_cgroup *objcg = get_obj_cgroup_from_folio(folio);
+ bool may_zswap = !objcg || obj_cgroup_may_zswap(objcg);
+
+ if (objcg)
+ obj_cgroup_put(objcg);
+ return may_zswap;
+}
+
+/* Can a non-xswap device still take a slot? */
+static bool real_swap_has_room(void)
+{
+ struct swap_info_struct *si;
+ bool room = false;
+
+ spin_lock(&swap_avail_lock);
+ plist_for_each_entry(si, &swap_avail_head, avail_list) {
+ if (!(si->flags & SWP_XSWAP)) {
+ room = true;
+ break;
+ }
+ }
+ spin_unlock(&swap_avail_lock);
+ return room;
+}
+
/**
* folio_alloc_swap - allocate swap space for a folio
* @folio: folio we want to move to swap
@@ -2002,10 +2067,21 @@ int folio_alloc_swap(struct folio *folio)
{
unsigned int order = folio_order(folio);
unsigned int size = 1 << order;
+ bool may_zswap = true;
+ bool zswap_checked = false;
VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);
+ /*
+ * A folio zswap will not take must not get an xswap slot; without
+ * a device to route away from, skip the lookup.
+ */
+ if (atomic_read(&nr_xswap_files)) {
+ may_zswap = folio_may_zswap(folio);
+ zswap_checked = true;
+ }
+
if (order) {
/*
* Reject large allocation when THP_SWAP is disabled. Check below
@@ -2025,16 +2101,36 @@ int folio_alloc_swap(struct folio *folio)
}
again:
- local_lock(&percpu_swap_cluster.lock);
- if (!swap_alloc_fast(folio))
- swap_alloc_slow(folio);
- local_unlock(&percpu_swap_cluster.lock);
+ /*
+ * A folio that cannot zswap can only use a real device; skip the
+ * scan when none has room. The failure is still reported below.
+ */
+ if (may_zswap || real_swap_has_room()) {
+ local_lock(&percpu_swap_cluster.lock);
+ if (!swap_alloc_fast(folio, may_zswap))
+ swap_alloc_slow(folio, may_zswap);
+ local_unlock(&percpu_swap_cluster.lock);
+ }
if (!order && unlikely(!folio_test_swapcache(folio))) {
if (swap_sync_discard())
goto again;
}
+ /*
+ * The gate was skipped when there was no device; one may have
+ * appeared since, so ask the slot itself.
+ */
+ if (unlikely(!zswap_checked && folio_test_swapcache(folio) &&
+ (__swap_entry_to_info(folio->swap)->flags & SWP_XSWAP))) {
+ zswap_checked = true;
+ may_zswap = folio_may_zswap(folio);
+ if (!may_zswap) {
+ swap_cache_del_folio(folio);
+ goto again;
+ }
+ }
+
/* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */
if (unlikely(mem_cgroup_try_charge_swap(folio))) {
swap_cache_del_folio(folio);
@@ -2052,6 +2148,13 @@ int folio_alloc_swap(struct folio *folio)
if (mem_cgroup_get_folio_swap_margin(folio) <= 0)
return -ENOMEM;
+ /*
+ * The folio can only use a real device and none has room; splitting
+ * cannot help.
+ */
+ if (!may_zswap && !real_swap_has_room())
+ return -ENOMEM;
+
return order ? -E2BIG : -ENOMEM;
}
@@ -2466,12 +2569,13 @@ swp_entry_t swap_alloc_hibernation_slot(int type)
if (pcp_si == si && pcp_offset) {
ci = swap_cluster_lock(si, pcp_offset);
if (cluster_is_usable(ci, 0))
- offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset);
+ offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset,
+ false);
else
swap_cluster_unlock(ci);
}
if (!offset)
- offset = cluster_alloc_swap_entry(si, NULL);
+ offset = cluster_alloc_swap_entry(si, NULL, false);
local_unlock(&percpu_swap_cluster.lock);
if (offset)
entry = swp_entry(si->type, offset);
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap
2026-12-03 16:43 ` [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] mm, swap: no_cache disables SSD per-CPU caching causing O(N^2)
cluster scans
- [Medium] mm, swap: no_cache guard still overwrites percpu_swap_cluster
on stale offset
- [Medium] mm, swap: no_cache list order fragments HDDs and skips
nonfull_clusters
- [Medium] mm: folio_alloc_swap() forces redundant global
css_rstat_flush() per folio
- [Medium] mm: failed order-0 folio_alloc_swap() contends on swap_lock
and swap_avail_lock
- [Low] mm, swap: commit message omits no_cache caching and
frag_clusters changes
--
commit 177a3ab0dbe1d28a173ce9251f7f626766cd240d
Author: Baoquan He <hebaoquan@kylinos.cn>
> mm, swap: do not give an xswap slot to a cgroup that cannot zswap
>
> At this point, an xswap slot holds only a zswap-backed page. It has no
> storage of its own. If the folio's cgroup cannot take another zswap
> page, giving it an xswap slot is wasted: zswap refuses the page and the
> slot cannot hold it.
>
> Skip xswap devices when the cgroup cannot zswap, and try a physical swap
> device instead.
>
> The check is obj_cgroup_may_zswap(): zswap_store() uses the same check
> but first tries to shrink the memcg, while the allocation-time check does
> not, so it is stricter.
>
> Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
[Severity: Low]
The commit message only describes skipping xswap devices via
obj_cgroup_may_zswap(), omitting the no_cache plumbing, the
percpu_swap_cluster bypass on SSDs, and the frag_clusters-first policy in
cluster_alloc_swap_entry(). Could the changelog explain these allocator
changes and the rationale behind them?
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index b2d24e1a24f08..6c1cfa0d693b5 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -1240,6 +1242,10 @@ static unsigned long alloc_swap_scan_cluster(struct swap_info_struct *si,
> relocate_cluster(si, ci);
> swap_cluster_unlock(ci);
> if (si->flags & SWP_SOLIDSTATE) {
> + if (no_cache &&
> + (this_cpu_read(percpu_swap_cluster.si[order]) != si ||
> + !this_cpu_read(percpu_swap_cluster.offset[order])))
> + return found;
> this_cpu_write(percpu_swap_cluster.offset[order], next);
> this_cpu_write(percpu_swap_cluster.si[order], si);
> } else {
[Severity: Medium]
Can this no_cache guard still overwrite percpu_swap_cluster when si[order]
already matches si and offset[order] is non-zero?
If this CPU previously cached physical device si and that cluster later
became unusable, swap_alloc_fast() sees !cluster_is_usable(ci, order) and
returns false without clearing offset[order]. The following
swap_alloc_slow() with no_cache set then passes this check and caches a
fresh cluster on si, so subsequent may_zswap folios on this CPU bypass
higher-priority xswap via swap_alloc_fast().
Also, swap_cluster_populate() drops percpu_swap_cluster.lock across a
sleeping table allocation, so CPU migration can land on another CPU that
already caches si with a non-zero offset and clobber its cached offset.
Returning early whenever no_cache is true avoids both cases.
[ ... ]
> @@ -1376,20 +1385,33 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,
> }
>
> new_cluster:
> + /*
> + * A no_cache allocation cannot keep a per-CPU cluster, so pack it
> + * into a cluster already in use and leave the free ones to
> + * allocations that can use a whole one.
> + */
> + if (no_cache && order < PMD_ORDER) {
> + found = alloc_swap_scan_list(si, &si->frag_clusters[order],
> + folio, false, no_cache);
> + if (found)
> + goto done;
> + }
> +
> if (swap_prefer_free_cluster(si)) {
> - found = alloc_swap_scan_list(si, &si->free_clusters, folio, false);
> + found = alloc_swap_scan_list(si, &si->free_clusters, folio, false, no_cache);
> if (found)
> goto done;
> }
>
> if (order < PMD_ORDER) {
> - found = alloc_swap_scan_list(si, &si->nonfull_clusters[order], folio, true);
> + found = alloc_swap_scan_list(si, &si->nonfull_clusters[order],
> + folio, true, no_cache);
> if (found)
> goto done;
> }
[Severity: Medium]
Does this no_cache branch fragment rotational swap devices by scanning
frag_clusters[order] ahead of free_clusters? For non-SWP_SOLIDSTATE
devices, alloc_swap_scan_cluster() ignores no_cache and still caches next
in si->global_cluster->next[order], pinning a fragmented cluster for HDDs.
Also, when frag_clusters[order] is empty and swap_prefer_free_cluster(si)
is true, this takes a fresh cluster from free_clusters before checking
nonfull_clusters[order]. Checking SWP_SOLIDSTATE here and scanning
nonfull_clusters before free_clusters under no_cache would fix both.
[ ... ]
> @@ -1657,18 +1688,24 @@ static bool swap_alloc_fast(struct folio *folio)
> /* Rotate the device and switch to a new cluster */
> -static void swap_alloc_slow(struct folio *folio)
> +static void swap_alloc_slow(struct folio *folio, bool may_zswap)
> {
> struct swap_info_struct *si, *next;
>
> spin_lock(&swap_avail_lock);
> start_over:
> plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) {
> + /*
> + * Do not rotate a skipped device: the walk follows the
> + * rotation and would spin forever.
> + */
> + if (!may_zswap && (si->flags & SWP_XSWAP))
> + continue;
> /* Rotate the device and switch to a new cluster */
> plist_requeue(&si->avail_list, &swap_avail_head);
> spin_unlock(&swap_avail_lock);
> if (get_swap_device_info(si)) {
> - cluster_alloc_swap_entry(si, folio);
> + cluster_alloc_swap_entry(si, folio, !may_zswap);
> put_swap_device(si);
> if (folio_test_swapcache(folio))
> return;
[Severity: High]
Does passing no_cache = !may_zswap unconditionally cause a severe swapout
throughput regression on physical SSD swap devices for cgroups that cannot
zswap?
Once percpu_swap_cluster is empty, points to xswap, or reaches offset 0 on
a physical SSD, swap_alloc_fast() misses and alloc_swap_scan_cluster()
discards next under no_cache, permanently trapping later !may_zswap folios
on the slow path even when the physical SSD has higher priority than xswap.
Every folio then takes global swap_avail_lock twice (in
real_swap_has_room() and swap_alloc_slow()), takes si->lock twice (in
isolate_lock_cluster() and relocate_cluster()), and rescans
frag_clusters[order] from slot 0 in alloc_swap_scan_list().
Filling a 512-slot cluster degrades to O(SWAPFILE_CLUSTER^2) slot checks
under ci->lock, and tail insertion in move_cluster() rotates consecutive
allocations across different fragmented clusters, breaking sequential
swapout BIO merging.
[ ... ]
> @@ -2002,10 +2067,21 @@ int folio_alloc_swap(struct folio *folio)
> {
> unsigned int order = folio_order(folio);
> unsigned int size = 1 << order;
> + bool may_zswap = true;
> + bool zswap_checked = false;
>
> VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
> VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio);
>
> + /*
> + * A folio zswap will not take must not get an xswap slot; without
> + * a device to route away from, skip the lookup.
> + */
> + if (atomic_read(&nr_xswap_files)) {
> + may_zswap = folio_may_zswap(folio);
> + zswap_checked = true;
> + }
[Severity: Medium]
Does calling folio_may_zswap() on every folio here double forced cross-CPU
rstat flushes during swapout?
When an xswap device is active and an ancestor memcg has a finite non-zero
memory.zswap.max, obj_cgroup_may_zswap() forces css_rstat_flush(), taking
the global memory cgroup rstat lock with IRQs disabled across all possible
CPUs.
Since zswap_store() still calls obj_cgroup_may_zswap() in swap_writeout(),
every swapped folio triggers two forced global rstat flushes, plus extra
flushes on zero-filled folios, large-folio split retries, and failed swap
allocations.
[ ... ]
> @@ -2025,16 +2101,36 @@ int folio_alloc_swap(struct folio *folio)
> again:
> - local_lock(&percpu_swap_cluster.lock);
> - if (!swap_alloc_fast(folio))
> - swap_alloc_slow(folio);
> - local_unlock(&percpu_swap_cluster.lock);
> + /*
> + * A folio that cannot zswap can only use a real device; skip the
> + * scan when none has room. The failure is still reported below.
> + */
> + if (may_zswap || real_swap_has_room()) {
> + local_lock(&percpu_swap_cluster.lock);
> + if (!swap_alloc_fast(folio, may_zswap))
> + swap_alloc_slow(folio, may_zswap);
> + local_unlock(&percpu_swap_cluster.lock);
> + }
>
> if (!order && unlikely(!folio_test_swapcache(folio))) {
> if (swap_sync_discard())
> goto again;
> }
[ ... ]
> @@ -2052,6 +2148,13 @@ int folio_alloc_swap(struct folio *folio)
> if (mem_cgroup_get_folio_swap_margin(folio) <= 0)
> return -ENOMEM;
>
> + /*
> + * The folio can only use a real device and none has room; splitting
> + * cannot help.
> + */
> + if (!may_zswap && !real_swap_has_room())
> + return -ENOMEM;
> +
> return order ? -E2BIG : -ENOMEM;
> }
[Severity: Medium]
Can failed order-0 allocations for !may_zswap cgroups contend heavily on
swap_lock and swap_avail_lock when no real swap device has room?
Because get_nr_swap_pages() still counts free xswap space, vmscan keeps
calling folio_alloc_swap() on every anonymous folio in a cgroup that cannot
zswap even on an xswap-only host.
Each failed order-0 call takes global swap_avail_lock in
real_swap_has_room() without checking nr_real_swapfiles first, then
unconditionally runs swap_sync_discard() under global swap_lock even though
xswap never sets SWP_PAGE_DISCARD.
At the failed label, real_swap_has_room() takes swap_avail_lock a second
time even when order is 0, where the next line already returns -ENOMEM.
Gating this check on order and fast-pathing nr_real_swapfiles == 0 for both
real_swap_has_room() and swap_sync_discard() would avoid the contention.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=13
^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v8 14/14] mm, swap: skip the overcommit probe when removing xswap
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
` (12 preceding siblings ...)
2026-12-03 16:43 ` [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Baoquan He
@ 2026-12-03 16:43 ` Baoquan He
2026-12-04 16:48 ` sashiko-bot
13 siblings, 1 reply; 27+ messages in thread
From: Baoquan He @ 2026-12-03 16:43 UTC (permalink / raw)
To: linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
baoquan.he, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
swap_info_remove() probes whether Committed_AS still fits under the
commit limit left after the device's pages are subtracted. An xswap
device's pages are its address space, not backing storage, but they
raised the commit limit at creation. A workload can commit into that
headroom, after which the probe refuses to destroy the device.
Skip the probe for xswap until the overcommit accounting for it is
settled. Real swap devices keep the existing protection.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
---
mm/swapfile.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 6c1cfa0d693b..c66b4bbc3af7 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -3703,10 +3703,17 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
*/
static int swap_info_remove(struct swap_info_struct *p)
{
- if (!security_vm_enough_memory_mm(current->mm, p->pages))
- vm_unacct_memory(p->pages);
- else
- return -ENOMEM;
+ /*
+ * The device's pages are its address space, not backing storage.
+ * The commit limit it added may have been committed against, so
+ * skip the probe until the limit stops counting that space.
+ */
+ if (!(p->flags & SWP_XSWAP)) {
+ if (!security_vm_enough_memory_mm(current->mm, p->pages))
+ vm_unacct_memory(p->pages);
+ else
+ return -ENOMEM;
+ }
spin_lock(&p->lock);
del_from_avail_list(p, true);
--
2.54.0
^ permalink raw reply related [flat|nested] 27+ messages in thread* Re: [PATCH v8 14/14] mm, swap: skip the overcommit probe when removing xswap
2026-12-03 16:43 ` [PATCH v8 14/14] mm, swap: skip the overcommit probe when removing xswap Baoquan He
@ 2026-12-04 16:48 ` sashiko-bot
0 siblings, 0 replies; 27+ messages in thread
From: sashiko-bot @ 2026-12-04 16:48 UTC (permalink / raw)
To: Baoquan He, linux-mm
Cc: akpm, david, corbet, chrisl, kasong, urezki, hannes, yosry,
nphamcs, baohua, youngjun.park, kunwu.chan, klarasmodin,
Baoquan He
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm: swap_info_remove() breaks OVERCOMMIT_NEVER by skipping
xswap probe
--
commit 11c3ffbafc7523059ca6fb8d3aa9ae7249ff0268
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: skip the overcommit probe when removing xswap
This commit skips the security_vm_enough_memory_mm() overcommit check in
swap_info_remove() for SWP_XSWAP devices while keeping it for real swap
devices.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 6c1cfa0d693b5..c66b4bbc3af7f 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -3703,10 +3703,17 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
> */
> static int swap_info_remove(struct swap_info_struct *p)
> {
> - if (!security_vm_enough_memory_mm(current->mm, p->pages))
> - vm_unacct_memory(p->pages);
> - else
> - return -ENOMEM;
> + /*
> + * The device's pages are its address space, not backing storage.
> + * The commit limit it added may have been committed against, so
> + * skip the probe until the limit stops counting that space.
> + */
> + if (!(p->flags & SWP_XSWAP)) {
> + if (!security_vm_enough_memory_mm(current->mm, p->pages))
> + vm_unacct_memory(p->pages);
> + else
> + return -ENOMEM;
> + }
>
> spin_lock(&p->lock);
> del_from_avail_list(p, true);
[Severity: High]
Can skipping this probe break OVERCOMMIT_NEVER by dropping vm_commit_limit()
below vm_committed_as?
_enable_swap_info() still adds xswap's pages to total_swap_pages, which
vm_commit_limit() includes. Once swap_info_remove() subtracts p->pages
without probing, __vm_enough_memory() rejects every later accounted
allocation system-wide with -ENOMEM, even for CAP_SYS_ADMIN processes.
This also defeats OVERCOMMIT_NEVER protection for real swap devices when
xswap is active. Because /proc/swaps lists devices in ascending type order,
swapoff -a probes a lower-type real swap device against xswap's inflated
total_swap_pages, then removes xswap without probing.
Excluding xswap's virtual address space from vm_commit_limit() at activation
and removal would preserve OVERCOMMIT_NEVER for both xswap and real swap
devices.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=14
^ permalink raw reply [flat|nested] 27+ messages in thread