mm.h 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. #ifndef _LINUX_MM_H
  2. #define _LINUX_MM_H
  3. #include <linux/errno.h>
  4. #ifdef __KERNEL__
  5. #include <linux/gfp.h>
  6. #include <linux/bug.h>
  7. #include <linux/list.h>
  8. #include <linux/mmzone.h>
  9. #include <linux/rbtree.h>
  10. #include <linux/atomic.h>
  11. #include <linux/debug_locks.h>
  12. #include <linux/mm_types.h>
  13. #include <linux/range.h>
  14. #include <linux/pfn.h>
  15. #include <linux/bit_spinlock.h>
  16. #include <linux/shrinker.h>
  17. struct mempolicy;
  18. struct anon_vma;
  19. struct anon_vma_chain;
  20. struct file_ra_state;
  21. struct user_struct;
  22. struct writeback_control;
  23. #ifndef CONFIG_NEED_MULTIPLE_NODES /* Don't use mapnrs, do it properly */
  24. extern unsigned long max_mapnr;
  25. static inline void set_max_mapnr(unsigned long limit)
  26. {
  27. max_mapnr = limit;
  28. }
  29. #else
  30. static inline void set_max_mapnr(unsigned long limit) { }
  31. #endif
  32. extern unsigned long totalram_pages;
  33. extern void * high_memory;
  34. extern int page_cluster;
  35. #ifdef CONFIG_SYSCTL
  36. extern int sysctl_legacy_va_layout;
  37. #else
  38. #define sysctl_legacy_va_layout 0
  39. #endif
  40. #include <asm/page.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/processor.h>
  43. extern unsigned long sysctl_user_reserve_kbytes;
  44. extern unsigned long sysctl_admin_reserve_kbytes;
  45. #define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
  46. /* to align the pointer to the (next) page boundary */
  47. #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
  48. /* test whether an address (unsigned long or pointer) is aligned to PAGE_SIZE */
  49. #define PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)addr, PAGE_SIZE)
  50. /*
  51. * Linux kernel virtual memory manager primitives.
  52. * The idea being to have a "virtual" mm in the same way
  53. * we have a virtual fs - giving a cleaner interface to the
  54. * mm details, and allowing different kinds of memory mappings
  55. * (from shared memory to executable loading to arbitrary
  56. * mmap() functions).
  57. */
  58. extern struct kmem_cache *vm_area_cachep;
  59. #ifndef CONFIG_MMU
  60. extern struct rb_root nommu_region_tree;
  61. extern struct rw_semaphore nommu_region_sem;
  62. extern unsigned int kobjsize(const void *objp);
  63. #endif
  64. /*
  65. * vm_flags in vm_area_struct, see mm_types.h.
  66. */
  67. #define VM_NONE 0x00000000
  68. #define VM_READ 0x00000001 /* currently active flags */
  69. #define VM_WRITE 0x00000002
  70. #define VM_EXEC 0x00000004
  71. #define VM_SHARED 0x00000008
  72. /* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
  73. #define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
  74. #define VM_MAYWRITE 0x00000020
  75. #define VM_MAYEXEC 0x00000040
  76. #define VM_MAYSHARE 0x00000080
  77. #define VM_GROWSDOWN 0x00000100 /* general info on the segment */
  78. #define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */
  79. #define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */
  80. #define VM_LOCKED 0x00002000
  81. #define VM_IO 0x00004000 /* Memory mapped I/O or similar */
  82. /* Used by sys_madvise() */
  83. #define VM_SEQ_READ 0x00008000 /* App will access data sequentially */
  84. #define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */
  85. #define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */
  86. #define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */
  87. #define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */
  88. #define VM_NORESERVE 0x00200000 /* should the VM suppress accounting */
  89. #define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */
  90. #define VM_NONLINEAR 0x00800000 /* Is non-linear (remap_file_pages) */
  91. #define VM_ARCH_1 0x01000000 /* Architecture-specific flag */
  92. #define VM_DONTDUMP 0x04000000 /* Do not include in the core dump */
  93. #ifdef CONFIG_MEM_SOFT_DIRTY
  94. # define VM_SOFTDIRTY 0x08000000 /* Not soft dirty clean area */
  95. #else
  96. # define VM_SOFTDIRTY 0
  97. #endif
  98. #define VM_MIXEDMAP 0x10000000 /* Can contain "struct page" and pure PFN pages */
  99. #define VM_HUGEPAGE 0x20000000 /* MADV_HUGEPAGE marked this vma */
  100. #define VM_NOHUGEPAGE 0x40000000 /* MADV_NOHUGEPAGE marked this vma */
  101. #define VM_MERGEABLE 0x80000000 /* KSM may merge identical pages */
  102. #if defined(CONFIG_X86)
  103. # define VM_PAT VM_ARCH_1 /* PAT reserves whole VMA at once (x86) */
  104. #elif defined(CONFIG_PPC)
  105. # define VM_SAO VM_ARCH_1 /* Strong Access Ordering (powerpc) */
  106. #elif defined(CONFIG_PARISC)
  107. # define VM_GROWSUP VM_ARCH_1
  108. #elif defined(CONFIG_METAG)
  109. # define VM_GROWSUP VM_ARCH_1
  110. #elif defined(CONFIG_IA64)
  111. # define VM_GROWSUP VM_ARCH_1
  112. #elif !defined(CONFIG_MMU)
  113. # define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */
  114. #endif
  115. #ifndef VM_GROWSUP
  116. # define VM_GROWSUP VM_NONE
  117. #endif
  118. /* Bits set in the VMA until the stack is in its final location */
  119. #define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ)
  120. #ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */
  121. #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
  122. #endif
  123. #ifdef CONFIG_STACK_GROWSUP
  124. #define VM_STACK_FLAGS (VM_GROWSUP | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
  125. #else
  126. #define VM_STACK_FLAGS (VM_GROWSDOWN | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
  127. #endif
  128. /*
  129. * Special vmas that are non-mergable, non-mlock()able.
  130. * Note: mm/huge_memory.c VM_NO_THP depends on this definition.
  131. */
  132. #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP)
  133. /*
  134. * mapping from the currently active vm_flags protection bits (the
  135. * low four bits) to a page protection mask..
  136. */
  137. extern pgprot_t protection_map[16];
  138. #define FAULT_FLAG_WRITE 0x01 /* Fault was a write access */
  139. #define FAULT_FLAG_NONLINEAR 0x02 /* Fault was via a nonlinear mapping */
  140. #define FAULT_FLAG_MKWRITE 0x04 /* Fault was mkwrite of existing pte */
  141. #define FAULT_FLAG_ALLOW_RETRY 0x08 /* Retry fault if blocking */
  142. #define FAULT_FLAG_RETRY_NOWAIT 0x10 /* Don't drop mmap_sem and wait when retrying */
  143. #define FAULT_FLAG_KILLABLE 0x20 /* The fault task is in SIGKILL killable region */
  144. #define FAULT_FLAG_TRIED 0x40 /* second try */
  145. #define FAULT_FLAG_USER 0x80 /* The fault originated in userspace */
  146. /*
  147. * vm_fault is filled by the the pagefault handler and passed to the vma's
  148. * ->fault function. The vma's ->fault is responsible for returning a bitmask
  149. * of VM_FAULT_xxx flags that give details about how the fault was handled.
  150. *
  151. * pgoff should be used in favour of virtual_address, if possible. If pgoff
  152. * is used, one may implement ->remap_pages to get nonlinear mapping support.
  153. */
  154. struct vm_fault {
  155. unsigned int flags; /* FAULT_FLAG_xxx flags */
  156. pgoff_t pgoff; /* Logical page offset based on vma */
  157. void __user *virtual_address; /* Faulting virtual address */
  158. struct page *page; /* ->fault handlers should return a
  159. * page here, unless VM_FAULT_NOPAGE
  160. * is set (which is also implied by
  161. * VM_FAULT_ERROR).
  162. */
  163. };
  164. /*
  165. * These are the virtual MM functions - opening of an area, closing and
  166. * unmapping it (needed to keep files on disk up-to-date etc), pointer
  167. * to the functions called when a no-page or a wp-page exception occurs.
  168. */
  169. struct vm_operations_struct {
  170. void (*open)(struct vm_area_struct * area);
  171. void (*close)(struct vm_area_struct * area);
  172. int (*fault)(struct vm_area_struct *vma, struct vm_fault *vmf);
  173. /* notification that a previously read-only page is about to become
  174. * writable, if an error is returned it will cause a SIGBUS */
  175. int (*page_mkwrite)(struct vm_area_struct *vma, struct vm_fault *vmf);
  176. /* called by access_process_vm when get_user_pages() fails, typically
  177. * for use by special VMAs that can switch between memory and hardware
  178. */
  179. int (*access)(struct vm_area_struct *vma, unsigned long addr,
  180. void *buf, int len, int write);
  181. #ifdef CONFIG_NUMA
  182. /*
  183. * set_policy() op must add a reference to any non-NULL @new mempolicy
  184. * to hold the policy upon return. Caller should pass NULL @new to
  185. * remove a policy and fall back to surrounding context--i.e. do not
  186. * install a MPOL_DEFAULT policy, nor the task or system default
  187. * mempolicy.
  188. */
  189. int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
  190. /*
  191. * get_policy() op must add reference [mpol_get()] to any policy at
  192. * (vma,addr) marked as MPOL_SHARED. The shared policy infrastructure
  193. * in mm/mempolicy.c will do this automatically.
  194. * get_policy() must NOT add a ref if the policy at (vma,addr) is not
  195. * marked as MPOL_SHARED. vma policies are protected by the mmap_sem.
  196. * If no [shared/vma] mempolicy exists at the addr, get_policy() op
  197. * must return NULL--i.e., do not "fallback" to task or system default
  198. * policy.
  199. */
  200. struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
  201. unsigned long addr);
  202. int (*migrate)(struct vm_area_struct *vma, const nodemask_t *from,
  203. const nodemask_t *to, unsigned long flags);
  204. #endif
  205. /* called by sys_remap_file_pages() to populate non-linear mapping */
  206. int (*remap_pages)(struct vm_area_struct *vma, unsigned long addr,
  207. unsigned long size, pgoff_t pgoff);
  208. };
  209. struct mmu_gather;
  210. struct inode;
  211. #define page_private(page) ((page)->private)
  212. #define set_page_private(page, v) ((page)->private = (v))
  213. /* It's valid only if the page is free path or free_list */
  214. static inline void set_freepage_migratetype(struct page *page, int migratetype)
  215. {
  216. page->index = migratetype;
  217. }
  218. /* It's valid only if the page is free path or free_list */
  219. static inline int get_freepage_migratetype(struct page *page)
  220. {
  221. return page->index;
  222. }
  223. /*
  224. * FIXME: take this include out, include page-flags.h in
  225. * files which need it (119 of them)
  226. */
  227. #include <linux/page-flags.h>
  228. #include <linux/huge_mm.h>
  229. /*
  230. * Methods to modify the page usage count.
  231. *
  232. * What counts for a page usage:
  233. * - cache mapping (page->mapping)
  234. * - private data (page->private)
  235. * - page mapped in a task's page tables, each mapping
  236. * is counted separately
  237. *
  238. * Also, many kernel routines increase the page count before a critical
  239. * routine so they can be sure the page doesn't go away from under them.
  240. */
  241. /*
  242. * Drop a ref, return true if the refcount fell to zero (the page has no users)
  243. */
  244. static inline int put_page_testzero(struct page *page)
  245. {
  246. VM_BUG_ON(atomic_read(&page->_count) == 0);
  247. return atomic_dec_and_test(&page->_count);
  248. }
  249. /*
  250. * Try to grab a ref unless the page has a refcount of zero, return false if
  251. * that is the case.
  252. * This can be called when MMU is off so it must not access
  253. * any of the virtual mappings.
  254. */
  255. static inline int get_page_unless_zero(struct page *page)
  256. {
  257. return atomic_inc_not_zero(&page->_count);
  258. }
  259. /*
  260. * Try to drop a ref unless the page has a refcount of one, return false if
  261. * that is the case.
  262. * This is to make sure that the refcount won't become zero after this drop.
  263. * This can be called when MMU is off so it must not access
  264. * any of the virtual mappings.
  265. */
  266. static inline int put_page_unless_one(struct page *page)
  267. {
  268. return atomic_add_unless(&page->_count, -1, 1);
  269. }
  270. extern int page_is_ram(unsigned long pfn);
  271. /* Support for virtually mapped pages */
  272. struct page *vmalloc_to_page(const void *addr);
  273. unsigned long vmalloc_to_pfn(const void *addr);
  274. /*
  275. * Determine if an address is within the vmalloc range
  276. *
  277. * On nommu, vmalloc/vfree wrap through kmalloc/kfree directly, so there
  278. * is no special casing required.
  279. */
  280. static inline int is_vmalloc_addr(const void *x)
  281. {
  282. #ifdef CONFIG_MMU
  283. unsigned long addr = (unsigned long)x;
  284. return addr >= VMALLOC_START && addr < VMALLOC_END;
  285. #else
  286. return 0;
  287. #endif
  288. }
  289. #ifdef CONFIG_MMU
  290. extern int is_vmalloc_or_module_addr(const void *x);
  291. #else
  292. static inline int is_vmalloc_or_module_addr(const void *x)
  293. {
  294. return 0;
  295. }
  296. #endif
  297. static inline void compound_lock(struct page *page)
  298. {
  299. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  300. VM_BUG_ON(PageSlab(page));
  301. bit_spin_lock(PG_compound_lock, &page->flags);
  302. #endif
  303. }
  304. static inline void compound_unlock(struct page *page)
  305. {
  306. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  307. VM_BUG_ON(PageSlab(page));
  308. bit_spin_unlock(PG_compound_lock, &page->flags);
  309. #endif
  310. }
  311. static inline unsigned long compound_lock_irqsave(struct page *page)
  312. {
  313. unsigned long uninitialized_var(flags);
  314. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  315. local_irq_save(flags);
  316. compound_lock(page);
  317. #endif
  318. return flags;
  319. }
  320. static inline void compound_unlock_irqrestore(struct page *page,
  321. unsigned long flags)
  322. {
  323. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  324. compound_unlock(page);
  325. local_irq_restore(flags);
  326. #endif
  327. }
  328. static inline struct page *compound_head(struct page *page)
  329. {
  330. if (unlikely(PageTail(page)))
  331. return page->first_page;
  332. return page;
  333. }
  334. /*
  335. * The atomic page->_mapcount, starts from -1: so that transitions
  336. * both from it and to it can be tracked, using atomic_inc_and_test
  337. * and atomic_add_negative(-1).
  338. */
  339. static inline void page_mapcount_reset(struct page *page)
  340. {
  341. atomic_set(&(page)->_mapcount, -1);
  342. }
  343. static inline int page_mapcount(struct page *page)
  344. {
  345. return atomic_read(&(page)->_mapcount) + 1;
  346. }
  347. static inline int page_count(struct page *page)
  348. {
  349. return atomic_read(&compound_head(page)->_count);
  350. }
  351. static inline void get_huge_page_tail(struct page *page)
  352. {
  353. /*
  354. * __split_huge_page_refcount() cannot run
  355. * from under us.
  356. */
  357. VM_BUG_ON(page_mapcount(page) < 0);
  358. VM_BUG_ON(atomic_read(&page->_count) != 0);
  359. atomic_inc(&page->_mapcount);
  360. }
  361. extern bool __get_page_tail(struct page *page);
  362. static inline void get_page(struct page *page)
  363. {
  364. if (unlikely(PageTail(page)))
  365. if (likely(__get_page_tail(page)))
  366. return;
  367. /*
  368. * Getting a normal page or the head of a compound page
  369. * requires to already have an elevated page->_count.
  370. */
  371. VM_BUG_ON(atomic_read(&page->_count) <= 0);
  372. atomic_inc(&page->_count);
  373. }
  374. static inline struct page *virt_to_head_page(const void *x)
  375. {
  376. struct page *page = virt_to_page(x);
  377. return compound_head(page);
  378. }
  379. /*
  380. * Setup the page count before being freed into the page allocator for
  381. * the first time (boot or memory hotplug)
  382. */
  383. static inline void init_page_count(struct page *page)
  384. {
  385. atomic_set(&page->_count, 1);
  386. }
  387. /*
  388. * PageBuddy() indicate that the page is free and in the buddy system
  389. * (see mm/page_alloc.c).
  390. *
  391. * PAGE_BUDDY_MAPCOUNT_VALUE must be <= -2 but better not too close to
  392. * -2 so that an underflow of the page_mapcount() won't be mistaken
  393. * for a genuine PAGE_BUDDY_MAPCOUNT_VALUE. -128 can be created very
  394. * efficiently by most CPU architectures.
  395. */
  396. #define PAGE_BUDDY_MAPCOUNT_VALUE (-128)
  397. static inline int PageBuddy(struct page *page)
  398. {
  399. return atomic_read(&page->_mapcount) == PAGE_BUDDY_MAPCOUNT_VALUE;
  400. }
  401. static inline void __SetPageBuddy(struct page *page)
  402. {
  403. VM_BUG_ON(atomic_read(&page->_mapcount) != -1);
  404. atomic_set(&page->_mapcount, PAGE_BUDDY_MAPCOUNT_VALUE);
  405. }
  406. static inline void __ClearPageBuddy(struct page *page)
  407. {
  408. VM_BUG_ON(!PageBuddy(page));
  409. atomic_set(&page->_mapcount, -1);
  410. }
  411. void put_page(struct page *page);
  412. void put_pages_list(struct list_head *pages);
  413. void split_page(struct page *page, unsigned int order);
  414. int split_free_page(struct page *page);
  415. /*
  416. * Compound pages have a destructor function. Provide a
  417. * prototype for that function and accessor functions.
  418. * These are _only_ valid on the head of a PG_compound page.
  419. */
  420. typedef void compound_page_dtor(struct page *);
  421. static inline void set_compound_page_dtor(struct page *page,
  422. compound_page_dtor *dtor)
  423. {
  424. page[1].lru.next = (void *)dtor;
  425. }
  426. static inline compound_page_dtor *get_compound_page_dtor(struct page *page)
  427. {
  428. return (compound_page_dtor *)page[1].lru.next;
  429. }
  430. static inline int compound_order(struct page *page)
  431. {
  432. if (!PageHead(page))
  433. return 0;
  434. return (unsigned long)page[1].lru.prev;
  435. }
  436. static inline void set_compound_order(struct page *page, unsigned long order)
  437. {
  438. page[1].lru.prev = (void *)order;
  439. }
  440. #ifdef CONFIG_MMU
  441. /*
  442. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  443. * servicing faults for write access. In the normal case, do always want
  444. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  445. * that do not have writing enabled, when used by access_process_vm.
  446. */
  447. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  448. {
  449. if (likely(vma->vm_flags & VM_WRITE))
  450. pte = pte_mkwrite(pte);
  451. return pte;
  452. }
  453. #endif
  454. /*
  455. * Multiple processes may "see" the same page. E.g. for untouched
  456. * mappings of /dev/null, all processes see the same page full of
  457. * zeroes, and text pages of executables and shared libraries have
  458. * only one copy in memory, at most, normally.
  459. *
  460. * For the non-reserved pages, page_count(page) denotes a reference count.
  461. * page_count() == 0 means the page is free. page->lru is then used for
  462. * freelist management in the buddy allocator.
  463. * page_count() > 0 means the page has been allocated.
  464. *
  465. * Pages are allocated by the slab allocator in order to provide memory
  466. * to kmalloc and kmem_cache_alloc. In this case, the management of the
  467. * page, and the fields in 'struct page' are the responsibility of mm/slab.c
  468. * unless a particular usage is carefully commented. (the responsibility of
  469. * freeing the kmalloc memory is the caller's, of course).
  470. *
  471. * A page may be used by anyone else who does a __get_free_page().
  472. * In this case, page_count still tracks the references, and should only
  473. * be used through the normal accessor functions. The top bits of page->flags
  474. * and page->virtual store page management information, but all other fields
  475. * are unused and could be used privately, carefully. The management of this
  476. * page is the responsibility of the one who allocated it, and those who have
  477. * subsequently been given references to it.
  478. *
  479. * The other pages (we may call them "pagecache pages") are completely
  480. * managed by the Linux memory manager: I/O, buffers, swapping etc.
  481. * The following discussion applies only to them.
  482. *
  483. * A pagecache page contains an opaque `private' member, which belongs to the
  484. * page's address_space. Usually, this is the address of a circular list of
  485. * the page's disk buffers. PG_private must be set to tell the VM to call
  486. * into the filesystem to release these pages.
  487. *
  488. * A page may belong to an inode's memory mapping. In this case, page->mapping
  489. * is the pointer to the inode, and page->index is the file offset of the page,
  490. * in units of PAGE_CACHE_SIZE.
  491. *
  492. * If pagecache pages are not associated with an inode, they are said to be
  493. * anonymous pages. These may become associated with the swapcache, and in that
  494. * case PG_swapcache is set, and page->private is an offset into the swapcache.
  495. *
  496. * In either case (swapcache or inode backed), the pagecache itself holds one
  497. * reference to the page. Setting PG_private should also increment the
  498. * refcount. The each user mapping also has a reference to the page.
  499. *
  500. * The pagecache pages are stored in a per-mapping radix tree, which is
  501. * rooted at mapping->page_tree, and indexed by offset.
  502. * Where 2.4 and early 2.6 kernels kept dirty/clean pages in per-address_space
  503. * lists, we instead now tag pages as dirty/writeback in the radix tree.
  504. *
  505. * All pagecache pages may be subject to I/O:
  506. * - inode pages may need to be read from disk,
  507. * - inode pages which have been modified and are MAP_SHARED may need
  508. * to be written back to the inode on disk,
  509. * - anonymous pages (including MAP_PRIVATE file mappings) which have been
  510. * modified may need to be swapped out to swap space and (later) to be read
  511. * back into memory.
  512. */
  513. /*
  514. * The zone field is never updated after free_area_init_core()
  515. * sets it, so none of the operations on it need to be atomic.
  516. */
  517. /* Page flags: | [SECTION] | [NODE] | ZONE | [LAST_NID] | ... | FLAGS | */
  518. #define SECTIONS_PGOFF ((sizeof(unsigned long)*8) - SECTIONS_WIDTH)
  519. #define NODES_PGOFF (SECTIONS_PGOFF - NODES_WIDTH)
  520. #define ZONES_PGOFF (NODES_PGOFF - ZONES_WIDTH)
  521. #define LAST_NID_PGOFF (ZONES_PGOFF - LAST_NID_WIDTH)
  522. /*
  523. * Define the bit shifts to access each section. For non-existent
  524. * sections we define the shift as 0; that plus a 0 mask ensures
  525. * the compiler will optimise away reference to them.
  526. */
  527. #define SECTIONS_PGSHIFT (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0))
  528. #define NODES_PGSHIFT (NODES_PGOFF * (NODES_WIDTH != 0))
  529. #define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0))
  530. #define LAST_NID_PGSHIFT (LAST_NID_PGOFF * (LAST_NID_WIDTH != 0))
  531. /* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator */
  532. #ifdef NODE_NOT_IN_PAGE_FLAGS
  533. #define ZONEID_SHIFT (SECTIONS_SHIFT + ZONES_SHIFT)
  534. #define ZONEID_PGOFF ((SECTIONS_PGOFF < ZONES_PGOFF)? \
  535. SECTIONS_PGOFF : ZONES_PGOFF)
  536. #else
  537. #define ZONEID_SHIFT (NODES_SHIFT + ZONES_SHIFT)
  538. #define ZONEID_PGOFF ((NODES_PGOFF < ZONES_PGOFF)? \
  539. NODES_PGOFF : ZONES_PGOFF)
  540. #endif
  541. #define ZONEID_PGSHIFT (ZONEID_PGOFF * (ZONEID_SHIFT != 0))
  542. #if SECTIONS_WIDTH+NODES_WIDTH+ZONES_WIDTH > BITS_PER_LONG - NR_PAGEFLAGS
  543. #error SECTIONS_WIDTH+NODES_WIDTH+ZONES_WIDTH > BITS_PER_LONG - NR_PAGEFLAGS
  544. #endif
  545. #define ZONES_MASK ((1UL << ZONES_WIDTH) - 1)
  546. #define NODES_MASK ((1UL << NODES_WIDTH) - 1)
  547. #define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1)
  548. #define LAST_NID_MASK ((1UL << LAST_NID_WIDTH) - 1)
  549. #define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1)
  550. static inline enum zone_type page_zonenum(const struct page *page)
  551. {
  552. return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK;
  553. }
  554. #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
  555. #define SECTION_IN_PAGE_FLAGS
  556. #endif
  557. /*
  558. * The identification function is mainly used by the buddy allocator for
  559. * determining if two pages could be buddies. We are not really identifying
  560. * the zone since we could be using the section number id if we do not have
  561. * node id available in page flags.
  562. * We only guarantee that it will return the same value for two combinable
  563. * pages in a zone.
  564. */
  565. static inline int page_zone_id(struct page *page)
  566. {
  567. return (page->flags >> ZONEID_PGSHIFT) & ZONEID_MASK;
  568. }
  569. static inline int zone_to_nid(struct zone *zone)
  570. {
  571. #ifdef CONFIG_NUMA
  572. return zone->node;
  573. #else
  574. return 0;
  575. #endif
  576. }
  577. #ifdef NODE_NOT_IN_PAGE_FLAGS
  578. extern int page_to_nid(const struct page *page);
  579. #else
  580. static inline int page_to_nid(const struct page *page)
  581. {
  582. return (page->flags >> NODES_PGSHIFT) & NODES_MASK;
  583. }
  584. #endif
  585. #ifdef CONFIG_NUMA_BALANCING
  586. #ifdef LAST_NID_NOT_IN_PAGE_FLAGS
  587. static inline int page_nid_xchg_last(struct page *page, int nid)
  588. {
  589. return xchg(&page->_last_nid, nid);
  590. }
  591. static inline int page_nid_last(struct page *page)
  592. {
  593. return page->_last_nid;
  594. }
  595. static inline void page_nid_reset_last(struct page *page)
  596. {
  597. page->_last_nid = -1;
  598. }
  599. #else
  600. static inline int page_nid_last(struct page *page)
  601. {
  602. return (page->flags >> LAST_NID_PGSHIFT) & LAST_NID_MASK;
  603. }
  604. extern int page_nid_xchg_last(struct page *page, int nid);
  605. static inline void page_nid_reset_last(struct page *page)
  606. {
  607. int nid = (1 << LAST_NID_SHIFT) - 1;
  608. page->flags &= ~(LAST_NID_MASK << LAST_NID_PGSHIFT);
  609. page->flags |= (nid & LAST_NID_MASK) << LAST_NID_PGSHIFT;
  610. }
  611. #endif /* LAST_NID_NOT_IN_PAGE_FLAGS */
  612. #else
  613. static inline int page_nid_xchg_last(struct page *page, int nid)
  614. {
  615. return page_to_nid(page);
  616. }
  617. static inline int page_nid_last(struct page *page)
  618. {
  619. return page_to_nid(page);
  620. }
  621. static inline void page_nid_reset_last(struct page *page)
  622. {
  623. }
  624. #endif
  625. static inline struct zone *page_zone(const struct page *page)
  626. {
  627. return &NODE_DATA(page_to_nid(page))->node_zones[page_zonenum(page)];
  628. }
  629. #ifdef SECTION_IN_PAGE_FLAGS
  630. static inline void set_page_section(struct page *page, unsigned long section)
  631. {
  632. page->flags &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT);
  633. page->flags |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT;
  634. }
  635. static inline unsigned long page_to_section(const struct page *page)
  636. {
  637. return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
  638. }
  639. #endif
  640. static inline void set_page_zone(struct page *page, enum zone_type zone)
  641. {
  642. page->flags &= ~(ZONES_MASK << ZONES_PGSHIFT);
  643. page->flags |= (zone & ZONES_MASK) << ZONES_PGSHIFT;
  644. }
  645. static inline void set_page_node(struct page *page, unsigned long node)
  646. {
  647. page->flags &= ~(NODES_MASK << NODES_PGSHIFT);
  648. page->flags |= (node & NODES_MASK) << NODES_PGSHIFT;
  649. }
  650. static inline void set_page_links(struct page *page, enum zone_type zone,
  651. unsigned long node, unsigned long pfn)
  652. {
  653. set_page_zone(page, zone);
  654. set_page_node(page, node);
  655. #ifdef SECTION_IN_PAGE_FLAGS
  656. set_page_section(page, pfn_to_section_nr(pfn));
  657. #endif
  658. }
  659. /*
  660. * Some inline functions in vmstat.h depend on page_zone()
  661. */
  662. #include <linux/vmstat.h>
  663. static __always_inline void *lowmem_page_address(const struct page *page)
  664. {
  665. return __va(PFN_PHYS(page_to_pfn(page)));
  666. }
  667. #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
  668. #define HASHED_PAGE_VIRTUAL
  669. #endif
  670. #if defined(WANT_PAGE_VIRTUAL)
  671. #define page_address(page) ((page)->virtual)
  672. #define set_page_address(page, address) \
  673. do { \
  674. (page)->virtual = (address); \
  675. } while(0)
  676. #define page_address_init() do { } while(0)
  677. #endif
  678. #if defined(HASHED_PAGE_VIRTUAL)
  679. void *page_address(const struct page *page);
  680. void set_page_address(struct page *page, void *virtual);
  681. void page_address_init(void);
  682. #endif
  683. #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
  684. #define page_address(page) lowmem_page_address(page)
  685. #define set_page_address(page, address) do { } while(0)
  686. #define page_address_init() do { } while(0)
  687. #endif
  688. /*
  689. * On an anonymous page mapped into a user virtual memory area,
  690. * page->mapping points to its anon_vma, not to a struct address_space;
  691. * with the PAGE_MAPPING_ANON bit set to distinguish it. See rmap.h.
  692. *
  693. * On an anonymous page in a VM_MERGEABLE area, if CONFIG_KSM is enabled,
  694. * the PAGE_MAPPING_KSM bit may be set along with the PAGE_MAPPING_ANON bit;
  695. * and then page->mapping points, not to an anon_vma, but to a private
  696. * structure which KSM associates with that merged page. See ksm.h.
  697. *
  698. * PAGE_MAPPING_KSM without PAGE_MAPPING_ANON is currently never used.
  699. *
  700. * Please note that, confusingly, "page_mapping" refers to the inode
  701. * address_space which maps the page from disk; whereas "page_mapped"
  702. * refers to user virtual address space into which the page is mapped.
  703. */
  704. #define PAGE_MAPPING_ANON 1
  705. #define PAGE_MAPPING_KSM 2
  706. #define PAGE_MAPPING_FLAGS (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM)
  707. extern struct address_space *page_mapping(struct page *page);
  708. /* Neutral page->mapping pointer to address_space or anon_vma or other */
  709. static inline void *page_rmapping(struct page *page)
  710. {
  711. return (void *)((unsigned long)page->mapping & ~PAGE_MAPPING_FLAGS);
  712. }
  713. extern struct address_space *__page_file_mapping(struct page *);
  714. static inline
  715. struct address_space *page_file_mapping(struct page *page)
  716. {
  717. if (unlikely(PageSwapCache(page)))
  718. return __page_file_mapping(page);
  719. return page->mapping;
  720. }
  721. static inline int PageAnon(struct page *page)
  722. {
  723. return ((unsigned long)page->mapping & PAGE_MAPPING_ANON) != 0;
  724. }
  725. /*
  726. * Return the pagecache index of the passed page. Regular pagecache pages
  727. * use ->index whereas swapcache pages use ->private
  728. */
  729. static inline pgoff_t page_index(struct page *page)
  730. {
  731. if (unlikely(PageSwapCache(page)))
  732. return page_private(page);
  733. return page->index;
  734. }
  735. extern pgoff_t __page_file_index(struct page *page);
  736. /*
  737. * Return the file index of the page. Regular pagecache pages use ->index
  738. * whereas swapcache pages use swp_offset(->private)
  739. */
  740. static inline pgoff_t page_file_index(struct page *page)
  741. {
  742. if (unlikely(PageSwapCache(page)))
  743. return __page_file_index(page);
  744. return page->index;
  745. }
  746. /*
  747. * Return true if this page is mapped into pagetables.
  748. */
  749. static inline int page_mapped(struct page *page)
  750. {
  751. return atomic_read(&(page)->_mapcount) >= 0;
  752. }
  753. /*
  754. * Different kinds of faults, as returned by handle_mm_fault().
  755. * Used to decide whether a process gets delivered SIGBUS or
  756. * just gets major/minor fault counters bumped up.
  757. */
  758. #define VM_FAULT_MINOR 0 /* For backwards compat. Remove me quickly. */
  759. #define VM_FAULT_OOM 0x0001
  760. #define VM_FAULT_SIGBUS 0x0002
  761. #define VM_FAULT_MAJOR 0x0004
  762. #define VM_FAULT_WRITE 0x0008 /* Special case for get_user_pages */
  763. #define VM_FAULT_HWPOISON 0x0010 /* Hit poisoned small page */
  764. #define VM_FAULT_HWPOISON_LARGE 0x0020 /* Hit poisoned large page. Index encoded in upper bits */
  765. #define VM_FAULT_NOPAGE 0x0100 /* ->fault installed the pte, not return page */
  766. #define VM_FAULT_LOCKED 0x0200 /* ->fault locked the returned page */
  767. #define VM_FAULT_RETRY 0x0400 /* ->fault blocked, must retry */
  768. #define VM_FAULT_FALLBACK 0x0800 /* huge page fault failed, fall back to small */
  769. #define VM_FAULT_HWPOISON_LARGE_MASK 0xf000 /* encodes hpage index for large hwpoison */
  770. #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | VM_FAULT_HWPOISON | \
  771. VM_FAULT_FALLBACK | VM_FAULT_HWPOISON_LARGE)
  772. /* Encode hstate index for a hwpoisoned large page */
  773. #define VM_FAULT_SET_HINDEX(x) ((x) << 12)
  774. #define VM_FAULT_GET_HINDEX(x) (((x) >> 12) & 0xf)
  775. /*
  776. * Can be called by the pagefault handler when it gets a VM_FAULT_OOM.
  777. */
  778. extern void pagefault_out_of_memory(void);
  779. #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
  780. /*
  781. * Flags passed to show_mem() and show_free_areas() to suppress output in
  782. * various contexts.
  783. */
  784. #define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */
  785. #define SHOW_MEM_FILTER_PAGE_COUNT (0x0002u) /* page type count */
  786. extern void show_free_areas(unsigned int flags);
  787. extern bool skip_free_areas_node(unsigned int flags, int nid);
  788. int shmem_zero_setup(struct vm_area_struct *);
  789. extern int can_do_mlock(void);
  790. extern int user_shm_lock(size_t, struct user_struct *);
  791. extern void user_shm_unlock(size_t, struct user_struct *);
  792. /*
  793. * Parameter block passed down to zap_pte_range in exceptional cases.
  794. */
  795. struct zap_details {
  796. struct vm_area_struct *nonlinear_vma; /* Check page->index if set */
  797. struct address_space *check_mapping; /* Check page->mapping if set */
  798. pgoff_t first_index; /* Lowest page->index to unmap */
  799. pgoff_t last_index; /* Highest page->index to unmap */
  800. };
  801. struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
  802. pte_t pte);
  803. int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
  804. unsigned long size);
  805. void zap_page_range(struct vm_area_struct *vma, unsigned long address,
  806. unsigned long size, struct zap_details *);
  807. void unmap_vmas(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
  808. unsigned long start, unsigned long end);
  809. /**
  810. * mm_walk - callbacks for walk_page_range
  811. * @pgd_entry: if set, called for each non-empty PGD (top-level) entry
  812. * @pud_entry: if set, called for each non-empty PUD (2nd-level) entry
  813. * @pmd_entry: if set, called for each non-empty PMD (3rd-level) entry
  814. * this handler is required to be able to handle
  815. * pmd_trans_huge() pmds. They may simply choose to
  816. * split_huge_page() instead of handling it explicitly.
  817. * @pte_entry: if set, called for each non-empty PTE (4th-level) entry
  818. * @pte_hole: if set, called for each hole at all levels
  819. * @hugetlb_entry: if set, called for each hugetlb entry
  820. * *Caution*: The caller must hold mmap_sem() if @hugetlb_entry
  821. * is used.
  822. *
  823. * (see walk_page_range for more details)
  824. */
  825. struct mm_walk {
  826. int (*pgd_entry)(pgd_t *pgd, unsigned long addr,
  827. unsigned long next, struct mm_walk *walk);
  828. int (*pud_entry)(pud_t *pud, unsigned long addr,
  829. unsigned long next, struct mm_walk *walk);
  830. int (*pmd_entry)(pmd_t *pmd, unsigned long addr,
  831. unsigned long next, struct mm_walk *walk);
  832. int (*pte_entry)(pte_t *pte, unsigned long addr,
  833. unsigned long next, struct mm_walk *walk);
  834. int (*pte_hole)(unsigned long addr, unsigned long next,
  835. struct mm_walk *walk);
  836. int (*hugetlb_entry)(pte_t *pte, unsigned long hmask,
  837. unsigned long addr, unsigned long next,
  838. struct mm_walk *walk);
  839. struct mm_struct *mm;
  840. void *private;
  841. };
  842. int walk_page_range(unsigned long addr, unsigned long end,
  843. struct mm_walk *walk);
  844. void free_pgd_range(struct mmu_gather *tlb, unsigned long addr,
  845. unsigned long end, unsigned long floor, unsigned long ceiling);
  846. int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
  847. struct vm_area_struct *vma);
  848. void unmap_mapping_range(struct address_space *mapping,
  849. loff_t const holebegin, loff_t const holelen, int even_cows);
  850. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  851. unsigned long *pfn);
  852. int follow_phys(struct vm_area_struct *vma, unsigned long address,
  853. unsigned int flags, unsigned long *prot, resource_size_t *phys);
  854. int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
  855. void *buf, int len, int write);
  856. static inline void unmap_shared_mapping_range(struct address_space *mapping,
  857. loff_t const holebegin, loff_t const holelen)
  858. {
  859. unmap_mapping_range(mapping, holebegin, holelen, 0);
  860. }
  861. extern void truncate_pagecache(struct inode *inode, loff_t new);
  862. extern void truncate_setsize(struct inode *inode, loff_t newsize);
  863. void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end);
  864. int truncate_inode_page(struct address_space *mapping, struct page *page);
  865. int generic_error_remove_page(struct address_space *mapping, struct page *page);
  866. int invalidate_inode_page(struct page *page);
  867. #ifdef CONFIG_MMU
  868. extern int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  869. unsigned long address, unsigned int flags);
  870. extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  871. unsigned long address, unsigned int fault_flags);
  872. #else
  873. static inline int handle_mm_fault(struct mm_struct *mm,
  874. struct vm_area_struct *vma, unsigned long address,
  875. unsigned int flags)
  876. {
  877. /* should never happen if there's no MMU */
  878. BUG();
  879. return VM_FAULT_SIGBUS;
  880. }
  881. static inline int fixup_user_fault(struct task_struct *tsk,
  882. struct mm_struct *mm, unsigned long address,
  883. unsigned int fault_flags)
  884. {
  885. /* should never happen if there's no MMU */
  886. BUG();
  887. return -EFAULT;
  888. }
  889. #endif
  890. extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
  891. extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  892. void *buf, int len, int write);
  893. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  894. unsigned long start, unsigned long nr_pages,
  895. unsigned int foll_flags, struct page **pages,
  896. struct vm_area_struct **vmas, int *nonblocking);
  897. long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  898. unsigned long start, unsigned long nr_pages,
  899. int write, int force, struct page **pages,
  900. struct vm_area_struct **vmas);
  901. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  902. struct page **pages);
  903. struct kvec;
  904. int get_kernel_pages(const struct kvec *iov, int nr_pages, int write,
  905. struct page **pages);
  906. int get_kernel_page(unsigned long start, int write, struct page **pages);
  907. struct page *get_dump_page(unsigned long addr);
  908. extern int try_to_release_page(struct page * page, gfp_t gfp_mask);
  909. extern void do_invalidatepage(struct page *page, unsigned int offset,
  910. unsigned int length);
  911. int __set_page_dirty_nobuffers(struct page *page);
  912. int __set_page_dirty_no_writeback(struct page *page);
  913. int redirty_page_for_writepage(struct writeback_control *wbc,
  914. struct page *page);
  915. void account_page_dirtied(struct page *page, struct address_space *mapping);
  916. void account_page_writeback(struct page *page);
  917. int set_page_dirty(struct page *page);
  918. int set_page_dirty_lock(struct page *page);
  919. int clear_page_dirty_for_io(struct page *page);
  920. /* Is the vma a continuation of the stack vma above it? */
  921. static inline int vma_growsdown(struct vm_area_struct *vma, unsigned long addr)
  922. {
  923. return vma && (vma->vm_end == addr) && (vma->vm_flags & VM_GROWSDOWN);
  924. }
  925. static inline int stack_guard_page_start(struct vm_area_struct *vma,
  926. unsigned long addr)
  927. {
  928. return (vma->vm_flags & VM_GROWSDOWN) &&
  929. (vma->vm_start == addr) &&
  930. !vma_growsdown(vma->vm_prev, addr);
  931. }
  932. /* Is the vma a continuation of the stack vma below it? */
  933. static inline int vma_growsup(struct vm_area_struct *vma, unsigned long addr)
  934. {
  935. return vma && (vma->vm_start == addr) && (vma->vm_flags & VM_GROWSUP);
  936. }
  937. static inline int stack_guard_page_end(struct vm_area_struct *vma,
  938. unsigned long addr)
  939. {
  940. return (vma->vm_flags & VM_GROWSUP) &&
  941. (vma->vm_end == addr) &&
  942. !vma_growsup(vma->vm_next, addr);
  943. }
  944. extern pid_t
  945. vm_is_stack(struct task_struct *task, struct vm_area_struct *vma, int in_group);
  946. extern unsigned long move_page_tables(struct vm_area_struct *vma,
  947. unsigned long old_addr, struct vm_area_struct *new_vma,
  948. unsigned long new_addr, unsigned long len,
  949. bool need_rmap_locks);
  950. extern unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
  951. unsigned long end, pgprot_t newprot,
  952. int dirty_accountable, int prot_numa);
  953. extern int mprotect_fixup(struct vm_area_struct *vma,
  954. struct vm_area_struct **pprev, unsigned long start,
  955. unsigned long end, unsigned long newflags);
  956. /*
  957. * doesn't attempt to fault and will return short.
  958. */
  959. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  960. struct page **pages);
  961. /*
  962. * per-process(per-mm_struct) statistics.
  963. */
  964. static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
  965. {
  966. long val = atomic_long_read(&mm->rss_stat.count[member]);
  967. #ifdef SPLIT_RSS_COUNTING
  968. /*
  969. * counter is updated in asynchronous manner and may go to minus.
  970. * But it's never be expected number for users.
  971. */
  972. if (val < 0)
  973. val = 0;
  974. #endif
  975. return (unsigned long)val;
  976. }
  977. static inline void add_mm_counter(struct mm_struct *mm, int member, long value)
  978. {
  979. atomic_long_add(value, &mm->rss_stat.count[member]);
  980. }
  981. static inline void inc_mm_counter(struct mm_struct *mm, int member)
  982. {
  983. atomic_long_inc(&mm->rss_stat.count[member]);
  984. }
  985. static inline void dec_mm_counter(struct mm_struct *mm, int member)
  986. {
  987. atomic_long_dec(&mm->rss_stat.count[member]);
  988. }
  989. static inline unsigned long get_mm_rss(struct mm_struct *mm)
  990. {
  991. return get_mm_counter(mm, MM_FILEPAGES) +
  992. get_mm_counter(mm, MM_ANONPAGES);
  993. }
  994. static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm)
  995. {
  996. return max(mm->hiwater_rss, get_mm_rss(mm));
  997. }
  998. static inline unsigned long get_mm_hiwater_vm(struct mm_struct *mm)
  999. {
  1000. return max(mm->hiwater_vm, mm->total_vm);
  1001. }
  1002. static inline void update_hiwater_rss(struct mm_struct *mm)
  1003. {
  1004. unsigned long _rss = get_mm_rss(mm);
  1005. if ((mm)->hiwater_rss < _rss)
  1006. (mm)->hiwater_rss = _rss;
  1007. }
  1008. static inline void update_hiwater_vm(struct mm_struct *mm)
  1009. {
  1010. if (mm->hiwater_vm < mm->total_vm)
  1011. mm->hiwater_vm = mm->total_vm;
  1012. }
  1013. static inline void setmax_mm_hiwater_rss(unsigned long *maxrss,
  1014. struct mm_struct *mm)
  1015. {
  1016. unsigned long hiwater_rss = get_mm_hiwater_rss(mm);
  1017. if (*maxrss < hiwater_rss)
  1018. *maxrss = hiwater_rss;
  1019. }
  1020. #if defined(SPLIT_RSS_COUNTING)
  1021. void sync_mm_rss(struct mm_struct *mm);
  1022. #else
  1023. static inline void sync_mm_rss(struct mm_struct *mm)
  1024. {
  1025. }
  1026. #endif
  1027. int vma_wants_writenotify(struct vm_area_struct *vma);
  1028. extern pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1029. spinlock_t **ptl);
  1030. static inline pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1031. spinlock_t **ptl)
  1032. {
  1033. pte_t *ptep;
  1034. __cond_lock(*ptl, ptep = __get_locked_pte(mm, addr, ptl));
  1035. return ptep;
  1036. }
  1037. #ifdef __PAGETABLE_PUD_FOLDED
  1038. static inline int __pud_alloc(struct mm_struct *mm, pgd_t *pgd,
  1039. unsigned long address)
  1040. {
  1041. return 0;
  1042. }
  1043. #else
  1044. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address);
  1045. #endif
  1046. #ifdef __PAGETABLE_PMD_FOLDED
  1047. static inline int __pmd_alloc(struct mm_struct *mm, pud_t *pud,
  1048. unsigned long address)
  1049. {
  1050. return 0;
  1051. }
  1052. #else
  1053. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address);
  1054. #endif
  1055. int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
  1056. pmd_t *pmd, unsigned long address);
  1057. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address);
  1058. /*
  1059. * The following ifdef needed to get the 4level-fixup.h header to work.
  1060. * Remove it when 4level-fixup.h has been removed.
  1061. */
  1062. #if defined(CONFIG_MMU) && !defined(__ARCH_HAS_4LEVEL_HACK)
  1063. static inline pud_t *pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  1064. {
  1065. return (unlikely(pgd_none(*pgd)) && __pud_alloc(mm, pgd, address))?
  1066. NULL: pud_offset(pgd, address);
  1067. }
  1068. static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  1069. {
  1070. return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address))?
  1071. NULL: pmd_offset(pud, address);
  1072. }
  1073. #endif /* CONFIG_MMU && !__ARCH_HAS_4LEVEL_HACK */
  1074. #if USE_SPLIT_PTLOCKS
  1075. /*
  1076. * We tuck a spinlock to guard each pagetable page into its struct page,
  1077. * at page->private, with BUILD_BUG_ON to make sure that this will not
  1078. * overflow into the next struct page (as it might with DEBUG_SPINLOCK).
  1079. * When freeing, reset page->mapping so free_pages_check won't complain.
  1080. */
  1081. #define __pte_lockptr(page) &((page)->ptl)
  1082. #define pte_lock_init(_page) do { \
  1083. spin_lock_init(__pte_lockptr(_page)); \
  1084. } while (0)
  1085. #define pte_lock_deinit(page) ((page)->mapping = NULL)
  1086. #define pte_lockptr(mm, pmd) ({(void)(mm); __pte_lockptr(pmd_page(*(pmd)));})
  1087. #else /* !USE_SPLIT_PTLOCKS */
  1088. /*
  1089. * We use mm->page_table_lock to guard all pagetable pages of the mm.
  1090. */
  1091. #define pte_lock_init(page) do {} while (0)
  1092. #define pte_lock_deinit(page) do {} while (0)
  1093. #define pte_lockptr(mm, pmd) ({(void)(pmd); &(mm)->page_table_lock;})
  1094. #endif /* USE_SPLIT_PTLOCKS */
  1095. static inline void pgtable_page_ctor(struct page *page)
  1096. {
  1097. pte_lock_init(page);
  1098. inc_zone_page_state(page, NR_PAGETABLE);
  1099. }
  1100. static inline void pgtable_page_dtor(struct page *page)
  1101. {
  1102. pte_lock_deinit(page);
  1103. dec_zone_page_state(page, NR_PAGETABLE);
  1104. }
  1105. #define pte_offset_map_lock(mm, pmd, address, ptlp) \
  1106. ({ \
  1107. spinlock_t *__ptl = pte_lockptr(mm, pmd); \
  1108. pte_t *__pte = pte_offset_map(pmd, address); \
  1109. *(ptlp) = __ptl; \
  1110. spin_lock(__ptl); \
  1111. __pte; \
  1112. })
  1113. #define pte_unmap_unlock(pte, ptl) do { \
  1114. spin_unlock(ptl); \
  1115. pte_unmap(pte); \
  1116. } while (0)
  1117. #define pte_alloc_map(mm, vma, pmd, address) \
  1118. ((unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, vma, \
  1119. pmd, address))? \
  1120. NULL: pte_offset_map(pmd, address))
  1121. #define pte_alloc_map_lock(mm, pmd, address, ptlp) \
  1122. ((unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, NULL, \
  1123. pmd, address))? \
  1124. NULL: pte_offset_map_lock(mm, pmd, address, ptlp))
  1125. #define pte_alloc_kernel(pmd, address) \
  1126. ((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd, address))? \
  1127. NULL: pte_offset_kernel(pmd, address))
  1128. extern void free_area_init(unsigned long * zones_size);
  1129. extern void free_area_init_node(int nid, unsigned long * zones_size,
  1130. unsigned long zone_start_pfn, unsigned long *zholes_size);
  1131. extern void free_initmem(void);
  1132. /*
  1133. * Free reserved pages within range [PAGE_ALIGN(start), end & PAGE_MASK)
  1134. * into the buddy system. The freed pages will be poisoned with pattern
  1135. * "poison" if it's within range [0, UCHAR_MAX].
  1136. * Return pages freed into the buddy system.
  1137. */
  1138. extern unsigned long free_reserved_area(void *start, void *end,
  1139. int poison, char *s);
  1140. #ifdef CONFIG_HIGHMEM
  1141. /*
  1142. * Free a highmem page into the buddy system, adjusting totalhigh_pages
  1143. * and totalram_pages.
  1144. */
  1145. extern void free_highmem_page(struct page *page);
  1146. #endif
  1147. extern void adjust_managed_page_count(struct page *page, long count);
  1148. extern void mem_init_print_info(const char *str);
  1149. /* Free the reserved page into the buddy system, so it gets managed. */
  1150. static inline void __free_reserved_page(struct page *page)
  1151. {
  1152. ClearPageReserved(page);
  1153. init_page_count(page);
  1154. __free_page(page);
  1155. }
  1156. static inline void free_reserved_page(struct page *page)
  1157. {
  1158. __free_reserved_page(page);
  1159. adjust_managed_page_count(page, 1);
  1160. }
  1161. static inline void mark_page_reserved(struct page *page)
  1162. {
  1163. SetPageReserved(page);
  1164. adjust_managed_page_count(page, -1);
  1165. }
  1166. /*
  1167. * Default method to free all the __init memory into the buddy system.
  1168. * The freed pages will be poisoned with pattern "poison" if it's within
  1169. * range [0, UCHAR_MAX].
  1170. * Return pages freed into the buddy system.
  1171. */
  1172. static inline unsigned long free_initmem_default(int poison)
  1173. {
  1174. extern char __init_begin[], __init_end[];
  1175. return free_reserved_area(&__init_begin, &__init_end,
  1176. poison, "unused kernel");
  1177. }
  1178. static inline unsigned long get_num_physpages(void)
  1179. {
  1180. int nid;
  1181. unsigned long phys_pages = 0;
  1182. for_each_online_node(nid)
  1183. phys_pages += node_present_pages(nid);
  1184. return phys_pages;
  1185. }
  1186. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  1187. /*
  1188. * With CONFIG_HAVE_MEMBLOCK_NODE_MAP set, an architecture may initialise its
  1189. * zones, allocate the backing mem_map and account for memory holes in a more
  1190. * architecture independent manner. This is a substitute for creating the
  1191. * zone_sizes[] and zholes_size[] arrays and passing them to
  1192. * free_area_init_node()
  1193. *
  1194. * An architecture is expected to register range of page frames backed by
  1195. * physical memory with memblock_add[_node]() before calling
  1196. * free_area_init_nodes() passing in the PFN each zone ends at. At a basic
  1197. * usage, an architecture is expected to do something like
  1198. *
  1199. * unsigned long max_zone_pfns[MAX_NR_ZONES] = {max_dma, max_normal_pfn,
  1200. * max_highmem_pfn};
  1201. * for_each_valid_physical_page_range()
  1202. * memblock_add_node(base, size, nid)
  1203. * free_area_init_nodes(max_zone_pfns);
  1204. *
  1205. * free_bootmem_with_active_regions() calls free_bootmem_node() for each
  1206. * registered physical page range. Similarly
  1207. * sparse_memory_present_with_active_regions() calls memory_present() for
  1208. * each range when SPARSEMEM is enabled.
  1209. *
  1210. * See mm/page_alloc.c for more information on each function exposed by
  1211. * CONFIG_HAVE_MEMBLOCK_NODE_MAP.
  1212. */
  1213. extern void free_area_init_nodes(unsigned long *max_zone_pfn);
  1214. unsigned long node_map_pfn_alignment(void);
  1215. unsigned long __absent_pages_in_range(int nid, unsigned long start_pfn,
  1216. unsigned long end_pfn);
  1217. extern unsigned long absent_pages_in_range(unsigned long start_pfn,
  1218. unsigned long end_pfn);
  1219. extern void get_pfn_range_for_nid(unsigned int nid,
  1220. unsigned long *start_pfn, unsigned long *end_pfn);
  1221. extern unsigned long find_min_pfn_with_active_regions(void);
  1222. extern void free_bootmem_with_active_regions(int nid,
  1223. unsigned long max_low_pfn);
  1224. extern void sparse_memory_present_with_active_regions(int nid);
  1225. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  1226. #if !defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP) && \
  1227. !defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID)
  1228. static inline int __early_pfn_to_nid(unsigned long pfn)
  1229. {
  1230. return 0;
  1231. }
  1232. #else
  1233. /* please see mm/page_alloc.c */
  1234. extern int __meminit early_pfn_to_nid(unsigned long pfn);
  1235. #ifdef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  1236. /* there is a per-arch backend function. */
  1237. extern int __meminit __early_pfn_to_nid(unsigned long pfn);
  1238. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  1239. #endif
  1240. extern void set_dma_reserve(unsigned long new_dma_reserve);
  1241. extern void memmap_init_zone(unsigned long, int, unsigned long,
  1242. unsigned long, enum memmap_context);
  1243. extern void setup_per_zone_wmarks(void);
  1244. extern int __meminit init_per_zone_wmark_min(void);
  1245. extern void mem_init(void);
  1246. extern void __init mmap_init(void);
  1247. extern void show_mem(unsigned int flags);
  1248. extern void si_meminfo(struct sysinfo * val);
  1249. extern void si_meminfo_node(struct sysinfo *val, int nid);
  1250. extern __printf(3, 4)
  1251. void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...);
  1252. extern void setup_per_cpu_pageset(void);
  1253. extern void zone_pcp_update(struct zone *zone);
  1254. extern void zone_pcp_reset(struct zone *zone);
  1255. /* page_alloc.c */
  1256. extern int min_free_kbytes;
  1257. /* nommu.c */
  1258. extern atomic_long_t mmap_pages_allocated;
  1259. extern int nommu_shrink_inode_mappings(struct inode *, size_t, size_t);
  1260. /* interval_tree.c */
  1261. void vma_interval_tree_insert(struct vm_area_struct *node,
  1262. struct rb_root *root);
  1263. void vma_interval_tree_insert_after(struct vm_area_struct *node,
  1264. struct vm_area_struct *prev,
  1265. struct rb_root *root);
  1266. void vma_interval_tree_remove(struct vm_area_struct *node,
  1267. struct rb_root *root);
  1268. struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root *root,
  1269. unsigned long start, unsigned long last);
  1270. struct vm_area_struct *vma_interval_tree_iter_next(struct vm_area_struct *node,
  1271. unsigned long start, unsigned long last);
  1272. #define vma_interval_tree_foreach(vma, root, start, last) \
  1273. for (vma = vma_interval_tree_iter_first(root, start, last); \
  1274. vma; vma = vma_interval_tree_iter_next(vma, start, last))
  1275. static inline void vma_nonlinear_insert(struct vm_area_struct *vma,
  1276. struct list_head *list)
  1277. {
  1278. list_add_tail(&vma->shared.nonlinear, list);
  1279. }
  1280. void anon_vma_interval_tree_insert(struct anon_vma_chain *node,
  1281. struct rb_root *root);
  1282. void anon_vma_interval_tree_remove(struct anon_vma_chain *node,
  1283. struct rb_root *root);
  1284. struct anon_vma_chain *anon_vma_interval_tree_iter_first(
  1285. struct rb_root *root, unsigned long start, unsigned long last);
  1286. struct anon_vma_chain *anon_vma_interval_tree_iter_next(
  1287. struct anon_vma_chain *node, unsigned long start, unsigned long last);
  1288. #ifdef CONFIG_DEBUG_VM_RB
  1289. void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
  1290. #endif
  1291. #define anon_vma_interval_tree_foreach(avc, root, start, last) \
  1292. for (avc = anon_vma_interval_tree_iter_first(root, start, last); \
  1293. avc; avc = anon_vma_interval_tree_iter_next(avc, start, last))
  1294. /* mmap.c */
  1295. extern int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin);
  1296. extern int vma_adjust(struct vm_area_struct *vma, unsigned long start,
  1297. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert);
  1298. extern struct vm_area_struct *vma_merge(struct mm_struct *,
  1299. struct vm_area_struct *prev, unsigned long addr, unsigned long end,
  1300. unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
  1301. struct mempolicy *);
  1302. extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
  1303. extern int split_vma(struct mm_struct *,
  1304. struct vm_area_struct *, unsigned long addr, int new_below);
  1305. extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
  1306. extern void __vma_link_rb(struct mm_struct *, struct vm_area_struct *,
  1307. struct rb_node **, struct rb_node *);
  1308. extern void unlink_file_vma(struct vm_area_struct *);
  1309. extern struct vm_area_struct *copy_vma(struct vm_area_struct **,
  1310. unsigned long addr, unsigned long len, pgoff_t pgoff,
  1311. bool *need_rmap_locks);
  1312. extern void exit_mmap(struct mm_struct *);
  1313. extern int mm_take_all_locks(struct mm_struct *mm);
  1314. extern void mm_drop_all_locks(struct mm_struct *mm);
  1315. extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
  1316. extern struct file *get_mm_exe_file(struct mm_struct *mm);
  1317. extern int may_expand_vm(struct mm_struct *mm, unsigned long npages);
  1318. extern int install_special_mapping(struct mm_struct *mm,
  1319. unsigned long addr, unsigned long len,
  1320. unsigned long flags, struct page **pages);
  1321. extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  1322. extern unsigned long mmap_region(struct file *file, unsigned long addr,
  1323. unsigned long len, vm_flags_t vm_flags, unsigned long pgoff);
  1324. extern unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
  1325. unsigned long len, unsigned long prot, unsigned long flags,
  1326. unsigned long pgoff, unsigned long *populate);
  1327. extern int do_munmap(struct mm_struct *, unsigned long, size_t);
  1328. #ifdef CONFIG_MMU
  1329. extern int __mm_populate(unsigned long addr, unsigned long len,
  1330. int ignore_errors);
  1331. static inline void mm_populate(unsigned long addr, unsigned long len)
  1332. {
  1333. /* Ignore errors */
  1334. (void) __mm_populate(addr, len, 1);
  1335. }
  1336. #else
  1337. static inline void mm_populate(unsigned long addr, unsigned long len) {}
  1338. #endif
  1339. /* These take the mm semaphore themselves */
  1340. extern unsigned long vm_brk(unsigned long, unsigned long);
  1341. extern int vm_munmap(unsigned long, size_t);
  1342. extern unsigned long vm_mmap(struct file *, unsigned long,
  1343. unsigned long, unsigned long,
  1344. unsigned long, unsigned long);
  1345. struct vm_unmapped_area_info {
  1346. #define VM_UNMAPPED_AREA_TOPDOWN 1
  1347. unsigned long flags;
  1348. unsigned long length;
  1349. unsigned long low_limit;
  1350. unsigned long high_limit;
  1351. unsigned long align_mask;
  1352. unsigned long align_offset;
  1353. };
  1354. extern unsigned long unmapped_area(struct vm_unmapped_area_info *info);
  1355. extern unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info);
  1356. /*
  1357. * Search for an unmapped address range.
  1358. *
  1359. * We are looking for a range that:
  1360. * - does not intersect with any VMA;
  1361. * - is contained within the [low_limit, high_limit) interval;
  1362. * - is at least the desired size.
  1363. * - satisfies (begin_addr & align_mask) == (align_offset & align_mask)
  1364. */
  1365. static inline unsigned long
  1366. vm_unmapped_area(struct vm_unmapped_area_info *info)
  1367. {
  1368. if (!(info->flags & VM_UNMAPPED_AREA_TOPDOWN))
  1369. return unmapped_area(info);
  1370. else
  1371. return unmapped_area_topdown(info);
  1372. }
  1373. /* truncate.c */
  1374. extern void truncate_inode_pages(struct address_space *, loff_t);
  1375. extern void truncate_inode_pages_range(struct address_space *,
  1376. loff_t lstart, loff_t lend);
  1377. /* generic vm_area_ops exported for stackable file systems */
  1378. extern int filemap_fault(struct vm_area_struct *, struct vm_fault *);
  1379. extern int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf);
  1380. /* mm/page-writeback.c */
  1381. int write_one_page(struct page *page, int wait);
  1382. void task_dirty_inc(struct task_struct *tsk);
  1383. /* readahead.c */
  1384. #define VM_MAX_READAHEAD 128 /* kbytes */
  1385. #define VM_MIN_READAHEAD 16 /* kbytes (includes current page) */
  1386. int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
  1387. pgoff_t offset, unsigned long nr_to_read);
  1388. void page_cache_sync_readahead(struct address_space *mapping,
  1389. struct file_ra_state *ra,
  1390. struct file *filp,
  1391. pgoff_t offset,
  1392. unsigned long size);
  1393. void page_cache_async_readahead(struct address_space *mapping,
  1394. struct file_ra_state *ra,
  1395. struct file *filp,
  1396. struct page *pg,
  1397. pgoff_t offset,
  1398. unsigned long size);
  1399. unsigned long max_sane_readahead(unsigned long nr);
  1400. unsigned long ra_submit(struct file_ra_state *ra,
  1401. struct address_space *mapping,
  1402. struct file *filp);
  1403. /* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */
  1404. extern int expand_stack(struct vm_area_struct *vma, unsigned long address);
  1405. /* CONFIG_STACK_GROWSUP still needs to to grow downwards at some places */
  1406. extern int expand_downwards(struct vm_area_struct *vma,
  1407. unsigned long address);
  1408. #if VM_GROWSUP
  1409. extern int expand_upwards(struct vm_area_struct *vma, unsigned long address);
  1410. #else
  1411. #define expand_upwards(vma, address) do { } while (0)
  1412. #endif
  1413. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1414. extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
  1415. extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
  1416. struct vm_area_struct **pprev);
  1417. /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
  1418. NULL if none. Assume start_addr < end_addr. */
  1419. static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
  1420. {
  1421. struct vm_area_struct * vma = find_vma(mm,start_addr);
  1422. if (vma && end_addr <= vma->vm_start)
  1423. vma = NULL;
  1424. return vma;
  1425. }
  1426. static inline unsigned long vma_pages(struct vm_area_struct *vma)
  1427. {
  1428. return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  1429. }
  1430. /* Look up the first VMA which exactly match the interval vm_start ... vm_end */
  1431. static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,
  1432. unsigned long vm_start, unsigned long vm_end)
  1433. {
  1434. struct vm_area_struct *vma = find_vma(mm, vm_start);
  1435. if (vma && (vma->vm_start != vm_start || vma->vm_end != vm_end))
  1436. vma = NULL;
  1437. return vma;
  1438. }
  1439. #ifdef CONFIG_MMU
  1440. pgprot_t vm_get_page_prot(unsigned long vm_flags);
  1441. #else
  1442. static inline pgprot_t vm_get_page_prot(unsigned long vm_flags)
  1443. {
  1444. return __pgprot(0);
  1445. }
  1446. #endif
  1447. #ifdef CONFIG_ARCH_USES_NUMA_PROT_NONE
  1448. unsigned long change_prot_numa(struct vm_area_struct *vma,
  1449. unsigned long start, unsigned long end);
  1450. #endif
  1451. struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr);
  1452. int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
  1453. unsigned long pfn, unsigned long size, pgprot_t);
  1454. int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *);
  1455. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1456. unsigned long pfn);
  1457. int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
  1458. unsigned long pfn);
  1459. int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len);
  1460. struct page *follow_page_mask(struct vm_area_struct *vma,
  1461. unsigned long address, unsigned int foll_flags,
  1462. unsigned int *page_mask);
  1463. static inline struct page *follow_page(struct vm_area_struct *vma,
  1464. unsigned long address, unsigned int foll_flags)
  1465. {
  1466. unsigned int unused_page_mask;
  1467. return follow_page_mask(vma, address, foll_flags, &unused_page_mask);
  1468. }
  1469. #define FOLL_WRITE 0x01 /* check pte is writable */
  1470. #define FOLL_TOUCH 0x02 /* mark page accessed */
  1471. #define FOLL_GET 0x04 /* do get_page on page */
  1472. #define FOLL_DUMP 0x08 /* give error on hole if it would be zero */
  1473. #define FOLL_FORCE 0x10 /* get_user_pages read/write w/o permission */
  1474. #define FOLL_NOWAIT 0x20 /* if a disk transfer is needed, start the IO
  1475. * and return without waiting upon it */
  1476. #define FOLL_MLOCK 0x40 /* mark page as mlocked */
  1477. #define FOLL_SPLIT 0x80 /* don't return transhuge pages, split them */
  1478. #define FOLL_HWPOISON 0x100 /* check page is hwpoisoned */
  1479. #define FOLL_NUMA 0x200 /* force NUMA hinting page fault */
  1480. #define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */
  1481. typedef int (*pte_fn_t)(pte_t *pte, pgtable_t token, unsigned long addr,
  1482. void *data);
  1483. extern int apply_to_page_range(struct mm_struct *mm, unsigned long address,
  1484. unsigned long size, pte_fn_t fn, void *data);
  1485. #ifdef CONFIG_PROC_FS
  1486. void vm_stat_account(struct mm_struct *, unsigned long, struct file *, long);
  1487. #else
  1488. static inline void vm_stat_account(struct mm_struct *mm,
  1489. unsigned long flags, struct file *file, long pages)
  1490. {
  1491. mm->total_vm += pages;
  1492. }
  1493. #endif /* CONFIG_PROC_FS */
  1494. #ifdef CONFIG_DEBUG_PAGEALLOC
  1495. extern void kernel_map_pages(struct page *page, int numpages, int enable);
  1496. #ifdef CONFIG_HIBERNATION
  1497. extern bool kernel_page_present(struct page *page);
  1498. #endif /* CONFIG_HIBERNATION */
  1499. #else
  1500. static inline void
  1501. kernel_map_pages(struct page *page, int numpages, int enable) {}
  1502. #ifdef CONFIG_HIBERNATION
  1503. static inline bool kernel_page_present(struct page *page) { return true; }
  1504. #endif /* CONFIG_HIBERNATION */
  1505. #endif
  1506. extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
  1507. #ifdef __HAVE_ARCH_GATE_AREA
  1508. int in_gate_area_no_mm(unsigned long addr);
  1509. int in_gate_area(struct mm_struct *mm, unsigned long addr);
  1510. #else
  1511. int in_gate_area_no_mm(unsigned long addr);
  1512. #define in_gate_area(mm, addr) ({(void)mm; in_gate_area_no_mm(addr);})
  1513. #endif /* __HAVE_ARCH_GATE_AREA */
  1514. #ifdef CONFIG_SYSCTL
  1515. extern int sysctl_drop_caches;
  1516. int drop_caches_sysctl_handler(struct ctl_table *, int,
  1517. void __user *, size_t *, loff_t *);
  1518. #endif
  1519. unsigned long shrink_slab(struct shrink_control *shrink,
  1520. unsigned long nr_pages_scanned,
  1521. unsigned long lru_pages);
  1522. #ifndef CONFIG_MMU
  1523. #define randomize_va_space 0
  1524. #else
  1525. extern int randomize_va_space;
  1526. #endif
  1527. const char * arch_vma_name(struct vm_area_struct *vma);
  1528. void print_vma_addr(char *prefix, unsigned long rip);
  1529. void sparse_mem_maps_populate_node(struct page **map_map,
  1530. unsigned long pnum_begin,
  1531. unsigned long pnum_end,
  1532. unsigned long map_count,
  1533. int nodeid);
  1534. struct page *sparse_mem_map_populate(unsigned long pnum, int nid);
  1535. pgd_t *vmemmap_pgd_populate(unsigned long addr, int node);
  1536. pud_t *vmemmap_pud_populate(pgd_t *pgd, unsigned long addr, int node);
  1537. pmd_t *vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node);
  1538. pte_t *vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node);
  1539. void *vmemmap_alloc_block(unsigned long size, int node);
  1540. void *vmemmap_alloc_block_buf(unsigned long size, int node);
  1541. void vmemmap_verify(pte_t *, int, unsigned long, unsigned long);
  1542. int vmemmap_populate_basepages(unsigned long start, unsigned long end,
  1543. int node);
  1544. int vmemmap_populate(unsigned long start, unsigned long end, int node);
  1545. void vmemmap_populate_print_last(void);
  1546. #ifdef CONFIG_MEMORY_HOTPLUG
  1547. void vmemmap_free(unsigned long start, unsigned long end);
  1548. #endif
  1549. void register_page_bootmem_memmap(unsigned long section_nr, struct page *map,
  1550. unsigned long size);
  1551. enum mf_flags {
  1552. MF_COUNT_INCREASED = 1 << 0,
  1553. MF_ACTION_REQUIRED = 1 << 1,
  1554. MF_MUST_KILL = 1 << 2,
  1555. MF_SOFT_OFFLINE = 1 << 3,
  1556. };
  1557. extern int memory_failure(unsigned long pfn, int trapno, int flags);
  1558. extern void memory_failure_queue(unsigned long pfn, int trapno, int flags);
  1559. extern int unpoison_memory(unsigned long pfn);
  1560. extern int sysctl_memory_failure_early_kill;
  1561. extern int sysctl_memory_failure_recovery;
  1562. extern void shake_page(struct page *p, int access);
  1563. extern atomic_long_t num_poisoned_pages;
  1564. extern int soft_offline_page(struct page *page, int flags);
  1565. extern void dump_page(struct page *page);
  1566. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  1567. extern void clear_huge_page(struct page *page,
  1568. unsigned long addr,
  1569. unsigned int pages_per_huge_page);
  1570. extern void copy_user_huge_page(struct page *dst, struct page *src,
  1571. unsigned long addr, struct vm_area_struct *vma,
  1572. unsigned int pages_per_huge_page);
  1573. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
  1574. #ifdef CONFIG_DEBUG_PAGEALLOC
  1575. extern unsigned int _debug_guardpage_minorder;
  1576. static inline unsigned int debug_guardpage_minorder(void)
  1577. {
  1578. return _debug_guardpage_minorder;
  1579. }
  1580. static inline bool page_is_guard(struct page *page)
  1581. {
  1582. return test_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
  1583. }
  1584. #else
  1585. static inline unsigned int debug_guardpage_minorder(void) { return 0; }
  1586. static inline bool page_is_guard(struct page *page) { return false; }
  1587. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1588. #if MAX_NUMNODES > 1
  1589. void __init setup_nr_node_ids(void);
  1590. #else
  1591. static inline void setup_nr_node_ids(void) {}
  1592. #endif
  1593. #endif /* __KERNEL__ */
  1594. #endif /* _LINUX_MM_H */