From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D575BC3ABDA for ; Wed, 14 May 2025 20:17:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5484F6B0093; Wed, 14 May 2025 16:17:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F9DE6B0095; Wed, 14 May 2025 16:17:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3746B6B0096; Wed, 14 May 2025 16:17:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 172586B0093 for ; Wed, 14 May 2025 16:17:59 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D2282120712 for ; Wed, 14 May 2025 20:17:58 +0000 (UTC) X-FDA: 83442624636.12.178E222 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf12.hostedemail.com (Postfix) with ESMTP id D2F5040003 for ; Wed, 14 May 2025 20:17:56 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ShGKdljo; spf=pass (imf12.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1747253876; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2z1TlsbEqX0sItW8zsPfNuaZk6YPjORmjX5QgGuC680=; b=sGVNlKs3qAqRiAG6Nr4++pPaOleXy+9a0o/6/2GjhnksxdXiwHD3UGKzTRZFdapll1Efwe Th9o0kCUP4+irhbNt2zBkrixx0ISUHx6R7OQU97WNIs+eyHzkP6ycCbsRLzfqcdr+uijwF vXA2bZvvUQFPzdjBPxWv/+lJV/2Hlak= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ShGKdljo; spf=pass (imf12.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747253876; a=rsa-sha256; cv=none; b=u4hEGSxD27J8AKo9+bpojM1gQ5hAojil5HniQIq4BF9q0qHRpjePtqiKQ7S2JtgQirMSjR LRKaYqaXJ2EaNUn7PYWj/yfz13AOYY1KlXVg1SOZVnsgkhHEKabuyjUS0dbWOhTu5tQJu4 DrX66la89Z5p7QNymwtV/8UmlyP3QN0= Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-af59c920d32so84747a12.0 for ; Wed, 14 May 2025 13:17:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747253874; x=1747858674; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=2z1TlsbEqX0sItW8zsPfNuaZk6YPjORmjX5QgGuC680=; b=ShGKdljo4Kg5C6uUbVphoi3iAUZB0UAAvGkBLXpqpnZIvJ1M7fdygK/r5xw8nYDIyn IC4rzgg4rsJddH/MrPzuqyTlkm+toDP9+QH83oH3n2WXd26yGxFfVSa34DW8AeDKarTM pbda+UsXyhpUN6h0Z4CCDmi9TxmlvMOJF8/d7N+2bonDf3XiWMU4QKMOPsGfBMO1xkOk YEmqu9Lutl+MfSE9XfVChxgbvSZD2GhZ933c0AG90CqVrUOqPliXqvUHVaSVe0ispfaE sQbdULYRSuL0Q3e6WOW+Zf74Zo9qiDFoZC6lg3xtDX7xWaGBGUGWr4g6tIxf6Rnids95 bU1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747253874; x=1747858674; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=2z1TlsbEqX0sItW8zsPfNuaZk6YPjORmjX5QgGuC680=; b=lx6xuytFFIcSNZvN6I+/yW/BVKRTwKOVEbVM1IBKXhaFTmk1fJeco072FOmtK5gIba 3w2xv+/pyEqa0wshTTLfFys/Ncll4omeEpt4zr/tRHqSyrALz888bVZ1cfgfuZgI0vpx Wbg4TRyGFr7wjJCQXtsSNmQB1LCtkZTZsCzrEOkGxTQmRt1oHL9m6+1w140bmBv+nW9W NHt7nSfd1bXU6w6AoCGQFIuu1iUeRjouRTYXqDa8rSd0km3jTRzlgEMMaAJqgnbQxDpI l+UyCaIwkRw3z4SX+si9saCKI7l8liqJdAaIVfG7h41R3ihdn6aHf5jWU9ZOBH5YMC7e D8oQ== X-Gm-Message-State: AOJu0YyF8/WT89NPwFEAeRJfbnwkeXmAZUqlPWDHqfS6zD79cwNQ84Tp Uc5sdHkye4NJMX9/bgMZwJow7IpGyYuiEwEvOJsIIu9XytAUBeU4l6WXyjZ1BAA= X-Gm-Gg: ASbGnctE401ERhDGFgD3kX6iaqNBPaf7M0nHiuvvFu9uOcKN0KrVkep6Vxu/f6HXq34 yhUVcs8EssvYixA+NRzm54x2+KUSsbdfa5xUy83L9IkXeJ426JeN++P/xTCLYCURUcJf2roeyWC yP3f76725vEHqOx+ku3of+OR1ZD+jsdhmBJX5yrqDpvcXgGFPl3IWCV4jXhoIwrskidOvGBXlSs bzYv81aFycYq6wAfqeKkyGRstS6xB8TIUpjETCDYvhrxrV5lHmxThG0LaEfyzUQeruOReKBFF0/ TTkYwmGSK6cvVe55ETe7CsI8a1yKhuexEV0rH4fSjiglK5ENa9+M+jr0bQ+n5PwY5lfG64GW X-Google-Smtp-Source: AGHT+IFY6uiI2xptl3mpQDnMC5ZiwlzDn0u7+jZfbafhM4MOLlbJdrQIJg4fJbsOQyWnC8PPZjjyiA== X-Received: by 2002:a17:90b:2686:b0:2ff:6e72:b8e9 with SMTP id 98e67ed59e1d1-30e2e62c352mr7084586a91.25.1747253874465; Wed, 14 May 2025 13:17:54 -0700 (PDT) Received: from KASONG-MC4.tencent.com ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-30e33401934sm2003692a91.9.2025.05.14.13.17.49 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 14 May 2025 13:17:53 -0700 (PDT) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Matthew Wilcox , Hugh Dickins , Chris Li , David Hildenbrand , Yosry Ahmed , "Huang, Ying" , Nhat Pham , Johannes Weiner , Baolin Wang , Baoquan He , Barry Song , Kalesh Singh , Kemeng Shi , Tim Chen , Ryan Roberts , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH 03/28] mm/shmem, swap: remove SWAP_MAP_SHMEM Date: Thu, 15 May 2025 04:17:03 +0800 Message-ID: <20250514201729.48420-4-ryncsn@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250514201729.48420-1-ryncsn@gmail.com> References: <20250514201729.48420-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: ht1ixyyxtmspuc8xggcqku3em6dd974b X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: D2F5040003 X-HE-Tag: 1747253876-184615 X-HE-Meta: U2F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zoDPiZsfoRnYGkg7pem3qSrfDUQ0e+p115XxTkVsGMeQub/JoyetdU1NnipHopMKwRB+WFqXJw7+lqO/YLglkaXY8iHbslThVBChnFDl6f8ChVp7BMO+A6fgqN3bnWYiKIw7LGTn7JhCkTDJNu2vN1wJsXtbRaSn9YY32bG5qO40Hg2/Fd9ErixAVEIrrVd0ntd0pv7x+hx99CkH21F9NO5kJ8T7IrTGzvPR43WoPfHpcTqemmBLTZiyFCfYiKdRexn256Y8t/T9fgyVBYPc0Ul0Evj262ohfjd08wxZ+1UdyjIdTYvd+W7j2kiO3SnKqpzHjtLqL8/e15e2m4VrRFhiDn59cgdZBCNivwWGX37tCsJRCmXh7JzpiOWPT+E+diT2K2lZmGKG6My3DZ7VAxjk89pWary4umUPD5JD4Za07xwXcZG0Qwzq/7jMIeoBL/NO0D2IQq6qc9ZC4S0UKkHWdM+QLopvwhzte5XRmXixZo5j/VD4gHnwCo4Hxlxl7DYYPPLRhZy+OvxG422b1MivXypZw8jo78wjwrUOz/IwUrunQf4dO42HRU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Kairui Song 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 == SWAP_MAP_SHMEM value is basically the same as having swap count == 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 817e427a47d2..0e52ac4e817d 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 */ /* Special value in each swap_map continuation */ #define SWAP_CONT_MAX 0x7f /* Max count */ @@ -477,8 +476,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); @@ -541,11 +539,7 @@ static inline int add_swap_count_continuation(swp_entry_t swp, gfp_t gfp_mask) return 0; } -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; } @@ -596,6 +590,11 @@ static inline int add_swap_extent(struct swap_info_struct *sis, } #endif /* CONFIG_SWAP */ +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 99327c30507c..972bd0eca439 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1641,7 +1641,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) if (!folio_alloc_swap(folio, __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN)) { shmem_recalc_inode(inode, 0, nr_pages); - 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)); mutex_unlock(&shmem_swaplist_mutex); diff --git a/mm/swapfile.c b/mm/swapfile.c index 34188714479f..6b115149b845 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -192,7 +192,7 @@ static bool swap_is_last_map(struct swap_info_struct *si, unsigned char *map_end = map + nr_pages; unsigned char count = *map; - if (swap_count(count) != 1 && swap_count(count) != SWAP_MAP_SHMEM) + if (swap_count(count) != 1) return false; while (++map < map_end) { @@ -1359,12 +1359,6 @@ static unsigned char swap_entry_put_locked(struct swap_info_struct *si, if (usage == SWAP_HAS_CACHE) { VM_BUG_ON(!has_cache); has_cache = 0; - } else if (count == SWAP_MAP_SHMEM) { - /* - * Or we could insist on shmem.c using a special - * swap_shmem_free() and free_shmem_swap_and_cache()... - */ - count = 0; } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { if (count == COUNT_CONTINUED) { if (swap_count_continued(si, offset, count)) @@ -1478,7 +1472,7 @@ static bool swap_entries_put_map(struct swap_info_struct *si, if (nr <= 1) goto fallback; count = swap_count(data_race(si->swap_map[offset])); - if (count != 1 && count != SWAP_MAP_SHMEM) + if (count != 1) goto fallback; ci = lock_cluster(si, offset); @@ -1533,12 +1527,10 @@ static bool swap_entries_put_map_nr(struct swap_info_struct *si, /* * Check if it's the last ref of swap entry in the freeing path. - * Qualified vlaue includes 1, SWAP_HAS_CACHE or SWAP_MAP_SHMEM. */ static inline bool __maybe_unused swap_is_last_ref(unsigned char count) { - return (count == SWAP_HAS_CACHE) || (count == 1) || - (count == SWAP_MAP_SHMEM); + return (count == SWAP_HAS_CACHE) || (count == 1); } /* @@ -3536,7 +3528,6 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) offset = swp_offset(entry); VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); - VM_WARN_ON(usage == 1 && nr > 1); ci = lock_cluster(si, offset); err = 0; @@ -3596,27 +3587,28 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) return err; } -/* - * 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 entries + * 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 required * but could not be atomically allocated. Returns 0, just as if it succeeded, * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which * might occur if a page table entry has got corrupted. + * + * Note that we are currently not handling the case where nr > 1 and we need 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 swap + * entry it owns. */ -int swap_duplicate(swp_entry_t entry) +int swap_duplicate_nr(swp_entry_t entry, int nr) { int err = 0; - while (!err && __swap_duplicate(entry, 1, 1) == -ENOMEM) + while (!err && __swap_duplicate(entry, 1, nr) == -ENOMEM) err = add_swap_count_continuation(entry, GFP_ATOMIC); return err; } -- 2.49.0