Skip to content

Commit 6e44431

Browse files
committed
tracing/ftrace: Add a better way to pass data via the probe functions
With the redesign of the registration and execution of the function probes (triggers), data can now be passed from the setup of the probe to the probe callers that are specific to the trace_array it is on. Although, all probes still only affect the toplevel trace array, this change will allow for instances to have their own probes separated from other instances and the top array. That is, something like the stacktrace probe can be set to trace only in an instance and not the toplevel trace array. This isn't implement yet, but this change sets the ground work for the change. When a probe callback is triggered (someone writes the probe format into set_ftrace_filter), it calls register_ftrace_function_probe() passing in init_data that will be used to initialize the probe. Then for every matching function, register_ftrace_function_probe() will call the probe_ops->init() function with the init data that was passed to it, as well as an address to a place holder that is associated with the probe and the instance. The first occurrence will have a NULL in the pointer. The init() function will then initialize it. If other probes are added, or more functions are part of the probe, the place holder will be passed to the init() function with the place holder data that it was initialized to the last time. Then this place_holder is passed to each of the other probe_ops functions, where it can be used in the function callback. When the probe_ops free() function is called, it can be called either with the rip of the function that is being removed from the probe, or zero, indicating that there are no more functions attached to the probe, and the place holder is about to be freed. This gives the probe_ops a way to free the data it assigned to the place holder if it was allocade during the first init call. Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 7b60f3d commit 6e44431

5 files changed

Lines changed: 153 additions & 107 deletions

File tree

‎kernel/trace/ftrace.c‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ struct ftrace_func_probe {
11061106
struct ftrace_ops ops;
11071107
struct trace_array *tr;
11081108
struct list_head list;
1109+
void *data;
11091110
int ref;
11101111
};
11111112

@@ -3187,7 +3188,7 @@ t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
31873188
probe_ops = probe->probe_ops;
31883189

31893190
if (probe_ops->print)
3190-
return probe_ops->print(m, probe_entry->ip, probe_ops, NULL);
3191+
return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
31913192

31923193
seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
31933194
(void *)probe_ops->func);
@@ -3814,7 +3815,7 @@ static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
38143815
* on the hash. rcu_read_lock is too dangerous here.
38153816
*/
38163817
preempt_disable_notrace();
3817-
probe_ops->func(ip, parent_ip, probe->tr, probe_ops, NULL);
3818+
probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
38183819
preempt_enable_notrace();
38193820
}
38203821

@@ -3972,6 +3973,12 @@ static void release_probe(struct ftrace_func_probe *probe)
39723973

39733974
if (!probe->ref) {
39743975
probe_ops = probe->probe_ops;
3976+
/*
3977+
* Sending zero as ip tells probe_ops to free
3978+
* the probe->data itself
3979+
*/
3980+
if (probe_ops->free)
3981+
probe_ops->free(probe_ops, probe->tr, 0, probe->data);
39753982
list_del(&probe->list);
39763983
kfree(probe);
39773984
}
@@ -4060,9 +4067,15 @@ register_ftrace_function_probe(char *glob, struct trace_array *tr,
40604067
*/
40614068
if (probe_ops->init) {
40624069
ret = probe_ops->init(probe_ops, tr,
4063-
entry->ip, data);
4064-
if (ret < 0)
4070+
entry->ip, data,
4071+
&probe->data);
4072+
if (ret < 0) {
4073+
if (probe_ops->free && count)
4074+
probe_ops->free(probe_ops, tr,
4075+
0, probe->data);
4076+
probe->data = NULL;
40654077
goto out;
4078+
}
40664079
}
40674080
count++;
40684081
}
@@ -4109,7 +4122,7 @@ register_ftrace_function_probe(char *glob, struct trace_array *tr,
41094122
hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
41104123
if (ftrace_lookup_ip(old_hash, entry->ip))
41114124
continue;
4112-
probe_ops->free(probe_ops, tr, entry->ip, NULL);
4125+
probe_ops->free(probe_ops, tr, entry->ip, probe->data);
41134126
}
41144127
}
41154128
goto out_unlock;
@@ -4227,7 +4240,7 @@ unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
42274240
hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
42284241
hlist_del(&entry->hlist);
42294242
if (probe_ops->free)
4230-
probe_ops->free(probe_ops, tr, entry->ip, NULL);
4243+
probe_ops->free(probe_ops, tr, entry->ip, probe->data);
42314244
kfree(entry);
42324245
}
42334246
mutex_unlock(&ftrace_lock);

‎kernel/trace/trace.c‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6737,17 +6737,17 @@ static const struct file_operations tracing_dyn_info_fops = {
67376737
static void
67386738
ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
67396739
struct trace_array *tr, struct ftrace_probe_ops *ops,
6740-
void **data)
6740+
void *data)
67416741
{
67426742
tracing_snapshot();
67436743
}
67446744

67456745
static void
67466746
ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
67476747
struct trace_array *tr, struct ftrace_probe_ops *ops,
6748-
void **data)
6748+
void *data)
67496749
{
6750-
struct ftrace_func_mapper *mapper = ops->private_data;
6750+
struct ftrace_func_mapper *mapper = data;
67516751
long *count = NULL;
67526752

67536753
if (mapper)
@@ -6768,7 +6768,7 @@ static int
67686768
ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
67696769
struct ftrace_probe_ops *ops, void *data)
67706770
{
6771-
struct ftrace_func_mapper *mapper = ops->private_data;
6771+
struct ftrace_func_mapper *mapper = data;
67726772
long *count = NULL;
67736773

67746774
seq_printf(m, "%ps:", (void *)ip);
@@ -6788,18 +6788,32 @@ ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
67886788

67896789
static int
67906790
ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
6791-
unsigned long ip, void *data)
6791+
unsigned long ip, void *init_data, void **data)
67926792
{
6793-
struct ftrace_func_mapper *mapper = ops->private_data;
6793+
struct ftrace_func_mapper *mapper = *data;
6794+
6795+
if (!mapper) {
6796+
mapper = allocate_ftrace_func_mapper();
6797+
if (!mapper)
6798+
return -ENOMEM;
6799+
*data = mapper;
6800+
}
67946801

6795-
return ftrace_func_mapper_add_ip(mapper, ip, data);
6802+
return ftrace_func_mapper_add_ip(mapper, ip, init_data);
67966803
}
67976804

67986805
static void
67996806
ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
6800-
unsigned long ip, void **data)
6807+
unsigned long ip, void *data)
68016808
{
6802-
struct ftrace_func_mapper *mapper = ops->private_data;
6809+
struct ftrace_func_mapper *mapper = data;
6810+
6811+
if (!ip) {
6812+
if (!mapper)
6813+
return;
6814+
free_ftrace_func_mapper(mapper, NULL);
6815+
return;
6816+
}
68036817

68046818
ftrace_func_mapper_remove_ip(mapper, ip);
68056819
}
@@ -6842,12 +6856,6 @@ ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
68426856
if (!strlen(number))
68436857
goto out_reg;
68446858

6845-
if (!ops->private_data) {
6846-
ops->private_data = allocate_ftrace_func_mapper();
6847-
if (!ops->private_data)
6848-
return -ENOMEM;
6849-
}
6850-
68516859
/*
68526860
* We use the callback data field (which is a pointer)
68536861
* as our counter.

‎kernel/trace/trace.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -943,18 +943,18 @@ struct ftrace_probe_ops {
943943
unsigned long parent_ip,
944944
struct trace_array *tr,
945945
struct ftrace_probe_ops *ops,
946-
void **data);
946+
void *data);
947947
int (*init)(struct ftrace_probe_ops *ops,
948948
struct trace_array *tr,
949-
unsigned long ip, void *data);
949+
unsigned long ip, void *init_data,
950+
void **data);
950951
void (*free)(struct ftrace_probe_ops *ops,
951952
struct trace_array *tr,
952-
unsigned long ip, void **data);
953+
unsigned long ip, void *data);
953954
int (*print)(struct seq_file *m,
954955
unsigned long ip,
955956
struct ftrace_probe_ops *ops,
956957
void *data);
957-
void *private_data;
958958
};
959959

960960
struct ftrace_func_mapper;

‎kernel/trace/trace_events.c‎

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,117 +2471,139 @@ static void update_event_probe(struct event_probe_data *data)
24712471
static void
24722472
event_enable_probe(unsigned long ip, unsigned long parent_ip,
24732473
struct trace_array *tr, struct ftrace_probe_ops *ops,
2474-
void **_data)
2474+
void *data)
24752475
{
2476-
struct ftrace_func_mapper *mapper = ops->private_data;
2477-
struct event_probe_data *data;
2476+
struct ftrace_func_mapper *mapper = data;
2477+
struct event_probe_data *edata;
24782478
void **pdata;
24792479

24802480
pdata = ftrace_func_mapper_find_ip(mapper, ip);
24812481
if (!pdata || !*pdata)
24822482
return;
24832483

2484-
data = *pdata;
2485-
update_event_probe(data);
2484+
edata = *pdata;
2485+
update_event_probe(edata);
24862486
}
24872487

24882488
static void
24892489
event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
24902490
struct trace_array *tr, struct ftrace_probe_ops *ops,
2491-
void **_data)
2491+
void *data)
24922492
{
2493-
struct ftrace_func_mapper *mapper = ops->private_data;
2494-
struct event_probe_data *data;
2493+
struct ftrace_func_mapper *mapper = data;
2494+
struct event_probe_data *edata;
24952495
void **pdata;
24962496

24972497
pdata = ftrace_func_mapper_find_ip(mapper, ip);
24982498
if (!pdata || !*pdata)
24992499
return;
25002500

2501-
data = *pdata;
2501+
edata = *pdata;
25022502

2503-
if (!data->count)
2503+
if (!edata->count)
25042504
return;
25052505

25062506
/* Skip if the event is in a state we want to switch to */
2507-
if (data->enable == !(data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2507+
if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
25082508
return;
25092509

2510-
if (data->count != -1)
2511-
(data->count)--;
2510+
if (edata->count != -1)
2511+
(edata->count)--;
25122512

2513-
update_event_probe(data);
2513+
update_event_probe(edata);
25142514
}
25152515

25162516
static int
25172517
event_enable_print(struct seq_file *m, unsigned long ip,
2518-
struct ftrace_probe_ops *ops, void *_data)
2518+
struct ftrace_probe_ops *ops, void *data)
25192519
{
2520-
struct ftrace_func_mapper *mapper = ops->private_data;
2521-
struct event_probe_data *data;
2520+
struct ftrace_func_mapper *mapper = data;
2521+
struct event_probe_data *edata;
25222522
void **pdata;
25232523

25242524
pdata = ftrace_func_mapper_find_ip(mapper, ip);
25252525

25262526
if (WARN_ON_ONCE(!pdata || !*pdata))
25272527
return 0;
25282528

2529-
data = *pdata;
2529+
edata = *pdata;
25302530

25312531
seq_printf(m, "%ps:", (void *)ip);
25322532

25332533
seq_printf(m, "%s:%s:%s",
2534-
data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2535-
data->file->event_call->class->system,
2536-
trace_event_name(data->file->event_call));
2534+
edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2535+
edata->file->event_call->class->system,
2536+
trace_event_name(edata->file->event_call));
25372537

2538-
if (data->count == -1)
2538+
if (edata->count == -1)
25392539
seq_puts(m, ":unlimited\n");
25402540
else
2541-
seq_printf(m, ":count=%ld\n", data->count);
2541+
seq_printf(m, ":count=%ld\n", edata->count);
25422542

25432543
return 0;
25442544
}
25452545

25462546
static int
25472547
event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2548-
unsigned long ip, void *_data)
2548+
unsigned long ip, void *init_data, void **data)
25492549
{
2550-
struct ftrace_func_mapper *mapper = ops->private_data;
2551-
struct event_probe_data *data = _data;
2550+
struct ftrace_func_mapper *mapper = *data;
2551+
struct event_probe_data *edata = init_data;
25522552
int ret;
25532553

2554-
ret = ftrace_func_mapper_add_ip(mapper, ip, data);
2554+
if (!mapper) {
2555+
mapper = allocate_ftrace_func_mapper();
2556+
if (!mapper)
2557+
return -ENODEV;
2558+
*data = mapper;
2559+
}
2560+
2561+
ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
25552562
if (ret < 0)
25562563
return ret;
25572564

2558-
data->ref++;
2565+
edata->ref++;
2566+
2567+
return 0;
2568+
}
2569+
2570+
static int free_probe_data(void *data)
2571+
{
2572+
struct event_probe_data *edata = data;
25592573

2574+
edata->ref--;
2575+
if (!edata->ref) {
2576+
/* Remove the SOFT_MODE flag */
2577+
__ftrace_event_enable_disable(edata->file, 0, 1);
2578+
module_put(edata->file->event_call->mod);
2579+
kfree(edata);
2580+
}
25602581
return 0;
25612582
}
25622583

25632584
static void
25642585
event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2565-
unsigned long ip, void **_data)
2586+
unsigned long ip, void *data)
25662587
{
2567-
struct ftrace_func_mapper *mapper = ops->private_data;
2568-
struct event_probe_data *data;
2588+
struct ftrace_func_mapper *mapper = data;
2589+
struct event_probe_data *edata;
2590+
2591+
if (!ip) {
2592+
if (!mapper)
2593+
return;
2594+
free_ftrace_func_mapper(mapper, free_probe_data);
2595+
return;
2596+
}
25692597

2570-
data = ftrace_func_mapper_remove_ip(mapper, ip);
2598+
edata = ftrace_func_mapper_remove_ip(mapper, ip);
25712599

2572-
if (WARN_ON_ONCE(!data))
2600+
if (WARN_ON_ONCE(!edata))
25732601
return;
25742602

2575-
if (WARN_ON_ONCE(data->ref <= 0))
2603+
if (WARN_ON_ONCE(edata->ref <= 0))
25762604
return;
25772605

2578-
data->ref--;
2579-
if (!data->ref) {
2580-
/* Remove the SOFT_MODE flag */
2581-
__ftrace_event_enable_disable(data->file, 0, 1);
2582-
module_put(data->file->event_call->mod);
2583-
kfree(data);
2584-
}
2606+
free_probe_data(edata);
25852607
}
25862608

25872609
static struct ftrace_probe_ops event_enable_probe_ops = {
@@ -2659,12 +2681,6 @@ event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
26592681

26602682
ret = -ENOMEM;
26612683

2662-
if (!ops->private_data) {
2663-
ops->private_data = allocate_ftrace_func_mapper();
2664-
if (!ops->private_data)
2665-
goto out;
2666-
}
2667-
26682684
data = kzalloc(sizeof(*data), GFP_KERNEL);
26692685
if (!data)
26702686
goto out;

0 commit comments

Comments
 (0)