Skip to content

Commit 672a2bf

Browse files
Song Chenmhiramat
authored andcommitted
kernel/trace: Provide default impelentations defined in trace_probe_tmpl.h
There are 6 function definitions in trace_probe_tmpl.h, they are: 1, fetch_store_strlen 2, fetch_store_string 3, fetch_store_strlen_user 4, fetch_store_string_user 5, probe_mem_read 6, probe_mem_read_user Every C file which includes trace_probe_tmpl.h has to implement them, otherwise it gets warnings and errors. However, some of them are identical, like kprobe and eprobe, as a result, there is a lot redundant code in those 2 files. This patch would like to provide default behaviors for those functions which kprobe and eprobe can share by just including trace_probe_kernel.h with trace_probe_tmpl.h together. It removes redundant code, increases readability, and more importantly, makes it easier to introduce a new feature based on trace probe (it's possible). Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Song Chen <[email protected]> Reported-by: kernel test robot <[email protected]> Acked-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent 196b638 commit 672a2bf

File tree

4 files changed

+29
-116
lines changed

4 files changed

+29
-116
lines changed

kernel/trace/trace_eprobe.c

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ print_eprobe_event(struct trace_iterator *iter, int flags,
320320
return trace_handle_return(s);
321321
}
322322

323-
static unsigned long get_event_field(struct fetch_insn *code, void *rec)
323+
static nokprobe_inline unsigned long
324+
get_event_field(struct fetch_insn *code, void *rec)
324325
{
325326
struct ftrace_event_field *field = code->data;
326327
unsigned long val;
@@ -454,58 +455,6 @@ process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
454455
}
455456
NOKPROBE_SYMBOL(process_fetch_insn)
456457

457-
/* Return the length of string -- including null terminal byte */
458-
static nokprobe_inline int
459-
fetch_store_strlen_user(unsigned long addr)
460-
{
461-
return kern_fetch_store_strlen_user(addr);
462-
}
463-
464-
/* Return the length of string -- including null terminal byte */
465-
static nokprobe_inline int
466-
fetch_store_strlen(unsigned long addr)
467-
{
468-
return kern_fetch_store_strlen(addr);
469-
}
470-
471-
/*
472-
* Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
473-
* with max length and relative data location.
474-
*/
475-
static nokprobe_inline int
476-
fetch_store_string_user(unsigned long addr, void *dest, void *base)
477-
{
478-
return kern_fetch_store_string_user(addr, dest, base);
479-
}
480-
481-
/*
482-
* Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
483-
* length and relative data location.
484-
*/
485-
static nokprobe_inline int
486-
fetch_store_string(unsigned long addr, void *dest, void *base)
487-
{
488-
return kern_fetch_store_string(addr, dest, base);
489-
}
490-
491-
static nokprobe_inline int
492-
probe_mem_read_user(void *dest, void *src, size_t size)
493-
{
494-
const void __user *uaddr = (__force const void __user *)src;
495-
496-
return copy_from_user_nofault(dest, uaddr, size);
497-
}
498-
499-
static nokprobe_inline int
500-
probe_mem_read(void *dest, void *src, size_t size)
501-
{
502-
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
503-
if ((unsigned long)src < TASK_SIZE)
504-
return probe_mem_read_user(dest, src, size);
505-
#endif
506-
return copy_from_kernel_nofault(dest, src, size);
507-
}
508-
509458
/* eprobe handler */
510459
static inline void
511460
__eprobe_trace_func(struct eprobe_data *edata, void *rec)

kernel/trace/trace_events_synth.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,12 @@ static unsigned int trace_string(struct synth_trace_event *entry,
420420
data_offset += event->n_u64 * sizeof(u64);
421421
data_offset += data_size;
422422

423-
len = kern_fetch_store_strlen((unsigned long)str_val);
423+
len = fetch_store_strlen((unsigned long)str_val);
424424

425425
data_offset |= len << 16;
426426
*(u32 *)&entry->fields[*n_u64] = data_offset;
427427

428-
ret = kern_fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
428+
ret = fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
429429

430430
(*n_u64)++;
431431
} else {
@@ -473,7 +473,7 @@ static notrace void trace_event_raw_event_synth(void *__data,
473473
val_idx = var_ref_idx[field_pos];
474474
str_val = (char *)(long)var_ref_vals[val_idx];
475475

476-
len = kern_fetch_store_strlen((unsigned long)str_val);
476+
len = fetch_store_strlen((unsigned long)str_val);
477477

478478
fields_size += len;
479479
}

kernel/trace/trace_kprobe.c

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,60 +1218,6 @@ static const struct file_operations kprobe_profile_ops = {
12181218
.release = seq_release,
12191219
};
12201220

1221-
/* Kprobe specific fetch functions */
1222-
1223-
/* Return the length of string -- including null terminal byte */
1224-
static nokprobe_inline int
1225-
fetch_store_strlen_user(unsigned long addr)
1226-
{
1227-
return kern_fetch_store_strlen_user(addr);
1228-
}
1229-
1230-
/* Return the length of string -- including null terminal byte */
1231-
static nokprobe_inline int
1232-
fetch_store_strlen(unsigned long addr)
1233-
{
1234-
return kern_fetch_store_strlen(addr);
1235-
}
1236-
1237-
/*
1238-
* Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
1239-
* with max length and relative data location.
1240-
*/
1241-
static nokprobe_inline int
1242-
fetch_store_string_user(unsigned long addr, void *dest, void *base)
1243-
{
1244-
return kern_fetch_store_string_user(addr, dest, base);
1245-
}
1246-
1247-
/*
1248-
* Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
1249-
* length and relative data location.
1250-
*/
1251-
static nokprobe_inline int
1252-
fetch_store_string(unsigned long addr, void *dest, void *base)
1253-
{
1254-
return kern_fetch_store_string(addr, dest, base);
1255-
}
1256-
1257-
static nokprobe_inline int
1258-
probe_mem_read_user(void *dest, void *src, size_t size)
1259-
{
1260-
const void __user *uaddr = (__force const void __user *)src;
1261-
1262-
return copy_from_user_nofault(dest, uaddr, size);
1263-
}
1264-
1265-
static nokprobe_inline int
1266-
probe_mem_read(void *dest, void *src, size_t size)
1267-
{
1268-
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
1269-
if ((unsigned long)src < TASK_SIZE)
1270-
return probe_mem_read_user(dest, src, size);
1271-
#endif
1272-
return copy_from_kernel_nofault(dest, src, size);
1273-
}
1274-
12751221
/* Note that we don't verify it, since the code does not come from user space */
12761222
static int
12771223
process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,

kernel/trace/trace_probe_kernel.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
/* Return the length of string -- including null terminal byte */
1414
static nokprobe_inline int
15-
kern_fetch_store_strlen_user(unsigned long addr)
15+
fetch_store_strlen_user(unsigned long addr)
1616
{
1717
const void __user *uaddr = (__force const void __user *)addr;
1818
int ret;
@@ -29,14 +29,14 @@ kern_fetch_store_strlen_user(unsigned long addr)
2929

3030
/* Return the length of string -- including null terminal byte */
3131
static nokprobe_inline int
32-
kern_fetch_store_strlen(unsigned long addr)
32+
fetch_store_strlen(unsigned long addr)
3333
{
3434
int ret, len = 0;
3535
u8 c;
3636

3737
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
3838
if (addr < TASK_SIZE)
39-
return kern_fetch_store_strlen_user(addr);
39+
return fetch_store_strlen_user(addr);
4040
#endif
4141

4242
do {
@@ -63,7 +63,7 @@ static nokprobe_inline void set_data_loc(int ret, void *dest, void *__dest, void
6363
* with max length and relative data location.
6464
*/
6565
static nokprobe_inline int
66-
kern_fetch_store_string_user(unsigned long addr, void *dest, void *base)
66+
fetch_store_string_user(unsigned long addr, void *dest, void *base)
6767
{
6868
const void __user *uaddr = (__force const void __user *)addr;
6969
int maxlen = get_loc_len(*(u32 *)dest);
@@ -86,15 +86,15 @@ kern_fetch_store_string_user(unsigned long addr, void *dest, void *base)
8686
* length and relative data location.
8787
*/
8888
static nokprobe_inline int
89-
kern_fetch_store_string(unsigned long addr, void *dest, void *base)
89+
fetch_store_string(unsigned long addr, void *dest, void *base)
9090
{
9191
int maxlen = get_loc_len(*(u32 *)dest);
9292
void *__dest;
9393
long ret;
9494

9595
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
9696
if ((unsigned long)addr < TASK_SIZE)
97-
return kern_fetch_store_string_user(addr, dest, base);
97+
return fetch_store_string_user(addr, dest, base);
9898
#endif
9999

100100
if (unlikely(!maxlen))
@@ -112,4 +112,22 @@ kern_fetch_store_string(unsigned long addr, void *dest, void *base)
112112
return ret;
113113
}
114114

115+
static nokprobe_inline int
116+
probe_mem_read_user(void *dest, void *src, size_t size)
117+
{
118+
const void __user *uaddr = (__force const void __user *)src;
119+
120+
return copy_from_user_nofault(dest, uaddr, size);
121+
}
122+
123+
static nokprobe_inline int
124+
probe_mem_read(void *dest, void *src, size_t size)
125+
{
126+
#ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
127+
if ((unsigned long)src < TASK_SIZE)
128+
return probe_mem_read_user(dest, src, size);
129+
#endif
130+
return copy_from_kernel_nofault(dest, src, size);
131+
}
132+
115133
#endif /* __TRACE_PROBE_KERNEL_H_ */

0 commit comments

Comments
 (0)