Skip to content

Conversation

@f0rget-the-sad
Copy link

Without multithreading only two stacks present: ISR and main. As any stack they also could overflow, so it make sense to add stack guard for them also.

Remove stack guard dependency on multithreading and mark Z_RISCV_STACK_GUARD_SIZE bytes at the beginning of stack as read-only region with PMP entry.

cfriedt
cfriedt previously approved these changes Oct 29, 2024
ycsin
ycsin previously requested changes Nov 6, 2024
@cfriedt
Copy link
Member

cfriedt commented Nov 7, 2024

We need to have some guarantee that this code has test coverage.

@ycsin ycsin dismissed their stale review November 8, 2024 09:56

addressed

Copy link
Member

@fkokosinski fkokosinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code itself LGTM, but I agree with @cfriedt - we'd need to have some testing for this.

There're at least 3 places where we could add/modify a test:

  1. tests/kernel/threads/no-multithreading - it's already enabled on RISC-V QEMU-based targets, but I'm not sure if this is the right place to add memory protection related tests. Probably not.
  2. tests/kernel/mem_protect/mem_protect - we could add a RISC-V specific no multithreading testcase here
  3. we could add a separate arch-specific test in e.g. tests/arch/riscv/pmp (ARM has one in zephyr/tests/arch/arm/arm_mem_protect).

It should probably be both 2 and 3.

@f0rget-the-sad
Copy link
Author

The code itself LGTM, but I agree with @cfriedt - we'd need to have some testing for this.

There're at least 3 places where we could add/modify a test:

1. `tests/kernel/threads/no-multithreading` - it's already enabled on RISC-V QEMU-based targets, but I'm not sure if this is the right place to add memory protection related tests. Probably not.

2. `tests/kernel/mem_protect/mem_protect` - we could add a RISC-V specific no multithreading testcase here

3. we could add a separate arch-specific test in e.g. `tests/arch/riscv/pmp` (ARM has one in `zephyr/tests/arch/arm/arm_mem_protect`).

It should probably be both 2 and 3.

Thank you for pointing in right direction, I'll work on extending the test cases in next commit.

Without multithreading only two stacks present: ISR and main.
As any stack they also could overflow, so it make sense to add stack
guard for them also.

Remove stack guard dependency on multithreading and mark
`Z_RISCV_STACK_GUARD_SIZE` bytes at the beginning of stack as read-only
region with PMP entry.

Signed-off-by: Volodymyr Fialko <[email protected]>
@f0rget-the-sad f0rget-the-sad force-pushed the stack-guard-no-threading branch from 0cf9211 to d76fdd2 Compare November 18, 2024 08:58
Copy link
Member

@fkokosinski fkokosinski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for adding the test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... but if we want to go this route, then I'd rather use EXTRA_CFLAGS here and change VIA_TWISTER to something sounding less generic

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@fkokosinski fkokosinski requested review from cfriedt and ycsin November 18, 2024 12:46
Test if PMP protected regions prevents write access.

Signed-off-by: Volodymyr Fialko <[email protected]>
@f0rget-the-sad f0rget-the-sad force-pushed the stack-guard-no-threading branch from d76fdd2 to 2b13028 Compare November 18, 2024 13:22

tests:
arch.riscv.pmp.no-mt.isr-stack-guard:
extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=0
Copy link
Member

@ycsin ycsin Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could have used Kconfig instead? i.e. CONFIG_TEST_ISR_STACK_GUARD / CONFIG_TEST_MAIN_STACK_GUARD, and enable it here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Kconfig option usually meant to allow user to tweak something.
Here it's already possible to select different tests by name, so I don't see benefits in using Kconfig.

@nashif nashif merged commit 1310856 into zephyrproject-rtos:main Nov 20, 2024
30 checks passed
@github-actions
Copy link

Hi @f0rget-the-sad!
Congratulations on getting your very first Zephyr pull request merged 🎉🥳. This is a fantastic achievement, and we're thrilled to have you as part of our community!

To celebrate this milestone and showcase your contribution, we'd love to award you the Zephyr Technical Contributor badge. If you're interested, please claim your badge by filling out this form: Claim Your Zephyr Badge.

Thank you for your valuable input, and we look forward to seeing more of your contributions in the future! 🪁

@f0rget-the-sad f0rget-the-sad deleted the stack-guard-no-threading branch November 20, 2024 16:20
@fabiobaltieri fabiobaltieri mentioned this pull request Nov 20, 2024
@fabiobaltieri
Copy link
Member

Hi @f0rget-the-sad, unfortunately this PR broke the project CI run on main (https://github.com/zephyrproject-rtos/zephyr/actions/runs/11934344226), the fix is not obvious to me so I've opened a revert PR #81673.

Would you be able to troubleshoot and re-submit a new version that works in CI? (or open a hotfix if that's obvious to you).

Apologies for the extra work.

@f0rget-the-sad
Copy link
Author

This PR passed all checks triggered by CI: https://github.com/zephyrproject-rtos/zephyr/actions/runs/11893366338
I guess main CI runs even more tests, so question is: how do I trigger the main CI before merge?

@fabiobaltieri
Copy link
Member

fabiobaltieri commented Nov 20, 2024

I guess main CI runs even more tests, so question is: how do I trigger the main CI before merge?

Yeah the test sets can be different, you can get the list of failed tests from https://github.com/zephyrproject-rtos/zephyr/runs/33270031021 and run those manually, for example for qemu_riscv64/qemu_virt_riscv64:tests/kernel/fatal/no-multithreading/kernel.no-mt.arbitrary_reason you can use

$ west build -p -b qemu_riscv64/qemu_virt_riscv64 -T tests/kernel/fatal/no-multithreading/kernel.no-mt.arbitrary_reason
$ west build -t run
...
 FAIL - test_fatal in 0.000 seconds
===================================================================
===================================================================
PROJECT EXECUTION FAILED

@f0rget-the-sad
Copy link
Author

Ok, I manually tested tests/kernel/fatal/no-multithreading, tests/kernel/threads/no-multithreading and tests/lib/multi_heap (seems like all failures reported by main CI)with this hotfix: #81676
Let me know if there are other failures.

@fabiobaltieri
Copy link
Member

@f0rget-the-sad thanks for the quick turnaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Kernel area: RISCV RISCV Architecture (32-bit & 64-bit)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants