From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B586430FF2F for ; Sun, 16 Nov 2025 18:12:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763316752; cv=none; b=gYBRhsk252tIYRb8biAnrCDuP219nEt5aFB9FmUm/+sF2stdG57XZJWMNYqHwNRvXBficz04+/QR9Zynz5ynNiIUN9qZabJl6xhUFVk05R3aWWDBVYKggJIQabE7EpsXxQI4TYIUDJBJh930FeFGyt4Cig03Exsc5IvQRk1lPLA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763316752; c=relaxed/simple; bh=CO5b/2CYPoazWQ550i1BNehnt2KmCcxphOBeB36w38A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IfaWwVi88u2mFB5CBGz+7g33yJlhQvK4RqGAgXBdeptJHNYjH9x4NPluL/0OmDDEWMt2tz4gN+wlckbthUxz4efjgRDio8Ar2bgM/AmCQCeVyVLsYCu0GXjntQ1es66Lrb8iA2TgCrBMExX5sNNrYNxGF7vcOVQNWa9rLTTH/aQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=k+Af5Qph; arc=none smtp.client-ip=209.85.222.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="k+Af5Qph" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-8b144ec3aa8so373966785a.2 for ; Sun, 16 Nov 2025 10:12:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763316749; x=1763921549; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=l5EyM9JLLcI0p9xQ+9GuOSt7RQAmk5ALAj7+tQKQk5U=; b=k+Af5QphjueLlv9tfO4I/s9XTx00JuffvVREaPno367TG80FqbWaW7bQ2OeEGlQlKR ZycCIW/+ByDMC5hhuk3M+NHqbks72QhQFlaLr3ElTj0BrkNu6QtkTGJdkkQC+pegEmWg p1RHLwy1j1l5S4fsXHW6Bqyf2iNXW2WG0nzqEL9A7zg4TFHyr/clUnhc0IvWw7/BEolW sjNIPNyTL6wT/38X4xljqFd+qjnbu0EsmNn3PdUvoMaNAE6EVMEgR2GT5+4UYgvVYj2C EioQaY/wgsN1OUrwSIY1UOTZwnuuEjFI0ZNGrbQrxMgRL7PzEUPxzbZt44ODIlZxJ1kC z5iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763316749; x=1763921549; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=l5EyM9JLLcI0p9xQ+9GuOSt7RQAmk5ALAj7+tQKQk5U=; b=cDA4gtHQo3UVbWr0gdNRw3TogWi1WTsQKN6Dgy775ZGX8rGZhW+M+/w7ZSCSli+9VL zJc6FL0mh1ptOKPBpLMRuFiWOAkyZ2ZutzlE7H5gZ7vARaXixFSRYWu+ntI+Xx5ClB5j OHE8mlYXK8DzzsV5LmHhOt7b5WRumvIPnODQtInoib9lVxafY6oxqH0XSRzMKOluxAhT jPZF+wmpGBVPTRGEDrHLl2XPfgK7cVCoMMrDesY5pm4CdCPhdzdfbDiLFqpri5jP5NCi y+i+V5yl9bMDmdC9e6iPm9i6KCW6KsZ/A9+vWJuj37B8emD3I3zoXTgPD7xe4X8+f90b F2rA== X-Forwarded-Encrypted: i=1; AJvYcCWCe4hf0JcOwPTgQf8q3hhg1lnKaycz6W+Au1jAl20ArwSj+hUXGcJwjUH0cVx0mTuGgNJdTJKtfyYxWy8=@vger.kernel.org X-Gm-Message-State: AOJu0YzEzeEu1LqeeKfjzF4JxC1s8IxNu90Smhxk4cgDiDY+WUXDUCPZ 4VqMXaIVytq2dwUb1YHhQjbYT6A2y5pZAPtES7NtHsv31Nibt3dTTGR7 X-Gm-Gg: ASbGncuhY+wvm1z11i3hYGDAqewolduUad0Aa/AEyWBYrgb43R8PbOLtwNOWBYp4Qba 7Rxq+rQDANDPlzRV9BfMUpI+2QlAMWieqTzKR/5clly5/1PPXeE1hmZgsoaKu7gS0mLkQtB4sKl e4+zUIt8LY/4cHqKWThHsSlJAYDo46UZ8LLyxaxWQX8mcUs/qWdq6WGVbzVSDTGhkuidXPjecVA xXs1cIn5QzbHhz+eCiIHWshPS74MOhKgJWW/rMEwC2orE5mmetg4aeJ9p7A1f/KrAMTOsUF18Ko 8cxsfrZy66DfzylgEvCGdQSW13VD1WdY/IIJ9iKcxmtivpJy9FHQb+wJrqTz+JcaaLXo99KIAvS ERIhgPLpFV+Egq6MLiCyIlLF/UHnGM8CA9GtsYsw72CGODxY7bi72BOVoWIlkYIm+FJhb0fLP4F Q2mLLnXzM9/m4lwTZLZQd+DjffMwz3NsXXBIqqFCPBnw== X-Google-Smtp-Source: AGHT+IHQefRN+bjbWKY5VFWpotsL1pk/rclBTOsKTXipe0p4DuCnqbmJXiAmeZ/BgoYkjWuCZaWcOw== X-Received: by 2002:a05:620a:4869:b0:8b2:d72d:e41b with SMTP id af79cd13be357-8b2d72de8cdmr564625085a.44.1763316749357; Sun, 16 Nov 2025 10:12:29 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8b2e089b436sm305447785a.45.2025.11.16.10.12.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Nov 2025 10:12:28 -0800 (PST) From: Kairui Song Date: Mon, 17 Nov 2025 02:11:46 +0800 Subject: [PATCH v2 05/19] mm, swap: simplify the code and reduce indention Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20251117-swap-table-p2-v2-5-37730e6ea6d5@tencent.com> References: <20251117-swap-table-p2-v2-0-37730e6ea6d5@tencent.com> In-Reply-To: <20251117-swap-table-p2-v2-0-37730e6ea6d5@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Baoquan He , Barry Song , Chris Li , Nhat Pham , Yosry Ahmed , David Hildenbrand , Johannes Weiner , Youngjun Park , Hugh Dickins , Baolin Wang , Ying Huang , Kemeng Shi , Lorenzo Stoakes , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1763316712; l=4358; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=QCdSC5PjiK2CWn/AsMjUxzckDwd8J5J9QJFfp6q+46o=; b=iiHdIRbhcVHWdIq1mWA1h3WXak5y8YHl+lUzc9z4u5wBGAUQWUbwTFLzc+gzwvCBtApe3qrJ4 nY10gTrDhgbD8kAqxN4kBibudSouwodTDBa9h4EjPaSxrXDwhCVcc2x X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song Now swap cache is always used, multiple swap cache checks are no longer useful, remove them and reduce the code indention. No behavior change. Signed-off-by: Kairui Song --- mm/memory.c | 89 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 806becd6bad5..5bd417ae7f26 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4761,55 +4761,52 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out_release; page = folio_file_page(folio, swp_offset(entry)); - if (swapcache) { - /* - * Make sure folio_free_swap() or swapoff did not release the - * swapcache from under us. The page pin, and pte_same test - * below, are not enough to exclude that. Even if it is still - * swapcache, we need to check that the page's swap has not - * changed. - */ - if (unlikely(!folio_matches_swap_entry(folio, entry))) - goto out_page; - - if (unlikely(PageHWPoison(page))) { - /* - * hwpoisoned dirty swapcache pages are kept for killing - * owner processes (which may be unknown at hwpoison time) - */ - ret = VM_FAULT_HWPOISON; - goto out_page; - } - - /* - * KSM sometimes has to copy on read faults, for example, if - * folio->index of non-ksm folios would be nonlinear inside the - * anon VMA -- the ksm flag is lost on actual swapout. - */ - folio = ksm_might_need_to_copy(folio, vma, vmf->address); - if (unlikely(!folio)) { - ret = VM_FAULT_OOM; - folio = swapcache; - goto out_page; - } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) { - ret = VM_FAULT_HWPOISON; - folio = swapcache; - goto out_page; - } - if (folio != swapcache) - page = folio_page(folio, 0); + /* + * Make sure folio_free_swap() or swapoff did not release the + * swapcache from under us. The page pin, and pte_same test + * below, are not enough to exclude that. Even if it is still + * swapcache, we need to check that the page's swap has not + * changed. + */ + if (unlikely(!folio_matches_swap_entry(folio, entry))) + goto out_page; + if (unlikely(PageHWPoison(page))) { /* - * If we want to map a page that's in the swapcache writable, we - * have to detect via the refcount if we're really the exclusive - * owner. Try removing the extra reference from the local LRU - * caches if required. + * hwpoisoned dirty swapcache pages are kept for killing + * owner processes (which may be unknown at hwpoison time) */ - if ((vmf->flags & FAULT_FLAG_WRITE) && folio == swapcache && - !folio_test_ksm(folio) && !folio_test_lru(folio)) - lru_add_drain(); + ret = VM_FAULT_HWPOISON; + goto out_page; } + /* + * KSM sometimes has to copy on read faults, for example, if + * folio->index of non-ksm folios would be nonlinear inside the + * anon VMA -- the ksm flag is lost on actual swapout. + */ + folio = ksm_might_need_to_copy(folio, vma, vmf->address); + if (unlikely(!folio)) { + ret = VM_FAULT_OOM; + folio = swapcache; + goto out_page; + } else if (unlikely(folio == ERR_PTR(-EHWPOISON))) { + ret = VM_FAULT_HWPOISON; + folio = swapcache; + goto out_page; + } else if (folio != swapcache) + page = folio_page(folio, 0); + + /* + * If we want to map a page that's in the swapcache writable, we + * have to detect via the refcount if we're really the exclusive + * owner. Try removing the extra reference from the local LRU + * caches if required. + */ + if ((vmf->flags & FAULT_FLAG_WRITE) && + !folio_test_ksm(folio) && !folio_test_lru(folio)) + lru_add_drain(); + folio_throttle_swaprate(folio, GFP_KERNEL); /* @@ -4999,7 +4996,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) pte, pte, nr_pages); folio_unlock(folio); - if (folio != swapcache && swapcache) { + if (unlikely(folio != swapcache)) { /* * Hold the lock to avoid the swap entry to be reused * until we take the PT lock for the pte_same() check @@ -5037,7 +5034,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_unlock(folio); out_release: folio_put(folio); - if (folio != swapcache && swapcache) { + if (folio != swapcache) { folio_unlock(swapcache); folio_put(swapcache); } -- 2.51.2