OSDN Git Service

libbpf : make libbpf_num_possible_cpus function thread safe
authorTakshak Chahande <ctakshak@fb.com>
Wed, 31 Jul 2019 22:10:55 +0000 (15:10 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 1 Aug 2019 03:35:19 +0000 (20:35 -0700)
commit56fbc24116f458a0ea48f9f37fe770fd791042d9
tree17d137a6c297c88c4ad99dddf76b382ab9db9b42
parent5d01ab7bac467edfc530e6ccf953921def935c62
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: 6446b3155521 ("bpf: add a new API libbpf_num_possible_cpus()")
Signed-off-by: Takshak Chahande <ctakshak@fb.com>
Acked-by: Andrey Ignatov <rdna@fb.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/libbpf.c