net: stmmac: Meson GXBB: attempting to execute userspace memory

2016-11-25 Thread Heinrich Schuchardt
For Odroid C2 I have compiled kernel
4.9.0-rc6-next-20161124-1-gbf7e142
with one additional patch
https://github.com/xypron/kernel-odroid-c2/blob/master/patch/0001-stmmac-RTL8211F-Meson-GXBB-TX-throughput-problems.patch

I repeatedly see faults like the one below:

[ 2557.400796] Unhandled fault: synchronous external abort (0x9210)
at 0x40001e8ee4b0
[ 2557.952413] CPU: 0 PID: 22837 Comm: cc1 Tainted: G  D
4.9.0-rc6-next-20161124-1-gbf7e142 #1
[ 2557.962062] Hardware name: Hardkernel ODROID-C2 (DT)
[ 2557.966980] task: 80006ddb7080 task.stack: 80006dd9c000
[ 2557.972846] PC is at 0x6a0d98
[ 2557.975776] LR is at 0x6a0e54
[ 2557.978709] pc : [<006a0d98>] lr : [<006a0e54>]
pstate: 8000
[ 2557.986040] sp : f3ee5f80
[ 2557.989318] x29: f3ee5f80 x28: 4b3f1240
[ 2557.994578] x27: 012a7000 x26: 4b3f1288
[ 2557.999840] x25: 00f58f88 x24: 4b3f1240
[ 2558.005101] x23:  x22: 0001
[ 2558.010362] x21: 0001 x20: 4b3f1250
[ 2558.015623] x19: 0054 x18: 0001
[ 2558.020885] x17: 48acaa10 x16: 01285050
[ 2558.026146] x15: 4ad96dc8 x14: 001f
[ 2558.031407] x13: 4b3f1270 x12: 4b3f1258
[ 2558.036668] x11: 01347000 x10: 0661
[ 2558.041930] x9 : 0005 x8 : 0003
[ 2558.047191] x7 : 4b3f1240 x6 : 20020033
[ 2558.052452] x5 : 4b402020 x4 : 4b3e1aa0
[ 2558.057713] x3 : 000c x2 : 0020
[ 2558.062974] x1 : 00f45000 x0 : 0065
[ 2558.068235]
[ 2558.069712] Internal error: Attempting to execute userspace memory:
860f [#7] PREEMPT SMP
[ 2558.078155] Modules linked in: meson_rng rng_core meson_gxbb_wdt
ip_tables x_tables ipv6 dwmac_generic realtek dwmac_meson8b
stmmac_platform stmmac
[ 2558.091267] CPU: 0 PID: 22837 Comm: cc1 Tainted: G  D
4.9.0-rc6-next-20161124-1-gbf7e142 #1
[ 2558.100925] Hardware name: Hardkernel ODROID-C2 (DT)
[ 2558.105841] task: 80006ddb7080 task.stack: 80006dd9c000
[ 2558.111706] PC is at 0x6a0e54
[ 2558.114638] LR is at 0x6a0e54
[ 2558.117571] pc : [<006a0e54>] lr : [<006a0e54>]
pstate: 63c5
[ 2558.124902] sp : 80006dd9fec0
[ 2558.128179] x29:  x28: 80006ddb7080
[ 2558.133441] x27: 012a7000 x26: 4b3f1288
[ 2558.138702] x25: 00f58f88 x24: 4b3f1240
[ 2558.143963] x23: 8000 x22: 006a0d98
[ 2558.149225] x21:  x20: 80006e223000
[ 2558.154486] x19:  x18: 0010
[ 2558.159747] x17: 48acaa10 x16: 01285050
[ 2558.165008] x15: 88e91f07 x14: 0006
[ 2558.170270] x13: 08e91f15 x12: 000f
[ 2558.175531] x11: 0002 x10: 02ea
[ 2558.180792] x9 : 80006dd9fb40 x8 : 00010a8b
[ 2558.186053] x7 :  x6 : 020e
[ 2558.191315] x5 : 020f020e x4 : 
[ 2558.196576] x3 :  x2 : 020f
[ 2558.201837] x1 : 80006ddb7080 x0 : 
[ 2558.207098]
[ 2558.208565] Process cc1 (pid: 22837, stack limit = 0x80006dd9c000)
[ 2558.215035] Stack: (0x80006dd9fec0 to 0x80006dda)
[ 2558.220728] fec0: 0065 00f45000 0020
000c
[ 2558.228490] fee0: 4b3e1aa0 4b402020 20020033
4b3f1240
[ 2558.236253] ff00: 0003 0005 0661
01347000
[ 2558.244015] ff20: 4b3f1258 4b3f1270 001f
4ad96dc8
[ 2558.251778] ff40: 01285050 48acaa10 0001
0054
[ 2558.259540] ff60: 4b3f1250 0001 0001

[ 2558.267303] ff80: 4b3f1240 00f58f88 4b3f1288
012a7000
[ 2558.275065] ffa0: 4b3f1240 f3ee5f80 006a0e54
f3ee5f80
[ 2558.282828] ffc0: 006a0d98 8000 0003

[ 2558.290590] ffe0:   

[ 2558.298351] Call trace:
[ 2558.300769] Exception stack(0x80006dd9fcf0 to 0x80006dd9fe20)
[ 2558.307149] fce0:   
0001
[ 2558.314913] fd00: 80006dd9fec0 006a0e54 800073acf500
0004
[ 2558.322675] fd20:  08dbbc18 80006ddb7080
6dd9fdd0
[ 2558.330438] fd40: 80006dd9fd90 080ca878 80006dd9fe40
80006ddb7080
[ 2558.338200] fd60: 0004 03c0 80006dd9fe40
4b3f1240
[ 2558.345963] fd80: 00f58f88 4b3f1288 
80006ddb7080
[ 2558.353725] fda0: 020f  
020f020e
[ 2558.36

[PATCH v3 15/30] perf clang: Compile BPF script use builtin clang support

2016-11-25 Thread Wang Nan
After this patch, perf utilizes builtin clang support to build BPF
script, no longer depend on external clang.

Test:

  $ type clang
  -bash: type: clang: not found
  $ cat ~/.perfconfig
  $ echo '#define LINUX_VERSION_CODE 0x040700' > ./test.c
  $ cat ./tools/perf/tests/bpf-script-example.c >> ./test.c
  $ ./perf record -v --dry-run -e ./test.c 2>&1 | grep builtin
  bpf: builtin compiling successful

Can't pass cflags so unable to include kernel headers now. Will be fixed
by following commits.

Signed-off-by: Wang Nan 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
Link: 
http://lkml.kernel.org/r/1474874832-134786-13-git-send-email-wangn...@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/util/bpf-loader.c  | 15 +++
 tools/perf/util/c++/clang-c.h | 26 ++
 tools/perf/util/c++/clang.cpp | 29 +
 3 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index cf16b941..a0ea334f 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -14,11 +14,11 @@
 #include "debug.h"
 #include "bpf-loader.h"
 #include "bpf-prologue.h"
-#include "llvm-utils.h"
 #include "probe-event.h"
 #include "probe-finder.h" // for MAX_PROBES
 #include "parse-events.h"
 #include "llvm-utils.h"
+#include "c++/clang-c.h"
 
 #define DEFINE_PRINT_FN(name, level) \
 static int libbpf_##name(const char *fmt, ...) \
@@ -86,9 +86,16 @@ struct bpf_object *bpf__prepare_load(const char *filename, 
bool source)
void *obj_buf;
size_t obj_buf_sz;
 
-   err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz);
-   if (err)
-   return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
+   perf_clang__init();
+   err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
+   perf_clang__cleanup();
+   if (err) {
+   pr_warning("bpf: builtin compiling failed: %d, try 
external compiler\n", err);
+   err = llvm__compile_bpf(filename, &obj_buf, 
&obj_buf_sz);
+   if (err)
+   return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
+   } else
+   pr_debug("bpf: builtin compiling successful\n");
obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
 
if (!IS_ERR(obj) && llvm_param.dump_obj)
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 22b3936..0eadd79 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -1,16 +1,42 @@
 #ifndef PERF_UTIL_CLANG_C_H
 #define PERF_UTIL_CLANG_C_H
 
+#include /* for size_t */
+#include   /* for __maybe_unused */
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+#ifdef HAVE_LIBCLANGLLVM_SUPPORT
 extern void perf_clang__init(void);
 extern void perf_clang__cleanup(void);
 
 extern int test__clang_to_IR(void);
 extern int test__clang_to_obj(void);
 
+extern int perf_clang__compile_bpf(const char *filename,
+  void **p_obj_buf,
+  size_t *p_obj_buf_sz);
+#else
+
+
+static inline void perf_clang__init(void) { }
+static inline void perf_clang__cleanup(void) { }
+
+static inline int test__clang_to_IR(void) { return -1; }
+static inline int test__clang_to_obj(void) { return -1;}
+
+static inline int
+perf_clang__compile_bpf(const char *filename __maybe_unused,
+   void **p_obj_buf __maybe_unused,
+   size_t *p_obj_buf_sz __maybe_unused)
+{
+   return -ENOTSUP;
+}
+
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 2a1a75d..1e97415 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -163,4 +163,33 @@ void perf_clang__cleanup(void)
perf::LLVMCtx.reset(nullptr);
llvm::llvm_shutdown();
 }
+
+int perf_clang__compile_bpf(const char *filename,
+   void **p_obj_buf,
+   size_t *p_obj_buf_sz)
+{
+   using namespace perf;
+
+   if (!p_obj_buf || !p_obj_buf_sz)
+   return -EINVAL;
+
+   llvm::opt::ArgStringList CFlags;
+   auto M = getModuleFromSource(std::move(CFlags), filename);
+   if (!M)
+   return  -EINVAL;
+   auto O = getBPFObjectFromModule(&*M);
+   if (!O)
+   return -EINVAL;
+
+   size_t size = O->size_in_bytes();
+   void *buffer;
+
+   buffer = malloc(size);
+   if (!buffer)
+   return -ENOMEM;
+   memcpy(buffer, O->data(), size);
+   *p_obj_buf = buffer;
+   *p_obj_buf_sz = size;
+   return 0;
+}
 }
-- 
2.10.1



[PATCH v3 00/30] perf clang: Builtin clang and perfhook support

2016-11-25 Thread Wang Nan
This is version 3 of perf builtin clang and perfhook patch series.
Compare to v2 there is only minor changes:
 1. BPF map helpers in perf hooks now called 'perf_map_...',
instead of 'jit_helper_map_...'.
(Alexei Starovoitov)
 2. Rename bpf_map_{pin,get} to bpf_obj_{pin,get}, make them consist
with kernel.
(Joe Stringer).

Example in v2 should be changed accordingly:

  $ cat ./count_syscalls.c
  typedef unsigned long u64;
  
  #define BPF_MAP_TYPE_HASH 1
  #define BPF_MAP_TYPE_ARRAY 2
  
  enum GVAL {
  G_perf_pid,
  NR_GVALS
  };
  
  struct bpf_map_def SEC("maps") GVALS = {
  .type = BPF_MAP_TYPE_ARRAY,
  .key_size = sizeof(int),
  .value_size = sizeof(u64),
  .max_entries = NR_GVALS,
  };
  
  struct bpf_map_def SEC("maps") syscall_counter = {
  .type = BPF_MAP_TYPE_HASH,
  .key_size = sizeof(u64),
  .value_size = sizeof(u64),
  .max_entries = 512,
  };
  
  SEC("raw_syscalls:sys_enter")
  int func(void *ctx)
  {
  int key = G_perf_pid;
  u64 id = *((u64 *)(ctx + 8));
  int self_pid = bpf_get_current_pid_tgid() & 0x;
  int *perf_pid = bpf_map_lookup_elem(&GVALS, &key);
  u64 *counter;
  
  if (!perf_pid)
  return 0;
  if (*perf_pid == self_pid)
  return 0;
  counter = bpf_map_lookup_elem(&syscall_counter, &id);
  if (!counter) {
  u64 value = 1;
  bpf_map_update_elem(&syscall_counter, &id, &value, 0);
  return 0;
  }
  __sync_fetch_and_add(counter, 1);
  return 0;
  }
  
  SEC("perfhook:record_start")
  void record_start(void *ctx)
  {
  int perf_pid = getpid(), key = G_perf_pid;
  printf("Start count, perfpid=%d\n", perf_pid);
  perf_map_update_elem(ctx, &GVALS, &key, &perf_pid, 0);
  }
  
  SEC("perfhook:record_end")
  void record_end(void *ctx)
  {
  u64 key = -1, value;
  while (!perf_map_get_next_key(ctx, &syscall_counter, &key, &key)) {
  perf_map_lookup_elem(ctx, &syscall_counter, &key, &value);
  printf("syscall %ld\tcount: %ld\n", (long)key, (long)value);
  }
  }
  char _license[] SEC("license") = "GPL";
  int _version SEC("version") = LINUX_VERSION_CODE;
  $ sudo -s
  # ulimit -l unlimited
  # perf record -e ./count_syscalls.c echo "Haha"
  Start count, perfpid=25209
  Haha
  [ perf record: Woken up 1 times to write data ]
  syscall 293   count: 2
  syscall 8 count: 7
  syscall 11count: 763
  syscall 4 count: 43
  syscall 21count: 48
  syscall 86count: 1
  syscall 5 count: 791
  ...

Wang Nan (30):
  tools lib bpf: Add missing BPF functions
  tools lib bpf: Add private field for bpf_object
  tools lib bpf: Retrive bpf_map through offset of bpf_map_def
  perf tools: Introduce perf hooks
  perf tools: Pass context to perf hook functions
  perf llvm: Extract helpers in llvm-utils.c
  tools build: Add feature detection for LLVM
  tools build: Add feature detection for clang
  perf build: Add clang and llvm compile and linking support
  perf clang: Add builtin clang support ant test case
  perf clang: Use real file system for #include
  perf clang: Allow passing CFLAGS to builtin clang
  perf clang: Update test case to use real BPF script
  perf clang: Support compile IR to BPF object and add testcase
  perf clang: Compile BPF script use builtin clang support
  perf clang: Pass full path to builtin clang
  perf clang: Pass CFLAGS to builtin clang
  perf clang jit: Wrap llvm::Module using PerfModule
  perf clang jit: Insignt BPF and JIT functions in a Module
  perf clang jit: add PerfModule::doJIT to JIT perfhook functions
  perf clang jit: Export functions for jitted code
  perf clang jit: Actually JIT and hook in bpf loader
  perf clang jit: Collect the lowest address in maps section as map_base
  perf clang jit: Retrive fd of BPF map from its offset
  perf clang jit: Allow jitted perf hook access BPF maps
  perf clang: Link BPF functions declaration into perf
  perf clang: Declare BPF functions for BPF scripts automatically
  perf clang: Include helpers to BPF scripts
  perf clang builtin: Define hook helpers by default
  perf clang jit: Export getpid() to perf hook

 tools/build/feature/Makefile  |  18 +
 tools/build/feature/test-clang.cpp|  21 ++
 tools/build/feature/test-llvm.cpp |   8 +
 tools/lib/bpf/bpf.c   |  56 +++
 tools/lib/bpf/bpf.h   |   7 +
 tools/lib/bpf/libbpf.c|  35 ++
 tools/lib/bpf/libbpf.h|  13 +
 tools/perf/Makefile.config|  62 +++-
 tools/perf/Makefile.perf  |  23 +-
 tools/perf/builtin-record.c   |  11 +
 tools/perf/tests/Build|   4 +-
 tools/perf/tests/bpf-script-example.c |  30 +-
 tools/perf/tests/bpf-script-test-kbuild.c |   2 +
 tools/perf/tests/bpf-script-test-prologue.c   |   6 +-
 tools/perf/tests/bpf-script-test-relocation.c |  17

[PATCH v3 22/30] perf clang jit: Actually JIT and hook in bpf loader

2016-11-25 Thread Wang Nan
Makes perf_clang__compile_bpf() actually uses clang jit to compile perf
hooks. Returns a map through perf_clang__compile_bpf(), and set hooks
after bpf_object is created.

After this path jitting takes actions for bpf loader. For example:
  $ cat ./test.c
  /**/
  #define SEC(name) __attribute__((section(name), used))
  SEC("dofork=_do_fork")
  int dofork(void *ctx)
  {
  return 0;
  }
  extern int printf(const char *fmt, ...);
  SEC("perfhook:record_start")
  void record_start(void)
  {
  printf("Welcom to perf record\n");
  }
  SEC("perfhook:record_end")
  void record_end(void)
  {
  printf("Goodbye, perf record\n");
  }
  char _license[] SEC("license") = "GPL";
  int _version SEC("version") = LINUX_VERSION_CODE;
  /**/
  $ perf record -e ./test.c sleep 1
  Welcom to perf record
  [ perf record: Woken up 1 times to write data ]
  Goodbye, perf record
  [ perf record: Captured and wrote 0.014 MB perf.data ]

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/bpf-loader.c  | 11 ++-
 tools/perf/util/c++/clang-c.h | 18 --
 tools/perf/util/c++/clang.cpp | 28 +++-
 3 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index a0ea334f..e50045f 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -85,9 +85,11 @@ struct bpf_object *bpf__prepare_load(const char *filename, 
bool source)
int err;
void *obj_buf;
size_t obj_buf_sz;
+   jitted_funcs_map_t jitted_funcs_map;
 
