}
        }
 
+       /* Sync up the HVIP.LCOFIP bit changes (only clear) by the guest */
+       if ((csr->hvip ^ hvip) & (1UL << IRQ_PMU_OVF)) {
+               if (!(hvip & (1UL << IRQ_PMU_OVF)) &&
+                   !test_and_set_bit(IRQ_PMU_OVF, v->irqs_pending_mask))
+                       clear_bit(IRQ_PMU_OVF, v->irqs_pending);
+       }
+
        /* Sync-up AIA high interrupts */
        kvm_riscv_vcpu_aia_sync_interrupts(vcpu);
 
        if (irq < IRQ_LOCAL_MAX &&
            irq != IRQ_VS_SOFT &&
            irq != IRQ_VS_TIMER &&
-           irq != IRQ_VS_EXT)
+           irq != IRQ_VS_EXT &&
+           irq != IRQ_PMU_OVF)
                return -EINVAL;
 
        set_bit(irq, vcpu->arch.irqs_pending);
 int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq)
 {
        /*
-        * We only allow VS-mode software, timer, and external
+        * We only allow VS-mode software, timer, counter overflow and external
         * interrupts when irq is one of the local interrupts
         * defined by RISC-V privilege specification.
         */
        if (irq < IRQ_LOCAL_MAX &&
            irq != IRQ_VS_SOFT &&
            irq != IRQ_VS_TIMER &&
-           irq != IRQ_VS_EXT)
+           irq != IRQ_VS_EXT &&
+           irq != IRQ_PMU_OVF)
                return -EINVAL;
 
        clear_bit(irq, vcpu->arch.irqs_pending);
 
        /* Multi letter extensions (alphabetically sorted) */
        KVM_ISA_EXT_ARR(SMSTATEEN),
        KVM_ISA_EXT_ARR(SSAIA),
+       KVM_ISA_EXT_ARR(SSCOFPMF),
        KVM_ISA_EXT_ARR(SSTC),
        KVM_ISA_EXT_ARR(SVINVAL),
        KVM_ISA_EXT_ARR(SVNAPOT),
        switch (ext) {
        case KVM_RISCV_ISA_EXT_H:
                return false;
+       case KVM_RISCV_ISA_EXT_SSCOFPMF:
+               /* Sscofpmf depends on interrupt filtering defined in ssaia */
+               return __riscv_isa_extension_available(NULL, RISCV_ISA_EXT_SSAIA);
        case KVM_RISCV_ISA_EXT_V:
                return riscv_v_vstate_ctrl_user_allowed();
        default:
        case KVM_RISCV_ISA_EXT_C:
        case KVM_RISCV_ISA_EXT_I:
        case KVM_RISCV_ISA_EXT_M:
+       /* There is not architectural config bit to disable sscofpmf completely */
+       case KVM_RISCV_ISA_EXT_SSCOFPMF:
        case KVM_RISCV_ISA_EXT_SSTC:
        case KVM_RISCV_ISA_EXT_SVINVAL:
        case KVM_RISCV_ISA_EXT_SVNAPOT:
 
        return 0;
 }
 
+static void kvm_riscv_pmu_overflow(struct perf_event *perf_event,
+                                  struct perf_sample_data *data,
+                                  struct pt_regs *regs)
+{
+       struct kvm_pmc *pmc = perf_event->overflow_handler_context;
+       struct kvm_vcpu *vcpu = pmc->vcpu;
+       struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
+       struct riscv_pmu *rpmu = to_riscv_pmu(perf_event->pmu);
+       u64 period;
+
+       /*
+        * Stop the event counting by directly accessing the perf_event.
+        * Otherwise, this needs to deferred via a workqueue.
+        * That will introduce skew in the counter value because the actual
+        * physical counter would start after returning from this function.
+        * It will be stopped again once the workqueue is scheduled
+        */
+       rpmu->pmu.stop(perf_event, PERF_EF_UPDATE);
+
+       /*
+        * The hw counter would start automatically when this function returns.
+        * Thus, the host may continue to interrupt and inject it to the guest
+        * even without the guest configuring the next event. Depending on the hardware
+        * the host may have some sluggishness only if privilege mode filtering is not
+        * available. In an ideal world, where qemu is not the only capable hardware,
+        * this can be removed.
+        * FYI: ARM64 does this way while x86 doesn't do anything as such.
+        * TODO: Should we keep it for RISC-V ?
+        */
+       period = -(local64_read(&perf_event->count));
+
+       local64_set(&perf_event->hw.period_left, 0);
+       perf_event->attr.sample_period = period;
+       perf_event->hw.sample_period = period;
+
+       set_bit(pmc->idx, kvpmu->pmc_overflown);
+       kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_PMU_OVF);
+
+       rpmu->pmu.start(perf_event, PERF_EF_RELOAD);
+}
+
 static long kvm_pmu_create_perf_event(struct kvm_pmc *pmc, struct perf_event_attr *attr,
                                      unsigned long flags, unsigned long eidx,
                                      unsigned long evtdata)
         */
        attr->sample_period = kvm_pmu_get_sample_period(pmc);
 
