nommu.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  1. /*
  2. * linux/mm/nommu.c
  3. *
  4. * Replacement code for mm functions to support CPU's that don't
  5. * have any form of memory management unit (thus no virtual memory).
  6. *
  7. * See Documentation/nommu-mmap.txt
  8. *
  9. * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
  10. * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
  11. * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
  12. * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
  13. * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/swap.h>
  19. #include <linux/file.h>
  20. #include <linux/highmem.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/tracehook.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/mount.h>
  28. #include <linux/personality.h>
  29. #include <linux/security.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/audit.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/tlb.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/mmu_context.h>
  36. #include "internal.h"
  37. #if 0
  38. #define kenter(FMT, ...) \
  39. printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  40. #define kleave(FMT, ...) \
  41. printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  42. #define kdebug(FMT, ...) \
  43. printk(KERN_DEBUG "xxx" FMT"yyy\n", ##__VA_ARGS__)
  44. #else
  45. #define kenter(FMT, ...) \
  46. no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
  47. #define kleave(FMT, ...) \
  48. no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
  49. #define kdebug(FMT, ...) \
  50. no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
  51. #endif
  52. void *high_memory;
  53. struct page *mem_map;
  54. unsigned long max_mapnr;
  55. unsigned long num_physpages;
  56. unsigned long highest_memmap_pfn;
  57. struct percpu_counter vm_committed_as;
  58. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  59. int sysctl_overcommit_ratio = 50; /* default is 50% */
  60. int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
  61. int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
  62. int heap_stack_gap = 0;
  63. atomic_long_t mmap_pages_allocated;
  64. EXPORT_SYMBOL(mem_map);
  65. EXPORT_SYMBOL(num_physpages);
  66. /* list of mapped, potentially shareable regions */
  67. static struct kmem_cache *vm_region_jar;
  68. struct rb_root nommu_region_tree = RB_ROOT;
  69. DECLARE_RWSEM(nommu_region_sem);
  70. const struct vm_operations_struct generic_file_vm_ops = {
  71. };
  72. /*
  73. * Return the total memory allocated for this pointer, not
  74. * just what the caller asked for.
  75. *
  76. * Doesn't have to be accurate, i.e. may have races.
  77. */
  78. unsigned int kobjsize(const void *objp)
  79. {
  80. struct page *page;
  81. /*
  82. * If the object we have should not have ksize performed on it,
  83. * return size of 0
  84. */
  85. if (!objp || !virt_addr_valid(objp))
  86. return 0;
  87. page = virt_to_head_page(objp);
  88. /*
  89. * If the allocator sets PageSlab, we know the pointer came from
  90. * kmalloc().
  91. */
  92. if (PageSlab(page))
  93. return ksize(objp);
  94. /*
  95. * If it's not a compound page, see if we have a matching VMA
  96. * region. This test is intentionally done in reverse order,
  97. * so if there's no VMA, we still fall through and hand back
  98. * PAGE_SIZE for 0-order pages.
  99. */
  100. if (!PageCompound(page)) {
  101. struct vm_area_struct *vma;
  102. vma = find_vma(current->mm, (unsigned long)objp);
  103. if (vma)
  104. return vma->vm_end - vma->vm_start;
  105. }
  106. /*
  107. * The ksize() function is only guaranteed to work for pointers
  108. * returned by kmalloc(). So handle arbitrary pointers here.
  109. */
  110. return PAGE_SIZE << compound_order(page);
  111. }
  112. int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  113. unsigned long start, int nr_pages, unsigned int foll_flags,
  114. struct page **pages, struct vm_area_struct **vmas)
  115. {
  116. struct vm_area_struct *vma;
  117. unsigned long vm_flags;
  118. int i;
  119. /* calculate required read or write permissions.
  120. * If FOLL_FORCE is set, we only require the "MAY" flags.
  121. */
  122. vm_flags = (foll_flags & FOLL_WRITE) ?
  123. (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  124. vm_flags &= (foll_flags & FOLL_FORCE) ?
  125. (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  126. for (i = 0; i < nr_pages; i++) {
  127. vma = find_vma(mm, start);
  128. if (!vma)
  129. goto finish_or_fault;
  130. /* protect what we can, including chardevs */
  131. if ((vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
  132. !(vm_flags & vma->vm_flags))
  133. goto finish_or_fault;
  134. if (pages) {
  135. pages[i] = virt_to_page(start);
  136. if (pages[i])
  137. page_cache_get(pages[i]);
  138. }
  139. if (vmas)
  140. vmas[i] = vma;
  141. start = (start + PAGE_SIZE) & PAGE_MASK;
  142. }
  143. return i;
  144. finish_or_fault:
  145. return i ? : -EFAULT;
  146. }
  147. /*
  148. * get a list of pages in an address range belonging to the specified process
  149. * and indicate the VMA that covers each page
  150. * - this is potentially dodgy as we may end incrementing the page count of a
  151. * slab page or a secondary page from a compound page
  152. * - don't permit access to VMAs that don't support it, such as I/O mappings
  153. */
  154. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  155. unsigned long start, int nr_pages, int write, int force,
  156. struct page **pages, struct vm_area_struct **vmas)
  157. {
  158. int flags = 0;
  159. if (write)
  160. flags |= FOLL_WRITE;
  161. if (force)
  162. flags |= FOLL_FORCE;
  163. return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas);
  164. }
  165. EXPORT_SYMBOL(get_user_pages);
  166. /**
  167. * follow_pfn - look up PFN at a user virtual address
  168. * @vma: memory mapping
  169. * @address: user virtual address
  170. * @pfn: location to store found PFN
  171. *
  172. * Only IO mappings and raw PFN mappings are allowed.
  173. *
  174. * Returns zero and the pfn at @pfn on success, -ve otherwise.
  175. */
  176. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  177. unsigned long *pfn)
  178. {
  179. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  180. return -EINVAL;
  181. *pfn = address >> PAGE_SHIFT;
  182. return 0;
  183. }
  184. EXPORT_SYMBOL(follow_pfn);
  185. DEFINE_RWLOCK(vmlist_lock);
  186. struct vm_struct *vmlist;
  187. void vfree(const void *addr)
  188. {
  189. kfree(addr);
  190. }
  191. EXPORT_SYMBOL(vfree);
  192. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  193. {
  194. /*
  195. * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
  196. * returns only a logical address.
  197. */
  198. return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
  199. }
  200. EXPORT_SYMBOL(__vmalloc);
  201. void *vmalloc_user(unsigned long size)
  202. {
  203. void *ret;
  204. ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  205. PAGE_KERNEL);
  206. if (ret) {
  207. struct vm_area_struct *vma;
  208. down_write(&current->mm->mmap_sem);
  209. vma = find_vma(current->mm, (unsigned long)ret);
  210. if (vma)
  211. vma->vm_flags |= VM_USERMAP;
  212. up_write(&current->mm->mmap_sem);
  213. }
  214. return ret;
  215. }
  216. EXPORT_SYMBOL(vmalloc_user);
  217. struct page *vmalloc_to_page(const void *addr)
  218. {
  219. return virt_to_page(addr);
  220. }
  221. EXPORT_SYMBOL(vmalloc_to_page);
  222. unsigned long vmalloc_to_pfn(const void *addr)
  223. {
  224. return page_to_pfn(virt_to_page(addr));
  225. }
  226. EXPORT_SYMBOL(vmalloc_to_pfn);
  227. long vread(char *buf, char *addr, unsigned long count)
  228. {
  229. memcpy(buf, addr, count);
  230. return count;
  231. }
  232. long vwrite(char *buf, char *addr, unsigned long count)
  233. {
  234. /* Don't allow overflow */
  235. if ((unsigned long) addr + count < count)
  236. count = -(unsigned long) addr;
  237. memcpy(addr, buf, count);
  238. return(count);
  239. }
  240. /*
  241. * vmalloc - allocate virtually continguos memory
  242. *
  243. * @size: allocation size
  244. *
  245. * Allocate enough pages to cover @size from the page level
  246. * allocator and map them into continguos kernel virtual space.
  247. *
  248. * For tight control over page level allocator and protection flags
  249. * use __vmalloc() instead.
  250. */
  251. void *vmalloc(unsigned long size)
  252. {
  253. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
  254. }
  255. EXPORT_SYMBOL(vmalloc);
  256. /*
  257. * vzalloc - allocate virtually continguos memory with zero fill
  258. *
  259. * @size: allocation size
  260. *
  261. * Allocate enough pages to cover @size from the page level
  262. * allocator and map them into continguos kernel virtual space.
  263. * The memory allocated is set to zero.
  264. *
  265. * For tight control over page level allocator and protection flags
  266. * use __vmalloc() instead.
  267. */
  268. void *vzalloc(unsigned long size)
  269. {
  270. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
  271. PAGE_KERNEL);
  272. }
  273. EXPORT_SYMBOL(vzalloc);
  274. /**
  275. * vmalloc_node - allocate memory on a specific node
  276. * @size: allocation size
  277. * @node: numa node
  278. *
  279. * Allocate enough pages to cover @size from the page level
  280. * allocator and map them into contiguous kernel virtual space.
  281. *
  282. * For tight control over page level allocator and protection flags
  283. * use __vmalloc() instead.
  284. */
  285. void *vmalloc_node(unsigned long size, int node)
  286. {
  287. return vmalloc(size);
  288. }
  289. EXPORT_SYMBOL(vmalloc_node);
  290. /**
  291. * vzalloc_node - allocate memory on a specific node with zero fill
  292. * @size: allocation size
  293. * @node: numa node
  294. *
  295. * Allocate enough pages to cover @size from the page level
  296. * allocator and map them into contiguous kernel virtual space.
  297. * The memory allocated is set to zero.
  298. *
  299. * For tight control over page level allocator and protection flags
  300. * use __vmalloc() instead.
  301. */
  302. void *vzalloc_node(unsigned long size, int node)
  303. {
  304. return vzalloc(size);
  305. }
  306. EXPORT_SYMBOL(vzalloc_node);
  307. #ifndef PAGE_KERNEL_EXEC
  308. # define PAGE_KERNEL_EXEC PAGE_KERNEL
  309. #endif
  310. /**
  311. * vmalloc_exec - allocate virtually contiguous, executable memory
  312. * @size: allocation size
  313. *
  314. * Kernel-internal function to allocate enough pages to cover @size
  315. * the page level allocator and map them into contiguous and
  316. * executable kernel virtual space.
  317. *
  318. * For tight control over page level allocator and protection flags
  319. * use __vmalloc() instead.
  320. */
  321. void *vmalloc_exec(unsigned long size)
  322. {
  323. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC);
  324. }
  325. /**
  326. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  327. * @size: allocation size
  328. *
  329. * Allocate enough 32bit PA addressable pages to cover @size from the
  330. * page level allocator and map them into continguos kernel virtual space.
  331. */
  332. void *vmalloc_32(unsigned long size)
  333. {
  334. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
  335. }
  336. EXPORT_SYMBOL(vmalloc_32);
  337. /**
  338. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  339. * @size: allocation size
  340. *
  341. * The resulting memory area is 32bit addressable and zeroed so it can be
  342. * mapped to userspace without leaking data.
  343. *
  344. * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
  345. * remap_vmalloc_range() are permissible.
  346. */
  347. void *vmalloc_32_user(unsigned long size)
  348. {
  349. /*
  350. * We'll have to sort out the ZONE_DMA bits for 64-bit,
  351. * but for now this can simply use vmalloc_user() directly.
  352. */
  353. return vmalloc_user(size);
  354. }
  355. EXPORT_SYMBOL(vmalloc_32_user);
  356. void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
  357. {
  358. BUG();
  359. return NULL;
  360. }
  361. EXPORT_SYMBOL(vmap);
  362. void vunmap(const void *addr)
  363. {
  364. BUG();
  365. }
  366. EXPORT_SYMBOL(vunmap);
  367. void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
  368. {
  369. BUG();
  370. return NULL;
  371. }
  372. EXPORT_SYMBOL(vm_map_ram);
  373. void vm_unmap_ram(const void *mem, unsigned int count)
  374. {
  375. BUG();
  376. }
  377. EXPORT_SYMBOL(vm_unmap_ram);
  378. void vm_unmap_aliases(void)
  379. {
  380. }
  381. EXPORT_SYMBOL_GPL(vm_unmap_aliases);
  382. /*
  383. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  384. * have one.
  385. */
  386. void __attribute__((weak)) vmalloc_sync_all(void)
  387. {
  388. }
  389. /**
  390. * alloc_vm_area - allocate a range of kernel address space
  391. * @size: size of the area
  392. *
  393. * Returns: NULL on failure, vm_struct on success
  394. *
  395. * This function reserves a range of kernel address space, and
  396. * allocates pagetables to map that range. No actual mappings
  397. * are created. If the kernel address space is not shared
  398. * between processes, it syncs the pagetable across all
  399. * processes.
  400. */
  401. struct vm_struct *alloc_vm_area(size_t size)
  402. {
  403. BUG();
  404. return NULL;
  405. }
  406. EXPORT_SYMBOL_GPL(alloc_vm_area);
  407. void free_vm_area(struct vm_struct *area)
  408. {
  409. BUG();
  410. }
  411. EXPORT_SYMBOL_GPL(free_vm_area);
  412. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  413. struct page *page)
  414. {
  415. return -EINVAL;
  416. }
  417. EXPORT_SYMBOL(vm_insert_page);
  418. /*
  419. * sys_brk() for the most part doesn't need the global kernel
  420. * lock, except when an application is doing something nasty
  421. * like trying to un-brk an area that has already been mapped
  422. * to a regular file. in this case, the unmapping will need
  423. * to invoke file system routines that need the global lock.
  424. */
  425. SYSCALL_DEFINE1(brk, unsigned long, brk)
  426. {
  427. struct mm_struct *mm = current->mm;
  428. if (brk < mm->start_brk || brk > mm->context.end_brk)
  429. return mm->brk;
  430. if (mm->brk == brk)
  431. return mm->brk;
  432. /*
  433. * Always allow shrinking brk
  434. */
  435. if (brk <= mm->brk) {
  436. mm->brk = brk;
  437. return brk;
  438. }
  439. /*
  440. * Ok, looks good - let it rip.
  441. */
  442. flush_icache_range(mm->brk, brk);
  443. return mm->brk = brk;
  444. }
  445. /*
  446. * initialise the VMA and region record slabs
  447. */
  448. void __init mmap_init(void)
  449. {
  450. int ret;
  451. ret = percpu_counter_init(&vm_committed_as, 0);
  452. VM_BUG_ON(ret);
  453. vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC);
  454. }
  455. /*
  456. * validate the region tree
  457. * - the caller must hold the region lock
  458. */
  459. #ifdef CONFIG_DEBUG_NOMMU_REGIONS
  460. static noinline void validate_nommu_regions(void)
  461. {
  462. struct vm_region *region, *last;
  463. struct rb_node *p, *lastp;
  464. lastp = rb_first(&nommu_region_tree);
  465. if (!lastp)
  466. return;
  467. last = rb_entry(lastp, struct vm_region, vm_rb);
  468. BUG_ON(unlikely(last->vm_end <= last->vm_start));
  469. BUG_ON(unlikely(last->vm_top < last->vm_end));
  470. while ((p = rb_next(lastp))) {
  471. region = rb_entry(p, struct vm_region, vm_rb);
  472. last = rb_entry(lastp, struct vm_region, vm_rb);
  473. BUG_ON(unlikely(region->vm_end <= region->vm_start));
  474. BUG_ON(unlikely(region->vm_top < region->vm_end));
  475. BUG_ON(unlikely(region->vm_start < last->vm_top));
  476. lastp = p;
  477. }
  478. }
  479. #else
  480. static void validate_nommu_regions(void)
  481. {
  482. }
  483. #endif
  484. /*
  485. * add a region into the global tree
  486. */
  487. static void add_nommu_region(struct vm_region *region)
  488. {
  489. struct vm_region *pregion;
  490. struct rb_node **p, *parent;
  491. validate_nommu_regions();
  492. parent = NULL;
  493. p = &nommu_region_tree.rb_node;
  494. while (*p) {
  495. parent = *p;
  496. pregion = rb_entry(parent, struct vm_region, vm_rb);
  497. if (region->vm_start < pregion->vm_start)
  498. p = &(*p)->rb_left;
  499. else if (region->vm_start > pregion->vm_start)
  500. p = &(*p)->rb_right;
  501. else if (pregion == region)
  502. return;
  503. else
  504. BUG();
  505. }
  506. rb_link_node(&region->vm_rb, parent, p);
  507. rb_insert_color(&region->vm_rb, &nommu_region_tree);
  508. validate_nommu_regions();
  509. }
  510. /*
  511. * delete a region from the global tree
  512. */
  513. static void delete_nommu_region(struct vm_region *region)
  514. {
  515. BUG_ON(!nommu_region_tree.rb_node);
  516. validate_nommu_regions();
  517. rb_erase(&region->vm_rb, &nommu_region_tree);
  518. validate_nommu_regions();
  519. }
  520. /*
  521. * free a contiguous series of pages
  522. */
  523. static void free_page_series(unsigned long from, unsigned long to)
  524. {
  525. for (; from < to; from += PAGE_SIZE) {
  526. struct page *page = virt_to_page(from);
  527. kdebug("- free %lx", from);
  528. atomic_long_dec(&mmap_pages_allocated);
  529. if (page_count(page) != 1)
  530. kdebug("free page %p: refcount not one: %d",
  531. page, page_count(page));
  532. put_page(page);
  533. }
  534. }
  535. /*
  536. * release a reference to a region
  537. * - the caller must hold the region semaphore for writing, which this releases
  538. * - the region may not have been added to the tree yet, in which case vm_top
  539. * will equal vm_start
  540. */
  541. static void __put_nommu_region(struct vm_region *region)
  542. __releases(nommu_region_sem)
  543. {
  544. kenter("%p{%d}", region, region->vm_usage);
  545. BUG_ON(!nommu_region_tree.rb_node);
  546. if (--region->vm_usage == 0) {
  547. if (region->vm_top > region->vm_start)
  548. delete_nommu_region(region);
  549. up_write(&nommu_region_sem);
  550. if (region->vm_file)
  551. fput(region->vm_file);
  552. /* IO memory and memory shared directly out of the pagecache
  553. * from ramfs/tmpfs mustn't be released here */
  554. if (region->vm_flags & VM_MAPPED_COPY) {
  555. kdebug("free series");
  556. free_page_series(region->vm_start, region->vm_top);
  557. }
  558. kmem_cache_free(vm_region_jar, region);
  559. } else {
  560. up_write(&nommu_region_sem);
  561. }
  562. }
  563. /*
  564. * release a reference to a region
  565. */
  566. static void put_nommu_region(struct vm_region *region)
  567. {
  568. down_write(&nommu_region_sem);
  569. __put_nommu_region(region);
  570. }
  571. /*
  572. * update protection on a vma
  573. */
  574. static void protect_vma(struct vm_area_struct *vma, unsigned long flags)
  575. {
  576. #ifdef CONFIG_MPU
  577. struct mm_struct *mm = vma->vm_mm;
  578. long start = vma->vm_start & PAGE_MASK;
  579. while (start < vma->vm_end) {
  580. protect_page(mm, start, flags);
  581. start += PAGE_SIZE;
  582. }
  583. update_protections(mm);
  584. #endif
  585. }
  586. /*
  587. * add a VMA into a process's mm_struct in the appropriate place in the list
  588. * and tree and add to the address space's page tree also if not an anonymous
  589. * page
  590. * - should be called with mm->mmap_sem held writelocked
  591. */
  592. static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
  593. {
  594. struct vm_area_struct *pvma, **pp, *next;
  595. struct address_space *mapping;
  596. struct rb_node **p, *parent;
  597. kenter(",%p", vma);
  598. BUG_ON(!vma->vm_region);
  599. mm->map_count++;
  600. vma->vm_mm = mm;
  601. protect_vma(vma, vma->vm_flags);
  602. /* add the VMA to the mapping */
  603. if (vma->vm_file) {
  604. mapping = vma->vm_file->f_mapping;
  605. flush_dcache_mmap_lock(mapping);
  606. vma_prio_tree_insert(vma, &mapping->i_mmap);
  607. flush_dcache_mmap_unlock(mapping);
  608. }
  609. /* add the VMA to the tree */
  610. parent = NULL;
  611. p = &mm->mm_rb.rb_node;
  612. while (*p) {
  613. parent = *p;
  614. pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
  615. /* sort by: start addr, end addr, VMA struct addr in that order
  616. * (the latter is necessary as we may get identical VMAs) */
  617. if (vma->vm_start < pvma->vm_start)
  618. p = &(*p)->rb_left;
  619. else if (vma->vm_start > pvma->vm_start)
  620. p = &(*p)->rb_right;
  621. else if (vma->vm_end < pvma->vm_end)
  622. p = &(*p)->rb_left;
  623. else if (vma->vm_end > pvma->vm_end)
  624. p = &(*p)->rb_right;
  625. else if (vma < pvma)
  626. p = &(*p)->rb_left;
  627. else if (vma > pvma)
  628. p = &(*p)->rb_right;
  629. else
  630. BUG();
  631. }
  632. rb_link_node(&vma->vm_rb, parent, p);
  633. rb_insert_color(&vma->vm_rb, &mm->mm_rb);
  634. /* add VMA to the VMA list also */
  635. for (pp = &mm->mmap; (pvma = *pp); pp = &(*pp)->vm_next) {
  636. if (pvma->vm_start > vma->vm_start)
  637. break;
  638. if (pvma->vm_start < vma->vm_start)
  639. continue;
  640. if (pvma->vm_end < vma->vm_end)
  641. break;
  642. }
  643. next = *pp;
  644. *pp = vma;
  645. vma->vm_next = next;
  646. if (next)
  647. next->vm_prev = vma;
  648. }
  649. /*
  650. * delete a VMA from its owning mm_struct and address space
  651. */
  652. static void delete_vma_from_mm(struct vm_area_struct *vma)
  653. {
  654. struct vm_area_struct **pp;
  655. struct address_space *mapping;
  656. struct mm_struct *mm = vma->vm_mm;
  657. kenter("%p", vma);
  658. protect_vma(vma, 0);
  659. mm->map_count--;
  660. if (mm->mmap_cache == vma)
  661. mm->mmap_cache = NULL;
  662. /* remove the VMA from the mapping */
  663. if (vma->vm_file) {
  664. mapping = vma->vm_file->f_mapping;
  665. flush_dcache_mmap_lock(mapping);
  666. vma_prio_tree_remove(vma, &mapping->i_mmap);
  667. flush_dcache_mmap_unlock(mapping);
  668. }
  669. /* remove from the MM's tree and list */
  670. rb_erase(&vma->vm_rb, &mm->mm_rb);
  671. for (pp = &mm->mmap; *pp; pp = &(*pp)->vm_next) {
  672. if (*pp == vma) {
  673. *pp = vma->vm_next;
  674. break;
  675. }
  676. }
  677. vma->vm_mm = NULL;
  678. }
  679. /*
  680. * destroy a VMA record
  681. */
  682. static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
  683. {
  684. kenter("%p", vma);
  685. if (vma->vm_ops && vma->vm_ops->close)
  686. vma->vm_ops->close(vma);
  687. if (vma->vm_file) {
  688. fput(vma->vm_file);
  689. if (vma->vm_flags & VM_EXECUTABLE)
  690. removed_exe_file_vma(mm);
  691. }
  692. put_nommu_region(vma->vm_region);
  693. kmem_cache_free(vm_area_cachep, vma);
  694. }
  695. /*
  696. * look up the first VMA in which addr resides, NULL if none
  697. * - should be called with mm->mmap_sem at least held readlocked
  698. */
  699. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  700. {
  701. struct vm_area_struct *vma;
  702. struct rb_node *n = mm->mm_rb.rb_node;
  703. /* check the cache first */
  704. vma = mm->mmap_cache;
  705. if (vma && vma->vm_start <= addr && vma->vm_end > addr)
  706. return vma;
  707. /* trawl the tree (there may be multiple mappings in which addr
  708. * resides) */
  709. for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
  710. vma = rb_entry(n, struct vm_area_struct, vm_rb);
  711. if (vma->vm_start > addr)
  712. return NULL;
  713. if (vma->vm_end > addr) {
  714. mm->mmap_cache = vma;
  715. return vma;
  716. }
  717. }
  718. return NULL;
  719. }
  720. EXPORT_SYMBOL(find_vma);
  721. /*
  722. * find a VMA
  723. * - we don't extend stack VMAs under NOMMU conditions
  724. */
  725. struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
  726. {
  727. return find_vma(mm, addr);
  728. }
  729. /*
  730. * expand a stack to a given address
  731. * - not supported under NOMMU conditions
  732. */
  733. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  734. {
  735. return -ENOMEM;
  736. }
  737. /*
  738. * look up the first VMA exactly that exactly matches addr
  739. * - should be called with mm->mmap_sem at least held readlocked
  740. */
  741. static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
  742. unsigned long addr,
  743. unsigned long len)
  744. {
  745. struct vm_area_struct *vma;
  746. struct rb_node *n = mm->mm_rb.rb_node;
  747. unsigned long end = addr + len;
  748. /* check the cache first */
  749. vma = mm->mmap_cache;
  750. if (vma && vma->vm_start == addr && vma->vm_end == end)
  751. return vma;
  752. /* trawl the tree (there may be multiple mappings in which addr
  753. * resides) */
  754. for (n = rb_first(&mm->mm_rb); n; n = rb_next(n)) {
  755. vma = rb_entry(n, struct vm_area_struct, vm_rb);
  756. if (vma->vm_start < addr)
  757. continue;
  758. if (vma->vm_start > addr)
  759. return NULL;
  760. if (vma->vm_end == end) {
  761. mm->mmap_cache = vma;
  762. return vma;
  763. }
  764. }
  765. return NULL;
  766. }
  767. /*
  768. * determine whether a mapping should be permitted and, if so, what sort of
  769. * mapping we're capable of supporting
  770. */
  771. static int validate_mmap_request(struct file *file,
  772. unsigned long addr,
  773. unsigned long len,
  774. unsigned long prot,
  775. unsigned long flags,
  776. unsigned long pgoff,
  777. unsigned long *_capabilities)
  778. {
  779. unsigned long capabilities, rlen;
  780. unsigned long reqprot = prot;
  781. int ret;
  782. /* do the simple checks first */
  783. if (flags & MAP_FIXED) {
  784. printk(KERN_DEBUG
  785. "%d: Can't do fixed-address/overlay mmap of RAM\n",
  786. current->pid);
  787. return -EINVAL;
  788. }
  789. if ((flags & MAP_TYPE) != MAP_PRIVATE &&
  790. (flags & MAP_TYPE) != MAP_SHARED)
  791. return -EINVAL;
  792. if (!len)
  793. return -EINVAL;
  794. /* Careful about overflows.. */
  795. rlen = PAGE_ALIGN(len);
  796. if (!rlen || rlen > TASK_SIZE)
  797. return -ENOMEM;
  798. /* offset overflow? */
  799. if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
  800. return -EOVERFLOW;
  801. if (file) {
  802. /* validate file mapping requests */
  803. struct address_space *mapping;
  804. /* files must support mmap */
  805. if (!file->f_op || !file->f_op->mmap)
  806. return -ENODEV;
  807. /* work out if what we've got could possibly be shared
  808. * - we support chardevs that provide their own "memory"
  809. * - we support files/blockdevs that are memory backed
  810. */
  811. mapping = file->f_mapping;
  812. if (!mapping)
  813. mapping = file->f_path.dentry->d_inode->i_mapping;
  814. capabilities = 0;
  815. if (mapping && mapping->backing_dev_info)
  816. capabilities = mapping->backing_dev_info->capabilities;
  817. if (!capabilities) {
  818. /* no explicit capabilities set, so assume some
  819. * defaults */
  820. switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
  821. case S_IFREG:
  822. case S_IFBLK:
  823. capabilities = BDI_CAP_MAP_COPY;
  824. break;
  825. case S_IFCHR:
  826. capabilities =
  827. BDI_CAP_MAP_DIRECT |
  828. BDI_CAP_READ_MAP |
  829. BDI_CAP_WRITE_MAP;
  830. break;
  831. default:
  832. return -EINVAL;
  833. }
  834. }
  835. /* eliminate any capabilities that we can't support on this
  836. * device */
  837. if (!file->f_op->get_unmapped_area)
  838. capabilities &= ~BDI_CAP_MAP_DIRECT;
  839. if (!file->f_op->read)
  840. capabilities &= ~BDI_CAP_MAP_COPY;
  841. /* The file shall have been opened with read permission. */
  842. if (!(file->f_mode & FMODE_READ))
  843. return -EACCES;
  844. if (flags & MAP_SHARED) {
  845. /* do checks for writing, appending and locking */
  846. if ((prot & PROT_WRITE) &&
  847. !(file->f_mode & FMODE_WRITE))
  848. return -EACCES;
  849. if (IS_APPEND(file->f_path.dentry->d_inode) &&
  850. (file->f_mode & FMODE_WRITE))
  851. return -EACCES;
  852. if (locks_verify_locked(file->f_path.dentry->d_inode))
  853. return -EAGAIN;
  854. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  855. return -ENODEV;
  856. /* we mustn't privatise shared mappings */
  857. capabilities &= ~BDI_CAP_MAP_COPY;
  858. }
  859. else {
  860. /* we're going to read the file into private memory we
  861. * allocate */
  862. if (!(capabilities & BDI_CAP_MAP_COPY))
  863. return -ENODEV;
  864. /* we don't permit a private writable mapping to be
  865. * shared with the backing device */
  866. if (prot & PROT_WRITE)
  867. capabilities &= ~BDI_CAP_MAP_DIRECT;
  868. }
  869. if (capabilities & BDI_CAP_MAP_DIRECT) {
  870. if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
  871. ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
  872. ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
  873. ) {
  874. capabilities &= ~BDI_CAP_MAP_DIRECT;
  875. if (flags & MAP_SHARED) {
  876. printk(KERN_WARNING
  877. "MAP_SHARED not completely supported on !MMU\n");
  878. return -EINVAL;
  879. }
  880. }
  881. }
  882. /* handle executable mappings and implied executable
  883. * mappings */
  884. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  885. if (prot & PROT_EXEC)
  886. return -EPERM;
  887. }
  888. else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  889. /* handle implication of PROT_EXEC by PROT_READ */
  890. if (current->personality & READ_IMPLIES_EXEC) {
  891. if (capabilities & BDI_CAP_EXEC_MAP)
  892. prot |= PROT_EXEC;
  893. }
  894. }
  895. else if ((prot & PROT_READ) &&
  896. (prot & PROT_EXEC) &&
  897. !(capabilities & BDI_CAP_EXEC_MAP)
  898. ) {
  899. /* backing file is not executable, try to copy */
  900. capabilities &= ~BDI_CAP_MAP_DIRECT;
  901. }
  902. }
  903. else {
  904. /* anonymous mappings are always memory backed and can be
  905. * privately mapped
  906. */
  907. capabilities = BDI_CAP_MAP_COPY;
  908. /* handle PROT_EXEC implication by PROT_READ */
  909. if ((prot & PROT_READ) &&
  910. (current->personality & READ_IMPLIES_EXEC))
  911. prot |= PROT_EXEC;
  912. }
  913. /* allow the security API to have its say */
  914. ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  915. if (ret < 0)
  916. return ret;
  917. /* looks okay */
  918. *_capabilities = capabilities;
  919. return 0;
  920. }
  921. /*
  922. * we've determined that we can make the mapping, now translate what we
  923. * now know into VMA flags
  924. */
  925. static unsigned long determine_vm_flags(struct file *file,
  926. unsigned long prot,
  927. unsigned long flags,
  928. unsigned long capabilities)
  929. {
  930. unsigned long vm_flags;
  931. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
  932. /* vm_flags |= mm->def_flags; */
  933. if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
  934. /* attempt to share read-only copies of mapped file chunks */
  935. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  936. if (file && !(prot & PROT_WRITE))
  937. vm_flags |= VM_MAYSHARE;
  938. } else {
  939. /* overlay a shareable mapping on the backing device or inode
  940. * if possible - used for chardevs, ramfs/tmpfs/shmfs and
  941. * romfs/cramfs */
  942. vm_flags |= VM_MAYSHARE | (capabilities & BDI_CAP_VMFLAGS);
  943. if (flags & MAP_SHARED)
  944. vm_flags |= VM_SHARED;
  945. }
  946. /* refuse to let anyone share private mappings with this process if
  947. * it's being traced - otherwise breakpoints set in it may interfere
  948. * with another untraced process
  949. */
  950. if ((flags & MAP_PRIVATE) && tracehook_expect_breakpoints(current))
  951. vm_flags &= ~VM_MAYSHARE;
  952. return vm_flags;
  953. }
  954. /*
  955. * set up a shared mapping on a file (the driver or filesystem provides and
  956. * pins the storage)
  957. */
  958. static int do_mmap_shared_file(struct vm_area_struct *vma)
  959. {
  960. int ret;
  961. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  962. if (ret == 0) {
  963. vma->vm_region->vm_top = vma->vm_region->vm_end;
  964. return 0;
  965. }
  966. if (ret != -ENOSYS)
  967. return ret;
  968. /* getting -ENOSYS indicates that direct mmap isn't possible (as
  969. * opposed to tried but failed) so we can only give a suitable error as
  970. * it's not possible to make a private copy if MAP_SHARED was given */
  971. return -ENODEV;
  972. }
  973. /*
  974. * set up a private mapping or an anonymous shared mapping
  975. */
  976. static int do_mmap_private(struct vm_area_struct *vma,
  977. struct vm_region *region,
  978. unsigned long len,
  979. unsigned long capabilities)
  980. {
  981. struct page *pages;
  982. unsigned long total, point, n, rlen;
  983. void *base;
  984. int ret, order;
  985. /* invoke the file's mapping function so that it can keep track of
  986. * shared mappings on devices or memory
  987. * - VM_MAYSHARE will be set if it may attempt to share
  988. */
  989. if (capabilities & BDI_CAP_MAP_DIRECT) {
  990. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  991. if (ret == 0) {
  992. /* shouldn't return success if we're not sharing */
  993. BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
  994. vma->vm_region->vm_top = vma->vm_region->vm_end;
  995. return 0;
  996. }
  997. if (ret != -ENOSYS)
  998. return ret;
  999. /* getting an ENOSYS error indicates that direct mmap isn't
  1000. * possible (as opposed to tried but failed) so we'll try to
  1001. * make a private copy of the data and map that instead */
  1002. }
  1003. rlen = PAGE_ALIGN(len);
  1004. /* allocate some memory to hold the mapping
  1005. * - note that this may not return a page-aligned address if the object
  1006. * we're allocating is smaller than a page
  1007. */
  1008. order = get_order(rlen);
  1009. kdebug("alloc order %d for %lx", order, len);
  1010. pages = alloc_pages(GFP_KERNEL, order);
  1011. if (!pages)
  1012. goto enomem;
  1013. total = 1 << order;
  1014. atomic_long_add(total, &mmap_pages_allocated);
  1015. point = rlen >> PAGE_SHIFT;
  1016. /* we allocated a power-of-2 sized page set, so we may want to trim off
  1017. * the excess */
  1018. if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages) {
  1019. while (total > point) {
  1020. order = ilog2(total - point);
  1021. n = 1 << order;
  1022. kdebug("shave %lu/%lu @%lu", n, total - point, total);
  1023. atomic_long_sub(n, &mmap_pages_allocated);
  1024. total -= n;
  1025. set_page_refcounted(pages + total);
  1026. __free_pages(pages + total, order);
  1027. }
  1028. }
  1029. for (point = 1; point < total; point++)
  1030. set_page_refcounted(&pages[point]);
  1031. base = page_address(pages);
  1032. region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
  1033. region->vm_start = (unsigned long) base;
  1034. region->vm_end = region->vm_start + rlen;
  1035. region->vm_top = region->vm_start + (total << PAGE_SHIFT);
  1036. vma->vm_start = region->vm_start;
  1037. vma->vm_end = region->vm_start + len;
  1038. if (vma->vm_file) {
  1039. /* read the contents of a file into the copy */
  1040. mm_segment_t old_fs;
  1041. loff_t fpos;
  1042. fpos = vma->vm_pgoff;
  1043. fpos <<= PAGE_SHIFT;
  1044. old_fs = get_fs();
  1045. set_fs(KERNEL_DS);
  1046. ret = vma->vm_file->f_op->read(vma->vm_file, base, rlen, &fpos);
  1047. set_fs(old_fs);
  1048. if (ret < 0)
  1049. goto error_free;
  1050. /* clear the last little bit */
  1051. if (ret < rlen)
  1052. memset(base + ret, 0, rlen - ret);
  1053. }
  1054. return 0;
  1055. error_free:
  1056. free_page_series(region->vm_start, region->vm_end);
  1057. region->vm_start = vma->vm_start = 0;
  1058. region->vm_end = vma->vm_end = 0;
  1059. region->vm_top = 0;
  1060. return ret;
  1061. enomem:
  1062. printk("Allocation of length %lu from process %d (%s) failed\n",
  1063. len, current->pid, current->comm);
  1064. show_free_areas();
  1065. return -ENOMEM;
  1066. }
  1067. /*
  1068. * handle mapping creation for uClinux
  1069. */
  1070. unsigned long do_mmap_pgoff(struct file *file,
  1071. unsigned long addr,
  1072. unsigned long len,
  1073. unsigned long prot,
  1074. unsigned long flags,
  1075. unsigned long pgoff)
  1076. {
  1077. struct vm_area_struct *vma;
  1078. struct vm_region *region;
  1079. struct rb_node *rb;
  1080. unsigned long capabilities, vm_flags, result;
  1081. int ret;
  1082. kenter(",%lx,%lx,%lx,%lx,%lx", addr, len, prot, flags, pgoff);
  1083. /* decide whether we should attempt the mapping, and if so what sort of
  1084. * mapping */
  1085. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  1086. &capabilities);
  1087. if (ret < 0) {
  1088. kleave(" = %d [val]", ret);
  1089. return ret;
  1090. }
  1091. /* we ignore the address hint */
  1092. addr = 0;
  1093. /* we've determined that we can make the mapping, now translate what we
  1094. * now know into VMA flags */
  1095. vm_flags = determine_vm_flags(file, prot, flags, capabilities);
  1096. /* we're going to need to record the mapping */
  1097. region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
  1098. if (!region)
  1099. goto error_getting_region;
  1100. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1101. if (!vma)
  1102. goto error_getting_vma;
  1103. region->vm_usage = 1;
  1104. region->vm_flags = vm_flags;
  1105. region->vm_pgoff = pgoff;
  1106. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1107. vma->vm_flags = vm_flags;
  1108. vma->vm_pgoff = pgoff;
  1109. if (file) {
  1110. region->vm_file = file;
  1111. get_file(file);
  1112. vma->vm_file = file;
  1113. get_file(file);
  1114. if (vm_flags & VM_EXECUTABLE) {
  1115. added_exe_file_vma(current->mm);
  1116. vma->vm_mm = current->mm;
  1117. }
  1118. }
  1119. down_write(&nommu_region_sem);
  1120. /* if we want to share, we need to check for regions created by other
  1121. * mmap() calls that overlap with our proposed mapping
  1122. * - we can only share with a superset match on most regular files
  1123. * - shared mappings on character devices and memory backed files are
  1124. * permitted to overlap inexactly as far as we are concerned for in
  1125. * these cases, sharing is handled in the driver or filesystem rather
  1126. * than here
  1127. */
  1128. if (vm_flags & VM_MAYSHARE) {
  1129. struct vm_region *pregion;
  1130. unsigned long pglen, rpglen, pgend, rpgend, start;
  1131. pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1132. pgend = pgoff + pglen;
  1133. for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
  1134. pregion = rb_entry(rb, struct vm_region, vm_rb);
  1135. if (!(pregion->vm_flags & VM_MAYSHARE))
  1136. continue;
  1137. /* search for overlapping mappings on the same file */
  1138. if (pregion->vm_file->f_path.dentry->d_inode !=
  1139. file->f_path.dentry->d_inode)
  1140. continue;
  1141. if (pregion->vm_pgoff >= pgend)
  1142. continue;
  1143. rpglen = pregion->vm_end - pregion->vm_start;
  1144. rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1145. rpgend = pregion->vm_pgoff + rpglen;
  1146. if (pgoff >= rpgend)
  1147. continue;
  1148. /* handle inexactly overlapping matches between
  1149. * mappings */
  1150. if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
  1151. !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
  1152. /* new mapping is not a subset of the region */
  1153. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  1154. goto sharing_violation;
  1155. continue;
  1156. }
  1157. /* we've found a region we can share */
  1158. pregion->vm_usage++;
  1159. vma->vm_region = pregion;
  1160. start = pregion->vm_start;
  1161. start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
  1162. vma->vm_start = start;
  1163. vma->vm_end = start + len;
  1164. if (pregion->vm_flags & VM_MAPPED_COPY) {
  1165. kdebug("share copy");
  1166. vma->vm_flags |= VM_MAPPED_COPY;
  1167. } else {
  1168. kdebug("share mmap");
  1169. ret = do_mmap_shared_file(vma);
  1170. if (ret < 0) {
  1171. vma->vm_region = NULL;
  1172. vma->vm_start = 0;
  1173. vma->vm_end = 0;
  1174. pregion->vm_usage--;
  1175. pregion = NULL;
  1176. goto error_just_free;
  1177. }
  1178. }
  1179. fput(region->vm_file);
  1180. kmem_cache_free(vm_region_jar, region);
  1181. region = pregion;
  1182. result = start;
  1183. goto share;
  1184. }
  1185. /* obtain the address at which to make a shared mapping
  1186. * - this is the hook for quasi-memory character devices to
  1187. * tell us the location of a shared mapping
  1188. */
  1189. if (capabilities & BDI_CAP_MAP_DIRECT) {
  1190. addr = file->f_op->get_unmapped_area(file, addr, len,
  1191. pgoff, flags);
  1192. if (IS_ERR((void *) addr)) {
  1193. ret = addr;
  1194. if (ret != (unsigned long) -ENOSYS)
  1195. goto error_just_free;
  1196. /* the driver refused to tell us where to site
  1197. * the mapping so we'll have to attempt to copy
  1198. * it */
  1199. ret = (unsigned long) -ENODEV;
  1200. if (!(capabilities & BDI_CAP_MAP_COPY))
  1201. goto error_just_free;
  1202. capabilities &= ~BDI_CAP_MAP_DIRECT;
  1203. } else {
  1204. vma->vm_start = region->vm_start = addr;
  1205. vma->vm_end = region->vm_end = addr + len;
  1206. }
  1207. }
  1208. }
  1209. vma->vm_region = region;
  1210. /* set up the mapping
  1211. * - the region is filled in if BDI_CAP_MAP_DIRECT is still set
  1212. */
  1213. if (file && vma->vm_flags & VM_SHARED)
  1214. ret = do_mmap_shared_file(vma);
  1215. else
  1216. ret = do_mmap_private(vma, region, len, capabilities);
  1217. if (ret < 0)
  1218. goto error_just_free;
  1219. add_nommu_region(region);
  1220. /* clear anonymous mappings that don't ask for uninitialized data */
  1221. if (!vma->vm_file && !(flags & MAP_UNINITIALIZED))
  1222. memset((void *)region->vm_start, 0,
  1223. region->vm_end - region->vm_start);
  1224. /* okay... we have a mapping; now we have to register it */
  1225. result = vma->vm_start;
  1226. current->mm->total_vm += len >> PAGE_SHIFT;
  1227. share:
  1228. add_vma_to_mm(current->mm, vma);
  1229. /* we flush the region from the icache only when the first executable
  1230. * mapping of it is made */
  1231. if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
  1232. flush_icache_range(region->vm_start, region->vm_end);
  1233. region->vm_icache_flushed = true;
  1234. }
  1235. up_write(&nommu_region_sem);
  1236. kleave(" = %lx", result);
  1237. return result;
  1238. error_just_free:
  1239. up_write(&nommu_region_sem);
  1240. error:
  1241. if (region->vm_file)
  1242. fput(region->vm_file);
  1243. kmem_cache_free(vm_region_jar, region);
  1244. if (vma->vm_file)
  1245. fput(vma->vm_file);
  1246. if (vma->vm_flags & VM_EXECUTABLE)
  1247. removed_exe_file_vma(vma->vm_mm);
  1248. kmem_cache_free(vm_area_cachep, vma);
  1249. kleave(" = %d", ret);
  1250. return ret;
  1251. sharing_violation:
  1252. up_write(&nommu_region_sem);
  1253. printk(KERN_WARNING "Attempt to share mismatched mappings\n");
  1254. ret = -EINVAL;
  1255. goto error;
  1256. error_getting_vma:
  1257. kmem_cache_free(vm_region_jar, region);
  1258. printk(KERN_WARNING "Allocation of vma for %lu byte allocation"
  1259. " from process %d failed\n",
  1260. len, current->pid);
  1261. show_free_areas();
  1262. return -ENOMEM;
  1263. error_getting_region:
  1264. printk(KERN_WARNING "Allocation of vm region for %lu byte allocation"
  1265. " from process %d failed\n",
  1266. len, current->pid);
  1267. show_free_areas();
  1268. return -ENOMEM;
  1269. }
  1270. EXPORT_SYMBOL(do_mmap_pgoff);
  1271. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  1272. unsigned long, prot, unsigned long, flags,
  1273. unsigned long, fd, unsigned long, pgoff)
  1274. {
  1275. struct file *file = NULL;
  1276. unsigned long retval = -EBADF;
  1277. audit_mmap_fd(fd, flags);
  1278. if (!(flags & MAP_ANONYMOUS)) {
  1279. file = fget(fd);
  1280. if (!file)
  1281. goto out;
  1282. }
  1283. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  1284. down_write(&current->mm->mmap_sem);
  1285. retval = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  1286. up_write(&current->mm->mmap_sem);
  1287. if (file)
  1288. fput(file);
  1289. out:
  1290. return retval;
  1291. }
  1292. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  1293. struct mmap_arg_struct {
  1294. unsigned long addr;
  1295. unsigned long len;
  1296. unsigned long prot;
  1297. unsigned long flags;
  1298. unsigned long fd;
  1299. unsigned long offset;
  1300. };
  1301. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  1302. {
  1303. struct mmap_arg_struct a;
  1304. if (copy_from_user(&a, arg, sizeof(a)))
  1305. return -EFAULT;
  1306. if (a.offset & ~PAGE_MASK)
  1307. return -EINVAL;
  1308. return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  1309. a.offset >> PAGE_SHIFT);
  1310. }
  1311. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  1312. /*
  1313. * split a vma into two pieces at address 'addr', a new vma is allocated either
  1314. * for the first part or the tail.
  1315. */
  1316. int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
  1317. unsigned long addr, int new_below)
  1318. {
  1319. struct vm_area_struct *new;
  1320. struct vm_region *region;
  1321. unsigned long npages;
  1322. kenter("");
  1323. /* we're only permitted to split anonymous regions (these should have
  1324. * only a single usage on the region) */
  1325. if (vma->vm_file)
  1326. return -ENOMEM;
  1327. if (mm->map_count >= sysctl_max_map_count)
  1328. return -ENOMEM;
  1329. region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
  1330. if (!region)
  1331. return -ENOMEM;
  1332. new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  1333. if (!new) {
  1334. kmem_cache_free(vm_region_jar, region);
  1335. return -ENOMEM;
  1336. }
  1337. /* most fields are the same, copy all, and then fixup */
  1338. *new = *vma;
  1339. *region = *vma->vm_region;
  1340. new->vm_region = region;
  1341. npages = (addr - vma->vm_start) >> PAGE_SHIFT;
  1342. if (new_below) {
  1343. region->vm_top = region->vm_end = new->vm_end = addr;
  1344. } else {
  1345. region->vm_start = new->vm_start = addr;
  1346. region->vm_pgoff = new->vm_pgoff += npages;
  1347. }
  1348. if (new->vm_ops && new->vm_ops->open)
  1349. new->vm_ops->open(new);
  1350. delete_vma_from_mm(vma);
  1351. down_write(&nommu_region_sem);
  1352. delete_nommu_region(vma->vm_region);
  1353. if (new_below) {
  1354. vma->vm_region->vm_start = vma->vm_start = addr;
  1355. vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
  1356. } else {
  1357. vma->vm_region->vm_end = vma->vm_end = addr;
  1358. vma->vm_region->vm_top = addr;
  1359. }
  1360. add_nommu_region(vma->vm_region);
  1361. add_nommu_region(new->vm_region);
  1362. up_write(&nommu_region_sem);
  1363. add_vma_to_mm(mm, vma);
  1364. add_vma_to_mm(mm, new);
  1365. return 0;
  1366. }
  1367. /*
  1368. * shrink a VMA by removing the specified chunk from either the beginning or
  1369. * the end
  1370. */
  1371. static int shrink_vma(struct mm_struct *mm,
  1372. struct vm_area_struct *vma,
  1373. unsigned long from, unsigned long to)
  1374. {
  1375. struct vm_region *region;
  1376. kenter("");
  1377. /* adjust the VMA's pointers, which may reposition it in the MM's tree
  1378. * and list */
  1379. delete_vma_from_mm(vma);
  1380. if (from > vma->vm_start)
  1381. vma->vm_end = from;
  1382. else
  1383. vma->vm_start = to;
  1384. add_vma_to_mm(mm, vma);
  1385. /* cut the backing region down to size */
  1386. region = vma->vm_region;
  1387. BUG_ON(region->vm_usage != 1);
  1388. down_write(&nommu_region_sem);
  1389. delete_nommu_region(region);
  1390. if (from > region->vm_start) {
  1391. to = region->vm_top;
  1392. region->vm_top = region->vm_end = from;
  1393. } else {
  1394. region->vm_start = to;
  1395. }
  1396. add_nommu_region(region);
  1397. up_write(&nommu_region_sem);
  1398. free_page_series(from, to);
  1399. return 0;
  1400. }
  1401. /*
  1402. * release a mapping
  1403. * - under NOMMU conditions the chunk to be unmapped must be backed by a single
  1404. * VMA, though it need not cover the whole VMA
  1405. */
  1406. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
  1407. {
  1408. struct vm_area_struct *vma;
  1409. struct rb_node *rb;
  1410. unsigned long end = start + len;
  1411. int ret;
  1412. kenter(",%lx,%zx", start, len);
  1413. if (len == 0)
  1414. return -EINVAL;
  1415. /* find the first potentially overlapping VMA */
  1416. vma = find_vma(mm, start);
  1417. if (!vma) {
  1418. static int limit = 0;
  1419. if (limit < 5) {
  1420. printk(KERN_WARNING
  1421. "munmap of memory not mmapped by process %d"
  1422. " (%s): 0x%lx-0x%lx\n",
  1423. current->pid, current->comm,
  1424. start, start + len - 1);
  1425. limit++;
  1426. }
  1427. return -EINVAL;
  1428. }
  1429. /* we're allowed to split an anonymous VMA but not a file-backed one */
  1430. if (vma->vm_file) {
  1431. do {
  1432. if (start > vma->vm_start) {
  1433. kleave(" = -EINVAL [miss]");
  1434. return -EINVAL;
  1435. }
  1436. if (end == vma->vm_end)
  1437. goto erase_whole_vma;
  1438. rb = rb_next(&vma->vm_rb);
  1439. vma = rb_entry(rb, struct vm_area_struct, vm_rb);
  1440. } while (rb);
  1441. kleave(" = -EINVAL [split file]");
  1442. return -EINVAL;
  1443. } else {
  1444. /* the chunk must be a subset of the VMA found */
  1445. if (start == vma->vm_start && end == vma->vm_end)
  1446. goto erase_whole_vma;
  1447. if (start < vma->vm_start || end > vma->vm_end) {
  1448. kleave(" = -EINVAL [superset]");
  1449. return -EINVAL;
  1450. }
  1451. if (start & ~PAGE_MASK) {
  1452. kleave(" = -EINVAL [unaligned start]");
  1453. return -EINVAL;
  1454. }
  1455. if (end != vma->vm_end && end & ~PAGE_MASK) {
  1456. kleave(" = -EINVAL [unaligned split]");
  1457. return -EINVAL;
  1458. }
  1459. if (start != vma->vm_start && end != vma->vm_end) {
  1460. ret = split_vma(mm, vma, start, 1);
  1461. if (ret < 0) {
  1462. kleave(" = %d [split]", ret);
  1463. return ret;
  1464. }
  1465. }
  1466. return shrink_vma(mm, vma, start, end);
  1467. }
  1468. erase_whole_vma:
  1469. delete_vma_from_mm(vma);
  1470. delete_vma(mm, vma);
  1471. kleave(" = 0");
  1472. return 0;
  1473. }
  1474. EXPORT_SYMBOL(do_munmap);
  1475. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  1476. {
  1477. int ret;
  1478. struct mm_struct *mm = current->mm;
  1479. down_write(&mm->mmap_sem);
  1480. ret = do_munmap(mm, addr, len);
  1481. up_write(&mm->mmap_sem);
  1482. return ret;
  1483. }
  1484. /*
  1485. * release all the mappings made in a process's VM space
  1486. */
  1487. void exit_mmap(struct mm_struct *mm)
  1488. {
  1489. struct vm_area_struct *vma;
  1490. if (!mm)
  1491. return;
  1492. kenter("");
  1493. mm->total_vm = 0;
  1494. while ((vma = mm->mmap)) {
  1495. mm->mmap = vma->vm_next;
  1496. delete_vma_from_mm(vma);
  1497. delete_vma(mm, vma);
  1498. cond_resched();
  1499. }
  1500. kleave("");
  1501. }
  1502. unsigned long do_brk(unsigned long addr, unsigned long len)
  1503. {
  1504. return -ENOMEM;
  1505. }
  1506. /*
  1507. * expand (or shrink) an existing mapping, potentially moving it at the same
  1508. * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  1509. *
  1510. * under NOMMU conditions, we only permit changing a mapping's size, and only
  1511. * as long as it stays within the region allocated by do_mmap_private() and the
  1512. * block is not shareable
  1513. *
  1514. * MREMAP_FIXED is not supported under NOMMU conditions
  1515. */
  1516. unsigned long do_mremap(unsigned long addr,
  1517. unsigned long old_len, unsigned long new_len,
  1518. unsigned long flags, unsigned long new_addr)
  1519. {
  1520. struct vm_area_struct *vma;
  1521. /* insanity checks first */
  1522. if (old_len == 0 || new_len == 0)
  1523. return (unsigned long) -EINVAL;
  1524. if (addr & ~PAGE_MASK)
  1525. return -EINVAL;
  1526. if (flags & MREMAP_FIXED && new_addr != addr)
  1527. return (unsigned long) -EINVAL;
  1528. vma = find_vma_exact(current->mm, addr, old_len);
  1529. if (!vma)
  1530. return (unsigned long) -EINVAL;
  1531. if (vma->vm_end != vma->vm_start + old_len)
  1532. return (unsigned long) -EFAULT;
  1533. if (vma->vm_flags & VM_MAYSHARE)
  1534. return (unsigned long) -EPERM;
  1535. if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
  1536. return (unsigned long) -ENOMEM;
  1537. /* all checks complete - do it */
  1538. vma->vm_end = vma->vm_start + new_len;
  1539. return vma->vm_start;
  1540. }
  1541. EXPORT_SYMBOL(do_mremap);
  1542. SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  1543. unsigned long, new_len, unsigned long, flags,
  1544. unsigned long, new_addr)
  1545. {
  1546. unsigned long ret;
  1547. down_write(&current->mm->mmap_sem);
  1548. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  1549. up_write(&current->mm->mmap_sem);
  1550. return ret;
  1551. }
  1552. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  1553. unsigned int foll_flags)
  1554. {
  1555. return NULL;
  1556. }
  1557. int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  1558. unsigned long to, unsigned long size, pgprot_t prot)
  1559. {
  1560. vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
  1561. return 0;
  1562. }
  1563. EXPORT_SYMBOL(remap_pfn_range);
  1564. int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
  1565. unsigned long pgoff)
  1566. {
  1567. unsigned int size = vma->vm_end - vma->vm_start;
  1568. if (!(vma->vm_flags & VM_USERMAP))
  1569. return -EINVAL;
  1570. vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
  1571. vma->vm_end = vma->vm_start + size;
  1572. return 0;
  1573. }
  1574. EXPORT_SYMBOL(remap_vmalloc_range);
  1575. void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1576. {
  1577. }
  1578. unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
  1579. unsigned long len, unsigned long pgoff, unsigned long flags)
  1580. {
  1581. return -ENOMEM;
  1582. }
  1583. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1584. {
  1585. }
  1586. void unmap_mapping_range(struct address_space *mapping,
  1587. loff_t const holebegin, loff_t const holelen,
  1588. int even_cows)
  1589. {
  1590. }
  1591. EXPORT_SYMBOL(unmap_mapping_range);
  1592. /*
  1593. * Check that a process has enough memory to allocate a new virtual
  1594. * mapping. 0 means there is enough memory for the allocation to
  1595. * succeed and -ENOMEM implies there is not.
  1596. *
  1597. * We currently support three overcommit policies, which are set via the
  1598. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  1599. *
  1600. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  1601. * Additional code 2002 Jul 20 by Robert Love.
  1602. *
  1603. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  1604. *
  1605. * Note this is a helper function intended to be used by LSMs which
  1606. * wish to use this logic.
  1607. */
  1608. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  1609. {
  1610. unsigned long free, allowed;
  1611. vm_acct_memory(pages);
  1612. /*
  1613. * Sometimes we want to use more memory than we have
  1614. */
  1615. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  1616. return 0;
  1617. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  1618. unsigned long n;
  1619. free = global_page_state(NR_FILE_PAGES);
  1620. free += nr_swap_pages;
  1621. /*
  1622. * Any slabs which are created with the
  1623. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  1624. * which are reclaimable, under pressure. The dentry
  1625. * cache and most inode caches should fall into this
  1626. */
  1627. free += global_page_state(NR_SLAB_RECLAIMABLE);
  1628. /*
  1629. * Leave the last 3% for root
  1630. */
  1631. if (!cap_sys_admin)
  1632. free -= free / 32;
  1633. if (free > pages)
  1634. return 0;
  1635. /*
  1636. * nr_free_pages() is very expensive on large systems,
  1637. * only call if we're about to fail.
  1638. */
  1639. n = nr_free_pages();
  1640. /*
  1641. * Leave reserved pages. The pages are not for anonymous pages.
  1642. */
  1643. if (n <= totalreserve_pages)
  1644. goto error;
  1645. else
  1646. n -= totalreserve_pages;
  1647. /*
  1648. * Leave the last 3% for root
  1649. */
  1650. if (!cap_sys_admin)
  1651. n -= n / 32;
  1652. free += n;
  1653. if (free > pages)
  1654. return 0;
  1655. goto error;
  1656. }
  1657. allowed = totalram_pages * sysctl_overcommit_ratio / 100;
  1658. /*
  1659. * Leave the last 3% for root
  1660. */
  1661. if (!cap_sys_admin)
  1662. allowed -= allowed / 32;
  1663. allowed += total_swap_pages;
  1664. /* Don't let a single process grow too big:
  1665. leave 3% of the size of this process for other processes */
  1666. if (mm)
  1667. allowed -= mm->total_vm / 32;
  1668. if (percpu_counter_read_positive(&vm_committed_as) < allowed)
  1669. return 0;
  1670. error:
  1671. vm_unacct_memory(pages);
  1672. return -ENOMEM;
  1673. }
  1674. int in_gate_area_no_task(unsigned long addr)
  1675. {
  1676. return 0;
  1677. }
  1678. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1679. {
  1680. BUG();
  1681. return 0;
  1682. }
  1683. EXPORT_SYMBOL(filemap_fault);
  1684. /*
  1685. * Access another process' address space.
  1686. * - source/target buffer must be kernel space
  1687. */
  1688. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  1689. {
  1690. struct vm_area_struct *vma;
  1691. struct mm_struct *mm;
  1692. if (addr + len < addr)
  1693. return 0;
  1694. mm = get_task_mm(tsk);
  1695. if (!mm)
  1696. return 0;
  1697. down_read(&mm->mmap_sem);
  1698. /* the access must start within one of the target process's mappings */
  1699. vma = find_vma(mm, addr);
  1700. if (vma) {
  1701. /* don't overrun this mapping */
  1702. if (addr + len >= vma->vm_end)
  1703. len = vma->vm_end - addr;
  1704. /* only read or write mappings where it is permitted */
  1705. if (write && vma->vm_flags & VM_MAYWRITE)
  1706. copy_to_user_page(vma, NULL, addr,
  1707. (void *) addr, buf, len);
  1708. else if (!write && vma->vm_flags & VM_MAYREAD)
  1709. copy_from_user_page(vma, NULL, addr,
  1710. buf, (void *) addr, len);
  1711. else
  1712. len = 0;
  1713. } else {
  1714. len = 0;
  1715. }
  1716. up_read(&mm->mmap_sem);
  1717. mmput(mm);
  1718. return len;
  1719. }
  1720. /**
  1721. * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
  1722. * @inode: The inode to check
  1723. * @size: The current filesize of the inode
  1724. * @newsize: The proposed filesize of the inode
  1725. *
  1726. * Check the shared mappings on an inode on behalf of a shrinking truncate to
  1727. * make sure that that any outstanding VMAs aren't broken and then shrink the
  1728. * vm_regions that extend that beyond so that do_mmap_pgoff() doesn't
  1729. * automatically grant mappings that are too large.
  1730. */
  1731. int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
  1732. size_t newsize)
  1733. {
  1734. struct vm_area_struct *vma;
  1735. struct prio_tree_iter iter;
  1736. struct vm_region *region;
  1737. pgoff_t low, high;
  1738. size_t r_size, r_top;
  1739. low = newsize >> PAGE_SHIFT;
  1740. high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1741. down_write(&nommu_region_sem);
  1742. /* search for VMAs that fall within the dead zone */
  1743. vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
  1744. low, high) {
  1745. /* found one - only interested if it's shared out of the page
  1746. * cache */
  1747. if (vma->vm_flags & VM_SHARED) {
  1748. up_write(&nommu_region_sem);
  1749. return -ETXTBSY; /* not quite true, but near enough */
  1750. }
  1751. }
  1752. /* reduce any regions that overlap the dead zone - if in existence,
  1753. * these will be pointed to by VMAs that don't overlap the dead zone
  1754. *
  1755. * we don't check for any regions that start beyond the EOF as there
  1756. * shouldn't be any
  1757. */
  1758. vma_prio_tree_foreach(vma, &iter, &inode->i_mapping->i_mmap,
  1759. 0, ULONG_MAX) {
  1760. if (!(vma->vm_flags & VM_SHARED))
  1761. continue;
  1762. region = vma->vm_region;
  1763. r_size = region->vm_top - region->vm_start;
  1764. r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
  1765. if (r_top > newsize) {
  1766. region->vm_top -= r_top - newsize;
  1767. if (region->vm_end > region->vm_top)
  1768. region->vm_end = region->vm_top;
  1769. }
  1770. }
  1771. up_write(&nommu_region_sem);
  1772. return 0;
  1773. }