netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wang Yufen <wangyufen@huawei.com>
To: <andrii@kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>,
	<martin.lau@linux.dev>, <song@kernel.org>, <yhs@fb.com>,
	<john.fastabend@gmail.com>, <kpsingh@kernel.org>,
	<sdf@google.com>, <haoluo@google.com>, <jolsa@kernel.org>,
	<paul.walmsley@sifive.com>, <palmer@dabbelt.com>,
	<aou@eecs.berkeley.edu>, <davem@davemloft.net>, <kuba@kernel.org>,
	<hawk@kernel.org>, <nathan@kernel.org>, <ndesaulniers@google.com>,
	<trix@redhat.com>
Cc: <bpf@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<netdev@vger.kernel.org>, <llvm@lists.linux.dev>
Subject: [bpf-next 1/2] libbpf: Add make_path_and_pin() helper for bpf_xxx__pin()
Date: Fri, 9 Sep 2022 13:45:45 +0800	[thread overview]
Message-ID: <1662702346-29665-1-git-send-email-wangyufen@huawei.com> (raw)

Move make path, check path and pin to the common helper make_path_and_pin() to make
the code simpler.

Signed-off-by: Wang Yufen <wangyufen@huawei.com>
---
 tools/lib/bpf/libbpf.c | 56 ++++++++++++++++++++++----------------------------
 1 file changed, 24 insertions(+), 32 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3ad1392..5854b92 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -7755,16 +7755,11 @@ static int check_path(const char *path)
 	return err;
 }
 
-int bpf_program__pin(struct bpf_program *prog, const char *path)
+static int make_path_and_pin(int fd, const char *path)
 {
 	char *cp, errmsg[STRERR_BUFSIZE];
 	int err;
 
-	if (prog->fd < 0) {
-		pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name);
-		return libbpf_err(-EINVAL);
-	}
-
 	err = make_parent_dir(path);
 	if (err)
 		return libbpf_err(err);
@@ -7773,12 +7768,27 @@ int bpf_program__pin(struct bpf_program *prog, const char *path)
 	if (err)
 		return libbpf_err(err);
 
-	if (bpf_obj_pin(prog->fd, path)) {
+	if (bpf_obj_pin(fd, path)) {
 		err = -errno;
 		cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
-		pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, cp);
+		pr_warn("failed to pin at '%s': %s\n", path, cp);
 		return libbpf_err(err);
 	}
+	return 0;
+}
+
+int bpf_program__pin(struct bpf_program *prog, const char *path)
+{
+	int err;
+
+	if (prog->fd < 0) {
+		pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name);
+		return libbpf_err(-EINVAL);
+	}
+
+	err = make_path_and_pin(prog->fd, path);
+	if (err)
+		return libbpf_err(err);
 
 	pr_debug("prog '%s': pinned at '%s'\n", prog->name, path);
 	return 0;
@@ -7838,32 +7848,20 @@ int bpf_map__pin(struct bpf_map *map, const char *path)
 		map->pin_path = strdup(path);
 		if (!map->pin_path) {
 			err = -errno;
-			goto out_err;
+			cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
+			pr_warn("failed to pin map: %s\n", cp);
+			return libbpf_err(err);
 		}
 	}
 
-	err = make_parent_dir(map->pin_path);
-	if (err)
-		return libbpf_err(err);
-
-	err = check_path(map->pin_path);
+	err = make_path_and_pin(map->fd, map->pin_path);
 	if (err)
 		return libbpf_err(err);
 
-	if (bpf_obj_pin(map->fd, map->pin_path)) {
-		err = -errno;
-		goto out_err;
-	}
-
 	map->pinned = true;
 	pr_debug("pinned map '%s'\n", map->pin_path);
 
 	return 0;
-
-out_err:
-	cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
-	pr_warn("failed to pin map: %s\n", cp);
-	return libbpf_err(err);
 }
 
 int bpf_map__unpin(struct bpf_map *map, const char *path)
@@ -9611,19 +9609,13 @@ int bpf_link__pin(struct bpf_link *link, const char *path)
 
 	if (link->pin_path)
 		return libbpf_err(-EBUSY);
-	err = make_parent_dir(path);
-	if (err)
-		return libbpf_err(err);
-	err = check_path(path);
-	if (err)
-		return libbpf_err(err);
 
 	link->pin_path = strdup(path);
 	if (!link->pin_path)
 		return libbpf_err(-ENOMEM);
 
-	if (bpf_obj_pin(link->fd, link->pin_path)) {
-		err = -errno;
+	err = make_path_and_pin(link->fd, path);
+	if (err) {
 		zfree(&link->pin_path);
 		return libbpf_err(err);
 	}
-- 
1.8.3.1


             reply	other threads:[~2022-09-09  5:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-09  5:45 Wang Yufen [this message]
2022-09-09  5:45 ` [bpf-next 2/2] libbpf: Add pathname_concat() helper Wang Yufen
2022-09-09 17:16   ` sdf
2022-09-13  2:55     ` wangyufen
2022-09-22  0:42     ` Andrii Nakryiko
2022-09-09 17:06 ` [bpf-next 1/2] libbpf: Add make_path_and_pin() helper for bpf_xxx__pin() sdf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1662702346-29665-1-git-send-email-wangyufen@huawei.com \
    --to=wangyufen@huawei.com \
    --cc=andrii@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=haoluo@google.com \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=martin.lau@linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=trix@redhat.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).