From nobody Tue Feb 10 04:03:22 2026 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81A6F22AE45 for ; Fri, 19 Dec 2025 19:44:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766173499; cv=none; b=lRghOSXVCDo+/mUkawtk6bfAe2SrGTpoiIyFDB/VzJ5ExII3VZQ0WZghOazu4jScxoPwNfUlQGDUA8FGXUoCqWJRtEV+P01GMDaTzOueJY5FoZrTh0LiKwa8XWW+/Gt0uc5i7GZ72UmjX4O3N4eWvh8oi/qygNY2Fbuc2NK+1DQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766173499; c=relaxed/simple; bh=JUvM6nUGOYJ7aMfEl8h5cvA1xyJUb85NTuiyaTCPyLg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PCFF+FvfLaCAB5ipzZpedAUCUDUjIY1RRzgdRtU7XZF7OeQATsMEA9NcmsC1SacTHnlsRlqeQwcHeWtEn7fCI2cgzm+5Vo1vAYEmztdYxc+QRF0XGHEmPpoBBkHGOzFjky2vvXEFGIZcjjZj0KsH1MEnq30qB7VnkEasXdi1lBY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CvDkMq36; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CvDkMq36" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2a099233e8dso19986885ad.3 for ; Fri, 19 Dec 2025 11:44:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766173495; x=1766778295; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=2dPOaaOJ2TQSWYXjPLzkHtEJKqmOi8KzZJUw0xmP+Hk=; b=CvDkMq36JGmd/2y/GH4A0APytcQwQ9OLa1vpNqKtGxE2MnK9IMtF58LrTwz9EHK4AA tQe4hhQWx4LW0508UdKKurLhchFhYbHht+IlLzrFyW7TVsdND97OcuV854pIefqagrJl QZnHdWMrRCOVcZdqXh4ua/PgjNQvrj+8fqtG6J/SeY956DjXph6/k6NqPAAtsyY6z3I8 5ibcdrVTghc+yqNR7xQfLHaoGz4sbtAjPnDjDzPow0a3Uj+2TcaeFxF/Rin/I1XbSXq+ sfXEE0oY2fkj8F0+b5GpDk/TQB+y10pVgi0/YbVS2WsEUhF/ue6zxb7790O86f4d+FQe QVAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766173495; x=1766778295; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=2dPOaaOJ2TQSWYXjPLzkHtEJKqmOi8KzZJUw0xmP+Hk=; b=kchwNqzn/1h1omSorTJnVmRf+ZqbSn7lOtC9IBUNyRqSMYl73SwC+oXiXLJ14Nc32N d5zdJzf3ByRoghJhofFSKUvaeFm3ofUBPFzkmxlsVcS0SbHDHZoyKHhx1rCcYqoQiC5A YTIFzGLJvEDQBX9iyTT0r81fzSgD+/Ddkycpfj/jH9FfZm7tHQTiz/jC+16+dmqU8szW 4FN+pk8azZuzsLuUFXbEfnH9rz7YDmGto/672bvFtah4s/0f7cdVOmMJ+rL3eEckaKpA S7ya4zBL2M/AUKeDfLFAHAFP7mSQ83TyZB5cnXAtJt52w06Hupzw7IF5Re+o+gDERtjT yDNg== X-Forwarded-Encrypted: i=1; AJvYcCWj9L3bJ3OS76GkaoDGOXaMijJaBro+aE8dBTKWf7Sm0Yw+bchBLFMYSGeEcMzrtod7rmFm7DNNuBsG8SU=@vger.kernel.org X-Gm-Message-State: AOJu0YyqITAV4Em96Kg6EMrK4LOZTUu23nQrlEKkq16nxruCjAlS5Q03 0aYMPFXe/a8GbIPdFTWRDBY4VYC00o6IQHWqpcXdQXDGQ6mXWOjikrSQ X-Gm-Gg: AY/fxX7cHU8YUQYxqJiunVcZdtGcrRFS6FbS0068m0+0I4N1+f/ZkKc7hjaQGe9Rm9D lQ+I0VgEvCeJXC83hQ9tFMSNbEUH7Q/Jtz0h4RBeGpdZ2EGb7mSz8iUP/T62pmH+PZcAs9il0Yh BXPgsTHSrvqExgjMgy81XOWDfBzaIJFoB4yf+ixqRzJm/q7KfK4BKN/if6xfLFVQDmbswj7H6gT UhHrsJRml6hODdbYtx5Xw5rBYYv8G6MUbsaMnLVm5pvCyOFfbrnBlIWoDLeFP8e+kJ91WTI7iOZ f06d4Qyz5q/8RQONxDz0frjpUYHLh3mG+JE+LKSSog9pO8KpY9KpeNllmF2ObdD5yBIYdCT5CAB M+EXodX1/hgn8WP3p3C5avSzTJ38+I6bDdMVbDxd/5FLshcPnzUI0wvZ7IKhBKqUtbLZep/KKpU HmFRjFAc5X2RgSSDTM9i7fAlRmCJMW9wQrLPMbDrBFDxtGrqVHfiLWiDFxS5ymZ2k= X-Google-Smtp-Source: AGHT+IE4dkzSppOt3tHGWCnWXkosxODEpLg9AmqyJsLq4gVhbtsrGH39aXgfltGk3xyFoNlZwzd48w== X-Received: by 2002:a17:902:ef52:b0:2a0:dabc:1383 with SMTP id d9443c01a7336-2a2f22234a1mr44134155ad.14.1766173494957; Fri, 19 Dec 2025 11:44:54 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a2f3d76ceesm30170985ad.91.2025.12.19.11.44.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Dec 2025 11:44:54 -0800 (PST) From: Kairui Song Date: Sat, 20 Dec 2025 03:43:37 +0800 Subject: [PATCH v5 08/19] mm/shmem, swap: remove SWAP_MAP_SHMEM Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251220-swap-table-p2-v5-8-8862a265a033@tencent.com> References: <20251220-swap-table-p2-v5-0-8862a265a033@tencent.com> In-Reply-To: <20251220-swap-table-p2-v5-0-8862a265a033@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Baoquan He , Barry Song , Chris Li , Nhat Pham , Yosry Ahmed , David Hildenbrand , Johannes Weiner , Youngjun Park , Hugh Dickins , Baolin Wang , Ying Huang , Kemeng Shi , Lorenzo Stoakes , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1766173451; l=7240; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=JUOvtAP1dKiH1CMj9+4csv5/DFFUtuyY45QPHt+u/IE=; b=8Jn84cPjQ22CkwioQYmAbl/Yn1agcuCqkgakUWKAtfNfnUYO4P3hs7B2jD8IdxX2p72W98ogA i5Nuo+l6LCbDAysmgOoza5guwPPrK0mCtHVeysaL6aklklSSnaHnS1h X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Nhat Pham The SWAP_MAP_SHMEM state was introduced in the commit aaa468653b4a ("swap_info: note SWAP_MAP_SHMEM"), to quickly determine if a swap entry belongs to shmem during swapoff. However, swapoff has since been rewritten in the commit b56a2d8af914 ("mm: rid swapoff of quadratic complexity"). Now having swap count =3D=3D SWAP_MAP_SHMEM value is basically the same as having swap count =3D=3D 1, and swap_shmem_alloc() behaves analogously to swap_duplicate(). The only difference of note is that swap_shmem_alloc() does not check for -ENOMEM returned from __swap_duplicate(), but it is OK because shmem never re-duplicates any swap entry it owns. This will stil be safe if we use (batched) swap_duplicate() instead. This commit adds swap_duplicate_nr(), the batched variant of swap_duplicate(), and removes the SWAP_MAP_SHMEM state and the associated swap_shmem_alloc() helper to simplify the state machine (both mentally and in terms of actual code). We will also have an extra state/special value that can be repurposed (for swap entries that never gets re-duplicated). Signed-off-by: Nhat Pham Reviewed-by: Baolin Wang Tested-by: Baolin Wang Signed-off-by: Kairui Song --- include/linux/swap.h | 15 +++++++-------- mm/shmem.c | 2 +- mm/swapfile.c | 42 +++++++++++++++++------------------------- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 38ca3df68716..bf72b548a96d 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -230,7 +230,6 @@ enum { /* Special value in first swap_map */ #define SWAP_MAP_MAX 0x3e /* Max count */ #define SWAP_MAP_BAD 0x3f /* Note page is bad */ -#define SWAP_MAP_SHMEM 0xbf /* Owned by shmem/tmpfs */ =20 /* Special value in each swap_map continuation */ #define SWAP_CONT_MAX 0x7f /* Max count */ @@ -458,8 +457,7 @@ bool folio_free_swap(struct folio *folio); void put_swap_folio(struct folio *folio, swp_entry_t entry); extern swp_entry_t get_swap_page_of_type(int); extern int add_swap_count_continuation(swp_entry_t, gfp_t); -extern void swap_shmem_alloc(swp_entry_t, int); -extern int swap_duplicate(swp_entry_t); +extern int swap_duplicate_nr(swp_entry_t entry, int nr); extern int swapcache_prepare(swp_entry_t entry, int nr); extern void swap_free_nr(swp_entry_t entry, int nr_pages); extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); @@ -514,11 +512,7 @@ static inline int add_swap_count_continuation(swp_entr= y_t swp, gfp_t gfp_mask) return 0; } =20 -static inline void swap_shmem_alloc(swp_entry_t swp, int nr) -{ -} - -static inline int swap_duplicate(swp_entry_t swp) +static inline int swap_duplicate_nr(swp_entry_t swp, int nr_pages) { return 0; } @@ -569,6 +563,11 @@ static inline int add_swap_extent(struct swap_info_str= uct *sis, } #endif /* CONFIG_SWAP */ =20 +static inline int swap_duplicate(swp_entry_t entry) +{ + return swap_duplicate_nr(entry, 1); +} + static inline void free_swap_and_cache(swp_entry_t entry) { free_swap_and_cache_nr(entry, 1); diff --git a/mm/shmem.c b/mm/shmem.c index d7eeeaa9580d..e36330cdd066 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1667,7 +1667,7 @@ int shmem_writeout(struct folio *folio, struct swap_i= ocb **plug, spin_unlock(&shmem_swaplist_lock); } =20 - swap_shmem_alloc(folio->swap, nr_pages); + swap_duplicate_nr(folio->swap, nr_pages); shmem_delete_from_page_cache(folio, swp_to_radix_entry(folio->swap)); =20 BUG_ON(folio_mapped(folio)); diff --git a/mm/swapfile.c b/mm/swapfile.c index 3762b8f3f9e9..e23287c06f1c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -201,7 +201,7 @@ static bool swap_is_last_map(struct swap_info_struct *s= i, unsigned char *map_end =3D map + nr_pages; unsigned char count =3D *map; =20 - if (swap_count(count) !=3D 1 && swap_count(count) !=3D SWAP_MAP_SHMEM) + if (swap_count(count) !=3D 1) return false; =20 while (++map < map_end) { @@ -1523,12 +1523,6 @@ static unsigned char swap_entry_put_locked(struct sw= ap_info_struct *si, if (usage =3D=3D SWAP_HAS_CACHE) { VM_BUG_ON(!has_cache); has_cache =3D 0; - } else if (count =3D=3D SWAP_MAP_SHMEM) { - /* - * Or we could insist on shmem.c using a special - * swap_shmem_free() and free_shmem_swap_and_cache()... - */ - count =3D 0; } else if ((count & ~COUNT_CONTINUED) <=3D SWAP_MAP_MAX) { if (count =3D=3D COUNT_CONTINUED) { if (swap_count_continued(si, offset, count)) @@ -1626,7 +1620,7 @@ static bool swap_entries_put_map(struct swap_info_str= uct *si, if (nr <=3D 1) goto fallback; count =3D swap_count(data_race(si->swap_map[offset])); - if (count !=3D 1 && count !=3D SWAP_MAP_SHMEM) + if (count !=3D 1) goto fallback; =20 ci =3D swap_cluster_lock(si, offset); @@ -1680,12 +1674,10 @@ static bool swap_entries_put_map_nr(struct swap_inf= o_struct *si, =20 /* * Check if it's the last ref of swap entry in the freeing path. - * Qualified value includes 1, SWAP_HAS_CACHE or SWAP_MAP_SHMEM. */ static inline bool __maybe_unused swap_is_last_ref(unsigned char count) { - return (count =3D=3D SWAP_HAS_CACHE) || (count =3D=3D 1) || - (count =3D=3D SWAP_MAP_SHMEM); + return (count =3D=3D SWAP_HAS_CACHE) || (count =3D=3D 1); } =20 /* @@ -3678,7 +3670,6 @@ static int __swap_duplicate(swp_entry_t entry, unsign= ed char usage, int nr) =20 offset =3D swp_offset(entry); VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); - VM_WARN_ON(usage =3D=3D 1 && nr > 1); ci =3D swap_cluster_lock(si, offset); =20 err =3D 0; @@ -3738,27 +3729,28 @@ static int __swap_duplicate(swp_entry_t entry, unsi= gned char usage, int nr) return err; } =20 -/* - * Help swapoff by noting that swap entry belongs to shmem/tmpfs - * (in which case its reference count is never incremented). - */ -void swap_shmem_alloc(swp_entry_t entry, int nr) -{ - __swap_duplicate(entry, SWAP_MAP_SHMEM, nr); -} - -/* - * Increase reference count of swap entry by 1. +/** + * swap_duplicate_nr() - Increase reference count of nr contiguous swap en= tries + * by 1. + * + * @entry: first swap entry from which we want to increase the refcount. + * @nr: Number of entries in range. + * * Returns 0 for success, or -ENOMEM if a swap_count_continuation is requi= red * but could not be atomically allocated. Returns 0, just as if it succee= ded, * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), wh= ich * might occur if a page table entry has got corrupted. + * + * Note that we are currently not handling the case where nr > 1 and we ne= ed to + * add swap count continuation. This is OK, because no such user exists - = shmem + * is the only user that can pass nr > 1, and it never re-duplicates any s= wap + * entry it owns. */ -int swap_duplicate(swp_entry_t entry) +int swap_duplicate_nr(swp_entry_t entry, int nr) { int err =3D 0; =20 - while (!err && __swap_duplicate(entry, 1, 1) =3D=3D -ENOMEM) + while (!err && __swap_duplicate(entry, 1, nr) =3D=3D -ENOMEM) err =3D add_swap_count_continuation(entry, GFP_ATOMIC); return err; } --=20 2.52.0