diff mbox series

[v1,4/4] kselftest/arm64: Add simple test for MTE prctl

Message ID 20220310144335.2097457-5-broonie@kernel.org (mailing list archive)
State New
Headers show
Series kselftest/arm64: Miscelaneous MTE test updates | expand

Commit Message

Mark Brown March 10, 2022, 2:43 p.m. UTC
The current tests use the prctls for various things but there's no
coverage of the edges of the interface so add some basics. This isn't
hugely useful as it is (it originally had some coverage for the
combinations with asymmetric mode but we removed the prctl() for that)
but it might be a helpful starting point for future work, for example
covering error handling.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 tools/testing/selftests/arm64/mte/.gitignore  |   1 +
 .../testing/selftests/arm64/mte/check_prctl.c | 123 ++++++++++++++++++
 2 files changed, 124 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/mte/check_prctl.c

Comments

Joey Gouly March 10, 2022, 3:33 p.m. UTC | #1
Hi Mark,

On Thu, Mar 10, 2022 at 02:43:35PM +0000, Mark Brown wrote:
> The current tests use the prctls for various things but there's no
> coverage of the edges of the interface so add some basics. This isn't
> hugely useful as it is (it originally had some coverage for the
> combinations with asymmetric mode but we removed the prctl() for that)
> but it might be a helpful starting point for future work, for example
> covering error handling.
> 
> Signed-off-by: Mark Brown <broonie@kernel.org>
> ---

[..]

> diff --git a/tools/testing/selftests/arm64/mte/check_prctl.c b/tools/testing/selftests/arm64/mte/check_prctl.c
> new file mode 100644
> index 000000000000..500aefc5d7cd
> --- /dev/null
> +++ b/tools/testing/selftests/arm64/mte/check_prctl.c
> @@ -0,0 +1,123 @@
> +// SPDX-License-Identifier: GPL-2.0
> +// Copyright (C) 2022 ARM Limited
> +
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <string.h>
> +
> +#include <sys/auxv.h>
> +#include <sys/prctl.h>
> +
> +#include <asm/hwcap.h>
> +
> +#include "kselftest.h"
> +
> +static bool system_has_mte;

This looks unused (apart from being set in main()).

Thanks,
Joey
Mark Brown March 10, 2022, 3:37 p.m. UTC | #2
On Thu, Mar 10, 2022 at 03:33:06PM +0000, Joey Gouly wrote:
> On Thu, Mar 10, 2022 at 02:43:35PM +0000, Mark Brown wrote:

> > +static bool system_has_mte;

> This looks unused (apart from being set in main()).

It is, it was used in the asymmetric bits of the test.
Shuah Khan March 10, 2022, 8:07 p.m. UTC | #3
On 3/10/22 7:43 AM, Mark Brown wrote:
> The current tests use the prctls for various things but there's no
> coverage of the edges of the interface so add some basics. This isn't
> hugely useful as it is (it originally had some coverage for the
> combinations with asymmetric mode but we removed the prctl() for that)
> but it might be a helpful starting point for future work, for example
> covering error handling.
> 
> Signed-off-by: Mark Brown <broonie@kernel.org>
> ---
>   tools/testing/selftests/arm64/mte/.gitignore  |   1 +
>   .../testing/selftests/arm64/mte/check_prctl.c | 123 ++++++++++++++++++
>   2 files changed, 124 insertions(+)
>   create mode 100644 tools/testing/selftests/arm64/mte/check_prctl.c
> 

Looks good to me.

Reviewed-by: Shuah Khan <skhan@linuxfoundation.org>

thanks,
-- Shuah
diff mbox series

Patch

diff --git a/tools/testing/selftests/arm64/mte/.gitignore b/tools/testing/selftests/arm64/mte/.gitignore
index d1fe4ddf1669..052d0f9f92b3 100644
--- a/tools/testing/selftests/arm64/mte/.gitignore
+++ b/tools/testing/selftests/arm64/mte/.gitignore
@@ -3,5 +3,6 @@  check_gcr_el1_cswitch
 check_tags_inclusion
 check_child_memory
 check_mmap_options
+check_prctl
 check_ksm_options
 check_user_mem
diff --git a/tools/testing/selftests/arm64/mte/check_prctl.c b/tools/testing/selftests/arm64/mte/check_prctl.c
new file mode 100644
index 000000000000..500aefc5d7cd
--- /dev/null
+++ b/tools/testing/selftests/arm64/mte/check_prctl.c
@@ -0,0 +1,123 @@ 
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2022 ARM Limited
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/auxv.h>
+#include <sys/prctl.h>
+
+#include <asm/hwcap.h>
+
+#include "kselftest.h"
+
+static bool system_has_mte;
+
+static int set_tagged_addr_ctrl(int val)
+{
+	int ret;
+
+	ret = prctl(PR_SET_TAGGED_ADDR_CTRL, val, 0, 0, 0);
+	if (ret < 0)
+		ksft_print_msg("PR_SET_TAGGED_ADDR_CTRL: failed %d %d (%s)\n",
+			       ret, errno, strerror(errno));
+	return ret;
+}
+
+static int get_tagged_addr_ctrl(void)
+{
+	int ret;
+
+	ret = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
+	if (ret < 0)
+		ksft_print_msg("PR_GET_TAGGED_ADDR_CTRL failed: %d %d (%s)\n",
+			       ret, errno, strerror(errno));
+	return ret;
+}
+
+/*
+ * Read the current mode without having done any configuration, should
+ * run first.
+ */
+void check_basic_read(void)
+{
+	int ret;
+
+	ret = get_tagged_addr_ctrl();
+	if (ret < 0) {
+		ksft_test_result_fail("check_basic_read\n");
+		return;
+	}
+
+	if (ret & PR_MTE_TCF_SYNC)
+		ksft_print_msg("SYNC enabled\n");
+	if (ret & PR_MTE_TCF_ASYNC)
+		ksft_print_msg("ASYNC enabled\n");
+
+	/* Any configuration is valid */
+	ksft_test_result_pass("check_basic_read\n");
+}
+
+/*
+ * Attempt to set a specified combination of modes.
+ */
+void set_mode_test(const char *name, int hwcap2, int mask)
+{
+	int ret;
+
+	if ((getauxval(AT_HWCAP2) & hwcap2) != hwcap2) {
+		ksft_test_result_skip("%s\n", name);
+		return;
+	}
+
+	ret = set_tagged_addr_ctrl(mask);
+	if (ret < 0) {
+		ksft_test_result_fail("%s\n", name);
+		return;
+	}
+
+	ret = get_tagged_addr_ctrl();
+	if (ret < 0) {
+		ksft_test_result_fail("%s\n", name);
+		return;
+	}
+
+	if ((ret & PR_MTE_TCF_MASK) == mask) {
+		ksft_test_result_pass("%s\n", name);
+	} else {
+		ksft_print_msg("Got %x, expected %x\n",
+			       (ret & PR_MTE_TCF_MASK), mask);
+		ksft_test_result_fail("%s\n", name);
+	}
+}
+
+struct mte_mode {
+	int mask;
+	int hwcap2;
+	const char *name;
+} mte_modes[] = {
+	{ PR_MTE_TCF_NONE,  0,          "NONE"  },
+	{ PR_MTE_TCF_SYNC,  HWCAP2_MTE, "SYNC"  },
+	{ PR_MTE_TCF_ASYNC, HWCAP2_MTE, "ASYNC" },
+	{ PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC,  HWCAP2_MTE, "SYNC+ASYNC"  },
+};
+
+int main(void)
+{
+	int i;
+
+	system_has_mte = getauxval(AT_HWCAP2) & HWCAP2_MTE;
+
+	ksft_print_header();
+	ksft_set_plan(5);
+
+	check_basic_read();
+	for (i = 0; i < ARRAY_SIZE(mte_modes); i++)
+		set_mode_test(mte_modes[i].name, mte_modes[i].hwcap2,
+			      mte_modes[i].mask);
+
+	ksft_print_cnts();
+
+	return 0;
+}