1 From 8fb8de9f1fd5b7356b445c3aeb01061b999b9127 Mon Sep 17 00:00:00 2001
2 From: Martynas Pumputis <m@lambda.lt>
3 Date: Thu, 31 Jan 2019 10:19:33 +0100
4 Subject: bpf, selftests: fix handling of sparse CPU allocations
6 [ Upstream commit 1bb54c4071f585ebef56ce8fdfe6026fa2cbcddd ]
8 Previously, bpf_num_possible_cpus() had a bug when calculating a
9 number of possible CPUs in the case of sparse CPU allocations, as
10 it was considering only the first range or element of
11 /sys/devices/system/cpu/possible.
13 E.g. in the case of "0,2-3" (CPU 1 is not available), the function
14 returned 1 instead of 3.
16 This patch fixes the function by making it parse all CPU ranges and
19 Signed-off-by: Martynas Pumputis <m@lambda.lt>
20 Acked-by: Yonghong Song <yhs@fb.com>
21 Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
22 Signed-off-by: Sasha Levin <sashal@kernel.org>
24 tools/testing/selftests/bpf/bpf_util.h | 30 +++++++++++++++++---------
25 1 file changed, 20 insertions(+), 10 deletions(-)
27 diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
28 index 315a44fa32af..84fd6f1bf33e 100644
29 --- a/tools/testing/selftests/bpf/bpf_util.h
30 +++ b/tools/testing/selftests/bpf/bpf_util.h
31 @@ -13,7 +13,7 @@ static inline unsigned int bpf_num_possible_cpus(void)
32 unsigned int start, end, possible_cpus = 0;
36 + int len, n, i, j = 0;
38 fp = fopen(fcpu, "r");
40 @@ -21,17 +21,27 @@ static inline unsigned int bpf_num_possible_cpus(void)
44 - while (fgets(buff, sizeof(buff), fp)) {
45 - n = sscanf(buff, "%u-%u", &start, &end);
47 - printf("Failed to retrieve # possible CPUs!\n");
49 - } else if (n == 1) {
51 + if (!fgets(buff, sizeof(buff), fp)) {
52 + printf("Failed to read %s!\n", fcpu);
57 + for (i = 0; i <= len; i++) {
58 + if (buff[i] == ',' || buff[i] == '\0') {
60 + n = sscanf(&buff[j], "%u-%u", &start, &end);
62 + printf("Failed to retrieve # possible CPUs!\n");
64 + } else if (n == 1) {
67 + possible_cpus += end - start + 1;
70 - possible_cpus = start == 0 ? end + 1 : 0;