Skip to contentSkip to conversation

libdtrace: retain module metadata across incremental discovery

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

lore
Share & tools
lore
#0 lore raw

PATCH 2/20 libdtrace: retain module metadata across incremental discovery

Stop unloading cached modules in dtrace_update(), including vmlinux. Keep discovered symbol tables, ranges, types, and BTF IDs for the handle lifetime while collecting newly loaded modules. Module disappearance or reloading no longer replaces cached metadata.

Separate failed ELF-load cleanup from module destruction, retaining the existing handle ownership of shared CTF and BTF. Document the new lifetime contract and cover incremental discovery with both kernel symbol formats.

---
 INCOMPATIBILITIES                          |  10 ++
 libdtrace/dt_btf.c                         |   2 +-
 libdtrace/dt_impl.h                        |   1 +
 libdtrace/dt_module.c                      |  90 +++++-----
 libdtrace/dt_open.c                        |   1 -
 libdtrace/dtrace.h                         |   8 +-
 test/internals/btf/tst.module-discovery.sh |  11 ++
 test/triggers/Build                        |  11 +-
 test/triggers/module-discovery.c           | 195 +++++++++++++++++++++
 9 files changed, 282 insertions(+), 47 deletions(-)
 create mode 100755 test/internals/btf/tst.module-discovery.sh
 create mode 100644 test/triggers/module-discovery.c
INCOMPATIBILITIES +10 -0
index bf55019e..933c5c86 100644
@@ -183,3 +183,13 @@ proc:::signal-discard
 	expiration should be discarded, no signal-discard probe is fired.
 	The reason for this behaviour is that SDT probes do not work in
 	IRQ context.
+
+
+Kernel module metadata lifetime
+--------------------------------
+dtrace_update() discovers new kernel modules and retains previously discovered
+symbols, address ranges, and types, including those for vmlinux.  Returned
+metadata remains valid across updates until dtrace_close().  Dynamic kernel
+module unloading and reloading are not supported: a module name continues to
+refer to its first discovered instance, even if it disappears or reappears at
+a different address.  Open a new handle to obtain a fresh view of the kernel.
libdtrace/dt_btf.c +1 -1
index 55b58bf5..54011e4d 100644
@@ -1002,7 +1002,7 @@ dt_btf_get_module_ids(dtrace_hdl_t *dtp)
 			return dt_set_errno(dtp, errno);
 
 		dmp = dt_module_lookup_by_name(dtp, name);
-		if (dmp == NULL)
+		if (dmp == NULL || dmp->dm_btf_id != 0)
 			continue;
 
 		dmp->dm_btf_id = id;
