]> www.infradead.org Git - users/hch/misc.git/commitdiff
overflow: add range_overflows() and range_end_overflows()
authorJani Nikula <jani.nikula@intel.com>
Fri, 29 Aug 2025 17:46:01 +0000 (20:46 +0300)
committerJani Nikula <jani.nikula@intel.com>
Mon, 8 Sep 2025 12:36:19 +0000 (15:36 +0300)
Move the range_overflows() and range_end_overflows() along with the _t
variants over from drm/i915 and drm/buddy to overflow.h.

Cc: Kees Cook <kees@kernel.org>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: linux-hardening@vger.kernel.org
Reviewed-by: Kees Cook <kees@kernel.org>
Reviewed-by: Jouni Högander <jouni.hogander@intel.com>
Acked-by: Thomas Zimmermann <tzimmermann@suse.de>
Link: https://lore.kernel.org/r/20250829174601.2163064-3-jani.nikula@intel.com
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
drivers/gpu/drm/i915/i915_utils.h
include/drm/drm_buddy.h
include/linux/overflow.h

index 968dae94153243b31e11e4b030fe3b0c9f0deb4d..eb4d43c40009916b845190ff1dbe46b4fcc875f2 100644 (file)
@@ -67,76 +67,6 @@ bool i915_error_injected(void);
                drm_err(&(i915)->drm, fmt, ##__VA_ARGS__); \
 })
 
-/**
- * range_overflows() - Check if a range is out of bounds
- * @start: Start of the range.
- * @size:  Size of the range.
- * @max:   Exclusive upper boundary.
- *
- * A strict check to determine if the range [@start, @start + @size) is
- * invalid with respect to the allowable range [0, @max). Any range
- * starting at or beyond @max is considered an overflow, even if @size is 0.
- *
- * Returns: true if the range is out of bounds.
- */
-#define range_overflows(start, size, max) ({ \
-       typeof(start) start__ = (start); \
-       typeof(size) size__ = (size); \
-       typeof(max) max__ = (max); \
-       (void)(&start__ == &size__); \
-       (void)(&start__ == &max__); \
-       start__ >= max__ || size__ > max__ - start__; \
-})
-
-/**
- * range_overflows_t() - Check if a range is out of bounds
- * @type:  Data type to use.
- * @start: Start of the range.
- * @size:  Size of the range.
- * @max:   Exclusive upper boundary.
- *
- * Same as range_overflows() but forcing the parameters to @type.
- *
- * Returns: true if the range is out of bounds.
- */
-#define range_overflows_t(type, start, size, max) \
-       range_overflows((type)(start), (type)(size), (type)(max))
-
-/**
- * range_end_overflows() - Check if a range's endpoint is out of bounds
- * @start: Start of the range.
- * @size:  Size of the range.
- * @max:   Exclusive upper boundary.
- *
- * Checks only if the endpoint of a range (@start + @size) exceeds @max.
- * Unlike range_overflows(), a zero-sized range at the boundary (@start == @max)
- * is not considered an overflow. Useful for iterator-style checks.
- *
- * Returns: true if the endpoint exceeds the boundary.
- */
-#define range_end_overflows(start, size, max) ({ \
-       typeof(start) start__ = (start); \
-       typeof(size) size__ = (size); \
-       typeof(max) max__ = (max); \
-       (void)(&start__ == &size__); \
-       (void)(&start__ == &max__); \
-       start__ > max__ || size__ > max__ - start__; \
-})
-
-/**
- * range_end_overflows_t() - Check if a range's endpoint is out of bounds
- * @type:  Data type to use.
- * @start: Start of the range.
- * @size:  Size of the range.
- * @max:   Exclusive upper boundary.
- *
- * Same as range_end_overflows() but forcing the parameters to @type.
- *
- * Returns: true if the endpoint exceeds the boundary.
- */
-#define range_end_overflows_t(type, start, size, max) \
-       range_end_overflows((type)(start), (type)(size), (type)(max))
-
 #define ptr_mask_bits(ptr, n) ({                                       \
        unsigned long __v = (unsigned long)(ptr);                       \
        (typeof(ptr))(__v & -BIT(n));                                   \
