Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758208AbbLCQE6 (ORCPT ); Thu, 3 Dec 2015 11:04:58 -0500 Received: from aserp1040.oracle.com ([141.146.126.69]:20009 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750844AbbLCQE5 (ORCPT ); Thu, 3 Dec 2015 11:04:57 -0500 Subject: Re: [PATCH] linux/log2.h: Fix roundup_pow_of_two(0) To: Andrey Ryabinin References: <1449156616-11474-1-git-send-email-sasha.levin@oracle.com> Cc: Linus Torvalds , LKML From: Sasha Levin Message-ID: <56606825.4000105@oracle.com> Date: Thu, 3 Dec 2015 11:04:53 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Source-IP: aserv0022.oracle.com [141.146.126.234] Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1508 Lines: 40 On 12/03/2015 10:39 AM, Andrey Ryabinin wrote: > 2015-12-03 18:30 GMT+03:00 Sasha Levin : >> > Passing 0 to roundup_pow_of_two would lead to wrapping around and trying to >> > find the last set bit on (unsigned long)(-1), which is obviously wrong. >> > >> > Instead, deal with this case by rounding it up to the closest power of two >> > (2 ** 0). >> > >> > Signed-off-by: Sasha Levin >> > --- >> > include/linux/log2.h | 3 +++ >> > 1 file changed, 3 insertions(+) >> > >> > diff --git a/include/linux/log2.h b/include/linux/log2.h >> > index fd7ff3d..b6bdf0c 100644 >> > --- a/include/linux/log2.h >> > +++ b/include/linux/log2.h >> > @@ -60,6 +60,9 @@ bool is_power_of_2(unsigned long n) >> > static inline __attribute__((const)) >> > unsigned long __roundup_pow_of_two(unsigned long n) >> > { >> > + if (n == 0) >> > + return 1UL << 0; >> > + > Perhaps we should fix callers instead? > Comment near roundup_pow_of_two() says that result is undefined when n == 0: That's how I've started doing it, but when it showed up with 3 different callers I figured it's better to fix it at the source. This fix would return a valid value and is working fine with the callers. Thanks, Sasha -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/