Skip to contentSkip to conversation

trace-cmd: Still work without memfd_create()

PATCH New Steven Rostedt 1 participant 3 messages Apr 28, 2022 Kbuild

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Still work without memfd_create()

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

As some systems do not implement memfd_create() be able to fallback to the tempfile method.

Depends on: https://lore.kernel.org/r/[email protected]
https://patchwork.kernel.org/project/linux-trace-devel/patch/[email protected]/

Steven Rostedt (Google) (2):
trace-cmd Makefile: Change test-build to link as well trace-cmd agent: Test if memfd_create() is available

 Makefile                                          | 13 +++++++++++--
 lib/trace-cmd/include/private/trace-cmd-private.h |  5 +++++
 lib/trace-cmd/trace-msg.c                         |  8 ++++++++
 3 files changed, 24 insertions(+), 2 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/2 trace-cmd Makefile: Change test-build to link as well

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

Remove the '-c' option from the test-build macro build. To test if a function exists, it needs to go through the link phase, otherwise it can pass with an undefined symbol.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index 982514baad01..9fbc6de25d05 100644
@@ -163,7 +163,7 @@ export NO_PYTHON
 # $(call test-build, snippet, ret) -> ret if snippet compiles
 #                                  -> empty otherwise
 test-build = $(if $(shell sh -c 'echo "$(1)" | \
-	$(CC) -o /dev/null -c -x c - > /dev/null 2>&1 && echo y'), $2)
+	$(CC) -o /dev/null -x c - > /dev/null 2>&1 && echo y'), $2)
 
 UDIS86_AVAILABLE := $(call test-build,\#include <udis86.h>, y)
 ifneq ($(strip $(UDIS86_AVAILABLE)), y)
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/2 trace-cmd agent: Test if memfd_create() is available

In reply to Steven Rostedt

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

Some systems do not have memfd_create() available and it is a regression to require it. Fall back to tempfs if memfd_create() is not available at build time.

---
 Makefile                                          | 11 ++++++++++-
 lib/trace-cmd/include/private/trace-cmd-private.h |  5 +++++
 lib/trace-cmd/trace-msg.c                         |  8 ++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
Makefile +10 -1
index 9fbc6de25d05..abc4ac723db4 100644
@@ -184,6 +184,15 @@ endef
 # have flush/fua block layer instead of barriers?
 blk-flags := $(call test-build,$(BLK_TC_FLUSH_SOURCE),-DHAVE_BLK_TC_FLUSH)
 
+define MEMFD_CREATE_SOURCE
+#define _GNU_SOURCE
+#include <sys/mman.h>
+int main(void) { return memfd_create(\"test\", 0); }
+endef
+
+# have memfd_create available
+memfd-flags := $(call test-build,$(MEMFD_CREATE_SOURCE),-DHAVE_MEMFD_CREATE)
+
 ifeq ("$(origin O)", "command line")
 
   saved-output := $(O)
@@ -363,7 +372,7 @@ endif
 # Append required CFLAGS
 override CFLAGS += $(INCLUDES) $(VAR_DIR)
 override CFLAGS += $(PLUGIN_DIR_TRACECMD_SQ)
-override CFLAGS += $(udis86-flags) $(blk-flags)
+override CFLAGS += $(udis86-flags) $(blk-flags) $(memfd-flags)
 override LDFLAGS += $(udis86-ldflags)
 
 CMD_TARGETS = trace-cmd $(BUILD_PYTHON)
lib/trace-cmd/include/private/trace-cmd-private.h +5 -0
index 0efc2a1c4850..3b84170f2258 100644
@@ -380,6 +380,8 @@ enum tracecmd_msg_flags {
 	TRACECMD_MSG_FL_USE_VSOCK	= 1 << 1,
 };
 
+#define MSG_CACHE_FILE "/tmp/trace_msg_cacheXXXXXX"
+
 /* for both client and server */
 struct tracecmd_msg_handle {
 	int			fd;
@@ -389,6 +391,9 @@ struct tracecmd_msg_handle {
 	bool			done;
 	bool			cache;
 	int			cfd;
+#ifndef HAVE_MEMFD_CREATE
+	char			cfile[sizeof(MSG_CACHE_FILE)];
+#endif
 };
 
 struct tracecmd_tsync_protos {
lib/trace-cmd/trace-msg.c +8 -0
index 5ba2eeb2d183..9899fa8796e2 100644
@@ -591,9 +591,17 @@ tracecmd_msg_handle_alloc(int fd, unsigned long flags)
 int tracecmd_msg_handle_cache(struct tracecmd_msg_handle *msg_handle)
 {
 	if (msg_handle->cfd < 0) {
+#ifdef HAVE_MEMFD_CREATE
 		msg_handle->cfd = memfd_create("trace_msg_cache", 0);
 		if (msg_handle->cfd < 0)
 			return -1;
+#else
+		strcpy(msg_handle->cfile, MSG_CACHE_FILE);
+		msg_handle->cfd = mkstemp(msg_handle->cfile);
+		if (msg_handle->cfd < 0)
+			return -1;
+		unlink(msg_handle->cfile);
+#endif
 	}
 	msg_handle->cache = true;
 	return 0;
Signature · 1 line
2.35.1