Skip to content

Commit 41794f1

Browse files
committed
ftrace: Added ftrace_func_mapper for function probe triggers
In order to move the ops to the function probes directly, they need a way to map function ips to their own data without depending on the infrastructure of the function probes, as the data field will be going away. New helper functions are added that are based on the ftrace_hash code. ftrace_func_mapper functions are there to let the probes map ips to their data. These can be allocated by the probe ops, and referenced in the function callbacks. Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent bca6c8d commit 41794f1

3 files changed

Lines changed: 210 additions & 15 deletions

File tree

‎kernel/trace/ftrace.c‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3808,6 +3808,147 @@ static void ftrace_free_entry(struct ftrace_func_probe *entry)
38083808
kfree(entry);
38093809
}
38103810

3811+
struct ftrace_func_map {
3812+
struct ftrace_func_entry entry;
3813+
void *data;
3814+
};
3815+
3816+
struct ftrace_func_mapper {
3817+
struct ftrace_hash hash;
3818+
};
3819+
3820+
/**
3821+
* allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
3822+
*
3823+
* Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
3824+
*/
3825+
struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
3826+
{
3827+
struct ftrace_hash *hash;
3828+
3829+
/*
3830+
* The mapper is simply a ftrace_hash, but since the entries
3831+
* in the hash are not ftrace_func_entry type, we define it
3832+
* as a separate structure.
3833+
*/
3834+
hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3835+
return (struct ftrace_func_mapper *)hash;
3836+
}
3837+
3838+
/**
3839+
* ftrace_func_mapper_find_ip - Find some data mapped to an ip
3840+
* @mapper: The mapper that has the ip maps
3841+
* @ip: the instruction pointer to find the data for
3842+
*
3843+
* Returns the data mapped to @ip if found otherwise NULL. The return
3844+
* is actually the address of the mapper data pointer. The address is
3845+
* returned for use cases where the data is no bigger than a long, and
3846+
* the user can use the data pointer as its data instead of having to
3847+
* allocate more memory for the reference.
3848+
*/
3849+
void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
3850+
unsigned long ip)
3851+
{
3852+
struct ftrace_func_entry *entry;
3853+
struct ftrace_func_map *map;
3854+
3855+
entry = ftrace_lookup_ip(&mapper->hash, ip);
3856+
if (!entry)
3857+
return NULL;
3858+
3859+
map = (struct ftrace_func_map *)entry;
3860+
return &map->data;
3861+
}
3862+
3863+
/**
3864+
* ftrace_func_mapper_add_ip - Map some data to an ip
3865+
* @mapper: The mapper that has the ip maps
3866+
* @ip: The instruction pointer address to map @data to
3867+
* @data: The data to map to @ip
3868+
*
3869+
* Returns 0 on succes otherwise an error.
3870+
*/
3871+
int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
3872+
unsigned long ip, void *data)
3873+
{
3874+
struct ftrace_func_entry *entry;
3875+
struct ftrace_func_map *map;
3876+
3877+
entry = ftrace_lookup_ip(&mapper->hash, ip);
3878+
if (entry)
3879+
return -EBUSY;
3880+
3881+
map = kmalloc(sizeof(*map), GFP_KERNEL);
3882+
if (!map)
3883+
return -ENOMEM;
3884+
3885+
map->entry.ip = ip;
3886+
map->data = data;
3887+
3888+
__add_hash_entry(&mapper->hash, &map->entry);
3889+
3890+
return 0;
3891+
}
3892+
3893+
/**
3894+
* ftrace_func_mapper_remove_ip - Remove an ip from the mapping
3895+
* @mapper: The mapper that has the ip maps
3896+
* @ip: The instruction pointer address to remove the data from
3897+
*
3898+
* Returns the data if it is found, otherwise NULL.
3899+
* Note, if the data pointer is used as the data itself, (see
3900+
* ftrace_func_mapper_find_ip(), then the return value may be meaningless,
3901+
* if the data pointer was set to zero.
3902+
*/
3903+
void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
3904+
unsigned long ip)
3905+
{
3906+
struct ftrace_func_entry *entry;
3907+
struct ftrace_func_map *map;
3908+
void *data;
3909+
3910+
entry = ftrace_lookup_ip(&mapper->hash, ip);
3911+
if (!entry)
3912+
return NULL;
3913+
3914+
map = (struct ftrace_func_map *)entry;
3915+
data = map->data;
3916+
3917+
remove_hash_entry(&mapper->hash, entry);
3918+
kfree(entry);
3919+
3920+
return data;
3921+
}
3922+
3923+
/**
3924+
* free_ftrace_func_mapper - free a mapping of ips and data
3925+
* @mapper: The mapper that has the ip maps
3926+
* @free_func: A function to be called on each data item.
3927+
*
3928+
* This is used to free the function mapper. The @free_func is optional
3929+
* and can be used if the data needs to be freed as well.
3930+
*/
3931+
void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
3932+
ftrace_mapper_func free_func)
3933+
{
3934+
struct ftrace_func_entry *entry;
3935+
struct ftrace_func_map *map;
3936+
struct hlist_head *hhd;
3937+
int size = 1 << mapper->hash.size_bits;
3938+
int i;
3939+
3940+
if (free_func && mapper->hash.count) {
3941+
for (i = 0; i < size; i++) {
3942+
hhd = &mapper->hash.buckets[i];
3943+
hlist_for_each_entry(entry, hhd, hlist) {
3944+
map = (struct ftrace_func_map *)entry;
3945+
free_func(map);
3946+
}
3947+
}
3948+
}
3949+
free_ftrace_hash(&mapper->hash);
3950+
}
3951+
38113952
int
38123953
register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
38133954
void *data)

