From: Vasily Gorbik <gor@linux.ibm.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Sebastian Reichel <sebastian.reichel@collabora.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Michael Turquette <mturquette@baylibre.com>,
Stephen Boyd <sboyd@kernel.org>,
David Laight <David.Laight@aculab.com>,
linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org,
Niklas Schnelle <schnelle@linux.ibm.com>,
kernel@collabora.com
Subject: Re: [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW
Date: Wed, 25 Oct 2023 17:05:13 +0200 [thread overview]
Message-ID: <your-ad-here.call-01698246313-ext-3263@work.hours> (raw)
In-Reply-To: <CAHk-=wjO5ivM6k7iMiThO9JfxH0dhLe=mcC4TQwReU0nBCnWpg@mail.gmail.com>
On Tue, Oct 24, 2023 at 12:53:03PM -1000, Linus Torvalds wrote:
>+/*
>+ * The other cases need to be split by type.
>+ *
>+ * Signed cases seem badly defined, but do exist. We should
>+ * consider removing them from this _Generic(), and fixing any
>+ * result 'not compatible with any association' cases.
>+ */
>+#define __div_round_up(n,d) _Generic((n)+(d), \
>+ unsigned long long: __div_round_up_ull(n,d), \
>+ long long: __div_round_up_ll(n,d), \
>+ unsigned long: __div_round_up_ul(n,d), \
>+ long: __div_round_up_l(n,d), \
>+ unsigned int: __div_round_up_u(n,d), \
>+ int: __div_round_up_i(n,d))
You probably want
#define __div_round_up(n,d) _Generic((n)+(d), \
unsigned long long: __div_round_up_ull, \
long long: __div_round_up_ll, \
unsigned long: __div_round_up_ul, \
long: __div_round_up_l, \
unsigned int: __div_round_up_u, \
int: __div_round_up_i)(n,d)
to avoid early type-checking for expressions that will be discarded
and prevent errors like:
drivers/net/ethernet/microchip/sparx5/sparx5_tc_flower.c: In function 'sparx5_tc_flower_parse_act_police':
drivers/net/ethernet/microchip/sparx5/sparx5_main.h:435:34: error: conversion from 'long long unsigned int' to 'long unsigned int' changes value from '25000000000' to '3525163520' [-Werror=overflow]
435 | #define SPX5_SDLB_GROUP_RATE_MAX 25000000000ULL
| ^~~~~~~~~~~~~~
./include/linux/div_round_up.h:68:42: note: in definition of macro '__div_round_up'
68 | unsigned long: __div_round_up_ul(n,d), \
| ^
./include/linux/div_round_up.h:50:9: note: in expansion of macro '__keep_const'
50 | __keep_const(n, div_round_up(n,d))
| ^~~~~~~~~~~~
./include/linux/math.h:37:22: note: in expansion of macro '__KERNEL_DIV_ROUND_UP'
37 | #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
| ^~~~~~~~~~~~~~~~~~~~~
drivers/net/ethernet/microchip/sparx5/sparx5_tc_flower.c:732:38: note: in expansion of macro 'SPX5_SDLB_GROUP_RATE_MAX'
732 | if (pol->rate > DIV_ROUND_UP(SPX5_SDLB_GROUP_RATE_MAX, 1000)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~
drivers/net/ethernet/microchip/sparx5/sparx5_main.h:435:34: error: overflow in conversion from 'long long unsigned int' to 'long int' changes value from '25000000000' to '-769803776' [-Werror=overflow]
...
drivers/net/ethernet/microchip/sparx5/sparx5_main.h:435:34: error: conversion from 'long long unsigned int' to 'unsigned int' changes value from '25000000000' to '3525163520' [-Werror=overflow]
...
drivers/net/ethernet/microchip/sparx5/sparx5_main.h:435:34: error: overflow in conversion from 'long long unsigned int' to 'int' changes value from '25000000000' to '-769803776' [-Werror=overflow]
Plus typos fixes below passes allyesconfig for s390, 32-bit x86 and arm.
static inline unsigned long long __div_round_up_ull(unsigned long long n, unsigned long d)
{
#ifdef CONFIG_32BIT
if (!n)
- return 0
- do_div(n-1, d);
- return n+1;
+ return 0;
+ n--;
+ do_div(n, d);
+ return n + 1;
#else
return __const_div_round_up(n,d);
#endif
next prev parent reply other threads:[~2023-10-25 15:05 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-24 16:18 [PATCH v4 0/3] Fix clock division overflow problem Sebastian Reichel
2023-10-24 16:18 ` [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW Sebastian Reichel
2023-10-24 16:26 ` David Laight
2023-10-24 19:32 ` Linus Torvalds
2023-10-24 22:53 ` Linus Torvalds
2023-10-25 8:03 ` Rasmus Villemoes
2023-10-25 17:37 ` Linus Torvalds
2023-10-25 8:38 ` David Laight
2023-10-25 17:41 ` Linus Torvalds
2023-10-26 8:41 ` David Laight
2023-10-25 15:05 ` Vasily Gorbik [this message]
2023-10-25 17:43 ` Linus Torvalds
2023-10-26 8:57 ` David Laight
2023-10-26 16:54 ` Linus Torvalds
2023-10-27 7:24 ` David Laight
2023-10-25 17:36 ` Sebastian Reichel
2023-10-25 17:28 ` Sebastian Reichel
2023-10-24 16:18 ` [PATCH v4 2/3] clk: divider: Fix divisor masking on 64 bit platforms Sebastian Reichel
2023-10-24 16:18 ` [PATCH v4 3/3] clk: composite: replace open-coded abs_diff() Sebastian Reichel
2023-10-24 16:23 ` Andy Shevchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=your-ad-here.call-01698246313-ext-3263@work.hours \
--to=gor@linux.ibm.com \
--cc=David.Laight@aculab.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=kernel@collabora.com \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mturquette@baylibre.com \
--cc=sboyd@kernel.org \
--cc=schnelle@linux.ibm.com \
--cc=sebastian.reichel@collabora.com \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox