Skip to contentSkip to conversation

perf inject jit: Ignore memfd mmap events if jitdump present

PATCH Superseded Brian Robbins 3 participants 6 messages Aug 2, 2022 – Aug 10, 2022 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf inject jit: Ignore memfd mmap events if jitdump present

Some processes store jitted code in memfd mappings to avoid having rwx mappings. These processes map the code with a writeable mapping and a read-execute mapping. They write the code using the writeable mapping and then unmap the writeable mapping. All subsequent execution is through the read-execute mapping.

perf inject --jit ignores //anon* mappings for each process where a jitdump is present because it expects to inject mmap events for each jitted code range, and said jitted code ranges will overlap with the //anon* mappings.

Ignore /memfd: mappings so that jitted code contained in /memfd:
mappings is treated the same way as jitted code contained in //anon* mappings.

---
 tools/perf/util/jitdump.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/util/jitdump.c +6 -2
index a23255773c60..335a3c61940b 100644
@@ -845,8 +845,12 @@ jit_process(struct perf_session *session,
 	if (jit_detect(filename, pid, nsi)) {
 		nsinfo__put(nsi);
 
-		// Strip //anon* mmaps if we processed a jitdump for this pid
-		if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0))
+		/*
+		 * Strip //anon* and /memfd:* mmaps if we processed a jitdump for this pid
+		 */
+		if (jit_has_pid(machine, pid) &&
+			((strncmp(filename, "//anon", 6) == 0) ||
+			 (strncmp(filename, "/memfd:", 7) == 0))
 			return 1;
 
 		return 0;
Signature · 1 line
2.25.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Brian Robbins

Some processes store jitted code in memfd mappings to avoid having rwx mappings. These processes map the code with a writeable mapping and a read-execute mapping. They write the code using the writeable mapping and then unmap the writeable mapping. All subsequent execution is through the read-execute mapping.

perf inject --jit ignores //anon* mappings for each process where a jitdump is present because it expects to inject mmap events for each jitted code range, and said jitted code ranges will overlap with the //anon* mappings.

Quoted 5 lines

Ignore /memfd: mappings so that jitted code contained in /memfd:
mappings is treated the same way as jitted code contained in //anon* mappings.

Quoted 10 lines
---
 tools/perf/util/jitdump.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/util/jitdump.c +0 -0
index a23255773c60..335a3c61940b 100644
@@ -845,8 +845,12 @@ jit_process(struct perf_session *session,
        if (jit_detect(filename, pid, nsi)) {

tools/perf/util/jitdump.c jit_process(struct perf_session *session, · line 853

Quoted 10 lines
tools/perf/util/jitdump.c +6 -2
                nsinfo__put(nsi);
 
-               // Strip //anon* mmaps if we processed a jitdump for this pid
-               if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0))
+               /*
+                * Strip //anon* and /memfd:* mmaps if we processed a jitdump for this pid
+                */
+               if (jit_has_pid(machine, pid) &&
+                       ((strncmp(filename, "//anon", 6) == 0) ||
+                        (strncmp(filename, "/memfd:", 7) == 0))

Related to this there is the prctl PR_SET_VMA_ANON_NAME which will name mapping to start with "[anon:"
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/proc.rst#n434
I wonder also we should be checking the pages are executable.

Thanks,
Ian

Quoted 6 lines
tools/perf/util/jitdump.c +0 -1
                        return 1;
 
                return 0;
--

2.25.1

#2 lore raw
Quoted 48 lines · Ian Rogers
Quoted 12 lines · Brian Robbins

Some processes store jitted code in memfd mappings to avoid having rwx mappings. These processes map the code with a writeable mapping and a read-execute mapping. They write the code using the writeable mapping and then unmap the writeable mapping. All subsequent execution is through the read-execute mapping.

perf inject --jit ignores //anon* mappings for each process where a jitdump is present because it expects to inject mmap events for each jitted code range, and said jitted code ranges will overlap with the //anon* mappings.

Quoted 5 lines

Ignore /memfd: mappings so that jitted code contained in /memfd:
mappings is treated the same way as jitted code contained in //anon* mappings.

Quoted 10 lines
---
 tools/perf/util/jitdump.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/util/jitdump.c +0 -0
index a23255773c60..335a3c61940b 100644
@@ -845,8 +845,12 @@ jit_process(struct perf_session *session,
        if (jit_detect(filename, pid, nsi)) {

tools/perf/util/jitdump.c jit_process(struct perf_session *session, · line 853

Quoted 10 lines
tools/perf/util/jitdump.c +6 -2
                nsinfo__put(nsi);
 
-               // Strip //anon* mmaps if we processed a jitdump for this pid
-               if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0))
+               /*
+                * Strip //anon* and /memfd:* mmaps if we processed a jitdump for this pid
+                */
+               if (jit_has_pid(machine, pid) &&
+                       ((strncmp(filename, "//anon", 6) == 0) ||
+                        (strncmp(filename, "/memfd:", 7) == 0))

Related to this there is the prctl PR_SET_VMA_ANON_NAME which will name mapping to start with "[anon:"
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/proc.rst#n434
I wonder also we should be checking the pages are executable.

Quoted 9 lines

Thanks,
Ian

Quoted 6 lines
tools/perf/util/jitdump.c +0 -1
                        return 1;
 
                return 0;
--

2.25.1

I have not run into this case yet, but I suspect you are right that this should be handled as well. I can create a follow-up patch for this.

Thanks.
--Brian

#3 lore raw
In reply to Ian Rogers

Em Thu, Aug 04, 2022 at 08:22:14AM -0700, Ian Rogers escreveu:

Quoted 14 lines
Quoted 12 lines · Brian Robbins

Some processes store jitted code in memfd mappings to avoid having rwx mappings. These processes map the code with a writeable mapping and a read-execute mapping. They write the code using the writeable mapping and then unmap the writeable mapping. All subsequent execution is through the read-execute mapping.

perf inject --jit ignores //anon* mappings for each process where a jitdump is present because it expects to inject mmap events for each jitted code range, and said jitted code ranges will overlap with the //anon* mappings.

Quoted 7 lines
Quoted 5 lines

Ignore /memfd: mappings so that jitted code contained in /memfd:
mappings is treated the same way as jitted code contained in //anon* mappings.

  CC      /tmp/build/perf/util/jitdump.o
  CC      /tmp/build/perf/pmu-events/pmu-events.o
  LD      /tmp/build/perf/pmu-events/pmu-events-in.o
util/jitdump.c: In function ‘jit_process’:
util/jitdump.c:853:65: error: expected ‘)’ before ‘return’
  853 |                          (strncmp(filename, "/memfd:", 7) == 0))
      |                                                                 ^
      |                                                                 )
  854 |                         return 1;
      |                         ~~~~~~

util/jitdump.c:851:20: note: to match this ‘(’
851 | if (jit_has_pid(machine, pid) &&
| ^
util/jitdump.c:857:9: error: expected expression before ‘}’ token

  857 |         }
      |         ^

make[4]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:96: /tmp/build/perf/util/jitdump.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:139: util] Error 2

Trying to fix now.

- Arnaldo

Quoted 35 lines
Quoted 10 lines
---
 tools/perf/util/jitdump.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/util/jitdump.c +0 -0
index a23255773c60..335a3c61940b 100644
@@ -845,8 +845,12 @@ jit_process(struct perf_session *session,
        if (jit_detect(filename, pid, nsi)) {

tools/perf/util/jitdump.c jit_process(struct perf_session *session, · line 853

Quoted 10 lines
tools/perf/util/jitdump.c +6 -2
                nsinfo__put(nsi);
 
-               // Strip //anon* mmaps if we processed a jitdump for this pid
-               if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0))
+               /*
+                * Strip //anon* and /memfd:* mmaps if we processed a jitdump for this pid
+                */
+               if (jit_has_pid(machine, pid) &&
+                       ((strncmp(filename, "//anon", 6) == 0) ||
+                        (strncmp(filename, "/memfd:", 7) == 0))

Related to this there is the prctl PR_SET_VMA_ANON_NAME which will name mapping to start with "[anon:"
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/proc.rst#n434
I wonder also we should be checking the pages are executable.

Thanks,
Ian

Quoted 6 lines
tools/perf/util/jitdump.c +0 -1
                        return 1;
 
                return 0;
--

2.25.1

Signature · 1 line
- Arnaldo
#4 lore raw

Em Wed, Aug 10, 2022 at 10:00:14AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 20 lines

Em Thu, Aug 04, 2022 at 08:22:14AM -0700, Ian Rogers escreveu:

Quoted 4 lines · Brian Robbins
Quoted 1 line
  CC      /tmp/build/perf/util/jitdump.o
  CC      /tmp/build/perf/pmu-events/pmu-events.o
  LD      /tmp/build/perf/pmu-events/pmu-events-in.o
util/jitdump.c: In function ‘jit_process’:
util/jitdump.c:853:65: error: expected ‘)’ before ‘return’
  853 |                          (strncmp(filename, "/memfd:", 7) == 0))
      |                                                                 ^
      |                                                                 )
  854 |                         return 1;
      |                         ~~~~~~

util/jitdump.c:851:20: note: to match this ‘(’
851 | if (jit_has_pid(machine, pid) &&
| ^
util/jitdump.c:857:9: error: expected expression before ‘}’ token

Quoted 8 lines
  857 |         }
      |         ^

make[4]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:96: /tmp/build/perf/util/jitdump.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:139: util] Error 2

Trying to fix now.

Trivial, please compile test it...

- Arnaldo

tools/perf/util/jitdump.c +1 -1
index 335a3c61940b7b39..9983d21a5c42d70e 100644
@@ -850,7 +850,7 @@ jit_process(struct perf_session *session,
 		 */
 		if (jit_has_pid(machine, pid) &&
 			((strncmp(filename, "//anon", 6) == 0) ||
-			 (strncmp(filename, "/memfd:", 7) == 0))
+			 (strncmp(filename, "/memfd:", 7) == 0)))
 			return 1;
 
 		return 0;
#5 lore raw
Quoted 39 lines · Arnaldo Carvalho de Melo

Em Wed, Aug 10, 2022 at 10:00:14AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 20 lines

Em Thu, Aug 04, 2022 at 08:22:14AM -0700, Ian Rogers escreveu:

Quoted 4 lines · Brian Robbins
Quoted 1 line
  CC      /tmp/build/perf/util/jitdump.o
  CC      /tmp/build/perf/pmu-events/pmu-events.o
  LD      /tmp/build/perf/pmu-events/pmu-events-in.o
util/jitdump.c: In function ‘jit_process’:
util/jitdump.c:853:65: error: expected ‘)’ before ‘return’
  853 |                          (strncmp(filename, "/memfd:", 7) == 0))
      |                                                                 ^
      |                                                                 )
  854 |                         return 1;
      |                         ~~~~~~

util/jitdump.c:851:20: note: to match this ‘(’
851 | if (jit_has_pid(machine, pid) &&
| ^
util/jitdump.c:857:9: error: expected expression before ‘}’ token

Quoted 8 lines
  857 |         }
      |         ^

make[4]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:96: /tmp/build/perf/util/jitdump.o] Error 1 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [/var/home/acme/git/perf/tools/build/Makefile.build:139: util] Error 2

Trying to fix now.

Trivial, please compile test it...

- Arnaldo

tools/perf/util/jitdump.c +0 -0
index 335a3c61940b7b39..9983d21a5c42d70e 100644

tools/perf/util/jitdump.c jit_process(struct perf_session *session, · line 856

Quoted 8 lines
tools/perf/util/jitdump.c +1 -1
@@ -850,7 +850,7 @@ jit_process(struct perf_session *session,
 		 */
 		if (jit_has_pid(machine, pid) &&
 			((strncmp(filename, "//anon", 6) == 0) ||
-			 (strncmp(filename, "/memfd:", 7) == 0))
+			 (strncmp(filename, "/memfd:", 7) == 0)))
 			return 1;
 
 		return 0;

Apologies about that. Fixed this in v2.

--Brian