Skip to contentSkip to conversation

kernel-shark: Add check for return of ksmodel_get_cpu_front()

PATCH In review Steven Rostedt 2 participants 5 messages May 7, 2021 – May 14, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel-shark: Add check for return of ksmodel_get_cpu_front()

From: Steven Rostedt (VMware) <[email protected]>

When I loaded two trace.dat files (host and guest), but the mapping of which host thread is associated to which guest vCPU was missing from the file, it caused a SEGFAULT. That's because in fillTaskGraph(), the lamGetPidCPU() calls ksmodule_get_cpu_front() which returns a negative number and does not set index. But the next line checks data[index] where index is some random number, and the application crashes.

By checking the return of ksmodule_get_cpu_front(), and if it is negative do not reference data[index] and just let eFront be nullptr, the application shows no mapping, but at least it does not crash.

---
You can reproduce this with:

http://rostedt.org/private/trace-no-map.tar.bz2

src/KsPlotTools.cpp +1 -1
index 225dc34..abef5f8 100644
@@ -1280,7 +1280,7 @@ void Graph::fillTaskGraph(int sd, int pid)
 						 false,
 						 _collectionPtr,
 						 &index);
-		if (index >= 0)
+		if (cpuFront >= 0 && index >= 0)
 			eFront = _histoPtr->data[index];
 
 		cpuBack = ksmodel_get_cpu_back(_histoPtr, bin,
#1 lore raw

Hi Steven,

Quoted 24 lines · Steven Rostedt

From: Steven Rostedt (VMware) <[email protected]>

When I loaded two trace.dat files (host and guest), but the mapping of which host thread is associated to which guest vCPU was missing from the file, it caused a SEGFAULT. That's because in fillTaskGraph(), the lamGetPidCPU() calls ksmodule_get_cpu_front() which returns a negative number and does not set index. But the next line checks data[index] where index is some random number, and the application crashes.

By checking the return of ksmodule_get_cpu_front(), and if it is negative do not reference data[index] and just let eFront be nullptr, the application shows no mapping, but at least it does not crash.

---
You can reproduce this with:

http://rostedt.org/private/trace-no-map.tar.bz2

src/KsPlotTools.cpp +0 -0
index 225dc34..abef5f8 100644

src/KsPlotTools.cpp void Graph::fillTaskGraph(int sd, int pid) · line 1282

Quoted 3 lines
src/KsPlotTools.cpp +0 -0
@@ -1280,7 +1280,7 @@ void Graph::fillTaskGraph(int sd, int pid)
  						 false,
  						 _collectionPtr,
  						 &index);

I wonder why this fails? Is it because "pid" is negative? The fix below is appropriate, however we should detect negative PIDs and abort plotting much earlier.

Thanks!
Yordan

Quoted 6 lines
src/KsPlotTools.cpp +1 -1
-		if (index >= 0)
+		if (cpuFront >= 0 && index >= 0)
  			eFront = _histoPtr->data[index];
 
  		cpuBack = ksmodel_get_cpu_back(_histoPtr, bin,
#2 lore raw
Quoted 4 lines · On Tue, 11 May 2021 16:25:35 +0300 Yordan Karadzhov <[email protected]> wrote:
Quoted 4 lines

You can reproduce this with:

http://rostedt.org/private/trace-no-map.tar.bz2

Quoted 12 lines

src/KsPlotTools.cpp void Graph::fillTaskGraph(int sd, int pid) · line 1282

Quoted 8 lines
src/KsPlotTools.cpp +0 -0
index 225dc34..abef5f8 100644
@@ -1280,7 +1280,7 @@ void Graph::fillTaskGraph(int sd, int pid)
  						 false,
  						 _collectionPtr,
  						 &index);  

I wonder why this fails? Is it because "pid" is negative? The fix below is appropriate, however we should detect negative PIDs and abort plotting much earlier.

I believe the issue is that we failed to map which host task goes with which guest vCPUU, and just randomly picked one (or none).

I fixed trace-cmd to get the mappings when qemu is not found, and KernelShark works fine on that case. This bug only appears when it can't find the host thread that corresponds to the guest vCPU.

-- Steve

#3 lore raw
Quoted 6 lines · Steven Rostedt
Quoted 3 lines · On Tue, 11 May 2021 16:25:35 +0300 Yordan Karadzhov <[email protected]> wrote:
Quoted 3 lines

You can reproduce this with:

http://rostedt.org/private/trace-no-map.tar.bz2

What should I do to make it SEGFAULT?
Y.

Quoted 23 lines
Quoted 13 lines

src/KsPlotTools.cpp void Graph::fillTaskGraph(int sd, int pid) · line 1282

Quoted 9 lines
src/KsPlotTools.cpp +0 -0
index 225dc34..abef5f8 100644
@@ -1280,7 +1280,7 @@ void Graph::fillTaskGraph(int sd, int pid)
   						 false,
   						 _collectionPtr,
   						 &index);

I wonder why this fails? Is it because "pid" is negative? The fix below is appropriate, however we should detect negative PIDs and abort plotting much earlier.

I believe the issue is that we failed to map which host task goes with which guest vCPUU, and just randomly picked one (or none).

I fixed trace-cmd to get the mappings when qemu is not found, and KernelShark works fine on that case. This bug only appears when it can't find the host thread that corresponds to the guest vCPU.

-- Steve

#4 lore raw
In reply to Steven Rostedt
Quoted 9 lines · Steven Rostedt

From: Steven Rostedt (VMware) <[email protected]>

When I loaded two trace.dat files (host and guest), but the mapping of which host thread is associated to which guest vCPU was missing from the file, it caused a SEGFAULT. That's because in fillTaskGraph(), the lamGetPidCPU() calls ksmodule_get_cpu_front() which returns a negative number and does not set index. But the next line checks data[index] where index is some random number, and the application crashes.

Quoted 5 lines

By checking the return of ksmodule_get_cpu_front(), and if it is negative do not reference data[index] and just let eFront be nullptr, the application shows no mapping, but at least it does not crash.

I am applying this one.

Thanks Steven!
Y.

Quoted 19 lines

---
You can reproduce this with:

http://rostedt.org/private/trace-no-map.tar.bz2

src/KsPlotTools.cpp +1 -1
index 225dc34..abef5f8 100644
@@ -1280,7 +1280,7 @@ void Graph::fillTaskGraph(int sd, int pid)
  						 false,
  						 _collectionPtr,
  						 &index);
-		if (index >= 0)
+		if (cpuFront >= 0 && index >= 0)
  			eFront = _histoPtr->data[index];
 
  		cpuBack = ksmodel_get_cpu_back(_histoPtr, bin,