]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/4.19.29/bpf-selftests-fix-handling-of-sparse-cpu-allocations.patch
fixes for 4.19
[thirdparty/kernel/stable-queue.git] / releases / 4.19.29 / bpf-selftests-fix-handling-of-sparse-cpu-allocations.patch
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
5
6 [ Upstream commit 1bb54c4071f585ebef56ce8fdfe6026fa2cbcddd ]
7
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.
12
13 E.g. in the case of "0,2-3" (CPU 1 is not available), the function
14 returned 1 instead of 3.
15
16 This patch fixes the function by making it parse all CPU ranges and
17 elements.
18
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>
23 ---
24 tools/testing/selftests/bpf/bpf_util.h | 30 +++++++++++++++++---------
25 1 file changed, 20 insertions(+), 10 deletions(-)
26
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;
33 char buff[128];
34 FILE *fp;
35 - int n;
36 + int len, n, i, j = 0;
37
38 fp = fopen(fcpu, "r");
39 if (!fp) {
40 @@ -21,17 +21,27 @@ static inline unsigned int bpf_num_possible_cpus(void)
41 exit(1);
42 }
43
44 - while (fgets(buff, sizeof(buff), fp)) {
45 - n = sscanf(buff, "%u-%u", &start, &end);
46 - if (n == 0) {
47 - printf("Failed to retrieve # possible CPUs!\n");
48 - exit(1);
49 - } else if (n == 1) {
50 - end = start;
51 + if (!fgets(buff, sizeof(buff), fp)) {
52 + printf("Failed to read %s!\n", fcpu);
53 + exit(1);
54 + }
55 +
56 + len = strlen(buff);
57 + for (i = 0; i <= len; i++) {
58 + if (buff[i] == ',' || buff[i] == '\0') {
59 + buff[i] = '\0';
60 + n = sscanf(&buff[j], "%u-%u", &start, &end);
61 + if (n <= 0) {
62 + printf("Failed to retrieve # possible CPUs!\n");
63 + exit(1);
64 + } else if (n == 1) {
65 + end = start;
66 + }
67 + possible_cpus += end - start + 1;
68 + j = i + 1;
69 }
70 - possible_cpus = start == 0 ? end + 1 : 0;
71 - break;
72 }
73 +
74 fclose(fp);
75
76 return possible_cpus;
77 --
78 2.19.1
79