nommu.c 48 KB

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