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

perf session: Return error code for perf_session__new() function on failure

This patch is to return error code of perf_new_session function on
failure instead of NULL.

Test Results:

Before Fix:

$ perf c2c report -input
failed to open nput: No such file or directory

$ echo $?
0
$

After Fix:

$ perf c2c report -input
failed to open nput: No such file or directory

$ echo $?
254
$

Committer notes:

Fix 'perf tests topology' case, where we use that TEST_ASSERT_VAL(...,
session), i.e. we need to pass zero in case of failure, which was the
case before when NULL was returned by perf_session__new() for failure,
but now we need to negate the result of IS_ERR(session) to respect that
TEST_ASSERT_VAL) expectation of zero meaning failure.

Reported-by: Nageswara R Sastry <rnsastry@linux.vnet.ibm.com>
Signed-off-by: Mamatha Inamdar <mamatha4@linux.vnet.ibm.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Nageswara R Sastry <rnsastry@linux.vnet.ibm.com>
Acked-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
Reviewed-by: Jiri Olsa <jolsa@redhat.com>
Reviewed-by: Mukesh Ojha <mojha@codeaurora.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jeremie Galarneau <jeremie.galarneau@efficios.com>
Cc: Kate Stewart <kstewart@linuxfoundation.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Shawn Landden <shawn@git.icu>
Cc: Song Liu <songliubraving@fb.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Tzvetomir Stoyanov <tstoyanov@vmware.com>
Link: http://lore.kernel.org/lkml/20190822071223.17892.45782.stgit@localhost.localdomain
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Mamatha Inamdar and committed by
Arnaldo Carvalho de Melo
6ef81c55 9e6124d9

