From patchwork Wed Mar 27 14:45:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13606748 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 502B4CD11DD for ; Wed, 27 Mar 2024 14:45:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9BC86B0087; Wed, 27 Mar 2024 10:45:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D25A16B0098; Wed, 27 Mar 2024 10:45:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BC6C86B009B; Wed, 27 Mar 2024 10:45:51 -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 9B6286B0087 for ; Wed, 27 Mar 2024 10:45:51 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 561C8120548 for ; Wed, 27 Mar 2024 14:45:51 +0000 (UTC) X-FDA: 81943093302.23.73955A1 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf14.hostedemail.com (Postfix) with ESMTP id 51F44100007 for ; Wed, 27 Mar 2024 14:45:49 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711550749; a=rsa-sha256; cv=none; b=YNXrbRaNvrvhpoH9UCI+TlcBZ0xLJ1RTy+QqMOf4Scf0xfW3C0DbSmojQjKBO+2FUcgWAZ 6hlHWHjF0PgT0bghecqSbAS9xvSgMZz5YD7FPTi9GRl9ndpY7k8rQH8BNHP32f1fKR0sZM /87i8rTSJYJNzr+57uaz7fTZ0ITj1TM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711550749; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=UtU/1FNUweilsgQ+mXalknTzajaDsyABr3xrflvB3Fo=; b=UDvyxf8MZUPnMOvZByebK+rmyjH1BEYbPjX+MYBRaDrwU483YsP0NV1BPUK04KscE8jlY6 +5GMYt62r1n41m6VuFONxZiB3hx/VTVvbrIjsp+3pDaQg0AR7FuDhGZOFsBse3agpf5fMa XzjR4X2TW7eJIJ5i5YjZujssbY/Uw7E= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6BA2C2F4; Wed, 27 Mar 2024 07:46:22 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 703203F64C; Wed, 27 Mar 2024 07:45:46 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , David Hildenbrand , Matthew Wilcox , Huang Ying , Gao Xiang , Yu Zhao , Yang Shi , Michal Hocko , Kefeng Wang , Barry Song <21cnbao@gmail.com>, Chris Li , Lance Yang Cc: Ryan Roberts , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 0/6] Swap-out mTHP without splitting Date: Wed, 27 Mar 2024 14:45:31 +0000 Message-Id: <20240327144537.4165578-1-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 51F44100007 X-Stat-Signature: rd4zaaju6mejw1tyuu5dhe53s8g8q1y4 X-HE-Tag: 1711550749-581535 X-HE-Meta: U2FsdGVkX19QgemwYoWqaslH0gWfvZEPMe57W0zPlNnjRharj2hQXYe3ZMsvnuPu6VOVuRBIVHDNN3MDFr+IY2ACm7mNbIPPsRHx2u/s09xqZkXqr+UjlczjCS5RFduozVW4k0x6kLeiOvbaWPzvqGMt2OI/OlVNLqrduf6ldmuL5IDhDv1tVRl0Bl0RQvoGrCznhFUiCTn4s/uHf+tO0hCmS0KJVTqzfF14IoP/tnnMVbUM+aVtuVzModq5QKyQbDWRoxp75tZcZp9VcRT7bx+2JO5SOo4aGcWE9zgosiQUrIzo5dmOTQhGOfgrtunVbsz4IP2SEB3JqLQv/rrJMGnQ9WQ/vQhYFMHpCuLc6XQX5FyqssbBVQrh3XbJ29F4lCm4v5gm9gR1RcDxS/1DFs3KfP8d7I5APiJSyFjxTTQ3DpaoxG3uoJCJKkKhOcByU9A33sHlwfmqUW0xgFwCUpXDt8/cjgcQEWD6C1jn0WdhLuqFQ8ZYecDnayRcTWAUm3LeX18yGDjLi8YdAOoSi89x5nlKP+9xoAvwwSQQYGXVNo4YJ6zAJM0tAnTsf/rM0YxPPA3G35SF1ZERTEzV9B8VnudfFCzs4WgpzGslRTByCCXNol72k91GlXsyhLfC9O7Mqz4lu/fUY58yr79dpOQFVPq29TyHyDvGJHXw3RSeZ0QpaE6BiFxxpcBCgIVOeHFvZpNqnmdWP0MClTljlcg5pIsSJqAtcZmOSbLFmg7Fn0UR0XAIt3+oOJG7KuTYfHhbgfwbPQQGpr2HeP6tMziCOoUpdhaj5KpoRu2Ylpjs7MUTSeyl7Po7Zc5gzJio4Q54dhbzAoQNyH16TlZsX4aFIYlvNAMui4ExCOPyGy8V0VGZrl0HNr/tspzpqOlDp/t1B042ErhSQZpWMpJNsTmTEucy3HHhPyNSh5dkxuEJbhH++AfkGa21rztpU6xCKoIGm3UGtvhtci3wsGH dRFpGwbn vCBpbKFs1ifR7yy5+3JH917alE2Zf/LsactRoKahT9Df4Fu4/9k73Rc1b21q4Gh4J3DkSEQCFVUKEhkLkYdwmZA0iti+2K7EiosQBxdAMj/U5uA6McEw37tpCxMfDqqve2nHMx851LoDgPLsUntgOep80PIv49+wOVKKtwR4sFvIbNDuGQ8MpI+/++OPiXF8BwirqlyvRFXryFdJBIUygimJSIaPpxF5C+5HOjix3mIKYonISExjTTCuLkw== 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: Hi All, This series adds support for swapping out multi-size THP (mTHP) without needing to first split the large folio via split_huge_page_to_list_to_order(). It closely follows the approach already used to swap-out PMD-sized THP. There are a couple of reasons for swapping out mTHP without splitting: - Performance: It is expensive to split a large folio and under extreme memory pressure some workloads regressed performance when using 64K mTHP vs 4K small folios because of this extra cost in the swap-out path. This series not only eliminates the regression but makes it faster to swap out 64K mTHP vs 4K small folios. - Memory fragmentation avoidance: If we can avoid splitting a large folio memory is less likely to become fragmented, making it easier to re-allocate a large folio in future. - Performance: Enables a separate series [5] to swap-in whole mTHPs, which means we won't lose the TLB-efficiency benefits of mTHP once the memory has been through a swap cycle. I've done what I thought was the smallest change possible, and as a result, this approach is only employed when the swap is backed by a non-rotating block device (just as PMD-sized THP is supported today). Discussion against the RFC concluded that this is sufficient. Performance Testing =================== I've run some swap performance tests on Ampere Altra VM (arm64) with 8 CPUs. The VM is set up with a 35G block ram device as the swap device and the test is run from inside a memcg limited to 40G memory. I've then run `usemem` from vm-scalability with 70 processes, each allocating and writing 1G of memory. I've repeated everything 6 times and taken the mean performance improvement relative to 4K page baseline: | alloc size | baseline | + this series | | | mm-unstable (~v6.9-rc1) | | |:-----------|------------------------:|------------------------:| | 4K Page | 0.0% | 1.3% | | 64K THP | -13.6% | 46.3% | | 2M THP | 91.4% | 89.6% | So with this change, the 64K swap performance goes from a 14% regression to a 46% improvement. While 2M shows a small regression I'm confident that this is just noise. --- The series applies against mm-unstable (4e567abb6482) with the addition of a small fix for an arm64 build break (reported at [6]). Changes since v4 [4] ==================== - patch #3: - Added R-B from Huang, Ying - thanks! - patch #4: - get_swap_pages() now takes order instead of nr_pages (per Huang, Ying) - Removed WARN_ON_ONCE() from get_swap_pages() - Reworded comment for scan_swap_map_try_ssd_cluster() (per Huang, Ying) - Unified VM_WARN_ON()s in scan_swap_map_slots() to scan: (per Huang, Ying) - Removed redundant "order == 0" check (per Huang, Ying) - patch #5: - Marked list_empty() check with data_race() (per David) - Added R-B from Barry and David - thanks! - patch #6: - Implemented mkold_ptes() generic helper (pre David) - Enhanced folio_pte_batch() to report any_young (per David) - madvise_cold_or_pageout_pte_range() sets old in batch (per David) - Added R-B from Barry - thanks! Changes since v3 [3] ==================== - Renamed SWAP_NEXT_NULL -> SWAP_NEXT_INVALID (per Huang, Ying) - Simplified max offset calculation (per Huang, Ying) - Reinstated struct percpu_cluster to contain per-cluster, per-order `next` offset (per Huang, Ying) - Removed swap_alloc_large() and merged its functionality into scan_swap_map_slots() (per Huang, Ying) - Avoid extra cost of folio ref and lock due to removal of CLUSTER_FLAG_HUGE by freeing swap entries in batches (see patch 2) (per DavidH) - vmscan splits folio if its partially mapped (per Barry Song, DavidH) - Avoid splitting in MADV_PAGEOUT path (per Barry Song) - Dropped "mm: swap: Simplify ssd behavior when scanner steals entry" patch since it's not actually a problem for THP as I first thought. Changes since v2 [2] ==================== - Reuse scan_swap_map_try_ssd_cluster() between order-0 and order > 0 allocation. This required some refactoring to make everything work nicely (new patches 2 and 3). - Fix bug where nr_swap_pages would say there are pages available but the scanner would not be able to allocate them because they were reserved for the per-cpu allocator. We now allow stealing of order-0 entries from the high order per-cpu clusters (in addition to exisiting stealing from order-0 per-cpu clusters). Changes since v1 [1] ==================== - patch 1: - Use cluster_set_count() instead of cluster_set_count_flag() in swap_alloc_cluster() since we no longer have any flag to set. I was unable to kill cluster_set_count_flag() as proposed against v1 as other call sites depend explicitly setting flags to 0. - patch 2: - Moved large_next[] array into percpu_cluster to make it per-cpu (recommended by Huang, Ying). - large_next[] array is dynamically allocated because PMD_ORDER is not compile-time constant for powerpc (fixes build error). [1] https://lore.kernel.org/linux-mm/20231010142111.3997780-1-ryan.roberts@arm.com/ [2] https://lore.kernel.org/linux-mm/20231017161302.2518826-1-ryan.roberts@arm.com/ [3] https://lore.kernel.org/linux-mm/20231025144546.577640-1-ryan.roberts@arm.com/ [4] https://lore.kernel.org/all/20240311150058.1122862-1-ryan.roberts@arm.com/ [5] https://lore.kernel.org/linux-mm/20240304081348.197341-1-21cnbao@gmail.com/ [6] https://lore.kernel.org/all/b9944ac1-3919-4bb2-8b65-f3e5c52bc2aa@arm.com/ Thanks, Ryan Ryan Roberts (6): mm: swap: Remove CLUSTER_FLAG_HUGE from swap_cluster_info:flags mm: swap: free_swap_and_cache_nr() as batched free_swap_and_cache() mm: swap: Simplify struct percpu_cluster mm: swap: Allow storage of all mTHP orders mm: vmscan: Avoid split during shrink_folio_list() mm: madvise: Avoid split during MADV_PAGEOUT and MADV_COLD include/linux/pgtable.h | 58 ++++++++ include/linux/swap.h | 35 +++-- mm/huge_memory.c | 3 - mm/internal.h | 60 +++++++- mm/madvise.c | 100 +++++++------ mm/memory.c | 17 +-- mm/swap_slots.c | 6 +- mm/swapfile.c | 306 ++++++++++++++++++++++------------------ mm/vmscan.c | 9 +- 9 files changed, 380 insertions(+), 214 deletions(-) -- 2.25.1