Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1283133 > unrolled thread

[PATCH] linux/log2.h: Fix roundup_pow_of_two(0)

Started bySasha Levin <sasha.levin@oracle.com>
First post2015-12-03 16:40 +0100
Last post2015-12-03 17:20 +0100
Articles 4 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] linux/log2.h: Fix roundup_pow_of_two(0) Sasha Levin <sasha.levin@oracle.com> - 2015-12-03 16:40 +0100
    Re: [PATCH] linux/log2.h: Fix roundup_pow_of_two(0) Andrey Ryabinin <ryabinin.a.a@gmail.com> - 2015-12-03 16:40 +0100
      Re: [PATCH] linux/log2.h: Fix roundup_pow_of_two(0) Sasha Levin <sasha.levin@oracle.com> - 2015-12-03 17:10 +0100
        Re: [PATCH] linux/log2.h: Fix roundup_pow_of_two(0) Andrey Ryabinin <ryabinin.a.a@gmail.com> - 2015-12-03 17:20 +0100

#1283133 — [PATCH] linux/log2.h: Fix roundup_pow_of_two(0)

FromSasha Levin <sasha.levin@oracle.com>
Date2015-12-03 16:40 +0100
Subject[PATCH] linux/log2.h: Fix roundup_pow_of_two(0)
Message-ID<qBE9c-6XG-27@gated-at.bofh.it>
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 <sasha.levin@oracle.com>
---
 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;
+
 	return 1UL << fls_long(n - 1);
 }
 
-- 
1.7.10.4

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

[toc] | [next] | [standalone]


#1283137

FromAndrey Ryabinin <ryabinin.a.a@gmail.com>
Date2015-12-03 16:40 +0100
Message-ID<qBE9d-6XG-49@gated-at.bofh.it>
In reply to#1283133
2015-12-03 18:30 GMT+03:00 Sasha Levin <sasha.levin@oracle.com>:
> 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 <sasha.levin@oracle.com>
> ---
>  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:

/**
 * roundup_pow_of_two - round the given value up to nearest power of two
 * @n - parameter
 *
 * round the given value up to the nearest power of two
 * - the result is undefined when n == 0
--
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/

[toc] | [prev] | [next] | [standalone]


#1283160

FromSasha Levin <sasha.levin@oracle.com>
Date2015-12-03 17:10 +0100
Message-ID<qBECe-7oz-23@gated-at.bofh.it>
In reply to#1283137
On 12/03/2015 10:39 AM, Andrey Ryabinin wrote:
> 2015-12-03 18:30 GMT+03:00 Sasha Levin <sasha.levin@oracle.com>:
>> > 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 <sasha.levin@oracle.com>
>> > ---
>> >  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/

[toc] | [prev] | [next] | [standalone]


#1283164

FromAndrey Ryabinin <ryabinin.a.a@gmail.com>
Date2015-12-03 17:20 +0100
Message-ID<qBELU-7sA-9@gated-at.bofh.it>
In reply to#1283160
2015-12-03 19:04 GMT+03:00 Sasha Levin <sasha.levin@oracle.com>:
> On 12/03/2015 10:39 AM, Andrey Ryabinin wrote:
>> 2015-12-03 18:30 GMT+03:00 Sasha Levin <sasha.levin@oracle.com>:
>>> > 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 <sasha.levin@oracle.com>
>>> > ---
>>> >  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.
>

In that case patch should update the comment.

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

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web