Skip to contentSkip to conversation

drivers/perf: Fix kernel panic due to the invalid mon_ctx pointer

PATCH Superseded Shanker Donthineni 2 participants 3 messages Oct 26, 2023 Arm PMU profiling and debugging

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/perf: Fix kernel panic due to the invalid mon_ctx pointer

The return pointer from the resctrl_arch_mon_ctx_alloc_no_wait() function is saved in a 32-bit variable 'hwc->idx,' which results in the loss of the upper 32 bits. This, in turn, triggers a kernel panic when attempting to access a corrupted pointer.

This patch addresses the issue by utilizing the IDR data structure to keep track of a 32-bit value associated with a pointer (64-bit value).

---
 drivers/perf/resctrl_pmu.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
drivers/perf/resctrl_pmu.c +24 -3
index 99a2b90b5d83..68cda619da48 100644
@@ -6,6 +6,8 @@
 #include <linux/perf_event.h>
 #include <linux/platform_device.h>
 #include <linux/resctrl.h>
+#include <linux/mutex.h>
+#include <linux/idr.h>
 
 struct resctrl_pmu {
 	struct pmu pmu;
@@ -25,6 +27,9 @@ RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 7);
 RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(domain, config, 16, 23);
 RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(resctrl_id, config1, 0, 63);
 
+static DEFINE_MUTEX(resctrl_pmu_lock);
+static DEFINE_IDR(resctrl_pmu_idr);
+
 static void resctrl_pmu_do_nothing(struct pmu *pmu)
 {
 }
@@ -69,12 +74,17 @@ static void resctrl_pmu_event_destroy(struct perf_event *event)
 	struct hw_perf_event *hwc = &event->hw;
 	u16 event_num = get_event(event);
 	struct rdt_resource *r;
+	void *mon_ctx;
 
 	r = resctrl_event_get_resource(event_num);
 	if (!r)
 		return;
 
-	resctrl_arch_mon_ctx_free(r, event_num, hwc->idx);
+	mutex_lock(&resctrl_pmu_lock);
+	mon_ctx = idr_remove(&resctrl_pmu_idr, hwc->idx);
+	mutex_unlock(&resctrl_pmu_lock);
+
+	resctrl_arch_mon_ctx_free(r, event_num, mon_ctx);
 }
 
 static int resctrl_pmu_event_init(struct perf_event *event)
@@ -87,6 +97,7 @@ static int resctrl_pmu_event_init(struct perf_event *event)
 	struct rdt_domain *d;
 	u16 event_num, domain_id;
 	u32 closid, rmid;
+	void *mon_ctx;
 	int err;
 	u64 id;
 
@@ -144,7 +155,12 @@ static int resctrl_pmu_event_init(struct perf_event *event)
 			return -EINVAL;
 	}
 
-	hwc->idx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num);
+	mon_ctx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num);
+
+	mutex_lock(&resctrl_pmu_lock);
+	hwc->idx = idr_alloc(&resctrl_pmu_idr, mon_ctx, 0, INT_MAX, GFP_KERNEL);
+	mutex_unlock(&resctrl_pmu_lock);
+
 	if (hwc->idx == -ENOSPC)
 		return -ENOSPC;
 	event->destroy = resctrl_pmu_event_destroy;
@@ -162,6 +178,7 @@ static void resctrl_pmu_event_update(struct perf_event *event)
 	struct rdt_resource *r;
 	struct rdt_domain *d;
 	u32 closid, rmid;
+	void *mon_ctx;
 	int err;
 
 	event_num = get_event(event);
@@ -179,11 +196,15 @@ static void resctrl_pmu_event_update(struct perf_event *event)
 	if (!cpumask_test_cpu(smp_processor_id(), &d->cpu_mask))
 		return;
 
