From patchwork Tue Dec 24 14:38:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13920201 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 21AC7E7718D for ; Tue, 24 Dec 2024 14:40:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FFEC6B00A6; Tue, 24 Dec 2024 09:40:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 988616B00A8; Tue, 24 Dec 2024 09:40:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78F2F6B00A9; Tue, 24 Dec 2024 09:40:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4B8E06B00A6 for ; Tue, 24 Dec 2024 09:40:27 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1296D450E0 for ; Tue, 24 Dec 2024 14:40:27 +0000 (UTC) X-FDA: 82930112538.04.E92A088 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf27.hostedemail.com (Postfix) with ESMTP id 4920A4000F for ; Tue, 24 Dec 2024 14:39:42 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IwYzmo7d; spf=pass (imf27.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=1735051206; 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=oGCoWj+d4yQp+yoZIDEe8P3zgKNWoImSkpotl3PB1J8=; b=SD5Kk4VSq4PsOFWT31aKaS9wRYJXwu2GWvbBdilZ1RDd0vMIBImiRyoVfxY8Oxx6B3r6PR LHmwT7bZ+uWGfvAMcwCesaxj2bt0Bkr+54YEWkwUawfAYFinbs4pOZbUgC5PcbB7KHHlTc XvViv558qYoGFd+J8zHmo7knhsyfsPU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735051206; a=rsa-sha256; cv=none; b=UdKl+Mfz+oBmLc3m3XRSu965WS8NoLVh64ZonpF3aeh41UU8/uPHuOBgmiPAI7Gu3US/8J ErlPf/wThx4vxcNJzHWBmMMYHUtj6GoOvvJr6jeriaXpr+80e7is+2Y8HZA+vccF9DDq0e +tNAzyjeUH+ZY/bHWhf5Rxt0F4K+4UQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IwYzmo7d; spf=pass (imf27.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 Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-21644e6140cso55616555ad.1 for ; Tue, 24 Dec 2024 06:40:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735051223; x=1735656023; 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=oGCoWj+d4yQp+yoZIDEe8P3zgKNWoImSkpotl3PB1J8=; b=IwYzmo7dSvluOWQIQFegsrsuwOPPGQGT0nguCUOcHkvmC4yvd3IOUUV820X5Fn1X30 meKJefl2KB3OK4q9ijhYNKXICESg0NM+gGOcwJMKM9WTkN2Rvix4qniEf/mV3QrUIRFh 4yFDLDi8cuJulunWDIzTPX6/FVey5FJHNWJ56zxNwvyk98twPzbF1fmtMJKs5Yl7uOGI 4JxZ6zKU0F7F65V2VgPfjE5T2zpg3RuCikausYDKnyx5wu7L/ZSMy1QWtOqAQvld4kPk QD1pi15ZVff1b4OWPsZuftZPA0BUgmhATd29dvSqI7r+q82T6cCWTeHA+7xQCdxmJ9AW vv6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735051223; x=1735656023; 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=oGCoWj+d4yQp+yoZIDEe8P3zgKNWoImSkpotl3PB1J8=; b=R/0Ddf/EtDQz7bdnGJLqkwJ3uwrgUu/UwSkQ13dJQhroNHWCYGL6bs+HybeO1u0Z1U aF90V6QekanFjtcJ0HsSTSrIKatYkXkQxww5E8Q0clwW75bB/wKCgKBQHDHO7NjldmZs C5kv4nqYkj51DU8g+ZaSlgGEviSN/WkwKbvUbCeQNp1QsXwStMUWA7Hq6VonFTJLHX1C RUefDtaz36cUYIw+WWyO5TLvEtHrRH/b69799bKiyjlH/eXIA9wKDxJKmE1uj7R4OabA 5rBb7CUS/EMnzIZHqzaNm91DvRqWwCx3NBEv0IXN7fm/OIiBJt76cKq/wVHk7aaNDAfU rv0A== X-Gm-Message-State: AOJu0YwT81oky+8G57fHGJG8xuagHC8zi+8tVLk86mACVa/fcalAy9bw dOFDNVzBKhVo4oUcPiFvFMn89apoBjPgr/nbvkPMd/3hlNUZSOC9qjzLhtmnydw= X-Gm-Gg: ASbGncushvDP1ubhbLPerthR5T6HeUfYJQvUEWEuzVAHp4DrMfE2moH6w19e9P2QXf0 QuXGlkgScjjwJB0etx2ZUw4qhJqMd/nB2yMwrLqvIyNMuZqUyh2B9xDVDpn8sd6vH0Bs+BxB65v hE+ULBNV3F5e+yOhTvGRHGBFQ6XnbKFBdfxazPYbiJgNGh1vwCx0UF6wJG5dsgf/gC/KMFNvmQZ qMW0dvKO7Mmezh4q6CJq8IMXgNewAc2jbvOK5VHEMvl3QElQ+N4n6NjNlXWT+jCvhlKcDqoIoq9 mQ== X-Google-Smtp-Source: AGHT+IGAPgVHp5vUFJh+SSN/uYjKj3b0hRrsN2QqzC9V+t4vENg01vQU53MJCWTYsvcbffhDkXP5bA== X-Received: by 2002:a17:902:d4c9:b0:215:6f9b:e447 with SMTP id d9443c01a7336-219e6ebf228mr175354085ad.30.1735051222881; Tue, 24 Dec 2024 06:40:22 -0800 (PST) Received: from KASONG-MC4.tencent.com ([115.171.41.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9cdec1sm90598735ad.136.2024.12.24.06.40.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 24 Dec 2024 06:40: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 v2 13/13] mm, swap_slots: remove slot cache for freeing path Date: Tue, 24 Dec 2024 22:38:11 +0800 Message-ID: <20241224143811.33462-14-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241224143811.33462-1-ryncsn@gmail.com> References: <20241224143811.33462-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4920A4000F X-Stat-Signature: qnud3eaoistdp9g6xknthakjk3tnbjg3 X-Rspam-User: X-HE-Tag: 1735051182-60995 X-HE-Meta: U2FsdGVkX18AdyaHSfeE4YD02miu3WMe7mc+cpgxWhFMBgjYn1mgGMZhrBfFj3oZc9H7fC9SR9oIi2zT5/okK4oJsm328q4R4AIHJmC8MSsQ5IDAK3CicP1fF/CC8KCAxr394GtPao1nuS5rz1xWpxTIF1sBNjA1W+vwsZAwsSg1TTpJypeVBoeyh77qWekqabP1u+MBg/E4mKQpNDgDcDhn6QAjUcutG1B4rKr8rBrTKO+P4Ns3BpnTP5r5W1XqFv7O+5WAUlotvhTGKA4lZQ22LqL8+kp1NG6++5unvvhwQXWj89OVO7BAWOupS7vJ3x9A3BsSuKWFtej3VM/rwZTuXohlXvJI35dQV9ngXAA1ERmKzJ6CxmuJ5IrTOrCNfwoBR94oE3SnyNB2Q6Bs6Pnfoc60C5/cgneyLky53gnyQgeJOSls4IXLm0WmFpEu2mnGAHuzmsIeDnUGQ+Dn6oKU/EM/ybP43QZQt75F563ESyRDOn0axHdZhI/5p2OQjaU/5CcmqHhDYKyAbXdn92bRjNNjYS8uAVQAuTdecPPYY5vbz7WJlAZPDjem4KjFt3NlhMtO6As77rfCDoJ2KCz4ABSEh6qLZJcZKiRpU4AdGOWjJUTRjidHVWW1w3uUJSkKiEiMo0KNOolEgkJnR/20zS2xn3qeDCuHBMRap7ZORg74ajbGC2l+ru7RZHZNsHeuNtaWblWESRvUhNbfpI/HdkhG72Mg8vpt6jvS4ZtqY5rbA/SxVNFQI1q1UZm8eCPpKAVxhzXM6OQFC5SprdmFUugaQA1HOBD3YzdLJ/Bg9a8DMvGSpJao0lhqDEGwoamikPQm43jCArAqD7ZRMpLkPEgx4V2NQQ53ZbfJsgBSZSqtIUM7yIon+MC0e35znHDBixY5PnbIzwhm01kV6IVD8xkYfXzhDqrxdNgmLwqHZ5D9TXZdZS7qb28jtBQYZ4+BRfZqFwuNf7+J85K vgahoCkt Sl9ytdlY/KccJ2GZmWDmRElEt8vljx4Qj4S9Cefr47tA+jd7IblBwo3A9O1MMHE2kV4isp7yub1HHo9X8srVRz5K/8p/1Tfuilp6Tfthywlg2gmd3PzqSOQiVOo5a/4lkT0bZQlgRhJDYmvlPqaPuLph1F+s+Vj7Rf3JiW9iENPkHcD7p6PxwSfYZF9Hs0zExv05ms6MIGl5747rokCE0FqRRjgczWg8kMJTtGBf158HbGy2ZadqbOSQRCaeOTxFaMbN9mk0EAgRRRVY8IUyvwtuUB+MSxYaqdBSsY/g57qvMNw/dnprhJkwZTqIyCrjCGrqg/3Oex7ggsSHcZWPjc4oWMoofuSmgIcXhk6AdzZxOy/nVdwrYtkjblcO2u7bUBOvzI4Vrhl/hLBoV1pTz0+qt9X7F+mNi4mA03tN33KLQ6QOIkmEjP1lx3MT9WqARa5KBJTM6pk2G3FjSI0kK/06e+VF4Cb0XE4kRtC4jHyxtUu8cA4BurexgsQFc2rdyUZzYjL5gPzaXdTpP6Dc+LYcYAc8aV+4MpPiOXksmcnObEtjhuHeN57J6VkqpejlEabkt+JTCpwUj9UJnLPbSo+Xv4QWlwZPueoZuzlKrS5bIa60/p8CcOVoEAA== 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 482c531bdd8b..c5b5a3cd2a92 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); @@ -1121,8 +1121,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 = @@ -1427,9 +1429,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; } @@ -1457,13 +1459,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: @@ -1483,15 +1482,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)); @@ -1511,7 +1508,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, @@ -1519,28 +1515,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); } @@ -1581,18 +1562,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); @@ -1601,6 +1576,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) @@ -1608,8 +1584,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); + } } }