memory.c 65 KB

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