Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ST boards: Fix sleep tracing #13034

Merged
merged 1 commit into from
Jun 5, 2020

Conversation

hugueskamba
Copy link
Collaborator

@hugueskamba hugueskamba commented May 29, 2020

Summary of changes

Prevent singleton lock if the RTOS is not yet ready.

lp_ticker is used during the RTOS initialization process.
ST lp_ticker implementation calls sleep functions
which in turn attempts to print to the console when sleep tracing
is enabled. Console initialization attempts to lock the singleton mutex.

fixes #11497
fixes #12593

To test:

  1. Clone mbed-os-example-blinky.
  2. Build, program, and open a serial terminal with: mbed compile -t <TOOLCHAIN> -m <ST_TARGET> -f --sterm -DMBED_SLEEP_TRACING_ENABLED.
  3. Sleep tracing is enabled (and printing on the console), the board does not reboot.

Impact of changes

Sleep tracing can be enabled with ST boards.

Migration actions required

N/A

Documentation

N/A


Pull request type

[X] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[X] Covered by existing mbed-os tests (Greentea or Unittest)
[] Tests / results supplied as part of this PR

Reviewers

@kjbracey-arm @c1728p9 @korjaa @LMESTM @evedon


Copy link
Contributor

@0xc0170 0xc0170 left a comment

Choose a reason for hiding this comment

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

if we remove usage of singleton_mutex_id in this file, then does it still need to be in mbed rtos start ? it is defined there and initialized but not used. Only used here in this file.

Good to have these issues fixed!

@kjbracey
Copy link
Contributor

I'm a bit wary of this - osKernelGetState is a much heavier operation than reading a variable. But on the other hand, singleton_lock is only normally used after an atomic check, I think.

But what is the actual problem with the original code? I still don't understand the failure mode from our discussion. Is singleton_mutex_id getting set before the kernel is started?

@kjbracey
Copy link
Contributor

kjbracey commented May 29, 2020

Another possibility along the same lines - how about if the crashy thing we put into the mutex error callback chose not to crash if the kernel isn't running?

That would have the general effect of Mutex::lock and Mutex::unlock quietly doing nothing while called in weird situations.

Maybe that would cover up too much. But the main thing we were trying to catch with the crash was the "call from IRQ" mistake - that's definitely a bogus thing to do. But if the kernel is not running, then a failed mutex lock attempt is not really harmful. (Assuming they unlock before the kernel is started again).

@kjbracey
Copy link
Contributor

kjbracey commented May 29, 2020

Is singleton_mutex_id getting set before the kernel is started?

Yes, it is. Okay, then setting it at the start of mbed_start would be another approach. I think that seems reasonable - we don't run the general C++ setup until then, so doing it as the first thing before mbed_toolchain_init() seems like it will be fine.

We're already assuming single-threaded at least until that call is made - but that could spawn new threads in object constructors.

@ciarmcom ciarmcom requested review from c1728p9, evedon, kjbracey, korjaa, LMESTM and a team May 29, 2020 13:00
@ciarmcom
Copy link
Member

@hugueskamba, thank you for your changes.
@korjaa @LMESTM @evedon @c1728p9 @kjbracey-arm @ARMmbed/mbed-os-core @ARMmbed/mbed-os-maintainers please review.

@hugueskamba hugueskamba force-pushed the hk_fix_sleep_tracing branch 2 times, most recently from e79ae2f to 37d5713 Compare June 1, 2020 10:26
@hugueskamba
Copy link
Collaborator Author

Is singleton_mutex_id getting set before the kernel is started?

Yes, it is. Okay, then setting it at the start of mbed_start would be another approach. I think that seems reasonable - we don't run the general C++ setup until then, so doing it as the first thing before mbed_toolchain_init() seems like it will be fine.

We're already assuming single-threaded at least until that call is made - but that could spawn new threads in object constructors.

Thanks. Done.

rtos/source/TARGET_CORTEX/mbed_boot.c Outdated Show resolved Hide resolved
rtos/source/TARGET_CORTEX/mbed_boot.c Outdated Show resolved Hide resolved
@mergify mergify bot dismissed kjbracey’s stale review June 1, 2020 13:31

Pull request has been modified.

int main(void);
static void mbed_cpy_nvic(void);
void mbed_rtos_init_singleton_mutex(void);
Copy link
Contributor

@kjbracey kjbracey Jun 1, 2020

Choose a reason for hiding this comment

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

This can be properly publicly declared in mbed_boot.h. mbed_rtx_rtos.c doesn't seem to have its own header, but it includes mbed_boot.h, and that's where mbed_rtos_start is declared.

(Although maybe everything else there is public. Stick a @internal or whatever Doxygen uses on it).

Copy link
Collaborator Author

@hugueskamba hugueskamba Jun 1, 2020

Choose a reason for hiding this comment

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

Done.
Everything is already internal documentation.

rtos/source/TARGET_CORTEX/mbed_boot.c Outdated Show resolved Hide resolved
Prevent singleton lock if the RTOS is not yet ready.

lp_ticker is used during the RTOS initialization process.
ST lp_ticker implementation calls sleep functions
which in turn attempts to print to the console when sleep tracing
is enabled. Console initialization attempts to lock the singleton mutex.
@mergify mergify bot dismissed kjbracey’s stale review June 1, 2020 14:31

Pull request has been modified.

@mergify mergify bot added needs: CI and removed needs: work labels Jun 1, 2020
hugueskamba referenced this pull request Jun 1, 2020
Allow singleton_lock and singleton_unlock to be called before the
RTOS has been started by checking for a valid mutex before locking
and unlocking it.
@0xc0170
Copy link
Contributor

0xc0170 commented Jun 5, 2020

CI started

@mbed-ci
Copy link

mbed-ci commented Jun 5, 2020

Test run: SUCCESS

Summary: 6 of 6 test jobs passed
Build number : 1
Build artifacts

@0xc0170 0xc0170 added the release-type: patch Indentifies a PR as containing just a patch label Jun 5, 2020
@0xc0170 0xc0170 merged commit 0449825 into ARMmbed:master Jun 5, 2020
@mergify mergify bot added release version missing When PR does not contain release version, bot should label it and we fix it afterwards and removed ready for merge labels Jun 5, 2020
@mergify
Copy link

mergify bot commented Jun 5, 2020

This PR does not contain release version label after merging.

@hugueskamba hugueskamba deleted the hk_fix_sleep_tracing branch June 5, 2020 23:57
@adbridge adbridge added release-version: 6.1.0 Release-pending and removed release-type: patch Indentifies a PR as containing just a patch Release-pending labels Jun 24, 2020
@0xc0170 0xc0170 removed the release version missing When PR does not contain release version, bot should label it and we fix it afterwards label Jun 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants