[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <911dc3b4-c511-4ef2-a159-091780987965@redhat.com>
Date: Tue, 2 Sep 2025 12:18:27 +0200
From: David Hildenbrand <david@...hat.com>
To: Kairui Song <kasong@...cent.com>, linux-mm@...ck.org
Cc: Andrew Morton <akpm@...ux-foundation.org>,
Matthew Wilcox <willy@...radead.org>, Hugh Dickins <hughd@...gle.com>,
Chris Li <chrisl@...nel.org>, Barry Song <baohua@...nel.org>,
Baoquan He <bhe@...hat.com>, Nhat Pham <nphamcs@...il.com>,
Kemeng Shi <shikemeng@...weicloud.com>,
Baolin Wang <baolin.wang@...ux.alibaba.com>,
Ying Huang <ying.huang@...ux.alibaba.com>,
Johannes Weiner <hannes@...xchg.org>, Yosry Ahmed <yosryahmed@...gle.com>,
Lorenzo Stoakes <lorenzo.stoakes@...cle.com>, Zi Yan <ziy@...dia.com>,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH 2/9] mm, swap: always lock and check the swap cache folio
before use
On 22.08.25 21:20, Kairui Song wrote:
> From: Kairui Song <kasong@...cent.com>
>
> Swap cache lookup is lockless, it only increases the reference count
> of the returned folio. That's not enough to ensure a folio is stable in
> the swap cache, so the folio could be removed from the swap cache at any
> time. The caller always has to lock and check the folio before use.
>
> Document this as a comment, and introduce a helper for swap cache folio
> verification with proper sanity checks.
>
> Also, sanitize all current users to use this convention, and use the new
> helper when possible for easier debugging. Some existing callers won't
> cause any major problem right now, only trivial issues like incorrect
> readahead statistic (swapin) or wasted loop (swapoff). It's better to
> always follow this convention to make things robust.
>
> Signed-off-by: Kairui Song <kasong@...cent.com>
> ---
[...]
> +/**
> + * folio_contains_swap - Does this folio contain this swap entry?
> + * @folio: The folio.
> + * @entry: The swap entry to check against.
> + *
> + * Swap version of folio_contains()
> + *
> + * Context: The caller should have the folio locked to ensure
> + * nothing will move it out of the swap cache.
> + * Return: true or false.
> + */
I appreciate the kerneldoc.
Intuitively, this should be called "..._swap_entry".
But I wonder if "contains" is really the right term to use here. It's
more like that a swap entry "belongs to" (was assigned to) a folio, right?
Sure, we store the information in the folio, but the "contains" is a bit
weird.
folio_matches_swp_entry() maybe?
> +static inline bool folio_contains_swap(struct folio *folio, swp_entry_t entry)
> +{
const struct folio *
> + pgoff_t offset = swp_offset(entry);
> +
> + VM_WARN_ON_ONCE(!folio_test_locked(folio));
> + if (unlikely(!folio_test_swapcache(folio)))
> + return false;
> + if (unlikely(swp_type(entry) != swp_type(folio->swap)))
> + return false;
> + return offset - swp_offset(folio->swap) < folio_nr_pages(folio);
> +}
> +
> void show_swap_cache_info(void);
> void *get_shadow_from_swap_cache(swp_entry_t entry);
> int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
> @@ -144,6 +167,11 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)
> return 0;
> }
>
--
Cheers
David / dhildenb
Powered by blists - more mailing lists