From e763d6f9774eb363e13e1b66ad52988c44b3bfe3 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 23 Aug 2018 14:41:56 +0300 Subject: [PATCH] net: tcp: net_tcp_queue_pkt: Lock while appending to sent_list net_tcp_queue_pkt() is a single point where packets are added to tcp->sent_list, and this happens from application thread. This list is also accessed (iterated on, items removed) from a number of delayed work handlers. These handlers are serialized among themselves. Thus, the only issue is serializing access to this list between net_tcp_queue_pkt() running in app thread and delayes works running in own cooperatively scheduled thread. This patch does this, and as adding to the list is very quick operation, it just uses irq_lock(), as interrupt jitter added is very minimal and usage of mutex would likely give the same amount of jitter anyway. Signed-off-by: Paul Sokolovsky --- subsys/net/ip/tcp.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/subsys/net/ip/tcp.c b/subsys/net/ip/tcp.c index 85e66118a53c2..286b0d544fb68 100644 --- a/subsys/net/ip/tcp.c +++ b/subsys/net/ip/tcp.c @@ -846,7 +846,14 @@ int net_tcp_queue_data(struct net_context *context, struct net_pkt *pkt) */ static int net_tcp_queue_pkt(struct net_context *context, struct net_pkt *pkt) { + unsigned int key; + + /* slist is not thread-safe, can be interrupted by retry timer and + * accessed concurrently, protect against that. + */ + key = irq_lock(); sys_slist_append(&context->tcp->sent_list, &pkt->sent_list); + irq_unlock(key); /* We need to restart retry_timer if it is stopped. */ if (k_delayed_work_remaining_get(&context->tcp->retry_timer) == 0) {