diff options
| author | Martynas Pumputis <m@lambda.lt> | 2019-01-31 04:19:33 -0500 |
|---|---|---|
| committer | Daniel Borkmann <daniel@iogearbox.net> | 2019-01-31 17:13:22 -0500 |
| commit | 1bb54c4071f585ebef56ce8fdfe6026fa2cbcddd (patch) | |
| tree | 7250dc1999a2829c27b5e721446c49cad6b759f3 /tools/testing | |
| parent | 9d90436ece8f2c5b4da617e679590137e54ce4cb (diff) | |
bpf, selftests: fix handling of sparse CPU allocations
Previously, bpf_num_possible_cpus() had a bug when calculating a
number of possible CPUs in the case of sparse CPU allocations, as
it was considering only the first range or element of
/sys/devices/system/cpu/possible.
E.g. in the case of "0,2-3" (CPU 1 is not available), the function
returned 1 instead of 3.
This patch fixes the function by making it parse all CPU ranges and
elements.
Signed-off-by: Martynas Pumputis <m@lambda.lt>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/testing')
| -rw-r--r-- | tools/testing/selftests/bpf/bpf_util.h | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h index 315a44fa32af..84fd6f1bf33e 100644 --- a/tools/testing/selftests/bpf/bpf_util.h +++ b/tools/testing/selftests/bpf/bpf_util.h | |||
| @@ -13,7 +13,7 @@ static inline unsigned int bpf_num_possible_cpus(void) | |||
| 13 | unsigned int start, end, possible_cpus = 0; | 13 | unsigned int start, end, possible_cpus = 0; |
| 14 | char buff[128]; | 14 | char buff[128]; |
| 15 | FILE *fp; | 15 | FILE *fp; |
| 16 | int n; | 16 | int len, n, i, j = 0; |
| 17 | 17 | ||
| 18 | fp = fopen(fcpu, "r"); | 18 | fp = fopen(fcpu, "r"); |
| 19 | if (!fp) { | 19 | if (!fp) { |
| @@ -21,17 +21,27 @@ static inline unsigned int bpf_num_possible_cpus(void) | |||
| 21 | exit(1); | 21 | exit(1); |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | while (fgets(buff, sizeof(buff), fp)) { | 24 | if (!fgets(buff, sizeof(buff), fp)) { |
| 25 | n = sscanf(buff, "%u-%u", &start, &end); | 25 | printf("Failed to read %s!\n", fcpu); |
| 26 | if (n == 0) { | 26 | exit(1); |
| 27 | printf("Failed to retrieve # possible CPUs!\n"); | 27 | } |
| 28 | exit(1); | 28 | |
| 29 | } else if (n == 1) { | 29 | len = strlen(buff); |
| 30 | end = start; | 30 | for (i = 0; i <= len; i++) { |
| 31 | if (buff[i] == ',' || buff[i] == '\0') { | ||
| 32 | buff[i] = '\0'; | ||
| 33 | n = sscanf(&buff[j], "%u-%u", &start, &end); | ||
| 34 | if (n <= 0) { | ||
| 35 | printf("Failed to retrieve # possible CPUs!\n"); | ||
| 36 | exit(1); | ||
| 37 | } else if (n == 1) { | ||
| 38 | end = start; | ||
| 39 | } | ||
| 40 | possible_cpus += end - start + 1; | ||
| 41 | j = i + 1; | ||
| 31 | } | 42 | } |
| 32 | possible_cpus = start == 0 ? end + 1 : 0; | ||
| 33 | break; | ||
| 34 | } | 43 | } |
| 44 | |||
| 35 | fclose(fp); | 45 | fclose(fp); |
| 36 | 46 | ||
| 37 | return possible_cpus; | 47 | return possible_cpus; |
