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 3DF17606A31C for ; Sun, 29 Nov 2026 15:08:45 +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=C8xHPnpg Received: by kanga.kvack.org (Postfix) id 5AD696B05B6; Sun, 29 Nov 2026 10:08:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 55DFD6B05B7; Sun, 29 Nov 2026 10:08:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44D966B05B8; Sun, 29 Nov 2026 10:08:43 -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 CCB736B05B6 for ; Sun, 29 Nov 2026 10:08:42 -0500 (EST) Received: from smtpin24.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 52EC4A855C for ; Sun, 29 Nov 2026 15:08:42 +0000 (UTC) X-FDA: 85488488484.24.7E42227 Received: from mta1.migadu.com (out-96.mta1.migadu.com [95.215.58.96]) by imf17.hostedemail.com (Postfix) with ESMTP id 4BEA540010 for ; Sun, 29 Nov 2026 15:08:40 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=C8xHPnpg; spf=pass (imf17.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.96 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=1795964920; b=WyVnI7L6fNuy68TPllV5k26G0u2SQV2nW+JrQV2fdJuK1sHJQJpaW35ZuYfzYXe/Ec9atJ qeH/fH/jA7bdjE0LUHQ8V7fJYxOTz7mXS+HwB13vxOrN0iCya7012isk8UmU0vaHrnLaaS wQTiPFSSZA929g6P2LcMLvWNAJf3u3U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795964920; 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=sfdLmPXD78gNR0ys0t0J47QbrYNPPzbPetuGyK+RPSXOXaKpY7qmDC3dzRiLvsyWjMb7hr uUrFAEFnoCsxSAzYGfhIGjWg4FITHjoPs0gFFEJJpsVnItPq/TyVu1CMxweYY527DzW5iQ CJwtjH4L97qglE+CsSlM32/MGElTI+g= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=C8xHPnpg; spf=pass (imf17.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.96 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=1795964918; v=1; x=1796569718; b=C8xHPnpgjaediSSv58wpDEpZo5KoxovwnQEQsShVjisIYpycFwbBOauCgD6z29vULMjMc3Mq TrK/ebkUSw6ObV2C0S0uFRXTIjlyFVjN7B7EJU7kXcDp5MaZn3wSunjTxPErCcaR/vpcvBJJFlr zQiReer9y0HTkWeGqRriyJ8g= X-Envelope-To: linux-mm@kvack.org Received: by mta11.migadu.com with ESMTPS id bf1cb69abb437f9c; Sun, 29 Nov 2026 15:08:38 +0000 X-Mizu-Trace-ID: bf1cb69abb437f9c X-Migadu-Flow: FLOW_OUT From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org, corbet@lwn.net, nphamcs@gmail.com, baohua@kernel.org, youngjun.park@lge.com, shikemeng@huaweicloud.com, yosry@kernel.org, chengming.zhou@linux.dev, kunwu.chan@gmail.com, klarasmodin@gmail.com, linux-kernel@vger.kernel.org, baoquan.he@linux.dev, Baoquan He Subject: [PATCH v7 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Date: Sun, 29 Nov 2026 23:07:46 +0800 Message-ID: <20261129150755.191662-6-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261129150755.191662-1-baoquan.he@linux.dev> References: <20261129150755.191662-1-baoquan.he@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 4BEA540010 X-Stat-Signature: w7c57ge7zomxdgxu8zz15ym66ywedktj 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-HE-Tag: 1795964920-738685 X-HE-Meta: U2FsdGVkX1+RMChnZ69tTX2x1McO4f9mBSRvp/r6XuccUjneA9Azp1AKxSTP1o1BXAbKYfBzYTo29UI1tEDH7YQpuz6DO8WE9OF3cjR7as8Ihsq7Ju0CXDh0qDF7xeYGti6c+iX0vTFrmwatvh9z4sRSzo3I34arSpDTsvi4buF9+BvN4CrVWPiuFjqZqp0sOgV7Vhljn06OLd7BG7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8P2LKy+gL4nBlwetmegx4/yXZmnbBod54zW+r4AcBs1G+Pvqpz1kOCq8GDIfw465QdW+/CnnZIhu0lJde0/90AC9DFBh+MRZEqDoJPvDNp0ZcjadnKh8pyO4dk3/RczpHcS2kOoye4Qbw9TKVbqKQu0BDTyvUTU4FdDdvid/JaquuluJ+liEhx90dnEB0lbqoZiiqYgH/9zlcB82QVEZH9hEYWJiRBBY1li6U322ts4riSU0AoZ3l2Lu2acFqPcJVFJ0lEWahyZH/JldcMNhQyKATx1I/cSiloIekB00Gkg58+SAX3ykdUoq8qx3S92fkZWRNgVG3UD0hPSQ== 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