Skip to contentSkip to conversation

bpf: Move iterator functions into special init section

PATCH Superseded Jiri Olsa 3 participants 4 messages Nov 9, 2020 – Nov 10, 2020

Subsystems (5)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH v5 1/1 bpf: Move iterator functions into special init section

With upcoming changes to pahole, that change the way how and which kernel functions are stored in BTF data, we need a way to recognize iterator functions.

Iterator functions need to be in BTF data, but have no real body and are currently placed in .init.text section, so they are freed after kernel init and are filtered out of BTF data because of that.

The solution is to place these functions under new section:
.init.bpf.preserve_type

And add 2 new symbols to mark that area:
__init_bpf_preserve_type_begin
__init_bpf_preserve_type_end

The code in pahole responsible for picking up the functions will be able to recognize functions from this section and add them to the BTF data and filter out all other .init.text functions.

---
v5 changes:
- use "" in __section macro due to:

    33def8498fdde180 ("treewide: Convert macro and uses of __section(foo) to __section("foo")")
    [Arnaldo]

v4: https://lore.kernel.org/bpf/[email protected]/

 include/asm-generic/vmlinux.lds.h | 16 +++++++++++++++-
 include/linux/bpf.h               |  8 +++++++-
 include/linux/init.h              |  1 +
 3 files changed, 23 insertions(+), 2 deletions(-)
include/asm-generic/vmlinux.lds.h +15 -1
index b2b3d81b1535..f91029b3443b 100644
@@ -685,8 +685,21 @@
 	.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) {			\
 		*(.BTF_ids)						\
 	}
+
+/*
+ * .init.bpf.preserve_type
+ *
+ * This section store special BPF function and marks them
+ * with begin/end symbols pair for the sake of pahole tool.
+ */
+#define INIT_BPF_PRESERVE_TYPE						\
+	__init_bpf_preserve_type_begin = .;                             \
+	*(.init.bpf.preserve_type)                                      \
+	__init_bpf_preserve_type_end = .;				\
+	MEM_DISCARD(init.bpf.preserve_type)
 #else
 #define BTF
+#define INIT_BPF_PRESERVE_TYPE
 #endif
 
 /*
@@ -741,7 +754,8 @@
 #define INIT_TEXT							\
 	*(.init.text .init.text.*)					\
 	*(.text.startup)						\
-	MEM_DISCARD(init.text*)
+	MEM_DISCARD(init.text*)						\
+	INIT_BPF_PRESERVE_TYPE
 
 #define EXIT_DATA							\
 	*(.exit.data .exit.data.*)					\
include/linux/bpf.h +7 -1
index 2b16bf48aab6..73e8ededde3e 100644
@@ -1276,10 +1276,16 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
 int bpf_obj_get_user(const char __user *pathname, int flags);
 
+#ifdef CONFIG_DEBUG_INFO_BTF
+#define BPF_INIT __init_bpf_preserve_type
+#else
+#define BPF_INIT __init
+#endif
+
 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
 #define DEFINE_BPF_ITER_FUNC(target, args...)			\
 	extern int bpf_iter_ ## target(args);			\
-	int __init bpf_iter_ ## target(args) { return 0; }
+	int BPF_INIT bpf_iter_ ## target(args) { return 0; }
 
 struct bpf_iter_aux_info {
 	struct bpf_map *map;
include/linux/init.h +1 -0
index 7b53cb3092ee..a7c71e3b5f9a 100644
@@ -52,6 +52,7 @@
 #define __initconst	__section(".init.rodata")
 #define __exitdata	__section(".exit.data")
 #define __exit_call	__used __section(".exitcall.exit")
+#define __init_bpf_preserve_type __section(".init.bpf.preserve_type")
 
 /*
  * modpost check for section mismatches during the kernel build.
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 101 lines · Jiri Olsa

With upcoming changes to pahole, that change the way how and which kernel functions are stored in BTF data, we need a way to recognize iterator functions.

Iterator functions need to be in BTF data, but have no real body and are currently placed in .init.text section, so they are freed after kernel init and are filtered out of BTF data because of that.

The solution is to place these functions under new section:
.init.bpf.preserve_type

And add 2 new symbols to mark that area:
__init_bpf_preserve_type_begin
__init_bpf_preserve_type_end

The code in pahole responsible for picking up the functions will be able to recognize functions from this section and add them to the BTF data and filter out all other .init.text functions.

---
v5 changes:
- use "" in __section macro due to:

     33def8498fdde180 ("treewide: Convert macro and uses of __section(foo) to __section("foo")")
     [Arnaldo]

v4: https://lore.kernel.org/bpf/[email protected]/

  include/asm-generic/vmlinux.lds.h | 16 +++++++++++++++-
  include/linux/bpf.h               |  8 +++++++-
  include/linux/init.h              |  1 +
  3 files changed, 23 insertions(+), 2 deletions(-)
include/asm-generic/vmlinux.lds.h +15 -1
index b2b3d81b1535..f91029b3443b 100644
@@ -685,8 +685,21 @@
  	.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) {			\
  		*(.BTF_ids)						\
  	}
+
+/*
+ * .init.bpf.preserve_type
+ *
+ * This section store special BPF function and marks them
+ * with begin/end symbols pair for the sake of pahole tool.
+ */
+#define INIT_BPF_PRESERVE_TYPE						\
+	__init_bpf_preserve_type_begin = .;                             \
+	*(.init.bpf.preserve_type)                                      \
+	__init_bpf_preserve_type_end = .;				\
+	MEM_DISCARD(init.bpf.preserve_type)
  #else
  #define BTF
