memory.c 65 KB

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