Skip to contentSkip to conversation

arm64: mte: move register initialization to C

PATCH Superseded Evgenii Stepanov 2 participants 2 messages Sep 1, 2022 – Sep 4, 2022 arm64

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 arm64: mte: move register initialization to C

If FEAT_MTE2 is disabled via the arm64.nomte command line argument on a CPU that claims to support FEAT_MTE2, the kernel will use Tagged Normal in the MAIR. If we interpret arm64.nomte to mean that the CPU does not in fact implement FEAT_MTE2, setting the system register like this may lead to UNSPECIFIED behavior. Fix it by arranging for MAIR to be set in the C function cpu_enable_mte which is called based on the sanitized version of the system register.

There is no need for the rest of the MTE-related system register initialization to happen from assembly, with the exception of TCR_EL1, which must be set to include at least TBI1 because the secondary CPUs access KASan-allocated data structures early. Therefore, make the TCR_EL1 initialization unconditional and move the rest of the initialization to cpu_enable_mte so that we no longer have a dependency on the unsanitized ID register value.

---
Changelog since v2:
- Fixed register initialization on cpu_resume code path.

Changelog since v1:
- Keep TBI1 off unless CONFIG_ARM64_MTE
- Fixed mask application in the RGSR_EL1 computation (bug found by Kenny Root).
- Changed code formatting

 arch/arm64/include/asm/mte.h   |  2 ++
 arch/arm64/kernel/cpufeature.c |  2 ++
 arch/arm64/kernel/mte.c        | 51 ++++++++++++++++++++++++++++++++++
 arch/arm64/kernel/suspend.c    |  2 ++
 arch/arm64/mm/proc.S           | 46 ++++--------------------------
 5 files changed, 62 insertions(+), 41 deletions(-)
arch/arm64/include/asm/mte.h +2 -0
index aa523591a44e5..5e80315e71894 100644
@@ -42,7 +42,9 @@ void mte_sync_tags(pte_t old_pte, pte_t pte);
 void mte_copy_page_tags(void *kto, const void *kfrom);
 void mte_thread_init_user(void);
 void mte_thread_switch(struct task_struct *next);
+void mte_cpu_setup(void);
 void mte_suspend_enter(void);
+void mte_suspend_exit(void);
 long set_mte_ctrl(struct task_struct *task, unsigned long arg);
 long get_mte_ctrl(struct task_struct *task);
 int mte_ptrace_copy_tags(struct task_struct *child, long request,
arch/arm64/kernel/cpufeature.c +2 -0
index af4de817d7123..fdc28f8e3307b 100644
@@ -2036,6 +2036,8 @@ static void cpu_enable_mte(struct arm64_cpu_capabilities const *cap)
 	sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_ATA | SCTLR_EL1_ATA0);
 	isb();
 
+	mte_cpu_setup();
+
 	/*
 	 * Clear the tags in the zero page. This needs to be done via the
 	 * linear map which has the Tagged attribute.
arch/arm64/kernel/mte.c +51 -0

63 lines not on this page · raw message

arch/arm64/kernel/suspend.c +2 -0
index 9135fe0f3df53..8b02d310838f9 100644
@@ -43,6 +43,8 @@ void notrace __cpu_suspend_exit(void)
 {
 	unsigned int cpu = smp_processor_id();
 
+	mte_suspend_exit();
+
 	/*
 	 * We are resuming from reset with the idmap active in TTBR0_EL1.
 	 * We must uninstall the idmap and restore the expected MMU
arch/arm64/mm/proc.S +5 -41

68 lines not on this page · raw message

Signature · 1 line
2.37.2.789.g6183377224-goog
#1 lore raw
Thank you for the patch! Yet something to improve:

Hi Evgenii,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on arm64/for-next/core]
[also build test ERROR on linus/master v6.0-rc3 next-20220901] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Evgenii-Stepanov/arm64-mte-move-register-initialization-to-C/20220902-052059
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
config: arm64-randconfig-c033-20220904 (https://download.01.org/0day-ci/archive/20220904/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/23927b7e78758b39d3463ae0a79b6b1af8fdef8b
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Evgenii-Stepanov/arm64-mte-move-register-initialization-to-C/20220902-052059 git checkout 23927b7e78758b39d3463ae0a79b6b1af8fdef8b
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash arch/arm64/kernel/

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

arch/arm64/kernel/suspend.c:34:13: warning: no previous prototype for 'cpu_suspend_set_dbg_restorer' [-Wmissing-prototypes]
34 | void __init cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
arch/arm64/kernel/suspend.c: In function '__cpu_suspend_exit':

>> arch/arm64/kernel/suspend.c:46:9: error: implicit declaration of function 'mte_suspend_exit'; did you mean 'mte_suspend_enter'? [-Werror=implicit-function-declaration]
      46 |         mte_suspend_exit();
         |         ^~~~~~~~~~~~~~~~
         |         mte_suspend_enter

cc1: some warnings being treated as errors

vim +46 arch/arm64/kernel/suspend.c

    41	
    42	void notrace __cpu_suspend_exit(void)
    43	{
    44		unsigned int cpu = smp_processor_id();
    45	
  > 46		mte_suspend_exit();
    47	
    48		/*
    49		 * We are resuming from reset with the idmap active in TTBR0_EL1.
    50		 * We must uninstall the idmap and restore the expected MMU
    51		 * state before we can possibly return to userspace.
    52		 */
    53		cpu_uninstall_idmap();
    54	
    55		/* Restore CnP bit in TTBR1_EL1 */
    56		if (system_supports_cnp())
    57			cpu_replace_ttbr1(lm_alias(swapper_pg_dir), idmap_pg_dir);
    58	
    59		/*
    60		 * PSTATE was not saved over suspend/resume, re-enable any detected
    61		 * features that might not have been set correctly.
    62		 */
    63		__uaccess_enable_hw_pan();
    64	
    65		/*
    66		 * Restore HW breakpoint registers to sane values
    67		 * before debug exceptions are possibly reenabled
    68		 * by cpu_suspend()s local_daif_restore() call.
    69		 */
    70		if (hw_breakpoint_restore)
    71			hw_breakpoint_restore(cpu);
    72	
    73		/*
    74		 * On resume, firmware implementing dynamic mitigation will
    75		 * have turned the mitigation on. If the user has forcefully
    76		 * disabled it, make sure their wishes are obeyed.
    77		 */
    78		spectre_v4_enable_mitigation(NULL);
    79	
    80		/* Restore additional feature-specific configuration */
    81		ptrauth_suspend_exit();
    82	}
    83	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp