Commit 6046b49b authored by Thomas Zimmermann's avatar Thomas Zimmermann
Browse files

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: default avatarThomas Zimmermann <tzimmermann@suse.de>
Reviewed-by: default avatarJavier Martinez Canillas <javierm@redhat.com>
Link: https://lore.kernel.org/r/20250410083834.10810-3-tzimmermann@suse.de
parent 314c45e3
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
// 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");
+9 −0
Original line number Diff line number Diff line
@@ -13,6 +13,15 @@
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
 */
+4 −25
Original line number Diff line number Diff line
@@ -33,28 +33,6 @@
#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,
+2 −10
Original line number Diff line number Diff line
@@ -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,
+2 −12
Original line number Diff line number Diff line
@@ -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 *
Loading