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 47E6C606A32C for ; Thu, 3 Dec 2026 16:44:28 +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=BRFTJVw0 Received: by kanga.kvack.org (Postfix) id F0E776B05BD; Thu, 3 Dec 2026 11:44:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EBF506B05BE; Thu, 3 Dec 2026 11:44:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFBB26B05BF; Thu, 3 Dec 2026 11:44:20 -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 5A5C96B05BD for ; Thu, 3 Dec 2026 11:44:20 -0500 (EST) Received: from smtpin04.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CD869A843E for ; Thu, 3 Dec 2026 16:44:19 +0000 (UTC) X-FDA: 85503244638.04.D05B220 Received: from mta0.migadu.com (out-64.mta0.migadu.com [91.218.175.64]) by imf01.hostedemail.com (Postfix) with ESMTP id 394D740006 for ; Thu, 3 Dec 2026 16:44:15 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BRFTJVw0; spf=pass (imf01.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.64 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796316258; b=A8lTXaM39nPAd1+LDzDOeDuEttXB7FPtH3vQjMvH7eOZKJgvDdvBUhFXLpaW0T082vRdbS JIHow+UGiHlR2jLNwijLwnFacuqCMDG8Ty7B+QSVE2fpJI7HT/fNtMhmhCWA0LddSqjqRY EkeIUCeIm32gberDoA+IU6HGTmikzRw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796316258; 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=RRiFjuL0waGNDhJ0iqDa0pKd/kkt2yNMSRQ9mB7viIY=; b=Lh+cCHJg6Xm3LuAO/08CATkxO7zYo9LM5rEFaQ/xyg0GFp8tZGuwgM4oarrl+U4/WIODb9 LVRo2JI7L2SKdxxrQ13alfYyou+3ScVdn4f9UtTZKKQFNCZO/MhyPTcU/58XrN8ypr4mBc r2qDOHL6g6eA2er/VIH1PzV8VdPs+g4= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BRFTJVw0; spf=pass (imf01.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.64 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=Lcup8QrLK8VQSkMs17NPv0FQqSMBVPUPFxtVxyy8nUc=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796316254; v=1; x=1796921054; b=BRFTJVw0R1cIR6QQO4tt/csk0R+QaV4Lvsibz9IdNSc32EM3B2CRkGQZKg2SrBld1kzI0kty RBKiKEGGj6NvMOrXX3jtSNc8ox8smwcy++sW1YEIk4dTjy0h/9TVS/ujMMcnb59XWUlm90+T1zH t768oWOvjHV8dBZvdvi2Y0RY= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id 542e31f5403bd68c; Thu, 03 Dec 2026 16:44:14 +0000 X-Mizu-Trace-ID: 542e31f5403bd68c 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 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Date: Fri, 4 Dec 2026 00:43:27 +0800 Message-ID: <20261203164336.864691-6-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-Queue-Id: 394D740006 X-Rspamd-Server: rspam03 X-Stat-Signature: q3edcsmaxp68ipxcprzgswzfpm8sse7w 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: 1796316255-761715 X-HE-Meta: U2FsdGVkX1+OhXUHpRbglJHXtIlc1cZr9sCHAfee5QgnyzoW+kES8g9ndcf7r26dkGZP5/vmthSV/xPLGUNhBNHo089/i/hHBEZEs5mE0bfyLDmSFp8oDGUgRSN5FKn5303wmcJusMpjjy5iDSAm4qDLuxSunhG+djsxVuEZtfUQlF3MlAITKt88LXte968JtYlnsHnYA4qBhl8k40MvFRQo3SaLa4hxBPkzRT4SZYXuzZ4wg9XLy3apkn8ylvCjbo2i5f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AJWP/53tuE8LcXP34w7XQIuzK1AauEdZ9cBcJfA8SHRwv17m13emZwcFkvbbDb+QsiIHe517s544r9kdeffY8MpEbSclftDx4mKw6CBtVGY82H31T41lwzsg/NIrpqhoQSj2sW0d2QpjnJz+ZWZUDvM587/Jd53E6paAVLGNYTuW+et2dtMwQbsAYyhr/1D4IwUztYtS58qGjNJ/3PBfzmm97uks0jBZcCPbCX30P+9zoCHRLB6IZeGVsBPY6T55iqT3CVjsaiAx+g== 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 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 --- 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