Skip to contentSkip to conversation

ctf: give shared kernel types a synthetic module owner

PATCH In review Kris Van Hees 2 participants 3 messages Oct 30 – Nov 6

lore
Share & tools
lore
#0 lore raw

PATCH 3/20 ctf: give shared kernel types a synthetic module owner

Store the shared dictionary in shared_ctf->dm_ctfp and remove dt_shared_ctf from the DTrace handle. Archive, embedded, and BTF sources use the same owner; BTF-backed vmlinux imports the parent instead of aliasing it. Register each dictionary owning module and use a module lookup helper for shared type consumers.

Release the shared dictionary through module destruction and reserve its name against userspace module creation. Retain handle-owned shared BTF until explicit BTF parents are introduced, and release the BTF path string on handle teardown.

Add regression coverage for archive and BTF parent imports, embedded CTF, missing data, and persistence across module discovery. Exercise shared-first and vmlinux-first loading on separate handles.

---
 libdtrace/dt_btf.c                   |  47 +++--
 libdtrace/dt_cc.c                    |  11 +-
 libdtrace/dt_cg.c                    |   2 +-
 libdtrace/dt_impl.h                  |  10 +-
 libdtrace/dt_module.c                |  74 ++++++--
 libdtrace/dt_module.h                |   1 +
 libdtrace/dt_open.c                  |   3 +-
 libdtrace/dt_prov_proc.c             |   5 +-
 libdtrace/dt_provider_tp.c           |   3 +-
 test/internals/btf/tst.shared-ctf.sh |  14 ++
 test/triggers/Build                  |   7 +-
 test/triggers/shared-ctf.c           | 265 +++++++++++++++++++++++++++
 12 files changed, 392 insertions(+), 50 deletions(-)
 create mode 100755 test/internals/btf/tst.shared-ctf.sh
 create mode 100644 test/triggers/shared-ctf.c
