Skip to contentSkip to conversation

Clarify how the usdt_prids key is stored on the BPF stack

PATCH In review [email protected] 2 participants 4 messages Feb 20, 2025 – Mar 19, 2025 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Clarify how the usdt_prids key is stored on the BPF stack

From: Eugene Loh <[email protected]>

While one can access the BPF stack relative to %r9, the whole point of DT_TRAMP_SP_SLOT(0) is to make trampoline code more readable. So use it.

---
 libdtrace/dt_prov_uprobe.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
libdtrace/dt_prov_uprobe.c +7 -14
index 5d9f74244..f1323cc31 100644
@@ -1015,22 +1015,15 @@ static int trampoline(dt_pcb_t *pcb, uint_t exitlbl)
 	emit(dlp,  BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, 32));
 
 	/*
-	 * Look up in the BPF 'usdt_prids' map.  Space for the look-up key
-	 * will be used on the BPF stack:
-	 *
-	 *     offset                                       value
-	 *
-	 *     -sizeof(usdt_prids_map_key_t)                pid (in %r0)
-	 *
-	 *     -sizeof(usdt_prids_map_key_t) + sizeof(pid_t)
-	 *     ==
-	 *     -sizeof(dtrace_id_t)                         underlying-probe prid
+	 * Look up in the BPF 'usdt_prids' map.  The key should fit into
+	 * trampoline stack slot 0.
 	 */
-	emit(dlp,  BPF_STORE(BPF_W, BPF_REG_9, (int)(-sizeof(usdt_prids_map_key_t)), BPF_REG_0));
-	emit(dlp,  BPF_STORE_IMM(BPF_W, BPF_REG_9, (int)(-sizeof(dtrace_id_t)), uprp->desc->id));
+	assert(sizeof(usdt_prids_map_key_t) <= DT_STK_SLOT_SZ);
+	emit(dlp,  BPF_STORE(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0), BPF_REG_0));
+	emit(dlp,  BPF_STORE_IMM(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0) + sizeof(pid_t), uprp->desc->id));
 	dt_cg_xsetx(dlp, usdt_prids, DT_LBL_NONE, BPF_REG_1, usdt_prids->di_id);
-	emit(dlp,  BPF_MOV_REG(BPF_REG_2, BPF_REG_9));
-	emit(dlp,  BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, (int)(-sizeof(usdt_prids_map_key_t))));
+	emit(dlp,  BPF_MOV_REG(BPF_REG_2, BPF_REG_FP));
+	emit(dlp,  BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, DT_TRAMP_SP_SLOT(0)));
 	emit(dlp,  BPF_CALL_HELPER(BPF_FUNC_map_lookup_elem));
 	emit(dlp,  BPF_BRANCH_IMM(BPF_JEQ, BPF_REG_0, 0, lbl_exit));
 
Signature · 1 line
2.43.5
#1 lore raw

PATCH 2/2 Extend the USDT bit mask to multiple words

From: Eugene Loh <[email protected]>

Currently, USDT is limited to 64 probe descriptions since the underlying probe uses a 64-bit mask to decide which probes to execute.

Change to a multi-word bit mask that can be extended to however many probe descriptions there are.

Also, change the mask words to be 32-bit rather than 64-bit. The reason is that, commonly, there will be fewer than 32 probe descriptions. In this case, we shorten the value of the "USDT prids" BPF map from 16 bytes

        uint32_t        prid;
        long long       mask[1];
down to 8 bytes
        uint32_t        prid;
        uint32_t        mask[1];

(The second member is smaller and no longer costs extra padding.)

We also add an

        extern int usdt_prids_map_val_extra_bytes;

to denote how many extra bytes will be needed for the extended mask. This value is computed by usdt_prids_map_val_extra_bytes_init(). Currently, this function is awkwardly called in gmap_create_usdt(), just before the value is needed. Such a call to a provider-specific function is clumsy, but there are no other calls to the provider between compilation (where the number of statements is determined) and this map creation.

