int perf_pmu_register(struct pmu *pmu, const char *name, int type)
 {
-       int cpu, ret;
+       int cpu, ret, max = PERF_TYPE_MAX;
 
        mutex_lock(&pmus_lock);
        ret = -ENOMEM;
                goto skip_type;
        pmu->name = name;
 
-       if (type < 0) {
-               type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
-               if (type < 0) {
-                       ret = type;
+       if (type != PERF_TYPE_SOFTWARE) {
+               if (type >= 0)
+                       max = type;
+
+               ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
+               if (ret < 0)
                        goto free_pdc;
-               }
+
+               WARN_ON(type >= 0 && ret != type);
+
+               type = ret;
        }
        pmu->type = type;
 
        put_device(pmu->dev);
 
 free_idr:
-       if (pmu->type >= PERF_TYPE_MAX)
+       if (pmu->type != PERF_TYPE_SOFTWARE)
                idr_remove(&pmu_idr, pmu->type);
 
 free_pdc:
        synchronize_rcu();
 
        free_percpu(pmu->pmu_disable_count);
-       if (pmu->type >= PERF_TYPE_MAX)
+       if (pmu->type != PERF_TYPE_SOFTWARE)
                idr_remove(&pmu_idr, pmu->type);
        if (pmu_bus_running) {
                if (pmu->nr_addr_filters)
 
 static struct pmu *perf_init_event(struct perf_event *event)
 {
+       int idx, type, ret;
        struct pmu *pmu;
-       int idx;
-       int ret;
 
        idx = srcu_read_lock(&pmus_srcu);
 
        }
 
        rcu_read_lock();
-       pmu = idr_find(&pmu_idr, event->attr.type);
+       /*
+        * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE
+        * are often aliases for PERF_TYPE_RAW.
+        */
+       type = event->attr.type;
+       if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)
+               type = PERF_TYPE_RAW;
+
+again:
+       pmu = idr_find(&pmu_idr, type);
        rcu_read_unlock();
        if (pmu) {
                ret = perf_try_init_event(pmu, event);
+               if (ret == -ENOENT && event->attr.type != type) {
+                       type = event->attr.type;
+                       goto again;
+               }
+
                if (ret)
                        pmu = ERR_PTR(ret);
+
                goto unlock;
        }