nommu.c 44 KB

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