---
 libdtrace/dt_bpf.c                            |   6 +-
 libdtrace/dt_bpf_maps.h                       |   3 +-
 libdtrace/dt_prov_uprobe.c                    |  81 ++++++++---
 .../usdt/tst.many_probe_descriptions.r        |   1 +
 .../usdt/tst.many_probe_descriptions.sh       |  64 +++++++++
 .../usdt/tst.many_probe_descriptions2.r       |   1 +
 .../usdt/tst.many_probe_descriptions2.sh      | 127 ++++++++++++++++++
 7 files changed, 260 insertions(+), 23 deletions(-)
 create mode 100644 test/unittest/usdt/tst.many_probe_descriptions.r
 create mode 100755 test/unittest/usdt/tst.many_probe_descriptions.sh
 create mode 100644 test/unittest/usdt/tst.many_probe_descriptions2.r
 create mode 100755 test/unittest/usdt/tst.many_probe_descriptions2.sh
libdtrace/dt_bpf.c +5 -1
index 662fd81a4..1ed9376ea 100644
@@ -940,6 +940,7 @@ gmap_create_probes(dtrace_hdl_t *dtp)
 	return 0;
 }
 
+void usdt_prids_map_val_extra_bytes_init(dtrace_hdl_t *dtp);
 /*
  * Create the 'usdt_names' and 'usdt_prids' BPF maps.
  *
@@ -965,8 +966,11 @@ gmap_create_usdt(dtrace_hdl_t *dtp)
 	if (dtp->dt_usdt_namesmap_fd == -1)
 		return -1;
 
+	usdt_prids_map_val_extra_bytes_init(dtp);
+
 	dtp->dt_usdt_pridsmap_fd = create_gmap(dtp, "usdt_prids", BPF_MAP_TYPE_HASH,
-	    sizeof(usdt_prids_map_key_t), sizeof(usdt_prids_map_val_t), nusdtprobes);
+	    sizeof(usdt_prids_map_key_t),
+	    sizeof(usdt_prids_map_val_t) + usdt_prids_map_val_extra_bytes, nusdtprobes);
 	if (dtp->dt_usdt_pridsmap_fd == -1)
 		return -1;
 
libdtrace/dt_bpf_maps.h +2 -1
index 884dc3983..ba17d8942 100644
@@ -48,8 +48,9 @@ typedef struct usdt_prids_map_key {
 } usdt_prids_map_key_t;
 typedef struct usdt_prids_map_val {
 	uint32_t	prid;		/* should be dtrace_id_t, sys/dtrace_types.h */
-	long long	mask;
+	uint32_t	mask[1];
 } usdt_prids_map_val_t;
+extern int usdt_prids_map_val_extra_bytes;
 
 #ifdef  __cplusplus
 }
libdtrace/dt_prov_uprobe.c +60 -21

186 lines not on this page · raw message

test/unittest/usdt/tst.many_probe_descriptions.r new file +1 -0
new file mode 100644
index 000000000..2e9ba477f
@@ -0,0 +1 @@
+success
test/unittest/usdt/tst.many_probe_descriptions.sh new file +64 -0

64 lines not on this page · raw message

test/unittest/usdt/tst.many_probe_descriptions2.r new file +1 -0
new file mode 100644
index 000000000..2e9ba477f
@@ -0,0 +1 @@
+success
test/unittest/usdt/tst.many_probe_descriptions2.sh new file +127 -0

127 lines not on this page · raw message

Signature · 1 line
2.43.5

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 9 lines · Kris Van Hees via DTrace-devel
Quoted 6 lines · On Wed, Feb 19, 2025 at 11:43:49PM -0500, [email protected] wrote:

While one can access the BPF stack relative to %r9, the whole point of DT_TRAMP_SP_SLOT(0) is to make trampoline code more readable. So use it.

Still applies but see below...

Quoted 22 lines
Quoted 22 lines
---
 libdtrace/dt_prov_uprobe.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
