All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/msm: Avoid potential overflow in timeout_to_jiffies()
@ 2021-09-17  0:59 Marek Vasut
  2021-09-17  7:03   ` Arnd Bergmann
  2021-10-01 17:17 ` Dmitry Baryshkov
  0 siblings, 2 replies; 4+ messages in thread
From: Marek Vasut @ 2021-09-17  0:59 UTC (permalink / raw)
  To: linux-arm-msm
  Cc: freedreno, dri-devel, Marek Vasut, Arnd Bergmann, Jordan Crouse,
	Rob Clark, stable

The return type of ktime_divns() is s64. The timeout_to_jiffies() currently
assigns the result of this ktime_divns() to unsigned long, which on 32 bit
systems may overflow. Furthermore, the result of this function is sometimes
also passed to functions which expect signed long, dma_fence_wait_timeout()
is one such example.

Fix this by adjusting the type of remaining_jiffies to s64, so we do not
suffer overflow there, and return a value limited to range of 0..INT_MAX,
which is safe for all usecases of this timeout.

The above overflow can be triggered if userspace passes in too large timeout
value, larger than INT_MAX / HZ seconds. The kernel detects it and complains
about "schedule_timeout: wrong timeout value %lx" and generates a warning
backtrace.

Note that this fixes commit 6cedb8b377bb ("drm/msm: avoid using 'timespec'"),
because the previously used timespec_to_jiffies() function returned unsigned
long instead of s64:
static inline unsigned long timespec_to_jiffies(const struct timespec *value)

Fixes: 6cedb8b377bb ("drm/msm: avoid using 'timespec'")
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Jordan Crouse <jcrouse@codeaurora.org>
Cc: Rob Clark <robdclark@chromium.org>
Cc: stable@vger.kernel.org # 5.6+
---
NOTE: This is related to Mesa MR
      https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12886
---
 drivers/gpu/drm/msm/msm_drv.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/msm/msm_drv.h b/drivers/gpu/drm/msm/msm_drv.h
index 0b2686b060c73..d96b254b8aa46 100644
--- a/drivers/gpu/drm/msm/msm_drv.h
+++ b/drivers/gpu/drm/msm/msm_drv.h
@@ -543,7 +543,7 @@ static inline int align_pitch(int width, int bpp)
 static inline unsigned long timeout_to_jiffies(const ktime_t *timeout)
 {
 	ktime_t now = ktime_get();
-	unsigned long remaining_jiffies;
+	s64 remaining_jiffies;
 
 	if (ktime_compare(*timeout, now) < 0) {
 		remaining_jiffies = 0;
@@ -552,7 +552,7 @@ static inline unsigned long timeout_to_jiffies(const ktime_t *timeout)
 		remaining_jiffies = ktime_divns(rem, NSEC_PER_SEC / HZ);
 	}
 
-	return remaining_jiffies;
+	return clamp(remaining_jiffies, 0LL, (s64)INT_MAX);
 }
 
 #endif /* __MSM_DRV_H__ */
-- 
2.33.0


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

* Re: [PATCH] drm/msm: Avoid potential overflow in timeout_to_jiffies()
  2021-09-17  0:59 [PATCH] drm/msm: Avoid potential overflow in timeout_to_jiffies() Marek Vasut
@ 2021-09-17  7:03   ` Arnd Bergmann
  2021-10-01 17:17 ` Dmitry Baryshkov
  1 sibling, 0 replies; 4+ messages in thread
From: Arnd Bergmann @ 2021-09-17  7:03 UTC (permalink / raw)
  To: Marek Vasut
  Cc: linux-arm-msm, freedreno, dri-devel, Arnd Bergmann,
	Jordan Crouse, Rob Clark, # 3.4.x

On Fri, Sep 17, 2021 at 2:59 AM Marek Vasut <marex@denx.de> wrote:
>
> The return type of ktime_divns() is s64. The timeout_to_jiffies() currently
> assigns the result of this ktime_divns() to unsigned long, which on 32 bit
> systems may overflow. Furthermore, the result of this function is sometimes
> also passed to functions which expect signed long, dma_fence_wait_timeout()
> is one such example.
>
> Fix this by adjusting the type of remaining_jiffies to s64, so we do not
> suffer overflow there, and return a value limited to range of 0..INT_MAX,
> which is safe for all usecases of this timeout.
>
> The above overflow can be triggered if userspace passes in too large timeout
> value, larger than INT_MAX / HZ seconds. The kernel detects it and complains
> about "schedule_timeout: wrong timeout value %lx" and generates a warning
> backtrace.
>
> Note that this fixes commit 6cedb8b377bb ("drm/msm: avoid using 'timespec'"),
> because the previously used timespec_to_jiffies() function returned unsigned
> long instead of s64:
> static inline unsigned long timespec_to_jiffies(const struct timespec *value)
>
> Fixes: 6cedb8b377bb ("drm/msm: avoid using 'timespec'")
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Jordan Crouse <jcrouse@codeaurora.org>
> Cc: Rob Clark <robdclark@chromium.org>
> Cc: stable@vger.kernel.org # 5.6+
> ---

Acked-by: Arnd Bergmann <arnd@arndb.de>

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

