nommu.c 46 KB

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