2024-02-14 12:42:02

by Paul Heidekrüger

[permalink] [raw]
Subject: [PATCH RFC] kunit: tool: add 'mte=on' qemu arg on arm64

Hi!

I was running some KASan tests with kunit.py recently and noticed that
when KASan is run in hw tags mode, we manually have to add the required
`mte=on` option to kunit_tool's qemu invocation, as the tests will
otherwise crash.

To make life easier, I was looking into ways for kunit.py to recognise
when MTE support was required and set the option automatically.

All solutions I could come up with for having kunit_tool conditionally
pass `mte=on` to qemu, either entailed duplicate code or required
parsing of kernel's config file again. I was working under the
assumption that only after configuring the kernel we would know whether
the 'mte=on' option was necessary, as CONFIG_ARM64_MTE is not visible
before.

Only afterwads did I realise that the qemu arm64 config that kunit_tool
falls back on, uses the `virt` machine, which supports MTE in any case.
So, could it be as easy as just adding the `mte=on` option to
kunit_tool's arm64 config? Would this be a welcome addition?

What do you think?

Many thanks,
Paul

Signed-off-by: Paul Heidekrüger <[email protected]>
---
tools/testing/kunit/qemu_configs/arm64.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/kunit/qemu_configs/arm64.py b/tools/testing/kunit/qemu_configs/arm64.py
index d3ff27024755..a525f7e1093b 100644
--- a/tools/testing/kunit/qemu_configs/arm64.py
+++ b/tools/testing/kunit/qemu_configs/arm64.py
@@ -9,4 +9,4 @@ CONFIG_SERIAL_AMBA_PL011_CONSOLE=y''',
qemu_arch='aarch64',
kernel_path='arch/arm64/boot/Image.gz',
kernel_command_line='console=ttyAMA0',
- extra_qemu_params=['-machine', 'virt', '-cpu', 'max,pauth-impdef=on'])
+ extra_qemu_params=['-machine', 'virt,mte=on', '-cpu', 'max,pauth-impdef=on'])
--
2.40.1



2024-02-20 00:46:30

by David Gow

[permalink] [raw]
Subject: Re: [PATCH RFC] kunit: tool: add 'mte=on' qemu arg on arm64

On Wed, 14 Feb 2024 at 20:41, Paul Heidekrüger <[email protected]> wrote:
>
> Hi!
>
> I was running some KASan tests with kunit.py recently and noticed that
> when KASan is run in hw tags mode, we manually have to add the required
> `mte=on` option to kunit_tool's qemu invocation, as the tests will
> otherwise crash.
>
> To make life easier, I was looking into ways for kunit.py to recognise
> when MTE support was required and set the option automatically.
>
> All solutions I could come up with for having kunit_tool conditionally
> pass `mte=on` to qemu, either entailed duplicate code or required
> parsing of kernel's config file again. I was working under the
> assumption that only after configuring the kernel we would know whether
> the 'mte=on' option was necessary, as CONFIG_ARM64_MTE is not visible
> before.
>
> Only afterwads did I realise that the qemu arm64 config that kunit_tool
> falls back on, uses the `virt` machine, which supports MTE in any case.
> So, could it be as easy as just adding the `mte=on` option to
> kunit_tool's arm64 config? Would this be a welcome addition?
>
> What do you think?
>
> Many thanks,
> Paul
>
> Signed-off-by: Paul Heidekrüger <[email protected]>
> ---

I think this is fine. I'd be a little bit concerned if this were only
supported in newer qemu versions, but it seems to go back to 6.2, so
should be okay. I think it's better to just enable it unconditionally
by default rather than trying to parse the config.

The KASAN tests seemed to work fine with HW tags in my testing here. I
do wonder if there's a way to make the tests skip themselves if MTE
isn't available: is there a way of doing a runtime check for this?

Regardless, this is:
Reviewed-by: David Gow <[email protected]>

-- David

> tools/testing/kunit/qemu_configs/arm64.py | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/testing/kunit/qemu_configs/arm64.py b/tools/testing/kunit/qemu_configs/arm64.py
> index d3ff27024755..a525f7e1093b 100644
> --- a/tools/testing/kunit/qemu_configs/arm64.py
> +++ b/tools/testing/kunit/qemu_configs/arm64.py
> @@ -9,4 +9,4 @@ CONFIG_SERIAL_AMBA_PL011_CONSOLE=y''',
> qemu_arch='aarch64',
> kernel_path='arch/arm64/boot/Image.gz',
> kernel_command_line='console=ttyAMA0',
> - extra_qemu_params=['-machine', 'virt', '-cpu', 'max,pauth-impdef=on'])
> + extra_qemu_params=['-machine', 'virt,mte=on', '-cpu', 'max,pauth-impdef=on'])
> --
> 2.40.1
>


Attachments:
smime.p7s (3.92 kB)
S/MIME Cryptographic Signature

2024-02-23 20:52:32

by Paul Heidekrüger

[permalink] [raw]
Subject: Re: [PATCH RFC] kunit: tool: add 'mte=on' qemu arg on arm64