perf_clang__init();
-   err = perf_clang__compile_bpf(filename, &obj_buf, &obj_buf_sz);
+   err = perf_clang__compile_bpf(filename, &obj_buf,
+ &obj_buf_sz, &jitted_funcs_map);
perf_clang__cleanup();
if (err) {
pr_warning("bpf: builtin compiling failed: %d, try 
external compiler\n", err);
@@ -101,6 +103,13 @@ struct bpf_object *bpf__prepare_load(const char *filename, 
bool source)
if (!IS_ERR(obj) && llvm_param.dump_obj)
llvm__dump_obj(filename, obj_buf, obj_buf_sz);
 
+   /*
+* Call perf_clang__hook_jitted_func even IS_ERR(obj) to make 
sure
+* the C++ map pointer is deleted.
+*/
+   if (jitted_funcs_map)
+   perf_clang__hook_jitted_func(jitted_funcs_map, obj, 
IS_ERR(obj));
+
free(obj_buf);
} else
obj = bpf_object__open(filename);
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 9f75e41..021b1ad 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -8,6 +8,7 @@
 extern "C" {
 #endif
 
+typedef void *jitted_funcs_map_t;
 #ifdef HAVE_LIBCLANGLLVM_SUPPORT
 extern void perf_clang__init(void);
 extern void perf_clang__cleanup(void);
@@ -20,7 +21,11 @@ extern void test__clang_callback(int x);
 
 extern int perf_clang__compile_bpf(const char *filename,
   void **p_obj_buf,
-  size_t *p_obj_buf_sz);
+  size_t *p_obj_buf_sz,
+  jitted_funcs_map_t *p_funcs_map);
+
+extern int
+perf_clang__hook_jitted_func(jitted_funcs_map_t map, void *ctx, bool is_err);
 #else
 
 
@@ -34,7 +39,16 @@ static inline int test__clang_jit(void) { return -1;}
 static inline int
 perf_clang__compile_bpf(const char *filename __maybe_unused,
void **p_obj_buf __maybe_unused,
-   size_t *p_obj_buf_sz __maybe_unused)
+   size_t *p_obj_buf_sz __maybe_unused,
+   jitted_funcs_map_t *p_funcs_map __maybe_unused)
+{
+   return -ENOTSUP;
+}
+
+static inline int
+perf_clang__hook_jitted_func(jitted_funcs_map_t map __maybe_unused,
+void *ctx __maybe_unused,
+bool is_err __maybe_unused)
 {
return -ENOTSUP;
 }
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 325fbe4..f2608f5 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -388,7 +388,8 @@ void perf_clang__cleanup(void)
 
 int perf_clang__compile_bpf(const char *_filename,
void **p_obj_buf,
-   size_t *p_obj_buf_sz)
+   size_t *p_obj_buf_sz,
+   jitted_funcs_map_t *p_funcs_map)
 {
using namespace perf;
 
@@ -415,6 +416,31 @@ int perf_clang__compile_bpf(const char *_filename,
memcpy(buffer, O->data(), si

[PATCH v3 08/30] tools build: Add feature detection for clang

2016-11-25 Thread Wang Nan
Check if basic clang compiling environment is ready.

Doesn't like 'llvm-config --libs' which can returns llvm libraries in
right order and duplicates some libraries if necessary, there's no
correspondence for clang libraries (-lclangxxx). to avoid extra
complexity and to avoid new clang breaking libraries ordering, use
--start-group and --end-group.

In this test case, manually identify required clang libs and hope it to
be stable. Putting all clang libraries here is possible (use make's
wildcard), but then feature checking becomes very slow.

Signed-off-by: Wang Nan 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
Link: 
http://lkml.kernel.org/r/1474874832-134786-5-git-send-email-wangn...@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/build/feature/Makefile   | 10 ++
 tools/build/feature/test-clang.cpp | 21 +
 2 files changed, 31 insertions(+)
 create mode 100644 tools/build/feature/test-clang.cpp

diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index c09de59..871d553 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -237,6 +237,16 @@ $(OUTPUT)test-llvm.bin:
$(shell $(LLVM_CONFIG) --libs Core BPF) \
$(shell $(LLVM_CONFIG) --system-libs)
 
+$(OUTPUT)test-clang.bin:
+   $(BUILDXX) -std=gnu++11 \
+   -I$(shell $(LLVM_CONFIG) --includedir)  \
+   -L$(shell $(LLVM_CONFIG) --libdir)  \
+   -Wl,--start-group -lclangBasic -lclangDriver\
+ -lclangFrontend -lclangEdit -lclangLex\
+ -lclangAST -Wl,--end-group\
+   $(shell $(LLVM_CONFIG) --libs Core option)  \
+   $(shell $(LLVM_CONFIG) --system-libs)
+
 -include $(OUTPUT)*.d
 
 ###
diff --git a/tools/build/feature/test-clang.cpp 
b/tools/build/feature/test-clang.cpp
new file mode 100644
index 000..e23c1b1
--- /dev/null
+++ b/tools/build/feature/test-clang.cpp
@@ -0,0 +1,21 @@
+#include "clang/Basic/VirtualFileSystem.h"
+#include "clang/Driver/Driver.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace clang;
+using namespace clang::driver;
+
+int main()
+{
+   IntrusiveRefCntPtr DiagID(new DiagnosticIDs());
+   IntrusiveRefCntPtr DiagOpts = new 
DiagnosticOptions();
+
+   DiagnosticsEngine Diags(DiagID, &*DiagOpts);
+   Driver TheDriver("test", "bpf-pc-linux", Diags);
+
+   llvm::llvm_shutdown();
+   return 0;
+}
-- 
2.10.1



[PATCH v3 05/30] perf tools: Pass context to perf hook functions

2016-11-25 Thread Wang Nan
Pass a pointer to perf hook functions so they receive context
information created durnig setup.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/tests/perf-hooks.c | 14 +-
 tools/perf/util/perf-hooks.c  | 10 +++---
 tools/perf/util/perf-hooks.h  |  6 --
 3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/tools/perf/tests/perf-hooks.c b/tools/perf/tests/perf-hooks.c
index 9338cb2..665ecc1 100644
--- a/tools/perf/tests/perf-hooks.c
+++ b/tools/perf/tests/perf-hooks.c
@@ -15,13 +15,13 @@ static void sigsegv_handler(int sig __maybe_unused)
exit(-1);
 }
 
-static int hook_flags;
 
-static void the_hook(void)
+static void the_hook(void *_hook_flags)
 {
+   int *hook_flags = _hook_flags;
int *p = NULL;
 
-   hook_flags = 1234;
+   *hook_flags = 1234;
 
/* Generate a segfault, test perf_hooks__recover */
*p = 0;
@@ -29,13 +29,17 @@ static void the_hook(void)
 
 int test__perf_hooks(int subtest __maybe_unused)
 {
+   int hook_flags = 0;
+
signal(SIGSEGV, sigsegv_handler);
-   perf_hooks__set_hook("test", the_hook);
+   perf_hooks__set_hook("test", the_hook, &hook_flags);
perf_hooks__invoke_test();
 
/* hook is triggered? */
-   if (hook_flags != 1234)
+   if (hook_flags != 1234) {
+   pr_debug("Setting failed: %d (%p)\n", hook_flags, &hook_flags);
return TEST_FAIL;
+   }
 
/* the buggy hook is removed? */
if (perf_hooks__get_hook("test"))
diff --git a/tools/perf/util/perf-hooks.c b/tools/perf/util/perf-hooks.c
index 4ce88e3..cb36830 100644
--- a/tools/perf/util/perf-hooks.c
+++ b/tools/perf/util/perf-hooks.c
@@ -27,7 +27,7 @@ void perf_hooks__invoke(const struct perf_hook_desc *desc)
*(current_perf_hook->p_hook_func) = NULL;
} else {
current_perf_hook = desc;
-   (**desc->p_hook_func)();
+   (**desc->p_hook_func)(desc->hook_ctx);
}
current_perf_hook = NULL;
 }
@@ -41,7 +41,9 @@ void perf_hooks__recover(void)
 #define PERF_HOOK(name)\
 perf_hook_func_t __perf_hook_func_##name = NULL;   \
 struct perf_hook_desc __perf_hook_desc_##name =\
-   {.hook_name = #name, .p_hook_func = &__perf_hook_func_##name};
+   {.hook_name = #name,\
+.p_hook_func = &__perf_hook_func_##name,   \
+.hook_ctx = NULL};
 #include "perf-hooks-list.h"
 #undef PERF_HOOK
 
@@ -54,7 +56,8 @@ static struct perf_hook_desc *perf_hooks[] = {
 #undef PERF_HOOK
 
 int perf_hooks__set_hook(const char *hook_name,
-perf_hook_func_t hook_func)
+perf_hook_func_t hook_func,
+void *hook_ctx)
 {
unsigned int i;
 
@@ -65,6 +68,7 @@ int perf_hooks__set_hook(const char *hook_name,
if (*(perf_hooks[i]->p_hook_func))
pr_warning("Overwrite existing hook: %s\n", hook_name);
*(perf_hooks[i]->p_hook_func) = hook_func;
+   perf_hooks[i]->hook_ctx = hook_ctx;
return 0;
}
return -ENOENT;
diff --git a/tools/perf/util/perf-hooks.h b/tools/perf/util/perf-hooks.h
index 1d482b2..838d579 100644
--- a/tools/perf/util/perf-hooks.h
+++ b/tools/perf/util/perf-hooks.h
@@ -5,10 +5,11 @@
 extern "C" {
 #endif
 
-typedef void (*perf_hook_func_t)(void);
+typedef void (*perf_hook_func_t)(void *ctx);
 struct perf_hook_desc {
const char * const hook_name;
perf_hook_func_t * const p_hook_func;
+   void *hook_ctx;
 };
 
 extern void perf_hooks__invoke(const struct perf_hook_desc *);
@@ -26,7 +27,8 @@ static inline void perf_hooks__invoke_##name(void)\
 
 extern int
 perf_hooks__set_hook(const char *hook_name,
-perf_hook_func_t hook_func);
+perf_hook_func_t hook_func,
+void *hook_ctx);
 
 extern perf_hook_func_t
 perf_hooks__get_hook(const char *hook_name);
-- 
2.10.1



[PATCH v3 10/30] perf clang: Add builtin clang support ant test case

2016-11-25 Thread Wang Nan
Add basic clang support in clang.cpp and test__clang() testcase. The
first testcase checks if builtin clang is able to generate LLVM IR.

tests/clang.c is a proxy. Real testcase resides in
utils/c++/clang-test.cpp in c++ and exports C interface to perf test
subsystem.

Test result:

   $ perf test -v clang
   51: Test builtin clang support   :
   51.1: Test builtin clang compile C source to IR  :
   --- start ---
   test child forked, pid 13215
   test child finished with 0
    end 
   Test builtin clang support subtest 0: Ok

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/tests/Build |  1 +
 tools/perf/tests/builtin-test.c|  9 
 tools/perf/tests/clang.c   | 42 +
 tools/perf/tests/tests.h   |  3 ++
 tools/perf/util/Build  |  2 +
 tools/perf/util/c++/Build  |  2 +
 tools/perf/util/c++/clang-c.h  | 16 +++
 tools/perf/util/c++/clang-test.cpp | 31 
 tools/perf/util/c++/clang.cpp  | 96 ++
 tools/perf/util/c++/clang.h| 16 +++
 10 files changed, 218 insertions(+)
 create mode 100644 tools/perf/tests/clang.c
 create mode 100644 tools/perf/util/c++/Build
 create mode 100644 tools/perf/util/c++/clang-c.h
 create mode 100644 tools/perf/util/c++/clang-test.cpp
 create mode 100644 tools/perf/util/c++/clang.cpp
 create mode 100644 tools/perf/util/c++/clang.h

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index af3ec94..6676c2d 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -43,6 +43,7 @@ perf-y += sdt.o
 perf-y += is_printable_array.o
 perf-y += bitmap.o
 perf-y += perf-hooks.o
+perf-y += clang.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
$(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index dab83f7..33aaa52 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -234,6 +234,15 @@ static struct test generic_tests[] = {
.func = test__perf_hooks,
},
{
+   .desc = "Test builtin clang support",
+   .func = test__clang,
+   .subtest = {
+   .skip_if_fail   = true,
+   .get_nr = test__clang_subtest_get_nr,
+   .get_desc   = test__clang_subtest_get_desc,
+   }
+   },
+   {
.func = NULL,
},
 };
diff --git a/tools/perf/tests/clang.c b/tools/perf/tests/clang.c
new file mode 100644
index 000..57ee160
--- /dev/null
+++ b/tools/perf/tests/clang.c
@@ -0,0 +1,42 @@
+#include "tests.h"
+#include "debug.h"
+#include "util.h"
+#include "c++/clang-c.h"
+
+static struct {
+   int (*func)(void);
+   const char *desc;
+} clang_testcase_table[] = {
+#ifdef HAVE_LIBCLANGLLVM_SUPPORT
+   {
+   .func = test__clang_to_IR,
+   .desc = "Test builtin clang compile C source to IR",
+   },
+#endif
+};
+
+int test__clang_subtest_get_nr(void)
+{
+   return (int)ARRAY_SIZE(clang_testcase_table);
+}
+
+const char *test__clang_subtest_get_desc(int i)
+{
+   if (i < 0 || i >= (int)ARRAY_SIZE(clang_testcase_table))
+   return NULL;
+   return clang_testcase_table[i].desc;
+}
+
+#ifndef HAVE_LIBCLANGLLVM_SUPPORT
+int test__clang(int i __maybe_unused)
+{
+   return TEST_SKIP;
+}
+#else
+int test__clang(int i __maybe_unused)
+{
+   if (i < 0 || i >= (int)ARRAY_SIZE(clang_testcase_table))
+   return TEST_FAIL;
+   return clang_testcase_table[i].func();
+}
+#endif
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 3a1f98f..0d7b251 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -92,6 +92,9 @@ int test__sdt_event(int subtest);
 int test__is_printable_array(int subtest);
 int test__bitmap_print(int subtest);
 int test__perf_hooks(int subtest);
+int test__clang(int subtest);
+const char *test__clang_subtest_get_desc(int subtest);
+int test__clang_subtest_get_nr(void);
 
 #if defined(__arm__) || defined(__aarch64__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index b2a47aa..743a889 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -125,6 +125,8 @@ endif
 
 libperf-y += perf-hooks.o
 
+libperf-$(CONFIG_CXX) += c++/
+
 CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 # avoid compiler warnings in 32-bit mode
 CFLAGS_genelf_debug.o  += -Wno-packed
diff --git a/tools/perf/util/c++/Build b/tools/perf/util/c++/Build
new file mode 100644
index 000..988fef1
--- /dev/null
+++ b/tools/perf/util/c++/Build
@@ -0,0 +1,2 @@
+libperf-$(CONFIG_CLANGLLVM) += clang.o
+libperf-$(CONFIG_CLANGLLVM) += clang-test.o
diff --git a/tools/

[PATCH v3 25/30] perf clang jit: Allow jitted perf hook access BPF maps

2016-11-25 Thread Wang Nan
Newly introduced jit-helpers.[ch] defines a series of helpers which helps
jitted perf hook functions accessing BPF maps defined in their BPF scripts.
The helpers fetches fd of 'struct bpf_map' from 'struct bpf_object' and the
address of 'struct bpf_map_def' in jitted file. 'struct bpf_object' is the
context passed to hooks.

Jit helpers added in this commits are all leading with 'perf_'. We don't use
'bpf_' prefix because in following commits 'bpf_' prefix is going to be assigned
to kernel side BPF map operations. Same operation has different protocol for
kernel and user.

 Example:

  $ cat ./test.c
  /***/
  #define SEC(name) __attribute__((section(name), used))
  #define BPF_MAP_TYPE_ARRAY 2
  #define BPF_MAP_TYPE_PERF_EVENT_ARRAY 4
  #define BPF_FUNC_map_lookup_elem 1
  static void *(*bpf_map_lookup_elem)(void *map, void *key) =
  (void *) BPF_FUNC_map_lookup_elem;
  struct bpf_map_def {
  unsigned int type;
  unsigned int key_size;
  unsigned int value_size;
  unsigned int max_entries;
  };
  struct bpf_map_def SEC("maps") counter = {
  .type = BPF_MAP_TYPE_ARRAY,
  .key_size = sizeof(int),
  .value_size = sizeof(int),
  .max_entries = 1,
  };
  extern int perf_map_update_elem(void *ctx, struct bpf_map_def *map,
 void *key, void *value, unsigned long flags);
  extern int perf_map_lookup_elem(void *ctx, struct bpf_map_def *map,
 void *key, void *value);
  SEC("sys_close=SyS_close")
  int sys_close(void *ctx)
  {
  int key = 0;
  int *value;
  value = bpf_map_lookup_elem(&counter, &key);
  if (!value)
  return 0;
  __sync_fetch_and_add(value, 1);
  return 0;
  }
  extern int printf(const char *fmt, ...);
  SEC("perfhook:record_start")
  void record_start(void *ctx)
  {
  int key = 0;
  int value = 1;
  printf("Welcom to perf record\n");
  perf_map_update_elem(ctx, &counter, &key, &value, 0);
  }

  SEC("perfhook:record_end")
  void record_end(void *ctx)
  {
  int key = 0;
  int value;
  perf_map_lookup_elem(ctx, &counter, &key, &value);
  printf("Goodbye, perf record, value=%d\n", value);
  }
  char _license[] SEC("license") = "GPL";
  int _version SEC("version") = LINUX_VERSION_CODE;
  /***/
  $ sudo perf record  -e ./test.c echo Hehe
  Welcom to perf record
  Hehe
  [ perf record: Woken up 1 times to write data ]
  Goodbye, perf record, value=10644
  [ perf record: Captured and wrote 0.014 MB perf.data ]

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/Build |  1 +
 tools/perf/util/c++/clang.cpp |  9 ++-
 tools/perf/util/jit-helpers.c | 57 +++
 tools/perf/util/jit-helpers.h | 28 +
 4 files changed, 94 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/jit-helpers.c
 create mode 100644 tools/perf/util/jit-helpers.h

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 743a889..33773cb 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -124,6 +124,7 @@ libperf-$(CONFIG_DWARF) += genelf_debug.o
 endif
 
 libperf-y += perf-hooks.o
+libperf-y += jit-helpers.o
 
 libperf-$(CONFIG_CXX) += c++/
 
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index f8ea9bd..48bd3ee 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -38,6 +38,7 @@
 #include "llvm-utils.h"
 #include "util-cxx.h"
 #include "perf-hooks.h"
+#include "jit-helpers.h"
 
 namespace perf {
 
@@ -196,12 +197,18 @@ PerfModule::toBPFObject(void)
return std::move(Buffer);
 }
 
+#define __stringify_1(x)   #x
+#define __stringify(x) __stringify_1(x)
 static std::map exported_funcs =
 {
-#define EXPORT(f) {#f, (const void *)&f}
+#define EXPORT(f) {__stringify(f), (const void *)&f}
EXPORT(test__clang_callback),
EXPORT(printf),
EXPORT(puts),
+   EXPORT(JIT_HELPER_FUNC_NAME(map_update_elem)),
+   EXPORT(JIT_HELPER_FUNC_NAME(map_lookup_elem)),
+   EXPORT(JIT_HELPER_FUNC_NAME(map_get_next_key)),
+   EXPORT(JIT_HELPER_FUNC_NAME(map_pin)),
 #undef EXPORT
 };
 
diff --git a/tools/perf/util/jit-helpers.c b/tools/perf/util/jit-helpers.c
new file mode 100644
index 000..1a37a20
--- /dev/null
+++ b/tools/perf/util/jit-helpers.c
@@ -0,0 +1,57 @@
+/*
+ * jit-helper.c
+ *
+ * Copyright (C) 2016 Wang Nan 
+ * Copyright (C) 2016 Huawei Inc.
+ *
+ * Provide helpers which can be invoked by jit scripts attached to
+ * perf hooks.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include "asm/bug.h"
+
+static int get_bpf_map_fd(struct bpf_object *obj, void *map)
+{
+   int fd;
+   char errbuf[BUFSIZ];
+
+   fd = bpf__map_fd(obj, map);
+   if (fd < 0) {
+  

[PATCH v3 29/30] perf clang builtin: Define hook helpers by default

2016-11-25 Thread Wang Nan
Append declarations of helpers to default include file. All functions
appear in exported_funcs array should be declared here except
test__clang_callback, because it is used for perf test only.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/c++/bpf-helper-str.c | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/tools/perf/util/c++/bpf-helper-str.c 
b/tools/perf/util/c++/bpf-helper-str.c
index 17f915c..f4d6d57 100644
--- a/tools/perf/util/c++/bpf-helper-str.c
+++ b/tools/perf/util/c++/bpf-helper-str.c
@@ -10,6 +10,13 @@ const char clang_builtin_bpf_helper_str[] =
 "  unsigned int max_entries;\n"
 "};\n"
 "#define SEC(NAME) __attribute__((section(NAME), used))\n"
+"extern int printf(const char *, ...);\n"
+"extern int puts(const char *);\n"
+"extern int perf_map_update_elem(void *, void *, void *, void *, unsigned 
long);\n"
+"extern int perf_map_lookup_elem(void *, void *, void *, void *);\n"
+"extern int perf_map_get_next_key(void *, void *, void *, void *);\n"
+"extern int perf_map_pin(void *, void *, const char *);\n"
+"extern int perf_map_get(const char *);\n"
 "#endif\n"
 "#endif"
 ;
-- 
2.10.1



[PATCH v3 18/30] perf clang jit: Wrap llvm::Module using PerfModule

2016-11-25 Thread Wang Nan
Use PerfModule wrap llvm::Module and return perf::PerfModule in APIs to
replace llvm::Module. Following commits are going to add new functions
to PerfModule.

getBPFObjectFromModule is merged to a method of perf::PerfModule.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/c++/clang-test.cpp | 10 +-
 tools/perf/util/c++/clang.cpp  | 20 +---
 tools/perf/util/c++/clang.h| 22 --
 3 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/tools/perf/util/c++/clang-test.cpp 
b/tools/perf/util/c++/clang-test.cpp
index 9b11e8c..fb05e56 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -13,18 +13,18 @@ public:
~perf_clang_scope() {perf_clang__cleanup();}
 };
 
-static std::unique_ptr
+static std::unique_ptr
 __test__clang_to_IR(void)
 {
unsigned int kernel_version;
 
if (fetch_kernel_version(&kernel_version, NULL, 0))
-   return std::unique_ptr(nullptr);
+   return std::unique_ptr(nullptr);
 
std::string cflag_kver("-DLINUX_VERSION_CODE=" +
std::to_string(kernel_version));
 
-   std::unique_ptr M =
+   std::unique_ptr M =
perf::getModuleFromSource({cflag_kver.c_str()},
  "perf-test.c",
  test_llvm__bpf_base_prog);
@@ -39,7 +39,7 @@ int test__clang_to_IR(void)
auto M = __test__clang_to_IR();
if (!M)
return -1;
-   for (llvm::Function& F : *M)
+   for (llvm::Function& F : *(M->getModule()))
if (F.getName() == "bpf_func__SyS_epoll_wait")
return 0;
return -1;
@@ -53,7 +53,7 @@ int test__clang_to_obj(void)
if (!M)
return -1;
 
-   auto Buffer = perf::getBPFObjectFromModule(&*M);
+   auto Buffer = M->toBPFObject();
if (!Buffer)
return -1;
return 0;
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 3a3b9791..d31b0a5 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -64,7 +64,7 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, 
StringRef& Path,
return CI;
 }
 
-static std::unique_ptr
+static std::unique_ptr
 getModuleFromSource(llvm::opt::ArgStringList CFlags,
StringRef Path, IntrusiveRefCntPtr VFS)
 {
@@ -80,12 +80,12 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags,
 
std::unique_ptr Act(new EmitLLVMOnlyAction(&*LLVMCtx));
if (!Clang.ExecuteAction(*Act))
-   return std::unique_ptr(nullptr);
+   return std::unique_ptr(nullptr);
 
-   return Act->takeModule();
+   return std::unique_ptr(new 
PerfModule(std::move(Act->takeModule(;
 }
 
-std::unique_ptr
+std::unique_ptr
 getModuleFromSource(llvm::opt::ArgStringList CFlags,
StringRef Name, StringRef Content)
 {
@@ -106,15 +106,21 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags,
return getModuleFromSource(std::move(CFlags), Name, OverlayFS);
 }
 
-std::unique_ptr
+std::unique_ptr
 getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
 {
IntrusiveRefCntPtr VFS(vfs::getRealFileSystem());
return getModuleFromSource(std::move(CFlags), Path, VFS);
 }
 
+PerfModule::PerfModule(std::unique_ptr&& M) : 
Module(std::move(M))
+{
+
+}
+
+
 std::unique_ptr>
-getBPFObjectFromModule(llvm::Module *Module)
+PerfModule::toBPFObject(void)
 {
using namespace llvm;
 
@@ -278,7 +284,7 @@ int perf_clang__compile_bpf(const char *_filename,
auto M = getModuleFromSource(std::move(CFlags), Opts.getFileName());
if (!M)
return  -EINVAL;
-   auto O = getBPFObjectFromModule(&*M);
+   auto O = M->toBPFObject();
if (!O)
return -EINVAL;
 
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index dd8b042..cbb291b 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -11,16 +11,26 @@ namespace perf {
 
 using namespace llvm;
 
-std::unique_ptr
+class PerfModule {
+private:
+   std::unique_ptr Module;
+public:
+   inline llvm::Module *getModule(void)
+   {
+   return Module.get();
+   }
+
+   PerfModule(std::unique_ptr&& M);
+
+   std::unique_ptr> toBPFObject(void);
+};
+
+std::unique_ptr
 getModuleFromSource(opt::ArgStringList CFlags,
StringRef Name, StringRef Content);
 
-std::unique_ptr
+std::unique_ptr
 getModuleFromSource(opt::ArgStringList CFlags,
StringRef Path);
-
-std::unique_ptr>
-getBPFObjectFromModule(llvm::Module *Module);
-
 }
 #endif
-- 
2.10.1



[PATCH v3 12/30] perf clang: Allow passing CFLAGS to builtin clang

2016-11-25 Thread Wang Nan
Improve getModuleFromSource() API to accept a cflags list. This feature
will be used to pass LINUX_VERSION_CODE and -I flags.

Signed-off-by: Wang Nan 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
Link: 
http://lkml.kernel.org/r/1474874832-134786-9-git-send-email-wangn...@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/util/c++/clang-test.cpp |  5 +++--
 tools/perf/util/c++/clang.cpp  | 21 +
 tools/perf/util/c++/clang.h|  8 ++--
 3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/tools/perf/util/c++/clang-test.cpp 
b/tools/perf/util/c++/clang-test.cpp
index 3da6bfa..0f484fb 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -16,8 +16,9 @@ int test__clang_to_IR(void)
perf_clang_scope _scope;
 
std::unique_ptr M =
-   perf::getModuleFromSource("perf-test.c",
- "int myfunc(void) {return 1;}");
+   perf::getModuleFromSource({"-DRESULT=1"},
+ "perf-test.c",
+ "int myfunc(void) {return RESULT;}");
 
if (!M)
return -1;
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index cf96199..715ca0a 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -29,7 +29,8 @@ static std::unique_ptr LLVMCtx;
 using namespace clang;
 
 static CompilerInvocation *
-createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
+createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
+DiagnosticsEngine& Diags)
 {
llvm::opt::ArgStringList CCArgs {
"-cc1",
@@ -45,6 +46,8 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& 
Diags)
"-Wno-unused-value",
"-Wno-pointer-sign",
"-x", "c"};
+
+   CCArgs.append(CFlags.begin(), CFlags.end());
CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
 
FrontendOptions& Opts = CI->getFrontendOpts();
@@ -54,8 +57,8 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& 
Diags)
 }
 
 static std::unique_ptr
-getModuleFromSource(StringRef Path,
-   IntrusiveRefCntPtr VFS)
+getModuleFromSource(llvm::opt::ArgStringList CFlags,
+   StringRef Path, IntrusiveRefCntPtr VFS)
 {
CompilerInstance Clang;
Clang.createDiagnostics();
@@ -63,7 +66,8 @@ getModuleFromSource(StringRef Path,
Clang.setVirtualFileSystem(&*VFS);
 
IntrusiveRefCntPtr CI =
-   createCompilerInvocation(Path, Clang.getDiagnostics());
+   createCompilerInvocation(std::move(CFlags), Path,
+Clang.getDiagnostics());
Clang.setInvocation(&*CI);
 
std::unique_ptr Act(new EmitLLVMOnlyAction(&*LLVMCtx));
@@ -74,7 +78,8 @@ getModuleFromSource(StringRef Path,
 }
 
 std::unique_ptr
-getModuleFromSource(StringRef Name, StringRef Content)
+getModuleFromSource(llvm::opt::ArgStringList CFlags,
+   StringRef Name, StringRef Content)
 {
using namespace vfs;
 
@@ -90,14 +95,14 @@ getModuleFromSource(StringRef Name, StringRef Content)
OverlayFS->pushOverlay(MemFS);
MemFS->addFile(Twine(Name), 0, 
llvm::MemoryBuffer::getMemBuffer(Content));
 
-   return getModuleFromSource(Name, OverlayFS);
+   return getModuleFromSource(std::move(CFlags), Name, OverlayFS);
 }
 
 std::unique_ptr
-getModuleFromSource(StringRef Path)
+getModuleFromSource(llvm::opt::ArgStringList CFlags, StringRef Path)
 {
IntrusiveRefCntPtr VFS(vfs::getRealFileSystem());
-   return getModuleFromSource(Path, VFS);
+   return getModuleFromSource(std::move(CFlags), Path, VFS);
 }
 
 }
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index 90aff01..b4fc2a9 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -4,16 +4,20 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Option/Option.h"
 #include 
+
 namespace perf {
 
 using namespace llvm;
 
 std::unique_ptr
-getModuleFromSource(StringRef Name, StringRef Content);
+getModuleFromSource(opt::ArgStringList CFlags,
+   StringRef Name, StringRef Content);
 
 std::unique_ptr
-getModuleFromSource(StringRef Path);
+getModuleFromSource(opt::ArgStringList CFlags,
+   StringRef Path);
 
 }
 #endif
-- 
2.10.1



[PATCH v3 28/30] perf clang: Include helpers to BPF scripts

2016-11-25 Thread Wang Nan
Automatically include some commonly used macros and struct definitions
into BPF scripts. Script writers are no longer required to define
'SEC' and 'struct bpf_map_def' in each of their scripts.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/tests/bpf-script-example.c |  2 +-
 tools/perf/tests/bpf-script-test-kbuild.c |  2 ++
 tools/perf/tests/bpf-script-test-prologue.c   |  4 +++-
 tools/perf/tests/bpf-script-test-relocation.c |  3 +--
 tools/perf/util/c++/Build |  1 +
 tools/perf/util/c++/bpf-helper-str.c  | 15 +++
 tools/perf/util/c++/clang-bpf-includes.h  |  1 +
 tools/perf/util/c++/clang.cpp |  1 +
 8 files changed, 25 insertions(+), 4 deletions(-)
 create mode 100644 tools/perf/util/c++/bpf-helper-str.c

diff --git a/tools/perf/tests/bpf-script-example.c 
b/tools/perf/tests/bpf-script-example.c
index 42dc341..e60bebf 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -9,6 +9,7 @@
 #define BPF_ANY 0
 #define BPF_MAP_TYPE_ARRAY 2
 
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
 struct bpf_map_def {
unsigned int type;
unsigned int key_size;
@@ -18,7 +19,6 @@ struct bpf_map_def {
 
 #define SEC(NAME) __attribute__((section(NAME), used))
 
-#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
 #define BPF_FUNC_map_lookup_elem 1
 #define BPF_FUNC_map_update_elem 2
 
diff --git a/tools/perf/tests/bpf-script-test-kbuild.c 
b/tools/perf/tests/bpf-script-test-kbuild.c
index 3626924..f1b48a4 100644
--- a/tools/perf/tests/bpf-script-test-kbuild.c
+++ b/tools/perf/tests/bpf-script-test-kbuild.c
@@ -6,7 +6,9 @@
 # error Need LINUX_VERSION_CODE
 # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" 
into llvm section of ~/.perfconfig'
 #endif
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
 #define SEC(NAME) __attribute__((section(NAME), used))
+#endif
 
 #include 
 #include 
diff --git a/tools/perf/tests/bpf-script-test-prologue.c 
b/tools/perf/tests/bpf-script-test-prologue.c
index ada812b..e2176c9 100644
--- a/tools/perf/tests/bpf-script-test-prologue.c
+++ b/tools/perf/tests/bpf-script-test-prologue.c
@@ -6,7 +6,6 @@
 # error Need LINUX_VERSION_CODE
 # error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" 
into llvm section of ~/.perfconfig'
 #endif
-#define SEC(NAME) __attribute__((section(NAME), used))
 
 #include 
 
@@ -14,6 +13,9 @@
 #define FMODE_WRITE0x2
 
 #ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
+
+#define SEC(NAME) __attribute__((section(NAME), used))
+
 static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
(void *) 6;
 #endif
diff --git a/tools/perf/tests/bpf-script-test-relocation.c 
b/tools/perf/tests/bpf-script-test-relocation.c
index 57c96a3..bb54926 100644
--- a/tools/perf/tests/bpf-script-test-relocation.c
+++ b/tools/perf/tests/bpf-script-test-relocation.c
@@ -9,6 +9,7 @@
 #define BPF_ANY 0
 #define BPF_MAP_TYPE_ARRAY 2
 
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
 struct bpf_map_def {
unsigned int type;
unsigned int key_size;
@@ -17,8 +18,6 @@ struct bpf_map_def {
 };
 
 #define SEC(NAME) __attribute__((section(NAME), used))
-
-#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
 #define BPF_FUNC_map_lookup_elem 1
 #define BPF_FUNC_map_update_elem 2
 
diff --git a/tools/perf/util/c++/Build b/tools/perf/util/c++/Build
index bd71abf..faa0268 100644
--- a/tools/perf/util/c++/Build
+++ b/tools/perf/util/c++/Build
@@ -1,3 +1,4 @@
 libperf-$(CONFIG_CLANGLLVM) += clang.o
 libperf-$(CONFIG_CLANGLLVM) += clang-test.o
 libperf-$(CONFIG_CLANGLLVM) += bpf-funcs-str.o
+libperf-$(CONFIG_CLANGLLVM) += bpf-helper-str.o
diff --git a/tools/perf/util/c++/bpf-helper-str.c 
b/tools/perf/util/c++/bpf-helper-str.c
new file mode 100644
index 000..17f915c
--- /dev/null
+++ b/tools/perf/util/c++/bpf-helper-str.c
@@ -0,0 +1,15 @@
+#include "clang-bpf-includes.h"
+const char clang_builtin_bpf_helper_str[] =
+"#ifdef BUILTIN_CLANG_DEFAULT_INCLUDE\n"
+"#ifndef BPF_HELPER_DEFINED\n"
+"#define BPF_HELPER_DEFINED\n"
+"struct bpf_map_def {\n"
+"  unsigned int type;\n"
+"  unsigned int key_size;\n"
+"  unsigned int value_size;\n"
+"  unsigned int max_entries;\n"
+"};\n"
+"#define SEC(NAME) __attribute__((section(NAME), used))\n"
+"#endif\n"
+"#endif"
+;
diff --git a/tools/perf/util/c++/clang-bpf-includes.h 
b/tools/perf/util/c++/clang-bpf-includes.h
index 385a5bb..577b40c 100644
--- a/tools/perf/util/c++/clang-bpf-includes.h
+++ b/tools/perf/util/c++/clang-bpf-includes.h
@@ -5,6 +5,7 @@ extern "C" {
 #endif
 
 extern const char clang_builtin_bpf_funcs_str[];
+extern const char clang_builtin_bpf_helper_str[];
 
 #ifdef __cplusplus
 }
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 926dae1..5c1efe8 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -48,6 +48

[PATCH v3 20/30] perf clang jit: add PerfModule::doJIT to JIT perfhook functions

2016-11-25 Thread Wang Nan
PerfModule::doJIT JIT compile perfhook functions and saves result into
a map. Add a test case for it.

At this stage perfhook functions can do no useful things because they
can't invoke external functions and can't return value. Following
commits are going to make improvment.

Don't hook functions right after jitted because bpf_object is unavailable
during jitting but it should be the context of jitted functions.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/tests/bpf-script-example.c |  8 
 tools/perf/tests/clang.c  |  4 ++
 tools/perf/util/c++/clang-c.h |  2 +
 tools/perf/util/c++/clang-test.cpp| 32 +++-
 tools/perf/util/c++/clang.cpp | 71 +++
 tools/perf/util/c++/clang.h   | 13 +++
 6 files changed, 128 insertions(+), 2 deletions(-)

diff --git a/tools/perf/tests/bpf-script-example.c 
b/tools/perf/tests/bpf-script-example.c
index 268e5f8..265036e 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -46,3 +46,11 @@ int bpf_func__SyS_epoll_wait(void *ctx)
 }
 char _license[] SEC("license") = "GPL";
 int _version SEC("version") = LINUX_VERSION_CODE;
+
+#ifdef TEST_PERF_HOOK
+SEC("perfhook:test")
+void hook_test(void)
+{
+   return;
+}
+#endif
diff --git a/tools/perf/tests/clang.c b/tools/perf/tests/clang.c
index 2964c06..f274e62 100644
--- a/tools/perf/tests/clang.c
+++ b/tools/perf/tests/clang.c
@@ -16,6 +16,10 @@ static struct {
.func = test__clang_to_obj,
.desc = "Test builtin clang compile C source to ELF object",
},
+   {
+   .func = test__clang_jit,
+   .desc = "Test builtin clang compile mixed BPF and native code",
+   },
 #endif
 };
 
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 0eadd79..5ebcb41 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -14,6 +14,7 @@ extern void perf_clang__cleanup(void);
 
 extern int test__clang_to_IR(void);
 extern int test__clang_to_obj(void);
+extern int test__clang_jit(void);
 
 extern int perf_clang__compile_bpf(const char *filename,
   void **p_obj_buf,
@@ -26,6 +27,7 @@ static inline void perf_clang__cleanup(void) { }
 
 static inline int test__clang_to_IR(void) { return -1; }
 static inline int test__clang_to_obj(void) { return -1;}
+static inline int test__clang_jit(void) { return -1;}
 
 static inline int
 perf_clang__compile_bpf(const char *filename __maybe_unused,
diff --git a/tools/perf/util/c++/clang-test.cpp 
b/tools/perf/util/c++/clang-test.cpp
index fb05e56..2b4aa8d 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -5,6 +5,7 @@
 
 #include 
 #include 
+#include 
 #include 
 
 class perf_clang_scope {
@@ -14,7 +15,7 @@ public:
 };
 
 static std::unique_ptr
-__test__clang_to_IR(void)
+__test__clang_to_IR(bool perfhook)
 {
unsigned int kernel_version;
 
@@ -23,14 +24,22 @@ __test__clang_to_IR(void)
 
std::string cflag_kver("-DLINUX_VERSION_CODE=" +
std::to_string(kernel_version));
+   std::string cflag_perfhook(perfhook ? "-DTEST_PERF_HOOK=1" : "");
 
std::unique_ptr M =
-   perf::getModuleFromSource({cflag_kver.c_str()},
+   perf::getModuleFromSource({cflag_kver.c_str(),
+  cflag_perfhook.c_str()},
  "perf-test.c",
  test_llvm__bpf_base_prog);
return M;
 }
 
+static std::unique_ptr
+__test__clang_to_IR(void)
+{
+   return __test__clang_to_IR(false);
+}
+
 extern "C" {
 int test__clang_to_IR(void)
 {
@@ -59,4 +68,23 @@ int test__clang_to_obj(void)
return 0;
 }
 
+int test__clang_jit(void)
+{
+   perf_clang_scope _scope;
+
+   auto M = __test__clang_to_IR(true);
+   if (!M)
+   return -1;
+
+   if (M->doJIT())
+   return -1;
+
+   std::unique_ptr hooks(M->copyJITResult());
+   for (auto i : *hooks)
+   perf_hooks__set_hook(i.first.c_str(), i.second, NULL);
+
+   perf_hooks__invoke_test();
+   return 0;
+}
+
 }
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 98d05e2..03012b2 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -14,9 +14,14 @@
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
+#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
+#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
+#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #includ

[PATCH v3 26/30] perf clang: Link BPF functions declaration into perf

2016-11-25 Thread Wang Nan
Use a shell script to generate BPF functions declarations from kernel
source code, embed the generated header into a C string. Following
commits will utilizes clang's virtual file system to automatically
include this header to all BPF scripts.

The generated header is wrapped by a BUILTIN_CLANG_NO_DEFAULT_INCLUDE.
This macro will be used by following commits to allow user disable this
and other builtin includes.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/c++/Build|   1 +
 tools/perf/util/c++/bpf-funcs-str.c  | 228 +++
 tools/perf/util/c++/clang-bpf-includes.h |  12 ++
 3 files changed, 241 insertions(+)
 create mode 100644 tools/perf/util/c++/bpf-funcs-str.c
 create mode 100644 tools/perf/util/c++/clang-bpf-includes.h

diff --git a/tools/perf/util/c++/Build b/tools/perf/util/c++/Build
index 988fef1..bd71abf 100644
--- a/tools/perf/util/c++/Build
+++ b/tools/perf/util/c++/Build
@@ -1,2 +1,3 @@
 libperf-$(CONFIG_CLANGLLVM) += clang.o
 libperf-$(CONFIG_CLANGLLVM) += clang-test.o
+libperf-$(CONFIG_CLANGLLVM) += bpf-funcs-str.o
diff --git a/tools/perf/util/c++/bpf-funcs-str.c 
b/tools/perf/util/c++/bpf-funcs-str.c
new file mode 100644
index 000..f6bcf76
--- /dev/null
+++ b/tools/perf/util/c++/bpf-funcs-str.c
@@ -0,0 +1,228 @@
+/*
+ * This file is generated by following script:
+ *
+ * #!/bin/bash
+ * TEMP_KBUILD=$(mktemp -d)
+ * KERNEL_DIR=$(pwd)
+ * OUTPUT=tools/perf/util/c++/bpf-funcs-str.c
+ * rm -rf $OUTPUT
+ * echo "Use temp dir: $TEMP_KBUILD"
+ * function finish()
+ * {
+ * rm -rf $TEMP_KBUILD
+ * }
+ * trap finish EXIT
+ * SRCLIST=$(find -name "*.c" | xargs grep bpf_func_proto -l)
+ * cd $TEMP_KBUILD
+ * yes '' | make -C $KERNEL_DIR O=`pwd` oldconfig
+ * cat << EOF >> ./.config
+ * CONFIG_BPF=y
+ * CONFIG_BPF_SYSCALL=y
+ * CONFIG_PERF_EVENTS=y
+ * CONFIG_SOCK_CGROUP_DATA=y
+ * EOF
+ * yes '' | make -C $KERNEL_DIR O=`pwd` oldconfig
+ * FIXOBJLIST=""
+ * for src in ${SRCLIST}
+ * do
+ * mkdir -p $(dirname $src)
+ * cat << EOF > "${src}-fix.c"
+ * #include 
+ * #undef __init
+ * #define __init __attribute__((constructor))
+ * #include "`basename $src`"
+ * EOF
+ * if [ $(basename $src) == "syscall.c" ]
+ * then
+ * cat << EOF >> "${src}-fix.c"
+ * const struct bpf_verifier_ops *
+ * find_prog_type_export(enum bpf_prog_type type)
+ * {
+ * struct bpf_prog_aux aux;
+ * struct bpf_prog p = {.aux = &aux };
+ * if (find_prog_type(type, &p))
+ * return NULL;
+ * return p.aux->ops;
+ * }
+ * EOF
+ * fi
+ * FIXOBJLIST="$FIXOBJLIST ${src}-fix.o"
+ * done
+ * function dolink()
+ * {
+ * touch ./syms.c
+ * echo gcc kernel/bpf/main.o ./syms.c $FIXOBJLIST -o ./gen
+ * gcc kernel/bpf/main.o ./syms.c $FIXOBJLIST -o ./gen
+ * }
+ * MAIN=kernel/bpf/main.c
+ * cat << EOF > $MAIN
+ * #include 
+ * #include 
+ * struct bpf_func {
+ *   const char *name;
+ *   int id;
+ * } bpf_funcs[] = {
+ * EOF
+ * grep '^[[:space:]]BPF_FUNC_[^ ]*,' $KERNEL_DIR/include/uapi/linux/bpf.h | \
+ * sed -e 's/.*BPF_FUNC_\([^,]*\),.*$/\1/g' | \
+ * xargs -n 1 sh -c 'echo {.name = \"$1\", .id = BPF_FUNC_$1}, >> '"$MAIN" 
sh
+ * cat << EOF >> $MAIN
+ * {NULL, -1},
+ * };
+ * int capable(int x) {return 1;}
+ * int trace_printk_init_buffers(void) {return 0;}
+ * static int x;
+ * void *metadata_dst_alloc_percpu(int a, int b) {return &x;}
+ * int ___ratelimit(void *a, const void *func) {return 0;}
+ * extern const struct bpf_verifier_ops *
+ * find_prog_type_export(enum bpf_prog_type type);
+ * extern int printf(const char *fmt, ...);
+ * int main(int argc, char *argv[])
+ * {
+ * struct bpf_func *f = &bpf_funcs[0];
+ * printf("#ifndef BPF_FUNCS_DEFINED\n");
+ * printf("#define BPF_FUNCS_DEFINED\n");
+ * while (f->id != -1) {
+ * enum bpf_prog_type t;
+ * const enum bpf_arg_type *argt;
+ * const struct bpf_verifier_ops *ops = NULL;
+ * const struct bpf_func_proto *proto = NULL;
+ * if (f->id == 0)
+ * goto skip;
+ * for (t = BPF_PROG_TYPE_UNSPEC + 1; ; t++) {
+ * ops = find_prog_type_export(t);
+ * if (!ops)
+ * break;
+ * proto = ops->get_func_proto(f->id);
+ * if (proto)
+ * break;
+ * }
+ * if (!proto) {
+ * printf("static void (*%s)(void) = (void *)-1;\n", 
f->name);
+ * continue;
+ * }
+ * printf("static ");
+ * switch (proto->ret_type) {
+ * case RET_INTEGER:
+ * printf("long ");
+ * break;
+ * case RET_PTR_TO_MAP_VALUE_OR_NULL:
+ * printf("void *");
+ * 

[PATCH v3 27/30] perf clang: Declare BPF functions for BPF scripts automatically

2016-11-25 Thread Wang Nan
Use Clang's OverlayFileSystem, add '-include' options to make builtin
clang define BPF functions. After this patch BPF script writer needn't
define BPF functions by their own.

Add -DBUILTIN_CLANG_DEFAULT_INCLUDE to builtin clang so when adopting
builtin clang BPF functions can be automatically defined, and keep
undefined when using external clang. Passing a
-UBUILTIN_CLANG_DEFAULT_INCLUDE to cancel this defaudefinition.

Test cases are updated to avoid redefinition of these functions.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/tests/bpf-script-example.c | 18 +--
 tools/perf/tests/bpf-script-test-prologue.c   |  2 ++
 tools/perf/tests/bpf-script-test-relocation.c | 18 +--
 tools/perf/util/c++/clang.cpp | 33 ++-
 4 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/tools/perf/tests/bpf-script-example.c 
b/tools/perf/tests/bpf-script-example.c
index ccbc19c..42dc341 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -8,13 +8,6 @@
 #endif
 #define BPF_ANY 0
 #define BPF_MAP_TYPE_ARRAY 2
-#define BPF_FUNC_map_lookup_elem 1
-#define BPF_FUNC_map_update_elem 2
-
-static void *(*bpf_map_lookup_elem)(void *map, void *key) =
-   (void *) BPF_FUNC_map_lookup_elem;
-static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int 
flags) =
-   (void *) BPF_FUNC_map_update_elem;
 
 struct bpf_map_def {
unsigned int type;
@@ -24,6 +17,17 @@ struct bpf_map_def {
 };
 
 #define SEC(NAME) __attribute__((section(NAME), used))
+
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
+#define BPF_FUNC_map_lookup_elem 1
+#define BPF_FUNC_map_update_elem 2
+
+static void *(*bpf_map_lookup_elem)(void *map, void *key) =
+   (void *) BPF_FUNC_map_lookup_elem;
+static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int 
flags) =
+   (void *) BPF_FUNC_map_update_elem;
+#endif
+
 struct bpf_map_def SEC("maps") flip_table = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
diff --git a/tools/perf/tests/bpf-script-test-prologue.c 
b/tools/perf/tests/bpf-script-test-prologue.c
index 7230e62..ada812b 100644
--- a/tools/perf/tests/bpf-script-test-prologue.c
+++ b/tools/perf/tests/bpf-script-test-prologue.c
@@ -13,8 +13,10 @@
 #define FMODE_READ 0x1
 #define FMODE_WRITE0x2
 
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
 static void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
(void *) 6;
+#endif
 
 SEC("func=null_lseek file->f_mode offset orig")
 int bpf_func__null_lseek(void *ctx, int err, unsigned long f_mode,
diff --git a/tools/perf/tests/bpf-script-test-relocation.c 
b/tools/perf/tests/bpf-script-test-relocation.c
index 93af774..57c96a3 100644
--- a/tools/perf/tests/bpf-script-test-relocation.c
+++ b/tools/perf/tests/bpf-script-test-relocation.c
@@ -8,13 +8,6 @@
 #endif
 #define BPF_ANY 0
 #define BPF_MAP_TYPE_ARRAY 2
-#define BPF_FUNC_map_lookup_elem 1
-#define BPF_FUNC_map_update_elem 2
-
-static void *(*bpf_map_lookup_elem)(void *map, void *key) =
-   (void *) BPF_FUNC_map_lookup_elem;
-static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int 
flags) =
-   (void *) BPF_FUNC_map_update_elem;
 
 struct bpf_map_def {
unsigned int type;
@@ -24,6 +17,17 @@ struct bpf_map_def {
 };
 
 #define SEC(NAME) __attribute__((section(NAME), used))
+
+#ifndef BUILTIN_CLANG_DEFAULT_INCLUDE
+#define BPF_FUNC_map_lookup_elem 1
+#define BPF_FUNC_map_update_elem 2
+
+static void *(*bpf_map_lookup_elem)(void *map, void *key) =
+   (void *) BPF_FUNC_map_lookup_elem;
+static void *(*bpf_map_update_elem)(void *map, void *key, void *value, int 
flags) =
+   (void *) BPF_FUNC_map_update_elem;
+#endif
+
 struct bpf_map_def SEC("maps") my_table = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 48bd3ee..926dae1 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -39,9 +39,17 @@
 #include "util-cxx.h"
 #include "perf-hooks.h"
 #include "jit-helpers.h"
+#include "clang-bpf-includes.h"
 
 namespace perf {
 
+static struct BPFHeader {
+   llvm::StringRef Path;
+   llvm::StringRef Content;
+} BPFHeaders[] = {
+   {"/virtual/bpf-funcs.h", clang_builtin_bpf_funcs_str},
+};
+
 static std::unique_ptr LLVMCtx;
 
 using namespace clang;
@@ -66,6 +74,11 @@ createCompilerInvocation(llvm::opt::ArgStringList CFlags, 
StringRef& Path,
"-x", "c"};
 
CCArgs.append(CFlags.begin(), CFlags.end());
+   for (BPFHeader &h : BPFHeaders) {
+   CCArgs.append(1, "-include");
+   CCArgs.append(1, h.Path.begin());
+   }
+
CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
 
FrontendOpti

[PATCH v3 09/30] perf build: Add clang and llvm compile and linking support

2016-11-25 Thread Wang Nan
Add necessary c++ flags and link libraries to support builtin clang and
LLVM. Add all llvm and clang libraries, so don't need to worry about
clang changes its libraries setting. However, linking perf would take
much longer than usual.

Signed-off-by: Wang Nan 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
Link: 
http://lkml.kernel.org/r/1474874832-134786-6-git-send-email-wangn...@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/Makefile.config | 35 +++
 tools/perf/Makefile.perf   | 23 ++-
 tools/perf/tests/make  |  2 ++
 3 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 8a493d4..b7c9c80 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -136,6 +136,7 @@ endif
 # Treat warnings as errors unless directed not to
 ifneq ($(WERROR),0)
   CFLAGS += -Werror
+  CXXFLAGS += -Werror
 endif
 
 ifndef DEBUG
@@ -182,6 +183,13 @@ CFLAGS += -Wall
 CFLAGS += -Wextra
 CFLAGS += -std=gnu99
 
+CXXFLAGS += -std=gnu++11 -fno-exceptions -fno-rtti
+CXXFLAGS += -Wall
+CXXFLAGS += -fno-omit-frame-pointer
+CXXFLAGS += -ggdb3
+CXXFLAGS += -funwind-tables
+CXXFLAGS += -Wno-strict-aliasing
+
 # Enforce a non-executable stack, as we may regress (again) in the future by
 # adding assembler files missing the .GNU-stack linker note.
 LDFLAGS += -Wl,-z,noexecstack
@@ -783,6 +791,33 @@ ifndef NO_JVMTI
   endif
 endif
 
+USE_CXX = 0
+USE_CLANGLLVM = 0
+ifdef LIBCLANGLLVM
+  $(call feature_check,cxx)
+  ifneq ($(feature-cxx), 1)
+msg := $(warning No g++ found, disable clang and llvm support. Please 
install g++)
+  else
+$(call feature_check,llvm)
+ifneq ($(feature-llvm), 1)
+  msg := $(warning No libLLVM found, disable clang and llvm support. 
Please install llvm-dev)
+else
+  $(call feature_check,clang)
+  ifneq ($(feature-clang), 1)
+msg := $(warning No libclang found, disable clang and llvm support. 
Please install libclang-dev)
+  else
+CFLAGS += -DHAVE_LIBCLANGLLVM_SUPPORT
+CXXFLAGS += -DHAVE_LIBCLANGLLVM_SUPPORT -I$(shell $(LLVM_CONFIG) 
--includedir)
+$(call detected,CONFIG_CXX)
+$(call detected,CONFIG_CLANGLLVM)
+   USE_CXX = 1
+   USE_LLVM = 1
+   USE_CLANG = 1
+  endif
+endif
+  endif
+endif
+
 # Among the variables below, these:
 #   perfexecdir
 #   template_dir
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 3cb1df4..dfb20dd 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -88,6 +88,10 @@ include ../scripts/utilities.mak
 # and bypass the feature detection
 #
 # Define NO_JVMTI if you do not want jvmti agent built
+#
+# Define LIBCLANGLLVM if you DO want builtin clang and llvm support.
+# When selected, pass LLVM_CONFIG=/path/to/llvm-config to `make' if
+# llvm-config is not in $PATH.
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
@@ -143,6 +147,7 @@ endef
 $(call allow-override,CC,$(CROSS_COMPILE)gcc)
 $(call allow-override,AR,$(CROSS_COMPILE)ar)
 $(call allow-override,LD,$(CROSS_COMPILE)ld)
+$(call allow-override,CXX,$(CROSS_COMPILE)g++)
 
 LD += $(EXTRA_LDFLAGS)
 
@@ -151,6 +156,7 @@ HOSTLD  ?= ld
 HOSTAR  ?= ar
 
 PKG_CONFIG = $(CROSS_COMPILE)pkg-config
+LLVM_CONFIG ?= llvm-config
 
 RM  = rm -f
 LN  = ln -f
@@ -338,6 +344,21 @@ endif
 
 LIBS = -Wl,--whole-archive $(PERFLIBS) -Wl,--no-whole-archive 
-Wl,--start-group $(EXTLIBS) -Wl,--end-group
 
+ifeq ($(USE_CLANG), 1)
+  CLANGLIBS_LIST = AST Basic CodeGen Driver Frontend Lex Tooling Edit Sema 
Analysis Parse Serialization
+  LIBCLANG = $(foreach l,$(CLANGLIBS_LIST),$(wildcard $(shell $(LLVM_CONFIG) 
--libdir)/libclang$(l).a))
+  LIBS += -Wl,--start-group $(LIBCLANG) -Wl,--end-group
+endif
+
+ifeq ($(USE_LLVM), 1)
+  LIBLLVM = $(shell $(LLVM_CONFIG) --libs all) $(shell $(LLVM_CONFIG) 
--system-libs)
+  LIBS += -L$(shell $(LLVM_CONFIG) --libdir) $(LIBLLVM)
+endif
+
+ifeq ($(USE_CXX), 1)
+  LIBS += -lstdc++
+endif
+
 export INSTALL SHELL_PATH
 
 ### Build rules
@@ -356,7 +377,7 @@ strip: $(PROGRAMS) $(OUTPUT)perf
 
 PERF_IN := $(OUTPUT)perf-in.o
 
-export srctree OUTPUT RM CC LD AR CFLAGS V BISON FLEX AWK
+export srctree OUTPUT RM CC CXX LD AR CFLAGS CXXFLAGS V BISON FLEX AWK
 export HOSTCC HOSTLD HOSTAR
 include $(srctree)/tools/build/Makefile.include
 
diff --git a/tools/perf/tests/make b/tools/perf/tests/make
index 08ed7f1..aa49b66 100644
--- a/tools/perf/tests/make
+++ b/tools/perf/tests/make
@@ -83,6 +83,7 @@ make_no_libbpf:= NO_LIBBPF=1
 make_no_libcrypto   := NO_LIBCRYPTO=1
 make_with_babeltrace:= LIBBABELTRACE=1
 make_no_sdt:= NO_SDT=1
+make_with_clangllvm := LIBCLANGLLVM=1
 make_tags   := tags
 make_cscope := cscope
 make_help   := help
@@ -139,6 +140,7 @@ run += make_no_libbionic
 run += make_no_auxtrace
 run += make_no_libbpf
 ru

[PATCH v3 03/30] tools lib bpf: Retrive bpf_map through offset of bpf_map_def

2016-11-25 Thread Wang Nan
Add a new API to libbpf, caller is able to get bpf_map through the
offset of bpf_map_def to 'maps' section.

The API will be used to help jitted perf hook code find fd of a map.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/lib/bpf/libbpf.c | 12 
 tools/lib/bpf/libbpf.h |  8 
 2 files changed, 20 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 866d5cd..2e97459 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1524,3 +1524,15 @@ bpf_object__find_map_by_name(struct bpf_object *obj, 
const char *name)
}
return NULL;
 }
+
+struct bpf_map *
+bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
+{
+   int i;
+
+   for (i = 0; i < obj->nr_maps; i++) {
+   if (obj->maps[i].offset == offset)
+   return &obj->maps[i];
+   }
+   return ERR_PTR(-ENOENT);
+}
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 0c0b012..a5a8b86 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -24,6 +24,7 @@
 #include 
 #include 
 #include 
+#include   // for size_t
 
 enum libbpf_errno {
__LIBBPF_ERRNO__START = 4000,
@@ -200,6 +201,13 @@ struct bpf_map;
 struct bpf_map *
 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
 
+/*
+ * Get bpf_map through the offset of corresponding struct bpf_map_def
+ * in the bpf object file.
+ */
+struct bpf_map *
+bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
+
 struct bpf_map *
 bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
 #define bpf_map__for_each(pos, obj)\
-- 
2.10.1



[PATCH v3 11/30] perf clang: Use real file system for #include

2016-11-25 Thread Wang Nan
Utilize clang's OverlayFileSystem facility, allow CompilerInstance to
access real file system.

With this patch '#include' directive can be used.

Add a new getModuleFromSource for real file.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/c++/clang.cpp | 44 +++
 tools/perf/util/c++/clang.h   |  3 +++
 2 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index c17b117..cf96199 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -15,6 +15,7 @@
 #include "clang/Tooling/Tooling.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Option/Option.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/ManagedStatic.h"
 #include 
 
@@ -27,14 +28,6 @@ static std::unique_ptr LLVMCtx;
 
 using namespace clang;
 
-static vfs::InMemoryFileSystem *
-buildVFS(StringRef& Name, StringRef& Content)
-{
-   vfs::InMemoryFileSystem *VFS = new vfs::InMemoryFileSystem(true);
-   VFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content));
-   return VFS;
-}
-
 static CompilerInvocation *
 createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags)
 {
@@ -60,17 +53,17 @@ createCompilerInvocation(StringRef& Path, 
DiagnosticsEngine& Diags)
return CI;
 }
 
-std::unique_ptr
-getModuleFromSource(StringRef Name, StringRef Content)
+static std::unique_ptr
+getModuleFromSource(StringRef Path,
+   IntrusiveRefCntPtr VFS)
 {
CompilerInstance Clang;
Clang.createDiagnostics();
 
-   IntrusiveRefCntPtr VFS = buildVFS(Name, Content);
Clang.setVirtualFileSystem(&*VFS);
 
IntrusiveRefCntPtr CI =
-   createCompilerInvocation(Name, Clang.getDiagnostics());
+   createCompilerInvocation(Path, Clang.getDiagnostics());
Clang.setInvocation(&*CI);
 
std::unique_ptr Act(new EmitLLVMOnlyAction(&*LLVMCtx));
@@ -80,6 +73,33 @@ getModuleFromSource(StringRef Name, StringRef Content)
return Act->takeModule();
 }
 
+std::unique_ptr
+getModuleFromSource(StringRef Name, StringRef Content)
+{
+   using namespace vfs;
+
+   llvm::IntrusiveRefCntPtr OverlayFS(
+   new OverlayFileSystem(getRealFileSystem()));
+   llvm::IntrusiveRefCntPtr MemFS(
+   new InMemoryFileSystem(true));
+
+   /*
+* pushOverlay helps setting working dir for MemFS. Must call
+* before addFile.
+*/
+   OverlayFS->pushOverlay(MemFS);
+   MemFS->addFile(Twine(Name), 0, 
llvm::MemoryBuffer::getMemBuffer(Content));
+
+   return getModuleFromSource(Name, OverlayFS);
+}
+
+std::unique_ptr
+getModuleFromSource(StringRef Path)
+{
+   IntrusiveRefCntPtr VFS(vfs::getRealFileSystem());
+   return getModuleFromSource(Path, VFS);
+}
+
 }
 
 extern "C" {
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index f64483b..90aff01 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -12,5 +12,8 @@ using namespace llvm;
 std::unique_ptr
 getModuleFromSource(StringRef Name, StringRef Content);
 
+std::unique_ptr
+getModuleFromSource(StringRef Path);
+
 }
 #endif
