From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) (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 5DFA531076D for ; Sun, 16 Nov 2025 18:12:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763316777; cv=none; b=V9bsuUKlPzbFdEMsmQca7/YjThvWxHv0kDfr4dV71Wg8iurbbw2+CAGN3AyVwq3QwUOEZ8+vnasRnOGt1/qVmEuqdN/bwihByuJjUrvp5jteh+7hoyQCpsgvvBMg4IikqQdMWMFtZ3V2fxZIVmkHdJqYuwmrGv2B9Dk7jiZrvVQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763316777; c=relaxed/simple; bh=Jc6png9Q3LMYJntNeMKW9Wklrk239lekg9Mp6dWzGWU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IgP1G1W/Vim07glO0UNP7J3A82NFTQ5zwKUpZpGRNog5AbiktCNUyEz6Yhf65cWgklInkBOGd/QsqHzM1Lg2oD7AXlhMZyzqXMOexZx0+6w6fDiO7rrVAMimPqs01FFRCT7DwPT6RB38u0ns1N+KAzbEgcbXyBvPvRAOtvExfmI= 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=XjH8h0ca; arc=none smtp.client-ip=209.85.222.174 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="XjH8h0ca" Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-8b23b6d9f11so359151085a.3 for ; Sun, 16 Nov 2025 10:12:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763316774; x=1763921574; 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=xZOKrPxmb2yknUYuBNcmT2AdChM+0jcdM9YzJtn8wWw=; b=XjH8h0caumKk280vpBqYOCwJc4lFUB6BVQOr4dpysow8hIYW6aF9iJIqedYN3cq8+e 7G9mUXtHZlqOET/9HepI2+FBkr3ifZdvDtM1W7X64p569MW6ahcQ4yHsVSBquL5MdK0+ 7+hCxSTzEil2x/N/B0lc3orAUHdrGrTjXQFg/VD7Rp2G0noM9Hh1NxfJ/FpZmCmb44lz 2+Yw0FRRmYEOk9BK0POXsZ16Af31hVn3J0xtIr7D5NMXBgrlBC3hwRJFfQ9AVk1cTjiH jXZjCXQjBC/2OGcc9elnW28gNxc3wJqc6aGWZPxM5TlttpY6vpE1xJmMkQedKFLj0PRV /3gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763316774; x=1763921574; 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=xZOKrPxmb2yknUYuBNcmT2AdChM+0jcdM9YzJtn8wWw=; b=cKP63QmJlHSmVio8j0HORw297V9iBcEr/HWfySvajih4Pgj5oCQYMt3bWJHDTYLVd8 qgvLgJ0SQmbt/6BZBGJLykealbEeAL+TpSouuZykRwv7ftEukVCL0P7nIaCsLRmgwnDH BqjVpotaLRUQUzEu9pE9JSOzsmNlf7Mw0Kf/7MKB46WLEAMrObKja3B5+inKK2cJhUq5 /tb57h9e6695mGzP3d73nw1woTCjfNCosS+F7hZml0uhxFKnOZHlMojNJJZdQflWP/xl 7ig2zpd3sWEW4nWRw0kTrkI+fbURht17B4pDbJ5wz66rOHmsu2oEpmdcw8WTuH9/Ud8i PJBQ== X-Forwarded-Encrypted: i=1; AJvYcCWMguQXSxgjmnsBduxc5meHXoJ533LTpu72NWeKkUO2AxCsJZcrIAjWdWsnKU94lMyLDGh9lAOJcw9o1W8=@vger.kernel.org X-Gm-Message-State: AOJu0YwMxQ6qIGtw9iC7LpIYi1FMivkzWSxvzhRNcl1J5c2Yb1t93ln1 5DIRQ8pL6/bxJ+S6IlgHbSpI3gVBe2CS7lmhlJI9yg9bkJP5pxE8wYwr X-Gm-Gg: ASbGncuE+bxPs4jHfjANiBl71YsaP4sb8A/ogX38TDuo0x/SzrwJrysorlU54rWh63N BxX4tLQdYfGWGmzMBljvv3cER1l1WqQtyxJgrfjQxBFAV49gSlOu2MvVVSMaLR0OjCNwKcTfDYv Cgm7vhpoQa5rjA96iK5nL/hEoMI4jsDaz9mL9bJBxMf6lJ54t5mdtQabBXWCTqsb+dyKz7Z4FRt XaDyUNdxMpk6TDIl6H6ZBNHPGhLgiS0aWQhvb23AYJhE5hIdyPuqumnoJHxCgpCphZZHPWoKllg s5baO9aOgmLFJDW46iRDTWxGXb7Gqa45iEwm3+0F/YZBa+ocEZt1XdeqL93TwHcgFRA1yjsHnsQ Qg5qZOgYzqOyv5nk+WkD+qqf5wW5XzAbMucH7Pk+zzpNlDXdi1CEyLpCZg/AY63aF9A8bDOAIeu ebRQR8I5tAKCKiLlsKK729p5TqXJkqTiiNFkJANAe4N75Uoq3AWi/2 X-Google-Smtp-Source: AGHT+IGOdJP/EsSTwaVr7AFygqAdGBuot84duSfzZCknJi9NXOfM5aRT2Xo2RnGeSbtxqEmDaKfJKQ== X-Received: by 2002:a05:620a:370e:b0:88b:72c0:2169 with SMTP id af79cd13be357-8b2c31d26b4mr1257853385a.90.1763316774218; Sun, 16 Nov 2025 10:12:54 -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.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Nov 2025 10:12:53 -0800 (PST) From: Kairui Song Date: Mon, 17 Nov 2025 02:11:50 +0800 Subject: [PATCH v2 09/19] mm, swap: swap entry of a bad slot should not be considered as swapped out 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-9-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=4721; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=sgwq02tPOBUNRyuiV2R8t3DdMa9GcyMTM6y5wg237JM=; b=0sOxzrOyuIFS8YnjIQJYvn9L24bV5TicCy3kyR9s2zrzrBPagFA+d4AP3z8w2OjrrECblyayO QKmE5kh6UmBBVxbf83nXuSr+wKaEnOGsK2U7R/mMwC5jEOk60axXTvs X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song When checking if a swap entry is swapped out, we simply check if the bitwise result of the count value is larger than 0. But SWAP_MAP_BAD will also be considered as a swao count value larger than 0. SWAP_MAP_BAD being considered as a count value larger than 0 is useful for the swap allocator: they will be seen as a used slot, so the allocator will skip them. But for the swapped out check, this isn't correct. There is currently no observable issue. The swapped out check is only useful for readahead and folio swapped-out status check. For readahead, the swap cache layer will abort upon checking and updating the swap map. For the folio swapped out status check, the swap allocator will never allocate an entry of bad slots to folio, so that part is fine too. The worst that could happen now is redundant allocation/freeing of folios and waste CPU time. This also makes it easier to get rid of swap map checking and update during folio insertion in the swap cache layer. Signed-off-by: Kairui Song --- include/linux/swap.h | 6 ++++-- mm/swap_state.c | 4 ++-- mm/swapfile.c | 22 +++++++++++----------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index bf72b548a96d..936fa8f9e5f3 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -466,7 +466,8 @@ int find_first_swap(dev_t *device); extern unsigned int count_swap_pages(int, int); extern sector_t swapdev_block(int, pgoff_t); extern int __swap_count(swp_entry_t entry); -extern bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry); +extern bool swap_entry_swapped(struct swap_info_struct *si, + unsigned long offset); extern int swp_swapcount(swp_entry_t entry); struct backing_dev_info; extern struct swap_info_struct *get_swap_device(swp_entry_t entry); @@ -535,7 +536,8 @@ static inline int __swap_count(swp_entry_t entry) return 0; } -static inline bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry) +static inline bool swap_entry_swapped(struct swap_info_struct *si, + unsigned long offset) { return false; } diff --git a/mm/swap_state.c b/mm/swap_state.c index 826bec661305..cbcebda22d2e 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -527,8 +527,8 @@ struct folio *swap_cache_alloc_folio(swp_entry_t entry, gfp_t gfp_mask, if (folio) return folio; - /* Skip allocation for unused swap slot for readahead path. */ - if (!swap_entry_swapped(si, entry)) + /* Skip allocation for unused and bad swap slot for readahead. */ + if (!swap_entry_swapped(si, swp_offset(entry))) return NULL; /* Allocate a new folio to be added into the swap cache. */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 580adf9d93bd..d2e60734ce8f 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1765,21 +1765,21 @@ int __swap_count(swp_entry_t entry) return swap_count(si->swap_map[offset]); } -/* - * How many references to @entry are currently swapped out? - * This does not give an exact answer when swap count is continued, - * but does include the high COUNT_CONTINUED flag to allow for that. +/** + * swap_entry_swapped - Check if the swap entry at @offset is swapped. + * @si: the swap device. + * @offset: offset of the swap entry. */ -bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry) +bool swap_entry_swapped(struct swap_info_struct *si, unsigned long offset) { - pgoff_t offset = swp_offset(entry); struct swap_cluster_info *ci; int count; ci = swap_cluster_lock(si, offset); count = swap_count(si->swap_map[offset]); swap_cluster_unlock(ci); - return !!count; + + return count && count != SWAP_MAP_BAD; } /* @@ -1865,7 +1865,7 @@ static bool folio_swapped(struct folio *folio) return false; if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!folio_test_large(folio))) - return swap_entry_swapped(si, entry); + return swap_entry_swapped(si, swp_offset(entry)); return swap_page_trans_huge_swapped(si, entry, folio_order(folio)); } @@ -3677,10 +3677,10 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) count = si->swap_map[offset + i]; /* - * swapin_readahead() doesn't check if a swap entry is valid, so the - * swap entry could be SWAP_MAP_BAD. Check here with lock held. + * Allocator never allocates bad slots, and readahead is guarded + * by swap_entry_swapped. */ - if (unlikely(swap_count(count) == SWAP_MAP_BAD)) { + if (WARN_ON(swap_count(count) == SWAP_MAP_BAD)) { err = -ENOENT; goto unlock_out; } -- 2.51.2