Commit 6803ee25 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Extract and generalize CPU mask parsing logic

This logic is re-used for parsing a set of online CPUs. Having it as an
isolated piece of code working with input string makes it conveninent to test
this logic as well. While refactoring, also improve the robustness of original
implementation.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191212013548.1690564-1-andriin@fb.com
parent 7708bd43
...@@ -6523,61 +6523,104 @@ void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear) ...@@ -6523,61 +6523,104 @@ void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
} }
} }
int libbpf_num_possible_cpus(void) int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
{ {
static const char *fcpu = "/sys/devices/system/cpu/possible"; int err = 0, n, len, start, end = -1;
int len = 0, n = 0, il = 0, ir = 0; bool *tmp;
unsigned int start = 0, end = 0;
int tmp_cpus = 0;
static int cpus;
char buf[128];
int error = 0;
int fd = -1;
tmp_cpus = READ_ONCE(cpus); *mask = NULL;
if (tmp_cpus > 0) *mask_sz = 0;
return tmp_cpus;
/* Each sub string separated by ',' has format \d+-\d+ or \d+ */
while (*s) {
if (*s == ',' || *s == '\n') {
s++;
continue;
}
n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
if (n <= 0 || n > 2) {
pr_warn("Failed to get CPU range %s: %d\n", s, n);
err = -EINVAL;
goto cleanup;
} else if (n == 1) {
end = start;
}
if (start < 0 || start > end) {
pr_warn("Invalid CPU range [%d,%d] in %s\n",
start, end, s);
err = -EINVAL;
goto cleanup;
}
tmp = realloc(*mask, end + 1);
if (!tmp) {
err = -ENOMEM;
goto cleanup;
}
*mask = tmp;
memset(tmp + *mask_sz, 0, start - *mask_sz);
memset(tmp + start, 1, end - start + 1);
*mask_sz = end + 1;
s += len;
}
if (!*mask_sz) {
pr_warn("Empty CPU range\n");
return -EINVAL;
}
return 0;
cleanup:
free(*mask);
*mask = NULL;
return err;
}
int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
{
int fd, err = 0, len;
char buf[128];
fd = open(fcpu, O_RDONLY); fd = open(fcpu, O_RDONLY);
if (fd < 0) { if (fd < 0) {
error = errno; err = -errno;
pr_warn("Failed to open file %s: %s\n", fcpu, strerror(error)); pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
return -error; return err;
} }
len = read(fd, buf, sizeof(buf)); len = read(fd, buf, sizeof(buf));
close(fd); close(fd);
if (len <= 0) { if (len <= 0) {
error = len ? errno : EINVAL; err = len ? -errno : -EINVAL;
pr_warn("Failed to read # of possible cpus from %s: %s\n", pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
fcpu, strerror(error)); return err;
return -error;
} }
if (len == sizeof(buf)) { if (len >= sizeof(buf)) {
pr_warn("File %s size overflow\n", fcpu); pr_warn("CPU mask is too big in file %s\n", fcpu);
return -EOVERFLOW; return -E2BIG;
} }
buf[len] = '\0'; buf[len] = '\0';
for (ir = 0, tmp_cpus = 0; ir <= len; ir++) { return parse_cpu_mask_str(buf, mask, mask_sz);
/* Each sub string separated by ',' has format \d+-\d+ or \d+ */ }
if (buf[ir] == ',' || buf[ir] == '\0') {
buf[ir] = '\0'; int libbpf_num_possible_cpus(void)
n = sscanf(&buf[il], "%u-%u", &start, &end); {
if (n <= 0) { static const char *fcpu = "/sys/devices/system/cpu/possible";
pr_warn("Failed to get # CPUs from %s\n", static int cpus;
&buf[il]); int err, n, i, tmp_cpus;
return -EINVAL; bool *mask;
} else if (n == 1) {
end = start; tmp_cpus = READ_ONCE(cpus);
} if (tmp_cpus > 0)
tmp_cpus += end - start + 1; return tmp_cpus;
il = ir + 1;
} err = parse_cpu_mask_file(fcpu, &mask, &n);
} if (err)
if (tmp_cpus <= 0) { return err;
pr_warn("Invalid #CPUs %d from %s\n", tmp_cpus, fcpu);
return -EINVAL; tmp_cpus = 0;
for (i = 0; i < n; i++) {
if (mask[i])
tmp_cpus++;
} }
free(mask);
WRITE_ONCE(cpus, tmp_cpus); WRITE_ONCE(cpus, tmp_cpus);
return tmp_cpus; return tmp_cpus;
......
...@@ -95,6 +95,8 @@ static inline bool libbpf_validate_opts(const char *opts, ...@@ -95,6 +95,8 @@ static inline bool libbpf_validate_opts(const char *opts,
#define OPTS_GET(opts, field, fallback_value) \ #define OPTS_GET(opts, field, fallback_value) \
(OPTS_HAS(opts, field) ? (opts)->field : fallback_value) (OPTS_HAS(opts, field) ? (opts)->field : fallback_value)
int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz);
int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
int libbpf__load_raw_btf(const char *raw_types, size_t types_len, int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
const char *str_sec, size_t str_len); const char *str_sec, size_t str_len);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment