Skip to contentSkip to conversation

kselftest: arm64/mte: Tests for user-space MTE

PATCH Superseded Amit Daniel Kachhap 2 participants 13 messages Sep 1, 2020 – Sep 23, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover kselftest: arm64/mte: Tests for user-space MTE

These patch series adds below kselftests to test the user-space support for the ARMv8.5 Memory Tagging Extension present in arm64 tree [1].

1) This test-case verifies that the memory allocated by kernel mmap interface can support tagged memory access. It first checks the presence of tags at address[56:59] and then proceeds with read and write. The pass criteria for this test is that tag fault exception should not happen.

2) This test-case crosses the valid memory to the invalid memory. In this memory area valid tags are not inserted so read and write should not pass. The pass criteria for this test is that tag fault exception should happen for all the illegal addresses. This test also verfies that PSTATE.TCO works properly.

3) This test-case verifies that the memory inherited by child process from parent process should have same tags copied. The pass criteria for this test is that tag fault exception should not happen.

4) This test checks different mmap flags with PROT_MTE memory protection.

5) This testcase checks that KSM should not merge pages containing different MTE tag values. However, if the tags are same then the pages may merge. This testcase uses the generic ksm sysfs interfaces to verify the MTE behaviour, so this testcase is not fullproof and may be impacted due to other load in the system.

6) Fifth test verifies that syscalls read/write etc works by considering that user pointer has valid/invalid allocation tags.

To simplify the testing, a copy of the patchset on top of a recent linux tree can be found at [2].

Thanks,
Amit Daniel

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/mte [2]: http://linux-arm.org/git?p=linux-ak.git;a=shortlog;h=refs/heads/kselftest-mte-mainline-v1

Amit Daniel Kachhap (6):
kselftest/arm64: Add utilities and a test to validate mte memory kselftest/arm64: Verify mte tag inclusion via prctl kselftest/arm64: Check forked child mte memory accessibility kselftest/arm64: Verify all different mmap MTE options kselftest/arm64: Verify KSM page merge for MTE pages kselftest/arm64: Check mte tagged user address in kernel

 tools/testing/selftests/arm64/Makefile        |   2 +-
 tools/testing/selftests/arm64/mte/.gitignore  |   6 +
 tools/testing/selftests/arm64/mte/Makefile    |  29 ++
 .../selftests/arm64/mte/check_buffer_fill.c   | 476 ++++++++++++++++++
 .../selftests/arm64/mte/check_child_memory.c  | 195 +++++++
 .../selftests/arm64/mte/check_ksm_options.c   | 131 +++++
 .../selftests/arm64/mte/check_mmap_options.c  | 262 ++++++++++
 .../arm64/mte/check_tags_inclusion.c          | 183 +++++++
 .../selftests/arm64/mte/check_user_mem.c      | 118 +++++
 .../selftests/arm64/mte/mte_common_util.c     | 374 ++++++++++++++
 .../selftests/arm64/mte/mte_common_util.h     | 135 +++++
 tools/testing/selftests/arm64/mte/mte_def.h   |  26 +
 .../testing/selftests/arm64/mte/mte_helper.S  | 116 +++++
 13 files changed, 2052 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/arm64/mte/.gitignore
 create mode 100644 tools/testing/selftests/arm64/mte/Makefile
 create mode 100644 tools/testing/selftests/arm64/mte/check_buffer_fill.c
 create mode 100644 tools/testing/selftests/arm64/mte/check_child_memory.c
 create mode 100644 tools/testing/selftests/arm64/mte/check_ksm_options.c
 create mode 100644 tools/testing/selftests/arm64/mte/check_mmap_options.c
 create mode 100644 tools/testing/selftests/arm64/mte/check_tags_inclusion.c
 create mode 100644 tools/testing/selftests/arm64/mte/check_user_mem.c
 create mode 100644 tools/testing/selftests/arm64/mte/mte_common_util.c
 create mode 100644 tools/testing/selftests/arm64/mte/mte_common_util.h
 create mode 100644 tools/testing/selftests/arm64/mte/mte_def.h
 create mode 100644 tools/testing/selftests/arm64/mte/mte_helper.S
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/6 kselftest/arm64: Add utilities and a test to validate mte memory

