mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] fix mult_frac() multiple argument evaluation bug
@ 2023-05-19 20:24 Alexey Dobriyan
  2023-05-19 22:36 ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Alexey Dobriyan @ 2023-05-19 20:24 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel

mult_frac() evaluates _all_ arguments multiple times in the body.

Clarify comment while I'm at it.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 include/linux/math.h |   22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

--- a/include/linux/math.h
+++ b/include/linux/math.h
@@ -118,17 +118,17 @@ __STRUCT_FRACT(s32)
 __STRUCT_FRACT(u32)
 #undef __STRUCT_FRACT
 
-/*
- * Multiplies an integer by a fraction, while avoiding unnecessary
- * overflow or loss of precision.
- */
-#define mult_frac(x, numer, denom)(			\
-{							\
-	typeof(x) quot = (x) / (denom);			\
-	typeof(x) rem  = (x) % (denom);			\
-	(quot * (numer)) + ((rem * (numer)) / (denom));	\
-}							\
-)
+/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */
+#define mult_frac(x, n, d)	\
+({				\
+	typeof(x) x_ = (x);	\
+	typeof(n) n_ = (n);	\
+	typeof(d) d_ = (d);	\
+				\
+	typeof(x) q = x_ / d_;	\
+	typeof(x) r = x_ % d_;	\
+	q * n_ + r * n_ / d_;	\
+})
 
 #define sector_div(a, b) do_div(a, b)
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] fix mult_frac() multiple argument evaluation bug
  2023-05-19 20:24 [PATCH] fix mult_frac() multiple argument evaluation bug Alexey Dobriyan
@ 2023-05-19 22:36 ` Andrew Morton
  2023-05-20 18:24   ` Alexey Dobriyan
  2023-05-20 18:25   ` [PATCH v2] " Alexey Dobriyan
  0 siblings, 2 replies; 4+ messages in thread
From: Andrew Morton @ 2023-05-19 22:36 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: linux-kernel

On Fri, 19 May 2023 23:24:54 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote:

> mult_frac() evaluates _all_ arguments multiple times in the body.
> 
> --- a/include/linux/math.h
> +++ b/include/linux/math.h
> @@ -118,17 +118,17 @@ __STRUCT_FRACT(s32)
>  __STRUCT_FRACT(u32)
>  #undef __STRUCT_FRACT
>  
> -/*
> - * Multiplies an integer by a fraction, while avoiding unnecessary
> - * overflow or loss of precision.
> - */
> -#define mult_frac(x, numer, denom)(			\
> -{							\
> -	typeof(x) quot = (x) / (denom);			\
> -	typeof(x) rem  = (x) % (denom);			\
> -	(quot * (numer)) + ((rem * (numer)) / (denom));	\
> -}							\
> -)
> +/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */
> +#define mult_frac(x, n, d)	\
> +({				\
> +	typeof(x) x_ = (x);	\
> +	typeof(n) n_ = (n);	\
> +	typeof(d) d_ = (d);	\
> +				\
> +	typeof(x) q = x_ / d_;	\
> +	typeof(x) r = x_ % d_;	\
> +	q * n_ + r * n_ / d_;	\
> +})
>  

I like, but the compiler doesn't.  x86_64 allmodconfig, gcc-12.2.0.

In file included from ./include/linux/math64.h:6,
                 from ./include/linux/time.h:6,
                 from ./include/linux/ktime.h:24,
                 from ./include/linux/timer.h:6,
                 from drivers/target/target_core_iblock.c:16:
drivers/target/target_core_iblock.c: In function 'iblock_configure_device':
drivers/target/target_core_iblock.c:127:73: error: passing argument 1 of 'queue_max_hw_sectors' makes pointer from integer without a cast [-Werror=int-conversion]
  127 |         dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q),
      |                                                                         ^
      |                                                                         |
      |                                                                         unsigned int
./include/linux/math.h:129:16: note: in definition of macro 'mult_frac'
  129 |         typeof(x) r = x_ % d_;  \
      |                ^
In file included from drivers/target/target_core_iblock.c:18:
./include/linux/blkdev.h:1112:77: note: expected 'const struct request_queue *' but argument is of type 'unsigned int'
 1112 | static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
      |                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
cc1: all warnings being treated as errors
make[3]: *** [scripts/Makefile.build:252: drivers/target/target_core_iblock.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [scripts/Makefile.build:494: drivers/target] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [scripts/Makefile.build:494: drivers] Error 2
make: *** [Makefile:2026: .] Error 2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] fix mult_frac() multiple argument evaluation bug
  2023-05-19 22:36 ` Andrew Morton
@ 2023-05-20 18:24   ` Alexey Dobriyan
  2023-05-20 18:25   ` [PATCH v2] " Alexey Dobriyan
  1 sibling, 0 replies; 4+ messages in thread
