Skip to content

Commit f3ba50a

Browse files
committed
arm64: Add support for user sub-page fault probing
With MTE, even if the pte allows an access, a mismatched tag somewhere within a page can still cause a fault. Select ARCH_HAS_SUBPAGE_FAULTS if MTE is enabled and implement the probe_subpage_writeable() function. Note that get_user() is sufficient for the writeable MTE check since the same tag mismatch fault would be triggered by a read. The caller of probe_subpage_writeable() will need to check the pte permissions (put_user, GUP). Signed-off-by: Catalin Marinas <[email protected]> Cc: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent da32b58 commit f3ba50a

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,7 @@ config ARM64_MTE
18711871
depends on AS_HAS_LSE_ATOMICS
18721872
# Required for tag checking in the uaccess routines
18731873
depends on ARM64_PAN
1874+
select ARCH_HAS_SUBPAGE_FAULTS
18741875
select ARCH_USES_HIGH_VMA_FLAGS
18751876
help
18761877
Memory Tagging (part of the ARMv8.5 Extensions) provides

arch/arm64/include/asm/mte.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ long set_mte_ctrl(struct task_struct *task, unsigned long arg);
4747
long get_mte_ctrl(struct task_struct *task);
4848
int mte_ptrace_copy_tags(struct task_struct *child, long request,
4949
unsigned long addr, unsigned long data);
50+
size_t mte_probe_user_range(const char __user *uaddr, size_t size);
5051

5152
#else /* CONFIG_ARM64_MTE */
5253

arch/arm64/include/asm/uaccess.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,4 +460,19 @@ static inline int __copy_from_user_flushcache(void *dst, const void __user *src,
460460
}
461461
#endif
462462

463+
#ifdef CONFIG_ARCH_HAS_SUBPAGE_FAULTS
464+
465+
/*
466+
* Return 0 on success, the number of bytes not probed otherwise.
467+
*/
468+
static inline size_t probe_subpage_writeable(const char __user *uaddr,
469+
size_t size)
470+
{
471+
if (!system_supports_mte())
472+
return 0;
473+
return mte_probe_user_range(uaddr, size);
474+
}
475+
476+
#endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */
477+
463478
#endif /* __ASM_UACCESS_H */

arch/arm64/kernel/mte.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/swapops.h>
1616
#include <linux/thread_info.h>
1717
#include <linux/types.h>
18+
#include <linux/uaccess.h>
1819
#include <linux/uio.h>
1920

2021
#include <asm/barrier.h>
@@ -543,3 +544,32 @@ static int register_mte_tcf_preferred_sysctl(void)
543544
return 0;
544545
}
545546
subsys_initcall(register_mte_tcf_preferred_sysctl);
547+
548+
/*
549+
* Return 0 on success, the number of bytes not probed otherwise.
550+
*/
551+
size_t mte_probe_user_range(const char __user *uaddr, size_t size)
552+
{
553+
const char __user *end = uaddr + size;
554+
int err = 0;
555+
char val;
556+
557+
__raw_get_user(val, uaddr, err);
558+
if (err)
559+
return size;
560+
561+
uaddr = PTR_ALIGN(uaddr, MTE_GRANULE_SIZE);
562+
while (uaddr < end) {
563+
/*
564+
* A read is sufficient for mte, the caller should have probed
565+
* for the pte write permission if required.
566+
*/
567+
__raw_get_user(val, uaddr, err);
568+
if (err)
569+
return end - uaddr;
570+
uaddr += MTE_GRANULE_SIZE;
571+
}
572+
(void)val;
573+
574+
return 0;
575+
}

0 commit comments

Comments
 (0)