This test checks that the memory tag is present after mte allocation and the memory is accessible with those tags. This testcase verifies both sync, async and none mte error mode. The allocated mte buffers are verified for Allocated range (no error expected while accessing buffer), Underflow range (SIGSEGV fault is expected with SEGV_MTESERR signal code) and Overflow range (SIGSEGV fault is expected with SEGV_MTEAERR signal code).

Different test scenarios covered here are,
* Verify that mte memory are accessible at byte/block level.
* Force underflow and overflow to occur and check the data consistency.
* Check to/from between tagged and untagged memory.
* Check that initial allocated memory to have 0 tag.

This change also creates the necessary infrastructure to add MTE test cases. MTE kselftests can use the several utility functions provided here to add wide variety of MTE test scenarios.

GCC compiler need flag '-march=armv8.5-a+memtag' so those flags are verified before compilation.

The mte testcases can be launched with kselftest framework as,

make TARGETS=arm64 ARM64_SUBTARGETS=mte kselftest

or compiled as,

make -C tools/testing/selftests TARGETS=arm64 ARM64_SUBTARGETS=mte CC='compiler'

---
 tools/testing/selftests/arm64/Makefile        |   2 +-
 tools/testing/selftests/arm64/mte/.gitignore  |   1 +
 tools/testing/selftests/arm64/mte/Makefile    |  29 ++
 .../selftests/arm64/mte/check_buffer_fill.c   | 476 ++++++++++++++++++
 .../selftests/arm64/mte/mte_common_util.c     | 374 ++++++++++++++
 .../selftests/arm64/mte/mte_common_util.h     | 135 +++++
 tools/testing/selftests/arm64/mte/mte_def.h   |  26 +
 .../testing/selftests/arm64/mte/mte_helper.S  | 116 +++++
 8 files changed, 1158 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/arm64/mte/.gitignore
 create mode 100644 tools/testing/selftests/arm64/mte/Makefile
 create mode 100644 tools/testing/selftests/arm64/mte/check_buffer_fill.c
 create mode 100644 tools/testing/selftests/arm64/mte/mte_common_util.c
 create mode 100644 tools/testing/selftests/arm64/mte/mte_common_util.h
 create mode 100644 tools/testing/selftests/arm64/mte/mte_def.h
 create mode 100644 tools/testing/selftests/arm64/mte/mte_helper.S
tools/testing/selftests/arm64/Makefile +1 -1
index 93b567d23c8b..3723d9dea11a 100644
@@ -4,7 +4,7 @@
 ARCH ?= $(shell uname -m 2>/dev/null || echo not)
 
 ifneq (,$(filter $(ARCH),aarch64 arm64))
-ARM64_SUBTARGETS ?= tags signal
+ARM64_SUBTARGETS ?= tags signal mte
 else
 ARM64_SUBTARGETS :=
 endif
tools/testing/selftests/arm64/mte/.gitignore new file +1 -0
new file mode 100644
index 000000000000..3f8c1f6c82b9
@@ -0,0 +1 @@
+check_buffer_fill
tools/testing/selftests/arm64/mte/Makefile new file +29 -0
new file mode 100644
index 000000000000..2480226dfe57
@@ -0,0 +1,29 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2020 ARM Limited
+
+CFLAGS += -std=gnu99 -I.
+SRCS := $(filter-out mte_common_util.c,$(wildcard *.c))
+PROGS := $(patsubst %.c,%,$(SRCS))
+
+#Add mte compiler option
+ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep gcc),)
+CFLAGS += -march=armv8.5-a+memtag
+endif
+
+#check if the compiler works well
+mte_cc_support := $(shell if ($(CC) $(CFLAGS) -E -x c /dev/null -o /dev/null 2>&1) then echo "1"; fi)
+
+ifeq ($(mte_cc_support),1)
+# Generated binaries to be installed by top KSFT script
+TEST_GEN_PROGS := $(PROGS)
+
+# Get Kernel headers installed and use them.
+KSFT_KHDR_INSTALL := 1
+endif
+
+# Include KSFT lib.mk.
+include ../../lib.mk
+
+ifeq ($(mte_cc_support),1)
+$(TEST_GEN_PROGS): mte_common_util.c mte_common_util.h mte_helper.S
+endif
tools/testing/selftests/arm64/mte/check_buffer_fill.c new file +476 -0

