arm64: mte: Report async tag faults before suspend
[linux-2.6-microblaze.git] / arch / arm64 / kernel / mte.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 ARM Ltd.
4  */
5
6 #include <linux/bitops.h>
7 #include <linux/kernel.h>
8 #include <linux/mm.h>
9 #include <linux/prctl.h>
10 #include <linux/sched.h>
11 #include <linux/sched/mm.h>
12 #include <linux/string.h>
13 #include <linux/swap.h>
14 #include <linux/swapops.h>
15 #include <linux/thread_info.h>
16 #include <linux/types.h>
17 #include <linux/uio.h>
18
19 #include <asm/barrier.h>
20 #include <asm/cpufeature.h>
21 #include <asm/mte.h>
22 #include <asm/ptrace.h>
23 #include <asm/sysreg.h>
24
25 u64 gcr_kernel_excl __ro_after_init;
26
27 static bool report_fault_once = true;
28
29 #ifdef CONFIG_KASAN_HW_TAGS
30 /* Whether the MTE asynchronous mode is enabled. */
31 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
32 EXPORT_SYMBOL_GPL(mte_async_mode);
33 #endif
34
35 static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
36 {
37         pte_t old_pte = READ_ONCE(*ptep);
38
39         if (check_swap && is_swap_pte(old_pte)) {
40                 swp_entry_t entry = pte_to_swp_entry(old_pte);
41
42                 if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
43                         return;
44         }
45
46         page_kasan_tag_reset(page);
47         /*
48          * We need smp_wmb() in between setting the flags and clearing the
49          * tags because if another thread reads page->flags and builds a
50          * tagged address out of it, there is an actual dependency to the
51          * memory access, but on the current thread we do not guarantee that
52          * the new page->flags are visible before the tags were updated.
53          */
54         smp_wmb();
55         mte_clear_page_tags(page_address(page));
56 }
57
58 void mte_sync_tags(pte_t *ptep, pte_t pte)
59 {
60         struct page *page = pte_page(pte);
61         long i, nr_pages = compound_nr(page);
62         bool check_swap = nr_pages == 1;
63
64         /* if PG_mte_tagged is set, tags have already been initialised */
65         for (i = 0; i < nr_pages; i++, page++) {
66                 if (!test_and_set_bit(PG_mte_tagged, &page->flags))
67                         mte_sync_page_tags(page, ptep, check_swap);
68         }
69 }
70
71 int memcmp_pages(struct page *page1, struct page *page2)
72 {
73         char *addr1, *addr2;
74         int ret;
75
76         addr1 = page_address(page1);
77         addr2 = page_address(page2);
78         ret = memcmp(addr1, addr2, PAGE_SIZE);
79
80         if (!system_supports_mte() || ret)
81                 return ret;
82
83         /*
84          * If the page content is identical but at least one of the pages is
85          * tagged, return non-zero to avoid KSM merging. If only one of the
86          * pages is tagged, set_pte_at() may zero or change the tags of the
87          * other page via mte_sync_tags().
88          */
89         if (test_bit(PG_mte_tagged, &page1->flags) ||
90             test_bit(PG_mte_tagged, &page2->flags))
91                 return addr1 != addr2;
92
93         return ret;
94 }
95
96 void mte_init_tags(u64 max_tag)
97 {
98         static bool gcr_kernel_excl_initialized;
99
100         if (!gcr_kernel_excl_initialized) {
101                 /*
102                  * The format of the tags in KASAN is 0xFF and in MTE is 0xF.
103                  * This conversion extracts an MTE tag from a KASAN tag.
104                  */
105                 u64 incl = GENMASK(FIELD_GET(MTE_TAG_MASK >> MTE_TAG_SHIFT,
106                                              max_tag), 0);
107
108                 gcr_kernel_excl = ~incl & SYS_GCR_EL1_EXCL_MASK;
109                 gcr_kernel_excl_initialized = true;
110         }
111
112         /* Enable the kernel exclude mask for random tags generation. */
113         write_sysreg_s(SYS_GCR_EL1_RRND | gcr_kernel_excl, SYS_GCR_EL1);
114 }
115
116 static inline void __mte_enable_kernel(const char *mode, unsigned long tcf)
117 {
118         /* Enable MTE Sync Mode for EL1. */
119         sysreg_clear_set(sctlr_el1, SCTLR_ELx_TCF_MASK, tcf);
120         isb();
121
122         pr_info_once("MTE: enabled in %s mode at EL1\n", mode);
123 }
124
125 #ifdef CONFIG_KASAN_HW_TAGS
126 void mte_enable_kernel_sync(void)
127 {
128         /*
129          * Make sure we enter this function when no PE has set
130          * async mode previously.
131          */
132         WARN_ONCE(system_uses_mte_async_mode(),
133                         "MTE async mode enabled system wide!");
134
135         __mte_enable_kernel("synchronous", SCTLR_ELx_TCF_SYNC);
136 }
137
138 void mte_enable_kernel_async(void)
139 {
140         __mte_enable_kernel("asynchronous", SCTLR_ELx_TCF_ASYNC);
141
142         /*
143          * MTE async mode is set system wide by the first PE that
144          * executes this function.
145          *
146          * Note: If in future KASAN acquires a runtime switching
147          * mode in between sync and async, this strategy needs
148          * to be reviewed.
149          */
150         if (!system_uses_mte_async_mode())
151                 static_branch_enable(&mte_async_mode);
152 }
153 #endif
154
155 void mte_set_report_once(bool state)
156 {
157         WRITE_ONCE(report_fault_once, state);
158 }
159
160 bool mte_report_once(void)
161 {
162         return READ_ONCE(report_fault_once);
163 }
164
165 #ifdef CONFIG_KASAN_HW_TAGS
166 void mte_check_tfsr_el1(void)
167 {
168         u64 tfsr_el1;
169
170         if (!system_supports_mte())
171                 return;
172
173         tfsr_el1 = read_sysreg_s(SYS_TFSR_EL1);
174
175         if (unlikely(tfsr_el1 & SYS_TFSR_EL1_TF1)) {
176                 /*
177                  * Note: isb() is not required after this direct write
178                  * because there is no indirect read subsequent to it
179                  * (per ARM DDI 0487F.c table D13-1).
180                  */
181                 write_sysreg_s(0, SYS_TFSR_EL1);
182
183                 kasan_report_async();
184         }
185 }
186 #endif
187
188 static void update_sctlr_el1_tcf0(u64 tcf0)
189 {
190         /* ISB required for the kernel uaccess routines */
191         sysreg_clear_set(sctlr_el1, SCTLR_EL1_TCF0_MASK, tcf0);
192         isb();
193 }
194
195 static void set_sctlr_el1_tcf0(u64 tcf0)
196 {
197         /*
198          * mte_thread_switch() checks current->thread.sctlr_tcf0 as an
199          * optimisation. Disable preemption so that it does not see
200          * the variable update before the SCTLR_EL1.TCF0 one.
201          */
202         preempt_disable();
203         current->thread.sctlr_tcf0 = tcf0;
204         update_sctlr_el1_tcf0(tcf0);
205         preempt_enable();
206 }
207
208 static void update_gcr_el1_excl(u64 excl)
209 {
210
211         /*
212          * Note that the mask controlled by the user via prctl() is an
213          * include while GCR_EL1 accepts an exclude mask.
214          * No need for ISB since this only affects EL0 currently, implicit
215          * with ERET.
216          */
217         sysreg_clear_set_s(SYS_GCR_EL1, SYS_GCR_EL1_EXCL_MASK, excl);
218 }
219
220 static void set_gcr_el1_excl(u64 excl)
221 {
222         current->thread.gcr_user_excl = excl;
223
224         /*
225          * SYS_GCR_EL1 will be set to current->thread.gcr_user_excl value
226          * by mte_set_user_gcr() in kernel_exit,
227          */
228 }
229
230 void flush_mte_state(void)
231 {
232         if (!system_supports_mte())
233                 return;
234
235         /* clear any pending asynchronous tag fault */
236         dsb(ish);
237         write_sysreg_s(0, SYS_TFSRE0_EL1);
238         clear_thread_flag(TIF_MTE_ASYNC_FAULT);
239         /* disable tag checking */
240         set_sctlr_el1_tcf0(SCTLR_EL1_TCF0_NONE);
241         /* reset tag generation mask */
242         set_gcr_el1_excl(SYS_GCR_EL1_EXCL_MASK);
243 }
244
245 void mte_thread_switch(struct task_struct *next)
246 {
247         if (!system_supports_mte())
248                 return;
249
250         /* avoid expensive SCTLR_EL1 accesses if no change */
251         if (current->thread.sctlr_tcf0 != next->thread.sctlr_tcf0)
252                 update_sctlr_el1_tcf0(next->thread.sctlr_tcf0);
253         else
254                 isb();
255
256         /*
257          * Check if an async tag exception occurred at EL1.
258          *
259          * Note: On the context switch path we rely on the dsb() present
260          * in __switch_to() to guarantee that the indirect writes to TFSR_EL1
261          * are synchronized before this point.
262          * isb() above is required for the same reason.
263          *
264          */
265         mte_check_tfsr_el1();
266 }
267
268 void mte_suspend_enter(void)
269 {
270         if (!system_supports_mte())
271                 return;
272
273         /*
274          * The barriers are required to guarantee that the indirect writes
275          * to TFSR_EL1 are synchronized before we report the state.
276          */
277         dsb(nsh);
278         isb();
279
280         /* Report SYS_TFSR_EL1 before suspend entry */
281         mte_check_tfsr_el1();
282 }
283
284 void mte_suspend_exit(void)
285 {
286         if (!system_supports_mte())
287                 return;
288
289         update_gcr_el1_excl(gcr_kernel_excl);
290 }
291
292 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
293 {
294         u64 tcf0;
295         u64 gcr_excl = ~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
296                        SYS_GCR_EL1_EXCL_MASK;
297
298         if (!system_supports_mte())
299                 return 0;
300
301         switch (arg & PR_MTE_TCF_MASK) {
302         case PR_MTE_TCF_NONE:
303                 tcf0 = SCTLR_EL1_TCF0_NONE;
304                 break;
305         case PR_MTE_TCF_SYNC:
306                 tcf0 = SCTLR_EL1_TCF0_SYNC;
307                 break;
308         case PR_MTE_TCF_ASYNC:
309                 tcf0 = SCTLR_EL1_TCF0_ASYNC;
310                 break;
311         default:
312                 return -EINVAL;
313         }
314
315         if (task != current) {
316                 task->thread.sctlr_tcf0 = tcf0;
317                 task->thread.gcr_user_excl = gcr_excl;
318         } else {
319                 set_sctlr_el1_tcf0(tcf0);
320                 set_gcr_el1_excl(gcr_excl);
321         }
322
323         return 0;
324 }
325
326 long get_mte_ctrl(struct task_struct *task)
327 {
328         unsigned long ret;
329         u64 incl = ~task->thread.gcr_user_excl & SYS_GCR_EL1_EXCL_MASK;
330
331         if (!system_supports_mte())
332                 return 0;
333
334         ret = incl << PR_MTE_TAG_SHIFT;
335
336         switch (task->thread.sctlr_tcf0) {
337         case SCTLR_EL1_TCF0_NONE:
338                 ret |= PR_MTE_TCF_NONE;
339                 break;
340         case SCTLR_EL1_TCF0_SYNC:
341                 ret |= PR_MTE_TCF_SYNC;
342                 break;
343         case SCTLR_EL1_TCF0_ASYNC:
344                 ret |= PR_MTE_TCF_ASYNC;
345                 break;
346         }
347
348         return ret;
349 }
350
351 /*
352  * Access MTE tags in another process' address space as given in mm. Update
353  * the number of tags copied. Return 0 if any tags copied, error otherwise.
354  * Inspired by __access_remote_vm().
355  */
356 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
357                                 struct iovec *kiov, unsigned int gup_flags)
358 {
359         struct vm_area_struct *vma;
360         void __user *buf = kiov->iov_base;
361         size_t len = kiov->iov_len;
362         int ret;
363         int write = gup_flags & FOLL_WRITE;
364
365         if (!access_ok(buf, len))
366                 return -EFAULT;
367
368         if (mmap_read_lock_killable(mm))
369                 return -EIO;
370
371         while (len) {
372                 unsigned long tags, offset;
373                 void *maddr;
374                 struct page *page = NULL;
375
376                 ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page,
377                                             &vma, NULL);
378                 if (ret <= 0)
379                         break;
380
381                 /*
382                  * Only copy tags if the page has been mapped as PROT_MTE
383                  * (PG_mte_tagged set). Otherwise the tags are not valid and
384                  * not accessible to user. Moreover, an mprotect(PROT_MTE)
385                  * would cause the existing tags to be cleared if the page
386                  * was never mapped with PROT_MTE.
387                  */
388                 if (!(vma->vm_flags & VM_MTE)) {
389                         ret = -EOPNOTSUPP;
390                         put_page(page);
391                         break;
392                 }
393                 WARN_ON_ONCE(!test_bit(PG_mte_tagged, &page->flags));
394
395                 /* limit access to the end of the page */
396                 offset = offset_in_page(addr);
397                 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
398
399                 maddr = page_address(page);
400                 if (write) {
401                         tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
402                         set_page_dirty_lock(page);
403                 } else {
404                         tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
405                 }
406                 put_page(page);
407
408                 /* error accessing the tracer's buffer */
409                 if (!tags)
410                         break;
411
412                 len -= tags;
413                 buf += tags;
414                 addr += tags * MTE_GRANULE_SIZE;
415         }
416         mmap_read_unlock(mm);
417
418         /* return an error if no tags copied */
419         kiov->iov_len = buf - kiov->iov_base;
420         if (!kiov->iov_len) {
421                 /* check for error accessing the tracee's address space */
422                 if (ret <= 0)
423                         return -EIO;
424                 else
425                         return -EFAULT;
426         }
427
428         return 0;
429 }
430
431 /*
432  * Copy MTE tags in another process' address space at 'addr' to/from tracer's
433  * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
434  */
435 static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
436                               struct iovec *kiov, unsigned int gup_flags)
437 {
438         struct mm_struct *mm;
439         int ret;
440
441         mm = get_task_mm(tsk);
442         if (!mm)
443                 return -EPERM;
444
445         if (!tsk->ptrace || (current != tsk->parent) ||
446             ((get_dumpable(mm) != SUID_DUMP_USER) &&
447              !ptracer_capable(tsk, mm->user_ns))) {
448                 mmput(mm);
449                 return -EPERM;
450         }
451
452         ret = __access_remote_tags(mm, addr, kiov, gup_flags);
453         mmput(mm);
454
455         return ret;
456 }
457
458 int mte_ptrace_copy_tags(struct task_struct *child, long request,
459                          unsigned long addr, unsigned long data)
460 {
461         int ret;
462         struct iovec kiov;
463         struct iovec __user *uiov = (void __user *)data;
464         unsigned int gup_flags = FOLL_FORCE;
465
466         if (!system_supports_mte())
467                 return -EIO;
468
469         if (get_user(kiov.iov_base, &uiov->iov_base) ||
470             get_user(kiov.iov_len, &uiov->iov_len))
471                 return -EFAULT;
472
473         if (request == PTRACE_POKEMTETAGS)
474                 gup_flags |= FOLL_WRITE;
475
476         /* align addr to the MTE tag granule */
477         addr &= MTE_GRANULE_MASK;
478
479         ret = access_remote_tags(child, addr, &kiov, gup_flags);
480         if (!ret)
481                 ret = put_user(kiov.iov_len, &uiov->iov_len);
482
483         return ret;
484 }