From patchwork Mon Apr 7 23:42:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 14042004 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 9A374C36010 for ; Mon, 7 Apr 2025 23:43:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 732AB6B0027; Mon, 7 Apr 2025 19:42:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 65E456B0028; Mon, 7 Apr 2025 19:42:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3FABC6B002A; Mon, 7 Apr 2025 19:42:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 15CB76B0028 for ; Mon, 7 Apr 2025 19:42:34 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 360761A172E for ; Mon, 7 Apr 2025 23:42:35 +0000 (UTC) X-FDA: 83308874670.18.B2CCA48 Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) by imf15.hostedemail.com (Postfix) with ESMTP id 627E0A0004 for ; Mon, 7 Apr 2025 23:42:33 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=X4J1YfR8; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.177 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1744069353; 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:in-reply-to:references:references:dkim-signature; bh=mLJdhqYj+zTYLwyNRCW52GVf6fq8/yyGxPtSSQksBmE=; b=ZsPZlDCzu6MZGl0cf/34kwO+xl/f7B6Txsdq/eGUOH1KcYPH3ld6Rw3/7lD4QIwWPMjjfB 5gM5j6b/aNk81b7ZhfvVOqOfQCsVQeXQPx4F5GGKm8NB4800mFYmD5NpmlcurcMmA9gMHV 45e7AVGebT71cQBXD34qPuCIiIel47Q= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=X4J1YfR8; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.177 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744069353; a=rsa-sha256; cv=none; b=zxlGi5GZfAwhVBXpskfHWe6QZt/pFerYviSMauPcjdR3H5RDexwL5qtkNr2Vff8aUOfgsv gCD0rbPQLpwOJuD7Wsn6nZ3dbaAUEvewBal8oSVDmVCFuvFMuaPzqI2/VVKjcDkdIQi+hO FcMqWZYqbiMx58ywsDOXZ4ZtTJSVMY8= Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-6ff27ad48beso45587987b3.0 for ; Mon, 07 Apr 2025 16:42:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744069352; x=1744674152; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mLJdhqYj+zTYLwyNRCW52GVf6fq8/yyGxPtSSQksBmE=; b=X4J1YfR8V1/EyFUp+m7zUXCVAbKbKQUzAkD1fKotJqHJmFnwIUYuQsiVB1ylGmXyL+ CFvSyYEepQgIYO3k3Y1UB81kP5sCKQ1ujTFkgGYG8x6G3JtSQNrIWMCmBs5drNbl7r0z BGxqbZ9XrEI14i8DNBQbxRVrL/7OfHFlnl1Tik9C6slyHUllcuuOfnA4zY+9HBPz5l8m oGn2EF81mgKiVmuD3uEfiHeVveual438c5QYn3Q+RP/dHloMmPE72Lf8Rcj+uz19Sshl W90Xx48CaM2nP0hayQTag6MhuFBcdAIyeZOzs9tU+VBjlvu6CFDzoYNHBda3qM/a/VbB b7Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744069352; x=1744674152; h=content-transfer-encoding:mime-version: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=mLJdhqYj+zTYLwyNRCW52GVf6fq8/yyGxPtSSQksBmE=; b=J7/nECc2ZfKkqzoIEScaBGIf0GPQLaGo8IIY6/y1HRAYmT8skH7ZF7V9Bvkx6Ep/CE P3zIabhFi8k8smcAtznmxunOS5r+xezrZyNFfF+EF/vvqSHBIRDQ66OhTYSXKCmTfIA4 OAJ66PxgEiXrtLySgir9sSspdhiIh+ITKcFDOmyKhA8Q9/TJ0skRS/QoH+RslcRlvrrx AQeNcJhpT+iTF76ZFV1AfROIM4PJDjLVuEWA+2uj159DpLGoK5oBF1Q8sprUoGj9gBcQ Z9i+mWitSwSh0QZOos/bpHnOOPIyLRJGuHJ+eOmLfQX0HmIL6OiEe8S6UHboAMpv8Rro kFZQ== X-Gm-Message-State: AOJu0YyKKN0tGtMyTsB//hpe8bjCI1Q5vyfhixHRGAdZk6telOazwC7t al6LhdxVm44haug5izQL9By/q7sbKyddaWMcY+xSm5KtWVDESG77p7/5pA== X-Gm-Gg: ASbGncvz28meBId9Bmfas0uszWPQ4yTsAjNY9XikXtqq3AdWKvNky/2MQu7c8PEO0cg GrOTFnepYGO56O3xgfAGpyWfqBQrGBkUpIy8vPClUxF7Sk5rp+Ekm+9AqaaD7bYrvOSmqOawf71 3WqUwbN7YvoDQlHRF2YDHjkJKl7+NWT5xCdIGEp8UMUVuY7XB/eyENVxXqcpRbURsdhl0TgJHfr +rKcSTRm48mTNrCm43s4CXpEKgBCYVAUHXHlGeFXysqd4eDdPe0MbAGsrG6i09cxvvoC95TLV3Z QmYbDaMIyGc1V7xA6dzm6MtTJk6c1lbhIncS X-Google-Smtp-Source: AGHT+IFYT4abdql8kqgVGeGYJ9Wg2G43qD5+sFoTbpPfUBUetFZ6KN/IufxrelAcp5w2iktxFkJT+Q== X-Received: by 2002:a05:690c:7207:b0:6f9:7ec7:386a with SMTP id 00721157ae682-703f41bbb6cmr186335597b3.21.1744069352322; Mon, 07 Apr 2025 16:42:32 -0700 (PDT) Received: from localhost ([2a03:2880:25ff:73::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-703d1e7731fsm28302937b3.58.2025.04.07.16.42.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Apr 2025 16:42:32 -0700 (PDT) From: Nhat Pham To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, hughd@google.com, yosry.ahmed@linux.dev, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, len.brown@intel.com, chengming.zhou@linux.dev, kasong@tencent.com, chrisl@kernel.org, huang.ying.caritas@gmail.com, ryan.roberts@arm.com, viro@zeniv.linux.org.uk, baohua@kernel.org, osalvador@suse.de, lorenzo.stoakes@oracle.com, christophe.leroy@csgroup.eu, pavel@kernel.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-pm@vger.kernel.org Subject: [RFC PATCH 12/14] vswap: support THP swapin and batch free_swap_and_cache Date: Mon, 7 Apr 2025 16:42:13 -0700 Message-ID: <20250407234223.1059191-13-nphamcs@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250407234223.1059191-1-nphamcs@gmail.com> References: <20250407234223.1059191-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam01 X-Stat-Signature: bg3itr7q3dz8y6z14fnp9z7ofjqku6rf X-Rspam-User: X-Rspamd-Queue-Id: 627E0A0004 X-HE-Tag: 1744069353-699324 X-HE-Meta: U2FsdGVkX1+kH5/yqFM+AqPJfTU3TNFPCsPRCkDsR4XW3dgZWKvfqmQxvSsvZUV/muBG/DJm6fitbf28xb7cU4DztEvFlHYTmH7LIbH74i8KF4mK7XR6xAi946UZJlg3Gr/wIn5evXB6pb+yj5ja3eBeyvD6jArXxOBkI3VSI5iThXylbNZDNy11xRiqEW7rZYgTdnjY1inVE7QrmQVM+o8fu6hqCGiV5giK4tZJcEmaiobzMKYbZN7SsHWujIRV88AGcyre1+KM9e1pTV3kViWW3fmKx7yltF/vASZryp1YJCvJvZ8lMH/pvxsC85l78GegPpkP0plPE3L18Fls1L7zgy6V/QaR9Gd05qXRtotLh4mxNUOkKgos77EzUnUNA5eG5GIntQ3BfY8HK/JM0WtYpGR9OLhAw0GsF6JlYcouuCP2muWtNcRKbJ73+rXa1pwn8f387WVxk7xAF2xsazo8NGXM8dirXZjYv4yIyg0fO/CsokOkUW+MFFnHcxod+S4aDPnxTrNzOgmUj95VyN/xNWgAmqlK4swu6eiYm9xQoFOOvlgOWlEqNN8uU2XiY5LpkoYmTfcclUz8DLrXL5oYKXYif5M2e9xn0ijyT/vs/QmN5VHmwo/s2UvBMounWaLIiYihoIK5+XLUXFyL64jbLBU1T7mhhfqIzydLz3Ox2A6cbK4kjIr+JGC5yd1EtXCjAsIZJFMajufNPsGhGhw7YDVsNBr4z36rmuQfLuhSrFTNjSor0+HrZvpPnq4JRVItHcmm49rk+nIfQ7MaFNLWtPd12d/VBwZB6fMnqRmPRAk3H1xPdfQiwgBOcGHiEbrZHibK3bywJpm3ODNp1Y3IVDNtpd229VBf3RQnOUCZR1l6iU0XgA/oZfiEBL9bELbXAuWo6LlNRgGcrXeDUd1mhHaytZ8I0AGotVBNJ66zoWBTyuTaqhGPgqfRZf21qFY52SsqVJie9YSJyIU 5Xpn1Iv7 cq9u2uO/6+gFBu7FV1gtjcZ3k6OcwMTW17NDlYbTsWrFcBRgbMHFOzwaRGQD69yiTG323Bf0k3a84FAQUFjt1apwmgDngmRlCmKtSx67t1UF8suXehtFnrUynYg9/yAhF+/zhLdVqlUKxoV6W65hk0q5oX2JVCymp1OhZklEBBnDt1ypzCvGkMpuZ4uYoTCw/KtBoP4CxXy3h1/r41WAnEmNmq7J3JMpvuPJR+jDfArBTRZWn1PS3Iqaq5sCupMoWTV+6FZK+z/MEyIu842JweBD82emaG1gnuPpt8M1YSK9UsrGEbSgChHnW5Hb/N5nYdIJmp2sj+dtbLEmXSkav1I8izqrAh7cqfga8K0+v1pcUS+K2GPO8qHc4puVTn3BsPTs/u6zaGmzY+h+GCJgNCFzpAgMePZDEbx2NTGuiy8Bshxfyu/eDdEWRTOhX7BQ45w95/halYA/beER+WDNlzipwDhRLF67ck4bQNKUlmIpqCcmcwiGplwnEdcLpISyCxuEmIXQ16O7YI2S86IF2uTIv6AAZTznWLnjkRpyWqWJj4Oo= 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: This patch implements the required functionalities for THP swapin and batched free_swap_and_cache() in the virtual swap space design. The central requirement is the range of entries we are working with must have no mixed backing states: 1. For now, zswap-backed entries are not supported for these batched operations. 2. All the entries must be backed by the same type. 3. If the swap entries in the batch are backed by in-memory folio, it must be the same folio (i.e they correspond to the subpages of that folio). 4. If the swap entries in the batch are backed by slots on swapfiles, it must be the same swapfile, and these physical swap slots must also be contiguous. Signed-off-by: Nhat Pham --- include/linux/swap.h | 6 +++ mm/internal.h | 14 +------ mm/memory.c | 16 ++++++-- mm/vswap.c | 89 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 16 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 98cdfe0c1da7..c3a10c952116 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -763,6 +763,7 @@ bool vswap_folio_backed(swp_entry_t entry, int nr); void vswap_store_folio(swp_entry_t entry, struct folio *folio); void swap_zeromap_folio_set(struct folio *folio); void vswap_assoc_zswap(swp_entry_t entry, struct zswap_entry *zswap_entry); +bool vswap_can_swapin_thp(swp_entry_t entry, int nr); static inline bool trylock_swapoff(swp_entry_t entry, struct swap_info_struct **si) @@ -862,6 +863,11 @@ static inline void vswap_assoc_zswap(swp_entry_t entry, { } +static inline bool vswap_can_swapin_thp(swp_entry_t entry, int nr) +{ + return true; +} + static inline bool trylock_swapoff(swp_entry_t entry, struct swap_info_struct **si) { diff --git a/mm/internal.h b/mm/internal.h index 51061691a731..6694e7a14745 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -268,14 +268,7 @@ static inline swp_entry_t swap_nth(swp_entry_t entry, long n) return (swp_entry_t) { entry.val + n }; } -/* temporary disallow batched swap operations */ -static inline swp_entry_t swap_move(swp_entry_t entry, long delta) -{ - swp_entry_t next_entry; - - next_entry.val = 0; - return next_entry; -} +swp_entry_t swap_move(swp_entry_t entry, long delta); #else static inline swp_entry_t swap_nth(swp_entry_t entry, long n) { @@ -344,8 +337,6 @@ static inline pte_t pte_next_swp_offset(pte_t pte) * max_nr must be at least one and must be limited by the caller so scanning * cannot exceed a single page table. * - * Note that for virtual swap space, we will not batch anything for now. - * * Return: the number of table entries in the batch. */ static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte) @@ -360,9 +351,6 @@ static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte) VM_WARN_ON(!is_swap_pte(pte)); VM_WARN_ON(non_swap_entry(entry)); - if (IS_ENABLED(CONFIG_VIRTUAL_SWAP)) - return 1; - cgroup_id = lookup_swap_cgroup_id(entry); while (ptep < end_ptep) { pte = ptep_get(ptep); diff --git a/mm/memory.c b/mm/memory.c index c5c34efafa81..5abb464913ef 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4226,10 +4226,8 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf) * A large swapped out folio could be partially or fully in zswap. We * lack handling for such cases, so fallback to swapping in order-0 * folio. - * - * We also disable THP swapin on the virtual swap implementation, for now. */ - if (!zswap_never_enabled() || IS_ENABLED(CONFIG_VIRTUAL_SWAP)) + if (!zswap_never_enabled()) goto fallback; entry = pte_to_swp_entry(vmf->orig_pte); @@ -4419,6 +4417,18 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) } need_clear_cache = true; + /* + * Recheck to make sure the entire range is still + * THP-swapin-able. Note that before we call + * swapcache_prepare(), entries in the range can + * still have their backing status changed. + */ + if (IS_ENABLED(CONFIG_VIRTUAL_SWAP) && + !vswap_can_swapin_thp(entry, nr_pages)) { + schedule_timeout_uninterruptible(1); + goto out_page; + } + mem_cgroup_swapin_uncharge_swap(entry, nr_pages); shadow = get_shadow_from_swap_cache(entry); diff --git a/mm/vswap.c b/mm/vswap.c index fcc7807ba89b..c09a7efc2aeb 100644 --- a/mm/vswap.c +++ b/mm/vswap.c @@ -9,6 +9,7 @@ #include #include #include +#include "internal.h" #include "swap.h" /* @@ -1104,6 +1105,94 @@ bool vswap_folio_backed(swp_entry_t entry, int nr) && type == VSWAP_FOLIO; } +/** + * vswap_can_swapin_thp - check if the swap entries can be swapped in as a THP. + * @entry: the first virtual swap slot in the range. + * @nr: the number of slots in the range. + * + * For now, we can only swap in a THP if the entire range is zero-filled, or if + * the entire range is backed by a contiguous range of physical swap slots on a + * swapfile. + */ +bool vswap_can_swapin_thp(swp_entry_t entry, int nr) +{ + enum swap_type type; + + return vswap_check_backing(entry, &type, nr) == nr && + (type == VSWAP_ZERO || type == VSWAP_SWAPFILE); +} + +/** + * swap_move - increment the swap slot by delta, checking the backing state and + * return 0 if the backing state does not match (i.e wrong backing + * state type, or wrong offset on the backing stores). + * @entry: the original virtual swap slot. + * @delta: the offset to increment the original slot. + * + * Note that this function is racy unless we can pin the backing state of these + * swap slots down with swapcache_prepare(). + * + * Caller should only rely on this function as a best-effort hint otherwise, + * and should double-check after ensuring the whole range is pinned down. + * + * Return: the incremented virtual swap slot if the backing state matches, or + * 0 if the backing state does not match. + */ +swp_entry_t swap_move(swp_entry_t entry, long delta) +{ + struct swp_desc *desc, *next_desc; + swp_entry_t next_entry; + bool invalid = true; + struct folio *folio; + enum swap_type type; + swp_slot_t slot; + + next_entry.val = entry.val + delta; + + rcu_read_lock(); + desc = xa_load(&vswap_map, entry.val); + next_desc = xa_load(&vswap_map, next_entry.val); + + if (!desc || !next_desc) { + rcu_read_unlock(); + return (swp_entry_t){0}; + } + + read_lock(&desc->lock); + if (desc->type == VSWAP_ZSWAP) { + read_unlock(&desc->lock); + goto rcu_unlock; + } + + type = desc->type; + if (type == VSWAP_FOLIO) + folio = desc->folio; + + if (type == VSWAP_SWAPFILE) + slot = desc->slot; + read_unlock(&desc->lock); + + read_lock(&next_desc->lock); + if (next_desc->type != type) + goto next_unlock; + + if (type == VSWAP_SWAPFILE && + (swp_slot_type(next_desc->slot) != swp_slot_type(slot) || + swp_slot_offset(next_desc->slot) != + swp_slot_offset(slot) + delta)) + goto next_unlock; + + if (type == VSWAP_FOLIO && next_desc->folio != folio) + goto next_unlock; + + invalid = false; +next_unlock: + read_unlock(&next_desc->lock); +rcu_unlock: + rcu_read_unlock(); + return invalid ? (swp_entry_t){0} : next_entry; +} + /* * Return the count of contiguous swap entries that share the same * VSWAP_ZERO status as the starting entry. If is_zeromap is not NULL,