libdtrace/dt_prov_uprobe.c +0 -10
index 5d9f74244..f1323cc31 100644
@@ -1015,22 +1015,15 @@ static int trampoline(dt_pcb_t *pcb, uint_t exitlbl)
 	emit(dlp,  BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, 32));
 
 	/*
-	 * Look up in the BPF 'usdt_prids' map.  Space for the look-up key
-	 * will be used on the BPF stack:
-	 *
-	 *     offset                                       value
-	 *
-	 *     -sizeof(usdt_prids_map_key_t)                pid (in %r0)
-	 *
-	 *     -sizeof(usdt_prids_map_key_t) + sizeof(pid_t)
-	 *     ==
-	 *     -sizeof(dtrace_id_t)                         underlying-probe prid
Quoted 8 lines

libdtrace/dt_prov_uprobe.c static int trampoline(dt_pcb_t *pcb, uint_t exitlbl) · line 1023

Quoted 8 lines
libdtrace/dt_prov_uprobe.c +5 -2
+	 * Look up in the BPF 'usdt_prids' map.  The key should fit into
+	 * trampoline stack slot 0.
 	 */
-	emit(dlp,  BPF_STORE(BPF_W, BPF_REG_9, (int)(-sizeof(usdt_prids_map_key_t)), BPF_REG_0));
-	emit(dlp,  BPF_STORE_IMM(BPF_W, BPF_REG_9, (int)(-sizeof(dtrace_id_t)), uprp->desc->id));
+	assert(sizeof(usdt_prids_map_key_t) <= DT_STK_SLOT_SZ);
+	emit(dlp,  BPF_STORE(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0), BPF_REG_0));
+	emit(dlp,  BPF_STORE_IMM(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0) + sizeof(pid_t), uprp->desc->id));

I get a compiler warning here:

libdtrace/dt_prov_uprobe.c: In function â?~trampolineâ?T:
include/bpf_asm.h:119:24: warning: overflow in conversion from â?~long unsigned intâ?T to â?~short intâ?T changes value from â?~18446744073709551524â?T to â?~-92â?T [-Woverflo]
119 | .off = (ofs), \
| ^
libdtrace/dt_as.h:42:69: note: in definition of macro â?~emitleâ?T
42 | dt_irnode_t *dip = dt_cg_node_alloc((lbl), (instr)); \
| ^~~~~

libdtrace/dt_prov_uprobe.c:1013:9: note: in expansion of macro â?~emitâ?T

 1013 |         emit(dlp,  BPF_STORE_IMM(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0) + sizeof(pid_t), uprp->desc->id));
      |         ^~~~

libdtrace/dt_prov_uprobe.c:1013:20: note: in expansion of macro â?~BPF_STORE_IMMâ?T
1013 | emit(dlp, BPF_STORE_IMM(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0) + sizeof(pid_t), uprp->desc->id));

You need a (int) cast for sizeof(pid_t) similar to the casts that were in the code before. I'll add it in as I merge.

Quoted 17 lines
Quoted 12 lines
libdtrace/dt_prov_uprobe.c +2 -2
 	dt_cg_xsetx(dlp, usdt_prids, DT_LBL_NONE, BPF_REG_1, usdt_prids->di_id);
-	emit(dlp,  BPF_MOV_REG(BPF_REG_2, BPF_REG_9));
-	emit(dlp,  BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, (int)(-sizeof(usdt_prids_map_key_t))));
+	emit(dlp,  BPF_MOV_REG(BPF_REG_2, BPF_REG_FP));
+	emit(dlp,  BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, DT_TRAMP_SP_SLOT(0)));
 	emit(dlp,  BPF_CALL_HELPER(BPF_FUNC_map_lookup_elem));
 	emit(dlp,  BPF_BRANCH_IMM(BPF_JEQ, BPF_REG_0, 0, lbl_exit));
 
Signature · 1 line
2.43.5

_______________________________________________
DTrace-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/dtrace-devel

author
#4 lore raw
Quoted 50 lines · Kris Van Hees
Quoted 7 lines · Kris Van Hees via DTrace-devel
Quoted 5 lines · On Wed, Feb 19, 2025 at 11:43:49PM -0500, [email protected] wrote:

While one can access the BPF stack relative to %r9, the whole point of DT_TRAMP_SP_SLOT(0) is to make trampoline code more readable. So use it.

Still applies but see below...

Quoted 22 lines
Quoted 22 lines
---
  libdtrace/dt_prov_uprobe.c | 21 +++++++--------------
  1 file changed, 7 insertions(+), 14 deletions(-)
