nommu.c 54 KB

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