Re: [RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections

2015-05-27 Thread Alexei Starovoitov
On Thu, May 28, 2015 at 11:34:37AM +0800, Wangnan (F) wrote:
> >...
> >>+static int
> >>+bpf_object__init_kversion(struct bpf_object *obj,
> >>+ void *data, size_t size)
> >>+{
> >>+   u32 kver;
> >>+   if (size < sizeof(kver)) {
> >shouldn't it be '!=' ?
> 
> Is it possible that LLVM pads 'version' section so the size of it becomes
> longer than sizeof(u32)?

llvm will not be padding a section unless user asks for it.
Try objdump on *_kern.o

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections

2015-05-27 Thread Wangnan (F)



On 2015/5/28 9:48, Alexei Starovoitov wrote:

On Wed, May 27, 2015 at 05:19:44AM +, Wang Nan wrote:

Expand bpf_obj_elf_collect() to collect license and kernel version
information in eBPF object file. eBPF object file should have a section
named 'license', which contains a string. It should also have a section
named 'version', contains a u32 LINUX_VERSION_CODE.

bpf_obj_validate() is introduced to validate object file after loaded.
Currently it only check existance of 'version' section.

Signed-off-by: Wang Nan 
---

...

+static int
+bpf_object__init_kversion(struct bpf_object *obj,
+ void *data, size_t size)
+{
+   u32 kver;
+   if (size < sizeof(kver)) {

shouldn't it be '!=' ?


Is it possible that LLVM pads 'version' section so the size of it becomes
longer than sizeof(u32)?

Thank you.


+   pr_warning("invalid kver section in %s\n", obj->path);
+   return -EINVAL;
+   }
+   memcpy(, data, sizeof(kver));
+   obj->kern_version = kver;
+   pr_debug("kernel version of %s is %x\n", obj->path,
+obj->kern_version);
+   return 0;
+}
+



--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections

2015-05-27 Thread Alexei Starovoitov
On Wed, May 27, 2015 at 05:19:44AM +, Wang Nan wrote:
> Expand bpf_obj_elf_collect() to collect license and kernel version
> information in eBPF object file. eBPF object file should have a section
> named 'license', which contains a string. It should also have a section
> named 'version', contains a u32 LINUX_VERSION_CODE.
> 
> bpf_obj_validate() is introduced to validate object file after loaded.
> Currently it only check existance of 'version' section.
> 
> Signed-off-by: Wang Nan 
> ---
...
> +static int
> +bpf_object__init_kversion(struct bpf_object *obj,
> +   void *data, size_t size)
> +{
> + u32 kver;
> + if (size < sizeof(kver)) {

shouldn't it be '!=' ?

> + pr_warning("invalid kver section in %s\n", obj->path);
> + return -EINVAL;
> + }
> + memcpy(, data, sizeof(kver));
> + obj->kern_version = kver;
> + pr_debug("kernel version of %s is %x\n", obj->path,
> +  obj->kern_version);
> + return 0;
> +}
> +
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections

2015-05-27 Thread Wangnan (F)



On 2015/5/28 9:48, Alexei Starovoitov wrote:

On Wed, May 27, 2015 at 05:19:44AM +, Wang Nan wrote:

Expand bpf_obj_elf_collect() to collect license and kernel version
information in eBPF object file. eBPF object file should have a section
named 'license', which contains a string. It should also have a section
named 'version', contains a u32 LINUX_VERSION_CODE.

bpf_obj_validate() is introduced to validate object file after loaded.
Currently it only check existance of 'version' section.

Signed-off-by: Wang Nan wangn...@huawei.com
---

...

+static int
+bpf_object__init_kversion(struct bpf_object *obj,
+ void *data, size_t size)
+{
+   u32 kver;
+   if (size  sizeof(kver)) {

shouldn't it be '!=' ?


Is it possible that LLVM pads 'version' section so the size of it becomes
longer than sizeof(u32)?

Thank you.


+   pr_warning(invalid kver section in %s\n, obj-path);
+   return -EINVAL;
+   }
+   memcpy(kver, data, sizeof(kver));
+   obj-kern_version = kver;
+   pr_debug(kernel version of %s is %x\n, obj-path,
+obj-kern_version);
+   return 0;
+}
+



--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections

2015-05-27 Thread Alexei Starovoitov
On Wed, May 27, 2015 at 05:19:44AM +, Wang Nan wrote:
 Expand bpf_obj_elf_collect() to collect license and kernel version
 information in eBPF object file. eBPF object file should have a section
 named 'license', which contains a string. It should also have a section
 named 'version', contains a u32 LINUX_VERSION_CODE.
 
 bpf_obj_validate() is introduced to validate object file after loaded.
 Currently it only check existance of 'version' section.
 
 Signed-off-by: Wang Nan wangn...@huawei.com
 ---
...
 +static int
 +bpf_object__init_kversion(struct bpf_object *obj,
 +   void *data, size_t size)
 +{
 + u32 kver;
 + if (size  sizeof(kver)) {

shouldn't it be '!=' ?

 + pr_warning(invalid kver section in %s\n, obj-path);
 + return -EINVAL;
 + }
 + memcpy(kver, data, sizeof(kver));
 + obj-kern_version = kver;
 + pr_debug(kernel version of %s is %x\n, obj-path,
 +  obj-kern_version);
 + return 0;
 +}
 +
--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections

2015-05-27 Thread Alexei Starovoitov
On Thu, May 28, 2015 at 11:34:37AM +0800, Wangnan (F) wrote:
 ...
 +static int
 +bpf_object__init_kversion(struct bpf_object *obj,
 + void *data, size_t size)
 +{
 +   u32 kver;
 +   if (size  sizeof(kver)) {
 shouldn't it be '!=' ?
 
 Is it possible that LLVM pads 'version' section so the size of it becomes
 longer than sizeof(u32)?

llvm will not be padding a section unless user asks for it.
Try objdump on *_kern.o

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections

2015-05-26 Thread Wang Nan
Expand bpf_obj_elf_collect() to collect license and kernel version
information in eBPF object file. eBPF object file should have a section
named 'license', which contains a string. It should also have a section
named 'version', contains a u32 LINUX_VERSION_CODE.

bpf_obj_validate() is introduced to validate object file after loaded.
Currently it only check existance of 'version' section.

Signed-off-by: Wang Nan 
---
 tools/lib/bpf/libbpf.c | 52 ++
 1 file changed, 52 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 16e47a3..15525ad 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -14,6 +14,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -81,6 +82,8 @@ void libbpf_set_print(int (*warn)(const char *format, ...),
 #endif
 
 struct bpf_object {
+   char license[64];
+   u32 kern_version;
/*
 * Information when doing elf related work. Only valid if fd
 * is valid.
@@ -197,6 +200,32 @@ mismatch:
return -EINVAL;
 }
 
+static int
+bpf_object__init_license(struct bpf_object *obj,
+void *data, size_t size)
+{
+   memcpy(obj->license, data,
+  min(size, sizeof(obj->license) - 1));
+   pr_debug("license of %s is %s\n", obj->path, obj->license);
+   return 0;
+}
+
+static int
+bpf_object__init_kversion(struct bpf_object *obj,
+ void *data, size_t size)
+{
+   u32 kver;
+   if (size < sizeof(kver)) {
+   pr_warning("invalid kver section in %s\n", obj->path);
+   return -EINVAL;
+   }
+   memcpy(, data, sizeof(kver));
+   obj->kern_version = kver;
+   pr_debug("kernel version of %s is %x\n", obj->path,
+obj->kern_version);
+   return 0;
+}
+
 static int bpf_object__elf_collect(struct bpf_object *obj)
 {
Elf *elf = obj->efile.elf;
@@ -243,11 +272,32 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 name, (unsigned long)data->d_size,
 (int)sh.sh_link, (unsigned long)sh.sh_flags,
 (int)sh.sh_type);
+
+   if (strcmp(name, "license") == 0)
+   err = bpf_object__init_license(obj,
+  data->d_buf,
+  data->d_size);
+   else if (strcmp(name, "version") == 0)
+   err = bpf_object__init_kversion(obj,
+   data->d_buf,
+   data->d_size);
+   if (err)
+   goto out;
}
 out:
return err;
 }
 
+static int bpf_object__validate(struct bpf_object *obj)
+{
+   if (obj->kern_version == 0) {
+   pr_warning("%s doesn't provide kernel version\n",
+  obj->path);
+   return -EINVAL;
+   }
+   return 0;
+}
+
 struct bpf_object *bpf_object__open(const char *path)
 {
struct bpf_object *obj;
@@ -273,6 +323,8 @@ struct bpf_object *bpf_object__open(const char *path)
goto out;
if (bpf_object__elf_collect(obj))
goto out;
+   if (bpf_object__validate(obj))
+   goto out;
 
bpf_object__elf_finish(obj);
return obj;
-- 
1.8.3.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[RFC PATCH v4 09/29] bpf tools: Collect version and license from ELF sections

2015-05-26 Thread Wang Nan
Expand bpf_obj_elf_collect() to collect license and kernel version
information in eBPF object file. eBPF object file should have a section
named 'license', which contains a string. It should also have a section
named 'version', contains a u32 LINUX_VERSION_CODE.

bpf_obj_validate() is introduced to validate object file after loaded.
Currently it only check existance of 'version' section.

Signed-off-by: Wang Nan wangn...@huawei.com
---
 tools/lib/bpf/libbpf.c | 52 ++
 1 file changed, 52 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 16e47a3..15525ad 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -14,6 +14,7 @@
 #include fcntl.h
 #include errno.h
 #include asm/unistd.h
+#include linux/kernel.h
 #include linux/bpf.h
 #include libelf.h
 #include gelf.h
@@ -81,6 +82,8 @@ void libbpf_set_print(int (*warn)(const char *format, ...),
 #endif
 
 struct bpf_object {
+   char license[64];
+   u32 kern_version;
/*
 * Information when doing elf related work. Only valid if fd
 * is valid.
@@ -197,6 +200,32 @@ mismatch:
return -EINVAL;
 }
 
+static int
+bpf_object__init_license(struct bpf_object *obj,
+void *data, size_t size)
+{
+   memcpy(obj-license, data,
+  min(size, sizeof(obj-license) - 1));
+   pr_debug(license of %s is %s\n, obj-path, obj-license);
+   return 0;
+}
+
+static int
+bpf_object__init_kversion(struct bpf_object *obj,
+ void *data, size_t size)
+{
+   u32 kver;
+   if (size  sizeof(kver)) {
+   pr_warning(invalid kver section in %s\n, obj-path);
+   return -EINVAL;
+   }
+   memcpy(kver, data, sizeof(kver));
+   obj-kern_version = kver;
+   pr_debug(kernel version of %s is %x\n, obj-path,
+obj-kern_version);
+   return 0;
+}
+
 static int bpf_object__elf_collect(struct bpf_object *obj)
 {
Elf *elf = obj-efile.elf;
@@ -243,11 +272,32 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 name, (unsigned long)data-d_size,
 (int)sh.sh_link, (unsigned long)sh.sh_flags,
 (int)sh.sh_type);
+
+   if (strcmp(name, license) == 0)
+   err = bpf_object__init_license(obj,
+  data-d_buf,
+  data-d_size);
+   else if (strcmp(name, version) == 0)
+   err = bpf_object__init_kversion(obj,
+   data-d_buf,
+   data-d_size);
+   if (err)
+   goto out;
}
 out:
return err;
 }
 
+static int bpf_object__validate(struct bpf_object *obj)
+{
+   if (obj-kern_version == 0) {
+   pr_warning(%s doesn't provide kernel version\n,
+  obj-path);
+   return -EINVAL;
+   }
+   return 0;
+}
+
 struct bpf_object *bpf_object__open(const char *path)
 {
struct bpf_object *obj;
@@ -273,6 +323,8 @@ struct bpf_object *bpf_object__open(const char *path)
goto out;
if (bpf_object__elf_collect(obj))
goto out;
+   if (bpf_object__validate(obj))
+   goto out;
 
bpf_object__elf_finish(obj);
return obj;
-- 
1.8.3.4

--
To unsubscribe from this list: send the line unsubscribe linux-kernel in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/