-- 
2.10.1



[PATCH v3 19/30] perf clang jit: Insignt BPF and JIT functions in a Module

2016-11-25 Thread Wang Nan
Identify BPF functions, JIT functions and maps during init. Functions in
section starting with "perfhook:" are JIT functions. They will be JIT
compiled and hooked at perfhooks.

During init of PerfModule, mark JIT functions as AvailableExternallyLinkage.
LLVM skips functions with linkage like this so they won't be compiled
into BPF objects.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/c++/clang.cpp | 32 
 tools/perf/util/c++/clang.h   |  7 +++
 2 files changed, 39 insertions(+)

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index d31b0a5..98d05e2 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -115,15 +115,47 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, 
StringRef Path)
 
 PerfModule::PerfModule(std::unique_ptr&& M) : 
Module(std::move(M))
 {
+   for (llvm::Function& F : *Module) {
+   if (F.getLinkage() != llvm::GlobalValue::ExternalLinkage)
+   continue;
+
+   if (StringRef(F.getSection()).startswith("perfhook:"))
+   JITFunctions.insert(&F);
+   else
+   BPFFunctions.insert(&F);
+   }
 
+   for (auto V = Module->global_begin(); V != Module->global_end(); V++) {
+   llvm::GlobalVariable *GV = &*V;
+   if (StringRef(GV->getSection()) == llvm::StringRef("maps"))
+   Maps.insert(GV);
+   }
 }
 
+void PerfModule::prepareBPF(void)
+{
+   for (llvm::Function *F : JITFunctions)
+   F->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
+   for (llvm::Function *F : BPFFunctions)
+   F->setLinkage(llvm::GlobalValue::ExternalLinkage);
+
+}
+
+void PerfModule::prepareJIT(void)
+{
+   for (llvm::Function *F : BPFFunctions)
+   F->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
+   for (llvm::Function *F : JITFunctions)
+   F->setLinkage(llvm::GlobalValue::ExternalLinkage);
+
+}
 
 std::unique_ptr>
 PerfModule::toBPFObject(void)
 {
using namespace llvm;
 
+   prepareBPF();
std::string TargetTriple("bpf-pc-linux");
std::string Error;
const Target* Target = TargetRegistry::lookupTarget(TargetTriple, 
Error);
diff --git a/tools/perf/util/c++/clang.h b/tools/perf/util/c++/clang.h
index cbb291b..1eb71a6 100644
--- a/tools/perf/util/c++/clang.h
+++ b/tools/perf/util/c++/clang.h
@@ -6,6 +6,7 @@
 #include "llvm/IR/Module.h"
 #include "llvm/Option/Option.h"
 #include 
+#include 
 
 namespace perf {
 
@@ -14,6 +15,12 @@ using namespace llvm;
 class PerfModule {
 private:
std::unique_ptr Module;
+
+   std::set Maps;
+   std::set BPFFunctions;
+   std::set JITFunctions;
+   void prepareBPF(void);
+   void prepareJIT(void);
 public:
inline llvm::Module *getModule(void)
{
-- 
2.10.1



[PATCH v3 06/30] perf llvm: Extract helpers in llvm-utils.c

2016-11-25 Thread Wang Nan
Following commits will use builtin clang to compile BPF script.
llvm__get_kbuild_opts() and llvm__get_nr_cpus() are extracted to help
building '-DKERNEL_VERSION_CODE' and '-D__NR_CPUS__' macros.

Doing object dumping in bpf loader, so futher builtin clang compiling
needn't consider it.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/bpf-loader.c |  4 +++
 tools/perf/util/llvm-utils.c | 76 +---
 tools/perf/util/llvm-utils.h |  6 
 3 files changed, 68 insertions(+), 18 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index a5fd275..cf16b941 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -90,6 +90,10 @@ struct bpf_object *bpf__prepare_load(const char *filename, 
bool source)
if (err)
return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
+
+   if (!IS_ERR(obj) && llvm_param.dump_obj)
+   llvm__dump_obj(filename, obj_buf, obj_buf_sz);
+
free(obj_buf);
} else
obj = bpf_object__open(filename);
diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
index 27b6f303..b23ff44 100644
--- a/tools/perf/util/llvm-utils.c
+++ b/tools/perf/util/llvm-utils.c
@@ -7,6 +7,7 @@
 #include 
 #include 
 #include 
+#include 
 #include "debug.h"
 #include "llvm-utils.h"
 #include "config.h"
@@ -282,9 +283,10 @@ static const char *kinc_fetch_script =
 "rm -rf $TMPDIR\n"
 "exit $RET\n";
 
-static inline void
-get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
+void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
 {
+   static char *saved_kbuild_dir;
+   static char *saved_kbuild_include_opts;
int err;
 
if (!kbuild_dir || !kbuild_include_opts)
@@ -293,10 +295,28 @@ get_kbuild_opts(char **kbuild_dir, char 
**kbuild_include_opts)
*kbuild_dir = NULL;
*kbuild_include_opts = NULL;
 
+   if (saved_kbuild_dir && saved_kbuild_include_opts &&
+   !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
+   *kbuild_dir = strdup(saved_kbuild_dir);
+   *kbuild_include_opts = strdup(saved_kbuild_include_opts);
+
+   if (*kbuild_dir && *kbuild_include_opts)
+   return;
+
+   zfree(kbuild_dir);
+   zfree(kbuild_include_opts);
+   /*
+* Don't fall through: it may breaks saved_kbuild_dir and
+* saved_kbuild_include_opts if detect them again when
+* memory is low.
+*/
+   return;
+   }
+
if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
pr_debug("Skip kbuild options detection.\n");
-   return;
+   goto errout;
}
 
err = detect_kbuild_dir(kbuild_dir);
@@ -306,7 +326,7 @@ get_kbuild_opts(char **kbuild_dir, char 
**kbuild_include_opts)
 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
 " \tdetection.\n\n");
-   return;
+   goto errout;
}
 
pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
@@ -325,14 +345,43 @@ get_kbuild_opts(char **kbuild_dir, char 
**kbuild_include_opts)
 
free(*kbuild_dir);
*kbuild_dir = NULL;
-   return;
+   goto errout;
}
 
pr_debug("include option is set to %s\n", *kbuild_include_opts);
+
+   saved_kbuild_dir = strdup(*kbuild_dir);
+   saved_kbuild_include_opts = strdup(*kbuild_include_opts);
+
+   if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
+   zfree(&saved_kbuild_dir);
+   zfree(&saved_kbuild_include_opts);
+   }
+   return;
+errout:
+   saved_kbuild_dir = ERR_PTR(-EINVAL);
+   saved_kbuild_include_opts = ERR_PTR(-EINVAL);
 }
 
-static void
-dump_obj(const char *path, void *obj_buf, size_t size)
+int llvm__get_nr_cpus(void)
+{
+   static int nr_cpus_avail = 0;
+   char serr[STRERR_BUFSIZE];
+
+   if (nr_cpus_avail > 0)
+   return nr_cpus_avail;
+
+   nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
+   if (nr_cpus_avail <= 0) {
+   pr_err(
+"WARNING:\tunable to get available CPUs in this system: %s\n"
+"\tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
+   nr_cpus_avail = 128;
+   }
+   return nr_cpus_avail;
+}
+
+void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
 {
char *obj_path = strdup(path);
FILE *fp

[PATCH v3 17/30] perf clang: Pass CFLAGS to builtin clang

2016-11-25 Thread Wang Nan
Pass -DLINUX_VERSION_CODE, -D__NR_CPUS__, llvm.clang-opt config options
and CFLAGS detected by kbuild detector to builtin clang so BPF scripts
can use kernel headers and user defined options like external clang
compiler.

Test:
  # perf record -v --dry-run -e tools/perf/tests/bpf-script-test-kbuild.c ls 
2>&1 | grep built
  bpf: builtin compiling successful

Committer notes:

Before installing the required clang/llvm devel files to have it
builtin:

  # perf record -v --dry-run -e tools/perf/tests/bpf-script-test-kbuild.c ls 
2>&1 | grep built
  bpf: builtin compiling failed: -95, try external compiler

I.e. it falls back to using the external compiler.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/c++/clang.cpp | 105 --
 tools/perf/util/llvm-utils.h  |   9 +++-
 2 files changed, 109 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 610f1cf..3a3b9791 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -27,6 +27,8 @@
 
 #include "clang.h"
 #include "clang-c.h"
+#include "llvm-utils.h"
+#include "util-cxx.h"
 
 namespace perf {
 
@@ -147,6 +149,101 @@ getBPFObjectFromModule(llvm::Module *Module)
return std::move(Buffer);
 }
 
+class ClangOptions {
+   llvm::SmallString FileName;
+   llvm::SmallString<64> KVerDef;
+   llvm::SmallString<64> NRCpusDef;
+   char *kbuild_dir;
+   char *kbuild_include_opts;
+   char *clang_opt;
+public:
+   ClangOptions(const char *filename) : FileName(filename),
+KVerDef(""),
+NRCpusDef(""),
+kbuild_dir(NULL),
+kbuild_include_opts(NULL),
+clang_opt(NULL)
+   {
+   llvm::sys::fs::make_absolute(FileName);
+
+   unsigned int kver;
+   if (!fetch_kernel_version(&kver, NULL, 0))
+   KVerDef = "-DLINUX_VERSION_CODE=" + 
std::to_string(kver);
+
+   int nr_cpus = llvm__get_nr_cpus();
+   if (nr_cpus > 0)
+   NRCpusDef = "-D__NR_CPUS__=" + std::to_string(nr_cpus);
+
+   if (llvm_param.clang_opt)
+   clang_opt = strdup(llvm_param.clang_opt);
+
+   llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
+   if (!kbuild_dir || !kbuild_include_opts) {
+   free(kbuild_dir);
+   free(kbuild_include_opts);
+   kbuild_dir = kbuild_include_opts = NULL;
+   }
+   }
+
+   ~ClangOptions()
+   {
+   free(kbuild_dir);
+   free(kbuild_include_opts);
+   free(clang_opt);
+   }
+
+   static void fillCFlagsFromString(opt::ArgStringList &CFlags, char *s, 
bool check = false)
+   {
+   if (!s)
+   return;
+
+   SmallVector Terms;
+   StringRef Opts(s);
+   Opts.split(Terms, ' ');
+
+   for (auto i = Terms.begin(); i != Terms.end(); i++)
+   s[i->end() - Opts.begin()] = '\0';
+
+   for (auto i = Terms.begin(); i != Terms.end(); i++) {
+   if (!check) {
+   CFlags.push_back(i->begin());
+   continue;
+   }
+
+   if (i->startswith("-I"))
+   CFlags.push_back(i->begin());
+   else if (i->startswith("-D"))
+   CFlags.push_back(i->begin());
+   else if (*i == "-include") {
+   CFlags.push_back((i++)->begin());
+   /* Let clang report this error */
+   if (i == Terms.end())
+   break;
+   CFlags.push_back(i->begin());
+   }
+   }
+   }
+
+   void getCFlags(opt::ArgStringList &CFlags)
+   {
+   CFlags.push_back(KVerDef.c_str());
+   CFlags.push_back(NRCpusDef.c_str());
+
+   fillCFlagsFromString(CFlags, clang_opt);
+   fillCFlagsFromString(CFlags, kbuild_include_opts, true);
+
+   if (kbuild_dir) {
+   CFlags.push_back("-working-directory");
+   CFlags.push_back(kbuild_dir);
+   }
+   }
+
+   const char *getFileName(void)
+   {
+   return FileName.c_str();
+   }
+};
+
 }
 
 extern "C" {
@@ -174,11 +271,11 @@ int perf_clang__compile_bpf(const char *_filename,
if (!p_obj_buf || !p_

Xmas Offer

2016-11-25 Thread Mrs Julie Leach
You are a recipient to Mrs Julie Leach Donation of $3 million USD. Contact ( 
julieleac...@gmail.com ) for claims.


[PATCH v3 07/30] tools build: Add feature detection for LLVM

2016-11-25 Thread Wang Nan
Check if basic LLVM compiling environment is ready.

Use llvm-config to detect include and library directories. Avoid using
'llvm-config --cxxflags' because its result contain some unwanted flags
like --sysroot (if LLVM is built by yocto).

Use '?=' to set LLVM_CONFIG, so explicitly passing LLVM_CONFIG to make
would override it.

Use 'llvm-config --libs BPF' to check if BPF backend is compiled in.
Since now BPF bytecode is the only required backend, no need to waste
time linking llvm and clang if BPF backend is missing. This also
introduce an implicit requirement that LLVM should be new enough.  Old
LLVM doesn't support BPF backend.

Signed-off-by: Wang Nan 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
Link: 
http://lkml.kernel.org/r/1474874832-134786-4-git-send-email-wangn...@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/build/feature/Makefile  | 8 
 tools/build/feature/test-llvm.cpp | 8 
 2 files changed, 16 insertions(+)
 create mode 100644 tools/build/feature/test-llvm.cpp

diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index 8f668bc..c09de59 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -55,6 +55,7 @@ FILES := $(addprefix $(OUTPUT),$(FILES))
 CC := $(CROSS_COMPILE)gcc -MD
 CXX := $(CROSS_COMPILE)g++ -MD
 PKG_CONFIG := $(CROSS_COMPILE)pkg-config
+LLVM_CONFIG ?= llvm-config
 
 all: $(FILES)
 
@@ -229,6 +230,13 @@ $(OUTPUT)test-cxx.bin:
 $(OUTPUT)test-jvmti.bin:
$(BUILD)
 
+$(OUTPUT)test-llvm.bin:
+   $(BUILDXX) -std=gnu++11 \
+   -I$(shell $(LLVM_CONFIG) --includedir)  \
+   -L$(shell $(LLVM_CONFIG) --libdir)  \
+   $(shell $(LLVM_CONFIG) --libs Core BPF) \
+   $(shell $(LLVM_CONFIG) --system-libs)
+
 -include $(OUTPUT)*.d
 
 ###
diff --git a/tools/build/feature/test-llvm.cpp 
b/tools/build/feature/test-llvm.cpp
new file mode 100644
index 000..d8d2cee
--- /dev/null
+++ b/tools/build/feature/test-llvm.cpp
@@ -0,0 +1,8 @@
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/raw_ostream.h"
+int main()
+{
+   llvm::errs() << "Hello World!\n";
+   llvm::llvm_shutdown();
+   return 0;
+}
-- 
2.10.1



[PATCH v3 24/30] perf clang jit: Retrive fd of BPF map from its offset

2016-11-25 Thread Wang Nan
bpf__map_fd() is introduced to retrive fd of a BPF map through its
offset in BPF object. This function is going be used in further
commits which allow scripts jitted by builtin clang access BPF maps.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/bpf-loader.c | 37 +
 tools/perf/util/bpf-loader.h | 19 +++
 2 files changed, 56 insertions(+)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index 81c6fed..86aa99b 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -19,6 +19,7 @@
 #include "parse-events.h"
 #include "llvm-utils.h"
 #include "c++/clang-c.h"
+#include "asm/bug.h"   // for WARN_ONCE
 
 #define DEFINE_PRINT_FN(name, level) \
 static int libbpf_##name(const char *fmt, ...) \
@@ -1644,6 +1645,28 @@ int bpf__setup_stdout(struct perf_evlist *evlist 
__maybe_unused)
return 0;
 }
 
+int bpf__map_fd(struct bpf_object *obj, void *jit_map)
+{
+   struct bpf_obj_priv *priv = bpf_object__priv(obj);
+   struct bpf_map *map;
+   size_t map_offset;
+   void *map_base;
+
+   if (IS_ERR(priv))
+   return PTR_ERR(priv);
+   if (!priv)
+   return -EINVAL;
+
+   map_base = priv->map_base;
+   map_offset = jit_map - map_base;
+   map = bpf_object__find_map_by_offset(obj, map_offset);
+   WARN_ONCE(IS_ERR(map), "can't find map offset %zu from '%s'\n",
+ map_offset, bpf_object__name(obj));
+   if (IS_ERR(map))
+   return -ENOENT;
+   return bpf_map__fd(map);
+}
+
 #define ERRNO_OFFSET(e)((e) - __BPF_LOADER_ERRNO__START)
 #define ERRCODE_OFFSET(c)  ERRNO_OFFSET(BPF_LOADER_ERRNO__##c)
 #define NR_ERRNO   (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START)
@@ -1825,3 +1848,17 @@ int bpf__strerror_setup_stdout(struct perf_evlist 
*evlist __maybe_unused,
bpf__strerror_end(buf, size);
return 0;
 }
+
+int bpf__strerror_map_fd(struct bpf_object *obj, void *jit_map,
+int err, char *buf, size_t size)
+{
+   struct bpf_obj_priv *priv = bpf_object__priv(obj);
+   ptrdiff_t offset = priv ? jit_map - priv->map_base : jit_map - NULL;
+
+   bpf__strerror_head(err, buf, size);
+   bpf__strerror_entry(EINVAL, "No map in BPF object %s", 
bpf_object__name(obj));
+   bpf__strerror_entry(ENOENT, "Can't find map offset %lx in BPF object 
%s",
+   (unsigned long)offset, bpf_object__name(obj));
+   bpf__strerror_end(buf, size);
+   return 0;
+}
diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h
index f2b737b..c40812b 100644
--- a/tools/perf/util/bpf-loader.h
+++ b/tools/perf/util/bpf-loader.h
@@ -84,6 +84,9 @@ int bpf__setup_stdout(struct perf_evlist *evlist);
 int bpf__strerror_setup_stdout(struct perf_evlist *evlist, int err,
   char *buf, size_t size);
 
+int bpf__map_fd(struct bpf_object *obj, void *jit_map);
+int bpf__strerror_map_fd(struct bpf_object *obj, void *jit_map,
+int err, char *buf, size_t size);
 #else
 static inline struct bpf_object *
 bpf__prepare_load(const char *filename __maybe_unused,
@@ -136,6 +139,13 @@ bpf__setup_stdout(struct perf_evlist *evlist 
__maybe_unused)
 }
 
 static inline int
+bpf__map_fd(struct bpf_object *obj __maybe_unused,
+   void *map_ptr __maybe_unused)
+{
+   return -ENOTSUP;
+}
+
+static inline int
 __bpf_strerror(char *buf, size_t size)
 {
if (!size)
@@ -196,5 +206,14 @@ bpf__strerror_setup_stdout(struct perf_evlist *evlist 
__maybe_unused,
 {
return __bpf_strerror(buf, size);
 }
+
+static inline int
+bpf__strerror_map_fd(struct bpf_object *obj __maybe_unused,
+void *jit_map __maybe_unused,
+int err __maybe_unused,
+char *buf, size_t size);
+{
+   return __bpf_strerror(buf, size);
+}
 #endif
 #endif
-- 
2.10.1



[PATCH v3 30/30] perf clang jit: Export getpid() to perf hook

2016-11-25 Thread Wang Nan
After this patch perf hooks can retrive pid of perf itself by calling
getpid. It is important for excluding event from perf.

This commit is also an example to show how to export more helpers to
hooked script.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/c++/bpf-helper-str.c | 1 +
 tools/perf/util/c++/clang.cpp| 1 +
 2 files changed, 2 insertions(+)

diff --git a/tools/perf/util/c++/bpf-helper-str.c 
b/tools/perf/util/c++/bpf-helper-str.c
index f4d6d57..1ad6ec0 100644
--- a/tools/perf/util/c++/bpf-helper-str.c
+++ b/tools/perf/util/c++/bpf-helper-str.c
@@ -12,6 +12,7 @@ const char clang_builtin_bpf_helper_str[] =
 "#define SEC(NAME) __attribute__((section(NAME), used))\n"
 "extern int printf(const char *, ...);\n"
 "extern int puts(const char *);\n"
+"extern int getpid(void);\n"
 "extern int perf_map_update_elem(void *, void *, void *, void *, unsigned 
long);\n"
 "extern int perf_map_lookup_elem(void *, void *, void *, void *);\n"
 "extern int perf_map_get_next_key(void *, void *, void *, void *);\n"
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 5c1efe8..213c58f 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -236,6 +236,7 @@ static std::map 
exported_funcs =
EXPORT(test__clang_callback),
EXPORT(printf),
EXPORT(puts),
+   EXPORT(getpid),
EXPORT(JIT_HELPER_FUNC_NAME(map_update_elem)),
EXPORT(JIT_HELPER_FUNC_NAME(map_lookup_elem)),
EXPORT(JIT_HELPER_FUNC_NAME(map_get_next_key)),
-- 
2.10.1



[PATCH v3 21/30] perf clang jit: Export functions for jitted code

2016-11-25 Thread Wang Nan
After this patch functions attached on perf hooks is allowed to invoke
external functions. Add a testcase for this feature.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/tests/Build|  2 +-
 tools/perf/tests/bpf-script-example.c |  4 
 tools/perf/util/c++/clang-c.h |  2 ++
 tools/perf/util/c++/clang-test.cpp|  9 +
 tools/perf/util/c++/clang.cpp | 17 -
 5 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 6676c2d..d6e6e00 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -49,7 +49,7 @@ $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c 
tests/Build
$(call rule_mkdir)
$(Q)echo '#include ' > $@
$(Q)echo 'const char test_llvm__bpf_base_prog[] =' >> $@
-   $(Q)sed -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
+   $(Q)sed -e 's/\\//g' -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/g' $< >> $@
$(Q)echo ';' >> $@
 
 $(OUTPUT)tests/llvm-src-kbuild.c: tests/bpf-script-test-kbuild.c tests/Build
diff --git a/tools/perf/tests/bpf-script-example.c 
b/tools/perf/tests/bpf-script-example.c
index 265036e..ccbc19c 100644
--- a/tools/perf/tests/bpf-script-example.c
+++ b/tools/perf/tests/bpf-script-example.c
@@ -48,9 +48,13 @@ char _license[] SEC("license") = "GPL";
 int _version SEC("version") = LINUX_VERSION_CODE;
 
 #ifdef TEST_PERF_HOOK
+extern int printf(const char *fmt, ...);
+extern void test__clang_callback(int x);
 SEC("perfhook:test")
 void hook_test(void)
 {
+   printf("Hello, hook_test\n");
+   test__clang_callback(1234);
return;
 }
 #endif
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 5ebcb41..9f75e41 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -16,6 +16,8 @@ extern int test__clang_to_IR(void);
 extern int test__clang_to_obj(void);
 extern int test__clang_jit(void);
 
+extern void test__clang_callback(int x);
+
 extern int perf_clang__compile_bpf(const char *filename,
   void **p_obj_buf,
   size_t *p_obj_buf_sz);
diff --git a/tools/perf/util/c++/clang-test.cpp 
b/tools/perf/util/c++/clang-test.cpp
index 2b4aa8d..0bdb807 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -68,6 +68,13 @@ int test__clang_to_obj(void)
return 0;
 }
 
+static int callback_flag;
+
+void test__clang_callback(int x)
+{
+   callback_flag = x;
+}
+
 int test__clang_jit(void)
 {
perf_clang_scope _scope;
@@ -84,6 +91,8 @@ int test__clang_jit(void)
perf_hooks__set_hook(i.first.c_str(), i.second, NULL);
 
perf_hooks__invoke_test();
+   if (callback_flag != 1234)
+   return -1;
return 0;
 }
 
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 03012b2..325fbe4 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -30,6 +30,8 @@
 #include "llvm/Target/TargetOptions.h"
 #include 
 #include 
+#include 
+#include 
 
 #include "clang.h"
 #include "clang-c.h"
@@ -194,6 +196,15 @@ PerfModule::toBPFObject(void)
return std::move(Buffer);
 }
 
+static std::map exported_funcs =
+{
+#define EXPORT(f) {#f, (const void *)&f}
+   EXPORT(test__clang_callback),
+   EXPORT(printf),
+   EXPORT(puts),
+#undef EXPORT
+};
+
 /*
  * Use a global memory manager so allocated code and data won't be released
  * when object destroy.
@@ -220,7 +231,11 @@ int PerfModule::doJIT(void)
 
auto Resolver = createLambdaResolver(
[](const std::string &Name) {
-   return RuntimeDyld::SymbolInfo(nullptr);
+   auto i = exported_funcs.find(Name);
+   if (i == exported_funcs.end())
+   return RuntimeDyld::SymbolInfo(nullptr);
+   return 
RuntimeDyld::SymbolInfo((uint64_t)(i->second),
+  
JITSymbolFlags::Exported);
},
[](const std::string &Name) {
return RuntimeDyld::SymbolInfo(nullptr);
-- 
2.10.1



[PATCH v3 14/30] perf clang: Support compile IR to BPF object and add testcase

2016-11-25 Thread Wang Nan
getBPFObjectFromModule() is introduced to compile LLVM IR(Module)
to BPF object. Add new testcase for it.

Test result:
  $ ./buildperf/perf test -v clang
  51: Test builtin clang support   :
  51.1: Test builtin clang compile C source to IR  :
  --- start ---
  test child forked, pid 21822
  test child finished with 0
   end 
  Test builtin clang support subtest 0: Ok
  51.2: Test builtin clang compile C source to ELF object  :
  --- start ---
  test child forked, pid 21823
  test child finished with 0
   end 
  Test builtin clang support subtest 1: Ok

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/tests/clang.c   |  6 -
 tools/perf/util/c++/clang-c.h  |  1 +
 tools/perf/util/c++/clang-test.cpp | 31 +-
 tools/perf/util/c++/clang.cpp  | 45 ++
 tools/perf/util/c++/clang.h|  3 +++
 5 files changed, 79 insertions(+), 7 deletions(-)

diff --git a/tools/perf/tests/clang.c b/tools/perf/tests/clang.c
index 57ee160..2964c06 100644
--- a/tools/perf/tests/clang.c
+++ b/tools/perf/tests/clang.c
@@ -12,6 +12,10 @@ static struct {
.func = test__clang_to_IR,
.desc = "Test builtin clang compile C source to IR",
},
+   {
+   .func = test__clang_to_obj,
+   .desc = "Test builtin clang compile C source to ELF object",
+   },
 #endif
 };
 
@@ -33,7 +37,7 @@ int test__clang(int i __maybe_unused)
return TEST_SKIP;
 }
 #else
-int test__clang(int i __maybe_unused)
+int test__clang(int i)
 {
if (i < 0 || i >= (int)ARRAY_SIZE(clang_testcase_table))
return TEST_FAIL;
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index dcde4b5..22b3936 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -9,6 +9,7 @@ extern void perf_clang__init(void);
 extern void perf_clang__cleanup(void);
 
 extern int test__clang_to_IR(void);
+extern int test__clang_to_obj(void);
 
 #ifdef __cplusplus
 }
diff --git a/tools/perf/util/c++/clang-test.cpp 
b/tools/perf/util/c++/clang-test.cpp
index d84e760..9b11e8c 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -13,15 +13,13 @@ public:
~perf_clang_scope() {perf_clang__cleanup();}
 };
 
-extern "C" {
-
-int test__clang_to_IR(void)
+static std::unique_ptr
+__test__clang_to_IR(void)
 {
-   perf_clang_scope _scope;
unsigned int kernel_version;
 
if (fetch_kernel_version(&kernel_version, NULL, 0))
-   return -1;
+   return std::unique_ptr(nullptr);
 
std::string cflag_kver("-DLINUX_VERSION_CODE=" +
std::to_string(kernel_version));
@@ -30,14 +28,35 @@ int test__clang_to_IR(void)
perf::getModuleFromSource({cflag_kver.c_str()},
  "perf-test.c",
  test_llvm__bpf_base_prog);
+   return M;
+}
+
+extern "C" {
+int test__clang_to_IR(void)
+{
+   perf_clang_scope _scope;
 
+   auto M = __test__clang_to_IR();
if (!M)
return -1;
-
for (llvm::Function& F : *M)
if (F.getName() == "bpf_func__SyS_epoll_wait")
return 0;
return -1;
 }
 
+int test__clang_to_obj(void)
+{
+   perf_clang_scope _scope;
+
+   auto M = __test__clang_to_IR();
+   if (!M)
+   return -1;
+
+   auto Buffer = perf::getBPFObjectFromModule(&*M);
+   if (!Buffer)
+   return -1;
+   return 0;
+}
+
 }
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 715ca0a..2a1a75d 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -13,10 +13,15 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Tooling/Tooling.h"
+#include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Option/Option.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetOptions.h"
 #include 
 
 #include "clang.h"
@@ -105,12 +110,52 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, 
StringRef Path)
return getModuleFromSource(std::move(CFlags), Path, VFS);
 }
 
+std::unique_ptr>
+getBPFObjectFromModule(llvm::Module *Module)
+{
+   using namespace llvm;
+
+   std::string TargetTriple("bpf-pc-linux");
+   std::string Error;
+   const Target* Target = TargetRegistry::lookupTarget(TargetTriple, 
Error);
+   if (!Target) {
+   llvm::errs() << Error;
+   return

[PATCH v3 16/30] perf clang: Pass full path to builtin clang

2016-11-25 Thread Wang Nan
If clang changes its working directory, relative path passed to
perf_clang__compile_bpf() becomes invalid. Before running clang,
convert it to absolute path so file can be found even working directory
is changed.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/c++/clang.cpp | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index 1e97415..610f1cf 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -13,6 +13,7 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Option/Option.h"
@@ -164,7 +165,7 @@ void perf_clang__cleanup(void)
llvm::llvm_shutdown();
 }
 
-int perf_clang__compile_bpf(const char *filename,
+int perf_clang__compile_bpf(const char *_filename,
void **p_obj_buf,
size_t *p_obj_buf_sz)
 {
@@ -173,8 +174,11 @@ int perf_clang__compile_bpf(const char *filename,
if (!p_obj_buf || !p_obj_buf_sz)
return -EINVAL;
 
+   llvm::SmallString FileName(_filename);
+   llvm::sys::fs::make_absolute(FileName);
+
llvm::opt::ArgStringList CFlags;
-   auto M = getModuleFromSource(std::move(CFlags), filename);
+   auto M = getModuleFromSource(std::move(CFlags), FileName.data());
if (!M)
return  -EINVAL;
auto O = getBPFObjectFromModule(&*M);
-- 
2.10.1



[PATCH v3 23/30] perf clang jit: Collect the lowest address in maps section as map_base

2016-11-25 Thread Wang Nan
During jitting, find the lowest address in maps section and store its
value to _map_base. Pass its value out through perf_clang__compile_bpf().
map_base is useful for jitted functions accessing BPF maps.

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
---
 tools/perf/util/bpf-loader.c  | 39 +--
 tools/perf/util/c++/clang-c.h |  6 --
 tools/perf/util/c++/clang.cpp | 15 +--
 tools/perf/util/c++/clang.h   |  5 +
 4 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c
index e50045f..81c6fed 100644
--- a/tools/perf/util/bpf-loader.c
+++ b/tools/perf/util/bpf-loader.c
@@ -47,6 +47,10 @@ struct bpf_prog_priv {
int *type_mapping;
 };
 
+struct bpf_obj_priv {
+   void *map_base;
+};
+
 static bool libbpf_initialized;
 
 struct bpf_object *
@@ -70,9 +74,20 @@ bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, 
const char *name)
return obj;
 }
 
+static void
+clear_obj_priv(struct bpf_object *obj __maybe_unused,
+  void *_priv)
+{
+   struct bpf_obj_priv *priv = _priv;
+
+   free(priv);
+}
+
 struct bpf_object *bpf__prepare_load(const char *filename, bool source)
 {
struct bpf_object *obj;
+   void *map_base = NULL;
+   int err;
 
if (!libbpf_initialized) {
libbpf_set_print(libbpf_warning,
@@ -82,14 +97,14 @@ struct bpf_object *bpf__prepare_load(const char *filename, 
bool source)
}
 
if (source) {
-   int err;
void *obj_buf;
size_t obj_buf_sz;
jitted_funcs_map_t jitted_funcs_map;
 
perf_clang__init();
err = perf_clang__compile_bpf(filename, &obj_buf,
- &obj_buf_sz, &jitted_funcs_map);
+ &obj_buf_sz, &jitted_funcs_map,
+ &map_base);
perf_clang__cleanup();
if (err) {
pr_warning("bpf: builtin compiling failed: %d, try 
external compiler\n", err);
@@ -119,7 +134,27 @@ struct bpf_object *bpf__prepare_load(const char *filename, 
bool source)
return obj;
}
 
+   if (map_base) {
+   struct bpf_obj_priv *priv = calloc(sizeof(*priv), 1);
+
+   if (!priv) {
+   pr_debug("bpf: failed to alloc priv for object\n");
+   err = -ENOMEM;
+   goto errout;
+   }
+   priv->map_base = map_base;
+
+   err = bpf_object__set_priv(obj, priv, clear_obj_priv);
+   if (err) {
+   pr_debug("Failed to set priv for object '%s'\n", 
filename);
+   goto errout;
+   }
+   }
+
return obj;
+errout:
+   bpf_object__close(obj);
+   return ERR_PTR(err);
 }
 
 void bpf__clear(void)
diff --git a/tools/perf/util/c++/clang-c.h b/tools/perf/util/c++/clang-c.h
index 021b1ad..4cf651b 100644
--- a/tools/perf/util/c++/clang-c.h
+++ b/tools/perf/util/c++/clang-c.h
@@ -22,7 +22,8 @@ extern void test__clang_callback(int x);
 extern int perf_clang__compile_bpf(const char *filename,
   void **p_obj_buf,
   size_t *p_obj_buf_sz,
-  jitted_funcs_map_t *p_funcs_map);
+  jitted_funcs_map_t *p_funcs_map,
+  void **p_map_base);
 
 extern int
 perf_clang__hook_jitted_func(jitted_funcs_map_t map, void *ctx, bool is_err);
@@ -40,7 +41,8 @@ static inline int
 perf_clang__compile_bpf(const char *filename __maybe_unused,
void **p_obj_buf __maybe_unused,
size_t *p_obj_buf_sz __maybe_unused,
-   jitted_funcs_map_t *p_funcs_map __maybe_unused)
+   jitted_funcs_map_t *p_funcs_map __maybe_unused,
+   void **p_map_base __maybe_unused)
 {
return -ENOTSUP;
 }
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp
index f2608f5..f8ea9bd 100644
--- a/tools/perf/util/c++/clang.cpp
+++ b/tools/perf/util/c++/clang.cpp
@@ -122,7 +122,7 @@ getModuleFromSource(llvm::opt::ArgStringList CFlags, 
StringRef Path)
return getModuleFromSource(std::move(CFlags), Path, VFS);
 }
 
-PerfModule::PerfModule(std::unique_ptr&& M) : 
Module(std::move(M))
+PerfModule::PerfModule(std::unique_ptr&& M) : 
Module(std::move(M)), _map_base(NULL)
 {
for (llvm::Function& F : *Module) {
if (F.getLinkage() != llvm::GlobalValue::ExternalLinkage)
@@ -247,6 +247,13 @@ int PerfModule::doJIT(void)
&JITMemoryManager,
std::move(Resolver));

[PATCH v3 04/30] perf tools: Introduce perf hooks

2016-11-25 Thread Wang Nan
Perf hooks allow hooking user code at perf events. They can be used for
manipulation of BPF maps, taking snapshot and reporting results. In this
patch two perf hook points are introduced: record_start and record_end.

To avoid buggy user actions, a SIGSEGV signal handler is introduced into
'perf record'. It turns off perf hook if it causes a segfault and report
an error to help debugging.

A test case for perf hook is introduced.

Test result:
  $ ./buildperf/perf test -v hook
  50: Test perf hooks  :
  --- start ---
  test child forked, pid 10311
  SIGSEGV is observed as expected, try to recover.
  Fatal error (SEGFAULT) in perf hook 'test'
  test child finished with 0
   end 
  Test perf hooks: Ok

Signed-off-by: Wang Nan 
Cc: Arnaldo Carvalho de Melo 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
---
 tools/perf/builtin-record.c   | 11 +
 tools/perf/tests/Build|  1 +
 tools/perf/tests/builtin-test.c   |  4 ++
 tools/perf/tests/perf-hooks.c | 44 
 tools/perf/tests/tests.h  |  1 +
 tools/perf/util/Build |  2 +
 tools/perf/util/perf-hooks-list.h |  3 ++
 tools/perf/util/perf-hooks.c  | 84 +++
 tools/perf/util/perf-hooks.h  | 37 +
 9 files changed, 187 insertions(+)
 create mode 100644 tools/perf/tests/perf-hooks.c
 create mode 100644 tools/perf/util/perf-hooks-list.h
 create mode 100644 tools/perf/util/perf-hooks.c
 create mode 100644 tools/perf/util/perf-hooks.h

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 67d2a90..fa26865 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -37,6 +37,7 @@
 #include "util/llvm-utils.h"
 #include "util/bpf-loader.h"
 #include "util/trigger.h"
+#include "util/perf-hooks.h"
 #include "asm/bug.h"
 
 #include 
@@ -206,6 +207,12 @@ static void sig_handler(int sig)
done = 1;
 }
 
+static void sigsegv_handler(int sig)
+{
+   perf_hooks__recover();
+   sighandler_dump_stack(sig);
+}
+
 static void record__sig_exit(void)
 {
if (signr == -1)
@@ -833,6 +840,7 @@ static int __cmd_record(struct record *rec, int argc, const 
char **argv)
signal(SIGCHLD, sig_handler);
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
+   signal(SIGSEGV, sigsegv_handler);
 
if (rec->opts.auxtrace_snapshot_mode || rec->switch_output) {
signal(SIGUSR2, snapshot_sig_handler);
@@ -970,6 +978,7 @@ static int __cmd_record(struct record *rec, int argc, const 
char **argv)
 
trigger_ready(&auxtrace_snapshot_trigger);
trigger_ready(&switch_output_trigger);
+   perf_hooks__invoke_record_start();
for (;;) {
unsigned long long hits = rec->samples;
 
@@ -1114,6 +1123,8 @@ static int __cmd_record(struct record *rec, int argc, 
const char **argv)
}
}
 
+   perf_hooks__invoke_record_end();
+
if (!err && !quiet) {
char samples[128];
const char *postfix = rec->timestamp_filename ?
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 8a4ce49..af3ec94 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -42,6 +42,7 @@ perf-y += backward-ring-buffer.o
 perf-y += sdt.o
 perf-y += is_printable_array.o
 perf-y += bitmap.o
+perf-y += perf-hooks.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
$(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 778668a..dab83f7 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -230,6 +230,10 @@ static struct test generic_tests[] = {
.func = test__bitmap_print,
},
{
+   .desc = "Test perf hooks",
+   .func = test__perf_hooks,
+   },
+   {
.func = NULL,
},
 };
diff --git a/tools/perf/tests/perf-hooks.c b/tools/perf/tests/perf-hooks.c
new file mode 100644
index 000..9338cb2
--- /dev/null
+++ b/tools/perf/tests/perf-hooks.c
@@ -0,0 +1,44 @@
+#include 
+#include 
+
+#include "tests.h"
+#include "debug.h"
+#include "util.h"
+#include "perf-hooks.h"
+
+static void sigsegv_handler(int sig __maybe_unused)
+{
+   pr_debug("SIGSEGV is observed as expected, try to recover.\n");
+   perf_hooks__recover();
+   signal(SIGSEGV, SIG_DFL);
+   raise(SIGSEGV);
+   exit(-1);
+}
+
+static int hook_flags;
+
+static void the_hook(void)
+{
+   int *p = NULL;
+
+   hook_flags = 1234;
+
+   /* Generate a segfault, test perf_hooks__recover */
+   *p = 0;
+}
+
+int test__perf_hooks(int subtest __maybe_unused)
+{
+   signal(SIGSEGV, sigsegv_handler);
+   perf_hooks__set_hook("test", the_hook);
+   perf_hooks__invoke_test();
+
+   /* hook is triggered? */
+   if (hook_flags != 1234)
+   return T

[PATCH v3 13/30] perf clang: Update test case to use real BPF script

2016-11-25 Thread Wang Nan
Allow C++ code to use util.h and tests/llvm.h. Let 'perf test' compile a
real BPF script.

Signed-off-by: Wang Nan 
Cc: Alexei Starovoitov 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Zefan Li 
Cc: pi3or...@163.com
Link: 
http://lkml.kernel.org/r/1474874832-134786-10-git-send-email-wangn...@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/Makefile.config | 27 +++
 tools/perf/tests/llvm.h|  7 +++
 tools/perf/util/c++/clang-test.cpp | 17 ++---
 tools/perf/util/util-cxx.h | 26 ++
 4 files changed, 62 insertions(+), 15 deletions(-)
 create mode 100644 tools/perf/util/util-cxx.h

diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index b7c9c80..09c2a98 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -212,24 +212,27 @@ ifeq ($(DEBUG),0)
   endif
 endif
 
-CFLAGS += -I$(src-perf)/util/include
-CFLAGS += -I$(src-perf)/arch/$(ARCH)/include
-CFLAGS += -I$(srctree)/tools/include/uapi
-CFLAGS += -I$(srctree)/tools/include/
-CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/uapi
-CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/
-CFLAGS += -I$(srctree)/tools/arch/$(ARCH)/
+INC_FLAGS += -I$(src-perf)/util/include
+INC_FLAGS += -I$(src-perf)/arch/$(ARCH)/include
+INC_FLAGS += -I$(srctree)/tools/include/uapi
+INC_FLAGS += -I$(srctree)/tools/include/
+INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/uapi
+INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/include/
+INC_FLAGS += -I$(srctree)/tools/arch/$(ARCH)/
 
 # $(obj-perf)  for generated common-cmds.h
 # $(obj-perf)/util for generated bison/flex headers
 ifneq ($(OUTPUT),)
-CFLAGS += -I$(obj-perf)/util
-CFLAGS += -I$(obj-perf)
+INC_FLAGS += -I$(obj-perf)/util
+INC_FLAGS += -I$(obj-perf)
 endif
 
-CFLAGS += -I$(src-perf)/util
-CFLAGS += -I$(src-perf)
-CFLAGS += -I$(srctree)/tools/lib/
+INC_FLAGS += -I$(src-perf)/util
+INC_FLAGS += -I$(src-perf)
+INC_FLAGS += -I$(srctree)/tools/lib/
+
+CFLAGS   += $(INC_FLAGS)
+CXXFLAGS += $(INC_FLAGS)
 
 CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
 
diff --git a/tools/perf/tests/llvm.h b/tools/perf/tests/llvm.h
index 0eaa604..b835717 100644
--- a/tools/perf/tests/llvm.h
+++ b/tools/perf/tests/llvm.h
@@ -1,6 +1,10 @@
 #ifndef PERF_TEST_LLVM_H
 #define PERF_TEST_LLVM_H
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #include  /* for size_t */
 #include  /* for bool */
 
@@ -20,4 +24,7 @@ enum test_llvm__testcase {
 int test_llvm__fetch_bpf_obj(void **p_obj_buf, size_t *p_obj_buf_sz,
 enum test_llvm__testcase index, bool force,
 bool *should_load_fail);
+#ifdef __cplusplus
+}
+#endif
 #endif
diff --git a/tools/perf/util/c++/clang-test.cpp 
b/tools/perf/util/c++/clang-test.cpp
index 0f484fb..d84e760 100644
--- a/tools/perf/util/c++/clang-test.cpp
+++ b/tools/perf/util/c++/clang-test.cpp
@@ -3,6 +3,10 @@
 #include "llvm/IR/Function.h"
 #include "llvm/IR/LLVMContext.h"
 
+#include 
+#include 
+#include 
+
 class perf_clang_scope {
 public:
explicit perf_clang_scope() {perf_clang__init();}
@@ -14,17 +18,24 @@ extern "C" {
 int test__clang_to_IR(void)
 {
perf_clang_scope _scope;
+   unsigned int kernel_version;
+
+   if (fetch_kernel_version(&kernel_version, NULL, 0))
+   return -1;
+
+   std::string cflag_kver("-DLINUX_VERSION_CODE=" +
+   std::to_string(kernel_version));
 
std::unique_ptr M =
-   perf::getModuleFromSource({"-DRESULT=1"},
+   perf::getModuleFromSource({cflag_kver.c_str()},
  "perf-test.c",
- "int myfunc(void) {return RESULT;}");
+ test_llvm__bpf_base_prog);
 
if (!M)
return -1;
 
for (llvm::Function& F : *M)
-   if (F.getName() == "myfunc")
+   if (F.getName() == "bpf_func__SyS_epoll_wait")
return 0;
return -1;
 }
diff --git a/tools/perf/util/util-cxx.h b/tools/perf/util/util-cxx.h
new file mode 100644
index 000..0e0e019
--- /dev/null
+++ b/tools/perf/util/util-cxx.h
@@ -0,0 +1,26 @@
+/*
+ * Support C++ source use utilities defined in util.h
+ */
+
+#ifndef PERF_UTIL_UTIL_CXX_H
+#define PERF_UTIL_UTIL_CXX_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Now 'new' is the only C++ keyword found in util.h:
+ * in tools/include/linux/rbtree.h
+ *
+ * Other keywords, like class and delete, should be
+ * redefined if necessary.
+ */
+#define new _new
+#include "util.h"
+#undef new
+
+#ifdef __cplusplus
+}
+#endif
+#endif
-- 
2.10.1



[PATCH v3 01/30] tools lib bpf: Add missing BPF functions

2016-11-25 Thread Wang Nan
Add more BPF map operations to libbpf. Also add bpf_obj_{pin,get}(). They
can be used on not only BPF maps but also BPF programs.

Signed-off-by: Wang Nan 
Cc: Alexei Starovoitov 
Cc: Arnaldo Carvalho de Melo 
Cc: Joe Stringer 
Cc: Li Zefan 
---
 tools/lib/bpf/bpf.c | 56 +
 tools/lib/bpf/bpf.h |  7 +++
 2 files changed, 63 insertions(+)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 4212ed6..8143536 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -110,3 +110,59 @@ int bpf_map_update_elem(int fd, void *key, void *value,
 
return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
 }
+
+int bpf_map_lookup_elem(int fd, void *key, void *value)
+{
+   union bpf_attr attr;
+
+   bzero(&attr, sizeof(attr));
+   attr.map_fd = fd;
+   attr.key = ptr_to_u64(key);
+   attr.value = ptr_to_u64(value);
+
+   return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_delete_elem(int fd, void *key)
+{
+   union bpf_attr attr;
+
+   bzero(&attr, sizeof(attr));
+   attr.map_fd = fd;
+   attr.key = ptr_to_u64(key);
+
+   return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_get_next_key(int fd, void *key, void *next_key)
+{
+   union bpf_attr attr;
+
+   bzero(&attr, sizeof(attr));
+   attr.map_fd = fd;
+   attr.key = ptr_to_u64(key);
+   attr.next_key = ptr_to_u64(next_key);
+
+   return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
+}
+
+int bpf_obj_pin(int fd, const char *pathname)
+{
+   union bpf_attr attr;
+
+   bzero(&attr, sizeof(attr));
+   attr.pathname = ptr_to_u64((void *)pathname);
+   attr.bpf_fd = fd;
+
+   return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
+}
+
+int bpf_obj_get(const char *pathname)
+{
+   union bpf_attr attr;
+
+   bzero(&attr, sizeof(attr));
+   attr.pathname = ptr_to_u64((void *)pathname);
+
+   return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
+}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index e8ba540..253c3db 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -35,4 +35,11 @@ int bpf_load_program(enum bpf_prog_type type, struct 
bpf_insn *insns,
 
 int bpf_map_update_elem(int fd, void *key, void *value,
u64 flags);
+
+int bpf_map_lookup_elem(int fd, void *key, void *value);
+int bpf_map_delete_elem(int fd, void *key);
+int bpf_map_get_next_key(int fd, void *key, void *next_key);
+int bpf_obj_pin(int fd, const char *pathname);
+int bpf_obj_get(const char *pathname);
+
 #endif
-- 
2.10.1



[PATCH v3 02/30] tools lib bpf: Add private field for bpf_object

2016-11-25 Thread Wang Nan
Similar to other classes defined in libbpf.h (map and program), allow
'object' class has its own private data.

Signed-off-by: Wang Nan 
Cc: Alexei Starovoitov 
Cc: Arnaldo Carvalho de Melo 
Cc: Li Zefan 
---
 tools/lib/bpf/libbpf.c | 23 +++
 tools/lib/bpf/libbpf.h |  5 +
 2 files changed, 28 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 96a2b2f..866d5cd 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -229,6 +229,10 @@ struct bpf_object {
 * all objects.
 */
struct list_head list;
+
+   void *priv;
+   bpf_object_clear_priv_t clear_priv;
+
char path[];
 };
 #define obj_elf_valid(o)   ((o)->efile.elf)
@@ -1229,6 +1233,9 @@ void bpf_object__close(struct bpf_object *obj)
if (!obj)
return;
 
+   if (obj->clear_priv)
+   obj->clear_priv(obj, obj->priv);
+
bpf_object__elf_finish(obj);
bpf_object__unload(obj);
 
@@ -1282,6 +1289,22 @@ unsigned int bpf_object__kversion(struct bpf_object *obj)
return obj ? obj->kern_version : 0;
 }
 
+int bpf_object__set_priv(struct bpf_object *obj, void *priv,
+bpf_object_clear_priv_t clear_priv)
+{
+   if (obj->priv && obj->clear_priv)
+   obj->clear_priv(obj, obj->priv);
+
+   obj->priv = priv;
+   obj->clear_priv = clear_priv;
+   return 0;
+}
+
+void *bpf_object__priv(struct bpf_object *obj)
+{
+   return obj ? obj->priv : ERR_PTR(-EINVAL);
+}
+
 struct bpf_program *
 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
 {
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index dd7a513..0c0b012 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -79,6 +79,11 @@ struct bpf_object *bpf_object__next(struct bpf_object *prev);
 (pos) != NULL; \
 (pos) = (tmp), (tmp) = bpf_object__next(tmp))
 
+typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
+int bpf_object__set_priv(struct bpf_object *obj, void *priv,
+bpf_object_clear_priv_t clear_priv);
+void *bpf_object__priv(struct bpf_object *prog);
+
 /* Accessors of bpf_program. */
 struct bpf_program;
 struct bpf_program *bpf_program__next(struct bpf_program *prog,
-- 
2.10.1



Re: [PATCH v3 2/3] zram: revalidate disk under init_lock

2016-11-25 Thread Sergey Senozhatsky

Hi,

On (11/25/16 17:35), Minchan Kim wrote:
> [1] moved revalidate_disk call out of init_lock to avoid lockdep
> false-positive splat. However, [2] remove init_lock in IO path
> so there is no worry about lockdep splat. So, let's restore it.
> This patch need to set BDI_CAP_STABLE_WRITES atomically in
> next patch.

can we break that dependency on the next patch if we would
set BDI_CAP_STABLE_WRITES when we allocate the queue?

queue->backing_dev_info.capabilities |= BDI_CAP_CGROUP_WRITEBACK;

-ss


Re: [PATCH v3 3/3] zram: support BDI_CAP_STABLE_WRITES

2016-11-25 Thread Sergey Senozhatsky
Hello Minchan,

On (11/25/16 17:35), Minchan Kim wrote:
[..]
> +static void zram_revalidate_disk(struct zram *zram)
> +{
> + revalidate_disk(zram->disk);
> + zram->disk->queue->backing_dev_info.capabilities |=
> + BDI_CAP_STABLE_WRITES;
> +}
> +
>  /*
>   * Check if request is within bounds and aligned on zram logical blocks.
>   */
> @@ -1094,7 +1102,7 @@ static ssize_t disksize_store(struct device *dev,
>   zram->comp = comp;
>   zram->disksize = disksize;
>   set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
> - revalidate_disk(zram->disk);
> + zram_revalidate_disk(zram);
>   up_write(&zram->init_lock);
>  
>   return len;
> @@ -1142,7 +1150,7 @@ static ssize_t reset_store(struct device *dev,
>   /* Make sure all the pending I/O are finished */
>   fsync_bdev(bdev);
>   zram_reset_device(zram);
> - revalidate_disk(zram->disk);
> + zram_revalidate_disk(zram);
>   bdput(bdev);
>  
>   mutex_lock(&bdev->bd_mutex);

why not set it just once, when we allocate queue/disk and configure both
of them:  in zram_add()

queue->backing_dev_info.capabilities |= BDI_CAP_CGROUP_WRITEBACK;

-ss


Re: [PATCH] Fixes for compiling with clang

2016-11-25 Thread kbuild test robot
Hi Peter,

[auto build test ERROR on kbuild/for-next]
[also build test ERROR on v4.9-rc6 next-20161125]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/Peter-Foley/Fixes-for-compiling-with-clang/20161126-124017
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild.git 
for-next
config: avr32-atngw100_defconfig (attached as .config)
compiler: avr-gcc (GCC) 4.9.2
reproduce:
wget 
https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross
 -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=avr32 

All errors (new ones prefixed by >>):

>> /bin/sh: 0: Illegal option - 
--
>> /bin/sh: 0: Illegal option - 
   kernel/time/Kconfig:155:warning: range is invalid
--
>> /bin/sh: 0: Illegal option - 
   kernel/time/Kconfig:155:warning: range is invalid
>> /bin/sh: 0: Illegal option - 
   avr-gcc: error: unrecognized command line option '-mno-pic'
   avr-gcc: error: unrecognized command line option '-march=ap'
   make[2]: *** [kernel/bounds.s] Error 1
   make[2]: Target '__build' not remade because of errors.
   make[1]: *** [prepare0] Error 2
   make[1]: Target 'prepare' not remade because of errors.
   make: *** [sub-make] Error 2

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation


.config.gz
Description: application/gzip


Re: [PATCH v2] Staging: iio: adc: fix sysfs files modes in ad7192.c

2016-11-25 Thread Boyan Vladinov

Ignore patch version...comments below

Will resend it

On 24.11.2016 19:38, Boyan Vladinov wrote:

Fixes sysfs entries user/group modes and coding style warnings
found by checkpatch.pl tool. Also use the IIO_DEVICE_ATTR_[RO|RW] and
macros to create device attributes

Signed-off-by: Boyan Vladinov 
---
 drivers/staging/iio/adc/ad7192.c | 45 +++-
 1 file changed, 26 insertions(+), 19 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7192.c b/drivers/staging/iio/adc/ad7192.c
index 1fb68c01abd5..630b04d8128f 100644
--- a/drivers/staging/iio/adc/ad7192.c
+++ b/drivers/staging/iio/adc/ad7192.c
@@ -324,8 +324,8 @@ static int ad7192_setup(struct ad7192_state *st,
 }

 static ssize_t
-ad7192_show_scale_available(struct device *dev,
-   struct device_attribute *attr, char *buf)
+in_v_m_v_scale_available_show(struct device *dev, struct device_attribute 
*attr,
+ char *buf)
 {
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
struct ad7192_state *st = iio_priv(indio_dev);
@@ -340,15 +340,19 @@ ad7192_show_scale_available(struct device *dev,
return len;
 }

-static IIO_DEVICE_ATTR_NAMED(in_v_m_v_scale_available,
-in_voltage-voltage_scale_available,

behavior changed after removed `in_voltage-voltage_scale_available`

-S_IRUGO, ad7192_show_scale_available, NULL, 0);
+static ssize_t
+in_voltage_scale_available_show(struct device *dev,
+   struct device_attribute *attr, char *buf)
+{
+   return in_v_m_v_scale_available_show(dev, attr, buf);
+}
+
+static IIO_DEVICE_ATTR_RO(in_v_m_v_scale_available, 0);

-static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO,
-  ad7192_show_scale_available, NULL, 0);
+static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0);

-static ssize_t ad7192_show_ac_excitation(struct device *dev,
-struct device_attribute *attr,
+static ssize_t ac_excitation_en_show(struct device *dev,
+struct device_attribute *attr,
 char *buf)
 {
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
@@ -357,8 +361,8 @@ static ssize_t ad7192_show_ac_excitation(struct device *dev,
return sprintf(buf, "%d\n", !!(st->mode & AD7192_MODE_ACX));
 }

-static ssize_t ad7192_show_bridge_switch(struct device *dev,
-struct device_attribute *attr,
+static ssize_t bridge_switch_en_show(struct device *dev,
+struct device_attribute *attr,
 char *buf)
 {
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
@@ -367,8 +371,8 @@ static ssize_t ad7192_show_bridge_switch(struct device *dev,
return sprintf(buf, "%d\n", !!(st->gpocon & AD7192_GPOCON_BPDSW));
 }

-static ssize_t ad7192_set(struct device *dev,
- struct device_attribute *attr,
+static ssize_t bridge_switch_en_store(struct device *dev,
+ struct device_attribute *attr,
  const char *buf,
  size_t len)
 {
@@ -412,13 +416,16 @@ static ssize_t ad7192_set(struct device *dev,
return ret ? ret : len;
 }

-static IIO_DEVICE_ATTR(bridge_switch_en, S_IRUGO | S_IWUSR,
-  ad7192_show_bridge_switch, ad7192_set,
-  AD7192_REG_GPOCON);
+static ssize_t ac_excitation_en_store(struct device *dev,
+ struct device_attribute *attr,
+const char *buf, size_t len)
+{
+   return bridge_switch_en_store(dev, attr, buf, len);
+}
+
+static IIO_DEVICE_ATTR_RW(bridge_switch_en, AD7192_REG_GPOCON);

-static IIO_DEVICE_ATTR(ac_excitation_en, S_IRUGO | S_IWUSR,
-  ad7192_show_ac_excitation, ad7192_set,
-  AD7192_REG_MODE);
+static IIO_DEVICE_ATTR_RW(ac_excitation_en, AD7192_REG_MODE);

 static struct attribute *ad7192_attributes[] = {
&iio_dev_attr_in_v_m_v_scale_available.dev_attr.attr,



Xmas Offer

2016-11-25 Thread Mrs Julie Leach
You are a recipient to Mrs Julie Leach Donation of $3 million USD. Contact ( 
julieleac...@gmail.com ) for claims.


[PATCH] Fixes for compiling with clang

2016-11-25 Thread Peter Foley
Move definition of HOSTCC to allow use of cc-name.
Suppress warnings about unsupported optimization options.
Disable clang's integrated assembler which is incompatible with kernel
asm constructs.

Signed-off-by: Peter Foley 
---
 Makefile | 23 +--
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index 0ede48ba5aaf..b69ad7e4a6d2 100644
--- a/Makefile
+++ b/Makefile
@@ -299,16 +299,6 @@ CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo 
$$BASH; \
  else if [ -x /bin/bash ]; then echo /bin/bash; \
  else echo sh; fi ; fi)
 
-HOSTCC   = gcc
-HOSTCXX  = g++
-HOSTCFLAGS   = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 
-fomit-frame-pointer -std=gnu89
-HOSTCXXFLAGS = -O2
-
-ifeq ($(shell $(HOSTCC) -v 2>&1 | grep -c "clang version"), 1)
-HOSTCFLAGS  += -Wno-unused-value -Wno-unused-parameter \
-   -Wno-missing-field-initializers -fno-delete-null-pointer-checks
-endif
-
 # Decide whether to build built-in, modular, or both.
 # Normally, just do built-in.
 
@@ -343,6 +333,16 @@ export KBUILD_CHECKSRC KBUILD_SRC KBUILD_EXTMOD
 scripts/Kbuild.include: ;
 include scripts/Kbuild.include
 
+HOSTCC   = gcc
+HOSTCXX  = g++
+HOSTCFLAGS   = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 
-fomit-frame-pointer -std=gnu89 \
+   -Wno-unused-value -Wno-unused-parameter 
-Wno-missing-field-initializers
+HOSTCXXFLAGS = -O2
+
+ifneq ($(cc-name),clang)
+HOSTCFLAGS +=  -fno-delete-null-pointer-checks
+endif
+
 # Make variables (CC, etc...)
 AS = $(CROSS_COMPILE)as
 LD = $(CROSS_COMPILE)ld
@@ -685,6 +685,7 @@ KBUILD_CFLAGS += $(stackp-flag)
 ifeq ($(cc-name),clang)
 KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
 KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
+KBUILD_CFLAGS += $(call cc-disable-warning, ignored-optimization-argument)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
 KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
@@ -695,6 +696,8 @@ KBUILD_CFLAGS += $(call cc-disable-warning, 
tautological-compare)
 # See modpost pattern 2
 KBUILD_CFLAGS += $(call cc-option, -mno-global-merge,)
 KBUILD_CFLAGS += $(call cc-option, -fcatch-undefined-behavior)
+KBUILD_CFLAGS += $(call cc-option, -fno-integrated-as)
+KBUILD_AFLAGS += $(call cc-option, -fno-integrated-as)
 else
 
 # These warnings generated too much noise in a regular build.
-- 
2.11.0.rc2



Re: [RFC 1/1] LSM ptags: Add tagging of processes

2016-11-25 Thread Tetsuo Handa
Jose Bollo wrote:
> +/**
> + * is_valid_utf8 - Is buffer a valid utf8 string?
> + *
> + * @buffer: the start of the string
> + * @length: length in bytes of the buffer
> + *
> + * Return 1 when valid or else returns 0
> + */

Do we really need to check UTF-8 inside kernel? What do you do if
people start using UTF-32 in the future? There was a discussion
about use of encoding inside kernel started at
http://lkml.kernel.org/r/20071103164303.GA26707@ubuntu .



> +
> +/**
> + * _ptags_read - Implement the reading of the tags
> + *
> + * @ptags: tags structure of the readen task
> + * @result: a pointer for storing the read result
> + * @uns: user namespace proxy
> + *
> + * Returns the count of byte read or the negative code -ENOMEM
> + * if an allocation failed.
> + */
> +static int _ptags_read(struct _ptags *ptags, char **result, struct uns uns)
> +{
> + unsigned idx, count;
> + size_t size;
> + struct entry *entries, *entry;
> + char *buffer;
> + struct value *value;
> + struct item *item;
> +
> + /* init loops */
> + count = ptags->count;
> + entries = ptags->entries;
> +
> + /* compute printed size */
> + size = 0;
> + for (idx = 0; idx < count; idx++) {
> + entry = &entries[idx];
> + value = entry_read(entry, uns);
> + if (value) {
> + item = value_get(*value);
> + size += entry_name(*entry)->length
> + + (unsigned)value_is_kept(*value)
> + + (item ? 2 + item->length : 1);
> + }
> + }
> +
> + if (size > INT_MAX)
> + return -E2BIG;

This sanity check is useless. INT_MAX is 2,147,483,647 but kmalloc() can't
allocate larger than 8,388,608 bytes if PAGE_SIZE = 4096 and MAX_ORDER = 11.

> + buffer = kmalloc(size, GFP_KERNEL);
> + if (!buffer)
> + return -ENOMEM;

Moreover, kmalloc() will not try to allocate larger than 32,768 bytes
(PAGE_ALLOC_COSTLY_ORDER = 3). Although __GFP_NOFAIL can force kmalloc() to
retry by invoking the OOM killer, such behavior might change in near future
due to http://lkml.kernel.org/r/20161123064925.9716-3-mho...@kernel.org .

Given these constants

#define MAXCOUNT4000
#define MAXTAGLEN   4000
#define MAXVALUELEN 32700

and someone tried to use as many and long as possible tags, what is
possible max value for "size"? I think that that value can easily exceed
32,768 and kmalloc() won't be reliable. vmalloc() can be used as a fallback
when kmalloc() failed, but is trying to pass possible max value for "size"
to vmalloc() reasonable? Shouldn't this function be rewritten not to
allocate so much memory?

Also, how much memory will be consumed if everybody tried to use tags
as many and long as possible?

> +
> + /* print in the buffer */
> + *result = buffer;
> + for (idx = 0; idx < count; idx++) {
> + entry = &entries[idx];
> + value = entry_read(entry, uns);
> + if (value) {
> + if (value_is_kept(*value))
> + *buffer++ = KEEP_CHAR;
> + item = entry_name(*entry);
> + memcpy(buffer, item->value, item->length);
> + buffer += item->length;
> + item = value_get(*value);
> + if (item) {
> + *buffer++ = ASSIGN_CHAR;
> + memcpy(buffer, item->value, item->length);
> + buffer += item->length;
> + }
> + *buffer++ = EOL_CHAR;
> + }
> + }
> +
> + return (int)size;
> +}


fixed drivers/staging/most/mostcore/core.c coding style

2016-11-25 Thread wenhungyang
fix drivers/staging/most/mostcore/core.c coding style

1. move static from MACRO to function name
2. change symbolic permission such as 'S_IRUGO' to number

>From 3e1dba83090c58251aae1dc7aca431bb11632a98 Mon Sep 17 00:00:00 2001
From: wenhungyang 
Date: Wed, 23 Nov 2016 21:55:36 +0800
Subject: [c157e58488d1] fix core.c coding-style
To: fyony...@gmail.com

Signed-off-by: wenhungyang 
---
 drivers/staging/most/mostcore/core.c | 40 ++--
 1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/drivers/staging/most/mostcore/core.c b/drivers/staging/most/mostcore/core.c
index 4c580d1..9286c36 100644
--- a/drivers/staging/most/mostcore/core.c
+++ b/drivers/staging/most/mostcore/core.c
@@ -342,15 +342,15 @@ static ssize_t show_channel_starving(struct most_c_obj *c,
 }
 
 #define create_show_channel_attribute(val) \
-	static MOST_CHNL_ATTR(val, S_IRUGO, show_##val, NULL)
+	MOST_CHNL_ATTR(val, 0444, show_##val, NULL)
 
-create_show_channel_attribute(available_directions);
-create_show_channel_attribute(available_datatypes);
-create_show_channel_attribute(number_of_packet_buffers);
-create_show_channel_attribute(number_of_stream_buffers);
-create_show_channel_attribute(size_of_stream_buffer);
-create_show_channel_attribute(size_of_packet_buffer);
-create_show_channel_attribute(channel_starving);
+static create_show_channel_attribute(available_directions);
+static create_show_channel_attribute(available_datatypes);
+static create_show_channel_attribute(number_of_packet_buffers);
+static create_show_channel_attribute(number_of_stream_buffers);
+static create_show_channel_attribute(size_of_stream_buffer);
+static create_show_channel_attribute(size_of_packet_buffer);
+static create_show_channel_attribute(channel_starving);
 
 static ssize_t show_set_number_of_buffers(struct most_c_obj *c,
 	  struct most_c_attr *attr,
@@ -494,16 +494,16 @@ static ssize_t store_set_packets_per_xact(struct most_c_obj *c,
 }
 
 #define create_channel_attribute(value) \
-	static MOST_CHNL_ATTR(value, S_IRUGO | S_IWUSR, \
+	MOST_CHNL_ATTR(value, 0644, \
 			  show_##value, \
 			  store_##value)
 
-create_channel_attribute(set_buffer_size);
-create_channel_attribute(set_number_of_buffers);
-create_channel_attribute(set_direction);
-create_channel_attribute(set_datatype);
-create_channel_attribute(set_subbuffer_size);
-create_channel_attribute(set_packets_per_xact);
+static create_channel_attribute(set_buffer_size);
+static create_channel_attribute(set_number_of_buffers);
+static create_channel_attribute(set_direction);
+static create_channel_attribute(set_datatype);
+static create_channel_attribute(set_subbuffer_size);
+static create_channel_attribute(set_packets_per_xact);
 
 /**
  * most_channel_def_attrs - array of default attributes of channel object
@@ -690,10 +690,10 @@ static ssize_t show_interface(struct most_inst_obj *instance_obj,
 }
 
 #define create_inst_attribute(value) \
-	static MOST_INST_ATTR(value, S_IRUGO, show_##value, NULL)
+	MOST_INST_ATTR(value, 0444, show_##value, NULL)
 
-create_inst_attribute(description);
-create_inst_attribute(interface);
+static create_inst_attribute(description);
+static create_inst_attribute(interface);
 
 static struct attribute *most_inst_def_attrs[] = {
 	&most_inst_attr_description.attr,
@@ -1016,7 +1016,7 @@ static ssize_t store_add_link(struct most_aim_obj *aim_obj,
 }
 
 static struct most_aim_attribute most_aim_attr_add_link =
-	__ATTR(add_link, S_IRUGO | S_IWUSR, show_add_link, store_add_link);
+	__ATTR(add_link, 0644, show_add_link, store_add_link);
 
 /**
  * store_remove_link - store function for remove_link attribute
@@ -1059,7 +1059,7 @@ static ssize_t store_remove_link(struct most_aim_obj *aim_obj,
 }
 
 static struct most_aim_attribute most_aim_attr_remove_link =
-	__ATTR(remove_link, S_IWUSR, NULL, store_remove_link);
+	__ATTR(remove_link, 0200, NULL, store_remove_link);
 
 static struct attribute *most_aim_def_attrs[] = {
 	&most_aim_attr_add_link.attr,
-- 
2.7.4



[PATCH] IIO: Change msleep to usleep_range for small msecs

2016-11-25 Thread Aniroop Mathur
msleep(1~20) may not do what the caller intends, and will often sleep longer.
(~20 ms actual sleep for any value given in the 1~20ms range)
This is not the desired behaviour for many cases like device resume time,
device suspend time, device enable time, data reading time, etc.
Thus, change msleep to usleep_range for precise wakeups.

Signed-off-by: Aniroop Mathur 
---
 drivers/iio/adc/exynos_adc.c |  2 +-
 drivers/iio/pressure/bmp280-core.c   | 14 +++---
 drivers/staging/iio/meter/ade7753.c  |  2 +-
 drivers/staging/iio/meter/ade7753.h  |  2 +-
 drivers/staging/iio/meter/ade7754.c  |  2 +-
 drivers/staging/iio/meter/ade7754.h  |  2 +-
 drivers/staging/iio/meter/ade7758.h  |  2 +-
 drivers/staging/iio/meter/ade7758_core.c |  2 +-
 drivers/staging/iio/meter/ade7759.c  |  2 +-
 drivers/staging/iio/meter/ade7759.h  |  2 +-
 drivers/staging/iio/meter/ade7854.c  |  2 +-
 drivers/staging/iio/meter/ade7854.h  |  2 +-
 12 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/drivers/iio/adc/exynos_adc.c b/drivers/iio/adc/exynos_adc.c
index c15756d..ad1775b 100644
--- a/drivers/iio/adc/exynos_adc.c
+++ b/drivers/iio/adc/exynos_adc.c
@@ -632,7 +632,7 @@ static irqreturn_t exynos_ts_isr(int irq, void *dev_id)
input_report_key(info->input, BTN_TOUCH, 1);
input_sync(info->input);
 
-   msleep(1);
+   usleep_range(1000, 1100);
};
 
writel(0, ADC_V1_CLRINTPNDNUP(info->regs));
diff --git a/drivers/iio/pressure/bmp280-core.c 
b/drivers/iio/pressure/bmp280-core.c
index e5a533c..4d18826 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -65,7 +65,7 @@ struct bmp280_data {
struct bmp180_calib calib;
struct regulator *vddd;
struct regulator *vdda;
-   unsigned int start_up_time; /* in milliseconds */
+   unsigned int start_up_time; /* in microseconds */
 
/* log of base 2 of oversampling rate */
u8 oversampling_press;
@@ -935,14 +935,14 @@ int bmp280_common_probe(struct device *dev,
data->chip_info = &bmp180_chip_info;
data->oversampling_press = ilog2(8);
data->oversampling_temp = ilog2(1);
-   data->start_up_time = 10;
+   data->start_up_time = 1;
break;
case BMP280_CHIP_ID:
indio_dev->num_channels = 2;
data->chip_info = &bmp280_chip_info;
data->oversampling_press = ilog2(16);
data->oversampling_temp = ilog2(2);
-   data->start_up_time = 2;
+   data->start_up_time = 2000;
break;
case BME280_CHIP_ID:
indio_dev->num_channels = 3;
@@ -950,7 +950,7 @@ int bmp280_common_probe(struct device *dev,
data->oversampling_press = ilog2(16);
data->oversampling_humid = ilog2(16);
data->oversampling_temp = ilog2(2);
-   data->start_up_time = 2;
+   data->start_up_time = 2000;
break;
default:
return -EINVAL;
@@ -979,7 +979,7 @@ int bmp280_common_probe(struct device *dev,
goto out_disable_vddd;
}
/* Wait to make sure we started up properly */
-   mdelay(data->start_up_time);
+   usleep_range(data->start_up_time, data->start_up_time + 100);
 
/* Bring chip out of reset if there is an assigned GPIO line */
gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
@@ -1038,7 +1038,7 @@ int bmp280_common_probe(struct device *dev,
 * Set autosuspend to two orders of magnitude larger than the
 * start-up time.
 */
-   pm_runtime_set_autosuspend_delay(dev, data->start_up_time *100);
+   pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
pm_runtime_use_autosuspend(dev);
pm_runtime_put(dev);
 
@@ -1101,7 +1101,7 @@ static int bmp280_runtime_resume(struct device *dev)
ret = regulator_enable(data->vdda);
if (ret)
return ret;
-   msleep(data->start_up_time);
+   usleep_range(data->start_up_time, data->start_up_time + 100);
return data->chip_info->chip_config(data);
 }
 #endif /* CONFIG_PM */
diff --git a/drivers/staging/iio/meter/ade7753.c 
b/drivers/staging/iio/meter/ade7753.c
index 4b5f05f..671dc99 100644
--- a/drivers/staging/iio/meter/ade7753.c
+++ b/drivers/staging/iio/meter/ade7753.c
@@ -377,7 +377,7 @@ static int ade7753_initial_setup(struct iio_dev *indio_dev)
}
 
ade7753_reset(dev);
-   msleep(ADE7753_STARTUP_DELAY);
+   usleep_range(ADE7753_STARTUP_DELAY, ADE7753_STARTUP_DELAY + 100);
 
 err_ret:
return ret;
diff --git a/drivers/staging/iio/meter/ade7753.h 
b/drivers/staging/iio/meter/ade7753.h
index a9d93cc..bfe7491 100644
--- a/drivers/staging/iio/meter/ade7753.h
+++ b/drivers/staging/iio/meter

Re: [PATCH 1/3] perf sched timehist: Mark schedule function in callchains

2016-11-25 Thread David Ahern
On 11/23/16 11:30 PM, Namhyung Kim wrote:
> Hi David,
> 
> On Wed, Nov 23, 2016 at 10:13:46PM -0500, David Ahern wrote:
>> On 11/23/16 8:11 PM, Namhyung Kim wrote:
>>> The sched_switch event always captured from the scheduler function.  So
>>> it'd be great omit them from the callchain.  This patch marks the
>>> functions to be omitted by later patch.
>>
>> I had this covered by a symbol filter:
>>
>> https://github.com/dsahern/linux/blob/perf/full-monty-4.1/tools/perf/builtin-sched.c#L3000
>>
>> Not sure what happened over the years but that should still work and
>> allows the user to add more symbols to ignore:
> 
> The symbol filter was removed by commit be39db9f2932 ("perf symbols:
> Remove symbol_filter_t machinery").

That's unfortunate. The exclude list is huge in removing redundant callchains 
and getting more relevant information on the screen with a stack depth of 5. 
I'll see what I can cook up with the existing code.



Re: [PATCH 2/2] x86/intel_rdt: Update task closid immediately on CPU in rmdir and unmount

2016-11-25 Thread Fenghua Yu
On Wed, Nov 23, 2016 at 03:23:50PM +0100, Thomas Gleixner wrote:
> On Fri, 18 Nov 2016, Fenghua Yu wrote:
> Reworked untested patch below.

The reworked patch passes my baisc tests. But I have a quesiton on
rdt_move_group_tasks() (please see below).

> 
> Thanks,
> 
>   tglx
> 
> 8<-
> --- a/arch/x86/kernel/cpu/intel_rdt_rdtgroup.c
> +++ b/arch/x86/kernel/cpu/intel_rdt_rdtgroup.c
> @@ -194,12 +194,13 @@ static int rdtgroup_cpus_show(struct ker
>  /*
>   * This is safe against intel_rdt_sched_in() called from __switch_to()
>   * because __switch_to() is executed with interrupts disabled. A local call
> - * from rdt_update_percpu_closid() is proteced against __switch_to() because
> + * from rdt_update_closid() is proteced against __switch_to() because
>   * preemption is disabled.
>   */
> -static void rdt_update_cpu_closid(void *v)
> +static void rdt_update_cpu_closid(void *closid)
>  {
> - this_cpu_write(cpu_closid, *(int *)v);
> + if (closid)
> + this_cpu_write(cpu_closid, *(int *)closid);
>   /*
>* We cannot unconditionally write the MSR because the current
>* executing task might have its own closid selected. Just reuse
> @@ -208,14 +209,23 @@ static void rdt_update_cpu_closid(void *
>   intel_rdt_sched_in();
>  }
>  
> -/* Update the per cpu closid and eventually the PGR_ASSOC MSR */
> -static void rdt_update_percpu_closid(const struct cpumask *cpu_mask, int 
> closid)
> +/*
> + * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
> + *
> + * Per task closids must have been set up before calling this function.
> + *
> + * The per cpu closids are updated with the smp function call, when @closid
> + * is not NULL. If @closid is NULL then all affected percpu closids must
> + * have been set up before calling this function.
> + */
> +static void
> +rdt_update_closid(const struct cpumask *cpu_mask, int *closid)
>  {
>   int cpu = get_cpu();
>  
>   if (cpumask_test_cpu(cpu, cpu_mask))
> - rdt_update_cpu_closid(&closid);
> - smp_call_function_many(cpu_mask, rdt_update_cpu_closid, &closid, 1);
> + rdt_update_cpu_closid(closid);
> + smp_call_function_many(cpu_mask, rdt_update_cpu_closid, closid, 1);
>   put_cpu();
>  }
>  
> @@ -264,7 +274,7 @@ static ssize_t rdtgroup_cpus_write(struc
>   /* Give any dropped cpus to rdtgroup_default */
>   cpumask_or(&rdtgroup_default.cpu_mask,
>  &rdtgroup_default.cpu_mask, tmpmask);
> - rdt_update_percpu_closid(tmpmask, rdtgroup_default.closid);
> + rdt_update_closid(tmpmask, &rdtgroup_default.closid);
>   }
>  
>   /*
> @@ -278,7 +288,7 @@ static ssize_t rdtgroup_cpus_write(struc
>   continue;
>   cpumask_andnot(&r->cpu_mask, &r->cpu_mask, tmpmask);
>   }
> - rdt_update_percpu_closid(tmpmask, rdtgrp->closid);
> + rdt_update_closid(tmpmask, &rdtgrp->closid);
>   }
>  
>   /* Done pushing/pulling - update this group with new mask */
> @@ -807,18 +817,49 @@ static int reset_all_cbms(struct rdt_res
>  }
>  
>  /*
> - * Forcibly remove all of subdirectories under root.
> + * Move tasks from one to the other group. If @from is NULL, then all tasks
> + * in the systems are moved unconditionally (used for teardown).
> + *
> + * If @mask is not NULL the cpus on which moved tasks are running are set
> + * in that mask so the update smp function call is restricted to affected
> + * cpus.
>   */
> -static void rmdir_all_sub(void)
> +static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
> +  struct cpumask *mask)
>  {
> - struct rdtgroup *rdtgrp, *tmp;
>   struct task_struct *p, *t;
>  
> - /* move all tasks to default resource group */
>   read_lock(&tasklist_lock);
> - for_each_process_thread(p, t)
> - t->closid = 0;
> + for_each_process_thread(p, t) {
> + if (!from || t->closid == from->closid) {
> + t->closid = to->closid;
> +#ifdef CONFIG_SMP
> + /*
> +  * This is safe on x86 w/o barriers as the ordering
> +  * of writing to task_cpu() and t->on_cpu is
> +  * reverse to the reading here. The detection is
> +  * inaccurate as tasks might move or schedule
> +  * before the smp function call takes place. In
> +  * such a case the function call is pointless, but
> +  * there is no other side effect.
> +  */

If process p1 is running on CPU1 before this point,

> + if (mask && t->on_cpu)
> + cpumask_set_cpu(task_cpu(t), mask);

If between CPU1 is set in mask and rdt_update_closid(tmpmask, NULL) is
called, p1 is switched to CPU2, and process p2 with its own closid
(e.g. 2)

Xmas Offer

2016-11-25 Thread Mrs Julie Leach
You are a recipient to Mrs Julie Leach Donation of $3 million USD. Contact ( 
julieleac...@gmail.com ) for claims.


Re: [PATCH net] mvpp2: use correct size for memset

2016-11-25 Thread David Miller
From: Arnd Bergmann 
Date: Thu, 24 Nov 2016 17:28:12 +0100

> gcc-7 detects a short memset in mvpp2, introduced in the original
> merge of the driver:
> 
> drivers/net/ethernet/marvell/mvpp2.c: In function 'mvpp2_cls_init':
> drivers/net/ethernet/marvell/mvpp2.c:3296:2: error: 'memset' used with length 
> equal to number of elements without multiplication by element size 
> [-Werror=memset-elt-size]
> 
> The result seems to be that we write uninitialized data into the
> flow table registers, although we did not get any warning about
> that uninitialized data usage.
> 
> Using sizeof() lets us initialize then entire array instead.
> 
> Fixes: 3f518509dedc ("ethernet: Add new driver for Marvell Armada 375 network 
> unit")
> Signed-off-by: Arnd Bergmann 

Applied, thanks.


Re: [PATCH] irda: fix overly long udelay()

2016-11-25 Thread David Miller
From: Arnd Bergmann 
Date: Thu, 24 Nov 2016 17:26:22 +0100

> irda_get_mtt() returns a hardcoded '1' in some cases,
> and with gcc-7, we get a build error because this triggers a
> compile-time check in udelay():
> 
> drivers/net/irda/w83977af_ir.o: In function `w83977af_hard_xmit':
> w83977af_ir.c:(.text.w83977af_hard_xmit+0x14c): undefined reference to 
> `__bad_udelay'
> 
> Older compilers did not run into this because they either did not
> completely inline the irda_get_mtt() or did not consider the
> 1 value a constant expression.
> 
> The code has been wrong since the start of git history.
> 
> Signed-off-by: Arnd Bergmann 
 ...
> @@ -518,7 +518,9 @@ static netdev_tx_t w83977af_hard_xmit(struct sk_buff *skb,
>   
>   mtt = irda_get_mtt(skb);
>   pr_debug("%s(%ld), mtt=%d\n", __func__ , jiffies, mtt);
> - if (mtt)
> + if (mtt > 1000)
> + mdelay(mtt/1000);
> + else if (mtt)
>   udelay(mtt);

I know this isn't caused by you, but wow what is going on with the
indentation here?!?!?


RE: [patch v3 1/1] platform/x86: move module mlx-platform from arch/x86 to drivers/platform/x86

2016-11-25 Thread Vadim Pasternak


> -Original Message-
> From: Andy Shevchenko [mailto:andy.shevche...@gmail.com]
> Sent: Friday, November 25, 2016 12:03 PM
> To: Vadim Pasternak 
> Cc: Thomas Gleixner ; dvh...@infradead.org; platform-
> driver-...@vger.kernel.org; x...@kernel.org; linux-kernel@vger.kernel.org;
> j...@resnulli.us; andriy.shevche...@linux.intel.com
> Subject: Re: [patch v3 1/1] platform/x86: move module mlx-platform from
> arch/x86 to drivers/platform/x86
> 
> On Fri, Nov 25, 2016 at 6:14 AM, Vadim Pasternak 
> wrote:
> >> Thanks for reply.
> >>
> >> > First of all, please avoid top posting.
> >>
> >> Sorry for that.
> >> I posted on top, because it doesn't come as reply to something (but
> >> it seems I should post at bottom in suc case).
> >>
> >> > I will process the pdx86 mailing list and queue this week.
> >
> > I am very sorry for reminding, but I see that it's still not merged.
> 
> 
> Sorry for that, we encounter one issue with repository permissions, so, 
> currently
> I can't do much. We are trying to resolve this as soon as possible.
> We still have time before merge window (one week or so).
> 
Hi Andy,

OK, I see.
I just have another patch, you already made review for that.
And I am waiting for the merge, in order to send this patch (this is the change 
in mlx-platform.c module, which is going to migrate).
And I am afraid I can missed merge window for it.

Thanks,
Vadim.

> --
> With Best Regards,
> Andy Shevchenko


Re: [PATCH] net: ieee802154: drop duplicate header delay.h

2016-11-25 Thread David Miller
From: Geliang Tang 
Date: Thu, 24 Nov 2016 21:58:32 +0800

> Drop duplicate header delay.h from adf7242.c.
> 
> Signed-off-by: Geliang Tang 

Applied.


Re: [PATCH] net/mlx5: drop duplicate header delay.h

2016-11-25 Thread David Miller
From: Geliang Tang 
Date: Thu, 24 Nov 2016 21:58:33 +0800

> Drop duplicate header delay.h from mlx5/core/main.c.
> 
> Signed-off-by: Geliang Tang 

Applied.


Re: [PATCH] ibmvnic: drop duplicate header seq_file.h

2016-11-25 Thread David Miller
From: Geliang Tang 
Date: Thu, 24 Nov 2016 21:58:29 +0800

> Drop duplicate header seq_file.h from ibmvnic.c.
> 
> Signed-off-by: Geliang Tang 

Applied.


Re: [patch] net/mlx5: remove a duplicate condition

2016-11-25 Thread David Miller
From: Dan Carpenter 
Date: Thu, 24 Nov 2016 14:03:45 +0300

> We verified that MLX5_FLOW_CONTEXT_ACTION_COUNT was set on the first
> line of the function so we don't need to check again here.
> 
> Signed-off-by: Dan Carpenter 

Applied.


Re: [PATCH 0/4] net: thunderx: Support for 80xx, RED, PFC e.t.c

2016-11-25 Thread David Miller
From: sunil.kovv...@gmail.com
Date: Thu, 24 Nov 2016 14:47:59 +0530

> This patch series adds support for SLM modules present on 80xx
> silicon, enables ramdom early discard, backpressure generation,
> PFC and some ethtool changes to display supported link modes e.t.c.

Series applied to net-next.


[PATCH v4 1/2] perf sdt: add scanning of sdt probles arguments

2016-11-25 Thread Alexis Berlemont
During a "perf buildid-cache --add" command, the section
".note.stapsdt" of the "added" binary is scanned in order to list the
available SDT markers available in a binary. The parts containing the
probes arguments were left unscanned.

The whole section is now parsed; the probe arguments are extracted for
later use.

Signed-off-by: Alexis Berlemont 
---
 tools/perf/util/symbol-elf.c | 25 +++--
 tools/perf/util/symbol.h |  1 +
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 99400b0..7725c3f 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1822,7 +1822,7 @@ void kcore_extract__delete(struct kcore_extract *kce)
 static int populate_sdt_note(Elf **elf, const char *data, size_t len,
 struct list_head *sdt_notes)
 {
-   const char *provider, *name;
+   const char *provider, *name, *args;
struct sdt_note *tmp = NULL;
GElf_Ehdr ehdr;
GElf_Addr base_off = 0;
@@ -1881,6 +1881,25 @@ static int populate_sdt_note(Elf **elf, const char 
*data, size_t len,
goto out_free_prov;
}
 
+   args = memchr(name, '\0', data + len - name);
+
+   /*
+* There is no argument if:
+* - We reached the end of the note;
+* - There is not enough room to hold a potential string;
+* - The argument string is empty or just contains ':'.
+*/
+   if (args == NULL || data + len - args < 2 ||
+   args[1] == ':' || args[1] == '\0')
+   tmp->args = NULL;
+   else {
+   tmp->args = strdup(++args);
+   if (!tmp->args) {
+   ret = -ENOMEM;
+   goto out_free_name;
+   }
+   }
+
if (gelf_getclass(*elf) == ELFCLASS32) {
memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
tmp->bit32 = true;
@@ -1892,7 +1911,7 @@ static int populate_sdt_note(Elf **elf, const char *data, 
size_t len,
if (!gelf_getehdr(*elf, &ehdr)) {
pr_debug("%s : cannot get elf header.\n", __func__);
ret = -EBADF;
-   goto out_free_name;
+   goto out_free_args;
}
 
/* Adjust the prelink effect :
@@ -1917,6 +1936,8 @@ static int populate_sdt_note(Elf **elf, const char *data, 
size_t len,
list_add_tail(&tmp->note_list, sdt_notes);
return 0;
 
+out_free_args:
+   free(tmp->args);
 out_free_name:
free(tmp->name);
 out_free_prov:
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index dec7e2d4..db1953e 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -348,6 +348,7 @@ int arch__choose_best_symbol(struct symbol *syma, struct 
symbol *symb);
 struct sdt_note {
char *name; /* name of the note*/
char *provider; /* provider name */
+   char *args;
bool bit32; /* whether the location is 32 bits? */
union { /* location, base and semaphore addrs */
Elf64_Addr a64[3];
-- 
2.10.2



[PATCH v4 2/2] perf probe: add sdt probes arguments into the uprobe cmd string

2016-11-25 Thread Alexis Berlemont
An sdt probe can be associated with arguments but they were not passed
to the user probe tracing interface (uprobe_events); this patch adapts
the sdt argument descriptors according to the uprobe input format.

As the uprobe parser does not support scaled address mode, perf will
skip arguments which cannot be adapted to the uprobe format.

Here are the results:

$ perf buildid-cache -v --add test_sdt
$ perf probe -x test_sdt sdt_libfoo:table_frob
$ perf probe -x test_sdt sdt_libfoo:table_diddle
$ perf record -e sdt_libfoo:table_frob -e sdt_libfoo:table_diddle test_sdt
$ perf script
test_sdt  ...   666.255678:   sdt_libfoo:table_frob: (4004d7) arg0=0 arg1=0
test_sdt  ...   666.255683: sdt_libfoo:table_diddle: (40051a) arg0=0 arg1=0
test_sdt  ...   666.255686:   sdt_libfoo:table_frob: (4004d7) arg0=1 arg1=2
test_sdt  ...   666.255689: sdt_libfoo:table_diddle: (40051a) arg0=3 arg1=4
test_sdt  ...   666.255692:   sdt_libfoo:table_frob: (4004d7) arg0=2 arg1=4
test_sdt  ...   666.255694: sdt_libfoo:table_diddle: (40051a) arg0=6 arg1=8

Signed-off-by: Alexis Berlemont 
---
 tools/perf/arch/x86/util/perf_regs.c |  18 
 tools/perf/util/perf_regs.c  |   4 +
 tools/perf/util/perf_regs.h  |  13 +++
 tools/perf/util/probe-file.c | 169 ++-
 4 files changed, 200 insertions(+), 4 deletions(-)

diff --git a/tools/perf/arch/x86/util/perf_regs.c 
b/tools/perf/arch/x86/util/perf_regs.c
index c5db14f..52a1e65 100644
--- a/tools/perf/arch/x86/util/perf_regs.c
+++ b/tools/perf/arch/x86/util/perf_regs.c
@@ -26,3 +26,21 @@ const struct sample_reg sample_reg_masks[] = {
 #endif
SMPL_REG_END
 };
+
+const struct sdt_name_reg sdt_reg_renamings[] = {
+   SDT_NAME_REG(eax, ax),
+   SDT_NAME_REG(rax, ax),
+   SDT_NAME_REG(ebx, bx),
+   SDT_NAME_REG(rbx, bx),
+   SDT_NAME_REG(ecx, cx),
+   SDT_NAME_REG(rcx, cx),
+   SDT_NAME_REG(edx, dx),
+   SDT_NAME_REG(rdx, dx),
+   SDT_NAME_REG(esi, si),
+   SDT_NAME_REG(rsi, si),
+   SDT_NAME_REG(edi, di),
+   SDT_NAME_REG(rdi, di),
+   SDT_NAME_REG(ebp, bp),
+   SDT_NAME_REG(rbp, bp),
+   SDT_NAME_REG_END,
+};
diff --git a/tools/perf/util/perf_regs.c b/tools/perf/util/perf_regs.c
index c4023f2..1c21150 100644
--- a/tools/perf/util/perf_regs.c
+++ b/tools/perf/util/perf_regs.c
@@ -6,6 +6,10 @@ const struct sample_reg __weak sample_reg_masks[] = {
SMPL_REG_END
 };
 
+const struct sdt_name_reg __weak sdt_reg_renamings[] = {
+   SDT_NAME_REG_END,
+};
+
 #ifdef HAVE_PERF_REGS_SUPPORT
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
 {
diff --git a/tools/perf/util/perf_regs.h b/tools/perf/util/perf_regs.h
index 679d6e4..41815ca 100644
--- a/tools/perf/util/perf_regs.h
+++ b/tools/perf/util/perf_regs.h
@@ -15,6 +15,19 @@ struct sample_reg {
 
 extern const struct sample_reg sample_reg_masks[];
 
+struct sdt_name_reg {
+   const char *sdt_name;
+   const char *uprobe_name;
+};
+#define SDT_NAME_REG(n, m) {.sdt_name = "%" #n, .uprobe_name = "%" #m}
+#define SDT_NAME_REG_END {.sdt_name = NULL, .uprobe_name = NULL}
+
+/*
+ * The table sdt_reg_renamings is used for adjusting gcc/gas-generated
+ * registers before filling the uprobe tracer interface.
+ */
+extern const struct sdt_name_reg sdt_reg_renamings[];
+
 #ifdef HAVE_PERF_REGS_SUPPORT
 #include 
 
diff --git a/tools/perf/util/probe-file.c b/tools/perf/util/probe-file.c
index 436b647..75033c7 100644
--- a/tools/perf/util/probe-file.c
+++ b/tools/perf/util/probe-file.c
@@ -27,6 +27,7 @@
 #include "probe-event.h"
 #include "probe-file.h"
 #include "session.h"
+#include "perf_regs.h"
 
 #define MAX_CMDLEN 256
 
@@ -687,6 +688,165 @@ static unsigned long long sdt_note__get_addr(struct 
sdt_note *note)
 : (unsigned long long)note->addr.a64[0];
 }
 
+static const char * const type_to_suffix[] = {
+   ":s64", "", "", "", ":s32", "", ":s16", ":s8",
+   "", ":u8", ":u16", "", ":u32", "", "", "", ":u64"
+};
+
+static int synthesize_sdt_probe_arg(struct strbuf *buf, int i, const char *arg)
+{
+   const struct sdt_name_reg *rnames;
+   char *tmp, *desc = strdup(arg);
+   const char *prefix = "", *suffix = "";
+   int ret = -1;
+
+   if (desc == NULL) {
+   pr_debug4("Allocation error\n");
+   return ret;
+   }
+
+   tmp = strchr(desc, '@');
+   if (tmp) {
+   long type_idx;
+   /*
+* Isolate the string number and convert it into a
+* binary value; this will be an index to get suffix
+* of the uprobe name (defining the type)
+*/
+   tmp[0] = '\0';
+   type_idx = strtol(desc, NULL, 10);
+   if (type_idx == LONG_MIN ||
+   type_idx == LONG_MAX) {
+   pr_debug4("Failed to get sdt type\n");
+   goto error;
+   }
+   suffix = type_to

[PATCH v4 0/2] perf probe: add sdt probes arguments into the uprobe cmd string

2016-11-25 Thread Alexis Berlemont
Hi Arnaldo,

Here is another patch set which fixes the issues you noticed.

Thank you.

Alexis.

Alexis Berlemont (2):
  perf sdt: add scanning of sdt probles arguments
  perf probe: add sdt probes arguments into the uprobe cmd string

 tools/perf/arch/x86/util/perf_regs.c |  18 
 tools/perf/util/perf_regs.c  |   4 +
 tools/perf/util/perf_regs.h  |  13 +++
 tools/perf/util/probe-file.c | 169 ++-
 tools/perf/util/symbol-elf.c |  25 +-
 tools/perf/util/symbol.h |   1 +
 6 files changed, 224 insertions(+), 6 deletions(-)

-- 
2.10.2



Re: [PATCH] x86/kbuild: enable modversions for symbols exported from asm

2016-11-25 Thread Nicholas Piggin
On Fri, 25 Nov 2016 10:00:46 -0800
Linus Torvalds  wrote:

> On Thu, Nov 24, 2016 at 4:40 PM, Nicholas Piggin  wrote:
> >>
> >> Yes, manual "marking" is never going to be a viable solution.  
> >
> > I guess it really depends on how exactly you want to use it. For distros
> > that do stable ABI but rarely may have to break something for security
> > reasons, it should work and give exact control.  
> 
> No. Because nobody else will care, so unless it's like a single symbol
> or something, it will just be a maintenance nightmare.

Yeah that's true, and as I realized a distro can rename a symbol if they
make incompatible changes which happens very rarely. Avoids having to
carry some whole infrastructure upstream for it.

> 
> > What else do people *actually* use it for? Preventing mismatched modules
> > when .git version is not attached and release version of the kernel has
> > not been bumped. Is that it?  
> 
> It used to be very useful for avoiding loading stale modules and then
> wasting days on debugging something that wasn't the case when you had
> forgotten to do "make modules_install". Change some subtle internal
> ABI issue (add/remove a parameter, whatever) and it would really help.
> 
> These days, for me, LOCALVERSION_AUTO and module signing are what I
> personally tend to use.
> 
> The modversions stuff may just be too painful to bother with. Very few
> people probably use it, and the ones that do likely don't have any
> overriding reason why.
> 
> So I'd personally be ok with just saying "let's disable it for now",
> and see if anybody even notices and cares, and then has a good enough
> explanation of why. It's entirely possible that most users are "I
> enabled it ten years ago, I didn't even realize it was still in my
> defconfig".

That sounds good. Should we try to get 4.9 working (which we could
do relatively easily with a few arch reverts), and then disable
modversions for 4.10? (at which point we can un-revert Al's arch
patches)

Thanks,
Nick


Re: [PATCH RFC] hlist_add_tail_rcu disable sparse warning

2016-11-25 Thread David Miller
From: "Michael S. Tsirkin" 
Date: Wed, 23 Nov 2016 22:48:19 +0200

> I would appreciate review to confirm the function doesn't
> do anything unsafe though.
> 
> In particular, should this use __hlist_for_each_rcu instead?
> I note that __hlist_for_each_rcu does rcu_dereference
> internally, which is missing here.

I personally think it should use __hlist_for_each_rcu, otherwise
nothing expresses the rcu-ness of the operation.


Re: [PATCH v18 0/4] Introduce usb charger framework to deal with the usb gadget power negotation

2016-11-25 Thread NeilBrown
On Sat, Nov 26 2016, Mark Brown wrote:

> [ Unknown signature status ]
> On Tue, Nov 22, 2016 at 09:40:07AM +1100, NeilBrown wrote:
>
>> I agree that the question of where the responsibility for information
>> aggregation lies is open for discussion. If fact all details on how
>> things should work are always open for discussion.
>> I don't agree that this is the main different between our positions,
>> though I can see how you might get that impression.
>
>> You could even fix them so they look *exactly* like the notifiers that
>> Baolin is proposing.  This is my key point.  It is not the end result
>> that I particularly object to (though I do object to some details).  It
>
> Ah, OK.  This really hadn't been at all clear - both Baolin and I had
> the impression that the it was both that were blockers for you.  What
> were the details here?

I don't really like the idea of a separate "usb charger" object.  It
looks too much like a "midlayer" and they tend to get in the way.  But
if a convincing case could be made that changing from the current design
to that aspect of the proposed design brings measurable benefits, then I
would certainly assess that case on its merits.  No such case was made,
and the patchset didn't seem to even acknowledge the existing design.

When I said "I do object to some details" it was details of the end
result, not details of what took responsibility of information
aggregation (in case that wasn't clear).  Those details were everything
that duplicated existing functionality, or ignored existing
functionality, or was simply unworkable.  e.g. the lack of proper
integration with extcon, the new sysfs attributes, the name-lookup
mechanism.  Probably others.

>
>> is the process of getting to the end result that I don't like.  If the
>> current system doesn't work and something different is needed, then the
>> correct thing to do is to transform the existing system into something
>> new that works better.  This should be a clear series of steps.  Each
>
> Sometimes there's something to be said for working out what we want
> things to look like before setting out to make these gradual
> refactorings and sometimes the refactorings are just more pain than
> they're worth, especially when they go across subsystems.  In this case
> I do worry about the cross subsystem aspect causing hassle, it may be
> more practical to do anything that represents an interface change by
> adding the new interface, converting the users to it and then removing
> the old interface.

Yes, you need a clear vision of the goal.  You also need a clear vision
of the starting point. There was no evidence of the latter.
Yes, sometimes you need to create a new thing and transition users over,
then discard the old.  There was no discarding of the old.

>
> At the very least the series should grow to incorporate conversion of
> the existing users though.  Baolin, I think this does need adding to the
> series but probably best to think about how to do it - some of Neil's
> suggestions for incremental steps do seem like they should be useful
> for organizing things here, probably we can get some things that can be
> done internally within individual drivers merged while everything else
> is under discussion.

I would be very encouraged to see those simple things done first!
Seeing the series grow isn't much fun, but seeing preliminary work land
certainly is.

>
>> But I think here my key point got lost too, in part because it was hard
>> to refer to an actual instance.
>> My point was that in the present patch set, the "usb charger" is given
>> a name which is dependant on discovery order, and only supports
>> lookup-by-name.  This cannot work.
>
> There's two bits here: one is the way names are assigned and the other
> is the lookup by name.  I agree that the lookup by name isn't
> particularly useful as things stand, that could just be dropped until
> some naming mechanism is added.  We'd be more likely to use phandles in
> DT systems, I don't know what ACPI systems would look like but I guess
> it'd be something similar.
>
>> If they supported lookup by phy-name or lookup-by-active (i.e. "find me
>> any usb-charger which has power available"), or look up by some other
>> attribute, then discover-order naming could work.  But the only
>> lookup-mechanism is by-name, and the names aren't reliably stable.  So
>> the name/lookup system proposed cannot possibly do anything useful
>> with more than one usb_charger.
>
> Baolin, I think adding a DT binding and lookup mechanism makes sense
> here - do you agree?

We already have a lookup mechanism for a battery charger to find the phy
that it gets current from: devm_usb_get_phy_by_phandle() (or even
devm_usb_get_phy() if there is known to only be one phy).  We would need
a case to be made that the existing mechanism cannot be used before we
consider "adding a DT binding and lookup mechanism".

Thanks,
NeilBrown


signature.asc
Description: PGP signature


Re: [PATCH] cpuset: Remove unused 'struct cpuset*' variable

2016-11-25 Thread Zefan Li
On 2016/11/25 17:46, Arnd Bergmann wrote:
> On Friday, November 25, 2016 1:46:04 PM CET Zefan Li wrote:
>> On 2016/11/25 12:55, Kirtika Ruchandani wrote:
>>> 'struct cpuset* cs' that is set but not used, was introduced in commit
>>> 1f7dd3e5a6e4 ("cgroup: fix handling of multi-destination migration from 
>>> subtree_control enabling").
>>> cpuset_cancel_attach() uses css_cs(css) instead. Compiling with W=1
>>> gives the folllowing harmless warning, which we'd like to fix to
>>> reduce the noise with W=1 in the kernel.
>>>
>>> kernel/cpuset.c: In function ‘cpuset_cancel_attach’:
>>> kernel/cpuset.c:1502:17: warning: variable ‘cs’ set but not used 
>>> [-Wunused-but-set-variable]
>>>   struct cpuset *cs;
>>>  ^
>>>
>>> Fixes: 1f7dd3e5a6e4 ("cgroup: fix handling of multi-destination migration 
>>> from subtree_control enabling").
>>
>> This isn't a bug, so I don't think this tag is proper.
> 
> I think it's ok since the changelog makes it clear that the
> warning is harmless. It's still useful information to know
> what commit introduced the warning, and the warning is fixed
> by this patch.
> 

People like stable tree maintainers use scripts to find out bug fixes
that needs to be backported to older kernels, and those scripts tracks
the Fixes tag. No doubt this patch doesn't require backporting, so
it's better avoid using this tag.



Re: [lustre-devel] [PATCH 09/10] staging: lustre: libcfs: remove zero comparisons in headers

2016-11-25 Thread Dilger, Andreas
On Nov 18, 2016, at 09:48, James Simmons  wrote:
> 
> Remove the zero comparisions in the libcfs headers.
> 
> Signed-off-by: James Simmons 
> ---
> .../lustre/include/linux/libcfs/libcfs_crypto.h|2 +-
> .../lustre/include/linux/libcfs/libcfs_fail.h  |4 +-
> .../lustre/include/linux/libcfs/libcfs_hash.h  |   32 ++--
> 3 files changed, 19 insertions(+), 19 deletions(-)
> 
> diff --git a/drivers/staging/lustre/include/linux/libcfs/libcfs_crypto.h 
> b/drivers/staging/lustre/include/linux/libcfs/libcfs_crypto.h
> index a0865e5..8f34c5d 100644
> --- a/drivers/staging/lustre/include/linux/libcfs/libcfs_crypto.h
> +++ b/drivers/staging/lustre/include/linux/libcfs/libcfs_crypto.h
> @@ -137,7 +137,7 @@ static inline unsigned char cfs_crypto_hash_alg(const 
> char *algname)
>   enum cfs_crypto_hash_alg hash_alg;
> 
>   for (hash_alg = 0; hash_alg < CFS_HASH_ALG_MAX; hash_alg++)
> - if (strcmp(hash_types[hash_alg].cht_name, algname) == 0)
> + if (!strcmp(hash_types[hash_alg].cht_name, algname))

I'd really rather keep the "== 0" comparison for strcmp(), because IMHO
!strcmp() reads like "the strings do not compare the same" and is confusing.

> static inline int
> cfs_hash_is_iterating(struct cfs_hash *hs)
> {
>   /* someone is calling cfs_hash_for_each_* */
> - return hs->hs_iterating || hs->hs_iterators != 0;
> + return hs->hs_iterating || hs->hs_iterators;

Likewise, I'd rather keep comparisons == 0 or != 0 for cases where the
variable is an actual number instead of just a return code or a pointer.

I don't think we need to be totally dogmatic about removing every comparison
with 0 in all of the code, as that can reduce readability in some cases.

I know this patch has already landed, and it isn't the end of the world
either way, but just wanted to forestall similar changes being made through
the rest of the code.

Cheers, Andreas


Re: Enabling peer to peer device transactions for PCIe devices

2016-11-25 Thread Alex Deucher
On Fri, Nov 25, 2016 at 2:34 PM, Jason Gunthorpe
 wrote:
> On Fri, Nov 25, 2016 at 12:16:30PM -0500, Serguei Sagalovitch wrote:
>
>> b) Allocation may not  have CPU address  at all - only GPU one.
>
> But you don't expect RDMA to work in the case, right?
>
> GPU people need to stop doing this windowed memory stuff :)
>

Blame 32 bit systems and GPUs with tons of vram :)

I think resizable bars are finally coming in a useful way so this
should go away soon.

Alex


Re: [PATCH] ARM: pxa: ezx: fix a910 camera data

2016-11-25 Thread Stefan Schmidt
Hello.

On 25.11.2016 20:53, Robert Jarzmik wrote:
> Stefan Schmidt  writes:
> 
>> Hello.
>>
>> On 24.11.2016 17:29, Arnd Bergmann wrote:
>>> The camera_supply_dummy_device definition is shared between a780 and a910,
>>> but only provided when the first is enabled and fails to build for a
>>> configuration with only a910:
>>>
>>> arch/arm/mach-pxa/ezx.c:1097:3: error: 'camera_supply_dummy_device' 
>>> undeclared here (not in a function)
>>>
>>> This moves the definition into its own section.
>>>
>>> Fixes: 6c1b417adc8f ("ARM: pxa: ezx: use the new pxa_camera platform_data")
>>> Signed-off-by: Arnd Bergmann 
>>> ---
>>>  arch/arm/mach-pxa/ezx.c | 56 
>>> ++---
>>
>> I wonder what we should do with ezx.c.
>>
>> As far as I know neither Daniel nor Harald or myself are doing anything
>> with this devices anymore. Besides a basic compile test having an ack or
>> reviewed by from our side is a bit worthless. :/
>>
>> I should still have some of these phones around in a box somewhere. If
>> there is someone with a good motivation and time to take over on this
>> platform we will find a way to get the person this devices.
>>
>> Any takers? Robert? I guess you are already overloaded but you might
>> also have an interest. Worth asking :)
> Oh yes, I'm very interested in your box. Besides I really like old platforms
> :)


Great! I should have at least 3 or 4 different devices from the EZX
platform around. I will go and search for the box over the weekend :)

>> In the case nobody wants to pick up here what would you consider the
>> bets way forward? I could send a patch removing ezx platform support
>> from the kernel (basically ezx.c plus build support) or I can send a
>> patch marking it at least orphan in MAINTAINERS. Let me know what you think.
>>
>> Daniel, Harald, if one of you is still interested in these and what to
>> pick up the work again, please speak up now. :)
> Unless another maintainer steps in, you can submit a patch to transfer the
> maintainance onto me, and we'll see off mailing lists how we could arange the
> boards transfer.

I cc'ed another developer who did a lot of work regarding EZX.

Antonio, as you can see from the mail above we are pondering what who
will maintain the ezx platform in the kernel going forward. Neither
Daniel, Harald or me is going to do so. If you have time, interest and
motivation to do so please speak up. I know life moved on and you ahve
other projects and interests so do not feel pressured here. Just say no
if you have no interest. Robert already agreed to act as a fallback so
we would still be safe. :)

regards
Stefan Schmidt


Re: [PATCH 3/6] dax: add tracepoint infrastructure, PMD tracing

2016-11-25 Thread Linus Torvalds
On Fri, Nov 25, 2016 at 1:48 PM, Theodore Ts'o  wrote:
>
> There is a reason why people want to be able to do that, and that's
> because kprobes doesn't give you access to the arguments and return
> codes to the functions.

Honestly, that's simply not a good reason.

What if everybody did this? Do we pollute the whole kernel with this crap? No.

And if not, then what's so special about something like afs that it
would make sense there?

The thing is, with function tracing, you *can* get the return value
and arguments. Sure, you'll probably need to write eBPF and just
attach it to that fentry call point, and yes, if something is inlined
you're just screwed, but Christ, if you do debugging that way you
shouldn't be writing kernel code in the first place.

If you cannot do filesystem debugging without tracing every single
function entry, you are doing something seriously wrong. Add a couple
of relevant and valid trace points to get the initial arguments etc
(and perhaps to turn on the function tracing going down the stack).

> After all, we need *some* way of saying this can never be considered
> stable.

Oh, if you pollute the kernel with random idiotic trace points, not
only are they not going to be considered stable, after a while people
should stop pulling from you.

I do think we should probably add a few generic VFS level breakpoints
to make it easier for people to catch the arguments they get from the
VFS layer (not every system call - if you're a filesystem person, you
_shouldn't_ care about all the stuff that the VFS layer caches for you
so that you never even have to see it). I do think that Al's "no trace
points what-so-ever" is too strict.

But I think a lot of people add complete crap with the "maybe it's
needed some day" kind of mentality.

The tracepoints should have a good _specific_ reason, and they should
make sense. Not be randomly sprinkled "just because".

 Linus


Re: [PATCH] ARM: lpc32xx: drop duplicate header device.h

2016-11-25 Thread Arnd Bergmann
On Monday, November 21, 2016 3:59:05 PM CET Sylvain Lemieux wrote:
> On Fri, 2016-11-18 at 22:21 +0800, Geliang Tang wrote:
> > Drop duplicate header device.h from phy3250.c.
> > 
> > Signed-off-by: Geliang Tang 

> Reviewed-by: Sylvain Lemieux 
> 

Applied into arm-soc/next/fixes-non-critical, I see we already merged
your other pull requests, so it seems appropriate to take this
trivial change directly.

Arnd


Re: [PATCH] ARM: ixp4xx: drop duplicate header gpio.h

2016-11-25 Thread Arnd Bergmann
On Friday, November 18, 2016 10:21:10 PM CET Geliang Tang wrote:
> Drop duplicate header gpio.h from dsmg600-setup.c.
> 
> Signed-off-by: Geliang Tang 
> 

Applied to arm-soc/next/fixes-non-critical, thanks

Arnd



Re: [GIT PULL] ARM: at91: drivers for 4.10

2016-11-25 Thread Arnd Bergmann
On Monday, November 14, 2016 7:44:38 PM CET Alexandre Belloni wrote:
> Drivers for 4.10:
> 
>  - few fixes for the memory drivers
>  - minimal security module driver
>  - support for the Secure SRAM
> 

For reference, Olof pulled this into next/drivers on Nov 18.

Arnd


Re: [lustre-devel] [patch] staging: lustre/ptlrpc: small leak on allocation failure

2016-11-25 Thread Dilger, Andreas
On Nov 24, 2016, at 04:12, Dan Carpenter  wrote:
> 
> We should free "desc" before returning NULL.
> 
> Signed-off-by: Dan Carpenter 

Reviewed-by: Andreas Dilger 

> diff --git a/drivers/staging/lustre/lustre/ptlrpc/client.c 
> b/drivers/staging/lustre/lustre/ptlrpc/client.c
> index ac959ef..8047413 100644
> --- a/drivers/staging/lustre/lustre/ptlrpc/client.c
> +++ b/drivers/staging/lustre/lustre/ptlrpc/client.c
> @@ -128,12 +128,12 @@ struct ptlrpc_bulk_desc *ptlrpc_new_bulk(unsigned int 
> nfrags,
>   GET_KIOV(desc) = kcalloc(nfrags, sizeof(*GET_KIOV(desc)),
>GFP_NOFS);
>   if (!GET_KIOV(desc))
> - goto out;
> + goto free_desc;
>   } else {
>   GET_KVEC(desc) = kcalloc(nfrags, sizeof(*GET_KVEC(desc)),
>GFP_NOFS);
>   if (!GET_KVEC(desc))
> - goto out;
> + goto free_desc;
>   }
> 
>   spin_lock_init(&desc->bd_lock);
> @@ -154,7 +154,8 @@ struct ptlrpc_bulk_desc *ptlrpc_new_bulk(unsigned int 
> nfrags,
>   LNetInvalidateHandle(&desc->bd_mds[i]);
> 
>   return desc;
> -out:
> +free_desc:
> + kfree(desc);
>   return NULL;
> }
> 
> ___
> lustre-devel mailing list
> lustre-de...@lists.lustre.org
> http://lists.lustre.org/listinfo.cgi/lustre-devel-lustre.org



Re: [GIT PULL]: ARM ARTPEC changes for 4.10

2016-11-25 Thread Arnd Bergmann
On Thursday, November 10, 2016 4:09:31 PM CET Jesper Nilsson wrote:
> Please pull the below signed tag for a trio of minor changes
> adding PCIe for the ARM ARTPEC SoC.
> 
> Thanks!
> 
> /Jesper
> 
> The following changes since commit bc33b0ca11e3df46a4fa7639ba488c9d4911:
> 
>   Linux 4.9-rc4 (2016-11-05 16:23:36 -0700)
> 
> are available in the git repository at:
> 
>   git://git.kernel.org/pub/scm/linux/kernel/git/jesper/artpec.git 
> tags/artpec-for-4.10
> 
> for you to fetch changes up to fa5541fc806771a108cd2a48245a229f1ba539ea:
> 
>   ARM: dts: artpec: add pcie support (2016-11-10 15:51:10 +0100)
> 
> 
> ARTPEC changes for 4.10
> 
> 
> Niklas Cassel (3):
>   ARM: ARTPEC-6: add select MFD_SYSCON to MACH_ARTPEC6
>   ARM: ARTPEC-6: add pcie related options
>   ARM: dts: artpec: add pcie support
> 
> 

Hi Jesper and Niklas,

I just found the old pull request while going through my mail backlog.

A few things for you to remember for next time:

- please send pull requests "To: a...@kernel.org" so we know they
  are destined for arm-soc

- please split up changes to the platform code from dts changes,
  defconfig changes and driver changes. Each of them gets sent
  to Linus in a separate arm-soc branch, so we have to pull them
  in separately too

- For the signed tag, please put in a cleartext description of
  the branch, just like you describe each commit in its changelog
  text. The tag comment becomes the merge commit text.

- I've looked at the three patches individually and cherry-picked
  the first into next/soc and the third into next/dt. The patch
  "ARM: ARTPEC-6: add pcie related options" is no longer needed
  after commit e13688f ("ARM: select PCI_DOMAINS config from
  ARCH_MULTIPLATFORM"), so I dropped that.

Arnd


Re: [PATCH] orangefs: Axe some dead code

2016-11-25 Thread Mike Marshall
We're on rc7 now. Linus said in LWN that there might be a rc8 this time.
I'll try to get this pulled in 4.9-rc8 I hope, or sometime in 4.10...
it is just a
few lines of code that I don't think can be reached. Sorry for the confusion.

-Mike

On Fri, Nov 25, 2016 at 4:51 PM, Dan Carpenter  wrote:
> On Thu, Nov 24, 2016 at 07:31:11AM -0500, Mike Marshall wrote:
>> This seems like a good and proper patch to me, and simple too.
>> But like all changes, it needs tested. While I was testing it, I
>> discovered a regression in the associated userspace code. I
>> "bisected" (we use SVN for the userspace part of Orangefs)
>> down to the commit that caused the regression, and some
>> of the userspace folks are going to fix it.
>>
>> I don't think I should ask Linus to pull this patch until I can
>> test it. Since we're about to go into rc7, it might not go in
>> until the next go around...
>>
>
> Linus is on 4.9-rc7 so you're saying this would probably go into 4.11?
>
> regards,
> dan carpenter
>


Re: [PATCH net-next 1/5] net: mvneta: Use cacheable memory to store the rx buffer virtual address

2016-11-25 Thread kbuild test robot
Hi Gregory,

[auto build test ERROR on ]

url:
https://github.com/0day-ci/linux/commits/Gregory-CLEMENT/Support-Armada-37xx-SoC-ARMv8-64-bits-in-mvneta-driver/20161126-050621
base:
config: parisc-allmodconfig (attached as .config)
compiler: hppa-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
wget 
https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross
 -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=parisc 

Note: the 
linux-review/Gregory-CLEMENT/Support-Armada-37xx-SoC-ARMv8-64-bits-in-mvneta-driver/20161126-050621
 HEAD 5f44108a5c983ae4477f811485fdc4ee12294e72 builds fine.
  It only hurts bisectibility.

All errors (new ones prefixed by >>):


vim +2745 drivers/net/ethernet/marvell/mvneta.c

  2739 DMA_FROM_DEVICE);
  2740  if (unlikely(dma_mapping_error(pp->dev->dev.parent, 
phys_addr))) {
  2741  mvneta_frag_free(pp->frag_size, data);
  2742  return -ENOMEM;
  2743  }
  2744  
> 2745  phys_addr += pp->rx_offset_correction;
  2746  rx_desc->buf_phys_addr = phys_addr;
  2747  rx_desc->buf_cookie = (uintptr_t)data;
  2748  

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation


.config.gz
Description: application/gzip


Re: RFC: documentation of the autogroup feature [v2]

2016-11-25 Thread Peter Zijlstra
On Fri, Nov 25, 2016 at 05:33:23PM +0100, Michael Kerrisk (man-pages) wrote:

> Okay -- you're really quite the ASCII artist. And somehow,
> I think you needed to compose the mail in LaTeX. But thanks
> for the detail. It's helpful, for me at least.

Hehe, its been a while since I did LaTeX, so I'd probably make a mess of
it :-) Glad my ramblings made sense.

> > Note that this property, where the weight of the server entity is
> > independent from its child entities is a desired feature. Without that
> > it would be impossible to control the relative weights of groups, and
> > that is the sole parameter of the WFQ model.
> > 
> > It is also why Linus so likes autogroups, each session competes equally
> > amongst one another.
> 
> I get it. But, the behavior changes for the process nice value are
> undocumented, and they should be documented. I understand
> what the behavior change was. But not yet when.

Well, its all undocumented -- I suppose you're about to go fix that :-)

But think of it differently, think of the group as a container, then the
behaviour inside the container is exactly as expected.


Re: [RESEND PATCH] arm: assabet_defconfig: disable IDE subsystem

2016-11-25 Thread Arnd Bergmann
On Monday, October 31, 2016 7:24:46 PM CET Bartlomiej Zolnierkiewicz wrote:
> On Monday, October 31, 2016 07:14:13 PM Bartlomiej Zolnierkiewicz wrote:
> > On Monday, October 31, 2016 03:46:22 PM Russell King - ARM Linux wrote:
> > > On Wed, Oct 26, 2016 at 07:01:12PM +0200, Bartlomiej Zolnierkiewicz wrote:
> > > > > I'd be fine with just getting a pull request with all the patches that
> > > > > had no negative feedback and that were not already applied (if any).
> > > > 
> > > > Here it is (sorry for taking so long).
> > > 
> > > I've just been digging in the dmesg logs from when I was using the
> > > Assabet+Neponset as my firewall, and it was having to use the IDE
> > > ide-cs driver rather than the pata pcmcia driver.
> > > 
> > > I don't recall whether the pata pcmcia driver was a problem or not,
> > > as the PCMCIA interface can't cope with _any_ 32-bit accesses.  I
> > > think PATA tries to use the "highest" possible access size by
> > > default...
> > 
> > It doesn't actually - it defaults to 16-bits for PIO data access and
> > you must explicitly enable 32-bits using ATA_PFLAG_PIO32 port flag
> > (pata_pcmcia doesn't set it so it should be okay).  Also taskfile
> > registers are accessed using 8-bits access by default transport
> > functions (which are used by pata_pcmcia).
> 
> Please also note that:
> 
> - assebet_defconfig currently doesn't even enable ide-cs
>   (CONFIG_BLK_DEV_IDECS) in the mainline kernel
> 
> - neponset_defconfig doesn't even enable IDE (CONFIG_IDE)
>   in the mainline kernel
> 
> so there is no risk of breaking anything.. 

I noticed this older pull request in my todo folder, my interpretation
is that the concern was resolved and we simply missed it.

I've pulled it into next/defconfig for v4.10 now, with the above
in the merge commit text for reference.

Thanks,

Arnd


Re: [PATCH v2] staging: lustre: osc: Performance tune for LRU

2016-11-25 Thread Dan Carpenter
On Wed, Nov 23, 2016 at 06:01:45PM -0500, James Simmons wrote:
> From: Jinshan Xiong 
> 
> Early launch page LRU work in osc_io_rw_iter_init();
> Change the page LRU shrinking policy by OSC attributes;
> Delete the contented lock osc_object::oo_seatbelt
> 

The cli_name() stuff should be in a separate patch.  It's hard to review
this stuff (or possibly I just found the first deal breaker and gave
up).

Anyway, please review this again and break it up into separate patches.

regards,
dan carpenter

PS:  I glanced at the patch again and the very first line is changing
"lru" to "LRU" in a comment.  We'll get a million performance increases
from that I bet.  Don't mix random white space changes into it...




Re: [PATCH] ARM: pxa: ezx: fix a910 camera data

2016-11-25 Thread Arnd Bergmann
On Friday, November 25, 2016 8:48:53 PM CET Robert Jarzmik wrote:
> Arnd Bergmann  writes:
> 
> > The camera_supply_dummy_device definition is shared between a780 and a910,
> > but only provided when the first is enabled and fails to build for a
> > configuration with only a910:
> >
> > arch/arm/mach-pxa/ezx.c:1097:3: error: 'camera_supply_dummy_device' 
> > undeclared here (not in a function)
> >
> > This moves the definition into its own section.
> >
> > Fixes: 6c1b417adc8f ("ARM: pxa: ezx: use the new pxa_camera platform_data")
> > Signed-off-by: Arnd Bergmann 
> 
> Ah yes, I'll queue that up in pxa/fixes.
> 
> This also means that you have a test robot which beats my Jenkins, as mine
> didn't complain. Do you have a specific defconfig or is it a randconfig which
> reveals that ?
> 

It showed up in randconfig builds, two out of several hundred.
I'm not surprised that nobody else caught it.

Arnd



Re: [PATCH v3 2/4] Documentation/atomic_ops.txt: convert to ReST markup

2016-11-25 Thread Peter Zijlstra
On Fri, Nov 25, 2016 at 03:59:45PM +0100, Silvio Fricke wrote:
> ... and move to core-api folder.
> 
> Signed-off-by: Silvio Fricke 
> ---
>  Documentation/atomic_ops.txt => Documentation/core-api/atomic_ops.rst | 777 
> +---
>  Documentation/core-api/index.rst  |   1 
> +-
>  Documentation/process/volatile-considered-harmful.rst |   3 
> +-
>  3 files changed, 404 insertions(+), 377 deletions(-)

Not a fan of this. The atomic_ops.txt file needs a lot of love, and I
wouldn't want to edit a .rst file.

Then again, I probably won't actually get around to fixing this document
any time soon either.

But if and when I would get around to it, I'll have to change it back to
a regular .txt file.


Cash Grant

2016-11-25 Thread Mrs Julie Leach
You are a recipient to Mrs Julie Leach Donation of $3 million USD. Contact 
(julieleac...@gmail.com ) for claims.


[PATCH] uio: pruss: add clk_disable()

2016-11-25 Thread Alexey Khoroshilov
pruss_probe() enables gdev->pruss_clk, but there is no clk_disable()
in the driver.

The patch adds clk_disable() to pruss_cleanup() and error handling for
clk_enable().

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Alexey Khoroshilov 
---
 drivers/uio/uio_pruss.c | 10 +-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/uio/uio_pruss.c b/drivers/uio/uio_pruss.c
index ca9e2fafb0b6..31d5b1d3b5af 100644
--- a/drivers/uio/uio_pruss.c
+++ b/drivers/uio/uio_pruss.c
@@ -111,6 +111,7 @@ static void pruss_cleanup(struct device *dev, struct 
uio_pruss_dev *gdev)
  gdev->sram_vaddr,
  sram_pool_sz);
kfree(gdev->info);
+   clk_disable(gdev->pruss_clk);
clk_put(gdev->pruss_clk);
kfree(gdev);
 }
@@ -143,7 +144,14 @@ static int pruss_probe(struct platform_device *pdev)
kfree(gdev);
return ret;
} else {
-   clk_enable(gdev->pruss_clk);
+   ret = clk_enable(gdev->pruss_clk);
+   if (ret) {
+   dev_err(dev, "Failed to enable clock\n");
+   clk_put(gdev->pruss_clk);
+   kfree(gdev->info);
+   kfree(gdev);
+   return ret;
+   }
}
 
regs_prussio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-- 
2.7.4



Re: mm: BUG in pgtable_pmd_page_dtor

2016-11-25 Thread Andrey Ryabinin


On 11/25/2016 05:08 PM, Vlastimil Babka wrote:
> On 11/25/2016 02:07 PM, Kirill A. Shutemov wrote:
>>> --- a/mm/debug.c
>>> +++ b/mm/debug.c
>>> @@ -59,6 +59,10 @@ void __dump_page(struct page *page, const char *reason)
>>>  
>>> pr_emerg("flags: %#lx(%pGp)\n", page->flags, &page->flags);
>>>  
>>> +   print_hex_dump(KERN_ALERT, "raw: ", DUMP_PREFIX_NONE,
>>> +   32, (sizeof(unsigned long) == 8) ? 8 : 4,
>>
>> That's a very fancy way to write sizeof(unsigned long) ;)
>  
> Ah, damnit, thanks.
> 
> 8<
> From 08d2ee803567c13e3de7ce7e19338fe5286cc6b8 Mon Sep 17 00:00:00 2001
> From: Vlastimil Babka 
> Date: Fri, 25 Nov 2016 09:08:05 +0100
> Subject: [PATCH v3] mm, debug: print raw struct page data in __dump_page()
> 
> The __dump_page() function is used when a page metadata inconsistency is
> detected, either by standard runtime checks, or extra checks in 
> CONFIG_DEBUG_VM
> builds. It prints some of the relevant metadata, but not the whole struct 
> page,
> which is based on unions and interpretation is dependent on the context.
> 
> This means that sometimes e.g. a VM_BUG_ON_PAGE() checks certain field, which
> is however not printed by __dump_page() and the resulting bug report may then
> lack clues that could help in determining the root cause. This patch solves
> the problem by simply printing the whole struct page word by word, so no part
> is missing, but the interpretation of the data is left to developers. This is
> similar to e.g. x86_64 raw stack dumps.
> 
> Example output:
> 
>  page:ea0475c0 count:1 mapcount:0 mapping:  (null) index:0x0
>  flags: 0x1000400(reserved)
>  raw: 01000400   0001
>  raw: ea0475e0 ea0475e0  
>  page dumped because: VM_BUG_ON_PAGE(1)
> 
> [aryabi...@virtuozzo.com: suggested print_hex_dump()]
> Signed-off-by: Vlastimil Babka 

Acked-by: Andrey Ryabinin 


Re: [PATCH] orangefs: Axe some dead code

2016-11-25 Thread Dan Carpenter
On Thu, Nov 24, 2016 at 07:31:11AM -0500, Mike Marshall wrote:
> This seems like a good and proper patch to me, and simple too.
> But like all changes, it needs tested. While I was testing it, I
> discovered a regression in the associated userspace code. I
> "bisected" (we use SVN for the userspace part of Orangefs)
> down to the commit that caused the regression, and some
> of the userspace folks are going to fix it.
> 
> I don't think I should ask Linus to pull this patch until I can
> test it. Since we're about to go into rc7, it might not go in
> until the next go around...
> 

Linus is on 4.9-rc7 so you're saying this would probably go into 4.11?

regards,
dan carpenter



Re: RFC: documentation of the autogroup feature [v2]

2016-11-25 Thread Peter Zijlstra
On Fri, Nov 25, 2016 at 09:54:05PM +0100, Michael Kerrisk (man-pages) wrote:
> So, part of what I was struggling with was what you meant by cfs-cgroup.
> Do you mean the CFS bandwidth control features added in Linux 3.2?

Nope, /me digs around for a bit... around here I suppose:

 68318b8e0b61 ("Hook up group scheduler with control groups")

68318b8e0b61 v2.6.24-rc1~151

But I really have no idea what that looked like.

In any case, for the case of autogroup, the behaviour has always been,
autogroups came quite late.


[GIT PULL] ACPI fixes for v4.9-rc7

2016-11-25 Thread Rafael J. Wysocki
Hi Linus,

Please pull from the tag

 git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git \
 acpi-4.9-rc7

with top-most commit 7e5c07af8693e72b23aefb70da88b31b30c35b22

 Merge branches 'acpi-sleep-fixes' and 'acpi-wdat-fixes'

on top of commit 9c763584b7c8911106bb77af7e648bef09af9d80

 Linux 4.9-rc6

to receive ACPI fixes for v4.9-rc7.

One of them reverts a recent ACPI commit that attempted to improve
reboot/power-off on some systems, but introduced problems elsewhere,
and the other one fixes kernel builds with the new WDAT watchdog
driver enabled in some configurations.

Specifics:

 - Revert the recent commit that caused the ACPI _PTS method to
   be executed in the power-off/reboot code path (as per the
   specification) in an attempt to improve things on some systems
   (apparently expecting _PTS to be executed in that code path),
   but broke power-off/reboot on at least one other machine (Rafael
   Wysocki).

 - Fix kernel builds with the new WDAT watchdog driver enabled in
   some configurations by explicitly selecting WATCHDOG_CORE when
   enabling the WDAT watchdog driver (Mika Westerberg).

Thanks!


---

Mika Westerberg (1):
  watchdog: wdat_wdt: Select WATCHDOG_CORE

Rafael J. Wysocki (1):
  Revert "ACPI: Execute _PTS before system reboot"

---

 drivers/acpi/sleep.c | 29 ++---
 drivers/watchdog/Kconfig |  1 +
 2 files changed, 7 insertions(+), 23 deletions(-)


Re: [PATCH 3/6] dax: add tracepoint infrastructure, PMD tracing

2016-11-25 Thread Theodore Ts'o
On Fri, Nov 25, 2016 at 11:51:26AM -0800, Linus Torvalds wrote:
> We do have filesystem code that is just disgusting. As an example:
> fs/afs/ tends to have these crazy "_enter()/_exit()" macros in every
> single function. If you want that, use the function tracer. That seems
> to be just debugging code that has been left around for others to
> stumble over. I do *not* believe that we should encourage that kind of
> "machine gun spray" use of tracepoints.

There is a reason why people want to be able to do that, and that's
because kprobes doesn't give you access to the arguments and return
codes to the functions.  Maybe there could be a way to do this more
easily using DWARF information and EBPF magic, perhaps?  It won't help
for inlined functions, of course, but most of the functions where
people want to do this aren't generally functions which are going to
be inlined, but rather things like write_begin, writepages, which are
called via a struct ops table and so will never be inlined to begin
with.

And it *is* handy to be able to do this when you don't know ahead of
time that you might need to debug a production system that is
malfunctioning for some reason.  This is the "S" in RAS (Reliability,
Availability, Serviceability).  This is why it's nice if there were a
way to be clear that it is intended for debugging purposes only ---
and maybe kprobes with EBPF and DWARF would be the answer.

After all, we need *some* way of saying this can never be considered
stable --- what would we do if some userspace program like powertop
started depending on a function name via ktrace and that function
disappeared --- would the userspace application really be intended to
demand that we revert the recatoring, because eliminating a function
name that they were depending on via ktrace point broke them?

- Ted


[PATCH] perf kmem stat: Track memory freed

2016-11-25 Thread David Ahern
Track freed memory as well as allocations and show the net in the
summary.

Signed-off-by: David Ahern 
---
 tools/perf/builtin-kmem.c | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index f184ecf9b0b3..cd662dd8eaf8 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -50,6 +50,7 @@ struct alloc_stat {
u64 ptr;
u64 bytes_req;
u64 bytes_alloc;
+   u64 last_alloc;
u32 hit;
u32 pingpong;
 
@@ -63,7 +64,7 @@ static struct rb_root root_alloc_sorted;
 static struct rb_root root_caller_stat;
 static struct rb_root root_caller_sorted;
 
-static unsigned long total_requested, total_allocated;
+static unsigned long total_requested, total_allocated, total_freed;
 static unsigned long nr_allocs, nr_cross_allocs;
 
 /* filters for controlling start and stop of time of analysis */
@@ -110,6 +111,8 @@ static int insert_alloc_stat(unsigned long call_site, 
unsigned long ptr,
}
data->call_site = call_site;
data->alloc_cpu = cpu;
+   data->last_alloc = bytes_alloc;
+
return 0;
 }
 
@@ -228,6 +231,8 @@ static int perf_evsel__process_free_event(struct perf_evsel 
*evsel,
if (!s_alloc)
return 0;
 
+   total_freed += s_alloc->last_alloc;
+
if ((short)sample->cpu != s_alloc->alloc_cpu) {
s_alloc->pingpong++;
 
@@ -1145,6 +1150,11 @@ static void print_slab_summary(void)
printf("\n\n");
printf("Total bytes requested: %'lu\n", total_requested);
printf("Total bytes allocated: %'lu\n", total_allocated);
+   printf("Total bytes freed: %'lu\n", total_freed);
+   if (total_allocated > total_freed) {
+   printf("Net total bytes allocated: %'lu\n",
+   total_allocated - total_freed);
+   }
printf("Total bytes wasted on internal fragmentation: %'lu\n",
   total_allocated - total_requested);
printf("Internal fragmentation: %f%%\n",
-- 
2.7.4 (Apple Git-66)



Re: [PATCH 0/3] virtio/vringh: kill off ACCESS_ONCE()

2016-11-25 Thread Christian Borntraeger
On 11/25/2016 10:08 PM, Michael S. Tsirkin wrote:
> On Fri, Nov 25, 2016 at 05:49:45PM +0100, Christian Borntraeger wrote:
>> On 11/25/2016 05:17 PM, Peter Zijlstra wrote:
>>> On Fri, Nov 25, 2016 at 04:10:04PM +, Mark Rutland wrote:
 On Fri, Nov 25, 2016 at 04:21:39PM +0100, Dmitry Vyukov wrote:
>>>
> What are use cases for such primitive that won't be OK with "read once
> _and_ atomically"?

 I have none to hand.
>>>
>>> Whatever triggers the __builtin_memcpy() paths, and even the size==8
>>> paths on 32bit.
>>>
>>> You could put a WARN in there to easily find them.
>>
>> There were several cases that I found during writing the *ONCE stuff.
>> For example there are some 32bit ppc variants with 64bit PTEs. Some for
>> others (I think sparc). And the mm/ code is perfectly fine with these
>> PTE accesses being done NOT atomic.
> 
> In that case do we even need _ONCE at all?

Yes. For example look at gup_pmd_range. Here several checks are made on the pmd.
It is important the the check for pmd_none is made on the same value than
the check for pmd_trans_huge, but it is not important that the value is still up
to date. 
And there are really cases where we cannot read the  thing atomically, e.g. on 
m68k and sparc(32bit) pmd_t is defined as array of longs.

Another problem is that a compiler can implement the following code as 2 memory
reads (e.g. if you have compare instructions that work on memory) instead of a 
memory read and 2 compares

int check(unsigned long *value_p) {
unsigned long value = *value_p;
if (condition_a(value))
return 1;
if (condition_b(value))
return 2;
return 3;
}

With READ_ONCE you forbid that. In past times you would have used barrier() 
after 
the assignment to achieve the same goal.


> Are there assumptions these are two 32 bit reads?

It depends on the code. Some places (e.g. in gup) assumes that the access via
READ_ONCE is atomic (which it is for sane compilers as long as the pointer
is <= word size). In some others places just one bit is tested.
> 
> 
>>
>>>
>>> The advantage of introducing the SINGLE_{LOAD,STORE}() helpers is that
>>> they compiletime validate this the size is 'right' and can runtime check
>>> alignment constraints.
>>>
>>> IE, they are strictly stronger than {READ,WRITE}_ONCE().
>>>
> 



[PATCH 5/6] perf kmem: Add option to specify time window of interest

2016-11-25 Thread David Ahern
From: David Ahern 

Add option to allow user to control analysis window. e.g., collect data
for time window and analyze a segment of interest within that window.

Signed-off-by: David Ahern 
---
 tools/perf/Documentation/perf-kmem.txt |  7 +++
 tools/perf/builtin-kmem.c  | 24 
 2 files changed, 31 insertions(+)

diff --git a/tools/perf/Documentation/perf-kmem.txt 
b/tools/perf/Documentation/perf-kmem.txt
index ff0f433b3fce..479fc3261a50 100644
--- a/tools/perf/Documentation/perf-kmem.txt
+++ b/tools/perf/Documentation/perf-kmem.txt
@@ -61,6 +61,13 @@ OPTIONS
default, but this option shows live (currently allocated) pages
instead.  (This option works with --page option only)
 
+--time::
+   Only analyze samples within given time window: ,. Times
+   have the format seconds.microseconds. If start is not given (i.e., time
+   string is ',x.y') then analysis starts at the beginning of the file. If
+   stop time is not given (i.e, time string is 'x.y,') then analysis goes
+   to end of file.
+
 SEE ALSO
 
 linkperf:perf-record[1]
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index d426dcb18ce9..f184ecf9b0b3 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -11,6 +11,7 @@
 #include "util/session.h"
 #include "util/tool.h"
 #include "util/callchain.h"
+#include "util/time-utils.h"
 
 #include 
 #include "util/trace-event.h"
@@ -65,6 +66,10 @@ static struct rb_root root_caller_sorted;
 static unsigned long total_requested, total_allocated;
 static unsigned long nr_allocs, nr_cross_allocs;
 
+/* filters for controlling start and stop of time of analysis */
+static struct perf_time ptime;
+const char *time_str;
+
 static int insert_alloc_stat(unsigned long call_site, unsigned long ptr,
 int bytes_req, int bytes_alloc, int cpu)
 {
@@ -907,6 +912,15 @@ static int perf_evsel__process_page_free_event(struct 
perf_evsel *evsel,
return 0;
 }
 
+static bool perf_kmem__skip_sample(struct perf_sample *sample)
+{
+   /* skip sample based on time? */
+   if (perf_time__skip_sample(&ptime, sample->time))
+   return true;
+
+   return false;
+}
+
 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
  struct perf_sample *sample);
 
@@ -926,6 +940,9 @@ static int process_sample_event(struct perf_tool *tool 
__maybe_unused,
return -1;
}
 
+   if (perf_kmem__skip_sample(sample))
+   return 0;
+
dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), 
thread->tid);
 
if (evsel->handler != NULL) {
@@ -1884,6 +1901,8 @@ int cmd_kmem(int argc, const char **argv, const char 
*prefix __maybe_unused)
OPT_CALLBACK_NOOPT(0, "page", NULL, NULL, "Analyze page allocator",
   parse_page_opt),
OPT_BOOLEAN(0, "live", &live_page, "Show live page stat"),
+   OPT_STRING(0, "time", &time_str, "str",
+  "Time span of interest (start,stop)"),
OPT_END()
};
const char *const kmem_subcommands[] = { "record", "stat", NULL };
@@ -1944,6 +1963,11 @@ int cmd_kmem(int argc, const char **argv, const char 
*prefix __maybe_unused)
 
symbol__init(&session->header.env);
 
+   if (perf_time__parse_str(&ptime, time_str) != 0) {
+   pr_err("Invalid time string\n");
+   return -EINVAL;
+   }
+
if (!strcmp(argv[0], "stat")) {
setlocale(LC_ALL, "");
 
-- 
2.7.4 (Apple Git-66)



[PATCH 3/6] perf script: Add option to specify time window of interest

2016-11-25 Thread David Ahern
From: David Ahern 

Add option to allow user to control analysis window. e.g., collect data
for some amount of time and analyze a segment of interest within that
window.

Signed-off-by: David Ahern 
---
 tools/perf/Documentation/perf-script.txt |  7 +++
 tools/perf/builtin-script.c  | 15 ++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/tools/perf/Documentation/perf-script.txt 
b/tools/perf/Documentation/perf-script.txt
index 0f6ee09f7256..5dc5c6a09ac4 100644
--- a/tools/perf/Documentation/perf-script.txt
+++ b/tools/perf/Documentation/perf-script.txt
@@ -292,6 +292,13 @@ include::itrace.txt[]
 --force::
Don't do ownership validation.
 
+--time::
+   Only analyze samples within given time window: ,. Times
+   have the format seconds.microseconds. If start is not given (i.e., time
+   string is ',x.y') then analysis starts at the beginning of the file. If
+   stop time is not given (i.e, time string is 'x.y,') then analysis goes
+   to end of file.
+
 SEE ALSO
 
 linkperf:perf-record[1], linkperf:perf-script-perl[1],
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 066b4bf73780..52d6a020346a 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -22,6 +22,7 @@
 #include "util/thread_map.h"
 #include "util/stat.h"
 #include "util/thread-stack.h"
+#include "util/time-utils.h"
 #include 
 #include 
 #include 
@@ -833,6 +834,8 @@ struct perf_script {
struct cpu_map  *cpus;
struct thread_map   *threads;
int name_width;
+   const char  *time_str;
+   struct  perf_time ptime;
 };
 
 static int perf_evlist__max_name_len(struct perf_evlist *evlist)
@@ -1014,6 +1017,9 @@ static int process_sample_event(struct perf_tool *tool,
struct perf_script *scr = container_of(tool, struct perf_script, tool);
struct addr_location al;
 
+   if (perf_time__skip_sample(&scr->ptime, sample->time))
+   return 0;
+
if (debug_mode) {
if (sample->time < last_timestamp) {
pr_err("Samples misordered, previous: %" PRIu64
@@ -2186,7 +2192,8 @@ int cmd_script(int argc, const char **argv, const char 
*prefix __maybe_unused)
"Enable symbol demangling"),
OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
"Enable kernel symbol demangling"),
-
+   OPT_STRING(0, "time", &script.time_str, "str",
+  "Time span of interest (start,stop)"),
OPT_END()
};
const char * const script_subcommands[] = { "record", "report", NULL };
@@ -2465,6 +2472,12 @@ int cmd_script(int argc, const char **argv, const char 
*prefix __maybe_unused)
if (err < 0)
goto out_delete;
 
+   /* needs to be parsed after looking up reference time */
+   if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
+   pr_err("Invalid time string\n");
+   return -EINVAL;
+   }
+
err = __cmd_script(&script);
 
flush_scripting();
-- 
2.7.4 (Apple Git-66)



[PATCH 6/6] perf report: Add option to specify time window of interest

2016-11-25 Thread David Ahern
From: David Ahern 

Add option to allow user to control analysis window. e.g., collect data
for time window and analyze a segment of interest within that window.

Signed-off-by: David Ahern 
---
 tools/perf/Documentation/perf-report.txt |  7 +++
 tools/perf/builtin-report.c  | 14 +-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/tools/perf/Documentation/perf-report.txt 
b/tools/perf/Documentation/perf-report.txt
index 2d1746295abf..3a166ae4a4d3 100644
--- a/tools/perf/Documentation/perf-report.txt
+++ b/tools/perf/Documentation/perf-report.txt
@@ -382,6 +382,13 @@ OPTIONS
 --header-only::
Show only perf.data header (forces --stdio).
 
+--time::
+   Only analyze samples within given time window: ,. Times
+   have the format seconds.microseconds. If start is not given (i.e., time
+   string is ',x.y') then analysis starts at the beginning of the file. If
+   stop time is not given (i.e, time string is 'x.y,') then analysis goes
+   to end of file.
+
 --itrace::
Options for decoding instruction tracing data. The options are:
 
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 3dfbfffe2ecd..6565a263a275 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -36,7 +36,7 @@
 #include "util/hist.h"
 #include "util/data.h"
 #include "arch/common.h"
-
+#include "util/time-utils.h"
 #include "util/auxtrace.h"
 
 #include 
@@ -59,6 +59,8 @@ struct report {
const char  *pretty_printing_style;
const char  *cpu_list;
const char  *symbol_filter_str;
+   const char  *time_str;
+   struct  perf_time ptime;
float   min_percent;
u64 nr_entries;
u64 queue_size;
@@ -158,6 +160,9 @@ static int process_sample_event(struct perf_tool *tool,
};
int ret = 0;
 
+   if (perf_time__skip_sample(&rep->ptime, sample->time))
+   return 0;
+
if (machine__resolve(machine, &al, sample) < 0) {
pr_debug("problem processing %d event, skipping it.\n",
 event->header.type);
@@ -830,6 +835,8 @@ int cmd_report(int argc, const char **argv, const char 
*prefix __maybe_unused)
OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
 "'always' (default), 'never' or 'auto' only 
applicable to --stdio mode",
 stdio__config_color, "always"),
+   OPT_STRING(0, "time", &report.time_str, "str",
+  "Time span of interest (start,stop)"),
OPT_END()
};
struct perf_data_file file = {
@@ -1015,6 +1022,11 @@ int cmd_report(int argc, const char **argv, const char 
*prefix __maybe_unused)
if (symbol__init(&session->header.env) < 0)
goto error;
 
+   if (perf_time__parse_str(&report.ptime, report.time_str) != 0) {
+   pr_err("Invalid time string\n");
+   return -EINVAL;
+   }
+
sort__setup_elide(stdout);
 
ret = __cmd_report(&report);
-- 
2.7.4 (Apple Git-66)



[PATCH 4/6] perf sched timehist: Add option to specify time window of interest

2016-11-25 Thread David Ahern
From: David Ahern 

Add option to allow user to control analysis window. e.g., collect data
for time window and analyze a segment of interest within that window.

Signed-off-by: David Ahern 
---
 tools/perf/Documentation/perf-sched.txt |  8 ++
 tools/perf/builtin-sched.c  | 51 +
 2 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/tools/perf/Documentation/perf-sched.txt 
b/tools/perf/Documentation/perf-sched.txt
index 121c60da03e5..7775b1eb2bee 100644
--- a/tools/perf/Documentation/perf-sched.txt
+++ b/tools/perf/Documentation/perf-sched.txt
@@ -132,6 +132,14 @@ OPTIONS for 'perf sched timehist'
 --migrations::
Show migration events.
 
+--time::
+   Only analyze samples within given time window: ,. Times
+   have the format seconds.microseconds. If start is not given (i.e., time
+   string is ',x.y') then analysis starts at the beginning of the file. If
+   stop time is not given (i.e, time string is 'x.y,') then analysis goes
+   to end of file.
+
+
 SEE ALSO
 
 linkperf:perf-record[1]
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 4f9e7cba4ebf..1656f2c9f638 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -15,6 +15,7 @@
 #include "util/color.h"
 #include "util/stat.h"
 #include "util/callchain.h"
+#include "util/time-utils.h"
 
 #include 
 #include "util/trace-event.h"
@@ -205,6 +206,8 @@ struct perf_sched {
boolshow_wakeups;
boolshow_migrations;
u64 skipped_samples;
+   const char  *time_str;
+   struct perf_time ptime;
 };
 
 /* per thread run time data */
@@ -1837,13 +1840,14 @@ static void timehist_header(struct perf_sched *sched)
 static void timehist_print_sample(struct perf_sched *sched,
  struct perf_sample *sample,
  struct addr_location *al,
- struct thread *thread)
+ struct thread *thread,
+ u64 t)
 {
struct thread_runtime *tr = thread__priv(thread);
u32 max_cpus = sched->max_cpu + 1;
char tstr[64];
 
-   timestamp__scnprintf_usec(sample->time, tstr, sizeof(tstr));
+   timestamp__scnprintf_usec(t, tstr, sizeof(tstr));
printf("%15s [%04d] ", tstr, sample->cpu);
 
if (sched->show_cpu_visual) {
@@ -2194,7 +2198,8 @@ static int timehist_sched_wakeup_event(struct perf_tool 
*tool,
tr->ready_to_run = sample->time;
 
/* show wakeups if requested */
-   if (sched->show_wakeups)
+   if (sched->show_wakeups &&
+   !perf_time__skip_sample(&sched->ptime, sample->time))
timehist_print_wakeup_event(sched, sample, machine, thread);
 
return 0;
@@ -2288,10 +2293,11 @@ static int timehist_sched_change_event(struct perf_tool 
*tool,
   struct machine *machine)
 {
struct perf_sched *sched = container_of(tool, struct perf_sched, tool);
+   struct perf_time *ptime = &sched->ptime;
struct addr_location al;
struct thread *thread;
struct thread_runtime *tr = NULL;
-   u64 tprev;
+   u64 tprev, t = sample->time;
int rc = 0;
 
if (machine__resolve(machine, &al, sample) < 0) {
@@ -2318,9 +2324,35 @@ static int timehist_sched_change_event(struct perf_tool 
*tool,
 
tprev = perf_evsel__get_time(evsel, sample->cpu);
 
-   timehist_update_runtime_stats(tr, sample->time, tprev);
+   /*
+* If start time given:
+* - sample time is under window user cares about - skip sample
+* - tprev is under window user cares about  - reset to start of window
+*/
+   if (ptime->start && ptime->start > t)
+   goto out;
+
+   if (ptime->start > tprev)
+   tprev = ptime->start;
+
+   /*
+* If end time given:
+* - previous sched event is out of window - we are done
+* - sample time is beyond window user cares about - reset it
+*   to close out stats for time window interest
+*/
+   if (ptime->end) {
+   if (tprev > ptime->end)
+   goto out;
+
+   if (t > ptime->end)
+   t = ptime->end;
+   }
+
+   timehist_update_runtime_stats(tr, t, tprev);
+
if (!sched->summary_only)
-   timehist_print_sample(sched, sample, &al, thread);
+   timehist_print_sample(sched, sample, &al, thread, t);
 
 out:
if (tr) {
@@ -2583,6 +2615,11 @@ static int perf_sched__timehist(struct perf_sched *sched)
 
symbol__init(&session->header.env);
 
+   if (perf_time__parse_str(&sched->ptime, sched->time_str) != 0) {
+   pr_err("Invalid time string\n");
+   return -EINVAL;
+   }
+
if (timehist_check_attr(sched, evlis

Re: [GIT PULL] STi defconfig fix for v4.9-rcs

2016-11-25 Thread Arnd Bergmann
On Wednesday, November 23, 2016 9:59:14 AM CET Patrice Chotard wrote:
> STi defconfig fix:
> 
> Enable HVA (Hardware Video Accelerator) video encoder
> driver for STMicroelectronics SoC.

Defconfig changes like this don't seem particularly urgent. Unless
there is a good reason, I'd suggest putting this into v4.10 instead.

Arnd


[PATCH 2/6] perf tool: Move parse_nsec_time to time-utils.c

2016-11-25 Thread David Ahern
From: David Ahern 

Code move only; no functional change intended.

Signed-off-by: David Ahern 
---
 tools/perf/util/time-utils.c | 35 ++-
 tools/perf/util/time-utils.h |  2 ++
 tools/perf/util/util.c   | 33 -
 tools/perf/util/util.h   |  2 --
 4 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/tools/perf/util/time-utils.c b/tools/perf/util/time-utils.c
index e584aeae9834..0453a7beeef4 100644
--- a/tools/perf/util/time-utils.c
+++ b/tools/perf/util/time-utils.c
@@ -1,5 +1,6 @@
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -7,7 +8,39 @@
 #include "../perf.h"
 #include "debug.h"
 #include "time-utils.h"
-#include "util.h"
+
+int parse_nsec_time(const char *str, u64 *ptime)
+{
+   u64 time_sec, time_nsec;
+   char *end;
+
+   time_sec = strtoul(str, &end, 10);
+   if (*end != '.' && *end != '\0')
+   return -1;
+
+   if (*end == '.') {
+   int i;
+   char nsec_buf[10];
+
+   if (strlen(++end) > 9)
+   return -1;
+
+   strncpy(nsec_buf, end, 9);
+   nsec_buf[9] = '\0';
+
+   /* make it nsec precision */
+   for (i = strlen(nsec_buf); i < 9; i++)
+   nsec_buf[i] = '0';
+
+   time_nsec = strtoul(nsec_buf, &end, 10);
+   if (*end != '\0')
+   return -1;
+   } else
+   time_nsec = 0;
+
+   *ptime = time_sec * NSEC_PER_SEC + time_nsec;
+   return 0;
+}
 
 static int parse_timestr_sec_nsec(struct perf_time *ptime,
  char *start_str, char *end_str)
diff --git a/tools/perf/util/time-utils.h b/tools/perf/util/time-utils.h
index 4368a481251d..d110d4d98854 100644
--- a/tools/perf/util/time-utils.h
+++ b/tools/perf/util/time-utils.h
@@ -5,6 +5,8 @@ struct perf_time {
u64 start, end;
 };
 
+int parse_nsec_time(const char *str, u64 *ptime);
+
 int perf_time__parse_str(struct perf_time *ptime, const char *ostr);
 
 bool perf_time__skip_sample(struct perf_time *ptime, u64 timestamp);
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 67ac765da27a..9ddd98827d12 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -400,39 +400,6 @@ void sighandler_dump_stack(int sig)
raise(sig);
 }
 
-int parse_nsec_time(const char *str, u64 *ptime)
-{
-   u64 time_sec, time_nsec;
-   char *end;
-
-   time_sec = strtoul(str, &end, 10);
-   if (*end != '.' && *end != '\0')
-   return -1;
-
-   if (*end == '.') {
-   int i;
-   char nsec_buf[10];
-
-   if (strlen(++end) > 9)
-   return -1;
-
-   strncpy(nsec_buf, end, 9);
-   nsec_buf[9] = '\0';
-
-   /* make it nsec precision */
-   for (i = strlen(nsec_buf); i < 9; i++)
-   nsec_buf[i] = '0';
-
-   time_nsec = strtoul(nsec_buf, &end, 10);
-   if (*end != '\0')
-   return -1;
-   } else
-   time_nsec = 0;
-
-   *ptime = time_sec * NSEC_PER_SEC + time_nsec;
-   return 0;
-}
-
 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
 {
u64  sec = timestamp / NSEC_PER_SEC;
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 79662d67891e..1d639e38aa82 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -179,8 +179,6 @@ static inline void *zalloc(size_t size)
 #undef tolower
 #undef toupper
 
-int parse_nsec_time(const char *str, u64 *ptime);
-
 extern unsigned char sane_ctype[256];
 #define GIT_SPACE  0x01
 #define GIT_DIGIT  0x02
-- 
2.7.4 (Apple Git-66)



[PATCH 1/6] perf tool: Add time-based utility functions

2016-11-25 Thread David Ahern
From: David Ahern 

Add function to parse a user time string of the form ,
where start and stop are time in sec.nsec format. Both start and stop
times are optional.

Add function to determine if a sample time is within a given time
time window of interest.

Signed-off-by: David Ahern 
---
 tools/perf/util/Build|  1 +
 tools/perf/util/time-utils.c | 85 
 tools/perf/util/time-utils.h | 12 +++
 3 files changed, 98 insertions(+)
 create mode 100644 tools/perf/util/time-utils.c
 create mode 100644 tools/perf/util/time-utils.h

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 1dc67efad634..78f139978e7a 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -87,6 +87,7 @@ libperf-y += help-unknown-cmd.o
 libperf-y += mem-events.o
 libperf-y += vsprintf.o
 libperf-y += drv_configs.o
+libperf-y += time-utils.o
 
 libperf-$(CONFIG_LIBBPF) += bpf-loader.o
 libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o
diff --git a/tools/perf/util/time-utils.c b/tools/perf/util/time-utils.c
new file mode 100644
index ..e584aeae9834
--- /dev/null
+++ b/tools/perf/util/time-utils.c
@@ -0,0 +1,85 @@
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "../perf.h"
+#include "debug.h"
+#include "time-utils.h"
+#include "util.h"
+
+static int parse_timestr_sec_nsec(struct perf_time *ptime,
+ char *start_str, char *end_str)
+{
+   if (start_str && (*start_str != '\0') &&
+   (parse_nsec_time(start_str, &ptime->start) != 0)) {
+   return -1;
+   }
+
+   if (end_str && (*end_str != '\0') &&
+   (parse_nsec_time(end_str, &ptime->end) != 0)) {
+   return -1;
+   }
+
+   return 0;
+}
+
+int perf_time__parse_str(struct perf_time *ptime, const char *ostr)
+{
+   char *start_str, *end_str;
+   char *d, *str;
+   int rc = 0;
+
+   if (ostr == NULL || *ostr == '\0')
+   return 0;
+
+   /* copy original string because we need to modify it */
+   str = strdup(ostr);
+   if (str == NULL)
+   return -ENOMEM;
+
+   ptime->start = 0;
+   ptime->end = 0;
+
+   /* str has the format: ,
+* variations: ,
+* ,
+* ,
+*/
+   start_str = str;
+   d = strchr(start_str, ',');
+   if (d) {
+   *d = '\0';
+   ++d;
+   }
+   end_str = d;
+
+   rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
+
+   free(str);
+
+   /* make sure end time is after start time if it was given */
+   if (rc == 0 && ptime->end && ptime->end < ptime->start)
+   return -EINVAL;
+
+   pr_debug("start time %" PRIu64 ", ", ptime->start);
+   pr_debug("end time %" PRIu64 "\n", ptime->end);
+
+   return rc;
+}
+
+bool perf_time__skip_sample(struct perf_time *ptime, u64 timestamp)
+{
+   /* if time is not set don't drop sample */
+   if (timestamp == 0)
+   return false;
+
+   /* otherwise compare sample time to time window */
+   if ((ptime->start && timestamp < ptime->start) ||
+   (ptime->end && timestamp > ptime->end)) {
+   return true;
+   }
+
+   return false;
+}
diff --git a/tools/perf/util/time-utils.h b/tools/perf/util/time-utils.h
new file mode 100644
index ..4368a481251d
--- /dev/null
+++ b/tools/perf/util/time-utils.h
@@ -0,0 +1,12 @@
+#ifndef _TIME_UTILS_H_
+#define _TIME_UTILS_H_
+
+struct perf_time {
+   u64 start, end;
+};
+
+int perf_time__parse_str(struct perf_time *ptime, const char *ostr);
+
+bool perf_time__skip_sample(struct perf_time *ptime, u64 timestamp);
+
+#endif
-- 
2.7.4 (Apple Git-66)



[PATCH 0/6] perf: Add option to specify time window of interest

2016-11-25 Thread David Ahern
From: David Ahern 

This series allows users to collect data and analyze a time window of
interest within the file.

David Ahern (6):
  perf tool: Add time-based utility functions
  perf tool: Move parse_nsec_time to time-utils.c
  perf script: Add option to specify time window of interest
  perf sched timehist: Add option to specify time window of interest
  perf kmem: Add option to specify time window of interest
  perf report: Add option to specify time window of interest

 tools/perf/Documentation/perf-kmem.txt   |   7 ++
 tools/perf/Documentation/perf-report.txt |   7 ++
 tools/perf/Documentation/perf-sched.txt  |   8 +++
 tools/perf/Documentation/perf-script.txt |   7 ++
 tools/perf/builtin-kmem.c|  24 +++
 tools/perf/builtin-report.c  |  14 +++-
 tools/perf/builtin-sched.c   |  51 +++--
 tools/perf/builtin-script.c  |  15 +++-
 tools/perf/util/Build|   1 +
 tools/perf/util/time-utils.c | 118 +++
 tools/perf/util/time-utils.h |  14 
 tools/perf/util/util.c   |  33 -
 tools/perf/util/util.h   |   2 -
 13 files changed, 258 insertions(+), 43 deletions(-)
 create mode 100644 tools/perf/util/time-utils.c
 create mode 100644 tools/perf/util/time-utils.h

-- 
2.7.4 (Apple Git-66)



Re: [GIT PULL] STi DT fix for v4.9-rcs round 2

2016-11-25 Thread Arnd Bergmann
On Wednesday, November 23, 2016 9:59:16 AM CET Patrice Chotard wrote:
> STi DT fix:
> 
> The I2C nodes are missing #address-cells and #size-cells.
> This is causing warning at device tree compilation when
> some I2C device sub-nodes are defined.
> 
> 

Pulled into fixes branch, thanks!

Arnd



Re: [PATCH] ARM: pxa: ezx: fix a910 camera data

2016-11-25 Thread Harald Welte
Hi Stefan,

On Thu, Nov 24, 2016 at 11:10:09PM +0100, Stefan Schmidt wrote:
> Daniel, Harald, if one of you is still interested in these and what to
> pick up the work again, please speak up now. :)

I have no interest, motivtaion nor time to still work on ezx support.  I
guess the number of people running a recent kernel on more than 10 year
old 2G-only phones is pretty low these days.

If anyone has a serious interest in taking this over, I think I still
have some A780 and A1200 units that I am happy to provide.

Regards,
Harald
-- 
- Harald Weltehttp://laforge.gnumonks.org/

"Privacy in residential applications is a desirable marketing option."
  (ETSI EN 300 175-7 Ch. A6)


Re: [PATCH] cxgb4: fix memory leak on txq_info

2016-11-25 Thread Colin Ian King
On 25/11/16 21:10, David Miller wrote:
> From: Colin King 
> Date: Wed, 23 Nov 2016 11:02:44 +
> 
>> From: Colin Ian King 
>>
>> Currently if txq_info->uldtxq cannot be allocated then
>> txq_info->txq is being kfree'd (which is redundant because it
>> is NULL) instead of txq_info. Fix this by instead kfree'ing
>> txq_info.
>>
>> Signed-off-by: Colin Ian King 
> 
> Applied, but Colin you _really_ need to start properly marking your
> networking patch submissions by indicating in the subject which
> tree your change is for.  In this case I figured out it was
> net-next, but you must say this explicitly in the Subject line
> via "Subject: [PATCH net-next] ..."
> 
> Thanks.
> 
Understood, will do next time, apologies for that.

Colin


Re: [PATCH] xen-scsifront: Add a missing call to kfree

2016-11-25 Thread Dan Carpenter
On Mon, Nov 21, 2016 at 07:01:36AM +0100, Juergen Gross wrote:
> On 19/11/16 19:22, Quentin Lambert wrote:
> > Most error branches following the call to kmalloc contain
> > a call to kfree. This patch add these calls where they are
> > missing.
> > 
> > This issue was found with Hector.
> > 
> > Signed-off-by: Quentin Lambert 
> 
> Nice catch. I think this will need some more work, I'll do a
> follow-on patch.

Yeah.  It's weird how we free it on the success path and all the failure
paths except one.  But it looks so deliberate.  What's going on with
that?

Could you send your follow on patch as a reply to the thread?

regards,
dan carpenter



[POC/RFC PATCH] overlayfs: constant inode numbers

2016-11-25 Thread Miklos Szeredi
Here's a really preliminary patch to allow inode numbers to be constant across
copy ups and be consistent between st_ino and d_ino.

It only works if underlying lower and upper dirs are all on the same filesystem
(so there's only a single inode namespace to deal with).

Performance of readdir is probably dismal, definitely needs improving.

Readdir of overlay root doesn't get ino of "." right.  This probably needs
special casing.

And it doesn't yet deal with the hard link copy-up issue, which is a somewhat
separate problem.

Thanks,
Miklos

---
 fs/overlayfs/copy_up.c   |   16 +++--
 fs/overlayfs/dir.c   |9 +
 fs/overlayfs/inode.c |   45 +--
 fs/overlayfs/namei.c |   17 +-
 fs/overlayfs/overlayfs.h |9 -
 fs/overlayfs/ovl_entry.h |1 
 fs/overlayfs/readdir.c   |   78 +--
 fs/overlayfs/util.c  |   15 +
 8 files changed, 160 insertions(+), 30 deletions(-)

--- a/fs/overlayfs/copy_up.c
+++ b/fs/overlayfs/copy_up.c
@@ -177,6 +177,7 @@ int ovl_set_attr(struct dentry *upperden
 {
int err = 0;
 
+   inode_lock(upperdentry->d_inode);
if (!S_ISLNK(stat->mode)) {
struct iattr attr = {
.ia_valid = ATTR_MODE,
@@ -194,6 +195,15 @@ int ovl_set_attr(struct dentry *upperden
}
if (!err)
ovl_set_timestamps(upperdentry, stat);
+   inode_unlock(upperdentry->d_inode);
+
+   if (!err) {
+   char buf[17];
+
+   snprintf(buf, sizeof(buf), "%llx", stat->ino);
+   err = ovl_do_setxattr(upperdentry, OVL_XATTR_INO,
+ buf, strlen(buf), 0);
+   }
 
return err;
 }
@@ -258,12 +268,12 @@ static int ovl_copy_up_locked(struct den
if (err)
goto out_cleanup;
 
-   inode_lock(newdentry->d_inode);
err = ovl_set_attr(newdentry, stat);
-   inode_unlock(newdentry->d_inode);
if (err)
goto out_cleanup;
 
+   ovl_dentry_set_ino(dentry, stat->ino);
+
err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
if (err)
goto out_cleanup;
@@ -379,7 +389,7 @@ int ovl_copy_up(struct dentry *dentry)
}
 
ovl_path_lower(next, &lowerpath);
-   err = vfs_getattr(&lowerpath, &stat);
+   err = ovl_getattr_int(next, &lowerpath, &stat);
if (!err)
err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
 
--- a/fs/overlayfs/overlayfs.h
+++ b/fs/overlayfs/overlayfs.h
@@ -20,6 +20,7 @@ enum ovl_path_type {
 #define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."
 #define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX "opaque"
 #define OVL_XATTR_REDIRECT OVL_XATTR_PREFIX "redirect"
+#define OVL_XATTR_INO OVL_XATTR_PREFIX "ino"
 
 #define OVL_ISUPPER_MASK 1UL
 
@@ -161,6 +162,8 @@ bool ovl_redirect_dir(struct super_block
 void ovl_clear_redirect_dir(struct super_block *sb);
 const char *ovl_dentry_get_redirect(struct dentry *dentry);
 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
+u64 ovl_dentry_get_ino(struct dentry *dentry);
+void ovl_dentry_set_ino(struct dentry *dentry, u64 ino);
 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
 void ovl_inode_init(struct inode *inode, struct inode *realinode,
bool is_upper);
@@ -171,9 +174,10 @@ bool ovl_is_whiteout(struct dentry *dent
 struct file *ovl_path_open(struct path *path, int flags);
 
 /* namei.c */
-int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
+int ovl_path_next(int idx, struct dentry *dentry, struct path *path, int 
*idxp);
 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, unsigned 
int flags);
 bool ovl_lower_positive(struct dentry *dentry);
+struct dentry *ovl_dentry_at_idx(struct dentry *dentry, int idx);
 
 /* readdir.c */
 extern const struct file_operations ovl_dir_operations;
@@ -196,6 +200,9 @@ struct posix_acl *ovl_get_acl(struct ino
 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags);
 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags);
 bool ovl_is_private_xattr(const char *name);
+int ovl_getattr_int(struct dentry *dentry, struct path *realpath,
+   struct kstat *stat);
+void ovl_get_ino(struct dentry *realdentry, u64 *ino);
 
 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode);
--- a/fs/overlayfs/inode.c
+++ b/fs/overlayfs/inode.c
@@ -15,6 +15,7 @@
 #include 
 #include 
 #include 
+#include 
 #include "overlayfs.h"
 
 static int ovl_copy_up_truncate(struct dentry *dentry)
@@ -33,7 +34,7 @@ static int ovl_copy_up_truncate(struct d
ovl_path_lower(dentry, &lowerpath);
 
old_cred = ovl_override_creds(dentry->d_sb);
-   err = vfs_geta

Re: [PATCH] drivers: net: davinci_mdio: use builtin_platform_driver

2016-11-25 Thread David Miller
From: Geliang Tang 
Date: Wed, 23 Nov 2016 22:45:43 +0800

> @@ -536,11 +536,7 @@ static struct platform_driver davinci_mdio_driver = {
>   .remove = davinci_mdio_remove,
>  };
>  
> -static int __init davinci_mdio_init(void)
> -{
> - return platform_driver_register(&davinci_mdio_driver);
> -}
> -device_initcall(davinci_mdio_init);
> +builtin_platform_driver(davinci_mdio_driver);
>  

As noted by others this is not a correct transformation, the existing
code works properly when modular.  But it will not with this change.

device_initcall() is rerouted to module_init() inside of a module
build, whereas the thing builtin_platform_driver() expands to does
not.


[mm] 68ab21008a: BUG:unable_to_handle_kernel

2016-11-25 Thread kernel test robot
FYI, we noticed the following commit:

commit 68ab21008a656abeb1fe2c7117a67eeab4d68ded ("mm: ovl: copy-up on 
MAP_SHARED")
url: 
https://github.com/0day-ci/linux/commits/Miklos-Szeredi/overlayfs-fix-ro-rw-fd-data-inconsistecies/20161124-233654
base: https://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git 
overlayfs-next

in testcase: trinity
with following parameters:

runtime: 300s

test-description: Trinity is a linux system call fuzz tester.
test-url: http://codemonkey.org.uk/projects/trinity/


on test machine: qemu-system-x86_64 -enable-kvm -cpu IvyBridge -m 360M

caused below changes:


+--+++
|  | b45dbaab96 | 68ab21008a |
+--+++
| boot_successes   | 4  | 0  |
| boot_failures| 0  | 5  |
| BUG:unable_to_handle_kernel  | 0  | 5  |
| Oops | 0  | 5  |
| RIP:vm_mmap_pgoff| 0  | 5  |
| calltrace:SyS_mmap_pgoff | 0  | 5  |
| Kernel_panic-not_syncing:Fatal_exception | 0  | 5  |
+--+++



[5.829350] 
job=/lkp/scheduled/vm-ivb41-yocto-ia32-22/trinity-300s-yocto-tiny-i386-2016-04-22.cgz-68ab21008a656abeb1fe2c7117a67eeab4d68ded-20161126-104135-1r99nk0-0.yaml
[5.829350] run-job 
/lkp/scheduled/vm-ivb41-yocto-ia32-22/trinity-300s-yocto-tiny-i386-2016-04-22.cgz-68ab21008a656abeb1fe2c7117a67eeab4d68ded-20161126-104135-1r99nk0-0.yaml
[5.829350] /bin/busybox wget -q 
http://inn:80/~lkp/cgi-bin/lkp-jobfile-append-var?job_file=/lkp/scheduled/vm-ivb41-yocto-ia32-22/trinity-300s-yocto-tiny-i386-2016-04-22.cgz-68ab21008a656abeb1fe2c7117a67eeab4d68ded-20161126-104135-1r99nk0-0.yaml&job_state=running
 -O /dev/null
[   16.146778] BUG: unable to handle kernel NULL pointer dereference at 
0018
[   16.150092] IP: [] vm_mmap_pgoff+0x63/0xe7
[   16.152858] PGD 71bf067 
[   16.153453] PUD 688b067 
PMD 0 
[   16.154890] 
[   16.156053] Oops:  [#1] SMP
[   16.157251] Modules linked in:
[   16.158444] CPU: 0 PID: 1483 Comm: trinity Not tainted 
4.9.0-rc4-00032-g68ab210 #1
[   16.160691] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 
Debian-1.8.2-1 04/01/2014
[   16.162901] task: 880006cc1c00 task.stack: c927c000
[   16.164145] RIP: 0010:[]  [] 
vm_mmap_pgoff+0x63/0xe7
[   16.166525] RSP: :c927fe90  EFLAGS: 00010246
[   16.167620] RAX: 880006cc1c00 RBX:  RCX: 0001
[   16.169186] RDX: 1000 RSI:  RDI: 
[   16.170761] RBP: c927fed8 R08: 0021 R09: 
[   16.172283] R10:  R11:  R12: 0001
[   16.173878] R13: 1000 R14: 880006cc8800 R15: 
[   16.175332] FS:  () GS:88001600(0063) 
knlGS:09af2840
[   16.177455] CS:  0010 DS: 002b ES: 002b CR0: 80050033
[   16.178827] CR2: 0018 CR3: 071b4000 CR4: 001406f0
[   16.180266] Stack:
[   16.181094]   1000  
880004b6ff00
[   16.183365]  1000 0021 1000 

[   16.185548]   c927ff30 812036f8 
00060027
[   16.187824] Call Trace:
[   16.188656]  [] SyS_mmap_pgoff+0x184/0x1a9
[   16.189918]  [] do_int80_syscall_32+0x64/0xbf
[   16.191138]  [] entry_INT80_compat+0x38/0x50
[   16.192397] Code: 03 00 00 75 21 49 83 c6 68 4c 89 45 b8 49 c7 c5 fc ff ff 
ff 4c 89 f7 e8 c0 4c 8f 00 85 c0 4c 8b 45 b8 75 79 eb 37 f6 c1 02 75 da <48> 8b 
47 18 8b 10 0f ba e2 1a 73 19 48 8b 48 60 4c 89 45 b8 ba 
[   16.201562] RIP  [] vm_mmap_pgoff+0x63/0xe7
[   16.203254]  RSP 
[   16.204501] CR2: 0018
[   16.206063] ---[ end trace 910d3120db07d449 ]---
[   16.226016] Kernel panic - not syncing: Fatal exception
[   16.227115] Kernel Offset: disabled

Elapsed time: 20


To reproduce:

git clone 
git://git.kernel.org/pub/scm/linux/kernel/git/wfg/lkp-tests.git
cd lkp-tests
bin/lkp qemu -k  job-script  # job-script is attached in this 
email



Thanks,
Kernel Test Robot
#
# Automatically generated file; DO NOT EDIT.
# Linux/x86_64 4.9.0-rc4 Kernel Configuration
#
CONFIG_64BIT=y
CONFIG_X86_64=y
CONFIG_X86=y
CONFIG_INSTRUCTION_DECODER=y
CONFIG_OUTPUT_FORMAT="elf64-x86-64"
CONFIG_ARCH_DEFCONFIG="arch/x86/configs/x86_64_defconfig"
CONFIG_LOCKDEP_SUPPORT=y
CONFIG_STACKTRACE_SUPPORT=y
CONFIG_MMU=y
CONFIG_ARCH_MMAP_RND_BITS_MIN=28
CONFIG_ARCH_MMAP_RND_BITS_MAX=32
CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MIN=8
CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX=16
CONFIG_NEED_DMA_MAP_STATE

  1   2   3   4   5   6   7   8   9   >