All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 bpf] bpf: selftests: handle sparse CPU allocations
@ 2019-01-31  9:19 Martynas Pumputis
  2019-01-31 16:48 ` Y Song
  2019-01-31 22:26 ` Daniel Borkmann
  0 siblings, 2 replies; 3+ messages in thread
From: Martynas Pumputis @ 2019-01-31  9:19 UTC (permalink / raw)
  To: netdev; +Cc: ys114321, ast, daniel, m

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>

---
Testing of the patch: https://gist.github.com/brb/5369b5cfd08babb80cf2c4081dc19762
---
 tools/testing/selftests/bpf/bpf_util.h | 31 +++++++++++++++++---------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
index 315a44fa32af..442c3ab2d688 100644
--- a/tools/testing/selftests/bpf/bpf_util.h
+++ b/tools/testing/selftests/bpf/bpf_util.h
@@ -9,11 +9,12 @@
 
 static inline unsigned int bpf_num_possible_cpus(void)
 {
+
 	static const char *fcpu = "/sys/devices/system/cpu/possible";
 	unsigned int start, end, possible_cpus = 0;
 	char buff[128];
 	FILE *fp;
-	int n;
+	int len, n, i, j = 0;
 
 	fp = fopen(fcpu, "r");
 	if (!fp) {
@@ -21,17 +22,27 @@ static inline unsigned int bpf_num_possible_cpus(void)
 		exit(1);
 	}
 
-	while (fgets(buff, sizeof(buff), fp)) {
-		n = sscanf(buff, "%u-%u", &start, &end);
-		if (n == 0) {
-			printf("Failed to retrieve # possible CPUs!\n");
-			exit(1);
-		} else if (n == 1) {
-			end = start;
+	if (!fgets(buff, sizeof(buff), fp)) {
+		printf("Failed to read %s!\n", fcpu);
+		exit(1);
+	}
+
+	len = strlen(buff);
+	for (i = 0; i <= len; i++) {
+		if (buff[i] == ',' || buff[i] == '\0') {
+			buff[i] = '\0';
+			n = sscanf(&buff[j], "%u-%u", &start, &end);
+			if (n <= 0) {
+				printf("Failed to retrieve # possible CPUs!\n");
+				exit(1);
+			} else if (n == 1) {
+				end = start;
+			}
+			possible_cpus += end - start + 1;
+			j = i + 1;
 		}
-		possible_cpus = start == 0 ? end + 1 : 0;
-		break;
 	}
+
 	fclose(fp);
 
 	return possible_cpus;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 bpf] bpf: selftests: handle sparse CPU allocations
  2019-01-31  9:19 [PATCH v2 bpf] bpf: selftests: handle sparse CPU allocations Martynas Pumputis
@ 2019-01-31 16:48 ` Y Song
  2019-01-31 22:26 ` Daniel Borkmann
  1 sibling, 0 replies; 3+ messages in thread
From: Y Song @ 2019-01-31 16:48 UTC (permalink / raw)
  To: Martynas Pumputis; +Cc: netdev, Alexei Starovoitov, Daniel Borkmann

On Thu, Jan 31, 2019 at 1:18 AM Martynas Pumputis <m@lambda.lt> wrote:
>
> 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>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 bpf] bpf: selftests: handle sparse CPU allocations
  2019-01-31  9:19 [PATCH v2 bpf] bpf: selftests: handle sparse CPU allocations Martynas Pumputis
  2019-01-31 16:48 ` Y Song
@ 2019-01-31 22:26 ` Daniel Borkmann
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Borkmann @ 2019-01-31 22:26 UTC (permalink / raw)
  To: Martynas Pumputis, netdev; +Cc: ys114321, ast

On 01/31/2019 10:19 AM, Martynas Pumputis wrote:
> 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>
> 
> ---
> Testing of the patch: https://gist.github.com/brb/5369b5cfd08babb80cf2c4081dc19762
> ---
>  tools/testing/selftests/bpf/bpf_util.h | 31 +++++++++++++++++---------
>  1 file changed, 21 insertions(+), 10 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
> index 315a44fa32af..442c3ab2d688 100644
> --- a/tools/testing/selftests/bpf/bpf_util.h
> +++ b/tools/testing/selftests/bpf/bpf_util.h
> @@ -9,11 +9,12 @@
>  
>  static inline unsigned int bpf_num_possible_cpus(void)
>  {
> +
>  	static const char *fcpu = "/sys/devices/system/cpu/possible";

Applied and removed above whitespace while at it, thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-01-31 22:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-31  9:19 [PATCH v2 bpf] bpf: selftests: handle sparse CPU allocations Martynas Pumputis
2019-01-31 16:48 ` Y Song
2019-01-31 22:26 ` Daniel Borkmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.