+#define INIT_BPF_PRESERVE_TYPE
  #endif
 
  /*
@@ -741,7 +754,8 @@
  #define INIT_TEXT							\
  	*(.init.text .init.text.*)					\
  	*(.text.startup)						\
-	MEM_DISCARD(init.text*)
+	MEM_DISCARD(init.text*)						\
+	INIT_BPF_PRESERVE_TYPE
 
  #define EXIT_DATA							\
  	*(.exit.data .exit.data.*)					\
include/linux/bpf.h +7 -1
index 2b16bf48aab6..73e8ededde3e 100644
@@ -1276,10 +1276,16 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd);
  int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
  int bpf_obj_get_user(const char __user *pathname, int flags);
 
+#ifdef CONFIG_DEBUG_INFO_BTF
+#define BPF_INIT __init_bpf_preserve_type
+#else
+#define BPF_INIT __init
+#endif
+
  #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
  #define DEFINE_BPF_ITER_FUNC(target, args...)			\
  	extern int bpf_iter_ ## target(args);			\
-	int __init bpf_iter_ ## target(args) { return 0; }
+	int BPF_INIT bpf_iter_ ## target(args) { return 0; }
 
  struct bpf_iter_aux_info {
  	struct bpf_map *map;
include/linux/init.h +0 -0
index 7b53cb3092ee..a7c71e3b5f9a 100644

include/linux/init.h line 55

Quoted 4 lines
include/linux/init.h +1 -0
@@ -52,6 +52,7 @@
  #define __initconst	__section(".init.rodata")
  #define __exitdata	__section(".exit.data")
  #define __exit_call	__used __section(".exitcall.exit")
+#define __init_bpf_preserve_type __section(".init.bpf.preserve_type")

Small nit, why this detour via BPF_INIT define? Couldn't we just:

#ifdef CONFIG_DEBUG_INFO_BTF
#define __init_bpf_preserve_type __section(".init.bpf.preserve_type")
#else
#define __init_bpf_preserve_type __init
#endif

Also, the comment above the existing defines says '/* These are for everybody (although not all archs will actually discard it in modules) */' ... We should probably not add the __init_bpf_preserve_type right under this listing as-is in your patch, but instead 'separate' it by adding a small comment on top of its definition by explaining its purpose more clearly for others.

Quoted 3 lines
include/linux/init.h +0 -0
  /*
   * modpost check for section mismatches during the kernel build.
 
#2 lore raw

On Mon, Nov 09, 2020 at 11:04:34PM +0100, Daniel Borkmann wrote:

SNIP

Quoted 17 lines

include/linux/init.h line 55

Quoted 8 lines

index 7b53cb3092ee..a7c71e3b5f9a 100644

include/linux/init.h +1 -0
@@ -52,6 +52,7 @@
  #define __initconst	__section(".init.rodata")
  #define __exitdata	__section(".exit.data")
  #define __exit_call	__used __section(".exitcall.exit")
+#define __init_bpf_preserve_type __section(".init.bpf.preserve_type")

Small nit, why this detour via BPF_INIT define? Couldn't we just:

#ifdef CONFIG_DEBUG_INFO_BTF
#define __init_bpf_preserve_type __section(".init.bpf.preserve_type")
#else
#define __init_bpf_preserve_type __init
#endif

Quoted 5 lines

Also, the comment above the existing defines says '/* These are for everybody (although not all archs will actually discard it in modules) */' ... We should probably not add the __init_bpf_preserve_type right under this listing as-is in your patch, but instead 'separate' it by adding a small comment on top of its definition by explaining its purpose more clearly for others.

