From nobody Tue Dec 2 00:45:59 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 E607D2D7805 for ; Mon, 24 Nov 2025 19:16:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764011781; cv=none; b=Sqi88qmQURcDBfRd6N2Tck/TFRH65bYF2TDU2epL09V1Ru2xqAmdbLgptCZNIMnVDF0hMS3q4iAcOwFlHs2Q1oFwSmbiAhE555IusaDRj9L+0jSS5B0XMv1ZPkvaQmXaJX2DuR1bOYvex85w/eBroKDEtZoKUUl3GO7802CqkrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764011781; c=relaxed/simple; bh=uZu/oK8HF6f9aElwNggI6FXrGUebt3fNoU+h3a7hMx4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FGLGLXbdij5C8SCtHlc0hgKbDliA++NtU3tNvHbmIcPIRECGHrH4F83nE+nEsU1lF/9YRRIKWo0sZmGXG6L9E2I/+AHQjDAQFyoA0jEJSJlXHwoMXMcZiMfFqaKPNSEja4/clI442UXDXZVBiu6hOj0lHcP83rZ2B1RlHL8lZI8= 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=LioHTHqy; arc=none smtp.client-ip=209.85.210.179 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="LioHTHqy" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7bb3092e4d7so4988313b3a.0 for ; Mon, 24 Nov 2025 11:16:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764011777; x=1764616577; 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=GpNi8FjhSIttEAU4/SYztJU0s3eplrF9RCrsYzGaLWo=; b=LioHTHqyGoTGmRShqOtBD4sJkVz1pBLaOn+Z8haaNyg/e5siGwBZfNcwiewRrS/uqw t7LkOXjpip9boVVr6wgsnReQoU2UIzQPmAXaGbvbOE1TzDeL1xO6kZaH6VPn0oZ8nlJo OvIjD57hNqCjYnToTB+AI7lESVXObehG+B3Us3qiQUR+uW7BWegMh9IcncqA1Y7xjCro 7XgNY0hd+M8hJerJIPc7X7YF1s/M13XjppCpV4gsK90EEgB7TdcAx2oOqW5ujZuJp51L Xb4DSfo3mmKLag0hMkJmTanNnliaDp2hC0G70hECEc/MG8+Mrp7j4aTftTiD6nO+/OL6 Z2UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764011777; x=1764616577; 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=GpNi8FjhSIttEAU4/SYztJU0s3eplrF9RCrsYzGaLWo=; b=BC9ls+jkv1uu4egqSMfOaR7VS+L76RfImHTk7Z092sEXDVv5uJqVfGknyRaYcRqUM+ KbFgDB68gGeUCXekCmFIlCVtQtGpWKXJEQ9dym4QeaSjQ1qpLV76WFqwngGbV2lS9VEM PZZeoZH0MZnAcqO/EqxfwshRCXzwdhERplMyigUTi9tK3FRLEOSIo/mBRO6wghQqFk6+ qz5AuQdLT9v+Qcqcl/uWTkulh7aLnKJzdf/BwC3J50ySkwA94OVTbCOF8w2rCx1CmrlS 81HU83utmkZhKJYnmO3Y3QjKufdIsLmeG12YyABGyLIcUwpQp2NQF14BIYOiTCm8XXAv VSdQ== X-Forwarded-Encrypted: i=1; AJvYcCX4gFQbw18AmrmGU49SUFffk3iYaAt0Mm6HRtLI1ucf8VpPkf9sPiWOaluNalEBwM2P/Z8NuvbcJNhsj1U=@vger.kernel.org X-Gm-Message-State: AOJu0YxzMCNJ7I9SdVM6b3TDoboiurbuH1GPCbXxKHNrI3upvcPTVKSY PnVFCECqyfszI5/eKimVi4pt5Wbt0wTrCpa91H9bXn04dTvO0zlrZjyL X-Gm-Gg: ASbGnctpxOBhFSjzFxqFapFIQ35gmObu/CwsFFw302P7C2WFQwLEG6dDANly4Vw081+ EOivRJ2/+IxVfANP/3ordot7PT9FTOT7V0q63xJAkKA7QC7pGnNvlhweibrNw0UkaM7qlECmUD7 aTbv9T+MHs4M1EuQRj6eYjX8c7t9qOVwu7UDbPcicAIaVzFsalLmceqqESCtuOVs+tc4ChFAXmL eO9ORonF9hDmaHgVpgeTQnGBsQoq5rsgCQY3VQa4uyzA0uTcIOVf7ybt1J1JdWoY/gs/Dzpy9tt jlSlKO5OE29I6pXuz4MLDeFcs5ftwt6W+t2XV7TCo/K6QU0o8Ba0JsMx5p4vCNPY8LcMA0T/n3J aD19cAokbrUb8VnLxZMn7R4GSm4KJR64WKPHbuy1tR2owiJAlwjGGma89GqCRpXQ/X4V056nZlh 34KGgrQgNOBQfNLRMGR8TjnfBsrMrR+jw66XeloSXFhmoZTEwVeG6sROECXvg= X-Google-Smtp-Source: AGHT+IECvy8wsfwYygzgHpvNBsk6OsB2Az3ifuap9gn5TViDmzLyWX3HaNQQ51t3DUydcesHNZXR3Q== X-Received: by 2002:a05:6a21:6d96:b0:334:a784:3046 with SMTP id adf61e73a8af0-3614ed971e2mr14909542637.38.1764011777067; Mon, 24 Nov 2025 11:16:17 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-bd75def75ffsm14327479a12.3.2025.11.24.11.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Nov 2025 11:16:16 -0800 (PST) From: Kairui Song Date: Tue, 25 Nov 2025 03:13:51 +0800 Subject: [PATCH v3 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: <20251125-swap-table-p2-v3-8-33f54f707a5c@tencent.com> References: <20251125-swap-table-p2-v3-0-33f54f707a5c@tencent.com> In-Reply-To: <20251125-swap-table-p2-v3-0-33f54f707a5c@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=1764011730; l=7126; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=mVLrWCcOCF2opEwIpHmoO8gRgwudRDdzZKps7g3enEw=; b=gjX6UZ//yplqGvwKWzYk0/CGzU4i15dfWYfnqKLpnNStNKAwg1KK+h+8qq0JEo/rh7L7WJgEM ZE9ktkILD3nDsYG5yQ0QDKAumJbGDDjOdE35IHicpm9T9e0+t2jBweo 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 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 5853db044031..cc07246985ef 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) { @@ -1519,12 +1519,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)) @@ -1622,7 +1616,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); @@ -1676,12 +1670,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 /* @@ -3674,7 +3666,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; @@ -3734,27 +3725,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