+	mutex_lock(&resctrl_pmu_lock);
+	mon_ctx = idr_find(&resctrl_pmu_idr, hwc->idx);
+	mutex_unlock(&resctrl_pmu_lock);
+
 	do {
 		prev = local64_read(&hwc->prev_count);
 
 		err = resctrl_arch_rmid_read(r, d, closid, rmid,
-					     event_num, &now, hwc->idx);
+					     event_num, &now, mon_ctx);
 		if (err)
 			return;
 	} while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
Signature · 7 lines
2.25.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw

[ Since I made the mistake of looking... ]

Quoted 7 lines · Shanker Donthineni

The return pointer from the resctrl_arch_mon_ctx_alloc_no_wait() function is saved in a 32-bit variable 'hwc->idx,' which results in the loss of the upper 32 bits. This, in turn, triggers a kernel panic when attempting to access a corrupted pointer.

This patch addresses the issue by utilizing the IDR data structure to keep track of a 32-bit value associated with a pointer (64-bit value).

This seems unnecessaraily complicated - there's plenty of space in that anonymous hw_perf_event "driver data" union (I know I cram quite a lot in there for arm-cmn) so it should just be a case of picking a different appropriately-sized field to (ab)use.

Thanks,
Robin.

Quoted 84 lines
---
  drivers/perf/resctrl_pmu.c | 27 ++++++++++++++++++++++++---
  1 file changed, 24 insertions(+), 3 deletions(-)
drivers/perf/resctrl_pmu.c +22 -2
index 99a2b90b5d83..68cda619da48 100644
@@ -6,6 +6,8 @@
  #include <linux/perf_event.h>
  #include <linux/platform_device.h>
  #include <linux/resctrl.h>
+#include <linux/mutex.h>
+#include <linux/idr.h>
 
  struct resctrl_pmu {
  	struct pmu pmu;
@@ -25,6 +27,9 @@ RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 7);
  RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(domain, config, 16, 23);
  RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(resctrl_id, config1, 0, 63);
 
+static DEFINE_MUTEX(resctrl_pmu_lock);
+static DEFINE_IDR(resctrl_pmu_idr);
+
  static void resctrl_pmu_do_nothing(struct pmu *pmu)
  {
  }
@@ -69,12 +74,17 @@ static void resctrl_pmu_event_destroy(struct perf_event *event)
  	struct hw_perf_event *hwc = &event->hw;
  	u16 event_num = get_event(event);
  	struct rdt_resource *r;
+	void *mon_ctx;
 
  	r = resctrl_event_get_resource(event_num);
  	if (!r)
  		return;
 
-	resctrl_arch_mon_ctx_free(r, event_num, hwc->idx);
+	mutex_lock(&resctrl_pmu_lock);
+	mon_ctx = idr_remove(&resctrl_pmu_idr, hwc->idx);
+	mutex_unlock(&resctrl_pmu_lock);
+
+	resctrl_arch_mon_ctx_free(r, event_num, mon_ctx);
  }
 
  static int resctrl_pmu_event_init(struct perf_event *event)
@@ -87,6 +97,7 @@ static int resctrl_pmu_event_init(struct perf_event *event)
  	struct rdt_domain *d;
  	u16 event_num, domain_id;
  	u32 closid, rmid;
+	void *mon_ctx;
  	int err;
  	u64 id;
 
@@ -144,7 +155,12 @@ static int resctrl_pmu_event_init(struct perf_event *event)
  			return -EINVAL;
  	}
 
-	hwc->idx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num);
+	mon_ctx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num);
+
+	mutex_lock(&resctrl_pmu_lock);
+	hwc->idx = idr_alloc(&resctrl_pmu_idr, mon_ctx, 0, INT_MAX, GFP_KERNEL);
+	mutex_unlock(&resctrl_pmu_lock);
+
  	if (hwc->idx == -ENOSPC)
  		return -ENOSPC;
  	event->destroy = resctrl_pmu_event_destroy;
