nommu.c 49 KB

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