From 8f2d77c9d3995324df30ebe1a7db71004a27523e Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Mon, 18 May 2026 16:07:48 +0200 Subject: [PATCH] cpumf/pai: Improve -m XXX argument verification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Speed up the check of the option -m argument and improve the error message. The argument number must be a power of 2 number and this check is improved. Furthermore split the error message and provide one message for invalid characters and one error message for the argument not being a power of 2 number. Signed-off-by: Thomas Richter Suggested-by: Eduard Stefes Suggested-by: Juergen Christ Tested-by: Jan Polensky Reviewed-by: Jan Polensky Reviewed-by: Juergen Christ Signed-off-by: Jan Höppner --- cpumf/pai.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/cpumf/pai.c b/cpumf/pai.c index b8da83e7..34ae4a4c 100644 --- a/cpumf/pai.c +++ b/cpumf/pai.c @@ -962,19 +962,13 @@ static void record_cpus_nnpa(const char *cp) parse_cpulist(S390_EVT_PAI_NNPA, cp); } -/* Mapsize must be power of 2 and larger than 4. Count bits in n and - * return 0 if input is invalid and has a bit count larger than one. +/* Mapsize must be power of 2 and larger than 4. Return true in this case. */ -static unsigned long check_mapsize(unsigned long n) +static bool check_mapsize(unsigned long n) { - int bit, cnt = 0; - if (n < 4) return 0; - for (bit = 0; bit < __BITS_PER_LONG; ++bit) - if (n & (1 << bit)) - ++cnt; - return cnt == 1 ? n : 0; + return (n & (n - 1)) == 0; } static void setprio(const char *prio) @@ -1040,11 +1034,11 @@ int main(int argc, char **argv) errx(EXIT_FAILURE, "Invalid argument for -%c", ch); break; case 'm': - errno = 0; mapsize = strtoul(optarg, &slash, 0); - mapsize = check_mapsize(mapsize); - if (errno || !mapsize || *slash) + if (!mapsize || *slash) errx(EXIT_FAILURE, "Invalid argument for -%c", ch); + if (!check_mapsize(mapsize)) + errx(EXIT_FAILURE, "No power of 2 number for -%c", ch); break; case 'n': record_cpus_nnpa(optarg);