nommu.c 51 KB

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