Commit 56fbc241 authored by Takshak Chahande's avatar Takshak Chahande Committed by Alexei Starovoitov

libbpf : make libbpf_num_possible_cpus function thread safe

Having static variable `cpus` in libbpf_num_possible_cpus function
without guarding it with mutex makes this function thread-unsafe.

If multiple threads accessing this function, in the current form; it
leads to incrementing the static variable value `cpus` in the multiple
of total available CPUs.

Used local stack variable to calculate the number of possible CPUs and
then updated the static variable using WRITE_ONCE().

Changes since v1:
 * added stack variable to calculate cpus
 * serialized static variable update using WRITE_ONCE()
 * fixed Fixes tag

Fixes: 6446b315 ("bpf: add a new API libbpf_num_possible_cpus()")
Signed-off-by: default avatarTakshak Chahande <ctakshak@fb.com>
Acked-by: default avatarAndrey Ignatov <rdna@fb.com>
Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 5d01ab7b
...@@ -4995,13 +4995,15 @@ int libbpf_num_possible_cpus(void) ...@@ -4995,13 +4995,15 @@ int libbpf_num_possible_cpus(void)
static const char *fcpu = "/sys/devices/system/cpu/possible"; static const char *fcpu = "/sys/devices/system/cpu/possible";
int len = 0, n = 0, il = 0, ir = 0; int len = 0, n = 0, il = 0, ir = 0;
unsigned int start = 0, end = 0; unsigned int start = 0, end = 0;
int tmp_cpus = 0;
static int cpus; static int cpus;
char buf[128]; char buf[128];
int error = 0; int error = 0;
int fd = -1; int fd = -1;
if (cpus > 0) tmp_cpus = READ_ONCE(cpus);
return cpus; if (tmp_cpus > 0)
return tmp_cpus;
fd = open(fcpu, O_RDONLY); fd = open(fcpu, O_RDONLY);
if (fd < 0) { if (fd < 0) {
...@@ -5024,7 +5026,7 @@ int libbpf_num_possible_cpus(void) ...@@ -5024,7 +5026,7 @@ int libbpf_num_possible_cpus(void)
} }
buf[len] = '\0'; buf[len] = '\0';
for (ir = 0, cpus = 0; ir <= len; ir++) { for (ir = 0, tmp_cpus = 0; ir <= len; ir++) {
/* Each sub string separated by ',' has format \d+-\d+ or \d+ */ /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
if (buf[ir] == ',' || buf[ir] == '\0') { if (buf[ir] == ',' || buf[ir] == '\0') {
buf[ir] = '\0'; buf[ir] = '\0';
...@@ -5036,13 +5038,15 @@ int libbpf_num_possible_cpus(void) ...@@ -5036,13 +5038,15 @@ int libbpf_num_possible_cpus(void)
} else if (n == 1) { } else if (n == 1) {
end = start; end = start;
} }
cpus += end - start + 1; tmp_cpus += end - start + 1;
il = ir + 1; il = ir + 1;
} }
} }
if (cpus <= 0) { if (tmp_cpus <= 0) {
pr_warning("Invalid #CPUs %d from %s\n", cpus, fcpu); pr_warning("Invalid #CPUs %d from %s\n", tmp_cpus, fcpu);
return -EINVAL; return -EINVAL;
} }
return cpus;
WRITE_ONCE(cpus, tmp_cpus);
return tmp_cpus;
} }
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