ret = -EBUSY;
                goto out;
        }
-       if (!atomic64_read(&map->usercnt)) {
-               /* maps with timers must be either held by user space
-                * or pinned in bpffs.
-                */
-               ret = -EPERM;
-               goto out;
-       }
        /* allocate hrtimer via map_kmalloc to use memcg accounting */
        t = bpf_map_kmalloc_node(map, sizeof(*t), GFP_ATOMIC, map->numa_node);
        if (!t) {
        rcu_assign_pointer(t->callback_fn, NULL);
        hrtimer_init(&t->timer, clockid, HRTIMER_MODE_REL_SOFT);
        t->timer.function = bpf_timer_cb;
-       timer->timer = t;
+       WRITE_ONCE(timer->timer, t);
+       /* Guarantee the order between timer->timer and map->usercnt. So
+        * when there are concurrent uref release and bpf timer init, either
+        * bpf_timer_cancel_and_free() called by uref release reads a no-NULL
+        * timer or atomic64_read() below returns a zero usercnt.
+        */
+       smp_mb();
+       if (!atomic64_read(&map->usercnt)) {
+               /* maps with timers must be either held by user space
+                * or pinned in bpffs.
+                */
+               WRITE_ONCE(timer->timer, NULL);
+               kfree(t);
+               ret = -EPERM;
+       }
 out:
        __bpf_spin_unlock_irqrestore(&timer->lock);
        return ret;
        /* The subsequent bpf_timer_start/cancel() helpers won't be able to use
         * this timer, since it won't be initialized.
         */
-       timer->timer = NULL;
+       WRITE_ONCE(timer->timer, NULL);
 out:
        __bpf_spin_unlock_irqrestore(&timer->lock);
        if (!t)