mmap.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mm/mmap.c
  4. *
  5. * Written by obz.
  6. *
  7. * Address space accounting code <alan@lxorguk.ukuu.org.uk>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/backing-dev.h>
  13. #include <linux/mm.h>
  14. #include <linux/mm_inline.h>
  15. #include <linux/shm.h>
  16. #include <linux/mman.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/swap.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/capability.h>
  21. #include <linux/init.h>
  22. #include <linux/file.h>
  23. #include <linux/fs.h>
  24. #include <linux/personality.h>
  25. #include <linux/security.h>
  26. #include <linux/hugetlb.h>
  27. #include <linux/shmem_fs.h>
  28. #include <linux/profile.h>
  29. #include <linux/export.h>
  30. #include <linux/mount.h>
  31. #include <linux/mempolicy.h>
  32. #include <linux/rmap.h>
  33. #include <linux/mmu_notifier.h>
  34. #include <linux/mmdebug.h>
  35. #include <linux/perf_event.h>
  36. #include <linux/audit.h>
  37. #include <linux/khugepaged.h>
  38. #include <linux/uprobes.h>
  39. #include <linux/notifier.h>
  40. #include <linux/memory.h>
  41. #include <linux/printk.h>
  42. #include <linux/userfaultfd_k.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/pkeys.h>
  45. #include <linux/oom.h>
  46. #include <linux/sched/mm.h>
  47. #include <linux/ksm.h>
  48. #include <linux/memfd.h>
  49. #include <linux/uaccess.h>
  50. #include <asm/cacheflush.h>
  51. #include <asm/tlb.h>
  52. #include <asm/mmu_context.h>
  53. #define CREATE_TRACE_POINTS
  54. #include <trace/events/mmap.h>
  55. #include "internal.h"
  56. #ifndef arch_mmap_check
  57. #define arch_mmap_check(addr, len, flags) (0)
  58. #endif
  59. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
  60. const int mmap_rnd_bits_min = CONFIG_ARCH_MMAP_RND_BITS_MIN;
  61. int mmap_rnd_bits_max __ro_after_init = CONFIG_ARCH_MMAP_RND_BITS_MAX;
  62. int mmap_rnd_bits __read_mostly = CONFIG_ARCH_MMAP_RND_BITS;
  63. #endif
  64. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
  65. const int mmap_rnd_compat_bits_min = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN;
  66. const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
  67. int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
  68. #endif
  69. static bool ignore_rlimit_data;
  70. core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644);
  71. /* Update vma->vm_page_prot to reflect vma->vm_flags. */
  72. void vma_set_page_prot(struct vm_area_struct *vma)
  73. {
  74. vm_flags_t vm_flags = vma->vm_flags;
  75. pgprot_t vm_page_prot;
  76. vm_page_prot = vm_pgprot_modify(vma->vm_page_prot, vm_flags);
  77. if (vma_wants_writenotify(vma, vm_page_prot)) {
  78. vm_flags &= ~VM_SHARED;
  79. vm_page_prot = vm_pgprot_modify(vm_page_prot, vm_flags);
  80. }
  81. /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */
  82. WRITE_ONCE(vma->vm_page_prot, vm_page_prot);
  83. }
  84. /*
  85. * check_brk_limits() - Use platform specific check of range & verify mlock
  86. * limits.
  87. * @addr: The address to check
  88. * @len: The size of increase.
  89. *
  90. * Return: 0 on success.
  91. */
  92. static int check_brk_limits(unsigned long addr, unsigned long len)
  93. {
  94. unsigned long mapped_addr;
  95. mapped_addr = get_unmapped_area(NULL, addr, len, 0, MAP_FIXED);
  96. if (IS_ERR_VALUE(mapped_addr))
  97. return mapped_addr;
  98. return mlock_future_ok(current->mm,
  99. current->mm->def_flags & VM_LOCKED, len)
  100. ? 0 : -EAGAIN;
  101. }
  102. SYSCALL_DEFINE1(brk, unsigned long, brk)
  103. {
  104. unsigned long newbrk, oldbrk, origbrk;
  105. struct mm_struct *mm = current->mm;
  106. struct vm_area_struct *brkvma, *next = NULL;
  107. unsigned long min_brk;
  108. bool populate = false;
  109. LIST_HEAD(uf);
  110. struct vma_iterator vmi;
  111. if (mmap_write_lock_killable(mm))
  112. return -EINTR;
  113. origbrk = mm->brk;
  114. min_brk = mm->start_brk;
  115. #ifdef CONFIG_COMPAT_BRK
  116. /*
  117. * CONFIG_COMPAT_BRK can still be overridden by setting
  118. * randomize_va_space to 2, which will still cause mm->start_brk
  119. * to be arbitrarily shifted
  120. */
  121. if (!current->brk_randomized)
  122. min_brk = mm->end_data;
  123. #endif
  124. if (brk < min_brk)
  125. goto out;
  126. /*
  127. * Check against rlimit here. If this check is done later after the test
  128. * of oldbrk with newbrk then it can escape the test and let the data
  129. * segment grow beyond its set limit the in case where the limit is
  130. * not page aligned -Ram Gupta
  131. */
  132. if (check_data_rlimit(rlimit(RLIMIT_DATA), brk, mm->start_brk,
  133. mm->end_data, mm->start_data))
  134. goto out;
  135. newbrk = PAGE_ALIGN(brk);
  136. oldbrk = PAGE_ALIGN(mm->brk);
  137. if (oldbrk == newbrk) {
  138. mm->brk = brk;
  139. goto success;
  140. }
  141. /* Always allow shrinking brk. */
  142. if (brk <= mm->brk) {
  143. /* Search one past newbrk */
  144. vma_iter_init(&vmi, mm, newbrk);
  145. brkvma = vma_find(&vmi, oldbrk);
  146. if (!brkvma || brkvma->vm_start >= oldbrk)
  147. goto out; /* mapping intersects with an existing non-brk vma. */
  148. /*
  149. * mm->brk must be protected by write mmap_lock.
  150. * do_vmi_align_munmap() will drop the lock on success, so
  151. * update it before calling do_vma_munmap().
  152. */
  153. mm->brk = brk;
  154. if (do_vmi_align_munmap(&vmi, brkvma, mm, newbrk, oldbrk, &uf,
  155. /* unlock = */ true))
  156. goto out;
  157. goto success_unlocked;
  158. }
  159. if (check_brk_limits(oldbrk, newbrk - oldbrk))
  160. goto out;
  161. /*
  162. * Only check if the next VMA is within the stack_guard_gap of the
  163. * expansion area
  164. */
  165. vma_iter_init(&vmi, mm, oldbrk);
  166. next = vma_find(&vmi, newbrk + PAGE_SIZE + stack_guard_gap);
  167. if (next && newbrk + PAGE_SIZE > vm_start_gap(next))
  168. goto out;
  169. brkvma = vma_prev_limit(&vmi, mm->start_brk);
  170. /* Ok, looks good - let it rip. */
  171. if (do_brk_flags(&vmi, brkvma, oldbrk, newbrk - oldbrk, 0) < 0)
  172. goto out;
  173. mm->brk = brk;
  174. if (mm->def_flags & VM_LOCKED)
  175. populate = true;
  176. success:
  177. mmap_write_unlock(mm);
  178. success_unlocked:
  179. userfaultfd_unmap_complete(mm, &uf);
  180. if (populate)
  181. mm_populate(oldbrk, newbrk - oldbrk);
  182. return brk;
  183. out:
  184. mm->brk = origbrk;
  185. mmap_write_unlock(mm);
  186. return origbrk;
  187. }
  188. /*
  189. * If a hint addr is less than mmap_min_addr change hint to be as
  190. * low as possible but still greater than mmap_min_addr
  191. */
  192. static inline unsigned long round_hint_to_min(unsigned long hint)
  193. {
  194. hint &= PAGE_MASK;
  195. if (((void *)hint != NULL) &&
  196. (hint < mmap_min_addr))
  197. return PAGE_ALIGN(mmap_min_addr);
  198. return hint;
  199. }
  200. bool mlock_future_ok(const struct mm_struct *mm, bool is_vma_locked,
  201. unsigned long bytes)
  202. {
  203. unsigned long locked_pages, limit_pages;
  204. if (!is_vma_locked || capable(CAP_IPC_LOCK))
  205. return true;
  206. locked_pages = bytes >> PAGE_SHIFT;
  207. locked_pages += mm->locked_vm;
  208. limit_pages = rlimit(RLIMIT_MEMLOCK);
  209. limit_pages >>= PAGE_SHIFT;
  210. return locked_pages <= limit_pages;
  211. }
  212. static inline u64 file_mmap_size_max(struct file *file, struct inode *inode)
  213. {
  214. if (S_ISREG(inode->i_mode))
  215. return MAX_LFS_FILESIZE;
  216. if (S_ISBLK(inode->i_mode))
  217. return MAX_LFS_FILESIZE;
  218. if (S_ISSOCK(inode->i_mode))
  219. return MAX_LFS_FILESIZE;
  220. /* Special "we do even unsigned file positions" case */
  221. if (file->f_op->fop_flags & FOP_UNSIGNED_OFFSET)
  222. return 0;
  223. /* Yes, random drivers might want more. But I'm tired of buggy drivers */
  224. return ULONG_MAX;
  225. }
  226. static inline bool file_mmap_ok(struct file *file, struct inode *inode,
  227. unsigned long pgoff, unsigned long len)
  228. {
  229. u64 maxsize = file_mmap_size_max(file, inode);
  230. if (maxsize && len > maxsize)
  231. return false;
  232. maxsize -= len;
  233. if (pgoff > maxsize >> PAGE_SHIFT)
  234. return false;
  235. return true;
  236. }
  237. /**
  238. * do_mmap() - Perform a userland memory mapping into the current process
  239. * address space of length @len with protection bits @prot, mmap flags @flags
  240. * (from which VMA flags will be inferred), and any additional VMA flags to
  241. * apply @vm_flags. If this is a file-backed mapping then the file is specified
  242. * in @file and page offset into the file via @pgoff.
  243. *
  244. * This function does not perform security checks on the file and assumes, if
  245. * @uf is non-NULL, the caller has provided a list head to track unmap events
  246. * for userfaultfd @uf.
  247. *
  248. * It also simply indicates whether memory population is required by setting
  249. * @populate, which must be non-NULL, expecting the caller to actually perform
  250. * this task itself if appropriate.
  251. *
  252. * This function will invoke architecture-specific (and if provided and
  253. * relevant, file system-specific) logic to determine the most appropriate
  254. * unmapped area in which to place the mapping if not MAP_FIXED.
  255. *
  256. * Callers which require userland mmap() behaviour should invoke vm_mmap(),
  257. * which is also exported for module use.
  258. *
  259. * Those which require this behaviour less security checks, userfaultfd and
  260. * populate behaviour, and who handle the mmap write lock themselves, should
  261. * call this function.
  262. *
  263. * Note that the returned address may reside within a merged VMA if an
  264. * appropriate merge were to take place, so it doesn't necessarily specify the
  265. * start of a VMA, rather only the start of a valid mapped range of length
  266. * @len bytes, rounded down to the nearest page size.
  267. *
  268. * The caller must write-lock current->mm->mmap_lock.
  269. *
  270. * @file: An optional struct file pointer describing the file which is to be
  271. * mapped, if a file-backed mapping.
  272. * @addr: If non-zero, hints at (or if @flags has MAP_FIXED set, specifies) the
  273. * address at which to perform this mapping. See mmap (2) for details. Must be
  274. * page-aligned.
  275. * @len: The length of the mapping. Will be page-aligned and must be at least 1
  276. * page in size.
  277. * @prot: Protection bits describing access required to the mapping. See mmap
  278. * (2) for details.
  279. * @flags: Flags specifying how the mapping should be performed, see mmap (2)
  280. * for details.
  281. * @vm_flags: VMA flags which should be set by default, or 0 otherwise.
  282. * @pgoff: Page offset into the @file if file-backed, should be 0 otherwise.
  283. * @populate: A pointer to a value which will be set to 0 if no population of
  284. * the range is required, or the number of bytes to populate if it is. Must be
  285. * non-NULL. See mmap (2) for details as to under what circumstances population
  286. * of the range occurs.
  287. * @uf: An optional pointer to a list head to track userfaultfd unmap events
  288. * should unmapping events arise. If provided, it is up to the caller to manage
  289. * this.
  290. *
  291. * Returns: Either an error, or the address at which the requested mapping has
  292. * been performed.
  293. */
  294. unsigned long do_mmap(struct file *file, unsigned long addr,
  295. unsigned long len, unsigned long prot,
  296. unsigned long flags, vm_flags_t vm_flags,
  297. unsigned long pgoff, unsigned long *populate,
  298. struct list_head *uf)
  299. {
  300. struct mm_struct *mm = current->mm;
  301. int pkey = 0;
  302. *populate = 0;
  303. mmap_assert_write_locked(mm);
  304. if (!len)
  305. return -EINVAL;
  306. /*
  307. * Does the application expect PROT_READ to imply PROT_EXEC?
  308. *
  309. * (the exception is when the underlying filesystem is noexec
  310. * mounted, in which case we don't add PROT_EXEC.)
  311. */
  312. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  313. if (!(file && path_noexec(&file->f_path)))
  314. prot |= PROT_EXEC;
  315. /* force arch specific MAP_FIXED handling in get_unmapped_area */
  316. if (flags & MAP_FIXED_NOREPLACE)
  317. flags |= MAP_FIXED;
  318. if (!(flags & MAP_FIXED))
  319. addr = round_hint_to_min(addr);
  320. /* Careful about overflows.. */
  321. len = PAGE_ALIGN(len);
  322. if (!len)
  323. return -ENOMEM;
  324. /* offset overflow? */
  325. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  326. return -EOVERFLOW;
  327. /* Too many mappings? */
  328. if (mm->map_count > sysctl_max_map_count)
  329. return -ENOMEM;
  330. /*
  331. * addr is returned from get_unmapped_area,
  332. * There are two cases:
  333. * 1> MAP_FIXED == false
  334. * unallocated memory, no need to check sealing.
  335. * 1> MAP_FIXED == true
  336. * sealing is checked inside mmap_region when
  337. * do_vmi_munmap is called.
  338. */
  339. if (prot == PROT_EXEC) {
  340. pkey = execute_only_pkey(mm);
  341. if (pkey < 0)
  342. pkey = 0;
  343. }
  344. /* Do simple checking here so the lower-level routines won't have
  345. * to. we assume access permissions have been handled by the open
  346. * of the memory object, so we don't do any here.
  347. */
  348. vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(file, flags) |
  349. mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  350. /* Obtain the address to map to. we verify (or select) it and ensure
  351. * that it represents a valid section of the address space.
  352. */
  353. addr = __get_unmapped_area(file, addr, len, pgoff, flags, vm_flags);
  354. if (IS_ERR_VALUE(addr))
  355. return addr;
  356. if (flags & MAP_FIXED_NOREPLACE) {
  357. if (find_vma_intersection(mm, addr, addr + len))
  358. return -EEXIST;
  359. }
  360. if (flags & MAP_LOCKED)
  361. if (!can_do_mlock())
  362. return -EPERM;
  363. if (!mlock_future_ok(mm, vm_flags & VM_LOCKED, len))
  364. return -EAGAIN;
  365. if (file) {
  366. struct inode *inode = file_inode(file);
  367. unsigned long flags_mask;
  368. int err;
  369. if (!file_mmap_ok(file, inode, pgoff, len))
  370. return -EOVERFLOW;
  371. flags_mask = LEGACY_MAP_MASK;
  372. if (file->f_op->fop_flags & FOP_MMAP_SYNC)
  373. flags_mask |= MAP_SYNC;
  374. switch (flags & MAP_TYPE) {
  375. case MAP_SHARED:
  376. /*
  377. * Force use of MAP_SHARED_VALIDATE with non-legacy
  378. * flags. E.g. MAP_SYNC is dangerous to use with
  379. * MAP_SHARED as you don't know which consistency model
  380. * you will get. We silently ignore unsupported flags
  381. * with MAP_SHARED to preserve backward compatibility.
  382. */
  383. flags &= LEGACY_MAP_MASK;
  384. fallthrough;
  385. case MAP_SHARED_VALIDATE:
  386. if (flags & ~flags_mask)
  387. return -EOPNOTSUPP;
  388. if (prot & PROT_WRITE) {
  389. if (!(file->f_mode & FMODE_WRITE))
  390. return -EACCES;
  391. if (IS_SWAPFILE(file->f_mapping->host))
  392. return -ETXTBSY;
  393. }
  394. /*
  395. * Make sure we don't allow writing to an append-only
  396. * file..
  397. */
  398. if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
  399. return -EACCES;
  400. vm_flags |= VM_SHARED | VM_MAYSHARE;
  401. if (!(file->f_mode & FMODE_WRITE))
  402. vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
  403. fallthrough;
  404. case MAP_PRIVATE:
  405. if (!(file->f_mode & FMODE_READ))
  406. return -EACCES;
  407. if (path_noexec(&file->f_path)) {
  408. if (vm_flags & VM_EXEC)
  409. return -EPERM;
  410. vm_flags &= ~VM_MAYEXEC;
  411. }
  412. if (!can_mmap_file(file))
  413. return -ENODEV;
  414. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  415. return -EINVAL;
  416. break;
  417. default:
  418. return -EINVAL;
  419. }
  420. /*
  421. * Check to see if we are violating any seals and update VMA
  422. * flags if necessary to avoid future seal violations.
  423. */
  424. err = memfd_check_seals_mmap(file, &vm_flags);
  425. if (err)
  426. return (unsigned long)err;
  427. } else {
  428. switch (flags & MAP_TYPE) {
  429. case MAP_SHARED:
  430. if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
  431. return -EINVAL;
  432. /*
  433. * Ignore pgoff.
  434. */
  435. pgoff = 0;
  436. vm_flags |= VM_SHARED | VM_MAYSHARE;
  437. break;
  438. case MAP_DROPPABLE:
  439. if (VM_DROPPABLE == VM_NONE)
  440. return -ENOTSUPP;
  441. /*
  442. * A locked or stack area makes no sense to be droppable.
  443. *
  444. * Also, since droppable pages can just go away at any time
  445. * it makes no sense to copy them on fork or dump them.
  446. *
  447. * And don't attempt to combine with hugetlb for now.
  448. */
  449. if (flags & (MAP_LOCKED | MAP_HUGETLB))
  450. return -EINVAL;
  451. if (vm_flags & (VM_GROWSDOWN | VM_GROWSUP))
  452. return -EINVAL;
  453. vm_flags |= VM_DROPPABLE;
  454. /*
  455. * If the pages can be dropped, then it doesn't make
  456. * sense to reserve them.
  457. */
  458. vm_flags |= VM_NORESERVE;
  459. /*
  460. * Likewise, they're volatile enough that they
  461. * shouldn't survive forks or coredumps.
  462. */
  463. vm_flags |= VM_WIPEONFORK | VM_DONTDUMP;
  464. fallthrough;
  465. case MAP_PRIVATE:
  466. /*
  467. * Set pgoff according to addr for anon_vma.
  468. */
  469. pgoff = addr >> PAGE_SHIFT;
  470. break;
  471. default:
  472. return -EINVAL;
  473. }
  474. }
  475. /*
  476. * Set 'VM_NORESERVE' if we should not account for the
  477. * memory use of this mapping.
  478. */
  479. if (flags & MAP_NORESERVE) {
  480. /* We honor MAP_NORESERVE if allowed to overcommit */
  481. if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  482. vm_flags |= VM_NORESERVE;
  483. /* hugetlb applies strict overcommit unless MAP_NORESERVE */
  484. if (file && is_file_hugepages(file))
  485. vm_flags |= VM_NORESERVE;
  486. }
  487. addr = mmap_region(file, addr, len, vm_flags, pgoff, uf);
  488. if (!IS_ERR_VALUE(addr) &&
  489. ((vm_flags & VM_LOCKED) ||
  490. (flags & (MAP_POPULATE | MAP_NONBLOCK)) == MAP_POPULATE))
  491. *populate = len;
  492. return addr;
  493. }
  494. unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
  495. unsigned long prot, unsigned long flags,
  496. unsigned long fd, unsigned long pgoff)
  497. {
  498. struct file *file = NULL;
  499. unsigned long retval;
  500. if (!(flags & MAP_ANONYMOUS)) {
  501. audit_mmap_fd(fd, flags);
  502. file = fget(fd);
  503. if (!file)
  504. return -EBADF;
  505. if (is_file_hugepages(file)) {
  506. len = ALIGN(len, huge_page_size(hstate_file(file)));
  507. } else if (unlikely(flags & MAP_HUGETLB)) {
  508. retval = -EINVAL;
  509. goto out_fput;
  510. }
  511. } else if (flags & MAP_HUGETLB) {
  512. struct hstate *hs;
  513. hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
  514. if (!hs)
  515. return -EINVAL;
  516. len = ALIGN(len, huge_page_size(hs));
  517. /*
  518. * VM_NORESERVE is used because the reservations will be
  519. * taken when vm_ops->mmap() is called
  520. */
  521. file = hugetlb_file_setup(HUGETLB_ANON_FILE, len,
  522. mk_vma_flags(VMA_NORESERVE_BIT),
  523. HUGETLB_ANONHUGE_INODE,
  524. (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK);
  525. if (IS_ERR(file))
  526. return PTR_ERR(file);
  527. }
  528. retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
  529. out_fput:
  530. if (file)
  531. fput(file);
  532. return retval;
  533. }
  534. SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
  535. unsigned long, prot, unsigned long, flags,
  536. unsigned long, fd, unsigned long, pgoff)
  537. {
  538. return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
  539. }
  540. #ifdef __ARCH_WANT_SYS_OLD_MMAP
  541. struct mmap_arg_struct {
  542. unsigned long addr;
  543. unsigned long len;
  544. unsigned long prot;
  545. unsigned long flags;
  546. unsigned long fd;
  547. unsigned long offset;
  548. };
  549. SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
  550. {
  551. struct mmap_arg_struct a;
  552. if (copy_from_user(&a, arg, sizeof(a)))
  553. return -EFAULT;
  554. if (offset_in_page(a.offset))
  555. return -EINVAL;
  556. return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
  557. a.offset >> PAGE_SHIFT);
  558. }
  559. #endif /* __ARCH_WANT_SYS_OLD_MMAP */
  560. /*
  561. * Determine if the allocation needs to ensure that there is no
  562. * existing mapping within it's guard gaps, for use as start_gap.
  563. */
  564. static inline unsigned long stack_guard_placement(vm_flags_t vm_flags)
  565. {
  566. if (vm_flags & VM_SHADOW_STACK)
  567. return PAGE_SIZE;
  568. return 0;
  569. }
  570. /*
  571. * Search for an unmapped address range.
  572. *
  573. * We are looking for a range that:
  574. * - does not intersect with any VMA;
  575. * - is contained within the [low_limit, high_limit) interval;
  576. * - is at least the desired size.
  577. * - satisfies (begin_addr & align_mask) == (align_offset & align_mask)
  578. */
  579. unsigned long vm_unmapped_area(struct vm_unmapped_area_info *info)
  580. {
  581. unsigned long addr;
  582. if (info->flags & VM_UNMAPPED_AREA_TOPDOWN)
  583. addr = unmapped_area_topdown(info);
  584. else
  585. addr = unmapped_area(info);
  586. trace_vm_unmapped_area(addr, info);
  587. return addr;
  588. }
  589. /* Get an address range which is currently unmapped.
  590. * For shmat() with addr=0.
  591. *
  592. * Ugly calling convention alert:
  593. * Return value with the low bits set means error value,
  594. * ie
  595. * if (ret & ~PAGE_MASK)
  596. * error = ret;
  597. *
  598. * This function "knows" that -ENOMEM has the bits set.
  599. */
  600. unsigned long
  601. generic_get_unmapped_area(struct file *filp, unsigned long addr,
  602. unsigned long len, unsigned long pgoff,
  603. unsigned long flags, vm_flags_t vm_flags)
  604. {
  605. struct mm_struct *mm = current->mm;
  606. struct vm_area_struct *vma, *prev;
  607. struct vm_unmapped_area_info info = {};
  608. const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);
  609. if (len > mmap_end - mmap_min_addr)
  610. return -ENOMEM;
  611. if (flags & MAP_FIXED)
  612. return addr;
  613. if (addr) {
  614. addr = PAGE_ALIGN(addr);
  615. vma = find_vma_prev(mm, addr, &prev);
  616. if (mmap_end - len >= addr && addr >= mmap_min_addr &&
  617. (!vma || addr + len <= vm_start_gap(vma)) &&
  618. (!prev || addr >= vm_end_gap(prev)))
  619. return addr;
  620. }
  621. info.length = len;
  622. info.low_limit = mm->mmap_base;
  623. info.high_limit = mmap_end;
  624. info.start_gap = stack_guard_placement(vm_flags);
  625. if (filp && is_file_hugepages(filp))
  626. info.align_mask = huge_page_mask_align(filp);
  627. return vm_unmapped_area(&info);
  628. }
  629. #ifndef HAVE_ARCH_UNMAPPED_AREA
  630. unsigned long
  631. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  632. unsigned long len, unsigned long pgoff,
  633. unsigned long flags, vm_flags_t vm_flags)
  634. {
  635. return generic_get_unmapped_area(filp, addr, len, pgoff, flags,
  636. vm_flags);
  637. }
  638. #endif
  639. /*
  640. * This mmap-allocator allocates new areas top-down from below the
  641. * stack's low limit (the base):
  642. */
  643. unsigned long
  644. generic_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
  645. unsigned long len, unsigned long pgoff,
  646. unsigned long flags, vm_flags_t vm_flags)
  647. {
  648. struct vm_area_struct *vma, *prev;
  649. struct mm_struct *mm = current->mm;
  650. struct vm_unmapped_area_info info = {};
  651. const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags);
  652. /* requested length too big for entire address space */
  653. if (len > mmap_end - mmap_min_addr)
  654. return -ENOMEM;
  655. if (flags & MAP_FIXED)
  656. return addr;
  657. /* requesting a specific address */
  658. if (addr) {
  659. addr = PAGE_ALIGN(addr);
  660. vma = find_vma_prev(mm, addr, &prev);
  661. if (mmap_end - len >= addr && addr >= mmap_min_addr &&
  662. (!vma || addr + len <= vm_start_gap(vma)) &&
  663. (!prev || addr >= vm_end_gap(prev)))
  664. return addr;
  665. }
  666. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  667. info.length = len;
  668. info.low_limit = PAGE_SIZE;
  669. info.high_limit = arch_get_mmap_base(addr, mm->mmap_base);
  670. info.start_gap = stack_guard_placement(vm_flags);
  671. if (filp && is_file_hugepages(filp))
  672. info.align_mask = huge_page_mask_align(filp);
  673. addr = vm_unmapped_area(&info);
  674. /*
  675. * A failed mmap() very likely causes application failure,
  676. * so fall back to the bottom-up function here. This scenario
  677. * can happen with large stack limits and large mmap()
  678. * allocations.
  679. */
  680. if (offset_in_page(addr)) {
  681. VM_BUG_ON(addr != -ENOMEM);
  682. info.flags = 0;
  683. info.low_limit = TASK_UNMAPPED_BASE;
  684. info.high_limit = mmap_end;
  685. addr = vm_unmapped_area(&info);
  686. }
  687. return addr;
  688. }
  689. #ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
  690. unsigned long
  691. arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr,
  692. unsigned long len, unsigned long pgoff,
  693. unsigned long flags, vm_flags_t vm_flags)
  694. {
  695. return generic_get_unmapped_area_topdown(filp, addr, len, pgoff, flags,
  696. vm_flags);
  697. }
  698. #endif
  699. unsigned long mm_get_unmapped_area_vmflags(struct file *filp, unsigned long addr,
  700. unsigned long len, unsigned long pgoff,
  701. unsigned long flags, vm_flags_t vm_flags)
  702. {
  703. if (mm_flags_test(MMF_TOPDOWN, current->mm))
  704. return arch_get_unmapped_area_topdown(filp, addr, len, pgoff,
  705. flags, vm_flags);
  706. return arch_get_unmapped_area(filp, addr, len, pgoff, flags, vm_flags);
  707. }
  708. unsigned long
  709. __get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  710. unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags)
  711. {
  712. unsigned long (*get_area)(struct file *, unsigned long,
  713. unsigned long, unsigned long, unsigned long)
  714. = NULL;
  715. unsigned long error = arch_mmap_check(addr, len, flags);
  716. if (error)
  717. return error;
  718. /* Careful about overflows.. */
  719. if (len > TASK_SIZE)
  720. return -ENOMEM;
  721. if (file) {
  722. if (file->f_op->get_unmapped_area)
  723. get_area = file->f_op->get_unmapped_area;
  724. } else if (flags & MAP_SHARED) {
  725. /*
  726. * mmap_region() will call shmem_zero_setup() to create a file,
  727. * so use shmem's get_unmapped_area in case it can be huge.
  728. */
  729. get_area = shmem_get_unmapped_area;
  730. }
  731. /* Always treat pgoff as zero for anonymous memory. */
  732. if (!file)
  733. pgoff = 0;
  734. if (get_area) {
  735. addr = get_area(file, addr, len, pgoff, flags);
  736. } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && !file
  737. && !addr /* no hint */
  738. && IS_ALIGNED(len, PMD_SIZE)) {
  739. /* Ensures that larger anonymous mappings are THP aligned. */
  740. addr = thp_get_unmapped_area_vmflags(file, addr, len,
  741. pgoff, flags, vm_flags);
  742. } else {
  743. addr = mm_get_unmapped_area_vmflags(file, addr, len,
  744. pgoff, flags, vm_flags);
  745. }
  746. if (IS_ERR_VALUE(addr))
  747. return addr;
  748. if (addr > TASK_SIZE - len)
  749. return -ENOMEM;
  750. if (offset_in_page(addr))
  751. return -EINVAL;
  752. error = security_mmap_addr(addr);
  753. return error ? error : addr;
  754. }
  755. unsigned long
  756. mm_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
  757. unsigned long pgoff, unsigned long flags)
  758. {
  759. return mm_get_unmapped_area_vmflags(file, addr, len, pgoff, flags, 0);
  760. }
  761. EXPORT_SYMBOL(mm_get_unmapped_area);
  762. /**
  763. * find_vma_intersection() - Look up the first VMA which intersects the interval
  764. * @mm: The process address space.
  765. * @start_addr: The inclusive start user address.
  766. * @end_addr: The exclusive end user address.
  767. *
  768. * Returns: The first VMA within the provided range, %NULL otherwise. Assumes
  769. * start_addr < end_addr.
  770. */
  771. struct vm_area_struct *find_vma_intersection(struct mm_struct *mm,
  772. unsigned long start_addr,
  773. unsigned long end_addr)
  774. {
  775. unsigned long index = start_addr;
  776. mmap_assert_locked(mm);
  777. return mt_find(&mm->mm_mt, &index, end_addr - 1);
  778. }
  779. EXPORT_SYMBOL(find_vma_intersection);
  780. /**
  781. * find_vma() - Find the VMA for a given address, or the next VMA.
  782. * @mm: The mm_struct to check
  783. * @addr: The address
  784. *
  785. * Returns: The VMA associated with addr, or the next VMA.
  786. * May return %NULL in the case of no VMA at addr or above.
  787. */
  788. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  789. {
  790. unsigned long index = addr;
  791. mmap_assert_locked(mm);
  792. return mt_find(&mm->mm_mt, &index, ULONG_MAX);
  793. }
  794. EXPORT_SYMBOL(find_vma);
  795. /**
  796. * find_vma_prev() - Find the VMA for a given address, or the next vma and
  797. * set %pprev to the previous VMA, if any.
  798. * @mm: The mm_struct to check
  799. * @addr: The address
  800. * @pprev: The pointer to set to the previous VMA
  801. *
  802. * Note that RCU lock is missing here since the external mmap_lock() is used
  803. * instead.
  804. *
  805. * Returns: The VMA associated with @addr, or the next vma.
  806. * May return %NULL in the case of no vma at addr or above.
  807. */
  808. struct vm_area_struct *
  809. find_vma_prev(struct mm_struct *mm, unsigned long addr,
  810. struct vm_area_struct **pprev)
  811. {
  812. struct vm_area_struct *vma;
  813. VMA_ITERATOR(vmi, mm, addr);
  814. vma = vma_iter_load(&vmi);
  815. *pprev = vma_prev(&vmi);
  816. if (!vma)
  817. vma = vma_next(&vmi);
  818. return vma;
  819. }
  820. /* enforced gap between the expanding stack and other mappings. */
  821. unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
  822. static int __init cmdline_parse_stack_guard_gap(char *p)
  823. {
  824. unsigned long val;
  825. char *endptr;
  826. val = simple_strtoul(p, &endptr, 10);
  827. if (!*endptr)
  828. stack_guard_gap = val << PAGE_SHIFT;
  829. return 1;
  830. }
  831. __setup("stack_guard_gap=", cmdline_parse_stack_guard_gap);
  832. #ifdef CONFIG_STACK_GROWSUP
  833. int expand_stack_locked(struct vm_area_struct *vma, unsigned long address)
  834. {
  835. return expand_upwards(vma, address);
  836. }
  837. struct vm_area_struct *find_extend_vma_locked(struct mm_struct *mm, unsigned long addr)
  838. {
  839. struct vm_area_struct *vma, *prev;
  840. addr &= PAGE_MASK;
  841. vma = find_vma_prev(mm, addr, &prev);
  842. if (vma && (vma->vm_start <= addr))
  843. return vma;
  844. if (!prev)
  845. return NULL;
  846. if (expand_stack_locked(prev, addr))
  847. return NULL;
  848. if (prev->vm_flags & VM_LOCKED)
  849. populate_vma_page_range(prev, addr, prev->vm_end, NULL);
  850. return prev;
  851. }
  852. #else
  853. int expand_stack_locked(struct vm_area_struct *vma, unsigned long address)
  854. {
  855. return expand_downwards(vma, address);
  856. }
  857. struct vm_area_struct *find_extend_vma_locked(struct mm_struct *mm, unsigned long addr)
  858. {
  859. struct vm_area_struct *vma;
  860. unsigned long start;
  861. addr &= PAGE_MASK;
  862. vma = find_vma(mm, addr);
  863. if (!vma)
  864. return NULL;
  865. if (vma->vm_start <= addr)
  866. return vma;
  867. start = vma->vm_start;
  868. if (expand_stack_locked(vma, addr))
  869. return NULL;
  870. if (vma->vm_flags & VM_LOCKED)
  871. populate_vma_page_range(vma, addr, start, NULL);
  872. return vma;
  873. }
  874. #endif
  875. #if defined(CONFIG_STACK_GROWSUP)
  876. #define vma_expand_up(vma,addr) expand_upwards(vma, addr)
  877. #define vma_expand_down(vma, addr) (-EFAULT)
  878. #else
  879. #define vma_expand_up(vma,addr) (-EFAULT)
  880. #define vma_expand_down(vma, addr) expand_downwards(vma, addr)
  881. #endif
  882. /*
  883. * expand_stack(): legacy interface for page faulting. Don't use unless
  884. * you have to.
  885. *
  886. * This is called with the mm locked for reading, drops the lock, takes
  887. * the lock for writing, tries to look up a vma again, expands it if
  888. * necessary, and downgrades the lock to reading again.
  889. *
  890. * If no vma is found or it can't be expanded, it returns NULL and has
  891. * dropped the lock.
  892. */
  893. struct vm_area_struct *expand_stack(struct mm_struct *mm, unsigned long addr)
  894. {
  895. struct vm_area_struct *vma, *prev;
  896. mmap_read_unlock(mm);
  897. if (mmap_write_lock_killable(mm))
  898. return NULL;
  899. vma = find_vma_prev(mm, addr, &prev);
  900. if (vma && vma->vm_start <= addr)
  901. goto success;
  902. if (prev && !vma_expand_up(prev, addr)) {
  903. vma = prev;
  904. goto success;
  905. }
  906. if (vma && !vma_expand_down(vma, addr))
  907. goto success;
  908. mmap_write_unlock(mm);
  909. return NULL;
  910. success:
  911. mmap_write_downgrade(mm);
  912. return vma;
  913. }
  914. /* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls.
  915. * @mm: The mm_struct
  916. * @start: The start address to munmap
  917. * @len: The length to be munmapped.
  918. * @uf: The userfaultfd list_head
  919. *
  920. * Return: 0 on success, error otherwise.
  921. */
  922. int do_munmap(struct mm_struct *mm, unsigned long start, size_t len,
  923. struct list_head *uf)
  924. {
  925. VMA_ITERATOR(vmi, mm, start);
  926. return do_vmi_munmap(&vmi, mm, start, len, uf, false);
  927. }
  928. int vm_munmap(unsigned long start, size_t len)
  929. {
  930. return __vm_munmap(start, len, false);
  931. }
  932. EXPORT_SYMBOL(vm_munmap);
  933. SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
  934. {
  935. addr = untagged_addr(addr);
  936. return __vm_munmap(addr, len, true);
  937. }
  938. /*
  939. * Emulation of deprecated remap_file_pages() syscall.
  940. */
  941. SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
  942. unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
  943. {
  944. struct mm_struct *mm = current->mm;
  945. struct vm_area_struct *vma;
  946. unsigned long populate = 0;
  947. unsigned long ret = -EINVAL;
  948. struct file *file;
  949. vm_flags_t vm_flags;
  950. pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
  951. current->comm, current->pid);
  952. if (prot)
  953. return ret;
  954. start = start & PAGE_MASK;
  955. size = size & PAGE_MASK;
  956. if (start + size <= start)
  957. return ret;
  958. /* Does pgoff wrap? */
  959. if (pgoff + (size >> PAGE_SHIFT) < pgoff)
  960. return ret;
  961. if (mmap_read_lock_killable(mm))
  962. return -EINTR;
  963. /*
  964. * Look up VMA under read lock first so we can perform the security
  965. * without holding locks (which can be problematic). We reacquire a
  966. * write lock later and check nothing changed underneath us.
  967. */
  968. vma = vma_lookup(mm, start);
  969. if (!vma || !(vma->vm_flags & VM_SHARED)) {
  970. mmap_read_unlock(mm);
  971. return -EINVAL;
  972. }
  973. prot |= vma->vm_flags & VM_READ ? PROT_READ : 0;
  974. prot |= vma->vm_flags & VM_WRITE ? PROT_WRITE : 0;
  975. prot |= vma->vm_flags & VM_EXEC ? PROT_EXEC : 0;
  976. flags &= MAP_NONBLOCK;
  977. flags |= MAP_SHARED | MAP_FIXED | MAP_POPULATE;
  978. if (vma->vm_flags & VM_LOCKED)
  979. flags |= MAP_LOCKED;
  980. /* Save vm_flags used to calculate prot and flags, and recheck later. */
  981. vm_flags = vma->vm_flags;
  982. file = get_file(vma->vm_file);
  983. mmap_read_unlock(mm);
  984. /* Call outside mmap_lock to be consistent with other callers. */
  985. ret = security_mmap_file(file, prot, flags);
  986. if (ret) {
  987. fput(file);
  988. return ret;
  989. }
  990. ret = -EINVAL;
  991. /* OK security check passed, take write lock + let it rip. */
  992. if (mmap_write_lock_killable(mm)) {
  993. fput(file);
  994. return -EINTR;
  995. }
  996. vma = vma_lookup(mm, start);
  997. if (!vma)
  998. goto out;
  999. /* Make sure things didn't change under us. */
  1000. if (vma->vm_flags != vm_flags)
  1001. goto out;
  1002. if (vma->vm_file != file)
  1003. goto out;
  1004. if (start + size > vma->vm_end) {
  1005. VMA_ITERATOR(vmi, mm, vma->vm_end);
  1006. struct vm_area_struct *next, *prev = vma;
  1007. for_each_vma_range(vmi, next, start + size) {
  1008. /* hole between vmas ? */
  1009. if (next->vm_start != prev->vm_end)
  1010. goto out;
  1011. if (next->vm_file != vma->vm_file)
  1012. goto out;
  1013. if (next->vm_flags != vma->vm_flags)
  1014. goto out;
  1015. if (start + size <= next->vm_end)
  1016. break;
  1017. prev = next;
  1018. }
  1019. if (!next)
  1020. goto out;
  1021. }
  1022. ret = do_mmap(vma->vm_file, start, size,
  1023. prot, flags, 0, pgoff, &populate, NULL);
  1024. out:
  1025. mmap_write_unlock(mm);
  1026. fput(file);
  1027. if (populate)
  1028. mm_populate(ret, populate);
  1029. if (!IS_ERR_VALUE(ret))
  1030. ret = 0;
  1031. return ret;
  1032. }
  1033. int vm_brk_flags(unsigned long addr, unsigned long request, vm_flags_t vm_flags)
  1034. {
  1035. struct mm_struct *mm = current->mm;
  1036. struct vm_area_struct *vma = NULL;
  1037. unsigned long len;
  1038. int ret;
  1039. bool populate;
  1040. LIST_HEAD(uf);
  1041. VMA_ITERATOR(vmi, mm, addr);
  1042. len = PAGE_ALIGN(request);
  1043. if (len < request)
  1044. return -ENOMEM;
  1045. if (!len)
  1046. return 0;
  1047. /* Until we need other flags, refuse anything except VM_EXEC. */
  1048. if ((vm_flags & (~VM_EXEC)) != 0)
  1049. return -EINVAL;
  1050. if (mmap_write_lock_killable(mm))
  1051. return -EINTR;
  1052. ret = check_brk_limits(addr, len);
  1053. if (ret)
  1054. goto limits_failed;
  1055. ret = do_vmi_munmap(&vmi, mm, addr, len, &uf, 0);
  1056. if (ret)
  1057. goto munmap_failed;
  1058. vma = vma_prev(&vmi);
  1059. ret = do_brk_flags(&vmi, vma, addr, len, vm_flags);
  1060. populate = ((mm->def_flags & VM_LOCKED) != 0);
  1061. mmap_write_unlock(mm);
  1062. userfaultfd_unmap_complete(mm, &uf);
  1063. if (populate && !ret)
  1064. mm_populate(addr, len);
  1065. return ret;
  1066. munmap_failed:
  1067. limits_failed:
  1068. mmap_write_unlock(mm);
  1069. return ret;
  1070. }
  1071. EXPORT_SYMBOL(vm_brk_flags);
  1072. static
  1073. unsigned long tear_down_vmas(struct mm_struct *mm, struct vma_iterator *vmi,
  1074. struct vm_area_struct *vma, unsigned long end)
  1075. {
  1076. unsigned long nr_accounted = 0;
  1077. int count = 0;
  1078. mmap_assert_write_locked(mm);
  1079. vma_iter_set(vmi, vma->vm_end);
  1080. do {
  1081. if (vma->vm_flags & VM_ACCOUNT)
  1082. nr_accounted += vma_pages(vma);
  1083. vma_mark_detached(vma);
  1084. remove_vma(vma);
  1085. count++;
  1086. cond_resched();
  1087. vma = vma_next(vmi);
  1088. } while (vma && vma->vm_end <= end);
  1089. VM_WARN_ON_ONCE(count != mm->map_count);
  1090. return nr_accounted;
  1091. }
  1092. /* Release all mmaps. */
  1093. void exit_mmap(struct mm_struct *mm)
  1094. {
  1095. struct mmu_gather tlb;
  1096. struct vm_area_struct *vma;
  1097. unsigned long nr_accounted = 0;
  1098. VMA_ITERATOR(vmi, mm, 0);
  1099. struct unmap_desc unmap;
  1100. /* mm's last user has gone, and its about to be pulled down */
  1101. mmu_notifier_release(mm);
  1102. mmap_read_lock(mm);
  1103. arch_exit_mmap(mm);
  1104. vma = vma_next(&vmi);
  1105. if (!vma) {
  1106. /* Can happen if dup_mmap() received an OOM */
  1107. mmap_read_unlock(mm);
  1108. mmap_write_lock(mm);
  1109. goto destroy;
  1110. }
  1111. unmap_all_init(&unmap, &vmi, vma);
  1112. flush_cache_mm(mm);
  1113. tlb_gather_mmu_fullmm(&tlb, mm);
  1114. /* update_hiwater_rss(mm) here? but nobody should be looking */
  1115. /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */
  1116. unmap_vmas(&tlb, &unmap);
  1117. mmap_read_unlock(mm);
  1118. /*
  1119. * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper
  1120. * because the memory has been already freed.
  1121. */
  1122. mm_flags_set(MMF_OOM_SKIP, mm);
  1123. mmap_write_lock(mm);
  1124. unmap.mm_wr_locked = true;
  1125. mt_clear_in_rcu(&mm->mm_mt);
  1126. unmap_pgtable_init(&unmap, &vmi);
  1127. free_pgtables(&tlb, &unmap);
  1128. tlb_finish_mmu(&tlb);
  1129. /*
  1130. * Walk the list again, actually closing and freeing it, with preemption
  1131. * enabled, without holding any MM locks besides the unreachable
  1132. * mmap_write_lock.
  1133. */
  1134. nr_accounted = tear_down_vmas(mm, &vmi, vma, ULONG_MAX);
  1135. destroy:
  1136. __mt_destroy(&mm->mm_mt);
  1137. trace_exit_mmap(mm);
  1138. mmap_write_unlock(mm);
  1139. vm_unacct_memory(nr_accounted);
  1140. }
  1141. /*
  1142. * Return true if the calling process may expand its vm space by the passed
  1143. * number of pages
  1144. */
  1145. bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
  1146. {
  1147. if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
  1148. return false;
  1149. if (is_data_mapping(flags) &&
  1150. mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
  1151. /* Workaround for Valgrind */
  1152. if (rlimit(RLIMIT_DATA) == 0 &&
  1153. mm->data_vm + npages <= rlimit_max(RLIMIT_DATA) >> PAGE_SHIFT)
  1154. return true;
  1155. pr_warn_once("%s (%d): VmData %lu exceed data ulimit %lu. Update limits%s.\n",
  1156. current->comm, current->pid,
  1157. (mm->data_vm + npages) << PAGE_SHIFT,
  1158. rlimit(RLIMIT_DATA),
  1159. ignore_rlimit_data ? "" : " or use boot option ignore_rlimit_data");
  1160. if (!ignore_rlimit_data)
  1161. return false;
  1162. }
  1163. return true;
  1164. }
  1165. void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
  1166. {
  1167. WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm)+npages);
  1168. if (is_exec_mapping(flags))
  1169. mm->exec_vm += npages;
  1170. else if (is_stack_mapping(flags))
  1171. mm->stack_vm += npages;
  1172. else if (is_data_mapping(flags))
  1173. mm->data_vm += npages;
  1174. }
  1175. static vm_fault_t special_mapping_fault(struct vm_fault *vmf);
  1176. /*
  1177. * Close hook, called for unmap() and on the old vma for mremap().
  1178. *
  1179. * Having a close hook prevents vma merging regardless of flags.
  1180. */
  1181. static void special_mapping_close(struct vm_area_struct *vma)
  1182. {
  1183. const struct vm_special_mapping *sm = vma->vm_private_data;
  1184. if (sm->close)
  1185. sm->close(sm, vma);
  1186. }
  1187. static const char *special_mapping_name(struct vm_area_struct *vma)
  1188. {
  1189. return ((struct vm_special_mapping *)vma->vm_private_data)->name;
  1190. }
  1191. static int special_mapping_mremap(struct vm_area_struct *new_vma)
  1192. {
  1193. struct vm_special_mapping *sm = new_vma->vm_private_data;
  1194. if (WARN_ON_ONCE(current->mm != new_vma->vm_mm))
  1195. return -EFAULT;
  1196. if (sm->mremap)
  1197. return sm->mremap(sm, new_vma);
  1198. return 0;
  1199. }
  1200. static int special_mapping_split(struct vm_area_struct *vma, unsigned long addr)
  1201. {
  1202. /*
  1203. * Forbid splitting special mappings - kernel has expectations over
  1204. * the number of pages in mapping. Together with VM_DONTEXPAND
  1205. * the size of vma should stay the same over the special mapping's
  1206. * lifetime.
  1207. */
  1208. return -EINVAL;
  1209. }
  1210. static const struct vm_operations_struct special_mapping_vmops = {
  1211. .close = special_mapping_close,
  1212. .fault = special_mapping_fault,
  1213. .mremap = special_mapping_mremap,
  1214. .name = special_mapping_name,
  1215. /* vDSO code relies that VVAR can't be accessed remotely */
  1216. .access = NULL,
  1217. .may_split = special_mapping_split,
  1218. };
  1219. static vm_fault_t special_mapping_fault(struct vm_fault *vmf)
  1220. {
  1221. struct vm_area_struct *vma = vmf->vma;
  1222. pgoff_t pgoff;
  1223. struct page **pages;
  1224. struct vm_special_mapping *sm = vma->vm_private_data;
  1225. if (sm->fault)
  1226. return sm->fault(sm, vmf->vma, vmf);
  1227. pages = sm->pages;
  1228. for (pgoff = vmf->pgoff; pgoff && *pages; ++pages)
  1229. pgoff--;
  1230. if (*pages) {
  1231. struct page *page = *pages;
  1232. get_page(page);
  1233. vmf->page = page;
  1234. return 0;
  1235. }
  1236. return VM_FAULT_SIGBUS;
  1237. }
  1238. static struct vm_area_struct *__install_special_mapping(
  1239. struct mm_struct *mm,
  1240. unsigned long addr, unsigned long len,
  1241. vm_flags_t vm_flags, void *priv,
  1242. const struct vm_operations_struct *ops)
  1243. {
  1244. int ret;
  1245. struct vm_area_struct *vma;
  1246. vma = vm_area_alloc(mm);
  1247. if (unlikely(vma == NULL))
  1248. return ERR_PTR(-ENOMEM);
  1249. vma_set_range(vma, addr, addr + len, 0);
  1250. vm_flags |= mm->def_flags | VM_DONTEXPAND;
  1251. if (pgtable_supports_soft_dirty())
  1252. vm_flags |= VM_SOFTDIRTY;
  1253. vm_flags_init(vma, vm_flags & ~VM_LOCKED_MASK);
  1254. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  1255. vma->vm_ops = ops;
  1256. vma->vm_private_data = priv;
  1257. ret = insert_vm_struct(mm, vma);
  1258. if (ret)
  1259. goto out;
  1260. vm_stat_account(mm, vma->vm_flags, len >> PAGE_SHIFT);
  1261. perf_event_mmap(vma);
  1262. return vma;
  1263. out:
  1264. vm_area_free(vma);
  1265. return ERR_PTR(ret);
  1266. }
  1267. bool vma_is_special_mapping(const struct vm_area_struct *vma,
  1268. const struct vm_special_mapping *sm)
  1269. {
  1270. return vma->vm_private_data == sm &&
  1271. vma->vm_ops == &special_mapping_vmops;
  1272. }
  1273. /*
  1274. * Called with mm->mmap_lock held for writing.
  1275. * Insert a new vma covering the given region, with the given flags.
  1276. * Its pages are supplied by the given array of struct page *.
  1277. * The array can be shorter than len >> PAGE_SHIFT if it's null-terminated.
  1278. * The region past the last page supplied will always produce SIGBUS.
  1279. * The array pointer and the pages it points to are assumed to stay alive
  1280. * for as long as this mapping might exist.
  1281. */
  1282. struct vm_area_struct *_install_special_mapping(
  1283. struct mm_struct *mm,
  1284. unsigned long addr, unsigned long len,
  1285. vm_flags_t vm_flags, const struct vm_special_mapping *spec)
  1286. {
  1287. return __install_special_mapping(mm, addr, len, vm_flags, (void *)spec,
  1288. &special_mapping_vmops);
  1289. }
  1290. #ifdef CONFIG_SYSCTL
  1291. #if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \
  1292. defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT)
  1293. int sysctl_legacy_va_layout;
  1294. #endif
  1295. static const struct ctl_table mmap_table[] = {
  1296. {
  1297. .procname = "max_map_count",
  1298. .data = &sysctl_max_map_count,
  1299. .maxlen = sizeof(sysctl_max_map_count),
  1300. .mode = 0644,
  1301. .proc_handler = proc_dointvec_minmax,
  1302. .extra1 = SYSCTL_ZERO,
  1303. },
  1304. #if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \
  1305. defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT)
  1306. {
  1307. .procname = "legacy_va_layout",
  1308. .data = &sysctl_legacy_va_layout,
  1309. .maxlen = sizeof(sysctl_legacy_va_layout),
  1310. .mode = 0644,
  1311. .proc_handler = proc_dointvec_minmax,
  1312. .extra1 = SYSCTL_ZERO,
  1313. },
  1314. #endif
  1315. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_BITS
  1316. {
  1317. .procname = "mmap_rnd_bits",
  1318. .data = &mmap_rnd_bits,
  1319. .maxlen = sizeof(mmap_rnd_bits),
  1320. .mode = 0600,
  1321. .proc_handler = proc_dointvec_minmax,
  1322. .extra1 = (void *)&mmap_rnd_bits_min,
  1323. .extra2 = (void *)&mmap_rnd_bits_max,
  1324. },
  1325. #endif
  1326. #ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
  1327. {
  1328. .procname = "mmap_rnd_compat_bits",
  1329. .data = &mmap_rnd_compat_bits,
  1330. .maxlen = sizeof(mmap_rnd_compat_bits),
  1331. .mode = 0600,
  1332. .proc_handler = proc_dointvec_minmax,
  1333. .extra1 = (void *)&mmap_rnd_compat_bits_min,
  1334. .extra2 = (void *)&mmap_rnd_compat_bits_max,
  1335. },
  1336. #endif
  1337. };
  1338. #endif /* CONFIG_SYSCTL */
  1339. /*
  1340. * initialise the percpu counter for VM, initialise VMA state.
  1341. */
  1342. void __init mmap_init(void)
  1343. {
  1344. int ret;
  1345. ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
  1346. VM_BUG_ON(ret);
  1347. #ifdef CONFIG_SYSCTL
  1348. register_sysctl_init("vm", mmap_table);
  1349. #endif
  1350. vma_state_init();
  1351. }
  1352. /*
  1353. * Initialise sysctl_user_reserve_kbytes.
  1354. *
  1355. * This is intended to prevent a user from starting a single memory hogging
  1356. * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
  1357. * mode.
  1358. *
  1359. * The default value is min(3% of free memory, 128MB)
  1360. * 128MB is enough to recover with sshd/login, bash, and top/kill.
  1361. */
  1362. static int init_user_reserve(void)
  1363. {
  1364. unsigned long free_kbytes;
  1365. free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
  1366. sysctl_user_reserve_kbytes = min(free_kbytes / 32, SZ_128K);
  1367. return 0;
  1368. }
  1369. subsys_initcall(init_user_reserve);
  1370. /*
  1371. * Initialise sysctl_admin_reserve_kbytes.
  1372. *
  1373. * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
  1374. * to log in and kill a memory hogging process.
  1375. *
  1376. * Systems with more than 256MB will reserve 8MB, enough to recover
  1377. * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
  1378. * only reserve 3% of free pages by default.
  1379. */
  1380. static int init_admin_reserve(void)
  1381. {
  1382. unsigned long free_kbytes;
  1383. free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
  1384. sysctl_admin_reserve_kbytes = min(free_kbytes / 32, SZ_8K);
  1385. return 0;
  1386. }
  1387. subsys_initcall(init_admin_reserve);
  1388. /*
  1389. * Reinititalise user and admin reserves if memory is added or removed.
  1390. *
  1391. * The default user reserve max is 128MB, and the default max for the
  1392. * admin reserve is 8MB. These are usually, but not always, enough to
  1393. * enable recovery from a memory hogging process using login/sshd, a shell,
  1394. * and tools like top. It may make sense to increase or even disable the
  1395. * reserve depending on the existence of swap or variations in the recovery
  1396. * tools. So, the admin may have changed them.
  1397. *
  1398. * If memory is added and the reserves have been eliminated or increased above
  1399. * the default max, then we'll trust the admin.
  1400. *
  1401. * If memory is removed and there isn't enough free memory, then we
  1402. * need to reset the reserves.
  1403. *
  1404. * Otherwise keep the reserve set by the admin.
  1405. */
  1406. static int reserve_mem_notifier(struct notifier_block *nb,
  1407. unsigned long action, void *data)
  1408. {
  1409. unsigned long tmp, free_kbytes;
  1410. switch (action) {
  1411. case MEM_ONLINE:
  1412. /* Default max is 128MB. Leave alone if modified by operator. */
  1413. tmp = sysctl_user_reserve_kbytes;
  1414. if (tmp > 0 && tmp < SZ_128K)
  1415. init_user_reserve();
  1416. /* Default max is 8MB. Leave alone if modified by operator. */
  1417. tmp = sysctl_admin_reserve_kbytes;
  1418. if (tmp > 0 && tmp < SZ_8K)
  1419. init_admin_reserve();
  1420. break;
  1421. case MEM_OFFLINE:
  1422. free_kbytes = K(global_zone_page_state(NR_FREE_PAGES));
  1423. if (sysctl_user_reserve_kbytes > free_kbytes) {
  1424. init_user_reserve();
  1425. pr_info("vm.user_reserve_kbytes reset to %lu\n",
  1426. sysctl_user_reserve_kbytes);
  1427. }
  1428. if (sysctl_admin_reserve_kbytes > free_kbytes) {
  1429. init_admin_reserve();
  1430. pr_info("vm.admin_reserve_kbytes reset to %lu\n",
  1431. sysctl_admin_reserve_kbytes);
  1432. }
  1433. break;
  1434. default:
  1435. break;
  1436. }
  1437. return NOTIFY_OK;
  1438. }
  1439. static int __meminit init_reserve_notifier(void)
  1440. {
  1441. if (hotplug_memory_notifier(reserve_mem_notifier, DEFAULT_CALLBACK_PRI))
  1442. pr_err("Failed registering memory add/remove notifier for admin reserve\n");
  1443. return 0;
  1444. }
  1445. subsys_initcall(init_reserve_notifier);
  1446. /*
  1447. * Obtain a read lock on mm->mmap_lock, if the specified address is below the
  1448. * start of the VMA, the intent is to perform a write, and it is a
  1449. * downward-growing stack, then attempt to expand the stack to contain it.
  1450. *
  1451. * This function is intended only for obtaining an argument page from an ELF
  1452. * image, and is almost certainly NOT what you want to use for any other
  1453. * purpose.
  1454. *
  1455. * IMPORTANT - VMA fields are accessed without an mmap lock being held, so the
  1456. * VMA referenced must not be linked in any user-visible tree, i.e. it must be a
  1457. * new VMA being mapped.
  1458. *
  1459. * The function assumes that addr is either contained within the VMA or below
  1460. * it, and makes no attempt to validate this value beyond that.
  1461. *
  1462. * Returns true if the read lock was obtained and a stack was perhaps expanded,
  1463. * false if the stack expansion failed.
  1464. *
  1465. * On stack expansion the function temporarily acquires an mmap write lock
  1466. * before downgrading it.
  1467. */
  1468. bool mmap_read_lock_maybe_expand(struct mm_struct *mm,
  1469. struct vm_area_struct *new_vma,
  1470. unsigned long addr, bool write)
  1471. {
  1472. if (!write || addr >= new_vma->vm_start) {
  1473. mmap_read_lock(mm);
  1474. return true;
  1475. }
  1476. if (!(new_vma->vm_flags & VM_GROWSDOWN))
  1477. return false;
  1478. mmap_write_lock(mm);
  1479. if (expand_downwards(new_vma, addr)) {
  1480. mmap_write_unlock(mm);
  1481. return false;
  1482. }
  1483. mmap_write_downgrade(mm);
  1484. return true;
  1485. }
  1486. __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
  1487. {
  1488. struct vm_area_struct *mpnt, *tmp;
  1489. int retval;
  1490. unsigned long charge = 0;
  1491. LIST_HEAD(uf);
  1492. VMA_ITERATOR(vmi, mm, 0);
  1493. if (mmap_write_lock_killable(oldmm))
  1494. return -EINTR;
  1495. flush_cache_dup_mm(oldmm);
  1496. uprobe_dup_mmap(oldmm, mm);
  1497. /*
  1498. * Not linked in yet - no deadlock potential:
  1499. */
  1500. mmap_write_lock_nested(mm, SINGLE_DEPTH_NESTING);
  1501. /* No ordering required: file already has been exposed. */
  1502. dup_mm_exe_file(mm, oldmm);
  1503. mm->total_vm = oldmm->total_vm;
  1504. mm->data_vm = oldmm->data_vm;
  1505. mm->exec_vm = oldmm->exec_vm;
  1506. mm->stack_vm = oldmm->stack_vm;
  1507. /* Use __mt_dup() to efficiently build an identical maple tree. */
  1508. retval = __mt_dup(&oldmm->mm_mt, &mm->mm_mt, GFP_KERNEL);
  1509. if (unlikely(retval))
  1510. goto out;
  1511. mt_clear_in_rcu(vmi.mas.tree);
  1512. for_each_vma(vmi, mpnt) {
  1513. struct file *file;
  1514. retval = vma_start_write_killable(mpnt);
  1515. if (retval < 0)
  1516. goto loop_out;
  1517. if (mpnt->vm_flags & VM_DONTCOPY) {
  1518. retval = vma_iter_clear_gfp(&vmi, mpnt->vm_start,
  1519. mpnt->vm_end, GFP_KERNEL);
  1520. if (retval)
  1521. goto loop_out;
  1522. vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt));
  1523. continue;
  1524. }
  1525. charge = 0;
  1526. if (mpnt->vm_flags & VM_ACCOUNT) {
  1527. unsigned long len = vma_pages(mpnt);
  1528. if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
  1529. goto fail_nomem;
  1530. charge = len;
  1531. }
  1532. tmp = vm_area_dup(mpnt);
  1533. if (!tmp)
  1534. goto fail_nomem;
  1535. retval = vma_dup_policy(mpnt, tmp);
  1536. if (retval)
  1537. goto fail_nomem_policy;
  1538. tmp->vm_mm = mm;
  1539. retval = dup_userfaultfd(tmp, &uf);
  1540. if (retval)
  1541. goto fail_nomem_anon_vma_fork;
  1542. if (tmp->vm_flags & VM_WIPEONFORK) {
  1543. /*
  1544. * VM_WIPEONFORK gets a clean slate in the child.
  1545. * Don't prepare anon_vma until fault since we don't
  1546. * copy page for current vma.
  1547. */
  1548. tmp->anon_vma = NULL;
  1549. } else if (anon_vma_fork(tmp, mpnt))
  1550. goto fail_nomem_anon_vma_fork;
  1551. vm_flags_clear(tmp, VM_LOCKED_MASK);
  1552. /*
  1553. * Copy/update hugetlb private vma information.
  1554. */
  1555. if (is_vm_hugetlb_page(tmp))
  1556. hugetlb_dup_vma_private(tmp);
  1557. /*
  1558. * Link the vma into the MT. After using __mt_dup(), memory
  1559. * allocation is not necessary here, so it cannot fail.
  1560. */
  1561. vma_iter_bulk_store(&vmi, tmp);
  1562. mm->map_count++;
  1563. if (tmp->vm_ops && tmp->vm_ops->open)
  1564. tmp->vm_ops->open(tmp);
  1565. file = tmp->vm_file;
  1566. if (file) {
  1567. struct address_space *mapping = file->f_mapping;
  1568. get_file(file);
  1569. i_mmap_lock_write(mapping);
  1570. if (vma_is_shared_maywrite(tmp))
  1571. mapping_allow_writable(mapping);
  1572. flush_dcache_mmap_lock(mapping);
  1573. /* insert tmp into the share list, just after mpnt */
  1574. vma_interval_tree_insert_after(tmp, mpnt,
  1575. &mapping->i_mmap);
  1576. flush_dcache_mmap_unlock(mapping);
  1577. i_mmap_unlock_write(mapping);
  1578. }
  1579. if (!(tmp->vm_flags & VM_WIPEONFORK))
  1580. retval = copy_page_range(tmp, mpnt);
  1581. if (retval) {
  1582. mpnt = vma_next(&vmi);
  1583. goto loop_out;
  1584. }
  1585. }
  1586. /* a new mm has just been created */
  1587. retval = arch_dup_mmap(oldmm, mm);
  1588. loop_out:
  1589. vma_iter_free(&vmi);
  1590. if (!retval) {
  1591. mt_set_in_rcu(vmi.mas.tree);
  1592. ksm_fork(mm, oldmm);
  1593. khugepaged_fork(mm, oldmm);
  1594. } else {
  1595. unsigned long end;
  1596. /*
  1597. * The entire maple tree has already been duplicated, but
  1598. * replacing the vmas failed at mpnt (which could be NULL if
  1599. * all were allocated but the last vma was not fully set up).
  1600. * Use the start address of the failure point to clean up the
  1601. * partially initialized tree.
  1602. */
  1603. if (!mm->map_count) {
  1604. /* zero vmas were written to the new tree. */
  1605. end = 0;
  1606. } else if (mpnt) {
  1607. /* partial tree failure */
  1608. end = mpnt->vm_start;
  1609. } else {
  1610. /* All vmas were written to the new tree */
  1611. end = ULONG_MAX;
  1612. }
  1613. /* Hide mm from oom killer because the memory is being freed */
  1614. mm_flags_set(MMF_OOM_SKIP, mm);
  1615. if (end) {
  1616. vma_iter_set(&vmi, 0);
  1617. tmp = vma_next(&vmi);
  1618. UNMAP_STATE(unmap, &vmi, /* first = */ tmp,
  1619. /* vma_start = */ 0, /* vma_end = */ end,
  1620. /* prev = */ NULL, /* next = */ NULL);
  1621. /*
  1622. * Don't iterate over vmas beyond the failure point for
  1623. * both unmap_vma() and free_pgtables().
  1624. */
  1625. unmap.tree_end = end;
  1626. flush_cache_mm(mm);
  1627. unmap_region(&unmap);
  1628. charge = tear_down_vmas(mm, &vmi, tmp, end);
  1629. vm_unacct_memory(charge);
  1630. }
  1631. __mt_destroy(&mm->mm_mt);
  1632. /*
  1633. * The mm_struct is going to exit, but the locks will be dropped
  1634. * first. Set the mm_struct as unstable is advisable as it is
  1635. * not fully initialised.
  1636. */
  1637. mm_flags_set(MMF_UNSTABLE, mm);
  1638. }
  1639. out:
  1640. mmap_write_unlock(mm);
  1641. flush_tlb_mm(oldmm);
  1642. mmap_write_unlock(oldmm);
  1643. if (!retval)
  1644. dup_userfaultfd_complete(&uf);
  1645. else
  1646. dup_userfaultfd_fail(&uf);
  1647. return retval;
  1648. fail_nomem_anon_vma_fork:
  1649. mpol_put(vma_policy(tmp));
  1650. fail_nomem_policy:
  1651. vm_area_free(tmp);
  1652. fail_nomem:
  1653. retval = -ENOMEM;
  1654. vm_unacct_memory(charge);
  1655. goto loop_out;
  1656. }