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

perf namespaces: Add functions to access nsinfo

Having functions to access nsinfo reduces the places where reference
counting checking needs to be added.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: André Almeida <andrealmeid@collabora.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Hao Luo <haoluo@google.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.garry@huawei.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Miaoqian Lin <linmq006@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
Cc: Song Liu <song@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Yury Norov <yury.norov@gmail.com>
Link: http://lore.kernel.org/lkml/20220211103415.2737789-14-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
bcaf0a97 b80675fa

+63 -27
+1 -1
tools/perf/builtin-inject.c
··· 356 356 nnsi = nsinfo__copy(nsi); 357 357 if (nnsi) { 358 358 nsinfo__put(nsi); 359 - nnsi->need_setns = false; 359 + nsinfo__clear_need_setns(nnsi); 360 360 nsi = nnsi; 361 361 } 362 362 dso = machine__findnew_vdso(machine, thread);
+1 -1
tools/perf/builtin-probe.c
··· 217 217 return ret; 218 218 } 219 219 nsip = nsinfo__new(ns_pid); 220 - if (nsip && nsip->need_setns) 220 + if (nsip && nsinfo__need_setns(nsip)) 221 221 params.nsi = nsinfo__get(nsip); 222 222 nsinfo__put(nsip); 223 223
+2 -2
tools/perf/util/build-id.c
··· 706 706 if (is_kallsyms) { 707 707 if (copyfile("/proc/kallsyms", filename)) 708 708 goto out_free; 709 - } else if (nsi && nsi->need_setns) { 709 + } else if (nsi && nsinfo__need_setns(nsi)) { 710 710 if (copyfile_ns(name, filename, nsi)) 711 711 goto out_free; 712 712 } else if (link(realname, filename) && errno != EEXIST && ··· 730 730 goto out_free; 731 731 } 732 732 if (access(filename, F_OK)) { 733 - if (nsi && nsi->need_setns) { 733 + if (nsi && nsinfo__need_setns(nsi)) { 734 734 if (copyfile_ns(debugfile, filename, 735 735 nsi)) 736 736 goto out_free;
+5 -5
tools/perf/util/jitdump.c
··· 382 382 383 383 static pid_t jr_entry_pid(struct jit_buf_desc *jd, union jr_entry *jr) 384 384 { 385 - if (jd->nsi && jd->nsi->in_pidns) 386 - return jd->nsi->tgid; 385 + if (jd->nsi && nsinfo__in_pidns(jd->nsi)) 386 + return nsinfo__tgid(jd->nsi); 387 387 return jr->load.pid; 388 388 } 389 389 390 390 static pid_t jr_entry_tid(struct jit_buf_desc *jd, union jr_entry *jr) 391 391 { 392 - if (jd->nsi && jd->nsi->in_pidns) 393 - return jd->nsi->pid; 392 + if (jd->nsi && nsinfo__in_pidns(jd->nsi)) 393 + return nsinfo__pid(jd->nsi); 394 394 return jr->load.tid; 395 395 } 396 396 ··· 779 779 * pid does not match mmap pid 780 780 * pid==0 in system-wide mode (synthesized) 781 781 */ 782 - if (pid && pid2 != nsi->nstgid) 782 + if (pid && pid2 != nsinfo__nstgid(nsi)) 783 783 return -1; 784 784 /* 785 785 * validate suffix
+2 -2
tools/perf/util/map.c
··· 151 151 152 152 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) { 153 153 snprintf(newfilename, sizeof(newfilename), 154 - "/tmp/perf-%d.map", nsi->pid); 154 + "/tmp/perf-%d.map", nsinfo__pid(nsi)); 155 155 filename = newfilename; 156 156 } 157 157 ··· 168 168 nnsi = nsinfo__copy(nsi); 169 169 if (nnsi) { 170 170 nsinfo__put(nsi); 171 - nnsi->need_setns = false; 171 + nsinfo__clear_need_setns(nnsi); 172 172 nsi = nnsi; 173 173 } 174 174 pgoff = 0;
+40 -10
tools/perf/util/namespaces.c
··· 76 76 if (strstr(statln, "Tgid:") != NULL) { 77 77 nsi->tgid = (pid_t)strtol(strrchr(statln, '\t'), 78 78 NULL, 10); 79 - nsi->nstgid = nsi->tgid; 79 + nsi->nstgid = nsinfo__tgid(nsi); 80 80 } 81 81 82 82 if (strstr(statln, "NStgid:") != NULL) { ··· 108 108 if (snprintf(oldns, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) 109 109 return rv; 110 110 111 - if (asprintf(&newns, "/proc/%d/ns/mnt", nsi->pid) == -1) 111 + if (asprintf(&newns, "/proc/%d/ns/mnt", nsinfo__pid(nsi)) == -1) 112 112 return rv; 113 113 114 114 if (stat(oldns, &old_stat) < 0) ··· 129 129 /* If we're dealing with a process that is in a different PID namespace, 130 130 * attempt to work out the innermost tgid for the process. 131 131 */ 132 - if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsi->pid) >= PATH_MAX) 132 + if (snprintf(spath, PATH_MAX, "/proc/%d/status", nsinfo__pid(nsi)) >= PATH_MAX) 133 133 goto out; 134 134 135 135 rv = nsinfo__get_nspid(nsi, spath); ··· 166 166 return nsi; 167 167 } 168 168 169 - struct nsinfo *nsinfo__copy(struct nsinfo *nsi) 169 + struct nsinfo *nsinfo__copy(const struct nsinfo *nsi) 170 170 { 171 171 struct nsinfo *nnsi; 172 172 ··· 175 175 176 176 nnsi = calloc(1, sizeof(*nnsi)); 177 177 if (nnsi != NULL) { 178 - nnsi->pid = nsi->pid; 179 - nnsi->tgid = nsi->tgid; 180 - nnsi->nstgid = nsi->nstgid; 181 - nnsi->need_setns = nsi->need_setns; 182 - nnsi->in_pidns = nsi->in_pidns; 178 + nnsi->pid = nsinfo__pid(nsi); 179 + nnsi->tgid = nsinfo__tgid(nsi); 180 + nnsi->nstgid = nsinfo__nstgid(nsi); 181 + nnsi->need_setns = nsinfo__need_setns(nsi); 182 + nnsi->in_pidns = nsinfo__in_pidns(nsi); 183 183 if (nsi->mntns_path) { 184 184 nnsi->mntns_path = strdup(nsi->mntns_path); 185 185 if (!nnsi->mntns_path) { ··· 193 193 return nnsi; 194 194 } 195 195 196 - void nsinfo__delete(struct nsinfo *nsi) 196 + static void nsinfo__delete(struct nsinfo *nsi) 197 197 { 198 198 zfree(&nsi->mntns_path); 199 199 free(nsi); ··· 210 210 { 211 211 if (nsi && refcount_dec_and_test(&nsi->refcnt)) 212 212 nsinfo__delete(nsi); 213 + } 214 + 215 + bool nsinfo__need_setns(const struct nsinfo *nsi) 216 + { 217 + return nsi->need_setns; 218 + } 219 + 220 + void nsinfo__clear_need_setns(struct nsinfo *nsi) 221 + { 222 + nsi->need_setns = false; 223 + } 224 + 225 + pid_t nsinfo__tgid(const struct nsinfo *nsi) 226 + { 227 + return nsi->tgid; 228 + } 229 + 230 + pid_t nsinfo__nstgid(const struct nsinfo *nsi) 231 + { 232 + return nsi->nstgid; 233 + } 234 + 235 + pid_t nsinfo__pid(const struct nsinfo *nsi) 236 + { 237 + return nsi->pid; 238 + } 239 + 240 + pid_t nsinfo__in_pidns(const struct nsinfo *nsi) 241 + { 242 + return nsi->in_pidns; 213 243 } 214 244 215 245 void nsinfo__mountns_enter(struct nsinfo *nsi,
+8 -2
tools/perf/util/namespaces.h
··· 47 47 48 48 int nsinfo__init(struct nsinfo *nsi); 49 49 struct nsinfo *nsinfo__new(pid_t pid); 50 - struct nsinfo *nsinfo__copy(struct nsinfo *nsi); 51 - void nsinfo__delete(struct nsinfo *nsi); 50 + struct nsinfo *nsinfo__copy(const struct nsinfo *nsi); 52 51 53 52 struct nsinfo *nsinfo__get(struct nsinfo *nsi); 54 53 void nsinfo__put(struct nsinfo *nsi); 54 + 55 + bool nsinfo__need_setns(const struct nsinfo *nsi); 56 + void nsinfo__clear_need_setns(struct nsinfo *nsi); 57 + pid_t nsinfo__tgid(const struct nsinfo *nsi); 58 + pid_t nsinfo__nstgid(const struct nsinfo *nsi); 59 + pid_t nsinfo__pid(const struct nsinfo *nsi); 60 + pid_t nsinfo__in_pidns(const struct nsinfo *nsi); 55 61 56 62 void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc); 57 63 void nsinfo__mountns_exit(struct nscookie *nc);
+4 -4
tools/perf/util/symbol.c
··· 1735 1735 1736 1736 nsi = *nsip; 1737 1737 1738 - if (nsi->need_setns) { 1739 - snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid); 1738 + if (nsinfo__need_setns(nsi)) { 1739 + snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__nstgid(nsi)); 1740 1740 nsinfo__mountns_enter(nsi, &nsc); 1741 1741 rc = access(filebuf, R_OK); 1742 1742 nsinfo__mountns_exit(&nsc); ··· 1748 1748 if (nnsi) { 1749 1749 nsinfo__put(nsi); 1750 1750 1751 - nnsi->need_setns = false; 1752 - snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid); 1751 + nsinfo__clear_need_setns(nnsi); 1752 + snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__tgid(nnsi)); 1753 1753 *nsip = nnsi; 1754 1754 rc = 0; 1755 1755 }