* @subpage: if not NULL, only increase the swap count of this subpage.
*
* Typically called when the folio is unmapped and have its swap entry to
- * take its palce.
+ * take its place: Swap entries allocated to a folio has count == 0 and pinned
+ * by swap cache. The swap cache pin doesn't increase the swap count. This
+ * helper sets the initial count == 1 and increases the count as the folio is
+ * unmapped and swap entries referencing the slots are generated to replace
+ * the folio.
*
* Context: Caller must ensure the folio is locked and in the swap cache.
* NOTE: The caller also has to ensure there is no raced call to
return count < 0 ? 0 : count;
}
-static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,
- swp_entry_t entry, int order)
+/*
+ * folio_maybe_swapped - Test if a folio covers any swap slot with count > 0.
+ *
+ * Check if a folio is swapped. Holding the folio lock ensures the folio won't
+ * go from not-swapped to swapped because the initial swap count increment can
+ * only be done by folio_dup_swap, which also locks the folio. But a concurrent
+ * decrease of swap count is possible through swap_put_entries_direct, so this
+ * may return a false positive.
+ *
+ * Context: Caller must ensure the folio is locked and in the swap cache.
+ */
+static bool folio_maybe_swapped(struct folio *folio)
{
+ swp_entry_t entry = folio->swap;
struct swap_cluster_info *ci;
- unsigned int nr_pages = 1 << order;
- unsigned long roffset = swp_offset(entry);
- unsigned long offset = round_down(roffset, nr_pages);
- unsigned int ci_off;
- int i;
+ unsigned int ci_off, ci_end;
bool ret = false;
- ci = swap_cluster_lock(si, offset);
- if (nr_pages == 1) {
- ci_off = roffset % SWAPFILE_CLUSTER;
- if (swp_tb_get_count(__swap_table_get(ci, ci_off)))
- ret = true;
- goto unlock_out;
- }
- for (i = 0; i < nr_pages; i++) {
- ci_off = (offset + i) % SWAPFILE_CLUSTER;
- if (swp_tb_get_count(__swap_table_get(ci, ci_off))) {
- ret = true;
- break;
- }
- }
-unlock_out:
- swap_cluster_unlock(ci);
- return ret;
-}
-
-static bool folio_swapped(struct folio *folio)
-{
- swp_entry_t entry = folio->swap;
- struct swap_info_struct *si;
-
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
- si = __swap_entry_to_info(entry);
- if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!folio_test_large(folio)))
- return swap_entry_swapped(si, entry);
+ ci = __swap_entry_to_cluster(entry);
+ ci_off = swp_cluster_offset(entry);
+ ci_end = ci_off + folio_nr_pages(folio);
+ /*
+ * Extra locking not needed, folio lock ensures its swap entries
+ * won't be released, the backing data won't be gone either.
+ */
+ rcu_read_lock();
+ do {
+ if (__swp_tb_get_count(__swap_table_get(ci, ci_off))) {
+ ret = true;
+ break;
+ }
+ } while (++ci_off < ci_end);
+ rcu_read_unlock();
- return swap_page_trans_huge_swapped(si, entry, folio_order(folio));
+ return ret;
}
static bool folio_swapcache_freeable(struct folio *folio)
{
if (!folio_swapcache_freeable(folio))
return false;
- if (folio_swapped(folio))
+ if (folio_maybe_swapped(folio))
return false;
swap_cache_del_folio(folio);
*
* Context: Caller must ensure there is no race condition on the reference
* owner. e.g., locking the PTL of a PTE containing the entry being increased.
+ * Also the swap entry must have a count >= 1. Otherwise folio_dup_swap should
+ * be used.
*/
int swap_dup_entry_direct(swp_entry_t entry)
{
return -EINVAL;
}
+ /*
+ * The caller must be increasing the swap count from a direct
+ * reference of the swap slot (e.g. a swap entry in page table).
+ * So the swap count must be >= 1.
+ */
+ VM_WARN_ON_ONCE(!swap_entry_swapped(si, entry));
+
return swap_dup_entries_cluster(si, swp_offset(entry), 1);
}