From patchwork Thu May 2 08:49: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: 13651438 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 13715C4345F for ; Thu, 2 May 2024 08:50:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DE1F6B00A2; Thu, 2 May 2024 04:50:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9672F6B00A3; Thu, 2 May 2024 04:50:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B9846B00A4; Thu, 2 May 2024 04:50:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5B8026B00A2 for ; Thu, 2 May 2024 04:50:49 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2280F1C0C14 for ; Thu, 2 May 2024 08:50:44 +0000 (UTC) X-FDA: 82072835208.12.E535637 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf07.hostedemail.com (Postfix) with ESMTP id 47D2340020 for ; Thu, 2 May 2024 08:50:42 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KN0h02OV; spf=pass (imf07.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.178 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=1714639842; 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=3PiDuUAmL04AUzqx0Yl3XvlHAjrbgNtWVpZDxU2pdrk=; b=krqMYp3BfjG/E0X1/SKCDZ0iDx79MeLkwIxIxpMz9YVnJDTckAS8ew5Nwc1TRSHeaehfee Zdx4cExN+bpcwFidGvfC5w8ho11v/wvMpRlUuHPeIB9KfYrUM1NP4tEMHwhykHH8cpMvtb EwEhKYBhTXS2f6oeERRCtak9X6+ztXk= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KN0h02OV; spf=pass (imf07.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.178 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=1714639842; a=rsa-sha256; cv=none; b=unoEj9aYf7TNpyGMpKXVMDBmUGm+cdH+NnZpnjvZ7hVMXQaVEQHTT9oxCrmLVeGaPT/Bfl u52Gp5njhlKXE0rn6iTQUZzrqvE8njfrB0lIyM8hgL1aaJ0tDM1Ftangfu6zSz+KDiATUo DigD5VHtkZEcLqAmzQgNSZ/PAC0lGYU= Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1ec486198b6so22128715ad.1 for ; Thu, 02 May 2024 01:50:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714639840; x=1715244640; 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=3PiDuUAmL04AUzqx0Yl3XvlHAjrbgNtWVpZDxU2pdrk=; b=KN0h02OVKUkV6tl58KwmKsVkBeVlCefrxbtVDml2QWqKJkPC6GbQqHzc0cYj0TgYWa 9fjnsXvzcMr57M9ET5yru+aciIQJyWimbkmVxStN00PX2FeYeq163b5HAn2qeffCCGJh 8U5OmLcJSv2cdsQdKJY/5VDLrYmn0j966nc32R9qg5x35cXjjp0jIBoMPW1SIV3eHZn4 DFXhrP8OT6IxdV7bJV8KuAn4bI9ZL/rNYdlEyhMJ01XhnfD/lk+Y1ky4ase/IdFKspWg RNuWnVWhuT3o7uyUU3a89SRE4jVTtQKd9lGrohT97iwJKiZ58eMzo0JVn+C0L9gf/28q n3kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714639840; x=1715244640; 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=3PiDuUAmL04AUzqx0Yl3XvlHAjrbgNtWVpZDxU2pdrk=; b=kAJsdBDGZgkcTRarBK9i+AjSTVPoKvU4xgH/LBU3mYr1bJOju3z0aKRGsc6S/EOZuN Z8RF9oKwS0RAJlVctMDEcEqBtS+8W48j7em7UK055EgCP5V489xZ0BhRluLrZc1vP1yD +5jHe7LmHHi37OD9o0v3cCQSWfZW9t7qMtGkhYKOOFfgcdTUWJ0ZK8M7ZUrf3FfQvluY ViuArFKmQwyBOKKepeuLWi645vpiH7LzAlZkI/jWdlSakcm6mVFns147XogZRvTFYl9b 6a1bRFsSIKylBh5kLihAUz8GWxKXdHttt56YC5b8dqGt7y3QMuTwA1SoyeT6497TrGXm TRjQ== X-Gm-Message-State: AOJu0YxKEFChFaT+X6tdAQdlYZRIl1MOScS2o02Xe42yqJKEeo2Bm6vD ZsO5fhnF/wtHT3WmJFn2+K9JC8QdCEUdR19+J6cLKbqXBr+hvni1xZpl36UaA2Q= X-Google-Smtp-Source: AGHT+IFQrOIhO4DW3kC/6U9SsFXYwY7es2c4HumW8eBEkpYQadnethvvJ5+C7OMiTTPsFpu/GER8Pg== X-Received: by 2002:a17:903:246:b0:1e7:8d21:7fd7 with SMTP id j6-20020a170903024600b001e78d217fd7mr6287081plh.28.1714639840392; Thu, 02 May 2024 01:50:40 -0700 (PDT) Received: from KASONG-MB2.tencent.com ([1.203.116.31]) by smtp.gmail.com with ESMTPSA id j12-20020a170903024c00b001e43576a7a1sm737712plh.222.2024.05.02.01.50.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 02 May 2024 01:50:38 -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 v4 12/12] mm/swap: reduce swap cache search space Date: Thu, 2 May 2024 16:49:39 +0800 Message-ID: <20240502084939.30250-5-ryncsn@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240502084609.28376-1-ryncsn@gmail.com> References: <20240502084609.28376-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Queue-Id: 47D2340020 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: nqsewzbncr1no8keskbzqzfeqdexr37z X-HE-Tag: 1714639842-416211 X-HE-Meta: U2FsdGVkX1++gajPFJ3TYJ/OXV7wdzPsyXq/dShObCZmDvwLatAK/hg4jyeEZf/y5670Ba4aPl72i+SAO0vSflLSOcoYMOxWcIloGm3UCci/CssOizV458QIOWoiUV94OPv5B9p909hVscqJrBUuFwX7oiCFoBbYyRJ8XQZHqms/8UoFhGokEx1/U3MZmFH1mK2p81HRlEcOVwGp9efWu9+iKkY833d9bsL1gJFSn/RDDs/XdSspt67e8ztky4ie+SZKP9smVaZfkaTJ/Lbkd2CVqqH17vG+xPhS3aZ9aZHjXpNoKKBwq3MTdRhOf+vgi/2rNVBHCcQS4KffgG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C8vV3DdYFNn+w3oGQRtNNXp3eD/Q9yn+wR1FoB65UW8hVkR8a1xm0rQd8YwJOCh8/i1bYxwBXQowMeIznV6J5+hjN2u4fRa38//1IWCHJFCieUSlQ1MXgGWK4hlwaKC4OXBoo6ZnhACNCF4F7DdxdyWfDNGtFeF31pYUxZDqHplEQ6SqnxZM1DALEnlg2+VAOQ0mBT7qM1cqWdko94RtSbVK3J 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 swap device. But when doing swap cache look up or insert, we are still using the offset of the whole large swap device. 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 if we are using the offset from the whole swap device, 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 | 19 ++++++++++--------- mm/swapfile.c | 6 +++--- 7 files changed, 32 insertions(+), 16 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index d35d526ed48f..45829cc049d2 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2918,7 +2918,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 d11536ef59ef..81b005c459cb 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6165,7 +6165,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..09415d4c7843 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); @@ -248,18 +248,19 @@ void delete_from_swap_cache(struct folio *folio) void clear_shadow_from_swap_cache(int type, unsigned long begin, unsigned long end) { - unsigned long curr = begin; + unsigned long curr = begin, offset; void *old; for (;;) { + offset = curr & SWAP_ADDRESS_SPACE_MASK; swp_entry_t entry = swp_entry(type, curr); struct address_space *address_space = swap_address_space(entry); - XA_STATE(xas, &address_space->i_pages, curr); + XA_STATE(xas, &address_space->i_pages, offset); xas_set_update(&xas, workingset_update_node); xa_lock_irq(&address_space->i_pages); - xas_for_each(&xas, old, end) { + xas_for_each(&xas, old, offset + min(end - curr, SWAP_ADDRESS_SPACE_PAGES)) { if (!xa_is_value(old)) continue; xas_store(&xas, NULL); @@ -350,7 +351,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 +421,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 +448,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 0b0ae6e8c764..4f0e8b2ac8aa 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);