mprotect.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * mm/mprotect.c
  3. *
  4. * (C) Copyright 1994 Linus Torvalds
  5. * (C) Copyright 2002 Christoph Hellwig
  6. *
  7. * Address space accounting code <alan@lxorguk.ukuu.org.uk>
  8. * (C) Copyright 2002 Red Hat Inc, All Rights Reserved
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/hugetlb.h>
  12. #include <linux/shm.h>
  13. #include <linux/mman.h>
  14. #include <linux/fs.h>
  15. #include <linux/highmem.h>
  16. #include <linux/security.h>
  17. #include <linux/mempolicy.h>
  18. #include <linux/personality.h>
  19. #include <linux/syscalls.h>
  20. #include <linux/swap.h>
  21. #include <linux/swapops.h>
  22. #include <linux/mmu_notifier.h>
  23. #include <linux/migrate.h>
  24. #include <linux/perf_event.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/tlbflush.h>
  29. #ifndef pgprot_modify
  30. static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
  31. {
  32. return newprot;
  33. }
  34. #endif
  35. static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  36. unsigned long addr, unsigned long end, pgprot_t newprot,
  37. int dirty_accountable, int prot_numa, bool *ret_all_same_node)
  38. {
  39. struct mm_struct *mm = vma->vm_mm;
  40. pte_t *pte, oldpte;
  41. spinlock_t *ptl;
  42. unsigned long pages = 0;
  43. bool all_same_node = true;
  44. int last_nid = -1;
  45. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  46. arch_enter_lazy_mmu_mode();
  47. do {
  48. oldpte = *pte;
  49. if (pte_present(oldpte)) {
  50. pte_t ptent;
  51. bool updated = false;
  52. ptent = ptep_modify_prot_start(mm, addr, pte);
  53. if (!prot_numa) {
  54. ptent = pte_modify(ptent, newprot);
  55. updated = true;
  56. } else {
  57. struct page *page;
  58. page = vm_normal_page(vma, addr, oldpte);
  59. if (page) {
  60. int this_nid = page_to_nid(page);
  61. if (last_nid == -1)
  62. last_nid = this_nid;
  63. if (last_nid != this_nid)
  64. all_same_node = false;
  65. /* only check non-shared pages */
  66. if (!pte_numa(oldpte) &&
  67. page_mapcount(page) == 1) {
  68. ptent = pte_mknuma(ptent);
  69. updated = true;
  70. }
  71. }
  72. }
  73. /*
  74. * Avoid taking write faults for pages we know to be
  75. * dirty.
  76. */
  77. if (dirty_accountable && pte_dirty(ptent)) {
  78. ptent = pte_mkwrite(ptent);
  79. updated = true;
  80. }
  81. if (updated)
  82. pages++;
  83. ptep_modify_prot_commit(mm, addr, pte, ptent);
  84. } else if (IS_ENABLED(CONFIG_MIGRATION) && !pte_file(oldpte)) {
  85. swp_entry_t entry = pte_to_swp_entry(oldpte);
  86. if (is_write_migration_entry(entry)) {
  87. pte_t newpte;
  88. /*
  89. * A protection check is difficult so
  90. * just be safe and disable write
  91. */
  92. make_migration_entry_read(&entry);
  93. newpte = swp_entry_to_pte(entry);
  94. if (pte_swp_soft_dirty(oldpte))
  95. newpte = pte_swp_mksoft_dirty(newpte);
  96. set_pte_at(mm, addr, pte, newpte);
  97. }
  98. pages++;
  99. }
  100. } while (pte++, addr += PAGE_SIZE, addr != end);
  101. arch_leave_lazy_mmu_mode();
  102. pte_unmap_unlock(pte - 1, ptl);
  103. *ret_all_same_node = all_same_node;
  104. return pages;
  105. }
  106. #ifdef CONFIG_NUMA_BALANCING
  107. static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
  108. pmd_t *pmd)
  109. {
  110. spin_lock(&mm->page_table_lock);
  111. set_pmd_at(mm, addr & PMD_MASK, pmd, pmd_mknuma(*pmd));
  112. spin_unlock(&mm->page_table_lock);
  113. }
  114. #else
  115. static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
  116. pmd_t *pmd)
  117. {
  118. BUG();
  119. }
  120. #endif /* CONFIG_NUMA_BALANCING */
  121. static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
  122. pud_t *pud, unsigned long addr, unsigned long end,
  123. pgprot_t newprot, int dirty_accountable, int prot_numa)
  124. {
  125. pmd_t *pmd;
  126. unsigned long next;
  127. unsigned long pages = 0;
  128. bool all_same_node;
  129. pmd = pmd_offset(pud, addr);
  130. do {
  131. next = pmd_addr_end(addr, end);
  132. if (pmd_trans_huge(*pmd)) {
  133. if (next - addr != HPAGE_PMD_SIZE)
  134. split_huge_page_pmd(vma, addr, pmd);
  135. else if (change_huge_pmd(vma, pmd, addr, newprot,
  136. prot_numa)) {
  137. pages += HPAGE_PMD_NR;
  138. continue;
  139. }
  140. /* fall through */
  141. }
  142. if (pmd_none_or_clear_bad(pmd))
  143. continue;
  144. pages += change_pte_range(vma, pmd, addr, next, newprot,
  145. dirty_accountable, prot_numa, &all_same_node);
  146. /*
  147. * If we are changing protections for NUMA hinting faults then
  148. * set pmd_numa if the examined pages were all on the same
  149. * node. This allows a regular PMD to be handled as one fault
  150. * and effectively batches the taking of the PTL
  151. */
  152. if (prot_numa && all_same_node)
  153. change_pmd_protnuma(vma->vm_mm, addr, pmd);
  154. } while (pmd++, addr = next, addr != end);
  155. return pages;
  156. }
  157. static inline unsigned long change_pud_range(struct vm_area_struct *vma,
  158. pgd_t *pgd, unsigned long addr, unsigned long end,
  159. pgprot_t newprot, int dirty_accountable, int prot_numa)
  160. {
  161. pud_t *pud;
  162. unsigned long next;
  163. unsigned long pages = 0;
  164. pud = pud_offset(pgd, addr);
  165. do {
  166. next = pud_addr_end(addr, end);
  167. if (pud_none_or_clear_bad(pud))
  168. continue;
  169. pages += change_pmd_range(vma, pud, addr, next, newprot,
  170. dirty_accountable, prot_numa);
  171. } while (pud++, addr = next, addr != end);
  172. return pages;
  173. }
  174. static unsigned long change_protection_range(struct vm_area_struct *vma,
  175. unsigned long addr, unsigned long end, pgprot_t newprot,
  176. int dirty_accountable, int prot_numa)
  177. {
  178. struct mm_struct *mm = vma->vm_mm;
  179. pgd_t *pgd;
  180. unsigned long next;
  181. unsigned long start = addr;
  182. unsigned long pages = 0;
  183. BUG_ON(addr >= end);
  184. pgd = pgd_offset(mm, addr);
  185. flush_cache_range(vma, addr, end);
  186. do {
  187. next = pgd_addr_end(addr, end);
  188. if (pgd_none_or_clear_bad(pgd))
  189. continue;
  190. pages += change_pud_range(vma, pgd, addr, next, newprot,
  191. dirty_accountable, prot_numa);
  192. } while (pgd++, addr = next, addr != end);
  193. /* Only flush the TLB if we actually modified any entries: */
  194. if (pages)
  195. flush_tlb_range(vma, start, end);
  196. return pages;
  197. }
  198. unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
  199. unsigned long end, pgprot_t newprot,
  200. int dirty_accountable, int prot_numa)
  201. {
  202. struct mm_struct *mm = vma->vm_mm;
  203. unsigned long pages;
  204. mmu_notifier_invalidate_range_start(mm, start, end);
  205. if (is_vm_hugetlb_page(vma))
  206. pages = hugetlb_change_protection(vma, start, end, newprot);
  207. else
  208. pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
  209. mmu_notifier_invalidate_range_end(mm, start, end);
  210. return pages;
  211. }
  212. int
  213. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  214. unsigned long start, unsigned long end, unsigned long newflags)
  215. {
  216. struct mm_struct *mm = vma->vm_mm;
  217. unsigned long oldflags = vma->vm_flags;
  218. long nrpages = (end - start) >> PAGE_SHIFT;
  219. unsigned long charged = 0;
  220. pgoff_t pgoff;
  221. int error;
  222. int dirty_accountable = 0;
  223. if (newflags == oldflags) {
  224. *pprev = vma;
  225. return 0;
  226. }
  227. /*
  228. * If we make a private mapping writable we increase our commit;
  229. * but (without finer accounting) cannot reduce our commit if we
  230. * make it unwritable again. hugetlb mapping were accounted for
  231. * even if read-only so there is no need to account for them here
  232. */
  233. if (newflags & VM_WRITE) {
  234. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  235. VM_SHARED|VM_NORESERVE))) {
  236. charged = nrpages;
  237. if (security_vm_enough_memory_mm(mm, charged))
  238. return -ENOMEM;
  239. newflags |= VM_ACCOUNT;
  240. }
  241. }
  242. /*
  243. * First try to merge with previous and/or next vma.
  244. */
  245. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  246. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  247. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  248. if (*pprev) {
  249. vma = *pprev;
  250. goto success;
  251. }
  252. *pprev = vma;
  253. if (start != vma->vm_start) {
  254. error = split_vma(mm, vma, start, 1);
  255. if (error)
  256. goto fail;
  257. }
  258. if (end != vma->vm_end) {
  259. error = split_vma(mm, vma, end, 0);
  260. if (error)
  261. goto fail;
  262. }
  263. success:
  264. /*
  265. * vm_flags and vm_page_prot are protected by the mmap_sem
  266. * held in write mode.
  267. */
  268. vma->vm_flags = newflags;
  269. vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
  270. vm_get_page_prot(newflags));
  271. if (vma_wants_writenotify(vma)) {
  272. vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
  273. dirty_accountable = 1;
  274. }
  275. change_protection(vma, start, end, vma->vm_page_prot,
  276. dirty_accountable, 0);
  277. vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
  278. vm_stat_account(mm, newflags, vma->vm_file, nrpages);
  279. perf_event_mmap(vma);
  280. return 0;
  281. fail:
  282. vm_unacct_memory(charged);
  283. return error;
  284. }
  285. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  286. unsigned long, prot)
  287. {
  288. unsigned long vm_flags, nstart, end, tmp, reqprot;
  289. struct vm_area_struct *vma, *prev;
  290. int error = -EINVAL;
  291. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  292. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  293. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  294. return -EINVAL;
  295. if (start & ~PAGE_MASK)
  296. return -EINVAL;
  297. if (!len)
  298. return 0;
  299. len = PAGE_ALIGN(len);
  300. end = start + len;
  301. if (end <= start)
  302. return -ENOMEM;
  303. if (!arch_validate_prot(prot))
  304. return -EINVAL;
  305. reqprot = prot;
  306. /*
  307. * Does the application expect PROT_READ to imply PROT_EXEC:
  308. */
  309. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  310. prot |= PROT_EXEC;
  311. vm_flags = calc_vm_prot_bits(prot);
  312. down_write(&current->mm->mmap_sem);
  313. vma = find_vma(current->mm, start);
  314. error = -ENOMEM;
  315. if (!vma)
  316. goto out;
  317. prev = vma->vm_prev;
  318. if (unlikely(grows & PROT_GROWSDOWN)) {
  319. if (vma->vm_start >= end)
  320. goto out;
  321. start = vma->vm_start;
  322. error = -EINVAL;
  323. if (!(vma->vm_flags & VM_GROWSDOWN))
  324. goto out;
  325. } else {
  326. if (vma->vm_start > start)
  327. goto out;
  328. if (unlikely(grows & PROT_GROWSUP)) {
  329. end = vma->vm_end;
  330. error = -EINVAL;
  331. if (!(vma->vm_flags & VM_GROWSUP))
  332. goto out;
  333. }
  334. }
  335. if (start > vma->vm_start)
  336. prev = vma;
  337. for (nstart = start ; ; ) {
  338. unsigned long newflags;
  339. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  340. newflags = vm_flags;
  341. newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
  342. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  343. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  344. error = -EACCES;
  345. goto out;
  346. }
  347. error = security_file_mprotect(vma, reqprot, prot);
  348. if (error)
  349. goto out;
  350. tmp = vma->vm_end;
  351. if (tmp > end)
  352. tmp = end;
  353. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  354. if (error)
  355. goto out;
  356. nstart = tmp;
  357. if (nstart < prev->vm_end)
  358. nstart = prev->vm_end;
  359. if (nstart >= end)
  360. goto out;
  361. vma = prev->vm_next;
  362. if (!vma || vma->vm_start != nstart) {
  363. error = -ENOMEM;
  364. goto out;
  365. }
  366. }
  367. out:
  368. up_write(&current->mm->mmap_sem);
  369. return error;
  370. }