[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH 8/8] lib/ukschedcoop: Fix race condition when trying to wake up current thread


  • To: minios-devel@xxxxxxxxxxxxx
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Tue, 23 Apr 2019 13:41:44 +0300
  • Cc: felipe.huici@xxxxxxxxx, Florian.Schmidt@xxxxxxxxx, simon.kuenzer@xxxxxxxxx, yuri.volchkov@xxxxxxxxx, sharan.santhanam@xxxxxxxxx
  • Delivery-date: Tue, 23 Apr 2019 10:42:04 +0000
  • Ironport-phdr: 9a23:557d9xPid5ayuZtDQlAl6mtUPXoX/o7sNwtQ0KIMzox0K/37oMbcNUDSrc9gkEXOFd2Cra4d0qyK4+u6ACRAuc/H7ClaNsQUFlcssoY/p0QYGsmLCEn2frbBThcRO4B8bmJj5GyxKkNPGczzNBX4q3y26iMOSF2kbVImbuv6FZTPgMupyuu854PcYxlShDq6fLh+MAi6oR/eu8ULnIduMKk8xxjGrndWZuhd2GdkKU6Okxrm6cq84YJv/z5Mt/498sJLTLn3cbk/QbFEFjotLno75NfstRnNTAuP4mUTX2ALmRdWAAbL8Q/3UI7pviT1quRy1i+aPdbrTb8vQjSt871rSB7zhygZMTMy7XzahdZxjKJfpxKhugB/zovJa4ybKPZyYqXQds4cSGFcXMheSjZBD5u8YYUREuQPM+VWoY7/qFsAthayGRWgCfnzxjJSmnP6was32PkhHwHc2wwgGsoDvWrTrNXuKKcSUOa1x7TOwzXed/NWxCr25Y/UfRAmuvGMQbNwcczLxUkrCgPFlkiQpJf5MDOOzOgNq3Wb4PF6WeK1jG4qsgd8qSWsyMc0koTFm40Yxk3e+Sh6wIs5P8O0RFB5bNK+HpZcrzyWOoRrTs84QGxluDw2xqMItJO1ZiQG1ZsqywDZZveaaYaH+AjjW/yUITpggXJlf6+wiAiq/Ei7z+38StG00FFXripZitXMtm4C1xjU6sWfVPt9+12u2TeL1wzJ9u5EOlo4lbLGK5E62LIwjJ0TvVzCHi/whkr2kLebelgr9+S18ejqYbXrqoWCO4NqiAzyKKojltS6AesiMwgOW2ab+f671L3m5UD2XLJKjuYqkqnYtpDWP8AbprOhAw9IyYss9w2/Ay2+0NQFhnYLNkhFeBWfg4jzJ17OOOz4Deu4g1m0lTdrxvbGPrzmApXWN3TMjanufahj5E5Y0wczydFf54lICrEaOv7yVVH+tNrCAh8+KQy0zP7tCM9h2YMGRWKPHqiZPbvSv1+M4eIvOeiMa5UTuDrnNvYq+/7ujXo4mVAAYamkxp0XZ26kEfRiOUqWemDgjckcEW0SpAoxUPTqiEGeUT5Uf3uyUbwz5jU6CIK+E4jPXICtgKGA3CinH51bfWZGBU6QHnfsbYqLQO0AZzyPIsV5iDwLSaChS5M91RGprAL60LpnIfDO+iICs5LvzsN16PfVlREu9Tx7FcKd3HuIT2xvmGMHWSM53KRlrkNm0FuMz7V4ieRCFdNP//NJThs6NZnEwuxhCtDyXwXBftGTRFalX9WpHzcxT9MvzN8UeEt9HcutgQzH3yWwGLAZjaKEBIEs+KLGw3fxP9p9y2rB1KQ5j1gmX9FPNWy8iq5h8AjTA5LGk0Wal6ata6QRxyjN+3mfwmqKpk5XSxR8XrvYXSNXWkyDqNX/50TZCrOjF7kjGg9A0tKZbLtHbJvul1oVau3kPYH1ZHmtmmH4IQuQ2/vYZ43xZ2QbmiHAEFUsmBtV5WuMcxI5UHTy61nCBSBjQAq8K3jn9vNz/Su2
  • Ironport-sdr: naryss6NLvyxYVTU0SHEw0YSAnB/SyFusaySO/8cNORWRa3PB15S1nrhRfP085Yfr1ZBTy2Mjz kqrXU2/w1M+w==
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>

