@@ -304,16 +304,6 @@ static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
304
304
305
305
#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
306
306
307
- static struct btf * traceprobe_get_btf (void )
308
- {
309
- struct btf * btf = bpf_get_btf_vmlinux ();
310
-
311
- if (IS_ERR_OR_NULL (btf ))
312
- return NULL ;
313
-
314
- return btf ;
315
- }
316
-
317
307
static u32 btf_type_int (const struct btf_type * t )
318
308
{
319
309
return * (u32 * )(t + 1 );
@@ -371,42 +361,49 @@ static const char *type_from_btf_id(struct btf *btf, s32 id)
371
361
return NULL ;
372
362
}
373
363
374
- static const struct btf_type * find_btf_func_proto (const char * funcname )
364
+ static const struct btf_type * find_btf_func_proto (const char * funcname ,
365
+ struct btf * * btf_p )
375
366
{
376
- struct btf * btf = traceprobe_get_btf ();
377
367
const struct btf_type * t ;
368
+ struct btf * btf = NULL ;
378
369
s32 id ;
379
370
380
- if (!btf || ! funcname )
371
+ if (!funcname )
381
372
return ERR_PTR (- EINVAL );
382
373
383
- id = btf_find_by_name_kind ( btf , funcname , BTF_KIND_FUNC );
374
+ id = bpf_find_btf_id ( funcname , BTF_KIND_FUNC , & btf );
384
375
if (id <= 0 )
385
376
return ERR_PTR (- ENOENT );
386
377
387
378
/* Get BTF_KIND_FUNC type */
388
379
t = btf_type_by_id (btf , id );
389
380
if (!t || !btf_type_is_func (t ))
390
- return ERR_PTR ( - ENOENT ) ;
381
+ goto err ;
391
382
392
383
/* The type of BTF_KIND_FUNC is BTF_KIND_FUNC_PROTO */
393
384
t = btf_type_by_id (btf , t -> type );
394
385
if (!t || !btf_type_is_func_proto (t ))
395
- return ERR_PTR ( - ENOENT ) ;
386
+ goto err ;
396
387
388
+ * btf_p = btf ;
397
389
return t ;
390
+
391
+ err :
392
+ btf_put (btf );
393
+ return ERR_PTR (- ENOENT );
398
394
}
399
395
400
396
static const struct btf_param * find_btf_func_param (const char * funcname , s32 * nr ,
401
- bool tracepoint )
397
+ struct btf * * btf_p , bool tracepoint )
402
398
{
403
399
const struct btf_param * param ;
404
400
const struct btf_type * t ;
401
+ struct btf * btf ;
405
402
406
403
if (!funcname || !nr )
407
404
return ERR_PTR (- EINVAL );
408
405
409
- t = find_btf_func_proto (funcname );
406
+ t = find_btf_func_proto (funcname , & btf );
410
407
if (IS_ERR (t ))
411
408
return (const struct btf_param * )t ;
412
409
@@ -419,29 +416,37 @@ static const struct btf_param *find_btf_func_param(const char *funcname, s32 *nr
419
416
param ++ ;
420
417
}
421
418
422
- if (* nr > 0 )
419
+ if (* nr > 0 ) {
420
+ * btf_p = btf ;
423
421
return param ;
424
- else
425
- return NULL ;
422
+ }
423
+
424
+ btf_put (btf );
425
+ return NULL ;
426
+ }
427
+
428
+ static void clear_btf_context (struct traceprobe_parse_context * ctx )
429
+ {
430
+ if (ctx -> btf ) {
431
+ btf_put (ctx -> btf );
432
+ ctx -> btf = NULL ;
433
+ ctx -> params = NULL ;
434
+ ctx -> nr_params = 0 ;
435
+ }
426
436
}
427
437
428
438
static int parse_btf_arg (const char * varname , struct fetch_insn * code ,
429
439
struct traceprobe_parse_context * ctx )
430
440
{
431
- struct btf * btf = traceprobe_get_btf ();
432
441
const struct btf_param * params ;
433
442
int i ;
434
443
435
- if (!btf ) {
436
- trace_probe_log_err (ctx -> offset , NOSUP_BTFARG );
437
- return - EOPNOTSUPP ;
438
- }
439
-
440
444
if (WARN_ON_ONCE (!ctx -> funcname ))
441
445
return - EINVAL ;
442
446
443
447
if (!ctx -> params ) {
444
- params = find_btf_func_param (ctx -> funcname , & ctx -> nr_params ,
448
+ params = find_btf_func_param (ctx -> funcname ,
449
+ & ctx -> nr_params , & ctx -> btf ,
445
450
ctx -> flags & TPARG_FL_TPOINT );
446
451
if (IS_ERR_OR_NULL (params )) {
447
452
trace_probe_log_err (ctx -> offset , NO_BTF_ENTRY );
@@ -452,7 +457,7 @@ static int parse_btf_arg(const char *varname, struct fetch_insn *code,
452
457
params = ctx -> params ;
453
458
454
459
for (i = 0 ; i < ctx -> nr_params ; i ++ ) {
455
- const char * name = btf_name_by_offset (btf , params [i ].name_off );
460
+ const char * name = btf_name_by_offset (ctx -> btf , params [i ].name_off );
456
461
457
462
if (name && !strcmp (name , varname )) {
458
463
code -> op = FETCH_OP_ARG ;
@@ -470,7 +475,7 @@ static int parse_btf_arg(const char *varname, struct fetch_insn *code,
470
475
static const struct fetch_type * parse_btf_arg_type (int arg_idx ,
471
476
struct traceprobe_parse_context * ctx )
472
477
{
473
- struct btf * btf = traceprobe_get_btf () ;
478
+ struct btf * btf = ctx -> btf ;
474
479
const char * typestr = NULL ;
475
480
476
481
if (btf && ctx -> params ) {
@@ -485,14 +490,17 @@ static const struct fetch_type *parse_btf_arg_type(int arg_idx,
485
490
static const struct fetch_type * parse_btf_retval_type (
486
491
struct traceprobe_parse_context * ctx )
487
492
{
488
- struct btf * btf = traceprobe_get_btf ();
489
493
const char * typestr = NULL ;
490
494
const struct btf_type * t ;
495
+ struct btf * btf ;
491
496
492
- if (btf && ctx -> funcname ) {
493
- t = find_btf_func_proto (ctx -> funcname );
494
- if (!IS_ERR (t ))
497
+ if (ctx -> funcname ) {
498
+ /* Do not use ctx->btf, because it must be used with ctx->param */
499
+ t = find_btf_func_proto (ctx -> funcname , & btf );
500
+ if (!IS_ERR (t )) {
495
501
typestr = type_from_btf_id (btf , t -> type );
502
+ btf_put (btf );
503
+ }
496
504
}
497
505
498
506
return find_fetch_type (typestr , ctx -> flags );
@@ -501,21 +509,25 @@ static const struct fetch_type *parse_btf_retval_type(
501
509
static bool is_btf_retval_void (const char * funcname )
502
510
{
503
511
const struct btf_type * t ;
512
+ struct btf * btf ;
513
+ bool ret ;
504
514
505
- t = find_btf_func_proto (funcname );
515
+ t = find_btf_func_proto (funcname , & btf );
506
516
if (IS_ERR (t ))
507
517
return false;
508
518
509
- return t -> type == 0 ;
519
+ ret = (t -> type == 0 );
520
+ btf_put (btf );
521
+ return ret ;
510
522
}
511
523
#else
512
- static struct btf * traceprobe_get_btf ( void )
524
+ static void clear_btf_context ( struct traceprobe_parse_context * ctx )
513
525
{
514
- return NULL ;
526
+ ctx -> btf = NULL ;
515
527
}
516
528
517
529
static const struct btf_param * find_btf_func_param (const char * funcname , s32 * nr ,
518
- bool tracepoint )
530
+ struct btf * * btf_p , bool tracepoint )
519
531
{
520
532
return ERR_PTR (- EOPNOTSUPP );
521
533
}
@@ -1231,15 +1243,14 @@ static int sprint_nth_btf_arg(int idx, const char *type,
1231
1243
char * buf , int bufsize ,
1232
1244
struct traceprobe_parse_context * ctx )
1233
1245
{
1234
- struct btf * btf = traceprobe_get_btf ();
1235
1246
const char * name ;
1236
1247
int ret ;
1237
1248
1238
1249
if (idx >= ctx -> nr_params ) {
1239
1250
trace_probe_log_err (0 , NO_BTFARG );
1240
1251
return - ENOENT ;
1241
1252
}
1242
- name = btf_name_by_offset (btf , ctx -> params [idx ].name_off );
1253
+ name = btf_name_by_offset (ctx -> btf , ctx -> params [idx ].name_off );
1243
1254
if (!name ) {
1244
1255
trace_probe_log_err (0 , NO_BTF_ENTRY );
1245
1256
return - ENOENT ;
@@ -1271,7 +1282,7 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[],
1271
1282
return NULL ;
1272
1283
}
1273
1284
1274
- params = find_btf_func_param (ctx -> funcname , & nr_params ,
1285
+ params = find_btf_func_param (ctx -> funcname , & nr_params , & ctx -> btf ,
1275
1286
ctx -> flags & TPARG_FL_TPOINT );
1276
1287
if (IS_ERR_OR_NULL (params )) {
1277
1288
if (args_idx != -1 ) {
@@ -1337,6 +1348,11 @@ const char **traceprobe_expand_meta_args(int argc, const char *argv[],
1337
1348
return ERR_PTR (ret );
1338
1349
}
1339
1350
1351
+ void traceprobe_finish_parse (struct traceprobe_parse_context * ctx )
1352
+ {
1353
+ clear_btf_context (ctx );
1354
+ }
1355
+
1340
1356
int traceprobe_update_arg (struct probe_arg * arg )
1341
1357
{
1342
1358
struct fetch_insn * code = arg -> code ;
0 commit comments