nommu.c 48 KB

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