mm.h 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_MM_H
  3. #define _LINUX_MM_H
  4. #include <linux/errno.h>
  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/mmap_lock.h>
  15. #include <linux/range.h>
  16. #include <linux/pfn.h>
  17. #include <linux/percpu-refcount.h>
  18. #include <linux/bit_spinlock.h>
  19. #include <linux/shrinker.h>
  20. #include <linux/resource.h>
  21. #include <linux/page_ext.h>
  22. #include <linux/err.h>
  23. #include <linux/page-flags.h>
  24. #include <linux/page_ref.h>
  25. #include <linux/overflow.h>
  26. #include <linux/sizes.h>
  27. #include <linux/sched.h>
  28. #include <linux/pgtable.h>
  29. #include <linux/kasan.h>
  30. #include <linux/page_pinner.h>
  31. #include <linux/memremap.h>
  32. #include <linux/android_kabi.h>
  33. struct mempolicy;
  34. struct anon_vma;
  35. struct anon_vma_chain;
  36. struct user_struct;
  37. struct pt_regs;
  38. extern int sysctl_page_lock_unfairness;
  39. void init_mm_internals(void);
  40. #ifndef CONFIG_NUMA /* Don't use mapnrs, do it properly */
  41. extern unsigned long max_mapnr;
  42. static inline void set_max_mapnr(unsigned long limit)
  43. {
  44. max_mapnr = limit;
  45. }
  46. #else
  47. static inline void set_max_mapnr(unsigned long limit) { }
  48. #endif
  49. extern atomic_long_t _totalram_pages;
  50. static inline unsigned long totalram_pages(void)
  51. {
  52. return (unsigned long)atomic_long_read(&_totalram_pages);
  53. }
  54. static inline void totalram_pages_inc(void)
  55. {
  56. atomic_long_inc(&_totalram_pages);
  57. }
  58. static inline void totalram_pages_dec(void)
  59. {
  60. atomic_long_dec(&_totalram_pages);
  61. }
  62. static inline void totalram_pages_add(long count)
  63. {
  64. atomic_long_add(count, &_totalram_pages);
  65. }
  66. extern void * high_memory;
  67. extern int page_cluster;
  68. #ifdef CONFIG_SYSCTL
  69. extern int sysctl_legacy_va_layout;
  70. #else
  71. #define sysctl_legacy_va_layout 0
  72. #endif
  73. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
  74. extern const int mmap_rnd_bits_min;
  75. extern const int mmap_rnd_bits_max;
  76. extern int mmap_rnd_bits __read_mostly;
  77. #endif
  78. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
  79. extern const int mmap_rnd_compat_bits_min;
  80. extern const int mmap_rnd_compat_bits_max;
  81. extern int mmap_rnd_compat_bits __read_mostly;
  82. #endif
  83. #include <asm/page.h>
  84. #include <asm/processor.h>
  85. /*
  86. * Architectures that support memory tagging (assigning tags to memory regions,
  87. * embedding these tags into addresses that point to these memory regions, and
  88. * checking that the memory and the pointer tags match on memory accesses)
  89. * redefine this macro to strip tags from pointers.
  90. * It's defined as noop for architectures that don't support memory tagging.
  91. */
  92. #ifndef untagged_addr
  93. #define untagged_addr(addr) (addr)
  94. #endif
  95. #ifndef __pa_symbol
  96. #define __pa_symbol(x) __pa(RELOC_HIDE((unsigned long)(x), 0))
  97. #endif
  98. #ifndef page_to_virt
  99. #define page_to_virt(x) __va(PFN_PHYS(page_to_pfn(x)))
  100. #endif
  101. #ifndef lm_alias
  102. #define lm_alias(x) __va(__pa_symbol(x))
  103. #endif
  104. /*
  105. * To prevent common memory management code establishing
  106. * a zero page mapping on a read fault.
  107. * This macro should be defined within <asm/pgtable.h>.
  108. * s390 does this to prevent multiplexing of hardware bits
  109. * related to the physical page in case of virtualization.
  110. */
  111. #ifndef mm_forbids_zeropage
  112. #define mm_forbids_zeropage(X) (0)
  113. #endif
  114. /*
  115. * On some architectures it is expensive to call memset() for small sizes.
  116. * If an architecture decides to implement their own version of
  117. * mm_zero_struct_page they should wrap the defines below in a #ifndef and
  118. * define their own version of this macro in <asm/pgtable.h>
  119. */
  120. #if BITS_PER_LONG == 64
  121. /* This function must be updated when the size of struct page grows above 96
  122. * or reduces below 56. The idea that compiler optimizes out switch()
  123. * statement, and only leaves move/store instructions. Also the compiler can
  124. * combine write statements if they are both assignments and can be reordered,
  125. * this can result in several of the writes here being dropped.
  126. */
  127. #define mm_zero_struct_page(pp) __mm_zero_struct_page(pp)
  128. static inline void __mm_zero_struct_page(struct page *page)
  129. {
  130. unsigned long *_pp = (void *)page;
  131. /* Check that struct page is either 56, 64, 72, 80, 88 or 96 bytes */
  132. BUILD_BUG_ON(sizeof(struct page) & 7);
  133. BUILD_BUG_ON(sizeof(struct page) < 56);
  134. BUILD_BUG_ON(sizeof(struct page) > 96);
  135. switch (sizeof(struct page)) {
  136. case 96:
  137. _pp[11] = 0;
  138. fallthrough;
  139. case 88:
  140. _pp[10] = 0;
  141. fallthrough;
  142. case 80:
  143. _pp[9] = 0;
  144. fallthrough;
  145. case 72:
  146. _pp[8] = 0;
  147. fallthrough;
  148. case 64:
  149. _pp[7] = 0;
  150. fallthrough;
  151. case 56:
  152. _pp[6] = 0;
  153. _pp[5] = 0;
  154. _pp[4] = 0;
  155. _pp[3] = 0;
  156. _pp[2] = 0;
  157. _pp[1] = 0;
  158. _pp[0] = 0;
  159. }
  160. }
  161. #else
  162. #define mm_zero_struct_page(pp) ((void)memset((pp), 0, sizeof(struct page)))
  163. #endif
  164. /*
  165. * Default maximum number of active map areas, this limits the number of vmas
  166. * per mm struct. Users can overwrite this number by sysctl but there is a
  167. * problem.
  168. *
  169. * When a program's coredump is generated as ELF format, a section is created
  170. * per a vma. In ELF, the number of sections is represented in unsigned short.
  171. * This means the number of sections should be smaller than 65535 at coredump.
  172. * Because the kernel adds some informative sections to a image of program at
  173. * generating coredump, we need some margin. The number of extra sections is
  174. * 1-3 now and depends on arch. We use "5" as safe margin, here.
  175. *
  176. * ELF extended numbering allows more than 65535 sections, so 16-bit bound is
  177. * not a hard limit any more. Although some userspace tools can be surprised by
  178. * that.
  179. */
  180. #define MAPCOUNT_ELF_CORE_MARGIN (5)
  181. #define DEFAULT_MAX_MAP_COUNT (USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN)
  182. extern int sysctl_max_map_count;
  183. extern unsigned long sysctl_user_reserve_kbytes;
  184. extern unsigned long sysctl_admin_reserve_kbytes;
  185. extern int sysctl_overcommit_memory;
  186. extern int sysctl_overcommit_ratio;
  187. extern unsigned long sysctl_overcommit_kbytes;
  188. int overcommit_ratio_handler(struct ctl_table *, int, void *, size_t *,
  189. loff_t *);
  190. int overcommit_kbytes_handler(struct ctl_table *, int, void *, size_t *,
  191. loff_t *);
  192. int overcommit_policy_handler(struct ctl_table *, int, void *, size_t *,
  193. loff_t *);
  194. #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
  195. #define nth_page(page,n) pfn_to_page(page_to_pfn((page)) + (n))
  196. #define folio_page_idx(folio, p) (page_to_pfn(p) - folio_pfn(folio))
  197. #else
  198. #define nth_page(page,n) ((page) + (n))
  199. #define folio_page_idx(folio, p) ((p) - &(folio)->page)
  200. #endif
  201. /* to align the pointer to the (next) page boundary */
  202. #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
  203. /* to align the pointer to the (prev) page boundary */
  204. #define PAGE_ALIGN_DOWN(addr) ALIGN_DOWN(addr, PAGE_SIZE)
  205. /* test whether an address (unsigned long or pointer) is aligned to PAGE_SIZE */
  206. #define PAGE_ALIGNED(addr) IS_ALIGNED((unsigned long)(addr), PAGE_SIZE)
  207. #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
  208. static inline struct folio *lru_to_folio(struct list_head *head)
  209. {
  210. return list_entry((head)->prev, struct folio, lru);
  211. }
  212. void setup_initial_init_mm(void *start_code, void *end_code,
  213. void *end_data, void *brk);
  214. /*
  215. * Linux kernel virtual memory manager primitives.
  216. * The idea being to have a "virtual" mm in the same way
  217. * we have a virtual fs - giving a cleaner interface to the
  218. * mm details, and allowing different kinds of memory mappings
  219. * (from shared memory to executable loading to arbitrary
  220. * mmap() functions).
  221. */
  222. struct vm_area_struct *vm_area_alloc(struct mm_struct *);
  223. struct vm_area_struct *vm_area_dup(struct vm_area_struct *);
  224. void vm_area_free(struct vm_area_struct *);
  225. /* Use only if VMA has no other users */
  226. void __vm_area_free(struct vm_area_struct *vma);
  227. #ifndef CONFIG_MMU
  228. extern struct rb_root nommu_region_tree;
  229. extern struct rw_semaphore nommu_region_sem;
  230. extern unsigned int kobjsize(const void *objp);
  231. #endif
  232. /*
  233. * vm_flags in vm_area_struct, see mm_types.h.
  234. * When changing, update also include/trace/events/mmflags.h
  235. */
  236. #define VM_NONE 0x00000000
  237. #define VM_READ 0x00000001 /* currently active flags */
  238. #define VM_WRITE 0x00000002
  239. #define VM_EXEC 0x00000004
  240. #define VM_SHARED 0x00000008
  241. /* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
  242. #define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
  243. #define VM_MAYWRITE 0x00000020
  244. #define VM_MAYEXEC 0x00000040
  245. #define VM_MAYSHARE 0x00000080
  246. #define VM_GROWSDOWN 0x00000100 /* general info on the segment */
  247. #define VM_UFFD_MISSING 0x00000200 /* missing pages tracking */
  248. #define VM_PFNMAP 0x00000400 /* Page-ranges managed without "struct page", just pure PFN */
  249. #define VM_UFFD_WP 0x00001000 /* wrprotect pages tracking */
  250. #define VM_LOCKED 0x00002000
  251. #define VM_IO 0x00004000 /* Memory mapped I/O or similar */
  252. /* Used by sys_madvise() */
  253. #define VM_SEQ_READ 0x00008000 /* App will access data sequentially */
  254. #define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */
  255. #define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */
  256. #define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */
  257. #define VM_LOCKONFAULT 0x00080000 /* Lock the pages covered when they are faulted in */
  258. #define VM_ACCOUNT 0x00100000 /* Is a VM accounted object */
  259. #define VM_NORESERVE 0x00200000 /* should the VM suppress accounting */
  260. #define VM_HUGETLB 0x00400000 /* Huge TLB Page VM */
  261. #define VM_SYNC 0x00800000 /* Synchronous page faults */
  262. #define VM_ARCH_1 0x01000000 /* Architecture-specific flag */
  263. #define VM_WIPEONFORK 0x02000000 /* Wipe VMA contents in child. */
  264. #define VM_DONTDUMP 0x04000000 /* Do not include in the core dump */
  265. #ifdef CONFIG_MEM_SOFT_DIRTY
  266. # define VM_SOFTDIRTY 0x08000000 /* Not soft dirty clean area */
  267. #else
  268. # define VM_SOFTDIRTY 0
  269. #endif
  270. #define VM_MIXEDMAP 0x10000000 /* Can contain "struct page" and pure PFN pages */
  271. #define VM_HUGEPAGE 0x20000000 /* MADV_HUGEPAGE marked this vma */
  272. #define VM_NOHUGEPAGE 0x40000000 /* MADV_NOHUGEPAGE marked this vma */
  273. #define VM_MERGEABLE 0x80000000 /* KSM may merge identical pages */
  274. #ifdef CONFIG_ARCH_USES_HIGH_VMA_FLAGS
  275. #define VM_HIGH_ARCH_BIT_0 32 /* bit only usable on 64-bit architectures */
  276. #define VM_HIGH_ARCH_BIT_1 33 /* bit only usable on 64-bit architectures */
  277. #define VM_HIGH_ARCH_BIT_2 34 /* bit only usable on 64-bit architectures */
  278. #define VM_HIGH_ARCH_BIT_3 35 /* bit only usable on 64-bit architectures */
  279. #define VM_HIGH_ARCH_BIT_4 36 /* bit only usable on 64-bit architectures */
  280. #define VM_HIGH_ARCH_0 BIT(VM_HIGH_ARCH_BIT_0)
  281. #define VM_HIGH_ARCH_1 BIT(VM_HIGH_ARCH_BIT_1)
  282. #define VM_HIGH_ARCH_2 BIT(VM_HIGH_ARCH_BIT_2)
  283. #define VM_HIGH_ARCH_3 BIT(VM_HIGH_ARCH_BIT_3)
  284. #define VM_HIGH_ARCH_4 BIT(VM_HIGH_ARCH_BIT_4)
  285. #endif /* CONFIG_ARCH_USES_HIGH_VMA_FLAGS */
  286. #ifdef CONFIG_ARCH_HAS_PKEYS
  287. # define VM_PKEY_SHIFT VM_HIGH_ARCH_BIT_0
  288. # define VM_PKEY_BIT0 VM_HIGH_ARCH_0 /* A protection key is a 4-bit value */
  289. # define VM_PKEY_BIT1 VM_HIGH_ARCH_1 /* on x86 and 5-bit value on ppc64 */
  290. # define VM_PKEY_BIT2 VM_HIGH_ARCH_2
  291. # define VM_PKEY_BIT3 VM_HIGH_ARCH_3
  292. #ifdef CONFIG_PPC
  293. # define VM_PKEY_BIT4 VM_HIGH_ARCH_4
  294. #else
  295. # define VM_PKEY_BIT4 0
  296. #endif
  297. #endif /* CONFIG_ARCH_HAS_PKEYS */
  298. #if defined(CONFIG_X86)
  299. # define VM_PAT VM_ARCH_1 /* PAT reserves whole VMA at once (x86) */
  300. #elif defined(CONFIG_PPC)
  301. # define VM_SAO VM_ARCH_1 /* Strong Access Ordering (powerpc) */
  302. #elif defined(CONFIG_PARISC)
  303. # define VM_GROWSUP VM_ARCH_1
  304. #elif defined(CONFIG_IA64)
  305. # define VM_GROWSUP VM_ARCH_1
  306. #elif defined(CONFIG_SPARC64)
  307. # define VM_SPARC_ADI VM_ARCH_1 /* Uses ADI tag for access control */
  308. # define VM_ARCH_CLEAR VM_SPARC_ADI
  309. #elif defined(CONFIG_ARM64)
  310. # define VM_ARM64_BTI VM_ARCH_1 /* BTI guarded page, a.k.a. GP bit */
  311. # define VM_ARCH_CLEAR VM_ARM64_BTI
  312. #elif !defined(CONFIG_MMU)
  313. # define VM_MAPPED_COPY VM_ARCH_1 /* T if mapped copy of data (nommu mmap) */
  314. #endif
  315. #if defined(CONFIG_ARM64_MTE)
  316. # define VM_MTE VM_HIGH_ARCH_0 /* Use Tagged memory for access control */
  317. # define VM_MTE_ALLOWED VM_HIGH_ARCH_1 /* Tagged memory permitted */
  318. #else
  319. # define VM_MTE VM_NONE
  320. # define VM_MTE_ALLOWED VM_NONE
  321. #endif
  322. #ifndef VM_GROWSUP
  323. # define VM_GROWSUP VM_NONE
  324. #endif
  325. #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR
  326. # define VM_UFFD_MINOR_BIT 37
  327. # define VM_UFFD_MINOR BIT(VM_UFFD_MINOR_BIT) /* UFFD minor faults */
  328. #else /* !CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
  329. # define VM_UFFD_MINOR VM_NONE
  330. #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */
  331. /* Bits set in the VMA until the stack is in its final location */
  332. #define VM_STACK_INCOMPLETE_SETUP (VM_RAND_READ | VM_SEQ_READ | VM_STACK_EARLY)
  333. #define TASK_EXEC ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0)
  334. /* Common data flag combinations */
  335. #define VM_DATA_FLAGS_TSK_EXEC (VM_READ | VM_WRITE | TASK_EXEC | \
  336. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
  337. #define VM_DATA_FLAGS_NON_EXEC (VM_READ | VM_WRITE | VM_MAYREAD | \
  338. VM_MAYWRITE | VM_MAYEXEC)
  339. #define VM_DATA_FLAGS_EXEC (VM_READ | VM_WRITE | VM_EXEC | \
  340. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
  341. #ifndef VM_DATA_DEFAULT_FLAGS /* arch can override this */
  342. #define VM_DATA_DEFAULT_FLAGS VM_DATA_FLAGS_EXEC
  343. #endif
  344. #ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */
  345. #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
  346. #endif
  347. #ifdef CONFIG_STACK_GROWSUP
  348. #define VM_STACK VM_GROWSUP
  349. #define VM_STACK_EARLY VM_GROWSDOWN
  350. #else
  351. #define VM_STACK VM_GROWSDOWN
  352. #define VM_STACK_EARLY 0
  353. #endif
  354. #define VM_STACK_FLAGS (VM_STACK | VM_STACK_DEFAULT_FLAGS | VM_ACCOUNT)
  355. /* VMA basic access permission flags */
  356. #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC)
  357. /*
  358. * Special vmas that are non-mergable, non-mlock()able.
  359. */
  360. #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP)
  361. /* This mask prevents VMA from being scanned with khugepaged */
  362. #define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB)
  363. /* This mask defines which mm->def_flags a process can inherit its parent */
  364. #define VM_INIT_DEF_MASK VM_NOHUGEPAGE
  365. /* This mask represents all the VMA flag bits used by mlock */
  366. #define VM_LOCKED_MASK (VM_LOCKED | VM_LOCKONFAULT)
  367. /* Arch-specific flags to clear when updating VM flags on protection change */
  368. #ifndef VM_ARCH_CLEAR
  369. # define VM_ARCH_CLEAR VM_NONE
  370. #endif
  371. #define VM_FLAGS_CLEAR (ARCH_VM_PKEY_FLAGS | VM_ARCH_CLEAR)
  372. /*
  373. * mapping from the currently active vm_flags protection bits (the
  374. * low four bits) to a page protection mask..
  375. */
  376. /*
  377. * The default fault flags that should be used by most of the
  378. * arch-specific page fault handlers.
  379. */
  380. #define FAULT_FLAG_DEFAULT (FAULT_FLAG_ALLOW_RETRY | \
  381. FAULT_FLAG_KILLABLE | \
  382. FAULT_FLAG_INTERRUPTIBLE)
  383. /**
  384. * fault_flag_allow_retry_first - check ALLOW_RETRY the first time
  385. * @flags: Fault flags.
  386. *
  387. * This is mostly used for places where we want to try to avoid taking
  388. * the mmap_lock for too long a time when waiting for another condition
  389. * to change, in which case we can try to be polite to release the
  390. * mmap_lock in the first round to avoid potential starvation of other
  391. * processes that would also want the mmap_lock.
  392. *
  393. * Return: true if the page fault allows retry and this is the first
  394. * attempt of the fault handling; false otherwise.
  395. */
  396. static inline bool fault_flag_allow_retry_first(enum fault_flag flags)
  397. {
  398. return (flags & FAULT_FLAG_ALLOW_RETRY) &&
  399. (!(flags & FAULT_FLAG_TRIED));
  400. }
  401. #define FAULT_FLAG_TRACE \
  402. { FAULT_FLAG_WRITE, "WRITE" }, \
  403. { FAULT_FLAG_MKWRITE, "MKWRITE" }, \
  404. { FAULT_FLAG_ALLOW_RETRY, "ALLOW_RETRY" }, \
  405. { FAULT_FLAG_RETRY_NOWAIT, "RETRY_NOWAIT" }, \
  406. { FAULT_FLAG_KILLABLE, "KILLABLE" }, \
  407. { FAULT_FLAG_TRIED, "TRIED" }, \
  408. { FAULT_FLAG_USER, "USER" }, \
  409. { FAULT_FLAG_REMOTE, "REMOTE" }, \
  410. { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \
  411. { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \
  412. { FAULT_FLAG_VMA_LOCK, "VMA_LOCK" }
  413. /*
  414. * vm_fault is filled by the pagefault handler and passed to the vma's
  415. * ->fault function. The vma's ->fault is responsible for returning a bitmask
  416. * of VM_FAULT_xxx flags that give details about how the fault was handled.
  417. *
  418. * MM layer fills up gfp_mask for page allocations but fault handler might
  419. * alter it if its implementation requires a different allocation context.
  420. *
  421. * pgoff should be used in favour of virtual_address, if possible.
  422. */
  423. struct vm_fault {
  424. const struct {
  425. struct vm_area_struct *vma; /* Target VMA */
  426. gfp_t gfp_mask; /* gfp mask to be used for allocations */
  427. pgoff_t pgoff; /* Logical page offset based on vma */
  428. unsigned long address; /* Faulting virtual address - masked */
  429. unsigned long real_address; /* Faulting virtual address - unmasked */
  430. };
  431. enum fault_flag flags; /* FAULT_FLAG_xxx flags
  432. * XXX: should really be 'const' */
  433. pmd_t *pmd; /* Pointer to pmd entry matching
  434. * the 'address' */
  435. pud_t *pud; /* Pointer to pud entry matching
  436. * the 'address'
  437. */
  438. union {
  439. pte_t orig_pte; /* Value of PTE at the time of fault */
  440. pmd_t orig_pmd; /* Value of PMD at the time of fault,
  441. * used by PMD fault only.
  442. */
  443. };
  444. struct page *cow_page; /* Page handler may use for COW fault */
  445. struct page *page; /* ->fault handlers should return a
  446. * page here, unless VM_FAULT_NOPAGE
  447. * is set (which is also implied by
  448. * VM_FAULT_ERROR).
  449. */
  450. /* These three entries are valid only while holding ptl lock */
  451. pte_t *pte; /* Pointer to pte entry matching
  452. * the 'address'. NULL if the page
  453. * table hasn't been allocated.
  454. */
  455. spinlock_t *ptl; /* Page table lock.
  456. * Protects pte page table if 'pte'
  457. * is not NULL, otherwise pmd.
  458. */
  459. pgtable_t prealloc_pte; /* Pre-allocated pte page table.
  460. * vm_ops->map_pages() sets up a page
  461. * table from atomic context.
  462. * do_fault_around() pre-allocates
  463. * page table to avoid allocation from
  464. * atomic context.
  465. */
  466. };
  467. /* page entry size for vm->huge_fault() */
  468. enum page_entry_size {
  469. PE_SIZE_PTE = 0,
  470. PE_SIZE_PMD,
  471. PE_SIZE_PUD,
  472. };
  473. /*
  474. * These are the virtual MM functions - opening of an area, closing and
  475. * unmapping it (needed to keep files on disk up-to-date etc), pointer
  476. * to the functions called when a no-page or a wp-page exception occurs.
  477. */
  478. struct vm_operations_struct {
  479. void (*open)(struct vm_area_struct * area);
  480. /**
  481. * @close: Called when the VMA is being removed from the MM.
  482. * Context: User context. May sleep. Caller holds mmap_lock.
  483. */
  484. void (*close)(struct vm_area_struct * area);
  485. /* Called any time before splitting to check if it's allowed */
  486. int (*may_split)(struct vm_area_struct *area, unsigned long addr);
  487. int (*mremap)(struct vm_area_struct *area);
  488. /*
  489. * Called by mprotect() to make driver-specific permission
  490. * checks before mprotect() is finalised. The VMA must not
  491. * be modified. Returns 0 if eprotect() can proceed.
  492. */
  493. int (*mprotect)(struct vm_area_struct *vma, unsigned long start,
  494. unsigned long end, unsigned long newflags);
  495. vm_fault_t (*fault)(struct vm_fault *vmf);
  496. vm_fault_t (*huge_fault)(struct vm_fault *vmf,
  497. enum page_entry_size pe_size);
  498. vm_fault_t (*map_pages)(struct vm_fault *vmf,
  499. pgoff_t start_pgoff, pgoff_t end_pgoff);
  500. unsigned long (*pagesize)(struct vm_area_struct * area);
  501. /* notification that a previously read-only page is about to become
  502. * writable, if an error is returned it will cause a SIGBUS */
  503. vm_fault_t (*page_mkwrite)(struct vm_fault *vmf);
  504. /* same as page_mkwrite when using VM_PFNMAP|VM_MIXEDMAP */
  505. vm_fault_t (*pfn_mkwrite)(struct vm_fault *vmf);
  506. /* called by access_process_vm when get_user_pages() fails, typically
  507. * for use by special VMAs. See also generic_access_phys() for a generic
  508. * implementation useful for any iomem mapping.
  509. */
  510. int (*access)(struct vm_area_struct *vma, unsigned long addr,
  511. void *buf, int len, int write);
  512. /* Called by the /proc/PID/maps code to ask the vma whether it
  513. * has a special name. Returning non-NULL will also cause this
  514. * vma to be dumped unconditionally. */
  515. const char *(*name)(struct vm_area_struct *vma);
  516. #ifdef CONFIG_NUMA
  517. /*
  518. * set_policy() op must add a reference to any non-NULL @new mempolicy
  519. * to hold the policy upon return. Caller should pass NULL @new to
  520. * remove a policy and fall back to surrounding context--i.e. do not
  521. * install a MPOL_DEFAULT policy, nor the task or system default
  522. * mempolicy.
  523. */
  524. int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
  525. /*
  526. * get_policy() op must add reference [mpol_get()] to any policy at
  527. * (vma,addr) marked as MPOL_SHARED. The shared policy infrastructure
  528. * in mm/mempolicy.c will do this automatically.
  529. * get_policy() must NOT add a ref if the policy at (vma,addr) is not
  530. * marked as MPOL_SHARED. vma policies are protected by the mmap_lock.
  531. * If no [shared/vma] mempolicy exists at the addr, get_policy() op
  532. * must return NULL--i.e., do not "fallback" to task or system default
  533. * policy.
  534. */
  535. struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
  536. unsigned long addr);
  537. #endif
  538. /*
  539. * Called by vm_normal_page() for special PTEs to find the
  540. * page for @addr. This is useful if the default behavior
  541. * (using pte_page()) would not find the correct page.
  542. */
  543. struct page *(*find_special_page)(struct vm_area_struct *vma,
  544. unsigned long addr);
  545. ANDROID_KABI_RESERVE(1);
  546. ANDROID_KABI_RESERVE(2);
  547. ANDROID_KABI_RESERVE(3);
  548. ANDROID_KABI_RESERVE(4);
  549. };
  550. #ifdef CONFIG_PER_VMA_LOCK
  551. /*
  552. * Try to read-lock a vma. The function is allowed to occasionally yield false
  553. * locked result to avoid performance overhead, in which case we fall back to
  554. * using mmap_lock. The function should never yield false unlocked result.
  555. */
  556. static inline bool vma_start_read(struct vm_area_struct *vma)
  557. {
  558. /*
  559. * Check before locking. A race might cause false locked result.
  560. * We can use READ_ONCE() for the mm_lock_seq here, and don't need
  561. * ACQUIRE semantics, because this is just a lockless check whose result
  562. * we don't rely on for anything - the mm_lock_seq read against which we
  563. * need ordering is below.
  564. */
  565. if (READ_ONCE(vma->vm_lock_seq) == READ_ONCE(vma->vm_mm->mm_lock_seq))
  566. return false;
  567. if (unlikely(down_read_trylock(&vma->vm_lock->lock) == 0))
  568. return false;
  569. /*
  570. * Overflow might produce false locked result.
  571. * False unlocked result is impossible because we modify and check
  572. * vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq
  573. * modification invalidates all existing locks.
  574. *
  575. * We must use ACQUIRE semantics for the mm_lock_seq so that if we are
  576. * racing with vma_end_write_all(), we only start reading from the VMA
  577. * after it has been unlocked.
  578. * This pairs with RELEASE semantics in vma_end_write_all().
  579. */
  580. if (unlikely(vma->vm_lock_seq == smp_load_acquire(&vma->vm_mm->mm_lock_seq))) {
  581. up_read(&vma->vm_lock->lock);
  582. return false;
  583. }
  584. return true;
  585. }
  586. static inline void vma_end_read(struct vm_area_struct *vma)
  587. {
  588. rcu_read_lock(); /* keeps vma alive till the end of up_read */
  589. up_read(&vma->vm_lock->lock);
  590. rcu_read_unlock();
  591. }
  592. /* WARNING! Can only be used if mmap_lock is expected to be write-locked */
  593. static bool __is_vma_write_locked(struct vm_area_struct *vma, int *mm_lock_seq)
  594. {
  595. mmap_assert_write_locked(vma->vm_mm);
  596. /*
  597. * current task is holding mmap_write_lock, both vma->vm_lock_seq and
  598. * mm->mm_lock_seq can't be concurrently modified.
  599. */
  600. *mm_lock_seq = vma->vm_mm->mm_lock_seq;
  601. return (vma->vm_lock_seq == *mm_lock_seq);
  602. }
  603. /*
  604. * Begin writing to a VMA.
  605. * Exclude concurrent readers under the per-VMA lock until the currently
  606. * write-locked mmap_lock is dropped or downgraded.
  607. */
  608. static inline void vma_start_write(struct vm_area_struct *vma)
  609. {
  610. int mm_lock_seq;
  611. if (__is_vma_write_locked(vma, &mm_lock_seq))
  612. return;
  613. down_write(&vma->vm_lock->lock);
  614. /*
  615. * We should use WRITE_ONCE() here because we can have concurrent reads
  616. * from the early lockless pessimistic check in vma_start_read().
  617. * We don't really care about the correctness of that early check, but
  618. * we should use WRITE_ONCE() for cleanliness and to keep KCSAN happy.
  619. */
  620. WRITE_ONCE(vma->vm_lock_seq, mm_lock_seq);
  621. up_write(&vma->vm_lock->lock);
  622. }
  623. static inline bool vma_try_start_write(struct vm_area_struct *vma)
  624. {
  625. int mm_lock_seq;
  626. if (__is_vma_write_locked(vma, &mm_lock_seq))
  627. return true;
  628. if (!down_write_trylock(&vma->vm_lock->lock))
  629. return false;
  630. WRITE_ONCE(vma->vm_lock_seq, mm_lock_seq);
  631. up_write(&vma->vm_lock->lock);
  632. return true;
  633. }
  634. static inline void vma_assert_write_locked(struct vm_area_struct *vma)
  635. {
  636. int mm_lock_seq;
  637. VM_BUG_ON_VMA(!__is_vma_write_locked(vma, &mm_lock_seq), vma);
  638. }
  639. static inline void vma_assert_locked(struct vm_area_struct *vma)
  640. {
  641. if (!rwsem_is_locked(&vma->vm_lock->lock))
  642. vma_assert_write_locked(vma);
  643. }
  644. static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached)
  645. {
  646. /* When detaching vma should be write-locked */
  647. if (detached)
  648. vma_assert_write_locked(vma);
  649. vma->detached = detached;
  650. }
  651. static inline void release_fault_lock(struct vm_fault *vmf)
  652. {
  653. if (vmf->flags & FAULT_FLAG_VMA_LOCK)
  654. vma_end_read(vmf->vma);
  655. else
  656. mmap_read_unlock(vmf->vma->vm_mm);
  657. }
  658. static inline void assert_fault_locked(struct vm_fault *vmf)
  659. {
  660. if (vmf->flags & FAULT_FLAG_VMA_LOCK)
  661. vma_assert_locked(vmf->vma);
  662. else
  663. mmap_assert_locked(vmf->vma->vm_mm);
  664. }
  665. struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
  666. unsigned long address);
  667. #else /* CONFIG_PER_VMA_LOCK */
  668. static inline void vma_init_lock(struct vm_area_struct *vma) {}
  669. static inline bool vma_start_read(struct vm_area_struct *vma)
  670. { return false; }
  671. static inline void vma_end_read(struct vm_area_struct *vma) {}
  672. static inline void vma_start_write(struct vm_area_struct *vma) {}
  673. static inline bool vma_try_start_write(struct vm_area_struct *vma)
  674. { return true; }
  675. static inline void vma_assert_write_locked(struct vm_area_struct *vma)
  676. { mmap_assert_write_locked(vma->vm_mm); }
  677. static inline void vma_mark_detached(struct vm_area_struct *vma,
  678. bool detached) {}
  679. static inline void release_fault_lock(struct vm_fault *vmf)
  680. {
  681. mmap_read_unlock(vmf->vma->vm_mm);
  682. }
  683. static inline void assert_fault_locked(struct vm_fault *vmf)
  684. {
  685. mmap_assert_locked(vmf->vma->vm_mm);
  686. }
  687. static inline struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
  688. unsigned long address)
  689. {
  690. return NULL;
  691. }
  692. #endif /* CONFIG_PER_VMA_LOCK */
  693. /*
  694. * WARNING: vma_init does not initialize vma->vm_lock.
  695. * Use vm_area_alloc()/vm_area_free() if vma needs locking.
  696. */
  697. static inline void vma_init(struct vm_area_struct *vma, struct mm_struct *mm)
  698. {
  699. static const struct vm_operations_struct dummy_vm_ops = {};
  700. memset(vma, 0, sizeof(*vma));
  701. vma->vm_mm = mm;
  702. vma->vm_ops = &dummy_vm_ops;
  703. INIT_LIST_HEAD(&vma->anon_vma_chain);
  704. vma_mark_detached(vma, false);
  705. }
  706. /* Use when VMA is not part of the VMA tree and needs no locking */
  707. static inline void vm_flags_init(struct vm_area_struct *vma,
  708. vm_flags_t flags)
  709. {
  710. ACCESS_PRIVATE(vma, __vm_flags) = flags;
  711. }
  712. /*
  713. * Use when VMA is part of the VMA tree and modifications need coordination
  714. * Note: vm_flags_reset and vm_flags_reset_once do not lock the vma and
  715. * it should be locked explicitly beforehand.
  716. */
  717. static inline void vm_flags_reset(struct vm_area_struct *vma,
  718. vm_flags_t flags)
  719. {
  720. vma_assert_write_locked(vma);
  721. vm_flags_init(vma, flags);
  722. }
  723. static inline void vm_flags_reset_once(struct vm_area_struct *vma,
  724. vm_flags_t flags)
  725. {
  726. vma_assert_write_locked(vma);
  727. WRITE_ONCE(ACCESS_PRIVATE(vma, __vm_flags), flags);
  728. }
  729. static inline void vm_flags_set(struct vm_area_struct *vma,
  730. vm_flags_t flags)
  731. {
  732. vma_start_write(vma);
  733. ACCESS_PRIVATE(vma, __vm_flags) |= flags;
  734. }
  735. static inline void vm_flags_clear(struct vm_area_struct *vma,
  736. vm_flags_t flags)
  737. {
  738. vma_start_write(vma);
  739. ACCESS_PRIVATE(vma, __vm_flags) &= ~flags;
  740. }
  741. /*
  742. * Use only if VMA is not part of the VMA tree or has no other users and
  743. * therefore needs no locking.
  744. */
  745. static inline void __vm_flags_mod(struct vm_area_struct *vma,
  746. vm_flags_t set, vm_flags_t clear)
  747. {
  748. vm_flags_init(vma, (vma->vm_flags | set) & ~clear);
  749. }
  750. /*
  751. * Use only when the order of set/clear operations is unimportant, otherwise
  752. * use vm_flags_{set|clear} explicitly.
  753. */
  754. static inline void vm_flags_mod(struct vm_area_struct *vma,
  755. vm_flags_t set, vm_flags_t clear)
  756. {
  757. vma_start_write(vma);
  758. __vm_flags_mod(vma, set, clear);
  759. }
  760. static inline void vma_set_anonymous(struct vm_area_struct *vma)
  761. {
  762. vma->vm_ops = NULL;
  763. }
  764. static inline bool vma_is_anonymous(struct vm_area_struct *vma)
  765. {
  766. return !vma->vm_ops;
  767. }
  768. static inline bool vma_is_temporary_stack(struct vm_area_struct *vma)
  769. {
  770. int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP);
  771. if (!maybe_stack)
  772. return false;
  773. if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) ==
  774. VM_STACK_INCOMPLETE_SETUP)
  775. return true;
  776. return false;
  777. }
  778. static inline bool vma_is_foreign(struct vm_area_struct *vma)
  779. {
  780. if (!current->mm)
  781. return true;
  782. if (current->mm != vma->vm_mm)
  783. return true;
  784. return false;
  785. }
  786. static inline bool vma_is_accessible(struct vm_area_struct *vma)
  787. {
  788. return vma->vm_flags & VM_ACCESS_FLAGS;
  789. }
  790. static inline
  791. struct vm_area_struct *vma_find(struct vma_iterator *vmi, unsigned long max)
  792. {
  793. return mas_find(&vmi->mas, max);
  794. }
  795. static inline struct vm_area_struct *vma_next(struct vma_iterator *vmi)
  796. {
  797. /*
  798. * Uses vma_find() to get the first VMA when the iterator starts.
  799. * Calling mas_next() could skip the first entry.
  800. */
  801. return vma_find(vmi, ULONG_MAX);
  802. }
  803. static inline struct vm_area_struct *vma_prev(struct vma_iterator *vmi)
  804. {
  805. return mas_prev(&vmi->mas, 0);
  806. }
  807. static inline unsigned long vma_iter_addr(struct vma_iterator *vmi)
  808. {
  809. return vmi->mas.index;
  810. }
  811. #define for_each_vma(__vmi, __vma) \
  812. while (((__vma) = vma_next(&(__vmi))) != NULL)
  813. /* The MM code likes to work with exclusive end addresses */
  814. #define for_each_vma_range(__vmi, __vma, __end) \
  815. while (((__vma) = vma_find(&(__vmi), (__end) - 1)) != NULL)
  816. #ifdef CONFIG_SHMEM
  817. /*
  818. * The vma_is_shmem is not inline because it is used only by slow
  819. * paths in userfault.
  820. */
  821. bool vma_is_shmem(struct vm_area_struct *vma);
  822. #else
  823. static inline bool vma_is_shmem(struct vm_area_struct *vma) { return false; }
  824. #endif
  825. int vma_is_stack_for_current(struct vm_area_struct *vma);
  826. /* flush_tlb_range() takes a vma, not a mm, and can care about flags */
  827. #define TLB_FLUSH_VMA(mm,flags) { .vm_mm = (mm), .vm_flags = (flags) }
  828. struct mmu_gather;
  829. struct inode;
  830. static inline unsigned int compound_order(struct page *page)
  831. {
  832. if (!PageHead(page))
  833. return 0;
  834. return page[1].compound_order;
  835. }
  836. /**
  837. * folio_order - The allocation order of a folio.
  838. * @folio: The folio.
  839. *
  840. * A folio is composed of 2^order pages. See get_order() for the definition
  841. * of order.
  842. *
  843. * Return: The order of the folio.
  844. */
  845. static inline unsigned int folio_order(struct folio *folio)
  846. {
  847. if (!folio_test_large(folio))
  848. return 0;
  849. return folio->_folio_order;
  850. }
  851. #include <linux/huge_mm.h>
  852. /*
  853. * Methods to modify the page usage count.
  854. *
  855. * What counts for a page usage:
  856. * - cache mapping (page->mapping)
  857. * - private data (page->private)
  858. * - page mapped in a task's page tables, each mapping
  859. * is counted separately
  860. *
  861. * Also, many kernel routines increase the page count before a critical
  862. * routine so they can be sure the page doesn't go away from under them.
  863. */
  864. /*
  865. * Drop a ref, return true if the refcount fell to zero (the page has no users)
  866. */
  867. static inline int put_page_testzero(struct page *page)
  868. {
  869. int ret;
  870. VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
  871. ret = page_ref_dec_and_test(page);
  872. page_pinner_put_page(page);
  873. return ret;
  874. }
  875. static inline int folio_put_testzero(struct folio *folio)
  876. {
  877. return put_page_testzero(&folio->page);
  878. }
  879. /*
  880. * Try to grab a ref unless the page has a refcount of zero, return false if
  881. * that is the case.
  882. * This can be called when MMU is off so it must not access
  883. * any of the virtual mappings.
  884. */
  885. static inline bool get_page_unless_zero(struct page *page)
  886. {
  887. return page_ref_add_unless(page, 1, 0);
  888. }
  889. extern int page_is_ram(unsigned long pfn);
  890. enum {
  891. REGION_INTERSECTS,
  892. REGION_DISJOINT,
  893. REGION_MIXED,
  894. };
  895. int region_intersects(resource_size_t offset, size_t size, unsigned long flags,
  896. unsigned long desc);
  897. /* Support for virtually mapped pages */
  898. struct page *vmalloc_to_page(const void *addr);
  899. unsigned long vmalloc_to_pfn(const void *addr);
  900. /*
  901. * Determine if an address is within the vmalloc range
  902. *
  903. * On nommu, vmalloc/vfree wrap through kmalloc/kfree directly, so there
  904. * is no special casing required.
  905. */
  906. #ifndef is_ioremap_addr
  907. #define is_ioremap_addr(x) is_vmalloc_addr(x)
  908. #endif
  909. #ifdef CONFIG_MMU
  910. extern bool is_vmalloc_addr(const void *x);
  911. extern int is_vmalloc_or_module_addr(const void *x);
  912. #else
  913. static inline bool is_vmalloc_addr(const void *x)
  914. {
  915. return false;
  916. }
  917. static inline int is_vmalloc_or_module_addr(const void *x)
  918. {
  919. return 0;
  920. }
  921. #endif
  922. /*
  923. * How many times the entire folio is mapped as a single unit (eg by a
  924. * PMD or PUD entry). This is probably not what you want, except for
  925. * debugging purposes; look at folio_mapcount() or page_mapcount()
  926. * instead.
  927. */
  928. static inline int folio_entire_mapcount(struct folio *folio)
  929. {
  930. VM_BUG_ON_FOLIO(!folio_test_large(folio), folio);
  931. return atomic_read(folio_mapcount_ptr(folio)) + 1;
  932. }
  933. /*
  934. * Mapcount of compound page as a whole, does not include mapped sub-pages.
  935. *
  936. * Must be called only for compound pages.
  937. */
  938. static inline int compound_mapcount(struct page *page)
  939. {
  940. return folio_entire_mapcount(page_folio(page));
  941. }
  942. /*
  943. * The atomic page->_mapcount, starts from -1: so that transitions
  944. * both from it and to it can be tracked, using atomic_inc_and_test
  945. * and atomic_add_negative(-1).
  946. */
  947. static inline void page_mapcount_reset(struct page *page)
  948. {
  949. atomic_set(&(page)->_mapcount, -1);
  950. }
  951. int __page_mapcount(struct page *page);
  952. /*
  953. * Mapcount of 0-order page; when compound sub-page, includes
  954. * compound_mapcount().
  955. *
  956. * Result is undefined for pages which cannot be mapped into userspace.
  957. * For example SLAB or special types of pages. See function page_has_type().
  958. * They use this place in struct page differently.
  959. */
  960. static inline int page_mapcount(struct page *page)
  961. {
  962. if (unlikely(PageCompound(page)))
  963. return __page_mapcount(page);
  964. return atomic_read(&page->_mapcount) + 1;
  965. }
  966. int folio_mapcount(struct folio *folio);
  967. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  968. static inline int total_mapcount(struct page *page)
  969. {
  970. return folio_mapcount(page_folio(page));
  971. }
  972. #else
  973. static inline int total_mapcount(struct page *page)
  974. {
  975. return page_mapcount(page);
  976. }
  977. #endif
  978. static inline struct page *virt_to_head_page(const void *x)
  979. {
  980. struct page *page = virt_to_page(x);
  981. return compound_head(page);
  982. }
  983. static inline struct folio *virt_to_folio(const void *x)
  984. {
  985. struct page *page = virt_to_page(x);
  986. return page_folio(page);
  987. }
  988. void __folio_put(struct folio *folio);
  989. void put_pages_list(struct list_head *pages);
  990. void split_page(struct page *page, unsigned int order);
  991. void folio_copy(struct folio *dst, struct folio *src);
  992. unsigned long nr_free_buffer_pages(void);
  993. /*
  994. * Compound pages have a destructor function. Provide a
  995. * prototype for that function and accessor functions.
  996. * These are _only_ valid on the head of a compound page.
  997. */
  998. typedef void compound_page_dtor(struct page *);
  999. /* Keep the enum in sync with compound_page_dtors array in mm/page_alloc.c */
  1000. enum compound_dtor_id {
  1001. NULL_COMPOUND_DTOR,
  1002. COMPOUND_PAGE_DTOR,
  1003. #ifdef CONFIG_HUGETLB_PAGE
  1004. HUGETLB_PAGE_DTOR,
  1005. #endif
  1006. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1007. TRANSHUGE_PAGE_DTOR,
  1008. #endif
  1009. NR_COMPOUND_DTORS,
  1010. };
  1011. extern compound_page_dtor * const compound_page_dtors[NR_COMPOUND_DTORS];
  1012. static inline void set_compound_page_dtor(struct page *page,
  1013. enum compound_dtor_id compound_dtor)
  1014. {
  1015. VM_BUG_ON_PAGE(compound_dtor >= NR_COMPOUND_DTORS, page);
  1016. page[1].compound_dtor = compound_dtor;
  1017. }
  1018. void destroy_large_folio(struct folio *folio);
  1019. static inline int head_compound_pincount(struct page *head)
  1020. {
  1021. return atomic_read(compound_pincount_ptr(head));
  1022. }
  1023. static inline void set_compound_order(struct page *page, unsigned int order)
  1024. {
  1025. page[1].compound_order = order;
  1026. #ifdef CONFIG_64BIT
  1027. page[1].compound_nr = 1U << order;
  1028. #endif
  1029. }
  1030. /* Returns the number of pages in this potentially compound page. */
  1031. static inline unsigned long compound_nr(struct page *page)
  1032. {
  1033. if (!PageHead(page))
  1034. return 1;
  1035. #ifdef CONFIG_64BIT
  1036. return page[1].compound_nr;
  1037. #else
  1038. return 1UL << compound_order(page);
  1039. #endif
  1040. }
  1041. /* Returns the number of bytes in this potentially compound page. */
  1042. static inline unsigned long page_size(struct page *page)
  1043. {
  1044. return PAGE_SIZE << compound_order(page);
  1045. }
  1046. /* Returns the number of bits needed for the number of bytes in a page */
  1047. static inline unsigned int page_shift(struct page *page)
  1048. {
  1049. return PAGE_SHIFT + compound_order(page);
  1050. }
  1051. /**
  1052. * thp_order - Order of a transparent huge page.
  1053. * @page: Head page of a transparent huge page.
  1054. */
  1055. static inline unsigned int thp_order(struct page *page)
  1056. {
  1057. VM_BUG_ON_PGFLAGS(PageTail(page), page);
  1058. return compound_order(page);
  1059. }
  1060. /**
  1061. * thp_nr_pages - The number of regular pages in this huge page.
  1062. * @page: The head page of a huge page.
  1063. */
  1064. static inline int thp_nr_pages(struct page *page)
  1065. {
  1066. VM_BUG_ON_PGFLAGS(PageTail(page), page);
  1067. return compound_nr(page);
  1068. }
  1069. /**
  1070. * thp_size - Size of a transparent huge page.
  1071. * @page: Head page of a transparent huge page.
  1072. *
  1073. * Return: Number of bytes in this page.
  1074. */
  1075. static inline unsigned long thp_size(struct page *page)
  1076. {
  1077. return PAGE_SIZE << thp_order(page);
  1078. }
  1079. void free_compound_page(struct page *page);
  1080. #ifdef CONFIG_MMU
  1081. /*
  1082. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  1083. * servicing faults for write access. In the normal case, do always want
  1084. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  1085. * that do not have writing enabled, when used by access_process_vm.
  1086. */
  1087. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  1088. {
  1089. if (likely(vma->vm_flags & VM_WRITE))
  1090. pte = pte_mkwrite(pte);
  1091. return pte;
  1092. }
  1093. vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page);
  1094. void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr);
  1095. vm_fault_t finish_fault(struct vm_fault *vmf);
  1096. vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf);
  1097. #endif
  1098. /*
  1099. * Multiple processes may "see" the same page. E.g. for untouched
  1100. * mappings of /dev/null, all processes see the same page full of
  1101. * zeroes, and text pages of executables and shared libraries have
  1102. * only one copy in memory, at most, normally.
  1103. *
  1104. * For the non-reserved pages, page_count(page) denotes a reference count.
  1105. * page_count() == 0 means the page is free. page->lru is then used for
  1106. * freelist management in the buddy allocator.
  1107. * page_count() > 0 means the page has been allocated.
  1108. *
  1109. * Pages are allocated by the slab allocator in order to provide memory
  1110. * to kmalloc and kmem_cache_alloc. In this case, the management of the
  1111. * page, and the fields in 'struct page' are the responsibility of mm/slab.c
  1112. * unless a particular usage is carefully commented. (the responsibility of
  1113. * freeing the kmalloc memory is the caller's, of course).
  1114. *
  1115. * A page may be used by anyone else who does a __get_free_page().
  1116. * In this case, page_count still tracks the references, and should only
  1117. * be used through the normal accessor functions. The top bits of page->flags
  1118. * and page->virtual store page management information, but all other fields
  1119. * are unused and could be used privately, carefully. The management of this
  1120. * page is the responsibility of the one who allocated it, and those who have
  1121. * subsequently been given references to it.
  1122. *
  1123. * The other pages (we may call them "pagecache pages") are completely
  1124. * managed by the Linux memory manager: I/O, buffers, swapping etc.
  1125. * The following discussion applies only to them.
  1126. *
  1127. * A pagecache page contains an opaque `private' member, which belongs to the
  1128. * page's address_space. Usually, this is the address of a circular list of
  1129. * the page's disk buffers. PG_private must be set to tell the VM to call
  1130. * into the filesystem to release these pages.
  1131. *
  1132. * A page may belong to an inode's memory mapping. In this case, page->mapping
  1133. * is the pointer to the inode, and page->index is the file offset of the page,
  1134. * in units of PAGE_SIZE.
  1135. *
  1136. * If pagecache pages are not associated with an inode, they are said to be
  1137. * anonymous pages. These may become associated with the swapcache, and in that
  1138. * case PG_swapcache is set, and page->private is an offset into the swapcache.
  1139. *
  1140. * In either case (swapcache or inode backed), the pagecache itself holds one
  1141. * reference to the page. Setting PG_private should also increment the
  1142. * refcount. The each user mapping also has a reference to the page.
  1143. *
  1144. * The pagecache pages are stored in a per-mapping radix tree, which is
  1145. * rooted at mapping->i_pages, and indexed by offset.
  1146. * Where 2.4 and early 2.6 kernels kept dirty/clean pages in per-address_space
  1147. * lists, we instead now tag pages as dirty/writeback in the radix tree.
  1148. *
  1149. * All pagecache pages may be subject to I/O:
  1150. * - inode pages may need to be read from disk,
  1151. * - inode pages which have been modified and are MAP_SHARED may need
  1152. * to be written back to the inode on disk,
  1153. * - anonymous pages (including MAP_PRIVATE file mappings) which have been
  1154. * modified may need to be swapped out to swap space and (later) to be read
  1155. * back into memory.
  1156. */
  1157. #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_FS_DAX)
  1158. DECLARE_STATIC_KEY_FALSE(devmap_managed_key);
  1159. bool __put_devmap_managed_page_refs(struct page *page, int refs);
  1160. static inline bool put_devmap_managed_page_refs(struct page *page, int refs)
  1161. {
  1162. if (!static_branch_unlikely(&devmap_managed_key))
  1163. return false;
  1164. if (!is_zone_device_page(page))
  1165. return false;
  1166. return __put_devmap_managed_page_refs(page, refs);
  1167. }
  1168. #else /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */
  1169. static inline bool put_devmap_managed_page_refs(struct page *page, int refs)
  1170. {
  1171. return false;
  1172. }
  1173. #endif /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */
  1174. static inline bool put_devmap_managed_page(struct page *page)
  1175. {
  1176. return put_devmap_managed_page_refs(page, 1);
  1177. }
  1178. /* 127: arbitrary random number, small enough to assemble well */
  1179. #define folio_ref_zero_or_close_to_overflow(folio) \
  1180. ((unsigned int) folio_ref_count(folio) + 127u <= 127u)
  1181. /**
  1182. * folio_get - Increment the reference count on a folio.
  1183. * @folio: The folio.
  1184. *
  1185. * Context: May be called in any context, as long as you know that
  1186. * you have a refcount on the folio. If you do not already have one,
  1187. * folio_try_get() may be the right interface for you to use.
  1188. */
  1189. static inline void folio_get(struct folio *folio)
  1190. {
  1191. VM_BUG_ON_FOLIO(folio_ref_zero_or_close_to_overflow(folio), folio);
  1192. folio_ref_inc(folio);
  1193. }
  1194. static inline void get_page(struct page *page)
  1195. {
  1196. folio_get(page_folio(page));
  1197. }
  1198. bool __must_check try_grab_page(struct page *page, unsigned int flags);
  1199. static inline __must_check bool try_get_page(struct page *page)
  1200. {
  1201. page = compound_head(page);
  1202. if (WARN_ON_ONCE(page_ref_count(page) <= 0))
  1203. return false;
  1204. page_ref_inc(page);
  1205. return true;
  1206. }
  1207. /**
  1208. * folio_put - Decrement the reference count on a folio.
  1209. * @folio: The folio.
  1210. *
  1211. * If the folio's reference count reaches zero, the memory will be
  1212. * released back to the page allocator and may be used by another
  1213. * allocation immediately. Do not access the memory or the struct folio
  1214. * after calling folio_put() unless you can be sure that it wasn't the
  1215. * last reference.
  1216. *
  1217. * Context: May be called in process or interrupt context, but not in NMI
  1218. * context. May be called while holding a spinlock.
  1219. */
  1220. static inline void folio_put(struct folio *folio)
  1221. {
  1222. if (folio_put_testzero(folio))
  1223. __folio_put(folio);
  1224. }
  1225. /**
  1226. * folio_put_refs - Reduce the reference count on a folio.
  1227. * @folio: The folio.
  1228. * @refs: The amount to subtract from the folio's reference count.
  1229. *
  1230. * If the folio's reference count reaches zero, the memory will be
  1231. * released back to the page allocator and may be used by another
  1232. * allocation immediately. Do not access the memory or the struct folio
  1233. * after calling folio_put_refs() unless you can be sure that these weren't
  1234. * the last references.
  1235. *
  1236. * Context: May be called in process or interrupt context, but not in NMI
  1237. * context. May be called while holding a spinlock.
  1238. */
  1239. static inline void folio_put_refs(struct folio *folio, int refs)
  1240. {
  1241. if (folio_ref_sub_and_test(folio, refs))
  1242. __folio_put(folio);
  1243. }
  1244. void release_pages(struct page **pages, int nr);
  1245. /**
  1246. * folios_put - Decrement the reference count on an array of folios.
  1247. * @folios: The folios.
  1248. * @nr: How many folios there are.
  1249. *
  1250. * Like folio_put(), but for an array of folios. This is more efficient
  1251. * than writing the loop yourself as it will optimise the locks which
  1252. * need to be taken if the folios are freed.
  1253. *
  1254. * Context: May be called in process or interrupt context, but not in NMI
  1255. * context. May be called while holding a spinlock.
  1256. */
  1257. static inline void folios_put(struct folio **folios, unsigned int nr)
  1258. {
  1259. release_pages((struct page **)folios, nr);
  1260. }
  1261. static inline void put_page(struct page *page)
  1262. {
  1263. struct folio *folio = page_folio(page);
  1264. /*
  1265. * For some devmap managed pages we need to catch refcount transition
  1266. * from 2 to 1:
  1267. */
  1268. if (put_devmap_managed_page(&folio->page))
  1269. return;
  1270. folio_put(folio);
  1271. }
  1272. /*
  1273. * GUP_PIN_COUNTING_BIAS, and the associated functions that use it, overload
  1274. * the page's refcount so that two separate items are tracked: the original page
  1275. * reference count, and also a new count of how many pin_user_pages() calls were
  1276. * made against the page. ("gup-pinned" is another term for the latter).
  1277. *
  1278. * With this scheme, pin_user_pages() becomes special: such pages are marked as
  1279. * distinct from normal pages. As such, the unpin_user_page() call (and its
  1280. * variants) must be used in order to release gup-pinned pages.
  1281. *
  1282. * Choice of value:
  1283. *
  1284. * By making GUP_PIN_COUNTING_BIAS a power of two, debugging of page reference
  1285. * counts with respect to pin_user_pages() and unpin_user_page() becomes
  1286. * simpler, due to the fact that adding an even power of two to the page
  1287. * refcount has the effect of using only the upper N bits, for the code that
  1288. * counts up using the bias value. This means that the lower bits are left for
  1289. * the exclusive use of the original code that increments and decrements by one
  1290. * (or at least, by much smaller values than the bias value).
  1291. *
  1292. * Of course, once the lower bits overflow into the upper bits (and this is
  1293. * OK, because subtraction recovers the original values), then visual inspection
  1294. * no longer suffices to directly view the separate counts. However, for normal
  1295. * applications that don't have huge page reference counts, this won't be an
  1296. * issue.
  1297. *
  1298. * Locking: the lockless algorithm described in folio_try_get_rcu()
  1299. * provides safe operation for get_user_pages(), page_mkclean() and
  1300. * other calls that race to set up page table entries.
  1301. */
  1302. #define GUP_PIN_COUNTING_BIAS (1U << 10)
  1303. void unpin_user_page(struct page *page);
  1304. void unpin_user_pages_dirty_lock(struct page **pages, unsigned long npages,
  1305. bool make_dirty);
  1306. void unpin_user_page_range_dirty_lock(struct page *page, unsigned long npages,
  1307. bool make_dirty);
  1308. void unpin_user_pages(struct page **pages, unsigned long npages);
  1309. static inline bool is_cow_mapping(vm_flags_t flags)
  1310. {
  1311. return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  1312. }
  1313. #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP)
  1314. #define SECTION_IN_PAGE_FLAGS
  1315. #endif
  1316. /*
  1317. * The identification function is mainly used by the buddy allocator for
  1318. * determining if two pages could be buddies. We are not really identifying
  1319. * the zone since we could be using the section number id if we do not have
  1320. * node id available in page flags.
  1321. * We only guarantee that it will return the same value for two combinable
  1322. * pages in a zone.
  1323. */
  1324. static inline int page_zone_id(struct page *page)
  1325. {
  1326. return (page->flags >> ZONEID_PGSHIFT) & ZONEID_MASK;
  1327. }
  1328. #ifdef NODE_NOT_IN_PAGE_FLAGS
  1329. extern int page_to_nid(const struct page *page);
  1330. #else
  1331. static inline int page_to_nid(const struct page *page)
  1332. {
  1333. struct page *p = (struct page *)page;
  1334. return (PF_POISONED_CHECK(p)->flags >> NODES_PGSHIFT) & NODES_MASK;
  1335. }
  1336. #endif
  1337. static inline int folio_nid(const struct folio *folio)
  1338. {
  1339. return page_to_nid(&folio->page);
  1340. }
  1341. #ifdef CONFIG_NUMA_BALANCING
  1342. /* page access time bits needs to hold at least 4 seconds */
  1343. #define PAGE_ACCESS_TIME_MIN_BITS 12
  1344. #if LAST_CPUPID_SHIFT < PAGE_ACCESS_TIME_MIN_BITS
  1345. #define PAGE_ACCESS_TIME_BUCKETS \
  1346. (PAGE_ACCESS_TIME_MIN_BITS - LAST_CPUPID_SHIFT)
  1347. #else
  1348. #define PAGE_ACCESS_TIME_BUCKETS 0
  1349. #endif
  1350. #define PAGE_ACCESS_TIME_MASK \
  1351. (LAST_CPUPID_MASK << PAGE_ACCESS_TIME_BUCKETS)
  1352. static inline int cpu_pid_to_cpupid(int cpu, int pid)
  1353. {
  1354. return ((cpu & LAST__CPU_MASK) << LAST__PID_SHIFT) | (pid & LAST__PID_MASK);
  1355. }
  1356. static inline int cpupid_to_pid(int cpupid)
  1357. {
  1358. return cpupid & LAST__PID_MASK;
  1359. }
  1360. static inline int cpupid_to_cpu(int cpupid)
  1361. {
  1362. return (cpupid >> LAST__PID_SHIFT) & LAST__CPU_MASK;
  1363. }
  1364. static inline int cpupid_to_nid(int cpupid)
  1365. {
  1366. return cpu_to_node(cpupid_to_cpu(cpupid));
  1367. }
  1368. static inline bool cpupid_pid_unset(int cpupid)
  1369. {
  1370. return cpupid_to_pid(cpupid) == (-1 & LAST__PID_MASK);
  1371. }
  1372. static inline bool cpupid_cpu_unset(int cpupid)
  1373. {
  1374. return cpupid_to_cpu(cpupid) == (-1 & LAST__CPU_MASK);
  1375. }
  1376. static inline bool __cpupid_match_pid(pid_t task_pid, int cpupid)
  1377. {
  1378. return (task_pid & LAST__PID_MASK) == cpupid_to_pid(cpupid);
  1379. }
  1380. #define cpupid_match_pid(task, cpupid) __cpupid_match_pid(task->pid, cpupid)
  1381. #ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
  1382. static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
  1383. {
  1384. return xchg(&page->_last_cpupid, cpupid & LAST_CPUPID_MASK);
  1385. }
  1386. static inline int page_cpupid_last(struct page *page)
  1387. {
  1388. return page->_last_cpupid;
  1389. }
  1390. static inline void page_cpupid_reset_last(struct page *page)
  1391. {
  1392. page->_last_cpupid = -1 & LAST_CPUPID_MASK;
  1393. }
  1394. #else
  1395. static inline int page_cpupid_last(struct page *page)
  1396. {
  1397. return (page->flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
  1398. }
  1399. extern int page_cpupid_xchg_last(struct page *page, int cpupid);
  1400. static inline void page_cpupid_reset_last(struct page *page)
  1401. {
  1402. page->flags |= LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT;
  1403. }
  1404. #endif /* LAST_CPUPID_NOT_IN_PAGE_FLAGS */
  1405. static inline int xchg_page_access_time(struct page *page, int time)
  1406. {
  1407. int last_time;
  1408. last_time = page_cpupid_xchg_last(page, time >> PAGE_ACCESS_TIME_BUCKETS);
  1409. return last_time << PAGE_ACCESS_TIME_BUCKETS;
  1410. }
  1411. #else /* !CONFIG_NUMA_BALANCING */
  1412. static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
  1413. {
  1414. return page_to_nid(page); /* XXX */
  1415. }
  1416. static inline int xchg_page_access_time(struct page *page, int time)
  1417. {
  1418. return 0;
  1419. }
  1420. static inline int page_cpupid_last(struct page *page)
  1421. {
  1422. return page_to_nid(page); /* XXX */
  1423. }
  1424. static inline int cpupid_to_nid(int cpupid)
  1425. {
  1426. return -1;
  1427. }
  1428. static inline int cpupid_to_pid(int cpupid)
  1429. {
  1430. return -1;
  1431. }
  1432. static inline int cpupid_to_cpu(int cpupid)
  1433. {
  1434. return -1;
  1435. }
  1436. static inline int cpu_pid_to_cpupid(int nid, int pid)
  1437. {
  1438. return -1;
  1439. }
  1440. static inline bool cpupid_pid_unset(int cpupid)
  1441. {
  1442. return true;
  1443. }
  1444. static inline void page_cpupid_reset_last(struct page *page)
  1445. {
  1446. }
  1447. static inline bool cpupid_match_pid(struct task_struct *task, int cpupid)
  1448. {
  1449. return false;
  1450. }
  1451. #endif /* CONFIG_NUMA_BALANCING */
  1452. #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
  1453. /*
  1454. * KASAN per-page tags are stored xor'ed with 0xff. This allows to avoid
  1455. * setting tags for all pages to native kernel tag value 0xff, as the default
  1456. * value 0x00 maps to 0xff.
  1457. */
  1458. static inline u8 page_kasan_tag(const struct page *page)
  1459. {
  1460. u8 tag = 0xff;
  1461. if (kasan_enabled()) {
  1462. tag = (page->flags >> KASAN_TAG_PGSHIFT) & KASAN_TAG_MASK;
  1463. tag ^= 0xff;
  1464. }
  1465. return tag;
  1466. }
  1467. static inline void page_kasan_tag_set(struct page *page, u8 tag)
  1468. {
  1469. unsigned long old_flags, flags;
  1470. if (!kasan_enabled())
  1471. return;
  1472. tag ^= 0xff;
  1473. old_flags = READ_ONCE(page->flags);
  1474. do {
  1475. flags = old_flags;
  1476. flags &= ~(KASAN_TAG_MASK << KASAN_TAG_PGSHIFT);
  1477. flags |= (tag & KASAN_TAG_MASK) << KASAN_TAG_PGSHIFT;
  1478. } while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags)));
  1479. }
  1480. static inline void page_kasan_tag_reset(struct page *page)
  1481. {
  1482. if (kasan_enabled())
  1483. page_kasan_tag_set(page, 0xff);
  1484. }
  1485. #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
  1486. static inline u8 page_kasan_tag(const struct page *page)
  1487. {
  1488. return 0xff;
  1489. }
  1490. static inline void page_kasan_tag_set(struct page *page, u8 tag) { }
  1491. static inline void page_kasan_tag_reset(struct page *page) { }
  1492. #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
  1493. static inline struct zone *page_zone(const struct page *page)
  1494. {
  1495. return &NODE_DATA(page_to_nid(page))->node_zones[page_zonenum(page)];
  1496. }
  1497. static inline pg_data_t *page_pgdat(const struct page *page)
  1498. {
  1499. return NODE_DATA(page_to_nid(page));
  1500. }
  1501. static inline struct zone *folio_zone(const struct folio *folio)
  1502. {
  1503. return page_zone(&folio->page);
  1504. }
  1505. static inline pg_data_t *folio_pgdat(const struct folio *folio)
  1506. {
  1507. return page_pgdat(&folio->page);
  1508. }
  1509. #ifdef SECTION_IN_PAGE_FLAGS
  1510. static inline void set_page_section(struct page *page, unsigned long section)
  1511. {
  1512. page->flags &= ~(SECTIONS_MASK << SECTIONS_PGSHIFT);
  1513. page->flags |= (section & SECTIONS_MASK) << SECTIONS_PGSHIFT;
  1514. }
  1515. static inline unsigned long page_to_section(const struct page *page)
  1516. {
  1517. return (page->flags >> SECTIONS_PGSHIFT) & SECTIONS_MASK;
  1518. }
  1519. #endif
  1520. /**
  1521. * folio_pfn - Return the Page Frame Number of a folio.
  1522. * @folio: The folio.
  1523. *
  1524. * A folio may contain multiple pages. The pages have consecutive
  1525. * Page Frame Numbers.
  1526. *
  1527. * Return: The Page Frame Number of the first page in the folio.
  1528. */
  1529. static inline unsigned long folio_pfn(struct folio *folio)
  1530. {
  1531. return page_to_pfn(&folio->page);
  1532. }
  1533. static inline struct folio *pfn_folio(unsigned long pfn)
  1534. {
  1535. return page_folio(pfn_to_page(pfn));
  1536. }
  1537. static inline atomic_t *folio_pincount_ptr(struct folio *folio)
  1538. {
  1539. return &folio_page(folio, 1)->compound_pincount;
  1540. }
  1541. /**
  1542. * folio_maybe_dma_pinned - Report if a folio may be pinned for DMA.
  1543. * @folio: The folio.
  1544. *
  1545. * This function checks if a folio has been pinned via a call to
  1546. * a function in the pin_user_pages() family.
  1547. *
  1548. * For small folios, the return value is partially fuzzy: false is not fuzzy,
  1549. * because it means "definitely not pinned for DMA", but true means "probably
  1550. * pinned for DMA, but possibly a false positive due to having at least
  1551. * GUP_PIN_COUNTING_BIAS worth of normal folio references".
  1552. *
  1553. * False positives are OK, because: a) it's unlikely for a folio to
  1554. * get that many refcounts, and b) all the callers of this routine are
  1555. * expected to be able to deal gracefully with a false positive.
  1556. *
  1557. * For large folios, the result will be exactly correct. That's because
  1558. * we have more tracking data available: the compound_pincount is used
  1559. * instead of the GUP_PIN_COUNTING_BIAS scheme.
  1560. *
  1561. * For more information, please see Documentation/core-api/pin_user_pages.rst.
  1562. *
  1563. * Return: True, if it is likely that the page has been "dma-pinned".
  1564. * False, if the page is definitely not dma-pinned.
  1565. */
  1566. static inline bool folio_maybe_dma_pinned(struct folio *folio)
  1567. {
  1568. if (folio_test_large(folio))
  1569. return atomic_read(folio_pincount_ptr(folio)) > 0;
  1570. /*
  1571. * folio_ref_count() is signed. If that refcount overflows, then
  1572. * folio_ref_count() returns a negative value, and callers will avoid
  1573. * further incrementing the refcount.
  1574. *
  1575. * Here, for that overflow case, use the sign bit to count a little
  1576. * bit higher via unsigned math, and thus still get an accurate result.
  1577. */
  1578. return ((unsigned int)folio_ref_count(folio)) >=
  1579. GUP_PIN_COUNTING_BIAS;
  1580. }
  1581. static inline bool page_maybe_dma_pinned(struct page *page)
  1582. {
  1583. return folio_maybe_dma_pinned(page_folio(page));
  1584. }
  1585. /*
  1586. * This should most likely only be called during fork() to see whether we
  1587. * should break the cow immediately for an anon page on the src mm.
  1588. *
  1589. * The caller has to hold the PT lock and the vma->vm_mm->->write_protect_seq.
  1590. */
  1591. static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma,
  1592. struct page *page)
  1593. {
  1594. VM_BUG_ON(!(raw_read_seqcount(&vma->vm_mm->write_protect_seq) & 1));
  1595. if (!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))
  1596. return false;
  1597. return page_maybe_dma_pinned(page);
  1598. }
  1599. /* MIGRATE_CMA and ZONE_MOVABLE do not allow pin pages */
  1600. #ifdef CONFIG_MIGRATION
  1601. static inline bool is_longterm_pinnable_page(struct page *page)
  1602. {
  1603. #ifdef CONFIG_CMA
  1604. int mt = get_pageblock_migratetype(page);
  1605. if (mt == MIGRATE_CMA || mt == MIGRATE_ISOLATE)
  1606. return false;
  1607. #endif
  1608. /* The zero page may always be pinned */
  1609. if (is_zero_pfn(page_to_pfn(page)))
  1610. return true;
  1611. /* Coherent device memory must always allow eviction. */
  1612. if (is_device_coherent_page(page))
  1613. return false;
  1614. /* Otherwise, non-movable zone pages can be pinned. */
  1615. return !is_zone_movable_page(page);
  1616. }
  1617. #else
  1618. static inline bool is_longterm_pinnable_page(struct page *page)
  1619. {
  1620. return true;
  1621. }
  1622. #endif
  1623. static inline bool folio_is_longterm_pinnable(struct folio *folio)
  1624. {
  1625. return is_longterm_pinnable_page(&folio->page);
  1626. }
  1627. static inline void set_page_zone(struct page *page, enum zone_type zone)
  1628. {
  1629. page->flags &= ~(ZONES_MASK << ZONES_PGSHIFT);
  1630. page->flags |= (zone & ZONES_MASK) << ZONES_PGSHIFT;
  1631. }
  1632. static inline void set_page_node(struct page *page, unsigned long node)
  1633. {
  1634. page->flags &= ~(NODES_MASK << NODES_PGSHIFT);
  1635. page->flags |= (node & NODES_MASK) << NODES_PGSHIFT;
  1636. }
  1637. static inline void set_page_links(struct page *page, enum zone_type zone,
  1638. unsigned long node, unsigned long pfn)
  1639. {
  1640. set_page_zone(page, zone);
  1641. set_page_node(page, node);
  1642. #ifdef SECTION_IN_PAGE_FLAGS
  1643. set_page_section(page, pfn_to_section_nr(pfn));
  1644. #endif
  1645. }
  1646. /**
  1647. * folio_nr_pages - The number of pages in the folio.
  1648. * @folio: The folio.
  1649. *
  1650. * Return: A positive power of two.
  1651. */
  1652. static inline long folio_nr_pages(struct folio *folio)
  1653. {
  1654. if (!folio_test_large(folio))
  1655. return 1;
  1656. #ifdef CONFIG_64BIT
  1657. return folio->_folio_nr_pages;
  1658. #else
  1659. return 1L << folio->_folio_order;
  1660. #endif
  1661. }
  1662. /**
  1663. * folio_next - Move to the next physical folio.
  1664. * @folio: The folio we're currently operating on.
  1665. *
  1666. * If you have physically contiguous memory which may span more than
  1667. * one folio (eg a &struct bio_vec), use this function to move from one
  1668. * folio to the next. Do not use it if the memory is only virtually
  1669. * contiguous as the folios are almost certainly not adjacent to each
  1670. * other. This is the folio equivalent to writing ``page++``.
  1671. *
  1672. * Context: We assume that the folios are refcounted and/or locked at a
  1673. * higher level and do not adjust the reference counts.
  1674. * Return: The next struct folio.
  1675. */
  1676. static inline struct folio *folio_next(struct folio *folio)
  1677. {
  1678. return (struct folio *)folio_page(folio, folio_nr_pages(folio));
  1679. }
  1680. /**
  1681. * folio_shift - The size of the memory described by this folio.
  1682. * @folio: The folio.
  1683. *
  1684. * A folio represents a number of bytes which is a power-of-two in size.
  1685. * This function tells you which power-of-two the folio is. See also
  1686. * folio_size() and folio_order().
  1687. *
  1688. * Context: The caller should have a reference on the folio to prevent
  1689. * it from being split. It is not necessary for the folio to be locked.
  1690. * Return: The base-2 logarithm of the size of this folio.
  1691. */
  1692. static inline unsigned int folio_shift(struct folio *folio)
  1693. {
  1694. return PAGE_SHIFT + folio_order(folio);
  1695. }
  1696. /**
  1697. * folio_size - The number of bytes in a folio.
  1698. * @folio: The folio.
  1699. *
  1700. * Context: The caller should have a reference on the folio to prevent
  1701. * it from being split. It is not necessary for the folio to be locked.
  1702. * Return: The number of bytes in this folio.
  1703. */
  1704. static inline size_t folio_size(struct folio *folio)
  1705. {
  1706. return PAGE_SIZE << folio_order(folio);
  1707. }
  1708. /**
  1709. * folio_estimated_sharers - Estimate the number of sharers of a folio.
  1710. * @folio: The folio.
  1711. *
  1712. * folio_estimated_sharers() aims to serve as a function to efficiently
  1713. * estimate the number of processes sharing a folio. This is done by
  1714. * looking at the precise mapcount of the first subpage in the folio, and
  1715. * assuming the other subpages are the same. This may not be true for large
  1716. * folios. If you want exact mapcounts for exact calculations, look at
  1717. * page_mapcount() or folio_total_mapcount().
  1718. *
  1719. * Return: The estimated number of processes sharing a folio.
  1720. */
  1721. static inline int folio_estimated_sharers(struct folio *folio)
  1722. {
  1723. return page_mapcount(folio_page(folio, 0));
  1724. }
  1725. #ifndef HAVE_ARCH_MAKE_PAGE_ACCESSIBLE
  1726. static inline int arch_make_page_accessible(struct page *page)
  1727. {
  1728. return 0;
  1729. }
  1730. #endif
  1731. #ifndef HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE
  1732. static inline int arch_make_folio_accessible(struct folio *folio)
  1733. {
  1734. int ret;
  1735. long i, nr = folio_nr_pages(folio);
  1736. for (i = 0; i < nr; i++) {
  1737. ret = arch_make_page_accessible(folio_page(folio, i));
  1738. if (ret)
  1739. break;
  1740. }
  1741. return ret;
  1742. }
  1743. #endif
  1744. /*
  1745. * Some inline functions in vmstat.h depend on page_zone()
  1746. */
  1747. #include <linux/vmstat.h>
  1748. static __always_inline void *lowmem_page_address(const struct page *page)
  1749. {
  1750. return page_to_virt(page);
  1751. }
  1752. #if defined(CONFIG_HIGHMEM) && !defined(WANT_PAGE_VIRTUAL)
  1753. #define HASHED_PAGE_VIRTUAL
  1754. #endif
  1755. #if defined(WANT_PAGE_VIRTUAL)
  1756. static inline void *page_address(const struct page *page)
  1757. {
  1758. return page->virtual;
  1759. }
  1760. static inline void set_page_address(struct page *page, void *address)
  1761. {
  1762. page->virtual = address;
  1763. }
  1764. #define page_address_init() do { } while(0)
  1765. #endif
  1766. #if defined(HASHED_PAGE_VIRTUAL)
  1767. void *page_address(const struct page *page);
  1768. void set_page_address(struct page *page, void *virtual);
  1769. void page_address_init(void);
  1770. #endif
  1771. #if !defined(HASHED_PAGE_VIRTUAL) && !defined(WANT_PAGE_VIRTUAL)
  1772. #define page_address(page) lowmem_page_address(page)
  1773. #define set_page_address(page, address) do { } while(0)
  1774. #define page_address_init() do { } while(0)
  1775. #endif
  1776. static inline void *folio_address(const struct folio *folio)
  1777. {
  1778. return page_address(&folio->page);
  1779. }
  1780. extern void *page_rmapping(struct page *page);
  1781. extern pgoff_t __page_file_index(struct page *page);
  1782. /*
  1783. * Return the pagecache index of the passed page. Regular pagecache pages
  1784. * use ->index whereas swapcache pages use swp_offset(->private)
  1785. */
  1786. static inline pgoff_t page_index(struct page *page)
  1787. {
  1788. if (unlikely(PageSwapCache(page)))
  1789. return __page_file_index(page);
  1790. return page->index;
  1791. }
  1792. bool page_mapped(struct page *page);
  1793. bool folio_mapped(struct folio *folio);
  1794. /*
  1795. * Return true only if the page has been allocated with
  1796. * ALLOC_NO_WATERMARKS and the low watermark was not
  1797. * met implying that the system is under some pressure.
  1798. */
  1799. static inline bool page_is_pfmemalloc(const struct page *page)
  1800. {
  1801. /*
  1802. * lru.next has bit 1 set if the page is allocated from the
  1803. * pfmemalloc reserves. Callers may simply overwrite it if
  1804. * they do not need to preserve that information.
  1805. */
  1806. return (uintptr_t)page->lru.next & BIT(1);
  1807. }
  1808. /*
  1809. * Only to be called by the page allocator on a freshly allocated
  1810. * page.
  1811. */
  1812. static inline void set_page_pfmemalloc(struct page *page)
  1813. {
  1814. page->lru.next = (void *)BIT(1);
  1815. }
  1816. static inline void clear_page_pfmemalloc(struct page *page)
  1817. {
  1818. page->lru.next = NULL;
  1819. }
  1820. /*
  1821. * Can be called by the pagefault handler when it gets a VM_FAULT_OOM.
  1822. */
  1823. extern void pagefault_out_of_memory(void);
  1824. #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
  1825. #define offset_in_thp(page, p) ((unsigned long)(p) & (thp_size(page) - 1))
  1826. #define offset_in_folio(folio, p) ((unsigned long)(p) & (folio_size(folio) - 1))
  1827. /*
  1828. * Flags passed to show_mem() and show_free_areas() to suppress output in
  1829. * various contexts.
  1830. */
  1831. #define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */
  1832. extern void __show_free_areas(unsigned int flags, nodemask_t *nodemask, int max_zone_idx);
  1833. static void __maybe_unused show_free_areas(unsigned int flags, nodemask_t *nodemask)
  1834. {
  1835. __show_free_areas(flags, nodemask, MAX_NR_ZONES - 1);
  1836. }
  1837. /*
  1838. * Parameter block passed down to zap_pte_range in exceptional cases.
  1839. */
  1840. struct zap_details {
  1841. struct folio *single_folio; /* Locked folio to be unmapped */
  1842. bool even_cows; /* Zap COWed private pages too? */
  1843. zap_flags_t zap_flags; /* Extra flags for zapping */
  1844. };
  1845. /*
  1846. * Whether to drop the pte markers, for example, the uffd-wp information for
  1847. * file-backed memory. This should only be specified when we will completely
  1848. * drop the page in the mm, either by truncation or unmapping of the vma. By
  1849. * default, the flag is not set.
  1850. */
  1851. #define ZAP_FLAG_DROP_MARKER ((__force zap_flags_t) BIT(0))
  1852. /* Set in unmap_vmas() to indicate a final unmap call. Only used by hugetlb */
  1853. #define ZAP_FLAG_UNMAP ((__force zap_flags_t) BIT(1))
  1854. #ifdef CONFIG_MMU
  1855. extern bool can_do_mlock(void);
  1856. #else
  1857. static inline bool can_do_mlock(void) { return false; }
  1858. #endif
  1859. extern int user_shm_lock(size_t, struct ucounts *);
  1860. extern void user_shm_unlock(size_t, struct ucounts *);
  1861. struct folio *vm_normal_folio(struct vm_area_struct *vma, unsigned long addr,
  1862. pte_t pte);
  1863. struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
  1864. pte_t pte);
  1865. struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
  1866. pmd_t pmd);
  1867. void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
  1868. unsigned long size);
  1869. void zap_page_range(struct vm_area_struct *vma, unsigned long address,
  1870. unsigned long size);
  1871. void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
  1872. unsigned long size, struct zap_details *details);
  1873. void unmap_vmas(struct mmu_gather *tlb, struct maple_tree *mt,
  1874. struct vm_area_struct *start_vma, unsigned long start,
  1875. unsigned long end, unsigned long start_t,
  1876. unsigned long end_t, bool mm_wr_locked);
  1877. struct mmu_notifier_range;
  1878. void free_pgd_range(struct mmu_gather *tlb, unsigned long addr,
  1879. unsigned long end, unsigned long floor, unsigned long ceiling);
  1880. int
  1881. copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
  1882. int follow_pte(struct mm_struct *mm, unsigned long address,
  1883. pte_t **ptepp, spinlock_t **ptlp);
  1884. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  1885. unsigned long *pfn);
  1886. int follow_phys(struct vm_area_struct *vma, unsigned long address,
  1887. unsigned int flags, unsigned long *prot, resource_size_t *phys);
  1888. int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
  1889. void *buf, int len, int write);
  1890. extern void truncate_pagecache(struct inode *inode, loff_t new);
  1891. extern void truncate_setsize(struct inode *inode, loff_t newsize);
  1892. void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to);
  1893. void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end);
  1894. int generic_error_remove_page(struct address_space *mapping, struct page *page);
  1895. #ifdef CONFIG_MMU
  1896. extern vm_fault_t handle_mm_fault(struct vm_area_struct *vma,
  1897. unsigned long address, unsigned int flags,
  1898. struct pt_regs *regs);
  1899. extern int fixup_user_fault(struct mm_struct *mm,
  1900. unsigned long address, unsigned int fault_flags,
  1901. bool *unlocked);
  1902. void unmap_mapping_pages(struct address_space *mapping,
  1903. pgoff_t start, pgoff_t nr, bool even_cows);
  1904. void unmap_mapping_range(struct address_space *mapping,
  1905. loff_t const holebegin, loff_t const holelen, int even_cows);
  1906. struct vm_area_struct *lock_mm_and_find_vma(struct mm_struct *mm,
  1907. unsigned long address, struct pt_regs *regs);
  1908. #else
  1909. static inline vm_fault_t handle_mm_fault(struct vm_area_struct *vma,
  1910. unsigned long address, unsigned int flags,
  1911. struct pt_regs *regs)
  1912. {
  1913. /* should never happen if there's no MMU */
  1914. BUG();
  1915. return VM_FAULT_SIGBUS;
  1916. }
  1917. static inline int fixup_user_fault(struct mm_struct *mm, unsigned long address,
  1918. unsigned int fault_flags, bool *unlocked)
  1919. {
  1920. /* should never happen if there's no MMU */
  1921. BUG();
  1922. return -EFAULT;
  1923. }
  1924. static inline void unmap_mapping_pages(struct address_space *mapping,
  1925. pgoff_t start, pgoff_t nr, bool even_cows) { }
  1926. static inline void unmap_mapping_range(struct address_space *mapping,
  1927. loff_t const holebegin, loff_t const holelen, int even_cows) { }
  1928. #endif
  1929. static inline void unmap_shared_mapping_range(struct address_space *mapping,
  1930. loff_t const holebegin, loff_t const holelen)
  1931. {
  1932. unmap_mapping_range(mapping, holebegin, holelen, 0);
  1933. }
  1934. extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
  1935. void *buf, int len, unsigned int gup_flags);
  1936. extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  1937. void *buf, int len, unsigned int gup_flags);
  1938. extern int __access_remote_vm(struct mm_struct *mm, unsigned long addr,
  1939. void *buf, int len, unsigned int gup_flags);
  1940. long get_user_pages_remote(struct mm_struct *mm,
  1941. unsigned long start, unsigned long nr_pages,
  1942. unsigned int gup_flags, struct page **pages,
  1943. struct vm_area_struct **vmas, int *locked);
  1944. long pin_user_pages_remote(struct mm_struct *mm,
  1945. unsigned long start, unsigned long nr_pages,
  1946. unsigned int gup_flags, struct page **pages,
  1947. struct vm_area_struct **vmas, int *locked);
  1948. long get_user_pages(unsigned long start, unsigned long nr_pages,
  1949. unsigned int gup_flags, struct page **pages,
  1950. struct vm_area_struct **vmas);
  1951. long pin_user_pages(unsigned long start, unsigned long nr_pages,
  1952. unsigned int gup_flags, struct page **pages,
  1953. struct vm_area_struct **vmas);
  1954. long get_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
  1955. struct page **pages, unsigned int gup_flags);
  1956. long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
  1957. struct page **pages, unsigned int gup_flags);
  1958. int get_user_pages_fast(unsigned long start, int nr_pages,
  1959. unsigned int gup_flags, struct page **pages);
  1960. int pin_user_pages_fast(unsigned long start, int nr_pages,
  1961. unsigned int gup_flags, struct page **pages);
  1962. int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc);
  1963. int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc,
  1964. struct task_struct *task, bool bypass_rlim);
  1965. struct kvec;
  1966. int get_kernel_pages(const struct kvec *iov, int nr_pages, int write,
  1967. struct page **pages);
  1968. struct page *get_dump_page(unsigned long addr);
  1969. bool folio_mark_dirty(struct folio *folio);
  1970. bool set_page_dirty(struct page *page);
  1971. int set_page_dirty_lock(struct page *page);
  1972. int get_cmdline(struct task_struct *task, char *buffer, int buflen);
  1973. extern unsigned long move_page_tables(struct vm_area_struct *vma,
  1974. unsigned long old_addr, struct vm_area_struct *new_vma,
  1975. unsigned long new_addr, unsigned long len,
  1976. bool need_rmap_locks);
  1977. /*
  1978. * Flags used by change_protection(). For now we make it a bitmap so
  1979. * that we can pass in multiple flags just like parameters. However
  1980. * for now all the callers are only use one of the flags at the same
  1981. * time.
  1982. */
  1983. /*
  1984. * Whether we should manually check if we can map individual PTEs writable,
  1985. * because something (e.g., COW, uffd-wp) blocks that from happening for all
  1986. * PTEs automatically in a writable mapping.
  1987. */
  1988. #define MM_CP_TRY_CHANGE_WRITABLE (1UL << 0)
  1989. /* Whether this protection change is for NUMA hints */
  1990. #define MM_CP_PROT_NUMA (1UL << 1)
  1991. /* Whether this change is for write protecting */
  1992. #define MM_CP_UFFD_WP (1UL << 2) /* do wp */
  1993. #define MM_CP_UFFD_WP_RESOLVE (1UL << 3) /* Resolve wp */
  1994. #define MM_CP_UFFD_WP_ALL (MM_CP_UFFD_WP | \
  1995. MM_CP_UFFD_WP_RESOLVE)
  1996. extern unsigned long change_protection(struct mmu_gather *tlb,
  1997. struct vm_area_struct *vma, unsigned long start,
  1998. unsigned long end, pgprot_t newprot,
  1999. unsigned long cp_flags);
  2000. extern int mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
  2001. struct vm_area_struct **pprev, unsigned long start,
  2002. unsigned long end, unsigned long newflags);
  2003. /*
  2004. * doesn't attempt to fault and will return short.
  2005. */
  2006. int get_user_pages_fast_only(unsigned long start, int nr_pages,
  2007. unsigned int gup_flags, struct page **pages);
  2008. int pin_user_pages_fast_only(unsigned long start, int nr_pages,
  2009. unsigned int gup_flags, struct page **pages);
  2010. static inline bool get_user_page_fast_only(unsigned long addr,
  2011. unsigned int gup_flags, struct page **pagep)
  2012. {
  2013. return get_user_pages_fast_only(addr, 1, gup_flags, pagep) == 1;
  2014. }
  2015. /*
  2016. * per-process(per-mm_struct) statistics.
  2017. */
  2018. static inline unsigned long get_mm_counter(struct mm_struct *mm, int member)
  2019. {
  2020. long val = atomic_long_read(&mm->rss_stat.count[member]);
  2021. #ifdef SPLIT_RSS_COUNTING
  2022. /*
  2023. * counter is updated in asynchronous manner and may go to minus.
  2024. * But it's never be expected number for users.
  2025. */
  2026. if (val < 0)
  2027. val = 0;
  2028. #endif
  2029. return (unsigned long)val;
  2030. }
  2031. void mm_trace_rss_stat(struct mm_struct *mm, int member, long count);
  2032. static inline void add_mm_counter(struct mm_struct *mm, int member, long value)
  2033. {
  2034. long count = atomic_long_add_return(value, &mm->rss_stat.count[member]);
  2035. mm_trace_rss_stat(mm, member, count);
  2036. }
  2037. static inline void inc_mm_counter(struct mm_struct *mm, int member)
  2038. {
  2039. long count = atomic_long_inc_return(&mm->rss_stat.count[member]);
  2040. mm_trace_rss_stat(mm, member, count);
  2041. }
  2042. static inline void dec_mm_counter(struct mm_struct *mm, int member)
  2043. {
  2044. long count = atomic_long_dec_return(&mm->rss_stat.count[member]);
  2045. mm_trace_rss_stat(mm, member, count);
  2046. }
  2047. /* Optimized variant when page is already known not to be PageAnon */
  2048. static inline int mm_counter_file(struct page *page)
  2049. {
  2050. if (PageSwapBacked(page))
  2051. return MM_SHMEMPAGES;
  2052. return MM_FILEPAGES;
  2053. }
  2054. static inline int mm_counter(struct page *page)
  2055. {
  2056. if (PageAnon(page))
  2057. return MM_ANONPAGES;
  2058. return mm_counter_file(page);
  2059. }
  2060. static inline unsigned long get_mm_rss(struct mm_struct *mm)
  2061. {
  2062. return get_mm_counter(mm, MM_FILEPAGES) +
  2063. get_mm_counter(mm, MM_ANONPAGES) +
  2064. get_mm_counter(mm, MM_SHMEMPAGES);
  2065. }
  2066. static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm)
  2067. {
  2068. return max(mm->hiwater_rss, get_mm_rss(mm));
  2069. }
  2070. static inline unsigned long get_mm_hiwater_vm(struct mm_struct *mm)
  2071. {
  2072. return max(mm->hiwater_vm, mm->total_vm);
  2073. }
  2074. static inline void update_hiwater_rss(struct mm_struct *mm)
  2075. {
  2076. unsigned long _rss = get_mm_rss(mm);
  2077. if ((mm)->hiwater_rss < _rss)
  2078. (mm)->hiwater_rss = _rss;
  2079. }
  2080. static inline void update_hiwater_vm(struct mm_struct *mm)
  2081. {
  2082. if (mm->hiwater_vm < mm->total_vm)
  2083. mm->hiwater_vm = mm->total_vm;
  2084. }
  2085. static inline void reset_mm_hiwater_rss(struct mm_struct *mm)
  2086. {
  2087. mm->hiwater_rss = get_mm_rss(mm);
  2088. }
  2089. static inline void setmax_mm_hiwater_rss(unsigned long *maxrss,
  2090. struct mm_struct *mm)
  2091. {
  2092. unsigned long hiwater_rss = get_mm_hiwater_rss(mm);
  2093. if (*maxrss < hiwater_rss)
  2094. *maxrss = hiwater_rss;
  2095. }
  2096. #if defined(SPLIT_RSS_COUNTING)
  2097. void sync_mm_rss(struct mm_struct *mm);
  2098. #else
  2099. static inline void sync_mm_rss(struct mm_struct *mm)
  2100. {
  2101. }
  2102. #endif
  2103. #ifndef CONFIG_ARCH_HAS_PTE_SPECIAL
  2104. static inline int pte_special(pte_t pte)
  2105. {
  2106. return 0;
  2107. }
  2108. static inline pte_t pte_mkspecial(pte_t pte)
  2109. {
  2110. return pte;
  2111. }
  2112. #endif
  2113. #ifndef CONFIG_ARCH_HAS_PTE_DEVMAP
  2114. static inline int pte_devmap(pte_t pte)
  2115. {
  2116. return 0;
  2117. }
  2118. #endif
  2119. int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot);
  2120. extern pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
  2121. spinlock_t **ptl);
  2122. static inline pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr,
  2123. spinlock_t **ptl)
  2124. {
  2125. pte_t *ptep;
  2126. __cond_lock(*ptl, ptep = __get_locked_pte(mm, addr, ptl));
  2127. return ptep;
  2128. }
  2129. #ifdef __PAGETABLE_P4D_FOLDED
  2130. static inline int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd,
  2131. unsigned long address)
  2132. {
  2133. return 0;
  2134. }
  2135. #else
  2136. int __p4d_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address);
  2137. #endif
  2138. #if defined(__PAGETABLE_PUD_FOLDED) || !defined(CONFIG_MMU)
  2139. static inline int __pud_alloc(struct mm_struct *mm, p4d_t *p4d,
  2140. unsigned long address)
  2141. {
  2142. return 0;
  2143. }
  2144. static inline void mm_inc_nr_puds(struct mm_struct *mm) {}
  2145. static inline void mm_dec_nr_puds(struct mm_struct *mm) {}
  2146. #else
  2147. int __pud_alloc(struct mm_struct *mm, p4d_t *p4d, unsigned long address);
  2148. static inline void mm_inc_nr_puds(struct mm_struct *mm)
  2149. {
  2150. if (mm_pud_folded(mm))
  2151. return;
  2152. atomic_long_add(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes);
  2153. }
  2154. static inline void mm_dec_nr_puds(struct mm_struct *mm)
  2155. {
  2156. if (mm_pud_folded(mm))
  2157. return;
  2158. atomic_long_sub(PTRS_PER_PUD * sizeof(pud_t), &mm->pgtables_bytes);
  2159. }
  2160. #endif
  2161. #if defined(__PAGETABLE_PMD_FOLDED) || !defined(CONFIG_MMU)
  2162. static inline int __pmd_alloc(struct mm_struct *mm, pud_t *pud,
  2163. unsigned long address)
  2164. {
  2165. return 0;
  2166. }
  2167. static inline void mm_inc_nr_pmds(struct mm_struct *mm) {}
  2168. static inline void mm_dec_nr_pmds(struct mm_struct *mm) {}
  2169. #else
  2170. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address);
  2171. static inline void mm_inc_nr_pmds(struct mm_struct *mm)
  2172. {
  2173. if (mm_pmd_folded(mm))
  2174. return;
  2175. atomic_long_add(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes);
  2176. }
  2177. static inline void mm_dec_nr_pmds(struct mm_struct *mm)
  2178. {
  2179. if (mm_pmd_folded(mm))
  2180. return;
  2181. atomic_long_sub(PTRS_PER_PMD * sizeof(pmd_t), &mm->pgtables_bytes);
  2182. }
  2183. #endif
  2184. #ifdef CONFIG_MMU
  2185. static inline void mm_pgtables_bytes_init(struct mm_struct *mm)
  2186. {
  2187. atomic_long_set(&mm->pgtables_bytes, 0);
  2188. }
  2189. static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm)
  2190. {
  2191. return atomic_long_read(&mm->pgtables_bytes);
  2192. }
  2193. static inline void mm_inc_nr_ptes(struct mm_struct *mm)
  2194. {
  2195. atomic_long_add(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes);
  2196. }
  2197. static inline void mm_dec_nr_ptes(struct mm_struct *mm)
  2198. {
  2199. atomic_long_sub(PTRS_PER_PTE * sizeof(pte_t), &mm->pgtables_bytes);
  2200. }
  2201. #else
  2202. static inline void mm_pgtables_bytes_init(struct mm_struct *mm) {}
  2203. static inline unsigned long mm_pgtables_bytes(const struct mm_struct *mm)
  2204. {
  2205. return 0;
  2206. }
  2207. static inline void mm_inc_nr_ptes(struct mm_struct *mm) {}
  2208. static inline void mm_dec_nr_ptes(struct mm_struct *mm) {}
  2209. #endif
  2210. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd);
  2211. int __pte_alloc_kernel(pmd_t *pmd);
  2212. #if defined(CONFIG_MMU)
  2213. static inline p4d_t *p4d_alloc(struct mm_struct *mm, pgd_t *pgd,
  2214. unsigned long address)
  2215. {
  2216. return (unlikely(pgd_none(*pgd)) && __p4d_alloc(mm, pgd, address)) ?
  2217. NULL : p4d_offset(pgd, address);
  2218. }
  2219. static inline pud_t *pud_alloc(struct mm_struct *mm, p4d_t *p4d,
  2220. unsigned long address)
  2221. {
  2222. return (unlikely(p4d_none(*p4d)) && __pud_alloc(mm, p4d, address)) ?
  2223. NULL : pud_offset(p4d, address);
  2224. }
  2225. static inline pmd_t *pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  2226. {
  2227. return (unlikely(pud_none(*pud)) && __pmd_alloc(mm, pud, address))?
  2228. NULL: pmd_offset(pud, address);
  2229. }
  2230. #endif /* CONFIG_MMU */
  2231. #if USE_SPLIT_PTE_PTLOCKS
  2232. #if ALLOC_SPLIT_PTLOCKS
  2233. void __init ptlock_cache_init(void);
  2234. extern bool ptlock_alloc(struct page *page);
  2235. extern void ptlock_free(struct page *page);
  2236. static inline spinlock_t *ptlock_ptr(struct page *page)
  2237. {
  2238. return page->ptl;
  2239. }
  2240. #else /* ALLOC_SPLIT_PTLOCKS */
  2241. static inline void ptlock_cache_init(void)
  2242. {
  2243. }
  2244. static inline bool ptlock_alloc(struct page *page)
  2245. {
  2246. return true;
  2247. }
  2248. static inline void ptlock_free(struct page *page)
  2249. {
  2250. }
  2251. static inline spinlock_t *ptlock_ptr(struct page *page)
  2252. {
  2253. return &page->ptl;
  2254. }
  2255. #endif /* ALLOC_SPLIT_PTLOCKS */
  2256. static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
  2257. {
  2258. return ptlock_ptr(pmd_page(*pmd));
  2259. }
  2260. static inline bool ptlock_init(struct page *page)
  2261. {
  2262. /*
  2263. * prep_new_page() initialize page->private (and therefore page->ptl)
  2264. * with 0. Make sure nobody took it in use in between.
  2265. *
  2266. * It can happen if arch try to use slab for page table allocation:
  2267. * slab code uses page->slab_cache, which share storage with page->ptl.
  2268. */
  2269. VM_BUG_ON_PAGE(*(unsigned long *)&page->ptl, page);
  2270. if (!ptlock_alloc(page))
  2271. return false;
  2272. spin_lock_init(ptlock_ptr(page));
  2273. return true;
  2274. }
  2275. #else /* !USE_SPLIT_PTE_PTLOCKS */
  2276. /*
  2277. * We use mm->page_table_lock to guard all pagetable pages of the mm.
  2278. */
  2279. static inline spinlock_t *pte_lockptr(struct mm_struct *mm, pmd_t *pmd)
  2280. {
  2281. return &mm->page_table_lock;
  2282. }
  2283. static inline void ptlock_cache_init(void) {}
  2284. static inline bool ptlock_init(struct page *page) { return true; }
  2285. static inline void ptlock_free(struct page *page) {}
  2286. #endif /* USE_SPLIT_PTE_PTLOCKS */
  2287. static inline void pgtable_init(void)
  2288. {
  2289. ptlock_cache_init();
  2290. pgtable_cache_init();
  2291. }
  2292. static inline bool pgtable_pte_page_ctor(struct page *page)
  2293. {
  2294. if (!ptlock_init(page))
  2295. return false;
  2296. __SetPageTable(page);
  2297. inc_lruvec_page_state(page, NR_PAGETABLE);
  2298. return true;
  2299. }
  2300. static inline void pgtable_pte_page_dtor(struct page *page)
  2301. {
  2302. ptlock_free(page);
  2303. __ClearPageTable(page);
  2304. dec_lruvec_page_state(page, NR_PAGETABLE);
  2305. }
  2306. #define pte_offset_map_lock(mm, pmd, address, ptlp) \
  2307. ({ \
  2308. spinlock_t *__ptl = pte_lockptr(mm, pmd); \
  2309. pte_t *__pte = pte_offset_map(pmd, address); \
  2310. *(ptlp) = __ptl; \
  2311. spin_lock(__ptl); \
  2312. __pte; \
  2313. })
  2314. #define pte_unmap_unlock(pte, ptl) do { \
  2315. spin_unlock(ptl); \
  2316. pte_unmap(pte); \
  2317. } while (0)
  2318. #define pte_alloc(mm, pmd) (unlikely(pmd_none(*(pmd))) && __pte_alloc(mm, pmd))
  2319. #define pte_alloc_map(mm, pmd, address) \
  2320. (pte_alloc(mm, pmd) ? NULL : pte_offset_map(pmd, address))
  2321. #define pte_alloc_map_lock(mm, pmd, address, ptlp) \
  2322. (pte_alloc(mm, pmd) ? \
  2323. NULL : pte_offset_map_lock(mm, pmd, address, ptlp))
  2324. #define pte_alloc_kernel(pmd, address) \
  2325. ((unlikely(pmd_none(*(pmd))) && __pte_alloc_kernel(pmd))? \
  2326. NULL: pte_offset_kernel(pmd, address))
  2327. #if USE_SPLIT_PMD_PTLOCKS
  2328. static struct page *pmd_to_page(pmd_t *pmd)
  2329. {
  2330. unsigned long mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
  2331. return virt_to_page((void *)((unsigned long) pmd & mask));
  2332. }
  2333. static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
  2334. {
  2335. return ptlock_ptr(pmd_to_page(pmd));
  2336. }
  2337. static inline bool pmd_ptlock_init(struct page *page)
  2338. {
  2339. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  2340. page->pmd_huge_pte = NULL;
  2341. #endif
  2342. return ptlock_init(page);
  2343. }
  2344. static inline void pmd_ptlock_free(struct page *page)
  2345. {
  2346. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  2347. VM_BUG_ON_PAGE(page->pmd_huge_pte, page);
  2348. #endif
  2349. ptlock_free(page);
  2350. }
  2351. #define pmd_huge_pte(mm, pmd) (pmd_to_page(pmd)->pmd_huge_pte)
  2352. #else
  2353. static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
  2354. {
  2355. return &mm->page_table_lock;
  2356. }
  2357. static inline bool pmd_ptlock_init(struct page *page) { return true; }
  2358. static inline void pmd_ptlock_free(struct page *page) {}
  2359. #define pmd_huge_pte(mm, pmd) ((mm)->pmd_huge_pte)
  2360. #endif
  2361. static inline spinlock_t *pmd_lock(struct mm_struct *mm, pmd_t *pmd)
  2362. {
  2363. spinlock_t *ptl = pmd_lockptr(mm, pmd);
  2364. spin_lock(ptl);
  2365. return ptl;
  2366. }
  2367. static inline bool pgtable_pmd_page_ctor(struct page *page)
  2368. {
  2369. if (!pmd_ptlock_init(page))
  2370. return false;
  2371. __SetPageTable(page);
  2372. inc_lruvec_page_state(page, NR_PAGETABLE);
  2373. return true;
  2374. }
  2375. static inline void pgtable_pmd_page_dtor(struct page *page)
  2376. {
  2377. pmd_ptlock_free(page);
  2378. __ClearPageTable(page);
  2379. dec_lruvec_page_state(page, NR_PAGETABLE);
  2380. }
  2381. /*
  2382. * No scalability reason to split PUD locks yet, but follow the same pattern
  2383. * as the PMD locks to make it easier if we decide to. The VM should not be
  2384. * considered ready to switch to split PUD locks yet; there may be places
  2385. * which need to be converted from page_table_lock.
  2386. */
  2387. static inline spinlock_t *pud_lockptr(struct mm_struct *mm, pud_t *pud)
  2388. {
  2389. return &mm->page_table_lock;
  2390. }
  2391. static inline spinlock_t *pud_lock(struct mm_struct *mm, pud_t *pud)
  2392. {
  2393. spinlock_t *ptl = pud_lockptr(mm, pud);
  2394. spin_lock(ptl);
  2395. return ptl;
  2396. }
  2397. extern void __init pagecache_init(void);
  2398. extern void free_initmem(void);
  2399. /*
  2400. * Free reserved pages within range [PAGE_ALIGN(start), end & PAGE_MASK)
  2401. * into the buddy system. The freed pages will be poisoned with pattern
  2402. * "poison" if it's within range [0, UCHAR_MAX].
  2403. * Return pages freed into the buddy system.
  2404. */
  2405. extern unsigned long free_reserved_area(void *start, void *end,
  2406. int poison, const char *s);
  2407. extern void adjust_managed_page_count(struct page *page, long count);
  2408. extern void mem_init_print_info(void);
  2409. extern void reserve_bootmem_region(phys_addr_t start, phys_addr_t end);
  2410. /* Free the reserved page into the buddy system, so it gets managed. */
  2411. static inline void free_reserved_page(struct page *page)
  2412. {
  2413. ClearPageReserved(page);
  2414. init_page_count(page);
  2415. __free_page(page);
  2416. adjust_managed_page_count(page, 1);
  2417. }
  2418. #define free_highmem_page(page) free_reserved_page(page)
  2419. static inline void mark_page_reserved(struct page *page)
  2420. {
  2421. SetPageReserved(page);
  2422. adjust_managed_page_count(page, -1);
  2423. }
  2424. /*
  2425. * Default method to free all the __init memory into the buddy system.
  2426. * The freed pages will be poisoned with pattern "poison" if it's within
  2427. * range [0, UCHAR_MAX].
  2428. * Return pages freed into the buddy system.
  2429. */
  2430. static inline unsigned long free_initmem_default(int poison)
  2431. {
  2432. extern char __init_begin[], __init_end[];
  2433. return free_reserved_area(&__init_begin, &__init_end,
  2434. poison, "unused kernel image (initmem)");
  2435. }
  2436. static inline unsigned long get_num_physpages(void)
  2437. {
  2438. int nid;
  2439. unsigned long phys_pages = 0;
  2440. for_each_online_node(nid)
  2441. phys_pages += node_present_pages(nid);
  2442. return phys_pages;
  2443. }
  2444. /*
  2445. * Using memblock node mappings, an architecture may initialise its
  2446. * zones, allocate the backing mem_map and account for memory holes in an
  2447. * architecture independent manner.
  2448. *
  2449. * An architecture is expected to register range of page frames backed by
  2450. * physical memory with memblock_add[_node]() before calling
  2451. * free_area_init() passing in the PFN each zone ends at. At a basic
  2452. * usage, an architecture is expected to do something like
  2453. *
  2454. * unsigned long max_zone_pfns[MAX_NR_ZONES] = {max_dma, max_normal_pfn,
  2455. * max_highmem_pfn};
  2456. * for_each_valid_physical_page_range()
  2457. * memblock_add_node(base, size, nid, MEMBLOCK_NONE)
  2458. * free_area_init(max_zone_pfns);
  2459. */
  2460. void free_area_init(unsigned long *max_zone_pfn);
  2461. unsigned long node_map_pfn_alignment(void);
  2462. unsigned long __absent_pages_in_range(int nid, unsigned long start_pfn,
  2463. unsigned long end_pfn);
  2464. extern unsigned long absent_pages_in_range(unsigned long start_pfn,
  2465. unsigned long end_pfn);
  2466. extern void get_pfn_range_for_nid(unsigned int nid,
  2467. unsigned long *start_pfn, unsigned long *end_pfn);
  2468. #ifndef CONFIG_NUMA
  2469. static inline int early_pfn_to_nid(unsigned long pfn)
  2470. {
  2471. return 0;
  2472. }
  2473. #else
  2474. /* please see mm/page_alloc.c */
  2475. extern int __meminit early_pfn_to_nid(unsigned long pfn);
  2476. #endif
  2477. extern void set_dma_reserve(unsigned long new_dma_reserve);
  2478. extern void memmap_init_range(unsigned long, int, unsigned long,
  2479. unsigned long, unsigned long, enum meminit_context,
  2480. struct vmem_altmap *, int migratetype);
  2481. extern void setup_per_zone_wmarks(void);
  2482. extern void calculate_min_free_kbytes(void);
  2483. extern int __meminit init_per_zone_wmark_min(void);
  2484. extern void mem_init(void);
  2485. extern void __init mmap_init(void);
  2486. extern void __show_mem(unsigned int flags, nodemask_t *nodemask, int max_zone_idx);
  2487. static inline void show_mem(unsigned int flags, nodemask_t *nodemask)
  2488. {
  2489. __show_mem(flags, nodemask, MAX_NR_ZONES - 1);
  2490. }
  2491. extern long si_mem_available(void);
  2492. extern void si_meminfo(struct sysinfo * val);
  2493. extern void si_meminfo_node(struct sysinfo *val, int nid);
  2494. #ifdef __HAVE_ARCH_RESERVED_KERNEL_PAGES
  2495. extern unsigned long arch_reserved_kernel_pages(void);
  2496. #endif
  2497. extern __printf(3, 4)
  2498. void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...);
  2499. extern void setup_per_cpu_pageset(void);
  2500. /* page_alloc.c */
  2501. extern int min_free_kbytes;
  2502. extern int watermark_boost_factor;
  2503. extern int watermark_scale_factor;
  2504. extern bool arch_has_descending_max_zone_pfns(void);
  2505. /* nommu.c */
  2506. extern atomic_long_t mmap_pages_allocated;
  2507. extern int nommu_shrink_inode_mappings(struct inode *, size_t, size_t);
  2508. /* interval_tree.c */
  2509. void vma_interval_tree_insert(struct vm_area_struct *node,
  2510. struct rb_root_cached *root);
  2511. void vma_interval_tree_insert_after(struct vm_area_struct *node,
  2512. struct vm_area_struct *prev,
  2513. struct rb_root_cached *root);
  2514. void vma_interval_tree_remove(struct vm_area_struct *node,
  2515. struct rb_root_cached *root);
  2516. struct vm_area_struct *vma_interval_tree_iter_first(struct rb_root_cached *root,
  2517. unsigned long start, unsigned long last);
  2518. struct vm_area_struct *vma_interval_tree_iter_next(struct vm_area_struct *node,
  2519. unsigned long start, unsigned long last);
  2520. #define vma_interval_tree_foreach(vma, root, start, last) \
  2521. for (vma = vma_interval_tree_iter_first(root, start, last); \
  2522. vma; vma = vma_interval_tree_iter_next(vma, start, last))
  2523. void anon_vma_interval_tree_insert(struct anon_vma_chain *node,
  2524. struct rb_root_cached *root);
  2525. void anon_vma_interval_tree_remove(struct anon_vma_chain *node,
  2526. struct rb_root_cached *root);
  2527. struct anon_vma_chain *
  2528. anon_vma_interval_tree_iter_first(struct rb_root_cached *root,
  2529. unsigned long start, unsigned long last);
  2530. struct anon_vma_chain *anon_vma_interval_tree_iter_next(
  2531. struct anon_vma_chain *node, unsigned long start, unsigned long last);
  2532. #ifdef CONFIG_DEBUG_VM_RB
  2533. void anon_vma_interval_tree_verify(struct anon_vma_chain *node);
  2534. #endif
  2535. #define anon_vma_interval_tree_foreach(avc, root, start, last) \
  2536. for (avc = anon_vma_interval_tree_iter_first(root, start, last); \
  2537. avc; avc = anon_vma_interval_tree_iter_next(avc, start, last))
  2538. /* mmap.c */
  2539. extern int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin);
  2540. extern int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
  2541. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert,
  2542. struct vm_area_struct *expand);
  2543. static inline int vma_adjust(struct vm_area_struct *vma, unsigned long start,
  2544. unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
  2545. {
  2546. return __vma_adjust(vma, start, end, pgoff, insert, NULL);
  2547. }
  2548. extern struct vm_area_struct *vma_merge(struct mm_struct *,
  2549. struct vm_area_struct *prev, unsigned long addr, unsigned long end,
  2550. unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
  2551. struct mempolicy *, struct vm_userfaultfd_ctx, struct anon_vma_name *);
  2552. extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
  2553. extern int __split_vma(struct mm_struct *, struct vm_area_struct *,
  2554. unsigned long addr, int new_below);
  2555. extern int split_vma(struct mm_struct *, struct vm_area_struct *,
  2556. unsigned long addr, int new_below);
  2557. extern int insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
  2558. extern void unlink_file_vma(struct vm_area_struct *);
  2559. extern struct vm_area_struct *copy_vma(struct vm_area_struct **,
  2560. unsigned long addr, unsigned long len, pgoff_t pgoff,
  2561. bool *need_rmap_locks);
  2562. extern void exit_mmap(struct mm_struct *);
  2563. void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas);
  2564. void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas);
  2565. static inline int check_data_rlimit(unsigned long rlim,
  2566. unsigned long new,
  2567. unsigned long start,
  2568. unsigned long end_data,
  2569. unsigned long start_data)
  2570. {
  2571. if (rlim < RLIM_INFINITY) {
  2572. if (((new - start) + (end_data - start_data)) > rlim)
  2573. return -ENOSPC;
  2574. }
  2575. return 0;
  2576. }
  2577. extern int mm_take_all_locks(struct mm_struct *mm);
  2578. extern void mm_drop_all_locks(struct mm_struct *mm);
  2579. extern int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
  2580. extern int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
  2581. extern struct file *get_mm_exe_file(struct mm_struct *mm);
  2582. extern struct file *get_task_exe_file(struct task_struct *task);
  2583. extern bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long npages);
  2584. extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages);
  2585. extern bool vma_is_special_mapping(const struct vm_area_struct *vma,
  2586. const struct vm_special_mapping *sm);
  2587. extern struct vm_area_struct *_install_special_mapping(struct mm_struct *mm,
  2588. unsigned long addr, unsigned long len,
  2589. unsigned long flags,
  2590. const struct vm_special_mapping *spec);
  2591. /* This is an obsolete alternative to _install_special_mapping. */
  2592. extern int install_special_mapping(struct mm_struct *mm,
  2593. unsigned long addr, unsigned long len,
  2594. unsigned long flags, struct page **pages);
  2595. unsigned long randomize_stack_top(unsigned long stack_top);
  2596. unsigned long randomize_page(unsigned long start, unsigned long range);
  2597. extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  2598. extern unsigned long mmap_region(struct file *file, unsigned long addr,
  2599. unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
  2600. struct list_head *uf);
  2601. extern unsigned long do_mmap(struct file *file, unsigned long addr,
  2602. unsigned long len, unsigned long prot, unsigned long flags,
  2603. unsigned long pgoff, unsigned long *populate, struct list_head *uf);
  2604. extern int do_mas_munmap(struct ma_state *mas, struct mm_struct *mm,
  2605. unsigned long start, size_t len, struct list_head *uf,
  2606. bool downgrade);
  2607. extern int do_munmap(struct mm_struct *, unsigned long, size_t,
  2608. struct list_head *uf);
  2609. extern int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behavior);
  2610. #ifdef CONFIG_MMU
  2611. extern int __mm_populate(unsigned long addr, unsigned long len,
  2612. int ignore_errors);
  2613. static inline void mm_populate(unsigned long addr, unsigned long len)
  2614. {
  2615. /* Ignore errors */
  2616. (void) __mm_populate(addr, len, 1);
  2617. }
  2618. #else
  2619. static inline void mm_populate(unsigned long addr, unsigned long len) {}
  2620. #endif
  2621. /* These take the mm semaphore themselves */
  2622. extern int __must_check vm_brk(unsigned long, unsigned long);
  2623. extern int __must_check vm_brk_flags(unsigned long, unsigned long, unsigned long);
  2624. extern int vm_munmap(unsigned long, size_t);
  2625. extern unsigned long __must_check vm_mmap(struct file *, unsigned long,
  2626. unsigned long, unsigned long,
  2627. unsigned long, unsigned long);
  2628. struct vm_unmapped_area_info {
  2629. #define VM_UNMAPPED_AREA_TOPDOWN 1
  2630. unsigned long flags;
  2631. unsigned long length;
  2632. unsigned long low_limit;
  2633. unsigned long high_limit;
  2634. unsigned long align_mask;
  2635. unsigned long align_offset;
  2636. };
  2637. extern unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info);
  2638. /* truncate.c */
  2639. extern void truncate_inode_pages(struct address_space *, loff_t);
  2640. extern void truncate_inode_pages_range(struct address_space *,
  2641. loff_t lstart, loff_t lend);
  2642. extern void truncate_inode_pages_final(struct address_space *);
  2643. /* generic vm_area_ops exported for stackable file systems */
  2644. extern vm_fault_t filemap_fault(struct vm_fault *vmf);
  2645. extern vm_fault_t filemap_map_pages(struct vm_fault *vmf,
  2646. pgoff_t start_pgoff, pgoff_t end_pgoff);
  2647. extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf);
  2648. extern unsigned long stack_guard_gap;
  2649. /* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */
  2650. int expand_stack_locked(struct vm_area_struct *vma, unsigned long address);
  2651. struct vm_area_struct *expand_stack(struct mm_struct * mm, unsigned long addr);
  2652. /* CONFIG_STACK_GROWSUP still needs to grow downwards at some places */
  2653. int expand_downwards(struct vm_area_struct *vma, unsigned long address);
  2654. /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
  2655. extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
  2656. extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
  2657. struct vm_area_struct **pprev);
  2658. /*
  2659. * Look up the first VMA which intersects the interval [start_addr, end_addr)
  2660. * NULL if none. Assume start_addr < end_addr.
  2661. */
  2662. struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
  2663. unsigned long start_addr, unsigned long end_addr);
  2664. /**
  2665. * vma_lookup() - Find a VMA at a specific address
  2666. * @mm: The process address space.
  2667. * @addr: The user address.
  2668. *
  2669. * Return: The vm_area_struct at the given address, %NULL otherwise.
  2670. */
  2671. static inline
  2672. struct vm_area_struct *vma_lookup(struct mm_struct *mm, unsigned long addr)
  2673. {
  2674. return mtree_load(&mm->mm_mt, addr);
  2675. }
  2676. static inline unsigned long vm_start_gap(struct vm_area_struct *vma)
  2677. {
  2678. unsigned long vm_start = vma->vm_start;
  2679. if (vma->vm_flags & VM_GROWSDOWN) {
  2680. vm_start -= stack_guard_gap;
  2681. if (vm_start > vma->vm_start)
  2682. vm_start = 0;
  2683. }
  2684. return vm_start;
  2685. }
  2686. static inline unsigned long vm_end_gap(struct vm_area_struct *vma)
  2687. {
  2688. unsigned long vm_end = vma->vm_end;
  2689. if (vma->vm_flags & VM_GROWSUP) {
  2690. vm_end += stack_guard_gap;
  2691. if (vm_end < vma->vm_end)
  2692. vm_end = -PAGE_SIZE;
  2693. }
  2694. return vm_end;
  2695. }
  2696. static inline unsigned long vma_pages(struct vm_area_struct *vma)
  2697. {
  2698. return (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  2699. }
  2700. /* Look up the first VMA which exactly match the interval vm_start ... vm_end */
  2701. static inline struct vm_area_struct *find_exact_vma(struct mm_struct *mm,
  2702. unsigned long vm_start, unsigned long vm_end)
  2703. {
  2704. struct vm_area_struct *vma = vma_lookup(mm, vm_start);
  2705. if (vma && (vma->vm_start != vm_start || vma->vm_end != vm_end))
  2706. vma = NULL;
  2707. return vma;
  2708. }
  2709. static inline bool range_in_vma(struct vm_area_struct *vma,
  2710. unsigned long start, unsigned long end)
  2711. {
  2712. return (vma && vma->vm_start <= start && end <= vma->vm_end);
  2713. }
  2714. #ifdef CONFIG_MMU
  2715. pgprot_t vm_get_page_prot(unsigned long vm_flags);
  2716. void vma_set_page_prot(struct vm_area_struct *vma);
  2717. #else
  2718. static inline pgprot_t vm_get_page_prot(unsigned long vm_flags)
  2719. {
  2720. return __pgprot(0);
  2721. }
  2722. static inline void vma_set_page_prot(struct vm_area_struct *vma)
  2723. {
  2724. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  2725. }
  2726. #endif
  2727. void vma_set_file(struct vm_area_struct *vma, struct file *file);
  2728. #ifdef CONFIG_NUMA_BALANCING
  2729. unsigned long change_prot_numa(struct vm_area_struct *vma,
  2730. unsigned long start, unsigned long end);
  2731. #endif
  2732. struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr);
  2733. struct vm_area_struct *find_extend_vma_locked(struct mm_struct *,
  2734. unsigned long addr);
  2735. int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
  2736. unsigned long pfn, unsigned long size, pgprot_t);
  2737. int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,
  2738. unsigned long pfn, unsigned long size, pgprot_t prot);
  2739. int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *);
  2740. int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr,
  2741. struct page **pages, unsigned long *num);
  2742. int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
  2743. unsigned long num);
  2744. int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
  2745. unsigned long num);
  2746. vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  2747. unsigned long pfn);
  2748. vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
  2749. unsigned long pfn, pgprot_t pgprot);
  2750. vm_fault_t vmf_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
  2751. pfn_t pfn);
  2752. vm_fault_t vmf_insert_mixed_prot(struct vm_area_struct *vma, unsigned long addr,
  2753. pfn_t pfn, pgprot_t pgprot);
  2754. vm_fault_t vmf_insert_mixed_mkwrite(struct vm_area_struct *vma,
  2755. unsigned long addr, pfn_t pfn);
  2756. int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len);
  2757. static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma,
  2758. unsigned long addr, struct page *page)
  2759. {
  2760. int err = vm_insert_page(vma, addr, page);
  2761. if (err == -ENOMEM)
  2762. return VM_FAULT_OOM;
  2763. if (err < 0 && err != -EBUSY)
  2764. return VM_FAULT_SIGBUS;
  2765. return VM_FAULT_NOPAGE;
  2766. }
  2767. #ifndef io_remap_pfn_range
  2768. static inline int io_remap_pfn_range(struct vm_area_struct *vma,
  2769. unsigned long addr, unsigned long pfn,
  2770. unsigned long size, pgprot_t prot)
  2771. {
  2772. return remap_pfn_range(vma, addr, pfn, size, pgprot_decrypted(prot));
  2773. }
  2774. #endif
  2775. static inline vm_fault_t vmf_error(int err)
  2776. {
  2777. if (err == -ENOMEM)
  2778. return VM_FAULT_OOM;
  2779. return VM_FAULT_SIGBUS;
  2780. }
  2781. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  2782. unsigned int foll_flags);
  2783. #define FOLL_WRITE 0x01 /* check pte is writable */
  2784. #define FOLL_TOUCH 0x02 /* mark page accessed */
  2785. #define FOLL_GET 0x04 /* do get_page on page */
  2786. #define FOLL_DUMP 0x08 /* give error on hole if it would be zero */
  2787. #define FOLL_FORCE 0x10 /* get_user_pages read/write w/o permission */
  2788. #define FOLL_NOWAIT 0x20 /* if a disk transfer is needed, start the IO
  2789. * and return without waiting upon it */
  2790. #define FOLL_NOFAULT 0x80 /* do not fault in pages */
  2791. #define FOLL_HWPOISON 0x100 /* check page is hwpoisoned */
  2792. #define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */
  2793. #define FOLL_TRIED 0x800 /* a retry, previous pass started an IO */
  2794. #define FOLL_REMOTE 0x2000 /* we are working on non-current tsk/mm */
  2795. #define FOLL_ANON 0x8000 /* don't do file mappings */
  2796. #define FOLL_LONGTERM 0x10000 /* mapping lifetime is indefinite: see below */
  2797. #define FOLL_SPLIT_PMD 0x20000 /* split huge pmd before returning */
  2798. #define FOLL_PIN 0x40000 /* pages must be released via unpin_user_page */
  2799. #define FOLL_FAST_ONLY 0x80000 /* gup_fast: prevent fall-back to slow gup */
  2800. /*
  2801. * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each
  2802. * other. Here is what they mean, and how to use them:
  2803. *
  2804. * FOLL_LONGTERM indicates that the page will be held for an indefinite time
  2805. * period _often_ under userspace control. This is in contrast to
  2806. * iov_iter_get_pages(), whose usages are transient.
  2807. *
  2808. * FIXME: For pages which are part of a filesystem, mappings are subject to the
  2809. * lifetime enforced by the filesystem and we need guarantees that longterm
  2810. * users like RDMA and V4L2 only establish mappings which coordinate usage with
  2811. * the filesystem. Ideas for this coordination include revoking the longterm
  2812. * pin, delaying writeback, bounce buffer page writeback, etc. As FS DAX was
  2813. * added after the problem with filesystems was found FS DAX VMAs are
  2814. * specifically failed. Filesystem pages are still subject to bugs and use of
  2815. * FOLL_LONGTERM should be avoided on those pages.
  2816. *
  2817. * FIXME: Also NOTE that FOLL_LONGTERM is not supported in every GUP call.
  2818. * Currently only get_user_pages() and get_user_pages_fast() support this flag
  2819. * and calls to get_user_pages_[un]locked are specifically not allowed. This
  2820. * is due to an incompatibility with the FS DAX check and
  2821. * FAULT_FLAG_ALLOW_RETRY.
  2822. *
  2823. * In the CMA case: long term pins in a CMA region would unnecessarily fragment
  2824. * that region. And so, CMA attempts to migrate the page before pinning, when
  2825. * FOLL_LONGTERM is specified.
  2826. *
  2827. * FOLL_PIN indicates that a special kind of tracking (not just page->_refcount,
  2828. * but an additional pin counting system) will be invoked. This is intended for
  2829. * anything that gets a page reference and then touches page data (for example,
  2830. * Direct IO). This lets the filesystem know that some non-file-system entity is
  2831. * potentially changing the pages' data. In contrast to FOLL_GET (whose pages
  2832. * are released via put_page()), FOLL_PIN pages must be released, ultimately, by
  2833. * a call to unpin_user_page().
  2834. *
  2835. * FOLL_PIN is similar to FOLL_GET: both of these pin pages. They use different
  2836. * and separate refcounting mechanisms, however, and that means that each has
  2837. * its own acquire and release mechanisms:
  2838. *
  2839. * FOLL_GET: get_user_pages*() to acquire, and put_page() to release.
  2840. *
  2841. * FOLL_PIN: pin_user_pages*() to acquire, and unpin_user_pages to release.
  2842. *
  2843. * FOLL_PIN and FOLL_GET are mutually exclusive for a given function call.
  2844. * (The underlying pages may experience both FOLL_GET-based and FOLL_PIN-based
  2845. * calls applied to them, and that's perfectly OK. This is a constraint on the
  2846. * callers, not on the pages.)
  2847. *
  2848. * FOLL_PIN should be set internally by the pin_user_pages*() APIs, never
  2849. * directly by the caller. That's in order to help avoid mismatches when
  2850. * releasing pages: get_user_pages*() pages must be released via put_page(),
  2851. * while pin_user_pages*() pages must be released via unpin_user_page().
  2852. *
  2853. * Please see Documentation/core-api/pin_user_pages.rst for more information.
  2854. */
  2855. static inline int vm_fault_to_errno(vm_fault_t vm_fault, int foll_flags)
  2856. {
  2857. if (vm_fault & VM_FAULT_OOM)
  2858. return -ENOMEM;
  2859. if (vm_fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  2860. return (foll_flags & FOLL_HWPOISON) ? -EHWPOISON : -EFAULT;
  2861. if (vm_fault & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  2862. return -EFAULT;
  2863. return 0;
  2864. }
  2865. /*
  2866. * Indicates for which pages that are write-protected in the page table,
  2867. * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the
  2868. * GUP pin will remain consistent with the pages mapped into the page tables
  2869. * of the MM.
  2870. *
  2871. * Temporary unmapping of PageAnonExclusive() pages or clearing of
  2872. * PageAnonExclusive() has to protect against concurrent GUP:
  2873. * * Ordinary GUP: Using the PT lock
  2874. * * GUP-fast and fork(): mm->write_protect_seq
  2875. * * GUP-fast and KSM or temporary unmapping (swap, migration): see
  2876. * page_try_share_anon_rmap()
  2877. *
  2878. * Must be called with the (sub)page that's actually referenced via the
  2879. * page table entry, which might not necessarily be the head page for a
  2880. * PTE-mapped THP.
  2881. */
  2882. static inline bool gup_must_unshare(unsigned int flags, struct page *page)
  2883. {
  2884. /*
  2885. * FOLL_WRITE is implicitly handled correctly as the page table entry
  2886. * has to be writable -- and if it references (part of) an anonymous
  2887. * folio, that part is required to be marked exclusive.
  2888. */
  2889. if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
  2890. return false;
  2891. /*
  2892. * Note: PageAnon(page) is stable until the page is actually getting
  2893. * freed.
  2894. */
  2895. if (!PageAnon(page))
  2896. return false;
  2897. /* Paired with a memory barrier in page_try_share_anon_rmap(). */
  2898. if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
  2899. smp_rmb();
  2900. /*
  2901. * During GUP-fast we might not get called on the head page for a
  2902. * hugetlb page that is mapped using cont-PTE, because GUP-fast does
  2903. * not work with the abstracted hugetlb PTEs that always point at the
  2904. * head page. For hugetlb, PageAnonExclusive only applies on the head
  2905. * page (as it cannot be partially COW-shared), so lookup the head page.
  2906. */
  2907. if (unlikely(!PageHead(page) && PageHuge(page)))
  2908. page = compound_head(page);
  2909. /*
  2910. * Note that PageKsm() pages cannot be exclusive, and consequently,
  2911. * cannot get pinned.
  2912. */
  2913. return !PageAnonExclusive(page);
  2914. }
  2915. /*
  2916. * Indicates whether GUP can follow a PROT_NONE mapped page, or whether
  2917. * a (NUMA hinting) fault is required.
  2918. */
  2919. static inline bool gup_can_follow_protnone(unsigned int flags)
  2920. {
  2921. /*
  2922. * FOLL_FORCE has to be able to make progress even if the VMA is
  2923. * inaccessible. Further, FOLL_FORCE access usually does not represent
  2924. * application behaviour and we should avoid triggering NUMA hinting
  2925. * faults.
  2926. */
  2927. return flags & FOLL_FORCE;
  2928. }
  2929. typedef int (*pte_fn_t)(pte_t *pte, unsigned long addr, void *data);
  2930. extern int apply_to_page_range(struct mm_struct *mm, unsigned long address,
  2931. unsigned long size, pte_fn_t fn, void *data);
  2932. extern int apply_to_existing_page_range(struct mm_struct *mm,
  2933. unsigned long address, unsigned long size,
  2934. pte_fn_t fn, void *data);
  2935. extern void __init init_mem_debugging_and_hardening(void);
  2936. #ifdef CONFIG_PAGE_POISONING
  2937. extern void __kernel_poison_pages(struct page *page, int numpages);
  2938. extern void __kernel_unpoison_pages(struct page *page, int numpages);
  2939. extern bool _page_poisoning_enabled_early;
  2940. DECLARE_STATIC_KEY_FALSE(_page_poisoning_enabled);
  2941. static inline bool page_poisoning_enabled(void)
  2942. {
  2943. return _page_poisoning_enabled_early;
  2944. }
  2945. /*
  2946. * For use in fast paths after init_mem_debugging() has run, or when a
  2947. * false negative result is not harmful when called too early.
  2948. */
  2949. static inline bool page_poisoning_enabled_static(void)
  2950. {
  2951. return static_branch_unlikely(&_page_poisoning_enabled);
  2952. }
  2953. static inline void kernel_poison_pages(struct page *page, int numpages)
  2954. {
  2955. if (page_poisoning_enabled_static())
  2956. __kernel_poison_pages(page, numpages);
  2957. }
  2958. static inline void kernel_unpoison_pages(struct page *page, int numpages)
  2959. {
  2960. if (page_poisoning_enabled_static())
  2961. __kernel_unpoison_pages(page, numpages);
  2962. }
  2963. #else
  2964. static inline bool page_poisoning_enabled(void) { return false; }
  2965. static inline bool page_poisoning_enabled_static(void) { return false; }
  2966. static inline void __kernel_poison_pages(struct page *page, int nunmpages) { }
  2967. static inline void kernel_poison_pages(struct page *page, int numpages) { }
  2968. static inline void kernel_unpoison_pages(struct page *page, int numpages) { }
  2969. #endif
  2970. DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc);
  2971. static inline bool want_init_on_alloc(gfp_t flags)
  2972. {
  2973. if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON,
  2974. &init_on_alloc))
  2975. return true;
  2976. return flags & __GFP_ZERO;
  2977. }
  2978. DECLARE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free);
  2979. static inline bool want_init_on_free(void)
  2980. {
  2981. return static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON,
  2982. &init_on_free);
  2983. }
  2984. extern bool _debug_pagealloc_enabled_early;
  2985. DECLARE_STATIC_KEY_FALSE(_debug_pagealloc_enabled);
  2986. static inline bool debug_pagealloc_enabled(void)
  2987. {
  2988. return IS_ENABLED(CONFIG_DEBUG_PAGEALLOC) &&
  2989. _debug_pagealloc_enabled_early;
  2990. }
  2991. /*
  2992. * For use in fast paths after init_debug_pagealloc() has run, or when a
  2993. * false negative result is not harmful when called too early.
  2994. */
  2995. static inline bool debug_pagealloc_enabled_static(void)
  2996. {
  2997. if (!IS_ENABLED(CONFIG_DEBUG_PAGEALLOC))
  2998. return false;
  2999. return static_branch_unlikely(&_debug_pagealloc_enabled);
  3000. }
  3001. #ifdef CONFIG_DEBUG_PAGEALLOC
  3002. /*
  3003. * To support DEBUG_PAGEALLOC architecture must ensure that
  3004. * __kernel_map_pages() never fails
  3005. */
  3006. extern void __kernel_map_pages(struct page *page, int numpages, int enable);
  3007. static inline void debug_pagealloc_map_pages(struct page *page, int numpages)
  3008. {
  3009. if (debug_pagealloc_enabled_static())
  3010. __kernel_map_pages(page, numpages, 1);
  3011. }
  3012. static inline void debug_pagealloc_unmap_pages(struct page *page, int numpages)
  3013. {
  3014. if (debug_pagealloc_enabled_static())
  3015. __kernel_map_pages(page, numpages, 0);
  3016. }
  3017. #else /* CONFIG_DEBUG_PAGEALLOC */
  3018. static inline void debug_pagealloc_map_pages(struct page *page, int numpages) {}
  3019. static inline void debug_pagealloc_unmap_pages(struct page *page, int numpages) {}
  3020. #endif /* CONFIG_DEBUG_PAGEALLOC */
  3021. #ifdef __HAVE_ARCH_GATE_AREA
  3022. extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
  3023. extern int in_gate_area_no_mm(unsigned long addr);
  3024. extern int in_gate_area(struct mm_struct *mm, unsigned long addr);
  3025. #else
  3026. static inline struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  3027. {
  3028. return NULL;
  3029. }
  3030. static inline int in_gate_area_no_mm(unsigned long addr) { return 0; }
  3031. static inline int in_gate_area(struct mm_struct *mm, unsigned long addr)
  3032. {
  3033. return 0;
  3034. }
  3035. #endif /* __HAVE_ARCH_GATE_AREA */
  3036. extern bool process_shares_mm(struct task_struct *p, struct mm_struct *mm);
  3037. #ifdef CONFIG_SYSCTL
  3038. extern int sysctl_drop_caches;
  3039. int drop_caches_sysctl_handler(struct ctl_table *, int, void *, size_t *,
  3040. loff_t *);
  3041. #endif
  3042. void drop_slab(void);
  3043. #ifndef CONFIG_MMU
  3044. #define randomize_va_space 0
  3045. #else
  3046. extern int randomize_va_space;
  3047. #endif
  3048. const char * arch_vma_name(struct vm_area_struct *vma);
  3049. #ifdef CONFIG_MMU
  3050. void print_vma_addr(char *prefix, unsigned long rip);
  3051. #else
  3052. static inline void print_vma_addr(char *prefix, unsigned long rip)
  3053. {
  3054. }
  3055. #endif
  3056. void *sparse_buffer_alloc(unsigned long size);
  3057. struct page * __populate_section_memmap(unsigned long pfn,
  3058. unsigned long nr_pages, int nid, struct vmem_altmap *altmap,
  3059. struct dev_pagemap *pgmap);
  3060. pgd_t *vmemmap_pgd_populate(unsigned long addr, int node);
  3061. p4d_t *vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node);
  3062. pud_t *vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node);
  3063. pmd_t *vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node);
  3064. pte_t *vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
  3065. struct vmem_altmap *altmap, struct page *reuse);
  3066. void *vmemmap_alloc_block(unsigned long size, int node);
  3067. struct vmem_altmap;
  3068. void *vmemmap_alloc_block_buf(unsigned long size, int node,
  3069. struct vmem_altmap *altmap);
  3070. void vmemmap_verify(pte_t *, int, unsigned long, unsigned long);
  3071. int vmemmap_populate_basepages(unsigned long start, unsigned long end,
  3072. int node, struct vmem_altmap *altmap);
  3073. int vmemmap_populate(unsigned long start, unsigned long end, int node,
  3074. struct vmem_altmap *altmap);
  3075. void vmemmap_populate_print_last(void);
  3076. #ifdef CONFIG_MEMORY_HOTPLUG
  3077. void vmemmap_free(unsigned long start, unsigned long end,
  3078. struct vmem_altmap *altmap);
  3079. #endif
  3080. void register_page_bootmem_memmap(unsigned long section_nr, struct page *map,
  3081. unsigned long nr_pages);
  3082. enum mf_flags {
  3083. MF_COUNT_INCREASED = 1 << 0,
  3084. MF_ACTION_REQUIRED = 1 << 1,
  3085. MF_MUST_KILL = 1 << 2,
  3086. MF_SOFT_OFFLINE = 1 << 3,
  3087. MF_UNPOISON = 1 << 4,
  3088. MF_SW_SIMULATED = 1 << 5,
  3089. MF_NO_RETRY = 1 << 6,
  3090. };
  3091. int mf_dax_kill_procs(struct address_space *mapping, pgoff_t index,
  3092. unsigned long count, int mf_flags);
  3093. extern int memory_failure(unsigned long pfn, int flags);
  3094. extern void memory_failure_queue_kick(int cpu);
  3095. extern int unpoison_memory(unsigned long pfn);
  3096. extern int sysctl_memory_failure_early_kill;
  3097. extern int sysctl_memory_failure_recovery;
  3098. extern void shake_page(struct page *p);
  3099. extern atomic_long_t num_poisoned_pages __read_mostly;
  3100. extern int soft_offline_page(unsigned long pfn, int flags);
  3101. #ifdef CONFIG_MEMORY_FAILURE
  3102. extern void memory_failure_queue(unsigned long pfn, int flags);
  3103. extern int __get_huge_page_for_hwpoison(unsigned long pfn, int flags);
  3104. #else
  3105. static inline void memory_failure_queue(unsigned long pfn, int flags)
  3106. {
  3107. }
  3108. static inline int __get_huge_page_for_hwpoison(unsigned long pfn, int flags)
  3109. {
  3110. return 0;
  3111. }
  3112. #endif
  3113. #ifndef arch_memory_failure
  3114. static inline int arch_memory_failure(unsigned long pfn, int flags)
  3115. {
  3116. return -ENXIO;
  3117. }
  3118. #endif
  3119. #ifndef arch_is_platform_page
  3120. static inline bool arch_is_platform_page(u64 paddr)
  3121. {
  3122. return false;
  3123. }
  3124. #endif
  3125. /*
  3126. * Error handlers for various types of pages.
  3127. */
  3128. enum mf_result {
  3129. MF_IGNORED, /* Error: cannot be handled */
  3130. MF_FAILED, /* Error: handling failed */
  3131. MF_DELAYED, /* Will be handled later */
  3132. MF_RECOVERED, /* Successfully recovered */
  3133. };
  3134. enum mf_action_page_type {
  3135. MF_MSG_KERNEL,
  3136. MF_MSG_KERNEL_HIGH_ORDER,
  3137. MF_MSG_SLAB,
  3138. MF_MSG_DIFFERENT_COMPOUND,
  3139. MF_MSG_HUGE,
  3140. MF_MSG_FREE_HUGE,
  3141. MF_MSG_UNMAP_FAILED,
  3142. MF_MSG_DIRTY_SWAPCACHE,
  3143. MF_MSG_CLEAN_SWAPCACHE,
  3144. MF_MSG_DIRTY_MLOCKED_LRU,
  3145. MF_MSG_CLEAN_MLOCKED_LRU,
  3146. MF_MSG_DIRTY_UNEVICTABLE_LRU,
  3147. MF_MSG_CLEAN_UNEVICTABLE_LRU,
  3148. MF_MSG_DIRTY_LRU,
  3149. MF_MSG_CLEAN_LRU,
  3150. MF_MSG_TRUNCATED_LRU,
  3151. MF_MSG_BUDDY,
  3152. MF_MSG_DAX,
  3153. MF_MSG_UNSPLIT_THP,
  3154. MF_MSG_UNKNOWN,
  3155. };
  3156. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  3157. extern void clear_huge_page(struct page *page,
  3158. unsigned long addr_hint,
  3159. unsigned int pages_per_huge_page);
  3160. extern void copy_user_huge_page(struct page *dst, struct page *src,
  3161. unsigned long addr_hint,
  3162. struct vm_area_struct *vma,
  3163. unsigned int pages_per_huge_page);
  3164. extern long copy_huge_page_from_user(struct page *dst_page,
  3165. const void __user *usr_src,
  3166. unsigned int pages_per_huge_page,
  3167. bool allow_pagefault);
  3168. /**
  3169. * vma_is_special_huge - Are transhuge page-table entries considered special?
  3170. * @vma: Pointer to the struct vm_area_struct to consider
  3171. *
  3172. * Whether transhuge page-table entries are considered "special" following
  3173. * the definition in vm_normal_page().
  3174. *
  3175. * Return: true if transhuge page-table entries should be considered special,
  3176. * false otherwise.
  3177. */
  3178. static inline bool vma_is_special_huge(const struct vm_area_struct *vma)
  3179. {
  3180. return vma_is_dax(vma) || (vma->vm_file &&
  3181. (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)));
  3182. }
  3183. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */
  3184. #ifdef CONFIG_DEBUG_PAGEALLOC
  3185. extern unsigned int _debug_guardpage_minorder;
  3186. DECLARE_STATIC_KEY_FALSE(_debug_guardpage_enabled);
  3187. static inline unsigned int debug_guardpage_minorder(void)
  3188. {
  3189. return _debug_guardpage_minorder;
  3190. }
  3191. static inline bool debug_guardpage_enabled(void)
  3192. {
  3193. return static_branch_unlikely(&_debug_guardpage_enabled);
  3194. }
  3195. static inline bool page_is_guard(struct page *page)
  3196. {
  3197. if (!debug_guardpage_enabled())
  3198. return false;
  3199. return PageGuard(page);
  3200. }
  3201. #else
  3202. static inline unsigned int debug_guardpage_minorder(void) { return 0; }
  3203. static inline bool debug_guardpage_enabled(void) { return false; }
  3204. static inline bool page_is_guard(struct page *page) { return false; }
  3205. #endif /* CONFIG_DEBUG_PAGEALLOC */
  3206. #if MAX_NUMNODES > 1
  3207. void __init setup_nr_node_ids(void);
  3208. #else
  3209. static inline void setup_nr_node_ids(void) {}
  3210. #endif
  3211. struct seq_file;
  3212. void seq_printf(struct seq_file *m, const char *f, ...);
  3213. static inline void show_val_meminfo(struct seq_file *m,
  3214. const char *str, long size)
  3215. {
  3216. char name[17];
  3217. int len = strlen(str);
  3218. if (len <= 15) {
  3219. sprintf(name, "%s:", str);
  3220. } else {
  3221. strncpy(name, str, 15);
  3222. name[15] = ':';
  3223. name[16] = '\0';
  3224. }
  3225. seq_printf(m, "%-16s%8ld kB\n", name, size);
  3226. }
  3227. extern int memcmp_pages(struct page *page1, struct page *page2);
  3228. static inline int pages_identical(struct page *page1, struct page *page2)
  3229. {
  3230. return !memcmp_pages(page1, page2);
  3231. }
  3232. #ifdef CONFIG_MAPPING_DIRTY_HELPERS
  3233. unsigned long clean_record_shared_mapping_range(struct address_space *mapping,
  3234. pgoff_t first_index, pgoff_t nr,
  3235. pgoff_t bitmap_pgoff,
  3236. unsigned long *bitmap,
  3237. pgoff_t *start,
  3238. pgoff_t *end);
  3239. unsigned long wp_shared_mapping_range(struct address_space *mapping,
  3240. pgoff_t first_index, pgoff_t nr);
  3241. #endif
  3242. extern int sysctl_nr_trim_pages;
  3243. extern int reclaim_shmem_address_space(struct address_space *mapping);
  3244. #ifdef CONFIG_PRINTK
  3245. void mem_dump_obj(void *object);
  3246. #else
  3247. static inline void mem_dump_obj(void *object) {}
  3248. #endif
  3249. /**
  3250. * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it
  3251. * @seals: the seals to check
  3252. * @vma: the vma to operate on
  3253. *
  3254. * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on
  3255. * the vma flags. Return 0 if check pass, or <0 for errors.
  3256. */
  3257. static inline int seal_check_future_write(int seals, struct vm_area_struct *vma)
  3258. {
  3259. if (seals & F_SEAL_FUTURE_WRITE) {
  3260. /*
  3261. * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
  3262. * "future write" seal active.
  3263. */
  3264. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
  3265. return -EPERM;
  3266. /*
  3267. * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as
  3268. * MAP_SHARED and read-only, take care to not allow mprotect to
  3269. * revert protections on such mappings. Do this only for shared
  3270. * mappings. For private mappings, don't need to mask
  3271. * VM_MAYWRITE as we still want them to be COW-writable.
  3272. */
  3273. if (vma->vm_flags & VM_SHARED)
  3274. vm_flags_clear(vma, VM_MAYWRITE);
  3275. }
  3276. return 0;
  3277. }
  3278. #ifdef CONFIG_ANON_VMA_NAME
  3279. int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
  3280. unsigned long len_in,
  3281. struct anon_vma_name *anon_name);
  3282. #else
  3283. static inline int
  3284. madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
  3285. unsigned long len_in, struct anon_vma_name *anon_name) {
  3286. return 0;
  3287. }
  3288. #endif
  3289. #define GPU_PAGE_MAGIC (0x9A0E06B9A0E)
  3290. #endif /* _LINUX_MM_H */