* In turns kcov_remote_stop() clears those pointers from task_struct to stop
  * collecting coverage and copies all collected coverage into the kcov area.
  */
+
+static inline bool kcov_mode_enabled(unsigned int mode)
+{
+       return (mode & ~KCOV_IN_CTXSW) != KCOV_MODE_DISABLED;
+}
+
 void kcov_remote_start(u64 handle)
 {
+       struct task_struct *t = current;
        struct kcov_remote *remote;
        struct kcov *kcov;
+       unsigned int mode;
        void *area;
-       struct task_struct *t;
        unsigned int size;
-       enum kcov_mode mode;
        int sequence;
 
        if (WARN_ON(!kcov_check_handle(handle, true, true, true)))
                return;
        if (WARN_ON(!in_task()))
                return;
-       t = current;
+
        /*
         * Check that kcov_remote_start is not called twice
         * nor called by user tasks (with enabled kcov).
         */
-       if (WARN_ON(t->kcov))
+       mode = READ_ONCE(t->kcov_mode);
+       if (WARN_ON(kcov_mode_enabled(mode)))
                return;
 
        kcov_debug("handle = %llx\n", handle);
 void kcov_remote_stop(void)
 {
        struct task_struct *t = current;
-       struct kcov *kcov = t->kcov;
-       void *area = t->kcov_area;
-       unsigned int size = t->kcov_size;
-       int sequence = t->kcov_sequence;
+       struct kcov *kcov;
+       unsigned int mode;
+       void *area;
+       unsigned int size;
+       int sequence;
 
-       if (!kcov)
+       mode = READ_ONCE(t->kcov_mode);
+       barrier();
+       if (!kcov_mode_enabled(mode))
                return;
+       kcov = t->kcov;
+       area = t->kcov_area;
+       size = t->kcov_size;
+       sequence = t->kcov_sequence;
 
        kcov_stop(t);