Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Restore proper cwd on return from mnt namespace

When reporting on 'record' server we try to retrieve/use the mnt
namespace of the profiled tasks. We use following API with cookie to
hold the return namespace, roughly:

nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc)
setns(newns, 0);
...
new ns related open..
...
nsinfo__mountns_exit(struct nscookie *nc)
setns(nc->oldns)

Once finished we setns to old namespace, which also sets the current
working directory (cwd) to "/", trashing the cwd we had.

This is mostly fine, because we use absolute paths almost everywhere,
but it screws up 'perf diff':

# perf diff
failed to open perf.data: No such file or directory (try 'perf record' first)
...

Adding the current working directory to be part of the cookie and
restoring it in the nsinfo__mountns_exit call.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Krister Johansen <kjlx@templeofstupid.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Fixes: 843ff37bb59e ("perf symbols: Find symbols in different mount namespace")
Link: http://lkml.kernel.org/r/20181101170001.30019-1-jolsa@kernel.org
[ No need to check for NULL args for free(), use zfree() for struct members ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
b01c1f69 8feb8efe

+16 -2
+15 -2
tools/perf/util/namespaces.c
··· 18 18 #include <stdio.h> 19 19 #include <string.h> 20 20 #include <unistd.h> 21 + #include <asm/bug.h> 21 22 22 23 struct namespaces *namespaces__new(struct namespaces_event *event) 23 24 { ··· 187 186 char curpath[PATH_MAX]; 188 187 int oldns = -1; 189 188 int newns = -1; 189 + char *oldcwd = NULL; 190 190 191 191 if (nc == NULL) 192 192 return; ··· 201 199 if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) 202 200 return; 203 201 202 + oldcwd = get_current_dir_name(); 203 + if (!oldcwd) 204 + return; 205 + 204 206 oldns = open(curpath, O_RDONLY); 205 207 if (oldns < 0) 206 - return; 208 + goto errout; 207 209 208 210 newns = open(nsi->mntns_path, O_RDONLY); 209 211 if (newns < 0) ··· 216 210 if (setns(newns, CLONE_NEWNS) < 0) 217 211 goto errout; 218 212 213 + nc->oldcwd = oldcwd; 219 214 nc->oldns = oldns; 220 215 nc->newns = newns; 221 216 return; 222 217 223 218 errout: 219 + free(oldcwd); 224 220 if (oldns > -1) 225 221 close(oldns); 226 222 if (newns > -1) ··· 231 223 232 224 void nsinfo__mountns_exit(struct nscookie *nc) 233 225 { 234 - if (nc == NULL || nc->oldns == -1 || nc->newns == -1) 226 + if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd) 235 227 return; 236 228 237 229 setns(nc->oldns, CLONE_NEWNS); 230 + 231 + if (nc->oldcwd) { 232 + WARN_ON_ONCE(chdir(nc->oldcwd)); 233 + zfree(&nc->oldcwd); 234 + } 238 235 239 236 if (nc->oldns > -1) { 240 237 close(nc->oldns);
+1
tools/perf/util/namespaces.h
··· 38 38 struct nscookie { 39 39 int oldns; 40 40 int newns; 41 + char *oldcwd; 41 42 }; 42 43 43 44 int nsinfo__init(struct nsinfo *nsi);