Skip to contentSkip to conversation

bpf: Add more information into bpffs

PATCH In review Yafang Shao 3 participants 11 messages Feb 11, 2022 – Feb 13, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover bpf: Add more information into bpffs

We have a bpf manager (bpfman) running on the host to maintain the lifecycle of all the bpf progs and maps on this host.

The purpose of the bpfman includes,
- pin and unpin bpf progs and maps
- attach and detach bpf progs
- update running bpf progs and the related bpf maps
- update some running bpf progs only without updating the related bpf maps
- check the possible conflict of bpf progs
- etc

In order to safely do these works, we used many tools to get the required information, like bpftool and some other customized tools. Some of these works seem a little heavy. After the preloaded progs.debug and maps.debug are introduced, we decided to improve these two files to get the full view of progs and maps that required by bpfman.

This patchset includes below features,
- the pinned file of progs and maps are introduced into these two files
- the relateship of progs and maps is introduced into progs.debug

With these features we may also improve how bpftool get the pinned file[1] in the next step.

[1]. https://github.com/torvalds/linux/blob/master/tools/bpf/bpftool/prog.c#L648

Yafang Shao (4):
bpf: Add pin_name into struct bpf_prog_aux
bpf: Add pin_name into struct bpf_map
bpf: show pinned file name in {progs, maps}.debug
bpf: Show the used maps of each prog in progs.debug

 include/linux/bpf.h                           |   3 +
 include/uapi/linux/bpf.h                      |   1 +
 kernel/bpf/inode.c                            |  29 +-
 kernel/bpf/preload/iterators/iterators.bpf.c  |  33 +-
 kernel/bpf/preload/iterators/iterators.skel.h | 676 +++++++++++++++-----------
 5 files changed, 460 insertions(+), 282 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/4 bpf: Add pin_name into struct bpf_prog_aux

A new member pin_name is added into struct bpf_prog_aux, which will be set when the prog is set and cleared when the pinned file is removed.

---
 include/linux/bpf.h      |  2 ++
 include/uapi/linux/bpf.h |  1 +
 kernel/bpf/inode.c       | 20 +++++++++++++++++++-
 3 files changed, 22 insertions(+), 1 deletion(-)
include/linux/bpf.h +2 -0
index 0ceb25b..9cf8055 100644
@@ -933,6 +933,8 @@ struct bpf_prog_aux {
 		struct work_struct work;
 		struct rcu_head	rcu;
 	};
