mprotect.c 10 KB

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