libdtrace/dt_impl.h +1 -0
index 243fd2a7..0290e0b4 100644
@@ -202,6 +202,7 @@ typedef struct dt_kern_path {
 #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_KERN_SYMS		0x40	/* kernel symbols discovered and finalized */
 
 /*
  * Why do we need (only) 4 slots?  The maximum amount of string arguments to
libdtrace/dt_module.c +50 -40

195 lines not on this page · raw message

libdtrace/dt_open.c +0 -1
index 1baf3844..9dd15483 100644
@@ -1227,7 +1227,6 @@ dtrace_init(dtrace_hdl_t *dtp)
 	 * Initialize the BPF library handling.
 	 */
 	dt_bpf_init(dtp);
-	dt_btf_get_module_ids(dtp);
 	dt_dlib_init(dtp);
 
 	/*
libdtrace/dtrace.h +4 -4
index 82965fbd..2b4bf583 100644
@@ -1,6 +1,6 @@
 /*
  * Oracle Linux DTrace.
- * Copyright (c) 2007, 2025, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 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.
  */
@@ -480,9 +480,9 @@ extern pid_t dtrace_proc_getpid(dtrace_hdl_t *dtp,
  * information cached for each of the program objects in use by DTrace.  The
  * resulting struct contains pointers to address range arrays and arbitrary-
  * length strings, including object, symbol, and type names, that are persistent
- * until the next call to dtrace_update().  Once dtrace_update() is called, any
- * cached values must be flushed and not used subsequently by the client
- * program.
+ * until dtrace_close().  dtrace_update() discovers additional kernel modules
+ * without invalidating existing metadata.  Kernel module unloading and reloading
+ * are not supported: metadata for the first discovered instance is retained.
  */
 
 #define	DTRACE_OBJ_EXEC	 ((const char *)0L)	/* primary executable file */
test/internals/btf/tst.module-discovery.sh new file +11 -0
new file mode 100755
index 00000000..4edff5df
@@ -0,0 +1,11 @@
+#!/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: Module discovery adds new symbols and retains metadata across
+#            repeated updates, module disappearance, and module reloading.
+#
+
+test/triggers/module-discovery
test/triggers/Build +10 -1
index f8a08d6c..20763860 100644
@@ -24,7 +24,8 @@ 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
+    libproc-lookup-victim libproc-execing-bkpts libproc-execing-bkpts-victim \
+    module-discovery
 INTERNAL_32BIT_TRIGGERS := libproc-sleeper-32 libproc-sleeper-pie-32
 INTERNAL_TRIGGERS = $(INTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(INTERNAL_32BIT_TRIGGERS))
 
@@ -244,3 +245,11 @@ ustack-tst-bigstack-spin_CFLAGS := -O0 -fno-inline -fno-omit-frame-pointer
 ustack-tst-spin_CFLAGS := -O0
 ustack-tst-mtspin_CFLAGS := -O0
 ustack-tst-mtspin_LIBS := -lpthread
+
+module-discovery_CFLAGS := -Ilibproc -Ilibdtrace -Ilibcommon -Wno-pedantic
+module-discovery_LDFLAGS := -Wl,--wrap=fopen64 \
+    -Wl,--wrap=dt_bpf_btf_get_next_id -Wl,--wrap=dt_bpf_btf_get_fd_by_id \
+    -Wl,--wrap=dt_bpf_btf_get_info_by_fd
+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)
test/triggers/module-discovery.c new file +195 -0

195 lines not on this page · raw message

Signature · 1 line
2.52.0
Reviewed-by on v1 2/20
#1 lore raw

On 30 Oct 2026, Kris Van Hees spake thusly:

Quoted 4 lines

Stop unloading cached modules in dtrace_update(), including vmlinux. Keep discovered symbol tables, ranges, types, and BTF IDs for the handle lifetime while collecting newly loaded modules. Module disappearance or reloading no longer replaces cached metadata.

Hm. Interesting. I considered doing this back in the day but more or less immediately hit problems with wanting to remove dtrace.ko and loading a modified one.

I guess we don't care about replacement of modules with bugfixed ones any more? (What happens if someone does it?)

Quoted 3 lines

Separate failed ELF-load cleanup from module destruction, retaining the existing handle ownership of shared CTF and BTF. Document the new lifetime contract and cover incremental discovery with both kernel symbol formats.

(I don't really know what this series is for, or why you're doing this. Is the point of this incremental discovery? Or is it just that the compiler resolves these things to addresses, so having them change just causes trouble because the resolution is already done? If so, this seems reasonable -- one hopes that the verifier would trap cases where the addresses used now resolve to something else.)

Quoted 1 line

(I suspect there are more simplifications one could make, given this, but the below seems like it should work.)

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

On 30 Oct 2026, Kris Van Hees spake thusly:

Quoted 4 lines

Stop unloading cached modules in dtrace_update(), including vmlinux. Keep discovered symbol tables, ranges, types, and BTF IDs for the handle lifetime while collecting newly loaded modules. Module disappearance or reloading no longer replaces cached metadata.

Hm. Interesting. I considered doing this back in the day but more or less immediately hit problems with wanting to remove dtrace.ko and loading a modified one.

Since we no longer have a dtrace.ko, that is not an issue.

Quoted 2 lines

I guess we don't care about replacement of modules with bugfixed ones any more? (What happens if someone does it?)

Well, is a module is unloaded and reloaded with a different content, then an active tracing session already will face trouble because there is e.g. a potential that addresses were put in the trace buffer based on the old version of the module, but symbol resolution takes place in the consumer using the new version, and thus you get wrong results. Of ocurse, that would be (possibly) of short duration - but either way, the system changing underneath us is just not a case that we can really deal with. That was something we could deal with much better when dtrace had an important piece living in the kernel because we could (in a pretty seamless way) re-enable probes based on the enablings that were stored in the kernel. Now we would have to react to a module unloading and reloading from userspace which means there would be delays and all kinds of side effects that would disrupt ongoing tracing anyway.

Quoted 10 lines
Quoted 3 lines

Separate failed ELF-load cleanup from module destruction, retaining the existing handle ownership of shared CTF and BTF. Document the new lifetime contract and cover incremental discovery with both kernel symbol formats.

(I don't really know what this series is for, or why you're doing this. Is the point of this incremental discovery? Or is it just that the compiler resolves these things to addresses, so having them change just causes trouble because the resolution is already done? If so, this seems reasonable -- one hopes that the verifier would trap cases where the addresses used now resolve to something else.)

Combination of all the above :) The main purpose is remove complexity that is no longer really beneficial to the operation of DTrace, and also to makes it easier to simply and streamline the code that handles modules symbol and type data - the extra complexity was lowering performance without really offering much of a benefit.

Quoted 10 lines
Quoted 1 line

(I suspect there are more simplifications one could make, given this, but the below seems like it should work.)

--
NULL && (void)