+
+	char pin_name[BPF_PIN_NAME_LEN];
 };
 
 struct bpf_array_aux {
include/uapi/linux/bpf.h +1 -0
index c14fed8..bada5cc 100644
@@ -1217,6 +1217,7 @@ struct bpf_stack_build_id {
 };
 
 #define BPF_OBJ_NAME_LEN 16U
+#define BPF_PIN_NAME_LEN 64U
 
 union bpf_attr {
 	struct { /* anonymous struct used by BPF_MAP_CREATE command */
kernel/bpf/inode.c +19 -1
index 4477ca8..f1a8811 100644
@@ -437,6 +437,8 @@ static int bpf_iter_link_pin_kernel(struct dentry *parent,
 static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 			  enum bpf_type type)
 {
+	struct bpf_prog_aux *aux;
+	struct bpf_prog *prog;
 	struct dentry *dentry;
 	struct inode *dir;
 	struct path path;
@@ -461,6 +463,10 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 
 	switch (type) {
 	case BPF_TYPE_PROG:
+		prog = raw;
+		aux = prog->aux;
+		(void) strncpy_from_user(aux->pin_name, pathname, BPF_PIN_NAME_LEN);
+		aux->pin_name[BPF_PIN_NAME_LEN - 1] = '\0';
 		ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
 		break;
 	case BPF_TYPE_MAP:
@@ -611,12 +617,24 @@ static int bpf_show_options(struct seq_file *m, struct dentry *root)
 
 static void bpf_free_inode(struct inode *inode)
 {
+	struct bpf_prog_aux *aux;
+	struct bpf_prog *prog;
 	enum bpf_type type;
 
 	if (S_ISLNK(inode->i_mode))
 		kfree(inode->i_link);
-	if (!bpf_inode_type(inode, &type))
+	if (!bpf_inode_type(inode, &type)) {
+		switch (type) {
+		case BPF_TYPE_PROG:
+			prog = inode->i_private;
+			aux = prog->aux;
+			aux->pin_name[0] = '\0';
+			break;
+		default:
+			break;
+		}
 		bpf_any_put(inode->i_private, type);
+	}
 	free_inode_nonrcu(inode);
 }
 
Signature · 1 line
1.8.3.1
maintainer
#2 lore raw

Re: [PATCH 1/4] bpf: Add pin_name into struct bpf_prog_aux

Quoted 15 lines · Yafang Shao

A new member pin_name is added into struct bpf_prog_aux, which will be set when the prog is set and cleared when the pinned file is removed.

---
  include/linux/bpf.h      |  2 ++
  include/uapi/linux/bpf.h |  1 +
  kernel/bpf/inode.c       | 20 +++++++++++++++++++-
  3 files changed, 22 insertions(+), 1 deletion(-)
include/linux/bpf.h +0 -0
index 0ceb25b..9cf8055 100644

include/linux/bpf.h struct bpf_prog_aux { · line 938

Quoted 6 lines
include/linux/bpf.h +2 -0
@@ -933,6 +933,8 @@ struct bpf_prog_aux {
  		struct work_struct work;
  		struct rcu_head	rcu;
  	};
+
+	char pin_name[BPF_PIN_NAME_LEN];
  };

I'm afraid this is not possible. You are assuming a 1:1 relationship between prog and pin location, but it's really a 1:n (prog can be pinned in multiple locations and also across multiple mount instances). Also, you can create hard links of pins which are not handled via bpf_obj_do_pin().

Quoted 64 lines
include/linux/bpf.h +0 -0
  struct bpf_array_aux {
include/uapi/linux/bpf.h +1 -0
index c14fed8..bada5cc 100644
@@ -1217,6 +1217,7 @@ struct bpf_stack_build_id {
  };
 
  #define BPF_OBJ_NAME_LEN 16U
+#define BPF_PIN_NAME_LEN 64U
 
  union bpf_attr {
  	struct { /* anonymous struct used by BPF_MAP_CREATE command */
kernel/bpf/inode.c +19 -1
index 4477ca8..f1a8811 100644
@@ -437,6 +437,8 @@ static int bpf_iter_link_pin_kernel(struct dentry *parent,
  static int bpf_obj_do_pin(const char __user *pathname, void *raw,
  			  enum bpf_type type)
  {
+	struct bpf_prog_aux *aux;
+	struct bpf_prog *prog;
  	struct dentry *dentry;
  	struct inode *dir;
  	struct path path;
@@ -461,6 +463,10 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 
  	switch (type) {
  	case BPF_TYPE_PROG:
+		prog = raw;
+		aux = prog->aux;
+		(void) strncpy_from_user(aux->pin_name, pathname, BPF_PIN_NAME_LEN);
+		aux->pin_name[BPF_PIN_NAME_LEN - 1] = '\0';
  		ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
  		break;
  	case BPF_TYPE_MAP:
@@ -611,12 +617,24 @@ static int bpf_show_options(struct seq_file *m, struct dentry *root)
 
  static void bpf_free_inode(struct inode *inode)
  {
+	struct bpf_prog_aux *aux;
+	struct bpf_prog *prog;
  	enum bpf_type type;
 
  	if (S_ISLNK(inode->i_mode))
  		kfree(inode->i_link);
-	if (!bpf_inode_type(inode, &type))
+	if (!bpf_inode_type(inode, &type)) {
+		switch (type) {
+		case BPF_TYPE_PROG:
+			prog = inode->i_private;
+			aux = prog->aux;
+			aux->pin_name[0] = '\0';
+			break;
+		default:
+			break;
+		}
  		bpf_any_put(inode->i_private, type);
+	}
  	free_inode_nonrcu(inode);
  }
 
maintainer
#3 lore raw

Re: [PATCH 1/4] bpf: Add pin_name into struct bpf_prog_aux

Quoted 11 lines · Daniel Borkmann
Quoted 10 lines · Yafang Shao

A new member pin_name is added into struct bpf_prog_aux, which will be set when the prog is set and cleared when the pinned file is removed.

---
  include/linux/bpf.h      |  2 ++ include/uapi/linux/bpf.h |  1 + kernel/bpf/inode.c       | 20 +++++++++++++++++++-

  3 files changed, 22 insertions(+), 1 deletion(-)
Quoted 16 lines

include/linux/bpf.h struct bpf_prog_aux { · line 938

Quoted 11 lines
include/linux/bpf.h +2 -0
index 0ceb25b..9cf8055 100644
@@ -933,6 +933,8 @@ struct bpf_prog_aux {
           struct work_struct work;
           struct rcu_head    rcu;
       };
+
+    char pin_name[BPF_PIN_NAME_LEN];
   };

I'm afraid this is not possible. You are assuming a 1:1 relationship between prog and pin location, but it's really a 1:n (prog can be pinned in multiple locations and also across multiple mount instances). Also, you can create hard links of pins which are not handled via bpf_obj_do_pin().

(Same is also true for BPF maps wrt patch 2.)

#4 lore raw

Re: [PATCH 1/4] bpf: Add pin_name into struct bpf_prog_aux

Quoted 30 lines · Daniel Borkmann
Quoted 11 lines · Daniel Borkmann
Quoted 10 lines · Yafang Shao

A new member pin_name is added into struct bpf_prog_aux, which will be set when the prog is set and cleared when the pinned file is removed.

---
  include/linux/bpf.h      |  2 ++
  include/uapi/linux/bpf.h |  1 +
  kernel/bpf/inode.c       | 20 +++++++++++++++++++-
  3 files changed, 22 insertions(+), 1 deletion(-)
Quoted 16 lines

include/linux/bpf.h struct bpf_prog_aux { · line 938

Quoted 11 lines
include/linux/bpf.h +2 -0
index 0ceb25b..9cf8055 100644
@@ -933,6 +933,8 @@ struct bpf_prog_aux {
          struct work_struct work;
          struct rcu_head    rcu;
      };
+
+    char pin_name[BPF_PIN_NAME_LEN];
  };

I'm afraid this is not possible. You are assuming a 1:1 relationship between prog and pin location, but it's really a 1:n (prog can be pinned in multiple locations and also across multiple mount instances). Also, you can create hard links of pins which are not handled via bpf_obj_do_pin().

Quoted 1 line

(Same is also true for BPF maps wrt patch 2.)

Thanks again for the reminder.

Signature · 2 lines
Thanks
Yafang
#5 lore raw

Re: [PATCH 1/4] bpf: Add pin_name into struct bpf_prog_aux

In reply to Daniel Borkmann
Quoted 12 lines · Daniel Borkmann
Quoted 10 lines · Yafang Shao

A new member pin_name is added into struct bpf_prog_aux, which will be set when the prog is set and cleared when the pinned file is removed.

---
  include/linux/bpf.h      |  2 ++
  include/uapi/linux/bpf.h |  1 +
  kernel/bpf/inode.c       | 20 +++++++++++++++++++-
  3 files changed, 22 insertions(+), 1 deletion(-)
Quoted 15 lines

include/linux/bpf.h struct bpf_prog_aux { · line 938

Quoted 11 lines
include/linux/bpf.h +2 -0
index 0ceb25b..9cf8055 100644
@@ -933,6 +933,8 @@ struct bpf_prog_aux {
              struct work_struct work;
              struct rcu_head rcu;
      };
+
+     char pin_name[BPF_PIN_NAME_LEN];
  };

I'm afraid this is not possible. You are assuming a 1:1 relationship between prog and pin location, but it's really a 1:n (prog can be pinned in multiple locations and also across multiple mount instances).

Thanks for the explanation!
I didn't notice the 1:n relationship before. I will read the code more to try to find if there's a good way to implement it.

Quoted 3 lines

Also, you can create hard links of pins
which are not handled via bpf_obj_do_pin().

Yes, it seems we should introduce bpf_{link, unlink, rename} first instead of the simple_* one.

Quoted 65 lines
Quoted 64 lines
include/linux/bpf.h +0 -0
  struct bpf_array_aux {
include/uapi/linux/bpf.h +1 -0
index c14fed8..bada5cc 100644
@@ -1217,6 +1217,7 @@ struct bpf_stack_build_id {
  };
 
  #define BPF_OBJ_NAME_LEN 16U
+#define BPF_PIN_NAME_LEN 64U
 
  union bpf_attr {
      struct { /* anonymous struct used by BPF_MAP_CREATE command */
kernel/bpf/inode.c +19 -1
index 4477ca8..f1a8811 100644
@@ -437,6 +437,8 @@ static int bpf_iter_link_pin_kernel(struct dentry *parent,
  static int bpf_obj_do_pin(const char __user *pathname, void *raw,
                        enum bpf_type type)
  {
+     struct bpf_prog_aux *aux;
+     struct bpf_prog *prog;
      struct dentry *dentry;
      struct inode *dir;
      struct path path;
@@ -461,6 +463,10 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 
      switch (type) {
      case BPF_TYPE_PROG:
+             prog = raw;
+             aux = prog->aux;
+             (void) strncpy_from_user(aux->pin_name, pathname, BPF_PIN_NAME_LEN);
+             aux->pin_name[BPF_PIN_NAME_LEN - 1] = '\0';
              ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
              break;
      case BPF_TYPE_MAP:
@@ -611,12 +617,24 @@ static int bpf_show_options(struct seq_file *m, struct dentry *root)
 
  static void bpf_free_inode(struct inode *inode)
  {
+     struct bpf_prog_aux *aux;
+     struct bpf_prog *prog;
      enum bpf_type type;
 
      if (S_ISLNK(inode->i_mode))
              kfree(inode->i_link);
-     if (!bpf_inode_type(inode, &type))
+     if (!bpf_inode_type(inode, &type)) {
+             switch (type) {
+             case BPF_TYPE_PROG:
+                     prog = inode->i_private;
+                     aux = prog->aux;
+                     aux->pin_name[0] = '\0';
+                     break;
+             default:
+                     break;
+             }
              bpf_any_put(inode->i_private, type);
+     }
      free_inode_nonrcu(inode);
  }
 
Signature · 2 lines
Thanks
Yafang
#6 lore raw

Re: [PATCH 1/4] bpf: Add pin_name into struct bpf_prog_aux

In reply to Yafang Shao
Thank you for the patch! Perhaps something to improve:

Hi Yafang,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf/master]
[also build test WARNING on net/master horms-ipvs/master net-next/master v5.17-rc3 next-20220211] [cannot apply to bpf-next/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Yafang-Shao/bpf-Add-more-information-into-bpffs/20220211-201319
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
config: arc-allyesconfig (https://download.01.org/0day-ci/archive/20220211/202202112213.WGiJCCYD-lkp(a)intel.com/config)
compiler: arceb-elf-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/6cd35bc70f99caee380d84f5ba9256ac5fe03860
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Yafang-Shao/bpf-Add-more-information-into-bpffs/20220211-201319 git checkout 6cd35bc70f99caee380d84f5ba9256ac5fe03860
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arc SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

kernel/bpf/inode.c: In function 'bpf_obj_do_pin':

>> kernel/bpf/inode.c:469:24: warning: ignoring return value of 'strncpy_from_user' declared with attribute 'warn_unused_result' [-Wunused-result]
     469 |                 (void) strncpy_from_user(aux->pin_name, pathname, BPF_PIN_NAME_LEN);
         |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +469 kernel/bpf/inode.c

437
438 static int bpf_obj_do_pin(const char __user *pathname, void *raw,
439 enum bpf_type type)
440 {
441 struct bpf_prog_aux *aux;
442 struct bpf_prog *prog;
443 struct dentry *dentry;
444 struct inode *dir;
445 struct path path;
446 umode_t mode;
447 int ret;
448
449 dentry = user_path_create(AT_FDCWD, pathname, &path, 0);
450 if (IS_ERR(dentry))
451 return PTR_ERR(dentry);
452
453 mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
454
455 ret = security_path_mknod(&path, dentry, mode, 0);
456 if (ret)
457 goto out;
458
459 dir = d_inode(path.dentry);
460 if (dir->i_op != &bpf_dir_iops) {
461 ret = -EPERM;
462 goto out;
463 }
464
465 switch (type) {
466 case BPF_TYPE_PROG:
467 prog = raw;
468 aux = prog->aux;

 > 469			(void) strncpy_from_user(aux->pin_name, pathname, BPF_PIN_NAME_LEN);

470 aux->pin_name[BPF_PIN_NAME_LEN - 1] = '\0';
471 ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
472 break;
473 case BPF_TYPE_MAP:
474 ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw);
475 break;
476 case BPF_TYPE_LINK:
477 ret = vfs_mkobj(dentry, mode, bpf_mklink, raw);
478 break;
479 default:
480 ret = -EPERM;
481 }
482 out:
483 done_path_create(&path, dentry);
484 return ret;
485 }
486

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

Re: [PATCH 1/4] bpf: Add pin_name into struct bpf_prog_aux

Quoted 28 lines · kernel test robot

Hi Yafang,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf/master]
[also build test WARNING on net/master horms-ipvs/master net-next/master v5.17-rc3 next-20220211] [cannot apply to bpf-next/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Yafang-Shao/bpf-Add-more-information-into-bpffs/20220211-201319
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master
config: arc-allyesconfig (https://download.01.org/0day-ci/archive/20220211/202202112213.WGiJCCYD-lkp(a)intel.com/config)
compiler: arceb-elf-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/6cd35bc70f99caee380d84f5ba9256ac5fe03860
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Yafang-Shao/bpf-Add-more-information-into-bpffs/20220211-201319 git checkout 6cd35bc70f99caee380d84f5ba9256ac5fe03860
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arc SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag as appropriate

Quoted 9 lines

All warnings (new ones prefixed by >>):

kernel/bpf/inode.c: In function 'bpf_obj_do_pin':

Quoted 1 line
Quoted 1 line

kernel/bpf/inode.c:469:24: warning: ignoring return value of 'strncpy_from_user' declared with attribute 'warn_unused_result' [-Wunused-result]

     469 |                 (void) strncpy_from_user(aux->pin_name, pathname, BPF_PIN_NAME_LEN);
         |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks for the report.
I will improve it to avoid this warning.

Quoted 57 lines

vim +469 kernel/bpf/inode.c

   437
   438  static int bpf_obj_do_pin(const char __user *pathname, void *raw,
   439                            enum bpf_type type)
   440  {
   441          struct bpf_prog_aux *aux;
   442          struct bpf_prog *prog;
   443          struct dentry *dentry;
   444          struct inode *dir;
   445          struct path path;
   446          umode_t mode;
   447          int ret;
   448
   449          dentry = user_path_create(AT_FDCWD, pathname, &path, 0);
   450          if (IS_ERR(dentry))
   451                  return PTR_ERR(dentry);
   452
   453          mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
   454
   455          ret = security_path_mknod(&path, dentry, mode, 0);
   456          if (ret)
   457                  goto out;
   458
   459          dir = d_inode(path.dentry);
   460          if (dir->i_op != &bpf_dir_iops) {
   461                  ret = -EPERM;
   462                  goto out;
   463          }
   464
   465          switch (type) {
   466          case BPF_TYPE_PROG:
   467                  prog = raw;
   468                  aux = prog->aux;
Quoted 1 line

469 (void) strncpy_from_user(aux->pin_name, pathname, BPF_PIN_NAME_LEN);

   470                  aux->pin_name[BPF_PIN_NAME_LEN - 1] = '\0';
   471                  ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
   472                  break;
   473          case BPF_TYPE_MAP:
   474                  ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw);
   475                  break;
   476          case BPF_TYPE_LINK:
   477                  ret = vfs_mkobj(dentry, mode, bpf_mklink, raw);
   478                  break;
   479          default:
   480                  ret = -EPERM;
   481          }
   482  out:
   483          done_path_create(&path, dentry);
   484          return ret;
   485  }
   486

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

Signature · 2 lines
Thanks
Yafang
#8 lore raw

PATCH 2/4 bpf: Add pin_name into struct bpf_map

In reply to Yafang Shao

A new member pin_name is added into struct bpf_map, which will set when the map is pinned and cleared when the pinned file is removed.

---
 include/linux/bpf.h | 1 +
 kernel/bpf/inode.c  | 9 +++++++++
 2 files changed, 10 insertions(+)
include/linux/bpf.h +1 -0
index 9cf8055..220de69 100644
@@ -182,6 +182,7 @@ struct bpf_map {
 	struct mem_cgroup *memcg;
 #endif
 	char name[BPF_OBJ_NAME_LEN];
+	char pin_name[BPF_PIN_NAME_LEN];
 	bool bypass_spec_v1;
 	bool frozen; /* write-once; write-protected by freeze_mutex */
 	/* 14 bytes hole */
kernel/bpf/inode.c +9 -0
index f1a8811..586adef 100644
@@ -440,6 +440,7 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 	struct bpf_prog_aux *aux;
 	struct bpf_prog *prog;
 	struct dentry *dentry;
+	struct bpf_map *map;
 	struct inode *dir;
 	struct path path;
 	umode_t mode;
@@ -470,6 +471,9 @@ static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 		ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
 		break;
 	case BPF_TYPE_MAP:
+		map = raw;
+		(void) strncpy_from_user(map->pin_name, pathname, BPF_PIN_NAME_LEN);
+		map->pin_name[BPF_PIN_NAME_LEN - 1] = '\0';
 		ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw);
 		break;
 	case BPF_TYPE_LINK:
@@ -619,6 +623,7 @@ static void bpf_free_inode(struct inode *inode)
 {
 	struct bpf_prog_aux *aux;
 	struct bpf_prog *prog;
+	struct bpf_map *map;
 	enum bpf_type type;
 
 	if (S_ISLNK(inode->i_mode))
@@ -630,6 +635,10 @@ static void bpf_free_inode(struct inode *inode)
 			aux = prog->aux;
 			aux->pin_name[0] = '\0';
 			break;
+		case BPF_TYPE_MAP:
+			map = inode->i_private;
+			map->pin_name[0] = '\0';
+			break;
 		default:
 			break;
 		}
Signature · 1 line
1.8.3.1
#9 lore raw

PATCH 3/4 bpf: Show pinned file name in {progs, maps}.debug

In reply to Yafang Shao

If the progs or maps are pinned into bpffs, the pinned file name can be displayed in progs.debug or maps.debug. It will help us to maintain the lifecycle of pinned maps and progs.

The result as follows,

$ cat progs.debug
  id name             attached         pinned
  19 dump_bpf_map     bpf_iter_bpf_map
  21 dump_bpf_prog    bpf_iter_bpf_prog
  22                                   /var/run/pnc/bpf/tc/prog/cls-ingress
  23                                   /var/run/pnc/bpf/tc/prog/cls-egress
  24                                   /var/run/pnc/bpf/tc/prog/connect4
  25                                   /var/run/pnc/bpf/tc/prog/sockops
  26                                   /var/run/pnc/bpf/tc/prog/skmsg
$ cat maps.debug
  id name             max_entries  pinned
  13 iterator.rodata  1
  14 .rodata.str1.1   1
  16                  32           /var/run/pnc/bpf//tc//globals/ctrl
  17                  256          /var/run/pnc/bpf//tc//globals/events
  18                  102400       /var/run/pnc/bpf//tc//globals/egw_stat
  19                  1024         /var/run/pnc/bpf//tc//globals/egw_config
  20                  1024         /var/run/pnc/bpf//tc//globals/egw_mapping
  21                  102400       /var/run/pnc/bpf//tc//globals/egw_sock_info
  22                  0            /var/run/pnc/bpf//tc//globals/egw_sk_storage
  23                  102400       /var/run/pnc/bpf//tc//globals/egw_sock
  24                  1048576      /var/run/pnc/bpf//tc//globals/ip_identity
  25                  1024         /var/run/pnc/bpf//tc//globals/metrics
  26                  1024         /var/run/pnc/bpf//tc//globals/ns_ctrl
  27                  262144       /var/run/pnc/bpf//tc//globals/identity_ctrl
  28                  4194304      /var/run/pnc/bpf//tc//globals/rule
  29                  16384        /var/run/pnc/bpf//tc//globals/rgroup_rule
  30                  1048576      /var/run/pnc/bpf//tc//globals/rgroup_ref
  31                  102400       /var/run/pnc/bpf//tc//globals/policy_stats
  32                  102400       /var/run/pnc/bpf//tc//globals/lazy_record
  33                  102400       /var/run/pnc/bpf//tc//globals/flow_stats
  34                  1024         /var/run/pnc/bpf//tc//globals/fs_filter_raddr
  35                  1024         /var/run/pnc/bpf//tc//globals/fs_filter_l4addr

The pinned name in these two files will be disappeared if the pinned file is removed.

---
 kernel/bpf/preload/iterators/iterators.bpf.c  |  16 +-
 kernel/bpf/preload/iterators/iterators.skel.h | 599 ++++++++++++++------------
 2 files changed, 334 insertions(+), 281 deletions(-)
kernel/bpf/preload/iterators/iterators.bpf.c +11 -5
index 03af863..cf6b2c0 100644
@@ -16,6 +16,7 @@ struct bpf_map {
 	__u32 id;
 	char name[16];
 	__u32 max_entries;
+	char pin_name[64];
 };
 
 struct bpf_iter__bpf_map {
@@ -44,6 +45,7 @@ struct bpf_prog_aux {
 	struct bpf_prog *dst_prog;
 	struct bpf_func_info *func_info;
 	struct btf *btf;
+	char pin_name[64];
 };
 
 struct bpf_prog {
@@ -84,9 +86,11 @@ int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
 		return 0;
 
 	if (seq_num == 0)
-		BPF_SEQ_PRINTF(seq, "  id name             max_entries\n");
+		BPF_SEQ_PRINTF(seq, "%4s %-16s %-12s %s\n",
+						"id", "name", "max_entries", "pinned");
 
-	BPF_SEQ_PRINTF(seq, "%4u %-16s%6d\n", map->id, map->name, map->max_entries);
+	BPF_SEQ_PRINTF(seq, "%4u %-16s %-12d %s\n",
+					map->id, map->name, map->max_entries, map->pin_name);
 	return 0;
 }
 
@@ -103,11 +107,13 @@ int dump_bpf_prog(struct bpf_iter__bpf_prog *ctx)
 
 	aux = prog->aux;
 	if (seq_num == 0)
-		BPF_SEQ_PRINTF(seq, "  id name             attached\n");
+		BPF_SEQ_PRINTF(seq, "%4s %-16s %-16s %s\n",
+						"id", "name", "attached", "pinned");
 
-	BPF_SEQ_PRINTF(seq, "%4u %-16s %s %s\n", aux->id,
+	BPF_SEQ_PRINTF(seq, "%4u %-16s %-16s %s %s\n", aux->id,
 		       get_name(aux->btf, aux->func_info[0].type_id, aux->name),
-		       aux->attach_func_name, aux->dst_prog->aux->name);
+		       aux->attach_func_name, aux->pin_name,
+			   aux->dst_prog->aux->name);
 	return 0;
 }
 char LICENSE[] SEC("license") = "GPL";
kernel/bpf/preload/iterators/iterators.skel.h +323 -276

675 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#10 lore raw

PATCH 4/4 bpf: Show the used maps of each prog in progs.debug

In reply to Yafang Shao

We can get bpf maps via maps.debug, and bpf progs via progs.debug, but we don't know the relationship of these progs and maps. Let show the used maps of each prog in progs.debug.

The result as follows,

$ cat progs.debug
  id name             attached         pinned
  19 dump_bpf_map     bpf_iter_bpf_map
    maps: 14 13
  21 dump_bpf_prog    bpf_iter_bpf_prog
    maps: 14 13
  36                                   /var/run/pnc/bpf/tc/prog/cls-ingress
    maps: 26 35 27 45 44 43 34 36 41 37 38 42
  37                                   /var/run/pnc/bpf/tc/prog/cls-egress
    maps: 26 35 27 32 45 44 28 43 34 36 41 37 38 42
  38                                   /var/run/pnc/bpf/tc/prog/connect4
    maps: 26 35 34 30 29 32 28
  39                                   /var/run/pnc/bpf/tc/prog/sockops
    maps: 35 32 28 33 31
  40                                   /var/run/pnc/bpf/tc/prog/skmsg
    maps: 35 33 31
---
 kernel/bpf/preload/iterators/iterators.bpf.c  |  17 +
 kernel/bpf/preload/iterators/iterators.skel.h | 587 +++++++++++++++-----------
 2 files changed, 349 insertions(+), 255 deletions(-)
kernel/bpf/preload/iterators/iterators.bpf.c +17 -0
index cf6b2c0..e4c273d 100644
@@ -46,6 +46,8 @@ struct bpf_prog_aux {
 	struct bpf_func_info *func_info;
 	struct btf *btf;
 	char pin_name[64];
+	__u32 used_map_cnt;
+	struct bpf_map **used_maps;
 };
 
 struct bpf_prog {
@@ -101,6 +103,9 @@ int dump_bpf_prog(struct bpf_iter__bpf_prog *ctx)
 	__u64 seq_num = ctx->meta->seq_num;
 	struct bpf_prog *prog = ctx->prog;
 	struct bpf_prog_aux *aux;
+	struct bpf_map *map;
+	__u32 id;
+	__u32 i;
 
 	if (!prog)
 		return 0;
@@ -114,6 +119,18 @@ int dump_bpf_prog(struct bpf_iter__bpf_prog *ctx)
 		       get_name(aux->btf, aux->func_info[0].type_id, aux->name),
 		       aux->attach_func_name, aux->pin_name,
 			   aux->dst_prog->aux->name);
+
+	if (!aux->used_map_cnt)
+		return 0;
+
+	BPF_SEQ_PRINTF(seq, "    maps:");
+	for (i = 0; i < aux->used_map_cnt && i < 64; i++) {
+		bpf_probe_read_kernel(&map, sizeof(map), aux->used_maps + i);
+		id = BPF_CORE_READ(map, id);
+		BPF_SEQ_PRINTF(seq, " %u", id);
+	}
+	BPF_SEQ_PRINTF(seq, "\n");
+
 	return 0;
 }
 char LICENSE[] SEC("license") = "GPL";
kernel/bpf/preload/iterators/iterators.skel.h +332 -255

618 lines not on this page · raw message

Signature · 1 line
1.8.3.1