+86 -57
+3 -2
tools/perf/builtin-annotate.c
··· 40 40 #include <dlfcn.h> 41 41 #include <errno.h> 42 42 #include <linux/bitmap.h> 43 + #include <linux/err.h> 43 44 44 45 struct perf_annotate { 45 46 struct perf_tool tool; ··· 585 584 data.path = input_name; 586 585 587 586 annotate.session = perf_session__new(&data, false, &annotate.tool); 588 - if (annotate.session == NULL) 589 - return -1; 587 + if (IS_ERR(annotate.session)) 588 + return PTR_ERR(annotate.session); 590 589 591 590 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header, 592 591 HEADER_BRANCH_STACK);
+3 -2
tools/perf/builtin-buildid-cache.c
··· 28 28 #include "util/util.h" 29 29 #include "util/probe-file.h" 30 30 #include <linux/string.h> 31 + #include <linux/err.h> 31 32 32 33 static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid) 33 34 { ··· 423 422 data.force = force; 424 423 425 424 session = perf_session__new(&data, false, NULL); 426 - if (session == NULL) 427 - return -1; 425 + if (IS_ERR(session)) 426 + return PTR_ERR(session); 428 427 } 429 428 430 429 if (symbol__init(session ? &session->header.env : NULL) < 0)
+3 -2
tools/perf/builtin-buildid-list.c
··· 18 18 #include "util/symbol.h" 19 19 #include "util/data.h" 20 20 #include <errno.h> 21 + #include <linux/err.h> 21 22 22 23 static int sysfs__fprintf_build_id(FILE *fp) 23 24 { ··· 66 65 goto out; 67 66 68 67 session = perf_session__new(&data, false, &build_id__mark_dso_hit_ops); 69 - if (session == NULL) 70 - return -1; 68 + if (IS_ERR(session)) 69 + return PTR_ERR(session); 71 70 72 71 /* 73 72 * We take all buildids when the file contains AUX area tracing data
+4 -2
tools/perf/builtin-c2c.c
··· 13 13 #include <errno.h> 14 14 #include <inttypes.h> 15 15 #include <linux/compiler.h> 16 + #include <linux/err.h> 16 17 #include <linux/kernel.h> 17 18 #include <linux/stringify.h> 18 19 #include <linux/zalloc.h> ··· 2782 2781 } 2783 2782 2784 2783 session = perf_session__new(&data, 0, &c2c.tool); 2785 - if (session == NULL) { 2786 - pr_debug("No memory for session\n"); 2784 + if (IS_ERR(session)) { 2785 + err = PTR_ERR(session); 2786 + pr_debug("Error creating perf session\n"); 2787 2787 goto out; 2788 2788 } 2789 2789
+5 -4
tools/perf/builtin-diff.c
··· 23 23 #include "util/time-utils.h" 24 24 #include "util/annotate.h" 25 25 #include "util/map.h" 26 + #include <linux/err.h> 26 27 #include <linux/zalloc.h> 27 28 #include <subcmd/pager.h> 28 29 #include <subcmd/parse-options.h> ··· 1154 1153 1155 1154 data__for_each_file(i, d) { 1156 1155 d->session = perf_session__new(&d->data, false, &pdiff.tool); 1157 - if (!d->session) { 1156 + if (IS_ERR(d->session)) { 1158 1157 pr_err("Failed to open %s\n", d->data.path); 1159 - return -1; 1158 + return PTR_ERR(d->session); 1160 1159 } 1161 1160 1162 1161 has_br_stack = perf_header__has_feat(&d->session->header, ··· 1186 1185 1187 1186 data__for_each_file(i, d) { 1188 1187 d->session = perf_session__new(&d->data, false, &pdiff.tool); 1189 - if (!d->session) { 1188 + if (IS_ERR(d->session)) { 1189 + ret = PTR_ERR(d->session); 1190 1190 pr_err("Failed to open %s\n", d->data.path); 1191 - ret = -1; 1192 1191 goto out_delete; 1193 1192 } 1194 1193
+3 -2
tools/perf/builtin-evlist.c
··· 15 15 #include "util/session.h" 16 16 #include "util/data.h" 17 17 #include "util/debug.h" 18 + #include <linux/err.h> 18 19 19 20 static int __cmd_evlist(const char *file_name, struct perf_attr_details *details) 20 21 { ··· 29 28 bool has_tracepoint = false; 30 29 31 30 session = perf_session__new(&data, 0, NULL); 32 - if (session == NULL) 33 - return -1; 31 + if (IS_ERR(session)) 32 + return PTR_ERR(session); 34 33 35 34 evlist__for_each_entry(session->evlist, pos) { 36 35 perf_evsel__fprintf(pos, details, stdout);
+3 -2
tools/perf/builtin-inject.c
··· 23 23 #include "util/symbol.h" 24 24 #include "util/synthetic-events.h" 25 25 #include "util/thread.h" 26 + #include <linux/err.h> 26 27 27 28 #include <subcmd/parse-options.h> 28 29 ··· 836 835 837 836 data.path = inject.input_name; 838 837 inject.session = perf_session__new(&data, true, &inject.tool); 839 - if (inject.session == NULL) 840 - return -1; 838 + if (IS_ERR(inject.session)) 839 + return PTR_ERR(inject.session); 841 840 842 841 if (zstd_init(&(inject.session->zstd_data), 0) < 0) 843 842 pr_warning("Decompression initialization failed.\n");
+3 -2
tools/perf/builtin-kmem.c
··· 14 14 #include "util/tool.h" 15 15 #include "util/callchain.h" 16 16 #include "util/time-utils.h" 17 + #include <linux/err.h> 17 18 18 19 #include <subcmd/pager.h> 19 20 #include <subcmd/parse-options.h> ··· 1957 1956 data.path = input_name; 1958 1957 1959 1958 kmem_session = session = perf_session__new(&data, false, &perf_kmem); 1960 - if (session == NULL) 1961 - return -1; 1959 + if (IS_ERR(session)) 1960 + return PTR_ERR(session); 1962 1961 1963 1962 ret = -1; 1964 1963
+5 -4
tools/perf/builtin-kvm.c
··· 33 33 #include <sys/stat.h> 34 34 #include <fcntl.h> 35 35 36 + #include <linux/err.h> 36 37 #include <linux/kernel.h> 37 38 #include <linux/string.h> 38 39 #include <linux/time64.h> ··· 1092 1091 1093 1092 kvm->tool = eops; 1094 1093 kvm->session = perf_session__new(&file, false, &kvm->tool); 1095 - if (!kvm->session) { 1094 + if (IS_ERR(kvm->session)) { 1096 1095 pr_err("Initializing perf session failed\n"); 1097 - return -1; 1096 + return PTR_ERR(kvm->session); 1098 1097 } 1099 1098 1100 1099 symbol__init(&kvm->session->header.env); ··· 1447 1446 * perf session 1448 1447 */ 1449 1448 kvm->session = perf_session__new(&data, false, &kvm->tool); 1450 - if (kvm->session == NULL) { 1451 - err = -1; 1449 + if (IS_ERR(kvm->session)) { 1450 + err = PTR_ERR(kvm->session); 1452 1451 goto out; 1453 1452 } 1454 1453 kvm->session->evlist = kvm->evlist;
+3 -2
tools/perf/builtin-lock.c
··· 30 30 #include <linux/hash.h> 31 31 #include <linux/kernel.h> 32 32 #include <linux/zalloc.h> 33 + #include <linux/err.h> 33 34 34 35 static struct perf_session *session; 35 36 ··· 873 872 }; 874 873 875 874 session = perf_session__new(&data, false, &eops); 876 - if (!session) { 875 + if (IS_ERR(session)) { 877 876 pr_err("Initializing perf session failed\n"); 878 - return -1; 877 + return PTR_ERR(session); 879 878 } 880 879 881 880 symbol__init(&session->header.env);
+3 -2
tools/perf/builtin-mem.c
··· 17 17 #include "util/dso.h" 18 18 #include "util/map.h" 19 19 #include "util/symbol.h" 20 + #include <linux/err.h> 20 21 21 22 #define MEM_OPERATION_LOAD 0x1 22 23 #define MEM_OPERATION_STORE 0x2 ··· 250 249 struct perf_session *session = perf_session__new(&data, false, 251 250 &mem->tool); 252 251 253 - if (session == NULL) 254 - return -1; 252 + if (IS_ERR(session)) 253 + return PTR_ERR(session); 255 254 256 255 if (mem->cpu_list) { 257 256 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
+3 -2
tools/perf/builtin-record.c
··· 54 54 #include <signal.h> 55 55 #include <sys/mman.h> 56 56 #include <sys/wait.h> 57 + #include <linux/err.h> 57 58 #include <linux/string.h> 58 59 #include <linux/time64.h> 59 60 #include <linux/zalloc.h> ··· 1355 1354 } 1356 1355 1357 1356 session = perf_session__new(data, false, tool); 1358 - if (session == NULL) { 1357 + if (IS_ERR(session)) { 1359 1358 pr_err("Perf session creation failed.\n"); 1360 - return -1; 1359 + return PTR_ERR(session); 1361 1360 } 1362 1361 1363 1362 fd = perf_data__fd(data);
+2 -2
tools/perf/builtin-report.c
··· 1269 1269 1270 1270 repeat: 1271 1271 session = perf_session__new(&data, false, &report.tool); 1272 - if (session == NULL) 1273 - return -1; 1272 + if (IS_ERR(session)) 1273 + return PTR_ERR(session); 1274 1274 1275 1275 ret = evswitch__init(&report.evswitch, session->evlist, stderr); 1276 1276 if (ret)
+6 -5
tools/perf/builtin-sched.c
··· 40 40 #include <api/fs/fs.h> 41 41 #include <perf/cpumap.h> 42 42 #include <linux/time64.h> 43 + #include <linux/err.h> 43 44 44 45 #include <linux/ctype.h> 45 46 ··· 1798 1797 int rc = -1; 1799 1798 1800 1799 session = perf_session__new(&data, false, &sched->tool); 1801 - if (session == NULL) { 1802 - pr_debug("No Memory for session\n"); 1803 - return -1; 1800 + if (IS_ERR(session)) { 1801 + pr_debug("Error creating perf session"); 1802 + return PTR_ERR(session); 1804 1803 } 1805 1804 1806 1805 symbol__init(&session->header.env); ··· 2990 2989 symbol_conf.use_callchain = sched->show_callchain; 2991 2990 2992 2991 session = perf_session__new(&data, false, &sched->tool); 2993 - if (session == NULL) 2994 - return -ENOMEM; 2992 + if (IS_ERR(session)) 2993 + return PTR_ERR(session); 2995 2994 2996 2995 evlist = session->evlist; 2997 2996
+5 -4
tools/perf/builtin-script.c
··· 52 52 #include <unistd.h> 53 53 #include <subcmd/pager.h> 54 54 #include <perf/evlist.h> 55 + #include <linux/err.h> 55 56 #include "util/record.h" 56 57 #include "util/util.h" 57 58 #include "perf.h" ··· 3084 3083 int i = 0; 3085 3084 3086 3085 session = perf_session__new(&data, false, NULL); 3087 - if (!session) 3088 - return -1; 3086 + if (IS_ERR(session)) 3087 + return PTR_ERR(session); 3089 3088 3090 3089 snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); 3091 3090 ··· 3755 3754 } 3756 3755 3757 3756 session = perf_session__new(&data, false, &script.tool); 3758 - if (session == NULL) 3759 - return -1; 3757 + if (IS_ERR(session)) 3758 + return PTR_ERR(session); 3760 3759 3761 3760 if (header || header_only) { 3762 3761 script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
+6 -5
tools/perf/builtin-stat.c
··· 83 83 #include <unistd.h> 84 84 #include <sys/time.h> 85 85 #include <sys/resource.h> 86 + #include <linux/err.h> 86 87 87 88 #include <linux/ctype.h> 88 89 #include <perf/evlist.h> ··· 1437 1436 } 1438 1437 1439 1438 session = perf_session__new(data, false, NULL); 1440 - if (session == NULL) { 1441 - pr_err("Perf session creation failed.\n"); 1442 - return -1; 1439 + if (IS_ERR(session)) { 1440 + pr_err("Perf session creation failed\n"); 1441 + return PTR_ERR(session); 1443 1442 } 1444 1443 1445 1444 init_features(session); ··· 1636 1635 perf_stat.data.mode = PERF_DATA_MODE_READ; 1637 1636 1638 1637 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool); 1639 - if (session == NULL) 1640 - return -1; 1638 + if (IS_ERR(session)) 1639 + return PTR_ERR(session); 1641 1640 1642 1641 perf_stat.session = session; 1643 1642 stat_config.output = stderr;
+3 -2
tools/perf/builtin-timechart.c
··· 35 35 #include "util/tool.h" 36 36 #include "util/data.h" 37 37 #include "util/debug.h" 38 + #include <linux/err.h> 38 39 39 40 #ifdef LACKS_OPEN_MEMSTREAM_PROTOTYPE 40 41 FILE *open_memstream(char **ptr, size_t *sizeloc); ··· 1602 1601 &tchart->tool); 1603 1602 int ret = -EINVAL; 1604 1603 1605 - if (session == NULL) 1606 - return -1; 1604 + if (IS_ERR(session)) 1605 + return PTR_ERR(session); 1607 1606 1608 1607 symbol__init(&session->header.env); 1609 1608
+3 -2
tools/perf/builtin-top.c
··· 77 77 #include <linux/stringify.h> 78 78 #include <linux/time64.h> 79 79 #include <linux/types.h> 80 + #include <linux/err.h> 80 81 81 82 #include <linux/ctype.h> 82 83 ··· 1673 1672 } 1674 1673 1675 1674 top.session = perf_session__new(NULL, false, NULL); 1676 - if (top.session == NULL) { 1677 - status = -1; 1675 + if (IS_ERR(top.session)) { 1676 + status = PTR_ERR(top.session); 1678 1677 goto out_delete_evlist; 1679 1678 } 1680 1679
+2 -2
tools/perf/builtin-trace.c
··· 3585 3585 trace->multiple_threads = true; 3586 3586 3587 3587 session = perf_session__new(&data, false, &trace->tool); 3588 - if (session == NULL) 3589 - return -1; 3588 + if (IS_ERR(session)) 3589 + return PTR_ERR(session); 3590 3590 3591 3591 if (trace->opts.target.pid) 3592 3592 symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
+3 -2
tools/perf/tests/topology.c
··· 8 8 #include "session.h" 9 9 #include "evlist.h" 10 10 #include "debug.h" 11 + #include <linux/err.h> 11 12 12 13 #define TEMPL "/tmp/perf-test-XXXXXX" 13 14 #define DATA_SIZE 10 ··· 40 39 }; 41 40 42 41 session = perf_session__new(&data, false, NULL); 43 - TEST_ASSERT_VAL("can't get session", session); 42 + TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); 44 43 45 44 session->evlist = perf_evlist__new_default(); 46 45 TEST_ASSERT_VAL("can't get evlist", session->evlist); ··· 71 70 int i; 72 71 73 72 session = perf_session__new(&data, false, NULL); 74 - TEST_ASSERT_VAL("can't get session", session); 73 + TEST_ASSERT_VAL("can't get session", !IS_ERR(session)); 75 74 76 75 /* On platforms with large numbers of CPUs process_cpu_topology() 77 76 * might issue an error while reading the perf.data file section
+4 -1
tools/perf/util/data-convert-bt.c
··· 30 30 #include "machine.h" 31 31 #include "config.h" 32 32 #include <linux/ctype.h> 33 + #include <linux/err.h> 33 34 34 35 #define pr_N(n, fmt, ...) \ 35 36 eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__) ··· 1620 1619 err = -1; 1621 1620 /* perf.data session */ 1622 1621 session = perf_session__new(&data, 0, &c.tool); 1623 - if (!session) 1622 + if (IS_ERR(session)) { 1623 + err = PTR_ERR(session); 1624 1624 goto free_writer; 1625 + } 1625 1626 1626 1627 if (c.queue_size) { 1627 1628 ordered_events__set_alloc_size(&session->ordered_events,
+11 -4
tools/perf/util/session.c
··· 34 34 #include "../perf.h" 35 35 #include "arch/common.h" 36 36 #include <internal/lib.h> 37 + #include <linux/err.h> 37 38 38 39 #ifdef HAVE_ZSTD_SUPPORT 39 40 static int perf_session__process_compressed_event(struct perf_session *session, ··· 188 187 struct perf_session *perf_session__new(struct perf_data *data, 189 188 bool repipe, struct perf_tool *tool) 190 189 { 190 + int ret = -ENOMEM; 191 191 struct perf_session *session = zalloc(sizeof(*session)); 192 192 193 193 if (!session) ··· 203 201 204 202 perf_env__init(&session->header.env); 205 203 if (data) { 206 - if (perf_data__open(data)) 204 + ret = perf_data__open(data); 205 + if (ret < 0) 207 206 goto out_delete; 208 207 209 208 session->data = data; 210 209 211 210 if (perf_data__is_read(data)) { 212 - if (perf_session__open(session) < 0) 211 + ret = perf_session__open(session); 212 + if (ret < 0) 213 213 goto out_delete; 214 214 215 215 /* ··· 226 222 perf_evlist__init_trace_event_sample_raw(session->evlist); 227 223 228 224 /* Open the directory data. */ 229 - if (data->is_dir && perf_data__open_dir(data)) 225 + if (data->is_dir) { 226 + ret = perf_data__open_dir(data); 227 + if (ret) 230 228 goto out_delete; 229 + } 231 230 } 232 231 } else { 233 232 session->machines.host.env = &perf_env; ··· 263 256 out_delete: 264 257 perf_session__delete(session); 265 258 out: 266 - return NULL; 259 + return ERR_PTR(ret); 267 260 } 268 261 269 262 static void perf_session__delete_threads(struct perf_session *session)