-       event = perf_event_create_kernel_counter(attr, -1, current, NULL, pmc);
+       event = perf_event_create_kernel_counter(attr, -1, current, kvm_riscv_pmu_overflow, pmc);
        if (IS_ERR(event)) {
                pr_err("kvm pmu event creation failed for eidx %lx: %ld\n", eidx, PTR_ERR(event));
                return PTR_ERR(event);
                pmc_index = i + ctr_base;
                if (!test_bit(pmc_index, kvpmu->pmc_in_use))
                        continue;
+               /* The guest started the counter again. Reset the overflow status */
+               clear_bit(pmc_index, kvpmu->pmc_overflown);
                pmc = &kvpmu->pmc[pmc_index];
                if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE) {
                        pmc->counter_val = ival;
                        else if (pmc->perf_event)
                                pmc->counter_val += perf_event_read_value(pmc->perf_event,
                                                                          &enabled, &running);
-                       /* TODO: Add counter overflow support when sscofpmf support is added */
+                       /*
+                        * The counter and overflow indicies in the snapshot region are w.r.to
+                        * cbase. Modify the set bit in the counter mask instead of the pmc_index
+                        * which indicates the absolute counter index.
+                        */
+                       if (test_bit(pmc_index, kvpmu->pmc_overflown))
+                               kvpmu->sdata->ctr_overflow_mask |= BIT(i);
                        kvpmu->sdata->ctr_values[i] = pmc->counter_val;
                        shmem_needs_update = true;
                }
                if (flags & SBI_PMU_STOP_FLAG_RESET) {
                        pmc->event_idx = SBI_PMU_EVENT_IDX_INVALID;
                        clear_bit(pmc_index, kvpmu->pmc_in_use);
+                       clear_bit(pmc_index, kvpmu->pmc_overflown);
+                       if (snap_flag_set) {
+                               /*
+                                * Only clear the given counter as the caller is responsible to
+                                * validate both the overflow mask and configured counters.
+                                */
+                               kvpmu->sdata->ctr_overflow_mask &= ~BIT(i);
+                               shmem_needs_update = true;
+                       }
                }
        }
 
                pmc = &kvpmu->pmc[i];
                pmc->idx = i;
                pmc->event_idx = SBI_PMU_EVENT_IDX_INVALID;
+               pmc->vcpu = vcpu;
                if (i < kvpmu->num_hw_ctrs) {
                        pmc->cinfo.type = SBI_PMU_CTR_TYPE_HW;
                        if (i < 3)
        if (!kvpmu)
                return;
 
-       for_each_set_bit(i, kvpmu->pmc_in_use, RISCV_MAX_COUNTERS) {
+       for_each_set_bit(i, kvpmu->pmc_in_use, RISCV_KVM_MAX_COUNTERS) {
                pmc = &kvpmu->pmc[i];
                pmc->counter_val = 0;
                kvm_pmu_release_perf_event(pmc);
                pmc->event_idx = SBI_PMU_EVENT_IDX_INVALID;
        }
-       bitmap_zero(kvpmu->pmc_in_use, RISCV_MAX_COUNTERS);
+       bitmap_zero(kvpmu->pmc_in_use, RISCV_KVM_MAX_COUNTERS);
+       bitmap_zero(kvpmu->pmc_overflown, RISCV_KVM_MAX_COUNTERS);
        memset(&kvpmu->fw_event, 0, SBI_PMU_FW_MAX * sizeof(struct kvm_fw_event));
        kvm_pmu_clear_snapshot_area(vcpu);
 }