From: Alexey Dobriyan @ 2023-05-20 18:24 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

On Fri, May 19, 2023 at 03:36:22PM -0700, Andrew Morton wrote:
> On Fri, 19 May 2023 23:24:54 +0300 Alexey Dobriyan <adobriyan@gmail.com> wrote:
> 
> > mult_frac() evaluates _all_ arguments multiple times in the body.
> > 
> > --- a/include/linux/math.h
> > +++ b/include/linux/math.h
> > @@ -118,17 +118,17 @@ __STRUCT_FRACT(s32)
> >  __STRUCT_FRACT(u32)
> >  #undef __STRUCT_FRACT
> >  
> > -/*
> > - * Multiplies an integer by a fraction, while avoiding unnecessary
> > - * overflow or loss of precision.
> > - */
> > -#define mult_frac(x, numer, denom)(			\
> > -{							\
> > -	typeof(x) quot = (x) / (denom);			\
> > -	typeof(x) rem  = (x) % (denom);			\
> > -	(quot * (numer)) + ((rem * (numer)) / (denom));	\
> > -}							\
> > -)
> > +/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */
> > +#define mult_frac(x, n, d)	\
> > +({				\
> > +	typeof(x) x_ = (x);	\
> > +	typeof(n) n_ = (n);	\
> > +	typeof(d) d_ = (d);	\
> > +				\
> > +	typeof(x) q = x_ / d_;	\
> > +	typeof(x) r = x_ % d_;	\
> > +	q * n_ + r * n_ / d_;	\
> > +})
> >  
> 
> I like, but the compiler doesn't.  x86_64 allmodconfig, gcc-12.2.0.
> 
> In file included from ./include/linux/math64.h:6,
>                  from ./include/linux/time.h:6,
>                  from ./include/linux/ktime.h:24,
>                  from ./include/linux/timer.h:6,
>                  from drivers/target/target_core_iblock.c:16:
> drivers/target/target_core_iblock.c: In function 'iblock_configure_device':
> drivers/target/target_core_iblock.c:127:73: error: passing argument 1 of 'queue_max_hw_sectors' makes pointer from integer without a cast [-Werror=int-conversion]
>   127 |         dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q),
>       |                                                                         ^
>       |                                                                         |
>       |                                                                         unsigned int
> ./include/linux/math.h:129:16: note: in definition of macro 'mult_frac'
>   129 |         typeof(x) r = x_ % d_;  \
>       |                ^
> In file included from drivers/target/target_core_iblock.c:18:
> ./include/linux/blkdev.h:1112:77: note: expected 'const struct request_queue *' but argument is of type 'unsigned int'
>  1112 | static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
>       |                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

In my defence, original macro has the same bug:

	#define M(a)			\
	({				\
		typeof(a) b = ...;	\
		typeof(a) c = ...;	\

Now, first typeof can use f(b) because second b doesn't exist yet.
But second typeof can not because second b already exists with potentially
incompatible type.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] fix mult_frac() multiple argument evaluation bug
  2023-05-19 22:36 ` Andrew Morton
  2023-05-20 18:24   ` Alexey Dobriyan
@ 2023-05-20 18:25   ` Alexey Dobriyan
  1 sibling, 0 replies; 4+ messages in thread
From: Alexey Dobriyan @ 2023-05-20 18:25 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel

mult_frac() evaluates _all_ arguments multiple times in the body.

Clarify comment while I'm at it.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 include/linux/math.h |   22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

--- a/include/linux/math.h
+++ b/include/linux/math.h
@@ -118,17 +118,17 @@ __STRUCT_FRACT(s32)
 __STRUCT_FRACT(u32)
 #undef __STRUCT_FRACT
 
-/*
- * Multiplies an integer by a fraction, while avoiding unnecessary
- * overflow or loss of precision.
- */
-#define mult_frac(x, numer, denom)(			\
-{							\
-	typeof(x) quot = (x) / (denom);			\
-	typeof(x) rem  = (x) % (denom);			\
-	(quot * (numer)) + ((rem * (numer)) / (denom));	\
-}							\
-)
+/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */
+#define mult_frac(x, n, d)	\
+({				\
+	typeof(x) x_ = (x);	\
+	typeof(n) n_ = (n);	\
+	typeof(d) d_ = (d);	\
+				\
+	typeof(x_) q = x_ / d_;	\
+	typeof(x_) r = x_ % d_;	\
+	q * n_ + r * n_ / d_;	\
+})
 
 #define sector_div(a, b) do_div(a, b)
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-05-20 18:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-19 20:24 [PATCH] fix mult_frac() multiple argument evaluation bug Alexey Dobriyan
2023-05-19 22:36 ` Andrew Morton
2023-05-20 18:24   ` Alexey Dobriyan
2023-05-20 18:25   ` [PATCH v2] " Alexey Dobriyan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®