@@ -162,6 +178,7 @@ static void resctrl_pmu_event_update(struct perf_event *event)
  	struct rdt_resource *r;
  	struct rdt_domain *d;
  	u32 closid, rmid;
+	void *mon_ctx;
  	int err;
 
  	event_num = get_event(event);
@@ -179,11 +196,15 @@ static void resctrl_pmu_event_update(struct perf_event *event)
  	if (!cpumask_test_cpu(smp_processor_id(), &d->cpu_mask))
  		return;
 
+	mutex_lock(&resctrl_pmu_lock);
+	mon_ctx = idr_find(&resctrl_pmu_idr, hwc->idx);
+	mutex_unlock(&resctrl_pmu_lock);

drivers/perf/resctrl_pmu.c static void resctrl_pmu_event_update(struct perf_event *event) · line 210

Quoted 10 lines
drivers/perf/resctrl_pmu.c +2 -1
+
  	do {
  		prev = local64_read(&hwc->prev_count);
 
  		err = resctrl_arch_rmid_read(r, d, closid, rmid,
-					     event_num, &now, hwc->idx);
+					     event_num, &now, mon_ctx);
  		if (err)
  			return;
  	} while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#2 lore raw

Hi Robin,

Quoted 11 lines · Robin Murphy

External email: Use caution opening links or attachments

[ Since I made the mistake of looking... ]

Quoted 5 lines · Shanker Donthineni

The return pointer from the resctrl_arch_mon_ctx_alloc_no_wait() function is saved in a 32-bit variable 'hwc->idx,' which results in the loss of the upper 32 bits. This, in turn, triggers a kernel panic when attempting to access a corrupted pointer.

Quoted 8 lines
Quoted 2 lines

This patch addresses the issue by utilizing the IDR data structure to keep track of a 32-bit value associated with a pointer (64-bit value).

This seems unnecessaraily complicated - there's plenty of space in that anonymous hw_perf_event "driver data" union (I know I cram quite a lot in there for arm-cmn) so it should just be a case of picking a different appropriately-sized field to (ab)use.

It appears that there is already a private pointer named 'pmu_private' in the 'perf_event' structure. I will submit version 2 of the code with this modification.

drivers/perf/resctrl_pmu.c +5 -5
@@ -66,7 +66,6 @@ static struct rdt_resource *resctrl_event_get_resource(u16 event_num)
 
 static void resctrl_pmu_event_destroy(struct perf_event *event)
 {
-       struct hw_perf_event *hwc = &event->hw;
        u16 event_num = get_event(event);
        struct rdt_resource *r;
 
@@ -74,7 +73,7 @@ static void resctrl_pmu_event_destroy(struct perf_event *event)
        if (!r)
                return;
 
-       resctrl_arch_mon_ctx_free(r, event_num, hwc->idx);
+       resctrl_arch_mon_ctx_free(r, event_num, event->pmu_private);
 }
 
 static int resctrl_pmu_event_init(struct perf_event *event)
@@ -144,8 +143,8 @@ static int resctrl_pmu_event_init(struct perf_event *event)
                        return -EINVAL;
        }
 
-       hwc->idx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num);
-       if (hwc->idx == -ENOSPC)
+       event->pmu_private = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num);
+       if (IS_ERR_OR_NULL(event->pmu_private))
                return -ENOSPC;
        event->destroy = resctrl_pmu_event_destroy;
        local64_set(&hwc->prev_count, 0);
@@ -183,7 +182,8 @@ static void resctrl_pmu_event_update(struct perf_event *event)
                prev = local64_read(&hwc->prev_count);
 
                err = resctrl_arch_rmid_read(r, d, closid, rmid,
-                                            event_num, &now, hwc->idx);
+                                            event_num, &now,
+                                            event->pmu_private);
 
 
 
Quoted 91 lines

Thanks,
Robin.

Quoted 88 lines