libdtrace/dt_prov_uprobe.c +0 -10
index 5d9f74244..f1323cc31 100644
@@ -1015,22 +1015,15 @@ static int trampoline(dt_pcb_t *pcb, uint_t exitlbl)
  	emit(dlp,  BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, 32));
 
  	/*
-	 * Look up in the BPF 'usdt_prids' map.  Space for the look-up key
-	 * will be used on the BPF stack:
-	 *
-	 *     offset                                       value
-	 *
-	 *     -sizeof(usdt_prids_map_key_t)                pid (in %r0)
-	 *
-	 *     -sizeof(usdt_prids_map_key_t) + sizeof(pid_t)
-	 *     ==
-	 *     -sizeof(dtrace_id_t)                         underlying-probe prid
Quoted 8 lines

libdtrace/dt_prov_uprobe.c static int trampoline(dt_pcb_t *pcb, uint_t exitlbl) · line 1023

Quoted 8 lines
libdtrace/dt_prov_uprobe.c +5 -2
+	 * Look up in the BPF 'usdt_prids' map.  The key should fit into
+	 * trampoline stack slot 0.
  	 */
-	emit(dlp,  BPF_STORE(BPF_W, BPF_REG_9, (int)(-sizeof(usdt_prids_map_key_t)), BPF_REG_0));
-	emit(dlp,  BPF_STORE_IMM(BPF_W, BPF_REG_9, (int)(-sizeof(dtrace_id_t)), uprp->desc->id));
+	assert(sizeof(usdt_prids_map_key_t) <= DT_STK_SLOT_SZ);
+	emit(dlp,  BPF_STORE(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0), BPF_REG_0));
+	emit(dlp,  BPF_STORE_IMM(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0) + sizeof(pid_t), uprp->desc->id));

I get a compiler warning here:

libdtrace/dt_prov_uprobe.c: In function â?~trampolineâ?T:
include/bpf_asm.h:119:24: warning: overflow in conversion from â?~long unsigned intâ?T to â?~short intâ?T changes value from â?~18446744073709551524â?T to â?~-92â?T [-Woverflo]
119 | .off = (ofs), \
| ^
libdtrace/dt_as.h:42:69: note: in definition of macro â?~emitleâ?T

    42 |                  dt_irnode_t *dip = dt_cg_node_alloc((lbl), (instr)); \
       |                                                              ^~~~~
Quoted 8 lines

libdtrace/dt_prov_uprobe.c:1013:9: note: in expansion of macro â?~emitâ?T

  1013 |         emit(dlp,  BPF_STORE_IMM(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0) + sizeof(pid_t), uprp->desc->id));
       |         ^~~~

libdtrace/dt_prov_uprobe.c:1013:20: note: in expansion of macro â?~BPF_STORE_IMMâ?T
1013 | emit(dlp, BPF_STORE_IMM(BPF_W, BPF_REG_FP, DT_TRAMP_SP_SLOT(0) + sizeof(pid_t), uprp->desc->id));

You need a (int) cast for sizeof(pid_t) similar to the casts that were in the code before. I'll add it in as I merge.

Thanks.  Might this correction already be in the 2/2 patch?  (Not that that's the right place for it, but...)

Quoted 16 lines
Quoted 16 lines
Quoted 12 lines
libdtrace/dt_prov_uprobe.c +2 -2
  	dt_cg_xsetx(dlp, usdt_prids, DT_LBL_NONE, BPF_REG_1, usdt_prids->di_id);
-	emit(dlp,  BPF_MOV_REG(BPF_REG_2, BPF_REG_9));
-	emit(dlp,  BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, (int)(-sizeof(usdt_prids_map_key_t))));
+	emit(dlp,  BPF_MOV_REG(BPF_REG_2, BPF_REG_FP));
+	emit(dlp,  BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, DT_TRAMP_SP_SLOT(0)));
  	emit(dlp,  BPF_CALL_HELPER(BPF_FUNC_map_lookup_elem));
  	emit(dlp,  BPF_BRANCH_IMM(BPF_JEQ, BPF_REG_0, 0, lbl_exit));
 
Signature · 1 line
2.43.5

_______________________________________________
DTrace-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/dtrace-devel