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

perf bench inject-buildid: Handle writen() errors

The build on fedora:35 and fedora:rawhide with clang is failing with:

49 41.00 fedora:35 : FAIL clang version 13.0.0 (Fedora 13.0.0~rc1-1.fc35)
bench/inject-buildid.c:351:6: error: variable 'len' set but not used [-Werror,-Wunused-but-set-variable]
u64 len = 0;
^
1 error generated.
make[3]: *** [/git/perf-5.14.0-rc7/tools/build/Makefile.build:139: bench] Error 2
50 41.11 fedora:rawhide : FAIL clang version 13.0.0 (Fedora 13.0.0~rc1-1.fc35)
bench/inject-buildid.c:351:6: error: variable 'len' set but not used [-Werror,-Wunused-but-set-variable]
u64 len = 0;
^
1 error generated.
make[3]: *** [/git/perf-5.14.0-rc7/tools/build/Makefile.build:139: bench] Error 2

That 'len' variable is not used at all, so just make sure all the
synthesize_RECORD() routines return ssize_t to propagate the writen()
return, as it may fail, ditch the 'ret' var and bail out if those
routines fail.

Fixes: 0bf02a0d80427f26 ("perf bench: Add build-id injection benchmark")
Acked-by: Namhyung Kim <namhyung@kernel.org>
Link: http://lore.kernel.org/lkml/CAM9d7cgEZNSor+B+7Y2C+QYGme_v5aH0Zn0RLfxoQ+Fy83EHrg@mail.gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+30 -22
+30 -22
tools/perf/bench/inject-buildid.c
··· 133 133 return 0x400000ULL + dso->ino * 8192ULL; 134 134 } 135 135 136 - static u32 synthesize_attr(struct bench_data *data) 136 + static ssize_t synthesize_attr(struct bench_data *data) 137 137 { 138 138 union perf_event event; 139 139 ··· 151 151 return writen(data->input_pipe[1], &event, event.header.size); 152 152 } 153 153 154 - static u32 synthesize_fork(struct bench_data *data) 154 + static ssize_t synthesize_fork(struct bench_data *data) 155 155 { 156 156 union perf_event event; 157 157 ··· 169 169 return writen(data->input_pipe[1], &event, event.header.size); 170 170 } 171 171 172 - static u32 synthesize_mmap(struct bench_data *data, struct bench_dso *dso, 173 - u64 timestamp) 172 + static ssize_t synthesize_mmap(struct bench_data *data, struct bench_dso *dso, u64 timestamp) 174 173 { 175 174 union perf_event event; 176 175 size_t len = offsetof(struct perf_record_mmap2, filename); ··· 197 198 198 199 if (len > sizeof(event.mmap2)) { 199 200 /* write mmap2 event first */ 200 - writen(data->input_pipe[1], &event, len - bench_id_hdr_size); 201 + if (writen(data->input_pipe[1], &event, len - bench_id_hdr_size) < 0) 202 + return -1; 201 203 /* zero-fill sample id header */ 202 204 memset(id_hdr_ptr, 0, bench_id_hdr_size); 203 205 /* put timestamp in the right position */ 204 206 ts_idx = (bench_id_hdr_size / sizeof(u64)) - 2; 205 207 id_hdr_ptr[ts_idx] = timestamp; 206 - writen(data->input_pipe[1], id_hdr_ptr, bench_id_hdr_size); 207 - } else { 208 - ts_idx = (len / sizeof(u64)) - 2; 209 - id_hdr_ptr[ts_idx] = timestamp; 210 - writen(data->input_pipe[1], &event, len); 208 + if (writen(data->input_pipe[1], id_hdr_ptr, bench_id_hdr_size) < 0) 209 + return -1; 210 + 211 + return len; 211 212 } 212 - return len; 213 + 214 + ts_idx = (len / sizeof(u64)) - 2; 215 + id_hdr_ptr[ts_idx] = timestamp; 216 + return writen(data->input_pipe[1], &event, len); 213 217 } 214 218 215 - static u32 synthesize_sample(struct bench_data *data, struct bench_dso *dso, 216 - u64 timestamp) 219 + static ssize_t synthesize_sample(struct bench_data *data, struct bench_dso *dso, u64 timestamp) 217 220 { 218 221 union perf_event event; 219 222 struct perf_sample sample = { ··· 234 233 return writen(data->input_pipe[1], &event, event.header.size); 235 234 } 236 235 237 - static u32 synthesize_flush(struct bench_data *data) 236 + static ssize_t synthesize_flush(struct bench_data *data) 238 237 { 239 238 struct perf_event_header header = { 240 239 .size = sizeof(header), ··· 349 348 int status; 350 349 unsigned int i, k; 351 350 struct rusage rusage; 352 - u64 len = 0; 353 351 354 352 /* this makes the child to run */ 355 353 if (perf_header__write_pipe(data->input_pipe[1]) < 0) 356 354 return -1; 357 355 358 - len += synthesize_attr(data); 359 - len += synthesize_fork(data); 356 + if (synthesize_attr(data) < 0) 357 + return -1; 358 + 359 + if (synthesize_fork(data) < 0) 360 + return -1; 360 361 361 362 for (i = 0; i < nr_mmaps; i++) { 362 363 int idx = rand() % (nr_dsos - 1); ··· 366 363 u64 timestamp = rand() % 1000000; 367 364 368 365 pr_debug2(" [%d] injecting: %s\n", i+1, dso->name); 369 - len += synthesize_mmap(data, dso, timestamp); 366 + if (synthesize_mmap(data, dso, timestamp) < 0) 367 + return -1; 370 368 371 - for (k = 0; k < nr_samples; k++) 372 - len += synthesize_sample(data, dso, timestamp + k * 1000); 369 + for (k = 0; k < nr_samples; k++) { 370 + if (synthesize_sample(data, dso, timestamp + k * 1000) < 0) 371 + return -1; 372 + } 373 373 374 - if ((i + 1) % 10 == 0) 375 - len += synthesize_flush(data); 374 + if ((i + 1) % 10 == 0) { 375 + if (synthesize_flush(data) < 0) 376 + return -1; 377 + } 376 378 } 377 379 378 380 /* this makes the child to finish */