From patchwork Wed Jul 31 06:49:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Li X-Patchwork-Id: 13748191 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 71096C3DA64 for ; Wed, 31 Jul 2024 06:49:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BE2DF6B0092; Wed, 31 Jul 2024 02:49:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 881AD6B008C; Wed, 31 Jul 2024 02:49:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 609706B0089; Wed, 31 Jul 2024 02:49:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BA0296B0092 for ; Wed, 31 Jul 2024 02:49:26 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5C8F7C0154 for ; Wed, 31 Jul 2024 06:49:26 +0000 (UTC) X-FDA: 82399121532.30.3A91D24 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id A1A7C40018 for ; Wed, 31 Jul 2024 06:49:24 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="gK8KjJ/l"; spf=pass (imf12.hostedemail.com: domain of chrisl@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=chrisl@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722408560; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hIKa+KUWmxnDu4eCbL13MQiZrNi13NWmJgisa7SqNE0=; b=SeL0tW+vdYQfUAkNTmeSNZf9r9YlwstqnpykHIzG5NQEobEEi6bh2/SZotsBA4zgdFex68 Ez0KcIadJE/kjNjsG0kWfkMWCQ0ypzIEcIRC/K00IQciQOcKrG8ZrDRhKwWxRsMXsW98NH pNElRZF3lSMPZSpp4WR4LTB+g8CghOI= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="gK8KjJ/l"; spf=pass (imf12.hostedemail.com: domain of chrisl@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=chrisl@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722408560; a=rsa-sha256; cv=none; b=Sj7x+5VTe2vlslrdLYOURdS50lKLappnj6iiizzrGMMS9bQUl8Vm1BxvLtx7L6XlJB8yAh QRngyDHhNOCr/201d9d/jq760hwPryP6otFZ7625Qm63ejijCJVsVENvRFFzjY8LsHvCs0 E2kzUwShMb2rWyslieGzQ0Hxr+RkRdQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id CCEE7621F9; Wed, 31 Jul 2024 06:49:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A6C8C116B1; Wed, 31 Jul 2024 06:49:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722408563; bh=dTXhdRo4kj8AjPmtS92IPfvC0Tt9HRSF2uv8IzXug84=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gK8KjJ/ls7+wOOlC/d0FIoIXSj0R3NDeM9+iXvu+adQMveBC7Fw4bO4QtnnIORD7t HUaqkSKzJ3O4YQGooMEdusu0u8qfpoUpzlmdWhcCXnhafPdp39veiPnVP4pFVj6eaC Hypx12Tzh9rSc1aX45e+Kl/Ng6w495hCq3L9rZIDOoQso4g78OyJQbcYWeXWQ5GHH9 6VgkoFE2SXNqcZZiJV3hEhnax2ILh+N417/Re33nNrOxCA3Twr3F8+lVaqlhwkikqP bn6Giz7nv1xSti5xL2dvq+0TrKdFxCVvQi6LVviMLQVcNQkX1FXlpdPVc3yZ7ZCwc/ S9uV63wbVZ7iQ== From: chrisl@kernel.org Date: Tue, 30 Jul 2024 23:49:18 -0700 Subject: [PATCH v5 6/9] mm: swap: allow cache reclaim to skip slot cache MIME-Version: 1.0 Message-Id: <20240730-swap-allocator-v5-6-cb9c148b9297@kernel.org> References: <20240730-swap-allocator-v5-0-cb9c148b9297@kernel.org> In-Reply-To: <20240730-swap-allocator-v5-0-cb9c148b9297@kernel.org> To: Andrew Morton Cc: Kairui Song , Hugh Dickins , Ryan Roberts , "Huang, Ying" , Kalesh Singh , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Chris Li , Barry Song X-Mailer: b4 0.13.0 X-Rspam-User: X-Stat-Signature: bpsmm8byspbd41reytxjh1s4ekjadpwp X-Rspamd-Queue-Id: A1A7C40018 X-Rspamd-Server: rspam11 X-HE-Tag: 1722408564-818503 X-HE-Meta: U2FsdGVkX1/yaXitGCc2LmN5laadcNHwfhBCFX419o2Lckq+7+ZgTh2dVYf7SRGd0uC19ixCxGctIN29pv0wCe5gqan8xXscjcGIIhX+lvatg65y1t/2AadT5LKFXSCxz5e8aloc8uTzC33FMdhFZ9lZkA5u0YTCH/X5ESXOhTrDEabmYj1y53vQdGNQKNNxKL3EfoqEMgmVOMHA7CFJP+BDTcwi2AUKTZH6iwg7aCHLNQpKyadKlOqjB5Pqrsso79lOv+fwHthJ5aNdY+guegosMvd9YAgzTr54Ky59cn85k5O4lBpv7wW0CGVm387jARU4a6ORy7oGjqdbF/fMRM7TeKU4uJwbfM2qUA2d8b5XxEZWFccIdrJLDzzI9xqjis4L39IRWFrsTOfwhw8zWyey0rbgh9yRjTbScTOHJl+e+QaJ5Mu3VR2ElxMobClkzO/8eVQLrdM4gcGJJ9yLIL/TCzbtWTCWFjfqKVsYmAP2R2dNUjIVTWOeriZXYTZWfabTYcJ6vjNc7txaP4LYQ12ZdSNmcPjYAToCQrzSLP2k1Fbzzt8DPyQfZYbdXQPGSdatoE2xn4PgmI8jseHHMMHu6a4YmZlnkp4XiVd/XWxoMEFZxv6vsmWTOBw6xT4hiatbRwzJ26GDFYeYcnj8w1Jgllc33roIMg8K0R10FBbQlPqFzsiNtdGB9/9Zp8rXznVCa3121nLa5mfbSWrK/zuvbJD4/q3sJZzbaAljMfv9kC9K4/H4ggSD7lIs1NYQTv6jlbxxTMnagp6deQoAV8lF77q+/PH8DGhgYybBXftNuyv9TzeYMmNDDN6OokwA3CAfw+B9zBsCLpldP9Gh0DpHWWPGm9OmonqqTdEsUDJpbkYpiE5so8K3uURk+h88x3i6ysMKPELNVyCZ0KzczySWZJfVc0dnH2VLG9/Ue/bb5mJuMxyfmg4hUd8QFeQsFTD71Zmo/OomY4d3C5c h6Se2FHR buEna0L+P4xBHP3eL7iEuUPWkNzLjdK/XS77LlwmNp1DBIQtc5BLK90+lrPQeZ7d5q7wJs7mKkLV4d7ysBil7GjFL55ZQMk9tlfi7aovlS9kVCIY9SLqD8EJptBIOsTO9Gg0is8lTEsEHrgwkGYzRgwCZ38iDCWSP4neoSGPazl0puJrcu+ZeZFWdNZ+Bd7dmp6o/KrGl4nQECM1i61bSONveAe32oRzNNuZD/GrrUgipdIFYE7/vl9yanA== 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 Currently we free the reclaimed slots through slot cache even if the slot is required to be empty immediately. As a result the reclaim caller will see the slot still occupied even after a successful reclaim, and need to keep reclaiming until slot cache get flushed. This caused ineffective or over reclaim when SWAP is under stress. So introduce a new flag allowing the slot to be emptied bypassing the slot cache. Signed-off-by: Kairui Song --- mm/swapfile.c | 152 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 109 insertions(+), 43 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 9b63b2262cc2..4c0fc0409d3c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -53,8 +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_range_alloc(struct swap_info_struct *si, unsigned long offset, unsigned int nr_entries); +static bool folio_swapcache_freeable(struct folio *folio); +static struct swap_cluster_info *lock_cluster_or_swap_info( + struct swap_info_struct *si, unsigned long offset); +static void unlock_cluster_or_swap_info(struct swap_info_struct *si, + struct swap_cluster_info *ci); static DEFINE_SPINLOCK(swap_lock); static unsigned int nr_swapfiles; @@ -129,8 +136,25 @@ static inline unsigned char swap_count(unsigned char ent) * corresponding page */ #define TTRS_UNMAPPED 0x2 -/* Reclaim the swap entry if swap is getting full*/ +/* Reclaim the swap entry if swap is getting full */ #define TTRS_FULL 0x4 +/* Reclaim directly, bypass the slot cache and don't touch device lock */ +#define TTRS_DIRECT 0x8 + +static bool swap_is_has_cache(struct swap_info_struct *si, + unsigned long offset, int nr_pages) +{ + unsigned char *map = si->swap_map + offset; + unsigned char *map_end = map + nr_pages; + + do { + VM_BUG_ON(!(*map & SWAP_HAS_CACHE)); + if (*map != SWAP_HAS_CACHE) + return false; + } while (++map < map_end); + + return true; +} /* * returns number of pages in the folio that backs the swap entry. If positive, @@ -141,12 +165,22 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset, unsigned long flags) { swp_entry_t entry = swp_entry(si->type, offset); + struct address_space *address_space = swap_address_space(entry); + struct swap_cluster_info *ci; struct folio *folio; - int ret = 0; + int ret, nr_pages; + bool need_reclaim; - folio = filemap_get_folio(swap_address_space(entry), swap_cache_index(entry)); + folio = filemap_get_folio(address_space, swap_cache_index(entry)); if (IS_ERR(folio)) return 0; + + /* offset could point to the middle of a large folio */ + entry = folio->swap; + offset = swp_offset(entry); + nr_pages = folio_nr_pages(folio); + ret = -nr_pages; + /* * When this function is called from scan_swap_map_slots() and it's * called by vmscan.c at reclaiming folios. So we hold a folio lock @@ -154,14 +188,50 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, * case and you should use folio_free_swap() with explicit folio_lock() * in usual operations. */ - if (folio_trylock(folio)) { - if ((flags & TTRS_ANYWAY) || - ((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) || - ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio))) - ret = folio_free_swap(folio); - folio_unlock(folio); + if (!folio_trylock(folio)) + goto out; + + need_reclaim = ((flags & TTRS_ANYWAY) || + ((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) || + ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio))); + if (!need_reclaim || !folio_swapcache_freeable(folio)) + goto out_unlock; + + /* + * It's safe to delete the folio from swap cache only if the folio's + * swap_map is HAS_CACHE only, which means the slots have no page table + * reference or pending writeback, and can't be allocated to others. + */ + ci = lock_cluster_or_swap_info(si, offset); + need_reclaim = swap_is_has_cache(si, offset, nr_pages); + unlock_cluster_or_swap_info(si, ci); + if (!need_reclaim) + goto out_unlock; + + if (!(flags & TTRS_DIRECT)) { + /* Free through slot cache */ + delete_from_swap_cache(folio); + folio_set_dirty(folio); + ret = nr_pages; + goto out_unlock; } - ret = ret ? folio_nr_pages(folio) : -folio_nr_pages(folio); + + xa_lock_irq(&address_space->i_pages); + __delete_from_swap_cache(folio, entry, NULL); + xa_unlock_irq(&address_space->i_pages); + folio_ref_sub(folio, nr_pages); + folio_set_dirty(folio); + + spin_lock(&si->lock); + /* Only sinple page folio can be backed by zswap */ + if (!nr_pages) + zswap_invalidate(entry); + swap_entry_range_free(si, entry, nr_pages); + spin_unlock(&si->lock); + ret = nr_pages; +out_unlock: + folio_unlock(folio); +out: folio_put(folio); return ret; } @@ -903,7 +973,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si, if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { int swap_was_freed; spin_unlock(&si->lock); - swap_was_freed = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); + swap_was_freed = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY | TTRS_DIRECT); spin_lock(&si->lock); /* entry was freed successfully, try to use this again */ if (swap_was_freed > 0) @@ -1340,9 +1410,6 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) unsigned long offset = swp_offset(entry); struct swap_cluster_info *ci; struct swap_info_struct *si; - unsigned char *map; - unsigned int i, free_entries = 0; - unsigned char val; int size = 1 << swap_entry_order(folio_order(folio)); si = _swap_info_get(entry); @@ -1350,23 +1417,14 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry) return; ci = lock_cluster_or_swap_info(si, offset); - if (size > 1) { - map = si->swap_map + offset; - for (i = 0; i < size; i++) { - val = map[i]; - VM_BUG_ON(!(val & SWAP_HAS_CACHE)); - if (val == SWAP_HAS_CACHE) - free_entries++; - } - if (free_entries == size) { - unlock_cluster_or_swap_info(si, ci); - spin_lock(&si->lock); - swap_entry_range_free(si, entry, size); - spin_unlock(&si->lock); - return; - } + if (size > 1 && swap_is_has_cache(si, offset, size)) { + unlock_cluster_or_swap_info(si, ci); + spin_lock(&si->lock); + swap_entry_range_free(si, entry, size); + spin_unlock(&si->lock); + return; } - for (i = 0; i < size; i++, entry.val++) { + for (int i = 0; i < size; i++, entry.val++) { if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) { unlock_cluster_or_swap_info(si, ci); free_swap_slot(entry); @@ -1526,16 +1584,7 @@ static bool folio_swapped(struct folio *folio) return swap_page_trans_huge_swapped(si, entry, folio_order(folio)); } -/** - * folio_free_swap() - Free the swap space used for this folio. - * @folio: The folio to remove. - * - * If swap is getting full, or if there are no more mappings of this folio, - * then call folio_free_swap to free its swap space. - * - * Return: true if we were able to release the swap space. - */ -bool folio_free_swap(struct folio *folio) +static bool folio_swapcache_freeable(struct folio *folio) { VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); @@ -1543,8 +1592,6 @@ bool folio_free_swap(struct folio *folio) return false; if (folio_test_writeback(folio)) return false; - if (folio_swapped(folio)) - return false; /* * Once hibernation has begun to create its image of memory, @@ -1564,6 +1611,25 @@ bool folio_free_swap(struct folio *folio) if (pm_suspended_storage()) return false; + return true; +} + +/** + * folio_free_swap() - Free the swap space used for this folio. + * @folio: The folio to remove. + * + * If swap is getting full, or if there are no more mappings of this folio, + * then call folio_free_swap to free its swap space. + * + * Return: true if we were able to release the swap space. + */ +bool folio_free_swap(struct folio *folio) +{ + if (!folio_swapcache_freeable(folio)) + return false; + if (folio_swapped(folio)) + return false; + delete_from_swap_cache(folio); folio_set_dirty(folio); return true; @@ -1640,7 +1706,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr) * to the next boundary. */ nr = __try_to_reclaim_swap(si, offset, - TTRS_UNMAPPED | TTRS_FULL); + TTRS_UNMAPPED | TTRS_FULL); if (nr == 0) nr = 1; else if (nr < 0)