Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/sysfb: Share helpers for integer validation

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

+48 -72
+27
drivers/gpu/drm/sysfb/drm_sysfb.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 2 3 + #include <linux/export.h> 4 + #include <linux/limits.h> 5 + #include <linux/minmax.h> 3 6 #include <linux/module.h> 4 7 8 + #include <drm/drm_print.h> 9 + 5 10 #include "drm_sysfb_helper.h" 11 + 12 + int drm_sysfb_get_validated_int(struct drm_device *dev, const char *name, 13 + u64 value, u32 max) 14 + { 15 + if (value > min(max, INT_MAX)) { 16 + drm_warn(dev, "%s of %llu exceeds maximum of %u\n", name, value, max); 17 + return -EINVAL; 18 + } 19 + return value; 20 + } 21 + EXPORT_SYMBOL(drm_sysfb_get_validated_int); 22 + 23 + int drm_sysfb_get_validated_int0(struct drm_device *dev, const char *name, 24 + u64 value, u32 max) 25 + { 26 + if (!value) { 27 + drm_warn(dev, "%s of 0 not allowed\n", name); 28 + return -EINVAL; 29 + } 30 + return drm_sysfb_get_validated_int(dev, name, value, max); 31 + } 32 + EXPORT_SYMBOL(drm_sysfb_get_validated_int0); 6 33 7 34 MODULE_DESCRIPTION("Helpers for DRM sysfb drivers"); 8 35 MODULE_LICENSE("GPL");
+9
drivers/gpu/drm/sysfb/drm_sysfb_helper.h
··· 14 14 struct drm_scanout_buffer; 15 15 16 16 /* 17 + * Input parsing 18 + */ 19 + 20 + int drm_sysfb_get_validated_int(struct drm_device *dev, const char *name, 21 + u64 value, u32 max); 22 + int drm_sysfb_get_validated_int0(struct drm_device *dev, const char *name, 23 + u64 value, u32 max); 24 + 25 + /* 17 26 * Display modes 18 27 */ 19 28
+4 -25
drivers/gpu/drm/sysfb/efidrm.c
··· 33 33 #define DRIVER_MAJOR 1 34 34 #define DRIVER_MINOR 0 35 35 36 - static int efidrm_get_validated_int(struct drm_device *dev, const char *name, 37 - u64 value, u32 max) 38 - { 39 - if (max > INT_MAX) 40 - max = INT_MAX; 41 - if (value > max) { 42 - drm_err(dev, "%s of %llu exceeds maximum of %u\n", name, value, max); 43 - return -EINVAL; 44 - } 45 - return value; 46 - } 47 - 48 - static int efidrm_get_validated_int0(struct drm_device *dev, const char *name, 49 - u64 value, u32 max) 50 - { 51 - if (!value) { 52 - drm_err(dev, "%s of 0 not allowed\n", name); 53 - return -EINVAL; 54 - } 55 - return efidrm_get_validated_int(dev, name, value, max); 56 - } 57 - 58 36 static s64 efidrm_get_validated_size0(struct drm_device *dev, const char *name, 59 37 u64 value, u64 max) 60 38 { ··· 48 70 49 71 static int efidrm_get_width_si(struct drm_device *dev, const struct screen_info *si) 50 72 { 51 - return efidrm_get_validated_int0(dev, "width", si->lfb_width, U16_MAX); 73 + return drm_sysfb_get_validated_int0(dev, "width", si->lfb_width, U16_MAX); 52 74 } 53 75 54 76 static int efidrm_get_height_si(struct drm_device *dev, const struct screen_info *si) 55 77 { 56 - return efidrm_get_validated_int0(dev, "height", si->lfb_height, U16_MAX); 78 + return drm_sysfb_get_validated_int0(dev, "height", si->lfb_height, U16_MAX); 57 79 } 58 80 59 81 static struct resource *efidrm_get_memory_si(struct drm_device *dev, ··· 80 102 if (!lfb_linelength) 81 103 lfb_linelength = drm_format_info_min_pitch(format, 0, width); 82 104 83 - return efidrm_get_validated_int0(dev, "stride", lfb_linelength, div64_u64(size, height)); 105 + return drm_sysfb_get_validated_int0(dev, "stride", lfb_linelength, 106 + div64_u64(size, height)); 84 107 } 85 108 86 109 static u64 efidrm_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,
+2 -10
drivers/gpu/drm/sysfb/ofdrm.c
··· 78 78 79 79 static int display_get_validated_int(struct drm_device *dev, const char *name, uint32_t value) 80 80 { 81 - if (value > INT_MAX) { 82 - drm_err(dev, "invalid framebuffer %s of %u\n", name, value); 83 - return -EINVAL; 84 - } 85 - return (int)value; 81 + return drm_sysfb_get_validated_int(dev, name, value, INT_MAX); 86 82 } 87 83 88 84 static int display_get_validated_int0(struct drm_device *dev, const char *name, uint32_t value) 89 85 { 90 - if (!value) { 91 - drm_err(dev, "invalid framebuffer %s of %u\n", name, value); 92 - return -EINVAL; 93 - } 94 - return display_get_validated_int(dev, name, value); 86 + return drm_sysfb_get_validated_int0(dev, name, value, INT_MAX); 95 87 } 96 88 97 89 static const struct drm_format_info *display_get_validated_format(struct drm_device *dev,
+2 -12
drivers/gpu/drm/sysfb/simpledrm.c
··· 42 42 simplefb_get_validated_int(struct drm_device *dev, const char *name, 43 43 uint32_t value) 44 44 { 45 - if (value > INT_MAX) { 46 - drm_err(dev, "simplefb: invalid framebuffer %s of %u\n", 47 - name, value); 48 - return -EINVAL; 49 - } 50 - return (int)value; 45 + return drm_sysfb_get_validated_int(dev, name, value, INT_MAX); 51 46 } 52 47 53 48 static int 54 49 simplefb_get_validated_int0(struct drm_device *dev, const char *name, 55 50 uint32_t value) 56 51 { 57 - if (!value) { 58 - drm_err(dev, "simplefb: invalid framebuffer %s of %u\n", 59 - name, value); 60 - return -EINVAL; 61 - } 62 - return simplefb_get_validated_int(dev, name, value); 52 + return drm_sysfb_get_validated_int0(dev, name, value, INT_MAX); 63 53 } 64 54 65 55 static const struct drm_format_info *
+4 -25
drivers/gpu/drm/sysfb/vesadrm.c
··· 36 36 37 37 #define VESADRM_GAMMA_LUT_SIZE 256 38 38 39 - static int vesadrm_get_validated_int(struct drm_device *dev, const char *name, 40 - u64 value, u32 max) 41 - { 42 - if (max > INT_MAX) 43 - max = INT_MAX; 44 - if (value > max) { 45 - drm_err(dev, "%s of %llu exceeds maximum of %u\n", name, value, max); 46 - return -EINVAL; 47 - } 48 - return value; 49 - } 50 - 51 - static int vesadrm_get_validated_int0(struct drm_device *dev, const char *name, 52 - u64 value, u32 max) 53 - { 54 - if (!value) { 55 - drm_err(dev, "%s of 0 not allowed\n", name); 56 - return -EINVAL; 57 - } 58 - return vesadrm_get_validated_int(dev, name, value, max); 59 - } 60 - 61 39 static s64 vesadrm_get_validated_size0(struct drm_device *dev, const char *name, 62 40 u64 value, u64 max) 63 41 { ··· 51 73 52 74 static int vesadrm_get_width_si(struct drm_device *dev, const struct screen_info *si) 53 75 { 54 - return vesadrm_get_validated_int0(dev, "width", si->lfb_width, U16_MAX); 76 + return drm_sysfb_get_validated_int0(dev, "width", si->lfb_width, U16_MAX); 55 77 } 56 78 57 79 static int vesadrm_get_height_si(struct drm_device *dev, const struct screen_info *si) 58 80 { 59 - return vesadrm_get_validated_int0(dev, "height", si->lfb_height, U16_MAX); 81 + return drm_sysfb_get_validated_int0(dev, "height", si->lfb_height, U16_MAX); 60 82 } 61 83 62 84 static struct resource *vesadrm_get_memory_si(struct drm_device *dev, ··· 83 105 if (!lfb_linelength) 84 106 lfb_linelength = drm_format_info_min_pitch(format, 0, width); 85 107 86 - return vesadrm_get_validated_int0(dev, "stride", lfb_linelength, div64_u64(size, height)); 108 + return drm_sysfb_get_validated_int0(dev, "stride", lfb_linelength, 109 + div64_u64(size, height)); 87 110 } 88 111 89 112 static u64 vesadrm_get_visible_size_si(struct drm_device *dev, const struct screen_info *si,