Skip to content

Commit

Permalink
libbpf-tools/syscount: Fix incorrect errno usage (#4991)
Browse files Browse the repository at this point in the history
In read_vals_batch function, the errno set by bpf_map_lookup_and_delete_batch()
may change if the next warn() fails. So the errno of read_vals:240 may be
incorrect.

bpf_map_lookup_and_delete_batch() already returns errno with a minus added on
failure, so used the return value instead of errno.
  • Loading branch information
ekyooo committed May 9, 2024
1 parent 1150523 commit 2447411
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions libbpf-tools/syscount.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static void print_timestamp()

static bool batch_map_ops = true; /* hope for the best */

static bool read_vals_batch(int fd, struct data_ext_t *vals, __u32 *count)
static int read_vals_batch(int fd, struct data_ext_t *vals, __u32 *count)
{
struct data_t orig_vals[*count];
void *in = NULL, *out;
Expand All @@ -203,13 +203,13 @@ static bool read_vals_batch(int fd, struct data_ext_t *vals, __u32 *count)
n = *count - n_read;
err = bpf_map_lookup_and_delete_batch(fd, &in, &out,
keys + n_read, orig_vals + n_read, &n, NULL);
if (err && errno != ENOENT) {
if (err < 0 && err != -ENOENT) {
/* we want to propagate EINVAL upper, so that
* the batch_map_ops flag is set to false */
if (errno != EINVAL)
if (err != -EINVAL)
warn("bpf_map_lookup_and_delete_batch: %s\n",
strerror(-err));
return false;
return err;
}
n_read += n;
in = out;
Expand All @@ -223,7 +223,7 @@ static bool read_vals_batch(int fd, struct data_ext_t *vals, __u32 *count)
}

*count = n_read;
return true;
return 0;
}

static bool read_vals(int fd, struct data_ext_t *vals, __u32 *count)
Expand All @@ -236,12 +236,12 @@ static bool read_vals(int fd, struct data_ext_t *vals, __u32 *count)
int err;

if (batch_map_ops) {
bool ok = read_vals_batch(fd, vals, count);
if (!ok && errno == EINVAL) {
err = read_vals_batch(fd, vals, count);
if (err < 0 && err == -EINVAL) {
/* fall back to a racy variant */
batch_map_ops = false;
} else {
return ok;
return err >= 0;
}
}

Expand Down

0 comments on commit 2447411

Please sign in to comment.