memory.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436
  1. /*
  2. * linux/mm/memory.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. */
  6. /*
  7. * demand-loading started 01.12.91 - seems it is high on the list of
  8. * things wanted, and it should be easy to implement. - Linus
  9. */
  10. /*
  11. * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
  12. * pages started 02.12.91, seems to work. - Linus.
  13. *
  14. * Tested sharing by executing about 30 /bin/sh: under the old kernel it
  15. * would have taken more than the 6M I have free, but it worked well as
  16. * far as I could see.
  17. *
  18. * Also corrected some "invalidate()"s - I wasn't doing enough of them.
  19. */
  20. /*
  21. * Real VM (paging to/from disk) started 18.12.91. Much more work and
  22. * thought has to go into this. Oh, well..
  23. * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
  24. * Found it. Everything seems to work now.
  25. * 20.12.91 - Ok, making the swap-device changeable like the root.
  26. */
  27. /*
  28. * 05.04.94 - Multi-page memory management added for v1.1.
  29. * Idea by Alex Bligh (alex@cconcepts.co.uk)
  30. *
  31. * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
  32. * (Gerhard.Wichert@pdb.siemens.de)
  33. *
  34. * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
  35. */
  36. #include <linux/kernel_stat.h>
  37. #include <linux/mm.h>
  38. #include <linux/hugetlb.h>
  39. #include <linux/mman.h>
  40. #include <linux/swap.h>
  41. #include <linux/highmem.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/rmap.h>
  44. #include <linux/module.h>
  45. #include <linux/init.h>
  46. #include <asm/pgalloc.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/tlb.h>
  49. #include <asm/tlbflush.h>
  50. #include <asm/pgtable.h>
  51. #include <linux/swapops.h>
  52. #include <linux/elf.h>
  53. #ifndef CONFIG_NEED_MULTIPLE_NODES
  54. /* use the per-pgdat data instead for discontigmem - mbligh */
  55. unsigned long max_mapnr;
  56. struct page *mem_map;
  57. EXPORT_SYMBOL(max_mapnr);
  58. EXPORT_SYMBOL(mem_map);
  59. #endif
  60. unsigned long num_physpages;
  61. /*
  62. * A number of key systems in x86 including ioremap() rely on the assumption
  63. * that high_memory defines the upper bound on direct map memory, then end
  64. * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
  65. * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
  66. * and ZONE_HIGHMEM.
  67. */
  68. void * high_memory;
  69. unsigned long vmalloc_earlyreserve;
  70. EXPORT_SYMBOL(num_physpages);
  71. EXPORT_SYMBOL(high_memory);
  72. EXPORT_SYMBOL(vmalloc_earlyreserve);
  73. /*
  74. * If a p?d_bad entry is found while walking page tables, report
  75. * the error, before resetting entry to p?d_none. Usually (but
  76. * very seldom) called out from the p?d_none_or_clear_bad macros.
  77. */
  78. void pgd_clear_bad(pgd_t *pgd)
  79. {
  80. pgd_ERROR(*pgd);
  81. pgd_clear(pgd);
  82. }
  83. void pud_clear_bad(pud_t *pud)
  84. {
  85. pud_ERROR(*pud);
  86. pud_clear(pud);
  87. }
  88. void pmd_clear_bad(pmd_t *pmd)
  89. {
  90. pmd_ERROR(*pmd);
  91. pmd_clear(pmd);
  92. }
  93. /*
  94. * Note: this doesn't free the actual pages themselves. That
  95. * has been handled earlier when unmapping all the memory regions.
  96. */
  97. static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
  98. {
  99. struct page *page = pmd_page(*pmd);
  100. pmd_clear(pmd);
  101. pte_lock_deinit(page);
  102. pte_free_tlb(tlb, page);
  103. dec_page_state(nr_page_table_pages);
  104. tlb->mm->nr_ptes--;
  105. }
  106. static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  107. unsigned long addr, unsigned long end,
  108. unsigned long floor, unsigned long ceiling)
  109. {
  110. pmd_t *pmd;
  111. unsigned long next;
  112. unsigned long start;
  113. start = addr;
  114. pmd = pmd_offset(pud, addr);
  115. do {
  116. next = pmd_addr_end(addr, end);
  117. if (pmd_none_or_clear_bad(pmd))
  118. continue;
  119. free_pte_range(tlb, pmd);
  120. } while (pmd++, addr = next, addr != end);
  121. start &= PUD_MASK;
  122. if (start < floor)
  123. return;
  124. if (ceiling) {
  125. ceiling &= PUD_MASK;
  126. if (!ceiling)
  127. return;
  128. }
  129. if (end - 1 > ceiling - 1)
  130. return;
  131. pmd = pmd_offset(pud, start);
  132. pud_clear(pud);
  133. pmd_free_tlb(tlb, pmd);
  134. }
  135. static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  136. unsigned long addr, unsigned long end,
  137. unsigned long floor, unsigned long ceiling)
  138. {
  139. pud_t *pud;
  140. unsigned long next;
  141. unsigned long start;
  142. start = addr;
  143. pud = pud_offset(pgd, addr);
  144. do {
  145. next = pud_addr_end(addr, end);
  146. if (pud_none_or_clear_bad(pud))
  147. continue;
  148. free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  149. } while (pud++, addr = next, addr != end);
  150. start &= PGDIR_MASK;
  151. if (start < floor)
  152. return;
  153. if (ceiling) {
  154. ceiling &= PGDIR_MASK;
  155. if (!ceiling)
  156. return;
  157. }
  158. if (end - 1 > ceiling - 1)
  159. return;
  160. pud = pud_offset(pgd, start);
  161. pgd_clear(pgd);
  162. pud_free_tlb(tlb, pud);
  163. }
  164. /*
  165. * This function frees user-level page tables of a process.
  166. *
  167. * Must be called with pagetable lock held.
  168. */
  169. void free_pgd_range(struct mmu_gather **tlb,
  170. unsigned long addr, unsigned long end,
  171. unsigned long floor, unsigned long ceiling)
  172. {
  173. pgd_t *pgd;
  174. unsigned long next;
  175. unsigned long start;
  176. /*
  177. * The next few lines have given us lots of grief...
  178. *
  179. * Why are we testing PMD* at this top level? Because often
  180. * there will be no work to do at all, and we'd prefer not to
  181. * go all the way down to the bottom just to discover that.
  182. *
  183. * Why all these "- 1"s? Because 0 represents both the bottom
  184. * of the address space and the top of it (using -1 for the
  185. * top wouldn't help much: the masks would do the wrong thing).
  186. * The rule is that addr 0 and floor 0 refer to the bottom of
  187. * the address space, but end 0 and ceiling 0 refer to the top
  188. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  189. * that end 0 case should be mythical).
  190. *
  191. * Wherever addr is brought up or ceiling brought down, we must
  192. * be careful to reject "the opposite 0" before it confuses the
  193. * subsequent tests. But what about where end is brought down
  194. * by PMD_SIZE below? no, end can't go down to 0 there.
  195. *
  196. * Whereas we round start (addr) and ceiling down, by different
  197. * masks at different levels, in order to test whether a table
  198. * now has no other vmas using it, so can be freed, we don't
  199. * bother to round floor or end up - the tests don't need that.
  200. */
  201. addr &= PMD_MASK;
  202. if (addr < floor) {
  203. addr += PMD_SIZE;
  204. if (!addr)
  205. return;
  206. }
  207. if (ceiling) {
  208. ceiling &= PMD_MASK;
  209. if (!ceiling)
  210. return;
  211. }
  212. if (end - 1 > ceiling - 1)
  213. end -= PMD_SIZE;
  214. if (addr > end - 1)
  215. return;
  216. start = addr;
  217. pgd = pgd_offset((*tlb)->mm, addr);
  218. do {
  219. next = pgd_addr_end(addr, end);
  220. if (pgd_none_or_clear_bad(pgd))
  221. continue;
  222. free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
  223. } while (pgd++, addr = next, addr != end);
  224. if (!(*tlb)->fullmm)
  225. flush_tlb_pgtables((*tlb)->mm, start, end);
  226. }
  227. void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
  228. unsigned long floor, unsigned long ceiling)
  229. {
  230. while (vma) {
  231. struct vm_area_struct *next = vma->vm_next;
  232. unsigned long addr = vma->vm_start;
  233. /*
  234. * Hide vma from rmap and vmtruncate before freeing pgtables
  235. */
  236. anon_vma_unlink(vma);
  237. unlink_file_vma(vma);
  238. if (is_hugepage_only_range(vma->vm_mm, addr, HPAGE_SIZE)) {
  239. hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
  240. floor, next? next->vm_start: ceiling);
  241. } else {
  242. /*
  243. * Optimization: gather nearby vmas into one call down
  244. */
  245. while (next && next->vm_start <= vma->vm_end + PMD_SIZE
  246. && !is_hugepage_only_range(vma->vm_mm, next->vm_start,
  247. HPAGE_SIZE)) {
  248. vma = next;
  249. next = vma->vm_next;
  250. anon_vma_unlink(vma);
  251. unlink_file_vma(vma);
  252. }
  253. free_pgd_range(tlb, addr, vma->vm_end,
  254. floor, next? next->vm_start: ceiling);
  255. }
  256. vma = next;
  257. }
  258. }
  259. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
  260. {
  261. struct page *new = pte_alloc_one(mm, address);
  262. if (!new)
  263. return -ENOMEM;
  264. pte_lock_init(new);
  265. spin_lock(&mm->page_table_lock);
  266. if (pmd_present(*pmd)) { /* Another has populated it */
  267. pte_lock_deinit(new);
  268. pte_free(new);
  269. } else {
  270. mm->nr_ptes++;
  271. inc_page_state(nr_page_table_pages);
  272. pmd_populate(mm, pmd, new);
  273. }
  274. spin_unlock(&mm->page_table_lock);
  275. return 0;
  276. }
  277. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
  278. {
  279. pte_t *new = pte_alloc_one_kernel(&init_mm, address);
  280. if (!new)
  281. return -ENOMEM;
  282. spin_lock(&init_mm.page_table_lock);
  283. if (pmd_present(*pmd)) /* Another has populated it */
  284. pte_free_kernel(new);
  285. else
  286. pmd_populate_kernel(&init_mm, pmd, new);
  287. spin_unlock(&init_mm.page_table_lock);
  288. return 0;
  289. }
  290. static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
  291. {
  292. if (file_rss)
  293. add_mm_counter(mm, file_rss, file_rss);
  294. if (anon_rss)
  295. add_mm_counter(mm, anon_rss, anon_rss);
  296. }
  297. /*
  298. * This function is called to print an error when a bad pte
  299. * is found. For example, we might have a PFN-mapped pte in
  300. * a region that doesn't allow it.
  301. *
  302. * The calling function must still handle the error.
  303. */
  304. void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
  305. {
  306. printk(KERN_ERR "Bad pte = %08llx, process = %s, "
  307. "vm_flags = %lx, vaddr = %lx\n",
  308. (long long)pte_val(pte),
  309. (vma->vm_mm == current->mm ? current->comm : "???"),
  310. vma->vm_flags, vaddr);
  311. dump_stack();
  312. }
  313. static inline int is_cow_mapping(unsigned int flags)
  314. {
  315. return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  316. }
  317. /*
  318. * This function gets the "struct page" associated with a pte.
  319. *
  320. * NOTE! Some mappings do not have "struct pages". A raw PFN mapping
  321. * will have each page table entry just pointing to a raw page frame
  322. * number, and as far as the VM layer is concerned, those do not have
  323. * pages associated with them - even if the PFN might point to memory
  324. * that otherwise is perfectly fine and has a "struct page".
  325. *
  326. * The way we recognize those mappings is through the rules set up
  327. * by "remap_pfn_range()": the vma will have the VM_PFNMAP bit set,
  328. * and the vm_pgoff will point to the first PFN mapped: thus every
  329. * page that is a raw mapping will always honor the rule
  330. *
  331. * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
  332. *
  333. * and if that isn't true, the page has been COW'ed (in which case it
  334. * _does_ have a "struct page" associated with it even if it is in a
  335. * VM_PFNMAP range).
  336. */
  337. struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
  338. {
  339. unsigned long pfn = pte_pfn(pte);
  340. if (vma->vm_flags & VM_PFNMAP) {
  341. unsigned long off = (addr - vma->vm_start) >> PAGE_SHIFT;
  342. if (pfn == vma->vm_pgoff + off)
  343. return NULL;
  344. if (!is_cow_mapping(vma->vm_flags))
  345. return NULL;
  346. }
  347. /*
  348. * Add some anal sanity checks for now. Eventually,
  349. * we should just do "return pfn_to_page(pfn)", but
  350. * in the meantime we check that we get a valid pfn,
  351. * and that the resulting page looks ok.
  352. *
  353. * Remove this test eventually!
  354. */
  355. if (unlikely(!pfn_valid(pfn))) {
  356. print_bad_pte(vma, pte, addr);
  357. return NULL;
  358. }
  359. /*
  360. * NOTE! We still have PageReserved() pages in the page
  361. * tables.
  362. *
  363. * The PAGE_ZERO() pages and various VDSO mappings can
  364. * cause them to exist.
  365. */
  366. return pfn_to_page(pfn);
  367. }
  368. /*
  369. * copy one vm_area from one task to the other. Assumes the page tables
  370. * already present in the new task to be cleared in the whole range
  371. * covered by this vma.
  372. */
  373. static inline void
  374. copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  375. pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
  376. unsigned long addr, int *rss)
  377. {
  378. unsigned long vm_flags = vma->vm_flags;
  379. pte_t pte = *src_pte;
  380. struct page *page;
  381. /* pte contains position in swap or file, so copy. */
  382. if (unlikely(!pte_present(pte))) {
  383. if (!pte_file(pte)) {
  384. swap_duplicate(pte_to_swp_entry(pte));
  385. /* make sure dst_mm is on swapoff's mmlist. */
  386. if (unlikely(list_empty(&dst_mm->mmlist))) {
  387. spin_lock(&mmlist_lock);
  388. if (list_empty(&dst_mm->mmlist))
  389. list_add(&dst_mm->mmlist,
  390. &src_mm->mmlist);
  391. spin_unlock(&mmlist_lock);
  392. }
  393. }
  394. goto out_set_pte;
  395. }
  396. /*
  397. * If it's a COW mapping, write protect it both
  398. * in the parent and the child
  399. */
  400. if (is_cow_mapping(vm_flags)) {
  401. ptep_set_wrprotect(src_mm, addr, src_pte);
  402. pte = *src_pte;
  403. }
  404. /*
  405. * If it's a shared mapping, mark it clean in
  406. * the child
  407. */
  408. if (vm_flags & VM_SHARED)
  409. pte = pte_mkclean(pte);
  410. pte = pte_mkold(pte);
  411. page = vm_normal_page(vma, addr, pte);
  412. if (page) {
  413. get_page(page);
  414. page_dup_rmap(page);
  415. rss[!!PageAnon(page)]++;
  416. }
  417. out_set_pte:
  418. set_pte_at(dst_mm, addr, dst_pte, pte);
  419. }
  420. static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  421. pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
  422. unsigned long addr, unsigned long end)
  423. {
  424. pte_t *src_pte, *dst_pte;
  425. spinlock_t *src_ptl, *dst_ptl;
  426. int progress = 0;
  427. int rss[2];
  428. again:
  429. rss[1] = rss[0] = 0;
  430. dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
  431. if (!dst_pte)
  432. return -ENOMEM;
  433. src_pte = pte_offset_map_nested(src_pmd, addr);
  434. src_ptl = pte_lockptr(src_mm, src_pmd);
  435. spin_lock(src_ptl);
  436. do {
  437. /*
  438. * We are holding two locks at this point - either of them
  439. * could generate latencies in another task on another CPU.
  440. */
  441. if (progress >= 32) {
  442. progress = 0;
  443. if (need_resched() ||
  444. need_lockbreak(src_ptl) ||
  445. need_lockbreak(dst_ptl))
  446. break;
  447. }
  448. if (pte_none(*src_pte)) {
  449. progress++;
  450. continue;
  451. }
  452. copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
  453. progress += 8;
  454. } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
  455. spin_unlock(src_ptl);
  456. pte_unmap_nested(src_pte - 1);
  457. add_mm_rss(dst_mm, rss[0], rss[1]);
  458. pte_unmap_unlock(dst_pte - 1, dst_ptl);
  459. cond_resched();
  460. if (addr != end)
  461. goto again;
  462. return 0;
  463. }
  464. static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  465. pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
  466. unsigned long addr, unsigned long end)
  467. {
  468. pmd_t *src_pmd, *dst_pmd;
  469. unsigned long next;
  470. dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
  471. if (!dst_pmd)
  472. return -ENOMEM;
  473. src_pmd = pmd_offset(src_pud, addr);
  474. do {
  475. next = pmd_addr_end(addr, end);
  476. if (pmd_none_or_clear_bad(src_pmd))
  477. continue;
  478. if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
  479. vma, addr, next))
  480. return -ENOMEM;
  481. } while (dst_pmd++, src_pmd++, addr = next, addr != end);
  482. return 0;
  483. }
  484. static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  485. pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
  486. unsigned long addr, unsigned long end)
  487. {
  488. pud_t *src_pud, *dst_pud;
  489. unsigned long next;
  490. dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
  491. if (!dst_pud)
  492. return -ENOMEM;
  493. src_pud = pud_offset(src_pgd, addr);
  494. do {
  495. next = pud_addr_end(addr, end);
  496. if (pud_none_or_clear_bad(src_pud))
  497. continue;
  498. if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
  499. vma, addr, next))
  500. return -ENOMEM;
  501. } while (dst_pud++, src_pud++, addr = next, addr != end);
  502. return 0;
  503. }
  504. int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  505. struct vm_area_struct *vma)
  506. {
  507. pgd_t *src_pgd, *dst_pgd;
  508. unsigned long next;
  509. unsigned long addr = vma->vm_start;
  510. unsigned long end = vma->vm_end;
  511. /*
  512. * Don't copy ptes where a page fault will fill them correctly.
  513. * Fork becomes much lighter when there are big shared or private
  514. * readonly mappings. The tradeoff is that copy_page_range is more
  515. * efficient than faulting.
  516. */
  517. if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
  518. if (!vma->anon_vma)
  519. return 0;
  520. }
  521. if (is_vm_hugetlb_page(vma))
  522. return copy_hugetlb_page_range(dst_mm, src_mm, vma);
  523. dst_pgd = pgd_offset(dst_mm, addr);
  524. src_pgd = pgd_offset(src_mm, addr);
  525. do {
  526. next = pgd_addr_end(addr, end);
  527. if (pgd_none_or_clear_bad(src_pgd))
  528. continue;
  529. if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
  530. vma, addr, next))
  531. return -ENOMEM;
  532. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  533. return 0;
  534. }
  535. static unsigned long zap_pte_range(struct mmu_gather *tlb,
  536. struct vm_area_struct *vma, pmd_t *pmd,
  537. unsigned long addr, unsigned long end,
  538. long *zap_work, struct zap_details *details)
  539. {
  540. struct mm_struct *mm = tlb->mm;
  541. pte_t *pte;
  542. spinlock_t *ptl;
  543. int file_rss = 0;
  544. int anon_rss = 0;
  545. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  546. do {
  547. pte_t ptent = *pte;
  548. if (pte_none(ptent)) {
  549. (*zap_work)--;
  550. continue;
  551. }
  552. if (pte_present(ptent)) {
  553. struct page *page;
  554. (*zap_work) -= PAGE_SIZE;
  555. page = vm_normal_page(vma, addr, ptent);
  556. if (unlikely(details) && page) {
  557. /*
  558. * unmap_shared_mapping_pages() wants to
  559. * invalidate cache without truncating:
  560. * unmap shared but keep private pages.
  561. */
  562. if (details->check_mapping &&
  563. details->check_mapping != page->mapping)
  564. continue;
  565. /*
  566. * Each page->index must be checked when
  567. * invalidating or truncating nonlinear.
  568. */
  569. if (details->nonlinear_vma &&
  570. (page->index < details->first_index ||
  571. page->index > details->last_index))
  572. continue;
  573. }
  574. ptent = ptep_get_and_clear_full(mm, addr, pte,
  575. tlb->fullmm);
  576. tlb_remove_tlb_entry(tlb, pte, addr);
  577. if (unlikely(!page))
  578. continue;
  579. if (unlikely(details) && details->nonlinear_vma
  580. && linear_page_index(details->nonlinear_vma,
  581. addr) != page->index)
  582. set_pte_at(mm, addr, pte,
  583. pgoff_to_pte(page->index));
  584. if (PageAnon(page))
  585. anon_rss--;
  586. else {
  587. if (pte_dirty(ptent))
  588. set_page_dirty(page);
  589. if (pte_young(ptent))
  590. mark_page_accessed(page);
  591. file_rss--;
  592. }
  593. page_remove_rmap(page);
  594. tlb_remove_page(tlb, page);
  595. continue;
  596. }
  597. /*
  598. * If details->check_mapping, we leave swap entries;
  599. * if details->nonlinear_vma, we leave file entries.
  600. */
  601. if (unlikely(details))
  602. continue;
  603. if (!pte_file(ptent))
  604. free_swap_and_cache(pte_to_swp_entry(ptent));
  605. pte_clear_full(mm, addr, pte, tlb->fullmm);
  606. } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
  607. add_mm_rss(mm, file_rss, anon_rss);
  608. pte_unmap_unlock(pte - 1, ptl);
  609. return addr;
  610. }
  611. static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
  612. struct vm_area_struct *vma, pud_t *pud,
  613. unsigned long addr, unsigned long end,
  614. long *zap_work, struct zap_details *details)
  615. {
  616. pmd_t *pmd;
  617. unsigned long next;
  618. pmd = pmd_offset(pud, addr);
  619. do {
  620. next = pmd_addr_end(addr, end);
  621. if (pmd_none_or_clear_bad(pmd)) {
  622. (*zap_work)--;
  623. continue;
  624. }
  625. next = zap_pte_range(tlb, vma, pmd, addr, next,
  626. zap_work, details);
  627. } while (pmd++, addr = next, (addr != end && *zap_work > 0));
  628. return addr;
  629. }
  630. static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
  631. struct vm_area_struct *vma, pgd_t *pgd,
  632. unsigned long addr, unsigned long end,
  633. long *zap_work, struct zap_details *details)
  634. {
  635. pud_t *pud;
  636. unsigned long next;
  637. pud = pud_offset(pgd, addr);
  638. do {
  639. next = pud_addr_end(addr, end);
  640. if (pud_none_or_clear_bad(pud)) {
  641. (*zap_work)--;
  642. continue;
  643. }
  644. next = zap_pmd_range(tlb, vma, pud, addr, next,
  645. zap_work, details);
  646. } while (pud++, addr = next, (addr != end && *zap_work > 0));
  647. return addr;
  648. }
  649. static unsigned long unmap_page_range(struct mmu_gather *tlb,
  650. struct vm_area_struct *vma,
  651. unsigned long addr, unsigned long end,
  652. long *zap_work, struct zap_details *details)
  653. {
  654. pgd_t *pgd;
  655. unsigned long next;
  656. if (details && !details->check_mapping && !details->nonlinear_vma)
  657. details = NULL;
  658. BUG_ON(addr >= end);
  659. tlb_start_vma(tlb, vma);
  660. pgd = pgd_offset(vma->vm_mm, addr);
  661. do {
  662. next = pgd_addr_end(addr, end);
  663. if (pgd_none_or_clear_bad(pgd)) {
  664. (*zap_work)--;
  665. continue;
  666. }
  667. next = zap_pud_range(tlb, vma, pgd, addr, next,
  668. zap_work, details);
  669. } while (pgd++, addr = next, (addr != end && *zap_work > 0));
  670. tlb_end_vma(tlb, vma);
  671. return addr;
  672. }
  673. #ifdef CONFIG_PREEMPT
  674. # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
  675. #else
  676. /* No preempt: go for improved straight-line efficiency */
  677. # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
  678. #endif
  679. /**
  680. * unmap_vmas - unmap a range of memory covered by a list of vma's
  681. * @tlbp: address of the caller's struct mmu_gather
  682. * @vma: the starting vma
  683. * @start_addr: virtual address at which to start unmapping
  684. * @end_addr: virtual address at which to end unmapping
  685. * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
  686. * @details: details of nonlinear truncation or shared cache invalidation
  687. *
  688. * Returns the end address of the unmapping (restart addr if interrupted).
  689. *
  690. * Unmap all pages in the vma list.
  691. *
  692. * We aim to not hold locks for too long (for scheduling latency reasons).
  693. * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
  694. * return the ending mmu_gather to the caller.
  695. *
  696. * Only addresses between `start' and `end' will be unmapped.
  697. *
  698. * The VMA list must be sorted in ascending virtual address order.
  699. *
  700. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  701. * range after unmap_vmas() returns. So the only responsibility here is to
  702. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  703. * drops the lock and schedules.
  704. */
  705. unsigned long unmap_vmas(struct mmu_gather **tlbp,
  706. struct vm_area_struct *vma, unsigned long start_addr,
  707. unsigned long end_addr, unsigned long *nr_accounted,
  708. struct zap_details *details)
  709. {
  710. long zap_work = ZAP_BLOCK_SIZE;
  711. unsigned long tlb_start = 0; /* For tlb_finish_mmu */
  712. int tlb_start_valid = 0;
  713. unsigned long start = start_addr;
  714. spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
  715. int fullmm = (*tlbp)->fullmm;
  716. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
  717. unsigned long end;
  718. start = max(vma->vm_start, start_addr);
  719. if (start >= vma->vm_end)
  720. continue;
  721. end = min(vma->vm_end, end_addr);
  722. if (end <= vma->vm_start)
  723. continue;
  724. if (vma->vm_flags & VM_ACCOUNT)
  725. *nr_accounted += (end - start) >> PAGE_SHIFT;
  726. while (start != end) {
  727. if (!tlb_start_valid) {
  728. tlb_start = start;
  729. tlb_start_valid = 1;
  730. }
  731. if (unlikely(is_vm_hugetlb_page(vma))) {
  732. unmap_hugepage_range(vma, start, end);
  733. zap_work -= (end - start) /
  734. (HPAGE_SIZE / PAGE_SIZE);
  735. start = end;
  736. } else
  737. start = unmap_page_range(*tlbp, vma,
  738. start, end, &zap_work, details);
  739. if (zap_work > 0) {
  740. BUG_ON(start != end);
  741. break;
  742. }
  743. tlb_finish_mmu(*tlbp, tlb_start, start);
  744. if (need_resched() ||
  745. (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
  746. if (i_mmap_lock) {
  747. *tlbp = NULL;
  748. goto out;
  749. }
  750. cond_resched();
  751. }
  752. *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
  753. tlb_start_valid = 0;
  754. zap_work = ZAP_BLOCK_SIZE;
  755. }
  756. }
  757. out:
  758. return start; /* which is now the end (or restart) address */
  759. }
  760. /**
  761. * zap_page_range - remove user pages in a given range
  762. * @vma: vm_area_struct holding the applicable pages
  763. * @address: starting address of pages to zap
  764. * @size: number of bytes to zap
  765. * @details: details of nonlinear truncation or shared cache invalidation
  766. */
  767. unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
  768. unsigned long size, struct zap_details *details)
  769. {
  770. struct mm_struct *mm = vma->vm_mm;
  771. struct mmu_gather *tlb;
  772. unsigned long end = address + size;
  773. unsigned long nr_accounted = 0;
  774. lru_add_drain();
  775. tlb = tlb_gather_mmu(mm, 0);
  776. update_hiwater_rss(mm);
  777. end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
  778. if (tlb)
  779. tlb_finish_mmu(tlb, address, end);
  780. return end;
  781. }
  782. /*
  783. * Do a quick page-table lookup for a single page.
  784. */
  785. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  786. unsigned int flags)
  787. {
  788. pgd_t *pgd;
  789. pud_t *pud;
  790. pmd_t *pmd;
  791. pte_t *ptep, pte;
  792. spinlock_t *ptl;
  793. struct page *page;
  794. struct mm_struct *mm = vma->vm_mm;
  795. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  796. if (!IS_ERR(page)) {
  797. BUG_ON(flags & FOLL_GET);
  798. goto out;
  799. }
  800. page = NULL;
  801. pgd = pgd_offset(mm, address);
  802. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  803. goto no_page_table;
  804. pud = pud_offset(pgd, address);
  805. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  806. goto no_page_table;
  807. pmd = pmd_offset(pud, address);
  808. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  809. goto no_page_table;
  810. if (pmd_huge(*pmd)) {
  811. BUG_ON(flags & FOLL_GET);
  812. page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
  813. goto out;
  814. }
  815. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  816. if (!ptep)
  817. goto out;
  818. pte = *ptep;
  819. if (!pte_present(pte))
  820. goto unlock;
  821. if ((flags & FOLL_WRITE) && !pte_write(pte))
  822. goto unlock;
  823. page = vm_normal_page(vma, address, pte);
  824. if (unlikely(!page))
  825. goto unlock;
  826. if (flags & FOLL_GET)
  827. get_page(page);
  828. if (flags & FOLL_TOUCH) {
  829. if ((flags & FOLL_WRITE) &&
  830. !pte_dirty(pte) && !PageDirty(page))
  831. set_page_dirty(page);
  832. mark_page_accessed(page);
  833. }
  834. unlock:
  835. pte_unmap_unlock(ptep, ptl);
  836. out:
  837. return page;
  838. no_page_table:
  839. /*
  840. * When core dumping an enormous anonymous area that nobody
  841. * has touched so far, we don't want to allocate page tables.
  842. */
  843. if (flags & FOLL_ANON) {
  844. page = ZERO_PAGE(address);
  845. if (flags & FOLL_GET)
  846. get_page(page);
  847. BUG_ON(flags & FOLL_WRITE);
  848. }
  849. return page;
  850. }
  851. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  852. unsigned long start, int len, int write, int force,
  853. struct page **pages, struct vm_area_struct **vmas)
  854. {
  855. int i;
  856. unsigned int vm_flags;
  857. /*
  858. * Require read or write permissions.
  859. * If 'force' is set, we only require the "MAY" flags.
  860. */
  861. vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  862. vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  863. i = 0;
  864. do {
  865. struct vm_area_struct *vma;
  866. unsigned int foll_flags;
  867. vma = find_extend_vma(mm, start);
  868. if (!vma && in_gate_area(tsk, start)) {
  869. unsigned long pg = start & PAGE_MASK;
  870. struct vm_area_struct *gate_vma = get_gate_vma(tsk);
  871. pgd_t *pgd;
  872. pud_t *pud;
  873. pmd_t *pmd;
  874. pte_t *pte;
  875. if (write) /* user gate pages are read-only */
  876. return i ? : -EFAULT;
  877. if (pg > TASK_SIZE)
  878. pgd = pgd_offset_k(pg);
  879. else
  880. pgd = pgd_offset_gate(mm, pg);
  881. BUG_ON(pgd_none(*pgd));
  882. pud = pud_offset(pgd, pg);
  883. BUG_ON(pud_none(*pud));
  884. pmd = pmd_offset(pud, pg);
  885. if (pmd_none(*pmd))
  886. return i ? : -EFAULT;
  887. pte = pte_offset_map(pmd, pg);
  888. if (pte_none(*pte)) {
  889. pte_unmap(pte);
  890. return i ? : -EFAULT;
  891. }
  892. if (pages) {
  893. struct page *page = vm_normal_page(gate_vma, start, *pte);
  894. pages[i] = page;
  895. if (page)
  896. get_page(page);
  897. }
  898. pte_unmap(pte);
  899. if (vmas)
  900. vmas[i] = gate_vma;
  901. i++;
  902. start += PAGE_SIZE;
  903. len--;
  904. continue;
  905. }
  906. if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP))
  907. || !(vm_flags & vma->vm_flags))
  908. return i ? : -EFAULT;
  909. if (is_vm_hugetlb_page(vma)) {
  910. i = follow_hugetlb_page(mm, vma, pages, vmas,
  911. &start, &len, i);
  912. continue;
  913. }
  914. foll_flags = FOLL_TOUCH;
  915. if (pages)
  916. foll_flags |= FOLL_GET;
  917. if (!write && !(vma->vm_flags & VM_LOCKED) &&
  918. (!vma->vm_ops || !vma->vm_ops->nopage))
  919. foll_flags |= FOLL_ANON;
  920. do {
  921. struct page *page;
  922. if (write)
  923. foll_flags |= FOLL_WRITE;
  924. cond_resched();
  925. while (!(page = follow_page(vma, start, foll_flags))) {
  926. int ret;
  927. ret = __handle_mm_fault(mm, vma, start,
  928. foll_flags & FOLL_WRITE);
  929. /*
  930. * The VM_FAULT_WRITE bit tells us that do_wp_page has
  931. * broken COW when necessary, even if maybe_mkwrite
  932. * decided not to set pte_write. We can thus safely do
  933. * subsequent page lookups as if they were reads.
  934. */
  935. if (ret & VM_FAULT_WRITE)
  936. foll_flags &= ~FOLL_WRITE;
  937. switch (ret & ~VM_FAULT_WRITE) {
  938. case VM_FAULT_MINOR:
  939. tsk->min_flt++;
  940. break;
  941. case VM_FAULT_MAJOR:
  942. tsk->maj_flt++;
  943. break;
  944. case VM_FAULT_SIGBUS:
  945. return i ? i : -EFAULT;
  946. case VM_FAULT_OOM:
  947. return i ? i : -ENOMEM;
  948. default:
  949. BUG();
  950. }
  951. }
  952. if (pages) {
  953. pages[i] = page;
  954. flush_dcache_page(page);
  955. }
  956. if (vmas)
  957. vmas[i] = vma;
  958. i++;
  959. start += PAGE_SIZE;
  960. len--;
  961. } while (len && start < vma->vm_end);
  962. } while (len);
  963. return i;
  964. }
  965. EXPORT_SYMBOL(get_user_pages);
  966. static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  967. unsigned long addr, unsigned long end, pgprot_t prot)
  968. {
  969. pte_t *pte;
  970. spinlock_t *ptl;
  971. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  972. if (!pte)
  973. return -ENOMEM;
  974. do {
  975. struct page *page = ZERO_PAGE(addr);
  976. pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
  977. page_cache_get(page);
  978. page_add_file_rmap(page);
  979. inc_mm_counter(mm, file_rss);
  980. BUG_ON(!pte_none(*pte));
  981. set_pte_at(mm, addr, pte, zero_pte);
  982. } while (pte++, addr += PAGE_SIZE, addr != end);
  983. pte_unmap_unlock(pte - 1, ptl);
  984. return 0;
  985. }
  986. static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
  987. unsigned long addr, unsigned long end, pgprot_t prot)
  988. {
  989. pmd_t *pmd;
  990. unsigned long next;
  991. pmd = pmd_alloc(mm, pud, addr);
  992. if (!pmd)
  993. return -ENOMEM;
  994. do {
  995. next = pmd_addr_end(addr, end);
  996. if (zeromap_pte_range(mm, pmd, addr, next, prot))
  997. return -ENOMEM;
  998. } while (pmd++, addr = next, addr != end);
  999. return 0;
  1000. }
  1001. static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1002. unsigned long addr, unsigned long end, pgprot_t prot)
  1003. {
  1004. pud_t *pud;
  1005. unsigned long next;
  1006. pud = pud_alloc(mm, pgd, addr);
  1007. if (!pud)
  1008. return -ENOMEM;
  1009. do {
  1010. next = pud_addr_end(addr, end);
  1011. if (zeromap_pmd_range(mm, pud, addr, next, prot))
  1012. return -ENOMEM;
  1013. } while (pud++, addr = next, addr != end);
  1014. return 0;
  1015. }
  1016. int zeromap_page_range(struct vm_area_struct *vma,
  1017. unsigned long addr, unsigned long size, pgprot_t prot)
  1018. {
  1019. pgd_t *pgd;
  1020. unsigned long next;
  1021. unsigned long end = addr + size;
  1022. struct mm_struct *mm = vma->vm_mm;
  1023. int err;
  1024. BUG_ON(addr >= end);
  1025. pgd = pgd_offset(mm, addr);
  1026. flush_cache_range(vma, addr, end);
  1027. do {
  1028. next = pgd_addr_end(addr, end);
  1029. err = zeromap_pud_range(mm, pgd, addr, next, prot);
  1030. if (err)
  1031. break;
  1032. } while (pgd++, addr = next, addr != end);
  1033. return err;
  1034. }
  1035. pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl)
  1036. {
  1037. pgd_t * pgd = pgd_offset(mm, addr);
  1038. pud_t * pud = pud_alloc(mm, pgd, addr);
  1039. if (pud) {
  1040. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  1041. if (pmd)
  1042. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  1043. }
  1044. return NULL;
  1045. }
  1046. /*
  1047. * This is the old fallback for page remapping.
  1048. *
  1049. * For historical reasons, it only allows reserved pages. Only
  1050. * old drivers should use this, and they needed to mark their
  1051. * pages reserved for the old functions anyway.
  1052. */
  1053. static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot)
  1054. {
  1055. int retval;
  1056. pte_t *pte;
  1057. spinlock_t *ptl;
  1058. retval = -EINVAL;
  1059. if (PageAnon(page))
  1060. goto out;
  1061. retval = -ENOMEM;
  1062. flush_dcache_page(page);
  1063. pte = get_locked_pte(mm, addr, &ptl);
  1064. if (!pte)
  1065. goto out;
  1066. retval = -EBUSY;
  1067. if (!pte_none(*pte))
  1068. goto out_unlock;
  1069. /* Ok, finally just insert the thing.. */
  1070. get_page(page);
  1071. inc_mm_counter(mm, file_rss);
  1072. page_add_file_rmap(page);
  1073. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1074. retval = 0;
  1075. out_unlock:
  1076. pte_unmap_unlock(pte, ptl);
  1077. out:
  1078. return retval;
  1079. }
  1080. /*
  1081. * This allows drivers to insert individual pages they've allocated
  1082. * into a user vma.
  1083. *
  1084. * The page has to be a nice clean _individual_ kernel allocation.
  1085. * If you allocate a compound page, you need to have marked it as
  1086. * such (__GFP_COMP), or manually just split the page up yourself
  1087. * (which is mainly an issue of doing "set_page_count(page, 1)" for
  1088. * each sub-page, and then freeing them one by one when you free
  1089. * them rather than freeing it as a compound page).
  1090. *
  1091. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1092. * took an arbitrary page protection parameter. This doesn't allow
  1093. * that. Your vma protection will have to be set up correctly, which
  1094. * means that if you want a shared writable mapping, you'd better
  1095. * ask for a shared writable mapping!
  1096. *
  1097. * The page does not need to be reserved.
  1098. */
  1099. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page)
  1100. {
  1101. if (addr < vma->vm_start || addr >= vma->vm_end)
  1102. return -EFAULT;
  1103. if (!page_count(page))
  1104. return -EINVAL;
  1105. vma->vm_flags |= VM_INSERTPAGE;
  1106. return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot);
  1107. }
  1108. EXPORT_SYMBOL(vm_insert_page);
  1109. /*
  1110. * maps a range of physical memory into the requested pages. the old
  1111. * mappings are removed. any references to nonexistent pages results
  1112. * in null mappings (currently treated as "copy-on-access")
  1113. */
  1114. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1115. unsigned long addr, unsigned long end,
  1116. unsigned long pfn, pgprot_t prot)
  1117. {
  1118. pte_t *pte;
  1119. spinlock_t *ptl;
  1120. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1121. if (!pte)
  1122. return -ENOMEM;
  1123. do {
  1124. BUG_ON(!pte_none(*pte));
  1125. set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
  1126. pfn++;
  1127. } while (pte++, addr += PAGE_SIZE, addr != end);
  1128. pte_unmap_unlock(pte - 1, ptl);
  1129. return 0;
  1130. }
  1131. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1132. unsigned long addr, unsigned long end,
  1133. unsigned long pfn, pgprot_t prot)
  1134. {
  1135. pmd_t *pmd;
  1136. unsigned long next;
  1137. pfn -= addr >> PAGE_SHIFT;
  1138. pmd = pmd_alloc(mm, pud, addr);
  1139. if (!pmd)
  1140. return -ENOMEM;
  1141. do {
  1142. next = pmd_addr_end(addr, end);
  1143. if (remap_pte_range(mm, pmd, addr, next,
  1144. pfn + (addr >> PAGE_SHIFT), prot))
  1145. return -ENOMEM;
  1146. } while (pmd++, addr = next, addr != end);
  1147. return 0;
  1148. }
  1149. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1150. unsigned long addr, unsigned long end,
  1151. unsigned long pfn, pgprot_t prot)
  1152. {
  1153. pud_t *pud;
  1154. unsigned long next;
  1155. pfn -= addr >> PAGE_SHIFT;
  1156. pud = pud_alloc(mm, pgd, addr);
  1157. if (!pud)
  1158. return -ENOMEM;
  1159. do {
  1160. next = pud_addr_end(addr, end);
  1161. if (remap_pmd_range(mm, pud, addr, next,
  1162. pfn + (addr >> PAGE_SHIFT), prot))
  1163. return -ENOMEM;
  1164. } while (pud++, addr = next, addr != end);
  1165. return 0;
  1166. }
  1167. /* Note: this is only safe if the mm semaphore is held when called. */
  1168. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1169. unsigned long pfn, unsigned long size, pgprot_t prot)
  1170. {
  1171. pgd_t *pgd;
  1172. unsigned long next;
  1173. unsigned long end = addr + PAGE_ALIGN(size);
  1174. struct mm_struct *mm = vma->vm_mm;
  1175. int err;
  1176. /*
  1177. * Physically remapped pages are special. Tell the
  1178. * rest of the world about it:
  1179. * VM_IO tells people not to look at these pages
  1180. * (accesses can have side effects).
  1181. * VM_RESERVED is specified all over the place, because
  1182. * in 2.4 it kept swapout's vma scan off this vma; but
  1183. * in 2.6 the LRU scan won't even find its pages, so this
  1184. * flag means no more than count its pages in reserved_vm,
  1185. * and omit it from core dump, even when VM_IO turned off.
  1186. * VM_PFNMAP tells the core MM that the base pages are just
  1187. * raw PFN mappings, and do not have a "struct page" associated
  1188. * with them.
  1189. *
  1190. * There's a horrible special case to handle copy-on-write
  1191. * behaviour that some programs depend on. We mark the "original"
  1192. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  1193. */
  1194. if (is_cow_mapping(vma->vm_flags)) {
  1195. if (addr != vma->vm_start || end != vma->vm_end)
  1196. return -EINVAL;
  1197. vma->vm_pgoff = pfn;
  1198. }
  1199. vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
  1200. BUG_ON(addr >= end);
  1201. pfn -= addr >> PAGE_SHIFT;
  1202. pgd = pgd_offset(mm, addr);
  1203. flush_cache_range(vma, addr, end);
  1204. do {
  1205. next = pgd_addr_end(addr, end);
  1206. err = remap_pud_range(mm, pgd, addr, next,
  1207. pfn + (addr >> PAGE_SHIFT), prot);
  1208. if (err)
  1209. break;
  1210. } while (pgd++, addr = next, addr != end);
  1211. return err;
  1212. }
  1213. EXPORT_SYMBOL(remap_pfn_range);
  1214. /*
  1215. * handle_pte_fault chooses page fault handler according to an entry
  1216. * which was read non-atomically. Before making any commitment, on
  1217. * those architectures or configurations (e.g. i386 with PAE) which
  1218. * might give a mix of unmatched parts, do_swap_page and do_file_page
  1219. * must check under lock before unmapping the pte and proceeding
  1220. * (but do_wp_page is only called after already making such a check;
  1221. * and do_anonymous_page and do_no_page can safely check later on).
  1222. */
  1223. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  1224. pte_t *page_table, pte_t orig_pte)
  1225. {
  1226. int same = 1;
  1227. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1228. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1229. spinlock_t *ptl = pte_lockptr(mm, pmd);
  1230. spin_lock(ptl);
  1231. same = pte_same(*page_table, orig_pte);
  1232. spin_unlock(ptl);
  1233. }
  1234. #endif
  1235. pte_unmap(page_table);
  1236. return same;
  1237. }
  1238. /*
  1239. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  1240. * servicing faults for write access. In the normal case, do always want
  1241. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  1242. * that do not have writing enabled, when used by access_process_vm.
  1243. */
  1244. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  1245. {
  1246. if (likely(vma->vm_flags & VM_WRITE))
  1247. pte = pte_mkwrite(pte);
  1248. return pte;
  1249. }
  1250. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va)
  1251. {
  1252. /*
  1253. * If the source page was a PFN mapping, we don't have
  1254. * a "struct page" for it. We do a best-effort copy by
  1255. * just copying from the original user address. If that
  1256. * fails, we just zero-fill it. Live with it.
  1257. */
  1258. if (unlikely(!src)) {
  1259. void *kaddr = kmap_atomic(dst, KM_USER0);
  1260. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  1261. /*
  1262. * This really shouldn't fail, because the page is there
  1263. * in the page tables. But it might just be unreadable,
  1264. * in which case we just give up and fill the result with
  1265. * zeroes.
  1266. */
  1267. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  1268. memset(kaddr, 0, PAGE_SIZE);
  1269. kunmap_atomic(kaddr, KM_USER0);
  1270. return;
  1271. }
  1272. copy_user_highpage(dst, src, va);
  1273. }
  1274. /*
  1275. * This routine handles present pages, when users try to write
  1276. * to a shared page. It is done by copying the page to a new address
  1277. * and decrementing the shared-page counter for the old page.
  1278. *
  1279. * Note that this routine assumes that the protection checks have been
  1280. * done by the caller (the low-level page fault routine in most cases).
  1281. * Thus we can safely just mark it writable once we've done any necessary
  1282. * COW.
  1283. *
  1284. * We also mark the page dirty at this point even though the page will
  1285. * change only once the write actually happens. This avoids a few races,
  1286. * and potentially makes it more efficient.
  1287. *
  1288. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1289. * but allow concurrent faults), with pte both mapped and locked.
  1290. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1291. */
  1292. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1293. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1294. spinlock_t *ptl, pte_t orig_pte)
  1295. {
  1296. struct page *old_page, *new_page;
  1297. pte_t entry;
  1298. int ret = VM_FAULT_MINOR;
  1299. old_page = vm_normal_page(vma, address, orig_pte);
  1300. if (!old_page)
  1301. goto gotten;
  1302. if (PageAnon(old_page) && !TestSetPageLocked(old_page)) {
  1303. int reuse = can_share_swap_page(old_page);
  1304. unlock_page(old_page);
  1305. if (reuse) {
  1306. flush_cache_page(vma, address, pte_pfn(orig_pte));
  1307. entry = pte_mkyoung(orig_pte);
  1308. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1309. ptep_set_access_flags(vma, address, page_table, entry, 1);
  1310. update_mmu_cache(vma, address, entry);
  1311. lazy_mmu_prot_update(entry);
  1312. ret |= VM_FAULT_WRITE;
  1313. goto unlock;
  1314. }
  1315. }
  1316. /*
  1317. * Ok, we need to copy. Oh, well..
  1318. */
  1319. page_cache_get(old_page);
  1320. gotten:
  1321. pte_unmap_unlock(page_table, ptl);
  1322. if (unlikely(anon_vma_prepare(vma)))
  1323. goto oom;
  1324. if (old_page == ZERO_PAGE(address)) {
  1325. new_page = alloc_zeroed_user_highpage(vma, address);
  1326. if (!new_page)
  1327. goto oom;
  1328. } else {
  1329. new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
  1330. if (!new_page)
  1331. goto oom;
  1332. cow_user_page(new_page, old_page, address);
  1333. }
  1334. /*
  1335. * Re-check the pte - we dropped the lock
  1336. */
  1337. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1338. if (likely(pte_same(*page_table, orig_pte))) {
  1339. if (old_page) {
  1340. page_remove_rmap(old_page);
  1341. if (!PageAnon(old_page)) {
  1342. dec_mm_counter(mm, file_rss);
  1343. inc_mm_counter(mm, anon_rss);
  1344. }
  1345. } else
  1346. inc_mm_counter(mm, anon_rss);
  1347. flush_cache_page(vma, address, pte_pfn(orig_pte));
  1348. entry = mk_pte(new_page, vma->vm_page_prot);
  1349. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1350. ptep_establish(vma, address, page_table, entry);
  1351. update_mmu_cache(vma, address, entry);
  1352. lazy_mmu_prot_update(entry);
  1353. lru_cache_add_active(new_page);
  1354. page_add_new_anon_rmap(new_page, vma, address);
  1355. /* Free the old page.. */
  1356. new_page = old_page;
  1357. ret |= VM_FAULT_WRITE;
  1358. }
  1359. if (new_page)
  1360. page_cache_release(new_page);
  1361. if (old_page)
  1362. page_cache_release(old_page);
  1363. unlock:
  1364. pte_unmap_unlock(page_table, ptl);
  1365. return ret;
  1366. oom:
  1367. if (old_page)
  1368. page_cache_release(old_page);
  1369. return VM_FAULT_OOM;
  1370. }
  1371. /*
  1372. * Helper functions for unmap_mapping_range().
  1373. *
  1374. * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
  1375. *
  1376. * We have to restart searching the prio_tree whenever we drop the lock,
  1377. * since the iterator is only valid while the lock is held, and anyway
  1378. * a later vma might be split and reinserted earlier while lock dropped.
  1379. *
  1380. * The list of nonlinear vmas could be handled more efficiently, using
  1381. * a placeholder, but handle it in the same way until a need is shown.
  1382. * It is important to search the prio_tree before nonlinear list: a vma
  1383. * may become nonlinear and be shifted from prio_tree to nonlinear list
  1384. * while the lock is dropped; but never shifted from list to prio_tree.
  1385. *
  1386. * In order to make forward progress despite restarting the search,
  1387. * vm_truncate_count is used to mark a vma as now dealt with, so we can
  1388. * quickly skip it next time around. Since the prio_tree search only
  1389. * shows us those vmas affected by unmapping the range in question, we
  1390. * can't efficiently keep all vmas in step with mapping->truncate_count:
  1391. * so instead reset them all whenever it wraps back to 0 (then go to 1).
  1392. * mapping->truncate_count and vma->vm_truncate_count are protected by
  1393. * i_mmap_lock.
  1394. *
  1395. * In order to make forward progress despite repeatedly restarting some
  1396. * large vma, note the restart_addr from unmap_vmas when it breaks out:
  1397. * and restart from that address when we reach that vma again. It might
  1398. * have been split or merged, shrunk or extended, but never shifted: so
  1399. * restart_addr remains valid so long as it remains in the vma's range.
  1400. * unmap_mapping_range forces truncate_count to leap over page-aligned
  1401. * values so we can save vma's restart_addr in its truncate_count field.
  1402. */
  1403. #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
  1404. static void reset_vma_truncate_counts(struct address_space *mapping)
  1405. {
  1406. struct vm_area_struct *vma;
  1407. struct prio_tree_iter iter;
  1408. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
  1409. vma->vm_truncate_count = 0;
  1410. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1411. vma->vm_truncate_count = 0;
  1412. }
  1413. static int unmap_mapping_range_vma(struct vm_area_struct *vma,
  1414. unsigned long start_addr, unsigned long end_addr,
  1415. struct zap_details *details)
  1416. {
  1417. unsigned long restart_addr;
  1418. int need_break;
  1419. again:
  1420. restart_addr = vma->vm_truncate_count;
  1421. if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
  1422. start_addr = restart_addr;
  1423. if (start_addr >= end_addr) {
  1424. /* Top of vma has been split off since last time */
  1425. vma->vm_truncate_count = details->truncate_count;
  1426. return 0;
  1427. }
  1428. }
  1429. restart_addr = zap_page_range(vma, start_addr,
  1430. end_addr - start_addr, details);
  1431. need_break = need_resched() ||
  1432. need_lockbreak(details->i_mmap_lock);
  1433. if (restart_addr >= end_addr) {
  1434. /* We have now completed this vma: mark it so */
  1435. vma->vm_truncate_count = details->truncate_count;
  1436. if (!need_break)
  1437. return 0;
  1438. } else {
  1439. /* Note restart_addr in vma's truncate_count field */
  1440. vma->vm_truncate_count = restart_addr;
  1441. if (!need_break)
  1442. goto again;
  1443. }
  1444. spin_unlock(details->i_mmap_lock);
  1445. cond_resched();
  1446. spin_lock(details->i_mmap_lock);
  1447. return -EINTR;
  1448. }
  1449. static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
  1450. struct zap_details *details)
  1451. {
  1452. struct vm_area_struct *vma;
  1453. struct prio_tree_iter iter;
  1454. pgoff_t vba, vea, zba, zea;
  1455. restart:
  1456. vma_prio_tree_foreach(vma, &iter, root,
  1457. details->first_index, details->last_index) {
  1458. /* Skip quickly over those we have already dealt with */
  1459. if (vma->vm_truncate_count == details->truncate_count)
  1460. continue;
  1461. vba = vma->vm_pgoff;
  1462. vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
  1463. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  1464. zba = details->first_index;
  1465. if (zba < vba)
  1466. zba = vba;
  1467. zea = details->last_index;
  1468. if (zea > vea)
  1469. zea = vea;
  1470. if (unmap_mapping_range_vma(vma,
  1471. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  1472. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  1473. details) < 0)
  1474. goto restart;
  1475. }
  1476. }
  1477. static inline void unmap_mapping_range_list(struct list_head *head,
  1478. struct zap_details *details)
  1479. {
  1480. struct vm_area_struct *vma;
  1481. /*
  1482. * In nonlinear VMAs there is no correspondence between virtual address
  1483. * offset and file offset. So we must perform an exhaustive search
  1484. * across *all* the pages in each nonlinear VMA, not just the pages
  1485. * whose virtual address lies outside the file truncation point.
  1486. */
  1487. restart:
  1488. list_for_each_entry(vma, head, shared.vm_set.list) {
  1489. /* Skip quickly over those we have already dealt with */
  1490. if (vma->vm_truncate_count == details->truncate_count)
  1491. continue;
  1492. details->nonlinear_vma = vma;
  1493. if (unmap_mapping_range_vma(vma, vma->vm_start,
  1494. vma->vm_end, details) < 0)
  1495. goto restart;
  1496. }
  1497. }
  1498. /**
  1499. * unmap_mapping_range - unmap the portion of all mmaps
  1500. * in the specified address_space corresponding to the specified
  1501. * page range in the underlying file.
  1502. * @mapping: the address space containing mmaps to be unmapped.
  1503. * @holebegin: byte in first page to unmap, relative to the start of
  1504. * the underlying file. This will be rounded down to a PAGE_SIZE
  1505. * boundary. Note that this is different from vmtruncate(), which
  1506. * must keep the partial page. In contrast, we must get rid of
  1507. * partial pages.
  1508. * @holelen: size of prospective hole in bytes. This will be rounded
  1509. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  1510. * end of the file.
  1511. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  1512. * but 0 when invalidating pagecache, don't throw away private data.
  1513. */
  1514. void unmap_mapping_range(struct address_space *mapping,
  1515. loff_t const holebegin, loff_t const holelen, int even_cows)
  1516. {
  1517. struct zap_details details;
  1518. pgoff_t hba = holebegin >> PAGE_SHIFT;
  1519. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1520. /* Check for overflow. */
  1521. if (sizeof(holelen) > sizeof(hlen)) {
  1522. long long holeend =
  1523. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1524. if (holeend & ~(long long)ULONG_MAX)
  1525. hlen = ULONG_MAX - hba + 1;
  1526. }
  1527. details.check_mapping = even_cows? NULL: mapping;
  1528. details.nonlinear_vma = NULL;
  1529. details.first_index = hba;
  1530. details.last_index = hba + hlen - 1;
  1531. if (details.last_index < details.first_index)
  1532. details.last_index = ULONG_MAX;
  1533. details.i_mmap_lock = &mapping->i_mmap_lock;
  1534. spin_lock(&mapping->i_mmap_lock);
  1535. /* serialize i_size write against truncate_count write */
  1536. smp_wmb();
  1537. /* Protect against page faults, and endless unmapping loops */
  1538. mapping->truncate_count++;
  1539. /*
  1540. * For archs where spin_lock has inclusive semantics like ia64
  1541. * this smp_mb() will prevent to read pagetable contents
  1542. * before the truncate_count increment is visible to
  1543. * other cpus.
  1544. */
  1545. smp_mb();
  1546. if (unlikely(is_restart_addr(mapping->truncate_count))) {
  1547. if (mapping->truncate_count == 0)
  1548. reset_vma_truncate_counts(mapping);
  1549. mapping->truncate_count++;
  1550. }
  1551. details.truncate_count = mapping->truncate_count;
  1552. if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
  1553. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  1554. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  1555. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  1556. spin_unlock(&mapping->i_mmap_lock);
  1557. }
  1558. EXPORT_SYMBOL(unmap_mapping_range);
  1559. /*
  1560. * Handle all mappings that got truncated by a "truncate()"
  1561. * system call.
  1562. *
  1563. * NOTE! We have to be ready to update the memory sharing
  1564. * between the file and the memory map for a potential last
  1565. * incomplete page. Ugly, but necessary.
  1566. */
  1567. int vmtruncate(struct inode * inode, loff_t offset)
  1568. {
  1569. struct address_space *mapping = inode->i_mapping;
  1570. unsigned long limit;
  1571. if (inode->i_size < offset)
  1572. goto do_expand;
  1573. /*
  1574. * truncation of in-use swapfiles is disallowed - it would cause
  1575. * subsequent swapout to scribble on the now-freed blocks.
  1576. */
  1577. if (IS_SWAPFILE(inode))
  1578. goto out_busy;
  1579. i_size_write(inode, offset);
  1580. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1581. truncate_inode_pages(mapping, offset);
  1582. goto out_truncate;
  1583. do_expand:
  1584. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1585. if (limit != RLIM_INFINITY && offset > limit)
  1586. goto out_sig;
  1587. if (offset > inode->i_sb->s_maxbytes)
  1588. goto out_big;
  1589. i_size_write(inode, offset);
  1590. out_truncate:
  1591. if (inode->i_op && inode->i_op->truncate)
  1592. inode->i_op->truncate(inode);
  1593. return 0;
  1594. out_sig:
  1595. send_sig(SIGXFSZ, current, 0);
  1596. out_big:
  1597. return -EFBIG;
  1598. out_busy:
  1599. return -ETXTBSY;
  1600. }
  1601. EXPORT_SYMBOL(vmtruncate);
  1602. int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
  1603. {
  1604. struct address_space *mapping = inode->i_mapping;
  1605. /*
  1606. * If the underlying filesystem is not going to provide
  1607. * a way to truncate a range of blocks (punch a hole) -
  1608. * we should return failure right now.
  1609. */
  1610. if (!inode->i_op || !inode->i_op->truncate_range)
  1611. return -ENOSYS;
  1612. mutex_lock(&inode->i_mutex);
  1613. down_write(&inode->i_alloc_sem);
  1614. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1615. truncate_inode_pages_range(mapping, offset, end);
  1616. inode->i_op->truncate_range(inode, offset, end);
  1617. up_write(&inode->i_alloc_sem);
  1618. mutex_unlock(&inode->i_mutex);
  1619. return 0;
  1620. }
  1621. EXPORT_SYMBOL(vmtruncate_range);
  1622. /*
  1623. * Primitive swap readahead code. We simply read an aligned block of
  1624. * (1 << page_cluster) entries in the swap area. This method is chosen
  1625. * because it doesn't cost us any seek time. We also make sure to queue
  1626. * the 'original' request together with the readahead ones...
  1627. *
  1628. * This has been extended to use the NUMA policies from the mm triggering
  1629. * the readahead.
  1630. *
  1631. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  1632. */
  1633. void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
  1634. {
  1635. #ifdef CONFIG_NUMA
  1636. struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
  1637. #endif
  1638. int i, num;
  1639. struct page *new_page;
  1640. unsigned long offset;
  1641. /*
  1642. * Get the number of handles we should do readahead io to.
  1643. */
  1644. num = valid_swaphandles(entry, &offset);
  1645. for (i = 0; i < num; offset++, i++) {
  1646. /* Ok, do the async read-ahead now */
  1647. new_page = read_swap_cache_async(swp_entry(swp_type(entry),
  1648. offset), vma, addr);
  1649. if (!new_page)
  1650. break;
  1651. page_cache_release(new_page);
  1652. #ifdef CONFIG_NUMA
  1653. /*
  1654. * Find the next applicable VMA for the NUMA policy.
  1655. */
  1656. addr += PAGE_SIZE;
  1657. if (addr == 0)
  1658. vma = NULL;
  1659. if (vma) {
  1660. if (addr >= vma->vm_end) {
  1661. vma = next_vma;
  1662. next_vma = vma ? vma->vm_next : NULL;
  1663. }
  1664. if (vma && addr < vma->vm_start)
  1665. vma = NULL;
  1666. } else {
  1667. if (next_vma && addr >= next_vma->vm_start) {
  1668. vma = next_vma;
  1669. next_vma = vma->vm_next;
  1670. }
  1671. }
  1672. #endif
  1673. }
  1674. lru_add_drain(); /* Push any new pages onto the LRU now */
  1675. }
  1676. /*
  1677. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1678. * but allow concurrent faults), and pte mapped but not yet locked.
  1679. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1680. */
  1681. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1682. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1683. int write_access, pte_t orig_pte)
  1684. {
  1685. spinlock_t *ptl;
  1686. struct page *page;
  1687. swp_entry_t entry;
  1688. pte_t pte;
  1689. int ret = VM_FAULT_MINOR;
  1690. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  1691. goto out;
  1692. entry = pte_to_swp_entry(orig_pte);
  1693. again:
  1694. page = lookup_swap_cache(entry);
  1695. if (!page) {
  1696. swapin_readahead(entry, address, vma);
  1697. page = read_swap_cache_async(entry, vma, address);
  1698. if (!page) {
  1699. /*
  1700. * Back out if somebody else faulted in this pte
  1701. * while we released the pte lock.
  1702. */
  1703. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1704. if (likely(pte_same(*page_table, orig_pte)))
  1705. ret = VM_FAULT_OOM;
  1706. goto unlock;
  1707. }
  1708. /* Had to read the page from swap area: Major fault */
  1709. ret = VM_FAULT_MAJOR;
  1710. inc_page_state(pgmajfault);
  1711. grab_swap_token();
  1712. }
  1713. mark_page_accessed(page);
  1714. lock_page(page);
  1715. if (!PageSwapCache(page)) {
  1716. /* Page migration has occured */
  1717. unlock_page(page);
  1718. page_cache_release(page);
  1719. goto again;
  1720. }
  1721. /*
  1722. * Back out if somebody else already faulted in this pte.
  1723. */
  1724. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1725. if (unlikely(!pte_same(*page_table, orig_pte)))
  1726. goto out_nomap;
  1727. if (unlikely(!PageUptodate(page))) {
  1728. ret = VM_FAULT_SIGBUS;
  1729. goto out_nomap;
  1730. }
  1731. /* The page isn't present yet, go ahead with the fault. */
  1732. inc_mm_counter(mm, anon_rss);
  1733. pte = mk_pte(page, vma->vm_page_prot);
  1734. if (write_access && can_share_swap_page(page)) {
  1735. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  1736. write_access = 0;
  1737. }
  1738. flush_icache_page(vma, page);
  1739. set_pte_at(mm, address, page_table, pte);
  1740. page_add_anon_rmap(page, vma, address);
  1741. swap_free(entry);
  1742. if (vm_swap_full())
  1743. remove_exclusive_swap_page(page);
  1744. unlock_page(page);
  1745. if (write_access) {
  1746. if (do_wp_page(mm, vma, address,
  1747. page_table, pmd, ptl, pte) == VM_FAULT_OOM)
  1748. ret = VM_FAULT_OOM;
  1749. goto out;
  1750. }
  1751. /* No need to invalidate - it was non-present before */
  1752. update_mmu_cache(vma, address, pte);
  1753. lazy_mmu_prot_update(pte);
  1754. unlock:
  1755. pte_unmap_unlock(page_table, ptl);
  1756. out:
  1757. return ret;
  1758. out_nomap:
  1759. pte_unmap_unlock(page_table, ptl);
  1760. unlock_page(page);
  1761. page_cache_release(page);
  1762. return ret;
  1763. }
  1764. /*
  1765. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1766. * but allow concurrent faults), and pte mapped but not yet locked.
  1767. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1768. */
  1769. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1770. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1771. int write_access)
  1772. {
  1773. struct page *page;
  1774. spinlock_t *ptl;
  1775. pte_t entry;
  1776. if (write_access) {
  1777. /* Allocate our own private page. */
  1778. pte_unmap(page_table);
  1779. if (unlikely(anon_vma_prepare(vma)))
  1780. goto oom;
  1781. page = alloc_zeroed_user_highpage(vma, address);
  1782. if (!page)
  1783. goto oom;
  1784. entry = mk_pte(page, vma->vm_page_prot);
  1785. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1786. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1787. if (!pte_none(*page_table))
  1788. goto release;
  1789. inc_mm_counter(mm, anon_rss);
  1790. lru_cache_add_active(page);
  1791. page_add_new_anon_rmap(page, vma, address);
  1792. } else {
  1793. /* Map the ZERO_PAGE - vm_page_prot is readonly */
  1794. page = ZERO_PAGE(address);
  1795. page_cache_get(page);
  1796. entry = mk_pte(page, vma->vm_page_prot);
  1797. ptl = pte_lockptr(mm, pmd);
  1798. spin_lock(ptl);
  1799. if (!pte_none(*page_table))
  1800. goto release;
  1801. inc_mm_counter(mm, file_rss);
  1802. page_add_file_rmap(page);
  1803. }
  1804. set_pte_at(mm, address, page_table, entry);
  1805. /* No need to invalidate - it was non-present before */
  1806. update_mmu_cache(vma, address, entry);
  1807. lazy_mmu_prot_update(entry);
  1808. unlock:
  1809. pte_unmap_unlock(page_table, ptl);
  1810. return VM_FAULT_MINOR;
  1811. release:
  1812. page_cache_release(page);
  1813. goto unlock;
  1814. oom:
  1815. return VM_FAULT_OOM;
  1816. }
  1817. /*
  1818. * do_no_page() tries to create a new page mapping. It aggressively
  1819. * tries to share with existing pages, but makes a separate copy if
  1820. * the "write_access" parameter is true in order to avoid the next
  1821. * page fault.
  1822. *
  1823. * As this is called only for pages that do not currently exist, we
  1824. * do not need to flush old virtual caches or the TLB.
  1825. *
  1826. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1827. * but allow concurrent faults), and pte mapped but not yet locked.
  1828. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1829. */
  1830. static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1831. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1832. int write_access)
  1833. {
  1834. spinlock_t *ptl;
  1835. struct page *new_page;
  1836. struct address_space *mapping = NULL;
  1837. pte_t entry;
  1838. unsigned int sequence = 0;
  1839. int ret = VM_FAULT_MINOR;
  1840. int anon = 0;
  1841. pte_unmap(page_table);
  1842. BUG_ON(vma->vm_flags & VM_PFNMAP);
  1843. if (vma->vm_file) {
  1844. mapping = vma->vm_file->f_mapping;
  1845. sequence = mapping->truncate_count;
  1846. smp_rmb(); /* serializes i_size against truncate_count */
  1847. }
  1848. retry:
  1849. new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
  1850. /*
  1851. * No smp_rmb is needed here as long as there's a full
  1852. * spin_lock/unlock sequence inside the ->nopage callback
  1853. * (for the pagecache lookup) that acts as an implicit
  1854. * smp_mb() and prevents the i_size read to happen
  1855. * after the next truncate_count read.
  1856. */
  1857. /* no page was available -- either SIGBUS or OOM */
  1858. if (new_page == NOPAGE_SIGBUS)
  1859. return VM_FAULT_SIGBUS;
  1860. if (new_page == NOPAGE_OOM)
  1861. return VM_FAULT_OOM;
  1862. /*
  1863. * Should we do an early C-O-W break?
  1864. */
  1865. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  1866. struct page *page;
  1867. if (unlikely(anon_vma_prepare(vma)))
  1868. goto oom;
  1869. page = alloc_page_vma(GFP_HIGHUSER, vma, address);
  1870. if (!page)
  1871. goto oom;
  1872. copy_user_highpage(page, new_page, address);
  1873. page_cache_release(new_page);
  1874. new_page = page;
  1875. anon = 1;
  1876. }
  1877. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1878. /*
  1879. * For a file-backed vma, someone could have truncated or otherwise
  1880. * invalidated this page. If unmap_mapping_range got called,
  1881. * retry getting the page.
  1882. */
  1883. if (mapping && unlikely(sequence != mapping->truncate_count)) {
  1884. pte_unmap_unlock(page_table, ptl);
  1885. page_cache_release(new_page);
  1886. cond_resched();
  1887. sequence = mapping->truncate_count;
  1888. smp_rmb();
  1889. goto retry;
  1890. }
  1891. /*
  1892. * This silly early PAGE_DIRTY setting removes a race
  1893. * due to the bad i386 page protection. But it's valid
  1894. * for other architectures too.
  1895. *
  1896. * Note that if write_access is true, we either now have
  1897. * an exclusive copy of the page, or this is a shared mapping,
  1898. * so we can make it writable and dirty to avoid having to
  1899. * handle that later.
  1900. */
  1901. /* Only go through if we didn't race with anybody else... */
  1902. if (pte_none(*page_table)) {
  1903. flush_icache_page(vma, new_page);
  1904. entry = mk_pte(new_page, vma->vm_page_prot);
  1905. if (write_access)
  1906. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1907. set_pte_at(mm, address, page_table, entry);
  1908. if (anon) {
  1909. inc_mm_counter(mm, anon_rss);
  1910. lru_cache_add_active(new_page);
  1911. page_add_new_anon_rmap(new_page, vma, address);
  1912. } else {
  1913. inc_mm_counter(mm, file_rss);
  1914. page_add_file_rmap(new_page);
  1915. }
  1916. } else {
  1917. /* One of our sibling threads was faster, back out. */
  1918. page_cache_release(new_page);
  1919. goto unlock;
  1920. }
  1921. /* no need to invalidate: a not-present page shouldn't be cached */
  1922. update_mmu_cache(vma, address, entry);
  1923. lazy_mmu_prot_update(entry);
  1924. unlock:
  1925. pte_unmap_unlock(page_table, ptl);
  1926. return ret;
  1927. oom:
  1928. page_cache_release(new_page);
  1929. return VM_FAULT_OOM;
  1930. }
  1931. /*
  1932. * Fault of a previously existing named mapping. Repopulate the pte
  1933. * from the encoded file_pte if possible. This enables swappable
  1934. * nonlinear vmas.
  1935. *
  1936. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1937. * but allow concurrent faults), and pte mapped but not yet locked.
  1938. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1939. */
  1940. static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1941. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1942. int write_access, pte_t orig_pte)
  1943. {
  1944. pgoff_t pgoff;
  1945. int err;
  1946. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  1947. return VM_FAULT_MINOR;
  1948. if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
  1949. /*
  1950. * Page table corrupted: show pte and kill process.
  1951. */
  1952. print_bad_pte(vma, orig_pte, address);
  1953. return VM_FAULT_OOM;
  1954. }
  1955. /* We can then assume vm->vm_ops && vma->vm_ops->populate */
  1956. pgoff = pte_to_pgoff(orig_pte);
  1957. err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
  1958. vma->vm_page_prot, pgoff, 0);
  1959. if (err == -ENOMEM)
  1960. return VM_FAULT_OOM;
  1961. if (err)
  1962. return VM_FAULT_SIGBUS;
  1963. return VM_FAULT_MAJOR;
  1964. }
  1965. /*
  1966. * These routines also need to handle stuff like marking pages dirty
  1967. * and/or accessed for architectures that don't do it in hardware (most
  1968. * RISC architectures). The early dirtying is also good on the i386.
  1969. *
  1970. * There is also a hook called "update_mmu_cache()" that architectures
  1971. * with external mmu caches can use to update those (ie the Sparc or
  1972. * PowerPC hashed page tables that act as extended TLBs).
  1973. *
  1974. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1975. * but allow concurrent faults), and pte mapped but not yet locked.
  1976. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1977. */
  1978. static inline int handle_pte_fault(struct mm_struct *mm,
  1979. struct vm_area_struct *vma, unsigned long address,
  1980. pte_t *pte, pmd_t *pmd, int write_access)
  1981. {
  1982. pte_t entry;
  1983. pte_t old_entry;
  1984. spinlock_t *ptl;
  1985. old_entry = entry = *pte;
  1986. if (!pte_present(entry)) {
  1987. if (pte_none(entry)) {
  1988. if (!vma->vm_ops || !vma->vm_ops->nopage)
  1989. return do_anonymous_page(mm, vma, address,
  1990. pte, pmd, write_access);
  1991. return do_no_page(mm, vma, address,
  1992. pte, pmd, write_access);
  1993. }
  1994. if (pte_file(entry))
  1995. return do_file_page(mm, vma, address,
  1996. pte, pmd, write_access, entry);
  1997. return do_swap_page(mm, vma, address,
  1998. pte, pmd, write_access, entry);
  1999. }
  2000. ptl = pte_lockptr(mm, pmd);
  2001. spin_lock(ptl);
  2002. if (unlikely(!pte_same(*pte, entry)))
  2003. goto unlock;
  2004. if (write_access) {
  2005. if (!pte_write(entry))
  2006. return do_wp_page(mm, vma, address,
  2007. pte, pmd, ptl, entry);
  2008. entry = pte_mkdirty(entry);
  2009. }
  2010. entry = pte_mkyoung(entry);
  2011. if (!pte_same(old_entry, entry)) {
  2012. ptep_set_access_flags(vma, address, pte, entry, write_access);
  2013. update_mmu_cache(vma, address, entry);
  2014. lazy_mmu_prot_update(entry);
  2015. } else {
  2016. /*
  2017. * This is needed only for protection faults but the arch code
  2018. * is not yet telling us if this is a protection fault or not.
  2019. * This still avoids useless tlb flushes for .text page faults
  2020. * with threads.
  2021. */
  2022. if (write_access)
  2023. flush_tlb_page(vma, address);
  2024. }
  2025. unlock:
  2026. pte_unmap_unlock(pte, ptl);
  2027. return VM_FAULT_MINOR;
  2028. }
  2029. /*
  2030. * By the time we get here, we already hold the mm semaphore
  2031. */
  2032. int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2033. unsigned long address, int write_access)
  2034. {
  2035. pgd_t *pgd;
  2036. pud_t *pud;
  2037. pmd_t *pmd;
  2038. pte_t *pte;
  2039. __set_current_state(TASK_RUNNING);
  2040. inc_page_state(pgfault);
  2041. if (unlikely(is_vm_hugetlb_page(vma)))
  2042. return hugetlb_fault(mm, vma, address, write_access);
  2043. pgd = pgd_offset(mm, address);
  2044. pud = pud_alloc(mm, pgd, address);
  2045. if (!pud)
  2046. return VM_FAULT_OOM;
  2047. pmd = pmd_alloc(mm, pud, address);
  2048. if (!pmd)
  2049. return VM_FAULT_OOM;
  2050. pte = pte_alloc_map(mm, pmd, address);
  2051. if (!pte)
  2052. return VM_FAULT_OOM;
  2053. return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
  2054. }
  2055. EXPORT_SYMBOL_GPL(__handle_mm_fault);
  2056. #ifndef __PAGETABLE_PUD_FOLDED
  2057. /*
  2058. * Allocate page upper directory.
  2059. * We've already handled the fast-path in-line.
  2060. */
  2061. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  2062. {
  2063. pud_t *new = pud_alloc_one(mm, address);
  2064. if (!new)
  2065. return -ENOMEM;
  2066. spin_lock(&mm->page_table_lock);
  2067. if (pgd_present(*pgd)) /* Another has populated it */
  2068. pud_free(new);
  2069. else
  2070. pgd_populate(mm, pgd, new);
  2071. spin_unlock(&mm->page_table_lock);
  2072. return 0;
  2073. }
  2074. #else
  2075. /* Workaround for gcc 2.96 */
  2076. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  2077. {
  2078. return 0;
  2079. }
  2080. #endif /* __PAGETABLE_PUD_FOLDED */
  2081. #ifndef __PAGETABLE_PMD_FOLDED
  2082. /*
  2083. * Allocate page middle directory.
  2084. * We've already handled the fast-path in-line.
  2085. */
  2086. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  2087. {
  2088. pmd_t *new = pmd_alloc_one(mm, address);
  2089. if (!new)
  2090. return -ENOMEM;
  2091. spin_lock(&mm->page_table_lock);
  2092. #ifndef __ARCH_HAS_4LEVEL_HACK
  2093. if (pud_present(*pud)) /* Another has populated it */
  2094. pmd_free(new);
  2095. else
  2096. pud_populate(mm, pud, new);
  2097. #else
  2098. if (pgd_present(*pud)) /* Another has populated it */
  2099. pmd_free(new);
  2100. else
  2101. pgd_populate(mm, pud, new);
  2102. #endif /* __ARCH_HAS_4LEVEL_HACK */
  2103. spin_unlock(&mm->page_table_lock);
  2104. return 0;
  2105. }
  2106. #else
  2107. /* Workaround for gcc 2.96 */
  2108. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  2109. {
  2110. return 0;
  2111. }
  2112. #endif /* __PAGETABLE_PMD_FOLDED */
  2113. int make_pages_present(unsigned long addr, unsigned long end)
  2114. {
  2115. int ret, len, write;
  2116. struct vm_area_struct * vma;
  2117. vma = find_vma(current->mm, addr);
  2118. if (!vma)
  2119. return -1;
  2120. write = (vma->vm_flags & VM_WRITE) != 0;
  2121. if (addr >= end)
  2122. BUG();
  2123. if (end > vma->vm_end)
  2124. BUG();
  2125. len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
  2126. ret = get_user_pages(current, current->mm, addr,
  2127. len, write, 0, NULL, NULL);
  2128. if (ret < 0)
  2129. return ret;
  2130. return ret == len ? 0 : -1;
  2131. }
  2132. /*
  2133. * Map a vmalloc()-space virtual address to the physical page.
  2134. */
  2135. struct page * vmalloc_to_page(void * vmalloc_addr)
  2136. {
  2137. unsigned long addr = (unsigned long) vmalloc_addr;
  2138. struct page *page = NULL;
  2139. pgd_t *pgd = pgd_offset_k(addr);
  2140. pud_t *pud;
  2141. pmd_t *pmd;
  2142. pte_t *ptep, pte;
  2143. if (!pgd_none(*pgd)) {
  2144. pud = pud_offset(pgd, addr);
  2145. if (!pud_none(*pud)) {
  2146. pmd = pmd_offset(pud, addr);
  2147. if (!pmd_none(*pmd)) {
  2148. ptep = pte_offset_map(pmd, addr);
  2149. pte = *ptep;
  2150. if (pte_present(pte))
  2151. page = pte_page(pte);
  2152. pte_unmap(ptep);
  2153. }
  2154. }
  2155. }
  2156. return page;
  2157. }
  2158. EXPORT_SYMBOL(vmalloc_to_page);
  2159. /*
  2160. * Map a vmalloc()-space virtual address to the physical page frame number.
  2161. */
  2162. unsigned long vmalloc_to_pfn(void * vmalloc_addr)
  2163. {
  2164. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  2165. }
  2166. EXPORT_SYMBOL(vmalloc_to_pfn);
  2167. #if !defined(__HAVE_ARCH_GATE_AREA)
  2168. #if defined(AT_SYSINFO_EHDR)
  2169. static struct vm_area_struct gate_vma;
  2170. static int __init gate_vma_init(void)
  2171. {
  2172. gate_vma.vm_mm = NULL;
  2173. gate_vma.vm_start = FIXADDR_USER_START;
  2174. gate_vma.vm_end = FIXADDR_USER_END;
  2175. gate_vma.vm_page_prot = PAGE_READONLY;
  2176. gate_vma.vm_flags = 0;
  2177. return 0;
  2178. }
  2179. __initcall(gate_vma_init);
  2180. #endif
  2181. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  2182. {
  2183. #ifdef AT_SYSINFO_EHDR
  2184. return &gate_vma;
  2185. #else
  2186. return NULL;
  2187. #endif
  2188. }
  2189. int in_gate_area_no_task(unsigned long addr)
  2190. {
  2191. #ifdef AT_SYSINFO_EHDR
  2192. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  2193. return 1;
  2194. #endif
  2195. return 0;
  2196. }
  2197. #endif /* __HAVE_ARCH_GATE_AREA */