From mboxrd@z Thu Jan 1 00:00:00 1970 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 72AADC7115B for ; Thu, 19 Jun 2025 17:56:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E84A36B009A; Thu, 19 Jun 2025 13:56:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DE8316B009B; Thu, 19 Jun 2025 13:56:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C88646B009C; Thu, 19 Jun 2025 13:56:01 -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 B27296B009A for ; Thu, 19 Jun 2025 13:56:01 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3BDA61A07D3 for ; Thu, 19 Jun 2025 17:56:01 +0000 (UTC) X-FDA: 83572903722.09.E089C6F Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf14.hostedemail.com (Postfix) with ESMTP id 50422100008 for ; Thu, 19 Jun 2025 17:55:59 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ndoSmzAa; spf=pass (imf14.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.169 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=1750355759; 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=b0J5rIzwckRCud+/m8yBCOqHh4RXMbBP/Pgn0V8wAaM=; b=rV8gha8FEzb4Aca1O2Hy4RhPinWdhwgDK2ticYLiGLuUf6omfQi0rq7iqeVtx0wCNNGdiK f2U1/rjr1PfJv5wLT6CnlZtu1qhVzNUIBD3tY+uJshndZi5G/gJHZoXrvq6W/F0nWftAKm bzALeSGo+CjfJl3p0iS4/UVswRN++sc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750355759; a=rsa-sha256; cv=none; b=NnzC1ulc6eq3IkmgBJpyKx1zISkTh3dqtPigXdfTy4rfZT9OTaCta8vtuF9yysnxPF9ChD 2DHbrXJR/P2rbFLJxlRNOOcJSFO3AA39kUW5MO7TVvX1DtCm5cI0BL6fLVe7pziCA0DZsO 3UZLiq2EE7iRNVbmTVtiwVSuQXZbpBA= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ndoSmzAa; spf=pass (imf14.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-23602481460so9225215ad.0 for ; Thu, 19 Jun 2025 10:55:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750355757; x=1750960557; 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=b0J5rIzwckRCud+/m8yBCOqHh4RXMbBP/Pgn0V8wAaM=; b=ndoSmzAaQsaC53ThyS5RiCfuzUsx2FeqRQFYqsmjli2gg2sPtElSKJDeiY4uMvJ6f6 SlNf7jz9JwN6ozukqnfxPcnrj1u05B9GydF2BuIt/h+5BwhgwL9E9gktwA2bfIKRAVPH z+5Fphd76Q6o8gJ04IXnwhErUHxX2M/Xg73B6WDBnjiw8xoMvpSDpmhdbz6AuUh3QOnY j1RVfMgoaN1uU+Y86jmwOgyy392dpL8+YXo8I9vTrAdGrp3vkaVvAnnP8/Bu5pezWMBp 2cg2miDGg9fDffegdsJCHoZN9a9oFKyrxRpSUV9yqTqKnkYf9lAmUBzGa9euemWYQlbX 2weg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750355757; x=1750960557; 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=b0J5rIzwckRCud+/m8yBCOqHh4RXMbBP/Pgn0V8wAaM=; b=k15Ee68THcJ6x33j+odgeRF8O8mUX/aKT2msaPXD7sRfKwqHyF3CKact4loC3GWJwl woboUEJm4RVEOLi+WdRpdS3SXoBegAD9zlDkASdI+WbJNxzkFLkj5yMIP+BrO7w8btRn XHyj2bqEA4QyHxKPXYXm0oXnNptCWXnrD9KIDoy1iuiLLg9TFY8VRnE54DpQekV25NPy 2rTgGAYH5hwSmrX1APhUWxc4aScHEcgKoRS7SAMZrhu0er/Kb4qrbAa7/8ZGNsMQTkud V7x4RTTIzZLJxGYqttYzogEUv5t+CXPXjtA4FlZ7zYzskmxQc52HDuZEg9izE+wA0Exp zB5w== X-Gm-Message-State: AOJu0Yz7sqMvvAzL43vNCBaNMEXgjs17XJ+EekJlWTJgRLlg7ySxyJ7J IZZRtHpRr9tUKm5dW/VCUsV4U3ym1oGv8n9rW7LFKmrX5oVPE3Z2Ad3VwBP1ZwyF9RM= X-Gm-Gg: ASbGncte6DErGCbR2I5cn1DAZQJ3AjplecAab15C+5HIfEvwONWluQvtlCpLKyPXKDU cUJxDYISVws9dfDEeEXt/GMCR7SRATFM6qU2zrNNKU9Zd/Lr6g1iGlLn9eV/GrtoOzS38rMkgud YdLrxYSZdnLWD4QYvVhyXMGDH13Ku9sBeSVmiDwALMrxDZAF5tXWWL+zn3UKGhRtYFe0Yn8gpVg 9yf/yrwJbaUNJtCMw10Ie7l7dr836zX7fbWcGKOBKcbrKJlhOlzUCXQ2y5MKN6NKJV0MT9bEkCZ 6teLONsoHVz29GS5A+h7nCAidtvBZTdN/oEjV/gclq0f2tTzRkKBaQbZcDjkqDR85F/aAcsAaKz ankQJR/5LOnbreH7s0g== X-Google-Smtp-Source: AGHT+IFjL5lSBKqSLv1e4AfpQIYPMDh9hgjRu/MF7siVCMwu5I/gaWVJSunXz3+4fKOuSsbg2ckBuQ== X-Received: by 2002:a17:902:f68e:b0:234:cf24:3be8 with SMTP id d9443c01a7336-2366b12f541mr369757775ad.28.1750355757240; Thu, 19 Jun 2025 10:55:57 -0700 (PDT) Received: from KASONG-MC4.tencent.com ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-237d83efa44sm255215ad.77.2025.06.19.10.55.53 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 19 Jun 2025 10:55:56 -0700 (PDT) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Hugh Dickins , Baolin Wang , Matthew Wilcox , Kemeng Shi , Chris Li , Nhat Pham , Baoquan He , Barry Song , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 3/4] mm/shmem, swap: improve mthp swapin process Date: Fri, 20 Jun 2025 01:55:37 +0800 Message-ID: <20250619175538.15799-4-ryncsn@gmail.com> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250619175538.15799-1-ryncsn@gmail.com> References: <20250619175538.15799-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 50422100008 X-Stat-Signature: h37919358ziy6haicmaby65ge871ox4a X-Rspam-User: X-HE-Tag: 1750355759-38521 X-HE-Meta: U2FsdGVkX1+esi5/WvqpOCmf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hnf8LJxdri/bnvxMbLI8er9z8xeLO2EACxwFYgPQAhDWd7v+mO0wX5WExzDqi5biCzrHSMSY1ZNiB/tweScuTzUfQtAI7gupslWF9kFAmHEJ+3Y+ngB962VqXxNns2OHh9O1Erq9soqWqvOfMS1BRRPUdRXPxwW/W4qBqu9FCl19B1DW1cyYEwMh2oOD21NaJ0RoM80CxVoVFp4BEqbYyE/Yr3lH9QtbBtAVRO4Um7tctvxDSLFgstkDUvlWUqKHJ5Ce7GNJqUDEiE2UoDk0Wtn7+0iXbtKciSjX0+DPQNvtSWf/mNzpgrpCXFSf756xQUtFGzk6tpvn2/xc9o8GYyVkGqtU52w88z6O/4nlkXKMGYQBiIij2a4HNmMnNY4ks3llFiBZA55zFneToycF7qaM92J9litlB+HMPKjDWKnLjBKD6Wa0O4bempUB9onbHvX0WhB1Ag9SPkqIHCkIrLafcHLW8vWBX5EzZawZIqnWrZ0BaqRFAcQ== 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 Tidy up the mTHP swapin code, reduce duplicated codes and slightly tweak the workflow. For SWP_SYNCHRONOUS_IO devices, we should skip the readahead and swap cache even if the swapin falls back to order 0. Readahead is not helpful for such devices. Also consolidates the mTHP related check to one place so they are now all wrapped by CONFIG_TRANSPARENT_HUGEPAGE, and will be trimmed off by compiler if not needed. Signed-off-by: Kairui Song Reviewed-by: Kemeng Shi --- mm/shmem.c | 175 ++++++++++++++++++++++++----------------------------- 1 file changed, 78 insertions(+), 97 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index ce44d1da08cd..721f5aa68572 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1975,31 +1975,51 @@ static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, return ERR_PTR(error); } -static struct folio *shmem_swap_alloc_folio(struct inode *inode, +static struct folio *shmem_swapin_direct(struct inode *inode, struct vm_area_struct *vma, pgoff_t index, - swp_entry_t entry, int order, gfp_t gfp) + swp_entry_t entry, int *order, gfp_t gfp) { struct shmem_inode_info *info = SHMEM_I(inode); + int nr_pages = 1 << *order; struct folio *new; + pgoff_t offset; void *shadow; - int nr_pages; /* * We have arrived here because our zones are constrained, so don't * limit chance of success with further cpuset and node constraints. */ gfp &= ~GFP_CONSTRAINT_MASK; - if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && order > 0) { - gfp_t huge_gfp = vma_thp_gfp_mask(vma); + if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { + if (WARN_ON_ONCE(*order)) + return ERR_PTR(-EINVAL); + } else if (*order) { + /* + * If uffd is active for the vma, we need per-page fault + * fidelity to maintain the uffd semantics, then fallback + * to swapin order-0 folio, as well as for zswap case. + * Any existing sub folio in the swap cache also blocks + * mTHP swapin. + */ + if ((vma && userfaultfd_armed(vma)) || + !zswap_never_enabled() || + non_swapcache_batch(entry, nr_pages) != nr_pages) { + offset = index - round_down(index, nr_pages); + entry = swp_entry(swp_type(entry), + swp_offset(entry) + offset); + *order = 0; + nr_pages = 1; + } else { + gfp_t huge_gfp = vma_thp_gfp_mask(vma); - gfp = limit_gfp_mask(huge_gfp, gfp); + gfp = limit_gfp_mask(huge_gfp, gfp); + } } - new = shmem_alloc_folio(gfp, order, info, index); + new = shmem_alloc_folio(gfp, *order, info, index); if (!new) return ERR_PTR(-ENOMEM); - nr_pages = folio_nr_pages(new); if (mem_cgroup_swapin_charge_folio(new, vma ? vma->vm_mm : NULL, gfp, entry)) { folio_put(new); @@ -2165,8 +2185,12 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, swap_free_nr(swap, nr_pages); } -static int shmem_split_large_entry(struct inode *inode, pgoff_t index, - swp_entry_t swap, gfp_t gfp) +/* + * Split an existing large swap entry. @index should point to one sub mapping + * slot within the entry @swap, this sub slot will be split into order 0. + */ +static int shmem_split_swap_entry(struct inode *inode, pgoff_t index, + swp_entry_t swap, gfp_t gfp) { struct address_space *mapping = inode->i_mapping; XA_STATE_ORDER(xas, &mapping->i_pages, index, 0); @@ -2226,7 +2250,6 @@ static int shmem_split_large_entry(struct inode *inode, pgoff_t index, cur_order = split_order; split_order = xas_try_split_min_order(split_order); } - unlock: xas_unlock_irq(&xas); @@ -2237,7 +2260,7 @@ static int shmem_split_large_entry(struct inode *inode, pgoff_t index, if (xas_error(&xas)) return xas_error(&xas); - return entry_order; + return 0; } /* @@ -2254,11 +2277,11 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, struct address_space *mapping = inode->i_mapping; struct mm_struct *fault_mm = vma ? vma->vm_mm : NULL; struct shmem_inode_info *info = SHMEM_I(inode); + int error, nr_pages, order, swap_order; struct swap_info_struct *si; struct folio *folio = NULL; bool skip_swapcache = false; swp_entry_t swap; - int error, nr_pages, order, split_order; VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); swap = radix_to_swp_entry(*foliop); @@ -2283,110 +2306,66 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, /* Look it up and read it in.. */ folio = swap_cache_get_folio(swap, NULL, 0); if (!folio) { - int nr_pages = 1 << order; - bool fallback_order0 = false; - /* Or update major stats only when swapin succeeds?? */ if (fault_type) { *fault_type |= VM_FAULT_MAJOR; count_vm_event(PGMAJFAULT); count_memcg_event_mm(fault_mm, PGMAJFAULT); } - - /* - * If uffd is active for the vma, we need per-page fault - * fidelity to maintain the uffd semantics, then fallback - * to swapin order-0 folio, as well as for zswap case. - * Any existing sub folio in the swap cache also blocks - * mTHP swapin. - */ - if (order > 0 && ((vma && unlikely(userfaultfd_armed(vma))) || - !zswap_never_enabled() || - non_swapcache_batch(swap, nr_pages) != nr_pages)) - fallback_order0 = true; - - /* Skip swapcache for synchronous device. */ - if (!fallback_order0 && data_race(si->flags & SWP_SYNCHRONOUS_IO)) { - folio = shmem_swap_alloc_folio(inode, vma, index, swap, order, gfp); + /* Try direct mTHP swapin bypassing swap cache and readahead */ + if (data_race(si->flags & SWP_SYNCHRONOUS_IO)) { + swap_order = order; + folio = shmem_swapin_direct(inode, vma, index, + swap, &swap_order, gfp); if (!IS_ERR(folio)) { skip_swapcache = true; goto alloced; } - - /* - * Fallback to swapin order-0 folio unless the swap entry - * already exists. - */ + /* Fallback if order > 0 swapin failed with -ENOMEM */ error = PTR_ERR(folio); folio = NULL; - if (error == -EEXIST) + if (error != -ENOMEM || !swap_order) goto failed; } - /* - * Now swap device can only swap in order 0 folio, then we - * should split the large swap entry stored in the pagecache - * if necessary. + * Try order 0 swapin using swap cache and readahead, it still + * may return order > 0 folio due to raced swap cache. */ - split_order = shmem_split_large_entry(inode, index, swap, gfp); - if (split_order < 0) { - error = split_order; - goto failed; - } - - /* - * If the large swap entry has already been split, it is - * necessary to recalculate the new swap entry based on - * the old order alignment. - */ - if (split_order > 0) { - pgoff_t offset = index - round_down(index, 1 << split_order); - - swap = swp_entry(swp_type(swap), swp_offset(swap) + offset); - } - - /* Here we actually start the io */ folio = shmem_swapin_cluster(swap, gfp, info, index); if (!folio) { error = -ENOMEM; goto failed; } - } else if (order > folio_order(folio)) { - /* - * Swap readahead may swap in order 0 folios into swapcache - * asynchronously, while the shmem mapping can still stores - * large swap entries. In such cases, we should split the - * large swap entry to prevent possible data corruption. - */ - split_order = shmem_split_large_entry(inode, index, swap, gfp); - if (split_order < 0) { - folio_put(folio); - folio = NULL; - error = split_order; - goto failed; - } - - /* - * If the large swap entry has already been split, it is - * necessary to recalculate the new swap entry based on - * the old order alignment. - */ - if (split_order > 0) { - pgoff_t offset = index - round_down(index, 1 << split_order); - - swap = swp_entry(swp_type(swap), swp_offset(swap) + offset); - } - } else if (order < folio_order(folio)) { - swap.val = round_down(swp_type(swap), folio_order(folio)); } - alloced: + /* + * We need to split an existing large entry if swapin brought in a + * smaller folio due to various of reasons. + * + * And worth noting there is a special case: if there is a smaller + * cached folio that covers @swap, but not @index (it only covers + * first few sub entries of the large entry, but @index points to + * later parts), the swap cache lookup will still see this folio, + * And we need to split the large entry here. Later checks will fail, + * as it can't satisfy the swap requirement, and we will retry + * the swapin from beginning. + */ + swap_order = folio_order(folio); + if (order > swap_order) { + error = shmem_split_swap_entry(inode, index, swap, gfp); + if (error) + goto failed_nolock; + } + + index = round_down(index, 1 << swap_order); + swap.val = round_down(swap.val, 1 << swap_order); + /* We have to do this with folio locked to prevent races */ folio_lock(folio); if ((!skip_swapcache && !folio_test_swapcache(folio)) || folio->swap.val != swap.val) { error = -EEXIST; - goto unlock; + goto failed_unlock; } if (!folio_test_uptodate(folio)) { error = -EIO; @@ -2407,8 +2386,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, goto failed; } - error = shmem_add_to_page_cache(folio, mapping, - round_down(index, nr_pages), + error = shmem_add_to_page_cache(folio, mapping, index, swp_to_radix_entry(swap), gfp); if (error) goto failed; @@ -2419,8 +2397,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, folio_mark_accessed(folio); if (skip_swapcache) { + swapcache_clear(si, folio->swap, folio_nr_pages(folio)); folio->swap.val = 0; - swapcache_clear(si, swap, nr_pages); } else { delete_from_swap_cache(folio); } @@ -2436,13 +2414,16 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, if (error == -EIO) shmem_set_folio_swapin_error(inode, index, folio, swap, skip_swapcache); -unlock: - if (skip_swapcache) - swapcache_clear(si, swap, folio_nr_pages(folio)); - if (folio) { +failed_unlock: + if (folio) folio_unlock(folio); - folio_put(folio); +failed_nolock: + if (skip_swapcache) { + swapcache_clear(si, folio->swap, folio_nr_pages(folio)); + folio->swap.val = 0; } + if (folio) + folio_put(folio); put_swap_device(si); return error; } -- 2.50.0