lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <baf075b2370a13cddd597a3d421b5f39290d87a4@intel.com>
Date: Tue, 27 Jan 2026 15:58:13 +0200
From: Jani Nikula <jani.nikula@...ux.intel.com>
To: Cristian Ciocaltea <cristian.ciocaltea@...labora.com>, Maarten Lankhorst
 <maarten.lankhorst@...ux.intel.com>, Maxime Ripard <mripard@...nel.org>,
 Thomas Zimmermann <tzimmermann@...e.de>, David Airlie <airlied@...il.com>,
 Simona Vetter <simona@...ll.ch>, Sandy Huang <hjc@...k-chips.com>, Heiko
 Stübner <heiko@...ech.de>, Andy Yan
 <andy.yan@...k-chips.com>, Louis
 Chauvet <louis.chauvet@...tlin.com>, Haneen Mohammed
 <hamohammed.sa@...il.com>, Melissa Wen <melissa.srw@...il.com>
Cc: Robert Mader <robert.mader@...labora.com>, kernel@...labora.com,
 dri-devel@...ts.freedesktop.org, linux-kernel@...r.kernel.org,
 linux-arm-kernel@...ts.infradead.org, linux-rockchip@...ts.infradead.org,
 Nícolas F. R. A. Prado <nfraprado@...labora.com>,
 Diederik de Haas
 <diederik@...ow-tech.com>, Andy Shevchenko
 <andriy.shevchenko@...ux.intel.com>
Subject: Re: [PATCH v5 1/4] uapi: Provide DIV_ROUND_CLOSEST()

On Tue, 27 Jan 2026, Cristian Ciocaltea <cristian.ciocaltea@...labora.com> wrote:
> Currently DIV_ROUND_CLOSEST() is only available for the kernel via
> include/linux/math.h.
>
> Expose it to userland as well by adding __KERNEL_DIV_ROUND_CLOSEST() as
> a common definition in uapi.
>
> Additionally, ensure it allows building ISO C applications by switching
> from the 'typeof' GNU extension to the ISO-friendly __typeof__.

I am not convinced that it's a good idea to make the implementation of
kernel DIV_ROUND_CLOSEST() part of the kernel UAPI, which is what this
change effectively does.

I'd at least like to get an ack from Andy Shevchenko first (Cc'd).


BR,
Jani.

> Reviewed-by: Nícolas F. R. A. Prado <nfraprado@...labora.com>
> Tested-by: Diederik de Haas <diederik@...ow-tech.com>
> Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@...labora.com>
> ---
>  include/linux/math.h       | 18 +-----------------
>  include/uapi/linux/const.h | 17 +++++++++++++++++
>  2 files changed, 18 insertions(+), 17 deletions(-)
>
> diff --git a/include/linux/math.h b/include/linux/math.h
> index 6dc1d1d32fbc..1e8fb3efbc8c 100644
> --- a/include/linux/math.h
> +++ b/include/linux/math.h
> @@ -89,23 +89,7 @@
>  }							\
>  )
>  
> -/*
> - * Divide positive or negative dividend by positive or negative divisor
> - * and round to closest integer. Result is undefined for negative
> - * divisors if the dividend variable type is unsigned and for negative
> - * dividends if the divisor variable type is unsigned.
> - */
> -#define DIV_ROUND_CLOSEST(x, divisor)(			\
> -{							\
> -	typeof(x) __x = x;				\
> -	typeof(divisor) __d = divisor;			\
> -	(((typeof(x))-1) > 0 ||				\
> -	 ((typeof(divisor))-1) > 0 ||			\
> -	 (((__x) > 0) == ((__d) > 0))) ?		\
> -		(((__x) + ((__d) / 2)) / (__d)) :	\
> -		(((__x) - ((__d) / 2)) / (__d));	\
> -}							\
> -)
> +#define DIV_ROUND_CLOSEST __KERNEL_DIV_ROUND_CLOSEST
>  /*
>   * Same as above but for u64 dividends. divisor must be a 32-bit
>   * number.
> diff --git a/include/uapi/linux/const.h b/include/uapi/linux/const.h
> index b8f629ef135f..471877322f47 100644
> --- a/include/uapi/linux/const.h
> +++ b/include/uapi/linux/const.h
> @@ -50,4 +50,21 @@
>  
>  #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
>  
> +/*
> + * Divide positive or negative dividend by positive or negative divisor
> + * and round to closest integer. Result is undefined for negative
> + * divisors if the dividend variable type is unsigned and for negative
> + * dividends if the divisor variable type is unsigned.
> + */
> +#define __KERNEL_DIV_ROUND_CLOSEST(x, divisor)(		\
> +{							\
> +	__typeof__(x) __x = x;				\
> +	__typeof__(divisor) __d = divisor;		\
> +	(((__typeof__(x))-1) > 0 ||			\
> +	 ((__typeof__(divisor))-1) > 0 ||		\
> +	 (((__x) > 0) == ((__d) > 0))) ?		\
> +		(((__x) + ((__d) / 2)) / (__d)) :	\
> +		(((__x) - ((__d) / 2)) / (__d));	\
> +}							\
> +)
>  #endif /* _UAPI_LINUX_CONST_H */

-- 
Jani Nikula, Intel

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