From patchwork Tue Mar 25 16:25:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kemeng Shi X-Patchwork-Id: 14028180 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 570F4C3600C for ; Tue, 25 Mar 2025 07:30:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E7AA280001; Tue, 25 Mar 2025 03:30:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 700B5280004; Tue, 25 Mar 2025 03:30:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38149280006; Tue, 25 Mar 2025 03:30:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0BA1D280004 for ; Tue, 25 Mar 2025 03:30:03 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E3EFC1CC7A1 for ; Tue, 25 Mar 2025 07:30:03 +0000 (UTC) X-FDA: 83259249486.25.0B33B31 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by imf17.hostedemail.com (Postfix) with ESMTP id 7D9BF40011 for ; Tue, 25 Mar 2025 07:30:01 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=none; spf=none (imf17.hostedemail.com: domain of shikemeng@huaweicloud.com has no SPF policy when checking 45.249.212.51) smtp.mailfrom=shikemeng@huaweicloud.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1742887802; a=rsa-sha256; cv=none; b=jSpHH2/7bMSPYUb9cTKBIHqPxydi69KmV3MY72oZHEvsWWzOK3cnvABGjnLThKRpmOibux MBuR0crw/CwQlr3kqea2tdf3cOYr/k1S8deK3WL4G0JR7acXYPfX5Uo2x0C6IB+2yBjI7z 1NioScqdOJoTFzaQ7FRPGv7kh/acq1U= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=none; spf=none (imf17.hostedemail.com: domain of shikemeng@huaweicloud.com has no SPF policy when checking 45.249.212.51) smtp.mailfrom=shikemeng@huaweicloud.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1742887802; 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=zny/eAhxiSEEY3GNbCL0Q/neb1ncUIDag20t2Toxa9M=; b=Jf3nXEUdIFdy0IrpOmxMldgxTeL/qujctCzDepxz+QUVibmrRbfq9gwRf0T06qcAYBj0+a 345tfzIHVvmli2bYEPiTpIjakiBlABJuKHT85q+8mqScqrODgv8Xss1HjICs/3LU7X1Nw/ M4fGfC/PNsvjRbYqLt5hkwFTZQsGPvU= Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZMM4W6HJdz4f3lgS for ; Tue, 25 Mar 2025 15:29:31 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 3BAFC1A0ACF for ; Tue, 25 Mar 2025 15:29:56 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgB3M2ZxW+JnZofaHQ--.14216S4; Tue, 25 Mar 2025 15:29:56 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, tim.c.chen@linux.intel.com, bhe@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/8] mm: swap: enable swap_entry_range_free() to drop any kind of last ref Date: Wed, 26 Mar 2025 00:25:22 +0800 Message-Id: <20250325162528.68385-3-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250325162528.68385-1-shikemeng@huaweicloud.com> References: <20250325162528.68385-1-shikemeng@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: Syh0CgB3M2ZxW+JnZofaHQ--.14216S4 X-Coremail-Antispam: 1UD129KBjvJXoWxWr1ktF18tF48GF4ruryDJrb_yoWrCr18pF 9Igrn8Kr4xJF17K3y3Jw4Yyw4Sv3yxWw10qF9rGw1Sv3ZxJry0qFyDA3yYkFyUA34kuF9I ka1qqr17GF4jyFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPFb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUXwA2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK 0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4 x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l 84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I 8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AK xVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2AFwI 0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUaknY DUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 7D9BF40011 X-Stat-Signature: 8xhfeehb98dfh1xhqccozryqtjifrfk3 X-HE-Tag: 1742887801-376574 X-HE-Meta: U2FsdGVkX18hIeqtAHV7qWCwFxUvOQ7LtTOCIvIDbhuIGpDRlP4aKfQPJuwvq3wIonhoDPHUyKDamdCEzI0MmVcW7JFtIGZS1VmkxqO7nDYjFSTjKWOuE0OOgH7HlPCfLC4FMWpZeCqdDn48jBFzkbGap+7fHD7lUj8jLwnsEuYUqw7K4uQMDBePVbW+15wSOgLsp3yfU7e7d2nXS/aI1MsrYHbPCYZhJqsLP4RtqlTySA10FVnMitk0CQgMU1KzKeJIwtvU45YIibqmubLOfdzl7VcajHxW7yrX8QwBTRL4uSmNF5lajNnrLCEjZKtI07MQ/Sl9f/0K0+lpRPyk0BGFHW2yWriz2BpAgu1t43jKt2950NTqFBQveiNR7m2q2AK0Q2DlEYrUmXcuuJzCJAuYUPubnToFYkO7fkI3Nl7ipRPSp544Y9eMY050t5r/pgp4fhoJTZy//Wdg04TNJLVt9ta74byUNsiRaUVxfXTqlmX5Dl8DGnFkDnVpFOYl35072go10ZfQvSrndtr3hKaysZxDlL+rP2OpOg7Tm/YYSldu7cn8PcXIFKFLT7mrdwCSfElNKJUL5ln5HtPhqxVN5xyeZOC/CKwkI8fUhaRHXUVtuxaCjksSX7tf06UucM4a0lB9HoDoSsJAuvMTUhddFV+W5ebB9YYEbLnBD6mtLKI7xossiEwC9OPBQEu7UelFNFFUnj/itC7PYe4aqiF/OWZNrzjxERhNSxk+iWAbGeSIqYCQLhRO8CK3ApASUURU2IF2Eh78R2JajBJxfrMAX3S75z9ExG7/P3XltEweVCgv/5PGYkLG7U66lyJ8kS2e2/bvkP7iGbcJ+c38VHSrDt4YkjelGXufarl/lL6XNA7qzbkb/OC7SRAHnTL0OGQmNUE94Q4TADfJWJ6sdvNenh0htXqcjr0ctoeWAYCVkwDJ9fXSYX3zdR3nLELf8mmNrBcY4OtZNBKh/2D UCvNi5PB XZM7SBPNwM0vNRE8RDESLo6XXb4r0T0M1qf5k17C2cxFX+zPXudDCmF0Zbc2/DBMd6sEW3UygQ5lsuKhMgppQ7H6LKvrHoH3qQR2mhvMQhePDC8lyCFYKd3pkxUYbYYsGG2r/jj2gw7VWr0rbK1NZO6Hx0wPbyZGuxdjBoB9PTjQehaJp478/lY/+ERTIz/qtV26YNq8X+FJLvrN6rgsmKdiLUxHZFRMIYQf0ORQjJ5qZks3KJy4HoxPCYA/Vt1JVLR0ZtJNJtp/NMSI3xszC/jda0nrSjTZtXhUJJScG7Ozo2C7lMjycxCFEWw== 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 Reviewed-by: Baoquan He --- 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);