‎kernel/trace/trace.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,8 +944,22 @@ struct ftrace_probe_ops {
944944
unsigned long ip,
945945
struct ftrace_probe_ops *ops,
946946
void *data);
947+
void *private_data;
947948
};
948949

950+
struct ftrace_func_mapper;
951+
typedef int (*ftrace_mapper_func)(void *data);
952+
953+
struct ftrace_func_mapper *allocate_ftrace_func_mapper(void);
954+
void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
955+
unsigned long ip);
956+
int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
957+
unsigned long ip, void *data);
958+
void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
959+
unsigned long ip);
960+
void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
961+
ftrace_mapper_func free_func);
962+
949963
extern int
950964
register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
951965
void *data);

‎kernel/trace/trace_events.c‎

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,32 +2460,44 @@ struct event_probe_data {
24602460
bool enable;
24612461
};
24622462

2463+
static void update_event_probe(struct event_probe_data *data)
2464+
{
2465+
if (data->enable)
2466+
clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2467+
else
2468+
set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2469+
}
2470+
24632471
static void
24642472
event_enable_probe(unsigned long ip, unsigned long parent_ip,
24652473
struct ftrace_probe_ops *ops, void **_data)
24662474
{
2467-
struct event_probe_data **pdata = (struct event_probe_data **)_data;
2468-
struct event_probe_data *data = *pdata;
2475+
struct ftrace_func_mapper *mapper = ops->private_data;
2476+
struct event_probe_data *data;
2477+
void **pdata;
24692478

2470-
if (!data)
2479+
pdata = ftrace_func_mapper_find_ip(mapper, ip);
2480+
if (!pdata || !*pdata)
24712481
return;
24722482

2473-
if (data->enable)
2474-
clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2475-
else
2476-
set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2483+
data = *pdata;
2484+
update_event_probe(data);
24772485
}
24782486

24792487
static void
24802488
event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
24812489
struct ftrace_probe_ops *ops, void **_data)
24822490
{
2483-
struct event_probe_data **pdata = (struct event_probe_data **)_data;
2484-
struct event_probe_data *data = *pdata;
2491+
struct ftrace_func_mapper *mapper = ops->private_data;
2492+
struct event_probe_data *data;
2493+
void **pdata;
24852494

2486-
if (!data)
2495+
pdata = ftrace_func_mapper_find_ip(mapper, ip);
2496+
if (!pdata || !*pdata)
24872497
return;
24882498

2499+
data = *pdata;
2500+
24892501
if (!data->count)
24902502
return;
24912503

@@ -2496,14 +2508,23 @@ event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
24962508
if (data->count != -1)
24972509
(data->count)--;
24982510

2499-
event_enable_probe(ip, parent_ip, ops, _data);
2511+
update_event_probe(data);
25002512
}
25012513

25022514
static int
25032515
event_enable_print(struct seq_file *m, unsigned long ip,
25042516
struct ftrace_probe_ops *ops, void *_data)
25052517
{
2506-
struct event_probe_data *data = _data;
2518+
struct ftrace_func_mapper *mapper = ops->private_data;
2519+
struct event_probe_data *data;
2520+
void **pdata;
2521+
2522+
pdata = ftrace_func_mapper_find_ip(mapper, ip);
2523+
2524+
if (WARN_ON_ONCE(!pdata || !*pdata))
2525+
return 0;
2526+
2527+
data = *pdata;
25072528

25082529
seq_printf(m, "%ps:", (void *)ip);
25092530

@@ -2524,19 +2545,31 @@ static int
25242545
event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
25252546
void **_data)
25262547
{
2548+
struct ftrace_func_mapper *mapper = ops->private_data;
25272549
struct event_probe_data **pdata = (struct event_probe_data **)_data;
25282550
struct event_probe_data *data = *pdata;
2551+
int ret;
2552+
2553+
ret = ftrace_func_mapper_add_ip(mapper, ip, data);
2554+
if (ret < 0)
2555+
return ret;
25292556

25302557
data->ref++;
2558+
25312559
return 0;
25322560
}
25332561

25342562
static void
25352563
event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
25362564
void **_data)
25372565
{
2538-
struct event_probe_data **pdata = (struct event_probe_data **)_data;
2539-
struct event_probe_data *data = *pdata;
2566+
struct ftrace_func_mapper *mapper = ops->private_data;
2567+
struct event_probe_data *data;
2568+
2569+
data = ftrace_func_mapper_remove_ip(mapper, ip);
2570+
2571+
if (WARN_ON_ONCE(!data))
2572+
return;
25402573

25412574
if (WARN_ON_ONCE(data->ref <= 0))
25422575
return;
@@ -2548,7 +2581,6 @@ event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
25482581
module_put(data->file->event_call->mod);
25492582
kfree(data);
25502583
}
2551-
*pdata = NULL;
25522584
}
25532585

25542586
static struct ftrace_probe_ops event_enable_probe_ops = {
@@ -2627,6 +2659,13 @@ event_enable_func(struct ftrace_hash *hash,
26272659
}
26282660

26292661
ret = -ENOMEM;
2662+
2663+
if (!ops->private_data) {
2664+
ops->private_data = allocate_ftrace_func_mapper();
2665+
if (!ops->private_data)
2666+
goto out;
2667+
}
2668+
26302669
data = kzalloc(sizeof(*data), GFP_KERNEL);
26312670
if (!data)
26322671
goto out;
@@ -2663,6 +2702,7 @@ event_enable_func(struct ftrace_hash *hash,
26632702
ret = __ftrace_event_enable_disable(file, 1, 1);
26642703
if (ret < 0)
26652704
goto out_put;
2705+
26662706
ret = register_ftrace_function_probe(glob, ops, data);
26672707
/*
26682708
* The above returns on success the # of functions enabled,

0 commit comments

Comments
 (0)