drm/sysfb: Share helpers for integer validation
authorThomas Zimmermann <tzimmermann@suse.de>
Thu, 10 Apr 2025 08:37:24 +0000 (10:37 +0200)
committerThomas Zimmermann <tzimmermann@suse.de>
Mon, 14 Apr 2025 08:16:13 +0000 (10:16 +0200)
Provide sysfb helpers for validating framebuffer integer values
against limits. Update drivers. If a driver did not specify a limit
for a certain value, use INT_MAX.

v2:
- declare module information near EOF (Javier)

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
Link: https://lore.kernel.org/r/20250410083834.10810-3-tzimmermann@suse.de
drivers/gpu/drm/sysfb/drm_sysfb.c
drivers/gpu/drm/sysfb/drm_sysfb_helper.h
drivers/gpu/drm/sysfb/efidrm.c
drivers/gpu/drm/sysfb/ofdrm.c
drivers/gpu/drm/sysfb/simpledrm.c
drivers/gpu/drm/sysfb/vesadrm.c

index c083d21fd9cabaa3616fe7ef6be23b919b750481..308f82153b1582c53d0680d907454a7136a3e7fb 100644 (file)
@@ -1,8 +1,35 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include <linux/export.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 
+#include <drm/drm_print.h>
+
 #include "drm_sysfb_helper.h"
 
+int drm_sysfb_get_validated_int(struct drm_device *dev, const char *name,
+                               u64 value, u32 max)
+{
+       if (value > min(max, INT_MAX)) {
+               drm_warn(dev, "%s of %llu exceeds maximum of %u\n", name, value, max);
+               return -EINVAL;
+       }
+       return value;
+}
+EXPORT_SYMBOL(drm_sysfb_get_validated_int);
+
+int drm_sysfb_get_validated_int0(struct drm_device *dev, const char *name,
+                                u64 value, u32 max)
+{
+       if (!value) {
+               drm_warn(dev, "%s of 0 not allowed\n", name);
+               return -EINVAL;
+       }
+       return drm_sysfb_get_validated_int(dev, name, value, max);
+}
+EXPORT_SYMBOL(drm_sysfb_get_validated_int0);
+
 MODULE_DESCRIPTION("Helpers for DRM sysfb drivers");
 MODULE_LICENSE("GPL");
index ee94d6199b601cebbcca6d7da0009a1ad634a78c..1697cf7ace973c86a0016217ed271e1ebd586504 100644 (file)
 struct drm_format_info;
 struct drm_scanout_buffer;
 
+/*
+ * Input parsing
+ */
+
+int drm_sysfb_get_validated_int(struct drm_device *dev, const char *name,
+                               u64 value, u32 max);
+int drm_sysfb_get_validated_int0(struct drm_device *dev, const char *name,
+                                u64 value, u32 max);
+
 /*
  * Display modes
  */
index 3cfd5d2cbf48c2e6aaca56f83f7914da425d84ff..e3c7c930e5ad0a8a01deb3fdb6f083f4fe09a834 100644 (file)
 #define DRIVER_MAJOR   1
 #define DRIVER_MINOR   0
 