ok, for some reason I thought I needed to add it to init.h, but as it's bpf specific, perhaps we can omit init.h change completely.. how about the change below?

thanks,
jirka

---

include/asm-generic/vmlinux.lds.h +15 -1
index b2b3d81b1535..f91029b3443b 100644
@@ -685,8 +685,21 @@
 	.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) {			\
 		*(.BTF_ids)						\
 	}
+
+/*
+ * .init.bpf.preserve_type
+ *
+ * This section store special BPF function and marks them
+ * with begin/end symbols pair for the sake of pahole tool.
+ */
+#define INIT_BPF_PRESERVE_TYPE						\
+	__init_bpf_preserve_type_begin = .;                             \
+	*(.init.bpf.preserve_type)                                      \
+	__init_bpf_preserve_type_end = .;				\
+	MEM_DISCARD(init.bpf.preserve_type)
 #else
 #define BTF
+#define INIT_BPF_PRESERVE_TYPE
 #endif
 
 /*
@@ -741,7 +754,8 @@
 #define INIT_TEXT							\
 	*(.init.text .init.text.*)					\
 	*(.text.startup)						\
-	MEM_DISCARD(init.text*)
+	MEM_DISCARD(init.text*)						\
+	INIT_BPF_PRESERVE_TYPE
 
 #define EXIT_DATA							\
 	*(.exit.data .exit.data.*)					\
include/linux/bpf.h +11 -1
index 2b16bf48aab6..1739a92516ed 100644
@@ -1276,10 +1276,20 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd);
 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
 int bpf_obj_get_user(const char __user *pathname, int flags);
 
+/* In case we generate BTF data, we need to group all iterator
+ * functions into special init section, so pahole can track them.
+ * Otherwise pure __init section is enough.
+ */
+#ifdef CONFIG_DEBUG_INFO_BTF
+#define __init_bpf_preserve_type __section(".init.bpf.preserve_type")
+#else
+#define __init_bpf_preserve_type __init
+#endif
+
 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
 #define DEFINE_BPF_ITER_FUNC(target, args...)			\
 	extern int bpf_iter_ ## target(args);			\
-	int __init bpf_iter_ ## target(args) { return 0; }
+	int __init_bpf_preserve_type bpf_iter_ ## target(args) { return 0; }
 
 struct bpf_iter_aux_info {
 	struct bpf_map *map;
maintainer
#3 lore raw
Quoted 27 lines · Jiri Olsa

On Mon, Nov 09, 2020 at 11:04:34PM +0100, Daniel Borkmann wrote:

SNIP

Quoted 17 lines

include/linux/init.h line 55

Quoted 8 lines

index 7b53cb3092ee..a7c71e3b5f9a 100644

include/linux/init.h +1 -0
@@ -52,6 +52,7 @@
   #define __initconst	__section(".init.rodata")
   #define __exitdata	__section(".exit.data")
   #define __exit_call	__used __section(".exitcall.exit")
+#define __init_bpf_preserve_type __section(".init.bpf.preserve_type")

Small nit, why this detour via BPF_INIT define? Couldn't we just:

#ifdef CONFIG_DEBUG_INFO_BTF
#define __init_bpf_preserve_type __section(".init.bpf.preserve_type")
#else
#define __init_bpf_preserve_type __init
#endif

Quoted 5 lines

Also, the comment above the existing defines says '/* These are for everybody (although not all archs will actually discard it in modules) */' ... We should probably not add the __init_bpf_preserve_type right under this listing as-is in your patch, but instead 'separate' it by adding a small comment on top of its definition by explaining its purpose more clearly for others.

Quoted 3 lines

ok, for some reason I thought I needed to add it to init.h, but as it's bpf specific, perhaps we can omit init.h change completely.. how about the change below?

Agree, that looks much better, thanks!