Skip to content

Commit

Permalink
net: lwm2m: Make CoAP block contetx allocation thread safe
Browse files Browse the repository at this point in the history
Use global engine mutex to protect allocation/deallocation of the CoAP
block contexts.

Signed-off-by: Robert Lubos <[email protected]>
  • Loading branch information
rlubos committed Oct 15, 2024
1 parent 3bd83be commit 1537be1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions subsys/net/lib/lwm2m/lwm2m_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,9 @@ static int lwm2m_engine_init(void)

lwm2m_clear_block_contexts();
#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER)
lwm2m_engine_lock();
(void)memset(output_block_contexts, 0, sizeof(output_block_contexts));
lwm2m_engine_unlock();
#endif

STRUCT_SECTION_FOREACH(lwm2m_init_func, init) {
Expand Down
26 changes: 23 additions & 3 deletions subsys/net/lib/lwm2m/lwm2m_message_handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ enum coap_block_size lwm2m_default_block_size(void)

void lwm2m_clear_block_contexts(void)
{
lwm2m_engine_lock();
(void)memset(block1_contexts, 0, sizeof(block1_contexts));
lwm2m_engine_unlock();
}

static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_context **ctx)
Expand All @@ -130,6 +132,8 @@ static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_
return -EFAULT;
}

lwm2m_engine_lock();

*ctx = NULL;
timestamp = k_uptime_get();
for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) {
Expand All @@ -149,6 +153,7 @@ static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_
}

if (*ctx == NULL) {
lwm2m_engine_unlock();
LOG_ERR("Cannot find free block context");
return -ENOMEM;
}
Expand All @@ -160,6 +165,8 @@ static int init_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_
(*ctx)->last_block = false;
memset(&(*ctx)->opaque, 0, sizeof((*ctx)->opaque));

lwm2m_engine_unlock();

return 0;
}

Expand All @@ -174,6 +181,8 @@ static int get_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_c

*ctx = NULL;

lwm2m_engine_lock();

for (i = 0; i < NUM_BLOCK1_CONTEXT; i++) {
if (lwm2m_obj_path_equal(path, &block1_contexts[i].path)) {
*ctx = &block1_contexts[i];
Expand All @@ -183,6 +192,8 @@ static int get_block_ctx(const struct lwm2m_obj_path *path, struct lwm2m_block_c
}
}

lwm2m_engine_unlock();

if (*ctx == NULL) {
return -ENOENT;
}
Expand All @@ -196,24 +207,31 @@ static void free_block_ctx(struct lwm2m_block_context *ctx)
return;
}

lwm2m_engine_lock();
memset(&ctx->path, 0, sizeof(struct lwm2m_obj_path));
lwm2m_engine_unlock();
}

#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER)
STATIC int request_output_block_ctx(struct coap_block_context **ctx)
{
*ctx = NULL;
int ret = -ENOMEM;
int i;

*ctx = NULL;

lwm2m_engine_lock();
for (i = 0; i < NUM_OUTPUT_BLOCK_CONTEXT; i++) {
if (lwm2m_output_block_context()[i].block_size == 0) {
*ctx = &lwm2m_output_block_context()[i];
(*ctx)->block_size = OUTPUT_CONTEXT_IN_USE_MARK;
return 0;
ret = 0;
break;
}
}
lwm2m_engine_unlock();

return -ENOMEM;
return ret;
}

STATIC void release_output_block_ctx(struct coap_block_context **ctx)
Expand All @@ -224,12 +242,14 @@ STATIC void release_output_block_ctx(struct coap_block_context **ctx)
return;
}

lwm2m_engine_lock();
for (i = 0; i < NUM_OUTPUT_BLOCK_CONTEXT; i++) {
if (&lwm2m_output_block_context()[i] == *ctx) {
lwm2m_output_block_context()[i].block_size = 0;
*ctx = NULL;
}
}
lwm2m_engine_unlock();
}


Expand Down

0 comments on commit 1537be1

Please sign in to comment.