Skip to content

Commit b5fa903

Browse files
mhiramatrostedt
authored andcommitted
fprobe: Add fprobe_header encoding feature
Fprobe store its data structure address and size on the fgraph return stack by __fprobe_header. But most 64bit architecture can combine those to one unsigned long value because 4 MSB in the kernel address are the same. With this encoding, fprobe can consume less space on ret_stack. This introduces asm/fprobe.h to define arch dependent encode/decode macros. Note that since fprobe depends on CONFIG_HAVE_FUNCTION_GRAPH_FREGS, currently only arm64, loongarch, riscv, s390 and x86 are supported. Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Acked-by: Heiko Carstens <hca@linux.ibm.com> # s390 Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com> Cc: Florent Revest <revest@chromium.org> Cc: Martin KaFai Lau <martin.lau@linux.dev> Cc: bpf <bpf@vger.kernel.org> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Alan Maguire <alan.maguire@oracle.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Will Deacon <will@kernel.org> Cc: Huacai Chen <chenhuacai@kernel.org> Cc: WANG Xuerui <kernel@xen0n.name> Cc: Paul Walmsley <paul.walmsley@sifive.com> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Albert Ou <aou@eecs.berkeley.edu> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: Alexander Gordeev <agordeev@linux.ibm.com> Cc: Christian Borntraeger <borntraeger@linux.ibm.com> Cc: Sven Schnelle <svens@linux.ibm.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: x86@kernel.org Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Link: https://lore.kernel.org/173519005783.391279.5307910947400277525.stgit@devnote2 Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 4346ba1 commit b5fa903

File tree

7 files changed

+100
-0
lines changed

7 files changed

+100
-0
lines changed

arch/arm64/include/asm/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ syscall-y += unistd_32.h
88
syscall-y += unistd_compat_32.h
99

1010
generic-y += early_ioremap.h
11+
generic-y += fprobe.h
1112
generic-y += mcs_spinlock.h
1213
generic-y += mmzone.h
1314
generic-y += qrwlock.h

arch/loongarch/include/asm/fprobe.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_LOONGARCH_FPROBE_H
3+
#define _ASM_LOONGARCH_FPROBE_H
4+
5+
/*
6+
* Explicitly undef ARCH_DEFINE_ENCODE_FPROBE_HEADER, because loongarch does not
7+
* have enough number of fixed MSBs of the address of kernel objects for
8+
* encoding the size of data in fprobe_header. Use 2-entries encoding instead.
9+
*/
10+
#undef ARCH_DEFINE_ENCODE_FPROBE_HEADER
11+
12+
#endif /* _ASM_LOONGARCH_FPROBE_H */

arch/riscv/include/asm/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ syscall-y += syscall_table_64.h
44

55
generic-y += early_ioremap.h
66
generic-y += flat.h
7+
generic-y += fprobe.h
78
generic-y += kvm_para.h
89
generic-y += mmzone.h
910
generic-y += mcs_spinlock.h

arch/s390/include/asm/fprobe.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_S390_FPROBE_H
3+
#define _ASM_S390_FPROBE_H
4+
5+
#include <asm-generic/fprobe.h>
6+
7+
#undef FPROBE_HEADER_MSB_PATTERN
8+
#define FPROBE_HEADER_MSB_PATTERN 0
9+
10+
#endif /* _ASM_S390_FPROBE_H */

arch/x86/include/asm/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ generated-y += unistd_64_x32.h
1010
generated-y += xen-hypercalls.h
1111

1212
generic-y += early_ioremap.h
13+
generic-y += fprobe.h
1314
generic-y += mcs_spinlock.h
1415
generic-y += mmzone.h

include/asm-generic/fprobe.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Generic arch dependent fprobe macros.
4+
*/
5+
#ifndef __ASM_GENERIC_FPROBE_H__
6+
#define __ASM_GENERIC_FPROBE_H__
7+
8+
#include <linux/bits.h>
9+
10+
#ifdef CONFIG_64BIT
11+
/*
12+
* Encoding the size and the address of fprobe into one 64bit entry.
13+
* The 32bit architectures should use 2 entries to store those info.
14+
*/
15+
16+
#define ARCH_DEFINE_ENCODE_FPROBE_HEADER
17+
18+
#define FPROBE_HEADER_MSB_SIZE_SHIFT (BITS_PER_LONG - FPROBE_DATA_SIZE_BITS)
19+
#define FPROBE_HEADER_MSB_MASK \
20+
GENMASK(FPROBE_HEADER_MSB_SIZE_SHIFT - 1, 0)
21+
22+
/*
23+
* By default, this expects the MSBs in the address of kprobe is 0xf.
24+
* If any arch needs another fixed pattern (e.g. s390 is zero filled),
25+
* override this.
26+
*/
27+
#define FPROBE_HEADER_MSB_PATTERN \
28+
GENMASK(BITS_PER_LONG - 1, FPROBE_HEADER_MSB_SIZE_SHIFT)
29+
30+
#define arch_fprobe_header_encodable(fp) \
31+
(((unsigned long)(fp) & ~FPROBE_HEADER_MSB_MASK) == \
32+
FPROBE_HEADER_MSB_PATTERN)
33+
34+
#define arch_encode_fprobe_header(fp, size) \
35+
(((unsigned long)(fp) & FPROBE_HEADER_MSB_MASK) | \
36+
((unsigned long)(size) << FPROBE_HEADER_MSB_SIZE_SHIFT))
37+
38+
#define arch_decode_fprobe_header_size(val) \
39+
((unsigned long)(val) >> FPROBE_HEADER_MSB_SIZE_SHIFT)
40+
41+
#define arch_decode_fprobe_header_fp(val) \
42+
((struct fprobe *)(((unsigned long)(val) & FPROBE_HEADER_MSB_MASK) | \
43+
FPROBE_HEADER_MSB_PATTERN))
44+
#endif /* CONFIG_64BIT */
45+
46+
#endif /* __ASM_GENERIC_FPROBE_H__ */

kernel/trace/fprobe.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <linux/slab.h>
1414
#include <linux/sort.h>
1515

16+
#include <asm/fprobe.h>
17+
1618
#include "trace.h"
1719

1820
#define FPROBE_IP_HASH_BITS 8
@@ -143,6 +145,31 @@ static int del_fprobe_hash(struct fprobe *fp)
143145
return 0;
144146
}
145147

148+
#ifdef ARCH_DEFINE_ENCODE_FPROBE_HEADER
149+
150+
/* The arch should encode fprobe_header info into one unsigned long */
151+
#define FPROBE_HEADER_SIZE_IN_LONG 1
152+
153+
static inline bool write_fprobe_header(unsigned long *stack,
154+
struct fprobe *fp, unsigned int size_words)
155+
{
156+
if (WARN_ON_ONCE(size_words > MAX_FPROBE_DATA_SIZE_WORD ||
157+
!arch_fprobe_header_encodable(fp)))
158+
return false;
159+
160+
*stack = arch_encode_fprobe_header(fp, size_words);
161+
return true;
162+
}
163+
164+
static inline void read_fprobe_header(unsigned long *stack,
165+
struct fprobe **fp, unsigned int *size_words)
166+
{
167+
*fp = arch_decode_fprobe_header_fp(*stack);
168+
*size_words = arch_decode_fprobe_header_size(*stack);
169+
}
170+
171+
#else
172+
146173
/* Generic fprobe_header */
147174
struct __fprobe_header {
148175
struct fprobe *fp;
@@ -173,6 +200,8 @@ static inline void read_fprobe_header(unsigned long *stack,
173200
*size_words = fph->size_words;
174201
}
175202

203+
#endif
204+
176205
/*
177206
* fprobe shadow stack management:
178207
* Since fprobe shares a single fgraph_ops, it needs to share the stack entry

0 commit comments

Comments
 (0)