nommu.c 45 KB

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