All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next] libbpf : make libbpf_num_possible_cpus function thread safe
@ 2019-07-30 22:24 Takshak Chahande
  2019-07-30 23:05 ` Jakub Kicinski
  0 siblings, 1 reply; 2+ messages in thread
From: Takshak Chahande @ 2019-07-30 22:24 UTC (permalink / raw)
  To: netdev; +Cc: ast, daniel, rdna, ctakshak, kernel-team, hechaol

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


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

* Re: [PATCH bpf-next] libbpf : make libbpf_num_possible_cpus function thread safe
  2019-07-30 22:24 [PATCH bpf-next] libbpf : make libbpf_num_possible_cpus function thread safe Takshak Chahande
@ 2019-07-30 23:05 ` Jakub Kicinski
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Kicinski @ 2019-07-30 23:05 UTC (permalink / raw)
  To: Takshak Chahande; +Cc: netdev, ast, daniel, rdna, kernel-team, hechaol

On Tue, 30 Jul 2019 15:24:47 -0700, Takshak Chahande wrote:
> 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; 

Can we just use stack variable for the calculations and
READ_ONCE()/WRITE_ONCE() for assignment to the static?  
libbpf itself uses this helper so caller caching wouldn't
work there.

> 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.

I don't understand can you rephrase?

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

No new line after the fixes tag, also I think you're missing quotation
marks around the commit title?

> Signed-off-by: Takshak Chahande <ctakshak@fb.com>
> Acked-by: Andrey Ignatov <rdna@fb.com>

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

end of thread, other threads:[~2019-07-30 23:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-30 22:24 [PATCH bpf-next] libbpf : make libbpf_num_possible_cpus function thread safe Takshak Chahande
2019-07-30 23:05 ` Jakub Kicinski

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.