mprotect.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. /*
  88. * A protection check is difficult so
  89. * just be safe and disable write
  90. */
  91. make_migration_entry_read(&entry);
  92. set_pte_at(mm, addr, pte,
  93. swp_entry_to_pte(entry));
  94. pages++;
  95. }
  96. }
  97. } while (pte++, addr += PAGE_SIZE, addr != end);
  98. arch_leave_lazy_mmu_mode();
  99. pte_unmap_unlock(pte - 1, ptl);
  100. *ret_all_same_node = all_same_node;
  101. return pages;
  102. }
  103. #ifdef CONFIG_NUMA_BALANCING
  104. static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
  105. pmd_t *pmd)
  106. {
  107. spin_lock(&mm->page_table_lock);
  108. set_pmd_at(mm, addr & PMD_MASK, pmd, pmd_mknuma(*pmd));
  109. spin_unlock(&mm->page_table_lock);
  110. }
  111. #else
  112. static inline void change_pmd_protnuma(struct mm_struct *mm, unsigned long addr,
  113. pmd_t *pmd)
  114. {
  115. BUG();
  116. }
  117. #endif /* CONFIG_NUMA_BALANCING */
  118. static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
  119. pud_t *pud, unsigned long addr, unsigned long end,
  120. pgprot_t newprot, int dirty_accountable, int prot_numa)
  121. {
  122. pmd_t *pmd;
  123. unsigned long next;
  124. unsigned long pages = 0;
  125. bool all_same_node;
  126. pmd = pmd_offset(pud, addr);
  127. do {
  128. next = pmd_addr_end(addr, end);
  129. if (pmd_trans_huge(*pmd)) {
  130. if (next - addr != HPAGE_PMD_SIZE)
  131. split_huge_page_pmd(vma, addr, pmd);
  132. else {
  133. int nr_ptes = change_huge_pmd(vma, pmd, addr,
  134. newprot, prot_numa);
  135. if (nr_ptes) {
  136. if (nr_ptes == HPAGE_PMD_NR)
  137. pages++;
  138. continue;
  139. }
  140. }
  141. /* fall through */
  142. }
  143. if (pmd_none_or_clear_bad(pmd))
  144. continue;
  145. pages += change_pte_range(vma, pmd, addr, next, newprot,
  146. dirty_accountable, prot_numa, &all_same_node);
  147. /*
  148. * If we are changing protections for NUMA hinting faults then
  149. * set pmd_numa if the examined pages were all on the same
  150. * node. This allows a regular PMD to be handled as one fault
  151. * and effectively batches the taking of the PTL
  152. */
  153. if (prot_numa && all_same_node)
  154. change_pmd_protnuma(vma->vm_mm, addr, pmd);
  155. } while (pmd++, addr = next, addr != end);
  156. return pages;
  157. }
  158. static inline unsigned long change_pud_range(struct vm_area_struct *vma,
  159. pgd_t *pgd, unsigned long addr, unsigned long end,
  160. pgprot_t newprot, int dirty_accountable, int prot_numa)
  161. {
  162. pud_t *pud;
  163. unsigned long next;
  164. unsigned long pages = 0;
  165. pud = pud_offset(pgd, addr);
  166. do {
  167. next = pud_addr_end(addr, end);
  168. if (pud_none_or_clear_bad(pud))
  169. continue;
  170. pages += change_pmd_range(vma, pud, addr, next, newprot,
  171. dirty_accountable, prot_numa);
  172. } while (pud++, addr = next, addr != end);
  173. return pages;
  174. }
  175. static unsigned long change_protection_range(struct vm_area_struct *vma,
  176. unsigned long addr, unsigned long end, pgprot_t newprot,
  177. int dirty_accountable, int prot_numa)
  178. {
  179. struct mm_struct *mm = vma->vm_mm;
  180. pgd_t *pgd;
  181. unsigned long next;
  182. unsigned long start = addr;
  183. unsigned long pages = 0;
  184. BUG_ON(addr >= end);
  185. pgd = pgd_offset(mm, addr);
  186. flush_cache_range(vma, addr, end);
  187. do {
  188. next = pgd_addr_end(addr, end);
  189. if (pgd_none_or_clear_bad(pgd))
  190. continue;
  191. pages += change_pud_range(vma, pgd, addr, next, newprot,
  192. dirty_accountable, prot_numa);
  193. } while (pgd++, addr = next, addr != end);
  194. /* Only flush the TLB if we actually modified any entries: */
  195. if (pages)
  196. flush_tlb_range(vma, start, end);
  197. return pages;
  198. }
  199. unsigned long change_protection(struct vm_area_struct *vma, unsigned long start,
  200. unsigned long end, pgprot_t newprot,
  201. int dirty_accountable, int prot_numa)
  202. {
  203. struct mm_struct *mm = vma->vm_mm;
  204. unsigned long pages;
  205. mmu_notifier_invalidate_range_start(mm, start, end);
  206. if (is_vm_hugetlb_page(vma))
  207. pages = hugetlb_change_protection(vma, start, end, newprot);
  208. else
  209. pages = change_protection_range(vma, start, end, newprot, dirty_accountable, prot_numa);
  210. mmu_notifier_invalidate_range_end(mm, start, end);
  211. return pages;
  212. }
  213. int
  214. mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
  215. unsigned long start, unsigned long end, unsigned long newflags)
  216. {
  217. struct mm_struct *mm = vma->vm_mm;
  218. unsigned long oldflags = vma->vm_flags;
  219. long nrpages = (end - start) >> PAGE_SHIFT;
  220. unsigned long charged = 0;
  221. pgoff_t pgoff;
  222. int error;
  223. int dirty_accountable = 0;
  224. if (newflags == oldflags) {
  225. *pprev = vma;
  226. return 0;
  227. }
  228. /*
  229. * If we make a private mapping writable we increase our commit;
  230. * but (without finer accounting) cannot reduce our commit if we
  231. * make it unwritable again. hugetlb mapping were accounted for
  232. * even if read-only so there is no need to account for them here
  233. */
  234. if (newflags & VM_WRITE) {
  235. if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
  236. VM_SHARED|VM_NORESERVE))) {
  237. charged = nrpages;
  238. if (security_vm_enough_memory_mm(mm, charged))
  239. return -ENOMEM;
  240. newflags |= VM_ACCOUNT;
  241. }
  242. }
  243. /*
  244. * First try to merge with previous and/or next vma.
  245. */
  246. pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  247. *pprev = vma_merge(mm, *pprev, start, end, newflags,
  248. vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
  249. if (*pprev) {
  250. vma = *pprev;
  251. goto success;
  252. }
  253. *pprev = vma;
  254. if (start != vma->vm_start) {
  255. error = split_vma(mm, vma, start, 1);
  256. if (error)
  257. goto fail;
  258. }
  259. if (end != vma->vm_end) {
  260. error = split_vma(mm, vma, end, 0);
  261. if (error)
  262. goto fail;
  263. }
  264. success:
  265. /*
  266. * vm_flags and vm_page_prot are protected by the mmap_sem
  267. * held in write mode.
  268. */
  269. vma->vm_flags = newflags;
  270. vma->vm_page_prot = pgprot_modify(vma->vm_page_prot,
  271. vm_get_page_prot(newflags));
  272. if (vma_wants_writenotify(vma)) {
  273. vma->vm_page_prot = vm_get_page_prot(newflags & ~VM_SHARED);
  274. dirty_accountable = 1;
  275. }
  276. change_protection(vma, start, end, vma->vm_page_prot,
  277. dirty_accountable, 0);
  278. vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
  279. vm_stat_account(mm, newflags, vma->vm_file, nrpages);
  280. perf_event_mmap(vma);
  281. return 0;
  282. fail:
  283. vm_unacct_memory(charged);
  284. return error;
  285. }
  286. SYSCALL_DEFINE3(mprotect, unsigned long, start, size_t, len,
  287. unsigned long, prot)
  288. {
  289. unsigned long vm_flags, nstart, end, tmp, reqprot;
  290. struct vm_area_struct *vma, *prev;
  291. int error = -EINVAL;
  292. const int grows = prot & (PROT_GROWSDOWN|PROT_GROWSUP);
  293. prot &= ~(PROT_GROWSDOWN|PROT_GROWSUP);
  294. if (grows == (PROT_GROWSDOWN|PROT_GROWSUP)) /* can't be both */
  295. return -EINVAL;
  296. if (start & ~PAGE_MASK)
  297. return -EINVAL;
  298. if (!len)
  299. return 0;
  300. len = PAGE_ALIGN(len);
  301. end = start + len;
  302. if (end <= start)
  303. return -ENOMEM;
  304. if (!arch_validate_prot(prot))
  305. return -EINVAL;
  306. reqprot = prot;
  307. /*
  308. * Does the application expect PROT_READ to imply PROT_EXEC:
  309. */
  310. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  311. prot |= PROT_EXEC;
  312. vm_flags = calc_vm_prot_bits(prot);
  313. down_write(&current->mm->mmap_sem);
  314. vma = find_vma(current->mm, start);
  315. error = -ENOMEM;
  316. if (!vma)
  317. goto out;
  318. prev = vma->vm_prev;
  319. if (unlikely(grows & PROT_GROWSDOWN)) {
  320. if (vma->vm_start >= end)
  321. goto out;
  322. start = vma->vm_start;
  323. error = -EINVAL;
  324. if (!(vma->vm_flags & VM_GROWSDOWN))
  325. goto out;
  326. } else {
  327. if (vma->vm_start > start)
  328. goto out;
  329. if (unlikely(grows & PROT_GROWSUP)) {
  330. end = vma->vm_end;
  331. error = -EINVAL;
  332. if (!(vma->vm_flags & VM_GROWSUP))
  333. goto out;
  334. }
  335. }
  336. if (start > vma->vm_start)
  337. prev = vma;
  338. for (nstart = start ; ; ) {
  339. unsigned long newflags;
  340. /* Here we know that vma->vm_start <= nstart < vma->vm_end. */
  341. newflags = vm_flags;
  342. newflags |= (vma->vm_flags & ~(VM_READ | VM_WRITE | VM_EXEC));
  343. /* newflags >> 4 shift VM_MAY% in place of VM_% */
  344. if ((newflags & ~(newflags >> 4)) & (VM_READ | VM_WRITE | VM_EXEC)) {
  345. error = -EACCES;
  346. goto out;
  347. }
  348. error = security_file_mprotect(vma, reqprot, prot);
  349. if (error)
  350. goto out;
  351. tmp = vma->vm_end;
  352. if (tmp > end)
  353. tmp = end;
  354. error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
  355. if (error)
  356. goto out;
  357. nstart = tmp;
  358. if (nstart < prev->vm_end)
  359. nstart = prev->vm_end;
  360. if (nstart >= end)
  361. goto out;
  362. vma = prev->vm_next;
  363. if (!vma || vma->vm_start != nstart) {
  364. error = -ENOMEM;
  365. goto out;
  366. }
  367. }
  368. out:
  369. up_write(&current->mm->mmap_sem);
  370. return error;
  371. }