[tip:perf/core] perf kcore_copy: Amend the offset of sections that remap kernel text

2018-05-23 Thread tip-bot for Adrian Hunter
Commit-ID:  22916fdb9c50e8fb303bdcedca88fd8798a85844
Gitweb: https://git.kernel.org/tip/22916fdb9c50e8fb303bdcedca88fd8798a85844
Author: Adrian Hunter 
AuthorDate: Tue, 22 May 2018 13:54:45 +0300
Committer:  Arnaldo Carvalho de Melo 
CommitDate: Wed, 23 May 2018 10:26:44 -0300

perf kcore_copy: Amend the offset of sections that remap kernel text

x86 PTI entry trampolines all map to the same physical page. If that is
reflected in the program headers of /proc/kcore, then do the same for the
copy of kcore.

Signed-off-by: Adrian Hunter 
Cc: Alexander Shishkin 
Cc: Andi Kleen 
Cc: Andy Lutomirski 
Cc: Dave Hansen 
Cc: H. Peter Anvin 
Cc: Jiri Olsa 
Cc: Joerg Roedel 
Cc: Peter Zijlstra 
Cc: Thomas Gleixner 
Cc: x...@kernel.org
Link: 
http://lkml.kernel.org/r/1526986485-6562-18-git-send-email-adrian.hun...@intel.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/util/symbol-elf.c | 53 ++--
 1 file changed, 51 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 584966913aeb..29770ea61768 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1390,6 +1390,7 @@ struct phdr_data {
u64 addr;
u64 len;
struct list_head node;
+   struct phdr_data *remaps;
 };
 
 struct sym_data {
@@ -1597,16 +1598,62 @@ static int kcore_copy__read_maps(struct kcore_copy_info 
*kci, Elf *elf)
return 0;
 }
 
+static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
+{
+   struct phdr_data *p, *k = NULL;
+   u64 kend;
+
+   if (!kci->stext)
+   return;
+
+   /* Find phdr that corresponds to the kernel map (contains stext) */
+   kcore_copy__for_each_phdr(kci, p) {
+   u64 pend = p->addr + p->len - 1;
+
+   if (p->addr <= kci->stext && pend >= kci->stext) {
+   k = p;
+   break;
+   }
+   }
+
+   if (!k)
+   return;
+
+   kend = k->offset + k->len;
+
+   /* Find phdrs that remap the kernel */
+   kcore_copy__for_each_phdr(kci, p) {
+   u64 pend = p->offset + p->len;
+
+   if (p == k)
+   continue;
+
+   if (p->offset >= k->offset && pend <= kend)
+   p->remaps = k;
+   }
+}
+
 static void kcore_copy__layout(struct kcore_copy_info *kci)
 {
struct phdr_data *p;
off_t rel = 0;
 
+   kcore_copy__find_remaps(kci);
+
kcore_copy__for_each_phdr(kci, p) {
-   p->rel = rel;
-   rel += p->len;
+   if (!p->remaps) {
+   p->rel = rel;
+   rel += p->len;
+   }
kci->phnum += 1;
}
+
+   kcore_copy__for_each_phdr(kci, p) {
+   struct phdr_data *k = p->remaps;
+
+   if (k)
+   p->rel = p->offset - k->offset + k->rel;
+   }
 }
 
 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
@@ -1821,6 +1868,8 @@ int kcore_copy(const char *from_dir, const char *to_dir)
kcore_copy__for_each_phdr(, p) {
off_t offs = p->rel + offset;
 
+   if (p->remaps)
+   continue;
if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
goto out_extract_close;
}


[tip:perf/core] perf kcore_copy: Amend the offset of sections that remap kernel text

2018-05-23 Thread tip-bot for Adrian Hunter
Commit-ID:  22916fdb9c50e8fb303bdcedca88fd8798a85844
Gitweb: https://git.kernel.org/tip/22916fdb9c50e8fb303bdcedca88fd8798a85844
Author: Adrian Hunter 
AuthorDate: Tue, 22 May 2018 13:54:45 +0300
Committer:  Arnaldo Carvalho de Melo 
CommitDate: Wed, 23 May 2018 10:26:44 -0300

perf kcore_copy: Amend the offset of sections that remap kernel text

x86 PTI entry trampolines all map to the same physical page. If that is
reflected in the program headers of /proc/kcore, then do the same for the
copy of kcore.

Signed-off-by: Adrian Hunter 
Cc: Alexander Shishkin 
Cc: Andi Kleen 
Cc: Andy Lutomirski 
Cc: Dave Hansen 
Cc: H. Peter Anvin 
Cc: Jiri Olsa 
Cc: Joerg Roedel 
Cc: Peter Zijlstra 
Cc: Thomas Gleixner 
Cc: x...@kernel.org
Link: 
http://lkml.kernel.org/r/1526986485-6562-18-git-send-email-adrian.hun...@intel.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/util/symbol-elf.c | 53 ++--
 1 file changed, 51 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 584966913aeb..29770ea61768 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -1390,6 +1390,7 @@ struct phdr_data {
u64 addr;
u64 len;
struct list_head node;
+   struct phdr_data *remaps;
 };
 
 struct sym_data {
@@ -1597,16 +1598,62 @@ static int kcore_copy__read_maps(struct kcore_copy_info 
*kci, Elf *elf)
return 0;
 }
 
+static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
+{
+   struct phdr_data *p, *k = NULL;
+   u64 kend;
+
+   if (!kci->stext)
+   return;
+
+   /* Find phdr that corresponds to the kernel map (contains stext) */
+   kcore_copy__for_each_phdr(kci, p) {
+   u64 pend = p->addr + p->len - 1;
+
+   if (p->addr <= kci->stext && pend >= kci->stext) {
+   k = p;
+   break;
+   }
+   }
+
+   if (!k)
+   return;
+
+   kend = k->offset + k->len;
+
+   /* Find phdrs that remap the kernel */
+   kcore_copy__for_each_phdr(kci, p) {
+   u64 pend = p->offset + p->len;
+
+   if (p == k)
+   continue;
+
+   if (p->offset >= k->offset && pend <= kend)
+   p->remaps = k;
+   }
+}
+
 static void kcore_copy__layout(struct kcore_copy_info *kci)
 {
struct phdr_data *p;
off_t rel = 0;
 
+   kcore_copy__find_remaps(kci);
+
kcore_copy__for_each_phdr(kci, p) {
-   p->rel = rel;
-   rel += p->len;
+   if (!p->remaps) {
+   p->rel = rel;
+   rel += p->len;
+   }
kci->phnum += 1;
}
+
+   kcore_copy__for_each_phdr(kci, p) {
+   struct phdr_data *k = p->remaps;
+
+   if (k)
+   p->rel = p->offset - k->offset + k->rel;
+   }
 }
 
 static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
@@ -1821,6 +1868,8 @@ int kcore_copy(const char *from_dir, const char *to_dir)
kcore_copy__for_each_phdr(, p) {
off_t offs = p->rel + offset;
 
+   if (p->remaps)
+   continue;
if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
goto out_extract_close;
}