drm/msm: Avoid potential overflow in timeout_to_jiffies()
Subsystems (4)
PATCH 1/1 drm/msm: Avoid potential overflow in timeout_to_jiffies()
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)
---
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(-)
drivers/gpu/drm/msm/msm_drv.h +2 -2
@@ -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__ */
Signature · 1 line
2.33.0
Quoted 21 lines · Marek Vasut
The return type of
ktime_divns()is s64. Thetimeout_to_jiffies()currently assigns the result of thisktime_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_jiffiesto 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 usedtimespec_to_jiffies()function returned unsigned long instead of s64:
static inline unsigned longtimespec_to_jiffies(const struct timespec *value)
Quoted 7 lines
Fixes 6cedb8b377bb ("drm/msm: avoid using 'timespec'")Signed-off-by Marek Vasut <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Jordan Crouse <[email protected]>Cc Rob Clark <[email protected]>Cc [email protected] # 5.6+---
Quoted 20 lines · Marek Vasut
The return type of
ktime_divns()is s64. Thetimeout_to_jiffies()currently assigns the result of thisktime_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_jiffiesto 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 usedtimespec_to_jiffies()function returned unsigned long instead of s64:
static inline unsigned longtimespec_to_jiffies(const struct timespec *value)
Quoted 6 lines
Fixes 6cedb8b377bb ("drm/msm: avoid using 'timespec'")Signed-off-by Marek Vasut <[email protected]>Cc Arnd Bergmann <[email protected]>Cc Jordan Crouse <[email protected]>Cc Rob Clark <[email protected]>Cc [email protected] # 5.6+
Quoted 30 lines
---
NOTE: This is related to Mesa MRhttps://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12886 --- drivers/gpu/drm/msm/msm_drv.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/gpu/drm/msm/msm_drv.h +2
-2@@ -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__ */
Signature · 2 lines
With best wishes Dmitry