[bpf-next 1/3] sample/bpf: Avoid repetitive definition of log2 func

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



log2 func is defined and used in following three files:
    samples/bpf/lathist_kern.c
    samples/bpf/lwt_len_hist_kern.c
    samples/bpf/tracex2_kern.c

There's no need to repeat define them many times, so i added
a "common.h" file which maintains common codes, you just need
to include it in your file and future we can put more common codes
into this file.

Signed-off-by: Xin Hao <xhao@xxxxxxxxxxxxxxxxx>
---
 samples/bpf/common_kern.h       | 30 ++++++++++++++++++++++++++++++
 samples/bpf/lathist_kern.c      | 25 +------------------------
 samples/bpf/lwt_len_hist_kern.c | 23 +----------------------
 samples/bpf/tracex2_kern.c      | 23 +----------------------
 4 files changed, 33 insertions(+), 68 deletions(-)
 create mode 100644 samples/bpf/common_kern.h

diff --git a/samples/bpf/common_kern.h b/samples/bpf/common_kern.h
new file mode 100644
index 000000000000..c23b44238d02
--- /dev/null
+++ b/samples/bpf/common_kern.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ */
+
+static unsigned int log2(unsigned int v)
+{
+	unsigned int r;
+	unsigned int shift;
+
+	r = (v > 0xFFFF) << 4; v >>= r;
+	shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
+	shift = (v > 0xF) << 2; v >>= shift; r |= shift;
+	shift = (v > 0x3) << 1; v >>= shift; r |= shift;
+	r |= (v >> 1);
+
+	return r;
+}
+
+static unsigned int log2l(unsigned long v)
+{
+	unsigned int hi = v >> 32;
+
+	if (hi)
+		return log2(hi) + 32;
+	else
+		return log2(v);
+}
diff --git a/samples/bpf/lathist_kern.c b/samples/bpf/lathist_kern.c
index ca9c2e4e69aa..0978e24dd01c 100644
--- a/samples/bpf/lathist_kern.c
+++ b/samples/bpf/lathist_kern.c
@@ -9,6 +9,7 @@
 #include <linux/ptrace.h>
 #include <uapi/linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include "common_kern.h"
 
 #define MAX_ENTRIES	20
 #define MAX_CPU		4
@@ -37,30 +38,6 @@ int bpf_prog1(struct pt_regs *ctx)
 	return 0;
 }
 
-static unsigned int log2(unsigned int v)
-{
-	unsigned int r;
-	unsigned int shift;
-
-	r = (v > 0xFFFF) << 4; v >>= r;
-	shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
-	shift = (v > 0xF) << 2; v >>= shift; r |= shift;
-	shift = (v > 0x3) << 1; v >>= shift; r |= shift;
-	r |= (v >> 1);
-
-	return r;
-}
-
-static unsigned int log2l(unsigned long v)
-{
-	unsigned int hi = v >> 32;
-
-	if (hi)
-		return log2(hi) + 32;
-	else
-		return log2(v);
-}
-
 struct bpf_map_def SEC("maps") my_lat = {
 	.type = BPF_MAP_TYPE_ARRAY,
 	.key_size = sizeof(int),
diff --git a/samples/bpf/lwt_len_hist_kern.c b/samples/bpf/lwt_len_hist_kern.c
index 9ed63e10e170..6e61072d602b 100644
--- a/samples/bpf/lwt_len_hist_kern.c
+++ b/samples/bpf/lwt_len_hist_kern.c
@@ -15,6 +15,7 @@
 #include <uapi/linux/ip.h>
 #include <uapi/linux/in.h>
 #include <bpf/bpf_helpers.h>
+#include "common_kern.h"
 
 # define printk(fmt, ...)						\
 		({							\
@@ -41,28 +42,6 @@ struct bpf_elf_map SEC("maps") lwt_len_hist_map = {
 	.max_elem = 1024,
 };
 
-static unsigned int log2(unsigned int v)
-{
-	unsigned int r;
-	unsigned int shift;
-
-	r = (v > 0xFFFF) << 4; v >>= r;
-	shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
-	shift = (v > 0xF) << 2; v >>= shift; r |= shift;
-	shift = (v > 0x3) << 1; v >>= shift; r |= shift;
-	r |= (v >> 1);
-	return r;
-}
-
-static unsigned int log2l(unsigned long v)
-{
-	unsigned int hi = v >> 32;
-	if (hi)
-		return log2(hi) + 32;
-	else
-		return log2(v);
-}
-
 SEC("len_hist")
 int do_len_hist(struct __sk_buff *skb)
 {
diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c
index 5bc696bac27d..7a8cda216d54 100644
--- a/samples/bpf/tracex2_kern.c
+++ b/samples/bpf/tracex2_kern.c
@@ -11,6 +11,7 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 #include "trace_common.h"
+#include "common_kern.h"
 
 struct {
 	__uint(type, BPF_MAP_TYPE_HASH);
@@ -42,28 +43,6 @@ int bpf_prog2(struct pt_regs *ctx)
 	return 0;
 }
 
-static unsigned int log2(unsigned int v)
-{
-	unsigned int r;
-	unsigned int shift;
-
-	r = (v > 0xFFFF) << 4; v >>= r;
-	shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
-	shift = (v > 0xF) << 2; v >>= shift; r |= shift;
-	shift = (v > 0x3) << 1; v >>= shift; r |= shift;
-	r |= (v >> 1);
-	return r;
-}
-
-static unsigned int log2l(unsigned long v)
-{
-	unsigned int hi = v >> 32;
-	if (hi)
-		return log2(hi) + 32;
-	else
-		return log2(v);
-}
-
 struct hist_key {
 	char comm[16];
 	u64 pid_tgid;
-- 
2.28.0




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux