From patchwork Thu Nov 21 22:25:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13882404 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 1B55CE64025 for ; Thu, 21 Nov 2024 22:26:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8DA326B0093; Thu, 21 Nov 2024 17:26:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 862346B0095; Thu, 21 Nov 2024 17:26:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 702A26B0096; Thu, 21 Nov 2024 17:26:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 52F536B0093 for ; Thu, 21 Nov 2024 17:26:40 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0C514C1430 for ; Thu, 21 Nov 2024 22:26:40 +0000 (UTC) X-FDA: 82811535030.13.D6847BA Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf20.hostedemail.com (Postfix) with ESMTP id AE95B1C000A for ; Thu, 21 Nov 2024 22:25:31 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=g32+NVki; spf=pass (imf20.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=21cnbao@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=1732227846; 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=pZAV40eLukdAUm3AIOVig1F6aMrQYsyjN2fkwd6I2Qs=; b=h4XJuAsN32VCk+uq8opOYK/5a5Eo6vn0r0AgaJWdxr2Isq9Kb3rW82qh87WCYiWdXaQTrA D+UD7TrratW7sN3JxGkY+dNbQJhj0nAs0hmaMZ6mLXcgvhEm6D2EkVsuP/kWTwQ9K7jqP9 fcSXU+w7ZKwG7Q9rorYuFNu5ubradVw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732227846; a=rsa-sha256; cv=none; b=5d1irg5WN9DU2gCAb15SuLDdRanTlGP7aN6pWz67YT3jiLcWa8QPOAiKVDtyPBiEIotvlL NkkCccATZXazhkVHmtC9gdZBaz55o1lHOP7DuVhTdSykYJ8X51Gs4aQiAbBJMQkipvhhN1 mmLBEAwPKSlY+lvhav0gxTPY2FLMcFI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=g32+NVki; spf=pass (imf20.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2128383b86eso14641435ad.2 for ; Thu, 21 Nov 2024 14:26:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732227997; x=1732832797; 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=pZAV40eLukdAUm3AIOVig1F6aMrQYsyjN2fkwd6I2Qs=; b=g32+NVki++up8RN0ecNBCFdNsZPkPu8a322r6w0fG2IctIlEWJBSMLmlmQyH8s3Quv twM2Ki4ukdc7YCDIlY7Q8e51BJPK2ncaDdBi8qXg6NB2zrVF1OOO/glrKa/ea83UWqYP TcqmSkCAwv6ehV5yml01jwvo4HBzrvx5EdDNAsT48C2CzZiEKIiUPVvffRFIxQhAp6Rg nAE4/RBHp9Hs6j72NWURm+2BCoXkbK3fyevYK1Evo5yb+y7TK8uULsUNe1ZNHMUeNuLn Xc6762e3rwoAwO1DlJE72eeydBu95sdfzKvx24jRQcaYtVT7cKe+JdZHwLm16ZJE1x2k HDrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732227997; x=1732832797; 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=pZAV40eLukdAUm3AIOVig1F6aMrQYsyjN2fkwd6I2Qs=; b=v7E4kpfKW2qmDTJliogDhE4FWOT4/Kkc4x8xrTpDZ35MzvqcaHkb/T4pFfM/n5OLFG aaGXdwWYis00gZtzFozCI4xudOqPdJ8ghVJgfLnjktt8SfPYhjIdklgDIg3R9k1+oPl8 syEE8YT4/eHacC4wvr0dxGauYe/lHtEq3vO/UfBiDZke04RYtWXV+hbBSuzEhRDaohdm 1dMQQK+MKPzxhQcOeLrWyLU8hDO72x5RtWoyirKCYdboPxK2b2hN/V52F2vx+ejbnI91 pp4c16kOuZ49VR7qKf42lX13BkVhsGx1QBvxfuIf8OlKlpvJ/7v6cJ5jvdj0rV8tqXbw MKtQ== X-Forwarded-Encrypted: i=1; AJvYcCUFJJI3y4dQ36bbm6HT+v1/UEac9EjpRIp3gDbnoNS60Nx9bfjrt/yfk1gZYU3/cr8l7RfN5N4dXw==@kvack.org X-Gm-Message-State: AOJu0YxirccHv+0f67/QgwwW36OPHUYHdH45nnll0iLjUxs1sEkXwW0N GQSigAu2iWZsxIJqMTNp0Hx12Ikl8AswvgnhTy+oMMoKOS8j7I7K X-Gm-Gg: ASbGncuWIbnqIompXtit0nlHUvf2SMzWWlJLpF958GUuvCbFQIk6YPnVmw12ymBCSpX GKxYvrDnSHQk47vLIc5lVLjgeoLKmq6upBUkBmcJy3V1Qhl3w2KM0LfNes+lHG9FcIb6RHaHdTf xjeQxquVx915JXdD6gB4+QmLiHTUDVbhLUVlaqyCf8SLwsrD9yaLUzQVH7vLUWKlvUz5h4A24qo c0k5DsbB/V421L500gUmkhywMD9G0eCN/IC2CVhah3DgXTuZoc4zb20ZkfZ83EXmnUffhOd X-Google-Smtp-Source: AGHT+IFk8IcOaVtDODSKrSa6m+UG9KLonALKh/GnCgbMtgcUZBCu3HpUQkp54L4Z2yATbp/3yYDCjw== X-Received: by 2002:a17:903:244b:b0:211:f6e4:d68e with SMTP id d9443c01a7336-2129fce2e23mr6801055ad.10.1732227996748; Thu, 21 Nov 2024 14:26:36 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:9d64:b0ba:faf2:680e]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dba22f4sm3334745ad.100.2024.11.21.14.26.28 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 21 Nov 2024 14:26:36 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: axboe@kernel.dk, bala.seshasayee@linux.intel.com, chrisl@kernel.org, david@redhat.com, hannes@cmpxchg.org, kanchana.p.sridhar@intel.com, kasong@tencent.com, linux-block@vger.kernel.org, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, senozhatsky@chromium.org, surenb@google.com, terrelln@fb.com, usamaarif642@gmail.com, v-songbaohua@oppo.com, wajdi.k.feghali@intel.com, willy@infradead.org, ying.huang@intel.com, yosryahmed@google.com, yuzhao@google.com, zhengtangquan@oppo.com, zhouchengming@bytedance.com, Chuanhua Han Subject: [PATCH RFC v3 4/4] mm: fall back to four small folios if mTHP allocation fails Date: Fri, 22 Nov 2024 11:25:21 +1300 Message-Id: <20241121222521.83458-5-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20241121222521.83458-1-21cnbao@gmail.com> References: <20241121222521.83458-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: AE95B1C000A X-Stat-Signature: i5aqdse16wxfm38w3gqnx66jwrcrkf3n X-HE-Tag: 1732227931-407041 X-HE-Meta: U2FsdGVkX1/2VdYvTEedogmgN2BHcrJgbTl6PVxBbqJuYRHdltiHMgenSpkv1ixh8RjOSjUPI9hSlJGF+vb65e+wkgp7g31KsJjOp/00bqJysmWDikrtfJ7B32i/3DK3/dYLaZEf8YYs90TxlTeKAwJ1elwfgHTyQ7DHHwP2jcnRCK+xqOAI0L33fQqA+8NfXl27CDz1uUNFc3qtBSKw5eRXw0d7AKznkEskSRd7CwpujuTrIkW3I4MFtKTuwFofsyKKZuIgDTuuZu2fnpxwgJpv8nE41cZzh4+evdlg/anmc2HoJRGjDwQZNiJC56D6BAdFyjBfhEENSpQ6dN+y988YnlW1LQagvK9EKNJtfiSTyBU9ZMwgo0TNByPjirAXwm2VPjO1xY8Vt5PBJNSJmVkH+9bZVRJWuwumqvbyMM28dt1H3g4pgJHgjXv+54Pugckb/Uu3cGqEiL80VafRSG1T0Gr3IRyckmUX7ZK2R+uwLgMk9yBHo+xlwqEZWlFdnkp8KgRE+6oH5kb8Bvw3fYPpiWDbc+oY2T74E4r3minZuunKj5V1f2UeOHMsOVWfmRVdlbQG7k0CPi6oWjzsBllxc5t8KlIJ65cR1gEMcExNJP3pYvf0fJjStok/OT2eEBMv9VGH5TOMmC569R22LUb6nUsp8gx8BTRiy91LxXeY50cPp7AszF7dWyx8tQhF1qep8vJ+bxQxGRB1/nZRF0tVrRzacFGvdUpHzsP8QdY/KxTcZ7JvY3/5dHJ34OXwg9W2LSpejRdPvjxIY9oyk9YFqKGgut0uaz8XXn2qYW9zAliRO1zEf+AuOXCtVwbOeOsvF09yJ9qNJG70AG88f/UrkmfGOar7siNvwW31/zc2xgZOaVWpQwYegdjTm1b0vnDviiKNLD2DU5FS/j51BbLg2r9eHLtH/R+WlbXB5RPBlaXaSwfnXvMjo/Rxlto8ExO2tmjITRy4d6IU08M LWT9Oxrz NIpMqvpha1d7wkEJwrF7Wv2P9KSLK+ySGiUMO6DyyC8PA2xkY6pm8gKvjARV8xtODsLmew9kGEB8INIbccmssBc9mx4GT18cUAeT+G0j2GGdhdWkZRMOLnfeG+hF3XymmVOGZXmdi3ifuOia/aEFigqd3/ppHnDrhAcn8tSjORNfSZlo6Cs9nluEc3IyCbcsgZJSriaaiwwM8x7e82FiOGmCT6jUsukU6rAAsRRCPZRmJf2CqGe9bDwnY36xaap/3d2e32+nWnl5j03ksvI7c1VJM/IRGNpVGYUcnOgOVwQ7j0VPGZJWBFXMe7tEpaJJbTS5etDsKs5rYXYfHR6NesQXNuPqHHg16lhK4kD3/g/5+ROsX+xvc2QJ2xuQKCV19L0GDhluR26lw2y12u8deViEPoT/h4Qjkvsbq 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: Barry Song The swapfile can compress/decompress at 4 * PAGES granularity, reducing CPU usage and improving the compression ratio. However, if allocating an mTHP fails and we fall back to a single small folio, the entire large block must still be decompressed. This results in a 16 KiB area requiring 4 page faults, where each fault decompresses 16 KiB but retrieves only 4 KiB of data from the block. To address this inefficiency, we instead fall back to 4 small folios, ensuring that each decompression occurs only once. Allowing swap_read_folio() to decompress and read into an array of 4 folios would be extremely complex, requiring extensive changes throughout the stack, including swap_read_folio, zeromap, zswap, and final swap implementations like zRAM. In contrast, having these components fill a large folio with 4 subpages is much simpler. To avoid a full-stack modification, we introduce a per-CPU order-2 large folio as a buffer. This buffer is used for swap_read_folio(), after which the data is copied into the 4 small folios. Finally, in do_swap_page(), all these small folios are mapped. Co-developed-by: Chuanhua Han Signed-off-by: Chuanhua Han Signed-off-by: Barry Song --- mm/memory.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 192 insertions(+), 11 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 209885a4134f..e551570c1425 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4042,6 +4042,15 @@ static struct folio *__alloc_swap_folio(struct vm_fault *vmf) return folio; } +#define BATCH_SWPIN_ORDER 2 +#define BATCH_SWPIN_COUNT (1 << BATCH_SWPIN_ORDER) +#define BATCH_SWPIN_SIZE (PAGE_SIZE << BATCH_SWPIN_ORDER) + +struct batch_swpin_buffer { + struct folio *folio; + struct mutex mutex; +}; + #ifdef CONFIG_TRANSPARENT_HUGEPAGE static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) { @@ -4120,7 +4129,101 @@ static inline unsigned long thp_swap_suitable_orders(pgoff_t swp_offset, return orders; } -static struct folio *alloc_swap_folio(struct vm_fault *vmf) +static DEFINE_PER_CPU(struct batch_swpin_buffer, swp_buf); + +static int __init batch_swpin_buffer_init(void) +{ + int ret, cpu; + struct batch_swpin_buffer *buf; + + for_each_possible_cpu(cpu) { + buf = per_cpu_ptr(&swp_buf, cpu); + buf->folio = (struct folio *)alloc_pages_node(cpu_to_node(cpu), + GFP_KERNEL | __GFP_COMP, BATCH_SWPIN_ORDER); + if (!buf->folio) { + ret = -ENOMEM; + goto err; + } + mutex_init(&buf->mutex); + } + return 0; + +err: + for_each_possible_cpu(cpu) { + buf = per_cpu_ptr(&swp_buf, cpu); + if (buf->folio) { + folio_put(buf->folio); + buf->folio = NULL; + } + } + return ret; +} +core_initcall(batch_swpin_buffer_init); + +static struct folio *alloc_batched_swap_folios(struct vm_fault *vmf, + struct batch_swpin_buffer **buf, struct folio **folios, + swp_entry_t entry) +{ + unsigned long haddr = ALIGN_DOWN(vmf->address, BATCH_SWPIN_SIZE); + struct batch_swpin_buffer *sbuf = raw_cpu_ptr(&swp_buf); + struct folio *folio = sbuf->folio; + unsigned long addr; + int i; + + if (unlikely(!folio)) + return NULL; + + for (i = 0; i < BATCH_SWPIN_COUNT; i++) { + addr = haddr + i * PAGE_SIZE; + folios[i] = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vmf->vma, addr); + if (!folios[i]) + goto err; + if (mem_cgroup_swapin_charge_folio(folios[i], vmf->vma->vm_mm, + GFP_KERNEL, entry)) + goto err; + } + + mutex_lock(&sbuf->mutex); + *buf = sbuf; +#ifdef CONFIG_MEMCG + folio->memcg_data = (*folios)->memcg_data; +#endif + return folio; + +err: + for (i--; i >= 0; i--) + folio_put(folios[i]); + return NULL; +} + +static void fill_batched_swap_folios(struct vm_fault *vmf, + void *shadow, struct batch_swpin_buffer *buf, + struct folio *folio, struct folio **folios) +{ + unsigned long haddr = ALIGN_DOWN(vmf->address, BATCH_SWPIN_SIZE); + unsigned long addr; + int i; + + for (i = 0; i < BATCH_SWPIN_COUNT; i++) { + addr = haddr + i * PAGE_SIZE; + __folio_set_locked(folios[i]); + __folio_set_swapbacked(folios[i]); + if (shadow) + workingset_refault(folios[i], shadow); + folio_add_lru(folios[i]); + copy_user_highpage(&folios[i]->page, folio_page(folio, i), + addr, vmf->vma); + if (folio_test_uptodate(folio)) + folio_mark_uptodate(folios[i]); + } + + folio->flags &= ~(PAGE_FLAGS_CHECK_AT_PREP & ~(1UL << PG_head)); + mutex_unlock(&buf->mutex); +} + +static struct folio *alloc_swap_folio(struct vm_fault *vmf, + struct batch_swpin_buffer **buf, + struct folio **folios) { struct vm_area_struct *vma = vmf->vma; unsigned long orders; @@ -4180,6 +4283,9 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf) pte_unmap_unlock(pte, ptl); + if (!orders) + goto fallback; + /* Try allocating the highest of the remaining orders. */ gfp = vma_thp_gfp_mask(vma); while (orders) { @@ -4194,14 +4300,29 @@ static struct folio *alloc_swap_folio(struct vm_fault *vmf) order = next_order(&orders, order); } + /* + * During swap-out, a THP might have been compressed into multiple + * order-2 blocks to optimize CPU usage and compression ratio. + * Attempt to batch swap-in 4 smaller folios to ensure they are + * decompressed together as a single unit only once. + */ + return alloc_batched_swap_folios(vmf, buf, folios, entry); + fallback: return __alloc_swap_folio(vmf); } #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ -static struct folio *alloc_swap_folio(struct vm_fault *vmf) +static struct folio *alloc_swap_folio(struct vm_fault *vmf, + struct batch_swpin_buffer **buf, + struct folio **folios) { return __alloc_swap_folio(vmf); } +static inline void fill_batched_swap_folios(struct vm_fault *vmf, + void *shadow, struct batch_swpin_buffer *buf, + struct folio *folio, struct folio **folios) +{ +} #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ static DECLARE_WAIT_QUEUE_HEAD(swapcache_wq); @@ -4216,6 +4337,8 @@ static DECLARE_WAIT_QUEUE_HEAD(swapcache_wq); */ vm_fault_t do_swap_page(struct vm_fault *vmf) { + struct folio *folios[BATCH_SWPIN_COUNT] = { NULL }; + struct batch_swpin_buffer *buf = NULL; struct vm_area_struct *vma = vmf->vma; struct folio *swapcache, *folio = NULL; DECLARE_WAITQUEUE(wait, current); @@ -4228,7 +4351,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) pte_t pte; vm_fault_t ret = 0; void *shadow = NULL; - int nr_pages; + int nr_pages, i; unsigned long page_idx; unsigned long address; pte_t *ptep; @@ -4296,7 +4419,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && __swap_count(entry) == 1) { /* skip swapcache */ - folio = alloc_swap_folio(vmf); + folio = alloc_swap_folio(vmf, &buf, folios); if (folio) { __folio_set_locked(folio); __folio_set_swapbacked(folio); @@ -4327,10 +4450,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) mem_cgroup_swapin_uncharge_swap(entry, nr_pages); shadow = get_shadow_from_swap_cache(entry); - if (shadow) + if (shadow && !buf) workingset_refault(folio, shadow); - - folio_add_lru(folio); + if (!buf) + folio_add_lru(folio); /* To provide entry to swap_read_folio() */ folio->swap = entry; @@ -4361,6 +4484,16 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) count_vm_event(PGMAJFAULT); count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); page = folio_file_page(folio, swp_offset(entry)); + /* + * Copy data into batched small folios from the large + * folio buffer + */ + if (buf) { + fill_batched_swap_folios(vmf, shadow, buf, folio, folios); + folio = folios[0]; + page = &folios[0]->page; + goto do_map; + } } else if (PageHWPoison(page)) { /* * hwpoisoned dirty swapcache pages are kept for killing @@ -4415,6 +4548,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) lru_add_drain(); } +do_map: folio_throttle_swaprate(folio, GFP_KERNEL); /* @@ -4431,8 +4565,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) } /* allocated large folios for SWP_SYNCHRONOUS_IO */ - if (folio_test_large(folio) && !folio_test_swapcache(folio)) { - unsigned long nr = folio_nr_pages(folio); + if ((folio_test_large(folio) || buf) && !folio_test_swapcache(folio)) { + unsigned long nr = buf ? BATCH_SWPIN_COUNT : folio_nr_pages(folio); unsigned long folio_start = ALIGN_DOWN(vmf->address, nr * PAGE_SIZE); unsigned long idx = (vmf->address - folio_start) / PAGE_SIZE; pte_t *folio_ptep = vmf->pte - idx; @@ -4527,6 +4661,42 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) } } + /* Batched mapping of allocated small folios for SWP_SYNCHRONOUS_IO */ + if (buf) { + for (i = 0; i < nr_pages; i++) + arch_swap_restore(swp_entry(swp_type(entry), + swp_offset(entry) + i), folios[i]); + swap_free_nr(entry, nr_pages); + add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages); + add_mm_counter(vma->vm_mm, MM_SWAPENTS, -nr_pages); + rmap_flags |= RMAP_EXCLUSIVE; + for (i = 0; i < nr_pages; i++) { + unsigned long addr = address + i * PAGE_SIZE; + + pte = mk_pte(&folios[i]->page, vma->vm_page_prot); + if (pte_swp_soft_dirty(vmf->orig_pte)) + pte = pte_mksoft_dirty(pte); + if (pte_swp_uffd_wp(vmf->orig_pte)) + pte = pte_mkuffd_wp(pte); + if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) && + !pte_needs_soft_dirty_wp(vma, pte)) { + pte = pte_mkwrite(pte, vma); + if ((vmf->flags & FAULT_FLAG_WRITE) && (i == page_idx)) { + pte = pte_mkdirty(pte); + vmf->flags &= ~FAULT_FLAG_WRITE; + } + } + flush_icache_page(vma, &folios[i]->page); + folio_add_new_anon_rmap(folios[i], vma, addr, rmap_flags); + set_pte_at(vma->vm_mm, addr, ptep + i, pte); + arch_do_swap_page_nr(vma->vm_mm, vma, addr, pte, pte, 1); + if (i == page_idx) + vmf->orig_pte = pte; + folio_unlock(folios[i]); + } + goto wp_page; + } + /* * Some architectures may have to restore extra metadata to the page * when reading from swap. This metadata may be indexed by swap entry @@ -4612,6 +4782,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_put(swapcache); } +wp_page: if (vmf->flags & FAULT_FLAG_WRITE) { ret |= do_wp_page(vmf); if (ret & VM_FAULT_ERROR) @@ -4638,9 +4809,19 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); out_page: - folio_unlock(folio); + if (!buf) { + folio_unlock(folio); + } else { + for (i = 0; i < BATCH_SWPIN_COUNT; i++) + folio_unlock(folios[i]); + } out_release: - folio_put(folio); + if (!buf) { + folio_put(folio); + } else { + for (i = 0; i < BATCH_SWPIN_COUNT; i++) + folio_put(folios[i]); + } if (folio != swapcache && swapcache) { folio_unlock(swapcache); folio_put(swapcache);