nommu.c 51 KB

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