Skip to content

Commit 3eaea21

Browse files
anakryikomhiramat
authored andcommitted
uprobes: encapsulate preparation of uprobe args buffer
Move the logic of fetching temporary per-CPU uprobe buffer and storing uprobes args into it to a new helper function. Store data size as part of this buffer, simplifying interfaces a bit, as now we only pass single uprobe_cpu_buffer reference around, instead of pointer + dsize. This logic was duplicated across uprobe_dispatcher and uretprobe_dispatcher, and now will be centralized. All this is also in preparation to make this uprobe_cpu_buffer handling logic optional in the next patch. Link: https://lore.kernel.org/all/[email protected]/ [Masami: update for v6.9-rc3 kernel] Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Jiri Olsa <[email protected]> Acked-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent e67572c commit 3eaea21

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

kernel/trace/trace_uprobe.c

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ static const struct file_operations uprobe_profile_ops = {
854854
struct uprobe_cpu_buffer {
855855
struct mutex mutex;
856856
void *buf;
857+
int dsize;
857858
};
858859
static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
859860
static int uprobe_buffer_refcnt;
@@ -943,9 +944,26 @@ static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
943944
mutex_unlock(&ucb->mutex);
944945
}
945946

947+
static struct uprobe_cpu_buffer *prepare_uprobe_buffer(struct trace_uprobe *tu,
948+
struct pt_regs *regs)
949+
{
950+
struct uprobe_cpu_buffer *ucb;
951+
int dsize, esize;
952+
953+
esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
954+
dsize = __get_data_size(&tu->tp, regs, NULL);
955+
956+
ucb = uprobe_buffer_get();
957+
ucb->dsize = tu->tp.size + dsize;
958+
959+
store_trace_args(ucb->buf, &tu->tp, regs, NULL, esize, dsize);
960+
961+
return ucb;
962+
}
963+
946964
static void __uprobe_trace_func(struct trace_uprobe *tu,
947965
unsigned long func, struct pt_regs *regs,
948-
struct uprobe_cpu_buffer *ucb, int dsize,
966+
struct uprobe_cpu_buffer *ucb,
949967
struct trace_event_file *trace_file)
950968
{
951969
struct uprobe_trace_entry_head *entry;
@@ -956,14 +974,14 @@ static void __uprobe_trace_func(struct trace_uprobe *tu,
956974

957975
WARN_ON(call != trace_file->event_call);
958976

959-
if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
977+
if (WARN_ON_ONCE(ucb->dsize > PAGE_SIZE))
960978
return;
961979

962980
if (trace_trigger_soft_disabled(trace_file))
963981
return;
964982

965983
esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
966-
size = esize + tu->tp.size + dsize;
984+
size = esize + ucb->dsize;
967985
entry = trace_event_buffer_reserve(&fbuffer, trace_file, size);
968986
if (!entry)
969987
return;
@@ -977,14 +995,14 @@ static void __uprobe_trace_func(struct trace_uprobe *tu,
977995
data = DATAOF_TRACE_ENTRY(entry, false);
978996
}
979997

980-
memcpy(data, ucb->buf, tu->tp.size + dsize);
998+
memcpy(data, ucb->buf, ucb->dsize);
981999

9821000
trace_event_buffer_commit(&fbuffer);
9831001
}
9841002

9851003
/* uprobe handler */
9861004
static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
987-
struct uprobe_cpu_buffer *ucb, int dsize)
1005+
struct uprobe_cpu_buffer *ucb)
9881006
{
9891007
struct event_file_link *link;
9901008

@@ -993,21 +1011,21 @@ static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
9931011

9941012
rcu_read_lock();
9951013
trace_probe_for_each_link_rcu(link, &tu->tp)
996-
__uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
1014+
__uprobe_trace_func(tu, 0, regs, ucb, link->file);
9971015
rcu_read_unlock();
9981016

9991017
return 0;
10001018
}
10011019

10021020
static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
10031021
struct pt_regs *regs,
1004-
struct uprobe_cpu_buffer *ucb, int dsize)
1022+
struct uprobe_cpu_buffer *ucb)
10051023
{
10061024
struct event_file_link *link;
10071025

10081026
rcu_read_lock();
10091027
trace_probe_for_each_link_rcu(link, &tu->tp)
1010-
__uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
1028+
__uprobe_trace_func(tu, func, regs, ucb, link->file);
10111029
rcu_read_unlock();
10121030
}
10131031