libdtrace/dt_btf.c +28 -19
index 54011e4d..dce44987 100644
@@ -383,7 +383,7 @@ dt_btf_add_to_ctf(dtrace_hdl_t *dtp, dt_btf_t *btf, ctf_dict_t *ctf,
 	 * If we already have shared_ctf, we may be looking for a type in that
 	 * shared_ctf.
 	 */
-	if (dtp->dt_shared_ctf && btf != dtp->dt_shared_btf) {
+	if (dt_module_shared_ctf(dtp) && btf != dtp->dt_shared_btf) {
 		if (type_id < dtp->dt_shared_btf->type_cnt)
 			return dtp->dt_shared_btf->ctfids[type_id];
 
@@ -724,9 +724,13 @@ dt_btf_to_ctf(dtrace_hdl_t *dtp, dt_module_t *dmp, dt_btf_t *btf)
 	if (dmp) {
 		int	err;
 
-		err = ctf_import(ctf, dtp->dt_shared_ctf);
-		if (err == CTF_ERR)
+		err = ctf_import(ctf, dt_module_shared_ctf(dtp));
+		if (err == CTF_ERR ||
+		    ctf_parent_name_set(ctf, "shared_ctf") == CTF_ERR) {
+			dt_ctf_error(dtp, ctf);
+			ctf_close(ctf);
 			return NULL;
+		}
 
 		ctf_setspecific(ctf, dmp);
 
@@ -775,8 +779,11 @@ dt_btf_to_ctf(dtrace_hdl_t *dtp, dt_module_t *dmp, dt_btf_t *btf)
 	}
 
 out:
-	if (ctf_update(ctf) == CTF_ERR)
+	if (ctf_update(ctf) == CTF_ERR) {
+		dt_ctf_error(dtp, ctf);
+		ctf_close(ctf);
 		return NULL;
+	}
 
 	return ctf;
 }
@@ -830,23 +837,25 @@ dt_btf_module_ctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
 
 #ifdef HAVE_LIBCTF
 	/*
-	 * If the module is 'vmlinux', we are creating the shared CTF.
-	 * If the module uses the shared BTF, we create CTF for a NULL BTF.
-	 * Otherwise, convert the module BTF.
+	 * The shared module owns the dictionary generated from vmlinux BTF.
+	 * vmlinux itself, like every other module, imports this dictionary.
 	 */
-	if (strcmp(dmp->dm_name, "vmlinux") == 0)
-		ctf = dt_btf_to_ctf(dtp, NULL, btf);
-	else if (btf == dtp->dt_shared_btf)
-		ctf = dt_btf_to_ctf(dtp, dmp, NULL);
-	else
-		ctf = dt_btf_to_ctf(dtp, dmp, btf);
+	if (dmp == dtp->dt_exec && dt_module_shared_ctf(dtp) == NULL) {
+		dt_module_t *shared = dt_module_create(dtp, "shared_ctf");
 
-	/*
-	 * If generating CTF data for the main kernel, store it as the shared
-	 * CTF.
-	 */
-	if (!dtp->dt_shared_ctf && dtp->dt_shared_btf && btf == dtp->dt_shared_btf)
-		dtp->dt_shared_ctf = ctf;
+		if (shared == NULL) {
+			dt_set_errno(dtp, EDT_NOMEM);
+			return NULL;
+		}
+		shared->dm_ctfp = dt_btf_to_ctf(dtp, NULL, btf);
+		if (shared->dm_ctfp == NULL)
+			return NULL;
+		ctf_setspecific(shared->dm_ctfp, shared);
+		shared->dm_flags |= DT_DM_LOADED;
+	}
+
+	ctf = dt_btf_to_ctf(dtp, dmp,
+			    btf == dtp->dt_shared_btf ? NULL : btf);
 
 	dt_dprintf("Generated %s CTF from BTF (%d types).\n", dmp->dm_name,
 		   btf->type_cnt);
libdtrace/dt_cc.c +6 -5
index b49ecb0e..4b639bdd 100644
@@ -79,6 +79,7 @@
 #include <dirent.h>
 #include <port.h>
 #include <dt_pcap.h>
+#include <dt_module.h>
 #include <dt_program.h>
 #include <dt_provider.h>
 #include <dt_probe.h>
@@ -1084,7 +1085,7 @@ dt_link_construct(dtrace_hdl_t *dtp, const dt_probe_t *prp, dtrace_difo_t *dp,
 			case DT_CONST_TASK_REAL_PARENT:
 			case DT_CONST_TASK_COMM:
 			case DT_CONST_TASK_MM: {
-				ctf_file_t *cfp = dtp->dt_shared_ctf;
+				ctf_file_t *cfp = dt_module_shared_ctf(dtp);
 				ctf_id_t type;
 				ctf_membinfo_t ctm;
 				int rc = 0;
@@ -1120,7 +1121,7 @@ dt_link_construct(dtrace_hdl_t *dtp, const dt_probe_t *prp, dtrace_difo_t *dp,
 			}
 			case DT_CONST_TASK_MM_ARG_START:
 			case DT_CONST_TASK_MM_ARG_END: {
-				ctf_file_t *cfp = dtp->dt_shared_ctf;
+				ctf_file_t *cfp = dt_module_shared_ctf(dtp);
 				ctf_id_t type;
 				ctf_membinfo_t ctm;
 				int rc = 0;
@@ -1146,7 +1147,7 @@ dt_link_construct(dtrace_hdl_t *dtp, const dt_probe_t *prp, dtrace_difo_t *dp,
 				continue;
 			}
 			case DT_CONST_MUTEX_OWNER: {
-				ctf_file_t *cfp = dtp->dt_shared_ctf;
+				ctf_file_t *cfp = dt_module_shared_ctf(dtp);
 				ctf_id_t type;
 				ctf_membinfo_t ctm;
 				int rc;
@@ -1175,7 +1176,7 @@ dt_link_construct(dtrace_hdl_t *dtp, const dt_probe_t *prp, dtrace_difo_t *dp,
 				 *
 				 * FIXME: what about CONFIG_PREEMPT_RT?
 				 */
-				ctf_file_t *cfp = dtp->dt_shared_ctf;
+				ctf_file_t *cfp = dt_module_shared_ctf(dtp);
 				ctf_id_t type;
 				ctf_membinfo_t ctm;
 				int rc;
@@ -1256,7 +1257,7 @@ dt_link_construct(dtrace_hdl_t *dtp, const dt_probe_t *prp, dtrace_difo_t *dp,
 	return pc;
 
  err_ctf:
-	dtp->dt_ctferr = ctf_errno(dtp->dt_shared_ctf);
+	dtp->dt_ctferr = ctf_errno(dt_module_shared_ctf(dtp));
 	return dt_set_errno(dtp, EDT_CTF);
 }
 
libdtrace/dt_cg.c +1 -1
index 275a311d..03d114d4 100644
@@ -5329,7 +5329,7 @@ dt_cg_uregs(unsigned int idx, dt_node_t *dnp, dt_irlist_t *dlp, dt_regset_t *drp
 		 * since it can be smaller, at least on aarch64.
 		 */
 		{
-			ctf_file_t *cfp = yypcb->pcb_hdl->dt_shared_ctf;
+			ctf_file_t *cfp = dt_module_shared_ctf(yypcb->pcb_hdl);
 			ctf_id_t type;
 
 			type = ctf_lookup_by_name(cfp, "struct pt_regs");
libdtrace/dt_impl.h +5 -5
index 0290e0b4..682dda82 100644
@@ -198,10 +198,11 @@ typedef struct dt_kern_path {
 	char *dkp_path;		       /* full name including path */
 } dt_kern_path_t;
 
-#define DT_DM_LOADED		0x1	/* module symbol and type data is loaded */
-#define DT_DM_KERNEL		0x2	/* module is associated with a kernel object */
-#define DT_DM_CTF_ARCHIVED	0x4	/* module found in a CTF archive */
-#define DT_DM_KERN_UNLOADED	0x8	/* module not loaded into the kernel */
+#define DT_DM_LOADED		0x01	/* module symbol and type data is loaded */
+#define DT_DM_KERNEL		0x02	/* module is associated with a kernel object */
+#define DT_DM_CTF_ARCHIVED	0x04	/* module found in a CTF archive */
+#define DT_DM_KERN_UNLOADED	0x08	/* module not loaded into the kernel */
+#define DT_DM_SHARED		0x20	/* module owns shared kernel CTF */
 #define DT_DM_KERN_SYMS		0x40	/* kernel symbols discovered and finalized */
 
 /*
@@ -311,7 +312,6 @@ struct dtrace_hdl {
 	void *dt_ctfa_data;	/* backing data for dt_ctfa */
 	char *dt_btf_path;	/* path to vmlinux.btf */
 	struct dt_btf *dt_shared_btf; /* BTF data for the kernel (shared) */
-	ctf_file_t *dt_shared_ctf; /* Handle to the shared CTF */
 	dt_htab_t *dt_kernpaths; /* hash table of dt_kern_path_t's */
 	dt_module_t *dt_exec;	/* pointer to executable module */
 	dt_module_t *dt_cdefs;	/* pointer to C dynamic type module */
libdtrace/dt_module.c +60 -14

186 lines not on this page · raw message

libdtrace/dt_module.h +1 -0
index a5327d37..c94cf7b5 100644
@@ -22,6 +22,7 @@ extern dt_module_t *dt_module_lookup_by_name(dtrace_hdl_t *, const char *);
 extern dt_module_t *dt_module_lookup_by_ctf(dtrace_hdl_t *, ctf_file_t *);
 extern int dt_module_sym_iter(const dt_module_t *, proc_sym_f *, void *);
 
+extern ctf_file_t *dt_module_shared_ctf(dtrace_hdl_t *);
 extern ctf_file_t *dt_module_getctf(dtrace_hdl_t *, dt_module_t *);
 extern dt_ident_t *dt_module_extern(dtrace_hdl_t *, dt_module_t *,
     const char *, const dtrace_typeinfo_t *);
libdtrace/dt_open.c +1 -2
index 9dd15483..6182a5da 100644
@@ -1316,8 +1316,6 @@ dtrace_close(dtrace_hdl_t *dtp)
 
 	if (dtp->dt_shared_btf != NULL)
 		dt_btf_destroy(dtp, dtp->dt_shared_btf);
-	if (dtp->dt_shared_ctf != NULL)
-		ctf_close(dtp->dt_shared_ctf);
 	if (dtp->dt_ctfa != NULL)
 		ctf_arc_close(dtp->dt_ctfa);
 	free(dtp->dt_ctfa_data);
@@ -1369,6 +1367,7 @@ dtrace_close(dtrace_hdl_t *dtp)
 	free(dtp->dt_drops);
 	free(dtp->dt_module_path);
 	free(dtp->dt_ctfa_path);
+	free(dtp->dt_btf_path);
 	free(dtp);
 
 	dt_debug_dump(0);
libdtrace/dt_prov_proc.c +3 -2
index f7e46a5a..af3105e4 100644
@@ -1,11 +1,12 @@
 /*
  * Oracle Linux DTrace.
- * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2023, 2026, Oracle and/or its affiliates. All rights reserved.
  * Licensed under the Universal Permissive License v 1.0 as shown at
  * http://oss.oracle.com/licenses/upl.
  *
  * The 'proc' SDT provider for DTrace specific probes.
  */
+#include <dt_module.h>
 #include <assert.h>
 #include <errno.h>
 
@@ -131,7 +132,7 @@ static int trampoline(dt_pcb_t *pcb, uint_t exitlbl)
 			emit(dlp, BPF_STORE(BPF_DW, BPF_REG_7, DMST_ARG(0), BPF_REG_0));
 		}
 	} else if (strcmp(prp->desc->prb, "exit") == 0) {
-		ctf_file_t	*cfp = dtp->dt_shared_ctf;
+		ctf_file_t	*cfp = dt_module_shared_ctf(dtp);
 		ctf_id_t	type;
 		ctf_membinfo_t	ctm;
 		int		rc = 0;
libdtrace/dt_provider_tp.c +2 -1
index 564cb66c..93fa8417 100644
@@ -6,6 +6,7 @@
  *
  * Provider support code for tracepoint-based probes.
  */
+#include <dt_module.h>
 #include <errno.h>
 #include <ctype.h>
 #include <stdio.h>
@@ -322,7 +323,7 @@ dt_tp_event_info(dtrace_hdl_t *dtp, FILE *f, dt_valid_arg_f *valid_arg,
 
 			strp = tstr;
 			if (alpha) {
-				ctf_file_t	*ctfp = dtp->dt_shared_ctf;
+				ctf_file_t	*ctfp = dt_module_shared_ctf(dtp);
 				ctf_id_t	type;
 				size_t		esize = 1;
 
test/internals/btf/tst.shared-ctf.sh new file +14 -0
new file mode 100755
index 00000000..ade9cc6a
@@ -0,0 +1,14 @@
+#!/bin/bash
+#
+# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
+# Licensed under the Universal Permissive License v 1.0 as shown at
+# http://oss.oracle.com/licenses/upl.
+#
+# ASSERTION: A shared CTF module owns archive, BTF, and embedded dictionaries,
+#            survives module discovery, and reports missing type data.
+#
+
+dir=$tmpdir/shared-ctf.$$
+mkdir -p "$dir" || exit 1
+
+test/triggers/shared-ctf "$dir"
test/triggers/Build +6 -1
index 20763860..947341c6 100644
@@ -25,7 +25,7 @@ EXTERNAL_TRIGGERS = $(EXTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(EX
 INTERNAL_64BIT_TRIGGERS = libproc-pldd libproc-consistency libproc-sleeper \
     libproc-sleeper-pie libproc-dlmadopen libproc-lookup-by-name \
     libproc-lookup-victim libproc-execing-bkpts libproc-execing-bkpts-victim \
-    module-discovery
+    module-discovery shared-ctf
 INTERNAL_32BIT_TRIGGERS := libproc-sleeper-32 libproc-sleeper-pie-32
 INTERNAL_TRIGGERS = $(INTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(INTERNAL_32BIT_TRIGGERS))
 
@@ -253,3 +253,8 @@ module-discovery_LDFLAGS := -Wl,--wrap=fopen64 \
 module-discovery_DEPS := build-libdtrace.a build-libproc.a libcommon.a libport.a
 module-discovery_LIBS := $(objdir)/build-libdtrace.a $(objdir)/build-libproc.a \
     $(objdir)/build-libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)
+
+# Exercise shared dictionary ownership using libdtrace's private module APIs.
+shared-ctf_CFLAGS := -Ilibproc -Ilibdtrace -Ilibcommon -Wno-pedantic
+shared-ctf_DEPS := build-libdtrace.a build-libproc.a libcommon.a libport.a
+shared-ctf_LIBS := $(objdir)/build-libdtrace.a $(objdir)/build-libproc.a $(objdir)/build-libcommon.a $(objdir)/build-libport.a $(libdtrace_LIBS)
test/triggers/shared-ctf.c new file +265 -0

265 lines not on this page · raw message

Signature · 1 line
2.52.0
#1 lore raw

On 30 Oct 2026, Kris Van Hees stated:

Quoted 2 lines

Store the shared dictionary in shared_ctf->dm_ctfp and remove dt_shared_ctf from the DTrace handle.

This is effectively reversing a change I made ten years ago :P because having it as a separate module was too problematic given that it didn't correspond to a *real* module any more (in particular, we don't want people to be able to look it up by name with `, etc.)

As far as I could tell, all the advantages of being a module were actually disadvantages for the shared dict, and no advantages were left. It's not actually a module, and none of the things you can do to modules should be doable on the shared CTF, and it didn't make the code simpler to conflate the two either.

So... what benefits does this bring?

Signature · 1 line
NULL && (void)
#2 lore raw
Quoted 10 lines · Nix

On 30 Oct 2026, Kris Van Hees stated:

Quoted 2 lines

Store the shared dictionary in shared_ctf->dm_ctfp and remove dt_shared_ctf from the DTrace handle.

This is effectively reversing a change I made ten years ago :P because having it as a separate module was too problematic given that it didn't correspond to a *real* module any more (in particular, we don't want people to be able to look it up by name with `, etc.)

Quoted 7 lines

As far as I could tell, all the advantages of being a module were actually disadvantages for the shared dict, and no advantages were left. It's not actually a module, and none of the things you can do to modules should be doable on the shared CTF, and it didn't make the code simpler to conflate the two either.

So... what benefits does this bring?

It makes it cleaner to be able to e.g. share the module data between multiple handles within the same process (which we will be doing with the PCP support Alan contributed). It also avoids needing to store the shared_ctf data in the handle itself, because it is truly associated with kernel module data.

Kris