bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: KP Singh <kpsingh@chromium.org>
To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org
Cc: Anton Protopopov <a.s.protopopov@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>, Florent Revest <revest@chromium.org>
Subject: [PATCH] tools: libbpf: Add bpf_object__open_buffer_xattr
Date: Fri, 27 Sep 2019 15:08:34 +0200	[thread overview]
Message-ID: <20190927130834.18829-1-kpsingh@chromium.org> (raw)

From: KP Singh <kpsingh@google.com>

Introduce struct bpf_object_open_buffer_attr and an API function,
bpf_object__open_xattr, as the existing API, bpf_object__open_buffer,
doesn't provide a way to specify neither the "needs_kver" nor
the "flags" parameter to the internal call to the
__bpf_object__open which makes it inconvenient for loading BPF
objects that do not require a kernel version from a buffer.

The flags attribute in the bpf_object_open_buffer_attr is set
to MAPS_RELAX_COMPAT when used in bpf_object__open_buffer to
maintain backward compatibility as this was added to load objects
with non-compat map definitions in:

commit c034a177d3c8 ("bpf: bpftool, add flag to allow non-compat map
		      definitions")

and bpf_object__open_buffer was called with this flag enabled (as a
boolean true value).

The existing "bpf_object__open_xattr" cannot be modified to
maintain API compatibility.

Reported-by: Anton Protopopov <a.s.protopopov@gmail.com>
Signed-off-by: KP Singh <kpsingh@google.com>
---
 tools/lib/bpf/libbpf.c   | 39 ++++++++++++++++++++++++++++-----------
 tools/lib/bpf/libbpf.h   | 10 ++++++++++
 tools/lib/bpf/libbpf.map |  5 +++++
 3 files changed, 43 insertions(+), 11 deletions(-)

This patch is assimilates the feedback from:

  https://lore.kernel.org/bpf/20190815000330.12044-1-a.s.protopopov@gmail.com/

I have added a "Reported-by:" tag, but please feel free to update to
"Co-developed-by" if it's more appropriate from an attribution perspective.

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 2b57d7ea7836..1f1f2e92832b 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -2752,25 +2752,42 @@ struct bpf_object *bpf_object__open(const char *path)
 	return bpf_object__open_xattr(&attr);
 }
 
-struct bpf_object *bpf_object__open_buffer(void *obj_buf,
-					   size_t obj_buf_sz,
-					   const char *name)
+struct bpf_object *
+bpf_object__open_buffer_xattr(struct bpf_object_open_buffer_attr *attr)
 {
 	char tmp_name[64];
 
 	/* param validation */
-	if (!obj_buf || obj_buf_sz <= 0)
-		return NULL;
+	if (!attr || !attr->obj_buf || !(attr->obj_buf_sz <= 0))
+		return ERR_PTR(-EINVAL);
 
-	if (!name) {
+	if (!attr->obj_name) {
 		snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
-			 (unsigned long)obj_buf,
-			 (unsigned long)obj_buf_sz);
-		name = tmp_name;
+			 (unsigned long)attr->obj_buf,
+			 (unsigned long)attr->obj_buf_sz);
+		attr->obj_name = tmp_name;
 	}
-	pr_debug("loading object '%s' from buffer\n", name);
+	pr_debug("loading object '%s' from buffer\n", attr->obj_name);
+
+	return __bpf_object__open(attr->obj_name, attr->obj_buf,
+				  attr->obj_buf_sz,
+				  bpf_prog_type__needs_kver(attr->prog_type),
+				  attr->flags);
+}
+
+struct bpf_object *bpf_object__open_buffer(void *obj_buf,
+					   size_t obj_buf_sz,
+					   const char *name)
+{
+	struct bpf_object_open_buffer_attr attr = {
+		.obj_name	= name,
+		.obj_buf	= obj_buf,
+		.obj_buf_sz	= obj_buf_sz,
+		.prog_type	= BPF_PROG_TYPE_UNSPEC,
+		.flags		=  MAPS_RELAX_COMPAT,
+	};
 
-	return __bpf_object__open(name, obj_buf, obj_buf_sz, true, true);
+	return bpf_object__open_buffer_xattr(&attr);
 }
 
 int bpf_object__unload(struct bpf_object *obj)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 5cbf459ece0b..ad0f5a263594 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -67,6 +67,14 @@ struct bpf_object_open_attr {
 	enum bpf_prog_type prog_type;
 };
 
+struct bpf_object_open_buffer_attr {
+	const char *obj_name;
+	void *obj_buf;
+	size_t obj_buf_sz;
+	enum bpf_prog_type prog_type;
+	int flags;
+};
+
 LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
 LIBBPF_API struct bpf_object *
 bpf_object__open_xattr(struct bpf_object_open_attr *attr);
@@ -75,6 +83,8 @@ struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
 LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
 						      size_t obj_buf_sz,
 						      const char *name);
+LIBBPF_API struct bpf_object *
+bpf_object__open_buffer_xattr(struct bpf_object_open_buffer_attr *attr);
 int bpf_object__section_size(const struct bpf_object *obj, const char *name,
 			     __u32 *size);
 int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index f9d316e873d8..4d5dcc58c73d 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -184,3 +184,8 @@ LIBBPF_0.0.4 {
 		perf_buffer__new_raw;
 		perf_buffer__poll;
 } LIBBPF_0.0.3;
+
+LIBBPF_0.0.5 {
+	global:
+		bpf_object__open_buffer_xattr;
+} LIBBPF_0.0.4;
-- 
2.20.1


             reply	other threads:[~2019-09-27 13:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-27 13:08 KP Singh [this message]
2019-09-27 16:06 ` Andrii Nakryiko
2019-09-30  7:12   ` Toke Høiland-Jørgensen
2019-09-30 13:24     ` KP Singh
2019-09-30 16:50     ` Andrii Nakryiko

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=20190927130834.18829-1-kpsingh@chromium.org \
    --to=kpsingh@chromium.org \
    --cc=a.s.protopopov@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=revest@chromium.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.com \
    --subject='Re: [PATCH] tools: libbpf: Add bpf_object__open_buffer_xattr' \
    /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

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).