1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2007-2008 Paul Mackerras, IBM Corp.
4  */
5 
6 #include <linux/errno.h>
7 #include <linux/kernel.h>
8 #include <linux/gfp.h>
9 #include <linux/types.h>
10 #include <linux/pagewalk.h>
11 #include <linux/hugetlb.h>
12 #include <linux/syscalls.h>
13 
14 #include <linux/pgtable.h>
15 #include <linux/uaccess.h>
16 
17 /*
18  * Free all pages allocated for subpage protection maps and pointers.
19  * Also makes sure that the subpage_prot_table structure is
20  * reinitialized for the next user.
21  */
22 void subpage_prot_free(struct mm_struct *mm)
23 {
24 	struct subpage_prot_table *spt = mm_ctx_subpage_prot(&mm->context);
25 	unsigned long i, j, addr;
26 	u32 **p;
27 
28 	if (!spt)
29 		return;
30 
31 	for (i = 0; i < 4; ++i) {
32 		if (spt->low_prot[i]) {
33 			free_page((unsigned long)spt->low_prot[i]);
34 			spt->low_prot[i] = NULL;
35 		}
36 	}
37 	addr = 0;
38 	for (i = 0; i < (TASK_SIZE_USER64 >> 43); ++i) {
39 		p = spt->protptrs[i];
40 		if (!p)
41 			continue;
42 		spt->protptrs[i] = NULL;
43 		for (j = 0; j < SBP_L2_COUNT && addr < spt->maxaddr;
44 		     ++j, addr += PAGE_SIZE)
45 			if (p[j])
46 				free_page((unsigned long)p[j]);
47 		free_page((unsigned long)p);
48 	}
49 	spt->maxaddr = 0;
50 	kfree(spt);
51 }
52 
53 static void hpte_flush_range(struct mm_struct *mm, unsigned long addr,
54 			     int npages)
55 {
56 	pgd_t *pgd;
57 	p4d_t *p4d;
58 	pud_t *pud;
59 	pmd_t *pmd;
60 	pte_t *pte;
61 	spinlock_t *ptl;
62 
63 	pgd = pgd_offset(mm, addr);
64 	p4d = p4d_offset(pgd, addr);
65 	if (p4d_none(*p4d))
66 		return;
67 	pud = pud_offset(p4d, addr);
68 	if (pud_none(*pud))
69 		return;
70 	pmd = pmd_offset(pud, addr);
71 	if (pmd_none(*pmd))
72 		return;
73 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
74 	if (!pte)
75 		return;
76 	arch_enter_lazy_mmu_mode();
77 	for (; npages > 0; --npages) {
78 		pte_update(mm, addr, pte, 0, 0, 0);
79 		addr += PAGE_SIZE;
80 		++pte;
81 	}
82 	arch_leave_lazy_mmu_mode();
83 	pte_unmap_unlock(pte - 1, ptl);
84 }
85 
86 /*
87  * Clear the subpage protection map for an address range, allowing
88  * all accesses that are allowed by the pte permissions.
89  */
90 static void subpage_prot_clear(unsigned long addr, unsigned long len)
91 {
92 	struct mm_struct *mm = current->mm;
93 	struct subpage_prot_table *spt;
94 	u32 **spm, *spp;
95 	unsigned long i;
96 	size_t nw;
97 	unsigned long next, limit;
98 
99 	mmap_write_lock(mm);
100 
101 	spt = mm_ctx_subpage_prot(&mm->context);
102 	if (!spt)
103 		goto err_out;
104 
105 	limit = addr + len;
106 	if (limit > spt->maxaddr)
107 		limit = spt->maxaddr;
108 	for (; addr < limit; addr = next) {
109 		next = pmd_addr_end(addr, limit);
110 		if (addr < 0x100000000UL) {
111 			spm = spt->low_prot;
112 		} else {
113 			spm = spt->protptrs[addr >> SBP_L3_SHIFT];
114 			if (!spm)
115 				continue;
116 		}
117 		spp = spm[(addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
118 		if (!spp)
119 			continue;
120 		spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
121 
122 		i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
123 		nw = PTRS_PER_PTE - i;
124 		if (addr + (nw << PAGE_SHIFT) > next)
125 			nw = (next - addr) >> PAGE_SHIFT;
126 
127 		memset(spp, 0, nw * sizeof(u32));
128 
129 		/* now flush any existing HPTEs for the range */
130 		hpte_flush_range(mm, addr, nw);
131 	}
132 
133 err_out:
134 	mmap_write_unlock(mm);
135 }
136 
137 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
138 static int subpage_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
139 				  unsigned long end, struct mm_walk *walk)
140 {
141 	struct vm_area_struct *vma = walk->vma;
142 	split_huge_pmd(vma, pmd, addr);
143 	return 0;
144 }
145 
146 static const struct mm_walk_ops subpage_walk_ops = {
147 	.pmd_entry	= subpage_walk_pmd_entry,
148 };
149 
150 static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
151 				    unsigned long len)
152 {
153 	struct vm_area_struct *vma;
154 	VMA_ITERATOR(vmi, mm, addr);
155 
156 	/*
157 	 * We don't try too hard, we just mark all the vma in that range
158 	 * VM_NOHUGEPAGE and split them.
159 	 */
160 	for_each_vma_range(vmi, vma, addr + len) {
161 		vm_flags_set(vma, VM_NOHUGEPAGE);
162 		walk_page_vma(vma, &subpage_walk_ops, NULL);
163 	}
164 }
165 #else
166 static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
167 				    unsigned long len)
168 {
169 	return;
170 }
171 #endif
172 
173 /*
174  * Copy in a subpage protection map for an address range.
175  * The map has 2 bits per 4k subpage, so 32 bits per 64k page.
176  * Each 2-bit field is 0 to allow any access, 1 to prevent writes,
177  * 2 or 3 to prevent all accesses.
178  * Note that the normal page protections also apply; the subpage
179  * protection mechanism is an additional constraint, so putting 0
180  * in a 2-bit field won't allow writes to a page that is otherwise
181  * write-protected.
182  */
183 SYSCALL_DEFINE3(subpage_prot, unsigned long, addr,
184 		unsigned long, len, u32 __user *, map)
185 {
186 	struct mm_struct *mm = current->mm;
187 	struct subpage_prot_table *spt;
188 	u32 **spm, *spp;
189 	unsigned long i;
190 	size_t nw;
191 	unsigned long next, limit;
192 	int err;
193 
194 	if (radix_enabled())
195 		return -ENOENT;
196 
197 	/* Check parameters */
198 	if ((addr & ~PAGE_MASK) || (len & ~PAGE_MASK) ||
199 	    addr >= mm->task_size || len >= mm->task_size ||
200 	    addr + len > mm->task_size)
201 		return -EINVAL;
202 
203 	if (is_hugepage_only_range(mm, addr, len))
204 		return -EINVAL;
205 
206 	if (!map) {
207 		/* Clear out the protection map for the address range */
208 		subpage_prot_clear(addr, len);
209 		return 0;
210 	}
211 
212 	if (!access_ok(map, (len >> PAGE_SHIFT) * sizeof(u32)))
213 		return -EFAULT;
214 
215 	mmap_write_lock(mm);
216 
217 	spt = mm_ctx_subpage_prot(&mm->context);
218 	if (!spt) {
219 		/*
220 		 * Allocate subpage prot table if not already done.
221 		 * Do this with mmap_lock held
222 		 */
223 		spt = kzalloc(sizeof(struct subpage_prot_table), GFP_KERNEL);
224 		if (!spt) {
225 			err = -ENOMEM;
226 			goto out;
227 		}
228 		mm->context.hash_context->spt = spt;
229 	}
230 
231 	subpage_mark_vma_nohuge(mm, addr, len);
232 	for (limit = addr + len; addr < limit; addr = next) {
233 		next = pmd_addr_end(addr, limit);
234 		err = -ENOMEM;
235 		if (addr < 0x100000000UL) {
236 			spm = spt->low_prot;
237 		} else {
238 			spm = spt->protptrs[addr >> SBP_L3_SHIFT];
239 			if (!spm) {
240 				spm = (u32 **)get_zeroed_page(GFP_KERNEL);
241 				if (!spm)
242 					goto out;
243 				spt->protptrs[addr >> SBP_L3_SHIFT] = spm;
244 			}
245 		}
246 		spm += (addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1);
247 		spp = *spm;
248 		if (!spp) {
249 			spp = (u32 *)get_zeroed_page(GFP_KERNEL);
250 			if (!spp)
251 				goto out;
252 			*spm = spp;
253 		}
254 		spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
255 
256 		local_irq_disable();
257 		demote_segment_4k(mm, addr);
258 		local_irq_enable();
259 
260 		i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
261 		nw = PTRS_PER_PTE - i;
262 		if (addr + (nw << PAGE_SHIFT) > next)
263 			nw = (next - addr) >> PAGE_SHIFT;
264 
265 		mmap_write_unlock(mm);
266 		if (__copy_from_user(spp, map, nw * sizeof(u32)))
267 			return -EFAULT;
268 		map += nw;
269 		mmap_write_lock(mm);
270 
271 		/* now flush any existing HPTEs for the range */
272 		hpte_flush_range(mm, addr, nw);
273 	}
274 	if (limit > spt->maxaddr)
275 		spt->maxaddr = limit;
276 	err = 0;
277  out:
278 	mmap_write_unlock(mm);
279 	return err;
280 }
281