476 lines not on this page · raw message

tools/testing/selftests/arm64/mte/mte_common_util.c new file +374 -0

374 lines not on this page · raw message

tools/testing/selftests/arm64/mte/mte_common_util.h new file +135 -0

135 lines not on this page · raw message

tools/testing/selftests/arm64/mte/mte_def.h new file +26 -0
new file mode 100644
index 000000000000..cdcf3614d29e
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2020 ARM Limited */
+
+#define MT_TAG_SHIFT		56
+#define MT_TAG_MASK		0xFUL
+#define MT_FREE_TAG		0x0UL
+#define MT_GRANULE_SIZE         16
+#define MT_TAG_COUNT		16
+#define MT_INCLUDE_TAG_MASK	0xFFFF
+#define MT_EXCLUDE_TAG_MASK	0x0
+
+#define MT_ALIGN_GRANULE	(MT_GRANULE_SIZE - 1)
+#define MT_CLEAR_TAG(x)		((x) & ~(MT_TAG_MASK << MT_TAG_SHIFT))
+#define MT_SET_TAG(x, y)	((x) | (y << MT_TAG_SHIFT))
+#define MT_FETCH_TAG(x)		((x >> MT_TAG_SHIFT) & (MT_TAG_MASK))
+#define MT_ALIGN_UP(x)		((x + MT_ALIGN_GRANULE) & ~(MT_ALIGN_GRANULE))
+
+#define MT_PSTATE_TCO_SHIFT	25
+#define MT_PSTATE_TCO_MASK	~(0x1 << MT_PSTATE_TCO_SHIFT)
+#define MT_PSTATE_TCO_EN	1
+#define MT_PSTATE_TCO_DIS	0
+
+#define MT_EXCLUDE_TAG(x)		(1 << (x))
+#define MT_INCLUDE_VALID_TAG(x)		(MT_INCLUDE_TAG_MASK ^ MT_EXCLUDE_TAG(x))
+#define MT_INCLUDE_VALID_TAGS(x)	(MT_INCLUDE_TAG_MASK ^ (x))
+#define MTE_ALLOW_NON_ZERO_TAG		MT_INCLUDE_VALID_TAG(0)
tools/testing/selftests/arm64/mte/mte_helper.S new file +116 -0

116 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH 1/6] kselftest/arm64: Add utilities and a test to validate mte memory

Quoted 23 lines · Amit Daniel Kachhap
tools/testing/selftests/arm64/mte/mte_common_util.c new file +17 -0
new file mode 100644
index 000000000000..ac311919567d
@@ -0,0 +1,374 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2020 ARM Limited
+
+#include <fcntl.h>
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <linux/auxvec.h>
+#include <sys/auxv.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+
+#include <asm/hwcap.h>
+

tools/testing/selftests/arm64/mte/mte_common_util.c line 27

Quoted 10 lines
tools/testing/selftests/arm64/mte/mte_common_util.c new file +10 -0
+#include "kselftest.h"
+#include "mte_common_util.h"
+#include "mte_def.h"
+
+/* The temp file must be created in a tmpfs filesystem */
+#ifdef ANDROID
+# define TEMPFILENAME    "/storage/tmp_XXXXXX"
+#else
+# define TEMPFILENAME    "/tmp/tmp_XXXXXX"
+#endif

That's not guaranteed to be tmpfs (it's not on my Debian install). I think you'd have a better chance with /dev/shm/tmp_XXXXXX.

Signature · 1 line
Catalin
maintainer
#3 lore raw

