From nobody Wed Dec 17 10:38:10 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC8BD1990C7 for ; Tue, 18 Mar 2025 06:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278238; cv=none; b=usGXEeLYUUY9796jtnBWEzBoL8TI5XBh/jmU1YWGk+6UOOegBS5f5qF7ecs+0RPls0phwq4njmss4GG6L+bjpVWHSUgrvejcZ6ZJSqhMEHTpakWiPQiXHnIWm82z44A2GSMkujdXvTJMUF/EfW/I7nN6c1LIz/y1v7m6pAoKqhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278238; c=relaxed/simple; bh=UwZytSL0yAwPGn0QNOeHia0F0VwHgcO4ocO5gsJlb4g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VuOxovs9gc9UGl0mdm+kkL+O2SKD+t0BzcSht/ThCqLkExEo+CNCaOVRg7kvPwrVi5RSy0mLE96s3KHiLz0l3QL+Dk6fPh8/IO72A7GwH+KSlcQyqrqsutL+BYLlnUEKuZjTRhQZHoTm7QiLCU0A+MiVlOn/6PB8x9puSmnVH7s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZH1f70XJbz4f3m6S for ; Tue, 18 Mar 2025 14:10:07 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 2DAF11A01A1 for ; Tue, 18 Mar 2025 14:10:31 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgD3AGRVDtln65ErGw--.37695S3; Tue, 18 Mar 2025 14:10:31 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: tim.c.chen@linux.intel.com, ryncsn@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/8] mm: swap: rename __swap_[entry/entries]_free[_locked] to swap_[entry/entries]_put[_locked] Date: Tue, 18 Mar 2025 23:06:07 +0800 Message-Id: <20250318150614.6415-2-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250318150614.6415-1-shikemeng@huaweicloud.com> References: <20250318150614.6415-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Syh0CgD3AGRVDtln65ErGw--.37695S3 X-Coremail-Antispam: 1UD129KBjvJXoWxZrWUKFW5Zw1DuF4rGF47CFg_yoWrKr45pF 9Igrn8KFs7Jr1xGws7Xw48Zr4Fvw4xW3W8tFy7Gw12vasxJryFqFyDC3y7KFyUCr95CF90 ya1Utry7GF4jqr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPFb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUGwA2048vs2IY020Ec7CjxVAFwI0_Jrv_JF4l8cAvFVAK 0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4 x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l 84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I 8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AK xVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2AFwI 0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUswZ2 DUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" In __swap_entry_free[_locked] and __swap_entries_free, we decrease count first and only free swap entry if count drops to zero. This behavior is more akin to a put() operation rather than a free() operation. Therefore, rename these functions with "put" instead of "free". Additionally, add "_nr" suffix to swap_entries_put to indicate the input range may span swap clusters. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 628f67974a7c..5a775456e26c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1347,9 +1347,9 @@ static struct swap_info_struct *_swap_info_get(swp_en= try_t entry) return NULL; } =20 -static unsigned char __swap_entry_free_locked(struct swap_info_struct *si, - unsigned long offset, - unsigned char usage) +static unsigned char swap_entry_put_locked(struct swap_info_struct *si, + unsigned long offset, + unsigned char usage) { unsigned char count; unsigned char has_cache; @@ -1453,15 +1453,15 @@ struct swap_info_struct *get_swap_device(swp_entry_= t entry) return NULL; } =20 -static unsigned char __swap_entry_free(struct swap_info_struct *si, - swp_entry_t entry) +static unsigned char swap_entry_put(struct swap_info_struct *si, + swp_entry_t entry) { struct swap_cluster_info *ci; unsigned long offset =3D swp_offset(entry); unsigned char usage; =20 ci =3D lock_cluster(si, offset); - usage =3D __swap_entry_free_locked(si, offset, 1); + usage =3D swap_entry_put_locked(si, offset, 1); if (!usage) swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); unlock_cluster(ci); @@ -1469,8 +1469,8 @@ static unsigned char __swap_entry_free(struct swap_in= fo_struct *si, return usage; } =20 -static bool __swap_entries_free(struct swap_info_struct *si, - swp_entry_t entry, int nr) +static bool swap_entries_put_nr(struct swap_info_struct *si, + swp_entry_t entry, int nr) { unsigned long offset =3D swp_offset(entry); unsigned int type =3D swp_type(entry); @@ -1501,7 +1501,7 @@ static bool __swap_entries_free(struct swap_info_stru= ct *si, fallback: for (i =3D 0; i < nr; i++) { if (data_race(si->swap_map[offset + i])) { - count =3D __swap_entry_free(si, swp_entry(type, offset + i)); + count =3D swap_entry_put(si, swp_entry(type, offset + i)); if (count =3D=3D SWAP_HAS_CACHE) has_cache =3D true; } else { @@ -1552,7 +1552,7 @@ static void cluster_swap_free_nr(struct swap_info_str= uct *si, =20 ci =3D lock_cluster(si, offset); do { - if (!__swap_entry_free_locked(si, offset, usage)) + if (!swap_entry_put_locked(si, offset, usage)) swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); } while (++offset < end); unlock_cluster(ci); @@ -1599,7 +1599,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t = entry) swap_entry_range_free(si, ci, entry, size); else { for (int i =3D 0; i < size; i++, entry.val++) { - if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) + if (!swap_entry_put_locked(si, offset + i, SWAP_HAS_CACHE)) swap_entry_range_free(si, ci, entry, 1); } } @@ -1798,7 +1798,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr) /* * First free all entries in the range. */ - any_only_cache =3D __swap_entries_free(si, entry, nr); + any_only_cache =3D swap_entries_put_nr(si, entry, nr); =20 /* * Short-circuit the below loop if none of the entries had their @@ -1811,7 +1811,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr) * Now go back over the range trying to reclaim the swap cache. This is * more efficient for large folios because we will only try to reclaim * the swap once per folio in the common case. If we do - * __swap_entry_free() and __try_to_reclaim_swap() in the same loop, the + * swap_entry_put() and __try_to_reclaim_swap() in the same loop, the * latter will get a reference and lock the folio for every individual * page but will only succeed once the swap slot for every subpage is * zero. @@ -3758,7 +3758,7 @@ int add_swap_count_continuation(swp_entry_t entry, gf= p_t gfp_mask) * into, carry if so, or else fail until a new continuation page is alloca= ted; * when the original swap_map count is decremented from 0 with continuatio= n, * borrow from the continuation and report whether it still holds more. - * Called while __swap_duplicate() or caller of __swap_entry_free_locked() + * Called while __swap_duplicate() or caller of swap_entry_put_locked() * holds cluster lock. */ static bool swap_count_continued(struct swap_info_struct *si, --=20 2.30.0 From nobody Wed Dec 17 10:38:10 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC8661537CB for ; Tue, 18 Mar 2025 06:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278236; cv=none; b=Kt4WVIBPypQCVFTw/l35GsGwqQFFD4UnYwTN9+jL3ECqzqSyuQW2voafKMsbW6uhBqt5mwEzDTUlJAi0G8UKshCvZsx4esbQaUQq0xE6TME7B9kmoT20N1WIHCglY6IcfzQK9BLYRh9fvnyNrPXLCyMvayE5vpDYpBR2bEctGjc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278236; c=relaxed/simple; bh=GUb99Lj/OwfZW0EwweWP6qWw+RN3IGEAx9Posv6ktC0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=r/SS0Dnqo7OYRJ138QwShX3hdfcju1ycU+/0WGyS7fOAgL5HijnCDNIbmSsD13x719KPlcKMUlJKBJ7cQpj2lY6H0KmmdBhHXYSg2yXdMlmktT1+/6x27BMowMeqWWnpKBFw9pz3RtK6bzaH7+wxGHdJsjEU0Yig6JSx69Wsrg0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZH1f72R9gz4f3m6n for ; Tue, 18 Mar 2025 14:10:07 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 6FAA51A01A1 for ; Tue, 18 Mar 2025 14:10:31 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgD3AGRVDtln65ErGw--.37695S4; Tue, 18 Mar 2025 14:10:31 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: tim.c.chen@linux.intel.com, ryncsn@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/8] mm: swap: remove unneeded VM_BUG_ON(*map != SWAP_HAS_CACHE) in swap_entry_range_free() Date: Tue, 18 Mar 2025 23:06:08 +0800 Message-Id: <20250318150614.6415-3-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250318150614.6415-1-shikemeng@huaweicloud.com> References: <20250318150614.6415-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Syh0CgD3AGRVDtln65ErGw--.37695S4 X-Coremail-Antispam: 1UD129KBjvJXoWxWr45tF15GrW8Gw1UJrW5Jrb_yoWrXFy5pF 9Igrn8Kr4xAF13K3y3Jw45tw4Sv3yxWw10gF9rGw1av3ZxJry0qFyDA3yjkFy5A3s5uasI k3WDtr17WF4jvFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 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/ Content-Type: text/plain; charset="utf-8" As all callers of swap_entry_range_free() have already ensured slots to be freed are marked as SWAP_HAS_CACHE while holding the cluster lock, the BUG_ON check can be safely removed. After this, the function swap_entry_range_free() could drop any kind of last flag, rename it to swap_entries_free() and update it's comment accordingly. This is a preparation to use swap_entries_free() to drop last ref count and SWAP_MAP_SHMEM flag. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 5a775456e26c..0aa7ce82c013 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 =3D lock_cluster(si, offset); usage =3D 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); =20 return usage; @@ -1493,7 +1493,7 @@ static bool swap_entries_put_nr(struct swap_info_stru= ct *si, for (i =3D 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); =20 return has_cache; @@ -1512,12 +1512,12 @@ static bool swap_entries_put_nr(struct swap_info_st= ruct *si, } =20 /* - * Drop the last HAS_CACHE flag of swap entries, caller have to - * ensure all entries belong to the same cgroup. + * Drop the last flag(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 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 =3D swp_offset(entry); unsigned char *map =3D si->swap_map + offset; @@ -1530,7 +1530,6 @@ static void swap_entry_range_free(struct swap_info_st= ruct *si, =20 ci->count -=3D nr_pages; do { - VM_BUG_ON(*map !=3D SWAP_HAS_CACHE); *map =3D 0; } while (++map < map_end); =20 @@ -1553,7 +1552,7 @@ static void cluster_swap_free_nr(struct swap_info_str= uct *si, ci =3D 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 +1595,11 @@ void put_swap_folio(struct folio *folio, swp_entry_= t entry) =20 ci =3D 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 =3D 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); --=20 2.30.0 From nobody Wed Dec 17 10:38:10 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCE5B1A08A8 for ; Tue, 18 Mar 2025 06:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278237; cv=none; b=MMwKq2jm8T1D9tyZzNbAWFXaLLx30VbIJQscqfiiSfODV66kCS/c30IrimgXUAvJY/mI82tsVJzmdcTZqyGAZfZNVR6CpVg8B/hSVPn2vKg7oY/obphaj8q7l2vd8MR1zKqUcmnZzYBWVhxdQlreXAR18oNt2SB8ja11WllIX7k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278237; c=relaxed/simple; bh=xQ2vLNUOITTjK87cbyOQnMEVkZyaNe8zjFSltjCWh1U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SJosqA2iTr/jiJXzmZQeOUZFfPUfMX7qlBUgQluog1xjpihnPGCDi/w+GoZeIFFmTJ0iE228qY/9OAfnesvfOV/kaTG17KIRJttFpAjOdbO4DR5QPq8VhMZo3x1bK6TW42tTSRbJYul1OsWtE1gx7B2ySwWdu3g62T1fT/tC55g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZH1f74GZ6z4f3m6t for ; Tue, 18 Mar 2025 14:10:07 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id AE4271A01A1 for ; Tue, 18 Mar 2025 14:10:31 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgD3AGRVDtln65ErGw--.37695S5; Tue, 18 Mar 2025 14:10:31 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: tim.c.chen@linux.intel.com, ryncsn@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/8] mm: swap: use swap_entries_free() to free swap entry in swap_entry_put_locked() Date: Tue, 18 Mar 2025 23:06:09 +0800 Message-Id: <20250318150614.6415-4-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250318150614.6415-1-shikemeng@huaweicloud.com> References: <20250318150614.6415-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Syh0CgD3AGRVDtln65ErGw--.37695S5 X-Coremail-Antispam: 1UD129KBjvJXoWxZF1DKr13Gry5JF48Ar1fWFg_yoW5XFW7pF ZIgrs8tF4xAr17Kw45Aw45ZayFvw4xGw1jgF9rGw13ZasxJryFgFyqy3y2gFyUA3s5uas8 A3WUtr17uF4jyFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPFb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUWwA2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK 0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4 x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l 84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I 8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AK xVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2AFwI 0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUaTKu UUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" In swap_entry_put_locked(), we will set slot to SWAP_HAS_CACHE before using swap_entries_free() to do actual swap entry freeing. This introduce an unnecessary intermediate state. By using swap_entries_free() in swap_entry_put_locked(), we can eliminate the need to set slot to SWAP_HAS_CACHE. This change would make the behavior of swap_entry_put_locked() more consistent with other put() operations which will do actual free work after put last reference. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen Reviewed-by: Kairui Song --- mm/swapfile.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 0aa7ce82c013..40e41e514813 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1348,9 +1348,11 @@ static struct swap_info_struct *_swap_info_get(swp_e= ntry_t entry) } =20 static unsigned char swap_entry_put_locked(struct swap_info_struct *si, - unsigned long offset, + struct swap_cluster_info *ci, + swp_entry_t entry, unsigned char usage) { + unsigned long offset =3D swp_offset(entry); unsigned char count; unsigned char has_cache; =20 @@ -1382,7 +1384,7 @@ static unsigned char swap_entry_put_locked(struct swa= p_info_struct *si, if (usage) WRITE_ONCE(si->swap_map[offset], usage); else - WRITE_ONCE(si->swap_map[offset], SWAP_HAS_CACHE); + swap_entries_free(si, ci, entry, 1); =20 return usage; } @@ -1461,9 +1463,7 @@ static unsigned char swap_entry_put(struct swap_info_= struct *si, unsigned char usage; =20 ci =3D lock_cluster(si, offset); - usage =3D swap_entry_put_locked(si, offset, 1); - if (!usage) - swap_entries_free(si, ci, swp_entry(si->type, offset), 1); + usage =3D swap_entry_put_locked(si, ci, entry, 1); unlock_cluster(ci); =20 return usage; @@ -1551,8 +1551,8 @@ static void cluster_swap_free_nr(struct swap_info_str= uct *si, =20 ci =3D lock_cluster(si, offset); do { - if (!swap_entry_put_locked(si, offset, usage)) - swap_entries_free(si, ci, swp_entry(si->type, offset), 1); + swap_entry_put_locked(si, ci, swp_entry(si->type, offset), + usage); } while (++offset < end); unlock_cluster(ci); } @@ -1596,12 +1596,9 @@ void put_swap_folio(struct folio *folio, swp_entry_t= entry) ci =3D lock_cluster(si, offset); if (swap_only_has_cache(si, offset, size)) swap_entries_free(si, ci, entry, size); - else { - for (int i =3D 0; i < size; i++, entry.val++) { - if (!swap_entry_put_locked(si, offset + i, SWAP_HAS_CACHE)) - swap_entries_free(si, ci, entry, 1); - } - } + else + for (int i =3D 0; i < size; i++, entry.val++) + swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE); unlock_cluster(ci); } =20 --=20 2.30.0 From nobody Wed Dec 17 10:38:10 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCE041991CF for ; Tue, 18 Mar 2025 06:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278236; cv=none; b=pZpzw0KWMkjY8ptuPV+UtC4ORSNJhUhy80DRkMS6ZtU/M9uTe+Ni9ugL2v4fIkpsppNZ8yv1QhO3fK/pZF9DHtOjERvsW8SboE/Jyzt3EhPrHe0//fqbut3Tzy/iNyVoMbJAIuEiCYY4Cv8BQNbZ9sp1tLKX3mA9IuWhl1xSC4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278236; c=relaxed/simple; bh=TosNBFGLYtXUl6+FGOOEqH0M2hgTEMSlWtRjBRA5XWs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e25sj0y85PncM8MP2cZuEN+8WqYY3T/clYlrYbcistPJiosU+5kKR7N0TzZ/L1z0ILadPl/EbYopLFiJjFij6xUtfsx78GI0CELChQCOEVivACnR/NKYh4rLO28d/EHHXP3WrBNyE2L1hyhzc+FYsHE4N1XtevDRuxWrmT9Mb/o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZH1fG5PMKz4f3jZd for ; Tue, 18 Mar 2025 14:10:14 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 097371A06DC for ; Tue, 18 Mar 2025 14:10:32 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgD3AGRVDtln65ErGw--.37695S6; Tue, 18 Mar 2025 14:10:31 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: tim.c.chen@linux.intel.com, ryncsn@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/8] mm: swap: use swap_entries_free() drop last ref count in swap_entries_put_nr() Date: Tue, 18 Mar 2025 23:06:10 +0800 Message-Id: <20250318150614.6415-5-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250318150614.6415-1-shikemeng@huaweicloud.com> References: <20250318150614.6415-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Syh0CgD3AGRVDtln65ErGw--.37695S6 X-Coremail-Antispam: 1UD129KBjvdXoWrKrWUWrWrAr4fuw4Utw43ZFb_yoWfuFb_ua 9Yy34kCw47Jr1DGw12yr1jvr9Yga1qyF1rZw1ftFWayF1rJF95ZwsFgr98Aw10vw40v3s3 A3Zaqrn5Ar1DujkaLaAFLSUrUUUUjb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbqkYFVCjjxCrM7AC8VAFwI0_Wr0E3s1l1xkIjI8I6I8E6xAIw20E Y4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l87I20VAvwVAaII0Ic2I_JFv_Gryl82 xGYIkIc2x26280x7IE14v26r126s0DM28IrcIa0xkI8VCY1x0267AKxVW8JVW5JwA2ocxC 64kIII0Yj41l84x0c7CEw4AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26F1j6w1UM2 8EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq 3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8w Aqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE 14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCY1x0262kKe7 AKxVWUAVWUtwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02 F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw 1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7Cj xVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r 1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jv Wl9UUUUU= X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" Use swap_entries_free() to directly free swap entries when the swap entries are not cached and referenced, without needing to set swap entries to set intermediate SWAP_HAS_CACHE state. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 40e41e514813..2a08d9e89f90 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1490,10 +1490,11 @@ static bool swap_entries_put_nr(struct swap_info_st= ruct *si, unlock_cluster(ci); goto fallback; } - for (i =3D 0; i < nr; i++) - WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE); if (!has_cache) swap_entries_free(si, ci, entry, nr); + else + for (i =3D 0; i < nr; i++) + WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE); unlock_cluster(ci); =20 return has_cache; --=20 2.30.0 From nobody Wed Dec 17 10:38:10 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 737041A5B98 for ; Tue, 18 Mar 2025 06:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278237; cv=none; b=VoC2FV2LLpekKXKZ8m8XrF00LEihVWDsZiQACxRRPIy8FcPRmMrlExiWj9DnA4kYpXZCbz0wAWTv3ky7XNLpKbi09/4DuJspTbZBYZqtpCHKbQUJDIaW2kImdVPiVYZMxS0dMJI8+zqizkZWxtVYvpLOWr7O2C06dcekNiaNJyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278237; c=relaxed/simple; bh=QaZ8ojyWyVzoLJwRo0NCl6n42OFW5+5ek5WcDeAgOw0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XJe1KOLFK2M4p3kcL0lajAJP9VbKLHwQSd0X/Vy9meQCu+PQsi5i38dVefc1eTEUM4wSLiEntvaMzEn+M3WmFuyhnykR0SNmDbJ2t8pWYIkFMOJ82w/ZzFzqqDvMY2zHhElQb20a3mkNlRikDrljLxrFboCEi/QDcMsZaLBfb6g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZH1fH0PThz4f3jtW for ; Tue, 18 Mar 2025 14:10:15 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 51F5B1A0CC7 for ; Tue, 18 Mar 2025 14:10:32 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgD3AGRVDtln65ErGw--.37695S7; Tue, 18 Mar 2025 14:10:32 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: tim.c.chen@linux.intel.com, ryncsn@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 5/8] mm: swap: drop last SWAP_MAP_SHMEM flag in batch in swap_entries_put_nr() Date: Tue, 18 Mar 2025 23:06:11 +0800 Message-Id: <20250318150614.6415-6-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250318150614.6415-1-shikemeng@huaweicloud.com> References: <20250318150614.6415-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Syh0CgD3AGRVDtln65ErGw--.37695S7 X-Coremail-Antispam: 1UD129KBjvJXoWrKF1Dtry5ur18tryxAw1kKrg_yoW8Jr18pF WFg34DKF48tr12yFWUA3Z8CryYgr4vkF1Yqr9rG34fA34Dtw45u34vyayrAF1Utrn5CFyY g3ZFgryxuFWqvrUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAv FVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJw A2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE 3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr2 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2 AFwI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAq x4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r 1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF 7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxV WUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU s3kuDUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" The SWAP_MAP_SHMEM indicates last map from shmem. Therefore we can drop SWAP_MAP_SHMEM in batch in similar way to drop last ref count in batch. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 2a08d9e89f90..134b061ef1ae 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -192,7 +192,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) + if (swap_count(count) !=3D 1 && swap_count(count) !=3D SWAP_MAP_SHMEM) return false; =20 while (++map < map_end) { @@ -1479,7 +1479,10 @@ static bool swap_entries_put_nr(struct swap_info_str= uct *si, unsigned char count; int i; =20 - if (nr <=3D 1 || swap_count(data_race(si->swap_map[offset])) !=3D 1) + 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) goto fallback; /* cross into another cluster */ if (nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER) --=20 2.30.0 From nobody Wed Dec 17 10:38:10 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 736A81A316D for ; Tue, 18 Mar 2025 06:10:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278238; cv=none; b=KZtOw5HlReWjNMS4t2VmU53ooJnqYHzkg2MsiTm8aJzie60bjl+P701jktpfWTJ18I3h/bjTOAFWBaAROofVhekQYjowjBD3gbh/cMmZfNP83OM184FDLioxnius5eDw1+t+rZ08KMV7oAYgmLWyyaoNbLYh5tQTVW1KXalQuZE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278238; c=relaxed/simple; bh=oN4/gIUYRrJ0tAMjk4oEuWB/HAh/Xcy6TWD5Z2oTMUE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Udwh/iClzL2jlcRXS/PFS2Zj7lZMv8v61fiy3LA1NwWn32ElgWgDFzz3zdsBopNyl51LwMr62r2owksxun9CrW20BFz+zGDeKKpLZ9drCtYlE+n4GydzbJAC1JMDSDQ4FS0YUGi0OmBB6PsyI67wxpwpy49PekEO/gAZyd+qdcs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZH1fH2LcXz4f3jkw for ; Tue, 18 Mar 2025 14:10:15 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 9468B1A125B for ; Tue, 18 Mar 2025 14:10:32 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgD3AGRVDtln65ErGw--.37695S8; Tue, 18 Mar 2025 14:10:32 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: tim.c.chen@linux.intel.com, ryncsn@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 6/8] mm: swap: free each cluster individually in swap_entries_put_map_nr() Date: Tue, 18 Mar 2025 23:06:12 +0800 Message-Id: <20250318150614.6415-7-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250318150614.6415-1-shikemeng@huaweicloud.com> References: <20250318150614.6415-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Syh0CgD3AGRVDtln65ErGw--.37695S8 X-Coremail-Antispam: 1UD129KBjvJXoWxGry5Jw13Kr4xGr48CF17Wrg_yoWruFyrpF yagrn8Krs7Xr43Jr4xJw4DZrWrZ3yvgF1Uta47Gr1SywnxJr1rWFyvy3ySgFyUC34kur90 y3W7K347uFs0qr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAv FVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJw A2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE 3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr2 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2 AFwI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAq x4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r 1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF 7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxV WUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU s3kuDUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" 1. Factor out general swap_entries_put_map() helper to drop entries belong to one cluster. If entries are last map, free entries in batch, otherwise put entries with cluster lock acquired and released only once. 2. Iterate and call swap_entries_put_map() for each cluster in swap_entries_put_nr() to leverage batch-remove for last map belong to one cluster and reduce lock acquire/release in fallback case. 3. As swap_entries_put_nr() won't handle SWAP_HSA_CACHE drop, rename it to swap_entries_put_map_nr(). 4. As we won't drop each entry invidually with swap_entry_put() now, do reclaim in free_swap_and_cache_nr() is because swap_entries_put_map_nr() is general routine to drop reference and the relcaim work should only be done in free_swap_and_cache_nr(). Remove stale comment accordingly. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 70 +++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 134b061ef1ae..370509eb2f1f 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1455,25 +1455,10 @@ struct swap_info_struct *get_swap_device(swp_entry_= t entry) return NULL; } =20 -static unsigned char swap_entry_put(struct swap_info_struct *si, - swp_entry_t entry) -{ - struct swap_cluster_info *ci; - unsigned long offset =3D swp_offset(entry); - unsigned char usage; - - ci =3D lock_cluster(si, offset); - usage =3D swap_entry_put_locked(si, ci, entry, 1); - unlock_cluster(ci); - - return usage; -} - -static bool swap_entries_put_nr(struct swap_info_struct *si, - swp_entry_t entry, int nr) +static bool swap_entries_put_map(struct swap_info_struct *si, + swp_entry_t entry, int nr) { unsigned long offset =3D swp_offset(entry); - unsigned int type =3D swp_type(entry); struct swap_cluster_info *ci; bool has_cache =3D false; unsigned char count; @@ -1484,14 +1469,10 @@ static bool swap_entries_put_nr(struct swap_info_st= ruct *si, count =3D swap_count(data_race(si->swap_map[offset])); if (count !=3D 1 && count !=3D SWAP_MAP_SHMEM) goto fallback; - /* cross into another cluster */ - if (nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER) - goto fallback; =20 ci =3D lock_cluster(si, offset); if (!swap_is_last_map(si, offset, nr, &has_cache)) { - unlock_cluster(ci); - goto fallback; + goto locked_fallback; } if (!has_cache) swap_entries_free(si, ci, entry, nr); @@ -1503,15 +1484,34 @@ static bool swap_entries_put_nr(struct swap_info_st= ruct *si, return has_cache; =20 fallback: - for (i =3D 0; i < nr; i++) { - if (data_race(si->swap_map[offset + i])) { - count =3D swap_entry_put(si, swp_entry(type, offset + i)); - if (count =3D=3D SWAP_HAS_CACHE) - has_cache =3D true; - } else { - WARN_ON_ONCE(1); - } + ci =3D lock_cluster(si, offset); +locked_fallback: + for (i =3D 0; i < nr; i++, entry.val++) { + count =3D swap_entry_put_locked(si, ci, entry, 1); + if (count =3D=3D SWAP_HAS_CACHE) + has_cache =3D true; + } + unlock_cluster(ci); + return has_cache; + +} + +static bool swap_entries_put_map_nr(struct swap_info_struct *si, + swp_entry_t entry, int nr) +{ + int cluster_nr, cluster_rest; + unsigned long offset =3D swp_offset(entry); + bool has_cache =3D false; + + cluster_rest =3D SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER; + while (nr) { + cluster_nr =3D min(nr, cluster_rest); + has_cache |=3D swap_entries_put_map(si, entry, cluster_nr); + cluster_rest =3D SWAPFILE_CLUSTER; + nr -=3D cluster_nr; + entry.val +=3D cluster_nr; } + return has_cache; } =20 @@ -1798,7 +1798,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr) /* * First free all entries in the range. */ - any_only_cache =3D swap_entries_put_nr(si, entry, nr); + any_only_cache =3D swap_entries_put_map_nr(si, entry, nr); =20 /* * Short-circuit the below loop if none of the entries had their @@ -1808,13 +1808,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int n= r) goto out; =20 /* - * Now go back over the range trying to reclaim the swap cache. This is - * more efficient for large folios because we will only try to reclaim - * the swap once per folio in the common case. If we do - * swap_entry_put() and __try_to_reclaim_swap() in the same loop, the - * latter will get a reference and lock the folio for every individual - * page but will only succeed once the swap slot for every subpage is - * zero. + * Now go back over the range trying to reclaim the swap cache. */ for (offset =3D start_offset; offset < end_offset; offset +=3D nr) { nr =3D 1; --=20 2.30.0 From nobody Wed Dec 17 10:38:10 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF8431A8F61 for ; Tue, 18 Mar 2025 06:10:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278237; cv=none; b=f8eknoM2kpKrJLMhbJurSR5ZW0voSOaLiJdGptG2ZqDT2YDHtJ1bLowuoEkNN4t2LC2A/YdxpbWbwFScF9OSLf3/Y9mMCfOe2FgdnhR30MfyTA55qp4kT0H/zA+LEallK5VDsVNM01OYKKERKSbguiLJ6KeQowwCs28Ji4iNI7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278237; c=relaxed/simple; bh=oqna5Rz45Q4X3GnawdekU8NIig5uLs/LkUqyBRC0S9Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V0LHLfTgATizeTUmvYaiUUxBcgF7WRnW53MTCudDATJBW1Sxg6JMP6mSnQu+9QUP3B63m2h6LuVDJrz/oLD0Pc8PAv4hrIfmyjWm1+bIewZeOw2BjJCLj1gE1x0ussocsn9JSjJFkFvGM+WmgojCu2MgxPjVwc72dJF3cmHR1Kg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4ZH1f85Kd0z4f3m6S for ; Tue, 18 Mar 2025 14:10:08 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id D37C31A1BCD for ; Tue, 18 Mar 2025 14:10:32 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgD3AGRVDtln65ErGw--.37695S9; Tue, 18 Mar 2025 14:10:32 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: tim.c.chen@linux.intel.com, ryncsn@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 7/8] mm: swap: factor out helper to drop cache of entries within a single cluster Date: Tue, 18 Mar 2025 23:06:13 +0800 Message-Id: <20250318150614.6415-8-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250318150614.6415-1-shikemeng@huaweicloud.com> References: <20250318150614.6415-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Syh0CgD3AGRVDtln65ErGw--.37695S9 X-Coremail-Antispam: 1UD129KBjvJXoW7uFWkAFy8JF13WF4UXFy7Wrg_yoW8Zr43pF 9Igwn8KF48Xr13Gw4xXw45JaySv3yxWw1UXFy7Gw13Z3Z3Jr10gFyqyrWavry5Crykur98 Ca4ktry2gF1jyF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAv FVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJw A2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE 3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr2 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2 AFwI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAq x4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r 1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF 7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxV WUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU s3kuDUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" Factor out helper swap_entries_put_cache() from put_swap_folio() to server as a general-purpose routine for dropping cache flag of entries within a single cluster. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 370509eb2f1f..919daaa3922a 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1455,6 +1455,21 @@ struct swap_info_struct *get_swap_device(swp_entry_t= entry) return NULL; } =20 +static void swap_entries_put_cache(struct swap_info_struct *si, + swp_entry_t entry, int nr) +{ + unsigned long offset =3D swp_offset(entry); + struct swap_cluster_info *ci; + + ci =3D lock_cluster(si, offset); + if (swap_only_has_cache(si, offset, nr)) + swap_entries_free(si, ci, entry, nr); + else + for (int i =3D 0; i < nr; i++, entry.val++) + swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE); + unlock_cluster(ci); +} + static bool swap_entries_put_map(struct swap_info_struct *si, swp_entry_t entry, int nr) { @@ -1588,8 +1603,6 @@ void swap_free_nr(swp_entry_t entry, int nr_pages) */ void put_swap_folio(struct folio *folio, swp_entry_t entry) { - unsigned long offset =3D swp_offset(entry); - struct swap_cluster_info *ci; struct swap_info_struct *si; int size =3D 1 << swap_entry_order(folio_order(folio)); =20 @@ -1597,13 +1610,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t= entry) if (!si) return; =20 - ci =3D lock_cluster(si, offset); - if (swap_only_has_cache(si, offset, size)) - swap_entries_free(si, ci, entry, size); - else - for (int i =3D 0; i < size; i++, entry.val++) - swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE); - unlock_cluster(ci); + swap_entries_put_cache(si, entry, size); } =20 int __swap_count(swp_entry_t entry) --=20 2.30.0 From nobody Wed Dec 17 10:38:10 2025 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 234A51DED4E for ; Tue, 18 Mar 2025 06:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278240; cv=none; b=CtPjoupo3MVsDwt4UvsnnAcDzOYt2OMMFwB1F3MwjjnlBdrrw6VsF4W4JmVqt5xn+/UWD84g2p1Rghu2CWscq+UbpPFdXPGz5Bd2VFdX+nGkRFuNEbJ15bzQOG0qr4evu4ZmxTGSrGEt7+80P1quUx9jwRp+uF8/HZXUXoE1yOQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742278240; c=relaxed/simple; bh=WuvlYnrWWxu7g30G6TS0IykBZnJ6thjOo/dMA2gvvaE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GWbW3/VKviHKShLevnQCTtx171QuViXKNeiJ1yltSUXWz8Ul87k3oB27diK71a15aXjT/Rjyx8s88b+li+FvfBy7oN1IFNDwXNH0HdITWk8mbFK/IVbd/HBfJ+p4y0Nx2BKedPdBtkGDpUibFSPTi0kLtonDFNkBQTDjb0tJ0q0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4ZH1fB3fqdz4f3khS for ; Tue, 18 Mar 2025 14:10:10 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 233171A1265 for ; Tue, 18 Mar 2025 14:10:33 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgD3AGRVDtln65ErGw--.37695S10; Tue, 18 Mar 2025 14:10:32 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: tim.c.chen@linux.intel.com, ryncsn@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 8/8] mm: swap: replace cluster_swap_free_nr() with swap_entries_put_[map/cache]() Date: Tue, 18 Mar 2025 23:06:14 +0800 Message-Id: <20250318150614.6415-9-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250318150614.6415-1-shikemeng@huaweicloud.com> References: <20250318150614.6415-1-shikemeng@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: Syh0CgD3AGRVDtln65ErGw--.37695S10 X-Coremail-Antispam: 1UD129KBjvJXoWxCF4UZF4rAFy8Zw1fGF4fuFg_yoW5Xr45pF 9agrn8Kr4xJr1xJr4xAw4DZryav397W3WUJFy7Ww1rZasrKr92qF1vyrWxCry5G34kurZI k3Zrtr9ruFs0yr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAv FVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJw A2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE 3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr2 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2 AFwI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAq x4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r 1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF 7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxV W8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU s3kuDUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" Replace cluster_swap_free_nr() with swap_entries_put_[map/cache]() to remove repeat code and leverage batch-remove for entries with last flag. After removing cluster_swap_free_nr, only functions with "_nr" suffix could free entries spanning cross clusters. Add corresponding description in comment of swap_entries_put_map_nr() as is first function with "_nr" suffix and have a non-suffix variant function swap_entries_put_map(). Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 919daaa3922a..7f3e9ce998f9 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1511,6 +1511,11 @@ static bool swap_entries_put_map(struct swap_info_st= ruct *si, =20 } =20 +/* + * Only functions with "_nr" suffix are able to free entries spanning + * cross multi clusters, so ensure the range is within a single cluster + * when freeing entries with functions without "_nr" suffix. + */ static bool swap_entries_put_map_nr(struct swap_info_struct *si, swp_entry_t entry, int nr) { @@ -1561,21 +1566,6 @@ static void swap_entries_free(struct swap_info_struc= t *si, partial_free_cluster(si, ci); } =20 -static void cluster_swap_free_nr(struct swap_info_struct *si, - unsigned long offset, int nr_pages, - unsigned char usage) -{ - struct swap_cluster_info *ci; - unsigned long end =3D offset + nr_pages; - - ci =3D lock_cluster(si, offset); - do { - swap_entry_put_locked(si, ci, swp_entry(si->type, offset), - usage); - } while (++offset < end); - unlock_cluster(ci); -} - /* * Caller has made sure that the swap device corresponding to entry * is still around or has not been recycled. @@ -1592,7 +1582,7 @@ void swap_free_nr(swp_entry_t entry, int nr_pages) =20 while (nr_pages) { nr =3D min_t(int, nr_pages, SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER= ); - cluster_swap_free_nr(sis, offset, nr, 1); + swap_entries_put_map(sis, swp_entry(sis->type, offset), nr); offset +=3D nr; nr_pages -=3D nr; } @@ -3615,11 +3605,13 @@ int swapcache_prepare(swp_entry_t entry, int nr) return __swap_duplicate(entry, SWAP_HAS_CACHE, nr); } =20 +/* + * Caller should ensure entries belong to the same folio so + * the entries won't span cross cluster boundary. + */ void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int n= r) { - unsigned long offset =3D swp_offset(entry); - - cluster_swap_free_nr(si, offset, nr, SWAP_HAS_CACHE); + swap_entries_put_cache(si, entry, nr); } =20 struct swap_info_struct *swp_swap_info(swp_entry_t entry) --=20 2.30.0