mprotect.c 10 KB

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