Re: [PATCH 1/6] kselftest/arm64: Add utilities and a test to validate mte memory

In reply to Amit Daniel Kachhap
Quoted 25 lines · Amit Daniel Kachhap
tools/testing/selftests/arm64/mte/mte_helper.S new file +19 -0
new file mode 100644
index 000000000000..91af6d1293f8
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2020 ARM Limited */
+
+#include "mte_def.h"
+
+#define ENTRY(name) \
+	.globl name ;\
+	.p2align 2;\
+	.type name, @function ;\
+name:
+
+#define ENDPROC(name) \
+	.size name, .-name ;
+
+	.text
+/*
+ * mte_insert_random_tag: Insert random tag and different from
+ *			 the orginal tag if source pointer has it.
+ * Input:

tools/testing/selftests/arm64/mte/mte_helper.S line 29

Quoted 10 lines
tools/testing/selftests/arm64/mte/mte_helper.S new file +10 -0
+ *		x0 - source pointer with a tag/no-tag
+ * Return:
+ *		x0 - pointer with random tag
+ */
+ENTRY(mte_insert_random_tag)
+	mov	x1, #0x0
+	gmi	x1, x0, x1
+	irg	x0, x0, x1
+	ret
+ENDPROC(mte_insert_random_tag)

What was the reason for gmi here? The test fails when you have an include mask of 0x8000 (exclude mask 0x7fff) and x0 has tag 0xf. In this case we exclude the only allowed tag here, so the CPU falls back to the default tag 0.

You can (a) stop the check_multiple_included_tags() earlier to have two allowed tags here, (b) clear the pointer old tag so that you don't end up in this scenario or (c) simply remove the gmi. My preference is the latter, we don't test the hardware here, we only want to check whether the kernel sets the GCR_EL1 correctly.

BTW, you also remove mov x1, #0, just:

	irg	x0, x0, xzr
Signature · 1 line
Catalin
maintainer
#4 lore raw

Re: [PATCH 1/6] kselftest/arm64: Add utilities and a test to validate mte memory

Quoted 48 lines · Catalin Marinas
Quoted 25 lines · Amit Daniel Kachhap
tools/testing/selftests/arm64/mte/mte_helper.S new file +19 -0
new file mode 100644
index 000000000000..91af6d1293f8
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2020 ARM Limited */
+
+#include "mte_def.h"
+
+#define ENTRY(name) \
+	.globl name ;\
+	.p2align 2;\
+	.type name, @function ;\
+name:
+
+#define ENDPROC(name) \
+	.size name, .-name ;
+
+	.text
+/*
+ * mte_insert_random_tag: Insert random tag and different from
+ *			 the orginal tag if source pointer has it.
+ * Input:

tools/testing/selftests/arm64/mte/mte_helper.S line 29

Quoted 10 lines
tools/testing/selftests/arm64/mte/mte_helper.S new file +10 -0
+ *		x0 - source pointer with a tag/no-tag
+ * Return:
+ *		x0 - pointer with random tag
+ */
+ENTRY(mte_insert_random_tag)
+	mov	x1, #0x0
+	gmi	x1, x0, x1
+	irg	x0, x0, x1
+	ret
+ENDPROC(mte_insert_random_tag)

What was the reason for gmi here? The test fails when you have an include mask of 0x8000 (exclude mask 0x7fff) and x0 has tag 0xf. In this case we exclude the only allowed tag here, so the CPU falls back to the default tag 0.

You can (a) stop the check_multiple_included_tags() earlier to have two allowed tags here, (b) clear the pointer old tag so that you don't end up in this scenario or (c) simply remove the gmi. My preference is the latter, we don't test the hardware here, we only want to check whether the kernel sets the GCR_EL1 correctly.

Quoted 3 lines

BTW, you also remove mov x1, #0, just:

	irg	x0, x0, xzr

Ah, removing gmi breaks the check_user_mem test as it occasionally gets the same tag when it expects to be different. I'll leave this to you to fix, maybe use two different functions, one with gmi and another without.

In addition, could you please add the PR_MTE_* definitions and PROT_MTE to a header file in the MTE kselftests (mte-def.h maybe)? They should be bracketed with #ifndef ... #endif. The reason is that we'd like to queue these patches on their own branch on top of vanilla 5.9-rc3 rather than on top of for-next/mte.

Thanks.

Signature · 1 line
Catalin
#5 lore raw

PATCH 2/6 kselftest/arm64: Verify mte tag inclusion via prctl

In reply to Amit Daniel Kachhap

This testcase verifies that the tag generated with "irg" instruction contains only included tags. This is done via prtcl call.

This test covers 4 scenarios,
* At least one included tag.
* More than one included tags.
* None included.
* All included.

---
 tools/testing/selftests/arm64/mte/.gitignore  |   1 +
 .../arm64/mte/check_tags_inclusion.c          | 183 ++++++++++++++++++
 2 files changed, 184 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/mte/check_tags_inclusion.c
tools/testing/selftests/arm64/mte/.gitignore +1 -0
index 3f8c1f6c82b9..c3fca255d3d6 100644
@@ -1 +1,2 @@
 check_buffer_fill
+check_tags_inclusion
tools/testing/selftests/arm64/mte/check_tags_inclusion.c new file +183 -0

183 lines not on this page · raw message

Signature · 1 line
2.17.1
#6 lore raw

PATCH 3/6 kselftest/arm64: Check forked child mte memory accessibility

In reply to Amit Daniel Kachhap

This test covers the mte memory behaviour of the forked process with different mapping properties and flags. It checks that all bytes of forked child memory are accessible with the same tag as that of the parent and memory accessed outside the tag range causes fault to occur.

---
 tools/testing/selftests/arm64/mte/.gitignore  |   1 +
 .../selftests/arm64/mte/check_child_memory.c  | 195 ++++++++++++++++++
 2 files changed, 196 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/mte/check_child_memory.c
tools/testing/selftests/arm64/mte/.gitignore +1 -0
index c3fca255d3d6..b5fcc0fb4d97 100644
@@ -1,2 +1,3 @@
 check_buffer_fill
 check_tags_inclusion
+check_child_memory
tools/testing/selftests/arm64/mte/check_child_memory.c new file +195 -0

195 lines not on this page · raw message

Signature · 1 line
2.17.1
#7 lore raw

PATCH 4/6 kselftest/arm64: Verify all different mmap MTE options

In reply to Amit Daniel Kachhap

This testcase checks the different unsupported/supported options for mmap if used with PROT_MTE memory protection flag. These checks are,

* Either pstate.tco enable or prctl PR_MTE_TCF_NONE option should not cause any tag mismatch faults.
* Different combinations of anonymous/file memory mmap, mprotect, sync/async error mode and private/shared mappings should work.
* mprotect should not be able to clear the PROT_MTE page property.

---
 tools/testing/selftests/arm64/mte/.gitignore  |   1 +
 .../selftests/arm64/mte/check_mmap_options.c  | 262 ++++++++++++++++++
 2 files changed, 263 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/mte/check_mmap_options.c
tools/testing/selftests/arm64/mte/.gitignore +1 -0
index b5fcc0fb4d97..79a215d3bbd0 100644
@@ -1,3 +1,4 @@
 check_buffer_fill
 check_tags_inclusion
 check_child_memory
+check_mmap_options
tools/testing/selftests/arm64/mte/check_mmap_options.c new file +262 -0

262 lines not on this page · raw message

Signature · 1 line
2.17.1
#8 lore raw

PATCH 5/6 kselftest/arm64: Verify KSM page merge for MTE pages

In reply to Amit Daniel Kachhap

Add a testcase to check that KSM should not merge pages containing same data with same/different MTE tag values.

This testcase has one positive tests and passes if page merging happens according to the above rule. It also saves and restores any modified ksm sysfs entries.

Cc Shuah Khan <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
 tools/testing/selftests/arm64/mte/.gitignore  |   1 +
 .../selftests/arm64/mte/check_ksm_options.c   | 131 ++++++++++++++++++
 2 files changed, 132 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/mte/check_ksm_options.c
tools/testing/selftests/arm64/mte/.gitignore +1 -0
index 79a215d3bbd0..44e9bfdaeca6 100644
@@ -2,3 +2,4 @@ check_buffer_fill
 check_tags_inclusion
 check_child_memory
 check_mmap_options
+check_ksm_options
tools/testing/selftests/arm64/mte/check_ksm_options.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.17.1
#9 lore raw

PATCH 6/6 kselftest/arm64: Check mte tagged user address in kernel

In reply to Amit Daniel Kachhap

Add a testcase to check that user address with valid/invalid mte tag works in kernel mode. This test verifies the kernel API's __arch_copy_from_user/__arch_copy_to_user works by considering if the user pointer has valid/invalid allocation tags.

In MTE sync mode a SIGSEV fault is generated if a user memory with invalid tag is accessed in kernel. In async mode no such fault occurs.

Cc Shuah Khan <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
---
 tools/testing/selftests/arm64/mte/.gitignore  |   1 +
 .../selftests/arm64/mte/check_user_mem.c      | 118 ++++++++++++++++++
 2 files changed, 119 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/mte/check_user_mem.c
tools/testing/selftests/arm64/mte/.gitignore +1 -0
index 44e9bfdaeca6..bc3ac63f3314 100644
@@ -3,3 +3,4 @@ check_tags_inclusion
 check_child_memory
 check_mmap_options
 check_ksm_options
+check_user_mem
tools/testing/selftests/arm64/mte/check_user_mem.c new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#10 lore raw

Re: [PATCH 6/6] kselftest/arm64: Check mte tagged user address in kernel

Quoted 8 lines · Amit Daniel Kachhap

Add a testcase to check that user address with valid/invalid mte tag works in kernel mode. This test verifies the kernel API's __arch_copy_from_user/__arch_copy_to_user works by considering if the user pointer has valid/invalid allocation tags.

In MTE sync mode a SIGSEV fault is generated if a user memory with invalid tag is accessed in kernel. In async mode no such fault occurs.

We don't generate a SIGSEGV for faults in the uaccess routines. The kernel simply returns less copied bytes than what was requested or -1 and setting errno.

BTW, Qemu has a bug and it reports the wrong exception class (lower DABT) for a tag check fault while in the uaccess routines, leading to kernel panic (bad mode in synchronous abort handler).

