From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CDB8B606A32C for ; Thu, 3 Dec 2026 16:44:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=pxvIqV/k Received: by kanga.kvack.org (Postfix) id 17AA26B05BE; Thu, 3 Dec 2026 11:44:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 12AC26B05BF; Thu, 3 Dec 2026 11:44:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 067406B05C0; Thu, 3 Dec 2026 11:44:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id CF7266B05BE for ; Thu, 3 Dec 2026 11:44:26 -0500 (EST) Received: from smtpin10.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 621281406E7 for ; Thu, 3 Dec 2026 16:44:26 +0000 (UTC) X-FDA: 85503244932.10.C32C196 Received: from mta1.migadu.com (out-147.mta1.migadu.com [95.215.58.147]) by imf23.hostedemail.com (Postfix) with ESMTP id BF309140007 for ; Thu, 3 Dec 2026 16:44:22 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="pxvIqV/k"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf23.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.147 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796316264; b=AyDa9DwgkG3oW9y4oXiTTwq5M5kbH3kXca0AquyecSz6qKCnJCTRUHqB+7YXQSVRx2OfIc Reqb0/drdSUtf8lccvCDgZTLFvxKKv1Ct0FzNP7Q1bEuDHH1puNylugMvQCTS3cMhi8Cgm 1inweEWalY5/N52F8QlFjony+zOKeVU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796316264; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KObD6+iBN6T8O3YwNfVU3oThvKdLETGo/xOpoi3TTzE=; b=V3oQU+KVi6DHQ0QKnJl07OXbf9b+vtZYvHk71JQUPIZDJuhGcDCH/ix8wM32dx4TSW9dhv hTk+4qVv5uptJzvv9H/ThVMU8kSS9CqpLJRHszXjdn9h2jBX2w8jQfQCRZVxcJRElPE/Fw rc2VdQEOAJ/lFqZscZczLZOJkcLIflU= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="pxvIqV/k"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf23.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.147 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=GzA10W6RZKUzwaRBCRimKZILpU31rOosVTJgXWHg4lw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796316259; v=1; x=1796921059; b=pxvIqV/k1JUnMnARDhryirLeUnW8uiSl6INigYuXGgpuxpVYpUs8YshlIeuZ6wflFHiHtJyx 3xw/y5KVjf8K7Dx+WLmxsJ/uV3uKGVI74N5ZEZUjAGfRgVk26EKjG4wlZ58iE8sctaAJPvq5awx /w6CsQHiOq0SPhOEvbuzMS6I= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id 3374b966b92a316c; Thu, 03 Dec 2026 16:44:19 +0000 X-Mizu-Trace-ID: 3374b966b92a316c X-Migadu-Flow: FLOW_OUT From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, corbet@lwn.net, chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org, yosry@kernel.org, nphamcs@gmail.com, baohua@kernel.org, youngjun.park@lge.com, kunwu.chan@gmail.com, klarasmodin@gmail.com, baoquan.he@linux.dev, Baoquan He Subject: [PATCH v8 06/14] mm, swap: create the xswap device from swapon(2) Date: Fri, 4 Dec 2026 00:43:28 +0800 Message-ID: <20261203164336.864691-7-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261203164336.864691-1-baoquan.he@linux.dev> References: <20261203164336.864691-1-baoquan.he@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: BF309140007 X-Stat-Signature: am5wtbb6jy3kcqpthboz9xz46rddcfqh X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-HE-Tag: 1796316262-611971 X-HE-Meta: U2FsdGVkX1/7i1LIgV/BNcebLhBmFUQVDuy+dyNOrQ/6w6YdRWcdZYpbnCdv5uou6uvmZdrB92bkZYjbfWS6ijOVnP7J60bMuHViZ5PBM10GbKnHq8l5XUY6u9ZS868wWinJ+euxFMvPUuv7ZWSbcZo7qRAo5wo4ycOfr/7+sr0mnTm/43f99x2rQyAvB3o+VmcrY3aRe6mbUXYA7061QpY/4IYX7AdKinXKk/UEa1r/V5osSDJGJoie8mvdUhADX6Ui4m8gPCSDJ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c/vn1FyhcOGWWRDcH2d7iwhLT+ez0SIY97jYEBJN0PL+6Qb0lxuVrSXq+FTgy+NaYlIJwZW6oXrkk6nuaav9+PD5xhGDoAvy7g8t4P7Co9hpu4Sp9mTYa5uvYCxq21+gSVFc7/CHQCpfogqR7QDxR+4= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Baoquan He 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 --- 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