From f72c3dee5a155ca3a5b0b6c8fe0a82ce73766e14 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 14 Jun 2018 17:38:41 +0300 Subject: [PATCH 1/4] kconfig: Set main thread priority to -1 if cooperative If user has set CONFIG_COOP_ENABLED, then set the main thread default priority to -1. This is needed in later patches to enforce this setting for networking code which requires that CONFIG_COOP_ENABLED=y and CONFIG_MAIN_THREAD_PRIORITY < 0. Signed-off-by: Jukka Rissanen --- kernel/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/Kconfig b/kernel/Kconfig index e1b694ba57adc..760a68df660f8 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -80,6 +80,7 @@ config MAIN_THREAD_PRIORITY int prompt "Priority of initialization/main thread" default 0 + default -1 if COOP_ENABLED default -2 if !PREEMPT_ENABLED help Priority at which the initialization thread runs, including the start From a57572a51c53f4b29b3badb61e3edaffa9c3b0c9 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 14 Jun 2018 17:42:04 +0300 Subject: [PATCH 2/4] net: Require that system workqueue priority < 0 The networking subsystem does not work properly if running in preemptive priority. So make sure that system workqueue priority is set <0 which means cooperative priority. Signed-off-by: Jukka Rissanen --- subsys/net/Kconfig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/subsys/net/Kconfig b/subsys/net/Kconfig index 0e313f74f74ad..6fca555cf3339 100644 --- a/subsys/net/Kconfig +++ b/subsys/net/Kconfig @@ -91,6 +91,11 @@ config NETWORKING if NETWORKING +# The networking subsystem requires the system workqueue to execute at +# a cooperative priority. +config SYSTEM_WORKQUEUE_PRIORITY + range -256 -1 + source "subsys/net/Kconfig.hostname" source "subsys/net/ip/Kconfig" From 8dcae2c6d0f10c8fc6cbb93a5c7ed404914f89e6 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 14 Jun 2018 17:44:20 +0300 Subject: [PATCH 3/4] net: Make sure CONFIG_COOP_ENABLED is set The networking subsystem requires cooperative priorities to be enabled so make sure CONFIG_COOP_ENABLED is set. Signed-off-by: Jukka Rissanen --- subsys/net/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/subsys/net/Kconfig b/subsys/net/Kconfig index 6fca555cf3339..e7790fb4d755e 100644 --- a/subsys/net/Kconfig +++ b/subsys/net/Kconfig @@ -85,6 +85,7 @@ config NETWORKING select NET_BUF select POLL select ENTROPY_GENERATOR + select COOP_ENABLED default n help This option enabled generic link layer and IP networking support. From 072afc1ba35642d9fab6d6e39514c2a7006ed70a Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 14 Jun 2018 17:45:35 +0300 Subject: [PATCH 4/4] net: Add build check for main thread priority Make sure that networking subsystem cannot be compiled unless CONFIG_MAIN_THREAD_PRIORITY < 0. This will avoid hard to track bugs as network subsystem functions should not be called from preemptive thread. Signed-off-by: Jukka Rissanen --- subsys/net/ip/net_core.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/subsys/net/ip/net_core.c b/subsys/net/ip/net_core.c index c850b53b422e1..d3bb1ecb82191 100644 --- a/subsys/net/ip/net_core.c +++ b/subsys/net/ip/net_core.c @@ -419,4 +419,11 @@ static int net_init(struct device *unused) return status; } +/* The networking subsystem requires the main thread to execute at a + * cooperative priority to function correctly. If this build assert triggers + * verify your configuration to ensure that cooperative threads are enabled + * and that the main thread priority is negative (cooperative). + */ +BUILD_ASSERT(CONFIG_MAIN_THREAD_PRIORITY < 0); + SYS_INIT(net_init, POST_KERNEL, CONFIG_NET_INIT_PRIO);