From patchwork Tue Apr 23 17:03:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13640395 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 C66F5C4345F for ; Tue, 23 Apr 2024 17:04:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AA636B014F; Tue, 23 Apr 2024 13:04:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 433A26B0151; Tue, 23 Apr 2024 13:04:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 25F776B0152; Tue, 23 Apr 2024 13:04:30 -0400 (EDT) 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 F0C466B014F for ; Tue, 23 Apr 2024 13:04:29 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8FB84160886 for ; Tue, 23 Apr 2024 17:04:29 +0000 (UTC) X-FDA: 82041420258.30.5E9819F Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf18.hostedemail.com (Postfix) with ESMTP id 96EA41C0013 for ; Tue, 23 Apr 2024 17:04:27 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nVpEwD1K; spf=pass (imf18.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.44 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=1713891867; 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=CnMzyp4v7D+bPlM4VXqNTLQwTcA8u3vvnjSU90ZgW/k=; b=NWt3o/1hbTmJjG4V7A+OGCjoUH7zDdnma2++KMo6QtSDsI7/iDqM3rx2bRYpGlkLmleiuH jFOMC+XGJBm37Ybytm/gDvsA7CV5TU5P3ZmWCTp781xfbNjbUcozMxDS611zZb0ZQOdEF5 u8yoQSTRhLLujrOkoQmZ4gQOMRi9yY4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713891867; a=rsa-sha256; cv=none; b=iFXvr0m8h2VEfFJ6MoKDE9QmuM6+UJDzCfOqELerjGlmUvvizZTsQA4lEULjF5ArzOoiZC nh2Aze+nAAmpN+wELtEI3tSkiyP2bRYyatXIYTaj08QQ+S3XLNZHgwMl3tMGACwf2t6X0C KLUlg22Zku/w+OHdGQ7d9Dd6cuuGyrM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nVpEwD1K; spf=pass (imf18.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2a78c2e253aso4679456a91.3 for ; Tue, 23 Apr 2024 10:04:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713891866; x=1714496666; 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=CnMzyp4v7D+bPlM4VXqNTLQwTcA8u3vvnjSU90ZgW/k=; b=nVpEwD1K0nIJt7KQ7NtjxKn0KbV+O62FsEyU0qBM/aaGV/9sYOqsPL+AzfG+4A/N6/ 36wZlp1uDbKXYmnCq7UfkqkaIHJDg8CnOKQj4lT7VMiYJePZrzMckCpUbT/reqyCsa56 g3dY4j7aezugU8jnfeo0lvabURJLLcdrn0J5m3WSjCBEfZT8H6+pQiQC8wkH55emsnv+ pmMT1Ctm1+butlKPLEkYHm6tVO7eHsWoVnsZ+UkV29jDR9xAWysHHO+dJ86jqoWxyS/k 9Lda7c1e3BO3RgX0hZJnEjetXGVWqi/yzmzQhaZ4KvbUqQgQvpoYcvdnuaw+Fy4RfGMM Gcqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713891866; x=1714496666; 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=CnMzyp4v7D+bPlM4VXqNTLQwTcA8u3vvnjSU90ZgW/k=; b=XM+KYCkmajxnxRTHD1qoSaO/cAUX+1di6aXMSRMvISf4fab1kAxELZv+Vq5rApYNE6 C6xQyN3daYZCdFdMFo5mvvk6PZ0IxyPi29QxAt2BcJY5vPIK1WPNJN9v5p7v8r8oizrH SZLQfNsJZK/pJRHYe3tQsTlIRM2S+K5ced0iJ6HuoC1kiadKQsfnRiKOt9cBGlGHhGV+ V66VxmtA3udq2yqPP8QPsI7s4SO0ugVsX3Z24USGnzHhe7FWXQjjUGweWtl0cdjkHmMT zl0xcmIuTlJ0TbPxweTl6XzjHaD5jt4Ig0tVFuHZ08CuV/IDWswlNCNLmEDb/oyfxYQT iT9Q== X-Gm-Message-State: AOJu0YxqzJqqiKvRJVBFCwHjv/summxyIU7f7mL5fTS//eSKaSzJ5Zqb YXrDz/zgJkRickX6du5VKc21v0vjWXlFFBITwGv+fERhQrcMdFMcpHaHOi84jStfCg== X-Google-Smtp-Source: AGHT+IHsdf0a0hXaosoeNJct0li+Z9JISuE3KsVNLbRolhiRYgiN4NDjYwA6cqx8BtEiKf1+SehY8w== X-Received: by 2002:a17:90b:3506:b0:2a7:e71:eb72 with SMTP id ls6-20020a17090b350600b002a70e71eb72mr37270pjb.0.1713891866020; Tue, 23 Apr 2024 10:04:26 -0700 (PDT) Received: from KASONG-MB2.tencent.com ([1.203.116.31]) by smtp.gmail.com with ESMTPSA id s19-20020a17090a881300b002a5d684a6a7sm9641148pjn.10.2024.04.23.10.04.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 23 Apr 2024 10:04:25 -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 v2 8/8] mm/swap: reduce swap cache search space Date: Wed, 24 Apr 2024 01:03:39 +0800 Message-ID: <20240423170339.54131-9-ryncsn@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240423170339.54131-1-ryncsn@gmail.com> References: <20240423170339.54131-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Stat-Signature: j9nuwsen6cgfrn4o76i7ewscitdo6hr5 X-Rspamd-Queue-Id: 96EA41C0013 X-Rspamd-Server: rspam06 X-Rspam-User: X-HE-Tag: 1713891867-341551 X-HE-Meta: U2FsdGVkX18vBYj8LN8OT4cFNnp84IA2dslJ8z7xvNOT/6YPe73Ol8vjGcWDZFhEmc8ZOtEXpEU+RkILSnYG9dlUf1vIUssu8nNDfKROGgD2y4G64tvuQHSl0Pn3CQXhDpyeL55HejCz4t8Yp1Cr4AD/VR2OYuM2kskU1Ch6Y94zEp9zB0CAbQSMbQS4tzWa8TonxYHKoTxF7DyqvJlG8R7nJKyy51w6iAcpybwAm6v3g0+ccFe8b3n4kriDtbv09ALgLCNy2dfjeVL0a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uSRjkXxwV6kyFFbujFfTlTdXlgwU5aHXtcPMKaa4AFwVA+QfJh1p07RBCuPsIgpqzd+hnq/4Z5swauiP6eZD7aomXM2tVen9vfZM25kgfKJGTMTQr+6L0r8ogRzjjJikNWrK9amYdNpM+60Ta1mriAtMcmEZpIptJPp3Jm59g+TdZvmQdUOJcZmd/F0wke+WiVp88tl+DcTdKa7izQZEDjmNgIYHHklAmRUvaaJ+I3U9F9Kx5/3udnpWjBjxrD2vHlIdNx3t9suOUCG7s6Epcu6j7o 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 9859aa4f7553..1208d60792f0 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2903,7 +2903,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 fabce2b50c69..04d7be7f30dc 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5934,7 +5934,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 0aad0d9a621b..cbe33ab52a73 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1762,7 +1762,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 bfc7e8c58a6d..9dbb54c72770 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); struct page *page; page = 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 2387f5e131d7..3d838659f55f 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -138,7 +138,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; /* @@ -2110,7 +2110,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; @@ -3421,7 +3421,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);