memory.c 63 KB

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