From patchwork Thu Mar 20 11:48:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kemeng Shi X-Patchwork-Id: 14023365 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 71004C36002 for ; Thu, 20 Mar 2025 02:53:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF53A280018; Wed, 19 Mar 2025 22:52:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFA0F280017; Wed, 19 Mar 2025 22:52:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6DFF728001B; Wed, 19 Mar 2025 22:52:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A372D280017 for ; Wed, 19 Mar 2025 22:52:58 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 745961A0A89 for ; Thu, 20 Mar 2025 02:52:58 +0000 (UTC) X-FDA: 83240407236.18.2AB2392 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by imf20.hostedemail.com (Postfix) with ESMTP id 7F8B01C0004 for ; Thu, 20 Mar 2025 02:52:54 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of shikemeng@huaweicloud.com designates 45.249.212.51 as permitted sender) smtp.mailfrom=shikemeng@huaweicloud.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1742439176; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nM4uR4rFJEQC1uocg9DTkYXzS83ZQGCogr8+3sWDvd0=; b=6G43onBpOxWNBEfeFhivFrAPDf+Y2FB2xXWDgb8U64RLfdRvqawBqyNygy6NBGzUY1Dy+o NHSgNXA3JsOdLO25b5AIGyjAjGn7jZaggqJMlveYZhQDtypip7WKqdYTqsvm0jIInCOOvG HX/Q6AxtKm4cskfTUNxgmdMq5YfSBVI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of shikemeng@huaweicloud.com designates 45.249.212.51 as permitted sender) smtp.mailfrom=shikemeng@huaweicloud.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1742439176; a=rsa-sha256; cv=none; b=hPALDv5lqPqICIutx6hfUt+QKUrT7PSBQWc412H//RMjaeCb0iZdDrf1YvPe5/WhcEJsdX PVoTlcYTFsJ57oFg9jVVZga1GzVKeiclPGHz6WkPX8TbpcP7+K+mpiQhZd7WbfSqOThSv1 UO5d1Wl1bdFuWRUHoCNRvaufcITjmv8= Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZJ99D2HqGz4f3jtP for ; Thu, 20 Mar 2025 10:52:32 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 9E49F1A0A22 for ; Thu, 20 Mar 2025 10:52:49 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP3 (Coremail) with SMTP id _Ch0CgAHW8X_gttnYGGmGw--.42197S4; Thu, 20 Mar 2025 10:52:49 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, tim.c.chen@linux.intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/8] mm: swap: enable swap_entry_range_free() to drop any kind of last ref Date: Thu, 20 Mar 2025 19:48:23 +0800 Message-Id: <20250320114829.25751-3-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250320114829.25751-1-shikemeng@huaweicloud.com> References: <20250320114829.25751-1-shikemeng@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: _Ch0CgAHW8X_gttnYGGmGw--.42197S4 X-Coremail-Antispam: 1UD129KBjvJXoWxWr1ktF18tF48GF4ruryDJrb_yoWrAFy7pF 9Igrn8Kr4xJF13K3y3Jw4Yyw4Sv3yxWw18ZF9rGw1Sv3ZxJry0qFyDA3yYkFyUA34kuF90 ka1qqr17GF4jyFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPFb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUXwA2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK 0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4 x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l 84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I 8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AK xVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2AFwI 0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUVveH DUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ X-Stat-Signature: wze7qdwjtarfyipja4du3mi9reh77591 X-Rspamd-Queue-Id: 7F8B01C0004 X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1742439174-104432 X-HE-Meta: U2FsdGVkX18uvcQxC9Pg4MhjsZbfIdcp2tJx3fG/ppRmR8KMghGS2ADkl1TMc1F6SNs9aKBCYR39JpTjzoJM3dLrEiHk6xgbL9ShZvsJenPax1i5CXRe/Ja7o9Gz2L04tglHbP1NFZuXKn5ZXDhu/SabvtXPuqa78AWip98qotvkp1oaGsduwivm00shAiAH+qmRRgkWMfyGkwqdr1KkZkcc+gObe5OsTlzLWNn8XqL7PtN5kTD954k6EZx55FI3h6TqAk9kG/vbnmyrSTHKBo0cEphg6IZ1eaiffhLoG/BVkCI7D7gYUOL13JBcY0OlpojaWhZFVdP0MnlLSpvNtWl/ugqUZAyNNPqhgC/OUZdbmgZMx3+Ye2FZZtq6teOVGLsFYIdTvtmW5aO1muXpSv6rgYb7ZMrGlWIb75wlHB83x8LzDl5FKpnk16nfaMdTs2Q7ff4JlP7g7nBkNT1mJpKzzssSuVWSlhnWZnUl0T0FZbf3tki0UBN0U1eOkAkv/NIuq4FZAKqnOTT67fAFokwD6XcHVmR8Irlifzi56HvOyq6N7MOEE9AnvOo8nGYCzxq/gyXbc7lwsF/5CqUNd5EcPSxRel4MFujdR/G1MGYxBlYjbpklXt8849owak3FLiXgkd4OLW1bnN+R0KiQlXDLmgvQ8FWGV8PMoAbUhnKnZVnG+9yTsjoQYcE43g8wBOeptwE45DVMOVsKt+8DAp/iPYguTzMhOvum5zkgXsKBpE6oRnRXR6UMCn2BC5kzSWIPUNrRxPdS1Atb6CmLOEkSaQDxYWzI4BcYGr/3VgbQVxseLkgW3UAEKjfq3PDSwZoncEz9yCRvtf7bjSTkmw6p82U402lBitswgsVgdghbTO99kg2ORVeS7lr7PM2QBYG7pQrCQOhnFhTf38BxcyyzhNs53mg65JCUjyXQsrWlBl2aevFMlddJK3Hl790dVrJYDSFMVIZJtzOP99O 0weCMqvI EgWrMRm57pHsDOjbMfezJ4x8tfcf7EPLR3A7ucsBppvE4LIZLscZF75P4IhdupD/tJG4PbvQOPlGbH+GS2GhaeU5ab+D6pXT+KxPwWZWjr0xX+3smxDtLrChx9c7gUhuEYxPZO0nSFUFtrRJX4Ha+sO5KLh0nXCtbYRJiaeHvo5shOZYu52BA9z461aSHhKZYX+tD2nb7nTGBv6+GB+9Cd+FStvgIvdiu0UN4cpKkW4D2Ji8xXNcjh1DQe9Z78LNW24n7vcx5/tRCI8k= 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: The original VM_BUG_ON only allows swap_entry_range_free() to drop last SWAP_HAS_CACHE ref. By allowing other kind of last ref in VM_BUG_ON, swap_entry_range_free() could be a more general-purpose function able to handle all kind of last ref. Following thi change, also rename swap_entry_range_free() to swap_entries_free() and update it's comment accordingly. This is a preparation to use swap_entries_free() to drop more kind of last ref other than SWAP_HAS_CACHE. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 5a775456e26c..76720ca76aae 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -52,9 +52,9 @@ static bool swap_count_continued(struct swap_info_struct *, pgoff_t, unsigned char); static void free_swap_count_continuations(struct swap_info_struct *); -static void swap_entry_range_free(struct swap_info_struct *si, - struct swap_cluster_info *ci, - swp_entry_t entry, unsigned int nr_pages); +static void swap_entries_free(struct swap_info_struct *si, + struct swap_cluster_info *ci, + swp_entry_t entry, unsigned int nr_pages); static void swap_range_alloc(struct swap_info_struct *si, unsigned int nr_entries); static bool folio_swapcache_freeable(struct folio *folio); @@ -1463,7 +1463,7 @@ static unsigned char swap_entry_put(struct swap_info_struct *si, ci = lock_cluster(si, offset); usage = swap_entry_put_locked(si, offset, 1); if (!usage) - swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); + swap_entries_free(si, ci, swp_entry(si->type, offset), 1); unlock_cluster(ci); return usage; @@ -1493,7 +1493,7 @@ static bool swap_entries_put_nr(struct swap_info_struct *si, for (i = 0; i < nr; i++) WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE); if (!has_cache) - swap_entry_range_free(si, ci, entry, nr); + swap_entries_free(si, ci, entry, nr); unlock_cluster(ci); return has_cache; @@ -1512,12 +1512,18 @@ static bool swap_entries_put_nr(struct swap_info_struct *si, } /* - * Drop the last HAS_CACHE flag of swap entries, caller have to - * ensure all entries belong to the same cgroup. + * Drop the last ref(1, SWAP_HAS_CACHE or SWAP_MAP_SHMEM) of swap entries, + * caller have to ensure all entries belong to the same cgroup and cluster. */ -static void swap_entry_range_free(struct swap_info_struct *si, - struct swap_cluster_info *ci, - swp_entry_t entry, unsigned int nr_pages) +static inline bool swap_is_last_ref(unsigned char count) +{ + return (count == SWAP_HAS_CACHE) || (count == 1) || + (count == SWAP_MAP_SHMEM); +} + +static void swap_entries_free(struct swap_info_struct *si, + struct swap_cluster_info *ci, + swp_entry_t entry, unsigned int nr_pages) { unsigned long offset = swp_offset(entry); unsigned char *map = si->swap_map + offset; @@ -1530,7 +1536,7 @@ static void swap_entry_range_free(struct swap_info_struct *si, ci->count -= nr_pages; do { - VM_BUG_ON(*map != SWAP_HAS_CACHE); + VM_BUG_ON(!swap_is_last_ref(*map)); *map = 0; } while (++map < map_end); @@ -1553,7 +1559,7 @@ static void cluster_swap_free_nr(struct swap_info_struct *si, ci = lock_cluster(si, offset); do { if (!swap_entry_put_locked(si, offset, usage)) - swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); + swap_entries_free(si, ci, swp_entry(si->type, offset), 1); } while (++offset < end); unlock_cluster(ci); } @@ -1596,11 +1602,11 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) ci = lock_cluster(si, offset); if (swap_only_has_cache(si, offset, size)) - swap_entry_range_free(si, ci, entry, size); + swap_entries_free(si, ci, entry, size); else { for (int i = 0; i < size; i++, entry.val++) { if (!swap_entry_put_locked(si, offset + i, SWAP_HAS_CACHE)) - swap_entry_range_free(si, ci, entry, 1); + swap_entries_free(si, ci, entry, 1); } } unlock_cluster(ci);