The previous patch introduced a race condition: if the current thread
blocks and yields, and an interrupt is triggered whose handle tries to
wake up the current thread then the thread will remain blocked until the
next interrupt (potentially forever) because the current thread cannot
be added to the ready threads list.

The solution is to set a flag on the current thread as soon as the next
thread is scheduled. The interrupt will queue the current thread if and
only if this flag is set.

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
 lib/uksched/include/uk/thread.h | 5 +++++
 lib/ukschedcoop/schedcoop.c     | 7 ++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/lib/uksched/include/uk/thread.h b/lib/uksched/include/uk/thread.h
index 71e39225..10fd4a33 100644
--- a/lib/uksched/include/uk/thread.h
+++ b/lib/uksched/include/uk/thread.h
@@ -96,6 +96,7 @@ struct uk_thread *uk_thread_current(void)
 
 #define RUNNABLE_FLAG   0x00000001
 #define EXITED_FLAG     0x00000002
+#define QUEUEABLE_FLAG  0x00000004
 
 #define is_runnable(_thread)    ((_thread)->flags &   RUNNABLE_FLAG)
 #define set_runnable(_thread)   ((_thread)->flags |=  RUNNABLE_FLAG)
@@ -104,6 +105,10 @@ struct uk_thread *uk_thread_current(void)
 #define is_exited(_thread)      ((_thread)->flags &   EXITED_FLAG)
 #define set_exited(_thread)     ((_thread)->flags |=  EXITED_FLAG)
 
+#define is_queueable(_thread)    ((_thread)->flags &   QUEUEABLE_FLAG)
+#define set_queueable(_thread)   ((_thread)->flags |=  QUEUEABLE_FLAG)
+#define clear_queueable(_thread) ((_thread)->flags &= ~QUEUEABLE_FLAG)
+
 int uk_thread_init(struct uk_thread *thread,
                struct ukplat_ctx_callbacks *cbs, struct uk_alloc *allocator,
                const char *name, void *stack,
diff --git a/lib/ukschedcoop/schedcoop.c b/lib/ukschedcoop/schedcoop.c
index 90559052..7aa411c7 100644
--- a/lib/ukschedcoop/schedcoop.c
+++ b/lib/ukschedcoop/schedcoop.c
@@ -109,6 +109,9 @@ static void schedcoop_schedule(struct uk_sched *s)
                        else if (prev->wakeup_time > 0)
                                UK_TAILQ_INSERT_TAIL(&prv->sleeping_threads, 
prev,
                                                thread_list);
+                       else
+                               set_queueable(prev);
+                       clear_queueable(next);
                        ukplat_stack_set_current_thread(next);
                        break;
                } else if (is_runnable(prev)) {
@@ -204,8 +207,10 @@ static void schedcoop_thread_woken(struct uk_sched *s, 
struct uk_thread *t)
 
        if (t->wakeup_time > 0)
                UK_TAILQ_REMOVE(&prv->sleeping_threads, t, thread_list);
-       if (t != uk_thread_current())
+       if (t != uk_thread_current() || is_queueable(t)) {
                UK_TAILQ_INSERT_TAIL(&prv->thread_list, t, thread_list);
+               clear_queueable(t);
+       }
 }
 
 static void idle_thread_fn(void *unused __unused)
-- 
2.11.0


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.