---
  drivers/perf/resctrl_pmu.c | 27 ++++++++++++++++++++++++---

  1 file changed, 24 insertions(+), 3 deletions(-)
drivers/perf/resctrl_pmu.c +23 -2
index 99a2b90b5d83..68cda619da48 100644
@@ -6,6 +6,8 @@
   #include <linux/perf_event.h>
   #include <linux/platform_device.h>
   #include <linux/resctrl.h>
+#include <linux/mutex.h>
+#include <linux/idr.h>
 
   struct resctrl_pmu {
       struct pmu pmu;
@@ -25,6 +27,9 @@ RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 7);
   RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(domain, config, 16, 23);
   RESCTRL_PMU_EVENT_ATTR_EXTRACTOR(resctrl_id, config1, 0, 63);
 
+static DEFINE_MUTEX(resctrl_pmu_lock);
+static DEFINE_IDR(resctrl_pmu_idr);
+
   static void resctrl_pmu_do_nothing(struct pmu *pmu)
   {
   }
@@ -69,12 +74,17 @@ static void resctrl_pmu_event_destroy(struct perf_event *event)
       struct hw_perf_event *hwc = &event->hw;
       u16 event_num = get_event(event);
       struct rdt_resource *r;
+     void *mon_ctx;
 
       r = resctrl_event_get_resource(event_num);
       if (!r)
               return;
 
-     resctrl_arch_mon_ctx_free(r, event_num, hwc->idx);
+     mutex_lock(&resctrl_pmu_lock);
+     mon_ctx = idr_remove(&resctrl_pmu_idr, hwc->idx);
+     mutex_unlock(&resctrl_pmu_lock);
+
+     resctrl_arch_mon_ctx_free(r, event_num, mon_ctx);
   }
 
   static int resctrl_pmu_event_init(struct perf_event *event)
@@ -87,6 +97,7 @@ static int resctrl_pmu_event_init(struct perf_event *event)
       struct rdt_domain *d;
       u16 event_num, domain_id;
       u32 closid, rmid;
+     void *mon_ctx;
       int err;
       u64 id;
 
@@ -144,7 +155,12 @@ static int resctrl_pmu_event_init(struct perf_event *event)
                       return -EINVAL;
       }
 
-     hwc->idx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num);
+     mon_ctx = resctrl_arch_mon_ctx_alloc_no_wait(r, event_num);
+
+     mutex_lock(&resctrl_pmu_lock);
+     hwc->idx = idr_alloc(&resctrl_pmu_idr, mon_ctx, 0, INT_MAX, GFP_KERNEL);
+     mutex_unlock(&resctrl_pmu_lock);
+
       if (hwc->idx == -ENOSPC)
               return -ENOSPC;
       event->destroy = resctrl_pmu_event_destroy;
@@ -162,6 +178,7 @@ static void resctrl_pmu_event_update(struct perf_event *event)
       struct rdt_resource *r;
       struct rdt_domain *d;
       u32 closid, rmid;
+     void *mon_ctx;
       int err;
 
       event_num = get_event(event);
@@ -179,11 +196,15 @@ static void resctrl_pmu_event_update(struct perf_event *event)
       if (!cpumask_test_cpu(smp_processor_id(), &d->cpu_mask))
               return;
 
+     mutex_lock(&resctrl_pmu_lock);
+     mon_ctx = idr_find(&resctrl_pmu_idr, hwc->idx);
+     mutex_unlock(&resctrl_pmu_lock);
+
       do {
               prev = local64_read(&hwc->prev_count);
 
Quoted 6 lines

drivers/perf/resctrl_pmu.c static void resctrl_pmu_event_update(struct perf_event *event) · line 210

Quoted 6 lines
drivers/perf/resctrl_pmu.c +1 -1
               err = resctrl_arch_rmid_read(r, d, closid, rmid,
-                                          event_num, &now, hwc->idx);
+                                          event_num, &now, mon_ctx);
               if (err)
                       return;
       } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel