!event->pending_work) {
                        event->pending_work = 1;
                        dec = false;
+                       WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount));
                        task_work_add(current, &event->pending_task, TWA_RESUME);
                }
                if (dec)
 
 #define DETACH_GROUP   0x01UL
 #define DETACH_CHILD   0x02UL
+#define DETACH_DEAD    0x04UL
 
 /*
  * Cross CPU call to remove a performance event
                update_cgrp_time_from_cpuctx(cpuctx, false);
        }
 
+       /*
+        * Ensure event_sched_out() switches to OFF, at the very least
+        * this avoids raising perf_pending_task() at this time.
+        */
+       if (flags & DETACH_DEAD)
+               event->pending_disable = 1;
        event_sched_out(event, cpuctx, ctx);
        if (flags & DETACH_GROUP)
                perf_group_detach(event);
        if (flags & DETACH_CHILD)
                perf_child_detach(event);
        list_del_event(event, ctx);
+       if (flags & DETACH_DEAD)
+               event->state = PERF_EVENT_STATE_DEAD;
 
        if (!ctx->nr_events && ctx->is_active) {
                if (ctx == &cpuctx->ctx)
 
        ctx = perf_event_ctx_lock(event);
        WARN_ON_ONCE(ctx->parent_ctx);
-       perf_remove_from_context(event, DETACH_GROUP);
 
-       raw_spin_lock_irq(&ctx->lock);
        /*
         * Mark this event as STATE_DEAD, there is no external reference to it
         * anymore.
         * Thus this guarantees that we will in fact observe and kill _ALL_
         * child events.
         */
-       event->state = PERF_EVENT_STATE_DEAD;
-       raw_spin_unlock_irq(&ctx->lock);
+       perf_remove_from_context(event, DETACH_GROUP|DETACH_DEAD);
 
        perf_event_ctx_unlock(event, ctx);
 
        if (rctx >= 0)
                perf_swevent_put_recursion_context(rctx);
        preempt_enable_notrace();
+
+       put_event(event);
 }
 
 #ifdef CONFIG_GUEST_PERF_EVENTS