From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2D3EE77188 for ; Mon, 30 Dec 2024 17:47:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 618B76B00A6; Mon, 30 Dec 2024 12:47:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C7246B00A7; Mon, 30 Dec 2024 12:47:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 41A256B00A8; Mon, 30 Dec 2024 12:47:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1A7106B00A6 for ; Mon, 30 Dec 2024 12:47:27 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CB1ED81951 for ; Mon, 30 Dec 2024 17:47:26 +0000 (UTC) X-FDA: 82952354982.03.88BC7A2 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf11.hostedemail.com (Postfix) with ESMTP id C0FCA40017 for ; Mon, 30 Dec 2024 17:46:41 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Wj0Z7nkA; spf=pass (imf11.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735580823; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ixG1z0DI2xzvsIxDfP6EypWP22FNN2ivx8hCEgTlMv0=; b=l8xm/aF8RNkkG2VmGlzlasZO8gbVHDP/PZsb2pOiebCE2S9NVA2tmtqHkFq5SeawD9qyXV AJBmOJ/KegtVbr8qltsR3e2IijVmuQ+WNQ5aKerhNjBLEis2yzYEPcatGCLRH5eQB6KgB8 CiAW5I9/S2AgB4MdWgXmvgt3IXM4c+Q= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Wj0Z7nkA; spf=pass (imf11.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735580823; a=rsa-sha256; cv=none; b=sVIsIU2zltUCGhKnxN4aIEOm9AVLC7jrcRKACcpnJYCdm6np9R7OwRhAYQQD+zd+Si5BQw UbB50u2WPdS5Nbv3cC41vEReMGBMksWjAmzXTx/5qBJWxdci1m4wbFxawRUovGeDSZrJFc en5urMZgUvm1gjX4sMos8QPPypK3cvg= Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2163b0c09afso120034025ad.0 for ; Mon, 30 Dec 2024 09:47:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735580843; x=1736185643; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=ixG1z0DI2xzvsIxDfP6EypWP22FNN2ivx8hCEgTlMv0=; b=Wj0Z7nkAPE02KdJTacGGgPy2r/UajkXfLfCewLO7iGZl3T9u1P3w9WCIQK6kPW/HXX G9LpOFSSPlqrS/O7S1qm8rWpuflDW3AX0sYo1TdcBOofGGWCRqKQyfssFAidEyJdB36O 5H3WwbSnneAu1tvL4zIzRCAb5ALBF0iVewzl4OmRj7aHJFwhIOp357rvtrihLoOSB4nF PIw1X3qHHl3NqleKIkdQ7pn4G9gq332yEDcbLABebVqcnJsukn8pHRS9n0Vzv2wA5V4u RqUBPCbHbVSJ1ZzbJRSqyuqlsv2wPsAmpG3gJc5kFaIAgA5bIhduDMt6wiNyIC/MMopb viCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735580843; x=1736185643; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ixG1z0DI2xzvsIxDfP6EypWP22FNN2ivx8hCEgTlMv0=; b=bwTytB1904EBYZMWRRSSR7ZG20eXMgoSHpc5uPL6o+A8LdPbbHYN39lC18RG/PbLuR qgH/tWHFle28UY8hVJknSvOjbc0PsA46n/4dg+5r4TztjPA+EqEMyo4KsgNYXD8Tg6p9 V8FEr0IwzurEOZ9SYpxi8rFPPjYrJVDRBzPxmeFD+t2vvSo7+9raMJJyj6wvDXnkYQqp QZlekUIkJn8GpyWNpgnIYYmte5kzmNo/AUlFRILtEVCEi5Yrd93MuoubgAZE+Y3K9Epr DDVEwYQ10Stt5xa33w1N8cJSgbWva6JHFS+LicqaEtk3+Mabf6FP2M4GysiL9icR+RtL Kpkg== X-Gm-Message-State: AOJu0YwzIpbJqAKfQcpGTlpPPBWNa1zGXAGkghsmTaUhoOnEL4t5U2oi QTwpEq94V02Dv6//C2D4RZIByi+u1kGdxJnFTpcqppI8+685EnLNpmgO/YT97GpK2g== X-Gm-Gg: ASbGncuMVvb2JAHYQMVsKP8/0wW/4fVgpgsR42bSQLWK/QuMoNJK7IyEDwK3bjt59GO o26vTpFGDMTKvxXm47xWI3tXBclVV0Zw6npX6gWNDAPc0Q/puwJ0uwpArBE7OEsyU9ek3GMMt0M uVI0vcVb3wjN6/h59yf7dnLOHdQTES3IQZmA7dSyIg5Di7uvxE+XaVnVMAXQ/Af3xZJBi89HE64 wBp4mstgbs9ZFx50Odp+HO5CBwzILiaoCBTrmfU/+/WWN7Rruie+Elxi8o9uPLAdwSzWCFECho+ X-Google-Smtp-Source: AGHT+IFTPLqAurs/FG+CnxzXJMSklhchhlzwmJ/iejwXl+pptqXzgKFWQYwbBdGHMm6+KyVR0VVzug== X-Received: by 2002:a17:903:320a:b0:216:539d:37c3 with SMTP id d9443c01a7336-219e6e9e8d0mr524521965ad.24.1735580843135; Mon, 30 Dec 2024 09:47:23 -0800 (PST) Received: from KASONG-MC4.tencent.com ([1.203.117.231]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9cdf25sm180687695ad.118.2024.12.30.09.47.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Dec 2024 09:47:22 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Ryan Roberts , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v3 13/13] mm, swap_slots: remove slot cache for freeing path Date: Tue, 31 Dec 2024 01:46:21 +0800 Message-ID: <20241230174621.61185-14-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241230174621.61185-1-ryncsn@gmail.com> References: <20241230174621.61185-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: C0FCA40017 X-Stat-Signature: wc44qc49uzakz5zibnuiisw5uyqwftz6 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1735580801-98089 X-HE-Meta: U2FsdGVkX19hQU0XGa25bn8tHjQYdn98EdUJlRr/wisM58V7bmM0eVHKWQAIFbkwui42nUP/wjEj33CopvmZQ6i0en6R41wY7oE4arZZtnivoyUxjOwrVej8xY3xQBrtGLr53YDD+yYFv1FFm4SlWrL7qDAnAt8yNtWT1tWX/0UJAwiNxDIGhHo54ad7dbWWMY7cvYo6NWoOeGzjEUufcg8InxY6cqMibiqMrGUpRG07/bwNawbK24679Rc0a7FsQ5nNGZmDqunVwZqaadskJVFfFulghN8cXsPVUAHVXi8vGtM+17YUbv2ggqauLfd2o58bJmZ+Ff2Wj+24RsK0mBQwu+2/RQNNNwJDGtjwgvjRri+09d2LIoaZzv0gSyknW8dvnPTSyETMmKBef/RwoAmbEYrc7dIxwoM3nqbkN94vnxaQA4+3DU1pneJok36ye55okGo/a0bg0mzSa1LrL+XvNsexR6s2hp4WDtCcZ9akbwxH8JSihNMsbmTXuX/izOoGM3rgrQSbxYvioJDNTMiQigzQYXZX+ti1lEvT/T6T93VWerp0E+CUwYyomBTVGE9Hg4gmq9q2faaaq+rd+F7zLSH0pV0ju+xpL4xpLXmVajaD0dqoiHOv8DahAw4vrB5SSJ4x545GDx7UBTOWMiXebcbqt+FmPnchok5lPLU2q7i2KNGyoVRGEBiTulVRoKcaO3Gz5GGOH/QE2/zw7022cAlqWLZIyZx3YVj66Hc/XWHVi8ijcUmCsI9Mvbbi0rGirjMFjrTEdbegTKNyqE2f/Ud1Y75AKzFYtoZyCnRyJTjP1JyrOWHvhRyQyuIr8cbGK5q9Bk/s0FJHaeAJMhx1bAyg9j5OgHhjXJImJvfLn5AYt0OKa2CIV8YLuipkhxcZlrbb55kXtKvRG5YH7yO9uP4JjnWdOnV5jeio2oRdMb7jRsJv+Fm1+dOGeZ60MP99lRqzLknRYnUea68 lYyYfvxC XvgCjEJmnDFzZHO0tA92wqZdqH27x+xjQBuwqyCdyqFVkEJKEPyL7sqvct819zbw6RnZsjPrVcl92xMcRwnGO1aIk80DJVi7IFGzNgw4y5sEfTba3gOE+NUVJ1t36NH7e1oPNMYv6b1rx5L/jaSw+XoFRczW1fwZZ11nUZa3ss3ui4My/b3+1kZDbSAysesKOQJnLdp9DJeXXPKKagTaRpj00H9suJBwwvYkNWHNjY8Hz9ZPnQV2oY2EGWlh0HegoOU8miAI9oYOxNl87u6zJ1SpbFZtDYMptYSOBhIpsObC4inRbmYu4SBIvbE64f8IMnxp1KiXv093f7mI/9b9AIQ9WDmeKFNNeGAQ2jfKoreOWZM8ZGSfixV2YYJDFMVTfLlUrmMmU6OZTQJwAyAQRbpaIKs3zAW3MB3MP5Xx6wBon1XsZ9IksX3MT1ogJOk0d3nNY2F3kxZXfuYf1tX2GDp+BkBNobiNsfuwGKmHe0oHt5kC5zF/cvfzDl+jHgVNm+5BV55IpXgLhP56lV/70f0vozp9+dD3QkDdsRGNOJ4IQmjLa1MnVsCFD811yE4FKhBOyAOA84Zqio+rJRdCNJnRxYIo9jUEVf9B8OnP0FHth09v44UHI9Yq8RA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Kairui Song The slot cache for freeing path is mostly for reducing the overhead of si->lock. As we have basically eliminated the si->lock usage for freeing path, it can be removed. This helps simplify the code, and avoids swap entries from being hold in cache upon freeing. The delayed freeing of entries have been causing trouble for further optimizations for zswap [1] and in theory will also cause more fragmentation, and extra overhead. Test with build linux kernel showed both performance and fragmentation is better without the cache: tiem make -j96 / 768M memcg, 4K pages, 10G ZRAM, avg of 4 test run:: Before: Sys time: 36047.78, Real time: 472.43 After: (-7.6% sys time, -7.3% real time) Sys time: 33314.76, Real time: 437.67 time make -j96 / 1152M memcg, 64K mTHP, 10G ZRAM, avg of 4 test run: Before: Sys time: 46859.04, Real time: 562.63 hugepages-64kB/stats/swpout: 1783392 hugepages-64kB/stats/swpout_fallback: 240875 After: (-23.3% sys time, -21.3% real time) Sys time: 35958.87, Real time: 442.69 hugepages-64kB/stats/swpout: 1866267 hugepages-64kB/stats/swpout_fallback: 158330 Sequential SWAP should be also slightly faster, tests didn't show a measurable difference though, at least no regression: Swapin 4G zero page on ZRAM (time in us): Before (avg. 1923756) 1912391 1927023 1927957 1916527 1918263 1914284 1934753 1940813 1921791 After (avg. 1922290): 1919101 1925743 1916810 1917007 1923930 1935152 1917403 1923549 1921913 Link: https://lore.kernel.org/all/CAMgjq7ACohT_uerSz8E_994ZZCv709Zor+43hdmesW_59W1BWw@mail.gmail.com/[1] Suggested-by: Chris Li Signed-off-by: Kairui Song --- include/linux/swap_slots.h | 3 -- mm/swap_slots.c | 78 +++++---------------------------- mm/swapfile.c | 89 +++++++++++++++----------------------- 3 files changed, 44 insertions(+), 126 deletions(-) diff --git a/include/linux/swap_slots.h b/include/linux/swap_slots.h index 15adfb8c813a..840aec3523b2 100644 --- a/include/linux/swap_slots.h +++ b/include/linux/swap_slots.h @@ -16,15 +16,12 @@ struct swap_slots_cache { swp_entry_t *slots; int nr; int cur; - spinlock_t free_lock; /* protects slots_ret, n_ret */ - swp_entry_t *slots_ret; int n_ret; }; void disable_swap_slots_cache_lock(void); void reenable_swap_slots_cache_unlock(void); void enable_swap_slots_cache(void); -void free_swap_slot(swp_entry_t entry); extern bool swap_slot_cache_enabled; diff --git a/mm/swap_slots.c b/mm/swap_slots.c index 13ab3b771409..9c7c171df7ba 100644 --- a/mm/swap_slots.c +++ b/mm/swap_slots.c @@ -43,17 +43,15 @@ static DEFINE_MUTEX(swap_slots_cache_mutex); /* Serialize swap slots cache enable/disable operations */ static DEFINE_MUTEX(swap_slots_cache_enable_mutex); -static void __drain_swap_slots_cache(unsigned int type); +static void __drain_swap_slots_cache(void); #define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled) -#define SLOTS_CACHE 0x1 -#define SLOTS_CACHE_RET 0x2 static void deactivate_swap_slots_cache(void) { mutex_lock(&swap_slots_cache_mutex); swap_slot_cache_active = false; - __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET); + __drain_swap_slots_cache(); mutex_unlock(&swap_slots_cache_mutex); } @@ -72,7 +70,7 @@ void disable_swap_slots_cache_lock(void) if (swap_slot_cache_initialized) { /* serialize with cpu hotplug operations */ cpus_read_lock(); - __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET); + __drain_swap_slots_cache(); cpus_read_unlock(); } } @@ -113,7 +111,7 @@ static bool check_cache_active(void) static int alloc_swap_slot_cache(unsigned int cpu) { struct swap_slots_cache *cache; - swp_entry_t *slots, *slots_ret; + swp_entry_t *slots; /* * Do allocation outside swap_slots_cache_mutex @@ -125,28 +123,19 @@ static int alloc_swap_slot_cache(unsigned int cpu) if (!slots) return -ENOMEM; - slots_ret = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t), - GFP_KERNEL); - if (!slots_ret) { - kvfree(slots); - return -ENOMEM; - } - mutex_lock(&swap_slots_cache_mutex); cache = &per_cpu(swp_slots, cpu); - if (cache->slots || cache->slots_ret) { + if (cache->slots) { /* cache already allocated */ mutex_unlock(&swap_slots_cache_mutex); kvfree(slots); - kvfree(slots_ret); return 0; } if (!cache->lock_initialized) { mutex_init(&cache->alloc_lock); - spin_lock_init(&cache->free_lock); cache->lock_initialized = true; } cache->nr = 0; @@ -160,19 +149,16 @@ static int alloc_swap_slot_cache(unsigned int cpu) */ mb(); cache->slots = slots; - cache->slots_ret = slots_ret; mutex_unlock(&swap_slots_cache_mutex); return 0; } -static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, - bool free_slots) +static void drain_slots_cache_cpu(unsigned int cpu, bool free_slots) { struct swap_slots_cache *cache; - swp_entry_t *slots = NULL; cache = &per_cpu(swp_slots, cpu); - if ((type & SLOTS_CACHE) && cache->slots) { + if (cache->slots) { mutex_lock(&cache->alloc_lock); swapcache_free_entries(cache->slots + cache->cur, cache->nr); cache->cur = 0; @@ -183,20 +169,9 @@ static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, } mutex_unlock(&cache->alloc_lock); } - if ((type & SLOTS_CACHE_RET) && cache->slots_ret) { - spin_lock_irq(&cache->free_lock); - swapcache_free_entries(cache->slots_ret, cache->n_ret); - cache->n_ret = 0; - if (free_slots && cache->slots_ret) { - slots = cache->slots_ret; - cache->slots_ret = NULL; - } - spin_unlock_irq(&cache->free_lock); - kvfree(slots); - } } -static void __drain_swap_slots_cache(unsigned int type) +static void __drain_swap_slots_cache(void) { unsigned int cpu; @@ -224,13 +199,13 @@ static void __drain_swap_slots_cache(unsigned int type) * There are no slots on such cpu that need to be drained. */ for_each_online_cpu(cpu) - drain_slots_cache_cpu(cpu, type, false); + drain_slots_cache_cpu(cpu, false); } static int free_slot_cache(unsigned int cpu) { mutex_lock(&swap_slots_cache_mutex); - drain_slots_cache_cpu(cpu, SLOTS_CACHE | SLOTS_CACHE_RET, true); + drain_slots_cache_cpu(cpu, true); mutex_unlock(&swap_slots_cache_mutex); return 0; } @@ -269,39 +244,6 @@ static int refill_swap_slots_cache(struct swap_slots_cache *cache) return cache->nr; } -void free_swap_slot(swp_entry_t entry) -{ - struct swap_slots_cache *cache; - - /* Large folio swap slot is not covered. */ - zswap_invalidate(entry); - - cache = raw_cpu_ptr(&swp_slots); - if (likely(use_swap_slot_cache && cache->slots_ret)) { - spin_lock_irq(&cache->free_lock); - /* Swap slots cache may be deactivated before acquiring lock */ - if (!use_swap_slot_cache || !cache->slots_ret) { - spin_unlock_irq(&cache->free_lock); - goto direct_free; - } - if (cache->n_ret >= SWAP_SLOTS_CACHE_SIZE) { - /* - * Return slots to global pool. - * The current swap_map value is SWAP_HAS_CACHE. - * Set it to 0 to indicate it is available for - * allocation in global pool - */ - swapcache_free_entries(cache->slots_ret, cache->n_ret); - cache->n_ret = 0; - } - cache->slots_ret[cache->n_ret++] = entry; - spin_unlock_irq(&cache->free_lock); - } else { -direct_free: - swapcache_free_entries(&entry, 1); - } -} - swp_entry_t folio_alloc_swap(struct folio *folio) { swp_entry_t entry; diff --git a/mm/swapfile.c b/mm/swapfile.c index e57e5453a25b..d623f5b6dc4c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -53,14 +53,15 @@ 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, swp_entry_t entry, - unsigned int nr_pages); +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_range_alloc(struct swap_info_struct *si, unsigned int nr_entries); static bool folio_swapcache_freeable(struct folio *folio); static struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, unsigned long offset); -static void unlock_cluster(struct swap_cluster_info *ci); +static inline void unlock_cluster(struct swap_cluster_info *ci); static DEFINE_SPINLOCK(swap_lock); static unsigned int nr_swapfiles; @@ -261,10 +262,9 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, folio_ref_sub(folio, nr_pages); folio_set_dirty(folio); - /* Only sinple page folio can be backed by zswap */ - if (nr_pages == 1) - zswap_invalidate(entry); - swap_entry_range_free(si, entry, nr_pages); + ci = lock_cluster(si, offset); + swap_entry_range_free(si, ci, entry, nr_pages); + unlock_cluster(ci); ret = nr_pages; out_unlock: folio_unlock(folio); @@ -1125,8 +1125,10 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, * Use atomic clear_bit operations only on zeromap instead of non-atomic * bitmap_clear to prevent adjacent bits corruption due to simultaneous writes. */ - for (i = 0; i < nr_entries; i++) + for (i = 0; i < nr_entries; i++) { clear_bit(offset + i, si->zeromap); + zswap_invalidate(swp_entry(si->type, offset + i)); + } if (si->flags & SWP_BLKDEV) swap_slot_free_notify = @@ -1431,9 +1433,9 @@ static unsigned char __swap_entry_free(struct swap_info_struct *si, ci = lock_cluster(si, offset); usage = __swap_entry_free_locked(si, offset, 1); - unlock_cluster(ci); if (!usage) - free_swap_slot(entry); + swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); + unlock_cluster(ci); return usage; } @@ -1461,13 +1463,10 @@ static bool __swap_entries_free(struct swap_info_struct *si, } for (i = 0; i < nr; i++) WRITE_ONCE(si->swap_map[offset + i], SWAP_HAS_CACHE); + if (!has_cache) + swap_entry_range_free(si, ci, entry, nr); unlock_cluster(ci); - if (!has_cache) { - for (i = 0; i < nr; i++) - zswap_invalidate(swp_entry(si->type, offset + i)); - swap_entry_range_free(si, entry, nr); - } return has_cache; fallback: @@ -1487,15 +1486,13 @@ static bool __swap_entries_free(struct swap_info_struct *si, * Drop the last HAS_CACHE flag of swap entries, caller have to * ensure all entries belong to the same cgroup. */ -static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry, - unsigned int nr_pages) +static void swap_entry_range_free(struct swap_info_struct *si, + struct swap_cluster_info *ci, + swp_entry_t entry, unsigned int nr_pages) { unsigned long offset = swp_offset(entry); unsigned char *map = si->swap_map + offset; unsigned char *map_end = map + nr_pages; - struct swap_cluster_info *ci; - - ci = lock_cluster(si, offset); /* It should never free entries across different clusters */ VM_BUG_ON(ci != offset_to_cluster(si, offset + nr_pages - 1)); @@ -1515,7 +1512,6 @@ static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry free_cluster(si, ci); else partial_free_cluster(si, ci); - unlock_cluster(ci); } static void cluster_swap_free_nr(struct swap_info_struct *si, @@ -1523,28 +1519,13 @@ static void cluster_swap_free_nr(struct swap_info_struct *si, unsigned char usage) { struct swap_cluster_info *ci; - DECLARE_BITMAP(to_free, BITS_PER_LONG) = { 0 }; - int i, nr; + unsigned long end = offset + nr_pages; ci = lock_cluster(si, offset); - while (nr_pages) { - nr = min(BITS_PER_LONG, nr_pages); - for (i = 0; i < nr; i++) { - if (!__swap_entry_free_locked(si, offset + i, usage)) - bitmap_set(to_free, i, 1); - } - if (!bitmap_empty(to_free, BITS_PER_LONG)) { - unlock_cluster(ci); - for_each_set_bit(i, to_free, BITS_PER_LONG) - free_swap_slot(swp_entry(si->type, offset + i)); - if (nr == nr_pages) - return; - bitmap_clear(to_free, 0, BITS_PER_LONG); - ci = lock_cluster(si, offset); - } - offset += nr; - nr_pages -= nr; - } + do { + if (!__swap_entry_free_locked(si, offset, usage)) + swap_entry_range_free(si, ci, swp_entry(si->type, offset), 1); + } while (++offset < end); unlock_cluster(ci); } @@ -1585,18 +1566,12 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) return; ci = lock_cluster(si, offset); - if (size > 1 && swap_is_has_cache(si, offset, size)) { - unlock_cluster(ci); - swap_entry_range_free(si, entry, size); - return; - } - for (int i = 0; i < size; i++, entry.val++) { - if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) { - unlock_cluster(ci); - free_swap_slot(entry); - if (i == size - 1) - return; - lock_cluster(si, offset); + if (swap_is_has_cache(si, offset, size)) + swap_entry_range_free(si, ci, entry, size); + else { + for (int i = 0; i < size; i++, entry.val++) { + if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) + swap_entry_range_free(si, ci, entry, 1); } } unlock_cluster(ci); @@ -1605,6 +1580,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) void swapcache_free_entries(swp_entry_t *entries, int n) { int i; + struct swap_cluster_info *ci; struct swap_info_struct *si = NULL; if (n <= 0) @@ -1612,8 +1588,11 @@ void swapcache_free_entries(swp_entry_t *entries, int n) for (i = 0; i < n; ++i) { si = _swap_info_get(entries[i]); - if (si) - swap_entry_range_free(si, entries[i], 1); + if (si) { + ci = lock_cluster(si, swp_offset(entries[i])); + swap_entry_range_free(si, ci, entries[i], 1); + unlock_cluster(ci); + } } } -- 2.47.1