From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.0 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7D28C282D7 for ; Wed, 30 Jan 2019 09:18:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B52222083B for ; Wed, 30 Jan 2019 09:18:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="NutGzEcT" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730240AbfA3JSl (ORCPT ); Wed, 30 Jan 2019 04:18:41 -0500 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:50895 "EHLO out1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726427AbfA3JSj (ORCPT ); Wed, 30 Jan 2019 04:18:39 -0500 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id D987222647; Wed, 30 Jan 2019 04:18:37 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 30 Jan 2019 04:18:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=PS+6LohfgIR5zjDDo caEB/WIIp+2Xe4yKLVWa5JMaGU=; b=NutGzEcTqVlkxSbgiEJ74yU51P1mCxmoq Qft8ud+6L3vjl+YzgAZo4KOTU3jMxyXhr2XMhTcAyListt/t5T0cLAjul6Rd/MXY OVl0VwnvY/Tq9+GsD3X7E25OlJo+CrLjBjPghQH0FkvfYt226HnFaqCeHmxmymnh MsuLc+OY/aJ7BUYWn4oFa+xhh4vPgUD7nUl9PNjjONN5eOhan/5VbgYEe6hoQL/a 5Q9D83uv8ljaGF3j4PkaEgnRRDGHYFmlEnxGHzPAbtc11x8iUWT+/ZeV2kkhTIz/ gUJBGIDbsi5ZzeVEB0r8/ExJnd4lF8oH0OBhxjffF/Ym+Gcc1iWNw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrjeeggddtudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucenucfjughrpefhvffufffkofgggfestdekredtredttdenucfhrhhomhepofgrrh hthihnrghsucfruhhmphhuthhishcuoehmsehlrghmsggurgdrlhhtqeenucfkphepiedv rddvtdefrddvjedruddufeenucfrrghrrghmpehmrghilhhfrhhomhepmheslhgrmhgsug grrdhlthenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from ceuse.home (113.27.203.62.dynamic.wline.res.cust.swisscom.ch [62.203.27.113]) by mail.messagingengine.com (Postfix) with ESMTPA id 2B68B10319; Wed, 30 Jan 2019 04:18:36 -0500 (EST) From: Martynas Pumputis To: netdev@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, m@lambda.lt Subject: [PATCH] bpf: selftests: handle sparse CPU allocations Date: Wed, 30 Jan 2019 10:19:00 +0100 Message-Id: <20190130091900.4044-1-m@lambda.lt> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 --- tools/testing/selftests/bpf/bpf_util.h | 29 +++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h index 315a44fa32af..8cab50408204 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) unsigned int start, end, possible_cpus = 0; char buff[128]; FILE *fp; - int n; + int n, i, j = 0; fp = fopen(fcpu, "r"); if (!fp) { @@ -21,17 +21,26 @@ 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); + } + + for (i = 0; i <= strlen(buff); 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