-static int efidrm_get_validated_int(struct drm_device *dev, const char *name,
-                                   u64 value, u32 max)
-{
-       if (max > INT_MAX)
-               max = INT_MAX;
-       if (value > max) {
-               drm_err(dev, "%s of %llu exceeds maximum of %u\n", name, value, max);
-               return -EINVAL;
-       }
-       return value;
-}
-
-static int efidrm_get_validated_int0(struct drm_device *dev, const char *name,
-                                    u64 value, u32 max)
-{
-       if (!value) {
-               drm_err(dev, "%s of 0 not allowed\n", name);
-               return -EINVAL;
-       }
-       return efidrm_get_validated_int(dev, name, value, max);
-}
-
 static s64 efidrm_get_validated_size0(struct drm_device *dev, const char *name,
                                      u64 value, u64 max)
 {
@@ -70,12 +48,12 @@ static s64 efidrm_get_validated_size0(struct drm_device *dev, const char *name,
 
 static int efidrm_get_width_si(struct drm_device *dev, const struct screen_info *si)
 {
-       return efidrm_get_validated_int0(dev, "width", si->lfb_width, U16_MAX);
+       return drm_sysfb_get_validated_int0(dev, "width", si->lfb_width, U16_MAX);
 }
 
 static int efidrm_get_height_si(struct drm_device *dev, const struct screen_info *si)
 {
-       return efidrm_get_validated_int0(dev, "height", si->lfb_height, U16_MAX);
+       return drm_sysfb_get_validated_int0(dev, "height", si->lfb_height, U16_MAX);
 }
 
 static struct resource *efidrm_get_memory_si(struct drm_device *dev,
@@ -102,7 +80,8 @@ static int efidrm_get_stride_si(struct drm_device *dev, const struct screen_info
        if (!lfb_linelength)
                lfb_linelength = drm_format_info_min_pitch(format, 0, width);
 
-       return efidrm_get_validated_int0(dev, "stride", lfb_linelength, div64_u64(size, height));
+       return drm_sysfb_get_validated_int0(dev, "stride", lfb_linelength,
+                                           div64_u64(size, height));
 }
 
 static u64 efidrm_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
index 86c1a0c80ceb3f2b28adf6a6877a5a9bec6c2bc7..fddfe8bea9f7f3d60cb412153c0b52a98c778a62 100644 (file)
@@ -78,20 +78,12 @@ enum ofdrm_model {
 
 static int display_get_validated_int(struct drm_device *dev, const char *name, uint32_t value)
 {
-       if (value > INT_MAX) {
-               drm_err(dev, "invalid framebuffer %s of %u\n", name, value);
-               return -EINVAL;
-       }
-       return (int)value;
+       return drm_sysfb_get_validated_int(dev, name, value, INT_MAX);
 }
 
 static int display_get_validated_int0(struct drm_device *dev, const char *name, uint32_t value)
 {
-       if (!value) {
-               drm_err(dev, "invalid framebuffer %s of %u\n", name, value);
-               return -EINVAL;
-       }
-       return display_get_validated_int(dev, name, value);
+       return drm_sysfb_get_validated_int0(dev, name, value, INT_MAX);
 }
 
 static const struct drm_format_info *display_get_validated_format(struct drm_device *dev,
index f37b1994de71f2c5272f929899027c307cbdf65a..a1c3119330deffc9e122b83941f3697e5b87f277 100644 (file)
@@ -42,24 +42,14 @@ static int
 simplefb_get_validated_int(struct drm_device *dev, const char *name,
                           uint32_t value)
 {
-       if (value > INT_MAX) {
-               drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
-                       name, value);
-               return -EINVAL;
-       }
-       return (int)value;
+       return drm_sysfb_get_validated_int(dev, name, value, INT_MAX);
 }
 
 static int
 simplefb_get_validated_int0(struct drm_device *dev, const char *name,
                            uint32_t value)
 {
-       if (!value) {
-               drm_err(dev, "simplefb: invalid framebuffer %s of %u\n",
-                       name, value);
-               return -EINVAL;
-       }
-       return simplefb_get_validated_int(dev, name, value);
+       return drm_sysfb_get_validated_int0(dev, name, value, INT_MAX);
 }
 
 static const struct drm_format_info *
index 9cc50e3072ead81bf050f70602876a97097a18ec..d87ff77be20dec01a94010cba1323dc359edbd11 100644 (file)
 
 #define VESADRM_GAMMA_LUT_SIZE 256
 
-static int vesadrm_get_validated_int(struct drm_device *dev, const char *name,
-                                    u64 value, u32 max)
-{
-       if (max > INT_MAX)
-               max = INT_MAX;
-       if (value > max) {
-               drm_err(dev, "%s of %llu exceeds maximum of %u\n", name, value, max);
-               return -EINVAL;
-       }
-       return value;
-}
-
-static int vesadrm_get_validated_int0(struct drm_device *dev, const char *name,
-                                     u64 value, u32 max)
-{
-       if (!value) {
-               drm_err(dev, "%s of 0 not allowed\n", name);
-               return -EINVAL;
-       }
-       return vesadrm_get_validated_int(dev, name, value, max);
-}
-
 static s64 vesadrm_get_validated_size0(struct drm_device *dev, const char *name,
                                       u64 value, u64 max)
 {
@@ -73,12 +51,12 @@ static s64 vesadrm_get_validated_size0(struct drm_device *dev, const char *name,
 
 static int vesadrm_get_width_si(struct drm_device *dev, const struct screen_info *si)
 {
-       return vesadrm_get_validated_int0(dev, "width", si->lfb_width, U16_MAX);
+       return drm_sysfb_get_validated_int0(dev, "width", si->lfb_width, U16_MAX);
 }
 
 static int vesadrm_get_height_si(struct drm_device *dev, const struct screen_info *si)
 {
-       return vesadrm_get_validated_int0(dev, "height", si->lfb_height, U16_MAX);
+       return drm_sysfb_get_validated_int0(dev, "height", si->lfb_height, U16_MAX);
 }
 
 static struct resource *vesadrm_get_memory_si(struct drm_device *dev,
@@ -105,7 +83,8 @@ static int vesadrm_get_stride_si(struct drm_device *dev, const struct screen_inf
        if (!lfb_linelength)
                lfb_linelength = drm_format_info_min_pitch(format, 0, width);
 
-       return vesadrm_get_validated_int0(dev, "stride", lfb_linelength, div64_u64(size, height));
+       return drm_sysfb_get_validated_int0(dev, "stride", lfb_linelength,
+                                           div64_u64(size, height));
 }
 
 static u64 vesadrm_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,