Skip to content

Commit b04d52e

Browse files
oleg-nesterovrostedt
authored andcommitted
tracing/kprobes: Turn trace_probe->files into list_head
I think that "ftrace_event_file *trace_probe[]" complicates the code for no reason, turn it into list_head to simplify the code. enable_trace_probe() no longer needs synchronize_sched(). This needs the extra sizeof(list_head) memory for every attached ftrace_event_file, hopefully not a problem in this case. Link: http://lkml.kernel.org/r/20130620173814.GA13165@redhat.com Acked-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Signed-off-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 3baa5e4 commit b04d52e

1 file changed

Lines changed: 37 additions & 101 deletions

File tree

‎kernel/trace/trace_kprobe.c‎

Lines changed: 37 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ struct trace_probe {
3535
const char *symbol; /* symbol name */
3636
struct ftrace_event_class class;
3737
struct ftrace_event_call call;
38-
struct ftrace_event_file * __rcu *files;
38+
struct list_head files;
3939
ssize_t size; /* trace entry size */
4040
unsigned int nr_args;
4141
struct probe_arg args[];
4242
};
4343

44+
struct event_file_link {
45+
struct ftrace_event_file *file;
46+
struct list_head list;
47+
};
48+
4449
#define SIZEOF_TRACE_PROBE(n) \
4550
(offsetof(struct trace_probe, args) + \
4651
(sizeof(struct probe_arg) * (n)))
@@ -150,6 +155,7 @@ static struct trace_probe *alloc_trace_probe(const char *group,
150155
goto error;
151156

152157
INIT_LIST_HEAD(&tp->list);
158+
INIT_LIST_HEAD(&tp->files);
153159
return tp;
154160
error:
155161
kfree(tp->call.name);
@@ -183,22 +189,6 @@ static struct trace_probe *find_trace_probe(const char *event,
183189
return NULL;
184190
}
185191

186-
/*
187-
* This and enable_trace_probe/disable_trace_probe rely on event_mutex
188-
* held by the caller, __ftrace_set_clr_event().
189-
*/
190-
static int trace_probe_nr_files(struct trace_probe *tp)
191-
{
192-
struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
193-
int ret = 0;
194-
195-
if (file)
196-
while (*(file++))
197-
ret++;
198-
199-
return ret;
200-
}
201-
202192
/*
203193
* Enable trace_probe
204194
* if the file is NULL, enable "perf" handler, or enable "trace" handler.
@@ -209,29 +199,18 @@ enable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
209199
int ret = 0;
210200

211201
if (file) {
212-
struct ftrace_event_file **new, **old;
213-
int n = trace_probe_nr_files(tp);
214-
215-
old = rcu_dereference_raw(tp->files);
216-
/* 1 is for new one and 1 is for stopper */
217-
new = kzalloc((n + 2) * sizeof(struct ftrace_event_file *),
218-
GFP_KERNEL);
219-
if (!new) {
202+
struct event_file_link *link;
203+
204+
link = kmalloc(sizeof(*link), GFP_KERNEL);
205+
if (!link) {
220206
ret = -ENOMEM;
221207
goto out;
222208
}
223-
memcpy(new, old, n * sizeof(struct ftrace_event_file *));
224-
new[n] = file;
225-
/* The last one keeps a NULL */
226209

227-
rcu_assign_pointer(tp->files, new);
228-
tp->flags |= TP_FLAG_TRACE;
210+
link->file = file;
211+
list_add_tail_rcu(&link->list, &tp->files);
229212

230-
if (old) {
231-
/* Make sure the probe is done with old files */
232-
synchronize_sched();
233-
kfree(old);
234-
}
213+
tp->flags |= TP_FLAG_TRACE;
235214
} else
236215
tp->flags |= TP_FLAG_PROFILE;
237216

@@ -245,24 +224,16 @@ enable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
245224
return ret;
246225
}
247226

