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

Bluetooth: Host: fix stack overflow and improve timing performance for ECC emulation #82217

Merged
merged 4 commits into from
Nov 28, 2024

Conversation

valeriosetti
Copy link
Collaborator

This PR fixes issues (1) and (3) reported here, i.e.

  1. Without the increase in CONFIG_BT_LONG_WQ_STACK_SIZE all Bluetooth pairings on the nRF53 and nRF54 series will fail on target
  2. There's nearly a 10x increase in pairing time (takes up to 10 seconds) which is not only really bad, but will also lead to timeouts on tests on hardware as well

and also potentially reduce issue (4), i.e.

  1. There's a significant increase in memory as pointed out by TinyCrypt deprecation - library's usage removal part 2 (bluetooth) #79931 (comment)

@valeriosetti
Copy link
Collaborator Author

@sjanc if/when the CI is green, can you please launch AutoPTS?

Copy link
Contributor

@cvinayak cvinayak left a comment

Choose a reason for hiding this comment

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

nitpick... Nice if the commit title is "Bluetooth: Host: ...". Otherwise LGTM.

When BT_SEND_ECC_EMULATION and the platform uses Mbed TLS as PSA
Crypto provider, we select the Cortex-M software optimized
implementation of the secp256r1 curve algorithms. This is much
faster than the standard support provided by Mbed TLS and it
also reduces ROM footprint.

Signed-off-by: Valerio Setti <[email protected]>
After the switch from TinyCrypt to PSA Crypto API as crypto
backend, runtime crashes might happen on some platform due
to BT_LONG_WQ's stack size not being large enough. This
commit fixes this issue.

Signed-off-by: Valerio Setti <[email protected]>
@valeriosetti valeriosetti changed the title bt: host: fix stack overflow and improve timing performance for ECC emulation Bluetooth: jost: fix stack overflow and improve timing performance for ECC emulation Nov 28, 2024
@valeriosetti valeriosetti changed the title Bluetooth: jost: fix stack overflow and improve timing performance for ECC emulation Bluetooth: Host: fix stack overflow and improve timing performance for ECC emulation Nov 28, 2024
jhedberg
jhedberg previously approved these changes Nov 28, 2024
Copy link
Member

@jhedberg jhedberg left a comment

Choose a reason for hiding this comment

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

Looks fine to me, assuming that CI and auto-pts are green.

@jhedberg
Copy link
Member

And assuming that the tests are green, I think it would be good to fast-track merging this. Not sure if it'd be ok to add the Hotfix label, since this isn't fixing direct upstream CI breakage, but it is definitely unblocking other kinds of testing and potentially fixing downstream CI issues.

This reverts commit 184c0f9.

Reducing system heap memory to 1024 caused runtime failures on some
platforms. This commit revert that change and return to the previous
size.

Signed-off-by: Valerio Setti <[email protected]>
alwa-nordic
alwa-nordic previously approved these changes Nov 28, 2024
Copy link
Collaborator

@alwa-nordic alwa-nordic left a comment

Choose a reason for hiding this comment

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

Approved as a quick fix.

But there should be a followup. The Bluetooth subsystem should not have to know about all kinds of implementations of crypto and when it's possible to enable them.

Instead Bluetooth should just declare a dependency on a particular API, and the crypto subsystem should figure out how to optimize that request. Something like select WANT_CRYPTO_SECP256R1 maybe?

@zephyrbot zephyrbot added area: Bluetooth HCI Bluetooth HCI Driver area: Samples Samples labels Nov 28, 2024
Copy link
Collaborator

@Thalley Thalley left a comment

Choose a reason for hiding this comment

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

Changes look good, but I'll need to run some on-target tests first

alwa-nordic
alwa-nordic previously approved these changes Nov 28, 2024
@sjanc
Copy link
Collaborator

sjanc commented Nov 28, 2024

#AutoPTS run zephyr nrf53 SM

@codecoup-tester
Copy link

Scheduled PR #82217 (comment), board: nrf53, estimated start time: 10:53:12, test case count: 71, estimated duration: 0

Test cases to be runSM/CEN/PROT/BV-01-C
SM/CEN/JW/BV-05-C
SM/CEN/JW/BI-04-C
SM/CEN/JW/BI-01-C
SM/CEN/PKE/BV-01-C
SM/CEN/PKE/BV-04-C
SM/CEN/PKE/BI-01-C
SM/CEN/PKE/BI-02-C
SM/CEN/OOB/BV-01-C
SM/CEN/OOB/BV-03-C
SM/CEN/OOB/BV-09-C
SM/CEN/OOB/BI-01-C
SM/CEN/EKS/BV-01-C
SM/CEN/E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jhedberg
Copy link
Member

A hotfix for the (unrelated) CI failure is here: #82222

Thalley
Thalley previously approved these changes Nov 28, 2024
Copy link
Collaborator

@Thalley Thalley left a comment

Choose a reason for hiding this comment

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

Everything works out of the box and the performs is on par with TC

Great work

@codecoup-tester
Copy link

AutoPTS Bot results:

Failed tests (2)SM SM/CEN/KDU/BI-04-C FAIL
SM SM/PER/KDU/BI-04-C FAIL
Successful tests (69)SM SM/CEN/EKS/BI-01-C PASS
SM SM/CEN/EKS/BV-01-C PASS
SM SM/CEN/JW/BI-01-C PASS
SM SM/CEN/JW/BI-04-C PASS
SM SM/CEN/JW/BV-05-C PASS
SM SM/CEN/KDU/BV-04-C PASS
SM SM/CEN/KDU/BV-05-C PASS
SM SM/CEN/KDU/BV-06-C PASS
SM SM/CEN/KDU/BV-10-C PASS
SM SM/CEN/KDU/BV-11-C PASS
SM SM/CEN/OOB/BI-01-C PASS
SM SM/CEN/OOB/BV-01-C PASS
SM SM/CEN/OOB/BV-03-C PASS
SM SM/CEN/OOB/BV-09-C PASS
SM SM/CEN/PIS/BV-02-C PASS
SM SM/CEN/PIS/BV-03-C PASS
SM SM/CEN/PKE/BI-01-C PASS
SM SM/CEN/PKE/BI-02-C PASS
SM SM/CEN/PKE/BV-01-C PASS
SM SM/CEN/PKE/BV-04-C PASS
SM SM/CEN/PROT/BV-01-C PASS
SM SM/CEN/SCJW/BI-01-C PASS
SM SM/CEN/SCJW/BV-01-C PASS
SM SM/CEN/SCJW/BV-04-C PASS
SM SM/CEN/SCOB/BI-01-C PASS
SM SM/CEN/SCOB/BI-04-C PASS
SM SM/CEN/SCOB/BV-01-C PASS
SM SM/CEN/SCOB/BV-04-C PASS
SM SM/CEN/SCPK/BI-01-C PASS
SM SM/CEN/SCPK/BI-02-C PASS
SM SM/CEN/SCPK/BV-01-C PASS
SM SM/CEN/SCPK/BV-04-C PASS
SM SM/CEN/SIGN/BI-01-C PASS
SM SM/CEN/SIGN/BV-01-C PASS
SM SM/CEN/SIGN/BV-03-C PASS
SM SM/PER/EKS/BI-02-C PASS
SM SM/PER/EKS/BV-02-C PASS
SM SM/PER/JW/BI-02-C PASS
SM SM/PER/JW/BI-03-C PASS
SM SM/PER/JW/BV-02-C PASS
SM SM/PER/KDU/BI-02-C PASS
SM SM/PER/KDU/BI-03-C PASS
SM SM/PER/KDU/BV-01-C PASS
SM SM/PER/KDU/BV-02-C PASS
SM SM/PER/KDU/BV-03-C PASS
SM SM/PER/KDU/BV-07-C PASS
SM SM/PER/KDU/BV-08-C PASS
SM SM/PER/KDU/BV-09-C PASS
SM SM/PER/OOB/BI-02-C PASS
SM SM/PER/OOB/BV-02-C PASS
SM SM/PER/OOB/BV-04-C PASS
SM SM/PER/OOB/BV-10-C PASS
SM SM/PER/PIS/BV-01-C PASS
SM SM/PER/PIS/BV-02-C PASS
SM SM/PER/PKE/BI-03-C PASS
SM SM/PER/PKE/BV-02-C PASS
SM SM/PER/PKE/BV-05-C PASS
SM SM/PER/PROT/BV-02-C PASS
SM SM/PER/SCJW/BI-02-C PASS
SM SM/PER/SCJW/BV-02-C PASS
SM SM/PER/SCJW/BV-03-C PASS
SM SM/PER/SCOB/BI-02-C PASS
SM SM/PER/SCOB/BI-03-C PASS
SM SM/PER/SCOB/BV-02-C PASS
SM SM/PER/SCOB/BV-03-C PASS
SM SM/PER/SCPK/BI-03-C PASS
SM SM/PER/SCPK/BI-04-C PASS
SM SM/PER/SCPK/BV-02-C PASS
SM SM/PER/SCPK/BV-03-C PASS

@Thalley
Copy link
Collaborator

Thalley commented Nov 28, 2024

AutoPTS Bot results:
Failed tests (2)

Successful tests (69)

These are expected results (the failing tests were also failing before the TC deprecation).

This commit comments out some test AC cases that are failing in bsim.
These will be investigated in PR zephyrproject-rtos#82199.

Signed-off-by: Valerio Setti <[email protected]>
@valeriosetti
Copy link
Collaborator Author

valeriosetti commented Nov 28, 2024

@fabiobaltieri here's a follow-up for the results of west build -p always -b nrf52dk/nrf52832 samples/bluetooth/peripheral_hids on different branches:

  • main
Memory region         Used Size  Region Size  %age Used
           FLASH:      203988 B       512 KB     38.91%
             RAM:       30904 B        64 KB     47.16%
        IDT_LIST:          0 GB        32 KB      0.00%
  • tinycrypt (as you reported in 79931)
Memory region         Used Size  Region Size  %age Used
           FLASH:      181116 B       512 KB     34.55%
             RAM:       29240 B        64 KB     44.62%
        IDT_LIST:          0 GB        32 KB      0.00%
  • this PR
Memory region         Used Size  Region Size  %age Used
           FLASH:      191408 B       512 KB     36.51%
             RAM:       30968 B        64 KB     47.25%
        IDT_LIST:          0 GB        32 KB      0.00%

The ROM increase is half of what it was with #79931.

Edit: if you also want to save RAM, you can set CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT to a lower value. The default 16 is quite large for basic BT application I think. With 4 for example you'll have

Memory region         Used Size  Region Size  %age Used
           FLASH:      191408 B       512 KB     36.51%
             RAM:       29752 B        64 KB     45.40%
        IDT_LIST:          0 GB        32 KB      0.00%

@fabiobaltieri
Copy link
Member

@valeriosetti cool, thanks for the followup.

@jhedberg
Copy link
Member

This is also fixing breakage on Silabs boards, as discovered here: #81702

@jhedberg jhedberg added the Hotfix Fix for issues blocking development, i.e. CI issues, tests failing in CI, etc. label Nov 28, 2024
@jhedberg
Copy link
Member

Added the Hotfix label, since I think this falls under what's defined here: https://docs.zephyrproject.org/latest/project/dev_env_and_tools.html#hotfix

@fabiobaltieri fabiobaltieri merged commit 1320b3d into zephyrproject-rtos:main Nov 28, 2024
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Bluetooth Audio area: Bluetooth HCI Bluetooth HCI Driver area: Bluetooth Host area: Bluetooth area: Samples Samples Hotfix Fix for issues blocking development, i.e. CI issues, tests failing in CI, etc. platform: nRF BSIM Nordic Semiconductors, nRF BabbleSim
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

9 participants