@@ -1335,7 +1353,7 @@ static bool uprobe_perf_filter(struct uprobe_consumer *uc,
13351353

13361354
static void __uprobe_perf_func(struct trace_uprobe *tu,
13371355
unsigned long func, struct pt_regs *regs,
1338-
struct uprobe_cpu_buffer *ucb, int dsize)
1356+
struct uprobe_cpu_buffer *ucb)
13391357
{
13401358
struct trace_event_call *call = trace_probe_event_call(&tu->tp);
13411359
struct uprobe_trace_entry_head *entry;
@@ -1356,7 +1374,7 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
13561374

13571375
esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
13581376

1359-
size = esize + tu->tp.size + dsize;
1377+
size = esize + ucb->dsize;
13601378
size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
13611379
if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
13621380
return;
@@ -1379,13 +1397,10 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
13791397
data = DATAOF_TRACE_ENTRY(entry, false);
13801398
}
13811399

1382-
memcpy(data, ucb->buf, tu->tp.size + dsize);
1400+
memcpy(data, ucb->buf, ucb->dsize);
13831401

1384-
if (size - esize > tu->tp.size + dsize) {
1385-
int len = tu->tp.size + dsize;
1386-
1387-
memset(data + len, 0, size - esize - len);
1388-
}
1402+
if (size - esize > ucb->dsize)
1403+
memset(data + ucb->dsize, 0, size - esize - ucb->dsize);
13891404

13901405
perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
13911406
head, NULL);
@@ -1395,21 +1410,21 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
13951410

13961411
/* uprobe profile handler */
13971412
static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1398-
struct uprobe_cpu_buffer *ucb, int dsize)
1413+
struct uprobe_cpu_buffer *ucb)
13991414
{
14001415
if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
14011416
return UPROBE_HANDLER_REMOVE;
14021417

14031418
if (!is_ret_probe(tu))
1404-
__uprobe_perf_func(tu, 0, regs, ucb, dsize);
1419+
__uprobe_perf_func(tu, 0, regs, ucb);
14051420
return 0;
14061421
}
14071422

14081423
static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
14091424
struct pt_regs *regs,
1410-
struct uprobe_cpu_buffer *ucb, int dsize)
1425+
struct uprobe_cpu_buffer *ucb)
14111426
{
1412-
__uprobe_perf_func(tu, func, regs, ucb, dsize);
1427+
__uprobe_perf_func(tu, func, regs, ucb);
14131428
}
14141429

14151430
int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
@@ -1475,10 +1490,8 @@ static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
14751490
struct trace_uprobe *tu;
14761491
struct uprobe_dispatch_data udd;
14771492
struct uprobe_cpu_buffer *ucb;
1478-
int dsize, esize;
14791493
int ret = 0;
14801494

1481-
14821495
tu = container_of(con, struct trace_uprobe, consumer);
14831496
tu->nhit++;
14841497

@@ -1490,18 +1503,14 @@ static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
14901503
if (WARN_ON_ONCE(!uprobe_cpu_buffer))
14911504
return 0;
14921505

1493-
dsize = __get_data_size(&tu->tp, regs, NULL);
1494-
esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1495-
1496-
ucb = uprobe_buffer_get();
1497-
store_trace_args(ucb->buf, &tu->tp, regs, NULL, esize, dsize);
1506+
ucb = prepare_uprobe_buffer(tu, regs);
14981507

14991508
if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1500-
ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1509+
ret |= uprobe_trace_func(tu, regs, ucb);
15011510

15021511
#ifdef CONFIG_PERF_EVENTS
15031512
if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1504-
ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1513+
ret |= uprobe_perf_func(tu, regs, ucb);
15051514
#endif
15061515
uprobe_buffer_put(ucb);
15071516
return ret;
@@ -1513,7 +1522,6 @@ static int uretprobe_dispatcher(struct uprobe_consumer *con,
15131522
struct trace_uprobe *tu;
15141523
struct uprobe_dispatch_data udd;
15151524
struct uprobe_cpu_buffer *ucb;
1516-
int dsize, esize;
15171525

15181526
tu = container_of(con, struct trace_uprobe, consumer);
15191527

@@ -1525,18 +1533,14 @@ static int uretprobe_dispatcher(struct uprobe_consumer *con,
15251533
if (WARN_ON_ONCE(!uprobe_cpu_buffer))
15261534
return 0;
15271535

1528-
dsize = __get_data_size(&tu->tp, regs, NULL);
1529-
esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1530-
1531-
ucb = uprobe_buffer_get();
1532-
store_trace_args(ucb->buf, &tu->tp, regs, NULL, esize, dsize);
1536+
ucb = prepare_uprobe_buffer(tu, regs);
15331537

15341538
if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1535-
uretprobe_trace_func(tu, func, regs, ucb, dsize);
1539+
uretprobe_trace_func(tu, func, regs, ucb);
15361540

15371541
#ifdef CONFIG_PERF_EVENTS
15381542
if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1539-
uretprobe_perf_func(tu, func, regs, ucb, dsize);
1543+
uretprobe_perf_func(tu, func, regs, ucb);
15401544
#endif
15411545
uprobe_buffer_put(ucb);
15421546
return 0;

0 commit comments

Comments
 (0)