git: c840d5cec260 - main - LinuxKPI: add kstrtoint_from_user() and DECLARE_FLEX_ARRAY()

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Mon, 14 Feb 2022 00:21:43 UTC
The branch main has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=c840d5cec2607932d077ffcfee7ccf0a6b50cdf3

commit c840d5cec2607932d077ffcfee7ccf0a6b50cdf3
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2022-02-09 11:58:40 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2022-02-14 00:20:41 +0000

    LinuxKPI: add kstrtoint_from_user() and DECLARE_FLEX_ARRAY()
    
    Add an implementation of kstrtoint_from_user() based on the other
    implementations and an attempt at DECLARE_FLEX_ARRAY() which works
    for the driver needing it.
    
    MFC after:      3 days
    Reviewed by:    hselasky
    Differential Revision: https://reviews.freebsd.org/D34231
---
 sys/compat/linuxkpi/common/include/linux/kernel.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/kernel.h b/sys/compat/linuxkpi/common/include/linux/kernel.h
index a84f577dd912..39520cd271ac 100644
--- a/sys/compat/linuxkpi/common/include/linux/kernel.h
+++ b/sys/compat/linuxkpi/common/include/linux/kernel.h
@@ -509,6 +509,21 @@ kstrtobool_from_user(const char __user *s, size_t count, bool *res)
 	return (kstrtobool(buf, res));
 }
 
+static inline int
+kstrtoint_from_user(const char __user *s, size_t count, unsigned int base,
+    int *p)
+{
+	char buf[36] = {};
+
+	if (count > (sizeof(buf) - 1))
+		count = (sizeof(buf) - 1);
+
+	if (copy_from_user(buf, s, count))
+		return (-EFAULT);
+
+	return (kstrtoint(buf, base, p));
+}
+
 static inline int
 kstrtou8_from_user(const char __user *s, size_t count, unsigned int base,
     u8 *p)
@@ -684,6 +699,9 @@ hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen)
 	return (0);
 }
 
+#define	DECLARE_FLEX_ARRAY(_t, _n)					\
+    struct { struct { } __dummy_ ## _n; _t _n[]; }
+
 /*
  * Checking if an option is defined would be easy if we could do CPP inside CPP.
  * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with.  In either