From patchwork Thu Jan 31 09:19:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martynas Pumputis X-Patchwork-Id: 1034070 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=lambda.lt Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="fPiaVga/"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43qvn04zvCz9sDB for ; Thu, 31 Jan 2019 20:19:00 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728895AbfAaJS7 (ORCPT ); Thu, 31 Jan 2019 04:18:59 -0500 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:33201 "EHLO out1-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725895AbfAaJS6 (ORCPT ); Thu, 31 Jan 2019 04:18:58 -0500 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 0CCA321F5C; Thu, 31 Jan 2019 04:18:57 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 31 Jan 2019 04:18:57 -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=5Z8FC9KoHsqRPHQzj nkWF0ocDkc732EqL5X7yN6T5Jo=; b=fPiaVga/G+xPd/D1P86P7aucJK4gDjgIr tttTpyM6rg6jMMBqWRkzY4LH/OPT+ktrwwg/ip6+DRg0EA7283htAV9MkSnivcpk fXwCpLZwQAqvnrlEg0DjwTT26Edr4FXro/hH0LC2pYUF9RPBDYCSt1M/vGY72jSU v+qZ1Vcp4aVMN/SaVAscjr6Bxiap0BRCCFyk2zAMECGvL2pfPlOE8K8Yz+gLreYz u+Ar4cbcby5gk4f7e6pF+C91veXoI1ownMSe88oavm3iYab1xMM04zYCfVxA4G0E Z+MI99CsHhduiHz7QgP6lIAWnev6TinzdQIe3gmEtl1jxEJvfWR4w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrjeeigddtvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucenucfjughrpefhvffufffkofgggfestdekredtredttdenucfhrhhomhepofgrrh hthihnrghsucfruhhmphhuthhishcuoehmsehlrghmsggurgdrlhhtqeenucffohhmrghi nhepghhithhhuhgsrdgtohhmnecukfhppeduleegrddvfedtrdduheehrddujeejnecurf grrhgrmhepmhgrihhlfhhrohhmpehmsehlrghmsggurgdrlhhtnecuvehluhhsthgvrhfu ihiivgepud X-ME-Proxy: Received: from ceuse.localdomain (unknown [194.230.155.177]) by mail.messagingengine.com (Postfix) with ESMTPA id AD3AAE4536; Thu, 31 Jan 2019 04:18:54 -0500 (EST) From: Martynas Pumputis To: netdev@vger.kernel.org Cc: ys114321@gmail.com, ast@kernel.org, daniel@iogearbox.net, m@lambda.lt Subject: [PATCH v2 bpf] bpf: selftests: handle sparse CPU allocations Date: Thu, 31 Jan 2019 10:19:33 +0100 Message-Id: <20190131091933.27764-1-m@lambda.lt> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 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 Acked-by: Yonghong Song --- 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;