perf tools: Pedantic fixes for mmap_pages
Adrian Hunter
reviewercover perf tools: Pedantic fixes for mmap_pages
Hi
Here are some pedantic fixes for mmap_pages.
Adrian Hunter (4):
perf tools: Remove unnecessary parentheses perf tools: Fix max mmap_pages
perf tools: Fix mmap pages rounding to power of 2 perf tools: Fix display of incorrect mmap pages
tools/perf/builtin-record.c | 2 +- tools/perf/util/evlist.c | 10 ++++++---- tools/perf/util/util.h | 12 ++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-)
Regards
Adrian
Adrian Hunter
reviewerPATCH 1/4 perf tools: Remove unnecessary parentheses
Remove unnecessary parentheses.
--- tools/perf/util/evlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/evlist.c +1 -1
@@ -732,7 +732,7 @@ static long parse_pages_arg(const char *str, unsigned long min,return -EINVAL ; } -if ((pages ==0 ) && (min ==0 )) { +if (pages ==0 && min ==0 ) {/* leave number of pages at 0 */ }else if (pages < (1UL <<31 ) && !is_power_of_2 (pages)) {/* round pages up to next power of 2 */
Signature · 1 line
1.7.11.7
tip-bot for Adrian Hunter
merge bot[tip:perf/core] perf evlist: Remove unnecessary parentheses
perf evlist: Remove unnecessary parentheses
Commit-ID: 2bcab6c146fde0d0286b132068111f98b6217460 Gitweb: http://git.kernel.org/tip/2bcab6c146fde0d0286b132068111f98b6217460 Author: Adrian Hunter <[email protected]> AuthorDate: Mon, 9 Dec 2013 15:18:37 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 9 Dec 2013 15:17:31 -0300
perf evlist: Remove unnecessary parentheses
--- tools/perf/util/evlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/evlist.c +1 -1
@@ -732,7 +732,7 @@ static long parse_pages_arg(const char *str, unsigned long min,return -EINVAL ; } -if ((pages ==0 ) && (min ==0 )) { +if (pages ==0 && min ==0 ) {/* leave number of pages at 0 */ }else if (pages < (1UL <<31 ) && !is_power_of_2 (pages)) {/* round pages up to next power of 2 */
Adrian Hunter
reviewerPATCH 2/4 perf tools: Fix max mmap_pages
In reply to Adrian Hunter'SIZE_MAX / page_size' is an upper limit for
the maximum number of mmap pages, not a lower
limit. Change the condition accordingly.
--- tools/perf/util/evlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/evlist.c +1 -1
@@ -754,7 +754,7 @@ int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,unsigned long max =UINT_MAX ;long pages; -if (max <SIZE_MAX / page_size) +if (max >SIZE_MAX / page_size) max =SIZE_MAX / page_size; pages =parse_pages_arg (str,1 , max);
Signature · 1 line
1.7.11.7
Re: [PATCH 2/4] perf tools: Fix max mmap_pages
Quoted 14 lines · Adrian Hunter
'
SIZE_MAX/page_size' is an upper limit for
the maximum number of mmap pages, not a lower
limit. Change the condition accordingly.Signed-off-by Adrian Hunter <[email protected]>--- tools/perf/util/evlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/evlist.c +0
-0
tools/perf/util/evlist.c int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, · line 758
Quoted 6 lines
tools/perf/util/evlist.c +1
-1@@ -754,7 +754,7 @@ int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,unsigned long max =UINT_MAX ;long pages; -if (max <SIZE_MAX / page_size) +if (max >SIZE_MAX / page_size) max =SIZE_MAX / page_size;
I agree with the change.. but it looks like the check is superfluos, maybe we want to define max as:
unsigned long max = SIZE_MAX / page_size
?
jirka
Quoted 5 lines
tools/perf/util/evlist.c +0
-0pages =parse_pages_arg (str,1 , max);Signature · 1 line
1.7.11.7
Adrian Hunter
reviewerRe: [PATCH 2/4] perf tools: Fix max mmap_pages
Quoted 18 lines · Jiri Olsa
Quoted 17 lines · Adrian Hunter
'
SIZE_MAX/page_size' is an upper limit for
the maximum number of mmap pages, not a lower
limit. Change the condition accordingly.Signed-off-by Adrian Hunter <[email protected]>--- tools/perf/util/evlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/evlist.c +0
-0@@ -754,7 +754,7 @@ int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,unsigned long max =UINT_MAX ;long pages;
Quoted 8 lines
tools/perf/util/evlist.c int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, · line 758
Quoted 3 lines
tools/perf/util/evlist.c +1
-1-if (max <SIZE_MAX / page_size) +if (max >SIZE_MAX / page_size) max =SIZE_MAX / page_size;I agree with the change.. but it looks like the check is superfluos, maybe we want to define max as:
unsigned long max =
SIZE_MAX/page_size
I think it is OK because mmap_pages (in struct perf_record_opts) is an unsigned int i.e.
max = MIN(UINT_MAX, SIZE_MAX / 4096)
c.f.
32-bit 64-bit
UINT_MAX 2^32 - 1 2^32 - 1SIZE_MAX 2^32 - 1 2^64 - 1SIZE_MAX / 4096 2^20 - 1 2^52 - 1
So on 32-bit the max is SIZE_MAX / 4096 but on
64-bit the max is UINT_MAX
Quoted 11 lines
?
jirka
Quoted 5 lines
tools/perf/util/evlist.c +0
-0pages =parse_pages_arg (str,1 , max);Signature · 1 line
1.7.11.7
tip-bot for Adrian Hunter
merge bot[tip:perf/core] perf evlist: Fix max mmap_pages
In reply to Adrian Hunterperf evlist: Fix max mmap_pages
Commit-ID: f5ae9c424e4c80c44e9ab7e5ff4a6b79490c23be Gitweb: http://git.kernel.org/tip/f5ae9c424e4c80c44e9ab7e5ff4a6b79490c23be Author: Adrian Hunter <[email protected]> AuthorDate: Mon, 9 Dec 2013 15:18:38 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 9 Dec 2013 15:17:38 -0300
perf evlist: Fix max mmap_pages
'SIZE_MAX / page_size' is an upper limit for the maximum number of mmap pages, not a lower limit. Change the condition accordingly.
--- tools/perf/util/evlist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/evlist.c +1 -1
@@ -754,7 +754,7 @@ int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,unsigned long max =UINT_MAX ;long pages; -if (max <SIZE_MAX / page_size) +if (max >SIZE_MAX / page_size) max =SIZE_MAX / page_size; pages =parse_pages_arg (str,1 , max);
Adrian Hunter
reviewerPATCH 3/4 perf tools: Fix mmap pages rounding to power of 2
In reply to Adrian Hunter'next_pow2()' only works for 'unsigned int' but the argument is 'unsigned long'. Checking for values less than (1 << 31) ensures that 'next_pow2()' is not passed a value out of range but lets anything else go through unvalidated.
As a result mmap_pages of zero is used e.g.
perf record -v -m2147483649 uname
mmap size 0B
failed to mmap with 22 (Invalid argument)
Fixed:
perf record -m2147483649 uname
rounding mmap pages size to 17592186044416 bytes (4294967296 pages) Invalid argument for --mmap_pages/-m
--- tools/perf/util/evlist.c | 6 ++++-- tools/perf/util/util.h | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-)
tools/perf/util/evlist.c +4 -2
@@ -734,9 +734,11 @@ static long parse_pages_arg(const char *str, unsigned long min,if (pages ==0 && min ==0 ) {/* leave number of pages at 0 */ - }else if (pages < (1UL <<31 ) && !is_power_of_2 (pages)) { + }else if (!is_power_of_2 (pages)) {/* round pages up to next power of 2 */ - pages =next_pow2 (pages); + pages =next_pow2_l (pages); +if (!pages) +return -EINVAL ;pr_info ("rounding mmap pages size to %lu bytes (%lu pages)\n" , pages * page_size, pages); }
tools/perf/util/util.h +12 -0
@@ -73,6 +73,7 @@#include <sys/ttydefaults.h> #include <lk/debugfs.h> #include <termios.h> +#include <linux/bitops.h> extern const char *graph_line;extern const char *graph_dotted_line;
@@ -281,6 +282,17 @@ static inline unsigned next_pow2(unsigned x)return 1ULL << (32 -__builtin_clz (x -1 )); } +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 +} +size_t hex_width (u64 v);int hex2u64 (const char *ptr,u64 *val);
Signature · 1 line
1.7.11.7
tip-bot for Adrian Hunter
merge bot[tip:perf/core] perf evlist: Fix mmap pages rounding to power of 2
perf evlist: Fix mmap pages rounding to power of 2
Commit-ID: 1dbfa9387b397f2c4b8c65411b3e3fdf9284d2b1 Gitweb: http://git.kernel.org/tip/1dbfa9387b397f2c4b8c65411b3e3fdf9284d2b1 Author: Adrian Hunter <[email protected]> AuthorDate: Mon, 9 Dec 2013 15:18:39 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 9 Dec 2013 15:18:05 -0300
perf evlist: Fix mmap pages rounding to power of 2
'next_pow2()' only works for 'unsigned int' but the argument is 'unsigned long'. Checking for values less than (1 << 31) ensures that 'next_pow2()' is not passed a value out of range but lets anything else go through unvalidated.
As a result mmap_pages of zero is used e.g.
perf record -v -m2147483649 uname
mmap size 0B
failed to mmap with 22 (Invalid argument)
Fixed:
perf record -m2147483649 uname
rounding mmap pages size to 17592186044416 bytes (4294967296 pages) Invalid argument for --mmap_pages/-m
--- tools/perf/util/evlist.c | 6 ++++-- tools/perf/util/util.h | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-)
tools/perf/util/evlist.c +4 -2
@@ -734,9 +734,11 @@ static long parse_pages_arg(const char *str, unsigned long min,if (pages ==0 && min ==0 ) {/* leave number of pages at 0 */ - }else if (pages < (1UL <<31 ) && !is_power_of_2 (pages)) { + }else if (!is_power_of_2 (pages)) {/* round pages up to next power of 2 */ - pages =next_pow2 (pages); + pages =next_pow2_l (pages); +if (!pages) +return -EINVAL ;pr_info ("rounding mmap pages size to %lu bytes (%lu pages)\n" , pages * page_size, pages); }
tools/perf/util/util.h +12 -0
@@ -73,6 +73,7 @@#include <sys/ttydefaults.h> #include <lk/debugfs.h> #include <termios.h> +#include <linux/bitops.h> extern const char *graph_line;extern const char *graph_dotted_line;
@@ -281,6 +282,17 @@ static inline unsigned next_pow2(unsigned x)return 1ULL << (32 -__builtin_clz (x -1 )); } +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 +} +size_t hex_width (u64 v);int hex2u64 (const char *ptr,u64 *val);
Adrian Hunter
reviewerPATCH 4/4 perf tools: Fix display of incorrect mmap pages
In reply to Adrian Hunter'mmap_pages' is 'unsigned int' not 'int' e.g.
perf record -m2147483648 uname
Permission error mapping pages.
Consider increasing /proc/sys/kernel/perf_event_mlock_kb, or try again with a smaller value of -m/--mmap_pages. (current value: -2147483648)
Fixed:
perf record -m2147483648 uname
Permission error mapping pages.
Consider increasing /proc/sys/kernel/perf_event_mlock_kb, or try again with a smaller value of -m/--mmap_pages. (current value: 2147483648)
--- tools/perf/builtin-record.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-record.c +1 -1
@@ -224,7 +224,7 @@ try_again:"Consider increasing " "/proc/sys/kernel/perf_event_mlock_kb,\n" "or try again with a smaller value of -m/--mmap_pages.\n" - "(current value: %d)\n", opts->mmap_pages); + "(current value: %u)\n", opts->mmap_pages); rc = -errno; }else {pr_err ("failed to mmap with %d (%s)\n" , errno,strerror (errno));
Signature · 1 line
1.7.11.7
tip-bot for Adrian Hunter
merge bot[tip:perf/core] perf record: Fix display of incorrect mmap pages
perf record: Fix display of incorrect mmap pages
Commit-ID: 53653d70a0784a997748cc9e315ddf19d310e812 Gitweb: http://git.kernel.org/tip/53653d70a0784a997748cc9e315ddf19d310e812 Author: Adrian Hunter <[email protected]> AuthorDate: Mon, 9 Dec 2013 15:18:40 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 9 Dec 2013 15:17:23 -0300
perf record: Fix display of incorrect mmap pages
'mmap_pages' is 'unsigned int' not 'int' e.g.
perf record -m2147483648 uname
Permission error mapping pages.
Consider increasing /proc/sys/kernel/perf_event_mlock_kb, or try again with a smaller value of -m/--mmap_pages. (current value: -2147483648)
Fixed:
perf record -m2147483648 uname
Permission error mapping pages.
Consider increasing /proc/sys/kernel/perf_event_mlock_kb, or try again with a smaller value of -m/--mmap_pages. (current value: 2147483648)
--- tools/perf/builtin-record.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-record.c +1 -1
@@ -224,7 +224,7 @@ try_again:"Consider increasing " "/proc/sys/kernel/perf_event_mlock_kb,\n" "or try again with a smaller value of -m/--mmap_pages.\n" - "(current value: %d)\n", opts->mmap_pages); + "(current value: %u)\n", opts->mmap_pages); rc = -errno; }else {pr_err ("failed to mmap with %d (%s)\n" , errno,strerror (errno));
Quoted 11 lines · Adrian Hunter
Hi
Here are some pedantic fixes for
mmap_pages.Adrian Hunter (4):
perf tools: Remove unnecessary parentheses perf tools: Fix maxmmap_pages
perf tools: Fix mmap pages rounding to power of 2 perf tools: Fix display of incorrect mmap pages
Quoted 4 lines
tools/perf/builtin-record.c | 2 +- tools/perf/util/evlist.c | 10 ++++++---- tools/perf/util/util.h | 12 ++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-)
thanks,
jirka