Skip to content

Commit 9c0bf4b

Browse files
teburdfabiobaltieri
authored andcommitted
kernel: Obtain current cpu inside of locks for thread usage
Obtaining the CPU outside of the spin locks on SMP would result in an assert failing on __ASSERT(!z_smp_mobile()) which makes sense as the current cpu may change. Signed-off-by: Tom Burdick <[email protected]>
1 parent 948b2ab commit 9c0bf4b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

kernel/usage.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ void z_sched_usage_start(struct k_thread *thread)
9898

9999
void z_sched_usage_stop(void)
100100
{
101-
struct _cpu *cpu = _current_cpu;
102101
k_spinlock_key_t k = k_spin_lock(&usage_lock);
102+
103+
struct _cpu *cpu = _current_cpu;
104+
103105
uint32_t u0 = cpu->usage0;
104106

105107
if (u0 != 0) {
@@ -122,8 +124,9 @@ void z_sched_cpu_usage(uint8_t cpu_id, struct k_thread_runtime_stats *stats)
122124
k_spinlock_key_t key;
123125
struct _cpu *cpu;
124126

125-
cpu = _current_cpu;
126127
key = k_spin_lock(&usage_lock);
128+
cpu = _current_cpu;
129+
127130

128131
if (&_kernel.cpus[cpu_id] == cpu) {
129132
uint32_t now = usage_now();
@@ -173,8 +176,9 @@ void z_sched_thread_usage(struct k_thread *thread,
173176
struct _cpu *cpu;
174177
k_spinlock_key_t key;
175178

176-
cpu = _current_cpu;
177179
key = k_spin_lock(&usage_lock);
180+
cpu = _current_cpu;
181+
178182

179183
if (thread == cpu->current) {
180184
uint32_t now = usage_now();
@@ -245,14 +249,15 @@ int k_thread_runtime_stats_enable(k_tid_t thread)
245249

246250
int k_thread_runtime_stats_disable(k_tid_t thread)
247251
{
248-
struct _cpu *cpu = _current_cpu;
249252
k_spinlock_key_t key;
250253

251254
CHECKIF(thread == NULL) {
252255
return -EINVAL;
253256
}
254257

255258
key = k_spin_lock(&usage_lock);
259+
struct _cpu *cpu = _current_cpu;
260+
256261
if (thread->base.usage.track_usage) {
257262
thread->base.usage.track_usage = false;
258263

0 commit comments

Comments
 (0)