index 9689a7c5dd36b25d7ddefa0ec9fd4de210349829..236971681514bd5541c9bb601b317bf16a76d9a0 100644 (file)
 
 #include <drm/drm_print.h>
 
-#define range_overflows(start, size, max) ({ \
-       typeof(start) start__ = (start); \
-       typeof(size) size__ = (size); \
-       typeof(max) max__ = (max); \
-       (void)(&start__ == &size__); \
-       (void)(&start__ == &max__); \
-       start__ >= max__ || size__ > max__ - start__; \
-})
-
 #define DRM_BUDDY_RANGE_ALLOCATION             BIT(0)
 #define DRM_BUDDY_TOPDOWN_ALLOCATION           BIT(1)
 #define DRM_BUDDY_CONTIGUOUS_ALLOCATION                BIT(2)
index 154ed0dbb43f3e9d609dc46824b01798ca987ff7..725f95f7e4164b68cb110294e099f5815030f5e2 100644 (file)
@@ -238,6 +238,76 @@ static inline bool __must_check __must_check_overflow(bool overflow)
                              __overflows_type_constexpr(n, T), \
                              __overflows_type(n, T))
 
+/**
+ * range_overflows() - Check if a range is out of bounds
+ * @start: Start of the range.
+ * @size:  Size of the range.
+ * @max:   Exclusive upper boundary.
+ *
+ * A strict check to determine if the range [@start, @start + @size) is
+ * invalid with respect to the allowable range [0, @max). Any range
+ * starting at or beyond @max is considered an overflow, even if @size is 0.
+ *
+ * Returns: true if the range is out of bounds.
+ */
+#define range_overflows(start, size, max) ({ \
+       typeof(start) start__ = (start); \
+       typeof(size) size__ = (size); \
+       typeof(max) max__ = (max); \
+       (void)(&start__ == &size__); \
+       (void)(&start__ == &max__); \
+       start__ >= max__ || size__ > max__ - start__; \
+})
+
+/**
+ * range_overflows_t() - Check if a range is out of bounds
+ * @type:  Data type to use.
+ * @start: Start of the range.
+ * @size:  Size of the range.
+ * @max:   Exclusive upper boundary.
+ *
+ * Same as range_overflows() but forcing the parameters to @type.
+ *
+ * Returns: true if the range is out of bounds.
+ */
+#define range_overflows_t(type, start, size, max) \
+       range_overflows((type)(start), (type)(size), (type)(max))
+
+/**
+ * range_end_overflows() - Check if a range's endpoint is out of bounds
+ * @start: Start of the range.
+ * @size:  Size of the range.
+ * @max:   Exclusive upper boundary.
+ *
+ * Checks only if the endpoint of a range (@start + @size) exceeds @max.
+ * Unlike range_overflows(), a zero-sized range at the boundary (@start == @max)
+ * is not considered an overflow. Useful for iterator-style checks.
+ *
+ * Returns: true if the endpoint exceeds the boundary.
+ */
+#define range_end_overflows(start, size, max) ({ \
+       typeof(start) start__ = (start); \
+       typeof(size) size__ = (size); \
+       typeof(max) max__ = (max); \
+       (void)(&start__ == &size__); \
+       (void)(&start__ == &max__); \
+       start__ > max__ || size__ > max__ - start__; \
+})
+
+/**
+ * range_end_overflows_t() - Check if a range's endpoint is out of bounds
+ * @type:  Data type to use.
+ * @start: Start of the range.
+ * @size:  Size of the range.
+ * @max:   Exclusive upper boundary.
+ *
+ * Same as range_end_overflows() but forcing the parameters to @type.
+ *
+ * Returns: true if the endpoint exceeds the boundary.
+ */
+#define range_end_overflows_t(type, start, size, max) \
+       range_end_overflows((type)(start), (type)(size), (type)(max))
+
 /**
  * castable_to_type - like __same_type(), but also allows for casted literals
  *