Skip to content

Commit c51be00

Browse files
wjablon1kv2019i
authored andcommitted
telemetry: fix lock leak on slot alloc failure
During allocation of a counter slot, spinlock isn't released in case of failure. This could lead to deadlock in case of failed allocation. This change fixes that by adding the missing release operation. Signed-off-by: Wojciech Jablonski <wojciech.jablonski@intel.com>
1 parent 24059e4 commit c51be00

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/debug/telemetry/performance_monitor.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static bool perf_bitmap_is_bit_clear(struct perf_bitmap * const bitmap, size_t b
128128

129129
struct perf_data_item_comp *perf_data_getnext(void)
130130
{
131-
int idx;
131+
size_t idx;
132132
int ret = perf_bitmap_alloc(&performance_data_bitmap, &idx);
133133

134134
if (ret < 0)
@@ -138,8 +138,10 @@ struct perf_data_item_comp *perf_data_getnext(void)
138138
* ,and always set bit on bitmap alloc.
139139
*/
140140
ret = perf_bitmap_setbit(&performance_data_bitmap, idx);
141-
if (ret < 0)
141+
if (ret < 0) {
142+
perf_bitmap_free(&performance_data_bitmap, idx);
142143
return NULL;
144+
}
143145
return &perf_data[idx];
144146
}
145147

@@ -445,24 +447,30 @@ int io_perf_monitor_init(void)
445447

446448
static struct io_perf_data_item *io_perf_monitor_get_next_slot(struct io_perf_monitor_ctx *self)
447449
{
448-
int idx;
450+
size_t idx;
449451
int ret;
450452
k_spinlock_key_t key = k_spin_lock(&self->lock);
451453

452454
ret = perf_bitmap_alloc(&self->io_performance_data_bitmap, &idx);
453455
if (ret < 0)
454-
return NULL;
456+
goto out_unlock;
455457
/* ref. FW did not set the bits, but here we do it to not have to use
456458
* isFree() check that the bitarray does not provide yet. Instead we will use isClear
457459
* ,and always set bit on bitmap alloc.
458460
*/
459461

460462
ret = perf_bitmap_setbit(&self->io_performance_data_bitmap, idx);
461-
if (ret < 0)
462-
return NULL;
463+
if (ret < 0) {
464+
perf_bitmap_free(&self->io_performance_data_bitmap, idx);
465+
goto out_unlock;
466+
}
463467

464468
k_spin_unlock(&self->lock, key);
465469
return &self->io_perf_data[idx];
470+
471+
out_unlock:
472+
k_spin_unlock(&self->lock, key);
473+
return NULL;
466474
}
467475

468476
int io_perf_monitor_release_slot(struct io_perf_data_item *item)

0 commit comments

Comments
 (0)