On 20.02.2024 08:46, David Gow wrote:
> On Wed, 14 Feb 2024 at 20:41, Paul Heidekrüger <[email protected]> wrote:
> >
> > Hi!
> >
> > I was running some KASan tests with kunit.py recently and noticed that
> > when KASan is run in hw tags mode, we manually have to add the required
> > `mte=on` option to kunit_tool's qemu invocation, as the tests will
> > otherwise crash.
> >
> > To make life easier, I was looking into ways for kunit.py to recognise
> > when MTE support was required and set the option automatically.
> >
> > All solutions I could come up with for having kunit_tool conditionally
> > pass `mte=on` to qemu, either entailed duplicate code or required
> > parsing of kernel's config file again. I was working under the
> > assumption that only after configuring the kernel we would know whether
> > the 'mte=on' option was necessary, as CONFIG_ARM64_MTE is not visible
> > before.
> >
> > Only afterwads did I realise that the qemu arm64 config that kunit_tool
> > falls back on, uses the `virt` machine, which supports MTE in any case.
> > So, could it be as easy as just adding the `mte=on` option to
> > kunit_tool's arm64 config? Would this be a welcome addition?
> >
> > What do you think?
> >
> > Many thanks,
> > Paul
> >
> > Signed-off-by: Paul Heidekrüger <[email protected]>
> > ---
>
> I think this is fine. I'd be a little bit concerned if this were only
> supported in newer qemu versions, but it seems to go back to 6.2, so
> should be okay. I think it's better to just enable it unconditionally
> by default rather than trying to parse the config.
>
> The KASAN tests seemed to work fine with HW tags in my testing here. I
> do wonder if there's a way to make the tests skip themselves if MTE
> isn't available: is there a way of doing a runtime check for this?

Huh, interesting. Even though "mte=on" isn't set on your side?

I get the following output without the MTE patch.

➜ ./tools/testing/kunit/kunit.py run --kunitconfig=mm/kasan/.kunitconfig --arch=arm64
[14:08:11] Configuring KUnit Kernel ...
[14:08:11] Building KUnit Kernel ...
Populating config with:
$ make ARCH=arm64 O=.kunit olddefconfig
Building with:
$ make ARCH=arm64 O=.kunit --jobs=8
[14:08:23] Starting KUnit Kernel (1/1)...
[14:08:23] ============================================================
Running tests with:
$ qemu-system-aarch64 -nodefaults -m 1024 -kernel .kunit/arch/arm64/boot/Image.gz -append 'kunit.enable=1 console=ttyAMA0 kunit_shutdown=reboot' -no-reboot -nographic -serial stdio -machine virt -cpu max,pauth-impdef=on
[14:08:23] kasan: test: Can't run KASAN tests with KASAN disabled
[14:08:23] # kasan: # failed to initialize (-1)
[14:08:23] [FAILED] kasan
[14:08:23] ============================================================
[14:08:23] Testing complete. Ran 1 tests: failed: 1
[14:08:24] Elapsed time: 12.374s total, 0.001s configuring, 11.937s building, 0.382s running

Where the mentioned .kunitconfig has the following options set for KASan.

CONFIG_KUNIT=y
CONFIG_KUNIT_ALL_TESTS=n

CONFIG_FTRACE=y
CONFIG_STACK_TRACER=y

CONFIG_KASAN=y
CONFIG_KASAN_HW_TAGS=y
CONFIG_KASAN_KUNIT_TEST=y

With the MTE patch from my previous email, everything works just fine.

Based on that, do you have a guess why it's working for you and why it isn't for
me?

> Regardless, this is:
> Reviewed-by: David Gow <[email protected]>

Thanks! I'll be sending a non-RFC patch shortly.

Many thanks,
Paul


2024-02-23 21:08:16

by Paul Heidekrüger

[permalink] [raw]
Subject: [PATCH v1] kunit: tool: add 'mte=on' qemu arg on arm64

Tests relying on the ARM Memory Tagging Extension (MTE) may crash when
the corresponding qemu option, which is available since qemu 6.2, is not
explicitly passed to kunit_tool via the command line.

To make life easier, enable MTE by default for kunit_tool's arm64
qemu config.

Link: https://lore.kernel.org/all/[email protected]/T/#u
Reviewed-by: David Gow <[email protected]>
Signed-off-by: Paul Heidekrüger <[email protected]>
---
tools/testing/kunit/qemu_configs/arm64.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/kunit/qemu_configs/arm64.py b/tools/testing/kunit/qemu_configs/arm64.py
index d3ff27024755..a525f7e1093b 100644
--- a/tools/testing/kunit/qemu_configs/arm64.py
+++ b/tools/testing/kunit/qemu_configs/arm64.py
@@ -9,4 +9,4 @@ CONFIG_SERIAL_AMBA_PL011_CONSOLE=y''',
qemu_arch='aarch64',
kernel_path='arch/arm64/boot/Image.gz',
kernel_command_line='console=ttyAMA0',
- extra_qemu_params=['-machine', 'virt', '-cpu', 'max,pauth-impdef=on'])
+ extra_qemu_params=['-machine', 'virt,mte=on', '-cpu', 'max,pauth-impdef=on'])
--
2.40.1