Skip to content

Commit 843ff37

Browse files
kmjohansenacmel
authored andcommitted
perf symbols: Find symbols in different mount namespace
Teach perf how to resolve symbols from binaries that are in a different mount namespace from the tool. This allows perf to generate meaningful stack traces even if the binary resides in a different mount namespace from the tool. Signed-off-by: Krister Johansen <kjlx@templeofstupid.com> Tested-by: Brendan Gregg <brendan.d.gregg@gmail.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas-Mich Richter <tmricht@linux.vnet.ibm.com> Link: http://lkml.kernel.org/r/1499305693-1599-2-git-send-email-kjlx@templeofstupid.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 86bcdb5 commit 843ff37

8 files changed

Lines changed: 180 additions & 0 deletions

File tree

‎tools/perf/util/dso.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ void dso__delete(struct dso *dso)
12361236
dso_cache__free(dso);
12371237
dso__free_a2l(dso);
12381238
zfree(&dso->symsrc_filename);
1239+
nsinfo__zput(dso->nsinfo);
12391240
pthread_mutex_destroy(&dso->lock);
12401241
free(dso);
12411242
}

‎tools/perf/util/dso.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/types.h>
1111
#include <linux/bitops.h>
1212
#include "map.h"
13+
#include "namespaces.h"
1314
#include "build-id.h"
1415

1516
enum dso_binary_type {
@@ -187,6 +188,7 @@ struct dso {
187188
void *priv;
188189
u64 db_id;
189190
};
191+
struct nsinfo *nsinfo;
190192
refcount_t refcnt;
191193
char name[0];
192194
};

‎tools/perf/util/map.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "machine.h"
1717
#include <linux/string.h>
1818
#include "srcline.h"
19+
#include "namespaces.h"
1920
#include "unwind.h"
2021

2122
static void __maps__insert(struct maps *maps, struct map *map);
@@ -200,6 +201,7 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
200201
if (type != MAP__FUNCTION)
201202
dso__set_loaded(dso, map->type);
202203
}
204+
dso->nsinfo = nsinfo__get(thread->nsinfo);
203205
dso__put(dso);
204206
}
205207
return map;

‎tools/perf/util/namespaces.c‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#include "namespaces.h"
1010
#include "util.h"
1111
#include "event.h"
12+
#include <sys/types.h>
13+
#include <sys/stat.h>
14+
#include <sched.h>
1215
#include <stdlib.h>
1316
#include <stdio.h>
1417
#include <string.h>
18+
#include <unistd.h>
1519

1620
struct namespaces *namespaces__new(struct namespaces_event *event)
1721
{
@@ -35,3 +39,126 @@ void namespaces__free(struct namespaces *namespaces)
3539
{
3640
free(namespaces);
3741
}
42+
43+
void nsinfo__init(struct nsinfo *nsi)
44+
{
45+
char oldns[PATH_MAX];
46+
char *newns = NULL;
47+
struct stat old_stat;
48+
struct stat new_stat;
49+
50+
if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
51+
return;
52+
53+
if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1)
54+
return;
55+
56+
if (stat(oldns, &old_stat) < 0)
57+
goto out;
58+
59+
if (stat(newns, &new_stat) < 0)
60+
goto out;
61+
62+
/* Check if the mount namespaces differ, if so then indicate that we
63+
* want to switch as part of looking up dso/map data.
64+
*/
65+
if (old_stat.st_ino != new_stat.st_ino) {
66+
nsi->need_setns = true;
67+
nsi->mntns_path = newns;
68+
newns = NULL;
69+
}
70+
71+
out:
72+
free(newns);
73+
}
74+
75+
struct nsinfo *nsinfo__new(pid_t pid)
76+
{
77+
struct nsinfo *nsi = calloc(1, sizeof(*nsi));
78+
79+
if (nsi != NULL) {
80+
nsi->pid = pid;
81+
nsi->need_setns = false;
82+
nsinfo__init(nsi);
83+
refcount_set(&nsi->refcnt, 1);
84+
}
85+
86+
return nsi;
87+
}
88+
89+
void nsinfo__delete(struct nsinfo *nsi)
90+
{
91+
zfree(&nsi->mntns_path);
92+
free(nsi);
93+
}
94+
95+
struct nsinfo *nsinfo__get(struct nsinfo *nsi)
96+
{
97+
if (nsi)
98+
refcount_inc(&nsi->refcnt);
99+
return nsi;
100+
}
101+
102+
void nsinfo__put(struct nsinfo *nsi)
103+
{
104+
if (nsi && refcount_dec_and_test(&nsi->refcnt))
105+
nsinfo__delete(nsi);
106+
}
107+
108+
void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc)
109+
{
110+
char curpath[PATH_MAX];
111+
int oldns = -1;
112+
int newns = -1;
113+
114+
if (nc == NULL)
115+
return;
116+
117+
nc->oldns = -1;
118+
nc->newns = -1;
119+
120+
if (!nsi || !nsi->need_setns)
121+
return;
122+
123+
if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX)
124+
return;
125+
126+
oldns = open(curpath, O_RDONLY);
127+
if (oldns < 0)
128+
return;
129+
130+
newns = open(nsi->mntns_path, O_RDONLY);
131+
if (newns < 0)
132+
goto errout;
133+
134+
if (setns(newns, CLONE_NEWNS) < 0)
135+
goto errout;
136+
137+
nc->oldns = oldns;
138+
nc->newns = newns;
139+
return;
140+
141+
errout:
142+
if (oldns > -1)
143+
close(oldns);
144+
if (newns > -1)
145+
close(newns);
146+
}
147+
148+
void nsinfo__mountns_exit(struct nscookie *nc)
149+
{
150+
if (nc == NULL || nc->oldns == -1 || nc->newns == -1)
151+
return;
152+
153+
setns(nc->oldns, CLONE_NEWNS);
154+
155+
if (nc->oldns > -1) {
156+
close(nc->oldns);
157+
nc->oldns = -1;
158+
}
159+
160+
if (nc->newns > -1) {
161+
close(nc->newns);
162+
nc->newns = -1;
163+
}
164+
}

