Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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