#ifndef __MM_INTERNAL_H
#define __MM_INTERNAL_H
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/khugepaged.h>
#include <linux/mm.h>
#include <linux/mm_inline.h>
#include <linux/mmu_notifier.h>
#include <linux/pagemap.h>
#include <linux/pagewalk.h>
#include <linux/rmap.h>
#include <linux/swap.h>
#include <linux/leafops.h>
#include <linux/tracepoint-defs.h>
#include "vma.h"
struct folio_batch;
int __copy_remote_mm_str(struct mm_struct *mm, unsigned long addr,
void *buf, int len, unsigned int gup_flags);
bool workingset_test_recent(void *shadow, bool file, bool *workingset,
bool flush);
void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages);
void *workingset_eviction(struct folio *folio,
struct mem_cgroup *target_memcg);
void workingset_refault(struct folio *folio, void *shadow);
void workingset_activation(struct folio *folio);
void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma);
static inline bool folio_may_be_lru_cached(const struct folio *folio)
{
return !folio_test_large(folio);
}
static inline void lru_cache_enable(void)
{
atomic_dec(&lru_disable_count);
}
void lru_cache_disable(void);
void lru_add_drain(void);
void lru_add_drain_cpu(int cpu);
void lru_add_drain_cpu_zone(struct zone *zone);
void folio_deactivate(struct folio *folio);
void folio_mark_lazyfree(struct folio *folio);
unsigned long zone_reclaimable_pages(struct zone *zone);
unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
gfp_t gfp_mask, const nodemask_t *mask);
unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
int zone_idx);
#define MEMCG_RECLAIM_MAY_SWAP (1 << 1)
#define MEMCG_RECLAIM_PROACTIVE (1 << 2)
enum {
MIN_SWAPPINESS = 0,
MAX_SWAPPINESS = 200,
SWAPPINESS_ANON_ONLY = MAX_SWAPPINESS + 1,
};
unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
unsigned long nr_pages,
gfp_t gfp_mask,
unsigned int reclaim_options,
int *swappiness);
#ifdef CONFIG_NUMA
extern int sysctl_min_unmapped_ratio;
extern int sysctl_min_slab_ratio;
#endif
struct pagetable_move_control {
struct vm_area_struct *old;
struct vm_area_struct *new;
unsigned long old_addr;
unsigned long old_end;
unsigned long new_addr;
unsigned long len_in;
bool need_rmap_locks;
bool for_stack;
};
#define PAGETABLE_MOVE(name, old_, new_, old_addr_, new_addr_, len_) \
struct pagetable_move_control name = { \
.old = old_, \
.new = new_, \
.old_addr = old_addr_, \
.old_end = (old_addr_) + (len_), \
.new_addr = new_addr_, \
.len_in = len_, \
}
#define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\
__GFP_NOWARN|__GFP_RETRY_MAYFAIL|__GFP_NOFAIL|\
__GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\
__GFP_NOLOCKDEP)
#define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS))
#define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE)
#define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
#define WARN_ON_ONCE_GFP(cond, gfp) ({ \
static bool __section(".data..once") __warned; \
int __ret_warn_once = !!(cond); \
\
if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \
__warned = true; \
WARN_ON(1); \
} \
unlikely(__ret_warn_once); \
})
void page_writeback_init(void);
#define ENTIRELY_MAPPED 0x800000
#define FOLIO_PAGES_MAPPED (ENTIRELY_MAPPED - 1)
#define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */
static inline int folio_nr_pages_mapped(const struct folio *folio)
{
if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT))
return -1;
return atomic_read(&folio->_nr_pages_mapped) & FOLIO_PAGES_MAPPED;
}
static inline swp_entry_t folio_swap(swp_entry_t entry,
const struct folio *folio)
{
swp_entry_t swap = {
.val = ALIGN_DOWN(entry.val, folio_nr_pages(folio)),
};
return swap;
}
static inline void *folio_raw_mapping(const struct folio *folio)
{
unsigned long mapping = (unsigned long)folio->mapping;
return (void *)(mapping & ~FOLIO_MAPPING_FLAGS);
}
static inline void vma_close(struct vm_area_struct *vma)
{
if (vma->vm_ops && vma->vm_ops->close) {
vma->vm_ops->close(vma);
vma->vm_ops = &vma_dummy_vm_ops;
}
}
static inline int mmap_file(struct file *file, struct vm_area_struct *vma)
{
const unsigned long orig_start = vma->vm_start;
const unsigned long orig_end = vma->vm_end;
const vma_flags_t orig_flags = vma->flags;
int err;
err = vfs_mmap(file, vma);
if (unlikely(err || !vma->vm_ops))
vma->vm_ops = &vma_dummy_vm_ops;
if (unlikely(err))
return err;
err = mmap_hook_validate(orig_start, orig_end, &orig_flags, vma);
if (unlikely(err)) {
vma->vm_start = orig_start;
vma->vm_end = orig_end;
}
return err;
}
void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap);
#ifdef CONFIG_MMU
bool cond_install_uffd_wp_ptes(struct vm_area_struct *vma,
unsigned long addr, pte_t *ptep, pte_t pte,
unsigned long nr_ptes);
static inline void get_anon_vma(struct anon_vma *anon_vma)
{
atomic_inc(&anon_vma->refcount);
}
void __put_anon_vma(struct anon_vma *anon_vma);
static inline void put_anon_vma(struct anon_vma *anon_vma)
{
if (atomic_dec_and_test(&anon_vma->refcount))
__put_anon_vma(anon_vma);
}
static inline void anon_vma_lock_write(struct anon_vma *anon_vma)
{
down_write(&anon_vma->root->rwsem);
}
static inline void anon_vma_unlock_write(struct anon_vma *anon_vma)
{
up_write(&anon_vma->root->rwsem);
}
static inline void anon_vma_lock_read(struct anon_vma *anon_vma)
{
down_read(&anon_vma->root->rwsem);
}
static inline int anon_vma_trylock_read(struct anon_vma *anon_vma)
{
return down_read_trylock(&anon_vma->root->rwsem);
}
static inline void anon_vma_unlock_read(struct anon_vma *anon_vma)
{
up_read(&anon_vma->root->rwsem);
}
struct anon_vma *folio_get_anon_vma(const struct folio *folio);
enum vma_operation {
VMA_OP_SPLIT,
VMA_OP_MERGE_UNFAULTED,
VMA_OP_REMAP,
VMA_OP_FORK,
};
int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src,
enum vma_operation operation);
int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma);
int __anon_vma_prepare(struct vm_area_struct *vma);
void unlink_anon_vmas(struct vm_area_struct *vma);
static inline int anon_vma_prepare(struct vm_area_struct *vma)
{
if (likely(vma_has_anon_rmap(vma)))
return 0;
return __anon_vma_prepare(vma);
}
typedef int __bitwise fpb_t;
#define FPB_RESPECT_DIRTY ((__force fpb_t)BIT(0))
#define FPB_RESPECT_SOFT_DIRTY ((__force fpb_t)BIT(1))
#define FPB_RESPECT_WRITE ((__force fpb_t)BIT(2))
#define FPB_MERGE_WRITE ((__force fpb_t)BIT(3))
#define FPB_MERGE_YOUNG_DIRTY ((__force fpb_t)BIT(4))
static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags)
{
if (!(flags & FPB_RESPECT_DIRTY))
pte = pte_mkclean(pte);
if (likely(!(flags & FPB_RESPECT_SOFT_DIRTY)))
pte = pte_clear_soft_dirty(pte);
if (likely(!(flags & FPB_RESPECT_WRITE)))
pte = pte_wrprotect(pte);
return pte_mkold(pte);
}
static inline unsigned int folio_pte_batch_flags(struct folio *folio,
struct vm_area_struct *vma, pte_t *ptep, pte_t *ptentp,
unsigned int max_nr, fpb_t flags)
{
bool any_writable = false, any_young = false, any_dirty = false;
pte_t expected_pte, pte = *ptentp;
unsigned int nr, cur_nr;
VM_WARN_ON_FOLIO(!pte_present(pte), folio);
VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio);
VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) != folio, folio);
VM_WARN_ON(virt_addr_valid(ptentp) && PageTable(virt_to_page(ptentp)));
max_nr = min_t(unsigned long, max_nr,
folio_pfn(folio) + folio_nr_pages(folio) - pte_pfn(pte));
nr = pte_batch_hint(ptep, pte);
expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags);
ptep = ptep + nr;
while (nr < max_nr) {
pte = ptep_get(ptep);
if (!pte_same(__pte_batch_clear_ignored(pte, flags), expected_pte))
break;
if (flags & FPB_MERGE_WRITE)
any_writable |= pte_write(pte);
if (flags & FPB_MERGE_YOUNG_DIRTY) {
any_young |= pte_young(pte);
any_dirty |= pte_dirty(pte);
}
cur_nr = pte_batch_hint(ptep, pte);
expected_pte = pte_advance_pfn(expected_pte, cur_nr);
ptep += cur_nr;
nr += cur_nr;
}
if (any_writable)
*ptentp = pte_mkwrite(*ptentp, vma);
if (any_young)
*ptentp = pte_mkyoung(*ptentp);
if (any_dirty)
*ptentp = pte_mkdirty(*ptentp);
return min(nr, max_nr);
}
unsigned int folio_pte_batch(struct folio *folio, pte_t *ptep, pte_t pte,
unsigned int max_nr);
static inline pte_t pte_move_swp_offset(pte_t pte, long delta)
{
const softleaf_t entry = softleaf_from_pte(pte);
pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry),
(swp_offset(entry) + delta)));
if (pte_swp_soft_dirty(pte))
new = pte_swp_mksoft_dirty(new);
if (pte_swp_exclusive(pte))
new = pte_swp_mkexclusive(new);
if (pte_swp_uffd(pte))
new = pte_swp_mkuffd(new);
return new;
}
static inline pte_t pte_next_swp_offset(pte_t pte)
{
return pte_move_swp_offset(pte, 1);
}
static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte)
{
pte_t expected_pte = pte_next_swp_offset(pte);
const pte_t *end_ptep = start_ptep + max_nr;
pte_t *ptep = start_ptep + 1;
VM_WARN_ON(max_nr < 1);
VM_WARN_ON(!softleaf_is_swap(softleaf_from_pte(pte)));
while (ptep < end_ptep) {
pte = ptep_get(ptep);
if (!pte_same(pte, expected_pte))
break;
expected_pte = pte_next_swp_offset(expected_pte);
ptep++;
}
return ptep - start_ptep;
}
#endif /* CONFIG_MMU */
void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio,
int nr_throttled);
static inline void acct_reclaim_writeback(struct folio *folio)
{
pg_data_t *pgdat = folio_pgdat(folio);
int nr_throttled = atomic_read(&pgdat->nr_writeback_throttled);
if (nr_throttled)
__acct_reclaim_writeback(pgdat, folio, nr_throttled);
}
static inline void wake_throttle_isolated(pg_data_t *pgdat)
{
wait_queue_head_t *wqh;
wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_ISOLATED];
if (waitqueue_active(wqh))
wake_up(wqh);
}
vm_fault_t __vmf_anon_prepare(struct vm_fault *vmf);
static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf)
{
vm_fault_t ret = __vmf_anon_prepare(vmf);
if (unlikely(ret & VM_FAULT_RETRY))
vma_end_read(vmf->vma);
return ret;
}
vm_fault_t do_swap_page(struct vm_fault *vmf);
void folio_rotate_reclaimable(struct folio *folio);
bool __folio_end_writeback(struct folio *folio);
void deactivate_file_folio(struct folio *folio);
void folio_activate(struct folio *folio);
void free_pgtables(struct mmu_gather *tlb, struct unmap_desc *desc);
void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte);
static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp)
{
spinlock_t *ptl = pmd_lock(mm, pmdp);
spin_unlock(ptl);
}
struct zap_details;
void zap_vma_range_batched(struct mmu_gather *tlb,
struct vm_area_struct *vma, unsigned long addr,
unsigned long size, struct zap_details *details);
int zap_vma_for_reaping(struct vm_area_struct *vma);
int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio,
gfp_t gfp);
void page_cache_ra_order(struct readahead_control *, struct file_ra_state *);
void force_page_cache_ra(struct readahead_control *, unsigned long nr);
static inline void force_page_cache_readahead(struct address_space *mapping,
struct file *file, pgoff_t index, unsigned long nr_to_read)
{
DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index);
force_page_cache_ra(&ractl, nr_to_read);
}
static inline void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2)
__acquires(ptl1)
__acquires(ptl2)
{
if (ptl1 > ptl2)
swap(ptl1, ptl2);
spin_lock(ptl1);
if (ptl1 != ptl2)
spin_lock_nested(ptl2, SINGLE_DEPTH_NESTING);
else
__acquire(ptl2);
}
static inline void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2)
__releases(ptl1)
__releases(ptl2)
{
spin_unlock(ptl1);
if (ptl1 != ptl2)
spin_unlock(ptl2);
else
__release(ptl2);
}
unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start,
pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);
unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);
int truncate_inode_folio(struct address_space *mapping, struct folio *folio);
bool truncate_inode_partial_folio(struct folio *folio, loff_t lstart,
loff_t lend, pgoff_t *pstart, pgoff_t *pend);
long mapping_evict_folio(struct address_space *mapping, struct folio *folio);
unsigned long mapping_try_invalidate(struct address_space *mapping,
pgoff_t start, pgoff_t end, unsigned long *nr_failed);
static inline bool folio_evictable(struct folio *folio)
{
bool ret;
rcu_read_lock();
ret = !mapping_unevictable(folio_mapping(folio)) &&
!folio_test_mlocked(folio);
rcu_read_unlock();
return ret;
}
static inline void set_page_refcounted(struct page *page)
{
VM_BUG_ON_PAGE(PageTail(page), page);
VM_BUG_ON_PAGE(page_ref_count(page), page);
set_page_count(page, 1);
}
static inline void set_pages_refcounted(struct page *page, unsigned long nr_pages)
{
unsigned long pfn = page_to_pfn(page);
for (; nr_pages--; pfn++)
set_page_refcounted(pfn_to_page(pfn));
}
static inline bool folio_needs_release(struct folio *folio)
{
struct address_space *mapping = folio_mapping(folio);
return folio_has_private(folio) ||
(mapping && mapping_release_always(mapping));
}
extern unsigned long highest_memmap_pfn;
#define MAX_RECLAIM_RETRIES 16
bool folio_isolate_lru(struct folio *folio);
void folio_putback_lru(struct folio *folio);
extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason);
int user_proactive_reclaim(char *buf,
struct mem_cgroup *memcg, pg_data_t *pgdat);
pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address);
void set_recommended_min_free_kbytes(void);
#define K(x) ((x) << (PAGE_SHIFT-10))
extern char * const zone_names[MAX_NR_ZONES];
extern int min_free_kbytes;
extern int defrag_mode;
void setup_per_zone_wmarks(void);
void calculate_min_free_kbytes(void);
int __meminit init_per_zone_wmark_min(void);
extern int __isolate_free_page(struct page *page, unsigned int order);
extern void __putback_isolated_page(struct page *page, unsigned int order,
int mt);
static inline void folio_set_order(struct folio *folio, unsigned int order)
{
if (WARN_ON_ONCE(!order || !folio_test_large(folio)))
return;
VM_WARN_ON_ONCE(order > MAX_FOLIO_ORDER);
folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order;
#ifdef NR_PAGES_IN_LARGE_FOLIO
folio->_nr_pages = 1U << order;
#endif
}
bool __folio_unqueue_deferred_split(struct folio *folio);
static inline bool folio_unqueue_deferred_split(struct folio *folio)
{
if (folio_order(folio) <= 1 || !folio_test_large_rmappable(folio))
return false;
if (data_race(list_empty(&folio->_deferred_list)))
return false;
return __folio_unqueue_deferred_split(folio);
}
static inline struct folio *page_rmappable_folio(struct page *page)
{
struct folio *folio = (struct folio *)page;
if (folio && folio_test_large(folio))
folio_set_large_rmappable(folio);
return folio;
}
static inline void prep_compound_head(struct page *page, unsigned int order)
{
struct folio *folio = (struct folio *)page;
folio_set_order(folio, order);
atomic_set(&folio->_large_mapcount, -1);
if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT))
atomic_set(&folio->_nr_pages_mapped, 0);
if (IS_ENABLED(CONFIG_MM_ID)) {
folio->_mm_ids = 0;
folio->_mm_id_mapcount[0] = -1;
folio->_mm_id_mapcount[1] = -1;
}
if (IS_ENABLED(CONFIG_64BIT) || order > 1) {
atomic_set(&folio->_pincount, 0);
atomic_set(&folio->_entire_mapcount, -1);
}
if (order > 1)
INIT_LIST_HEAD(&folio->_deferred_list);
}
static inline void prep_compound_tail(struct page *tail,
const struct page *head, unsigned int order)
{
tail->mapping = TAIL_MAPPING;
set_compound_head(tail, head, order);
VM_WARN_ON_ONCE(tail->private);
}
#if defined CONFIG_COMPACTION || defined CONFIG_CMA
struct compact_control {
struct list_head freepages[NR_PAGE_ORDERS];
struct list_head migratepages;
unsigned int nr_freepages;
unsigned int nr_migratepages;
unsigned long free_pfn;
unsigned long migrate_pfn;
unsigned long fast_start_pfn;
struct zone *zone;
unsigned long total_migrate_scanned;
unsigned long total_free_scanned;
unsigned short fast_search_fail;
short search_order;
const gfp_t gfp_mask;
int order;
int migratetype;
const unsigned int alloc_flags;
const int highest_zoneidx;
enum migrate_mode mode;
bool ignore_skip_hint;
bool no_set_skip_hint;
bool ignore_block_suitable;
bool direct_compaction;
bool proactive_compaction;
bool whole_zone;
bool contended;
bool finish_pageblock;
bool alloc_contig;
};
struct capture_control {
struct zone *zone;
int migratetype;
int order;
struct page *page;
};
unsigned long
isolate_freepages_range(struct compact_control *cc,
unsigned long start_pfn, unsigned long end_pfn);
int
isolate_migratepages_range(struct compact_control *cc,
unsigned long low_pfn, unsigned long end_pfn);
#endif /* CONFIG_COMPACTION || CONFIG_CMA */
struct cma;
#ifdef CONFIG_CMA
bool cma_validate_zones(struct cma *cma);
void *cma_reserve_early(struct cma *cma, unsigned long size);
#else
static inline bool cma_validate_zones(struct cma *cma)
{
return false;
}
static inline void *cma_reserve_early(struct cma *cma, unsigned long size)
{
return NULL;
}
#endif
struct anon_vma *folio_anon_vma(const struct folio *folio);
#ifdef CONFIG_MMU
void unmap_mapping_folio(struct folio *folio);
extern long populate_vma_page_range(struct vm_area_struct *vma,
unsigned long start, unsigned long end, int *locked);
extern long faultin_page_range(struct mm_struct *mm, unsigned long start,
unsigned long end, bool write, int *locked);
bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked,
unsigned long bytes);
static inline bool
folio_within_range(struct folio *folio, struct vm_area_struct *vma,
unsigned long start, unsigned long end)
{
const unsigned long vma_pglen = vma_pages(vma);
pgoff_t pgoff_folio, pgoff_vma_start;
unsigned long addr;
VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio);
if (start > end)
return false;
pgoff_folio = folio_pgoff(folio);
pgoff_vma_start = folio_test_anon(folio) ?
vma_start_anon_pgoff(vma) : vma_start_pgoff(vma);
if (start < vma->vm_start)
start = vma->vm_start;
if (end > vma->vm_end)
end = vma->vm_end;
if (!in_range(pgoff_folio, pgoff_vma_start, vma_pglen))
return false;
addr = vma->vm_start + ((pgoff_folio - pgoff_vma_start) << PAGE_SHIFT);
return !(addr < start || end - addr < folio_size(folio));
}
static inline bool
folio_within_vma(struct folio *folio, struct vm_area_struct *vma)
{
return folio_within_range(folio, vma, vma->vm_start, vma->vm_end);
}
void mlock_folio(struct folio *folio);
static inline void mlock_vma_folio(struct folio *folio,
struct vm_area_struct *vma)
{
if (vma_test(vma, VMA_LOCKED_BIT))
mlock_folio(folio);
}
void munlock_folio(struct folio *folio);
static inline void munlock_vma_folio(struct folio *folio,
struct vm_area_struct *vma)
{
if (unlikely(vma_test_any_mask(vma, VMA_LOCKED_MASK)))
munlock_folio(folio);
}
void mlock_new_folio(struct folio *folio);
bool need_mlock_drain(int cpu);
void mlock_drain_local(void);
void mlock_drain_remote(int cpu);
extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma);
static inline unsigned long __vma_address(const struct vm_area_struct *vma,
pgoff_t pgoff, pgoff_t pgoff_start, unsigned long nr_pages)
{
unsigned long address;
if (pgoff >= pgoff_start) {
address = vma->vm_start +
((pgoff - pgoff_start) << PAGE_SHIFT);
if (address < vma->vm_start || address >= vma->vm_end)
address = -EFAULT;
} else if (pgoff + nr_pages - 1 >= pgoff_start) {
address = vma->vm_start;
} else {
address = -EFAULT;
}
return address;
}
static inline unsigned long vma_filebacked_address(const struct vm_area_struct *vma,
pgoff_t pgoff, unsigned long nr_pages)
{
VM_WARN_ON_ONCE(vma_is_anonymous(vma));
return __vma_address(vma, pgoff, vma_start_pgoff(vma), nr_pages);
}
static inline unsigned long vma_anon_address(const struct vm_area_struct *vma,
pgoff_t pgoff_anon, unsigned long nr_pages)
{
VM_WARN_ON_ONCE(!vma_is_cow_mapping(vma));
return __vma_address(vma, pgoff_anon, vma_start_anon_pgoff(vma), nr_pages);
}
static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw)
{
const pgoff_t pgoff_end = pvmw->pgoff + pvmw->nr_pages;
const struct vm_area_struct *vma = pvmw->vma;
pgoff_t pgoff_vma_start;
unsigned long address;
if (pvmw->nr_pages == 1)
return pvmw->address + PAGE_SIZE;
if (pvmw->pgoff_is_anon)
pgoff_vma_start = vma_start_anon_pgoff(vma);
else
pgoff_vma_start = vma_start_pgoff(vma);
address = vma->vm_start +
((pgoff_end - pgoff_vma_start) << PAGE_SHIFT);
if (address < vma->vm_start || address > vma->vm_end)
address = vma->vm_end;
return address;
}
static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
struct file *fpin)
{
int flags = vmf->flags;
if (fpin)
return fpin;
if (fault_flag_allow_retry_first(flags) &&
!(flags & FAULT_FLAG_RETRY_NOWAIT)) {
fpin = get_file(vmf->vma->vm_file);
release_fault_lock(vmf);
}
return fpin;
}
static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
{
if (!vma_is_mm_backed(vma))
return false;
if (vma_test_single_mask(vma, VMA_DROPPABLE))
return false;
if (vma_is_dax(vma) || vma_is_hugetlb(vma))
return false;
return vma != get_gate_vma(current->mm);
}
#else /* !CONFIG_MMU */
static inline void unmap_mapping_folio(struct folio *folio) { }
static inline void mlock_new_folio(struct folio *folio) { }
static inline bool need_mlock_drain(int cpu) { return false; }
static inline void mlock_drain_local(void) { }
static inline void mlock_drain_remote(int cpu) { }
#endif /* !CONFIG_MMU */
#ifdef CONFIG_NUMA
extern int node_reclaim_mode;
extern unsigned long node_reclaim(struct pglist_data *pgdat,
gfp_t gfp_mask, unsigned int order);
int find_next_best_node_in(int node, nodemask_t *used_node_mask,
const nodemask_t *candidates);
#else
#define node_reclaim_mode 0
static inline unsigned long node_reclaim(struct pglist_data *pgdat,
gfp_t mask, unsigned int order)
{
return 0;
}
static inline int find_next_best_node_in(int node, nodemask_t *used_node_mask,
const nodemask_t *candidates)
{
return NUMA_NO_NODE;
}
#endif
static inline bool node_reclaim_enabled(void)
{
return node_reclaim_mode & (RECLAIM_ZONE|RECLAIM_WRITE|RECLAIM_UNMAP);
}
#ifdef CONFIG_MEMORY_FAILURE
int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill);
void shake_folio(struct folio *folio);
typedef int hwpoison_filter_func_t(struct page *p);
void hwpoison_filter_register(hwpoison_filter_func_t *filter);
void hwpoison_filter_unregister(void);
#define MAGIC_HWPOISON 0x48575053U /* HWPS */
void SetPageHWPoisonTakenOff(struct page *page);
void ClearPageHWPoisonTakenOff(struct page *page);
bool take_page_off_buddy(struct page *page);
bool put_page_back_buddy(struct page *page);
struct task_struct *task_early_kill(struct task_struct *tsk, int force_early);
void add_to_kill_ksm(struct task_struct *tsk, const struct page *p,
struct vm_area_struct *vma, struct list_head *to_kill,
unsigned long ksm_addr);
unsigned long page_mapped_in_vma(const struct page *page,
struct vm_area_struct *vma);
#else
static inline int unmap_poisoned_folio(struct folio *folio, unsigned long pfn, bool must_kill)
{
return -EBUSY;
}
#endif
extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long,
unsigned long, unsigned long,
unsigned long, unsigned long);
unsigned long reclaim_pages(struct list_head *folio_list);
unsigned int reclaim_clean_pages_from_list(struct zone *zone,
struct list_head *folio_list);
enum ttu_flags;
struct tlbflush_unmap_batch;
extern struct workqueue_struct *mm_percpu_wq;
#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
void try_to_unmap_flush(void);
void try_to_unmap_flush_dirty(void);
void flush_tlb_batched_pending(struct mm_struct *mm);
#else
static inline void try_to_unmap_flush(void)
{
}
static inline void try_to_unmap_flush_dirty(void)
{
}
static inline void flush_tlb_batched_pending(struct mm_struct *mm)
{
}
#endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
extern const struct trace_print_flags pageflag_names[];
extern const struct trace_print_flags vmaflag_names[];
extern const struct trace_print_flags gfpflag_names[];
void setup_zone_pageset(struct zone *zone);
struct migration_target_control {
int nid;
nodemask_t *nmask;
gfp_t gfp_mask;
enum migrate_reason reason;
};
size_t splice_folio_into_pipe(struct pipe_inode_info *pipe,
struct folio *folio, loff_t fpos, size_t size);
static inline bool vma_is_single_threaded_private(struct vm_area_struct *vma)
{
if (vma->vm_flags & VM_SHARED)
return false;
return atomic_read(&vma->vm_mm->mm_users) == 1;
}
#ifdef CONFIG_NUMA_BALANCING
bool folio_can_map_prot_numa(struct folio *folio, struct vm_area_struct *vma,
bool is_private_single_threaded);
#else
static inline bool folio_can_map_prot_numa(struct folio *folio,
struct vm_area_struct *vma, bool is_private_single_threaded)
{
return false;
}
#endif
int numa_migrate_check(struct folio *folio, struct vm_fault *vmf,
unsigned long addr, int *flags, bool writable,
int *last_cpupid);
void free_zone_device_folio(struct folio *folio);
int migrate_device_coherent_folio(struct folio *folio);
int __must_check try_grab_folio(struct folio *folio, int refs,
unsigned int flags);
void touch_pud(struct vm_area_struct *vma, unsigned long addr,
pud_t *pud, bool write);
bool touch_pmd(struct vm_area_struct *vma, unsigned long addr,
pmd_t *pmd, bool write);
static inline int get_order_from_str(const char *size_str,
unsigned long valid_orders)
{
unsigned long size;
char *endptr;
int order;
size = memparse(size_str, &endptr);
if (!is_power_of_2(size))
return -EINVAL;
order = get_order(size);
if (BIT(order) & ~valid_orders)
return -EINVAL;
return order;
}
enum {
FOLL_TOUCH = 1 << 16,
FOLL_TRIED = 1 << 17,
FOLL_REMOTE = 1 << 18,
FOLL_PIN = 1 << 19,
FOLL_FAST_ONLY = 1 << 20,
FOLL_UNLOCKABLE = 1 << 21,
FOLL_MADV_POPULATE = 1 << 22,
};
#define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \
FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \
FOLL_MADV_POPULATE)
static inline bool gup_must_unshare(struct vm_area_struct *vma,
unsigned int flags, struct page *page)
{
if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
return false;
if (!PageAnon(page)) {
if (!(flags & FOLL_LONGTERM))
return false;
if (!vma)
return true;
return vma_is_cow_mapping(vma);
}
if (IS_ENABLED(CONFIG_HAVE_GUP_FAST))
smp_rmb();
return !PageAnonExclusive(page);
}
static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma)
{
if (!pgtable_supports_soft_dirty())
return false;
return !(vma->vm_flags & VM_SOFTDIRTY);
}
static inline bool pmd_needs_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd)
{
return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd);
}
static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte)
{
return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte);
}
unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg,
int priority);
int shmem_add_to_page_cache(struct folio *folio,
struct address_space *mapping,
pgoff_t index, void *expected, gfp_t gfp);
int shmem_inode_acct_blocks(struct inode *inode, long pages);
bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped);
#ifdef CONFIG_SHRINKER_DEBUG
static inline __printf(2, 0) int shrinker_debugfs_name_alloc(
struct shrinker *shrinker, const char *fmt, va_list ap)
{
shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
return shrinker->name ? 0 : -ENOMEM;
}
static inline void shrinker_debugfs_name_free(struct shrinker *shrinker)
{
kfree_const(shrinker->name);
shrinker->name = NULL;
}
extern int shrinker_debugfs_add(struct shrinker *shrinker);
extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
int *debugfs_id);
extern void shrinker_debugfs_remove(struct dentry *debugfs_entry,
int debugfs_id);
#else /* CONFIG_SHRINKER_DEBUG */
static inline int shrinker_debugfs_add(struct shrinker *shrinker)
{
return 0;
}
static inline int shrinker_debugfs_name_alloc(struct shrinker *shrinker,
const char *fmt, va_list ap)
{
return 0;
}
static inline void shrinker_debugfs_name_free(struct shrinker *shrinker)
{
}
static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
int *debugfs_id)
{
*debugfs_id = -1;
return NULL;
}
static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry,
int debugfs_id)
{
}
#endif /* CONFIG_SHRINKER_DEBUG */
void workingset_update_node(struct xa_node *node);
extern struct list_lru shadow_nodes;
#define mapping_set_update(xas, mapping) do { \
if (!dax_mapping(mapping) && !shmem_mapping(mapping)) { \
xas_set_update(xas, workingset_update_node); \
xas_set_lru(xas, &shadow_nodes); \
} \
} while (0)
unsigned long move_page_tables(struct pagetable_move_control *pmc);
#ifdef CONFIG_UNACCEPTED_MEMORY
void accept_page(struct page *page);
#else /* CONFIG_UNACCEPTED_MEMORY */
static inline void accept_page(struct page *page)
{
}
#endif /* CONFIG_UNACCEPTED_MEMORY */
int walk_page_range_mm_unsafe(struct mm_struct *mm, unsigned long start,
unsigned long end, const struct mm_walk_ops *ops,
void *private);
int walk_page_range_vma_unsafe(struct vm_area_struct *vma, unsigned long start,
unsigned long end, const struct mm_walk_ops *ops,
void *private);
int walk_page_range_debug(struct mm_struct *mm, unsigned long start,
unsigned long end, const struct mm_walk_ops *ops,
pgd_t *pgd, void *private);
void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm);
int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm);
int remap_pfn_range_prepare(struct vm_area_desc *desc);
int remap_pfn_range_complete(struct vm_area_struct *vma,
struct mmap_action *action);
int simple_ioremap_prepare(struct vm_area_desc *desc);
int map_kernel_pages_prepare(struct vm_area_desc *desc);
int map_kernel_pages_complete(struct vm_area_struct *vma,
struct mmap_action *action);
int map_discontig_kernel_pages_prepare(struct vm_area_desc *desc);
int map_discontig_kernel_pages_complete(struct vm_area_struct *vma,
struct mmap_action *action);
static inline int io_remap_pfn_range_prepare(struct vm_area_desc *desc)
{
struct mmap_action *action = &desc->action;
const unsigned long orig_pfn = action->remap.start_pfn;
const pgprot_t orig_pgprot = action->remap.pgprot;
const unsigned long size = action->remap.size;
const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size);
int err;
action->remap.start_pfn = pfn;
action->remap.pgprot = pgprot_decrypted(orig_pgprot);
err = remap_pfn_range_prepare(desc);
if (err)
return err;
action->type = MMAP_REMAP_PFN;
return 0;
}
static inline void maybe_rmap_unlock_action(struct vm_area_struct *vma,
struct mmap_action *action)
{
struct file *file;
if (!action->hide_from_rmap_until_complete)
return;
VM_WARN_ON_ONCE(vma_is_anonymous(vma));
file = vma->vm_file;
i_mmap_unlock_write(file->f_mapping);
action->hide_from_rmap_until_complete = false;
}
#ifdef CONFIG_MMU_NOTIFIER
static inline bool clear_flush_young_ptes_notify(struct vm_area_struct *vma,
unsigned long addr, pte_t *ptep, unsigned int nr)
{
bool young;
young = clear_flush_young_ptes(vma, addr, ptep, nr);
young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr,
addr + nr * PAGE_SIZE);
return young;
}
static inline bool pmdp_clear_flush_young_notify(struct vm_area_struct *vma,
unsigned long addr, pmd_t *pmdp)
{
bool young;
young = pmdp_clear_flush_young(vma, addr, pmdp);
young |= mmu_notifier_clear_flush_young(vma->vm_mm, addr, addr + PMD_SIZE);
return young;
}
static inline bool test_and_clear_young_ptes_notify(struct vm_area_struct *vma,
unsigned long addr, pte_t *ptep, unsigned int nr)
{
bool young;
young = test_and_clear_young_ptes(vma, addr, ptep, nr);
young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + nr * PAGE_SIZE);
return young;
}
static inline bool pmdp_test_and_clear_young_notify(struct vm_area_struct *vma,
unsigned long addr, pmd_t *pmdp)
{
bool young;
young = pmdp_test_and_clear_young(vma, addr, pmdp);
young |= mmu_notifier_clear_young(vma->vm_mm, addr, addr + PMD_SIZE);
return young;
}
#else /* CONFIG_MMU_NOTIFIER */
#define clear_flush_young_ptes_notify clear_flush_young_ptes
#define pmdp_clear_flush_young_notify pmdp_clear_flush_young
#define test_and_clear_young_ptes_notify test_and_clear_young_ptes
#define pmdp_test_and_clear_young_notify pmdp_test_and_clear_young
#endif /* CONFIG_MMU_NOTIFIER */
extern int sysctl_max_map_count;
static inline int get_sysctl_max_map_count(void)
{
return READ_ONCE(sysctl_max_map_count);
}
bool may_expand_vm(struct mm_struct *mm, const vma_flags_t *vma_flags,
unsigned long npages);
static inline void mm_prepare_for_swap_entries(struct mm_struct *mm)
{
if (list_empty(&mm->mmlist)) {
spin_lock(&mmlist_lock);
if (list_empty(&mm->mmlist))
list_add(&mm->mmlist, &init_mm.mmlist);
spin_unlock(&mmlist_lock);
}
}
static inline bool can_spin_trylock(void)
{
if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq()))
return false;
if (!IS_ENABLED(CONFIG_SMP) && in_nmi())
return false;
return true;
}
bool file_is_dev_zero(const struct file *file);
#endif /* __MM_INTERNAL_H */