From nobody Tue Dec 16 13:20:29 2025 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 881B434D38E for ; Thu, 4 Dec 2025 19:30:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764876619; cv=none; b=Q5kQfN0xuLRETVvrg3WfVqH1kre9QhmrErNCOHxwxTNCP08Bxsr45ibYzJ5n7IsA/RF04WZfYqC5Bi/WiCTuk7eoNrKejFF5bYz+bhU8T1cjaw2KmER49ABnTGn+Y2elTZZQC0+mUhkY0Y59aUX4t4JAD2Ccy2NZjqF+9nhEhBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764876619; c=relaxed/simple; bh=SY5bgYjGBbyPekog53bcEmm1Y9UCprRDCy3o9B04q9E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=buuOKuc0FAEoRI1VqZZ8ejPKw9RWyl5X1obuwh+K9+DLIW8M+PsDJ7/4Ot3ksbBNW6EJw9z4hA+ALRe72/Maqoy6kvmb7DECEY9QU4WIbgQmhTGJYoeoO1B4QiQ2i3KEZ9bumE2JPOLFPAmgF3KWQqT7I6GBWDjHkRwUjng05bM= 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=IYgTCDYu; arc=none smtp.client-ip=209.85.210.171 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="IYgTCDYu" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7baf61be569so1605937b3a.3 for ; Thu, 04 Dec 2025 11:30:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764876616; x=1765481416; 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=3tLTYBb1R8OZr/Q1lTi3HD1NNbsd1epvJzg5LKD0iOs=; b=IYgTCDYuZUe288uOOKl2tCMnm8S/FWcdhAGLN7E9aV4f16intU61+JL20QzcH6q7hY JG4dXg3NHDEX1XerK2UhqUSiRzksx8a8xzivUSWM4KXYnmINFgiiXcu40/ShIWpgXtzq tvZeouDDBOunGyQu4Z0ZClknByXtMNHb/VtiKaHFEuiii0YaJ034+XHygCRd2QLiq3Yq /UEFUKDzETjCBmUC6G0Wq/KAudUhbkb9O8iVpLzJfKZtyW7XIxYJegYLPZ6lwaeGR2U3 plQ3Z2Ncfcj6yng8FWZJdttxBQAxdjehr7w2b5d2kp00AXef+N0cZ0bCSwHVZlLNVJCh 9QzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764876616; x=1765481416; 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=3tLTYBb1R8OZr/Q1lTi3HD1NNbsd1epvJzg5LKD0iOs=; b=q0+Xh1OjfzysKINBocTuGXLoYuQXTXKW+ikArHiRXwKnApb/+ICcxPydZVOfbfnXgI hTkRwjSLnbIar5ElvWz6YyM91Fi2W2MH0eZai+/Jm213buQ1NpySkbeihXa5vvTjm4nx Karzrl4iyvWkcXRuySgil8BWwwORFC5+aoH4z0CiPVvdVq313Z2sb6dl8+dbzFBjQIjM Ncn55WrEBsAUWL2xePJoHM4D/V1XxpaK9W09It7X5jZSQbt+LrolqYLAWza9nQR5dHYW uzCIt3x7ranc22lvMD49pNjbJJMCsiS2NBVj5TbkLWqkOAQmhrpgr2ulSVYRxHM80Cxd t4gw== X-Forwarded-Encrypted: i=1; AJvYcCXH3mzDJXG/SYBgGkxQCCkaVRdgrnWNSiyiHQjKL5TOgK8gn9SK6N+NFMAI1W/Mzi/6WeJGb/iPOxWFMUY=@vger.kernel.org X-Gm-Message-State: AOJu0YyC1P14pTvrreaIdzxCNc0kz8TRs8PeTFo++W9/gX0/He4e54jP DKZLiht27vXB0Xhkvrpo3ke3Y1hCF4OBSh5xj1WF1HE5jyomKqL/g5xh X-Gm-Gg: ASbGncuJG8zsH9XakQuFYngIpdOHCoOHJPkJKheijH6AOWn3gi6KDnZxsa3LFFFze3H dZrCNCv7qT0wlrbTlh7pvZBihtF7bpHCuOyX0wpW5AZaY9/iQkWXNcuJF9nR+XRa23bohJRD/KB q+p40i7uaySFqG3yuTU7SD/1dJQfXe97abPFl9FDkGG5Edb7QrL6t0SgOykgkXKLZwx7nr5p6jz AOI/r1Fxw0ebQ8sXAtbpS5eqn8RkEBUTGik5M84fOG4cRXl5y3d2P4e6U5aguw/Y/bxPFYOTQlY qNpCxi6K94NQ4Mq4ZN+bdeZEbpI9ahTZjkjAtHUZWVK9Ch+lz10YdhiOl1LRPDz4G7KZGeXEsmX kWXHnGyBtzjvpAmnNxs7JX7WqX4rDutmuyp7YpoiXy27g+4WtoLDshcFLYosWyEVulOh6oiy0Kk py3ynB4oX15l6T3q30UDApuRzKjDgzFbLTNCVzTfjA0Xn7KtUlHY7Qhkf2MtY= X-Google-Smtp-Source: AGHT+IHvJYLo8F8+iVs5g+AC7Z1DpoGJWGbAAjlkPHHFpXjznCercQgieQwO0K2vl+49n6Q84tHoRQ== X-Received: by 2002:a05:6a20:748d:b0:35e:a390:ca13 with SMTP id adf61e73a8af0-363f5e953eemr9424236637.57.1764876615737; Thu, 04 Dec 2025 11:30:15 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-bf686b3b5a9sm2552926a12.9.2025.12.04.11.30.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Dec 2025 11:30:15 -0800 (PST) From: Kairui Song Date: Fri, 05 Dec 2025 03:29:16 +0800 Subject: [PATCH v4 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: <20251205-swap-table-p2-v4-8-cb7e28a26a40@tencent.com> References: <20251205-swap-table-p2-v4-0-cb7e28a26a40@tencent.com> In-Reply-To: <20251205-swap-table-p2-v4-0-cb7e28a26a40@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=1764876574; l=7240; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=TGM4pt6Vv98jEk/nHCqFhIlNFGrXBJCep7RNsaXlLtY=; b=2ZtxbooaUmEm69Q7j6Un4XL+5k8vWv15mF1yXP0Jzx5HPPvYKOBqYBDrzdiF8Ky+e19Y5W3Fs 8SIvKR6OGpMCZCbHPiV6hmppOtDCIKekU+9Jm3Vnk+caHaNmLi5r/sT 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 d08248fd67ff..eb9bd9241f99 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1654,7 +1654,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