Back to home page

LXR

 
 

    


0001 /* internal.h: mm/ internal definitions
0002  *
0003  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
0004  * Written by David Howells (dhowells@redhat.com)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either version
0009  * 2 of the License, or (at your option) any later version.
0010  */
0011 #ifndef __MM_INTERNAL_H
0012 #define __MM_INTERNAL_H
0013 
0014 #include <linux/fs.h>
0015 #include <linux/mm.h>
0016 #include <linux/pagemap.h>
0017 #include <linux/tracepoint-defs.h>
0018 
0019 /*
0020  * The set of flags that only affect watermark checking and reclaim
0021  * behaviour. This is used by the MM to obey the caller constraints
0022  * about IO, FS and watermark checking while ignoring placement
0023  * hints such as HIGHMEM usage.
0024  */
0025 #define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\
0026             __GFP_NOWARN|__GFP_REPEAT|__GFP_NOFAIL|\
0027             __GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\
0028             __GFP_ATOMIC)
0029 
0030 /* The GFP flags allowed during early boot */
0031 #define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS))
0032 
0033 /* Control allocation cpuset and node placement constraints */
0034 #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE)
0035 
0036 /* Do not use these with a slab allocator */
0037 #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
0038 
0039 void page_writeback_init(void);
0040 
0041 int do_swap_page(struct vm_fault *vmf);
0042 
0043 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
0044         unsigned long floor, unsigned long ceiling);
0045 
0046 void unmap_page_range(struct mmu_gather *tlb,
0047                  struct vm_area_struct *vma,
0048                  unsigned long addr, unsigned long end,
0049                  struct zap_details *details);
0050 
0051 extern int __do_page_cache_readahead(struct address_space *mapping,
0052         struct file *filp, pgoff_t offset, unsigned long nr_to_read,
0053         unsigned long lookahead_size);
0054 
0055 /*
0056  * Submit IO for the read-ahead request in file_ra_state.
0057  */
0058 static inline unsigned long ra_submit(struct file_ra_state *ra,
0059         struct address_space *mapping, struct file *filp)
0060 {
0061     return __do_page_cache_readahead(mapping, filp,
0062                     ra->start, ra->size, ra->async_size);
0063 }
0064 
0065 /*
0066  * Turn a non-refcounted page (->_refcount == 0) into refcounted with
0067  * a count of one.
0068  */
0069 static inline void set_page_refcounted(struct page *page)
0070 {
0071     VM_BUG_ON_PAGE(PageTail(page), page);
0072     VM_BUG_ON_PAGE(page_ref_count(page), page);
0073     set_page_count(page, 1);
0074 }
0075 
0076 extern unsigned long highest_memmap_pfn;
0077 
0078 /*
0079  * in mm/vmscan.c:
0080  */
0081 extern int isolate_lru_page(struct page *page);
0082 extern void putback_lru_page(struct page *page);
0083 extern bool pgdat_reclaimable(struct pglist_data *pgdat);
0084 
0085 /*
0086  * in mm/rmap.c:
0087  */
0088 extern pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address);
0089 
0090 /*
0091  * in mm/page_alloc.c
0092  */
0093 
0094 /*
0095  * Structure for holding the mostly immutable allocation parameters passed
0096  * between functions involved in allocations, including the alloc_pages*
0097  * family of functions.
0098  *
0099  * nodemask, migratetype and high_zoneidx are initialized only once in
0100  * __alloc_pages_nodemask() and then never change.
0101  *
0102  * zonelist, preferred_zone and classzone_idx are set first in
0103  * __alloc_pages_nodemask() for the fast path, and might be later changed
0104  * in __alloc_pages_slowpath(). All other functions pass the whole strucure
0105  * by a const pointer.
0106  */
0107 struct alloc_context {
0108     struct zonelist *zonelist;
0109     nodemask_t *nodemask;
0110     struct zoneref *preferred_zoneref;
0111     int migratetype;
0112     enum zone_type high_zoneidx;
0113     bool spread_dirty_pages;
0114 };
0115 
0116 #define ac_classzone_idx(ac) zonelist_zone_idx(ac->preferred_zoneref)
0117 
0118 /*
0119  * Locate the struct page for both the matching buddy in our
0120  * pair (buddy1) and the combined O(n+1) page they form (page).
0121  *
0122  * 1) Any buddy B1 will have an order O twin B2 which satisfies
0123  * the following equation:
0124  *     B2 = B1 ^ (1 << O)
0125  * For example, if the starting buddy (buddy2) is #8 its order
0126  * 1 buddy is #10:
0127  *     B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
0128  *
0129  * 2) Any buddy B will have an order O+1 parent P which
0130  * satisfies the following equation:
0131  *     P = B & ~(1 << O)
0132  *
0133  * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
0134  */
0135 static inline unsigned long
0136 __find_buddy_index(unsigned long page_idx, unsigned int order)
0137 {
0138     return page_idx ^ (1 << order);
0139 }
0140 
0141 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
0142                 unsigned long end_pfn, struct zone *zone);
0143 
0144 static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
0145                 unsigned long end_pfn, struct zone *zone)
0146 {
0147     if (zone->contiguous)
0148         return pfn_to_page(start_pfn);
0149 
0150     return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
0151 }
0152 
0153 extern int __isolate_free_page(struct page *page, unsigned int order);
0154 extern void __free_pages_bootmem(struct page *page, unsigned long pfn,
0155                     unsigned int order);
0156 extern void prep_compound_page(struct page *page, unsigned int order);
0157 extern void post_alloc_hook(struct page *page, unsigned int order,
0158                     gfp_t gfp_flags);
0159 extern int user_min_free_kbytes;
0160 
0161 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
0162 
0163 /*
0164  * in mm/compaction.c
0165  */
0166 /*
0167  * compact_control is used to track pages being migrated and the free pages
0168  * they are being migrated to during memory compaction. The free_pfn starts
0169  * at the end of a zone and migrate_pfn begins at the start. Movable pages
0170  * are moved to the end of a zone during a compaction run and the run
0171  * completes when free_pfn <= migrate_pfn
0172  */
0173 struct compact_control {
0174     struct list_head freepages; /* List of free pages to migrate to */
0175     struct list_head migratepages;  /* List of pages being migrated */
0176     unsigned long nr_freepages; /* Number of isolated free pages */
0177     unsigned long nr_migratepages;  /* Number of pages to migrate */
0178     unsigned long free_pfn;     /* isolate_freepages search base */
0179     unsigned long migrate_pfn;  /* isolate_migratepages search base */
0180     unsigned long last_migrated_pfn;/* Not yet flushed page being freed */
0181     enum migrate_mode mode;     /* Async or sync migration mode */
0182     bool ignore_skip_hint;      /* Scan blocks even if marked skip */
0183     bool ignore_block_suitable; /* Scan blocks considered unsuitable */
0184     bool direct_compaction;     /* False from kcompactd or /proc/... */
0185     bool whole_zone;        /* Whole zone should/has been scanned */
0186     int order;          /* order a direct compactor needs */
0187     const gfp_t gfp_mask;       /* gfp mask of a direct compactor */
0188     const unsigned int alloc_flags; /* alloc flags of a direct compactor */
0189     const int classzone_idx;    /* zone index of a direct compactor */
0190     struct zone *zone;
0191     bool contended;         /* Signal lock or sched contention */
0192 };
0193 
0194 unsigned long
0195 isolate_freepages_range(struct compact_control *cc,
0196             unsigned long start_pfn, unsigned long end_pfn);
0197 unsigned long
0198 isolate_migratepages_range(struct compact_control *cc,
0199                unsigned long low_pfn, unsigned long end_pfn);
0200 int find_suitable_fallback(struct free_area *area, unsigned int order,
0201             int migratetype, bool only_stealable, bool *can_steal);
0202 
0203 #endif
0204 
0205 /*
0206  * This function returns the order of a free page in the buddy system. In
0207  * general, page_zone(page)->lock must be held by the caller to prevent the
0208  * page from being allocated in parallel and returning garbage as the order.
0209  * If a caller does not hold page_zone(page)->lock, it must guarantee that the
0210  * page cannot be allocated or merged in parallel. Alternatively, it must
0211  * handle invalid values gracefully, and use page_order_unsafe() below.
0212  */
0213 static inline unsigned int page_order(struct page *page)
0214 {
0215     /* PageBuddy() must be checked by the caller */
0216     return page_private(page);
0217 }
0218 
0219 /*
0220  * Like page_order(), but for callers who cannot afford to hold the zone lock.
0221  * PageBuddy() should be checked first by the caller to minimize race window,
0222  * and invalid values must be handled gracefully.
0223  *
0224  * READ_ONCE is used so that if the caller assigns the result into a local
0225  * variable and e.g. tests it for valid range before using, the compiler cannot
0226  * decide to remove the variable and inline the page_private(page) multiple
0227  * times, potentially observing different values in the tests and the actual
0228  * use of the result.
0229  */
0230 #define page_order_unsafe(page)     READ_ONCE(page_private(page))
0231 
0232 static inline bool is_cow_mapping(vm_flags_t flags)
0233 {
0234     return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
0235 }
0236 
0237 /*
0238  * These three helpers classifies VMAs for virtual memory accounting.
0239  */
0240 
0241 /*
0242  * Executable code area - executable, not writable, not stack
0243  */
0244 static inline bool is_exec_mapping(vm_flags_t flags)
0245 {
0246     return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC;
0247 }
0248 
0249 /*
0250  * Stack area - atomatically grows in one direction
0251  *
0252  * VM_GROWSUP / VM_GROWSDOWN VMAs are always private anonymous:
0253  * do_mmap() forbids all other combinations.
0254  */
0255 static inline bool is_stack_mapping(vm_flags_t flags)
0256 {
0257     return (flags & VM_STACK) == VM_STACK;
0258 }
0259 
0260 /*
0261  * Data area - private, writable, not stack
0262  */
0263 static inline bool is_data_mapping(vm_flags_t flags)
0264 {
0265     return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE;
0266 }
0267 
0268 /* mm/util.c */
0269 void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
0270         struct vm_area_struct *prev, struct rb_node *rb_parent);
0271 
0272 #ifdef CONFIG_MMU
0273 extern long populate_vma_page_range(struct vm_area_struct *vma,
0274         unsigned long start, unsigned long end, int *nonblocking);
0275 extern void munlock_vma_pages_range(struct vm_area_struct *vma,
0276             unsigned long start, unsigned long end);
0277 static inline void munlock_vma_pages_all(struct vm_area_struct *vma)
0278 {
0279     munlock_vma_pages_range(vma, vma->vm_start, vma->vm_end);
0280 }
0281 
0282 /*
0283  * must be called with vma's mmap_sem held for read or write, and page locked.
0284  */
0285 extern void mlock_vma_page(struct page *page);
0286 extern unsigned int munlock_vma_page(struct page *page);
0287 
0288 /*
0289  * Clear the page's PageMlocked().  This can be useful in a situation where
0290  * we want to unconditionally remove a page from the pagecache -- e.g.,
0291  * on truncation or freeing.
0292  *
0293  * It is legal to call this function for any page, mlocked or not.
0294  * If called for a page that is still mapped by mlocked vmas, all we do
0295  * is revert to lazy LRU behaviour -- semantics are not broken.
0296  */
0297 extern void clear_page_mlock(struct page *page);
0298 
0299 /*
0300  * mlock_migrate_page - called only from migrate_misplaced_transhuge_page()
0301  * (because that does not go through the full procedure of migration ptes):
0302  * to migrate the Mlocked page flag; update statistics.
0303  */
0304 static inline void mlock_migrate_page(struct page *newpage, struct page *page)
0305 {
0306     if (TestClearPageMlocked(page)) {
0307         int nr_pages = hpage_nr_pages(page);
0308 
0309         /* Holding pmd lock, no change in irq context: __mod is safe */
0310         __mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
0311         SetPageMlocked(newpage);
0312         __mod_zone_page_state(page_zone(newpage), NR_MLOCK, nr_pages);
0313     }
0314 }
0315 
0316 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma);
0317 
0318 /*
0319  * At what user virtual address is page expected in @vma?
0320  */
0321 static inline unsigned long
0322 __vma_address(struct page *page, struct vm_area_struct *vma)
0323 {
0324     pgoff_t pgoff = page_to_pgoff(page);
0325     return vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
0326 }
0327 
0328 static inline unsigned long
0329 vma_address(struct page *page, struct vm_area_struct *vma)
0330 {
0331     unsigned long address = __vma_address(page, vma);
0332 
0333     /* page should be within @vma mapping range */
0334     VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
0335 
0336     return address;
0337 }
0338 
0339 #else /* !CONFIG_MMU */
0340 static inline void clear_page_mlock(struct page *page) { }
0341 static inline void mlock_vma_page(struct page *page) { }
0342 static inline void mlock_migrate_page(struct page *new, struct page *old) { }
0343 
0344 #endif /* !CONFIG_MMU */
0345 
0346 /*
0347  * Return the mem_map entry representing the 'offset' subpage within
0348  * the maximally aligned gigantic page 'base'.  Handle any discontiguity
0349  * in the mem_map at MAX_ORDER_NR_PAGES boundaries.
0350  */
0351 static inline struct page *mem_map_offset(struct page *base, int offset)
0352 {
0353     if (unlikely(offset >= MAX_ORDER_NR_PAGES))
0354         return nth_page(base, offset);
0355     return base + offset;
0356 }
0357 
0358 /*
0359  * Iterator over all subpages within the maximally aligned gigantic
0360  * page 'base'.  Handle any discontiguity in the mem_map.
0361  */
0362 static inline struct page *mem_map_next(struct page *iter,
0363                         struct page *base, int offset)
0364 {
0365     if (unlikely((offset & (MAX_ORDER_NR_PAGES - 1)) == 0)) {
0366         unsigned long pfn = page_to_pfn(base) + offset;
0367         if (!pfn_valid(pfn))
0368             return NULL;
0369         return pfn_to_page(pfn);
0370     }
0371     return iter + 1;
0372 }
0373 
0374 /*
0375  * FLATMEM and DISCONTIGMEM configurations use alloc_bootmem_node,
0376  * so all functions starting at paging_init should be marked __init
0377  * in those cases. SPARSEMEM, however, allows for memory hotplug,
0378  * and alloc_bootmem_node is not used.
0379  */
0380 #ifdef CONFIG_SPARSEMEM
0381 #define __paginginit __meminit
0382 #else
0383 #define __paginginit __init
0384 #endif
0385 
0386 /* Memory initialisation debug and verification */
0387 enum mminit_level {
0388     MMINIT_WARNING,
0389     MMINIT_VERIFY,
0390     MMINIT_TRACE
0391 };
0392 
0393 #ifdef CONFIG_DEBUG_MEMORY_INIT
0394 
0395 extern int mminit_loglevel;
0396 
0397 #define mminit_dprintk(level, prefix, fmt, arg...) \
0398 do { \
0399     if (level < mminit_loglevel) { \
0400         if (level <= MMINIT_WARNING) \
0401             pr_warn("mminit::" prefix " " fmt, ##arg);  \
0402         else \
0403             printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \
0404     } \
0405 } while (0)
0406 
0407 extern void mminit_verify_pageflags_layout(void);
0408 extern void mminit_verify_zonelist(void);
0409 #else
0410 
0411 static inline void mminit_dprintk(enum mminit_level level,
0412                 const char *prefix, const char *fmt, ...)
0413 {
0414 }
0415 
0416 static inline void mminit_verify_pageflags_layout(void)
0417 {
0418 }
0419 
0420 static inline void mminit_verify_zonelist(void)
0421 {
0422 }
0423 #endif /* CONFIG_DEBUG_MEMORY_INIT */
0424 
0425 /* mminit_validate_memmodel_limits is independent of CONFIG_DEBUG_MEMORY_INIT */
0426 #if defined(CONFIG_SPARSEMEM)
0427 extern void mminit_validate_memmodel_limits(unsigned long *start_pfn,
0428                 unsigned long *end_pfn);
0429 #else
0430 static inline void mminit_validate_memmodel_limits(unsigned long *start_pfn,
0431                 unsigned long *end_pfn)
0432 {
0433 }
0434 #endif /* CONFIG_SPARSEMEM */
0435 
0436 #define NODE_RECLAIM_NOSCAN -2
0437 #define NODE_RECLAIM_FULL   -1
0438 #define NODE_RECLAIM_SOME   0
0439 #define NODE_RECLAIM_SUCCESS    1
0440 
0441 extern int hwpoison_filter(struct page *p);
0442 
0443 extern u32 hwpoison_filter_dev_major;
0444 extern u32 hwpoison_filter_dev_minor;
0445 extern u64 hwpoison_filter_flags_mask;
0446 extern u64 hwpoison_filter_flags_value;
0447 extern u64 hwpoison_filter_memcg;
0448 extern u32 hwpoison_filter_enable;
0449 
0450 extern unsigned long  __must_check vm_mmap_pgoff(struct file *, unsigned long,
0451         unsigned long, unsigned long,
0452         unsigned long, unsigned long);
0453 
0454 extern void set_pageblock_order(void);
0455 unsigned long reclaim_clean_pages_from_list(struct zone *zone,
0456                         struct list_head *page_list);
0457 /* The ALLOC_WMARK bits are used as an index to zone->watermark */
0458 #define ALLOC_WMARK_MIN     WMARK_MIN
0459 #define ALLOC_WMARK_LOW     WMARK_LOW
0460 #define ALLOC_WMARK_HIGH    WMARK_HIGH
0461 #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */
0462 
0463 /* Mask to get the watermark bits */
0464 #define ALLOC_WMARK_MASK    (ALLOC_NO_WATERMARKS-1)
0465 
0466 #define ALLOC_HARDER        0x10 /* try to alloc harder */
0467 #define ALLOC_HIGH      0x20 /* __GFP_HIGH set */
0468 #define ALLOC_CPUSET        0x40 /* check for correct cpuset */
0469 #define ALLOC_CMA       0x80 /* allow allocations from CMA areas */
0470 
0471 enum ttu_flags;
0472 struct tlbflush_unmap_batch;
0473 
0474 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
0475 void try_to_unmap_flush(void);
0476 void try_to_unmap_flush_dirty(void);
0477 #else
0478 static inline void try_to_unmap_flush(void)
0479 {
0480 }
0481 static inline void try_to_unmap_flush_dirty(void)
0482 {
0483 }
0484 
0485 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
0486 
0487 extern const struct trace_print_flags pageflag_names[];
0488 extern const struct trace_print_flags vmaflag_names[];
0489 extern const struct trace_print_flags gfpflag_names[];
0490 
0491 #endif  /* __MM_INTERNAL_H */