* Re: [PATCH] drm/msm: Avoid potential overflow in timeout_to_jiffies()
@ 2021-09-17  7:03   ` Arnd Bergmann
  0 siblings, 0 replies; 4+ messages in thread
From: Arnd Bergmann @ 2021-09-17  7:03 UTC (permalink / raw)
  To: Marek Vasut
  Cc: linux-arm-msm, freedreno, dri-devel, Arnd Bergmann,
	Jordan Crouse, Rob Clark, # 3.4.x

On Fri, Sep 17, 2021 at 2:59 AM Marek Vasut <marex@denx.de> wrote:
>
> The return type of ktime_divns() is s64. The timeout_to_jiffies() currently
> assigns the result of this ktime_divns() to unsigned long, which on 32 bit
> systems may overflow. Furthermore, the result of this function is sometimes
> also passed to functions which expect signed long, dma_fence_wait_timeout()
> is one such example.
>
> Fix this by adjusting the type of remaining_jiffies to s64, so we do not
> suffer overflow there, and return a value limited to range of 0..INT_MAX,
> which is safe for all usecases of this timeout.
>
> The above overflow can be triggered if userspace passes in too large timeout
> value, larger than INT_MAX / HZ seconds. The kernel detects it and complains
> about "schedule_timeout: wrong timeout value %lx" and generates a warning
> backtrace.
>
> Note that this fixes commit 6cedb8b377bb ("drm/msm: avoid using 'timespec'"),
> because the previously used timespec_to_jiffies() function returned unsigned
> long instead of s64:
> static inline unsigned long timespec_to_jiffies(const struct timespec *value)
>
> Fixes: 6cedb8b377bb ("drm/msm: avoid using 'timespec'")
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Jordan Crouse <jcrouse@codeaurora.org>
> Cc: Rob Clark <robdclark@chromium.org>
> Cc: stable@vger.kernel.org # 5.6+
> ---

Acked-by: Arnd Bergmann <arnd@arndb.de>

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

* Re: [PATCH] drm/msm: Avoid potential overflow in timeout_to_jiffies()
  2021-09-17  0:59 [PATCH] drm/msm: Avoid potential overflow in timeout_to_jiffies() Marek Vasut
  2021-09-17  7:03   ` Arnd Bergmann
@ 2021-10-01 17:17 ` Dmitry Baryshkov
  1 sibling, 0 replies; 4+ messages in thread
From: Dmitry Baryshkov @ 2021-10-01 17:17 UTC (permalink / raw)
  To: Marek Vasut, linux-arm-msm
  Cc: freedreno, dri-devel, Arnd Bergmann, Jordan Crouse, Rob Clark, stable

On 17/09/2021 03:59, Marek Vasut wrote:
> The return type of ktime_divns() is s64. The timeout_to_jiffies() currently
> assigns the result of this ktime_divns() to unsigned long, which on 32 bit
> systems may overflow. Furthermore, the result of this function is sometimes
> also passed to functions which expect signed long, dma_fence_wait_timeout()
> is one such example.
> 
> Fix this by adjusting the type of remaining_jiffies to s64, so we do not
> suffer overflow there, and return a value limited to range of 0..INT_MAX,
> which is safe for all usecases of this timeout.
> 
> The above overflow can be triggered if userspace passes in too large timeout
> value, larger than INT_MAX / HZ seconds. The kernel detects it and complains
> about "schedule_timeout: wrong timeout value %lx" and generates a warning
> backtrace.
> 
> Note that this fixes commit 6cedb8b377bb ("drm/msm: avoid using 'timespec'"),
> because the previously used timespec_to_jiffies() function returned unsigned
> long instead of s64:
> static inline unsigned long timespec_to_jiffies(const struct timespec *value)
> 
> Fixes: 6cedb8b377bb ("drm/msm: avoid using 'timespec'")
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Jordan Crouse <jcrouse@codeaurora.org>
> Cc: Rob Clark <robdclark@chromium.org>
> Cc: stable@vger.kernel.org # 5.6+

Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>

> ---
> NOTE: This is related to Mesa MR
>        https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12886
> ---
>   drivers/gpu/drm/msm/msm_drv.h | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/msm/msm_drv.h b/drivers/gpu/drm/msm/msm_drv.h
> index 0b2686b060c73..d96b254b8aa46 100644
> --- a/drivers/gpu/drm/msm/msm_drv.h
> +++ b/drivers/gpu/drm/msm/msm_drv.h
> @@ -543,7 +543,7 @@ static inline int align_pitch(int width, int bpp)
>   static inline unsigned long timeout_to_jiffies(const ktime_t *timeout)
>   {
>   	ktime_t now = ktime_get();
> -	unsigned long remaining_jiffies;
> +	s64 remaining_jiffies;
>   
>   	if (ktime_compare(*timeout, now) < 0) {
>   		remaining_jiffies = 0;
> @@ -552,7 +552,7 @@ static inline unsigned long timeout_to_jiffies(const ktime_t *timeout)
>   		remaining_jiffies = ktime_divns(rem, NSEC_PER_SEC / HZ);
>   	}
>   
> -	return remaining_jiffies;
> +	return clamp(remaining_jiffies, 0LL, (s64)INT_MAX);
>   }
>   
>   #endif /* __MSM_DRV_H__ */
> 


-- 
With best wishes
Dmitry

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

end of thread, other threads:[~2021-10-01 17:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-17  0:59 [PATCH] drm/msm: Avoid potential overflow in timeout_to_jiffies() Marek Vasut
2021-09-17  7:03 ` Arnd Bergmann
2021-09-17  7:03   ` Arnd Bergmann
2021-10-01 17:17 ` Dmitry Baryshkov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.