/* * This must be called only on folios that have * been verified to be in the swap cache.
*/ void __delete_from_swap_cache(struct folio *folio,
swp_entry_t entry, void *shadow)
{ struct address_space *address_space = swap_address_space(entry); int i; long nr = folio_nr_pages(folio);
pgoff_t idx = swap_cache_index(entry);
XA_STATE(xas, &address_space->i_pages, idx);
/* * This must be called only on folios that have * been verified to be in the swap cache and locked. * It will never put the folio into the free list, * the caller has a reference on the folio.
*/ void delete_from_swap_cache(struct folio *folio)
{
swp_entry_t entry = folio->swap; struct address_space *address_space = swap_address_space(entry);
/* search the next swapcache until we meet end */
curr = ALIGN((curr + 1), SWAP_ADDRESS_SPACE_PAGES); if (curr > end) break;
}
}
/* * If we are the only user, then try to free up the swap cache. * * Its ok to check the swapcache flag without the folio lock * here because we are going to recheck again inside * folio_free_swap() _with_ the lock. * - Marcelo
*/ void free_swap_cache(struct folio *folio)
{ if (folio_test_swapcache(folio) && !folio_mapped(folio) &&
folio_trylock(folio)) {
folio_free_swap(folio);
folio_unlock(folio);
}
}
/* * Freeing a folio and also freeing any swap cache associated with * this folio if it is the last user.
*/ void free_folio_and_swap_cache(struct folio *folio)
{
free_swap_cache(folio); if (!is_huge_zero_folio(folio))
folio_put(folio);
}
/* * Passed an array of pages, drop them all from swapcache and then release * them. They are removed from the LRU and freed if this is their last use.
*/ void free_pages_and_swap_cache(struct encoded_page **pages, int nr)
{ struct folio_batch folios; unsignedint refs[PAGEVEC_SIZE];
folio_batch_init(&folios); for (int i = 0; i < nr; i++) { struct folio *folio = page_folio(encoded_page_ptr(pages[i]));
/* * Lookup a swap entry in the swap cache. A found folio will be returned * unlocked and with its refcount incremented - we rely on the kernel * lock getting page table operations atomic even if we drop the folio * lock before returning. * * Caller must lock the swap device or hold a reference to keep it valid.
*/ struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsignedlong addr)
{ struct folio *folio;
/* * At the moment, we don't support PG_readahead for anon THP * so let's bail out rather than confusing the readahead stat.
*/ if (unlikely(folio_test_large(folio))) return folio;
readahead = folio_test_clear_readahead(folio); if (vma && vma_ra) { unsignedlong ra_val; int win, hits;
if (readahead) {
count_vm_event(SWAP_RA_HIT); if (!vma || !vma_ra)
atomic_inc(&swapin_readahead_hits);
}
} else {
folio = NULL;
}
return folio;
}
/** * filemap_get_incore_folio - Find and get a folio from the page or swap caches. * @mapping: The address_space to search. * @index: The page cache index. * * This differs from filemap_get_folio() in that it will also look for the * folio in the swap cache. * * Return: The found folio or %NULL.
*/ struct folio *filemap_get_incore_folio(struct address_space *mapping,
pgoff_t index)
{
swp_entry_t swp; struct swap_info_struct *si; struct folio *folio = filemap_get_entry(mapping, index);
if (!folio) return ERR_PTR(-ENOENT); if (!xa_is_value(folio)) return folio; if (!shmem_mapping(mapping)) return ERR_PTR(-ENOENT);
swp = radix_to_swp_entry(folio); /* There might be swapin error entries in shmem mapping. */ if (non_swap_entry(swp)) return ERR_PTR(-ENOENT); /* Prevent swapoff from happening to us */
si = get_swap_device(swp); if (!si) return ERR_PTR(-ENOENT);
index = swap_cache_index(swp);
folio = filemap_get_folio(swap_address_space(swp), index);
put_swap_device(si); return folio;
}
*new_page_allocated = false; for (;;) { int err; /* * First check the swap cache. Since this is normally * called after swap_cache_get_folio() failed, re-calling * that would confuse statistics.
*/
folio = filemap_get_folio(swap_address_space(entry),
swap_cache_index(entry)); if (!IS_ERR(folio)) goto got_folio;
/* * Just skip read ahead for unused swap slot.
*/ if (!swap_entry_swapped(si, entry)) goto put_and_return;
/* * Get a new folio to read into from swap. Allocate it now if * new_folio not exist, before marking swap_map SWAP_HAS_CACHE, * when -EEXIST will cause any racers to loop around until we * add it to cache.
*/ if (!new_folio) {
new_folio = folio_alloc_mpol(gfp_mask, 0, mpol, ilx, numa_node_id()); if (!new_folio) goto put_and_return;
}
/* * Swap entry may have been freed since our caller observed it.
*/
err = swapcache_prepare(entry, 1); if (!err) break; elseif (err != -EEXIST) goto put_and_return;
/* * Protect against a recursive call to __read_swap_cache_async() * on the same entry waiting forever here because SWAP_HAS_CACHE * is set but the folio is not the swap cache yet. This can * happen today if mem_cgroup_swapin_charge_folio() below * triggers reclaim through zswap, which may call * __read_swap_cache_async() in the writeback path.
*/ if (skip_if_exists) goto put_and_return;
/* * We might race against __delete_from_swap_cache(), and * stumble across a swap_map entry whose SWAP_HAS_CACHE * has not yet been cleared. Or race against another * __read_swap_cache_async(), which has set SWAP_HAS_CACHE * in swap_map, but not yet added its folio to swap cache.
*/
schedule_timeout_uninterruptible(1);
}
/* * The swap entry is ours to swap in. Prepare the new folio.
*/
__folio_set_locked(new_folio);
__folio_set_swapbacked(new_folio);
if (mem_cgroup_swapin_charge_folio(new_folio, NULL, gfp_mask, entry)) goto fail_unlock;
/* May fail (-ENOMEM) if XArray node allocation failed. */ if (add_to_swap_cache(new_folio, entry, gfp_mask & GFP_RECLAIM_MASK, &shadow)) goto fail_unlock;
memcg1_swapin(entry, 1);
if (shadow)
workingset_refault(new_folio, shadow);
/* Caller will initiate read into locked new_folio */
folio_add_lru(new_folio);
*new_page_allocated = true;
folio = new_folio;
got_folio:
result = folio; goto put_and_return;
/* * Locate a page of swap in physical memory, reserving swap cache space * and reading the disk if it is not already cached. * A failure return means that either the page allocation failed or that * the swap entry is no longer in use. * * get/put_swap_device() aren't needed to call this function, because * __read_swap_cache_async() call them and swap_read_folio() holds the * swap cache folio lock.
*/ struct folio *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct vm_area_struct *vma, unsignedlong addr, struct swap_iocb **plug)
{ struct swap_info_struct *si; bool page_allocated; struct mempolicy *mpol;
pgoff_t ilx; struct folio *folio;
si = get_swap_device(entry); if (!si) return NULL;
staticunsignedint __swapin_nr_pages(unsignedlong prev_offset, unsignedlong offset, int hits, int max_pages, int prev_win)
{ unsignedint pages, last_ra;
/* * This heuristic has been found to work well on both sequential and * random loads, swapping to hard disk or to SSD: please don't ask * what the "+ 2" means, it just happens to work well, that's all.
*/
pages = hits + 2; if (pages == 2) { /* * We can have no readahead hits to judge by: but must not get * stuck here forever, so check for an adjacent offset instead * (and don't even bother to check whether swap type is same).
*/ if (offset != prev_offset + 1 && offset != prev_offset - 1)
pages = 1;
} else { unsignedint roundup = 4; while (roundup < pages)
roundup <<= 1;
pages = roundup;
}
if (pages > max_pages)
pages = max_pages;
/* Don't shrink readahead too fast */
last_ra = prev_win / 2; if (pages < last_ra)
pages = last_ra;
/** * swap_cluster_readahead - swap in pages in hope we need them soon * @entry: swap entry of this memory * @gfp_mask: memory allocation flags * @mpol: NUMA memory allocation policy to be applied * @ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE * * Returns the struct folio for entry and addr, after queueing swapin. * * Primitive swap readahead code. We simply read an aligned block of * (1 << page_cluster) entries in the swap area. This method is chosen * because it doesn't cost us any seek time. We also make sure to queue * the 'original' request together with the readahead ones... * * Note: it is intentional that the same NUMA policy and interleave index * are used for every page of the readahead: neighbouring pages on swap * are fairly likely to have been swapped out from the same node.
*/ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx)
{ struct folio *folio; unsignedlong entry_offset = swp_offset(entry); unsignedlong offset = entry_offset; unsignedlong start_offset, end_offset; unsignedlong mask; struct swap_info_struct *si = swp_swap_info(entry); struct blk_plug plug; struct swap_iocb *splug = NULL; bool page_allocated;
mask = swapin_nr_pages(offset) - 1; if (!mask) goto skip;
/* Read a page_cluster sized and aligned cluster around offset. */
start_offset = offset & ~mask;
end_offset = offset | mask; if (!start_offset) /* First page is swap header. */
start_offset++; if (end_offset >= si->max)
end_offset = si->max - 1;
blk_start_plug(&plug); for (offset = start_offset; offset <= end_offset ; offset++) { /* Ok, do the async read-ahead now */
folio = __read_swap_cache_async(
swp_entry(swp_type(entry), offset),
gfp_mask, mpol, ilx, &page_allocated, false); if (!folio) continue; if (page_allocated) {
swap_read_folio(folio, &splug); if (offset != entry_offset) {
folio_set_readahead(folio);
count_vm_event(SWAP_RA);
}
}
folio_put(folio);
}
blk_finish_plug(&plug);
swap_read_unplug(splug);
lru_add_drain(); /* Push any new pages onto the LRU now */
skip: /* The page was likely read above, so no need for plugging here */
folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx,
&page_allocated, false); if (unlikely(page_allocated))
swap_read_folio(folio, NULL); return folio;
}
int init_swap_address_space(unsignedint type, unsignedlong nr_pages)
{ struct address_space *spaces, *space; unsignedint i, nr;
nr = DIV_ROUND_UP(nr_pages, SWAP_ADDRESS_SPACE_PAGES);
spaces = kvcalloc(nr, sizeof(struct address_space), GFP_KERNEL); if (!spaces) return -ENOMEM; for (i = 0; i < nr; i++) {
space = spaces + i;
xa_init_flags(&space->i_pages, XA_FLAGS_LOCK_IRQ);
atomic_set(&space->i_mmap_writable, 0);
space->a_ops = &swap_aops; /* swap cache doesn't use writeback related tags */
mapping_set_no_writeback_tags(space);
}
nr_swapper_spaces[type] = nr;
swapper_spaces[type] = spaces;
if (faddr == prev_faddr + PAGE_SIZE)
left = faddr; elseif (prev_faddr == faddr + PAGE_SIZE)
left = faddr - (win << PAGE_SHIFT) + PAGE_SIZE; else
left = faddr - (((win - 1) / 2) << PAGE_SHIFT);
right = left + (win << PAGE_SHIFT); if ((long)left < 0)
left = 0;
*start = max3(left, vma->vm_start, faddr & PMD_MASK);
*end = min3(right, vma->vm_end, (faddr & PMD_MASK) + PMD_SIZE);
return win;
}
/** * swap_vma_readahead - swap in pages in hope we need them soon * @targ_entry: swap entry of the targeted memory * @gfp_mask: memory allocation flags * @mpol: NUMA memory allocation policy to be applied * @targ_ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE * @vmf: fault information * * Returns the struct folio for entry and addr, after queueing swapin. * * Primitive swap readahead code. We simply read in a few pages whose * virtual addresses are around the fault address in the same vma. * * Caller must hold read mmap_lock if vmf->vma is not NULL. *
*/ staticstruct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t targ_ilx, struct vm_fault *vmf)
{ struct blk_plug plug; struct swap_iocb *splug = NULL; struct folio *folio;
pte_t *pte = NULL, pentry; int win; unsignedlong start, end, addr;
swp_entry_t entry;
pgoff_t ilx; bool page_allocated;
if (!pte++) {
pte = pte_offset_map(vmf->pmd, addr); if (!pte) break;
}
pentry = ptep_get_lockless(pte); if (!is_swap_pte(pentry)) continue;
entry = pte_to_swp_entry(pentry); if (unlikely(non_swap_entry(entry))) continue;
pte_unmap(pte);
pte = NULL; /* * Readahead entry may come from a device that we are not * holding a reference to, try to grab a reference, or skip.
*/ if (swp_type(entry) != swp_type(targ_entry)) {
si = get_swap_device(entry); if (!si) continue;
}
folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx,
&page_allocated, false); if (si)
put_swap_device(si); if (!folio) continue; if (page_allocated) {
swap_read_folio(folio, &splug); if (addr != vmf->address) {
folio_set_readahead(folio);
count_vm_event(SWAP_RA);
}
}
folio_put(folio);
} if (pte)
pte_unmap(pte);
blk_finish_plug(&plug);
swap_read_unplug(splug);
lru_add_drain();
skip: /* The folio was likely read above, so no need for plugging here */
folio = __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx,
&page_allocated, false); if (unlikely(page_allocated))
swap_read_folio(folio, NULL); return folio;
}
/** * swapin_readahead - swap in pages in hope we need them soon * @entry: swap entry of this memory * @gfp_mask: memory allocation flags * @vmf: fault information * * Returns the struct folio for entry and addr, after queueing swapin. * * It's a main entry function for swap readahead. By the configuration, * it will read ahead blocks by cluster-based(ie, physical disk based) * or vma-based(ie, virtual address based on faulty address) readahead.
*/ struct folio *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, struct vm_fault *vmf)
{ struct mempolicy *mpol;
pgoff_t ilx; struct folio *folio;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.