nommu.c 52 KB

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