lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:	Fri, 13 Dec 2013 10:53:35 +0200
From:	Adrian Hunter <adrian.hunter@...el.com>
To:	Arnaldo Carvalho de Melo <acme@...stprotocols.net>
Cc:	Peter Zijlstra <a.p.zijlstra@...llo.nl>,
	Ingo Molnar <mingo@...hat.com>, linux-kernel@...r.kernel.org,
	David Ahern <dsahern@...il.com>,
	Frederic Weisbecker <fweisbec@...il.com>,
	Jiri Olsa <jolsa@...hat.com>, Mike Galbraith <efault@....de>,
	Namhyung Kim <namhyung@...il.com>,
	Paul Mackerras <paulus@...ba.org>,
	Stephane Eranian <eranian@...gle.com>
Subject: [PATCH 1/2] perf tools: Fix next_pow2_l()

My implementation of next_pow2_l() was incorrect.
e.g.

  perf record -m4296015872 uname
  rounding mmap pages size to 17592186044416 bytes (4294967296 pages)
  Invalid argument for --mmap_pages/-m

Notice that the next power-of-2 value 4294967296 is
less than the option value 4296015872.

Change to using  __builtin_clzl() and prevent the
shift being equal to the width of the operand.
Also __builtin_clzl(x) is undefined if x is 0,
so adjust the condition to preclude that
possibility.  Now:

  perf record -m4296015872 uname
  rounding mmap pages size to 35184372088832 bytes (8589934592 pages)
  Invalid argument for --mmap_pages/-m

Signed-off-by: Adrian Hunter <adrian.hunter@...el.com>
---
 tools/perf/util/util.h | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index a1eea3e..ae609fe 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -284,13 +284,14 @@ static inline unsigned next_pow2(unsigned x)
 
 static inline unsigned long next_pow2_l(unsigned long x)
 {
-#if BITS_PER_LONG == 64
-	if (x <= (1UL << 31))
-		return next_pow2(x);
-	return (unsigned long)next_pow2(x >> 32) << 32;
-#else
-	return next_pow2(x);
-#endif
+	int leading_zeros;
+
+	if (x < 2)
+		return 1;
+	leading_zeros = __builtin_clzl(x - 1);
+	if (!leading_zeros)
+		return 0;
+	return 1UL << (BITS_PER_LONG - leading_zeros);
 }
 
 size_t hex_width(u64 v);
-- 
1.7.11.7

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