From patchwork Mon Apr 29 19:11:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13647565 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 0FF2BC4345F for ; Mon, 29 Apr 2024 19:13:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94E3C6B00A4; Mon, 29 Apr 2024 15:13:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D6D26B00A5; Mon, 29 Apr 2024 15:13:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 751E06B00A6; Mon, 29 Apr 2024 15:13:33 -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 522E26B00A4 for ; Mon, 29 Apr 2024 15:13:33 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CFC3DA0179 for ; Mon, 29 Apr 2024 19:13:32 +0000 (UTC) X-FDA: 82063518264.21.F606C1A Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf18.hostedemail.com (Postfix) with ESMTP id 2ACF91C0007 for ; Mon, 29 Apr 2024 19:13:30 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aZCjxFZZ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714418011; 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=PY7g2cpmkbLDRH/cw0JP1fam/luoqXTBgJ66zM17H9Q=; b=fRcftqzZiGDz0FbgOxrUK9jc+Uj89tENEBi2ST3CPV7OeYQg44rvzPTb9Itfe9feJMpRK8 4Mll9vwgPmS8VJAVU5TSLQBFwAUIXV5YGW5HZoXgdpygN5vXgy6OcKHDu4fQmA4msZ0HYf BnAtDeUb7HkzUwZmIyN/x7ixGfCk1pw= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aZCjxFZZ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=ryncsn@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714418011; a=rsa-sha256; cv=none; b=PVHpF6aeWlfLS7iYQi+d5Y+WIZXPQH68zgBJp+zBY0IaVD92F9s/TR4B6UqMdj5URHxTWr W4TZFLpIu6vhakVf1/3W/blDTUkaOVOGun9+zJtEwrPZI6hOwpfkgCCvCDjyupUWJfpbG6 ac+/yedxumfFe6+2okTKPpRN0CuWqzY= Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-60274726da6so3280896a12.2 for ; Mon, 29 Apr 2024 12:13:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714418009; x=1715022809; 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=PY7g2cpmkbLDRH/cw0JP1fam/luoqXTBgJ66zM17H9Q=; b=aZCjxFZZuAWyhCm2O2m5FbULFAjjnc5nEYzE9rd6a1retn41VR6rkl+1K8Fb6UpK4M aHppB+/zVZVEMjuqO/T5TjK8tHfe3JC6fZ5udzHt1OBWC7VBat4lsLqqb2eVDkMvse/Q hv7X9qxGZWjWufkmPXcSJRSyc+trNtzZllcpl0H+xTRmUgXDeefwdqXOoXqWUE12gZti 8W9USTuEai5w7SlWv9XxkiwMHSHrL/J+QoF+jCXmVUDcYApNBuJsdwemWETNu8t1guLH SUAIAzHWDY2SYSyYHjoB/JiunmYthn3TE/4rJIYWmnf34AWTNO3hiXDBycUQCWVuCXtL s0IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714418009; x=1715022809; 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=PY7g2cpmkbLDRH/cw0JP1fam/luoqXTBgJ66zM17H9Q=; b=hYvMXADf1OZK5RntUReuDN2KaOv2pTUdkwGfmRtJGcFFHgt6mDVP4rOU1Oj1MrJnWs rK9TGEgRhhMAnY53D5u0viiSbdicvm9EtcVq/K7PVylrBV7l1ZIYnpyxCg30Tt5N0Z4z xjEPGt9dKXXkF48cTQGokwnd8nhOcIhe02SXrC5tnXNl2OdzdzxOT99g8cQquhWeFvob FKzhtHurumhnP0xSi97oBiWzwaxIGPdCme242WK1nRj17DG/MsEe1BZLWo+wia5CGE0X Tg58XKAlaN25Zj91JF3j897UVYM3zekiC7jk2xO8qJGBRsdSBOH4XdyxpvT5wG/rGeGj BuzA== X-Gm-Message-State: AOJu0YwNoUnVSV+es3ZYwEQhrkre7L4MerhDBpwG8Sj5PyVQGkSuY93g uDip3R+2bdLTbbFsM1HCPaQbXf5W22OZWn3LDSX8avtGwO1f5rEe/Ha0puJi0Bw= X-Google-Smtp-Source: AGHT+IExOJ/p/bJW9q/f21ksPY2gdiW8nSTfaSxNTc1Poa54zyXrIO+xTyTWMo/yh/qLCiK2/+zrZA== X-Received: by 2002:a05:6a20:4386:b0:1a7:ae5d:5fb4 with SMTP id i6-20020a056a20438600b001a7ae5d5fb4mr15127477pzl.28.1714418009348; Mon, 29 Apr 2024 12:13:29 -0700 (PDT) Received: from KASONG-MB2.tencent.com ([1.203.116.31]) by smtp.gmail.com with ESMTPSA id e10-20020aa7980a000000b006ed38291aebsm20307988pfl.178.2024.04.29.12.13.25 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 29 Apr 2024 12:13:28 -0700 (PDT) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , "Huang, Ying" , Matthew Wilcox , Chris Li , Barry Song , Ryan Roberts , Neil Brown , Minchan Kim , Hugh Dickins , David Hildenbrand , Yosry Ahmed , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v3 12/12] mm/swap: reduce swap cache search space Date: Tue, 30 Apr 2024 03:11:38 +0800 Message-ID: <20240429191138.34123-5-ryncsn@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240429190500.30979-1-ryncsn@gmail.com> References: <20240429190500.30979-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 2ACF91C0007 X-Stat-Signature: fcmpu5k8nfh58ij51fxhe81ubx9j6bd1 X-Rspam-User: X-HE-Tag: 1714418010-541439 X-HE-Meta: U2FsdGVkX18ULJNSyW4eVDpL727rRzjKtQo3/IEXPns2Nx5tkb+1uxspK1idGbodz7ihBdFvKzaaUUZ4Flf0D7xl6KaM3SVocP1iix0hhnDBRFDEEsUWAJp96fzxJFBUlJTmBeJLUK+hkdoqbPKtP/5kYyJVoNbxH/JnybyYMO+IsEZb7oiWepG6r4/JCirjlnEnH7PqXkdivlsD0bYZ/04cb3I61jL5Nw4NoBZTxSzVxTlB6/GG8F7htulOvQ3qDqFR7eHwYgG1x/z88bd3YtxFz5CSu9ysj3dPnYLR6sDNp4OCbE/x8hbjqMFctzQgw79753aSlZPCOq/TEzeGyCkf95wYGJbDENCqy5IU+Lm3U2BeKy2hqZ2HTDDdBAZhSVm4/gzT7Y3+0XDtLqqfisLTVCT/8p6hfzJVsl1GMMuhwt1IjXOJPt4vrwncSM1gaphISoRNEknfyMeKvQxTqcZANG2dls5tQSO7Wwp03VnaDpnATOjwjvrVWfZ6+BYklNqJNr86C/Totc9NRXqI7qZHA+GI0k+k3eZqPl4QUFibhBgwYuXnmpmUsSCO74xhOVengnAElIXlgjmPoLl12u+DEAIlaz11jbKG8FDQ4syOwl3C9n3SM4bcaO76oPzih4dwzrXrmFt/9MB0CJl0JxzQIpRAa79wFZqTOqlN08eDSghQE2R4a96KgofrzbmTAFCD2NtlOTXYNatRKfeh3rvax2X7y9TDT3u4G+ESiBccPy3poM8FfIqgNybPujDYaK5FPv74p90bGpTMjGkDwAc5mEha3LehcwN/G0RXBypDP7JYgboF3xB39heMccwbLMb9nm/6AtQc5CE+g8vKYqCiD8EXZlf6ydeqx+IMl4ivp4IWt8bzHL7V/oiZsUkUUrtDU7jU/PUWpoEYgKVWY2HHsRTGC1DUy2tUO9O8vC+9D30EHr9YFLYLNimsb7l4/1XvwraYNIwKI0cDX/9 MG44ydzm 7+8OMv6Jw0WR+lCx7Mk/1wocXxpplCULRr8e3eV/iYVhzo7b20fbHKs71D1Hk0KWPT+VaAQDKqOq6e8tDDvqR1A8jS5HuItkdssFj9KwyLY1xxhjF/0CBs/x/EIfFsMJi4B8zyyC2cIUNB8o5OSN9FU8Brj/9bAOWek0wxROapcKCUHQwOVE1VKneJTYbKmIGxweNvG4E60tvU6KuFbx6YDaBxV9wzZt2J7q5SwuqvYbvRw6xEf1UyOgG3jql6mrzpMJHrHAKX8gY8uY= 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 use one swap_address_space for every 64M chunk to reduce lock contention, this is like having a set of smaller swap files inside one big swap file. But when doing swap cache look up or insert, we are still using the offset of the whole large swap file. This is OK for correctness, as the offset (key) is unique. But Xarray is specially optimized for small indexes, it creates the radix tree levels lazily to be just enough to fit the largest key stored in one Xarray. So we are wasting tree nodes unnecessarily. For 64M chunk it should only take at most 3 levels to contain everything. But we are using the offset from the whole swap file, so the offset (key) value will be way beyond 64M, and so will the tree level. Optimize this by using a new helper swap_cache_index to get a swap entry's unique offset in its own 64M swap_address_space. I see a ~1% performance gain in benchmark and actual workload with high memory pressure. Test with `time memhog 128G` inside a 8G memcg using 128G swap (ramdisk with SWP_SYNCHRONOUS_IO dropped, tested 3 times, results are stable. The test result is similar but the improvement is smaller if SWP_SYNCHRONOUS_IO is enabled, as swap out path can never skip swap cache): Before: 6.07user 250.74system 4:17.26elapsed 99%CPU (0avgtext+0avgdata 8373376maxresident)k 0inputs+0outputs (55major+33555018minor)pagefaults 0swaps After (1.8% faster): 6.08user 246.09system 4:12.58elapsed 99%CPU (0avgtext+0avgdata 8373248maxresident)k 0inputs+0outputs (54major+33555027minor)pagefaults 0swaps Similar result with MySQL and sysbench using swap: Before: 94055.61 qps After (0.8% faster): 94834.91 qps Radix tree slab usage is also very slightly lower. Signed-off-by: Kairui Song --- mm/huge_memory.c | 2 +- mm/memcontrol.c | 2 +- mm/mincore.c | 2 +- mm/shmem.c | 2 +- mm/swap.h | 15 +++++++++++++++ mm/swap_state.c | 12 ++++++------ mm/swapfile.c | 6 +++--- 7 files changed, 28 insertions(+), 13 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 8261b5669397..d0c6d30d72f2 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2841,7 +2841,7 @@ static void __split_huge_page(struct page *page, struct list_head *list, split_page_memcg(head, order, new_order); if (folio_test_anon(folio) && folio_test_swapcache(folio)) { - offset = swp_offset(folio->swap); + offset = swap_cache_index(folio->swap); swap_cache = swap_address_space(folio->swap); xa_lock(&swap_cache->i_pages); } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 602ad5faad4d..8a75eb6c86cc 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5983,7 +5983,7 @@ static struct page *mc_handle_swap_pte(struct vm_area_struct *vma, * Because swap_cache_get_folio() updates some statistics counter, * we call find_get_page() with swapper_space directly. */ - page = find_get_page(swap_address_space(ent), swp_offset(ent)); + page = find_get_page(swap_address_space(ent), swap_cache_index(ent)); entry->val = ent.val; return page; diff --git a/mm/mincore.c b/mm/mincore.c index dad3622cc963..e31cf1bde614 100644 --- a/mm/mincore.c +++ b/mm/mincore.c @@ -139,7 +139,7 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, } else { #ifdef CONFIG_SWAP *vec = mincore_page(swap_address_space(entry), - swp_offset(entry)); + swap_cache_index(entry)); #else WARN_ON(1); *vec = 1; diff --git a/mm/shmem.c b/mm/shmem.c index fa2a0ed97507..326315c12feb 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1756,7 +1756,7 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, old = *foliop; entry = old->swap; - swap_index = swp_offset(entry); + swap_index = swap_cache_index(entry); swap_mapping = swap_address_space(entry); /* diff --git a/mm/swap.h b/mm/swap.h index 82023ab93205..93e3e1b58a7f 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -27,6 +27,7 @@ void __swap_writepage(struct folio *folio, struct writeback_control *wbc); /* One swap address space for each 64M swap space */ #define SWAP_ADDRESS_SPACE_SHIFT 14 #define SWAP_ADDRESS_SPACE_PAGES (1 << SWAP_ADDRESS_SPACE_SHIFT) +#define SWAP_ADDRESS_SPACE_MASK (BIT(SWAP_ADDRESS_SPACE_SHIFT) - 1) extern struct address_space *swapper_spaces[]; #define swap_address_space(entry) \ (&swapper_spaces[swp_type(entry)][swp_offset(entry) \ @@ -40,6 +41,15 @@ static inline loff_t swap_dev_pos(swp_entry_t entry) return ((loff_t)swp_offset(entry)) << PAGE_SHIFT; } +/* + * Return the swap cache index of the swap entry. + */ +static inline pgoff_t swap_cache_index(swp_entry_t entry) +{ + BUILD_BUG_ON((SWP_OFFSET_MASK | SWAP_ADDRESS_SPACE_MASK) != SWP_OFFSET_MASK); + return swp_offset(entry) & SWAP_ADDRESS_SPACE_MASK; +} + void show_swap_cache_info(void); bool add_to_swap(struct folio *folio); void *get_shadow_from_swap_cache(swp_entry_t entry); @@ -86,6 +96,11 @@ static inline struct address_space *swap_address_space(swp_entry_t entry) return NULL; } +static inline pgoff_t swap_cache_index(swp_entry_t entry) +{ + return 0; +} + static inline void show_swap_cache_info(void) { } diff --git a/mm/swap_state.c b/mm/swap_state.c index 642c30d8376c..9994b8d17741 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -72,7 +72,7 @@ void show_swap_cache_info(void) void *get_shadow_from_swap_cache(swp_entry_t entry) { struct address_space *address_space = swap_address_space(entry); - pgoff_t idx = swp_offset(entry); + pgoff_t idx = swap_cache_index(entry); void *shadow; shadow = xa_load(&address_space->i_pages, idx); @@ -89,7 +89,7 @@ int add_to_swap_cache(struct folio *folio, swp_entry_t entry, gfp_t gfp, void **shadowp) { struct address_space *address_space = swap_address_space(entry); - pgoff_t idx = swp_offset(entry); + pgoff_t idx = swap_cache_index(entry); XA_STATE_ORDER(xas, &address_space->i_pages, idx, folio_order(folio)); unsigned long i, nr = folio_nr_pages(folio); void *old; @@ -144,7 +144,7 @@ void __delete_from_swap_cache(struct folio *folio, struct address_space *address_space = swap_address_space(entry); int i; long nr = folio_nr_pages(folio); - pgoff_t idx = swp_offset(entry); + pgoff_t idx = swap_cache_index(entry); XA_STATE(xas, &address_space->i_pages, idx); xas_set_update(&xas, workingset_update_node); @@ -350,7 +350,7 @@ struct folio *swap_cache_get_folio(swp_entry_t entry, { struct folio *folio; - folio = filemap_get_folio(swap_address_space(entry), swp_offset(entry)); + folio = filemap_get_folio(swap_address_space(entry), swap_cache_index(entry)); if (!IS_ERR(folio)) { bool vma_ra = swap_use_vma_readahead(); bool readahead; @@ -420,7 +420,7 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping, si = get_swap_device(swp); if (!si) return ERR_PTR(-ENOENT); - index = swp_offset(swp); + index = swap_cache_index(swp); folio = filemap_get_folio(swap_address_space(swp), index); put_swap_device(si); return folio; @@ -447,7 +447,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, * that would confuse statistics. */ folio = filemap_get_folio(swap_address_space(entry), - swp_offset(entry)); + swap_cache_index(entry)); if (!IS_ERR(folio)) goto got_folio; diff --git a/mm/swapfile.c b/mm/swapfile.c index 6f028262898b..81bd61d0a7a6 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -142,7 +142,7 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, struct folio *folio; int ret = 0; - folio = filemap_get_folio(swap_address_space(entry), offset); + folio = filemap_get_folio(swap_address_space(entry), swap_cache_index(entry)); if (IS_ERR(folio)) return 0; /* @@ -2158,7 +2158,7 @@ static int try_to_unuse(unsigned int type) (i = find_next_to_unuse(si, i)) != 0) { entry = swp_entry(type, i); - folio = filemap_get_folio(swap_address_space(entry), i); + folio = filemap_get_folio(swap_address_space(entry), swap_cache_index(entry)); if (IS_ERR(folio)) continue; @@ -3476,7 +3476,7 @@ EXPORT_SYMBOL_GPL(swapcache_mapping); pgoff_t __folio_swap_cache_index(struct folio *folio) { - return swp_offset(folio->swap); + return swap_cache_index(folio->swap); } EXPORT_SYMBOL_GPL(__folio_swap_cache_index);