From nobody Wed Dec 17 23:25:08 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 7C9FA54654 for ; Thu, 13 Mar 2025 12:09:27 +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=1741867770; cv=none; b=eKsA1aeW1K0vNTn0lDWcdtwFmABIYRxRPd5KoO7gnvsi/CCSiFt+FsQPys+eQt8MO5u1qk5ZckV8GKthZxF/dhGGxgtTJ0aJiXLsV3nAt3DAf8e0dmJ97HiObNGAyrVfy/kwtaWniBdLxzKKHGOpvLLbMuJOIwXsr/C4oEA+tVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867770; c=relaxed/simple; bh=K+C6Thnd1fMiMEXaduaPw+rWEgDJbOdEG0FCKmBb8/4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GwL9ru2hUHvShx6SSr3etmQVMbb4mmy6NNcDJfWlurotFCs6S2wCbA/jWUItBEQS8xeGWbiJPXySZejmDkrzc0thd7arF9pmS8W4vHohRy03LmeULJos8HBFWya8fKq83OPFUK8udCXn6gOGi8+KN2FQfyFlWbapeyYHYh1B4yQ= 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 4ZD5rX4FnBz4f3m7J for ; Thu, 13 Mar 2025 20:09:00 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 7BC901A1199 for ; Thu, 13 Mar 2025 20:09:24 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S3; Thu, 13 Mar 2025 20:09:24 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/9] mm: swap: rename __swap_[entry/entries]_free[_locked] to swap_[entry/entries]_put[_locked] Date: Fri, 14 Mar 2025 05:05:07 +0800 Message-Id: <20250313210515.9920-2-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S3 X-Coremail-Antispam: 1UD129KBjvJXoWxZrWUKFW5Zw1DuF4rGF47CFg_yoWrtFyxpF 9Igrn8KFs7Xr1fGw4xXw48ZrWFvw4xWw18tFy7Gw1avasxJryFqFyDC3y7KFyUCr95Cr90 ya1Utry7GF4jvF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBjb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUGwA2048vs2IY020Ec7CjxVAFwI0_Jrv_JF4l8cAvFVAK 0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4 x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l 84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I 8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AK xVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64 vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK 8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I 0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07js2-5UUUUU= 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 23:25:08 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 D08CB1EF084 for ; Thu, 13 Mar 2025 12:09:27 +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=1741867769; cv=none; b=Yv/PMz70nTDwnPflq9AMuq3tJgtFscOf79xJ6X7NVNzWye884WpvEE+dWUmw61xOdZJdtnMWd+3QMmXfakF3YiM2mltsecbWqZRouiOwXjWbFWDfB/5BV1slcivuyO+WrxAif5jhvs0anCb+5crUGxhzgzh0gUi1lv24K/dJjQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867769; c=relaxed/simple; bh=Watlu32mQEUr0EHeSbFRfUzpvuA9/fTueq606bQNYaI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=K3KZLPXWTSr1G2d8xQbgSUc+85hzWvOPwh76+SXIrk7RLOwPK4WTD578h74HGoNu6i210jTsRN7YuA2I8sBHW4B/zA/hqr8ni4UI/t1bJkdnOz42DbS7HzF2isFCXKsN4DyVzUv0tSaExx1tFIuN9Fu5rtjHM2RaX+dJo7nam/8= 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 4ZD5rX677Tz4f3mHb for ; Thu, 13 Mar 2025 20:09:00 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id BA97D1A1614 for ; Thu, 13 Mar 2025 20:09:24 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S4; Thu, 13 Mar 2025 20:09:24 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/9] mm: swap: factor out the actual swap entry freeing logic to new helper Date: Fri, 14 Mar 2025 05:05:08 +0800 Message-Id: <20250313210515.9920-3-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S4 X-Coremail-Antispam: 1UD129KBjvJXoW7AF13JF4UXrykCF4DJF17GFg_yoW8Zry5pF nIgrn8Ga1xJr93GFZxAFn8Z34akw4Fg3W8WF9rGwnayas3JryIqFnrArWYkry5Ja4ku34a 9Fs8Kr17uF4YyFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB0b4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUXwA2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK 0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4 x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l 84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I 8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AK xVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64 vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK 8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I 0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUsmiiDUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" Factor out the actual swap entry freeing logic to new helper __swap_entries_free(). This allow us to futher simplify other swap entry freeing code by leveraging __swap_entries_free() helper function. Signed-off-by: Kemeng Shi --- mm/swapfile.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 5a775456e26c..7c886f9dd6f9 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1347,6 +1347,25 @@ static struct swap_info_struct *_swap_info_get(swp_e= ntry_t entry) return NULL; } =20 +static inline 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); + + VM_BUG_ON(cluster_is_empty(ci)); + VM_BUG_ON(ci->count < nr_pages); + + ci->count -=3D nr_pages; + mem_cgroup_uncharge_swap(entry, nr_pages); + swap_range_free(si, offset, nr_pages); + + if (!ci->count) + free_cluster(si, ci); + else + partial_free_cluster(si, ci); +} + static unsigned char swap_entry_put_locked(struct swap_info_struct *si, unsigned long offset, unsigned char usage) @@ -1525,22 +1544,13 @@ static void swap_entry_range_free(struct swap_info_= struct *si, =20 /* It should never free entries across different clusters */ VM_BUG_ON(ci !=3D offset_to_cluster(si, offset + nr_pages - 1)); - VM_BUG_ON(cluster_is_empty(ci)); - VM_BUG_ON(ci->count < nr_pages); =20 - ci->count -=3D nr_pages; do { VM_BUG_ON(*map !=3D SWAP_HAS_CACHE); *map =3D 0; } while (++map < map_end); =20 - mem_cgroup_uncharge_swap(entry, nr_pages); - swap_range_free(si, offset, nr_pages); - - if (!ci->count) - free_cluster(si, ci); - else - partial_free_cluster(si, ci); + __swap_entries_free(si, ci, entry, nr_pages); } =20 static void cluster_swap_free_nr(struct swap_info_struct *si, --=20 2.30.0 From nobody Wed Dec 17 23:25:08 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 0A30E1FAC51 for ; Thu, 13 Mar 2025 12:09:27 +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=1741867770; cv=none; b=jhRT3ANIdOYwdpmT9SFTM+Jo9ATcR5sjDKMERh1WW0yCYW+043ngIEhHybRNiTru3Rt/pUpaTI6fJ/EY3n9xo/ZBrlfA00n7Hv93UOfjD4o8mwDwuoebYQDnG9xUtV0FtQ9+PZMzey63KWzouiBlK4XTuaic4dSprnR55tm4AGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867770; c=relaxed/simple; bh=h4Mxghdi5fRQZt+NzNnL6Ag/UNAtkzSc8WHhzBml3Vw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X9igKf1D9BWHdkgBmKBJU5vHzWtJ5CY19643BVqrbkKnHHzjkapOM3aGzTGDbI1x47V8NSipag/3aIkTHsagir+QevyCd/R0zVZkN11YqUMgX9zFXyfBqTu/cnH3eIZOsj1GpF/4jSoSIlwtMF9pRwU8QJi7iia2oP/xnXMy9gs= 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.56 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 dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4ZD5rZ4FpSz4f3jrh for ; Thu, 13 Mar 2025 20:09:02 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 06E2C1A1693 for ; Thu, 13 Mar 2025 20:09:25 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S5; Thu, 13 Mar 2025 20:09:24 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/9] mm: swap: use __swap_entry_free() to free swap entry in swap_entry_put_locked() Date: Fri, 14 Mar 2025 05:05:09 +0800 Message-Id: <20250313210515.9920-4-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S5 X-Coremail-Antispam: 1UD129KBjvJXoWxZF13AF1UArW3Jw4xXFyxXwb_yoW5WrW8pF 9Igrs8tF4xZr17K3yrJw45ZayFvw4xWw10qFnrGw13ZasxXryFgFyDC3y2gFyUA3s5uF98 Z3WUtr17ur42vFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB0b4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUWwA2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK 0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4 x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l 84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I 8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AK xVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2Ij64 vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK 8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I 0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU3G-eDUUUU 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_entry_range_free to do actual swap entry freeing. This introduce an unnecessary intermediate state. By using __swap_entry_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 --- mm/swapfile.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 7c886f9dd6f9..ba37b9bff586 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1367,9 +1367,11 @@ static inline void __swap_entries_free(struct swap_i= nfo_struct *si, } =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 @@ -1398,10 +1400,9 @@ static unsigned char swap_entry_put_locked(struct sw= ap_info_struct *si, } =20 usage =3D count | has_cache; - if (usage) - WRITE_ONCE(si->swap_map[offset], usage); - else - WRITE_ONCE(si->swap_map[offset], SWAP_HAS_CACHE); + WRITE_ONCE(si->swap_map[offset], usage); + if (!usage) + __swap_entries_free(si, ci, entry, 1); =20 return usage; } @@ -1480,9 +1481,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_entry_range_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; @@ -1562,8 +1561,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_entry_range_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); } @@ -1607,12 +1606,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_entry_range_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); - } - } + 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 23:25:08 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 0552F1F152C for ; Thu, 13 Mar 2025 12:09:27 +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=1741867770; cv=none; b=c3KubpvAbh3YRgyC8FCd5F7dyC+DG/1Wqp+cY48Z2c3+ZA8ajvzIFWxkEzcO08A0vb2O8wO5wfsCaiod+Q1gmQwOvink/5fYEYJM7FCEh3uutIAt0RKbLl4sKcjzttgVD7Ub8XI3dzU4ZY85Mxxa3aKwJFSWAZofxat0FdA+CEY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867770; c=relaxed/simple; bh=+EFcCF5/6cYZkeBIbHkUNAr7o0ldpzhKTtdz7azgZPc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rMT2TVEpAYyOV2vnALPf1mdU6Uhbw3h7YZe0HTIg56WSzCI7RpeHE9bury9MO0mdfiTV0IR7Q0OamUdRyrEn9FBLbIfMJJYlQnDEGM6gplPaVIRZyTODpvBRVG8gUxI+7J9o6MsQs0UCfsCeoy9DYQcBcSSsA4Ab1BfTh3FXv4k= 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 4ZD5rh1DlHz4f3jkn for ; Thu, 13 Mar 2025 20:09:08 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 3E5EF1A1695 for ; Thu, 13 Mar 2025 20:09:25 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S6; Thu, 13 Mar 2025 20:09:25 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/9] mm: swap: remove unneeded VM_BUG_ON(*map != SWAP_HAS_CACHE) in swap_entry_range_free() Date: Fri, 14 Mar 2025 05:05:10 +0800 Message-Id: <20250313210515.9920-5-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S6 X-Coremail-Antispam: 1UD129KBjvJXoWxWr45tF15GrW8Gw1UJry7Wrg_yoW5CryrpF 9agwn8KFs7Jr13KrW7Jw4qka1S9397WF18ZF9rGw1av3Z3Jry0qFyDArWjkFyUAFykuasI y3WDtr17Ga1jvFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB2b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAv FVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJw A2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE 3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr2 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU3XTQUUUUU 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 1 and SWAP_MAP_SHMEM flag. Signed-off-by: Kemeng Shi Reviewed-by: Tim Chen --- mm/swapfile.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index ba37b9bff586..14b7b37996ff 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); @@ -1511,7 +1511,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; @@ -1530,12 +1530,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. */ -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; @@ -1545,7 +1545,6 @@ static void swap_entry_range_free(struct swap_info_st= ruct *si, VM_BUG_ON(ci !=3D offset_to_cluster(si, offset + nr_pages - 1)); =20 do { - VM_BUG_ON(*map !=3D SWAP_HAS_CACHE); *map =3D 0; } while (++map < map_end); =20 @@ -1605,7 +1604,7 @@ 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++) swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE); --=20 2.30.0 From nobody Wed Dec 17 23:25:08 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 5CF992571BF for ; Thu, 13 Mar 2025 12:09:28 +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=1741867770; cv=none; b=R5hToQmJqtAnnSWt1Q4DVsHyHZgS8FBBxftyJoRUVMb+OJnTSsSok/R0rQyn7H1KqF3epaaGDC75Lcf3Gg4LYxctMFzc3FL4K1P63EN1XiaXst5xUfEnFIjApH2nn5vrsgRYf/VMtgIcYCPlhbeKcCX36r/+4bSNi6lRDgIIkqg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867770; c=relaxed/simple; bh=cN23TyK3IT0ra4A3paPBKtVBA96DRWb+pv6+Ty39nd8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nFS4fMTv2OJqnjyC5TKferUcb2J6YBxbNuZY/Aom4eJbe4iJfJ/r1TdDfHU4Dy1WrOZ3KzJz/1JUJzI9kLYfMAhqBHRe+aX5o9a/gHGGqH5WUW02xbgTkRKSpVCSw+L21RNZDcm+5GYssNbyDVTCe/Df6P+bjCdXPu/gS2QTi9g= 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 4ZD5rh3DNNz4f3jLh for ; Thu, 13 Mar 2025 20:09:08 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 845421A11AD for ; Thu, 13 Mar 2025 20:09:25 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S7; Thu, 13 Mar 2025 20:09:25 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/9] mm: swap: use swap_entries_free() drop last 1 flag in swap_entries_put_nr() Date: Fri, 14 Mar 2025 05:05:11 +0800 Message-Id: <20250313210515.9920-6-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S7 X-Coremail-Antispam: 1UD129KBjvdXoWrZw1rAF4DAw1kuF13Ar1DKFg_yoW3urb_ua 9Ykw1kJw43XF1DuF17tFyjvr9Yg395AF13ZF4ftFWayFZ8GF95Zw4qgr98C34vvw1Ivwn3 Aanavr1kArnrGjkaLaAFLSUrUUUUjb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbS8YFVCjjxCrM7AC8VAFwI0_Wr0E3s1l1xkIjI8I6I8E6xAIw20E Y4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l87I20VAvwVAaII0Ic2I_JFv_Gryl82 xGYIkIc2x26280x7IE14v26r126s0DM28IrcIa0xkI8VCY1x0267AKxVW8JVW5JwA2ocxC 64kIII0Yj41l84x0c7CEw4AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26F1j6w1UM2 8EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq 3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8w Aqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE 14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCF04k20xvY0x 0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E 7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcV C0I7IYx2IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF 04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7 CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jyYLPUUUUU= X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ Content-Type: text/plain; charset="utf-8" Use swap_entries_free() to drop last 1 flag to eliminate the need to set slot to the 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 14b7b37996ff..0ce0ca08594e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1508,10 +1508,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 23:25:08 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 C7307266596 for ; Thu, 13 Mar 2025 12:09:28 +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=1741867770; cv=none; b=BiR10TfjmMcqYQxBV1K7xtt880hT8XsShbfUGNRCFJdtNKtvASqfHILZCAJG7y0fWtpZ6mQ8GlH8RDP7Kb3wpT1yX0le7qK+zGtTHH4Qflz3TTYg73yE4EhCUjpMd7nJSMf/AVbzAR2hi52xb43eyOpKGytGp4tdSU0eqDwKtAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867770; c=relaxed/simple; bh=/LI5apJjiHyV5oq+l/98j0tZbIKNvSpgMoDEpwqivK8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a/8bWlDRB+WiXe1IYEbB1BX3WoTKajJZR9NRtuma82CuAlOW6q8Of/W4Bf2SfuoGeU6I6wXMfBH9inYhwylc4zvXv/shfYR0NvMGwpLcPXzruL6D2cZ5GYpfqYpTgUXgW9Rdldw2z5xyTgund1Rc5cev/2TugOSU4lcMBiXSx0Q= 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 4ZD5rh5Kwwz4f3jtK for ; Thu, 13 Mar 2025 20:09:08 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id CB7D71A0AA4 for ; Thu, 13 Mar 2025 20:09:25 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S8; Thu, 13 Mar 2025 20:09:25 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/9] mm: swap: drop last SWAP_MAP_SHMEM flag in batch in swap_entries_put_nr() Date: Fri, 14 Mar 2025 05:05:12 +0800 Message-Id: <20250313210515.9920-7-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S8 X-Coremail-Antispam: 1UD129KBjvdXoWrKF1Dtry5ur18tryxAFykAFb_yoWkCrc_ur Z0qw1kCF4DJFsruF12y34xXrykGws7AF98X3W0qFW2q3yDKrySqwsFkrW7trn8J3yfurs5 Aas5Zr43uwsxJjkaLaAFLSUrUUUUjb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUIcSsGvfJTRUUUbS8YFVCjjxCrM7AC8VAFwI0_Wr0E3s1l1xkIjI8I6I8E6xAIw20E Y4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l87I20VAvwVAaII0Ic2I_JFv_Gryl82 xGYIkIc2x26280x7IE14v26r126s0DM28IrcIa0xkI8VCY1x0267AKxVW5JVCq3wA2ocxC 64kIII0Yj41l84x0c7CEw4AK67xGY2AK021l84ACjcxK6xIIjxv20xvE14v26F1j6w1UM2 8EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4UJVWxJr1l84ACjcxK6I8E87Iv67AKxVW0oVCq 3wA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8w Aqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE 14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCF04k20xvY0x 0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E 7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcV C0I7IYx2IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF 04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7 CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jstxDUUUUU= 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 1 flag 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 0ce0ca08594e..2d0f5d630211 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) { @@ -1497,7 +1497,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 23:25:08 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 C7294262D38 for ; Thu, 13 Mar 2025 12:09:28 +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=1741867770; cv=none; b=PHJoIUoXepY1VAesLtiGaEYrho8Dwmcx7rMMY5CAZo3Fq/q9paVx+13pXXG0QLR1etE6LvY/heyjgbSVI45DlXW5xtoggZP/Fle7rIU+BtjfbfnFxX8s9kJHLtQDY1tfZTfbJoq3o/XAJJL5CDEJGLqzSuDghJFrR4MdCILZ4GE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867770; c=relaxed/simple; bh=jsDW7j7a6aZwVM9zgmMLLPc159swEv+yzr9HX/fNnzg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HZyW7mo1HHQY2n8T74kWfuv3lfUgUb6H6M64zetPtBBLydEoIdS4QYBke7iWhTAZtv6eKiXl+0Ed9tLEjqn6KTl0lENtO1kHG5RXyAF9Tt99GZi2hdgfaEXJxfNq7Fpz4Ycn1auFLPUBSwJjkNiCC+Xzn6OK+oAS+hPHEBhSI7Y= 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 4ZD5rb4XG5z4f3jrl for ; Thu, 13 Mar 2025 20:09:03 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 1466F1A1697 for ; Thu, 13 Mar 2025 20:09:26 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S9; Thu, 13 Mar 2025 20:09:25 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/9] mm: swap: free each cluster individually in swap_entries_put_map_nr() Date: Fri, 14 Mar 2025 05:05:13 +0800 Message-Id: <20250313210515.9920-8-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S9 X-Coremail-Antispam: 1UD129KBjvJXoWxGry8CF4rtFy3uryUAw1rtFb_yoW5tFy8pF yagrn8tr4xZr17Jr4xAws8ArWru3y0gF1jqF9rGr1Sy3srAr18WFyvy3yYvFyUC34kur98 t3W7K347uFs8tr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB2b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAv FVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJw A2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE 3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr2 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUIL05UUUUU 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(). Signed-off-by: Kemeng Shi --- mm/swapfile.c | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 2d0f5d630211..ebac9ff74ba7 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1473,25 +1473,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) +static bool swap_entries_put_map(struct swap_info_struct *si, + swp_entry_t entry, int nr) { - 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) -{ - 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; @@ -1502,9 +1487,6 @@ static bool swap_entries_put_nr(struct swap_info_stru= ct *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)) { @@ -1521,15 +1503,33 @@ 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); + 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 @@ -1807,7 +1807,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 --=20 2.30.0 From nobody Wed Dec 17 23:25:08 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 008F2266B5F for ; Thu, 13 Mar 2025 12:09:28 +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=1741867770; cv=none; b=s1yaV3x0l23iFSswLSVVCel2EO30Z+bgxDBbT++vHEBP/vBIppoVWUATkhTJ0XBI/kGy3s7iSN5o5mkaEK8T+vHiCA+4RN1y4Bit85QELAfn+PzW1RPHjs7dmp3iklGEn/QOi/I/5+qekJBC/EqqS3QFnWcoFM2/GMMDBcsF3Ms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867770; c=relaxed/simple; bh=phExd1IhDL3I3mCEX1pldEAIZl6Lf8yYKt4S94nEJiI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OP2n66HwdIG72p/5qFTs5wZcsN71e/u5fRZ3rgbW7A8j4BhaF4CnOS7NcuS3yGtzwWKZVrWduXLkyfHSz+OINBlcJ8G6jnr2Fk/HO2jkIf/kKILpPvioL89pQClge8guQYnFYsP84txtIDW3qzG4LO7ZYo4KmJvi1TpSkC2Dlx8= 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 4ZD5rZ360Fz4f3mHb for ; Thu, 13 Mar 2025 20:09:02 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 51D5E1A06DC for ; Thu, 13 Mar 2025 20:09:26 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S10; Thu, 13 Mar 2025 20:09:26 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 8/9] mm: swap: factor out helper to drop cache of entries within a single cluster Date: Fri, 14 Mar 2025 05:05:14 +0800 Message-Id: <20250313210515.9920-9-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S10 X-Coremail-Antispam: 1UD129KBjvJXoW7uFWkAF4DJr4rGryrJF13Arb_yoW8CFykpF 9Igrn8KF48Xr13Gw4xJw45J3ySv3yIg3WUZFy7Gw13Z3Z3Jr10gFyqyrWa9ryYkr95ur98 Ca48try2gF1jyF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB2b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAv FVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJw A2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE 3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr2 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUIL05UUUUU 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 ebac9ff74ba7..343b34eb2a81 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1473,6 +1473,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) { @@ -1597,8 +1612,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 @@ -1606,13 +1619,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 23:25:08 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 41F81266B67 for ; Thu, 13 Mar 2025 12:09:29 +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=1741867771; cv=none; b=UbqtY/TcjydMiMkKeJbtNzajQx3Cxwz2yOrF76EDhrkUgVtB/FTv1IoCiyEdmyfU8New2doA48FpOeoHuKmkGuMSc1zoxbZApLvXQ5ur5d+nZDiqM2Xbi78QQ+rI7yfabeF1tzaP3CIpxGZsQ7hPT+XTPdn+5tGh4YiO67E5Jvk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741867771; c=relaxed/simple; bh=+RFdnEFAFri2hpkX/ZUNN4h/qboM5E7p1+9yls2oJg4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gyqQSIAv0VGFtDsM/HSJK6qPNbPYdCxWNXbxv2WfgKcWYUoERHMMot29TAeJCnt8Pbh81XtKuQePZ5lRsTc4egHNgZKqxXugpdT+E/H2Odt98YNM7NdjrfQNuCIJybmMKRZM/lPFeSxQdvMQBR6WFgFYUdLZMg4a5N5la855acI= 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.56 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.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4ZD5rc10f1z4f3jrd for ; Thu, 13 Mar 2025 20:09:04 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.75]) by mail.maildlp.com (Postfix) with ESMTP id 8EF9E1A1697 for ; Thu, 13 Mar 2025 20:09:26 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP2 (Coremail) with SMTP id Syh0CgCnsGPyytJnTaZeGQ--.9643S11; Thu, 13 Mar 2025 20:09:26 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org Cc: kasong@tencent.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 9/9] mm: swap: replace cluster_swap_free_nr() with swap_entries_put_[map/cache]() Date: Fri, 14 Mar 2025 05:05:15 +0800 Message-Id: <20250313210515.9920-10-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20250313210515.9920-1-shikemeng@huaweicloud.com> References: <20250313210515.9920-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: Syh0CgCnsGPyytJnTaZeGQ--.9643S11 X-Coremail-Antispam: 1UD129KBjvJXoW7KFy7Wr47tryrJryDJr1rtFb_yoW8Aw18pF 93Wr1DKr4fJr1fKw4Ivw4DZrWav3ykGw1UJF9rWr1Fy3Z7tryIgF1vkrWxu345Gr95urZI kanrt3srWrs8tr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUB2b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUAVCq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAv FVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJw A2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE 3s1l84ACjcxK6I8E87Iv6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr2 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv 67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUIL05UUUUU 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. Signed-off-by: Kemeng Shi --- mm/swapfile.c | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 343b34eb2a81..c27cf09d84a6 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1570,21 +1570,6 @@ static void swap_entries_free(struct swap_info_struc= t *si, __swap_entries_free(si, ci, entry, nr_pages); } =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. @@ -1601,7 +1586,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; } @@ -3632,9 +3617,7 @@ int swapcache_prepare(swp_entry_t entry, int nr) =20 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