248-
static int
249-
trace_probe_file_index(struct trace_probe *tp, struct ftrace_event_file *file)
227+
static struct event_file_link *
228+
find_event_file_link(struct trace_probe *tp, struct ftrace_event_file *file)
250229
{
251-
struct ftrace_event_file **files;
252-
int i;
230+
struct event_file_link *link;
253231

254-
/*
255-
* Since all tp->files updater is protected by probe_enable_lock,
256-
* we don't need to lock an rcu_read_lock.
257-
*/
258-
files = rcu_dereference_raw(tp->files);
259-
if (files) {
260-
for (i = 0; files[i]; i++)
261-
if (files[i] == file)
262-
return i;
263-
}
232+
list_for_each_entry(link, &tp->files, list)
233+
if (link->file == file)
234+
return link;
264235

265-
return -1;
236+
return NULL;
266237
}
267238

268239
/*
@@ -275,38 +246,23 @@ disable_trace_probe(struct trace_probe *tp, struct ftrace_event_file *file)
275246
int ret = 0;
276247

277248
if (file) {
278-
struct ftrace_event_file **new, **old;
279-
int n = trace_probe_nr_files(tp);
280-
int i, j;
249+
struct event_file_link *link;
281250

282-
old = rcu_dereference_raw(tp->files);
283-
if (n == 0 || trace_probe_file_index(tp, file) < 0) {
251+
link = find_event_file_link(tp, file);
252+
if (!link) {
284253
ret = -EINVAL;
285254
goto out;
286255
}
287256

288-
if (n == 1) { /* Remove the last file */
289-
tp->flags &= ~TP_FLAG_TRACE;
290-
new = NULL;
291-
} else {
292-
new = kzalloc(n * sizeof(struct ftrace_event_file *),
293-
GFP_KERNEL);
294-
if (!new) {
295-
ret = -ENOMEM;
296-
goto out;
297-
}
298-
299-
/* This copy & check loop copies the NULL stopper too */
300-
for (i = 0, j = 0; j < n && i < n + 1; i++)
301-
if (old[i] != file)
302-
new[j++] = old[i];
303-
}
257+
list_del_rcu(&link->list);
258+
/* synchronize with kprobe_trace_func/kretprobe_trace_func */
259+
synchronize_sched();
260+
kfree(link);
304261

305-
rcu_assign_pointer(tp->files, new);
262+
if (!list_empty(&tp->files))
263+
goto out;
306264

307-
/* Make sure the probe is done with old files */
308-
synchronize_sched();
309-
kfree(old);
265+
tp->flags &= ~TP_FLAG_TRACE;
310266
} else
311267
tp->flags &= ~TP_FLAG_PROFILE;
312268

@@ -871,20 +827,10 @@ __kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs,
871827
static __kprobes void
872828
kprobe_trace_func(struct trace_probe *tp, struct pt_regs *regs)
873829
{
874-
/*
875-
* Note: preempt is already disabled around the kprobe handler.
876-
* However, we still need an smp_read_barrier_depends() corresponding
877-
* to smp_wmb() in rcu_assign_pointer() to access the pointer.
878-
*/
879-
struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
880-
881-
if (unlikely(!file))
882-
return;
830+
struct event_file_link *link;
883831

884-
while (*file) {
885-
__kprobe_trace_func(tp, regs, *file);
886-
file++;
887-
}
832+
list_for_each_entry_rcu(link, &tp->files, list)
833+
__kprobe_trace_func(tp, regs, link->file);
888834
}
889835

890836
/* Kretprobe handler */
@@ -931,20 +877,10 @@ static __kprobes void
931877
kretprobe_trace_func(struct trace_probe *tp, struct kretprobe_instance *ri,
932878
struct pt_regs *regs)
933879
{
934-
/*
935-
* Note: preempt is already disabled around the kprobe handler.
936-
* However, we still need an smp_read_barrier_depends() corresponding
937-
* to smp_wmb() in rcu_assign_pointer() to access the pointer.
938-
*/
939-
struct ftrace_event_file **file = rcu_dereference_raw(tp->files);
940-
941-
if (unlikely(!file))
942-
return;
880+
struct event_file_link *link;
943881

944-
while (*file) {
945-
__kretprobe_trace_func(tp, ri, regs, *file);
946-
file++;
947-
}
882+
list_for_each_entry_rcu(link, &tp->files, list)
883+
__kretprobe_trace_func(tp, ri, regs, link->file);
948884
}
949885

950886
/* Event entry printers */

0 commit comments

Comments
 (0)