nommu.c 51 KB

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