Netdev List
 help / color / mirror / Atom feed
From: Takshak Chahande <ctakshak@fb.com>
To: <netdev@vger.kernel.org>
Cc: <ast@kernel.org>, <daniel@iogearbox.net>, <rdna@fb.com>,
	<ctakshak@fb.com>, <kernel-team@fb.com>, <hechaol@fb.com>
Subject: [PATCH bpf-next] libbpf : make libbpf_num_possible_cpus function thread safe
Date: Tue, 30 Jul 2019 15:24:47 -0700	[thread overview]
Message-ID: <20190730222447.3918919-1-ctakshak@fb.com> (raw)

Having static variable `cpus` in libbpf_num_possible_cpus function without
guarding it with mutex makes this function thread-unsafe.

If multiple threads accessing this function, in the current form; it
leads to incrementing the static variable value `cpus` in the multiple
of total available CPUs.

Let caching the number of possile CPUs handled by libbpf's users than
this library itself; and let this function be rock bottom one which reads
and parse the file (/sys/devices/system/cpu/possible) everytime it gets
called to simplify the things.

Fixes: 6446b3155521 (bpf: add a new API libbpf_num_possible_cpus())

Signed-off-by: Takshak Chahande <ctakshak@fb.com>
Acked-by: Andrey Ignatov <rdna@fb.com>
---
 tools/lib/bpf/libbpf.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ead915aec349..e7ac0e02287e 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4998,14 +4998,11 @@ int libbpf_num_possible_cpus(void)
 	static const char *fcpu = "/sys/devices/system/cpu/possible";
 	int len = 0, n = 0, il = 0, ir = 0;
 	unsigned int start = 0, end = 0;
-	static int cpus;
 	char buf[128];
 	int error = 0;
+	int cpus = 0;
 	int fd = -1;
 
-	if (cpus > 0)
-		return cpus;
-
 	fd = open(fcpu, O_RDONLY);
 	if (fd < 0) {
 		error = errno;
-- 
2.17.1


             reply	other threads:[~2019-07-30 22:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-30 22:24 Takshak Chahande [this message]
2019-07-30 23:05 ` [PATCH bpf-next] libbpf : make libbpf_num_possible_cpus function thread safe Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190730222447.3918919-1-ctakshak@fb.com \
    --to=ctakshak@fb.com \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=hechaol@fb.com \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=rdna@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox