Skip to content

Conversation

@ZhaoxiangJin
Copy link
Contributor

Three changes in this PR:

  1. Remove redundant XOR calculation in k_ipi_work_add
    In the current code, target_cpus ^= (1U << _current_cpu->id) is first used to remove the current core. Then, k_ipi_work_add performs target_cpus ^ (1U << _current_cpu->id) again when passing parameters. This will add the current core to the mask again, causing the current core to receive IPI and directly call cpu_freq_next_pstate() at the end, which may lead to duplicate execution. This first change is to change target_cpus ^ (1U << _current_cpu->id) to target_cpus in k_ipi_work_add to avoid the second XOR.
  2. Avoid redundant cpu_freq_policy_reset()
    The current code calls cpu_freq_policy_reset() once within the SMP branch of the cpu_freq_timer_handler function, and then calls it again at the beginning of cpu_freq_next_pstate(). This causes repeated resets of pstate_best and num_unprocessed_cpus, which prevents the 'last core' from being reached. The initiating core should perform a reset before broadcasting, and other cores should not reset again.
  3. Improve cpu_freq documentation.

peter-mitsis
peter-mitsis previously approved these changes Oct 29, 2025
Copy link
Contributor

@peter-mitsis peter-mitsis left a comment

Choose a reason for hiding this comment

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

Good catches. Thanks!

seankyer
seankyer previously approved these changes Oct 29, 2025
kartben
kartben previously approved these changes Oct 30, 2025
Copy link
Contributor

@kartben kartben left a comment

Choose a reason for hiding this comment

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

+1 for docs

@seankyer
Copy link
Member

@kartben Should this be added to 4.3 release? Is there a label that must be added?

@ZhaoxiangJin ZhaoxiangJin changed the title Improve cpu freq subsys Fix two deficiencies in the CPU frequency scaling subsystem Oct 31, 2025
@ZhaoxiangJin ZhaoxiangJin added this to the v4.3.0 milestone Oct 31, 2025
@ZhaoxiangJin ZhaoxiangJin added the bug The issue is a bug, or the PR is fixing a bug label Oct 31, 2025
@ZhaoxiangJin ZhaoxiangJin modified the milestone: v4.3.0 Oct 31, 2025
*/

target_cpus = (num_cpus == 32U) ? 0xFFFFFFFF : (1U << num_cpus) - 1U;
target_cpus ^= (1U << _current_cpu->id),
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: typo?

Suggested change
target_cpus ^= (1U << _current_cpu->id),
target_cpus ^= (1U << _current_cpu->id);

uint32_t target_cpus;
int ret;

__ASSERT(num_cpus <= 32U, "Too many CPUs");
Copy link
Contributor

Choose a reason for hiding this comment

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

Outside PR scope: maybe the Kconfig for CPUFreq should impose range 1 32 to CONFIG_MP_MAX_NUM_CPUS instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be more appropriate to create a separate PR for a global update.

Comment on lines 64 to 65
The SoC supporting CPU Frequency Scaling must uphold Zephyr's requirement that the system timer remains
constant over the lifetime of the program. See :ref:`Kernel Timing <kernel_timing>` for more information.
Copy link
Contributor

Choose a reason for hiding this comment

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

Quoting the part of the Kernel Timing page which I think this is referring (emphasis mine):

The frequency of [the system timer] counter is required to be steady over time

Proposal:

Suggested change
The SoC supporting CPU Frequency Scaling must uphold Zephyr's requirement that the system timer remains
constant over the lifetime of the program. See :ref:`Kernel Timing <kernel_timing>` for more information.
The SoC supporting CPU Frequency Scaling must uphold Zephyr's requirement that the system timer frequency
remains steady over the lifetime of the program. See :ref:`Kernel Timing <kernel_timing>` for more information.

In the current code, 'target_cpus ^= (1U << _current_cpu->id)'
is first used to remove the current core. Then, k_ipi_work_add
performs 'target_cpus ^ (1U << _current_cpu->id)' again when
passing parameters. This will add the current core to the mask
again, causing the current core to receive IPI and directly call
cpu_freq_next_pstate() at the end, which may lead to duplicate
execution.

This commit changed 'target_cpus ^ (1U << _current_cpu->id)' to
'target_cpus' in k_ipi_work_add to avoid the second XOR.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
The current code calls cpu_freq_policy_reset() once within
the SMP branch of the cpu_freq_timer_handler function, and
then calls it again at the beginning of cpu_freq_next_pstate().
This causes repeated resets of 'pstate_best' and
'num_unprocessed_cpus', which prevents the 'last core' from
being reached. The initiating core should perform a reset
before broadcasting, and other cores should not reset again.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
Improve cpu_freq documentation.

Signed-off-by: Zhaoxiang Jin <Zhaoxiang.Jin_1@nxp.com>
@ZhaoxiangJin ZhaoxiangJin dismissed stale reviews from kartben, seankyer, and peter-mitsis via 8341cf1 October 31, 2025 13:45
@ZhaoxiangJin ZhaoxiangJin force-pushed the improve-cpu-freq-subsys branch from ac294e3 to 8341cf1 Compare October 31, 2025 13:45
@ZhaoxiangJin ZhaoxiangJin requested review from kartben, mathieuchopstm, peter-mitsis and seankyer and removed request for mathieuchopstm October 31, 2025 13:51
@sonarqubecloud
Copy link

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

Labels

area: CPU Frequency Scaling bug The issue is a bug, or the PR is fixing a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants