nommu.c 48 KB

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