mm.h 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  1. #ifndef _LINUX_MM_H
  2. #define _LINUX_MM_H
  3. #include <linux/errno.h>
  4. #ifdef __KERNEL__
  5. #include <linux/mmdebug.h>
  6. #include <linux/gfp.h>
  7. #include <linux/bug.h>
  8. #include <linux/list.h>
  9. #include <linux/mmzone.h>
  10. #include <linux/rbtree.h>
  11. #include <linux/atomic.h>
  12. #include <linux/debug_locks.h>
  13. #include <linux/mm_types.h>
  14. #include <linux/range.h>
  15. #include <linux/pfn.h>
  16. #include <linux/percpu-refcount.h>
  17. #include <linux/bit_spinlock.h>
  18. #include <linux/shrinker.h>
  19. #include <linux/resource.h>
  20. #include <linux/page_ext.h>
  21. #include <linux/err.h>
  22. #include <linux/page_ref.h>
  23. struct mempolicy;
  24. struct anon_vma;
  25. struct anon_vma_chain;
  26. struct file_ra_state;
  27. struct user_struct;
  28. struct writeback_control;
  29. struct bdi_writeback;
  30. void init_mm_internals(void);
  31. #ifndef CONFIG_NEED_MULTIPLE_NODES /* Don't use mapnrs, do it properly */
  32. extern unsigned long max_mapnr;
  33. static inline void set_max_mapnr(unsigned long limit)
  34. {
  35. max_mapnr = limit;
  36. }
  37. #else
  38. static inline void set_max_mapnr(unsigned long limit) { }
  39. #endif
  40. extern unsigned long totalram_pages;
  41. extern void * high_memory;
  42. extern int page_cluster;
  43. #ifdef CONFIG_SYSCTL
  44. extern int sysctl_legacy_va_layout;
  45. #else
  46. #define sysctl_legacy_va_layout 0
  47. #endif
  48. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
  49. extern const int mmap_rnd_bits_min;
  50. extern const int mmap_rnd_bits_max;
  51. extern int mmap_rnd_bits __read_mostly;
  52. #endif
  53. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
  54. extern const int mmap_rnd_compat_bits_min;
  55. extern const int mmap_rnd_compat_bits_max;
  56. extern int mmap_rnd_compat_bits __read_mostly;
  57. #endif
  58. #include <asm/page.h>
  59. #include <asm/pgtable.h>
  60. #include <asm/processor.h>
  61. #ifndef __pa_symbol
  62. #define __pa_symbol(x) __pa(RELOC_HIDE((unsigned long)(x), 0))
  63. #endif
  64. #ifndef page_to_virt
  65. #define page_to_virt(x) __va(PFN_PHYS(page_to_pfn(x)))
  66. #endif
  67. #ifndef lm_alias
  68. #define lm_alias(x) __va(__pa_symbol(x))
  69. #endif
  70. /*
  71. * To prevent common memory management code establishing
  72. * a zero page mapping on a read fault.
  73. * This macro should be defined within <asm/pgtable.h>.
  74. * s390 does this to prevent multiplexing of hardware bits
  75. * related to the physical page in case of virtualization.
  76. */
  77. #ifndef mm_forbids_zeropage
  78. #define mm_forbids_zeropage(X) (0)
  79. #endif
  80. /*
  81. * Default maximum number of active map areas, this limits the number of vmas
  82. * per mm struct. Users can overwrite this number by sysctl but there is a
  83. * problem.
  84. *
  85. * When a program's coredump is generated as ELF format, a section is created
  86. * per a vma. In ELF, the number of sections is represented in unsigned short.
  87. * This means the number of sections should be smaller than 65535 at coredump.
  88. * Because the kernel adds some informative sections to a image of program at
  89. * generating coredump, we need some margin. The number of extra sections is
  90. * 1-3 now and depends on arch. We use "5" as safe margin, here.
  91. *
  92. * ELF extended numbering allows more than 65535 sections, so 16-bit bound is
  93. * not a hard limit any more. Although some userspace tools can be surprised by
  94. * that.
  95. */
  96. #define MAPCOUNT_ELF_CORE_MARGIN (5)
  97. #define DEFAULT_MAX_MAP_COUNT (USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN)
  98. extern int sysctl_max_map_count;
  99. extern unsigned long sysctl_user_reserve_kbytes;
  100. extern unsigned long sysctl_admin_reserve_kbytes;
  101. extern int sysctl_overcommit_memory;
  102. extern int sysctl_overcommit_ratio;
  103. extern unsigned long sysctl_overcommit_kbytes;
  104. extern int overcommit_ratio_handler(struct ctl_table *, int, void __user *,
  105. size_t *, loff_t *);
  106. extern int overcommit_kbytes_handler(struct ctl_table *, int, void __user *,
  107. size_t *, loff_t *);
  108. #define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
  109. /* to align the pointer to the (next) page boundary */
  110. #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
  111. /* test whether an address (unsigned long or pointer) is aligned to PAGE_SIZE */
  112. #define PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr), PAGE_SIZE)
  113. /*
  114. * Linux kernel virtual memory manager primitives.
  115. * The idea being to have a "virtual" mm in the same way
  116. * we have a virtual fs - giving a cleaner interface to the
  117. * mm details, and allowing different kinds of memory mappings
  118. * (from shared memory to executable loading to arbitrary
  119. * mmap() functions).
  120. */
  121. extern struct kmem_cache *vm_area_cachep;
  122. #ifndef CONFIG_MMU
  123. extern struct rb_root nommu_region_tree;
  124. extern struct rw_semaphore nommu_region_sem;
  125. extern unsigned int kobjsize(const void *objp);
  126. #endif
  127. /*
  128. * vm_flags in vm_area_struct, see mm_types.h.
  129. * When changing, update also include/trace/events/mmflags.h
  130. */
  131. #define VM_NONE 0x00000000
  132. #define VM_READ 0x00000001 /* currently active flags */
  133. #define VM_WRITE 0x00000002
  134. #define VM_EXEC 0x00000004
  135. #define VM_SHARED 0x00000008
  136. /* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
  137. #define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
  138. #define VM_MAYWRITE 0x00000020
  139. #define VM_MAYEXEC 0x00000040
  140. #define VM_MAYSHARE 0x00000080
  141. #define VM_GROWSDOWN 0x00000100 /* general info on the segment */
  142. #define VM_UFFD_MISSING 0x00000200 /* missing pages tracking */
  143. #define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */
  144. #define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */
  145. #define VM_UFFD_WP 0x00001000 /* wrprotect pages tracking */
  146. #define VM_LOCKED 0x00002000
  147. #define VM_IO 0x00004000 /* Memory mapped I/O or similar */
  148. /* Used by sys_madvise() */
  149. #define VM_SEQ_READ 0x00008000 /* App will access data sequentially */
  150. #define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */
  151. #define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */
  152. #define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */
  153. #define VM_LOCKONFAULT 0x00080000 /* Lock the pages covered when they are faulted in */
  154. #define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */
  155. #define VM_NORESERVE 0x00200000 /* should the VM suppress accounting */
  156. #define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */
  157. #define VM_ARCH_1 0x01000000 /* Architecture-specific flag */
  158. #define VM_ARCH_2 0x02000000
  159. #define VM_DONTDUMP 0x04000000 /* Do not include in the core dump */
  160. #ifdef CONFIG_MEM_SOFT_DIRTY
  161. # define VM_SOFTDIRTY 0x08000000 /* Not soft dirty clean area */
  162. #else
  163. # define VM_SOFTDIRTY 0
  164. #endif
  165. #define VM_MIXEDMAP 0x10000000 /* Can contain "struct page" and pure PFN pages */
  166. #define VM_HUGEPAGE 0x20000000 /* MADV_HUGEPAGE marked this vma */
  167. #define VM_NOHUGEPAGE 0x40000000 /* MADV_NOHUGEPAGE marked this vma */
  168. #define VM_MERGEABLE 0x80000000 /* KSM may merge identical pages */
  169. #ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS
  170. #define VM_HIGH_ARCH_BIT_0 32 /* bit only usable on 64-bit architectures */
  171. #define VM_HIGH_ARCH_BIT_1 33 /* bit only usable on 64-bit architectures */
  172. #define VM_HIGH_ARCH_BIT_2 34 /* bit only usable on 64-bit architectures */
  173. #define VM_HIGH_ARCH_BIT_3 35 /* bit only usable on 64-bit architectures */
  174. #define VM_HIGH_ARCH_BIT_4 36 /* bit only usable on 64-bit architectures */
  175. #define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0)
  176. #define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1)
  177. #define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2)
  178. #define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3)
  179. #define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4)
  180. #endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */
  181. #if defined(CONFIG_X86)
  182. # define VM_PAT VM_ARCH_1 /* PAT reserves whole VMA at once (x86) */
  183. #if defined (CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS)
  184. # define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0
  185. # define VM_PKEY_BIT0 VM_HIGH_ARCH_0 /* A protection key is a 4-bit value */
  186. # define VM_PKEY_BIT1 VM_HIGH_ARCH_1
  187. # define VM_PKEY_BIT2 VM_HIGH_ARCH_2
  188. # define VM_PKEY_BIT3 VM_HIGH_ARCH_3
  189. #endif
  190. #elif defined(CONFIG_PPC)
  191. # define VM_SAO VM_ARCH_1 /* Strong Access Ordering (powerpc) */
  192. #elif defined(CONFIG_PARISC)
  193. # define VM_GROWSUP VM_ARCH_1
  194. #elif defined(CONFIG_METAG)
  195. # define VM_GROWSUP VM_ARCH_1
  196. #elif defined(CONFIG_IA64)
  197. # define VM_GROWSUP VM_ARCH_1
  198. #elif !defined(CONFIG_MMU)
  199. # define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */
  200. #endif
  201. #if defined(CONFIG_X86_INTEL_MPX)
  202. /* MPX specific bounds table or bounds directory */
  203. # define VM_MPX VM_HIGH_ARCH_BIT_4
  204. #else
  205. # define VM_MPX VM_NONE
  206. #endif
  207. #ifndef VM_GROWSUP
  208. # define VM_GROWSUP VM_NONE
  209. #endif
  210. /* Bits set in the VMA until the stack is in its final location */
  211. #define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ)
  212. #ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */
  213. #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
  214. #endif
  215. #ifdef CONFIG_STACK_GROWSUP
  216. #define VM_STACK VM_GROWSUP
  217. #else
  218. #define VM_STACK VM_GROWSDOWN
  219. #endif
  220. #define VM_STACK_FLAGS (VM_STACK | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
  221. /*
  222. * Special vmas that are non-mergable, non-mlock()able.
  223. * Note: mm/huge_memory.c VM_NO_THP depends on this definition.
  224. */
  225. #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP)
  226. /* This mask defines which mm->def_flags a process can inherit its parent */
  227. #define VM_INIT_DEF_MASK VM_NOHUGEPAGE
  228. /* This mask is used to clear all the VMA flags used by mlock */
  229. #define VM_LOCKED_CLEAR_MASK (~(VM_LOCKED | VM_LOCKONFAULT))
  230. /*
  231. * mapping from the currently active vm_flags protection bits (the
  232. * low four bits) to a page protection mask..
  233. */
  234. extern pgprot_t protection_map[16];
  235. #define FAULT_FLAG_WRITE 0x01 /* Fault was a write access */
  236. #define FAULT_FLAG_MKWRITE 0x02 /* Fault was mkwrite of existing pte */
  237. #define FAULT_FLAG_ALLOW_RETRY 0x04 /* Retry fault if blocking */
  238. #define FAULT_FLAG_RETRY_NOWAIT 0x08 /* Don't drop mmap_sem and wait when retrying */
  239. #define FAULT_FLAG_KILLABLE 0x10 /* The fault task is in SIGKILL killable region */
  240. #define FAULT_FLAG_TRIED 0x20 /* Second try */
  241. #define FAULT_FLAG_USER 0x40 /* The fault originated in userspace */
  242. #define FAULT_FLAG_REMOTE 0x80 /* faulting for non current tsk/mm */
  243. #define FAULT_FLAG_INSTRUCTION 0x100 /* The fault was during an instruction fetch */
  244. #define FAULT_FLAG_TRACE \
  245. { FAULT_FLAG_WRITE, "WRITE" }, \
  246. { FAULT_FLAG_MKWRITE, "MKWRITE" }, \
  247. { FAULT_FLAG_ALLOW_RETRY, "ALLOW_RETRY" }, \
  248. { FAULT_FLAG_RETRY_NOWAIT, "RETRY_NOWAIT" }, \
  249. { FAULT_FLAG_KILLABLE, "KILLABLE" }, \
  250. { FAULT_FLAG_TRIED, "TRIED" }, \
  251. { FAULT_FLAG_USER, "USER" }, \
  252. { FAULT_FLAG_REMOTE, "REMOTE" }, \
  253. { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }
  254. /*
  255. * vm_fault is filled by the the pagefault handler and passed to the vma's
  256. * ->fault function. The vma's ->fault is responsible for returning a bitmask
  257. * of VM_FAULT_xxx flags that give details about how the fault was handled.
  258. *
  259. * MM layer fills up gfp_mask for page allocations but fault handler might
  260. * alter it if its implementation requires a different allocation context.
  261. *
  262. * pgoff should be used in favour of virtual_address, if possible.
  263. */
  264. struct vm_fault {
  265. struct vm_area_struct *vma; /* Target VMA */
  266. unsigned int flags; /* FAULT_FLAG_xxx flags */
  267. gfp_t gfp_mask; /* gfp mask to be used for allocations */
  268. pgoff_t pgoff; /* Logical page offset based on vma */
  269. unsigned long address; /* Faulting virtual address */
  270. pmd_t *pmd; /* Pointer to pmd entry matching
  271. * the 'address' */
  272. pud_t *pud; /* Pointer to pud entry matching
  273. * the 'address'
  274. */
  275. pte_t orig_pte; /* Value of PTE at the time of fault */
  276. struct page *cow_page; /* Page handler may use for COW fault */
  277. struct mem_cgroup *memcg; /* Cgroup cow_page belongs to */
  278. struct page *page; /* ->fault handlers should return a
  279. * page here, unless VM_FAULT_NOPAGE
  280. * is set (which is also implied by
  281. * VM_FAULT_ERROR).
  282. */
  283. /* These three entries are valid only while holding ptl lock */
  284. pte_t *pte; /* Pointer to pte entry matching
  285. * the 'address'. NULL if the page
  286. * table hasn't been allocated.
  287. */
  288. spinlock_t *ptl; /* Page table lock.
  289. * Protects pte page table if 'pte'
  290. * is not NULL, otherwise pmd.
  291. */
  292. pgtable_t prealloc_pte; /* Pre-allocated pte page table.
  293. * vm_ops->map_pages() calls
  294. * alloc_set_pte() from atomic context.
  295. * do_fault_around() pre-allocates
  296. * page table to avoid allocation from
  297. * atomic context.
  298. */
  299. };
  300. /* page entry size for vm->huge_fault() */
  301. enum page_entry_size {
  302. PE_SIZE_PTE = 0,
  303. PE_SIZE_PMD,
  304. PE_SIZE_PUD,
  305. };
  306. /*
  307. * These are the virtual MM functions - opening of an area, closing and
  308. * unmapping it (needed to keep files on disk up-to-date etc), pointer
  309. * to the functions called when a no-page or a wp-page exception occurs.
  310. */
  311. struct vm_operations_struct {
  312. void (*open)(struct vm_area_struct * area);
  313. void (*close)(struct vm_area_struct * area);
  314. int (*mremap)(struct vm_area_struct * area);
  315. int (*fault)(struct vm_fault *vmf);
  316. int (*huge_fault)(struct vm_fault *vmf, enum page_entry_size pe_size);
  317. void (*map_pages)(struct vm_fault *vmf,
  318. pgoff_t start_pgoff, pgoff_t end_pgoff);
  319. /* notification that a previously read-only page is about to become
  320. * writable, if an error is returned it will cause a SIGBUS */
  321. int (*page_mkwrite)(struct vm_fault *vmf);
  322. /* same as page_mkwrite when using VM_PFNMAP|VM_MIXEDMAP */
  323. int (*pfn_mkwrite)(struct vm_fault *vmf);
  324. /* called by access_process_vm when get_user_pages() fails, typically
  325. * for use by special VMAs that can switch between memory and hardware
  326. */
  327. int (*access)(struct vm_area_struct *vma, unsigned long addr,
  328. void *buf, int len, int write);
  329. /* Called by the /proc/PID/maps code to ask the vma whether it
  330. * has a special name. Returning non-NULL will also cause this
  331. * vma to be dumped unconditionally. */
  332. const char *(*name)(struct vm_area_struct *vma);
  333. #ifdef CONFIG_NUMA
  334. /*
  335. * set_policy() op must add a reference to any non-NULL @new mempolicy
  336. * to hold the policy upon return. Caller should pass NULL @new to
  337. * remove a policy and fall back to surrounding context--i.e. do not
  338. * install a MPOL_DEFAULT policy, nor the task or system default
  339. * mempolicy.
  340. */
  341. int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
  342. /*
  343. * get_policy() op must add reference [mpol_get()] to any policy at
  344. * (vma,addr) marked as MPOL_SHARED. The shared policy infrastructure
  345. * in mm/mempolicy.c will do this automatically.
  346. * get_policy() must NOT add a ref if the policy at (vma,addr) is not
  347. * marked as MPOL_SHARED. vma policies are protected by the mmap_sem.
  348. * If no [shared/vma] mempolicy exists at the addr, get_policy() op
  349. * must return NULL--i.e., do not "fallback" to task or system default
  350. * policy.
  351. */
  352. struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
  353. unsigned long addr);
  354. #endif
  355. /*
  356. * Called by vm_normal_page() for special PTEs to find the
  357. * page for @addr. This is useful if the default behavior
  358. * (using pte_page()) would not find the correct page.
  359. */
  360. struct page *(*find_special_page)(struct vm_area_struct *vma,
  361. unsigned long addr);
  362. };
  363. struct mmu_gather;
  364. struct inode;
  365. #define page_private(page) ((page)->private)
  366. #define set_page_private(page, v) ((page)->private = (v))
  367. #if !defined(__HAVE_ARCH_PTE_DEVMAP) || !defined(CONFIG_TRANSPARENT_HUGEPAGE)
  368. static inline int pmd_devmap(pmd_t pmd)
  369. {
  370. return 0;
  371. }
  372. static inline int pud_devmap(pud_t pud)
  373. {
  374. return 0;
  375. }
  376. static inline int pgd_devmap(pgd_t pgd)
  377. {
  378. return 0;
  379. }
  380. #endif
  381. /*
  382. * FIXME: take this include out, include page-flags.h in
  383. * files which need it (119 of them)
  384. */
  385. #include <linux/page-flags.h>
  386. #include <linux/huge_mm.h>
  387. /*
  388. * Methods to modify the page usage count.
  389. *
  390. * What counts for a page usage:
  391. * - cache mapping (page->mapping)
  392. * - private data (page->private)
  393. * - page mapped in a task's page tables, each mapping
  394. * is counted separately
  395. *
  396. * Also, many kernel routines increase the page count before a critical
  397. * routine so they can be sure the page doesn't go away from under them.
  398. */
  399. /*
  400. * Drop a ref, return true if the refcount fell to zero (the page has no users)
  401. */
  402. static inline int put_page_testzero(struct page *page)
  403. {
  404. VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
  405. return page_ref_dec_and_test(page);
  406. }
  407. /*
  408. * Try to grab a ref unless the page has a refcount of zero, return false if
  409. * that is the case.
  410. * This can be called when MMU is off so it must not access
  411. * any of the virtual mappings.
  412. */
  413. static inline int get_page_unless_zero(struct page *page)
  414. {
  415. return page_ref_add_unless(page, 1, 0);
  416. }
  417. extern int page_is_ram(unsigned long pfn);
  418. enum {
  419. REGION_INTERSECTS,
  420. REGION_DISJOINT,
  421. REGION_MIXED,
  422. };
  423. int region_intersects(resource_size_t offset, size_t size, unsigned long flags,
  424. unsigned long desc);
  425. /* Support for virtually mapped pages */
  426. struct page *vmalloc_to_page(const void *addr);
  427. unsigned long vmalloc_to_pfn(const void *addr);
  428. /*
  429. * Determine if an address is within the vmalloc range
  430. *
  431. * On nommu, vmalloc/vfree wrap through kmalloc/kfree directly, so there
  432. * is no special casing required.
  433. */
  434. static inline bool is_vmalloc_addr(const void *x)
  435. {
  436. #ifdef CONFIG_MMU
  437. unsigned long addr = (unsigned long)x;
  438. return addr >= VMALLOC_START && addr < VMALLOC_END;
  439. #else
  440. return false;
  441. #endif
  442. }
  443. #ifdef CONFIG_MMU
  444. extern int is_vmalloc_or_module_addr(const void *x);
  445. #else
  446. static inline int is_vmalloc_or_module_addr(const void *x)
  447. {
  448. return 0;
  449. }
  450. #endif
  451. extern void *kvmalloc_node(size_t size, gfp_t flags, int node);
  452. static inline void *kvmalloc(size_t size, gfp_t flags)
  453. {
  454. return kvmalloc_node(size, flags, NUMA_NO_NODE);
  455. }
  456. static inline void *kvzalloc_node(size_t size, gfp_t flags, int node)
  457. {
  458. return kvmalloc_node(size, flags | __GFP_ZERO, node);
  459. }
  460. static inline void *kvzalloc(size_t size, gfp_t flags)
  461. {
  462. return kvmalloc(size, flags | __GFP_ZERO);
  463. }
  464. static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags)
  465. {
  466. if (size != 0 && n > SIZE_MAX / size)
  467. return NULL;
  468. return kvmalloc(n * size, flags);
  469. }
  470. extern void kvfree(const void *addr);
  471. static inline atomic_t *compound_mapcount_ptr(struct page *page)
  472. {
  473. return &page[1].compound_mapcount;
  474. }
  475. static inline int compound_mapcount(struct page *page)
  476. {
  477. VM_BUG_ON_PAGE(!PageCompound(page), page);
  478. page = compound_head(page);
  479. return atomic_read(compound_mapcount_ptr(page)) + 1;
  480. }
  481. /*
  482. * The atomic page->_mapcount, starts from -1: so that transitions
  483. * both from it and to it can be tracked, using atomic_inc_and_test
  484. * and atomic_add_negative(-1).
  485. */
  486. static inline void page_mapcount_reset(struct page *page)
  487. {
  488. atomic_set(&(page)->_mapcount, -1);
  489. }
  490. int __page_mapcount(struct page *page);
  491. static inline int page_mapcount(struct page *page)
  492. {
  493. VM_BUG_ON_PAGE(PageSlab(page), page);
  494. if (unlikely(PageCompound(page)))
  495. return __page_mapcount(page);
  496. return atomic_read(&page->_mapcount) + 1;
  497. }
  498. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  499. int total_mapcount(struct page *page);
  500. int page_trans_huge_mapcount(struct page *page, int *total_mapcount);
  501. #else
  502. static inline int total_mapcount(struct page *page)
  503. {
  504. return page_mapcount(page);
  505. }
  506. static inline int page_trans_huge_mapcount(struct page *page,
  507. int *total_mapcount)
  508. {
  509. int mapcount = page_mapcount(page);
  510. if (total_mapcount)
  511. *total_mapcount = mapcount;
  512. return mapcount;
  513. }
  514. #endif
  515. static inline struct page *virt_to_head_page(const void *x)
  516. {
  517. struct page *page = virt_to_page(x);
  518. return compound_head(page);
  519. }
  520. void __put_page(struct page *page);
  521. void put_pages_list(struct list_head *pages);
  522. void split_page(struct page *page, unsigned int order);
  523. /*
  524. * Compound pages have a destructor function. Provide a
  525. * prototype for that function and accessor functions.
  526. * These are _only_ valid on the head of a compound page.
  527. */
  528. typedef void compound_page_dtor(struct page *);
  529. /* Keep the enum in sync with compound_page_dtors array in mm/page_alloc.c */
  530. enum compound_dtor_id {
  531. NULL_COMPOUND_DTOR,
  532. COMPOUND_PAGE_DTOR,
  533. #ifdef CONFIG_HUGETLB_PAGE
  534. HUGETLB_PAGE_DTOR,
  535. #endif
  536. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  537. TRANSHUGE_PAGE_DTOR,
  538. #endif
  539. NR_COMPOUND_DTORS,
  540. };
  541. extern compound_page_dtor * const compound_page_dtors[];
  542. static inline void set_compound_page_dtor(struct page *page,
  543. enum compound_dtor_id compound_dtor)
  544. {
  545. VM_BUG_ON_PAGE(compound_dtor >= NR_COMPOUND_DTORS, page);
  546. page[1].compound_dtor = compound_dtor;
  547. }
  548. static inline compound_page_dtor *get_compound_page_dtor(struct page *page)
  549. {
  550. VM_BUG_ON_PAGE(page[1].compound_dtor >= NR_COMPOUND_DTORS, page);
  551. return compound_page_dtors[page[1].compound_dtor];
  552. }
  553. static inline unsigned int compound_order(struct page *page)
  554. {
  555. if (!PageHead(page))
  556. return 0;
  557. return page[1].compound_order;
  558. }
  559. static inline void set_compound_order(struct page *page, unsigned int order)
  560. {
  561. page[1].compound_order = order;
  562. }
  563. void free_compound_page(struct page *page);
  564. #ifdef CONFIG_MMU
  565. /*
  566. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  567. * servicing faults for write access. In the normal case, do always want
  568. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  569. * that do not have writing enabled, when used by access_process_vm.
  570. */
  571. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  572. {
  573. if (likely(vma->vm_flags & VM_WRITE))
  574. pte = pte_mkwrite(pte);
  575. return pte;
  576. }
  577. int alloc_set_pte(struct vm_fault *vmf, struct mem_cgroup *memcg,
  578. struct page *page);
  579. int finish_fault(struct vm_fault *vmf);
  580. int finish_mkwrite_fault(struct vm_fault *vmf);
  581. #endif
  582. /*
  583. * Multiple processes may "see" the same page. E.g. for untouched
  584. * mappings of /dev/null, all processes see the same page full of
  585. * zeroes, and text pages of executables and shared libraries have
  586. * only one copy in memory, at most, normally.
  587. *
  588. * For the non-reserved pages, page_count(page) denotes a reference count.
  589. * page_count() == 0 means the page is free. page->lru is then used for
  590. * freelist management in the buddy allocator.
  591. * page_count() > 0 means the page has been allocated.
  592. *
  593. * Pages are allocated by the slab allocator in order to provide memory
  594. * to kmalloc and kmem_cache_alloc. In this case, the management of the
  595. * page, and the fields in 'struct page' are the responsibility of mm/slab.c
  596. * unless a particular usage is carefully commented. (the responsibility of
  597. * freeing the kmalloc memory is the caller's, of course).
  598. *
  599. * A page may be used by anyone else who does a __get_free_page().
  600. * In this case, page_count still tracks the references, and should only
  601. * be used through the normal accessor functions. The top bits of page->flags
  602. * and page->virtual store page management information, but all other fields
  603. * are unused and could be used privately, carefully. The management of this
  604. * page is the responsibility of the one who allocated it, and those who have
  605. * subsequently been given references to it.
  606. *
  607. * The other pages (we may call them "pagecache pages") are completely
  608. * managed by the Linux memory manager: I/O, buffers, swapping etc.
  609. * The following discussion applies only to them.
  610. *
  611. * A pagecache page contains an opaque `private' member, which belongs to the
  612. * page's address_space. Usually, this is the address of a circular list of
  613. * the page's disk buffers. PG_private must be set to tell the VM to call
  614. * into the filesystem to release these pages.
  615. *
  616. * A page may belong to an inode's memory mapping. In this case, page->mapping
  617. * is the pointer to the inode, and page->index is the file offset of the page,
  618. * in units of PAGE_SIZE.
  619. *
  620. * If pagecache pages are not associated with an inode, they are said to be
  621. * anonymous pages. These may become associated with the swapcache, and in that
  622. * case PG_swapcache is set, and page->private is an offset into the swapcache.
  623. *
  624. * In either case (swapcache or inode backed), the pagecache itself holds one
  625. * reference to the page. Setting PG_private should also increment the
  626. * refcount. The each user mapping also has a reference to the page.
  627. *
  628. * The pagecache pages are stored in a per-mapping radix tree, which is
  629. * rooted at mapping->page_tree, and indexed by offset.
  630. * Where 2.4 and early 2.6 kernels kept dirty/clean pages in per-address_space
  631. * lists, we instead now tag pages as dirty/writeback in the radix tree.
  632. *
  633. * All pagecache pages may be subject to I/O:
  634. * - inode pages may need to be read from disk,
  635. * - inode pages which have been modified and are MAP_SHARED may need
  636. * to be written back to the inode on disk,
  637. * - anonymous pages (including MAP_PRIVATE file mappings) which have been
  638. * modified may need to be swapped out to swap space and (later) to be read
  639. * back into memory.
  640. */
  641. /*
  642. * The zone field is never updated after free_area_init_core()
  643. * sets it, so none of the operations on it need to be atomic.
  644. */
  645. /* Page flags: | [SECTION] | [NODE] | ZONE | [LAST_CPUPID] | ... | FLAGS | */
  646. #define SECTIONS_PGOFF ((sizeof(unsigned long)*8) - SECTIONS_WIDTH)
  647. #define NODES_PGOFF (SECTIONS_PGOFF - NODES_WIDTH)
  648. #define ZONES_PGOFF (NODES_PGOFF - ZONES_WIDTH)
  649. #define LAST_CPUPID_PGOFF (ZONES_PGOFF - LAST_CPUPID_WIDTH)
  650. /*
  651. * Define the bit shifts to access each section. For non-existent
  652. * sections we define the shift as 0; that plus a 0 mask ensures
  653. * the compiler will optimise away reference to them.
  654. */
  655. #define SECTIONS_PGSHIFT (SECTIONS_PGOFF * (SECTIONS_WIDTH != 0))
  656. #define NODES_PGSHIFT (NODES_PGOFF * (NODES_WIDTH != 0))
  657. #define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0))
  658. #define LAST_CPUPID_PGSHIFT (LAST_CPUPID_PGOFF * (LAST_CPUPID_WIDTH != 0))
  659. /* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator */
  660. #ifdef NODE_NOT_IN_PAGE_FLAGS
  661. #define ZONEID_SHIFT (SECTIONS_SHIFT + ZONES_SHIFT)
  662. #define ZONEID_PGOFF ((SECTIONS_PGOFF < ZONES_PGOFF)? \
  663. SECTIONS_PGOFF : ZONES_PGOFF)
  664. #else
  665. #define ZONEID_SHIFT (NODES_SHIFT + ZONES_SHIFT)
  666. #define ZONEID_PGOFF ((NODES_PGOFF < ZONES_PGOFF)? \
  667. NODES_PGOFF : ZONES_PGOFF)
  668. #endif
  669. #define ZONEID_PGSHIFT (ZONEID_PGOFF * (ZONEID_SHIFT != 0))
  670. #if SECTIONS_WIDTH+NODES_WIDTH+ZONES_WIDTH > BITS_PER_LONG - NR_PAGEFLAGS
  671. #error SECTIONS_WIDTH+NODES_WIDTH+ZONES_WIDTH > BITS_PER_LONG - NR_PAGEFLAGS
  672. #endif
  673. #define ZONES_MASK ((1UL << ZONES_WIDTH) - 1)
  674. #define NODES_MASK ((1UL << NODES_WIDTH) - 1)
  675. #define SECTIONS_MASK ((1UL << SECTIONS_WIDTH) - 1)
  676. #define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1)
  677. #define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1)
  678. static inline enum zone_type page_zonenum(const struct page *page)
  679. {
  680. return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK;
  681. }
  682. #ifdef CONFIG_ZONE_DEVICE
  683. static inline bool is_zone_device_page(const struct page *page)
  684. {
  685. return page_zonenum(page) == ZONE_DEVICE;
  686. }
  687. #else
  688. static inline bool is_zone_device_page(const struct page *page)
  689. {
  690. return false;
  691. }
  692. #endif
  693. static inline void get_page(struct page *page)
  694. {
  695. page = compound_head(page);
  696. /*
  697. * Getting a normal page or the head of a compound page
  698. * requires to already have an elevated page->_refcount.
  699. */
  700. VM_BUG_ON_PAGE(page_ref_count(page) <= 0, page);
  701. page_ref_inc(page);
  702. }
  703. static inline void put_page(struct page *page)
  704. {
  705. page = compound_head(page);
  706. if (put_page_testzero(page))
  707. __put_page(page);
  708. }
  709. #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
  710. #define SECTION_IN_PAGE_FLAGS
  711. #endif
  712. /*
  713. * The identification function is mainly used by the buddy allocator for
  714. * determining if two pages could be buddies. We are not really identifying
  715. * the zone since we could be using the section number id if we do not have
  716. * node id available in page flags.
  717. * We only guarantee that it will return the same value for two combinable
  718. * pages in a zone.
  719. */
  720. static inline int page_zone_id(struct page *page)
  721. {
  722. return (page->flags >> ZONEID_PGSHIFT) & ZONEID_MASK;
  723. }
  724. static inline int zone_to_nid(struct zone *zone)
  725. {
  726. #ifdef CONFIG_NUMA
  727. return zone->node;
  728. #else
  729. return 0;
  730. #endif
  731. }
  732. #ifdef NODE_NOT_IN_PAGE_FLAGS
  733. extern int page_to_nid(const struct page *page);
  734. #else
  735. static inline int page_to_nid(const struct page *page)
  736. {
  737. return (page->flags >> NODES_PGSHIFT) & NODES_MASK;
  738. }
  739. #endif
  740. #ifdef CONFIG_NUMA_BALANCING
  741. static inline int cpu_pid_to_cpupid(int cpu, int pid)
  742. {
  743. return ((cpu & LAST__CPU_MASK) << LAST__PID_SHIFT) | (pid & LAST__PID_MASK);
  744. }
  745. static inline int cpupid_to_pid(int cpupid)
  746. {
  747. return cpupid & LAST__PID_MASK;
  748. }
  749. static inline int cpupid_to_cpu(int cpupid)
  750. {
  751. return (cpupid >> LAST__PID_SHIFT) & LAST__CPU_MASK;
  752. }
  753. static inline int cpupid_to_nid(int cpupid)
  754. {
  755. return cpu_to_node(cpupid_to_cpu(cpupid));
  756. }
  757. static inline bool cpupid_pid_unset(int cpupid)
  758. {
  759. return cpupid_to_pid(cpupid) == (-1 & LAST__PID_MASK);
  760. }
  761. static inline bool cpupid_cpu_unset(int cpupid)
  762. {
  763. return cpupid_to_cpu(cpupid) == (-1 & LAST__CPU_MASK);
  764. }
  765. static inline bool __cpupid_match_pid(pid_t task_pid, int cpupid)
  766. {
  767. return (task_pid & LAST__PID_MASK) == cpupid_to_pid(cpupid);
  768. }
  769. #define cpupid_match_pid(task, cpupid) __cpupid_match_pid(task->pid, cpupid)
  770. #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
  771. static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
  772. {
  773. return xchg(&page->_last_cpupid, cpupid & LAST_CPUPID_MASK);
  774. }
  775. static inline int page_cpupid_last(struct page *page)
  776. {
  777. return page->_last_cpupid;
  778. }
  779. static inline void page_cpupid_reset_last(struct page *page)
  780. {
  781. page->_last_cpupid = -1 & LAST_CPUPID_MASK;
  782. }
  783. #else
  784. static inline int page_cpupid_last(struct page *page)
  785. {
  786. return (page->flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
  787. }
  788. extern int page_cpupid_xchg_last(struct page *page, int cpupid);
  789. static inline void page_cpupid_reset_last(struct page *page)
  790. {
  791. page->flags |= LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT;
  792. }
  793. #endif /* LAST_CPUPID_NOT_IN_PAGE_FLAGS */
  794. #else /* !CONFIG_NUMA_BALANCING */
  795. static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
  796. {
  797. return page_to_nid(page); /* XXX */
  798. }
  799. static inline int page_cpupid_last(struct page *page)
  800. {
  801. return page_to_nid(page); /* XXX */
  802. }
  803. static inline int cpupid_to_nid(int cpupid)
  804. {
  805. return -1;
  806. }
  807. static inline int cpupid_to_pid(int cpupid)
  808. {
  809. return -1;
  810. }
  811. static inline int cpupid_to_cpu(int cpupid)
  812. {
  813. return -1;
  814. }
  815. static inline int cpu_pid_to_cpupid(int nid, int pid)
  816. {
  817. return -1;
  818. }
  819. static inline bool cpupid_pid_unset(int cpupid)
  820. {
  821. return 1;
  822. }
  823. static inline void page_cpupid_reset_last(struct page *page)
  824. {
  825. }
  826. static inline bool cpupid_match_pid(struct task_struct *task, int cpupid)
  827. {
  828. return false;
  829. }
  830. #endif /* CONFIG_NUMA_BALANCING */
  831. static inline struct zone *page_zone(const struct page *page)
  832. {
  833. return &NODE_DATA(page_to_nid(page))->node_zones[page_zonenum(page)];
  834. }
  835. static inline pg_data_t *page_pgdat(const struct page *page)
  836. {
  837. return NODE_DATA(page_to_nid(page));
  838. }
  839. #ifdef SECTION_IN_PAGE_FLAGS
  840. static inline void set_page_section(struct page *page, unsigned long section)
  841. {
  842. page->flags &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT);
  843. page->flags |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT;
  844. }
  845. static inline unsigned long page_to_section(const struct page *page)
  846. {
  847. return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
  848. }
  849. #endif
  850. static inline void set_page_zone(struct page *page, enum zone_type zone)
  851. {
  852. page->flags &= ~(ZONES_MASK << ZONES_PGSHIFT);
  853. page->flags |= (zone & ZONES_MASK) << ZONES_PGSHIFT;
  854. }
  855. static inline void set_page_node(struct page *page, unsigned long node)
  856. {
  857. page->flags &= ~(NODES_MASK << NODES_PGSHIFT);
  858. page->flags |= (node & NODES_MASK) << NODES_PGSHIFT;
  859. }
  860. static inline void set_page_links(struct page *page, enum zone_type zone,
  861. unsigned long node, unsigned long pfn)
  862. {
  863. set_page_zone(page, zone);
  864. set_page_node(page, node);
  865. #ifdef SECTION_IN_PAGE_FLAGS
  866. set_page_section(page, pfn_to_section_nr(pfn));
  867. #endif
  868. }
  869. #ifdef CONFIG_MEMCG
  870. static inline struct mem_cgroup *page_memcg(struct page *page)
  871. {
  872. return page->mem_cgroup;
  873. }
  874. static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
  875. {
  876. WARN_ON_ONCE(!rcu_read_lock_held());
  877. return READ_ONCE(page->mem_cgroup);
  878. }
  879. #else
  880. static inline struct mem_cgroup *page_memcg(struct page *page)
  881. {
  882. return NULL;
  883. }
  884. static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
  885. {
  886. WARN_ON_ONCE(!rcu_read_lock_held());
  887. return NULL;
  888. }
  889. #endif
  890. /*
  891. * Some inline functions in vmstat.h depend on page_zone()
  892. */
  893. #include <linux/vmstat.h>
  894. static __always_inline void *lowmem_page_address(const struct page *page)
  895. {
  896. return page_to_virt(page);
  897. }
  898. #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
  899. #define HASHED_PAGE_VIRTUAL
  900. #endif
  901. #if defined(WANT_PAGE_VIRTUAL)
  902. static inline void *page_address(const struct page *page)
  903. {
  904. return page->virtual;
  905. }
  906. static inline void set_page_address(struct page *page, void *address)
  907. {
  908. page->virtual = address;
  909. }
  910. #define page_address_init() do { } while(0)
  911. #endif
  912. #if defined(HASHED_PAGE_VIRTUAL)
  913. void *page_address(const struct page *page);
  914. void set_page_address(struct page *page, void *virtual);
  915. void page_address_init(void);
  916. #endif
  917. #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
  918. #define page_address(page) lowmem_page_address(page)
  919. #define set_page_address(page, address) do { } while(0)
  920. #define page_address_init() do { } while(0)
  921. #endif
  922. extern void *page_rmapping(struct page *page);
  923. extern struct anon_vma *page_anon_vma(struct page *page);
  924. extern struct address_space *page_mapping(struct page *page);
  925. extern struct address_space *__page_file_mapping(struct page *);
  926. static inline
  927. struct address_space *page_file_mapping(struct page *page)
  928. {
  929. if (unlikely(PageSwapCache(page)))
  930. return __page_file_mapping(page);
  931. return page->mapping;
  932. }
  933. extern pgoff_t __page_file_index(struct page *page);
  934. /*
  935. * Return the pagecache index of the passed page. Regular pagecache pages
  936. * use ->index whereas swapcache pages use swp_offset(->private)
  937. */
  938. static inline pgoff_t page_index(struct page *page)
  939. {
  940. if (unlikely(PageSwapCache(page)))
  941. return __page_file_index(page);
  942. return page->index;
  943. }
  944. bool page_mapped(struct page *page);
  945. struct address_space *page_mapping(struct page *page);
  946. /*
  947. * Return true only if the page has been allocated with
  948. * ALLOC_NO_WATERMARKS and the low watermark was not
  949. * met implying that the system is under some pressure.
  950. */
  951. static inline bool page_is_pfmemalloc(struct page *page)
  952. {
  953. /*
  954. * Page index cannot be this large so this must be
  955. * a pfmemalloc page.
  956. */
  957. return page->index == -1UL;
  958. }
  959. /*
  960. * Only to be called by the page allocator on a freshly allocated
  961. * page.
  962. */
  963. static inline void set_page_pfmemalloc(struct page *page)
  964. {
  965. page->index = -1UL;
  966. }
  967. static inline void clear_page_pfmemalloc(struct page *page)
  968. {
  969. page->index = 0;
  970. }
  971. /*
  972. * Different kinds of faults, as returned by handle_mm_fault().
  973. * Used to decide whether a process gets delivered SIGBUS or
  974. * just gets major/minor fault counters bumped up.
  975. */
  976. #define VM_FAULT_OOM 0x0001
  977. #define VM_FAULT_SIGBUS 0x0002
  978. #define VM_FAULT_MAJOR 0x0004
  979. #define VM_FAULT_WRITE 0x0008 /* Special case for get_user_pages */
  980. #define VM_FAULT_HWPOISON 0x0010 /* Hit poisoned small page */
  981. #define VM_FAULT_HWPOISON_LARGE 0x0020 /* Hit poisoned large page. Index encoded in upper bits */
  982. #define VM_FAULT_SIGSEGV 0x0040
  983. #define VM_FAULT_NOPAGE 0x0100 /* ->fault installed the pte, not return page */
  984. #define VM_FAULT_LOCKED 0x0200 /* ->fault locked the returned page */
  985. #define VM_FAULT_RETRY 0x0400 /* ->fault blocked, must retry */
  986. #define VM_FAULT_FALLBACK 0x0800 /* huge page fault failed, fall back to small */
  987. #define VM_FAULT_DONE_COW 0x1000 /* ->fault has fully handled COW */
  988. #define VM_FAULT_HWPOISON_LARGE_MASK 0xf000 /* encodes hpage index for large hwpoison */
  989. #define VM_FAULT_ERROR (VM_FAULT_OOM | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | \
  990. VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE | \
  991. VM_FAULT_FALLBACK)
  992. #define VM_FAULT_RESULT_TRACE \
  993. { VM_FAULT_OOM, "OOM" }, \
  994. { VM_FAULT_SIGBUS, "SIGBUS" }, \
  995. { VM_FAULT_MAJOR, "MAJOR" }, \
  996. { VM_FAULT_WRITE, "WRITE" }, \
  997. { VM_FAULT_HWPOISON, "HWPOISON" }, \
  998. { VM_FAULT_HWPOISON_LARGE, "HWPOISON_LARGE" }, \
  999. { VM_FAULT_SIGSEGV, "SIGSEGV" }, \
  1000. { VM_FAULT_NOPAGE, "NOPAGE" }, \
  1001. { VM_FAULT_LOCKED, "LOCKED" }, \
  1002. { VM_FAULT_RETRY, "RETRY" }, \
  1003. { VM_FAULT_FALLBACK, "FALLBACK" }, \
  1004. { VM_FAULT_DONE_COW, "DONE_COW" }
  1005. /* Encode hstate index for a hwpoisoned large page */
  1006. #define VM_FAULT_SET_HINDEX(x) ((x) << 12)
  1007. #define VM_FAULT_GET_HINDEX(x) (((x) >> 12) & 0xf)
  1008. /*
  1009. * Can be called by the pagefault handler when it gets a VM_FAULT_OOM.
  1010. */
  1011. extern void pagefault_out_of_memory(void);
  1012. #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
  1013. /*
  1014. * Flags passed to show_mem() and show_free_areas() to suppress output in
  1015. * various contexts.
  1016. */
  1017. #define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */
  1018. extern void show_free_areas(unsigned int flags, nodemask_t *nodemask);
  1019. extern bool can_do_mlock(void);
  1020. extern int user_shm_lock(size_t, struct user_struct *);
  1021. extern void user_shm_unlock(size_t, struct user_struct *);
  1022. /*
  1023. * Parameter block passed down to zap_pte_range in exceptional cases.
  1024. */
  1025. struct zap_details {
  1026. struct address_space *check_mapping; /* Check page->mapping if set */
  1027. pgoff_t first_index; /* Lowest page->index to unmap */
  1028. pgoff_t last_index; /* Highest page->index to unmap */
  1029. };
  1030. struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
  1031. pte_t pte);
  1032. struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
  1033. pmd_t pmd);
  1034. int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
  1035. unsigned long size);
  1036. void zap_page_range(struct vm_area_struct *vma, unsigned long address,
  1037. unsigned long size);
  1038. void unmap_vmas(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
  1039. unsigned long start, unsigned long end);
  1040. /**
  1041. * mm_walk - callbacks for walk_page_range
  1042. * @pud_entry: if set, called for each non-empty PUD (2nd-level) entry
  1043. * this handler should only handle pud_trans_huge() puds.
  1044. * the pmd_entry or pte_entry callbacks will be used for
  1045. * regular PUDs.
  1046. * @pmd_entry: if set, called for each non-empty PMD (3rd-level) entry
  1047. * this handler is required to be able to handle
  1048. * pmd_trans_huge() pmds. They may simply choose to
  1049. * split_huge_page() instead of handling it explicitly.
  1050. * @pte_entry: if set, called for each non-empty PTE (4th-level) entry
  1051. * @pte_hole: if set, called for each hole at all levels
  1052. * @hugetlb_entry: if set, called for each hugetlb entry
  1053. * @test_walk: caller specific callback function to determine whether
  1054. * we walk over the current vma or not. Returning 0
  1055. * value means "do page table walk over the current vma,"
  1056. * and a negative one means "abort current page table walk
  1057. * right now." 1 means "skip the current vma."
  1058. * @mm: mm_struct representing the target process of page table walk
  1059. * @vma: vma currently walked (NULL if walking outside vmas)
  1060. * @private: private data for callbacks' usage
  1061. *
  1062. * (see the comment on walk_page_range() for more details)
  1063. */
  1064. struct mm_walk {
  1065. int (*pud_entry)(pud_t *pud, unsigned long addr,
  1066. unsigned long next, struct mm_walk *walk);
  1067. int (*pmd_entry)(pmd_t *pmd, unsigned long addr,
  1068. unsigned long next, struct mm_walk *walk);
  1069. int (*pte_entry)(pte_t *pte, unsigned long addr,
  1070. unsigned long next, struct mm_walk *walk);
  1071. int (*pte_hole)(unsigned long addr, unsigned long next,
  1072. struct mm_walk *walk);
  1073. int (*hugetlb_entry)(pte_t *pte, unsigned long hmask,
  1074. unsigned long addr, unsigned long next,
  1075. struct mm_walk *walk);
  1076. int (*test_walk)(unsigned long addr, unsigned long next,
  1077. struct mm_walk *walk);
  1078. struct mm_struct *mm;
  1079. struct vm_area_struct *vma;
  1080. void *private;
  1081. };
  1082. int walk_page_range(unsigned long addr, unsigned long end,
  1083. struct mm_walk *walk);
  1084. int walk_page_vma(struct vm_area_struct *vma, struct mm_walk *walk);
  1085. void free_pgd_range(struct mmu_gather *tlb, unsigned long addr,
  1086. unsigned long end, unsigned long floor, unsigned long ceiling);
  1087. int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
  1088. struct vm_area_struct *vma);
  1089. void unmap_mapping_range(struct address_space *mapping,
  1090. loff_t const holebegin, loff_t const holelen, int even_cows);
  1091. int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
  1092. unsigned long *start, unsigned long *end,
  1093. pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp);
  1094. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  1095. unsigned long *pfn);
  1096. int follow_phys(struct vm_area_struct *vma, unsigned long address,
  1097. unsigned int flags, unsigned long *prot, resource_size_t *phys);
  1098. int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
  1099. void *buf, int len, int write);
  1100. static inline void unmap_shared_mapping_range(struct address_space *mapping,
  1101. loff_t const holebegin, loff_t const holelen)
  1102. {
  1103. unmap_mapping_range(mapping, holebegin, holelen, 0);
  1104. }
  1105. extern void truncate_pagecache(struct inode *inode, loff_t new);
  1106. extern void truncate_setsize(struct inode *inode, loff_t newsize);
  1107. void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to);
  1108. void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end);
  1109. int truncate_inode_page(struct address_space *mapping, struct page *page);
  1110. int generic_error_remove_page(struct address_space *mapping, struct page *page);
  1111. int invalidate_inode_page(struct page *page);
  1112. #ifdef CONFIG_MMU
  1113. extern int handle_mm_fault(struct vm_area_struct *vma, unsigned long address,
  1114. unsigned int flags);
  1115. extern int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  1116. unsigned long address, unsigned int fault_flags,
  1117. bool *unlocked);
  1118. #else
  1119. static inline int handle_mm_fault(struct vm_area_struct *vma,
  1120. unsigned long address, unsigned int flags)
  1121. {
  1122. /* should never happen if there's no MMU */
  1123. BUG();
  1124. return VM_FAULT_SIGBUS;
  1125. }
  1126. static inline int fixup_user_fault(struct task_struct *tsk,
  1127. struct mm_struct *mm, unsigned long address,
  1128. unsigned int fault_flags, bool *unlocked)
  1129. {
  1130. /* should never happen if there's no MMU */
  1131. BUG();
  1132. return -EFAULT;
  1133. }
  1134. #endif
  1135. extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
  1136. unsigned int gup_flags);
  1137. extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  1138. void *buf, int len, unsigned int gup_flags);
  1139. extern int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
  1140. unsigned long addr, void *buf, int len, unsigned int gup_flags);
  1141. long get_user_pages_remote(struct task_struct *tsk, struct mm_struct *mm,
  1142. unsigned long start, unsigned long nr_pages,
  1143. unsigned int gup_flags, struct page **pages,
  1144. struct vm_area_struct **vmas, int *locked);
  1145. long get_user_pages(unsigned long start, unsigned long nr_pages,
  1146. unsigned int gup_flags, struct page **pages,
  1147. struct vm_area_struct **vmas);
  1148. long get_user_pages_locked(unsigned long start, unsigned long nr_pages,
  1149. unsigned int gup_flags, struct page **pages, int *locked);
  1150. long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
  1151. struct page **pages, unsigned int gup_flags);
  1152. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  1153. struct page **pages);
  1154. /* Container for pinned pfns / pages */
  1155. struct frame_vector {
  1156. unsigned int nr_allocated; /* Number of frames we have space for */
  1157. unsigned int nr_frames; /* Number of frames stored in ptrs array */
  1158. bool got_ref; /* Did we pin pages by getting page ref? */
  1159. bool is_pfns; /* Does array contain pages or pfns? */
  1160. void *ptrs[0]; /* Array of pinned pfns / pages. Use
  1161. * pfns_vector_pages() or pfns_vector_pfns()
  1162. * for access */
  1163. };
  1164. struct frame_vector *frame_vector_create(unsigned int nr_frames);
  1165. void frame_vector_destroy(struct frame_vector *vec);
  1166. int get_vaddr_frames(unsigned long start, unsigned int nr_pfns,
  1167. unsigned int gup_flags, struct frame_vector *vec);
  1168. void put_vaddr_frames(struct frame_vector *vec);
  1169. int frame_vector_to_pages(struct frame_vector *vec);
  1170. void frame_vector_to_pfns(struct frame_vector *vec);
  1171. static inline unsigned int frame_vector_count(struct frame_vector *vec)
  1172. {
  1173. return vec->nr_frames;
  1174. }
  1175. static inline struct page **frame_vector_pages(struct frame_vector *vec)
  1176. {
  1177. if (vec->is_pfns) {
  1178. int err = frame_vector_to_pages(vec);
  1179. if (err)
  1180. return ERR_PTR(err);
  1181. }
  1182. return (struct page **)(vec->ptrs);
  1183. }
  1184. static inline unsigned long *frame_vector_pfns(struct frame_vector *vec)
  1185. {
  1186. if (!vec->is_pfns)
  1187. frame_vector_to_pfns(vec);
  1188. return (unsigned long *)(vec->ptrs);
  1189. }
  1190. struct kvec;
  1191. int get_kernel_pages(const struct kvec *iov, int nr_pages, int write,
  1192. struct page **pages);
  1193. int get_kernel_page(unsigned long start, int write, struct page **pages);
  1194. struct page *get_dump_page(unsigned long addr);
  1195. extern int try_to_release_page(struct page * page, gfp_t gfp_mask);
  1196. extern void do_invalidatepage(struct page *page, unsigned int offset,
  1197. unsigned int length);
  1198. int __set_page_dirty_nobuffers(struct page *page);
  1199. int __set_page_dirty_no_writeback(struct page *page);
  1200. int redirty_page_for_writepage(struct writeback_control *wbc,
  1201. struct page *page);
  1202. void account_page_dirtied(struct page *page, struct address_space *mapping);
  1203. void account_page_cleaned(struct page *page, struct address_space *mapping,
  1204. struct bdi_writeback *wb);
  1205. int set_page_dirty(struct page *page);
  1206. int set_page_dirty_lock(struct page *page);
  1207. void cancel_dirty_page(struct page *page);
  1208. int clear_page_dirty_for_io(struct page *page);
  1209. int get_cmdline(struct task_struct *task, char *buffer, int buflen);
  1210. static inline bool vma_is_anonymous(struct vm_area_struct *vma)
  1211. {
  1212. return !vma->vm_ops;
  1213. }
  1214. #ifdef CONFIG_SHMEM
  1215. /*
  1216. * The vma_is_shmem is not inline because it is used only by slow
  1217. * paths in userfault.
  1218. */
  1219. bool vma_is_shmem(struct vm_area_struct *vma);
  1220. #else
  1221. static inline bool vma_is_shmem(struct vm_area_struct *vma) { return false; }
  1222. #endif
  1223. int vma_is_stack_for_current(struct vm_area_struct *vma);
  1224. extern unsigned long move_page_tables(struct vm_area_struct *vma,
  1225. unsigned long old_addr, struct vm_area_struct *new_vma,
  1226. unsigned long new_addr, unsigned long len,
  1227. bool need_rmap_locks);
  1228. extern unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
  1229. unsigned long end, pgprot_t newprot,
  1230. int dirty_accountable, int prot_numa);
  1231. extern int mprotect_fixup(struct vm_area_struct *vma,
  1232. struct vm_area_struct **pprev, unsigned long start,
  1233. unsigned long end, unsigned long newflags);
  1234. /*
  1235. * doesn't attempt to fault and will return short.
  1236. */
  1237. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  1238. struct page **pages);
  1239. /*
  1240. * per-process(per-mm_struct) statistics.
  1241. */
  1242. static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
  1243. {
  1244. long val = atomic_long_read(&mm->rss_stat.count[member]);
  1245. #ifdef SPLIT_RSS_COUNTING
  1246. /*
  1247. * counter is updated in asynchronous manner and may go to minus.
  1248. * But it's never be expected number for users.
  1249. */
  1250. if (val < 0)
  1251. val = 0;
  1252. #endif
  1253. return (unsigned long)val;
  1254. }
  1255. static inline void add_mm_counter(struct mm_struct *mm, int member, long value)
  1256. {
  1257. atomic_long_add(value, &mm->rss_stat.count[member]);
  1258. }
  1259. static inline void inc_mm_counter(struct mm_struct *mm, int member)
  1260. {
  1261. atomic_long_inc(&mm->rss_stat.count[member]);
  1262. }
  1263. static inline void dec_mm_counter(struct mm_struct *mm, int member)
  1264. {
  1265. atomic_long_dec(&mm->rss_stat.count[member]);
  1266. }
  1267. /* Optimized variant when page is already known not to be PageAnon */
  1268. static inline int mm_counter_file(struct page *page)
  1269. {
  1270. if (PageSwapBacked(page))
  1271. return MM_SHMEMPAGES;
  1272. return MM_FILEPAGES;
  1273. }
  1274. static inline int mm_counter(struct page *page)
  1275. {
  1276. if (PageAnon(page))
  1277. return MM_ANONPAGES;
  1278. return mm_counter_file(page);
  1279. }
  1280. static inline unsigned long get_mm_rss(struct mm_struct *mm)
  1281. {
  1282. return get_mm_counter(mm, MM_FILEPAGES) +
  1283. get_mm_counter(mm, MM_ANONPAGES) +
  1284. get_mm_counter(mm, MM_SHMEMPAGES);
  1285. }
  1286. static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm)
  1287. {
  1288. return max(mm->hiwater_rss, get_mm_rss(mm));
  1289. }
  1290. static inline unsigned long get_mm_hiwater_vm(struct mm_struct *mm)
  1291. {
  1292. return max(mm->hiwater_vm, mm->total_vm);
  1293. }
  1294. static inline void update_hiwater_rss(struct mm_struct *mm)
  1295. {
  1296. unsigned long _rss = get_mm_rss(mm);
  1297. if ((mm)->hiwater_rss < _rss)
  1298. (mm)->hiwater_rss = _rss;
  1299. }
  1300. static inline void update_hiwater_vm(struct mm_struct *mm)
  1301. {
  1302. if (mm->hiwater_vm < mm->total_vm)
  1303. mm->hiwater_vm = mm->total_vm;
  1304. }
  1305. static inline void reset_mm_hiwater_rss(struct mm_struct *mm)
  1306. {
  1307. mm->hiwater_rss = get_mm_rss(mm);
  1308. }
  1309. static inline void setmax_mm_hiwater_rss(unsigned long *maxrss,
  1310. struct mm_struct *mm)
  1311. {
  1312. unsigned long hiwater_rss = get_mm_hiwater_rss(mm);
  1313. if (*maxrss < hiwater_rss)
  1314. *maxrss = hiwater_rss;
  1315. }
  1316. #if defined(SPLIT_RSS_COUNTING)
  1317. void sync_mm_rss(struct mm_struct *mm);
  1318. #else
  1319. static inline void sync_mm_rss(struct mm_struct *mm)
  1320. {
  1321. }
  1322. #endif
  1323. #ifndef __HAVE_ARCH_PTE_DEVMAP
  1324. static inline int pte_devmap(pte_t pte)
  1325. {
  1326. return 0;
  1327. }
  1328. #endif
  1329. int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot);
  1330. extern pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1331. spinlock_t **ptl);
  1332. static inline pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1333. spinlock_t **ptl)
  1334. {
  1335. pte_t *ptep;
  1336. __cond_lock(*ptl, ptep = __get_locked_pte(mm, addr, ptl));
  1337. return ptep;
  1338. }
  1339. #ifdef __PAGETABLE_P4D_FOLDED
  1340. static inline int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd,
  1341. unsigned long address)
  1342. {
  1343. return 0;
  1344. }
  1345. #else
  1346. int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address);
  1347. #endif
  1348. #ifdef __PAGETABLE_PUD_FOLDED
  1349. static inline int __pud_alloc(struct mm_struct *mm, p4d_t *p4d,
  1350. unsigned long address)
  1351. {
  1352. return 0;
  1353. }
  1354. #else
  1355. int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address);
  1356. #endif
  1357. #if defined(__PAGETABLE_PMD_FOLDED) || !defined(CONFIG_MMU)
  1358. static inline int __pmd_alloc(struct mm_struct *mm, pud_t *pud,
  1359. unsigned long address)
  1360. {
  1361. return 0;
  1362. }
  1363. static inline void mm_nr_pmds_init(struct mm_struct *mm) {}
  1364. static inline unsigned long mm_nr_pmds(struct mm_struct *mm)
  1365. {
  1366. return 0;
  1367. }
  1368. static inline void mm_inc_nr_pmds(struct mm_struct *mm) {}
  1369. static inline void mm_dec_nr_pmds(struct mm_struct *mm) {}
  1370. #else
  1371. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address);
  1372. static inline void mm_nr_pmds_init(struct mm_struct *mm)
  1373. {
  1374. atomic_long_set(&mm->nr_pmds, 0);
  1375. }
  1376. static inline unsigned long mm_nr_pmds(struct mm_struct *mm)
  1377. {
  1378. return atomic_long_read(&mm->nr_pmds);
  1379. }
  1380. static inline void mm_inc_nr_pmds(struct mm_struct *mm)
  1381. {
  1382. atomic_long_inc(&mm->nr_pmds);
  1383. }
  1384. static inline void mm_dec_nr_pmds(struct mm_struct *mm)
  1385. {
  1386. atomic_long_dec(&mm->nr_pmds);
  1387. }
  1388. #endif
  1389. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address);
  1390. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address);
  1391. /*
  1392. * The following ifdef needed to get the 4level-fixup.h header to work.
  1393. * Remove it when 4level-fixup.h has been removed.
  1394. */
  1395. #if defined(CONFIG_MMU) && !defined(__ARCH_HAS_4LEVEL_HACK)
  1396. #ifndef __ARCH_HAS_5LEVEL_HACK
  1397. static inline p4d_t *p4d_alloc(struct mm_struct *mm, pgd_t *pgd,
  1398. unsigned long address)
  1399. {
  1400. return (unlikely(pgd_none(*pgd)) && __p4d_alloc(mm, pgd, address)) ?
  1401. NULL : p4d_offset(pgd, address);
  1402. }
  1403. static inline pud_t *pud_alloc(struct mm_struct *mm, p4d_t *p4d,
  1404. unsigned long address)
  1405. {
  1406. return (unlikely(p4d_none(*p4d)) && __pud_alloc(mm, p4d, address)) ?
  1407. NULL : pud_offset(p4d, address);
  1408. }
  1409. #endif /* !__ARCH_HAS_5LEVEL_HACK */
  1410. static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  1411. {
  1412. return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address))?
  1413. NULL: pmd_offset(pud, address);
  1414. }
  1415. #endif /* CONFIG_MMU && !__ARCH_HAS_4LEVEL_HACK */
  1416. #if USE_SPLIT_PTE_PTLOCKS
  1417. #if ALLOC_SPLIT_PTLOCKS
  1418. void __init ptlock_cache_init(void);
  1419. extern bool ptlock_alloc(struct page *page);
  1420. extern void ptlock_free(struct page *page);
  1421. static inline spinlock_t *ptlock_ptr(struct page *page)
  1422. {
  1423. return page->ptl;
  1424. }
  1425. #else /* ALLOC_SPLIT_PTLOCKS */
  1426. static inline void ptlock_cache_init(void)
  1427. {
  1428. }
  1429. static inline bool ptlock_alloc(struct page *page)
  1430. {
  1431. return true;
  1432. }
  1433. static inline void ptlock_free(struct page *page)
  1434. {
  1435. }
  1436. static inline spinlock_t *ptlock_ptr(struct page *page)
  1437. {
  1438. return &page->ptl;
  1439. }
  1440. #endif /* ALLOC_SPLIT_PTLOCKS */
  1441. static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
  1442. {
  1443. return ptlock_ptr(pmd_page(*pmd));
  1444. }
  1445. static inline bool ptlock_init(struct page *page)
  1446. {
  1447. /*
  1448. * prep_new_page() initialize page->private (and therefore page->ptl)
  1449. * with 0. Make sure nobody took it in use in between.
  1450. *
  1451. * It can happen if arch try to use slab for page table allocation:
  1452. * slab code uses page->slab_cache, which share storage with page->ptl.
  1453. */
  1454. VM_BUG_ON_PAGE(*(unsigned long *)&page->ptl, page);
  1455. if (!ptlock_alloc(page))
  1456. return false;
  1457. spin_lock_init(ptlock_ptr(page));
  1458. return true;
  1459. }
  1460. /* Reset page->mapping so free_pages_check won't complain. */
  1461. static inline void pte_lock_deinit(struct page *page)
  1462. {
  1463. page->mapping = NULL;
  1464. ptlock_free(page);
  1465. }
  1466. #else /* !USE_SPLIT_PTE_PTLOCKS */
  1467. /*
  1468. * We use mm->page_table_lock to guard all pagetable pages of the mm.
  1469. */
  1470. static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
  1471. {
  1472. return &mm->page_table_lock;
  1473. }
  1474. static inline void ptlock_cache_init(void) {}
  1475. static inline bool ptlock_init(struct page *page) { return true; }
  1476. static inline void pte_lock_deinit(struct page *page) {}
  1477. #endif /* USE_SPLIT_PTE_PTLOCKS */
  1478. static inline void pgtable_init(void)
  1479. {
  1480. ptlock_cache_init();
  1481. pgtable_cache_init();
  1482. }
  1483. static inline bool pgtable_page_ctor(struct page *page)
  1484. {
  1485. if (!ptlock_init(page))
  1486. return false;
  1487. inc_zone_page_state(page, NR_PAGETABLE);
  1488. return true;
  1489. }
  1490. static inline void pgtable_page_dtor(struct page *page)
  1491. {
  1492. pte_lock_deinit(page);
  1493. dec_zone_page_state(page, NR_PAGETABLE);
  1494. }
  1495. #define pte_offset_map_lock(mm, pmd, address, ptlp) \
  1496. ({ \
  1497. spinlock_t *__ptl = pte_lockptr(mm, pmd); \
  1498. pte_t *__pte = pte_offset_map(pmd, address); \
  1499. *(ptlp) = __ptl; \
  1500. spin_lock(__ptl); \
  1501. __pte; \
  1502. })
  1503. #define pte_unmap_unlock(pte, ptl) do { \
  1504. spin_unlock(ptl); \
  1505. pte_unmap(pte); \
  1506. } while (0)
  1507. #define pte_alloc(mm, pmd, address) \
  1508. (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd, address))
  1509. #define pte_alloc_map(mm, pmd, address) \
  1510. (pte_alloc(mm, pmd, address) ? NULL : pte_offset_map(pmd, address))
  1511. #define pte_alloc_map_lock(mm, pmd, address, ptlp) \
  1512. (pte_alloc(mm, pmd, address) ? \
  1513. NULL : pte_offset_map_lock(mm, pmd, address, ptlp))
  1514. #define pte_alloc_kernel(pmd, address) \
  1515. ((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd, address))? \
  1516. NULL: pte_offset_kernel(pmd, address))
  1517. #if USE_SPLIT_PMD_PTLOCKS
  1518. static struct page *pmd_to_page(pmd_t *pmd)
  1519. {
  1520. unsigned long mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
  1521. return virt_to_page((void *)((unsigned long) pmd & mask));
  1522. }
  1523. static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
  1524. {
  1525. return ptlock_ptr(pmd_to_page(pmd));
  1526. }
  1527. static inline bool pgtable_pmd_page_ctor(struct page *page)
  1528. {
  1529. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1530. page->pmd_huge_pte = NULL;
  1531. #endif
  1532. return ptlock_init(page);
  1533. }
  1534. static inline void pgtable_pmd_page_dtor(struct page *page)
  1535. {
  1536. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1537. VM_BUG_ON_PAGE(page->pmd_huge_pte, page);
  1538. #endif
  1539. ptlock_free(page);
  1540. }
  1541. #define pmd_huge_pte(mm, pmd) (pmd_to_page(pmd)->pmd_huge_pte)
  1542. #else
  1543. static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
  1544. {
  1545. return &mm->page_table_lock;
  1546. }
  1547. static inline bool pgtable_pmd_page_ctor(struct page *page) { return true; }
  1548. static inline void pgtable_pmd_page_dtor(struct page *page) {}
  1549. #define pmd_huge_pte(mm, pmd) ((mm)->pmd_huge_pte)
  1550. #endif
  1551. static inline spinlock_t *pmd_lock(struct mm_struct *mm, pmd_t *pmd)
  1552. {
  1553. spinlock_t *ptl = pmd_lockptr(mm, pmd);
  1554. spin_lock(ptl);
  1555. return ptl;
  1556. }
  1557. /*
  1558. * No scalability reason to split PUD locks yet, but follow the same pattern
  1559. * as the PMD locks to make it easier if we decide to. The VM should not be
  1560. * considered ready to switch to split PUD locks yet; there may be places
  1561. * which need to be converted from page_table_lock.
  1562. */
  1563. static inline spinlock_t *pud_lockptr(struct mm_struct *mm, pud_t *pud)
  1564. {
  1565. return &mm->page_table_lock;
  1566. }
  1567. static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud)
  1568. {
  1569. spinlock_t *ptl = pud_lockptr(mm, pud);
  1570. spin_lock(ptl);
  1571. return ptl;
  1572. }
  1573. extern void __init pagecache_init(void);
  1574. extern void free_area_init(unsigned long * zones_size);
  1575. extern void free_area_init_node(int nid, unsigned long * zones_size,
  1576. unsigned long zone_start_pfn, unsigned long *zholes_size);
  1577. extern void free_initmem(void);
  1578. /*
  1579. * Free reserved pages within range [PAGE_ALIGN(start), end & PAGE_MASK)
  1580. * into the buddy system. The freed pages will be poisoned with pattern
  1581. * "poison" if it's within range [0, UCHAR_MAX].
  1582. * Return pages freed into the buddy system.
  1583. */
  1584. extern unsigned long free_reserved_area(void *start, void *end,
  1585. int poison, char *s);
  1586. #ifdef CONFIG_HIGHMEM
  1587. /*
  1588. * Free a highmem page into the buddy system, adjusting totalhigh_pages
  1589. * and totalram_pages.
  1590. */
  1591. extern void free_highmem_page(struct page *page);
  1592. #endif
  1593. extern void adjust_managed_page_count(struct page *page, long count);
  1594. extern void mem_init_print_info(const char *str);
  1595. extern void reserve_bootmem_region(phys_addr_t start, phys_addr_t end);
  1596. /* Free the reserved page into the buddy system, so it gets managed. */
  1597. static inline void __free_reserved_page(struct page *page)
  1598. {
  1599. ClearPageReserved(page);
  1600. init_page_count(page);
  1601. __free_page(page);
  1602. }
  1603. static inline void free_reserved_page(struct page *page)
  1604. {
  1605. __free_reserved_page(page);
  1606. adjust_managed_page_count(page, 1);
  1607. }
  1608. static inline void mark_page_reserved(struct page *page)
  1609. {
  1610. SetPageReserved(page);
  1611. adjust_managed_page_count(page, -1);
  1612. }
  1613. /*
  1614. * Default method to free all the __init memory into the buddy system.
  1615. * The freed pages will be poisoned with pattern "poison" if it's within
  1616. * range [0, UCHAR_MAX].
  1617. * Return pages freed into the buddy system.
  1618. */
  1619. static inline unsigned long free_initmem_default(int poison)
  1620. {
  1621. extern char __init_begin[], __init_end[];
  1622. return free_reserved_area(&__init_begin, &__init_end,
  1623. poison, "unused kernel");
  1624. }
  1625. static inline unsigned long get_num_physpages(void)
  1626. {
  1627. int nid;
  1628. unsigned long phys_pages = 0;
  1629. for_each_online_node(nid)
  1630. phys_pages += node_present_pages(nid);
  1631. return phys_pages;
  1632. }
  1633. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  1634. /*
  1635. * With CONFIG_HAVE_MEMBLOCK_NODE_MAP set, an architecture may initialise its
  1636. * zones, allocate the backing mem_map and account for memory holes in a more
  1637. * architecture independent manner. This is a substitute for creating the
  1638. * zone_sizes[] and zholes_size[] arrays and passing them to
  1639. * free_area_init_node()
  1640. *
  1641. * An architecture is expected to register range of page frames backed by
  1642. * physical memory with memblock_add[_node]() before calling
  1643. * free_area_init_nodes() passing in the PFN each zone ends at. At a basic
  1644. * usage, an architecture is expected to do something like
  1645. *
  1646. * unsigned long max_zone_pfns[MAX_NR_ZONES] = {max_dma, max_normal_pfn,
  1647. * max_highmem_pfn};
  1648. * for_each_valid_physical_page_range()
  1649. * memblock_add_node(base, size, nid)
  1650. * free_area_init_nodes(max_zone_pfns);
  1651. *
  1652. * free_bootmem_with_active_regions() calls free_bootmem_node() for each
  1653. * registered physical page range. Similarly
  1654. * sparse_memory_present_with_active_regions() calls memory_present() for
  1655. * each range when SPARSEMEM is enabled.
  1656. *
  1657. * See mm/page_alloc.c for more information on each function exposed by
  1658. * CONFIG_HAVE_MEMBLOCK_NODE_MAP.
  1659. */
  1660. extern void free_area_init_nodes(unsigned long *max_zone_pfn);
  1661. unsigned long node_map_pfn_alignment(void);
  1662. unsigned long __absent_pages_in_range(int nid, unsigned long start_pfn,
  1663. unsigned long end_pfn);
  1664. extern unsigned long absent_pages_in_range(unsigned long start_pfn,
  1665. unsigned long end_pfn);
  1666. extern void get_pfn_range_for_nid(unsigned int nid,
  1667. unsigned long *start_pfn, unsigned long *end_pfn);
  1668. extern unsigned long find_min_pfn_with_active_regions(void);
  1669. extern void free_bootmem_with_active_regions(int nid,
  1670. unsigned long max_low_pfn);
  1671. extern void sparse_memory_present_with_active_regions(int nid);
  1672. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  1673. #if !defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP) && \
  1674. !defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID)
  1675. static inline int __early_pfn_to_nid(unsigned long pfn,
  1676. struct mminit_pfnnid_cache *state)
  1677. {
  1678. return 0;
  1679. }
  1680. #else
  1681. /* please see mm/page_alloc.c */
  1682. extern int __meminit early_pfn_to_nid(unsigned long pfn);
  1683. /* there is a per-arch backend function. */
  1684. extern int __meminit __early_pfn_to_nid(unsigned long pfn,
  1685. struct mminit_pfnnid_cache *state);
  1686. #endif
  1687. extern void set_dma_reserve(unsigned long new_dma_reserve);
  1688. extern void memmap_init_zone(unsigned long, int, unsigned long,
  1689. unsigned long, enum memmap_context);
  1690. extern void setup_per_zone_wmarks(void);
  1691. extern int __meminit init_per_zone_wmark_min(void);
  1692. extern void mem_init(void);
  1693. extern void __init mmap_init(void);
  1694. extern void show_mem(unsigned int flags, nodemask_t *nodemask);
  1695. extern long si_mem_available(void);
  1696. extern void si_meminfo(struct sysinfo * val);
  1697. extern void si_meminfo_node(struct sysinfo *val, int nid);
  1698. #ifdef __HAVE_ARCH_RESERVED_KERNEL_PAGES
  1699. extern unsigned long arch_reserved_kernel_pages(void);
  1700. #endif
  1701. extern __printf(3, 4)
  1702. void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...);
  1703. extern void setup_per_cpu_pageset(void);
  1704. extern void zone_pcp_update(struct zone *zone);
  1705. extern void zone_pcp_reset(struct zone *zone);
  1706. /* page_alloc.c */
  1707. extern int min_free_kbytes;
  1708. extern int watermark_scale_factor;
  1709. /* nommu.c */
  1710. extern atomic_long_t mmap_pages_allocated;
  1711. extern int nommu_shrink_inode_mappings(struct inode *, size_t, size_t);
  1712. /* interval_tree.c */
  1713. void vma_interval_tree_insert(struct vm_area_struct *node,
  1714. struct rb_root *root);
  1715. void vma_interval_tree_insert_after(struct vm_area_struct *node,
  1716. struct vm_area_struct *prev,
  1717. struct rb_root *root);
  1718. void vma_interval_tree_remove(struct vm_area_struct *node,
  1719. struct rb_root *root);
  1720. struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root *root,
  1721. unsigned long start, unsigned long last);
  1722. struct vm_area_struct *vma_interval_tree_iter_next(struct vm_area_struct *node,
  1723. unsigned long start, unsigned long last);
  1724. #define vma_interval_tree_foreach(vma, root, start, last) \
  1725. for (vma = vma_interval_tree_iter_first(root, start, last); \
  1726. vma; vma = vma_interval_tree_iter_next(vma, start, last))
  1727. void anon_vma_interval_tree_insert(struct anon_vma_chain *node,
  1728. struct rb_root *root);
  1729. void anon_vma_interval_tree_remove(struct anon_vma_chain *node,
  1730. struct rb_root *root);
  1731. struct anon_vma_chain *anon_vma_interval_tree_iter_first(
  1732. struct rb_root *root, unsigned long start, unsigned long last);
  1733. struct anon_vma_chain *anon_vma_interval_tree_iter_next(
  1734. struct anon_vma_chain *node, unsigned long start, unsigned long last);
  1735. #ifdef CONFIG_DEBUG_VM_RB
  1736. void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
  1737. #endif
  1738. #define anon_vma_interval_tree_foreach(avc, root, start, last) \
  1739. for (avc = anon_vma_interval_tree_iter_first(root, start, last); \
  1740. avc; avc = anon_vma_interval_tree_iter_next(avc, start, last))
  1741. /* mmap.c */
  1742. extern int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin);
  1743. extern int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
  1744. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
  1745. struct vm_area_struct *expand);
  1746. static inline int vma_adjust(struct vm_area_struct *vma, unsigned long start,
  1747. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
  1748. {
  1749. return __vma_adjust(vma, start, end, pgoff, insert, NULL);
  1750. }
  1751. extern struct vm_area_struct *vma_merge(struct mm_struct *,
  1752. struct vm_area_struct *prev, unsigned long addr, unsigned long end,
  1753. unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
  1754. struct mempolicy *, struct vm_userfaultfd_ctx);
  1755. extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
  1756. extern int __split_vma(struct mm_struct *, struct vm_area_struct *,
  1757. unsigned long addr, int new_below);
  1758. extern int split_vma(struct mm_struct *, struct vm_area_struct *,
  1759. unsigned long addr, int new_below);
  1760. extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
  1761. extern void __vma_link_rb(struct mm_struct *, struct vm_area_struct *,
  1762. struct rb_node **, struct rb_node *);
  1763. extern void unlink_file_vma(struct vm_area_struct *);
  1764. extern struct vm_area_struct *copy_vma(struct vm_area_struct **,
  1765. unsigned long addr, unsigned long len, pgoff_t pgoff,
  1766. bool *need_rmap_locks);
  1767. extern void exit_mmap(struct mm_struct *);
  1768. static inline int check_data_rlimit(unsigned long rlim,
  1769. unsigned long new,
  1770. unsigned long start,
  1771. unsigned long end_data,
  1772. unsigned long start_data)
  1773. {
  1774. if (rlim < RLIM_INFINITY) {
  1775. if (((new - start) + (end_data - start_data)) > rlim)
  1776. return -ENOSPC;
  1777. }
  1778. return 0;
  1779. }
  1780. extern int mm_take_all_locks(struct mm_struct *mm);
  1781. extern void mm_drop_all_locks(struct mm_struct *mm);
  1782. extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
  1783. extern struct file *get_mm_exe_file(struct mm_struct *mm);
  1784. extern struct file *get_task_exe_file(struct task_struct *task);
  1785. extern bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long npages);
  1786. extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages);
  1787. extern bool vma_is_special_mapping(const struct vm_area_struct *vma,
  1788. const struct vm_special_mapping *sm);
  1789. extern struct vm_area_struct *_install_special_mapping(struct mm_struct *mm,
  1790. unsigned long addr, unsigned long len,
  1791. unsigned long flags,
  1792. const struct vm_special_mapping *spec);
  1793. /* This is an obsolete alternative to _install_special_mapping. */
  1794. extern int install_special_mapping(struct mm_struct *mm,
  1795. unsigned long addr, unsigned long len,
  1796. unsigned long flags, struct page **pages);
  1797. extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  1798. extern unsigned long mmap_region(struct file *file, unsigned long addr,
  1799. unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
  1800. struct list_head *uf);
  1801. extern unsigned long do_mmap(struct file *file, unsigned long addr,
  1802. unsigned long len, unsigned long prot, unsigned long flags,
  1803. vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate,
  1804. struct list_head *uf);
  1805. extern int do_munmap(struct mm_struct *, unsigned long, size_t,
  1806. struct list_head *uf);
  1807. static inline unsigned long
  1808. do_mmap_pgoff(struct file *file, unsigned long addr,
  1809. unsigned long len, unsigned long prot, unsigned long flags,
  1810. unsigned long pgoff, unsigned long *populate,
  1811. struct list_head *uf)
  1812. {
  1813. return do_mmap(file, addr, len, prot, flags, 0, pgoff, populate, uf);
  1814. }
  1815. #ifdef CONFIG_MMU
  1816. extern int __mm_populate(unsigned long addr, unsigned long len,
  1817. int ignore_errors);
  1818. static inline void mm_populate(unsigned long addr, unsigned long len)
  1819. {
  1820. /* Ignore errors */
  1821. (void) __mm_populate(addr, len, 1);
  1822. }
  1823. #else
  1824. static inline void mm_populate(unsigned long addr, unsigned long len) {}
  1825. #endif
  1826. /* These take the mm semaphore themselves */
  1827. extern int __must_check vm_brk(unsigned long, unsigned long);
  1828. extern int __must_check vm_brk_flags(unsigned long, unsigned long, unsigned long);
  1829. extern int vm_munmap(unsigned long, size_t);
  1830. extern unsigned long __must_check vm_mmap(struct file *, unsigned long,
  1831. unsigned long, unsigned long,
  1832. unsigned long, unsigned long);
  1833. struct vm_unmapped_area_info {
  1834. #define VM_UNMAPPED_AREA_TOPDOWN 1
  1835. unsigned long flags;
  1836. unsigned long length;
  1837. unsigned long low_limit;
  1838. unsigned long high_limit;
  1839. unsigned long align_mask;
  1840. unsigned long align_offset;
  1841. };
  1842. extern unsigned long unmapped_area(struct vm_unmapped_area_info *info);
  1843. extern unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info);
  1844. /*
  1845. * Search for an unmapped address range.
  1846. *
  1847. * We are looking for a range that:
  1848. * - does not intersect with any VMA;
  1849. * - is contained within the [low_limit, high_limit) interval;
  1850. * - is at least the desired size.
  1851. * - satisfies (begin_addr & align_mask) == (align_offset & align_mask)
  1852. */
  1853. static inline unsigned long
  1854. vm_unmapped_area(struct vm_unmapped_area_info *info)
  1855. {
  1856. if (info->flags & VM_UNMAPPED_AREA_TOPDOWN)
  1857. return unmapped_area_topdown(info);
  1858. else
  1859. return unmapped_area(info);
  1860. }
  1861. /* truncate.c */
  1862. extern void truncate_inode_pages(struct address_space *, loff_t);
  1863. extern void truncate_inode_pages_range(struct address_space *,
  1864. loff_t lstart, loff_t lend);
  1865. extern void truncate_inode_pages_final(struct address_space *);
  1866. /* generic vm_area_ops exported for stackable file systems */
  1867. extern int filemap_fault(struct vm_fault *vmf);
  1868. extern void filemap_map_pages(struct vm_fault *vmf,
  1869. pgoff_t start_pgoff, pgoff_t end_pgoff);
  1870. extern int filemap_page_mkwrite(struct vm_fault *vmf);
  1871. /* mm/page-writeback.c */
  1872. int __must_check write_one_page(struct page *page);
  1873. void task_dirty_inc(struct task_struct *tsk);
  1874. /* readahead.c */
  1875. #define VM_MAX_READAHEAD 128 /* kbytes */
  1876. #define VM_MIN_READAHEAD 16 /* kbytes (includes current page) */
  1877. int force_page_cache_readahead(struct address_space *mapping, struct file *filp,
  1878. pgoff_t offset, unsigned long nr_to_read);
  1879. void page_cache_sync_readahead(struct address_space *mapping,
  1880. struct file_ra_state *ra,
  1881. struct file *filp,
  1882. pgoff_t offset,
  1883. unsigned long size);
  1884. void page_cache_async_readahead(struct address_space *mapping,
  1885. struct file_ra_state *ra,
  1886. struct file *filp,
  1887. struct page *pg,
  1888. pgoff_t offset,
  1889. unsigned long size);
  1890. extern unsigned long stack_guard_gap;
  1891. /* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */
  1892. extern int expand_stack(struct vm_area_struct *vma, unsigned long address);
  1893. /* CONFIG_STACK_GROWSUP still needs to to grow downwards at some places */
  1894. extern int expand_downwards(struct vm_area_struct *vma,
  1895. unsigned long address);
  1896. #if VM_GROWSUP
  1897. extern int expand_upwards(struct vm_area_struct *vma, unsigned long address);
  1898. #else
  1899. #define expand_upwards(vma, address) (0)
  1900. #endif
  1901. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  1902. extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
  1903. extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
  1904. struct vm_area_struct **pprev);
  1905. /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
  1906. NULL if none. Assume start_addr < end_addr. */
  1907. static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
  1908. {
  1909. struct vm_area_struct * vma = find_vma(mm,start_addr);
  1910. if (vma && end_addr <= vma->vm_start)
  1911. vma = NULL;
  1912. return vma;
  1913. }
  1914. static inline unsigned long vm_start_gap(struct vm_area_struct *vma)
  1915. {
  1916. unsigned long vm_start = vma->vm_start;
  1917. if (vma->vm_flags & VM_GROWSDOWN) {
  1918. vm_start -= stack_guard_gap;
  1919. if (vm_start > vma->vm_start)
  1920. vm_start = 0;
  1921. }
  1922. return vm_start;
  1923. }
  1924. static inline unsigned long vm_end_gap(struct vm_area_struct *vma)
  1925. {
  1926. unsigned long vm_end = vma->vm_end;
  1927. if (vma->vm_flags & VM_GROWSUP) {
  1928. vm_end += stack_guard_gap;
  1929. if (vm_end < vma->vm_end)
  1930. vm_end = -PAGE_SIZE;
  1931. }
  1932. return vm_end;
  1933. }
  1934. static inline unsigned long vma_pages(struct vm_area_struct *vma)
  1935. {
  1936. return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  1937. }
  1938. /* Look up the first VMA which exactly match the interval vm_start ... vm_end */
  1939. static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,
  1940. unsigned long vm_start, unsigned long vm_end)
  1941. {
  1942. struct vm_area_struct *vma = find_vma(mm, vm_start);
  1943. if (vma && (vma->vm_start != vm_start || vma->vm_end != vm_end))
  1944. vma = NULL;
  1945. return vma;
  1946. }
  1947. #ifdef CONFIG_MMU
  1948. pgprot_t vm_get_page_prot(unsigned long vm_flags);
  1949. void vma_set_page_prot(struct vm_area_struct *vma);
  1950. #else
  1951. static inline pgprot_t vm_get_page_prot(unsigned long vm_flags)
  1952. {
  1953. return __pgprot(0);
  1954. }
  1955. static inline void vma_set_page_prot(struct vm_area_struct *vma)
  1956. {
  1957. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  1958. }
  1959. #endif
  1960. #ifdef CONFIG_NUMA_BALANCING
  1961. unsigned long change_prot_numa(struct vm_area_struct *vma,
  1962. unsigned long start, unsigned long end);
  1963. #endif
  1964. struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr);
  1965. int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
  1966. unsigned long pfn, unsigned long size, pgprot_t);
  1967. int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *);
  1968. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1969. unsigned long pfn);
  1970. int vm_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
  1971. unsigned long pfn, pgprot_t pgprot);
  1972. int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
  1973. pfn_t pfn);
  1974. int vm_insert_mixed_mkwrite(struct vm_area_struct *vma, unsigned long addr,
  1975. pfn_t pfn);
  1976. int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len);
  1977. struct page *follow_page_mask(struct vm_area_struct *vma,
  1978. unsigned long address, unsigned int foll_flags,
  1979. unsigned int *page_mask);
  1980. static inline struct page *follow_page(struct vm_area_struct *vma,
  1981. unsigned long address, unsigned int foll_flags)
  1982. {
  1983. unsigned int unused_page_mask;
  1984. return follow_page_mask(vma, address, foll_flags, &unused_page_mask);
  1985. }
  1986. #define FOLL_WRITE 0x01 /* check pte is writable */
  1987. #define FOLL_TOUCH 0x02 /* mark page accessed */
  1988. #define FOLL_GET 0x04 /* do get_page on page */
  1989. #define FOLL_DUMP 0x08 /* give error on hole if it would be zero */
  1990. #define FOLL_FORCE 0x10 /* get_user_pages read/write w/o permission */
  1991. #define FOLL_NOWAIT 0x20 /* if a disk transfer is needed, start the IO
  1992. * and return without waiting upon it */
  1993. #define FOLL_POPULATE 0x40 /* fault in page */
  1994. #define FOLL_SPLIT 0x80 /* don't return transhuge pages, split them */
  1995. #define FOLL_HWPOISON 0x100 /* check page is hwpoisoned */
  1996. #define FOLL_NUMA 0x200 /* force NUMA hinting page fault */
  1997. #define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */
  1998. #define FOLL_TRIED 0x800 /* a retry, previous pass started an IO */
  1999. #define FOLL_MLOCK 0x1000 /* lock present pages */
  2000. #define FOLL_REMOTE 0x2000 /* we are working on non-current tsk/mm */
  2001. #define FOLL_COW 0x4000 /* internal GUP flag */
  2002. static inline int vm_fault_to_errno(int vm_fault, int foll_flags)
  2003. {
  2004. if (vm_fault & VM_FAULT_OOM)
  2005. return -ENOMEM;
  2006. if (vm_fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  2007. return (foll_flags & FOLL_HWPOISON) ? -EHWPOISON : -EFAULT;
  2008. if (vm_fault & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  2009. return -EFAULT;
  2010. return 0;
  2011. }
  2012. typedef int (*pte_fn_t)(pte_t *pte, pgtable_t token, unsigned long addr,
  2013. void *data);
  2014. extern int apply_to_page_range(struct mm_struct *mm, unsigned long address,
  2015. unsigned long size, pte_fn_t fn, void *data);
  2016. #ifdef CONFIG_PAGE_POISONING
  2017. extern bool page_poisoning_enabled(void);
  2018. extern void kernel_poison_pages(struct page *page, int numpages, int enable);
  2019. extern bool page_is_poisoned(struct page *page);
  2020. #else
  2021. static inline bool page_poisoning_enabled(void) { return false; }
  2022. static inline void kernel_poison_pages(struct page *page, int numpages,
  2023. int enable) { }
  2024. static inline bool page_is_poisoned(struct page *page) { return false; }
  2025. #endif
  2026. #ifdef CONFIG_DEBUG_PAGEALLOC
  2027. extern bool _debug_pagealloc_enabled;
  2028. extern void __kernel_map_pages(struct page *page, int numpages, int enable);
  2029. static inline bool debug_pagealloc_enabled(void)
  2030. {
  2031. return _debug_pagealloc_enabled;
  2032. }
  2033. static inline void
  2034. kernel_map_pages(struct page *page, int numpages, int enable)
  2035. {
  2036. if (!debug_pagealloc_enabled())
  2037. return;
  2038. __kernel_map_pages(page, numpages, enable);
  2039. }
  2040. #ifdef CONFIG_HIBERNATION
  2041. extern bool kernel_page_present(struct page *page);
  2042. #endif /* CONFIG_HIBERNATION */
  2043. #else /* CONFIG_DEBUG_PAGEALLOC */
  2044. static inline void
  2045. kernel_map_pages(struct page *page, int numpages, int enable) {}
  2046. #ifdef CONFIG_HIBERNATION
  2047. static inline bool kernel_page_present(struct page *page) { return true; }
  2048. #endif /* CONFIG_HIBERNATION */
  2049. static inline bool debug_pagealloc_enabled(void)
  2050. {
  2051. return false;
  2052. }
  2053. #endif /* CONFIG_DEBUG_PAGEALLOC */
  2054. #ifdef __HAVE_ARCH_GATE_AREA
  2055. extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
  2056. extern int in_gate_area_no_mm(unsigned long addr);
  2057. extern int in_gate_area(struct mm_struct *mm, unsigned long addr);
  2058. #else
  2059. static inline struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  2060. {
  2061. return NULL;
  2062. }
  2063. static inline int in_gate_area_no_mm(unsigned long addr) { return 0; }
  2064. static inline int in_gate_area(struct mm_struct *mm, unsigned long addr)
  2065. {
  2066. return 0;
  2067. }
  2068. #endif /* __HAVE_ARCH_GATE_AREA */
  2069. extern bool process_shares_mm(struct task_struct *p, struct mm_struct *mm);
  2070. #ifdef CONFIG_SYSCTL
  2071. extern int sysctl_drop_caches;
  2072. int drop_caches_sysctl_handler(struct ctl_table *, int,
  2073. void __user *, size_t *, loff_t *);
  2074. #endif
  2075. void drop_slab(void);
  2076. void drop_slab_node(int nid);
  2077. #ifndef CONFIG_MMU
  2078. #define randomize_va_space 0
  2079. #else
  2080. extern int randomize_va_space;
  2081. #endif
  2082. const char * arch_vma_name(struct vm_area_struct *vma);
  2083. void print_vma_addr(char *prefix, unsigned long rip);
  2084. void sparse_mem_maps_populate_node(struct page **map_map,
  2085. unsigned long pnum_begin,
  2086. unsigned long pnum_end,
  2087. unsigned long map_count,
  2088. int nodeid);
  2089. struct page *sparse_mem_map_populate(unsigned long pnum, int nid);
  2090. pgd_t *vmemmap_pgd_populate(unsigned long addr, int node);
  2091. p4d_t *vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node);
  2092. pud_t *vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node);
  2093. pmd_t *vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node);
  2094. pte_t *vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node);
  2095. void *vmemmap_alloc_block(unsigned long size, int node);
  2096. struct vmem_altmap;
  2097. void *__vmemmap_alloc_block_buf(unsigned long size, int node,
  2098. struct vmem_altmap *altmap);
  2099. static inline void *vmemmap_alloc_block_buf(unsigned long size, int node)
  2100. {
  2101. return __vmemmap_alloc_block_buf(size, node, NULL);
  2102. }
  2103. void vmemmap_verify(pte_t *, int, unsigned long, unsigned long);
  2104. int vmemmap_populate_basepages(unsigned long start, unsigned long end,
  2105. int node);
  2106. int vmemmap_populate(unsigned long start, unsigned long end, int node);
  2107. void vmemmap_populate_print_last(void);
  2108. #ifdef CONFIG_MEMORY_HOTPLUG
  2109. void vmemmap_free(unsigned long start, unsigned long end);
  2110. #endif
  2111. void register_page_bootmem_memmap(unsigned long section_nr, struct page *map,
  2112. unsigned long size);
  2113. enum mf_flags {
  2114. MF_COUNT_INCREASED = 1 << 0,
  2115. MF_ACTION_REQUIRED = 1 << 1,
  2116. MF_MUST_KILL = 1 << 2,
  2117. MF_SOFT_OFFLINE = 1 << 3,
  2118. };
  2119. extern int memory_failure(unsigned long pfn, int trapno, int flags);
  2120. extern void memory_failure_queue(unsigned long pfn, int trapno, int flags);
  2121. extern int unpoison_memory(unsigned long pfn);
  2122. extern int get_hwpoison_page(struct page *page);
  2123. #define put_hwpoison_page(page) put_page(page)
  2124. extern int sysctl_memory_failure_early_kill;
  2125. extern int sysctl_memory_failure_recovery;
  2126. extern void shake_page(struct page *p, int access);
  2127. extern atomic_long_t num_poisoned_pages;
  2128. extern int soft_offline_page(struct page *page, int flags);
  2129. /*
  2130. * Error handlers for various types of pages.
  2131. */
  2132. enum mf_result {
  2133. MF_IGNORED, /* Error: cannot be handled */
  2134. MF_FAILED, /* Error: handling failed */
  2135. MF_DELAYED, /* Will be handled later */
  2136. MF_RECOVERED, /* Successfully recovered */
  2137. };
  2138. enum mf_action_page_type {
  2139. MF_MSG_KERNEL,
  2140. MF_MSG_KERNEL_HIGH_ORDER,
  2141. MF_MSG_SLAB,
  2142. MF_MSG_DIFFERENT_COMPOUND,
  2143. MF_MSG_POISONED_HUGE,
  2144. MF_MSG_HUGE,
  2145. MF_MSG_FREE_HUGE,
  2146. MF_MSG_UNMAP_FAILED,
  2147. MF_MSG_DIRTY_SWAPCACHE,
  2148. MF_MSG_CLEAN_SWAPCACHE,
  2149. MF_MSG_DIRTY_MLOCKED_LRU,
  2150. MF_MSG_CLEAN_MLOCKED_LRU,
  2151. MF_MSG_DIRTY_UNEVICTABLE_LRU,
  2152. MF_MSG_CLEAN_UNEVICTABLE_LRU,
  2153. MF_MSG_DIRTY_LRU,
  2154. MF_MSG_CLEAN_LRU,
  2155. MF_MSG_TRUNCATED_LRU,
  2156. MF_MSG_BUDDY,
  2157. MF_MSG_BUDDY_2ND,
  2158. MF_MSG_UNKNOWN,
  2159. };
  2160. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  2161. extern void clear_huge_page(struct page *page,
  2162. unsigned long addr_hint,
  2163. unsigned int pages_per_huge_page);
  2164. extern void copy_user_huge_page(struct page *dst, struct page *src,
  2165. unsigned long addr, struct vm_area_struct *vma,
  2166. unsigned int pages_per_huge_page);
  2167. extern long copy_huge_page_from_user(struct page *dst_page,
  2168. const void __user *usr_src,
  2169. unsigned int pages_per_huge_page,
  2170. bool allow_pagefault);
  2171. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
  2172. extern struct page_ext_operations debug_guardpage_ops;
  2173. #ifdef CONFIG_DEBUG_PAGEALLOC
  2174. extern unsigned int _debug_guardpage_minorder;
  2175. extern bool _debug_guardpage_enabled;
  2176. static inline unsigned int debug_guardpage_minorder(void)
  2177. {
  2178. return _debug_guardpage_minorder;
  2179. }
  2180. static inline bool debug_guardpage_enabled(void)
  2181. {
  2182. return _debug_guardpage_enabled;
  2183. }
  2184. static inline bool page_is_guard(struct page *page)
  2185. {
  2186. struct page_ext *page_ext;
  2187. if (!debug_guardpage_enabled())
  2188. return false;
  2189. page_ext = lookup_page_ext(page);
  2190. if (unlikely(!page_ext))
  2191. return false;
  2192. return test_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
  2193. }
  2194. #else
  2195. static inline unsigned int debug_guardpage_minorder(void) { return 0; }
  2196. static inline bool debug_guardpage_enabled(void) { return false; }
  2197. static inline bool page_is_guard(struct page *page) { return false; }
  2198. #endif /* CONFIG_DEBUG_PAGEALLOC */
  2199. #if MAX_NUMNODES > 1
  2200. void __init setup_nr_node_ids(void);
  2201. #else
  2202. static inline void setup_nr_node_ids(void) {}
  2203. #endif
  2204. #endif /* __KERNEL__ */
  2205. #endif /* _LINUX_MM_H */