Quoted 25 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +25 -0
+static int check_usermem_access_fault(int mem_type, int mode, int mapping)
+{
+	int fd, ret, i, err;
+	char val = 'A';
+	size_t len, read_len;
+	void *ptr, *ptr_next;
+	bool fault;
+
+	len = 2 * page_sz;
+	err = KSFT_FAIL;
+	/*
+	 * Accessing user memory in kernel with invalid tag should fault in sync
+	 * mode but may not fault in async mode as per the implemented MTE
+	 * support in Arm64 kernel.
+	 */
+	if (mode == MTE_ASYNC_ERR)
+		fault = false;
+	else
+		fault = true;
+	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
+	fd = create_temp_file();
+	if (fd == -1)
+		return KSFT_FAIL;
+	for (i = 0; i < len; i++)
+		write(fd, &val, sizeof(val));

tools/testing/selftests/arm64/mte/check_user_mem.c line 56

Quoted 10 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +10 -0
+	lseek(fd, 0, 0);
+	ptr = mte_allocate_memory(len, mem_type, mapping, true);
+	if (check_allocated_memory(ptr, len, mem_type, true) != KSFT_PASS) {
+		close(fd);
+		return KSFT_FAIL;
+	}
+	mte_initialize_current_context(mode, (uintptr_t)ptr, len);
+	/* Copy from file into buffer with valid tag */
+	read_len = read(fd, ptr, len);
+	ret = errno;

My reading of the man page is that errno is set only if read() returns -1.

Quoted 11 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +11 -0
+	mte_wait_after_trig();
+	if ((cur_mte_cxt.fault_valid == true) || ret == EFAULT || read_len < len)
+		goto usermem_acc_err;
+	/* Verify same pattern is read */
+	for (i = 0; i < len; i++)
+		if (*(char *)(ptr + i) != val)
+			break;
+	if (i < len)
+		goto usermem_acc_err;
+
+	/* Tag the next half of memory with different value */

tools/testing/selftests/arm64/mte/check_user_mem.c line 77

Quoted 10 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +10 -0
+	ptr_next = (void *)((unsigned long)ptr + page_sz);
+	ptr_next = mte_insert_tags(ptr_next, page_sz);
+	if (!ptr_next)
+		goto usermem_acc_err;
+	lseek(fd, 0, 0);
+	/* Copy from file into buffer with invalid tag */
+	read_len = read(fd, ptr, len);
+	ret = errno;
+	mte_wait_after_trig();
+	if ((fault == true) &&

Nitpick: just use "if (fault &&), it's a bool already.

tools/testing/selftests/arm64/mte/check_user_mem.c line 81

Quoted 4 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +4 -0
+	    (cur_mte_cxt.fault_valid == true || ret == EFAULT || read_len < len)) {
+		err = KSFT_PASS;
+	} else if ((fault == false) &&
+		   (cur_mte_cxt.fault_valid == false && read_len == len)) {

Same here, !fault, !cur_mte_cxt.fault_valid.

Signature · 1 line
Catalin
#11 lore raw

Re: [PATCH 6/6] kselftest/arm64: Check mte tagged user address in kernel

Quoted 6 lines · Catalin Marinas
Quoted 5 lines · Amit Daniel Kachhap

Add a testcase to check that user address with valid/invalid mte tag works in kernel mode. This test verifies the kernel API's __arch_copy_from_user/__arch_copy_to_user works by considering if the user pointer has valid/invalid allocation tags.

Quoted 7 lines
Quoted 3 lines

In MTE sync mode a SIGSEV fault is generated if a user memory with invalid tag is accessed in kernel. In async mode no such fault occurs.

We don't generate a SIGSEGV for faults in the uaccess routines. The kernel simply returns less copied bytes than what was requested or -1 and setting errno.

ok. I will update in the next iteration.

Quoted 4 lines

BTW, Qemu has a bug and it reports the wrong exception class (lower DABT) for a tag check fault while in the uaccess routines, leading to kernel panic (bad mode in synchronous abort handler).

Yes I am also seeing this.

Quoted 31 lines
Quoted 31 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +30 -0
+static int check_usermem_access_fault(int mem_type, int mode, int mapping)
+{
+	int fd, ret, i, err;
+	char val = 'A';
+	size_t len, read_len;
+	void *ptr, *ptr_next;
+	bool fault;
+
+	len = 2 * page_sz;
+	err = KSFT_FAIL;
+	/*
+	 * Accessing user memory in kernel with invalid tag should fault in sync
+	 * mode but may not fault in async mode as per the implemented MTE
+	 * support in Arm64 kernel.
+	 */
+	if (mode == MTE_ASYNC_ERR)
+		fault = false;
+	else
+		fault = true;
+	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
+	fd = create_temp_file();
+	if (fd == -1)
+		return KSFT_FAIL;
+	for (i = 0; i < len; i++)
+		write(fd, &val, sizeof(val));
+	lseek(fd, 0, 0);
+	ptr = mte_allocate_memory(len, mem_type, mapping, true);
+	if (check_allocated_memory(ptr, len, mem_type, true) != KSFT_PASS) {
+		close(fd);
+		return KSFT_FAIL;
Quoted 8 lines

tools/testing/selftests/arm64/mte/check_user_mem.c line 56

Quoted 5 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +5 -0
+	}
+	mte_initialize_current_context(mode, (uintptr_t)ptr, len);
+	/* Copy from file into buffer with valid tag */
+	read_len = read(fd, ptr, len);
+	ret = errno;

My reading of the man page is that errno is set only if read() returns -1.

Yes. The checks should be optimized here.

Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +15 -0
+	mte_wait_after_trig();
+	if ((cur_mte_cxt.fault_valid == true) || ret == EFAULT || read_len < len)
+		goto usermem_acc_err;
+	/* Verify same pattern is read */
+	for (i = 0; i < len; i++)
+		if (*(char *)(ptr + i) != val)
+			break;
+	if (i < len)
+		goto usermem_acc_err;
+
+	/* Tag the next half of memory with different value */
+	ptr_next = (void *)((unsigned long)ptr + page_sz);
+	ptr_next = mte_insert_tags(ptr_next, page_sz);
+	if (!ptr_next)
+		goto usermem_acc_err;
Quoted 8 lines

tools/testing/selftests/arm64/mte/check_user_mem.c line 77

Quoted 6 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +6 -0
+	lseek(fd, 0, 0);
+	/* Copy from file into buffer with invalid tag */
+	read_len = read(fd, ptr, len);
+	ret = errno;
+	mte_wait_after_trig();
+	if ((fault == true) &&

Nitpick: just use "if (fault &&), it's a bool already.

ok.

Quoted 7 lines

tools/testing/selftests/arm64/mte/check_user_mem.c line 81

Quoted 5 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +4 -0
+	    (cur_mte_cxt.fault_valid == true || ret == EFAULT || read_len < len)) {
+		err = KSFT_PASS;
+	} else if ((fault == false) &&
+		   (cur_mte_cxt.fault_valid == false && read_len == len)) {

Same here, !fault, !cur_mte_cxt.fault_valid.

ok.

Quoted 1 line
maintainer
#12 lore raw

Re: [PATCH 6/6] kselftest/arm64: Check mte tagged user address in kernel

Quoted 41 lines · Amit Kachhap
Quoted 31 lines · Catalin Marinas
Quoted 30 lines · Amit Daniel Kachhap
tools/testing/selftests/arm64/mte/check_user_mem.c new file +30 -0
+static int check_usermem_access_fault(int mem_type, int mode, int mapping)
+{
+	int fd, ret, i, err;
+	char val = 'A';
+	size_t len, read_len;
+	void *ptr, *ptr_next;
+	bool fault;
+
+	len = 2 * page_sz;
+	err = KSFT_FAIL;
+	/*
+	 * Accessing user memory in kernel with invalid tag should fault in sync
+	 * mode but may not fault in async mode as per the implemented MTE
+	 * support in Arm64 kernel.
+	 */
+	if (mode == MTE_ASYNC_ERR)
+		fault = false;
+	else
+		fault = true;
+	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
+	fd = create_temp_file();
+	if (fd == -1)
+		return KSFT_FAIL;
+	for (i = 0; i < len; i++)
+		write(fd, &val, sizeof(val));
+	lseek(fd, 0, 0);
+	ptr = mte_allocate_memory(len, mem_type, mapping, true);
+	if (check_allocated_memory(ptr, len, mem_type, true) != KSFT_PASS) {
+		close(fd);
+		return KSFT_FAIL;
Quoted 8 lines

tools/testing/selftests/arm64/mte/check_user_mem.c line 56

Quoted 5 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +5 -0
+	}
+	mte_initialize_current_context(mode, (uintptr_t)ptr, len);
+	/* Copy from file into buffer with valid tag */
+	read_len = read(fd, ptr, len);
+	ret = errno;

My reading of the man page is that errno is set only if read() returns -1.

Quoted 1 line

Yes. The checks should be optimized here.

It's not about optimisation but correctness. The errno man page states that errno is only relevant if the syscall returns -1. So it may potentially hold a stale value (e.g. EFAULT) in case of read() success but the check below fails anyway:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
tools/testing/selftests/arm64/mte/check_user_mem.c new file +3 -0
+	mte_wait_after_trig();
+	if ((cur_mte_cxt.fault_valid == true) || ret == EFAULT || read_len < len)
+		goto usermem_acc_err;
Signature · 1 line
Catalin