‎tools/perf/util/namespaces.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "../perf.h"
1313
#include <linux/list.h>
14+
#include <linux/refcount.h>
1415

1516
struct namespaces_event;
1617

@@ -23,4 +24,36 @@ struct namespaces {
2324
struct namespaces *namespaces__new(struct namespaces_event *event);
2425
void namespaces__free(struct namespaces *namespaces);
2526

27+
struct nsinfo {
28+
pid_t pid;
29+
bool need_setns;
30+
char *mntns_path;
31+
refcount_t refcnt;
32+
};
33+
34+
struct nscookie {
35+
int oldns;
36+
int newns;
37+
};
38+
39+
void nsinfo__init(struct nsinfo *nsi);
40+
struct nsinfo *nsinfo__new(pid_t pid);
41+
void nsinfo__delete(struct nsinfo *nsi);
42+
43+
struct nsinfo *nsinfo__get(struct nsinfo *nsi);
44+
void nsinfo__put(struct nsinfo *nsi);
45+
46+
void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc);
47+
void nsinfo__mountns_exit(struct nscookie *nc);
48+
49+
static inline void __nsinfo__zput(struct nsinfo **nsip)
50+
{
51+
if (nsip) {
52+
nsinfo__put(*nsip);
53+
*nsip = NULL;
54+
}
55+
}
56+
57+
#define nsinfo__zput(nsi) __nsinfo__zput(&nsi)
58+
2659
#endif /* __PERF_NAMESPACES_H */

‎tools/perf/util/symbol.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "symbol.h"
1919
#include "strlist.h"
2020
#include "intlist.h"
21+
#include "namespaces.h"
22+
#include "vdso.h"
2123
#include "header.h"
2224
#include "path.h"
2325
#include "sane_ctype.h"
@@ -1436,9 +1438,17 @@ int dso__load(struct dso *dso, struct map *map)
14361438
struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
14371439
bool kmod;
14381440
unsigned char build_id[BUILD_ID_SIZE];
1441+
struct nscookie nsc;
14391442

1443+
nsinfo__mountns_enter(dso->nsinfo, &nsc);
14401444
pthread_mutex_lock(&dso->lock);
14411445

1446+
/* The vdso files always live in the host container, so don't go looking
1447+
* for them in the container's mount namespace.
1448+
*/
1449+
if (dso__is_vdso(dso))
1450+
nsinfo__mountns_exit(&nsc);
1451+
14421452
/* check again under the dso->lock */
14431453
if (dso__loaded(dso, map->type)) {
14441454
ret = 1;
@@ -1584,6 +1594,7 @@ int dso__load(struct dso *dso, struct map *map)
15841594
out:
15851595
dso__set_loaded(dso, map->type);
15861596
pthread_mutex_unlock(&dso->lock);
1597+
nsinfo__mountns_exit(&nsc);
15871598

15881599
return ret;
15891600
}

‎tools/perf/util/thread.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ struct thread *thread__new(pid_t pid, pid_t tid)
5959
list_add(&comm->list, &thread->comm_list);
6060
refcount_set(&thread->refcnt, 1);
6161
RB_CLEAR_NODE(&thread->rb_node);
62+
/* Thread holds first ref to nsdata. */
63+
thread->nsinfo = nsinfo__new(pid);
6264
}
6365

6466
return thread;
@@ -91,6 +93,7 @@ void thread__delete(struct thread *thread)
9193
comm__free(comm);
9294
}
9395
unwind__finish_access(thread);
96+
nsinfo__zput(thread->nsinfo);
9497

9598
free(thread);
9699
}

‎tools/perf/util/thread.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct thread {
3434

3535
void *priv;
3636
struct thread_stack *ts;
37+
struct nsinfo *nsinfo;
3738
#ifdef HAVE_LIBUNWIND_SUPPORT
3839
void *addr_space;
3940
struct unwind_libunwind_ops *unwind_libunwind_ops;

0 commit comments

Comments
 (0)